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/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.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 <sha1.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <libgen.h>
38 #include <time.h>
39 #include <paths.h>
40 #include <regex.h>
41 #include <getopt.h>
42 #include <util.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_diff.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
55 #include "got_send.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
60 #include "got_dial.h"
61 #include "got_patch.h"
62 #include "got_sigs.h"
63 #include "got_date.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
72 static void
73 catch_sigint(int signo)
74 {
75 sigint_received = 1;
76 }
78 static void
79 catch_sigpipe(int signo)
80 {
81 sigpipe_received = 1;
82 }
85 struct got_cmd {
86 const char *cmd_name;
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
90 };
92 __dead static void usage(int, int);
93 __dead static void usage_init(void);
94 __dead static void usage_import(void);
95 __dead static void usage_clone(void);
96 __dead static void usage_fetch(void);
97 __dead static void usage_checkout(void);
98 __dead static void usage_update(void);
99 __dead static void usage_log(void);
100 __dead static void usage_diff(void);
101 __dead static void usage_blame(void);
102 __dead static void usage_tree(void);
103 __dead static void usage_status(void);
104 __dead static void usage_ref(void);
105 __dead static void usage_branch(void);
106 __dead static void usage_tag(void);
107 __dead static void usage_add(void);
108 __dead static void usage_remove(void);
109 __dead static void usage_patch(void);
110 __dead static void usage_revert(void);
111 __dead static void usage_commit(void);
112 __dead static void usage_send(void);
113 __dead static void usage_cherrypick(void);
114 __dead static void usage_backout(void);
115 __dead static void usage_rebase(void);
116 __dead static void usage_histedit(void);
117 __dead static void usage_integrate(void);
118 __dead static void usage_merge(void);
119 __dead static void usage_stage(void);
120 __dead static void usage_unstage(void);
121 __dead static void usage_cat(void);
122 __dead static void usage_info(void);
124 static const struct got_error* cmd_init(int, char *[]);
125 static const struct got_error* cmd_import(int, char *[]);
126 static const struct got_error* cmd_clone(int, char *[]);
127 static const struct got_error* cmd_fetch(int, char *[]);
128 static const struct got_error* cmd_checkout(int, char *[]);
129 static const struct got_error* cmd_update(int, char *[]);
130 static const struct got_error* cmd_log(int, char *[]);
131 static const struct got_error* cmd_diff(int, char *[]);
132 static const struct got_error* cmd_blame(int, char *[]);
133 static const struct got_error* cmd_tree(int, char *[]);
134 static const struct got_error* cmd_status(int, char *[]);
135 static const struct got_error* cmd_ref(int, char *[]);
136 static const struct got_error* cmd_branch(int, char *[]);
137 static const struct got_error* cmd_tag(int, char *[]);
138 static const struct got_error* cmd_add(int, char *[]);
139 static const struct got_error* cmd_remove(int, char *[]);
140 static const struct got_error* cmd_patch(int, char *[]);
141 static const struct got_error* cmd_revert(int, char *[]);
142 static const struct got_error* cmd_commit(int, char *[]);
143 static const struct got_error* cmd_send(int, char *[]);
144 static const struct got_error* cmd_cherrypick(int, char *[]);
145 static const struct got_error* cmd_backout(int, char *[]);
146 static const struct got_error* cmd_rebase(int, char *[]);
147 static const struct got_error* cmd_histedit(int, char *[]);
148 static const struct got_error* cmd_integrate(int, char *[]);
149 static const struct got_error* cmd_merge(int, char *[]);
150 static const struct got_error* cmd_stage(int, char *[]);
151 static const struct got_error* cmd_unstage(int, char *[]);
152 static const struct got_error* cmd_cat(int, char *[]);
153 static const struct got_error* cmd_info(int, char *[]);
155 static const struct got_cmd got_commands[] = {
156 { "init", cmd_init, usage_init, "" },
157 { "import", cmd_import, usage_import, "im" },
158 { "clone", cmd_clone, usage_clone, "cl" },
159 { "fetch", cmd_fetch, usage_fetch, "fe" },
160 { "checkout", cmd_checkout, usage_checkout, "co" },
161 { "update", cmd_update, usage_update, "up" },
162 { "log", cmd_log, usage_log, "" },
163 { "diff", cmd_diff, usage_diff, "di" },
164 { "blame", cmd_blame, usage_blame, "bl" },
165 { "tree", cmd_tree, usage_tree, "tr" },
166 { "status", cmd_status, usage_status, "st" },
167 { "ref", cmd_ref, usage_ref, "" },
168 { "branch", cmd_branch, usage_branch, "br" },
169 { "tag", cmd_tag, usage_tag, "" },
170 { "add", cmd_add, usage_add, "" },
171 { "remove", cmd_remove, usage_remove, "rm" },
172 { "patch", cmd_patch, usage_patch, "pa" },
173 { "revert", cmd_revert, usage_revert, "rv" },
174 { "commit", cmd_commit, usage_commit, "ci" },
175 { "send", cmd_send, usage_send, "se" },
176 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
177 { "backout", cmd_backout, usage_backout, "bo" },
178 { "rebase", cmd_rebase, usage_rebase, "rb" },
179 { "histedit", cmd_histedit, usage_histedit, "he" },
180 { "integrate", cmd_integrate, usage_integrate,"ig" },
181 { "merge", cmd_merge, usage_merge, "mg" },
182 { "stage", cmd_stage, usage_stage, "sg" },
183 { "unstage", cmd_unstage, usage_unstage, "ug" },
184 { "cat", cmd_cat, usage_cat, "" },
185 { "info", cmd_info, usage_info, "" },
186 };
188 static void
189 list_commands(FILE *fp)
191 size_t i;
193 fprintf(fp, "commands:");
194 for (i = 0; i < nitems(got_commands); i++) {
195 const struct got_cmd *cmd = &got_commands[i];
196 fprintf(fp, " %s", cmd->cmd_name);
198 fputc('\n', fp);
201 __dead static void
202 option_conflict(char a, char b)
204 errx(1, "-%c and -%c options are mutually exclusive", a, b);
207 int
208 main(int argc, char *argv[])
210 const struct got_cmd *cmd;
211 size_t i;
212 int ch;
213 int hflag = 0, Vflag = 0;
214 static const struct option longopts[] = {
215 { "version", no_argument, NULL, 'V' },
216 { NULL, 0, NULL, 0 }
217 };
219 setlocale(LC_CTYPE, "");
221 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
222 switch (ch) {
223 case 'h':
224 hflag = 1;
225 break;
226 case 'V':
227 Vflag = 1;
228 break;
229 default:
230 usage(hflag, 1);
231 /* NOTREACHED */
235 argc -= optind;
236 argv += optind;
237 optind = 1;
238 optreset = 1;
240 if (Vflag) {
241 got_version_print_str();
242 return 0;
245 if (argc <= 0)
246 usage(hflag, hflag ? 0 : 1);
248 signal(SIGINT, catch_sigint);
249 signal(SIGPIPE, catch_sigpipe);
251 for (i = 0; i < nitems(got_commands); i++) {
252 const struct got_error *error;
254 cmd = &got_commands[i];
256 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
257 strcmp(cmd->cmd_alias, argv[0]) != 0)
258 continue;
260 if (hflag)
261 cmd->cmd_usage();
263 error = cmd->cmd_main(argc, argv);
264 if (error && error->code != GOT_ERR_CANCELLED &&
265 error->code != GOT_ERR_PRIVSEP_EXIT &&
266 !(sigpipe_received &&
267 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
268 !(sigint_received &&
269 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
270 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
271 return 1;
274 return 0;
277 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
278 list_commands(stderr);
279 return 1;
282 __dead static void
283 usage(int hflag, int status)
285 FILE *fp = (status == 0) ? stdout : stderr;
287 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
288 getprogname());
289 if (hflag)
290 list_commands(fp);
291 exit(status);
294 static const struct got_error *
295 get_editor(char **abspath)
297 const struct got_error *err = NULL;
298 const char *editor;
300 *abspath = NULL;
302 editor = getenv("VISUAL");
303 if (editor == NULL)
304 editor = getenv("EDITOR");
306 if (editor) {
307 err = got_path_find_prog(abspath, editor);
308 if (err)
309 return err;
312 if (*abspath == NULL) {
313 *abspath = strdup("/bin/ed");
314 if (*abspath == NULL)
315 return got_error_from_errno("strdup");
318 return NULL;
321 static const struct got_error *
322 apply_unveil(const char *repo_path, int repo_read_only,
323 const char *worktree_path)
325 const struct got_error *err;
327 #ifdef PROFILE
328 if (unveil("gmon.out", "rwc") != 0)
329 return got_error_from_errno2("unveil", "gmon.out");
330 #endif
331 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
332 return got_error_from_errno2("unveil", repo_path);
334 if (worktree_path && unveil(worktree_path, "rwc") != 0)
335 return got_error_from_errno2("unveil", worktree_path);
337 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
338 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
340 err = got_privsep_unveil_exec_helpers();
341 if (err != NULL)
342 return err;
344 if (unveil(NULL, NULL) != 0)
345 return got_error_from_errno("unveil");
347 return NULL;
350 __dead static void
351 usage_init(void)
353 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
354 exit(1);
357 static const struct got_error *
358 cmd_init(int argc, char *argv[])
360 const struct got_error *error = NULL;
361 char *repo_path = NULL;
362 int ch;
364 while ((ch = getopt(argc, argv, "")) != -1) {
365 switch (ch) {
366 default:
367 usage_init();
368 /* NOTREACHED */
372 argc -= optind;
373 argv += optind;
375 #ifndef PROFILE
376 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
377 err(1, "pledge");
378 #endif
379 if (argc != 1)
380 usage_init();
382 repo_path = strdup(argv[0]);
383 if (repo_path == NULL)
384 return got_error_from_errno("strdup");
386 got_path_strip_trailing_slashes(repo_path);
388 error = got_path_mkdir(repo_path);
389 if (error &&
390 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
391 goto done;
393 error = apply_unveil(repo_path, 0, NULL);
394 if (error)
395 goto done;
397 error = got_repo_init(repo_path);
398 done:
399 free(repo_path);
400 return error;
403 __dead static void
404 usage_import(void)
406 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
407 "[-r repository-path] [-I pattern] path\n", getprogname());
408 exit(1);
411 static int
412 spawn_editor(const char *editor, const char *file)
414 pid_t pid;
415 sig_t sighup, sigint, sigquit;
416 int st = -1;
418 sighup = signal(SIGHUP, SIG_IGN);
419 sigint = signal(SIGINT, SIG_IGN);
420 sigquit = signal(SIGQUIT, SIG_IGN);
422 switch (pid = fork()) {
423 case -1:
424 goto doneediting;
425 case 0:
426 execl(editor, editor, file, (char *)NULL);
427 _exit(127);
430 while (waitpid(pid, &st, 0) == -1)
431 if (errno != EINTR)
432 break;
434 doneediting:
435 (void)signal(SIGHUP, sighup);
436 (void)signal(SIGINT, sigint);
437 (void)signal(SIGQUIT, sigquit);
439 if (!WIFEXITED(st)) {
440 errno = EINTR;
441 return -1;
444 return WEXITSTATUS(st);
447 static const struct got_error *
448 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
449 const char *initial_content, size_t initial_content_len,
450 int require_modification)
452 const struct got_error *err = NULL;
453 char *line = NULL;
454 size_t linesize = 0;
455 ssize_t linelen;
456 struct stat st, st2;
457 FILE *fp = NULL;
458 size_t len, logmsg_len;
459 char *initial_content_stripped = NULL, *buf = NULL, *s;
461 *logmsg = NULL;
463 if (stat(logmsg_path, &st) == -1)
464 return got_error_from_errno2("stat", logmsg_path);
466 if (spawn_editor(editor, logmsg_path) == -1)
467 return got_error_from_errno("failed spawning editor");
469 if (stat(logmsg_path, &st2) == -1)
470 return got_error_from_errno("stat");
472 if (require_modification &&
473 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
474 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
475 "no changes made to commit message, aborting");
477 /*
478 * Set up a stripped version of the initial content without comments
479 * and blank lines. We need this in order to check if the message
480 * has in fact been edited.
481 */
482 initial_content_stripped = malloc(initial_content_len + 1);
483 if (initial_content_stripped == NULL)
484 return got_error_from_errno("malloc");
485 initial_content_stripped[0] = '\0';
487 buf = strdup(initial_content);
488 if (buf == NULL) {
489 err = got_error_from_errno("strdup");
490 goto done;
492 s = buf;
493 len = 0;
494 while ((line = strsep(&s, "\n")) != NULL) {
495 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
496 continue; /* remove comments and leading empty lines */
497 len = strlcat(initial_content_stripped, line,
498 initial_content_len + 1);
499 if (len >= initial_content_len + 1) {
500 err = got_error(GOT_ERR_NO_SPACE);
501 goto done;
504 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
505 initial_content_stripped[len - 1] = '\0';
506 len--;
509 logmsg_len = st2.st_size;
510 *logmsg = malloc(logmsg_len + 1);
511 if (*logmsg == NULL)
512 return got_error_from_errno("malloc");
513 (*logmsg)[0] = '\0';
515 fp = fopen(logmsg_path, "re");
516 if (fp == NULL) {
517 err = got_error_from_errno("fopen");
518 goto done;
521 len = 0;
522 while ((linelen = getline(&line, &linesize, fp)) != -1) {
523 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
524 continue; /* remove comments and leading empty lines */
525 len = strlcat(*logmsg, line, logmsg_len + 1);
526 if (len >= logmsg_len + 1) {
527 err = got_error(GOT_ERR_NO_SPACE);
528 goto done;
531 free(line);
532 if (ferror(fp)) {
533 err = got_ferror(fp, GOT_ERR_IO);
534 goto done;
536 while (len > 0 && (*logmsg)[len - 1] == '\n') {
537 (*logmsg)[len - 1] = '\0';
538 len--;
541 if (len == 0) {
542 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
543 "commit message cannot be empty, aborting");
544 goto done;
546 if (require_modification &&
547 strcmp(*logmsg, initial_content_stripped) == 0)
548 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
549 "no changes made to commit message, aborting");
550 done:
551 free(initial_content_stripped);
552 free(buf);
553 if (fp && fclose(fp) == EOF && err == NULL)
554 err = got_error_from_errno("fclose");
555 if (err) {
556 free(*logmsg);
557 *logmsg = NULL;
559 return err;
562 static const struct got_error *
563 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
564 const char *path_dir, const char *branch_name)
566 char *initial_content = NULL;
567 const struct got_error *err = NULL;
568 int initial_content_len;
569 int fd = -1;
571 initial_content_len = asprintf(&initial_content,
572 "\n# %s to be imported to branch %s\n", path_dir,
573 branch_name);
574 if (initial_content_len == -1)
575 return got_error_from_errno("asprintf");
577 err = got_opentemp_named_fd(logmsg_path, &fd,
578 GOT_TMPDIR_STR "/got-importmsg");
579 if (err)
580 goto done;
582 if (write(fd, initial_content, initial_content_len) == -1) {
583 err = got_error_from_errno2("write", *logmsg_path);
584 goto done;
587 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
588 initial_content_len, 1);
589 done:
590 if (fd != -1 && close(fd) == -1 && err == NULL)
591 err = got_error_from_errno2("close", *logmsg_path);
592 free(initial_content);
593 if (err) {
594 free(*logmsg_path);
595 *logmsg_path = NULL;
597 return err;
600 static const struct got_error *
601 import_progress(void *arg, const char *path)
603 printf("A %s\n", path);
604 return NULL;
607 static int
608 valid_author(const char *author)
610 /*
611 * Really dumb email address check; we're only doing this to
612 * avoid git's object parser breaking on commits we create.
613 */
614 while (*author && *author != '<')
615 author++;
616 if (*author != '<')
617 return 0;
618 while (*author && *author != '@')
619 author++;
620 if (*author != '@')
621 return 0;
622 while (*author && *author != '>')
623 author++;
624 return *author == '>';
627 static const struct got_error *
628 get_author(char **author, struct got_repository *repo,
629 struct got_worktree *worktree)
631 const struct got_error *err = NULL;
632 const char *got_author = NULL, *name, *email;
633 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
635 *author = NULL;
637 if (worktree)
638 worktree_conf = got_worktree_get_gotconfig(worktree);
639 repo_conf = got_repo_get_gotconfig(repo);
641 /*
642 * Priority of potential author information sources, from most
643 * significant to least significant:
644 * 1) work tree's .got/got.conf file
645 * 2) repository's got.conf file
646 * 3) repository's git config file
647 * 4) environment variables
648 * 5) global git config files (in user's home directory or /etc)
649 */
651 if (worktree_conf)
652 got_author = got_gotconfig_get_author(worktree_conf);
653 if (got_author == NULL)
654 got_author = got_gotconfig_get_author(repo_conf);
655 if (got_author == NULL) {
656 name = got_repo_get_gitconfig_author_name(repo);
657 email = got_repo_get_gitconfig_author_email(repo);
658 if (name && email) {
659 if (asprintf(author, "%s <%s>", name, email) == -1)
660 return got_error_from_errno("asprintf");
661 return NULL;
664 got_author = getenv("GOT_AUTHOR");
665 if (got_author == NULL) {
666 name = got_repo_get_global_gitconfig_author_name(repo);
667 email = got_repo_get_global_gitconfig_author_email(
668 repo);
669 if (name && email) {
670 if (asprintf(author, "%s <%s>", name, email)
671 == -1)
672 return got_error_from_errno("asprintf");
673 return NULL;
675 /* TODO: Look up user in password database? */
676 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
680 *author = strdup(got_author);
681 if (*author == NULL)
682 return got_error_from_errno("strdup");
684 if (!valid_author(*author)) {
685 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
686 free(*author);
687 *author = NULL;
689 return err;
692 static const struct got_error *
693 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
694 struct got_worktree *worktree)
696 const char *got_allowed_signers = NULL;
697 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
699 *allowed_signers = NULL;
701 if (worktree)
702 worktree_conf = got_worktree_get_gotconfig(worktree);
703 repo_conf = got_repo_get_gotconfig(repo);
705 /*
706 * Priority of potential author information sources, from most
707 * significant to least significant:
708 * 1) work tree's .got/got.conf file
709 * 2) repository's got.conf file
710 */
712 if (worktree_conf)
713 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
714 worktree_conf);
715 if (got_allowed_signers == NULL)
716 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
717 repo_conf);
719 if (got_allowed_signers) {
720 *allowed_signers = strdup(got_allowed_signers);
721 if (*allowed_signers == NULL)
722 return got_error_from_errno("strdup");
724 return NULL;
727 static const struct got_error *
728 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
729 struct got_worktree *worktree)
731 const char *got_revoked_signers = NULL;
732 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
734 *revoked_signers = NULL;
736 if (worktree)
737 worktree_conf = got_worktree_get_gotconfig(worktree);
738 repo_conf = got_repo_get_gotconfig(repo);
740 /*
741 * Priority of potential author information sources, from most
742 * significant to least significant:
743 * 1) work tree's .got/got.conf file
744 * 2) repository's got.conf file
745 */
747 if (worktree_conf)
748 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
749 worktree_conf);
750 if (got_revoked_signers == NULL)
751 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
752 repo_conf);
754 if (got_revoked_signers) {
755 *revoked_signers = strdup(got_revoked_signers);
756 if (*revoked_signers == NULL)
757 return got_error_from_errno("strdup");
759 return NULL;
762 static const struct got_error *
763 get_gitconfig_path(char **gitconfig_path)
765 const char *homedir = getenv("HOME");
767 *gitconfig_path = NULL;
768 if (homedir) {
769 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
770 return got_error_from_errno("asprintf");
773 return NULL;
776 static const struct got_error *
777 cmd_import(int argc, char *argv[])
779 const struct got_error *error = NULL;
780 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
781 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
782 const char *branch_name = "main";
783 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
784 struct got_repository *repo = NULL;
785 struct got_reference *branch_ref = NULL, *head_ref = NULL;
786 struct got_object_id *new_commit_id = NULL;
787 int ch;
788 struct got_pathlist_head ignores;
789 struct got_pathlist_entry *pe;
790 int preserve_logmsg = 0;
791 int *pack_fds = NULL;
793 TAILQ_INIT(&ignores);
795 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
796 switch (ch) {
797 case 'b':
798 branch_name = optarg;
799 break;
800 case 'm':
801 logmsg = strdup(optarg);
802 if (logmsg == NULL) {
803 error = got_error_from_errno("strdup");
804 goto done;
806 break;
807 case 'r':
808 repo_path = realpath(optarg, NULL);
809 if (repo_path == NULL) {
810 error = got_error_from_errno2("realpath",
811 optarg);
812 goto done;
814 break;
815 case 'I':
816 if (optarg[0] == '\0')
817 break;
818 error = got_pathlist_insert(&pe, &ignores, optarg,
819 NULL);
820 if (error)
821 goto done;
822 break;
823 default:
824 usage_import();
825 /* NOTREACHED */
829 argc -= optind;
830 argv += optind;
832 #ifndef PROFILE
833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
834 "unveil",
835 NULL) == -1)
836 err(1, "pledge");
837 #endif
838 if (argc != 1)
839 usage_import();
841 if (repo_path == NULL) {
842 repo_path = getcwd(NULL, 0);
843 if (repo_path == NULL)
844 return got_error_from_errno("getcwd");
846 got_path_strip_trailing_slashes(repo_path);
847 error = get_gitconfig_path(&gitconfig_path);
848 if (error)
849 goto done;
850 error = got_repo_pack_fds_open(&pack_fds);
851 if (error != NULL)
852 goto done;
853 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
854 if (error)
855 goto done;
857 error = get_author(&author, repo, NULL);
858 if (error)
859 return error;
861 /*
862 * Don't let the user create a branch name with a leading '-'.
863 * While technically a valid reference name, this case is usually
864 * an unintended typo.
865 */
866 if (branch_name[0] == '-')
867 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
869 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
870 error = got_error_from_errno("asprintf");
871 goto done;
874 error = got_ref_open(&branch_ref, repo, refname, 0);
875 if (error) {
876 if (error->code != GOT_ERR_NOT_REF)
877 goto done;
878 } else {
879 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
880 "import target branch already exists");
881 goto done;
884 path_dir = realpath(argv[0], NULL);
885 if (path_dir == NULL) {
886 error = got_error_from_errno2("realpath", argv[0]);
887 goto done;
889 got_path_strip_trailing_slashes(path_dir);
891 /*
892 * unveil(2) traverses exec(2); if an editor is used we have
893 * to apply unveil after the log message has been written.
894 */
895 if (logmsg == NULL || strlen(logmsg) == 0) {
896 error = get_editor(&editor);
897 if (error)
898 goto done;
899 free(logmsg);
900 error = collect_import_msg(&logmsg, &logmsg_path, editor,
901 path_dir, refname);
902 if (error) {
903 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
904 logmsg_path != NULL)
905 preserve_logmsg = 1;
906 goto done;
910 if (unveil(path_dir, "r") != 0) {
911 error = got_error_from_errno2("unveil", path_dir);
912 if (logmsg_path)
913 preserve_logmsg = 1;
914 goto done;
917 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
918 if (error) {
919 if (logmsg_path)
920 preserve_logmsg = 1;
921 goto done;
924 error = got_repo_import(&new_commit_id, path_dir, logmsg,
925 author, &ignores, repo, import_progress, NULL);
926 if (error) {
927 if (logmsg_path)
928 preserve_logmsg = 1;
929 goto done;
932 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
933 if (error) {
934 if (logmsg_path)
935 preserve_logmsg = 1;
936 goto done;
939 error = got_ref_write(branch_ref, repo);
940 if (error) {
941 if (logmsg_path)
942 preserve_logmsg = 1;
943 goto done;
946 error = got_object_id_str(&id_str, new_commit_id);
947 if (error) {
948 if (logmsg_path)
949 preserve_logmsg = 1;
950 goto done;
953 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
954 if (error) {
955 if (error->code != GOT_ERR_NOT_REF) {
956 if (logmsg_path)
957 preserve_logmsg = 1;
958 goto done;
961 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
962 branch_ref);
963 if (error) {
964 if (logmsg_path)
965 preserve_logmsg = 1;
966 goto done;
969 error = got_ref_write(head_ref, repo);
970 if (error) {
971 if (logmsg_path)
972 preserve_logmsg = 1;
973 goto done;
977 printf("Created branch %s with commit %s\n",
978 got_ref_get_name(branch_ref), id_str);
979 done:
980 if (pack_fds) {
981 const struct got_error *pack_err =
982 got_repo_pack_fds_close(pack_fds);
983 if (error == NULL)
984 error = pack_err;
986 if (preserve_logmsg) {
987 fprintf(stderr, "%s: log message preserved in %s\n",
988 getprogname(), logmsg_path);
989 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
990 error = got_error_from_errno2("unlink", logmsg_path);
991 free(logmsg);
992 free(logmsg_path);
993 free(repo_path);
994 free(editor);
995 free(refname);
996 free(new_commit_id);
997 free(id_str);
998 free(author);
999 free(gitconfig_path);
1000 if (branch_ref)
1001 got_ref_close(branch_ref);
1002 if (head_ref)
1003 got_ref_close(head_ref);
1004 return error;
1007 __dead static void
1008 usage_clone(void)
1010 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
1011 "[-R reference] repository-url [directory]\n", getprogname());
1012 exit(1);
1015 struct got_fetch_progress_arg {
1016 char last_scaled_size[FMT_SCALED_STRSIZE];
1017 int last_p_indexed;
1018 int last_p_resolved;
1019 int verbosity;
1021 struct got_repository *repo;
1023 int create_configs;
1024 int configs_created;
1025 struct {
1026 struct got_pathlist_head *symrefs;
1027 struct got_pathlist_head *wanted_branches;
1028 struct got_pathlist_head *wanted_refs;
1029 const char *proto;
1030 const char *host;
1031 const char *port;
1032 const char *remote_repo_path;
1033 const char *git_url;
1034 int fetch_all_branches;
1035 int mirror_references;
1036 } config_info;
1039 /* XXX forward declaration */
1040 static const struct got_error *
1041 create_config_files(const char *proto, const char *host, const char *port,
1042 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1043 int mirror_references, struct got_pathlist_head *symrefs,
1044 struct got_pathlist_head *wanted_branches,
1045 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1047 static const struct got_error *
1048 fetch_progress(void *arg, const char *message, off_t packfile_size,
1049 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1051 const struct got_error *err = NULL;
1052 struct got_fetch_progress_arg *a = arg;
1053 char scaled_size[FMT_SCALED_STRSIZE];
1054 int p_indexed, p_resolved;
1055 int print_size = 0, print_indexed = 0, print_resolved = 0;
1058 * In order to allow a failed clone to be resumed with 'got fetch'
1059 * we try to create configuration files as soon as possible.
1060 * Once the server has sent information about its default branch
1061 * we have all required information.
1063 if (a->create_configs && !a->configs_created &&
1064 !TAILQ_EMPTY(a->config_info.symrefs)) {
1065 err = create_config_files(a->config_info.proto,
1066 a->config_info.host, a->config_info.port,
1067 a->config_info.remote_repo_path,
1068 a->config_info.git_url,
1069 a->config_info.fetch_all_branches,
1070 a->config_info.mirror_references,
1071 a->config_info.symrefs,
1072 a->config_info.wanted_branches,
1073 a->config_info.wanted_refs, a->repo);
1074 if (err)
1075 return err;
1076 a->configs_created = 1;
1079 if (a->verbosity < 0)
1080 return NULL;
1082 if (message && message[0] != '\0') {
1083 printf("\rserver: %s", message);
1084 fflush(stdout);
1085 return NULL;
1088 if (packfile_size > 0 || nobj_indexed > 0) {
1089 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1090 (a->last_scaled_size[0] == '\0' ||
1091 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1092 print_size = 1;
1093 if (strlcpy(a->last_scaled_size, scaled_size,
1094 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1095 return got_error(GOT_ERR_NO_SPACE);
1097 if (nobj_indexed > 0) {
1098 p_indexed = (nobj_indexed * 100) / nobj_total;
1099 if (p_indexed != a->last_p_indexed) {
1100 a->last_p_indexed = p_indexed;
1101 print_indexed = 1;
1102 print_size = 1;
1105 if (nobj_resolved > 0) {
1106 p_resolved = (nobj_resolved * 100) /
1107 (nobj_total - nobj_loose);
1108 if (p_resolved != a->last_p_resolved) {
1109 a->last_p_resolved = p_resolved;
1110 print_resolved = 1;
1111 print_indexed = 1;
1112 print_size = 1;
1117 if (print_size || print_indexed || print_resolved)
1118 printf("\r");
1119 if (print_size)
1120 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1121 if (print_indexed)
1122 printf("; indexing %d%%", p_indexed);
1123 if (print_resolved)
1124 printf("; resolving deltas %d%%", p_resolved);
1125 if (print_size || print_indexed || print_resolved)
1126 fflush(stdout);
1128 return NULL;
1131 static const struct got_error *
1132 create_symref(const char *refname, struct got_reference *target_ref,
1133 int verbosity, struct got_repository *repo)
1135 const struct got_error *err;
1136 struct got_reference *head_symref;
1138 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1139 if (err)
1140 return err;
1142 err = got_ref_write(head_symref, repo);
1143 if (err == NULL && verbosity > 0) {
1144 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1145 got_ref_get_name(target_ref));
1147 got_ref_close(head_symref);
1148 return err;
1151 static const struct got_error *
1152 list_remote_refs(struct got_pathlist_head *symrefs,
1153 struct got_pathlist_head *refs)
1155 const struct got_error *err;
1156 struct got_pathlist_entry *pe;
1158 TAILQ_FOREACH(pe, symrefs, entry) {
1159 const char *refname = pe->path;
1160 const char *targetref = pe->data;
1162 printf("%s: %s\n", refname, targetref);
1165 TAILQ_FOREACH(pe, refs, entry) {
1166 const char *refname = pe->path;
1167 struct got_object_id *id = pe->data;
1168 char *id_str;
1170 err = got_object_id_str(&id_str, id);
1171 if (err)
1172 return err;
1173 printf("%s: %s\n", refname, id_str);
1174 free(id_str);
1177 return NULL;
1180 static const struct got_error *
1181 create_ref(const char *refname, struct got_object_id *id,
1182 int verbosity, struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_reference *ref;
1186 char *id_str;
1188 err = got_object_id_str(&id_str, id);
1189 if (err)
1190 return err;
1192 err = got_ref_alloc(&ref, refname, id);
1193 if (err)
1194 goto done;
1196 err = got_ref_write(ref, repo);
1197 got_ref_close(ref);
1199 if (err == NULL && verbosity >= 0)
1200 printf("Created reference %s: %s\n", refname, id_str);
1201 done:
1202 free(id_str);
1203 return err;
1206 static int
1207 match_wanted_ref(const char *refname, const char *wanted_ref)
1209 if (strncmp(refname, "refs/", 5) != 0)
1210 return 0;
1211 refname += 5;
1214 * Prevent fetching of references that won't make any
1215 * sense outside of the remote repository's context.
1217 if (strncmp(refname, "got/", 4) == 0)
1218 return 0;
1219 if (strncmp(refname, "remotes/", 8) == 0)
1220 return 0;
1222 if (strncmp(wanted_ref, "refs/", 5) == 0)
1223 wanted_ref += 5;
1225 /* Allow prefix match. */
1226 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1227 return 1;
1229 /* Allow exact match. */
1230 return (strcmp(refname, wanted_ref) == 0);
1233 static int
1234 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1236 struct got_pathlist_entry *pe;
1238 TAILQ_FOREACH(pe, wanted_refs, entry) {
1239 if (match_wanted_ref(refname, pe->path))
1240 return 1;
1243 return 0;
1246 static const struct got_error *
1247 create_wanted_ref(const char *refname, struct got_object_id *id,
1248 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1250 const struct got_error *err;
1251 char *remote_refname;
1253 if (strncmp("refs/", refname, 5) == 0)
1254 refname += 5;
1256 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1257 remote_repo_name, refname) == -1)
1258 return got_error_from_errno("asprintf");
1260 err = create_ref(remote_refname, id, verbosity, repo);
1261 free(remote_refname);
1262 return err;
1265 static const struct got_error *
1266 create_gotconfig(const char *proto, const char *host, const char *port,
1267 const char *remote_repo_path, const char *default_branch,
1268 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1269 struct got_pathlist_head *wanted_refs, int mirror_references,
1270 struct got_repository *repo)
1272 const struct got_error *err = NULL;
1273 char *gotconfig_path = NULL;
1274 char *gotconfig = NULL;
1275 FILE *gotconfig_file = NULL;
1276 const char *branchname = NULL;
1277 char *branches = NULL, *refs = NULL;
1278 ssize_t n;
1280 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1281 struct got_pathlist_entry *pe;
1282 TAILQ_FOREACH(pe, wanted_branches, entry) {
1283 char *s;
1284 branchname = pe->path;
1285 if (strncmp(branchname, "refs/heads/", 11) == 0)
1286 branchname += 11;
1287 if (asprintf(&s, "%s\"%s\" ",
1288 branches ? branches : "", branchname) == -1) {
1289 err = got_error_from_errno("asprintf");
1290 goto done;
1292 free(branches);
1293 branches = s;
1295 } else if (!fetch_all_branches && default_branch) {
1296 branchname = default_branch;
1297 if (strncmp(branchname, "refs/heads/", 11) == 0)
1298 branchname += 11;
1299 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 goto done;
1304 if (!TAILQ_EMPTY(wanted_refs)) {
1305 struct got_pathlist_entry *pe;
1306 TAILQ_FOREACH(pe, wanted_refs, entry) {
1307 char *s;
1308 const char *refname = pe->path;
1309 if (strncmp(refname, "refs/", 5) == 0)
1310 branchname += 5;
1311 if (asprintf(&s, "%s\"%s\" ",
1312 refs ? refs : "", refname) == -1) {
1313 err = got_error_from_errno("asprintf");
1314 goto done;
1316 free(refs);
1317 refs = s;
1321 /* Create got.conf(5). */
1322 gotconfig_path = got_repo_get_path_gotconfig(repo);
1323 if (gotconfig_path == NULL) {
1324 err = got_error_from_errno("got_repo_get_path_gotconfig");
1325 goto done;
1327 gotconfig_file = fopen(gotconfig_path, "ae");
1328 if (gotconfig_file == NULL) {
1329 err = got_error_from_errno2("fopen", gotconfig_path);
1330 goto done;
1332 if (asprintf(&gotconfig,
1333 "remote \"%s\" {\n"
1334 "\tserver %s\n"
1335 "\tprotocol %s\n"
1336 "%s%s%s"
1337 "\trepository \"%s\"\n"
1338 "%s%s%s"
1339 "%s%s%s"
1340 "%s"
1341 "%s"
1342 "}\n",
1343 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1344 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1345 remote_repo_path, branches ? "\tbranch { " : "",
1346 branches ? branches : "", branches ? "}\n" : "",
1347 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1348 mirror_references ? "\tmirror_references yes\n" : "",
1349 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1350 err = got_error_from_errno("asprintf");
1351 goto done;
1353 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1354 if (n != strlen(gotconfig)) {
1355 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1356 goto done;
1359 done:
1360 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1361 err = got_error_from_errno2("fclose", gotconfig_path);
1362 free(gotconfig_path);
1363 free(branches);
1364 return err;
1367 static const struct got_error *
1368 create_gitconfig(const char *git_url, const char *default_branch,
1369 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1370 struct got_pathlist_head *wanted_refs, int mirror_references,
1371 struct got_repository *repo)
1373 const struct got_error *err = NULL;
1374 char *gitconfig_path = NULL;
1375 char *gitconfig = NULL;
1376 FILE *gitconfig_file = NULL;
1377 char *branches = NULL, *refs = NULL;
1378 const char *branchname;
1379 ssize_t n;
1381 /* Create a config file Git can understand. */
1382 gitconfig_path = got_repo_get_path_gitconfig(repo);
1383 if (gitconfig_path == NULL) {
1384 err = got_error_from_errno("got_repo_get_path_gitconfig");
1385 goto done;
1387 gitconfig_file = fopen(gitconfig_path, "ae");
1388 if (gitconfig_file == NULL) {
1389 err = got_error_from_errno2("fopen", gitconfig_path);
1390 goto done;
1392 if (fetch_all_branches) {
1393 if (mirror_references) {
1394 if (asprintf(&branches,
1395 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1396 err = got_error_from_errno("asprintf");
1397 goto done;
1399 } else if (asprintf(&branches,
1400 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1401 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1402 err = got_error_from_errno("asprintf");
1403 goto done;
1405 } else if (!TAILQ_EMPTY(wanted_branches)) {
1406 struct got_pathlist_entry *pe;
1407 TAILQ_FOREACH(pe, wanted_branches, entry) {
1408 char *s;
1409 branchname = pe->path;
1410 if (strncmp(branchname, "refs/heads/", 11) == 0)
1411 branchname += 11;
1412 if (mirror_references) {
1413 if (asprintf(&s,
1414 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1415 branches ? branches : "",
1416 branchname, branchname) == -1) {
1417 err = got_error_from_errno("asprintf");
1418 goto done;
1420 } else if (asprintf(&s,
1421 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1422 branches ? branches : "",
1423 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1424 branchname) == -1) {
1425 err = got_error_from_errno("asprintf");
1426 goto done;
1428 free(branches);
1429 branches = s;
1431 } else {
1433 * If the server specified a default branch, use just that one.
1434 * Otherwise fall back to fetching all branches on next fetch.
1436 if (default_branch) {
1437 branchname = default_branch;
1438 if (strncmp(branchname, "refs/heads/", 11) == 0)
1439 branchname += 11;
1440 } else
1441 branchname = "*"; /* fall back to all branches */
1442 if (mirror_references) {
1443 if (asprintf(&branches,
1444 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1445 branchname, branchname) == -1) {
1446 err = got_error_from_errno("asprintf");
1447 goto done;
1449 } else if (asprintf(&branches,
1450 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1451 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1452 branchname) == -1) {
1453 err = got_error_from_errno("asprintf");
1454 goto done;
1457 if (!TAILQ_EMPTY(wanted_refs)) {
1458 struct got_pathlist_entry *pe;
1459 TAILQ_FOREACH(pe, wanted_refs, entry) {
1460 char *s;
1461 const char *refname = pe->path;
1462 if (strncmp(refname, "refs/", 5) == 0)
1463 refname += 5;
1464 if (mirror_references) {
1465 if (asprintf(&s,
1466 "%s\tfetch = refs/%s:refs/%s\n",
1467 refs ? refs : "", refname, refname) == -1) {
1468 err = got_error_from_errno("asprintf");
1469 goto done;
1471 } else if (asprintf(&s,
1472 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1473 refs ? refs : "",
1474 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1475 refname) == -1) {
1476 err = got_error_from_errno("asprintf");
1477 goto done;
1479 free(refs);
1480 refs = s;
1484 if (asprintf(&gitconfig,
1485 "[remote \"%s\"]\n"
1486 "\turl = %s\n"
1487 "%s"
1488 "%s"
1489 "\tfetch = refs/tags/*:refs/tags/*\n",
1490 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1491 refs ? refs : "") == -1) {
1492 err = got_error_from_errno("asprintf");
1493 goto done;
1495 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1496 if (n != strlen(gitconfig)) {
1497 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1498 goto done;
1500 done:
1501 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1502 err = got_error_from_errno2("fclose", gitconfig_path);
1503 free(gitconfig_path);
1504 free(branches);
1505 return err;
1508 static const struct got_error *
1509 create_config_files(const char *proto, const char *host, const char *port,
1510 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1511 int mirror_references, struct got_pathlist_head *symrefs,
1512 struct got_pathlist_head *wanted_branches,
1513 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1515 const struct got_error *err = NULL;
1516 const char *default_branch = NULL;
1517 struct got_pathlist_entry *pe;
1520 * If we asked for a set of wanted branches then use the first
1521 * one of those.
1523 if (!TAILQ_EMPTY(wanted_branches)) {
1524 pe = TAILQ_FIRST(wanted_branches);
1525 default_branch = pe->path;
1526 } else {
1527 /* First HEAD ref listed by server is the default branch. */
1528 TAILQ_FOREACH(pe, symrefs, entry) {
1529 const char *refname = pe->path;
1530 const char *target = pe->data;
1532 if (strcmp(refname, GOT_REF_HEAD) != 0)
1533 continue;
1535 default_branch = target;
1536 break;
1540 /* Create got.conf(5). */
1541 err = create_gotconfig(proto, host, port, remote_repo_path,
1542 default_branch, fetch_all_branches, wanted_branches,
1543 wanted_refs, mirror_references, repo);
1544 if (err)
1545 return err;
1547 /* Create a config file Git can understand. */
1548 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1549 wanted_branches, wanted_refs, mirror_references, repo);
1552 static const struct got_error *
1553 cmd_clone(int argc, char *argv[])
1555 const struct got_error *error = NULL;
1556 const char *uri, *dirname;
1557 char *proto, *host, *port, *repo_name, *server_path;
1558 char *default_destdir = NULL, *id_str = NULL;
1559 const char *repo_path;
1560 struct got_repository *repo = NULL;
1561 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1562 struct got_pathlist_entry *pe;
1563 struct got_object_id *pack_hash = NULL;
1564 int ch, fetchfd = -1, fetchstatus;
1565 pid_t fetchpid = -1;
1566 struct got_fetch_progress_arg fpa;
1567 char *git_url = NULL;
1568 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1569 int list_refs_only = 0;
1570 int *pack_fds = NULL;
1572 TAILQ_INIT(&refs);
1573 TAILQ_INIT(&symrefs);
1574 TAILQ_INIT(&wanted_branches);
1575 TAILQ_INIT(&wanted_refs);
1577 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1578 switch (ch) {
1579 case 'a':
1580 fetch_all_branches = 1;
1581 break;
1582 case 'b':
1583 error = got_pathlist_append(&wanted_branches,
1584 optarg, NULL);
1585 if (error)
1586 return error;
1587 break;
1588 case 'l':
1589 list_refs_only = 1;
1590 break;
1591 case 'm':
1592 mirror_references = 1;
1593 break;
1594 case 'v':
1595 if (verbosity < 0)
1596 verbosity = 0;
1597 else if (verbosity < 3)
1598 verbosity++;
1599 break;
1600 case 'q':
1601 verbosity = -1;
1602 break;
1603 case 'R':
1604 error = got_pathlist_append(&wanted_refs,
1605 optarg, NULL);
1606 if (error)
1607 return error;
1608 break;
1609 default:
1610 usage_clone();
1611 break;
1614 argc -= optind;
1615 argv += optind;
1617 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1618 option_conflict('a', 'b');
1619 if (list_refs_only) {
1620 if (!TAILQ_EMPTY(&wanted_branches))
1621 option_conflict('l', 'b');
1622 if (fetch_all_branches)
1623 option_conflict('l', 'a');
1624 if (mirror_references)
1625 option_conflict('l', 'm');
1626 if (!TAILQ_EMPTY(&wanted_refs))
1627 option_conflict('l', 'R');
1630 uri = argv[0];
1632 if (argc == 1)
1633 dirname = NULL;
1634 else if (argc == 2)
1635 dirname = argv[1];
1636 else
1637 usage_clone();
1639 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1640 &repo_name, uri);
1641 if (error)
1642 goto done;
1644 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1645 host, port ? ":" : "", port ? port : "",
1646 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1647 error = got_error_from_errno("asprintf");
1648 goto done;
1651 if (strcmp(proto, "git") == 0) {
1652 #ifndef PROFILE
1653 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1654 "sendfd dns inet unveil", NULL) == -1)
1655 err(1, "pledge");
1656 #endif
1657 } else if (strcmp(proto, "git+ssh") == 0 ||
1658 strcmp(proto, "ssh") == 0) {
1659 #ifndef PROFILE
1660 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1661 "sendfd unveil", NULL) == -1)
1662 err(1, "pledge");
1663 #endif
1664 } else if (strcmp(proto, "http") == 0 ||
1665 strcmp(proto, "git+http") == 0) {
1666 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1667 goto done;
1668 } else {
1669 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1670 goto done;
1672 if (dirname == NULL) {
1673 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1674 error = got_error_from_errno("asprintf");
1675 goto done;
1677 repo_path = default_destdir;
1678 } else
1679 repo_path = dirname;
1681 if (!list_refs_only) {
1682 error = got_path_mkdir(repo_path);
1683 if (error &&
1684 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1685 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1686 goto done;
1687 if (!got_path_dir_is_empty(repo_path)) {
1688 error = got_error_path(repo_path,
1689 GOT_ERR_DIR_NOT_EMPTY);
1690 goto done;
1694 error = got_dial_apply_unveil(proto);
1695 if (error)
1696 goto done;
1698 error = apply_unveil(repo_path, 0, NULL);
1699 if (error)
1700 goto done;
1702 if (verbosity >= 0)
1703 printf("Connecting to %s%s%s\n", host,
1704 port ? ":" : "", port ? port : "");
1706 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1707 server_path, verbosity);
1708 if (error)
1709 goto done;
1711 if (!list_refs_only) {
1712 error = got_repo_init(repo_path);
1713 if (error)
1714 goto done;
1715 error = got_repo_pack_fds_open(&pack_fds);
1716 if (error != NULL)
1717 goto done;
1718 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1719 if (error)
1720 goto done;
1723 fpa.last_scaled_size[0] = '\0';
1724 fpa.last_p_indexed = -1;
1725 fpa.last_p_resolved = -1;
1726 fpa.verbosity = verbosity;
1727 fpa.create_configs = 1;
1728 fpa.configs_created = 0;
1729 fpa.repo = repo;
1730 fpa.config_info.symrefs = &symrefs;
1731 fpa.config_info.wanted_branches = &wanted_branches;
1732 fpa.config_info.wanted_refs = &wanted_refs;
1733 fpa.config_info.proto = proto;
1734 fpa.config_info.host = host;
1735 fpa.config_info.port = port;
1736 fpa.config_info.remote_repo_path = server_path;
1737 fpa.config_info.git_url = git_url;
1738 fpa.config_info.fetch_all_branches = fetch_all_branches;
1739 fpa.config_info.mirror_references = mirror_references;
1740 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1741 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1742 fetch_all_branches, &wanted_branches, &wanted_refs,
1743 list_refs_only, verbosity, fetchfd, repo,
1744 fetch_progress, &fpa);
1745 if (error)
1746 goto done;
1748 if (list_refs_only) {
1749 error = list_remote_refs(&symrefs, &refs);
1750 goto done;
1753 if (pack_hash == NULL) {
1754 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1755 "server sent an empty pack file");
1756 goto done;
1758 error = got_object_id_str(&id_str, pack_hash);
1759 if (error)
1760 goto done;
1761 if (verbosity >= 0)
1762 printf("\nFetched %s.pack\n", id_str);
1763 free(id_str);
1765 /* Set up references provided with the pack file. */
1766 TAILQ_FOREACH(pe, &refs, entry) {
1767 const char *refname = pe->path;
1768 struct got_object_id *id = pe->data;
1769 char *remote_refname;
1771 if (is_wanted_ref(&wanted_refs, refname) &&
1772 !mirror_references) {
1773 error = create_wanted_ref(refname, id,
1774 GOT_FETCH_DEFAULT_REMOTE_NAME,
1775 verbosity - 1, repo);
1776 if (error)
1777 goto done;
1778 continue;
1781 error = create_ref(refname, id, verbosity - 1, repo);
1782 if (error)
1783 goto done;
1785 if (mirror_references)
1786 continue;
1788 if (strncmp("refs/heads/", refname, 11) != 0)
1789 continue;
1791 if (asprintf(&remote_refname,
1792 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1793 refname + 11) == -1) {
1794 error = got_error_from_errno("asprintf");
1795 goto done;
1797 error = create_ref(remote_refname, id, verbosity - 1, repo);
1798 free(remote_refname);
1799 if (error)
1800 goto done;
1803 /* Set the HEAD reference if the server provided one. */
1804 TAILQ_FOREACH(pe, &symrefs, entry) {
1805 struct got_reference *target_ref;
1806 const char *refname = pe->path;
1807 const char *target = pe->data;
1808 char *remote_refname = NULL, *remote_target = NULL;
1810 if (strcmp(refname, GOT_REF_HEAD) != 0)
1811 continue;
1813 error = got_ref_open(&target_ref, repo, target, 0);
1814 if (error) {
1815 if (error->code == GOT_ERR_NOT_REF) {
1816 error = NULL;
1817 continue;
1819 goto done;
1822 error = create_symref(refname, target_ref, verbosity, repo);
1823 got_ref_close(target_ref);
1824 if (error)
1825 goto done;
1827 if (mirror_references)
1828 continue;
1830 if (strncmp("refs/heads/", target, 11) != 0)
1831 continue;
1833 if (asprintf(&remote_refname,
1834 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1835 refname) == -1) {
1836 error = got_error_from_errno("asprintf");
1837 goto done;
1839 if (asprintf(&remote_target,
1840 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1841 target + 11) == -1) {
1842 error = got_error_from_errno("asprintf");
1843 free(remote_refname);
1844 goto done;
1846 error = got_ref_open(&target_ref, repo, remote_target, 0);
1847 if (error) {
1848 free(remote_refname);
1849 free(remote_target);
1850 if (error->code == GOT_ERR_NOT_REF) {
1851 error = NULL;
1852 continue;
1854 goto done;
1856 error = create_symref(remote_refname, target_ref,
1857 verbosity - 1, repo);
1858 free(remote_refname);
1859 free(remote_target);
1860 got_ref_close(target_ref);
1861 if (error)
1862 goto done;
1864 if (pe == NULL) {
1866 * We failed to set the HEAD reference. If we asked for
1867 * a set of wanted branches use the first of one of those
1868 * which could be fetched instead.
1870 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1871 const char *target = pe->path;
1872 struct got_reference *target_ref;
1874 error = got_ref_open(&target_ref, repo, target, 0);
1875 if (error) {
1876 if (error->code == GOT_ERR_NOT_REF) {
1877 error = NULL;
1878 continue;
1880 goto done;
1883 error = create_symref(GOT_REF_HEAD, target_ref,
1884 verbosity, repo);
1885 got_ref_close(target_ref);
1886 if (error)
1887 goto done;
1888 break;
1892 if (verbosity >= 0)
1893 printf("Created %s repository '%s'\n",
1894 mirror_references ? "mirrored" : "cloned", repo_path);
1895 done:
1896 if (pack_fds) {
1897 const struct got_error *pack_err =
1898 got_repo_pack_fds_close(pack_fds);
1899 if (error == NULL)
1900 error = pack_err;
1902 if (fetchpid > 0) {
1903 if (kill(fetchpid, SIGTERM) == -1)
1904 error = got_error_from_errno("kill");
1905 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1906 error = got_error_from_errno("waitpid");
1908 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1909 error = got_error_from_errno("close");
1910 if (repo) {
1911 const struct got_error *close_err = got_repo_close(repo);
1912 if (error == NULL)
1913 error = close_err;
1915 TAILQ_FOREACH(pe, &refs, entry) {
1916 free((void *)pe->path);
1917 free(pe->data);
1919 got_pathlist_free(&refs);
1920 TAILQ_FOREACH(pe, &symrefs, entry) {
1921 free((void *)pe->path);
1922 free(pe->data);
1924 got_pathlist_free(&symrefs);
1925 got_pathlist_free(&wanted_branches);
1926 got_pathlist_free(&wanted_refs);
1927 free(pack_hash);
1928 free(proto);
1929 free(host);
1930 free(port);
1931 free(server_path);
1932 free(repo_name);
1933 free(default_destdir);
1934 free(git_url);
1935 return error;
1938 static const struct got_error *
1939 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1940 int replace_tags, int verbosity, struct got_repository *repo)
1942 const struct got_error *err = NULL;
1943 char *new_id_str = NULL;
1944 struct got_object_id *old_id = NULL;
1946 err = got_object_id_str(&new_id_str, new_id);
1947 if (err)
1948 goto done;
1950 if (!replace_tags &&
1951 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1952 err = got_ref_resolve(&old_id, repo, ref);
1953 if (err)
1954 goto done;
1955 if (got_object_id_cmp(old_id, new_id) == 0)
1956 goto done;
1957 if (verbosity >= 0) {
1958 printf("Rejecting update of existing tag %s: %s\n",
1959 got_ref_get_name(ref), new_id_str);
1961 goto done;
1964 if (got_ref_is_symbolic(ref)) {
1965 if (verbosity >= 0) {
1966 printf("Replacing reference %s: %s\n",
1967 got_ref_get_name(ref),
1968 got_ref_get_symref_target(ref));
1970 err = got_ref_change_symref_to_ref(ref, new_id);
1971 if (err)
1972 goto done;
1973 err = got_ref_write(ref, repo);
1974 if (err)
1975 goto done;
1976 } else {
1977 err = got_ref_resolve(&old_id, repo, ref);
1978 if (err)
1979 goto done;
1980 if (got_object_id_cmp(old_id, new_id) == 0)
1981 goto done;
1983 err = got_ref_change_ref(ref, new_id);
1984 if (err)
1985 goto done;
1986 err = got_ref_write(ref, repo);
1987 if (err)
1988 goto done;
1991 if (verbosity >= 0)
1992 printf("Updated %s: %s\n", got_ref_get_name(ref),
1993 new_id_str);
1994 done:
1995 free(old_id);
1996 free(new_id_str);
1997 return err;
2000 static const struct got_error *
2001 update_symref(const char *refname, struct got_reference *target_ref,
2002 int verbosity, struct got_repository *repo)
2004 const struct got_error *err = NULL, *unlock_err;
2005 struct got_reference *symref;
2006 int symref_is_locked = 0;
2008 err = got_ref_open(&symref, repo, refname, 1);
2009 if (err) {
2010 if (err->code != GOT_ERR_NOT_REF)
2011 return err;
2012 err = got_ref_alloc_symref(&symref, refname, target_ref);
2013 if (err)
2014 goto done;
2016 err = got_ref_write(symref, repo);
2017 if (err)
2018 goto done;
2020 if (verbosity >= 0)
2021 printf("Created reference %s: %s\n",
2022 got_ref_get_name(symref),
2023 got_ref_get_symref_target(symref));
2024 } else {
2025 symref_is_locked = 1;
2027 if (strcmp(got_ref_get_symref_target(symref),
2028 got_ref_get_name(target_ref)) == 0)
2029 goto done;
2031 err = got_ref_change_symref(symref,
2032 got_ref_get_name(target_ref));
2033 if (err)
2034 goto done;
2036 err = got_ref_write(symref, repo);
2037 if (err)
2038 goto done;
2040 if (verbosity >= 0)
2041 printf("Updated %s: %s\n", got_ref_get_name(symref),
2042 got_ref_get_symref_target(symref));
2045 done:
2046 if (symref_is_locked) {
2047 unlock_err = got_ref_unlock(symref);
2048 if (unlock_err && err == NULL)
2049 err = unlock_err;
2051 got_ref_close(symref);
2052 return err;
2055 __dead static void
2056 usage_fetch(void)
2058 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2059 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2060 "[remote-repository-name]\n",
2061 getprogname());
2062 exit(1);
2065 static const struct got_error *
2066 delete_missing_ref(struct got_reference *ref,
2067 int verbosity, struct got_repository *repo)
2069 const struct got_error *err = NULL;
2070 struct got_object_id *id = NULL;
2071 char *id_str = NULL;
2073 if (got_ref_is_symbolic(ref)) {
2074 err = got_ref_delete(ref, repo);
2075 if (err)
2076 return err;
2077 if (verbosity >= 0) {
2078 printf("Deleted %s: %s\n",
2079 got_ref_get_name(ref),
2080 got_ref_get_symref_target(ref));
2082 } else {
2083 err = got_ref_resolve(&id, repo, ref);
2084 if (err)
2085 return err;
2086 err = got_object_id_str(&id_str, id);
2087 if (err)
2088 goto done;
2090 err = got_ref_delete(ref, repo);
2091 if (err)
2092 goto done;
2093 if (verbosity >= 0) {
2094 printf("Deleted %s: %s\n",
2095 got_ref_get_name(ref), id_str);
2098 done:
2099 free(id);
2100 free(id_str);
2101 return NULL;
2104 static const struct got_error *
2105 delete_missing_refs(struct got_pathlist_head *their_refs,
2106 struct got_pathlist_head *their_symrefs,
2107 const struct got_remote_repo *remote,
2108 int verbosity, struct got_repository *repo)
2110 const struct got_error *err = NULL, *unlock_err;
2111 struct got_reflist_head my_refs;
2112 struct got_reflist_entry *re;
2113 struct got_pathlist_entry *pe;
2114 char *remote_namespace = NULL;
2115 char *local_refname = NULL;
2117 TAILQ_INIT(&my_refs);
2119 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2120 == -1)
2121 return got_error_from_errno("asprintf");
2123 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2124 if (err)
2125 goto done;
2127 TAILQ_FOREACH(re, &my_refs, entry) {
2128 const char *refname = got_ref_get_name(re->ref);
2129 const char *their_refname;
2131 if (remote->mirror_references) {
2132 their_refname = refname;
2133 } else {
2134 if (strncmp(refname, remote_namespace,
2135 strlen(remote_namespace)) == 0) {
2136 if (strcmp(refname + strlen(remote_namespace),
2137 GOT_REF_HEAD) == 0)
2138 continue;
2139 if (asprintf(&local_refname, "refs/heads/%s",
2140 refname + strlen(remote_namespace)) == -1) {
2141 err = got_error_from_errno("asprintf");
2142 goto done;
2144 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2145 continue;
2147 their_refname = local_refname;
2150 TAILQ_FOREACH(pe, their_refs, entry) {
2151 if (strcmp(their_refname, pe->path) == 0)
2152 break;
2154 if (pe != NULL)
2155 continue;
2157 TAILQ_FOREACH(pe, their_symrefs, entry) {
2158 if (strcmp(their_refname, pe->path) == 0)
2159 break;
2161 if (pe != NULL)
2162 continue;
2164 err = delete_missing_ref(re->ref, verbosity, repo);
2165 if (err)
2166 break;
2168 if (local_refname) {
2169 struct got_reference *ref;
2170 err = got_ref_open(&ref, repo, local_refname, 1);
2171 if (err) {
2172 if (err->code != GOT_ERR_NOT_REF)
2173 break;
2174 free(local_refname);
2175 local_refname = NULL;
2176 continue;
2178 err = delete_missing_ref(ref, verbosity, repo);
2179 if (err)
2180 break;
2181 unlock_err = got_ref_unlock(ref);
2182 got_ref_close(ref);
2183 if (unlock_err && err == NULL) {
2184 err = unlock_err;
2185 break;
2188 free(local_refname);
2189 local_refname = NULL;
2192 done:
2193 free(remote_namespace);
2194 free(local_refname);
2195 return err;
2198 static const struct got_error *
2199 update_wanted_ref(const char *refname, struct got_object_id *id,
2200 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2202 const struct got_error *err, *unlock_err;
2203 char *remote_refname;
2204 struct got_reference *ref;
2206 if (strncmp("refs/", refname, 5) == 0)
2207 refname += 5;
2209 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2210 remote_repo_name, refname) == -1)
2211 return got_error_from_errno("asprintf");
2213 err = got_ref_open(&ref, repo, remote_refname, 1);
2214 if (err) {
2215 if (err->code != GOT_ERR_NOT_REF)
2216 goto done;
2217 err = create_ref(remote_refname, id, verbosity, repo);
2218 } else {
2219 err = update_ref(ref, id, 0, verbosity, repo);
2220 unlock_err = got_ref_unlock(ref);
2221 if (unlock_err && err == NULL)
2222 err = unlock_err;
2223 got_ref_close(ref);
2225 done:
2226 free(remote_refname);
2227 return err;
2230 static const struct got_error *
2231 delete_ref(struct got_repository *repo, struct got_reference *ref)
2233 const struct got_error *err = NULL;
2234 struct got_object_id *id = NULL;
2235 char *id_str = NULL;
2236 const char *target;
2238 if (got_ref_is_symbolic(ref)) {
2239 target = got_ref_get_symref_target(ref);
2240 } else {
2241 err = got_ref_resolve(&id, repo, ref);
2242 if (err)
2243 goto done;
2244 err = got_object_id_str(&id_str, id);
2245 if (err)
2246 goto done;
2247 target = id_str;
2250 err = got_ref_delete(ref, repo);
2251 if (err)
2252 goto done;
2254 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2255 done:
2256 free(id);
2257 free(id_str);
2258 return err;
2261 static const struct got_error *
2262 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2264 const struct got_error *err = NULL;
2265 struct got_reflist_head refs;
2266 struct got_reflist_entry *re;
2267 char *prefix;
2269 TAILQ_INIT(&refs);
2271 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2272 err = got_error_from_errno("asprintf");
2273 goto done;
2275 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2276 if (err)
2277 goto done;
2279 TAILQ_FOREACH(re, &refs, entry)
2280 delete_ref(repo, re->ref);
2281 done:
2282 got_ref_list_free(&refs);
2283 return err;
2286 static const struct got_error *
2287 cmd_fetch(int argc, char *argv[])
2289 const struct got_error *error = NULL, *unlock_err;
2290 char *cwd = NULL, *repo_path = NULL;
2291 const char *remote_name;
2292 char *proto = NULL, *host = NULL, *port = NULL;
2293 char *repo_name = NULL, *server_path = NULL;
2294 const struct got_remote_repo *remotes, *remote = NULL;
2295 int nremotes;
2296 char *id_str = NULL;
2297 struct got_repository *repo = NULL;
2298 struct got_worktree *worktree = NULL;
2299 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2300 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2301 struct got_pathlist_entry *pe;
2302 struct got_object_id *pack_hash = NULL;
2303 int i, ch, fetchfd = -1, fetchstatus;
2304 pid_t fetchpid = -1;
2305 struct got_fetch_progress_arg fpa;
2306 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2307 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2308 int *pack_fds = NULL;
2310 TAILQ_INIT(&refs);
2311 TAILQ_INIT(&symrefs);
2312 TAILQ_INIT(&wanted_branches);
2313 TAILQ_INIT(&wanted_refs);
2315 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2316 switch (ch) {
2317 case 'a':
2318 fetch_all_branches = 1;
2319 break;
2320 case 'b':
2321 error = got_pathlist_append(&wanted_branches,
2322 optarg, NULL);
2323 if (error)
2324 return error;
2325 break;
2326 case 'd':
2327 delete_refs = 1;
2328 break;
2329 case 'l':
2330 list_refs_only = 1;
2331 break;
2332 case 'r':
2333 repo_path = realpath(optarg, NULL);
2334 if (repo_path == NULL)
2335 return got_error_from_errno2("realpath",
2336 optarg);
2337 got_path_strip_trailing_slashes(repo_path);
2338 break;
2339 case 't':
2340 replace_tags = 1;
2341 break;
2342 case 'v':
2343 if (verbosity < 0)
2344 verbosity = 0;
2345 else if (verbosity < 3)
2346 verbosity++;
2347 break;
2348 case 'q':
2349 verbosity = -1;
2350 break;
2351 case 'R':
2352 error = got_pathlist_append(&wanted_refs,
2353 optarg, NULL);
2354 if (error)
2355 return error;
2356 break;
2357 case 'X':
2358 delete_remote = 1;
2359 break;
2360 default:
2361 usage_fetch();
2362 break;
2365 argc -= optind;
2366 argv += optind;
2368 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2369 option_conflict('a', 'b');
2370 if (list_refs_only) {
2371 if (!TAILQ_EMPTY(&wanted_branches))
2372 option_conflict('l', 'b');
2373 if (fetch_all_branches)
2374 option_conflict('l', 'a');
2375 if (delete_refs)
2376 option_conflict('l', 'd');
2377 if (delete_remote)
2378 option_conflict('l', 'X');
2380 if (delete_remote) {
2381 if (fetch_all_branches)
2382 option_conflict('X', 'a');
2383 if (!TAILQ_EMPTY(&wanted_branches))
2384 option_conflict('X', 'b');
2385 if (delete_refs)
2386 option_conflict('X', 'd');
2387 if (replace_tags)
2388 option_conflict('X', 't');
2389 if (!TAILQ_EMPTY(&wanted_refs))
2390 option_conflict('X', 'R');
2393 if (argc == 0) {
2394 if (delete_remote)
2395 errx(1, "-X option requires a remote name");
2396 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2397 } else if (argc == 1)
2398 remote_name = argv[0];
2399 else
2400 usage_fetch();
2402 cwd = getcwd(NULL, 0);
2403 if (cwd == NULL) {
2404 error = got_error_from_errno("getcwd");
2405 goto done;
2408 error = got_repo_pack_fds_open(&pack_fds);
2409 if (error != NULL)
2410 goto done;
2412 if (repo_path == NULL) {
2413 error = got_worktree_open(&worktree, cwd);
2414 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2415 goto done;
2416 else
2417 error = NULL;
2418 if (worktree) {
2419 repo_path =
2420 strdup(got_worktree_get_repo_path(worktree));
2421 if (repo_path == NULL)
2422 error = got_error_from_errno("strdup");
2423 if (error)
2424 goto done;
2425 } else {
2426 repo_path = strdup(cwd);
2427 if (repo_path == NULL) {
2428 error = got_error_from_errno("strdup");
2429 goto done;
2434 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2435 if (error)
2436 goto done;
2438 if (delete_remote) {
2439 error = delete_refs_for_remote(repo, remote_name);
2440 goto done; /* nothing else to do */
2443 if (worktree) {
2444 worktree_conf = got_worktree_get_gotconfig(worktree);
2445 if (worktree_conf) {
2446 got_gotconfig_get_remotes(&nremotes, &remotes,
2447 worktree_conf);
2448 for (i = 0; i < nremotes; i++) {
2449 if (strcmp(remotes[i].name, remote_name) == 0) {
2450 remote = &remotes[i];
2451 break;
2456 if (remote == NULL) {
2457 repo_conf = got_repo_get_gotconfig(repo);
2458 if (repo_conf) {
2459 got_gotconfig_get_remotes(&nremotes, &remotes,
2460 repo_conf);
2461 for (i = 0; i < nremotes; i++) {
2462 if (strcmp(remotes[i].name, remote_name) == 0) {
2463 remote = &remotes[i];
2464 break;
2469 if (remote == NULL) {
2470 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2471 for (i = 0; i < nremotes; i++) {
2472 if (strcmp(remotes[i].name, remote_name) == 0) {
2473 remote = &remotes[i];
2474 break;
2478 if (remote == NULL) {
2479 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2480 goto done;
2483 if (TAILQ_EMPTY(&wanted_branches)) {
2484 if (!fetch_all_branches)
2485 fetch_all_branches = remote->fetch_all_branches;
2486 for (i = 0; i < remote->nfetch_branches; i++) {
2487 got_pathlist_append(&wanted_branches,
2488 remote->fetch_branches[i], NULL);
2491 if (TAILQ_EMPTY(&wanted_refs)) {
2492 for (i = 0; i < remote->nfetch_refs; i++) {
2493 got_pathlist_append(&wanted_refs,
2494 remote->fetch_refs[i], NULL);
2498 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2499 &repo_name, remote->fetch_url);
2500 if (error)
2501 goto done;
2503 if (strcmp(proto, "git") == 0) {
2504 #ifndef PROFILE
2505 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2506 "sendfd dns inet unveil", NULL) == -1)
2507 err(1, "pledge");
2508 #endif
2509 } else if (strcmp(proto, "git+ssh") == 0 ||
2510 strcmp(proto, "ssh") == 0) {
2511 #ifndef PROFILE
2512 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2513 "sendfd unveil", NULL) == -1)
2514 err(1, "pledge");
2515 #endif
2516 } else if (strcmp(proto, "http") == 0 ||
2517 strcmp(proto, "git+http") == 0) {
2518 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2519 goto done;
2520 } else {
2521 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2522 goto done;
2525 error = got_dial_apply_unveil(proto);
2526 if (error)
2527 goto done;
2529 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2530 if (error)
2531 goto done;
2533 if (verbosity >= 0)
2534 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2535 port ? ":" : "", port ? port : "");
2537 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2538 server_path, verbosity);
2539 if (error)
2540 goto done;
2542 fpa.last_scaled_size[0] = '\0';
2543 fpa.last_p_indexed = -1;
2544 fpa.last_p_resolved = -1;
2545 fpa.verbosity = verbosity;
2546 fpa.repo = repo;
2547 fpa.create_configs = 0;
2548 fpa.configs_created = 0;
2549 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2550 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2551 remote->mirror_references, fetch_all_branches, &wanted_branches,
2552 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2553 fetch_progress, &fpa);
2554 if (error)
2555 goto done;
2557 if (list_refs_only) {
2558 error = list_remote_refs(&symrefs, &refs);
2559 goto done;
2562 if (pack_hash == NULL) {
2563 if (verbosity >= 0)
2564 printf("Already up-to-date\n");
2565 } else if (verbosity >= 0) {
2566 error = got_object_id_str(&id_str, pack_hash);
2567 if (error)
2568 goto done;
2569 printf("\nFetched %s.pack\n", id_str);
2570 free(id_str);
2571 id_str = NULL;
2574 /* Update references provided with the pack file. */
2575 TAILQ_FOREACH(pe, &refs, entry) {
2576 const char *refname = pe->path;
2577 struct got_object_id *id = pe->data;
2578 struct got_reference *ref;
2579 char *remote_refname;
2581 if (is_wanted_ref(&wanted_refs, refname) &&
2582 !remote->mirror_references) {
2583 error = update_wanted_ref(refname, id,
2584 remote->name, verbosity, repo);
2585 if (error)
2586 goto done;
2587 continue;
2590 if (remote->mirror_references ||
2591 strncmp("refs/tags/", refname, 10) == 0) {
2592 error = got_ref_open(&ref, repo, refname, 1);
2593 if (error) {
2594 if (error->code != GOT_ERR_NOT_REF)
2595 goto done;
2596 error = create_ref(refname, id, verbosity,
2597 repo);
2598 if (error)
2599 goto done;
2600 } else {
2601 error = update_ref(ref, id, replace_tags,
2602 verbosity, repo);
2603 unlock_err = got_ref_unlock(ref);
2604 if (unlock_err && error == NULL)
2605 error = unlock_err;
2606 got_ref_close(ref);
2607 if (error)
2608 goto done;
2610 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2611 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2612 remote_name, refname + 11) == -1) {
2613 error = got_error_from_errno("asprintf");
2614 goto done;
2617 error = got_ref_open(&ref, repo, remote_refname, 1);
2618 if (error) {
2619 if (error->code != GOT_ERR_NOT_REF)
2620 goto done;
2621 error = create_ref(remote_refname, id,
2622 verbosity, repo);
2623 if (error)
2624 goto done;
2625 } else {
2626 error = update_ref(ref, id, replace_tags,
2627 verbosity, repo);
2628 unlock_err = got_ref_unlock(ref);
2629 if (unlock_err && error == NULL)
2630 error = unlock_err;
2631 got_ref_close(ref);
2632 if (error)
2633 goto done;
2636 /* Also create a local branch if none exists yet. */
2637 error = got_ref_open(&ref, repo, refname, 1);
2638 if (error) {
2639 if (error->code != GOT_ERR_NOT_REF)
2640 goto done;
2641 error = create_ref(refname, id, verbosity,
2642 repo);
2643 if (error)
2644 goto done;
2645 } else {
2646 unlock_err = got_ref_unlock(ref);
2647 if (unlock_err && error == NULL)
2648 error = unlock_err;
2649 got_ref_close(ref);
2653 if (delete_refs) {
2654 error = delete_missing_refs(&refs, &symrefs, remote,
2655 verbosity, repo);
2656 if (error)
2657 goto done;
2660 if (!remote->mirror_references) {
2661 /* Update remote HEAD reference if the server provided one. */
2662 TAILQ_FOREACH(pe, &symrefs, entry) {
2663 struct got_reference *target_ref;
2664 const char *refname = pe->path;
2665 const char *target = pe->data;
2666 char *remote_refname = NULL, *remote_target = NULL;
2668 if (strcmp(refname, GOT_REF_HEAD) != 0)
2669 continue;
2671 if (strncmp("refs/heads/", target, 11) != 0)
2672 continue;
2674 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2675 remote->name, refname) == -1) {
2676 error = got_error_from_errno("asprintf");
2677 goto done;
2679 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2680 remote->name, target + 11) == -1) {
2681 error = got_error_from_errno("asprintf");
2682 free(remote_refname);
2683 goto done;
2686 error = got_ref_open(&target_ref, repo, remote_target,
2687 0);
2688 if (error) {
2689 free(remote_refname);
2690 free(remote_target);
2691 if (error->code == GOT_ERR_NOT_REF) {
2692 error = NULL;
2693 continue;
2695 goto done;
2697 error = update_symref(remote_refname, target_ref,
2698 verbosity, repo);
2699 free(remote_refname);
2700 free(remote_target);
2701 got_ref_close(target_ref);
2702 if (error)
2703 goto done;
2706 done:
2707 if (fetchpid > 0) {
2708 if (kill(fetchpid, SIGTERM) == -1)
2709 error = got_error_from_errno("kill");
2710 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2711 error = got_error_from_errno("waitpid");
2713 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2714 error = got_error_from_errno("close");
2715 if (repo) {
2716 const struct got_error *close_err = got_repo_close(repo);
2717 if (error == NULL)
2718 error = close_err;
2720 if (worktree)
2721 got_worktree_close(worktree);
2722 if (pack_fds) {
2723 const struct got_error *pack_err =
2724 got_repo_pack_fds_close(pack_fds);
2725 if (error == NULL)
2726 error = pack_err;
2728 TAILQ_FOREACH(pe, &refs, entry) {
2729 free((void *)pe->path);
2730 free(pe->data);
2732 got_pathlist_free(&refs);
2733 TAILQ_FOREACH(pe, &symrefs, entry) {
2734 free((void *)pe->path);
2735 free(pe->data);
2737 got_pathlist_free(&symrefs);
2738 got_pathlist_free(&wanted_branches);
2739 got_pathlist_free(&wanted_refs);
2740 free(id_str);
2741 free(cwd);
2742 free(repo_path);
2743 free(pack_hash);
2744 free(proto);
2745 free(host);
2746 free(port);
2747 free(server_path);
2748 free(repo_name);
2749 return error;
2753 __dead static void
2754 usage_checkout(void)
2756 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2757 "[-p prefix] [-q] repository-path [worktree-path]\n",
2758 getprogname());
2759 exit(1);
2762 static void
2763 show_worktree_base_ref_warning(void)
2765 fprintf(stderr, "%s: warning: could not create a reference "
2766 "to the work tree's base commit; the commit could be "
2767 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2768 "repository writable and running 'got update' will prevent this\n",
2769 getprogname());
2772 struct got_checkout_progress_arg {
2773 const char *worktree_path;
2774 int had_base_commit_ref_error;
2775 int verbosity;
2778 static const struct got_error *
2779 checkout_progress(void *arg, unsigned char status, const char *path)
2781 struct got_checkout_progress_arg *a = arg;
2783 /* Base commit bump happens silently. */
2784 if (status == GOT_STATUS_BUMP_BASE)
2785 return NULL;
2787 if (status == GOT_STATUS_BASE_REF_ERR) {
2788 a->had_base_commit_ref_error = 1;
2789 return NULL;
2792 while (path[0] == '/')
2793 path++;
2795 if (a->verbosity >= 0)
2796 printf("%c %s/%s\n", status, a->worktree_path, path);
2798 return NULL;
2801 static const struct got_error *
2802 check_cancelled(void *arg)
2804 if (sigint_received || sigpipe_received)
2805 return got_error(GOT_ERR_CANCELLED);
2806 return NULL;
2809 static const struct got_error *
2810 check_linear_ancestry(struct got_object_id *commit_id,
2811 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2812 struct got_repository *repo)
2814 const struct got_error *err = NULL;
2815 struct got_object_id *yca_id;
2817 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2818 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2819 if (err)
2820 return err;
2822 if (yca_id == NULL)
2823 return got_error(GOT_ERR_ANCESTRY);
2826 * Require a straight line of history between the target commit
2827 * and the work tree's base commit.
2829 * Non-linear situations such as this require a rebase:
2831 * (commit) D F (base_commit)
2832 * \ /
2833 * C E
2834 * \ /
2835 * B (yca)
2836 * |
2837 * A
2839 * 'got update' only handles linear cases:
2840 * Update forwards in time: A (base/yca) - B - C - D (commit)
2841 * Update backwards in time: D (base) - C - B - A (commit/yca)
2843 if (allow_forwards_in_time_only) {
2844 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2845 return got_error(GOT_ERR_ANCESTRY);
2846 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2847 got_object_id_cmp(base_commit_id, yca_id) != 0)
2848 return got_error(GOT_ERR_ANCESTRY);
2850 free(yca_id);
2851 return NULL;
2854 static const struct got_error *
2855 check_same_branch(struct got_object_id *commit_id,
2856 struct got_reference *head_ref, struct got_object_id *yca_id,
2857 struct got_repository *repo)
2859 const struct got_error *err = NULL;
2860 struct got_commit_graph *graph = NULL;
2861 struct got_object_id *head_commit_id = NULL;
2862 int is_same_branch = 0;
2864 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2865 if (err)
2866 goto done;
2868 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2869 is_same_branch = 1;
2870 goto done;
2872 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2873 is_same_branch = 1;
2874 goto done;
2877 err = got_commit_graph_open(&graph, "/", 1);
2878 if (err)
2879 goto done;
2881 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2882 check_cancelled, NULL);
2883 if (err)
2884 goto done;
2886 for (;;) {
2887 struct got_object_id *id;
2888 err = got_commit_graph_iter_next(&id, graph, repo,
2889 check_cancelled, NULL);
2890 if (err) {
2891 if (err->code == GOT_ERR_ITER_COMPLETED)
2892 err = NULL;
2893 break;
2896 if (id) {
2897 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2898 break;
2899 if (got_object_id_cmp(id, commit_id) == 0) {
2900 is_same_branch = 1;
2901 break;
2905 done:
2906 if (graph)
2907 got_commit_graph_close(graph);
2908 free(head_commit_id);
2909 if (!err && !is_same_branch)
2910 err = got_error(GOT_ERR_ANCESTRY);
2911 return err;
2914 static const struct got_error *
2915 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2917 static char msg[512];
2918 const char *branch_name;
2920 if (got_ref_is_symbolic(ref))
2921 branch_name = got_ref_get_symref_target(ref);
2922 else
2923 branch_name = got_ref_get_name(ref);
2925 if (strncmp("refs/heads/", branch_name, 11) == 0)
2926 branch_name += 11;
2928 snprintf(msg, sizeof(msg),
2929 "target commit is not contained in branch '%s'; "
2930 "the branch to use must be specified with -b; "
2931 "if necessary a new branch can be created for "
2932 "this commit with 'got branch -c %s BRANCH_NAME'",
2933 branch_name, commit_id_str);
2935 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2938 static const struct got_error *
2939 cmd_checkout(int argc, char *argv[])
2941 const struct got_error *error = NULL;
2942 struct got_repository *repo = NULL;
2943 struct got_reference *head_ref = NULL, *ref = NULL;
2944 struct got_worktree *worktree = NULL;
2945 char *repo_path = NULL;
2946 char *worktree_path = NULL;
2947 const char *path_prefix = "";
2948 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2949 char *commit_id_str = NULL;
2950 struct got_object_id *commit_id = NULL;
2951 char *cwd = NULL;
2952 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2953 struct got_pathlist_head paths;
2954 struct got_checkout_progress_arg cpa;
2955 int *pack_fds = NULL;
2957 TAILQ_INIT(&paths);
2959 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2960 switch (ch) {
2961 case 'b':
2962 branch_name = optarg;
2963 break;
2964 case 'c':
2965 commit_id_str = strdup(optarg);
2966 if (commit_id_str == NULL)
2967 return got_error_from_errno("strdup");
2968 break;
2969 case 'E':
2970 allow_nonempty = 1;
2971 break;
2972 case 'p':
2973 path_prefix = optarg;
2974 break;
2975 case 'q':
2976 verbosity = -1;
2977 break;
2978 default:
2979 usage_checkout();
2980 /* NOTREACHED */
2984 argc -= optind;
2985 argv += optind;
2987 #ifndef PROFILE
2988 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2989 "unveil", NULL) == -1)
2990 err(1, "pledge");
2991 #endif
2992 if (argc == 1) {
2993 char *base, *dotgit;
2994 const char *path;
2995 repo_path = realpath(argv[0], NULL);
2996 if (repo_path == NULL)
2997 return got_error_from_errno2("realpath", argv[0]);
2998 cwd = getcwd(NULL, 0);
2999 if (cwd == NULL) {
3000 error = got_error_from_errno("getcwd");
3001 goto done;
3003 if (path_prefix[0])
3004 path = path_prefix;
3005 else
3006 path = repo_path;
3007 error = got_path_basename(&base, path);
3008 if (error)
3009 goto done;
3010 dotgit = strstr(base, ".git");
3011 if (dotgit)
3012 *dotgit = '\0';
3013 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3014 error = got_error_from_errno("asprintf");
3015 free(base);
3016 goto done;
3018 free(base);
3019 } else if (argc == 2) {
3020 repo_path = realpath(argv[0], NULL);
3021 if (repo_path == NULL) {
3022 error = got_error_from_errno2("realpath", argv[0]);
3023 goto done;
3025 worktree_path = realpath(argv[1], NULL);
3026 if (worktree_path == NULL) {
3027 if (errno != ENOENT) {
3028 error = got_error_from_errno2("realpath",
3029 argv[1]);
3030 goto done;
3032 worktree_path = strdup(argv[1]);
3033 if (worktree_path == NULL) {
3034 error = got_error_from_errno("strdup");
3035 goto done;
3038 } else
3039 usage_checkout();
3041 got_path_strip_trailing_slashes(repo_path);
3042 got_path_strip_trailing_slashes(worktree_path);
3044 error = got_repo_pack_fds_open(&pack_fds);
3045 if (error != NULL)
3046 goto done;
3048 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3049 if (error != NULL)
3050 goto done;
3052 /* Pre-create work tree path for unveil(2) */
3053 error = got_path_mkdir(worktree_path);
3054 if (error) {
3055 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3056 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3057 goto done;
3058 if (!allow_nonempty &&
3059 !got_path_dir_is_empty(worktree_path)) {
3060 error = got_error_path(worktree_path,
3061 GOT_ERR_DIR_NOT_EMPTY);
3062 goto done;
3066 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3067 if (error)
3068 goto done;
3070 error = got_ref_open(&head_ref, repo, branch_name, 0);
3071 if (error != NULL)
3072 goto done;
3074 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3075 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3076 goto done;
3078 error = got_worktree_open(&worktree, worktree_path);
3079 if (error != NULL)
3080 goto done;
3082 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3083 path_prefix);
3084 if (error != NULL)
3085 goto done;
3086 if (!same_path_prefix) {
3087 error = got_error(GOT_ERR_PATH_PREFIX);
3088 goto done;
3091 if (commit_id_str) {
3092 struct got_reflist_head refs;
3093 TAILQ_INIT(&refs);
3094 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3095 NULL);
3096 if (error)
3097 goto done;
3098 error = got_repo_match_object_id(&commit_id, NULL,
3099 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3100 got_ref_list_free(&refs);
3101 if (error)
3102 goto done;
3103 error = check_linear_ancestry(commit_id,
3104 got_worktree_get_base_commit_id(worktree), 0, repo);
3105 if (error != NULL) {
3106 if (error->code == GOT_ERR_ANCESTRY) {
3107 error = checkout_ancestry_error(
3108 head_ref, commit_id_str);
3110 goto done;
3112 error = check_same_branch(commit_id, head_ref, NULL, repo);
3113 if (error) {
3114 if (error->code == GOT_ERR_ANCESTRY) {
3115 error = checkout_ancestry_error(
3116 head_ref, commit_id_str);
3118 goto done;
3120 error = got_worktree_set_base_commit_id(worktree, repo,
3121 commit_id);
3122 if (error)
3123 goto done;
3124 /* Expand potentially abbreviated commit ID string. */
3125 free(commit_id_str);
3126 error = got_object_id_str(&commit_id_str, commit_id);
3127 if (error)
3128 goto done;
3129 } else {
3130 commit_id = got_object_id_dup(
3131 got_worktree_get_base_commit_id(worktree));
3132 if (commit_id == NULL) {
3133 error = got_error_from_errno("got_object_id_dup");
3134 goto done;
3136 error = got_object_id_str(&commit_id_str, commit_id);
3137 if (error)
3138 goto done;
3141 error = got_pathlist_append(&paths, "", NULL);
3142 if (error)
3143 goto done;
3144 cpa.worktree_path = worktree_path;
3145 cpa.had_base_commit_ref_error = 0;
3146 cpa.verbosity = verbosity;
3147 error = got_worktree_checkout_files(worktree, &paths, repo,
3148 checkout_progress, &cpa, check_cancelled, NULL);
3149 if (error != NULL)
3150 goto done;
3152 if (got_ref_is_symbolic(head_ref)) {
3153 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3154 if (error)
3155 goto done;
3156 refname = got_ref_get_name(ref);
3157 } else
3158 refname = got_ref_get_name(head_ref);
3159 printf("Checked out %s: %s\n", refname, commit_id_str);
3160 printf("Now shut up and hack\n");
3161 if (cpa.had_base_commit_ref_error)
3162 show_worktree_base_ref_warning();
3163 done:
3164 if (pack_fds) {
3165 const struct got_error *pack_err =
3166 got_repo_pack_fds_close(pack_fds);
3167 if (error == NULL)
3168 error = pack_err;
3170 if (head_ref)
3171 got_ref_close(head_ref);
3172 if (ref)
3173 got_ref_close(ref);
3174 got_pathlist_free(&paths);
3175 free(commit_id_str);
3176 free(commit_id);
3177 free(repo_path);
3178 free(worktree_path);
3179 free(cwd);
3180 return error;
3183 struct got_update_progress_arg {
3184 int did_something;
3185 int conflicts;
3186 int obstructed;
3187 int not_updated;
3188 int missing;
3189 int not_deleted;
3190 int unversioned;
3191 int verbosity;
3194 static void
3195 print_update_progress_stats(struct got_update_progress_arg *upa)
3197 if (!upa->did_something)
3198 return;
3200 if (upa->conflicts > 0)
3201 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3202 if (upa->obstructed > 0)
3203 printf("File paths obstructed by a non-regular file: %d\n",
3204 upa->obstructed);
3205 if (upa->not_updated > 0)
3206 printf("Files not updated because of existing merge "
3207 "conflicts: %d\n", upa->not_updated);
3211 * The meaning of some status codes differs between merge-style operations and
3212 * update operations. For example, the ! status code means "file was missing"
3213 * if changes were merged into the work tree, and "missing file was restored"
3214 * if the work tree was updated. This function should be used by any operation
3215 * which merges changes into the work tree without updating the work tree.
3217 static void
3218 print_merge_progress_stats(struct got_update_progress_arg *upa)
3220 if (!upa->did_something)
3221 return;
3223 if (upa->conflicts > 0)
3224 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3225 if (upa->obstructed > 0)
3226 printf("File paths obstructed by a non-regular file: %d\n",
3227 upa->obstructed);
3228 if (upa->missing > 0)
3229 printf("Files which had incoming changes but could not be "
3230 "found in the work tree: %d\n", upa->missing);
3231 if (upa->not_deleted > 0)
3232 printf("Files not deleted due to differences in deleted "
3233 "content: %d\n", upa->not_deleted);
3234 if (upa->unversioned > 0)
3235 printf("Files not merged because an unversioned file was "
3236 "found in the work tree: %d\n", upa->unversioned);
3239 __dead static void
3240 usage_update(void)
3242 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3243 "[path ...]\n",
3244 getprogname());
3245 exit(1);
3248 static const struct got_error *
3249 update_progress(void *arg, unsigned char status, const char *path)
3251 struct got_update_progress_arg *upa = arg;
3253 if (status == GOT_STATUS_EXISTS ||
3254 status == GOT_STATUS_BASE_REF_ERR)
3255 return NULL;
3257 upa->did_something = 1;
3259 /* Base commit bump happens silently. */
3260 if (status == GOT_STATUS_BUMP_BASE)
3261 return NULL;
3263 if (status == GOT_STATUS_CONFLICT)
3264 upa->conflicts++;
3265 if (status == GOT_STATUS_OBSTRUCTED)
3266 upa->obstructed++;
3267 if (status == GOT_STATUS_CANNOT_UPDATE)
3268 upa->not_updated++;
3269 if (status == GOT_STATUS_MISSING)
3270 upa->missing++;
3271 if (status == GOT_STATUS_CANNOT_DELETE)
3272 upa->not_deleted++;
3273 if (status == GOT_STATUS_UNVERSIONED)
3274 upa->unversioned++;
3276 while (path[0] == '/')
3277 path++;
3278 if (upa->verbosity >= 0)
3279 printf("%c %s\n", status, path);
3281 return NULL;
3284 static const struct got_error *
3285 switch_head_ref(struct got_reference *head_ref,
3286 struct got_object_id *commit_id, struct got_worktree *worktree,
3287 struct got_repository *repo)
3289 const struct got_error *err = NULL;
3290 char *base_id_str;
3291 int ref_has_moved = 0;
3293 /* Trivial case: switching between two different references. */
3294 if (strcmp(got_ref_get_name(head_ref),
3295 got_worktree_get_head_ref_name(worktree)) != 0) {
3296 printf("Switching work tree from %s to %s\n",
3297 got_worktree_get_head_ref_name(worktree),
3298 got_ref_get_name(head_ref));
3299 return got_worktree_set_head_ref(worktree, head_ref);
3302 err = check_linear_ancestry(commit_id,
3303 got_worktree_get_base_commit_id(worktree), 0, repo);
3304 if (err) {
3305 if (err->code != GOT_ERR_ANCESTRY)
3306 return err;
3307 ref_has_moved = 1;
3309 if (!ref_has_moved)
3310 return NULL;
3312 /* Switching to a rebased branch with the same reference name. */
3313 err = got_object_id_str(&base_id_str,
3314 got_worktree_get_base_commit_id(worktree));
3315 if (err)
3316 return err;
3317 printf("Reference %s now points at a different branch\n",
3318 got_worktree_get_head_ref_name(worktree));
3319 printf("Switching work tree from %s to %s\n", base_id_str,
3320 got_worktree_get_head_ref_name(worktree));
3321 return NULL;
3324 static const struct got_error *
3325 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3327 const struct got_error *err;
3328 int in_progress;
3330 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3331 if (err)
3332 return err;
3333 if (in_progress)
3334 return got_error(GOT_ERR_REBASING);
3336 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3337 if (err)
3338 return err;
3339 if (in_progress)
3340 return got_error(GOT_ERR_HISTEDIT_BUSY);
3342 return NULL;
3345 static const struct got_error *
3346 check_merge_in_progress(struct got_worktree *worktree,
3347 struct got_repository *repo)
3349 const struct got_error *err;
3350 int in_progress;
3352 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3353 if (err)
3354 return err;
3355 if (in_progress)
3356 return got_error(GOT_ERR_MERGE_BUSY);
3358 return NULL;
3361 static const struct got_error *
3362 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3363 char *argv[], struct got_worktree *worktree)
3365 const struct got_error *err = NULL;
3366 char *path;
3367 struct got_pathlist_entry *new;
3368 int i;
3370 if (argc == 0) {
3371 path = strdup("");
3372 if (path == NULL)
3373 return got_error_from_errno("strdup");
3374 return got_pathlist_append(paths, path, NULL);
3377 for (i = 0; i < argc; i++) {
3378 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3379 if (err)
3380 break;
3381 err = got_pathlist_insert(&new, paths, path, NULL);
3382 if (err || new == NULL /* duplicate */) {
3383 free(path);
3384 if (err)
3385 break;
3389 return err;
3392 static const struct got_error *
3393 wrap_not_worktree_error(const struct got_error *orig_err,
3394 const char *cmdname, const char *path)
3396 const struct got_error *err;
3397 struct got_repository *repo;
3398 static char msg[512];
3399 int *pack_fds = NULL;
3401 err = got_repo_pack_fds_open(&pack_fds);
3402 if (err)
3403 return err;
3405 err = got_repo_open(&repo, path, NULL, pack_fds);
3406 if (err)
3407 return orig_err;
3409 snprintf(msg, sizeof(msg),
3410 "'got %s' needs a work tree in addition to a git repository\n"
3411 "Work trees can be checked out from this Git repository with "
3412 "'got checkout'.\n"
3413 "The got(1) manual page contains more information.", cmdname);
3414 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3415 got_repo_close(repo);
3416 if (pack_fds) {
3417 const struct got_error *pack_err =
3418 got_repo_pack_fds_close(pack_fds);
3419 if (err == NULL)
3420 err = pack_err;
3422 return err;
3425 static const struct got_error *
3426 cmd_update(int argc, char *argv[])
3428 const struct got_error *error = NULL;
3429 struct got_repository *repo = NULL;
3430 struct got_worktree *worktree = NULL;
3431 char *worktree_path = NULL;
3432 struct got_object_id *commit_id = NULL;
3433 char *commit_id_str = NULL;
3434 const char *branch_name = NULL;
3435 struct got_reference *head_ref = NULL;
3436 struct got_pathlist_head paths;
3437 struct got_pathlist_entry *pe;
3438 int ch, verbosity = 0;
3439 struct got_update_progress_arg upa;
3440 int *pack_fds = NULL;
3442 TAILQ_INIT(&paths);
3444 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3445 switch (ch) {
3446 case 'b':
3447 branch_name = optarg;
3448 break;
3449 case 'c':
3450 commit_id_str = strdup(optarg);
3451 if (commit_id_str == NULL)
3452 return got_error_from_errno("strdup");
3453 break;
3454 case 'q':
3455 verbosity = -1;
3456 break;
3457 default:
3458 usage_update();
3459 /* NOTREACHED */
3463 argc -= optind;
3464 argv += optind;
3466 #ifndef PROFILE
3467 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3468 "unveil", NULL) == -1)
3469 err(1, "pledge");
3470 #endif
3471 worktree_path = getcwd(NULL, 0);
3472 if (worktree_path == NULL) {
3473 error = got_error_from_errno("getcwd");
3474 goto done;
3477 error = got_repo_pack_fds_open(&pack_fds);
3478 if (error != NULL)
3479 goto done;
3481 error = got_worktree_open(&worktree, worktree_path);
3482 if (error) {
3483 if (error->code == GOT_ERR_NOT_WORKTREE)
3484 error = wrap_not_worktree_error(error, "update",
3485 worktree_path);
3486 goto done;
3489 error = check_rebase_or_histedit_in_progress(worktree);
3490 if (error)
3491 goto done;
3493 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3494 NULL, pack_fds);
3495 if (error != NULL)
3496 goto done;
3498 error = apply_unveil(got_repo_get_path(repo), 0,
3499 got_worktree_get_root_path(worktree));
3500 if (error)
3501 goto done;
3503 error = check_merge_in_progress(worktree, repo);
3504 if (error)
3505 goto done;
3507 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3508 if (error)
3509 goto done;
3511 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3512 got_worktree_get_head_ref_name(worktree), 0);
3513 if (error != NULL)
3514 goto done;
3515 if (commit_id_str == NULL) {
3516 error = got_ref_resolve(&commit_id, repo, head_ref);
3517 if (error != NULL)
3518 goto done;
3519 error = got_object_id_str(&commit_id_str, commit_id);
3520 if (error != NULL)
3521 goto done;
3522 } else {
3523 struct got_reflist_head refs;
3524 TAILQ_INIT(&refs);
3525 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3526 NULL);
3527 if (error)
3528 goto done;
3529 error = got_repo_match_object_id(&commit_id, NULL,
3530 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3531 got_ref_list_free(&refs);
3532 free(commit_id_str);
3533 commit_id_str = NULL;
3534 if (error)
3535 goto done;
3536 error = got_object_id_str(&commit_id_str, commit_id);
3537 if (error)
3538 goto done;
3541 if (branch_name) {
3542 struct got_object_id *head_commit_id;
3543 TAILQ_FOREACH(pe, &paths, entry) {
3544 if (pe->path_len == 0)
3545 continue;
3546 error = got_error_msg(GOT_ERR_BAD_PATH,
3547 "switching between branches requires that "
3548 "the entire work tree gets updated");
3549 goto done;
3551 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3552 if (error)
3553 goto done;
3554 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3555 repo);
3556 free(head_commit_id);
3557 if (error != NULL)
3558 goto done;
3559 error = check_same_branch(commit_id, head_ref, NULL, repo);
3560 if (error)
3561 goto done;
3562 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3563 if (error)
3564 goto done;
3565 } else {
3566 error = check_linear_ancestry(commit_id,
3567 got_worktree_get_base_commit_id(worktree), 0, repo);
3568 if (error != NULL) {
3569 if (error->code == GOT_ERR_ANCESTRY)
3570 error = got_error(GOT_ERR_BRANCH_MOVED);
3571 goto done;
3573 error = check_same_branch(commit_id, head_ref, NULL, repo);
3574 if (error)
3575 goto done;
3578 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3579 commit_id) != 0) {
3580 error = got_worktree_set_base_commit_id(worktree, repo,
3581 commit_id);
3582 if (error)
3583 goto done;
3586 memset(&upa, 0, sizeof(upa));
3587 upa.verbosity = verbosity;
3588 error = got_worktree_checkout_files(worktree, &paths, repo,
3589 update_progress, &upa, check_cancelled, NULL);
3590 if (error != NULL)
3591 goto done;
3593 if (upa.did_something) {
3594 printf("Updated to %s: %s\n",
3595 got_worktree_get_head_ref_name(worktree), commit_id_str);
3596 } else
3597 printf("Already up-to-date\n");
3599 print_update_progress_stats(&upa);
3600 done:
3601 if (pack_fds) {
3602 const struct got_error *pack_err =
3603 got_repo_pack_fds_close(pack_fds);
3604 if (error == NULL)
3605 error = pack_err;
3607 free(worktree_path);
3608 TAILQ_FOREACH(pe, &paths, entry)
3609 free((char *)pe->path);
3610 got_pathlist_free(&paths);
3611 free(commit_id);
3612 free(commit_id_str);
3613 return error;
3616 static const struct got_error *
3617 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3618 const char *path, int diff_context, int ignore_whitespace,
3619 int force_text_diff, struct got_repository *repo, FILE *outfile)
3621 const struct got_error *err = NULL;
3622 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3623 FILE *f1 = NULL, *f2 = NULL;
3624 int fd1 = -1, fd2 = -1;
3626 fd1 = got_opentempfd();
3627 if (fd1 == -1)
3628 return got_error_from_errno("got_opentempfd");
3629 fd2 = got_opentempfd();
3630 if (fd2 == -1) {
3631 err = got_error_from_errno("got_opentempfd");
3632 goto done;
3635 if (blob_id1) {
3636 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3637 fd1);
3638 if (err)
3639 goto done;
3642 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3643 if (err)
3644 goto done;
3646 f1 = got_opentemp();
3647 if (f1 == NULL) {
3648 err = got_error_from_errno("got_opentemp");
3649 goto done;
3651 f2 = got_opentemp();
3652 if (f2 == NULL) {
3653 err = got_error_from_errno("got_opentemp");
3654 goto done;
3657 while (path[0] == '/')
3658 path++;
3659 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3660 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3661 force_text_diff, outfile);
3662 done:
3663 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3664 err = got_error_from_errno("close");
3665 if (blob1)
3666 got_object_blob_close(blob1);
3667 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3668 err = got_error_from_errno("close");
3669 got_object_blob_close(blob2);
3670 if (f1 && fclose(f1) == EOF && err == NULL)
3671 err = got_error_from_errno("fclose");
3672 if (f2 && fclose(f2) == EOF && err == NULL)
3673 err = got_error_from_errno("fclose");
3674 return err;
3677 static const struct got_error *
3678 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3679 const char *path, int diff_context, int ignore_whitespace,
3680 int force_text_diff, struct got_repository *repo, FILE *outfile)
3682 const struct got_error *err = NULL;
3683 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3684 struct got_diff_blob_output_unidiff_arg arg;
3685 FILE *f1 = NULL, *f2 = NULL;
3686 int fd1 = -1, fd2 = -1;
3688 if (tree_id1) {
3689 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3690 if (err)
3691 goto done;
3692 fd1 = got_opentempfd();
3693 if (fd1 == -1) {
3694 err = got_error_from_errno("got_opentempfd");
3695 goto done;
3699 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3700 if (err)
3701 goto done;
3703 f1 = got_opentemp();
3704 if (f1 == NULL) {
3705 err = got_error_from_errno("got_opentemp");
3706 goto done;
3709 f2 = got_opentemp();
3710 if (f2 == NULL) {
3711 err = got_error_from_errno("got_opentemp");
3712 goto done;
3714 fd2 = got_opentempfd();
3715 if (fd2 == -1) {
3716 err = got_error_from_errno("got_opentempfd");
3717 goto done;
3719 arg.diff_context = diff_context;
3720 arg.ignore_whitespace = ignore_whitespace;
3721 arg.force_text_diff = force_text_diff;
3722 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3723 arg.outfile = outfile;
3724 arg.line_offsets = NULL;
3725 arg.nlines = 0;
3726 while (path[0] == '/')
3727 path++;
3728 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3729 got_diff_blob_output_unidiff, &arg, 1);
3730 done:
3731 if (tree1)
3732 got_object_tree_close(tree1);
3733 if (tree2)
3734 got_object_tree_close(tree2);
3735 if (f1 && fclose(f1) == EOF && err == NULL)
3736 err = got_error_from_errno("fclose");
3737 if (f2 && fclose(f2) == EOF && err == NULL)
3738 err = got_error_from_errno("fclose");
3739 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3740 err = got_error_from_errno("close");
3741 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3742 err = got_error_from_errno("close");
3743 return err;
3746 static const struct got_error *
3747 get_changed_paths(struct got_pathlist_head *paths,
3748 struct got_commit_object *commit, struct got_repository *repo)
3750 const struct got_error *err = NULL;
3751 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3752 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3753 struct got_object_qid *qid;
3755 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3756 if (qid != NULL) {
3757 struct got_commit_object *pcommit;
3758 err = got_object_open_as_commit(&pcommit, repo,
3759 &qid->id);
3760 if (err)
3761 return err;
3763 tree_id1 = got_object_id_dup(
3764 got_object_commit_get_tree_id(pcommit));
3765 if (tree_id1 == NULL) {
3766 got_object_commit_close(pcommit);
3767 return got_error_from_errno("got_object_id_dup");
3769 got_object_commit_close(pcommit);
3773 if (tree_id1) {
3774 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3775 if (err)
3776 goto done;
3779 tree_id2 = got_object_commit_get_tree_id(commit);
3780 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3781 if (err)
3782 goto done;
3784 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3785 got_diff_tree_collect_changed_paths, paths, 0);
3786 done:
3787 if (tree1)
3788 got_object_tree_close(tree1);
3789 if (tree2)
3790 got_object_tree_close(tree2);
3791 free(tree_id1);
3792 return err;
3795 static const struct got_error *
3796 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3797 const char *path, int diff_context, struct got_repository *repo,
3798 FILE *outfile)
3800 const struct got_error *err = NULL;
3801 struct got_commit_object *pcommit = NULL;
3802 char *id_str1 = NULL, *id_str2 = NULL;
3803 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3804 struct got_object_qid *qid;
3806 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3807 if (qid != NULL) {
3808 err = got_object_open_as_commit(&pcommit, repo,
3809 &qid->id);
3810 if (err)
3811 return err;
3812 err = got_object_id_str(&id_str1, &qid->id);
3813 if (err)
3814 goto done;
3817 err = got_object_id_str(&id_str2, id);
3818 if (err)
3819 goto done;
3821 if (path && path[0] != '\0') {
3822 int obj_type;
3823 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3824 if (err)
3825 goto done;
3826 if (pcommit) {
3827 err = got_object_id_by_path(&obj_id1, repo,
3828 pcommit, path);
3829 if (err) {
3830 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3831 free(obj_id2);
3832 goto done;
3836 err = got_object_get_type(&obj_type, repo, obj_id2);
3837 if (err) {
3838 free(obj_id2);
3839 goto done;
3841 fprintf(outfile,
3842 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3843 fprintf(outfile, "commit - %s\n",
3844 id_str1 ? id_str1 : "/dev/null");
3845 fprintf(outfile, "commit + %s\n", id_str2);
3846 switch (obj_type) {
3847 case GOT_OBJ_TYPE_BLOB:
3848 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3849 0, 0, repo, outfile);
3850 break;
3851 case GOT_OBJ_TYPE_TREE:
3852 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3853 0, 0, repo, outfile);
3854 break;
3855 default:
3856 err = got_error(GOT_ERR_OBJ_TYPE);
3857 break;
3859 free(obj_id1);
3860 free(obj_id2);
3861 } else {
3862 obj_id2 = got_object_commit_get_tree_id(commit);
3863 if (pcommit)
3864 obj_id1 = got_object_commit_get_tree_id(pcommit);
3865 fprintf(outfile,
3866 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3867 fprintf(outfile, "commit - %s\n",
3868 id_str1 ? id_str1 : "/dev/null");
3869 fprintf(outfile, "commit + %s\n", id_str2);
3870 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3871 repo, outfile);
3873 done:
3874 free(id_str1);
3875 free(id_str2);
3876 if (pcommit)
3877 got_object_commit_close(pcommit);
3878 return err;
3881 static char *
3882 get_datestr(time_t *time, char *datebuf)
3884 struct tm mytm, *tm;
3885 char *p, *s;
3887 tm = gmtime_r(time, &mytm);
3888 if (tm == NULL)
3889 return NULL;
3890 s = asctime_r(tm, datebuf);
3891 if (s == NULL)
3892 return NULL;
3893 p = strchr(s, '\n');
3894 if (p)
3895 *p = '\0';
3896 return s;
3899 static const struct got_error *
3900 match_commit(int *have_match, struct got_object_id *id,
3901 struct got_commit_object *commit, regex_t *regex)
3903 const struct got_error *err = NULL;
3904 regmatch_t regmatch;
3905 char *id_str = NULL, *logmsg = NULL;
3907 *have_match = 0;
3909 err = got_object_id_str(&id_str, id);
3910 if (err)
3911 return err;
3913 err = got_object_commit_get_logmsg(&logmsg, commit);
3914 if (err)
3915 goto done;
3917 if (regexec(regex, got_object_commit_get_author(commit), 1,
3918 &regmatch, 0) == 0 ||
3919 regexec(regex, got_object_commit_get_committer(commit), 1,
3920 &regmatch, 0) == 0 ||
3921 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3922 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3923 *have_match = 1;
3924 done:
3925 free(id_str);
3926 free(logmsg);
3927 return err;
3930 static void
3931 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3932 regex_t *regex)
3934 regmatch_t regmatch;
3935 struct got_pathlist_entry *pe;
3937 *have_match = 0;
3939 TAILQ_FOREACH(pe, changed_paths, entry) {
3940 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3941 *have_match = 1;
3942 break;
3947 static const struct got_error *
3948 match_patch(int *have_match, struct got_commit_object *commit,
3949 struct got_object_id *id, const char *path, int diff_context,
3950 struct got_repository *repo, regex_t *regex, FILE *f)
3952 const struct got_error *err = NULL;
3953 char *line = NULL;
3954 size_t linesize = 0;
3955 ssize_t linelen;
3956 regmatch_t regmatch;
3958 *have_match = 0;
3960 err = got_opentemp_truncate(f);
3961 if (err)
3962 return err;
3964 err = print_patch(commit, id, path, diff_context, repo, f);
3965 if (err)
3966 goto done;
3968 if (fseeko(f, 0L, SEEK_SET) == -1) {
3969 err = got_error_from_errno("fseeko");
3970 goto done;
3973 while ((linelen = getline(&line, &linesize, f)) != -1) {
3974 if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3975 *have_match = 1;
3976 break;
3979 done:
3980 free(line);
3981 return err;
3984 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3986 static const struct got_error*
3987 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3988 struct got_object_id *id, struct got_repository *repo,
3989 int local_only)
3991 static const struct got_error *err = NULL;
3992 struct got_reflist_entry *re;
3993 char *s;
3994 const char *name;
3996 *refs_str = NULL;
3998 TAILQ_FOREACH(re, refs, entry) {
3999 struct got_tag_object *tag = NULL;
4000 struct got_object_id *ref_id;
4001 int cmp;
4003 name = got_ref_get_name(re->ref);
4004 if (strcmp(name, GOT_REF_HEAD) == 0)
4005 continue;
4006 if (strncmp(name, "refs/", 5) == 0)
4007 name += 5;
4008 if (strncmp(name, "got/", 4) == 0)
4009 continue;
4010 if (strncmp(name, "heads/", 6) == 0)
4011 name += 6;
4012 if (strncmp(name, "remotes/", 8) == 0) {
4013 if (local_only)
4014 continue;
4015 name += 8;
4016 s = strstr(name, "/" GOT_REF_HEAD);
4017 if (s != NULL && s[strlen(s)] == '\0')
4018 continue;
4020 err = got_ref_resolve(&ref_id, repo, re->ref);
4021 if (err)
4022 break;
4023 if (strncmp(name, "tags/", 5) == 0) {
4024 err = got_object_open_as_tag(&tag, repo, ref_id);
4025 if (err) {
4026 if (err->code != GOT_ERR_OBJ_TYPE) {
4027 free(ref_id);
4028 break;
4030 /* Ref points at something other than a tag. */
4031 err = NULL;
4032 tag = NULL;
4035 cmp = got_object_id_cmp(tag ?
4036 got_object_tag_get_object_id(tag) : ref_id, id);
4037 free(ref_id);
4038 if (tag)
4039 got_object_tag_close(tag);
4040 if (cmp != 0)
4041 continue;
4042 s = *refs_str;
4043 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4044 s ? ", " : "", name) == -1) {
4045 err = got_error_from_errno("asprintf");
4046 free(s);
4047 *refs_str = NULL;
4048 break;
4050 free(s);
4053 return err;
4056 static const struct got_error *
4057 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4058 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4060 const struct got_error *err = NULL;
4061 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4062 char *comma, *s, *nl;
4063 struct got_reflist_head *refs;
4064 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4065 struct tm tm;
4066 time_t committer_time;
4068 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4069 if (refs) {
4070 err = build_refs_str(&ref_str, refs, id, repo, 1);
4071 if (err)
4072 return err;
4074 /* Display the first matching ref only. */
4075 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4076 *comma = '\0';
4079 if (ref_str == NULL) {
4080 err = got_object_id_str(&id_str, id);
4081 if (err)
4082 return err;
4085 committer_time = got_object_commit_get_committer_time(commit);
4086 if (gmtime_r(&committer_time, &tm) == NULL) {
4087 err = got_error_from_errno("gmtime_r");
4088 goto done;
4090 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4091 err = got_error(GOT_ERR_NO_SPACE);
4092 goto done;
4095 err = got_object_commit_get_logmsg(&logmsg0, commit);
4096 if (err)
4097 goto done;
4099 s = logmsg0;
4100 while (isspace((unsigned char)s[0]))
4101 s++;
4103 nl = strchr(s, '\n');
4104 if (nl) {
4105 *nl = '\0';
4108 if (ref_str)
4109 printf("%s%-7s %s\n", datebuf, ref_str, s);
4110 else
4111 printf("%s%.7s %s\n", datebuf, id_str, s);
4113 if (fflush(stdout) != 0 && err == NULL)
4114 err = got_error_from_errno("fflush");
4115 done:
4116 free(id_str);
4117 free(ref_str);
4118 free(logmsg0);
4119 return err;
4122 static const struct got_error *
4123 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4124 struct got_repository *repo, const char *path,
4125 struct got_pathlist_head *changed_paths, int show_patch,
4126 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4127 const char *custom_refs_str)
4129 const struct got_error *err = NULL;
4130 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4131 char datebuf[26];
4132 time_t committer_time;
4133 const char *author, *committer;
4134 char *refs_str = NULL;
4136 err = got_object_id_str(&id_str, id);
4137 if (err)
4138 return err;
4140 if (custom_refs_str == NULL) {
4141 struct got_reflist_head *refs;
4142 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4143 if (refs) {
4144 err = build_refs_str(&refs_str, refs, id, repo, 0);
4145 if (err)
4146 goto done;
4150 printf(GOT_COMMIT_SEP_STR);
4151 if (custom_refs_str)
4152 printf("commit %s (%s)\n", id_str, custom_refs_str);
4153 else
4154 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4155 refs_str ? refs_str : "", refs_str ? ")" : "");
4156 free(id_str);
4157 id_str = NULL;
4158 free(refs_str);
4159 refs_str = NULL;
4160 printf("from: %s\n", got_object_commit_get_author(commit));
4161 committer_time = got_object_commit_get_committer_time(commit);
4162 datestr = get_datestr(&committer_time, datebuf);
4163 if (datestr)
4164 printf("date: %s UTC\n", datestr);
4165 author = got_object_commit_get_author(commit);
4166 committer = got_object_commit_get_committer(commit);
4167 if (strcmp(author, committer) != 0)
4168 printf("via: %s\n", committer);
4169 if (got_object_commit_get_nparents(commit) > 1) {
4170 const struct got_object_id_queue *parent_ids;
4171 struct got_object_qid *qid;
4172 int n = 1;
4173 parent_ids = got_object_commit_get_parent_ids(commit);
4174 STAILQ_FOREACH(qid, parent_ids, entry) {
4175 err = got_object_id_str(&id_str, &qid->id);
4176 if (err)
4177 goto done;
4178 printf("parent %d: %s\n", n++, id_str);
4179 free(id_str);
4180 id_str = NULL;
4184 err = got_object_commit_get_logmsg(&logmsg0, commit);
4185 if (err)
4186 goto done;
4188 logmsg = logmsg0;
4189 do {
4190 line = strsep(&logmsg, "\n");
4191 if (line)
4192 printf(" %s\n", line);
4193 } while (line);
4194 free(logmsg0);
4196 if (changed_paths) {
4197 struct got_pathlist_entry *pe;
4198 TAILQ_FOREACH(pe, changed_paths, entry) {
4199 struct got_diff_changed_path *cp = pe->data;
4200 printf(" %c %s\n", cp->status, pe->path);
4202 printf("\n");
4204 if (show_patch) {
4205 err = print_patch(commit, id, path, diff_context, repo, stdout);
4206 if (err == 0)
4207 printf("\n");
4210 if (fflush(stdout) != 0 && err == NULL)
4211 err = got_error_from_errno("fflush");
4212 done:
4213 free(id_str);
4214 free(refs_str);
4215 return err;
4218 static const struct got_error *
4219 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4220 struct got_repository *repo, const char *path, int show_changed_paths,
4221 int show_patch, const char *search_pattern, int diff_context, int limit,
4222 int log_branches, int reverse_display_order,
4223 struct got_reflist_object_id_map *refs_idmap, int one_line,
4224 FILE *tmpfile)
4226 const struct got_error *err;
4227 struct got_commit_graph *graph;
4228 regex_t regex;
4229 int have_match;
4230 struct got_object_id_queue reversed_commits;
4231 struct got_object_qid *qid;
4232 struct got_commit_object *commit;
4233 struct got_pathlist_head changed_paths;
4234 struct got_pathlist_entry *pe;
4236 STAILQ_INIT(&reversed_commits);
4237 TAILQ_INIT(&changed_paths);
4239 if (search_pattern && regcomp(&regex, search_pattern,
4240 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4241 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4243 err = got_commit_graph_open(&graph, path, !log_branches);
4244 if (err)
4245 return err;
4246 err = got_commit_graph_iter_start(graph, root_id, repo,
4247 check_cancelled, NULL);
4248 if (err)
4249 goto done;
4250 for (;;) {
4251 struct got_object_id *id;
4253 if (sigint_received || sigpipe_received)
4254 break;
4256 err = got_commit_graph_iter_next(&id, graph, repo,
4257 check_cancelled, NULL);
4258 if (err) {
4259 if (err->code == GOT_ERR_ITER_COMPLETED)
4260 err = NULL;
4261 break;
4263 if (id == NULL)
4264 break;
4266 err = got_object_open_as_commit(&commit, repo, id);
4267 if (err)
4268 break;
4270 if (show_changed_paths && !reverse_display_order) {
4271 err = get_changed_paths(&changed_paths, commit, repo);
4272 if (err)
4273 break;
4276 if (search_pattern) {
4277 err = match_commit(&have_match, id, commit, &regex);
4278 if (err) {
4279 got_object_commit_close(commit);
4280 break;
4282 if (have_match == 0 && show_changed_paths)
4283 match_changed_paths(&have_match,
4284 &changed_paths, &regex);
4285 if (have_match == 0 && show_patch) {
4286 err = match_patch(&have_match, commit, id,
4287 path, diff_context, repo, &regex,
4288 tmpfile);
4289 if (err)
4290 break;
4292 if (have_match == 0) {
4293 got_object_commit_close(commit);
4294 TAILQ_FOREACH(pe, &changed_paths, entry) {
4295 free((char *)pe->path);
4296 free(pe->data);
4298 got_pathlist_free(&changed_paths);
4299 continue;
4303 if (reverse_display_order) {
4304 err = got_object_qid_alloc(&qid, id);
4305 if (err)
4306 break;
4307 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4308 got_object_commit_close(commit);
4309 } else {
4310 if (one_line)
4311 err = print_commit_oneline(commit, id,
4312 repo, refs_idmap);
4313 else
4314 err = print_commit(commit, id, repo, path,
4315 show_changed_paths ? &changed_paths : NULL,
4316 show_patch, diff_context, refs_idmap, NULL);
4317 got_object_commit_close(commit);
4318 if (err)
4319 break;
4321 if ((limit && --limit == 0) ||
4322 (end_id && got_object_id_cmp(id, end_id) == 0))
4323 break;
4325 TAILQ_FOREACH(pe, &changed_paths, entry) {
4326 free((char *)pe->path);
4327 free(pe->data);
4329 got_pathlist_free(&changed_paths);
4331 if (reverse_display_order) {
4332 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4333 err = got_object_open_as_commit(&commit, repo,
4334 &qid->id);
4335 if (err)
4336 break;
4337 if (show_changed_paths) {
4338 err = get_changed_paths(&changed_paths,
4339 commit, repo);
4340 if (err)
4341 break;
4343 if (one_line)
4344 err = print_commit_oneline(commit, &qid->id,
4345 repo, refs_idmap);
4346 else
4347 err = print_commit(commit, &qid->id, repo, path,
4348 show_changed_paths ? &changed_paths : NULL,
4349 show_patch, diff_context, refs_idmap, NULL);
4350 got_object_commit_close(commit);
4351 if (err)
4352 break;
4353 TAILQ_FOREACH(pe, &changed_paths, entry) {
4354 free((char *)pe->path);
4355 free(pe->data);
4357 got_pathlist_free(&changed_paths);
4360 done:
4361 while (!STAILQ_EMPTY(&reversed_commits)) {
4362 qid = STAILQ_FIRST(&reversed_commits);
4363 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4364 got_object_qid_free(qid);
4366 TAILQ_FOREACH(pe, &changed_paths, entry) {
4367 free((char *)pe->path);
4368 free(pe->data);
4370 got_pathlist_free(&changed_paths);
4371 if (search_pattern)
4372 regfree(&regex);
4373 got_commit_graph_close(graph);
4374 return err;
4377 __dead static void
4378 usage_log(void)
4380 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4381 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4382 "[-r repository-path] [-R] [path]\n", getprogname());
4383 exit(1);
4386 static int
4387 get_default_log_limit(void)
4389 const char *got_default_log_limit;
4390 long long n;
4391 const char *errstr;
4393 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4394 if (got_default_log_limit == NULL)
4395 return 0;
4396 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4397 if (errstr != NULL)
4398 return 0;
4399 return n;
4402 static const struct got_error *
4403 cmd_log(int argc, char *argv[])
4405 const struct got_error *error;
4406 struct got_repository *repo = NULL;
4407 struct got_worktree *worktree = NULL;
4408 struct got_object_id *start_id = NULL, *end_id = NULL;
4409 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4410 const char *start_commit = NULL, *end_commit = NULL;
4411 const char *search_pattern = NULL;
4412 int diff_context = -1, ch;
4413 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4414 int reverse_display_order = 0, one_line = 0;
4415 const char *errstr;
4416 struct got_reflist_head refs;
4417 struct got_reflist_object_id_map *refs_idmap = NULL;
4418 FILE *tmpfile = NULL;
4419 int *pack_fds = NULL;
4421 TAILQ_INIT(&refs);
4423 #ifndef PROFILE
4424 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4425 NULL)
4426 == -1)
4427 err(1, "pledge");
4428 #endif
4430 limit = get_default_log_limit();
4432 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4433 switch (ch) {
4434 case 'p':
4435 show_patch = 1;
4436 break;
4437 case 'P':
4438 show_changed_paths = 1;
4439 break;
4440 case 'c':
4441 start_commit = optarg;
4442 break;
4443 case 'C':
4444 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4445 &errstr);
4446 if (errstr != NULL)
4447 errx(1, "number of context lines is %s: %s",
4448 errstr, optarg);
4449 break;
4450 case 'l':
4451 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4452 if (errstr != NULL)
4453 errx(1, "number of commits is %s: %s",
4454 errstr, optarg);
4455 break;
4456 case 'b':
4457 log_branches = 1;
4458 break;
4459 case 'r':
4460 repo_path = realpath(optarg, NULL);
4461 if (repo_path == NULL)
4462 return got_error_from_errno2("realpath",
4463 optarg);
4464 got_path_strip_trailing_slashes(repo_path);
4465 break;
4466 case 'R':
4467 reverse_display_order = 1;
4468 break;
4469 case 's':
4470 one_line = 1;
4471 break;
4472 case 'S':
4473 search_pattern = optarg;
4474 break;
4475 case 'x':
4476 end_commit = optarg;
4477 break;
4478 default:
4479 usage_log();
4480 /* NOTREACHED */
4484 argc -= optind;
4485 argv += optind;
4487 if (diff_context == -1)
4488 diff_context = 3;
4489 else if (!show_patch)
4490 errx(1, "-C requires -p");
4492 if (one_line && (show_patch || show_changed_paths))
4493 errx(1, "cannot use -s with -p or -P");
4495 cwd = getcwd(NULL, 0);
4496 if (cwd == NULL) {
4497 error = got_error_from_errno("getcwd");
4498 goto done;
4501 error = got_repo_pack_fds_open(&pack_fds);
4502 if (error != NULL)
4503 goto done;
4505 if (repo_path == NULL) {
4506 error = got_worktree_open(&worktree, cwd);
4507 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4508 goto done;
4509 error = NULL;
4512 if (argc == 1) {
4513 if (worktree) {
4514 error = got_worktree_resolve_path(&path, worktree,
4515 argv[0]);
4516 if (error)
4517 goto done;
4518 } else {
4519 path = strdup(argv[0]);
4520 if (path == NULL) {
4521 error = got_error_from_errno("strdup");
4522 goto done;
4525 } else if (argc != 0)
4526 usage_log();
4528 if (repo_path == NULL) {
4529 repo_path = worktree ?
4530 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4532 if (repo_path == NULL) {
4533 error = got_error_from_errno("strdup");
4534 goto done;
4537 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4538 if (error != NULL)
4539 goto done;
4541 error = apply_unveil(got_repo_get_path(repo), 1,
4542 worktree ? got_worktree_get_root_path(worktree) : NULL);
4543 if (error)
4544 goto done;
4546 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4547 if (error)
4548 goto done;
4550 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4551 if (error)
4552 goto done;
4554 if (start_commit == NULL) {
4555 struct got_reference *head_ref;
4556 struct got_commit_object *commit = NULL;
4557 error = got_ref_open(&head_ref, repo,
4558 worktree ? got_worktree_get_head_ref_name(worktree)
4559 : GOT_REF_HEAD, 0);
4560 if (error != NULL)
4561 goto done;
4562 error = got_ref_resolve(&start_id, repo, head_ref);
4563 got_ref_close(head_ref);
4564 if (error != NULL)
4565 goto done;
4566 error = got_object_open_as_commit(&commit, repo,
4567 start_id);
4568 if (error != NULL)
4569 goto done;
4570 got_object_commit_close(commit);
4571 } else {
4572 error = got_repo_match_object_id(&start_id, NULL,
4573 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4574 if (error != NULL)
4575 goto done;
4577 if (end_commit != NULL) {
4578 error = got_repo_match_object_id(&end_id, NULL,
4579 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4580 if (error != NULL)
4581 goto done;
4584 if (worktree) {
4586 * If a path was specified on the command line it was resolved
4587 * to a path in the work tree above. Prepend the work tree's
4588 * path prefix to obtain the corresponding in-repository path.
4590 if (path) {
4591 const char *prefix;
4592 prefix = got_worktree_get_path_prefix(worktree);
4593 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4594 (path[0] != '\0') ? "/" : "", path) == -1) {
4595 error = got_error_from_errno("asprintf");
4596 goto done;
4599 } else
4600 error = got_repo_map_path(&in_repo_path, repo,
4601 path ? path : "");
4602 if (error != NULL)
4603 goto done;
4604 if (in_repo_path) {
4605 free(path);
4606 path = in_repo_path;
4609 if (worktree) {
4610 /* Release work tree lock. */
4611 got_worktree_close(worktree);
4612 worktree = NULL;
4615 if (search_pattern && show_patch) {
4616 tmpfile = got_opentemp();
4617 if (tmpfile == NULL) {
4618 error = got_error_from_errno("got_opentemp");
4619 goto done;
4623 error = print_commits(start_id, end_id, repo, path ? path : "",
4624 show_changed_paths, show_patch, search_pattern, diff_context,
4625 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4626 tmpfile);
4627 done:
4628 free(path);
4629 free(repo_path);
4630 free(cwd);
4631 if (worktree)
4632 got_worktree_close(worktree);
4633 if (repo) {
4634 const struct got_error *close_err = got_repo_close(repo);
4635 if (error == NULL)
4636 error = close_err;
4638 if (pack_fds) {
4639 const struct got_error *pack_err =
4640 got_repo_pack_fds_close(pack_fds);
4641 if (error == NULL)
4642 error = pack_err;
4644 if (refs_idmap)
4645 got_reflist_object_id_map_free(refs_idmap);
4646 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4647 error = got_error_from_errno("fclose");
4648 got_ref_list_free(&refs);
4649 return error;
4652 __dead static void
4653 usage_diff(void)
4655 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4656 "[-r repository-path] [-s] [-w] [-P] "
4657 "[object1 object2 | path ...]\n", getprogname());
4658 exit(1);
4661 struct print_diff_arg {
4662 struct got_repository *repo;
4663 struct got_worktree *worktree;
4664 int diff_context;
4665 const char *id_str;
4666 int header_shown;
4667 int diff_staged;
4668 enum got_diff_algorithm diff_algo;
4669 int ignore_whitespace;
4670 int force_text_diff;
4671 FILE *f1;
4672 FILE *f2;
4676 * Create a file which contains the target path of a symlink so we can feed
4677 * it as content to the diff engine.
4679 static const struct got_error *
4680 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4681 const char *abspath)
4683 const struct got_error *err = NULL;
4684 char target_path[PATH_MAX];
4685 ssize_t target_len, outlen;
4687 *fd = -1;
4689 if (dirfd != -1) {
4690 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4691 if (target_len == -1)
4692 return got_error_from_errno2("readlinkat", abspath);
4693 } else {
4694 target_len = readlink(abspath, target_path, PATH_MAX);
4695 if (target_len == -1)
4696 return got_error_from_errno2("readlink", abspath);
4699 *fd = got_opentempfd();
4700 if (*fd == -1)
4701 return got_error_from_errno("got_opentempfd");
4703 outlen = write(*fd, target_path, target_len);
4704 if (outlen == -1) {
4705 err = got_error_from_errno("got_opentempfd");
4706 goto done;
4709 if (lseek(*fd, 0, SEEK_SET) == -1) {
4710 err = got_error_from_errno2("lseek", abspath);
4711 goto done;
4713 done:
4714 if (err) {
4715 close(*fd);
4716 *fd = -1;
4718 return err;
4721 static const struct got_error *
4722 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4723 const char *path, struct got_object_id *blob_id,
4724 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4725 int dirfd, const char *de_name)
4727 struct print_diff_arg *a = arg;
4728 const struct got_error *err = NULL;
4729 struct got_blob_object *blob1 = NULL;
4730 int fd = -1, fd1 = -1, fd2 = -1;
4731 FILE *f2 = NULL;
4732 char *abspath = NULL, *label1 = NULL;
4733 struct stat sb;
4734 off_t size1 = 0;
4735 int f2_exists = 1;
4737 if (a->diff_staged) {
4738 if (staged_status != GOT_STATUS_MODIFY &&
4739 staged_status != GOT_STATUS_ADD &&
4740 staged_status != GOT_STATUS_DELETE)
4741 return NULL;
4742 } else {
4743 if (staged_status == GOT_STATUS_DELETE)
4744 return NULL;
4745 if (status == GOT_STATUS_NONEXISTENT)
4746 return got_error_set_errno(ENOENT, path);
4747 if (status != GOT_STATUS_MODIFY &&
4748 status != GOT_STATUS_ADD &&
4749 status != GOT_STATUS_DELETE &&
4750 status != GOT_STATUS_CONFLICT)
4751 return NULL;
4754 err = got_opentemp_truncate(a->f1);
4755 if (err)
4756 return got_error_from_errno("got_opentemp_truncate");
4757 err = got_opentemp_truncate(a->f2);
4758 if (err)
4759 return got_error_from_errno("got_opentemp_truncate");
4761 if (!a->header_shown) {
4762 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4763 got_worktree_get_root_path(a->worktree));
4764 printf("commit - %s\n", a->id_str);
4765 printf("path + %s%s\n",
4766 got_worktree_get_root_path(a->worktree),
4767 a->diff_staged ? " (staged changes)" : "");
4768 a->header_shown = 1;
4771 if (a->diff_staged) {
4772 const char *label1 = NULL, *label2 = NULL;
4773 switch (staged_status) {
4774 case GOT_STATUS_MODIFY:
4775 label1 = path;
4776 label2 = path;
4777 break;
4778 case GOT_STATUS_ADD:
4779 label2 = path;
4780 break;
4781 case GOT_STATUS_DELETE:
4782 label1 = path;
4783 break;
4784 default:
4785 return got_error(GOT_ERR_FILE_STATUS);
4787 fd1 = got_opentempfd();
4788 if (fd1 == -1) {
4789 err = got_error_from_errno("got_opentempfd");
4790 goto done;
4792 fd2 = got_opentempfd();
4793 if (fd2 == -1) {
4794 err = got_error_from_errno("got_opentempfd");
4795 goto done;
4797 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4798 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4799 a->diff_algo, a->diff_context, a->ignore_whitespace,
4800 a->force_text_diff, a->repo, stdout);
4801 goto done;
4804 fd1 = got_opentempfd();
4805 if (fd1 == -1) {
4806 err = got_error_from_errno("got_opentempfd");
4807 goto done;
4810 if (staged_status == GOT_STATUS_ADD ||
4811 staged_status == GOT_STATUS_MODIFY) {
4812 char *id_str;
4813 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4814 8192, fd1);
4815 if (err)
4816 goto done;
4817 err = got_object_id_str(&id_str, staged_blob_id);
4818 if (err)
4819 goto done;
4820 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4821 err = got_error_from_errno("asprintf");
4822 free(id_str);
4823 goto done;
4825 free(id_str);
4826 } else if (status != GOT_STATUS_ADD) {
4827 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4828 fd1);
4829 if (err)
4830 goto done;
4833 if (status != GOT_STATUS_DELETE) {
4834 if (asprintf(&abspath, "%s/%s",
4835 got_worktree_get_root_path(a->worktree), path) == -1) {
4836 err = got_error_from_errno("asprintf");
4837 goto done;
4840 if (dirfd != -1) {
4841 fd = openat(dirfd, de_name,
4842 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4843 if (fd == -1) {
4844 if (!got_err_open_nofollow_on_symlink()) {
4845 err = got_error_from_errno2("openat",
4846 abspath);
4847 goto done;
4849 err = get_symlink_target_file(&fd, dirfd,
4850 de_name, abspath);
4851 if (err)
4852 goto done;
4854 } else {
4855 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4856 if (fd == -1) {
4857 if (!got_err_open_nofollow_on_symlink()) {
4858 err = got_error_from_errno2("open",
4859 abspath);
4860 goto done;
4862 err = get_symlink_target_file(&fd, dirfd,
4863 de_name, abspath);
4864 if (err)
4865 goto done;
4868 if (fstat(fd, &sb) == -1) {
4869 err = got_error_from_errno2("fstat", abspath);
4870 goto done;
4872 f2 = fdopen(fd, "r");
4873 if (f2 == NULL) {
4874 err = got_error_from_errno2("fdopen", abspath);
4875 goto done;
4877 fd = -1;
4878 } else {
4879 sb.st_size = 0;
4880 f2_exists = 0;
4883 if (blob1) {
4884 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4885 a->f1, blob1);
4886 if (err)
4887 goto done;
4890 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4891 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4892 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4893 done:
4894 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4895 err = got_error_from_errno("close");
4896 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4897 err = got_error_from_errno("close");
4898 if (blob1)
4899 got_object_blob_close(blob1);
4900 if (fd != -1 && close(fd) == -1 && err == NULL)
4901 err = got_error_from_errno("close");
4902 if (f2 && fclose(f2) == EOF && err == NULL)
4903 err = got_error_from_errno("fclose");
4904 free(abspath);
4905 return err;
4908 static const struct got_error *
4909 cmd_diff(int argc, char *argv[])
4911 const struct got_error *error;
4912 struct got_repository *repo = NULL;
4913 struct got_worktree *worktree = NULL;
4914 char *cwd = NULL, *repo_path = NULL;
4915 const char *commit_args[2] = { NULL, NULL };
4916 int ncommit_args = 0;
4917 struct got_object_id *ids[2] = { NULL, NULL };
4918 char *labels[2] = { NULL, NULL };
4919 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4920 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4921 int force_text_diff = 0, force_path = 0, rflag = 0;
4922 const char *errstr;
4923 struct got_reflist_head refs;
4924 struct got_pathlist_head paths;
4925 struct got_pathlist_entry *pe;
4926 FILE *f1 = NULL, *f2 = NULL;
4927 int fd1 = -1, fd2 = -1;
4928 int *pack_fds = NULL;
4930 TAILQ_INIT(&refs);
4931 TAILQ_INIT(&paths);
4933 #ifndef PROFILE
4934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4935 NULL) == -1)
4936 err(1, "pledge");
4937 #endif
4939 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4940 switch (ch) {
4941 case 'a':
4942 force_text_diff = 1;
4943 break;
4944 case 'c':
4945 if (ncommit_args >= 2)
4946 errx(1, "too many -c options used");
4947 commit_args[ncommit_args++] = optarg;
4948 break;
4949 case 'C':
4950 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4951 &errstr);
4952 if (errstr != NULL)
4953 errx(1, "number of context lines is %s: %s",
4954 errstr, optarg);
4955 break;
4956 case 'r':
4957 repo_path = realpath(optarg, NULL);
4958 if (repo_path == NULL)
4959 return got_error_from_errno2("realpath",
4960 optarg);
4961 got_path_strip_trailing_slashes(repo_path);
4962 rflag = 1;
4963 break;
4964 case 's':
4965 diff_staged = 1;
4966 break;
4967 case 'w':
4968 ignore_whitespace = 1;
4969 break;
4970 case 'P':
4971 force_path = 1;
4972 break;
4973 default:
4974 usage_diff();
4975 /* NOTREACHED */
4979 argc -= optind;
4980 argv += optind;
4982 cwd = getcwd(NULL, 0);
4983 if (cwd == NULL) {
4984 error = got_error_from_errno("getcwd");
4985 goto done;
4988 error = got_repo_pack_fds_open(&pack_fds);
4989 if (error != NULL)
4990 goto done;
4992 if (repo_path == NULL) {
4993 error = got_worktree_open(&worktree, cwd);
4994 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4995 goto done;
4996 else
4997 error = NULL;
4998 if (worktree) {
4999 repo_path =
5000 strdup(got_worktree_get_repo_path(worktree));
5001 if (repo_path == NULL) {
5002 error = got_error_from_errno("strdup");
5003 goto done;
5005 } else {
5006 repo_path = strdup(cwd);
5007 if (repo_path == NULL) {
5008 error = got_error_from_errno("strdup");
5009 goto done;
5014 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5015 free(repo_path);
5016 if (error != NULL)
5017 goto done;
5019 if (rflag || worktree == NULL || ncommit_args > 0) {
5020 if (force_path) {
5021 error = got_error_msg(GOT_ERR_NOT_IMPL,
5022 "-P option can only be used when diffing "
5023 "a work tree");
5024 goto done;
5026 if (diff_staged) {
5027 error = got_error_msg(GOT_ERR_NOT_IMPL,
5028 "-s option can only be used when diffing "
5029 "a work tree");
5030 goto done;
5034 error = apply_unveil(got_repo_get_path(repo), 1,
5035 worktree ? got_worktree_get_root_path(worktree) : NULL);
5036 if (error)
5037 goto done;
5039 if ((!force_path && argc == 2) || ncommit_args > 0) {
5040 int obj_type = (ncommit_args > 0 ?
5041 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5042 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5043 NULL);
5044 if (error)
5045 goto done;
5046 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5047 const char *arg;
5048 if (ncommit_args > 0)
5049 arg = commit_args[i];
5050 else
5051 arg = argv[i];
5052 error = got_repo_match_object_id(&ids[i], &labels[i],
5053 arg, obj_type, &refs, repo);
5054 if (error) {
5055 if (error->code != GOT_ERR_NOT_REF &&
5056 error->code != GOT_ERR_NO_OBJ)
5057 goto done;
5058 if (ncommit_args > 0)
5059 goto done;
5060 error = NULL;
5061 break;
5066 f1 = got_opentemp();
5067 if (f1 == NULL) {
5068 error = got_error_from_errno("got_opentemp");
5069 goto done;
5072 f2 = got_opentemp();
5073 if (f2 == NULL) {
5074 error = got_error_from_errno("got_opentemp");
5075 goto done;
5078 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5079 struct print_diff_arg arg;
5080 char *id_str;
5082 if (worktree == NULL) {
5083 if (argc == 2 && ids[0] == NULL) {
5084 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5085 goto done;
5086 } else if (argc == 2 && ids[1] == NULL) {
5087 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5088 goto done;
5089 } else if (argc > 0) {
5090 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5091 "%s", "specified paths cannot be resolved");
5092 goto done;
5093 } else {
5094 error = got_error(GOT_ERR_NOT_WORKTREE);
5095 goto done;
5099 error = get_worktree_paths_from_argv(&paths, argc, argv,
5100 worktree);
5101 if (error)
5102 goto done;
5104 error = got_object_id_str(&id_str,
5105 got_worktree_get_base_commit_id(worktree));
5106 if (error)
5107 goto done;
5108 arg.repo = repo;
5109 arg.worktree = worktree;
5110 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5111 arg.diff_context = diff_context;
5112 arg.id_str = id_str;
5113 arg.header_shown = 0;
5114 arg.diff_staged = diff_staged;
5115 arg.ignore_whitespace = ignore_whitespace;
5116 arg.force_text_diff = force_text_diff;
5117 arg.f1 = f1;
5118 arg.f2 = f2;
5120 error = got_worktree_status(worktree, &paths, repo, 0,
5121 print_diff, &arg, check_cancelled, NULL);
5122 free(id_str);
5123 goto done;
5126 if (ncommit_args == 1) {
5127 struct got_commit_object *commit;
5128 error = got_object_open_as_commit(&commit, repo, ids[0]);
5129 if (error)
5130 goto done;
5132 labels[1] = labels[0];
5133 ids[1] = ids[0];
5134 if (got_object_commit_get_nparents(commit) > 0) {
5135 const struct got_object_id_queue *pids;
5136 struct got_object_qid *pid;
5137 pids = got_object_commit_get_parent_ids(commit);
5138 pid = STAILQ_FIRST(pids);
5139 ids[0] = got_object_id_dup(&pid->id);
5140 if (ids[0] == NULL) {
5141 error = got_error_from_errno(
5142 "got_object_id_dup");
5143 got_object_commit_close(commit);
5144 goto done;
5146 error = got_object_id_str(&labels[0], ids[0]);
5147 if (error) {
5148 got_object_commit_close(commit);
5149 goto done;
5151 } else {
5152 ids[0] = NULL;
5153 labels[0] = strdup("/dev/null");
5154 if (labels[0] == NULL) {
5155 error = got_error_from_errno("strdup");
5156 got_object_commit_close(commit);
5157 goto done;
5161 got_object_commit_close(commit);
5164 if (ncommit_args == 0 && argc > 2) {
5165 error = got_error_msg(GOT_ERR_BAD_PATH,
5166 "path arguments cannot be used when diffing two objects");
5167 goto done;
5170 if (ids[0]) {
5171 error = got_object_get_type(&type1, repo, ids[0]);
5172 if (error)
5173 goto done;
5176 error = got_object_get_type(&type2, repo, ids[1]);
5177 if (error)
5178 goto done;
5179 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5180 error = got_error(GOT_ERR_OBJ_TYPE);
5181 goto done;
5183 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5184 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5185 "path arguments cannot be used when diffing blobs");
5186 goto done;
5189 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5190 char *in_repo_path;
5191 struct got_pathlist_entry *new;
5192 if (worktree) {
5193 const char *prefix;
5194 char *p;
5195 error = got_worktree_resolve_path(&p, worktree,
5196 argv[i]);
5197 if (error)
5198 goto done;
5199 prefix = got_worktree_get_path_prefix(worktree);
5200 while (prefix[0] == '/')
5201 prefix++;
5202 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5203 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5204 p) == -1) {
5205 error = got_error_from_errno("asprintf");
5206 free(p);
5207 goto done;
5209 free(p);
5210 } else {
5211 char *mapped_path, *s;
5212 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5213 if (error)
5214 goto done;
5215 s = mapped_path;
5216 while (s[0] == '/')
5217 s++;
5218 in_repo_path = strdup(s);
5219 if (in_repo_path == NULL) {
5220 error = got_error_from_errno("asprintf");
5221 free(mapped_path);
5222 goto done;
5224 free(mapped_path);
5227 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5228 if (error || new == NULL /* duplicate */)
5229 free(in_repo_path);
5230 if (error)
5231 goto done;
5234 if (worktree) {
5235 /* Release work tree lock. */
5236 got_worktree_close(worktree);
5237 worktree = NULL;
5240 fd1 = got_opentempfd();
5241 if (fd1 == -1) {
5242 error = got_error_from_errno("got_opentempfd");
5243 goto done;
5246 fd2 = got_opentempfd();
5247 if (fd2 == -1) {
5248 error = got_error_from_errno("got_opentempfd");
5249 goto done;
5252 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5253 case GOT_OBJ_TYPE_BLOB:
5254 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5255 fd1, fd2, ids[0], ids[1], NULL, NULL,
5256 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5257 ignore_whitespace, force_text_diff, repo, stdout);
5258 break;
5259 case GOT_OBJ_TYPE_TREE:
5260 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5261 ids[0], ids[1], &paths, "", "",
5262 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5263 ignore_whitespace, force_text_diff, repo, stdout);
5264 break;
5265 case GOT_OBJ_TYPE_COMMIT:
5266 printf("diff %s %s\n", labels[0], labels[1]);
5267 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5268 fd1, fd2, ids[0], ids[1], &paths,
5269 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5270 ignore_whitespace, force_text_diff, repo, stdout);
5271 break;
5272 default:
5273 error = got_error(GOT_ERR_OBJ_TYPE);
5275 done:
5276 free(labels[0]);
5277 free(labels[1]);
5278 free(ids[0]);
5279 free(ids[1]);
5280 if (worktree)
5281 got_worktree_close(worktree);
5282 if (repo) {
5283 const struct got_error *close_err = got_repo_close(repo);
5284 if (error == NULL)
5285 error = close_err;
5287 if (pack_fds) {
5288 const struct got_error *pack_err =
5289 got_repo_pack_fds_close(pack_fds);
5290 if (error == NULL)
5291 error = pack_err;
5293 TAILQ_FOREACH(pe, &paths, entry)
5294 free((char *)pe->path);
5295 got_pathlist_free(&paths);
5296 got_ref_list_free(&refs);
5297 if (f1 && fclose(f1) == EOF && error == NULL)
5298 error = got_error_from_errno("fclose");
5299 if (f2 && fclose(f2) == EOF && error == NULL)
5300 error = got_error_from_errno("fclose");
5301 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5302 error = got_error_from_errno("close");
5303 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5304 error = got_error_from_errno("close");
5305 return error;
5308 __dead static void
5309 usage_blame(void)
5311 fprintf(stderr,
5312 "usage: %s blame [-c commit] [-r repository-path] path\n",
5313 getprogname());
5314 exit(1);
5317 struct blame_line {
5318 int annotated;
5319 char *id_str;
5320 char *committer;
5321 char datebuf[11]; /* YYYY-MM-DD + NUL */
5324 struct blame_cb_args {
5325 struct blame_line *lines;
5326 int nlines;
5327 int nlines_prec;
5328 int lineno_cur;
5329 off_t *line_offsets;
5330 FILE *f;
5331 struct got_repository *repo;
5334 static const struct got_error *
5335 blame_cb(void *arg, int nlines, int lineno,
5336 struct got_commit_object *commit, struct got_object_id *id)
5338 const struct got_error *err = NULL;
5339 struct blame_cb_args *a = arg;
5340 struct blame_line *bline;
5341 char *line = NULL;
5342 size_t linesize = 0;
5343 off_t offset;
5344 struct tm tm;
5345 time_t committer_time;
5347 if (nlines != a->nlines ||
5348 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5349 return got_error(GOT_ERR_RANGE);
5351 if (sigint_received)
5352 return got_error(GOT_ERR_ITER_COMPLETED);
5354 if (lineno == -1)
5355 return NULL; /* no change in this commit */
5357 /* Annotate this line. */
5358 bline = &a->lines[lineno - 1];
5359 if (bline->annotated)
5360 return NULL;
5361 err = got_object_id_str(&bline->id_str, id);
5362 if (err)
5363 return err;
5365 bline->committer = strdup(got_object_commit_get_committer(commit));
5366 if (bline->committer == NULL) {
5367 err = got_error_from_errno("strdup");
5368 goto done;
5371 committer_time = got_object_commit_get_committer_time(commit);
5372 if (gmtime_r(&committer_time, &tm) == NULL)
5373 return got_error_from_errno("gmtime_r");
5374 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5375 &tm) == 0) {
5376 err = got_error(GOT_ERR_NO_SPACE);
5377 goto done;
5379 bline->annotated = 1;
5381 /* Print lines annotated so far. */
5382 bline = &a->lines[a->lineno_cur - 1];
5383 if (!bline->annotated)
5384 goto done;
5386 offset = a->line_offsets[a->lineno_cur - 1];
5387 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5388 err = got_error_from_errno("fseeko");
5389 goto done;
5392 while (bline->annotated) {
5393 char *smallerthan, *at, *nl, *committer;
5394 size_t len;
5396 if (getline(&line, &linesize, a->f) == -1) {
5397 if (ferror(a->f))
5398 err = got_error_from_errno("getline");
5399 break;
5402 committer = bline->committer;
5403 smallerthan = strchr(committer, '<');
5404 if (smallerthan && smallerthan[1] != '\0')
5405 committer = smallerthan + 1;
5406 at = strchr(committer, '@');
5407 if (at)
5408 *at = '\0';
5409 len = strlen(committer);
5410 if (len >= 9)
5411 committer[8] = '\0';
5413 nl = strchr(line, '\n');
5414 if (nl)
5415 *nl = '\0';
5416 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5417 bline->id_str, bline->datebuf, committer, line);
5419 a->lineno_cur++;
5420 bline = &a->lines[a->lineno_cur - 1];
5422 done:
5423 free(line);
5424 return err;
5427 static const struct got_error *
5428 cmd_blame(int argc, char *argv[])
5430 const struct got_error *error;
5431 struct got_repository *repo = NULL;
5432 struct got_worktree *worktree = NULL;
5433 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5434 char *link_target = NULL;
5435 struct got_object_id *obj_id = NULL;
5436 struct got_object_id *commit_id = NULL;
5437 struct got_commit_object *commit = NULL;
5438 struct got_blob_object *blob = NULL;
5439 char *commit_id_str = NULL;
5440 struct blame_cb_args bca;
5441 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5442 off_t filesize;
5443 int *pack_fds = NULL;
5444 FILE *f1 = NULL, *f2 = NULL;
5446 fd1 = got_opentempfd();
5447 if (fd1 == -1)
5448 return got_error_from_errno("got_opentempfd");
5450 memset(&bca, 0, sizeof(bca));
5452 #ifndef PROFILE
5453 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5454 NULL) == -1)
5455 err(1, "pledge");
5456 #endif
5458 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5459 switch (ch) {
5460 case 'c':
5461 commit_id_str = optarg;
5462 break;
5463 case 'r':
5464 repo_path = realpath(optarg, NULL);
5465 if (repo_path == NULL)
5466 return got_error_from_errno2("realpath",
5467 optarg);
5468 got_path_strip_trailing_slashes(repo_path);
5469 break;
5470 default:
5471 usage_blame();
5472 /* NOTREACHED */
5476 argc -= optind;
5477 argv += optind;
5479 if (argc == 1)
5480 path = argv[0];
5481 else
5482 usage_blame();
5484 cwd = getcwd(NULL, 0);
5485 if (cwd == NULL) {
5486 error = got_error_from_errno("getcwd");
5487 goto done;
5490 error = got_repo_pack_fds_open(&pack_fds);
5491 if (error != NULL)
5492 goto done;
5494 if (repo_path == NULL) {
5495 error = got_worktree_open(&worktree, cwd);
5496 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5497 goto done;
5498 else
5499 error = NULL;
5500 if (worktree) {
5501 repo_path =
5502 strdup(got_worktree_get_repo_path(worktree));
5503 if (repo_path == NULL) {
5504 error = got_error_from_errno("strdup");
5505 if (error)
5506 goto done;
5508 } else {
5509 repo_path = strdup(cwd);
5510 if (repo_path == NULL) {
5511 error = got_error_from_errno("strdup");
5512 goto done;
5517 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5518 if (error != NULL)
5519 goto done;
5521 if (worktree) {
5522 const char *prefix = got_worktree_get_path_prefix(worktree);
5523 char *p;
5525 error = got_worktree_resolve_path(&p, worktree, path);
5526 if (error)
5527 goto done;
5528 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5529 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5530 p) == -1) {
5531 error = got_error_from_errno("asprintf");
5532 free(p);
5533 goto done;
5535 free(p);
5536 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5537 } else {
5538 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5539 if (error)
5540 goto done;
5541 error = got_repo_map_path(&in_repo_path, repo, path);
5543 if (error)
5544 goto done;
5546 if (commit_id_str == NULL) {
5547 struct got_reference *head_ref;
5548 error = got_ref_open(&head_ref, repo, worktree ?
5549 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5550 if (error != NULL)
5551 goto done;
5552 error = got_ref_resolve(&commit_id, repo, head_ref);
5553 got_ref_close(head_ref);
5554 if (error != NULL)
5555 goto done;
5556 } else {
5557 struct got_reflist_head refs;
5558 TAILQ_INIT(&refs);
5559 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5560 NULL);
5561 if (error)
5562 goto done;
5563 error = got_repo_match_object_id(&commit_id, NULL,
5564 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5565 got_ref_list_free(&refs);
5566 if (error)
5567 goto done;
5570 if (worktree) {
5571 /* Release work tree lock. */
5572 got_worktree_close(worktree);
5573 worktree = NULL;
5576 error = got_object_open_as_commit(&commit, repo, commit_id);
5577 if (error)
5578 goto done;
5580 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5581 commit, repo);
5582 if (error)
5583 goto done;
5585 error = got_object_id_by_path(&obj_id, repo, commit,
5586 link_target ? link_target : in_repo_path);
5587 if (error)
5588 goto done;
5590 error = got_object_get_type(&obj_type, repo, obj_id);
5591 if (error)
5592 goto done;
5594 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5595 error = got_error_path(link_target ? link_target : in_repo_path,
5596 GOT_ERR_OBJ_TYPE);
5597 goto done;
5600 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5601 if (error)
5602 goto done;
5603 bca.f = got_opentemp();
5604 if (bca.f == NULL) {
5605 error = got_error_from_errno("got_opentemp");
5606 goto done;
5608 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5609 &bca.line_offsets, bca.f, blob);
5610 if (error || bca.nlines == 0)
5611 goto done;
5613 /* Don't include \n at EOF in the blame line count. */
5614 if (bca.line_offsets[bca.nlines - 1] == filesize)
5615 bca.nlines--;
5617 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5618 if (bca.lines == NULL) {
5619 error = got_error_from_errno("calloc");
5620 goto done;
5622 bca.lineno_cur = 1;
5623 bca.nlines_prec = 0;
5624 i = bca.nlines;
5625 while (i > 0) {
5626 i /= 10;
5627 bca.nlines_prec++;
5629 bca.repo = repo;
5631 fd2 = got_opentempfd();
5632 if (fd2 == -1) {
5633 error = got_error_from_errno("got_opentempfd");
5634 goto done;
5636 fd3 = got_opentempfd();
5637 if (fd3 == -1) {
5638 error = got_error_from_errno("got_opentempfd");
5639 goto done;
5641 f1 = got_opentemp();
5642 if (f1 == NULL) {
5643 error = got_error_from_errno("got_opentemp");
5644 goto done;
5646 f2 = got_opentemp();
5647 if (f2 == NULL) {
5648 error = got_error_from_errno("got_opentemp");
5649 goto done;
5651 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5652 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5653 check_cancelled, NULL, fd2, fd3, f1, f2);
5654 done:
5655 free(in_repo_path);
5656 free(link_target);
5657 free(repo_path);
5658 free(cwd);
5659 free(commit_id);
5660 free(obj_id);
5661 if (commit)
5662 got_object_commit_close(commit);
5664 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5665 error = got_error_from_errno("close");
5666 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5667 error = got_error_from_errno("close");
5668 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5669 error = got_error_from_errno("close");
5670 if (f1 && fclose(f1) == EOF && error == NULL)
5671 error = got_error_from_errno("fclose");
5672 if (f2 && fclose(f2) == EOF && error == NULL)
5673 error = got_error_from_errno("fclose");
5675 if (blob)
5676 got_object_blob_close(blob);
5677 if (worktree)
5678 got_worktree_close(worktree);
5679 if (repo) {
5680 const struct got_error *close_err = got_repo_close(repo);
5681 if (error == NULL)
5682 error = close_err;
5684 if (pack_fds) {
5685 const struct got_error *pack_err =
5686 got_repo_pack_fds_close(pack_fds);
5687 if (error == NULL)
5688 error = pack_err;
5690 if (bca.lines) {
5691 for (i = 0; i < bca.nlines; i++) {
5692 struct blame_line *bline = &bca.lines[i];
5693 free(bline->id_str);
5694 free(bline->committer);
5696 free(bca.lines);
5698 free(bca.line_offsets);
5699 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5700 error = got_error_from_errno("fclose");
5701 return error;
5704 __dead static void
5705 usage_tree(void)
5707 fprintf(stderr,
5708 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5709 getprogname());
5710 exit(1);
5713 static const struct got_error *
5714 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5715 const char *root_path, struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 int is_root_path = (strcmp(path, root_path) == 0);
5719 const char *modestr = "";
5720 mode_t mode = got_tree_entry_get_mode(te);
5721 char *link_target = NULL;
5723 path += strlen(root_path);
5724 while (path[0] == '/')
5725 path++;
5727 if (got_object_tree_entry_is_submodule(te))
5728 modestr = "$";
5729 else if (S_ISLNK(mode)) {
5730 int i;
5732 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5733 if (err)
5734 return err;
5735 for (i = 0; i < strlen(link_target); i++) {
5736 if (!isprint((unsigned char)link_target[i]))
5737 link_target[i] = '?';
5740 modestr = "@";
5742 else if (S_ISDIR(mode))
5743 modestr = "/";
5744 else if (mode & S_IXUSR)
5745 modestr = "*";
5747 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5748 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5749 link_target ? " -> ": "", link_target ? link_target : "");
5751 free(link_target);
5752 return NULL;
5755 static const struct got_error *
5756 print_tree(const char *path, struct got_commit_object *commit,
5757 int show_ids, int recurse, const char *root_path,
5758 struct got_repository *repo)
5760 const struct got_error *err = NULL;
5761 struct got_object_id *tree_id = NULL;
5762 struct got_tree_object *tree = NULL;
5763 int nentries, i;
5765 err = got_object_id_by_path(&tree_id, repo, commit, path);
5766 if (err)
5767 goto done;
5769 err = got_object_open_as_tree(&tree, repo, tree_id);
5770 if (err)
5771 goto done;
5772 nentries = got_object_tree_get_nentries(tree);
5773 for (i = 0; i < nentries; i++) {
5774 struct got_tree_entry *te;
5775 char *id = NULL;
5777 if (sigint_received || sigpipe_received)
5778 break;
5780 te = got_object_tree_get_entry(tree, i);
5781 if (show_ids) {
5782 char *id_str;
5783 err = got_object_id_str(&id_str,
5784 got_tree_entry_get_id(te));
5785 if (err)
5786 goto done;
5787 if (asprintf(&id, "%s ", id_str) == -1) {
5788 err = got_error_from_errno("asprintf");
5789 free(id_str);
5790 goto done;
5792 free(id_str);
5794 err = print_entry(te, id, path, root_path, repo);
5795 free(id);
5796 if (err)
5797 goto done;
5799 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5800 char *child_path;
5801 if (asprintf(&child_path, "%s%s%s", path,
5802 path[0] == '/' && path[1] == '\0' ? "" : "/",
5803 got_tree_entry_get_name(te)) == -1) {
5804 err = got_error_from_errno("asprintf");
5805 goto done;
5807 err = print_tree(child_path, commit, show_ids, 1,
5808 root_path, repo);
5809 free(child_path);
5810 if (err)
5811 goto done;
5814 done:
5815 if (tree)
5816 got_object_tree_close(tree);
5817 free(tree_id);
5818 return err;
5821 static const struct got_error *
5822 cmd_tree(int argc, char *argv[])
5824 const struct got_error *error;
5825 struct got_repository *repo = NULL;
5826 struct got_worktree *worktree = NULL;
5827 const char *path, *refname = NULL;
5828 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5829 struct got_object_id *commit_id = NULL;
5830 struct got_commit_object *commit = NULL;
5831 char *commit_id_str = NULL;
5832 int show_ids = 0, recurse = 0;
5833 int ch;
5834 int *pack_fds = NULL;
5836 #ifndef PROFILE
5837 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5838 NULL) == -1)
5839 err(1, "pledge");
5840 #endif
5842 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5843 switch (ch) {
5844 case 'c':
5845 commit_id_str = optarg;
5846 break;
5847 case 'r':
5848 repo_path = realpath(optarg, NULL);
5849 if (repo_path == NULL)
5850 return got_error_from_errno2("realpath",
5851 optarg);
5852 got_path_strip_trailing_slashes(repo_path);
5853 break;
5854 case 'i':
5855 show_ids = 1;
5856 break;
5857 case 'R':
5858 recurse = 1;
5859 break;
5860 default:
5861 usage_tree();
5862 /* NOTREACHED */
5866 argc -= optind;
5867 argv += optind;
5869 if (argc == 1)
5870 path = argv[0];
5871 else if (argc > 1)
5872 usage_tree();
5873 else
5874 path = NULL;
5876 cwd = getcwd(NULL, 0);
5877 if (cwd == NULL) {
5878 error = got_error_from_errno("getcwd");
5879 goto done;
5882 error = got_repo_pack_fds_open(&pack_fds);
5883 if (error != NULL)
5884 goto done;
5886 if (repo_path == NULL) {
5887 error = got_worktree_open(&worktree, cwd);
5888 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5889 goto done;
5890 else
5891 error = NULL;
5892 if (worktree) {
5893 repo_path =
5894 strdup(got_worktree_get_repo_path(worktree));
5895 if (repo_path == NULL)
5896 error = got_error_from_errno("strdup");
5897 if (error)
5898 goto done;
5899 } else {
5900 repo_path = strdup(cwd);
5901 if (repo_path == NULL) {
5902 error = got_error_from_errno("strdup");
5903 goto done;
5908 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5909 if (error != NULL)
5910 goto done;
5912 if (worktree) {
5913 const char *prefix = got_worktree_get_path_prefix(worktree);
5914 char *p;
5916 if (path == NULL)
5917 path = "";
5918 error = got_worktree_resolve_path(&p, worktree, path);
5919 if (error)
5920 goto done;
5921 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5922 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5923 p) == -1) {
5924 error = got_error_from_errno("asprintf");
5925 free(p);
5926 goto done;
5928 free(p);
5929 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5930 if (error)
5931 goto done;
5932 } else {
5933 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5934 if (error)
5935 goto done;
5936 if (path == NULL)
5937 path = "/";
5938 error = got_repo_map_path(&in_repo_path, repo, path);
5939 if (error != NULL)
5940 goto done;
5943 if (commit_id_str == NULL) {
5944 struct got_reference *head_ref;
5945 if (worktree)
5946 refname = got_worktree_get_head_ref_name(worktree);
5947 else
5948 refname = GOT_REF_HEAD;
5949 error = got_ref_open(&head_ref, repo, refname, 0);
5950 if (error != NULL)
5951 goto done;
5952 error = got_ref_resolve(&commit_id, repo, head_ref);
5953 got_ref_close(head_ref);
5954 if (error != NULL)
5955 goto done;
5956 } else {
5957 struct got_reflist_head refs;
5958 TAILQ_INIT(&refs);
5959 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5960 NULL);
5961 if (error)
5962 goto done;
5963 error = got_repo_match_object_id(&commit_id, NULL,
5964 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5965 got_ref_list_free(&refs);
5966 if (error)
5967 goto done;
5970 if (worktree) {
5971 /* Release work tree lock. */
5972 got_worktree_close(worktree);
5973 worktree = NULL;
5976 error = got_object_open_as_commit(&commit, repo, commit_id);
5977 if (error)
5978 goto done;
5980 error = print_tree(in_repo_path, commit, show_ids, recurse,
5981 in_repo_path, repo);
5982 done:
5983 free(in_repo_path);
5984 free(repo_path);
5985 free(cwd);
5986 free(commit_id);
5987 if (commit)
5988 got_object_commit_close(commit);
5989 if (worktree)
5990 got_worktree_close(worktree);
5991 if (repo) {
5992 const struct got_error *close_err = got_repo_close(repo);
5993 if (error == NULL)
5994 error = close_err;
5996 if (pack_fds) {
5997 const struct got_error *pack_err =
5998 got_repo_pack_fds_close(pack_fds);
5999 if (error == NULL)
6000 error = pack_err;
6002 return error;
6005 __dead static void
6006 usage_status(void)
6008 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
6009 "[-S status-codes] [path ...]\n", getprogname());
6010 exit(1);
6013 struct got_status_arg {
6014 char *status_codes;
6015 int suppress;
6018 static const struct got_error *
6019 print_status(void *arg, unsigned char status, unsigned char staged_status,
6020 const char *path, struct got_object_id *blob_id,
6021 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6022 int dirfd, const char *de_name)
6024 struct got_status_arg *st = arg;
6026 if (status == staged_status && (status == GOT_STATUS_DELETE))
6027 status = GOT_STATUS_NO_CHANGE;
6028 if (st != NULL && st->status_codes) {
6029 size_t ncodes = strlen(st->status_codes);
6030 int i, j = 0;
6032 for (i = 0; i < ncodes ; i++) {
6033 if (st->suppress) {
6034 if (status == st->status_codes[i] ||
6035 staged_status == st->status_codes[i]) {
6036 j++;
6037 continue;
6039 } else {
6040 if (status == st->status_codes[i] ||
6041 staged_status == st->status_codes[i])
6042 break;
6046 if (st->suppress && j == 0)
6047 goto print;
6049 if (i == ncodes)
6050 return NULL;
6052 print:
6053 printf("%c%c %s\n", status, staged_status, path);
6054 return NULL;
6057 static const struct got_error *
6058 cmd_status(int argc, char *argv[])
6060 const struct got_error *error = NULL;
6061 struct got_repository *repo = NULL;
6062 struct got_worktree *worktree = NULL;
6063 struct got_status_arg st;
6064 char *cwd = NULL;
6065 struct got_pathlist_head paths;
6066 struct got_pathlist_entry *pe;
6067 int ch, i, no_ignores = 0;
6068 int *pack_fds = NULL;
6070 TAILQ_INIT(&paths);
6072 memset(&st, 0, sizeof(st));
6073 st.status_codes = NULL;
6074 st.suppress = 0;
6076 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6077 switch (ch) {
6078 case 'I':
6079 no_ignores = 1;
6080 break;
6081 case 'S':
6082 if (st.status_codes != NULL && st.suppress == 0)
6083 option_conflict('S', 's');
6084 st.suppress = 1;
6085 /* fallthrough */
6086 case 's':
6087 for (i = 0; i < strlen(optarg); i++) {
6088 switch (optarg[i]) {
6089 case GOT_STATUS_MODIFY:
6090 case GOT_STATUS_ADD:
6091 case GOT_STATUS_DELETE:
6092 case GOT_STATUS_CONFLICT:
6093 case GOT_STATUS_MISSING:
6094 case GOT_STATUS_OBSTRUCTED:
6095 case GOT_STATUS_UNVERSIONED:
6096 case GOT_STATUS_MODE_CHANGE:
6097 case GOT_STATUS_NONEXISTENT:
6098 break;
6099 default:
6100 errx(1, "invalid status code '%c'",
6101 optarg[i]);
6104 if (ch == 's' && st.suppress)
6105 option_conflict('s', 'S');
6106 st.status_codes = optarg;
6107 break;
6108 default:
6109 usage_status();
6110 /* NOTREACHED */
6114 argc -= optind;
6115 argv += optind;
6117 #ifndef PROFILE
6118 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6119 NULL) == -1)
6120 err(1, "pledge");
6121 #endif
6122 cwd = getcwd(NULL, 0);
6123 if (cwd == NULL) {
6124 error = got_error_from_errno("getcwd");
6125 goto done;
6128 error = got_repo_pack_fds_open(&pack_fds);
6129 if (error != NULL)
6130 goto done;
6132 error = got_worktree_open(&worktree, cwd);
6133 if (error) {
6134 if (error->code == GOT_ERR_NOT_WORKTREE)
6135 error = wrap_not_worktree_error(error, "status", cwd);
6136 goto done;
6139 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6140 NULL, pack_fds);
6141 if (error != NULL)
6142 goto done;
6144 error = apply_unveil(got_repo_get_path(repo), 1,
6145 got_worktree_get_root_path(worktree));
6146 if (error)
6147 goto done;
6149 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6150 if (error)
6151 goto done;
6153 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6154 print_status, &st, check_cancelled, NULL);
6155 done:
6156 if (pack_fds) {
6157 const struct got_error *pack_err =
6158 got_repo_pack_fds_close(pack_fds);
6159 if (error == NULL)
6160 error = pack_err;
6163 TAILQ_FOREACH(pe, &paths, entry)
6164 free((char *)pe->path);
6165 got_pathlist_free(&paths);
6166 free(cwd);
6167 return error;
6170 __dead static void
6171 usage_ref(void)
6173 fprintf(stderr,
6174 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6175 "[-s reference] [-d] [name]\n",
6176 getprogname());
6177 exit(1);
6180 static const struct got_error *
6181 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6183 static const struct got_error *err = NULL;
6184 struct got_reflist_head refs;
6185 struct got_reflist_entry *re;
6187 TAILQ_INIT(&refs);
6188 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6189 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6190 repo);
6191 if (err)
6192 return err;
6194 TAILQ_FOREACH(re, &refs, entry) {
6195 char *refstr;
6196 refstr = got_ref_to_str(re->ref);
6197 if (refstr == NULL) {
6198 err = got_error_from_errno("got_ref_to_str");
6199 break;
6201 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6202 free(refstr);
6205 got_ref_list_free(&refs);
6206 return err;
6209 static const struct got_error *
6210 delete_ref_by_name(struct got_repository *repo, const char *refname)
6212 const struct got_error *err;
6213 struct got_reference *ref;
6215 err = got_ref_open(&ref, repo, refname, 0);
6216 if (err)
6217 return err;
6219 err = delete_ref(repo, ref);
6220 got_ref_close(ref);
6221 return err;
6224 static const struct got_error *
6225 add_ref(struct got_repository *repo, const char *refname, const char *target)
6227 const struct got_error *err = NULL;
6228 struct got_object_id *id = NULL;
6229 struct got_reference *ref = NULL;
6230 struct got_reflist_head refs;
6233 * Don't let the user create a reference name with a leading '-'.
6234 * While technically a valid reference name, this case is usually
6235 * an unintended typo.
6237 if (refname[0] == '-')
6238 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6240 TAILQ_INIT(&refs);
6241 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6242 if (err)
6243 goto done;
6244 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6245 &refs, repo);
6246 got_ref_list_free(&refs);
6247 if (err)
6248 goto done;
6250 err = got_ref_alloc(&ref, refname, id);
6251 if (err)
6252 goto done;
6254 err = got_ref_write(ref, repo);
6255 done:
6256 if (ref)
6257 got_ref_close(ref);
6258 free(id);
6259 return err;
6262 static const struct got_error *
6263 add_symref(struct got_repository *repo, const char *refname, const char *target)
6265 const struct got_error *err = NULL;
6266 struct got_reference *ref = NULL;
6267 struct got_reference *target_ref = NULL;
6270 * Don't let the user create a reference name with a leading '-'.
6271 * While technically a valid reference name, this case is usually
6272 * an unintended typo.
6274 if (refname[0] == '-')
6275 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6277 err = got_ref_open(&target_ref, repo, target, 0);
6278 if (err)
6279 return err;
6281 err = got_ref_alloc_symref(&ref, refname, target_ref);
6282 if (err)
6283 goto done;
6285 err = got_ref_write(ref, repo);
6286 done:
6287 if (target_ref)
6288 got_ref_close(target_ref);
6289 if (ref)
6290 got_ref_close(ref);
6291 return err;
6294 static const struct got_error *
6295 cmd_ref(int argc, char *argv[])
6297 const struct got_error *error = NULL;
6298 struct got_repository *repo = NULL;
6299 struct got_worktree *worktree = NULL;
6300 char *cwd = NULL, *repo_path = NULL;
6301 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6302 const char *obj_arg = NULL, *symref_target= NULL;
6303 char *refname = NULL;
6304 int *pack_fds = NULL;
6306 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6307 switch (ch) {
6308 case 'c':
6309 obj_arg = optarg;
6310 break;
6311 case 'd':
6312 do_delete = 1;
6313 break;
6314 case 'r':
6315 repo_path = realpath(optarg, NULL);
6316 if (repo_path == NULL)
6317 return got_error_from_errno2("realpath",
6318 optarg);
6319 got_path_strip_trailing_slashes(repo_path);
6320 break;
6321 case 'l':
6322 do_list = 1;
6323 break;
6324 case 's':
6325 symref_target = optarg;
6326 break;
6327 case 't':
6328 sort_by_time = 1;
6329 break;
6330 default:
6331 usage_ref();
6332 /* NOTREACHED */
6336 if (obj_arg && do_list)
6337 option_conflict('c', 'l');
6338 if (obj_arg && do_delete)
6339 option_conflict('c', 'd');
6340 if (obj_arg && symref_target)
6341 option_conflict('c', 's');
6342 if (symref_target && do_delete)
6343 option_conflict('s', 'd');
6344 if (symref_target && do_list)
6345 option_conflict('s', 'l');
6346 if (do_delete && do_list)
6347 option_conflict('d', 'l');
6348 if (sort_by_time && !do_list)
6349 errx(1, "-t option requires -l option");
6351 argc -= optind;
6352 argv += optind;
6354 if (do_list) {
6355 if (argc != 0 && argc != 1)
6356 usage_ref();
6357 if (argc == 1) {
6358 refname = strdup(argv[0]);
6359 if (refname == NULL) {
6360 error = got_error_from_errno("strdup");
6361 goto done;
6364 } else {
6365 if (argc != 1)
6366 usage_ref();
6367 refname = strdup(argv[0]);
6368 if (refname == NULL) {
6369 error = got_error_from_errno("strdup");
6370 goto done;
6374 if (refname)
6375 got_path_strip_trailing_slashes(refname);
6377 #ifndef PROFILE
6378 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6379 "sendfd unveil", NULL) == -1)
6380 err(1, "pledge");
6381 #endif
6382 cwd = getcwd(NULL, 0);
6383 if (cwd == NULL) {
6384 error = got_error_from_errno("getcwd");
6385 goto done;
6388 error = got_repo_pack_fds_open(&pack_fds);
6389 if (error != NULL)
6390 goto done;
6392 if (repo_path == NULL) {
6393 error = got_worktree_open(&worktree, cwd);
6394 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6395 goto done;
6396 else
6397 error = NULL;
6398 if (worktree) {
6399 repo_path =
6400 strdup(got_worktree_get_repo_path(worktree));
6401 if (repo_path == NULL)
6402 error = got_error_from_errno("strdup");
6403 if (error)
6404 goto done;
6405 } else {
6406 repo_path = strdup(cwd);
6407 if (repo_path == NULL) {
6408 error = got_error_from_errno("strdup");
6409 goto done;
6414 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6415 if (error != NULL)
6416 goto done;
6418 #ifndef PROFILE
6419 if (do_list) {
6420 /* Remove "cpath" promise. */
6421 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6422 NULL) == -1)
6423 err(1, "pledge");
6425 #endif
6427 error = apply_unveil(got_repo_get_path(repo), do_list,
6428 worktree ? got_worktree_get_root_path(worktree) : NULL);
6429 if (error)
6430 goto done;
6432 if (do_list)
6433 error = list_refs(repo, refname, sort_by_time);
6434 else if (do_delete)
6435 error = delete_ref_by_name(repo, refname);
6436 else if (symref_target)
6437 error = add_symref(repo, refname, symref_target);
6438 else {
6439 if (obj_arg == NULL)
6440 usage_ref();
6441 error = add_ref(repo, refname, obj_arg);
6443 done:
6444 free(refname);
6445 if (repo) {
6446 const struct got_error *close_err = got_repo_close(repo);
6447 if (error == NULL)
6448 error = close_err;
6450 if (worktree)
6451 got_worktree_close(worktree);
6452 if (pack_fds) {
6453 const struct got_error *pack_err =
6454 got_repo_pack_fds_close(pack_fds);
6455 if (error == NULL)
6456 error = pack_err;
6458 free(cwd);
6459 free(repo_path);
6460 return error;
6463 __dead static void
6464 usage_branch(void)
6466 fprintf(stderr,
6467 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6468 "[-n] [name]\n", getprogname());
6469 exit(1);
6472 static const struct got_error *
6473 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6474 struct got_reference *ref)
6476 const struct got_error *err = NULL;
6477 const char *refname, *marker = " ";
6478 char *refstr;
6480 refname = got_ref_get_name(ref);
6481 if (worktree && strcmp(refname,
6482 got_worktree_get_head_ref_name(worktree)) == 0) {
6483 struct got_object_id *id = NULL;
6485 err = got_ref_resolve(&id, repo, ref);
6486 if (err)
6487 return err;
6488 if (got_object_id_cmp(id,
6489 got_worktree_get_base_commit_id(worktree)) == 0)
6490 marker = "* ";
6491 else
6492 marker = "~ ";
6493 free(id);
6496 if (strncmp(refname, "refs/heads/", 11) == 0)
6497 refname += 11;
6498 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6499 refname += 18;
6500 if (strncmp(refname, "refs/remotes/", 13) == 0)
6501 refname += 13;
6503 refstr = got_ref_to_str(ref);
6504 if (refstr == NULL)
6505 return got_error_from_errno("got_ref_to_str");
6507 printf("%s%s: %s\n", marker, refname, refstr);
6508 free(refstr);
6509 return NULL;
6512 static const struct got_error *
6513 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6515 const char *refname;
6517 if (worktree == NULL)
6518 return got_error(GOT_ERR_NOT_WORKTREE);
6520 refname = got_worktree_get_head_ref_name(worktree);
6522 if (strncmp(refname, "refs/heads/", 11) == 0)
6523 refname += 11;
6524 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6525 refname += 18;
6527 printf("%s\n", refname);
6529 return NULL;
6532 static const struct got_error *
6533 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6534 int sort_by_time)
6536 static const struct got_error *err = NULL;
6537 struct got_reflist_head refs;
6538 struct got_reflist_entry *re;
6539 struct got_reference *temp_ref = NULL;
6540 int rebase_in_progress, histedit_in_progress;
6542 TAILQ_INIT(&refs);
6544 if (worktree) {
6545 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6546 worktree);
6547 if (err)
6548 return err;
6550 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6551 worktree);
6552 if (err)
6553 return err;
6555 if (rebase_in_progress || histedit_in_progress) {
6556 err = got_ref_open(&temp_ref, repo,
6557 got_worktree_get_head_ref_name(worktree), 0);
6558 if (err)
6559 return err;
6560 list_branch(repo, worktree, temp_ref);
6561 got_ref_close(temp_ref);
6565 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6566 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6567 repo);
6568 if (err)
6569 return err;
6571 TAILQ_FOREACH(re, &refs, entry)
6572 list_branch(repo, worktree, re->ref);
6574 got_ref_list_free(&refs);
6576 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6577 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6578 repo);
6579 if (err)
6580 return err;
6582 TAILQ_FOREACH(re, &refs, entry)
6583 list_branch(repo, worktree, re->ref);
6585 got_ref_list_free(&refs);
6587 return NULL;
6590 static const struct got_error *
6591 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6592 const char *branch_name)
6594 const struct got_error *err = NULL;
6595 struct got_reference *ref = NULL;
6596 char *refname, *remote_refname = NULL;
6598 if (strncmp(branch_name, "refs/", 5) == 0)
6599 branch_name += 5;
6600 if (strncmp(branch_name, "heads/", 6) == 0)
6601 branch_name += 6;
6602 else if (strncmp(branch_name, "remotes/", 8) == 0)
6603 branch_name += 8;
6605 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6606 return got_error_from_errno("asprintf");
6608 if (asprintf(&remote_refname, "refs/remotes/%s",
6609 branch_name) == -1) {
6610 err = got_error_from_errno("asprintf");
6611 goto done;
6614 err = got_ref_open(&ref, repo, refname, 0);
6615 if (err) {
6616 const struct got_error *err2;
6617 if (err->code != GOT_ERR_NOT_REF)
6618 goto done;
6620 * Keep 'err' intact such that if neither branch exists
6621 * we report "refs/heads" rather than "refs/remotes" in
6622 * our error message.
6624 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6625 if (err2)
6626 goto done;
6627 err = NULL;
6630 if (worktree &&
6631 strcmp(got_worktree_get_head_ref_name(worktree),
6632 got_ref_get_name(ref)) == 0) {
6633 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6634 "will not delete this work tree's current branch");
6635 goto done;
6638 err = delete_ref(repo, ref);
6639 done:
6640 if (ref)
6641 got_ref_close(ref);
6642 free(refname);
6643 free(remote_refname);
6644 return err;
6647 static const struct got_error *
6648 add_branch(struct got_repository *repo, const char *branch_name,
6649 struct got_object_id *base_commit_id)
6651 const struct got_error *err = NULL;
6652 struct got_reference *ref = NULL;
6653 char *base_refname = NULL, *refname = NULL;
6656 * Don't let the user create a branch name with a leading '-'.
6657 * While technically a valid reference name, this case is usually
6658 * an unintended typo.
6660 if (branch_name[0] == '-')
6661 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6663 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6664 branch_name += 11;
6666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6667 err = got_error_from_errno("asprintf");
6668 goto done;
6671 err = got_ref_open(&ref, repo, refname, 0);
6672 if (err == NULL) {
6673 err = got_error(GOT_ERR_BRANCH_EXISTS);
6674 goto done;
6675 } else if (err->code != GOT_ERR_NOT_REF)
6676 goto done;
6678 err = got_ref_alloc(&ref, refname, base_commit_id);
6679 if (err)
6680 goto done;
6682 err = got_ref_write(ref, repo);
6683 done:
6684 if (ref)
6685 got_ref_close(ref);
6686 free(base_refname);
6687 free(refname);
6688 return err;
6691 static const struct got_error *
6692 cmd_branch(int argc, char *argv[])
6694 const struct got_error *error = NULL;
6695 struct got_repository *repo = NULL;
6696 struct got_worktree *worktree = NULL;
6697 char *cwd = NULL, *repo_path = NULL;
6698 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6699 const char *delref = NULL, *commit_id_arg = NULL;
6700 struct got_reference *ref = NULL;
6701 struct got_pathlist_head paths;
6702 struct got_pathlist_entry *pe;
6703 struct got_object_id *commit_id = NULL;
6704 char *commit_id_str = NULL;
6705 int *pack_fds = NULL;
6707 TAILQ_INIT(&paths);
6709 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6710 switch (ch) {
6711 case 'c':
6712 commit_id_arg = optarg;
6713 break;
6714 case 'd':
6715 delref = optarg;
6716 break;
6717 case 'r':
6718 repo_path = realpath(optarg, NULL);
6719 if (repo_path == NULL)
6720 return got_error_from_errno2("realpath",
6721 optarg);
6722 got_path_strip_trailing_slashes(repo_path);
6723 break;
6724 case 'l':
6725 do_list = 1;
6726 break;
6727 case 'n':
6728 do_update = 0;
6729 break;
6730 case 't':
6731 sort_by_time = 1;
6732 break;
6733 default:
6734 usage_branch();
6735 /* NOTREACHED */
6739 if (do_list && delref)
6740 option_conflict('l', 'd');
6741 if (sort_by_time && !do_list)
6742 errx(1, "-t option requires -l option");
6744 argc -= optind;
6745 argv += optind;
6747 if (!do_list && !delref && argc == 0)
6748 do_show = 1;
6750 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6751 errx(1, "-c option can only be used when creating a branch");
6753 if (do_list || delref) {
6754 if (argc > 0)
6755 usage_branch();
6756 } else if (!do_show && argc != 1)
6757 usage_branch();
6759 #ifndef PROFILE
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6761 "sendfd unveil", NULL) == -1)
6762 err(1, "pledge");
6763 #endif
6764 cwd = getcwd(NULL, 0);
6765 if (cwd == NULL) {
6766 error = got_error_from_errno("getcwd");
6767 goto done;
6770 error = got_repo_pack_fds_open(&pack_fds);
6771 if (error != NULL)
6772 goto done;
6774 if (repo_path == NULL) {
6775 error = got_worktree_open(&worktree, cwd);
6776 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6777 goto done;
6778 else
6779 error = NULL;
6780 if (worktree) {
6781 repo_path =
6782 strdup(got_worktree_get_repo_path(worktree));
6783 if (repo_path == NULL)
6784 error = got_error_from_errno("strdup");
6785 if (error)
6786 goto done;
6787 } else {
6788 repo_path = strdup(cwd);
6789 if (repo_path == NULL) {
6790 error = got_error_from_errno("strdup");
6791 goto done;
6796 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6797 if (error != NULL)
6798 goto done;
6800 #ifndef PROFILE
6801 if (do_list || do_show) {
6802 /* Remove "cpath" promise. */
6803 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6804 NULL) == -1)
6805 err(1, "pledge");
6807 #endif
6809 error = apply_unveil(got_repo_get_path(repo), do_list,
6810 worktree ? got_worktree_get_root_path(worktree) : NULL);
6811 if (error)
6812 goto done;
6814 if (do_show)
6815 error = show_current_branch(repo, worktree);
6816 else if (do_list)
6817 error = list_branches(repo, worktree, sort_by_time);
6818 else if (delref)
6819 error = delete_branch(repo, worktree, delref);
6820 else {
6821 struct got_reflist_head refs;
6822 TAILQ_INIT(&refs);
6823 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6824 NULL);
6825 if (error)
6826 goto done;
6827 if (commit_id_arg == NULL)
6828 commit_id_arg = worktree ?
6829 got_worktree_get_head_ref_name(worktree) :
6830 GOT_REF_HEAD;
6831 error = got_repo_match_object_id(&commit_id, NULL,
6832 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6833 got_ref_list_free(&refs);
6834 if (error)
6835 goto done;
6836 error = add_branch(repo, argv[0], commit_id);
6837 if (error)
6838 goto done;
6839 if (worktree && do_update) {
6840 struct got_update_progress_arg upa;
6841 char *branch_refname = NULL;
6843 error = got_object_id_str(&commit_id_str, commit_id);
6844 if (error)
6845 goto done;
6846 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6847 worktree);
6848 if (error)
6849 goto done;
6850 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6851 == -1) {
6852 error = got_error_from_errno("asprintf");
6853 goto done;
6855 error = got_ref_open(&ref, repo, branch_refname, 0);
6856 free(branch_refname);
6857 if (error)
6858 goto done;
6859 error = switch_head_ref(ref, commit_id, worktree,
6860 repo);
6861 if (error)
6862 goto done;
6863 error = got_worktree_set_base_commit_id(worktree, repo,
6864 commit_id);
6865 if (error)
6866 goto done;
6867 memset(&upa, 0, sizeof(upa));
6868 error = got_worktree_checkout_files(worktree, &paths,
6869 repo, update_progress, &upa, check_cancelled,
6870 NULL);
6871 if (error)
6872 goto done;
6873 if (upa.did_something) {
6874 printf("Updated to %s: %s\n",
6875 got_worktree_get_head_ref_name(worktree),
6876 commit_id_str);
6878 print_update_progress_stats(&upa);
6881 done:
6882 if (ref)
6883 got_ref_close(ref);
6884 if (repo) {
6885 const struct got_error *close_err = got_repo_close(repo);
6886 if (error == NULL)
6887 error = close_err;
6889 if (worktree)
6890 got_worktree_close(worktree);
6891 if (pack_fds) {
6892 const struct got_error *pack_err =
6893 got_repo_pack_fds_close(pack_fds);
6894 if (error == NULL)
6895 error = pack_err;
6897 free(cwd);
6898 free(repo_path);
6899 free(commit_id);
6900 free(commit_id_str);
6901 TAILQ_FOREACH(pe, &paths, entry)
6902 free((char *)pe->path);
6903 got_pathlist_free(&paths);
6904 return error;
6908 __dead static void
6909 usage_tag(void)
6911 fprintf(stderr,
6912 "usage: %s tag [-c commit] [-r repository] [-l] "
6913 "[-m message] [-s signer-id] [-v] [-V] name\n",
6914 getprogname());
6915 exit(1);
6918 #if 0
6919 static const struct got_error *
6920 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6922 const struct got_error *err = NULL;
6923 struct got_reflist_entry *re, *se, *new;
6924 struct got_object_id *re_id, *se_id;
6925 struct got_tag_object *re_tag, *se_tag;
6926 time_t re_time, se_time;
6928 STAILQ_FOREACH(re, tags, entry) {
6929 se = STAILQ_FIRST(sorted);
6930 if (se == NULL) {
6931 err = got_reflist_entry_dup(&new, re);
6932 if (err)
6933 return err;
6934 STAILQ_INSERT_HEAD(sorted, new, entry);
6935 continue;
6936 } else {
6937 err = got_ref_resolve(&re_id, repo, re->ref);
6938 if (err)
6939 break;
6940 err = got_object_open_as_tag(&re_tag, repo, re_id);
6941 free(re_id);
6942 if (err)
6943 break;
6944 re_time = got_object_tag_get_tagger_time(re_tag);
6945 got_object_tag_close(re_tag);
6948 while (se) {
6949 err = got_ref_resolve(&se_id, repo, re->ref);
6950 if (err)
6951 break;
6952 err = got_object_open_as_tag(&se_tag, repo, se_id);
6953 free(se_id);
6954 if (err)
6955 break;
6956 se_time = got_object_tag_get_tagger_time(se_tag);
6957 got_object_tag_close(se_tag);
6959 if (se_time > re_time) {
6960 err = got_reflist_entry_dup(&new, re);
6961 if (err)
6962 return err;
6963 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6964 break;
6966 se = STAILQ_NEXT(se, entry);
6967 continue;
6970 done:
6971 return err;
6973 #endif
6975 static const struct got_error *
6976 get_tag_refname(char **refname, const char *tag_name)
6978 const struct got_error *err;
6980 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6981 *refname = strdup(tag_name);
6982 if (*refname == NULL)
6983 return got_error_from_errno("strdup");
6984 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6985 err = got_error_from_errno("asprintf");
6986 *refname = NULL;
6987 return err;
6990 return NULL;
6993 static const struct got_error *
6994 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6995 const char *allowed_signers, const char *revoked_signers, int verbosity)
6997 static const struct got_error *err = NULL;
6998 struct got_reflist_head refs;
6999 struct got_reflist_entry *re;
7000 char *wanted_refname = NULL;
7001 int bad_sigs = 0;
7003 TAILQ_INIT(&refs);
7005 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7006 if (err)
7007 return err;
7009 if (tag_name) {
7010 struct got_reference *ref;
7011 err = get_tag_refname(&wanted_refname, tag_name);
7012 if (err)
7013 goto done;
7014 /* Wanted tag reference should exist. */
7015 err = got_ref_open(&ref, repo, wanted_refname, 0);
7016 if (err)
7017 goto done;
7018 got_ref_close(ref);
7021 TAILQ_FOREACH(re, &refs, entry) {
7022 const char *refname;
7023 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7024 char datebuf[26];
7025 const char *tagger, *ssh_sig = NULL;
7026 char *sig_msg = NULL;
7027 time_t tagger_time;
7028 struct got_object_id *id;
7029 struct got_tag_object *tag;
7030 struct got_commit_object *commit = NULL;
7032 refname = got_ref_get_name(re->ref);
7033 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7034 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7035 continue;
7036 refname += 10;
7037 refstr = got_ref_to_str(re->ref);
7038 if (refstr == NULL) {
7039 err = got_error_from_errno("got_ref_to_str");
7040 break;
7043 err = got_ref_resolve(&id, repo, re->ref);
7044 if (err)
7045 break;
7046 err = got_object_open_as_tag(&tag, repo, id);
7047 if (err) {
7048 if (err->code != GOT_ERR_OBJ_TYPE) {
7049 free(id);
7050 break;
7052 /* "lightweight" tag */
7053 err = got_object_open_as_commit(&commit, repo, id);
7054 if (err) {
7055 free(id);
7056 break;
7058 tagger = got_object_commit_get_committer(commit);
7059 tagger_time =
7060 got_object_commit_get_committer_time(commit);
7061 err = got_object_id_str(&id_str, id);
7062 free(id);
7063 if (err)
7064 break;
7065 } else {
7066 free(id);
7067 tagger = got_object_tag_get_tagger(tag);
7068 tagger_time = got_object_tag_get_tagger_time(tag);
7069 err = got_object_id_str(&id_str,
7070 got_object_tag_get_object_id(tag));
7071 if (err)
7072 break;
7075 if (verify_tags) {
7076 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7077 got_object_tag_get_message(tag));
7078 if (ssh_sig && allowed_signers == NULL) {
7079 err = got_error_msg(
7080 GOT_ERR_VERIFY_TAG_SIGNATURE,
7081 "SSH signature verification requires "
7082 "setting allowed_signers in "
7083 "got.conf(5)");
7084 break;
7088 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7089 free(refstr);
7090 printf("from: %s\n", tagger);
7091 datestr = get_datestr(&tagger_time, datebuf);
7092 if (datestr)
7093 printf("date: %s UTC\n", datestr);
7094 if (commit)
7095 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7096 else {
7097 switch (got_object_tag_get_object_type(tag)) {
7098 case GOT_OBJ_TYPE_BLOB:
7099 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7100 id_str);
7101 break;
7102 case GOT_OBJ_TYPE_TREE:
7103 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7104 id_str);
7105 break;
7106 case GOT_OBJ_TYPE_COMMIT:
7107 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7108 id_str);
7109 break;
7110 case GOT_OBJ_TYPE_TAG:
7111 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7112 id_str);
7113 break;
7114 default:
7115 break;
7118 free(id_str);
7120 if (ssh_sig) {
7121 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7122 allowed_signers, revoked_signers, verbosity);
7123 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7124 bad_sigs = 1;
7125 else if (err)
7126 break;
7127 printf("signature: %s", sig_msg);
7128 free(sig_msg);
7129 sig_msg = NULL;
7132 if (commit) {
7133 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7134 if (err)
7135 break;
7136 got_object_commit_close(commit);
7137 } else {
7138 tagmsg0 = strdup(got_object_tag_get_message(tag));
7139 got_object_tag_close(tag);
7140 if (tagmsg0 == NULL) {
7141 err = got_error_from_errno("strdup");
7142 break;
7146 tagmsg = tagmsg0;
7147 do {
7148 line = strsep(&tagmsg, "\n");
7149 if (line)
7150 printf(" %s\n", line);
7151 } while (line);
7152 free(tagmsg0);
7154 done:
7155 got_ref_list_free(&refs);
7156 free(wanted_refname);
7158 if (err == NULL && bad_sigs)
7159 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7160 return err;
7163 static const struct got_error *
7164 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7165 const char *tag_name, const char *repo_path)
7167 const struct got_error *err = NULL;
7168 char *template = NULL, *initial_content = NULL;
7169 char *editor = NULL;
7170 int initial_content_len;
7171 int fd = -1;
7173 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7174 err = got_error_from_errno("asprintf");
7175 goto done;
7178 initial_content_len = asprintf(&initial_content,
7179 "\n# tagging commit %s as %s\n",
7180 commit_id_str, tag_name);
7181 if (initial_content_len == -1) {
7182 err = got_error_from_errno("asprintf");
7183 goto done;
7186 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7187 if (err)
7188 goto done;
7190 if (write(fd, initial_content, initial_content_len) == -1) {
7191 err = got_error_from_errno2("write", *tagmsg_path);
7192 goto done;
7195 err = get_editor(&editor);
7196 if (err)
7197 goto done;
7198 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7199 initial_content_len, 1);
7200 done:
7201 free(initial_content);
7202 free(template);
7203 free(editor);
7205 if (fd != -1 && close(fd) == -1 && err == NULL)
7206 err = got_error_from_errno2("close", *tagmsg_path);
7208 if (err) {
7209 free(*tagmsg);
7210 *tagmsg = NULL;
7212 return err;
7215 static const struct got_error *
7216 add_tag(struct got_repository *repo, const char *tagger,
7217 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7218 const char *signer_id, int verbosity)
7220 const struct got_error *err = NULL;
7221 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7222 char *label = NULL, *commit_id_str = NULL;
7223 struct got_reference *ref = NULL;
7224 char *refname = NULL, *tagmsg = NULL;
7225 char *tagmsg_path = NULL, *tag_id_str = NULL;
7226 int preserve_tagmsg = 0;
7227 struct got_reflist_head refs;
7229 TAILQ_INIT(&refs);
7232 * Don't let the user create a tag name with a leading '-'.
7233 * While technically a valid reference name, this case is usually
7234 * an unintended typo.
7236 if (tag_name[0] == '-')
7237 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7239 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7240 if (err)
7241 goto done;
7243 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7244 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7245 if (err)
7246 goto done;
7248 err = got_object_id_str(&commit_id_str, commit_id);
7249 if (err)
7250 goto done;
7252 err = get_tag_refname(&refname, tag_name);
7253 if (err)
7254 goto done;
7255 if (strncmp("refs/tags/", tag_name, 10) == 0)
7256 tag_name += 10;
7258 err = got_ref_open(&ref, repo, refname, 0);
7259 if (err == NULL) {
7260 err = got_error(GOT_ERR_TAG_EXISTS);
7261 goto done;
7262 } else if (err->code != GOT_ERR_NOT_REF)
7263 goto done;
7265 if (tagmsg_arg == NULL) {
7266 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7267 tag_name, got_repo_get_path(repo));
7268 if (err) {
7269 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7270 tagmsg_path != NULL)
7271 preserve_tagmsg = 1;
7272 goto done;
7274 /* Editor is done; we can now apply unveil(2) */
7275 err = got_sigs_apply_unveil();
7276 if (err)
7277 goto done;
7278 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7279 if (err)
7280 goto done;
7283 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7284 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7285 verbosity);
7286 if (err) {
7287 if (tagmsg_path)
7288 preserve_tagmsg = 1;
7289 goto done;
7292 err = got_ref_alloc(&ref, refname, tag_id);
7293 if (err) {
7294 if (tagmsg_path)
7295 preserve_tagmsg = 1;
7296 goto done;
7299 err = got_ref_write(ref, repo);
7300 if (err) {
7301 if (tagmsg_path)
7302 preserve_tagmsg = 1;
7303 goto done;
7306 err = got_object_id_str(&tag_id_str, tag_id);
7307 if (err) {
7308 if (tagmsg_path)
7309 preserve_tagmsg = 1;
7310 goto done;
7312 printf("Created tag %s\n", tag_id_str);
7313 done:
7314 if (preserve_tagmsg) {
7315 fprintf(stderr, "%s: tag message preserved in %s\n",
7316 getprogname(), tagmsg_path);
7317 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7318 err = got_error_from_errno2("unlink", tagmsg_path);
7319 free(tag_id_str);
7320 if (ref)
7321 got_ref_close(ref);
7322 free(commit_id);
7323 free(commit_id_str);
7324 free(refname);
7325 free(tagmsg);
7326 free(tagmsg_path);
7327 got_ref_list_free(&refs);
7328 return err;
7331 static const struct got_error *
7332 cmd_tag(int argc, char *argv[])
7334 const struct got_error *error = NULL;
7335 struct got_repository *repo = NULL;
7336 struct got_worktree *worktree = NULL;
7337 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7338 char *gitconfig_path = NULL, *tagger = NULL;
7339 char *allowed_signers = NULL, *revoked_signers = NULL;
7340 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7341 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7342 const char *signer_id = NULL;
7343 int *pack_fds = NULL;
7345 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7346 switch (ch) {
7347 case 'c':
7348 commit_id_arg = optarg;
7349 break;
7350 case 'm':
7351 tagmsg = optarg;
7352 break;
7353 case 'r':
7354 repo_path = realpath(optarg, NULL);
7355 if (repo_path == NULL)
7356 return got_error_from_errno2("realpath",
7357 optarg);
7358 got_path_strip_trailing_slashes(repo_path);
7359 break;
7360 case 'l':
7361 do_list = 1;
7362 break;
7363 case 's':
7364 signer_id = optarg;
7365 break;
7366 case 'V':
7367 verify_tags = 1;
7368 break;
7369 case 'v':
7370 if (verbosity < 0)
7371 verbosity = 0;
7372 else if (verbosity < 3)
7373 verbosity++;
7374 break;
7375 default:
7376 usage_tag();
7377 /* NOTREACHED */
7381 argc -= optind;
7382 argv += optind;
7384 if (do_list || verify_tags) {
7385 if (commit_id_arg != NULL)
7386 errx(1,
7387 "-c option can only be used when creating a tag");
7388 if (tagmsg) {
7389 if (do_list)
7390 option_conflict('l', 'm');
7391 else
7392 option_conflict('V', 'm');
7394 if (signer_id) {
7395 if (do_list)
7396 option_conflict('l', 's');
7397 else
7398 option_conflict('V', 's');
7400 if (argc > 1)
7401 usage_tag();
7402 } else if (argc != 1)
7403 usage_tag();
7405 if (argc == 1)
7406 tag_name = argv[0];
7408 #ifndef PROFILE
7409 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7410 "sendfd unveil", NULL) == -1)
7411 err(1, "pledge");
7412 #endif
7413 cwd = getcwd(NULL, 0);
7414 if (cwd == NULL) {
7415 error = got_error_from_errno("getcwd");
7416 goto done;
7419 error = got_repo_pack_fds_open(&pack_fds);
7420 if (error != NULL)
7421 goto done;
7423 if (repo_path == NULL) {
7424 error = got_worktree_open(&worktree, cwd);
7425 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7426 goto done;
7427 else
7428 error = NULL;
7429 if (worktree) {
7430 repo_path =
7431 strdup(got_worktree_get_repo_path(worktree));
7432 if (repo_path == NULL)
7433 error = got_error_from_errno("strdup");
7434 if (error)
7435 goto done;
7436 } else {
7437 repo_path = strdup(cwd);
7438 if (repo_path == NULL) {
7439 error = got_error_from_errno("strdup");
7440 goto done;
7445 if (do_list || verify_tags) {
7446 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7447 if (error != NULL)
7448 goto done;
7449 error = get_allowed_signers(&allowed_signers, repo, worktree);
7450 if (error)
7451 goto done;
7452 error = get_revoked_signers(&revoked_signers, repo, worktree);
7453 if (error)
7454 goto done;
7455 if (worktree) {
7456 /* Release work tree lock. */
7457 got_worktree_close(worktree);
7458 worktree = NULL;
7462 * Remove "cpath" promise unless needed for signature tmpfile
7463 * creation.
7465 if (verify_tags)
7466 got_sigs_apply_unveil();
7467 else {
7468 #ifndef PROFILE
7469 if (pledge("stdio rpath wpath flock proc exec sendfd "
7470 "unveil", NULL) == -1)
7471 err(1, "pledge");
7472 #endif
7474 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7475 if (error)
7476 goto done;
7477 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7478 revoked_signers, verbosity);
7479 } else {
7480 error = get_gitconfig_path(&gitconfig_path);
7481 if (error)
7482 goto done;
7483 error = got_repo_open(&repo, repo_path, gitconfig_path,
7484 pack_fds);
7485 if (error != NULL)
7486 goto done;
7488 error = get_author(&tagger, repo, worktree);
7489 if (error)
7490 goto done;
7491 if (worktree) {
7492 /* Release work tree lock. */
7493 got_worktree_close(worktree);
7494 worktree = NULL;
7497 if (tagmsg) {
7498 if (signer_id) {
7499 error = got_sigs_apply_unveil();
7500 if (error)
7501 goto done;
7503 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7504 if (error)
7505 goto done;
7508 if (commit_id_arg == NULL) {
7509 struct got_reference *head_ref;
7510 struct got_object_id *commit_id;
7511 error = got_ref_open(&head_ref, repo,
7512 worktree ? got_worktree_get_head_ref_name(worktree)
7513 : GOT_REF_HEAD, 0);
7514 if (error)
7515 goto done;
7516 error = got_ref_resolve(&commit_id, repo, head_ref);
7517 got_ref_close(head_ref);
7518 if (error)
7519 goto done;
7520 error = got_object_id_str(&commit_id_str, commit_id);
7521 free(commit_id);
7522 if (error)
7523 goto done;
7526 error = add_tag(repo, tagger, tag_name,
7527 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7528 signer_id, verbosity);
7530 done:
7531 if (repo) {
7532 const struct got_error *close_err = got_repo_close(repo);
7533 if (error == NULL)
7534 error = close_err;
7536 if (worktree)
7537 got_worktree_close(worktree);
7538 if (pack_fds) {
7539 const struct got_error *pack_err =
7540 got_repo_pack_fds_close(pack_fds);
7541 if (error == NULL)
7542 error = pack_err;
7544 free(cwd);
7545 free(repo_path);
7546 free(gitconfig_path);
7547 free(commit_id_str);
7548 free(tagger);
7549 free(allowed_signers);
7550 free(revoked_signers);
7551 return error;
7554 __dead static void
7555 usage_add(void)
7557 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7558 getprogname());
7559 exit(1);
7562 static const struct got_error *
7563 add_progress(void *arg, unsigned char status, const char *path)
7565 while (path[0] == '/')
7566 path++;
7567 printf("%c %s\n", status, path);
7568 return NULL;
7571 static const struct got_error *
7572 cmd_add(int argc, char *argv[])
7574 const struct got_error *error = NULL;
7575 struct got_repository *repo = NULL;
7576 struct got_worktree *worktree = NULL;
7577 char *cwd = NULL;
7578 struct got_pathlist_head paths;
7579 struct got_pathlist_entry *pe;
7580 int ch, can_recurse = 0, no_ignores = 0;
7581 int *pack_fds = NULL;
7583 TAILQ_INIT(&paths);
7585 while ((ch = getopt(argc, argv, "IR")) != -1) {
7586 switch (ch) {
7587 case 'I':
7588 no_ignores = 1;
7589 break;
7590 case 'R':
7591 can_recurse = 1;
7592 break;
7593 default:
7594 usage_add();
7595 /* NOTREACHED */
7599 argc -= optind;
7600 argv += optind;
7602 #ifndef PROFILE
7603 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7604 NULL) == -1)
7605 err(1, "pledge");
7606 #endif
7607 if (argc < 1)
7608 usage_add();
7610 cwd = getcwd(NULL, 0);
7611 if (cwd == NULL) {
7612 error = got_error_from_errno("getcwd");
7613 goto done;
7616 error = got_repo_pack_fds_open(&pack_fds);
7617 if (error != NULL)
7618 goto done;
7620 error = got_worktree_open(&worktree, cwd);
7621 if (error) {
7622 if (error->code == GOT_ERR_NOT_WORKTREE)
7623 error = wrap_not_worktree_error(error, "add", cwd);
7624 goto done;
7627 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7628 NULL, pack_fds);
7629 if (error != NULL)
7630 goto done;
7632 error = apply_unveil(got_repo_get_path(repo), 1,
7633 got_worktree_get_root_path(worktree));
7634 if (error)
7635 goto done;
7637 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7638 if (error)
7639 goto done;
7641 if (!can_recurse) {
7642 char *ondisk_path;
7643 struct stat sb;
7644 TAILQ_FOREACH(pe, &paths, entry) {
7645 if (asprintf(&ondisk_path, "%s/%s",
7646 got_worktree_get_root_path(worktree),
7647 pe->path) == -1) {
7648 error = got_error_from_errno("asprintf");
7649 goto done;
7651 if (lstat(ondisk_path, &sb) == -1) {
7652 if (errno == ENOENT) {
7653 free(ondisk_path);
7654 continue;
7656 error = got_error_from_errno2("lstat",
7657 ondisk_path);
7658 free(ondisk_path);
7659 goto done;
7661 free(ondisk_path);
7662 if (S_ISDIR(sb.st_mode)) {
7663 error = got_error_msg(GOT_ERR_BAD_PATH,
7664 "adding directories requires -R option");
7665 goto done;
7670 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7671 NULL, repo, no_ignores);
7672 done:
7673 if (repo) {
7674 const struct got_error *close_err = got_repo_close(repo);
7675 if (error == NULL)
7676 error = close_err;
7678 if (worktree)
7679 got_worktree_close(worktree);
7680 if (pack_fds) {
7681 const struct got_error *pack_err =
7682 got_repo_pack_fds_close(pack_fds);
7683 if (error == NULL)
7684 error = pack_err;
7686 TAILQ_FOREACH(pe, &paths, entry)
7687 free((char *)pe->path);
7688 got_pathlist_free(&paths);
7689 free(cwd);
7690 return error;
7693 __dead static void
7694 usage_remove(void)
7696 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7697 "path ...\n", getprogname());
7698 exit(1);
7701 static const struct got_error *
7702 print_remove_status(void *arg, unsigned char status,
7703 unsigned char staged_status, const char *path)
7705 while (path[0] == '/')
7706 path++;
7707 if (status == GOT_STATUS_NONEXISTENT)
7708 return NULL;
7709 if (status == staged_status && (status == GOT_STATUS_DELETE))
7710 status = GOT_STATUS_NO_CHANGE;
7711 printf("%c%c %s\n", status, staged_status, path);
7712 return NULL;
7715 static const struct got_error *
7716 cmd_remove(int argc, char *argv[])
7718 const struct got_error *error = NULL;
7719 struct got_worktree *worktree = NULL;
7720 struct got_repository *repo = NULL;
7721 const char *status_codes = NULL;
7722 char *cwd = NULL;
7723 struct got_pathlist_head paths;
7724 struct got_pathlist_entry *pe;
7725 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7726 int ignore_missing_paths = 0;
7727 int *pack_fds = NULL;
7729 TAILQ_INIT(&paths);
7731 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7732 switch (ch) {
7733 case 'f':
7734 delete_local_mods = 1;
7735 ignore_missing_paths = 1;
7736 break;
7737 case 'k':
7738 keep_on_disk = 1;
7739 break;
7740 case 'R':
7741 can_recurse = 1;
7742 break;
7743 case 's':
7744 for (i = 0; i < strlen(optarg); i++) {
7745 switch (optarg[i]) {
7746 case GOT_STATUS_MODIFY:
7747 delete_local_mods = 1;
7748 break;
7749 case GOT_STATUS_MISSING:
7750 ignore_missing_paths = 1;
7751 break;
7752 default:
7753 errx(1, "invalid status code '%c'",
7754 optarg[i]);
7757 status_codes = optarg;
7758 break;
7759 default:
7760 usage_remove();
7761 /* NOTREACHED */
7765 argc -= optind;
7766 argv += optind;
7768 #ifndef PROFILE
7769 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7770 NULL) == -1)
7771 err(1, "pledge");
7772 #endif
7773 if (argc < 1)
7774 usage_remove();
7776 cwd = getcwd(NULL, 0);
7777 if (cwd == NULL) {
7778 error = got_error_from_errno("getcwd");
7779 goto done;
7782 error = got_repo_pack_fds_open(&pack_fds);
7783 if (error != NULL)
7784 goto done;
7786 error = got_worktree_open(&worktree, cwd);
7787 if (error) {
7788 if (error->code == GOT_ERR_NOT_WORKTREE)
7789 error = wrap_not_worktree_error(error, "remove", cwd);
7790 goto done;
7793 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7794 NULL, pack_fds);
7795 if (error)
7796 goto done;
7798 error = apply_unveil(got_repo_get_path(repo), 1,
7799 got_worktree_get_root_path(worktree));
7800 if (error)
7801 goto done;
7803 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7804 if (error)
7805 goto done;
7807 if (!can_recurse) {
7808 char *ondisk_path;
7809 struct stat sb;
7810 TAILQ_FOREACH(pe, &paths, entry) {
7811 if (asprintf(&ondisk_path, "%s/%s",
7812 got_worktree_get_root_path(worktree),
7813 pe->path) == -1) {
7814 error = got_error_from_errno("asprintf");
7815 goto done;
7817 if (lstat(ondisk_path, &sb) == -1) {
7818 if (errno == ENOENT) {
7819 free(ondisk_path);
7820 continue;
7822 error = got_error_from_errno2("lstat",
7823 ondisk_path);
7824 free(ondisk_path);
7825 goto done;
7827 free(ondisk_path);
7828 if (S_ISDIR(sb.st_mode)) {
7829 error = got_error_msg(GOT_ERR_BAD_PATH,
7830 "removing directories requires -R option");
7831 goto done;
7836 error = got_worktree_schedule_delete(worktree, &paths,
7837 delete_local_mods, status_codes, print_remove_status, NULL,
7838 repo, keep_on_disk, ignore_missing_paths);
7839 done:
7840 if (repo) {
7841 const struct got_error *close_err = got_repo_close(repo);
7842 if (error == NULL)
7843 error = close_err;
7845 if (worktree)
7846 got_worktree_close(worktree);
7847 if (pack_fds) {
7848 const struct got_error *pack_err =
7849 got_repo_pack_fds_close(pack_fds);
7850 if (error == NULL)
7851 error = pack_err;
7853 TAILQ_FOREACH(pe, &paths, entry)
7854 free((char *)pe->path);
7855 got_pathlist_free(&paths);
7856 free(cwd);
7857 return error;
7860 __dead static void
7861 usage_patch(void)
7863 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7864 "[-R] [patchfile]\n", getprogname());
7865 exit(1);
7868 static const struct got_error *
7869 patch_from_stdin(int *patchfd)
7871 const struct got_error *err = NULL;
7872 ssize_t r;
7873 char *path, buf[BUFSIZ];
7874 sig_t sighup, sigint, sigquit;
7876 err = got_opentemp_named_fd(&path, patchfd,
7877 GOT_TMPDIR_STR "/got-patch");
7878 if (err)
7879 return err;
7880 unlink(path);
7881 free(path);
7883 sighup = signal(SIGHUP, SIG_DFL);
7884 sigint = signal(SIGINT, SIG_DFL);
7885 sigquit = signal(SIGQUIT, SIG_DFL);
7887 for (;;) {
7888 r = read(0, buf, sizeof(buf));
7889 if (r == -1) {
7890 err = got_error_from_errno("read");
7891 break;
7893 if (r == 0)
7894 break;
7895 if (write(*patchfd, buf, r) == -1) {
7896 err = got_error_from_errno("write");
7897 break;
7901 signal(SIGHUP, sighup);
7902 signal(SIGINT, sigint);
7903 signal(SIGQUIT, sigquit);
7905 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7906 err = got_error_from_errno("lseek");
7908 if (err != NULL) {
7909 close(*patchfd);
7910 *patchfd = -1;
7913 return err;
7916 static const struct got_error *
7917 patch_progress(void *arg, const char *old, const char *new,
7918 unsigned char status, const struct got_error *error, int old_from,
7919 int old_lines, int new_from, int new_lines, int offset,
7920 int ws_mangled, const struct got_error *hunk_err)
7922 const char *path = new == NULL ? old : new;
7924 while (*path == '/')
7925 path++;
7927 if (status != 0)
7928 printf("%c %s\n", status, path);
7930 if (error != NULL)
7931 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7933 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7934 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7935 old_lines, new_from, new_lines);
7936 if (hunk_err != NULL)
7937 printf("%s\n", hunk_err->msg);
7938 else if (offset != 0)
7939 printf("applied with offset %d\n", offset);
7940 else
7941 printf("hunk contains mangled whitespace\n");
7944 return NULL;
7947 static const struct got_error *
7948 cmd_patch(int argc, char *argv[])
7950 const struct got_error *error = NULL, *close_error = NULL;
7951 struct got_worktree *worktree = NULL;
7952 struct got_repository *repo = NULL;
7953 const char *errstr;
7954 char *cwd = NULL;
7955 int ch, nop = 0, strip = -1, reverse = 0;
7956 int patchfd;
7957 int *pack_fds = NULL;
7959 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7960 switch (ch) {
7961 case 'n':
7962 nop = 1;
7963 break;
7964 case 'p':
7965 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7966 if (errstr != NULL)
7967 errx(1, "pathname strip count is %s: %s",
7968 errstr, optarg);
7969 break;
7970 case 'R':
7971 reverse = 1;
7972 break;
7973 default:
7974 usage_patch();
7975 /* NOTREACHED */
7979 argc -= optind;
7980 argv += optind;
7982 if (argc == 0) {
7983 error = patch_from_stdin(&patchfd);
7984 if (error)
7985 return error;
7986 } else if (argc == 1) {
7987 patchfd = open(argv[0], O_RDONLY);
7988 if (patchfd == -1) {
7989 error = got_error_from_errno2("open", argv[0]);
7990 return error;
7992 } else
7993 usage_patch();
7995 if ((cwd = getcwd(NULL, 0)) == NULL) {
7996 error = got_error_from_errno("getcwd");
7997 goto done;
8000 error = got_repo_pack_fds_open(&pack_fds);
8001 if (error != NULL)
8002 goto done;
8004 error = got_worktree_open(&worktree, cwd);
8005 if (error != NULL)
8006 goto done;
8008 const char *repo_path = got_worktree_get_repo_path(worktree);
8009 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8010 if (error != NULL)
8011 goto done;
8013 error = apply_unveil(got_repo_get_path(repo), 0,
8014 got_worktree_get_root_path(worktree));
8015 if (error != NULL)
8016 goto done;
8018 #ifndef PROFILE
8019 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8020 NULL) == -1)
8021 err(1, "pledge");
8022 #endif
8024 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8025 &patch_progress, NULL, check_cancelled, NULL);
8027 done:
8028 if (repo) {
8029 close_error = got_repo_close(repo);
8030 if (error == NULL)
8031 error = close_error;
8033 if (worktree != NULL) {
8034 close_error = got_worktree_close(worktree);
8035 if (error == NULL)
8036 error = close_error;
8038 if (pack_fds) {
8039 const struct got_error *pack_err =
8040 got_repo_pack_fds_close(pack_fds);
8041 if (error == NULL)
8042 error = pack_err;
8044 free(cwd);
8045 return error;
8048 __dead static void
8049 usage_revert(void)
8051 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8052 "path ...\n", getprogname());
8053 exit(1);
8056 static const struct got_error *
8057 revert_progress(void *arg, unsigned char status, const char *path)
8059 if (status == GOT_STATUS_UNVERSIONED)
8060 return NULL;
8062 while (path[0] == '/')
8063 path++;
8064 printf("%c %s\n", status, path);
8065 return NULL;
8068 struct choose_patch_arg {
8069 FILE *patch_script_file;
8070 const char *action;
8073 static const struct got_error *
8074 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8075 int nchanges, const char *action)
8077 const struct got_error *err;
8078 char *line = NULL;
8079 size_t linesize = 0;
8080 ssize_t linelen;
8082 switch (status) {
8083 case GOT_STATUS_ADD:
8084 printf("A %s\n%s this addition? [y/n] ", path, action);
8085 break;
8086 case GOT_STATUS_DELETE:
8087 printf("D %s\n%s this deletion? [y/n] ", path, action);
8088 break;
8089 case GOT_STATUS_MODIFY:
8090 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8091 return got_error_from_errno("fseek");
8092 printf(GOT_COMMIT_SEP_STR);
8093 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8094 printf("%s", line);
8095 if (linelen == -1 && ferror(patch_file)) {
8096 err = got_error_from_errno("getline");
8097 free(line);
8098 return err;
8100 free(line);
8101 printf(GOT_COMMIT_SEP_STR);
8102 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8103 path, n, nchanges, action);
8104 break;
8105 default:
8106 return got_error_path(path, GOT_ERR_FILE_STATUS);
8109 return NULL;
8112 static const struct got_error *
8113 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8114 FILE *patch_file, int n, int nchanges)
8116 const struct got_error *err = NULL;
8117 char *line = NULL;
8118 size_t linesize = 0;
8119 ssize_t linelen;
8120 int resp = ' ';
8121 struct choose_patch_arg *a = arg;
8123 *choice = GOT_PATCH_CHOICE_NONE;
8125 if (a->patch_script_file) {
8126 char *nl;
8127 err = show_change(status, path, patch_file, n, nchanges,
8128 a->action);
8129 if (err)
8130 return err;
8131 linelen = getline(&line, &linesize, a->patch_script_file);
8132 if (linelen == -1) {
8133 if (ferror(a->patch_script_file))
8134 return got_error_from_errno("getline");
8135 return NULL;
8137 nl = strchr(line, '\n');
8138 if (nl)
8139 *nl = '\0';
8140 if (strcmp(line, "y") == 0) {
8141 *choice = GOT_PATCH_CHOICE_YES;
8142 printf("y\n");
8143 } else if (strcmp(line, "n") == 0) {
8144 *choice = GOT_PATCH_CHOICE_NO;
8145 printf("n\n");
8146 } else if (strcmp(line, "q") == 0 &&
8147 status == GOT_STATUS_MODIFY) {
8148 *choice = GOT_PATCH_CHOICE_QUIT;
8149 printf("q\n");
8150 } else
8151 printf("invalid response '%s'\n", line);
8152 free(line);
8153 return NULL;
8156 while (resp != 'y' && resp != 'n' && resp != 'q') {
8157 err = show_change(status, path, patch_file, n, nchanges,
8158 a->action);
8159 if (err)
8160 return err;
8161 resp = getchar();
8162 if (resp == '\n')
8163 resp = getchar();
8164 if (status == GOT_STATUS_MODIFY) {
8165 if (resp != 'y' && resp != 'n' && resp != 'q') {
8166 printf("invalid response '%c'\n", resp);
8167 resp = ' ';
8169 } else if (resp != 'y' && resp != 'n') {
8170 printf("invalid response '%c'\n", resp);
8171 resp = ' ';
8175 if (resp == 'y')
8176 *choice = GOT_PATCH_CHOICE_YES;
8177 else if (resp == 'n')
8178 *choice = GOT_PATCH_CHOICE_NO;
8179 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8180 *choice = GOT_PATCH_CHOICE_QUIT;
8182 return NULL;
8185 static const struct got_error *
8186 cmd_revert(int argc, char *argv[])
8188 const struct got_error *error = NULL;
8189 struct got_worktree *worktree = NULL;
8190 struct got_repository *repo = NULL;
8191 char *cwd = NULL, *path = NULL;
8192 struct got_pathlist_head paths;
8193 struct got_pathlist_entry *pe;
8194 int ch, can_recurse = 0, pflag = 0;
8195 FILE *patch_script_file = NULL;
8196 const char *patch_script_path = NULL;
8197 struct choose_patch_arg cpa;
8198 int *pack_fds = NULL;
8200 TAILQ_INIT(&paths);
8202 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8203 switch (ch) {
8204 case 'p':
8205 pflag = 1;
8206 break;
8207 case 'F':
8208 patch_script_path = optarg;
8209 break;
8210 case 'R':
8211 can_recurse = 1;
8212 break;
8213 default:
8214 usage_revert();
8215 /* NOTREACHED */
8219 argc -= optind;
8220 argv += optind;
8222 #ifndef PROFILE
8223 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8224 "unveil", NULL) == -1)
8225 err(1, "pledge");
8226 #endif
8227 if (argc < 1)
8228 usage_revert();
8229 if (patch_script_path && !pflag)
8230 errx(1, "-F option can only be used together with -p option");
8232 cwd = getcwd(NULL, 0);
8233 if (cwd == NULL) {
8234 error = got_error_from_errno("getcwd");
8235 goto done;
8238 error = got_repo_pack_fds_open(&pack_fds);
8239 if (error != NULL)
8240 goto done;
8242 error = got_worktree_open(&worktree, cwd);
8243 if (error) {
8244 if (error->code == GOT_ERR_NOT_WORKTREE)
8245 error = wrap_not_worktree_error(error, "revert", cwd);
8246 goto done;
8249 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8250 NULL, pack_fds);
8251 if (error != NULL)
8252 goto done;
8254 if (patch_script_path) {
8255 patch_script_file = fopen(patch_script_path, "re");
8256 if (patch_script_file == NULL) {
8257 error = got_error_from_errno2("fopen",
8258 patch_script_path);
8259 goto done;
8262 error = apply_unveil(got_repo_get_path(repo), 1,
8263 got_worktree_get_root_path(worktree));
8264 if (error)
8265 goto done;
8267 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8268 if (error)
8269 goto done;
8271 if (!can_recurse) {
8272 char *ondisk_path;
8273 struct stat sb;
8274 TAILQ_FOREACH(pe, &paths, entry) {
8275 if (asprintf(&ondisk_path, "%s/%s",
8276 got_worktree_get_root_path(worktree),
8277 pe->path) == -1) {
8278 error = got_error_from_errno("asprintf");
8279 goto done;
8281 if (lstat(ondisk_path, &sb) == -1) {
8282 if (errno == ENOENT) {
8283 free(ondisk_path);
8284 continue;
8286 error = got_error_from_errno2("lstat",
8287 ondisk_path);
8288 free(ondisk_path);
8289 goto done;
8291 free(ondisk_path);
8292 if (S_ISDIR(sb.st_mode)) {
8293 error = got_error_msg(GOT_ERR_BAD_PATH,
8294 "reverting directories requires -R option");
8295 goto done;
8300 cpa.patch_script_file = patch_script_file;
8301 cpa.action = "revert";
8302 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8303 pflag ? choose_patch : NULL, &cpa, repo);
8304 done:
8305 if (patch_script_file && fclose(patch_script_file) == EOF &&
8306 error == NULL)
8307 error = got_error_from_errno2("fclose", patch_script_path);
8308 if (repo) {
8309 const struct got_error *close_err = got_repo_close(repo);
8310 if (error == NULL)
8311 error = close_err;
8313 if (worktree)
8314 got_worktree_close(worktree);
8315 if (pack_fds) {
8316 const struct got_error *pack_err =
8317 got_repo_pack_fds_close(pack_fds);
8318 if (error == NULL)
8319 error = pack_err;
8321 free(path);
8322 free(cwd);
8323 return error;
8326 __dead static void
8327 usage_commit(void)
8329 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8330 "[path ...]\n", getprogname());
8331 exit(1);
8334 struct collect_commit_logmsg_arg {
8335 const char *cmdline_log;
8336 const char *prepared_log;
8337 int non_interactive;
8338 const char *editor;
8339 const char *worktree_path;
8340 const char *branch_name;
8341 const char *repo_path;
8342 char *logmsg_path;
8346 static const struct got_error *
8347 read_prepared_logmsg(char **logmsg, const char *path)
8349 const struct got_error *err = NULL;
8350 FILE *f = NULL;
8351 struct stat sb;
8352 size_t r;
8354 *logmsg = NULL;
8355 memset(&sb, 0, sizeof(sb));
8357 f = fopen(path, "re");
8358 if (f == NULL)
8359 return got_error_from_errno2("fopen", path);
8361 if (fstat(fileno(f), &sb) == -1) {
8362 err = got_error_from_errno2("fstat", path);
8363 goto done;
8365 if (sb.st_size == 0) {
8366 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8367 goto done;
8370 *logmsg = malloc(sb.st_size + 1);
8371 if (*logmsg == NULL) {
8372 err = got_error_from_errno("malloc");
8373 goto done;
8376 r = fread(*logmsg, 1, sb.st_size, f);
8377 if (r != sb.st_size) {
8378 if (ferror(f))
8379 err = got_error_from_errno2("fread", path);
8380 else
8381 err = got_error(GOT_ERR_IO);
8382 goto done;
8384 (*logmsg)[sb.st_size] = '\0';
8385 done:
8386 if (fclose(f) == EOF && err == NULL)
8387 err = got_error_from_errno2("fclose", path);
8388 if (err) {
8389 free(*logmsg);
8390 *logmsg = NULL;
8392 return err;
8396 static const struct got_error *
8397 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8398 void *arg)
8400 char *initial_content = NULL;
8401 struct got_pathlist_entry *pe;
8402 const struct got_error *err = NULL;
8403 char *template = NULL;
8404 struct collect_commit_logmsg_arg *a = arg;
8405 int initial_content_len;
8406 int fd = -1;
8407 size_t len;
8409 /* if a message was specified on the command line, just use it */
8410 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8411 len = strlen(a->cmdline_log) + 1;
8412 *logmsg = malloc(len + 1);
8413 if (*logmsg == NULL)
8414 return got_error_from_errno("malloc");
8415 strlcpy(*logmsg, a->cmdline_log, len);
8416 return NULL;
8417 } else if (a->prepared_log != NULL && a->non_interactive)
8418 return read_prepared_logmsg(logmsg, a->prepared_log);
8420 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8421 return got_error_from_errno("asprintf");
8423 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8424 if (err)
8425 goto done;
8427 if (a->prepared_log) {
8428 char *msg;
8429 err = read_prepared_logmsg(&msg, a->prepared_log);
8430 if (err)
8431 goto done;
8432 if (write(fd, msg, strlen(msg)) == -1) {
8433 err = got_error_from_errno2("write", a->logmsg_path);
8434 free(msg);
8435 goto done;
8437 free(msg);
8440 initial_content_len = asprintf(&initial_content,
8441 "\n# changes to be committed on branch %s:\n",
8442 a->branch_name);
8443 if (initial_content_len == -1) {
8444 err = got_error_from_errno("asprintf");
8445 goto done;
8448 if (write(fd, initial_content, initial_content_len) == -1) {
8449 err = got_error_from_errno2("write", a->logmsg_path);
8450 goto done;
8453 TAILQ_FOREACH(pe, commitable_paths, entry) {
8454 struct got_commitable *ct = pe->data;
8455 dprintf(fd, "# %c %s\n",
8456 got_commitable_get_status(ct),
8457 got_commitable_get_path(ct));
8460 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8461 initial_content_len, a->prepared_log ? 0 : 1);
8462 done:
8463 free(initial_content);
8464 free(template);
8466 if (fd != -1 && close(fd) == -1 && err == NULL)
8467 err = got_error_from_errno2("close", a->logmsg_path);
8469 /* Editor is done; we can now apply unveil(2) */
8470 if (err == NULL)
8471 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8472 if (err) {
8473 free(*logmsg);
8474 *logmsg = NULL;
8476 return err;
8479 static const struct got_error *
8480 cmd_commit(int argc, char *argv[])
8482 const struct got_error *error = NULL;
8483 struct got_worktree *worktree = NULL;
8484 struct got_repository *repo = NULL;
8485 char *cwd = NULL, *id_str = NULL;
8486 struct got_object_id *id = NULL;
8487 const char *logmsg = NULL;
8488 char *prepared_logmsg = NULL;
8489 struct collect_commit_logmsg_arg cl_arg;
8490 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8491 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8492 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8493 struct got_pathlist_head paths;
8494 int *pack_fds = NULL;
8496 TAILQ_INIT(&paths);
8497 cl_arg.logmsg_path = NULL;
8499 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8500 switch (ch) {
8501 case 'F':
8502 if (logmsg != NULL)
8503 option_conflict('F', 'm');
8504 prepared_logmsg = realpath(optarg, NULL);
8505 if (prepared_logmsg == NULL)
8506 return got_error_from_errno2("realpath",
8507 optarg);
8508 break;
8509 case 'm':
8510 if (prepared_logmsg)
8511 option_conflict('m', 'F');
8512 logmsg = optarg;
8513 break;
8514 case 'N':
8515 non_interactive = 1;
8516 break;
8517 case 'S':
8518 allow_bad_symlinks = 1;
8519 break;
8520 default:
8521 usage_commit();
8522 /* NOTREACHED */
8526 argc -= optind;
8527 argv += optind;
8529 #ifndef PROFILE
8530 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8531 "unveil", NULL) == -1)
8532 err(1, "pledge");
8533 #endif
8534 cwd = getcwd(NULL, 0);
8535 if (cwd == NULL) {
8536 error = got_error_from_errno("getcwd");
8537 goto done;
8540 error = got_repo_pack_fds_open(&pack_fds);
8541 if (error != NULL)
8542 goto done;
8544 error = got_worktree_open(&worktree, cwd);
8545 if (error) {
8546 if (error->code == GOT_ERR_NOT_WORKTREE)
8547 error = wrap_not_worktree_error(error, "commit", cwd);
8548 goto done;
8551 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8552 if (error)
8553 goto done;
8554 if (rebase_in_progress) {
8555 error = got_error(GOT_ERR_REBASING);
8556 goto done;
8559 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8560 worktree);
8561 if (error)
8562 goto done;
8564 error = get_gitconfig_path(&gitconfig_path);
8565 if (error)
8566 goto done;
8567 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8568 gitconfig_path, pack_fds);
8569 if (error != NULL)
8570 goto done;
8572 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8573 if (error)
8574 goto done;
8575 if (merge_in_progress) {
8576 error = got_error(GOT_ERR_MERGE_BUSY);
8577 goto done;
8580 error = get_author(&author, repo, worktree);
8581 if (error)
8582 return error;
8585 * unveil(2) traverses exec(2); if an editor is used we have
8586 * to apply unveil after the log message has been written.
8588 if (logmsg == NULL || strlen(logmsg) == 0)
8589 error = get_editor(&editor);
8590 else
8591 error = apply_unveil(got_repo_get_path(repo), 0,
8592 got_worktree_get_root_path(worktree));
8593 if (error)
8594 goto done;
8596 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8597 if (error)
8598 goto done;
8600 cl_arg.editor = editor;
8601 cl_arg.cmdline_log = logmsg;
8602 cl_arg.prepared_log = prepared_logmsg;
8603 cl_arg.non_interactive = non_interactive;
8604 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8605 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8606 if (!histedit_in_progress) {
8607 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8608 error = got_error(GOT_ERR_COMMIT_BRANCH);
8609 goto done;
8611 cl_arg.branch_name += 11;
8613 cl_arg.repo_path = got_repo_get_path(repo);
8614 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8615 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8616 print_status, NULL, repo);
8617 if (error) {
8618 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8619 cl_arg.logmsg_path != NULL)
8620 preserve_logmsg = 1;
8621 goto done;
8624 error = got_object_id_str(&id_str, id);
8625 if (error)
8626 goto done;
8627 printf("Created commit %s\n", id_str);
8628 done:
8629 if (preserve_logmsg) {
8630 fprintf(stderr, "%s: log message preserved in %s\n",
8631 getprogname(), cl_arg.logmsg_path);
8632 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8633 error == NULL)
8634 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8635 free(cl_arg.logmsg_path);
8636 if (repo) {
8637 const struct got_error *close_err = got_repo_close(repo);
8638 if (error == NULL)
8639 error = close_err;
8641 if (worktree)
8642 got_worktree_close(worktree);
8643 if (pack_fds) {
8644 const struct got_error *pack_err =
8645 got_repo_pack_fds_close(pack_fds);
8646 if (error == NULL)
8647 error = pack_err;
8649 free(cwd);
8650 free(id_str);
8651 free(gitconfig_path);
8652 free(editor);
8653 free(author);
8654 free(prepared_logmsg);
8655 return error;
8658 __dead static void
8659 usage_send(void)
8661 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8662 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8663 "[remote-repository]\n", getprogname());
8664 exit(1);
8667 static void
8668 print_load_info(int print_colored, int print_found, int print_trees,
8669 int ncolored, int nfound, int ntrees)
8671 if (print_colored) {
8672 printf("%d commit%s colored", ncolored,
8673 ncolored == 1 ? "" : "s");
8675 if (print_found) {
8676 printf("%s%d object%s found",
8677 ncolored > 0 ? "; " : "",
8678 nfound, nfound == 1 ? "" : "s");
8680 if (print_trees) {
8681 printf("; %d tree%s scanned", ntrees,
8682 ntrees == 1 ? "" : "s");
8686 struct got_send_progress_arg {
8687 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8688 int verbosity;
8689 int last_ncolored;
8690 int last_nfound;
8691 int last_ntrees;
8692 int loading_done;
8693 int last_ncommits;
8694 int last_nobj_total;
8695 int last_p_deltify;
8696 int last_p_written;
8697 int last_p_sent;
8698 int printed_something;
8699 int sent_something;
8700 struct got_pathlist_head *delete_branches;
8703 static const struct got_error *
8704 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8705 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8706 int nobj_written, off_t bytes_sent, const char *refname, int success)
8708 struct got_send_progress_arg *a = arg;
8709 char scaled_packsize[FMT_SCALED_STRSIZE];
8710 char scaled_sent[FMT_SCALED_STRSIZE];
8711 int p_deltify = 0, p_written = 0, p_sent = 0;
8712 int print_colored = 0, print_found = 0, print_trees = 0;
8713 int print_searching = 0, print_total = 0;
8714 int print_deltify = 0, print_written = 0, print_sent = 0;
8716 if (a->verbosity < 0)
8717 return NULL;
8719 if (refname) {
8720 const char *status = success ? "accepted" : "rejected";
8722 if (success) {
8723 struct got_pathlist_entry *pe;
8724 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8725 const char *branchname = pe->path;
8726 if (got_path_cmp(branchname, refname,
8727 strlen(branchname), strlen(refname)) == 0) {
8728 status = "deleted";
8729 a->sent_something = 1;
8730 break;
8735 if (a->printed_something)
8736 putchar('\n');
8737 printf("Server has %s %s", status, refname);
8738 a->printed_something = 1;
8739 return NULL;
8742 if (a->last_ncolored != ncolored) {
8743 print_colored = 1;
8744 a->last_ncolored = ncolored;
8747 if (a->last_nfound != nfound) {
8748 print_colored = 1;
8749 print_found = 1;
8750 a->last_nfound = nfound;
8753 if (a->last_ntrees != ntrees) {
8754 print_colored = 1;
8755 print_found = 1;
8756 print_trees = 1;
8757 a->last_ntrees = ntrees;
8760 if ((print_colored || print_found || print_trees) &&
8761 !a->loading_done) {
8762 printf("\r");
8763 print_load_info(print_colored, print_found, print_trees,
8764 ncolored, nfound, ntrees);
8765 a->printed_something = 1;
8766 fflush(stdout);
8767 return NULL;
8768 } else if (!a->loading_done) {
8769 printf("\r");
8770 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8771 printf("\n");
8772 a->loading_done = 1;
8775 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8776 return got_error_from_errno("fmt_scaled");
8777 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8778 return got_error_from_errno("fmt_scaled");
8780 if (a->last_ncommits != ncommits) {
8781 print_searching = 1;
8782 a->last_ncommits = ncommits;
8785 if (a->last_nobj_total != nobj_total) {
8786 print_searching = 1;
8787 print_total = 1;
8788 a->last_nobj_total = nobj_total;
8791 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8792 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8793 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8794 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8795 return got_error(GOT_ERR_NO_SPACE);
8798 if (nobj_deltify > 0 || nobj_written > 0) {
8799 if (nobj_deltify > 0) {
8800 p_deltify = (nobj_deltify * 100) / nobj_total;
8801 if (p_deltify != a->last_p_deltify) {
8802 a->last_p_deltify = p_deltify;
8803 print_searching = 1;
8804 print_total = 1;
8805 print_deltify = 1;
8808 if (nobj_written > 0) {
8809 p_written = (nobj_written * 100) / nobj_total;
8810 if (p_written != a->last_p_written) {
8811 a->last_p_written = p_written;
8812 print_searching = 1;
8813 print_total = 1;
8814 print_deltify = 1;
8815 print_written = 1;
8820 if (bytes_sent > 0) {
8821 p_sent = (bytes_sent * 100) / packfile_size;
8822 if (p_sent != a->last_p_sent) {
8823 a->last_p_sent = p_sent;
8824 print_searching = 1;
8825 print_total = 1;
8826 print_deltify = 1;
8827 print_written = 1;
8828 print_sent = 1;
8830 a->sent_something = 1;
8833 if (print_searching || print_total || print_deltify || print_written ||
8834 print_sent)
8835 printf("\r");
8836 if (print_searching)
8837 printf("packing %d reference%s", ncommits,
8838 ncommits == 1 ? "" : "s");
8839 if (print_total)
8840 printf("; %d object%s", nobj_total,
8841 nobj_total == 1 ? "" : "s");
8842 if (print_deltify)
8843 printf("; deltify: %d%%", p_deltify);
8844 if (print_sent)
8845 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8846 scaled_packsize, p_sent);
8847 else if (print_written)
8848 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8849 scaled_packsize, p_written);
8850 if (print_searching || print_total || print_deltify ||
8851 print_written || print_sent) {
8852 a->printed_something = 1;
8853 fflush(stdout);
8855 return NULL;
8858 static const struct got_error *
8859 cmd_send(int argc, char *argv[])
8861 const struct got_error *error = NULL;
8862 char *cwd = NULL, *repo_path = NULL;
8863 const char *remote_name;
8864 char *proto = NULL, *host = NULL, *port = NULL;
8865 char *repo_name = NULL, *server_path = NULL;
8866 const struct got_remote_repo *remotes, *remote = NULL;
8867 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8868 struct got_repository *repo = NULL;
8869 struct got_worktree *worktree = NULL;
8870 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8871 struct got_pathlist_head branches;
8872 struct got_pathlist_head tags;
8873 struct got_reflist_head all_branches;
8874 struct got_reflist_head all_tags;
8875 struct got_pathlist_head delete_args;
8876 struct got_pathlist_head delete_branches;
8877 struct got_reflist_entry *re;
8878 struct got_pathlist_entry *pe;
8879 int i, ch, sendfd = -1, sendstatus;
8880 pid_t sendpid = -1;
8881 struct got_send_progress_arg spa;
8882 int verbosity = 0, overwrite_refs = 0;
8883 int send_all_branches = 0, send_all_tags = 0;
8884 struct got_reference *ref = NULL;
8885 int *pack_fds = NULL;
8887 TAILQ_INIT(&branches);
8888 TAILQ_INIT(&tags);
8889 TAILQ_INIT(&all_branches);
8890 TAILQ_INIT(&all_tags);
8891 TAILQ_INIT(&delete_args);
8892 TAILQ_INIT(&delete_branches);
8894 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8895 switch (ch) {
8896 case 'a':
8897 send_all_branches = 1;
8898 break;
8899 case 'b':
8900 error = got_pathlist_append(&branches, optarg, NULL);
8901 if (error)
8902 return error;
8903 nbranches++;
8904 break;
8905 case 'd':
8906 error = got_pathlist_append(&delete_args, optarg, NULL);
8907 if (error)
8908 return error;
8909 break;
8910 case 'f':
8911 overwrite_refs = 1;
8912 break;
8913 case 'r':
8914 repo_path = realpath(optarg, NULL);
8915 if (repo_path == NULL)
8916 return got_error_from_errno2("realpath",
8917 optarg);
8918 got_path_strip_trailing_slashes(repo_path);
8919 break;
8920 case 't':
8921 error = got_pathlist_append(&tags, optarg, NULL);
8922 if (error)
8923 return error;
8924 ntags++;
8925 break;
8926 case 'T':
8927 send_all_tags = 1;
8928 break;
8929 case 'v':
8930 if (verbosity < 0)
8931 verbosity = 0;
8932 else if (verbosity < 3)
8933 verbosity++;
8934 break;
8935 case 'q':
8936 verbosity = -1;
8937 break;
8938 default:
8939 usage_send();
8940 /* NOTREACHED */
8943 argc -= optind;
8944 argv += optind;
8946 if (send_all_branches && !TAILQ_EMPTY(&branches))
8947 option_conflict('a', 'b');
8948 if (send_all_tags && !TAILQ_EMPTY(&tags))
8949 option_conflict('T', 't');
8952 if (argc == 0)
8953 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8954 else if (argc == 1)
8955 remote_name = argv[0];
8956 else
8957 usage_send();
8959 cwd = getcwd(NULL, 0);
8960 if (cwd == NULL) {
8961 error = got_error_from_errno("getcwd");
8962 goto done;
8965 error = got_repo_pack_fds_open(&pack_fds);
8966 if (error != NULL)
8967 goto done;
8969 if (repo_path == NULL) {
8970 error = got_worktree_open(&worktree, cwd);
8971 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8972 goto done;
8973 else
8974 error = NULL;
8975 if (worktree) {
8976 repo_path =
8977 strdup(got_worktree_get_repo_path(worktree));
8978 if (repo_path == NULL)
8979 error = got_error_from_errno("strdup");
8980 if (error)
8981 goto done;
8982 } else {
8983 repo_path = strdup(cwd);
8984 if (repo_path == NULL) {
8985 error = got_error_from_errno("strdup");
8986 goto done;
8991 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8992 if (error)
8993 goto done;
8995 if (worktree) {
8996 worktree_conf = got_worktree_get_gotconfig(worktree);
8997 if (worktree_conf) {
8998 got_gotconfig_get_remotes(&nremotes, &remotes,
8999 worktree_conf);
9000 for (i = 0; i < nremotes; i++) {
9001 if (strcmp(remotes[i].name, remote_name) == 0) {
9002 remote = &remotes[i];
9003 break;
9008 if (remote == NULL) {
9009 repo_conf = got_repo_get_gotconfig(repo);
9010 if (repo_conf) {
9011 got_gotconfig_get_remotes(&nremotes, &remotes,
9012 repo_conf);
9013 for (i = 0; i < nremotes; i++) {
9014 if (strcmp(remotes[i].name, remote_name) == 0) {
9015 remote = &remotes[i];
9016 break;
9021 if (remote == NULL) {
9022 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9023 for (i = 0; i < nremotes; i++) {
9024 if (strcmp(remotes[i].name, remote_name) == 0) {
9025 remote = &remotes[i];
9026 break;
9030 if (remote == NULL) {
9031 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9032 goto done;
9035 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9036 &repo_name, remote->send_url);
9037 if (error)
9038 goto done;
9040 if (strcmp(proto, "git") == 0) {
9041 #ifndef PROFILE
9042 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9043 "sendfd dns inet unveil", NULL) == -1)
9044 err(1, "pledge");
9045 #endif
9046 } else if (strcmp(proto, "git+ssh") == 0 ||
9047 strcmp(proto, "ssh") == 0) {
9048 #ifndef PROFILE
9049 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9050 "sendfd unveil", NULL) == -1)
9051 err(1, "pledge");
9052 #endif
9053 } else if (strcmp(proto, "http") == 0 ||
9054 strcmp(proto, "git+http") == 0) {
9055 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9056 goto done;
9057 } else {
9058 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9059 goto done;
9062 error = got_dial_apply_unveil(proto);
9063 if (error)
9064 goto done;
9066 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9067 if (error)
9068 goto done;
9070 if (send_all_branches) {
9071 error = got_ref_list(&all_branches, repo, "refs/heads",
9072 got_ref_cmp_by_name, NULL);
9073 if (error)
9074 goto done;
9075 TAILQ_FOREACH(re, &all_branches, entry) {
9076 const char *branchname = got_ref_get_name(re->ref);
9077 error = got_pathlist_append(&branches,
9078 branchname, NULL);
9079 if (error)
9080 goto done;
9081 nbranches++;
9083 } else if (nbranches == 0) {
9084 for (i = 0; i < remote->nsend_branches; i++) {
9085 got_pathlist_append(&branches,
9086 remote->send_branches[i], NULL);
9090 if (send_all_tags) {
9091 error = got_ref_list(&all_tags, repo, "refs/tags",
9092 got_ref_cmp_by_name, NULL);
9093 if (error)
9094 goto done;
9095 TAILQ_FOREACH(re, &all_tags, entry) {
9096 const char *tagname = got_ref_get_name(re->ref);
9097 error = got_pathlist_append(&tags,
9098 tagname, NULL);
9099 if (error)
9100 goto done;
9101 ntags++;
9106 * To prevent accidents only branches in refs/heads/ can be deleted
9107 * with 'got send -d'.
9108 * Deleting anything else requires local repository access or Git.
9110 TAILQ_FOREACH(pe, &delete_args, entry) {
9111 const char *branchname = pe->path;
9112 char *s;
9113 struct got_pathlist_entry *new;
9114 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9115 s = strdup(branchname);
9116 if (s == NULL) {
9117 error = got_error_from_errno("strdup");
9118 goto done;
9120 } else {
9121 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9122 error = got_error_from_errno("asprintf");
9123 goto done;
9126 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9127 if (error || new == NULL /* duplicate */)
9128 free(s);
9129 if (error)
9130 goto done;
9131 ndelete_branches++;
9134 if (nbranches == 0 && ndelete_branches == 0) {
9135 struct got_reference *head_ref;
9136 if (worktree)
9137 error = got_ref_open(&head_ref, repo,
9138 got_worktree_get_head_ref_name(worktree), 0);
9139 else
9140 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9141 if (error)
9142 goto done;
9143 if (got_ref_is_symbolic(head_ref)) {
9144 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9145 got_ref_close(head_ref);
9146 if (error)
9147 goto done;
9148 } else
9149 ref = head_ref;
9150 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9151 NULL);
9152 if (error)
9153 goto done;
9154 nbranches++;
9157 if (verbosity >= 0)
9158 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9159 port ? ":" : "", port ? port : "");
9161 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9162 server_path, verbosity);
9163 if (error)
9164 goto done;
9166 memset(&spa, 0, sizeof(spa));
9167 spa.last_scaled_packsize[0] = '\0';
9168 spa.last_p_deltify = -1;
9169 spa.last_p_written = -1;
9170 spa.verbosity = verbosity;
9171 spa.delete_branches = &delete_branches;
9172 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9173 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9174 check_cancelled, NULL);
9175 if (spa.printed_something)
9176 putchar('\n');
9177 if (error)
9178 goto done;
9179 if (!spa.sent_something && verbosity >= 0)
9180 printf("Already up-to-date\n");
9181 done:
9182 if (sendpid > 0) {
9183 if (kill(sendpid, SIGTERM) == -1)
9184 error = got_error_from_errno("kill");
9185 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9186 error = got_error_from_errno("waitpid");
9188 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9189 error = got_error_from_errno("close");
9190 if (repo) {
9191 const struct got_error *close_err = got_repo_close(repo);
9192 if (error == NULL)
9193 error = close_err;
9195 if (worktree)
9196 got_worktree_close(worktree);
9197 if (pack_fds) {
9198 const struct got_error *pack_err =
9199 got_repo_pack_fds_close(pack_fds);
9200 if (error == NULL)
9201 error = pack_err;
9203 if (ref)
9204 got_ref_close(ref);
9205 got_pathlist_free(&branches);
9206 got_pathlist_free(&tags);
9207 got_ref_list_free(&all_branches);
9208 got_ref_list_free(&all_tags);
9209 got_pathlist_free(&delete_args);
9210 TAILQ_FOREACH(pe, &delete_branches, entry)
9211 free((char *)pe->path);
9212 got_pathlist_free(&delete_branches);
9213 free(cwd);
9214 free(repo_path);
9215 free(proto);
9216 free(host);
9217 free(port);
9218 free(server_path);
9219 free(repo_name);
9220 return error;
9223 __dead static void
9224 usage_cherrypick(void)
9226 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9227 exit(1);
9230 static const struct got_error *
9231 cmd_cherrypick(int argc, char *argv[])
9233 const struct got_error *error = NULL;
9234 struct got_worktree *worktree = NULL;
9235 struct got_repository *repo = NULL;
9236 char *cwd = NULL, *commit_id_str = NULL;
9237 struct got_object_id *commit_id = NULL;
9238 struct got_commit_object *commit = NULL;
9239 struct got_object_qid *pid;
9240 int ch;
9241 struct got_update_progress_arg upa;
9242 int *pack_fds = NULL;
9244 while ((ch = getopt(argc, argv, "")) != -1) {
9245 switch (ch) {
9246 default:
9247 usage_cherrypick();
9248 /* NOTREACHED */
9252 argc -= optind;
9253 argv += optind;
9255 #ifndef PROFILE
9256 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9257 "unveil", NULL) == -1)
9258 err(1, "pledge");
9259 #endif
9260 if (argc != 1)
9261 usage_cherrypick();
9263 cwd = getcwd(NULL, 0);
9264 if (cwd == NULL) {
9265 error = got_error_from_errno("getcwd");
9266 goto done;
9269 error = got_repo_pack_fds_open(&pack_fds);
9270 if (error != NULL)
9271 goto done;
9273 error = got_worktree_open(&worktree, cwd);
9274 if (error) {
9275 if (error->code == GOT_ERR_NOT_WORKTREE)
9276 error = wrap_not_worktree_error(error, "cherrypick",
9277 cwd);
9278 goto done;
9281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9282 NULL, pack_fds);
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 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9292 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9293 if (error)
9294 goto done;
9295 error = got_object_id_str(&commit_id_str, commit_id);
9296 if (error)
9297 goto done;
9299 error = got_object_open_as_commit(&commit, repo, commit_id);
9300 if (error)
9301 goto done;
9302 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9303 memset(&upa, 0, sizeof(upa));
9304 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9305 commit_id, repo, update_progress, &upa, check_cancelled,
9306 NULL);
9307 if (error != NULL)
9308 goto done;
9310 if (upa.did_something)
9311 printf("Merged commit %s\n", commit_id_str);
9312 print_merge_progress_stats(&upa);
9313 done:
9314 if (commit)
9315 got_object_commit_close(commit);
9316 free(commit_id_str);
9317 if (worktree)
9318 got_worktree_close(worktree);
9319 if (repo) {
9320 const struct got_error *close_err = got_repo_close(repo);
9321 if (error == NULL)
9322 error = close_err;
9324 if (pack_fds) {
9325 const struct got_error *pack_err =
9326 got_repo_pack_fds_close(pack_fds);
9327 if (error == NULL)
9328 error = pack_err;
9331 return error;
9334 __dead static void
9335 usage_backout(void)
9337 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9338 exit(1);
9341 static const struct got_error *
9342 cmd_backout(int argc, char *argv[])
9344 const struct got_error *error = NULL;
9345 struct got_worktree *worktree = NULL;
9346 struct got_repository *repo = NULL;
9347 char *cwd = NULL, *commit_id_str = NULL;
9348 struct got_object_id *commit_id = NULL;
9349 struct got_commit_object *commit = NULL;
9350 struct got_object_qid *pid;
9351 int ch;
9352 struct got_update_progress_arg upa;
9353 int *pack_fds = NULL;
9355 while ((ch = getopt(argc, argv, "")) != -1) {
9356 switch (ch) {
9357 default:
9358 usage_backout();
9359 /* NOTREACHED */
9363 argc -= optind;
9364 argv += optind;
9366 #ifndef PROFILE
9367 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9368 "unveil", NULL) == -1)
9369 err(1, "pledge");
9370 #endif
9371 if (argc != 1)
9372 usage_backout();
9374 cwd = getcwd(NULL, 0);
9375 if (cwd == NULL) {
9376 error = got_error_from_errno("getcwd");
9377 goto done;
9380 error = got_repo_pack_fds_open(&pack_fds);
9381 if (error != NULL)
9382 goto done;
9384 error = got_worktree_open(&worktree, cwd);
9385 if (error) {
9386 if (error->code == GOT_ERR_NOT_WORKTREE)
9387 error = wrap_not_worktree_error(error, "backout", cwd);
9388 goto done;
9391 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9392 NULL, pack_fds);
9393 if (error != NULL)
9394 goto done;
9396 error = apply_unveil(got_repo_get_path(repo), 0,
9397 got_worktree_get_root_path(worktree));
9398 if (error)
9399 goto done;
9401 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9402 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9403 if (error)
9404 goto done;
9405 error = got_object_id_str(&commit_id_str, commit_id);
9406 if (error)
9407 goto done;
9409 error = got_object_open_as_commit(&commit, repo, commit_id);
9410 if (error)
9411 goto done;
9412 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9413 if (pid == NULL) {
9414 error = got_error(GOT_ERR_ROOT_COMMIT);
9415 goto done;
9418 memset(&upa, 0, sizeof(upa));
9419 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9420 repo, update_progress, &upa, check_cancelled, NULL);
9421 if (error != NULL)
9422 goto done;
9424 if (upa.did_something)
9425 printf("Backed out commit %s\n", commit_id_str);
9426 print_merge_progress_stats(&upa);
9427 done:
9428 if (commit)
9429 got_object_commit_close(commit);
9430 free(commit_id_str);
9431 if (worktree)
9432 got_worktree_close(worktree);
9433 if (repo) {
9434 const struct got_error *close_err = got_repo_close(repo);
9435 if (error == NULL)
9436 error = close_err;
9438 if (pack_fds) {
9439 const struct got_error *pack_err =
9440 got_repo_pack_fds_close(pack_fds);
9441 if (error == NULL)
9442 error = pack_err;
9444 return error;
9447 __dead static void
9448 usage_rebase(void)
9450 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9451 getprogname());
9452 exit(1);
9455 static void
9456 trim_logmsg(char *logmsg, int limit)
9458 char *nl;
9459 size_t len;
9461 len = strlen(logmsg);
9462 if (len > limit)
9463 len = limit;
9464 logmsg[len] = '\0';
9465 nl = strchr(logmsg, '\n');
9466 if (nl)
9467 *nl = '\0';
9470 static const struct got_error *
9471 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9473 const struct got_error *err;
9474 char *logmsg0 = NULL;
9475 const char *s;
9477 err = got_object_commit_get_logmsg(&logmsg0, commit);
9478 if (err)
9479 return err;
9481 s = logmsg0;
9482 while (isspace((unsigned char)s[0]))
9483 s++;
9485 *logmsg = strdup(s);
9486 if (*logmsg == NULL) {
9487 err = got_error_from_errno("strdup");
9488 goto done;
9491 trim_logmsg(*logmsg, limit);
9492 done:
9493 free(logmsg0);
9494 return err;
9497 static const struct got_error *
9498 show_rebase_merge_conflict(struct got_object_id *id,
9499 struct got_repository *repo)
9501 const struct got_error *err;
9502 struct got_commit_object *commit = NULL;
9503 char *id_str = NULL, *logmsg = NULL;
9505 err = got_object_open_as_commit(&commit, repo, id);
9506 if (err)
9507 return err;
9509 err = got_object_id_str(&id_str, id);
9510 if (err)
9511 goto done;
9513 id_str[12] = '\0';
9515 err = get_short_logmsg(&logmsg, 42, commit);
9516 if (err)
9517 goto done;
9519 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9520 done:
9521 free(id_str);
9522 got_object_commit_close(commit);
9523 free(logmsg);
9524 return err;
9527 static const struct got_error *
9528 show_rebase_progress(struct got_commit_object *commit,
9529 struct got_object_id *old_id, struct got_object_id *new_id)
9531 const struct got_error *err;
9532 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9534 err = got_object_id_str(&old_id_str, old_id);
9535 if (err)
9536 goto done;
9538 if (new_id) {
9539 err = got_object_id_str(&new_id_str, new_id);
9540 if (err)
9541 goto done;
9544 old_id_str[12] = '\0';
9545 if (new_id_str)
9546 new_id_str[12] = '\0';
9548 err = get_short_logmsg(&logmsg, 42, commit);
9549 if (err)
9550 goto done;
9552 printf("%s -> %s: %s\n", old_id_str,
9553 new_id_str ? new_id_str : "no-op change", logmsg);
9554 done:
9555 free(old_id_str);
9556 free(new_id_str);
9557 free(logmsg);
9558 return err;
9561 static const struct got_error *
9562 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9563 struct got_reference *branch, struct got_reference *new_base_branch,
9564 struct got_reference *tmp_branch, struct got_repository *repo,
9565 int create_backup)
9567 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9568 return got_worktree_rebase_complete(worktree, fileindex,
9569 new_base_branch, tmp_branch, branch, repo, create_backup);
9572 static const struct got_error *
9573 rebase_commit(struct got_pathlist_head *merged_paths,
9574 struct got_worktree *worktree, struct got_fileindex *fileindex,
9575 struct got_reference *tmp_branch,
9576 struct got_object_id *commit_id, struct got_repository *repo)
9578 const struct got_error *error;
9579 struct got_commit_object *commit;
9580 struct got_object_id *new_commit_id;
9582 error = got_object_open_as_commit(&commit, repo, commit_id);
9583 if (error)
9584 return error;
9586 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9587 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9588 if (error) {
9589 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9590 goto done;
9591 error = show_rebase_progress(commit, commit_id, NULL);
9592 } else {
9593 error = show_rebase_progress(commit, commit_id, new_commit_id);
9594 free(new_commit_id);
9596 done:
9597 got_object_commit_close(commit);
9598 return error;
9601 struct check_path_prefix_arg {
9602 const char *path_prefix;
9603 size_t len;
9604 int errcode;
9607 static const struct got_error *
9608 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9609 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9610 struct got_object_id *id1, struct got_object_id *id2,
9611 const char *path1, const char *path2,
9612 mode_t mode1, mode_t mode2, struct got_repository *repo)
9614 struct check_path_prefix_arg *a = arg;
9616 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9617 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9618 return got_error(a->errcode);
9620 return NULL;
9623 static const struct got_error *
9624 check_path_prefix(struct got_object_id *parent_id,
9625 struct got_object_id *commit_id, const char *path_prefix,
9626 int errcode, struct got_repository *repo)
9628 const struct got_error *err;
9629 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9630 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9631 struct check_path_prefix_arg cpp_arg;
9633 if (got_path_is_root_dir(path_prefix))
9634 return NULL;
9636 err = got_object_open_as_commit(&commit, repo, commit_id);
9637 if (err)
9638 goto done;
9640 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9641 if (err)
9642 goto done;
9644 err = got_object_open_as_tree(&tree1, repo,
9645 got_object_commit_get_tree_id(parent_commit));
9646 if (err)
9647 goto done;
9649 err = got_object_open_as_tree(&tree2, repo,
9650 got_object_commit_get_tree_id(commit));
9651 if (err)
9652 goto done;
9654 cpp_arg.path_prefix = path_prefix;
9655 while (cpp_arg.path_prefix[0] == '/')
9656 cpp_arg.path_prefix++;
9657 cpp_arg.len = strlen(cpp_arg.path_prefix);
9658 cpp_arg.errcode = errcode;
9659 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9660 check_path_prefix_in_diff, &cpp_arg, 0);
9661 done:
9662 if (tree1)
9663 got_object_tree_close(tree1);
9664 if (tree2)
9665 got_object_tree_close(tree2);
9666 if (commit)
9667 got_object_commit_close(commit);
9668 if (parent_commit)
9669 got_object_commit_close(parent_commit);
9670 return err;
9673 static const struct got_error *
9674 collect_commits(struct got_object_id_queue *commits,
9675 struct got_object_id *initial_commit_id,
9676 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9677 const char *path_prefix, int path_prefix_errcode,
9678 struct got_repository *repo)
9680 const struct got_error *err = NULL;
9681 struct got_commit_graph *graph = NULL;
9682 struct got_object_id *parent_id = NULL;
9683 struct got_object_qid *qid;
9684 struct got_object_id *commit_id = initial_commit_id;
9686 err = got_commit_graph_open(&graph, "/", 1);
9687 if (err)
9688 return err;
9690 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9691 check_cancelled, NULL);
9692 if (err)
9693 goto done;
9694 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9695 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9696 check_cancelled, NULL);
9697 if (err) {
9698 if (err->code == GOT_ERR_ITER_COMPLETED) {
9699 err = got_error_msg(GOT_ERR_ANCESTRY,
9700 "ran out of commits to rebase before "
9701 "youngest common ancestor commit has "
9702 "been reached?!?");
9704 goto done;
9705 } else {
9706 err = check_path_prefix(parent_id, commit_id,
9707 path_prefix, path_prefix_errcode, repo);
9708 if (err)
9709 goto done;
9711 err = got_object_qid_alloc(&qid, commit_id);
9712 if (err)
9713 goto done;
9714 STAILQ_INSERT_HEAD(commits, qid, entry);
9715 commit_id = parent_id;
9718 done:
9719 got_commit_graph_close(graph);
9720 return err;
9723 static const struct got_error *
9724 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9726 const struct got_error *err = NULL;
9727 time_t committer_time;
9728 struct tm tm;
9729 char datebuf[11]; /* YYYY-MM-DD + NUL */
9730 char *author0 = NULL, *author, *smallerthan;
9731 char *logmsg0 = NULL, *logmsg, *newline;
9733 committer_time = got_object_commit_get_committer_time(commit);
9734 if (gmtime_r(&committer_time, &tm) == NULL)
9735 return got_error_from_errno("gmtime_r");
9736 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9737 return got_error(GOT_ERR_NO_SPACE);
9739 author0 = strdup(got_object_commit_get_author(commit));
9740 if (author0 == NULL)
9741 return got_error_from_errno("strdup");
9742 author = author0;
9743 smallerthan = strchr(author, '<');
9744 if (smallerthan && smallerthan[1] != '\0')
9745 author = smallerthan + 1;
9746 author[strcspn(author, "@>")] = '\0';
9748 err = got_object_commit_get_logmsg(&logmsg0, commit);
9749 if (err)
9750 goto done;
9751 logmsg = logmsg0;
9752 while (*logmsg == '\n')
9753 logmsg++;
9754 newline = strchr(logmsg, '\n');
9755 if (newline)
9756 *newline = '\0';
9758 if (asprintf(brief_str, "%s %s %s",
9759 datebuf, author, logmsg) == -1)
9760 err = got_error_from_errno("asprintf");
9761 done:
9762 free(author0);
9763 free(logmsg0);
9764 return err;
9767 static const struct got_error *
9768 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9769 struct got_repository *repo)
9771 const struct got_error *err;
9772 char *id_str;
9774 err = got_object_id_str(&id_str, id);
9775 if (err)
9776 return err;
9778 err = got_ref_delete(ref, repo);
9779 if (err)
9780 goto done;
9782 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9783 done:
9784 free(id_str);
9785 return err;
9788 static const struct got_error *
9789 print_backup_ref(const char *branch_name, const char *new_id_str,
9790 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9791 struct got_reflist_object_id_map *refs_idmap,
9792 struct got_repository *repo)
9794 const struct got_error *err = NULL;
9795 struct got_reflist_head *refs;
9796 char *refs_str = NULL;
9797 struct got_object_id *new_commit_id = NULL;
9798 struct got_commit_object *new_commit = NULL;
9799 char *new_commit_brief_str = NULL;
9800 struct got_object_id *yca_id = NULL;
9801 struct got_commit_object *yca_commit = NULL;
9802 char *yca_id_str = NULL, *yca_brief_str = NULL;
9803 char *custom_refs_str;
9805 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9806 return got_error_from_errno("asprintf");
9808 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9809 0, 0, refs_idmap, custom_refs_str);
9810 if (err)
9811 goto done;
9813 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9814 if (err)
9815 goto done;
9817 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9818 if (refs) {
9819 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9820 if (err)
9821 goto done;
9824 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9825 if (err)
9826 goto done;
9828 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9829 if (err)
9830 goto done;
9832 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9833 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9834 if (err)
9835 goto done;
9837 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9838 refs_str ? " (" : "", refs_str ? refs_str : "",
9839 refs_str ? ")" : "", new_commit_brief_str);
9840 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9841 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9842 free(refs_str);
9843 refs_str = NULL;
9845 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9846 if (err)
9847 goto done;
9849 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9850 if (err)
9851 goto done;
9853 err = got_object_id_str(&yca_id_str, yca_id);
9854 if (err)
9855 goto done;
9857 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9858 if (refs) {
9859 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9860 if (err)
9861 goto done;
9863 printf("history forked at %s%s%s%s\n %s\n",
9864 yca_id_str,
9865 refs_str ? " (" : "", refs_str ? refs_str : "",
9866 refs_str ? ")" : "", yca_brief_str);
9868 done:
9869 free(custom_refs_str);
9870 free(new_commit_id);
9871 free(refs_str);
9872 free(yca_id);
9873 free(yca_id_str);
9874 free(yca_brief_str);
9875 if (new_commit)
9876 got_object_commit_close(new_commit);
9877 if (yca_commit)
9878 got_object_commit_close(yca_commit);
9880 return NULL;
9883 static const struct got_error *
9884 process_backup_refs(const char *backup_ref_prefix,
9885 const char *wanted_branch_name,
9886 int delete, struct got_repository *repo)
9888 const struct got_error *err;
9889 struct got_reflist_head refs, backup_refs;
9890 struct got_reflist_entry *re;
9891 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9892 struct got_object_id *old_commit_id = NULL;
9893 char *branch_name = NULL;
9894 struct got_commit_object *old_commit = NULL;
9895 struct got_reflist_object_id_map *refs_idmap = NULL;
9896 int wanted_branch_found = 0;
9898 TAILQ_INIT(&refs);
9899 TAILQ_INIT(&backup_refs);
9901 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9902 if (err)
9903 return err;
9905 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9906 if (err)
9907 goto done;
9909 if (wanted_branch_name) {
9910 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9911 wanted_branch_name += 11;
9914 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9915 got_ref_cmp_by_commit_timestamp_descending, repo);
9916 if (err)
9917 goto done;
9919 TAILQ_FOREACH(re, &backup_refs, entry) {
9920 const char *refname = got_ref_get_name(re->ref);
9921 char *slash;
9923 err = check_cancelled(NULL);
9924 if (err)
9925 break;
9927 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9928 if (err)
9929 break;
9931 err = got_object_open_as_commit(&old_commit, repo,
9932 old_commit_id);
9933 if (err)
9934 break;
9936 if (strncmp(backup_ref_prefix, refname,
9937 backup_ref_prefix_len) == 0)
9938 refname += backup_ref_prefix_len;
9940 while (refname[0] == '/')
9941 refname++;
9943 branch_name = strdup(refname);
9944 if (branch_name == NULL) {
9945 err = got_error_from_errno("strdup");
9946 break;
9948 slash = strrchr(branch_name, '/');
9949 if (slash) {
9950 *slash = '\0';
9951 refname += strlen(branch_name) + 1;
9954 if (wanted_branch_name == NULL ||
9955 strcmp(wanted_branch_name, branch_name) == 0) {
9956 wanted_branch_found = 1;
9957 if (delete) {
9958 err = delete_backup_ref(re->ref,
9959 old_commit_id, repo);
9960 } else {
9961 err = print_backup_ref(branch_name, refname,
9962 old_commit_id, old_commit, refs_idmap,
9963 repo);
9965 if (err)
9966 break;
9969 free(old_commit_id);
9970 old_commit_id = NULL;
9971 free(branch_name);
9972 branch_name = NULL;
9973 got_object_commit_close(old_commit);
9974 old_commit = NULL;
9977 if (wanted_branch_name && !wanted_branch_found) {
9978 err = got_error_fmt(GOT_ERR_NOT_REF,
9979 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9981 done:
9982 if (refs_idmap)
9983 got_reflist_object_id_map_free(refs_idmap);
9984 got_ref_list_free(&refs);
9985 got_ref_list_free(&backup_refs);
9986 free(old_commit_id);
9987 free(branch_name);
9988 if (old_commit)
9989 got_object_commit_close(old_commit);
9990 return err;
9993 static const struct got_error *
9994 abort_progress(void *arg, unsigned char status, const char *path)
9997 * Unversioned files should not clutter progress output when
9998 * an operation is aborted.
10000 if (status == GOT_STATUS_UNVERSIONED)
10001 return NULL;
10003 return update_progress(arg, status, path);
10006 static const struct got_error *
10007 cmd_rebase(int argc, char *argv[])
10009 const struct got_error *error = NULL;
10010 struct got_worktree *worktree = NULL;
10011 struct got_repository *repo = NULL;
10012 struct got_fileindex *fileindex = NULL;
10013 char *cwd = NULL;
10014 struct got_reference *branch = NULL;
10015 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10016 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10017 struct got_object_id *resume_commit_id = NULL;
10018 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10019 struct got_commit_object *commit = NULL;
10020 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10021 int histedit_in_progress = 0, merge_in_progress = 0;
10022 int create_backup = 1, list_backups = 0, delete_backups = 0;
10023 struct got_object_id_queue commits;
10024 struct got_pathlist_head merged_paths;
10025 const struct got_object_id_queue *parent_ids;
10026 struct got_object_qid *qid, *pid;
10027 struct got_update_progress_arg upa;
10028 int *pack_fds = NULL;
10030 STAILQ_INIT(&commits);
10031 TAILQ_INIT(&merged_paths);
10032 memset(&upa, 0, sizeof(upa));
10034 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10035 switch (ch) {
10036 case 'a':
10037 abort_rebase = 1;
10038 break;
10039 case 'c':
10040 continue_rebase = 1;
10041 break;
10042 case 'l':
10043 list_backups = 1;
10044 break;
10045 case 'X':
10046 delete_backups = 1;
10047 break;
10048 default:
10049 usage_rebase();
10050 /* NOTREACHED */
10054 argc -= optind;
10055 argv += optind;
10057 #ifndef PROFILE
10058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10059 "unveil", NULL) == -1)
10060 err(1, "pledge");
10061 #endif
10062 if (list_backups) {
10063 if (abort_rebase)
10064 option_conflict('l', 'a');
10065 if (continue_rebase)
10066 option_conflict('l', 'c');
10067 if (delete_backups)
10068 option_conflict('l', 'X');
10069 if (argc != 0 && argc != 1)
10070 usage_rebase();
10071 } else if (delete_backups) {
10072 if (abort_rebase)
10073 option_conflict('X', 'a');
10074 if (continue_rebase)
10075 option_conflict('X', 'c');
10076 if (list_backups)
10077 option_conflict('l', 'X');
10078 if (argc != 0 && argc != 1)
10079 usage_rebase();
10080 } else {
10081 if (abort_rebase && continue_rebase)
10082 usage_rebase();
10083 else if (abort_rebase || continue_rebase) {
10084 if (argc != 0)
10085 usage_rebase();
10086 } else if (argc != 1)
10087 usage_rebase();
10090 cwd = getcwd(NULL, 0);
10091 if (cwd == NULL) {
10092 error = got_error_from_errno("getcwd");
10093 goto done;
10096 error = got_repo_pack_fds_open(&pack_fds);
10097 if (error != NULL)
10098 goto done;
10100 error = got_worktree_open(&worktree, cwd);
10101 if (error) {
10102 if (list_backups || delete_backups) {
10103 if (error->code != GOT_ERR_NOT_WORKTREE)
10104 goto done;
10105 } else {
10106 if (error->code == GOT_ERR_NOT_WORKTREE)
10107 error = wrap_not_worktree_error(error,
10108 "rebase", cwd);
10109 goto done;
10113 error = got_repo_open(&repo,
10114 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10115 pack_fds);
10116 if (error != NULL)
10117 goto done;
10119 error = apply_unveil(got_repo_get_path(repo), 0,
10120 worktree ? got_worktree_get_root_path(worktree) : NULL);
10121 if (error)
10122 goto done;
10124 if (list_backups || delete_backups) {
10125 error = process_backup_refs(
10126 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10127 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10128 goto done; /* nothing else to do */
10131 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10132 worktree);
10133 if (error)
10134 goto done;
10135 if (histedit_in_progress) {
10136 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10137 goto done;
10140 error = got_worktree_merge_in_progress(&merge_in_progress,
10141 worktree, repo);
10142 if (error)
10143 goto done;
10144 if (merge_in_progress) {
10145 error = got_error(GOT_ERR_MERGE_BUSY);
10146 goto done;
10149 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10150 if (error)
10151 goto done;
10153 if (abort_rebase) {
10154 if (!rebase_in_progress) {
10155 error = got_error(GOT_ERR_NOT_REBASING);
10156 goto done;
10158 error = got_worktree_rebase_continue(&resume_commit_id,
10159 &new_base_branch, &tmp_branch, &branch, &fileindex,
10160 worktree, repo);
10161 if (error)
10162 goto done;
10163 printf("Switching work tree to %s\n",
10164 got_ref_get_symref_target(new_base_branch));
10165 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10166 new_base_branch, abort_progress, &upa);
10167 if (error)
10168 goto done;
10169 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10170 print_merge_progress_stats(&upa);
10171 goto done; /* nothing else to do */
10174 if (continue_rebase) {
10175 if (!rebase_in_progress) {
10176 error = got_error(GOT_ERR_NOT_REBASING);
10177 goto done;
10179 error = got_worktree_rebase_continue(&resume_commit_id,
10180 &new_base_branch, &tmp_branch, &branch, &fileindex,
10181 worktree, repo);
10182 if (error)
10183 goto done;
10185 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10186 resume_commit_id, repo);
10187 if (error)
10188 goto done;
10190 yca_id = got_object_id_dup(resume_commit_id);
10191 if (yca_id == NULL) {
10192 error = got_error_from_errno("got_object_id_dup");
10193 goto done;
10195 } else {
10196 error = got_ref_open(&branch, repo, argv[0], 0);
10197 if (error != NULL)
10198 goto done;
10201 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10202 if (error)
10203 goto done;
10205 if (!continue_rebase) {
10206 struct got_object_id *base_commit_id;
10208 base_commit_id = got_worktree_get_base_commit_id(worktree);
10209 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10210 base_commit_id, branch_head_commit_id, 1, repo,
10211 check_cancelled, NULL);
10212 if (error)
10213 goto done;
10214 if (yca_id == NULL) {
10215 error = got_error_msg(GOT_ERR_ANCESTRY,
10216 "specified branch shares no common ancestry "
10217 "with work tree's branch");
10218 goto done;
10221 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10222 if (error) {
10223 if (error->code != GOT_ERR_ANCESTRY)
10224 goto done;
10225 error = NULL;
10226 } else {
10227 struct got_pathlist_head paths;
10228 printf("%s is already based on %s\n",
10229 got_ref_get_name(branch),
10230 got_worktree_get_head_ref_name(worktree));
10231 error = switch_head_ref(branch, branch_head_commit_id,
10232 worktree, repo);
10233 if (error)
10234 goto done;
10235 error = got_worktree_set_base_commit_id(worktree, repo,
10236 branch_head_commit_id);
10237 if (error)
10238 goto done;
10239 TAILQ_INIT(&paths);
10240 error = got_pathlist_append(&paths, "", NULL);
10241 if (error)
10242 goto done;
10243 error = got_worktree_checkout_files(worktree,
10244 &paths, repo, update_progress, &upa,
10245 check_cancelled, NULL);
10246 got_pathlist_free(&paths);
10247 if (error)
10248 goto done;
10249 if (upa.did_something) {
10250 char *id_str;
10251 error = got_object_id_str(&id_str,
10252 branch_head_commit_id);
10253 if (error)
10254 goto done;
10255 printf("Updated to %s: %s\n",
10256 got_worktree_get_head_ref_name(worktree),
10257 id_str);
10258 free(id_str);
10259 } else
10260 printf("Already up-to-date\n");
10261 print_update_progress_stats(&upa);
10262 goto done;
10266 commit_id = branch_head_commit_id;
10267 error = got_object_open_as_commit(&commit, repo, commit_id);
10268 if (error)
10269 goto done;
10271 parent_ids = got_object_commit_get_parent_ids(commit);
10272 pid = STAILQ_FIRST(parent_ids);
10273 if (pid == NULL) {
10274 error = got_error(GOT_ERR_EMPTY_REBASE);
10275 goto done;
10277 error = collect_commits(&commits, commit_id, &pid->id,
10278 yca_id, got_worktree_get_path_prefix(worktree),
10279 GOT_ERR_REBASE_PATH, repo);
10280 got_object_commit_close(commit);
10281 commit = NULL;
10282 if (error)
10283 goto done;
10285 if (!continue_rebase) {
10286 error = got_worktree_rebase_prepare(&new_base_branch,
10287 &tmp_branch, &fileindex, worktree, branch, repo);
10288 if (error)
10289 goto done;
10292 if (STAILQ_EMPTY(&commits)) {
10293 if (continue_rebase) {
10294 error = rebase_complete(worktree, fileindex,
10295 branch, new_base_branch, tmp_branch, repo,
10296 create_backup);
10297 goto done;
10298 } else {
10299 /* Fast-forward the reference of the branch. */
10300 struct got_object_id *new_head_commit_id;
10301 char *id_str;
10302 error = got_ref_resolve(&new_head_commit_id, repo,
10303 new_base_branch);
10304 if (error)
10305 goto done;
10306 error = got_object_id_str(&id_str, new_head_commit_id);
10307 printf("Forwarding %s to commit %s\n",
10308 got_ref_get_name(branch), id_str);
10309 free(id_str);
10310 error = got_ref_change_ref(branch,
10311 new_head_commit_id);
10312 if (error)
10313 goto done;
10314 /* No backup needed since objects did not change. */
10315 create_backup = 0;
10319 pid = NULL;
10320 STAILQ_FOREACH(qid, &commits, entry) {
10322 commit_id = &qid->id;
10323 parent_id = pid ? &pid->id : yca_id;
10324 pid = qid;
10326 memset(&upa, 0, sizeof(upa));
10327 error = got_worktree_rebase_merge_files(&merged_paths,
10328 worktree, fileindex, parent_id, commit_id, repo,
10329 update_progress, &upa, check_cancelled, NULL);
10330 if (error)
10331 goto done;
10333 print_merge_progress_stats(&upa);
10334 if (upa.conflicts > 0 || upa.missing > 0 ||
10335 upa.not_deleted > 0 || upa.unversioned > 0) {
10336 if (upa.conflicts > 0) {
10337 error = show_rebase_merge_conflict(&qid->id,
10338 repo);
10339 if (error)
10340 goto done;
10342 got_worktree_rebase_pathlist_free(&merged_paths);
10343 break;
10346 error = rebase_commit(&merged_paths, worktree, fileindex,
10347 tmp_branch, commit_id, repo);
10348 got_worktree_rebase_pathlist_free(&merged_paths);
10349 if (error)
10350 goto done;
10353 if (upa.conflicts > 0 || upa.missing > 0 ||
10354 upa.not_deleted > 0 || upa.unversioned > 0) {
10355 error = got_worktree_rebase_postpone(worktree, fileindex);
10356 if (error)
10357 goto done;
10358 if (upa.conflicts > 0 && upa.missing == 0 &&
10359 upa.not_deleted == 0 && upa.unversioned == 0) {
10360 error = got_error_msg(GOT_ERR_CONFLICTS,
10361 "conflicts must be resolved before rebasing "
10362 "can continue");
10363 } else if (upa.conflicts > 0) {
10364 error = got_error_msg(GOT_ERR_CONFLICTS,
10365 "conflicts must be resolved before rebasing "
10366 "can continue; changes destined for some "
10367 "files were not yet merged and should be "
10368 "merged manually if required before the "
10369 "rebase operation is continued");
10370 } else {
10371 error = got_error_msg(GOT_ERR_CONFLICTS,
10372 "changes destined for some files were not "
10373 "yet merged and should be merged manually "
10374 "if required before the rebase operation "
10375 "is continued");
10377 } else
10378 error = rebase_complete(worktree, fileindex, branch,
10379 new_base_branch, tmp_branch, repo, create_backup);
10380 done:
10381 got_object_id_queue_free(&commits);
10382 free(branch_head_commit_id);
10383 free(resume_commit_id);
10384 free(yca_id);
10385 if (commit)
10386 got_object_commit_close(commit);
10387 if (branch)
10388 got_ref_close(branch);
10389 if (new_base_branch)
10390 got_ref_close(new_base_branch);
10391 if (tmp_branch)
10392 got_ref_close(tmp_branch);
10393 if (worktree)
10394 got_worktree_close(worktree);
10395 if (repo) {
10396 const struct got_error *close_err = got_repo_close(repo);
10397 if (error == NULL)
10398 error = close_err;
10400 if (pack_fds) {
10401 const struct got_error *pack_err =
10402 got_repo_pack_fds_close(pack_fds);
10403 if (error == NULL)
10404 error = pack_err;
10406 return error;
10409 __dead static void
10410 usage_histedit(void)
10412 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10413 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10414 getprogname());
10415 exit(1);
10418 #define GOT_HISTEDIT_PICK 'p'
10419 #define GOT_HISTEDIT_EDIT 'e'
10420 #define GOT_HISTEDIT_FOLD 'f'
10421 #define GOT_HISTEDIT_DROP 'd'
10422 #define GOT_HISTEDIT_MESG 'm'
10424 static const struct got_histedit_cmd {
10425 unsigned char code;
10426 const char *name;
10427 const char *desc;
10428 } got_histedit_cmds[] = {
10429 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10430 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10431 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10432 "be used" },
10433 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10434 { GOT_HISTEDIT_MESG, "mesg",
10435 "single-line log message for commit above (open editor if empty)" },
10438 struct got_histedit_list_entry {
10439 TAILQ_ENTRY(got_histedit_list_entry) entry;
10440 struct got_object_id *commit_id;
10441 const struct got_histedit_cmd *cmd;
10442 char *logmsg;
10444 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10446 static const struct got_error *
10447 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10448 FILE *f, struct got_repository *repo)
10450 const struct got_error *err = NULL;
10451 char *logmsg = NULL, *id_str = NULL;
10452 struct got_commit_object *commit = NULL;
10453 int n;
10455 err = got_object_open_as_commit(&commit, repo, commit_id);
10456 if (err)
10457 goto done;
10459 err = get_short_logmsg(&logmsg, 34, commit);
10460 if (err)
10461 goto done;
10463 err = got_object_id_str(&id_str, commit_id);
10464 if (err)
10465 goto done;
10467 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10468 if (n < 0)
10469 err = got_ferror(f, GOT_ERR_IO);
10470 done:
10471 if (commit)
10472 got_object_commit_close(commit);
10473 free(id_str);
10474 free(logmsg);
10475 return err;
10478 static const struct got_error *
10479 histedit_write_commit_list(struct got_object_id_queue *commits,
10480 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10481 struct got_repository *repo)
10483 const struct got_error *err = NULL;
10484 struct got_object_qid *qid;
10485 const char *histedit_cmd = NULL;
10487 if (STAILQ_EMPTY(commits))
10488 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10490 STAILQ_FOREACH(qid, commits, entry) {
10491 histedit_cmd = got_histedit_cmds[0].name;
10492 if (edit_only)
10493 histedit_cmd = "edit";
10494 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10495 histedit_cmd = "fold";
10496 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10497 if (err)
10498 break;
10499 if (edit_logmsg_only) {
10500 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10501 if (n < 0) {
10502 err = got_ferror(f, GOT_ERR_IO);
10503 break;
10508 return err;
10511 static const struct got_error *
10512 write_cmd_list(FILE *f, const char *branch_name,
10513 struct got_object_id_queue *commits)
10515 const struct got_error *err = NULL;
10516 size_t i;
10517 int n;
10518 char *id_str;
10519 struct got_object_qid *qid;
10521 qid = STAILQ_FIRST(commits);
10522 err = got_object_id_str(&id_str, &qid->id);
10523 if (err)
10524 return err;
10526 n = fprintf(f,
10527 "# Editing the history of branch '%s' starting at\n"
10528 "# commit %s\n"
10529 "# Commits will be processed in order from top to "
10530 "bottom of this file.\n", branch_name, id_str);
10531 if (n < 0) {
10532 err = got_ferror(f, GOT_ERR_IO);
10533 goto done;
10536 n = fprintf(f, "# Available histedit commands:\n");
10537 if (n < 0) {
10538 err = got_ferror(f, GOT_ERR_IO);
10539 goto done;
10542 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10543 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10544 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10545 cmd->desc);
10546 if (n < 0) {
10547 err = got_ferror(f, GOT_ERR_IO);
10548 break;
10551 done:
10552 free(id_str);
10553 return err;
10556 static const struct got_error *
10557 histedit_syntax_error(int lineno)
10559 static char msg[42];
10560 int ret;
10562 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10563 lineno);
10564 if (ret == -1 || ret >= sizeof(msg))
10565 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10567 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10570 static const struct got_error *
10571 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10572 char *logmsg, struct got_repository *repo)
10574 const struct got_error *err;
10575 struct got_commit_object *folded_commit = NULL;
10576 char *id_str, *folded_logmsg = NULL;
10578 err = got_object_id_str(&id_str, hle->commit_id);
10579 if (err)
10580 return err;
10582 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10583 if (err)
10584 goto done;
10586 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10587 if (err)
10588 goto done;
10589 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10590 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10591 folded_logmsg) == -1) {
10592 err = got_error_from_errno("asprintf");
10594 done:
10595 if (folded_commit)
10596 got_object_commit_close(folded_commit);
10597 free(id_str);
10598 free(folded_logmsg);
10599 return err;
10602 static struct got_histedit_list_entry *
10603 get_folded_commits(struct got_histedit_list_entry *hle)
10605 struct got_histedit_list_entry *prev, *folded = NULL;
10607 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10608 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10609 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10610 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10611 folded = prev;
10612 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10615 return folded;
10618 static const struct got_error *
10619 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10620 struct got_repository *repo)
10622 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10623 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10624 const struct got_error *err = NULL;
10625 struct got_commit_object *commit = NULL;
10626 int logmsg_len;
10627 int fd;
10628 struct got_histedit_list_entry *folded = NULL;
10630 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10631 if (err)
10632 return err;
10634 folded = get_folded_commits(hle);
10635 if (folded) {
10636 while (folded != hle) {
10637 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10638 folded = TAILQ_NEXT(folded, entry);
10639 continue;
10641 err = append_folded_commit_msg(&new_msg, folded,
10642 logmsg, repo);
10643 if (err)
10644 goto done;
10645 free(logmsg);
10646 logmsg = new_msg;
10647 folded = TAILQ_NEXT(folded, entry);
10651 err = got_object_id_str(&id_str, hle->commit_id);
10652 if (err)
10653 goto done;
10654 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10655 if (err)
10656 goto done;
10657 logmsg_len = asprintf(&new_msg,
10658 "%s\n# original log message of commit %s: %s",
10659 logmsg ? logmsg : "", id_str, orig_logmsg);
10660 if (logmsg_len == -1) {
10661 err = got_error_from_errno("asprintf");
10662 goto done;
10664 free(logmsg);
10665 logmsg = new_msg;
10667 err = got_object_id_str(&id_str, hle->commit_id);
10668 if (err)
10669 goto done;
10671 err = got_opentemp_named_fd(&logmsg_path, &fd,
10672 GOT_TMPDIR_STR "/got-logmsg");
10673 if (err)
10674 goto done;
10676 write(fd, logmsg, logmsg_len);
10677 close(fd);
10679 err = get_editor(&editor);
10680 if (err)
10681 goto done;
10683 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10684 logmsg_len, 0);
10685 if (err) {
10686 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10687 goto done;
10688 err = NULL;
10689 hle->logmsg = strdup(new_msg);
10690 if (hle->logmsg == NULL)
10691 err = got_error_from_errno("strdup");
10693 done:
10694 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10695 err = got_error_from_errno2("unlink", logmsg_path);
10696 free(logmsg_path);
10697 free(logmsg);
10698 free(orig_logmsg);
10699 free(editor);
10700 if (commit)
10701 got_object_commit_close(commit);
10702 return err;
10705 static const struct got_error *
10706 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10707 FILE *f, struct got_repository *repo)
10709 const struct got_error *err = NULL;
10710 char *line = NULL, *p, *end;
10711 size_t i, size;
10712 ssize_t len;
10713 int lineno = 0;
10714 const struct got_histedit_cmd *cmd;
10715 struct got_object_id *commit_id = NULL;
10716 struct got_histedit_list_entry *hle = NULL;
10718 for (;;) {
10719 len = getline(&line, &size, f);
10720 if (len == -1) {
10721 const struct got_error *getline_err;
10722 if (feof(f))
10723 break;
10724 getline_err = got_error_from_errno("getline");
10725 err = got_ferror(f, getline_err->code);
10726 break;
10728 lineno++;
10729 p = line;
10730 while (isspace((unsigned char)p[0]))
10731 p++;
10732 if (p[0] == '#' || p[0] == '\0') {
10733 free(line);
10734 line = NULL;
10735 continue;
10737 cmd = NULL;
10738 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10739 cmd = &got_histedit_cmds[i];
10740 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10741 isspace((unsigned char)p[strlen(cmd->name)])) {
10742 p += strlen(cmd->name);
10743 break;
10745 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10746 p++;
10747 break;
10750 if (i == nitems(got_histedit_cmds)) {
10751 err = histedit_syntax_error(lineno);
10752 break;
10754 while (isspace((unsigned char)p[0]))
10755 p++;
10756 if (cmd->code == GOT_HISTEDIT_MESG) {
10757 if (hle == NULL || hle->logmsg != NULL) {
10758 err = got_error(GOT_ERR_HISTEDIT_CMD);
10759 break;
10761 if (p[0] == '\0') {
10762 err = histedit_edit_logmsg(hle, repo);
10763 if (err)
10764 break;
10765 } else {
10766 hle->logmsg = strdup(p);
10767 if (hle->logmsg == NULL) {
10768 err = got_error_from_errno("strdup");
10769 break;
10772 free(line);
10773 line = NULL;
10774 continue;
10775 } else {
10776 end = p;
10777 while (end[0] && !isspace((unsigned char)end[0]))
10778 end++;
10779 *end = '\0';
10781 err = got_object_resolve_id_str(&commit_id, repo, p);
10782 if (err) {
10783 /* override error code */
10784 err = histedit_syntax_error(lineno);
10785 break;
10788 hle = malloc(sizeof(*hle));
10789 if (hle == NULL) {
10790 err = got_error_from_errno("malloc");
10791 break;
10793 hle->cmd = cmd;
10794 hle->commit_id = commit_id;
10795 hle->logmsg = NULL;
10796 commit_id = NULL;
10797 free(line);
10798 line = NULL;
10799 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10802 free(line);
10803 free(commit_id);
10804 return err;
10807 static const struct got_error *
10808 histedit_check_script(struct got_histedit_list *histedit_cmds,
10809 struct got_object_id_queue *commits, struct got_repository *repo)
10811 const struct got_error *err = NULL;
10812 struct got_object_qid *qid;
10813 struct got_histedit_list_entry *hle;
10814 static char msg[92];
10815 char *id_str;
10817 if (TAILQ_EMPTY(histedit_cmds))
10818 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10819 "histedit script contains no commands");
10820 if (STAILQ_EMPTY(commits))
10821 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10823 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10824 struct got_histedit_list_entry *hle2;
10825 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10826 if (hle == hle2)
10827 continue;
10828 if (got_object_id_cmp(hle->commit_id,
10829 hle2->commit_id) != 0)
10830 continue;
10831 err = got_object_id_str(&id_str, hle->commit_id);
10832 if (err)
10833 return err;
10834 snprintf(msg, sizeof(msg), "commit %s is listed "
10835 "more than once in histedit script", id_str);
10836 free(id_str);
10837 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10841 STAILQ_FOREACH(qid, commits, entry) {
10842 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10843 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10844 break;
10846 if (hle == NULL) {
10847 err = got_object_id_str(&id_str, &qid->id);
10848 if (err)
10849 return err;
10850 snprintf(msg, sizeof(msg),
10851 "commit %s missing from histedit script", id_str);
10852 free(id_str);
10853 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10857 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10858 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10859 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10860 "last commit in histedit script cannot be folded");
10862 return NULL;
10865 static const struct got_error *
10866 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10867 const char *path, struct got_object_id_queue *commits,
10868 struct got_repository *repo)
10870 const struct got_error *err = NULL;
10871 char *editor;
10872 FILE *f = NULL;
10874 err = get_editor(&editor);
10875 if (err)
10876 return err;
10878 if (spawn_editor(editor, path) == -1) {
10879 err = got_error_from_errno("failed spawning editor");
10880 goto done;
10883 f = fopen(path, "re");
10884 if (f == NULL) {
10885 err = got_error_from_errno("fopen");
10886 goto done;
10888 err = histedit_parse_list(histedit_cmds, f, repo);
10889 if (err)
10890 goto done;
10892 err = histedit_check_script(histedit_cmds, commits, repo);
10893 done:
10894 if (f && fclose(f) == EOF && err == NULL)
10895 err = got_error_from_errno("fclose");
10896 free(editor);
10897 return err;
10900 static const struct got_error *
10901 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10902 struct got_object_id_queue *, const char *, const char *,
10903 struct got_repository *);
10905 static const struct got_error *
10906 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10907 struct got_object_id_queue *commits, const char *branch_name,
10908 int edit_logmsg_only, int fold_only, int edit_only,
10909 struct got_repository *repo)
10911 const struct got_error *err;
10912 FILE *f = NULL;
10913 char *path = NULL;
10915 err = got_opentemp_named(&path, &f, "got-histedit");
10916 if (err)
10917 return err;
10919 err = write_cmd_list(f, branch_name, commits);
10920 if (err)
10921 goto done;
10923 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10924 fold_only, edit_only, repo);
10925 if (err)
10926 goto done;
10928 if (edit_logmsg_only || fold_only || edit_only) {
10929 rewind(f);
10930 err = histedit_parse_list(histedit_cmds, f, repo);
10931 } else {
10932 if (fclose(f) == EOF) {
10933 err = got_error_from_errno("fclose");
10934 goto done;
10936 f = NULL;
10937 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10938 if (err) {
10939 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10940 err->code != GOT_ERR_HISTEDIT_CMD)
10941 goto done;
10942 err = histedit_edit_list_retry(histedit_cmds, err,
10943 commits, path, branch_name, repo);
10946 done:
10947 if (f && fclose(f) == EOF && err == NULL)
10948 err = got_error_from_errno("fclose");
10949 if (path && unlink(path) != 0 && err == NULL)
10950 err = got_error_from_errno2("unlink", path);
10951 free(path);
10952 return err;
10955 static const struct got_error *
10956 histedit_save_list(struct got_histedit_list *histedit_cmds,
10957 struct got_worktree *worktree, struct got_repository *repo)
10959 const struct got_error *err = NULL;
10960 char *path = NULL;
10961 FILE *f = NULL;
10962 struct got_histedit_list_entry *hle;
10963 struct got_commit_object *commit = NULL;
10965 err = got_worktree_get_histedit_script_path(&path, worktree);
10966 if (err)
10967 return err;
10969 f = fopen(path, "we");
10970 if (f == NULL) {
10971 err = got_error_from_errno2("fopen", path);
10972 goto done;
10974 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10975 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10976 repo);
10977 if (err)
10978 break;
10980 if (hle->logmsg) {
10981 int n = fprintf(f, "%c %s\n",
10982 GOT_HISTEDIT_MESG, hle->logmsg);
10983 if (n < 0) {
10984 err = got_ferror(f, GOT_ERR_IO);
10985 break;
10989 done:
10990 if (f && fclose(f) == EOF && err == NULL)
10991 err = got_error_from_errno("fclose");
10992 free(path);
10993 if (commit)
10994 got_object_commit_close(commit);
10995 return err;
10998 static void
10999 histedit_free_list(struct got_histedit_list *histedit_cmds)
11001 struct got_histedit_list_entry *hle;
11003 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11004 TAILQ_REMOVE(histedit_cmds, hle, entry);
11005 free(hle);
11009 static const struct got_error *
11010 histedit_load_list(struct got_histedit_list *histedit_cmds,
11011 const char *path, struct got_repository *repo)
11013 const struct got_error *err = NULL;
11014 FILE *f = NULL;
11016 f = fopen(path, "re");
11017 if (f == NULL) {
11018 err = got_error_from_errno2("fopen", path);
11019 goto done;
11022 err = histedit_parse_list(histedit_cmds, f, repo);
11023 done:
11024 if (f && fclose(f) == EOF && err == NULL)
11025 err = got_error_from_errno("fclose");
11026 return err;
11029 static const struct got_error *
11030 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11031 const struct got_error *edit_err, struct got_object_id_queue *commits,
11032 const char *path, const char *branch_name, struct got_repository *repo)
11034 const struct got_error *err = NULL, *prev_err = edit_err;
11035 int resp = ' ';
11037 while (resp != 'c' && resp != 'r' && resp != 'a') {
11038 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11039 "or (a)bort: ", getprogname(), prev_err->msg);
11040 resp = getchar();
11041 if (resp == '\n')
11042 resp = getchar();
11043 if (resp == 'c') {
11044 histedit_free_list(histedit_cmds);
11045 err = histedit_run_editor(histedit_cmds, path, commits,
11046 repo);
11047 if (err) {
11048 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11049 err->code != GOT_ERR_HISTEDIT_CMD)
11050 break;
11051 prev_err = err;
11052 resp = ' ';
11053 continue;
11055 break;
11056 } else if (resp == 'r') {
11057 histedit_free_list(histedit_cmds);
11058 err = histedit_edit_script(histedit_cmds,
11059 commits, branch_name, 0, 0, 0, repo);
11060 if (err) {
11061 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11062 err->code != GOT_ERR_HISTEDIT_CMD)
11063 break;
11064 prev_err = err;
11065 resp = ' ';
11066 continue;
11068 break;
11069 } else if (resp == 'a') {
11070 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11071 break;
11072 } else
11073 printf("invalid response '%c'\n", resp);
11076 return err;
11079 static const struct got_error *
11080 histedit_complete(struct got_worktree *worktree,
11081 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11082 struct got_reference *branch, struct got_repository *repo)
11084 printf("Switching work tree to %s\n",
11085 got_ref_get_symref_target(branch));
11086 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11087 branch, repo);
11090 static const struct got_error *
11091 show_histedit_progress(struct got_commit_object *commit,
11092 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11094 const struct got_error *err;
11095 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11097 err = got_object_id_str(&old_id_str, hle->commit_id);
11098 if (err)
11099 goto done;
11101 if (new_id) {
11102 err = got_object_id_str(&new_id_str, new_id);
11103 if (err)
11104 goto done;
11107 old_id_str[12] = '\0';
11108 if (new_id_str)
11109 new_id_str[12] = '\0';
11111 if (hle->logmsg) {
11112 logmsg = strdup(hle->logmsg);
11113 if (logmsg == NULL) {
11114 err = got_error_from_errno("strdup");
11115 goto done;
11117 trim_logmsg(logmsg, 42);
11118 } else {
11119 err = get_short_logmsg(&logmsg, 42, commit);
11120 if (err)
11121 goto done;
11124 switch (hle->cmd->code) {
11125 case GOT_HISTEDIT_PICK:
11126 case GOT_HISTEDIT_EDIT:
11127 printf("%s -> %s: %s\n", old_id_str,
11128 new_id_str ? new_id_str : "no-op change", logmsg);
11129 break;
11130 case GOT_HISTEDIT_DROP:
11131 case GOT_HISTEDIT_FOLD:
11132 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11133 logmsg);
11134 break;
11135 default:
11136 break;
11138 done:
11139 free(old_id_str);
11140 free(new_id_str);
11141 return err;
11144 static const struct got_error *
11145 histedit_commit(struct got_pathlist_head *merged_paths,
11146 struct got_worktree *worktree, struct got_fileindex *fileindex,
11147 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11148 struct got_repository *repo)
11150 const struct got_error *err;
11151 struct got_commit_object *commit;
11152 struct got_object_id *new_commit_id;
11154 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11155 && hle->logmsg == NULL) {
11156 err = histedit_edit_logmsg(hle, repo);
11157 if (err)
11158 return err;
11161 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11162 if (err)
11163 return err;
11165 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11166 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11167 hle->logmsg, repo);
11168 if (err) {
11169 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11170 goto done;
11171 err = show_histedit_progress(commit, hle, NULL);
11172 } else {
11173 err = show_histedit_progress(commit, hle, new_commit_id);
11174 free(new_commit_id);
11176 done:
11177 got_object_commit_close(commit);
11178 return err;
11181 static const struct got_error *
11182 histedit_skip_commit(struct got_histedit_list_entry *hle,
11183 struct got_worktree *worktree, struct got_repository *repo)
11185 const struct got_error *error;
11186 struct got_commit_object *commit;
11188 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11189 repo);
11190 if (error)
11191 return error;
11193 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11194 if (error)
11195 return error;
11197 error = show_histedit_progress(commit, hle, NULL);
11198 got_object_commit_close(commit);
11199 return error;
11202 static const struct got_error *
11203 check_local_changes(void *arg, unsigned char status,
11204 unsigned char staged_status, const char *path,
11205 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11206 struct got_object_id *commit_id, int dirfd, const char *de_name)
11208 int *have_local_changes = arg;
11210 switch (status) {
11211 case GOT_STATUS_ADD:
11212 case GOT_STATUS_DELETE:
11213 case GOT_STATUS_MODIFY:
11214 case GOT_STATUS_CONFLICT:
11215 *have_local_changes = 1;
11216 return got_error(GOT_ERR_CANCELLED);
11217 default:
11218 break;
11221 switch (staged_status) {
11222 case GOT_STATUS_ADD:
11223 case GOT_STATUS_DELETE:
11224 case GOT_STATUS_MODIFY:
11225 *have_local_changes = 1;
11226 return got_error(GOT_ERR_CANCELLED);
11227 default:
11228 break;
11231 return NULL;
11234 static const struct got_error *
11235 cmd_histedit(int argc, char *argv[])
11237 const struct got_error *error = NULL;
11238 struct got_worktree *worktree = NULL;
11239 struct got_fileindex *fileindex = NULL;
11240 struct got_repository *repo = NULL;
11241 char *cwd = NULL;
11242 struct got_reference *branch = NULL;
11243 struct got_reference *tmp_branch = NULL;
11244 struct got_object_id *resume_commit_id = NULL;
11245 struct got_object_id *base_commit_id = NULL;
11246 struct got_object_id *head_commit_id = NULL;
11247 struct got_commit_object *commit = NULL;
11248 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11249 struct got_update_progress_arg upa;
11250 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11251 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11252 int list_backups = 0, delete_backups = 0;
11253 const char *edit_script_path = NULL;
11254 struct got_object_id_queue commits;
11255 struct got_pathlist_head merged_paths;
11256 const struct got_object_id_queue *parent_ids;
11257 struct got_object_qid *pid;
11258 struct got_histedit_list histedit_cmds;
11259 struct got_histedit_list_entry *hle;
11260 int *pack_fds = NULL;
11262 STAILQ_INIT(&commits);
11263 TAILQ_INIT(&histedit_cmds);
11264 TAILQ_INIT(&merged_paths);
11265 memset(&upa, 0, sizeof(upa));
11267 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11268 switch (ch) {
11269 case 'a':
11270 abort_edit = 1;
11271 break;
11272 case 'c':
11273 continue_edit = 1;
11274 break;
11275 case 'e':
11276 edit_only = 1;
11277 break;
11278 case 'f':
11279 fold_only = 1;
11280 break;
11281 case 'F':
11282 edit_script_path = optarg;
11283 break;
11284 case 'm':
11285 edit_logmsg_only = 1;
11286 break;
11287 case 'l':
11288 list_backups = 1;
11289 break;
11290 case 'X':
11291 delete_backups = 1;
11292 break;
11293 default:
11294 usage_histedit();
11295 /* NOTREACHED */
11299 argc -= optind;
11300 argv += optind;
11302 #ifndef PROFILE
11303 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11304 "unveil", NULL) == -1)
11305 err(1, "pledge");
11306 #endif
11307 if (abort_edit && continue_edit)
11308 option_conflict('a', 'c');
11309 if (edit_script_path && edit_logmsg_only)
11310 option_conflict('F', 'm');
11311 if (abort_edit && edit_logmsg_only)
11312 option_conflict('a', 'm');
11313 if (continue_edit && edit_logmsg_only)
11314 option_conflict('c', 'm');
11315 if (abort_edit && fold_only)
11316 option_conflict('a', 'f');
11317 if (continue_edit && fold_only)
11318 option_conflict('c', 'f');
11319 if (fold_only && edit_logmsg_only)
11320 option_conflict('f', 'm');
11321 if (edit_script_path && fold_only)
11322 option_conflict('F', 'f');
11323 if (abort_edit && edit_only)
11324 option_conflict('a', 'e');
11325 if (continue_edit && edit_only)
11326 option_conflict('c', 'e');
11327 if (edit_only && edit_logmsg_only)
11328 option_conflict('e', 'm');
11329 if (edit_script_path && edit_only)
11330 option_conflict('F', 'e');
11331 if (list_backups) {
11332 if (abort_edit)
11333 option_conflict('l', 'a');
11334 if (continue_edit)
11335 option_conflict('l', 'c');
11336 if (edit_script_path)
11337 option_conflict('l', 'F');
11338 if (edit_logmsg_only)
11339 option_conflict('l', 'm');
11340 if (fold_only)
11341 option_conflict('l', 'f');
11342 if (edit_only)
11343 option_conflict('l', 'e');
11344 if (delete_backups)
11345 option_conflict('l', 'X');
11346 if (argc != 0 && argc != 1)
11347 usage_histedit();
11348 } else if (delete_backups) {
11349 if (abort_edit)
11350 option_conflict('X', 'a');
11351 if (continue_edit)
11352 option_conflict('X', 'c');
11353 if (edit_script_path)
11354 option_conflict('X', 'F');
11355 if (edit_logmsg_only)
11356 option_conflict('X', 'm');
11357 if (fold_only)
11358 option_conflict('X', 'f');
11359 if (edit_only)
11360 option_conflict('X', 'e');
11361 if (list_backups)
11362 option_conflict('X', 'l');
11363 if (argc != 0 && argc != 1)
11364 usage_histedit();
11365 } else if (argc != 0)
11366 usage_histedit();
11369 * This command cannot apply unveil(2) in all cases because the
11370 * user may choose to run an editor to edit the histedit script
11371 * and to edit individual commit log messages.
11372 * unveil(2) traverses exec(2); if an editor is used we have to
11373 * apply unveil after edit script and log messages have been written.
11374 * XXX TODO: Make use of unveil(2) where possible.
11377 cwd = getcwd(NULL, 0);
11378 if (cwd == NULL) {
11379 error = got_error_from_errno("getcwd");
11380 goto done;
11383 error = got_repo_pack_fds_open(&pack_fds);
11384 if (error != NULL)
11385 goto done;
11387 error = got_worktree_open(&worktree, cwd);
11388 if (error) {
11389 if (list_backups || delete_backups) {
11390 if (error->code != GOT_ERR_NOT_WORKTREE)
11391 goto done;
11392 } else {
11393 if (error->code == GOT_ERR_NOT_WORKTREE)
11394 error = wrap_not_worktree_error(error,
11395 "histedit", cwd);
11396 goto done;
11400 if (list_backups || delete_backups) {
11401 error = got_repo_open(&repo,
11402 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11403 NULL, pack_fds);
11404 if (error != NULL)
11405 goto done;
11406 error = apply_unveil(got_repo_get_path(repo), 0,
11407 worktree ? got_worktree_get_root_path(worktree) : NULL);
11408 if (error)
11409 goto done;
11410 error = process_backup_refs(
11411 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11412 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11413 goto done; /* nothing else to do */
11416 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11417 NULL, pack_fds);
11418 if (error != NULL)
11419 goto done;
11421 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11422 if (error)
11423 goto done;
11424 if (rebase_in_progress) {
11425 error = got_error(GOT_ERR_REBASING);
11426 goto done;
11429 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11430 repo);
11431 if (error)
11432 goto done;
11433 if (merge_in_progress) {
11434 error = got_error(GOT_ERR_MERGE_BUSY);
11435 goto done;
11438 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11439 if (error)
11440 goto done;
11442 if (edit_in_progress && edit_logmsg_only) {
11443 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11444 "histedit operation is in progress in this "
11445 "work tree and must be continued or aborted "
11446 "before the -m option can be used");
11447 goto done;
11449 if (edit_in_progress && fold_only) {
11450 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11451 "histedit operation is in progress in this "
11452 "work tree and must be continued or aborted "
11453 "before the -f option can be used");
11454 goto done;
11456 if (edit_in_progress && edit_only) {
11457 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11458 "histedit operation is in progress in this "
11459 "work tree and must be continued or aborted "
11460 "before the -e option can be used");
11461 goto done;
11464 if (edit_in_progress && abort_edit) {
11465 error = got_worktree_histedit_continue(&resume_commit_id,
11466 &tmp_branch, &branch, &base_commit_id, &fileindex,
11467 worktree, repo);
11468 if (error)
11469 goto done;
11470 printf("Switching work tree to %s\n",
11471 got_ref_get_symref_target(branch));
11472 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11473 branch, base_commit_id, abort_progress, &upa);
11474 if (error)
11475 goto done;
11476 printf("Histedit of %s aborted\n",
11477 got_ref_get_symref_target(branch));
11478 print_merge_progress_stats(&upa);
11479 goto done; /* nothing else to do */
11480 } else if (abort_edit) {
11481 error = got_error(GOT_ERR_NOT_HISTEDIT);
11482 goto done;
11485 if (continue_edit) {
11486 char *path;
11488 if (!edit_in_progress) {
11489 error = got_error(GOT_ERR_NOT_HISTEDIT);
11490 goto done;
11493 error = got_worktree_get_histedit_script_path(&path, worktree);
11494 if (error)
11495 goto done;
11497 error = histedit_load_list(&histedit_cmds, path, repo);
11498 free(path);
11499 if (error)
11500 goto done;
11502 error = got_worktree_histedit_continue(&resume_commit_id,
11503 &tmp_branch, &branch, &base_commit_id, &fileindex,
11504 worktree, repo);
11505 if (error)
11506 goto done;
11508 error = got_ref_resolve(&head_commit_id, repo, branch);
11509 if (error)
11510 goto done;
11512 error = got_object_open_as_commit(&commit, repo,
11513 head_commit_id);
11514 if (error)
11515 goto done;
11516 parent_ids = got_object_commit_get_parent_ids(commit);
11517 pid = STAILQ_FIRST(parent_ids);
11518 if (pid == NULL) {
11519 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11520 goto done;
11522 error = collect_commits(&commits, head_commit_id, &pid->id,
11523 base_commit_id, got_worktree_get_path_prefix(worktree),
11524 GOT_ERR_HISTEDIT_PATH, repo);
11525 got_object_commit_close(commit);
11526 commit = NULL;
11527 if (error)
11528 goto done;
11529 } else {
11530 if (edit_in_progress) {
11531 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11532 goto done;
11535 error = got_ref_open(&branch, repo,
11536 got_worktree_get_head_ref_name(worktree), 0);
11537 if (error != NULL)
11538 goto done;
11540 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11541 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11542 "will not edit commit history of a branch outside "
11543 "the \"refs/heads/\" reference namespace");
11544 goto done;
11547 error = got_ref_resolve(&head_commit_id, repo, branch);
11548 got_ref_close(branch);
11549 branch = NULL;
11550 if (error)
11551 goto done;
11553 error = got_object_open_as_commit(&commit, repo,
11554 head_commit_id);
11555 if (error)
11556 goto done;
11557 parent_ids = got_object_commit_get_parent_ids(commit);
11558 pid = STAILQ_FIRST(parent_ids);
11559 if (pid == NULL) {
11560 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11561 goto done;
11563 error = collect_commits(&commits, head_commit_id, &pid->id,
11564 got_worktree_get_base_commit_id(worktree),
11565 got_worktree_get_path_prefix(worktree),
11566 GOT_ERR_HISTEDIT_PATH, repo);
11567 got_object_commit_close(commit);
11568 commit = NULL;
11569 if (error)
11570 goto done;
11572 if (STAILQ_EMPTY(&commits)) {
11573 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11574 goto done;
11577 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11578 &base_commit_id, &fileindex, worktree, repo);
11579 if (error)
11580 goto done;
11582 if (edit_script_path) {
11583 error = histedit_load_list(&histedit_cmds,
11584 edit_script_path, repo);
11585 if (error) {
11586 got_worktree_histedit_abort(worktree, fileindex,
11587 repo, branch, base_commit_id,
11588 abort_progress, &upa);
11589 print_merge_progress_stats(&upa);
11590 goto done;
11592 } else {
11593 const char *branch_name;
11594 branch_name = got_ref_get_symref_target(branch);
11595 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11596 branch_name += 11;
11597 error = histedit_edit_script(&histedit_cmds, &commits,
11598 branch_name, edit_logmsg_only, fold_only,
11599 edit_only, repo);
11600 if (error) {
11601 got_worktree_histedit_abort(worktree, fileindex,
11602 repo, branch, base_commit_id,
11603 abort_progress, &upa);
11604 print_merge_progress_stats(&upa);
11605 goto done;
11610 error = histedit_save_list(&histedit_cmds, worktree,
11611 repo);
11612 if (error) {
11613 got_worktree_histedit_abort(worktree, fileindex,
11614 repo, branch, base_commit_id,
11615 abort_progress, &upa);
11616 print_merge_progress_stats(&upa);
11617 goto done;
11622 error = histedit_check_script(&histedit_cmds, &commits, repo);
11623 if (error)
11624 goto done;
11626 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11627 if (resume_commit_id) {
11628 if (got_object_id_cmp(hle->commit_id,
11629 resume_commit_id) != 0)
11630 continue;
11632 resume_commit_id = NULL;
11633 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11634 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11635 error = histedit_skip_commit(hle, worktree,
11636 repo);
11637 if (error)
11638 goto done;
11639 } else {
11640 struct got_pathlist_head paths;
11641 int have_changes = 0;
11643 TAILQ_INIT(&paths);
11644 error = got_pathlist_append(&paths, "", NULL);
11645 if (error)
11646 goto done;
11647 error = got_worktree_status(worktree, &paths,
11648 repo, 0, check_local_changes, &have_changes,
11649 check_cancelled, NULL);
11650 got_pathlist_free(&paths);
11651 if (error) {
11652 if (error->code != GOT_ERR_CANCELLED)
11653 goto done;
11654 if (sigint_received || sigpipe_received)
11655 goto done;
11657 if (have_changes) {
11658 error = histedit_commit(NULL, worktree,
11659 fileindex, tmp_branch, hle, repo);
11660 if (error)
11661 goto done;
11662 } else {
11663 error = got_object_open_as_commit(
11664 &commit, repo, hle->commit_id);
11665 if (error)
11666 goto done;
11667 error = show_histedit_progress(commit,
11668 hle, NULL);
11669 got_object_commit_close(commit);
11670 commit = NULL;
11671 if (error)
11672 goto done;
11675 continue;
11678 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11679 error = histedit_skip_commit(hle, worktree, repo);
11680 if (error)
11681 goto done;
11682 continue;
11685 error = got_object_open_as_commit(&commit, repo,
11686 hle->commit_id);
11687 if (error)
11688 goto done;
11689 parent_ids = got_object_commit_get_parent_ids(commit);
11690 pid = STAILQ_FIRST(parent_ids);
11692 error = got_worktree_histedit_merge_files(&merged_paths,
11693 worktree, fileindex, &pid->id, hle->commit_id, repo,
11694 update_progress, &upa, check_cancelled, NULL);
11695 if (error)
11696 goto done;
11697 got_object_commit_close(commit);
11698 commit = NULL;
11700 print_merge_progress_stats(&upa);
11701 if (upa.conflicts > 0 || upa.missing > 0 ||
11702 upa.not_deleted > 0 || upa.unversioned > 0) {
11703 if (upa.conflicts > 0) {
11704 error = show_rebase_merge_conflict(
11705 hle->commit_id, repo);
11706 if (error)
11707 goto done;
11709 got_worktree_rebase_pathlist_free(&merged_paths);
11710 break;
11713 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11714 char *id_str;
11715 error = got_object_id_str(&id_str, hle->commit_id);
11716 if (error)
11717 goto done;
11718 printf("Stopping histedit for amending commit %s\n",
11719 id_str);
11720 free(id_str);
11721 got_worktree_rebase_pathlist_free(&merged_paths);
11722 error = got_worktree_histedit_postpone(worktree,
11723 fileindex);
11724 goto done;
11727 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11728 error = histedit_skip_commit(hle, worktree, repo);
11729 if (error)
11730 goto done;
11731 continue;
11734 error = histedit_commit(&merged_paths, worktree, fileindex,
11735 tmp_branch, hle, repo);
11736 got_worktree_rebase_pathlist_free(&merged_paths);
11737 if (error)
11738 goto done;
11741 if (upa.conflicts > 0 || upa.missing > 0 ||
11742 upa.not_deleted > 0 || upa.unversioned > 0) {
11743 error = got_worktree_histedit_postpone(worktree, fileindex);
11744 if (error)
11745 goto done;
11746 if (upa.conflicts > 0 && upa.missing == 0 &&
11747 upa.not_deleted == 0 && upa.unversioned == 0) {
11748 error = got_error_msg(GOT_ERR_CONFLICTS,
11749 "conflicts must be resolved before histedit "
11750 "can continue");
11751 } else if (upa.conflicts > 0) {
11752 error = got_error_msg(GOT_ERR_CONFLICTS,
11753 "conflicts must be resolved before histedit "
11754 "can continue; changes destined for some "
11755 "files were not yet merged and should be "
11756 "merged manually if required before the "
11757 "histedit operation is continued");
11758 } else {
11759 error = got_error_msg(GOT_ERR_CONFLICTS,
11760 "changes destined for some files were not "
11761 "yet merged and should be merged manually "
11762 "if required before the histedit operation "
11763 "is continued");
11765 } else
11766 error = histedit_complete(worktree, fileindex, tmp_branch,
11767 branch, repo);
11768 done:
11769 got_object_id_queue_free(&commits);
11770 histedit_free_list(&histedit_cmds);
11771 free(head_commit_id);
11772 free(base_commit_id);
11773 free(resume_commit_id);
11774 if (commit)
11775 got_object_commit_close(commit);
11776 if (branch)
11777 got_ref_close(branch);
11778 if (tmp_branch)
11779 got_ref_close(tmp_branch);
11780 if (worktree)
11781 got_worktree_close(worktree);
11782 if (repo) {
11783 const struct got_error *close_err = got_repo_close(repo);
11784 if (error == NULL)
11785 error = close_err;
11787 if (pack_fds) {
11788 const struct got_error *pack_err =
11789 got_repo_pack_fds_close(pack_fds);
11790 if (error == NULL)
11791 error = pack_err;
11793 return error;
11796 __dead static void
11797 usage_integrate(void)
11799 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11800 exit(1);
11803 static const struct got_error *
11804 cmd_integrate(int argc, char *argv[])
11806 const struct got_error *error = NULL;
11807 struct got_repository *repo = NULL;
11808 struct got_worktree *worktree = NULL;
11809 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11810 const char *branch_arg = NULL;
11811 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11812 struct got_fileindex *fileindex = NULL;
11813 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11814 int ch;
11815 struct got_update_progress_arg upa;
11816 int *pack_fds = NULL;
11818 while ((ch = getopt(argc, argv, "")) != -1) {
11819 switch (ch) {
11820 default:
11821 usage_integrate();
11822 /* NOTREACHED */
11826 argc -= optind;
11827 argv += optind;
11829 if (argc != 1)
11830 usage_integrate();
11831 branch_arg = argv[0];
11832 #ifndef PROFILE
11833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11834 "unveil", NULL) == -1)
11835 err(1, "pledge");
11836 #endif
11837 cwd = getcwd(NULL, 0);
11838 if (cwd == NULL) {
11839 error = got_error_from_errno("getcwd");
11840 goto done;
11843 error = got_repo_pack_fds_open(&pack_fds);
11844 if (error != NULL)
11845 goto done;
11847 error = got_worktree_open(&worktree, cwd);
11848 if (error) {
11849 if (error->code == GOT_ERR_NOT_WORKTREE)
11850 error = wrap_not_worktree_error(error, "integrate",
11851 cwd);
11852 goto done;
11855 error = check_rebase_or_histedit_in_progress(worktree);
11856 if (error)
11857 goto done;
11859 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11860 NULL, pack_fds);
11861 if (error != NULL)
11862 goto done;
11864 error = apply_unveil(got_repo_get_path(repo), 0,
11865 got_worktree_get_root_path(worktree));
11866 if (error)
11867 goto done;
11869 error = check_merge_in_progress(worktree, repo);
11870 if (error)
11871 goto done;
11873 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11874 error = got_error_from_errno("asprintf");
11875 goto done;
11878 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11879 &base_branch_ref, worktree, refname, repo);
11880 if (error)
11881 goto done;
11883 refname = strdup(got_ref_get_name(branch_ref));
11884 if (refname == NULL) {
11885 error = got_error_from_errno("strdup");
11886 got_worktree_integrate_abort(worktree, fileindex, repo,
11887 branch_ref, base_branch_ref);
11888 goto done;
11890 base_refname = strdup(got_ref_get_name(base_branch_ref));
11891 if (base_refname == NULL) {
11892 error = got_error_from_errno("strdup");
11893 got_worktree_integrate_abort(worktree, fileindex, repo,
11894 branch_ref, base_branch_ref);
11895 goto done;
11898 error = got_ref_resolve(&commit_id, repo, branch_ref);
11899 if (error)
11900 goto done;
11902 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11903 if (error)
11904 goto done;
11906 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11907 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11908 "specified branch has already been integrated");
11909 got_worktree_integrate_abort(worktree, fileindex, repo,
11910 branch_ref, base_branch_ref);
11911 goto done;
11914 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11915 if (error) {
11916 if (error->code == GOT_ERR_ANCESTRY)
11917 error = got_error(GOT_ERR_REBASE_REQUIRED);
11918 got_worktree_integrate_abort(worktree, fileindex, repo,
11919 branch_ref, base_branch_ref);
11920 goto done;
11923 memset(&upa, 0, sizeof(upa));
11924 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11925 branch_ref, base_branch_ref, update_progress, &upa,
11926 check_cancelled, NULL);
11927 if (error)
11928 goto done;
11930 printf("Integrated %s into %s\n", refname, base_refname);
11931 print_update_progress_stats(&upa);
11932 done:
11933 if (repo) {
11934 const struct got_error *close_err = got_repo_close(repo);
11935 if (error == NULL)
11936 error = close_err;
11938 if (worktree)
11939 got_worktree_close(worktree);
11940 if (pack_fds) {
11941 const struct got_error *pack_err =
11942 got_repo_pack_fds_close(pack_fds);
11943 if (error == NULL)
11944 error = pack_err;
11946 free(cwd);
11947 free(base_commit_id);
11948 free(commit_id);
11949 free(refname);
11950 free(base_refname);
11951 return error;
11954 __dead static void
11955 usage_merge(void)
11957 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11958 getprogname());
11959 exit(1);
11962 static const struct got_error *
11963 cmd_merge(int argc, char *argv[])
11965 const struct got_error *error = NULL;
11966 struct got_worktree *worktree = NULL;
11967 struct got_repository *repo = NULL;
11968 struct got_fileindex *fileindex = NULL;
11969 char *cwd = NULL, *id_str = NULL, *author = NULL;
11970 struct got_reference *branch = NULL, *wt_branch = NULL;
11971 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11972 struct got_object_id *wt_branch_tip = NULL;
11973 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11974 int interrupt_merge = 0;
11975 struct got_update_progress_arg upa;
11976 struct got_object_id *merge_commit_id = NULL;
11977 char *branch_name = NULL;
11978 int *pack_fds = NULL;
11980 memset(&upa, 0, sizeof(upa));
11982 while ((ch = getopt(argc, argv, "acn")) != -1) {
11983 switch (ch) {
11984 case 'a':
11985 abort_merge = 1;
11986 break;
11987 case 'c':
11988 continue_merge = 1;
11989 break;
11990 case 'n':
11991 interrupt_merge = 1;
11992 break;
11993 default:
11994 usage_rebase();
11995 /* NOTREACHED */
11999 argc -= optind;
12000 argv += optind;
12002 #ifndef PROFILE
12003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12004 "unveil", NULL) == -1)
12005 err(1, "pledge");
12006 #endif
12008 if (abort_merge && continue_merge)
12009 option_conflict('a', 'c');
12010 if (abort_merge || continue_merge) {
12011 if (argc != 0)
12012 usage_merge();
12013 } else if (argc != 1)
12014 usage_merge();
12016 cwd = getcwd(NULL, 0);
12017 if (cwd == NULL) {
12018 error = got_error_from_errno("getcwd");
12019 goto done;
12022 error = got_repo_pack_fds_open(&pack_fds);
12023 if (error != NULL)
12024 goto done;
12026 error = got_worktree_open(&worktree, cwd);
12027 if (error) {
12028 if (error->code == GOT_ERR_NOT_WORKTREE)
12029 error = wrap_not_worktree_error(error,
12030 "merge", cwd);
12031 goto done;
12034 error = got_repo_open(&repo,
12035 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12036 pack_fds);
12037 if (error != NULL)
12038 goto done;
12040 error = apply_unveil(got_repo_get_path(repo), 0,
12041 worktree ? got_worktree_get_root_path(worktree) : NULL);
12042 if (error)
12043 goto done;
12045 error = check_rebase_or_histedit_in_progress(worktree);
12046 if (error)
12047 goto done;
12049 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12050 repo);
12051 if (error)
12052 goto done;
12054 if (abort_merge) {
12055 if (!merge_in_progress) {
12056 error = got_error(GOT_ERR_NOT_MERGING);
12057 goto done;
12059 error = got_worktree_merge_continue(&branch_name,
12060 &branch_tip, &fileindex, worktree, repo);
12061 if (error)
12062 goto done;
12063 error = got_worktree_merge_abort(worktree, fileindex, repo,
12064 abort_progress, &upa);
12065 if (error)
12066 goto done;
12067 printf("Merge of %s aborted\n", branch_name);
12068 goto done; /* nothing else to do */
12071 error = get_author(&author, repo, worktree);
12072 if (error)
12073 goto done;
12075 if (continue_merge) {
12076 if (!merge_in_progress) {
12077 error = got_error(GOT_ERR_NOT_MERGING);
12078 goto done;
12080 error = got_worktree_merge_continue(&branch_name,
12081 &branch_tip, &fileindex, worktree, repo);
12082 if (error)
12083 goto done;
12084 } else {
12085 error = got_ref_open(&branch, repo, argv[0], 0);
12086 if (error != NULL)
12087 goto done;
12088 branch_name = strdup(got_ref_get_name(branch));
12089 if (branch_name == NULL) {
12090 error = got_error_from_errno("strdup");
12091 goto done;
12093 error = got_ref_resolve(&branch_tip, repo, branch);
12094 if (error)
12095 goto done;
12098 error = got_ref_open(&wt_branch, repo,
12099 got_worktree_get_head_ref_name(worktree), 0);
12100 if (error)
12101 goto done;
12102 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12103 if (error)
12104 goto done;
12105 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12106 wt_branch_tip, branch_tip, 0, repo,
12107 check_cancelled, NULL);
12108 if (error && error->code != GOT_ERR_ANCESTRY)
12109 goto done;
12111 if (!continue_merge) {
12112 error = check_path_prefix(wt_branch_tip, branch_tip,
12113 got_worktree_get_path_prefix(worktree),
12114 GOT_ERR_MERGE_PATH, repo);
12115 if (error)
12116 goto done;
12117 if (yca_id) {
12118 error = check_same_branch(wt_branch_tip, branch,
12119 yca_id, repo);
12120 if (error) {
12121 if (error->code != GOT_ERR_ANCESTRY)
12122 goto done;
12123 error = NULL;
12124 } else {
12125 static char msg[512];
12126 snprintf(msg, sizeof(msg),
12127 "cannot create a merge commit because "
12128 "%s is based on %s; %s can be integrated "
12129 "with 'got integrate' instead", branch_name,
12130 got_worktree_get_head_ref_name(worktree),
12131 branch_name);
12132 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12133 goto done;
12136 error = got_worktree_merge_prepare(&fileindex, worktree,
12137 branch, repo);
12138 if (error)
12139 goto done;
12141 error = got_worktree_merge_branch(worktree, fileindex,
12142 yca_id, branch_tip, repo, update_progress, &upa,
12143 check_cancelled, NULL);
12144 if (error)
12145 goto done;
12146 print_merge_progress_stats(&upa);
12147 if (!upa.did_something) {
12148 error = got_worktree_merge_abort(worktree, fileindex,
12149 repo, abort_progress, &upa);
12150 if (error)
12151 goto done;
12152 printf("Already up-to-date\n");
12153 goto done;
12157 if (interrupt_merge) {
12158 error = got_worktree_merge_postpone(worktree, fileindex);
12159 if (error)
12160 goto done;
12161 printf("Merge of %s interrupted on request\n", branch_name);
12162 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12163 upa.not_deleted > 0 || upa.unversioned > 0) {
12164 error = got_worktree_merge_postpone(worktree, fileindex);
12165 if (error)
12166 goto done;
12167 if (upa.conflicts > 0 && upa.missing == 0 &&
12168 upa.not_deleted == 0 && upa.unversioned == 0) {
12169 error = got_error_msg(GOT_ERR_CONFLICTS,
12170 "conflicts must be resolved before merging "
12171 "can continue");
12172 } else if (upa.conflicts > 0) {
12173 error = got_error_msg(GOT_ERR_CONFLICTS,
12174 "conflicts must be resolved before merging "
12175 "can continue; changes destined for some "
12176 "files were not yet merged and "
12177 "should be merged manually if required before the "
12178 "merge operation is continued");
12179 } else {
12180 error = got_error_msg(GOT_ERR_CONFLICTS,
12181 "changes destined for some "
12182 "files were not yet merged and should be "
12183 "merged manually if required before the "
12184 "merge operation is continued");
12186 goto done;
12187 } else {
12188 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12189 fileindex, author, NULL, 1, branch_tip, branch_name,
12190 repo, continue_merge ? print_status : NULL, NULL);
12191 if (error)
12192 goto done;
12193 error = got_worktree_merge_complete(worktree, fileindex, repo);
12194 if (error)
12195 goto done;
12196 error = got_object_id_str(&id_str, merge_commit_id);
12197 if (error)
12198 goto done;
12199 printf("Merged %s into %s: %s\n", branch_name,
12200 got_worktree_get_head_ref_name(worktree),
12201 id_str);
12204 done:
12205 free(id_str);
12206 free(merge_commit_id);
12207 free(author);
12208 free(branch_tip);
12209 free(branch_name);
12210 free(yca_id);
12211 if (branch)
12212 got_ref_close(branch);
12213 if (wt_branch)
12214 got_ref_close(wt_branch);
12215 if (worktree)
12216 got_worktree_close(worktree);
12217 if (repo) {
12218 const struct got_error *close_err = got_repo_close(repo);
12219 if (error == NULL)
12220 error = close_err;
12222 if (pack_fds) {
12223 const struct got_error *pack_err =
12224 got_repo_pack_fds_close(pack_fds);
12225 if (error == NULL)
12226 error = pack_err;
12228 return error;
12231 __dead static void
12232 usage_stage(void)
12234 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12235 "[-S] [file-path ...]\n",
12236 getprogname());
12237 exit(1);
12240 static const struct got_error *
12241 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12242 const char *path, struct got_object_id *blob_id,
12243 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12244 int dirfd, const char *de_name)
12246 const struct got_error *err = NULL;
12247 char *id_str = NULL;
12249 if (staged_status != GOT_STATUS_ADD &&
12250 staged_status != GOT_STATUS_MODIFY &&
12251 staged_status != GOT_STATUS_DELETE)
12252 return NULL;
12254 if (staged_status == GOT_STATUS_ADD ||
12255 staged_status == GOT_STATUS_MODIFY)
12256 err = got_object_id_str(&id_str, staged_blob_id);
12257 else
12258 err = got_object_id_str(&id_str, blob_id);
12259 if (err)
12260 return err;
12262 printf("%s %c %s\n", id_str, staged_status, path);
12263 free(id_str);
12264 return NULL;
12267 static const struct got_error *
12268 cmd_stage(int argc, char *argv[])
12270 const struct got_error *error = NULL;
12271 struct got_repository *repo = NULL;
12272 struct got_worktree *worktree = NULL;
12273 char *cwd = NULL;
12274 struct got_pathlist_head paths;
12275 struct got_pathlist_entry *pe;
12276 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12277 FILE *patch_script_file = NULL;
12278 const char *patch_script_path = NULL;
12279 struct choose_patch_arg cpa;
12280 int *pack_fds = NULL;
12282 TAILQ_INIT(&paths);
12284 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12285 switch (ch) {
12286 case 'l':
12287 list_stage = 1;
12288 break;
12289 case 'p':
12290 pflag = 1;
12291 break;
12292 case 'F':
12293 patch_script_path = optarg;
12294 break;
12295 case 'S':
12296 allow_bad_symlinks = 1;
12297 break;
12298 default:
12299 usage_stage();
12300 /* NOTREACHED */
12304 argc -= optind;
12305 argv += optind;
12307 #ifndef PROFILE
12308 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12309 "unveil", NULL) == -1)
12310 err(1, "pledge");
12311 #endif
12312 if (list_stage && (pflag || patch_script_path))
12313 errx(1, "-l option cannot be used with other options");
12314 if (patch_script_path && !pflag)
12315 errx(1, "-F option can only be used together with -p option");
12317 cwd = getcwd(NULL, 0);
12318 if (cwd == NULL) {
12319 error = got_error_from_errno("getcwd");
12320 goto done;
12323 error = got_repo_pack_fds_open(&pack_fds);
12324 if (error != NULL)
12325 goto done;
12327 error = got_worktree_open(&worktree, cwd);
12328 if (error) {
12329 if (error->code == GOT_ERR_NOT_WORKTREE)
12330 error = wrap_not_worktree_error(error, "stage", cwd);
12331 goto done;
12334 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12335 NULL, pack_fds);
12336 if (error != NULL)
12337 goto done;
12339 if (patch_script_path) {
12340 patch_script_file = fopen(patch_script_path, "re");
12341 if (patch_script_file == NULL) {
12342 error = got_error_from_errno2("fopen",
12343 patch_script_path);
12344 goto done;
12347 error = apply_unveil(got_repo_get_path(repo), 0,
12348 got_worktree_get_root_path(worktree));
12349 if (error)
12350 goto done;
12352 error = check_merge_in_progress(worktree, repo);
12353 if (error)
12354 goto done;
12356 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12357 if (error)
12358 goto done;
12360 if (list_stage)
12361 error = got_worktree_status(worktree, &paths, repo, 0,
12362 print_stage, NULL, check_cancelled, NULL);
12363 else {
12364 cpa.patch_script_file = patch_script_file;
12365 cpa.action = "stage";
12366 error = got_worktree_stage(worktree, &paths,
12367 pflag ? NULL : print_status, NULL,
12368 pflag ? choose_patch : NULL, &cpa,
12369 allow_bad_symlinks, repo);
12371 done:
12372 if (patch_script_file && fclose(patch_script_file) == EOF &&
12373 error == NULL)
12374 error = got_error_from_errno2("fclose", patch_script_path);
12375 if (repo) {
12376 const struct got_error *close_err = got_repo_close(repo);
12377 if (error == NULL)
12378 error = close_err;
12380 if (worktree)
12381 got_worktree_close(worktree);
12382 if (pack_fds) {
12383 const struct got_error *pack_err =
12384 got_repo_pack_fds_close(pack_fds);
12385 if (error == NULL)
12386 error = pack_err;
12388 TAILQ_FOREACH(pe, &paths, entry)
12389 free((char *)pe->path);
12390 got_pathlist_free(&paths);
12391 free(cwd);
12392 return error;
12395 __dead static void
12396 usage_unstage(void)
12398 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12399 "[file-path ...]\n",
12400 getprogname());
12401 exit(1);
12405 static const struct got_error *
12406 cmd_unstage(int argc, char *argv[])
12408 const struct got_error *error = NULL;
12409 struct got_repository *repo = NULL;
12410 struct got_worktree *worktree = NULL;
12411 char *cwd = NULL;
12412 struct got_pathlist_head paths;
12413 struct got_pathlist_entry *pe;
12414 int ch, pflag = 0;
12415 struct got_update_progress_arg upa;
12416 FILE *patch_script_file = NULL;
12417 const char *patch_script_path = NULL;
12418 struct choose_patch_arg cpa;
12419 int *pack_fds = NULL;
12421 TAILQ_INIT(&paths);
12423 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12424 switch (ch) {
12425 case 'p':
12426 pflag = 1;
12427 break;
12428 case 'F':
12429 patch_script_path = optarg;
12430 break;
12431 default:
12432 usage_unstage();
12433 /* NOTREACHED */
12437 argc -= optind;
12438 argv += optind;
12440 #ifndef PROFILE
12441 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12442 "unveil", NULL) == -1)
12443 err(1, "pledge");
12444 #endif
12445 if (patch_script_path && !pflag)
12446 errx(1, "-F option can only be used together with -p option");
12448 cwd = getcwd(NULL, 0);
12449 if (cwd == NULL) {
12450 error = got_error_from_errno("getcwd");
12451 goto done;
12454 error = got_repo_pack_fds_open(&pack_fds);
12455 if (error != NULL)
12456 goto done;
12458 error = got_worktree_open(&worktree, cwd);
12459 if (error) {
12460 if (error->code == GOT_ERR_NOT_WORKTREE)
12461 error = wrap_not_worktree_error(error, "unstage", cwd);
12462 goto done;
12465 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12466 NULL, pack_fds);
12467 if (error != NULL)
12468 goto done;
12470 if (patch_script_path) {
12471 patch_script_file = fopen(patch_script_path, "re");
12472 if (patch_script_file == NULL) {
12473 error = got_error_from_errno2("fopen",
12474 patch_script_path);
12475 goto done;
12479 error = apply_unveil(got_repo_get_path(repo), 0,
12480 got_worktree_get_root_path(worktree));
12481 if (error)
12482 goto done;
12484 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12485 if (error)
12486 goto done;
12488 cpa.patch_script_file = patch_script_file;
12489 cpa.action = "unstage";
12490 memset(&upa, 0, sizeof(upa));
12491 error = got_worktree_unstage(worktree, &paths, update_progress,
12492 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12493 if (!error)
12494 print_merge_progress_stats(&upa);
12495 done:
12496 if (patch_script_file && fclose(patch_script_file) == EOF &&
12497 error == NULL)
12498 error = got_error_from_errno2("fclose", patch_script_path);
12499 if (repo) {
12500 const struct got_error *close_err = got_repo_close(repo);
12501 if (error == NULL)
12502 error = close_err;
12504 if (worktree)
12505 got_worktree_close(worktree);
12506 if (pack_fds) {
12507 const struct got_error *pack_err =
12508 got_repo_pack_fds_close(pack_fds);
12509 if (error == NULL)
12510 error = pack_err;
12512 TAILQ_FOREACH(pe, &paths, entry)
12513 free((char *)pe->path);
12514 got_pathlist_free(&paths);
12515 free(cwd);
12516 return error;
12519 __dead static void
12520 usage_cat(void)
12522 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12523 "arg1 [arg2 ...]\n", getprogname());
12524 exit(1);
12527 static const struct got_error *
12528 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12530 const struct got_error *err;
12531 struct got_blob_object *blob;
12532 int fd = -1;
12534 fd = got_opentempfd();
12535 if (fd == -1)
12536 return got_error_from_errno("got_opentempfd");
12538 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12539 if (err)
12540 goto done;
12542 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12543 done:
12544 if (fd != -1 && close(fd) == -1 && err == NULL)
12545 err = got_error_from_errno("close");
12546 if (blob)
12547 got_object_blob_close(blob);
12548 return err;
12551 static const struct got_error *
12552 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12554 const struct got_error *err;
12555 struct got_tree_object *tree;
12556 int nentries, i;
12558 err = got_object_open_as_tree(&tree, repo, id);
12559 if (err)
12560 return err;
12562 nentries = got_object_tree_get_nentries(tree);
12563 for (i = 0; i < nentries; i++) {
12564 struct got_tree_entry *te;
12565 char *id_str;
12566 if (sigint_received || sigpipe_received)
12567 break;
12568 te = got_object_tree_get_entry(tree, i);
12569 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12570 if (err)
12571 break;
12572 fprintf(outfile, "%s %.7o %s\n", id_str,
12573 got_tree_entry_get_mode(te),
12574 got_tree_entry_get_name(te));
12575 free(id_str);
12578 got_object_tree_close(tree);
12579 return err;
12582 static const struct got_error *
12583 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12585 const struct got_error *err;
12586 struct got_commit_object *commit;
12587 const struct got_object_id_queue *parent_ids;
12588 struct got_object_qid *pid;
12589 char *id_str = NULL;
12590 const char *logmsg = NULL;
12591 char gmtoff[6];
12593 err = got_object_open_as_commit(&commit, repo, id);
12594 if (err)
12595 return err;
12597 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12598 if (err)
12599 goto done;
12601 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12602 parent_ids = got_object_commit_get_parent_ids(commit);
12603 fprintf(outfile, "numparents %d\n",
12604 got_object_commit_get_nparents(commit));
12605 STAILQ_FOREACH(pid, parent_ids, entry) {
12606 char *pid_str;
12607 err = got_object_id_str(&pid_str, &pid->id);
12608 if (err)
12609 goto done;
12610 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12611 free(pid_str);
12613 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12614 got_object_commit_get_author_gmtoff(commit));
12615 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12616 got_object_commit_get_author(commit),
12617 (long long)got_object_commit_get_author_time(commit),
12618 gmtoff);
12620 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12621 got_object_commit_get_committer_gmtoff(commit));
12622 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12623 got_object_commit_get_author(commit),
12624 (long long)got_object_commit_get_committer_time(commit),
12625 gmtoff);
12627 logmsg = got_object_commit_get_logmsg_raw(commit);
12628 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12629 fprintf(outfile, "%s", logmsg);
12630 done:
12631 free(id_str);
12632 got_object_commit_close(commit);
12633 return err;
12636 static const struct got_error *
12637 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12639 const struct got_error *err;
12640 struct got_tag_object *tag;
12641 char *id_str = NULL;
12642 const char *tagmsg = NULL;
12643 char gmtoff[6];
12645 err = got_object_open_as_tag(&tag, repo, id);
12646 if (err)
12647 return err;
12649 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12650 if (err)
12651 goto done;
12653 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12655 switch (got_object_tag_get_object_type(tag)) {
12656 case GOT_OBJ_TYPE_BLOB:
12657 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12658 GOT_OBJ_LABEL_BLOB);
12659 break;
12660 case GOT_OBJ_TYPE_TREE:
12661 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12662 GOT_OBJ_LABEL_TREE);
12663 break;
12664 case GOT_OBJ_TYPE_COMMIT:
12665 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12666 GOT_OBJ_LABEL_COMMIT);
12667 break;
12668 case GOT_OBJ_TYPE_TAG:
12669 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12670 GOT_OBJ_LABEL_TAG);
12671 break;
12672 default:
12673 break;
12676 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12677 got_object_tag_get_name(tag));
12679 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12680 got_object_tag_get_tagger_gmtoff(tag));
12681 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12682 got_object_tag_get_tagger(tag),
12683 (long long)got_object_tag_get_tagger_time(tag),
12684 gmtoff);
12686 tagmsg = got_object_tag_get_message(tag);
12687 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12688 fprintf(outfile, "%s", tagmsg);
12689 done:
12690 free(id_str);
12691 got_object_tag_close(tag);
12692 return err;
12695 static const struct got_error *
12696 cmd_cat(int argc, char *argv[])
12698 const struct got_error *error;
12699 struct got_repository *repo = NULL;
12700 struct got_worktree *worktree = NULL;
12701 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12702 const char *commit_id_str = NULL;
12703 struct got_object_id *id = NULL, *commit_id = NULL;
12704 struct got_commit_object *commit = NULL;
12705 int ch, obj_type, i, force_path = 0;
12706 struct got_reflist_head refs;
12707 int *pack_fds = NULL;
12709 TAILQ_INIT(&refs);
12711 #ifndef PROFILE
12712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12713 NULL) == -1)
12714 err(1, "pledge");
12715 #endif
12717 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12718 switch (ch) {
12719 case 'c':
12720 commit_id_str = optarg;
12721 break;
12722 case 'r':
12723 repo_path = realpath(optarg, NULL);
12724 if (repo_path == NULL)
12725 return got_error_from_errno2("realpath",
12726 optarg);
12727 got_path_strip_trailing_slashes(repo_path);
12728 break;
12729 case 'P':
12730 force_path = 1;
12731 break;
12732 default:
12733 usage_cat();
12734 /* NOTREACHED */
12738 argc -= optind;
12739 argv += optind;
12741 cwd = getcwd(NULL, 0);
12742 if (cwd == NULL) {
12743 error = got_error_from_errno("getcwd");
12744 goto done;
12747 error = got_repo_pack_fds_open(&pack_fds);
12748 if (error != NULL)
12749 goto done;
12751 if (repo_path == NULL) {
12752 error = got_worktree_open(&worktree, cwd);
12753 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12754 goto done;
12755 if (worktree) {
12756 repo_path = strdup(
12757 got_worktree_get_repo_path(worktree));
12758 if (repo_path == NULL) {
12759 error = got_error_from_errno("strdup");
12760 goto done;
12763 /* Release work tree lock. */
12764 got_worktree_close(worktree);
12765 worktree = NULL;
12769 if (repo_path == NULL) {
12770 repo_path = strdup(cwd);
12771 if (repo_path == NULL)
12772 return got_error_from_errno("strdup");
12775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12776 free(repo_path);
12777 if (error != NULL)
12778 goto done;
12780 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12781 if (error)
12782 goto done;
12784 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12785 if (error)
12786 goto done;
12788 if (commit_id_str == NULL)
12789 commit_id_str = GOT_REF_HEAD;
12790 error = got_repo_match_object_id(&commit_id, NULL,
12791 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12792 if (error)
12793 goto done;
12795 error = got_object_open_as_commit(&commit, repo, commit_id);
12796 if (error)
12797 goto done;
12799 for (i = 0; i < argc; i++) {
12800 if (force_path) {
12801 error = got_object_id_by_path(&id, repo, commit,
12802 argv[i]);
12803 if (error)
12804 break;
12805 } else {
12806 error = got_repo_match_object_id(&id, &label, argv[i],
12807 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12808 repo);
12809 if (error) {
12810 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12811 error->code != GOT_ERR_NOT_REF)
12812 break;
12813 error = got_object_id_by_path(&id, repo,
12814 commit, argv[i]);
12815 if (error)
12816 break;
12820 error = got_object_get_type(&obj_type, repo, id);
12821 if (error)
12822 break;
12824 switch (obj_type) {
12825 case GOT_OBJ_TYPE_BLOB:
12826 error = cat_blob(id, repo, stdout);
12827 break;
12828 case GOT_OBJ_TYPE_TREE:
12829 error = cat_tree(id, repo, stdout);
12830 break;
12831 case GOT_OBJ_TYPE_COMMIT:
12832 error = cat_commit(id, repo, stdout);
12833 break;
12834 case GOT_OBJ_TYPE_TAG:
12835 error = cat_tag(id, repo, stdout);
12836 break;
12837 default:
12838 error = got_error(GOT_ERR_OBJ_TYPE);
12839 break;
12841 if (error)
12842 break;
12843 free(label);
12844 label = NULL;
12845 free(id);
12846 id = NULL;
12848 done:
12849 free(label);
12850 free(id);
12851 free(commit_id);
12852 if (commit)
12853 got_object_commit_close(commit);
12854 if (worktree)
12855 got_worktree_close(worktree);
12856 if (repo) {
12857 const struct got_error *close_err = got_repo_close(repo);
12858 if (error == NULL)
12859 error = close_err;
12861 if (pack_fds) {
12862 const struct got_error *pack_err =
12863 got_repo_pack_fds_close(pack_fds);
12864 if (error == NULL)
12865 error = pack_err;
12868 got_ref_list_free(&refs);
12869 return error;
12872 __dead static void
12873 usage_info(void)
12875 fprintf(stderr, "usage: %s info [path ...]\n",
12876 getprogname());
12877 exit(1);
12880 static const struct got_error *
12881 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12882 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12883 struct got_object_id *commit_id)
12885 const struct got_error *err = NULL;
12886 char *id_str = NULL;
12887 char datebuf[128];
12888 struct tm mytm, *tm;
12889 struct got_pathlist_head *paths = arg;
12890 struct got_pathlist_entry *pe;
12893 * Clear error indication from any of the path arguments which
12894 * would cause this file index entry to be displayed.
12896 TAILQ_FOREACH(pe, paths, entry) {
12897 if (got_path_cmp(path, pe->path, strlen(path),
12898 pe->path_len) == 0 ||
12899 got_path_is_child(path, pe->path, pe->path_len))
12900 pe->data = NULL; /* no error */
12903 printf(GOT_COMMIT_SEP_STR);
12904 if (S_ISLNK(mode))
12905 printf("symlink: %s\n", path);
12906 else if (S_ISREG(mode)) {
12907 printf("file: %s\n", path);
12908 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12909 } else if (S_ISDIR(mode))
12910 printf("directory: %s\n", path);
12911 else
12912 printf("something: %s\n", path);
12914 tm = localtime_r(&mtime, &mytm);
12915 if (tm == NULL)
12916 return NULL;
12917 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12918 return got_error(GOT_ERR_NO_SPACE);
12919 printf("timestamp: %s\n", datebuf);
12921 if (blob_id) {
12922 err = got_object_id_str(&id_str, blob_id);
12923 if (err)
12924 return err;
12925 printf("based on blob: %s\n", id_str);
12926 free(id_str);
12929 if (staged_blob_id) {
12930 err = got_object_id_str(&id_str, staged_blob_id);
12931 if (err)
12932 return err;
12933 printf("based on staged blob: %s\n", id_str);
12934 free(id_str);
12937 if (commit_id) {
12938 err = got_object_id_str(&id_str, commit_id);
12939 if (err)
12940 return err;
12941 printf("based on commit: %s\n", id_str);
12942 free(id_str);
12945 return NULL;
12948 static const struct got_error *
12949 cmd_info(int argc, char *argv[])
12951 const struct got_error *error = NULL;
12952 struct got_worktree *worktree = NULL;
12953 char *cwd = NULL, *id_str = NULL;
12954 struct got_pathlist_head paths;
12955 struct got_pathlist_entry *pe;
12956 char *uuidstr = NULL;
12957 int ch, show_files = 0;
12958 int *pack_fds = NULL;
12960 TAILQ_INIT(&paths);
12962 while ((ch = getopt(argc, argv, "")) != -1) {
12963 switch (ch) {
12964 default:
12965 usage_info();
12966 /* NOTREACHED */
12970 argc -= optind;
12971 argv += optind;
12973 #ifndef PROFILE
12974 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12975 NULL) == -1)
12976 err(1, "pledge");
12977 #endif
12978 cwd = getcwd(NULL, 0);
12979 if (cwd == NULL) {
12980 error = got_error_from_errno("getcwd");
12981 goto done;
12984 error = got_repo_pack_fds_open(&pack_fds);
12985 if (error != NULL)
12986 goto done;
12988 error = got_worktree_open(&worktree, cwd);
12989 if (error) {
12990 if (error->code == GOT_ERR_NOT_WORKTREE)
12991 error = wrap_not_worktree_error(error, "info", cwd);
12992 goto done;
12995 #ifndef PROFILE
12996 /* Remove "cpath" promise. */
12997 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12998 NULL) == -1)
12999 err(1, "pledge");
13000 #endif
13001 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13002 if (error)
13003 goto done;
13005 if (argc >= 1) {
13006 error = get_worktree_paths_from_argv(&paths, argc, argv,
13007 worktree);
13008 if (error)
13009 goto done;
13010 show_files = 1;
13013 error = got_object_id_str(&id_str,
13014 got_worktree_get_base_commit_id(worktree));
13015 if (error)
13016 goto done;
13018 error = got_worktree_get_uuid(&uuidstr, worktree);
13019 if (error)
13020 goto done;
13022 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13023 printf("work tree base commit: %s\n", id_str);
13024 printf("work tree path prefix: %s\n",
13025 got_worktree_get_path_prefix(worktree));
13026 printf("work tree branch reference: %s\n",
13027 got_worktree_get_head_ref_name(worktree));
13028 printf("work tree UUID: %s\n", uuidstr);
13029 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13031 if (show_files) {
13032 struct got_pathlist_entry *pe;
13033 TAILQ_FOREACH(pe, &paths, entry) {
13034 if (pe->path_len == 0)
13035 continue;
13037 * Assume this path will fail. This will be corrected
13038 * in print_path_info() in case the path does suceeed.
13040 pe->data = (void *)got_error_path(pe->path,
13041 GOT_ERR_BAD_PATH);
13043 error = got_worktree_path_info(worktree, &paths,
13044 print_path_info, &paths, check_cancelled, NULL);
13045 if (error)
13046 goto done;
13047 TAILQ_FOREACH(pe, &paths, entry) {
13048 if (pe->data != NULL) {
13049 error = pe->data; /* bad path */
13050 break;
13054 done:
13055 if (pack_fds) {
13056 const struct got_error *pack_err =
13057 got_repo_pack_fds_close(pack_fds);
13058 if (error == NULL)
13059 error = pack_err;
13061 TAILQ_FOREACH(pe, &paths, entry)
13062 free((char *)pe->path);
13063 got_pathlist_free(&paths);
13064 free(cwd);
13065 free(id_str);
13066 free(uuidstr);
13067 return error;