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) {
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 err = print_commit(commit, qid->id, repo, path,
3318 show_changed_paths ? &changed_paths : NULL,
3319 show_patch, diff_context, refs);
3320 got_object_commit_close(commit);
3321 if (err)
3322 break;
3325 done:
3326 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3327 qid = SIMPLEQ_FIRST(&reversed_commits);
3328 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3329 got_object_qid_free(qid);
3331 TAILQ_FOREACH(pe, &changed_paths, entry) {
3332 free((char *)pe->path);
3333 free(pe->data);
3335 got_pathlist_free(&changed_paths);
3336 if (search_pattern)
3337 regfree(&regex);
3338 got_commit_graph_close(graph);
3339 return err;
3342 __dead static void
3343 usage_log(void)
3345 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3346 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3347 "[-R] [path]\n", getprogname());
3348 exit(1);
3351 static int
3352 get_default_log_limit(void)
3354 const char *got_default_log_limit;
3355 long long n;
3356 const char *errstr;
3358 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3359 if (got_default_log_limit == NULL)
3360 return 0;
3361 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3362 if (errstr != NULL)
3363 return 0;
3364 return n;
3367 static const struct got_error *
3368 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3369 struct got_repository *repo)
3371 const struct got_error *err = NULL;
3372 struct got_reference *ref;
3374 *id = NULL;
3376 err = got_ref_open(&ref, repo, commit_arg, 0);
3377 if (err == NULL) {
3378 int obj_type;
3379 err = got_ref_resolve(id, repo, ref);
3380 got_ref_close(ref);
3381 if (err)
3382 return err;
3383 err = got_object_get_type(&obj_type, repo, *id);
3384 if (err)
3385 return err;
3386 if (obj_type == GOT_OBJ_TYPE_TAG) {
3387 struct got_tag_object *tag;
3388 err = got_object_open_as_tag(&tag, repo, *id);
3389 if (err)
3390 return err;
3391 if (got_object_tag_get_object_type(tag) !=
3392 GOT_OBJ_TYPE_COMMIT) {
3393 got_object_tag_close(tag);
3394 return got_error(GOT_ERR_OBJ_TYPE);
3396 free(*id);
3397 *id = got_object_id_dup(
3398 got_object_tag_get_object_id(tag));
3399 if (*id == NULL)
3400 err = got_error_from_errno(
3401 "got_object_id_dup");
3402 got_object_tag_close(tag);
3403 if (err)
3404 return err;
3405 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3406 return got_error(GOT_ERR_OBJ_TYPE);
3407 } else {
3408 err = got_repo_match_object_id_prefix(id, commit_arg,
3409 GOT_OBJ_TYPE_COMMIT, repo);
3412 return err;
3415 static const struct got_error *
3416 cmd_log(int argc, char *argv[])
3418 const struct got_error *error;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 struct got_object_id *start_id = NULL, *end_id = NULL;
3422 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3423 const char *start_commit = NULL, *end_commit = NULL;
3424 const char *search_pattern = NULL;
3425 int diff_context = -1, ch;
3426 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3427 int reverse_display_order = 0;
3428 const char *errstr;
3429 struct got_reflist_head refs;
3431 SIMPLEQ_INIT(&refs);
3433 #ifndef PROFILE
3434 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3435 NULL)
3436 == -1)
3437 err(1, "pledge");
3438 #endif
3440 limit = get_default_log_limit();
3442 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3443 switch (ch) {
3444 case 'p':
3445 show_patch = 1;
3446 break;
3447 case 'P':
3448 show_changed_paths = 1;
3449 break;
3450 case 'c':
3451 start_commit = optarg;
3452 break;
3453 case 'C':
3454 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3455 &errstr);
3456 if (errstr != NULL)
3457 err(1, "-C option %s", errstr);
3458 break;
3459 case 'l':
3460 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3461 if (errstr != NULL)
3462 err(1, "-l option %s", errstr);
3463 break;
3464 case 'b':
3465 log_branches = 1;
3466 break;
3467 case 'r':
3468 repo_path = realpath(optarg, NULL);
3469 if (repo_path == NULL)
3470 return got_error_from_errno2("realpath",
3471 optarg);
3472 got_path_strip_trailing_slashes(repo_path);
3473 break;
3474 case 'R':
3475 reverse_display_order = 1;
3476 break;
3477 case 's':
3478 search_pattern = optarg;
3479 break;
3480 case 'x':
3481 end_commit = optarg;
3482 break;
3483 default:
3484 usage_log();
3485 /* NOTREACHED */
3489 argc -= optind;
3490 argv += optind;
3492 if (diff_context == -1)
3493 diff_context = 3;
3494 else if (!show_patch)
3495 errx(1, "-C reguires -p");
3497 cwd = getcwd(NULL, 0);
3498 if (cwd == NULL) {
3499 error = got_error_from_errno("getcwd");
3500 goto done;
3503 if (repo_path == NULL) {
3504 error = got_worktree_open(&worktree, cwd);
3505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3506 goto done;
3507 error = NULL;
3510 if (argc == 0) {
3511 path = strdup("");
3512 if (path == NULL) {
3513 error = got_error_from_errno("strdup");
3514 goto done;
3516 } else if (argc == 1) {
3517 if (worktree) {
3518 error = got_worktree_resolve_path(&path, worktree,
3519 argv[0]);
3520 if (error)
3521 goto done;
3522 } else {
3523 path = strdup(argv[0]);
3524 if (path == NULL) {
3525 error = got_error_from_errno("strdup");
3526 goto done;
3529 } else
3530 usage_log();
3532 if (repo_path == NULL) {
3533 repo_path = worktree ?
3534 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3536 if (repo_path == NULL) {
3537 error = got_error_from_errno("strdup");
3538 goto done;
3541 error = got_repo_open(&repo, repo_path, NULL);
3542 if (error != NULL)
3543 goto done;
3545 error = apply_unveil(got_repo_get_path(repo), 1,
3546 worktree ? got_worktree_get_root_path(worktree) : NULL);
3547 if (error)
3548 goto done;
3550 if (start_commit == NULL) {
3551 struct got_reference *head_ref;
3552 struct got_commit_object *commit = NULL;
3553 error = got_ref_open(&head_ref, repo,
3554 worktree ? got_worktree_get_head_ref_name(worktree)
3555 : GOT_REF_HEAD, 0);
3556 if (error != NULL)
3557 goto done;
3558 error = got_ref_resolve(&start_id, repo, head_ref);
3559 got_ref_close(head_ref);
3560 if (error != NULL)
3561 goto done;
3562 error = got_object_open_as_commit(&commit, repo,
3563 start_id);
3564 if (error != NULL)
3565 goto done;
3566 got_object_commit_close(commit);
3567 } else {
3568 error = resolve_commit_arg(&start_id, start_commit, repo);
3569 if (error != NULL)
3570 goto done;
3572 if (end_commit != NULL) {
3573 error = resolve_commit_arg(&end_id, end_commit, repo);
3574 if (error != NULL)
3575 goto done;
3578 if (worktree) {
3579 const char *prefix = got_worktree_get_path_prefix(worktree);
3580 char *p;
3581 if (asprintf(&p, "%s%s%s", prefix,
3582 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3583 error = got_error_from_errno("asprintf");
3584 goto done;
3586 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3587 free(p);
3588 } else
3589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3590 if (error != NULL)
3591 goto done;
3592 if (in_repo_path) {
3593 free(path);
3594 path = in_repo_path;
3597 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3598 if (error)
3599 goto done;
3601 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3602 show_patch, search_pattern, diff_context, limit, log_branches,
3603 reverse_display_order, &refs);
3604 done:
3605 free(path);
3606 free(repo_path);
3607 free(cwd);
3608 if (worktree)
3609 got_worktree_close(worktree);
3610 if (repo) {
3611 const struct got_error *repo_error;
3612 repo_error = got_repo_close(repo);
3613 if (error == NULL)
3614 error = repo_error;
3616 got_ref_list_free(&refs);
3617 return error;
3620 __dead static void
3621 usage_diff(void)
3623 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3624 "[-w] [object1 object2 | path]\n", getprogname());
3625 exit(1);
3628 struct print_diff_arg {
3629 struct got_repository *repo;
3630 struct got_worktree *worktree;
3631 int diff_context;
3632 const char *id_str;
3633 int header_shown;
3634 int diff_staged;
3635 int ignore_whitespace;
3639 * Create a file which contains the target path of a symlink so we can feed
3640 * it as content to the diff engine.
3642 static const struct got_error *
3643 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3644 const char *abspath)
3646 const struct got_error *err = NULL;
3647 char target_path[PATH_MAX];
3648 ssize_t target_len, outlen;
3650 *fd = -1;
3652 if (dirfd != -1) {
3653 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3654 if (target_len == -1)
3655 return got_error_from_errno2("readlinkat", abspath);
3656 } else {
3657 target_len = readlink(abspath, target_path, PATH_MAX);
3658 if (target_len == -1)
3659 return got_error_from_errno2("readlink", abspath);
3662 *fd = got_opentempfd();
3663 if (*fd == -1)
3664 return got_error_from_errno("got_opentempfd");
3666 outlen = write(*fd, target_path, target_len);
3667 if (outlen == -1) {
3668 err = got_error_from_errno("got_opentempfd");
3669 goto done;
3672 if (lseek(*fd, 0, SEEK_SET) == -1) {
3673 err = got_error_from_errno2("lseek", abspath);
3674 goto done;
3676 done:
3677 if (err) {
3678 close(*fd);
3679 *fd = -1;
3681 return err;
3684 static const struct got_error *
3685 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3686 const char *path, struct got_object_id *blob_id,
3687 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3688 int dirfd, const char *de_name)
3690 struct print_diff_arg *a = arg;
3691 const struct got_error *err = NULL;
3692 struct got_blob_object *blob1 = NULL;
3693 int fd = -1;
3694 FILE *f2 = NULL;
3695 char *abspath = NULL, *label1 = NULL;
3696 struct stat sb;
3698 if (a->diff_staged) {
3699 if (staged_status != GOT_STATUS_MODIFY &&
3700 staged_status != GOT_STATUS_ADD &&
3701 staged_status != GOT_STATUS_DELETE)
3702 return NULL;
3703 } else {
3704 if (staged_status == GOT_STATUS_DELETE)
3705 return NULL;
3706 if (status == GOT_STATUS_NONEXISTENT)
3707 return got_error_set_errno(ENOENT, path);
3708 if (status != GOT_STATUS_MODIFY &&
3709 status != GOT_STATUS_ADD &&
3710 status != GOT_STATUS_DELETE &&
3711 status != GOT_STATUS_CONFLICT)
3712 return NULL;
3715 if (!a->header_shown) {
3716 printf("diff %s %s%s\n", a->id_str,
3717 got_worktree_get_root_path(a->worktree),
3718 a->diff_staged ? " (staged changes)" : "");
3719 a->header_shown = 1;
3722 if (a->diff_staged) {
3723 const char *label1 = NULL, *label2 = NULL;
3724 switch (staged_status) {
3725 case GOT_STATUS_MODIFY:
3726 label1 = path;
3727 label2 = path;
3728 break;
3729 case GOT_STATUS_ADD:
3730 label2 = path;
3731 break;
3732 case GOT_STATUS_DELETE:
3733 label1 = path;
3734 break;
3735 default:
3736 return got_error(GOT_ERR_FILE_STATUS);
3738 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3739 label1, label2, a->diff_context, a->ignore_whitespace,
3740 a->repo, stdout);
3743 if (staged_status == GOT_STATUS_ADD ||
3744 staged_status == GOT_STATUS_MODIFY) {
3745 char *id_str;
3746 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3747 8192);
3748 if (err)
3749 goto done;
3750 err = got_object_id_str(&id_str, staged_blob_id);
3751 if (err)
3752 goto done;
3753 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3754 err = got_error_from_errno("asprintf");
3755 free(id_str);
3756 goto done;
3758 free(id_str);
3759 } else if (status != GOT_STATUS_ADD) {
3760 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3761 if (err)
3762 goto done;
3765 if (status != GOT_STATUS_DELETE) {
3766 if (asprintf(&abspath, "%s/%s",
3767 got_worktree_get_root_path(a->worktree), path) == -1) {
3768 err = got_error_from_errno("asprintf");
3769 goto done;
3772 if (dirfd != -1) {
3773 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3774 if (fd == -1) {
3775 if (errno != ELOOP) {
3776 err = got_error_from_errno2("openat",
3777 abspath);
3778 goto done;
3780 err = get_symlink_target_file(&fd, dirfd,
3781 de_name, abspath);
3782 if (err)
3783 goto done;
3785 } else {
3786 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3787 if (fd == -1) {
3788 if (errno != ELOOP) {
3789 err = got_error_from_errno2("open",
3790 abspath);
3791 goto done;
3793 err = get_symlink_target_file(&fd, dirfd,
3794 de_name, abspath);
3795 if (err)
3796 goto done;
3799 if (fstat(fd, &sb) == -1) {
3800 err = got_error_from_errno2("fstat", abspath);
3801 goto done;
3803 f2 = fdopen(fd, "r");
3804 if (f2 == NULL) {
3805 err = got_error_from_errno2("fdopen", abspath);
3806 goto done;
3808 fd = -1;
3809 } else
3810 sb.st_size = 0;
3812 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3813 a->diff_context, a->ignore_whitespace, stdout);
3814 done:
3815 if (blob1)
3816 got_object_blob_close(blob1);
3817 if (f2 && fclose(f2) == EOF && err == NULL)
3818 err = got_error_from_errno("fclose");
3819 if (fd != -1 && close(fd) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3821 free(abspath);
3822 return err;
3825 static const struct got_error *
3826 cmd_diff(int argc, char *argv[])
3828 const struct got_error *error;
3829 struct got_repository *repo = NULL;
3830 struct got_worktree *worktree = NULL;
3831 char *cwd = NULL, *repo_path = NULL;
3832 struct got_object_id *id1 = NULL, *id2 = NULL;
3833 const char *id_str1 = NULL, *id_str2 = NULL;
3834 char *label1 = NULL, *label2 = NULL;
3835 int type1, type2;
3836 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3837 const char *errstr;
3838 char *path = NULL;
3840 #ifndef PROFILE
3841 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3842 NULL) == -1)
3843 err(1, "pledge");
3844 #endif
3846 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3847 switch (ch) {
3848 case 'C':
3849 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3850 &errstr);
3851 if (errstr != NULL)
3852 err(1, "-C option %s", errstr);
3853 break;
3854 case 'r':
3855 repo_path = realpath(optarg, NULL);
3856 if (repo_path == NULL)
3857 return got_error_from_errno2("realpath",
3858 optarg);
3859 got_path_strip_trailing_slashes(repo_path);
3860 break;
3861 case 's':
3862 diff_staged = 1;
3863 break;
3864 case 'w':
3865 ignore_whitespace = 1;
3866 break;
3867 default:
3868 usage_diff();
3869 /* NOTREACHED */
3873 argc -= optind;
3874 argv += optind;
3876 cwd = getcwd(NULL, 0);
3877 if (cwd == NULL) {
3878 error = got_error_from_errno("getcwd");
3879 goto done;
3881 if (argc <= 1) {
3882 if (repo_path)
3883 errx(1,
3884 "-r option can't be used when diffing a work tree");
3885 error = got_worktree_open(&worktree, cwd);
3886 if (error) {
3887 if (error->code == GOT_ERR_NOT_WORKTREE)
3888 error = wrap_not_worktree_error(error, "diff",
3889 cwd);
3890 goto done;
3892 repo_path = strdup(got_worktree_get_repo_path(worktree));
3893 if (repo_path == NULL) {
3894 error = got_error_from_errno("strdup");
3895 goto done;
3897 if (argc == 1) {
3898 error = got_worktree_resolve_path(&path, worktree,
3899 argv[0]);
3900 if (error)
3901 goto done;
3902 } else {
3903 path = strdup("");
3904 if (path == NULL) {
3905 error = got_error_from_errno("strdup");
3906 goto done;
3909 } else if (argc == 2) {
3910 if (diff_staged)
3911 errx(1, "-s option can't be used when diffing "
3912 "objects in repository");
3913 id_str1 = argv[0];
3914 id_str2 = argv[1];
3915 if (repo_path == NULL) {
3916 error = got_worktree_open(&worktree, cwd);
3917 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3918 goto done;
3919 if (worktree) {
3920 repo_path = strdup(
3921 got_worktree_get_repo_path(worktree));
3922 if (repo_path == NULL) {
3923 error = got_error_from_errno("strdup");
3924 goto done;
3926 } else {
3927 repo_path = strdup(cwd);
3928 if (repo_path == NULL) {
3929 error = got_error_from_errno("strdup");
3930 goto done;
3934 } else
3935 usage_diff();
3937 error = got_repo_open(&repo, repo_path, NULL);
3938 free(repo_path);
3939 if (error != NULL)
3940 goto done;
3942 error = apply_unveil(got_repo_get_path(repo), 1,
3943 worktree ? got_worktree_get_root_path(worktree) : NULL);
3944 if (error)
3945 goto done;
3947 if (argc <= 1) {
3948 struct print_diff_arg arg;
3949 struct got_pathlist_head paths;
3950 char *id_str;
3952 TAILQ_INIT(&paths);
3954 error = got_object_id_str(&id_str,
3955 got_worktree_get_base_commit_id(worktree));
3956 if (error)
3957 goto done;
3958 arg.repo = repo;
3959 arg.worktree = worktree;
3960 arg.diff_context = diff_context;
3961 arg.id_str = id_str;
3962 arg.header_shown = 0;
3963 arg.diff_staged = diff_staged;
3964 arg.ignore_whitespace = ignore_whitespace;
3966 error = got_pathlist_append(&paths, path, NULL);
3967 if (error)
3968 goto done;
3970 error = got_worktree_status(worktree, &paths, repo, print_diff,
3971 &arg, check_cancelled, NULL);
3972 free(id_str);
3973 got_pathlist_free(&paths);
3974 goto done;
3977 error = got_repo_match_object_id(&id1, &label1, id_str1,
3978 GOT_OBJ_TYPE_ANY, 1, repo);
3979 if (error)
3980 goto done;
3982 error = got_repo_match_object_id(&id2, &label2, id_str2,
3983 GOT_OBJ_TYPE_ANY, 1, repo);
3984 if (error)
3985 goto done;
3987 error = got_object_get_type(&type1, repo, id1);
3988 if (error)
3989 goto done;
3991 error = got_object_get_type(&type2, repo, id2);
3992 if (error)
3993 goto done;
3995 if (type1 != type2) {
3996 error = got_error(GOT_ERR_OBJ_TYPE);
3997 goto done;
4000 switch (type1) {
4001 case GOT_OBJ_TYPE_BLOB:
4002 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4003 diff_context, ignore_whitespace, repo, stdout);
4004 break;
4005 case GOT_OBJ_TYPE_TREE:
4006 error = got_diff_objects_as_trees(id1, id2, "", "",
4007 diff_context, ignore_whitespace, repo, stdout);
4008 break;
4009 case GOT_OBJ_TYPE_COMMIT:
4010 printf("diff %s %s\n", label1, label2);
4011 error = got_diff_objects_as_commits(id1, id2, diff_context,
4012 ignore_whitespace, repo, stdout);
4013 break;
4014 default:
4015 error = got_error(GOT_ERR_OBJ_TYPE);
4017 done:
4018 free(label1);
4019 free(label2);
4020 free(id1);
4021 free(id2);
4022 free(path);
4023 if (worktree)
4024 got_worktree_close(worktree);
4025 if (repo) {
4026 const struct got_error *repo_error;
4027 repo_error = got_repo_close(repo);
4028 if (error == NULL)
4029 error = repo_error;
4031 return error;
4034 __dead static void
4035 usage_blame(void)
4037 fprintf(stderr,
4038 "usage: %s blame [-c commit] [-r repository-path] path\n",
4039 getprogname());
4040 exit(1);
4043 struct blame_line {
4044 int annotated;
4045 char *id_str;
4046 char *committer;
4047 char datebuf[11]; /* YYYY-MM-DD + NUL */
4050 struct blame_cb_args {
4051 struct blame_line *lines;
4052 int nlines;
4053 int nlines_prec;
4054 int lineno_cur;
4055 off_t *line_offsets;
4056 FILE *f;
4057 struct got_repository *repo;
4060 static const struct got_error *
4061 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4063 const struct got_error *err = NULL;
4064 struct blame_cb_args *a = arg;
4065 struct blame_line *bline;
4066 char *line = NULL;
4067 size_t linesize = 0;
4068 struct got_commit_object *commit = NULL;
4069 off_t offset;
4070 struct tm tm;
4071 time_t committer_time;
4073 if (nlines != a->nlines ||
4074 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4075 return got_error(GOT_ERR_RANGE);
4077 if (sigint_received)
4078 return got_error(GOT_ERR_ITER_COMPLETED);
4080 if (lineno == -1)
4081 return NULL; /* no change in this commit */
4083 /* Annotate this line. */
4084 bline = &a->lines[lineno - 1];
4085 if (bline->annotated)
4086 return NULL;
4087 err = got_object_id_str(&bline->id_str, id);
4088 if (err)
4089 return err;
4091 err = got_object_open_as_commit(&commit, a->repo, id);
4092 if (err)
4093 goto done;
4095 bline->committer = strdup(got_object_commit_get_committer(commit));
4096 if (bline->committer == NULL) {
4097 err = got_error_from_errno("strdup");
4098 goto done;
4101 committer_time = got_object_commit_get_committer_time(commit);
4102 if (localtime_r(&committer_time, &tm) == NULL)
4103 return got_error_from_errno("localtime_r");
4104 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4105 &tm) >= sizeof(bline->datebuf)) {
4106 err = got_error(GOT_ERR_NO_SPACE);
4107 goto done;
4109 bline->annotated = 1;
4111 /* Print lines annotated so far. */
4112 bline = &a->lines[a->lineno_cur - 1];
4113 if (!bline->annotated)
4114 goto done;
4116 offset = a->line_offsets[a->lineno_cur - 1];
4117 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4118 err = got_error_from_errno("fseeko");
4119 goto done;
4122 while (bline->annotated) {
4123 char *smallerthan, *at, *nl, *committer;
4124 size_t len;
4126 if (getline(&line, &linesize, a->f) == -1) {
4127 if (ferror(a->f))
4128 err = got_error_from_errno("getline");
4129 break;
4132 committer = bline->committer;
4133 smallerthan = strchr(committer, '<');
4134 if (smallerthan && smallerthan[1] != '\0')
4135 committer = smallerthan + 1;
4136 at = strchr(committer, '@');
4137 if (at)
4138 *at = '\0';
4139 len = strlen(committer);
4140 if (len >= 9)
4141 committer[8] = '\0';
4143 nl = strchr(line, '\n');
4144 if (nl)
4145 *nl = '\0';
4146 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4147 bline->id_str, bline->datebuf, committer, line);
4149 a->lineno_cur++;
4150 bline = &a->lines[a->lineno_cur - 1];
4152 done:
4153 if (commit)
4154 got_object_commit_close(commit);
4155 free(line);
4156 return err;
4159 static const struct got_error *
4160 cmd_blame(int argc, char *argv[])
4162 const struct got_error *error;
4163 struct got_repository *repo = NULL;
4164 struct got_worktree *worktree = NULL;
4165 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4166 char *link_target = NULL;
4167 struct got_object_id *obj_id = NULL;
4168 struct got_object_id *commit_id = NULL;
4169 struct got_blob_object *blob = NULL;
4170 char *commit_id_str = NULL;
4171 struct blame_cb_args bca;
4172 int ch, obj_type, i;
4173 size_t filesize;
4175 memset(&bca, 0, sizeof(bca));
4177 #ifndef PROFILE
4178 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4179 NULL) == -1)
4180 err(1, "pledge");
4181 #endif
4183 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4184 switch (ch) {
4185 case 'c':
4186 commit_id_str = optarg;
4187 break;
4188 case 'r':
4189 repo_path = realpath(optarg, NULL);
4190 if (repo_path == NULL)
4191 return got_error_from_errno2("realpath",
4192 optarg);
4193 got_path_strip_trailing_slashes(repo_path);
4194 break;
4195 default:
4196 usage_blame();
4197 /* NOTREACHED */
4201 argc -= optind;
4202 argv += optind;
4204 if (argc == 1)
4205 path = argv[0];
4206 else
4207 usage_blame();
4209 cwd = getcwd(NULL, 0);
4210 if (cwd == NULL) {
4211 error = got_error_from_errno("getcwd");
4212 goto done;
4214 if (repo_path == NULL) {
4215 error = got_worktree_open(&worktree, cwd);
4216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4217 goto done;
4218 else
4219 error = NULL;
4220 if (worktree) {
4221 repo_path =
4222 strdup(got_worktree_get_repo_path(worktree));
4223 if (repo_path == NULL) {
4224 error = got_error_from_errno("strdup");
4225 if (error)
4226 goto done;
4228 } else {
4229 repo_path = strdup(cwd);
4230 if (repo_path == NULL) {
4231 error = got_error_from_errno("strdup");
4232 goto done;
4237 error = got_repo_open(&repo, repo_path, NULL);
4238 if (error != NULL)
4239 goto done;
4241 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4242 if (error)
4243 goto done;
4245 if (worktree) {
4246 const char *prefix = got_worktree_get_path_prefix(worktree);
4247 char *p, *worktree_subdir = cwd +
4248 strlen(got_worktree_get_root_path(worktree));
4249 if (asprintf(&p, "%s%s%s%s%s",
4250 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4251 worktree_subdir, worktree_subdir[0] ? "/" : "",
4252 path) == -1) {
4253 error = got_error_from_errno("asprintf");
4254 goto done;
4256 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4257 free(p);
4258 } else {
4259 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4261 if (error)
4262 goto done;
4264 if (commit_id_str == NULL) {
4265 struct got_reference *head_ref;
4266 error = got_ref_open(&head_ref, repo, worktree ?
4267 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4268 if (error != NULL)
4269 goto done;
4270 error = got_ref_resolve(&commit_id, repo, head_ref);
4271 got_ref_close(head_ref);
4272 if (error != NULL)
4273 goto done;
4274 } else {
4275 error = got_repo_match_object_id(&commit_id, NULL,
4276 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4277 if (error)
4278 goto done;
4281 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4282 commit_id, repo);
4283 if (error)
4284 goto done;
4286 error = got_object_id_by_path(&obj_id, repo, commit_id,
4287 link_target ? link_target : in_repo_path);
4288 if (error)
4289 goto done;
4291 error = got_object_get_type(&obj_type, repo, obj_id);
4292 if (error)
4293 goto done;
4295 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4296 error = got_error_path(link_target ? link_target : in_repo_path,
4297 GOT_ERR_OBJ_TYPE);
4298 goto done;
4301 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4302 if (error)
4303 goto done;
4304 bca.f = got_opentemp();
4305 if (bca.f == NULL) {
4306 error = got_error_from_errno("got_opentemp");
4307 goto done;
4309 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4310 &bca.line_offsets, bca.f, blob);
4311 if (error || bca.nlines == 0)
4312 goto done;
4314 /* Don't include \n at EOF in the blame line count. */
4315 if (bca.line_offsets[bca.nlines - 1] == filesize)
4316 bca.nlines--;
4318 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4319 if (bca.lines == NULL) {
4320 error = got_error_from_errno("calloc");
4321 goto done;
4323 bca.lineno_cur = 1;
4324 bca.nlines_prec = 0;
4325 i = bca.nlines;
4326 while (i > 0) {
4327 i /= 10;
4328 bca.nlines_prec++;
4330 bca.repo = repo;
4332 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4333 repo, blame_cb, &bca, check_cancelled, NULL);
4334 done:
4335 free(in_repo_path);
4336 free(link_target);
4337 free(repo_path);
4338 free(cwd);
4339 free(commit_id);
4340 free(obj_id);
4341 if (blob)
4342 got_object_blob_close(blob);
4343 if (worktree)
4344 got_worktree_close(worktree);
4345 if (repo) {
4346 const struct got_error *repo_error;
4347 repo_error = got_repo_close(repo);
4348 if (error == NULL)
4349 error = repo_error;
4351 if (bca.lines) {
4352 for (i = 0; i < bca.nlines; i++) {
4353 struct blame_line *bline = &bca.lines[i];
4354 free(bline->id_str);
4355 free(bline->committer);
4357 free(bca.lines);
4359 free(bca.line_offsets);
4360 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4361 error = got_error_from_errno("fclose");
4362 return error;
4365 __dead static void
4366 usage_tree(void)
4368 fprintf(stderr,
4369 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4370 getprogname());
4371 exit(1);
4374 static const struct got_error *
4375 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4376 const char *root_path, struct got_repository *repo)
4378 const struct got_error *err = NULL;
4379 int is_root_path = (strcmp(path, root_path) == 0);
4380 const char *modestr = "";
4381 mode_t mode = got_tree_entry_get_mode(te);
4382 char *link_target = NULL;
4384 path += strlen(root_path);
4385 while (path[0] == '/')
4386 path++;
4388 if (got_object_tree_entry_is_submodule(te))
4389 modestr = "$";
4390 else if (S_ISLNK(mode)) {
4391 int i;
4393 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4394 if (err)
4395 return err;
4396 for (i = 0; i < strlen(link_target); i++) {
4397 if (!isprint((unsigned char)link_target[i]))
4398 link_target[i] = '?';
4401 modestr = "@";
4403 else if (S_ISDIR(mode))
4404 modestr = "/";
4405 else if (mode & S_IXUSR)
4406 modestr = "*";
4408 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4409 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4410 link_target ? " -> ": "", link_target ? link_target : "");
4412 free(link_target);
4413 return NULL;
4416 static const struct got_error *
4417 print_tree(const char *path, struct got_object_id *commit_id,
4418 int show_ids, int recurse, const char *root_path,
4419 struct got_repository *repo)
4421 const struct got_error *err = NULL;
4422 struct got_object_id *tree_id = NULL;
4423 struct got_tree_object *tree = NULL;
4424 int nentries, i;
4426 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4427 if (err)
4428 goto done;
4430 err = got_object_open_as_tree(&tree, repo, tree_id);
4431 if (err)
4432 goto done;
4433 nentries = got_object_tree_get_nentries(tree);
4434 for (i = 0; i < nentries; i++) {
4435 struct got_tree_entry *te;
4436 char *id = NULL;
4438 if (sigint_received || sigpipe_received)
4439 break;
4441 te = got_object_tree_get_entry(tree, i);
4442 if (show_ids) {
4443 char *id_str;
4444 err = got_object_id_str(&id_str,
4445 got_tree_entry_get_id(te));
4446 if (err)
4447 goto done;
4448 if (asprintf(&id, "%s ", id_str) == -1) {
4449 err = got_error_from_errno("asprintf");
4450 free(id_str);
4451 goto done;
4453 free(id_str);
4455 err = print_entry(te, id, path, root_path, repo);
4456 free(id);
4457 if (err)
4458 goto done;
4460 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4461 char *child_path;
4462 if (asprintf(&child_path, "%s%s%s", path,
4463 path[0] == '/' && path[1] == '\0' ? "" : "/",
4464 got_tree_entry_get_name(te)) == -1) {
4465 err = got_error_from_errno("asprintf");
4466 goto done;
4468 err = print_tree(child_path, commit_id, show_ids, 1,
4469 root_path, repo);
4470 free(child_path);
4471 if (err)
4472 goto done;
4475 done:
4476 if (tree)
4477 got_object_tree_close(tree);
4478 free(tree_id);
4479 return err;
4482 static const struct got_error *
4483 cmd_tree(int argc, char *argv[])
4485 const struct got_error *error;
4486 struct got_repository *repo = NULL;
4487 struct got_worktree *worktree = NULL;
4488 const char *path, *refname = NULL;
4489 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4490 struct got_object_id *commit_id = NULL;
4491 char *commit_id_str = NULL;
4492 int show_ids = 0, recurse = 0;
4493 int ch;
4495 #ifndef PROFILE
4496 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4497 NULL) == -1)
4498 err(1, "pledge");
4499 #endif
4501 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4502 switch (ch) {
4503 case 'c':
4504 commit_id_str = optarg;
4505 break;
4506 case 'r':
4507 repo_path = realpath(optarg, NULL);
4508 if (repo_path == NULL)
4509 return got_error_from_errno2("realpath",
4510 optarg);
4511 got_path_strip_trailing_slashes(repo_path);
4512 break;
4513 case 'i':
4514 show_ids = 1;
4515 break;
4516 case 'R':
4517 recurse = 1;
4518 break;
4519 default:
4520 usage_tree();
4521 /* NOTREACHED */
4525 argc -= optind;
4526 argv += optind;
4528 if (argc == 1)
4529 path = argv[0];
4530 else if (argc > 1)
4531 usage_tree();
4532 else
4533 path = NULL;
4535 cwd = getcwd(NULL, 0);
4536 if (cwd == NULL) {
4537 error = got_error_from_errno("getcwd");
4538 goto done;
4540 if (repo_path == NULL) {
4541 error = got_worktree_open(&worktree, cwd);
4542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4543 goto done;
4544 else
4545 error = NULL;
4546 if (worktree) {
4547 repo_path =
4548 strdup(got_worktree_get_repo_path(worktree));
4549 if (repo_path == NULL)
4550 error = got_error_from_errno("strdup");
4551 if (error)
4552 goto done;
4553 } else {
4554 repo_path = strdup(cwd);
4555 if (repo_path == NULL) {
4556 error = got_error_from_errno("strdup");
4557 goto done;
4562 error = got_repo_open(&repo, repo_path, NULL);
4563 if (error != NULL)
4564 goto done;
4566 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4567 if (error)
4568 goto done;
4570 if (path == NULL) {
4571 if (worktree) {
4572 char *p, *worktree_subdir = cwd +
4573 strlen(got_worktree_get_root_path(worktree));
4574 if (asprintf(&p, "%s/%s",
4575 got_worktree_get_path_prefix(worktree),
4576 worktree_subdir) == -1) {
4577 error = got_error_from_errno("asprintf");
4578 goto done;
4580 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4581 free(p);
4582 if (error)
4583 goto done;
4584 } else
4585 path = "/";
4587 if (in_repo_path == NULL) {
4588 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4589 if (error != NULL)
4590 goto done;
4593 if (commit_id_str == NULL) {
4594 struct got_reference *head_ref;
4595 if (worktree)
4596 refname = got_worktree_get_head_ref_name(worktree);
4597 else
4598 refname = GOT_REF_HEAD;
4599 error = got_ref_open(&head_ref, repo, refname, 0);
4600 if (error != NULL)
4601 goto done;
4602 error = got_ref_resolve(&commit_id, repo, head_ref);
4603 got_ref_close(head_ref);
4604 if (error != NULL)
4605 goto done;
4606 } else {
4607 error = got_repo_match_object_id(&commit_id, NULL,
4608 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4609 if (error)
4610 goto done;
4613 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4614 in_repo_path, repo);
4615 done:
4616 free(in_repo_path);
4617 free(repo_path);
4618 free(cwd);
4619 free(commit_id);
4620 if (worktree)
4621 got_worktree_close(worktree);
4622 if (repo) {
4623 const struct got_error *repo_error;
4624 repo_error = got_repo_close(repo);
4625 if (error == NULL)
4626 error = repo_error;
4628 return error;
4631 __dead static void
4632 usage_status(void)
4634 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4635 exit(1);
4638 static const struct got_error *
4639 print_status(void *arg, unsigned char status, unsigned char staged_status,
4640 const char *path, struct got_object_id *blob_id,
4641 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4642 int dirfd, const char *de_name)
4644 if (status == staged_status && (status == GOT_STATUS_DELETE))
4645 status = GOT_STATUS_NO_CHANGE;
4646 printf("%c%c %s\n", status, staged_status, path);
4647 return NULL;
4650 static const struct got_error *
4651 cmd_status(int argc, char *argv[])
4653 const struct got_error *error = NULL;
4654 struct got_repository *repo = NULL;
4655 struct got_worktree *worktree = NULL;
4656 char *cwd = NULL;
4657 struct got_pathlist_head paths;
4658 struct got_pathlist_entry *pe;
4659 int ch;
4661 TAILQ_INIT(&paths);
4663 while ((ch = getopt(argc, argv, "")) != -1) {
4664 switch (ch) {
4665 default:
4666 usage_status();
4667 /* NOTREACHED */
4671 argc -= optind;
4672 argv += optind;
4674 #ifndef PROFILE
4675 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4676 NULL) == -1)
4677 err(1, "pledge");
4678 #endif
4679 cwd = getcwd(NULL, 0);
4680 if (cwd == NULL) {
4681 error = got_error_from_errno("getcwd");
4682 goto done;
4685 error = got_worktree_open(&worktree, cwd);
4686 if (error) {
4687 if (error->code == GOT_ERR_NOT_WORKTREE)
4688 error = wrap_not_worktree_error(error, "status", cwd);
4689 goto done;
4692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4693 NULL);
4694 if (error != NULL)
4695 goto done;
4697 error = apply_unveil(got_repo_get_path(repo), 1,
4698 got_worktree_get_root_path(worktree));
4699 if (error)
4700 goto done;
4702 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4703 if (error)
4704 goto done;
4706 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4707 check_cancelled, NULL);
4708 done:
4709 TAILQ_FOREACH(pe, &paths, entry)
4710 free((char *)pe->path);
4711 got_pathlist_free(&paths);
4712 free(cwd);
4713 return error;
4716 __dead static void
4717 usage_ref(void)
4719 fprintf(stderr,
4720 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4721 "[-d] [name]\n",
4722 getprogname());
4723 exit(1);
4726 static const struct got_error *
4727 list_refs(struct got_repository *repo, const char *refname)
4729 static const struct got_error *err = NULL;
4730 struct got_reflist_head refs;
4731 struct got_reflist_entry *re;
4733 SIMPLEQ_INIT(&refs);
4734 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4735 if (err)
4736 return err;
4738 SIMPLEQ_FOREACH(re, &refs, entry) {
4739 char *refstr;
4740 refstr = got_ref_to_str(re->ref);
4741 if (refstr == NULL)
4742 return got_error_from_errno("got_ref_to_str");
4743 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4744 free(refstr);
4747 got_ref_list_free(&refs);
4748 return NULL;
4751 static const struct got_error *
4752 delete_ref(struct got_repository *repo, const char *refname)
4754 const struct got_error *err = NULL;
4755 struct got_reference *ref;
4757 err = got_ref_open(&ref, repo, refname, 0);
4758 if (err)
4759 return err;
4761 err = got_ref_delete(ref, repo);
4762 got_ref_close(ref);
4763 return err;
4766 static const struct got_error *
4767 add_ref(struct got_repository *repo, const char *refname, const char *target)
4769 const struct got_error *err = NULL;
4770 struct got_object_id *id;
4771 struct got_reference *ref = NULL;
4774 * Don't let the user create a reference name with a leading '-'.
4775 * While technically a valid reference name, this case is usually
4776 * an unintended typo.
4778 if (refname[0] == '-')
4779 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4781 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4782 repo);
4783 if (err) {
4784 struct got_reference *target_ref;
4786 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4787 return err;
4788 err = got_ref_open(&target_ref, repo, target, 0);
4789 if (err)
4790 return err;
4791 err = got_ref_resolve(&id, repo, target_ref);
4792 got_ref_close(target_ref);
4793 if (err)
4794 return err;
4797 err = got_ref_alloc(&ref, refname, id);
4798 if (err)
4799 goto done;
4801 err = got_ref_write(ref, repo);
4802 done:
4803 if (ref)
4804 got_ref_close(ref);
4805 free(id);
4806 return err;
4809 static const struct got_error *
4810 add_symref(struct got_repository *repo, const char *refname, const char *target)
4812 const struct got_error *err = NULL;
4813 struct got_reference *ref = NULL;
4814 struct got_reference *target_ref = NULL;
4817 * Don't let the user create a reference name with a leading '-'.
4818 * While technically a valid reference name, this case is usually
4819 * an unintended typo.
4821 if (refname[0] == '-')
4822 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4824 err = got_ref_open(&target_ref, repo, target, 0);
4825 if (err)
4826 return err;
4828 err = got_ref_alloc_symref(&ref, refname, target_ref);
4829 if (err)
4830 goto done;
4832 err = got_ref_write(ref, repo);
4833 done:
4834 if (target_ref)
4835 got_ref_close(target_ref);
4836 if (ref)
4837 got_ref_close(ref);
4838 return err;
4841 static const struct got_error *
4842 cmd_ref(int argc, char *argv[])
4844 const struct got_error *error = NULL;
4845 struct got_repository *repo = NULL;
4846 struct got_worktree *worktree = NULL;
4847 char *cwd = NULL, *repo_path = NULL;
4848 int ch, do_list = 0, do_delete = 0;
4849 const char *obj_arg = NULL, *symref_target= NULL;
4850 char *refname = NULL;
4852 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4853 switch (ch) {
4854 case 'c':
4855 obj_arg = optarg;
4856 break;
4857 case 'd':
4858 do_delete = 1;
4859 break;
4860 case 'r':
4861 repo_path = realpath(optarg, NULL);
4862 if (repo_path == NULL)
4863 return got_error_from_errno2("realpath",
4864 optarg);
4865 got_path_strip_trailing_slashes(repo_path);
4866 break;
4867 case 'l':
4868 do_list = 1;
4869 break;
4870 case 's':
4871 symref_target = optarg;
4872 break;
4873 default:
4874 usage_ref();
4875 /* NOTREACHED */
4879 if (obj_arg && do_list)
4880 errx(1, "-c and -l options are mutually exclusive");
4881 if (obj_arg && do_delete)
4882 errx(1, "-c and -d options are mutually exclusive");
4883 if (obj_arg && symref_target)
4884 errx(1, "-c and -s options are mutually exclusive");
4885 if (symref_target && do_delete)
4886 errx(1, "-s and -d options are mutually exclusive");
4887 if (symref_target && do_list)
4888 errx(1, "-s and -l options are mutually exclusive");
4889 if (do_delete && do_list)
4890 errx(1, "-d and -l options are mutually exclusive");
4892 argc -= optind;
4893 argv += optind;
4895 if (do_list) {
4896 if (argc != 0 && argc != 1)
4897 usage_ref();
4898 if (argc == 1) {
4899 refname = strdup(argv[0]);
4900 if (refname == NULL) {
4901 error = got_error_from_errno("strdup");
4902 goto done;
4905 } else {
4906 if (argc != 1)
4907 usage_ref();
4908 refname = strdup(argv[0]);
4909 if (refname == NULL) {
4910 error = got_error_from_errno("strdup");
4911 goto done;
4915 if (refname)
4916 got_path_strip_trailing_slashes(refname);
4918 #ifndef PROFILE
4919 if (do_list) {
4920 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4921 NULL) == -1)
4922 err(1, "pledge");
4923 } else {
4924 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4925 "sendfd unveil", NULL) == -1)
4926 err(1, "pledge");
4928 #endif
4929 cwd = getcwd(NULL, 0);
4930 if (cwd == NULL) {
4931 error = got_error_from_errno("getcwd");
4932 goto done;
4935 if (repo_path == NULL) {
4936 error = got_worktree_open(&worktree, cwd);
4937 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4938 goto done;
4939 else
4940 error = NULL;
4941 if (worktree) {
4942 repo_path =
4943 strdup(got_worktree_get_repo_path(worktree));
4944 if (repo_path == NULL)
4945 error = got_error_from_errno("strdup");
4946 if (error)
4947 goto done;
4948 } else {
4949 repo_path = strdup(cwd);
4950 if (repo_path == NULL) {
4951 error = got_error_from_errno("strdup");
4952 goto done;
4957 error = got_repo_open(&repo, repo_path, NULL);
4958 if (error != NULL)
4959 goto done;
4961 error = apply_unveil(got_repo_get_path(repo), do_list,
4962 worktree ? got_worktree_get_root_path(worktree) : NULL);
4963 if (error)
4964 goto done;
4966 if (do_list)
4967 error = list_refs(repo, refname);
4968 else if (do_delete)
4969 error = delete_ref(repo, refname);
4970 else if (symref_target)
4971 error = add_symref(repo, refname, symref_target);
4972 else {
4973 if (obj_arg == NULL)
4974 usage_ref();
4975 error = add_ref(repo, refname, obj_arg);
4977 done:
4978 free(refname);
4979 if (repo)
4980 got_repo_close(repo);
4981 if (worktree)
4982 got_worktree_close(worktree);
4983 free(cwd);
4984 free(repo_path);
4985 return error;
4988 __dead static void
4989 usage_branch(void)
4991 fprintf(stderr,
4992 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4993 "[name]\n", getprogname());
4994 exit(1);
4997 static const struct got_error *
4998 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4999 struct got_reference *ref)
5001 const struct got_error *err = NULL;
5002 const char *refname, *marker = " ";
5003 char *refstr;
5005 refname = got_ref_get_name(ref);
5006 if (worktree && strcmp(refname,
5007 got_worktree_get_head_ref_name(worktree)) == 0) {
5008 struct got_object_id *id = NULL;
5010 err = got_ref_resolve(&id, repo, ref);
5011 if (err)
5012 return err;
5013 if (got_object_id_cmp(id,
5014 got_worktree_get_base_commit_id(worktree)) == 0)
5015 marker = "* ";
5016 else
5017 marker = "~ ";
5018 free(id);
5021 if (strncmp(refname, "refs/heads/", 11) == 0)
5022 refname += 11;
5023 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5024 refname += 18;
5026 refstr = got_ref_to_str(ref);
5027 if (refstr == NULL)
5028 return got_error_from_errno("got_ref_to_str");
5030 printf("%s%s: %s\n", marker, refname, refstr);
5031 free(refstr);
5032 return NULL;
5035 static const struct got_error *
5036 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5038 const char *refname;
5040 if (worktree == NULL)
5041 return got_error(GOT_ERR_NOT_WORKTREE);
5043 refname = got_worktree_get_head_ref_name(worktree);
5045 if (strncmp(refname, "refs/heads/", 11) == 0)
5046 refname += 11;
5047 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5048 refname += 18;
5050 printf("%s\n", refname);
5052 return NULL;
5055 static const struct got_error *
5056 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5058 static const struct got_error *err = NULL;
5059 struct got_reflist_head refs;
5060 struct got_reflist_entry *re;
5061 struct got_reference *temp_ref = NULL;
5062 int rebase_in_progress, histedit_in_progress;
5064 SIMPLEQ_INIT(&refs);
5066 if (worktree) {
5067 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5068 worktree);
5069 if (err)
5070 return err;
5072 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5073 worktree);
5074 if (err)
5075 return err;
5077 if (rebase_in_progress || histedit_in_progress) {
5078 err = got_ref_open(&temp_ref, repo,
5079 got_worktree_get_head_ref_name(worktree), 0);
5080 if (err)
5081 return err;
5082 list_branch(repo, worktree, temp_ref);
5083 got_ref_close(temp_ref);
5087 err = got_ref_list(&refs, repo, "refs/heads",
5088 got_ref_cmp_by_name, NULL);
5089 if (err)
5090 return err;
5092 SIMPLEQ_FOREACH(re, &refs, entry)
5093 list_branch(repo, worktree, re->ref);
5095 got_ref_list_free(&refs);
5096 return NULL;
5099 static const struct got_error *
5100 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5101 const char *branch_name)
5103 const struct got_error *err = NULL;
5104 struct got_reference *ref = NULL;
5105 char *refname;
5107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5108 return got_error_from_errno("asprintf");
5110 err = got_ref_open(&ref, repo, refname, 0);
5111 if (err)
5112 goto done;
5114 if (worktree &&
5115 strcmp(got_worktree_get_head_ref_name(worktree),
5116 got_ref_get_name(ref)) == 0) {
5117 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5118 "will not delete this work tree's current branch");
5119 goto done;
5122 err = got_ref_delete(ref, repo);
5123 done:
5124 if (ref)
5125 got_ref_close(ref);
5126 free(refname);
5127 return err;
5130 static const struct got_error *
5131 add_branch(struct got_repository *repo, const char *branch_name,
5132 struct got_object_id *base_commit_id)
5134 const struct got_error *err = NULL;
5135 struct got_reference *ref = NULL;
5136 char *base_refname = NULL, *refname = NULL;
5139 * Don't let the user create a branch name with a leading '-'.
5140 * While technically a valid reference name, this case is usually
5141 * an unintended typo.
5143 if (branch_name[0] == '-')
5144 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5146 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5147 err = got_error_from_errno("asprintf");
5148 goto done;
5151 err = got_ref_open(&ref, repo, refname, 0);
5152 if (err == NULL) {
5153 err = got_error(GOT_ERR_BRANCH_EXISTS);
5154 goto done;
5155 } else if (err->code != GOT_ERR_NOT_REF)
5156 goto done;
5158 err = got_ref_alloc(&ref, refname, base_commit_id);
5159 if (err)
5160 goto done;
5162 err = got_ref_write(ref, repo);
5163 done:
5164 if (ref)
5165 got_ref_close(ref);
5166 free(base_refname);
5167 free(refname);
5168 return err;
5171 static const struct got_error *
5172 cmd_branch(int argc, char *argv[])
5174 const struct got_error *error = NULL;
5175 struct got_repository *repo = NULL;
5176 struct got_worktree *worktree = NULL;
5177 char *cwd = NULL, *repo_path = NULL;
5178 int ch, do_list = 0, do_show = 0, do_update = 1;
5179 const char *delref = NULL, *commit_id_arg = NULL;
5180 struct got_reference *ref = NULL;
5181 struct got_pathlist_head paths;
5182 struct got_pathlist_entry *pe;
5183 struct got_object_id *commit_id = NULL;
5184 char *commit_id_str = NULL;
5186 TAILQ_INIT(&paths);
5188 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5189 switch (ch) {
5190 case 'c':
5191 commit_id_arg = optarg;
5192 break;
5193 case 'd':
5194 delref = optarg;
5195 break;
5196 case 'r':
5197 repo_path = realpath(optarg, NULL);
5198 if (repo_path == NULL)
5199 return got_error_from_errno2("realpath",
5200 optarg);
5201 got_path_strip_trailing_slashes(repo_path);
5202 break;
5203 case 'l':
5204 do_list = 1;
5205 break;
5206 case 'n':
5207 do_update = 0;
5208 break;
5209 default:
5210 usage_branch();
5211 /* NOTREACHED */
5215 if (do_list && delref)
5216 errx(1, "-l and -d options are mutually exclusive");
5218 argc -= optind;
5219 argv += optind;
5221 if (!do_list && !delref && argc == 0)
5222 do_show = 1;
5224 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5225 errx(1, "-c option can only be used when creating a branch");
5227 if (do_list || delref) {
5228 if (argc > 0)
5229 usage_branch();
5230 } else if (!do_show && argc != 1)
5231 usage_branch();
5233 #ifndef PROFILE
5234 if (do_list || do_show) {
5235 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5236 NULL) == -1)
5237 err(1, "pledge");
5238 } else {
5239 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5240 "sendfd unveil", NULL) == -1)
5241 err(1, "pledge");
5243 #endif
5244 cwd = getcwd(NULL, 0);
5245 if (cwd == NULL) {
5246 error = got_error_from_errno("getcwd");
5247 goto done;
5250 if (repo_path == NULL) {
5251 error = got_worktree_open(&worktree, cwd);
5252 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5253 goto done;
5254 else
5255 error = NULL;
5256 if (worktree) {
5257 repo_path =
5258 strdup(got_worktree_get_repo_path(worktree));
5259 if (repo_path == NULL)
5260 error = got_error_from_errno("strdup");
5261 if (error)
5262 goto done;
5263 } else {
5264 repo_path = strdup(cwd);
5265 if (repo_path == NULL) {
5266 error = got_error_from_errno("strdup");
5267 goto done;
5272 error = got_repo_open(&repo, repo_path, NULL);
5273 if (error != NULL)
5274 goto done;
5276 error = apply_unveil(got_repo_get_path(repo), do_list,
5277 worktree ? got_worktree_get_root_path(worktree) : NULL);
5278 if (error)
5279 goto done;
5281 if (do_show)
5282 error = show_current_branch(repo, worktree);
5283 else if (do_list)
5284 error = list_branches(repo, worktree);
5285 else if (delref)
5286 error = delete_branch(repo, worktree, delref);
5287 else {
5288 if (commit_id_arg == NULL)
5289 commit_id_arg = worktree ?
5290 got_worktree_get_head_ref_name(worktree) :
5291 GOT_REF_HEAD;
5292 error = got_repo_match_object_id(&commit_id, NULL,
5293 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5294 if (error)
5295 goto done;
5296 error = add_branch(repo, argv[0], commit_id);
5297 if (error)
5298 goto done;
5299 if (worktree && do_update) {
5300 struct got_update_progress_arg upa;
5301 char *branch_refname = NULL;
5303 error = got_object_id_str(&commit_id_str, commit_id);
5304 if (error)
5305 goto done;
5306 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5307 worktree);
5308 if (error)
5309 goto done;
5310 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5311 == -1) {
5312 error = got_error_from_errno("asprintf");
5313 goto done;
5315 error = got_ref_open(&ref, repo, branch_refname, 0);
5316 free(branch_refname);
5317 if (error)
5318 goto done;
5319 error = switch_head_ref(ref, commit_id, worktree,
5320 repo);
5321 if (error)
5322 goto done;
5323 error = got_worktree_set_base_commit_id(worktree, repo,
5324 commit_id);
5325 if (error)
5326 goto done;
5327 memset(&upa, 0, sizeof(upa));
5328 error = got_worktree_checkout_files(worktree, &paths,
5329 repo, update_progress, &upa, check_cancelled,
5330 NULL);
5331 if (error)
5332 goto done;
5333 if (upa.did_something)
5334 printf("Updated to commit %s\n", commit_id_str);
5335 print_update_progress_stats(&upa);
5338 done:
5339 if (ref)
5340 got_ref_close(ref);
5341 if (repo)
5342 got_repo_close(repo);
5343 if (worktree)
5344 got_worktree_close(worktree);
5345 free(cwd);
5346 free(repo_path);
5347 free(commit_id);
5348 free(commit_id_str);
5349 TAILQ_FOREACH(pe, &paths, entry)
5350 free((char *)pe->path);
5351 got_pathlist_free(&paths);
5352 return error;
5356 __dead static void
5357 usage_tag(void)
5359 fprintf(stderr,
5360 "usage: %s tag [-c commit] [-r repository] [-l] "
5361 "[-m message] name\n", getprogname());
5362 exit(1);
5365 #if 0
5366 static const struct got_error *
5367 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5369 const struct got_error *err = NULL;
5370 struct got_reflist_entry *re, *se, *new;
5371 struct got_object_id *re_id, *se_id;
5372 struct got_tag_object *re_tag, *se_tag;
5373 time_t re_time, se_time;
5375 SIMPLEQ_FOREACH(re, tags, entry) {
5376 se = SIMPLEQ_FIRST(sorted);
5377 if (se == NULL) {
5378 err = got_reflist_entry_dup(&new, re);
5379 if (err)
5380 return err;
5381 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5382 continue;
5383 } else {
5384 err = got_ref_resolve(&re_id, repo, re->ref);
5385 if (err)
5386 break;
5387 err = got_object_open_as_tag(&re_tag, repo, re_id);
5388 free(re_id);
5389 if (err)
5390 break;
5391 re_time = got_object_tag_get_tagger_time(re_tag);
5392 got_object_tag_close(re_tag);
5395 while (se) {
5396 err = got_ref_resolve(&se_id, repo, re->ref);
5397 if (err)
5398 break;
5399 err = got_object_open_as_tag(&se_tag, repo, se_id);
5400 free(se_id);
5401 if (err)
5402 break;
5403 se_time = got_object_tag_get_tagger_time(se_tag);
5404 got_object_tag_close(se_tag);
5406 if (se_time > re_time) {
5407 err = got_reflist_entry_dup(&new, re);
5408 if (err)
5409 return err;
5410 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5411 break;
5413 se = SIMPLEQ_NEXT(se, entry);
5414 continue;
5417 done:
5418 return err;
5420 #endif
5422 static const struct got_error *
5423 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5425 static const struct got_error *err = NULL;
5426 struct got_reflist_head refs;
5427 struct got_reflist_entry *re;
5429 SIMPLEQ_INIT(&refs);
5431 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5432 if (err)
5433 return err;
5435 SIMPLEQ_FOREACH(re, &refs, entry) {
5436 const char *refname;
5437 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5438 char datebuf[26];
5439 const char *tagger;
5440 time_t tagger_time;
5441 struct got_object_id *id;
5442 struct got_tag_object *tag;
5443 struct got_commit_object *commit = NULL;
5445 refname = got_ref_get_name(re->ref);
5446 if (strncmp(refname, "refs/tags/", 10) != 0)
5447 continue;
5448 refname += 10;
5449 refstr = got_ref_to_str(re->ref);
5450 if (refstr == NULL) {
5451 err = got_error_from_errno("got_ref_to_str");
5452 break;
5454 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5455 free(refstr);
5457 err = got_ref_resolve(&id, repo, re->ref);
5458 if (err)
5459 break;
5460 err = got_object_open_as_tag(&tag, repo, id);
5461 if (err) {
5462 if (err->code != GOT_ERR_OBJ_TYPE) {
5463 free(id);
5464 break;
5466 /* "lightweight" tag */
5467 err = got_object_open_as_commit(&commit, repo, id);
5468 if (err) {
5469 free(id);
5470 break;
5472 tagger = got_object_commit_get_committer(commit);
5473 tagger_time =
5474 got_object_commit_get_committer_time(commit);
5475 err = got_object_id_str(&id_str, id);
5476 free(id);
5477 if (err)
5478 break;
5479 } else {
5480 free(id);
5481 tagger = got_object_tag_get_tagger(tag);
5482 tagger_time = got_object_tag_get_tagger_time(tag);
5483 err = got_object_id_str(&id_str,
5484 got_object_tag_get_object_id(tag));
5485 if (err)
5486 break;
5488 printf("from: %s\n", tagger);
5489 datestr = get_datestr(&tagger_time, datebuf);
5490 if (datestr)
5491 printf("date: %s UTC\n", datestr);
5492 if (commit)
5493 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5494 else {
5495 switch (got_object_tag_get_object_type(tag)) {
5496 case GOT_OBJ_TYPE_BLOB:
5497 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5498 id_str);
5499 break;
5500 case GOT_OBJ_TYPE_TREE:
5501 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5502 id_str);
5503 break;
5504 case GOT_OBJ_TYPE_COMMIT:
5505 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5506 id_str);
5507 break;
5508 case GOT_OBJ_TYPE_TAG:
5509 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5510 id_str);
5511 break;
5512 default:
5513 break;
5516 free(id_str);
5517 if (commit) {
5518 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5519 if (err)
5520 break;
5521 got_object_commit_close(commit);
5522 } else {
5523 tagmsg0 = strdup(got_object_tag_get_message(tag));
5524 got_object_tag_close(tag);
5525 if (tagmsg0 == NULL) {
5526 err = got_error_from_errno("strdup");
5527 break;
5531 tagmsg = tagmsg0;
5532 do {
5533 line = strsep(&tagmsg, "\n");
5534 if (line)
5535 printf(" %s\n", line);
5536 } while (line);
5537 free(tagmsg0);
5540 got_ref_list_free(&refs);
5541 return NULL;
5544 static const struct got_error *
5545 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5546 const char *tag_name, const char *repo_path)
5548 const struct got_error *err = NULL;
5549 char *template = NULL, *initial_content = NULL;
5550 char *editor = NULL;
5551 int fd = -1;
5553 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5554 err = got_error_from_errno("asprintf");
5555 goto done;
5558 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5559 commit_id_str, tag_name) == -1) {
5560 err = got_error_from_errno("asprintf");
5561 goto done;
5564 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5565 if (err)
5566 goto done;
5568 dprintf(fd, initial_content);
5569 close(fd);
5571 err = get_editor(&editor);
5572 if (err)
5573 goto done;
5574 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5575 done:
5576 free(initial_content);
5577 free(template);
5578 free(editor);
5580 /* Editor is done; we can now apply unveil(2) */
5581 if (err == NULL) {
5582 err = apply_unveil(repo_path, 0, NULL);
5583 if (err) {
5584 free(*tagmsg);
5585 *tagmsg = NULL;
5588 return err;
5591 static const struct got_error *
5592 add_tag(struct got_repository *repo, const char *tag_name,
5593 const char *commit_arg, const char *tagmsg_arg)
5595 const struct got_error *err = NULL;
5596 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5597 char *label = NULL, *commit_id_str = NULL;
5598 struct got_reference *ref = NULL;
5599 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5600 char *tagmsg_path = NULL, *tag_id_str = NULL;
5601 int preserve_tagmsg = 0;
5604 * Don't let the user create a tag name with a leading '-'.
5605 * While technically a valid reference name, this case is usually
5606 * an unintended typo.
5608 if (tag_name[0] == '-')
5609 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5611 err = get_author(&tagger, repo);
5612 if (err)
5613 return err;
5615 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5616 GOT_OBJ_TYPE_COMMIT, 1, repo);
5617 if (err)
5618 goto done;
5620 err = got_object_id_str(&commit_id_str, commit_id);
5621 if (err)
5622 goto done;
5624 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5625 refname = strdup(tag_name);
5626 if (refname == NULL) {
5627 err = got_error_from_errno("strdup");
5628 goto done;
5630 tag_name += 10;
5631 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5632 err = got_error_from_errno("asprintf");
5633 goto done;
5636 err = got_ref_open(&ref, repo, refname, 0);
5637 if (err == NULL) {
5638 err = got_error(GOT_ERR_TAG_EXISTS);
5639 goto done;
5640 } else if (err->code != GOT_ERR_NOT_REF)
5641 goto done;
5643 if (tagmsg_arg == NULL) {
5644 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5645 tag_name, got_repo_get_path(repo));
5646 if (err) {
5647 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5648 tagmsg_path != NULL)
5649 preserve_tagmsg = 1;
5650 goto done;
5654 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5655 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5656 if (err) {
5657 if (tagmsg_path)
5658 preserve_tagmsg = 1;
5659 goto done;
5662 err = got_ref_alloc(&ref, refname, tag_id);
5663 if (err) {
5664 if (tagmsg_path)
5665 preserve_tagmsg = 1;
5666 goto done;
5669 err = got_ref_write(ref, repo);
5670 if (err) {
5671 if (tagmsg_path)
5672 preserve_tagmsg = 1;
5673 goto done;
5676 err = got_object_id_str(&tag_id_str, tag_id);
5677 if (err) {
5678 if (tagmsg_path)
5679 preserve_tagmsg = 1;
5680 goto done;
5682 printf("Created tag %s\n", tag_id_str);
5683 done:
5684 if (preserve_tagmsg) {
5685 fprintf(stderr, "%s: tag message preserved in %s\n",
5686 getprogname(), tagmsg_path);
5687 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5688 err = got_error_from_errno2("unlink", tagmsg_path);
5689 free(tag_id_str);
5690 if (ref)
5691 got_ref_close(ref);
5692 free(commit_id);
5693 free(commit_id_str);
5694 free(refname);
5695 free(tagmsg);
5696 free(tagmsg_path);
5697 free(tagger);
5698 return err;
5701 static const struct got_error *
5702 cmd_tag(int argc, char *argv[])
5704 const struct got_error *error = NULL;
5705 struct got_repository *repo = NULL;
5706 struct got_worktree *worktree = NULL;
5707 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5708 char *gitconfig_path = NULL;
5709 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5710 int ch, do_list = 0;
5712 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5713 switch (ch) {
5714 case 'c':
5715 commit_id_arg = optarg;
5716 break;
5717 case 'm':
5718 tagmsg = optarg;
5719 break;
5720 case 'r':
5721 repo_path = realpath(optarg, NULL);
5722 if (repo_path == NULL)
5723 return got_error_from_errno2("realpath",
5724 optarg);
5725 got_path_strip_trailing_slashes(repo_path);
5726 break;
5727 case 'l':
5728 do_list = 1;
5729 break;
5730 default:
5731 usage_tag();
5732 /* NOTREACHED */
5736 argc -= optind;
5737 argv += optind;
5739 if (do_list) {
5740 if (commit_id_arg != NULL)
5741 errx(1,
5742 "-c option can only be used when creating a tag");
5743 if (tagmsg)
5744 errx(1, "-l and -m options are mutually exclusive");
5745 if (argc > 0)
5746 usage_tag();
5747 } else if (argc != 1)
5748 usage_tag();
5750 tag_name = argv[0];
5752 #ifndef PROFILE
5753 if (do_list) {
5754 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5755 NULL) == -1)
5756 err(1, "pledge");
5757 } else {
5758 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5759 "sendfd unveil", NULL) == -1)
5760 err(1, "pledge");
5762 #endif
5763 cwd = getcwd(NULL, 0);
5764 if (cwd == NULL) {
5765 error = got_error_from_errno("getcwd");
5766 goto done;
5769 if (repo_path == NULL) {
5770 error = got_worktree_open(&worktree, cwd);
5771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5772 goto done;
5773 else
5774 error = NULL;
5775 if (worktree) {
5776 repo_path =
5777 strdup(got_worktree_get_repo_path(worktree));
5778 if (repo_path == NULL)
5779 error = got_error_from_errno("strdup");
5780 if (error)
5781 goto done;
5782 } else {
5783 repo_path = strdup(cwd);
5784 if (repo_path == NULL) {
5785 error = got_error_from_errno("strdup");
5786 goto done;
5791 if (do_list) {
5792 error = got_repo_open(&repo, repo_path, NULL);
5793 if (error != NULL)
5794 goto done;
5795 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5796 if (error)
5797 goto done;
5798 error = list_tags(repo, worktree);
5799 } else {
5800 error = get_gitconfig_path(&gitconfig_path);
5801 if (error)
5802 goto done;
5803 error = got_repo_open(&repo, repo_path, gitconfig_path);
5804 if (error != NULL)
5805 goto done;
5807 if (tagmsg) {
5808 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5809 if (error)
5810 goto done;
5813 if (commit_id_arg == NULL) {
5814 struct got_reference *head_ref;
5815 struct got_object_id *commit_id;
5816 error = got_ref_open(&head_ref, repo,
5817 worktree ? got_worktree_get_head_ref_name(worktree)
5818 : GOT_REF_HEAD, 0);
5819 if (error)
5820 goto done;
5821 error = got_ref_resolve(&commit_id, repo, head_ref);
5822 got_ref_close(head_ref);
5823 if (error)
5824 goto done;
5825 error = got_object_id_str(&commit_id_str, commit_id);
5826 free(commit_id);
5827 if (error)
5828 goto done;
5831 error = add_tag(repo, tag_name,
5832 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5834 done:
5835 if (repo)
5836 got_repo_close(repo);
5837 if (worktree)
5838 got_worktree_close(worktree);
5839 free(cwd);
5840 free(repo_path);
5841 free(gitconfig_path);
5842 free(commit_id_str);
5843 return error;
5846 __dead static void
5847 usage_add(void)
5849 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5850 getprogname());
5851 exit(1);
5854 static const struct got_error *
5855 add_progress(void *arg, unsigned char status, const char *path)
5857 while (path[0] == '/')
5858 path++;
5859 printf("%c %s\n", status, path);
5860 return NULL;
5863 static const struct got_error *
5864 cmd_add(int argc, char *argv[])
5866 const struct got_error *error = NULL;
5867 struct got_repository *repo = NULL;
5868 struct got_worktree *worktree = NULL;
5869 char *cwd = NULL;
5870 struct got_pathlist_head paths;
5871 struct got_pathlist_entry *pe;
5872 int ch, can_recurse = 0, no_ignores = 0;
5874 TAILQ_INIT(&paths);
5876 while ((ch = getopt(argc, argv, "IR")) != -1) {
5877 switch (ch) {
5878 case 'I':
5879 no_ignores = 1;
5880 break;
5881 case 'R':
5882 can_recurse = 1;
5883 break;
5884 default:
5885 usage_add();
5886 /* NOTREACHED */
5890 argc -= optind;
5891 argv += optind;
5893 #ifndef PROFILE
5894 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5895 NULL) == -1)
5896 err(1, "pledge");
5897 #endif
5898 if (argc < 1)
5899 usage_add();
5901 cwd = getcwd(NULL, 0);
5902 if (cwd == NULL) {
5903 error = got_error_from_errno("getcwd");
5904 goto done;
5907 error = got_worktree_open(&worktree, cwd);
5908 if (error) {
5909 if (error->code == GOT_ERR_NOT_WORKTREE)
5910 error = wrap_not_worktree_error(error, "add", cwd);
5911 goto done;
5914 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5915 NULL);
5916 if (error != NULL)
5917 goto done;
5919 error = apply_unveil(got_repo_get_path(repo), 1,
5920 got_worktree_get_root_path(worktree));
5921 if (error)
5922 goto done;
5924 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5925 if (error)
5926 goto done;
5928 if (!can_recurse && no_ignores) {
5929 error = got_error_msg(GOT_ERR_BAD_PATH,
5930 "disregarding ignores requires -R option");
5931 goto done;
5935 if (!can_recurse) {
5936 char *ondisk_path;
5937 struct stat sb;
5938 TAILQ_FOREACH(pe, &paths, entry) {
5939 if (asprintf(&ondisk_path, "%s/%s",
5940 got_worktree_get_root_path(worktree),
5941 pe->path) == -1) {
5942 error = got_error_from_errno("asprintf");
5943 goto done;
5945 if (lstat(ondisk_path, &sb) == -1) {
5946 if (errno == ENOENT) {
5947 free(ondisk_path);
5948 continue;
5950 error = got_error_from_errno2("lstat",
5951 ondisk_path);
5952 free(ondisk_path);
5953 goto done;
5955 free(ondisk_path);
5956 if (S_ISDIR(sb.st_mode)) {
5957 error = got_error_msg(GOT_ERR_BAD_PATH,
5958 "adding directories requires -R option");
5959 goto done;
5964 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5965 NULL, repo, no_ignores);
5966 done:
5967 if (repo)
5968 got_repo_close(repo);
5969 if (worktree)
5970 got_worktree_close(worktree);
5971 TAILQ_FOREACH(pe, &paths, entry)
5972 free((char *)pe->path);
5973 got_pathlist_free(&paths);
5974 free(cwd);
5975 return error;
5978 __dead static void
5979 usage_remove(void)
5981 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5982 getprogname());
5983 exit(1);
5986 static const struct got_error *
5987 print_remove_status(void *arg, unsigned char status,
5988 unsigned char staged_status, const char *path)
5990 while (path[0] == '/')
5991 path++;
5992 if (status == GOT_STATUS_NONEXISTENT)
5993 return NULL;
5994 if (status == staged_status && (status == GOT_STATUS_DELETE))
5995 status = GOT_STATUS_NO_CHANGE;
5996 printf("%c%c %s\n", status, staged_status, path);
5997 return NULL;
6000 static const struct got_error *
6001 cmd_remove(int argc, char *argv[])
6003 const struct got_error *error = NULL;
6004 struct got_worktree *worktree = NULL;
6005 struct got_repository *repo = NULL;
6006 char *cwd = NULL;
6007 struct got_pathlist_head paths;
6008 struct got_pathlist_entry *pe;
6009 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
6011 TAILQ_INIT(&paths);
6013 while ((ch = getopt(argc, argv, "fkR")) != -1) {
6014 switch (ch) {
6015 case 'f':
6016 delete_local_mods = 1;
6017 break;
6018 case 'k':
6019 keep_on_disk = 1;
6020 break;
6021 case 'R':
6022 can_recurse = 1;
6023 break;
6024 default:
6025 usage_remove();
6026 /* NOTREACHED */
6030 argc -= optind;
6031 argv += optind;
6033 #ifndef PROFILE
6034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6035 NULL) == -1)
6036 err(1, "pledge");
6037 #endif
6038 if (argc < 1)
6039 usage_remove();
6041 cwd = getcwd(NULL, 0);
6042 if (cwd == NULL) {
6043 error = got_error_from_errno("getcwd");
6044 goto done;
6046 error = got_worktree_open(&worktree, cwd);
6047 if (error) {
6048 if (error->code == GOT_ERR_NOT_WORKTREE)
6049 error = wrap_not_worktree_error(error, "remove", cwd);
6050 goto done;
6053 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6054 NULL);
6055 if (error)
6056 goto done;
6058 error = apply_unveil(got_repo_get_path(repo), 1,
6059 got_worktree_get_root_path(worktree));
6060 if (error)
6061 goto done;
6063 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6064 if (error)
6065 goto done;
6067 if (!can_recurse) {
6068 char *ondisk_path;
6069 struct stat sb;
6070 TAILQ_FOREACH(pe, &paths, entry) {
6071 if (asprintf(&ondisk_path, "%s/%s",
6072 got_worktree_get_root_path(worktree),
6073 pe->path) == -1) {
6074 error = got_error_from_errno("asprintf");
6075 goto done;
6077 if (lstat(ondisk_path, &sb) == -1) {
6078 if (errno == ENOENT) {
6079 free(ondisk_path);
6080 continue;
6082 error = got_error_from_errno2("lstat",
6083 ondisk_path);
6084 free(ondisk_path);
6085 goto done;
6087 free(ondisk_path);
6088 if (S_ISDIR(sb.st_mode)) {
6089 error = got_error_msg(GOT_ERR_BAD_PATH,
6090 "removing directories requires -R option");
6091 goto done;
6096 error = got_worktree_schedule_delete(worktree, &paths,
6097 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6098 done:
6099 if (repo)
6100 got_repo_close(repo);
6101 if (worktree)
6102 got_worktree_close(worktree);
6103 TAILQ_FOREACH(pe, &paths, entry)
6104 free((char *)pe->path);
6105 got_pathlist_free(&paths);
6106 free(cwd);
6107 return error;
6110 __dead static void
6111 usage_revert(void)
6113 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6114 "path ...\n", getprogname());
6115 exit(1);
6118 static const struct got_error *
6119 revert_progress(void *arg, unsigned char status, const char *path)
6121 if (status == GOT_STATUS_UNVERSIONED)
6122 return NULL;
6124 while (path[0] == '/')
6125 path++;
6126 printf("%c %s\n", status, path);
6127 return NULL;
6130 struct choose_patch_arg {
6131 FILE *patch_script_file;
6132 const char *action;
6135 static const struct got_error *
6136 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6137 int nchanges, const char *action)
6139 char *line = NULL;
6140 size_t linesize = 0;
6141 ssize_t linelen;
6143 switch (status) {
6144 case GOT_STATUS_ADD:
6145 printf("A %s\n%s this addition? [y/n] ", path, action);
6146 break;
6147 case GOT_STATUS_DELETE:
6148 printf("D %s\n%s this deletion? [y/n] ", path, action);
6149 break;
6150 case GOT_STATUS_MODIFY:
6151 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6152 return got_error_from_errno("fseek");
6153 printf(GOT_COMMIT_SEP_STR);
6154 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6155 printf("%s", line);
6156 if (ferror(patch_file))
6157 return got_error_from_errno("getline");
6158 printf(GOT_COMMIT_SEP_STR);
6159 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6160 path, n, nchanges, action);
6161 break;
6162 default:
6163 return got_error_path(path, GOT_ERR_FILE_STATUS);
6166 return NULL;
6169 static const struct got_error *
6170 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6171 FILE *patch_file, int n, int nchanges)
6173 const struct got_error *err = NULL;
6174 char *line = NULL;
6175 size_t linesize = 0;
6176 ssize_t linelen;
6177 int resp = ' ';
6178 struct choose_patch_arg *a = arg;
6180 *choice = GOT_PATCH_CHOICE_NONE;
6182 if (a->patch_script_file) {
6183 char *nl;
6184 err = show_change(status, path, patch_file, n, nchanges,
6185 a->action);
6186 if (err)
6187 return err;
6188 linelen = getline(&line, &linesize, a->patch_script_file);
6189 if (linelen == -1) {
6190 if (ferror(a->patch_script_file))
6191 return got_error_from_errno("getline");
6192 return NULL;
6194 nl = strchr(line, '\n');
6195 if (nl)
6196 *nl = '\0';
6197 if (strcmp(line, "y") == 0) {
6198 *choice = GOT_PATCH_CHOICE_YES;
6199 printf("y\n");
6200 } else if (strcmp(line, "n") == 0) {
6201 *choice = GOT_PATCH_CHOICE_NO;
6202 printf("n\n");
6203 } else if (strcmp(line, "q") == 0 &&
6204 status == GOT_STATUS_MODIFY) {
6205 *choice = GOT_PATCH_CHOICE_QUIT;
6206 printf("q\n");
6207 } else
6208 printf("invalid response '%s'\n", line);
6209 free(line);
6210 return NULL;
6213 while (resp != 'y' && resp != 'n' && resp != 'q') {
6214 err = show_change(status, path, patch_file, n, nchanges,
6215 a->action);
6216 if (err)
6217 return err;
6218 resp = getchar();
6219 if (resp == '\n')
6220 resp = getchar();
6221 if (status == GOT_STATUS_MODIFY) {
6222 if (resp != 'y' && resp != 'n' && resp != 'q') {
6223 printf("invalid response '%c'\n", resp);
6224 resp = ' ';
6226 } else if (resp != 'y' && resp != 'n') {
6227 printf("invalid response '%c'\n", resp);
6228 resp = ' ';
6232 if (resp == 'y')
6233 *choice = GOT_PATCH_CHOICE_YES;
6234 else if (resp == 'n')
6235 *choice = GOT_PATCH_CHOICE_NO;
6236 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6237 *choice = GOT_PATCH_CHOICE_QUIT;
6239 return NULL;
6243 static const struct got_error *
6244 cmd_revert(int argc, char *argv[])
6246 const struct got_error *error = NULL;
6247 struct got_worktree *worktree = NULL;
6248 struct got_repository *repo = NULL;
6249 char *cwd = NULL, *path = NULL;
6250 struct got_pathlist_head paths;
6251 struct got_pathlist_entry *pe;
6252 int ch, can_recurse = 0, pflag = 0;
6253 FILE *patch_script_file = NULL;
6254 const char *patch_script_path = NULL;
6255 struct choose_patch_arg cpa;
6257 TAILQ_INIT(&paths);
6259 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6260 switch (ch) {
6261 case 'p':
6262 pflag = 1;
6263 break;
6264 case 'F':
6265 patch_script_path = optarg;
6266 break;
6267 case 'R':
6268 can_recurse = 1;
6269 break;
6270 default:
6271 usage_revert();
6272 /* NOTREACHED */
6276 argc -= optind;
6277 argv += optind;
6279 #ifndef PROFILE
6280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6281 "unveil", NULL) == -1)
6282 err(1, "pledge");
6283 #endif
6284 if (argc < 1)
6285 usage_revert();
6286 if (patch_script_path && !pflag)
6287 errx(1, "-F option can only be used together with -p option");
6289 cwd = getcwd(NULL, 0);
6290 if (cwd == NULL) {
6291 error = got_error_from_errno("getcwd");
6292 goto done;
6294 error = got_worktree_open(&worktree, cwd);
6295 if (error) {
6296 if (error->code == GOT_ERR_NOT_WORKTREE)
6297 error = wrap_not_worktree_error(error, "revert", cwd);
6298 goto done;
6301 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6302 NULL);
6303 if (error != NULL)
6304 goto done;
6306 if (patch_script_path) {
6307 patch_script_file = fopen(patch_script_path, "r");
6308 if (patch_script_file == NULL) {
6309 error = got_error_from_errno2("fopen",
6310 patch_script_path);
6311 goto done;
6314 error = apply_unveil(got_repo_get_path(repo), 1,
6315 got_worktree_get_root_path(worktree));
6316 if (error)
6317 goto done;
6319 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6320 if (error)
6321 goto done;
6323 if (!can_recurse) {
6324 char *ondisk_path;
6325 struct stat sb;
6326 TAILQ_FOREACH(pe, &paths, entry) {
6327 if (asprintf(&ondisk_path, "%s/%s",
6328 got_worktree_get_root_path(worktree),
6329 pe->path) == -1) {
6330 error = got_error_from_errno("asprintf");
6331 goto done;
6333 if (lstat(ondisk_path, &sb) == -1) {
6334 if (errno == ENOENT) {
6335 free(ondisk_path);
6336 continue;
6338 error = got_error_from_errno2("lstat",
6339 ondisk_path);
6340 free(ondisk_path);
6341 goto done;
6343 free(ondisk_path);
6344 if (S_ISDIR(sb.st_mode)) {
6345 error = got_error_msg(GOT_ERR_BAD_PATH,
6346 "reverting directories requires -R option");
6347 goto done;
6352 cpa.patch_script_file = patch_script_file;
6353 cpa.action = "revert";
6354 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6355 pflag ? choose_patch : NULL, &cpa, repo);
6356 done:
6357 if (patch_script_file && fclose(patch_script_file) == EOF &&
6358 error == NULL)
6359 error = got_error_from_errno2("fclose", patch_script_path);
6360 if (repo)
6361 got_repo_close(repo);
6362 if (worktree)
6363 got_worktree_close(worktree);
6364 free(path);
6365 free(cwd);
6366 return error;
6369 __dead static void
6370 usage_commit(void)
6372 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6373 getprogname());
6374 exit(1);
6377 struct collect_commit_logmsg_arg {
6378 const char *cmdline_log;
6379 const char *editor;
6380 const char *worktree_path;
6381 const char *branch_name;
6382 const char *repo_path;
6383 char *logmsg_path;
6387 static const struct got_error *
6388 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6389 void *arg)
6391 char *initial_content = NULL;
6392 struct got_pathlist_entry *pe;
6393 const struct got_error *err = NULL;
6394 char *template = NULL;
6395 struct collect_commit_logmsg_arg *a = arg;
6396 int fd;
6397 size_t len;
6399 /* if a message was specified on the command line, just use it */
6400 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6401 len = strlen(a->cmdline_log) + 1;
6402 *logmsg = malloc(len + 1);
6403 if (*logmsg == NULL)
6404 return got_error_from_errno("malloc");
6405 strlcpy(*logmsg, a->cmdline_log, len);
6406 return NULL;
6409 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6410 return got_error_from_errno("asprintf");
6412 if (asprintf(&initial_content,
6413 "\n# changes to be committed on branch %s:\n",
6414 a->branch_name) == -1)
6415 return got_error_from_errno("asprintf");
6417 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6418 if (err)
6419 goto done;
6421 dprintf(fd, initial_content);
6423 TAILQ_FOREACH(pe, commitable_paths, entry) {
6424 struct got_commitable *ct = pe->data;
6425 dprintf(fd, "# %c %s\n",
6426 got_commitable_get_status(ct),
6427 got_commitable_get_path(ct));
6429 close(fd);
6431 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6432 done:
6433 free(initial_content);
6434 free(template);
6436 /* Editor is done; we can now apply unveil(2) */
6437 if (err == NULL) {
6438 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6439 if (err) {
6440 free(*logmsg);
6441 *logmsg = NULL;
6444 return err;
6447 static const struct got_error *
6448 cmd_commit(int argc, char *argv[])
6450 const struct got_error *error = NULL;
6451 struct got_worktree *worktree = NULL;
6452 struct got_repository *repo = NULL;
6453 char *cwd = NULL, *id_str = NULL;
6454 struct got_object_id *id = NULL;
6455 const char *logmsg = NULL;
6456 struct collect_commit_logmsg_arg cl_arg;
6457 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6458 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6459 int allow_bad_symlinks = 0;
6460 struct got_pathlist_head paths;
6462 TAILQ_INIT(&paths);
6463 cl_arg.logmsg_path = NULL;
6465 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6466 switch (ch) {
6467 case 'm':
6468 logmsg = optarg;
6469 break;
6470 case 'S':
6471 allow_bad_symlinks = 1;
6472 break;
6473 default:
6474 usage_commit();
6475 /* NOTREACHED */
6479 argc -= optind;
6480 argv += optind;
6482 #ifndef PROFILE
6483 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6484 "unveil", NULL) == -1)
6485 err(1, "pledge");
6486 #endif
6487 cwd = getcwd(NULL, 0);
6488 if (cwd == NULL) {
6489 error = got_error_from_errno("getcwd");
6490 goto done;
6492 error = got_worktree_open(&worktree, cwd);
6493 if (error) {
6494 if (error->code == GOT_ERR_NOT_WORKTREE)
6495 error = wrap_not_worktree_error(error, "commit", cwd);
6496 goto done;
6499 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6500 if (error)
6501 goto done;
6502 if (rebase_in_progress) {
6503 error = got_error(GOT_ERR_REBASING);
6504 goto done;
6507 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6508 worktree);
6509 if (error)
6510 goto done;
6512 error = get_gitconfig_path(&gitconfig_path);
6513 if (error)
6514 goto done;
6515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6516 gitconfig_path);
6517 if (error != NULL)
6518 goto done;
6520 error = get_author(&author, repo);
6521 if (error)
6522 return error;
6525 * unveil(2) traverses exec(2); if an editor is used we have
6526 * to apply unveil after the log message has been written.
6528 if (logmsg == NULL || strlen(logmsg) == 0)
6529 error = get_editor(&editor);
6530 else
6531 error = apply_unveil(got_repo_get_path(repo), 0,
6532 got_worktree_get_root_path(worktree));
6533 if (error)
6534 goto done;
6536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6537 if (error)
6538 goto done;
6540 cl_arg.editor = editor;
6541 cl_arg.cmdline_log = logmsg;
6542 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6543 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6544 if (!histedit_in_progress) {
6545 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6546 error = got_error(GOT_ERR_COMMIT_BRANCH);
6547 goto done;
6549 cl_arg.branch_name += 11;
6551 cl_arg.repo_path = got_repo_get_path(repo);
6552 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6553 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6554 print_status, NULL, repo);
6555 if (error) {
6556 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6557 cl_arg.logmsg_path != NULL)
6558 preserve_logmsg = 1;
6559 goto done;
6562 error = got_object_id_str(&id_str, id);
6563 if (error)
6564 goto done;
6565 printf("Created commit %s\n", id_str);
6566 done:
6567 if (preserve_logmsg) {
6568 fprintf(stderr, "%s: log message preserved in %s\n",
6569 getprogname(), cl_arg.logmsg_path);
6570 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6571 error == NULL)
6572 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6573 free(cl_arg.logmsg_path);
6574 if (repo)
6575 got_repo_close(repo);
6576 if (worktree)
6577 got_worktree_close(worktree);
6578 free(cwd);
6579 free(id_str);
6580 free(gitconfig_path);
6581 free(editor);
6582 free(author);
6583 return error;
6586 __dead static void
6587 usage_cherrypick(void)
6589 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6590 exit(1);
6593 static const struct got_error *
6594 cmd_cherrypick(int argc, char *argv[])
6596 const struct got_error *error = NULL;
6597 struct got_worktree *worktree = NULL;
6598 struct got_repository *repo = NULL;
6599 char *cwd = NULL, *commit_id_str = NULL;
6600 struct got_object_id *commit_id = NULL;
6601 struct got_commit_object *commit = NULL;
6602 struct got_object_qid *pid;
6603 struct got_reference *head_ref = NULL;
6604 int ch;
6605 struct got_update_progress_arg upa;
6607 while ((ch = getopt(argc, argv, "")) != -1) {
6608 switch (ch) {
6609 default:
6610 usage_cherrypick();
6611 /* NOTREACHED */
6615 argc -= optind;
6616 argv += optind;
6618 #ifndef PROFILE
6619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6620 "unveil", NULL) == -1)
6621 err(1, "pledge");
6622 #endif
6623 if (argc != 1)
6624 usage_cherrypick();
6626 cwd = getcwd(NULL, 0);
6627 if (cwd == NULL) {
6628 error = got_error_from_errno("getcwd");
6629 goto done;
6631 error = got_worktree_open(&worktree, cwd);
6632 if (error) {
6633 if (error->code == GOT_ERR_NOT_WORKTREE)
6634 error = wrap_not_worktree_error(error, "cherrypick",
6635 cwd);
6636 goto done;
6639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6640 NULL);
6641 if (error != NULL)
6642 goto done;
6644 error = apply_unveil(got_repo_get_path(repo), 0,
6645 got_worktree_get_root_path(worktree));
6646 if (error)
6647 goto done;
6649 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6650 GOT_OBJ_TYPE_COMMIT, repo);
6651 if (error != NULL) {
6652 struct got_reference *ref;
6653 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6654 goto done;
6655 error = got_ref_open(&ref, repo, argv[0], 0);
6656 if (error != NULL)
6657 goto done;
6658 error = got_ref_resolve(&commit_id, repo, ref);
6659 got_ref_close(ref);
6660 if (error != NULL)
6661 goto done;
6663 error = got_object_id_str(&commit_id_str, commit_id);
6664 if (error)
6665 goto done;
6667 error = got_ref_open(&head_ref, repo,
6668 got_worktree_get_head_ref_name(worktree), 0);
6669 if (error != NULL)
6670 goto done;
6672 error = check_same_branch(commit_id, head_ref, NULL, repo);
6673 if (error) {
6674 if (error->code != GOT_ERR_ANCESTRY)
6675 goto done;
6676 error = NULL;
6677 } else {
6678 error = got_error(GOT_ERR_SAME_BRANCH);
6679 goto done;
6682 error = got_object_open_as_commit(&commit, repo, commit_id);
6683 if (error)
6684 goto done;
6685 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6686 memset(&upa, 0, sizeof(upa));
6687 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6688 commit_id, repo, update_progress, &upa, check_cancelled,
6689 NULL);
6690 if (error != NULL)
6691 goto done;
6693 if (upa.did_something)
6694 printf("Merged commit %s\n", commit_id_str);
6695 print_update_progress_stats(&upa);
6696 done:
6697 if (commit)
6698 got_object_commit_close(commit);
6699 free(commit_id_str);
6700 if (head_ref)
6701 got_ref_close(head_ref);
6702 if (worktree)
6703 got_worktree_close(worktree);
6704 if (repo)
6705 got_repo_close(repo);
6706 return error;
6709 __dead static void
6710 usage_backout(void)
6712 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6713 exit(1);
6716 static const struct got_error *
6717 cmd_backout(int argc, char *argv[])
6719 const struct got_error *error = NULL;
6720 struct got_worktree *worktree = NULL;
6721 struct got_repository *repo = NULL;
6722 char *cwd = NULL, *commit_id_str = NULL;
6723 struct got_object_id *commit_id = NULL;
6724 struct got_commit_object *commit = NULL;
6725 struct got_object_qid *pid;
6726 struct got_reference *head_ref = NULL;
6727 int ch;
6728 struct got_update_progress_arg upa;
6730 while ((ch = getopt(argc, argv, "")) != -1) {
6731 switch (ch) {
6732 default:
6733 usage_backout();
6734 /* NOTREACHED */
6738 argc -= optind;
6739 argv += optind;
6741 #ifndef PROFILE
6742 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6743 "unveil", NULL) == -1)
6744 err(1, "pledge");
6745 #endif
6746 if (argc != 1)
6747 usage_backout();
6749 cwd = getcwd(NULL, 0);
6750 if (cwd == NULL) {
6751 error = got_error_from_errno("getcwd");
6752 goto done;
6754 error = got_worktree_open(&worktree, cwd);
6755 if (error) {
6756 if (error->code == GOT_ERR_NOT_WORKTREE)
6757 error = wrap_not_worktree_error(error, "backout", cwd);
6758 goto done;
6761 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6762 NULL);
6763 if (error != NULL)
6764 goto done;
6766 error = apply_unveil(got_repo_get_path(repo), 0,
6767 got_worktree_get_root_path(worktree));
6768 if (error)
6769 goto done;
6771 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6772 GOT_OBJ_TYPE_COMMIT, repo);
6773 if (error != NULL) {
6774 struct got_reference *ref;
6775 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6776 goto done;
6777 error = got_ref_open(&ref, repo, argv[0], 0);
6778 if (error != NULL)
6779 goto done;
6780 error = got_ref_resolve(&commit_id, repo, ref);
6781 got_ref_close(ref);
6782 if (error != NULL)
6783 goto done;
6785 error = got_object_id_str(&commit_id_str, commit_id);
6786 if (error)
6787 goto done;
6789 error = got_ref_open(&head_ref, repo,
6790 got_worktree_get_head_ref_name(worktree), 0);
6791 if (error != NULL)
6792 goto done;
6794 error = check_same_branch(commit_id, head_ref, NULL, repo);
6795 if (error)
6796 goto done;
6798 error = got_object_open_as_commit(&commit, repo, commit_id);
6799 if (error)
6800 goto done;
6801 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6802 if (pid == NULL) {
6803 error = got_error(GOT_ERR_ROOT_COMMIT);
6804 goto done;
6807 memset(&upa, 0, sizeof(upa));
6808 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6809 update_progress, &upa, check_cancelled, NULL);
6810 if (error != NULL)
6811 goto done;
6813 if (upa.did_something)
6814 printf("Backed out commit %s\n", commit_id_str);
6815 print_update_progress_stats(&upa);
6816 done:
6817 if (commit)
6818 got_object_commit_close(commit);
6819 free(commit_id_str);
6820 if (head_ref)
6821 got_ref_close(head_ref);
6822 if (worktree)
6823 got_worktree_close(worktree);
6824 if (repo)
6825 got_repo_close(repo);
6826 return error;
6829 __dead static void
6830 usage_rebase(void)
6832 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6833 getprogname());
6834 exit(1);
6837 void
6838 trim_logmsg(char *logmsg, int limit)
6840 char *nl;
6841 size_t len;
6843 len = strlen(logmsg);
6844 if (len > limit)
6845 len = limit;
6846 logmsg[len] = '\0';
6847 nl = strchr(logmsg, '\n');
6848 if (nl)
6849 *nl = '\0';
6852 static const struct got_error *
6853 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6855 const struct got_error *err;
6856 char *logmsg0 = NULL;
6857 const char *s;
6859 err = got_object_commit_get_logmsg(&logmsg0, commit);
6860 if (err)
6861 return err;
6863 s = logmsg0;
6864 while (isspace((unsigned char)s[0]))
6865 s++;
6867 *logmsg = strdup(s);
6868 if (*logmsg == NULL) {
6869 err = got_error_from_errno("strdup");
6870 goto done;
6873 trim_logmsg(*logmsg, limit);
6874 done:
6875 free(logmsg0);
6876 return err;
6879 static const struct got_error *
6880 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6882 const struct got_error *err;
6883 struct got_commit_object *commit = NULL;
6884 char *id_str = NULL, *logmsg = NULL;
6886 err = got_object_open_as_commit(&commit, repo, id);
6887 if (err)
6888 return err;
6890 err = got_object_id_str(&id_str, id);
6891 if (err)
6892 goto done;
6894 id_str[12] = '\0';
6896 err = get_short_logmsg(&logmsg, 42, commit);
6897 if (err)
6898 goto done;
6900 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6901 done:
6902 free(id_str);
6903 got_object_commit_close(commit);
6904 free(logmsg);
6905 return err;
6908 static const struct got_error *
6909 show_rebase_progress(struct got_commit_object *commit,
6910 struct got_object_id *old_id, struct got_object_id *new_id)
6912 const struct got_error *err;
6913 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6915 err = got_object_id_str(&old_id_str, old_id);
6916 if (err)
6917 goto done;
6919 if (new_id) {
6920 err = got_object_id_str(&new_id_str, new_id);
6921 if (err)
6922 goto done;
6925 old_id_str[12] = '\0';
6926 if (new_id_str)
6927 new_id_str[12] = '\0';
6929 err = get_short_logmsg(&logmsg, 42, commit);
6930 if (err)
6931 goto done;
6933 printf("%s -> %s: %s\n", old_id_str,
6934 new_id_str ? new_id_str : "no-op change", logmsg);
6935 done:
6936 free(old_id_str);
6937 free(new_id_str);
6938 free(logmsg);
6939 return err;
6942 static const struct got_error *
6943 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6944 struct got_reference *branch, struct got_reference *new_base_branch,
6945 struct got_reference *tmp_branch, struct got_repository *repo)
6947 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6948 return got_worktree_rebase_complete(worktree, fileindex,
6949 new_base_branch, tmp_branch, branch, repo);
6952 static const struct got_error *
6953 rebase_commit(struct got_pathlist_head *merged_paths,
6954 struct got_worktree *worktree, struct got_fileindex *fileindex,
6955 struct got_reference *tmp_branch,
6956 struct got_object_id *commit_id, struct got_repository *repo)
6958 const struct got_error *error;
6959 struct got_commit_object *commit;
6960 struct got_object_id *new_commit_id;
6962 error = got_object_open_as_commit(&commit, repo, commit_id);
6963 if (error)
6964 return error;
6966 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6967 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6968 if (error) {
6969 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6970 goto done;
6971 error = show_rebase_progress(commit, commit_id, NULL);
6972 } else {
6973 error = show_rebase_progress(commit, commit_id, new_commit_id);
6974 free(new_commit_id);
6976 done:
6977 got_object_commit_close(commit);
6978 return error;
6981 struct check_path_prefix_arg {
6982 const char *path_prefix;
6983 size_t len;
6984 int errcode;
6987 static const struct got_error *
6988 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6989 struct got_blob_object *blob2, struct got_object_id *id1,
6990 struct got_object_id *id2, const char *path1, const char *path2,
6991 mode_t mode1, mode_t mode2, struct got_repository *repo)
6993 struct check_path_prefix_arg *a = arg;
6995 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6996 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6997 return got_error(a->errcode);
6999 return NULL;
7002 static const struct got_error *
7003 check_path_prefix(struct got_object_id *parent_id,
7004 struct got_object_id *commit_id, const char *path_prefix,
7005 int errcode, struct got_repository *repo)
7007 const struct got_error *err;
7008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7009 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7010 struct check_path_prefix_arg cpp_arg;
7012 if (got_path_is_root_dir(path_prefix))
7013 return NULL;
7015 err = got_object_open_as_commit(&commit, repo, commit_id);
7016 if (err)
7017 goto done;
7019 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7020 if (err)
7021 goto done;
7023 err = got_object_open_as_tree(&tree1, repo,
7024 got_object_commit_get_tree_id(parent_commit));
7025 if (err)
7026 goto done;
7028 err = got_object_open_as_tree(&tree2, repo,
7029 got_object_commit_get_tree_id(commit));
7030 if (err)
7031 goto done;
7033 cpp_arg.path_prefix = path_prefix;
7034 while (cpp_arg.path_prefix[0] == '/')
7035 cpp_arg.path_prefix++;
7036 cpp_arg.len = strlen(cpp_arg.path_prefix);
7037 cpp_arg.errcode = errcode;
7038 err = got_diff_tree(tree1, tree2, "", "", repo,
7039 check_path_prefix_in_diff, &cpp_arg, 0);
7040 done:
7041 if (tree1)
7042 got_object_tree_close(tree1);
7043 if (tree2)
7044 got_object_tree_close(tree2);
7045 if (commit)
7046 got_object_commit_close(commit);
7047 if (parent_commit)
7048 got_object_commit_close(parent_commit);
7049 return err;
7052 static const struct got_error *
7053 collect_commits(struct got_object_id_queue *commits,
7054 struct got_object_id *initial_commit_id,
7055 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7056 const char *path_prefix, int path_prefix_errcode,
7057 struct got_repository *repo)
7059 const struct got_error *err = NULL;
7060 struct got_commit_graph *graph = NULL;
7061 struct got_object_id *parent_id = NULL;
7062 struct got_object_qid *qid;
7063 struct got_object_id *commit_id = initial_commit_id;
7065 err = got_commit_graph_open(&graph, "/", 1);
7066 if (err)
7067 return err;
7069 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7070 check_cancelled, NULL);
7071 if (err)
7072 goto done;
7073 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7074 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7075 check_cancelled, NULL);
7076 if (err) {
7077 if (err->code == GOT_ERR_ITER_COMPLETED) {
7078 err = got_error_msg(GOT_ERR_ANCESTRY,
7079 "ran out of commits to rebase before "
7080 "youngest common ancestor commit has "
7081 "been reached?!?");
7083 goto done;
7084 } else {
7085 err = check_path_prefix(parent_id, commit_id,
7086 path_prefix, path_prefix_errcode, repo);
7087 if (err)
7088 goto done;
7090 err = got_object_qid_alloc(&qid, commit_id);
7091 if (err)
7092 goto done;
7093 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7094 commit_id = parent_id;
7097 done:
7098 got_commit_graph_close(graph);
7099 return err;
7102 static const struct got_error *
7103 cmd_rebase(int argc, char *argv[])
7105 const struct got_error *error = NULL;
7106 struct got_worktree *worktree = NULL;
7107 struct got_repository *repo = NULL;
7108 struct got_fileindex *fileindex = NULL;
7109 char *cwd = NULL;
7110 struct got_reference *branch = NULL;
7111 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7112 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7113 struct got_object_id *resume_commit_id = NULL;
7114 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7115 struct got_commit_object *commit = NULL;
7116 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7117 int histedit_in_progress = 0;
7118 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7119 struct got_object_id_queue commits;
7120 struct got_pathlist_head merged_paths;
7121 const struct got_object_id_queue *parent_ids;
7122 struct got_object_qid *qid, *pid;
7124 SIMPLEQ_INIT(&commits);
7125 TAILQ_INIT(&merged_paths);
7127 while ((ch = getopt(argc, argv, "ac")) != -1) {
7128 switch (ch) {
7129 case 'a':
7130 abort_rebase = 1;
7131 break;
7132 case 'c':
7133 continue_rebase = 1;
7134 break;
7135 default:
7136 usage_rebase();
7137 /* NOTREACHED */
7141 argc -= optind;
7142 argv += optind;
7144 #ifndef PROFILE
7145 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7146 "unveil", NULL) == -1)
7147 err(1, "pledge");
7148 #endif
7149 if (abort_rebase && continue_rebase)
7150 usage_rebase();
7151 else if (abort_rebase || continue_rebase) {
7152 if (argc != 0)
7153 usage_rebase();
7154 } else if (argc != 1)
7155 usage_rebase();
7157 cwd = getcwd(NULL, 0);
7158 if (cwd == NULL) {
7159 error = got_error_from_errno("getcwd");
7160 goto done;
7162 error = got_worktree_open(&worktree, cwd);
7163 if (error) {
7164 if (error->code == GOT_ERR_NOT_WORKTREE)
7165 error = wrap_not_worktree_error(error, "rebase", cwd);
7166 goto done;
7169 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7170 NULL);
7171 if (error != NULL)
7172 goto done;
7174 error = apply_unveil(got_repo_get_path(repo), 0,
7175 got_worktree_get_root_path(worktree));
7176 if (error)
7177 goto done;
7179 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7180 worktree);
7181 if (error)
7182 goto done;
7183 if (histedit_in_progress) {
7184 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7185 goto done;
7188 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7189 if (error)
7190 goto done;
7192 if (abort_rebase) {
7193 struct got_update_progress_arg upa;
7194 if (!rebase_in_progress) {
7195 error = got_error(GOT_ERR_NOT_REBASING);
7196 goto done;
7198 error = got_worktree_rebase_continue(&resume_commit_id,
7199 &new_base_branch, &tmp_branch, &branch, &fileindex,
7200 worktree, repo);
7201 if (error)
7202 goto done;
7203 printf("Switching work tree to %s\n",
7204 got_ref_get_symref_target(new_base_branch));
7205 memset(&upa, 0, sizeof(upa));
7206 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7207 new_base_branch, update_progress, &upa);
7208 if (error)
7209 goto done;
7210 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7211 print_update_progress_stats(&upa);
7212 goto done; /* nothing else to do */
7215 if (continue_rebase) {
7216 if (!rebase_in_progress) {
7217 error = got_error(GOT_ERR_NOT_REBASING);
7218 goto done;
7220 error = got_worktree_rebase_continue(&resume_commit_id,
7221 &new_base_branch, &tmp_branch, &branch, &fileindex,
7222 worktree, repo);
7223 if (error)
7224 goto done;
7226 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7227 resume_commit_id, repo);
7228 if (error)
7229 goto done;
7231 yca_id = got_object_id_dup(resume_commit_id);
7232 if (yca_id == NULL) {
7233 error = got_error_from_errno("got_object_id_dup");
7234 goto done;
7236 } else {
7237 error = got_ref_open(&branch, repo, argv[0], 0);
7238 if (error != NULL)
7239 goto done;
7242 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7243 if (error)
7244 goto done;
7246 if (!continue_rebase) {
7247 struct got_object_id *base_commit_id;
7249 base_commit_id = got_worktree_get_base_commit_id(worktree);
7250 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7251 base_commit_id, branch_head_commit_id, repo,
7252 check_cancelled, NULL);
7253 if (error)
7254 goto done;
7255 if (yca_id == NULL) {
7256 error = got_error_msg(GOT_ERR_ANCESTRY,
7257 "specified branch shares no common ancestry "
7258 "with work tree's branch");
7259 goto done;
7262 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7263 if (error) {
7264 if (error->code != GOT_ERR_ANCESTRY)
7265 goto done;
7266 error = NULL;
7267 } else {
7268 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7269 "specified branch resolves to a commit which "
7270 "is already contained in work tree's branch");
7271 goto done;
7273 error = got_worktree_rebase_prepare(&new_base_branch,
7274 &tmp_branch, &fileindex, worktree, branch, repo);
7275 if (error)
7276 goto done;
7279 commit_id = branch_head_commit_id;
7280 error = got_object_open_as_commit(&commit, repo, commit_id);
7281 if (error)
7282 goto done;
7284 parent_ids = got_object_commit_get_parent_ids(commit);
7285 pid = SIMPLEQ_FIRST(parent_ids);
7286 if (pid == NULL) {
7287 if (!continue_rebase) {
7288 struct got_update_progress_arg upa;
7289 memset(&upa, 0, sizeof(upa));
7290 error = got_worktree_rebase_abort(worktree, fileindex,
7291 repo, new_base_branch, update_progress, &upa);
7292 if (error)
7293 goto done;
7294 printf("Rebase of %s aborted\n",
7295 got_ref_get_name(branch));
7296 print_update_progress_stats(&upa);
7299 error = got_error(GOT_ERR_EMPTY_REBASE);
7300 goto done;
7302 error = collect_commits(&commits, commit_id, pid->id,
7303 yca_id, got_worktree_get_path_prefix(worktree),
7304 GOT_ERR_REBASE_PATH, repo);
7305 got_object_commit_close(commit);
7306 commit = NULL;
7307 if (error)
7308 goto done;
7310 if (SIMPLEQ_EMPTY(&commits)) {
7311 if (continue_rebase) {
7312 error = rebase_complete(worktree, fileindex,
7313 branch, new_base_branch, tmp_branch, repo);
7314 goto done;
7315 } else {
7316 /* Fast-forward the reference of the branch. */
7317 struct got_object_id *new_head_commit_id;
7318 char *id_str;
7319 error = got_ref_resolve(&new_head_commit_id, repo,
7320 new_base_branch);
7321 if (error)
7322 goto done;
7323 error = got_object_id_str(&id_str, new_head_commit_id);
7324 printf("Forwarding %s to commit %s\n",
7325 got_ref_get_name(branch), id_str);
7326 free(id_str);
7327 error = got_ref_change_ref(branch,
7328 new_head_commit_id);
7329 if (error)
7330 goto done;
7334 pid = NULL;
7335 SIMPLEQ_FOREACH(qid, &commits, entry) {
7336 struct got_update_progress_arg upa;
7338 commit_id = qid->id;
7339 parent_id = pid ? pid->id : yca_id;
7340 pid = qid;
7342 memset(&upa, 0, sizeof(upa));
7343 error = got_worktree_rebase_merge_files(&merged_paths,
7344 worktree, fileindex, parent_id, commit_id, repo,
7345 update_progress, &upa, check_cancelled, NULL);
7346 if (error)
7347 goto done;
7349 print_update_progress_stats(&upa);
7350 if (upa.conflicts > 0)
7351 rebase_status = GOT_STATUS_CONFLICT;
7353 if (rebase_status == GOT_STATUS_CONFLICT) {
7354 error = show_rebase_merge_conflict(qid->id, repo);
7355 if (error)
7356 goto done;
7357 got_worktree_rebase_pathlist_free(&merged_paths);
7358 break;
7361 error = rebase_commit(&merged_paths, worktree, fileindex,
7362 tmp_branch, commit_id, repo);
7363 got_worktree_rebase_pathlist_free(&merged_paths);
7364 if (error)
7365 goto done;
7368 if (rebase_status == GOT_STATUS_CONFLICT) {
7369 error = got_worktree_rebase_postpone(worktree, fileindex);
7370 if (error)
7371 goto done;
7372 error = got_error_msg(GOT_ERR_CONFLICTS,
7373 "conflicts must be resolved before rebasing can continue");
7374 } else
7375 error = rebase_complete(worktree, fileindex, branch,
7376 new_base_branch, tmp_branch, repo);
7377 done:
7378 got_object_id_queue_free(&commits);
7379 free(branch_head_commit_id);
7380 free(resume_commit_id);
7381 free(yca_id);
7382 if (commit)
7383 got_object_commit_close(commit);
7384 if (branch)
7385 got_ref_close(branch);
7386 if (new_base_branch)
7387 got_ref_close(new_base_branch);
7388 if (tmp_branch)
7389 got_ref_close(tmp_branch);
7390 if (worktree)
7391 got_worktree_close(worktree);
7392 if (repo)
7393 got_repo_close(repo);
7394 return error;
7397 __dead static void
7398 usage_histedit(void)
7400 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7401 getprogname());
7402 exit(1);
7405 #define GOT_HISTEDIT_PICK 'p'
7406 #define GOT_HISTEDIT_EDIT 'e'
7407 #define GOT_HISTEDIT_FOLD 'f'
7408 #define GOT_HISTEDIT_DROP 'd'
7409 #define GOT_HISTEDIT_MESG 'm'
7411 static struct got_histedit_cmd {
7412 unsigned char code;
7413 const char *name;
7414 const char *desc;
7415 } got_histedit_cmds[] = {
7416 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7417 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7418 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7419 "be used" },
7420 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7421 { GOT_HISTEDIT_MESG, "mesg",
7422 "single-line log message for commit above (open editor if empty)" },
7425 struct got_histedit_list_entry {
7426 TAILQ_ENTRY(got_histedit_list_entry) entry;
7427 struct got_object_id *commit_id;
7428 const struct got_histedit_cmd *cmd;
7429 char *logmsg;
7431 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7433 static const struct got_error *
7434 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7435 FILE *f, struct got_repository *repo)
7437 const struct got_error *err = NULL;
7438 char *logmsg = NULL, *id_str = NULL;
7439 struct got_commit_object *commit = NULL;
7440 int n;
7442 err = got_object_open_as_commit(&commit, repo, commit_id);
7443 if (err)
7444 goto done;
7446 err = get_short_logmsg(&logmsg, 34, commit);
7447 if (err)
7448 goto done;
7450 err = got_object_id_str(&id_str, commit_id);
7451 if (err)
7452 goto done;
7454 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7455 if (n < 0)
7456 err = got_ferror(f, GOT_ERR_IO);
7457 done:
7458 if (commit)
7459 got_object_commit_close(commit);
7460 free(id_str);
7461 free(logmsg);
7462 return err;
7465 static const struct got_error *
7466 histedit_write_commit_list(struct got_object_id_queue *commits,
7467 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7469 const struct got_error *err = NULL;
7470 struct got_object_qid *qid;
7472 if (SIMPLEQ_EMPTY(commits))
7473 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7475 SIMPLEQ_FOREACH(qid, commits, entry) {
7476 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7477 f, repo);
7478 if (err)
7479 break;
7480 if (edit_logmsg_only) {
7481 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7482 if (n < 0) {
7483 err = got_ferror(f, GOT_ERR_IO);
7484 break;
7489 return err;
7492 static const struct got_error *
7493 write_cmd_list(FILE *f, const char *branch_name,
7494 struct got_object_id_queue *commits)
7496 const struct got_error *err = NULL;
7497 int n, i;
7498 char *id_str;
7499 struct got_object_qid *qid;
7501 qid = SIMPLEQ_FIRST(commits);
7502 err = got_object_id_str(&id_str, qid->id);
7503 if (err)
7504 return err;
7506 n = fprintf(f,
7507 "# Editing the history of branch '%s' starting at\n"
7508 "# commit %s\n"
7509 "# Commits will be processed in order from top to "
7510 "bottom of this file.\n", branch_name, id_str);
7511 if (n < 0) {
7512 err = got_ferror(f, GOT_ERR_IO);
7513 goto done;
7516 n = fprintf(f, "# Available histedit commands:\n");
7517 if (n < 0) {
7518 err = got_ferror(f, GOT_ERR_IO);
7519 goto done;
7522 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7523 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7524 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7525 cmd->desc);
7526 if (n < 0) {
7527 err = got_ferror(f, GOT_ERR_IO);
7528 break;
7531 done:
7532 free(id_str);
7533 return err;
7536 static const struct got_error *
7537 histedit_syntax_error(int lineno)
7539 static char msg[42];
7540 int ret;
7542 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7543 lineno);
7544 if (ret == -1 || ret >= sizeof(msg))
7545 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7547 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7550 static const struct got_error *
7551 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7552 char *logmsg, struct got_repository *repo)
7554 const struct got_error *err;
7555 struct got_commit_object *folded_commit = NULL;
7556 char *id_str, *folded_logmsg = NULL;
7558 err = got_object_id_str(&id_str, hle->commit_id);
7559 if (err)
7560 return err;
7562 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7563 if (err)
7564 goto done;
7566 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7567 if (err)
7568 goto done;
7569 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7570 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7571 folded_logmsg) == -1) {
7572 err = got_error_from_errno("asprintf");
7574 done:
7575 if (folded_commit)
7576 got_object_commit_close(folded_commit);
7577 free(id_str);
7578 free(folded_logmsg);
7579 return err;
7582 static struct got_histedit_list_entry *
7583 get_folded_commits(struct got_histedit_list_entry *hle)
7585 struct got_histedit_list_entry *prev, *folded = NULL;
7587 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7588 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7589 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7590 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7591 folded = prev;
7592 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7595 return folded;
7598 static const struct got_error *
7599 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7600 struct got_repository *repo)
7602 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7603 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7604 const struct got_error *err = NULL;
7605 struct got_commit_object *commit = NULL;
7606 int fd;
7607 struct got_histedit_list_entry *folded = NULL;
7609 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7610 if (err)
7611 return err;
7613 folded = get_folded_commits(hle);
7614 if (folded) {
7615 while (folded != hle) {
7616 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7617 folded = TAILQ_NEXT(folded, entry);
7618 continue;
7620 err = append_folded_commit_msg(&new_msg, folded,
7621 logmsg, repo);
7622 if (err)
7623 goto done;
7624 free(logmsg);
7625 logmsg = new_msg;
7626 folded = TAILQ_NEXT(folded, entry);
7630 err = got_object_id_str(&id_str, hle->commit_id);
7631 if (err)
7632 goto done;
7633 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7634 if (err)
7635 goto done;
7636 if (asprintf(&new_msg,
7637 "%s\n# original log message of commit %s: %s",
7638 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7639 err = got_error_from_errno("asprintf");
7640 goto done;
7642 free(logmsg);
7643 logmsg = new_msg;
7645 err = got_object_id_str(&id_str, hle->commit_id);
7646 if (err)
7647 goto done;
7649 err = got_opentemp_named_fd(&logmsg_path, &fd,
7650 GOT_TMPDIR_STR "/got-logmsg");
7651 if (err)
7652 goto done;
7654 dprintf(fd, logmsg);
7655 close(fd);
7657 err = get_editor(&editor);
7658 if (err)
7659 goto done;
7661 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7662 if (err) {
7663 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7664 goto done;
7665 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7667 done:
7668 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7669 err = got_error_from_errno2("unlink", logmsg_path);
7670 free(logmsg_path);
7671 free(logmsg);
7672 free(orig_logmsg);
7673 free(editor);
7674 if (commit)
7675 got_object_commit_close(commit);
7676 return err;
7679 static const struct got_error *
7680 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7681 FILE *f, struct got_repository *repo)
7683 const struct got_error *err = NULL;
7684 char *line = NULL, *p, *end;
7685 size_t size;
7686 ssize_t len;
7687 int lineno = 0, i;
7688 const struct got_histedit_cmd *cmd;
7689 struct got_object_id *commit_id = NULL;
7690 struct got_histedit_list_entry *hle = NULL;
7692 for (;;) {
7693 len = getline(&line, &size, f);
7694 if (len == -1) {
7695 const struct got_error *getline_err;
7696 if (feof(f))
7697 break;
7698 getline_err = got_error_from_errno("getline");
7699 err = got_ferror(f, getline_err->code);
7700 break;
7702 lineno++;
7703 p = line;
7704 while (isspace((unsigned char)p[0]))
7705 p++;
7706 if (p[0] == '#' || p[0] == '\0') {
7707 free(line);
7708 line = NULL;
7709 continue;
7711 cmd = NULL;
7712 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7713 cmd = &got_histedit_cmds[i];
7714 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7715 isspace((unsigned char)p[strlen(cmd->name)])) {
7716 p += strlen(cmd->name);
7717 break;
7719 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7720 p++;
7721 break;
7724 if (i == nitems(got_histedit_cmds)) {
7725 err = histedit_syntax_error(lineno);
7726 break;
7728 while (isspace((unsigned char)p[0]))
7729 p++;
7730 if (cmd->code == GOT_HISTEDIT_MESG) {
7731 if (hle == NULL || hle->logmsg != NULL) {
7732 err = got_error(GOT_ERR_HISTEDIT_CMD);
7733 break;
7735 if (p[0] == '\0') {
7736 err = histedit_edit_logmsg(hle, repo);
7737 if (err)
7738 break;
7739 } else {
7740 hle->logmsg = strdup(p);
7741 if (hle->logmsg == NULL) {
7742 err = got_error_from_errno("strdup");
7743 break;
7746 free(line);
7747 line = NULL;
7748 continue;
7749 } else {
7750 end = p;
7751 while (end[0] && !isspace((unsigned char)end[0]))
7752 end++;
7753 *end = '\0';
7755 err = got_object_resolve_id_str(&commit_id, repo, p);
7756 if (err) {
7757 /* override error code */
7758 err = histedit_syntax_error(lineno);
7759 break;
7762 hle = malloc(sizeof(*hle));
7763 if (hle == NULL) {
7764 err = got_error_from_errno("malloc");
7765 break;
7767 hle->cmd = cmd;
7768 hle->commit_id = commit_id;
7769 hle->logmsg = NULL;
7770 commit_id = NULL;
7771 free(line);
7772 line = NULL;
7773 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7776 free(line);
7777 free(commit_id);
7778 return err;
7781 static const struct got_error *
7782 histedit_check_script(struct got_histedit_list *histedit_cmds,
7783 struct got_object_id_queue *commits, struct got_repository *repo)
7785 const struct got_error *err = NULL;
7786 struct got_object_qid *qid;
7787 struct got_histedit_list_entry *hle;
7788 static char msg[92];
7789 char *id_str;
7791 if (TAILQ_EMPTY(histedit_cmds))
7792 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7793 "histedit script contains no commands");
7794 if (SIMPLEQ_EMPTY(commits))
7795 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7797 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7798 struct got_histedit_list_entry *hle2;
7799 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7800 if (hle == hle2)
7801 continue;
7802 if (got_object_id_cmp(hle->commit_id,
7803 hle2->commit_id) != 0)
7804 continue;
7805 err = got_object_id_str(&id_str, hle->commit_id);
7806 if (err)
7807 return err;
7808 snprintf(msg, sizeof(msg), "commit %s is listed "
7809 "more than once in histedit script", id_str);
7810 free(id_str);
7811 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7815 SIMPLEQ_FOREACH(qid, commits, entry) {
7816 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7817 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7818 break;
7820 if (hle == NULL) {
7821 err = got_object_id_str(&id_str, qid->id);
7822 if (err)
7823 return err;
7824 snprintf(msg, sizeof(msg),
7825 "commit %s missing from histedit script", id_str);
7826 free(id_str);
7827 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7831 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7832 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7833 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7834 "last commit in histedit script cannot be folded");
7836 return NULL;
7839 static const struct got_error *
7840 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7841 const char *path, struct got_object_id_queue *commits,
7842 struct got_repository *repo)
7844 const struct got_error *err = NULL;
7845 char *editor;
7846 FILE *f = NULL;
7848 err = get_editor(&editor);
7849 if (err)
7850 return err;
7852 if (spawn_editor(editor, path) == -1) {
7853 err = got_error_from_errno("failed spawning editor");
7854 goto done;
7857 f = fopen(path, "r");
7858 if (f == NULL) {
7859 err = got_error_from_errno("fopen");
7860 goto done;
7862 err = histedit_parse_list(histedit_cmds, f, repo);
7863 if (err)
7864 goto done;
7866 err = histedit_check_script(histedit_cmds, commits, repo);
7867 done:
7868 if (f && fclose(f) != 0 && err == NULL)
7869 err = got_error_from_errno("fclose");
7870 free(editor);
7871 return err;
7874 static const struct got_error *
7875 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7876 struct got_object_id_queue *, const char *, const char *,
7877 struct got_repository *);
7879 static const struct got_error *
7880 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7881 struct got_object_id_queue *commits, const char *branch_name,
7882 int edit_logmsg_only, struct got_repository *repo)
7884 const struct got_error *err;
7885 FILE *f = NULL;
7886 char *path = NULL;
7888 err = got_opentemp_named(&path, &f, "got-histedit");
7889 if (err)
7890 return err;
7892 err = write_cmd_list(f, branch_name, commits);
7893 if (err)
7894 goto done;
7896 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7897 if (err)
7898 goto done;
7900 if (edit_logmsg_only) {
7901 rewind(f);
7902 err = histedit_parse_list(histedit_cmds, f, repo);
7903 } else {
7904 if (fclose(f) != 0) {
7905 err = got_error_from_errno("fclose");
7906 goto done;
7908 f = NULL;
7909 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7910 if (err) {
7911 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7912 err->code != GOT_ERR_HISTEDIT_CMD)
7913 goto done;
7914 err = histedit_edit_list_retry(histedit_cmds, err,
7915 commits, path, branch_name, repo);
7918 done:
7919 if (f && fclose(f) != 0 && err == NULL)
7920 err = got_error_from_errno("fclose");
7921 if (path && unlink(path) != 0 && err == NULL)
7922 err = got_error_from_errno2("unlink", path);
7923 free(path);
7924 return err;
7927 static const struct got_error *
7928 histedit_save_list(struct got_histedit_list *histedit_cmds,
7929 struct got_worktree *worktree, struct got_repository *repo)
7931 const struct got_error *err = NULL;
7932 char *path = NULL;
7933 FILE *f = NULL;
7934 struct got_histedit_list_entry *hle;
7935 struct got_commit_object *commit = NULL;
7937 err = got_worktree_get_histedit_script_path(&path, worktree);
7938 if (err)
7939 return err;
7941 f = fopen(path, "w");
7942 if (f == NULL) {
7943 err = got_error_from_errno2("fopen", path);
7944 goto done;
7946 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7947 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7948 repo);
7949 if (err)
7950 break;
7952 if (hle->logmsg) {
7953 int n = fprintf(f, "%c %s\n",
7954 GOT_HISTEDIT_MESG, hle->logmsg);
7955 if (n < 0) {
7956 err = got_ferror(f, GOT_ERR_IO);
7957 break;
7961 done:
7962 if (f && fclose(f) != 0 && err == NULL)
7963 err = got_error_from_errno("fclose");
7964 free(path);
7965 if (commit)
7966 got_object_commit_close(commit);
7967 return err;
7970 void
7971 histedit_free_list(struct got_histedit_list *histedit_cmds)
7973 struct got_histedit_list_entry *hle;
7975 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7976 TAILQ_REMOVE(histedit_cmds, hle, entry);
7977 free(hle);
7981 static const struct got_error *
7982 histedit_load_list(struct got_histedit_list *histedit_cmds,
7983 const char *path, struct got_repository *repo)
7985 const struct got_error *err = NULL;
7986 FILE *f = NULL;
7988 f = fopen(path, "r");
7989 if (f == NULL) {
7990 err = got_error_from_errno2("fopen", path);
7991 goto done;
7994 err = histedit_parse_list(histedit_cmds, f, repo);
7995 done:
7996 if (f && fclose(f) != 0 && err == NULL)
7997 err = got_error_from_errno("fclose");
7998 return err;
8001 static const struct got_error *
8002 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8003 const struct got_error *edit_err, struct got_object_id_queue *commits,
8004 const char *path, const char *branch_name, struct got_repository *repo)
8006 const struct got_error *err = NULL, *prev_err = edit_err;
8007 int resp = ' ';
8009 while (resp != 'c' && resp != 'r' && resp != 'a') {
8010 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8011 "or (a)bort: ", getprogname(), prev_err->msg);
8012 resp = getchar();
8013 if (resp == '\n')
8014 resp = getchar();
8015 if (resp == 'c') {
8016 histedit_free_list(histedit_cmds);
8017 err = histedit_run_editor(histedit_cmds, path, commits,
8018 repo);
8019 if (err) {
8020 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8021 err->code != GOT_ERR_HISTEDIT_CMD)
8022 break;
8023 prev_err = err;
8024 resp = ' ';
8025 continue;
8027 break;
8028 } else if (resp == 'r') {
8029 histedit_free_list(histedit_cmds);
8030 err = histedit_edit_script(histedit_cmds,
8031 commits, branch_name, 0, repo);
8032 if (err) {
8033 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8034 err->code != GOT_ERR_HISTEDIT_CMD)
8035 break;
8036 prev_err = err;
8037 resp = ' ';
8038 continue;
8040 break;
8041 } else if (resp == 'a') {
8042 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8043 break;
8044 } else
8045 printf("invalid response '%c'\n", resp);
8048 return err;
8051 static const struct got_error *
8052 histedit_complete(struct got_worktree *worktree,
8053 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8054 struct got_reference *branch, struct got_repository *repo)
8056 printf("Switching work tree to %s\n",
8057 got_ref_get_symref_target(branch));
8058 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8059 branch, repo);
8062 static const struct got_error *
8063 show_histedit_progress(struct got_commit_object *commit,
8064 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8066 const struct got_error *err;
8067 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8069 err = got_object_id_str(&old_id_str, hle->commit_id);
8070 if (err)
8071 goto done;
8073 if (new_id) {
8074 err = got_object_id_str(&new_id_str, new_id);
8075 if (err)
8076 goto done;
8079 old_id_str[12] = '\0';
8080 if (new_id_str)
8081 new_id_str[12] = '\0';
8083 if (hle->logmsg) {
8084 logmsg = strdup(hle->logmsg);
8085 if (logmsg == NULL) {
8086 err = got_error_from_errno("strdup");
8087 goto done;
8089 trim_logmsg(logmsg, 42);
8090 } else {
8091 err = get_short_logmsg(&logmsg, 42, commit);
8092 if (err)
8093 goto done;
8096 switch (hle->cmd->code) {
8097 case GOT_HISTEDIT_PICK:
8098 case GOT_HISTEDIT_EDIT:
8099 printf("%s -> %s: %s\n", old_id_str,
8100 new_id_str ? new_id_str : "no-op change", logmsg);
8101 break;
8102 case GOT_HISTEDIT_DROP:
8103 case GOT_HISTEDIT_FOLD:
8104 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8105 logmsg);
8106 break;
8107 default:
8108 break;
8110 done:
8111 free(old_id_str);
8112 free(new_id_str);
8113 return err;
8116 static const struct got_error *
8117 histedit_commit(struct got_pathlist_head *merged_paths,
8118 struct got_worktree *worktree, struct got_fileindex *fileindex,
8119 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8120 struct got_repository *repo)
8122 const struct got_error *err;
8123 struct got_commit_object *commit;
8124 struct got_object_id *new_commit_id;
8126 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8127 && hle->logmsg == NULL) {
8128 err = histedit_edit_logmsg(hle, repo);
8129 if (err)
8130 return err;
8133 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8134 if (err)
8135 return err;
8137 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8138 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8139 hle->logmsg, repo);
8140 if (err) {
8141 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8142 goto done;
8143 err = show_histedit_progress(commit, hle, NULL);
8144 } else {
8145 err = show_histedit_progress(commit, hle, new_commit_id);
8146 free(new_commit_id);
8148 done:
8149 got_object_commit_close(commit);
8150 return err;
8153 static const struct got_error *
8154 histedit_skip_commit(struct got_histedit_list_entry *hle,
8155 struct got_worktree *worktree, struct got_repository *repo)
8157 const struct got_error *error;
8158 struct got_commit_object *commit;
8160 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8161 repo);
8162 if (error)
8163 return error;
8165 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8166 if (error)
8167 return error;
8169 error = show_histedit_progress(commit, hle, NULL);
8170 got_object_commit_close(commit);
8171 return error;
8174 static const struct got_error *
8175 check_local_changes(void *arg, unsigned char status,
8176 unsigned char staged_status, const char *path,
8177 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8178 struct got_object_id *commit_id, int dirfd, const char *de_name)
8180 int *have_local_changes = arg;
8182 switch (status) {
8183 case GOT_STATUS_ADD:
8184 case GOT_STATUS_DELETE:
8185 case GOT_STATUS_MODIFY:
8186 case GOT_STATUS_CONFLICT:
8187 *have_local_changes = 1;
8188 return got_error(GOT_ERR_CANCELLED);
8189 default:
8190 break;
8193 switch (staged_status) {
8194 case GOT_STATUS_ADD:
8195 case GOT_STATUS_DELETE:
8196 case GOT_STATUS_MODIFY:
8197 *have_local_changes = 1;
8198 return got_error(GOT_ERR_CANCELLED);
8199 default:
8200 break;
8203 return NULL;
8206 static const struct got_error *
8207 cmd_histedit(int argc, char *argv[])
8209 const struct got_error *error = NULL;
8210 struct got_worktree *worktree = NULL;
8211 struct got_fileindex *fileindex = NULL;
8212 struct got_repository *repo = NULL;
8213 char *cwd = NULL;
8214 struct got_reference *branch = NULL;
8215 struct got_reference *tmp_branch = NULL;
8216 struct got_object_id *resume_commit_id = NULL;
8217 struct got_object_id *base_commit_id = NULL;
8218 struct got_object_id *head_commit_id = NULL;
8219 struct got_commit_object *commit = NULL;
8220 int ch, rebase_in_progress = 0;
8221 struct got_update_progress_arg upa;
8222 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8223 int edit_logmsg_only = 0;
8224 const char *edit_script_path = NULL;
8225 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8226 struct got_object_id_queue commits;
8227 struct got_pathlist_head merged_paths;
8228 const struct got_object_id_queue *parent_ids;
8229 struct got_object_qid *pid;
8230 struct got_histedit_list histedit_cmds;
8231 struct got_histedit_list_entry *hle;
8233 SIMPLEQ_INIT(&commits);
8234 TAILQ_INIT(&histedit_cmds);
8235 TAILQ_INIT(&merged_paths);
8236 memset(&upa, 0, sizeof(upa));
8238 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8239 switch (ch) {
8240 case 'a':
8241 abort_edit = 1;
8242 break;
8243 case 'c':
8244 continue_edit = 1;
8245 break;
8246 case 'F':
8247 edit_script_path = optarg;
8248 break;
8249 case 'm':
8250 edit_logmsg_only = 1;
8251 break;
8252 default:
8253 usage_histedit();
8254 /* NOTREACHED */
8258 argc -= optind;
8259 argv += optind;
8261 #ifndef PROFILE
8262 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8263 "unveil", NULL) == -1)
8264 err(1, "pledge");
8265 #endif
8266 if (abort_edit && continue_edit)
8267 errx(1, "histedit's -a and -c options are mutually exclusive");
8268 if (edit_script_path && edit_logmsg_only)
8269 errx(1, "histedit's -F and -m options are mutually exclusive");
8270 if (abort_edit && edit_logmsg_only)
8271 errx(1, "histedit's -a and -m options are mutually exclusive");
8272 if (continue_edit && edit_logmsg_only)
8273 errx(1, "histedit's -c and -m options are mutually exclusive");
8274 if (argc != 0)
8275 usage_histedit();
8278 * This command cannot apply unveil(2) in all cases because the
8279 * user may choose to run an editor to edit the histedit script
8280 * and to edit individual commit log messages.
8281 * unveil(2) traverses exec(2); if an editor is used we have to
8282 * apply unveil after edit script and log messages have been written.
8283 * XXX TODO: Make use of unveil(2) where possible.
8286 cwd = getcwd(NULL, 0);
8287 if (cwd == NULL) {
8288 error = got_error_from_errno("getcwd");
8289 goto done;
8291 error = got_worktree_open(&worktree, cwd);
8292 if (error) {
8293 if (error->code == GOT_ERR_NOT_WORKTREE)
8294 error = wrap_not_worktree_error(error, "histedit", cwd);
8295 goto done;
8298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8299 NULL);
8300 if (error != NULL)
8301 goto done;
8303 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8304 if (error)
8305 goto done;
8306 if (rebase_in_progress) {
8307 error = got_error(GOT_ERR_REBASING);
8308 goto done;
8311 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8312 if (error)
8313 goto done;
8315 if (edit_in_progress && edit_logmsg_only) {
8316 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8317 "histedit operation is in progress in this "
8318 "work tree and must be continued or aborted "
8319 "before the -m option can be used");
8320 goto done;
8323 if (edit_in_progress && abort_edit) {
8324 error = got_worktree_histedit_continue(&resume_commit_id,
8325 &tmp_branch, &branch, &base_commit_id, &fileindex,
8326 worktree, repo);
8327 if (error)
8328 goto done;
8329 printf("Switching work tree to %s\n",
8330 got_ref_get_symref_target(branch));
8331 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8332 branch, base_commit_id, update_progress, &upa);
8333 if (error)
8334 goto done;
8335 printf("Histedit of %s aborted\n",
8336 got_ref_get_symref_target(branch));
8337 print_update_progress_stats(&upa);
8338 goto done; /* nothing else to do */
8339 } else if (abort_edit) {
8340 error = got_error(GOT_ERR_NOT_HISTEDIT);
8341 goto done;
8344 if (continue_edit) {
8345 char *path;
8347 if (!edit_in_progress) {
8348 error = got_error(GOT_ERR_NOT_HISTEDIT);
8349 goto done;
8352 error = got_worktree_get_histedit_script_path(&path, worktree);
8353 if (error)
8354 goto done;
8356 error = histedit_load_list(&histedit_cmds, path, repo);
8357 free(path);
8358 if (error)
8359 goto done;
8361 error = got_worktree_histedit_continue(&resume_commit_id,
8362 &tmp_branch, &branch, &base_commit_id, &fileindex,
8363 worktree, repo);
8364 if (error)
8365 goto done;
8367 error = got_ref_resolve(&head_commit_id, repo, branch);
8368 if (error)
8369 goto done;
8371 error = got_object_open_as_commit(&commit, repo,
8372 head_commit_id);
8373 if (error)
8374 goto done;
8375 parent_ids = got_object_commit_get_parent_ids(commit);
8376 pid = SIMPLEQ_FIRST(parent_ids);
8377 if (pid == NULL) {
8378 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8379 goto done;
8381 error = collect_commits(&commits, head_commit_id, pid->id,
8382 base_commit_id, got_worktree_get_path_prefix(worktree),
8383 GOT_ERR_HISTEDIT_PATH, repo);
8384 got_object_commit_close(commit);
8385 commit = NULL;
8386 if (error)
8387 goto done;
8388 } else {
8389 if (edit_in_progress) {
8390 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8391 goto done;
8394 error = got_ref_open(&branch, repo,
8395 got_worktree_get_head_ref_name(worktree), 0);
8396 if (error != NULL)
8397 goto done;
8399 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8400 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8401 "will not edit commit history of a branch outside "
8402 "the \"refs/heads/\" reference namespace");
8403 goto done;
8406 error = got_ref_resolve(&head_commit_id, repo, branch);
8407 got_ref_close(branch);
8408 branch = NULL;
8409 if (error)
8410 goto done;
8412 error = got_object_open_as_commit(&commit, repo,
8413 head_commit_id);
8414 if (error)
8415 goto done;
8416 parent_ids = got_object_commit_get_parent_ids(commit);
8417 pid = SIMPLEQ_FIRST(parent_ids);
8418 if (pid == NULL) {
8419 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8420 goto done;
8422 error = collect_commits(&commits, head_commit_id, pid->id,
8423 got_worktree_get_base_commit_id(worktree),
8424 got_worktree_get_path_prefix(worktree),
8425 GOT_ERR_HISTEDIT_PATH, repo);
8426 got_object_commit_close(commit);
8427 commit = NULL;
8428 if (error)
8429 goto done;
8431 if (SIMPLEQ_EMPTY(&commits)) {
8432 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8433 goto done;
8436 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8437 &base_commit_id, &fileindex, worktree, repo);
8438 if (error)
8439 goto done;
8441 if (edit_script_path) {
8442 error = histedit_load_list(&histedit_cmds,
8443 edit_script_path, repo);
8444 if (error) {
8445 got_worktree_histedit_abort(worktree, fileindex,
8446 repo, branch, base_commit_id,
8447 update_progress, &upa);
8448 print_update_progress_stats(&upa);
8449 goto done;
8451 } else {
8452 const char *branch_name;
8453 branch_name = got_ref_get_symref_target(branch);
8454 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8455 branch_name += 11;
8456 error = histedit_edit_script(&histedit_cmds, &commits,
8457 branch_name, edit_logmsg_only, repo);
8458 if (error) {
8459 got_worktree_histedit_abort(worktree, fileindex,
8460 repo, branch, base_commit_id,
8461 update_progress, &upa);
8462 print_update_progress_stats(&upa);
8463 goto done;
8468 error = histedit_save_list(&histedit_cmds, worktree,
8469 repo);
8470 if (error) {
8471 got_worktree_histedit_abort(worktree, fileindex,
8472 repo, branch, base_commit_id,
8473 update_progress, &upa);
8474 print_update_progress_stats(&upa);
8475 goto done;
8480 error = histedit_check_script(&histedit_cmds, &commits, repo);
8481 if (error)
8482 goto done;
8484 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8485 if (resume_commit_id) {
8486 if (got_object_id_cmp(hle->commit_id,
8487 resume_commit_id) != 0)
8488 continue;
8490 resume_commit_id = NULL;
8491 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8492 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8493 error = histedit_skip_commit(hle, worktree,
8494 repo);
8495 if (error)
8496 goto done;
8497 } else {
8498 struct got_pathlist_head paths;
8499 int have_changes = 0;
8501 TAILQ_INIT(&paths);
8502 error = got_pathlist_append(&paths, "", NULL);
8503 if (error)
8504 goto done;
8505 error = got_worktree_status(worktree, &paths,
8506 repo, check_local_changes, &have_changes,
8507 check_cancelled, NULL);
8508 got_pathlist_free(&paths);
8509 if (error) {
8510 if (error->code != GOT_ERR_CANCELLED)
8511 goto done;
8512 if (sigint_received || sigpipe_received)
8513 goto done;
8515 if (have_changes) {
8516 error = histedit_commit(NULL, worktree,
8517 fileindex, tmp_branch, hle, repo);
8518 if (error)
8519 goto done;
8520 } else {
8521 error = got_object_open_as_commit(
8522 &commit, repo, hle->commit_id);
8523 if (error)
8524 goto done;
8525 error = show_histedit_progress(commit,
8526 hle, NULL);
8527 got_object_commit_close(commit);
8528 commit = NULL;
8529 if (error)
8530 goto done;
8533 continue;
8536 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8537 error = histedit_skip_commit(hle, worktree, repo);
8538 if (error)
8539 goto done;
8540 continue;
8543 error = got_object_open_as_commit(&commit, repo,
8544 hle->commit_id);
8545 if (error)
8546 goto done;
8547 parent_ids = got_object_commit_get_parent_ids(commit);
8548 pid = SIMPLEQ_FIRST(parent_ids);
8550 error = got_worktree_histedit_merge_files(&merged_paths,
8551 worktree, fileindex, pid->id, hle->commit_id, repo,
8552 update_progress, &upa, check_cancelled, NULL);
8553 if (error)
8554 goto done;
8555 got_object_commit_close(commit);
8556 commit = NULL;
8558 print_update_progress_stats(&upa);
8559 if (upa.conflicts > 0)
8560 rebase_status = GOT_STATUS_CONFLICT;
8562 if (rebase_status == GOT_STATUS_CONFLICT) {
8563 error = show_rebase_merge_conflict(hle->commit_id,
8564 repo);
8565 if (error)
8566 goto done;
8567 got_worktree_rebase_pathlist_free(&merged_paths);
8568 break;
8571 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8572 char *id_str;
8573 error = got_object_id_str(&id_str, hle->commit_id);
8574 if (error)
8575 goto done;
8576 printf("Stopping histedit for amending commit %s\n",
8577 id_str);
8578 free(id_str);
8579 got_worktree_rebase_pathlist_free(&merged_paths);
8580 error = got_worktree_histedit_postpone(worktree,
8581 fileindex);
8582 goto done;
8585 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8586 error = histedit_skip_commit(hle, worktree, repo);
8587 if (error)
8588 goto done;
8589 continue;
8592 error = histedit_commit(&merged_paths, worktree, fileindex,
8593 tmp_branch, hle, repo);
8594 got_worktree_rebase_pathlist_free(&merged_paths);
8595 if (error)
8596 goto done;
8599 if (rebase_status == GOT_STATUS_CONFLICT) {
8600 error = got_worktree_histedit_postpone(worktree, fileindex);
8601 if (error)
8602 goto done;
8603 error = got_error_msg(GOT_ERR_CONFLICTS,
8604 "conflicts must be resolved before histedit can continue");
8605 } else
8606 error = histedit_complete(worktree, fileindex, tmp_branch,
8607 branch, repo);
8608 done:
8609 got_object_id_queue_free(&commits);
8610 histedit_free_list(&histedit_cmds);
8611 free(head_commit_id);
8612 free(base_commit_id);
8613 free(resume_commit_id);
8614 if (commit)
8615 got_object_commit_close(commit);
8616 if (branch)
8617 got_ref_close(branch);
8618 if (tmp_branch)
8619 got_ref_close(tmp_branch);
8620 if (worktree)
8621 got_worktree_close(worktree);
8622 if (repo)
8623 got_repo_close(repo);
8624 return error;
8627 __dead static void
8628 usage_integrate(void)
8630 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8631 exit(1);
8634 static const struct got_error *
8635 cmd_integrate(int argc, char *argv[])
8637 const struct got_error *error = NULL;
8638 struct got_repository *repo = NULL;
8639 struct got_worktree *worktree = NULL;
8640 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8641 const char *branch_arg = NULL;
8642 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8643 struct got_fileindex *fileindex = NULL;
8644 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8645 int ch;
8646 struct got_update_progress_arg upa;
8648 while ((ch = getopt(argc, argv, "")) != -1) {
8649 switch (ch) {
8650 default:
8651 usage_integrate();
8652 /* NOTREACHED */
8656 argc -= optind;
8657 argv += optind;
8659 if (argc != 1)
8660 usage_integrate();
8661 branch_arg = argv[0];
8663 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8664 "unveil", NULL) == -1)
8665 err(1, "pledge");
8667 cwd = getcwd(NULL, 0);
8668 if (cwd == NULL) {
8669 error = got_error_from_errno("getcwd");
8670 goto done;
8673 error = got_worktree_open(&worktree, cwd);
8674 if (error) {
8675 if (error->code == GOT_ERR_NOT_WORKTREE)
8676 error = wrap_not_worktree_error(error, "integrate",
8677 cwd);
8678 goto done;
8681 error = check_rebase_or_histedit_in_progress(worktree);
8682 if (error)
8683 goto done;
8685 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8686 NULL);
8687 if (error != NULL)
8688 goto done;
8690 error = apply_unveil(got_repo_get_path(repo), 0,
8691 got_worktree_get_root_path(worktree));
8692 if (error)
8693 goto done;
8695 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8696 error = got_error_from_errno("asprintf");
8697 goto done;
8700 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8701 &base_branch_ref, worktree, refname, repo);
8702 if (error)
8703 goto done;
8705 refname = strdup(got_ref_get_name(branch_ref));
8706 if (refname == NULL) {
8707 error = got_error_from_errno("strdup");
8708 got_worktree_integrate_abort(worktree, fileindex, repo,
8709 branch_ref, base_branch_ref);
8710 goto done;
8712 base_refname = strdup(got_ref_get_name(base_branch_ref));
8713 if (base_refname == NULL) {
8714 error = got_error_from_errno("strdup");
8715 got_worktree_integrate_abort(worktree, fileindex, repo,
8716 branch_ref, base_branch_ref);
8717 goto done;
8720 error = got_ref_resolve(&commit_id, repo, branch_ref);
8721 if (error)
8722 goto done;
8724 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8725 if (error)
8726 goto done;
8728 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8729 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8730 "specified branch has already been integrated");
8731 got_worktree_integrate_abort(worktree, fileindex, repo,
8732 branch_ref, base_branch_ref);
8733 goto done;
8736 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8737 if (error) {
8738 if (error->code == GOT_ERR_ANCESTRY)
8739 error = got_error(GOT_ERR_REBASE_REQUIRED);
8740 got_worktree_integrate_abort(worktree, fileindex, repo,
8741 branch_ref, base_branch_ref);
8742 goto done;
8745 memset(&upa, 0, sizeof(upa));
8746 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8747 branch_ref, base_branch_ref, update_progress, &upa,
8748 check_cancelled, NULL);
8749 if (error)
8750 goto done;
8752 printf("Integrated %s into %s\n", refname, base_refname);
8753 print_update_progress_stats(&upa);
8754 done:
8755 if (repo)
8756 got_repo_close(repo);
8757 if (worktree)
8758 got_worktree_close(worktree);
8759 free(cwd);
8760 free(base_commit_id);
8761 free(commit_id);
8762 free(refname);
8763 free(base_refname);
8764 return error;
8767 __dead static void
8768 usage_stage(void)
8770 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8771 "[-S] [file-path ...]\n",
8772 getprogname());
8773 exit(1);
8776 static const struct got_error *
8777 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8778 const char *path, struct got_object_id *blob_id,
8779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8780 int dirfd, const char *de_name)
8782 const struct got_error *err = NULL;
8783 char *id_str = NULL;
8785 if (staged_status != GOT_STATUS_ADD &&
8786 staged_status != GOT_STATUS_MODIFY &&
8787 staged_status != GOT_STATUS_DELETE)
8788 return NULL;
8790 if (staged_status == GOT_STATUS_ADD ||
8791 staged_status == GOT_STATUS_MODIFY)
8792 err = got_object_id_str(&id_str, staged_blob_id);
8793 else
8794 err = got_object_id_str(&id_str, blob_id);
8795 if (err)
8796 return err;
8798 printf("%s %c %s\n", id_str, staged_status, path);
8799 free(id_str);
8800 return NULL;
8803 static const struct got_error *
8804 cmd_stage(int argc, char *argv[])
8806 const struct got_error *error = NULL;
8807 struct got_repository *repo = NULL;
8808 struct got_worktree *worktree = NULL;
8809 char *cwd = NULL;
8810 struct got_pathlist_head paths;
8811 struct got_pathlist_entry *pe;
8812 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8813 FILE *patch_script_file = NULL;
8814 const char *patch_script_path = NULL;
8815 struct choose_patch_arg cpa;
8817 TAILQ_INIT(&paths);
8819 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8820 switch (ch) {
8821 case 'l':
8822 list_stage = 1;
8823 break;
8824 case 'p':
8825 pflag = 1;
8826 break;
8827 case 'F':
8828 patch_script_path = optarg;
8829 break;
8830 case 'S':
8831 allow_bad_symlinks = 1;
8832 break;
8833 default:
8834 usage_stage();
8835 /* NOTREACHED */
8839 argc -= optind;
8840 argv += optind;
8842 #ifndef PROFILE
8843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8844 "unveil", NULL) == -1)
8845 err(1, "pledge");
8846 #endif
8847 if (list_stage && (pflag || patch_script_path))
8848 errx(1, "-l option cannot be used with other options");
8849 if (patch_script_path && !pflag)
8850 errx(1, "-F option can only be used together with -p option");
8852 cwd = getcwd(NULL, 0);
8853 if (cwd == NULL) {
8854 error = got_error_from_errno("getcwd");
8855 goto done;
8858 error = got_worktree_open(&worktree, cwd);
8859 if (error) {
8860 if (error->code == GOT_ERR_NOT_WORKTREE)
8861 error = wrap_not_worktree_error(error, "stage", cwd);
8862 goto done;
8865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8866 NULL);
8867 if (error != NULL)
8868 goto done;
8870 if (patch_script_path) {
8871 patch_script_file = fopen(patch_script_path, "r");
8872 if (patch_script_file == NULL) {
8873 error = got_error_from_errno2("fopen",
8874 patch_script_path);
8875 goto done;
8878 error = apply_unveil(got_repo_get_path(repo), 0,
8879 got_worktree_get_root_path(worktree));
8880 if (error)
8881 goto done;
8883 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8884 if (error)
8885 goto done;
8887 if (list_stage)
8888 error = got_worktree_status(worktree, &paths, repo,
8889 print_stage, NULL, check_cancelled, NULL);
8890 else {
8891 cpa.patch_script_file = patch_script_file;
8892 cpa.action = "stage";
8893 error = got_worktree_stage(worktree, &paths,
8894 pflag ? NULL : print_status, NULL,
8895 pflag ? choose_patch : NULL, &cpa,
8896 allow_bad_symlinks, repo);
8898 done:
8899 if (patch_script_file && fclose(patch_script_file) == EOF &&
8900 error == NULL)
8901 error = got_error_from_errno2("fclose", patch_script_path);
8902 if (repo)
8903 got_repo_close(repo);
8904 if (worktree)
8905 got_worktree_close(worktree);
8906 TAILQ_FOREACH(pe, &paths, entry)
8907 free((char *)pe->path);
8908 got_pathlist_free(&paths);
8909 free(cwd);
8910 return error;
8913 __dead static void
8914 usage_unstage(void)
8916 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8917 "[file-path ...]\n",
8918 getprogname());
8919 exit(1);
8923 static const struct got_error *
8924 cmd_unstage(int argc, char *argv[])
8926 const struct got_error *error = NULL;
8927 struct got_repository *repo = NULL;
8928 struct got_worktree *worktree = NULL;
8929 char *cwd = NULL;
8930 struct got_pathlist_head paths;
8931 struct got_pathlist_entry *pe;
8932 int ch, pflag = 0;
8933 struct got_update_progress_arg upa;
8934 FILE *patch_script_file = NULL;
8935 const char *patch_script_path = NULL;
8936 struct choose_patch_arg cpa;
8938 TAILQ_INIT(&paths);
8940 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8941 switch (ch) {
8942 case 'p':
8943 pflag = 1;
8944 break;
8945 case 'F':
8946 patch_script_path = optarg;
8947 break;
8948 default:
8949 usage_unstage();
8950 /* NOTREACHED */
8954 argc -= optind;
8955 argv += optind;
8957 #ifndef PROFILE
8958 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8959 "unveil", NULL) == -1)
8960 err(1, "pledge");
8961 #endif
8962 if (patch_script_path && !pflag)
8963 errx(1, "-F option can only be used together with -p option");
8965 cwd = getcwd(NULL, 0);
8966 if (cwd == NULL) {
8967 error = got_error_from_errno("getcwd");
8968 goto done;
8971 error = got_worktree_open(&worktree, cwd);
8972 if (error) {
8973 if (error->code == GOT_ERR_NOT_WORKTREE)
8974 error = wrap_not_worktree_error(error, "unstage", cwd);
8975 goto done;
8978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8979 NULL);
8980 if (error != NULL)
8981 goto done;
8983 if (patch_script_path) {
8984 patch_script_file = fopen(patch_script_path, "r");
8985 if (patch_script_file == NULL) {
8986 error = got_error_from_errno2("fopen",
8987 patch_script_path);
8988 goto done;
8992 error = apply_unveil(got_repo_get_path(repo), 0,
8993 got_worktree_get_root_path(worktree));
8994 if (error)
8995 goto done;
8997 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8998 if (error)
8999 goto done;
9001 cpa.patch_script_file = patch_script_file;
9002 cpa.action = "unstage";
9003 memset(&upa, 0, sizeof(upa));
9004 error = got_worktree_unstage(worktree, &paths, update_progress,
9005 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9006 if (!error)
9007 print_update_progress_stats(&upa);
9008 done:
9009 if (patch_script_file && fclose(patch_script_file) == EOF &&
9010 error == NULL)
9011 error = got_error_from_errno2("fclose", patch_script_path);
9012 if (repo)
9013 got_repo_close(repo);
9014 if (worktree)
9015 got_worktree_close(worktree);
9016 TAILQ_FOREACH(pe, &paths, entry)
9017 free((char *)pe->path);
9018 got_pathlist_free(&paths);
9019 free(cwd);
9020 return error;
9023 __dead static void
9024 usage_cat(void)
9026 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9027 "arg1 [arg2 ...]\n", getprogname());
9028 exit(1);
9031 static const struct got_error *
9032 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9034 const struct got_error *err;
9035 struct got_blob_object *blob;
9037 err = got_object_open_as_blob(&blob, repo, id, 8192);
9038 if (err)
9039 return err;
9041 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9042 got_object_blob_close(blob);
9043 return err;
9046 static const struct got_error *
9047 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9049 const struct got_error *err;
9050 struct got_tree_object *tree;
9051 int nentries, i;
9053 err = got_object_open_as_tree(&tree, repo, id);
9054 if (err)
9055 return err;
9057 nentries = got_object_tree_get_nentries(tree);
9058 for (i = 0; i < nentries; i++) {
9059 struct got_tree_entry *te;
9060 char *id_str;
9061 if (sigint_received || sigpipe_received)
9062 break;
9063 te = got_object_tree_get_entry(tree, i);
9064 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9065 if (err)
9066 break;
9067 fprintf(outfile, "%s %.7o %s\n", id_str,
9068 got_tree_entry_get_mode(te),
9069 got_tree_entry_get_name(te));
9070 free(id_str);
9073 got_object_tree_close(tree);
9074 return err;
9077 static const struct got_error *
9078 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9080 const struct got_error *err;
9081 struct got_commit_object *commit;
9082 const struct got_object_id_queue *parent_ids;
9083 struct got_object_qid *pid;
9084 char *id_str = NULL;
9085 const char *logmsg = NULL;
9087 err = got_object_open_as_commit(&commit, repo, id);
9088 if (err)
9089 return err;
9091 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9092 if (err)
9093 goto done;
9095 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9096 parent_ids = got_object_commit_get_parent_ids(commit);
9097 fprintf(outfile, "numparents %d\n",
9098 got_object_commit_get_nparents(commit));
9099 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9100 char *pid_str;
9101 err = got_object_id_str(&pid_str, pid->id);
9102 if (err)
9103 goto done;
9104 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9105 free(pid_str);
9107 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9108 got_object_commit_get_author(commit),
9109 got_object_commit_get_author_time(commit));
9111 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9112 got_object_commit_get_author(commit),
9113 got_object_commit_get_committer_time(commit));
9115 logmsg = got_object_commit_get_logmsg_raw(commit);
9116 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9117 fprintf(outfile, "%s", logmsg);
9118 done:
9119 free(id_str);
9120 got_object_commit_close(commit);
9121 return err;
9124 static const struct got_error *
9125 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9127 const struct got_error *err;
9128 struct got_tag_object *tag;
9129 char *id_str = NULL;
9130 const char *tagmsg = NULL;
9132 err = got_object_open_as_tag(&tag, repo, id);
9133 if (err)
9134 return err;
9136 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9137 if (err)
9138 goto done;
9140 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9142 switch (got_object_tag_get_object_type(tag)) {
9143 case GOT_OBJ_TYPE_BLOB:
9144 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9145 GOT_OBJ_LABEL_BLOB);
9146 break;
9147 case GOT_OBJ_TYPE_TREE:
9148 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9149 GOT_OBJ_LABEL_TREE);
9150 break;
9151 case GOT_OBJ_TYPE_COMMIT:
9152 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9153 GOT_OBJ_LABEL_COMMIT);
9154 break;
9155 case GOT_OBJ_TYPE_TAG:
9156 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9157 GOT_OBJ_LABEL_TAG);
9158 break;
9159 default:
9160 break;
9163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9164 got_object_tag_get_name(tag));
9166 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9167 got_object_tag_get_tagger(tag),
9168 got_object_tag_get_tagger_time(tag));
9170 tagmsg = got_object_tag_get_message(tag);
9171 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9172 fprintf(outfile, "%s", tagmsg);
9173 done:
9174 free(id_str);
9175 got_object_tag_close(tag);
9176 return err;
9179 static const struct got_error *
9180 cmd_cat(int argc, char *argv[])
9182 const struct got_error *error;
9183 struct got_repository *repo = NULL;
9184 struct got_worktree *worktree = NULL;
9185 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9186 const char *commit_id_str = NULL;
9187 struct got_object_id *id = NULL, *commit_id = NULL;
9188 int ch, obj_type, i, force_path = 0;
9190 #ifndef PROFILE
9191 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9192 NULL) == -1)
9193 err(1, "pledge");
9194 #endif
9196 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9197 switch (ch) {
9198 case 'c':
9199 commit_id_str = optarg;
9200 break;
9201 case 'r':
9202 repo_path = realpath(optarg, NULL);
9203 if (repo_path == NULL)
9204 return got_error_from_errno2("realpath",
9205 optarg);
9206 got_path_strip_trailing_slashes(repo_path);
9207 break;
9208 case 'P':
9209 force_path = 1;
9210 break;
9211 default:
9212 usage_cat();
9213 /* NOTREACHED */
9217 argc -= optind;
9218 argv += optind;
9220 cwd = getcwd(NULL, 0);
9221 if (cwd == NULL) {
9222 error = got_error_from_errno("getcwd");
9223 goto done;
9225 error = got_worktree_open(&worktree, cwd);
9226 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9227 goto done;
9228 if (worktree) {
9229 if (repo_path == NULL) {
9230 repo_path = strdup(
9231 got_worktree_get_repo_path(worktree));
9232 if (repo_path == NULL) {
9233 error = got_error_from_errno("strdup");
9234 goto done;
9239 if (repo_path == NULL) {
9240 repo_path = getcwd(NULL, 0);
9241 if (repo_path == NULL)
9242 return got_error_from_errno("getcwd");
9245 error = got_repo_open(&repo, repo_path, NULL);
9246 free(repo_path);
9247 if (error != NULL)
9248 goto done;
9250 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9251 if (error)
9252 goto done;
9254 if (commit_id_str == NULL)
9255 commit_id_str = GOT_REF_HEAD;
9256 error = got_repo_match_object_id(&commit_id, NULL,
9257 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9258 if (error)
9259 goto done;
9261 for (i = 0; i < argc; i++) {
9262 if (force_path) {
9263 error = got_object_id_by_path(&id, repo, commit_id,
9264 argv[i]);
9265 if (error)
9266 break;
9267 } else {
9268 error = got_repo_match_object_id(&id, &label, argv[i],
9269 GOT_OBJ_TYPE_ANY, 0, repo);
9270 if (error) {
9271 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9272 error->code != GOT_ERR_NOT_REF)
9273 break;
9274 error = got_object_id_by_path(&id, repo,
9275 commit_id, argv[i]);
9276 if (error)
9277 break;
9281 error = got_object_get_type(&obj_type, repo, id);
9282 if (error)
9283 break;
9285 switch (obj_type) {
9286 case GOT_OBJ_TYPE_BLOB:
9287 error = cat_blob(id, repo, stdout);
9288 break;
9289 case GOT_OBJ_TYPE_TREE:
9290 error = cat_tree(id, repo, stdout);
9291 break;
9292 case GOT_OBJ_TYPE_COMMIT:
9293 error = cat_commit(id, repo, stdout);
9294 break;
9295 case GOT_OBJ_TYPE_TAG:
9296 error = cat_tag(id, repo, stdout);
9297 break;
9298 default:
9299 error = got_error(GOT_ERR_OBJ_TYPE);
9300 break;
9302 if (error)
9303 break;
9304 free(label);
9305 label = NULL;
9306 free(id);
9307 id = NULL;
9309 done:
9310 free(label);
9311 free(id);
9312 free(commit_id);
9313 if (worktree)
9314 got_worktree_close(worktree);
9315 if (repo) {
9316 const struct got_error *repo_error;
9317 repo_error = got_repo_close(repo);
9318 if (error == NULL)
9319 error = repo_error;
9321 return error;
9324 __dead static void
9325 usage_info(void)
9327 fprintf(stderr, "usage: %s info [path ...]\n",
9328 getprogname());
9329 exit(1);
9332 static const struct got_error *
9333 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9334 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9335 struct got_object_id *commit_id)
9337 const struct got_error *err = NULL;
9338 char *id_str = NULL;
9339 char datebuf[128];
9340 struct tm mytm, *tm;
9341 struct got_pathlist_head *paths = arg;
9342 struct got_pathlist_entry *pe;
9345 * Clear error indication from any of the path arguments which
9346 * would cause this file index entry to be displayed.
9348 TAILQ_FOREACH(pe, paths, entry) {
9349 if (got_path_cmp(path, pe->path, strlen(path),
9350 pe->path_len) == 0 ||
9351 got_path_is_child(path, pe->path, pe->path_len))
9352 pe->data = NULL; /* no error */
9355 printf(GOT_COMMIT_SEP_STR);
9356 if (S_ISLNK(mode))
9357 printf("symlink: %s\n", path);
9358 else if (S_ISREG(mode)) {
9359 printf("file: %s\n", path);
9360 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9361 } else if (S_ISDIR(mode))
9362 printf("directory: %s\n", path);
9363 else
9364 printf("something: %s\n", path);
9366 tm = localtime_r(&mtime, &mytm);
9367 if (tm == NULL)
9368 return NULL;
9369 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9370 return got_error(GOT_ERR_NO_SPACE);
9371 printf("timestamp: %s\n", datebuf);
9373 if (blob_id) {
9374 err = got_object_id_str(&id_str, blob_id);
9375 if (err)
9376 return err;
9377 printf("based on blob: %s\n", id_str);
9378 free(id_str);
9381 if (staged_blob_id) {
9382 err = got_object_id_str(&id_str, staged_blob_id);
9383 if (err)
9384 return err;
9385 printf("based on staged blob: %s\n", id_str);
9386 free(id_str);
9389 if (commit_id) {
9390 err = got_object_id_str(&id_str, commit_id);
9391 if (err)
9392 return err;
9393 printf("based on commit: %s\n", id_str);
9394 free(id_str);
9397 return NULL;
9400 static const struct got_error *
9401 cmd_info(int argc, char *argv[])
9403 const struct got_error *error = NULL;
9404 struct got_worktree *worktree = NULL;
9405 char *cwd = NULL, *id_str = NULL;
9406 struct got_pathlist_head paths;
9407 struct got_pathlist_entry *pe;
9408 char *uuidstr = NULL;
9409 int ch, show_files = 0;
9411 TAILQ_INIT(&paths);
9413 while ((ch = getopt(argc, argv, "")) != -1) {
9414 switch (ch) {
9415 default:
9416 usage_info();
9417 /* NOTREACHED */
9421 argc -= optind;
9422 argv += optind;
9424 #ifndef PROFILE
9425 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9426 NULL) == -1)
9427 err(1, "pledge");
9428 #endif
9429 cwd = getcwd(NULL, 0);
9430 if (cwd == NULL) {
9431 error = got_error_from_errno("getcwd");
9432 goto done;
9435 error = got_worktree_open(&worktree, cwd);
9436 if (error) {
9437 if (error->code == GOT_ERR_NOT_WORKTREE)
9438 error = wrap_not_worktree_error(error, "status", cwd);
9439 goto done;
9442 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9443 if (error)
9444 goto done;
9446 if (argc >= 1) {
9447 error = get_worktree_paths_from_argv(&paths, argc, argv,
9448 worktree);
9449 if (error)
9450 goto done;
9451 show_files = 1;
9454 error = got_object_id_str(&id_str,
9455 got_worktree_get_base_commit_id(worktree));
9456 if (error)
9457 goto done;
9459 error = got_worktree_get_uuid(&uuidstr, worktree);
9460 if (error)
9461 goto done;
9463 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9464 printf("work tree base commit: %s\n", id_str);
9465 printf("work tree path prefix: %s\n",
9466 got_worktree_get_path_prefix(worktree));
9467 printf("work tree branch reference: %s\n",
9468 got_worktree_get_head_ref_name(worktree));
9469 printf("work tree UUID: %s\n", uuidstr);
9470 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9472 if (show_files) {
9473 struct got_pathlist_entry *pe;
9474 TAILQ_FOREACH(pe, &paths, entry) {
9475 if (pe->path_len == 0)
9476 continue;
9478 * Assume this path will fail. This will be corrected
9479 * in print_path_info() in case the path does suceeed.
9481 pe->data = (void *)got_error_path(pe->path,
9482 GOT_ERR_BAD_PATH);
9484 error = got_worktree_path_info(worktree, &paths,
9485 print_path_info, &paths, check_cancelled, NULL);
9486 if (error)
9487 goto done;
9488 TAILQ_FOREACH(pe, &paths, entry) {
9489 if (pe->data != NULL) {
9490 error = pe->data; /* bad path */
9491 break;
9495 done:
9496 TAILQ_FOREACH(pe, &paths, entry)
9497 free((char *)pe->path);
9498 got_pathlist_free(&paths);
9499 free(cwd);
9500 free(id_str);
9501 free(uuidstr);
9502 return error;