Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
112 __dead static void usage_info(void);
114 static const struct got_error* cmd_init(int, char *[]);
115 static const struct got_error* cmd_import(int, char *[]);
116 static const struct got_error* cmd_clone(int, char *[]);
117 static const struct got_error* cmd_fetch(int, char *[]);
118 static const struct got_error* cmd_checkout(int, char *[]);
119 static const struct got_error* cmd_update(int, char *[]);
120 static const struct got_error* cmd_log(int, char *[]);
121 static const struct got_error* cmd_diff(int, char *[]);
122 static const struct got_error* cmd_blame(int, char *[]);
123 static const struct got_error* cmd_tree(int, char *[]);
124 static const struct got_error* cmd_status(int, char *[]);
125 static const struct got_error* cmd_ref(int, char *[]);
126 static const struct got_error* cmd_branch(int, char *[]);
127 static const struct got_error* cmd_tag(int, char *[]);
128 static const struct got_error* cmd_add(int, char *[]);
129 static const struct got_error* cmd_remove(int, char *[]);
130 static const struct got_error* cmd_revert(int, char *[]);
131 static const struct got_error* cmd_commit(int, char *[]);
132 static const struct got_error* cmd_cherrypick(int, char *[]);
133 static const struct got_error* cmd_backout(int, char *[]);
134 static const struct got_error* cmd_rebase(int, char *[]);
135 static const struct got_error* cmd_histedit(int, char *[]);
136 static const struct got_error* cmd_integrate(int, char *[]);
137 static const struct got_error* cmd_stage(int, char *[]);
138 static const struct got_error* cmd_unstage(int, char *[]);
139 static const struct got_error* cmd_cat(int, char *[]);
140 static const struct got_error* cmd_info(int, char *[]);
142 static struct got_cmd got_commands[] = {
143 { "init", cmd_init, usage_init, "" },
144 { "import", cmd_import, usage_import, "im" },
145 { "clone", cmd_clone, usage_clone, "cl" },
146 { "fetch", cmd_fetch, usage_fetch, "fe" },
147 { "checkout", cmd_checkout, usage_checkout, "co" },
148 { "update", cmd_update, usage_update, "up" },
149 { "log", cmd_log, usage_log, "" },
150 { "diff", cmd_diff, usage_diff, "di" },
151 { "blame", cmd_blame, usage_blame, "bl" },
152 { "tree", cmd_tree, usage_tree, "tr" },
153 { "status", cmd_status, usage_status, "st" },
154 { "ref", cmd_ref, usage_ref, "" },
155 { "branch", cmd_branch, usage_branch, "br" },
156 { "tag", cmd_tag, usage_tag, "" },
157 { "add", cmd_add, usage_add, "" },
158 { "remove", cmd_remove, usage_remove, "rm" },
159 { "revert", cmd_revert, usage_revert, "rv" },
160 { "commit", cmd_commit, usage_commit, "ci" },
161 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
162 { "backout", cmd_backout, usage_backout, "bo" },
163 { "rebase", cmd_rebase, usage_rebase, "rb" },
164 { "histedit", cmd_histedit, usage_histedit, "he" },
165 { "integrate", cmd_integrate, usage_integrate,"ig" },
166 { "stage", cmd_stage, usage_stage, "sg" },
167 { "unstage", cmd_unstage, usage_unstage, "ug" },
168 { "cat", cmd_cat, usage_cat, "" },
169 { "info", cmd_info, usage_info, "" },
170 };
172 static void
173 list_commands(void)
175 int i;
177 fprintf(stderr, "commands:");
178 for (i = 0; i < nitems(got_commands); i++) {
179 struct got_cmd *cmd = &got_commands[i];
180 fprintf(stderr, " %s", cmd->cmd_name);
182 fputc('\n', stderr);
185 int
186 main(int argc, char *argv[])
188 struct got_cmd *cmd;
189 unsigned int i;
190 int ch;
191 int hflag = 0, Vflag = 0;
192 static struct option longopts[] = {
193 { "version", no_argument, NULL, 'V' },
194 { NULL, 0, NULL, 0}
195 };
197 setlocale(LC_CTYPE, "");
199 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
200 switch (ch) {
201 case 'h':
202 hflag = 1;
203 break;
204 case 'V':
205 Vflag = 1;
206 break;
207 default:
208 usage(hflag);
209 /* NOTREACHED */
213 argc -= optind;
214 argv += optind;
215 optind = 0;
217 if (Vflag) {
218 got_version_print_str();
219 return 1;
222 if (argc <= 0)
223 usage(hflag);
225 signal(SIGINT, catch_sigint);
226 signal(SIGPIPE, catch_sigpipe);
228 for (i = 0; i < nitems(got_commands); i++) {
229 const struct got_error *error;
231 cmd = &got_commands[i];
233 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
234 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 continue;
237 if (hflag)
238 got_commands[i].cmd_usage();
240 error = got_commands[i].cmd_main(argc, argv);
241 if (error && error->code != GOT_ERR_CANCELLED &&
242 error->code != GOT_ERR_PRIVSEP_EXIT &&
243 !(sigpipe_received &&
244 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
245 !(sigint_received &&
246 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
247 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
248 return 1;
251 return 0;
254 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
255 list_commands();
256 return 1;
259 __dead static void
260 usage(int hflag)
262 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
263 getprogname());
264 if (hflag)
265 list_commands();
266 exit(1);
269 static const struct got_error *
270 get_editor(char **abspath)
272 const struct got_error *err = NULL;
273 const char *editor;
275 *abspath = NULL;
277 editor = getenv("VISUAL");
278 if (editor == NULL)
279 editor = getenv("EDITOR");
281 if (editor) {
282 err = got_path_find_prog(abspath, editor);
283 if (err)
284 return err;
287 if (*abspath == NULL) {
288 *abspath = strdup("/bin/ed");
289 if (*abspath == NULL)
290 return got_error_from_errno("strdup");
293 return NULL;
296 static const struct got_error *
297 apply_unveil(const char *repo_path, int repo_read_only,
298 const char *worktree_path)
300 const struct got_error *err;
302 #ifdef PROFILE
303 if (unveil("gmon.out", "rwc") != 0)
304 return got_error_from_errno2("unveil", "gmon.out");
305 #endif
306 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
307 return got_error_from_errno2("unveil", repo_path);
309 if (worktree_path && unveil(worktree_path, "rwc") != 0)
310 return got_error_from_errno2("unveil", worktree_path);
312 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
313 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 err = got_privsep_unveil_exec_helpers();
316 if (err != NULL)
317 return err;
319 if (unveil(NULL, NULL) != 0)
320 return got_error_from_errno("unveil");
322 return NULL;
325 __dead static void
326 usage_init(void)
328 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 exit(1);
332 static const struct got_error *
333 cmd_init(int argc, char *argv[])
335 const struct got_error *error = NULL;
336 char *repo_path = NULL;
337 int ch;
339 while ((ch = getopt(argc, argv, "")) != -1) {
340 switch (ch) {
341 default:
342 usage_init();
343 /* NOTREACHED */
347 argc -= optind;
348 argv += optind;
350 #ifndef PROFILE
351 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
352 err(1, "pledge");
353 #endif
354 if (argc != 1)
355 usage_init();
357 repo_path = strdup(argv[0]);
358 if (repo_path == NULL)
359 return got_error_from_errno("strdup");
361 got_path_strip_trailing_slashes(repo_path);
363 error = got_path_mkdir(repo_path);
364 if (error &&
365 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
366 goto done;
368 error = apply_unveil(repo_path, 0, NULL);
369 if (error)
370 goto done;
372 error = got_repo_init(repo_path);
373 done:
374 free(repo_path);
375 return error;
378 __dead static void
379 usage_import(void)
381 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
382 "[-r repository-path] [-I pattern] path\n", getprogname());
383 exit(1);
386 int
387 spawn_editor(const char *editor, const char *file)
389 pid_t pid;
390 sig_t sighup, sigint, sigquit;
391 int st = -1;
393 sighup = signal(SIGHUP, SIG_IGN);
394 sigint = signal(SIGINT, SIG_IGN);
395 sigquit = signal(SIGQUIT, SIG_IGN);
397 switch (pid = fork()) {
398 case -1:
399 goto doneediting;
400 case 0:
401 execl(editor, editor, file, (char *)NULL);
402 _exit(127);
405 while (waitpid(pid, &st, 0) == -1)
406 if (errno != EINTR)
407 break;
409 doneediting:
410 (void)signal(SIGHUP, sighup);
411 (void)signal(SIGINT, sigint);
412 (void)signal(SIGQUIT, sigquit);
414 if (!WIFEXITED(st)) {
415 errno = EINTR;
416 return -1;
419 return WEXITSTATUS(st);
422 static const struct got_error *
423 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
424 const char *initial_content)
426 const struct got_error *err = NULL;
427 char buf[1024];
428 struct stat st, st2;
429 FILE *fp;
430 int content_changed = 0;
431 size_t len;
433 *logmsg = NULL;
435 if (stat(logmsg_path, &st) == -1)
436 return got_error_from_errno2("stat", logmsg_path);
438 if (spawn_editor(editor, logmsg_path) == -1)
439 return got_error_from_errno("failed spawning editor");
441 if (stat(logmsg_path, &st2) == -1)
442 return got_error_from_errno("stat");
444 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
445 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
446 "no changes made to commit message, aborting");
448 *logmsg = malloc(st2.st_size + 1);
449 if (*logmsg == NULL)
450 return got_error_from_errno("malloc");
451 (*logmsg)[0] = '\0';
452 len = 0;
454 fp = fopen(logmsg_path, "r");
455 if (fp == NULL) {
456 err = got_error_from_errno("fopen");
457 goto done;
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 if (!content_changed && strcmp(buf, initial_content) != 0)
461 content_changed = 1;
462 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
463 continue; /* remove comments and leading empty lines */
464 len = strlcat(*logmsg, buf, st2.st_size);
466 fclose(fp);
468 while (len > 0 && (*logmsg)[len - 1] == '\n') {
469 (*logmsg)[len - 1] = '\0';
470 len--;
473 if (len == 0 || !content_changed)
474 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
475 "commit message cannot be empty, aborting");
476 done:
477 if (err) {
478 free(*logmsg);
479 *logmsg = NULL;
481 return err;
484 static const struct got_error *
485 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
486 const char *path_dir, const char *branch_name)
488 char *initial_content = NULL;
489 const struct got_error *err = NULL;
490 int fd;
492 if (asprintf(&initial_content,
493 "\n# %s to be imported to branch %s\n", path_dir,
494 branch_name) == -1)
495 return got_error_from_errno("asprintf");
497 err = got_opentemp_named_fd(logmsg_path, &fd,
498 GOT_TMPDIR_STR "/got-importmsg");
499 if (err)
500 goto done;
502 dprintf(fd, initial_content);
503 close(fd);
505 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
506 done:
507 free(initial_content);
508 return err;
511 static const struct got_error *
512 import_progress(void *arg, const char *path)
514 printf("A %s\n", path);
515 return NULL;
518 static const struct got_error *
519 get_author(char **author, struct got_repository *repo)
521 const struct got_error *err = NULL;
522 const char *got_author, *name, *email;
524 *author = NULL;
526 name = got_repo_get_gitconfig_author_name(repo);
527 email = got_repo_get_gitconfig_author_email(repo);
528 if (name && email) {
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
531 return NULL;
534 got_author = getenv("GOT_AUTHOR");
535 if (got_author == NULL) {
536 name = got_repo_get_global_gitconfig_author_name(repo);
537 email = got_repo_get_global_gitconfig_author_email(repo);
538 if (name && email) {
539 if (asprintf(author, "%s <%s>", name, email) == -1)
540 return got_error_from_errno("asprintf");
541 return NULL;
543 /* TODO: Look up user in password database? */
544 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
547 *author = strdup(got_author);
548 if (*author == NULL)
549 return got_error_from_errno("strdup");
551 /*
552 * Really dumb email address check; we're only doing this to
553 * avoid git's object parser breaking on commits we create.
554 */
555 while (*got_author && *got_author != '<')
556 got_author++;
557 if (*got_author != '<') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '@')
562 got_author++;
563 if (*got_author != '@') {
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 goto done;
567 while (*got_author && *got_author != '>')
568 got_author++;
569 if (*got_author != '>')
570 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
571 done:
572 if (err) {
573 free(*author);
574 *author = NULL;
576 return err;
579 static const struct got_error *
580 get_gitconfig_path(char **gitconfig_path)
582 const char *homedir = getenv("HOME");
584 *gitconfig_path = NULL;
585 if (homedir) {
586 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
587 return got_error_from_errno("asprintf");
590 return NULL;
593 static const struct got_error *
594 cmd_import(int argc, char *argv[])
596 const struct got_error *error = NULL;
597 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
598 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
599 const char *branch_name = "main";
600 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
601 struct got_repository *repo = NULL;
602 struct got_reference *branch_ref = NULL, *head_ref = NULL;
603 struct got_object_id *new_commit_id = NULL;
604 int ch;
605 struct got_pathlist_head ignores;
606 struct got_pathlist_entry *pe;
607 int preserve_logmsg = 0;
609 TAILQ_INIT(&ignores);
611 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
612 switch (ch) {
613 case 'b':
614 branch_name = optarg;
615 break;
616 case 'm':
617 logmsg = strdup(optarg);
618 if (logmsg == NULL) {
619 error = got_error_from_errno("strdup");
620 goto done;
622 break;
623 case 'r':
624 repo_path = realpath(optarg, NULL);
625 if (repo_path == NULL) {
626 error = got_error_from_errno2("realpath",
627 optarg);
628 goto done;
630 break;
631 case 'I':
632 if (optarg[0] == '\0')
633 break;
634 error = got_pathlist_insert(&pe, &ignores, optarg,
635 NULL);
636 if (error)
637 goto done;
638 break;
639 default:
640 usage_import();
641 /* NOTREACHED */
645 argc -= optind;
646 argv += optind;
648 #ifndef PROFILE
649 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
650 "unveil",
651 NULL) == -1)
652 err(1, "pledge");
653 #endif
654 if (argc != 1)
655 usage_import();
657 if (repo_path == NULL) {
658 repo_path = getcwd(NULL, 0);
659 if (repo_path == NULL)
660 return got_error_from_errno("getcwd");
662 got_path_strip_trailing_slashes(repo_path);
663 error = get_gitconfig_path(&gitconfig_path);
664 if (error)
665 goto done;
666 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 if (error)
668 goto done;
670 error = get_author(&author, repo);
671 if (error)
672 return error;
674 /*
675 * Don't let the user create a branch name with a leading '-'.
676 * While technically a valid reference name, this case is usually
677 * an unintended typo.
678 */
679 if (branch_name[0] == '-')
680 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
682 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
683 error = got_error_from_errno("asprintf");
684 goto done;
687 error = got_ref_open(&branch_ref, repo, refname, 0);
688 if (error) {
689 if (error->code != GOT_ERR_NOT_REF)
690 goto done;
691 } else {
692 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
693 "import target branch already exists");
694 goto done;
697 path_dir = realpath(argv[0], NULL);
698 if (path_dir == NULL) {
699 error = got_error_from_errno2("realpath", argv[0]);
700 goto done;
702 got_path_strip_trailing_slashes(path_dir);
704 /*
705 * unveil(2) traverses exec(2); if an editor is used we have
706 * to apply unveil after the log message has been written.
707 */
708 if (logmsg == NULL || strlen(logmsg) == 0) {
709 error = get_editor(&editor);
710 if (error)
711 goto done;
712 free(logmsg);
713 error = collect_import_msg(&logmsg, &logmsg_path, editor,
714 path_dir, refname);
715 if (error) {
716 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
717 logmsg_path != NULL)
718 preserve_logmsg = 1;
719 goto done;
723 if (unveil(path_dir, "r") != 0) {
724 error = got_error_from_errno2("unveil", path_dir);
725 if (logmsg_path)
726 preserve_logmsg = 1;
727 goto done;
730 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
731 if (error) {
732 if (logmsg_path)
733 preserve_logmsg = 1;
734 goto done;
737 error = got_repo_import(&new_commit_id, path_dir, logmsg,
738 author, &ignores, repo, import_progress, NULL);
739 if (error) {
740 if (logmsg_path)
741 preserve_logmsg = 1;
742 goto done;
745 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
746 if (error) {
747 if (logmsg_path)
748 preserve_logmsg = 1;
749 goto done;
752 error = got_ref_write(branch_ref, repo);
753 if (error) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
759 error = got_object_id_str(&id_str, new_commit_id);
760 if (error) {
761 if (logmsg_path)
762 preserve_logmsg = 1;
763 goto done;
766 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
767 if (error) {
768 if (error->code != GOT_ERR_NOT_REF) {
769 if (logmsg_path)
770 preserve_logmsg = 1;
771 goto done;
774 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
775 branch_ref);
776 if (error) {
777 if (logmsg_path)
778 preserve_logmsg = 1;
779 goto done;
782 error = got_ref_write(head_ref, repo);
783 if (error) {
784 if (logmsg_path)
785 preserve_logmsg = 1;
786 goto done;
790 printf("Created branch %s with commit %s\n",
791 got_ref_get_name(branch_ref), id_str);
792 done:
793 if (preserve_logmsg) {
794 fprintf(stderr, "%s: log message preserved in %s\n",
795 getprogname(), logmsg_path);
796 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
797 error = got_error_from_errno2("unlink", logmsg_path);
798 free(logmsg);
799 free(logmsg_path);
800 free(repo_path);
801 free(editor);
802 free(refname);
803 free(new_commit_id);
804 free(id_str);
805 free(author);
806 free(gitconfig_path);
807 if (branch_ref)
808 got_ref_close(branch_ref);
809 if (head_ref)
810 got_ref_close(head_ref);
811 return error;
814 __dead static void
815 usage_clone(void)
817 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
818 "[-R reference] repository-url [directory]\n", getprogname());
819 exit(1);
822 struct got_fetch_progress_arg {
823 char last_scaled_size[FMT_SCALED_STRSIZE];
824 int last_p_indexed;
825 int last_p_resolved;
826 int verbosity;
827 };
829 static const struct got_error *
830 fetch_progress(void *arg, const char *message, off_t packfile_size,
831 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
833 struct got_fetch_progress_arg *a = arg;
834 char scaled_size[FMT_SCALED_STRSIZE];
835 int p_indexed, p_resolved;
836 int print_size = 0, print_indexed = 0, print_resolved = 0;
838 if (a->verbosity < 0)
839 return NULL;
841 if (message && message[0] != '\0') {
842 printf("\rserver: %s", message);
843 fflush(stdout);
844 return NULL;
847 if (packfile_size > 0 || nobj_indexed > 0) {
848 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
849 (a->last_scaled_size[0] == '\0' ||
850 strcmp(scaled_size, a->last_scaled_size)) != 0) {
851 print_size = 1;
852 if (strlcpy(a->last_scaled_size, scaled_size,
853 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
854 return got_error(GOT_ERR_NO_SPACE);
856 if (nobj_indexed > 0) {
857 p_indexed = (nobj_indexed * 100) / nobj_total;
858 if (p_indexed != a->last_p_indexed) {
859 a->last_p_indexed = p_indexed;
860 print_indexed = 1;
861 print_size = 1;
864 if (nobj_resolved > 0) {
865 p_resolved = (nobj_resolved * 100) /
866 (nobj_total - nobj_loose);
867 if (p_resolved != a->last_p_resolved) {
868 a->last_p_resolved = p_resolved;
869 print_resolved = 1;
870 print_indexed = 1;
871 print_size = 1;
876 if (print_size || print_indexed || print_resolved)
877 printf("\r");
878 if (print_size)
879 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
880 if (print_indexed)
881 printf("; indexing %d%%", p_indexed);
882 if (print_resolved)
883 printf("; resolving deltas %d%%", p_resolved);
884 if (print_size || print_indexed || print_resolved)
885 fflush(stdout);
887 return NULL;
890 static const struct got_error *
891 create_symref(const char *refname, struct got_reference *target_ref,
892 int verbosity, struct got_repository *repo)
894 const struct got_error *err;
895 struct got_reference *head_symref;
897 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
898 if (err)
899 return err;
901 err = got_ref_write(head_symref, repo);
902 got_ref_close(head_symref);
903 if (err == NULL && verbosity > 0) {
904 printf("Created reference %s: %s\n", GOT_REF_HEAD,
905 got_ref_get_name(target_ref));
907 return err;
910 static const struct got_error *
911 list_remote_refs(struct got_pathlist_head *symrefs,
912 struct got_pathlist_head *refs)
914 const struct got_error *err;
915 struct got_pathlist_entry *pe;
917 TAILQ_FOREACH(pe, symrefs, entry) {
918 const char *refname = pe->path;
919 const char *targetref = pe->data;
921 printf("%s: %s\n", refname, targetref);
924 TAILQ_FOREACH(pe, refs, entry) {
925 const char *refname = pe->path;
926 struct got_object_id *id = pe->data;
927 char *id_str;
929 err = got_object_id_str(&id_str, id);
930 if (err)
931 return err;
932 printf("%s: %s\n", refname, id_str);
933 free(id_str);
936 return NULL;
939 static const struct got_error *
940 create_ref(const char *refname, struct got_object_id *id,
941 int verbosity, struct got_repository *repo)
943 const struct got_error *err = NULL;
944 struct got_reference *ref;
945 char *id_str;
947 err = got_object_id_str(&id_str, id);
948 if (err)
949 return err;
951 err = got_ref_alloc(&ref, refname, id);
952 if (err)
953 goto done;
955 err = got_ref_write(ref, repo);
956 got_ref_close(ref);
958 if (err == NULL && verbosity >= 0)
959 printf("Created reference %s: %s\n", refname, id_str);
960 done:
961 free(id_str);
962 return err;
965 static int
966 match_wanted_ref(const char *refname, const char *wanted_ref)
968 if (strncmp(refname, "refs/", 5) != 0)
969 return 0;
970 refname += 5;
972 /*
973 * Prevent fetching of references that won't make any
974 * sense outside of the remote repository's context.
975 */
976 if (strncmp(refname, "got/", 4) == 0)
977 return 0;
978 if (strncmp(refname, "remotes/", 8) == 0)
979 return 0;
981 if (strncmp(wanted_ref, "refs/", 5) == 0)
982 wanted_ref += 5;
984 /* Allow prefix match. */
985 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
986 return 1;
988 /* Allow exact match. */
989 return (strcmp(refname, wanted_ref) == 0);
992 static int
993 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
995 struct got_pathlist_entry *pe;
997 TAILQ_FOREACH(pe, wanted_refs, entry) {
998 if (match_wanted_ref(refname, pe->path))
999 return 1;
1002 return 0;
1005 static const struct got_error *
1006 create_wanted_ref(const char *refname, struct got_object_id *id,
1007 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1009 const struct got_error *err;
1010 char *remote_refname;
1012 if (strncmp("refs/", refname, 5) == 0)
1013 refname += 5;
1015 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1016 remote_repo_name, refname) == -1)
1017 return got_error_from_errno("asprintf");
1019 err = create_ref(remote_refname, id, verbosity, repo);
1020 free(remote_refname);
1021 return err;
1024 static const struct got_error *
1025 cmd_clone(int argc, char *argv[])
1027 const struct got_error *error = NULL;
1028 const char *uri, *dirname;
1029 char *proto, *host, *port, *repo_name, *server_path;
1030 char *default_destdir = NULL, *id_str = NULL;
1031 const char *repo_path;
1032 struct got_repository *repo = NULL;
1033 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1034 struct got_pathlist_entry *pe;
1035 struct got_object_id *pack_hash = NULL;
1036 int ch, fetchfd = -1, fetchstatus;
1037 pid_t fetchpid = -1;
1038 struct got_fetch_progress_arg fpa;
1039 char *git_url = NULL;
1040 char *gitconfig_path = NULL;
1041 char *gitconfig = NULL;
1042 FILE *gitconfig_file = NULL;
1043 ssize_t n;
1044 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1045 int list_refs_only = 0;
1046 struct got_reference *head_symref = NULL;
1048 TAILQ_INIT(&refs);
1049 TAILQ_INIT(&symrefs);
1050 TAILQ_INIT(&wanted_branches);
1051 TAILQ_INIT(&wanted_refs);
1053 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1054 switch (ch) {
1055 case 'a':
1056 fetch_all_branches = 1;
1057 break;
1058 case 'b':
1059 error = got_pathlist_append(&wanted_branches,
1060 optarg, NULL);
1061 if (error)
1062 return error;
1063 break;
1064 case 'l':
1065 list_refs_only = 1;
1066 break;
1067 case 'm':
1068 mirror_references = 1;
1069 break;
1070 case 'v':
1071 if (verbosity < 0)
1072 verbosity = 0;
1073 else if (verbosity < 3)
1074 verbosity++;
1075 break;
1076 case 'q':
1077 verbosity = -1;
1078 break;
1079 case 'R':
1080 error = got_pathlist_append(&wanted_refs,
1081 optarg, NULL);
1082 if (error)
1083 return error;
1084 break;
1085 default:
1086 usage_clone();
1087 break;
1090 argc -= optind;
1091 argv += optind;
1093 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1094 errx(1, "-a and -b options are mutually exclusive");
1095 if (list_refs_only) {
1096 if (!TAILQ_EMPTY(&wanted_branches))
1097 errx(1, "-l and -b options are mutually exclusive");
1098 if (fetch_all_branches)
1099 errx(1, "-l and -a options are mutually exclusive");
1100 if (mirror_references)
1101 errx(1, "-l and -m options are mutually exclusive");
1102 if (verbosity == -1)
1103 errx(1, "-l and -q options are mutually exclusive");
1104 if (!TAILQ_EMPTY(&wanted_refs))
1105 errx(1, "-l and -R options are mutually exclusive");
1108 uri = argv[0];
1110 if (argc == 1)
1111 dirname = NULL;
1112 else if (argc == 2)
1113 dirname = argv[1];
1114 else
1115 usage_clone();
1117 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1118 &repo_name, argv[0]);
1119 if (error)
1120 goto done;
1122 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1123 host, port ? ":" : "", port ? port : "",
1124 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1125 error = got_error_from_errno("asprintf");
1126 goto done;
1129 if (strcmp(proto, "git") == 0) {
1130 #ifndef PROFILE
1131 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1132 "sendfd dns inet unveil", NULL) == -1)
1133 err(1, "pledge");
1134 #endif
1135 } else if (strcmp(proto, "git+ssh") == 0 ||
1136 strcmp(proto, "ssh") == 0) {
1137 #ifndef PROFILE
1138 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1139 "sendfd unveil", NULL) == -1)
1140 err(1, "pledge");
1141 #endif
1142 } else if (strcmp(proto, "http") == 0 ||
1143 strcmp(proto, "git+http") == 0) {
1144 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1145 goto done;
1146 } else {
1147 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1148 goto done;
1150 if (dirname == NULL) {
1151 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1152 error = got_error_from_errno("asprintf");
1153 goto done;
1155 repo_path = default_destdir;
1156 } else
1157 repo_path = dirname;
1159 if (!list_refs_only) {
1160 error = got_path_mkdir(repo_path);
1161 if (error)
1162 goto done;
1164 error = got_repo_init(repo_path);
1165 if (error)
1166 goto done;
1167 error = got_repo_open(&repo, repo_path, NULL);
1168 if (error)
1169 goto done;
1172 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1173 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1174 error = got_error_from_errno2("unveil",
1175 GOT_FETCH_PATH_SSH);
1176 goto done;
1179 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1180 if (error)
1181 goto done;
1183 if (verbosity >= 0)
1184 printf("Connecting to %s%s%s\n", host,
1185 port ? ":" : "", port ? port : "");
1187 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1188 server_path, verbosity);
1189 if (error)
1190 goto done;
1192 fpa.last_scaled_size[0] = '\0';
1193 fpa.last_p_indexed = -1;
1194 fpa.last_p_resolved = -1;
1195 fpa.verbosity = verbosity;
1196 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1197 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1198 fetch_all_branches, &wanted_branches, &wanted_refs,
1199 list_refs_only, verbosity, fetchfd, repo,
1200 fetch_progress, &fpa);
1201 if (error)
1202 goto done;
1204 if (list_refs_only) {
1205 error = list_remote_refs(&symrefs, &refs);
1206 goto done;
1209 error = got_object_id_str(&id_str, pack_hash);
1210 if (error)
1211 goto done;
1212 if (verbosity >= 0)
1213 printf("\nFetched %s.pack\n", id_str);
1214 free(id_str);
1216 /* Set up references provided with the pack file. */
1217 TAILQ_FOREACH(pe, &refs, entry) {
1218 const char *refname = pe->path;
1219 struct got_object_id *id = pe->data;
1220 char *remote_refname;
1222 if (is_wanted_ref(&wanted_refs, refname) &&
1223 !mirror_references) {
1224 error = create_wanted_ref(refname, id,
1225 GOT_FETCH_DEFAULT_REMOTE_NAME,
1226 verbosity - 1, repo);
1227 if (error)
1228 goto done;
1229 continue;
1232 error = create_ref(refname, id, verbosity - 1, repo);
1233 if (error)
1234 goto done;
1236 if (mirror_references)
1237 continue;
1239 if (strncmp("refs/heads/", refname, 11) != 0)
1240 continue;
1242 if (asprintf(&remote_refname,
1243 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1244 refname + 11) == -1) {
1245 error = got_error_from_errno("asprintf");
1246 goto done;
1248 error = create_ref(remote_refname, id, verbosity - 1, repo);
1249 free(remote_refname);
1250 if (error)
1251 goto done;
1254 /* Set the HEAD reference if the server provided one. */
1255 TAILQ_FOREACH(pe, &symrefs, entry) {
1256 struct got_reference *target_ref;
1257 const char *refname = pe->path;
1258 const char *target = pe->data;
1259 char *remote_refname = NULL, *remote_target = NULL;
1261 if (strcmp(refname, GOT_REF_HEAD) != 0)
1262 continue;
1264 error = got_ref_open(&target_ref, repo, target, 0);
1265 if (error) {
1266 if (error->code == GOT_ERR_NOT_REF) {
1267 error = NULL;
1268 continue;
1270 goto done;
1273 error = create_symref(refname, target_ref, verbosity, repo);
1274 got_ref_close(target_ref);
1275 if (error)
1276 goto done;
1278 if (mirror_references)
1279 continue;
1281 if (strncmp("refs/heads/", target, 11) != 0)
1282 continue;
1284 if (asprintf(&remote_refname,
1285 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1286 refname) == -1) {
1287 error = got_error_from_errno("asprintf");
1288 goto done;
1290 if (asprintf(&remote_target,
1291 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1292 target + 11) == -1) {
1293 error = got_error_from_errno("asprintf");
1294 free(remote_refname);
1295 goto done;
1297 error = got_ref_open(&target_ref, repo, remote_target, 0);
1298 if (error) {
1299 free(remote_refname);
1300 free(remote_target);
1301 if (error->code == GOT_ERR_NOT_REF) {
1302 error = NULL;
1303 continue;
1305 goto done;
1307 error = create_symref(remote_refname, target_ref,
1308 verbosity - 1, repo);
1309 free(remote_refname);
1310 free(remote_target);
1311 got_ref_close(target_ref);
1312 if (error)
1313 goto done;
1315 if (pe == NULL) {
1317 * We failed to set the HEAD reference. If we asked for
1318 * a set of wanted branches use the first of one of those
1319 * which could be fetched instead.
1321 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1322 const char *target = pe->path;
1323 struct got_reference *target_ref;
1325 error = got_ref_open(&target_ref, repo, target, 0);
1326 if (error) {
1327 if (error->code == GOT_ERR_NOT_REF) {
1328 error = NULL;
1329 continue;
1331 goto done;
1334 error = create_symref(GOT_REF_HEAD, target_ref,
1335 verbosity, repo);
1336 got_ref_close(target_ref);
1337 if (error)
1338 goto done;
1339 break;
1343 /* Create a config file git-fetch(1) can understand. */
1344 gitconfig_path = got_repo_get_path_gitconfig(repo);
1345 if (gitconfig_path == NULL) {
1346 error = got_error_from_errno("got_repo_get_path_gitconfig");
1347 goto done;
1349 gitconfig_file = fopen(gitconfig_path, "a");
1350 if (gitconfig_file == NULL) {
1351 error = got_error_from_errno2("fopen", gitconfig_path);
1352 goto done;
1354 if (mirror_references) {
1355 if (asprintf(&gitconfig,
1356 "[remote \"%s\"]\n"
1357 "\turl = %s\n"
1358 "\tfetch = +refs/*:refs/*\n"
1359 "\tmirror = true\n",
1360 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1361 error = got_error_from_errno("asprintf");
1362 goto done;
1364 } else if (fetch_all_branches) {
1365 if (asprintf(&gitconfig,
1366 "[remote \"%s\"]\n"
1367 "\turl = %s\n"
1368 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1369 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1370 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1371 error = got_error_from_errno("asprintf");
1372 goto done;
1374 } else {
1375 const char *branchname;
1378 * If the server specified a default branch, use just that one.
1379 * Otherwise fall back to fetching all branches on next fetch.
1381 if (head_symref) {
1382 branchname = got_ref_get_symref_target(head_symref);
1383 if (strncmp(branchname, "refs/heads/", 11) == 0)
1384 branchname += 11;
1385 } else
1386 branchname = "*"; /* fall back to all branches */
1387 if (asprintf(&gitconfig,
1388 "[remote \"%s\"]\n"
1389 "\turl = %s\n"
1390 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1391 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1392 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1393 branchname) == -1) {
1394 error = got_error_from_errno("asprintf");
1395 goto done;
1398 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1399 if (n != strlen(gitconfig)) {
1400 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1401 goto done;
1404 if (verbosity >= 0)
1405 printf("Created %s repository '%s'\n",
1406 mirror_references ? "mirrored" : "cloned", repo_path);
1407 done:
1408 if (fetchpid > 0) {
1409 if (kill(fetchpid, SIGTERM) == -1)
1410 error = got_error_from_errno("kill");
1411 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1412 error = got_error_from_errno("waitpid");
1414 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1415 error = got_error_from_errno("close");
1416 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1417 error = got_error_from_errno("fclose");
1418 if (repo)
1419 got_repo_close(repo);
1420 if (head_symref)
1421 got_ref_close(head_symref);
1422 TAILQ_FOREACH(pe, &refs, entry) {
1423 free((void *)pe->path);
1424 free(pe->data);
1426 got_pathlist_free(&refs);
1427 TAILQ_FOREACH(pe, &symrefs, entry) {
1428 free((void *)pe->path);
1429 free(pe->data);
1431 got_pathlist_free(&symrefs);
1432 got_pathlist_free(&wanted_branches);
1433 got_pathlist_free(&wanted_refs);
1434 free(pack_hash);
1435 free(proto);
1436 free(host);
1437 free(port);
1438 free(server_path);
1439 free(repo_name);
1440 free(default_destdir);
1441 free(gitconfig_path);
1442 free(git_url);
1443 return error;
1446 static const struct got_error *
1447 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1448 int replace_tags, int verbosity, struct got_repository *repo)
1450 const struct got_error *err = NULL;
1451 char *new_id_str = NULL;
1452 struct got_object_id *old_id = NULL;
1454 err = got_object_id_str(&new_id_str, new_id);
1455 if (err)
1456 goto done;
1458 if (!replace_tags &&
1459 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1460 err = got_ref_resolve(&old_id, repo, ref);
1461 if (err)
1462 goto done;
1463 if (got_object_id_cmp(old_id, new_id) == 0)
1464 goto done;
1465 if (verbosity >= 0) {
1466 printf("Rejecting update of existing tag %s: %s\n",
1467 got_ref_get_name(ref), new_id_str);
1469 goto done;
1472 if (got_ref_is_symbolic(ref)) {
1473 if (verbosity >= 0) {
1474 printf("Replacing reference %s: %s\n",
1475 got_ref_get_name(ref),
1476 got_ref_get_symref_target(ref));
1478 err = got_ref_change_symref_to_ref(ref, new_id);
1479 if (err)
1480 goto done;
1481 err = got_ref_write(ref, repo);
1482 if (err)
1483 goto done;
1484 } else {
1485 err = got_ref_resolve(&old_id, repo, ref);
1486 if (err)
1487 goto done;
1488 if (got_object_id_cmp(old_id, new_id) == 0)
1489 goto done;
1491 err = got_ref_change_ref(ref, new_id);
1492 if (err)
1493 goto done;
1494 err = got_ref_write(ref, repo);
1495 if (err)
1496 goto done;
1499 if (verbosity >= 0)
1500 printf("Updated %s: %s\n", got_ref_get_name(ref),
1501 new_id_str);
1502 done:
1503 free(old_id);
1504 free(new_id_str);
1505 return err;
1508 static const struct got_error *
1509 update_symref(const char *refname, struct got_reference *target_ref,
1510 int verbosity, struct got_repository *repo)
1512 const struct got_error *err = NULL, *unlock_err;
1513 struct got_reference *symref;
1514 int symref_is_locked = 0;
1516 err = got_ref_open(&symref, repo, refname, 1);
1517 if (err) {
1518 if (err->code != GOT_ERR_NOT_REF)
1519 return err;
1520 err = got_ref_alloc_symref(&symref, refname, target_ref);
1521 if (err)
1522 goto done;
1524 err = got_ref_write(symref, repo);
1525 if (err)
1526 goto done;
1528 if (verbosity >= 0)
1529 printf("Created reference %s: %s\n",
1530 got_ref_get_name(symref),
1531 got_ref_get_symref_target(symref));
1532 } else {
1533 symref_is_locked = 1;
1535 if (strcmp(got_ref_get_symref_target(symref),
1536 got_ref_get_name(target_ref)) == 0)
1537 goto done;
1539 err = got_ref_change_symref(symref,
1540 got_ref_get_name(target_ref));
1541 if (err)
1542 goto done;
1544 err = got_ref_write(symref, repo);
1545 if (err)
1546 goto done;
1548 if (verbosity >= 0)
1549 printf("Updated %s: %s\n", got_ref_get_name(symref),
1550 got_ref_get_symref_target(symref));
1553 done:
1554 if (symref_is_locked) {
1555 unlock_err = got_ref_unlock(symref);
1556 if (unlock_err && err == NULL)
1557 err = unlock_err;
1559 got_ref_close(symref);
1560 return err;
1563 __dead static void
1564 usage_fetch(void)
1566 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1567 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1568 "[remote-repository-name]\n",
1569 getprogname());
1570 exit(1);
1573 static const struct got_error *
1574 delete_missing_ref(struct got_reference *ref,
1575 int verbosity, struct got_repository *repo)
1577 const struct got_error *err = NULL;
1578 struct got_object_id *id = NULL;
1579 char *id_str = NULL;
1581 if (got_ref_is_symbolic(ref)) {
1582 err = got_ref_delete(ref, repo);
1583 if (err)
1584 return err;
1585 if (verbosity >= 0) {
1586 printf("Deleted reference %s: %s\n",
1587 got_ref_get_name(ref),
1588 got_ref_get_symref_target(ref));
1590 } else {
1591 err = got_ref_resolve(&id, repo, ref);
1592 if (err)
1593 return err;
1594 err = got_object_id_str(&id_str, id);
1595 if (err)
1596 goto done;
1598 err = got_ref_delete(ref, repo);
1599 if (err)
1600 goto done;
1601 if (verbosity >= 0) {
1602 printf("Deleted reference %s: %s\n",
1603 got_ref_get_name(ref), id_str);
1606 done:
1607 free(id);
1608 free(id_str);
1609 return NULL;
1612 static const struct got_error *
1613 delete_missing_refs(struct got_pathlist_head *their_refs,
1614 struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1615 int verbosity, struct got_repository *repo)
1617 const struct got_error *err = NULL, *unlock_err;
1618 struct got_reflist_head my_refs;
1619 struct got_reflist_entry *re;
1620 struct got_pathlist_entry *pe;
1621 char *remote_namespace = NULL;
1622 char *local_refname = NULL;
1624 SIMPLEQ_INIT(&my_refs);
1626 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1627 == -1)
1628 return got_error_from_errno("asprintf");
1630 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1631 if (err)
1632 goto done;
1634 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1635 const char *refname = got_ref_get_name(re->ref);
1637 if (!remote->mirror_references) {
1638 if (strncmp(refname, remote_namespace,
1639 strlen(remote_namespace)) == 0) {
1640 if (strcmp(refname + strlen(remote_namespace),
1641 GOT_REF_HEAD) == 0)
1642 continue;
1643 if (asprintf(&local_refname, "refs/heads/%s",
1644 refname + strlen(remote_namespace)) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 goto done;
1648 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1649 continue;
1652 TAILQ_FOREACH(pe, their_refs, entry) {
1653 if (strcmp(local_refname, pe->path) == 0)
1654 break;
1656 if (pe != NULL)
1657 continue;
1659 TAILQ_FOREACH(pe, their_symrefs, entry) {
1660 if (strcmp(local_refname, pe->path) == 0)
1661 break;
1663 if (pe != NULL)
1664 continue;
1666 err = delete_missing_ref(re->ref, verbosity, repo);
1667 if (err)
1668 break;
1670 if (local_refname) {
1671 struct got_reference *ref;
1672 err = got_ref_open(&ref, repo, local_refname, 1);
1673 if (err) {
1674 if (err->code != GOT_ERR_NOT_REF)
1675 break;
1676 free(local_refname);
1677 local_refname = NULL;
1678 continue;
1680 err = delete_missing_ref(ref, verbosity, repo);
1681 if (err)
1682 break;
1683 unlock_err = got_ref_unlock(ref);
1684 got_ref_close(ref);
1685 if (unlock_err && err == NULL) {
1686 err = unlock_err;
1687 break;
1690 free(local_refname);
1691 local_refname = NULL;
1694 done:
1695 free(remote_namespace);
1696 free(local_refname);
1697 return err;
1700 static const struct got_error *
1701 update_wanted_ref(const char *refname, struct got_object_id *id,
1702 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1704 const struct got_error *err, *unlock_err;
1705 char *remote_refname;
1706 struct got_reference *ref;
1708 if (strncmp("refs/", refname, 5) == 0)
1709 refname += 5;
1711 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1712 remote_repo_name, refname) == -1)
1713 return got_error_from_errno("asprintf");
1715 err = got_ref_open(&ref, repo, remote_refname, 1);
1716 if (err) {
1717 if (err->code != GOT_ERR_NOT_REF)
1718 goto done;
1719 err = create_ref(remote_refname, id, verbosity, repo);
1720 } else {
1721 err = update_ref(ref, id, 0, verbosity, repo);
1722 unlock_err = got_ref_unlock(ref);
1723 if (unlock_err && err == NULL)
1724 err = unlock_err;
1725 got_ref_close(ref);
1727 done:
1728 free(remote_refname);
1729 return err;
1732 static const struct got_error *
1733 cmd_fetch(int argc, char *argv[])
1735 const struct got_error *error = NULL, *unlock_err;
1736 char *cwd = NULL, *repo_path = NULL;
1737 const char *remote_name;
1738 char *proto = NULL, *host = NULL, *port = NULL;
1739 char *repo_name = NULL, *server_path = NULL;
1740 struct got_remote_repo *remotes, *remote = NULL;
1741 int nremotes;
1742 char *id_str = NULL;
1743 struct got_repository *repo = NULL;
1744 struct got_worktree *worktree = NULL;
1745 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1746 struct got_pathlist_entry *pe;
1747 struct got_object_id *pack_hash = NULL;
1748 int i, ch, fetchfd = -1, fetchstatus;
1749 pid_t fetchpid = -1;
1750 struct got_fetch_progress_arg fpa;
1751 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1752 int delete_refs = 0, replace_tags = 0;
1754 TAILQ_INIT(&refs);
1755 TAILQ_INIT(&symrefs);
1756 TAILQ_INIT(&wanted_branches);
1757 TAILQ_INIT(&wanted_refs);
1759 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1760 switch (ch) {
1761 case 'a':
1762 fetch_all_branches = 1;
1763 break;
1764 case 'b':
1765 error = got_pathlist_append(&wanted_branches,
1766 optarg, NULL);
1767 if (error)
1768 return error;
1769 break;
1770 case 'd':
1771 delete_refs = 1;
1772 break;
1773 case 'l':
1774 list_refs_only = 1;
1775 break;
1776 case 'r':
1777 repo_path = realpath(optarg, NULL);
1778 if (repo_path == NULL)
1779 return got_error_from_errno2("realpath",
1780 optarg);
1781 got_path_strip_trailing_slashes(repo_path);
1782 break;
1783 case 't':
1784 replace_tags = 1;
1785 break;
1786 case 'v':
1787 if (verbosity < 0)
1788 verbosity = 0;
1789 else if (verbosity < 3)
1790 verbosity++;
1791 break;
1792 case 'q':
1793 verbosity = -1;
1794 break;
1795 case 'R':
1796 error = got_pathlist_append(&wanted_refs,
1797 optarg, NULL);
1798 if (error)
1799 return error;
1800 break;
1801 default:
1802 usage_fetch();
1803 break;
1806 argc -= optind;
1807 argv += optind;
1809 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1810 errx(1, "-a and -b options are mutually exclusive");
1811 if (list_refs_only) {
1812 if (!TAILQ_EMPTY(&wanted_branches))
1813 errx(1, "-l and -b options are mutually exclusive");
1814 if (fetch_all_branches)
1815 errx(1, "-l and -a options are mutually exclusive");
1816 if (delete_refs)
1817 errx(1, "-l and -d options are mutually exclusive");
1818 if (verbosity == -1)
1819 errx(1, "-l and -q options are mutually exclusive");
1822 if (argc == 0)
1823 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1824 else if (argc == 1)
1825 remote_name = argv[0];
1826 else
1827 usage_fetch();
1829 cwd = getcwd(NULL, 0);
1830 if (cwd == NULL) {
1831 error = got_error_from_errno("getcwd");
1832 goto done;
1835 if (repo_path == NULL) {
1836 error = got_worktree_open(&worktree, cwd);
1837 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1838 goto done;
1839 else
1840 error = NULL;
1841 if (worktree) {
1842 repo_path =
1843 strdup(got_worktree_get_repo_path(worktree));
1844 if (repo_path == NULL)
1845 error = got_error_from_errno("strdup");
1846 if (error)
1847 goto done;
1848 } else {
1849 repo_path = strdup(cwd);
1850 if (repo_path == NULL) {
1851 error = got_error_from_errno("strdup");
1852 goto done;
1857 error = got_repo_open(&repo, repo_path, NULL);
1858 if (error)
1859 goto done;
1861 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1862 for (i = 0; i < nremotes; i++) {
1863 remote = &remotes[i];
1864 if (strcmp(remote->name, remote_name) == 0)
1865 break;
1867 if (i == nremotes) {
1868 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1869 goto done;
1872 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1873 &repo_name, remote->url);
1874 if (error)
1875 goto done;
1877 if (strcmp(proto, "git") == 0) {
1878 #ifndef PROFILE
1879 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1880 "sendfd dns inet unveil", NULL) == -1)
1881 err(1, "pledge");
1882 #endif
1883 } else if (strcmp(proto, "git+ssh") == 0 ||
1884 strcmp(proto, "ssh") == 0) {
1885 #ifndef PROFILE
1886 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1887 "sendfd unveil", NULL) == -1)
1888 err(1, "pledge");
1889 #endif
1890 } else if (strcmp(proto, "http") == 0 ||
1891 strcmp(proto, "git+http") == 0) {
1892 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1893 goto done;
1894 } else {
1895 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1896 goto done;
1899 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1900 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1901 error = got_error_from_errno2("unveil",
1902 GOT_FETCH_PATH_SSH);
1903 goto done;
1906 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1907 if (error)
1908 goto done;
1910 if (verbosity >= 0)
1911 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1912 port ? ":" : "", port ? port : "");
1914 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1915 server_path, verbosity);
1916 if (error)
1917 goto done;
1919 fpa.last_scaled_size[0] = '\0';
1920 fpa.last_p_indexed = -1;
1921 fpa.last_p_resolved = -1;
1922 fpa.verbosity = verbosity;
1923 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1924 remote->mirror_references, fetch_all_branches, &wanted_branches,
1925 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1926 fetch_progress, &fpa);
1927 if (error)
1928 goto done;
1930 if (list_refs_only) {
1931 error = list_remote_refs(&symrefs, &refs);
1932 goto done;
1935 if (pack_hash == NULL) {
1936 if (verbosity >= 0)
1937 printf("Already up-to-date\n");
1938 } else if (verbosity >= 0) {
1939 error = got_object_id_str(&id_str, pack_hash);
1940 if (error)
1941 goto done;
1942 printf("\nFetched %s.pack\n", id_str);
1943 free(id_str);
1944 id_str = NULL;
1947 /* Update references provided with the pack file. */
1948 TAILQ_FOREACH(pe, &refs, entry) {
1949 const char *refname = pe->path;
1950 struct got_object_id *id = pe->data;
1951 struct got_reference *ref;
1952 char *remote_refname;
1954 if (is_wanted_ref(&wanted_refs, refname) &&
1955 !remote->mirror_references) {
1956 error = update_wanted_ref(refname, id,
1957 remote->name, verbosity, repo);
1958 if (error)
1959 goto done;
1960 continue;
1963 if (remote->mirror_references ||
1964 strncmp("refs/tags/", refname, 10) == 0) {
1965 error = got_ref_open(&ref, repo, refname, 1);
1966 if (error) {
1967 if (error->code != GOT_ERR_NOT_REF)
1968 goto done;
1969 error = create_ref(refname, id, verbosity,
1970 repo);
1971 if (error)
1972 goto done;
1973 } else {
1974 error = update_ref(ref, id, replace_tags,
1975 verbosity, repo);
1976 unlock_err = got_ref_unlock(ref);
1977 if (unlock_err && error == NULL)
1978 error = unlock_err;
1979 got_ref_close(ref);
1980 if (error)
1981 goto done;
1983 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1984 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1985 remote_name, refname + 11) == -1) {
1986 error = got_error_from_errno("asprintf");
1987 goto done;
1990 error = got_ref_open(&ref, repo, remote_refname, 1);
1991 if (error) {
1992 if (error->code != GOT_ERR_NOT_REF)
1993 goto done;
1994 error = create_ref(remote_refname, id,
1995 verbosity, repo);
1996 if (error)
1997 goto done;
1998 } else {
1999 error = update_ref(ref, id, replace_tags,
2000 verbosity, repo);
2001 unlock_err = got_ref_unlock(ref);
2002 if (unlock_err && error == NULL)
2003 error = unlock_err;
2004 got_ref_close(ref);
2005 if (error)
2006 goto done;
2009 /* Also create a local branch if none exists yet. */
2010 error = got_ref_open(&ref, repo, refname, 1);
2011 if (error) {
2012 if (error->code != GOT_ERR_NOT_REF)
2013 goto done;
2014 error = create_ref(refname, id, verbosity,
2015 repo);
2016 if (error)
2017 goto done;
2018 } else {
2019 unlock_err = got_ref_unlock(ref);
2020 if (unlock_err && error == NULL)
2021 error = unlock_err;
2022 got_ref_close(ref);
2026 if (delete_refs) {
2027 error = delete_missing_refs(&refs, &symrefs, remote,
2028 verbosity, repo);
2029 if (error)
2030 goto done;
2033 if (!remote->mirror_references) {
2034 /* Update remote HEAD reference if the server provided one. */
2035 TAILQ_FOREACH(pe, &symrefs, entry) {
2036 struct got_reference *target_ref;
2037 const char *refname = pe->path;
2038 const char *target = pe->data;
2039 char *remote_refname = NULL, *remote_target = NULL;
2041 if (strcmp(refname, GOT_REF_HEAD) != 0)
2042 continue;
2044 if (strncmp("refs/heads/", target, 11) != 0)
2045 continue;
2047 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2048 remote->name, refname) == -1) {
2049 error = got_error_from_errno("asprintf");
2050 goto done;
2052 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2053 remote->name, target + 11) == -1) {
2054 error = got_error_from_errno("asprintf");
2055 free(remote_refname);
2056 goto done;
2059 error = got_ref_open(&target_ref, repo, remote_target,
2060 0);
2061 if (error) {
2062 free(remote_refname);
2063 free(remote_target);
2064 if (error->code == GOT_ERR_NOT_REF) {
2065 error = NULL;
2066 continue;
2068 goto done;
2070 error = update_symref(remote_refname, target_ref,
2071 verbosity, repo);
2072 free(remote_refname);
2073 free(remote_target);
2074 got_ref_close(target_ref);
2075 if (error)
2076 goto done;
2079 done:
2080 if (fetchpid > 0) {
2081 if (kill(fetchpid, SIGTERM) == -1)
2082 error = got_error_from_errno("kill");
2083 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2084 error = got_error_from_errno("waitpid");
2086 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2087 error = got_error_from_errno("close");
2088 if (repo)
2089 got_repo_close(repo);
2090 if (worktree)
2091 got_worktree_close(worktree);
2092 TAILQ_FOREACH(pe, &refs, entry) {
2093 free((void *)pe->path);
2094 free(pe->data);
2096 got_pathlist_free(&refs);
2097 TAILQ_FOREACH(pe, &symrefs, entry) {
2098 free((void *)pe->path);
2099 free(pe->data);
2101 got_pathlist_free(&symrefs);
2102 got_pathlist_free(&wanted_branches);
2103 got_pathlist_free(&wanted_refs);
2104 free(id_str);
2105 free(cwd);
2106 free(repo_path);
2107 free(pack_hash);
2108 free(proto);
2109 free(host);
2110 free(port);
2111 free(server_path);
2112 free(repo_name);
2113 return error;
2117 __dead static void
2118 usage_checkout(void)
2120 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2121 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2122 exit(1);
2125 static void
2126 show_worktree_base_ref_warning(void)
2128 fprintf(stderr, "%s: warning: could not create a reference "
2129 "to the work tree's base commit; the commit could be "
2130 "garbage-collected by Git; making the repository "
2131 "writable and running 'got update' will prevent this\n",
2132 getprogname());
2135 struct got_checkout_progress_arg {
2136 const char *worktree_path;
2137 int had_base_commit_ref_error;
2140 static const struct got_error *
2141 checkout_progress(void *arg, unsigned char status, const char *path)
2143 struct got_checkout_progress_arg *a = arg;
2145 /* Base commit bump happens silently. */
2146 if (status == GOT_STATUS_BUMP_BASE)
2147 return NULL;
2149 if (status == GOT_STATUS_BASE_REF_ERR) {
2150 a->had_base_commit_ref_error = 1;
2151 return NULL;
2154 while (path[0] == '/')
2155 path++;
2157 printf("%c %s/%s\n", status, a->worktree_path, path);
2158 return NULL;
2161 static const struct got_error *
2162 check_cancelled(void *arg)
2164 if (sigint_received || sigpipe_received)
2165 return got_error(GOT_ERR_CANCELLED);
2166 return NULL;
2169 static const struct got_error *
2170 check_linear_ancestry(struct got_object_id *commit_id,
2171 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 struct got_object_id *yca_id;
2177 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2178 commit_id, base_commit_id, repo, check_cancelled, NULL);
2179 if (err)
2180 return err;
2182 if (yca_id == NULL)
2183 return got_error(GOT_ERR_ANCESTRY);
2186 * Require a straight line of history between the target commit
2187 * and the work tree's base commit.
2189 * Non-linear situations such as this require a rebase:
2191 * (commit) D F (base_commit)
2192 * \ /
2193 * C E
2194 * \ /
2195 * B (yca)
2196 * |
2197 * A
2199 * 'got update' only handles linear cases:
2200 * Update forwards in time: A (base/yca) - B - C - D (commit)
2201 * Update backwards in time: D (base) - C - B - A (commit/yca)
2203 if (allow_forwards_in_time_only) {
2204 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2205 return got_error(GOT_ERR_ANCESTRY);
2206 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2207 got_object_id_cmp(base_commit_id, yca_id) != 0)
2208 return got_error(GOT_ERR_ANCESTRY);
2210 free(yca_id);
2211 return NULL;
2214 static const struct got_error *
2215 check_same_branch(struct got_object_id *commit_id,
2216 struct got_reference *head_ref, struct got_object_id *yca_id,
2217 struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_commit_graph *graph = NULL;
2221 struct got_object_id *head_commit_id = NULL;
2222 int is_same_branch = 0;
2224 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2225 if (err)
2226 goto done;
2228 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2229 is_same_branch = 1;
2230 goto done;
2232 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2233 is_same_branch = 1;
2234 goto done;
2237 err = got_commit_graph_open(&graph, "/", 1);
2238 if (err)
2239 goto done;
2241 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2242 check_cancelled, NULL);
2243 if (err)
2244 goto done;
2246 for (;;) {
2247 struct got_object_id *id;
2248 err = got_commit_graph_iter_next(&id, graph, repo,
2249 check_cancelled, NULL);
2250 if (err) {
2251 if (err->code == GOT_ERR_ITER_COMPLETED)
2252 err = NULL;
2253 break;
2256 if (id) {
2257 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2258 break;
2259 if (got_object_id_cmp(id, commit_id) == 0) {
2260 is_same_branch = 1;
2261 break;
2265 done:
2266 if (graph)
2267 got_commit_graph_close(graph);
2268 free(head_commit_id);
2269 if (!err && !is_same_branch)
2270 err = got_error(GOT_ERR_ANCESTRY);
2271 return err;
2274 static const struct got_error *
2275 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2277 static char msg[512];
2278 const char *branch_name;
2280 if (got_ref_is_symbolic(ref))
2281 branch_name = got_ref_get_symref_target(ref);
2282 else
2283 branch_name = got_ref_get_name(ref);
2285 if (strncmp("refs/heads/", branch_name, 11) == 0)
2286 branch_name += 11;
2288 snprintf(msg, sizeof(msg),
2289 "target commit is not contained in branch '%s'; "
2290 "the branch to use must be specified with -b; "
2291 "if necessary a new branch can be created for "
2292 "this commit with 'got branch -c %s BRANCH_NAME'",
2293 branch_name, commit_id_str);
2295 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2298 static const struct got_error *
2299 cmd_checkout(int argc, char *argv[])
2301 const struct got_error *error = NULL;
2302 struct got_repository *repo = NULL;
2303 struct got_reference *head_ref = NULL;
2304 struct got_worktree *worktree = NULL;
2305 char *repo_path = NULL;
2306 char *worktree_path = NULL;
2307 const char *path_prefix = "";
2308 const char *branch_name = GOT_REF_HEAD;
2309 char *commit_id_str = NULL;
2310 int ch, same_path_prefix, allow_nonempty = 0;
2311 struct got_pathlist_head paths;
2312 struct got_checkout_progress_arg cpa;
2314 TAILQ_INIT(&paths);
2316 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2317 switch (ch) {
2318 case 'b':
2319 branch_name = optarg;
2320 break;
2321 case 'c':
2322 commit_id_str = strdup(optarg);
2323 if (commit_id_str == NULL)
2324 return got_error_from_errno("strdup");
2325 break;
2326 case 'E':
2327 allow_nonempty = 1;
2328 break;
2329 case 'p':
2330 path_prefix = optarg;
2331 break;
2332 default:
2333 usage_checkout();
2334 /* NOTREACHED */
2338 argc -= optind;
2339 argv += optind;
2341 #ifndef PROFILE
2342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2343 "unveil", NULL) == -1)
2344 err(1, "pledge");
2345 #endif
2346 if (argc == 1) {
2347 char *cwd, *base, *dotgit;
2348 repo_path = realpath(argv[0], NULL);
2349 if (repo_path == NULL)
2350 return got_error_from_errno2("realpath", argv[0]);
2351 cwd = getcwd(NULL, 0);
2352 if (cwd == NULL) {
2353 error = got_error_from_errno("getcwd");
2354 goto done;
2356 if (path_prefix[0]) {
2357 base = basename(path_prefix);
2358 if (base == NULL) {
2359 error = got_error_from_errno2("basename",
2360 path_prefix);
2361 goto done;
2363 } else {
2364 base = basename(repo_path);
2365 if (base == NULL) {
2366 error = got_error_from_errno2("basename",
2367 repo_path);
2368 goto done;
2371 dotgit = strstr(base, ".git");
2372 if (dotgit)
2373 *dotgit = '\0';
2374 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2375 error = got_error_from_errno("asprintf");
2376 free(cwd);
2377 goto done;
2379 free(cwd);
2380 } else if (argc == 2) {
2381 repo_path = realpath(argv[0], NULL);
2382 if (repo_path == NULL) {
2383 error = got_error_from_errno2("realpath", argv[0]);
2384 goto done;
2386 worktree_path = realpath(argv[1], NULL);
2387 if (worktree_path == NULL) {
2388 if (errno != ENOENT) {
2389 error = got_error_from_errno2("realpath",
2390 argv[1]);
2391 goto done;
2393 worktree_path = strdup(argv[1]);
2394 if (worktree_path == NULL) {
2395 error = got_error_from_errno("strdup");
2396 goto done;
2399 } else
2400 usage_checkout();
2402 got_path_strip_trailing_slashes(repo_path);
2403 got_path_strip_trailing_slashes(worktree_path);
2405 error = got_repo_open(&repo, repo_path, NULL);
2406 if (error != NULL)
2407 goto done;
2409 /* Pre-create work tree path for unveil(2) */
2410 error = got_path_mkdir(worktree_path);
2411 if (error) {
2412 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2413 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2414 goto done;
2415 if (!allow_nonempty &&
2416 !got_path_dir_is_empty(worktree_path)) {
2417 error = got_error_path(worktree_path,
2418 GOT_ERR_DIR_NOT_EMPTY);
2419 goto done;
2423 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2424 if (error)
2425 goto done;
2427 error = got_ref_open(&head_ref, repo, branch_name, 0);
2428 if (error != NULL)
2429 goto done;
2431 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2432 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2433 goto done;
2435 error = got_worktree_open(&worktree, worktree_path);
2436 if (error != NULL)
2437 goto done;
2439 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2440 path_prefix);
2441 if (error != NULL)
2442 goto done;
2443 if (!same_path_prefix) {
2444 error = got_error(GOT_ERR_PATH_PREFIX);
2445 goto done;
2448 if (commit_id_str) {
2449 struct got_object_id *commit_id;
2450 error = got_repo_match_object_id(&commit_id, NULL,
2451 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2452 if (error)
2453 goto done;
2454 error = check_linear_ancestry(commit_id,
2455 got_worktree_get_base_commit_id(worktree), 0, repo);
2456 if (error != NULL) {
2457 free(commit_id);
2458 if (error->code == GOT_ERR_ANCESTRY) {
2459 error = checkout_ancestry_error(
2460 head_ref, commit_id_str);
2462 goto done;
2464 error = check_same_branch(commit_id, head_ref, NULL, repo);
2465 if (error) {
2466 if (error->code == GOT_ERR_ANCESTRY) {
2467 error = checkout_ancestry_error(
2468 head_ref, commit_id_str);
2470 goto done;
2472 error = got_worktree_set_base_commit_id(worktree, repo,
2473 commit_id);
2474 free(commit_id);
2475 if (error)
2476 goto done;
2479 error = got_pathlist_append(&paths, "", NULL);
2480 if (error)
2481 goto done;
2482 cpa.worktree_path = worktree_path;
2483 cpa.had_base_commit_ref_error = 0;
2484 error = got_worktree_checkout_files(worktree, &paths, repo,
2485 checkout_progress, &cpa, check_cancelled, NULL);
2486 if (error != NULL)
2487 goto done;
2489 printf("Now shut up and hack\n");
2490 if (cpa.had_base_commit_ref_error)
2491 show_worktree_base_ref_warning();
2492 done:
2493 got_pathlist_free(&paths);
2494 free(commit_id_str);
2495 free(repo_path);
2496 free(worktree_path);
2497 return error;
2500 struct got_update_progress_arg {
2501 int did_something;
2502 int conflicts;
2503 int obstructed;
2504 int not_updated;
2507 void
2508 print_update_progress_stats(struct got_update_progress_arg *upa)
2510 if (!upa->did_something)
2511 return;
2513 if (upa->conflicts > 0)
2514 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2515 if (upa->obstructed > 0)
2516 printf("File paths obstructed by a non-regular file: %d\n",
2517 upa->obstructed);
2518 if (upa->not_updated > 0)
2519 printf("Files not updated because of existing merge "
2520 "conflicts: %d\n", upa->not_updated);
2523 __dead static void
2524 usage_update(void)
2526 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2527 getprogname());
2528 exit(1);
2531 static const struct got_error *
2532 update_progress(void *arg, unsigned char status, const char *path)
2534 struct got_update_progress_arg *upa = arg;
2536 if (status == GOT_STATUS_EXISTS ||
2537 status == GOT_STATUS_BASE_REF_ERR)
2538 return NULL;
2540 upa->did_something = 1;
2542 /* Base commit bump happens silently. */
2543 if (status == GOT_STATUS_BUMP_BASE)
2544 return NULL;
2546 if (status == GOT_STATUS_CONFLICT)
2547 upa->conflicts++;
2548 if (status == GOT_STATUS_OBSTRUCTED)
2549 upa->obstructed++;
2550 if (status == GOT_STATUS_CANNOT_UPDATE)
2551 upa->not_updated++;
2553 while (path[0] == '/')
2554 path++;
2555 printf("%c %s\n", status, path);
2556 return NULL;
2559 static const struct got_error *
2560 switch_head_ref(struct got_reference *head_ref,
2561 struct got_object_id *commit_id, struct got_worktree *worktree,
2562 struct got_repository *repo)
2564 const struct got_error *err = NULL;
2565 char *base_id_str;
2566 int ref_has_moved = 0;
2568 /* Trivial case: switching between two different references. */
2569 if (strcmp(got_ref_get_name(head_ref),
2570 got_worktree_get_head_ref_name(worktree)) != 0) {
2571 printf("Switching work tree from %s to %s\n",
2572 got_worktree_get_head_ref_name(worktree),
2573 got_ref_get_name(head_ref));
2574 return got_worktree_set_head_ref(worktree, head_ref);
2577 err = check_linear_ancestry(commit_id,
2578 got_worktree_get_base_commit_id(worktree), 0, repo);
2579 if (err) {
2580 if (err->code != GOT_ERR_ANCESTRY)
2581 return err;
2582 ref_has_moved = 1;
2584 if (!ref_has_moved)
2585 return NULL;
2587 /* Switching to a rebased branch with the same reference name. */
2588 err = got_object_id_str(&base_id_str,
2589 got_worktree_get_base_commit_id(worktree));
2590 if (err)
2591 return err;
2592 printf("Reference %s now points at a different branch\n",
2593 got_worktree_get_head_ref_name(worktree));
2594 printf("Switching work tree from %s to %s\n", base_id_str,
2595 got_worktree_get_head_ref_name(worktree));
2596 return NULL;
2599 static const struct got_error *
2600 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2602 const struct got_error *err;
2603 int in_progress;
2605 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2606 if (err)
2607 return err;
2608 if (in_progress)
2609 return got_error(GOT_ERR_REBASING);
2611 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2612 if (err)
2613 return err;
2614 if (in_progress)
2615 return got_error(GOT_ERR_HISTEDIT_BUSY);
2617 return NULL;
2620 static const struct got_error *
2621 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2622 char *argv[], struct got_worktree *worktree)
2624 const struct got_error *err = NULL;
2625 char *path;
2626 int i;
2628 if (argc == 0) {
2629 path = strdup("");
2630 if (path == NULL)
2631 return got_error_from_errno("strdup");
2632 return got_pathlist_append(paths, path, NULL);
2635 for (i = 0; i < argc; i++) {
2636 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2637 if (err)
2638 break;
2639 err = got_pathlist_append(paths, path, NULL);
2640 if (err) {
2641 free(path);
2642 break;
2646 return err;
2649 static const struct got_error *
2650 wrap_not_worktree_error(const struct got_error *orig_err,
2651 const char *cmdname, const char *path)
2653 const struct got_error *err;
2654 struct got_repository *repo;
2655 static char msg[512];
2657 err = got_repo_open(&repo, path, NULL);
2658 if (err)
2659 return orig_err;
2661 snprintf(msg, sizeof(msg),
2662 "'got %s' needs a work tree in addition to a git repository\n"
2663 "Work trees can be checked out from this Git repository with "
2664 "'got checkout'.\n"
2665 "The got(1) manual page contains more information.", cmdname);
2666 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2667 got_repo_close(repo);
2668 return err;
2671 static const struct got_error *
2672 cmd_update(int argc, char *argv[])
2674 const struct got_error *error = NULL;
2675 struct got_repository *repo = NULL;
2676 struct got_worktree *worktree = NULL;
2677 char *worktree_path = NULL;
2678 struct got_object_id *commit_id = NULL;
2679 char *commit_id_str = NULL;
2680 const char *branch_name = NULL;
2681 struct got_reference *head_ref = NULL;
2682 struct got_pathlist_head paths;
2683 struct got_pathlist_entry *pe;
2684 int ch;
2685 struct got_update_progress_arg upa;
2687 TAILQ_INIT(&paths);
2689 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2690 switch (ch) {
2691 case 'b':
2692 branch_name = optarg;
2693 break;
2694 case 'c':
2695 commit_id_str = strdup(optarg);
2696 if (commit_id_str == NULL)
2697 return got_error_from_errno("strdup");
2698 break;
2699 default:
2700 usage_update();
2701 /* NOTREACHED */
2705 argc -= optind;
2706 argv += optind;
2708 #ifndef PROFILE
2709 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2710 "unveil", NULL) == -1)
2711 err(1, "pledge");
2712 #endif
2713 worktree_path = getcwd(NULL, 0);
2714 if (worktree_path == NULL) {
2715 error = got_error_from_errno("getcwd");
2716 goto done;
2718 error = got_worktree_open(&worktree, worktree_path);
2719 if (error) {
2720 if (error->code == GOT_ERR_NOT_WORKTREE)
2721 error = wrap_not_worktree_error(error, "update",
2722 worktree_path);
2723 goto done;
2726 error = check_rebase_or_histedit_in_progress(worktree);
2727 if (error)
2728 goto done;
2730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2731 NULL);
2732 if (error != NULL)
2733 goto done;
2735 error = apply_unveil(got_repo_get_path(repo), 0,
2736 got_worktree_get_root_path(worktree));
2737 if (error)
2738 goto done;
2740 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2741 if (error)
2742 goto done;
2744 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2745 got_worktree_get_head_ref_name(worktree), 0);
2746 if (error != NULL)
2747 goto done;
2748 if (commit_id_str == NULL) {
2749 error = got_ref_resolve(&commit_id, repo, head_ref);
2750 if (error != NULL)
2751 goto done;
2752 error = got_object_id_str(&commit_id_str, commit_id);
2753 if (error != NULL)
2754 goto done;
2755 } else {
2756 error = got_repo_match_object_id(&commit_id, NULL,
2757 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2758 free(commit_id_str);
2759 commit_id_str = NULL;
2760 if (error)
2761 goto done;
2762 error = got_object_id_str(&commit_id_str, commit_id);
2763 if (error)
2764 goto done;
2767 if (branch_name) {
2768 struct got_object_id *head_commit_id;
2769 TAILQ_FOREACH(pe, &paths, entry) {
2770 if (pe->path_len == 0)
2771 continue;
2772 error = got_error_msg(GOT_ERR_BAD_PATH,
2773 "switching between branches requires that "
2774 "the entire work tree gets updated");
2775 goto done;
2777 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2778 if (error)
2779 goto done;
2780 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2781 repo);
2782 free(head_commit_id);
2783 if (error != NULL)
2784 goto done;
2785 error = check_same_branch(commit_id, head_ref, NULL, repo);
2786 if (error)
2787 goto done;
2788 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2789 if (error)
2790 goto done;
2791 } else {
2792 error = check_linear_ancestry(commit_id,
2793 got_worktree_get_base_commit_id(worktree), 0, repo);
2794 if (error != NULL) {
2795 if (error->code == GOT_ERR_ANCESTRY)
2796 error = got_error(GOT_ERR_BRANCH_MOVED);
2797 goto done;
2799 error = check_same_branch(commit_id, head_ref, NULL, repo);
2800 if (error)
2801 goto done;
2804 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2805 commit_id) != 0) {
2806 error = got_worktree_set_base_commit_id(worktree, repo,
2807 commit_id);
2808 if (error)
2809 goto done;
2812 memset(&upa, 0, sizeof(upa));
2813 error = got_worktree_checkout_files(worktree, &paths, repo,
2814 update_progress, &upa, check_cancelled, NULL);
2815 if (error != NULL)
2816 goto done;
2818 if (upa.did_something)
2819 printf("Updated to commit %s\n", commit_id_str);
2820 else
2821 printf("Already up-to-date\n");
2822 print_update_progress_stats(&upa);
2823 done:
2824 free(worktree_path);
2825 TAILQ_FOREACH(pe, &paths, entry)
2826 free((char *)pe->path);
2827 got_pathlist_free(&paths);
2828 free(commit_id);
2829 free(commit_id_str);
2830 return error;
2833 static const struct got_error *
2834 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2835 const char *path, int diff_context, int ignore_whitespace,
2836 struct got_repository *repo)
2838 const struct got_error *err = NULL;
2839 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2841 if (blob_id1) {
2842 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2843 if (err)
2844 goto done;
2847 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2848 if (err)
2849 goto done;
2851 while (path[0] == '/')
2852 path++;
2853 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2854 ignore_whitespace, stdout);
2855 done:
2856 if (blob1)
2857 got_object_blob_close(blob1);
2858 got_object_blob_close(blob2);
2859 return err;
2862 static const struct got_error *
2863 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2864 const char *path, int diff_context, int ignore_whitespace,
2865 struct got_repository *repo)
2867 const struct got_error *err = NULL;
2868 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2869 struct got_diff_blob_output_unidiff_arg arg;
2871 if (tree_id1) {
2872 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2873 if (err)
2874 goto done;
2877 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2878 if (err)
2879 goto done;
2881 arg.diff_context = diff_context;
2882 arg.ignore_whitespace = ignore_whitespace;
2883 arg.outfile = stdout;
2884 while (path[0] == '/')
2885 path++;
2886 err = got_diff_tree(tree1, tree2, path, path, repo,
2887 got_diff_blob_output_unidiff, &arg, 1);
2888 done:
2889 if (tree1)
2890 got_object_tree_close(tree1);
2891 if (tree2)
2892 got_object_tree_close(tree2);
2893 return err;
2896 static const struct got_error *
2897 get_changed_paths(struct got_pathlist_head *paths,
2898 struct got_commit_object *commit, struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2902 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2903 struct got_object_qid *qid;
2905 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2906 if (qid != NULL) {
2907 struct got_commit_object *pcommit;
2908 err = got_object_open_as_commit(&pcommit, repo,
2909 qid->id);
2910 if (err)
2911 return err;
2913 tree_id1 = got_object_commit_get_tree_id(pcommit);
2914 got_object_commit_close(pcommit);
2918 if (tree_id1) {
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2920 if (err)
2921 goto done;
2924 tree_id2 = got_object_commit_get_tree_id(commit);
2925 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2926 if (err)
2927 goto done;
2929 err = got_diff_tree(tree1, tree2, "", "", repo,
2930 got_diff_tree_collect_changed_paths, paths, 0);
2931 done:
2932 if (tree1)
2933 got_object_tree_close(tree1);
2934 if (tree2)
2935 got_object_tree_close(tree2);
2936 return err;
2939 static const struct got_error *
2940 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2941 const char *path, int diff_context, struct got_repository *repo)
2943 const struct got_error *err = NULL;
2944 struct got_commit_object *pcommit = NULL;
2945 char *id_str1 = NULL, *id_str2 = NULL;
2946 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2947 struct got_object_qid *qid;
2949 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2950 if (qid != NULL) {
2951 err = got_object_open_as_commit(&pcommit, repo,
2952 qid->id);
2953 if (err)
2954 return err;
2957 if (path && path[0] != '\0') {
2958 int obj_type;
2959 err = got_object_id_by_path(&obj_id2, repo, id, path);
2960 if (err)
2961 goto done;
2962 err = got_object_id_str(&id_str2, obj_id2);
2963 if (err) {
2964 free(obj_id2);
2965 goto done;
2967 if (pcommit) {
2968 err = got_object_id_by_path(&obj_id1, repo,
2969 qid->id, path);
2970 if (err) {
2971 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
2972 free(obj_id2);
2973 goto done;
2975 } else {
2976 err = got_object_id_str(&id_str1, obj_id1);
2977 if (err) {
2978 free(obj_id2);
2979 goto done;
2983 err = got_object_get_type(&obj_type, repo, obj_id2);
2984 if (err) {
2985 free(obj_id2);
2986 goto done;
2988 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2989 switch (obj_type) {
2990 case GOT_OBJ_TYPE_BLOB:
2991 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2992 0, repo);
2993 break;
2994 case GOT_OBJ_TYPE_TREE:
2995 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2996 0, repo);
2997 break;
2998 default:
2999 err = got_error(GOT_ERR_OBJ_TYPE);
3000 break;
3002 free(obj_id1);
3003 free(obj_id2);
3004 } else {
3005 obj_id2 = got_object_commit_get_tree_id(commit);
3006 err = got_object_id_str(&id_str2, obj_id2);
3007 if (err)
3008 goto done;
3009 obj_id1 = got_object_commit_get_tree_id(pcommit);
3010 err = got_object_id_str(&id_str1, obj_id1);
3011 if (err)
3012 goto done;
3013 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3014 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3016 done:
3017 free(id_str1);
3018 free(id_str2);
3019 if (pcommit)
3020 got_object_commit_close(pcommit);
3021 return err;
3024 static char *
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3028 char *p, *s;
3030 tm = gmtime_r(time, &mytm);
3031 if (tm == NULL)
3032 return NULL;
3033 s = asctime_r(tm, datebuf);
3034 if (s == NULL)
3035 return NULL;
3036 p = strchr(s, '\n');
3037 if (p)
3038 *p = '\0';
3039 return s;
3042 static const struct got_error *
3043 match_logmsg(int *have_match, struct got_object_id *id,
3044 struct got_commit_object *commit, regex_t *regex)
3046 const struct got_error *err = NULL;
3047 regmatch_t regmatch;
3048 char *id_str = NULL, *logmsg = NULL;
3050 *have_match = 0;
3052 err = got_object_id_str(&id_str, id);
3053 if (err)
3054 return err;
3056 err = got_object_commit_get_logmsg(&logmsg, commit);
3057 if (err)
3058 goto done;
3060 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3061 *have_match = 1;
3062 done:
3063 free(id_str);
3064 free(logmsg);
3065 return err;
3068 static void
3069 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3070 regex_t *regex)
3072 regmatch_t regmatch;
3073 struct got_pathlist_entry *pe;
3075 *have_match = 0;
3077 TAILQ_FOREACH(pe, changed_paths, entry) {
3078 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3079 *have_match = 1;
3080 break;
3085 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3087 static const struct got_error *
3088 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3089 struct got_repository *repo, const char *path,
3090 struct got_pathlist_head *changed_paths, int show_patch,
3091 int diff_context, struct got_reflist_head *refs)
3093 const struct got_error *err = NULL;
3094 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3095 char datebuf[26];
3096 time_t committer_time;
3097 const char *author, *committer;
3098 char *refs_str = NULL;
3099 struct got_reflist_entry *re;
3101 SIMPLEQ_FOREACH(re, refs, entry) {
3102 char *s;
3103 const char *name;
3104 struct got_tag_object *tag = NULL;
3105 int cmp;
3107 name = got_ref_get_name(re->ref);
3108 if (strcmp(name, GOT_REF_HEAD) == 0)
3109 continue;
3110 if (strncmp(name, "refs/", 5) == 0)
3111 name += 5;
3112 if (strncmp(name, "got/", 4) == 0)
3113 continue;
3114 if (strncmp(name, "heads/", 6) == 0)
3115 name += 6;
3116 if (strncmp(name, "remotes/", 8) == 0) {
3117 name += 8;
3118 s = strstr(name, "/" GOT_REF_HEAD);
3119 if (s != NULL && s[strlen(s)] == '\0')
3120 continue;
3122 if (strncmp(name, "tags/", 5) == 0) {
3123 err = got_object_open_as_tag(&tag, repo, re->id);
3124 if (err) {
3125 if (err->code != GOT_ERR_OBJ_TYPE)
3126 return err;
3127 /* Ref points at something other than a tag. */
3128 err = NULL;
3129 tag = NULL;
3132 cmp = got_object_id_cmp(tag ?
3133 got_object_tag_get_object_id(tag) : re->id, id);
3134 if (tag)
3135 got_object_tag_close(tag);
3136 if (cmp != 0)
3137 continue;
3138 s = refs_str;
3139 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3140 name) == -1) {
3141 err = got_error_from_errno("asprintf");
3142 free(s);
3143 return err;
3145 free(s);
3147 err = got_object_id_str(&id_str, id);
3148 if (err)
3149 return err;
3151 printf(GOT_COMMIT_SEP_STR);
3152 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3153 refs_str ? refs_str : "", refs_str ? ")" : "");
3154 free(id_str);
3155 id_str = NULL;
3156 free(refs_str);
3157 refs_str = NULL;
3158 printf("from: %s\n", got_object_commit_get_author(commit));
3159 committer_time = got_object_commit_get_committer_time(commit);
3160 datestr = get_datestr(&committer_time, datebuf);
3161 if (datestr)
3162 printf("date: %s UTC\n", datestr);
3163 author = got_object_commit_get_author(commit);
3164 committer = got_object_commit_get_committer(commit);
3165 if (strcmp(author, committer) != 0)
3166 printf("via: %s\n", committer);
3167 if (got_object_commit_get_nparents(commit) > 1) {
3168 const struct got_object_id_queue *parent_ids;
3169 struct got_object_qid *qid;
3170 int n = 1;
3171 parent_ids = got_object_commit_get_parent_ids(commit);
3172 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3173 err = got_object_id_str(&id_str, qid->id);
3174 if (err)
3175 return err;
3176 printf("parent %d: %s\n", n++, id_str);
3177 free(id_str);
3181 err = got_object_commit_get_logmsg(&logmsg0, commit);
3182 if (err)
3183 return err;
3185 logmsg = logmsg0;
3186 do {
3187 line = strsep(&logmsg, "\n");
3188 if (line)
3189 printf(" %s\n", line);
3190 } while (line);
3191 free(logmsg0);
3193 if (changed_paths) {
3194 struct got_pathlist_entry *pe;
3195 TAILQ_FOREACH(pe, changed_paths, entry) {
3196 struct got_diff_changed_path *cp = pe->data;
3197 printf(" %c %s\n", cp->status, pe->path);
3199 printf("\n");
3201 if (show_patch) {
3202 err = print_patch(commit, id, path, diff_context, repo);
3203 if (err == 0)
3204 printf("\n");
3207 if (fflush(stdout) != 0 && err == NULL)
3208 err = got_error_from_errno("fflush");
3209 return err;
3212 static const struct got_error *
3213 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3214 struct got_repository *repo, const char *path, int show_changed_paths,
3215 int show_patch, const char *search_pattern, int diff_context, int limit,
3216 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3218 const struct got_error *err;
3219 struct got_commit_graph *graph;
3220 regex_t regex;
3221 int have_match;
3222 struct got_object_id_queue reversed_commits;
3223 struct got_object_qid *qid;
3224 struct got_commit_object *commit;
3225 struct got_pathlist_head changed_paths;
3226 struct got_pathlist_entry *pe;
3228 SIMPLEQ_INIT(&reversed_commits);
3229 TAILQ_INIT(&changed_paths);
3231 if (search_pattern && regcomp(&regex, search_pattern,
3232 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3233 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3235 err = got_commit_graph_open(&graph, path, !log_branches);
3236 if (err)
3237 return err;
3238 err = got_commit_graph_iter_start(graph, root_id, repo,
3239 check_cancelled, NULL);
3240 if (err)
3241 goto done;
3242 for (;;) {
3243 struct got_object_id *id;
3245 if (sigint_received || sigpipe_received)
3246 break;
3248 err = got_commit_graph_iter_next(&id, graph, repo,
3249 check_cancelled, NULL);
3250 if (err) {
3251 if (err->code == GOT_ERR_ITER_COMPLETED)
3252 err = NULL;
3253 break;
3255 if (id == NULL)
3256 break;
3258 err = got_object_open_as_commit(&commit, repo, id);
3259 if (err)
3260 break;
3262 if (show_changed_paths && !reverse_display_order) {
3263 err = get_changed_paths(&changed_paths, commit, repo);
3264 if (err)
3265 break;
3268 if (search_pattern) {
3269 err = match_logmsg(&have_match, id, commit, &regex);
3270 if (err) {
3271 got_object_commit_close(commit);
3272 break;
3274 if (have_match == 0 && show_changed_paths)
3275 match_changed_paths(&have_match,
3276 &changed_paths, &regex);
3277 if (have_match == 0) {
3278 got_object_commit_close(commit);
3279 TAILQ_FOREACH(pe, &changed_paths, entry) {
3280 free((char *)pe->path);
3281 free(pe->data);
3283 got_pathlist_free(&changed_paths);
3284 continue;
3288 if (reverse_display_order) {
3289 err = got_object_qid_alloc(&qid, id);
3290 if (err)
3291 break;
3292 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3293 got_object_commit_close(commit);
3294 } else {
3295 err = print_commit(commit, id, repo, path,
3296 show_changed_paths ? &changed_paths : NULL,
3297 show_patch, diff_context, refs);
3298 got_object_commit_close(commit);
3299 if (err)
3300 break;
3302 if ((limit && --limit == 0) ||
3303 (end_id && got_object_id_cmp(id, end_id) == 0))
3304 break;
3306 TAILQ_FOREACH(pe, &changed_paths, entry) {
3307 free((char *)pe->path);
3308 free(pe->data);
3310 got_pathlist_free(&changed_paths);
3312 if (reverse_display_order) {
3313 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3314 err = got_object_open_as_commit(&commit, repo, qid->id);
3315 if (err)
3316 break;
3317 if (show_changed_paths) {
3318 err = get_changed_paths(&changed_paths,
3319 commit, repo);
3320 if (err)
3321 break;
3323 err = print_commit(commit, qid->id, repo, path,
3324 show_changed_paths ? &changed_paths : NULL,
3325 show_patch, diff_context, refs);
3326 got_object_commit_close(commit);
3327 if (err)
3328 break;
3329 TAILQ_FOREACH(pe, &changed_paths, entry) {
3330 free((char *)pe->path);
3331 free(pe->data);
3333 got_pathlist_free(&changed_paths);
3336 done:
3337 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3338 qid = SIMPLEQ_FIRST(&reversed_commits);
3339 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3340 got_object_qid_free(qid);
3342 TAILQ_FOREACH(pe, &changed_paths, entry) {
3343 free((char *)pe->path);
3344 free(pe->data);
3346 got_pathlist_free(&changed_paths);
3347 if (search_pattern)
3348 regfree(&regex);
3349 got_commit_graph_close(graph);
3350 return err;
3353 __dead static void
3354 usage_log(void)
3356 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3357 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3358 "[-R] [path]\n", getprogname());
3359 exit(1);
3362 static int
3363 get_default_log_limit(void)
3365 const char *got_default_log_limit;
3366 long long n;
3367 const char *errstr;
3369 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3370 if (got_default_log_limit == NULL)
3371 return 0;
3372 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3373 if (errstr != NULL)
3374 return 0;
3375 return n;
3378 static const struct got_error *
3379 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3380 struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 struct got_reference *ref;
3385 *id = NULL;
3387 err = got_ref_open(&ref, repo, commit_arg, 0);
3388 if (err == NULL) {
3389 int obj_type;
3390 err = got_ref_resolve(id, repo, ref);
3391 got_ref_close(ref);
3392 if (err)
3393 return err;
3394 err = got_object_get_type(&obj_type, repo, *id);
3395 if (err)
3396 return err;
3397 if (obj_type == GOT_OBJ_TYPE_TAG) {
3398 struct got_tag_object *tag;
3399 err = got_object_open_as_tag(&tag, repo, *id);
3400 if (err)
3401 return err;
3402 if (got_object_tag_get_object_type(tag) !=
3403 GOT_OBJ_TYPE_COMMIT) {
3404 got_object_tag_close(tag);
3405 return got_error(GOT_ERR_OBJ_TYPE);
3407 free(*id);
3408 *id = got_object_id_dup(
3409 got_object_tag_get_object_id(tag));
3410 if (*id == NULL)
3411 err = got_error_from_errno(
3412 "got_object_id_dup");
3413 got_object_tag_close(tag);
3414 if (err)
3415 return err;
3416 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3417 return got_error(GOT_ERR_OBJ_TYPE);
3418 } else {
3419 err = got_repo_match_object_id_prefix(id, commit_arg,
3420 GOT_OBJ_TYPE_COMMIT, repo);
3423 return err;
3426 static const struct got_error *
3427 cmd_log(int argc, char *argv[])
3429 const struct got_error *error;
3430 struct got_repository *repo = NULL;
3431 struct got_worktree *worktree = NULL;
3432 struct got_object_id *start_id = NULL, *end_id = NULL;
3433 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3434 const char *start_commit = NULL, *end_commit = NULL;
3435 const char *search_pattern = NULL;
3436 int diff_context = -1, ch;
3437 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3438 int reverse_display_order = 0;
3439 const char *errstr;
3440 struct got_reflist_head refs;
3442 SIMPLEQ_INIT(&refs);
3444 #ifndef PROFILE
3445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3446 NULL)
3447 == -1)
3448 err(1, "pledge");
3449 #endif
3451 limit = get_default_log_limit();
3453 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3454 switch (ch) {
3455 case 'p':
3456 show_patch = 1;
3457 break;
3458 case 'P':
3459 show_changed_paths = 1;
3460 break;
3461 case 'c':
3462 start_commit = optarg;
3463 break;
3464 case 'C':
3465 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3466 &errstr);
3467 if (errstr != NULL)
3468 err(1, "-C option %s", errstr);
3469 break;
3470 case 'l':
3471 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3472 if (errstr != NULL)
3473 err(1, "-l option %s", errstr);
3474 break;
3475 case 'b':
3476 log_branches = 1;
3477 break;
3478 case 'r':
3479 repo_path = realpath(optarg, NULL);
3480 if (repo_path == NULL)
3481 return got_error_from_errno2("realpath",
3482 optarg);
3483 got_path_strip_trailing_slashes(repo_path);
3484 break;
3485 case 'R':
3486 reverse_display_order = 1;
3487 break;
3488 case 's':
3489 search_pattern = optarg;
3490 break;
3491 case 'x':
3492 end_commit = optarg;
3493 break;
3494 default:
3495 usage_log();
3496 /* NOTREACHED */
3500 argc -= optind;
3501 argv += optind;
3503 if (diff_context == -1)
3504 diff_context = 3;
3505 else if (!show_patch)
3506 errx(1, "-C reguires -p");
3508 cwd = getcwd(NULL, 0);
3509 if (cwd == NULL) {
3510 error = got_error_from_errno("getcwd");
3511 goto done;
3514 if (repo_path == NULL) {
3515 error = got_worktree_open(&worktree, cwd);
3516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3517 goto done;
3518 error = NULL;
3521 if (argc == 0) {
3522 path = strdup("");
3523 if (path == NULL) {
3524 error = got_error_from_errno("strdup");
3525 goto done;
3527 } else if (argc == 1) {
3528 if (worktree) {
3529 error = got_worktree_resolve_path(&path, worktree,
3530 argv[0]);
3531 if (error)
3532 goto done;
3533 } else {
3534 path = strdup(argv[0]);
3535 if (path == NULL) {
3536 error = got_error_from_errno("strdup");
3537 goto done;
3540 } else
3541 usage_log();
3543 if (repo_path == NULL) {
3544 repo_path = worktree ?
3545 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3547 if (repo_path == NULL) {
3548 error = got_error_from_errno("strdup");
3549 goto done;
3552 error = got_repo_open(&repo, repo_path, NULL);
3553 if (error != NULL)
3554 goto done;
3556 error = apply_unveil(got_repo_get_path(repo), 1,
3557 worktree ? got_worktree_get_root_path(worktree) : NULL);
3558 if (error)
3559 goto done;
3561 if (start_commit == NULL) {
3562 struct got_reference *head_ref;
3563 struct got_commit_object *commit = NULL;
3564 error = got_ref_open(&head_ref, repo,
3565 worktree ? got_worktree_get_head_ref_name(worktree)
3566 : GOT_REF_HEAD, 0);
3567 if (error != NULL)
3568 goto done;
3569 error = got_ref_resolve(&start_id, repo, head_ref);
3570 got_ref_close(head_ref);
3571 if (error != NULL)
3572 goto done;
3573 error = got_object_open_as_commit(&commit, repo,
3574 start_id);
3575 if (error != NULL)
3576 goto done;
3577 got_object_commit_close(commit);
3578 } else {
3579 error = resolve_commit_arg(&start_id, start_commit, repo);
3580 if (error != NULL)
3581 goto done;
3583 if (end_commit != NULL) {
3584 error = resolve_commit_arg(&end_id, end_commit, repo);
3585 if (error != NULL)
3586 goto done;
3589 if (worktree) {
3590 const char *prefix = got_worktree_get_path_prefix(worktree);
3591 char *p;
3592 if (asprintf(&p, "%s%s%s", prefix,
3593 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3594 error = got_error_from_errno("asprintf");
3595 goto done;
3597 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3598 free(p);
3599 } else
3600 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3601 if (error != NULL)
3602 goto done;
3603 if (in_repo_path) {
3604 free(path);
3605 path = in_repo_path;
3608 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3609 if (error)
3610 goto done;
3612 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3613 show_patch, search_pattern, diff_context, limit, log_branches,
3614 reverse_display_order, &refs);
3615 done:
3616 free(path);
3617 free(repo_path);
3618 free(cwd);
3619 if (worktree)
3620 got_worktree_close(worktree);
3621 if (repo) {
3622 const struct got_error *repo_error;
3623 repo_error = got_repo_close(repo);
3624 if (error == NULL)
3625 error = repo_error;
3627 got_ref_list_free(&refs);
3628 return error;
3631 __dead static void
3632 usage_diff(void)
3634 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3635 "[-w] [object1 object2 | path]\n", getprogname());
3636 exit(1);
3639 struct print_diff_arg {
3640 struct got_repository *repo;
3641 struct got_worktree *worktree;
3642 int diff_context;
3643 const char *id_str;
3644 int header_shown;
3645 int diff_staged;
3646 int ignore_whitespace;
3650 * Create a file which contains the target path of a symlink so we can feed
3651 * it as content to the diff engine.
3653 static const struct got_error *
3654 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3655 const char *abspath)
3657 const struct got_error *err = NULL;
3658 char target_path[PATH_MAX];
3659 ssize_t target_len, outlen;
3661 *fd = -1;
3663 if (dirfd != -1) {
3664 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3665 if (target_len == -1)
3666 return got_error_from_errno2("readlinkat", abspath);
3667 } else {
3668 target_len = readlink(abspath, target_path, PATH_MAX);
3669 if (target_len == -1)
3670 return got_error_from_errno2("readlink", abspath);
3673 *fd = got_opentempfd();
3674 if (*fd == -1)
3675 return got_error_from_errno("got_opentempfd");
3677 outlen = write(*fd, target_path, target_len);
3678 if (outlen == -1) {
3679 err = got_error_from_errno("got_opentempfd");
3680 goto done;
3683 if (lseek(*fd, 0, SEEK_SET) == -1) {
3684 err = got_error_from_errno2("lseek", abspath);
3685 goto done;
3687 done:
3688 if (err) {
3689 close(*fd);
3690 *fd = -1;
3692 return err;
3695 static const struct got_error *
3696 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3697 const char *path, struct got_object_id *blob_id,
3698 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3699 int dirfd, const char *de_name)
3701 struct print_diff_arg *a = arg;
3702 const struct got_error *err = NULL;
3703 struct got_blob_object *blob1 = NULL;
3704 int fd = -1;
3705 FILE *f2 = NULL;
3706 char *abspath = NULL, *label1 = NULL;
3707 struct stat sb;
3709 if (a->diff_staged) {
3710 if (staged_status != GOT_STATUS_MODIFY &&
3711 staged_status != GOT_STATUS_ADD &&
3712 staged_status != GOT_STATUS_DELETE)
3713 return NULL;
3714 } else {
3715 if (staged_status == GOT_STATUS_DELETE)
3716 return NULL;
3717 if (status == GOT_STATUS_NONEXISTENT)
3718 return got_error_set_errno(ENOENT, path);
3719 if (status != GOT_STATUS_MODIFY &&
3720 status != GOT_STATUS_ADD &&
3721 status != GOT_STATUS_DELETE &&
3722 status != GOT_STATUS_CONFLICT)
3723 return NULL;
3726 if (!a->header_shown) {
3727 printf("diff %s %s%s\n", a->id_str,
3728 got_worktree_get_root_path(a->worktree),
3729 a->diff_staged ? " (staged changes)" : "");
3730 a->header_shown = 1;
3733 if (a->diff_staged) {
3734 const char *label1 = NULL, *label2 = NULL;
3735 switch (staged_status) {
3736 case GOT_STATUS_MODIFY:
3737 label1 = path;
3738 label2 = path;
3739 break;
3740 case GOT_STATUS_ADD:
3741 label2 = path;
3742 break;
3743 case GOT_STATUS_DELETE:
3744 label1 = path;
3745 break;
3746 default:
3747 return got_error(GOT_ERR_FILE_STATUS);
3749 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3750 label1, label2, a->diff_context, a->ignore_whitespace,
3751 a->repo, stdout);
3754 if (staged_status == GOT_STATUS_ADD ||
3755 staged_status == GOT_STATUS_MODIFY) {
3756 char *id_str;
3757 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3758 8192);
3759 if (err)
3760 goto done;
3761 err = got_object_id_str(&id_str, staged_blob_id);
3762 if (err)
3763 goto done;
3764 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3765 err = got_error_from_errno("asprintf");
3766 free(id_str);
3767 goto done;
3769 free(id_str);
3770 } else if (status != GOT_STATUS_ADD) {
3771 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3772 if (err)
3773 goto done;
3776 if (status != GOT_STATUS_DELETE) {
3777 if (asprintf(&abspath, "%s/%s",
3778 got_worktree_get_root_path(a->worktree), path) == -1) {
3779 err = got_error_from_errno("asprintf");
3780 goto done;
3783 if (dirfd != -1) {
3784 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3785 if (fd == -1) {
3786 if (errno != ELOOP) {
3787 err = got_error_from_errno2("openat",
3788 abspath);
3789 goto done;
3791 err = get_symlink_target_file(&fd, dirfd,
3792 de_name, abspath);
3793 if (err)
3794 goto done;
3796 } else {
3797 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3798 if (fd == -1) {
3799 if (errno != ELOOP) {
3800 err = got_error_from_errno2("open",
3801 abspath);
3802 goto done;
3804 err = get_symlink_target_file(&fd, dirfd,
3805 de_name, abspath);
3806 if (err)
3807 goto done;
3810 if (fstat(fd, &sb) == -1) {
3811 err = got_error_from_errno2("fstat", abspath);
3812 goto done;
3814 f2 = fdopen(fd, "r");
3815 if (f2 == NULL) {
3816 err = got_error_from_errno2("fdopen", abspath);
3817 goto done;
3819 fd = -1;
3820 } else
3821 sb.st_size = 0;
3823 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3824 a->diff_context, a->ignore_whitespace, stdout);
3825 done:
3826 if (blob1)
3827 got_object_blob_close(blob1);
3828 if (f2 && fclose(f2) == EOF && err == NULL)
3829 err = got_error_from_errno("fclose");
3830 if (fd != -1 && close(fd) == -1 && err == NULL)
3831 err = got_error_from_errno("close");
3832 free(abspath);
3833 return err;
3836 static const struct got_error *
3837 cmd_diff(int argc, char *argv[])
3839 const struct got_error *error;
3840 struct got_repository *repo = NULL;
3841 struct got_worktree *worktree = NULL;
3842 char *cwd = NULL, *repo_path = NULL;
3843 struct got_object_id *id1 = NULL, *id2 = NULL;
3844 const char *id_str1 = NULL, *id_str2 = NULL;
3845 char *label1 = NULL, *label2 = NULL;
3846 int type1, type2;
3847 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3848 const char *errstr;
3849 char *path = NULL;
3851 #ifndef PROFILE
3852 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3853 NULL) == -1)
3854 err(1, "pledge");
3855 #endif
3857 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3858 switch (ch) {
3859 case 'C':
3860 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3861 &errstr);
3862 if (errstr != NULL)
3863 err(1, "-C option %s", errstr);
3864 break;
3865 case 'r':
3866 repo_path = realpath(optarg, NULL);
3867 if (repo_path == NULL)
3868 return got_error_from_errno2("realpath",
3869 optarg);
3870 got_path_strip_trailing_slashes(repo_path);
3871 break;
3872 case 's':
3873 diff_staged = 1;
3874 break;
3875 case 'w':
3876 ignore_whitespace = 1;
3877 break;
3878 default:
3879 usage_diff();
3880 /* NOTREACHED */
3884 argc -= optind;
3885 argv += optind;
3887 cwd = getcwd(NULL, 0);
3888 if (cwd == NULL) {
3889 error = got_error_from_errno("getcwd");
3890 goto done;
3892 if (argc <= 1) {
3893 if (repo_path)
3894 errx(1,
3895 "-r option can't be used when diffing a work tree");
3896 error = got_worktree_open(&worktree, cwd);
3897 if (error) {
3898 if (error->code == GOT_ERR_NOT_WORKTREE)
3899 error = wrap_not_worktree_error(error, "diff",
3900 cwd);
3901 goto done;
3903 repo_path = strdup(got_worktree_get_repo_path(worktree));
3904 if (repo_path == NULL) {
3905 error = got_error_from_errno("strdup");
3906 goto done;
3908 if (argc == 1) {
3909 error = got_worktree_resolve_path(&path, worktree,
3910 argv[0]);
3911 if (error)
3912 goto done;
3913 } else {
3914 path = strdup("");
3915 if (path == NULL) {
3916 error = got_error_from_errno("strdup");
3917 goto done;
3920 } else if (argc == 2) {
3921 if (diff_staged)
3922 errx(1, "-s option can't be used when diffing "
3923 "objects in repository");
3924 id_str1 = argv[0];
3925 id_str2 = argv[1];
3926 if (repo_path == NULL) {
3927 error = got_worktree_open(&worktree, cwd);
3928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3929 goto done;
3930 if (worktree) {
3931 repo_path = strdup(
3932 got_worktree_get_repo_path(worktree));
3933 if (repo_path == NULL) {
3934 error = got_error_from_errno("strdup");
3935 goto done;
3937 } else {
3938 repo_path = strdup(cwd);
3939 if (repo_path == NULL) {
3940 error = got_error_from_errno("strdup");
3941 goto done;
3945 } else
3946 usage_diff();
3948 error = got_repo_open(&repo, repo_path, NULL);
3949 free(repo_path);
3950 if (error != NULL)
3951 goto done;
3953 error = apply_unveil(got_repo_get_path(repo), 1,
3954 worktree ? got_worktree_get_root_path(worktree) : NULL);
3955 if (error)
3956 goto done;
3958 if (argc <= 1) {
3959 struct print_diff_arg arg;
3960 struct got_pathlist_head paths;
3961 char *id_str;
3963 TAILQ_INIT(&paths);
3965 error = got_object_id_str(&id_str,
3966 got_worktree_get_base_commit_id(worktree));
3967 if (error)
3968 goto done;
3969 arg.repo = repo;
3970 arg.worktree = worktree;
3971 arg.diff_context = diff_context;
3972 arg.id_str = id_str;
3973 arg.header_shown = 0;
3974 arg.diff_staged = diff_staged;
3975 arg.ignore_whitespace = ignore_whitespace;
3977 error = got_pathlist_append(&paths, path, NULL);
3978 if (error)
3979 goto done;
3981 error = got_worktree_status(worktree, &paths, repo, print_diff,
3982 &arg, check_cancelled, NULL);
3983 free(id_str);
3984 got_pathlist_free(&paths);
3985 goto done;
3988 error = got_repo_match_object_id(&id1, &label1, id_str1,
3989 GOT_OBJ_TYPE_ANY, 1, repo);
3990 if (error)
3991 goto done;
3993 error = got_repo_match_object_id(&id2, &label2, id_str2,
3994 GOT_OBJ_TYPE_ANY, 1, repo);
3995 if (error)
3996 goto done;
3998 error = got_object_get_type(&type1, repo, id1);
3999 if (error)
4000 goto done;
4002 error = got_object_get_type(&type2, repo, id2);
4003 if (error)
4004 goto done;
4006 if (type1 != type2) {
4007 error = got_error(GOT_ERR_OBJ_TYPE);
4008 goto done;
4011 switch (type1) {
4012 case GOT_OBJ_TYPE_BLOB:
4013 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4014 diff_context, ignore_whitespace, repo, stdout);
4015 break;
4016 case GOT_OBJ_TYPE_TREE:
4017 error = got_diff_objects_as_trees(id1, id2, "", "",
4018 diff_context, ignore_whitespace, repo, stdout);
4019 break;
4020 case GOT_OBJ_TYPE_COMMIT:
4021 printf("diff %s %s\n", label1, label2);
4022 error = got_diff_objects_as_commits(id1, id2, diff_context,
4023 ignore_whitespace, repo, stdout);
4024 break;
4025 default:
4026 error = got_error(GOT_ERR_OBJ_TYPE);
4028 done:
4029 free(label1);
4030 free(label2);
4031 free(id1);
4032 free(id2);
4033 free(path);
4034 if (worktree)
4035 got_worktree_close(worktree);
4036 if (repo) {
4037 const struct got_error *repo_error;
4038 repo_error = got_repo_close(repo);
4039 if (error == NULL)
4040 error = repo_error;
4042 return error;
4045 __dead static void
4046 usage_blame(void)
4048 fprintf(stderr,
4049 "usage: %s blame [-c commit] [-r repository-path] path\n",
4050 getprogname());
4051 exit(1);
4054 struct blame_line {
4055 int annotated;
4056 char *id_str;
4057 char *committer;
4058 char datebuf[11]; /* YYYY-MM-DD + NUL */
4061 struct blame_cb_args {
4062 struct blame_line *lines;
4063 int nlines;
4064 int nlines_prec;
4065 int lineno_cur;
4066 off_t *line_offsets;
4067 FILE *f;
4068 struct got_repository *repo;
4071 static const struct got_error *
4072 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4074 const struct got_error *err = NULL;
4075 struct blame_cb_args *a = arg;
4076 struct blame_line *bline;
4077 char *line = NULL;
4078 size_t linesize = 0;
4079 struct got_commit_object *commit = NULL;
4080 off_t offset;
4081 struct tm tm;
4082 time_t committer_time;
4084 if (nlines != a->nlines ||
4085 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4086 return got_error(GOT_ERR_RANGE);
4088 if (sigint_received)
4089 return got_error(GOT_ERR_ITER_COMPLETED);
4091 if (lineno == -1)
4092 return NULL; /* no change in this commit */
4094 /* Annotate this line. */
4095 bline = &a->lines[lineno - 1];
4096 if (bline->annotated)
4097 return NULL;
4098 err = got_object_id_str(&bline->id_str, id);
4099 if (err)
4100 return err;
4102 err = got_object_open_as_commit(&commit, a->repo, id);
4103 if (err)
4104 goto done;
4106 bline->committer = strdup(got_object_commit_get_committer(commit));
4107 if (bline->committer == NULL) {
4108 err = got_error_from_errno("strdup");
4109 goto done;
4112 committer_time = got_object_commit_get_committer_time(commit);
4113 if (localtime_r(&committer_time, &tm) == NULL)
4114 return got_error_from_errno("localtime_r");
4115 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4116 &tm) >= sizeof(bline->datebuf)) {
4117 err = got_error(GOT_ERR_NO_SPACE);
4118 goto done;
4120 bline->annotated = 1;
4122 /* Print lines annotated so far. */
4123 bline = &a->lines[a->lineno_cur - 1];
4124 if (!bline->annotated)
4125 goto done;
4127 offset = a->line_offsets[a->lineno_cur - 1];
4128 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4129 err = got_error_from_errno("fseeko");
4130 goto done;
4133 while (bline->annotated) {
4134 char *smallerthan, *at, *nl, *committer;
4135 size_t len;
4137 if (getline(&line, &linesize, a->f) == -1) {
4138 if (ferror(a->f))
4139 err = got_error_from_errno("getline");
4140 break;
4143 committer = bline->committer;
4144 smallerthan = strchr(committer, '<');
4145 if (smallerthan && smallerthan[1] != '\0')
4146 committer = smallerthan + 1;
4147 at = strchr(committer, '@');
4148 if (at)
4149 *at = '\0';
4150 len = strlen(committer);
4151 if (len >= 9)
4152 committer[8] = '\0';
4154 nl = strchr(line, '\n');
4155 if (nl)
4156 *nl = '\0';
4157 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4158 bline->id_str, bline->datebuf, committer, line);
4160 a->lineno_cur++;
4161 bline = &a->lines[a->lineno_cur - 1];
4163 done:
4164 if (commit)
4165 got_object_commit_close(commit);
4166 free(line);
4167 return err;
4170 static const struct got_error *
4171 cmd_blame(int argc, char *argv[])
4173 const struct got_error *error;
4174 struct got_repository *repo = NULL;
4175 struct got_worktree *worktree = NULL;
4176 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4177 char *link_target = NULL;
4178 struct got_object_id *obj_id = NULL;
4179 struct got_object_id *commit_id = NULL;
4180 struct got_blob_object *blob = NULL;
4181 char *commit_id_str = NULL;
4182 struct blame_cb_args bca;
4183 int ch, obj_type, i;
4184 size_t filesize;
4186 memset(&bca, 0, sizeof(bca));
4188 #ifndef PROFILE
4189 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4190 NULL) == -1)
4191 err(1, "pledge");
4192 #endif
4194 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4195 switch (ch) {
4196 case 'c':
4197 commit_id_str = optarg;
4198 break;
4199 case 'r':
4200 repo_path = realpath(optarg, NULL);
4201 if (repo_path == NULL)
4202 return got_error_from_errno2("realpath",
4203 optarg);
4204 got_path_strip_trailing_slashes(repo_path);
4205 break;
4206 default:
4207 usage_blame();
4208 /* NOTREACHED */
4212 argc -= optind;
4213 argv += optind;
4215 if (argc == 1)
4216 path = argv[0];
4217 else
4218 usage_blame();
4220 cwd = getcwd(NULL, 0);
4221 if (cwd == NULL) {
4222 error = got_error_from_errno("getcwd");
4223 goto done;
4225 if (repo_path == NULL) {
4226 error = got_worktree_open(&worktree, cwd);
4227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4228 goto done;
4229 else
4230 error = NULL;
4231 if (worktree) {
4232 repo_path =
4233 strdup(got_worktree_get_repo_path(worktree));
4234 if (repo_path == NULL) {
4235 error = got_error_from_errno("strdup");
4236 if (error)
4237 goto done;
4239 } else {
4240 repo_path = strdup(cwd);
4241 if (repo_path == NULL) {
4242 error = got_error_from_errno("strdup");
4243 goto done;
4248 error = got_repo_open(&repo, repo_path, NULL);
4249 if (error != NULL)
4250 goto done;
4252 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4253 if (error)
4254 goto done;
4256 if (worktree) {
4257 const char *prefix = got_worktree_get_path_prefix(worktree);
4258 char *p, *worktree_subdir = cwd +
4259 strlen(got_worktree_get_root_path(worktree));
4260 if (asprintf(&p, "%s%s%s%s%s",
4261 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4262 worktree_subdir, worktree_subdir[0] ? "/" : "",
4263 path) == -1) {
4264 error = got_error_from_errno("asprintf");
4265 goto done;
4267 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4268 free(p);
4269 } else {
4270 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4272 if (error)
4273 goto done;
4275 if (commit_id_str == NULL) {
4276 struct got_reference *head_ref;
4277 error = got_ref_open(&head_ref, repo, worktree ?
4278 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4279 if (error != NULL)
4280 goto done;
4281 error = got_ref_resolve(&commit_id, repo, head_ref);
4282 got_ref_close(head_ref);
4283 if (error != NULL)
4284 goto done;
4285 } else {
4286 error = got_repo_match_object_id(&commit_id, NULL,
4287 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4288 if (error)
4289 goto done;
4292 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4293 commit_id, repo);
4294 if (error)
4295 goto done;
4297 error = got_object_id_by_path(&obj_id, repo, commit_id,
4298 link_target ? link_target : in_repo_path);
4299 if (error)
4300 goto done;
4302 error = got_object_get_type(&obj_type, repo, obj_id);
4303 if (error)
4304 goto done;
4306 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4307 error = got_error_path(link_target ? link_target : in_repo_path,
4308 GOT_ERR_OBJ_TYPE);
4309 goto done;
4312 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4313 if (error)
4314 goto done;
4315 bca.f = got_opentemp();
4316 if (bca.f == NULL) {
4317 error = got_error_from_errno("got_opentemp");
4318 goto done;
4320 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4321 &bca.line_offsets, bca.f, blob);
4322 if (error || bca.nlines == 0)
4323 goto done;
4325 /* Don't include \n at EOF in the blame line count. */
4326 if (bca.line_offsets[bca.nlines - 1] == filesize)
4327 bca.nlines--;
4329 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4330 if (bca.lines == NULL) {
4331 error = got_error_from_errno("calloc");
4332 goto done;
4334 bca.lineno_cur = 1;
4335 bca.nlines_prec = 0;
4336 i = bca.nlines;
4337 while (i > 0) {
4338 i /= 10;
4339 bca.nlines_prec++;
4341 bca.repo = repo;
4343 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4344 repo, blame_cb, &bca, check_cancelled, NULL);
4345 done:
4346 free(in_repo_path);
4347 free(link_target);
4348 free(repo_path);
4349 free(cwd);
4350 free(commit_id);
4351 free(obj_id);
4352 if (blob)
4353 got_object_blob_close(blob);
4354 if (worktree)
4355 got_worktree_close(worktree);
4356 if (repo) {
4357 const struct got_error *repo_error;
4358 repo_error = got_repo_close(repo);
4359 if (error == NULL)
4360 error = repo_error;
4362 if (bca.lines) {
4363 for (i = 0; i < bca.nlines; i++) {
4364 struct blame_line *bline = &bca.lines[i];
4365 free(bline->id_str);
4366 free(bline->committer);
4368 free(bca.lines);
4370 free(bca.line_offsets);
4371 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4372 error = got_error_from_errno("fclose");
4373 return error;
4376 __dead static void
4377 usage_tree(void)
4379 fprintf(stderr,
4380 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4381 getprogname());
4382 exit(1);
4385 static const struct got_error *
4386 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4387 const char *root_path, struct got_repository *repo)
4389 const struct got_error *err = NULL;
4390 int is_root_path = (strcmp(path, root_path) == 0);
4391 const char *modestr = "";
4392 mode_t mode = got_tree_entry_get_mode(te);
4393 char *link_target = NULL;
4395 path += strlen(root_path);
4396 while (path[0] == '/')
4397 path++;
4399 if (got_object_tree_entry_is_submodule(te))
4400 modestr = "$";
4401 else if (S_ISLNK(mode)) {
4402 int i;
4404 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4405 if (err)
4406 return err;
4407 for (i = 0; i < strlen(link_target); i++) {
4408 if (!isprint((unsigned char)link_target[i]))
4409 link_target[i] = '?';
4412 modestr = "@";
4414 else if (S_ISDIR(mode))
4415 modestr = "/";
4416 else if (mode & S_IXUSR)
4417 modestr = "*";
4419 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4420 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4421 link_target ? " -> ": "", link_target ? link_target : "");
4423 free(link_target);
4424 return NULL;
4427 static const struct got_error *
4428 print_tree(const char *path, struct got_object_id *commit_id,
4429 int show_ids, int recurse, const char *root_path,
4430 struct got_repository *repo)
4432 const struct got_error *err = NULL;
4433 struct got_object_id *tree_id = NULL;
4434 struct got_tree_object *tree = NULL;
4435 int nentries, i;
4437 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4438 if (err)
4439 goto done;
4441 err = got_object_open_as_tree(&tree, repo, tree_id);
4442 if (err)
4443 goto done;
4444 nentries = got_object_tree_get_nentries(tree);
4445 for (i = 0; i < nentries; i++) {
4446 struct got_tree_entry *te;
4447 char *id = NULL;
4449 if (sigint_received || sigpipe_received)
4450 break;
4452 te = got_object_tree_get_entry(tree, i);
4453 if (show_ids) {
4454 char *id_str;
4455 err = got_object_id_str(&id_str,
4456 got_tree_entry_get_id(te));
4457 if (err)
4458 goto done;
4459 if (asprintf(&id, "%s ", id_str) == -1) {
4460 err = got_error_from_errno("asprintf");
4461 free(id_str);
4462 goto done;
4464 free(id_str);
4466 err = print_entry(te, id, path, root_path, repo);
4467 free(id);
4468 if (err)
4469 goto done;
4471 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4472 char *child_path;
4473 if (asprintf(&child_path, "%s%s%s", path,
4474 path[0] == '/' && path[1] == '\0' ? "" : "/",
4475 got_tree_entry_get_name(te)) == -1) {
4476 err = got_error_from_errno("asprintf");
4477 goto done;
4479 err = print_tree(child_path, commit_id, show_ids, 1,
4480 root_path, repo);
4481 free(child_path);
4482 if (err)
4483 goto done;
4486 done:
4487 if (tree)
4488 got_object_tree_close(tree);
4489 free(tree_id);
4490 return err;
4493 static const struct got_error *
4494 cmd_tree(int argc, char *argv[])
4496 const struct got_error *error;
4497 struct got_repository *repo = NULL;
4498 struct got_worktree *worktree = NULL;
4499 const char *path, *refname = NULL;
4500 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4501 struct got_object_id *commit_id = NULL;
4502 char *commit_id_str = NULL;
4503 int show_ids = 0, recurse = 0;
4504 int ch;
4506 #ifndef PROFILE
4507 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4508 NULL) == -1)
4509 err(1, "pledge");
4510 #endif
4512 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4513 switch (ch) {
4514 case 'c':
4515 commit_id_str = optarg;
4516 break;
4517 case 'r':
4518 repo_path = realpath(optarg, NULL);
4519 if (repo_path == NULL)
4520 return got_error_from_errno2("realpath",
4521 optarg);
4522 got_path_strip_trailing_slashes(repo_path);
4523 break;
4524 case 'i':
4525 show_ids = 1;
4526 break;
4527 case 'R':
4528 recurse = 1;
4529 break;
4530 default:
4531 usage_tree();
4532 /* NOTREACHED */
4536 argc -= optind;
4537 argv += optind;
4539 if (argc == 1)
4540 path = argv[0];
4541 else if (argc > 1)
4542 usage_tree();
4543 else
4544 path = NULL;
4546 cwd = getcwd(NULL, 0);
4547 if (cwd == NULL) {
4548 error = got_error_from_errno("getcwd");
4549 goto done;
4551 if (repo_path == NULL) {
4552 error = got_worktree_open(&worktree, cwd);
4553 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4554 goto done;
4555 else
4556 error = NULL;
4557 if (worktree) {
4558 repo_path =
4559 strdup(got_worktree_get_repo_path(worktree));
4560 if (repo_path == NULL)
4561 error = got_error_from_errno("strdup");
4562 if (error)
4563 goto done;
4564 } else {
4565 repo_path = strdup(cwd);
4566 if (repo_path == NULL) {
4567 error = got_error_from_errno("strdup");
4568 goto done;
4573 error = got_repo_open(&repo, repo_path, NULL);
4574 if (error != NULL)
4575 goto done;
4577 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4578 if (error)
4579 goto done;
4581 if (path == NULL) {
4582 if (worktree) {
4583 char *p, *worktree_subdir = cwd +
4584 strlen(got_worktree_get_root_path(worktree));
4585 if (asprintf(&p, "%s/%s",
4586 got_worktree_get_path_prefix(worktree),
4587 worktree_subdir) == -1) {
4588 error = got_error_from_errno("asprintf");
4589 goto done;
4591 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4592 free(p);
4593 if (error)
4594 goto done;
4595 } else
4596 path = "/";
4598 if (in_repo_path == NULL) {
4599 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4600 if (error != NULL)
4601 goto done;
4604 if (commit_id_str == NULL) {
4605 struct got_reference *head_ref;
4606 if (worktree)
4607 refname = got_worktree_get_head_ref_name(worktree);
4608 else
4609 refname = GOT_REF_HEAD;
4610 error = got_ref_open(&head_ref, repo, refname, 0);
4611 if (error != NULL)
4612 goto done;
4613 error = got_ref_resolve(&commit_id, repo, head_ref);
4614 got_ref_close(head_ref);
4615 if (error != NULL)
4616 goto done;
4617 } else {
4618 error = got_repo_match_object_id(&commit_id, NULL,
4619 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4620 if (error)
4621 goto done;
4624 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4625 in_repo_path, repo);
4626 done:
4627 free(in_repo_path);
4628 free(repo_path);
4629 free(cwd);
4630 free(commit_id);
4631 if (worktree)
4632 got_worktree_close(worktree);
4633 if (repo) {
4634 const struct got_error *repo_error;
4635 repo_error = got_repo_close(repo);
4636 if (error == NULL)
4637 error = repo_error;
4639 return error;
4642 __dead static void
4643 usage_status(void)
4645 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4646 exit(1);
4649 static const struct got_error *
4650 print_status(void *arg, unsigned char status, unsigned char staged_status,
4651 const char *path, struct got_object_id *blob_id,
4652 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4653 int dirfd, const char *de_name)
4655 if (status == staged_status && (status == GOT_STATUS_DELETE))
4656 status = GOT_STATUS_NO_CHANGE;
4657 printf("%c%c %s\n", status, staged_status, path);
4658 return NULL;
4661 static const struct got_error *
4662 cmd_status(int argc, char *argv[])
4664 const struct got_error *error = NULL;
4665 struct got_repository *repo = NULL;
4666 struct got_worktree *worktree = NULL;
4667 char *cwd = NULL;
4668 struct got_pathlist_head paths;
4669 struct got_pathlist_entry *pe;
4670 int ch;
4672 TAILQ_INIT(&paths);
4674 while ((ch = getopt(argc, argv, "")) != -1) {
4675 switch (ch) {
4676 default:
4677 usage_status();
4678 /* NOTREACHED */
4682 argc -= optind;
4683 argv += optind;
4685 #ifndef PROFILE
4686 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4687 NULL) == -1)
4688 err(1, "pledge");
4689 #endif
4690 cwd = getcwd(NULL, 0);
4691 if (cwd == NULL) {
4692 error = got_error_from_errno("getcwd");
4693 goto done;
4696 error = got_worktree_open(&worktree, cwd);
4697 if (error) {
4698 if (error->code == GOT_ERR_NOT_WORKTREE)
4699 error = wrap_not_worktree_error(error, "status", cwd);
4700 goto done;
4703 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4704 NULL);
4705 if (error != NULL)
4706 goto done;
4708 error = apply_unveil(got_repo_get_path(repo), 1,
4709 got_worktree_get_root_path(worktree));
4710 if (error)
4711 goto done;
4713 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4714 if (error)
4715 goto done;
4717 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4718 check_cancelled, NULL);
4719 done:
4720 TAILQ_FOREACH(pe, &paths, entry)
4721 free((char *)pe->path);
4722 got_pathlist_free(&paths);
4723 free(cwd);
4724 return error;
4727 __dead static void
4728 usage_ref(void)
4730 fprintf(stderr,
4731 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4732 "[-d] [name]\n",
4733 getprogname());
4734 exit(1);
4737 static const struct got_error *
4738 list_refs(struct got_repository *repo, const char *refname)
4740 static const struct got_error *err = NULL;
4741 struct got_reflist_head refs;
4742 struct got_reflist_entry *re;
4744 SIMPLEQ_INIT(&refs);
4745 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4746 if (err)
4747 return err;
4749 SIMPLEQ_FOREACH(re, &refs, entry) {
4750 char *refstr;
4751 refstr = got_ref_to_str(re->ref);
4752 if (refstr == NULL)
4753 return got_error_from_errno("got_ref_to_str");
4754 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4755 free(refstr);
4758 got_ref_list_free(&refs);
4759 return NULL;
4762 static const struct got_error *
4763 delete_ref(struct got_repository *repo, const char *refname)
4765 const struct got_error *err = NULL;
4766 struct got_reference *ref;
4768 err = got_ref_open(&ref, repo, refname, 0);
4769 if (err)
4770 return err;
4772 err = got_ref_delete(ref, repo);
4773 got_ref_close(ref);
4774 return err;
4777 static const struct got_error *
4778 add_ref(struct got_repository *repo, const char *refname, const char *target)
4780 const struct got_error *err = NULL;
4781 struct got_object_id *id;
4782 struct got_reference *ref = NULL;
4785 * Don't let the user create a reference name with a leading '-'.
4786 * While technically a valid reference name, this case is usually
4787 * an unintended typo.
4789 if (refname[0] == '-')
4790 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4792 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4793 repo);
4794 if (err) {
4795 struct got_reference *target_ref;
4797 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4798 return err;
4799 err = got_ref_open(&target_ref, repo, target, 0);
4800 if (err)
4801 return err;
4802 err = got_ref_resolve(&id, repo, target_ref);
4803 got_ref_close(target_ref);
4804 if (err)
4805 return err;
4808 err = got_ref_alloc(&ref, refname, id);
4809 if (err)
4810 goto done;
4812 err = got_ref_write(ref, repo);
4813 done:
4814 if (ref)
4815 got_ref_close(ref);
4816 free(id);
4817 return err;
4820 static const struct got_error *
4821 add_symref(struct got_repository *repo, const char *refname, const char *target)
4823 const struct got_error *err = NULL;
4824 struct got_reference *ref = NULL;
4825 struct got_reference *target_ref = NULL;
4828 * Don't let the user create a reference name with a leading '-'.
4829 * While technically a valid reference name, this case is usually
4830 * an unintended typo.
4832 if (refname[0] == '-')
4833 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4835 err = got_ref_open(&target_ref, repo, target, 0);
4836 if (err)
4837 return err;
4839 err = got_ref_alloc_symref(&ref, refname, target_ref);
4840 if (err)
4841 goto done;
4843 err = got_ref_write(ref, repo);
4844 done:
4845 if (target_ref)
4846 got_ref_close(target_ref);
4847 if (ref)
4848 got_ref_close(ref);
4849 return err;
4852 static const struct got_error *
4853 cmd_ref(int argc, char *argv[])
4855 const struct got_error *error = NULL;
4856 struct got_repository *repo = NULL;
4857 struct got_worktree *worktree = NULL;
4858 char *cwd = NULL, *repo_path = NULL;
4859 int ch, do_list = 0, do_delete = 0;
4860 const char *obj_arg = NULL, *symref_target= NULL;
4861 char *refname = NULL;
4863 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4864 switch (ch) {
4865 case 'c':
4866 obj_arg = optarg;
4867 break;
4868 case 'd':
4869 do_delete = 1;
4870 break;
4871 case 'r':
4872 repo_path = realpath(optarg, NULL);
4873 if (repo_path == NULL)
4874 return got_error_from_errno2("realpath",
4875 optarg);
4876 got_path_strip_trailing_slashes(repo_path);
4877 break;
4878 case 'l':
4879 do_list = 1;
4880 break;
4881 case 's':
4882 symref_target = optarg;
4883 break;
4884 default:
4885 usage_ref();
4886 /* NOTREACHED */
4890 if (obj_arg && do_list)
4891 errx(1, "-c and -l options are mutually exclusive");
4892 if (obj_arg && do_delete)
4893 errx(1, "-c and -d options are mutually exclusive");
4894 if (obj_arg && symref_target)
4895 errx(1, "-c and -s options are mutually exclusive");
4896 if (symref_target && do_delete)
4897 errx(1, "-s and -d options are mutually exclusive");
4898 if (symref_target && do_list)
4899 errx(1, "-s and -l options are mutually exclusive");
4900 if (do_delete && do_list)
4901 errx(1, "-d and -l options are mutually exclusive");
4903 argc -= optind;
4904 argv += optind;
4906 if (do_list) {
4907 if (argc != 0 && argc != 1)
4908 usage_ref();
4909 if (argc == 1) {
4910 refname = strdup(argv[0]);
4911 if (refname == NULL) {
4912 error = got_error_from_errno("strdup");
4913 goto done;
4916 } else {
4917 if (argc != 1)
4918 usage_ref();
4919 refname = strdup(argv[0]);
4920 if (refname == NULL) {
4921 error = got_error_from_errno("strdup");
4922 goto done;
4926 if (refname)
4927 got_path_strip_trailing_slashes(refname);
4929 #ifndef PROFILE
4930 if (do_list) {
4931 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4932 NULL) == -1)
4933 err(1, "pledge");
4934 } else {
4935 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4936 "sendfd unveil", NULL) == -1)
4937 err(1, "pledge");
4939 #endif
4940 cwd = getcwd(NULL, 0);
4941 if (cwd == NULL) {
4942 error = got_error_from_errno("getcwd");
4943 goto done;
4946 if (repo_path == NULL) {
4947 error = got_worktree_open(&worktree, cwd);
4948 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4949 goto done;
4950 else
4951 error = NULL;
4952 if (worktree) {
4953 repo_path =
4954 strdup(got_worktree_get_repo_path(worktree));
4955 if (repo_path == NULL)
4956 error = got_error_from_errno("strdup");
4957 if (error)
4958 goto done;
4959 } else {
4960 repo_path = strdup(cwd);
4961 if (repo_path == NULL) {
4962 error = got_error_from_errno("strdup");
4963 goto done;
4968 error = got_repo_open(&repo, repo_path, NULL);
4969 if (error != NULL)
4970 goto done;
4972 error = apply_unveil(got_repo_get_path(repo), do_list,
4973 worktree ? got_worktree_get_root_path(worktree) : NULL);
4974 if (error)
4975 goto done;
4977 if (do_list)
4978 error = list_refs(repo, refname);
4979 else if (do_delete)
4980 error = delete_ref(repo, refname);
4981 else if (symref_target)
4982 error = add_symref(repo, refname, symref_target);
4983 else {
4984 if (obj_arg == NULL)
4985 usage_ref();
4986 error = add_ref(repo, refname, obj_arg);
4988 done:
4989 free(refname);
4990 if (repo)
4991 got_repo_close(repo);
4992 if (worktree)
4993 got_worktree_close(worktree);
4994 free(cwd);
4995 free(repo_path);
4996 return error;
4999 __dead static void
5000 usage_branch(void)
5002 fprintf(stderr,
5003 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5004 "[name]\n", getprogname());
5005 exit(1);
5008 static const struct got_error *
5009 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5010 struct got_reference *ref)
5012 const struct got_error *err = NULL;
5013 const char *refname, *marker = " ";
5014 char *refstr;
5016 refname = got_ref_get_name(ref);
5017 if (worktree && strcmp(refname,
5018 got_worktree_get_head_ref_name(worktree)) == 0) {
5019 struct got_object_id *id = NULL;
5021 err = got_ref_resolve(&id, repo, ref);
5022 if (err)
5023 return err;
5024 if (got_object_id_cmp(id,
5025 got_worktree_get_base_commit_id(worktree)) == 0)
5026 marker = "* ";
5027 else
5028 marker = "~ ";
5029 free(id);
5032 if (strncmp(refname, "refs/heads/", 11) == 0)
5033 refname += 11;
5034 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5035 refname += 18;
5037 refstr = got_ref_to_str(ref);
5038 if (refstr == NULL)
5039 return got_error_from_errno("got_ref_to_str");
5041 printf("%s%s: %s\n", marker, refname, refstr);
5042 free(refstr);
5043 return NULL;
5046 static const struct got_error *
5047 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5049 const char *refname;
5051 if (worktree == NULL)
5052 return got_error(GOT_ERR_NOT_WORKTREE);
5054 refname = got_worktree_get_head_ref_name(worktree);
5056 if (strncmp(refname, "refs/heads/", 11) == 0)
5057 refname += 11;
5058 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5059 refname += 18;
5061 printf("%s\n", refname);
5063 return NULL;
5066 static const struct got_error *
5067 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5069 static const struct got_error *err = NULL;
5070 struct got_reflist_head refs;
5071 struct got_reflist_entry *re;
5072 struct got_reference *temp_ref = NULL;
5073 int rebase_in_progress, histedit_in_progress;
5075 SIMPLEQ_INIT(&refs);
5077 if (worktree) {
5078 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5079 worktree);
5080 if (err)
5081 return err;
5083 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5084 worktree);
5085 if (err)
5086 return err;
5088 if (rebase_in_progress || histedit_in_progress) {
5089 err = got_ref_open(&temp_ref, repo,
5090 got_worktree_get_head_ref_name(worktree), 0);
5091 if (err)
5092 return err;
5093 list_branch(repo, worktree, temp_ref);
5094 got_ref_close(temp_ref);
5098 err = got_ref_list(&refs, repo, "refs/heads",
5099 got_ref_cmp_by_name, NULL);
5100 if (err)
5101 return err;
5103 SIMPLEQ_FOREACH(re, &refs, entry)
5104 list_branch(repo, worktree, re->ref);
5106 got_ref_list_free(&refs);
5107 return NULL;
5110 static const struct got_error *
5111 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5112 const char *branch_name)
5114 const struct got_error *err = NULL;
5115 struct got_reference *ref = NULL;
5116 char *refname;
5118 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5119 return got_error_from_errno("asprintf");
5121 err = got_ref_open(&ref, repo, refname, 0);
5122 if (err)
5123 goto done;
5125 if (worktree &&
5126 strcmp(got_worktree_get_head_ref_name(worktree),
5127 got_ref_get_name(ref)) == 0) {
5128 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5129 "will not delete this work tree's current branch");
5130 goto done;
5133 err = got_ref_delete(ref, repo);
5134 done:
5135 if (ref)
5136 got_ref_close(ref);
5137 free(refname);
5138 return err;
5141 static const struct got_error *
5142 add_branch(struct got_repository *repo, const char *branch_name,
5143 struct got_object_id *base_commit_id)
5145 const struct got_error *err = NULL;
5146 struct got_reference *ref = NULL;
5147 char *base_refname = NULL, *refname = NULL;
5150 * Don't let the user create a branch name with a leading '-'.
5151 * While technically a valid reference name, this case is usually
5152 * an unintended typo.
5154 if (branch_name[0] == '-')
5155 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5157 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5158 err = got_error_from_errno("asprintf");
5159 goto done;
5162 err = got_ref_open(&ref, repo, refname, 0);
5163 if (err == NULL) {
5164 err = got_error(GOT_ERR_BRANCH_EXISTS);
5165 goto done;
5166 } else if (err->code != GOT_ERR_NOT_REF)
5167 goto done;
5169 err = got_ref_alloc(&ref, refname, base_commit_id);
5170 if (err)
5171 goto done;
5173 err = got_ref_write(ref, repo);
5174 done:
5175 if (ref)
5176 got_ref_close(ref);
5177 free(base_refname);
5178 free(refname);
5179 return err;
5182 static const struct got_error *
5183 cmd_branch(int argc, char *argv[])
5185 const struct got_error *error = NULL;
5186 struct got_repository *repo = NULL;
5187 struct got_worktree *worktree = NULL;
5188 char *cwd = NULL, *repo_path = NULL;
5189 int ch, do_list = 0, do_show = 0, do_update = 1;
5190 const char *delref = NULL, *commit_id_arg = NULL;
5191 struct got_reference *ref = NULL;
5192 struct got_pathlist_head paths;
5193 struct got_pathlist_entry *pe;
5194 struct got_object_id *commit_id = NULL;
5195 char *commit_id_str = NULL;
5197 TAILQ_INIT(&paths);
5199 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5200 switch (ch) {
5201 case 'c':
5202 commit_id_arg = optarg;
5203 break;
5204 case 'd':
5205 delref = optarg;
5206 break;
5207 case 'r':
5208 repo_path = realpath(optarg, NULL);
5209 if (repo_path == NULL)
5210 return got_error_from_errno2("realpath",
5211 optarg);
5212 got_path_strip_trailing_slashes(repo_path);
5213 break;
5214 case 'l':
5215 do_list = 1;
5216 break;
5217 case 'n':
5218 do_update = 0;
5219 break;
5220 default:
5221 usage_branch();
5222 /* NOTREACHED */
5226 if (do_list && delref)
5227 errx(1, "-l and -d options are mutually exclusive");
5229 argc -= optind;
5230 argv += optind;
5232 if (!do_list && !delref && argc == 0)
5233 do_show = 1;
5235 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5236 errx(1, "-c option can only be used when creating a branch");
5238 if (do_list || delref) {
5239 if (argc > 0)
5240 usage_branch();
5241 } else if (!do_show && argc != 1)
5242 usage_branch();
5244 #ifndef PROFILE
5245 if (do_list || do_show) {
5246 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5247 NULL) == -1)
5248 err(1, "pledge");
5249 } else {
5250 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5251 "sendfd unveil", NULL) == -1)
5252 err(1, "pledge");
5254 #endif
5255 cwd = getcwd(NULL, 0);
5256 if (cwd == NULL) {
5257 error = got_error_from_errno("getcwd");
5258 goto done;
5261 if (repo_path == NULL) {
5262 error = got_worktree_open(&worktree, cwd);
5263 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5264 goto done;
5265 else
5266 error = NULL;
5267 if (worktree) {
5268 repo_path =
5269 strdup(got_worktree_get_repo_path(worktree));
5270 if (repo_path == NULL)
5271 error = got_error_from_errno("strdup");
5272 if (error)
5273 goto done;
5274 } else {
5275 repo_path = strdup(cwd);
5276 if (repo_path == NULL) {
5277 error = got_error_from_errno("strdup");
5278 goto done;
5283 error = got_repo_open(&repo, repo_path, NULL);
5284 if (error != NULL)
5285 goto done;
5287 error = apply_unveil(got_repo_get_path(repo), do_list,
5288 worktree ? got_worktree_get_root_path(worktree) : NULL);
5289 if (error)
5290 goto done;
5292 if (do_show)
5293 error = show_current_branch(repo, worktree);
5294 else if (do_list)
5295 error = list_branches(repo, worktree);
5296 else if (delref)
5297 error = delete_branch(repo, worktree, delref);
5298 else {
5299 if (commit_id_arg == NULL)
5300 commit_id_arg = worktree ?
5301 got_worktree_get_head_ref_name(worktree) :
5302 GOT_REF_HEAD;
5303 error = got_repo_match_object_id(&commit_id, NULL,
5304 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5305 if (error)
5306 goto done;
5307 error = add_branch(repo, argv[0], commit_id);
5308 if (error)
5309 goto done;
5310 if (worktree && do_update) {
5311 struct got_update_progress_arg upa;
5312 char *branch_refname = NULL;
5314 error = got_object_id_str(&commit_id_str, commit_id);
5315 if (error)
5316 goto done;
5317 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5318 worktree);
5319 if (error)
5320 goto done;
5321 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5322 == -1) {
5323 error = got_error_from_errno("asprintf");
5324 goto done;
5326 error = got_ref_open(&ref, repo, branch_refname, 0);
5327 free(branch_refname);
5328 if (error)
5329 goto done;
5330 error = switch_head_ref(ref, commit_id, worktree,
5331 repo);
5332 if (error)
5333 goto done;
5334 error = got_worktree_set_base_commit_id(worktree, repo,
5335 commit_id);
5336 if (error)
5337 goto done;
5338 memset(&upa, 0, sizeof(upa));
5339 error = got_worktree_checkout_files(worktree, &paths,
5340 repo, update_progress, &upa, check_cancelled,
5341 NULL);
5342 if (error)
5343 goto done;
5344 if (upa.did_something)
5345 printf("Updated to commit %s\n", commit_id_str);
5346 print_update_progress_stats(&upa);
5349 done:
5350 if (ref)
5351 got_ref_close(ref);
5352 if (repo)
5353 got_repo_close(repo);
5354 if (worktree)
5355 got_worktree_close(worktree);
5356 free(cwd);
5357 free(repo_path);
5358 free(commit_id);
5359 free(commit_id_str);
5360 TAILQ_FOREACH(pe, &paths, entry)
5361 free((char *)pe->path);
5362 got_pathlist_free(&paths);
5363 return error;
5367 __dead static void
5368 usage_tag(void)
5370 fprintf(stderr,
5371 "usage: %s tag [-c commit] [-r repository] [-l] "
5372 "[-m message] name\n", getprogname());
5373 exit(1);
5376 #if 0
5377 static const struct got_error *
5378 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5380 const struct got_error *err = NULL;
5381 struct got_reflist_entry *re, *se, *new;
5382 struct got_object_id *re_id, *se_id;
5383 struct got_tag_object *re_tag, *se_tag;
5384 time_t re_time, se_time;
5386 SIMPLEQ_FOREACH(re, tags, entry) {
5387 se = SIMPLEQ_FIRST(sorted);
5388 if (se == NULL) {
5389 err = got_reflist_entry_dup(&new, re);
5390 if (err)
5391 return err;
5392 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5393 continue;
5394 } else {
5395 err = got_ref_resolve(&re_id, repo, re->ref);
5396 if (err)
5397 break;
5398 err = got_object_open_as_tag(&re_tag, repo, re_id);
5399 free(re_id);
5400 if (err)
5401 break;
5402 re_time = got_object_tag_get_tagger_time(re_tag);
5403 got_object_tag_close(re_tag);
5406 while (se) {
5407 err = got_ref_resolve(&se_id, repo, re->ref);
5408 if (err)
5409 break;
5410 err = got_object_open_as_tag(&se_tag, repo, se_id);
5411 free(se_id);
5412 if (err)
5413 break;
5414 se_time = got_object_tag_get_tagger_time(se_tag);
5415 got_object_tag_close(se_tag);
5417 if (se_time > re_time) {
5418 err = got_reflist_entry_dup(&new, re);
5419 if (err)
5420 return err;
5421 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5422 break;
5424 se = SIMPLEQ_NEXT(se, entry);
5425 continue;
5428 done:
5429 return err;
5431 #endif
5433 static const struct got_error *
5434 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5436 static const struct got_error *err = NULL;
5437 struct got_reflist_head refs;
5438 struct got_reflist_entry *re;
5440 SIMPLEQ_INIT(&refs);
5442 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5443 if (err)
5444 return err;
5446 SIMPLEQ_FOREACH(re, &refs, entry) {
5447 const char *refname;
5448 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5449 char datebuf[26];
5450 const char *tagger;
5451 time_t tagger_time;
5452 struct got_object_id *id;
5453 struct got_tag_object *tag;
5454 struct got_commit_object *commit = NULL;
5456 refname = got_ref_get_name(re->ref);
5457 if (strncmp(refname, "refs/tags/", 10) != 0)
5458 continue;
5459 refname += 10;
5460 refstr = got_ref_to_str(re->ref);
5461 if (refstr == NULL) {
5462 err = got_error_from_errno("got_ref_to_str");
5463 break;
5465 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5466 free(refstr);
5468 err = got_ref_resolve(&id, repo, re->ref);
5469 if (err)
5470 break;
5471 err = got_object_open_as_tag(&tag, repo, id);
5472 if (err) {
5473 if (err->code != GOT_ERR_OBJ_TYPE) {
5474 free(id);
5475 break;
5477 /* "lightweight" tag */
5478 err = got_object_open_as_commit(&commit, repo, id);
5479 if (err) {
5480 free(id);
5481 break;
5483 tagger = got_object_commit_get_committer(commit);
5484 tagger_time =
5485 got_object_commit_get_committer_time(commit);
5486 err = got_object_id_str(&id_str, id);
5487 free(id);
5488 if (err)
5489 break;
5490 } else {
5491 free(id);
5492 tagger = got_object_tag_get_tagger(tag);
5493 tagger_time = got_object_tag_get_tagger_time(tag);
5494 err = got_object_id_str(&id_str,
5495 got_object_tag_get_object_id(tag));
5496 if (err)
5497 break;
5499 printf("from: %s\n", tagger);
5500 datestr = get_datestr(&tagger_time, datebuf);
5501 if (datestr)
5502 printf("date: %s UTC\n", datestr);
5503 if (commit)
5504 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5505 else {
5506 switch (got_object_tag_get_object_type(tag)) {
5507 case GOT_OBJ_TYPE_BLOB:
5508 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5509 id_str);
5510 break;
5511 case GOT_OBJ_TYPE_TREE:
5512 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5513 id_str);
5514 break;
5515 case GOT_OBJ_TYPE_COMMIT:
5516 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5517 id_str);
5518 break;
5519 case GOT_OBJ_TYPE_TAG:
5520 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5521 id_str);
5522 break;
5523 default:
5524 break;
5527 free(id_str);
5528 if (commit) {
5529 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5530 if (err)
5531 break;
5532 got_object_commit_close(commit);
5533 } else {
5534 tagmsg0 = strdup(got_object_tag_get_message(tag));
5535 got_object_tag_close(tag);
5536 if (tagmsg0 == NULL) {
5537 err = got_error_from_errno("strdup");
5538 break;
5542 tagmsg = tagmsg0;
5543 do {
5544 line = strsep(&tagmsg, "\n");
5545 if (line)
5546 printf(" %s\n", line);
5547 } while (line);
5548 free(tagmsg0);
5551 got_ref_list_free(&refs);
5552 return NULL;
5555 static const struct got_error *
5556 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5557 const char *tag_name, const char *repo_path)
5559 const struct got_error *err = NULL;
5560 char *template = NULL, *initial_content = NULL;
5561 char *editor = NULL;
5562 int fd = -1;
5564 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5565 err = got_error_from_errno("asprintf");
5566 goto done;
5569 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5570 commit_id_str, tag_name) == -1) {
5571 err = got_error_from_errno("asprintf");
5572 goto done;
5575 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5576 if (err)
5577 goto done;
5579 dprintf(fd, initial_content);
5580 close(fd);
5582 err = get_editor(&editor);
5583 if (err)
5584 goto done;
5585 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5586 done:
5587 free(initial_content);
5588 free(template);
5589 free(editor);
5591 /* Editor is done; we can now apply unveil(2) */
5592 if (err == NULL) {
5593 err = apply_unveil(repo_path, 0, NULL);
5594 if (err) {
5595 free(*tagmsg);
5596 *tagmsg = NULL;
5599 return err;
5602 static const struct got_error *
5603 add_tag(struct got_repository *repo, const char *tag_name,
5604 const char *commit_arg, const char *tagmsg_arg)
5606 const struct got_error *err = NULL;
5607 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5608 char *label = NULL, *commit_id_str = NULL;
5609 struct got_reference *ref = NULL;
5610 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5611 char *tagmsg_path = NULL, *tag_id_str = NULL;
5612 int preserve_tagmsg = 0;
5615 * Don't let the user create a tag name with a leading '-'.
5616 * While technically a valid reference name, this case is usually
5617 * an unintended typo.
5619 if (tag_name[0] == '-')
5620 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5622 err = get_author(&tagger, repo);
5623 if (err)
5624 return err;
5626 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5627 GOT_OBJ_TYPE_COMMIT, 1, repo);
5628 if (err)
5629 goto done;
5631 err = got_object_id_str(&commit_id_str, commit_id);
5632 if (err)
5633 goto done;
5635 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5636 refname = strdup(tag_name);
5637 if (refname == NULL) {
5638 err = got_error_from_errno("strdup");
5639 goto done;
5641 tag_name += 10;
5642 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5643 err = got_error_from_errno("asprintf");
5644 goto done;
5647 err = got_ref_open(&ref, repo, refname, 0);
5648 if (err == NULL) {
5649 err = got_error(GOT_ERR_TAG_EXISTS);
5650 goto done;
5651 } else if (err->code != GOT_ERR_NOT_REF)
5652 goto done;
5654 if (tagmsg_arg == NULL) {
5655 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5656 tag_name, got_repo_get_path(repo));
5657 if (err) {
5658 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5659 tagmsg_path != NULL)
5660 preserve_tagmsg = 1;
5661 goto done;
5665 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5666 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5667 if (err) {
5668 if (tagmsg_path)
5669 preserve_tagmsg = 1;
5670 goto done;
5673 err = got_ref_alloc(&ref, refname, tag_id);
5674 if (err) {
5675 if (tagmsg_path)
5676 preserve_tagmsg = 1;
5677 goto done;
5680 err = got_ref_write(ref, repo);
5681 if (err) {
5682 if (tagmsg_path)
5683 preserve_tagmsg = 1;
5684 goto done;
5687 err = got_object_id_str(&tag_id_str, tag_id);
5688 if (err) {
5689 if (tagmsg_path)
5690 preserve_tagmsg = 1;
5691 goto done;
5693 printf("Created tag %s\n", tag_id_str);
5694 done:
5695 if (preserve_tagmsg) {
5696 fprintf(stderr, "%s: tag message preserved in %s\n",
5697 getprogname(), tagmsg_path);
5698 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5699 err = got_error_from_errno2("unlink", tagmsg_path);
5700 free(tag_id_str);
5701 if (ref)
5702 got_ref_close(ref);
5703 free(commit_id);
5704 free(commit_id_str);
5705 free(refname);
5706 free(tagmsg);
5707 free(tagmsg_path);
5708 free(tagger);
5709 return err;
5712 static const struct got_error *
5713 cmd_tag(int argc, char *argv[])
5715 const struct got_error *error = NULL;
5716 struct got_repository *repo = NULL;
5717 struct got_worktree *worktree = NULL;
5718 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5719 char *gitconfig_path = NULL;
5720 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5721 int ch, do_list = 0;
5723 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5724 switch (ch) {
5725 case 'c':
5726 commit_id_arg = optarg;
5727 break;
5728 case 'm':
5729 tagmsg = optarg;
5730 break;
5731 case 'r':
5732 repo_path = realpath(optarg, NULL);
5733 if (repo_path == NULL)
5734 return got_error_from_errno2("realpath",
5735 optarg);
5736 got_path_strip_trailing_slashes(repo_path);
5737 break;
5738 case 'l':
5739 do_list = 1;
5740 break;
5741 default:
5742 usage_tag();
5743 /* NOTREACHED */
5747 argc -= optind;
5748 argv += optind;
5750 if (do_list) {
5751 if (commit_id_arg != NULL)
5752 errx(1,
5753 "-c option can only be used when creating a tag");
5754 if (tagmsg)
5755 errx(1, "-l and -m options are mutually exclusive");
5756 if (argc > 0)
5757 usage_tag();
5758 } else if (argc != 1)
5759 usage_tag();
5761 tag_name = argv[0];
5763 #ifndef PROFILE
5764 if (do_list) {
5765 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5766 NULL) == -1)
5767 err(1, "pledge");
5768 } else {
5769 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5770 "sendfd unveil", NULL) == -1)
5771 err(1, "pledge");
5773 #endif
5774 cwd = getcwd(NULL, 0);
5775 if (cwd == NULL) {
5776 error = got_error_from_errno("getcwd");
5777 goto done;
5780 if (repo_path == NULL) {
5781 error = got_worktree_open(&worktree, cwd);
5782 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5783 goto done;
5784 else
5785 error = NULL;
5786 if (worktree) {
5787 repo_path =
5788 strdup(got_worktree_get_repo_path(worktree));
5789 if (repo_path == NULL)
5790 error = got_error_from_errno("strdup");
5791 if (error)
5792 goto done;
5793 } else {
5794 repo_path = strdup(cwd);
5795 if (repo_path == NULL) {
5796 error = got_error_from_errno("strdup");
5797 goto done;
5802 if (do_list) {
5803 error = got_repo_open(&repo, repo_path, NULL);
5804 if (error != NULL)
5805 goto done;
5806 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5807 if (error)
5808 goto done;
5809 error = list_tags(repo, worktree);
5810 } else {
5811 error = get_gitconfig_path(&gitconfig_path);
5812 if (error)
5813 goto done;
5814 error = got_repo_open(&repo, repo_path, gitconfig_path);
5815 if (error != NULL)
5816 goto done;
5818 if (tagmsg) {
5819 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5820 if (error)
5821 goto done;
5824 if (commit_id_arg == NULL) {
5825 struct got_reference *head_ref;
5826 struct got_object_id *commit_id;
5827 error = got_ref_open(&head_ref, repo,
5828 worktree ? got_worktree_get_head_ref_name(worktree)
5829 : GOT_REF_HEAD, 0);
5830 if (error)
5831 goto done;
5832 error = got_ref_resolve(&commit_id, repo, head_ref);
5833 got_ref_close(head_ref);
5834 if (error)
5835 goto done;
5836 error = got_object_id_str(&commit_id_str, commit_id);
5837 free(commit_id);
5838 if (error)
5839 goto done;
5842 error = add_tag(repo, tag_name,
5843 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5845 done:
5846 if (repo)
5847 got_repo_close(repo);
5848 if (worktree)
5849 got_worktree_close(worktree);
5850 free(cwd);
5851 free(repo_path);
5852 free(gitconfig_path);
5853 free(commit_id_str);
5854 return error;
5857 __dead static void
5858 usage_add(void)
5860 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5861 getprogname());
5862 exit(1);
5865 static const struct got_error *
5866 add_progress(void *arg, unsigned char status, const char *path)
5868 while (path[0] == '/')
5869 path++;
5870 printf("%c %s\n", status, path);
5871 return NULL;
5874 static const struct got_error *
5875 cmd_add(int argc, char *argv[])
5877 const struct got_error *error = NULL;
5878 struct got_repository *repo = NULL;
5879 struct got_worktree *worktree = NULL;
5880 char *cwd = NULL;
5881 struct got_pathlist_head paths;
5882 struct got_pathlist_entry *pe;
5883 int ch, can_recurse = 0, no_ignores = 0;
5885 TAILQ_INIT(&paths);
5887 while ((ch = getopt(argc, argv, "IR")) != -1) {
5888 switch (ch) {
5889 case 'I':
5890 no_ignores = 1;
5891 break;
5892 case 'R':
5893 can_recurse = 1;
5894 break;
5895 default:
5896 usage_add();
5897 /* NOTREACHED */
5901 argc -= optind;
5902 argv += optind;
5904 #ifndef PROFILE
5905 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5906 NULL) == -1)
5907 err(1, "pledge");
5908 #endif
5909 if (argc < 1)
5910 usage_add();
5912 cwd = getcwd(NULL, 0);
5913 if (cwd == NULL) {
5914 error = got_error_from_errno("getcwd");
5915 goto done;
5918 error = got_worktree_open(&worktree, cwd);
5919 if (error) {
5920 if (error->code == GOT_ERR_NOT_WORKTREE)
5921 error = wrap_not_worktree_error(error, "add", cwd);
5922 goto done;
5925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5926 NULL);
5927 if (error != NULL)
5928 goto done;
5930 error = apply_unveil(got_repo_get_path(repo), 1,
5931 got_worktree_get_root_path(worktree));
5932 if (error)
5933 goto done;
5935 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5936 if (error)
5937 goto done;
5939 if (!can_recurse && no_ignores) {
5940 error = got_error_msg(GOT_ERR_BAD_PATH,
5941 "disregarding ignores requires -R option");
5942 goto done;
5946 if (!can_recurse) {
5947 char *ondisk_path;
5948 struct stat sb;
5949 TAILQ_FOREACH(pe, &paths, entry) {
5950 if (asprintf(&ondisk_path, "%s/%s",
5951 got_worktree_get_root_path(worktree),
5952 pe->path) == -1) {
5953 error = got_error_from_errno("asprintf");
5954 goto done;
5956 if (lstat(ondisk_path, &sb) == -1) {
5957 if (errno == ENOENT) {
5958 free(ondisk_path);
5959 continue;
5961 error = got_error_from_errno2("lstat",
5962 ondisk_path);
5963 free(ondisk_path);
5964 goto done;
5966 free(ondisk_path);
5967 if (S_ISDIR(sb.st_mode)) {
5968 error = got_error_msg(GOT_ERR_BAD_PATH,
5969 "adding directories requires -R option");
5970 goto done;
5975 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5976 NULL, repo, no_ignores);
5977 done:
5978 if (repo)
5979 got_repo_close(repo);
5980 if (worktree)
5981 got_worktree_close(worktree);
5982 TAILQ_FOREACH(pe, &paths, entry)
5983 free((char *)pe->path);
5984 got_pathlist_free(&paths);
5985 free(cwd);
5986 return error;
5989 __dead static void
5990 usage_remove(void)
5992 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5993 getprogname());
5994 exit(1);
5997 static const struct got_error *
5998 print_remove_status(void *arg, unsigned char status,
5999 unsigned char staged_status, const char *path)
6001 while (path[0] == '/')
6002 path++;
6003 if (status == GOT_STATUS_NONEXISTENT)
6004 return NULL;
6005 if (status == staged_status && (status == GOT_STATUS_DELETE))
6006 status = GOT_STATUS_NO_CHANGE;
6007 printf("%c%c %s\n", status, staged_status, path);
6008 return NULL;
6011 static const struct got_error *
6012 cmd_remove(int argc, char *argv[])
6014 const struct got_error *error = NULL;
6015 struct got_worktree *worktree = NULL;
6016 struct got_repository *repo = NULL;
6017 char *cwd = NULL;
6018 struct got_pathlist_head paths;
6019 struct got_pathlist_entry *pe;
6020 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
6022 TAILQ_INIT(&paths);
6024 while ((ch = getopt(argc, argv, "fkR")) != -1) {
6025 switch (ch) {
6026 case 'f':
6027 delete_local_mods = 1;
6028 break;
6029 case 'k':
6030 keep_on_disk = 1;
6031 break;
6032 case 'R':
6033 can_recurse = 1;
6034 break;
6035 default:
6036 usage_remove();
6037 /* NOTREACHED */
6041 argc -= optind;
6042 argv += optind;
6044 #ifndef PROFILE
6045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6046 NULL) == -1)
6047 err(1, "pledge");
6048 #endif
6049 if (argc < 1)
6050 usage_remove();
6052 cwd = getcwd(NULL, 0);
6053 if (cwd == NULL) {
6054 error = got_error_from_errno("getcwd");
6055 goto done;
6057 error = got_worktree_open(&worktree, cwd);
6058 if (error) {
6059 if (error->code == GOT_ERR_NOT_WORKTREE)
6060 error = wrap_not_worktree_error(error, "remove", cwd);
6061 goto done;
6064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6065 NULL);
6066 if (error)
6067 goto done;
6069 error = apply_unveil(got_repo_get_path(repo), 1,
6070 got_worktree_get_root_path(worktree));
6071 if (error)
6072 goto done;
6074 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6075 if (error)
6076 goto done;
6078 if (!can_recurse) {
6079 char *ondisk_path;
6080 struct stat sb;
6081 TAILQ_FOREACH(pe, &paths, entry) {
6082 if (asprintf(&ondisk_path, "%s/%s",
6083 got_worktree_get_root_path(worktree),
6084 pe->path) == -1) {
6085 error = got_error_from_errno("asprintf");
6086 goto done;
6088 if (lstat(ondisk_path, &sb) == -1) {
6089 if (errno == ENOENT) {
6090 free(ondisk_path);
6091 continue;
6093 error = got_error_from_errno2("lstat",
6094 ondisk_path);
6095 free(ondisk_path);
6096 goto done;
6098 free(ondisk_path);
6099 if (S_ISDIR(sb.st_mode)) {
6100 error = got_error_msg(GOT_ERR_BAD_PATH,
6101 "removing directories requires -R option");
6102 goto done;
6107 error = got_worktree_schedule_delete(worktree, &paths,
6108 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6109 done:
6110 if (repo)
6111 got_repo_close(repo);
6112 if (worktree)
6113 got_worktree_close(worktree);
6114 TAILQ_FOREACH(pe, &paths, entry)
6115 free((char *)pe->path);
6116 got_pathlist_free(&paths);
6117 free(cwd);
6118 return error;
6121 __dead static void
6122 usage_revert(void)
6124 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6125 "path ...\n", getprogname());
6126 exit(1);
6129 static const struct got_error *
6130 revert_progress(void *arg, unsigned char status, const char *path)
6132 if (status == GOT_STATUS_UNVERSIONED)
6133 return NULL;
6135 while (path[0] == '/')
6136 path++;
6137 printf("%c %s\n", status, path);
6138 return NULL;
6141 struct choose_patch_arg {
6142 FILE *patch_script_file;
6143 const char *action;
6146 static const struct got_error *
6147 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6148 int nchanges, const char *action)
6150 char *line = NULL;
6151 size_t linesize = 0;
6152 ssize_t linelen;
6154 switch (status) {
6155 case GOT_STATUS_ADD:
6156 printf("A %s\n%s this addition? [y/n] ", path, action);
6157 break;
6158 case GOT_STATUS_DELETE:
6159 printf("D %s\n%s this deletion? [y/n] ", path, action);
6160 break;
6161 case GOT_STATUS_MODIFY:
6162 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6163 return got_error_from_errno("fseek");
6164 printf(GOT_COMMIT_SEP_STR);
6165 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6166 printf("%s", line);
6167 if (ferror(patch_file))
6168 return got_error_from_errno("getline");
6169 printf(GOT_COMMIT_SEP_STR);
6170 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6171 path, n, nchanges, action);
6172 break;
6173 default:
6174 return got_error_path(path, GOT_ERR_FILE_STATUS);
6177 return NULL;
6180 static const struct got_error *
6181 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6182 FILE *patch_file, int n, int nchanges)
6184 const struct got_error *err = NULL;
6185 char *line = NULL;
6186 size_t linesize = 0;
6187 ssize_t linelen;
6188 int resp = ' ';
6189 struct choose_patch_arg *a = arg;
6191 *choice = GOT_PATCH_CHOICE_NONE;
6193 if (a->patch_script_file) {
6194 char *nl;
6195 err = show_change(status, path, patch_file, n, nchanges,
6196 a->action);
6197 if (err)
6198 return err;
6199 linelen = getline(&line, &linesize, a->patch_script_file);
6200 if (linelen == -1) {
6201 if (ferror(a->patch_script_file))
6202 return got_error_from_errno("getline");
6203 return NULL;
6205 nl = strchr(line, '\n');
6206 if (nl)
6207 *nl = '\0';
6208 if (strcmp(line, "y") == 0) {
6209 *choice = GOT_PATCH_CHOICE_YES;
6210 printf("y\n");
6211 } else if (strcmp(line, "n") == 0) {
6212 *choice = GOT_PATCH_CHOICE_NO;
6213 printf("n\n");
6214 } else if (strcmp(line, "q") == 0 &&
6215 status == GOT_STATUS_MODIFY) {
6216 *choice = GOT_PATCH_CHOICE_QUIT;
6217 printf("q\n");
6218 } else
6219 printf("invalid response '%s'\n", line);
6220 free(line);
6221 return NULL;
6224 while (resp != 'y' && resp != 'n' && resp != 'q') {
6225 err = show_change(status, path, patch_file, n, nchanges,
6226 a->action);
6227 if (err)
6228 return err;
6229 resp = getchar();
6230 if (resp == '\n')
6231 resp = getchar();
6232 if (status == GOT_STATUS_MODIFY) {
6233 if (resp != 'y' && resp != 'n' && resp != 'q') {
6234 printf("invalid response '%c'\n", resp);
6235 resp = ' ';
6237 } else if (resp != 'y' && resp != 'n') {
6238 printf("invalid response '%c'\n", resp);
6239 resp = ' ';
6243 if (resp == 'y')
6244 *choice = GOT_PATCH_CHOICE_YES;
6245 else if (resp == 'n')
6246 *choice = GOT_PATCH_CHOICE_NO;
6247 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6248 *choice = GOT_PATCH_CHOICE_QUIT;
6250 return NULL;
6254 static const struct got_error *
6255 cmd_revert(int argc, char *argv[])
6257 const struct got_error *error = NULL;
6258 struct got_worktree *worktree = NULL;
6259 struct got_repository *repo = NULL;
6260 char *cwd = NULL, *path = NULL;
6261 struct got_pathlist_head paths;
6262 struct got_pathlist_entry *pe;
6263 int ch, can_recurse = 0, pflag = 0;
6264 FILE *patch_script_file = NULL;
6265 const char *patch_script_path = NULL;
6266 struct choose_patch_arg cpa;
6268 TAILQ_INIT(&paths);
6270 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6271 switch (ch) {
6272 case 'p':
6273 pflag = 1;
6274 break;
6275 case 'F':
6276 patch_script_path = optarg;
6277 break;
6278 case 'R':
6279 can_recurse = 1;
6280 break;
6281 default:
6282 usage_revert();
6283 /* NOTREACHED */
6287 argc -= optind;
6288 argv += optind;
6290 #ifndef PROFILE
6291 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6292 "unveil", NULL) == -1)
6293 err(1, "pledge");
6294 #endif
6295 if (argc < 1)
6296 usage_revert();
6297 if (patch_script_path && !pflag)
6298 errx(1, "-F option can only be used together with -p option");
6300 cwd = getcwd(NULL, 0);
6301 if (cwd == NULL) {
6302 error = got_error_from_errno("getcwd");
6303 goto done;
6305 error = got_worktree_open(&worktree, cwd);
6306 if (error) {
6307 if (error->code == GOT_ERR_NOT_WORKTREE)
6308 error = wrap_not_worktree_error(error, "revert", cwd);
6309 goto done;
6312 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6313 NULL);
6314 if (error != NULL)
6315 goto done;
6317 if (patch_script_path) {
6318 patch_script_file = fopen(patch_script_path, "r");
6319 if (patch_script_file == NULL) {
6320 error = got_error_from_errno2("fopen",
6321 patch_script_path);
6322 goto done;
6325 error = apply_unveil(got_repo_get_path(repo), 1,
6326 got_worktree_get_root_path(worktree));
6327 if (error)
6328 goto done;
6330 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6331 if (error)
6332 goto done;
6334 if (!can_recurse) {
6335 char *ondisk_path;
6336 struct stat sb;
6337 TAILQ_FOREACH(pe, &paths, entry) {
6338 if (asprintf(&ondisk_path, "%s/%s",
6339 got_worktree_get_root_path(worktree),
6340 pe->path) == -1) {
6341 error = got_error_from_errno("asprintf");
6342 goto done;
6344 if (lstat(ondisk_path, &sb) == -1) {
6345 if (errno == ENOENT) {
6346 free(ondisk_path);
6347 continue;
6349 error = got_error_from_errno2("lstat",
6350 ondisk_path);
6351 free(ondisk_path);
6352 goto done;
6354 free(ondisk_path);
6355 if (S_ISDIR(sb.st_mode)) {
6356 error = got_error_msg(GOT_ERR_BAD_PATH,
6357 "reverting directories requires -R option");
6358 goto done;
6363 cpa.patch_script_file = patch_script_file;
6364 cpa.action = "revert";
6365 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6366 pflag ? choose_patch : NULL, &cpa, repo);
6367 done:
6368 if (patch_script_file && fclose(patch_script_file) == EOF &&
6369 error == NULL)
6370 error = got_error_from_errno2("fclose", patch_script_path);
6371 if (repo)
6372 got_repo_close(repo);
6373 if (worktree)
6374 got_worktree_close(worktree);
6375 free(path);
6376 free(cwd);
6377 return error;
6380 __dead static void
6381 usage_commit(void)
6383 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6384 getprogname());
6385 exit(1);
6388 struct collect_commit_logmsg_arg {
6389 const char *cmdline_log;
6390 const char *editor;
6391 const char *worktree_path;
6392 const char *branch_name;
6393 const char *repo_path;
6394 char *logmsg_path;
6398 static const struct got_error *
6399 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6400 void *arg)
6402 char *initial_content = NULL;
6403 struct got_pathlist_entry *pe;
6404 const struct got_error *err = NULL;
6405 char *template = NULL;
6406 struct collect_commit_logmsg_arg *a = arg;
6407 int fd;
6408 size_t len;
6410 /* if a message was specified on the command line, just use it */
6411 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6412 len = strlen(a->cmdline_log) + 1;
6413 *logmsg = malloc(len + 1);
6414 if (*logmsg == NULL)
6415 return got_error_from_errno("malloc");
6416 strlcpy(*logmsg, a->cmdline_log, len);
6417 return NULL;
6420 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6421 return got_error_from_errno("asprintf");
6423 if (asprintf(&initial_content,
6424 "\n# changes to be committed on branch %s:\n",
6425 a->branch_name) == -1)
6426 return got_error_from_errno("asprintf");
6428 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6429 if (err)
6430 goto done;
6432 dprintf(fd, initial_content);
6434 TAILQ_FOREACH(pe, commitable_paths, entry) {
6435 struct got_commitable *ct = pe->data;
6436 dprintf(fd, "# %c %s\n",
6437 got_commitable_get_status(ct),
6438 got_commitable_get_path(ct));
6440 close(fd);
6442 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6443 done:
6444 free(initial_content);
6445 free(template);
6447 /* Editor is done; we can now apply unveil(2) */
6448 if (err == NULL) {
6449 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6450 if (err) {
6451 free(*logmsg);
6452 *logmsg = NULL;
6455 return err;
6458 static const struct got_error *
6459 cmd_commit(int argc, char *argv[])
6461 const struct got_error *error = NULL;
6462 struct got_worktree *worktree = NULL;
6463 struct got_repository *repo = NULL;
6464 char *cwd = NULL, *id_str = NULL;
6465 struct got_object_id *id = NULL;
6466 const char *logmsg = NULL;
6467 struct collect_commit_logmsg_arg cl_arg;
6468 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6469 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6470 int allow_bad_symlinks = 0;
6471 struct got_pathlist_head paths;
6473 TAILQ_INIT(&paths);
6474 cl_arg.logmsg_path = NULL;
6476 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6477 switch (ch) {
6478 case 'm':
6479 logmsg = optarg;
6480 break;
6481 case 'S':
6482 allow_bad_symlinks = 1;
6483 break;
6484 default:
6485 usage_commit();
6486 /* NOTREACHED */
6490 argc -= optind;
6491 argv += optind;
6493 #ifndef PROFILE
6494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6495 "unveil", NULL) == -1)
6496 err(1, "pledge");
6497 #endif
6498 cwd = getcwd(NULL, 0);
6499 if (cwd == NULL) {
6500 error = got_error_from_errno("getcwd");
6501 goto done;
6503 error = got_worktree_open(&worktree, cwd);
6504 if (error) {
6505 if (error->code == GOT_ERR_NOT_WORKTREE)
6506 error = wrap_not_worktree_error(error, "commit", cwd);
6507 goto done;
6510 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6511 if (error)
6512 goto done;
6513 if (rebase_in_progress) {
6514 error = got_error(GOT_ERR_REBASING);
6515 goto done;
6518 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6519 worktree);
6520 if (error)
6521 goto done;
6523 error = get_gitconfig_path(&gitconfig_path);
6524 if (error)
6525 goto done;
6526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6527 gitconfig_path);
6528 if (error != NULL)
6529 goto done;
6531 error = get_author(&author, repo);
6532 if (error)
6533 return error;
6536 * unveil(2) traverses exec(2); if an editor is used we have
6537 * to apply unveil after the log message has been written.
6539 if (logmsg == NULL || strlen(logmsg) == 0)
6540 error = get_editor(&editor);
6541 else
6542 error = apply_unveil(got_repo_get_path(repo), 0,
6543 got_worktree_get_root_path(worktree));
6544 if (error)
6545 goto done;
6547 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6548 if (error)
6549 goto done;
6551 cl_arg.editor = editor;
6552 cl_arg.cmdline_log = logmsg;
6553 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6554 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6555 if (!histedit_in_progress) {
6556 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6557 error = got_error(GOT_ERR_COMMIT_BRANCH);
6558 goto done;
6560 cl_arg.branch_name += 11;
6562 cl_arg.repo_path = got_repo_get_path(repo);
6563 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6564 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6565 print_status, NULL, repo);
6566 if (error) {
6567 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6568 cl_arg.logmsg_path != NULL)
6569 preserve_logmsg = 1;
6570 goto done;
6573 error = got_object_id_str(&id_str, id);
6574 if (error)
6575 goto done;
6576 printf("Created commit %s\n", id_str);
6577 done:
6578 if (preserve_logmsg) {
6579 fprintf(stderr, "%s: log message preserved in %s\n",
6580 getprogname(), cl_arg.logmsg_path);
6581 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6582 error == NULL)
6583 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6584 free(cl_arg.logmsg_path);
6585 if (repo)
6586 got_repo_close(repo);
6587 if (worktree)
6588 got_worktree_close(worktree);
6589 free(cwd);
6590 free(id_str);
6591 free(gitconfig_path);
6592 free(editor);
6593 free(author);
6594 return error;
6597 __dead static void
6598 usage_cherrypick(void)
6600 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6601 exit(1);
6604 static const struct got_error *
6605 cmd_cherrypick(int argc, char *argv[])
6607 const struct got_error *error = NULL;
6608 struct got_worktree *worktree = NULL;
6609 struct got_repository *repo = NULL;
6610 char *cwd = NULL, *commit_id_str = NULL;
6611 struct got_object_id *commit_id = NULL;
6612 struct got_commit_object *commit = NULL;
6613 struct got_object_qid *pid;
6614 struct got_reference *head_ref = NULL;
6615 int ch;
6616 struct got_update_progress_arg upa;
6618 while ((ch = getopt(argc, argv, "")) != -1) {
6619 switch (ch) {
6620 default:
6621 usage_cherrypick();
6622 /* NOTREACHED */
6626 argc -= optind;
6627 argv += optind;
6629 #ifndef PROFILE
6630 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6631 "unveil", NULL) == -1)
6632 err(1, "pledge");
6633 #endif
6634 if (argc != 1)
6635 usage_cherrypick();
6637 cwd = getcwd(NULL, 0);
6638 if (cwd == NULL) {
6639 error = got_error_from_errno("getcwd");
6640 goto done;
6642 error = got_worktree_open(&worktree, cwd);
6643 if (error) {
6644 if (error->code == GOT_ERR_NOT_WORKTREE)
6645 error = wrap_not_worktree_error(error, "cherrypick",
6646 cwd);
6647 goto done;
6650 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6651 NULL);
6652 if (error != NULL)
6653 goto done;
6655 error = apply_unveil(got_repo_get_path(repo), 0,
6656 got_worktree_get_root_path(worktree));
6657 if (error)
6658 goto done;
6660 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6661 GOT_OBJ_TYPE_COMMIT, repo);
6662 if (error != NULL) {
6663 struct got_reference *ref;
6664 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6665 goto done;
6666 error = got_ref_open(&ref, repo, argv[0], 0);
6667 if (error != NULL)
6668 goto done;
6669 error = got_ref_resolve(&commit_id, repo, ref);
6670 got_ref_close(ref);
6671 if (error != NULL)
6672 goto done;
6674 error = got_object_id_str(&commit_id_str, commit_id);
6675 if (error)
6676 goto done;
6678 error = got_ref_open(&head_ref, repo,
6679 got_worktree_get_head_ref_name(worktree), 0);
6680 if (error != NULL)
6681 goto done;
6683 error = check_same_branch(commit_id, head_ref, NULL, repo);
6684 if (error) {
6685 if (error->code != GOT_ERR_ANCESTRY)
6686 goto done;
6687 error = NULL;
6688 } else {
6689 error = got_error(GOT_ERR_SAME_BRANCH);
6690 goto done;
6693 error = got_object_open_as_commit(&commit, repo, commit_id);
6694 if (error)
6695 goto done;
6696 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6697 memset(&upa, 0, sizeof(upa));
6698 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6699 commit_id, repo, update_progress, &upa, check_cancelled,
6700 NULL);
6701 if (error != NULL)
6702 goto done;
6704 if (upa.did_something)
6705 printf("Merged commit %s\n", commit_id_str);
6706 print_update_progress_stats(&upa);
6707 done:
6708 if (commit)
6709 got_object_commit_close(commit);
6710 free(commit_id_str);
6711 if (head_ref)
6712 got_ref_close(head_ref);
6713 if (worktree)
6714 got_worktree_close(worktree);
6715 if (repo)
6716 got_repo_close(repo);
6717 return error;
6720 __dead static void
6721 usage_backout(void)
6723 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6724 exit(1);
6727 static const struct got_error *
6728 cmd_backout(int argc, char *argv[])
6730 const struct got_error *error = NULL;
6731 struct got_worktree *worktree = NULL;
6732 struct got_repository *repo = NULL;
6733 char *cwd = NULL, *commit_id_str = NULL;
6734 struct got_object_id *commit_id = NULL;
6735 struct got_commit_object *commit = NULL;
6736 struct got_object_qid *pid;
6737 struct got_reference *head_ref = NULL;
6738 int ch;
6739 struct got_update_progress_arg upa;
6741 while ((ch = getopt(argc, argv, "")) != -1) {
6742 switch (ch) {
6743 default:
6744 usage_backout();
6745 /* NOTREACHED */
6749 argc -= optind;
6750 argv += optind;
6752 #ifndef PROFILE
6753 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6754 "unveil", NULL) == -1)
6755 err(1, "pledge");
6756 #endif
6757 if (argc != 1)
6758 usage_backout();
6760 cwd = getcwd(NULL, 0);
6761 if (cwd == NULL) {
6762 error = got_error_from_errno("getcwd");
6763 goto done;
6765 error = got_worktree_open(&worktree, cwd);
6766 if (error) {
6767 if (error->code == GOT_ERR_NOT_WORKTREE)
6768 error = wrap_not_worktree_error(error, "backout", cwd);
6769 goto done;
6772 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6773 NULL);
6774 if (error != NULL)
6775 goto done;
6777 error = apply_unveil(got_repo_get_path(repo), 0,
6778 got_worktree_get_root_path(worktree));
6779 if (error)
6780 goto done;
6782 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6783 GOT_OBJ_TYPE_COMMIT, repo);
6784 if (error != NULL) {
6785 struct got_reference *ref;
6786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6787 goto done;
6788 error = got_ref_open(&ref, repo, argv[0], 0);
6789 if (error != NULL)
6790 goto done;
6791 error = got_ref_resolve(&commit_id, repo, ref);
6792 got_ref_close(ref);
6793 if (error != NULL)
6794 goto done;
6796 error = got_object_id_str(&commit_id_str, commit_id);
6797 if (error)
6798 goto done;
6800 error = got_ref_open(&head_ref, repo,
6801 got_worktree_get_head_ref_name(worktree), 0);
6802 if (error != NULL)
6803 goto done;
6805 error = check_same_branch(commit_id, head_ref, NULL, repo);
6806 if (error)
6807 goto done;
6809 error = got_object_open_as_commit(&commit, repo, commit_id);
6810 if (error)
6811 goto done;
6812 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6813 if (pid == NULL) {
6814 error = got_error(GOT_ERR_ROOT_COMMIT);
6815 goto done;
6818 memset(&upa, 0, sizeof(upa));
6819 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6820 update_progress, &upa, check_cancelled, NULL);
6821 if (error != NULL)
6822 goto done;
6824 if (upa.did_something)
6825 printf("Backed out commit %s\n", commit_id_str);
6826 print_update_progress_stats(&upa);
6827 done:
6828 if (commit)
6829 got_object_commit_close(commit);
6830 free(commit_id_str);
6831 if (head_ref)
6832 got_ref_close(head_ref);
6833 if (worktree)
6834 got_worktree_close(worktree);
6835 if (repo)
6836 got_repo_close(repo);
6837 return error;
6840 __dead static void
6841 usage_rebase(void)
6843 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6844 getprogname());
6845 exit(1);
6848 void
6849 trim_logmsg(char *logmsg, int limit)
6851 char *nl;
6852 size_t len;
6854 len = strlen(logmsg);
6855 if (len > limit)
6856 len = limit;
6857 logmsg[len] = '\0';
6858 nl = strchr(logmsg, '\n');
6859 if (nl)
6860 *nl = '\0';
6863 static const struct got_error *
6864 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6866 const struct got_error *err;
6867 char *logmsg0 = NULL;
6868 const char *s;
6870 err = got_object_commit_get_logmsg(&logmsg0, commit);
6871 if (err)
6872 return err;
6874 s = logmsg0;
6875 while (isspace((unsigned char)s[0]))
6876 s++;
6878 *logmsg = strdup(s);
6879 if (*logmsg == NULL) {
6880 err = got_error_from_errno("strdup");
6881 goto done;
6884 trim_logmsg(*logmsg, limit);
6885 done:
6886 free(logmsg0);
6887 return err;
6890 static const struct got_error *
6891 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6893 const struct got_error *err;
6894 struct got_commit_object *commit = NULL;
6895 char *id_str = NULL, *logmsg = NULL;
6897 err = got_object_open_as_commit(&commit, repo, id);
6898 if (err)
6899 return err;
6901 err = got_object_id_str(&id_str, id);
6902 if (err)
6903 goto done;
6905 id_str[12] = '\0';
6907 err = get_short_logmsg(&logmsg, 42, commit);
6908 if (err)
6909 goto done;
6911 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6912 done:
6913 free(id_str);
6914 got_object_commit_close(commit);
6915 free(logmsg);
6916 return err;
6919 static const struct got_error *
6920 show_rebase_progress(struct got_commit_object *commit,
6921 struct got_object_id *old_id, struct got_object_id *new_id)
6923 const struct got_error *err;
6924 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6926 err = got_object_id_str(&old_id_str, old_id);
6927 if (err)
6928 goto done;
6930 if (new_id) {
6931 err = got_object_id_str(&new_id_str, new_id);
6932 if (err)
6933 goto done;
6936 old_id_str[12] = '\0';
6937 if (new_id_str)
6938 new_id_str[12] = '\0';
6940 err = get_short_logmsg(&logmsg, 42, commit);
6941 if (err)
6942 goto done;
6944 printf("%s -> %s: %s\n", old_id_str,
6945 new_id_str ? new_id_str : "no-op change", logmsg);
6946 done:
6947 free(old_id_str);
6948 free(new_id_str);
6949 free(logmsg);
6950 return err;
6953 static const struct got_error *
6954 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6955 struct got_reference *branch, struct got_reference *new_base_branch,
6956 struct got_reference *tmp_branch, struct got_repository *repo)
6958 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6959 return got_worktree_rebase_complete(worktree, fileindex,
6960 new_base_branch, tmp_branch, branch, repo);
6963 static const struct got_error *
6964 rebase_commit(struct got_pathlist_head *merged_paths,
6965 struct got_worktree *worktree, struct got_fileindex *fileindex,
6966 struct got_reference *tmp_branch,
6967 struct got_object_id *commit_id, struct got_repository *repo)
6969 const struct got_error *error;
6970 struct got_commit_object *commit;
6971 struct got_object_id *new_commit_id;
6973 error = got_object_open_as_commit(&commit, repo, commit_id);
6974 if (error)
6975 return error;
6977 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6978 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6979 if (error) {
6980 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6981 goto done;
6982 error = show_rebase_progress(commit, commit_id, NULL);
6983 } else {
6984 error = show_rebase_progress(commit, commit_id, new_commit_id);
6985 free(new_commit_id);
6987 done:
6988 got_object_commit_close(commit);
6989 return error;
6992 struct check_path_prefix_arg {
6993 const char *path_prefix;
6994 size_t len;
6995 int errcode;
6998 static const struct got_error *
6999 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7000 struct got_blob_object *blob2, struct got_object_id *id1,
7001 struct got_object_id *id2, const char *path1, const char *path2,
7002 mode_t mode1, mode_t mode2, struct got_repository *repo)
7004 struct check_path_prefix_arg *a = arg;
7006 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7007 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7008 return got_error(a->errcode);
7010 return NULL;
7013 static const struct got_error *
7014 check_path_prefix(struct got_object_id *parent_id,
7015 struct got_object_id *commit_id, const char *path_prefix,
7016 int errcode, struct got_repository *repo)
7018 const struct got_error *err;
7019 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7020 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7021 struct check_path_prefix_arg cpp_arg;
7023 if (got_path_is_root_dir(path_prefix))
7024 return NULL;
7026 err = got_object_open_as_commit(&commit, repo, commit_id);
7027 if (err)
7028 goto done;
7030 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7031 if (err)
7032 goto done;
7034 err = got_object_open_as_tree(&tree1, repo,
7035 got_object_commit_get_tree_id(parent_commit));
7036 if (err)
7037 goto done;
7039 err = got_object_open_as_tree(&tree2, repo,
7040 got_object_commit_get_tree_id(commit));
7041 if (err)
7042 goto done;
7044 cpp_arg.path_prefix = path_prefix;
7045 while (cpp_arg.path_prefix[0] == '/')
7046 cpp_arg.path_prefix++;
7047 cpp_arg.len = strlen(cpp_arg.path_prefix);
7048 cpp_arg.errcode = errcode;
7049 err = got_diff_tree(tree1, tree2, "", "", repo,
7050 check_path_prefix_in_diff, &cpp_arg, 0);
7051 done:
7052 if (tree1)
7053 got_object_tree_close(tree1);
7054 if (tree2)
7055 got_object_tree_close(tree2);
7056 if (commit)
7057 got_object_commit_close(commit);
7058 if (parent_commit)
7059 got_object_commit_close(parent_commit);
7060 return err;
7063 static const struct got_error *
7064 collect_commits(struct got_object_id_queue *commits,
7065 struct got_object_id *initial_commit_id,
7066 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7067 const char *path_prefix, int path_prefix_errcode,
7068 struct got_repository *repo)
7070 const struct got_error *err = NULL;
7071 struct got_commit_graph *graph = NULL;
7072 struct got_object_id *parent_id = NULL;
7073 struct got_object_qid *qid;
7074 struct got_object_id *commit_id = initial_commit_id;
7076 err = got_commit_graph_open(&graph, "/", 1);
7077 if (err)
7078 return err;
7080 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7081 check_cancelled, NULL);
7082 if (err)
7083 goto done;
7084 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7085 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7086 check_cancelled, NULL);
7087 if (err) {
7088 if (err->code == GOT_ERR_ITER_COMPLETED) {
7089 err = got_error_msg(GOT_ERR_ANCESTRY,
7090 "ran out of commits to rebase before "
7091 "youngest common ancestor commit has "
7092 "been reached?!?");
7094 goto done;
7095 } else {
7096 err = check_path_prefix(parent_id, commit_id,
7097 path_prefix, path_prefix_errcode, repo);
7098 if (err)
7099 goto done;
7101 err = got_object_qid_alloc(&qid, commit_id);
7102 if (err)
7103 goto done;
7104 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7105 commit_id = parent_id;
7108 done:
7109 got_commit_graph_close(graph);
7110 return err;
7113 static const struct got_error *
7114 cmd_rebase(int argc, char *argv[])
7116 const struct got_error *error = NULL;
7117 struct got_worktree *worktree = NULL;
7118 struct got_repository *repo = NULL;
7119 struct got_fileindex *fileindex = NULL;
7120 char *cwd = NULL;
7121 struct got_reference *branch = NULL;
7122 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7123 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7124 struct got_object_id *resume_commit_id = NULL;
7125 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7126 struct got_commit_object *commit = NULL;
7127 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7128 int histedit_in_progress = 0;
7129 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7130 struct got_object_id_queue commits;
7131 struct got_pathlist_head merged_paths;
7132 const struct got_object_id_queue *parent_ids;
7133 struct got_object_qid *qid, *pid;
7135 SIMPLEQ_INIT(&commits);
7136 TAILQ_INIT(&merged_paths);
7138 while ((ch = getopt(argc, argv, "ac")) != -1) {
7139 switch (ch) {
7140 case 'a':
7141 abort_rebase = 1;
7142 break;
7143 case 'c':
7144 continue_rebase = 1;
7145 break;
7146 default:
7147 usage_rebase();
7148 /* NOTREACHED */
7152 argc -= optind;
7153 argv += optind;
7155 #ifndef PROFILE
7156 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7157 "unveil", NULL) == -1)
7158 err(1, "pledge");
7159 #endif
7160 if (abort_rebase && continue_rebase)
7161 usage_rebase();
7162 else if (abort_rebase || continue_rebase) {
7163 if (argc != 0)
7164 usage_rebase();
7165 } else if (argc != 1)
7166 usage_rebase();
7168 cwd = getcwd(NULL, 0);
7169 if (cwd == NULL) {
7170 error = got_error_from_errno("getcwd");
7171 goto done;
7173 error = got_worktree_open(&worktree, cwd);
7174 if (error) {
7175 if (error->code == GOT_ERR_NOT_WORKTREE)
7176 error = wrap_not_worktree_error(error, "rebase", cwd);
7177 goto done;
7180 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7181 NULL);
7182 if (error != NULL)
7183 goto done;
7185 error = apply_unveil(got_repo_get_path(repo), 0,
7186 got_worktree_get_root_path(worktree));
7187 if (error)
7188 goto done;
7190 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7191 worktree);
7192 if (error)
7193 goto done;
7194 if (histedit_in_progress) {
7195 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7196 goto done;
7199 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7200 if (error)
7201 goto done;
7203 if (abort_rebase) {
7204 struct got_update_progress_arg upa;
7205 if (!rebase_in_progress) {
7206 error = got_error(GOT_ERR_NOT_REBASING);
7207 goto done;
7209 error = got_worktree_rebase_continue(&resume_commit_id,
7210 &new_base_branch, &tmp_branch, &branch, &fileindex,
7211 worktree, repo);
7212 if (error)
7213 goto done;
7214 printf("Switching work tree to %s\n",
7215 got_ref_get_symref_target(new_base_branch));
7216 memset(&upa, 0, sizeof(upa));
7217 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7218 new_base_branch, update_progress, &upa);
7219 if (error)
7220 goto done;
7221 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7222 print_update_progress_stats(&upa);
7223 goto done; /* nothing else to do */
7226 if (continue_rebase) {
7227 if (!rebase_in_progress) {
7228 error = got_error(GOT_ERR_NOT_REBASING);
7229 goto done;
7231 error = got_worktree_rebase_continue(&resume_commit_id,
7232 &new_base_branch, &tmp_branch, &branch, &fileindex,
7233 worktree, repo);
7234 if (error)
7235 goto done;
7237 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7238 resume_commit_id, repo);
7239 if (error)
7240 goto done;
7242 yca_id = got_object_id_dup(resume_commit_id);
7243 if (yca_id == NULL) {
7244 error = got_error_from_errno("got_object_id_dup");
7245 goto done;
7247 } else {
7248 error = got_ref_open(&branch, repo, argv[0], 0);
7249 if (error != NULL)
7250 goto done;
7253 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7254 if (error)
7255 goto done;
7257 if (!continue_rebase) {
7258 struct got_object_id *base_commit_id;
7260 base_commit_id = got_worktree_get_base_commit_id(worktree);
7261 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7262 base_commit_id, branch_head_commit_id, repo,
7263 check_cancelled, NULL);
7264 if (error)
7265 goto done;
7266 if (yca_id == NULL) {
7267 error = got_error_msg(GOT_ERR_ANCESTRY,
7268 "specified branch shares no common ancestry "
7269 "with work tree's branch");
7270 goto done;
7273 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7274 if (error) {
7275 if (error->code != GOT_ERR_ANCESTRY)
7276 goto done;
7277 error = NULL;
7278 } else {
7279 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7280 "specified branch resolves to a commit which "
7281 "is already contained in work tree's branch");
7282 goto done;
7284 error = got_worktree_rebase_prepare(&new_base_branch,
7285 &tmp_branch, &fileindex, worktree, branch, repo);
7286 if (error)
7287 goto done;
7290 commit_id = branch_head_commit_id;
7291 error = got_object_open_as_commit(&commit, repo, commit_id);
7292 if (error)
7293 goto done;
7295 parent_ids = got_object_commit_get_parent_ids(commit);
7296 pid = SIMPLEQ_FIRST(parent_ids);
7297 if (pid == NULL) {
7298 if (!continue_rebase) {
7299 struct got_update_progress_arg upa;
7300 memset(&upa, 0, sizeof(upa));
7301 error = got_worktree_rebase_abort(worktree, fileindex,
7302 repo, new_base_branch, update_progress, &upa);
7303 if (error)
7304 goto done;
7305 printf("Rebase of %s aborted\n",
7306 got_ref_get_name(branch));
7307 print_update_progress_stats(&upa);
7310 error = got_error(GOT_ERR_EMPTY_REBASE);
7311 goto done;
7313 error = collect_commits(&commits, commit_id, pid->id,
7314 yca_id, got_worktree_get_path_prefix(worktree),
7315 GOT_ERR_REBASE_PATH, repo);
7316 got_object_commit_close(commit);
7317 commit = NULL;
7318 if (error)
7319 goto done;
7321 if (SIMPLEQ_EMPTY(&commits)) {
7322 if (continue_rebase) {
7323 error = rebase_complete(worktree, fileindex,
7324 branch, new_base_branch, tmp_branch, repo);
7325 goto done;
7326 } else {
7327 /* Fast-forward the reference of the branch. */
7328 struct got_object_id *new_head_commit_id;
7329 char *id_str;
7330 error = got_ref_resolve(&new_head_commit_id, repo,
7331 new_base_branch);
7332 if (error)
7333 goto done;
7334 error = got_object_id_str(&id_str, new_head_commit_id);
7335 printf("Forwarding %s to commit %s\n",
7336 got_ref_get_name(branch), id_str);
7337 free(id_str);
7338 error = got_ref_change_ref(branch,
7339 new_head_commit_id);
7340 if (error)
7341 goto done;
7345 pid = NULL;
7346 SIMPLEQ_FOREACH(qid, &commits, entry) {
7347 struct got_update_progress_arg upa;
7349 commit_id = qid->id;
7350 parent_id = pid ? pid->id : yca_id;
7351 pid = qid;
7353 memset(&upa, 0, sizeof(upa));
7354 error = got_worktree_rebase_merge_files(&merged_paths,
7355 worktree, fileindex, parent_id, commit_id, repo,
7356 update_progress, &upa, check_cancelled, NULL);
7357 if (error)
7358 goto done;
7360 print_update_progress_stats(&upa);
7361 if (upa.conflicts > 0)
7362 rebase_status = GOT_STATUS_CONFLICT;
7364 if (rebase_status == GOT_STATUS_CONFLICT) {
7365 error = show_rebase_merge_conflict(qid->id, repo);
7366 if (error)
7367 goto done;
7368 got_worktree_rebase_pathlist_free(&merged_paths);
7369 break;
7372 error = rebase_commit(&merged_paths, worktree, fileindex,
7373 tmp_branch, commit_id, repo);
7374 got_worktree_rebase_pathlist_free(&merged_paths);
7375 if (error)
7376 goto done;
7379 if (rebase_status == GOT_STATUS_CONFLICT) {
7380 error = got_worktree_rebase_postpone(worktree, fileindex);
7381 if (error)
7382 goto done;
7383 error = got_error_msg(GOT_ERR_CONFLICTS,
7384 "conflicts must be resolved before rebasing can continue");
7385 } else
7386 error = rebase_complete(worktree, fileindex, branch,
7387 new_base_branch, tmp_branch, repo);
7388 done:
7389 got_object_id_queue_free(&commits);
7390 free(branch_head_commit_id);
7391 free(resume_commit_id);
7392 free(yca_id);
7393 if (commit)
7394 got_object_commit_close(commit);
7395 if (branch)
7396 got_ref_close(branch);
7397 if (new_base_branch)
7398 got_ref_close(new_base_branch);
7399 if (tmp_branch)
7400 got_ref_close(tmp_branch);
7401 if (worktree)
7402 got_worktree_close(worktree);
7403 if (repo)
7404 got_repo_close(repo);
7405 return error;
7408 __dead static void
7409 usage_histedit(void)
7411 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7412 getprogname());
7413 exit(1);
7416 #define GOT_HISTEDIT_PICK 'p'
7417 #define GOT_HISTEDIT_EDIT 'e'
7418 #define GOT_HISTEDIT_FOLD 'f'
7419 #define GOT_HISTEDIT_DROP 'd'
7420 #define GOT_HISTEDIT_MESG 'm'
7422 static struct got_histedit_cmd {
7423 unsigned char code;
7424 const char *name;
7425 const char *desc;
7426 } got_histedit_cmds[] = {
7427 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7428 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7429 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7430 "be used" },
7431 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7432 { GOT_HISTEDIT_MESG, "mesg",
7433 "single-line log message for commit above (open editor if empty)" },
7436 struct got_histedit_list_entry {
7437 TAILQ_ENTRY(got_histedit_list_entry) entry;
7438 struct got_object_id *commit_id;
7439 const struct got_histedit_cmd *cmd;
7440 char *logmsg;
7442 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7444 static const struct got_error *
7445 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7446 FILE *f, struct got_repository *repo)
7448 const struct got_error *err = NULL;
7449 char *logmsg = NULL, *id_str = NULL;
7450 struct got_commit_object *commit = NULL;
7451 int n;
7453 err = got_object_open_as_commit(&commit, repo, commit_id);
7454 if (err)
7455 goto done;
7457 err = get_short_logmsg(&logmsg, 34, commit);
7458 if (err)
7459 goto done;
7461 err = got_object_id_str(&id_str, commit_id);
7462 if (err)
7463 goto done;
7465 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7466 if (n < 0)
7467 err = got_ferror(f, GOT_ERR_IO);
7468 done:
7469 if (commit)
7470 got_object_commit_close(commit);
7471 free(id_str);
7472 free(logmsg);
7473 return err;
7476 static const struct got_error *
7477 histedit_write_commit_list(struct got_object_id_queue *commits,
7478 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7480 const struct got_error *err = NULL;
7481 struct got_object_qid *qid;
7483 if (SIMPLEQ_EMPTY(commits))
7484 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7486 SIMPLEQ_FOREACH(qid, commits, entry) {
7487 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7488 f, repo);
7489 if (err)
7490 break;
7491 if (edit_logmsg_only) {
7492 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7493 if (n < 0) {
7494 err = got_ferror(f, GOT_ERR_IO);
7495 break;
7500 return err;
7503 static const struct got_error *
7504 write_cmd_list(FILE *f, const char *branch_name,
7505 struct got_object_id_queue *commits)
7507 const struct got_error *err = NULL;
7508 int n, i;
7509 char *id_str;
7510 struct got_object_qid *qid;
7512 qid = SIMPLEQ_FIRST(commits);
7513 err = got_object_id_str(&id_str, qid->id);
7514 if (err)
7515 return err;
7517 n = fprintf(f,
7518 "# Editing the history of branch '%s' starting at\n"
7519 "# commit %s\n"
7520 "# Commits will be processed in order from top to "
7521 "bottom of this file.\n", branch_name, id_str);
7522 if (n < 0) {
7523 err = got_ferror(f, GOT_ERR_IO);
7524 goto done;
7527 n = fprintf(f, "# Available histedit commands:\n");
7528 if (n < 0) {
7529 err = got_ferror(f, GOT_ERR_IO);
7530 goto done;
7533 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7534 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7535 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7536 cmd->desc);
7537 if (n < 0) {
7538 err = got_ferror(f, GOT_ERR_IO);
7539 break;
7542 done:
7543 free(id_str);
7544 return err;
7547 static const struct got_error *
7548 histedit_syntax_error(int lineno)
7550 static char msg[42];
7551 int ret;
7553 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7554 lineno);
7555 if (ret == -1 || ret >= sizeof(msg))
7556 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7558 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7561 static const struct got_error *
7562 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7563 char *logmsg, struct got_repository *repo)
7565 const struct got_error *err;
7566 struct got_commit_object *folded_commit = NULL;
7567 char *id_str, *folded_logmsg = NULL;
7569 err = got_object_id_str(&id_str, hle->commit_id);
7570 if (err)
7571 return err;
7573 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7574 if (err)
7575 goto done;
7577 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7578 if (err)
7579 goto done;
7580 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7581 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7582 folded_logmsg) == -1) {
7583 err = got_error_from_errno("asprintf");
7585 done:
7586 if (folded_commit)
7587 got_object_commit_close(folded_commit);
7588 free(id_str);
7589 free(folded_logmsg);
7590 return err;
7593 static struct got_histedit_list_entry *
7594 get_folded_commits(struct got_histedit_list_entry *hle)
7596 struct got_histedit_list_entry *prev, *folded = NULL;
7598 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7599 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7600 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7601 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7602 folded = prev;
7603 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7606 return folded;
7609 static const struct got_error *
7610 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7611 struct got_repository *repo)
7613 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7614 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7615 const struct got_error *err = NULL;
7616 struct got_commit_object *commit = NULL;
7617 int fd;
7618 struct got_histedit_list_entry *folded = NULL;
7620 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7621 if (err)
7622 return err;
7624 folded = get_folded_commits(hle);
7625 if (folded) {
7626 while (folded != hle) {
7627 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7628 folded = TAILQ_NEXT(folded, entry);
7629 continue;
7631 err = append_folded_commit_msg(&new_msg, folded,
7632 logmsg, repo);
7633 if (err)
7634 goto done;
7635 free(logmsg);
7636 logmsg = new_msg;
7637 folded = TAILQ_NEXT(folded, entry);
7641 err = got_object_id_str(&id_str, hle->commit_id);
7642 if (err)
7643 goto done;
7644 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7645 if (err)
7646 goto done;
7647 if (asprintf(&new_msg,
7648 "%s\n# original log message of commit %s: %s",
7649 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7650 err = got_error_from_errno("asprintf");
7651 goto done;
7653 free(logmsg);
7654 logmsg = new_msg;
7656 err = got_object_id_str(&id_str, hle->commit_id);
7657 if (err)
7658 goto done;
7660 err = got_opentemp_named_fd(&logmsg_path, &fd,
7661 GOT_TMPDIR_STR "/got-logmsg");
7662 if (err)
7663 goto done;
7665 dprintf(fd, logmsg);
7666 close(fd);
7668 err = get_editor(&editor);
7669 if (err)
7670 goto done;
7672 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7673 if (err) {
7674 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7675 goto done;
7676 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7678 done:
7679 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7680 err = got_error_from_errno2("unlink", logmsg_path);
7681 free(logmsg_path);
7682 free(logmsg);
7683 free(orig_logmsg);
7684 free(editor);
7685 if (commit)
7686 got_object_commit_close(commit);
7687 return err;
7690 static const struct got_error *
7691 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7692 FILE *f, struct got_repository *repo)
7694 const struct got_error *err = NULL;
7695 char *line = NULL, *p, *end;
7696 size_t size;
7697 ssize_t len;
7698 int lineno = 0, i;
7699 const struct got_histedit_cmd *cmd;
7700 struct got_object_id *commit_id = NULL;
7701 struct got_histedit_list_entry *hle = NULL;
7703 for (;;) {
7704 len = getline(&line, &size, f);
7705 if (len == -1) {
7706 const struct got_error *getline_err;
7707 if (feof(f))
7708 break;
7709 getline_err = got_error_from_errno("getline");
7710 err = got_ferror(f, getline_err->code);
7711 break;
7713 lineno++;
7714 p = line;
7715 while (isspace((unsigned char)p[0]))
7716 p++;
7717 if (p[0] == '#' || p[0] == '\0') {
7718 free(line);
7719 line = NULL;
7720 continue;
7722 cmd = NULL;
7723 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7724 cmd = &got_histedit_cmds[i];
7725 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7726 isspace((unsigned char)p[strlen(cmd->name)])) {
7727 p += strlen(cmd->name);
7728 break;
7730 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7731 p++;
7732 break;
7735 if (i == nitems(got_histedit_cmds)) {
7736 err = histedit_syntax_error(lineno);
7737 break;
7739 while (isspace((unsigned char)p[0]))
7740 p++;
7741 if (cmd->code == GOT_HISTEDIT_MESG) {
7742 if (hle == NULL || hle->logmsg != NULL) {
7743 err = got_error(GOT_ERR_HISTEDIT_CMD);
7744 break;
7746 if (p[0] == '\0') {
7747 err = histedit_edit_logmsg(hle, repo);
7748 if (err)
7749 break;
7750 } else {
7751 hle->logmsg = strdup(p);
7752 if (hle->logmsg == NULL) {
7753 err = got_error_from_errno("strdup");
7754 break;
7757 free(line);
7758 line = NULL;
7759 continue;
7760 } else {
7761 end = p;
7762 while (end[0] && !isspace((unsigned char)end[0]))
7763 end++;
7764 *end = '\0';
7766 err = got_object_resolve_id_str(&commit_id, repo, p);
7767 if (err) {
7768 /* override error code */
7769 err = histedit_syntax_error(lineno);
7770 break;
7773 hle = malloc(sizeof(*hle));
7774 if (hle == NULL) {
7775 err = got_error_from_errno("malloc");
7776 break;
7778 hle->cmd = cmd;
7779 hle->commit_id = commit_id;
7780 hle->logmsg = NULL;
7781 commit_id = NULL;
7782 free(line);
7783 line = NULL;
7784 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7787 free(line);
7788 free(commit_id);
7789 return err;
7792 static const struct got_error *
7793 histedit_check_script(struct got_histedit_list *histedit_cmds,
7794 struct got_object_id_queue *commits, struct got_repository *repo)
7796 const struct got_error *err = NULL;
7797 struct got_object_qid *qid;
7798 struct got_histedit_list_entry *hle;
7799 static char msg[92];
7800 char *id_str;
7802 if (TAILQ_EMPTY(histedit_cmds))
7803 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7804 "histedit script contains no commands");
7805 if (SIMPLEQ_EMPTY(commits))
7806 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7808 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7809 struct got_histedit_list_entry *hle2;
7810 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7811 if (hle == hle2)
7812 continue;
7813 if (got_object_id_cmp(hle->commit_id,
7814 hle2->commit_id) != 0)
7815 continue;
7816 err = got_object_id_str(&id_str, hle->commit_id);
7817 if (err)
7818 return err;
7819 snprintf(msg, sizeof(msg), "commit %s is listed "
7820 "more than once in histedit script", id_str);
7821 free(id_str);
7822 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7826 SIMPLEQ_FOREACH(qid, commits, entry) {
7827 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7828 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7829 break;
7831 if (hle == NULL) {
7832 err = got_object_id_str(&id_str, qid->id);
7833 if (err)
7834 return err;
7835 snprintf(msg, sizeof(msg),
7836 "commit %s missing from histedit script", id_str);
7837 free(id_str);
7838 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7842 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7843 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7844 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7845 "last commit in histedit script cannot be folded");
7847 return NULL;
7850 static const struct got_error *
7851 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7852 const char *path, struct got_object_id_queue *commits,
7853 struct got_repository *repo)
7855 const struct got_error *err = NULL;
7856 char *editor;
7857 FILE *f = NULL;
7859 err = get_editor(&editor);
7860 if (err)
7861 return err;
7863 if (spawn_editor(editor, path) == -1) {
7864 err = got_error_from_errno("failed spawning editor");
7865 goto done;
7868 f = fopen(path, "r");
7869 if (f == NULL) {
7870 err = got_error_from_errno("fopen");
7871 goto done;
7873 err = histedit_parse_list(histedit_cmds, f, repo);
7874 if (err)
7875 goto done;
7877 err = histedit_check_script(histedit_cmds, commits, repo);
7878 done:
7879 if (f && fclose(f) != 0 && err == NULL)
7880 err = got_error_from_errno("fclose");
7881 free(editor);
7882 return err;
7885 static const struct got_error *
7886 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7887 struct got_object_id_queue *, const char *, const char *,
7888 struct got_repository *);
7890 static const struct got_error *
7891 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7892 struct got_object_id_queue *commits, const char *branch_name,
7893 int edit_logmsg_only, struct got_repository *repo)
7895 const struct got_error *err;
7896 FILE *f = NULL;
7897 char *path = NULL;
7899 err = got_opentemp_named(&path, &f, "got-histedit");
7900 if (err)
7901 return err;
7903 err = write_cmd_list(f, branch_name, commits);
7904 if (err)
7905 goto done;
7907 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7908 if (err)
7909 goto done;
7911 if (edit_logmsg_only) {
7912 rewind(f);
7913 err = histedit_parse_list(histedit_cmds, f, repo);
7914 } else {
7915 if (fclose(f) != 0) {
7916 err = got_error_from_errno("fclose");
7917 goto done;
7919 f = NULL;
7920 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7921 if (err) {
7922 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7923 err->code != GOT_ERR_HISTEDIT_CMD)
7924 goto done;
7925 err = histedit_edit_list_retry(histedit_cmds, err,
7926 commits, path, branch_name, repo);
7929 done:
7930 if (f && fclose(f) != 0 && err == NULL)
7931 err = got_error_from_errno("fclose");
7932 if (path && unlink(path) != 0 && err == NULL)
7933 err = got_error_from_errno2("unlink", path);
7934 free(path);
7935 return err;
7938 static const struct got_error *
7939 histedit_save_list(struct got_histedit_list *histedit_cmds,
7940 struct got_worktree *worktree, struct got_repository *repo)
7942 const struct got_error *err = NULL;
7943 char *path = NULL;
7944 FILE *f = NULL;
7945 struct got_histedit_list_entry *hle;
7946 struct got_commit_object *commit = NULL;
7948 err = got_worktree_get_histedit_script_path(&path, worktree);
7949 if (err)
7950 return err;
7952 f = fopen(path, "w");
7953 if (f == NULL) {
7954 err = got_error_from_errno2("fopen", path);
7955 goto done;
7957 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7958 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7959 repo);
7960 if (err)
7961 break;
7963 if (hle->logmsg) {
7964 int n = fprintf(f, "%c %s\n",
7965 GOT_HISTEDIT_MESG, hle->logmsg);
7966 if (n < 0) {
7967 err = got_ferror(f, GOT_ERR_IO);
7968 break;
7972 done:
7973 if (f && fclose(f) != 0 && err == NULL)
7974 err = got_error_from_errno("fclose");
7975 free(path);
7976 if (commit)
7977 got_object_commit_close(commit);
7978 return err;
7981 void
7982 histedit_free_list(struct got_histedit_list *histedit_cmds)
7984 struct got_histedit_list_entry *hle;
7986 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7987 TAILQ_REMOVE(histedit_cmds, hle, entry);
7988 free(hle);
7992 static const struct got_error *
7993 histedit_load_list(struct got_histedit_list *histedit_cmds,
7994 const char *path, struct got_repository *repo)
7996 const struct got_error *err = NULL;
7997 FILE *f = NULL;
7999 f = fopen(path, "r");
8000 if (f == NULL) {
8001 err = got_error_from_errno2("fopen", path);
8002 goto done;
8005 err = histedit_parse_list(histedit_cmds, f, repo);
8006 done:
8007 if (f && fclose(f) != 0 && err == NULL)
8008 err = got_error_from_errno("fclose");
8009 return err;
8012 static const struct got_error *
8013 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8014 const struct got_error *edit_err, struct got_object_id_queue *commits,
8015 const char *path, const char *branch_name, struct got_repository *repo)
8017 const struct got_error *err = NULL, *prev_err = edit_err;
8018 int resp = ' ';
8020 while (resp != 'c' && resp != 'r' && resp != 'a') {
8021 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8022 "or (a)bort: ", getprogname(), prev_err->msg);
8023 resp = getchar();
8024 if (resp == '\n')
8025 resp = getchar();
8026 if (resp == 'c') {
8027 histedit_free_list(histedit_cmds);
8028 err = histedit_run_editor(histedit_cmds, path, commits,
8029 repo);
8030 if (err) {
8031 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8032 err->code != GOT_ERR_HISTEDIT_CMD)
8033 break;
8034 prev_err = err;
8035 resp = ' ';
8036 continue;
8038 break;
8039 } else if (resp == 'r') {
8040 histedit_free_list(histedit_cmds);
8041 err = histedit_edit_script(histedit_cmds,
8042 commits, branch_name, 0, repo);
8043 if (err) {
8044 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8045 err->code != GOT_ERR_HISTEDIT_CMD)
8046 break;
8047 prev_err = err;
8048 resp = ' ';
8049 continue;
8051 break;
8052 } else if (resp == 'a') {
8053 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8054 break;
8055 } else
8056 printf("invalid response '%c'\n", resp);
8059 return err;
8062 static const struct got_error *
8063 histedit_complete(struct got_worktree *worktree,
8064 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8065 struct got_reference *branch, struct got_repository *repo)
8067 printf("Switching work tree to %s\n",
8068 got_ref_get_symref_target(branch));
8069 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8070 branch, repo);
8073 static const struct got_error *
8074 show_histedit_progress(struct got_commit_object *commit,
8075 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8077 const struct got_error *err;
8078 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8080 err = got_object_id_str(&old_id_str, hle->commit_id);
8081 if (err)
8082 goto done;
8084 if (new_id) {
8085 err = got_object_id_str(&new_id_str, new_id);
8086 if (err)
8087 goto done;
8090 old_id_str[12] = '\0';
8091 if (new_id_str)
8092 new_id_str[12] = '\0';
8094 if (hle->logmsg) {
8095 logmsg = strdup(hle->logmsg);
8096 if (logmsg == NULL) {
8097 err = got_error_from_errno("strdup");
8098 goto done;
8100 trim_logmsg(logmsg, 42);
8101 } else {
8102 err = get_short_logmsg(&logmsg, 42, commit);
8103 if (err)
8104 goto done;
8107 switch (hle->cmd->code) {
8108 case GOT_HISTEDIT_PICK:
8109 case GOT_HISTEDIT_EDIT:
8110 printf("%s -> %s: %s\n", old_id_str,
8111 new_id_str ? new_id_str : "no-op change", logmsg);
8112 break;
8113 case GOT_HISTEDIT_DROP:
8114 case GOT_HISTEDIT_FOLD:
8115 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8116 logmsg);
8117 break;
8118 default:
8119 break;
8121 done:
8122 free(old_id_str);
8123 free(new_id_str);
8124 return err;
8127 static const struct got_error *
8128 histedit_commit(struct got_pathlist_head *merged_paths,
8129 struct got_worktree *worktree, struct got_fileindex *fileindex,
8130 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8131 struct got_repository *repo)
8133 const struct got_error *err;
8134 struct got_commit_object *commit;
8135 struct got_object_id *new_commit_id;
8137 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8138 && hle->logmsg == NULL) {
8139 err = histedit_edit_logmsg(hle, repo);
8140 if (err)
8141 return err;
8144 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8145 if (err)
8146 return err;
8148 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8149 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8150 hle->logmsg, repo);
8151 if (err) {
8152 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8153 goto done;
8154 err = show_histedit_progress(commit, hle, NULL);
8155 } else {
8156 err = show_histedit_progress(commit, hle, new_commit_id);
8157 free(new_commit_id);
8159 done:
8160 got_object_commit_close(commit);
8161 return err;
8164 static const struct got_error *
8165 histedit_skip_commit(struct got_histedit_list_entry *hle,
8166 struct got_worktree *worktree, struct got_repository *repo)
8168 const struct got_error *error;
8169 struct got_commit_object *commit;
8171 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8172 repo);
8173 if (error)
8174 return error;
8176 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8177 if (error)
8178 return error;
8180 error = show_histedit_progress(commit, hle, NULL);
8181 got_object_commit_close(commit);
8182 return error;
8185 static const struct got_error *
8186 check_local_changes(void *arg, unsigned char status,
8187 unsigned char staged_status, const char *path,
8188 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8189 struct got_object_id *commit_id, int dirfd, const char *de_name)
8191 int *have_local_changes = arg;
8193 switch (status) {
8194 case GOT_STATUS_ADD:
8195 case GOT_STATUS_DELETE:
8196 case GOT_STATUS_MODIFY:
8197 case GOT_STATUS_CONFLICT:
8198 *have_local_changes = 1;
8199 return got_error(GOT_ERR_CANCELLED);
8200 default:
8201 break;
8204 switch (staged_status) {
8205 case GOT_STATUS_ADD:
8206 case GOT_STATUS_DELETE:
8207 case GOT_STATUS_MODIFY:
8208 *have_local_changes = 1;
8209 return got_error(GOT_ERR_CANCELLED);
8210 default:
8211 break;
8214 return NULL;
8217 static const struct got_error *
8218 cmd_histedit(int argc, char *argv[])
8220 const struct got_error *error = NULL;
8221 struct got_worktree *worktree = NULL;
8222 struct got_fileindex *fileindex = NULL;
8223 struct got_repository *repo = NULL;
8224 char *cwd = NULL;
8225 struct got_reference *branch = NULL;
8226 struct got_reference *tmp_branch = NULL;
8227 struct got_object_id *resume_commit_id = NULL;
8228 struct got_object_id *base_commit_id = NULL;
8229 struct got_object_id *head_commit_id = NULL;
8230 struct got_commit_object *commit = NULL;
8231 int ch, rebase_in_progress = 0;
8232 struct got_update_progress_arg upa;
8233 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8234 int edit_logmsg_only = 0;
8235 const char *edit_script_path = NULL;
8236 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8237 struct got_object_id_queue commits;
8238 struct got_pathlist_head merged_paths;
8239 const struct got_object_id_queue *parent_ids;
8240 struct got_object_qid *pid;
8241 struct got_histedit_list histedit_cmds;
8242 struct got_histedit_list_entry *hle;
8244 SIMPLEQ_INIT(&commits);
8245 TAILQ_INIT(&histedit_cmds);
8246 TAILQ_INIT(&merged_paths);
8247 memset(&upa, 0, sizeof(upa));
8249 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8250 switch (ch) {
8251 case 'a':
8252 abort_edit = 1;
8253 break;
8254 case 'c':
8255 continue_edit = 1;
8256 break;
8257 case 'F':
8258 edit_script_path = optarg;
8259 break;
8260 case 'm':
8261 edit_logmsg_only = 1;
8262 break;
8263 default:
8264 usage_histedit();
8265 /* NOTREACHED */
8269 argc -= optind;
8270 argv += optind;
8272 #ifndef PROFILE
8273 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8274 "unveil", NULL) == -1)
8275 err(1, "pledge");
8276 #endif
8277 if (abort_edit && continue_edit)
8278 errx(1, "histedit's -a and -c options are mutually exclusive");
8279 if (edit_script_path && edit_logmsg_only)
8280 errx(1, "histedit's -F and -m options are mutually exclusive");
8281 if (abort_edit && edit_logmsg_only)
8282 errx(1, "histedit's -a and -m options are mutually exclusive");
8283 if (continue_edit && edit_logmsg_only)
8284 errx(1, "histedit's -c and -m options are mutually exclusive");
8285 if (argc != 0)
8286 usage_histedit();
8289 * This command cannot apply unveil(2) in all cases because the
8290 * user may choose to run an editor to edit the histedit script
8291 * and to edit individual commit log messages.
8292 * unveil(2) traverses exec(2); if an editor is used we have to
8293 * apply unveil after edit script and log messages have been written.
8294 * XXX TODO: Make use of unveil(2) where possible.
8297 cwd = getcwd(NULL, 0);
8298 if (cwd == NULL) {
8299 error = got_error_from_errno("getcwd");
8300 goto done;
8302 error = got_worktree_open(&worktree, cwd);
8303 if (error) {
8304 if (error->code == GOT_ERR_NOT_WORKTREE)
8305 error = wrap_not_worktree_error(error, "histedit", cwd);
8306 goto done;
8309 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8310 NULL);
8311 if (error != NULL)
8312 goto done;
8314 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8315 if (error)
8316 goto done;
8317 if (rebase_in_progress) {
8318 error = got_error(GOT_ERR_REBASING);
8319 goto done;
8322 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8323 if (error)
8324 goto done;
8326 if (edit_in_progress && edit_logmsg_only) {
8327 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8328 "histedit operation is in progress in this "
8329 "work tree and must be continued or aborted "
8330 "before the -m option can be used");
8331 goto done;
8334 if (edit_in_progress && abort_edit) {
8335 error = got_worktree_histedit_continue(&resume_commit_id,
8336 &tmp_branch, &branch, &base_commit_id, &fileindex,
8337 worktree, repo);
8338 if (error)
8339 goto done;
8340 printf("Switching work tree to %s\n",
8341 got_ref_get_symref_target(branch));
8342 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8343 branch, base_commit_id, update_progress, &upa);
8344 if (error)
8345 goto done;
8346 printf("Histedit of %s aborted\n",
8347 got_ref_get_symref_target(branch));
8348 print_update_progress_stats(&upa);
8349 goto done; /* nothing else to do */
8350 } else if (abort_edit) {
8351 error = got_error(GOT_ERR_NOT_HISTEDIT);
8352 goto done;
8355 if (continue_edit) {
8356 char *path;
8358 if (!edit_in_progress) {
8359 error = got_error(GOT_ERR_NOT_HISTEDIT);
8360 goto done;
8363 error = got_worktree_get_histedit_script_path(&path, worktree);
8364 if (error)
8365 goto done;
8367 error = histedit_load_list(&histedit_cmds, path, repo);
8368 free(path);
8369 if (error)
8370 goto done;
8372 error = got_worktree_histedit_continue(&resume_commit_id,
8373 &tmp_branch, &branch, &base_commit_id, &fileindex,
8374 worktree, repo);
8375 if (error)
8376 goto done;
8378 error = got_ref_resolve(&head_commit_id, repo, branch);
8379 if (error)
8380 goto done;
8382 error = got_object_open_as_commit(&commit, repo,
8383 head_commit_id);
8384 if (error)
8385 goto done;
8386 parent_ids = got_object_commit_get_parent_ids(commit);
8387 pid = SIMPLEQ_FIRST(parent_ids);
8388 if (pid == NULL) {
8389 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8390 goto done;
8392 error = collect_commits(&commits, head_commit_id, pid->id,
8393 base_commit_id, got_worktree_get_path_prefix(worktree),
8394 GOT_ERR_HISTEDIT_PATH, repo);
8395 got_object_commit_close(commit);
8396 commit = NULL;
8397 if (error)
8398 goto done;
8399 } else {
8400 if (edit_in_progress) {
8401 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8402 goto done;
8405 error = got_ref_open(&branch, repo,
8406 got_worktree_get_head_ref_name(worktree), 0);
8407 if (error != NULL)
8408 goto done;
8410 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8411 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8412 "will not edit commit history of a branch outside "
8413 "the \"refs/heads/\" reference namespace");
8414 goto done;
8417 error = got_ref_resolve(&head_commit_id, repo, branch);
8418 got_ref_close(branch);
8419 branch = NULL;
8420 if (error)
8421 goto done;
8423 error = got_object_open_as_commit(&commit, repo,
8424 head_commit_id);
8425 if (error)
8426 goto done;
8427 parent_ids = got_object_commit_get_parent_ids(commit);
8428 pid = SIMPLEQ_FIRST(parent_ids);
8429 if (pid == NULL) {
8430 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8431 goto done;
8433 error = collect_commits(&commits, head_commit_id, pid->id,
8434 got_worktree_get_base_commit_id(worktree),
8435 got_worktree_get_path_prefix(worktree),
8436 GOT_ERR_HISTEDIT_PATH, repo);
8437 got_object_commit_close(commit);
8438 commit = NULL;
8439 if (error)
8440 goto done;
8442 if (SIMPLEQ_EMPTY(&commits)) {
8443 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8444 goto done;
8447 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8448 &base_commit_id, &fileindex, worktree, repo);
8449 if (error)
8450 goto done;
8452 if (edit_script_path) {
8453 error = histedit_load_list(&histedit_cmds,
8454 edit_script_path, repo);
8455 if (error) {
8456 got_worktree_histedit_abort(worktree, fileindex,
8457 repo, branch, base_commit_id,
8458 update_progress, &upa);
8459 print_update_progress_stats(&upa);
8460 goto done;
8462 } else {
8463 const char *branch_name;
8464 branch_name = got_ref_get_symref_target(branch);
8465 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8466 branch_name += 11;
8467 error = histedit_edit_script(&histedit_cmds, &commits,
8468 branch_name, edit_logmsg_only, repo);
8469 if (error) {
8470 got_worktree_histedit_abort(worktree, fileindex,
8471 repo, branch, base_commit_id,
8472 update_progress, &upa);
8473 print_update_progress_stats(&upa);
8474 goto done;
8479 error = histedit_save_list(&histedit_cmds, worktree,
8480 repo);
8481 if (error) {
8482 got_worktree_histedit_abort(worktree, fileindex,
8483 repo, branch, base_commit_id,
8484 update_progress, &upa);
8485 print_update_progress_stats(&upa);
8486 goto done;
8491 error = histedit_check_script(&histedit_cmds, &commits, repo);
8492 if (error)
8493 goto done;
8495 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8496 if (resume_commit_id) {
8497 if (got_object_id_cmp(hle->commit_id,
8498 resume_commit_id) != 0)
8499 continue;
8501 resume_commit_id = NULL;
8502 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8503 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8504 error = histedit_skip_commit(hle, worktree,
8505 repo);
8506 if (error)
8507 goto done;
8508 } else {
8509 struct got_pathlist_head paths;
8510 int have_changes = 0;
8512 TAILQ_INIT(&paths);
8513 error = got_pathlist_append(&paths, "", NULL);
8514 if (error)
8515 goto done;
8516 error = got_worktree_status(worktree, &paths,
8517 repo, check_local_changes, &have_changes,
8518 check_cancelled, NULL);
8519 got_pathlist_free(&paths);
8520 if (error) {
8521 if (error->code != GOT_ERR_CANCELLED)
8522 goto done;
8523 if (sigint_received || sigpipe_received)
8524 goto done;
8526 if (have_changes) {
8527 error = histedit_commit(NULL, worktree,
8528 fileindex, tmp_branch, hle, repo);
8529 if (error)
8530 goto done;
8531 } else {
8532 error = got_object_open_as_commit(
8533 &commit, repo, hle->commit_id);
8534 if (error)
8535 goto done;
8536 error = show_histedit_progress(commit,
8537 hle, NULL);
8538 got_object_commit_close(commit);
8539 commit = NULL;
8540 if (error)
8541 goto done;
8544 continue;
8547 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8548 error = histedit_skip_commit(hle, worktree, repo);
8549 if (error)
8550 goto done;
8551 continue;
8554 error = got_object_open_as_commit(&commit, repo,
8555 hle->commit_id);
8556 if (error)
8557 goto done;
8558 parent_ids = got_object_commit_get_parent_ids(commit);
8559 pid = SIMPLEQ_FIRST(parent_ids);
8561 error = got_worktree_histedit_merge_files(&merged_paths,
8562 worktree, fileindex, pid->id, hle->commit_id, repo,
8563 update_progress, &upa, check_cancelled, NULL);
8564 if (error)
8565 goto done;
8566 got_object_commit_close(commit);
8567 commit = NULL;
8569 print_update_progress_stats(&upa);
8570 if (upa.conflicts > 0)
8571 rebase_status = GOT_STATUS_CONFLICT;
8573 if (rebase_status == GOT_STATUS_CONFLICT) {
8574 error = show_rebase_merge_conflict(hle->commit_id,
8575 repo);
8576 if (error)
8577 goto done;
8578 got_worktree_rebase_pathlist_free(&merged_paths);
8579 break;
8582 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8583 char *id_str;
8584 error = got_object_id_str(&id_str, hle->commit_id);
8585 if (error)
8586 goto done;
8587 printf("Stopping histedit for amending commit %s\n",
8588 id_str);
8589 free(id_str);
8590 got_worktree_rebase_pathlist_free(&merged_paths);
8591 error = got_worktree_histedit_postpone(worktree,
8592 fileindex);
8593 goto done;
8596 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8597 error = histedit_skip_commit(hle, worktree, repo);
8598 if (error)
8599 goto done;
8600 continue;
8603 error = histedit_commit(&merged_paths, worktree, fileindex,
8604 tmp_branch, hle, repo);
8605 got_worktree_rebase_pathlist_free(&merged_paths);
8606 if (error)
8607 goto done;
8610 if (rebase_status == GOT_STATUS_CONFLICT) {
8611 error = got_worktree_histedit_postpone(worktree, fileindex);
8612 if (error)
8613 goto done;
8614 error = got_error_msg(GOT_ERR_CONFLICTS,
8615 "conflicts must be resolved before histedit can continue");
8616 } else
8617 error = histedit_complete(worktree, fileindex, tmp_branch,
8618 branch, repo);
8619 done:
8620 got_object_id_queue_free(&commits);
8621 histedit_free_list(&histedit_cmds);
8622 free(head_commit_id);
8623 free(base_commit_id);
8624 free(resume_commit_id);
8625 if (commit)
8626 got_object_commit_close(commit);
8627 if (branch)
8628 got_ref_close(branch);
8629 if (tmp_branch)
8630 got_ref_close(tmp_branch);
8631 if (worktree)
8632 got_worktree_close(worktree);
8633 if (repo)
8634 got_repo_close(repo);
8635 return error;
8638 __dead static void
8639 usage_integrate(void)
8641 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8642 exit(1);
8645 static const struct got_error *
8646 cmd_integrate(int argc, char *argv[])
8648 const struct got_error *error = NULL;
8649 struct got_repository *repo = NULL;
8650 struct got_worktree *worktree = NULL;
8651 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8652 const char *branch_arg = NULL;
8653 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8654 struct got_fileindex *fileindex = NULL;
8655 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8656 int ch;
8657 struct got_update_progress_arg upa;
8659 while ((ch = getopt(argc, argv, "")) != -1) {
8660 switch (ch) {
8661 default:
8662 usage_integrate();
8663 /* NOTREACHED */
8667 argc -= optind;
8668 argv += optind;
8670 if (argc != 1)
8671 usage_integrate();
8672 branch_arg = argv[0];
8674 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8675 "unveil", NULL) == -1)
8676 err(1, "pledge");
8678 cwd = getcwd(NULL, 0);
8679 if (cwd == NULL) {
8680 error = got_error_from_errno("getcwd");
8681 goto done;
8684 error = got_worktree_open(&worktree, cwd);
8685 if (error) {
8686 if (error->code == GOT_ERR_NOT_WORKTREE)
8687 error = wrap_not_worktree_error(error, "integrate",
8688 cwd);
8689 goto done;
8692 error = check_rebase_or_histedit_in_progress(worktree);
8693 if (error)
8694 goto done;
8696 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8697 NULL);
8698 if (error != NULL)
8699 goto done;
8701 error = apply_unveil(got_repo_get_path(repo), 0,
8702 got_worktree_get_root_path(worktree));
8703 if (error)
8704 goto done;
8706 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8707 error = got_error_from_errno("asprintf");
8708 goto done;
8711 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8712 &base_branch_ref, worktree, refname, repo);
8713 if (error)
8714 goto done;
8716 refname = strdup(got_ref_get_name(branch_ref));
8717 if (refname == NULL) {
8718 error = got_error_from_errno("strdup");
8719 got_worktree_integrate_abort(worktree, fileindex, repo,
8720 branch_ref, base_branch_ref);
8721 goto done;
8723 base_refname = strdup(got_ref_get_name(base_branch_ref));
8724 if (base_refname == NULL) {
8725 error = got_error_from_errno("strdup");
8726 got_worktree_integrate_abort(worktree, fileindex, repo,
8727 branch_ref, base_branch_ref);
8728 goto done;
8731 error = got_ref_resolve(&commit_id, repo, branch_ref);
8732 if (error)
8733 goto done;
8735 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8736 if (error)
8737 goto done;
8739 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8740 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8741 "specified branch has already been integrated");
8742 got_worktree_integrate_abort(worktree, fileindex, repo,
8743 branch_ref, base_branch_ref);
8744 goto done;
8747 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8748 if (error) {
8749 if (error->code == GOT_ERR_ANCESTRY)
8750 error = got_error(GOT_ERR_REBASE_REQUIRED);
8751 got_worktree_integrate_abort(worktree, fileindex, repo,
8752 branch_ref, base_branch_ref);
8753 goto done;
8756 memset(&upa, 0, sizeof(upa));
8757 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8758 branch_ref, base_branch_ref, update_progress, &upa,
8759 check_cancelled, NULL);
8760 if (error)
8761 goto done;
8763 printf("Integrated %s into %s\n", refname, base_refname);
8764 print_update_progress_stats(&upa);
8765 done:
8766 if (repo)
8767 got_repo_close(repo);
8768 if (worktree)
8769 got_worktree_close(worktree);
8770 free(cwd);
8771 free(base_commit_id);
8772 free(commit_id);
8773 free(refname);
8774 free(base_refname);
8775 return error;
8778 __dead static void
8779 usage_stage(void)
8781 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8782 "[-S] [file-path ...]\n",
8783 getprogname());
8784 exit(1);
8787 static const struct got_error *
8788 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8789 const char *path, struct got_object_id *blob_id,
8790 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8791 int dirfd, const char *de_name)
8793 const struct got_error *err = NULL;
8794 char *id_str = NULL;
8796 if (staged_status != GOT_STATUS_ADD &&
8797 staged_status != GOT_STATUS_MODIFY &&
8798 staged_status != GOT_STATUS_DELETE)
8799 return NULL;
8801 if (staged_status == GOT_STATUS_ADD ||
8802 staged_status == GOT_STATUS_MODIFY)
8803 err = got_object_id_str(&id_str, staged_blob_id);
8804 else
8805 err = got_object_id_str(&id_str, blob_id);
8806 if (err)
8807 return err;
8809 printf("%s %c %s\n", id_str, staged_status, path);
8810 free(id_str);
8811 return NULL;
8814 static const struct got_error *
8815 cmd_stage(int argc, char *argv[])
8817 const struct got_error *error = NULL;
8818 struct got_repository *repo = NULL;
8819 struct got_worktree *worktree = NULL;
8820 char *cwd = NULL;
8821 struct got_pathlist_head paths;
8822 struct got_pathlist_entry *pe;
8823 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8824 FILE *patch_script_file = NULL;
8825 const char *patch_script_path = NULL;
8826 struct choose_patch_arg cpa;
8828 TAILQ_INIT(&paths);
8830 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8831 switch (ch) {
8832 case 'l':
8833 list_stage = 1;
8834 break;
8835 case 'p':
8836 pflag = 1;
8837 break;
8838 case 'F':
8839 patch_script_path = optarg;
8840 break;
8841 case 'S':
8842 allow_bad_symlinks = 1;
8843 break;
8844 default:
8845 usage_stage();
8846 /* NOTREACHED */
8850 argc -= optind;
8851 argv += optind;
8853 #ifndef PROFILE
8854 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8855 "unveil", NULL) == -1)
8856 err(1, "pledge");
8857 #endif
8858 if (list_stage && (pflag || patch_script_path))
8859 errx(1, "-l option cannot be used with other options");
8860 if (patch_script_path && !pflag)
8861 errx(1, "-F option can only be used together with -p option");
8863 cwd = getcwd(NULL, 0);
8864 if (cwd == NULL) {
8865 error = got_error_from_errno("getcwd");
8866 goto done;
8869 error = got_worktree_open(&worktree, cwd);
8870 if (error) {
8871 if (error->code == GOT_ERR_NOT_WORKTREE)
8872 error = wrap_not_worktree_error(error, "stage", cwd);
8873 goto done;
8876 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8877 NULL);
8878 if (error != NULL)
8879 goto done;
8881 if (patch_script_path) {
8882 patch_script_file = fopen(patch_script_path, "r");
8883 if (patch_script_file == NULL) {
8884 error = got_error_from_errno2("fopen",
8885 patch_script_path);
8886 goto done;
8889 error = apply_unveil(got_repo_get_path(repo), 0,
8890 got_worktree_get_root_path(worktree));
8891 if (error)
8892 goto done;
8894 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8895 if (error)
8896 goto done;
8898 if (list_stage)
8899 error = got_worktree_status(worktree, &paths, repo,
8900 print_stage, NULL, check_cancelled, NULL);
8901 else {
8902 cpa.patch_script_file = patch_script_file;
8903 cpa.action = "stage";
8904 error = got_worktree_stage(worktree, &paths,
8905 pflag ? NULL : print_status, NULL,
8906 pflag ? choose_patch : NULL, &cpa,
8907 allow_bad_symlinks, repo);
8909 done:
8910 if (patch_script_file && fclose(patch_script_file) == EOF &&
8911 error == NULL)
8912 error = got_error_from_errno2("fclose", patch_script_path);
8913 if (repo)
8914 got_repo_close(repo);
8915 if (worktree)
8916 got_worktree_close(worktree);
8917 TAILQ_FOREACH(pe, &paths, entry)
8918 free((char *)pe->path);
8919 got_pathlist_free(&paths);
8920 free(cwd);
8921 return error;
8924 __dead static void
8925 usage_unstage(void)
8927 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8928 "[file-path ...]\n",
8929 getprogname());
8930 exit(1);
8934 static const struct got_error *
8935 cmd_unstage(int argc, char *argv[])
8937 const struct got_error *error = NULL;
8938 struct got_repository *repo = NULL;
8939 struct got_worktree *worktree = NULL;
8940 char *cwd = NULL;
8941 struct got_pathlist_head paths;
8942 struct got_pathlist_entry *pe;
8943 int ch, pflag = 0;
8944 struct got_update_progress_arg upa;
8945 FILE *patch_script_file = NULL;
8946 const char *patch_script_path = NULL;
8947 struct choose_patch_arg cpa;
8949 TAILQ_INIT(&paths);
8951 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8952 switch (ch) {
8953 case 'p':
8954 pflag = 1;
8955 break;
8956 case 'F':
8957 patch_script_path = optarg;
8958 break;
8959 default:
8960 usage_unstage();
8961 /* NOTREACHED */
8965 argc -= optind;
8966 argv += optind;
8968 #ifndef PROFILE
8969 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8970 "unveil", NULL) == -1)
8971 err(1, "pledge");
8972 #endif
8973 if (patch_script_path && !pflag)
8974 errx(1, "-F option can only be used together with -p option");
8976 cwd = getcwd(NULL, 0);
8977 if (cwd == NULL) {
8978 error = got_error_from_errno("getcwd");
8979 goto done;
8982 error = got_worktree_open(&worktree, cwd);
8983 if (error) {
8984 if (error->code == GOT_ERR_NOT_WORKTREE)
8985 error = wrap_not_worktree_error(error, "unstage", cwd);
8986 goto done;
8989 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8990 NULL);
8991 if (error != NULL)
8992 goto done;
8994 if (patch_script_path) {
8995 patch_script_file = fopen(patch_script_path, "r");
8996 if (patch_script_file == NULL) {
8997 error = got_error_from_errno2("fopen",
8998 patch_script_path);
8999 goto done;
9003 error = apply_unveil(got_repo_get_path(repo), 0,
9004 got_worktree_get_root_path(worktree));
9005 if (error)
9006 goto done;
9008 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9009 if (error)
9010 goto done;
9012 cpa.patch_script_file = patch_script_file;
9013 cpa.action = "unstage";
9014 memset(&upa, 0, sizeof(upa));
9015 error = got_worktree_unstage(worktree, &paths, update_progress,
9016 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9017 if (!error)
9018 print_update_progress_stats(&upa);
9019 done:
9020 if (patch_script_file && fclose(patch_script_file) == EOF &&
9021 error == NULL)
9022 error = got_error_from_errno2("fclose", patch_script_path);
9023 if (repo)
9024 got_repo_close(repo);
9025 if (worktree)
9026 got_worktree_close(worktree);
9027 TAILQ_FOREACH(pe, &paths, entry)
9028 free((char *)pe->path);
9029 got_pathlist_free(&paths);
9030 free(cwd);
9031 return error;
9034 __dead static void
9035 usage_cat(void)
9037 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9038 "arg1 [arg2 ...]\n", getprogname());
9039 exit(1);
9042 static const struct got_error *
9043 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9045 const struct got_error *err;
9046 struct got_blob_object *blob;
9048 err = got_object_open_as_blob(&blob, repo, id, 8192);
9049 if (err)
9050 return err;
9052 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9053 got_object_blob_close(blob);
9054 return err;
9057 static const struct got_error *
9058 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9060 const struct got_error *err;
9061 struct got_tree_object *tree;
9062 int nentries, i;
9064 err = got_object_open_as_tree(&tree, repo, id);
9065 if (err)
9066 return err;
9068 nentries = got_object_tree_get_nentries(tree);
9069 for (i = 0; i < nentries; i++) {
9070 struct got_tree_entry *te;
9071 char *id_str;
9072 if (sigint_received || sigpipe_received)
9073 break;
9074 te = got_object_tree_get_entry(tree, i);
9075 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9076 if (err)
9077 break;
9078 fprintf(outfile, "%s %.7o %s\n", id_str,
9079 got_tree_entry_get_mode(te),
9080 got_tree_entry_get_name(te));
9081 free(id_str);
9084 got_object_tree_close(tree);
9085 return err;
9088 static const struct got_error *
9089 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9091 const struct got_error *err;
9092 struct got_commit_object *commit;
9093 const struct got_object_id_queue *parent_ids;
9094 struct got_object_qid *pid;
9095 char *id_str = NULL;
9096 const char *logmsg = NULL;
9098 err = got_object_open_as_commit(&commit, repo, id);
9099 if (err)
9100 return err;
9102 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9103 if (err)
9104 goto done;
9106 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9107 parent_ids = got_object_commit_get_parent_ids(commit);
9108 fprintf(outfile, "numparents %d\n",
9109 got_object_commit_get_nparents(commit));
9110 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9111 char *pid_str;
9112 err = got_object_id_str(&pid_str, pid->id);
9113 if (err)
9114 goto done;
9115 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9116 free(pid_str);
9118 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9119 got_object_commit_get_author(commit),
9120 got_object_commit_get_author_time(commit));
9122 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9123 got_object_commit_get_author(commit),
9124 got_object_commit_get_committer_time(commit));
9126 logmsg = got_object_commit_get_logmsg_raw(commit);
9127 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9128 fprintf(outfile, "%s", logmsg);
9129 done:
9130 free(id_str);
9131 got_object_commit_close(commit);
9132 return err;
9135 static const struct got_error *
9136 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9138 const struct got_error *err;
9139 struct got_tag_object *tag;
9140 char *id_str = NULL;
9141 const char *tagmsg = NULL;
9143 err = got_object_open_as_tag(&tag, repo, id);
9144 if (err)
9145 return err;
9147 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9148 if (err)
9149 goto done;
9151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9153 switch (got_object_tag_get_object_type(tag)) {
9154 case GOT_OBJ_TYPE_BLOB:
9155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9156 GOT_OBJ_LABEL_BLOB);
9157 break;
9158 case GOT_OBJ_TYPE_TREE:
9159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9160 GOT_OBJ_LABEL_TREE);
9161 break;
9162 case GOT_OBJ_TYPE_COMMIT:
9163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9164 GOT_OBJ_LABEL_COMMIT);
9165 break;
9166 case GOT_OBJ_TYPE_TAG:
9167 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9168 GOT_OBJ_LABEL_TAG);
9169 break;
9170 default:
9171 break;
9174 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9175 got_object_tag_get_name(tag));
9177 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9178 got_object_tag_get_tagger(tag),
9179 got_object_tag_get_tagger_time(tag));
9181 tagmsg = got_object_tag_get_message(tag);
9182 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9183 fprintf(outfile, "%s", tagmsg);
9184 done:
9185 free(id_str);
9186 got_object_tag_close(tag);
9187 return err;
9190 static const struct got_error *
9191 cmd_cat(int argc, char *argv[])
9193 const struct got_error *error;
9194 struct got_repository *repo = NULL;
9195 struct got_worktree *worktree = NULL;
9196 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9197 const char *commit_id_str = NULL;
9198 struct got_object_id *id = NULL, *commit_id = NULL;
9199 int ch, obj_type, i, force_path = 0;
9201 #ifndef PROFILE
9202 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9203 NULL) == -1)
9204 err(1, "pledge");
9205 #endif
9207 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9208 switch (ch) {
9209 case 'c':
9210 commit_id_str = optarg;
9211 break;
9212 case 'r':
9213 repo_path = realpath(optarg, NULL);
9214 if (repo_path == NULL)
9215 return got_error_from_errno2("realpath",
9216 optarg);
9217 got_path_strip_trailing_slashes(repo_path);
9218 break;
9219 case 'P':
9220 force_path = 1;
9221 break;
9222 default:
9223 usage_cat();
9224 /* NOTREACHED */
9228 argc -= optind;
9229 argv += optind;
9231 cwd = getcwd(NULL, 0);
9232 if (cwd == NULL) {
9233 error = got_error_from_errno("getcwd");
9234 goto done;
9236 error = got_worktree_open(&worktree, cwd);
9237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9238 goto done;
9239 if (worktree) {
9240 if (repo_path == NULL) {
9241 repo_path = strdup(
9242 got_worktree_get_repo_path(worktree));
9243 if (repo_path == NULL) {
9244 error = got_error_from_errno("strdup");
9245 goto done;
9250 if (repo_path == NULL) {
9251 repo_path = getcwd(NULL, 0);
9252 if (repo_path == NULL)
9253 return got_error_from_errno("getcwd");
9256 error = got_repo_open(&repo, repo_path, NULL);
9257 free(repo_path);
9258 if (error != NULL)
9259 goto done;
9261 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9262 if (error)
9263 goto done;
9265 if (commit_id_str == NULL)
9266 commit_id_str = GOT_REF_HEAD;
9267 error = got_repo_match_object_id(&commit_id, NULL,
9268 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9269 if (error)
9270 goto done;
9272 for (i = 0; i < argc; i++) {
9273 if (force_path) {
9274 error = got_object_id_by_path(&id, repo, commit_id,
9275 argv[i]);
9276 if (error)
9277 break;
9278 } else {
9279 error = got_repo_match_object_id(&id, &label, argv[i],
9280 GOT_OBJ_TYPE_ANY, 0, repo);
9281 if (error) {
9282 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9283 error->code != GOT_ERR_NOT_REF)
9284 break;
9285 error = got_object_id_by_path(&id, repo,
9286 commit_id, argv[i]);
9287 if (error)
9288 break;
9292 error = got_object_get_type(&obj_type, repo, id);
9293 if (error)
9294 break;
9296 switch (obj_type) {
9297 case GOT_OBJ_TYPE_BLOB:
9298 error = cat_blob(id, repo, stdout);
9299 break;
9300 case GOT_OBJ_TYPE_TREE:
9301 error = cat_tree(id, repo, stdout);
9302 break;
9303 case GOT_OBJ_TYPE_COMMIT:
9304 error = cat_commit(id, repo, stdout);
9305 break;
9306 case GOT_OBJ_TYPE_TAG:
9307 error = cat_tag(id, repo, stdout);
9308 break;
9309 default:
9310 error = got_error(GOT_ERR_OBJ_TYPE);
9311 break;
9313 if (error)
9314 break;
9315 free(label);
9316 label = NULL;
9317 free(id);
9318 id = NULL;
9320 done:
9321 free(label);
9322 free(id);
9323 free(commit_id);
9324 if (worktree)
9325 got_worktree_close(worktree);
9326 if (repo) {
9327 const struct got_error *repo_error;
9328 repo_error = got_repo_close(repo);
9329 if (error == NULL)
9330 error = repo_error;
9332 return error;
9335 __dead static void
9336 usage_info(void)
9338 fprintf(stderr, "usage: %s info [path ...]\n",
9339 getprogname());
9340 exit(1);
9343 static const struct got_error *
9344 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9346 struct got_object_id *commit_id)
9348 const struct got_error *err = NULL;
9349 char *id_str = NULL;
9350 char datebuf[128];
9351 struct tm mytm, *tm;
9352 struct got_pathlist_head *paths = arg;
9353 struct got_pathlist_entry *pe;
9356 * Clear error indication from any of the path arguments which
9357 * would cause this file index entry to be displayed.
9359 TAILQ_FOREACH(pe, paths, entry) {
9360 if (got_path_cmp(path, pe->path, strlen(path),
9361 pe->path_len) == 0 ||
9362 got_path_is_child(path, pe->path, pe->path_len))
9363 pe->data = NULL; /* no error */
9366 printf(GOT_COMMIT_SEP_STR);
9367 if (S_ISLNK(mode))
9368 printf("symlink: %s\n", path);
9369 else if (S_ISREG(mode)) {
9370 printf("file: %s\n", path);
9371 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9372 } else if (S_ISDIR(mode))
9373 printf("directory: %s\n", path);
9374 else
9375 printf("something: %s\n", path);
9377 tm = localtime_r(&mtime, &mytm);
9378 if (tm == NULL)
9379 return NULL;
9380 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9381 return got_error(GOT_ERR_NO_SPACE);
9382 printf("timestamp: %s\n", datebuf);
9384 if (blob_id) {
9385 err = got_object_id_str(&id_str, blob_id);
9386 if (err)
9387 return err;
9388 printf("based on blob: %s\n", id_str);
9389 free(id_str);
9392 if (staged_blob_id) {
9393 err = got_object_id_str(&id_str, staged_blob_id);
9394 if (err)
9395 return err;
9396 printf("based on staged blob: %s\n", id_str);
9397 free(id_str);
9400 if (commit_id) {
9401 err = got_object_id_str(&id_str, commit_id);
9402 if (err)
9403 return err;
9404 printf("based on commit: %s\n", id_str);
9405 free(id_str);
9408 return NULL;
9411 static const struct got_error *
9412 cmd_info(int argc, char *argv[])
9414 const struct got_error *error = NULL;
9415 struct got_worktree *worktree = NULL;
9416 char *cwd = NULL, *id_str = NULL;
9417 struct got_pathlist_head paths;
9418 struct got_pathlist_entry *pe;
9419 char *uuidstr = NULL;
9420 int ch, show_files = 0;
9422 TAILQ_INIT(&paths);
9424 while ((ch = getopt(argc, argv, "")) != -1) {
9425 switch (ch) {
9426 default:
9427 usage_info();
9428 /* NOTREACHED */
9432 argc -= optind;
9433 argv += optind;
9435 #ifndef PROFILE
9436 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9437 NULL) == -1)
9438 err(1, "pledge");
9439 #endif
9440 cwd = getcwd(NULL, 0);
9441 if (cwd == NULL) {
9442 error = got_error_from_errno("getcwd");
9443 goto done;
9446 error = got_worktree_open(&worktree, cwd);
9447 if (error) {
9448 if (error->code == GOT_ERR_NOT_WORKTREE)
9449 error = wrap_not_worktree_error(error, "status", cwd);
9450 goto done;
9453 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9454 if (error)
9455 goto done;
9457 if (argc >= 1) {
9458 error = get_worktree_paths_from_argv(&paths, argc, argv,
9459 worktree);
9460 if (error)
9461 goto done;
9462 show_files = 1;
9465 error = got_object_id_str(&id_str,
9466 got_worktree_get_base_commit_id(worktree));
9467 if (error)
9468 goto done;
9470 error = got_worktree_get_uuid(&uuidstr, worktree);
9471 if (error)
9472 goto done;
9474 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9475 printf("work tree base commit: %s\n", id_str);
9476 printf("work tree path prefix: %s\n",
9477 got_worktree_get_path_prefix(worktree));
9478 printf("work tree branch reference: %s\n",
9479 got_worktree_get_head_ref_name(worktree));
9480 printf("work tree UUID: %s\n", uuidstr);
9481 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9483 if (show_files) {
9484 struct got_pathlist_entry *pe;
9485 TAILQ_FOREACH(pe, &paths, entry) {
9486 if (pe->path_len == 0)
9487 continue;
9489 * Assume this path will fail. This will be corrected
9490 * in print_path_info() in case the path does suceeed.
9492 pe->data = (void *)got_error_path(pe->path,
9493 GOT_ERR_BAD_PATH);
9495 error = got_worktree_path_info(worktree, &paths,
9496 print_path_info, &paths, check_cancelled, NULL);
9497 if (error)
9498 goto done;
9499 TAILQ_FOREACH(pe, &paths, entry) {
9500 if (pe->data != NULL) {
9501 error = pe->data; /* bad path */
9502 break;
9506 done:
9507 TAILQ_FOREACH(pe, &paths, entry)
9508 free((char *)pe->path);
9509 got_pathlist_free(&paths);
9510 free(cwd);
9511 free(id_str);
9512 free(uuidstr);
9513 return error;