Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
40 #include <util.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_diff.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_send.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
58 #include "got_dial.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static volatile sig_atomic_t sigint_received;
65 static volatile sig_atomic_t sigpipe_received;
67 static void
68 catch_sigint(int signo)
69 {
70 sigint_received = 1;
71 }
73 static void
74 catch_sigpipe(int signo)
75 {
76 sigpipe_received = 1;
77 }
80 struct got_cmd {
81 const char *cmd_name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 const char *cmd_alias;
85 };
87 __dead static void usage(int, int);
88 __dead static void usage_init(void);
89 __dead static void usage_import(void);
90 __dead static void usage_clone(void);
91 __dead static void usage_fetch(void);
92 __dead static void usage_checkout(void);
93 __dead static void usage_update(void);
94 __dead static void usage_log(void);
95 __dead static void usage_diff(void);
96 __dead static void usage_blame(void);
97 __dead static void usage_tree(void);
98 __dead static void usage_status(void);
99 __dead static void usage_ref(void);
100 __dead static void usage_branch(void);
101 __dead static void usage_tag(void);
102 __dead static void usage_add(void);
103 __dead static void usage_remove(void);
104 __dead static void usage_revert(void);
105 __dead static void usage_commit(void);
106 __dead static void usage_send(void);
107 __dead static void usage_cherrypick(void);
108 __dead static void usage_backout(void);
109 __dead static void usage_rebase(void);
110 __dead static void usage_histedit(void);
111 __dead static void usage_integrate(void);
112 __dead static void usage_merge(void);
113 __dead static void usage_stage(void);
114 __dead static void usage_unstage(void);
115 __dead static void usage_cat(void);
116 __dead static void usage_info(void);
118 static const struct got_error* cmd_init(int, char *[]);
119 static const struct got_error* cmd_import(int, char *[]);
120 static const struct got_error* cmd_clone(int, char *[]);
121 static const struct got_error* cmd_fetch(int, char *[]);
122 static const struct got_error* cmd_checkout(int, char *[]);
123 static const struct got_error* cmd_update(int, char *[]);
124 static const struct got_error* cmd_log(int, char *[]);
125 static const struct got_error* cmd_diff(int, char *[]);
126 static const struct got_error* cmd_blame(int, char *[]);
127 static const struct got_error* cmd_tree(int, char *[]);
128 static const struct got_error* cmd_status(int, char *[]);
129 static const struct got_error* cmd_ref(int, char *[]);
130 static const struct got_error* cmd_branch(int, char *[]);
131 static const struct got_error* cmd_tag(int, char *[]);
132 static const struct got_error* cmd_add(int, char *[]);
133 static const struct got_error* cmd_remove(int, char *[]);
134 static const struct got_error* cmd_revert(int, char *[]);
135 static const struct got_error* cmd_commit(int, char *[]);
136 static const struct got_error* cmd_send(int, char *[]);
137 static const struct got_error* cmd_cherrypick(int, char *[]);
138 static const struct got_error* cmd_backout(int, char *[]);
139 static const struct got_error* cmd_rebase(int, char *[]);
140 static const struct got_error* cmd_histedit(int, char *[]);
141 static const struct got_error* cmd_integrate(int, char *[]);
142 static const struct got_error* cmd_merge(int, char *[]);
143 static const struct got_error* cmd_stage(int, char *[]);
144 static const struct got_error* cmd_unstage(int, char *[]);
145 static const struct got_error* cmd_cat(int, char *[]);
146 static const struct got_error* cmd_info(int, char *[]);
148 static struct got_cmd got_commands[] = {
149 { "init", cmd_init, usage_init, "" },
150 { "import", cmd_import, usage_import, "im" },
151 { "clone", cmd_clone, usage_clone, "cl" },
152 { "fetch", cmd_fetch, usage_fetch, "fe" },
153 { "checkout", cmd_checkout, usage_checkout, "co" },
154 { "update", cmd_update, usage_update, "up" },
155 { "log", cmd_log, usage_log, "" },
156 { "diff", cmd_diff, usage_diff, "di" },
157 { "blame", cmd_blame, usage_blame, "bl" },
158 { "tree", cmd_tree, usage_tree, "tr" },
159 { "status", cmd_status, usage_status, "st" },
160 { "ref", cmd_ref, usage_ref, "" },
161 { "branch", cmd_branch, usage_branch, "br" },
162 { "tag", cmd_tag, usage_tag, "" },
163 { "add", cmd_add, usage_add, "" },
164 { "remove", cmd_remove, usage_remove, "rm" },
165 { "revert", cmd_revert, usage_revert, "rv" },
166 { "commit", cmd_commit, usage_commit, "ci" },
167 { "send", cmd_send, usage_send, "se" },
168 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
169 { "backout", cmd_backout, usage_backout, "bo" },
170 { "rebase", cmd_rebase, usage_rebase, "rb" },
171 { "histedit", cmd_histedit, usage_histedit, "he" },
172 { "integrate", cmd_integrate, usage_integrate,"ig" },
173 { "merge", cmd_merge, usage_merge, "mg" },
174 { "stage", cmd_stage, usage_stage, "sg" },
175 { "unstage", cmd_unstage, usage_unstage, "ug" },
176 { "cat", cmd_cat, usage_cat, "" },
177 { "info", cmd_info, usage_info, "" },
178 };
180 static void
181 list_commands(FILE *fp)
183 size_t i;
185 fprintf(fp, "commands:");
186 for (i = 0; i < nitems(got_commands); i++) {
187 struct got_cmd *cmd = &got_commands[i];
188 fprintf(fp, " %s", cmd->cmd_name);
190 fputc('\n', fp);
193 __dead static void
194 option_conflict(char a, char b)
196 errx(1, "-%c and -%c options are mutually exclusive", a, b);
199 int
200 main(int argc, char *argv[])
202 struct got_cmd *cmd;
203 size_t i;
204 int ch;
205 int hflag = 0, Vflag = 0;
206 static struct option longopts[] = {
207 { "version", no_argument, NULL, 'V' },
208 { NULL, 0, NULL, 0 }
209 };
211 setlocale(LC_CTYPE, "");
213 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
214 switch (ch) {
215 case 'h':
216 hflag = 1;
217 break;
218 case 'V':
219 Vflag = 1;
220 break;
221 default:
222 usage(hflag, 1);
223 /* NOTREACHED */
227 argc -= optind;
228 argv += optind;
229 optind = 1;
230 optreset = 1;
232 if (Vflag) {
233 got_version_print_str();
234 return 0;
237 if (argc <= 0)
238 usage(hflag, hflag ? 0 : 1);
240 signal(SIGINT, catch_sigint);
241 signal(SIGPIPE, catch_sigpipe);
243 for (i = 0; i < nitems(got_commands); i++) {
244 const struct got_error *error;
246 cmd = &got_commands[i];
248 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
249 strcmp(cmd->cmd_alias, argv[0]) != 0)
250 continue;
252 if (hflag)
253 got_commands[i].cmd_usage();
255 error = got_commands[i].cmd_main(argc, argv);
256 if (error && error->code != GOT_ERR_CANCELLED &&
257 error->code != GOT_ERR_PRIVSEP_EXIT &&
258 !(sigpipe_received &&
259 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
260 !(sigint_received &&
261 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
262 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
263 return 1;
266 return 0;
269 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
270 list_commands(stderr);
271 return 1;
274 __dead static void
275 usage(int hflag, int status)
277 FILE *fp = (status == 0) ? stdout : stderr;
279 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
280 getprogname());
281 if (hflag)
282 list_commands(fp);
283 exit(status);
286 static const struct got_error *
287 get_editor(char **abspath)
289 const struct got_error *err = NULL;
290 const char *editor;
292 *abspath = NULL;
294 editor = getenv("VISUAL");
295 if (editor == NULL)
296 editor = getenv("EDITOR");
298 if (editor) {
299 err = got_path_find_prog(abspath, editor);
300 if (err)
301 return err;
304 if (*abspath == NULL) {
305 *abspath = strdup("/bin/ed");
306 if (*abspath == NULL)
307 return got_error_from_errno("strdup");
310 return NULL;
313 static const struct got_error *
314 apply_unveil(const char *repo_path, int repo_read_only,
315 const char *worktree_path)
317 const struct got_error *err;
319 #ifdef PROFILE
320 if (unveil("gmon.out", "rwc") != 0)
321 return got_error_from_errno2("unveil", "gmon.out");
322 #endif
323 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
324 return got_error_from_errno2("unveil", repo_path);
326 if (worktree_path && unveil(worktree_path, "rwc") != 0)
327 return got_error_from_errno2("unveil", worktree_path);
329 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
330 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
332 err = got_privsep_unveil_exec_helpers();
333 if (err != NULL)
334 return err;
336 if (unveil(NULL, NULL) != 0)
337 return got_error_from_errno("unveil");
339 return NULL;
342 __dead static void
343 usage_init(void)
345 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
346 exit(1);
349 static const struct got_error *
350 cmd_init(int argc, char *argv[])
352 const struct got_error *error = NULL;
353 char *repo_path = NULL;
354 int ch;
356 while ((ch = getopt(argc, argv, "")) != -1) {
357 switch (ch) {
358 default:
359 usage_init();
360 /* NOTREACHED */
364 argc -= optind;
365 argv += optind;
367 #ifndef PROFILE
368 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
369 err(1, "pledge");
370 #endif
371 if (argc != 1)
372 usage_init();
374 repo_path = strdup(argv[0]);
375 if (repo_path == NULL)
376 return got_error_from_errno("strdup");
378 got_path_strip_trailing_slashes(repo_path);
380 error = got_path_mkdir(repo_path);
381 if (error &&
382 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
383 goto done;
385 error = apply_unveil(repo_path, 0, NULL);
386 if (error)
387 goto done;
389 error = got_repo_init(repo_path);
390 done:
391 free(repo_path);
392 return error;
395 __dead static void
396 usage_import(void)
398 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
399 "[-r repository-path] [-I pattern] path\n", getprogname());
400 exit(1);
403 int
404 spawn_editor(const char *editor, const char *file)
406 pid_t pid;
407 sig_t sighup, sigint, sigquit;
408 int st = -1;
410 sighup = signal(SIGHUP, SIG_IGN);
411 sigint = signal(SIGINT, SIG_IGN);
412 sigquit = signal(SIGQUIT, SIG_IGN);
414 switch (pid = fork()) {
415 case -1:
416 goto doneediting;
417 case 0:
418 execl(editor, editor, file, (char *)NULL);
419 _exit(127);
422 while (waitpid(pid, &st, 0) == -1)
423 if (errno != EINTR)
424 break;
426 doneediting:
427 (void)signal(SIGHUP, sighup);
428 (void)signal(SIGINT, sigint);
429 (void)signal(SIGQUIT, sigquit);
431 if (!WIFEXITED(st)) {
432 errno = EINTR;
433 return -1;
436 return WEXITSTATUS(st);
439 static const struct got_error *
440 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 const char *initial_content, size_t initial_content_len,
442 int require_modification)
444 const struct got_error *err = NULL;
445 char *line = NULL;
446 size_t linesize = 0;
447 ssize_t linelen;
448 struct stat st, st2;
449 FILE *fp = NULL;
450 size_t len, logmsg_len;
451 char *initial_content_stripped = NULL, *buf = NULL, *s;
453 *logmsg = NULL;
455 if (stat(logmsg_path, &st) == -1)
456 return got_error_from_errno2("stat", logmsg_path);
458 if (spawn_editor(editor, logmsg_path) == -1)
459 return got_error_from_errno("failed spawning editor");
461 if (stat(logmsg_path, &st2) == -1)
462 return got_error_from_errno("stat");
464 if (require_modification &&
465 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
466 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
467 "no changes made to commit message, aborting");
469 /*
470 * Set up a stripped version of the initial content without comments
471 * and blank lines. We need this in order to check if the message
472 * has in fact been edited.
473 */
474 initial_content_stripped = malloc(initial_content_len + 1);
475 if (initial_content_stripped == NULL)
476 return got_error_from_errno("malloc");
477 initial_content_stripped[0] = '\0';
479 buf = strdup(initial_content);
480 if (buf == NULL) {
481 err = got_error_from_errno("strdup");
482 goto done;
484 s = buf;
485 len = 0;
486 while ((line = strsep(&s, "\n")) != NULL) {
487 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
488 continue; /* remove comments and leading empty lines */
489 len = strlcat(initial_content_stripped, line,
490 initial_content_len + 1);
491 if (len >= initial_content_len + 1) {
492 err = got_error(GOT_ERR_NO_SPACE);
493 goto done;
496 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
497 initial_content_stripped[len - 1] = '\0';
498 len--;
501 logmsg_len = st2.st_size;
502 *logmsg = malloc(logmsg_len + 1);
503 if (*logmsg == NULL)
504 return got_error_from_errno("malloc");
505 (*logmsg)[0] = '\0';
507 fp = fopen(logmsg_path, "r");
508 if (fp == NULL) {
509 err = got_error_from_errno("fopen");
510 goto done;
513 len = 0;
514 while ((linelen = getline(&line, &linesize, fp)) != -1) {
515 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
516 continue; /* remove comments and leading empty lines */
517 len = strlcat(*logmsg, line, logmsg_len + 1);
518 if (len >= logmsg_len + 1) {
519 err = got_error(GOT_ERR_NO_SPACE);
520 goto done;
523 free(line);
524 if (ferror(fp)) {
525 err = got_ferror(fp, GOT_ERR_IO);
526 goto done;
528 while (len > 0 && (*logmsg)[len - 1] == '\n') {
529 (*logmsg)[len - 1] = '\0';
530 len--;
533 if (len == 0) {
534 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
535 "commit message cannot be empty, aborting");
536 goto done;
538 if (require_modification &&
539 strcmp(*logmsg, initial_content_stripped) == 0)
540 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
541 "no changes made to commit message, aborting");
542 done:
543 free(initial_content_stripped);
544 free(buf);
545 if (fp && fclose(fp) == EOF && err == NULL)
546 err = got_error_from_errno("fclose");
547 if (err) {
548 free(*logmsg);
549 *logmsg = NULL;
551 return err;
554 static const struct got_error *
555 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
556 const char *path_dir, const char *branch_name)
558 char *initial_content = NULL;
559 const struct got_error *err = NULL;
560 int initial_content_len;
561 int fd = -1;
563 initial_content_len = asprintf(&initial_content,
564 "\n# %s to be imported to branch %s\n", path_dir,
565 branch_name);
566 if (initial_content_len == -1)
567 return got_error_from_errno("asprintf");
569 err = got_opentemp_named_fd(logmsg_path, &fd,
570 GOT_TMPDIR_STR "/got-importmsg");
571 if (err)
572 goto done;
574 if (write(fd, initial_content, initial_content_len) == -1) {
575 err = got_error_from_errno2("write", *logmsg_path);
576 goto done;
579 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
580 initial_content_len, 1);
581 done:
582 if (fd != -1 && close(fd) == -1 && err == NULL)
583 err = got_error_from_errno2("close", *logmsg_path);
584 free(initial_content);
585 if (err) {
586 free(*logmsg_path);
587 *logmsg_path = NULL;
589 return err;
592 static const struct got_error *
593 import_progress(void *arg, const char *path)
595 printf("A %s\n", path);
596 return NULL;
599 static const struct got_error *
600 get_author(char **author, struct got_repository *repo,
601 struct got_worktree *worktree)
603 const struct got_error *err = NULL;
604 const char *got_author = NULL, *name, *email;
605 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
607 *author = NULL;
609 if (worktree)
610 worktree_conf = got_worktree_get_gotconfig(worktree);
611 repo_conf = got_repo_get_gotconfig(repo);
613 /*
614 * Priority of potential author information sources, from most
615 * significant to least significant:
616 * 1) work tree's .got/got.conf file
617 * 2) repository's got.conf file
618 * 3) repository's git config file
619 * 4) environment variables
620 * 5) global git config files (in user's home directory or /etc)
621 */
623 if (worktree_conf)
624 got_author = got_gotconfig_get_author(worktree_conf);
625 if (got_author == NULL)
626 got_author = got_gotconfig_get_author(repo_conf);
627 if (got_author == NULL) {
628 name = got_repo_get_gitconfig_author_name(repo);
629 email = got_repo_get_gitconfig_author_email(repo);
630 if (name && email) {
631 if (asprintf(author, "%s <%s>", name, email) == -1)
632 return got_error_from_errno("asprintf");
633 return NULL;
636 got_author = getenv("GOT_AUTHOR");
637 if (got_author == NULL) {
638 name = got_repo_get_global_gitconfig_author_name(repo);
639 email = got_repo_get_global_gitconfig_author_email(
640 repo);
641 if (name && email) {
642 if (asprintf(author, "%s <%s>", name, email)
643 == -1)
644 return got_error_from_errno("asprintf");
645 return NULL;
647 /* TODO: Look up user in password database? */
648 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
652 *author = strdup(got_author);
653 if (*author == NULL)
654 return got_error_from_errno("strdup");
656 /*
657 * Really dumb email address check; we're only doing this to
658 * avoid git's object parser breaking on commits we create.
659 */
660 while (*got_author && *got_author != '<')
661 got_author++;
662 if (*got_author != '<') {
663 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
664 goto done;
666 while (*got_author && *got_author != '@')
667 got_author++;
668 if (*got_author != '@') {
669 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
670 goto done;
672 while (*got_author && *got_author != '>')
673 got_author++;
674 if (*got_author != '>')
675 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
676 done:
677 if (err) {
678 free(*author);
679 *author = NULL;
681 return err;
684 static const struct got_error *
685 get_gitconfig_path(char **gitconfig_path)
687 const char *homedir = getenv("HOME");
689 *gitconfig_path = NULL;
690 if (homedir) {
691 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
692 return got_error_from_errno("asprintf");
695 return NULL;
698 static const struct got_error *
699 cmd_import(int argc, char *argv[])
701 const struct got_error *error = NULL;
702 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
703 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
704 const char *branch_name = "main";
705 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
706 struct got_repository *repo = NULL;
707 struct got_reference *branch_ref = NULL, *head_ref = NULL;
708 struct got_object_id *new_commit_id = NULL;
709 int ch;
710 struct got_pathlist_head ignores;
711 struct got_pathlist_entry *pe;
712 int preserve_logmsg = 0;
714 TAILQ_INIT(&ignores);
716 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
717 switch (ch) {
718 case 'b':
719 branch_name = optarg;
720 break;
721 case 'm':
722 logmsg = strdup(optarg);
723 if (logmsg == NULL) {
724 error = got_error_from_errno("strdup");
725 goto done;
727 break;
728 case 'r':
729 repo_path = realpath(optarg, NULL);
730 if (repo_path == NULL) {
731 error = got_error_from_errno2("realpath",
732 optarg);
733 goto done;
735 break;
736 case 'I':
737 if (optarg[0] == '\0')
738 break;
739 error = got_pathlist_insert(&pe, &ignores, optarg,
740 NULL);
741 if (error)
742 goto done;
743 break;
744 default:
745 usage_import();
746 /* NOTREACHED */
750 argc -= optind;
751 argv += optind;
753 #ifndef PROFILE
754 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
755 "unveil",
756 NULL) == -1)
757 err(1, "pledge");
758 #endif
759 if (argc != 1)
760 usage_import();
762 if (repo_path == NULL) {
763 repo_path = getcwd(NULL, 0);
764 if (repo_path == NULL)
765 return got_error_from_errno("getcwd");
767 got_path_strip_trailing_slashes(repo_path);
768 error = get_gitconfig_path(&gitconfig_path);
769 if (error)
770 goto done;
771 error = got_repo_open(&repo, repo_path, gitconfig_path);
772 if (error)
773 goto done;
775 error = get_author(&author, repo, NULL);
776 if (error)
777 return error;
779 /*
780 * Don't let the user create a branch name with a leading '-'.
781 * While technically a valid reference name, this case is usually
782 * an unintended typo.
783 */
784 if (branch_name[0] == '-')
785 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
787 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
788 error = got_error_from_errno("asprintf");
789 goto done;
792 error = got_ref_open(&branch_ref, repo, refname, 0);
793 if (error) {
794 if (error->code != GOT_ERR_NOT_REF)
795 goto done;
796 } else {
797 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
798 "import target branch already exists");
799 goto done;
802 path_dir = realpath(argv[0], NULL);
803 if (path_dir == NULL) {
804 error = got_error_from_errno2("realpath", argv[0]);
805 goto done;
807 got_path_strip_trailing_slashes(path_dir);
809 /*
810 * unveil(2) traverses exec(2); if an editor is used we have
811 * to apply unveil after the log message has been written.
812 */
813 if (logmsg == NULL || strlen(logmsg) == 0) {
814 error = get_editor(&editor);
815 if (error)
816 goto done;
817 free(logmsg);
818 error = collect_import_msg(&logmsg, &logmsg_path, editor,
819 path_dir, refname);
820 if (error) {
821 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
822 logmsg_path != NULL)
823 preserve_logmsg = 1;
824 goto done;
828 if (unveil(path_dir, "r") != 0) {
829 error = got_error_from_errno2("unveil", path_dir);
830 if (logmsg_path)
831 preserve_logmsg = 1;
832 goto done;
835 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
836 if (error) {
837 if (logmsg_path)
838 preserve_logmsg = 1;
839 goto done;
842 error = got_repo_import(&new_commit_id, path_dir, logmsg,
843 author, &ignores, repo, import_progress, NULL);
844 if (error) {
845 if (logmsg_path)
846 preserve_logmsg = 1;
847 goto done;
850 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
851 if (error) {
852 if (logmsg_path)
853 preserve_logmsg = 1;
854 goto done;
857 error = got_ref_write(branch_ref, repo);
858 if (error) {
859 if (logmsg_path)
860 preserve_logmsg = 1;
861 goto done;
864 error = got_object_id_str(&id_str, new_commit_id);
865 if (error) {
866 if (logmsg_path)
867 preserve_logmsg = 1;
868 goto done;
871 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
872 if (error) {
873 if (error->code != GOT_ERR_NOT_REF) {
874 if (logmsg_path)
875 preserve_logmsg = 1;
876 goto done;
879 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
880 branch_ref);
881 if (error) {
882 if (logmsg_path)
883 preserve_logmsg = 1;
884 goto done;
887 error = got_ref_write(head_ref, repo);
888 if (error) {
889 if (logmsg_path)
890 preserve_logmsg = 1;
891 goto done;
895 printf("Created branch %s with commit %s\n",
896 got_ref_get_name(branch_ref), id_str);
897 done:
898 if (preserve_logmsg) {
899 fprintf(stderr, "%s: log message preserved in %s\n",
900 getprogname(), logmsg_path);
901 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
902 error = got_error_from_errno2("unlink", logmsg_path);
903 free(logmsg);
904 free(logmsg_path);
905 free(repo_path);
906 free(editor);
907 free(refname);
908 free(new_commit_id);
909 free(id_str);
910 free(author);
911 free(gitconfig_path);
912 if (branch_ref)
913 got_ref_close(branch_ref);
914 if (head_ref)
915 got_ref_close(head_ref);
916 return error;
919 __dead static void
920 usage_clone(void)
922 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
923 "[-R reference] repository-url [directory]\n", getprogname());
924 exit(1);
927 struct got_fetch_progress_arg {
928 char last_scaled_size[FMT_SCALED_STRSIZE];
929 int last_p_indexed;
930 int last_p_resolved;
931 int verbosity;
933 struct got_repository *repo;
935 int create_configs;
936 int configs_created;
937 struct {
938 struct got_pathlist_head *symrefs;
939 struct got_pathlist_head *wanted_branches;
940 struct got_pathlist_head *wanted_refs;
941 const char *proto;
942 const char *host;
943 const char *port;
944 const char *remote_repo_path;
945 const char *git_url;
946 int fetch_all_branches;
947 int mirror_references;
948 } config_info;
949 };
951 /* XXX forward declaration */
952 static const struct got_error *
953 create_config_files(const char *proto, const char *host, const char *port,
954 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
955 int mirror_references, struct got_pathlist_head *symrefs,
956 struct got_pathlist_head *wanted_branches,
957 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
959 static const struct got_error *
960 fetch_progress(void *arg, const char *message, off_t packfile_size,
961 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
963 const struct got_error *err = NULL;
964 struct got_fetch_progress_arg *a = arg;
965 char scaled_size[FMT_SCALED_STRSIZE];
966 int p_indexed, p_resolved;
967 int print_size = 0, print_indexed = 0, print_resolved = 0;
969 /*
970 * In order to allow a failed clone to be resumed with 'got fetch'
971 * we try to create configuration files as soon as possible.
972 * Once the server has sent information about its default branch
973 * we have all required information.
974 */
975 if (a->create_configs && !a->configs_created &&
976 !TAILQ_EMPTY(a->config_info.symrefs)) {
977 err = create_config_files(a->config_info.proto,
978 a->config_info.host, a->config_info.port,
979 a->config_info.remote_repo_path,
980 a->config_info.git_url,
981 a->config_info.fetch_all_branches,
982 a->config_info.mirror_references,
983 a->config_info.symrefs,
984 a->config_info.wanted_branches,
985 a->config_info.wanted_refs, a->repo);
986 if (err)
987 return err;
988 a->configs_created = 1;
991 if (a->verbosity < 0)
992 return NULL;
994 if (message && message[0] != '\0') {
995 printf("\rserver: %s", message);
996 fflush(stdout);
997 return NULL;
1000 if (packfile_size > 0 || nobj_indexed > 0) {
1001 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1002 (a->last_scaled_size[0] == '\0' ||
1003 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1004 print_size = 1;
1005 if (strlcpy(a->last_scaled_size, scaled_size,
1006 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1007 return got_error(GOT_ERR_NO_SPACE);
1009 if (nobj_indexed > 0) {
1010 p_indexed = (nobj_indexed * 100) / nobj_total;
1011 if (p_indexed != a->last_p_indexed) {
1012 a->last_p_indexed = p_indexed;
1013 print_indexed = 1;
1014 print_size = 1;
1017 if (nobj_resolved > 0) {
1018 p_resolved = (nobj_resolved * 100) /
1019 (nobj_total - nobj_loose);
1020 if (p_resolved != a->last_p_resolved) {
1021 a->last_p_resolved = p_resolved;
1022 print_resolved = 1;
1023 print_indexed = 1;
1024 print_size = 1;
1029 if (print_size || print_indexed || print_resolved)
1030 printf("\r");
1031 if (print_size)
1032 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1033 if (print_indexed)
1034 printf("; indexing %d%%", p_indexed);
1035 if (print_resolved)
1036 printf("; resolving deltas %d%%", p_resolved);
1037 if (print_size || print_indexed || print_resolved)
1038 fflush(stdout);
1040 return NULL;
1043 static const struct got_error *
1044 create_symref(const char *refname, struct got_reference *target_ref,
1045 int verbosity, struct got_repository *repo)
1047 const struct got_error *err;
1048 struct got_reference *head_symref;
1050 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1051 if (err)
1052 return err;
1054 err = got_ref_write(head_symref, repo);
1055 if (err == NULL && verbosity > 0) {
1056 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1057 got_ref_get_name(target_ref));
1059 got_ref_close(head_symref);
1060 return err;
1063 static const struct got_error *
1064 list_remote_refs(struct got_pathlist_head *symrefs,
1065 struct got_pathlist_head *refs)
1067 const struct got_error *err;
1068 struct got_pathlist_entry *pe;
1070 TAILQ_FOREACH(pe, symrefs, entry) {
1071 const char *refname = pe->path;
1072 const char *targetref = pe->data;
1074 printf("%s: %s\n", refname, targetref);
1077 TAILQ_FOREACH(pe, refs, entry) {
1078 const char *refname = pe->path;
1079 struct got_object_id *id = pe->data;
1080 char *id_str;
1082 err = got_object_id_str(&id_str, id);
1083 if (err)
1084 return err;
1085 printf("%s: %s\n", refname, id_str);
1086 free(id_str);
1089 return NULL;
1092 static const struct got_error *
1093 create_ref(const char *refname, struct got_object_id *id,
1094 int verbosity, struct got_repository *repo)
1096 const struct got_error *err = NULL;
1097 struct got_reference *ref;
1098 char *id_str;
1100 err = got_object_id_str(&id_str, id);
1101 if (err)
1102 return err;
1104 err = got_ref_alloc(&ref, refname, id);
1105 if (err)
1106 goto done;
1108 err = got_ref_write(ref, repo);
1109 got_ref_close(ref);
1111 if (err == NULL && verbosity >= 0)
1112 printf("Created reference %s: %s\n", refname, id_str);
1113 done:
1114 free(id_str);
1115 return err;
1118 static int
1119 match_wanted_ref(const char *refname, const char *wanted_ref)
1121 if (strncmp(refname, "refs/", 5) != 0)
1122 return 0;
1123 refname += 5;
1126 * Prevent fetching of references that won't make any
1127 * sense outside of the remote repository's context.
1129 if (strncmp(refname, "got/", 4) == 0)
1130 return 0;
1131 if (strncmp(refname, "remotes/", 8) == 0)
1132 return 0;
1134 if (strncmp(wanted_ref, "refs/", 5) == 0)
1135 wanted_ref += 5;
1137 /* Allow prefix match. */
1138 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1139 return 1;
1141 /* Allow exact match. */
1142 return (strcmp(refname, wanted_ref) == 0);
1145 static int
1146 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1148 struct got_pathlist_entry *pe;
1150 TAILQ_FOREACH(pe, wanted_refs, entry) {
1151 if (match_wanted_ref(refname, pe->path))
1152 return 1;
1155 return 0;
1158 static const struct got_error *
1159 create_wanted_ref(const char *refname, struct got_object_id *id,
1160 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1162 const struct got_error *err;
1163 char *remote_refname;
1165 if (strncmp("refs/", refname, 5) == 0)
1166 refname += 5;
1168 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1169 remote_repo_name, refname) == -1)
1170 return got_error_from_errno("asprintf");
1172 err = create_ref(remote_refname, id, verbosity, repo);
1173 free(remote_refname);
1174 return err;
1177 static const struct got_error *
1178 create_gotconfig(const char *proto, const char *host, const char *port,
1179 const char *remote_repo_path, const char *default_branch,
1180 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1181 struct got_pathlist_head *wanted_refs, int mirror_references,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 char *gotconfig_path = NULL;
1186 char *gotconfig = NULL;
1187 FILE *gotconfig_file = NULL;
1188 const char *branchname = NULL;
1189 char *branches = NULL, *refs = NULL;
1190 ssize_t n;
1192 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1193 struct got_pathlist_entry *pe;
1194 TAILQ_FOREACH(pe, wanted_branches, entry) {
1195 char *s;
1196 branchname = pe->path;
1197 if (strncmp(branchname, "refs/heads/", 11) == 0)
1198 branchname += 11;
1199 if (asprintf(&s, "%s\"%s\" ",
1200 branches ? branches : "", branchname) == -1) {
1201 err = got_error_from_errno("asprintf");
1202 goto done;
1204 free(branches);
1205 branches = s;
1207 } else if (!fetch_all_branches && default_branch) {
1208 branchname = default_branch;
1209 if (strncmp(branchname, "refs/heads/", 11) == 0)
1210 branchname += 11;
1211 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1212 err = got_error_from_errno("asprintf");
1213 goto done;
1216 if (!TAILQ_EMPTY(wanted_refs)) {
1217 struct got_pathlist_entry *pe;
1218 TAILQ_FOREACH(pe, wanted_refs, entry) {
1219 char *s;
1220 const char *refname = pe->path;
1221 if (strncmp(refname, "refs/", 5) == 0)
1222 branchname += 5;
1223 if (asprintf(&s, "%s\"%s\" ",
1224 refs ? refs : "", refname) == -1) {
1225 err = got_error_from_errno("asprintf");
1226 goto done;
1228 free(refs);
1229 refs = s;
1233 /* Create got.conf(5). */
1234 gotconfig_path = got_repo_get_path_gotconfig(repo);
1235 if (gotconfig_path == NULL) {
1236 err = got_error_from_errno("got_repo_get_path_gotconfig");
1237 goto done;
1239 gotconfig_file = fopen(gotconfig_path, "a");
1240 if (gotconfig_file == NULL) {
1241 err = got_error_from_errno2("fopen", gotconfig_path);
1242 goto done;
1244 if (asprintf(&gotconfig,
1245 "remote \"%s\" {\n"
1246 "\tserver %s\n"
1247 "\tprotocol %s\n"
1248 "%s%s%s"
1249 "\trepository \"%s\"\n"
1250 "%s%s%s"
1251 "%s%s%s"
1252 "%s"
1253 "%s"
1254 "}\n",
1255 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1256 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1257 remote_repo_path, branches ? "\tbranch { " : "",
1258 branches ? branches : "", branches ? "}\n" : "",
1259 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1260 mirror_references ? "\tmirror-references yes\n" : "",
1261 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1262 err = got_error_from_errno("asprintf");
1263 goto done;
1265 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1266 if (n != strlen(gotconfig)) {
1267 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1268 goto done;
1271 done:
1272 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1273 err = got_error_from_errno2("fclose", gotconfig_path);
1274 free(gotconfig_path);
1275 free(branches);
1276 return err;
1279 static const struct got_error *
1280 create_gitconfig(const char *git_url, const char *default_branch,
1281 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1282 struct got_pathlist_head *wanted_refs, int mirror_references,
1283 struct got_repository *repo)
1285 const struct got_error *err = NULL;
1286 char *gitconfig_path = NULL;
1287 char *gitconfig = NULL;
1288 FILE *gitconfig_file = NULL;
1289 char *branches = NULL, *refs = NULL;
1290 const char *branchname;
1291 ssize_t n;
1293 /* Create a config file Git can understand. */
1294 gitconfig_path = got_repo_get_path_gitconfig(repo);
1295 if (gitconfig_path == NULL) {
1296 err = got_error_from_errno("got_repo_get_path_gitconfig");
1297 goto done;
1299 gitconfig_file = fopen(gitconfig_path, "a");
1300 if (gitconfig_file == NULL) {
1301 err = got_error_from_errno2("fopen", gitconfig_path);
1302 goto done;
1304 if (fetch_all_branches) {
1305 if (mirror_references) {
1306 if (asprintf(&branches,
1307 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1308 err = got_error_from_errno("asprintf");
1309 goto done;
1311 } else if (asprintf(&branches,
1312 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1313 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1314 err = got_error_from_errno("asprintf");
1315 goto done;
1317 } else if (!TAILQ_EMPTY(wanted_branches)) {
1318 struct got_pathlist_entry *pe;
1319 TAILQ_FOREACH(pe, wanted_branches, entry) {
1320 char *s;
1321 branchname = pe->path;
1322 if (strncmp(branchname, "refs/heads/", 11) == 0)
1323 branchname += 11;
1324 if (mirror_references) {
1325 if (asprintf(&s,
1326 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1327 branches ? branches : "",
1328 branchname, branchname) == -1) {
1329 err = got_error_from_errno("asprintf");
1330 goto done;
1332 } else if (asprintf(&s,
1333 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1334 branches ? branches : "",
1335 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1336 branchname) == -1) {
1337 err = got_error_from_errno("asprintf");
1338 goto done;
1340 free(branches);
1341 branches = s;
1343 } else {
1345 * If the server specified a default branch, use just that one.
1346 * Otherwise fall back to fetching all branches on next fetch.
1348 if (default_branch) {
1349 branchname = default_branch;
1350 if (strncmp(branchname, "refs/heads/", 11) == 0)
1351 branchname += 11;
1352 } else
1353 branchname = "*"; /* fall back to all branches */
1354 if (mirror_references) {
1355 if (asprintf(&branches,
1356 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1357 branchname, branchname) == -1) {
1358 err = got_error_from_errno("asprintf");
1359 goto done;
1361 } else if (asprintf(&branches,
1362 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1363 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1364 branchname) == -1) {
1365 err = got_error_from_errno("asprintf");
1366 goto done;
1369 if (!TAILQ_EMPTY(wanted_refs)) {
1370 struct got_pathlist_entry *pe;
1371 TAILQ_FOREACH(pe, wanted_refs, entry) {
1372 char *s;
1373 const char *refname = pe->path;
1374 if (strncmp(refname, "refs/", 5) == 0)
1375 refname += 5;
1376 if (mirror_references) {
1377 if (asprintf(&s,
1378 "%s\tfetch = refs/%s:refs/%s\n",
1379 refs ? refs : "", refname, refname) == -1) {
1380 err = got_error_from_errno("asprintf");
1381 goto done;
1383 } else if (asprintf(&s,
1384 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1385 refs ? refs : "",
1386 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1387 refname) == -1) {
1388 err = got_error_from_errno("asprintf");
1389 goto done;
1391 free(refs);
1392 refs = s;
1396 if (asprintf(&gitconfig,
1397 "[remote \"%s\"]\n"
1398 "\turl = %s\n"
1399 "%s"
1400 "%s"
1401 "\tfetch = refs/tags/*:refs/tags/*\n",
1402 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1403 refs ? refs : "") == -1) {
1404 err = got_error_from_errno("asprintf");
1405 goto done;
1407 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1408 if (n != strlen(gitconfig)) {
1409 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1410 goto done;
1412 done:
1413 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1414 err = got_error_from_errno2("fclose", gitconfig_path);
1415 free(gitconfig_path);
1416 free(branches);
1417 return err;
1420 static const struct got_error *
1421 create_config_files(const char *proto, const char *host, const char *port,
1422 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1423 int mirror_references, struct got_pathlist_head *symrefs,
1424 struct got_pathlist_head *wanted_branches,
1425 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1427 const struct got_error *err = NULL;
1428 const char *default_branch = NULL;
1429 struct got_pathlist_entry *pe;
1432 * If we asked for a set of wanted branches then use the first
1433 * one of those.
1435 if (!TAILQ_EMPTY(wanted_branches)) {
1436 pe = TAILQ_FIRST(wanted_branches);
1437 default_branch = pe->path;
1438 } else {
1439 /* First HEAD ref listed by server is the default branch. */
1440 TAILQ_FOREACH(pe, symrefs, entry) {
1441 const char *refname = pe->path;
1442 const char *target = pe->data;
1444 if (strcmp(refname, GOT_REF_HEAD) != 0)
1445 continue;
1447 default_branch = target;
1448 break;
1452 /* Create got.conf(5). */
1453 err = create_gotconfig(proto, host, port, remote_repo_path,
1454 default_branch, fetch_all_branches, wanted_branches,
1455 wanted_refs, mirror_references, repo);
1456 if (err)
1457 return err;
1459 /* Create a config file Git can understand. */
1460 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1461 wanted_branches, wanted_refs, mirror_references, repo);
1464 static const struct got_error *
1465 cmd_clone(int argc, char *argv[])
1467 const struct got_error *error = NULL;
1468 const char *uri, *dirname;
1469 char *proto, *host, *port, *repo_name, *server_path;
1470 char *default_destdir = NULL, *id_str = NULL;
1471 const char *repo_path;
1472 struct got_repository *repo = NULL;
1473 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1474 struct got_pathlist_entry *pe;
1475 struct got_object_id *pack_hash = NULL;
1476 int ch, fetchfd = -1, fetchstatus;
1477 pid_t fetchpid = -1;
1478 struct got_fetch_progress_arg fpa;
1479 char *git_url = NULL;
1480 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1481 int list_refs_only = 0;
1483 TAILQ_INIT(&refs);
1484 TAILQ_INIT(&symrefs);
1485 TAILQ_INIT(&wanted_branches);
1486 TAILQ_INIT(&wanted_refs);
1488 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1489 switch (ch) {
1490 case 'a':
1491 fetch_all_branches = 1;
1492 break;
1493 case 'b':
1494 error = got_pathlist_append(&wanted_branches,
1495 optarg, NULL);
1496 if (error)
1497 return error;
1498 break;
1499 case 'l':
1500 list_refs_only = 1;
1501 break;
1502 case 'm':
1503 mirror_references = 1;
1504 break;
1505 case 'v':
1506 if (verbosity < 0)
1507 verbosity = 0;
1508 else if (verbosity < 3)
1509 verbosity++;
1510 break;
1511 case 'q':
1512 verbosity = -1;
1513 break;
1514 case 'R':
1515 error = got_pathlist_append(&wanted_refs,
1516 optarg, NULL);
1517 if (error)
1518 return error;
1519 break;
1520 default:
1521 usage_clone();
1522 break;
1525 argc -= optind;
1526 argv += optind;
1528 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1529 option_conflict('a', 'b');
1530 if (list_refs_only) {
1531 if (!TAILQ_EMPTY(&wanted_branches))
1532 option_conflict('l', 'b');
1533 if (fetch_all_branches)
1534 option_conflict('l', 'a');
1535 if (mirror_references)
1536 option_conflict('l', 'm');
1537 if (!TAILQ_EMPTY(&wanted_refs))
1538 option_conflict('l', 'R');
1541 uri = argv[0];
1543 if (argc == 1)
1544 dirname = NULL;
1545 else if (argc == 2)
1546 dirname = argv[1];
1547 else
1548 usage_clone();
1550 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1551 &repo_name, uri);
1552 if (error)
1553 goto done;
1555 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1556 host, port ? ":" : "", port ? port : "",
1557 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1558 error = got_error_from_errno("asprintf");
1559 goto done;
1562 if (strcmp(proto, "git") == 0) {
1563 #ifndef PROFILE
1564 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 "sendfd dns inet unveil", NULL) == -1)
1566 err(1, "pledge");
1567 #endif
1568 } else if (strcmp(proto, "git+ssh") == 0 ||
1569 strcmp(proto, "ssh") == 0) {
1570 #ifndef PROFILE
1571 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1572 "sendfd unveil", NULL) == -1)
1573 err(1, "pledge");
1574 #endif
1575 } else if (strcmp(proto, "http") == 0 ||
1576 strcmp(proto, "git+http") == 0) {
1577 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1578 goto done;
1579 } else {
1580 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1581 goto done;
1583 if (dirname == NULL) {
1584 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1585 error = got_error_from_errno("asprintf");
1586 goto done;
1588 repo_path = default_destdir;
1589 } else
1590 repo_path = dirname;
1592 if (!list_refs_only) {
1593 error = got_path_mkdir(repo_path);
1594 if (error &&
1595 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1596 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1597 goto done;
1598 if (!got_path_dir_is_empty(repo_path)) {
1599 error = got_error_path(repo_path,
1600 GOT_ERR_DIR_NOT_EMPTY);
1601 goto done;
1605 error = got_dial_apply_unveil(proto);
1606 if (error)
1607 goto done;
1609 error = apply_unveil(repo_path, 0, NULL);
1610 if (error)
1611 goto done;
1613 if (verbosity >= 0)
1614 printf("Connecting to %s%s%s\n", host,
1615 port ? ":" : "", port ? port : "");
1617 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 server_path, verbosity);
1619 if (error)
1620 goto done;
1622 if (!list_refs_only) {
1623 error = got_repo_init(repo_path);
1624 if (error)
1625 goto done;
1626 error = got_repo_open(&repo, repo_path, NULL);
1627 if (error)
1628 goto done;
1631 fpa.last_scaled_size[0] = '\0';
1632 fpa.last_p_indexed = -1;
1633 fpa.last_p_resolved = -1;
1634 fpa.verbosity = verbosity;
1635 fpa.create_configs = 1;
1636 fpa.configs_created = 0;
1637 fpa.repo = repo;
1638 fpa.config_info.symrefs = &symrefs;
1639 fpa.config_info.wanted_branches = &wanted_branches;
1640 fpa.config_info.wanted_refs = &wanted_refs;
1641 fpa.config_info.proto = proto;
1642 fpa.config_info.host = host;
1643 fpa.config_info.port = port;
1644 fpa.config_info.remote_repo_path = server_path;
1645 fpa.config_info.git_url = git_url;
1646 fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 fpa.config_info.mirror_references = mirror_references;
1648 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 fetch_all_branches, &wanted_branches, &wanted_refs,
1651 list_refs_only, verbosity, fetchfd, repo,
1652 fetch_progress, &fpa);
1653 if (error)
1654 goto done;
1656 if (list_refs_only) {
1657 error = list_remote_refs(&symrefs, &refs);
1658 goto done;
1661 error = got_object_id_str(&id_str, pack_hash);
1662 if (error)
1663 goto done;
1664 if (verbosity >= 0)
1665 printf("\nFetched %s.pack\n", id_str);
1666 free(id_str);
1668 /* Set up references provided with the pack file. */
1669 TAILQ_FOREACH(pe, &refs, entry) {
1670 const char *refname = pe->path;
1671 struct got_object_id *id = pe->data;
1672 char *remote_refname;
1674 if (is_wanted_ref(&wanted_refs, refname) &&
1675 !mirror_references) {
1676 error = create_wanted_ref(refname, id,
1677 GOT_FETCH_DEFAULT_REMOTE_NAME,
1678 verbosity - 1, repo);
1679 if (error)
1680 goto done;
1681 continue;
1684 error = create_ref(refname, id, verbosity - 1, repo);
1685 if (error)
1686 goto done;
1688 if (mirror_references)
1689 continue;
1691 if (strncmp("refs/heads/", refname, 11) != 0)
1692 continue;
1694 if (asprintf(&remote_refname,
1695 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1696 refname + 11) == -1) {
1697 error = got_error_from_errno("asprintf");
1698 goto done;
1700 error = create_ref(remote_refname, id, verbosity - 1, repo);
1701 free(remote_refname);
1702 if (error)
1703 goto done;
1706 /* Set the HEAD reference if the server provided one. */
1707 TAILQ_FOREACH(pe, &symrefs, entry) {
1708 struct got_reference *target_ref;
1709 const char *refname = pe->path;
1710 const char *target = pe->data;
1711 char *remote_refname = NULL, *remote_target = NULL;
1713 if (strcmp(refname, GOT_REF_HEAD) != 0)
1714 continue;
1716 error = got_ref_open(&target_ref, repo, target, 0);
1717 if (error) {
1718 if (error->code == GOT_ERR_NOT_REF) {
1719 error = NULL;
1720 continue;
1722 goto done;
1725 error = create_symref(refname, target_ref, verbosity, repo);
1726 got_ref_close(target_ref);
1727 if (error)
1728 goto done;
1730 if (mirror_references)
1731 continue;
1733 if (strncmp("refs/heads/", target, 11) != 0)
1734 continue;
1736 if (asprintf(&remote_refname,
1737 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1738 refname) == -1) {
1739 error = got_error_from_errno("asprintf");
1740 goto done;
1742 if (asprintf(&remote_target,
1743 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 target + 11) == -1) {
1745 error = got_error_from_errno("asprintf");
1746 free(remote_refname);
1747 goto done;
1749 error = got_ref_open(&target_ref, repo, remote_target, 0);
1750 if (error) {
1751 free(remote_refname);
1752 free(remote_target);
1753 if (error->code == GOT_ERR_NOT_REF) {
1754 error = NULL;
1755 continue;
1757 goto done;
1759 error = create_symref(remote_refname, target_ref,
1760 verbosity - 1, repo);
1761 free(remote_refname);
1762 free(remote_target);
1763 got_ref_close(target_ref);
1764 if (error)
1765 goto done;
1767 if (pe == NULL) {
1769 * We failed to set the HEAD reference. If we asked for
1770 * a set of wanted branches use the first of one of those
1771 * which could be fetched instead.
1773 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1774 const char *target = pe->path;
1775 struct got_reference *target_ref;
1777 error = got_ref_open(&target_ref, repo, target, 0);
1778 if (error) {
1779 if (error->code == GOT_ERR_NOT_REF) {
1780 error = NULL;
1781 continue;
1783 goto done;
1786 error = create_symref(GOT_REF_HEAD, target_ref,
1787 verbosity, repo);
1788 got_ref_close(target_ref);
1789 if (error)
1790 goto done;
1791 break;
1795 if (verbosity >= 0)
1796 printf("Created %s repository '%s'\n",
1797 mirror_references ? "mirrored" : "cloned", repo_path);
1798 done:
1799 if (fetchpid > 0) {
1800 if (kill(fetchpid, SIGTERM) == -1)
1801 error = got_error_from_errno("kill");
1802 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1803 error = got_error_from_errno("waitpid");
1805 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1806 error = got_error_from_errno("close");
1807 if (repo) {
1808 const struct got_error *close_err = got_repo_close(repo);
1809 if (error == NULL)
1810 error = close_err;
1812 TAILQ_FOREACH(pe, &refs, entry) {
1813 free((void *)pe->path);
1814 free(pe->data);
1816 got_pathlist_free(&refs);
1817 TAILQ_FOREACH(pe, &symrefs, entry) {
1818 free((void *)pe->path);
1819 free(pe->data);
1821 got_pathlist_free(&symrefs);
1822 got_pathlist_free(&wanted_branches);
1823 got_pathlist_free(&wanted_refs);
1824 free(pack_hash);
1825 free(proto);
1826 free(host);
1827 free(port);
1828 free(server_path);
1829 free(repo_name);
1830 free(default_destdir);
1831 free(git_url);
1832 return error;
1835 static const struct got_error *
1836 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1837 int replace_tags, int verbosity, struct got_repository *repo)
1839 const struct got_error *err = NULL;
1840 char *new_id_str = NULL;
1841 struct got_object_id *old_id = NULL;
1843 err = got_object_id_str(&new_id_str, new_id);
1844 if (err)
1845 goto done;
1847 if (!replace_tags &&
1848 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1849 err = got_ref_resolve(&old_id, repo, ref);
1850 if (err)
1851 goto done;
1852 if (got_object_id_cmp(old_id, new_id) == 0)
1853 goto done;
1854 if (verbosity >= 0) {
1855 printf("Rejecting update of existing tag %s: %s\n",
1856 got_ref_get_name(ref), new_id_str);
1858 goto done;
1861 if (got_ref_is_symbolic(ref)) {
1862 if (verbosity >= 0) {
1863 printf("Replacing reference %s: %s\n",
1864 got_ref_get_name(ref),
1865 got_ref_get_symref_target(ref));
1867 err = got_ref_change_symref_to_ref(ref, new_id);
1868 if (err)
1869 goto done;
1870 err = got_ref_write(ref, repo);
1871 if (err)
1872 goto done;
1873 } else {
1874 err = got_ref_resolve(&old_id, repo, ref);
1875 if (err)
1876 goto done;
1877 if (got_object_id_cmp(old_id, new_id) == 0)
1878 goto done;
1880 err = got_ref_change_ref(ref, new_id);
1881 if (err)
1882 goto done;
1883 err = got_ref_write(ref, repo);
1884 if (err)
1885 goto done;
1888 if (verbosity >= 0)
1889 printf("Updated %s: %s\n", got_ref_get_name(ref),
1890 new_id_str);
1891 done:
1892 free(old_id);
1893 free(new_id_str);
1894 return err;
1897 static const struct got_error *
1898 update_symref(const char *refname, struct got_reference *target_ref,
1899 int verbosity, struct got_repository *repo)
1901 const struct got_error *err = NULL, *unlock_err;
1902 struct got_reference *symref;
1903 int symref_is_locked = 0;
1905 err = got_ref_open(&symref, repo, refname, 1);
1906 if (err) {
1907 if (err->code != GOT_ERR_NOT_REF)
1908 return err;
1909 err = got_ref_alloc_symref(&symref, refname, target_ref);
1910 if (err)
1911 goto done;
1913 err = got_ref_write(symref, repo);
1914 if (err)
1915 goto done;
1917 if (verbosity >= 0)
1918 printf("Created reference %s: %s\n",
1919 got_ref_get_name(symref),
1920 got_ref_get_symref_target(symref));
1921 } else {
1922 symref_is_locked = 1;
1924 if (strcmp(got_ref_get_symref_target(symref),
1925 got_ref_get_name(target_ref)) == 0)
1926 goto done;
1928 err = got_ref_change_symref(symref,
1929 got_ref_get_name(target_ref));
1930 if (err)
1931 goto done;
1933 err = got_ref_write(symref, repo);
1934 if (err)
1935 goto done;
1937 if (verbosity >= 0)
1938 printf("Updated %s: %s\n", got_ref_get_name(symref),
1939 got_ref_get_symref_target(symref));
1942 done:
1943 if (symref_is_locked) {
1944 unlock_err = got_ref_unlock(symref);
1945 if (unlock_err && err == NULL)
1946 err = unlock_err;
1948 got_ref_close(symref);
1949 return err;
1952 __dead static void
1953 usage_fetch(void)
1955 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 "[remote-repository-name]\n",
1958 getprogname());
1959 exit(1);
1962 static const struct got_error *
1963 delete_missing_ref(struct got_reference *ref,
1964 int verbosity, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_object_id *id = NULL;
1968 char *id_str = NULL;
1970 if (got_ref_is_symbolic(ref)) {
1971 err = got_ref_delete(ref, repo);
1972 if (err)
1973 return err;
1974 if (verbosity >= 0) {
1975 printf("Deleted %s: %s\n",
1976 got_ref_get_name(ref),
1977 got_ref_get_symref_target(ref));
1979 } else {
1980 err = got_ref_resolve(&id, repo, ref);
1981 if (err)
1982 return err;
1983 err = got_object_id_str(&id_str, id);
1984 if (err)
1985 goto done;
1987 err = got_ref_delete(ref, repo);
1988 if (err)
1989 goto done;
1990 if (verbosity >= 0) {
1991 printf("Deleted %s: %s\n",
1992 got_ref_get_name(ref), id_str);
1995 done:
1996 free(id);
1997 free(id_str);
1998 return NULL;
2001 static const struct got_error *
2002 delete_missing_refs(struct got_pathlist_head *their_refs,
2003 struct got_pathlist_head *their_symrefs,
2004 const struct got_remote_repo *remote,
2005 int verbosity, struct got_repository *repo)
2007 const struct got_error *err = NULL, *unlock_err;
2008 struct got_reflist_head my_refs;
2009 struct got_reflist_entry *re;
2010 struct got_pathlist_entry *pe;
2011 char *remote_namespace = NULL;
2012 char *local_refname = NULL;
2014 TAILQ_INIT(&my_refs);
2016 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2017 == -1)
2018 return got_error_from_errno("asprintf");
2020 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2021 if (err)
2022 goto done;
2024 TAILQ_FOREACH(re, &my_refs, entry) {
2025 const char *refname = got_ref_get_name(re->ref);
2026 const char *their_refname;
2028 if (remote->mirror_references) {
2029 their_refname = refname;
2030 } else {
2031 if (strncmp(refname, remote_namespace,
2032 strlen(remote_namespace)) == 0) {
2033 if (strcmp(refname + strlen(remote_namespace),
2034 GOT_REF_HEAD) == 0)
2035 continue;
2036 if (asprintf(&local_refname, "refs/heads/%s",
2037 refname + strlen(remote_namespace)) == -1) {
2038 err = got_error_from_errno("asprintf");
2039 goto done;
2041 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2042 continue;
2044 their_refname = local_refname;
2047 TAILQ_FOREACH(pe, their_refs, entry) {
2048 if (strcmp(their_refname, pe->path) == 0)
2049 break;
2051 if (pe != NULL)
2052 continue;
2054 TAILQ_FOREACH(pe, their_symrefs, entry) {
2055 if (strcmp(their_refname, pe->path) == 0)
2056 break;
2058 if (pe != NULL)
2059 continue;
2061 err = delete_missing_ref(re->ref, verbosity, repo);
2062 if (err)
2063 break;
2065 if (local_refname) {
2066 struct got_reference *ref;
2067 err = got_ref_open(&ref, repo, local_refname, 1);
2068 if (err) {
2069 if (err->code != GOT_ERR_NOT_REF)
2070 break;
2071 free(local_refname);
2072 local_refname = NULL;
2073 continue;
2075 err = delete_missing_ref(ref, verbosity, repo);
2076 if (err)
2077 break;
2078 unlock_err = got_ref_unlock(ref);
2079 got_ref_close(ref);
2080 if (unlock_err && err == NULL) {
2081 err = unlock_err;
2082 break;
2085 free(local_refname);
2086 local_refname = NULL;
2089 done:
2090 free(remote_namespace);
2091 free(local_refname);
2092 return err;
2095 static const struct got_error *
2096 update_wanted_ref(const char *refname, struct got_object_id *id,
2097 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2099 const struct got_error *err, *unlock_err;
2100 char *remote_refname;
2101 struct got_reference *ref;
2103 if (strncmp("refs/", refname, 5) == 0)
2104 refname += 5;
2106 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2107 remote_repo_name, refname) == -1)
2108 return got_error_from_errno("asprintf");
2110 err = got_ref_open(&ref, repo, remote_refname, 1);
2111 if (err) {
2112 if (err->code != GOT_ERR_NOT_REF)
2113 goto done;
2114 err = create_ref(remote_refname, id, verbosity, repo);
2115 } else {
2116 err = update_ref(ref, id, 0, verbosity, repo);
2117 unlock_err = got_ref_unlock(ref);
2118 if (unlock_err && err == NULL)
2119 err = unlock_err;
2120 got_ref_close(ref);
2122 done:
2123 free(remote_refname);
2124 return err;
2127 static const struct got_error *
2128 delete_ref(struct got_repository *repo, struct got_reference *ref)
2130 const struct got_error *err = NULL;
2131 struct got_object_id *id = NULL;
2132 char *id_str = NULL;
2133 const char *target;
2135 if (got_ref_is_symbolic(ref)) {
2136 target = got_ref_get_symref_target(ref);
2137 } else {
2138 err = got_ref_resolve(&id, repo, ref);
2139 if (err)
2140 goto done;
2141 err = got_object_id_str(&id_str, id);
2142 if (err)
2143 goto done;
2144 target = id_str;
2147 err = got_ref_delete(ref, repo);
2148 if (err)
2149 goto done;
2151 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2152 done:
2153 free(id);
2154 free(id_str);
2155 return err;
2158 static const struct got_error *
2159 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2161 const struct got_error *err = NULL;
2162 struct got_reflist_head refs;
2163 struct got_reflist_entry *re;
2164 char *prefix;
2166 TAILQ_INIT(&refs);
2168 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2169 err = got_error_from_errno("asprintf");
2170 goto done;
2172 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2173 if (err)
2174 goto done;
2176 TAILQ_FOREACH(re, &refs, entry)
2177 delete_ref(repo, re->ref);
2178 done:
2179 got_ref_list_free(&refs);
2180 return err;
2183 static const struct got_error *
2184 cmd_fetch(int argc, char *argv[])
2186 const struct got_error *error = NULL, *unlock_err;
2187 char *cwd = NULL, *repo_path = NULL;
2188 const char *remote_name;
2189 char *proto = NULL, *host = NULL, *port = NULL;
2190 char *repo_name = NULL, *server_path = NULL;
2191 const struct got_remote_repo *remotes, *remote = NULL;
2192 int nremotes;
2193 char *id_str = NULL;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2196 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2197 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2198 struct got_pathlist_entry *pe;
2199 struct got_object_id *pack_hash = NULL;
2200 int i, ch, fetchfd = -1, fetchstatus;
2201 pid_t fetchpid = -1;
2202 struct got_fetch_progress_arg fpa;
2203 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2204 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2206 TAILQ_INIT(&refs);
2207 TAILQ_INIT(&symrefs);
2208 TAILQ_INIT(&wanted_branches);
2209 TAILQ_INIT(&wanted_refs);
2211 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2212 switch (ch) {
2213 case 'a':
2214 fetch_all_branches = 1;
2215 break;
2216 case 'b':
2217 error = got_pathlist_append(&wanted_branches,
2218 optarg, NULL);
2219 if (error)
2220 return error;
2221 break;
2222 case 'd':
2223 delete_refs = 1;
2224 break;
2225 case 'l':
2226 list_refs_only = 1;
2227 break;
2228 case 'r':
2229 repo_path = realpath(optarg, NULL);
2230 if (repo_path == NULL)
2231 return got_error_from_errno2("realpath",
2232 optarg);
2233 got_path_strip_trailing_slashes(repo_path);
2234 break;
2235 case 't':
2236 replace_tags = 1;
2237 break;
2238 case 'v':
2239 if (verbosity < 0)
2240 verbosity = 0;
2241 else if (verbosity < 3)
2242 verbosity++;
2243 break;
2244 case 'q':
2245 verbosity = -1;
2246 break;
2247 case 'R':
2248 error = got_pathlist_append(&wanted_refs,
2249 optarg, NULL);
2250 if (error)
2251 return error;
2252 break;
2253 case 'X':
2254 delete_remote = 1;
2255 break;
2256 default:
2257 usage_fetch();
2258 break;
2261 argc -= optind;
2262 argv += optind;
2264 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2265 option_conflict('a', 'b');
2266 if (list_refs_only) {
2267 if (!TAILQ_EMPTY(&wanted_branches))
2268 option_conflict('l', 'b');
2269 if (fetch_all_branches)
2270 option_conflict('l', 'a');
2271 if (delete_refs)
2272 option_conflict('l', 'd');
2273 if (delete_remote)
2274 option_conflict('l', 'X');
2276 if (delete_remote) {
2277 if (fetch_all_branches)
2278 option_conflict('X', 'a');
2279 if (!TAILQ_EMPTY(&wanted_branches))
2280 option_conflict('X', 'b');
2281 if (delete_refs)
2282 option_conflict('X', 'd');
2283 if (replace_tags)
2284 option_conflict('X', 't');
2285 if (!TAILQ_EMPTY(&wanted_refs))
2286 option_conflict('X', 'R');
2289 if (argc == 0) {
2290 if (delete_remote)
2291 errx(1, "-X option requires a remote name");
2292 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2293 } else if (argc == 1)
2294 remote_name = argv[0];
2295 else
2296 usage_fetch();
2298 cwd = getcwd(NULL, 0);
2299 if (cwd == NULL) {
2300 error = got_error_from_errno("getcwd");
2301 goto done;
2304 if (repo_path == NULL) {
2305 error = got_worktree_open(&worktree, cwd);
2306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2307 goto done;
2308 else
2309 error = NULL;
2310 if (worktree) {
2311 repo_path =
2312 strdup(got_worktree_get_repo_path(worktree));
2313 if (repo_path == NULL)
2314 error = got_error_from_errno("strdup");
2315 if (error)
2316 goto done;
2317 } else {
2318 repo_path = strdup(cwd);
2319 if (repo_path == NULL) {
2320 error = got_error_from_errno("strdup");
2321 goto done;
2326 error = got_repo_open(&repo, repo_path, NULL);
2327 if (error)
2328 goto done;
2330 if (delete_remote) {
2331 error = delete_refs_for_remote(repo, remote_name);
2332 goto done; /* nothing else to do */
2335 if (worktree) {
2336 worktree_conf = got_worktree_get_gotconfig(worktree);
2337 if (worktree_conf) {
2338 got_gotconfig_get_remotes(&nremotes, &remotes,
2339 worktree_conf);
2340 for (i = 0; i < nremotes; i++) {
2341 if (strcmp(remotes[i].name, remote_name) == 0) {
2342 remote = &remotes[i];
2343 break;
2348 if (remote == NULL) {
2349 repo_conf = got_repo_get_gotconfig(repo);
2350 if (repo_conf) {
2351 got_gotconfig_get_remotes(&nremotes, &remotes,
2352 repo_conf);
2353 for (i = 0; i < nremotes; i++) {
2354 if (strcmp(remotes[i].name, remote_name) == 0) {
2355 remote = &remotes[i];
2356 break;
2361 if (remote == NULL) {
2362 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2363 for (i = 0; i < nremotes; i++) {
2364 if (strcmp(remotes[i].name, remote_name) == 0) {
2365 remote = &remotes[i];
2366 break;
2370 if (remote == NULL) {
2371 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2372 goto done;
2375 if (TAILQ_EMPTY(&wanted_branches)) {
2376 if (!fetch_all_branches)
2377 fetch_all_branches = remote->fetch_all_branches;
2378 for (i = 0; i < remote->nfetch_branches; i++) {
2379 got_pathlist_append(&wanted_branches,
2380 remote->fetch_branches[i], NULL);
2383 if (TAILQ_EMPTY(&wanted_refs)) {
2384 for (i = 0; i < remote->nfetch_refs; i++) {
2385 got_pathlist_append(&wanted_refs,
2386 remote->fetch_refs[i], NULL);
2390 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2391 &repo_name, remote->fetch_url);
2392 if (error)
2393 goto done;
2395 if (strcmp(proto, "git") == 0) {
2396 #ifndef PROFILE
2397 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2398 "sendfd dns inet unveil", NULL) == -1)
2399 err(1, "pledge");
2400 #endif
2401 } else if (strcmp(proto, "git+ssh") == 0 ||
2402 strcmp(proto, "ssh") == 0) {
2403 #ifndef PROFILE
2404 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2405 "sendfd unveil", NULL) == -1)
2406 err(1, "pledge");
2407 #endif
2408 } else if (strcmp(proto, "http") == 0 ||
2409 strcmp(proto, "git+http") == 0) {
2410 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2411 goto done;
2412 } else {
2413 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2414 goto done;
2417 error = got_dial_apply_unveil(proto);
2418 if (error)
2419 goto done;
2421 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2422 if (error)
2423 goto done;
2425 if (verbosity >= 0)
2426 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2427 port ? ":" : "", port ? port : "");
2429 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2430 server_path, verbosity);
2431 if (error)
2432 goto done;
2434 fpa.last_scaled_size[0] = '\0';
2435 fpa.last_p_indexed = -1;
2436 fpa.last_p_resolved = -1;
2437 fpa.verbosity = verbosity;
2438 fpa.repo = repo;
2439 fpa.create_configs = 0;
2440 fpa.configs_created = 0;
2441 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2442 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2443 remote->mirror_references, fetch_all_branches, &wanted_branches,
2444 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2445 fetch_progress, &fpa);
2446 if (error)
2447 goto done;
2449 if (list_refs_only) {
2450 error = list_remote_refs(&symrefs, &refs);
2451 goto done;
2454 if (pack_hash == NULL) {
2455 if (verbosity >= 0)
2456 printf("Already up-to-date\n");
2457 } else if (verbosity >= 0) {
2458 error = got_object_id_str(&id_str, pack_hash);
2459 if (error)
2460 goto done;
2461 printf("\nFetched %s.pack\n", id_str);
2462 free(id_str);
2463 id_str = NULL;
2466 /* Update references provided with the pack file. */
2467 TAILQ_FOREACH(pe, &refs, entry) {
2468 const char *refname = pe->path;
2469 struct got_object_id *id = pe->data;
2470 struct got_reference *ref;
2471 char *remote_refname;
2473 if (is_wanted_ref(&wanted_refs, refname) &&
2474 !remote->mirror_references) {
2475 error = update_wanted_ref(refname, id,
2476 remote->name, verbosity, repo);
2477 if (error)
2478 goto done;
2479 continue;
2482 if (remote->mirror_references ||
2483 strncmp("refs/tags/", refname, 10) == 0) {
2484 error = got_ref_open(&ref, repo, refname, 1);
2485 if (error) {
2486 if (error->code != GOT_ERR_NOT_REF)
2487 goto done;
2488 error = create_ref(refname, id, verbosity,
2489 repo);
2490 if (error)
2491 goto done;
2492 } else {
2493 error = update_ref(ref, id, replace_tags,
2494 verbosity, repo);
2495 unlock_err = got_ref_unlock(ref);
2496 if (unlock_err && error == NULL)
2497 error = unlock_err;
2498 got_ref_close(ref);
2499 if (error)
2500 goto done;
2502 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2503 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2504 remote_name, refname + 11) == -1) {
2505 error = got_error_from_errno("asprintf");
2506 goto done;
2509 error = got_ref_open(&ref, repo, remote_refname, 1);
2510 if (error) {
2511 if (error->code != GOT_ERR_NOT_REF)
2512 goto done;
2513 error = create_ref(remote_refname, id,
2514 verbosity, repo);
2515 if (error)
2516 goto done;
2517 } else {
2518 error = update_ref(ref, id, replace_tags,
2519 verbosity, repo);
2520 unlock_err = got_ref_unlock(ref);
2521 if (unlock_err && error == NULL)
2522 error = unlock_err;
2523 got_ref_close(ref);
2524 if (error)
2525 goto done;
2528 /* Also create a local branch if none exists yet. */
2529 error = got_ref_open(&ref, repo, refname, 1);
2530 if (error) {
2531 if (error->code != GOT_ERR_NOT_REF)
2532 goto done;
2533 error = create_ref(refname, id, verbosity,
2534 repo);
2535 if (error)
2536 goto done;
2537 } else {
2538 unlock_err = got_ref_unlock(ref);
2539 if (unlock_err && error == NULL)
2540 error = unlock_err;
2541 got_ref_close(ref);
2545 if (delete_refs) {
2546 error = delete_missing_refs(&refs, &symrefs, remote,
2547 verbosity, repo);
2548 if (error)
2549 goto done;
2552 if (!remote->mirror_references) {
2553 /* Update remote HEAD reference if the server provided one. */
2554 TAILQ_FOREACH(pe, &symrefs, entry) {
2555 struct got_reference *target_ref;
2556 const char *refname = pe->path;
2557 const char *target = pe->data;
2558 char *remote_refname = NULL, *remote_target = NULL;
2560 if (strcmp(refname, GOT_REF_HEAD) != 0)
2561 continue;
2563 if (strncmp("refs/heads/", target, 11) != 0)
2564 continue;
2566 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2567 remote->name, refname) == -1) {
2568 error = got_error_from_errno("asprintf");
2569 goto done;
2571 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2572 remote->name, target + 11) == -1) {
2573 error = got_error_from_errno("asprintf");
2574 free(remote_refname);
2575 goto done;
2578 error = got_ref_open(&target_ref, repo, remote_target,
2579 0);
2580 if (error) {
2581 free(remote_refname);
2582 free(remote_target);
2583 if (error->code == GOT_ERR_NOT_REF) {
2584 error = NULL;
2585 continue;
2587 goto done;
2589 error = update_symref(remote_refname, target_ref,
2590 verbosity, repo);
2591 free(remote_refname);
2592 free(remote_target);
2593 got_ref_close(target_ref);
2594 if (error)
2595 goto done;
2598 done:
2599 if (fetchpid > 0) {
2600 if (kill(fetchpid, SIGTERM) == -1)
2601 error = got_error_from_errno("kill");
2602 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2603 error = got_error_from_errno("waitpid");
2605 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2606 error = got_error_from_errno("close");
2607 if (repo) {
2608 const struct got_error *close_err = got_repo_close(repo);
2609 if (error == NULL)
2610 error = close_err;
2612 if (worktree)
2613 got_worktree_close(worktree);
2614 TAILQ_FOREACH(pe, &refs, entry) {
2615 free((void *)pe->path);
2616 free(pe->data);
2618 got_pathlist_free(&refs);
2619 TAILQ_FOREACH(pe, &symrefs, entry) {
2620 free((void *)pe->path);
2621 free(pe->data);
2623 got_pathlist_free(&symrefs);
2624 got_pathlist_free(&wanted_branches);
2625 got_pathlist_free(&wanted_refs);
2626 free(id_str);
2627 free(cwd);
2628 free(repo_path);
2629 free(pack_hash);
2630 free(proto);
2631 free(host);
2632 free(port);
2633 free(server_path);
2634 free(repo_name);
2635 return error;
2639 __dead static void
2640 usage_checkout(void)
2642 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2643 "[-p prefix] [-q] repository-path [worktree-path]\n",
2644 getprogname());
2645 exit(1);
2648 static void
2649 show_worktree_base_ref_warning(void)
2651 fprintf(stderr, "%s: warning: could not create a reference "
2652 "to the work tree's base commit; the commit could be "
2653 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2654 "repository writable and running 'got update' will prevent this\n",
2655 getprogname());
2658 struct got_checkout_progress_arg {
2659 const char *worktree_path;
2660 int had_base_commit_ref_error;
2661 int verbosity;
2664 static const struct got_error *
2665 checkout_progress(void *arg, unsigned char status, const char *path)
2667 struct got_checkout_progress_arg *a = arg;
2669 /* Base commit bump happens silently. */
2670 if (status == GOT_STATUS_BUMP_BASE)
2671 return NULL;
2673 if (status == GOT_STATUS_BASE_REF_ERR) {
2674 a->had_base_commit_ref_error = 1;
2675 return NULL;
2678 while (path[0] == '/')
2679 path++;
2681 if (a->verbosity >= 0)
2682 printf("%c %s/%s\n", status, a->worktree_path, path);
2684 return NULL;
2687 static const struct got_error *
2688 check_cancelled(void *arg)
2690 if (sigint_received || sigpipe_received)
2691 return got_error(GOT_ERR_CANCELLED);
2692 return NULL;
2695 static const struct got_error *
2696 check_linear_ancestry(struct got_object_id *commit_id,
2697 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2698 struct got_repository *repo)
2700 const struct got_error *err = NULL;
2701 struct got_object_id *yca_id;
2703 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2704 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2705 if (err)
2706 return err;
2708 if (yca_id == NULL)
2709 return got_error(GOT_ERR_ANCESTRY);
2712 * Require a straight line of history between the target commit
2713 * and the work tree's base commit.
2715 * Non-linear situations such as this require a rebase:
2717 * (commit) D F (base_commit)
2718 * \ /
2719 * C E
2720 * \ /
2721 * B (yca)
2722 * |
2723 * A
2725 * 'got update' only handles linear cases:
2726 * Update forwards in time: A (base/yca) - B - C - D (commit)
2727 * Update backwards in time: D (base) - C - B - A (commit/yca)
2729 if (allow_forwards_in_time_only) {
2730 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2731 return got_error(GOT_ERR_ANCESTRY);
2732 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2733 got_object_id_cmp(base_commit_id, yca_id) != 0)
2734 return got_error(GOT_ERR_ANCESTRY);
2736 free(yca_id);
2737 return NULL;
2740 static const struct got_error *
2741 check_same_branch(struct got_object_id *commit_id,
2742 struct got_reference *head_ref, struct got_object_id *yca_id,
2743 struct got_repository *repo)
2745 const struct got_error *err = NULL;
2746 struct got_commit_graph *graph = NULL;
2747 struct got_object_id *head_commit_id = NULL;
2748 int is_same_branch = 0;
2750 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2751 if (err)
2752 goto done;
2754 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2755 is_same_branch = 1;
2756 goto done;
2758 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2759 is_same_branch = 1;
2760 goto done;
2763 err = got_commit_graph_open(&graph, "/", 1);
2764 if (err)
2765 goto done;
2767 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2768 check_cancelled, NULL);
2769 if (err)
2770 goto done;
2772 for (;;) {
2773 struct got_object_id *id;
2774 err = got_commit_graph_iter_next(&id, graph, repo,
2775 check_cancelled, NULL);
2776 if (err) {
2777 if (err->code == GOT_ERR_ITER_COMPLETED)
2778 err = NULL;
2779 break;
2782 if (id) {
2783 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2784 break;
2785 if (got_object_id_cmp(id, commit_id) == 0) {
2786 is_same_branch = 1;
2787 break;
2791 done:
2792 if (graph)
2793 got_commit_graph_close(graph);
2794 free(head_commit_id);
2795 if (!err && !is_same_branch)
2796 err = got_error(GOT_ERR_ANCESTRY);
2797 return err;
2800 static const struct got_error *
2801 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2803 static char msg[512];
2804 const char *branch_name;
2806 if (got_ref_is_symbolic(ref))
2807 branch_name = got_ref_get_symref_target(ref);
2808 else
2809 branch_name = got_ref_get_name(ref);
2811 if (strncmp("refs/heads/", branch_name, 11) == 0)
2812 branch_name += 11;
2814 snprintf(msg, sizeof(msg),
2815 "target commit is not contained in branch '%s'; "
2816 "the branch to use must be specified with -b; "
2817 "if necessary a new branch can be created for "
2818 "this commit with 'got branch -c %s BRANCH_NAME'",
2819 branch_name, commit_id_str);
2821 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2824 static const struct got_error *
2825 cmd_checkout(int argc, char *argv[])
2827 const struct got_error *error = NULL;
2828 struct got_repository *repo = NULL;
2829 struct got_reference *head_ref = NULL, *ref = NULL;
2830 struct got_worktree *worktree = NULL;
2831 char *repo_path = NULL;
2832 char *worktree_path = NULL;
2833 const char *path_prefix = "";
2834 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2835 char *commit_id_str = NULL;
2836 struct got_object_id *commit_id = NULL;
2837 char *cwd = NULL;
2838 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2839 struct got_pathlist_head paths;
2840 struct got_checkout_progress_arg cpa;
2842 TAILQ_INIT(&paths);
2844 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2845 switch (ch) {
2846 case 'b':
2847 branch_name = optarg;
2848 break;
2849 case 'c':
2850 commit_id_str = strdup(optarg);
2851 if (commit_id_str == NULL)
2852 return got_error_from_errno("strdup");
2853 break;
2854 case 'E':
2855 allow_nonempty = 1;
2856 break;
2857 case 'p':
2858 path_prefix = optarg;
2859 break;
2860 case 'q':
2861 verbosity = -1;
2862 break;
2863 default:
2864 usage_checkout();
2865 /* NOTREACHED */
2869 argc -= optind;
2870 argv += optind;
2872 #ifndef PROFILE
2873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2874 "unveil", NULL) == -1)
2875 err(1, "pledge");
2876 #endif
2877 if (argc == 1) {
2878 char *base, *dotgit;
2879 const char *path;
2880 repo_path = realpath(argv[0], NULL);
2881 if (repo_path == NULL)
2882 return got_error_from_errno2("realpath", argv[0]);
2883 cwd = getcwd(NULL, 0);
2884 if (cwd == NULL) {
2885 error = got_error_from_errno("getcwd");
2886 goto done;
2888 if (path_prefix[0])
2889 path = path_prefix;
2890 else
2891 path = repo_path;
2892 error = got_path_basename(&base, path);
2893 if (error)
2894 goto done;
2895 dotgit = strstr(base, ".git");
2896 if (dotgit)
2897 *dotgit = '\0';
2898 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2899 error = got_error_from_errno("asprintf");
2900 free(base);
2901 goto done;
2903 free(base);
2904 } else if (argc == 2) {
2905 repo_path = realpath(argv[0], NULL);
2906 if (repo_path == NULL) {
2907 error = got_error_from_errno2("realpath", argv[0]);
2908 goto done;
2910 worktree_path = realpath(argv[1], NULL);
2911 if (worktree_path == NULL) {
2912 if (errno != ENOENT) {
2913 error = got_error_from_errno2("realpath",
2914 argv[1]);
2915 goto done;
2917 worktree_path = strdup(argv[1]);
2918 if (worktree_path == NULL) {
2919 error = got_error_from_errno("strdup");
2920 goto done;
2923 } else
2924 usage_checkout();
2926 got_path_strip_trailing_slashes(repo_path);
2927 got_path_strip_trailing_slashes(worktree_path);
2929 error = got_repo_open(&repo, repo_path, NULL);
2930 if (error != NULL)
2931 goto done;
2933 /* Pre-create work tree path for unveil(2) */
2934 error = got_path_mkdir(worktree_path);
2935 if (error) {
2936 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2937 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2938 goto done;
2939 if (!allow_nonempty &&
2940 !got_path_dir_is_empty(worktree_path)) {
2941 error = got_error_path(worktree_path,
2942 GOT_ERR_DIR_NOT_EMPTY);
2943 goto done;
2947 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2948 if (error)
2949 goto done;
2951 error = got_ref_open(&head_ref, repo, branch_name, 0);
2952 if (error != NULL)
2953 goto done;
2955 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2956 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2957 goto done;
2959 error = got_worktree_open(&worktree, worktree_path);
2960 if (error != NULL)
2961 goto done;
2963 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2964 path_prefix);
2965 if (error != NULL)
2966 goto done;
2967 if (!same_path_prefix) {
2968 error = got_error(GOT_ERR_PATH_PREFIX);
2969 goto done;
2972 if (commit_id_str) {
2973 struct got_reflist_head refs;
2974 TAILQ_INIT(&refs);
2975 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2976 NULL);
2977 if (error)
2978 goto done;
2979 error = got_repo_match_object_id(&commit_id, NULL,
2980 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2981 got_ref_list_free(&refs);
2982 if (error)
2983 goto done;
2984 error = check_linear_ancestry(commit_id,
2985 got_worktree_get_base_commit_id(worktree), 0, repo);
2986 if (error != NULL) {
2987 free(commit_id);
2988 if (error->code == GOT_ERR_ANCESTRY) {
2989 error = checkout_ancestry_error(
2990 head_ref, commit_id_str);
2992 goto done;
2994 error = check_same_branch(commit_id, head_ref, NULL, repo);
2995 if (error) {
2996 if (error->code == GOT_ERR_ANCESTRY) {
2997 error = checkout_ancestry_error(
2998 head_ref, commit_id_str);
3000 goto done;
3002 error = got_worktree_set_base_commit_id(worktree, repo,
3003 commit_id);
3004 if (error)
3005 goto done;
3006 /* Expand potentially abbreviated commit ID string. */
3007 free(commit_id_str);
3008 error = got_object_id_str(&commit_id_str, commit_id);
3009 if (error)
3010 goto done;
3011 } else {
3012 commit_id = got_object_id_dup(
3013 got_worktree_get_base_commit_id(worktree));
3014 if (commit_id == NULL) {
3015 error = got_error_from_errno("got_object_id_dup");
3016 goto done;
3018 error = got_object_id_str(&commit_id_str, commit_id);
3019 if (error)
3020 goto done;
3023 error = got_pathlist_append(&paths, "", NULL);
3024 if (error)
3025 goto done;
3026 cpa.worktree_path = worktree_path;
3027 cpa.had_base_commit_ref_error = 0;
3028 cpa.verbosity = verbosity;
3029 error = got_worktree_checkout_files(worktree, &paths, repo,
3030 checkout_progress, &cpa, check_cancelled, NULL);
3031 if (error != NULL)
3032 goto done;
3034 if (got_ref_is_symbolic(head_ref)) {
3035 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3036 if (error)
3037 goto done;
3038 refname = got_ref_get_name(ref);
3039 } else
3040 refname = got_ref_get_name(head_ref);
3041 printf("Checked out %s: %s\n", refname, commit_id_str);
3042 printf("Now shut up and hack\n");
3043 if (cpa.had_base_commit_ref_error)
3044 show_worktree_base_ref_warning();
3045 done:
3046 if (head_ref)
3047 got_ref_close(head_ref);
3048 if (ref)
3049 got_ref_close(ref);
3050 got_pathlist_free(&paths);
3051 free(commit_id_str);
3052 free(commit_id);
3053 free(repo_path);
3054 free(worktree_path);
3055 free(cwd);
3056 return error;
3059 struct got_update_progress_arg {
3060 int did_something;
3061 int conflicts;
3062 int obstructed;
3063 int not_updated;
3064 int missing;
3065 int verbosity;
3068 void
3069 print_update_progress_stats(struct got_update_progress_arg *upa)
3071 if (!upa->did_something)
3072 return;
3074 if (upa->conflicts > 0)
3075 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3076 if (upa->obstructed > 0)
3077 printf("File paths obstructed by a non-regular file: %d\n",
3078 upa->obstructed);
3079 if (upa->not_updated > 0)
3080 printf("Files not updated because of existing merge "
3081 "conflicts: %d\n", upa->not_updated);
3084 __dead static void
3085 usage_update(void)
3087 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3088 "[path ...]\n",
3089 getprogname());
3090 exit(1);
3093 static const struct got_error *
3094 update_progress(void *arg, unsigned char status, const char *path)
3096 struct got_update_progress_arg *upa = arg;
3098 if (status == GOT_STATUS_EXISTS ||
3099 status == GOT_STATUS_BASE_REF_ERR)
3100 return NULL;
3102 upa->did_something = 1;
3104 /* Base commit bump happens silently. */
3105 if (status == GOT_STATUS_BUMP_BASE)
3106 return NULL;
3108 if (status == GOT_STATUS_CONFLICT)
3109 upa->conflicts++;
3110 if (status == GOT_STATUS_OBSTRUCTED)
3111 upa->obstructed++;
3112 if (status == GOT_STATUS_CANNOT_UPDATE)
3113 upa->not_updated++;
3114 if (status == GOT_STATUS_MISSING)
3115 upa->missing++;
3117 while (path[0] == '/')
3118 path++;
3119 if (upa->verbosity >= 0)
3120 printf("%c %s\n", status, path);
3122 return NULL;
3125 static const struct got_error *
3126 switch_head_ref(struct got_reference *head_ref,
3127 struct got_object_id *commit_id, struct got_worktree *worktree,
3128 struct got_repository *repo)
3130 const struct got_error *err = NULL;
3131 char *base_id_str;
3132 int ref_has_moved = 0;
3134 /* Trivial case: switching between two different references. */
3135 if (strcmp(got_ref_get_name(head_ref),
3136 got_worktree_get_head_ref_name(worktree)) != 0) {
3137 printf("Switching work tree from %s to %s\n",
3138 got_worktree_get_head_ref_name(worktree),
3139 got_ref_get_name(head_ref));
3140 return got_worktree_set_head_ref(worktree, head_ref);
3143 err = check_linear_ancestry(commit_id,
3144 got_worktree_get_base_commit_id(worktree), 0, repo);
3145 if (err) {
3146 if (err->code != GOT_ERR_ANCESTRY)
3147 return err;
3148 ref_has_moved = 1;
3150 if (!ref_has_moved)
3151 return NULL;
3153 /* Switching to a rebased branch with the same reference name. */
3154 err = got_object_id_str(&base_id_str,
3155 got_worktree_get_base_commit_id(worktree));
3156 if (err)
3157 return err;
3158 printf("Reference %s now points at a different branch\n",
3159 got_worktree_get_head_ref_name(worktree));
3160 printf("Switching work tree from %s to %s\n", base_id_str,
3161 got_worktree_get_head_ref_name(worktree));
3162 return NULL;
3165 static const struct got_error *
3166 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3168 const struct got_error *err;
3169 int in_progress;
3171 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3172 if (err)
3173 return err;
3174 if (in_progress)
3175 return got_error(GOT_ERR_REBASING);
3177 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3178 if (err)
3179 return err;
3180 if (in_progress)
3181 return got_error(GOT_ERR_HISTEDIT_BUSY);
3183 return NULL;
3186 static const struct got_error *
3187 check_merge_in_progress(struct got_worktree *worktree,
3188 struct got_repository *repo)
3190 const struct got_error *err;
3191 int in_progress;
3193 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3194 if (err)
3195 return err;
3196 if (in_progress)
3197 return got_error(GOT_ERR_MERGE_BUSY);
3199 return NULL;
3202 static const struct got_error *
3203 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3204 char *argv[], struct got_worktree *worktree)
3206 const struct got_error *err = NULL;
3207 char *path;
3208 int i;
3210 if (argc == 0) {
3211 path = strdup("");
3212 if (path == NULL)
3213 return got_error_from_errno("strdup");
3214 return got_pathlist_append(paths, path, NULL);
3217 for (i = 0; i < argc; i++) {
3218 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3219 if (err)
3220 break;
3221 err = got_pathlist_append(paths, path, NULL);
3222 if (err) {
3223 free(path);
3224 break;
3228 return err;
3231 static const struct got_error *
3232 wrap_not_worktree_error(const struct got_error *orig_err,
3233 const char *cmdname, const char *path)
3235 const struct got_error *err;
3236 struct got_repository *repo;
3237 static char msg[512];
3239 err = got_repo_open(&repo, path, NULL);
3240 if (err)
3241 return orig_err;
3243 snprintf(msg, sizeof(msg),
3244 "'got %s' needs a work tree in addition to a git repository\n"
3245 "Work trees can be checked out from this Git repository with "
3246 "'got checkout'.\n"
3247 "The got(1) manual page contains more information.", cmdname);
3248 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3249 got_repo_close(repo);
3250 return err;
3253 static const struct got_error *
3254 cmd_update(int argc, char *argv[])
3256 const struct got_error *error = NULL;
3257 struct got_repository *repo = NULL;
3258 struct got_worktree *worktree = NULL;
3259 char *worktree_path = NULL;
3260 struct got_object_id *commit_id = NULL;
3261 char *commit_id_str = NULL;
3262 const char *branch_name = NULL;
3263 struct got_reference *head_ref = NULL;
3264 struct got_pathlist_head paths;
3265 struct got_pathlist_entry *pe;
3266 int ch, verbosity = 0;
3267 struct got_update_progress_arg upa;
3269 TAILQ_INIT(&paths);
3271 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3272 switch (ch) {
3273 case 'b':
3274 branch_name = optarg;
3275 break;
3276 case 'c':
3277 commit_id_str = strdup(optarg);
3278 if (commit_id_str == NULL)
3279 return got_error_from_errno("strdup");
3280 break;
3281 case 'q':
3282 verbosity = -1;
3283 break;
3284 default:
3285 usage_update();
3286 /* NOTREACHED */
3290 argc -= optind;
3291 argv += optind;
3293 #ifndef PROFILE
3294 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3295 "unveil", NULL) == -1)
3296 err(1, "pledge");
3297 #endif
3298 worktree_path = getcwd(NULL, 0);
3299 if (worktree_path == NULL) {
3300 error = got_error_from_errno("getcwd");
3301 goto done;
3303 error = got_worktree_open(&worktree, worktree_path);
3304 if (error) {
3305 if (error->code == GOT_ERR_NOT_WORKTREE)
3306 error = wrap_not_worktree_error(error, "update",
3307 worktree_path);
3308 goto done;
3311 error = check_rebase_or_histedit_in_progress(worktree);
3312 if (error)
3313 goto done;
3315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3316 NULL);
3317 if (error != NULL)
3318 goto done;
3320 error = apply_unveil(got_repo_get_path(repo), 0,
3321 got_worktree_get_root_path(worktree));
3322 if (error)
3323 goto done;
3325 error = check_merge_in_progress(worktree, repo);
3326 if (error)
3327 goto done;
3329 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3330 if (error)
3331 goto done;
3333 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3334 got_worktree_get_head_ref_name(worktree), 0);
3335 if (error != NULL)
3336 goto done;
3337 if (commit_id_str == NULL) {
3338 error = got_ref_resolve(&commit_id, repo, head_ref);
3339 if (error != NULL)
3340 goto done;
3341 error = got_object_id_str(&commit_id_str, commit_id);
3342 if (error != NULL)
3343 goto done;
3344 } else {
3345 struct got_reflist_head refs;
3346 TAILQ_INIT(&refs);
3347 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3348 NULL);
3349 if (error)
3350 goto done;
3351 error = got_repo_match_object_id(&commit_id, NULL,
3352 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3353 got_ref_list_free(&refs);
3354 free(commit_id_str);
3355 commit_id_str = NULL;
3356 if (error)
3357 goto done;
3358 error = got_object_id_str(&commit_id_str, commit_id);
3359 if (error)
3360 goto done;
3363 if (branch_name) {
3364 struct got_object_id *head_commit_id;
3365 TAILQ_FOREACH(pe, &paths, entry) {
3366 if (pe->path_len == 0)
3367 continue;
3368 error = got_error_msg(GOT_ERR_BAD_PATH,
3369 "switching between branches requires that "
3370 "the entire work tree gets updated");
3371 goto done;
3373 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3374 if (error)
3375 goto done;
3376 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3377 repo);
3378 free(head_commit_id);
3379 if (error != NULL)
3380 goto done;
3381 error = check_same_branch(commit_id, head_ref, NULL, repo);
3382 if (error)
3383 goto done;
3384 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3385 if (error)
3386 goto done;
3387 } else {
3388 error = check_linear_ancestry(commit_id,
3389 got_worktree_get_base_commit_id(worktree), 0, repo);
3390 if (error != NULL) {
3391 if (error->code == GOT_ERR_ANCESTRY)
3392 error = got_error(GOT_ERR_BRANCH_MOVED);
3393 goto done;
3395 error = check_same_branch(commit_id, head_ref, NULL, repo);
3396 if (error)
3397 goto done;
3400 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3401 commit_id) != 0) {
3402 error = got_worktree_set_base_commit_id(worktree, repo,
3403 commit_id);
3404 if (error)
3405 goto done;
3408 memset(&upa, 0, sizeof(upa));
3409 upa.verbosity = verbosity;
3410 error = got_worktree_checkout_files(worktree, &paths, repo,
3411 update_progress, &upa, check_cancelled, NULL);
3412 if (error != NULL)
3413 goto done;
3415 if (upa.did_something) {
3416 printf("Updated to %s: %s\n",
3417 got_worktree_get_head_ref_name(worktree), commit_id_str);
3418 } else
3419 printf("Already up-to-date\n");
3420 print_update_progress_stats(&upa);
3421 done:
3422 free(worktree_path);
3423 TAILQ_FOREACH(pe, &paths, entry)
3424 free((char *)pe->path);
3425 got_pathlist_free(&paths);
3426 free(commit_id);
3427 free(commit_id_str);
3428 return error;
3431 static const struct got_error *
3432 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3433 const char *path, int diff_context, int ignore_whitespace,
3434 int force_text_diff, struct got_repository *repo)
3436 const struct got_error *err = NULL;
3437 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3439 if (blob_id1) {
3440 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3441 if (err)
3442 goto done;
3445 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3446 if (err)
3447 goto done;
3449 while (path[0] == '/')
3450 path++;
3451 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3452 diff_context, ignore_whitespace, force_text_diff, stdout);
3453 done:
3454 if (blob1)
3455 got_object_blob_close(blob1);
3456 got_object_blob_close(blob2);
3457 return err;
3460 static const struct got_error *
3461 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3462 const char *path, int diff_context, int ignore_whitespace,
3463 int force_text_diff, struct got_repository *repo)
3465 const struct got_error *err = NULL;
3466 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3467 struct got_diff_blob_output_unidiff_arg arg;
3469 if (tree_id1) {
3470 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3471 if (err)
3472 goto done;
3475 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3476 if (err)
3477 goto done;
3479 arg.diff_context = diff_context;
3480 arg.ignore_whitespace = ignore_whitespace;
3481 arg.force_text_diff = force_text_diff;
3482 arg.outfile = stdout;
3483 arg.line_offsets = NULL;
3484 arg.nlines = 0;
3485 while (path[0] == '/')
3486 path++;
3487 err = got_diff_tree(tree1, tree2, path, path, repo,
3488 got_diff_blob_output_unidiff, &arg, 1);
3489 done:
3490 if (tree1)
3491 got_object_tree_close(tree1);
3492 if (tree2)
3493 got_object_tree_close(tree2);
3494 return err;
3497 static const struct got_error *
3498 get_changed_paths(struct got_pathlist_head *paths,
3499 struct got_commit_object *commit, struct got_repository *repo)
3501 const struct got_error *err = NULL;
3502 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3503 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3504 struct got_object_qid *qid;
3506 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3507 if (qid != NULL) {
3508 struct got_commit_object *pcommit;
3509 err = got_object_open_as_commit(&pcommit, repo,
3510 qid->id);
3511 if (err)
3512 return err;
3514 tree_id1 = got_object_id_dup(
3515 got_object_commit_get_tree_id(pcommit));
3516 if (tree_id1 == NULL) {
3517 got_object_commit_close(pcommit);
3518 return got_error_from_errno("got_object_id_dup");
3520 got_object_commit_close(pcommit);
3524 if (tree_id1) {
3525 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3526 if (err)
3527 goto done;
3530 tree_id2 = got_object_commit_get_tree_id(commit);
3531 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3532 if (err)
3533 goto done;
3535 err = got_diff_tree(tree1, tree2, "", "", repo,
3536 got_diff_tree_collect_changed_paths, paths, 0);
3537 done:
3538 if (tree1)
3539 got_object_tree_close(tree1);
3540 if (tree2)
3541 got_object_tree_close(tree2);
3542 free(tree_id1);
3543 return err;
3546 static const struct got_error *
3547 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3548 const char *path, int diff_context, struct got_repository *repo)
3550 const struct got_error *err = NULL;
3551 struct got_commit_object *pcommit = NULL;
3552 char *id_str1 = NULL, *id_str2 = NULL;
3553 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3554 struct got_object_qid *qid;
3556 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3557 if (qid != NULL) {
3558 err = got_object_open_as_commit(&pcommit, repo,
3559 qid->id);
3560 if (err)
3561 return err;
3564 if (path && path[0] != '\0') {
3565 int obj_type;
3566 err = got_object_id_by_path(&obj_id2, repo, id, path);
3567 if (err)
3568 goto done;
3569 err = got_object_id_str(&id_str2, obj_id2);
3570 if (err) {
3571 free(obj_id2);
3572 goto done;
3574 if (pcommit) {
3575 err = got_object_id_by_path(&obj_id1, repo,
3576 qid->id, path);
3577 if (err) {
3578 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3579 free(obj_id2);
3580 goto done;
3582 } else {
3583 err = got_object_id_str(&id_str1, obj_id1);
3584 if (err) {
3585 free(obj_id2);
3586 goto done;
3590 err = got_object_get_type(&obj_type, repo, obj_id2);
3591 if (err) {
3592 free(obj_id2);
3593 goto done;
3595 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3596 switch (obj_type) {
3597 case GOT_OBJ_TYPE_BLOB:
3598 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3599 0, 0, repo);
3600 break;
3601 case GOT_OBJ_TYPE_TREE:
3602 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3603 0, 0, repo);
3604 break;
3605 default:
3606 err = got_error(GOT_ERR_OBJ_TYPE);
3607 break;
3609 free(obj_id1);
3610 free(obj_id2);
3611 } else {
3612 obj_id2 = got_object_commit_get_tree_id(commit);
3613 err = got_object_id_str(&id_str2, obj_id2);
3614 if (err)
3615 goto done;
3616 if (pcommit) {
3617 obj_id1 = got_object_commit_get_tree_id(pcommit);
3618 err = got_object_id_str(&id_str1, obj_id1);
3619 if (err)
3620 goto done;
3622 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3623 id_str2);
3624 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3625 repo);
3627 done:
3628 free(id_str1);
3629 free(id_str2);
3630 if (pcommit)
3631 got_object_commit_close(pcommit);
3632 return err;
3635 static char *
3636 get_datestr(time_t *time, char *datebuf)
3638 struct tm mytm, *tm;
3639 char *p, *s;
3641 tm = gmtime_r(time, &mytm);
3642 if (tm == NULL)
3643 return NULL;
3644 s = asctime_r(tm, datebuf);
3645 if (s == NULL)
3646 return NULL;
3647 p = strchr(s, '\n');
3648 if (p)
3649 *p = '\0';
3650 return s;
3653 static const struct got_error *
3654 match_logmsg(int *have_match, struct got_object_id *id,
3655 struct got_commit_object *commit, regex_t *regex)
3657 const struct got_error *err = NULL;
3658 regmatch_t regmatch;
3659 char *id_str = NULL, *logmsg = NULL;
3661 *have_match = 0;
3663 err = got_object_id_str(&id_str, id);
3664 if (err)
3665 return err;
3667 err = got_object_commit_get_logmsg(&logmsg, commit);
3668 if (err)
3669 goto done;
3671 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3672 *have_match = 1;
3673 done:
3674 free(id_str);
3675 free(logmsg);
3676 return err;
3679 static void
3680 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3681 regex_t *regex)
3683 regmatch_t regmatch;
3684 struct got_pathlist_entry *pe;
3686 *have_match = 0;
3688 TAILQ_FOREACH(pe, changed_paths, entry) {
3689 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3690 *have_match = 1;
3691 break;
3696 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3698 static const struct got_error*
3699 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3700 struct got_object_id *id, struct got_repository *repo)
3702 static const struct got_error *err = NULL;
3703 struct got_reflist_entry *re;
3704 char *s;
3705 const char *name;
3707 *refs_str = NULL;
3709 TAILQ_FOREACH(re, refs, entry) {
3710 struct got_tag_object *tag = NULL;
3711 struct got_object_id *ref_id;
3712 int cmp;
3714 name = got_ref_get_name(re->ref);
3715 if (strcmp(name, GOT_REF_HEAD) == 0)
3716 continue;
3717 if (strncmp(name, "refs/", 5) == 0)
3718 name += 5;
3719 if (strncmp(name, "got/", 4) == 0)
3720 continue;
3721 if (strncmp(name, "heads/", 6) == 0)
3722 name += 6;
3723 if (strncmp(name, "remotes/", 8) == 0) {
3724 name += 8;
3725 s = strstr(name, "/" GOT_REF_HEAD);
3726 if (s != NULL && s[strlen(s)] == '\0')
3727 continue;
3729 err = got_ref_resolve(&ref_id, repo, re->ref);
3730 if (err)
3731 break;
3732 if (strncmp(name, "tags/", 5) == 0) {
3733 err = got_object_open_as_tag(&tag, repo, ref_id);
3734 if (err) {
3735 if (err->code != GOT_ERR_OBJ_TYPE) {
3736 free(ref_id);
3737 break;
3739 /* Ref points at something other than a tag. */
3740 err = NULL;
3741 tag = NULL;
3744 cmp = got_object_id_cmp(tag ?
3745 got_object_tag_get_object_id(tag) : ref_id, id);
3746 free(ref_id);
3747 if (tag)
3748 got_object_tag_close(tag);
3749 if (cmp != 0)
3750 continue;
3751 s = *refs_str;
3752 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3753 s ? ", " : "", name) == -1) {
3754 err = got_error_from_errno("asprintf");
3755 free(s);
3756 *refs_str = NULL;
3757 break;
3759 free(s);
3762 return err;
3765 static const struct got_error *
3766 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3767 struct got_repository *repo, const char *path,
3768 struct got_pathlist_head *changed_paths, int show_patch,
3769 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3770 const char *custom_refs_str)
3772 const struct got_error *err = NULL;
3773 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3774 char datebuf[26];
3775 time_t committer_time;
3776 const char *author, *committer;
3777 char *refs_str = NULL;
3779 err = got_object_id_str(&id_str, id);
3780 if (err)
3781 return err;
3783 if (custom_refs_str == NULL) {
3784 struct got_reflist_head *refs;
3785 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3786 if (refs) {
3787 err = build_refs_str(&refs_str, refs, id, repo);
3788 if (err)
3789 goto done;
3793 printf(GOT_COMMIT_SEP_STR);
3794 if (custom_refs_str)
3795 printf("commit %s (%s)\n", id_str, custom_refs_str);
3796 else
3797 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3798 refs_str ? refs_str : "", refs_str ? ")" : "");
3799 free(id_str);
3800 id_str = NULL;
3801 free(refs_str);
3802 refs_str = NULL;
3803 printf("from: %s\n", got_object_commit_get_author(commit));
3804 committer_time = got_object_commit_get_committer_time(commit);
3805 datestr = get_datestr(&committer_time, datebuf);
3806 if (datestr)
3807 printf("date: %s UTC\n", datestr);
3808 author = got_object_commit_get_author(commit);
3809 committer = got_object_commit_get_committer(commit);
3810 if (strcmp(author, committer) != 0)
3811 printf("via: %s\n", committer);
3812 if (got_object_commit_get_nparents(commit) > 1) {
3813 const struct got_object_id_queue *parent_ids;
3814 struct got_object_qid *qid;
3815 int n = 1;
3816 parent_ids = got_object_commit_get_parent_ids(commit);
3817 STAILQ_FOREACH(qid, parent_ids, entry) {
3818 err = got_object_id_str(&id_str, qid->id);
3819 if (err)
3820 goto done;
3821 printf("parent %d: %s\n", n++, id_str);
3822 free(id_str);
3823 id_str = NULL;
3827 err = got_object_commit_get_logmsg(&logmsg0, commit);
3828 if (err)
3829 goto done;
3831 logmsg = logmsg0;
3832 do {
3833 line = strsep(&logmsg, "\n");
3834 if (line)
3835 printf(" %s\n", line);
3836 } while (line);
3837 free(logmsg0);
3839 if (changed_paths) {
3840 struct got_pathlist_entry *pe;
3841 TAILQ_FOREACH(pe, changed_paths, entry) {
3842 struct got_diff_changed_path *cp = pe->data;
3843 printf(" %c %s\n", cp->status, pe->path);
3845 printf("\n");
3847 if (show_patch) {
3848 err = print_patch(commit, id, path, diff_context, repo);
3849 if (err == 0)
3850 printf("\n");
3853 if (fflush(stdout) != 0 && err == NULL)
3854 err = got_error_from_errno("fflush");
3855 done:
3856 free(id_str);
3857 free(refs_str);
3858 return err;
3861 static const struct got_error *
3862 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3863 struct got_repository *repo, const char *path, int show_changed_paths,
3864 int show_patch, const char *search_pattern, int diff_context, int limit,
3865 int log_branches, int reverse_display_order,
3866 struct got_reflist_object_id_map *refs_idmap)
3868 const struct got_error *err;
3869 struct got_commit_graph *graph;
3870 regex_t regex;
3871 int have_match;
3872 struct got_object_id_queue reversed_commits;
3873 struct got_object_qid *qid;
3874 struct got_commit_object *commit;
3875 struct got_pathlist_head changed_paths;
3876 struct got_pathlist_entry *pe;
3878 STAILQ_INIT(&reversed_commits);
3879 TAILQ_INIT(&changed_paths);
3881 if (search_pattern && regcomp(&regex, search_pattern,
3882 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3883 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3885 err = got_commit_graph_open(&graph, path, !log_branches);
3886 if (err)
3887 return err;
3888 err = got_commit_graph_iter_start(graph, root_id, repo,
3889 check_cancelled, NULL);
3890 if (err)
3891 goto done;
3892 for (;;) {
3893 struct got_object_id *id;
3895 if (sigint_received || sigpipe_received)
3896 break;
3898 err = got_commit_graph_iter_next(&id, graph, repo,
3899 check_cancelled, NULL);
3900 if (err) {
3901 if (err->code == GOT_ERR_ITER_COMPLETED)
3902 err = NULL;
3903 break;
3905 if (id == NULL)
3906 break;
3908 err = got_object_open_as_commit(&commit, repo, id);
3909 if (err)
3910 break;
3912 if (show_changed_paths && !reverse_display_order) {
3913 err = get_changed_paths(&changed_paths, commit, repo);
3914 if (err)
3915 break;
3918 if (search_pattern) {
3919 err = match_logmsg(&have_match, id, commit, &regex);
3920 if (err) {
3921 got_object_commit_close(commit);
3922 break;
3924 if (have_match == 0 && show_changed_paths)
3925 match_changed_paths(&have_match,
3926 &changed_paths, &regex);
3927 if (have_match == 0) {
3928 got_object_commit_close(commit);
3929 TAILQ_FOREACH(pe, &changed_paths, entry) {
3930 free((char *)pe->path);
3931 free(pe->data);
3933 got_pathlist_free(&changed_paths);
3934 continue;
3938 if (reverse_display_order) {
3939 err = got_object_qid_alloc(&qid, id);
3940 if (err)
3941 break;
3942 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3943 got_object_commit_close(commit);
3944 } else {
3945 err = print_commit(commit, id, repo, path,
3946 show_changed_paths ? &changed_paths : NULL,
3947 show_patch, diff_context, refs_idmap, NULL);
3948 got_object_commit_close(commit);
3949 if (err)
3950 break;
3952 if ((limit && --limit == 0) ||
3953 (end_id && got_object_id_cmp(id, end_id) == 0))
3954 break;
3956 TAILQ_FOREACH(pe, &changed_paths, entry) {
3957 free((char *)pe->path);
3958 free(pe->data);
3960 got_pathlist_free(&changed_paths);
3962 if (reverse_display_order) {
3963 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3964 err = got_object_open_as_commit(&commit, repo, qid->id);
3965 if (err)
3966 break;
3967 if (show_changed_paths) {
3968 err = get_changed_paths(&changed_paths,
3969 commit, repo);
3970 if (err)
3971 break;
3973 err = print_commit(commit, qid->id, repo, path,
3974 show_changed_paths ? &changed_paths : NULL,
3975 show_patch, diff_context, refs_idmap, NULL);
3976 got_object_commit_close(commit);
3977 if (err)
3978 break;
3979 TAILQ_FOREACH(pe, &changed_paths, entry) {
3980 free((char *)pe->path);
3981 free(pe->data);
3983 got_pathlist_free(&changed_paths);
3986 done:
3987 while (!STAILQ_EMPTY(&reversed_commits)) {
3988 qid = STAILQ_FIRST(&reversed_commits);
3989 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3990 got_object_qid_free(qid);
3992 TAILQ_FOREACH(pe, &changed_paths, entry) {
3993 free((char *)pe->path);
3994 free(pe->data);
3996 got_pathlist_free(&changed_paths);
3997 if (search_pattern)
3998 regfree(&regex);
3999 got_commit_graph_close(graph);
4000 return err;
4003 __dead static void
4004 usage_log(void)
4006 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4007 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4008 "[-R] [path]\n", getprogname());
4009 exit(1);
4012 static int
4013 get_default_log_limit(void)
4015 const char *got_default_log_limit;
4016 long long n;
4017 const char *errstr;
4019 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4020 if (got_default_log_limit == NULL)
4021 return 0;
4022 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4023 if (errstr != NULL)
4024 return 0;
4025 return n;
4028 static const struct got_error *
4029 cmd_log(int argc, char *argv[])
4031 const struct got_error *error;
4032 struct got_repository *repo = NULL;
4033 struct got_worktree *worktree = NULL;
4034 struct got_object_id *start_id = NULL, *end_id = NULL;
4035 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4036 const char *start_commit = NULL, *end_commit = NULL;
4037 const char *search_pattern = NULL;
4038 int diff_context = -1, ch;
4039 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4040 int reverse_display_order = 0;
4041 const char *errstr;
4042 struct got_reflist_head refs;
4043 struct got_reflist_object_id_map *refs_idmap = NULL;
4045 TAILQ_INIT(&refs);
4047 #ifndef PROFILE
4048 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4049 NULL)
4050 == -1)
4051 err(1, "pledge");
4052 #endif
4054 limit = get_default_log_limit();
4056 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4057 switch (ch) {
4058 case 'p':
4059 show_patch = 1;
4060 break;
4061 case 'P':
4062 show_changed_paths = 1;
4063 break;
4064 case 'c':
4065 start_commit = optarg;
4066 break;
4067 case 'C':
4068 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4069 &errstr);
4070 if (errstr != NULL)
4071 err(1, "-C option %s", errstr);
4072 break;
4073 case 'l':
4074 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4075 if (errstr != NULL)
4076 err(1, "-l option %s", errstr);
4077 break;
4078 case 'b':
4079 log_branches = 1;
4080 break;
4081 case 'r':
4082 repo_path = realpath(optarg, NULL);
4083 if (repo_path == NULL)
4084 return got_error_from_errno2("realpath",
4085 optarg);
4086 got_path_strip_trailing_slashes(repo_path);
4087 break;
4088 case 'R':
4089 reverse_display_order = 1;
4090 break;
4091 case 's':
4092 search_pattern = optarg;
4093 break;
4094 case 'x':
4095 end_commit = optarg;
4096 break;
4097 default:
4098 usage_log();
4099 /* NOTREACHED */
4103 argc -= optind;
4104 argv += optind;
4106 if (diff_context == -1)
4107 diff_context = 3;
4108 else if (!show_patch)
4109 errx(1, "-C requires -p");
4111 cwd = getcwd(NULL, 0);
4112 if (cwd == NULL) {
4113 error = got_error_from_errno("getcwd");
4114 goto done;
4117 if (repo_path == NULL) {
4118 error = got_worktree_open(&worktree, cwd);
4119 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4120 goto done;
4121 error = NULL;
4124 if (argc == 1) {
4125 if (worktree) {
4126 error = got_worktree_resolve_path(&path, worktree,
4127 argv[0]);
4128 if (error)
4129 goto done;
4130 } else {
4131 path = strdup(argv[0]);
4132 if (path == NULL) {
4133 error = got_error_from_errno("strdup");
4134 goto done;
4137 } else if (argc != 0)
4138 usage_log();
4140 if (repo_path == NULL) {
4141 repo_path = worktree ?
4142 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4144 if (repo_path == NULL) {
4145 error = got_error_from_errno("strdup");
4146 goto done;
4149 error = got_repo_open(&repo, repo_path, NULL);
4150 if (error != NULL)
4151 goto done;
4153 error = apply_unveil(got_repo_get_path(repo), 1,
4154 worktree ? got_worktree_get_root_path(worktree) : NULL);
4155 if (error)
4156 goto done;
4158 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4159 if (error)
4160 goto done;
4162 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4163 if (error)
4164 goto done;
4166 if (start_commit == NULL) {
4167 struct got_reference *head_ref;
4168 struct got_commit_object *commit = NULL;
4169 error = got_ref_open(&head_ref, repo,
4170 worktree ? got_worktree_get_head_ref_name(worktree)
4171 : GOT_REF_HEAD, 0);
4172 if (error != NULL)
4173 goto done;
4174 error = got_ref_resolve(&start_id, repo, head_ref);
4175 got_ref_close(head_ref);
4176 if (error != NULL)
4177 goto done;
4178 error = got_object_open_as_commit(&commit, repo,
4179 start_id);
4180 if (error != NULL)
4181 goto done;
4182 got_object_commit_close(commit);
4183 } else {
4184 error = got_repo_match_object_id(&start_id, NULL,
4185 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4186 if (error != NULL)
4187 goto done;
4189 if (end_commit != NULL) {
4190 error = got_repo_match_object_id(&end_id, NULL,
4191 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4192 if (error != NULL)
4193 goto done;
4196 if (worktree) {
4198 * If a path was specified on the command line it was resolved
4199 * to a path in the work tree above. Prepend the work tree's
4200 * path prefix to obtain the corresponding in-repository path.
4202 if (path) {
4203 const char *prefix;
4204 prefix = got_worktree_get_path_prefix(worktree);
4205 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4206 (path[0] != '\0') ? "/" : "", path) == -1) {
4207 error = got_error_from_errno("asprintf");
4208 goto done;
4211 } else
4212 error = got_repo_map_path(&in_repo_path, repo,
4213 path ? path : "");
4214 if (error != NULL)
4215 goto done;
4216 if (in_repo_path) {
4217 free(path);
4218 path = in_repo_path;
4221 error = print_commits(start_id, end_id, repo, path ? path : "",
4222 show_changed_paths, show_patch, search_pattern, diff_context,
4223 limit, log_branches, reverse_display_order, refs_idmap);
4224 done:
4225 free(path);
4226 free(repo_path);
4227 free(cwd);
4228 if (worktree)
4229 got_worktree_close(worktree);
4230 if (repo) {
4231 const struct got_error *close_err = got_repo_close(repo);
4232 if (error == NULL)
4233 error = close_err;
4235 if (refs_idmap)
4236 got_reflist_object_id_map_free(refs_idmap);
4237 got_ref_list_free(&refs);
4238 return error;
4241 __dead static void
4242 usage_diff(void)
4244 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4245 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4246 exit(1);
4249 struct print_diff_arg {
4250 struct got_repository *repo;
4251 struct got_worktree *worktree;
4252 int diff_context;
4253 const char *id_str;
4254 int header_shown;
4255 int diff_staged;
4256 int ignore_whitespace;
4257 int force_text_diff;
4261 * Create a file which contains the target path of a symlink so we can feed
4262 * it as content to the diff engine.
4264 static const struct got_error *
4265 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4266 const char *abspath)
4268 const struct got_error *err = NULL;
4269 char target_path[PATH_MAX];
4270 ssize_t target_len, outlen;
4272 *fd = -1;
4274 if (dirfd != -1) {
4275 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4276 if (target_len == -1)
4277 return got_error_from_errno2("readlinkat", abspath);
4278 } else {
4279 target_len = readlink(abspath, target_path, PATH_MAX);
4280 if (target_len == -1)
4281 return got_error_from_errno2("readlink", abspath);
4284 *fd = got_opentempfd();
4285 if (*fd == -1)
4286 return got_error_from_errno("got_opentempfd");
4288 outlen = write(*fd, target_path, target_len);
4289 if (outlen == -1) {
4290 err = got_error_from_errno("got_opentempfd");
4291 goto done;
4294 if (lseek(*fd, 0, SEEK_SET) == -1) {
4295 err = got_error_from_errno2("lseek", abspath);
4296 goto done;
4298 done:
4299 if (err) {
4300 close(*fd);
4301 *fd = -1;
4303 return err;
4306 static const struct got_error *
4307 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4308 const char *path, struct got_object_id *blob_id,
4309 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4310 int dirfd, const char *de_name)
4312 struct print_diff_arg *a = arg;
4313 const struct got_error *err = NULL;
4314 struct got_blob_object *blob1 = NULL;
4315 int fd = -1;
4316 FILE *f2 = NULL;
4317 char *abspath = NULL, *label1 = NULL;
4318 struct stat sb;
4320 if (a->diff_staged) {
4321 if (staged_status != GOT_STATUS_MODIFY &&
4322 staged_status != GOT_STATUS_ADD &&
4323 staged_status != GOT_STATUS_DELETE)
4324 return NULL;
4325 } else {
4326 if (staged_status == GOT_STATUS_DELETE)
4327 return NULL;
4328 if (status == GOT_STATUS_NONEXISTENT)
4329 return got_error_set_errno(ENOENT, path);
4330 if (status != GOT_STATUS_MODIFY &&
4331 status != GOT_STATUS_ADD &&
4332 status != GOT_STATUS_DELETE &&
4333 status != GOT_STATUS_CONFLICT)
4334 return NULL;
4337 if (!a->header_shown) {
4338 printf("diff %s %s%s\n", a->id_str,
4339 got_worktree_get_root_path(a->worktree),
4340 a->diff_staged ? " (staged changes)" : "");
4341 a->header_shown = 1;
4344 if (a->diff_staged) {
4345 const char *label1 = NULL, *label2 = NULL;
4346 switch (staged_status) {
4347 case GOT_STATUS_MODIFY:
4348 label1 = path;
4349 label2 = path;
4350 break;
4351 case GOT_STATUS_ADD:
4352 label2 = path;
4353 break;
4354 case GOT_STATUS_DELETE:
4355 label1 = path;
4356 break;
4357 default:
4358 return got_error(GOT_ERR_FILE_STATUS);
4360 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4361 staged_blob_id, label1, label2, a->diff_context,
4362 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4365 if (staged_status == GOT_STATUS_ADD ||
4366 staged_status == GOT_STATUS_MODIFY) {
4367 char *id_str;
4368 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4369 8192);
4370 if (err)
4371 goto done;
4372 err = got_object_id_str(&id_str, staged_blob_id);
4373 if (err)
4374 goto done;
4375 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4376 err = got_error_from_errno("asprintf");
4377 free(id_str);
4378 goto done;
4380 free(id_str);
4381 } else if (status != GOT_STATUS_ADD) {
4382 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4383 if (err)
4384 goto done;
4387 if (status != GOT_STATUS_DELETE) {
4388 if (asprintf(&abspath, "%s/%s",
4389 got_worktree_get_root_path(a->worktree), path) == -1) {
4390 err = got_error_from_errno("asprintf");
4391 goto done;
4394 if (dirfd != -1) {
4395 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4396 if (fd == -1) {
4397 if (!got_err_open_nofollow_on_symlink()) {
4398 err = got_error_from_errno2("openat",
4399 abspath);
4400 goto done;
4402 err = get_symlink_target_file(&fd, dirfd,
4403 de_name, abspath);
4404 if (err)
4405 goto done;
4407 } else {
4408 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4409 if (fd == -1) {
4410 if (!got_err_open_nofollow_on_symlink()) {
4411 err = got_error_from_errno2("open",
4412 abspath);
4413 goto done;
4415 err = get_symlink_target_file(&fd, dirfd,
4416 de_name, abspath);
4417 if (err)
4418 goto done;
4421 if (fstat(fd, &sb) == -1) {
4422 err = got_error_from_errno2("fstat", abspath);
4423 goto done;
4425 f2 = fdopen(fd, "r");
4426 if (f2 == NULL) {
4427 err = got_error_from_errno2("fdopen", abspath);
4428 goto done;
4430 fd = -1;
4431 } else
4432 sb.st_size = 0;
4434 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4435 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4436 done:
4437 if (blob1)
4438 got_object_blob_close(blob1);
4439 if (f2 && fclose(f2) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4441 if (fd != -1 && close(fd) == -1 && err == NULL)
4442 err = got_error_from_errno("close");
4443 free(abspath);
4444 return err;
4447 static const struct got_error *
4448 cmd_diff(int argc, char *argv[])
4450 const struct got_error *error;
4451 struct got_repository *repo = NULL;
4452 struct got_worktree *worktree = NULL;
4453 char *cwd = NULL, *repo_path = NULL;
4454 struct got_object_id *id1 = NULL, *id2 = NULL;
4455 const char *id_str1 = NULL, *id_str2 = NULL;
4456 char *label1 = NULL, *label2 = NULL;
4457 int type1, type2;
4458 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4459 int force_text_diff = 0;
4460 const char *errstr;
4461 char *path = NULL;
4462 struct got_reflist_head refs;
4464 TAILQ_INIT(&refs);
4466 #ifndef PROFILE
4467 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4468 NULL) == -1)
4469 err(1, "pledge");
4470 #endif
4472 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4473 switch (ch) {
4474 case 'a':
4475 force_text_diff = 1;
4476 break;
4477 case 'C':
4478 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4479 &errstr);
4480 if (errstr != NULL)
4481 err(1, "-C option %s", errstr);
4482 break;
4483 case 'r':
4484 repo_path = realpath(optarg, NULL);
4485 if (repo_path == NULL)
4486 return got_error_from_errno2("realpath",
4487 optarg);
4488 got_path_strip_trailing_slashes(repo_path);
4489 break;
4490 case 's':
4491 diff_staged = 1;
4492 break;
4493 case 'w':
4494 ignore_whitespace = 1;
4495 break;
4496 default:
4497 usage_diff();
4498 /* NOTREACHED */
4502 argc -= optind;
4503 argv += optind;
4505 cwd = getcwd(NULL, 0);
4506 if (cwd == NULL) {
4507 error = got_error_from_errno("getcwd");
4508 goto done;
4510 if (argc <= 1) {
4511 if (repo_path)
4512 errx(1,
4513 "-r option can't be used when diffing a work tree");
4514 error = got_worktree_open(&worktree, cwd);
4515 if (error) {
4516 if (error->code == GOT_ERR_NOT_WORKTREE)
4517 error = wrap_not_worktree_error(error, "diff",
4518 cwd);
4519 goto done;
4521 repo_path = strdup(got_worktree_get_repo_path(worktree));
4522 if (repo_path == NULL) {
4523 error = got_error_from_errno("strdup");
4524 goto done;
4526 if (argc == 1) {
4527 error = got_worktree_resolve_path(&path, worktree,
4528 argv[0]);
4529 if (error)
4530 goto done;
4531 } else {
4532 path = strdup("");
4533 if (path == NULL) {
4534 error = got_error_from_errno("strdup");
4535 goto done;
4538 } else if (argc == 2) {
4539 if (diff_staged)
4540 errx(1, "-s option can't be used when diffing "
4541 "objects in repository");
4542 id_str1 = argv[0];
4543 id_str2 = argv[1];
4544 if (repo_path == NULL) {
4545 error = got_worktree_open(&worktree, cwd);
4546 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4547 goto done;
4548 repo_path = strdup(worktree ?
4549 got_worktree_get_repo_path(worktree) : cwd);
4550 if (repo_path == NULL) {
4551 error = got_error_from_errno("strdup");
4552 goto done;
4555 } else
4556 usage_diff();
4558 error = got_repo_open(&repo, repo_path, NULL);
4559 free(repo_path);
4560 if (error != NULL)
4561 goto done;
4563 error = apply_unveil(got_repo_get_path(repo), 1,
4564 worktree ? got_worktree_get_root_path(worktree) : NULL);
4565 if (error)
4566 goto done;
4568 if (argc <= 1) {
4569 struct print_diff_arg arg;
4570 struct got_pathlist_head paths;
4571 char *id_str;
4573 TAILQ_INIT(&paths);
4575 error = got_object_id_str(&id_str,
4576 got_worktree_get_base_commit_id(worktree));
4577 if (error)
4578 goto done;
4579 arg.repo = repo;
4580 arg.worktree = worktree;
4581 arg.diff_context = diff_context;
4582 arg.id_str = id_str;
4583 arg.header_shown = 0;
4584 arg.diff_staged = diff_staged;
4585 arg.ignore_whitespace = ignore_whitespace;
4586 arg.force_text_diff = force_text_diff;
4588 error = got_pathlist_append(&paths, path, NULL);
4589 if (error)
4590 goto done;
4592 error = got_worktree_status(worktree, &paths, repo, 0,
4593 print_diff, &arg, check_cancelled, NULL);
4594 free(id_str);
4595 got_pathlist_free(&paths);
4596 goto done;
4599 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4600 if (error)
4601 return error;
4603 error = got_repo_match_object_id(&id1, &label1, id_str1,
4604 GOT_OBJ_TYPE_ANY, &refs, repo);
4605 if (error)
4606 goto done;
4608 error = got_repo_match_object_id(&id2, &label2, id_str2,
4609 GOT_OBJ_TYPE_ANY, &refs, repo);
4610 if (error)
4611 goto done;
4613 error = got_object_get_type(&type1, repo, id1);
4614 if (error)
4615 goto done;
4617 error = got_object_get_type(&type2, repo, id2);
4618 if (error)
4619 goto done;
4621 if (type1 != type2) {
4622 error = got_error(GOT_ERR_OBJ_TYPE);
4623 goto done;
4626 switch (type1) {
4627 case GOT_OBJ_TYPE_BLOB:
4628 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4629 NULL, NULL, diff_context, ignore_whitespace,
4630 force_text_diff, repo, stdout);
4631 break;
4632 case GOT_OBJ_TYPE_TREE:
4633 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4634 "", "", diff_context, ignore_whitespace, force_text_diff,
4635 repo, stdout);
4636 break;
4637 case GOT_OBJ_TYPE_COMMIT:
4638 printf("diff %s %s\n", label1, label2);
4639 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4640 diff_context, ignore_whitespace, force_text_diff, repo,
4641 stdout);
4642 break;
4643 default:
4644 error = got_error(GOT_ERR_OBJ_TYPE);
4646 done:
4647 free(label1);
4648 free(label2);
4649 free(id1);
4650 free(id2);
4651 free(path);
4652 if (worktree)
4653 got_worktree_close(worktree);
4654 if (repo) {
4655 const struct got_error *close_err = got_repo_close(repo);
4656 if (error == NULL)
4657 error = close_err;
4659 got_ref_list_free(&refs);
4660 return error;
4663 __dead static void
4664 usage_blame(void)
4666 fprintf(stderr,
4667 "usage: %s blame [-c commit] [-r repository-path] path\n",
4668 getprogname());
4669 exit(1);
4672 struct blame_line {
4673 int annotated;
4674 char *id_str;
4675 char *committer;
4676 char datebuf[11]; /* YYYY-MM-DD + NUL */
4679 struct blame_cb_args {
4680 struct blame_line *lines;
4681 int nlines;
4682 int nlines_prec;
4683 int lineno_cur;
4684 off_t *line_offsets;
4685 FILE *f;
4686 struct got_repository *repo;
4689 static const struct got_error *
4690 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4692 const struct got_error *err = NULL;
4693 struct blame_cb_args *a = arg;
4694 struct blame_line *bline;
4695 char *line = NULL;
4696 size_t linesize = 0;
4697 struct got_commit_object *commit = NULL;
4698 off_t offset;
4699 struct tm tm;
4700 time_t committer_time;
4702 if (nlines != a->nlines ||
4703 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4704 return got_error(GOT_ERR_RANGE);
4706 if (sigint_received)
4707 return got_error(GOT_ERR_ITER_COMPLETED);
4709 if (lineno == -1)
4710 return NULL; /* no change in this commit */
4712 /* Annotate this line. */
4713 bline = &a->lines[lineno - 1];
4714 if (bline->annotated)
4715 return NULL;
4716 err = got_object_id_str(&bline->id_str, id);
4717 if (err)
4718 return err;
4720 err = got_object_open_as_commit(&commit, a->repo, id);
4721 if (err)
4722 goto done;
4724 bline->committer = strdup(got_object_commit_get_committer(commit));
4725 if (bline->committer == NULL) {
4726 err = got_error_from_errno("strdup");
4727 goto done;
4730 committer_time = got_object_commit_get_committer_time(commit);
4731 if (gmtime_r(&committer_time, &tm) == NULL)
4732 return got_error_from_errno("gmtime_r");
4733 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4734 &tm) == 0) {
4735 err = got_error(GOT_ERR_NO_SPACE);
4736 goto done;
4738 bline->annotated = 1;
4740 /* Print lines annotated so far. */
4741 bline = &a->lines[a->lineno_cur - 1];
4742 if (!bline->annotated)
4743 goto done;
4745 offset = a->line_offsets[a->lineno_cur - 1];
4746 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4747 err = got_error_from_errno("fseeko");
4748 goto done;
4751 while (bline->annotated) {
4752 char *smallerthan, *at, *nl, *committer;
4753 size_t len;
4755 if (getline(&line, &linesize, a->f) == -1) {
4756 if (ferror(a->f))
4757 err = got_error_from_errno("getline");
4758 break;
4761 committer = bline->committer;
4762 smallerthan = strchr(committer, '<');
4763 if (smallerthan && smallerthan[1] != '\0')
4764 committer = smallerthan + 1;
4765 at = strchr(committer, '@');
4766 if (at)
4767 *at = '\0';
4768 len = strlen(committer);
4769 if (len >= 9)
4770 committer[8] = '\0';
4772 nl = strchr(line, '\n');
4773 if (nl)
4774 *nl = '\0';
4775 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4776 bline->id_str, bline->datebuf, committer, line);
4778 a->lineno_cur++;
4779 bline = &a->lines[a->lineno_cur - 1];
4781 done:
4782 if (commit)
4783 got_object_commit_close(commit);
4784 free(line);
4785 return err;
4788 static const struct got_error *
4789 cmd_blame(int argc, char *argv[])
4791 const struct got_error *error;
4792 struct got_repository *repo = NULL;
4793 struct got_worktree *worktree = NULL;
4794 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4795 char *link_target = NULL;
4796 struct got_object_id *obj_id = NULL;
4797 struct got_object_id *commit_id = NULL;
4798 struct got_blob_object *blob = NULL;
4799 char *commit_id_str = NULL;
4800 struct blame_cb_args bca;
4801 int ch, obj_type, i;
4802 off_t filesize;
4804 memset(&bca, 0, sizeof(bca));
4806 #ifndef PROFILE
4807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4808 NULL) == -1)
4809 err(1, "pledge");
4810 #endif
4812 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4813 switch (ch) {
4814 case 'c':
4815 commit_id_str = optarg;
4816 break;
4817 case 'r':
4818 repo_path = realpath(optarg, NULL);
4819 if (repo_path == NULL)
4820 return got_error_from_errno2("realpath",
4821 optarg);
4822 got_path_strip_trailing_slashes(repo_path);
4823 break;
4824 default:
4825 usage_blame();
4826 /* NOTREACHED */
4830 argc -= optind;
4831 argv += optind;
4833 if (argc == 1)
4834 path = argv[0];
4835 else
4836 usage_blame();
4838 cwd = getcwd(NULL, 0);
4839 if (cwd == NULL) {
4840 error = got_error_from_errno("getcwd");
4841 goto done;
4843 if (repo_path == NULL) {
4844 error = got_worktree_open(&worktree, cwd);
4845 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4846 goto done;
4847 else
4848 error = NULL;
4849 if (worktree) {
4850 repo_path =
4851 strdup(got_worktree_get_repo_path(worktree));
4852 if (repo_path == NULL) {
4853 error = got_error_from_errno("strdup");
4854 if (error)
4855 goto done;
4857 } else {
4858 repo_path = strdup(cwd);
4859 if (repo_path == NULL) {
4860 error = got_error_from_errno("strdup");
4861 goto done;
4866 error = got_repo_open(&repo, repo_path, NULL);
4867 if (error != NULL)
4868 goto done;
4870 if (worktree) {
4871 const char *prefix = got_worktree_get_path_prefix(worktree);
4872 char *p;
4874 error = got_worktree_resolve_path(&p, worktree, path);
4875 if (error)
4876 goto done;
4877 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4878 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4879 p) == -1) {
4880 error = got_error_from_errno("asprintf");
4881 free(p);
4882 goto done;
4884 free(p);
4885 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4886 } else {
4887 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4888 if (error)
4889 goto done;
4890 error = got_repo_map_path(&in_repo_path, repo, path);
4892 if (error)
4893 goto done;
4895 if (commit_id_str == NULL) {
4896 struct got_reference *head_ref;
4897 error = got_ref_open(&head_ref, repo, worktree ?
4898 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4899 if (error != NULL)
4900 goto done;
4901 error = got_ref_resolve(&commit_id, repo, head_ref);
4902 got_ref_close(head_ref);
4903 if (error != NULL)
4904 goto done;
4905 } else {
4906 struct got_reflist_head refs;
4907 TAILQ_INIT(&refs);
4908 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4909 NULL);
4910 if (error)
4911 goto done;
4912 error = got_repo_match_object_id(&commit_id, NULL,
4913 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4914 got_ref_list_free(&refs);
4915 if (error)
4916 goto done;
4919 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4920 commit_id, repo);
4921 if (error)
4922 goto done;
4924 error = got_object_id_by_path(&obj_id, repo, commit_id,
4925 link_target ? link_target : in_repo_path);
4926 if (error)
4927 goto done;
4929 error = got_object_get_type(&obj_type, repo, obj_id);
4930 if (error)
4931 goto done;
4933 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4934 error = got_error_path(link_target ? link_target : in_repo_path,
4935 GOT_ERR_OBJ_TYPE);
4936 goto done;
4939 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4940 if (error)
4941 goto done;
4942 bca.f = got_opentemp();
4943 if (bca.f == NULL) {
4944 error = got_error_from_errno("got_opentemp");
4945 goto done;
4947 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4948 &bca.line_offsets, bca.f, blob);
4949 if (error || bca.nlines == 0)
4950 goto done;
4952 /* Don't include \n at EOF in the blame line count. */
4953 if (bca.line_offsets[bca.nlines - 1] == filesize)
4954 bca.nlines--;
4956 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4957 if (bca.lines == NULL) {
4958 error = got_error_from_errno("calloc");
4959 goto done;
4961 bca.lineno_cur = 1;
4962 bca.nlines_prec = 0;
4963 i = bca.nlines;
4964 while (i > 0) {
4965 i /= 10;
4966 bca.nlines_prec++;
4968 bca.repo = repo;
4970 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4971 repo, blame_cb, &bca, check_cancelled, NULL);
4972 done:
4973 free(in_repo_path);
4974 free(link_target);
4975 free(repo_path);
4976 free(cwd);
4977 free(commit_id);
4978 free(obj_id);
4979 if (blob)
4980 got_object_blob_close(blob);
4981 if (worktree)
4982 got_worktree_close(worktree);
4983 if (repo) {
4984 const struct got_error *close_err = got_repo_close(repo);
4985 if (error == NULL)
4986 error = close_err;
4988 if (bca.lines) {
4989 for (i = 0; i < bca.nlines; i++) {
4990 struct blame_line *bline = &bca.lines[i];
4991 free(bline->id_str);
4992 free(bline->committer);
4994 free(bca.lines);
4996 free(bca.line_offsets);
4997 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4998 error = got_error_from_errno("fclose");
4999 return error;
5002 __dead static void
5003 usage_tree(void)
5005 fprintf(stderr,
5006 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5007 getprogname());
5008 exit(1);
5011 static const struct got_error *
5012 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5013 const char *root_path, struct got_repository *repo)
5015 const struct got_error *err = NULL;
5016 int is_root_path = (strcmp(path, root_path) == 0);
5017 const char *modestr = "";
5018 mode_t mode = got_tree_entry_get_mode(te);
5019 char *link_target = NULL;
5021 path += strlen(root_path);
5022 while (path[0] == '/')
5023 path++;
5025 if (got_object_tree_entry_is_submodule(te))
5026 modestr = "$";
5027 else if (S_ISLNK(mode)) {
5028 int i;
5030 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5031 if (err)
5032 return err;
5033 for (i = 0; i < strlen(link_target); i++) {
5034 if (!isprint((unsigned char)link_target[i]))
5035 link_target[i] = '?';
5038 modestr = "@";
5040 else if (S_ISDIR(mode))
5041 modestr = "/";
5042 else if (mode & S_IXUSR)
5043 modestr = "*";
5045 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5046 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5047 link_target ? " -> ": "", link_target ? link_target : "");
5049 free(link_target);
5050 return NULL;
5053 static const struct got_error *
5054 print_tree(const char *path, struct got_object_id *commit_id,
5055 int show_ids, int recurse, const char *root_path,
5056 struct got_repository *repo)
5058 const struct got_error *err = NULL;
5059 struct got_object_id *tree_id = NULL;
5060 struct got_tree_object *tree = NULL;
5061 int nentries, i;
5063 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5064 if (err)
5065 goto done;
5067 err = got_object_open_as_tree(&tree, repo, tree_id);
5068 if (err)
5069 goto done;
5070 nentries = got_object_tree_get_nentries(tree);
5071 for (i = 0; i < nentries; i++) {
5072 struct got_tree_entry *te;
5073 char *id = NULL;
5075 if (sigint_received || sigpipe_received)
5076 break;
5078 te = got_object_tree_get_entry(tree, i);
5079 if (show_ids) {
5080 char *id_str;
5081 err = got_object_id_str(&id_str,
5082 got_tree_entry_get_id(te));
5083 if (err)
5084 goto done;
5085 if (asprintf(&id, "%s ", id_str) == -1) {
5086 err = got_error_from_errno("asprintf");
5087 free(id_str);
5088 goto done;
5090 free(id_str);
5092 err = print_entry(te, id, path, root_path, repo);
5093 free(id);
5094 if (err)
5095 goto done;
5097 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5098 char *child_path;
5099 if (asprintf(&child_path, "%s%s%s", path,
5100 path[0] == '/' && path[1] == '\0' ? "" : "/",
5101 got_tree_entry_get_name(te)) == -1) {
5102 err = got_error_from_errno("asprintf");
5103 goto done;
5105 err = print_tree(child_path, commit_id, show_ids, 1,
5106 root_path, repo);
5107 free(child_path);
5108 if (err)
5109 goto done;
5112 done:
5113 if (tree)
5114 got_object_tree_close(tree);
5115 free(tree_id);
5116 return err;
5119 static const struct got_error *
5120 cmd_tree(int argc, char *argv[])
5122 const struct got_error *error;
5123 struct got_repository *repo = NULL;
5124 struct got_worktree *worktree = NULL;
5125 const char *path, *refname = NULL;
5126 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5127 struct got_object_id *commit_id = NULL;
5128 char *commit_id_str = NULL;
5129 int show_ids = 0, recurse = 0;
5130 int ch;
5132 #ifndef PROFILE
5133 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5134 NULL) == -1)
5135 err(1, "pledge");
5136 #endif
5138 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5139 switch (ch) {
5140 case 'c':
5141 commit_id_str = optarg;
5142 break;
5143 case 'r':
5144 repo_path = realpath(optarg, NULL);
5145 if (repo_path == NULL)
5146 return got_error_from_errno2("realpath",
5147 optarg);
5148 got_path_strip_trailing_slashes(repo_path);
5149 break;
5150 case 'i':
5151 show_ids = 1;
5152 break;
5153 case 'R':
5154 recurse = 1;
5155 break;
5156 default:
5157 usage_tree();
5158 /* NOTREACHED */
5162 argc -= optind;
5163 argv += optind;
5165 if (argc == 1)
5166 path = argv[0];
5167 else if (argc > 1)
5168 usage_tree();
5169 else
5170 path = NULL;
5172 cwd = getcwd(NULL, 0);
5173 if (cwd == NULL) {
5174 error = got_error_from_errno("getcwd");
5175 goto done;
5177 if (repo_path == NULL) {
5178 error = got_worktree_open(&worktree, cwd);
5179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5180 goto done;
5181 else
5182 error = NULL;
5183 if (worktree) {
5184 repo_path =
5185 strdup(got_worktree_get_repo_path(worktree));
5186 if (repo_path == NULL)
5187 error = got_error_from_errno("strdup");
5188 if (error)
5189 goto done;
5190 } else {
5191 repo_path = strdup(cwd);
5192 if (repo_path == NULL) {
5193 error = got_error_from_errno("strdup");
5194 goto done;
5199 error = got_repo_open(&repo, repo_path, NULL);
5200 if (error != NULL)
5201 goto done;
5203 if (worktree) {
5204 const char *prefix = got_worktree_get_path_prefix(worktree);
5205 char *p;
5207 if (path == NULL)
5208 path = "";
5209 error = got_worktree_resolve_path(&p, worktree, path);
5210 if (error)
5211 goto done;
5212 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5213 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5214 p) == -1) {
5215 error = got_error_from_errno("asprintf");
5216 free(p);
5217 goto done;
5219 free(p);
5220 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5221 if (error)
5222 goto done;
5223 } else {
5224 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5225 if (error)
5226 goto done;
5227 if (path == NULL)
5228 path = "/";
5229 error = got_repo_map_path(&in_repo_path, repo, path);
5230 if (error != NULL)
5231 goto done;
5234 if (commit_id_str == NULL) {
5235 struct got_reference *head_ref;
5236 if (worktree)
5237 refname = got_worktree_get_head_ref_name(worktree);
5238 else
5239 refname = GOT_REF_HEAD;
5240 error = got_ref_open(&head_ref, repo, refname, 0);
5241 if (error != NULL)
5242 goto done;
5243 error = got_ref_resolve(&commit_id, repo, head_ref);
5244 got_ref_close(head_ref);
5245 if (error != NULL)
5246 goto done;
5247 } else {
5248 struct got_reflist_head refs;
5249 TAILQ_INIT(&refs);
5250 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5251 NULL);
5252 if (error)
5253 goto done;
5254 error = got_repo_match_object_id(&commit_id, NULL,
5255 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5256 got_ref_list_free(&refs);
5257 if (error)
5258 goto done;
5261 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5262 in_repo_path, repo);
5263 done:
5264 free(in_repo_path);
5265 free(repo_path);
5266 free(cwd);
5267 free(commit_id);
5268 if (worktree)
5269 got_worktree_close(worktree);
5270 if (repo) {
5271 const struct got_error *close_err = got_repo_close(repo);
5272 if (error == NULL)
5273 error = close_err;
5275 return error;
5278 __dead static void
5279 usage_status(void)
5281 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5282 "[-S status-codes] [path ...]\n", getprogname());
5283 exit(1);
5286 struct got_status_arg {
5287 char *status_codes;
5288 int suppress;
5291 static const struct got_error *
5292 print_status(void *arg, unsigned char status, unsigned char staged_status,
5293 const char *path, struct got_object_id *blob_id,
5294 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5295 int dirfd, const char *de_name)
5297 struct got_status_arg *st = arg;
5299 if (status == staged_status && (status == GOT_STATUS_DELETE))
5300 status = GOT_STATUS_NO_CHANGE;
5301 if (st != NULL && st->status_codes) {
5302 size_t ncodes = strlen(st->status_codes);
5303 int i, j = 0;
5305 for (i = 0; i < ncodes ; i++) {
5306 if (st->suppress) {
5307 if (status == st->status_codes[i] ||
5308 staged_status == st->status_codes[i]) {
5309 j++;
5310 continue;
5312 } else {
5313 if (status == st->status_codes[i] ||
5314 staged_status == st->status_codes[i])
5315 break;
5319 if (st->suppress && j == 0)
5320 goto print;
5322 if (i == ncodes)
5323 return NULL;
5325 print:
5326 printf("%c%c %s\n", status, staged_status, path);
5327 return NULL;
5330 static const struct got_error *
5331 cmd_status(int argc, char *argv[])
5333 const struct got_error *error = NULL;
5334 struct got_repository *repo = NULL;
5335 struct got_worktree *worktree = NULL;
5336 struct got_status_arg st;
5337 char *cwd = NULL;
5338 struct got_pathlist_head paths;
5339 struct got_pathlist_entry *pe;
5340 int ch, i, no_ignores = 0;
5342 TAILQ_INIT(&paths);
5344 memset(&st, 0, sizeof(st));
5345 st.status_codes = NULL;
5346 st.suppress = 0;
5348 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5349 switch (ch) {
5350 case 'I':
5351 no_ignores = 1;
5352 break;
5353 case 'S':
5354 if (st.status_codes != NULL && st.suppress == 0)
5355 option_conflict('S', 's');
5356 st.suppress = 1;
5357 /* fallthrough */
5358 case 's':
5359 for (i = 0; i < strlen(optarg); i++) {
5360 switch (optarg[i]) {
5361 case GOT_STATUS_MODIFY:
5362 case GOT_STATUS_ADD:
5363 case GOT_STATUS_DELETE:
5364 case GOT_STATUS_CONFLICT:
5365 case GOT_STATUS_MISSING:
5366 case GOT_STATUS_OBSTRUCTED:
5367 case GOT_STATUS_UNVERSIONED:
5368 case GOT_STATUS_MODE_CHANGE:
5369 case GOT_STATUS_NONEXISTENT:
5370 break;
5371 default:
5372 errx(1, "invalid status code '%c'",
5373 optarg[i]);
5376 if (ch == 's' && st.suppress)
5377 option_conflict('s', 'S');
5378 st.status_codes = optarg;
5379 break;
5380 default:
5381 usage_status();
5382 /* NOTREACHED */
5386 argc -= optind;
5387 argv += optind;
5389 #ifndef PROFILE
5390 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5391 NULL) == -1)
5392 err(1, "pledge");
5393 #endif
5394 cwd = getcwd(NULL, 0);
5395 if (cwd == NULL) {
5396 error = got_error_from_errno("getcwd");
5397 goto done;
5400 error = got_worktree_open(&worktree, cwd);
5401 if (error) {
5402 if (error->code == GOT_ERR_NOT_WORKTREE)
5403 error = wrap_not_worktree_error(error, "status", cwd);
5404 goto done;
5407 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5408 NULL);
5409 if (error != NULL)
5410 goto done;
5412 error = apply_unveil(got_repo_get_path(repo), 1,
5413 got_worktree_get_root_path(worktree));
5414 if (error)
5415 goto done;
5417 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5418 if (error)
5419 goto done;
5421 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5422 print_status, &st, check_cancelled, NULL);
5423 done:
5424 TAILQ_FOREACH(pe, &paths, entry)
5425 free((char *)pe->path);
5426 got_pathlist_free(&paths);
5427 free(cwd);
5428 return error;
5431 __dead static void
5432 usage_ref(void)
5434 fprintf(stderr,
5435 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5436 "[-d] [name]\n",
5437 getprogname());
5438 exit(1);
5441 static const struct got_error *
5442 list_refs(struct got_repository *repo, const char *refname)
5444 static const struct got_error *err = NULL;
5445 struct got_reflist_head refs;
5446 struct got_reflist_entry *re;
5448 TAILQ_INIT(&refs);
5449 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5450 if (err)
5451 return err;
5453 TAILQ_FOREACH(re, &refs, entry) {
5454 char *refstr;
5455 refstr = got_ref_to_str(re->ref);
5456 if (refstr == NULL)
5457 return got_error_from_errno("got_ref_to_str");
5458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5459 free(refstr);
5462 got_ref_list_free(&refs);
5463 return NULL;
5466 static const struct got_error *
5467 delete_ref_by_name(struct got_repository *repo, const char *refname)
5469 const struct got_error *err;
5470 struct got_reference *ref;
5472 err = got_ref_open(&ref, repo, refname, 0);
5473 if (err)
5474 return err;
5476 err = delete_ref(repo, ref);
5477 got_ref_close(ref);
5478 return err;
5481 static const struct got_error *
5482 add_ref(struct got_repository *repo, const char *refname, const char *target)
5484 const struct got_error *err = NULL;
5485 struct got_object_id *id;
5486 struct got_reference *ref = NULL;
5489 * Don't let the user create a reference name with a leading '-'.
5490 * While technically a valid reference name, this case is usually
5491 * an unintended typo.
5493 if (refname[0] == '-')
5494 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5497 repo);
5498 if (err) {
5499 struct got_reference *target_ref;
5501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5502 return err;
5503 err = got_ref_open(&target_ref, repo, target, 0);
5504 if (err)
5505 return err;
5506 err = got_ref_resolve(&id, repo, target_ref);
5507 got_ref_close(target_ref);
5508 if (err)
5509 return err;
5512 err = got_ref_alloc(&ref, refname, id);
5513 if (err)
5514 goto done;
5516 err = got_ref_write(ref, repo);
5517 done:
5518 if (ref)
5519 got_ref_close(ref);
5520 free(id);
5521 return err;
5524 static const struct got_error *
5525 add_symref(struct got_repository *repo, const char *refname, const char *target)
5527 const struct got_error *err = NULL;
5528 struct got_reference *ref = NULL;
5529 struct got_reference *target_ref = NULL;
5532 * Don't let the user create a reference name with a leading '-'.
5533 * While technically a valid reference name, this case is usually
5534 * an unintended typo.
5536 if (refname[0] == '-')
5537 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5539 err = got_ref_open(&target_ref, repo, target, 0);
5540 if (err)
5541 return err;
5543 err = got_ref_alloc_symref(&ref, refname, target_ref);
5544 if (err)
5545 goto done;
5547 err = got_ref_write(ref, repo);
5548 done:
5549 if (target_ref)
5550 got_ref_close(target_ref);
5551 if (ref)
5552 got_ref_close(ref);
5553 return err;
5556 static const struct got_error *
5557 cmd_ref(int argc, char *argv[])
5559 const struct got_error *error = NULL;
5560 struct got_repository *repo = NULL;
5561 struct got_worktree *worktree = NULL;
5562 char *cwd = NULL, *repo_path = NULL;
5563 int ch, do_list = 0, do_delete = 0;
5564 const char *obj_arg = NULL, *symref_target= NULL;
5565 char *refname = NULL;
5567 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5568 switch (ch) {
5569 case 'c':
5570 obj_arg = optarg;
5571 break;
5572 case 'd':
5573 do_delete = 1;
5574 break;
5575 case 'r':
5576 repo_path = realpath(optarg, NULL);
5577 if (repo_path == NULL)
5578 return got_error_from_errno2("realpath",
5579 optarg);
5580 got_path_strip_trailing_slashes(repo_path);
5581 break;
5582 case 'l':
5583 do_list = 1;
5584 break;
5585 case 's':
5586 symref_target = optarg;
5587 break;
5588 default:
5589 usage_ref();
5590 /* NOTREACHED */
5594 if (obj_arg && do_list)
5595 option_conflict('c', 'l');
5596 if (obj_arg && do_delete)
5597 option_conflict('c', 'd');
5598 if (obj_arg && symref_target)
5599 option_conflict('c', 's');
5600 if (symref_target && do_delete)
5601 option_conflict('s', 'd');
5602 if (symref_target && do_list)
5603 option_conflict('s', 'l');
5604 if (do_delete && do_list)
5605 option_conflict('d', 'l');
5607 argc -= optind;
5608 argv += optind;
5610 if (do_list) {
5611 if (argc != 0 && argc != 1)
5612 usage_ref();
5613 if (argc == 1) {
5614 refname = strdup(argv[0]);
5615 if (refname == NULL) {
5616 error = got_error_from_errno("strdup");
5617 goto done;
5620 } else {
5621 if (argc != 1)
5622 usage_ref();
5623 refname = strdup(argv[0]);
5624 if (refname == NULL) {
5625 error = got_error_from_errno("strdup");
5626 goto done;
5630 if (refname)
5631 got_path_strip_trailing_slashes(refname);
5633 #ifndef PROFILE
5634 if (do_list) {
5635 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5636 NULL) == -1)
5637 err(1, "pledge");
5638 } else {
5639 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5640 "sendfd unveil", NULL) == -1)
5641 err(1, "pledge");
5643 #endif
5644 cwd = getcwd(NULL, 0);
5645 if (cwd == NULL) {
5646 error = got_error_from_errno("getcwd");
5647 goto done;
5650 if (repo_path == NULL) {
5651 error = got_worktree_open(&worktree, cwd);
5652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5653 goto done;
5654 else
5655 error = NULL;
5656 if (worktree) {
5657 repo_path =
5658 strdup(got_worktree_get_repo_path(worktree));
5659 if (repo_path == NULL)
5660 error = got_error_from_errno("strdup");
5661 if (error)
5662 goto done;
5663 } else {
5664 repo_path = strdup(cwd);
5665 if (repo_path == NULL) {
5666 error = got_error_from_errno("strdup");
5667 goto done;
5672 error = got_repo_open(&repo, repo_path, NULL);
5673 if (error != NULL)
5674 goto done;
5676 error = apply_unveil(got_repo_get_path(repo), do_list,
5677 worktree ? got_worktree_get_root_path(worktree) : NULL);
5678 if (error)
5679 goto done;
5681 if (do_list)
5682 error = list_refs(repo, refname);
5683 else if (do_delete)
5684 error = delete_ref_by_name(repo, refname);
5685 else if (symref_target)
5686 error = add_symref(repo, refname, symref_target);
5687 else {
5688 if (obj_arg == NULL)
5689 usage_ref();
5690 error = add_ref(repo, refname, obj_arg);
5692 done:
5693 free(refname);
5694 if (repo) {
5695 const struct got_error *close_err = got_repo_close(repo);
5696 if (error == NULL)
5697 error = close_err;
5699 if (worktree)
5700 got_worktree_close(worktree);
5701 free(cwd);
5702 free(repo_path);
5703 return error;
5706 __dead static void
5707 usage_branch(void)
5709 fprintf(stderr,
5710 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5711 "[name]\n", getprogname());
5712 exit(1);
5715 static const struct got_error *
5716 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5717 struct got_reference *ref)
5719 const struct got_error *err = NULL;
5720 const char *refname, *marker = " ";
5721 char *refstr;
5723 refname = got_ref_get_name(ref);
5724 if (worktree && strcmp(refname,
5725 got_worktree_get_head_ref_name(worktree)) == 0) {
5726 struct got_object_id *id = NULL;
5728 err = got_ref_resolve(&id, repo, ref);
5729 if (err)
5730 return err;
5731 if (got_object_id_cmp(id,
5732 got_worktree_get_base_commit_id(worktree)) == 0)
5733 marker = "* ";
5734 else
5735 marker = "~ ";
5736 free(id);
5739 if (strncmp(refname, "refs/heads/", 11) == 0)
5740 refname += 11;
5741 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5742 refname += 18;
5743 if (strncmp(refname, "refs/remotes/", 13) == 0)
5744 refname += 13;
5746 refstr = got_ref_to_str(ref);
5747 if (refstr == NULL)
5748 return got_error_from_errno("got_ref_to_str");
5750 printf("%s%s: %s\n", marker, refname, refstr);
5751 free(refstr);
5752 return NULL;
5755 static const struct got_error *
5756 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5758 const char *refname;
5760 if (worktree == NULL)
5761 return got_error(GOT_ERR_NOT_WORKTREE);
5763 refname = got_worktree_get_head_ref_name(worktree);
5765 if (strncmp(refname, "refs/heads/", 11) == 0)
5766 refname += 11;
5767 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5768 refname += 18;
5770 printf("%s\n", refname);
5772 return NULL;
5775 static const struct got_error *
5776 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5778 static const struct got_error *err = NULL;
5779 struct got_reflist_head refs;
5780 struct got_reflist_entry *re;
5781 struct got_reference *temp_ref = NULL;
5782 int rebase_in_progress, histedit_in_progress;
5784 TAILQ_INIT(&refs);
5786 if (worktree) {
5787 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5788 worktree);
5789 if (err)
5790 return err;
5792 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5793 worktree);
5794 if (err)
5795 return err;
5797 if (rebase_in_progress || histedit_in_progress) {
5798 err = got_ref_open(&temp_ref, repo,
5799 got_worktree_get_head_ref_name(worktree), 0);
5800 if (err)
5801 return err;
5802 list_branch(repo, worktree, temp_ref);
5803 got_ref_close(temp_ref);
5807 err = got_ref_list(&refs, repo, "refs/heads",
5808 got_ref_cmp_by_name, NULL);
5809 if (err)
5810 return err;
5812 TAILQ_FOREACH(re, &refs, entry)
5813 list_branch(repo, worktree, re->ref);
5815 got_ref_list_free(&refs);
5817 err = got_ref_list(&refs, repo, "refs/remotes",
5818 got_ref_cmp_by_name, NULL);
5819 if (err)
5820 return err;
5822 TAILQ_FOREACH(re, &refs, entry)
5823 list_branch(repo, worktree, re->ref);
5825 got_ref_list_free(&refs);
5827 return NULL;
5830 static const struct got_error *
5831 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5832 const char *branch_name)
5834 const struct got_error *err = NULL;
5835 struct got_reference *ref = NULL;
5836 char *refname, *remote_refname = NULL;
5838 if (strncmp(branch_name, "refs/", 5) == 0)
5839 branch_name += 5;
5840 if (strncmp(branch_name, "heads/", 6) == 0)
5841 branch_name += 6;
5842 else if (strncmp(branch_name, "remotes/", 8) == 0)
5843 branch_name += 8;
5845 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5846 return got_error_from_errno("asprintf");
5848 if (asprintf(&remote_refname, "refs/remotes/%s",
5849 branch_name) == -1) {
5850 err = got_error_from_errno("asprintf");
5851 goto done;
5854 err = got_ref_open(&ref, repo, refname, 0);
5855 if (err) {
5856 const struct got_error *err2;
5857 if (err->code != GOT_ERR_NOT_REF)
5858 goto done;
5860 * Keep 'err' intact such that if neither branch exists
5861 * we report "refs/heads" rather than "refs/remotes" in
5862 * our error message.
5864 err2 = got_ref_open(&ref, repo, remote_refname, 0);
5865 if (err2)
5866 goto done;
5867 err = NULL;
5870 if (worktree &&
5871 strcmp(got_worktree_get_head_ref_name(worktree),
5872 got_ref_get_name(ref)) == 0) {
5873 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5874 "will not delete this work tree's current branch");
5875 goto done;
5878 err = delete_ref(repo, ref);
5879 done:
5880 if (ref)
5881 got_ref_close(ref);
5882 free(refname);
5883 free(remote_refname);
5884 return err;
5887 static const struct got_error *
5888 add_branch(struct got_repository *repo, const char *branch_name,
5889 struct got_object_id *base_commit_id)
5891 const struct got_error *err = NULL;
5892 struct got_reference *ref = NULL;
5893 char *base_refname = NULL, *refname = NULL;
5896 * Don't let the user create a branch name with a leading '-'.
5897 * While technically a valid reference name, this case is usually
5898 * an unintended typo.
5900 if (branch_name[0] == '-')
5901 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5903 if (strncmp(branch_name, "refs/heads/", 11) == 0)
5904 branch_name += 11;
5906 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5907 err = got_error_from_errno("asprintf");
5908 goto done;
5911 err = got_ref_open(&ref, repo, refname, 0);
5912 if (err == NULL) {
5913 err = got_error(GOT_ERR_BRANCH_EXISTS);
5914 goto done;
5915 } else if (err->code != GOT_ERR_NOT_REF)
5916 goto done;
5918 err = got_ref_alloc(&ref, refname, base_commit_id);
5919 if (err)
5920 goto done;
5922 err = got_ref_write(ref, repo);
5923 done:
5924 if (ref)
5925 got_ref_close(ref);
5926 free(base_refname);
5927 free(refname);
5928 return err;
5931 static const struct got_error *
5932 cmd_branch(int argc, char *argv[])
5934 const struct got_error *error = NULL;
5935 struct got_repository *repo = NULL;
5936 struct got_worktree *worktree = NULL;
5937 char *cwd = NULL, *repo_path = NULL;
5938 int ch, do_list = 0, do_show = 0, do_update = 1;
5939 const char *delref = NULL, *commit_id_arg = NULL;
5940 struct got_reference *ref = NULL;
5941 struct got_pathlist_head paths;
5942 struct got_pathlist_entry *pe;
5943 struct got_object_id *commit_id = NULL;
5944 char *commit_id_str = NULL;
5946 TAILQ_INIT(&paths);
5948 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5949 switch (ch) {
5950 case 'c':
5951 commit_id_arg = optarg;
5952 break;
5953 case 'd':
5954 delref = optarg;
5955 break;
5956 case 'r':
5957 repo_path = realpath(optarg, NULL);
5958 if (repo_path == NULL)
5959 return got_error_from_errno2("realpath",
5960 optarg);
5961 got_path_strip_trailing_slashes(repo_path);
5962 break;
5963 case 'l':
5964 do_list = 1;
5965 break;
5966 case 'n':
5967 do_update = 0;
5968 break;
5969 default:
5970 usage_branch();
5971 /* NOTREACHED */
5975 if (do_list && delref)
5976 option_conflict('l', 'd');
5978 argc -= optind;
5979 argv += optind;
5981 if (!do_list && !delref && argc == 0)
5982 do_show = 1;
5984 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5985 errx(1, "-c option can only be used when creating a branch");
5987 if (do_list || delref) {
5988 if (argc > 0)
5989 usage_branch();
5990 } else if (!do_show && argc != 1)
5991 usage_branch();
5993 #ifndef PROFILE
5994 if (do_list || do_show) {
5995 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5996 NULL) == -1)
5997 err(1, "pledge");
5998 } else {
5999 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6000 "sendfd unveil", NULL) == -1)
6001 err(1, "pledge");
6003 #endif
6004 cwd = getcwd(NULL, 0);
6005 if (cwd == NULL) {
6006 error = got_error_from_errno("getcwd");
6007 goto done;
6010 if (repo_path == NULL) {
6011 error = got_worktree_open(&worktree, cwd);
6012 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6013 goto done;
6014 else
6015 error = NULL;
6016 if (worktree) {
6017 repo_path =
6018 strdup(got_worktree_get_repo_path(worktree));
6019 if (repo_path == NULL)
6020 error = got_error_from_errno("strdup");
6021 if (error)
6022 goto done;
6023 } else {
6024 repo_path = strdup(cwd);
6025 if (repo_path == NULL) {
6026 error = got_error_from_errno("strdup");
6027 goto done;
6032 error = got_repo_open(&repo, repo_path, NULL);
6033 if (error != NULL)
6034 goto done;
6036 error = apply_unveil(got_repo_get_path(repo), do_list,
6037 worktree ? got_worktree_get_root_path(worktree) : NULL);
6038 if (error)
6039 goto done;
6041 if (do_show)
6042 error = show_current_branch(repo, worktree);
6043 else if (do_list)
6044 error = list_branches(repo, worktree);
6045 else if (delref)
6046 error = delete_branch(repo, worktree, delref);
6047 else {
6048 struct got_reflist_head refs;
6049 TAILQ_INIT(&refs);
6050 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6051 NULL);
6052 if (error)
6053 goto done;
6054 if (commit_id_arg == NULL)
6055 commit_id_arg = worktree ?
6056 got_worktree_get_head_ref_name(worktree) :
6057 GOT_REF_HEAD;
6058 error = got_repo_match_object_id(&commit_id, NULL,
6059 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6060 got_ref_list_free(&refs);
6061 if (error)
6062 goto done;
6063 error = add_branch(repo, argv[0], commit_id);
6064 if (error)
6065 goto done;
6066 if (worktree && do_update) {
6067 struct got_update_progress_arg upa;
6068 char *branch_refname = NULL;
6070 error = got_object_id_str(&commit_id_str, commit_id);
6071 if (error)
6072 goto done;
6073 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6074 worktree);
6075 if (error)
6076 goto done;
6077 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6078 == -1) {
6079 error = got_error_from_errno("asprintf");
6080 goto done;
6082 error = got_ref_open(&ref, repo, branch_refname, 0);
6083 free(branch_refname);
6084 if (error)
6085 goto done;
6086 error = switch_head_ref(ref, commit_id, worktree,
6087 repo);
6088 if (error)
6089 goto done;
6090 error = got_worktree_set_base_commit_id(worktree, repo,
6091 commit_id);
6092 if (error)
6093 goto done;
6094 memset(&upa, 0, sizeof(upa));
6095 error = got_worktree_checkout_files(worktree, &paths,
6096 repo, update_progress, &upa, check_cancelled,
6097 NULL);
6098 if (error)
6099 goto done;
6100 if (upa.did_something) {
6101 printf("Updated to %s: %s\n",
6102 got_worktree_get_head_ref_name(worktree),
6103 commit_id_str);
6105 print_update_progress_stats(&upa);
6108 done:
6109 if (ref)
6110 got_ref_close(ref);
6111 if (repo) {
6112 const struct got_error *close_err = got_repo_close(repo);
6113 if (error == NULL)
6114 error = close_err;
6116 if (worktree)
6117 got_worktree_close(worktree);
6118 free(cwd);
6119 free(repo_path);
6120 free(commit_id);
6121 free(commit_id_str);
6122 TAILQ_FOREACH(pe, &paths, entry)
6123 free((char *)pe->path);
6124 got_pathlist_free(&paths);
6125 return error;
6129 __dead static void
6130 usage_tag(void)
6132 fprintf(stderr,
6133 "usage: %s tag [-c commit] [-r repository] [-l] "
6134 "[-m message] name\n", getprogname());
6135 exit(1);
6138 #if 0
6139 static const struct got_error *
6140 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6142 const struct got_error *err = NULL;
6143 struct got_reflist_entry *re, *se, *new;
6144 struct got_object_id *re_id, *se_id;
6145 struct got_tag_object *re_tag, *se_tag;
6146 time_t re_time, se_time;
6148 STAILQ_FOREACH(re, tags, entry) {
6149 se = STAILQ_FIRST(sorted);
6150 if (se == NULL) {
6151 err = got_reflist_entry_dup(&new, re);
6152 if (err)
6153 return err;
6154 STAILQ_INSERT_HEAD(sorted, new, entry);
6155 continue;
6156 } else {
6157 err = got_ref_resolve(&re_id, repo, re->ref);
6158 if (err)
6159 break;
6160 err = got_object_open_as_tag(&re_tag, repo, re_id);
6161 free(re_id);
6162 if (err)
6163 break;
6164 re_time = got_object_tag_get_tagger_time(re_tag);
6165 got_object_tag_close(re_tag);
6168 while (se) {
6169 err = got_ref_resolve(&se_id, repo, re->ref);
6170 if (err)
6171 break;
6172 err = got_object_open_as_tag(&se_tag, repo, se_id);
6173 free(se_id);
6174 if (err)
6175 break;
6176 se_time = got_object_tag_get_tagger_time(se_tag);
6177 got_object_tag_close(se_tag);
6179 if (se_time > re_time) {
6180 err = got_reflist_entry_dup(&new, re);
6181 if (err)
6182 return err;
6183 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6184 break;
6186 se = STAILQ_NEXT(se, entry);
6187 continue;
6190 done:
6191 return err;
6193 #endif
6195 static const struct got_error *
6196 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6198 static const struct got_error *err = NULL;
6199 struct got_reflist_head refs;
6200 struct got_reflist_entry *re;
6202 TAILQ_INIT(&refs);
6204 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6205 if (err)
6206 return err;
6208 TAILQ_FOREACH(re, &refs, entry) {
6209 const char *refname;
6210 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6211 char datebuf[26];
6212 const char *tagger;
6213 time_t tagger_time;
6214 struct got_object_id *id;
6215 struct got_tag_object *tag;
6216 struct got_commit_object *commit = NULL;
6218 refname = got_ref_get_name(re->ref);
6219 if (strncmp(refname, "refs/tags/", 10) != 0)
6220 continue;
6221 refname += 10;
6222 refstr = got_ref_to_str(re->ref);
6223 if (refstr == NULL) {
6224 err = got_error_from_errno("got_ref_to_str");
6225 break;
6227 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6228 free(refstr);
6230 err = got_ref_resolve(&id, repo, re->ref);
6231 if (err)
6232 break;
6233 err = got_object_open_as_tag(&tag, repo, id);
6234 if (err) {
6235 if (err->code != GOT_ERR_OBJ_TYPE) {
6236 free(id);
6237 break;
6239 /* "lightweight" tag */
6240 err = got_object_open_as_commit(&commit, repo, id);
6241 if (err) {
6242 free(id);
6243 break;
6245 tagger = got_object_commit_get_committer(commit);
6246 tagger_time =
6247 got_object_commit_get_committer_time(commit);
6248 err = got_object_id_str(&id_str, id);
6249 free(id);
6250 if (err)
6251 break;
6252 } else {
6253 free(id);
6254 tagger = got_object_tag_get_tagger(tag);
6255 tagger_time = got_object_tag_get_tagger_time(tag);
6256 err = got_object_id_str(&id_str,
6257 got_object_tag_get_object_id(tag));
6258 if (err)
6259 break;
6261 printf("from: %s\n", tagger);
6262 datestr = get_datestr(&tagger_time, datebuf);
6263 if (datestr)
6264 printf("date: %s UTC\n", datestr);
6265 if (commit)
6266 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6267 else {
6268 switch (got_object_tag_get_object_type(tag)) {
6269 case GOT_OBJ_TYPE_BLOB:
6270 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6271 id_str);
6272 break;
6273 case GOT_OBJ_TYPE_TREE:
6274 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6275 id_str);
6276 break;
6277 case GOT_OBJ_TYPE_COMMIT:
6278 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6279 id_str);
6280 break;
6281 case GOT_OBJ_TYPE_TAG:
6282 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6283 id_str);
6284 break;
6285 default:
6286 break;
6289 free(id_str);
6290 if (commit) {
6291 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6292 if (err)
6293 break;
6294 got_object_commit_close(commit);
6295 } else {
6296 tagmsg0 = strdup(got_object_tag_get_message(tag));
6297 got_object_tag_close(tag);
6298 if (tagmsg0 == NULL) {
6299 err = got_error_from_errno("strdup");
6300 break;
6304 tagmsg = tagmsg0;
6305 do {
6306 line = strsep(&tagmsg, "\n");
6307 if (line)
6308 printf(" %s\n", line);
6309 } while (line);
6310 free(tagmsg0);
6313 got_ref_list_free(&refs);
6314 return NULL;
6317 static const struct got_error *
6318 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6319 const char *tag_name, const char *repo_path)
6321 const struct got_error *err = NULL;
6322 char *template = NULL, *initial_content = NULL;
6323 char *editor = NULL;
6324 int initial_content_len;
6325 int fd = -1;
6327 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6328 err = got_error_from_errno("asprintf");
6329 goto done;
6332 initial_content_len = asprintf(&initial_content,
6333 "\n# tagging commit %s as %s\n",
6334 commit_id_str, tag_name);
6335 if (initial_content_len == -1) {
6336 err = got_error_from_errno("asprintf");
6337 goto done;
6340 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6341 if (err)
6342 goto done;
6344 if (write(fd, initial_content, initial_content_len) == -1) {
6345 err = got_error_from_errno2("write", *tagmsg_path);
6346 goto done;
6349 err = get_editor(&editor);
6350 if (err)
6351 goto done;
6352 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6353 initial_content_len, 1);
6354 done:
6355 free(initial_content);
6356 free(template);
6357 free(editor);
6359 if (fd != -1 && close(fd) == -1 && err == NULL)
6360 err = got_error_from_errno2("close", *tagmsg_path);
6362 /* Editor is done; we can now apply unveil(2) */
6363 if (err == NULL)
6364 err = apply_unveil(repo_path, 0, NULL);
6365 if (err) {
6366 free(*tagmsg);
6367 *tagmsg = NULL;
6369 return err;
6372 static const struct got_error *
6373 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6374 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6376 const struct got_error *err = NULL;
6377 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6378 char *label = NULL, *commit_id_str = NULL;
6379 struct got_reference *ref = NULL;
6380 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6381 char *tagmsg_path = NULL, *tag_id_str = NULL;
6382 int preserve_tagmsg = 0;
6383 struct got_reflist_head refs;
6385 TAILQ_INIT(&refs);
6388 * Don't let the user create a tag name with a leading '-'.
6389 * While technically a valid reference name, this case is usually
6390 * an unintended typo.
6392 if (tag_name[0] == '-')
6393 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6395 err = get_author(&tagger, repo, worktree);
6396 if (err)
6397 return err;
6399 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6400 if (err)
6401 goto done;
6403 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6404 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6405 if (err)
6406 goto done;
6408 err = got_object_id_str(&commit_id_str, commit_id);
6409 if (err)
6410 goto done;
6412 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6413 refname = strdup(tag_name);
6414 if (refname == NULL) {
6415 err = got_error_from_errno("strdup");
6416 goto done;
6418 tag_name += 10;
6419 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6420 err = got_error_from_errno("asprintf");
6421 goto done;
6424 err = got_ref_open(&ref, repo, refname, 0);
6425 if (err == NULL) {
6426 err = got_error(GOT_ERR_TAG_EXISTS);
6427 goto done;
6428 } else if (err->code != GOT_ERR_NOT_REF)
6429 goto done;
6431 if (tagmsg_arg == NULL) {
6432 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6433 tag_name, got_repo_get_path(repo));
6434 if (err) {
6435 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6436 tagmsg_path != NULL)
6437 preserve_tagmsg = 1;
6438 goto done;
6442 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6443 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6444 if (err) {
6445 if (tagmsg_path)
6446 preserve_tagmsg = 1;
6447 goto done;
6450 err = got_ref_alloc(&ref, refname, tag_id);
6451 if (err) {
6452 if (tagmsg_path)
6453 preserve_tagmsg = 1;
6454 goto done;
6457 err = got_ref_write(ref, repo);
6458 if (err) {
6459 if (tagmsg_path)
6460 preserve_tagmsg = 1;
6461 goto done;
6464 err = got_object_id_str(&tag_id_str, tag_id);
6465 if (err) {
6466 if (tagmsg_path)
6467 preserve_tagmsg = 1;
6468 goto done;
6470 printf("Created tag %s\n", tag_id_str);
6471 done:
6472 if (preserve_tagmsg) {
6473 fprintf(stderr, "%s: tag message preserved in %s\n",
6474 getprogname(), tagmsg_path);
6475 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6476 err = got_error_from_errno2("unlink", tagmsg_path);
6477 free(tag_id_str);
6478 if (ref)
6479 got_ref_close(ref);
6480 free(commit_id);
6481 free(commit_id_str);
6482 free(refname);
6483 free(tagmsg);
6484 free(tagmsg_path);
6485 free(tagger);
6486 got_ref_list_free(&refs);
6487 return err;
6490 static const struct got_error *
6491 cmd_tag(int argc, char *argv[])
6493 const struct got_error *error = NULL;
6494 struct got_repository *repo = NULL;
6495 struct got_worktree *worktree = NULL;
6496 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6497 char *gitconfig_path = NULL;
6498 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6499 int ch, do_list = 0;
6501 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6502 switch (ch) {
6503 case 'c':
6504 commit_id_arg = optarg;
6505 break;
6506 case 'm':
6507 tagmsg = optarg;
6508 break;
6509 case 'r':
6510 repo_path = realpath(optarg, NULL);
6511 if (repo_path == NULL)
6512 return got_error_from_errno2("realpath",
6513 optarg);
6514 got_path_strip_trailing_slashes(repo_path);
6515 break;
6516 case 'l':
6517 do_list = 1;
6518 break;
6519 default:
6520 usage_tag();
6521 /* NOTREACHED */
6525 argc -= optind;
6526 argv += optind;
6528 if (do_list) {
6529 if (commit_id_arg != NULL)
6530 errx(1,
6531 "-c option can only be used when creating a tag");
6532 if (tagmsg)
6533 option_conflict('l', 'm');
6534 if (argc > 0)
6535 usage_tag();
6536 } else if (argc != 1)
6537 usage_tag();
6539 tag_name = argv[0];
6541 #ifndef PROFILE
6542 if (do_list) {
6543 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6544 NULL) == -1)
6545 err(1, "pledge");
6546 } else {
6547 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6548 "sendfd unveil", NULL) == -1)
6549 err(1, "pledge");
6551 #endif
6552 cwd = getcwd(NULL, 0);
6553 if (cwd == NULL) {
6554 error = got_error_from_errno("getcwd");
6555 goto done;
6558 if (repo_path == NULL) {
6559 error = got_worktree_open(&worktree, cwd);
6560 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6561 goto done;
6562 else
6563 error = NULL;
6564 if (worktree) {
6565 repo_path =
6566 strdup(got_worktree_get_repo_path(worktree));
6567 if (repo_path == NULL)
6568 error = got_error_from_errno("strdup");
6569 if (error)
6570 goto done;
6571 } else {
6572 repo_path = strdup(cwd);
6573 if (repo_path == NULL) {
6574 error = got_error_from_errno("strdup");
6575 goto done;
6580 if (do_list) {
6581 error = got_repo_open(&repo, repo_path, NULL);
6582 if (error != NULL)
6583 goto done;
6584 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6585 if (error)
6586 goto done;
6587 error = list_tags(repo, worktree);
6588 } else {
6589 error = get_gitconfig_path(&gitconfig_path);
6590 if (error)
6591 goto done;
6592 error = got_repo_open(&repo, repo_path, gitconfig_path);
6593 if (error != NULL)
6594 goto done;
6596 if (tagmsg) {
6597 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6598 if (error)
6599 goto done;
6602 if (commit_id_arg == NULL) {
6603 struct got_reference *head_ref;
6604 struct got_object_id *commit_id;
6605 error = got_ref_open(&head_ref, repo,
6606 worktree ? got_worktree_get_head_ref_name(worktree)
6607 : GOT_REF_HEAD, 0);
6608 if (error)
6609 goto done;
6610 error = got_ref_resolve(&commit_id, repo, head_ref);
6611 got_ref_close(head_ref);
6612 if (error)
6613 goto done;
6614 error = got_object_id_str(&commit_id_str, commit_id);
6615 free(commit_id);
6616 if (error)
6617 goto done;
6620 error = add_tag(repo, worktree, tag_name,
6621 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6623 done:
6624 if (repo) {
6625 const struct got_error *close_err = got_repo_close(repo);
6626 if (error == NULL)
6627 error = close_err;
6629 if (worktree)
6630 got_worktree_close(worktree);
6631 free(cwd);
6632 free(repo_path);
6633 free(gitconfig_path);
6634 free(commit_id_str);
6635 return error;
6638 __dead static void
6639 usage_add(void)
6641 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6642 getprogname());
6643 exit(1);
6646 static const struct got_error *
6647 add_progress(void *arg, unsigned char status, const char *path)
6649 while (path[0] == '/')
6650 path++;
6651 printf("%c %s\n", status, path);
6652 return NULL;
6655 static const struct got_error *
6656 cmd_add(int argc, char *argv[])
6658 const struct got_error *error = NULL;
6659 struct got_repository *repo = NULL;
6660 struct got_worktree *worktree = NULL;
6661 char *cwd = NULL;
6662 struct got_pathlist_head paths;
6663 struct got_pathlist_entry *pe;
6664 int ch, can_recurse = 0, no_ignores = 0;
6666 TAILQ_INIT(&paths);
6668 while ((ch = getopt(argc, argv, "IR")) != -1) {
6669 switch (ch) {
6670 case 'I':
6671 no_ignores = 1;
6672 break;
6673 case 'R':
6674 can_recurse = 1;
6675 break;
6676 default:
6677 usage_add();
6678 /* NOTREACHED */
6682 argc -= optind;
6683 argv += optind;
6685 #ifndef PROFILE
6686 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6687 NULL) == -1)
6688 err(1, "pledge");
6689 #endif
6690 if (argc < 1)
6691 usage_add();
6693 cwd = getcwd(NULL, 0);
6694 if (cwd == NULL) {
6695 error = got_error_from_errno("getcwd");
6696 goto done;
6699 error = got_worktree_open(&worktree, cwd);
6700 if (error) {
6701 if (error->code == GOT_ERR_NOT_WORKTREE)
6702 error = wrap_not_worktree_error(error, "add", cwd);
6703 goto done;
6706 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6707 NULL);
6708 if (error != NULL)
6709 goto done;
6711 error = apply_unveil(got_repo_get_path(repo), 1,
6712 got_worktree_get_root_path(worktree));
6713 if (error)
6714 goto done;
6716 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6717 if (error)
6718 goto done;
6720 if (!can_recurse) {
6721 char *ondisk_path;
6722 struct stat sb;
6723 TAILQ_FOREACH(pe, &paths, entry) {
6724 if (asprintf(&ondisk_path, "%s/%s",
6725 got_worktree_get_root_path(worktree),
6726 pe->path) == -1) {
6727 error = got_error_from_errno("asprintf");
6728 goto done;
6730 if (lstat(ondisk_path, &sb) == -1) {
6731 if (errno == ENOENT) {
6732 free(ondisk_path);
6733 continue;
6735 error = got_error_from_errno2("lstat",
6736 ondisk_path);
6737 free(ondisk_path);
6738 goto done;
6740 free(ondisk_path);
6741 if (S_ISDIR(sb.st_mode)) {
6742 error = got_error_msg(GOT_ERR_BAD_PATH,
6743 "adding directories requires -R option");
6744 goto done;
6749 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6750 NULL, repo, no_ignores);
6751 done:
6752 if (repo) {
6753 const struct got_error *close_err = got_repo_close(repo);
6754 if (error == NULL)
6755 error = close_err;
6757 if (worktree)
6758 got_worktree_close(worktree);
6759 TAILQ_FOREACH(pe, &paths, entry)
6760 free((char *)pe->path);
6761 got_pathlist_free(&paths);
6762 free(cwd);
6763 return error;
6766 __dead static void
6767 usage_remove(void)
6769 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6770 "path ...\n", getprogname());
6771 exit(1);
6774 static const struct got_error *
6775 print_remove_status(void *arg, unsigned char status,
6776 unsigned char staged_status, const char *path)
6778 while (path[0] == '/')
6779 path++;
6780 if (status == GOT_STATUS_NONEXISTENT)
6781 return NULL;
6782 if (status == staged_status && (status == GOT_STATUS_DELETE))
6783 status = GOT_STATUS_NO_CHANGE;
6784 printf("%c%c %s\n", status, staged_status, path);
6785 return NULL;
6788 static const struct got_error *
6789 cmd_remove(int argc, char *argv[])
6791 const struct got_error *error = NULL;
6792 struct got_worktree *worktree = NULL;
6793 struct got_repository *repo = NULL;
6794 const char *status_codes = NULL;
6795 char *cwd = NULL;
6796 struct got_pathlist_head paths;
6797 struct got_pathlist_entry *pe;
6798 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6800 TAILQ_INIT(&paths);
6802 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6803 switch (ch) {
6804 case 'f':
6805 delete_local_mods = 1;
6806 break;
6807 case 'k':
6808 keep_on_disk = 1;
6809 break;
6810 case 'R':
6811 can_recurse = 1;
6812 break;
6813 case 's':
6814 for (i = 0; i < strlen(optarg); i++) {
6815 switch (optarg[i]) {
6816 case GOT_STATUS_MODIFY:
6817 delete_local_mods = 1;
6818 break;
6819 case GOT_STATUS_MISSING:
6820 break;
6821 default:
6822 errx(1, "invalid status code '%c'",
6823 optarg[i]);
6826 status_codes = optarg;
6827 break;
6828 default:
6829 usage_remove();
6830 /* NOTREACHED */
6834 argc -= optind;
6835 argv += optind;
6837 #ifndef PROFILE
6838 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6839 NULL) == -1)
6840 err(1, "pledge");
6841 #endif
6842 if (argc < 1)
6843 usage_remove();
6845 cwd = getcwd(NULL, 0);
6846 if (cwd == NULL) {
6847 error = got_error_from_errno("getcwd");
6848 goto done;
6850 error = got_worktree_open(&worktree, cwd);
6851 if (error) {
6852 if (error->code == GOT_ERR_NOT_WORKTREE)
6853 error = wrap_not_worktree_error(error, "remove", cwd);
6854 goto done;
6857 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6858 NULL);
6859 if (error)
6860 goto done;
6862 error = apply_unveil(got_repo_get_path(repo), 1,
6863 got_worktree_get_root_path(worktree));
6864 if (error)
6865 goto done;
6867 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6868 if (error)
6869 goto done;
6871 if (!can_recurse) {
6872 char *ondisk_path;
6873 struct stat sb;
6874 TAILQ_FOREACH(pe, &paths, entry) {
6875 if (asprintf(&ondisk_path, "%s/%s",
6876 got_worktree_get_root_path(worktree),
6877 pe->path) == -1) {
6878 error = got_error_from_errno("asprintf");
6879 goto done;
6881 if (lstat(ondisk_path, &sb) == -1) {
6882 if (errno == ENOENT) {
6883 free(ondisk_path);
6884 continue;
6886 error = got_error_from_errno2("lstat",
6887 ondisk_path);
6888 free(ondisk_path);
6889 goto done;
6891 free(ondisk_path);
6892 if (S_ISDIR(sb.st_mode)) {
6893 error = got_error_msg(GOT_ERR_BAD_PATH,
6894 "removing directories requires -R option");
6895 goto done;
6900 error = got_worktree_schedule_delete(worktree, &paths,
6901 delete_local_mods, status_codes, print_remove_status, NULL,
6902 repo, keep_on_disk);
6903 done:
6904 if (repo) {
6905 const struct got_error *close_err = got_repo_close(repo);
6906 if (error == NULL)
6907 error = close_err;
6909 if (worktree)
6910 got_worktree_close(worktree);
6911 TAILQ_FOREACH(pe, &paths, entry)
6912 free((char *)pe->path);
6913 got_pathlist_free(&paths);
6914 free(cwd);
6915 return error;
6918 __dead static void
6919 usage_revert(void)
6921 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6922 "path ...\n", getprogname());
6923 exit(1);
6926 static const struct got_error *
6927 revert_progress(void *arg, unsigned char status, const char *path)
6929 if (status == GOT_STATUS_UNVERSIONED)
6930 return NULL;
6932 while (path[0] == '/')
6933 path++;
6934 printf("%c %s\n", status, path);
6935 return NULL;
6938 struct choose_patch_arg {
6939 FILE *patch_script_file;
6940 const char *action;
6943 static const struct got_error *
6944 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6945 int nchanges, const char *action)
6947 char *line = NULL;
6948 size_t linesize = 0;
6949 ssize_t linelen;
6951 switch (status) {
6952 case GOT_STATUS_ADD:
6953 printf("A %s\n%s this addition? [y/n] ", path, action);
6954 break;
6955 case GOT_STATUS_DELETE:
6956 printf("D %s\n%s this deletion? [y/n] ", path, action);
6957 break;
6958 case GOT_STATUS_MODIFY:
6959 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6960 return got_error_from_errno("fseek");
6961 printf(GOT_COMMIT_SEP_STR);
6962 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6963 printf("%s", line);
6964 if (ferror(patch_file))
6965 return got_error_from_errno("getline");
6966 printf(GOT_COMMIT_SEP_STR);
6967 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6968 path, n, nchanges, action);
6969 break;
6970 default:
6971 return got_error_path(path, GOT_ERR_FILE_STATUS);
6974 return NULL;
6977 static const struct got_error *
6978 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6979 FILE *patch_file, int n, int nchanges)
6981 const struct got_error *err = NULL;
6982 char *line = NULL;
6983 size_t linesize = 0;
6984 ssize_t linelen;
6985 int resp = ' ';
6986 struct choose_patch_arg *a = arg;
6988 *choice = GOT_PATCH_CHOICE_NONE;
6990 if (a->patch_script_file) {
6991 char *nl;
6992 err = show_change(status, path, patch_file, n, nchanges,
6993 a->action);
6994 if (err)
6995 return err;
6996 linelen = getline(&line, &linesize, a->patch_script_file);
6997 if (linelen == -1) {
6998 if (ferror(a->patch_script_file))
6999 return got_error_from_errno("getline");
7000 return NULL;
7002 nl = strchr(line, '\n');
7003 if (nl)
7004 *nl = '\0';
7005 if (strcmp(line, "y") == 0) {
7006 *choice = GOT_PATCH_CHOICE_YES;
7007 printf("y\n");
7008 } else if (strcmp(line, "n") == 0) {
7009 *choice = GOT_PATCH_CHOICE_NO;
7010 printf("n\n");
7011 } else if (strcmp(line, "q") == 0 &&
7012 status == GOT_STATUS_MODIFY) {
7013 *choice = GOT_PATCH_CHOICE_QUIT;
7014 printf("q\n");
7015 } else
7016 printf("invalid response '%s'\n", line);
7017 free(line);
7018 return NULL;
7021 while (resp != 'y' && resp != 'n' && resp != 'q') {
7022 err = show_change(status, path, patch_file, n, nchanges,
7023 a->action);
7024 if (err)
7025 return err;
7026 resp = getchar();
7027 if (resp == '\n')
7028 resp = getchar();
7029 if (status == GOT_STATUS_MODIFY) {
7030 if (resp != 'y' && resp != 'n' && resp != 'q') {
7031 printf("invalid response '%c'\n", resp);
7032 resp = ' ';
7034 } else if (resp != 'y' && resp != 'n') {
7035 printf("invalid response '%c'\n", resp);
7036 resp = ' ';
7040 if (resp == 'y')
7041 *choice = GOT_PATCH_CHOICE_YES;
7042 else if (resp == 'n')
7043 *choice = GOT_PATCH_CHOICE_NO;
7044 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7045 *choice = GOT_PATCH_CHOICE_QUIT;
7047 return NULL;
7051 static const struct got_error *
7052 cmd_revert(int argc, char *argv[])
7054 const struct got_error *error = NULL;
7055 struct got_worktree *worktree = NULL;
7056 struct got_repository *repo = NULL;
7057 char *cwd = NULL, *path = NULL;
7058 struct got_pathlist_head paths;
7059 struct got_pathlist_entry *pe;
7060 int ch, can_recurse = 0, pflag = 0;
7061 FILE *patch_script_file = NULL;
7062 const char *patch_script_path = NULL;
7063 struct choose_patch_arg cpa;
7065 TAILQ_INIT(&paths);
7067 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7068 switch (ch) {
7069 case 'p':
7070 pflag = 1;
7071 break;
7072 case 'F':
7073 patch_script_path = optarg;
7074 break;
7075 case 'R':
7076 can_recurse = 1;
7077 break;
7078 default:
7079 usage_revert();
7080 /* NOTREACHED */
7084 argc -= optind;
7085 argv += optind;
7087 #ifndef PROFILE
7088 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7089 "unveil", NULL) == -1)
7090 err(1, "pledge");
7091 #endif
7092 if (argc < 1)
7093 usage_revert();
7094 if (patch_script_path && !pflag)
7095 errx(1, "-F option can only be used together with -p option");
7097 cwd = getcwd(NULL, 0);
7098 if (cwd == NULL) {
7099 error = got_error_from_errno("getcwd");
7100 goto done;
7102 error = got_worktree_open(&worktree, cwd);
7103 if (error) {
7104 if (error->code == GOT_ERR_NOT_WORKTREE)
7105 error = wrap_not_worktree_error(error, "revert", cwd);
7106 goto done;
7109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7110 NULL);
7111 if (error != NULL)
7112 goto done;
7114 if (patch_script_path) {
7115 patch_script_file = fopen(patch_script_path, "r");
7116 if (patch_script_file == NULL) {
7117 error = got_error_from_errno2("fopen",
7118 patch_script_path);
7119 goto done;
7122 error = apply_unveil(got_repo_get_path(repo), 1,
7123 got_worktree_get_root_path(worktree));
7124 if (error)
7125 goto done;
7127 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7128 if (error)
7129 goto done;
7131 if (!can_recurse) {
7132 char *ondisk_path;
7133 struct stat sb;
7134 TAILQ_FOREACH(pe, &paths, entry) {
7135 if (asprintf(&ondisk_path, "%s/%s",
7136 got_worktree_get_root_path(worktree),
7137 pe->path) == -1) {
7138 error = got_error_from_errno("asprintf");
7139 goto done;
7141 if (lstat(ondisk_path, &sb) == -1) {
7142 if (errno == ENOENT) {
7143 free(ondisk_path);
7144 continue;
7146 error = got_error_from_errno2("lstat",
7147 ondisk_path);
7148 free(ondisk_path);
7149 goto done;
7151 free(ondisk_path);
7152 if (S_ISDIR(sb.st_mode)) {
7153 error = got_error_msg(GOT_ERR_BAD_PATH,
7154 "reverting directories requires -R option");
7155 goto done;
7160 cpa.patch_script_file = patch_script_file;
7161 cpa.action = "revert";
7162 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7163 pflag ? choose_patch : NULL, &cpa, repo);
7164 done:
7165 if (patch_script_file && fclose(patch_script_file) == EOF &&
7166 error == NULL)
7167 error = got_error_from_errno2("fclose", patch_script_path);
7168 if (repo) {
7169 const struct got_error *close_err = got_repo_close(repo);
7170 if (error == NULL)
7171 error = close_err;
7173 if (worktree)
7174 got_worktree_close(worktree);
7175 free(path);
7176 free(cwd);
7177 return error;
7180 __dead static void
7181 usage_commit(void)
7183 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7184 "[path ...]\n", getprogname());
7185 exit(1);
7188 struct collect_commit_logmsg_arg {
7189 const char *cmdline_log;
7190 const char *prepared_log;
7191 int non_interactive;
7192 const char *editor;
7193 const char *worktree_path;
7194 const char *branch_name;
7195 const char *repo_path;
7196 char *logmsg_path;
7200 static const struct got_error *
7201 read_prepared_logmsg(char **logmsg, const char *path)
7203 const struct got_error *err = NULL;
7204 FILE *f = NULL;
7205 struct stat sb;
7206 size_t r;
7208 *logmsg = NULL;
7209 memset(&sb, 0, sizeof(sb));
7211 f = fopen(path, "r");
7212 if (f == NULL)
7213 return got_error_from_errno2("fopen", path);
7215 if (fstat(fileno(f), &sb) == -1) {
7216 err = got_error_from_errno2("fstat", path);
7217 goto done;
7219 if (sb.st_size == 0) {
7220 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7221 goto done;
7224 *logmsg = malloc(sb.st_size + 1);
7225 if (*logmsg == NULL) {
7226 err = got_error_from_errno("malloc");
7227 goto done;
7230 r = fread(*logmsg, 1, sb.st_size, f);
7231 if (r != sb.st_size) {
7232 if (ferror(f))
7233 err = got_error_from_errno2("fread", path);
7234 else
7235 err = got_error(GOT_ERR_IO);
7236 goto done;
7238 (*logmsg)[sb.st_size] = '\0';
7239 done:
7240 if (fclose(f) == EOF && err == NULL)
7241 err = got_error_from_errno2("fclose", path);
7242 if (err) {
7243 free(*logmsg);
7244 *logmsg = NULL;
7246 return err;
7250 static const struct got_error *
7251 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7252 void *arg)
7254 char *initial_content = NULL;
7255 struct got_pathlist_entry *pe;
7256 const struct got_error *err = NULL;
7257 char *template = NULL;
7258 struct collect_commit_logmsg_arg *a = arg;
7259 int initial_content_len;
7260 int fd = -1;
7261 size_t len;
7263 /* if a message was specified on the command line, just use it */
7264 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7265 len = strlen(a->cmdline_log) + 1;
7266 *logmsg = malloc(len + 1);
7267 if (*logmsg == NULL)
7268 return got_error_from_errno("malloc");
7269 strlcpy(*logmsg, a->cmdline_log, len);
7270 return NULL;
7271 } else if (a->prepared_log != NULL && a->non_interactive)
7272 return read_prepared_logmsg(logmsg, a->prepared_log);
7274 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7275 return got_error_from_errno("asprintf");
7277 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7278 if (err)
7279 goto done;
7281 if (a->prepared_log) {
7282 char *msg;
7283 err = read_prepared_logmsg(&msg, a->prepared_log);
7284 if (err)
7285 goto done;
7286 if (write(fd, msg, strlen(msg)) == -1) {
7287 err = got_error_from_errno2("write", a->logmsg_path);
7288 free(msg);
7289 goto done;
7291 free(msg);
7294 initial_content_len = asprintf(&initial_content,
7295 "\n# changes to be committed on branch %s:\n",
7296 a->branch_name);
7297 if (initial_content_len == -1) {
7298 err = got_error_from_errno("asprintf");
7299 goto done;
7302 if (write(fd, initial_content, initial_content_len) == -1) {
7303 err = got_error_from_errno2("write", a->logmsg_path);
7304 goto done;
7307 TAILQ_FOREACH(pe, commitable_paths, entry) {
7308 struct got_commitable *ct = pe->data;
7309 dprintf(fd, "# %c %s\n",
7310 got_commitable_get_status(ct),
7311 got_commitable_get_path(ct));
7314 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7315 initial_content_len, a->prepared_log ? 0 : 1);
7316 done:
7317 free(initial_content);
7318 free(template);
7320 if (fd != -1 && close(fd) == -1 && err == NULL)
7321 err = got_error_from_errno2("close", a->logmsg_path);
7323 /* Editor is done; we can now apply unveil(2) */
7324 if (err == NULL)
7325 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7326 if (err) {
7327 free(*logmsg);
7328 *logmsg = NULL;
7330 return err;
7333 static const struct got_error *
7334 cmd_commit(int argc, char *argv[])
7336 const struct got_error *error = NULL;
7337 struct got_worktree *worktree = NULL;
7338 struct got_repository *repo = NULL;
7339 char *cwd = NULL, *id_str = NULL;
7340 struct got_object_id *id = NULL;
7341 const char *logmsg = NULL;
7342 char *prepared_logmsg = NULL;
7343 struct collect_commit_logmsg_arg cl_arg;
7344 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7345 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7346 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7347 struct got_pathlist_head paths;
7349 TAILQ_INIT(&paths);
7350 cl_arg.logmsg_path = NULL;
7352 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7353 switch (ch) {
7354 case 'F':
7355 if (logmsg != NULL)
7356 option_conflict('F', 'm');
7357 prepared_logmsg = realpath(optarg, NULL);
7358 if (prepared_logmsg == NULL)
7359 return got_error_from_errno2("realpath",
7360 optarg);
7361 break;
7362 case 'm':
7363 if (prepared_logmsg)
7364 option_conflict('m', 'F');
7365 logmsg = optarg;
7366 break;
7367 case 'N':
7368 non_interactive = 1;
7369 break;
7370 case 'S':
7371 allow_bad_symlinks = 1;
7372 break;
7373 default:
7374 usage_commit();
7375 /* NOTREACHED */
7379 argc -= optind;
7380 argv += optind;
7382 #ifndef PROFILE
7383 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7384 "unveil", NULL) == -1)
7385 err(1, "pledge");
7386 #endif
7387 cwd = getcwd(NULL, 0);
7388 if (cwd == NULL) {
7389 error = got_error_from_errno("getcwd");
7390 goto done;
7392 error = got_worktree_open(&worktree, cwd);
7393 if (error) {
7394 if (error->code == GOT_ERR_NOT_WORKTREE)
7395 error = wrap_not_worktree_error(error, "commit", cwd);
7396 goto done;
7399 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7400 if (error)
7401 goto done;
7402 if (rebase_in_progress) {
7403 error = got_error(GOT_ERR_REBASING);
7404 goto done;
7407 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7408 worktree);
7409 if (error)
7410 goto done;
7412 error = get_gitconfig_path(&gitconfig_path);
7413 if (error)
7414 goto done;
7415 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7416 gitconfig_path);
7417 if (error != NULL)
7418 goto done;
7420 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7421 if (error)
7422 goto done;
7423 if (merge_in_progress) {
7424 error = got_error(GOT_ERR_MERGE_BUSY);
7425 goto done;
7428 error = get_author(&author, repo, worktree);
7429 if (error)
7430 return error;
7433 * unveil(2) traverses exec(2); if an editor is used we have
7434 * to apply unveil after the log message has been written.
7436 if (logmsg == NULL || strlen(logmsg) == 0)
7437 error = get_editor(&editor);
7438 else
7439 error = apply_unveil(got_repo_get_path(repo), 0,
7440 got_worktree_get_root_path(worktree));
7441 if (error)
7442 goto done;
7444 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7445 if (error)
7446 goto done;
7448 cl_arg.editor = editor;
7449 cl_arg.cmdline_log = logmsg;
7450 cl_arg.prepared_log = prepared_logmsg;
7451 cl_arg.non_interactive = non_interactive;
7452 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7453 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7454 if (!histedit_in_progress) {
7455 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7456 error = got_error(GOT_ERR_COMMIT_BRANCH);
7457 goto done;
7459 cl_arg.branch_name += 11;
7461 cl_arg.repo_path = got_repo_get_path(repo);
7462 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7463 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7464 print_status, NULL, repo);
7465 if (error) {
7466 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7467 cl_arg.logmsg_path != NULL)
7468 preserve_logmsg = 1;
7469 goto done;
7472 error = got_object_id_str(&id_str, id);
7473 if (error)
7474 goto done;
7475 printf("Created commit %s\n", id_str);
7476 done:
7477 if (preserve_logmsg) {
7478 fprintf(stderr, "%s: log message preserved in %s\n",
7479 getprogname(), cl_arg.logmsg_path);
7480 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7481 error == NULL)
7482 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7483 free(cl_arg.logmsg_path);
7484 if (repo) {
7485 const struct got_error *close_err = got_repo_close(repo);
7486 if (error == NULL)
7487 error = close_err;
7489 if (worktree)
7490 got_worktree_close(worktree);
7491 free(cwd);
7492 free(id_str);
7493 free(gitconfig_path);
7494 free(editor);
7495 free(author);
7496 free(prepared_logmsg);
7497 return error;
7500 __dead static void
7501 usage_send(void)
7503 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7504 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7505 "[remote-repository]\n", getprogname());
7506 exit(1);
7509 struct got_send_progress_arg {
7510 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7511 int verbosity;
7512 int last_ncommits;
7513 int last_nobj_total;
7514 int last_p_deltify;
7515 int last_p_written;
7516 int last_p_sent;
7517 int printed_something;
7518 int sent_something;
7519 struct got_pathlist_head *delete_branches;
7522 static const struct got_error *
7523 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7524 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7525 int success)
7527 struct got_send_progress_arg *a = arg;
7528 char scaled_packsize[FMT_SCALED_STRSIZE];
7529 char scaled_sent[FMT_SCALED_STRSIZE];
7530 int p_deltify = 0, p_written = 0, p_sent = 0;
7531 int print_searching = 0, print_total = 0;
7532 int print_deltify = 0, print_written = 0, print_sent = 0;
7534 if (a->verbosity < 0)
7535 return NULL;
7537 if (refname) {
7538 const char *status = success ? "accepted" : "rejected";
7540 if (success) {
7541 struct got_pathlist_entry *pe;
7542 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7543 const char *branchname = pe->path;
7544 if (got_path_cmp(branchname, refname,
7545 strlen(branchname), strlen(refname)) == 0) {
7546 status = "deleted";
7547 a->sent_something = 1;
7548 break;
7553 if (a->printed_something)
7554 putchar('\n');
7555 printf("Server has %s %s", status, refname);
7556 a->printed_something = 1;
7557 return NULL;
7560 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7561 return got_error_from_errno("fmt_scaled");
7562 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7563 return got_error_from_errno("fmt_scaled");
7565 if (a->last_ncommits != ncommits) {
7566 print_searching = 1;
7567 a->last_ncommits = ncommits;
7570 if (a->last_nobj_total != nobj_total) {
7571 print_searching = 1;
7572 print_total = 1;
7573 a->last_nobj_total = nobj_total;
7576 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7577 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7578 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7579 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7580 return got_error(GOT_ERR_NO_SPACE);
7583 if (nobj_deltify > 0 || nobj_written > 0) {
7584 if (nobj_deltify > 0) {
7585 p_deltify = (nobj_deltify * 100) / nobj_total;
7586 if (p_deltify != a->last_p_deltify) {
7587 a->last_p_deltify = p_deltify;
7588 print_searching = 1;
7589 print_total = 1;
7590 print_deltify = 1;
7593 if (nobj_written > 0) {
7594 p_written = (nobj_written * 100) / nobj_total;
7595 if (p_written != a->last_p_written) {
7596 a->last_p_written = p_written;
7597 print_searching = 1;
7598 print_total = 1;
7599 print_deltify = 1;
7600 print_written = 1;
7605 if (bytes_sent > 0) {
7606 p_sent = (bytes_sent * 100) / packfile_size;
7607 if (p_sent != a->last_p_sent) {
7608 a->last_p_sent = p_sent;
7609 print_searching = 1;
7610 print_total = 1;
7611 print_deltify = 1;
7612 print_written = 1;
7613 print_sent = 1;
7615 a->sent_something = 1;
7618 if (print_searching || print_total || print_deltify || print_written ||
7619 print_sent)
7620 printf("\r");
7621 if (print_searching)
7622 printf("packing %d reference%s", ncommits,
7623 ncommits == 1 ? "" : "s");
7624 if (print_total)
7625 printf("; %d object%s", nobj_total,
7626 nobj_total == 1 ? "" : "s");
7627 if (print_deltify)
7628 printf("; deltify: %d%%", p_deltify);
7629 if (print_sent)
7630 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7631 scaled_packsize, p_sent);
7632 else if (print_written)
7633 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7634 scaled_packsize, p_written);
7635 if (print_searching || print_total || print_deltify ||
7636 print_written || print_sent) {
7637 a->printed_something = 1;
7638 fflush(stdout);
7640 return NULL;
7643 static const struct got_error *
7644 cmd_send(int argc, char *argv[])
7646 const struct got_error *error = NULL;
7647 char *cwd = NULL, *repo_path = NULL;
7648 const char *remote_name;
7649 char *proto = NULL, *host = NULL, *port = NULL;
7650 char *repo_name = NULL, *server_path = NULL;
7651 const struct got_remote_repo *remotes, *remote = NULL;
7652 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7653 struct got_repository *repo = NULL;
7654 struct got_worktree *worktree = NULL;
7655 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7656 struct got_pathlist_head branches;
7657 struct got_pathlist_head tags;
7658 struct got_reflist_head all_branches;
7659 struct got_reflist_head all_tags;
7660 struct got_pathlist_head delete_args;
7661 struct got_pathlist_head delete_branches;
7662 struct got_reflist_entry *re;
7663 struct got_pathlist_entry *pe;
7664 int i, ch, sendfd = -1, sendstatus;
7665 pid_t sendpid = -1;
7666 struct got_send_progress_arg spa;
7667 int verbosity = 0, overwrite_refs = 0;
7668 int send_all_branches = 0, send_all_tags = 0;
7669 struct got_reference *ref = NULL;
7671 TAILQ_INIT(&branches);
7672 TAILQ_INIT(&tags);
7673 TAILQ_INIT(&all_branches);
7674 TAILQ_INIT(&all_tags);
7675 TAILQ_INIT(&delete_args);
7676 TAILQ_INIT(&delete_branches);
7678 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7679 switch (ch) {
7680 case 'a':
7681 send_all_branches = 1;
7682 break;
7683 case 'b':
7684 error = got_pathlist_append(&branches, optarg, NULL);
7685 if (error)
7686 return error;
7687 nbranches++;
7688 break;
7689 case 'd':
7690 error = got_pathlist_append(&delete_args, optarg, NULL);
7691 if (error)
7692 return error;
7693 break;
7694 case 'f':
7695 overwrite_refs = 1;
7696 break;
7697 case 'r':
7698 repo_path = realpath(optarg, NULL);
7699 if (repo_path == NULL)
7700 return got_error_from_errno2("realpath",
7701 optarg);
7702 got_path_strip_trailing_slashes(repo_path);
7703 break;
7704 case 't':
7705 error = got_pathlist_append(&tags, optarg, NULL);
7706 if (error)
7707 return error;
7708 ntags++;
7709 break;
7710 case 'T':
7711 send_all_tags = 1;
7712 break;
7713 case 'v':
7714 if (verbosity < 0)
7715 verbosity = 0;
7716 else if (verbosity < 3)
7717 verbosity++;
7718 break;
7719 case 'q':
7720 verbosity = -1;
7721 break;
7722 default:
7723 usage_send();
7724 /* NOTREACHED */
7727 argc -= optind;
7728 argv += optind;
7730 if (send_all_branches && !TAILQ_EMPTY(&branches))
7731 option_conflict('a', 'b');
7732 if (send_all_tags && !TAILQ_EMPTY(&tags))
7733 option_conflict('T', 't');
7736 if (argc == 0)
7737 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7738 else if (argc == 1)
7739 remote_name = argv[0];
7740 else
7741 usage_send();
7743 cwd = getcwd(NULL, 0);
7744 if (cwd == NULL) {
7745 error = got_error_from_errno("getcwd");
7746 goto done;
7749 if (repo_path == NULL) {
7750 error = got_worktree_open(&worktree, cwd);
7751 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7752 goto done;
7753 else
7754 error = NULL;
7755 if (worktree) {
7756 repo_path =
7757 strdup(got_worktree_get_repo_path(worktree));
7758 if (repo_path == NULL)
7759 error = got_error_from_errno("strdup");
7760 if (error)
7761 goto done;
7762 } else {
7763 repo_path = strdup(cwd);
7764 if (repo_path == NULL) {
7765 error = got_error_from_errno("strdup");
7766 goto done;
7771 error = got_repo_open(&repo, repo_path, NULL);
7772 if (error)
7773 goto done;
7775 if (worktree) {
7776 worktree_conf = got_worktree_get_gotconfig(worktree);
7777 if (worktree_conf) {
7778 got_gotconfig_get_remotes(&nremotes, &remotes,
7779 worktree_conf);
7780 for (i = 0; i < nremotes; i++) {
7781 if (strcmp(remotes[i].name, remote_name) == 0) {
7782 remote = &remotes[i];
7783 break;
7788 if (remote == NULL) {
7789 repo_conf = got_repo_get_gotconfig(repo);
7790 if (repo_conf) {
7791 got_gotconfig_get_remotes(&nremotes, &remotes,
7792 repo_conf);
7793 for (i = 0; i < nremotes; i++) {
7794 if (strcmp(remotes[i].name, remote_name) == 0) {
7795 remote = &remotes[i];
7796 break;
7801 if (remote == NULL) {
7802 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7803 for (i = 0; i < nremotes; i++) {
7804 if (strcmp(remotes[i].name, remote_name) == 0) {
7805 remote = &remotes[i];
7806 break;
7810 if (remote == NULL) {
7811 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7812 goto done;
7815 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7816 &repo_name, remote->send_url);
7817 if (error)
7818 goto done;
7820 if (strcmp(proto, "git") == 0) {
7821 #ifndef PROFILE
7822 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7823 "sendfd dns inet unveil", NULL) == -1)
7824 err(1, "pledge");
7825 #endif
7826 } else if (strcmp(proto, "git+ssh") == 0 ||
7827 strcmp(proto, "ssh") == 0) {
7828 #ifndef PROFILE
7829 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7830 "sendfd unveil", NULL) == -1)
7831 err(1, "pledge");
7832 #endif
7833 } else if (strcmp(proto, "http") == 0 ||
7834 strcmp(proto, "git+http") == 0) {
7835 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7836 goto done;
7837 } else {
7838 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7839 goto done;
7842 error = got_dial_apply_unveil(proto);
7843 if (error)
7844 goto done;
7846 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7847 if (error)
7848 goto done;
7850 if (send_all_branches) {
7851 error = got_ref_list(&all_branches, repo, "refs/heads",
7852 got_ref_cmp_by_name, NULL);
7853 if (error)
7854 goto done;
7855 TAILQ_FOREACH(re, &all_branches, entry) {
7856 const char *branchname = got_ref_get_name(re->ref);
7857 error = got_pathlist_append(&branches,
7858 branchname, NULL);
7859 if (error)
7860 goto done;
7861 nbranches++;
7863 } else if (nbranches == 0) {
7864 for (i = 0; i < remote->nsend_branches; i++) {
7865 got_pathlist_append(&branches,
7866 remote->send_branches[i], NULL);
7870 if (send_all_tags) {
7871 error = got_ref_list(&all_tags, repo, "refs/tags",
7872 got_ref_cmp_by_name, NULL);
7873 if (error)
7874 goto done;
7875 TAILQ_FOREACH(re, &all_tags, entry) {
7876 const char *tagname = got_ref_get_name(re->ref);
7877 error = got_pathlist_append(&tags,
7878 tagname, NULL);
7879 if (error)
7880 goto done;
7881 ntags++;
7886 * To prevent accidents only branches in refs/heads/ can be deleted
7887 * with 'got send -d'.
7888 * Deleting anything else requires local repository access or Git.
7890 TAILQ_FOREACH(pe, &delete_args, entry) {
7891 const char *branchname = pe->path;
7892 char *s;
7893 struct got_pathlist_entry *new;
7894 if (strncmp(branchname, "refs/heads/", 11) == 0) {
7895 s = strdup(branchname);
7896 if (s == NULL) {
7897 error = got_error_from_errno("strdup");
7898 goto done;
7900 } else {
7901 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7902 error = got_error_from_errno("asprintf");
7903 goto done;
7906 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7907 if (error || new == NULL /* duplicate */)
7908 free(s);
7909 if (error)
7910 goto done;
7911 ndelete_branches++;
7914 if (nbranches == 0 && ndelete_branches == 0) {
7915 struct got_reference *head_ref;
7916 if (worktree)
7917 error = got_ref_open(&head_ref, repo,
7918 got_worktree_get_head_ref_name(worktree), 0);
7919 else
7920 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7921 if (error)
7922 goto done;
7923 if (got_ref_is_symbolic(head_ref)) {
7924 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7925 got_ref_close(head_ref);
7926 if (error)
7927 goto done;
7928 } else
7929 ref = head_ref;
7930 error = got_pathlist_append(&branches, got_ref_get_name(ref),
7931 NULL);
7932 if (error)
7933 goto done;
7934 nbranches++;
7937 if (verbosity >= 0)
7938 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7939 port ? ":" : "", port ? port : "");
7941 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7942 server_path, verbosity);
7943 if (error)
7944 goto done;
7946 memset(&spa, 0, sizeof(spa));
7947 spa.last_scaled_packsize[0] = '\0';
7948 spa.last_p_deltify = -1;
7949 spa.last_p_written = -1;
7950 spa.verbosity = verbosity;
7951 spa.delete_branches = &delete_branches;
7952 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7953 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7954 check_cancelled, NULL);
7955 if (spa.printed_something)
7956 putchar('\n');
7957 if (error)
7958 goto done;
7959 if (!spa.sent_something && verbosity >= 0)
7960 printf("Already up-to-date\n");
7961 done:
7962 if (sendpid > 0) {
7963 if (kill(sendpid, SIGTERM) == -1)
7964 error = got_error_from_errno("kill");
7965 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7966 error = got_error_from_errno("waitpid");
7968 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7969 error = got_error_from_errno("close");
7970 if (repo) {
7971 const struct got_error *close_err = got_repo_close(repo);
7972 if (error == NULL)
7973 error = close_err;
7975 if (worktree)
7976 got_worktree_close(worktree);
7977 if (ref)
7978 got_ref_close(ref);
7979 got_pathlist_free(&branches);
7980 got_pathlist_free(&tags);
7981 got_ref_list_free(&all_branches);
7982 got_ref_list_free(&all_tags);
7983 got_pathlist_free(&delete_args);
7984 TAILQ_FOREACH(pe, &delete_branches, entry)
7985 free((char *)pe->path);
7986 got_pathlist_free(&delete_branches);
7987 free(cwd);
7988 free(repo_path);
7989 free(proto);
7990 free(host);
7991 free(port);
7992 free(server_path);
7993 free(repo_name);
7994 return error;
7997 __dead static void
7998 usage_cherrypick(void)
8000 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8001 exit(1);
8004 static const struct got_error *
8005 cmd_cherrypick(int argc, char *argv[])
8007 const struct got_error *error = NULL;
8008 struct got_worktree *worktree = NULL;
8009 struct got_repository *repo = NULL;
8010 char *cwd = NULL, *commit_id_str = NULL;
8011 struct got_object_id *commit_id = NULL;
8012 struct got_commit_object *commit = NULL;
8013 struct got_object_qid *pid;
8014 int ch;
8015 struct got_update_progress_arg upa;
8017 while ((ch = getopt(argc, argv, "")) != -1) {
8018 switch (ch) {
8019 default:
8020 usage_cherrypick();
8021 /* NOTREACHED */
8025 argc -= optind;
8026 argv += optind;
8028 #ifndef PROFILE
8029 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8030 "unveil", NULL) == -1)
8031 err(1, "pledge");
8032 #endif
8033 if (argc != 1)
8034 usage_cherrypick();
8036 cwd = getcwd(NULL, 0);
8037 if (cwd == NULL) {
8038 error = got_error_from_errno("getcwd");
8039 goto done;
8041 error = got_worktree_open(&worktree, cwd);
8042 if (error) {
8043 if (error->code == GOT_ERR_NOT_WORKTREE)
8044 error = wrap_not_worktree_error(error, "cherrypick",
8045 cwd);
8046 goto done;
8049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8050 NULL);
8051 if (error != NULL)
8052 goto done;
8054 error = apply_unveil(got_repo_get_path(repo), 0,
8055 got_worktree_get_root_path(worktree));
8056 if (error)
8057 goto done;
8059 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8060 GOT_OBJ_TYPE_COMMIT, repo);
8061 if (error != NULL) {
8062 struct got_reference *ref;
8063 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8064 goto done;
8065 error = got_ref_open(&ref, repo, argv[0], 0);
8066 if (error != NULL)
8067 goto done;
8068 error = got_ref_resolve(&commit_id, repo, ref);
8069 got_ref_close(ref);
8070 if (error != NULL)
8071 goto done;
8073 error = got_object_id_str(&commit_id_str, commit_id);
8074 if (error)
8075 goto done;
8077 error = got_object_open_as_commit(&commit, repo, commit_id);
8078 if (error)
8079 goto done;
8080 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8081 memset(&upa, 0, sizeof(upa));
8082 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8083 commit_id, repo, update_progress, &upa, check_cancelled,
8084 NULL);
8085 if (error != NULL)
8086 goto done;
8088 if (upa.did_something)
8089 printf("Merged commit %s\n", commit_id_str);
8090 print_update_progress_stats(&upa);
8091 done:
8092 if (commit)
8093 got_object_commit_close(commit);
8094 free(commit_id_str);
8095 if (worktree)
8096 got_worktree_close(worktree);
8097 if (repo) {
8098 const struct got_error *close_err = got_repo_close(repo);
8099 if (error == NULL)
8100 error = close_err;
8102 return error;
8105 __dead static void
8106 usage_backout(void)
8108 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8109 exit(1);
8112 static const struct got_error *
8113 cmd_backout(int argc, char *argv[])
8115 const struct got_error *error = NULL;
8116 struct got_worktree *worktree = NULL;
8117 struct got_repository *repo = NULL;
8118 char *cwd = NULL, *commit_id_str = NULL;
8119 struct got_object_id *commit_id = NULL;
8120 struct got_commit_object *commit = NULL;
8121 struct got_object_qid *pid;
8122 int ch;
8123 struct got_update_progress_arg upa;
8125 while ((ch = getopt(argc, argv, "")) != -1) {
8126 switch (ch) {
8127 default:
8128 usage_backout();
8129 /* NOTREACHED */
8133 argc -= optind;
8134 argv += optind;
8136 #ifndef PROFILE
8137 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8138 "unveil", NULL) == -1)
8139 err(1, "pledge");
8140 #endif
8141 if (argc != 1)
8142 usage_backout();
8144 cwd = getcwd(NULL, 0);
8145 if (cwd == NULL) {
8146 error = got_error_from_errno("getcwd");
8147 goto done;
8149 error = got_worktree_open(&worktree, cwd);
8150 if (error) {
8151 if (error->code == GOT_ERR_NOT_WORKTREE)
8152 error = wrap_not_worktree_error(error, "backout", cwd);
8153 goto done;
8156 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8157 NULL);
8158 if (error != NULL)
8159 goto done;
8161 error = apply_unveil(got_repo_get_path(repo), 0,
8162 got_worktree_get_root_path(worktree));
8163 if (error)
8164 goto done;
8166 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8167 GOT_OBJ_TYPE_COMMIT, repo);
8168 if (error != NULL) {
8169 struct got_reference *ref;
8170 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8171 goto done;
8172 error = got_ref_open(&ref, repo, argv[0], 0);
8173 if (error != NULL)
8174 goto done;
8175 error = got_ref_resolve(&commit_id, repo, ref);
8176 got_ref_close(ref);
8177 if (error != NULL)
8178 goto done;
8180 error = got_object_id_str(&commit_id_str, commit_id);
8181 if (error)
8182 goto done;
8184 error = got_object_open_as_commit(&commit, repo, commit_id);
8185 if (error)
8186 goto done;
8187 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8188 if (pid == NULL) {
8189 error = got_error(GOT_ERR_ROOT_COMMIT);
8190 goto done;
8193 memset(&upa, 0, sizeof(upa));
8194 error = got_worktree_merge_files(worktree, commit_id, pid->id,
8195 repo, update_progress, &upa, check_cancelled, NULL);
8196 if (error != NULL)
8197 goto done;
8199 if (upa.did_something)
8200 printf("Backed out commit %s\n", commit_id_str);
8201 print_update_progress_stats(&upa);
8202 done:
8203 if (commit)
8204 got_object_commit_close(commit);
8205 free(commit_id_str);
8206 if (worktree)
8207 got_worktree_close(worktree);
8208 if (repo) {
8209 const struct got_error *close_err = got_repo_close(repo);
8210 if (error == NULL)
8211 error = close_err;
8213 return error;
8216 __dead static void
8217 usage_rebase(void)
8219 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8220 getprogname());
8221 exit(1);
8224 void
8225 trim_logmsg(char *logmsg, int limit)
8227 char *nl;
8228 size_t len;
8230 len = strlen(logmsg);
8231 if (len > limit)
8232 len = limit;
8233 logmsg[len] = '\0';
8234 nl = strchr(logmsg, '\n');
8235 if (nl)
8236 *nl = '\0';
8239 static const struct got_error *
8240 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8242 const struct got_error *err;
8243 char *logmsg0 = NULL;
8244 const char *s;
8246 err = got_object_commit_get_logmsg(&logmsg0, commit);
8247 if (err)
8248 return err;
8250 s = logmsg0;
8251 while (isspace((unsigned char)s[0]))
8252 s++;
8254 *logmsg = strdup(s);
8255 if (*logmsg == NULL) {
8256 err = got_error_from_errno("strdup");
8257 goto done;
8260 trim_logmsg(*logmsg, limit);
8261 done:
8262 free(logmsg0);
8263 return err;
8266 static const struct got_error *
8267 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8269 const struct got_error *err;
8270 struct got_commit_object *commit = NULL;
8271 char *id_str = NULL, *logmsg = NULL;
8273 err = got_object_open_as_commit(&commit, repo, id);
8274 if (err)
8275 return err;
8277 err = got_object_id_str(&id_str, id);
8278 if (err)
8279 goto done;
8281 id_str[12] = '\0';
8283 err = get_short_logmsg(&logmsg, 42, commit);
8284 if (err)
8285 goto done;
8287 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8288 done:
8289 free(id_str);
8290 got_object_commit_close(commit);
8291 free(logmsg);
8292 return err;
8295 static const struct got_error *
8296 show_rebase_progress(struct got_commit_object *commit,
8297 struct got_object_id *old_id, struct got_object_id *new_id)
8299 const struct got_error *err;
8300 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8302 err = got_object_id_str(&old_id_str, old_id);
8303 if (err)
8304 goto done;
8306 if (new_id) {
8307 err = got_object_id_str(&new_id_str, new_id);
8308 if (err)
8309 goto done;
8312 old_id_str[12] = '\0';
8313 if (new_id_str)
8314 new_id_str[12] = '\0';
8316 err = get_short_logmsg(&logmsg, 42, commit);
8317 if (err)
8318 goto done;
8320 printf("%s -> %s: %s\n", old_id_str,
8321 new_id_str ? new_id_str : "no-op change", logmsg);
8322 done:
8323 free(old_id_str);
8324 free(new_id_str);
8325 free(logmsg);
8326 return err;
8329 static const struct got_error *
8330 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8331 struct got_reference *branch, struct got_reference *new_base_branch,
8332 struct got_reference *tmp_branch, struct got_repository *repo,
8333 int create_backup)
8335 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8336 return got_worktree_rebase_complete(worktree, fileindex,
8337 new_base_branch, tmp_branch, branch, repo, create_backup);
8340 static const struct got_error *
8341 rebase_commit(struct got_pathlist_head *merged_paths,
8342 struct got_worktree *worktree, struct got_fileindex *fileindex,
8343 struct got_reference *tmp_branch,
8344 struct got_object_id *commit_id, struct got_repository *repo)
8346 const struct got_error *error;
8347 struct got_commit_object *commit;
8348 struct got_object_id *new_commit_id;
8350 error = got_object_open_as_commit(&commit, repo, commit_id);
8351 if (error)
8352 return error;
8354 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8355 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8356 if (error) {
8357 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8358 goto done;
8359 error = show_rebase_progress(commit, commit_id, NULL);
8360 } else {
8361 error = show_rebase_progress(commit, commit_id, new_commit_id);
8362 free(new_commit_id);
8364 done:
8365 got_object_commit_close(commit);
8366 return error;
8369 struct check_path_prefix_arg {
8370 const char *path_prefix;
8371 size_t len;
8372 int errcode;
8375 static const struct got_error *
8376 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8377 struct got_blob_object *blob2, struct got_object_id *id1,
8378 struct got_object_id *id2, const char *path1, const char *path2,
8379 mode_t mode1, mode_t mode2, struct got_repository *repo)
8381 struct check_path_prefix_arg *a = arg;
8383 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8384 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8385 return got_error(a->errcode);
8387 return NULL;
8390 static const struct got_error *
8391 check_path_prefix(struct got_object_id *parent_id,
8392 struct got_object_id *commit_id, const char *path_prefix,
8393 int errcode, struct got_repository *repo)
8395 const struct got_error *err;
8396 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8397 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8398 struct check_path_prefix_arg cpp_arg;
8400 if (got_path_is_root_dir(path_prefix))
8401 return NULL;
8403 err = got_object_open_as_commit(&commit, repo, commit_id);
8404 if (err)
8405 goto done;
8407 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8408 if (err)
8409 goto done;
8411 err = got_object_open_as_tree(&tree1, repo,
8412 got_object_commit_get_tree_id(parent_commit));
8413 if (err)
8414 goto done;
8416 err = got_object_open_as_tree(&tree2, repo,
8417 got_object_commit_get_tree_id(commit));
8418 if (err)
8419 goto done;
8421 cpp_arg.path_prefix = path_prefix;
8422 while (cpp_arg.path_prefix[0] == '/')
8423 cpp_arg.path_prefix++;
8424 cpp_arg.len = strlen(cpp_arg.path_prefix);
8425 cpp_arg.errcode = errcode;
8426 err = got_diff_tree(tree1, tree2, "", "", repo,
8427 check_path_prefix_in_diff, &cpp_arg, 0);
8428 done:
8429 if (tree1)
8430 got_object_tree_close(tree1);
8431 if (tree2)
8432 got_object_tree_close(tree2);
8433 if (commit)
8434 got_object_commit_close(commit);
8435 if (parent_commit)
8436 got_object_commit_close(parent_commit);
8437 return err;
8440 static const struct got_error *
8441 collect_commits(struct got_object_id_queue *commits,
8442 struct got_object_id *initial_commit_id,
8443 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8444 const char *path_prefix, int path_prefix_errcode,
8445 struct got_repository *repo)
8447 const struct got_error *err = NULL;
8448 struct got_commit_graph *graph = NULL;
8449 struct got_object_id *parent_id = NULL;
8450 struct got_object_qid *qid;
8451 struct got_object_id *commit_id = initial_commit_id;
8453 err = got_commit_graph_open(&graph, "/", 1);
8454 if (err)
8455 return err;
8457 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8458 check_cancelled, NULL);
8459 if (err)
8460 goto done;
8461 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8462 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8463 check_cancelled, NULL);
8464 if (err) {
8465 if (err->code == GOT_ERR_ITER_COMPLETED) {
8466 err = got_error_msg(GOT_ERR_ANCESTRY,
8467 "ran out of commits to rebase before "
8468 "youngest common ancestor commit has "
8469 "been reached?!?");
8471 goto done;
8472 } else {
8473 err = check_path_prefix(parent_id, commit_id,
8474 path_prefix, path_prefix_errcode, repo);
8475 if (err)
8476 goto done;
8478 err = got_object_qid_alloc(&qid, commit_id);
8479 if (err)
8480 goto done;
8481 STAILQ_INSERT_HEAD(commits, qid, entry);
8482 commit_id = parent_id;
8485 done:
8486 got_commit_graph_close(graph);
8487 return err;
8490 static const struct got_error *
8491 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8493 const struct got_error *err = NULL;
8494 time_t committer_time;
8495 struct tm tm;
8496 char datebuf[11]; /* YYYY-MM-DD + NUL */
8497 char *author0 = NULL, *author, *smallerthan;
8498 char *logmsg0 = NULL, *logmsg, *newline;
8500 committer_time = got_object_commit_get_committer_time(commit);
8501 if (gmtime_r(&committer_time, &tm) == NULL)
8502 return got_error_from_errno("gmtime_r");
8503 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8504 return got_error(GOT_ERR_NO_SPACE);
8506 author0 = strdup(got_object_commit_get_author(commit));
8507 if (author0 == NULL)
8508 return got_error_from_errno("strdup");
8509 author = author0;
8510 smallerthan = strchr(author, '<');
8511 if (smallerthan && smallerthan[1] != '\0')
8512 author = smallerthan + 1;
8513 author[strcspn(author, "@>")] = '\0';
8515 err = got_object_commit_get_logmsg(&logmsg0, commit);
8516 if (err)
8517 goto done;
8518 logmsg = logmsg0;
8519 while (*logmsg == '\n')
8520 logmsg++;
8521 newline = strchr(logmsg, '\n');
8522 if (newline)
8523 *newline = '\0';
8525 if (asprintf(brief_str, "%s %s %s",
8526 datebuf, author, logmsg) == -1)
8527 err = got_error_from_errno("asprintf");
8528 done:
8529 free(author0);
8530 free(logmsg0);
8531 return err;
8534 static const struct got_error *
8535 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8536 struct got_repository *repo)
8538 const struct got_error *err;
8539 char *id_str;
8541 err = got_object_id_str(&id_str, id);
8542 if (err)
8543 return err;
8545 err = got_ref_delete(ref, repo);
8546 if (err)
8547 goto done;
8549 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8550 done:
8551 free(id_str);
8552 return err;
8555 static const struct got_error *
8556 print_backup_ref(const char *branch_name, const char *new_id_str,
8557 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8558 struct got_reflist_object_id_map *refs_idmap,
8559 struct got_repository *repo)
8561 const struct got_error *err = NULL;
8562 struct got_reflist_head *refs;
8563 char *refs_str = NULL;
8564 struct got_object_id *new_commit_id = NULL;
8565 struct got_commit_object *new_commit = NULL;
8566 char *new_commit_brief_str = NULL;
8567 struct got_object_id *yca_id = NULL;
8568 struct got_commit_object *yca_commit = NULL;
8569 char *yca_id_str = NULL, *yca_brief_str = NULL;
8570 char *custom_refs_str;
8572 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8573 return got_error_from_errno("asprintf");
8575 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8576 0, 0, refs_idmap, custom_refs_str);
8577 if (err)
8578 goto done;
8580 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8581 if (err)
8582 goto done;
8584 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8585 if (refs) {
8586 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8587 if (err)
8588 goto done;
8591 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8592 if (err)
8593 goto done;
8595 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8596 if (err)
8597 goto done;
8599 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8600 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8601 if (err)
8602 goto done;
8604 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8605 refs_str ? " (" : "", refs_str ? refs_str : "",
8606 refs_str ? ")" : "", new_commit_brief_str);
8607 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8608 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8609 free(refs_str);
8610 refs_str = NULL;
8612 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8613 if (err)
8614 goto done;
8616 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8617 if (err)
8618 goto done;
8620 err = got_object_id_str(&yca_id_str, yca_id);
8621 if (err)
8622 goto done;
8624 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8625 if (refs) {
8626 err = build_refs_str(&refs_str, refs, yca_id, repo);
8627 if (err)
8628 goto done;
8630 printf("history forked at %s%s%s%s\n %s\n",
8631 yca_id_str,
8632 refs_str ? " (" : "", refs_str ? refs_str : "",
8633 refs_str ? ")" : "", yca_brief_str);
8635 done:
8636 free(custom_refs_str);
8637 free(new_commit_id);
8638 free(refs_str);
8639 free(yca_id);
8640 free(yca_id_str);
8641 free(yca_brief_str);
8642 if (new_commit)
8643 got_object_commit_close(new_commit);
8644 if (yca_commit)
8645 got_object_commit_close(yca_commit);
8647 return NULL;
8650 static const struct got_error *
8651 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8652 int delete, struct got_repository *repo)
8654 const struct got_error *err;
8655 struct got_reflist_head refs, backup_refs;
8656 struct got_reflist_entry *re;
8657 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8658 struct got_object_id *old_commit_id = NULL;
8659 char *branch_name = NULL;
8660 struct got_commit_object *old_commit = NULL;
8661 struct got_reflist_object_id_map *refs_idmap = NULL;
8662 int wanted_branch_found = 0;
8664 TAILQ_INIT(&refs);
8665 TAILQ_INIT(&backup_refs);
8667 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8668 if (err)
8669 return err;
8671 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8672 if (err)
8673 goto done;
8675 if (wanted_branch_name) {
8676 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8677 wanted_branch_name += 11;
8680 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8681 got_ref_cmp_by_commit_timestamp_descending, repo);
8682 if (err)
8683 goto done;
8685 TAILQ_FOREACH(re, &backup_refs, entry) {
8686 const char *refname = got_ref_get_name(re->ref);
8687 char *slash;
8689 err = check_cancelled(NULL);
8690 if (err)
8691 break;
8693 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8694 if (err)
8695 break;
8697 err = got_object_open_as_commit(&old_commit, repo,
8698 old_commit_id);
8699 if (err)
8700 break;
8702 if (strncmp(backup_ref_prefix, refname,
8703 backup_ref_prefix_len) == 0)
8704 refname += backup_ref_prefix_len;
8706 while (refname[0] == '/')
8707 refname++;
8709 branch_name = strdup(refname);
8710 if (branch_name == NULL) {
8711 err = got_error_from_errno("strdup");
8712 break;
8714 slash = strrchr(branch_name, '/');
8715 if (slash) {
8716 *slash = '\0';
8717 refname += strlen(branch_name) + 1;
8720 if (wanted_branch_name == NULL ||
8721 strcmp(wanted_branch_name, branch_name) == 0) {
8722 wanted_branch_found = 1;
8723 if (delete) {
8724 err = delete_backup_ref(re->ref,
8725 old_commit_id, repo);
8726 } else {
8727 err = print_backup_ref(branch_name, refname,
8728 old_commit_id, old_commit, refs_idmap,
8729 repo);
8731 if (err)
8732 break;
8735 free(old_commit_id);
8736 old_commit_id = NULL;
8737 free(branch_name);
8738 branch_name = NULL;
8739 got_object_commit_close(old_commit);
8740 old_commit = NULL;
8743 if (wanted_branch_name && !wanted_branch_found) {
8744 err = got_error_fmt(GOT_ERR_NOT_REF,
8745 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8747 done:
8748 if (refs_idmap)
8749 got_reflist_object_id_map_free(refs_idmap);
8750 got_ref_list_free(&refs);
8751 got_ref_list_free(&backup_refs);
8752 free(old_commit_id);
8753 free(branch_name);
8754 if (old_commit)
8755 got_object_commit_close(old_commit);
8756 return err;
8759 static const struct got_error *
8760 cmd_rebase(int argc, char *argv[])
8762 const struct got_error *error = NULL;
8763 struct got_worktree *worktree = NULL;
8764 struct got_repository *repo = NULL;
8765 struct got_fileindex *fileindex = NULL;
8766 char *cwd = NULL;
8767 struct got_reference *branch = NULL;
8768 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8769 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8770 struct got_object_id *resume_commit_id = NULL;
8771 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8772 struct got_commit_object *commit = NULL;
8773 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8774 int histedit_in_progress = 0, merge_in_progress = 0;
8775 int create_backup = 1, list_backups = 0, delete_backups = 0;
8776 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8777 struct got_object_id_queue commits;
8778 struct got_pathlist_head merged_paths;
8779 const struct got_object_id_queue *parent_ids;
8780 struct got_object_qid *qid, *pid;
8782 STAILQ_INIT(&commits);
8783 TAILQ_INIT(&merged_paths);
8785 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8786 switch (ch) {
8787 case 'a':
8788 abort_rebase = 1;
8789 break;
8790 case 'c':
8791 continue_rebase = 1;
8792 break;
8793 case 'l':
8794 list_backups = 1;
8795 break;
8796 case 'X':
8797 delete_backups = 1;
8798 break;
8799 default:
8800 usage_rebase();
8801 /* NOTREACHED */
8805 argc -= optind;
8806 argv += optind;
8808 #ifndef PROFILE
8809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8810 "unveil", NULL) == -1)
8811 err(1, "pledge");
8812 #endif
8813 if (list_backups) {
8814 if (abort_rebase)
8815 option_conflict('l', 'a');
8816 if (continue_rebase)
8817 option_conflict('l', 'c');
8818 if (delete_backups)
8819 option_conflict('l', 'X');
8820 if (argc != 0 && argc != 1)
8821 usage_rebase();
8822 } else if (delete_backups) {
8823 if (abort_rebase)
8824 option_conflict('X', 'a');
8825 if (continue_rebase)
8826 option_conflict('X', 'c');
8827 if (list_backups)
8828 option_conflict('l', 'X');
8829 if (argc != 0 && argc != 1)
8830 usage_rebase();
8831 } else {
8832 if (abort_rebase && continue_rebase)
8833 usage_rebase();
8834 else if (abort_rebase || continue_rebase) {
8835 if (argc != 0)
8836 usage_rebase();
8837 } else if (argc != 1)
8838 usage_rebase();
8841 cwd = getcwd(NULL, 0);
8842 if (cwd == NULL) {
8843 error = got_error_from_errno("getcwd");
8844 goto done;
8846 error = got_worktree_open(&worktree, cwd);
8847 if (error) {
8848 if (list_backups || delete_backups) {
8849 if (error->code != GOT_ERR_NOT_WORKTREE)
8850 goto done;
8851 } else {
8852 if (error->code == GOT_ERR_NOT_WORKTREE)
8853 error = wrap_not_worktree_error(error,
8854 "rebase", cwd);
8855 goto done;
8859 error = got_repo_open(&repo,
8860 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8861 if (error != NULL)
8862 goto done;
8864 error = apply_unveil(got_repo_get_path(repo), 0,
8865 worktree ? got_worktree_get_root_path(worktree) : NULL);
8866 if (error)
8867 goto done;
8869 if (list_backups || delete_backups) {
8870 error = process_backup_refs(
8871 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8872 argc == 1 ? argv[0] : NULL, delete_backups, repo);
8873 goto done; /* nothing else to do */
8876 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8877 worktree);
8878 if (error)
8879 goto done;
8880 if (histedit_in_progress) {
8881 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8882 goto done;
8885 error = got_worktree_merge_in_progress(&merge_in_progress,
8886 worktree, repo);
8887 if (error)
8888 goto done;
8889 if (merge_in_progress) {
8890 error = got_error(GOT_ERR_MERGE_BUSY);
8891 goto done;
8894 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8895 if (error)
8896 goto done;
8898 if (abort_rebase) {
8899 struct got_update_progress_arg upa;
8900 if (!rebase_in_progress) {
8901 error = got_error(GOT_ERR_NOT_REBASING);
8902 goto done;
8904 error = got_worktree_rebase_continue(&resume_commit_id,
8905 &new_base_branch, &tmp_branch, &branch, &fileindex,
8906 worktree, repo);
8907 if (error)
8908 goto done;
8909 printf("Switching work tree to %s\n",
8910 got_ref_get_symref_target(new_base_branch));
8911 memset(&upa, 0, sizeof(upa));
8912 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8913 new_base_branch, update_progress, &upa);
8914 if (error)
8915 goto done;
8916 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8917 print_update_progress_stats(&upa);
8918 goto done; /* nothing else to do */
8921 if (continue_rebase) {
8922 if (!rebase_in_progress) {
8923 error = got_error(GOT_ERR_NOT_REBASING);
8924 goto done;
8926 error = got_worktree_rebase_continue(&resume_commit_id,
8927 &new_base_branch, &tmp_branch, &branch, &fileindex,
8928 worktree, repo);
8929 if (error)
8930 goto done;
8932 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8933 resume_commit_id, repo);
8934 if (error)
8935 goto done;
8937 yca_id = got_object_id_dup(resume_commit_id);
8938 if (yca_id == NULL) {
8939 error = got_error_from_errno("got_object_id_dup");
8940 goto done;
8942 } else {
8943 error = got_ref_open(&branch, repo, argv[0], 0);
8944 if (error != NULL)
8945 goto done;
8948 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8949 if (error)
8950 goto done;
8952 if (!continue_rebase) {
8953 struct got_object_id *base_commit_id;
8955 base_commit_id = got_worktree_get_base_commit_id(worktree);
8956 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8957 base_commit_id, branch_head_commit_id, 1, repo,
8958 check_cancelled, NULL);
8959 if (error)
8960 goto done;
8961 if (yca_id == NULL) {
8962 error = got_error_msg(GOT_ERR_ANCESTRY,
8963 "specified branch shares no common ancestry "
8964 "with work tree's branch");
8965 goto done;
8968 error = check_same_branch(base_commit_id, branch, yca_id, repo);
8969 if (error) {
8970 if (error->code != GOT_ERR_ANCESTRY)
8971 goto done;
8972 error = NULL;
8973 } else {
8974 static char msg[128];
8975 snprintf(msg, sizeof(msg),
8976 "%s is already based on %s",
8977 got_ref_get_name(branch),
8978 got_worktree_get_head_ref_name(worktree));
8979 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8980 goto done;
8982 error = got_worktree_rebase_prepare(&new_base_branch,
8983 &tmp_branch, &fileindex, worktree, branch, repo);
8984 if (error)
8985 goto done;
8988 commit_id = branch_head_commit_id;
8989 error = got_object_open_as_commit(&commit, repo, commit_id);
8990 if (error)
8991 goto done;
8993 parent_ids = got_object_commit_get_parent_ids(commit);
8994 pid = STAILQ_FIRST(parent_ids);
8995 if (pid == NULL) {
8996 if (!continue_rebase) {
8997 struct got_update_progress_arg upa;
8998 memset(&upa, 0, sizeof(upa));
8999 error = got_worktree_rebase_abort(worktree, fileindex,
9000 repo, new_base_branch, update_progress, &upa);
9001 if (error)
9002 goto done;
9003 printf("Rebase of %s aborted\n",
9004 got_ref_get_name(branch));
9005 print_update_progress_stats(&upa);
9008 error = got_error(GOT_ERR_EMPTY_REBASE);
9009 goto done;
9011 error = collect_commits(&commits, commit_id, pid->id,
9012 yca_id, got_worktree_get_path_prefix(worktree),
9013 GOT_ERR_REBASE_PATH, repo);
9014 got_object_commit_close(commit);
9015 commit = NULL;
9016 if (error)
9017 goto done;
9019 if (STAILQ_EMPTY(&commits)) {
9020 if (continue_rebase) {
9021 error = rebase_complete(worktree, fileindex,
9022 branch, new_base_branch, tmp_branch, repo,
9023 create_backup);
9024 goto done;
9025 } else {
9026 /* Fast-forward the reference of the branch. */
9027 struct got_object_id *new_head_commit_id;
9028 char *id_str;
9029 error = got_ref_resolve(&new_head_commit_id, repo,
9030 new_base_branch);
9031 if (error)
9032 goto done;
9033 error = got_object_id_str(&id_str, new_head_commit_id);
9034 printf("Forwarding %s to commit %s\n",
9035 got_ref_get_name(branch), id_str);
9036 free(id_str);
9037 error = got_ref_change_ref(branch,
9038 new_head_commit_id);
9039 if (error)
9040 goto done;
9041 /* No backup needed since objects did not change. */
9042 create_backup = 0;
9046 pid = NULL;
9047 STAILQ_FOREACH(qid, &commits, entry) {
9048 struct got_update_progress_arg upa;
9050 commit_id = qid->id;
9051 parent_id = pid ? pid->id : yca_id;
9052 pid = qid;
9054 memset(&upa, 0, sizeof(upa));
9055 error = got_worktree_rebase_merge_files(&merged_paths,
9056 worktree, fileindex, parent_id, commit_id, repo,
9057 update_progress, &upa, check_cancelled, NULL);
9058 if (error)
9059 goto done;
9061 print_update_progress_stats(&upa);
9062 if (upa.conflicts > 0)
9063 rebase_status = GOT_STATUS_CONFLICT;
9065 if (rebase_status == GOT_STATUS_CONFLICT) {
9066 error = show_rebase_merge_conflict(qid->id, repo);
9067 if (error)
9068 goto done;
9069 got_worktree_rebase_pathlist_free(&merged_paths);
9070 break;
9073 error = rebase_commit(&merged_paths, worktree, fileindex,
9074 tmp_branch, commit_id, repo);
9075 got_worktree_rebase_pathlist_free(&merged_paths);
9076 if (error)
9077 goto done;
9080 if (rebase_status == GOT_STATUS_CONFLICT) {
9081 error = got_worktree_rebase_postpone(worktree, fileindex);
9082 if (error)
9083 goto done;
9084 error = got_error_msg(GOT_ERR_CONFLICTS,
9085 "conflicts must be resolved before rebasing can continue");
9086 } else
9087 error = rebase_complete(worktree, fileindex, branch,
9088 new_base_branch, tmp_branch, repo, create_backup);
9089 done:
9090 got_object_id_queue_free(&commits);
9091 free(branch_head_commit_id);
9092 free(resume_commit_id);
9093 free(yca_id);
9094 if (commit)
9095 got_object_commit_close(commit);
9096 if (branch)
9097 got_ref_close(branch);
9098 if (new_base_branch)
9099 got_ref_close(new_base_branch);
9100 if (tmp_branch)
9101 got_ref_close(tmp_branch);
9102 if (worktree)
9103 got_worktree_close(worktree);
9104 if (repo) {
9105 const struct got_error *close_err = got_repo_close(repo);
9106 if (error == NULL)
9107 error = close_err;
9109 return error;
9112 __dead static void
9113 usage_histedit(void)
9115 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9116 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9117 getprogname());
9118 exit(1);
9121 #define GOT_HISTEDIT_PICK 'p'
9122 #define GOT_HISTEDIT_EDIT 'e'
9123 #define GOT_HISTEDIT_FOLD 'f'
9124 #define GOT_HISTEDIT_DROP 'd'
9125 #define GOT_HISTEDIT_MESG 'm'
9127 static struct got_histedit_cmd {
9128 unsigned char code;
9129 const char *name;
9130 const char *desc;
9131 } got_histedit_cmds[] = {
9132 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9133 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9134 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9135 "be used" },
9136 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9137 { GOT_HISTEDIT_MESG, "mesg",
9138 "single-line log message for commit above (open editor if empty)" },
9141 struct got_histedit_list_entry {
9142 TAILQ_ENTRY(got_histedit_list_entry) entry;
9143 struct got_object_id *commit_id;
9144 const struct got_histedit_cmd *cmd;
9145 char *logmsg;
9147 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9149 static const struct got_error *
9150 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9151 FILE *f, struct got_repository *repo)
9153 const struct got_error *err = NULL;
9154 char *logmsg = NULL, *id_str = NULL;
9155 struct got_commit_object *commit = NULL;
9156 int n;
9158 err = got_object_open_as_commit(&commit, repo, commit_id);
9159 if (err)
9160 goto done;
9162 err = get_short_logmsg(&logmsg, 34, commit);
9163 if (err)
9164 goto done;
9166 err = got_object_id_str(&id_str, commit_id);
9167 if (err)
9168 goto done;
9170 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9171 if (n < 0)
9172 err = got_ferror(f, GOT_ERR_IO);
9173 done:
9174 if (commit)
9175 got_object_commit_close(commit);
9176 free(id_str);
9177 free(logmsg);
9178 return err;
9181 static const struct got_error *
9182 histedit_write_commit_list(struct got_object_id_queue *commits,
9183 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9185 const struct got_error *err = NULL;
9186 struct got_object_qid *qid;
9187 const char *histedit_cmd = NULL;
9189 if (STAILQ_EMPTY(commits))
9190 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9192 STAILQ_FOREACH(qid, commits, entry) {
9193 histedit_cmd = got_histedit_cmds[0].name;
9194 if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9195 histedit_cmd = "fold";
9196 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9197 if (err)
9198 break;
9199 if (edit_logmsg_only) {
9200 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9201 if (n < 0) {
9202 err = got_ferror(f, GOT_ERR_IO);
9203 break;
9208 return err;
9211 static const struct got_error *
9212 write_cmd_list(FILE *f, const char *branch_name,
9213 struct got_object_id_queue *commits)
9215 const struct got_error *err = NULL;
9216 size_t i;
9217 int n;
9218 char *id_str;
9219 struct got_object_qid *qid;
9221 qid = STAILQ_FIRST(commits);
9222 err = got_object_id_str(&id_str, qid->id);
9223 if (err)
9224 return err;
9226 n = fprintf(f,
9227 "# Editing the history of branch '%s' starting at\n"
9228 "# commit %s\n"
9229 "# Commits will be processed in order from top to "
9230 "bottom of this file.\n", branch_name, id_str);
9231 if (n < 0) {
9232 err = got_ferror(f, GOT_ERR_IO);
9233 goto done;
9236 n = fprintf(f, "# Available histedit commands:\n");
9237 if (n < 0) {
9238 err = got_ferror(f, GOT_ERR_IO);
9239 goto done;
9242 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9243 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9244 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9245 cmd->desc);
9246 if (n < 0) {
9247 err = got_ferror(f, GOT_ERR_IO);
9248 break;
9251 done:
9252 free(id_str);
9253 return err;
9256 static const struct got_error *
9257 histedit_syntax_error(int lineno)
9259 static char msg[42];
9260 int ret;
9262 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9263 lineno);
9264 if (ret == -1 || ret >= sizeof(msg))
9265 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9267 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9270 static const struct got_error *
9271 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9272 char *logmsg, struct got_repository *repo)
9274 const struct got_error *err;
9275 struct got_commit_object *folded_commit = NULL;
9276 char *id_str, *folded_logmsg = NULL;
9278 err = got_object_id_str(&id_str, hle->commit_id);
9279 if (err)
9280 return err;
9282 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9283 if (err)
9284 goto done;
9286 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9287 if (err)
9288 goto done;
9289 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9290 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9291 folded_logmsg) == -1) {
9292 err = got_error_from_errno("asprintf");
9294 done:
9295 if (folded_commit)
9296 got_object_commit_close(folded_commit);
9297 free(id_str);
9298 free(folded_logmsg);
9299 return err;
9302 static struct got_histedit_list_entry *
9303 get_folded_commits(struct got_histedit_list_entry *hle)
9305 struct got_histedit_list_entry *prev, *folded = NULL;
9307 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9308 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9309 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9310 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9311 folded = prev;
9312 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9315 return folded;
9318 static const struct got_error *
9319 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9320 struct got_repository *repo)
9322 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9323 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9324 const struct got_error *err = NULL;
9325 struct got_commit_object *commit = NULL;
9326 int logmsg_len;
9327 int fd;
9328 struct got_histedit_list_entry *folded = NULL;
9330 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9331 if (err)
9332 return err;
9334 folded = get_folded_commits(hle);
9335 if (folded) {
9336 while (folded != hle) {
9337 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9338 folded = TAILQ_NEXT(folded, entry);
9339 continue;
9341 err = append_folded_commit_msg(&new_msg, folded,
9342 logmsg, repo);
9343 if (err)
9344 goto done;
9345 free(logmsg);
9346 logmsg = new_msg;
9347 folded = TAILQ_NEXT(folded, entry);
9351 err = got_object_id_str(&id_str, hle->commit_id);
9352 if (err)
9353 goto done;
9354 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9355 if (err)
9356 goto done;
9357 logmsg_len = asprintf(&new_msg,
9358 "%s\n# original log message of commit %s: %s",
9359 logmsg ? logmsg : "", id_str, orig_logmsg);
9360 if (logmsg_len == -1) {
9361 err = got_error_from_errno("asprintf");
9362 goto done;
9364 free(logmsg);
9365 logmsg = new_msg;
9367 err = got_object_id_str(&id_str, hle->commit_id);
9368 if (err)
9369 goto done;
9371 err = got_opentemp_named_fd(&logmsg_path, &fd,
9372 GOT_TMPDIR_STR "/got-logmsg");
9373 if (err)
9374 goto done;
9376 write(fd, logmsg, logmsg_len);
9377 close(fd);
9379 err = get_editor(&editor);
9380 if (err)
9381 goto done;
9383 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9384 logmsg_len, 0);
9385 if (err) {
9386 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9387 goto done;
9388 err = NULL;
9389 hle->logmsg = strdup(new_msg);
9390 if (hle->logmsg == NULL)
9391 err = got_error_from_errno("strdup");
9393 done:
9394 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9395 err = got_error_from_errno2("unlink", logmsg_path);
9396 free(logmsg_path);
9397 free(logmsg);
9398 free(orig_logmsg);
9399 free(editor);
9400 if (commit)
9401 got_object_commit_close(commit);
9402 return err;
9405 static const struct got_error *
9406 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9407 FILE *f, struct got_repository *repo)
9409 const struct got_error *err = NULL;
9410 char *line = NULL, *p, *end;
9411 size_t i, size;
9412 ssize_t len;
9413 int lineno = 0;
9414 const struct got_histedit_cmd *cmd;
9415 struct got_object_id *commit_id = NULL;
9416 struct got_histedit_list_entry *hle = NULL;
9418 for (;;) {
9419 len = getline(&line, &size, f);
9420 if (len == -1) {
9421 const struct got_error *getline_err;
9422 if (feof(f))
9423 break;
9424 getline_err = got_error_from_errno("getline");
9425 err = got_ferror(f, getline_err->code);
9426 break;
9428 lineno++;
9429 p = line;
9430 while (isspace((unsigned char)p[0]))
9431 p++;
9432 if (p[0] == '#' || p[0] == '\0') {
9433 free(line);
9434 line = NULL;
9435 continue;
9437 cmd = NULL;
9438 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9439 cmd = &got_histedit_cmds[i];
9440 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9441 isspace((unsigned char)p[strlen(cmd->name)])) {
9442 p += strlen(cmd->name);
9443 break;
9445 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9446 p++;
9447 break;
9450 if (i == nitems(got_histedit_cmds)) {
9451 err = histedit_syntax_error(lineno);
9452 break;
9454 while (isspace((unsigned char)p[0]))
9455 p++;
9456 if (cmd->code == GOT_HISTEDIT_MESG) {
9457 if (hle == NULL || hle->logmsg != NULL) {
9458 err = got_error(GOT_ERR_HISTEDIT_CMD);
9459 break;
9461 if (p[0] == '\0') {
9462 err = histedit_edit_logmsg(hle, repo);
9463 if (err)
9464 break;
9465 } else {
9466 hle->logmsg = strdup(p);
9467 if (hle->logmsg == NULL) {
9468 err = got_error_from_errno("strdup");
9469 break;
9472 free(line);
9473 line = NULL;
9474 continue;
9475 } else {
9476 end = p;
9477 while (end[0] && !isspace((unsigned char)end[0]))
9478 end++;
9479 *end = '\0';
9481 err = got_object_resolve_id_str(&commit_id, repo, p);
9482 if (err) {
9483 /* override error code */
9484 err = histedit_syntax_error(lineno);
9485 break;
9488 hle = malloc(sizeof(*hle));
9489 if (hle == NULL) {
9490 err = got_error_from_errno("malloc");
9491 break;
9493 hle->cmd = cmd;
9494 hle->commit_id = commit_id;
9495 hle->logmsg = NULL;
9496 commit_id = NULL;
9497 free(line);
9498 line = NULL;
9499 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9502 free(line);
9503 free(commit_id);
9504 return err;
9507 static const struct got_error *
9508 histedit_check_script(struct got_histedit_list *histedit_cmds,
9509 struct got_object_id_queue *commits, struct got_repository *repo)
9511 const struct got_error *err = NULL;
9512 struct got_object_qid *qid;
9513 struct got_histedit_list_entry *hle;
9514 static char msg[92];
9515 char *id_str;
9517 if (TAILQ_EMPTY(histedit_cmds))
9518 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9519 "histedit script contains no commands");
9520 if (STAILQ_EMPTY(commits))
9521 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9523 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9524 struct got_histedit_list_entry *hle2;
9525 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9526 if (hle == hle2)
9527 continue;
9528 if (got_object_id_cmp(hle->commit_id,
9529 hle2->commit_id) != 0)
9530 continue;
9531 err = got_object_id_str(&id_str, hle->commit_id);
9532 if (err)
9533 return err;
9534 snprintf(msg, sizeof(msg), "commit %s is listed "
9535 "more than once in histedit script", id_str);
9536 free(id_str);
9537 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9541 STAILQ_FOREACH(qid, commits, entry) {
9542 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9543 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9544 break;
9546 if (hle == NULL) {
9547 err = got_object_id_str(&id_str, qid->id);
9548 if (err)
9549 return err;
9550 snprintf(msg, sizeof(msg),
9551 "commit %s missing from histedit script", id_str);
9552 free(id_str);
9553 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9557 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9558 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9559 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9560 "last commit in histedit script cannot be folded");
9562 return NULL;
9565 static const struct got_error *
9566 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9567 const char *path, struct got_object_id_queue *commits,
9568 struct got_repository *repo)
9570 const struct got_error *err = NULL;
9571 char *editor;
9572 FILE *f = NULL;
9574 err = get_editor(&editor);
9575 if (err)
9576 return err;
9578 if (spawn_editor(editor, path) == -1) {
9579 err = got_error_from_errno("failed spawning editor");
9580 goto done;
9583 f = fopen(path, "r");
9584 if (f == NULL) {
9585 err = got_error_from_errno("fopen");
9586 goto done;
9588 err = histedit_parse_list(histedit_cmds, f, repo);
9589 if (err)
9590 goto done;
9592 err = histedit_check_script(histedit_cmds, commits, repo);
9593 done:
9594 if (f && fclose(f) == EOF && err == NULL)
9595 err = got_error_from_errno("fclose");
9596 free(editor);
9597 return err;
9600 static const struct got_error *
9601 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9602 struct got_object_id_queue *, const char *, const char *,
9603 struct got_repository *);
9605 static const struct got_error *
9606 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9607 struct got_object_id_queue *commits, const char *branch_name,
9608 int edit_logmsg_only, int fold_only, struct got_repository *repo)
9610 const struct got_error *err;
9611 FILE *f = NULL;
9612 char *path = NULL;
9614 err = got_opentemp_named(&path, &f, "got-histedit");
9615 if (err)
9616 return err;
9618 err = write_cmd_list(f, branch_name, commits);
9619 if (err)
9620 goto done;
9622 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9623 fold_only, repo);
9624 if (err)
9625 goto done;
9627 if (edit_logmsg_only || fold_only) {
9628 rewind(f);
9629 err = histedit_parse_list(histedit_cmds, f, repo);
9630 } else {
9631 if (fclose(f) == EOF) {
9632 err = got_error_from_errno("fclose");
9633 goto done;
9635 f = NULL;
9636 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9637 if (err) {
9638 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9639 err->code != GOT_ERR_HISTEDIT_CMD)
9640 goto done;
9641 err = histedit_edit_list_retry(histedit_cmds, err,
9642 commits, path, branch_name, repo);
9645 done:
9646 if (f && fclose(f) == EOF && err == NULL)
9647 err = got_error_from_errno("fclose");
9648 if (path && unlink(path) != 0 && err == NULL)
9649 err = got_error_from_errno2("unlink", path);
9650 free(path);
9651 return err;
9654 static const struct got_error *
9655 histedit_save_list(struct got_histedit_list *histedit_cmds,
9656 struct got_worktree *worktree, struct got_repository *repo)
9658 const struct got_error *err = NULL;
9659 char *path = NULL;
9660 FILE *f = NULL;
9661 struct got_histedit_list_entry *hle;
9662 struct got_commit_object *commit = NULL;
9664 err = got_worktree_get_histedit_script_path(&path, worktree);
9665 if (err)
9666 return err;
9668 f = fopen(path, "w");
9669 if (f == NULL) {
9670 err = got_error_from_errno2("fopen", path);
9671 goto done;
9673 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9674 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9675 repo);
9676 if (err)
9677 break;
9679 if (hle->logmsg) {
9680 int n = fprintf(f, "%c %s\n",
9681 GOT_HISTEDIT_MESG, hle->logmsg);
9682 if (n < 0) {
9683 err = got_ferror(f, GOT_ERR_IO);
9684 break;
9688 done:
9689 if (f && fclose(f) == EOF && err == NULL)
9690 err = got_error_from_errno("fclose");
9691 free(path);
9692 if (commit)
9693 got_object_commit_close(commit);
9694 return err;
9697 void
9698 histedit_free_list(struct got_histedit_list *histedit_cmds)
9700 struct got_histedit_list_entry *hle;
9702 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9703 TAILQ_REMOVE(histedit_cmds, hle, entry);
9704 free(hle);
9708 static const struct got_error *
9709 histedit_load_list(struct got_histedit_list *histedit_cmds,
9710 const char *path, struct got_repository *repo)
9712 const struct got_error *err = NULL;
9713 FILE *f = NULL;
9715 f = fopen(path, "r");
9716 if (f == NULL) {
9717 err = got_error_from_errno2("fopen", path);
9718 goto done;
9721 err = histedit_parse_list(histedit_cmds, f, repo);
9722 done:
9723 if (f && fclose(f) == EOF && err == NULL)
9724 err = got_error_from_errno("fclose");
9725 return err;
9728 static const struct got_error *
9729 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9730 const struct got_error *edit_err, struct got_object_id_queue *commits,
9731 const char *path, const char *branch_name, struct got_repository *repo)
9733 const struct got_error *err = NULL, *prev_err = edit_err;
9734 int resp = ' ';
9736 while (resp != 'c' && resp != 'r' && resp != 'a') {
9737 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9738 "or (a)bort: ", getprogname(), prev_err->msg);
9739 resp = getchar();
9740 if (resp == '\n')
9741 resp = getchar();
9742 if (resp == 'c') {
9743 histedit_free_list(histedit_cmds);
9744 err = histedit_run_editor(histedit_cmds, path, commits,
9745 repo);
9746 if (err) {
9747 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9748 err->code != GOT_ERR_HISTEDIT_CMD)
9749 break;
9750 prev_err = err;
9751 resp = ' ';
9752 continue;
9754 break;
9755 } else if (resp == 'r') {
9756 histedit_free_list(histedit_cmds);
9757 err = histedit_edit_script(histedit_cmds,
9758 commits, branch_name, 0, 0, repo);
9759 if (err) {
9760 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9761 err->code != GOT_ERR_HISTEDIT_CMD)
9762 break;
9763 prev_err = err;
9764 resp = ' ';
9765 continue;
9767 break;
9768 } else if (resp == 'a') {
9769 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9770 break;
9771 } else
9772 printf("invalid response '%c'\n", resp);
9775 return err;
9778 static const struct got_error *
9779 histedit_complete(struct got_worktree *worktree,
9780 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9781 struct got_reference *branch, struct got_repository *repo)
9783 printf("Switching work tree to %s\n",
9784 got_ref_get_symref_target(branch));
9785 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9786 branch, repo);
9789 static const struct got_error *
9790 show_histedit_progress(struct got_commit_object *commit,
9791 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9793 const struct got_error *err;
9794 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9796 err = got_object_id_str(&old_id_str, hle->commit_id);
9797 if (err)
9798 goto done;
9800 if (new_id) {
9801 err = got_object_id_str(&new_id_str, new_id);
9802 if (err)
9803 goto done;
9806 old_id_str[12] = '\0';
9807 if (new_id_str)
9808 new_id_str[12] = '\0';
9810 if (hle->logmsg) {
9811 logmsg = strdup(hle->logmsg);
9812 if (logmsg == NULL) {
9813 err = got_error_from_errno("strdup");
9814 goto done;
9816 trim_logmsg(logmsg, 42);
9817 } else {
9818 err = get_short_logmsg(&logmsg, 42, commit);
9819 if (err)
9820 goto done;
9823 switch (hle->cmd->code) {
9824 case GOT_HISTEDIT_PICK:
9825 case GOT_HISTEDIT_EDIT:
9826 printf("%s -> %s: %s\n", old_id_str,
9827 new_id_str ? new_id_str : "no-op change", logmsg);
9828 break;
9829 case GOT_HISTEDIT_DROP:
9830 case GOT_HISTEDIT_FOLD:
9831 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9832 logmsg);
9833 break;
9834 default:
9835 break;
9837 done:
9838 free(old_id_str);
9839 free(new_id_str);
9840 return err;
9843 static const struct got_error *
9844 histedit_commit(struct got_pathlist_head *merged_paths,
9845 struct got_worktree *worktree, struct got_fileindex *fileindex,
9846 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9847 struct got_repository *repo)
9849 const struct got_error *err;
9850 struct got_commit_object *commit;
9851 struct got_object_id *new_commit_id;
9853 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9854 && hle->logmsg == NULL) {
9855 err = histedit_edit_logmsg(hle, repo);
9856 if (err)
9857 return err;
9860 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9861 if (err)
9862 return err;
9864 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9865 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9866 hle->logmsg, repo);
9867 if (err) {
9868 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9869 goto done;
9870 err = show_histedit_progress(commit, hle, NULL);
9871 } else {
9872 err = show_histedit_progress(commit, hle, new_commit_id);
9873 free(new_commit_id);
9875 done:
9876 got_object_commit_close(commit);
9877 return err;
9880 static const struct got_error *
9881 histedit_skip_commit(struct got_histedit_list_entry *hle,
9882 struct got_worktree *worktree, struct got_repository *repo)
9884 const struct got_error *error;
9885 struct got_commit_object *commit;
9887 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9888 repo);
9889 if (error)
9890 return error;
9892 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9893 if (error)
9894 return error;
9896 error = show_histedit_progress(commit, hle, NULL);
9897 got_object_commit_close(commit);
9898 return error;
9901 static const struct got_error *
9902 check_local_changes(void *arg, unsigned char status,
9903 unsigned char staged_status, const char *path,
9904 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9905 struct got_object_id *commit_id, int dirfd, const char *de_name)
9907 int *have_local_changes = arg;
9909 switch (status) {
9910 case GOT_STATUS_ADD:
9911 case GOT_STATUS_DELETE:
9912 case GOT_STATUS_MODIFY:
9913 case GOT_STATUS_CONFLICT:
9914 *have_local_changes = 1;
9915 return got_error(GOT_ERR_CANCELLED);
9916 default:
9917 break;
9920 switch (staged_status) {
9921 case GOT_STATUS_ADD:
9922 case GOT_STATUS_DELETE:
9923 case GOT_STATUS_MODIFY:
9924 *have_local_changes = 1;
9925 return got_error(GOT_ERR_CANCELLED);
9926 default:
9927 break;
9930 return NULL;
9933 static const struct got_error *
9934 cmd_histedit(int argc, char *argv[])
9936 const struct got_error *error = NULL;
9937 struct got_worktree *worktree = NULL;
9938 struct got_fileindex *fileindex = NULL;
9939 struct got_repository *repo = NULL;
9940 char *cwd = NULL;
9941 struct got_reference *branch = NULL;
9942 struct got_reference *tmp_branch = NULL;
9943 struct got_object_id *resume_commit_id = NULL;
9944 struct got_object_id *base_commit_id = NULL;
9945 struct got_object_id *head_commit_id = NULL;
9946 struct got_commit_object *commit = NULL;
9947 int ch, rebase_in_progress = 0, merge_in_progress = 0;
9948 struct got_update_progress_arg upa;
9949 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9950 int edit_logmsg_only = 0, fold_only = 0;
9951 int list_backups = 0, delete_backups = 0;
9952 const char *edit_script_path = NULL;
9953 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9954 struct got_object_id_queue commits;
9955 struct got_pathlist_head merged_paths;
9956 const struct got_object_id_queue *parent_ids;
9957 struct got_object_qid *pid;
9958 struct got_histedit_list histedit_cmds;
9959 struct got_histedit_list_entry *hle;
9961 STAILQ_INIT(&commits);
9962 TAILQ_INIT(&histedit_cmds);
9963 TAILQ_INIT(&merged_paths);
9964 memset(&upa, 0, sizeof(upa));
9966 while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9967 switch (ch) {
9968 case 'a':
9969 abort_edit = 1;
9970 break;
9971 case 'c':
9972 continue_edit = 1;
9973 break;
9974 case 'f':
9975 fold_only = 1;
9976 break;
9977 case 'F':
9978 edit_script_path = optarg;
9979 break;
9980 case 'm':
9981 edit_logmsg_only = 1;
9982 break;
9983 case 'l':
9984 list_backups = 1;
9985 break;
9986 case 'X':
9987 delete_backups = 1;
9988 break;
9989 default:
9990 usage_histedit();
9991 /* NOTREACHED */
9995 argc -= optind;
9996 argv += optind;
9998 #ifndef PROFILE
9999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10000 "unveil", NULL) == -1)
10001 err(1, "pledge");
10002 #endif
10003 if (abort_edit && continue_edit)
10004 option_conflict('a', 'c');
10005 if (edit_script_path && edit_logmsg_only)
10006 option_conflict('F', 'm');
10007 if (abort_edit && edit_logmsg_only)
10008 option_conflict('a', 'm');
10009 if (continue_edit && edit_logmsg_only)
10010 option_conflict('c', 'm');
10011 if (abort_edit && fold_only)
10012 option_conflict('a', 'f');
10013 if (continue_edit && fold_only)
10014 option_conflict('c', 'f');
10015 if (fold_only && edit_logmsg_only)
10016 option_conflict('f', 'm');
10017 if (edit_script_path && fold_only)
10018 option_conflict('F', 'f');
10019 if (list_backups) {
10020 if (abort_edit)
10021 option_conflict('l', 'a');
10022 if (continue_edit)
10023 option_conflict('l', 'c');
10024 if (edit_script_path)
10025 option_conflict('l', 'F');
10026 if (edit_logmsg_only)
10027 option_conflict('l', 'm');
10028 if (fold_only)
10029 option_conflict('l', 'f');
10030 if (delete_backups)
10031 option_conflict('l', 'X');
10032 if (argc != 0 && argc != 1)
10033 usage_histedit();
10034 } else if (delete_backups) {
10035 if (abort_edit)
10036 option_conflict('X', 'a');
10037 if (continue_edit)
10038 option_conflict('X', 'c');
10039 if (edit_script_path)
10040 option_conflict('X', 'F');
10041 if (edit_logmsg_only)
10042 option_conflict('X', 'm');
10043 if (fold_only)
10044 option_conflict('X', 'f');
10045 if (list_backups)
10046 option_conflict('X', 'l');
10047 if (argc != 0 && argc != 1)
10048 usage_histedit();
10049 } else if (argc != 0)
10050 usage_histedit();
10053 * This command cannot apply unveil(2) in all cases because the
10054 * user may choose to run an editor to edit the histedit script
10055 * and to edit individual commit log messages.
10056 * unveil(2) traverses exec(2); if an editor is used we have to
10057 * apply unveil after edit script and log messages have been written.
10058 * XXX TODO: Make use of unveil(2) where possible.
10061 cwd = getcwd(NULL, 0);
10062 if (cwd == NULL) {
10063 error = got_error_from_errno("getcwd");
10064 goto done;
10066 error = got_worktree_open(&worktree, cwd);
10067 if (error) {
10068 if (list_backups || delete_backups) {
10069 if (error->code != GOT_ERR_NOT_WORKTREE)
10070 goto done;
10071 } else {
10072 if (error->code == GOT_ERR_NOT_WORKTREE)
10073 error = wrap_not_worktree_error(error,
10074 "histedit", cwd);
10075 goto done;
10079 if (list_backups || delete_backups) {
10080 error = got_repo_open(&repo,
10081 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10082 NULL);
10083 if (error != NULL)
10084 goto done;
10085 error = apply_unveil(got_repo_get_path(repo), 0,
10086 worktree ? got_worktree_get_root_path(worktree) : NULL);
10087 if (error)
10088 goto done;
10089 error = process_backup_refs(
10090 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10091 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10092 goto done; /* nothing else to do */
10095 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10096 NULL);
10097 if (error != NULL)
10098 goto done;
10100 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10101 if (error)
10102 goto done;
10103 if (rebase_in_progress) {
10104 error = got_error(GOT_ERR_REBASING);
10105 goto done;
10108 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10109 repo);
10110 if (error)
10111 goto done;
10112 if (merge_in_progress) {
10113 error = got_error(GOT_ERR_MERGE_BUSY);
10114 goto done;
10117 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10118 if (error)
10119 goto done;
10121 if (edit_in_progress && edit_logmsg_only) {
10122 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10123 "histedit operation is in progress in this "
10124 "work tree and must be continued or aborted "
10125 "before the -m option can be used");
10126 goto done;
10128 if (edit_in_progress && fold_only) {
10129 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10130 "histedit operation is in progress in this "
10131 "work tree and must be continued or aborted "
10132 "before the -f option can be used");
10133 goto done;
10136 if (edit_in_progress && abort_edit) {
10137 error = got_worktree_histedit_continue(&resume_commit_id,
10138 &tmp_branch, &branch, &base_commit_id, &fileindex,
10139 worktree, repo);
10140 if (error)
10141 goto done;
10142 printf("Switching work tree to %s\n",
10143 got_ref_get_symref_target(branch));
10144 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10145 branch, base_commit_id, update_progress, &upa);
10146 if (error)
10147 goto done;
10148 printf("Histedit of %s aborted\n",
10149 got_ref_get_symref_target(branch));
10150 print_update_progress_stats(&upa);
10151 goto done; /* nothing else to do */
10152 } else if (abort_edit) {
10153 error = got_error(GOT_ERR_NOT_HISTEDIT);
10154 goto done;
10157 if (continue_edit) {
10158 char *path;
10160 if (!edit_in_progress) {
10161 error = got_error(GOT_ERR_NOT_HISTEDIT);
10162 goto done;
10165 error = got_worktree_get_histedit_script_path(&path, worktree);
10166 if (error)
10167 goto done;
10169 error = histedit_load_list(&histedit_cmds, path, repo);
10170 free(path);
10171 if (error)
10172 goto done;
10174 error = got_worktree_histedit_continue(&resume_commit_id,
10175 &tmp_branch, &branch, &base_commit_id, &fileindex,
10176 worktree, repo);
10177 if (error)
10178 goto done;
10180 error = got_ref_resolve(&head_commit_id, repo, branch);
10181 if (error)
10182 goto done;
10184 error = got_object_open_as_commit(&commit, repo,
10185 head_commit_id);
10186 if (error)
10187 goto done;
10188 parent_ids = got_object_commit_get_parent_ids(commit);
10189 pid = STAILQ_FIRST(parent_ids);
10190 if (pid == NULL) {
10191 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10192 goto done;
10194 error = collect_commits(&commits, head_commit_id, pid->id,
10195 base_commit_id, got_worktree_get_path_prefix(worktree),
10196 GOT_ERR_HISTEDIT_PATH, repo);
10197 got_object_commit_close(commit);
10198 commit = NULL;
10199 if (error)
10200 goto done;
10201 } else {
10202 if (edit_in_progress) {
10203 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10204 goto done;
10207 error = got_ref_open(&branch, repo,
10208 got_worktree_get_head_ref_name(worktree), 0);
10209 if (error != NULL)
10210 goto done;
10212 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10213 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10214 "will not edit commit history of a branch outside "
10215 "the \"refs/heads/\" reference namespace");
10216 goto done;
10219 error = got_ref_resolve(&head_commit_id, repo, branch);
10220 got_ref_close(branch);
10221 branch = NULL;
10222 if (error)
10223 goto done;
10225 error = got_object_open_as_commit(&commit, repo,
10226 head_commit_id);
10227 if (error)
10228 goto done;
10229 parent_ids = got_object_commit_get_parent_ids(commit);
10230 pid = STAILQ_FIRST(parent_ids);
10231 if (pid == NULL) {
10232 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10233 goto done;
10235 error = collect_commits(&commits, head_commit_id, pid->id,
10236 got_worktree_get_base_commit_id(worktree),
10237 got_worktree_get_path_prefix(worktree),
10238 GOT_ERR_HISTEDIT_PATH, repo);
10239 got_object_commit_close(commit);
10240 commit = NULL;
10241 if (error)
10242 goto done;
10244 if (STAILQ_EMPTY(&commits)) {
10245 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10246 goto done;
10249 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10250 &base_commit_id, &fileindex, worktree, repo);
10251 if (error)
10252 goto done;
10254 if (edit_script_path) {
10255 error = histedit_load_list(&histedit_cmds,
10256 edit_script_path, repo);
10257 if (error) {
10258 got_worktree_histedit_abort(worktree, fileindex,
10259 repo, branch, base_commit_id,
10260 update_progress, &upa);
10261 print_update_progress_stats(&upa);
10262 goto done;
10264 } else {
10265 const char *branch_name;
10266 branch_name = got_ref_get_symref_target(branch);
10267 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10268 branch_name += 11;
10269 error = histedit_edit_script(&histedit_cmds, &commits,
10270 branch_name, edit_logmsg_only, fold_only, repo);
10271 if (error) {
10272 got_worktree_histedit_abort(worktree, fileindex,
10273 repo, branch, base_commit_id,
10274 update_progress, &upa);
10275 print_update_progress_stats(&upa);
10276 goto done;
10281 error = histedit_save_list(&histedit_cmds, worktree,
10282 repo);
10283 if (error) {
10284 got_worktree_histedit_abort(worktree, fileindex,
10285 repo, branch, base_commit_id,
10286 update_progress, &upa);
10287 print_update_progress_stats(&upa);
10288 goto done;
10293 error = histedit_check_script(&histedit_cmds, &commits, repo);
10294 if (error)
10295 goto done;
10297 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10298 if (resume_commit_id) {
10299 if (got_object_id_cmp(hle->commit_id,
10300 resume_commit_id) != 0)
10301 continue;
10303 resume_commit_id = NULL;
10304 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10305 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10306 error = histedit_skip_commit(hle, worktree,
10307 repo);
10308 if (error)
10309 goto done;
10310 } else {
10311 struct got_pathlist_head paths;
10312 int have_changes = 0;
10314 TAILQ_INIT(&paths);
10315 error = got_pathlist_append(&paths, "", NULL);
10316 if (error)
10317 goto done;
10318 error = got_worktree_status(worktree, &paths,
10319 repo, 0, check_local_changes, &have_changes,
10320 check_cancelled, NULL);
10321 got_pathlist_free(&paths);
10322 if (error) {
10323 if (error->code != GOT_ERR_CANCELLED)
10324 goto done;
10325 if (sigint_received || sigpipe_received)
10326 goto done;
10328 if (have_changes) {
10329 error = histedit_commit(NULL, worktree,
10330 fileindex, tmp_branch, hle, repo);
10331 if (error)
10332 goto done;
10333 } else {
10334 error = got_object_open_as_commit(
10335 &commit, repo, hle->commit_id);
10336 if (error)
10337 goto done;
10338 error = show_histedit_progress(commit,
10339 hle, NULL);
10340 got_object_commit_close(commit);
10341 commit = NULL;
10342 if (error)
10343 goto done;
10346 continue;
10349 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10350 error = histedit_skip_commit(hle, worktree, repo);
10351 if (error)
10352 goto done;
10353 continue;
10356 error = got_object_open_as_commit(&commit, repo,
10357 hle->commit_id);
10358 if (error)
10359 goto done;
10360 parent_ids = got_object_commit_get_parent_ids(commit);
10361 pid = STAILQ_FIRST(parent_ids);
10363 error = got_worktree_histedit_merge_files(&merged_paths,
10364 worktree, fileindex, pid->id, hle->commit_id, repo,
10365 update_progress, &upa, check_cancelled, NULL);
10366 if (error)
10367 goto done;
10368 got_object_commit_close(commit);
10369 commit = NULL;
10371 print_update_progress_stats(&upa);
10372 if (upa.conflicts > 0)
10373 rebase_status = GOT_STATUS_CONFLICT;
10375 if (rebase_status == GOT_STATUS_CONFLICT) {
10376 error = show_rebase_merge_conflict(hle->commit_id,
10377 repo);
10378 if (error)
10379 goto done;
10380 got_worktree_rebase_pathlist_free(&merged_paths);
10381 break;
10384 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10385 char *id_str;
10386 error = got_object_id_str(&id_str, hle->commit_id);
10387 if (error)
10388 goto done;
10389 printf("Stopping histedit for amending commit %s\n",
10390 id_str);
10391 free(id_str);
10392 got_worktree_rebase_pathlist_free(&merged_paths);
10393 error = got_worktree_histedit_postpone(worktree,
10394 fileindex);
10395 goto done;
10398 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10399 error = histedit_skip_commit(hle, worktree, repo);
10400 if (error)
10401 goto done;
10402 continue;
10405 error = histedit_commit(&merged_paths, worktree, fileindex,
10406 tmp_branch, hle, repo);
10407 got_worktree_rebase_pathlist_free(&merged_paths);
10408 if (error)
10409 goto done;
10412 if (rebase_status == GOT_STATUS_CONFLICT) {
10413 error = got_worktree_histedit_postpone(worktree, fileindex);
10414 if (error)
10415 goto done;
10416 error = got_error_msg(GOT_ERR_CONFLICTS,
10417 "conflicts must be resolved before histedit can continue");
10418 } else
10419 error = histedit_complete(worktree, fileindex, tmp_branch,
10420 branch, repo);
10421 done:
10422 got_object_id_queue_free(&commits);
10423 histedit_free_list(&histedit_cmds);
10424 free(head_commit_id);
10425 free(base_commit_id);
10426 free(resume_commit_id);
10427 if (commit)
10428 got_object_commit_close(commit);
10429 if (branch)
10430 got_ref_close(branch);
10431 if (tmp_branch)
10432 got_ref_close(tmp_branch);
10433 if (worktree)
10434 got_worktree_close(worktree);
10435 if (repo) {
10436 const struct got_error *close_err = got_repo_close(repo);
10437 if (error == NULL)
10438 error = close_err;
10440 return error;
10443 __dead static void
10444 usage_integrate(void)
10446 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10447 exit(1);
10450 static const struct got_error *
10451 cmd_integrate(int argc, char *argv[])
10453 const struct got_error *error = NULL;
10454 struct got_repository *repo = NULL;
10455 struct got_worktree *worktree = NULL;
10456 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10457 const char *branch_arg = NULL;
10458 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10459 struct got_fileindex *fileindex = NULL;
10460 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10461 int ch;
10462 struct got_update_progress_arg upa;
10464 while ((ch = getopt(argc, argv, "")) != -1) {
10465 switch (ch) {
10466 default:
10467 usage_integrate();
10468 /* NOTREACHED */
10472 argc -= optind;
10473 argv += optind;
10475 if (argc != 1)
10476 usage_integrate();
10477 branch_arg = argv[0];
10478 #ifndef PROFILE
10479 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10480 "unveil", NULL) == -1)
10481 err(1, "pledge");
10482 #endif
10483 cwd = getcwd(NULL, 0);
10484 if (cwd == NULL) {
10485 error = got_error_from_errno("getcwd");
10486 goto done;
10489 error = got_worktree_open(&worktree, cwd);
10490 if (error) {
10491 if (error->code == GOT_ERR_NOT_WORKTREE)
10492 error = wrap_not_worktree_error(error, "integrate",
10493 cwd);
10494 goto done;
10497 error = check_rebase_or_histedit_in_progress(worktree);
10498 if (error)
10499 goto done;
10501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10502 NULL);
10503 if (error != NULL)
10504 goto done;
10506 error = apply_unveil(got_repo_get_path(repo), 0,
10507 got_worktree_get_root_path(worktree));
10508 if (error)
10509 goto done;
10511 error = check_merge_in_progress(worktree, repo);
10512 if (error)
10513 goto done;
10515 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10516 error = got_error_from_errno("asprintf");
10517 goto done;
10520 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10521 &base_branch_ref, worktree, refname, repo);
10522 if (error)
10523 goto done;
10525 refname = strdup(got_ref_get_name(branch_ref));
10526 if (refname == NULL) {
10527 error = got_error_from_errno("strdup");
10528 got_worktree_integrate_abort(worktree, fileindex, repo,
10529 branch_ref, base_branch_ref);
10530 goto done;
10532 base_refname = strdup(got_ref_get_name(base_branch_ref));
10533 if (base_refname == NULL) {
10534 error = got_error_from_errno("strdup");
10535 got_worktree_integrate_abort(worktree, fileindex, repo,
10536 branch_ref, base_branch_ref);
10537 goto done;
10540 error = got_ref_resolve(&commit_id, repo, branch_ref);
10541 if (error)
10542 goto done;
10544 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10545 if (error)
10546 goto done;
10548 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10549 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10550 "specified branch has already been integrated");
10551 got_worktree_integrate_abort(worktree, fileindex, repo,
10552 branch_ref, base_branch_ref);
10553 goto done;
10556 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10557 if (error) {
10558 if (error->code == GOT_ERR_ANCESTRY)
10559 error = got_error(GOT_ERR_REBASE_REQUIRED);
10560 got_worktree_integrate_abort(worktree, fileindex, repo,
10561 branch_ref, base_branch_ref);
10562 goto done;
10565 memset(&upa, 0, sizeof(upa));
10566 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10567 branch_ref, base_branch_ref, update_progress, &upa,
10568 check_cancelled, NULL);
10569 if (error)
10570 goto done;
10572 printf("Integrated %s into %s\n", refname, base_refname);
10573 print_update_progress_stats(&upa);
10574 done:
10575 if (repo) {
10576 const struct got_error *close_err = got_repo_close(repo);
10577 if (error == NULL)
10578 error = close_err;
10580 if (worktree)
10581 got_worktree_close(worktree);
10582 free(cwd);
10583 free(base_commit_id);
10584 free(commit_id);
10585 free(refname);
10586 free(base_refname);
10587 return error;
10590 __dead static void
10591 usage_merge(void)
10593 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10594 getprogname());
10595 exit(1);
10598 static const struct got_error *
10599 cmd_merge(int argc, char *argv[])
10601 const struct got_error *error = NULL;
10602 struct got_worktree *worktree = NULL;
10603 struct got_repository *repo = NULL;
10604 struct got_fileindex *fileindex = NULL;
10605 char *cwd = NULL, *id_str = NULL, *author = NULL;
10606 struct got_reference *branch = NULL, *wt_branch = NULL;
10607 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
10608 struct got_object_id *wt_branch_tip = NULL;
10609 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
10610 int interrupt_merge = 0;
10611 struct got_update_progress_arg upa;
10612 struct got_object_id *merge_commit_id = NULL;
10613 char *branch_name = NULL;
10615 memset(&upa, 0, sizeof(upa));
10617 while ((ch = getopt(argc, argv, "acn")) != -1) {
10618 switch (ch) {
10619 case 'a':
10620 abort_merge = 1;
10621 break;
10622 case 'c':
10623 continue_merge = 1;
10624 break;
10625 case 'n':
10626 interrupt_merge = 1;
10627 break;
10628 default:
10629 usage_rebase();
10630 /* NOTREACHED */
10634 argc -= optind;
10635 argv += optind;
10637 #ifndef PROFILE
10638 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10639 "unveil", NULL) == -1)
10640 err(1, "pledge");
10641 #endif
10643 if (abort_merge && continue_merge)
10644 option_conflict('a', 'c');
10645 if (abort_merge || continue_merge) {
10646 if (argc != 0)
10647 usage_merge();
10648 } else if (argc != 1)
10649 usage_merge();
10651 cwd = getcwd(NULL, 0);
10652 if (cwd == NULL) {
10653 error = got_error_from_errno("getcwd");
10654 goto done;
10657 error = got_worktree_open(&worktree, cwd);
10658 if (error) {
10659 if (error->code == GOT_ERR_NOT_WORKTREE)
10660 error = wrap_not_worktree_error(error,
10661 "merge", cwd);
10662 goto done;
10665 error = got_repo_open(&repo,
10666 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
10667 if (error != NULL)
10668 goto done;
10670 error = apply_unveil(got_repo_get_path(repo), 0,
10671 worktree ? got_worktree_get_root_path(worktree) : NULL);
10672 if (error)
10673 goto done;
10675 error = check_rebase_or_histedit_in_progress(worktree);
10676 if (error)
10677 goto done;
10679 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10680 repo);
10681 if (error)
10682 goto done;
10684 if (abort_merge) {
10685 if (!merge_in_progress) {
10686 error = got_error(GOT_ERR_NOT_MERGING);
10687 goto done;
10689 error = got_worktree_merge_continue(&branch_name,
10690 &branch_tip, &fileindex, worktree, repo);
10691 if (error)
10692 goto done;
10693 error = got_worktree_merge_abort(worktree, fileindex, repo,
10694 update_progress, &upa);
10695 if (error)
10696 goto done;
10697 printf("Merge of %s aborted\n", branch_name);
10698 goto done; /* nothing else to do */
10701 error = get_author(&author, repo, worktree);
10702 if (error)
10703 goto done;
10705 if (continue_merge) {
10706 if (!merge_in_progress) {
10707 error = got_error(GOT_ERR_NOT_MERGING);
10708 goto done;
10710 error = got_worktree_merge_continue(&branch_name,
10711 &branch_tip, &fileindex, worktree, repo);
10712 if (error)
10713 goto done;
10714 } else {
10715 error = got_ref_open(&branch, repo, argv[0], 0);
10716 if (error != NULL)
10717 goto done;
10718 branch_name = strdup(got_ref_get_name(branch));
10719 if (branch_name == NULL) {
10720 error = got_error_from_errno("strdup");
10721 goto done;
10723 error = got_ref_resolve(&branch_tip, repo, branch);
10724 if (error)
10725 goto done;
10728 error = got_ref_open(&wt_branch, repo,
10729 got_worktree_get_head_ref_name(worktree), 0);
10730 if (error)
10731 goto done;
10732 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
10733 if (error)
10734 goto done;
10735 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10736 wt_branch_tip, branch_tip, 0, repo,
10737 check_cancelled, NULL);
10738 if (error && error->code != GOT_ERR_ANCESTRY)
10739 goto done;
10741 if (!continue_merge) {
10742 error = check_path_prefix(wt_branch_tip, branch_tip,
10743 got_worktree_get_path_prefix(worktree),
10744 GOT_ERR_MERGE_PATH, repo);
10745 if (error)
10746 goto done;
10747 if (yca_id) {
10748 error = check_same_branch(wt_branch_tip, branch,
10749 yca_id, repo);
10750 if (error) {
10751 if (error->code != GOT_ERR_ANCESTRY)
10752 goto done;
10753 error = NULL;
10754 } else {
10755 static char msg[512];
10756 snprintf(msg, sizeof(msg),
10757 "cannot create a merge commit because "
10758 "%s is based on %s; %s can be integrated "
10759 "with 'got integrate' instead", branch_name,
10760 got_worktree_get_head_ref_name(worktree),
10761 branch_name);
10762 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
10763 goto done;
10766 error = got_worktree_merge_prepare(&fileindex, worktree,
10767 branch, repo);
10768 if (error)
10769 goto done;
10771 error = got_worktree_merge_branch(worktree, fileindex,
10772 yca_id, branch_tip, repo, update_progress, &upa,
10773 check_cancelled, NULL);
10774 if (error)
10775 goto done;
10776 print_update_progress_stats(&upa);
10777 if (!upa.did_something) {
10778 error = got_worktree_merge_abort(worktree, fileindex,
10779 repo, update_progress, &upa);
10780 if (error)
10781 goto done;
10782 printf("Already up-to-date\n");
10783 goto done;
10787 if (interrupt_merge) {
10788 error = got_worktree_merge_postpone(worktree, fileindex);
10789 if (error)
10790 goto done;
10791 printf("Merge of %s interrupted on request\n", branch_name);
10792 } else if (upa.conflicts > 0 || upa.missing > 0) {
10793 error = got_worktree_merge_postpone(worktree, fileindex);
10794 if (error)
10795 goto done;
10796 if (upa.conflicts > 0 && upa.missing == 0) {
10797 error = got_error_msg(GOT_ERR_CONFLICTS,
10798 "conflicts must be resolved before merging "
10799 "can continue");
10800 } else if (upa.conflicts > 0) {
10801 error = got_error_msg(GOT_ERR_CONFLICTS,
10802 "conflicts must be resolved before merging "
10803 "can continue; changes destined for missing "
10804 "files were not yet merged and "
10805 "should be merged manually if required before the "
10806 "merge operation is continued");
10807 } else {
10808 error = got_error_msg(GOT_ERR_CONFLICTS,
10809 "changes destined for missing "
10810 "files were not yet merged and should be "
10811 "merged manually if required before the "
10812 "merge operation is continued");
10814 goto done;
10815 } else {
10816 error = got_worktree_merge_commit(&merge_commit_id, worktree,
10817 fileindex, author, NULL, 1, branch_tip, branch_name, repo);
10818 if (error)
10819 goto done;
10820 error = got_worktree_merge_complete(worktree, fileindex, repo);
10821 if (error)
10822 goto done;
10823 error = got_object_id_str(&id_str, merge_commit_id);
10824 if (error)
10825 goto done;
10826 printf("Merged %s into %s: %s\n", branch_name,
10827 got_worktree_get_head_ref_name(worktree),
10828 id_str);
10831 done:
10832 free(id_str);
10833 free(merge_commit_id);
10834 free(author);
10835 free(branch_tip);
10836 free(branch_name);
10837 free(yca_id);
10838 if (branch)
10839 got_ref_close(branch);
10840 if (wt_branch)
10841 got_ref_close(wt_branch);
10842 if (worktree)
10843 got_worktree_close(worktree);
10844 if (repo) {
10845 const struct got_error *close_err = got_repo_close(repo);
10846 if (error == NULL)
10847 error = close_err;
10849 return error;
10852 __dead static void
10853 usage_stage(void)
10855 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10856 "[-S] [file-path ...]\n",
10857 getprogname());
10858 exit(1);
10861 static const struct got_error *
10862 print_stage(void *arg, unsigned char status, unsigned char staged_status,
10863 const char *path, struct got_object_id *blob_id,
10864 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10865 int dirfd, const char *de_name)
10867 const struct got_error *err = NULL;
10868 char *id_str = NULL;
10870 if (staged_status != GOT_STATUS_ADD &&
10871 staged_status != GOT_STATUS_MODIFY &&
10872 staged_status != GOT_STATUS_DELETE)
10873 return NULL;
10875 if (staged_status == GOT_STATUS_ADD ||
10876 staged_status == GOT_STATUS_MODIFY)
10877 err = got_object_id_str(&id_str, staged_blob_id);
10878 else
10879 err = got_object_id_str(&id_str, blob_id);
10880 if (err)
10881 return err;
10883 printf("%s %c %s\n", id_str, staged_status, path);
10884 free(id_str);
10885 return NULL;
10888 static const struct got_error *
10889 cmd_stage(int argc, char *argv[])
10891 const struct got_error *error = NULL;
10892 struct got_repository *repo = NULL;
10893 struct got_worktree *worktree = NULL;
10894 char *cwd = NULL;
10895 struct got_pathlist_head paths;
10896 struct got_pathlist_entry *pe;
10897 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10898 FILE *patch_script_file = NULL;
10899 const char *patch_script_path = NULL;
10900 struct choose_patch_arg cpa;
10902 TAILQ_INIT(&paths);
10904 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10905 switch (ch) {
10906 case 'l':
10907 list_stage = 1;
10908 break;
10909 case 'p':
10910 pflag = 1;
10911 break;
10912 case 'F':
10913 patch_script_path = optarg;
10914 break;
10915 case 'S':
10916 allow_bad_symlinks = 1;
10917 break;
10918 default:
10919 usage_stage();
10920 /* NOTREACHED */
10924 argc -= optind;
10925 argv += optind;
10927 #ifndef PROFILE
10928 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10929 "unveil", NULL) == -1)
10930 err(1, "pledge");
10931 #endif
10932 if (list_stage && (pflag || patch_script_path))
10933 errx(1, "-l option cannot be used with other options");
10934 if (patch_script_path && !pflag)
10935 errx(1, "-F option can only be used together with -p option");
10937 cwd = getcwd(NULL, 0);
10938 if (cwd == NULL) {
10939 error = got_error_from_errno("getcwd");
10940 goto done;
10943 error = got_worktree_open(&worktree, cwd);
10944 if (error) {
10945 if (error->code == GOT_ERR_NOT_WORKTREE)
10946 error = wrap_not_worktree_error(error, "stage", cwd);
10947 goto done;
10950 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10951 NULL);
10952 if (error != NULL)
10953 goto done;
10955 if (patch_script_path) {
10956 patch_script_file = fopen(patch_script_path, "r");
10957 if (patch_script_file == NULL) {
10958 error = got_error_from_errno2("fopen",
10959 patch_script_path);
10960 goto done;
10963 error = apply_unveil(got_repo_get_path(repo), 0,
10964 got_worktree_get_root_path(worktree));
10965 if (error)
10966 goto done;
10968 error = check_merge_in_progress(worktree, repo);
10969 if (error)
10970 goto done;
10972 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10973 if (error)
10974 goto done;
10976 if (list_stage)
10977 error = got_worktree_status(worktree, &paths, repo, 0,
10978 print_stage, NULL, check_cancelled, NULL);
10979 else {
10980 cpa.patch_script_file = patch_script_file;
10981 cpa.action = "stage";
10982 error = got_worktree_stage(worktree, &paths,
10983 pflag ? NULL : print_status, NULL,
10984 pflag ? choose_patch : NULL, &cpa,
10985 allow_bad_symlinks, repo);
10987 done:
10988 if (patch_script_file && fclose(patch_script_file) == EOF &&
10989 error == NULL)
10990 error = got_error_from_errno2("fclose", patch_script_path);
10991 if (repo) {
10992 const struct got_error *close_err = got_repo_close(repo);
10993 if (error == NULL)
10994 error = close_err;
10996 if (worktree)
10997 got_worktree_close(worktree);
10998 TAILQ_FOREACH(pe, &paths, entry)
10999 free((char *)pe->path);
11000 got_pathlist_free(&paths);
11001 free(cwd);
11002 return error;
11005 __dead static void
11006 usage_unstage(void)
11008 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11009 "[file-path ...]\n",
11010 getprogname());
11011 exit(1);
11015 static const struct got_error *
11016 cmd_unstage(int argc, char *argv[])
11018 const struct got_error *error = NULL;
11019 struct got_repository *repo = NULL;
11020 struct got_worktree *worktree = NULL;
11021 char *cwd = NULL;
11022 struct got_pathlist_head paths;
11023 struct got_pathlist_entry *pe;
11024 int ch, pflag = 0;
11025 struct got_update_progress_arg upa;
11026 FILE *patch_script_file = NULL;
11027 const char *patch_script_path = NULL;
11028 struct choose_patch_arg cpa;
11030 TAILQ_INIT(&paths);
11032 while ((ch = getopt(argc, argv, "pF:")) != -1) {
11033 switch (ch) {
11034 case 'p':
11035 pflag = 1;
11036 break;
11037 case 'F':
11038 patch_script_path = optarg;
11039 break;
11040 default:
11041 usage_unstage();
11042 /* NOTREACHED */
11046 argc -= optind;
11047 argv += optind;
11049 #ifndef PROFILE
11050 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11051 "unveil", NULL) == -1)
11052 err(1, "pledge");
11053 #endif
11054 if (patch_script_path && !pflag)
11055 errx(1, "-F option can only be used together with -p option");
11057 cwd = getcwd(NULL, 0);
11058 if (cwd == NULL) {
11059 error = got_error_from_errno("getcwd");
11060 goto done;
11063 error = got_worktree_open(&worktree, cwd);
11064 if (error) {
11065 if (error->code == GOT_ERR_NOT_WORKTREE)
11066 error = wrap_not_worktree_error(error, "unstage", cwd);
11067 goto done;
11070 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11071 NULL);
11072 if (error != NULL)
11073 goto done;
11075 if (patch_script_path) {
11076 patch_script_file = fopen(patch_script_path, "r");
11077 if (patch_script_file == NULL) {
11078 error = got_error_from_errno2("fopen",
11079 patch_script_path);
11080 goto done;
11084 error = apply_unveil(got_repo_get_path(repo), 0,
11085 got_worktree_get_root_path(worktree));
11086 if (error)
11087 goto done;
11089 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11090 if (error)
11091 goto done;
11093 cpa.patch_script_file = patch_script_file;
11094 cpa.action = "unstage";
11095 memset(&upa, 0, sizeof(upa));
11096 error = got_worktree_unstage(worktree, &paths, update_progress,
11097 &upa, pflag ? choose_patch : NULL, &cpa, repo);
11098 if (!error)
11099 print_update_progress_stats(&upa);
11100 done:
11101 if (patch_script_file && fclose(patch_script_file) == EOF &&
11102 error == NULL)
11103 error = got_error_from_errno2("fclose", patch_script_path);
11104 if (repo) {
11105 const struct got_error *close_err = got_repo_close(repo);
11106 if (error == NULL)
11107 error = close_err;
11109 if (worktree)
11110 got_worktree_close(worktree);
11111 TAILQ_FOREACH(pe, &paths, entry)
11112 free((char *)pe->path);
11113 got_pathlist_free(&paths);
11114 free(cwd);
11115 return error;
11118 __dead static void
11119 usage_cat(void)
11121 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11122 "arg1 [arg2 ...]\n", getprogname());
11123 exit(1);
11126 static const struct got_error *
11127 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11129 const struct got_error *err;
11130 struct got_blob_object *blob;
11132 err = got_object_open_as_blob(&blob, repo, id, 8192);
11133 if (err)
11134 return err;
11136 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11137 got_object_blob_close(blob);
11138 return err;
11141 static const struct got_error *
11142 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11144 const struct got_error *err;
11145 struct got_tree_object *tree;
11146 int nentries, i;
11148 err = got_object_open_as_tree(&tree, repo, id);
11149 if (err)
11150 return err;
11152 nentries = got_object_tree_get_nentries(tree);
11153 for (i = 0; i < nentries; i++) {
11154 struct got_tree_entry *te;
11155 char *id_str;
11156 if (sigint_received || sigpipe_received)
11157 break;
11158 te = got_object_tree_get_entry(tree, i);
11159 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11160 if (err)
11161 break;
11162 fprintf(outfile, "%s %.7o %s\n", id_str,
11163 got_tree_entry_get_mode(te),
11164 got_tree_entry_get_name(te));
11165 free(id_str);
11168 got_object_tree_close(tree);
11169 return err;
11172 static const struct got_error *
11173 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11175 const struct got_error *err;
11176 struct got_commit_object *commit;
11177 const struct got_object_id_queue *parent_ids;
11178 struct got_object_qid *pid;
11179 char *id_str = NULL;
11180 const char *logmsg = NULL;
11182 err = got_object_open_as_commit(&commit, repo, id);
11183 if (err)
11184 return err;
11186 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11187 if (err)
11188 goto done;
11190 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11191 parent_ids = got_object_commit_get_parent_ids(commit);
11192 fprintf(outfile, "numparents %d\n",
11193 got_object_commit_get_nparents(commit));
11194 STAILQ_FOREACH(pid, parent_ids, entry) {
11195 char *pid_str;
11196 err = got_object_id_str(&pid_str, pid->id);
11197 if (err)
11198 goto done;
11199 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11200 free(pid_str);
11202 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
11203 got_object_commit_get_author(commit),
11204 (long long)got_object_commit_get_author_time(commit));
11206 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
11207 got_object_commit_get_author(commit),
11208 (long long)got_object_commit_get_committer_time(commit));
11210 logmsg = got_object_commit_get_logmsg_raw(commit);
11211 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11212 fprintf(outfile, "%s", logmsg);
11213 done:
11214 free(id_str);
11215 got_object_commit_close(commit);
11216 return err;
11219 static const struct got_error *
11220 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11222 const struct got_error *err;
11223 struct got_tag_object *tag;
11224 char *id_str = NULL;
11225 const char *tagmsg = NULL;
11227 err = got_object_open_as_tag(&tag, repo, id);
11228 if (err)
11229 return err;
11231 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11232 if (err)
11233 goto done;
11235 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11237 switch (got_object_tag_get_object_type(tag)) {
11238 case GOT_OBJ_TYPE_BLOB:
11239 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11240 GOT_OBJ_LABEL_BLOB);
11241 break;
11242 case GOT_OBJ_TYPE_TREE:
11243 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11244 GOT_OBJ_LABEL_TREE);
11245 break;
11246 case GOT_OBJ_TYPE_COMMIT:
11247 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11248 GOT_OBJ_LABEL_COMMIT);
11249 break;
11250 case GOT_OBJ_TYPE_TAG:
11251 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11252 GOT_OBJ_LABEL_TAG);
11253 break;
11254 default:
11255 break;
11258 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11259 got_object_tag_get_name(tag));
11261 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
11262 got_object_tag_get_tagger(tag),
11263 (long long)got_object_tag_get_tagger_time(tag));
11265 tagmsg = got_object_tag_get_message(tag);
11266 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11267 fprintf(outfile, "%s", tagmsg);
11268 done:
11269 free(id_str);
11270 got_object_tag_close(tag);
11271 return err;
11274 static const struct got_error *
11275 cmd_cat(int argc, char *argv[])
11277 const struct got_error *error;
11278 struct got_repository *repo = NULL;
11279 struct got_worktree *worktree = NULL;
11280 char *cwd = NULL, *repo_path = NULL, *label = NULL;
11281 const char *commit_id_str = NULL;
11282 struct got_object_id *id = NULL, *commit_id = NULL;
11283 int ch, obj_type, i, force_path = 0;
11284 struct got_reflist_head refs;
11286 TAILQ_INIT(&refs);
11288 #ifndef PROFILE
11289 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11290 NULL) == -1)
11291 err(1, "pledge");
11292 #endif
11294 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11295 switch (ch) {
11296 case 'c':
11297 commit_id_str = optarg;
11298 break;
11299 case 'r':
11300 repo_path = realpath(optarg, NULL);
11301 if (repo_path == NULL)
11302 return got_error_from_errno2("realpath",
11303 optarg);
11304 got_path_strip_trailing_slashes(repo_path);
11305 break;
11306 case 'P':
11307 force_path = 1;
11308 break;
11309 default:
11310 usage_cat();
11311 /* NOTREACHED */
11315 argc -= optind;
11316 argv += optind;
11318 cwd = getcwd(NULL, 0);
11319 if (cwd == NULL) {
11320 error = got_error_from_errno("getcwd");
11321 goto done;
11323 error = got_worktree_open(&worktree, cwd);
11324 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11325 goto done;
11326 if (worktree) {
11327 if (repo_path == NULL) {
11328 repo_path = strdup(
11329 got_worktree_get_repo_path(worktree));
11330 if (repo_path == NULL) {
11331 error = got_error_from_errno("strdup");
11332 goto done;
11337 if (repo_path == NULL) {
11338 repo_path = getcwd(NULL, 0);
11339 if (repo_path == NULL)
11340 return got_error_from_errno("getcwd");
11343 error = got_repo_open(&repo, repo_path, NULL);
11344 free(repo_path);
11345 if (error != NULL)
11346 goto done;
11348 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11349 if (error)
11350 goto done;
11352 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11353 if (error)
11354 goto done;
11356 if (commit_id_str == NULL)
11357 commit_id_str = GOT_REF_HEAD;
11358 error = got_repo_match_object_id(&commit_id, NULL,
11359 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11360 if (error)
11361 goto done;
11363 for (i = 0; i < argc; i++) {
11364 if (force_path) {
11365 error = got_object_id_by_path(&id, repo, commit_id,
11366 argv[i]);
11367 if (error)
11368 break;
11369 } else {
11370 error = got_repo_match_object_id(&id, &label, argv[i],
11371 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11372 repo);
11373 if (error) {
11374 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11375 error->code != GOT_ERR_NOT_REF)
11376 break;
11377 error = got_object_id_by_path(&id, repo,
11378 commit_id, argv[i]);
11379 if (error)
11380 break;
11384 error = got_object_get_type(&obj_type, repo, id);
11385 if (error)
11386 break;
11388 switch (obj_type) {
11389 case GOT_OBJ_TYPE_BLOB:
11390 error = cat_blob(id, repo, stdout);
11391 break;
11392 case GOT_OBJ_TYPE_TREE:
11393 error = cat_tree(id, repo, stdout);
11394 break;
11395 case GOT_OBJ_TYPE_COMMIT:
11396 error = cat_commit(id, repo, stdout);
11397 break;
11398 case GOT_OBJ_TYPE_TAG:
11399 error = cat_tag(id, repo, stdout);
11400 break;
11401 default:
11402 error = got_error(GOT_ERR_OBJ_TYPE);
11403 break;
11405 if (error)
11406 break;
11407 free(label);
11408 label = NULL;
11409 free(id);
11410 id = NULL;
11412 done:
11413 free(label);
11414 free(id);
11415 free(commit_id);
11416 if (worktree)
11417 got_worktree_close(worktree);
11418 if (repo) {
11419 const struct got_error *close_err = got_repo_close(repo);
11420 if (error == NULL)
11421 error = close_err;
11423 got_ref_list_free(&refs);
11424 return error;
11427 __dead static void
11428 usage_info(void)
11430 fprintf(stderr, "usage: %s info [path ...]\n",
11431 getprogname());
11432 exit(1);
11435 static const struct got_error *
11436 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11437 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11438 struct got_object_id *commit_id)
11440 const struct got_error *err = NULL;
11441 char *id_str = NULL;
11442 char datebuf[128];
11443 struct tm mytm, *tm;
11444 struct got_pathlist_head *paths = arg;
11445 struct got_pathlist_entry *pe;
11448 * Clear error indication from any of the path arguments which
11449 * would cause this file index entry to be displayed.
11451 TAILQ_FOREACH(pe, paths, entry) {
11452 if (got_path_cmp(path, pe->path, strlen(path),
11453 pe->path_len) == 0 ||
11454 got_path_is_child(path, pe->path, pe->path_len))
11455 pe->data = NULL; /* no error */
11458 printf(GOT_COMMIT_SEP_STR);
11459 if (S_ISLNK(mode))
11460 printf("symlink: %s\n", path);
11461 else if (S_ISREG(mode)) {
11462 printf("file: %s\n", path);
11463 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11464 } else if (S_ISDIR(mode))
11465 printf("directory: %s\n", path);
11466 else
11467 printf("something: %s\n", path);
11469 tm = localtime_r(&mtime, &mytm);
11470 if (tm == NULL)
11471 return NULL;
11472 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11473 return got_error(GOT_ERR_NO_SPACE);
11474 printf("timestamp: %s\n", datebuf);
11476 if (blob_id) {
11477 err = got_object_id_str(&id_str, blob_id);
11478 if (err)
11479 return err;
11480 printf("based on blob: %s\n", id_str);
11481 free(id_str);
11484 if (staged_blob_id) {
11485 err = got_object_id_str(&id_str, staged_blob_id);
11486 if (err)
11487 return err;
11488 printf("based on staged blob: %s\n", id_str);
11489 free(id_str);
11492 if (commit_id) {
11493 err = got_object_id_str(&id_str, commit_id);
11494 if (err)
11495 return err;
11496 printf("based on commit: %s\n", id_str);
11497 free(id_str);
11500 return NULL;
11503 static const struct got_error *
11504 cmd_info(int argc, char *argv[])
11506 const struct got_error *error = NULL;
11507 struct got_worktree *worktree = NULL;
11508 char *cwd = NULL, *id_str = NULL;
11509 struct got_pathlist_head paths;
11510 struct got_pathlist_entry *pe;
11511 char *uuidstr = NULL;
11512 int ch, show_files = 0;
11514 TAILQ_INIT(&paths);
11516 while ((ch = getopt(argc, argv, "")) != -1) {
11517 switch (ch) {
11518 default:
11519 usage_info();
11520 /* NOTREACHED */
11524 argc -= optind;
11525 argv += optind;
11527 #ifndef PROFILE
11528 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11529 NULL) == -1)
11530 err(1, "pledge");
11531 #endif
11532 cwd = getcwd(NULL, 0);
11533 if (cwd == NULL) {
11534 error = got_error_from_errno("getcwd");
11535 goto done;
11538 error = got_worktree_open(&worktree, cwd);
11539 if (error) {
11540 if (error->code == GOT_ERR_NOT_WORKTREE)
11541 error = wrap_not_worktree_error(error, "info", cwd);
11542 goto done;
11545 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11546 if (error)
11547 goto done;
11549 if (argc >= 1) {
11550 error = get_worktree_paths_from_argv(&paths, argc, argv,
11551 worktree);
11552 if (error)
11553 goto done;
11554 show_files = 1;
11557 error = got_object_id_str(&id_str,
11558 got_worktree_get_base_commit_id(worktree));
11559 if (error)
11560 goto done;
11562 error = got_worktree_get_uuid(&uuidstr, worktree);
11563 if (error)
11564 goto done;
11566 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11567 printf("work tree base commit: %s\n", id_str);
11568 printf("work tree path prefix: %s\n",
11569 got_worktree_get_path_prefix(worktree));
11570 printf("work tree branch reference: %s\n",
11571 got_worktree_get_head_ref_name(worktree));
11572 printf("work tree UUID: %s\n", uuidstr);
11573 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11575 if (show_files) {
11576 struct got_pathlist_entry *pe;
11577 TAILQ_FOREACH(pe, &paths, entry) {
11578 if (pe->path_len == 0)
11579 continue;
11581 * Assume this path will fail. This will be corrected
11582 * in print_path_info() in case the path does suceeed.
11584 pe->data = (void *)got_error_path(pe->path,
11585 GOT_ERR_BAD_PATH);
11587 error = got_worktree_path_info(worktree, &paths,
11588 print_path_info, &paths, check_cancelled, NULL);
11589 if (error)
11590 goto done;
11591 TAILQ_FOREACH(pe, &paths, entry) {
11592 if (pe->data != NULL) {
11593 error = pe->data; /* bad path */
11594 break;
11598 done:
11599 TAILQ_FOREACH(pe, &paths, entry)
11600 free((char *)pe->path);
11601 got_pathlist_free(&paths);
11602 free(cwd);
11603 free(id_str);
11604 free(uuidstr);
11605 return error;