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_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
183 };
185 static void
186 list_commands(FILE *fp)
188 size_t i;
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
195 fputc('\n', fp);
198 __dead static void
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
204 int
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
208 size_t i;
209 int ch;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
213 { NULL, 0, NULL, 0 }
214 };
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
219 switch (ch) {
220 case 'h':
221 hflag = 1;
222 break;
223 case 'V':
224 Vflag = 1;
225 break;
226 default:
227 usage(hflag, 1);
228 /* NOTREACHED */
232 argc -= optind;
233 argv += optind;
234 optind = 1;
235 optreset = 1;
237 if (Vflag) {
238 got_version_print_str();
239 return 0;
242 if (argc <= 0)
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
255 continue;
257 if (hflag)
258 cmd->cmd_usage();
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
265 !(sigint_received &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
268 return 1;
271 return 0;
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
276 return 1;
279 __dead static void
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
285 getprogname());
286 if (hflag)
287 list_commands(fp);
288 exit(status);
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
295 const char *editor;
297 *abspath = NULL;
299 editor = getenv("VISUAL");
300 if (editor == NULL)
301 editor = getenv("EDITOR");
303 if (editor) {
304 err = got_path_find_prog(abspath, editor);
305 if (err)
306 return err;
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
315 return NULL;
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
324 #ifdef PROFILE
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
327 #endif
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
338 if (err != NULL)
339 return err;
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
344 return NULL;
347 __dead static void
348 usage_import(void)
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
352 exit(1);
355 static int
356 spawn_editor(const char *editor, const char *file)
358 pid_t pid;
359 sig_t sighup, sigint, sigquit;
360 int st = -1;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
367 case -1:
368 goto doneediting;
369 case 0:
370 execl(editor, editor, file, (char *)NULL);
371 _exit(127);
374 while (waitpid(pid, &st, 0) == -1)
375 if (errno != EINTR)
376 break;
378 doneediting:
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
384 errno = EINTR;
385 return -1;
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
397 char *line = NULL;
398 size_t linesize = 0;
399 ssize_t linelen;
400 struct stat st, st2;
401 FILE *fp = NULL;
402 size_t len, logmsg_len;
403 char *initial_content_stripped = NULL, *buf = NULL, *s;
405 *logmsg = NULL;
407 if (stat(logmsg_path, &st) == -1)
408 return got_error_from_errno2("stat", logmsg_path);
410 if (spawn_editor(editor, logmsg_path) == -1)
411 return got_error_from_errno("failed spawning editor");
413 if (stat(logmsg_path, &st2) == -1)
414 return got_error_from_errno("stat");
416 if (require_modification &&
417 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
421 /*
422 * Set up a stripped version of the initial content without comments
423 * and blank lines. We need this in order to check if the message
424 * has in fact been edited.
425 */
426 initial_content_stripped = malloc(initial_content_len + 1);
427 if (initial_content_stripped == NULL)
428 return got_error_from_errno("malloc");
429 initial_content_stripped[0] = '\0';
431 buf = strdup(initial_content);
432 if (buf == NULL) {
433 err = got_error_from_errno("strdup");
434 goto done;
436 s = buf;
437 len = 0;
438 while ((line = strsep(&s, "\n")) != NULL) {
439 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
440 continue; /* remove comments and leading empty lines */
441 len = strlcat(initial_content_stripped, line,
442 initial_content_len + 1);
443 if (len >= initial_content_len + 1) {
444 err = got_error(GOT_ERR_NO_SPACE);
445 goto done;
448 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
449 initial_content_stripped[len - 1] = '\0';
450 len--;
453 logmsg_len = st2.st_size;
454 *logmsg = malloc(logmsg_len + 1);
455 if (*logmsg == NULL)
456 return got_error_from_errno("malloc");
457 (*logmsg)[0] = '\0';
459 fp = fopen(logmsg_path, "re");
460 if (fp == NULL) {
461 err = got_error_from_errno("fopen");
462 goto done;
465 len = 0;
466 while ((linelen = getline(&line, &linesize, fp)) != -1) {
467 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
468 continue; /* remove comments and leading empty lines */
469 len = strlcat(*logmsg, line, logmsg_len + 1);
470 if (len >= logmsg_len + 1) {
471 err = got_error(GOT_ERR_NO_SPACE);
472 goto done;
475 free(line);
476 if (ferror(fp)) {
477 err = got_ferror(fp, GOT_ERR_IO);
478 goto done;
480 while (len > 0 && (*logmsg)[len - 1] == '\n') {
481 (*logmsg)[len - 1] = '\0';
482 len--;
485 if (len == 0) {
486 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
487 "commit message cannot be empty, aborting");
488 goto done;
490 if (require_modification &&
491 strcmp(*logmsg, initial_content_stripped) == 0)
492 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
493 "no changes made to commit message, aborting");
494 done:
495 free(initial_content_stripped);
496 free(buf);
497 if (fp && fclose(fp) == EOF && err == NULL)
498 err = got_error_from_errno("fclose");
499 if (err) {
500 free(*logmsg);
501 *logmsg = NULL;
503 return err;
506 static const struct got_error *
507 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
508 const char *path_dir, const char *branch_name)
510 char *initial_content = NULL;
511 const struct got_error *err = NULL;
512 int initial_content_len;
513 int fd = -1;
515 initial_content_len = asprintf(&initial_content,
516 "\n# %s to be imported to branch %s\n", path_dir,
517 branch_name);
518 if (initial_content_len == -1)
519 return got_error_from_errno("asprintf");
521 err = got_opentemp_named_fd(logmsg_path, &fd,
522 GOT_TMPDIR_STR "/got-importmsg");
523 if (err)
524 goto done;
526 if (write(fd, initial_content, initial_content_len) == -1) {
527 err = got_error_from_errno2("write", *logmsg_path);
528 goto done;
531 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
532 initial_content_len, 1);
533 done:
534 if (fd != -1 && close(fd) == -1 && err == NULL)
535 err = got_error_from_errno2("close", *logmsg_path);
536 free(initial_content);
537 if (err) {
538 free(*logmsg_path);
539 *logmsg_path = NULL;
541 return err;
544 static const struct got_error *
545 import_progress(void *arg, const char *path)
547 printf("A %s\n", path);
548 return NULL;
551 static int
552 valid_author(const char *author)
554 /*
555 * Really dumb email address check; we're only doing this to
556 * avoid git's object parser breaking on commits we create.
557 */
558 while (*author && *author != '<')
559 author++;
560 if (*author != '<')
561 return 0;
562 while (*author && *author != '@')
563 author++;
564 if (*author != '@')
565 return 0;
566 while (*author && *author != '>')
567 author++;
568 return *author == '>';
571 static const struct got_error *
572 get_author(char **author, struct got_repository *repo,
573 struct got_worktree *worktree)
575 const struct got_error *err = NULL;
576 const char *got_author = NULL, *name, *email;
577 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
579 *author = NULL;
581 if (worktree)
582 worktree_conf = got_worktree_get_gotconfig(worktree);
583 repo_conf = got_repo_get_gotconfig(repo);
585 /*
586 * Priority of potential author information sources, from most
587 * significant to least significant:
588 * 1) work tree's .got/got.conf file
589 * 2) repository's got.conf file
590 * 3) repository's git config file
591 * 4) environment variables
592 * 5) global git config files (in user's home directory or /etc)
593 */
595 if (worktree_conf)
596 got_author = got_gotconfig_get_author(worktree_conf);
597 if (got_author == NULL)
598 got_author = got_gotconfig_get_author(repo_conf);
599 if (got_author == NULL) {
600 name = got_repo_get_gitconfig_author_name(repo);
601 email = got_repo_get_gitconfig_author_email(repo);
602 if (name && email) {
603 if (asprintf(author, "%s <%s>", name, email) == -1)
604 return got_error_from_errno("asprintf");
605 return NULL;
608 got_author = getenv("GOT_AUTHOR");
609 if (got_author == NULL) {
610 name = got_repo_get_global_gitconfig_author_name(repo);
611 email = got_repo_get_global_gitconfig_author_email(
612 repo);
613 if (name && email) {
614 if (asprintf(author, "%s <%s>", name, email)
615 == -1)
616 return got_error_from_errno("asprintf");
617 return NULL;
619 /* TODO: Look up user in password database? */
620 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
624 *author = strdup(got_author);
625 if (*author == NULL)
626 return got_error_from_errno("strdup");
628 if (!valid_author(*author)) {
629 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
630 free(*author);
631 *author = NULL;
633 return err;
636 static const struct got_error *
637 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
638 struct got_worktree *worktree)
640 const char *got_allowed_signers = NULL;
641 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
643 *allowed_signers = NULL;
645 if (worktree)
646 worktree_conf = got_worktree_get_gotconfig(worktree);
647 repo_conf = got_repo_get_gotconfig(repo);
649 /*
650 * Priority of potential author information sources, from most
651 * significant to least significant:
652 * 1) work tree's .got/got.conf file
653 * 2) repository's got.conf file
654 */
656 if (worktree_conf)
657 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
658 worktree_conf);
659 if (got_allowed_signers == NULL)
660 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
661 repo_conf);
663 if (got_allowed_signers) {
664 *allowed_signers = strdup(got_allowed_signers);
665 if (*allowed_signers == NULL)
666 return got_error_from_errno("strdup");
668 return NULL;
671 static const struct got_error *
672 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
673 struct got_worktree *worktree)
675 const char *got_revoked_signers = NULL;
676 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
678 *revoked_signers = NULL;
680 if (worktree)
681 worktree_conf = got_worktree_get_gotconfig(worktree);
682 repo_conf = got_repo_get_gotconfig(repo);
684 /*
685 * Priority of potential author information sources, from most
686 * significant to least significant:
687 * 1) work tree's .got/got.conf file
688 * 2) repository's got.conf file
689 */
691 if (worktree_conf)
692 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
693 worktree_conf);
694 if (got_revoked_signers == NULL)
695 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
696 repo_conf);
698 if (got_revoked_signers) {
699 *revoked_signers = strdup(got_revoked_signers);
700 if (*revoked_signers == NULL)
701 return got_error_from_errno("strdup");
703 return NULL;
706 static const struct got_error *
707 get_signer_id(char **signer_id, struct got_repository *repo,
708 struct got_worktree *worktree)
710 const char *got_signer_id = NULL;
711 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
713 *signer_id = NULL;
715 if (worktree)
716 worktree_conf = got_worktree_get_gotconfig(worktree);
717 repo_conf = got_repo_get_gotconfig(repo);
719 /*
720 * Priority of potential author information sources, from most
721 * significant to least significant:
722 * 1) work tree's .got/got.conf file
723 * 2) repository's got.conf file
724 */
726 if (worktree_conf)
727 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
728 if (got_signer_id == NULL)
729 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
731 if (got_signer_id) {
732 *signer_id = strdup(got_signer_id);
733 if (*signer_id == NULL)
734 return got_error_from_errno("strdup");
736 return NULL;
739 static const struct got_error *
740 get_gitconfig_path(char **gitconfig_path)
742 const char *homedir = getenv("HOME");
744 *gitconfig_path = NULL;
745 if (homedir) {
746 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
747 return got_error_from_errno("asprintf");
750 return NULL;
753 static const struct got_error *
754 cmd_import(int argc, char *argv[])
756 const struct got_error *error = NULL;
757 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
758 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
759 const char *branch_name = "main";
760 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
761 struct got_repository *repo = NULL;
762 struct got_reference *branch_ref = NULL, *head_ref = NULL;
763 struct got_object_id *new_commit_id = NULL;
764 int ch;
765 struct got_pathlist_head ignores;
766 struct got_pathlist_entry *pe;
767 int preserve_logmsg = 0;
768 int *pack_fds = NULL;
770 TAILQ_INIT(&ignores);
772 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
773 switch (ch) {
774 case 'b':
775 branch_name = optarg;
776 break;
777 case 'm':
778 logmsg = strdup(optarg);
779 if (logmsg == NULL) {
780 error = got_error_from_errno("strdup");
781 goto done;
783 break;
784 case 'r':
785 repo_path = realpath(optarg, NULL);
786 if (repo_path == NULL) {
787 error = got_error_from_errno2("realpath",
788 optarg);
789 goto done;
791 break;
792 case 'I':
793 if (optarg[0] == '\0')
794 break;
795 error = got_pathlist_insert(&pe, &ignores, optarg,
796 NULL);
797 if (error)
798 goto done;
799 break;
800 default:
801 usage_import();
802 /* NOTREACHED */
806 argc -= optind;
807 argv += optind;
809 #ifndef PROFILE
810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
811 "unveil",
812 NULL) == -1)
813 err(1, "pledge");
814 #endif
815 if (argc != 1)
816 usage_import();
818 if (repo_path == NULL) {
819 repo_path = getcwd(NULL, 0);
820 if (repo_path == NULL)
821 return got_error_from_errno("getcwd");
823 got_path_strip_trailing_slashes(repo_path);
824 error = get_gitconfig_path(&gitconfig_path);
825 if (error)
826 goto done;
827 error = got_repo_pack_fds_open(&pack_fds);
828 if (error != NULL)
829 goto done;
830 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
831 if (error)
832 goto done;
834 error = get_author(&author, repo, NULL);
835 if (error)
836 return error;
838 /*
839 * Don't let the user create a branch name with a leading '-'.
840 * While technically a valid reference name, this case is usually
841 * an unintended typo.
842 */
843 if (branch_name[0] == '-')
844 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
846 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
847 error = got_error_from_errno("asprintf");
848 goto done;
851 error = got_ref_open(&branch_ref, repo, refname, 0);
852 if (error) {
853 if (error->code != GOT_ERR_NOT_REF)
854 goto done;
855 } else {
856 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
857 "import target branch already exists");
858 goto done;
861 path_dir = realpath(argv[0], NULL);
862 if (path_dir == NULL) {
863 error = got_error_from_errno2("realpath", argv[0]);
864 goto done;
866 got_path_strip_trailing_slashes(path_dir);
868 /*
869 * unveil(2) traverses exec(2); if an editor is used we have
870 * to apply unveil after the log message has been written.
871 */
872 if (logmsg == NULL || strlen(logmsg) == 0) {
873 error = get_editor(&editor);
874 if (error)
875 goto done;
876 free(logmsg);
877 error = collect_import_msg(&logmsg, &logmsg_path, editor,
878 path_dir, refname);
879 if (error) {
880 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
881 logmsg_path != NULL)
882 preserve_logmsg = 1;
883 goto done;
887 if (unveil(path_dir, "r") != 0) {
888 error = got_error_from_errno2("unveil", path_dir);
889 if (logmsg_path)
890 preserve_logmsg = 1;
891 goto done;
894 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
895 if (error) {
896 if (logmsg_path)
897 preserve_logmsg = 1;
898 goto done;
901 error = got_repo_import(&new_commit_id, path_dir, logmsg,
902 author, &ignores, repo, import_progress, NULL);
903 if (error) {
904 if (logmsg_path)
905 preserve_logmsg = 1;
906 goto done;
909 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
910 if (error) {
911 if (logmsg_path)
912 preserve_logmsg = 1;
913 goto done;
916 error = got_ref_write(branch_ref, repo);
917 if (error) {
918 if (logmsg_path)
919 preserve_logmsg = 1;
920 goto done;
923 error = got_object_id_str(&id_str, new_commit_id);
924 if (error) {
925 if (logmsg_path)
926 preserve_logmsg = 1;
927 goto done;
930 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
931 if (error) {
932 if (error->code != GOT_ERR_NOT_REF) {
933 if (logmsg_path)
934 preserve_logmsg = 1;
935 goto done;
938 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
939 branch_ref);
940 if (error) {
941 if (logmsg_path)
942 preserve_logmsg = 1;
943 goto done;
946 error = got_ref_write(head_ref, repo);
947 if (error) {
948 if (logmsg_path)
949 preserve_logmsg = 1;
950 goto done;
954 printf("Created branch %s with commit %s\n",
955 got_ref_get_name(branch_ref), id_str);
956 done:
957 if (pack_fds) {
958 const struct got_error *pack_err =
959 got_repo_pack_fds_close(pack_fds);
960 if (error == NULL)
961 error = pack_err;
963 if (preserve_logmsg) {
964 fprintf(stderr, "%s: log message preserved in %s\n",
965 getprogname(), logmsg_path);
966 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
967 error = got_error_from_errno2("unlink", logmsg_path);
968 free(logmsg);
969 free(logmsg_path);
970 free(repo_path);
971 free(editor);
972 free(refname);
973 free(new_commit_id);
974 free(id_str);
975 free(author);
976 free(gitconfig_path);
977 if (branch_ref)
978 got_ref_close(branch_ref);
979 if (head_ref)
980 got_ref_close(head_ref);
981 return error;
984 __dead static void
985 usage_clone(void)
987 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
988 "[-R reference] repository-url [directory]\n", getprogname());
989 exit(1);
992 struct got_fetch_progress_arg {
993 char last_scaled_size[FMT_SCALED_STRSIZE];
994 int last_p_indexed;
995 int last_p_resolved;
996 int verbosity;
998 struct got_repository *repo;
1000 int create_configs;
1001 int configs_created;
1002 struct {
1003 struct got_pathlist_head *symrefs;
1004 struct got_pathlist_head *wanted_branches;
1005 struct got_pathlist_head *wanted_refs;
1006 const char *proto;
1007 const char *host;
1008 const char *port;
1009 const char *remote_repo_path;
1010 const char *git_url;
1011 int fetch_all_branches;
1012 int mirror_references;
1013 } config_info;
1016 /* XXX forward declaration */
1017 static const struct got_error *
1018 create_config_files(const char *proto, const char *host, const char *port,
1019 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1020 int mirror_references, struct got_pathlist_head *symrefs,
1021 struct got_pathlist_head *wanted_branches,
1022 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1024 static const struct got_error *
1025 fetch_progress(void *arg, const char *message, off_t packfile_size,
1026 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1028 const struct got_error *err = NULL;
1029 struct got_fetch_progress_arg *a = arg;
1030 char scaled_size[FMT_SCALED_STRSIZE];
1031 int p_indexed, p_resolved;
1032 int print_size = 0, print_indexed = 0, print_resolved = 0;
1035 * In order to allow a failed clone to be resumed with 'got fetch'
1036 * we try to create configuration files as soon as possible.
1037 * Once the server has sent information about its default branch
1038 * we have all required information.
1040 if (a->create_configs && !a->configs_created &&
1041 !TAILQ_EMPTY(a->config_info.symrefs)) {
1042 err = create_config_files(a->config_info.proto,
1043 a->config_info.host, a->config_info.port,
1044 a->config_info.remote_repo_path,
1045 a->config_info.git_url,
1046 a->config_info.fetch_all_branches,
1047 a->config_info.mirror_references,
1048 a->config_info.symrefs,
1049 a->config_info.wanted_branches,
1050 a->config_info.wanted_refs, a->repo);
1051 if (err)
1052 return err;
1053 a->configs_created = 1;
1056 if (a->verbosity < 0)
1057 return NULL;
1059 if (message && message[0] != '\0') {
1060 printf("\rserver: %s", message);
1061 fflush(stdout);
1062 return NULL;
1065 if (packfile_size > 0 || nobj_indexed > 0) {
1066 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1067 (a->last_scaled_size[0] == '\0' ||
1068 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1069 print_size = 1;
1070 if (strlcpy(a->last_scaled_size, scaled_size,
1071 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1072 return got_error(GOT_ERR_NO_SPACE);
1074 if (nobj_indexed > 0) {
1075 p_indexed = (nobj_indexed * 100) / nobj_total;
1076 if (p_indexed != a->last_p_indexed) {
1077 a->last_p_indexed = p_indexed;
1078 print_indexed = 1;
1079 print_size = 1;
1082 if (nobj_resolved > 0) {
1083 p_resolved = (nobj_resolved * 100) /
1084 (nobj_total - nobj_loose);
1085 if (p_resolved != a->last_p_resolved) {
1086 a->last_p_resolved = p_resolved;
1087 print_resolved = 1;
1088 print_indexed = 1;
1089 print_size = 1;
1094 if (print_size || print_indexed || print_resolved)
1095 printf("\r");
1096 if (print_size)
1097 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1098 if (print_indexed)
1099 printf("; indexing %d%%", p_indexed);
1100 if (print_resolved)
1101 printf("; resolving deltas %d%%", p_resolved);
1102 if (print_size || print_indexed || print_resolved)
1103 fflush(stdout);
1105 return NULL;
1108 static const struct got_error *
1109 create_symref(const char *refname, struct got_reference *target_ref,
1110 int verbosity, struct got_repository *repo)
1112 const struct got_error *err;
1113 struct got_reference *head_symref;
1115 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1116 if (err)
1117 return err;
1119 err = got_ref_write(head_symref, repo);
1120 if (err == NULL && verbosity > 0) {
1121 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1122 got_ref_get_name(target_ref));
1124 got_ref_close(head_symref);
1125 return err;
1128 static const struct got_error *
1129 list_remote_refs(struct got_pathlist_head *symrefs,
1130 struct got_pathlist_head *refs)
1132 const struct got_error *err;
1133 struct got_pathlist_entry *pe;
1135 TAILQ_FOREACH(pe, symrefs, entry) {
1136 const char *refname = pe->path;
1137 const char *targetref = pe->data;
1139 printf("%s: %s\n", refname, targetref);
1142 TAILQ_FOREACH(pe, refs, entry) {
1143 const char *refname = pe->path;
1144 struct got_object_id *id = pe->data;
1145 char *id_str;
1147 err = got_object_id_str(&id_str, id);
1148 if (err)
1149 return err;
1150 printf("%s: %s\n", refname, id_str);
1151 free(id_str);
1154 return NULL;
1157 static const struct got_error *
1158 create_ref(const char *refname, struct got_object_id *id,
1159 int verbosity, struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_reference *ref;
1163 char *id_str;
1165 err = got_object_id_str(&id_str, id);
1166 if (err)
1167 return err;
1169 err = got_ref_alloc(&ref, refname, id);
1170 if (err)
1171 goto done;
1173 err = got_ref_write(ref, repo);
1174 got_ref_close(ref);
1176 if (err == NULL && verbosity >= 0)
1177 printf("Created reference %s: %s\n", refname, id_str);
1178 done:
1179 free(id_str);
1180 return err;
1183 static int
1184 match_wanted_ref(const char *refname, const char *wanted_ref)
1186 if (strncmp(refname, "refs/", 5) != 0)
1187 return 0;
1188 refname += 5;
1191 * Prevent fetching of references that won't make any
1192 * sense outside of the remote repository's context.
1194 if (strncmp(refname, "got/", 4) == 0)
1195 return 0;
1196 if (strncmp(refname, "remotes/", 8) == 0)
1197 return 0;
1199 if (strncmp(wanted_ref, "refs/", 5) == 0)
1200 wanted_ref += 5;
1202 /* Allow prefix match. */
1203 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1204 return 1;
1206 /* Allow exact match. */
1207 return (strcmp(refname, wanted_ref) == 0);
1210 static int
1211 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1213 struct got_pathlist_entry *pe;
1215 TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 if (match_wanted_ref(refname, pe->path))
1217 return 1;
1220 return 0;
1223 static const struct got_error *
1224 create_wanted_ref(const char *refname, struct got_object_id *id,
1225 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1227 const struct got_error *err;
1228 char *remote_refname;
1230 if (strncmp("refs/", refname, 5) == 0)
1231 refname += 5;
1233 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1234 remote_repo_name, refname) == -1)
1235 return got_error_from_errno("asprintf");
1237 err = create_ref(remote_refname, id, verbosity, repo);
1238 free(remote_refname);
1239 return err;
1242 static const struct got_error *
1243 create_gotconfig(const char *proto, const char *host, const char *port,
1244 const char *remote_repo_path, const char *default_branch,
1245 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1246 struct got_pathlist_head *wanted_refs, int mirror_references,
1247 struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 char *gotconfig_path = NULL;
1251 char *gotconfig = NULL;
1252 FILE *gotconfig_file = NULL;
1253 const char *branchname = NULL;
1254 char *branches = NULL, *refs = NULL;
1255 ssize_t n;
1257 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1258 struct got_pathlist_entry *pe;
1259 TAILQ_FOREACH(pe, wanted_branches, entry) {
1260 char *s;
1261 branchname = pe->path;
1262 if (strncmp(branchname, "refs/heads/", 11) == 0)
1263 branchname += 11;
1264 if (asprintf(&s, "%s\"%s\" ",
1265 branches ? branches : "", branchname) == -1) {
1266 err = got_error_from_errno("asprintf");
1267 goto done;
1269 free(branches);
1270 branches = s;
1272 } else if (!fetch_all_branches && default_branch) {
1273 branchname = default_branch;
1274 if (strncmp(branchname, "refs/heads/", 11) == 0)
1275 branchname += 11;
1276 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1277 err = got_error_from_errno("asprintf");
1278 goto done;
1281 if (!TAILQ_EMPTY(wanted_refs)) {
1282 struct got_pathlist_entry *pe;
1283 TAILQ_FOREACH(pe, wanted_refs, entry) {
1284 char *s;
1285 const char *refname = pe->path;
1286 if (strncmp(refname, "refs/", 5) == 0)
1287 branchname += 5;
1288 if (asprintf(&s, "%s\"%s\" ",
1289 refs ? refs : "", refname) == -1) {
1290 err = got_error_from_errno("asprintf");
1291 goto done;
1293 free(refs);
1294 refs = s;
1298 /* Create got.conf(5). */
1299 gotconfig_path = got_repo_get_path_gotconfig(repo);
1300 if (gotconfig_path == NULL) {
1301 err = got_error_from_errno("got_repo_get_path_gotconfig");
1302 goto done;
1304 gotconfig_file = fopen(gotconfig_path, "ae");
1305 if (gotconfig_file == NULL) {
1306 err = got_error_from_errno2("fopen", gotconfig_path);
1307 goto done;
1309 if (asprintf(&gotconfig,
1310 "remote \"%s\" {\n"
1311 "\tserver %s\n"
1312 "\tprotocol %s\n"
1313 "%s%s%s"
1314 "\trepository \"%s\"\n"
1315 "%s%s%s"
1316 "%s%s%s"
1317 "%s"
1318 "%s"
1319 "}\n",
1320 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1321 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1322 remote_repo_path, branches ? "\tbranch { " : "",
1323 branches ? branches : "", branches ? "}\n" : "",
1324 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1325 mirror_references ? "\tmirror_references yes\n" : "",
1326 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1327 err = got_error_from_errno("asprintf");
1328 goto done;
1330 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1331 if (n != strlen(gotconfig)) {
1332 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1333 goto done;
1336 done:
1337 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1338 err = got_error_from_errno2("fclose", gotconfig_path);
1339 free(gotconfig_path);
1340 free(branches);
1341 return err;
1344 static const struct got_error *
1345 create_gitconfig(const char *git_url, const char *default_branch,
1346 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1347 struct got_pathlist_head *wanted_refs, int mirror_references,
1348 struct got_repository *repo)
1350 const struct got_error *err = NULL;
1351 char *gitconfig_path = NULL;
1352 char *gitconfig = NULL;
1353 FILE *gitconfig_file = NULL;
1354 char *branches = NULL, *refs = NULL;
1355 const char *branchname;
1356 ssize_t n;
1358 /* Create a config file Git can understand. */
1359 gitconfig_path = got_repo_get_path_gitconfig(repo);
1360 if (gitconfig_path == NULL) {
1361 err = got_error_from_errno("got_repo_get_path_gitconfig");
1362 goto done;
1364 gitconfig_file = fopen(gitconfig_path, "ae");
1365 if (gitconfig_file == NULL) {
1366 err = got_error_from_errno2("fopen", gitconfig_path);
1367 goto done;
1369 if (fetch_all_branches) {
1370 if (mirror_references) {
1371 if (asprintf(&branches,
1372 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1373 err = got_error_from_errno("asprintf");
1374 goto done;
1376 } else if (asprintf(&branches,
1377 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1378 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1379 err = got_error_from_errno("asprintf");
1380 goto done;
1382 } else if (!TAILQ_EMPTY(wanted_branches)) {
1383 struct got_pathlist_entry *pe;
1384 TAILQ_FOREACH(pe, wanted_branches, entry) {
1385 char *s;
1386 branchname = pe->path;
1387 if (strncmp(branchname, "refs/heads/", 11) == 0)
1388 branchname += 11;
1389 if (mirror_references) {
1390 if (asprintf(&s,
1391 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1392 branches ? branches : "",
1393 branchname, branchname) == -1) {
1394 err = got_error_from_errno("asprintf");
1395 goto done;
1397 } else if (asprintf(&s,
1398 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1399 branches ? branches : "",
1400 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1401 branchname) == -1) {
1402 err = got_error_from_errno("asprintf");
1403 goto done;
1405 free(branches);
1406 branches = s;
1408 } else {
1410 * If the server specified a default branch, use just that one.
1411 * Otherwise fall back to fetching all branches on next fetch.
1413 if (default_branch) {
1414 branchname = default_branch;
1415 if (strncmp(branchname, "refs/heads/", 11) == 0)
1416 branchname += 11;
1417 } else
1418 branchname = "*"; /* fall back to all branches */
1419 if (mirror_references) {
1420 if (asprintf(&branches,
1421 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1422 branchname, branchname) == -1) {
1423 err = got_error_from_errno("asprintf");
1424 goto done;
1426 } else if (asprintf(&branches,
1427 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1428 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1429 branchname) == -1) {
1430 err = got_error_from_errno("asprintf");
1431 goto done;
1434 if (!TAILQ_EMPTY(wanted_refs)) {
1435 struct got_pathlist_entry *pe;
1436 TAILQ_FOREACH(pe, wanted_refs, entry) {
1437 char *s;
1438 const char *refname = pe->path;
1439 if (strncmp(refname, "refs/", 5) == 0)
1440 refname += 5;
1441 if (mirror_references) {
1442 if (asprintf(&s,
1443 "%s\tfetch = refs/%s:refs/%s\n",
1444 refs ? refs : "", refname, refname) == -1) {
1445 err = got_error_from_errno("asprintf");
1446 goto done;
1448 } else if (asprintf(&s,
1449 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1450 refs ? refs : "",
1451 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1452 refname) == -1) {
1453 err = got_error_from_errno("asprintf");
1454 goto done;
1456 free(refs);
1457 refs = s;
1461 if (asprintf(&gitconfig,
1462 "[remote \"%s\"]\n"
1463 "\turl = %s\n"
1464 "%s"
1465 "%s"
1466 "\tfetch = refs/tags/*:refs/tags/*\n",
1467 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1468 refs ? refs : "") == -1) {
1469 err = got_error_from_errno("asprintf");
1470 goto done;
1472 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1473 if (n != strlen(gitconfig)) {
1474 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1475 goto done;
1477 done:
1478 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1479 err = got_error_from_errno2("fclose", gitconfig_path);
1480 free(gitconfig_path);
1481 free(branches);
1482 return err;
1485 static const struct got_error *
1486 create_config_files(const char *proto, const char *host, const char *port,
1487 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1488 int mirror_references, struct got_pathlist_head *symrefs,
1489 struct got_pathlist_head *wanted_branches,
1490 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 const char *default_branch = NULL;
1494 struct got_pathlist_entry *pe;
1497 * If we asked for a set of wanted branches then use the first
1498 * one of those.
1500 if (!TAILQ_EMPTY(wanted_branches)) {
1501 pe = TAILQ_FIRST(wanted_branches);
1502 default_branch = pe->path;
1503 } else {
1504 /* First HEAD ref listed by server is the default branch. */
1505 TAILQ_FOREACH(pe, symrefs, entry) {
1506 const char *refname = pe->path;
1507 const char *target = pe->data;
1509 if (strcmp(refname, GOT_REF_HEAD) != 0)
1510 continue;
1512 default_branch = target;
1513 break;
1517 /* Create got.conf(5). */
1518 err = create_gotconfig(proto, host, port, remote_repo_path,
1519 default_branch, fetch_all_branches, wanted_branches,
1520 wanted_refs, mirror_references, repo);
1521 if (err)
1522 return err;
1524 /* Create a config file Git can understand. */
1525 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1526 wanted_branches, wanted_refs, mirror_references, repo);
1529 static const struct got_error *
1530 cmd_clone(int argc, char *argv[])
1532 const struct got_error *error = NULL;
1533 const char *uri, *dirname;
1534 char *proto, *host, *port, *repo_name, *server_path;
1535 char *default_destdir = NULL, *id_str = NULL;
1536 const char *repo_path;
1537 struct got_repository *repo = NULL;
1538 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1539 struct got_pathlist_entry *pe;
1540 struct got_object_id *pack_hash = NULL;
1541 int ch, fetchfd = -1, fetchstatus;
1542 pid_t fetchpid = -1;
1543 struct got_fetch_progress_arg fpa;
1544 char *git_url = NULL;
1545 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1546 int list_refs_only = 0;
1547 int *pack_fds = NULL;
1549 TAILQ_INIT(&refs);
1550 TAILQ_INIT(&symrefs);
1551 TAILQ_INIT(&wanted_branches);
1552 TAILQ_INIT(&wanted_refs);
1554 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1555 switch (ch) {
1556 case 'a':
1557 fetch_all_branches = 1;
1558 break;
1559 case 'b':
1560 error = got_pathlist_append(&wanted_branches,
1561 optarg, NULL);
1562 if (error)
1563 return error;
1564 break;
1565 case 'l':
1566 list_refs_only = 1;
1567 break;
1568 case 'm':
1569 mirror_references = 1;
1570 break;
1571 case 'v':
1572 if (verbosity < 0)
1573 verbosity = 0;
1574 else if (verbosity < 3)
1575 verbosity++;
1576 break;
1577 case 'q':
1578 verbosity = -1;
1579 break;
1580 case 'R':
1581 error = got_pathlist_append(&wanted_refs,
1582 optarg, NULL);
1583 if (error)
1584 return error;
1585 break;
1586 default:
1587 usage_clone();
1588 break;
1591 argc -= optind;
1592 argv += optind;
1594 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1595 option_conflict('a', 'b');
1596 if (list_refs_only) {
1597 if (!TAILQ_EMPTY(&wanted_branches))
1598 option_conflict('l', 'b');
1599 if (fetch_all_branches)
1600 option_conflict('l', 'a');
1601 if (mirror_references)
1602 option_conflict('l', 'm');
1603 if (!TAILQ_EMPTY(&wanted_refs))
1604 option_conflict('l', 'R');
1607 uri = argv[0];
1609 if (argc == 1)
1610 dirname = NULL;
1611 else if (argc == 2)
1612 dirname = argv[1];
1613 else
1614 usage_clone();
1616 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1617 &repo_name, uri);
1618 if (error)
1619 goto done;
1621 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1622 host, port ? ":" : "", port ? port : "",
1623 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1624 error = got_error_from_errno("asprintf");
1625 goto done;
1628 if (strcmp(proto, "git") == 0) {
1629 #ifndef PROFILE
1630 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1631 "sendfd dns inet unveil", NULL) == -1)
1632 err(1, "pledge");
1633 #endif
1634 } else if (strcmp(proto, "git+ssh") == 0 ||
1635 strcmp(proto, "ssh") == 0) {
1636 #ifndef PROFILE
1637 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1638 "sendfd unveil", NULL) == -1)
1639 err(1, "pledge");
1640 #endif
1641 } else if (strcmp(proto, "http") == 0 ||
1642 strcmp(proto, "git+http") == 0) {
1643 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1644 goto done;
1645 } else {
1646 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1647 goto done;
1649 if (dirname == NULL) {
1650 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1651 error = got_error_from_errno("asprintf");
1652 goto done;
1654 repo_path = default_destdir;
1655 } else
1656 repo_path = dirname;
1658 if (!list_refs_only) {
1659 error = got_path_mkdir(repo_path);
1660 if (error &&
1661 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1662 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1663 goto done;
1664 if (!got_path_dir_is_empty(repo_path)) {
1665 error = got_error_path(repo_path,
1666 GOT_ERR_DIR_NOT_EMPTY);
1667 goto done;
1671 error = got_dial_apply_unveil(proto);
1672 if (error)
1673 goto done;
1675 error = apply_unveil(repo_path, 0, NULL);
1676 if (error)
1677 goto done;
1679 if (verbosity >= 0)
1680 printf("Connecting to %s%s%s\n", host,
1681 port ? ":" : "", port ? port : "");
1683 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1684 server_path, verbosity);
1685 if (error)
1686 goto done;
1688 if (!list_refs_only) {
1689 error = got_repo_init(repo_path);
1690 if (error)
1691 goto done;
1692 error = got_repo_pack_fds_open(&pack_fds);
1693 if (error != NULL)
1694 goto done;
1695 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1696 if (error)
1697 goto done;
1700 fpa.last_scaled_size[0] = '\0';
1701 fpa.last_p_indexed = -1;
1702 fpa.last_p_resolved = -1;
1703 fpa.verbosity = verbosity;
1704 fpa.create_configs = 1;
1705 fpa.configs_created = 0;
1706 fpa.repo = repo;
1707 fpa.config_info.symrefs = &symrefs;
1708 fpa.config_info.wanted_branches = &wanted_branches;
1709 fpa.config_info.wanted_refs = &wanted_refs;
1710 fpa.config_info.proto = proto;
1711 fpa.config_info.host = host;
1712 fpa.config_info.port = port;
1713 fpa.config_info.remote_repo_path = server_path;
1714 fpa.config_info.git_url = git_url;
1715 fpa.config_info.fetch_all_branches = fetch_all_branches;
1716 fpa.config_info.mirror_references = mirror_references;
1717 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1718 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1719 fetch_all_branches, &wanted_branches, &wanted_refs,
1720 list_refs_only, verbosity, fetchfd, repo,
1721 fetch_progress, &fpa);
1722 if (error)
1723 goto done;
1725 if (list_refs_only) {
1726 error = list_remote_refs(&symrefs, &refs);
1727 goto done;
1730 if (pack_hash == NULL) {
1731 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1732 "server sent an empty pack file");
1733 goto done;
1735 error = got_object_id_str(&id_str, pack_hash);
1736 if (error)
1737 goto done;
1738 if (verbosity >= 0)
1739 printf("\nFetched %s.pack\n", id_str);
1740 free(id_str);
1742 /* Set up references provided with the pack file. */
1743 TAILQ_FOREACH(pe, &refs, entry) {
1744 const char *refname = pe->path;
1745 struct got_object_id *id = pe->data;
1746 char *remote_refname;
1748 if (is_wanted_ref(&wanted_refs, refname) &&
1749 !mirror_references) {
1750 error = create_wanted_ref(refname, id,
1751 GOT_FETCH_DEFAULT_REMOTE_NAME,
1752 verbosity - 1, repo);
1753 if (error)
1754 goto done;
1755 continue;
1758 error = create_ref(refname, id, verbosity - 1, repo);
1759 if (error)
1760 goto done;
1762 if (mirror_references)
1763 continue;
1765 if (strncmp("refs/heads/", refname, 11) != 0)
1766 continue;
1768 if (asprintf(&remote_refname,
1769 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1770 refname + 11) == -1) {
1771 error = got_error_from_errno("asprintf");
1772 goto done;
1774 error = create_ref(remote_refname, id, verbosity - 1, repo);
1775 free(remote_refname);
1776 if (error)
1777 goto done;
1780 /* Set the HEAD reference if the server provided one. */
1781 TAILQ_FOREACH(pe, &symrefs, entry) {
1782 struct got_reference *target_ref;
1783 const char *refname = pe->path;
1784 const char *target = pe->data;
1785 char *remote_refname = NULL, *remote_target = NULL;
1787 if (strcmp(refname, GOT_REF_HEAD) != 0)
1788 continue;
1790 error = got_ref_open(&target_ref, repo, target, 0);
1791 if (error) {
1792 if (error->code == GOT_ERR_NOT_REF) {
1793 error = NULL;
1794 continue;
1796 goto done;
1799 error = create_symref(refname, target_ref, verbosity, repo);
1800 got_ref_close(target_ref);
1801 if (error)
1802 goto done;
1804 if (mirror_references)
1805 continue;
1807 if (strncmp("refs/heads/", target, 11) != 0)
1808 continue;
1810 if (asprintf(&remote_refname,
1811 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1812 refname) == -1) {
1813 error = got_error_from_errno("asprintf");
1814 goto done;
1816 if (asprintf(&remote_target,
1817 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1818 target + 11) == -1) {
1819 error = got_error_from_errno("asprintf");
1820 free(remote_refname);
1821 goto done;
1823 error = got_ref_open(&target_ref, repo, remote_target, 0);
1824 if (error) {
1825 free(remote_refname);
1826 free(remote_target);
1827 if (error->code == GOT_ERR_NOT_REF) {
1828 error = NULL;
1829 continue;
1831 goto done;
1833 error = create_symref(remote_refname, target_ref,
1834 verbosity - 1, repo);
1835 free(remote_refname);
1836 free(remote_target);
1837 got_ref_close(target_ref);
1838 if (error)
1839 goto done;
1841 if (pe == NULL) {
1843 * We failed to set the HEAD reference. If we asked for
1844 * a set of wanted branches use the first of one of those
1845 * which could be fetched instead.
1847 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1848 const char *target = pe->path;
1849 struct got_reference *target_ref;
1851 error = got_ref_open(&target_ref, repo, target, 0);
1852 if (error) {
1853 if (error->code == GOT_ERR_NOT_REF) {
1854 error = NULL;
1855 continue;
1857 goto done;
1860 error = create_symref(GOT_REF_HEAD, target_ref,
1861 verbosity, repo);
1862 got_ref_close(target_ref);
1863 if (error)
1864 goto done;
1865 break;
1869 if (verbosity >= 0)
1870 printf("Created %s repository '%s'\n",
1871 mirror_references ? "mirrored" : "cloned", repo_path);
1872 done:
1873 if (pack_fds) {
1874 const struct got_error *pack_err =
1875 got_repo_pack_fds_close(pack_fds);
1876 if (error == NULL)
1877 error = pack_err;
1879 if (fetchpid > 0) {
1880 if (kill(fetchpid, SIGTERM) == -1)
1881 error = got_error_from_errno("kill");
1882 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1883 error = got_error_from_errno("waitpid");
1885 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1886 error = got_error_from_errno("close");
1887 if (repo) {
1888 const struct got_error *close_err = got_repo_close(repo);
1889 if (error == NULL)
1890 error = close_err;
1892 TAILQ_FOREACH(pe, &refs, entry) {
1893 free((void *)pe->path);
1894 free(pe->data);
1896 got_pathlist_free(&refs);
1897 TAILQ_FOREACH(pe, &symrefs, entry) {
1898 free((void *)pe->path);
1899 free(pe->data);
1901 got_pathlist_free(&symrefs);
1902 got_pathlist_free(&wanted_branches);
1903 got_pathlist_free(&wanted_refs);
1904 free(pack_hash);
1905 free(proto);
1906 free(host);
1907 free(port);
1908 free(server_path);
1909 free(repo_name);
1910 free(default_destdir);
1911 free(git_url);
1912 return error;
1915 static const struct got_error *
1916 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1917 int replace_tags, int verbosity, struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 char *new_id_str = NULL;
1921 struct got_object_id *old_id = NULL;
1923 err = got_object_id_str(&new_id_str, new_id);
1924 if (err)
1925 goto done;
1927 if (!replace_tags &&
1928 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1929 err = got_ref_resolve(&old_id, repo, ref);
1930 if (err)
1931 goto done;
1932 if (got_object_id_cmp(old_id, new_id) == 0)
1933 goto done;
1934 if (verbosity >= 0) {
1935 printf("Rejecting update of existing tag %s: %s\n",
1936 got_ref_get_name(ref), new_id_str);
1938 goto done;
1941 if (got_ref_is_symbolic(ref)) {
1942 if (verbosity >= 0) {
1943 printf("Replacing reference %s: %s\n",
1944 got_ref_get_name(ref),
1945 got_ref_get_symref_target(ref));
1947 err = got_ref_change_symref_to_ref(ref, new_id);
1948 if (err)
1949 goto done;
1950 err = got_ref_write(ref, repo);
1951 if (err)
1952 goto done;
1953 } else {
1954 err = got_ref_resolve(&old_id, repo, ref);
1955 if (err)
1956 goto done;
1957 if (got_object_id_cmp(old_id, new_id) == 0)
1958 goto done;
1960 err = got_ref_change_ref(ref, new_id);
1961 if (err)
1962 goto done;
1963 err = got_ref_write(ref, repo);
1964 if (err)
1965 goto done;
1968 if (verbosity >= 0)
1969 printf("Updated %s: %s\n", got_ref_get_name(ref),
1970 new_id_str);
1971 done:
1972 free(old_id);
1973 free(new_id_str);
1974 return err;
1977 static const struct got_error *
1978 update_symref(const char *refname, struct got_reference *target_ref,
1979 int verbosity, struct got_repository *repo)
1981 const struct got_error *err = NULL, *unlock_err;
1982 struct got_reference *symref;
1983 int symref_is_locked = 0;
1985 err = got_ref_open(&symref, repo, refname, 1);
1986 if (err) {
1987 if (err->code != GOT_ERR_NOT_REF)
1988 return err;
1989 err = got_ref_alloc_symref(&symref, refname, target_ref);
1990 if (err)
1991 goto done;
1993 err = got_ref_write(symref, repo);
1994 if (err)
1995 goto done;
1997 if (verbosity >= 0)
1998 printf("Created reference %s: %s\n",
1999 got_ref_get_name(symref),
2000 got_ref_get_symref_target(symref));
2001 } else {
2002 symref_is_locked = 1;
2004 if (strcmp(got_ref_get_symref_target(symref),
2005 got_ref_get_name(target_ref)) == 0)
2006 goto done;
2008 err = got_ref_change_symref(symref,
2009 got_ref_get_name(target_ref));
2010 if (err)
2011 goto done;
2013 err = got_ref_write(symref, repo);
2014 if (err)
2015 goto done;
2017 if (verbosity >= 0)
2018 printf("Updated %s: %s\n", got_ref_get_name(symref),
2019 got_ref_get_symref_target(symref));
2022 done:
2023 if (symref_is_locked) {
2024 unlock_err = got_ref_unlock(symref);
2025 if (unlock_err && err == NULL)
2026 err = unlock_err;
2028 got_ref_close(symref);
2029 return err;
2032 __dead static void
2033 usage_fetch(void)
2035 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2036 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2037 "[remote-repository-name]\n",
2038 getprogname());
2039 exit(1);
2042 static const struct got_error *
2043 delete_missing_ref(struct got_reference *ref,
2044 int verbosity, struct got_repository *repo)
2046 const struct got_error *err = NULL;
2047 struct got_object_id *id = NULL;
2048 char *id_str = NULL;
2050 if (got_ref_is_symbolic(ref)) {
2051 err = got_ref_delete(ref, repo);
2052 if (err)
2053 return err;
2054 if (verbosity >= 0) {
2055 printf("Deleted %s: %s\n",
2056 got_ref_get_name(ref),
2057 got_ref_get_symref_target(ref));
2059 } else {
2060 err = got_ref_resolve(&id, repo, ref);
2061 if (err)
2062 return err;
2063 err = got_object_id_str(&id_str, id);
2064 if (err)
2065 goto done;
2067 err = got_ref_delete(ref, repo);
2068 if (err)
2069 goto done;
2070 if (verbosity >= 0) {
2071 printf("Deleted %s: %s\n",
2072 got_ref_get_name(ref), id_str);
2075 done:
2076 free(id);
2077 free(id_str);
2078 return NULL;
2081 static const struct got_error *
2082 delete_missing_refs(struct got_pathlist_head *their_refs,
2083 struct got_pathlist_head *their_symrefs,
2084 const struct got_remote_repo *remote,
2085 int verbosity, struct got_repository *repo)
2087 const struct got_error *err = NULL, *unlock_err;
2088 struct got_reflist_head my_refs;
2089 struct got_reflist_entry *re;
2090 struct got_pathlist_entry *pe;
2091 char *remote_namespace = NULL;
2092 char *local_refname = NULL;
2094 TAILQ_INIT(&my_refs);
2096 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2097 == -1)
2098 return got_error_from_errno("asprintf");
2100 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2101 if (err)
2102 goto done;
2104 TAILQ_FOREACH(re, &my_refs, entry) {
2105 const char *refname = got_ref_get_name(re->ref);
2106 const char *their_refname;
2108 if (remote->mirror_references) {
2109 their_refname = refname;
2110 } else {
2111 if (strncmp(refname, remote_namespace,
2112 strlen(remote_namespace)) == 0) {
2113 if (strcmp(refname + strlen(remote_namespace),
2114 GOT_REF_HEAD) == 0)
2115 continue;
2116 if (asprintf(&local_refname, "refs/heads/%s",
2117 refname + strlen(remote_namespace)) == -1) {
2118 err = got_error_from_errno("asprintf");
2119 goto done;
2121 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2122 continue;
2124 their_refname = local_refname;
2127 TAILQ_FOREACH(pe, their_refs, entry) {
2128 if (strcmp(their_refname, pe->path) == 0)
2129 break;
2131 if (pe != NULL)
2132 continue;
2134 TAILQ_FOREACH(pe, their_symrefs, entry) {
2135 if (strcmp(their_refname, pe->path) == 0)
2136 break;
2138 if (pe != NULL)
2139 continue;
2141 err = delete_missing_ref(re->ref, verbosity, repo);
2142 if (err)
2143 break;
2145 if (local_refname) {
2146 struct got_reference *ref;
2147 err = got_ref_open(&ref, repo, local_refname, 1);
2148 if (err) {
2149 if (err->code != GOT_ERR_NOT_REF)
2150 break;
2151 free(local_refname);
2152 local_refname = NULL;
2153 continue;
2155 err = delete_missing_ref(ref, verbosity, repo);
2156 if (err)
2157 break;
2158 unlock_err = got_ref_unlock(ref);
2159 got_ref_close(ref);
2160 if (unlock_err && err == NULL) {
2161 err = unlock_err;
2162 break;
2165 free(local_refname);
2166 local_refname = NULL;
2169 done:
2170 free(remote_namespace);
2171 free(local_refname);
2172 return err;
2175 static const struct got_error *
2176 update_wanted_ref(const char *refname, struct got_object_id *id,
2177 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2179 const struct got_error *err, *unlock_err;
2180 char *remote_refname;
2181 struct got_reference *ref;
2183 if (strncmp("refs/", refname, 5) == 0)
2184 refname += 5;
2186 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2187 remote_repo_name, refname) == -1)
2188 return got_error_from_errno("asprintf");
2190 err = got_ref_open(&ref, repo, remote_refname, 1);
2191 if (err) {
2192 if (err->code != GOT_ERR_NOT_REF)
2193 goto done;
2194 err = create_ref(remote_refname, id, verbosity, repo);
2195 } else {
2196 err = update_ref(ref, id, 0, verbosity, repo);
2197 unlock_err = got_ref_unlock(ref);
2198 if (unlock_err && err == NULL)
2199 err = unlock_err;
2200 got_ref_close(ref);
2202 done:
2203 free(remote_refname);
2204 return err;
2207 static const struct got_error *
2208 delete_ref(struct got_repository *repo, struct got_reference *ref)
2210 const struct got_error *err = NULL;
2211 struct got_object_id *id = NULL;
2212 char *id_str = NULL;
2213 const char *target;
2215 if (got_ref_is_symbolic(ref)) {
2216 target = got_ref_get_symref_target(ref);
2217 } else {
2218 err = got_ref_resolve(&id, repo, ref);
2219 if (err)
2220 goto done;
2221 err = got_object_id_str(&id_str, id);
2222 if (err)
2223 goto done;
2224 target = id_str;
2227 err = got_ref_delete(ref, repo);
2228 if (err)
2229 goto done;
2231 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2232 done:
2233 free(id);
2234 free(id_str);
2235 return err;
2238 static const struct got_error *
2239 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2241 const struct got_error *err = NULL;
2242 struct got_reflist_head refs;
2243 struct got_reflist_entry *re;
2244 char *prefix;
2246 TAILQ_INIT(&refs);
2248 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2249 err = got_error_from_errno("asprintf");
2250 goto done;
2252 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2253 if (err)
2254 goto done;
2256 TAILQ_FOREACH(re, &refs, entry)
2257 delete_ref(repo, re->ref);
2258 done:
2259 got_ref_list_free(&refs);
2260 return err;
2263 static const struct got_error *
2264 cmd_fetch(int argc, char *argv[])
2266 const struct got_error *error = NULL, *unlock_err;
2267 char *cwd = NULL, *repo_path = NULL;
2268 const char *remote_name;
2269 char *proto = NULL, *host = NULL, *port = NULL;
2270 char *repo_name = NULL, *server_path = NULL;
2271 const struct got_remote_repo *remotes, *remote = NULL;
2272 int nremotes;
2273 char *id_str = NULL;
2274 struct got_repository *repo = NULL;
2275 struct got_worktree *worktree = NULL;
2276 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2277 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2278 struct got_pathlist_entry *pe;
2279 struct got_object_id *pack_hash = NULL;
2280 int i, ch, fetchfd = -1, fetchstatus;
2281 pid_t fetchpid = -1;
2282 struct got_fetch_progress_arg fpa;
2283 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2284 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2285 int *pack_fds = NULL;
2287 TAILQ_INIT(&refs);
2288 TAILQ_INIT(&symrefs);
2289 TAILQ_INIT(&wanted_branches);
2290 TAILQ_INIT(&wanted_refs);
2292 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2293 switch (ch) {
2294 case 'a':
2295 fetch_all_branches = 1;
2296 break;
2297 case 'b':
2298 error = got_pathlist_append(&wanted_branches,
2299 optarg, NULL);
2300 if (error)
2301 return error;
2302 break;
2303 case 'd':
2304 delete_refs = 1;
2305 break;
2306 case 'l':
2307 list_refs_only = 1;
2308 break;
2309 case 'r':
2310 repo_path = realpath(optarg, NULL);
2311 if (repo_path == NULL)
2312 return got_error_from_errno2("realpath",
2313 optarg);
2314 got_path_strip_trailing_slashes(repo_path);
2315 break;
2316 case 't':
2317 replace_tags = 1;
2318 break;
2319 case 'v':
2320 if (verbosity < 0)
2321 verbosity = 0;
2322 else if (verbosity < 3)
2323 verbosity++;
2324 break;
2325 case 'q':
2326 verbosity = -1;
2327 break;
2328 case 'R':
2329 error = got_pathlist_append(&wanted_refs,
2330 optarg, NULL);
2331 if (error)
2332 return error;
2333 break;
2334 case 'X':
2335 delete_remote = 1;
2336 break;
2337 default:
2338 usage_fetch();
2339 break;
2342 argc -= optind;
2343 argv += optind;
2345 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2346 option_conflict('a', 'b');
2347 if (list_refs_only) {
2348 if (!TAILQ_EMPTY(&wanted_branches))
2349 option_conflict('l', 'b');
2350 if (fetch_all_branches)
2351 option_conflict('l', 'a');
2352 if (delete_refs)
2353 option_conflict('l', 'd');
2354 if (delete_remote)
2355 option_conflict('l', 'X');
2357 if (delete_remote) {
2358 if (fetch_all_branches)
2359 option_conflict('X', 'a');
2360 if (!TAILQ_EMPTY(&wanted_branches))
2361 option_conflict('X', 'b');
2362 if (delete_refs)
2363 option_conflict('X', 'd');
2364 if (replace_tags)
2365 option_conflict('X', 't');
2366 if (!TAILQ_EMPTY(&wanted_refs))
2367 option_conflict('X', 'R');
2370 if (argc == 0) {
2371 if (delete_remote)
2372 errx(1, "-X option requires a remote name");
2373 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2374 } else if (argc == 1)
2375 remote_name = argv[0];
2376 else
2377 usage_fetch();
2379 cwd = getcwd(NULL, 0);
2380 if (cwd == NULL) {
2381 error = got_error_from_errno("getcwd");
2382 goto done;
2385 error = got_repo_pack_fds_open(&pack_fds);
2386 if (error != NULL)
2387 goto done;
2389 if (repo_path == NULL) {
2390 error = got_worktree_open(&worktree, cwd);
2391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2392 goto done;
2393 else
2394 error = NULL;
2395 if (worktree) {
2396 repo_path =
2397 strdup(got_worktree_get_repo_path(worktree));
2398 if (repo_path == NULL)
2399 error = got_error_from_errno("strdup");
2400 if (error)
2401 goto done;
2402 } else {
2403 repo_path = strdup(cwd);
2404 if (repo_path == NULL) {
2405 error = got_error_from_errno("strdup");
2406 goto done;
2411 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2412 if (error)
2413 goto done;
2415 if (delete_remote) {
2416 error = delete_refs_for_remote(repo, remote_name);
2417 goto done; /* nothing else to do */
2420 if (worktree) {
2421 worktree_conf = got_worktree_get_gotconfig(worktree);
2422 if (worktree_conf) {
2423 got_gotconfig_get_remotes(&nremotes, &remotes,
2424 worktree_conf);
2425 for (i = 0; i < nremotes; i++) {
2426 if (strcmp(remotes[i].name, remote_name) == 0) {
2427 remote = &remotes[i];
2428 break;
2433 if (remote == NULL) {
2434 repo_conf = got_repo_get_gotconfig(repo);
2435 if (repo_conf) {
2436 got_gotconfig_get_remotes(&nremotes, &remotes,
2437 repo_conf);
2438 for (i = 0; i < nremotes; i++) {
2439 if (strcmp(remotes[i].name, remote_name) == 0) {
2440 remote = &remotes[i];
2441 break;
2446 if (remote == NULL) {
2447 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2448 for (i = 0; i < nremotes; i++) {
2449 if (strcmp(remotes[i].name, remote_name) == 0) {
2450 remote = &remotes[i];
2451 break;
2455 if (remote == NULL) {
2456 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2457 goto done;
2460 if (TAILQ_EMPTY(&wanted_branches)) {
2461 if (!fetch_all_branches)
2462 fetch_all_branches = remote->fetch_all_branches;
2463 for (i = 0; i < remote->nfetch_branches; i++) {
2464 got_pathlist_append(&wanted_branches,
2465 remote->fetch_branches[i], NULL);
2468 if (TAILQ_EMPTY(&wanted_refs)) {
2469 for (i = 0; i < remote->nfetch_refs; i++) {
2470 got_pathlist_append(&wanted_refs,
2471 remote->fetch_refs[i], NULL);
2475 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2476 &repo_name, remote->fetch_url);
2477 if (error)
2478 goto done;
2480 if (strcmp(proto, "git") == 0) {
2481 #ifndef PROFILE
2482 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2483 "sendfd dns inet unveil", NULL) == -1)
2484 err(1, "pledge");
2485 #endif
2486 } else if (strcmp(proto, "git+ssh") == 0 ||
2487 strcmp(proto, "ssh") == 0) {
2488 #ifndef PROFILE
2489 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2490 "sendfd unveil", NULL) == -1)
2491 err(1, "pledge");
2492 #endif
2493 } else if (strcmp(proto, "http") == 0 ||
2494 strcmp(proto, "git+http") == 0) {
2495 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2496 goto done;
2497 } else {
2498 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2499 goto done;
2502 error = got_dial_apply_unveil(proto);
2503 if (error)
2504 goto done;
2506 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2507 if (error)
2508 goto done;
2510 if (verbosity >= 0)
2511 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2512 port ? ":" : "", port ? port : "");
2514 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2515 server_path, verbosity);
2516 if (error)
2517 goto done;
2519 fpa.last_scaled_size[0] = '\0';
2520 fpa.last_p_indexed = -1;
2521 fpa.last_p_resolved = -1;
2522 fpa.verbosity = verbosity;
2523 fpa.repo = repo;
2524 fpa.create_configs = 0;
2525 fpa.configs_created = 0;
2526 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2527 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2528 remote->mirror_references, fetch_all_branches, &wanted_branches,
2529 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2530 fetch_progress, &fpa);
2531 if (error)
2532 goto done;
2534 if (list_refs_only) {
2535 error = list_remote_refs(&symrefs, &refs);
2536 goto done;
2539 if (pack_hash == NULL) {
2540 if (verbosity >= 0)
2541 printf("Already up-to-date\n");
2542 } else if (verbosity >= 0) {
2543 error = got_object_id_str(&id_str, pack_hash);
2544 if (error)
2545 goto done;
2546 printf("\nFetched %s.pack\n", id_str);
2547 free(id_str);
2548 id_str = NULL;
2551 /* Update references provided with the pack file. */
2552 TAILQ_FOREACH(pe, &refs, entry) {
2553 const char *refname = pe->path;
2554 struct got_object_id *id = pe->data;
2555 struct got_reference *ref;
2556 char *remote_refname;
2558 if (is_wanted_ref(&wanted_refs, refname) &&
2559 !remote->mirror_references) {
2560 error = update_wanted_ref(refname, id,
2561 remote->name, verbosity, repo);
2562 if (error)
2563 goto done;
2564 continue;
2567 if (remote->mirror_references ||
2568 strncmp("refs/tags/", refname, 10) == 0) {
2569 error = got_ref_open(&ref, repo, refname, 1);
2570 if (error) {
2571 if (error->code != GOT_ERR_NOT_REF)
2572 goto done;
2573 error = create_ref(refname, id, verbosity,
2574 repo);
2575 if (error)
2576 goto done;
2577 } else {
2578 error = update_ref(ref, id, replace_tags,
2579 verbosity, repo);
2580 unlock_err = got_ref_unlock(ref);
2581 if (unlock_err && error == NULL)
2582 error = unlock_err;
2583 got_ref_close(ref);
2584 if (error)
2585 goto done;
2587 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2588 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2589 remote_name, refname + 11) == -1) {
2590 error = got_error_from_errno("asprintf");
2591 goto done;
2594 error = got_ref_open(&ref, repo, remote_refname, 1);
2595 if (error) {
2596 if (error->code != GOT_ERR_NOT_REF)
2597 goto done;
2598 error = create_ref(remote_refname, id,
2599 verbosity, repo);
2600 if (error)
2601 goto done;
2602 } else {
2603 error = update_ref(ref, id, replace_tags,
2604 verbosity, repo);
2605 unlock_err = got_ref_unlock(ref);
2606 if (unlock_err && error == NULL)
2607 error = unlock_err;
2608 got_ref_close(ref);
2609 if (error)
2610 goto done;
2613 /* Also create a local branch if none exists yet. */
2614 error = got_ref_open(&ref, repo, refname, 1);
2615 if (error) {
2616 if (error->code != GOT_ERR_NOT_REF)
2617 goto done;
2618 error = create_ref(refname, id, verbosity,
2619 repo);
2620 if (error)
2621 goto done;
2622 } else {
2623 unlock_err = got_ref_unlock(ref);
2624 if (unlock_err && error == NULL)
2625 error = unlock_err;
2626 got_ref_close(ref);
2630 if (delete_refs) {
2631 error = delete_missing_refs(&refs, &symrefs, remote,
2632 verbosity, repo);
2633 if (error)
2634 goto done;
2637 if (!remote->mirror_references) {
2638 /* Update remote HEAD reference if the server provided one. */
2639 TAILQ_FOREACH(pe, &symrefs, entry) {
2640 struct got_reference *target_ref;
2641 const char *refname = pe->path;
2642 const char *target = pe->data;
2643 char *remote_refname = NULL, *remote_target = NULL;
2645 if (strcmp(refname, GOT_REF_HEAD) != 0)
2646 continue;
2648 if (strncmp("refs/heads/", target, 11) != 0)
2649 continue;
2651 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2652 remote->name, refname) == -1) {
2653 error = got_error_from_errno("asprintf");
2654 goto done;
2656 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2657 remote->name, target + 11) == -1) {
2658 error = got_error_from_errno("asprintf");
2659 free(remote_refname);
2660 goto done;
2663 error = got_ref_open(&target_ref, repo, remote_target,
2664 0);
2665 if (error) {
2666 free(remote_refname);
2667 free(remote_target);
2668 if (error->code == GOT_ERR_NOT_REF) {
2669 error = NULL;
2670 continue;
2672 goto done;
2674 error = update_symref(remote_refname, target_ref,
2675 verbosity, repo);
2676 free(remote_refname);
2677 free(remote_target);
2678 got_ref_close(target_ref);
2679 if (error)
2680 goto done;
2683 done:
2684 if (fetchpid > 0) {
2685 if (kill(fetchpid, SIGTERM) == -1)
2686 error = got_error_from_errno("kill");
2687 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2688 error = got_error_from_errno("waitpid");
2690 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2691 error = got_error_from_errno("close");
2692 if (repo) {
2693 const struct got_error *close_err = got_repo_close(repo);
2694 if (error == NULL)
2695 error = close_err;
2697 if (worktree)
2698 got_worktree_close(worktree);
2699 if (pack_fds) {
2700 const struct got_error *pack_err =
2701 got_repo_pack_fds_close(pack_fds);
2702 if (error == NULL)
2703 error = pack_err;
2705 TAILQ_FOREACH(pe, &refs, entry) {
2706 free((void *)pe->path);
2707 free(pe->data);
2709 got_pathlist_free(&refs);
2710 TAILQ_FOREACH(pe, &symrefs, entry) {
2711 free((void *)pe->path);
2712 free(pe->data);
2714 got_pathlist_free(&symrefs);
2715 got_pathlist_free(&wanted_branches);
2716 got_pathlist_free(&wanted_refs);
2717 free(id_str);
2718 free(cwd);
2719 free(repo_path);
2720 free(pack_hash);
2721 free(proto);
2722 free(host);
2723 free(port);
2724 free(server_path);
2725 free(repo_name);
2726 return error;
2730 __dead static void
2731 usage_checkout(void)
2733 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2734 "[-p prefix] [-q] repository-path [worktree-path]\n",
2735 getprogname());
2736 exit(1);
2739 static void
2740 show_worktree_base_ref_warning(void)
2742 fprintf(stderr, "%s: warning: could not create a reference "
2743 "to the work tree's base commit; the commit could be "
2744 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2745 "repository writable and running 'got update' will prevent this\n",
2746 getprogname());
2749 struct got_checkout_progress_arg {
2750 const char *worktree_path;
2751 int had_base_commit_ref_error;
2752 int verbosity;
2755 static const struct got_error *
2756 checkout_progress(void *arg, unsigned char status, const char *path)
2758 struct got_checkout_progress_arg *a = arg;
2760 /* Base commit bump happens silently. */
2761 if (status == GOT_STATUS_BUMP_BASE)
2762 return NULL;
2764 if (status == GOT_STATUS_BASE_REF_ERR) {
2765 a->had_base_commit_ref_error = 1;
2766 return NULL;
2769 while (path[0] == '/')
2770 path++;
2772 if (a->verbosity >= 0)
2773 printf("%c %s/%s\n", status, a->worktree_path, path);
2775 return NULL;
2778 static const struct got_error *
2779 check_cancelled(void *arg)
2781 if (sigint_received || sigpipe_received)
2782 return got_error(GOT_ERR_CANCELLED);
2783 return NULL;
2786 static const struct got_error *
2787 check_linear_ancestry(struct got_object_id *commit_id,
2788 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2789 struct got_repository *repo)
2791 const struct got_error *err = NULL;
2792 struct got_object_id *yca_id;
2794 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2795 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2796 if (err)
2797 return err;
2799 if (yca_id == NULL)
2800 return got_error(GOT_ERR_ANCESTRY);
2803 * Require a straight line of history between the target commit
2804 * and the work tree's base commit.
2806 * Non-linear situations such as this require a rebase:
2808 * (commit) D F (base_commit)
2809 * \ /
2810 * C E
2811 * \ /
2812 * B (yca)
2813 * |
2814 * A
2816 * 'got update' only handles linear cases:
2817 * Update forwards in time: A (base/yca) - B - C - D (commit)
2818 * Update backwards in time: D (base) - C - B - A (commit/yca)
2820 if (allow_forwards_in_time_only) {
2821 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2822 return got_error(GOT_ERR_ANCESTRY);
2823 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2824 got_object_id_cmp(base_commit_id, yca_id) != 0)
2825 return got_error(GOT_ERR_ANCESTRY);
2827 free(yca_id);
2828 return NULL;
2831 static const struct got_error *
2832 check_same_branch(struct got_object_id *commit_id,
2833 struct got_reference *head_ref, struct got_object_id *yca_id,
2834 struct got_repository *repo)
2836 const struct got_error *err = NULL;
2837 struct got_commit_graph *graph = NULL;
2838 struct got_object_id *head_commit_id = NULL;
2839 int is_same_branch = 0;
2841 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2842 if (err)
2843 goto done;
2845 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2846 is_same_branch = 1;
2847 goto done;
2849 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2850 is_same_branch = 1;
2851 goto done;
2854 err = got_commit_graph_open(&graph, "/", 1);
2855 if (err)
2856 goto done;
2858 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2859 check_cancelled, NULL);
2860 if (err)
2861 goto done;
2863 for (;;) {
2864 struct got_object_id *id;
2865 err = got_commit_graph_iter_next(&id, graph, repo,
2866 check_cancelled, NULL);
2867 if (err) {
2868 if (err->code == GOT_ERR_ITER_COMPLETED)
2869 err = NULL;
2870 break;
2873 if (id) {
2874 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2875 break;
2876 if (got_object_id_cmp(id, commit_id) == 0) {
2877 is_same_branch = 1;
2878 break;
2882 done:
2883 if (graph)
2884 got_commit_graph_close(graph);
2885 free(head_commit_id);
2886 if (!err && !is_same_branch)
2887 err = got_error(GOT_ERR_ANCESTRY);
2888 return err;
2891 static const struct got_error *
2892 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2894 static char msg[512];
2895 const char *branch_name;
2897 if (got_ref_is_symbolic(ref))
2898 branch_name = got_ref_get_symref_target(ref);
2899 else
2900 branch_name = got_ref_get_name(ref);
2902 if (strncmp("refs/heads/", branch_name, 11) == 0)
2903 branch_name += 11;
2905 snprintf(msg, sizeof(msg),
2906 "target commit is not contained in branch '%s'; "
2907 "the branch to use must be specified with -b; "
2908 "if necessary a new branch can be created for "
2909 "this commit with 'got branch -c %s BRANCH_NAME'",
2910 branch_name, commit_id_str);
2912 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2915 static const struct got_error *
2916 cmd_checkout(int argc, char *argv[])
2918 const struct got_error *error = NULL;
2919 struct got_repository *repo = NULL;
2920 struct got_reference *head_ref = NULL, *ref = NULL;
2921 struct got_worktree *worktree = NULL;
2922 char *repo_path = NULL;
2923 char *worktree_path = NULL;
2924 const char *path_prefix = "";
2925 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2926 char *commit_id_str = NULL;
2927 struct got_object_id *commit_id = NULL;
2928 char *cwd = NULL;
2929 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2930 struct got_pathlist_head paths;
2931 struct got_checkout_progress_arg cpa;
2932 int *pack_fds = NULL;
2934 TAILQ_INIT(&paths);
2936 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2937 switch (ch) {
2938 case 'b':
2939 branch_name = optarg;
2940 break;
2941 case 'c':
2942 commit_id_str = strdup(optarg);
2943 if (commit_id_str == NULL)
2944 return got_error_from_errno("strdup");
2945 break;
2946 case 'E':
2947 allow_nonempty = 1;
2948 break;
2949 case 'p':
2950 path_prefix = optarg;
2951 break;
2952 case 'q':
2953 verbosity = -1;
2954 break;
2955 default:
2956 usage_checkout();
2957 /* NOTREACHED */
2961 argc -= optind;
2962 argv += optind;
2964 #ifndef PROFILE
2965 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2966 "unveil", NULL) == -1)
2967 err(1, "pledge");
2968 #endif
2969 if (argc == 1) {
2970 char *base, *dotgit;
2971 const char *path;
2972 repo_path = realpath(argv[0], NULL);
2973 if (repo_path == NULL)
2974 return got_error_from_errno2("realpath", argv[0]);
2975 cwd = getcwd(NULL, 0);
2976 if (cwd == NULL) {
2977 error = got_error_from_errno("getcwd");
2978 goto done;
2980 if (path_prefix[0])
2981 path = path_prefix;
2982 else
2983 path = repo_path;
2984 error = got_path_basename(&base, path);
2985 if (error)
2986 goto done;
2987 dotgit = strstr(base, ".git");
2988 if (dotgit)
2989 *dotgit = '\0';
2990 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2991 error = got_error_from_errno("asprintf");
2992 free(base);
2993 goto done;
2995 free(base);
2996 } else if (argc == 2) {
2997 repo_path = realpath(argv[0], NULL);
2998 if (repo_path == NULL) {
2999 error = got_error_from_errno2("realpath", argv[0]);
3000 goto done;
3002 worktree_path = realpath(argv[1], NULL);
3003 if (worktree_path == NULL) {
3004 if (errno != ENOENT) {
3005 error = got_error_from_errno2("realpath",
3006 argv[1]);
3007 goto done;
3009 worktree_path = strdup(argv[1]);
3010 if (worktree_path == NULL) {
3011 error = got_error_from_errno("strdup");
3012 goto done;
3015 } else
3016 usage_checkout();
3018 got_path_strip_trailing_slashes(repo_path);
3019 got_path_strip_trailing_slashes(worktree_path);
3021 error = got_repo_pack_fds_open(&pack_fds);
3022 if (error != NULL)
3023 goto done;
3025 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3026 if (error != NULL)
3027 goto done;
3029 /* Pre-create work tree path for unveil(2) */
3030 error = got_path_mkdir(worktree_path);
3031 if (error) {
3032 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3033 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3034 goto done;
3035 if (!allow_nonempty &&
3036 !got_path_dir_is_empty(worktree_path)) {
3037 error = got_error_path(worktree_path,
3038 GOT_ERR_DIR_NOT_EMPTY);
3039 goto done;
3043 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3044 if (error)
3045 goto done;
3047 error = got_ref_open(&head_ref, repo, branch_name, 0);
3048 if (error != NULL)
3049 goto done;
3051 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3052 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3053 goto done;
3055 error = got_worktree_open(&worktree, worktree_path);
3056 if (error != NULL)
3057 goto done;
3059 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3060 path_prefix);
3061 if (error != NULL)
3062 goto done;
3063 if (!same_path_prefix) {
3064 error = got_error(GOT_ERR_PATH_PREFIX);
3065 goto done;
3068 if (commit_id_str) {
3069 struct got_reflist_head refs;
3070 TAILQ_INIT(&refs);
3071 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3072 NULL);
3073 if (error)
3074 goto done;
3075 error = got_repo_match_object_id(&commit_id, NULL,
3076 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3077 got_ref_list_free(&refs);
3078 if (error)
3079 goto done;
3080 error = check_linear_ancestry(commit_id,
3081 got_worktree_get_base_commit_id(worktree), 0, repo);
3082 if (error != NULL) {
3083 if (error->code == GOT_ERR_ANCESTRY) {
3084 error = checkout_ancestry_error(
3085 head_ref, commit_id_str);
3087 goto done;
3089 error = check_same_branch(commit_id, head_ref, NULL, repo);
3090 if (error) {
3091 if (error->code == GOT_ERR_ANCESTRY) {
3092 error = checkout_ancestry_error(
3093 head_ref, commit_id_str);
3095 goto done;
3097 error = got_worktree_set_base_commit_id(worktree, repo,
3098 commit_id);
3099 if (error)
3100 goto done;
3101 /* Expand potentially abbreviated commit ID string. */
3102 free(commit_id_str);
3103 error = got_object_id_str(&commit_id_str, commit_id);
3104 if (error)
3105 goto done;
3106 } else {
3107 commit_id = got_object_id_dup(
3108 got_worktree_get_base_commit_id(worktree));
3109 if (commit_id == NULL) {
3110 error = got_error_from_errno("got_object_id_dup");
3111 goto done;
3113 error = got_object_id_str(&commit_id_str, commit_id);
3114 if (error)
3115 goto done;
3118 error = got_pathlist_append(&paths, "", NULL);
3119 if (error)
3120 goto done;
3121 cpa.worktree_path = worktree_path;
3122 cpa.had_base_commit_ref_error = 0;
3123 cpa.verbosity = verbosity;
3124 error = got_worktree_checkout_files(worktree, &paths, repo,
3125 checkout_progress, &cpa, check_cancelled, NULL);
3126 if (error != NULL)
3127 goto done;
3129 if (got_ref_is_symbolic(head_ref)) {
3130 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3131 if (error)
3132 goto done;
3133 refname = got_ref_get_name(ref);
3134 } else
3135 refname = got_ref_get_name(head_ref);
3136 printf("Checked out %s: %s\n", refname, commit_id_str);
3137 printf("Now shut up and hack\n");
3138 if (cpa.had_base_commit_ref_error)
3139 show_worktree_base_ref_warning();
3140 done:
3141 if (pack_fds) {
3142 const struct got_error *pack_err =
3143 got_repo_pack_fds_close(pack_fds);
3144 if (error == NULL)
3145 error = pack_err;
3147 if (head_ref)
3148 got_ref_close(head_ref);
3149 if (ref)
3150 got_ref_close(ref);
3151 got_pathlist_free(&paths);
3152 free(commit_id_str);
3153 free(commit_id);
3154 free(repo_path);
3155 free(worktree_path);
3156 free(cwd);
3157 return error;
3160 struct got_update_progress_arg {
3161 int did_something;
3162 int conflicts;
3163 int obstructed;
3164 int not_updated;
3165 int missing;
3166 int not_deleted;
3167 int unversioned;
3168 int verbosity;
3171 static void
3172 print_update_progress_stats(struct got_update_progress_arg *upa)
3174 if (!upa->did_something)
3175 return;
3177 if (upa->conflicts > 0)
3178 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3179 if (upa->obstructed > 0)
3180 printf("File paths obstructed by a non-regular file: %d\n",
3181 upa->obstructed);
3182 if (upa->not_updated > 0)
3183 printf("Files not updated because of existing merge "
3184 "conflicts: %d\n", upa->not_updated);
3188 * The meaning of some status codes differs between merge-style operations and
3189 * update operations. For example, the ! status code means "file was missing"
3190 * if changes were merged into the work tree, and "missing file was restored"
3191 * if the work tree was updated. This function should be used by any operation
3192 * which merges changes into the work tree without updating the work tree.
3194 static void
3195 print_merge_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->missing > 0)
3206 printf("Files which had incoming changes but could not be "
3207 "found in the work tree: %d\n", upa->missing);
3208 if (upa->not_deleted > 0)
3209 printf("Files not deleted due to differences in deleted "
3210 "content: %d\n", upa->not_deleted);
3211 if (upa->unversioned > 0)
3212 printf("Files not merged because an unversioned file was "
3213 "found in the work tree: %d\n", upa->unversioned);
3216 __dead static void
3217 usage_update(void)
3219 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3220 "[path ...]\n",
3221 getprogname());
3222 exit(1);
3225 static const struct got_error *
3226 update_progress(void *arg, unsigned char status, const char *path)
3228 struct got_update_progress_arg *upa = arg;
3230 if (status == GOT_STATUS_EXISTS ||
3231 status == GOT_STATUS_BASE_REF_ERR)
3232 return NULL;
3234 upa->did_something = 1;
3236 /* Base commit bump happens silently. */
3237 if (status == GOT_STATUS_BUMP_BASE)
3238 return NULL;
3240 if (status == GOT_STATUS_CONFLICT)
3241 upa->conflicts++;
3242 if (status == GOT_STATUS_OBSTRUCTED)
3243 upa->obstructed++;
3244 if (status == GOT_STATUS_CANNOT_UPDATE)
3245 upa->not_updated++;
3246 if (status == GOT_STATUS_MISSING)
3247 upa->missing++;
3248 if (status == GOT_STATUS_CANNOT_DELETE)
3249 upa->not_deleted++;
3250 if (status == GOT_STATUS_UNVERSIONED)
3251 upa->unversioned++;
3253 while (path[0] == '/')
3254 path++;
3255 if (upa->verbosity >= 0)
3256 printf("%c %s\n", status, path);
3258 return NULL;
3261 static const struct got_error *
3262 switch_head_ref(struct got_reference *head_ref,
3263 struct got_object_id *commit_id, struct got_worktree *worktree,
3264 struct got_repository *repo)
3266 const struct got_error *err = NULL;
3267 char *base_id_str;
3268 int ref_has_moved = 0;
3270 /* Trivial case: switching between two different references. */
3271 if (strcmp(got_ref_get_name(head_ref),
3272 got_worktree_get_head_ref_name(worktree)) != 0) {
3273 printf("Switching work tree from %s to %s\n",
3274 got_worktree_get_head_ref_name(worktree),
3275 got_ref_get_name(head_ref));
3276 return got_worktree_set_head_ref(worktree, head_ref);
3279 err = check_linear_ancestry(commit_id,
3280 got_worktree_get_base_commit_id(worktree), 0, repo);
3281 if (err) {
3282 if (err->code != GOT_ERR_ANCESTRY)
3283 return err;
3284 ref_has_moved = 1;
3286 if (!ref_has_moved)
3287 return NULL;
3289 /* Switching to a rebased branch with the same reference name. */
3290 err = got_object_id_str(&base_id_str,
3291 got_worktree_get_base_commit_id(worktree));
3292 if (err)
3293 return err;
3294 printf("Reference %s now points at a different branch\n",
3295 got_worktree_get_head_ref_name(worktree));
3296 printf("Switching work tree from %s to %s\n", base_id_str,
3297 got_worktree_get_head_ref_name(worktree));
3298 return NULL;
3301 static const struct got_error *
3302 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3304 const struct got_error *err;
3305 int in_progress;
3307 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3308 if (err)
3309 return err;
3310 if (in_progress)
3311 return got_error(GOT_ERR_REBASING);
3313 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3314 if (err)
3315 return err;
3316 if (in_progress)
3317 return got_error(GOT_ERR_HISTEDIT_BUSY);
3319 return NULL;
3322 static const struct got_error *
3323 check_merge_in_progress(struct got_worktree *worktree,
3324 struct got_repository *repo)
3326 const struct got_error *err;
3327 int in_progress;
3329 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3330 if (err)
3331 return err;
3332 if (in_progress)
3333 return got_error(GOT_ERR_MERGE_BUSY);
3335 return NULL;
3338 static const struct got_error *
3339 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3340 char *argv[], struct got_worktree *worktree)
3342 const struct got_error *err = NULL;
3343 char *path;
3344 struct got_pathlist_entry *new;
3345 int i;
3347 if (argc == 0) {
3348 path = strdup("");
3349 if (path == NULL)
3350 return got_error_from_errno("strdup");
3351 return got_pathlist_append(paths, path, NULL);
3354 for (i = 0; i < argc; i++) {
3355 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3356 if (err)
3357 break;
3358 err = got_pathlist_insert(&new, paths, path, NULL);
3359 if (err || new == NULL /* duplicate */) {
3360 free(path);
3361 if (err)
3362 break;
3366 return err;
3369 static const struct got_error *
3370 wrap_not_worktree_error(const struct got_error *orig_err,
3371 const char *cmdname, const char *path)
3373 const struct got_error *err;
3374 struct got_repository *repo;
3375 static char msg[512];
3376 int *pack_fds = NULL;
3378 err = got_repo_pack_fds_open(&pack_fds);
3379 if (err)
3380 return err;
3382 err = got_repo_open(&repo, path, NULL, pack_fds);
3383 if (err)
3384 return orig_err;
3386 snprintf(msg, sizeof(msg),
3387 "'got %s' needs a work tree in addition to a git repository\n"
3388 "Work trees can be checked out from this Git repository with "
3389 "'got checkout'.\n"
3390 "The got(1) manual page contains more information.", cmdname);
3391 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3392 got_repo_close(repo);
3393 if (pack_fds) {
3394 const struct got_error *pack_err =
3395 got_repo_pack_fds_close(pack_fds);
3396 if (err == NULL)
3397 err = pack_err;
3399 return err;
3402 static const struct got_error *
3403 cmd_update(int argc, char *argv[])
3405 const struct got_error *error = NULL;
3406 struct got_repository *repo = NULL;
3407 struct got_worktree *worktree = NULL;
3408 char *worktree_path = NULL;
3409 struct got_object_id *commit_id = NULL;
3410 char *commit_id_str = NULL;
3411 const char *branch_name = NULL;
3412 struct got_reference *head_ref = NULL;
3413 struct got_pathlist_head paths;
3414 struct got_pathlist_entry *pe;
3415 int ch, verbosity = 0;
3416 struct got_update_progress_arg upa;
3417 int *pack_fds = NULL;
3419 TAILQ_INIT(&paths);
3421 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3422 switch (ch) {
3423 case 'b':
3424 branch_name = optarg;
3425 break;
3426 case 'c':
3427 commit_id_str = strdup(optarg);
3428 if (commit_id_str == NULL)
3429 return got_error_from_errno("strdup");
3430 break;
3431 case 'q':
3432 verbosity = -1;
3433 break;
3434 default:
3435 usage_update();
3436 /* NOTREACHED */
3440 argc -= optind;
3441 argv += optind;
3443 #ifndef PROFILE
3444 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3445 "unveil", NULL) == -1)
3446 err(1, "pledge");
3447 #endif
3448 worktree_path = getcwd(NULL, 0);
3449 if (worktree_path == NULL) {
3450 error = got_error_from_errno("getcwd");
3451 goto done;
3454 error = got_repo_pack_fds_open(&pack_fds);
3455 if (error != NULL)
3456 goto done;
3458 error = got_worktree_open(&worktree, worktree_path);
3459 if (error) {
3460 if (error->code == GOT_ERR_NOT_WORKTREE)
3461 error = wrap_not_worktree_error(error, "update",
3462 worktree_path);
3463 goto done;
3466 error = check_rebase_or_histedit_in_progress(worktree);
3467 if (error)
3468 goto done;
3470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3471 NULL, pack_fds);
3472 if (error != NULL)
3473 goto done;
3475 error = apply_unveil(got_repo_get_path(repo), 0,
3476 got_worktree_get_root_path(worktree));
3477 if (error)
3478 goto done;
3480 error = check_merge_in_progress(worktree, repo);
3481 if (error)
3482 goto done;
3484 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3485 if (error)
3486 goto done;
3488 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3489 got_worktree_get_head_ref_name(worktree), 0);
3490 if (error != NULL)
3491 goto done;
3492 if (commit_id_str == NULL) {
3493 error = got_ref_resolve(&commit_id, repo, head_ref);
3494 if (error != NULL)
3495 goto done;
3496 error = got_object_id_str(&commit_id_str, commit_id);
3497 if (error != NULL)
3498 goto done;
3499 } else {
3500 struct got_reflist_head refs;
3501 TAILQ_INIT(&refs);
3502 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3503 NULL);
3504 if (error)
3505 goto done;
3506 error = got_repo_match_object_id(&commit_id, NULL,
3507 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3508 got_ref_list_free(&refs);
3509 free(commit_id_str);
3510 commit_id_str = NULL;
3511 if (error)
3512 goto done;
3513 error = got_object_id_str(&commit_id_str, commit_id);
3514 if (error)
3515 goto done;
3518 if (branch_name) {
3519 struct got_object_id *head_commit_id;
3520 TAILQ_FOREACH(pe, &paths, entry) {
3521 if (pe->path_len == 0)
3522 continue;
3523 error = got_error_msg(GOT_ERR_BAD_PATH,
3524 "switching between branches requires that "
3525 "the entire work tree gets updated");
3526 goto done;
3528 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3529 if (error)
3530 goto done;
3531 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3532 repo);
3533 free(head_commit_id);
3534 if (error != NULL)
3535 goto done;
3536 error = check_same_branch(commit_id, head_ref, NULL, repo);
3537 if (error)
3538 goto done;
3539 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3540 if (error)
3541 goto done;
3542 } else {
3543 error = check_linear_ancestry(commit_id,
3544 got_worktree_get_base_commit_id(worktree), 0, repo);
3545 if (error != NULL) {
3546 if (error->code == GOT_ERR_ANCESTRY)
3547 error = got_error(GOT_ERR_BRANCH_MOVED);
3548 goto done;
3550 error = check_same_branch(commit_id, head_ref, NULL, repo);
3551 if (error)
3552 goto done;
3555 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3556 commit_id) != 0) {
3557 error = got_worktree_set_base_commit_id(worktree, repo,
3558 commit_id);
3559 if (error)
3560 goto done;
3563 memset(&upa, 0, sizeof(upa));
3564 upa.verbosity = verbosity;
3565 error = got_worktree_checkout_files(worktree, &paths, repo,
3566 update_progress, &upa, check_cancelled, NULL);
3567 if (error != NULL)
3568 goto done;
3570 if (upa.did_something) {
3571 printf("Updated to %s: %s\n",
3572 got_worktree_get_head_ref_name(worktree), commit_id_str);
3573 } else
3574 printf("Already up-to-date\n");
3576 print_update_progress_stats(&upa);
3577 done:
3578 if (pack_fds) {
3579 const struct got_error *pack_err =
3580 got_repo_pack_fds_close(pack_fds);
3581 if (error == NULL)
3582 error = pack_err;
3584 free(worktree_path);
3585 TAILQ_FOREACH(pe, &paths, entry)
3586 free((char *)pe->path);
3587 got_pathlist_free(&paths);
3588 free(commit_id);
3589 free(commit_id_str);
3590 return error;
3593 static const struct got_error *
3594 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3595 const char *path, int diff_context, int ignore_whitespace,
3596 int force_text_diff, struct got_repository *repo, FILE *outfile)
3598 const struct got_error *err = NULL;
3599 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3600 FILE *f1 = NULL, *f2 = NULL;
3601 int fd1 = -1, fd2 = -1;
3603 fd1 = got_opentempfd();
3604 if (fd1 == -1)
3605 return got_error_from_errno("got_opentempfd");
3606 fd2 = got_opentempfd();
3607 if (fd2 == -1) {
3608 err = got_error_from_errno("got_opentempfd");
3609 goto done;
3612 if (blob_id1) {
3613 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3614 fd1);
3615 if (err)
3616 goto done;
3619 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3620 if (err)
3621 goto done;
3623 f1 = got_opentemp();
3624 if (f1 == NULL) {
3625 err = got_error_from_errno("got_opentemp");
3626 goto done;
3628 f2 = got_opentemp();
3629 if (f2 == NULL) {
3630 err = got_error_from_errno("got_opentemp");
3631 goto done;
3634 while (path[0] == '/')
3635 path++;
3636 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3637 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3638 force_text_diff, outfile);
3639 done:
3640 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3641 err = got_error_from_errno("close");
3642 if (blob1)
3643 got_object_blob_close(blob1);
3644 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3645 err = got_error_from_errno("close");
3646 got_object_blob_close(blob2);
3647 if (f1 && fclose(f1) == EOF && err == NULL)
3648 err = got_error_from_errno("fclose");
3649 if (f2 && fclose(f2) == EOF && err == NULL)
3650 err = got_error_from_errno("fclose");
3651 return err;
3654 static const struct got_error *
3655 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3656 const char *path, int diff_context, int ignore_whitespace,
3657 int force_text_diff, struct got_repository *repo, FILE *outfile)
3659 const struct got_error *err = NULL;
3660 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3661 struct got_diff_blob_output_unidiff_arg arg;
3662 FILE *f1 = NULL, *f2 = NULL;
3663 int fd1 = -1, fd2 = -1;
3665 if (tree_id1) {
3666 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3667 if (err)
3668 goto done;
3669 fd1 = got_opentempfd();
3670 if (fd1 == -1) {
3671 err = got_error_from_errno("got_opentempfd");
3672 goto done;
3676 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3677 if (err)
3678 goto done;
3680 f1 = got_opentemp();
3681 if (f1 == NULL) {
3682 err = got_error_from_errno("got_opentemp");
3683 goto done;
3686 f2 = got_opentemp();
3687 if (f2 == NULL) {
3688 err = got_error_from_errno("got_opentemp");
3689 goto done;
3691 fd2 = got_opentempfd();
3692 if (fd2 == -1) {
3693 err = got_error_from_errno("got_opentempfd");
3694 goto done;
3696 arg.diff_context = diff_context;
3697 arg.ignore_whitespace = ignore_whitespace;
3698 arg.force_text_diff = force_text_diff;
3699 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3700 arg.outfile = outfile;
3701 arg.line_offsets = NULL;
3702 arg.nlines = 0;
3703 while (path[0] == '/')
3704 path++;
3705 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3706 got_diff_blob_output_unidiff, &arg, 1);
3707 done:
3708 if (tree1)
3709 got_object_tree_close(tree1);
3710 if (tree2)
3711 got_object_tree_close(tree2);
3712 if (f1 && fclose(f1) == EOF && err == NULL)
3713 err = got_error_from_errno("fclose");
3714 if (f2 && fclose(f2) == EOF && err == NULL)
3715 err = got_error_from_errno("fclose");
3716 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3717 err = got_error_from_errno("close");
3718 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3719 err = got_error_from_errno("close");
3720 return err;
3723 static const struct got_error *
3724 get_changed_paths(struct got_pathlist_head *paths,
3725 struct got_commit_object *commit, struct got_repository *repo)
3727 const struct got_error *err = NULL;
3728 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3729 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3730 struct got_object_qid *qid;
3732 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3733 if (qid != NULL) {
3734 struct got_commit_object *pcommit;
3735 err = got_object_open_as_commit(&pcommit, repo,
3736 &qid->id);
3737 if (err)
3738 return err;
3740 tree_id1 = got_object_id_dup(
3741 got_object_commit_get_tree_id(pcommit));
3742 if (tree_id1 == NULL) {
3743 got_object_commit_close(pcommit);
3744 return got_error_from_errno("got_object_id_dup");
3746 got_object_commit_close(pcommit);
3750 if (tree_id1) {
3751 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3752 if (err)
3753 goto done;
3756 tree_id2 = got_object_commit_get_tree_id(commit);
3757 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3758 if (err)
3759 goto done;
3761 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3762 got_diff_tree_collect_changed_paths, paths, 0);
3763 done:
3764 if (tree1)
3765 got_object_tree_close(tree1);
3766 if (tree2)
3767 got_object_tree_close(tree2);
3768 free(tree_id1);
3769 return err;
3772 static const struct got_error *
3773 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3774 const char *path, int diff_context, struct got_repository *repo,
3775 FILE *outfile)
3777 const struct got_error *err = NULL;
3778 struct got_commit_object *pcommit = NULL;
3779 char *id_str1 = NULL, *id_str2 = NULL;
3780 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3781 struct got_object_qid *qid;
3783 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3784 if (qid != NULL) {
3785 err = got_object_open_as_commit(&pcommit, repo,
3786 &qid->id);
3787 if (err)
3788 return err;
3789 err = got_object_id_str(&id_str1, &qid->id);
3790 if (err)
3791 goto done;
3794 err = got_object_id_str(&id_str2, id);
3795 if (err)
3796 goto done;
3798 if (path && path[0] != '\0') {
3799 int obj_type;
3800 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3801 if (err)
3802 goto done;
3803 if (pcommit) {
3804 err = got_object_id_by_path(&obj_id1, repo,
3805 pcommit, path);
3806 if (err) {
3807 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3808 free(obj_id2);
3809 goto done;
3813 err = got_object_get_type(&obj_type, repo, obj_id2);
3814 if (err) {
3815 free(obj_id2);
3816 goto done;
3818 fprintf(outfile,
3819 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3820 fprintf(outfile, "commit - %s\n",
3821 id_str1 ? id_str1 : "/dev/null");
3822 fprintf(outfile, "commit + %s\n", id_str2);
3823 switch (obj_type) {
3824 case GOT_OBJ_TYPE_BLOB:
3825 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3826 0, 0, repo, outfile);
3827 break;
3828 case GOT_OBJ_TYPE_TREE:
3829 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3830 0, 0, repo, outfile);
3831 break;
3832 default:
3833 err = got_error(GOT_ERR_OBJ_TYPE);
3834 break;
3836 free(obj_id1);
3837 free(obj_id2);
3838 } else {
3839 obj_id2 = got_object_commit_get_tree_id(commit);
3840 if (pcommit)
3841 obj_id1 = got_object_commit_get_tree_id(pcommit);
3842 fprintf(outfile,
3843 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3844 fprintf(outfile, "commit - %s\n",
3845 id_str1 ? id_str1 : "/dev/null");
3846 fprintf(outfile, "commit + %s\n", id_str2);
3847 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3848 repo, outfile);
3850 done:
3851 free(id_str1);
3852 free(id_str2);
3853 if (pcommit)
3854 got_object_commit_close(pcommit);
3855 return err;
3858 static char *
3859 get_datestr(time_t *time, char *datebuf)
3861 struct tm mytm, *tm;
3862 char *p, *s;
3864 tm = gmtime_r(time, &mytm);
3865 if (tm == NULL)
3866 return NULL;
3867 s = asctime_r(tm, datebuf);
3868 if (s == NULL)
3869 return NULL;
3870 p = strchr(s, '\n');
3871 if (p)
3872 *p = '\0';
3873 return s;
3876 static const struct got_error *
3877 match_commit(int *have_match, struct got_object_id *id,
3878 struct got_commit_object *commit, regex_t *regex)
3880 const struct got_error *err = NULL;
3881 regmatch_t regmatch;
3882 char *id_str = NULL, *logmsg = NULL;
3884 *have_match = 0;
3886 err = got_object_id_str(&id_str, id);
3887 if (err)
3888 return err;
3890 err = got_object_commit_get_logmsg(&logmsg, commit);
3891 if (err)
3892 goto done;
3894 if (regexec(regex, got_object_commit_get_author(commit), 1,
3895 &regmatch, 0) == 0 ||
3896 regexec(regex, got_object_commit_get_committer(commit), 1,
3897 &regmatch, 0) == 0 ||
3898 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3899 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3900 *have_match = 1;
3901 done:
3902 free(id_str);
3903 free(logmsg);
3904 return err;
3907 static void
3908 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3909 regex_t *regex)
3911 regmatch_t regmatch;
3912 struct got_pathlist_entry *pe;
3914 *have_match = 0;
3916 TAILQ_FOREACH(pe, changed_paths, entry) {
3917 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3918 *have_match = 1;
3919 break;
3924 static const struct got_error *
3925 match_patch(int *have_match, struct got_commit_object *commit,
3926 struct got_object_id *id, const char *path, int diff_context,
3927 struct got_repository *repo, regex_t *regex, FILE *f)
3929 const struct got_error *err = NULL;
3930 char *line = NULL;
3931 size_t linesize = 0;
3932 ssize_t linelen;
3933 regmatch_t regmatch;
3935 *have_match = 0;
3937 err = got_opentemp_truncate(f);
3938 if (err)
3939 return err;
3941 err = print_patch(commit, id, path, diff_context, repo, f);
3942 if (err)
3943 goto done;
3945 if (fseeko(f, 0L, SEEK_SET) == -1) {
3946 err = got_error_from_errno("fseeko");
3947 goto done;
3950 while ((linelen = getline(&line, &linesize, f)) != -1) {
3951 if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3952 *have_match = 1;
3953 break;
3956 done:
3957 free(line);
3958 return err;
3961 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3963 static const struct got_error*
3964 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3965 struct got_object_id *id, struct got_repository *repo,
3966 int local_only)
3968 static const struct got_error *err = NULL;
3969 struct got_reflist_entry *re;
3970 char *s;
3971 const char *name;
3973 *refs_str = NULL;
3975 TAILQ_FOREACH(re, refs, entry) {
3976 struct got_tag_object *tag = NULL;
3977 struct got_object_id *ref_id;
3978 int cmp;
3980 name = got_ref_get_name(re->ref);
3981 if (strcmp(name, GOT_REF_HEAD) == 0)
3982 continue;
3983 if (strncmp(name, "refs/", 5) == 0)
3984 name += 5;
3985 if (strncmp(name, "got/", 4) == 0)
3986 continue;
3987 if (strncmp(name, "heads/", 6) == 0)
3988 name += 6;
3989 if (strncmp(name, "remotes/", 8) == 0) {
3990 if (local_only)
3991 continue;
3992 name += 8;
3993 s = strstr(name, "/" GOT_REF_HEAD);
3994 if (s != NULL && s[strlen(s)] == '\0')
3995 continue;
3997 err = got_ref_resolve(&ref_id, repo, re->ref);
3998 if (err)
3999 break;
4000 if (strncmp(name, "tags/", 5) == 0) {
4001 err = got_object_open_as_tag(&tag, repo, ref_id);
4002 if (err) {
4003 if (err->code != GOT_ERR_OBJ_TYPE) {
4004 free(ref_id);
4005 break;
4007 /* Ref points at something other than a tag. */
4008 err = NULL;
4009 tag = NULL;
4012 cmp = got_object_id_cmp(tag ?
4013 got_object_tag_get_object_id(tag) : ref_id, id);
4014 free(ref_id);
4015 if (tag)
4016 got_object_tag_close(tag);
4017 if (cmp != 0)
4018 continue;
4019 s = *refs_str;
4020 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4021 s ? ", " : "", name) == -1) {
4022 err = got_error_from_errno("asprintf");
4023 free(s);
4024 *refs_str = NULL;
4025 break;
4027 free(s);
4030 return err;
4033 static const struct got_error *
4034 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4035 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4037 const struct got_error *err = NULL;
4038 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4039 char *comma, *s, *nl;
4040 struct got_reflist_head *refs;
4041 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4042 struct tm tm;
4043 time_t committer_time;
4045 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4046 if (refs) {
4047 err = build_refs_str(&ref_str, refs, id, repo, 1);
4048 if (err)
4049 return err;
4051 /* Display the first matching ref only. */
4052 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4053 *comma = '\0';
4056 if (ref_str == NULL) {
4057 err = got_object_id_str(&id_str, id);
4058 if (err)
4059 return err;
4062 committer_time = got_object_commit_get_committer_time(commit);
4063 if (gmtime_r(&committer_time, &tm) == NULL) {
4064 err = got_error_from_errno("gmtime_r");
4065 goto done;
4067 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4068 err = got_error(GOT_ERR_NO_SPACE);
4069 goto done;
4072 err = got_object_commit_get_logmsg(&logmsg0, commit);
4073 if (err)
4074 goto done;
4076 s = logmsg0;
4077 while (isspace((unsigned char)s[0]))
4078 s++;
4080 nl = strchr(s, '\n');
4081 if (nl) {
4082 *nl = '\0';
4085 if (ref_str)
4086 printf("%s%-7s %s\n", datebuf, ref_str, s);
4087 else
4088 printf("%s%.7s %s\n", datebuf, id_str, s);
4090 if (fflush(stdout) != 0 && err == NULL)
4091 err = got_error_from_errno("fflush");
4092 done:
4093 free(id_str);
4094 free(ref_str);
4095 free(logmsg0);
4096 return err;
4099 static const struct got_error *
4100 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4101 struct got_repository *repo, const char *path,
4102 struct got_pathlist_head *changed_paths, int show_patch,
4103 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4104 const char *custom_refs_str)
4106 const struct got_error *err = NULL;
4107 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4108 char datebuf[26];
4109 time_t committer_time;
4110 const char *author, *committer;
4111 char *refs_str = NULL;
4113 err = got_object_id_str(&id_str, id);
4114 if (err)
4115 return err;
4117 if (custom_refs_str == NULL) {
4118 struct got_reflist_head *refs;
4119 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4120 if (refs) {
4121 err = build_refs_str(&refs_str, refs, id, repo, 0);
4122 if (err)
4123 goto done;
4127 printf(GOT_COMMIT_SEP_STR);
4128 if (custom_refs_str)
4129 printf("commit %s (%s)\n", id_str, custom_refs_str);
4130 else
4131 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4132 refs_str ? refs_str : "", refs_str ? ")" : "");
4133 free(id_str);
4134 id_str = NULL;
4135 free(refs_str);
4136 refs_str = NULL;
4137 printf("from: %s\n", got_object_commit_get_author(commit));
4138 committer_time = got_object_commit_get_committer_time(commit);
4139 datestr = get_datestr(&committer_time, datebuf);
4140 if (datestr)
4141 printf("date: %s UTC\n", datestr);
4142 author = got_object_commit_get_author(commit);
4143 committer = got_object_commit_get_committer(commit);
4144 if (strcmp(author, committer) != 0)
4145 printf("via: %s\n", committer);
4146 if (got_object_commit_get_nparents(commit) > 1) {
4147 const struct got_object_id_queue *parent_ids;
4148 struct got_object_qid *qid;
4149 int n = 1;
4150 parent_ids = got_object_commit_get_parent_ids(commit);
4151 STAILQ_FOREACH(qid, parent_ids, entry) {
4152 err = got_object_id_str(&id_str, &qid->id);
4153 if (err)
4154 goto done;
4155 printf("parent %d: %s\n", n++, id_str);
4156 free(id_str);
4157 id_str = NULL;
4161 err = got_object_commit_get_logmsg(&logmsg0, commit);
4162 if (err)
4163 goto done;
4165 logmsg = logmsg0;
4166 do {
4167 line = strsep(&logmsg, "\n");
4168 if (line)
4169 printf(" %s\n", line);
4170 } while (line);
4171 free(logmsg0);
4173 if (changed_paths) {
4174 struct got_pathlist_entry *pe;
4175 TAILQ_FOREACH(pe, changed_paths, entry) {
4176 struct got_diff_changed_path *cp = pe->data;
4177 printf(" %c %s\n", cp->status, pe->path);
4179 printf("\n");
4181 if (show_patch) {
4182 err = print_patch(commit, id, path, diff_context, repo, stdout);
4183 if (err == 0)
4184 printf("\n");
4187 if (fflush(stdout) != 0 && err == NULL)
4188 err = got_error_from_errno("fflush");
4189 done:
4190 free(id_str);
4191 free(refs_str);
4192 return err;
4195 static const struct got_error *
4196 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4197 struct got_repository *repo, const char *path, int show_changed_paths,
4198 int show_patch, const char *search_pattern, int diff_context, int limit,
4199 int log_branches, int reverse_display_order,
4200 struct got_reflist_object_id_map *refs_idmap, int one_line,
4201 FILE *tmpfile)
4203 const struct got_error *err;
4204 struct got_commit_graph *graph;
4205 regex_t regex;
4206 int have_match;
4207 struct got_object_id_queue reversed_commits;
4208 struct got_object_qid *qid;
4209 struct got_commit_object *commit;
4210 struct got_pathlist_head changed_paths;
4211 struct got_pathlist_entry *pe;
4213 STAILQ_INIT(&reversed_commits);
4214 TAILQ_INIT(&changed_paths);
4216 if (search_pattern && regcomp(&regex, search_pattern,
4217 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4218 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4220 err = got_commit_graph_open(&graph, path, !log_branches);
4221 if (err)
4222 return err;
4223 err = got_commit_graph_iter_start(graph, root_id, repo,
4224 check_cancelled, NULL);
4225 if (err)
4226 goto done;
4227 for (;;) {
4228 struct got_object_id *id;
4230 if (sigint_received || sigpipe_received)
4231 break;
4233 err = got_commit_graph_iter_next(&id, graph, repo,
4234 check_cancelled, NULL);
4235 if (err) {
4236 if (err->code == GOT_ERR_ITER_COMPLETED)
4237 err = NULL;
4238 break;
4240 if (id == NULL)
4241 break;
4243 err = got_object_open_as_commit(&commit, repo, id);
4244 if (err)
4245 break;
4247 if (show_changed_paths && !reverse_display_order) {
4248 err = get_changed_paths(&changed_paths, commit, repo);
4249 if (err)
4250 break;
4253 if (search_pattern) {
4254 err = match_commit(&have_match, id, commit, &regex);
4255 if (err) {
4256 got_object_commit_close(commit);
4257 break;
4259 if (have_match == 0 && show_changed_paths)
4260 match_changed_paths(&have_match,
4261 &changed_paths, &regex);
4262 if (have_match == 0 && show_patch) {
4263 err = match_patch(&have_match, commit, id,
4264 path, diff_context, repo, &regex,
4265 tmpfile);
4266 if (err)
4267 break;
4269 if (have_match == 0) {
4270 got_object_commit_close(commit);
4271 TAILQ_FOREACH(pe, &changed_paths, entry) {
4272 free((char *)pe->path);
4273 free(pe->data);
4275 got_pathlist_free(&changed_paths);
4276 continue;
4280 if (reverse_display_order) {
4281 err = got_object_qid_alloc(&qid, id);
4282 if (err)
4283 break;
4284 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4285 got_object_commit_close(commit);
4286 } else {
4287 if (one_line)
4288 err = print_commit_oneline(commit, id,
4289 repo, refs_idmap);
4290 else
4291 err = print_commit(commit, id, repo, path,
4292 show_changed_paths ? &changed_paths : NULL,
4293 show_patch, diff_context, refs_idmap, NULL);
4294 got_object_commit_close(commit);
4295 if (err)
4296 break;
4298 if ((limit && --limit == 0) ||
4299 (end_id && got_object_id_cmp(id, end_id) == 0))
4300 break;
4302 TAILQ_FOREACH(pe, &changed_paths, entry) {
4303 free((char *)pe->path);
4304 free(pe->data);
4306 got_pathlist_free(&changed_paths);
4308 if (reverse_display_order) {
4309 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4310 err = got_object_open_as_commit(&commit, repo,
4311 &qid->id);
4312 if (err)
4313 break;
4314 if (show_changed_paths) {
4315 err = get_changed_paths(&changed_paths,
4316 commit, repo);
4317 if (err)
4318 break;
4320 if (one_line)
4321 err = print_commit_oneline(commit, &qid->id,
4322 repo, refs_idmap);
4323 else
4324 err = print_commit(commit, &qid->id, repo, path,
4325 show_changed_paths ? &changed_paths : NULL,
4326 show_patch, diff_context, refs_idmap, NULL);
4327 got_object_commit_close(commit);
4328 if (err)
4329 break;
4330 TAILQ_FOREACH(pe, &changed_paths, entry) {
4331 free((char *)pe->path);
4332 free(pe->data);
4334 got_pathlist_free(&changed_paths);
4337 done:
4338 while (!STAILQ_EMPTY(&reversed_commits)) {
4339 qid = STAILQ_FIRST(&reversed_commits);
4340 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4341 got_object_qid_free(qid);
4343 TAILQ_FOREACH(pe, &changed_paths, entry) {
4344 free((char *)pe->path);
4345 free(pe->data);
4347 got_pathlist_free(&changed_paths);
4348 if (search_pattern)
4349 regfree(&regex);
4350 got_commit_graph_close(graph);
4351 return err;
4354 __dead static void
4355 usage_log(void)
4357 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4358 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4359 "[-r repository-path] [-R] [path]\n", getprogname());
4360 exit(1);
4363 static int
4364 get_default_log_limit(void)
4366 const char *got_default_log_limit;
4367 long long n;
4368 const char *errstr;
4370 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4371 if (got_default_log_limit == NULL)
4372 return 0;
4373 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4374 if (errstr != NULL)
4375 return 0;
4376 return n;
4379 static const struct got_error *
4380 cmd_log(int argc, char *argv[])
4382 const struct got_error *error;
4383 struct got_repository *repo = NULL;
4384 struct got_worktree *worktree = NULL;
4385 struct got_object_id *start_id = NULL, *end_id = NULL;
4386 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4387 const char *start_commit = NULL, *end_commit = NULL;
4388 const char *search_pattern = NULL;
4389 int diff_context = -1, ch;
4390 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4391 int reverse_display_order = 0, one_line = 0;
4392 const char *errstr;
4393 struct got_reflist_head refs;
4394 struct got_reflist_object_id_map *refs_idmap = NULL;
4395 FILE *tmpfile = NULL;
4396 int *pack_fds = NULL;
4398 TAILQ_INIT(&refs);
4400 #ifndef PROFILE
4401 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4402 NULL)
4403 == -1)
4404 err(1, "pledge");
4405 #endif
4407 limit = get_default_log_limit();
4409 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4410 switch (ch) {
4411 case 'p':
4412 show_patch = 1;
4413 break;
4414 case 'P':
4415 show_changed_paths = 1;
4416 break;
4417 case 'c':
4418 start_commit = optarg;
4419 break;
4420 case 'C':
4421 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4422 &errstr);
4423 if (errstr != NULL)
4424 errx(1, "number of context lines is %s: %s",
4425 errstr, optarg);
4426 break;
4427 case 'l':
4428 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4429 if (errstr != NULL)
4430 errx(1, "number of commits is %s: %s",
4431 errstr, optarg);
4432 break;
4433 case 'b':
4434 log_branches = 1;
4435 break;
4436 case 'r':
4437 repo_path = realpath(optarg, NULL);
4438 if (repo_path == NULL)
4439 return got_error_from_errno2("realpath",
4440 optarg);
4441 got_path_strip_trailing_slashes(repo_path);
4442 break;
4443 case 'R':
4444 reverse_display_order = 1;
4445 break;
4446 case 's':
4447 one_line = 1;
4448 break;
4449 case 'S':
4450 search_pattern = optarg;
4451 break;
4452 case 'x':
4453 end_commit = optarg;
4454 break;
4455 default:
4456 usage_log();
4457 /* NOTREACHED */
4461 argc -= optind;
4462 argv += optind;
4464 if (diff_context == -1)
4465 diff_context = 3;
4466 else if (!show_patch)
4467 errx(1, "-C requires -p");
4469 if (one_line && (show_patch || show_changed_paths))
4470 errx(1, "cannot use -s with -p or -P");
4472 cwd = getcwd(NULL, 0);
4473 if (cwd == NULL) {
4474 error = got_error_from_errno("getcwd");
4475 goto done;
4478 error = got_repo_pack_fds_open(&pack_fds);
4479 if (error != NULL)
4480 goto done;
4482 if (repo_path == NULL) {
4483 error = got_worktree_open(&worktree, cwd);
4484 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4485 goto done;
4486 error = NULL;
4489 if (argc == 1) {
4490 if (worktree) {
4491 error = got_worktree_resolve_path(&path, worktree,
4492 argv[0]);
4493 if (error)
4494 goto done;
4495 } else {
4496 path = strdup(argv[0]);
4497 if (path == NULL) {
4498 error = got_error_from_errno("strdup");
4499 goto done;
4502 } else if (argc != 0)
4503 usage_log();
4505 if (repo_path == NULL) {
4506 repo_path = worktree ?
4507 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4509 if (repo_path == NULL) {
4510 error = got_error_from_errno("strdup");
4511 goto done;
4514 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4515 if (error != NULL)
4516 goto done;
4518 error = apply_unveil(got_repo_get_path(repo), 1,
4519 worktree ? got_worktree_get_root_path(worktree) : NULL);
4520 if (error)
4521 goto done;
4523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4524 if (error)
4525 goto done;
4527 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4528 if (error)
4529 goto done;
4531 if (start_commit == NULL) {
4532 struct got_reference *head_ref;
4533 struct got_commit_object *commit = NULL;
4534 error = got_ref_open(&head_ref, repo,
4535 worktree ? got_worktree_get_head_ref_name(worktree)
4536 : GOT_REF_HEAD, 0);
4537 if (error != NULL)
4538 goto done;
4539 error = got_ref_resolve(&start_id, repo, head_ref);
4540 got_ref_close(head_ref);
4541 if (error != NULL)
4542 goto done;
4543 error = got_object_open_as_commit(&commit, repo,
4544 start_id);
4545 if (error != NULL)
4546 goto done;
4547 got_object_commit_close(commit);
4548 } else {
4549 error = got_repo_match_object_id(&start_id, NULL,
4550 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4551 if (error != NULL)
4552 goto done;
4554 if (end_commit != NULL) {
4555 error = got_repo_match_object_id(&end_id, NULL,
4556 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4557 if (error != NULL)
4558 goto done;
4561 if (worktree) {
4563 * If a path was specified on the command line it was resolved
4564 * to a path in the work tree above. Prepend the work tree's
4565 * path prefix to obtain the corresponding in-repository path.
4567 if (path) {
4568 const char *prefix;
4569 prefix = got_worktree_get_path_prefix(worktree);
4570 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4571 (path[0] != '\0') ? "/" : "", path) == -1) {
4572 error = got_error_from_errno("asprintf");
4573 goto done;
4576 } else
4577 error = got_repo_map_path(&in_repo_path, repo,
4578 path ? path : "");
4579 if (error != NULL)
4580 goto done;
4581 if (in_repo_path) {
4582 free(path);
4583 path = in_repo_path;
4586 if (worktree) {
4587 /* Release work tree lock. */
4588 got_worktree_close(worktree);
4589 worktree = NULL;
4592 if (search_pattern && show_patch) {
4593 tmpfile = got_opentemp();
4594 if (tmpfile == NULL) {
4595 error = got_error_from_errno("got_opentemp");
4596 goto done;
4600 error = print_commits(start_id, end_id, repo, path ? path : "",
4601 show_changed_paths, show_patch, search_pattern, diff_context,
4602 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4603 tmpfile);
4604 done:
4605 free(path);
4606 free(repo_path);
4607 free(cwd);
4608 if (worktree)
4609 got_worktree_close(worktree);
4610 if (repo) {
4611 const struct got_error *close_err = got_repo_close(repo);
4612 if (error == NULL)
4613 error = close_err;
4615 if (pack_fds) {
4616 const struct got_error *pack_err =
4617 got_repo_pack_fds_close(pack_fds);
4618 if (error == NULL)
4619 error = pack_err;
4621 if (refs_idmap)
4622 got_reflist_object_id_map_free(refs_idmap);
4623 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4624 error = got_error_from_errno("fclose");
4625 got_ref_list_free(&refs);
4626 return error;
4629 __dead static void
4630 usage_diff(void)
4632 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4633 "[-r repository-path] [-s] [-w] [-P] "
4634 "[object1 object2 | path ...]\n", getprogname());
4635 exit(1);
4638 struct print_diff_arg {
4639 struct got_repository *repo;
4640 struct got_worktree *worktree;
4641 int diff_context;
4642 const char *id_str;
4643 int header_shown;
4644 int diff_staged;
4645 enum got_diff_algorithm diff_algo;
4646 int ignore_whitespace;
4647 int force_text_diff;
4648 FILE *f1;
4649 FILE *f2;
4653 * Create a file which contains the target path of a symlink so we can feed
4654 * it as content to the diff engine.
4656 static const struct got_error *
4657 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4658 const char *abspath)
4660 const struct got_error *err = NULL;
4661 char target_path[PATH_MAX];
4662 ssize_t target_len, outlen;
4664 *fd = -1;
4666 if (dirfd != -1) {
4667 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4668 if (target_len == -1)
4669 return got_error_from_errno2("readlinkat", abspath);
4670 } else {
4671 target_len = readlink(abspath, target_path, PATH_MAX);
4672 if (target_len == -1)
4673 return got_error_from_errno2("readlink", abspath);
4676 *fd = got_opentempfd();
4677 if (*fd == -1)
4678 return got_error_from_errno("got_opentempfd");
4680 outlen = write(*fd, target_path, target_len);
4681 if (outlen == -1) {
4682 err = got_error_from_errno("got_opentempfd");
4683 goto done;
4686 if (lseek(*fd, 0, SEEK_SET) == -1) {
4687 err = got_error_from_errno2("lseek", abspath);
4688 goto done;
4690 done:
4691 if (err) {
4692 close(*fd);
4693 *fd = -1;
4695 return err;
4698 static const struct got_error *
4699 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4700 const char *path, struct got_object_id *blob_id,
4701 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4702 int dirfd, const char *de_name)
4704 struct print_diff_arg *a = arg;
4705 const struct got_error *err = NULL;
4706 struct got_blob_object *blob1 = NULL;
4707 int fd = -1, fd1 = -1, fd2 = -1;
4708 FILE *f2 = NULL;
4709 char *abspath = NULL, *label1 = NULL;
4710 struct stat sb;
4711 off_t size1 = 0;
4712 int f2_exists = 1;
4714 if (a->diff_staged) {
4715 if (staged_status != GOT_STATUS_MODIFY &&
4716 staged_status != GOT_STATUS_ADD &&
4717 staged_status != GOT_STATUS_DELETE)
4718 return NULL;
4719 } else {
4720 if (staged_status == GOT_STATUS_DELETE)
4721 return NULL;
4722 if (status == GOT_STATUS_NONEXISTENT)
4723 return got_error_set_errno(ENOENT, path);
4724 if (status != GOT_STATUS_MODIFY &&
4725 status != GOT_STATUS_ADD &&
4726 status != GOT_STATUS_DELETE &&
4727 status != GOT_STATUS_CONFLICT)
4728 return NULL;
4731 err = got_opentemp_truncate(a->f1);
4732 if (err)
4733 return got_error_from_errno("got_opentemp_truncate");
4734 err = got_opentemp_truncate(a->f2);
4735 if (err)
4736 return got_error_from_errno("got_opentemp_truncate");
4738 if (!a->header_shown) {
4739 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4740 got_worktree_get_root_path(a->worktree));
4741 printf("commit - %s\n", a->id_str);
4742 printf("path + %s%s\n",
4743 got_worktree_get_root_path(a->worktree),
4744 a->diff_staged ? " (staged changes)" : "");
4745 a->header_shown = 1;
4748 if (a->diff_staged) {
4749 const char *label1 = NULL, *label2 = NULL;
4750 switch (staged_status) {
4751 case GOT_STATUS_MODIFY:
4752 label1 = path;
4753 label2 = path;
4754 break;
4755 case GOT_STATUS_ADD:
4756 label2 = path;
4757 break;
4758 case GOT_STATUS_DELETE:
4759 label1 = path;
4760 break;
4761 default:
4762 return got_error(GOT_ERR_FILE_STATUS);
4764 fd1 = got_opentempfd();
4765 if (fd1 == -1) {
4766 err = got_error_from_errno("got_opentempfd");
4767 goto done;
4769 fd2 = got_opentempfd();
4770 if (fd2 == -1) {
4771 err = got_error_from_errno("got_opentempfd");
4772 goto done;
4774 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4775 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4776 a->diff_algo, a->diff_context, a->ignore_whitespace,
4777 a->force_text_diff, a->repo, stdout);
4778 goto done;
4781 fd1 = got_opentempfd();
4782 if (fd1 == -1) {
4783 err = got_error_from_errno("got_opentempfd");
4784 goto done;
4787 if (staged_status == GOT_STATUS_ADD ||
4788 staged_status == GOT_STATUS_MODIFY) {
4789 char *id_str;
4790 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4791 8192, fd1);
4792 if (err)
4793 goto done;
4794 err = got_object_id_str(&id_str, staged_blob_id);
4795 if (err)
4796 goto done;
4797 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4798 err = got_error_from_errno("asprintf");
4799 free(id_str);
4800 goto done;
4802 free(id_str);
4803 } else if (status != GOT_STATUS_ADD) {
4804 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4805 fd1);
4806 if (err)
4807 goto done;
4810 if (status != GOT_STATUS_DELETE) {
4811 if (asprintf(&abspath, "%s/%s",
4812 got_worktree_get_root_path(a->worktree), path) == -1) {
4813 err = got_error_from_errno("asprintf");
4814 goto done;
4817 if (dirfd != -1) {
4818 fd = openat(dirfd, de_name,
4819 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4820 if (fd == -1) {
4821 if (!got_err_open_nofollow_on_symlink()) {
4822 err = got_error_from_errno2("openat",
4823 abspath);
4824 goto done;
4826 err = get_symlink_target_file(&fd, dirfd,
4827 de_name, abspath);
4828 if (err)
4829 goto done;
4831 } else {
4832 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4833 if (fd == -1) {
4834 if (!got_err_open_nofollow_on_symlink()) {
4835 err = got_error_from_errno2("open",
4836 abspath);
4837 goto done;
4839 err = get_symlink_target_file(&fd, dirfd,
4840 de_name, abspath);
4841 if (err)
4842 goto done;
4845 if (fstat(fd, &sb) == -1) {
4846 err = got_error_from_errno2("fstat", abspath);
4847 goto done;
4849 f2 = fdopen(fd, "r");
4850 if (f2 == NULL) {
4851 err = got_error_from_errno2("fdopen", abspath);
4852 goto done;
4854 fd = -1;
4855 } else {
4856 sb.st_size = 0;
4857 f2_exists = 0;
4860 if (blob1) {
4861 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4862 a->f1, blob1);
4863 if (err)
4864 goto done;
4867 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4868 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4869 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4870 done:
4871 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4872 err = got_error_from_errno("close");
4873 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4874 err = got_error_from_errno("close");
4875 if (blob1)
4876 got_object_blob_close(blob1);
4877 if (fd != -1 && close(fd) == -1 && err == NULL)
4878 err = got_error_from_errno("close");
4879 if (f2 && fclose(f2) == EOF && err == NULL)
4880 err = got_error_from_errno("fclose");
4881 free(abspath);
4882 return err;
4885 static const struct got_error *
4886 cmd_diff(int argc, char *argv[])
4888 const struct got_error *error;
4889 struct got_repository *repo = NULL;
4890 struct got_worktree *worktree = NULL;
4891 char *cwd = NULL, *repo_path = NULL;
4892 const char *commit_args[2] = { NULL, NULL };
4893 int ncommit_args = 0;
4894 struct got_object_id *ids[2] = { NULL, NULL };
4895 char *labels[2] = { NULL, NULL };
4896 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4897 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4898 int force_text_diff = 0, force_path = 0, rflag = 0;
4899 const char *errstr;
4900 struct got_reflist_head refs;
4901 struct got_pathlist_head paths;
4902 struct got_pathlist_entry *pe;
4903 FILE *f1 = NULL, *f2 = NULL;
4904 int fd1 = -1, fd2 = -1;
4905 int *pack_fds = NULL;
4907 TAILQ_INIT(&refs);
4908 TAILQ_INIT(&paths);
4910 #ifndef PROFILE
4911 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4912 NULL) == -1)
4913 err(1, "pledge");
4914 #endif
4916 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4917 switch (ch) {
4918 case 'a':
4919 force_text_diff = 1;
4920 break;
4921 case 'c':
4922 if (ncommit_args >= 2)
4923 errx(1, "too many -c options used");
4924 commit_args[ncommit_args++] = optarg;
4925 break;
4926 case 'C':
4927 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4928 &errstr);
4929 if (errstr != NULL)
4930 errx(1, "number of context lines is %s: %s",
4931 errstr, optarg);
4932 break;
4933 case 'r':
4934 repo_path = realpath(optarg, NULL);
4935 if (repo_path == NULL)
4936 return got_error_from_errno2("realpath",
4937 optarg);
4938 got_path_strip_trailing_slashes(repo_path);
4939 rflag = 1;
4940 break;
4941 case 's':
4942 diff_staged = 1;
4943 break;
4944 case 'w':
4945 ignore_whitespace = 1;
4946 break;
4947 case 'P':
4948 force_path = 1;
4949 break;
4950 default:
4951 usage_diff();
4952 /* NOTREACHED */
4956 argc -= optind;
4957 argv += optind;
4959 cwd = getcwd(NULL, 0);
4960 if (cwd == NULL) {
4961 error = got_error_from_errno("getcwd");
4962 goto done;
4965 error = got_repo_pack_fds_open(&pack_fds);
4966 if (error != NULL)
4967 goto done;
4969 if (repo_path == NULL) {
4970 error = got_worktree_open(&worktree, cwd);
4971 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 goto done;
4973 else
4974 error = NULL;
4975 if (worktree) {
4976 repo_path =
4977 strdup(got_worktree_get_repo_path(worktree));
4978 if (repo_path == NULL) {
4979 error = got_error_from_errno("strdup");
4980 goto done;
4982 } else {
4983 repo_path = strdup(cwd);
4984 if (repo_path == NULL) {
4985 error = got_error_from_errno("strdup");
4986 goto done;
4991 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4992 free(repo_path);
4993 if (error != NULL)
4994 goto done;
4996 if (rflag || worktree == NULL || ncommit_args > 0) {
4997 if (force_path) {
4998 error = got_error_msg(GOT_ERR_NOT_IMPL,
4999 "-P option can only be used when diffing "
5000 "a work tree");
5001 goto done;
5003 if (diff_staged) {
5004 error = got_error_msg(GOT_ERR_NOT_IMPL,
5005 "-s option can only be used when diffing "
5006 "a work tree");
5007 goto done;
5011 error = apply_unveil(got_repo_get_path(repo), 1,
5012 worktree ? got_worktree_get_root_path(worktree) : NULL);
5013 if (error)
5014 goto done;
5016 if ((!force_path && argc == 2) || ncommit_args > 0) {
5017 int obj_type = (ncommit_args > 0 ?
5018 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5019 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5020 NULL);
5021 if (error)
5022 goto done;
5023 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5024 const char *arg;
5025 if (ncommit_args > 0)
5026 arg = commit_args[i];
5027 else
5028 arg = argv[i];
5029 error = got_repo_match_object_id(&ids[i], &labels[i],
5030 arg, obj_type, &refs, repo);
5031 if (error) {
5032 if (error->code != GOT_ERR_NOT_REF &&
5033 error->code != GOT_ERR_NO_OBJ)
5034 goto done;
5035 if (ncommit_args > 0)
5036 goto done;
5037 error = NULL;
5038 break;
5043 f1 = got_opentemp();
5044 if (f1 == NULL) {
5045 error = got_error_from_errno("got_opentemp");
5046 goto done;
5049 f2 = got_opentemp();
5050 if (f2 == NULL) {
5051 error = got_error_from_errno("got_opentemp");
5052 goto done;
5055 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5056 struct print_diff_arg arg;
5057 char *id_str;
5059 if (worktree == NULL) {
5060 if (argc == 2 && ids[0] == NULL) {
5061 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5062 goto done;
5063 } else if (argc == 2 && ids[1] == NULL) {
5064 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5065 goto done;
5066 } else if (argc > 0) {
5067 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5068 "%s", "specified paths cannot be resolved");
5069 goto done;
5070 } else {
5071 error = got_error(GOT_ERR_NOT_WORKTREE);
5072 goto done;
5076 error = get_worktree_paths_from_argv(&paths, argc, argv,
5077 worktree);
5078 if (error)
5079 goto done;
5081 error = got_object_id_str(&id_str,
5082 got_worktree_get_base_commit_id(worktree));
5083 if (error)
5084 goto done;
5085 arg.repo = repo;
5086 arg.worktree = worktree;
5087 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5088 arg.diff_context = diff_context;
5089 arg.id_str = id_str;
5090 arg.header_shown = 0;
5091 arg.diff_staged = diff_staged;
5092 arg.ignore_whitespace = ignore_whitespace;
5093 arg.force_text_diff = force_text_diff;
5094 arg.f1 = f1;
5095 arg.f2 = f2;
5097 error = got_worktree_status(worktree, &paths, repo, 0,
5098 print_diff, &arg, check_cancelled, NULL);
5099 free(id_str);
5100 goto done;
5103 if (ncommit_args == 1) {
5104 struct got_commit_object *commit;
5105 error = got_object_open_as_commit(&commit, repo, ids[0]);
5106 if (error)
5107 goto done;
5109 labels[1] = labels[0];
5110 ids[1] = ids[0];
5111 if (got_object_commit_get_nparents(commit) > 0) {
5112 const struct got_object_id_queue *pids;
5113 struct got_object_qid *pid;
5114 pids = got_object_commit_get_parent_ids(commit);
5115 pid = STAILQ_FIRST(pids);
5116 ids[0] = got_object_id_dup(&pid->id);
5117 if (ids[0] == NULL) {
5118 error = got_error_from_errno(
5119 "got_object_id_dup");
5120 got_object_commit_close(commit);
5121 goto done;
5123 error = got_object_id_str(&labels[0], ids[0]);
5124 if (error) {
5125 got_object_commit_close(commit);
5126 goto done;
5128 } else {
5129 ids[0] = NULL;
5130 labels[0] = strdup("/dev/null");
5131 if (labels[0] == NULL) {
5132 error = got_error_from_errno("strdup");
5133 got_object_commit_close(commit);
5134 goto done;
5138 got_object_commit_close(commit);
5141 if (ncommit_args == 0 && argc > 2) {
5142 error = got_error_msg(GOT_ERR_BAD_PATH,
5143 "path arguments cannot be used when diffing two objects");
5144 goto done;
5147 if (ids[0]) {
5148 error = got_object_get_type(&type1, repo, ids[0]);
5149 if (error)
5150 goto done;
5153 error = got_object_get_type(&type2, repo, ids[1]);
5154 if (error)
5155 goto done;
5156 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5157 error = got_error(GOT_ERR_OBJ_TYPE);
5158 goto done;
5160 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5161 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5162 "path arguments cannot be used when diffing blobs");
5163 goto done;
5166 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5167 char *in_repo_path;
5168 struct got_pathlist_entry *new;
5169 if (worktree) {
5170 const char *prefix;
5171 char *p;
5172 error = got_worktree_resolve_path(&p, worktree,
5173 argv[i]);
5174 if (error)
5175 goto done;
5176 prefix = got_worktree_get_path_prefix(worktree);
5177 while (prefix[0] == '/')
5178 prefix++;
5179 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5180 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5181 p) == -1) {
5182 error = got_error_from_errno("asprintf");
5183 free(p);
5184 goto done;
5186 free(p);
5187 } else {
5188 char *mapped_path, *s;
5189 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5190 if (error)
5191 goto done;
5192 s = mapped_path;
5193 while (s[0] == '/')
5194 s++;
5195 in_repo_path = strdup(s);
5196 if (in_repo_path == NULL) {
5197 error = got_error_from_errno("asprintf");
5198 free(mapped_path);
5199 goto done;
5201 free(mapped_path);
5204 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5205 if (error || new == NULL /* duplicate */)
5206 free(in_repo_path);
5207 if (error)
5208 goto done;
5211 if (worktree) {
5212 /* Release work tree lock. */
5213 got_worktree_close(worktree);
5214 worktree = NULL;
5217 fd1 = got_opentempfd();
5218 if (fd1 == -1) {
5219 error = got_error_from_errno("got_opentempfd");
5220 goto done;
5223 fd2 = got_opentempfd();
5224 if (fd2 == -1) {
5225 error = got_error_from_errno("got_opentempfd");
5226 goto done;
5229 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5230 case GOT_OBJ_TYPE_BLOB:
5231 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5232 fd1, fd2, ids[0], ids[1], NULL, NULL,
5233 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5234 ignore_whitespace, force_text_diff, repo, stdout);
5235 break;
5236 case GOT_OBJ_TYPE_TREE:
5237 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5238 ids[0], ids[1], &paths, "", "",
5239 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5240 ignore_whitespace, force_text_diff, repo, stdout);
5241 break;
5242 case GOT_OBJ_TYPE_COMMIT:
5243 printf("diff %s %s\n", labels[0], labels[1]);
5244 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5245 fd1, fd2, ids[0], ids[1], &paths,
5246 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5247 ignore_whitespace, force_text_diff, repo, stdout);
5248 break;
5249 default:
5250 error = got_error(GOT_ERR_OBJ_TYPE);
5252 done:
5253 free(labels[0]);
5254 free(labels[1]);
5255 free(ids[0]);
5256 free(ids[1]);
5257 if (worktree)
5258 got_worktree_close(worktree);
5259 if (repo) {
5260 const struct got_error *close_err = got_repo_close(repo);
5261 if (error == NULL)
5262 error = close_err;
5264 if (pack_fds) {
5265 const struct got_error *pack_err =
5266 got_repo_pack_fds_close(pack_fds);
5267 if (error == NULL)
5268 error = pack_err;
5270 TAILQ_FOREACH(pe, &paths, entry)
5271 free((char *)pe->path);
5272 got_pathlist_free(&paths);
5273 got_ref_list_free(&refs);
5274 if (f1 && fclose(f1) == EOF && error == NULL)
5275 error = got_error_from_errno("fclose");
5276 if (f2 && fclose(f2) == EOF && error == NULL)
5277 error = got_error_from_errno("fclose");
5278 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5279 error = got_error_from_errno("close");
5280 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5281 error = got_error_from_errno("close");
5282 return error;
5285 __dead static void
5286 usage_blame(void)
5288 fprintf(stderr,
5289 "usage: %s blame [-c commit] [-r repository-path] path\n",
5290 getprogname());
5291 exit(1);
5294 struct blame_line {
5295 int annotated;
5296 char *id_str;
5297 char *committer;
5298 char datebuf[11]; /* YYYY-MM-DD + NUL */
5301 struct blame_cb_args {
5302 struct blame_line *lines;
5303 int nlines;
5304 int nlines_prec;
5305 int lineno_cur;
5306 off_t *line_offsets;
5307 FILE *f;
5308 struct got_repository *repo;
5311 static const struct got_error *
5312 blame_cb(void *arg, int nlines, int lineno,
5313 struct got_commit_object *commit, struct got_object_id *id)
5315 const struct got_error *err = NULL;
5316 struct blame_cb_args *a = arg;
5317 struct blame_line *bline;
5318 char *line = NULL;
5319 size_t linesize = 0;
5320 off_t offset;
5321 struct tm tm;
5322 time_t committer_time;
5324 if (nlines != a->nlines ||
5325 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5326 return got_error(GOT_ERR_RANGE);
5328 if (sigint_received)
5329 return got_error(GOT_ERR_ITER_COMPLETED);
5331 if (lineno == -1)
5332 return NULL; /* no change in this commit */
5334 /* Annotate this line. */
5335 bline = &a->lines[lineno - 1];
5336 if (bline->annotated)
5337 return NULL;
5338 err = got_object_id_str(&bline->id_str, id);
5339 if (err)
5340 return err;
5342 bline->committer = strdup(got_object_commit_get_committer(commit));
5343 if (bline->committer == NULL) {
5344 err = got_error_from_errno("strdup");
5345 goto done;
5348 committer_time = got_object_commit_get_committer_time(commit);
5349 if (gmtime_r(&committer_time, &tm) == NULL)
5350 return got_error_from_errno("gmtime_r");
5351 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5352 &tm) == 0) {
5353 err = got_error(GOT_ERR_NO_SPACE);
5354 goto done;
5356 bline->annotated = 1;
5358 /* Print lines annotated so far. */
5359 bline = &a->lines[a->lineno_cur - 1];
5360 if (!bline->annotated)
5361 goto done;
5363 offset = a->line_offsets[a->lineno_cur - 1];
5364 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5365 err = got_error_from_errno("fseeko");
5366 goto done;
5369 while (bline->annotated) {
5370 char *smallerthan, *at, *nl, *committer;
5371 size_t len;
5373 if (getline(&line, &linesize, a->f) == -1) {
5374 if (ferror(a->f))
5375 err = got_error_from_errno("getline");
5376 break;
5379 committer = bline->committer;
5380 smallerthan = strchr(committer, '<');
5381 if (smallerthan && smallerthan[1] != '\0')
5382 committer = smallerthan + 1;
5383 at = strchr(committer, '@');
5384 if (at)
5385 *at = '\0';
5386 len = strlen(committer);
5387 if (len >= 9)
5388 committer[8] = '\0';
5390 nl = strchr(line, '\n');
5391 if (nl)
5392 *nl = '\0';
5393 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5394 bline->id_str, bline->datebuf, committer, line);
5396 a->lineno_cur++;
5397 bline = &a->lines[a->lineno_cur - 1];
5399 done:
5400 free(line);
5401 return err;
5404 static const struct got_error *
5405 cmd_blame(int argc, char *argv[])
5407 const struct got_error *error;
5408 struct got_repository *repo = NULL;
5409 struct got_worktree *worktree = NULL;
5410 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5411 char *link_target = NULL;
5412 struct got_object_id *obj_id = NULL;
5413 struct got_object_id *commit_id = NULL;
5414 struct got_commit_object *commit = NULL;
5415 struct got_blob_object *blob = NULL;
5416 char *commit_id_str = NULL;
5417 struct blame_cb_args bca;
5418 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5419 off_t filesize;
5420 int *pack_fds = NULL;
5421 FILE *f1 = NULL, *f2 = NULL;
5423 fd1 = got_opentempfd();
5424 if (fd1 == -1)
5425 return got_error_from_errno("got_opentempfd");
5427 memset(&bca, 0, sizeof(bca));
5429 #ifndef PROFILE
5430 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5431 NULL) == -1)
5432 err(1, "pledge");
5433 #endif
5435 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5436 switch (ch) {
5437 case 'c':
5438 commit_id_str = optarg;
5439 break;
5440 case 'r':
5441 repo_path = realpath(optarg, NULL);
5442 if (repo_path == NULL)
5443 return got_error_from_errno2("realpath",
5444 optarg);
5445 got_path_strip_trailing_slashes(repo_path);
5446 break;
5447 default:
5448 usage_blame();
5449 /* NOTREACHED */
5453 argc -= optind;
5454 argv += optind;
5456 if (argc == 1)
5457 path = argv[0];
5458 else
5459 usage_blame();
5461 cwd = getcwd(NULL, 0);
5462 if (cwd == NULL) {
5463 error = got_error_from_errno("getcwd");
5464 goto done;
5467 error = got_repo_pack_fds_open(&pack_fds);
5468 if (error != NULL)
5469 goto done;
5471 if (repo_path == NULL) {
5472 error = got_worktree_open(&worktree, cwd);
5473 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5474 goto done;
5475 else
5476 error = NULL;
5477 if (worktree) {
5478 repo_path =
5479 strdup(got_worktree_get_repo_path(worktree));
5480 if (repo_path == NULL) {
5481 error = got_error_from_errno("strdup");
5482 if (error)
5483 goto done;
5485 } else {
5486 repo_path = strdup(cwd);
5487 if (repo_path == NULL) {
5488 error = got_error_from_errno("strdup");
5489 goto done;
5494 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5495 if (error != NULL)
5496 goto done;
5498 if (worktree) {
5499 const char *prefix = got_worktree_get_path_prefix(worktree);
5500 char *p;
5502 error = got_worktree_resolve_path(&p, worktree, path);
5503 if (error)
5504 goto done;
5505 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5506 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5507 p) == -1) {
5508 error = got_error_from_errno("asprintf");
5509 free(p);
5510 goto done;
5512 free(p);
5513 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5514 } else {
5515 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5516 if (error)
5517 goto done;
5518 error = got_repo_map_path(&in_repo_path, repo, path);
5520 if (error)
5521 goto done;
5523 if (commit_id_str == NULL) {
5524 struct got_reference *head_ref;
5525 error = got_ref_open(&head_ref, repo, worktree ?
5526 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5527 if (error != NULL)
5528 goto done;
5529 error = got_ref_resolve(&commit_id, repo, head_ref);
5530 got_ref_close(head_ref);
5531 if (error != NULL)
5532 goto done;
5533 } else {
5534 struct got_reflist_head refs;
5535 TAILQ_INIT(&refs);
5536 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5537 NULL);
5538 if (error)
5539 goto done;
5540 error = got_repo_match_object_id(&commit_id, NULL,
5541 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5542 got_ref_list_free(&refs);
5543 if (error)
5544 goto done;
5547 if (worktree) {
5548 /* Release work tree lock. */
5549 got_worktree_close(worktree);
5550 worktree = NULL;
5553 error = got_object_open_as_commit(&commit, repo, commit_id);
5554 if (error)
5555 goto done;
5557 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5558 commit, repo);
5559 if (error)
5560 goto done;
5562 error = got_object_id_by_path(&obj_id, repo, commit,
5563 link_target ? link_target : in_repo_path);
5564 if (error)
5565 goto done;
5567 error = got_object_get_type(&obj_type, repo, obj_id);
5568 if (error)
5569 goto done;
5571 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5572 error = got_error_path(link_target ? link_target : in_repo_path,
5573 GOT_ERR_OBJ_TYPE);
5574 goto done;
5577 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5578 if (error)
5579 goto done;
5580 bca.f = got_opentemp();
5581 if (bca.f == NULL) {
5582 error = got_error_from_errno("got_opentemp");
5583 goto done;
5585 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5586 &bca.line_offsets, bca.f, blob);
5587 if (error || bca.nlines == 0)
5588 goto done;
5590 /* Don't include \n at EOF in the blame line count. */
5591 if (bca.line_offsets[bca.nlines - 1] == filesize)
5592 bca.nlines--;
5594 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5595 if (bca.lines == NULL) {
5596 error = got_error_from_errno("calloc");
5597 goto done;
5599 bca.lineno_cur = 1;
5600 bca.nlines_prec = 0;
5601 i = bca.nlines;
5602 while (i > 0) {
5603 i /= 10;
5604 bca.nlines_prec++;
5606 bca.repo = repo;
5608 fd2 = got_opentempfd();
5609 if (fd2 == -1) {
5610 error = got_error_from_errno("got_opentempfd");
5611 goto done;
5613 fd3 = got_opentempfd();
5614 if (fd3 == -1) {
5615 error = got_error_from_errno("got_opentempfd");
5616 goto done;
5618 f1 = got_opentemp();
5619 if (f1 == NULL) {
5620 error = got_error_from_errno("got_opentemp");
5621 goto done;
5623 f2 = got_opentemp();
5624 if (f2 == NULL) {
5625 error = got_error_from_errno("got_opentemp");
5626 goto done;
5628 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5629 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5630 check_cancelled, NULL, fd2, fd3, f1, f2);
5631 done:
5632 free(in_repo_path);
5633 free(link_target);
5634 free(repo_path);
5635 free(cwd);
5636 free(commit_id);
5637 free(obj_id);
5638 if (commit)
5639 got_object_commit_close(commit);
5641 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5642 error = got_error_from_errno("close");
5643 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5644 error = got_error_from_errno("close");
5645 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5646 error = got_error_from_errno("close");
5647 if (f1 && fclose(f1) == EOF && error == NULL)
5648 error = got_error_from_errno("fclose");
5649 if (f2 && fclose(f2) == EOF && error == NULL)
5650 error = got_error_from_errno("fclose");
5652 if (blob)
5653 got_object_blob_close(blob);
5654 if (worktree)
5655 got_worktree_close(worktree);
5656 if (repo) {
5657 const struct got_error *close_err = got_repo_close(repo);
5658 if (error == NULL)
5659 error = close_err;
5661 if (pack_fds) {
5662 const struct got_error *pack_err =
5663 got_repo_pack_fds_close(pack_fds);
5664 if (error == NULL)
5665 error = pack_err;
5667 if (bca.lines) {
5668 for (i = 0; i < bca.nlines; i++) {
5669 struct blame_line *bline = &bca.lines[i];
5670 free(bline->id_str);
5671 free(bline->committer);
5673 free(bca.lines);
5675 free(bca.line_offsets);
5676 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5677 error = got_error_from_errno("fclose");
5678 return error;
5681 __dead static void
5682 usage_tree(void)
5684 fprintf(stderr,
5685 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5686 getprogname());
5687 exit(1);
5690 static const struct got_error *
5691 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5692 const char *root_path, struct got_repository *repo)
5694 const struct got_error *err = NULL;
5695 int is_root_path = (strcmp(path, root_path) == 0);
5696 const char *modestr = "";
5697 mode_t mode = got_tree_entry_get_mode(te);
5698 char *link_target = NULL;
5700 path += strlen(root_path);
5701 while (path[0] == '/')
5702 path++;
5704 if (got_object_tree_entry_is_submodule(te))
5705 modestr = "$";
5706 else if (S_ISLNK(mode)) {
5707 int i;
5709 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5710 if (err)
5711 return err;
5712 for (i = 0; i < strlen(link_target); i++) {
5713 if (!isprint((unsigned char)link_target[i]))
5714 link_target[i] = '?';
5717 modestr = "@";
5719 else if (S_ISDIR(mode))
5720 modestr = "/";
5721 else if (mode & S_IXUSR)
5722 modestr = "*";
5724 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5725 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5726 link_target ? " -> ": "", link_target ? link_target : "");
5728 free(link_target);
5729 return NULL;
5732 static const struct got_error *
5733 print_tree(const char *path, struct got_commit_object *commit,
5734 int show_ids, int recurse, const char *root_path,
5735 struct got_repository *repo)
5737 const struct got_error *err = NULL;
5738 struct got_object_id *tree_id = NULL;
5739 struct got_tree_object *tree = NULL;
5740 int nentries, i;
5742 err = got_object_id_by_path(&tree_id, repo, commit, path);
5743 if (err)
5744 goto done;
5746 err = got_object_open_as_tree(&tree, repo, tree_id);
5747 if (err)
5748 goto done;
5749 nentries = got_object_tree_get_nentries(tree);
5750 for (i = 0; i < nentries; i++) {
5751 struct got_tree_entry *te;
5752 char *id = NULL;
5754 if (sigint_received || sigpipe_received)
5755 break;
5757 te = got_object_tree_get_entry(tree, i);
5758 if (show_ids) {
5759 char *id_str;
5760 err = got_object_id_str(&id_str,
5761 got_tree_entry_get_id(te));
5762 if (err)
5763 goto done;
5764 if (asprintf(&id, "%s ", id_str) == -1) {
5765 err = got_error_from_errno("asprintf");
5766 free(id_str);
5767 goto done;
5769 free(id_str);
5771 err = print_entry(te, id, path, root_path, repo);
5772 free(id);
5773 if (err)
5774 goto done;
5776 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5777 char *child_path;
5778 if (asprintf(&child_path, "%s%s%s", path,
5779 path[0] == '/' && path[1] == '\0' ? "" : "/",
5780 got_tree_entry_get_name(te)) == -1) {
5781 err = got_error_from_errno("asprintf");
5782 goto done;
5784 err = print_tree(child_path, commit, show_ids, 1,
5785 root_path, repo);
5786 free(child_path);
5787 if (err)
5788 goto done;
5791 done:
5792 if (tree)
5793 got_object_tree_close(tree);
5794 free(tree_id);
5795 return err;
5798 static const struct got_error *
5799 cmd_tree(int argc, char *argv[])
5801 const struct got_error *error;
5802 struct got_repository *repo = NULL;
5803 struct got_worktree *worktree = NULL;
5804 const char *path, *refname = NULL;
5805 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5806 struct got_object_id *commit_id = NULL;
5807 struct got_commit_object *commit = NULL;
5808 char *commit_id_str = NULL;
5809 int show_ids = 0, recurse = 0;
5810 int ch;
5811 int *pack_fds = NULL;
5813 #ifndef PROFILE
5814 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5815 NULL) == -1)
5816 err(1, "pledge");
5817 #endif
5819 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5820 switch (ch) {
5821 case 'c':
5822 commit_id_str = optarg;
5823 break;
5824 case 'r':
5825 repo_path = realpath(optarg, NULL);
5826 if (repo_path == NULL)
5827 return got_error_from_errno2("realpath",
5828 optarg);
5829 got_path_strip_trailing_slashes(repo_path);
5830 break;
5831 case 'i':
5832 show_ids = 1;
5833 break;
5834 case 'R':
5835 recurse = 1;
5836 break;
5837 default:
5838 usage_tree();
5839 /* NOTREACHED */
5843 argc -= optind;
5844 argv += optind;
5846 if (argc == 1)
5847 path = argv[0];
5848 else if (argc > 1)
5849 usage_tree();
5850 else
5851 path = NULL;
5853 cwd = getcwd(NULL, 0);
5854 if (cwd == NULL) {
5855 error = got_error_from_errno("getcwd");
5856 goto done;
5859 error = got_repo_pack_fds_open(&pack_fds);
5860 if (error != NULL)
5861 goto done;
5863 if (repo_path == NULL) {
5864 error = got_worktree_open(&worktree, cwd);
5865 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5866 goto done;
5867 else
5868 error = NULL;
5869 if (worktree) {
5870 repo_path =
5871 strdup(got_worktree_get_repo_path(worktree));
5872 if (repo_path == NULL)
5873 error = got_error_from_errno("strdup");
5874 if (error)
5875 goto done;
5876 } else {
5877 repo_path = strdup(cwd);
5878 if (repo_path == NULL) {
5879 error = got_error_from_errno("strdup");
5880 goto done;
5885 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5886 if (error != NULL)
5887 goto done;
5889 if (worktree) {
5890 const char *prefix = got_worktree_get_path_prefix(worktree);
5891 char *p;
5893 if (path == NULL)
5894 path = "";
5895 error = got_worktree_resolve_path(&p, worktree, path);
5896 if (error)
5897 goto done;
5898 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5899 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5900 p) == -1) {
5901 error = got_error_from_errno("asprintf");
5902 free(p);
5903 goto done;
5905 free(p);
5906 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5907 if (error)
5908 goto done;
5909 } else {
5910 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5911 if (error)
5912 goto done;
5913 if (path == NULL)
5914 path = "/";
5915 error = got_repo_map_path(&in_repo_path, repo, path);
5916 if (error != NULL)
5917 goto done;
5920 if (commit_id_str == NULL) {
5921 struct got_reference *head_ref;
5922 if (worktree)
5923 refname = got_worktree_get_head_ref_name(worktree);
5924 else
5925 refname = GOT_REF_HEAD;
5926 error = got_ref_open(&head_ref, repo, refname, 0);
5927 if (error != NULL)
5928 goto done;
5929 error = got_ref_resolve(&commit_id, repo, head_ref);
5930 got_ref_close(head_ref);
5931 if (error != NULL)
5932 goto done;
5933 } else {
5934 struct got_reflist_head refs;
5935 TAILQ_INIT(&refs);
5936 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5937 NULL);
5938 if (error)
5939 goto done;
5940 error = got_repo_match_object_id(&commit_id, NULL,
5941 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5942 got_ref_list_free(&refs);
5943 if (error)
5944 goto done;
5947 if (worktree) {
5948 /* Release work tree lock. */
5949 got_worktree_close(worktree);
5950 worktree = NULL;
5953 error = got_object_open_as_commit(&commit, repo, commit_id);
5954 if (error)
5955 goto done;
5957 error = print_tree(in_repo_path, commit, show_ids, recurse,
5958 in_repo_path, repo);
5959 done:
5960 free(in_repo_path);
5961 free(repo_path);
5962 free(cwd);
5963 free(commit_id);
5964 if (commit)
5965 got_object_commit_close(commit);
5966 if (worktree)
5967 got_worktree_close(worktree);
5968 if (repo) {
5969 const struct got_error *close_err = got_repo_close(repo);
5970 if (error == NULL)
5971 error = close_err;
5973 if (pack_fds) {
5974 const struct got_error *pack_err =
5975 got_repo_pack_fds_close(pack_fds);
5976 if (error == NULL)
5977 error = pack_err;
5979 return error;
5982 __dead static void
5983 usage_status(void)
5985 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5986 "[-S status-codes] [path ...]\n", getprogname());
5987 exit(1);
5990 struct got_status_arg {
5991 char *status_codes;
5992 int suppress;
5995 static const struct got_error *
5996 print_status(void *arg, unsigned char status, unsigned char staged_status,
5997 const char *path, struct got_object_id *blob_id,
5998 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5999 int dirfd, const char *de_name)
6001 struct got_status_arg *st = arg;
6003 if (status == staged_status && (status == GOT_STATUS_DELETE))
6004 status = GOT_STATUS_NO_CHANGE;
6005 if (st != NULL && st->status_codes) {
6006 size_t ncodes = strlen(st->status_codes);
6007 int i, j = 0;
6009 for (i = 0; i < ncodes ; i++) {
6010 if (st->suppress) {
6011 if (status == st->status_codes[i] ||
6012 staged_status == st->status_codes[i]) {
6013 j++;
6014 continue;
6016 } else {
6017 if (status == st->status_codes[i] ||
6018 staged_status == st->status_codes[i])
6019 break;
6023 if (st->suppress && j == 0)
6024 goto print;
6026 if (i == ncodes)
6027 return NULL;
6029 print:
6030 printf("%c%c %s\n", status, staged_status, path);
6031 return NULL;
6034 static const struct got_error *
6035 cmd_status(int argc, char *argv[])
6037 const struct got_error *error = NULL;
6038 struct got_repository *repo = NULL;
6039 struct got_worktree *worktree = NULL;
6040 struct got_status_arg st;
6041 char *cwd = NULL;
6042 struct got_pathlist_head paths;
6043 struct got_pathlist_entry *pe;
6044 int ch, i, no_ignores = 0;
6045 int *pack_fds = NULL;
6047 TAILQ_INIT(&paths);
6049 memset(&st, 0, sizeof(st));
6050 st.status_codes = NULL;
6051 st.suppress = 0;
6053 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6054 switch (ch) {
6055 case 'I':
6056 no_ignores = 1;
6057 break;
6058 case 'S':
6059 if (st.status_codes != NULL && st.suppress == 0)
6060 option_conflict('S', 's');
6061 st.suppress = 1;
6062 /* fallthrough */
6063 case 's':
6064 for (i = 0; i < strlen(optarg); i++) {
6065 switch (optarg[i]) {
6066 case GOT_STATUS_MODIFY:
6067 case GOT_STATUS_ADD:
6068 case GOT_STATUS_DELETE:
6069 case GOT_STATUS_CONFLICT:
6070 case GOT_STATUS_MISSING:
6071 case GOT_STATUS_OBSTRUCTED:
6072 case GOT_STATUS_UNVERSIONED:
6073 case GOT_STATUS_MODE_CHANGE:
6074 case GOT_STATUS_NONEXISTENT:
6075 break;
6076 default:
6077 errx(1, "invalid status code '%c'",
6078 optarg[i]);
6081 if (ch == 's' && st.suppress)
6082 option_conflict('s', 'S');
6083 st.status_codes = optarg;
6084 break;
6085 default:
6086 usage_status();
6087 /* NOTREACHED */
6091 argc -= optind;
6092 argv += optind;
6094 #ifndef PROFILE
6095 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6096 NULL) == -1)
6097 err(1, "pledge");
6098 #endif
6099 cwd = getcwd(NULL, 0);
6100 if (cwd == NULL) {
6101 error = got_error_from_errno("getcwd");
6102 goto done;
6105 error = got_repo_pack_fds_open(&pack_fds);
6106 if (error != NULL)
6107 goto done;
6109 error = got_worktree_open(&worktree, cwd);
6110 if (error) {
6111 if (error->code == GOT_ERR_NOT_WORKTREE)
6112 error = wrap_not_worktree_error(error, "status", cwd);
6113 goto done;
6116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6117 NULL, pack_fds);
6118 if (error != NULL)
6119 goto done;
6121 error = apply_unveil(got_repo_get_path(repo), 1,
6122 got_worktree_get_root_path(worktree));
6123 if (error)
6124 goto done;
6126 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6127 if (error)
6128 goto done;
6130 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6131 print_status, &st, check_cancelled, NULL);
6132 done:
6133 if (pack_fds) {
6134 const struct got_error *pack_err =
6135 got_repo_pack_fds_close(pack_fds);
6136 if (error == NULL)
6137 error = pack_err;
6140 TAILQ_FOREACH(pe, &paths, entry)
6141 free((char *)pe->path);
6142 got_pathlist_free(&paths);
6143 free(cwd);
6144 return error;
6147 __dead static void
6148 usage_ref(void)
6150 fprintf(stderr,
6151 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6152 "[-s reference] [-d] [name]\n",
6153 getprogname());
6154 exit(1);
6157 static const struct got_error *
6158 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6160 static const struct got_error *err = NULL;
6161 struct got_reflist_head refs;
6162 struct got_reflist_entry *re;
6164 TAILQ_INIT(&refs);
6165 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6166 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6167 repo);
6168 if (err)
6169 return err;
6171 TAILQ_FOREACH(re, &refs, entry) {
6172 char *refstr;
6173 refstr = got_ref_to_str(re->ref);
6174 if (refstr == NULL) {
6175 err = got_error_from_errno("got_ref_to_str");
6176 break;
6178 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6179 free(refstr);
6182 got_ref_list_free(&refs);
6183 return err;
6186 static const struct got_error *
6187 delete_ref_by_name(struct got_repository *repo, const char *refname)
6189 const struct got_error *err;
6190 struct got_reference *ref;
6192 err = got_ref_open(&ref, repo, refname, 0);
6193 if (err)
6194 return err;
6196 err = delete_ref(repo, ref);
6197 got_ref_close(ref);
6198 return err;
6201 static const struct got_error *
6202 add_ref(struct got_repository *repo, const char *refname, const char *target)
6204 const struct got_error *err = NULL;
6205 struct got_object_id *id = NULL;
6206 struct got_reference *ref = NULL;
6207 struct got_reflist_head refs;
6210 * Don't let the user create a reference name with a leading '-'.
6211 * While technically a valid reference name, this case is usually
6212 * an unintended typo.
6214 if (refname[0] == '-')
6215 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6217 TAILQ_INIT(&refs);
6218 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6219 if (err)
6220 goto done;
6221 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6222 &refs, repo);
6223 got_ref_list_free(&refs);
6224 if (err)
6225 goto done;
6227 err = got_ref_alloc(&ref, refname, id);
6228 if (err)
6229 goto done;
6231 err = got_ref_write(ref, repo);
6232 done:
6233 if (ref)
6234 got_ref_close(ref);
6235 free(id);
6236 return err;
6239 static const struct got_error *
6240 add_symref(struct got_repository *repo, const char *refname, const char *target)
6242 const struct got_error *err = NULL;
6243 struct got_reference *ref = NULL;
6244 struct got_reference *target_ref = NULL;
6247 * Don't let the user create a reference name with a leading '-'.
6248 * While technically a valid reference name, this case is usually
6249 * an unintended typo.
6251 if (refname[0] == '-')
6252 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6254 err = got_ref_open(&target_ref, repo, target, 0);
6255 if (err)
6256 return err;
6258 err = got_ref_alloc_symref(&ref, refname, target_ref);
6259 if (err)
6260 goto done;
6262 err = got_ref_write(ref, repo);
6263 done:
6264 if (target_ref)
6265 got_ref_close(target_ref);
6266 if (ref)
6267 got_ref_close(ref);
6268 return err;
6271 static const struct got_error *
6272 cmd_ref(int argc, char *argv[])
6274 const struct got_error *error = NULL;
6275 struct got_repository *repo = NULL;
6276 struct got_worktree *worktree = NULL;
6277 char *cwd = NULL, *repo_path = NULL;
6278 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6279 const char *obj_arg = NULL, *symref_target= NULL;
6280 char *refname = NULL;
6281 int *pack_fds = NULL;
6283 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6284 switch (ch) {
6285 case 'c':
6286 obj_arg = optarg;
6287 break;
6288 case 'd':
6289 do_delete = 1;
6290 break;
6291 case 'r':
6292 repo_path = realpath(optarg, NULL);
6293 if (repo_path == NULL)
6294 return got_error_from_errno2("realpath",
6295 optarg);
6296 got_path_strip_trailing_slashes(repo_path);
6297 break;
6298 case 'l':
6299 do_list = 1;
6300 break;
6301 case 's':
6302 symref_target = optarg;
6303 break;
6304 case 't':
6305 sort_by_time = 1;
6306 break;
6307 default:
6308 usage_ref();
6309 /* NOTREACHED */
6313 if (obj_arg && do_list)
6314 option_conflict('c', 'l');
6315 if (obj_arg && do_delete)
6316 option_conflict('c', 'd');
6317 if (obj_arg && symref_target)
6318 option_conflict('c', 's');
6319 if (symref_target && do_delete)
6320 option_conflict('s', 'd');
6321 if (symref_target && do_list)
6322 option_conflict('s', 'l');
6323 if (do_delete && do_list)
6324 option_conflict('d', 'l');
6325 if (sort_by_time && !do_list)
6326 errx(1, "-t option requires -l option");
6328 argc -= optind;
6329 argv += optind;
6331 if (do_list) {
6332 if (argc != 0 && argc != 1)
6333 usage_ref();
6334 if (argc == 1) {
6335 refname = strdup(argv[0]);
6336 if (refname == NULL) {
6337 error = got_error_from_errno("strdup");
6338 goto done;
6341 } else {
6342 if (argc != 1)
6343 usage_ref();
6344 refname = strdup(argv[0]);
6345 if (refname == NULL) {
6346 error = got_error_from_errno("strdup");
6347 goto done;
6351 if (refname)
6352 got_path_strip_trailing_slashes(refname);
6354 #ifndef PROFILE
6355 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6356 "sendfd unveil", NULL) == -1)
6357 err(1, "pledge");
6358 #endif
6359 cwd = getcwd(NULL, 0);
6360 if (cwd == NULL) {
6361 error = got_error_from_errno("getcwd");
6362 goto done;
6365 error = got_repo_pack_fds_open(&pack_fds);
6366 if (error != NULL)
6367 goto done;
6369 if (repo_path == NULL) {
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6372 goto done;
6373 else
6374 error = NULL;
6375 if (worktree) {
6376 repo_path =
6377 strdup(got_worktree_get_repo_path(worktree));
6378 if (repo_path == NULL)
6379 error = got_error_from_errno("strdup");
6380 if (error)
6381 goto done;
6382 } else {
6383 repo_path = strdup(cwd);
6384 if (repo_path == NULL) {
6385 error = got_error_from_errno("strdup");
6386 goto done;
6391 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6392 if (error != NULL)
6393 goto done;
6395 #ifndef PROFILE
6396 if (do_list) {
6397 /* Remove "cpath" promise. */
6398 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6399 NULL) == -1)
6400 err(1, "pledge");
6402 #endif
6404 error = apply_unveil(got_repo_get_path(repo), do_list,
6405 worktree ? got_worktree_get_root_path(worktree) : NULL);
6406 if (error)
6407 goto done;
6409 if (do_list)
6410 error = list_refs(repo, refname, sort_by_time);
6411 else if (do_delete)
6412 error = delete_ref_by_name(repo, refname);
6413 else if (symref_target)
6414 error = add_symref(repo, refname, symref_target);
6415 else {
6416 if (obj_arg == NULL)
6417 usage_ref();
6418 error = add_ref(repo, refname, obj_arg);
6420 done:
6421 free(refname);
6422 if (repo) {
6423 const struct got_error *close_err = got_repo_close(repo);
6424 if (error == NULL)
6425 error = close_err;
6427 if (worktree)
6428 got_worktree_close(worktree);
6429 if (pack_fds) {
6430 const struct got_error *pack_err =
6431 got_repo_pack_fds_close(pack_fds);
6432 if (error == NULL)
6433 error = pack_err;
6435 free(cwd);
6436 free(repo_path);
6437 return error;
6440 __dead static void
6441 usage_branch(void)
6443 fprintf(stderr,
6444 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6445 "[-n] [name]\n", getprogname());
6446 exit(1);
6449 static const struct got_error *
6450 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6451 struct got_reference *ref)
6453 const struct got_error *err = NULL;
6454 const char *refname, *marker = " ";
6455 char *refstr;
6457 refname = got_ref_get_name(ref);
6458 if (worktree && strcmp(refname,
6459 got_worktree_get_head_ref_name(worktree)) == 0) {
6460 struct got_object_id *id = NULL;
6462 err = got_ref_resolve(&id, repo, ref);
6463 if (err)
6464 return err;
6465 if (got_object_id_cmp(id,
6466 got_worktree_get_base_commit_id(worktree)) == 0)
6467 marker = "* ";
6468 else
6469 marker = "~ ";
6470 free(id);
6473 if (strncmp(refname, "refs/heads/", 11) == 0)
6474 refname += 11;
6475 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6476 refname += 18;
6477 if (strncmp(refname, "refs/remotes/", 13) == 0)
6478 refname += 13;
6480 refstr = got_ref_to_str(ref);
6481 if (refstr == NULL)
6482 return got_error_from_errno("got_ref_to_str");
6484 printf("%s%s: %s\n", marker, refname, refstr);
6485 free(refstr);
6486 return NULL;
6489 static const struct got_error *
6490 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6492 const char *refname;
6494 if (worktree == NULL)
6495 return got_error(GOT_ERR_NOT_WORKTREE);
6497 refname = got_worktree_get_head_ref_name(worktree);
6499 if (strncmp(refname, "refs/heads/", 11) == 0)
6500 refname += 11;
6501 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6502 refname += 18;
6504 printf("%s\n", refname);
6506 return NULL;
6509 static const struct got_error *
6510 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6511 int sort_by_time)
6513 static const struct got_error *err = NULL;
6514 struct got_reflist_head refs;
6515 struct got_reflist_entry *re;
6516 struct got_reference *temp_ref = NULL;
6517 int rebase_in_progress, histedit_in_progress;
6519 TAILQ_INIT(&refs);
6521 if (worktree) {
6522 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6523 worktree);
6524 if (err)
6525 return err;
6527 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6528 worktree);
6529 if (err)
6530 return err;
6532 if (rebase_in_progress || histedit_in_progress) {
6533 err = got_ref_open(&temp_ref, repo,
6534 got_worktree_get_head_ref_name(worktree), 0);
6535 if (err)
6536 return err;
6537 list_branch(repo, worktree, temp_ref);
6538 got_ref_close(temp_ref);
6542 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6543 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6544 repo);
6545 if (err)
6546 return err;
6548 TAILQ_FOREACH(re, &refs, entry)
6549 list_branch(repo, worktree, re->ref);
6551 got_ref_list_free(&refs);
6553 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6554 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6555 repo);
6556 if (err)
6557 return err;
6559 TAILQ_FOREACH(re, &refs, entry)
6560 list_branch(repo, worktree, re->ref);
6562 got_ref_list_free(&refs);
6564 return NULL;
6567 static const struct got_error *
6568 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6569 const char *branch_name)
6571 const struct got_error *err = NULL;
6572 struct got_reference *ref = NULL;
6573 char *refname, *remote_refname = NULL;
6575 if (strncmp(branch_name, "refs/", 5) == 0)
6576 branch_name += 5;
6577 if (strncmp(branch_name, "heads/", 6) == 0)
6578 branch_name += 6;
6579 else if (strncmp(branch_name, "remotes/", 8) == 0)
6580 branch_name += 8;
6582 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6583 return got_error_from_errno("asprintf");
6585 if (asprintf(&remote_refname, "refs/remotes/%s",
6586 branch_name) == -1) {
6587 err = got_error_from_errno("asprintf");
6588 goto done;
6591 err = got_ref_open(&ref, repo, refname, 0);
6592 if (err) {
6593 const struct got_error *err2;
6594 if (err->code != GOT_ERR_NOT_REF)
6595 goto done;
6597 * Keep 'err' intact such that if neither branch exists
6598 * we report "refs/heads" rather than "refs/remotes" in
6599 * our error message.
6601 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6602 if (err2)
6603 goto done;
6604 err = NULL;
6607 if (worktree &&
6608 strcmp(got_worktree_get_head_ref_name(worktree),
6609 got_ref_get_name(ref)) == 0) {
6610 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6611 "will not delete this work tree's current branch");
6612 goto done;
6615 err = delete_ref(repo, ref);
6616 done:
6617 if (ref)
6618 got_ref_close(ref);
6619 free(refname);
6620 free(remote_refname);
6621 return err;
6624 static const struct got_error *
6625 add_branch(struct got_repository *repo, const char *branch_name,
6626 struct got_object_id *base_commit_id)
6628 const struct got_error *err = NULL;
6629 struct got_reference *ref = NULL;
6630 char *base_refname = NULL, *refname = NULL;
6633 * Don't let the user create a branch name with a leading '-'.
6634 * While technically a valid reference name, this case is usually
6635 * an unintended typo.
6637 if (branch_name[0] == '-')
6638 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6640 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6641 branch_name += 11;
6643 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6644 err = got_error_from_errno("asprintf");
6645 goto done;
6648 err = got_ref_open(&ref, repo, refname, 0);
6649 if (err == NULL) {
6650 err = got_error(GOT_ERR_BRANCH_EXISTS);
6651 goto done;
6652 } else if (err->code != GOT_ERR_NOT_REF)
6653 goto done;
6655 err = got_ref_alloc(&ref, refname, base_commit_id);
6656 if (err)
6657 goto done;
6659 err = got_ref_write(ref, repo);
6660 done:
6661 if (ref)
6662 got_ref_close(ref);
6663 free(base_refname);
6664 free(refname);
6665 return err;
6668 static const struct got_error *
6669 cmd_branch(int argc, char *argv[])
6671 const struct got_error *error = NULL;
6672 struct got_repository *repo = NULL;
6673 struct got_worktree *worktree = NULL;
6674 char *cwd = NULL, *repo_path = NULL;
6675 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6676 const char *delref = NULL, *commit_id_arg = NULL;
6677 struct got_reference *ref = NULL;
6678 struct got_pathlist_head paths;
6679 struct got_pathlist_entry *pe;
6680 struct got_object_id *commit_id = NULL;
6681 char *commit_id_str = NULL;
6682 int *pack_fds = NULL;
6684 TAILQ_INIT(&paths);
6686 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6687 switch (ch) {
6688 case 'c':
6689 commit_id_arg = optarg;
6690 break;
6691 case 'd':
6692 delref = optarg;
6693 break;
6694 case 'r':
6695 repo_path = realpath(optarg, NULL);
6696 if (repo_path == NULL)
6697 return got_error_from_errno2("realpath",
6698 optarg);
6699 got_path_strip_trailing_slashes(repo_path);
6700 break;
6701 case 'l':
6702 do_list = 1;
6703 break;
6704 case 'n':
6705 do_update = 0;
6706 break;
6707 case 't':
6708 sort_by_time = 1;
6709 break;
6710 default:
6711 usage_branch();
6712 /* NOTREACHED */
6716 if (do_list && delref)
6717 option_conflict('l', 'd');
6718 if (sort_by_time && !do_list)
6719 errx(1, "-t option requires -l option");
6721 argc -= optind;
6722 argv += optind;
6724 if (!do_list && !delref && argc == 0)
6725 do_show = 1;
6727 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6728 errx(1, "-c option can only be used when creating a branch");
6730 if (do_list || delref) {
6731 if (argc > 0)
6732 usage_branch();
6733 } else if (!do_show && argc != 1)
6734 usage_branch();
6736 #ifndef PROFILE
6737 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6738 "sendfd unveil", NULL) == -1)
6739 err(1, "pledge");
6740 #endif
6741 cwd = getcwd(NULL, 0);
6742 if (cwd == NULL) {
6743 error = got_error_from_errno("getcwd");
6744 goto done;
6747 error = got_repo_pack_fds_open(&pack_fds);
6748 if (error != NULL)
6749 goto done;
6751 if (repo_path == NULL) {
6752 error = got_worktree_open(&worktree, cwd);
6753 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6754 goto done;
6755 else
6756 error = NULL;
6757 if (worktree) {
6758 repo_path =
6759 strdup(got_worktree_get_repo_path(worktree));
6760 if (repo_path == NULL)
6761 error = got_error_from_errno("strdup");
6762 if (error)
6763 goto done;
6764 } else {
6765 repo_path = strdup(cwd);
6766 if (repo_path == NULL) {
6767 error = got_error_from_errno("strdup");
6768 goto done;
6773 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6774 if (error != NULL)
6775 goto done;
6777 #ifndef PROFILE
6778 if (do_list || do_show) {
6779 /* Remove "cpath" promise. */
6780 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6781 NULL) == -1)
6782 err(1, "pledge");
6784 #endif
6786 error = apply_unveil(got_repo_get_path(repo), do_list,
6787 worktree ? got_worktree_get_root_path(worktree) : NULL);
6788 if (error)
6789 goto done;
6791 if (do_show)
6792 error = show_current_branch(repo, worktree);
6793 else if (do_list)
6794 error = list_branches(repo, worktree, sort_by_time);
6795 else if (delref)
6796 error = delete_branch(repo, worktree, delref);
6797 else {
6798 struct got_reflist_head refs;
6799 TAILQ_INIT(&refs);
6800 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6801 NULL);
6802 if (error)
6803 goto done;
6804 if (commit_id_arg == NULL)
6805 commit_id_arg = worktree ?
6806 got_worktree_get_head_ref_name(worktree) :
6807 GOT_REF_HEAD;
6808 error = got_repo_match_object_id(&commit_id, NULL,
6809 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6810 got_ref_list_free(&refs);
6811 if (error)
6812 goto done;
6813 error = add_branch(repo, argv[0], commit_id);
6814 if (error)
6815 goto done;
6816 if (worktree && do_update) {
6817 struct got_update_progress_arg upa;
6818 char *branch_refname = NULL;
6820 error = got_object_id_str(&commit_id_str, commit_id);
6821 if (error)
6822 goto done;
6823 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6824 worktree);
6825 if (error)
6826 goto done;
6827 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6828 == -1) {
6829 error = got_error_from_errno("asprintf");
6830 goto done;
6832 error = got_ref_open(&ref, repo, branch_refname, 0);
6833 free(branch_refname);
6834 if (error)
6835 goto done;
6836 error = switch_head_ref(ref, commit_id, worktree,
6837 repo);
6838 if (error)
6839 goto done;
6840 error = got_worktree_set_base_commit_id(worktree, repo,
6841 commit_id);
6842 if (error)
6843 goto done;
6844 memset(&upa, 0, sizeof(upa));
6845 error = got_worktree_checkout_files(worktree, &paths,
6846 repo, update_progress, &upa, check_cancelled,
6847 NULL);
6848 if (error)
6849 goto done;
6850 if (upa.did_something) {
6851 printf("Updated to %s: %s\n",
6852 got_worktree_get_head_ref_name(worktree),
6853 commit_id_str);
6855 print_update_progress_stats(&upa);
6858 done:
6859 if (ref)
6860 got_ref_close(ref);
6861 if (repo) {
6862 const struct got_error *close_err = got_repo_close(repo);
6863 if (error == NULL)
6864 error = close_err;
6866 if (worktree)
6867 got_worktree_close(worktree);
6868 if (pack_fds) {
6869 const struct got_error *pack_err =
6870 got_repo_pack_fds_close(pack_fds);
6871 if (error == NULL)
6872 error = pack_err;
6874 free(cwd);
6875 free(repo_path);
6876 free(commit_id);
6877 free(commit_id_str);
6878 TAILQ_FOREACH(pe, &paths, entry)
6879 free((char *)pe->path);
6880 got_pathlist_free(&paths);
6881 return error;
6885 __dead static void
6886 usage_tag(void)
6888 fprintf(stderr,
6889 "usage: %s tag [-c commit] [-r repository] [-l] "
6890 "[-m message] [-s signer-id] [-v] [-V] name\n",
6891 getprogname());
6892 exit(1);
6895 #if 0
6896 static const struct got_error *
6897 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6899 const struct got_error *err = NULL;
6900 struct got_reflist_entry *re, *se, *new;
6901 struct got_object_id *re_id, *se_id;
6902 struct got_tag_object *re_tag, *se_tag;
6903 time_t re_time, se_time;
6905 STAILQ_FOREACH(re, tags, entry) {
6906 se = STAILQ_FIRST(sorted);
6907 if (se == NULL) {
6908 err = got_reflist_entry_dup(&new, re);
6909 if (err)
6910 return err;
6911 STAILQ_INSERT_HEAD(sorted, new, entry);
6912 continue;
6913 } else {
6914 err = got_ref_resolve(&re_id, repo, re->ref);
6915 if (err)
6916 break;
6917 err = got_object_open_as_tag(&re_tag, repo, re_id);
6918 free(re_id);
6919 if (err)
6920 break;
6921 re_time = got_object_tag_get_tagger_time(re_tag);
6922 got_object_tag_close(re_tag);
6925 while (se) {
6926 err = got_ref_resolve(&se_id, repo, re->ref);
6927 if (err)
6928 break;
6929 err = got_object_open_as_tag(&se_tag, repo, se_id);
6930 free(se_id);
6931 if (err)
6932 break;
6933 se_time = got_object_tag_get_tagger_time(se_tag);
6934 got_object_tag_close(se_tag);
6936 if (se_time > re_time) {
6937 err = got_reflist_entry_dup(&new, re);
6938 if (err)
6939 return err;
6940 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6941 break;
6943 se = STAILQ_NEXT(se, entry);
6944 continue;
6947 done:
6948 return err;
6950 #endif
6952 static const struct got_error *
6953 get_tag_refname(char **refname, const char *tag_name)
6955 const struct got_error *err;
6957 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6958 *refname = strdup(tag_name);
6959 if (*refname == NULL)
6960 return got_error_from_errno("strdup");
6961 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6962 err = got_error_from_errno("asprintf");
6963 *refname = NULL;
6964 return err;
6967 return NULL;
6970 static const struct got_error *
6971 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6972 const char *allowed_signers, const char *revoked_signers, int verbosity)
6974 static const struct got_error *err = NULL;
6975 struct got_reflist_head refs;
6976 struct got_reflist_entry *re;
6977 char *wanted_refname = NULL;
6978 int bad_sigs = 0;
6980 TAILQ_INIT(&refs);
6982 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6983 if (err)
6984 return err;
6986 if (tag_name) {
6987 struct got_reference *ref;
6988 err = get_tag_refname(&wanted_refname, tag_name);
6989 if (err)
6990 goto done;
6991 /* Wanted tag reference should exist. */
6992 err = got_ref_open(&ref, repo, wanted_refname, 0);
6993 if (err)
6994 goto done;
6995 got_ref_close(ref);
6998 TAILQ_FOREACH(re, &refs, entry) {
6999 const char *refname;
7000 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7001 char datebuf[26];
7002 const char *tagger, *ssh_sig = NULL;
7003 char *sig_msg = NULL;
7004 time_t tagger_time;
7005 struct got_object_id *id;
7006 struct got_tag_object *tag;
7007 struct got_commit_object *commit = NULL;
7009 refname = got_ref_get_name(re->ref);
7010 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7011 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7012 continue;
7013 refname += 10;
7014 refstr = got_ref_to_str(re->ref);
7015 if (refstr == NULL) {
7016 err = got_error_from_errno("got_ref_to_str");
7017 break;
7020 err = got_ref_resolve(&id, repo, re->ref);
7021 if (err)
7022 break;
7023 err = got_object_open_as_tag(&tag, repo, id);
7024 if (err) {
7025 if (err->code != GOT_ERR_OBJ_TYPE) {
7026 free(id);
7027 break;
7029 /* "lightweight" tag */
7030 err = got_object_open_as_commit(&commit, repo, id);
7031 if (err) {
7032 free(id);
7033 break;
7035 tagger = got_object_commit_get_committer(commit);
7036 tagger_time =
7037 got_object_commit_get_committer_time(commit);
7038 err = got_object_id_str(&id_str, id);
7039 free(id);
7040 if (err)
7041 break;
7042 } else {
7043 free(id);
7044 tagger = got_object_tag_get_tagger(tag);
7045 tagger_time = got_object_tag_get_tagger_time(tag);
7046 err = got_object_id_str(&id_str,
7047 got_object_tag_get_object_id(tag));
7048 if (err)
7049 break;
7052 if (verify_tags) {
7053 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7054 got_object_tag_get_message(tag));
7055 if (ssh_sig && allowed_signers == NULL) {
7056 err = got_error_msg(
7057 GOT_ERR_VERIFY_TAG_SIGNATURE,
7058 "SSH signature verification requires "
7059 "setting allowed_signers in "
7060 "got.conf(5)");
7061 break;
7065 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7066 free(refstr);
7067 printf("from: %s\n", tagger);
7068 datestr = get_datestr(&tagger_time, datebuf);
7069 if (datestr)
7070 printf("date: %s UTC\n", datestr);
7071 if (commit)
7072 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7073 else {
7074 switch (got_object_tag_get_object_type(tag)) {
7075 case GOT_OBJ_TYPE_BLOB:
7076 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7077 id_str);
7078 break;
7079 case GOT_OBJ_TYPE_TREE:
7080 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7081 id_str);
7082 break;
7083 case GOT_OBJ_TYPE_COMMIT:
7084 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7085 id_str);
7086 break;
7087 case GOT_OBJ_TYPE_TAG:
7088 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7089 id_str);
7090 break;
7091 default:
7092 break;
7095 free(id_str);
7097 if (ssh_sig) {
7098 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7099 allowed_signers, revoked_signers, verbosity);
7100 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7101 bad_sigs = 1;
7102 else if (err)
7103 break;
7104 printf("signature: %s", sig_msg);
7105 free(sig_msg);
7106 sig_msg = NULL;
7109 if (commit) {
7110 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7111 if (err)
7112 break;
7113 got_object_commit_close(commit);
7114 } else {
7115 tagmsg0 = strdup(got_object_tag_get_message(tag));
7116 got_object_tag_close(tag);
7117 if (tagmsg0 == NULL) {
7118 err = got_error_from_errno("strdup");
7119 break;
7123 tagmsg = tagmsg0;
7124 do {
7125 line = strsep(&tagmsg, "\n");
7126 if (line)
7127 printf(" %s\n", line);
7128 } while (line);
7129 free(tagmsg0);
7131 done:
7132 got_ref_list_free(&refs);
7133 free(wanted_refname);
7135 if (err == NULL && bad_sigs)
7136 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7137 return err;
7140 static const struct got_error *
7141 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7142 const char *tag_name, const char *repo_path)
7144 const struct got_error *err = NULL;
7145 char *template = NULL, *initial_content = NULL;
7146 char *editor = NULL;
7147 int initial_content_len;
7148 int fd = -1;
7150 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7151 err = got_error_from_errno("asprintf");
7152 goto done;
7155 initial_content_len = asprintf(&initial_content,
7156 "\n# tagging commit %s as %s\n",
7157 commit_id_str, tag_name);
7158 if (initial_content_len == -1) {
7159 err = got_error_from_errno("asprintf");
7160 goto done;
7163 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7164 if (err)
7165 goto done;
7167 if (write(fd, initial_content, initial_content_len) == -1) {
7168 err = got_error_from_errno2("write", *tagmsg_path);
7169 goto done;
7172 err = get_editor(&editor);
7173 if (err)
7174 goto done;
7175 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7176 initial_content_len, 1);
7177 done:
7178 free(initial_content);
7179 free(template);
7180 free(editor);
7182 if (fd != -1 && close(fd) == -1 && err == NULL)
7183 err = got_error_from_errno2("close", *tagmsg_path);
7185 if (err) {
7186 free(*tagmsg);
7187 *tagmsg = NULL;
7189 return err;
7192 static const struct got_error *
7193 add_tag(struct got_repository *repo, const char *tagger,
7194 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7195 const char *signer_id, int verbosity)
7197 const struct got_error *err = NULL;
7198 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7199 char *label = NULL, *commit_id_str = NULL;
7200 struct got_reference *ref = NULL;
7201 char *refname = NULL, *tagmsg = NULL;
7202 char *tagmsg_path = NULL, *tag_id_str = NULL;
7203 int preserve_tagmsg = 0;
7204 struct got_reflist_head refs;
7206 TAILQ_INIT(&refs);
7209 * Don't let the user create a tag name with a leading '-'.
7210 * While technically a valid reference name, this case is usually
7211 * an unintended typo.
7213 if (tag_name[0] == '-')
7214 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7216 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7217 if (err)
7218 goto done;
7220 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7221 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7222 if (err)
7223 goto done;
7225 err = got_object_id_str(&commit_id_str, commit_id);
7226 if (err)
7227 goto done;
7229 err = get_tag_refname(&refname, tag_name);
7230 if (err)
7231 goto done;
7232 if (strncmp("refs/tags/", tag_name, 10) == 0)
7233 tag_name += 10;
7235 err = got_ref_open(&ref, repo, refname, 0);
7236 if (err == NULL) {
7237 err = got_error(GOT_ERR_TAG_EXISTS);
7238 goto done;
7239 } else if (err->code != GOT_ERR_NOT_REF)
7240 goto done;
7242 if (tagmsg_arg == NULL) {
7243 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7244 tag_name, got_repo_get_path(repo));
7245 if (err) {
7246 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7247 tagmsg_path != NULL)
7248 preserve_tagmsg = 1;
7249 goto done;
7251 /* Editor is done; we can now apply unveil(2) */
7252 err = got_sigs_apply_unveil();
7253 if (err)
7254 goto done;
7255 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7256 if (err)
7257 goto done;
7260 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7261 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7262 verbosity);
7263 if (err) {
7264 if (tagmsg_path)
7265 preserve_tagmsg = 1;
7266 goto done;
7269 err = got_ref_alloc(&ref, refname, tag_id);
7270 if (err) {
7271 if (tagmsg_path)
7272 preserve_tagmsg = 1;
7273 goto done;
7276 err = got_ref_write(ref, repo);
7277 if (err) {
7278 if (tagmsg_path)
7279 preserve_tagmsg = 1;
7280 goto done;
7283 err = got_object_id_str(&tag_id_str, tag_id);
7284 if (err) {
7285 if (tagmsg_path)
7286 preserve_tagmsg = 1;
7287 goto done;
7289 printf("Created tag %s\n", tag_id_str);
7290 done:
7291 if (preserve_tagmsg) {
7292 fprintf(stderr, "%s: tag message preserved in %s\n",
7293 getprogname(), tagmsg_path);
7294 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7295 err = got_error_from_errno2("unlink", tagmsg_path);
7296 free(tag_id_str);
7297 if (ref)
7298 got_ref_close(ref);
7299 free(commit_id);
7300 free(commit_id_str);
7301 free(refname);
7302 free(tagmsg);
7303 free(tagmsg_path);
7304 got_ref_list_free(&refs);
7305 return err;
7308 static const struct got_error *
7309 cmd_tag(int argc, char *argv[])
7311 const struct got_error *error = NULL;
7312 struct got_repository *repo = NULL;
7313 struct got_worktree *worktree = NULL;
7314 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7315 char *gitconfig_path = NULL, *tagger = NULL;
7316 char *allowed_signers = NULL, *revoked_signers = NULL;
7317 char *signer_id = NULL;
7318 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7319 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7320 int *pack_fds = NULL;
7322 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7323 switch (ch) {
7324 case 'c':
7325 commit_id_arg = optarg;
7326 break;
7327 case 'm':
7328 tagmsg = optarg;
7329 break;
7330 case 'r':
7331 repo_path = realpath(optarg, NULL);
7332 if (repo_path == NULL) {
7333 error = got_error_from_errno2("realpath",
7334 optarg);
7335 goto done;
7337 got_path_strip_trailing_slashes(repo_path);
7338 break;
7339 case 'l':
7340 do_list = 1;
7341 break;
7342 case 's':
7343 signer_id = strdup(optarg);
7344 if (signer_id == NULL) {
7345 error = got_error_from_errno("strdup");
7346 goto done;
7348 break;
7349 case 'V':
7350 verify_tags = 1;
7351 break;
7352 case 'v':
7353 if (verbosity < 0)
7354 verbosity = 0;
7355 else if (verbosity < 3)
7356 verbosity++;
7357 break;
7358 default:
7359 usage_tag();
7360 /* NOTREACHED */
7364 argc -= optind;
7365 argv += optind;
7367 if (do_list || verify_tags) {
7368 if (commit_id_arg != NULL)
7369 errx(1,
7370 "-c option can only be used when creating a tag");
7371 if (tagmsg) {
7372 if (do_list)
7373 option_conflict('l', 'm');
7374 else
7375 option_conflict('V', 'm');
7377 if (signer_id) {
7378 if (do_list)
7379 option_conflict('l', 's');
7380 else
7381 option_conflict('V', 's');
7383 if (argc > 1)
7384 usage_tag();
7385 } else if (argc != 1)
7386 usage_tag();
7388 if (argc == 1)
7389 tag_name = argv[0];
7391 #ifndef PROFILE
7392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7393 "sendfd unveil", NULL) == -1)
7394 err(1, "pledge");
7395 #endif
7396 cwd = getcwd(NULL, 0);
7397 if (cwd == NULL) {
7398 error = got_error_from_errno("getcwd");
7399 goto done;
7402 error = got_repo_pack_fds_open(&pack_fds);
7403 if (error != NULL)
7404 goto done;
7406 if (repo_path == NULL) {
7407 error = got_worktree_open(&worktree, cwd);
7408 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7409 goto done;
7410 else
7411 error = NULL;
7412 if (worktree) {
7413 repo_path =
7414 strdup(got_worktree_get_repo_path(worktree));
7415 if (repo_path == NULL)
7416 error = got_error_from_errno("strdup");
7417 if (error)
7418 goto done;
7419 } else {
7420 repo_path = strdup(cwd);
7421 if (repo_path == NULL) {
7422 error = got_error_from_errno("strdup");
7423 goto done;
7428 if (do_list || verify_tags) {
7429 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7430 if (error != NULL)
7431 goto done;
7432 error = get_allowed_signers(&allowed_signers, repo, worktree);
7433 if (error)
7434 goto done;
7435 error = get_revoked_signers(&revoked_signers, repo, worktree);
7436 if (error)
7437 goto done;
7438 if (worktree) {
7439 /* Release work tree lock. */
7440 got_worktree_close(worktree);
7441 worktree = NULL;
7445 * Remove "cpath" promise unless needed for signature tmpfile
7446 * creation.
7448 if (verify_tags)
7449 got_sigs_apply_unveil();
7450 else {
7451 #ifndef PROFILE
7452 if (pledge("stdio rpath wpath flock proc exec sendfd "
7453 "unveil", NULL) == -1)
7454 err(1, "pledge");
7455 #endif
7457 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7458 if (error)
7459 goto done;
7460 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7461 revoked_signers, verbosity);
7462 } else {
7463 error = get_gitconfig_path(&gitconfig_path);
7464 if (error)
7465 goto done;
7466 error = got_repo_open(&repo, repo_path, gitconfig_path,
7467 pack_fds);
7468 if (error != NULL)
7469 goto done;
7471 error = get_author(&tagger, repo, worktree);
7472 if (error)
7473 goto done;
7474 if (signer_id == NULL) {
7475 error = get_signer_id(&signer_id, repo, worktree);
7476 if (error)
7477 goto done;
7479 if (worktree) {
7480 /* Release work tree lock. */
7481 got_worktree_close(worktree);
7482 worktree = NULL;
7485 if (tagmsg) {
7486 if (signer_id) {
7487 error = got_sigs_apply_unveil();
7488 if (error)
7489 goto done;
7491 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7492 if (error)
7493 goto done;
7496 if (commit_id_arg == NULL) {
7497 struct got_reference *head_ref;
7498 struct got_object_id *commit_id;
7499 error = got_ref_open(&head_ref, repo,
7500 worktree ? got_worktree_get_head_ref_name(worktree)
7501 : GOT_REF_HEAD, 0);
7502 if (error)
7503 goto done;
7504 error = got_ref_resolve(&commit_id, repo, head_ref);
7505 got_ref_close(head_ref);
7506 if (error)
7507 goto done;
7508 error = got_object_id_str(&commit_id_str, commit_id);
7509 free(commit_id);
7510 if (error)
7511 goto done;
7514 error = add_tag(repo, tagger, tag_name,
7515 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7516 signer_id, verbosity);
7518 done:
7519 if (repo) {
7520 const struct got_error *close_err = got_repo_close(repo);
7521 if (error == NULL)
7522 error = close_err;
7524 if (worktree)
7525 got_worktree_close(worktree);
7526 if (pack_fds) {
7527 const struct got_error *pack_err =
7528 got_repo_pack_fds_close(pack_fds);
7529 if (error == NULL)
7530 error = pack_err;
7532 free(cwd);
7533 free(repo_path);
7534 free(gitconfig_path);
7535 free(commit_id_str);
7536 free(tagger);
7537 free(allowed_signers);
7538 free(revoked_signers);
7539 free(signer_id);
7540 return error;
7543 __dead static void
7544 usage_add(void)
7546 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7547 getprogname());
7548 exit(1);
7551 static const struct got_error *
7552 add_progress(void *arg, unsigned char status, const char *path)
7554 while (path[0] == '/')
7555 path++;
7556 printf("%c %s\n", status, path);
7557 return NULL;
7560 static const struct got_error *
7561 cmd_add(int argc, char *argv[])
7563 const struct got_error *error = NULL;
7564 struct got_repository *repo = NULL;
7565 struct got_worktree *worktree = NULL;
7566 char *cwd = NULL;
7567 struct got_pathlist_head paths;
7568 struct got_pathlist_entry *pe;
7569 int ch, can_recurse = 0, no_ignores = 0;
7570 int *pack_fds = NULL;
7572 TAILQ_INIT(&paths);
7574 while ((ch = getopt(argc, argv, "IR")) != -1) {
7575 switch (ch) {
7576 case 'I':
7577 no_ignores = 1;
7578 break;
7579 case 'R':
7580 can_recurse = 1;
7581 break;
7582 default:
7583 usage_add();
7584 /* NOTREACHED */
7588 argc -= optind;
7589 argv += optind;
7591 #ifndef PROFILE
7592 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7593 NULL) == -1)
7594 err(1, "pledge");
7595 #endif
7596 if (argc < 1)
7597 usage_add();
7599 cwd = getcwd(NULL, 0);
7600 if (cwd == NULL) {
7601 error = got_error_from_errno("getcwd");
7602 goto done;
7605 error = got_repo_pack_fds_open(&pack_fds);
7606 if (error != NULL)
7607 goto done;
7609 error = got_worktree_open(&worktree, cwd);
7610 if (error) {
7611 if (error->code == GOT_ERR_NOT_WORKTREE)
7612 error = wrap_not_worktree_error(error, "add", cwd);
7613 goto done;
7616 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7617 NULL, pack_fds);
7618 if (error != NULL)
7619 goto done;
7621 error = apply_unveil(got_repo_get_path(repo), 1,
7622 got_worktree_get_root_path(worktree));
7623 if (error)
7624 goto done;
7626 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7627 if (error)
7628 goto done;
7630 if (!can_recurse) {
7631 char *ondisk_path;
7632 struct stat sb;
7633 TAILQ_FOREACH(pe, &paths, entry) {
7634 if (asprintf(&ondisk_path, "%s/%s",
7635 got_worktree_get_root_path(worktree),
7636 pe->path) == -1) {
7637 error = got_error_from_errno("asprintf");
7638 goto done;
7640 if (lstat(ondisk_path, &sb) == -1) {
7641 if (errno == ENOENT) {
7642 free(ondisk_path);
7643 continue;
7645 error = got_error_from_errno2("lstat",
7646 ondisk_path);
7647 free(ondisk_path);
7648 goto done;
7650 free(ondisk_path);
7651 if (S_ISDIR(sb.st_mode)) {
7652 error = got_error_msg(GOT_ERR_BAD_PATH,
7653 "adding directories requires -R option");
7654 goto done;
7659 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7660 NULL, repo, no_ignores);
7661 done:
7662 if (repo) {
7663 const struct got_error *close_err = got_repo_close(repo);
7664 if (error == NULL)
7665 error = close_err;
7667 if (worktree)
7668 got_worktree_close(worktree);
7669 if (pack_fds) {
7670 const struct got_error *pack_err =
7671 got_repo_pack_fds_close(pack_fds);
7672 if (error == NULL)
7673 error = pack_err;
7675 TAILQ_FOREACH(pe, &paths, entry)
7676 free((char *)pe->path);
7677 got_pathlist_free(&paths);
7678 free(cwd);
7679 return error;
7682 __dead static void
7683 usage_remove(void)
7685 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7686 "path ...\n", getprogname());
7687 exit(1);
7690 static const struct got_error *
7691 print_remove_status(void *arg, unsigned char status,
7692 unsigned char staged_status, const char *path)
7694 while (path[0] == '/')
7695 path++;
7696 if (status == GOT_STATUS_NONEXISTENT)
7697 return NULL;
7698 if (status == staged_status && (status == GOT_STATUS_DELETE))
7699 status = GOT_STATUS_NO_CHANGE;
7700 printf("%c%c %s\n", status, staged_status, path);
7701 return NULL;
7704 static const struct got_error *
7705 cmd_remove(int argc, char *argv[])
7707 const struct got_error *error = NULL;
7708 struct got_worktree *worktree = NULL;
7709 struct got_repository *repo = NULL;
7710 const char *status_codes = NULL;
7711 char *cwd = NULL;
7712 struct got_pathlist_head paths;
7713 struct got_pathlist_entry *pe;
7714 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7715 int ignore_missing_paths = 0;
7716 int *pack_fds = NULL;
7718 TAILQ_INIT(&paths);
7720 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7721 switch (ch) {
7722 case 'f':
7723 delete_local_mods = 1;
7724 ignore_missing_paths = 1;
7725 break;
7726 case 'k':
7727 keep_on_disk = 1;
7728 break;
7729 case 'R':
7730 can_recurse = 1;
7731 break;
7732 case 's':
7733 for (i = 0; i < strlen(optarg); i++) {
7734 switch (optarg[i]) {
7735 case GOT_STATUS_MODIFY:
7736 delete_local_mods = 1;
7737 break;
7738 case GOT_STATUS_MISSING:
7739 ignore_missing_paths = 1;
7740 break;
7741 default:
7742 errx(1, "invalid status code '%c'",
7743 optarg[i]);
7746 status_codes = optarg;
7747 break;
7748 default:
7749 usage_remove();
7750 /* NOTREACHED */
7754 argc -= optind;
7755 argv += optind;
7757 #ifndef PROFILE
7758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7759 NULL) == -1)
7760 err(1, "pledge");
7761 #endif
7762 if (argc < 1)
7763 usage_remove();
7765 cwd = getcwd(NULL, 0);
7766 if (cwd == NULL) {
7767 error = got_error_from_errno("getcwd");
7768 goto done;
7771 error = got_repo_pack_fds_open(&pack_fds);
7772 if (error != NULL)
7773 goto done;
7775 error = got_worktree_open(&worktree, cwd);
7776 if (error) {
7777 if (error->code == GOT_ERR_NOT_WORKTREE)
7778 error = wrap_not_worktree_error(error, "remove", cwd);
7779 goto done;
7782 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7783 NULL, pack_fds);
7784 if (error)
7785 goto done;
7787 error = apply_unveil(got_repo_get_path(repo), 1,
7788 got_worktree_get_root_path(worktree));
7789 if (error)
7790 goto done;
7792 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7793 if (error)
7794 goto done;
7796 if (!can_recurse) {
7797 char *ondisk_path;
7798 struct stat sb;
7799 TAILQ_FOREACH(pe, &paths, entry) {
7800 if (asprintf(&ondisk_path, "%s/%s",
7801 got_worktree_get_root_path(worktree),
7802 pe->path) == -1) {
7803 error = got_error_from_errno("asprintf");
7804 goto done;
7806 if (lstat(ondisk_path, &sb) == -1) {
7807 if (errno == ENOENT) {
7808 free(ondisk_path);
7809 continue;
7811 error = got_error_from_errno2("lstat",
7812 ondisk_path);
7813 free(ondisk_path);
7814 goto done;
7816 free(ondisk_path);
7817 if (S_ISDIR(sb.st_mode)) {
7818 error = got_error_msg(GOT_ERR_BAD_PATH,
7819 "removing directories requires -R option");
7820 goto done;
7825 error = got_worktree_schedule_delete(worktree, &paths,
7826 delete_local_mods, status_codes, print_remove_status, NULL,
7827 repo, keep_on_disk, ignore_missing_paths);
7828 done:
7829 if (repo) {
7830 const struct got_error *close_err = got_repo_close(repo);
7831 if (error == NULL)
7832 error = close_err;
7834 if (worktree)
7835 got_worktree_close(worktree);
7836 if (pack_fds) {
7837 const struct got_error *pack_err =
7838 got_repo_pack_fds_close(pack_fds);
7839 if (error == NULL)
7840 error = pack_err;
7842 TAILQ_FOREACH(pe, &paths, entry)
7843 free((char *)pe->path);
7844 got_pathlist_free(&paths);
7845 free(cwd);
7846 return error;
7849 __dead static void
7850 usage_patch(void)
7852 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7853 "[-R] [patchfile]\n", getprogname());
7854 exit(1);
7857 static const struct got_error *
7858 patch_from_stdin(int *patchfd)
7860 const struct got_error *err = NULL;
7861 ssize_t r;
7862 char *path, buf[BUFSIZ];
7863 sig_t sighup, sigint, sigquit;
7865 err = got_opentemp_named_fd(&path, patchfd,
7866 GOT_TMPDIR_STR "/got-patch");
7867 if (err)
7868 return err;
7869 unlink(path);
7870 free(path);
7872 sighup = signal(SIGHUP, SIG_DFL);
7873 sigint = signal(SIGINT, SIG_DFL);
7874 sigquit = signal(SIGQUIT, SIG_DFL);
7876 for (;;) {
7877 r = read(0, buf, sizeof(buf));
7878 if (r == -1) {
7879 err = got_error_from_errno("read");
7880 break;
7882 if (r == 0)
7883 break;
7884 if (write(*patchfd, buf, r) == -1) {
7885 err = got_error_from_errno("write");
7886 break;
7890 signal(SIGHUP, sighup);
7891 signal(SIGINT, sigint);
7892 signal(SIGQUIT, sigquit);
7894 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7895 err = got_error_from_errno("lseek");
7897 if (err != NULL) {
7898 close(*patchfd);
7899 *patchfd = -1;
7902 return err;
7905 static const struct got_error *
7906 patch_progress(void *arg, const char *old, const char *new,
7907 unsigned char status, const struct got_error *error, int old_from,
7908 int old_lines, int new_from, int new_lines, int offset,
7909 int ws_mangled, const struct got_error *hunk_err)
7911 const char *path = new == NULL ? old : new;
7913 while (*path == '/')
7914 path++;
7916 if (status != 0)
7917 printf("%c %s\n", status, path);
7919 if (error != NULL)
7920 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7922 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7923 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7924 old_lines, new_from, new_lines);
7925 if (hunk_err != NULL)
7926 printf("%s\n", hunk_err->msg);
7927 else if (offset != 0)
7928 printf("applied with offset %d\n", offset);
7929 else
7930 printf("hunk contains mangled whitespace\n");
7933 return NULL;
7936 static const struct got_error *
7937 cmd_patch(int argc, char *argv[])
7939 const struct got_error *error = NULL, *close_error = NULL;
7940 struct got_worktree *worktree = NULL;
7941 struct got_repository *repo = NULL;
7942 const char *errstr;
7943 char *cwd = NULL;
7944 int ch, nop = 0, strip = -1, reverse = 0;
7945 int patchfd;
7946 int *pack_fds = NULL;
7948 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7949 switch (ch) {
7950 case 'n':
7951 nop = 1;
7952 break;
7953 case 'p':
7954 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7955 if (errstr != NULL)
7956 errx(1, "pathname strip count is %s: %s",
7957 errstr, optarg);
7958 break;
7959 case 'R':
7960 reverse = 1;
7961 break;
7962 default:
7963 usage_patch();
7964 /* NOTREACHED */
7968 argc -= optind;
7969 argv += optind;
7971 if (argc == 0) {
7972 error = patch_from_stdin(&patchfd);
7973 if (error)
7974 return error;
7975 } else if (argc == 1) {
7976 patchfd = open(argv[0], O_RDONLY);
7977 if (patchfd == -1) {
7978 error = got_error_from_errno2("open", argv[0]);
7979 return error;
7981 } else
7982 usage_patch();
7984 if ((cwd = getcwd(NULL, 0)) == NULL) {
7985 error = got_error_from_errno("getcwd");
7986 goto done;
7989 error = got_repo_pack_fds_open(&pack_fds);
7990 if (error != NULL)
7991 goto done;
7993 error = got_worktree_open(&worktree, cwd);
7994 if (error != NULL)
7995 goto done;
7997 const char *repo_path = got_worktree_get_repo_path(worktree);
7998 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7999 if (error != NULL)
8000 goto done;
8002 error = apply_unveil(got_repo_get_path(repo), 0,
8003 got_worktree_get_root_path(worktree));
8004 if (error != NULL)
8005 goto done;
8007 #ifndef PROFILE
8008 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8009 NULL) == -1)
8010 err(1, "pledge");
8011 #endif
8013 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8014 &patch_progress, NULL, check_cancelled, NULL);
8016 done:
8017 if (repo) {
8018 close_error = got_repo_close(repo);
8019 if (error == NULL)
8020 error = close_error;
8022 if (worktree != NULL) {
8023 close_error = got_worktree_close(worktree);
8024 if (error == NULL)
8025 error = close_error;
8027 if (pack_fds) {
8028 const struct got_error *pack_err =
8029 got_repo_pack_fds_close(pack_fds);
8030 if (error == NULL)
8031 error = pack_err;
8033 free(cwd);
8034 return error;
8037 __dead static void
8038 usage_revert(void)
8040 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8041 "path ...\n", getprogname());
8042 exit(1);
8045 static const struct got_error *
8046 revert_progress(void *arg, unsigned char status, const char *path)
8048 if (status == GOT_STATUS_UNVERSIONED)
8049 return NULL;
8051 while (path[0] == '/')
8052 path++;
8053 printf("%c %s\n", status, path);
8054 return NULL;
8057 struct choose_patch_arg {
8058 FILE *patch_script_file;
8059 const char *action;
8062 static const struct got_error *
8063 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8064 int nchanges, const char *action)
8066 const struct got_error *err;
8067 char *line = NULL;
8068 size_t linesize = 0;
8069 ssize_t linelen;
8071 switch (status) {
8072 case GOT_STATUS_ADD:
8073 printf("A %s\n%s this addition? [y/n] ", path, action);
8074 break;
8075 case GOT_STATUS_DELETE:
8076 printf("D %s\n%s this deletion? [y/n] ", path, action);
8077 break;
8078 case GOT_STATUS_MODIFY:
8079 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8080 return got_error_from_errno("fseek");
8081 printf(GOT_COMMIT_SEP_STR);
8082 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8083 printf("%s", line);
8084 if (linelen == -1 && ferror(patch_file)) {
8085 err = got_error_from_errno("getline");
8086 free(line);
8087 return err;
8089 free(line);
8090 printf(GOT_COMMIT_SEP_STR);
8091 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8092 path, n, nchanges, action);
8093 break;
8094 default:
8095 return got_error_path(path, GOT_ERR_FILE_STATUS);
8098 return NULL;
8101 static const struct got_error *
8102 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8103 FILE *patch_file, int n, int nchanges)
8105 const struct got_error *err = NULL;
8106 char *line = NULL;
8107 size_t linesize = 0;
8108 ssize_t linelen;
8109 int resp = ' ';
8110 struct choose_patch_arg *a = arg;
8112 *choice = GOT_PATCH_CHOICE_NONE;
8114 if (a->patch_script_file) {
8115 char *nl;
8116 err = show_change(status, path, patch_file, n, nchanges,
8117 a->action);
8118 if (err)
8119 return err;
8120 linelen = getline(&line, &linesize, a->patch_script_file);
8121 if (linelen == -1) {
8122 if (ferror(a->patch_script_file))
8123 return got_error_from_errno("getline");
8124 return NULL;
8126 nl = strchr(line, '\n');
8127 if (nl)
8128 *nl = '\0';
8129 if (strcmp(line, "y") == 0) {
8130 *choice = GOT_PATCH_CHOICE_YES;
8131 printf("y\n");
8132 } else if (strcmp(line, "n") == 0) {
8133 *choice = GOT_PATCH_CHOICE_NO;
8134 printf("n\n");
8135 } else if (strcmp(line, "q") == 0 &&
8136 status == GOT_STATUS_MODIFY) {
8137 *choice = GOT_PATCH_CHOICE_QUIT;
8138 printf("q\n");
8139 } else
8140 printf("invalid response '%s'\n", line);
8141 free(line);
8142 return NULL;
8145 while (resp != 'y' && resp != 'n' && resp != 'q') {
8146 err = show_change(status, path, patch_file, n, nchanges,
8147 a->action);
8148 if (err)
8149 return err;
8150 resp = getchar();
8151 if (resp == '\n')
8152 resp = getchar();
8153 if (status == GOT_STATUS_MODIFY) {
8154 if (resp != 'y' && resp != 'n' && resp != 'q') {
8155 printf("invalid response '%c'\n", resp);
8156 resp = ' ';
8158 } else if (resp != 'y' && resp != 'n') {
8159 printf("invalid response '%c'\n", resp);
8160 resp = ' ';
8164 if (resp == 'y')
8165 *choice = GOT_PATCH_CHOICE_YES;
8166 else if (resp == 'n')
8167 *choice = GOT_PATCH_CHOICE_NO;
8168 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8169 *choice = GOT_PATCH_CHOICE_QUIT;
8171 return NULL;
8174 static const struct got_error *
8175 cmd_revert(int argc, char *argv[])
8177 const struct got_error *error = NULL;
8178 struct got_worktree *worktree = NULL;
8179 struct got_repository *repo = NULL;
8180 char *cwd = NULL, *path = NULL;
8181 struct got_pathlist_head paths;
8182 struct got_pathlist_entry *pe;
8183 int ch, can_recurse = 0, pflag = 0;
8184 FILE *patch_script_file = NULL;
8185 const char *patch_script_path = NULL;
8186 struct choose_patch_arg cpa;
8187 int *pack_fds = NULL;
8189 TAILQ_INIT(&paths);
8191 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8192 switch (ch) {
8193 case 'p':
8194 pflag = 1;
8195 break;
8196 case 'F':
8197 patch_script_path = optarg;
8198 break;
8199 case 'R':
8200 can_recurse = 1;
8201 break;
8202 default:
8203 usage_revert();
8204 /* NOTREACHED */
8208 argc -= optind;
8209 argv += optind;
8211 #ifndef PROFILE
8212 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8213 "unveil", NULL) == -1)
8214 err(1, "pledge");
8215 #endif
8216 if (argc < 1)
8217 usage_revert();
8218 if (patch_script_path && !pflag)
8219 errx(1, "-F option can only be used together with -p option");
8221 cwd = getcwd(NULL, 0);
8222 if (cwd == NULL) {
8223 error = got_error_from_errno("getcwd");
8224 goto done;
8227 error = got_repo_pack_fds_open(&pack_fds);
8228 if (error != NULL)
8229 goto done;
8231 error = got_worktree_open(&worktree, cwd);
8232 if (error) {
8233 if (error->code == GOT_ERR_NOT_WORKTREE)
8234 error = wrap_not_worktree_error(error, "revert", cwd);
8235 goto done;
8238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8239 NULL, pack_fds);
8240 if (error != NULL)
8241 goto done;
8243 if (patch_script_path) {
8244 patch_script_file = fopen(patch_script_path, "re");
8245 if (patch_script_file == NULL) {
8246 error = got_error_from_errno2("fopen",
8247 patch_script_path);
8248 goto done;
8251 error = apply_unveil(got_repo_get_path(repo), 1,
8252 got_worktree_get_root_path(worktree));
8253 if (error)
8254 goto done;
8256 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8257 if (error)
8258 goto done;
8260 if (!can_recurse) {
8261 char *ondisk_path;
8262 struct stat sb;
8263 TAILQ_FOREACH(pe, &paths, entry) {
8264 if (asprintf(&ondisk_path, "%s/%s",
8265 got_worktree_get_root_path(worktree),
8266 pe->path) == -1) {
8267 error = got_error_from_errno("asprintf");
8268 goto done;
8270 if (lstat(ondisk_path, &sb) == -1) {
8271 if (errno == ENOENT) {
8272 free(ondisk_path);
8273 continue;
8275 error = got_error_from_errno2("lstat",
8276 ondisk_path);
8277 free(ondisk_path);
8278 goto done;
8280 free(ondisk_path);
8281 if (S_ISDIR(sb.st_mode)) {
8282 error = got_error_msg(GOT_ERR_BAD_PATH,
8283 "reverting directories requires -R option");
8284 goto done;
8289 cpa.patch_script_file = patch_script_file;
8290 cpa.action = "revert";
8291 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8292 pflag ? choose_patch : NULL, &cpa, repo);
8293 done:
8294 if (patch_script_file && fclose(patch_script_file) == EOF &&
8295 error == NULL)
8296 error = got_error_from_errno2("fclose", patch_script_path);
8297 if (repo) {
8298 const struct got_error *close_err = got_repo_close(repo);
8299 if (error == NULL)
8300 error = close_err;
8302 if (worktree)
8303 got_worktree_close(worktree);
8304 if (pack_fds) {
8305 const struct got_error *pack_err =
8306 got_repo_pack_fds_close(pack_fds);
8307 if (error == NULL)
8308 error = pack_err;
8310 free(path);
8311 free(cwd);
8312 return error;
8315 __dead static void
8316 usage_commit(void)
8318 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8319 "[path ...]\n", getprogname());
8320 exit(1);
8323 struct collect_commit_logmsg_arg {
8324 const char *cmdline_log;
8325 const char *prepared_log;
8326 int non_interactive;
8327 const char *editor;
8328 const char *worktree_path;
8329 const char *branch_name;
8330 const char *repo_path;
8331 char *logmsg_path;
8335 static const struct got_error *
8336 read_prepared_logmsg(char **logmsg, const char *path)
8338 const struct got_error *err = NULL;
8339 FILE *f = NULL;
8340 struct stat sb;
8341 size_t r;
8343 *logmsg = NULL;
8344 memset(&sb, 0, sizeof(sb));
8346 f = fopen(path, "re");
8347 if (f == NULL)
8348 return got_error_from_errno2("fopen", path);
8350 if (fstat(fileno(f), &sb) == -1) {
8351 err = got_error_from_errno2("fstat", path);
8352 goto done;
8354 if (sb.st_size == 0) {
8355 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8356 goto done;
8359 *logmsg = malloc(sb.st_size + 1);
8360 if (*logmsg == NULL) {
8361 err = got_error_from_errno("malloc");
8362 goto done;
8365 r = fread(*logmsg, 1, sb.st_size, f);
8366 if (r != sb.st_size) {
8367 if (ferror(f))
8368 err = got_error_from_errno2("fread", path);
8369 else
8370 err = got_error(GOT_ERR_IO);
8371 goto done;
8373 (*logmsg)[sb.st_size] = '\0';
8374 done:
8375 if (fclose(f) == EOF && err == NULL)
8376 err = got_error_from_errno2("fclose", path);
8377 if (err) {
8378 free(*logmsg);
8379 *logmsg = NULL;
8381 return err;
8385 static const struct got_error *
8386 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8387 void *arg)
8389 char *initial_content = NULL;
8390 struct got_pathlist_entry *pe;
8391 const struct got_error *err = NULL;
8392 char *template = NULL;
8393 struct collect_commit_logmsg_arg *a = arg;
8394 int initial_content_len;
8395 int fd = -1;
8396 size_t len;
8398 /* if a message was specified on the command line, just use it */
8399 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8400 len = strlen(a->cmdline_log) + 1;
8401 *logmsg = malloc(len + 1);
8402 if (*logmsg == NULL)
8403 return got_error_from_errno("malloc");
8404 strlcpy(*logmsg, a->cmdline_log, len);
8405 return NULL;
8406 } else if (a->prepared_log != NULL && a->non_interactive)
8407 return read_prepared_logmsg(logmsg, a->prepared_log);
8409 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8410 return got_error_from_errno("asprintf");
8412 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8413 if (err)
8414 goto done;
8416 if (a->prepared_log) {
8417 char *msg;
8418 err = read_prepared_logmsg(&msg, a->prepared_log);
8419 if (err)
8420 goto done;
8421 if (write(fd, msg, strlen(msg)) == -1) {
8422 err = got_error_from_errno2("write", a->logmsg_path);
8423 free(msg);
8424 goto done;
8426 free(msg);
8429 initial_content_len = asprintf(&initial_content,
8430 "\n# changes to be committed on branch %s:\n",
8431 a->branch_name);
8432 if (initial_content_len == -1) {
8433 err = got_error_from_errno("asprintf");
8434 goto done;
8437 if (write(fd, initial_content, initial_content_len) == -1) {
8438 err = got_error_from_errno2("write", a->logmsg_path);
8439 goto done;
8442 TAILQ_FOREACH(pe, commitable_paths, entry) {
8443 struct got_commitable *ct = pe->data;
8444 dprintf(fd, "# %c %s\n",
8445 got_commitable_get_status(ct),
8446 got_commitable_get_path(ct));
8449 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8450 initial_content_len, a->prepared_log ? 0 : 1);
8451 done:
8452 free(initial_content);
8453 free(template);
8455 if (fd != -1 && close(fd) == -1 && err == NULL)
8456 err = got_error_from_errno2("close", a->logmsg_path);
8458 /* Editor is done; we can now apply unveil(2) */
8459 if (err == NULL)
8460 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8461 if (err) {
8462 free(*logmsg);
8463 *logmsg = NULL;
8465 return err;
8468 static const struct got_error *
8469 cmd_commit(int argc, char *argv[])
8471 const struct got_error *error = NULL;
8472 struct got_worktree *worktree = NULL;
8473 struct got_repository *repo = NULL;
8474 char *cwd = NULL, *id_str = NULL;
8475 struct got_object_id *id = NULL;
8476 const char *logmsg = NULL;
8477 char *prepared_logmsg = NULL;
8478 struct collect_commit_logmsg_arg cl_arg;
8479 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8480 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8481 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8482 struct got_pathlist_head paths;
8483 int *pack_fds = NULL;
8485 TAILQ_INIT(&paths);
8486 cl_arg.logmsg_path = NULL;
8488 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8489 switch (ch) {
8490 case 'F':
8491 if (logmsg != NULL)
8492 option_conflict('F', 'm');
8493 prepared_logmsg = realpath(optarg, NULL);
8494 if (prepared_logmsg == NULL)
8495 return got_error_from_errno2("realpath",
8496 optarg);
8497 break;
8498 case 'm':
8499 if (prepared_logmsg)
8500 option_conflict('m', 'F');
8501 logmsg = optarg;
8502 break;
8503 case 'N':
8504 non_interactive = 1;
8505 break;
8506 case 'S':
8507 allow_bad_symlinks = 1;
8508 break;
8509 default:
8510 usage_commit();
8511 /* NOTREACHED */
8515 argc -= optind;
8516 argv += optind;
8518 #ifndef PROFILE
8519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8520 "unveil", NULL) == -1)
8521 err(1, "pledge");
8522 #endif
8523 cwd = getcwd(NULL, 0);
8524 if (cwd == NULL) {
8525 error = got_error_from_errno("getcwd");
8526 goto done;
8529 error = got_repo_pack_fds_open(&pack_fds);
8530 if (error != NULL)
8531 goto done;
8533 error = got_worktree_open(&worktree, cwd);
8534 if (error) {
8535 if (error->code == GOT_ERR_NOT_WORKTREE)
8536 error = wrap_not_worktree_error(error, "commit", cwd);
8537 goto done;
8540 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8541 if (error)
8542 goto done;
8543 if (rebase_in_progress) {
8544 error = got_error(GOT_ERR_REBASING);
8545 goto done;
8548 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8549 worktree);
8550 if (error)
8551 goto done;
8553 error = get_gitconfig_path(&gitconfig_path);
8554 if (error)
8555 goto done;
8556 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8557 gitconfig_path, pack_fds);
8558 if (error != NULL)
8559 goto done;
8561 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8562 if (error)
8563 goto done;
8564 if (merge_in_progress) {
8565 error = got_error(GOT_ERR_MERGE_BUSY);
8566 goto done;
8569 error = get_author(&author, repo, worktree);
8570 if (error)
8571 return error;
8574 * unveil(2) traverses exec(2); if an editor is used we have
8575 * to apply unveil after the log message has been written.
8577 if (logmsg == NULL || strlen(logmsg) == 0)
8578 error = get_editor(&editor);
8579 else
8580 error = apply_unveil(got_repo_get_path(repo), 0,
8581 got_worktree_get_root_path(worktree));
8582 if (error)
8583 goto done;
8585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8586 if (error)
8587 goto done;
8589 cl_arg.editor = editor;
8590 cl_arg.cmdline_log = logmsg;
8591 cl_arg.prepared_log = prepared_logmsg;
8592 cl_arg.non_interactive = non_interactive;
8593 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8594 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8595 if (!histedit_in_progress) {
8596 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8597 error = got_error(GOT_ERR_COMMIT_BRANCH);
8598 goto done;
8600 cl_arg.branch_name += 11;
8602 cl_arg.repo_path = got_repo_get_path(repo);
8603 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8604 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8605 print_status, NULL, repo);
8606 if (error) {
8607 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8608 cl_arg.logmsg_path != NULL)
8609 preserve_logmsg = 1;
8610 goto done;
8613 error = got_object_id_str(&id_str, id);
8614 if (error)
8615 goto done;
8616 printf("Created commit %s\n", id_str);
8617 done:
8618 if (preserve_logmsg) {
8619 fprintf(stderr, "%s: log message preserved in %s\n",
8620 getprogname(), cl_arg.logmsg_path);
8621 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8622 error == NULL)
8623 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8624 free(cl_arg.logmsg_path);
8625 if (repo) {
8626 const struct got_error *close_err = got_repo_close(repo);
8627 if (error == NULL)
8628 error = close_err;
8630 if (worktree)
8631 got_worktree_close(worktree);
8632 if (pack_fds) {
8633 const struct got_error *pack_err =
8634 got_repo_pack_fds_close(pack_fds);
8635 if (error == NULL)
8636 error = pack_err;
8638 free(cwd);
8639 free(id_str);
8640 free(gitconfig_path);
8641 free(editor);
8642 free(author);
8643 free(prepared_logmsg);
8644 return error;
8647 __dead static void
8648 usage_send(void)
8650 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8651 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8652 "[remote-repository]\n", getprogname());
8653 exit(1);
8656 static void
8657 print_load_info(int print_colored, int print_found, int print_trees,
8658 int ncolored, int nfound, int ntrees)
8660 if (print_colored) {
8661 printf("%d commit%s colored", ncolored,
8662 ncolored == 1 ? "" : "s");
8664 if (print_found) {
8665 printf("%s%d object%s found",
8666 ncolored > 0 ? "; " : "",
8667 nfound, nfound == 1 ? "" : "s");
8669 if (print_trees) {
8670 printf("; %d tree%s scanned", ntrees,
8671 ntrees == 1 ? "" : "s");
8675 struct got_send_progress_arg {
8676 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8677 int verbosity;
8678 int last_ncolored;
8679 int last_nfound;
8680 int last_ntrees;
8681 int loading_done;
8682 int last_ncommits;
8683 int last_nobj_total;
8684 int last_p_deltify;
8685 int last_p_written;
8686 int last_p_sent;
8687 int printed_something;
8688 int sent_something;
8689 struct got_pathlist_head *delete_branches;
8692 static const struct got_error *
8693 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8694 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8695 int nobj_written, off_t bytes_sent, const char *refname, int success)
8697 struct got_send_progress_arg *a = arg;
8698 char scaled_packsize[FMT_SCALED_STRSIZE];
8699 char scaled_sent[FMT_SCALED_STRSIZE];
8700 int p_deltify = 0, p_written = 0, p_sent = 0;
8701 int print_colored = 0, print_found = 0, print_trees = 0;
8702 int print_searching = 0, print_total = 0;
8703 int print_deltify = 0, print_written = 0, print_sent = 0;
8705 if (a->verbosity < 0)
8706 return NULL;
8708 if (refname) {
8709 const char *status = success ? "accepted" : "rejected";
8711 if (success) {
8712 struct got_pathlist_entry *pe;
8713 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8714 const char *branchname = pe->path;
8715 if (got_path_cmp(branchname, refname,
8716 strlen(branchname), strlen(refname)) == 0) {
8717 status = "deleted";
8718 a->sent_something = 1;
8719 break;
8724 if (a->printed_something)
8725 putchar('\n');
8726 printf("Server has %s %s", status, refname);
8727 a->printed_something = 1;
8728 return NULL;
8731 if (a->last_ncolored != ncolored) {
8732 print_colored = 1;
8733 a->last_ncolored = ncolored;
8736 if (a->last_nfound != nfound) {
8737 print_colored = 1;
8738 print_found = 1;
8739 a->last_nfound = nfound;
8742 if (a->last_ntrees != ntrees) {
8743 print_colored = 1;
8744 print_found = 1;
8745 print_trees = 1;
8746 a->last_ntrees = ntrees;
8749 if ((print_colored || print_found || print_trees) &&
8750 !a->loading_done) {
8751 printf("\r");
8752 print_load_info(print_colored, print_found, print_trees,
8753 ncolored, nfound, ntrees);
8754 a->printed_something = 1;
8755 fflush(stdout);
8756 return NULL;
8757 } else if (!a->loading_done) {
8758 printf("\r");
8759 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8760 printf("\n");
8761 a->loading_done = 1;
8764 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8765 return got_error_from_errno("fmt_scaled");
8766 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8767 return got_error_from_errno("fmt_scaled");
8769 if (a->last_ncommits != ncommits) {
8770 print_searching = 1;
8771 a->last_ncommits = ncommits;
8774 if (a->last_nobj_total != nobj_total) {
8775 print_searching = 1;
8776 print_total = 1;
8777 a->last_nobj_total = nobj_total;
8780 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8781 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8782 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8783 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8784 return got_error(GOT_ERR_NO_SPACE);
8787 if (nobj_deltify > 0 || nobj_written > 0) {
8788 if (nobj_deltify > 0) {
8789 p_deltify = (nobj_deltify * 100) / nobj_total;
8790 if (p_deltify != a->last_p_deltify) {
8791 a->last_p_deltify = p_deltify;
8792 print_searching = 1;
8793 print_total = 1;
8794 print_deltify = 1;
8797 if (nobj_written > 0) {
8798 p_written = (nobj_written * 100) / nobj_total;
8799 if (p_written != a->last_p_written) {
8800 a->last_p_written = p_written;
8801 print_searching = 1;
8802 print_total = 1;
8803 print_deltify = 1;
8804 print_written = 1;
8809 if (bytes_sent > 0) {
8810 p_sent = (bytes_sent * 100) / packfile_size;
8811 if (p_sent != a->last_p_sent) {
8812 a->last_p_sent = p_sent;
8813 print_searching = 1;
8814 print_total = 1;
8815 print_deltify = 1;
8816 print_written = 1;
8817 print_sent = 1;
8819 a->sent_something = 1;
8822 if (print_searching || print_total || print_deltify || print_written ||
8823 print_sent)
8824 printf("\r");
8825 if (print_searching)
8826 printf("packing %d reference%s", ncommits,
8827 ncommits == 1 ? "" : "s");
8828 if (print_total)
8829 printf("; %d object%s", nobj_total,
8830 nobj_total == 1 ? "" : "s");
8831 if (print_deltify)
8832 printf("; deltify: %d%%", p_deltify);
8833 if (print_sent)
8834 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8835 scaled_packsize, p_sent);
8836 else if (print_written)
8837 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8838 scaled_packsize, p_written);
8839 if (print_searching || print_total || print_deltify ||
8840 print_written || print_sent) {
8841 a->printed_something = 1;
8842 fflush(stdout);
8844 return NULL;
8847 static const struct got_error *
8848 cmd_send(int argc, char *argv[])
8850 const struct got_error *error = NULL;
8851 char *cwd = NULL, *repo_path = NULL;
8852 const char *remote_name;
8853 char *proto = NULL, *host = NULL, *port = NULL;
8854 char *repo_name = NULL, *server_path = NULL;
8855 const struct got_remote_repo *remotes, *remote = NULL;
8856 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8857 struct got_repository *repo = NULL;
8858 struct got_worktree *worktree = NULL;
8859 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8860 struct got_pathlist_head branches;
8861 struct got_pathlist_head tags;
8862 struct got_reflist_head all_branches;
8863 struct got_reflist_head all_tags;
8864 struct got_pathlist_head delete_args;
8865 struct got_pathlist_head delete_branches;
8866 struct got_reflist_entry *re;
8867 struct got_pathlist_entry *pe;
8868 int i, ch, sendfd = -1, sendstatus;
8869 pid_t sendpid = -1;
8870 struct got_send_progress_arg spa;
8871 int verbosity = 0, overwrite_refs = 0;
8872 int send_all_branches = 0, send_all_tags = 0;
8873 struct got_reference *ref = NULL;
8874 int *pack_fds = NULL;
8876 TAILQ_INIT(&branches);
8877 TAILQ_INIT(&tags);
8878 TAILQ_INIT(&all_branches);
8879 TAILQ_INIT(&all_tags);
8880 TAILQ_INIT(&delete_args);
8881 TAILQ_INIT(&delete_branches);
8883 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8884 switch (ch) {
8885 case 'a':
8886 send_all_branches = 1;
8887 break;
8888 case 'b':
8889 error = got_pathlist_append(&branches, optarg, NULL);
8890 if (error)
8891 return error;
8892 nbranches++;
8893 break;
8894 case 'd':
8895 error = got_pathlist_append(&delete_args, optarg, NULL);
8896 if (error)
8897 return error;
8898 break;
8899 case 'f':
8900 overwrite_refs = 1;
8901 break;
8902 case 'r':
8903 repo_path = realpath(optarg, NULL);
8904 if (repo_path == NULL)
8905 return got_error_from_errno2("realpath",
8906 optarg);
8907 got_path_strip_trailing_slashes(repo_path);
8908 break;
8909 case 't':
8910 error = got_pathlist_append(&tags, optarg, NULL);
8911 if (error)
8912 return error;
8913 ntags++;
8914 break;
8915 case 'T':
8916 send_all_tags = 1;
8917 break;
8918 case 'v':
8919 if (verbosity < 0)
8920 verbosity = 0;
8921 else if (verbosity < 3)
8922 verbosity++;
8923 break;
8924 case 'q':
8925 verbosity = -1;
8926 break;
8927 default:
8928 usage_send();
8929 /* NOTREACHED */
8932 argc -= optind;
8933 argv += optind;
8935 if (send_all_branches && !TAILQ_EMPTY(&branches))
8936 option_conflict('a', 'b');
8937 if (send_all_tags && !TAILQ_EMPTY(&tags))
8938 option_conflict('T', 't');
8941 if (argc == 0)
8942 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8943 else if (argc == 1)
8944 remote_name = argv[0];
8945 else
8946 usage_send();
8948 cwd = getcwd(NULL, 0);
8949 if (cwd == NULL) {
8950 error = got_error_from_errno("getcwd");
8951 goto done;
8954 error = got_repo_pack_fds_open(&pack_fds);
8955 if (error != NULL)
8956 goto done;
8958 if (repo_path == NULL) {
8959 error = got_worktree_open(&worktree, cwd);
8960 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8961 goto done;
8962 else
8963 error = NULL;
8964 if (worktree) {
8965 repo_path =
8966 strdup(got_worktree_get_repo_path(worktree));
8967 if (repo_path == NULL)
8968 error = got_error_from_errno("strdup");
8969 if (error)
8970 goto done;
8971 } else {
8972 repo_path = strdup(cwd);
8973 if (repo_path == NULL) {
8974 error = got_error_from_errno("strdup");
8975 goto done;
8980 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8981 if (error)
8982 goto done;
8984 if (worktree) {
8985 worktree_conf = got_worktree_get_gotconfig(worktree);
8986 if (worktree_conf) {
8987 got_gotconfig_get_remotes(&nremotes, &remotes,
8988 worktree_conf);
8989 for (i = 0; i < nremotes; i++) {
8990 if (strcmp(remotes[i].name, remote_name) == 0) {
8991 remote = &remotes[i];
8992 break;
8997 if (remote == NULL) {
8998 repo_conf = got_repo_get_gotconfig(repo);
8999 if (repo_conf) {
9000 got_gotconfig_get_remotes(&nremotes, &remotes,
9001 repo_conf);
9002 for (i = 0; i < nremotes; i++) {
9003 if (strcmp(remotes[i].name, remote_name) == 0) {
9004 remote = &remotes[i];
9005 break;
9010 if (remote == NULL) {
9011 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9012 for (i = 0; i < nremotes; i++) {
9013 if (strcmp(remotes[i].name, remote_name) == 0) {
9014 remote = &remotes[i];
9015 break;
9019 if (remote == NULL) {
9020 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9021 goto done;
9024 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9025 &repo_name, remote->send_url);
9026 if (error)
9027 goto done;
9029 if (strcmp(proto, "git") == 0) {
9030 #ifndef PROFILE
9031 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9032 "sendfd dns inet unveil", NULL) == -1)
9033 err(1, "pledge");
9034 #endif
9035 } else if (strcmp(proto, "git+ssh") == 0 ||
9036 strcmp(proto, "ssh") == 0) {
9037 #ifndef PROFILE
9038 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9039 "sendfd unveil", NULL) == -1)
9040 err(1, "pledge");
9041 #endif
9042 } else if (strcmp(proto, "http") == 0 ||
9043 strcmp(proto, "git+http") == 0) {
9044 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9045 goto done;
9046 } else {
9047 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9048 goto done;
9051 error = got_dial_apply_unveil(proto);
9052 if (error)
9053 goto done;
9055 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9056 if (error)
9057 goto done;
9059 if (send_all_branches) {
9060 error = got_ref_list(&all_branches, repo, "refs/heads",
9061 got_ref_cmp_by_name, NULL);
9062 if (error)
9063 goto done;
9064 TAILQ_FOREACH(re, &all_branches, entry) {
9065 const char *branchname = got_ref_get_name(re->ref);
9066 error = got_pathlist_append(&branches,
9067 branchname, NULL);
9068 if (error)
9069 goto done;
9070 nbranches++;
9072 } else if (nbranches == 0) {
9073 for (i = 0; i < remote->nsend_branches; i++) {
9074 got_pathlist_append(&branches,
9075 remote->send_branches[i], NULL);
9079 if (send_all_tags) {
9080 error = got_ref_list(&all_tags, repo, "refs/tags",
9081 got_ref_cmp_by_name, NULL);
9082 if (error)
9083 goto done;
9084 TAILQ_FOREACH(re, &all_tags, entry) {
9085 const char *tagname = got_ref_get_name(re->ref);
9086 error = got_pathlist_append(&tags,
9087 tagname, NULL);
9088 if (error)
9089 goto done;
9090 ntags++;
9095 * To prevent accidents only branches in refs/heads/ can be deleted
9096 * with 'got send -d'.
9097 * Deleting anything else requires local repository access or Git.
9099 TAILQ_FOREACH(pe, &delete_args, entry) {
9100 const char *branchname = pe->path;
9101 char *s;
9102 struct got_pathlist_entry *new;
9103 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9104 s = strdup(branchname);
9105 if (s == NULL) {
9106 error = got_error_from_errno("strdup");
9107 goto done;
9109 } else {
9110 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9111 error = got_error_from_errno("asprintf");
9112 goto done;
9115 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9116 if (error || new == NULL /* duplicate */)
9117 free(s);
9118 if (error)
9119 goto done;
9120 ndelete_branches++;
9123 if (nbranches == 0 && ndelete_branches == 0) {
9124 struct got_reference *head_ref;
9125 if (worktree)
9126 error = got_ref_open(&head_ref, repo,
9127 got_worktree_get_head_ref_name(worktree), 0);
9128 else
9129 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9130 if (error)
9131 goto done;
9132 if (got_ref_is_symbolic(head_ref)) {
9133 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9134 got_ref_close(head_ref);
9135 if (error)
9136 goto done;
9137 } else
9138 ref = head_ref;
9139 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9140 NULL);
9141 if (error)
9142 goto done;
9143 nbranches++;
9146 if (verbosity >= 0)
9147 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9148 port ? ":" : "", port ? port : "");
9150 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9151 server_path, verbosity);
9152 if (error)
9153 goto done;
9155 memset(&spa, 0, sizeof(spa));
9156 spa.last_scaled_packsize[0] = '\0';
9157 spa.last_p_deltify = -1;
9158 spa.last_p_written = -1;
9159 spa.verbosity = verbosity;
9160 spa.delete_branches = &delete_branches;
9161 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9162 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9163 check_cancelled, NULL);
9164 if (spa.printed_something)
9165 putchar('\n');
9166 if (error)
9167 goto done;
9168 if (!spa.sent_something && verbosity >= 0)
9169 printf("Already up-to-date\n");
9170 done:
9171 if (sendpid > 0) {
9172 if (kill(sendpid, SIGTERM) == -1)
9173 error = got_error_from_errno("kill");
9174 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9175 error = got_error_from_errno("waitpid");
9177 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9178 error = got_error_from_errno("close");
9179 if (repo) {
9180 const struct got_error *close_err = got_repo_close(repo);
9181 if (error == NULL)
9182 error = close_err;
9184 if (worktree)
9185 got_worktree_close(worktree);
9186 if (pack_fds) {
9187 const struct got_error *pack_err =
9188 got_repo_pack_fds_close(pack_fds);
9189 if (error == NULL)
9190 error = pack_err;
9192 if (ref)
9193 got_ref_close(ref);
9194 got_pathlist_free(&branches);
9195 got_pathlist_free(&tags);
9196 got_ref_list_free(&all_branches);
9197 got_ref_list_free(&all_tags);
9198 got_pathlist_free(&delete_args);
9199 TAILQ_FOREACH(pe, &delete_branches, entry)
9200 free((char *)pe->path);
9201 got_pathlist_free(&delete_branches);
9202 free(cwd);
9203 free(repo_path);
9204 free(proto);
9205 free(host);
9206 free(port);
9207 free(server_path);
9208 free(repo_name);
9209 return error;
9212 __dead static void
9213 usage_cherrypick(void)
9215 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9216 exit(1);
9219 static const struct got_error *
9220 cmd_cherrypick(int argc, char *argv[])
9222 const struct got_error *error = NULL;
9223 struct got_worktree *worktree = NULL;
9224 struct got_repository *repo = NULL;
9225 char *cwd = NULL, *commit_id_str = NULL;
9226 struct got_object_id *commit_id = NULL;
9227 struct got_commit_object *commit = NULL;
9228 struct got_object_qid *pid;
9229 int ch;
9230 struct got_update_progress_arg upa;
9231 int *pack_fds = NULL;
9233 while ((ch = getopt(argc, argv, "")) != -1) {
9234 switch (ch) {
9235 default:
9236 usage_cherrypick();
9237 /* NOTREACHED */
9241 argc -= optind;
9242 argv += optind;
9244 #ifndef PROFILE
9245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9246 "unveil", NULL) == -1)
9247 err(1, "pledge");
9248 #endif
9249 if (argc != 1)
9250 usage_cherrypick();
9252 cwd = getcwd(NULL, 0);
9253 if (cwd == NULL) {
9254 error = got_error_from_errno("getcwd");
9255 goto done;
9258 error = got_repo_pack_fds_open(&pack_fds);
9259 if (error != NULL)
9260 goto done;
9262 error = got_worktree_open(&worktree, cwd);
9263 if (error) {
9264 if (error->code == GOT_ERR_NOT_WORKTREE)
9265 error = wrap_not_worktree_error(error, "cherrypick",
9266 cwd);
9267 goto done;
9270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9271 NULL, pack_fds);
9272 if (error != NULL)
9273 goto done;
9275 error = apply_unveil(got_repo_get_path(repo), 0,
9276 got_worktree_get_root_path(worktree));
9277 if (error)
9278 goto done;
9280 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9281 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9282 if (error)
9283 goto done;
9284 error = got_object_id_str(&commit_id_str, commit_id);
9285 if (error)
9286 goto done;
9288 error = got_object_open_as_commit(&commit, repo, commit_id);
9289 if (error)
9290 goto done;
9291 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9292 memset(&upa, 0, sizeof(upa));
9293 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9294 commit_id, repo, update_progress, &upa, check_cancelled,
9295 NULL);
9296 if (error != NULL)
9297 goto done;
9299 if (upa.did_something)
9300 printf("Merged commit %s\n", commit_id_str);
9301 print_merge_progress_stats(&upa);
9302 done:
9303 if (commit)
9304 got_object_commit_close(commit);
9305 free(commit_id_str);
9306 if (worktree)
9307 got_worktree_close(worktree);
9308 if (repo) {
9309 const struct got_error *close_err = got_repo_close(repo);
9310 if (error == NULL)
9311 error = close_err;
9313 if (pack_fds) {
9314 const struct got_error *pack_err =
9315 got_repo_pack_fds_close(pack_fds);
9316 if (error == NULL)
9317 error = pack_err;
9320 return error;
9323 __dead static void
9324 usage_backout(void)
9326 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9327 exit(1);
9330 static const struct got_error *
9331 cmd_backout(int argc, char *argv[])
9333 const struct got_error *error = NULL;
9334 struct got_worktree *worktree = NULL;
9335 struct got_repository *repo = NULL;
9336 char *cwd = NULL, *commit_id_str = NULL;
9337 struct got_object_id *commit_id = NULL;
9338 struct got_commit_object *commit = NULL;
9339 struct got_object_qid *pid;
9340 int ch;
9341 struct got_update_progress_arg upa;
9342 int *pack_fds = NULL;
9344 while ((ch = getopt(argc, argv, "")) != -1) {
9345 switch (ch) {
9346 default:
9347 usage_backout();
9348 /* NOTREACHED */
9352 argc -= optind;
9353 argv += optind;
9355 #ifndef PROFILE
9356 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9357 "unveil", NULL) == -1)
9358 err(1, "pledge");
9359 #endif
9360 if (argc != 1)
9361 usage_backout();
9363 cwd = getcwd(NULL, 0);
9364 if (cwd == NULL) {
9365 error = got_error_from_errno("getcwd");
9366 goto done;
9369 error = got_repo_pack_fds_open(&pack_fds);
9370 if (error != NULL)
9371 goto done;
9373 error = got_worktree_open(&worktree, cwd);
9374 if (error) {
9375 if (error->code == GOT_ERR_NOT_WORKTREE)
9376 error = wrap_not_worktree_error(error, "backout", cwd);
9377 goto done;
9380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9381 NULL, pack_fds);
9382 if (error != NULL)
9383 goto done;
9385 error = apply_unveil(got_repo_get_path(repo), 0,
9386 got_worktree_get_root_path(worktree));
9387 if (error)
9388 goto done;
9390 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9391 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9392 if (error)
9393 goto done;
9394 error = got_object_id_str(&commit_id_str, commit_id);
9395 if (error)
9396 goto done;
9398 error = got_object_open_as_commit(&commit, repo, commit_id);
9399 if (error)
9400 goto done;
9401 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9402 if (pid == NULL) {
9403 error = got_error(GOT_ERR_ROOT_COMMIT);
9404 goto done;
9407 memset(&upa, 0, sizeof(upa));
9408 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9409 repo, update_progress, &upa, check_cancelled, NULL);
9410 if (error != NULL)
9411 goto done;
9413 if (upa.did_something)
9414 printf("Backed out commit %s\n", commit_id_str);
9415 print_merge_progress_stats(&upa);
9416 done:
9417 if (commit)
9418 got_object_commit_close(commit);
9419 free(commit_id_str);
9420 if (worktree)
9421 got_worktree_close(worktree);
9422 if (repo) {
9423 const struct got_error *close_err = got_repo_close(repo);
9424 if (error == NULL)
9425 error = close_err;
9427 if (pack_fds) {
9428 const struct got_error *pack_err =
9429 got_repo_pack_fds_close(pack_fds);
9430 if (error == NULL)
9431 error = pack_err;
9433 return error;
9436 __dead static void
9437 usage_rebase(void)
9439 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9440 getprogname());
9441 exit(1);
9444 static void
9445 trim_logmsg(char *logmsg, int limit)
9447 char *nl;
9448 size_t len;
9450 len = strlen(logmsg);
9451 if (len > limit)
9452 len = limit;
9453 logmsg[len] = '\0';
9454 nl = strchr(logmsg, '\n');
9455 if (nl)
9456 *nl = '\0';
9459 static const struct got_error *
9460 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9462 const struct got_error *err;
9463 char *logmsg0 = NULL;
9464 const char *s;
9466 err = got_object_commit_get_logmsg(&logmsg0, commit);
9467 if (err)
9468 return err;
9470 s = logmsg0;
9471 while (isspace((unsigned char)s[0]))
9472 s++;
9474 *logmsg = strdup(s);
9475 if (*logmsg == NULL) {
9476 err = got_error_from_errno("strdup");
9477 goto done;
9480 trim_logmsg(*logmsg, limit);
9481 done:
9482 free(logmsg0);
9483 return err;
9486 static const struct got_error *
9487 show_rebase_merge_conflict(struct got_object_id *id,
9488 struct got_repository *repo)
9490 const struct got_error *err;
9491 struct got_commit_object *commit = NULL;
9492 char *id_str = NULL, *logmsg = NULL;
9494 err = got_object_open_as_commit(&commit, repo, id);
9495 if (err)
9496 return err;
9498 err = got_object_id_str(&id_str, id);
9499 if (err)
9500 goto done;
9502 id_str[12] = '\0';
9504 err = get_short_logmsg(&logmsg, 42, commit);
9505 if (err)
9506 goto done;
9508 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9509 done:
9510 free(id_str);
9511 got_object_commit_close(commit);
9512 free(logmsg);
9513 return err;
9516 static const struct got_error *
9517 show_rebase_progress(struct got_commit_object *commit,
9518 struct got_object_id *old_id, struct got_object_id *new_id)
9520 const struct got_error *err;
9521 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9523 err = got_object_id_str(&old_id_str, old_id);
9524 if (err)
9525 goto done;
9527 if (new_id) {
9528 err = got_object_id_str(&new_id_str, new_id);
9529 if (err)
9530 goto done;
9533 old_id_str[12] = '\0';
9534 if (new_id_str)
9535 new_id_str[12] = '\0';
9537 err = get_short_logmsg(&logmsg, 42, commit);
9538 if (err)
9539 goto done;
9541 printf("%s -> %s: %s\n", old_id_str,
9542 new_id_str ? new_id_str : "no-op change", logmsg);
9543 done:
9544 free(old_id_str);
9545 free(new_id_str);
9546 free(logmsg);
9547 return err;
9550 static const struct got_error *
9551 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9552 struct got_reference *branch, struct got_reference *new_base_branch,
9553 struct got_reference *tmp_branch, struct got_repository *repo,
9554 int create_backup)
9556 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9557 return got_worktree_rebase_complete(worktree, fileindex,
9558 new_base_branch, tmp_branch, branch, repo, create_backup);
9561 static const struct got_error *
9562 rebase_commit(struct got_pathlist_head *merged_paths,
9563 struct got_worktree *worktree, struct got_fileindex *fileindex,
9564 struct got_reference *tmp_branch,
9565 struct got_object_id *commit_id, struct got_repository *repo)
9567 const struct got_error *error;
9568 struct got_commit_object *commit;
9569 struct got_object_id *new_commit_id;
9571 error = got_object_open_as_commit(&commit, repo, commit_id);
9572 if (error)
9573 return error;
9575 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9576 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9577 if (error) {
9578 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9579 goto done;
9580 error = show_rebase_progress(commit, commit_id, NULL);
9581 } else {
9582 error = show_rebase_progress(commit, commit_id, new_commit_id);
9583 free(new_commit_id);
9585 done:
9586 got_object_commit_close(commit);
9587 return error;
9590 struct check_path_prefix_arg {
9591 const char *path_prefix;
9592 size_t len;
9593 int errcode;
9596 static const struct got_error *
9597 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9598 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9599 struct got_object_id *id1, struct got_object_id *id2,
9600 const char *path1, const char *path2,
9601 mode_t mode1, mode_t mode2, struct got_repository *repo)
9603 struct check_path_prefix_arg *a = arg;
9605 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9606 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9607 return got_error(a->errcode);
9609 return NULL;
9612 static const struct got_error *
9613 check_path_prefix(struct got_object_id *parent_id,
9614 struct got_object_id *commit_id, const char *path_prefix,
9615 int errcode, struct got_repository *repo)
9617 const struct got_error *err;
9618 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9619 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9620 struct check_path_prefix_arg cpp_arg;
9622 if (got_path_is_root_dir(path_prefix))
9623 return NULL;
9625 err = got_object_open_as_commit(&commit, repo, commit_id);
9626 if (err)
9627 goto done;
9629 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9630 if (err)
9631 goto done;
9633 err = got_object_open_as_tree(&tree1, repo,
9634 got_object_commit_get_tree_id(parent_commit));
9635 if (err)
9636 goto done;
9638 err = got_object_open_as_tree(&tree2, repo,
9639 got_object_commit_get_tree_id(commit));
9640 if (err)
9641 goto done;
9643 cpp_arg.path_prefix = path_prefix;
9644 while (cpp_arg.path_prefix[0] == '/')
9645 cpp_arg.path_prefix++;
9646 cpp_arg.len = strlen(cpp_arg.path_prefix);
9647 cpp_arg.errcode = errcode;
9648 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9649 check_path_prefix_in_diff, &cpp_arg, 0);
9650 done:
9651 if (tree1)
9652 got_object_tree_close(tree1);
9653 if (tree2)
9654 got_object_tree_close(tree2);
9655 if (commit)
9656 got_object_commit_close(commit);
9657 if (parent_commit)
9658 got_object_commit_close(parent_commit);
9659 return err;
9662 static const struct got_error *
9663 collect_commits(struct got_object_id_queue *commits,
9664 struct got_object_id *initial_commit_id,
9665 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9666 const char *path_prefix, int path_prefix_errcode,
9667 struct got_repository *repo)
9669 const struct got_error *err = NULL;
9670 struct got_commit_graph *graph = NULL;
9671 struct got_object_id *parent_id = NULL;
9672 struct got_object_qid *qid;
9673 struct got_object_id *commit_id = initial_commit_id;
9675 err = got_commit_graph_open(&graph, "/", 1);
9676 if (err)
9677 return err;
9679 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9680 check_cancelled, NULL);
9681 if (err)
9682 goto done;
9683 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9684 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9685 check_cancelled, NULL);
9686 if (err) {
9687 if (err->code == GOT_ERR_ITER_COMPLETED) {
9688 err = got_error_msg(GOT_ERR_ANCESTRY,
9689 "ran out of commits to rebase before "
9690 "youngest common ancestor commit has "
9691 "been reached?!?");
9693 goto done;
9694 } else {
9695 err = check_path_prefix(parent_id, commit_id,
9696 path_prefix, path_prefix_errcode, repo);
9697 if (err)
9698 goto done;
9700 err = got_object_qid_alloc(&qid, commit_id);
9701 if (err)
9702 goto done;
9703 STAILQ_INSERT_HEAD(commits, qid, entry);
9704 commit_id = parent_id;
9707 done:
9708 got_commit_graph_close(graph);
9709 return err;
9712 static const struct got_error *
9713 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9715 const struct got_error *err = NULL;
9716 time_t committer_time;
9717 struct tm tm;
9718 char datebuf[11]; /* YYYY-MM-DD + NUL */
9719 char *author0 = NULL, *author, *smallerthan;
9720 char *logmsg0 = NULL, *logmsg, *newline;
9722 committer_time = got_object_commit_get_committer_time(commit);
9723 if (gmtime_r(&committer_time, &tm) == NULL)
9724 return got_error_from_errno("gmtime_r");
9725 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9726 return got_error(GOT_ERR_NO_SPACE);
9728 author0 = strdup(got_object_commit_get_author(commit));
9729 if (author0 == NULL)
9730 return got_error_from_errno("strdup");
9731 author = author0;
9732 smallerthan = strchr(author, '<');
9733 if (smallerthan && smallerthan[1] != '\0')
9734 author = smallerthan + 1;
9735 author[strcspn(author, "@>")] = '\0';
9737 err = got_object_commit_get_logmsg(&logmsg0, commit);
9738 if (err)
9739 goto done;
9740 logmsg = logmsg0;
9741 while (*logmsg == '\n')
9742 logmsg++;
9743 newline = strchr(logmsg, '\n');
9744 if (newline)
9745 *newline = '\0';
9747 if (asprintf(brief_str, "%s %s %s",
9748 datebuf, author, logmsg) == -1)
9749 err = got_error_from_errno("asprintf");
9750 done:
9751 free(author0);
9752 free(logmsg0);
9753 return err;
9756 static const struct got_error *
9757 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9758 struct got_repository *repo)
9760 const struct got_error *err;
9761 char *id_str;
9763 err = got_object_id_str(&id_str, id);
9764 if (err)
9765 return err;
9767 err = got_ref_delete(ref, repo);
9768 if (err)
9769 goto done;
9771 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9772 done:
9773 free(id_str);
9774 return err;
9777 static const struct got_error *
9778 print_backup_ref(const char *branch_name, const char *new_id_str,
9779 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9780 struct got_reflist_object_id_map *refs_idmap,
9781 struct got_repository *repo)
9783 const struct got_error *err = NULL;
9784 struct got_reflist_head *refs;
9785 char *refs_str = NULL;
9786 struct got_object_id *new_commit_id = NULL;
9787 struct got_commit_object *new_commit = NULL;
9788 char *new_commit_brief_str = NULL;
9789 struct got_object_id *yca_id = NULL;
9790 struct got_commit_object *yca_commit = NULL;
9791 char *yca_id_str = NULL, *yca_brief_str = NULL;
9792 char *custom_refs_str;
9794 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9795 return got_error_from_errno("asprintf");
9797 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9798 0, 0, refs_idmap, custom_refs_str);
9799 if (err)
9800 goto done;
9802 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9803 if (err)
9804 goto done;
9806 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9807 if (refs) {
9808 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9809 if (err)
9810 goto done;
9813 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9814 if (err)
9815 goto done;
9817 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9818 if (err)
9819 goto done;
9821 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9822 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9823 if (err)
9824 goto done;
9826 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9827 refs_str ? " (" : "", refs_str ? refs_str : "",
9828 refs_str ? ")" : "", new_commit_brief_str);
9829 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9830 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9831 free(refs_str);
9832 refs_str = NULL;
9834 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9835 if (err)
9836 goto done;
9838 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9839 if (err)
9840 goto done;
9842 err = got_object_id_str(&yca_id_str, yca_id);
9843 if (err)
9844 goto done;
9846 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9847 if (refs) {
9848 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9849 if (err)
9850 goto done;
9852 printf("history forked at %s%s%s%s\n %s\n",
9853 yca_id_str,
9854 refs_str ? " (" : "", refs_str ? refs_str : "",
9855 refs_str ? ")" : "", yca_brief_str);
9857 done:
9858 free(custom_refs_str);
9859 free(new_commit_id);
9860 free(refs_str);
9861 free(yca_id);
9862 free(yca_id_str);
9863 free(yca_brief_str);
9864 if (new_commit)
9865 got_object_commit_close(new_commit);
9866 if (yca_commit)
9867 got_object_commit_close(yca_commit);
9869 return NULL;
9872 static const struct got_error *
9873 process_backup_refs(const char *backup_ref_prefix,
9874 const char *wanted_branch_name,
9875 int delete, struct got_repository *repo)
9877 const struct got_error *err;
9878 struct got_reflist_head refs, backup_refs;
9879 struct got_reflist_entry *re;
9880 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9881 struct got_object_id *old_commit_id = NULL;
9882 char *branch_name = NULL;
9883 struct got_commit_object *old_commit = NULL;
9884 struct got_reflist_object_id_map *refs_idmap = NULL;
9885 int wanted_branch_found = 0;
9887 TAILQ_INIT(&refs);
9888 TAILQ_INIT(&backup_refs);
9890 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9891 if (err)
9892 return err;
9894 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9895 if (err)
9896 goto done;
9898 if (wanted_branch_name) {
9899 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9900 wanted_branch_name += 11;
9903 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9904 got_ref_cmp_by_commit_timestamp_descending, repo);
9905 if (err)
9906 goto done;
9908 TAILQ_FOREACH(re, &backup_refs, entry) {
9909 const char *refname = got_ref_get_name(re->ref);
9910 char *slash;
9912 err = check_cancelled(NULL);
9913 if (err)
9914 break;
9916 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9917 if (err)
9918 break;
9920 err = got_object_open_as_commit(&old_commit, repo,
9921 old_commit_id);
9922 if (err)
9923 break;
9925 if (strncmp(backup_ref_prefix, refname,
9926 backup_ref_prefix_len) == 0)
9927 refname += backup_ref_prefix_len;
9929 while (refname[0] == '/')
9930 refname++;
9932 branch_name = strdup(refname);
9933 if (branch_name == NULL) {
9934 err = got_error_from_errno("strdup");
9935 break;
9937 slash = strrchr(branch_name, '/');
9938 if (slash) {
9939 *slash = '\0';
9940 refname += strlen(branch_name) + 1;
9943 if (wanted_branch_name == NULL ||
9944 strcmp(wanted_branch_name, branch_name) == 0) {
9945 wanted_branch_found = 1;
9946 if (delete) {
9947 err = delete_backup_ref(re->ref,
9948 old_commit_id, repo);
9949 } else {
9950 err = print_backup_ref(branch_name, refname,
9951 old_commit_id, old_commit, refs_idmap,
9952 repo);
9954 if (err)
9955 break;
9958 free(old_commit_id);
9959 old_commit_id = NULL;
9960 free(branch_name);
9961 branch_name = NULL;
9962 got_object_commit_close(old_commit);
9963 old_commit = NULL;
9966 if (wanted_branch_name && !wanted_branch_found) {
9967 err = got_error_fmt(GOT_ERR_NOT_REF,
9968 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9970 done:
9971 if (refs_idmap)
9972 got_reflist_object_id_map_free(refs_idmap);
9973 got_ref_list_free(&refs);
9974 got_ref_list_free(&backup_refs);
9975 free(old_commit_id);
9976 free(branch_name);
9977 if (old_commit)
9978 got_object_commit_close(old_commit);
9979 return err;
9982 static const struct got_error *
9983 abort_progress(void *arg, unsigned char status, const char *path)
9986 * Unversioned files should not clutter progress output when
9987 * an operation is aborted.
9989 if (status == GOT_STATUS_UNVERSIONED)
9990 return NULL;
9992 return update_progress(arg, status, path);
9995 static const struct got_error *
9996 cmd_rebase(int argc, char *argv[])
9998 const struct got_error *error = NULL;
9999 struct got_worktree *worktree = NULL;
10000 struct got_repository *repo = NULL;
10001 struct got_fileindex *fileindex = NULL;
10002 char *cwd = NULL;
10003 struct got_reference *branch = NULL;
10004 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10005 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10006 struct got_object_id *resume_commit_id = NULL;
10007 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10008 struct got_commit_object *commit = NULL;
10009 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10010 int histedit_in_progress = 0, merge_in_progress = 0;
10011 int create_backup = 1, list_backups = 0, delete_backups = 0;
10012 struct got_object_id_queue commits;
10013 struct got_pathlist_head merged_paths;
10014 const struct got_object_id_queue *parent_ids;
10015 struct got_object_qid *qid, *pid;
10016 struct got_update_progress_arg upa;
10017 int *pack_fds = NULL;
10019 STAILQ_INIT(&commits);
10020 TAILQ_INIT(&merged_paths);
10021 memset(&upa, 0, sizeof(upa));
10023 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10024 switch (ch) {
10025 case 'a':
10026 abort_rebase = 1;
10027 break;
10028 case 'c':
10029 continue_rebase = 1;
10030 break;
10031 case 'l':
10032 list_backups = 1;
10033 break;
10034 case 'X':
10035 delete_backups = 1;
10036 break;
10037 default:
10038 usage_rebase();
10039 /* NOTREACHED */
10043 argc -= optind;
10044 argv += optind;
10046 #ifndef PROFILE
10047 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10048 "unveil", NULL) == -1)
10049 err(1, "pledge");
10050 #endif
10051 if (list_backups) {
10052 if (abort_rebase)
10053 option_conflict('l', 'a');
10054 if (continue_rebase)
10055 option_conflict('l', 'c');
10056 if (delete_backups)
10057 option_conflict('l', 'X');
10058 if (argc != 0 && argc != 1)
10059 usage_rebase();
10060 } else if (delete_backups) {
10061 if (abort_rebase)
10062 option_conflict('X', 'a');
10063 if (continue_rebase)
10064 option_conflict('X', 'c');
10065 if (list_backups)
10066 option_conflict('l', 'X');
10067 if (argc != 0 && argc != 1)
10068 usage_rebase();
10069 } else {
10070 if (abort_rebase && continue_rebase)
10071 usage_rebase();
10072 else if (abort_rebase || continue_rebase) {
10073 if (argc != 0)
10074 usage_rebase();
10075 } else if (argc != 1)
10076 usage_rebase();
10079 cwd = getcwd(NULL, 0);
10080 if (cwd == NULL) {
10081 error = got_error_from_errno("getcwd");
10082 goto done;
10085 error = got_repo_pack_fds_open(&pack_fds);
10086 if (error != NULL)
10087 goto done;
10089 error = got_worktree_open(&worktree, cwd);
10090 if (error) {
10091 if (list_backups || delete_backups) {
10092 if (error->code != GOT_ERR_NOT_WORKTREE)
10093 goto done;
10094 } else {
10095 if (error->code == GOT_ERR_NOT_WORKTREE)
10096 error = wrap_not_worktree_error(error,
10097 "rebase", cwd);
10098 goto done;
10102 error = got_repo_open(&repo,
10103 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10104 pack_fds);
10105 if (error != NULL)
10106 goto done;
10108 error = apply_unveil(got_repo_get_path(repo), 0,
10109 worktree ? got_worktree_get_root_path(worktree) : NULL);
10110 if (error)
10111 goto done;
10113 if (list_backups || delete_backups) {
10114 error = process_backup_refs(
10115 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10116 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10117 goto done; /* nothing else to do */
10120 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10121 worktree);
10122 if (error)
10123 goto done;
10124 if (histedit_in_progress) {
10125 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10126 goto done;
10129 error = got_worktree_merge_in_progress(&merge_in_progress,
10130 worktree, repo);
10131 if (error)
10132 goto done;
10133 if (merge_in_progress) {
10134 error = got_error(GOT_ERR_MERGE_BUSY);
10135 goto done;
10138 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10139 if (error)
10140 goto done;
10142 if (abort_rebase) {
10143 if (!rebase_in_progress) {
10144 error = got_error(GOT_ERR_NOT_REBASING);
10145 goto done;
10147 error = got_worktree_rebase_continue(&resume_commit_id,
10148 &new_base_branch, &tmp_branch, &branch, &fileindex,
10149 worktree, repo);
10150 if (error)
10151 goto done;
10152 printf("Switching work tree to %s\n",
10153 got_ref_get_symref_target(new_base_branch));
10154 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10155 new_base_branch, abort_progress, &upa);
10156 if (error)
10157 goto done;
10158 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10159 print_merge_progress_stats(&upa);
10160 goto done; /* nothing else to do */
10163 if (continue_rebase) {
10164 if (!rebase_in_progress) {
10165 error = got_error(GOT_ERR_NOT_REBASING);
10166 goto done;
10168 error = got_worktree_rebase_continue(&resume_commit_id,
10169 &new_base_branch, &tmp_branch, &branch, &fileindex,
10170 worktree, repo);
10171 if (error)
10172 goto done;
10174 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10175 resume_commit_id, repo);
10176 if (error)
10177 goto done;
10179 yca_id = got_object_id_dup(resume_commit_id);
10180 if (yca_id == NULL) {
10181 error = got_error_from_errno("got_object_id_dup");
10182 goto done;
10184 } else {
10185 error = got_ref_open(&branch, repo, argv[0], 0);
10186 if (error != NULL)
10187 goto done;
10190 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10191 if (error)
10192 goto done;
10194 if (!continue_rebase) {
10195 struct got_object_id *base_commit_id;
10197 base_commit_id = got_worktree_get_base_commit_id(worktree);
10198 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10199 base_commit_id, branch_head_commit_id, 1, repo,
10200 check_cancelled, NULL);
10201 if (error)
10202 goto done;
10203 if (yca_id == NULL) {
10204 error = got_error_msg(GOT_ERR_ANCESTRY,
10205 "specified branch shares no common ancestry "
10206 "with work tree's branch");
10207 goto done;
10210 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10211 if (error) {
10212 if (error->code != GOT_ERR_ANCESTRY)
10213 goto done;
10214 error = NULL;
10215 } else {
10216 struct got_pathlist_head paths;
10217 printf("%s is already based on %s\n",
10218 got_ref_get_name(branch),
10219 got_worktree_get_head_ref_name(worktree));
10220 error = switch_head_ref(branch, branch_head_commit_id,
10221 worktree, repo);
10222 if (error)
10223 goto done;
10224 error = got_worktree_set_base_commit_id(worktree, repo,
10225 branch_head_commit_id);
10226 if (error)
10227 goto done;
10228 TAILQ_INIT(&paths);
10229 error = got_pathlist_append(&paths, "", NULL);
10230 if (error)
10231 goto done;
10232 error = got_worktree_checkout_files(worktree,
10233 &paths, repo, update_progress, &upa,
10234 check_cancelled, NULL);
10235 got_pathlist_free(&paths);
10236 if (error)
10237 goto done;
10238 if (upa.did_something) {
10239 char *id_str;
10240 error = got_object_id_str(&id_str,
10241 branch_head_commit_id);
10242 if (error)
10243 goto done;
10244 printf("Updated to %s: %s\n",
10245 got_worktree_get_head_ref_name(worktree),
10246 id_str);
10247 free(id_str);
10248 } else
10249 printf("Already up-to-date\n");
10250 print_update_progress_stats(&upa);
10251 goto done;
10255 commit_id = branch_head_commit_id;
10256 error = got_object_open_as_commit(&commit, repo, commit_id);
10257 if (error)
10258 goto done;
10260 parent_ids = got_object_commit_get_parent_ids(commit);
10261 pid = STAILQ_FIRST(parent_ids);
10262 if (pid == NULL) {
10263 error = got_error(GOT_ERR_EMPTY_REBASE);
10264 goto done;
10266 error = collect_commits(&commits, commit_id, &pid->id,
10267 yca_id, got_worktree_get_path_prefix(worktree),
10268 GOT_ERR_REBASE_PATH, repo);
10269 got_object_commit_close(commit);
10270 commit = NULL;
10271 if (error)
10272 goto done;
10274 if (!continue_rebase) {
10275 error = got_worktree_rebase_prepare(&new_base_branch,
10276 &tmp_branch, &fileindex, worktree, branch, repo);
10277 if (error)
10278 goto done;
10281 if (STAILQ_EMPTY(&commits)) {
10282 if (continue_rebase) {
10283 error = rebase_complete(worktree, fileindex,
10284 branch, new_base_branch, tmp_branch, repo,
10285 create_backup);
10286 goto done;
10287 } else {
10288 /* Fast-forward the reference of the branch. */
10289 struct got_object_id *new_head_commit_id;
10290 char *id_str;
10291 error = got_ref_resolve(&new_head_commit_id, repo,
10292 new_base_branch);
10293 if (error)
10294 goto done;
10295 error = got_object_id_str(&id_str, new_head_commit_id);
10296 printf("Forwarding %s to commit %s\n",
10297 got_ref_get_name(branch), id_str);
10298 free(id_str);
10299 error = got_ref_change_ref(branch,
10300 new_head_commit_id);
10301 if (error)
10302 goto done;
10303 /* No backup needed since objects did not change. */
10304 create_backup = 0;
10308 pid = NULL;
10309 STAILQ_FOREACH(qid, &commits, entry) {
10311 commit_id = &qid->id;
10312 parent_id = pid ? &pid->id : yca_id;
10313 pid = qid;
10315 memset(&upa, 0, sizeof(upa));
10316 error = got_worktree_rebase_merge_files(&merged_paths,
10317 worktree, fileindex, parent_id, commit_id, repo,
10318 update_progress, &upa, check_cancelled, NULL);
10319 if (error)
10320 goto done;
10322 print_merge_progress_stats(&upa);
10323 if (upa.conflicts > 0 || upa.missing > 0 ||
10324 upa.not_deleted > 0 || upa.unversioned > 0) {
10325 if (upa.conflicts > 0) {
10326 error = show_rebase_merge_conflict(&qid->id,
10327 repo);
10328 if (error)
10329 goto done;
10331 got_worktree_rebase_pathlist_free(&merged_paths);
10332 break;
10335 error = rebase_commit(&merged_paths, worktree, fileindex,
10336 tmp_branch, commit_id, repo);
10337 got_worktree_rebase_pathlist_free(&merged_paths);
10338 if (error)
10339 goto done;
10342 if (upa.conflicts > 0 || upa.missing > 0 ||
10343 upa.not_deleted > 0 || upa.unversioned > 0) {
10344 error = got_worktree_rebase_postpone(worktree, fileindex);
10345 if (error)
10346 goto done;
10347 if (upa.conflicts > 0 && upa.missing == 0 &&
10348 upa.not_deleted == 0 && upa.unversioned == 0) {
10349 error = got_error_msg(GOT_ERR_CONFLICTS,
10350 "conflicts must be resolved before rebasing "
10351 "can continue");
10352 } else if (upa.conflicts > 0) {
10353 error = got_error_msg(GOT_ERR_CONFLICTS,
10354 "conflicts must be resolved before rebasing "
10355 "can continue; changes destined for some "
10356 "files were not yet merged and should be "
10357 "merged manually if required before the "
10358 "rebase operation is continued");
10359 } else {
10360 error = got_error_msg(GOT_ERR_CONFLICTS,
10361 "changes destined for some files were not "
10362 "yet merged and should be merged manually "
10363 "if required before the rebase operation "
10364 "is continued");
10366 } else
10367 error = rebase_complete(worktree, fileindex, branch,
10368 new_base_branch, tmp_branch, repo, create_backup);
10369 done:
10370 got_object_id_queue_free(&commits);
10371 free(branch_head_commit_id);
10372 free(resume_commit_id);
10373 free(yca_id);
10374 if (commit)
10375 got_object_commit_close(commit);
10376 if (branch)
10377 got_ref_close(branch);
10378 if (new_base_branch)
10379 got_ref_close(new_base_branch);
10380 if (tmp_branch)
10381 got_ref_close(tmp_branch);
10382 if (worktree)
10383 got_worktree_close(worktree);
10384 if (repo) {
10385 const struct got_error *close_err = got_repo_close(repo);
10386 if (error == NULL)
10387 error = close_err;
10389 if (pack_fds) {
10390 const struct got_error *pack_err =
10391 got_repo_pack_fds_close(pack_fds);
10392 if (error == NULL)
10393 error = pack_err;
10395 return error;
10398 __dead static void
10399 usage_histedit(void)
10401 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10402 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10403 getprogname());
10404 exit(1);
10407 #define GOT_HISTEDIT_PICK 'p'
10408 #define GOT_HISTEDIT_EDIT 'e'
10409 #define GOT_HISTEDIT_FOLD 'f'
10410 #define GOT_HISTEDIT_DROP 'd'
10411 #define GOT_HISTEDIT_MESG 'm'
10413 static const struct got_histedit_cmd {
10414 unsigned char code;
10415 const char *name;
10416 const char *desc;
10417 } got_histedit_cmds[] = {
10418 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10419 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10420 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10421 "be used" },
10422 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10423 { GOT_HISTEDIT_MESG, "mesg",
10424 "single-line log message for commit above (open editor if empty)" },
10427 struct got_histedit_list_entry {
10428 TAILQ_ENTRY(got_histedit_list_entry) entry;
10429 struct got_object_id *commit_id;
10430 const struct got_histedit_cmd *cmd;
10431 char *logmsg;
10433 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10435 static const struct got_error *
10436 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10437 FILE *f, struct got_repository *repo)
10439 const struct got_error *err = NULL;
10440 char *logmsg = NULL, *id_str = NULL;
10441 struct got_commit_object *commit = NULL;
10442 int n;
10444 err = got_object_open_as_commit(&commit, repo, commit_id);
10445 if (err)
10446 goto done;
10448 err = get_short_logmsg(&logmsg, 34, commit);
10449 if (err)
10450 goto done;
10452 err = got_object_id_str(&id_str, commit_id);
10453 if (err)
10454 goto done;
10456 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10457 if (n < 0)
10458 err = got_ferror(f, GOT_ERR_IO);
10459 done:
10460 if (commit)
10461 got_object_commit_close(commit);
10462 free(id_str);
10463 free(logmsg);
10464 return err;
10467 static const struct got_error *
10468 histedit_write_commit_list(struct got_object_id_queue *commits,
10469 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10470 struct got_repository *repo)
10472 const struct got_error *err = NULL;
10473 struct got_object_qid *qid;
10474 const char *histedit_cmd = NULL;
10476 if (STAILQ_EMPTY(commits))
10477 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10479 STAILQ_FOREACH(qid, commits, entry) {
10480 histedit_cmd = got_histedit_cmds[0].name;
10481 if (edit_only)
10482 histedit_cmd = "edit";
10483 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10484 histedit_cmd = "fold";
10485 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10486 if (err)
10487 break;
10488 if (edit_logmsg_only) {
10489 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10490 if (n < 0) {
10491 err = got_ferror(f, GOT_ERR_IO);
10492 break;
10497 return err;
10500 static const struct got_error *
10501 write_cmd_list(FILE *f, const char *branch_name,
10502 struct got_object_id_queue *commits)
10504 const struct got_error *err = NULL;
10505 size_t i;
10506 int n;
10507 char *id_str;
10508 struct got_object_qid *qid;
10510 qid = STAILQ_FIRST(commits);
10511 err = got_object_id_str(&id_str, &qid->id);
10512 if (err)
10513 return err;
10515 n = fprintf(f,
10516 "# Editing the history of branch '%s' starting at\n"
10517 "# commit %s\n"
10518 "# Commits will be processed in order from top to "
10519 "bottom of this file.\n", branch_name, id_str);
10520 if (n < 0) {
10521 err = got_ferror(f, GOT_ERR_IO);
10522 goto done;
10525 n = fprintf(f, "# Available histedit commands:\n");
10526 if (n < 0) {
10527 err = got_ferror(f, GOT_ERR_IO);
10528 goto done;
10531 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10532 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10533 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10534 cmd->desc);
10535 if (n < 0) {
10536 err = got_ferror(f, GOT_ERR_IO);
10537 break;
10540 done:
10541 free(id_str);
10542 return err;
10545 static const struct got_error *
10546 histedit_syntax_error(int lineno)
10548 static char msg[42];
10549 int ret;
10551 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10552 lineno);
10553 if (ret == -1 || ret >= sizeof(msg))
10554 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10556 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10559 static const struct got_error *
10560 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10561 char *logmsg, struct got_repository *repo)
10563 const struct got_error *err;
10564 struct got_commit_object *folded_commit = NULL;
10565 char *id_str, *folded_logmsg = NULL;
10567 err = got_object_id_str(&id_str, hle->commit_id);
10568 if (err)
10569 return err;
10571 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10572 if (err)
10573 goto done;
10575 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10576 if (err)
10577 goto done;
10578 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10579 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10580 folded_logmsg) == -1) {
10581 err = got_error_from_errno("asprintf");
10583 done:
10584 if (folded_commit)
10585 got_object_commit_close(folded_commit);
10586 free(id_str);
10587 free(folded_logmsg);
10588 return err;
10591 static struct got_histedit_list_entry *
10592 get_folded_commits(struct got_histedit_list_entry *hle)
10594 struct got_histedit_list_entry *prev, *folded = NULL;
10596 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10597 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10598 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10599 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10600 folded = prev;
10601 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10604 return folded;
10607 static const struct got_error *
10608 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10609 struct got_repository *repo)
10611 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10612 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10613 const struct got_error *err = NULL;
10614 struct got_commit_object *commit = NULL;
10615 int logmsg_len;
10616 int fd;
10617 struct got_histedit_list_entry *folded = NULL;
10619 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10620 if (err)
10621 return err;
10623 folded = get_folded_commits(hle);
10624 if (folded) {
10625 while (folded != hle) {
10626 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10627 folded = TAILQ_NEXT(folded, entry);
10628 continue;
10630 err = append_folded_commit_msg(&new_msg, folded,
10631 logmsg, repo);
10632 if (err)
10633 goto done;
10634 free(logmsg);
10635 logmsg = new_msg;
10636 folded = TAILQ_NEXT(folded, entry);
10640 err = got_object_id_str(&id_str, hle->commit_id);
10641 if (err)
10642 goto done;
10643 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10644 if (err)
10645 goto done;
10646 logmsg_len = asprintf(&new_msg,
10647 "%s\n# original log message of commit %s: %s",
10648 logmsg ? logmsg : "", id_str, orig_logmsg);
10649 if (logmsg_len == -1) {
10650 err = got_error_from_errno("asprintf");
10651 goto done;
10653 free(logmsg);
10654 logmsg = new_msg;
10656 err = got_object_id_str(&id_str, hle->commit_id);
10657 if (err)
10658 goto done;
10660 err = got_opentemp_named_fd(&logmsg_path, &fd,
10661 GOT_TMPDIR_STR "/got-logmsg");
10662 if (err)
10663 goto done;
10665 write(fd, logmsg, logmsg_len);
10666 close(fd);
10668 err = get_editor(&editor);
10669 if (err)
10670 goto done;
10672 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10673 logmsg_len, 0);
10674 if (err) {
10675 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10676 goto done;
10677 err = NULL;
10678 hle->logmsg = strdup(new_msg);
10679 if (hle->logmsg == NULL)
10680 err = got_error_from_errno("strdup");
10682 done:
10683 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10684 err = got_error_from_errno2("unlink", logmsg_path);
10685 free(logmsg_path);
10686 free(logmsg);
10687 free(orig_logmsg);
10688 free(editor);
10689 if (commit)
10690 got_object_commit_close(commit);
10691 return err;
10694 static const struct got_error *
10695 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10696 FILE *f, struct got_repository *repo)
10698 const struct got_error *err = NULL;
10699 char *line = NULL, *p, *end;
10700 size_t i, size;
10701 ssize_t len;
10702 int lineno = 0;
10703 const struct got_histedit_cmd *cmd;
10704 struct got_object_id *commit_id = NULL;
10705 struct got_histedit_list_entry *hle = NULL;
10707 for (;;) {
10708 len = getline(&line, &size, f);
10709 if (len == -1) {
10710 const struct got_error *getline_err;
10711 if (feof(f))
10712 break;
10713 getline_err = got_error_from_errno("getline");
10714 err = got_ferror(f, getline_err->code);
10715 break;
10717 lineno++;
10718 p = line;
10719 while (isspace((unsigned char)p[0]))
10720 p++;
10721 if (p[0] == '#' || p[0] == '\0') {
10722 free(line);
10723 line = NULL;
10724 continue;
10726 cmd = NULL;
10727 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10728 cmd = &got_histedit_cmds[i];
10729 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10730 isspace((unsigned char)p[strlen(cmd->name)])) {
10731 p += strlen(cmd->name);
10732 break;
10734 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10735 p++;
10736 break;
10739 if (i == nitems(got_histedit_cmds)) {
10740 err = histedit_syntax_error(lineno);
10741 break;
10743 while (isspace((unsigned char)p[0]))
10744 p++;
10745 if (cmd->code == GOT_HISTEDIT_MESG) {
10746 if (hle == NULL || hle->logmsg != NULL) {
10747 err = got_error(GOT_ERR_HISTEDIT_CMD);
10748 break;
10750 if (p[0] == '\0') {
10751 err = histedit_edit_logmsg(hle, repo);
10752 if (err)
10753 break;
10754 } else {
10755 hle->logmsg = strdup(p);
10756 if (hle->logmsg == NULL) {
10757 err = got_error_from_errno("strdup");
10758 break;
10761 free(line);
10762 line = NULL;
10763 continue;
10764 } else {
10765 end = p;
10766 while (end[0] && !isspace((unsigned char)end[0]))
10767 end++;
10768 *end = '\0';
10770 err = got_object_resolve_id_str(&commit_id, repo, p);
10771 if (err) {
10772 /* override error code */
10773 err = histedit_syntax_error(lineno);
10774 break;
10777 hle = malloc(sizeof(*hle));
10778 if (hle == NULL) {
10779 err = got_error_from_errno("malloc");
10780 break;
10782 hle->cmd = cmd;
10783 hle->commit_id = commit_id;
10784 hle->logmsg = NULL;
10785 commit_id = NULL;
10786 free(line);
10787 line = NULL;
10788 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10791 free(line);
10792 free(commit_id);
10793 return err;
10796 static const struct got_error *
10797 histedit_check_script(struct got_histedit_list *histedit_cmds,
10798 struct got_object_id_queue *commits, struct got_repository *repo)
10800 const struct got_error *err = NULL;
10801 struct got_object_qid *qid;
10802 struct got_histedit_list_entry *hle;
10803 static char msg[92];
10804 char *id_str;
10806 if (TAILQ_EMPTY(histedit_cmds))
10807 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10808 "histedit script contains no commands");
10809 if (STAILQ_EMPTY(commits))
10810 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10812 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10813 struct got_histedit_list_entry *hle2;
10814 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10815 if (hle == hle2)
10816 continue;
10817 if (got_object_id_cmp(hle->commit_id,
10818 hle2->commit_id) != 0)
10819 continue;
10820 err = got_object_id_str(&id_str, hle->commit_id);
10821 if (err)
10822 return err;
10823 snprintf(msg, sizeof(msg), "commit %s is listed "
10824 "more than once in histedit script", id_str);
10825 free(id_str);
10826 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10830 STAILQ_FOREACH(qid, commits, entry) {
10831 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10832 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10833 break;
10835 if (hle == NULL) {
10836 err = got_object_id_str(&id_str, &qid->id);
10837 if (err)
10838 return err;
10839 snprintf(msg, sizeof(msg),
10840 "commit %s missing from histedit script", id_str);
10841 free(id_str);
10842 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10846 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10847 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10848 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10849 "last commit in histedit script cannot be folded");
10851 return NULL;
10854 static const struct got_error *
10855 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10856 const char *path, struct got_object_id_queue *commits,
10857 struct got_repository *repo)
10859 const struct got_error *err = NULL;
10860 char *editor;
10861 FILE *f = NULL;
10863 err = get_editor(&editor);
10864 if (err)
10865 return err;
10867 if (spawn_editor(editor, path) == -1) {
10868 err = got_error_from_errno("failed spawning editor");
10869 goto done;
10872 f = fopen(path, "re");
10873 if (f == NULL) {
10874 err = got_error_from_errno("fopen");
10875 goto done;
10877 err = histedit_parse_list(histedit_cmds, f, repo);
10878 if (err)
10879 goto done;
10881 err = histedit_check_script(histedit_cmds, commits, repo);
10882 done:
10883 if (f && fclose(f) == EOF && err == NULL)
10884 err = got_error_from_errno("fclose");
10885 free(editor);
10886 return err;
10889 static const struct got_error *
10890 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10891 struct got_object_id_queue *, const char *, const char *,
10892 struct got_repository *);
10894 static const struct got_error *
10895 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10896 struct got_object_id_queue *commits, const char *branch_name,
10897 int edit_logmsg_only, int fold_only, int edit_only,
10898 struct got_repository *repo)
10900 const struct got_error *err;
10901 FILE *f = NULL;
10902 char *path = NULL;
10904 err = got_opentemp_named(&path, &f, "got-histedit");
10905 if (err)
10906 return err;
10908 err = write_cmd_list(f, branch_name, commits);
10909 if (err)
10910 goto done;
10912 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10913 fold_only, edit_only, repo);
10914 if (err)
10915 goto done;
10917 if (edit_logmsg_only || fold_only || edit_only) {
10918 rewind(f);
10919 err = histedit_parse_list(histedit_cmds, f, repo);
10920 } else {
10921 if (fclose(f) == EOF) {
10922 err = got_error_from_errno("fclose");
10923 goto done;
10925 f = NULL;
10926 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10927 if (err) {
10928 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10929 err->code != GOT_ERR_HISTEDIT_CMD)
10930 goto done;
10931 err = histedit_edit_list_retry(histedit_cmds, err,
10932 commits, path, branch_name, repo);
10935 done:
10936 if (f && fclose(f) == EOF && err == NULL)
10937 err = got_error_from_errno("fclose");
10938 if (path && unlink(path) != 0 && err == NULL)
10939 err = got_error_from_errno2("unlink", path);
10940 free(path);
10941 return err;
10944 static const struct got_error *
10945 histedit_save_list(struct got_histedit_list *histedit_cmds,
10946 struct got_worktree *worktree, struct got_repository *repo)
10948 const struct got_error *err = NULL;
10949 char *path = NULL;
10950 FILE *f = NULL;
10951 struct got_histedit_list_entry *hle;
10952 struct got_commit_object *commit = NULL;
10954 err = got_worktree_get_histedit_script_path(&path, worktree);
10955 if (err)
10956 return err;
10958 f = fopen(path, "we");
10959 if (f == NULL) {
10960 err = got_error_from_errno2("fopen", path);
10961 goto done;
10963 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10964 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10965 repo);
10966 if (err)
10967 break;
10969 if (hle->logmsg) {
10970 int n = fprintf(f, "%c %s\n",
10971 GOT_HISTEDIT_MESG, hle->logmsg);
10972 if (n < 0) {
10973 err = got_ferror(f, GOT_ERR_IO);
10974 break;
10978 done:
10979 if (f && fclose(f) == EOF && err == NULL)
10980 err = got_error_from_errno("fclose");
10981 free(path);
10982 if (commit)
10983 got_object_commit_close(commit);
10984 return err;
10987 static void
10988 histedit_free_list(struct got_histedit_list *histedit_cmds)
10990 struct got_histedit_list_entry *hle;
10992 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10993 TAILQ_REMOVE(histedit_cmds, hle, entry);
10994 free(hle);
10998 static const struct got_error *
10999 histedit_load_list(struct got_histedit_list *histedit_cmds,
11000 const char *path, struct got_repository *repo)
11002 const struct got_error *err = NULL;
11003 FILE *f = NULL;
11005 f = fopen(path, "re");
11006 if (f == NULL) {
11007 err = got_error_from_errno2("fopen", path);
11008 goto done;
11011 err = histedit_parse_list(histedit_cmds, f, repo);
11012 done:
11013 if (f && fclose(f) == EOF && err == NULL)
11014 err = got_error_from_errno("fclose");
11015 return err;
11018 static const struct got_error *
11019 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11020 const struct got_error *edit_err, struct got_object_id_queue *commits,
11021 const char *path, const char *branch_name, struct got_repository *repo)
11023 const struct got_error *err = NULL, *prev_err = edit_err;
11024 int resp = ' ';
11026 while (resp != 'c' && resp != 'r' && resp != 'a') {
11027 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11028 "or (a)bort: ", getprogname(), prev_err->msg);
11029 resp = getchar();
11030 if (resp == '\n')
11031 resp = getchar();
11032 if (resp == 'c') {
11033 histedit_free_list(histedit_cmds);
11034 err = histedit_run_editor(histedit_cmds, path, commits,
11035 repo);
11036 if (err) {
11037 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11038 err->code != GOT_ERR_HISTEDIT_CMD)
11039 break;
11040 prev_err = err;
11041 resp = ' ';
11042 continue;
11044 break;
11045 } else if (resp == 'r') {
11046 histedit_free_list(histedit_cmds);
11047 err = histedit_edit_script(histedit_cmds,
11048 commits, branch_name, 0, 0, 0, repo);
11049 if (err) {
11050 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11051 err->code != GOT_ERR_HISTEDIT_CMD)
11052 break;
11053 prev_err = err;
11054 resp = ' ';
11055 continue;
11057 break;
11058 } else if (resp == 'a') {
11059 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11060 break;
11061 } else
11062 printf("invalid response '%c'\n", resp);
11065 return err;
11068 static const struct got_error *
11069 histedit_complete(struct got_worktree *worktree,
11070 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11071 struct got_reference *branch, struct got_repository *repo)
11073 printf("Switching work tree to %s\n",
11074 got_ref_get_symref_target(branch));
11075 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11076 branch, repo);
11079 static const struct got_error *
11080 show_histedit_progress(struct got_commit_object *commit,
11081 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11083 const struct got_error *err;
11084 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11086 err = got_object_id_str(&old_id_str, hle->commit_id);
11087 if (err)
11088 goto done;
11090 if (new_id) {
11091 err = got_object_id_str(&new_id_str, new_id);
11092 if (err)
11093 goto done;
11096 old_id_str[12] = '\0';
11097 if (new_id_str)
11098 new_id_str[12] = '\0';
11100 if (hle->logmsg) {
11101 logmsg = strdup(hle->logmsg);
11102 if (logmsg == NULL) {
11103 err = got_error_from_errno("strdup");
11104 goto done;
11106 trim_logmsg(logmsg, 42);
11107 } else {
11108 err = get_short_logmsg(&logmsg, 42, commit);
11109 if (err)
11110 goto done;
11113 switch (hle->cmd->code) {
11114 case GOT_HISTEDIT_PICK:
11115 case GOT_HISTEDIT_EDIT:
11116 printf("%s -> %s: %s\n", old_id_str,
11117 new_id_str ? new_id_str : "no-op change", logmsg);
11118 break;
11119 case GOT_HISTEDIT_DROP:
11120 case GOT_HISTEDIT_FOLD:
11121 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11122 logmsg);
11123 break;
11124 default:
11125 break;
11127 done:
11128 free(old_id_str);
11129 free(new_id_str);
11130 return err;
11133 static const struct got_error *
11134 histedit_commit(struct got_pathlist_head *merged_paths,
11135 struct got_worktree *worktree, struct got_fileindex *fileindex,
11136 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11137 struct got_repository *repo)
11139 const struct got_error *err;
11140 struct got_commit_object *commit;
11141 struct got_object_id *new_commit_id;
11143 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11144 && hle->logmsg == NULL) {
11145 err = histedit_edit_logmsg(hle, repo);
11146 if (err)
11147 return err;
11150 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11151 if (err)
11152 return err;
11154 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11155 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11156 hle->logmsg, repo);
11157 if (err) {
11158 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11159 goto done;
11160 err = show_histedit_progress(commit, hle, NULL);
11161 } else {
11162 err = show_histedit_progress(commit, hle, new_commit_id);
11163 free(new_commit_id);
11165 done:
11166 got_object_commit_close(commit);
11167 return err;
11170 static const struct got_error *
11171 histedit_skip_commit(struct got_histedit_list_entry *hle,
11172 struct got_worktree *worktree, struct got_repository *repo)
11174 const struct got_error *error;
11175 struct got_commit_object *commit;
11177 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11178 repo);
11179 if (error)
11180 return error;
11182 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11183 if (error)
11184 return error;
11186 error = show_histedit_progress(commit, hle, NULL);
11187 got_object_commit_close(commit);
11188 return error;
11191 static const struct got_error *
11192 check_local_changes(void *arg, unsigned char status,
11193 unsigned char staged_status, const char *path,
11194 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11195 struct got_object_id *commit_id, int dirfd, const char *de_name)
11197 int *have_local_changes = arg;
11199 switch (status) {
11200 case GOT_STATUS_ADD:
11201 case GOT_STATUS_DELETE:
11202 case GOT_STATUS_MODIFY:
11203 case GOT_STATUS_CONFLICT:
11204 *have_local_changes = 1;
11205 return got_error(GOT_ERR_CANCELLED);
11206 default:
11207 break;
11210 switch (staged_status) {
11211 case GOT_STATUS_ADD:
11212 case GOT_STATUS_DELETE:
11213 case GOT_STATUS_MODIFY:
11214 *have_local_changes = 1;
11215 return got_error(GOT_ERR_CANCELLED);
11216 default:
11217 break;
11220 return NULL;
11223 static const struct got_error *
11224 cmd_histedit(int argc, char *argv[])
11226 const struct got_error *error = NULL;
11227 struct got_worktree *worktree = NULL;
11228 struct got_fileindex *fileindex = NULL;
11229 struct got_repository *repo = NULL;
11230 char *cwd = NULL;
11231 struct got_reference *branch = NULL;
11232 struct got_reference *tmp_branch = NULL;
11233 struct got_object_id *resume_commit_id = NULL;
11234 struct got_object_id *base_commit_id = NULL;
11235 struct got_object_id *head_commit_id = NULL;
11236 struct got_commit_object *commit = NULL;
11237 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11238 struct got_update_progress_arg upa;
11239 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11240 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11241 int list_backups = 0, delete_backups = 0;
11242 const char *edit_script_path = NULL;
11243 struct got_object_id_queue commits;
11244 struct got_pathlist_head merged_paths;
11245 const struct got_object_id_queue *parent_ids;
11246 struct got_object_qid *pid;
11247 struct got_histedit_list histedit_cmds;
11248 struct got_histedit_list_entry *hle;
11249 int *pack_fds = NULL;
11251 STAILQ_INIT(&commits);
11252 TAILQ_INIT(&histedit_cmds);
11253 TAILQ_INIT(&merged_paths);
11254 memset(&upa, 0, sizeof(upa));
11256 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11257 switch (ch) {
11258 case 'a':
11259 abort_edit = 1;
11260 break;
11261 case 'c':
11262 continue_edit = 1;
11263 break;
11264 case 'e':
11265 edit_only = 1;
11266 break;
11267 case 'f':
11268 fold_only = 1;
11269 break;
11270 case 'F':
11271 edit_script_path = optarg;
11272 break;
11273 case 'm':
11274 edit_logmsg_only = 1;
11275 break;
11276 case 'l':
11277 list_backups = 1;
11278 break;
11279 case 'X':
11280 delete_backups = 1;
11281 break;
11282 default:
11283 usage_histedit();
11284 /* NOTREACHED */
11288 argc -= optind;
11289 argv += optind;
11291 #ifndef PROFILE
11292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11293 "unveil", NULL) == -1)
11294 err(1, "pledge");
11295 #endif
11296 if (abort_edit && continue_edit)
11297 option_conflict('a', 'c');
11298 if (edit_script_path && edit_logmsg_only)
11299 option_conflict('F', 'm');
11300 if (abort_edit && edit_logmsg_only)
11301 option_conflict('a', 'm');
11302 if (continue_edit && edit_logmsg_only)
11303 option_conflict('c', 'm');
11304 if (abort_edit && fold_only)
11305 option_conflict('a', 'f');
11306 if (continue_edit && fold_only)
11307 option_conflict('c', 'f');
11308 if (fold_only && edit_logmsg_only)
11309 option_conflict('f', 'm');
11310 if (edit_script_path && fold_only)
11311 option_conflict('F', 'f');
11312 if (abort_edit && edit_only)
11313 option_conflict('a', 'e');
11314 if (continue_edit && edit_only)
11315 option_conflict('c', 'e');
11316 if (edit_only && edit_logmsg_only)
11317 option_conflict('e', 'm');
11318 if (edit_script_path && edit_only)
11319 option_conflict('F', 'e');
11320 if (list_backups) {
11321 if (abort_edit)
11322 option_conflict('l', 'a');
11323 if (continue_edit)
11324 option_conflict('l', 'c');
11325 if (edit_script_path)
11326 option_conflict('l', 'F');
11327 if (edit_logmsg_only)
11328 option_conflict('l', 'm');
11329 if (fold_only)
11330 option_conflict('l', 'f');
11331 if (edit_only)
11332 option_conflict('l', 'e');
11333 if (delete_backups)
11334 option_conflict('l', 'X');
11335 if (argc != 0 && argc != 1)
11336 usage_histedit();
11337 } else if (delete_backups) {
11338 if (abort_edit)
11339 option_conflict('X', 'a');
11340 if (continue_edit)
11341 option_conflict('X', 'c');
11342 if (edit_script_path)
11343 option_conflict('X', 'F');
11344 if (edit_logmsg_only)
11345 option_conflict('X', 'm');
11346 if (fold_only)
11347 option_conflict('X', 'f');
11348 if (edit_only)
11349 option_conflict('X', 'e');
11350 if (list_backups)
11351 option_conflict('X', 'l');
11352 if (argc != 0 && argc != 1)
11353 usage_histedit();
11354 } else if (argc != 0)
11355 usage_histedit();
11358 * This command cannot apply unveil(2) in all cases because the
11359 * user may choose to run an editor to edit the histedit script
11360 * and to edit individual commit log messages.
11361 * unveil(2) traverses exec(2); if an editor is used we have to
11362 * apply unveil after edit script and log messages have been written.
11363 * XXX TODO: Make use of unveil(2) where possible.
11366 cwd = getcwd(NULL, 0);
11367 if (cwd == NULL) {
11368 error = got_error_from_errno("getcwd");
11369 goto done;
11372 error = got_repo_pack_fds_open(&pack_fds);
11373 if (error != NULL)
11374 goto done;
11376 error = got_worktree_open(&worktree, cwd);
11377 if (error) {
11378 if (list_backups || delete_backups) {
11379 if (error->code != GOT_ERR_NOT_WORKTREE)
11380 goto done;
11381 } else {
11382 if (error->code == GOT_ERR_NOT_WORKTREE)
11383 error = wrap_not_worktree_error(error,
11384 "histedit", cwd);
11385 goto done;
11389 if (list_backups || delete_backups) {
11390 error = got_repo_open(&repo,
11391 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11392 NULL, pack_fds);
11393 if (error != NULL)
11394 goto done;
11395 error = apply_unveil(got_repo_get_path(repo), 0,
11396 worktree ? got_worktree_get_root_path(worktree) : NULL);
11397 if (error)
11398 goto done;
11399 error = process_backup_refs(
11400 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11401 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11402 goto done; /* nothing else to do */
11405 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11406 NULL, pack_fds);
11407 if (error != NULL)
11408 goto done;
11410 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11411 if (error)
11412 goto done;
11413 if (rebase_in_progress) {
11414 error = got_error(GOT_ERR_REBASING);
11415 goto done;
11418 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11419 repo);
11420 if (error)
11421 goto done;
11422 if (merge_in_progress) {
11423 error = got_error(GOT_ERR_MERGE_BUSY);
11424 goto done;
11427 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11428 if (error)
11429 goto done;
11431 if (edit_in_progress && edit_logmsg_only) {
11432 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11433 "histedit operation is in progress in this "
11434 "work tree and must be continued or aborted "
11435 "before the -m option can be used");
11436 goto done;
11438 if (edit_in_progress && fold_only) {
11439 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11440 "histedit operation is in progress in this "
11441 "work tree and must be continued or aborted "
11442 "before the -f option can be used");
11443 goto done;
11445 if (edit_in_progress && edit_only) {
11446 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11447 "histedit operation is in progress in this "
11448 "work tree and must be continued or aborted "
11449 "before the -e option can be used");
11450 goto done;
11453 if (edit_in_progress && abort_edit) {
11454 error = got_worktree_histedit_continue(&resume_commit_id,
11455 &tmp_branch, &branch, &base_commit_id, &fileindex,
11456 worktree, repo);
11457 if (error)
11458 goto done;
11459 printf("Switching work tree to %s\n",
11460 got_ref_get_symref_target(branch));
11461 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11462 branch, base_commit_id, abort_progress, &upa);
11463 if (error)
11464 goto done;
11465 printf("Histedit of %s aborted\n",
11466 got_ref_get_symref_target(branch));
11467 print_merge_progress_stats(&upa);
11468 goto done; /* nothing else to do */
11469 } else if (abort_edit) {
11470 error = got_error(GOT_ERR_NOT_HISTEDIT);
11471 goto done;
11474 if (continue_edit) {
11475 char *path;
11477 if (!edit_in_progress) {
11478 error = got_error(GOT_ERR_NOT_HISTEDIT);
11479 goto done;
11482 error = got_worktree_get_histedit_script_path(&path, worktree);
11483 if (error)
11484 goto done;
11486 error = histedit_load_list(&histedit_cmds, path, repo);
11487 free(path);
11488 if (error)
11489 goto done;
11491 error = got_worktree_histedit_continue(&resume_commit_id,
11492 &tmp_branch, &branch, &base_commit_id, &fileindex,
11493 worktree, repo);
11494 if (error)
11495 goto done;
11497 error = got_ref_resolve(&head_commit_id, repo, branch);
11498 if (error)
11499 goto done;
11501 error = got_object_open_as_commit(&commit, repo,
11502 head_commit_id);
11503 if (error)
11504 goto done;
11505 parent_ids = got_object_commit_get_parent_ids(commit);
11506 pid = STAILQ_FIRST(parent_ids);
11507 if (pid == NULL) {
11508 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11509 goto done;
11511 error = collect_commits(&commits, head_commit_id, &pid->id,
11512 base_commit_id, got_worktree_get_path_prefix(worktree),
11513 GOT_ERR_HISTEDIT_PATH, repo);
11514 got_object_commit_close(commit);
11515 commit = NULL;
11516 if (error)
11517 goto done;
11518 } else {
11519 if (edit_in_progress) {
11520 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11521 goto done;
11524 error = got_ref_open(&branch, repo,
11525 got_worktree_get_head_ref_name(worktree), 0);
11526 if (error != NULL)
11527 goto done;
11529 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11530 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11531 "will not edit commit history of a branch outside "
11532 "the \"refs/heads/\" reference namespace");
11533 goto done;
11536 error = got_ref_resolve(&head_commit_id, repo, branch);
11537 got_ref_close(branch);
11538 branch = NULL;
11539 if (error)
11540 goto done;
11542 error = got_object_open_as_commit(&commit, repo,
11543 head_commit_id);
11544 if (error)
11545 goto done;
11546 parent_ids = got_object_commit_get_parent_ids(commit);
11547 pid = STAILQ_FIRST(parent_ids);
11548 if (pid == NULL) {
11549 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11550 goto done;
11552 error = collect_commits(&commits, head_commit_id, &pid->id,
11553 got_worktree_get_base_commit_id(worktree),
11554 got_worktree_get_path_prefix(worktree),
11555 GOT_ERR_HISTEDIT_PATH, repo);
11556 got_object_commit_close(commit);
11557 commit = NULL;
11558 if (error)
11559 goto done;
11561 if (STAILQ_EMPTY(&commits)) {
11562 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11563 goto done;
11566 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11567 &base_commit_id, &fileindex, worktree, repo);
11568 if (error)
11569 goto done;
11571 if (edit_script_path) {
11572 error = histedit_load_list(&histedit_cmds,
11573 edit_script_path, repo);
11574 if (error) {
11575 got_worktree_histedit_abort(worktree, fileindex,
11576 repo, branch, base_commit_id,
11577 abort_progress, &upa);
11578 print_merge_progress_stats(&upa);
11579 goto done;
11581 } else {
11582 const char *branch_name;
11583 branch_name = got_ref_get_symref_target(branch);
11584 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11585 branch_name += 11;
11586 error = histedit_edit_script(&histedit_cmds, &commits,
11587 branch_name, edit_logmsg_only, fold_only,
11588 edit_only, repo);
11589 if (error) {
11590 got_worktree_histedit_abort(worktree, fileindex,
11591 repo, branch, base_commit_id,
11592 abort_progress, &upa);
11593 print_merge_progress_stats(&upa);
11594 goto done;
11599 error = histedit_save_list(&histedit_cmds, worktree,
11600 repo);
11601 if (error) {
11602 got_worktree_histedit_abort(worktree, fileindex,
11603 repo, branch, base_commit_id,
11604 abort_progress, &upa);
11605 print_merge_progress_stats(&upa);
11606 goto done;
11611 error = histedit_check_script(&histedit_cmds, &commits, repo);
11612 if (error)
11613 goto done;
11615 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11616 if (resume_commit_id) {
11617 if (got_object_id_cmp(hle->commit_id,
11618 resume_commit_id) != 0)
11619 continue;
11621 resume_commit_id = NULL;
11622 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11623 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11624 error = histedit_skip_commit(hle, worktree,
11625 repo);
11626 if (error)
11627 goto done;
11628 } else {
11629 struct got_pathlist_head paths;
11630 int have_changes = 0;
11632 TAILQ_INIT(&paths);
11633 error = got_pathlist_append(&paths, "", NULL);
11634 if (error)
11635 goto done;
11636 error = got_worktree_status(worktree, &paths,
11637 repo, 0, check_local_changes, &have_changes,
11638 check_cancelled, NULL);
11639 got_pathlist_free(&paths);
11640 if (error) {
11641 if (error->code != GOT_ERR_CANCELLED)
11642 goto done;
11643 if (sigint_received || sigpipe_received)
11644 goto done;
11646 if (have_changes) {
11647 error = histedit_commit(NULL, worktree,
11648 fileindex, tmp_branch, hle, repo);
11649 if (error)
11650 goto done;
11651 } else {
11652 error = got_object_open_as_commit(
11653 &commit, repo, hle->commit_id);
11654 if (error)
11655 goto done;
11656 error = show_histedit_progress(commit,
11657 hle, NULL);
11658 got_object_commit_close(commit);
11659 commit = NULL;
11660 if (error)
11661 goto done;
11664 continue;
11667 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11668 error = histedit_skip_commit(hle, worktree, repo);
11669 if (error)
11670 goto done;
11671 continue;
11674 error = got_object_open_as_commit(&commit, repo,
11675 hle->commit_id);
11676 if (error)
11677 goto done;
11678 parent_ids = got_object_commit_get_parent_ids(commit);
11679 pid = STAILQ_FIRST(parent_ids);
11681 error = got_worktree_histedit_merge_files(&merged_paths,
11682 worktree, fileindex, &pid->id, hle->commit_id, repo,
11683 update_progress, &upa, check_cancelled, NULL);
11684 if (error)
11685 goto done;
11686 got_object_commit_close(commit);
11687 commit = NULL;
11689 print_merge_progress_stats(&upa);
11690 if (upa.conflicts > 0 || upa.missing > 0 ||
11691 upa.not_deleted > 0 || upa.unversioned > 0) {
11692 if (upa.conflicts > 0) {
11693 error = show_rebase_merge_conflict(
11694 hle->commit_id, repo);
11695 if (error)
11696 goto done;
11698 got_worktree_rebase_pathlist_free(&merged_paths);
11699 break;
11702 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11703 char *id_str;
11704 error = got_object_id_str(&id_str, hle->commit_id);
11705 if (error)
11706 goto done;
11707 printf("Stopping histedit for amending commit %s\n",
11708 id_str);
11709 free(id_str);
11710 got_worktree_rebase_pathlist_free(&merged_paths);
11711 error = got_worktree_histedit_postpone(worktree,
11712 fileindex);
11713 goto done;
11716 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11717 error = histedit_skip_commit(hle, worktree, repo);
11718 if (error)
11719 goto done;
11720 continue;
11723 error = histedit_commit(&merged_paths, worktree, fileindex,
11724 tmp_branch, hle, repo);
11725 got_worktree_rebase_pathlist_free(&merged_paths);
11726 if (error)
11727 goto done;
11730 if (upa.conflicts > 0 || upa.missing > 0 ||
11731 upa.not_deleted > 0 || upa.unversioned > 0) {
11732 error = got_worktree_histedit_postpone(worktree, fileindex);
11733 if (error)
11734 goto done;
11735 if (upa.conflicts > 0 && upa.missing == 0 &&
11736 upa.not_deleted == 0 && upa.unversioned == 0) {
11737 error = got_error_msg(GOT_ERR_CONFLICTS,
11738 "conflicts must be resolved before histedit "
11739 "can continue");
11740 } else if (upa.conflicts > 0) {
11741 error = got_error_msg(GOT_ERR_CONFLICTS,
11742 "conflicts must be resolved before histedit "
11743 "can continue; changes destined for some "
11744 "files were not yet merged and should be "
11745 "merged manually if required before the "
11746 "histedit operation is continued");
11747 } else {
11748 error = got_error_msg(GOT_ERR_CONFLICTS,
11749 "changes destined for some files were not "
11750 "yet merged and should be merged manually "
11751 "if required before the histedit operation "
11752 "is continued");
11754 } else
11755 error = histedit_complete(worktree, fileindex, tmp_branch,
11756 branch, repo);
11757 done:
11758 got_object_id_queue_free(&commits);
11759 histedit_free_list(&histedit_cmds);
11760 free(head_commit_id);
11761 free(base_commit_id);
11762 free(resume_commit_id);
11763 if (commit)
11764 got_object_commit_close(commit);
11765 if (branch)
11766 got_ref_close(branch);
11767 if (tmp_branch)
11768 got_ref_close(tmp_branch);
11769 if (worktree)
11770 got_worktree_close(worktree);
11771 if (repo) {
11772 const struct got_error *close_err = got_repo_close(repo);
11773 if (error == NULL)
11774 error = close_err;
11776 if (pack_fds) {
11777 const struct got_error *pack_err =
11778 got_repo_pack_fds_close(pack_fds);
11779 if (error == NULL)
11780 error = pack_err;
11782 return error;
11785 __dead static void
11786 usage_integrate(void)
11788 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11789 exit(1);
11792 static const struct got_error *
11793 cmd_integrate(int argc, char *argv[])
11795 const struct got_error *error = NULL;
11796 struct got_repository *repo = NULL;
11797 struct got_worktree *worktree = NULL;
11798 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11799 const char *branch_arg = NULL;
11800 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11801 struct got_fileindex *fileindex = NULL;
11802 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11803 int ch;
11804 struct got_update_progress_arg upa;
11805 int *pack_fds = NULL;
11807 while ((ch = getopt(argc, argv, "")) != -1) {
11808 switch (ch) {
11809 default:
11810 usage_integrate();
11811 /* NOTREACHED */
11815 argc -= optind;
11816 argv += optind;
11818 if (argc != 1)
11819 usage_integrate();
11820 branch_arg = argv[0];
11821 #ifndef PROFILE
11822 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11823 "unveil", NULL) == -1)
11824 err(1, "pledge");
11825 #endif
11826 cwd = getcwd(NULL, 0);
11827 if (cwd == NULL) {
11828 error = got_error_from_errno("getcwd");
11829 goto done;
11832 error = got_repo_pack_fds_open(&pack_fds);
11833 if (error != NULL)
11834 goto done;
11836 error = got_worktree_open(&worktree, cwd);
11837 if (error) {
11838 if (error->code == GOT_ERR_NOT_WORKTREE)
11839 error = wrap_not_worktree_error(error, "integrate",
11840 cwd);
11841 goto done;
11844 error = check_rebase_or_histedit_in_progress(worktree);
11845 if (error)
11846 goto done;
11848 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11849 NULL, pack_fds);
11850 if (error != NULL)
11851 goto done;
11853 error = apply_unveil(got_repo_get_path(repo), 0,
11854 got_worktree_get_root_path(worktree));
11855 if (error)
11856 goto done;
11858 error = check_merge_in_progress(worktree, repo);
11859 if (error)
11860 goto done;
11862 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11863 error = got_error_from_errno("asprintf");
11864 goto done;
11867 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11868 &base_branch_ref, worktree, refname, repo);
11869 if (error)
11870 goto done;
11872 refname = strdup(got_ref_get_name(branch_ref));
11873 if (refname == NULL) {
11874 error = got_error_from_errno("strdup");
11875 got_worktree_integrate_abort(worktree, fileindex, repo,
11876 branch_ref, base_branch_ref);
11877 goto done;
11879 base_refname = strdup(got_ref_get_name(base_branch_ref));
11880 if (base_refname == NULL) {
11881 error = got_error_from_errno("strdup");
11882 got_worktree_integrate_abort(worktree, fileindex, repo,
11883 branch_ref, base_branch_ref);
11884 goto done;
11887 error = got_ref_resolve(&commit_id, repo, branch_ref);
11888 if (error)
11889 goto done;
11891 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11892 if (error)
11893 goto done;
11895 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11896 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11897 "specified branch has already been integrated");
11898 got_worktree_integrate_abort(worktree, fileindex, repo,
11899 branch_ref, base_branch_ref);
11900 goto done;
11903 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11904 if (error) {
11905 if (error->code == GOT_ERR_ANCESTRY)
11906 error = got_error(GOT_ERR_REBASE_REQUIRED);
11907 got_worktree_integrate_abort(worktree, fileindex, repo,
11908 branch_ref, base_branch_ref);
11909 goto done;
11912 memset(&upa, 0, sizeof(upa));
11913 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11914 branch_ref, base_branch_ref, update_progress, &upa,
11915 check_cancelled, NULL);
11916 if (error)
11917 goto done;
11919 printf("Integrated %s into %s\n", refname, base_refname);
11920 print_update_progress_stats(&upa);
11921 done:
11922 if (repo) {
11923 const struct got_error *close_err = got_repo_close(repo);
11924 if (error == NULL)
11925 error = close_err;
11927 if (worktree)
11928 got_worktree_close(worktree);
11929 if (pack_fds) {
11930 const struct got_error *pack_err =
11931 got_repo_pack_fds_close(pack_fds);
11932 if (error == NULL)
11933 error = pack_err;
11935 free(cwd);
11936 free(base_commit_id);
11937 free(commit_id);
11938 free(refname);
11939 free(base_refname);
11940 return error;
11943 __dead static void
11944 usage_merge(void)
11946 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11947 getprogname());
11948 exit(1);
11951 static const struct got_error *
11952 cmd_merge(int argc, char *argv[])
11954 const struct got_error *error = NULL;
11955 struct got_worktree *worktree = NULL;
11956 struct got_repository *repo = NULL;
11957 struct got_fileindex *fileindex = NULL;
11958 char *cwd = NULL, *id_str = NULL, *author = NULL;
11959 struct got_reference *branch = NULL, *wt_branch = NULL;
11960 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11961 struct got_object_id *wt_branch_tip = NULL;
11962 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11963 int interrupt_merge = 0;
11964 struct got_update_progress_arg upa;
11965 struct got_object_id *merge_commit_id = NULL;
11966 char *branch_name = NULL;
11967 int *pack_fds = NULL;
11969 memset(&upa, 0, sizeof(upa));
11971 while ((ch = getopt(argc, argv, "acn")) != -1) {
11972 switch (ch) {
11973 case 'a':
11974 abort_merge = 1;
11975 break;
11976 case 'c':
11977 continue_merge = 1;
11978 break;
11979 case 'n':
11980 interrupt_merge = 1;
11981 break;
11982 default:
11983 usage_rebase();
11984 /* NOTREACHED */
11988 argc -= optind;
11989 argv += optind;
11991 #ifndef PROFILE
11992 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11993 "unveil", NULL) == -1)
11994 err(1, "pledge");
11995 #endif
11997 if (abort_merge && continue_merge)
11998 option_conflict('a', 'c');
11999 if (abort_merge || continue_merge) {
12000 if (argc != 0)
12001 usage_merge();
12002 } else if (argc != 1)
12003 usage_merge();
12005 cwd = getcwd(NULL, 0);
12006 if (cwd == NULL) {
12007 error = got_error_from_errno("getcwd");
12008 goto done;
12011 error = got_repo_pack_fds_open(&pack_fds);
12012 if (error != NULL)
12013 goto done;
12015 error = got_worktree_open(&worktree, cwd);
12016 if (error) {
12017 if (error->code == GOT_ERR_NOT_WORKTREE)
12018 error = wrap_not_worktree_error(error,
12019 "merge", cwd);
12020 goto done;
12023 error = got_repo_open(&repo,
12024 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12025 pack_fds);
12026 if (error != NULL)
12027 goto done;
12029 error = apply_unveil(got_repo_get_path(repo), 0,
12030 worktree ? got_worktree_get_root_path(worktree) : NULL);
12031 if (error)
12032 goto done;
12034 error = check_rebase_or_histedit_in_progress(worktree);
12035 if (error)
12036 goto done;
12038 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12039 repo);
12040 if (error)
12041 goto done;
12043 if (abort_merge) {
12044 if (!merge_in_progress) {
12045 error = got_error(GOT_ERR_NOT_MERGING);
12046 goto done;
12048 error = got_worktree_merge_continue(&branch_name,
12049 &branch_tip, &fileindex, worktree, repo);
12050 if (error)
12051 goto done;
12052 error = got_worktree_merge_abort(worktree, fileindex, repo,
12053 abort_progress, &upa);
12054 if (error)
12055 goto done;
12056 printf("Merge of %s aborted\n", branch_name);
12057 goto done; /* nothing else to do */
12060 error = get_author(&author, repo, worktree);
12061 if (error)
12062 goto done;
12064 if (continue_merge) {
12065 if (!merge_in_progress) {
12066 error = got_error(GOT_ERR_NOT_MERGING);
12067 goto done;
12069 error = got_worktree_merge_continue(&branch_name,
12070 &branch_tip, &fileindex, worktree, repo);
12071 if (error)
12072 goto done;
12073 } else {
12074 error = got_ref_open(&branch, repo, argv[0], 0);
12075 if (error != NULL)
12076 goto done;
12077 branch_name = strdup(got_ref_get_name(branch));
12078 if (branch_name == NULL) {
12079 error = got_error_from_errno("strdup");
12080 goto done;
12082 error = got_ref_resolve(&branch_tip, repo, branch);
12083 if (error)
12084 goto done;
12087 error = got_ref_open(&wt_branch, repo,
12088 got_worktree_get_head_ref_name(worktree), 0);
12089 if (error)
12090 goto done;
12091 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12092 if (error)
12093 goto done;
12094 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12095 wt_branch_tip, branch_tip, 0, repo,
12096 check_cancelled, NULL);
12097 if (error && error->code != GOT_ERR_ANCESTRY)
12098 goto done;
12100 if (!continue_merge) {
12101 error = check_path_prefix(wt_branch_tip, branch_tip,
12102 got_worktree_get_path_prefix(worktree),
12103 GOT_ERR_MERGE_PATH, repo);
12104 if (error)
12105 goto done;
12106 if (yca_id) {
12107 error = check_same_branch(wt_branch_tip, branch,
12108 yca_id, repo);
12109 if (error) {
12110 if (error->code != GOT_ERR_ANCESTRY)
12111 goto done;
12112 error = NULL;
12113 } else {
12114 static char msg[512];
12115 snprintf(msg, sizeof(msg),
12116 "cannot create a merge commit because "
12117 "%s is based on %s; %s can be integrated "
12118 "with 'got integrate' instead", branch_name,
12119 got_worktree_get_head_ref_name(worktree),
12120 branch_name);
12121 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12122 goto done;
12125 error = got_worktree_merge_prepare(&fileindex, worktree,
12126 branch, repo);
12127 if (error)
12128 goto done;
12130 error = got_worktree_merge_branch(worktree, fileindex,
12131 yca_id, branch_tip, repo, update_progress, &upa,
12132 check_cancelled, NULL);
12133 if (error)
12134 goto done;
12135 print_merge_progress_stats(&upa);
12136 if (!upa.did_something) {
12137 error = got_worktree_merge_abort(worktree, fileindex,
12138 repo, abort_progress, &upa);
12139 if (error)
12140 goto done;
12141 printf("Already up-to-date\n");
12142 goto done;
12146 if (interrupt_merge) {
12147 error = got_worktree_merge_postpone(worktree, fileindex);
12148 if (error)
12149 goto done;
12150 printf("Merge of %s interrupted on request\n", branch_name);
12151 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12152 upa.not_deleted > 0 || upa.unversioned > 0) {
12153 error = got_worktree_merge_postpone(worktree, fileindex);
12154 if (error)
12155 goto done;
12156 if (upa.conflicts > 0 && upa.missing == 0 &&
12157 upa.not_deleted == 0 && upa.unversioned == 0) {
12158 error = got_error_msg(GOT_ERR_CONFLICTS,
12159 "conflicts must be resolved before merging "
12160 "can continue");
12161 } else if (upa.conflicts > 0) {
12162 error = got_error_msg(GOT_ERR_CONFLICTS,
12163 "conflicts must be resolved before merging "
12164 "can continue; changes destined for some "
12165 "files were not yet merged and "
12166 "should be merged manually if required before the "
12167 "merge operation is continued");
12168 } else {
12169 error = got_error_msg(GOT_ERR_CONFLICTS,
12170 "changes destined for some "
12171 "files were not yet merged and should be "
12172 "merged manually if required before the "
12173 "merge operation is continued");
12175 goto done;
12176 } else {
12177 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12178 fileindex, author, NULL, 1, branch_tip, branch_name,
12179 repo, continue_merge ? print_status : NULL, NULL);
12180 if (error)
12181 goto done;
12182 error = got_worktree_merge_complete(worktree, fileindex, repo);
12183 if (error)
12184 goto done;
12185 error = got_object_id_str(&id_str, merge_commit_id);
12186 if (error)
12187 goto done;
12188 printf("Merged %s into %s: %s\n", branch_name,
12189 got_worktree_get_head_ref_name(worktree),
12190 id_str);
12193 done:
12194 free(id_str);
12195 free(merge_commit_id);
12196 free(author);
12197 free(branch_tip);
12198 free(branch_name);
12199 free(yca_id);
12200 if (branch)
12201 got_ref_close(branch);
12202 if (wt_branch)
12203 got_ref_close(wt_branch);
12204 if (worktree)
12205 got_worktree_close(worktree);
12206 if (repo) {
12207 const struct got_error *close_err = got_repo_close(repo);
12208 if (error == NULL)
12209 error = close_err;
12211 if (pack_fds) {
12212 const struct got_error *pack_err =
12213 got_repo_pack_fds_close(pack_fds);
12214 if (error == NULL)
12215 error = pack_err;
12217 return error;
12220 __dead static void
12221 usage_stage(void)
12223 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12224 "[-S] [file-path ...]\n",
12225 getprogname());
12226 exit(1);
12229 static const struct got_error *
12230 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12231 const char *path, struct got_object_id *blob_id,
12232 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12233 int dirfd, const char *de_name)
12235 const struct got_error *err = NULL;
12236 char *id_str = NULL;
12238 if (staged_status != GOT_STATUS_ADD &&
12239 staged_status != GOT_STATUS_MODIFY &&
12240 staged_status != GOT_STATUS_DELETE)
12241 return NULL;
12243 if (staged_status == GOT_STATUS_ADD ||
12244 staged_status == GOT_STATUS_MODIFY)
12245 err = got_object_id_str(&id_str, staged_blob_id);
12246 else
12247 err = got_object_id_str(&id_str, blob_id);
12248 if (err)
12249 return err;
12251 printf("%s %c %s\n", id_str, staged_status, path);
12252 free(id_str);
12253 return NULL;
12256 static const struct got_error *
12257 cmd_stage(int argc, char *argv[])
12259 const struct got_error *error = NULL;
12260 struct got_repository *repo = NULL;
12261 struct got_worktree *worktree = NULL;
12262 char *cwd = NULL;
12263 struct got_pathlist_head paths;
12264 struct got_pathlist_entry *pe;
12265 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12266 FILE *patch_script_file = NULL;
12267 const char *patch_script_path = NULL;
12268 struct choose_patch_arg cpa;
12269 int *pack_fds = NULL;
12271 TAILQ_INIT(&paths);
12273 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12274 switch (ch) {
12275 case 'l':
12276 list_stage = 1;
12277 break;
12278 case 'p':
12279 pflag = 1;
12280 break;
12281 case 'F':
12282 patch_script_path = optarg;
12283 break;
12284 case 'S':
12285 allow_bad_symlinks = 1;
12286 break;
12287 default:
12288 usage_stage();
12289 /* NOTREACHED */
12293 argc -= optind;
12294 argv += optind;
12296 #ifndef PROFILE
12297 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12298 "unveil", NULL) == -1)
12299 err(1, "pledge");
12300 #endif
12301 if (list_stage && (pflag || patch_script_path))
12302 errx(1, "-l option cannot be used with other options");
12303 if (patch_script_path && !pflag)
12304 errx(1, "-F option can only be used together with -p option");
12306 cwd = getcwd(NULL, 0);
12307 if (cwd == NULL) {
12308 error = got_error_from_errno("getcwd");
12309 goto done;
12312 error = got_repo_pack_fds_open(&pack_fds);
12313 if (error != NULL)
12314 goto done;
12316 error = got_worktree_open(&worktree, cwd);
12317 if (error) {
12318 if (error->code == GOT_ERR_NOT_WORKTREE)
12319 error = wrap_not_worktree_error(error, "stage", cwd);
12320 goto done;
12323 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12324 NULL, pack_fds);
12325 if (error != NULL)
12326 goto done;
12328 if (patch_script_path) {
12329 patch_script_file = fopen(patch_script_path, "re");
12330 if (patch_script_file == NULL) {
12331 error = got_error_from_errno2("fopen",
12332 patch_script_path);
12333 goto done;
12336 error = apply_unveil(got_repo_get_path(repo), 0,
12337 got_worktree_get_root_path(worktree));
12338 if (error)
12339 goto done;
12341 error = check_merge_in_progress(worktree, repo);
12342 if (error)
12343 goto done;
12345 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12346 if (error)
12347 goto done;
12349 if (list_stage)
12350 error = got_worktree_status(worktree, &paths, repo, 0,
12351 print_stage, NULL, check_cancelled, NULL);
12352 else {
12353 cpa.patch_script_file = patch_script_file;
12354 cpa.action = "stage";
12355 error = got_worktree_stage(worktree, &paths,
12356 pflag ? NULL : print_status, NULL,
12357 pflag ? choose_patch : NULL, &cpa,
12358 allow_bad_symlinks, repo);
12360 done:
12361 if (patch_script_file && fclose(patch_script_file) == EOF &&
12362 error == NULL)
12363 error = got_error_from_errno2("fclose", patch_script_path);
12364 if (repo) {
12365 const struct got_error *close_err = got_repo_close(repo);
12366 if (error == NULL)
12367 error = close_err;
12369 if (worktree)
12370 got_worktree_close(worktree);
12371 if (pack_fds) {
12372 const struct got_error *pack_err =
12373 got_repo_pack_fds_close(pack_fds);
12374 if (error == NULL)
12375 error = pack_err;
12377 TAILQ_FOREACH(pe, &paths, entry)
12378 free((char *)pe->path);
12379 got_pathlist_free(&paths);
12380 free(cwd);
12381 return error;
12384 __dead static void
12385 usage_unstage(void)
12387 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12388 "[file-path ...]\n",
12389 getprogname());
12390 exit(1);
12394 static const struct got_error *
12395 cmd_unstage(int argc, char *argv[])
12397 const struct got_error *error = NULL;
12398 struct got_repository *repo = NULL;
12399 struct got_worktree *worktree = NULL;
12400 char *cwd = NULL;
12401 struct got_pathlist_head paths;
12402 struct got_pathlist_entry *pe;
12403 int ch, pflag = 0;
12404 struct got_update_progress_arg upa;
12405 FILE *patch_script_file = NULL;
12406 const char *patch_script_path = NULL;
12407 struct choose_patch_arg cpa;
12408 int *pack_fds = NULL;
12410 TAILQ_INIT(&paths);
12412 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12413 switch (ch) {
12414 case 'p':
12415 pflag = 1;
12416 break;
12417 case 'F':
12418 patch_script_path = optarg;
12419 break;
12420 default:
12421 usage_unstage();
12422 /* NOTREACHED */
12426 argc -= optind;
12427 argv += optind;
12429 #ifndef PROFILE
12430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12431 "unveil", NULL) == -1)
12432 err(1, "pledge");
12433 #endif
12434 if (patch_script_path && !pflag)
12435 errx(1, "-F option can only be used together with -p option");
12437 cwd = getcwd(NULL, 0);
12438 if (cwd == NULL) {
12439 error = got_error_from_errno("getcwd");
12440 goto done;
12443 error = got_repo_pack_fds_open(&pack_fds);
12444 if (error != NULL)
12445 goto done;
12447 error = got_worktree_open(&worktree, cwd);
12448 if (error) {
12449 if (error->code == GOT_ERR_NOT_WORKTREE)
12450 error = wrap_not_worktree_error(error, "unstage", cwd);
12451 goto done;
12454 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12455 NULL, pack_fds);
12456 if (error != NULL)
12457 goto done;
12459 if (patch_script_path) {
12460 patch_script_file = fopen(patch_script_path, "re");
12461 if (patch_script_file == NULL) {
12462 error = got_error_from_errno2("fopen",
12463 patch_script_path);
12464 goto done;
12468 error = apply_unveil(got_repo_get_path(repo), 0,
12469 got_worktree_get_root_path(worktree));
12470 if (error)
12471 goto done;
12473 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12474 if (error)
12475 goto done;
12477 cpa.patch_script_file = patch_script_file;
12478 cpa.action = "unstage";
12479 memset(&upa, 0, sizeof(upa));
12480 error = got_worktree_unstage(worktree, &paths, update_progress,
12481 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12482 if (!error)
12483 print_merge_progress_stats(&upa);
12484 done:
12485 if (patch_script_file && fclose(patch_script_file) == EOF &&
12486 error == NULL)
12487 error = got_error_from_errno2("fclose", patch_script_path);
12488 if (repo) {
12489 const struct got_error *close_err = got_repo_close(repo);
12490 if (error == NULL)
12491 error = close_err;
12493 if (worktree)
12494 got_worktree_close(worktree);
12495 if (pack_fds) {
12496 const struct got_error *pack_err =
12497 got_repo_pack_fds_close(pack_fds);
12498 if (error == NULL)
12499 error = pack_err;
12501 TAILQ_FOREACH(pe, &paths, entry)
12502 free((char *)pe->path);
12503 got_pathlist_free(&paths);
12504 free(cwd);
12505 return error;
12508 __dead static void
12509 usage_cat(void)
12511 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12512 "arg1 [arg2 ...]\n", getprogname());
12513 exit(1);
12516 static const struct got_error *
12517 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12519 const struct got_error *err;
12520 struct got_blob_object *blob;
12521 int fd = -1;
12523 fd = got_opentempfd();
12524 if (fd == -1)
12525 return got_error_from_errno("got_opentempfd");
12527 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12528 if (err)
12529 goto done;
12531 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12532 done:
12533 if (fd != -1 && close(fd) == -1 && err == NULL)
12534 err = got_error_from_errno("close");
12535 if (blob)
12536 got_object_blob_close(blob);
12537 return err;
12540 static const struct got_error *
12541 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12543 const struct got_error *err;
12544 struct got_tree_object *tree;
12545 int nentries, i;
12547 err = got_object_open_as_tree(&tree, repo, id);
12548 if (err)
12549 return err;
12551 nentries = got_object_tree_get_nentries(tree);
12552 for (i = 0; i < nentries; i++) {
12553 struct got_tree_entry *te;
12554 char *id_str;
12555 if (sigint_received || sigpipe_received)
12556 break;
12557 te = got_object_tree_get_entry(tree, i);
12558 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12559 if (err)
12560 break;
12561 fprintf(outfile, "%s %.7o %s\n", id_str,
12562 got_tree_entry_get_mode(te),
12563 got_tree_entry_get_name(te));
12564 free(id_str);
12567 got_object_tree_close(tree);
12568 return err;
12571 static const struct got_error *
12572 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12574 const struct got_error *err;
12575 struct got_commit_object *commit;
12576 const struct got_object_id_queue *parent_ids;
12577 struct got_object_qid *pid;
12578 char *id_str = NULL;
12579 const char *logmsg = NULL;
12580 char gmtoff[6];
12582 err = got_object_open_as_commit(&commit, repo, id);
12583 if (err)
12584 return err;
12586 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12587 if (err)
12588 goto done;
12590 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12591 parent_ids = got_object_commit_get_parent_ids(commit);
12592 fprintf(outfile, "numparents %d\n",
12593 got_object_commit_get_nparents(commit));
12594 STAILQ_FOREACH(pid, parent_ids, entry) {
12595 char *pid_str;
12596 err = got_object_id_str(&pid_str, &pid->id);
12597 if (err)
12598 goto done;
12599 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12600 free(pid_str);
12602 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12603 got_object_commit_get_author_gmtoff(commit));
12604 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12605 got_object_commit_get_author(commit),
12606 (long long)got_object_commit_get_author_time(commit),
12607 gmtoff);
12609 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12610 got_object_commit_get_committer_gmtoff(commit));
12611 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12612 got_object_commit_get_author(commit),
12613 (long long)got_object_commit_get_committer_time(commit),
12614 gmtoff);
12616 logmsg = got_object_commit_get_logmsg_raw(commit);
12617 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12618 fprintf(outfile, "%s", logmsg);
12619 done:
12620 free(id_str);
12621 got_object_commit_close(commit);
12622 return err;
12625 static const struct got_error *
12626 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12628 const struct got_error *err;
12629 struct got_tag_object *tag;
12630 char *id_str = NULL;
12631 const char *tagmsg = NULL;
12632 char gmtoff[6];
12634 err = got_object_open_as_tag(&tag, repo, id);
12635 if (err)
12636 return err;
12638 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12639 if (err)
12640 goto done;
12642 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12644 switch (got_object_tag_get_object_type(tag)) {
12645 case GOT_OBJ_TYPE_BLOB:
12646 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12647 GOT_OBJ_LABEL_BLOB);
12648 break;
12649 case GOT_OBJ_TYPE_TREE:
12650 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12651 GOT_OBJ_LABEL_TREE);
12652 break;
12653 case GOT_OBJ_TYPE_COMMIT:
12654 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12655 GOT_OBJ_LABEL_COMMIT);
12656 break;
12657 case GOT_OBJ_TYPE_TAG:
12658 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12659 GOT_OBJ_LABEL_TAG);
12660 break;
12661 default:
12662 break;
12665 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12666 got_object_tag_get_name(tag));
12668 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12669 got_object_tag_get_tagger_gmtoff(tag));
12670 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12671 got_object_tag_get_tagger(tag),
12672 (long long)got_object_tag_get_tagger_time(tag),
12673 gmtoff);
12675 tagmsg = got_object_tag_get_message(tag);
12676 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12677 fprintf(outfile, "%s", tagmsg);
12678 done:
12679 free(id_str);
12680 got_object_tag_close(tag);
12681 return err;
12684 static const struct got_error *
12685 cmd_cat(int argc, char *argv[])
12687 const struct got_error *error;
12688 struct got_repository *repo = NULL;
12689 struct got_worktree *worktree = NULL;
12690 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12691 const char *commit_id_str = NULL;
12692 struct got_object_id *id = NULL, *commit_id = NULL;
12693 struct got_commit_object *commit = NULL;
12694 int ch, obj_type, i, force_path = 0;
12695 struct got_reflist_head refs;
12696 int *pack_fds = NULL;
12698 TAILQ_INIT(&refs);
12700 #ifndef PROFILE
12701 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12702 NULL) == -1)
12703 err(1, "pledge");
12704 #endif
12706 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12707 switch (ch) {
12708 case 'c':
12709 commit_id_str = optarg;
12710 break;
12711 case 'r':
12712 repo_path = realpath(optarg, NULL);
12713 if (repo_path == NULL)
12714 return got_error_from_errno2("realpath",
12715 optarg);
12716 got_path_strip_trailing_slashes(repo_path);
12717 break;
12718 case 'P':
12719 force_path = 1;
12720 break;
12721 default:
12722 usage_cat();
12723 /* NOTREACHED */
12727 argc -= optind;
12728 argv += optind;
12730 cwd = getcwd(NULL, 0);
12731 if (cwd == NULL) {
12732 error = got_error_from_errno("getcwd");
12733 goto done;
12736 error = got_repo_pack_fds_open(&pack_fds);
12737 if (error != NULL)
12738 goto done;
12740 if (repo_path == NULL) {
12741 error = got_worktree_open(&worktree, cwd);
12742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12743 goto done;
12744 if (worktree) {
12745 repo_path = strdup(
12746 got_worktree_get_repo_path(worktree));
12747 if (repo_path == NULL) {
12748 error = got_error_from_errno("strdup");
12749 goto done;
12752 /* Release work tree lock. */
12753 got_worktree_close(worktree);
12754 worktree = NULL;
12758 if (repo_path == NULL) {
12759 repo_path = strdup(cwd);
12760 if (repo_path == NULL)
12761 return got_error_from_errno("strdup");
12764 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12765 free(repo_path);
12766 if (error != NULL)
12767 goto done;
12769 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12770 if (error)
12771 goto done;
12773 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12774 if (error)
12775 goto done;
12777 if (commit_id_str == NULL)
12778 commit_id_str = GOT_REF_HEAD;
12779 error = got_repo_match_object_id(&commit_id, NULL,
12780 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12781 if (error)
12782 goto done;
12784 error = got_object_open_as_commit(&commit, repo, commit_id);
12785 if (error)
12786 goto done;
12788 for (i = 0; i < argc; i++) {
12789 if (force_path) {
12790 error = got_object_id_by_path(&id, repo, commit,
12791 argv[i]);
12792 if (error)
12793 break;
12794 } else {
12795 error = got_repo_match_object_id(&id, &label, argv[i],
12796 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12797 repo);
12798 if (error) {
12799 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12800 error->code != GOT_ERR_NOT_REF)
12801 break;
12802 error = got_object_id_by_path(&id, repo,
12803 commit, argv[i]);
12804 if (error)
12805 break;
12809 error = got_object_get_type(&obj_type, repo, id);
12810 if (error)
12811 break;
12813 switch (obj_type) {
12814 case GOT_OBJ_TYPE_BLOB:
12815 error = cat_blob(id, repo, stdout);
12816 break;
12817 case GOT_OBJ_TYPE_TREE:
12818 error = cat_tree(id, repo, stdout);
12819 break;
12820 case GOT_OBJ_TYPE_COMMIT:
12821 error = cat_commit(id, repo, stdout);
12822 break;
12823 case GOT_OBJ_TYPE_TAG:
12824 error = cat_tag(id, repo, stdout);
12825 break;
12826 default:
12827 error = got_error(GOT_ERR_OBJ_TYPE);
12828 break;
12830 if (error)
12831 break;
12832 free(label);
12833 label = NULL;
12834 free(id);
12835 id = NULL;
12837 done:
12838 free(label);
12839 free(id);
12840 free(commit_id);
12841 if (commit)
12842 got_object_commit_close(commit);
12843 if (worktree)
12844 got_worktree_close(worktree);
12845 if (repo) {
12846 const struct got_error *close_err = got_repo_close(repo);
12847 if (error == NULL)
12848 error = close_err;
12850 if (pack_fds) {
12851 const struct got_error *pack_err =
12852 got_repo_pack_fds_close(pack_fds);
12853 if (error == NULL)
12854 error = pack_err;
12857 got_ref_list_free(&refs);
12858 return error;
12861 __dead static void
12862 usage_info(void)
12864 fprintf(stderr, "usage: %s info [path ...]\n",
12865 getprogname());
12866 exit(1);
12869 static const struct got_error *
12870 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12871 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12872 struct got_object_id *commit_id)
12874 const struct got_error *err = NULL;
12875 char *id_str = NULL;
12876 char datebuf[128];
12877 struct tm mytm, *tm;
12878 struct got_pathlist_head *paths = arg;
12879 struct got_pathlist_entry *pe;
12882 * Clear error indication from any of the path arguments which
12883 * would cause this file index entry to be displayed.
12885 TAILQ_FOREACH(pe, paths, entry) {
12886 if (got_path_cmp(path, pe->path, strlen(path),
12887 pe->path_len) == 0 ||
12888 got_path_is_child(path, pe->path, pe->path_len))
12889 pe->data = NULL; /* no error */
12892 printf(GOT_COMMIT_SEP_STR);
12893 if (S_ISLNK(mode))
12894 printf("symlink: %s\n", path);
12895 else if (S_ISREG(mode)) {
12896 printf("file: %s\n", path);
12897 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12898 } else if (S_ISDIR(mode))
12899 printf("directory: %s\n", path);
12900 else
12901 printf("something: %s\n", path);
12903 tm = localtime_r(&mtime, &mytm);
12904 if (tm == NULL)
12905 return NULL;
12906 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12907 return got_error(GOT_ERR_NO_SPACE);
12908 printf("timestamp: %s\n", datebuf);
12910 if (blob_id) {
12911 err = got_object_id_str(&id_str, blob_id);
12912 if (err)
12913 return err;
12914 printf("based on blob: %s\n", id_str);
12915 free(id_str);
12918 if (staged_blob_id) {
12919 err = got_object_id_str(&id_str, staged_blob_id);
12920 if (err)
12921 return err;
12922 printf("based on staged blob: %s\n", id_str);
12923 free(id_str);
12926 if (commit_id) {
12927 err = got_object_id_str(&id_str, commit_id);
12928 if (err)
12929 return err;
12930 printf("based on commit: %s\n", id_str);
12931 free(id_str);
12934 return NULL;
12937 static const struct got_error *
12938 cmd_info(int argc, char *argv[])
12940 const struct got_error *error = NULL;
12941 struct got_worktree *worktree = NULL;
12942 char *cwd = NULL, *id_str = NULL;
12943 struct got_pathlist_head paths;
12944 struct got_pathlist_entry *pe;
12945 char *uuidstr = NULL;
12946 int ch, show_files = 0;
12947 int *pack_fds = NULL;
12949 TAILQ_INIT(&paths);
12951 while ((ch = getopt(argc, argv, "")) != -1) {
12952 switch (ch) {
12953 default:
12954 usage_info();
12955 /* NOTREACHED */
12959 argc -= optind;
12960 argv += optind;
12962 #ifndef PROFILE
12963 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12964 NULL) == -1)
12965 err(1, "pledge");
12966 #endif
12967 cwd = getcwd(NULL, 0);
12968 if (cwd == NULL) {
12969 error = got_error_from_errno("getcwd");
12970 goto done;
12973 error = got_repo_pack_fds_open(&pack_fds);
12974 if (error != NULL)
12975 goto done;
12977 error = got_worktree_open(&worktree, cwd);
12978 if (error) {
12979 if (error->code == GOT_ERR_NOT_WORKTREE)
12980 error = wrap_not_worktree_error(error, "info", cwd);
12981 goto done;
12984 #ifndef PROFILE
12985 /* Remove "cpath" promise. */
12986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12987 NULL) == -1)
12988 err(1, "pledge");
12989 #endif
12990 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12991 if (error)
12992 goto done;
12994 if (argc >= 1) {
12995 error = get_worktree_paths_from_argv(&paths, argc, argv,
12996 worktree);
12997 if (error)
12998 goto done;
12999 show_files = 1;
13002 error = got_object_id_str(&id_str,
13003 got_worktree_get_base_commit_id(worktree));
13004 if (error)
13005 goto done;
13007 error = got_worktree_get_uuid(&uuidstr, worktree);
13008 if (error)
13009 goto done;
13011 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13012 printf("work tree base commit: %s\n", id_str);
13013 printf("work tree path prefix: %s\n",
13014 got_worktree_get_path_prefix(worktree));
13015 printf("work tree branch reference: %s\n",
13016 got_worktree_get_head_ref_name(worktree));
13017 printf("work tree UUID: %s\n", uuidstr);
13018 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13020 if (show_files) {
13021 struct got_pathlist_entry *pe;
13022 TAILQ_FOREACH(pe, &paths, entry) {
13023 if (pe->path_len == 0)
13024 continue;
13026 * Assume this path will fail. This will be corrected
13027 * in print_path_info() in case the path does suceeed.
13029 pe->data = (void *)got_error_path(pe->path,
13030 GOT_ERR_BAD_PATH);
13032 error = got_worktree_path_info(worktree, &paths,
13033 print_path_info, &paths, check_cancelled, NULL);
13034 if (error)
13035 goto done;
13036 TAILQ_FOREACH(pe, &paths, entry) {
13037 if (pe->data != NULL) {
13038 error = pe->data; /* bad path */
13039 break;
13043 done:
13044 if (pack_fds) {
13045 const struct got_error *pack_err =
13046 got_repo_pack_fds_close(pack_fds);
13047 if (error == NULL)
13048 error = pack_err;
13050 TAILQ_FOREACH(pe, &paths, entry)
13051 free((char *)pe->path);
13052 got_pathlist_free(&paths);
13053 free(cwd);
13054 free(id_str);
13055 free(uuidstr);
13056 return error;