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, uri);
1119 if (error)
1120 goto done;
1122 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1123 host, port ? ":" : "", port ? port : "",
1124 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1125 error = got_error_from_errno("asprintf");
1126 goto done;
1129 if (strcmp(proto, "git") == 0) {
1130 #ifndef PROFILE
1131 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1132 "sendfd dns inet unveil", NULL) == -1)
1133 err(1, "pledge");
1134 #endif
1135 } else if (strcmp(proto, "git+ssh") == 0 ||
1136 strcmp(proto, "ssh") == 0) {
1137 #ifndef PROFILE
1138 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1139 "sendfd unveil", NULL) == -1)
1140 err(1, "pledge");
1141 #endif
1142 } else if (strcmp(proto, "http") == 0 ||
1143 strcmp(proto, "git+http") == 0) {
1144 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1145 goto done;
1146 } else {
1147 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1148 goto done;
1150 if (dirname == NULL) {
1151 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1152 error = got_error_from_errno("asprintf");
1153 goto done;
1155 repo_path = default_destdir;
1156 } else
1157 repo_path = dirname;
1159 if (!list_refs_only) {
1160 error = got_path_mkdir(repo_path);
1161 if (error)
1162 goto done;
1164 error = got_repo_init(repo_path);
1165 if (error)
1166 goto done;
1167 error = got_repo_open(&repo, repo_path, NULL);
1168 if (error)
1169 goto done;
1172 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1173 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1174 error = got_error_from_errno2("unveil",
1175 GOT_FETCH_PATH_SSH);
1176 goto done;
1179 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1180 if (error)
1181 goto done;
1183 if (verbosity >= 0)
1184 printf("Connecting to %s%s%s\n", host,
1185 port ? ":" : "", port ? port : "");
1187 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1188 server_path, verbosity);
1189 if (error)
1190 goto done;
1192 fpa.last_scaled_size[0] = '\0';
1193 fpa.last_p_indexed = -1;
1194 fpa.last_p_resolved = -1;
1195 fpa.verbosity = verbosity;
1196 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1197 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1198 fetch_all_branches, &wanted_branches, &wanted_refs,
1199 list_refs_only, verbosity, fetchfd, repo,
1200 fetch_progress, &fpa);
1201 if (error)
1202 goto done;
1204 if (list_refs_only) {
1205 error = list_remote_refs(&symrefs, &refs);
1206 goto done;
1209 error = got_object_id_str(&id_str, pack_hash);
1210 if (error)
1211 goto done;
1212 if (verbosity >= 0)
1213 printf("\nFetched %s.pack\n", id_str);
1214 free(id_str);
1216 /* Set up references provided with the pack file. */
1217 TAILQ_FOREACH(pe, &refs, entry) {
1218 const char *refname = pe->path;
1219 struct got_object_id *id = pe->data;
1220 char *remote_refname;
1222 if (is_wanted_ref(&wanted_refs, refname) &&
1223 !mirror_references) {
1224 error = create_wanted_ref(refname, id,
1225 GOT_FETCH_DEFAULT_REMOTE_NAME,
1226 verbosity - 1, repo);
1227 if (error)
1228 goto done;
1229 continue;
1232 error = create_ref(refname, id, verbosity - 1, repo);
1233 if (error)
1234 goto done;
1236 if (mirror_references)
1237 continue;
1239 if (strncmp("refs/heads/", refname, 11) != 0)
1240 continue;
1242 if (asprintf(&remote_refname,
1243 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1244 refname + 11) == -1) {
1245 error = got_error_from_errno("asprintf");
1246 goto done;
1248 error = create_ref(remote_refname, id, verbosity - 1, repo);
1249 free(remote_refname);
1250 if (error)
1251 goto done;
1254 /* Set the HEAD reference if the server provided one. */
1255 TAILQ_FOREACH(pe, &symrefs, entry) {
1256 struct got_reference *target_ref;
1257 const char *refname = pe->path;
1258 const char *target = pe->data;
1259 char *remote_refname = NULL, *remote_target = NULL;
1261 if (strcmp(refname, GOT_REF_HEAD) != 0)
1262 continue;
1264 error = got_ref_open(&target_ref, repo, target, 0);
1265 if (error) {
1266 if (error->code == GOT_ERR_NOT_REF) {
1267 error = NULL;
1268 continue;
1270 goto done;
1273 error = create_symref(refname, target_ref, verbosity, repo);
1274 got_ref_close(target_ref);
1275 if (error)
1276 goto done;
1278 if (mirror_references)
1279 continue;
1281 if (strncmp("refs/heads/", target, 11) != 0)
1282 continue;
1284 if (asprintf(&remote_refname,
1285 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1286 refname) == -1) {
1287 error = got_error_from_errno("asprintf");
1288 goto done;
1290 if (asprintf(&remote_target,
1291 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1292 target + 11) == -1) {
1293 error = got_error_from_errno("asprintf");
1294 free(remote_refname);
1295 goto done;
1297 error = got_ref_open(&target_ref, repo, remote_target, 0);
1298 if (error) {
1299 free(remote_refname);
1300 free(remote_target);
1301 if (error->code == GOT_ERR_NOT_REF) {
1302 error = NULL;
1303 continue;
1305 goto done;
1307 error = create_symref(remote_refname, target_ref,
1308 verbosity - 1, repo);
1309 free(remote_refname);
1310 free(remote_target);
1311 got_ref_close(target_ref);
1312 if (error)
1313 goto done;
1315 if (pe == NULL) {
1317 * We failed to set the HEAD reference. If we asked for
1318 * a set of wanted branches use the first of one of those
1319 * which could be fetched instead.
1321 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1322 const char *target = pe->path;
1323 struct got_reference *target_ref;
1325 error = got_ref_open(&target_ref, repo, target, 0);
1326 if (error) {
1327 if (error->code == GOT_ERR_NOT_REF) {
1328 error = NULL;
1329 continue;
1331 goto done;
1334 error = create_symref(GOT_REF_HEAD, target_ref,
1335 verbosity, repo);
1336 got_ref_close(target_ref);
1337 if (error)
1338 goto done;
1339 break;
1343 /* Create a config file git-fetch(1) can understand. */
1344 gitconfig_path = got_repo_get_path_gitconfig(repo);
1345 if (gitconfig_path == NULL) {
1346 error = got_error_from_errno("got_repo_get_path_gitconfig");
1347 goto done;
1349 gitconfig_file = fopen(gitconfig_path, "a");
1350 if (gitconfig_file == NULL) {
1351 error = got_error_from_errno2("fopen", gitconfig_path);
1352 goto done;
1354 if (mirror_references) {
1355 if (asprintf(&gitconfig,
1356 "[remote \"%s\"]\n"
1357 "\turl = %s\n"
1358 "\tfetch = +refs/*:refs/*\n"
1359 "\tmirror = true\n",
1360 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1361 error = got_error_from_errno("asprintf");
1362 goto done;
1364 } else if (fetch_all_branches) {
1365 if (asprintf(&gitconfig,
1366 "[remote \"%s\"]\n"
1367 "\turl = %s\n"
1368 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1369 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1370 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1371 error = got_error_from_errno("asprintf");
1372 goto done;
1374 } else {
1375 const char *branchname;
1378 * If the server specified a default branch, use just that one.
1379 * Otherwise fall back to fetching all branches on next fetch.
1381 if (head_symref) {
1382 branchname = got_ref_get_symref_target(head_symref);
1383 if (strncmp(branchname, "refs/heads/", 11) == 0)
1384 branchname += 11;
1385 } else
1386 branchname = "*"; /* fall back to all branches */
1387 if (asprintf(&gitconfig,
1388 "[remote \"%s\"]\n"
1389 "\turl = %s\n"
1390 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1391 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1392 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1393 branchname) == -1) {
1394 error = got_error_from_errno("asprintf");
1395 goto done;
1398 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1399 if (n != strlen(gitconfig)) {
1400 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1401 goto done;
1404 if (verbosity >= 0)
1405 printf("Created %s repository '%s'\n",
1406 mirror_references ? "mirrored" : "cloned", repo_path);
1407 done:
1408 if (fetchpid > 0) {
1409 if (kill(fetchpid, SIGTERM) == -1)
1410 error = got_error_from_errno("kill");
1411 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1412 error = got_error_from_errno("waitpid");
1414 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1415 error = got_error_from_errno("close");
1416 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1417 error = got_error_from_errno("fclose");
1418 if (repo)
1419 got_repo_close(repo);
1420 if (head_symref)
1421 got_ref_close(head_symref);
1422 TAILQ_FOREACH(pe, &refs, entry) {
1423 free((void *)pe->path);
1424 free(pe->data);
1426 got_pathlist_free(&refs);
1427 TAILQ_FOREACH(pe, &symrefs, entry) {
1428 free((void *)pe->path);
1429 free(pe->data);
1431 got_pathlist_free(&symrefs);
1432 got_pathlist_free(&wanted_branches);
1433 got_pathlist_free(&wanted_refs);
1434 free(pack_hash);
1435 free(proto);
1436 free(host);
1437 free(port);
1438 free(server_path);
1439 free(repo_name);
1440 free(default_destdir);
1441 free(gitconfig_path);
1442 free(git_url);
1443 return error;
1446 static const struct got_error *
1447 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1448 int replace_tags, int verbosity, struct got_repository *repo)
1450 const struct got_error *err = NULL;
1451 char *new_id_str = NULL;
1452 struct got_object_id *old_id = NULL;
1454 err = got_object_id_str(&new_id_str, new_id);
1455 if (err)
1456 goto done;
1458 if (!replace_tags &&
1459 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1460 err = got_ref_resolve(&old_id, repo, ref);
1461 if (err)
1462 goto done;
1463 if (got_object_id_cmp(old_id, new_id) == 0)
1464 goto done;
1465 if (verbosity >= 0) {
1466 printf("Rejecting update of existing tag %s: %s\n",
1467 got_ref_get_name(ref), new_id_str);
1469 goto done;
1472 if (got_ref_is_symbolic(ref)) {
1473 if (verbosity >= 0) {
1474 printf("Replacing reference %s: %s\n",
1475 got_ref_get_name(ref),
1476 got_ref_get_symref_target(ref));
1478 err = got_ref_change_symref_to_ref(ref, new_id);
1479 if (err)
1480 goto done;
1481 err = got_ref_write(ref, repo);
1482 if (err)
1483 goto done;
1484 } else {
1485 err = got_ref_resolve(&old_id, repo, ref);
1486 if (err)
1487 goto done;
1488 if (got_object_id_cmp(old_id, new_id) == 0)
1489 goto done;
1491 err = got_ref_change_ref(ref, new_id);
1492 if (err)
1493 goto done;
1494 err = got_ref_write(ref, repo);
1495 if (err)
1496 goto done;
1499 if (verbosity >= 0)
1500 printf("Updated %s: %s\n", got_ref_get_name(ref),
1501 new_id_str);
1502 done:
1503 free(old_id);
1504 free(new_id_str);
1505 return err;
1508 static const struct got_error *
1509 update_symref(const char *refname, struct got_reference *target_ref,
1510 int verbosity, struct got_repository *repo)
1512 const struct got_error *err = NULL, *unlock_err;
1513 struct got_reference *symref;
1514 int symref_is_locked = 0;
1516 err = got_ref_open(&symref, repo, refname, 1);
1517 if (err) {
1518 if (err->code != GOT_ERR_NOT_REF)
1519 return err;
1520 err = got_ref_alloc_symref(&symref, refname, target_ref);
1521 if (err)
1522 goto done;
1524 err = got_ref_write(symref, repo);
1525 if (err)
1526 goto done;
1528 if (verbosity >= 0)
1529 printf("Created reference %s: %s\n",
1530 got_ref_get_name(symref),
1531 got_ref_get_symref_target(symref));
1532 } else {
1533 symref_is_locked = 1;
1535 if (strcmp(got_ref_get_symref_target(symref),
1536 got_ref_get_name(target_ref)) == 0)
1537 goto done;
1539 err = got_ref_change_symref(symref,
1540 got_ref_get_name(target_ref));
1541 if (err)
1542 goto done;
1544 err = got_ref_write(symref, repo);
1545 if (err)
1546 goto done;
1548 if (verbosity >= 0)
1549 printf("Updated %s: %s\n", got_ref_get_name(symref),
1550 got_ref_get_symref_target(symref));
1553 done:
1554 if (symref_is_locked) {
1555 unlock_err = got_ref_unlock(symref);
1556 if (unlock_err && err == NULL)
1557 err = unlock_err;
1559 got_ref_close(symref);
1560 return err;
1563 __dead static void
1564 usage_fetch(void)
1566 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1567 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1568 "[remote-repository-name]\n",
1569 getprogname());
1570 exit(1);
1573 static const struct got_error *
1574 delete_missing_ref(struct got_reference *ref,
1575 int verbosity, struct got_repository *repo)
1577 const struct got_error *err = NULL;
1578 struct got_object_id *id = NULL;
1579 char *id_str = NULL;
1581 if (got_ref_is_symbolic(ref)) {
1582 err = got_ref_delete(ref, repo);
1583 if (err)
1584 return err;
1585 if (verbosity >= 0) {
1586 printf("Deleted reference %s: %s\n",
1587 got_ref_get_name(ref),
1588 got_ref_get_symref_target(ref));
1590 } else {
1591 err = got_ref_resolve(&id, repo, ref);
1592 if (err)
1593 return err;
1594 err = got_object_id_str(&id_str, id);
1595 if (err)
1596 goto done;
1598 err = got_ref_delete(ref, repo);
1599 if (err)
1600 goto done;
1601 if (verbosity >= 0) {
1602 printf("Deleted reference %s: %s\n",
1603 got_ref_get_name(ref), id_str);
1606 done:
1607 free(id);
1608 free(id_str);
1609 return NULL;
1612 static const struct got_error *
1613 delete_missing_refs(struct got_pathlist_head *their_refs,
1614 struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1615 int verbosity, struct got_repository *repo)
1617 const struct got_error *err = NULL, *unlock_err;
1618 struct got_reflist_head my_refs;
1619 struct got_reflist_entry *re;
1620 struct got_pathlist_entry *pe;
1621 char *remote_namespace = NULL;
1622 char *local_refname = NULL;
1624 SIMPLEQ_INIT(&my_refs);
1626 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1627 == -1)
1628 return got_error_from_errno("asprintf");
1630 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1631 if (err)
1632 goto done;
1634 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1635 const char *refname = got_ref_get_name(re->ref);
1637 if (!remote->mirror_references) {
1638 if (strncmp(refname, remote_namespace,
1639 strlen(remote_namespace)) == 0) {
1640 if (strcmp(refname + strlen(remote_namespace),
1641 GOT_REF_HEAD) == 0)
1642 continue;
1643 if (asprintf(&local_refname, "refs/heads/%s",
1644 refname + strlen(remote_namespace)) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 goto done;
1648 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1649 continue;
1652 TAILQ_FOREACH(pe, their_refs, entry) {
1653 if (strcmp(local_refname, pe->path) == 0)
1654 break;
1656 if (pe != NULL)
1657 continue;
1659 TAILQ_FOREACH(pe, their_symrefs, entry) {
1660 if (strcmp(local_refname, pe->path) == 0)
1661 break;
1663 if (pe != NULL)
1664 continue;
1666 err = delete_missing_ref(re->ref, verbosity, repo);
1667 if (err)
1668 break;
1670 if (local_refname) {
1671 struct got_reference *ref;
1672 err = got_ref_open(&ref, repo, local_refname, 1);
1673 if (err) {
1674 if (err->code != GOT_ERR_NOT_REF)
1675 break;
1676 free(local_refname);
1677 local_refname = NULL;
1678 continue;
1680 err = delete_missing_ref(ref, verbosity, repo);
1681 if (err)
1682 break;
1683 unlock_err = got_ref_unlock(ref);
1684 got_ref_close(ref);
1685 if (unlock_err && err == NULL) {
1686 err = unlock_err;
1687 break;
1690 free(local_refname);
1691 local_refname = NULL;
1694 done:
1695 free(remote_namespace);
1696 free(local_refname);
1697 return err;
1700 static const struct got_error *
1701 update_wanted_ref(const char *refname, struct got_object_id *id,
1702 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1704 const struct got_error *err, *unlock_err;
1705 char *remote_refname;
1706 struct got_reference *ref;
1708 if (strncmp("refs/", refname, 5) == 0)
1709 refname += 5;
1711 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1712 remote_repo_name, refname) == -1)
1713 return got_error_from_errno("asprintf");
1715 err = got_ref_open(&ref, repo, remote_refname, 1);
1716 if (err) {
1717 if (err->code != GOT_ERR_NOT_REF)
1718 goto done;
1719 err = create_ref(remote_refname, id, verbosity, repo);
1720 } else {
1721 err = update_ref(ref, id, 0, verbosity, repo);
1722 unlock_err = got_ref_unlock(ref);
1723 if (unlock_err && err == NULL)
1724 err = unlock_err;
1725 got_ref_close(ref);
1727 done:
1728 free(remote_refname);
1729 return err;
1732 static const struct got_error *
1733 cmd_fetch(int argc, char *argv[])
1735 const struct got_error *error = NULL, *unlock_err;
1736 char *cwd = NULL, *repo_path = NULL;
1737 const char *remote_name;
1738 char *proto = NULL, *host = NULL, *port = NULL;
1739 char *repo_name = NULL, *server_path = NULL;
1740 struct got_remote_repo *remotes, *remote = NULL;
1741 int nremotes;
1742 char *id_str = NULL;
1743 struct got_repository *repo = NULL;
1744 struct got_worktree *worktree = NULL;
1745 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1746 struct got_pathlist_entry *pe;
1747 struct got_object_id *pack_hash = NULL;
1748 int i, ch, fetchfd = -1, fetchstatus;
1749 pid_t fetchpid = -1;
1750 struct got_fetch_progress_arg fpa;
1751 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1752 int delete_refs = 0, replace_tags = 0;
1754 TAILQ_INIT(&refs);
1755 TAILQ_INIT(&symrefs);
1756 TAILQ_INIT(&wanted_branches);
1757 TAILQ_INIT(&wanted_refs);
1759 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1760 switch (ch) {
1761 case 'a':
1762 fetch_all_branches = 1;
1763 break;
1764 case 'b':
1765 error = got_pathlist_append(&wanted_branches,
1766 optarg, NULL);
1767 if (error)
1768 return error;
1769 break;
1770 case 'd':
1771 delete_refs = 1;
1772 break;
1773 case 'l':
1774 list_refs_only = 1;
1775 break;
1776 case 'r':
1777 repo_path = realpath(optarg, NULL);
1778 if (repo_path == NULL)
1779 return got_error_from_errno2("realpath",
1780 optarg);
1781 got_path_strip_trailing_slashes(repo_path);
1782 break;
1783 case 't':
1784 replace_tags = 1;
1785 break;
1786 case 'v':
1787 if (verbosity < 0)
1788 verbosity = 0;
1789 else if (verbosity < 3)
1790 verbosity++;
1791 break;
1792 case 'q':
1793 verbosity = -1;
1794 break;
1795 case 'R':
1796 error = got_pathlist_append(&wanted_refs,
1797 optarg, NULL);
1798 if (error)
1799 return error;
1800 break;
1801 default:
1802 usage_fetch();
1803 break;
1806 argc -= optind;
1807 argv += optind;
1809 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1810 errx(1, "-a and -b options are mutually exclusive");
1811 if (list_refs_only) {
1812 if (!TAILQ_EMPTY(&wanted_branches))
1813 errx(1, "-l and -b options are mutually exclusive");
1814 if (fetch_all_branches)
1815 errx(1, "-l and -a options are mutually exclusive");
1816 if (delete_refs)
1817 errx(1, "-l and -d options are mutually exclusive");
1818 if (verbosity == -1)
1819 errx(1, "-l and -q options are mutually exclusive");
1822 if (argc == 0)
1823 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1824 else if (argc == 1)
1825 remote_name = argv[0];
1826 else
1827 usage_fetch();
1829 cwd = getcwd(NULL, 0);
1830 if (cwd == NULL) {
1831 error = got_error_from_errno("getcwd");
1832 goto done;
1835 if (repo_path == NULL) {
1836 error = got_worktree_open(&worktree, cwd);
1837 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1838 goto done;
1839 else
1840 error = NULL;
1841 if (worktree) {
1842 repo_path =
1843 strdup(got_worktree_get_repo_path(worktree));
1844 if (repo_path == NULL)
1845 error = got_error_from_errno("strdup");
1846 if (error)
1847 goto done;
1848 } else {
1849 repo_path = strdup(cwd);
1850 if (repo_path == NULL) {
1851 error = got_error_from_errno("strdup");
1852 goto done;
1857 error = got_repo_open(&repo, repo_path, NULL);
1858 if (error)
1859 goto done;
1861 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1862 for (i = 0; i < nremotes; i++) {
1863 remote = &remotes[i];
1864 if (strcmp(remote->name, remote_name) == 0)
1865 break;
1867 if (i == nremotes) {
1868 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1869 goto done;
1872 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1873 &repo_name, remote->url);
1874 if (error)
1875 goto done;
1877 if (strcmp(proto, "git") == 0) {
1878 #ifndef PROFILE
1879 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1880 "sendfd dns inet unveil", NULL) == -1)
1881 err(1, "pledge");
1882 #endif
1883 } else if (strcmp(proto, "git+ssh") == 0 ||
1884 strcmp(proto, "ssh") == 0) {
1885 #ifndef PROFILE
1886 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1887 "sendfd unveil", NULL) == -1)
1888 err(1, "pledge");
1889 #endif
1890 } else if (strcmp(proto, "http") == 0 ||
1891 strcmp(proto, "git+http") == 0) {
1892 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1893 goto done;
1894 } else {
1895 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1896 goto done;
1899 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1900 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1901 error = got_error_from_errno2("unveil",
1902 GOT_FETCH_PATH_SSH);
1903 goto done;
1906 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1907 if (error)
1908 goto done;
1910 if (verbosity >= 0)
1911 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1912 port ? ":" : "", port ? port : "");
1914 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1915 server_path, verbosity);
1916 if (error)
1917 goto done;
1919 fpa.last_scaled_size[0] = '\0';
1920 fpa.last_p_indexed = -1;
1921 fpa.last_p_resolved = -1;
1922 fpa.verbosity = verbosity;
1923 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1924 remote->mirror_references, fetch_all_branches, &wanted_branches,
1925 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1926 fetch_progress, &fpa);
1927 if (error)
1928 goto done;
1930 if (list_refs_only) {
1931 error = list_remote_refs(&symrefs, &refs);
1932 goto done;
1935 if (pack_hash == NULL) {
1936 if (verbosity >= 0)
1937 printf("Already up-to-date\n");
1938 } else if (verbosity >= 0) {
1939 error = got_object_id_str(&id_str, pack_hash);
1940 if (error)
1941 goto done;
1942 printf("\nFetched %s.pack\n", id_str);
1943 free(id_str);
1944 id_str = NULL;
1947 /* Update references provided with the pack file. */
1948 TAILQ_FOREACH(pe, &refs, entry) {
1949 const char *refname = pe->path;
1950 struct got_object_id *id = pe->data;
1951 struct got_reference *ref;
1952 char *remote_refname;
1954 if (is_wanted_ref(&wanted_refs, refname) &&
1955 !remote->mirror_references) {
1956 error = update_wanted_ref(refname, id,
1957 remote->name, verbosity, repo);
1958 if (error)
1959 goto done;
1960 continue;
1963 if (remote->mirror_references ||
1964 strncmp("refs/tags/", refname, 10) == 0) {
1965 error = got_ref_open(&ref, repo, refname, 1);
1966 if (error) {
1967 if (error->code != GOT_ERR_NOT_REF)
1968 goto done;
1969 error = create_ref(refname, id, verbosity,
1970 repo);
1971 if (error)
1972 goto done;
1973 } else {
1974 error = update_ref(ref, id, replace_tags,
1975 verbosity, repo);
1976 unlock_err = got_ref_unlock(ref);
1977 if (unlock_err && error == NULL)
1978 error = unlock_err;
1979 got_ref_close(ref);
1980 if (error)
1981 goto done;
1983 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1984 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1985 remote_name, refname + 11) == -1) {
1986 error = got_error_from_errno("asprintf");
1987 goto done;
1990 error = got_ref_open(&ref, repo, remote_refname, 1);
1991 if (error) {
1992 if (error->code != GOT_ERR_NOT_REF)
1993 goto done;
1994 error = create_ref(remote_refname, id,
1995 verbosity, repo);
1996 if (error)
1997 goto done;
1998 } else {
1999 error = update_ref(ref, id, replace_tags,
2000 verbosity, repo);
2001 unlock_err = got_ref_unlock(ref);
2002 if (unlock_err && error == NULL)
2003 error = unlock_err;
2004 got_ref_close(ref);
2005 if (error)
2006 goto done;
2009 /* Also create a local branch if none exists yet. */
2010 error = got_ref_open(&ref, repo, refname, 1);
2011 if (error) {
2012 if (error->code != GOT_ERR_NOT_REF)
2013 goto done;
2014 error = create_ref(refname, id, verbosity,
2015 repo);
2016 if (error)
2017 goto done;
2018 } else {
2019 unlock_err = got_ref_unlock(ref);
2020 if (unlock_err && error == NULL)
2021 error = unlock_err;
2022 got_ref_close(ref);
2026 if (delete_refs) {
2027 error = delete_missing_refs(&refs, &symrefs, remote,
2028 verbosity, repo);
2029 if (error)
2030 goto done;
2033 if (!remote->mirror_references) {
2034 /* Update remote HEAD reference if the server provided one. */
2035 TAILQ_FOREACH(pe, &symrefs, entry) {
2036 struct got_reference *target_ref;
2037 const char *refname = pe->path;
2038 const char *target = pe->data;
2039 char *remote_refname = NULL, *remote_target = NULL;
2041 if (strcmp(refname, GOT_REF_HEAD) != 0)
2042 continue;
2044 if (strncmp("refs/heads/", target, 11) != 0)
2045 continue;
2047 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2048 remote->name, refname) == -1) {
2049 error = got_error_from_errno("asprintf");
2050 goto done;
2052 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2053 remote->name, target + 11) == -1) {
2054 error = got_error_from_errno("asprintf");
2055 free(remote_refname);
2056 goto done;
2059 error = got_ref_open(&target_ref, repo, remote_target,
2060 0);
2061 if (error) {
2062 free(remote_refname);
2063 free(remote_target);
2064 if (error->code == GOT_ERR_NOT_REF) {
2065 error = NULL;
2066 continue;
2068 goto done;
2070 error = update_symref(remote_refname, target_ref,
2071 verbosity, repo);
2072 free(remote_refname);
2073 free(remote_target);
2074 got_ref_close(target_ref);
2075 if (error)
2076 goto done;
2079 done:
2080 if (fetchpid > 0) {
2081 if (kill(fetchpid, SIGTERM) == -1)
2082 error = got_error_from_errno("kill");
2083 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2084 error = got_error_from_errno("waitpid");
2086 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2087 error = got_error_from_errno("close");
2088 if (repo)
2089 got_repo_close(repo);
2090 if (worktree)
2091 got_worktree_close(worktree);
2092 TAILQ_FOREACH(pe, &refs, entry) {
2093 free((void *)pe->path);
2094 free(pe->data);
2096 got_pathlist_free(&refs);
2097 TAILQ_FOREACH(pe, &symrefs, entry) {
2098 free((void *)pe->path);
2099 free(pe->data);
2101 got_pathlist_free(&symrefs);
2102 got_pathlist_free(&wanted_branches);
2103 got_pathlist_free(&wanted_refs);
2104 free(id_str);
2105 free(cwd);
2106 free(repo_path);
2107 free(pack_hash);
2108 free(proto);
2109 free(host);
2110 free(port);
2111 free(server_path);
2112 free(repo_name);
2113 return error;
2117 __dead static void
2118 usage_checkout(void)
2120 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2121 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2122 exit(1);
2125 static void
2126 show_worktree_base_ref_warning(void)
2128 fprintf(stderr, "%s: warning: could not create a reference "
2129 "to the work tree's base commit; the commit could be "
2130 "garbage-collected by Git; making the repository "
2131 "writable and running 'got update' will prevent this\n",
2132 getprogname());
2135 struct got_checkout_progress_arg {
2136 const char *worktree_path;
2137 int had_base_commit_ref_error;
2140 static const struct got_error *
2141 checkout_progress(void *arg, unsigned char status, const char *path)
2143 struct got_checkout_progress_arg *a = arg;
2145 /* Base commit bump happens silently. */
2146 if (status == GOT_STATUS_BUMP_BASE)
2147 return NULL;
2149 if (status == GOT_STATUS_BASE_REF_ERR) {
2150 a->had_base_commit_ref_error = 1;
2151 return NULL;
2154 while (path[0] == '/')
2155 path++;
2157 printf("%c %s/%s\n", status, a->worktree_path, path);
2158 return NULL;
2161 static const struct got_error *
2162 check_cancelled(void *arg)
2164 if (sigint_received || sigpipe_received)
2165 return got_error(GOT_ERR_CANCELLED);
2166 return NULL;
2169 static const struct got_error *
2170 check_linear_ancestry(struct got_object_id *commit_id,
2171 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 struct got_object_id *yca_id;
2177 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2178 commit_id, base_commit_id, repo, check_cancelled, NULL);
2179 if (err)
2180 return err;
2182 if (yca_id == NULL)
2183 return got_error(GOT_ERR_ANCESTRY);
2186 * Require a straight line of history between the target commit
2187 * and the work tree's base commit.
2189 * Non-linear situations such as this require a rebase:
2191 * (commit) D F (base_commit)
2192 * \ /
2193 * C E
2194 * \ /
2195 * B (yca)
2196 * |
2197 * A
2199 * 'got update' only handles linear cases:
2200 * Update forwards in time: A (base/yca) - B - C - D (commit)
2201 * Update backwards in time: D (base) - C - B - A (commit/yca)
2203 if (allow_forwards_in_time_only) {
2204 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2205 return got_error(GOT_ERR_ANCESTRY);
2206 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2207 got_object_id_cmp(base_commit_id, yca_id) != 0)
2208 return got_error(GOT_ERR_ANCESTRY);
2210 free(yca_id);
2211 return NULL;
2214 static const struct got_error *
2215 check_same_branch(struct got_object_id *commit_id,
2216 struct got_reference *head_ref, struct got_object_id *yca_id,
2217 struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_commit_graph *graph = NULL;
2221 struct got_object_id *head_commit_id = NULL;
2222 int is_same_branch = 0;
2224 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2225 if (err)
2226 goto done;
2228 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2229 is_same_branch = 1;
2230 goto done;
2232 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2233 is_same_branch = 1;
2234 goto done;
2237 err = got_commit_graph_open(&graph, "/", 1);
2238 if (err)
2239 goto done;
2241 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2242 check_cancelled, NULL);
2243 if (err)
2244 goto done;
2246 for (;;) {
2247 struct got_object_id *id;
2248 err = got_commit_graph_iter_next(&id, graph, repo,
2249 check_cancelled, NULL);
2250 if (err) {
2251 if (err->code == GOT_ERR_ITER_COMPLETED)
2252 err = NULL;
2253 break;
2256 if (id) {
2257 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2258 break;
2259 if (got_object_id_cmp(id, commit_id) == 0) {
2260 is_same_branch = 1;
2261 break;
2265 done:
2266 if (graph)
2267 got_commit_graph_close(graph);
2268 free(head_commit_id);
2269 if (!err && !is_same_branch)
2270 err = got_error(GOT_ERR_ANCESTRY);
2271 return err;
2274 static const struct got_error *
2275 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2277 static char msg[512];
2278 const char *branch_name;
2280 if (got_ref_is_symbolic(ref))
2281 branch_name = got_ref_get_symref_target(ref);
2282 else
2283 branch_name = got_ref_get_name(ref);
2285 if (strncmp("refs/heads/", branch_name, 11) == 0)
2286 branch_name += 11;
2288 snprintf(msg, sizeof(msg),
2289 "target commit is not contained in branch '%s'; "
2290 "the branch to use must be specified with -b; "
2291 "if necessary a new branch can be created for "
2292 "this commit with 'got branch -c %s BRANCH_NAME'",
2293 branch_name, commit_id_str);
2295 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2298 static const struct got_error *
2299 cmd_checkout(int argc, char *argv[])
2301 const struct got_error *error = NULL;
2302 struct got_repository *repo = NULL;
2303 struct got_reference *head_ref = NULL;
2304 struct got_worktree *worktree = NULL;
2305 char *repo_path = NULL;
2306 char *worktree_path = NULL;
2307 const char *path_prefix = "";
2308 const char *branch_name = GOT_REF_HEAD;
2309 char *commit_id_str = NULL;
2310 int ch, same_path_prefix, allow_nonempty = 0;
2311 struct got_pathlist_head paths;
2312 struct got_checkout_progress_arg cpa;
2314 TAILQ_INIT(&paths);
2316 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2317 switch (ch) {
2318 case 'b':
2319 branch_name = optarg;
2320 break;
2321 case 'c':
2322 commit_id_str = strdup(optarg);
2323 if (commit_id_str == NULL)
2324 return got_error_from_errno("strdup");
2325 break;
2326 case 'E':
2327 allow_nonempty = 1;
2328 break;
2329 case 'p':
2330 path_prefix = optarg;
2331 break;
2332 default:
2333 usage_checkout();
2334 /* NOTREACHED */
2338 argc -= optind;
2339 argv += optind;
2341 #ifndef PROFILE
2342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2343 "unveil", NULL) == -1)
2344 err(1, "pledge");
2345 #endif
2346 if (argc == 1) {
2347 char *cwd, *base, *dotgit;
2348 repo_path = realpath(argv[0], NULL);
2349 if (repo_path == NULL)
2350 return got_error_from_errno2("realpath", argv[0]);
2351 cwd = getcwd(NULL, 0);
2352 if (cwd == NULL) {
2353 error = got_error_from_errno("getcwd");
2354 goto done;
2356 if (path_prefix[0]) {
2357 base = basename(path_prefix);
2358 if (base == NULL) {
2359 error = got_error_from_errno2("basename",
2360 path_prefix);
2361 goto done;
2363 } else {
2364 base = basename(repo_path);
2365 if (base == NULL) {
2366 error = got_error_from_errno2("basename",
2367 repo_path);
2368 goto done;
2371 dotgit = strstr(base, ".git");
2372 if (dotgit)
2373 *dotgit = '\0';
2374 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2375 error = got_error_from_errno("asprintf");
2376 free(cwd);
2377 goto done;
2379 free(cwd);
2380 } else if (argc == 2) {
2381 repo_path = realpath(argv[0], NULL);
2382 if (repo_path == NULL) {
2383 error = got_error_from_errno2("realpath", argv[0]);
2384 goto done;
2386 worktree_path = realpath(argv[1], NULL);
2387 if (worktree_path == NULL) {
2388 if (errno != ENOENT) {
2389 error = got_error_from_errno2("realpath",
2390 argv[1]);
2391 goto done;
2393 worktree_path = strdup(argv[1]);
2394 if (worktree_path == NULL) {
2395 error = got_error_from_errno("strdup");
2396 goto done;
2399 } else
2400 usage_checkout();
2402 got_path_strip_trailing_slashes(repo_path);
2403 got_path_strip_trailing_slashes(worktree_path);
2405 error = got_repo_open(&repo, repo_path, NULL);
2406 if (error != NULL)
2407 goto done;
2409 /* Pre-create work tree path for unveil(2) */
2410 error = got_path_mkdir(worktree_path);
2411 if (error) {
2412 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2413 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2414 goto done;
2415 if (!allow_nonempty &&
2416 !got_path_dir_is_empty(worktree_path)) {
2417 error = got_error_path(worktree_path,
2418 GOT_ERR_DIR_NOT_EMPTY);
2419 goto done;
2423 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2424 if (error)
2425 goto done;
2427 error = got_ref_open(&head_ref, repo, branch_name, 0);
2428 if (error != NULL)
2429 goto done;
2431 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2432 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2433 goto done;
2435 error = got_worktree_open(&worktree, worktree_path);
2436 if (error != NULL)
2437 goto done;
2439 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2440 path_prefix);
2441 if (error != NULL)
2442 goto done;
2443 if (!same_path_prefix) {
2444 error = got_error(GOT_ERR_PATH_PREFIX);
2445 goto done;
2448 if (commit_id_str) {
2449 struct got_object_id *commit_id;
2450 error = got_repo_match_object_id(&commit_id, NULL,
2451 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2452 if (error)
2453 goto done;
2454 error = check_linear_ancestry(commit_id,
2455 got_worktree_get_base_commit_id(worktree), 0, repo);
2456 if (error != NULL) {
2457 free(commit_id);
2458 if (error->code == GOT_ERR_ANCESTRY) {
2459 error = checkout_ancestry_error(
2460 head_ref, commit_id_str);
2462 goto done;
2464 error = check_same_branch(commit_id, head_ref, NULL, repo);
2465 if (error) {
2466 if (error->code == GOT_ERR_ANCESTRY) {
2467 error = checkout_ancestry_error(
2468 head_ref, commit_id_str);
2470 goto done;
2472 error = got_worktree_set_base_commit_id(worktree, repo,
2473 commit_id);
2474 free(commit_id);
2475 if (error)
2476 goto done;
2479 error = got_pathlist_append(&paths, "", NULL);
2480 if (error)
2481 goto done;
2482 cpa.worktree_path = worktree_path;
2483 cpa.had_base_commit_ref_error = 0;
2484 error = got_worktree_checkout_files(worktree, &paths, repo,
2485 checkout_progress, &cpa, check_cancelled, NULL);
2486 if (error != NULL)
2487 goto done;
2489 printf("Now shut up and hack\n");
2490 if (cpa.had_base_commit_ref_error)
2491 show_worktree_base_ref_warning();
2492 done:
2493 got_pathlist_free(&paths);
2494 free(commit_id_str);
2495 free(repo_path);
2496 free(worktree_path);
2497 return error;
2500 struct got_update_progress_arg {
2501 int did_something;
2502 int conflicts;
2503 int obstructed;
2504 int not_updated;
2507 void
2508 print_update_progress_stats(struct got_update_progress_arg *upa)
2510 if (!upa->did_something)
2511 return;
2513 if (upa->conflicts > 0)
2514 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2515 if (upa->obstructed > 0)
2516 printf("File paths obstructed by a non-regular file: %d\n",
2517 upa->obstructed);
2518 if (upa->not_updated > 0)
2519 printf("Files not updated because of existing merge "
2520 "conflicts: %d\n", upa->not_updated);
2523 __dead static void
2524 usage_update(void)
2526 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2527 getprogname());
2528 exit(1);
2531 static const struct got_error *
2532 update_progress(void *arg, unsigned char status, const char *path)
2534 struct got_update_progress_arg *upa = arg;
2536 if (status == GOT_STATUS_EXISTS ||
2537 status == GOT_STATUS_BASE_REF_ERR)
2538 return NULL;
2540 upa->did_something = 1;
2542 /* Base commit bump happens silently. */
2543 if (status == GOT_STATUS_BUMP_BASE)
2544 return NULL;
2546 if (status == GOT_STATUS_CONFLICT)
2547 upa->conflicts++;
2548 if (status == GOT_STATUS_OBSTRUCTED)
2549 upa->obstructed++;
2550 if (status == GOT_STATUS_CANNOT_UPDATE)
2551 upa->not_updated++;
2553 while (path[0] == '/')
2554 path++;
2555 printf("%c %s\n", status, path);
2556 return NULL;
2559 static const struct got_error *
2560 switch_head_ref(struct got_reference *head_ref,
2561 struct got_object_id *commit_id, struct got_worktree *worktree,
2562 struct got_repository *repo)
2564 const struct got_error *err = NULL;
2565 char *base_id_str;
2566 int ref_has_moved = 0;
2568 /* Trivial case: switching between two different references. */
2569 if (strcmp(got_ref_get_name(head_ref),
2570 got_worktree_get_head_ref_name(worktree)) != 0) {
2571 printf("Switching work tree from %s to %s\n",
2572 got_worktree_get_head_ref_name(worktree),
2573 got_ref_get_name(head_ref));
2574 return got_worktree_set_head_ref(worktree, head_ref);
2577 err = check_linear_ancestry(commit_id,
2578 got_worktree_get_base_commit_id(worktree), 0, repo);
2579 if (err) {
2580 if (err->code != GOT_ERR_ANCESTRY)
2581 return err;
2582 ref_has_moved = 1;
2584 if (!ref_has_moved)
2585 return NULL;
2587 /* Switching to a rebased branch with the same reference name. */
2588 err = got_object_id_str(&base_id_str,
2589 got_worktree_get_base_commit_id(worktree));
2590 if (err)
2591 return err;
2592 printf("Reference %s now points at a different branch\n",
2593 got_worktree_get_head_ref_name(worktree));
2594 printf("Switching work tree from %s to %s\n", base_id_str,
2595 got_worktree_get_head_ref_name(worktree));
2596 return NULL;
2599 static const struct got_error *
2600 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2602 const struct got_error *err;
2603 int in_progress;
2605 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2606 if (err)
2607 return err;
2608 if (in_progress)
2609 return got_error(GOT_ERR_REBASING);
2611 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2612 if (err)
2613 return err;
2614 if (in_progress)
2615 return got_error(GOT_ERR_HISTEDIT_BUSY);
2617 return NULL;
2620 static const struct got_error *
2621 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2622 char *argv[], struct got_worktree *worktree)
2624 const struct got_error *err = NULL;
2625 char *path;
2626 int i;
2628 if (argc == 0) {
2629 path = strdup("");
2630 if (path == NULL)
2631 return got_error_from_errno("strdup");
2632 return got_pathlist_append(paths, path, NULL);
2635 for (i = 0; i < argc; i++) {
2636 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2637 if (err)
2638 break;
2639 err = got_pathlist_append(paths, path, NULL);
2640 if (err) {
2641 free(path);
2642 break;
2646 return err;
2649 static const struct got_error *
2650 wrap_not_worktree_error(const struct got_error *orig_err,
2651 const char *cmdname, const char *path)
2653 const struct got_error *err;
2654 struct got_repository *repo;
2655 static char msg[512];
2657 err = got_repo_open(&repo, path, NULL);
2658 if (err)
2659 return orig_err;
2661 snprintf(msg, sizeof(msg),
2662 "'got %s' needs a work tree in addition to a git repository\n"
2663 "Work trees can be checked out from this Git repository with "
2664 "'got checkout'.\n"
2665 "The got(1) manual page contains more information.", cmdname);
2666 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2667 got_repo_close(repo);
2668 return err;
2671 static const struct got_error *
2672 cmd_update(int argc, char *argv[])
2674 const struct got_error *error = NULL;
2675 struct got_repository *repo = NULL;
2676 struct got_worktree *worktree = NULL;
2677 char *worktree_path = NULL;
2678 struct got_object_id *commit_id = NULL;
2679 char *commit_id_str = NULL;
2680 const char *branch_name = NULL;
2681 struct got_reference *head_ref = NULL;
2682 struct got_pathlist_head paths;
2683 struct got_pathlist_entry *pe;
2684 int ch;
2685 struct got_update_progress_arg upa;
2687 TAILQ_INIT(&paths);
2689 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2690 switch (ch) {
2691 case 'b':
2692 branch_name = optarg;
2693 break;
2694 case 'c':
2695 commit_id_str = strdup(optarg);
2696 if (commit_id_str == NULL)
2697 return got_error_from_errno("strdup");
2698 break;
2699 default:
2700 usage_update();
2701 /* NOTREACHED */
2705 argc -= optind;
2706 argv += optind;
2708 #ifndef PROFILE
2709 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2710 "unveil", NULL) == -1)
2711 err(1, "pledge");
2712 #endif
2713 worktree_path = getcwd(NULL, 0);
2714 if (worktree_path == NULL) {
2715 error = got_error_from_errno("getcwd");
2716 goto done;
2718 error = got_worktree_open(&worktree, worktree_path);
2719 if (error) {
2720 if (error->code == GOT_ERR_NOT_WORKTREE)
2721 error = wrap_not_worktree_error(error, "update",
2722 worktree_path);
2723 goto done;
2726 error = check_rebase_or_histedit_in_progress(worktree);
2727 if (error)
2728 goto done;
2730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2731 NULL);
2732 if (error != NULL)
2733 goto done;
2735 error = apply_unveil(got_repo_get_path(repo), 0,
2736 got_worktree_get_root_path(worktree));
2737 if (error)
2738 goto done;
2740 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2741 if (error)
2742 goto done;
2744 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2745 got_worktree_get_head_ref_name(worktree), 0);
2746 if (error != NULL)
2747 goto done;
2748 if (commit_id_str == NULL) {
2749 error = got_ref_resolve(&commit_id, repo, head_ref);
2750 if (error != NULL)
2751 goto done;
2752 error = got_object_id_str(&commit_id_str, commit_id);
2753 if (error != NULL)
2754 goto done;
2755 } else {
2756 error = got_repo_match_object_id(&commit_id, NULL,
2757 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2758 free(commit_id_str);
2759 commit_id_str = NULL;
2760 if (error)
2761 goto done;
2762 error = got_object_id_str(&commit_id_str, commit_id);
2763 if (error)
2764 goto done;
2767 if (branch_name) {
2768 struct got_object_id *head_commit_id;
2769 TAILQ_FOREACH(pe, &paths, entry) {
2770 if (pe->path_len == 0)
2771 continue;
2772 error = got_error_msg(GOT_ERR_BAD_PATH,
2773 "switching between branches requires that "
2774 "the entire work tree gets updated");
2775 goto done;
2777 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2778 if (error)
2779 goto done;
2780 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2781 repo);
2782 free(head_commit_id);
2783 if (error != NULL)
2784 goto done;
2785 error = check_same_branch(commit_id, head_ref, NULL, repo);
2786 if (error)
2787 goto done;
2788 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2789 if (error)
2790 goto done;
2791 } else {
2792 error = check_linear_ancestry(commit_id,
2793 got_worktree_get_base_commit_id(worktree), 0, repo);
2794 if (error != NULL) {
2795 if (error->code == GOT_ERR_ANCESTRY)
2796 error = got_error(GOT_ERR_BRANCH_MOVED);
2797 goto done;
2799 error = check_same_branch(commit_id, head_ref, NULL, repo);
2800 if (error)
2801 goto done;
2804 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2805 commit_id) != 0) {
2806 error = got_worktree_set_base_commit_id(worktree, repo,
2807 commit_id);
2808 if (error)
2809 goto done;
2812 memset(&upa, 0, sizeof(upa));
2813 error = got_worktree_checkout_files(worktree, &paths, repo,
2814 update_progress, &upa, check_cancelled, NULL);
2815 if (error != NULL)
2816 goto done;
2818 if (upa.did_something)
2819 printf("Updated to commit %s\n", commit_id_str);
2820 else
2821 printf("Already up-to-date\n");
2822 print_update_progress_stats(&upa);
2823 done:
2824 free(worktree_path);
2825 TAILQ_FOREACH(pe, &paths, entry)
2826 free((char *)pe->path);
2827 got_pathlist_free(&paths);
2828 free(commit_id);
2829 free(commit_id_str);
2830 return error;
2833 static const struct got_error *
2834 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2835 const char *path, int diff_context, int ignore_whitespace,
2836 struct got_repository *repo)
2838 const struct got_error *err = NULL;
2839 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2841 if (blob_id1) {
2842 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2843 if (err)
2844 goto done;
2847 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2848 if (err)
2849 goto done;
2851 while (path[0] == '/')
2852 path++;
2853 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2854 ignore_whitespace, stdout);
2855 done:
2856 if (blob1)
2857 got_object_blob_close(blob1);
2858 got_object_blob_close(blob2);
2859 return err;
2862 static const struct got_error *
2863 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2864 const char *path, int diff_context, int ignore_whitespace,
2865 struct got_repository *repo)
2867 const struct got_error *err = NULL;
2868 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2869 struct got_diff_blob_output_unidiff_arg arg;
2871 if (tree_id1) {
2872 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2873 if (err)
2874 goto done;
2877 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2878 if (err)
2879 goto done;
2881 arg.diff_context = diff_context;
2882 arg.ignore_whitespace = ignore_whitespace;
2883 arg.outfile = stdout;
2884 while (path[0] == '/')
2885 path++;
2886 err = got_diff_tree(tree1, tree2, path, path, repo,
2887 got_diff_blob_output_unidiff, &arg, 1);
2888 done:
2889 if (tree1)
2890 got_object_tree_close(tree1);
2891 if (tree2)
2892 got_object_tree_close(tree2);
2893 return err;
2896 static const struct got_error *
2897 get_changed_paths(struct got_pathlist_head *paths,
2898 struct got_commit_object *commit, struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2902 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2903 struct got_object_qid *qid;
2905 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2906 if (qid != NULL) {
2907 struct got_commit_object *pcommit;
2908 err = got_object_open_as_commit(&pcommit, repo,
2909 qid->id);
2910 if (err)
2911 return err;
2913 tree_id1 = got_object_commit_get_tree_id(pcommit);
2914 got_object_commit_close(pcommit);
2918 if (tree_id1) {
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2920 if (err)
2921 goto done;
2924 tree_id2 = got_object_commit_get_tree_id(commit);
2925 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2926 if (err)
2927 goto done;
2929 err = got_diff_tree(tree1, tree2, "", "", repo,
2930 got_diff_tree_collect_changed_paths, paths, 0);
2931 done:
2932 if (tree1)
2933 got_object_tree_close(tree1);
2934 if (tree2)
2935 got_object_tree_close(tree2);
2936 return err;
2939 static const struct got_error *
2940 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2941 const char *path, int diff_context, struct got_repository *repo)
2943 const struct got_error *err = NULL;
2944 struct got_commit_object *pcommit = NULL;
2945 char *id_str1 = NULL, *id_str2 = NULL;
2946 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2947 struct got_object_qid *qid;
2949 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2950 if (qid != NULL) {
2951 err = got_object_open_as_commit(&pcommit, repo,
2952 qid->id);
2953 if (err)
2954 return err;
2957 if (path && path[0] != '\0') {
2958 int obj_type;
2959 err = got_object_id_by_path(&obj_id2, repo, id, path);
2960 if (err)
2961 goto done;
2962 err = got_object_id_str(&id_str2, obj_id2);
2963 if (err) {
2964 free(obj_id2);
2965 goto done;
2967 if (pcommit) {
2968 err = got_object_id_by_path(&obj_id1, repo,
2969 qid->id, path);
2970 if (err) {
2971 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
2972 free(obj_id2);
2973 goto done;
2975 } else {
2976 err = got_object_id_str(&id_str1, obj_id1);
2977 if (err) {
2978 free(obj_id2);
2979 goto done;
2983 err = got_object_get_type(&obj_type, repo, obj_id2);
2984 if (err) {
2985 free(obj_id2);
2986 goto done;
2988 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2989 switch (obj_type) {
2990 case GOT_OBJ_TYPE_BLOB:
2991 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2992 0, repo);
2993 break;
2994 case GOT_OBJ_TYPE_TREE:
2995 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2996 0, repo);
2997 break;
2998 default:
2999 err = got_error(GOT_ERR_OBJ_TYPE);
3000 break;
3002 free(obj_id1);
3003 free(obj_id2);
3004 } else {
3005 obj_id2 = got_object_commit_get_tree_id(commit);
3006 err = got_object_id_str(&id_str2, obj_id2);
3007 if (err)
3008 goto done;
3009 obj_id1 = got_object_commit_get_tree_id(pcommit);
3010 err = got_object_id_str(&id_str1, obj_id1);
3011 if (err)
3012 goto done;
3013 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3014 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3016 done:
3017 free(id_str1);
3018 free(id_str2);
3019 if (pcommit)
3020 got_object_commit_close(pcommit);
3021 return err;
3024 static char *
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3028 char *p, *s;
3030 tm = gmtime_r(time, &mytm);
3031 if (tm == NULL)
3032 return NULL;
3033 s = asctime_r(tm, datebuf);
3034 if (s == NULL)
3035 return NULL;
3036 p = strchr(s, '\n');
3037 if (p)
3038 *p = '\0';
3039 return s;
3042 static const struct got_error *
3043 match_logmsg(int *have_match, struct got_object_id *id,
3044 struct got_commit_object *commit, regex_t *regex)
3046 const struct got_error *err = NULL;
3047 regmatch_t regmatch;
3048 char *id_str = NULL, *logmsg = NULL;
3050 *have_match = 0;
3052 err = got_object_id_str(&id_str, id);
3053 if (err)
3054 return err;
3056 err = got_object_commit_get_logmsg(&logmsg, commit);
3057 if (err)
3058 goto done;
3060 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3061 *have_match = 1;
3062 done:
3063 free(id_str);
3064 free(logmsg);
3065 return err;
3068 static void
3069 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3070 regex_t *regex)
3072 regmatch_t regmatch;
3073 struct got_pathlist_entry *pe;
3075 *have_match = 0;
3077 TAILQ_FOREACH(pe, changed_paths, entry) {
3078 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3079 *have_match = 1;
3080 break;
3085 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3087 static const struct got_error *
3088 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3089 struct got_repository *repo, const char *path,
3090 struct got_pathlist_head *changed_paths, int show_patch,
3091 int diff_context, struct got_reflist_head *refs)
3093 const struct got_error *err = NULL;
3094 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3095 char datebuf[26];
3096 time_t committer_time;
3097 const char *author, *committer;
3098 char *refs_str = NULL;
3099 struct got_reflist_entry *re;
3101 SIMPLEQ_FOREACH(re, refs, entry) {
3102 char *s;
3103 const char *name;
3104 struct got_tag_object *tag = NULL;
3105 int cmp;
3107 name = got_ref_get_name(re->ref);
3108 if (strcmp(name, GOT_REF_HEAD) == 0)
3109 continue;
3110 if (strncmp(name, "refs/", 5) == 0)
3111 name += 5;
3112 if (strncmp(name, "got/", 4) == 0)
3113 continue;
3114 if (strncmp(name, "heads/", 6) == 0)
3115 name += 6;
3116 if (strncmp(name, "remotes/", 8) == 0) {
3117 name += 8;
3118 s = strstr(name, "/" GOT_REF_HEAD);
3119 if (s != NULL && s[strlen(s)] == '\0')
3120 continue;
3122 if (strncmp(name, "tags/", 5) == 0) {
3123 err = got_object_open_as_tag(&tag, repo, re->id);
3124 if (err) {
3125 if (err->code != GOT_ERR_OBJ_TYPE)
3126 return err;
3127 /* Ref points at something other than a tag. */
3128 err = NULL;
3129 tag = NULL;
3132 cmp = got_object_id_cmp(tag ?
3133 got_object_tag_get_object_id(tag) : re->id, id);
3134 if (tag)
3135 got_object_tag_close(tag);
3136 if (cmp != 0)
3137 continue;
3138 s = refs_str;
3139 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3140 name) == -1) {
3141 err = got_error_from_errno("asprintf");
3142 free(s);
3143 return err;
3145 free(s);
3147 err = got_object_id_str(&id_str, id);
3148 if (err)
3149 return err;
3151 printf(GOT_COMMIT_SEP_STR);
3152 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3153 refs_str ? refs_str : "", refs_str ? ")" : "");
3154 free(id_str);
3155 id_str = NULL;
3156 free(refs_str);
3157 refs_str = NULL;
3158 printf("from: %s\n", got_object_commit_get_author(commit));
3159 committer_time = got_object_commit_get_committer_time(commit);
3160 datestr = get_datestr(&committer_time, datebuf);
3161 if (datestr)
3162 printf("date: %s UTC\n", datestr);
3163 author = got_object_commit_get_author(commit);
3164 committer = got_object_commit_get_committer(commit);
3165 if (strcmp(author, committer) != 0)
3166 printf("via: %s\n", committer);
3167 if (got_object_commit_get_nparents(commit) > 1) {
3168 const struct got_object_id_queue *parent_ids;
3169 struct got_object_qid *qid;
3170 int n = 1;
3171 parent_ids = got_object_commit_get_parent_ids(commit);
3172 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3173 err = got_object_id_str(&id_str, qid->id);
3174 if (err)
3175 return err;
3176 printf("parent %d: %s\n", n++, id_str);
3177 free(id_str);
3181 err = got_object_commit_get_logmsg(&logmsg0, commit);
3182 if (err)
3183 return err;
3185 logmsg = logmsg0;
3186 do {
3187 line = strsep(&logmsg, "\n");
3188 if (line)
3189 printf(" %s\n", line);
3190 } while (line);
3191 free(logmsg0);
3193 if (changed_paths) {
3194 struct got_pathlist_entry *pe;
3195 TAILQ_FOREACH(pe, changed_paths, entry) {
3196 struct got_diff_changed_path *cp = pe->data;
3197 printf(" %c %s\n", cp->status, pe->path);
3199 printf("\n");
3201 if (show_patch) {
3202 err = print_patch(commit, id, path, diff_context, repo);
3203 if (err == 0)
3204 printf("\n");
3207 if (fflush(stdout) != 0 && err == NULL)
3208 err = got_error_from_errno("fflush");
3209 return err;
3212 static const struct got_error *
3213 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3214 struct got_repository *repo, const char *path, int show_changed_paths,
3215 int show_patch, const char *search_pattern, int diff_context, int limit,
3216 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3218 const struct got_error *err;
3219 struct got_commit_graph *graph;
3220 regex_t regex;
3221 int have_match;
3222 struct got_object_id_queue reversed_commits;
3223 struct got_object_qid *qid;
3224 struct got_commit_object *commit;
3225 struct got_pathlist_head changed_paths;
3226 struct got_pathlist_entry *pe;
3228 SIMPLEQ_INIT(&reversed_commits);
3229 TAILQ_INIT(&changed_paths);
3231 if (search_pattern && regcomp(&regex, search_pattern,
3232 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3233 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3235 err = got_commit_graph_open(&graph, path, !log_branches);
3236 if (err)
3237 return err;
3238 err = got_commit_graph_iter_start(graph, root_id, repo,
3239 check_cancelled, NULL);
3240 if (err)
3241 goto done;
3242 for (;;) {
3243 struct got_object_id *id;
3245 if (sigint_received || sigpipe_received)
3246 break;
3248 err = got_commit_graph_iter_next(&id, graph, repo,
3249 check_cancelled, NULL);
3250 if (err) {
3251 if (err->code == GOT_ERR_ITER_COMPLETED)
3252 err = NULL;
3253 break;
3255 if (id == NULL)
3256 break;
3258 err = got_object_open_as_commit(&commit, repo, id);
3259 if (err)
3260 break;
3262 if (show_changed_paths && !reverse_display_order) {
3263 err = get_changed_paths(&changed_paths, commit, repo);
3264 if (err)
3265 break;
3268 if (search_pattern) {
3269 err = match_logmsg(&have_match, id, commit, &regex);
3270 if (err) {
3271 got_object_commit_close(commit);
3272 break;
3274 if (have_match == 0 && show_changed_paths)
3275 match_changed_paths(&have_match,
3276 &changed_paths, &regex);
3277 if (have_match == 0) {
3278 got_object_commit_close(commit);
3279 TAILQ_FOREACH(pe, &changed_paths, entry) {
3280 free((char *)pe->path);
3281 free(pe->data);
3283 got_pathlist_free(&changed_paths);
3284 continue;
3288 if (reverse_display_order) {
3289 err = got_object_qid_alloc(&qid, id);
3290 if (err)
3291 break;
3292 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3293 got_object_commit_close(commit);
3294 } else {
3295 err = print_commit(commit, id, repo, path,
3296 show_changed_paths ? &changed_paths : NULL,
3297 show_patch, diff_context, refs);
3298 got_object_commit_close(commit);
3299 if (err)
3300 break;
3302 if ((limit && --limit == 0) ||
3303 (end_id && got_object_id_cmp(id, end_id) == 0))
3304 break;
3306 TAILQ_FOREACH(pe, &changed_paths, entry) {
3307 free((char *)pe->path);
3308 free(pe->data);
3310 got_pathlist_free(&changed_paths);
3312 if (reverse_display_order) {
3313 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3314 err = got_object_open_as_commit(&commit, repo, qid->id);
3315 if (err)
3316 break;
3317 if (show_changed_paths) {
3318 err = get_changed_paths(&changed_paths,
3319 commit, repo);
3320 if (err)
3321 break;
3323 err = print_commit(commit, qid->id, repo, path,
3324 show_changed_paths ? &changed_paths : NULL,
3325 show_patch, diff_context, refs);
3326 got_object_commit_close(commit);
3327 if (err)
3328 break;
3329 TAILQ_FOREACH(pe, &changed_paths, entry) {
3330 free((char *)pe->path);
3331 free(pe->data);
3333 got_pathlist_free(&changed_paths);
3336 done:
3337 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3338 qid = SIMPLEQ_FIRST(&reversed_commits);
3339 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3340 got_object_qid_free(qid);
3342 TAILQ_FOREACH(pe, &changed_paths, entry) {
3343 free((char *)pe->path);
3344 free(pe->data);
3346 got_pathlist_free(&changed_paths);
3347 if (search_pattern)
3348 regfree(&regex);
3349 got_commit_graph_close(graph);
3350 return err;
3353 __dead static void
3354 usage_log(void)
3356 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3357 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3358 "[-R] [path]\n", getprogname());
3359 exit(1);
3362 static int
3363 get_default_log_limit(void)
3365 const char *got_default_log_limit;
3366 long long n;
3367 const char *errstr;
3369 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3370 if (got_default_log_limit == NULL)
3371 return 0;
3372 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3373 if (errstr != NULL)
3374 return 0;
3375 return n;
3378 static const struct got_error *
3379 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3380 struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 struct got_reference *ref;
3385 *id = NULL;
3387 err = got_ref_open(&ref, repo, commit_arg, 0);
3388 if (err == NULL) {
3389 int obj_type;
3390 err = got_ref_resolve(id, repo, ref);
3391 got_ref_close(ref);
3392 if (err)
3393 return err;
3394 err = got_object_get_type(&obj_type, repo, *id);
3395 if (err)
3396 return err;
3397 if (obj_type == GOT_OBJ_TYPE_TAG) {
3398 struct got_tag_object *tag;
3399 err = got_object_open_as_tag(&tag, repo, *id);
3400 if (err)
3401 return err;
3402 if (got_object_tag_get_object_type(tag) !=
3403 GOT_OBJ_TYPE_COMMIT) {
3404 got_object_tag_close(tag);
3405 return got_error(GOT_ERR_OBJ_TYPE);
3407 free(*id);
3408 *id = got_object_id_dup(
3409 got_object_tag_get_object_id(tag));
3410 if (*id == NULL)
3411 err = got_error_from_errno(
3412 "got_object_id_dup");
3413 got_object_tag_close(tag);
3414 if (err)
3415 return err;
3416 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3417 return got_error(GOT_ERR_OBJ_TYPE);
3418 } else {
3419 err = got_repo_match_object_id_prefix(id, commit_arg,
3420 GOT_OBJ_TYPE_COMMIT, repo);
3423 return err;
3426 static const struct got_error *
3427 cmd_log(int argc, char *argv[])
3429 const struct got_error *error;
3430 struct got_repository *repo = NULL;
3431 struct got_worktree *worktree = NULL;
3432 struct got_object_id *start_id = NULL, *end_id = NULL;
3433 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3434 const char *start_commit = NULL, *end_commit = NULL;
3435 const char *search_pattern = NULL;
3436 int diff_context = -1, ch;
3437 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3438 int reverse_display_order = 0;
3439 const char *errstr;
3440 struct got_reflist_head refs;
3442 SIMPLEQ_INIT(&refs);
3444 #ifndef PROFILE
3445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3446 NULL)
3447 == -1)
3448 err(1, "pledge");
3449 #endif
3451 limit = get_default_log_limit();
3453 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3454 switch (ch) {
3455 case 'p':
3456 show_patch = 1;
3457 break;
3458 case 'P':
3459 show_changed_paths = 1;
3460 break;
3461 case 'c':
3462 start_commit = optarg;
3463 break;
3464 case 'C':
3465 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3466 &errstr);
3467 if (errstr != NULL)
3468 err(1, "-C option %s", errstr);
3469 break;
3470 case 'l':
3471 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3472 if (errstr != NULL)
3473 err(1, "-l option %s", errstr);
3474 break;
3475 case 'b':
3476 log_branches = 1;
3477 break;
3478 case 'r':
3479 repo_path = realpath(optarg, NULL);
3480 if (repo_path == NULL)
3481 return got_error_from_errno2("realpath",
3482 optarg);
3483 got_path_strip_trailing_slashes(repo_path);
3484 break;
3485 case 'R':
3486 reverse_display_order = 1;
3487 break;
3488 case 's':
3489 search_pattern = optarg;
3490 break;
3491 case 'x':
3492 end_commit = optarg;
3493 break;
3494 default:
3495 usage_log();
3496 /* NOTREACHED */
3500 argc -= optind;
3501 argv += optind;
3503 if (diff_context == -1)
3504 diff_context = 3;
3505 else if (!show_patch)
3506 errx(1, "-C reguires -p");
3508 cwd = getcwd(NULL, 0);
3509 if (cwd == NULL) {
3510 error = got_error_from_errno("getcwd");
3511 goto done;
3514 if (repo_path == NULL) {
3515 error = got_worktree_open(&worktree, cwd);
3516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3517 goto done;
3518 error = NULL;
3521 if (argc == 0) {
3522 path = strdup("");
3523 if (path == NULL) {
3524 error = got_error_from_errno("strdup");
3525 goto done;
3527 } else if (argc == 1) {
3528 if (worktree) {
3529 error = got_worktree_resolve_path(&path, worktree,
3530 argv[0]);
3531 if (error)
3532 goto done;
3533 } else {
3534 path = strdup(argv[0]);
3535 if (path == NULL) {
3536 error = got_error_from_errno("strdup");
3537 goto done;
3540 } else
3541 usage_log();
3543 if (repo_path == NULL) {
3544 repo_path = worktree ?
3545 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3547 if (repo_path == NULL) {
3548 error = got_error_from_errno("strdup");
3549 goto done;
3552 error = got_repo_open(&repo, repo_path, NULL);
3553 if (error != NULL)
3554 goto done;
3556 error = apply_unveil(got_repo_get_path(repo), 1,
3557 worktree ? got_worktree_get_root_path(worktree) : NULL);
3558 if (error)
3559 goto done;
3561 if (start_commit == NULL) {
3562 struct got_reference *head_ref;
3563 struct got_commit_object *commit = NULL;
3564 error = got_ref_open(&head_ref, repo,
3565 worktree ? got_worktree_get_head_ref_name(worktree)
3566 : GOT_REF_HEAD, 0);
3567 if (error != NULL)
3568 goto done;
3569 error = got_ref_resolve(&start_id, repo, head_ref);
3570 got_ref_close(head_ref);
3571 if (error != NULL)
3572 goto done;
3573 error = got_object_open_as_commit(&commit, repo,
3574 start_id);
3575 if (error != NULL)
3576 goto done;
3577 got_object_commit_close(commit);
3578 } else {
3579 error = resolve_commit_arg(&start_id, start_commit, repo);
3580 if (error != NULL)
3581 goto done;
3583 if (end_commit != NULL) {
3584 error = resolve_commit_arg(&end_id, end_commit, repo);
3585 if (error != NULL)
3586 goto done;
3589 if (worktree) {
3590 const char *prefix = got_worktree_get_path_prefix(worktree);
3591 char *p;
3592 if (asprintf(&p, "%s%s%s", prefix,
3593 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3594 error = got_error_from_errno("asprintf");
3595 goto done;
3597 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3598 free(p);
3599 } else
3600 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3601 if (error != NULL)
3602 goto done;
3603 if (in_repo_path) {
3604 free(path);
3605 path = in_repo_path;
3608 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3609 if (error)
3610 goto done;
3612 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3613 show_patch, search_pattern, diff_context, limit, log_branches,
3614 reverse_display_order, &refs);
3615 done:
3616 free(path);
3617 free(repo_path);
3618 free(cwd);
3619 if (worktree)
3620 got_worktree_close(worktree);
3621 if (repo) {
3622 const struct got_error *repo_error;
3623 repo_error = got_repo_close(repo);
3624 if (error == NULL)
3625 error = repo_error;
3627 got_ref_list_free(&refs);
3628 return error;
3631 __dead static void
3632 usage_diff(void)
3634 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3635 "[-w] [object1 object2 | path]\n", getprogname());
3636 exit(1);
3639 struct print_diff_arg {
3640 struct got_repository *repo;
3641 struct got_worktree *worktree;
3642 int diff_context;
3643 const char *id_str;
3644 int header_shown;
3645 int diff_staged;
3646 int ignore_whitespace;
3650 * Create a file which contains the target path of a symlink so we can feed
3651 * it as content to the diff engine.
3653 static const struct got_error *
3654 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3655 const char *abspath)
3657 const struct got_error *err = NULL;
3658 char target_path[PATH_MAX];
3659 ssize_t target_len, outlen;
3661 *fd = -1;
3663 if (dirfd != -1) {
3664 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3665 if (target_len == -1)
3666 return got_error_from_errno2("readlinkat", abspath);
3667 } else {
3668 target_len = readlink(abspath, target_path, PATH_MAX);
3669 if (target_len == -1)
3670 return got_error_from_errno2("readlink", abspath);
3673 *fd = got_opentempfd();
3674 if (*fd == -1)
3675 return got_error_from_errno("got_opentempfd");
3677 outlen = write(*fd, target_path, target_len);
3678 if (outlen == -1) {
3679 err = got_error_from_errno("got_opentempfd");
3680 goto done;
3683 if (lseek(*fd, 0, SEEK_SET) == -1) {
3684 err = got_error_from_errno2("lseek", abspath);
3685 goto done;
3687 done:
3688 if (err) {
3689 close(*fd);
3690 *fd = -1;
3692 return err;
3695 static const struct got_error *
3696 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3697 const char *path, struct got_object_id *blob_id,
3698 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3699 int dirfd, const char *de_name)
3701 struct print_diff_arg *a = arg;
3702 const struct got_error *err = NULL;
3703 struct got_blob_object *blob1 = NULL;
3704 int fd = -1;
3705 FILE *f2 = NULL;
3706 char *abspath = NULL, *label1 = NULL;
3707 struct stat sb;
3709 if (a->diff_staged) {
3710 if (staged_status != GOT_STATUS_MODIFY &&
3711 staged_status != GOT_STATUS_ADD &&
3712 staged_status != GOT_STATUS_DELETE)
3713 return NULL;
3714 } else {
3715 if (staged_status == GOT_STATUS_DELETE)
3716 return NULL;
3717 if (status == GOT_STATUS_NONEXISTENT)
3718 return got_error_set_errno(ENOENT, path);
3719 if (status != GOT_STATUS_MODIFY &&
3720 status != GOT_STATUS_ADD &&
3721 status != GOT_STATUS_DELETE &&
3722 status != GOT_STATUS_CONFLICT)
3723 return NULL;
3726 if (!a->header_shown) {
3727 printf("diff %s %s%s\n", a->id_str,
3728 got_worktree_get_root_path(a->worktree),
3729 a->diff_staged ? " (staged changes)" : "");
3730 a->header_shown = 1;
3733 if (a->diff_staged) {
3734 const char *label1 = NULL, *label2 = NULL;
3735 switch (staged_status) {
3736 case GOT_STATUS_MODIFY:
3737 label1 = path;
3738 label2 = path;
3739 break;
3740 case GOT_STATUS_ADD:
3741 label2 = path;
3742 break;
3743 case GOT_STATUS_DELETE:
3744 label1 = path;
3745 break;
3746 default:
3747 return got_error(GOT_ERR_FILE_STATUS);
3749 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3750 label1, label2, a->diff_context, a->ignore_whitespace,
3751 a->repo, stdout);
3754 if (staged_status == GOT_STATUS_ADD ||
3755 staged_status == GOT_STATUS_MODIFY) {
3756 char *id_str;
3757 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3758 8192);
3759 if (err)
3760 goto done;
3761 err = got_object_id_str(&id_str, staged_blob_id);
3762 if (err)
3763 goto done;
3764 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3765 err = got_error_from_errno("asprintf");
3766 free(id_str);
3767 goto done;
3769 free(id_str);
3770 } else if (status != GOT_STATUS_ADD) {
3771 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3772 if (err)
3773 goto done;
3776 if (status != GOT_STATUS_DELETE) {
3777 if (asprintf(&abspath, "%s/%s",
3778 got_worktree_get_root_path(a->worktree), path) == -1) {
3779 err = got_error_from_errno("asprintf");
3780 goto done;
3783 if (dirfd != -1) {
3784 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3785 if (fd == -1) {
3786 if (errno != ELOOP) {
3787 err = got_error_from_errno2("openat",
3788 abspath);
3789 goto done;
3791 err = get_symlink_target_file(&fd, dirfd,
3792 de_name, abspath);
3793 if (err)
3794 goto done;
3796 } else {
3797 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3798 if (fd == -1) {
3799 if (errno != ELOOP) {
3800 err = got_error_from_errno2("open",
3801 abspath);
3802 goto done;
3804 err = get_symlink_target_file(&fd, dirfd,
3805 de_name, abspath);
3806 if (err)
3807 goto done;
3810 if (fstat(fd, &sb) == -1) {
3811 err = got_error_from_errno2("fstat", abspath);
3812 goto done;
3814 f2 = fdopen(fd, "r");
3815 if (f2 == NULL) {
3816 err = got_error_from_errno2("fdopen", abspath);
3817 goto done;
3819 fd = -1;
3820 } else
3821 sb.st_size = 0;
3823 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3824 a->diff_context, a->ignore_whitespace, stdout);
3825 done:
3826 if (blob1)
3827 got_object_blob_close(blob1);
3828 if (f2 && fclose(f2) == EOF && err == NULL)
3829 err = got_error_from_errno("fclose");
3830 if (fd != -1 && close(fd) == -1 && err == NULL)
3831 err = got_error_from_errno("close");
3832 free(abspath);
3833 return err;
3836 static const struct got_error *
3837 cmd_diff(int argc, char *argv[])
3839 const struct got_error *error;
3840 struct got_repository *repo = NULL;
3841 struct got_worktree *worktree = NULL;
3842 char *cwd = NULL, *repo_path = NULL;
3843 struct got_object_id *id1 = NULL, *id2 = NULL;
3844 const char *id_str1 = NULL, *id_str2 = NULL;
3845 char *label1 = NULL, *label2 = NULL;
3846 int type1, type2;
3847 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3848 const char *errstr;
3849 char *path = NULL;
3851 #ifndef PROFILE
3852 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3853 NULL) == -1)
3854 err(1, "pledge");
3855 #endif
3857 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3858 switch (ch) {
3859 case 'C':
3860 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3861 &errstr);
3862 if (errstr != NULL)
3863 err(1, "-C option %s", errstr);
3864 break;
3865 case 'r':
3866 repo_path = realpath(optarg, NULL);
3867 if (repo_path == NULL)
3868 return got_error_from_errno2("realpath",
3869 optarg);
3870 got_path_strip_trailing_slashes(repo_path);
3871 break;
3872 case 's':
3873 diff_staged = 1;
3874 break;
3875 case 'w':
3876 ignore_whitespace = 1;
3877 break;
3878 default:
3879 usage_diff();
3880 /* NOTREACHED */
3884 argc -= optind;
3885 argv += optind;
3887 cwd = getcwd(NULL, 0);
3888 if (cwd == NULL) {
3889 error = got_error_from_errno("getcwd");
3890 goto done;
3892 if (argc <= 1) {
3893 if (repo_path)
3894 errx(1,
3895 "-r option can't be used when diffing a work tree");
3896 error = got_worktree_open(&worktree, cwd);
3897 if (error) {
3898 if (error->code == GOT_ERR_NOT_WORKTREE)
3899 error = wrap_not_worktree_error(error, "diff",
3900 cwd);
3901 goto done;
3903 repo_path = strdup(got_worktree_get_repo_path(worktree));
3904 if (repo_path == NULL) {
3905 error = got_error_from_errno("strdup");
3906 goto done;
3908 if (argc == 1) {
3909 error = got_worktree_resolve_path(&path, worktree,
3910 argv[0]);
3911 if (error)
3912 goto done;
3913 } else {
3914 path = strdup("");
3915 if (path == NULL) {
3916 error = got_error_from_errno("strdup");
3917 goto done;
3920 } else if (argc == 2) {
3921 if (diff_staged)
3922 errx(1, "-s option can't be used when diffing "
3923 "objects in repository");
3924 id_str1 = argv[0];
3925 id_str2 = argv[1];
3926 if (repo_path == NULL) {
3927 error = got_worktree_open(&worktree, cwd);
3928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3929 goto done;
3930 if (worktree) {
3931 repo_path = strdup(
3932 got_worktree_get_repo_path(worktree));
3933 if (repo_path == NULL) {
3934 error = got_error_from_errno("strdup");
3935 goto done;
3937 } else {
3938 repo_path = strdup(cwd);
3939 if (repo_path == NULL) {
3940 error = got_error_from_errno("strdup");
3941 goto done;
3945 } else
3946 usage_diff();
3948 error = got_repo_open(&repo, repo_path, NULL);
3949 free(repo_path);
3950 if (error != NULL)
3951 goto done;
3953 error = apply_unveil(got_repo_get_path(repo), 1,
3954 worktree ? got_worktree_get_root_path(worktree) : NULL);
3955 if (error)
3956 goto done;
3958 if (argc <= 1) {
3959 struct print_diff_arg arg;
3960 struct got_pathlist_head paths;
3961 char *id_str;
3963 TAILQ_INIT(&paths);
3965 error = got_object_id_str(&id_str,
3966 got_worktree_get_base_commit_id(worktree));
3967 if (error)
3968 goto done;
3969 arg.repo = repo;
3970 arg.worktree = worktree;
3971 arg.diff_context = diff_context;
3972 arg.id_str = id_str;
3973 arg.header_shown = 0;
3974 arg.diff_staged = diff_staged;
3975 arg.ignore_whitespace = ignore_whitespace;
3977 error = got_pathlist_append(&paths, path, NULL);
3978 if (error)
3979 goto done;
3981 error = got_worktree_status(worktree, &paths, repo, print_diff,
3982 &arg, check_cancelled, NULL);
3983 free(id_str);
3984 got_pathlist_free(&paths);
3985 goto done;
3988 error = got_repo_match_object_id(&id1, &label1, id_str1,
3989 GOT_OBJ_TYPE_ANY, 1, repo);
3990 if (error)
3991 goto done;
3993 error = got_repo_match_object_id(&id2, &label2, id_str2,
3994 GOT_OBJ_TYPE_ANY, 1, repo);
3995 if (error)
3996 goto done;
3998 error = got_object_get_type(&type1, repo, id1);
3999 if (error)
4000 goto done;
4002 error = got_object_get_type(&type2, repo, id2);
4003 if (error)
4004 goto done;
4006 if (type1 != type2) {
4007 error = got_error(GOT_ERR_OBJ_TYPE);
4008 goto done;
4011 switch (type1) {
4012 case GOT_OBJ_TYPE_BLOB:
4013 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4014 diff_context, ignore_whitespace, repo, stdout);
4015 break;
4016 case GOT_OBJ_TYPE_TREE:
4017 error = got_diff_objects_as_trees(id1, id2, "", "",
4018 diff_context, ignore_whitespace, repo, stdout);
4019 break;
4020 case GOT_OBJ_TYPE_COMMIT:
4021 printf("diff %s %s\n", label1, label2);
4022 error = got_diff_objects_as_commits(id1, id2, diff_context,
4023 ignore_whitespace, repo, stdout);
4024 break;
4025 default:
4026 error = got_error(GOT_ERR_OBJ_TYPE);
4028 done:
4029 free(label1);
4030 free(label2);
4031 free(id1);
4032 free(id2);
4033 free(path);
4034 if (worktree)
4035 got_worktree_close(worktree);
4036 if (repo) {
4037 const struct got_error *repo_error;
4038 repo_error = got_repo_close(repo);
4039 if (error == NULL)
4040 error = repo_error;
4042 return error;
4045 __dead static void
4046 usage_blame(void)
4048 fprintf(stderr,
4049 "usage: %s blame [-c commit] [-r repository-path] path\n",
4050 getprogname());
4051 exit(1);
4054 struct blame_line {
4055 int annotated;
4056 char *id_str;
4057 char *committer;
4058 char datebuf[11]; /* YYYY-MM-DD + NUL */
4061 struct blame_cb_args {
4062 struct blame_line *lines;
4063 int nlines;
4064 int nlines_prec;
4065 int lineno_cur;
4066 off_t *line_offsets;
4067 FILE *f;
4068 struct got_repository *repo;
4071 static const struct got_error *
4072 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4074 const struct got_error *err = NULL;
4075 struct blame_cb_args *a = arg;
4076 struct blame_line *bline;
4077 char *line = NULL;
4078 size_t linesize = 0;
4079 struct got_commit_object *commit = NULL;
4080 off_t offset;
4081 struct tm tm;
4082 time_t committer_time;
4084 if (nlines != a->nlines ||
4085 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4086 return got_error(GOT_ERR_RANGE);
4088 if (sigint_received)
4089 return got_error(GOT_ERR_ITER_COMPLETED);
4091 if (lineno == -1)
4092 return NULL; /* no change in this commit */
4094 /* Annotate this line. */
4095 bline = &a->lines[lineno - 1];
4096 if (bline->annotated)
4097 return NULL;
4098 err = got_object_id_str(&bline->id_str, id);
4099 if (err)
4100 return err;
4102 err = got_object_open_as_commit(&commit, a->repo, id);
4103 if (err)
4104 goto done;
4106 bline->committer = strdup(got_object_commit_get_committer(commit));
4107 if (bline->committer == NULL) {
4108 err = got_error_from_errno("strdup");
4109 goto done;
4112 committer_time = got_object_commit_get_committer_time(commit);
4113 if (localtime_r(&committer_time, &tm) == NULL)
4114 return got_error_from_errno("localtime_r");
4115 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4116 &tm) >= sizeof(bline->datebuf)) {
4117 err = got_error(GOT_ERR_NO_SPACE);
4118 goto done;
4120 bline->annotated = 1;
4122 /* Print lines annotated so far. */
4123 bline = &a->lines[a->lineno_cur - 1];
4124 if (!bline->annotated)
4125 goto done;
4127 offset = a->line_offsets[a->lineno_cur - 1];
4128 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4129 err = got_error_from_errno("fseeko");
4130 goto done;
4133 while (bline->annotated) {
4134 char *smallerthan, *at, *nl, *committer;
4135 size_t len;
4137 if (getline(&line, &linesize, a->f) == -1) {
4138 if (ferror(a->f))
4139 err = got_error_from_errno("getline");
4140 break;
4143 committer = bline->committer;
4144 smallerthan = strchr(committer, '<');
4145 if (smallerthan && smallerthan[1] != '\0')
4146 committer = smallerthan + 1;
4147 at = strchr(committer, '@');
4148 if (at)
4149 *at = '\0';
4150 len = strlen(committer);
4151 if (len >= 9)
4152 committer[8] = '\0';
4154 nl = strchr(line, '\n');
4155 if (nl)
4156 *nl = '\0';
4157 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4158 bline->id_str, bline->datebuf, committer, line);
4160 a->lineno_cur++;
4161 bline = &a->lines[a->lineno_cur - 1];
4163 done:
4164 if (commit)
4165 got_object_commit_close(commit);
4166 free(line);
4167 return err;
4170 static const struct got_error *
4171 cmd_blame(int argc, char *argv[])
4173 const struct got_error *error;
4174 struct got_repository *repo = NULL;
4175 struct got_worktree *worktree = NULL;
4176 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4177 char *link_target = NULL;
4178 struct got_object_id *obj_id = NULL;
4179 struct got_object_id *commit_id = NULL;
4180 struct got_blob_object *blob = NULL;
4181 char *commit_id_str = NULL;
4182 struct blame_cb_args bca;
4183 int ch, obj_type, i;
4184 size_t filesize;
4186 memset(&bca, 0, sizeof(bca));
4188 #ifndef PROFILE
4189 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4190 NULL) == -1)
4191 err(1, "pledge");
4192 #endif
4194 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4195 switch (ch) {
4196 case 'c':
4197 commit_id_str = optarg;
4198 break;
4199 case 'r':
4200 repo_path = realpath(optarg, NULL);
4201 if (repo_path == NULL)
4202 return got_error_from_errno2("realpath",
4203 optarg);
4204 got_path_strip_trailing_slashes(repo_path);
4205 break;
4206 default:
4207 usage_blame();
4208 /* NOTREACHED */
4212 argc -= optind;
4213 argv += optind;
4215 if (argc == 1)
4216 path = argv[0];
4217 else
4218 usage_blame();
4220 cwd = getcwd(NULL, 0);
4221 if (cwd == NULL) {
4222 error = got_error_from_errno("getcwd");
4223 goto done;
4225 if (repo_path == NULL) {
4226 error = got_worktree_open(&worktree, cwd);
4227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4228 goto done;
4229 else
4230 error = NULL;
4231 if (worktree) {
4232 repo_path =
4233 strdup(got_worktree_get_repo_path(worktree));
4234 if (repo_path == NULL) {
4235 error = got_error_from_errno("strdup");
4236 if (error)
4237 goto done;
4239 } else {
4240 repo_path = strdup(cwd);
4241 if (repo_path == NULL) {
4242 error = got_error_from_errno("strdup");
4243 goto done;
4248 error = got_repo_open(&repo, repo_path, NULL);
4249 if (error != NULL)
4250 goto done;
4252 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4253 if (error)
4254 goto done;
4256 if (worktree) {
4257 const char *prefix = got_worktree_get_path_prefix(worktree);
4258 char *p, *worktree_subdir = cwd +
4259 strlen(got_worktree_get_root_path(worktree));
4260 if (asprintf(&p, "%s%s%s%s%s",
4261 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4262 worktree_subdir, worktree_subdir[0] ? "/" : "",
4263 path) == -1) {
4264 error = got_error_from_errno("asprintf");
4265 goto done;
4267 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4268 free(p);
4269 } else {
4270 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4272 if (error)
4273 goto done;
4275 if (commit_id_str == NULL) {
4276 struct got_reference *head_ref;
4277 error = got_ref_open(&head_ref, repo, worktree ?
4278 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4279 if (error != NULL)
4280 goto done;
4281 error = got_ref_resolve(&commit_id, repo, head_ref);
4282 got_ref_close(head_ref);
4283 if (error != NULL)
4284 goto done;
4285 } else {
4286 error = got_repo_match_object_id(&commit_id, NULL,
4287 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4288 if (error)
4289 goto done;
4292 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4293 commit_id, repo);
4294 if (error)
4295 goto done;
4297 error = got_object_id_by_path(&obj_id, repo, commit_id,
4298 link_target ? link_target : in_repo_path);
4299 if (error)
4300 goto done;
4302 error = got_object_get_type(&obj_type, repo, obj_id);
4303 if (error)
4304 goto done;
4306 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4307 error = got_error_path(link_target ? link_target : in_repo_path,
4308 GOT_ERR_OBJ_TYPE);
4309 goto done;
4312 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4313 if (error)
4314 goto done;
4315 bca.f = got_opentemp();
4316 if (bca.f == NULL) {
4317 error = got_error_from_errno("got_opentemp");
4318 goto done;
4320 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4321 &bca.line_offsets, bca.f, blob);
4322 if (error || bca.nlines == 0)
4323 goto done;
4325 /* Don't include \n at EOF in the blame line count. */
4326 if (bca.line_offsets[bca.nlines - 1] == filesize)
4327 bca.nlines--;
4329 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4330 if (bca.lines == NULL) {
4331 error = got_error_from_errno("calloc");
4332 goto done;
4334 bca.lineno_cur = 1;
4335 bca.nlines_prec = 0;
4336 i = bca.nlines;
4337 while (i > 0) {
4338 i /= 10;
4339 bca.nlines_prec++;
4341 bca.repo = repo;
4343 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4344 repo, blame_cb, &bca, check_cancelled, NULL);
4345 done:
4346 free(in_repo_path);
4347 free(link_target);
4348 free(repo_path);
4349 free(cwd);
4350 free(commit_id);
4351 free(obj_id);
4352 if (blob)
4353 got_object_blob_close(blob);
4354 if (worktree)
4355 got_worktree_close(worktree);
4356 if (repo) {
4357 const struct got_error *repo_error;
4358 repo_error = got_repo_close(repo);
4359 if (error == NULL)
4360 error = repo_error;
4362 if (bca.lines) {
4363 for (i = 0; i < bca.nlines; i++) {
4364 struct blame_line *bline = &bca.lines[i];
4365 free(bline->id_str);
4366 free(bline->committer);
4368 free(bca.lines);
4370 free(bca.line_offsets);
4371 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4372 error = got_error_from_errno("fclose");
4373 return error;
4376 __dead static void
4377 usage_tree(void)
4379 fprintf(stderr,
4380 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4381 getprogname());
4382 exit(1);
4385 static const struct got_error *
4386 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4387 const char *root_path, struct got_repository *repo)
4389 const struct got_error *err = NULL;
4390 int is_root_path = (strcmp(path, root_path) == 0);
4391 const char *modestr = "";
4392 mode_t mode = got_tree_entry_get_mode(te);
4393 char *link_target = NULL;
4395 path += strlen(root_path);
4396 while (path[0] == '/')
4397 path++;
4399 if (got_object_tree_entry_is_submodule(te))
4400 modestr = "$";
4401 else if (S_ISLNK(mode)) {
4402 int i;
4404 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4405 if (err)
4406 return err;
4407 for (i = 0; i < strlen(link_target); i++) {
4408 if (!isprint((unsigned char)link_target[i]))
4409 link_target[i] = '?';
4412 modestr = "@";
4414 else if (S_ISDIR(mode))
4415 modestr = "/";
4416 else if (mode & S_IXUSR)
4417 modestr = "*";
4419 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4420 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4421 link_target ? " -> ": "", link_target ? link_target : "");
4423 free(link_target);
4424 return NULL;
4427 static const struct got_error *
4428 print_tree(const char *path, struct got_object_id *commit_id,
4429 int show_ids, int recurse, const char *root_path,
4430 struct got_repository *repo)
4432 const struct got_error *err = NULL;
4433 struct got_object_id *tree_id = NULL;
4434 struct got_tree_object *tree = NULL;
4435 int nentries, i;
4437 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4438 if (err)
4439 goto done;
4441 err = got_object_open_as_tree(&tree, repo, tree_id);
4442 if (err)
4443 goto done;
4444 nentries = got_object_tree_get_nentries(tree);
4445 for (i = 0; i < nentries; i++) {
4446 struct got_tree_entry *te;
4447 char *id = NULL;
4449 if (sigint_received || sigpipe_received)
4450 break;
4452 te = got_object_tree_get_entry(tree, i);
4453 if (show_ids) {
4454 char *id_str;
4455 err = got_object_id_str(&id_str,
4456 got_tree_entry_get_id(te));
4457 if (err)
4458 goto done;
4459 if (asprintf(&id, "%s ", id_str) == -1) {
4460 err = got_error_from_errno("asprintf");
4461 free(id_str);
4462 goto done;
4464 free(id_str);
4466 err = print_entry(te, id, path, root_path, repo);
4467 free(id);
4468 if (err)
4469 goto done;
4471 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4472 char *child_path;
4473 if (asprintf(&child_path, "%s%s%s", path,
4474 path[0] == '/' && path[1] == '\0' ? "" : "/",
4475 got_tree_entry_get_name(te)) == -1) {
4476 err = got_error_from_errno("asprintf");
4477 goto done;
4479 err = print_tree(child_path, commit_id, show_ids, 1,
4480 root_path, repo);
4481 free(child_path);
4482 if (err)
4483 goto done;
4486 done:
4487 if (tree)
4488 got_object_tree_close(tree);
4489 free(tree_id);
4490 return err;
4493 static const struct got_error *
4494 cmd_tree(int argc, char *argv[])
4496 const struct got_error *error;
4497 struct got_repository *repo = NULL;
4498 struct got_worktree *worktree = NULL;
4499 const char *path, *refname = NULL;
4500 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4501 struct got_object_id *commit_id = NULL;
4502 char *commit_id_str = NULL;
4503 int show_ids = 0, recurse = 0;
4504 int ch;
4506 #ifndef PROFILE
4507 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4508 NULL) == -1)
4509 err(1, "pledge");
4510 #endif
4512 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4513 switch (ch) {
4514 case 'c':
4515 commit_id_str = optarg;
4516 break;
4517 case 'r':
4518 repo_path = realpath(optarg, NULL);
4519 if (repo_path == NULL)
4520 return got_error_from_errno2("realpath",
4521 optarg);
4522 got_path_strip_trailing_slashes(repo_path);
4523 break;
4524 case 'i':
4525 show_ids = 1;
4526 break;
4527 case 'R':
4528 recurse = 1;
4529 break;
4530 default:
4531 usage_tree();
4532 /* NOTREACHED */
4536 argc -= optind;
4537 argv += optind;
4539 if (argc == 1)
4540 path = argv[0];
4541 else if (argc > 1)
4542 usage_tree();
4543 else
4544 path = NULL;
4546 cwd = getcwd(NULL, 0);
4547 if (cwd == NULL) {
4548 error = got_error_from_errno("getcwd");
4549 goto done;
4551 if (repo_path == NULL) {
4552 error = got_worktree_open(&worktree, cwd);
4553 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4554 goto done;
4555 else
4556 error = NULL;
4557 if (worktree) {
4558 repo_path =
4559 strdup(got_worktree_get_repo_path(worktree));
4560 if (repo_path == NULL)
4561 error = got_error_from_errno("strdup");
4562 if (error)
4563 goto done;
4564 } else {
4565 repo_path = strdup(cwd);
4566 if (repo_path == NULL) {
4567 error = got_error_from_errno("strdup");
4568 goto done;
4573 error = got_repo_open(&repo, repo_path, NULL);
4574 if (error != NULL)
4575 goto done;
4577 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4578 if (error)
4579 goto done;
4581 if (path == NULL) {
4582 if (worktree) {
4583 char *p, *worktree_subdir = cwd +
4584 strlen(got_worktree_get_root_path(worktree));
4585 if (asprintf(&p, "%s/%s",
4586 got_worktree_get_path_prefix(worktree),
4587 worktree_subdir) == -1) {
4588 error = got_error_from_errno("asprintf");
4589 goto done;
4591 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4592 free(p);
4593 if (error)
4594 goto done;
4595 } else
4596 path = "/";
4598 if (in_repo_path == NULL) {
4599 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4600 if (error != NULL)
4601 goto done;
4604 if (commit_id_str == NULL) {
4605 struct got_reference *head_ref;
4606 if (worktree)
4607 refname = got_worktree_get_head_ref_name(worktree);
4608 else
4609 refname = GOT_REF_HEAD;
4610 error = got_ref_open(&head_ref, repo, refname, 0);
4611 if (error != NULL)
4612 goto done;
4613 error = got_ref_resolve(&commit_id, repo, head_ref);
4614 got_ref_close(head_ref);
4615 if (error != NULL)
4616 goto done;
4617 } else {
4618 error = got_repo_match_object_id(&commit_id, NULL,
4619 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4620 if (error)
4621 goto done;
4624 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4625 in_repo_path, repo);
4626 done:
4627 free(in_repo_path);
4628 free(repo_path);
4629 free(cwd);
4630 free(commit_id);
4631 if (worktree)
4632 got_worktree_close(worktree);
4633 if (repo) {
4634 const struct got_error *repo_error;
4635 repo_error = got_repo_close(repo);
4636 if (error == NULL)
4637 error = repo_error;
4639 return error;
4642 __dead static void
4643 usage_status(void)
4645 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4646 getprogname());
4647 exit(1);
4650 static const struct got_error *
4651 print_status(void *arg, unsigned char status, unsigned char staged_status,
4652 const char *path, struct got_object_id *blob_id,
4653 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4654 int dirfd, const char *de_name)
4656 if (status == staged_status && (status == GOT_STATUS_DELETE))
4657 status = GOT_STATUS_NO_CHANGE;
4658 if (arg) {
4659 char *status_codes = arg;
4660 size_t ncodes = strlen(status_codes);
4661 int i;
4662 for (i = 0; i < ncodes ; i++) {
4663 if (status == status_codes[i] ||
4664 staged_status == status_codes[i])
4665 break;
4667 if (i == ncodes)
4668 return NULL;
4670 printf("%c%c %s\n", status, staged_status, path);
4671 return NULL;
4674 static const struct got_error *
4675 cmd_status(int argc, char *argv[])
4677 const struct got_error *error = NULL;
4678 struct got_repository *repo = NULL;
4679 struct got_worktree *worktree = NULL;
4680 char *cwd = NULL, *status_codes = NULL;;
4681 struct got_pathlist_head paths;
4682 struct got_pathlist_entry *pe;
4683 int ch, i;
4685 TAILQ_INIT(&paths);
4687 while ((ch = getopt(argc, argv, "s:")) != -1) {
4688 switch (ch) {
4689 case 's':
4690 for (i = 0; i < strlen(optarg); i++) {
4691 switch (optarg[i]) {
4692 case GOT_STATUS_MODIFY:
4693 case GOT_STATUS_ADD:
4694 case GOT_STATUS_DELETE:
4695 case GOT_STATUS_CONFLICT:
4696 case GOT_STATUS_MISSING:
4697 case GOT_STATUS_OBSTRUCTED:
4698 case GOT_STATUS_UNVERSIONED:
4699 case GOT_STATUS_MODE_CHANGE:
4700 case GOT_STATUS_NONEXISTENT:
4701 break;
4702 default:
4703 errx(1, "invalid status code '%c'",
4704 optarg[i]);
4707 status_codes = optarg;
4708 break;
4709 default:
4710 usage_status();
4711 /* NOTREACHED */
4715 argc -= optind;
4716 argv += optind;
4718 #ifndef PROFILE
4719 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4720 NULL) == -1)
4721 err(1, "pledge");
4722 #endif
4723 cwd = getcwd(NULL, 0);
4724 if (cwd == NULL) {
4725 error = got_error_from_errno("getcwd");
4726 goto done;
4729 error = got_worktree_open(&worktree, cwd);
4730 if (error) {
4731 if (error->code == GOT_ERR_NOT_WORKTREE)
4732 error = wrap_not_worktree_error(error, "status", cwd);
4733 goto done;
4736 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4737 NULL);
4738 if (error != NULL)
4739 goto done;
4741 error = apply_unveil(got_repo_get_path(repo), 1,
4742 got_worktree_get_root_path(worktree));
4743 if (error)
4744 goto done;
4746 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4747 if (error)
4748 goto done;
4750 error = got_worktree_status(worktree, &paths, repo, print_status,
4751 status_codes, check_cancelled, NULL);
4752 done:
4753 TAILQ_FOREACH(pe, &paths, entry)
4754 free((char *)pe->path);
4755 got_pathlist_free(&paths);
4756 free(cwd);
4757 return error;
4760 __dead static void
4761 usage_ref(void)
4763 fprintf(stderr,
4764 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4765 "[-d] [name]\n",
4766 getprogname());
4767 exit(1);
4770 static const struct got_error *
4771 list_refs(struct got_repository *repo, const char *refname)
4773 static const struct got_error *err = NULL;
4774 struct got_reflist_head refs;
4775 struct got_reflist_entry *re;
4777 SIMPLEQ_INIT(&refs);
4778 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4779 if (err)
4780 return err;
4782 SIMPLEQ_FOREACH(re, &refs, entry) {
4783 char *refstr;
4784 refstr = got_ref_to_str(re->ref);
4785 if (refstr == NULL)
4786 return got_error_from_errno("got_ref_to_str");
4787 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4788 free(refstr);
4791 got_ref_list_free(&refs);
4792 return NULL;
4795 static const struct got_error *
4796 delete_ref(struct got_repository *repo, const char *refname)
4798 const struct got_error *err = NULL;
4799 struct got_reference *ref;
4801 err = got_ref_open(&ref, repo, refname, 0);
4802 if (err)
4803 return err;
4805 err = got_ref_delete(ref, repo);
4806 got_ref_close(ref);
4807 return err;
4810 static const struct got_error *
4811 add_ref(struct got_repository *repo, const char *refname, const char *target)
4813 const struct got_error *err = NULL;
4814 struct got_object_id *id;
4815 struct got_reference *ref = NULL;
4818 * Don't let the user create a reference name with a leading '-'.
4819 * While technically a valid reference name, this case is usually
4820 * an unintended typo.
4822 if (refname[0] == '-')
4823 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4825 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4826 repo);
4827 if (err) {
4828 struct got_reference *target_ref;
4830 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4831 return err;
4832 err = got_ref_open(&target_ref, repo, target, 0);
4833 if (err)
4834 return err;
4835 err = got_ref_resolve(&id, repo, target_ref);
4836 got_ref_close(target_ref);
4837 if (err)
4838 return err;
4841 err = got_ref_alloc(&ref, refname, id);
4842 if (err)
4843 goto done;
4845 err = got_ref_write(ref, repo);
4846 done:
4847 if (ref)
4848 got_ref_close(ref);
4849 free(id);
4850 return err;
4853 static const struct got_error *
4854 add_symref(struct got_repository *repo, const char *refname, const char *target)
4856 const struct got_error *err = NULL;
4857 struct got_reference *ref = NULL;
4858 struct got_reference *target_ref = NULL;
4861 * Don't let the user create a reference name with a leading '-'.
4862 * While technically a valid reference name, this case is usually
4863 * an unintended typo.
4865 if (refname[0] == '-')
4866 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4868 err = got_ref_open(&target_ref, repo, target, 0);
4869 if (err)
4870 return err;
4872 err = got_ref_alloc_symref(&ref, refname, target_ref);
4873 if (err)
4874 goto done;
4876 err = got_ref_write(ref, repo);
4877 done:
4878 if (target_ref)
4879 got_ref_close(target_ref);
4880 if (ref)
4881 got_ref_close(ref);
4882 return err;
4885 static const struct got_error *
4886 cmd_ref(int argc, char *argv[])
4888 const struct got_error *error = NULL;
4889 struct got_repository *repo = NULL;
4890 struct got_worktree *worktree = NULL;
4891 char *cwd = NULL, *repo_path = NULL;
4892 int ch, do_list = 0, do_delete = 0;
4893 const char *obj_arg = NULL, *symref_target= NULL;
4894 char *refname = NULL;
4896 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4897 switch (ch) {
4898 case 'c':
4899 obj_arg = optarg;
4900 break;
4901 case 'd':
4902 do_delete = 1;
4903 break;
4904 case 'r':
4905 repo_path = realpath(optarg, NULL);
4906 if (repo_path == NULL)
4907 return got_error_from_errno2("realpath",
4908 optarg);
4909 got_path_strip_trailing_slashes(repo_path);
4910 break;
4911 case 'l':
4912 do_list = 1;
4913 break;
4914 case 's':
4915 symref_target = optarg;
4916 break;
4917 default:
4918 usage_ref();
4919 /* NOTREACHED */
4923 if (obj_arg && do_list)
4924 errx(1, "-c and -l options are mutually exclusive");
4925 if (obj_arg && do_delete)
4926 errx(1, "-c and -d options are mutually exclusive");
4927 if (obj_arg && symref_target)
4928 errx(1, "-c and -s options are mutually exclusive");
4929 if (symref_target && do_delete)
4930 errx(1, "-s and -d options are mutually exclusive");
4931 if (symref_target && do_list)
4932 errx(1, "-s and -l options are mutually exclusive");
4933 if (do_delete && do_list)
4934 errx(1, "-d and -l options are mutually exclusive");
4936 argc -= optind;
4937 argv += optind;
4939 if (do_list) {
4940 if (argc != 0 && argc != 1)
4941 usage_ref();
4942 if (argc == 1) {
4943 refname = strdup(argv[0]);
4944 if (refname == NULL) {
4945 error = got_error_from_errno("strdup");
4946 goto done;
4949 } else {
4950 if (argc != 1)
4951 usage_ref();
4952 refname = strdup(argv[0]);
4953 if (refname == NULL) {
4954 error = got_error_from_errno("strdup");
4955 goto done;
4959 if (refname)
4960 got_path_strip_trailing_slashes(refname);
4962 #ifndef PROFILE
4963 if (do_list) {
4964 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4965 NULL) == -1)
4966 err(1, "pledge");
4967 } else {
4968 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4969 "sendfd unveil", NULL) == -1)
4970 err(1, "pledge");
4972 #endif
4973 cwd = getcwd(NULL, 0);
4974 if (cwd == NULL) {
4975 error = got_error_from_errno("getcwd");
4976 goto done;
4979 if (repo_path == NULL) {
4980 error = got_worktree_open(&worktree, cwd);
4981 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4982 goto done;
4983 else
4984 error = NULL;
4985 if (worktree) {
4986 repo_path =
4987 strdup(got_worktree_get_repo_path(worktree));
4988 if (repo_path == NULL)
4989 error = got_error_from_errno("strdup");
4990 if (error)
4991 goto done;
4992 } else {
4993 repo_path = strdup(cwd);
4994 if (repo_path == NULL) {
4995 error = got_error_from_errno("strdup");
4996 goto done;
5001 error = got_repo_open(&repo, repo_path, NULL);
5002 if (error != NULL)
5003 goto done;
5005 error = apply_unveil(got_repo_get_path(repo), do_list,
5006 worktree ? got_worktree_get_root_path(worktree) : NULL);
5007 if (error)
5008 goto done;
5010 if (do_list)
5011 error = list_refs(repo, refname);
5012 else if (do_delete)
5013 error = delete_ref(repo, refname);
5014 else if (symref_target)
5015 error = add_symref(repo, refname, symref_target);
5016 else {
5017 if (obj_arg == NULL)
5018 usage_ref();
5019 error = add_ref(repo, refname, obj_arg);
5021 done:
5022 free(refname);
5023 if (repo)
5024 got_repo_close(repo);
5025 if (worktree)
5026 got_worktree_close(worktree);
5027 free(cwd);
5028 free(repo_path);
5029 return error;
5032 __dead static void
5033 usage_branch(void)
5035 fprintf(stderr,
5036 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5037 "[name]\n", getprogname());
5038 exit(1);
5041 static const struct got_error *
5042 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5043 struct got_reference *ref)
5045 const struct got_error *err = NULL;
5046 const char *refname, *marker = " ";
5047 char *refstr;
5049 refname = got_ref_get_name(ref);
5050 if (worktree && strcmp(refname,
5051 got_worktree_get_head_ref_name(worktree)) == 0) {
5052 struct got_object_id *id = NULL;
5054 err = got_ref_resolve(&id, repo, ref);
5055 if (err)
5056 return err;
5057 if (got_object_id_cmp(id,
5058 got_worktree_get_base_commit_id(worktree)) == 0)
5059 marker = "* ";
5060 else
5061 marker = "~ ";
5062 free(id);
5065 if (strncmp(refname, "refs/heads/", 11) == 0)
5066 refname += 11;
5067 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5068 refname += 18;
5070 refstr = got_ref_to_str(ref);
5071 if (refstr == NULL)
5072 return got_error_from_errno("got_ref_to_str");
5074 printf("%s%s: %s\n", marker, refname, refstr);
5075 free(refstr);
5076 return NULL;
5079 static const struct got_error *
5080 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5082 const char *refname;
5084 if (worktree == NULL)
5085 return got_error(GOT_ERR_NOT_WORKTREE);
5087 refname = got_worktree_get_head_ref_name(worktree);
5089 if (strncmp(refname, "refs/heads/", 11) == 0)
5090 refname += 11;
5091 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5092 refname += 18;
5094 printf("%s\n", refname);
5096 return NULL;
5099 static const struct got_error *
5100 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5102 static const struct got_error *err = NULL;
5103 struct got_reflist_head refs;
5104 struct got_reflist_entry *re;
5105 struct got_reference *temp_ref = NULL;
5106 int rebase_in_progress, histedit_in_progress;
5108 SIMPLEQ_INIT(&refs);
5110 if (worktree) {
5111 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5112 worktree);
5113 if (err)
5114 return err;
5116 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5117 worktree);
5118 if (err)
5119 return err;
5121 if (rebase_in_progress || histedit_in_progress) {
5122 err = got_ref_open(&temp_ref, repo,
5123 got_worktree_get_head_ref_name(worktree), 0);
5124 if (err)
5125 return err;
5126 list_branch(repo, worktree, temp_ref);
5127 got_ref_close(temp_ref);
5131 err = got_ref_list(&refs, repo, "refs/heads",
5132 got_ref_cmp_by_name, NULL);
5133 if (err)
5134 return err;
5136 SIMPLEQ_FOREACH(re, &refs, entry)
5137 list_branch(repo, worktree, re->ref);
5139 got_ref_list_free(&refs);
5140 return NULL;
5143 static const struct got_error *
5144 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5145 const char *branch_name)
5147 const struct got_error *err = NULL;
5148 struct got_reference *ref = NULL;
5149 char *refname;
5151 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5152 return got_error_from_errno("asprintf");
5154 err = got_ref_open(&ref, repo, refname, 0);
5155 if (err)
5156 goto done;
5158 if (worktree &&
5159 strcmp(got_worktree_get_head_ref_name(worktree),
5160 got_ref_get_name(ref)) == 0) {
5161 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5162 "will not delete this work tree's current branch");
5163 goto done;
5166 err = got_ref_delete(ref, repo);
5167 done:
5168 if (ref)
5169 got_ref_close(ref);
5170 free(refname);
5171 return err;
5174 static const struct got_error *
5175 add_branch(struct got_repository *repo, const char *branch_name,
5176 struct got_object_id *base_commit_id)
5178 const struct got_error *err = NULL;
5179 struct got_reference *ref = NULL;
5180 char *base_refname = NULL, *refname = NULL;
5183 * Don't let the user create a branch name with a leading '-'.
5184 * While technically a valid reference name, this case is usually
5185 * an unintended typo.
5187 if (branch_name[0] == '-')
5188 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5190 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5191 err = got_error_from_errno("asprintf");
5192 goto done;
5195 err = got_ref_open(&ref, repo, refname, 0);
5196 if (err == NULL) {
5197 err = got_error(GOT_ERR_BRANCH_EXISTS);
5198 goto done;
5199 } else if (err->code != GOT_ERR_NOT_REF)
5200 goto done;
5202 err = got_ref_alloc(&ref, refname, base_commit_id);
5203 if (err)
5204 goto done;
5206 err = got_ref_write(ref, repo);
5207 done:
5208 if (ref)
5209 got_ref_close(ref);
5210 free(base_refname);
5211 free(refname);
5212 return err;
5215 static const struct got_error *
5216 cmd_branch(int argc, char *argv[])
5218 const struct got_error *error = NULL;
5219 struct got_repository *repo = NULL;
5220 struct got_worktree *worktree = NULL;
5221 char *cwd = NULL, *repo_path = NULL;
5222 int ch, do_list = 0, do_show = 0, do_update = 1;
5223 const char *delref = NULL, *commit_id_arg = NULL;
5224 struct got_reference *ref = NULL;
5225 struct got_pathlist_head paths;
5226 struct got_pathlist_entry *pe;
5227 struct got_object_id *commit_id = NULL;
5228 char *commit_id_str = NULL;
5230 TAILQ_INIT(&paths);
5232 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5233 switch (ch) {
5234 case 'c':
5235 commit_id_arg = optarg;
5236 break;
5237 case 'd':
5238 delref = optarg;
5239 break;
5240 case 'r':
5241 repo_path = realpath(optarg, NULL);
5242 if (repo_path == NULL)
5243 return got_error_from_errno2("realpath",
5244 optarg);
5245 got_path_strip_trailing_slashes(repo_path);
5246 break;
5247 case 'l':
5248 do_list = 1;
5249 break;
5250 case 'n':
5251 do_update = 0;
5252 break;
5253 default:
5254 usage_branch();
5255 /* NOTREACHED */
5259 if (do_list && delref)
5260 errx(1, "-l and -d options are mutually exclusive");
5262 argc -= optind;
5263 argv += optind;
5265 if (!do_list && !delref && argc == 0)
5266 do_show = 1;
5268 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5269 errx(1, "-c option can only be used when creating a branch");
5271 if (do_list || delref) {
5272 if (argc > 0)
5273 usage_branch();
5274 } else if (!do_show && argc != 1)
5275 usage_branch();
5277 #ifndef PROFILE
5278 if (do_list || do_show) {
5279 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5280 NULL) == -1)
5281 err(1, "pledge");
5282 } else {
5283 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5284 "sendfd unveil", NULL) == -1)
5285 err(1, "pledge");
5287 #endif
5288 cwd = getcwd(NULL, 0);
5289 if (cwd == NULL) {
5290 error = got_error_from_errno("getcwd");
5291 goto done;
5294 if (repo_path == NULL) {
5295 error = got_worktree_open(&worktree, cwd);
5296 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5297 goto done;
5298 else
5299 error = NULL;
5300 if (worktree) {
5301 repo_path =
5302 strdup(got_worktree_get_repo_path(worktree));
5303 if (repo_path == NULL)
5304 error = got_error_from_errno("strdup");
5305 if (error)
5306 goto done;
5307 } else {
5308 repo_path = strdup(cwd);
5309 if (repo_path == NULL) {
5310 error = got_error_from_errno("strdup");
5311 goto done;
5316 error = got_repo_open(&repo, repo_path, NULL);
5317 if (error != NULL)
5318 goto done;
5320 error = apply_unveil(got_repo_get_path(repo), do_list,
5321 worktree ? got_worktree_get_root_path(worktree) : NULL);
5322 if (error)
5323 goto done;
5325 if (do_show)
5326 error = show_current_branch(repo, worktree);
5327 else if (do_list)
5328 error = list_branches(repo, worktree);
5329 else if (delref)
5330 error = delete_branch(repo, worktree, delref);
5331 else {
5332 if (commit_id_arg == NULL)
5333 commit_id_arg = worktree ?
5334 got_worktree_get_head_ref_name(worktree) :
5335 GOT_REF_HEAD;
5336 error = got_repo_match_object_id(&commit_id, NULL,
5337 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5338 if (error)
5339 goto done;
5340 error = add_branch(repo, argv[0], commit_id);
5341 if (error)
5342 goto done;
5343 if (worktree && do_update) {
5344 struct got_update_progress_arg upa;
5345 char *branch_refname = NULL;
5347 error = got_object_id_str(&commit_id_str, commit_id);
5348 if (error)
5349 goto done;
5350 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5351 worktree);
5352 if (error)
5353 goto done;
5354 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5355 == -1) {
5356 error = got_error_from_errno("asprintf");
5357 goto done;
5359 error = got_ref_open(&ref, repo, branch_refname, 0);
5360 free(branch_refname);
5361 if (error)
5362 goto done;
5363 error = switch_head_ref(ref, commit_id, worktree,
5364 repo);
5365 if (error)
5366 goto done;
5367 error = got_worktree_set_base_commit_id(worktree, repo,
5368 commit_id);
5369 if (error)
5370 goto done;
5371 memset(&upa, 0, sizeof(upa));
5372 error = got_worktree_checkout_files(worktree, &paths,
5373 repo, update_progress, &upa, check_cancelled,
5374 NULL);
5375 if (error)
5376 goto done;
5377 if (upa.did_something)
5378 printf("Updated to commit %s\n", commit_id_str);
5379 print_update_progress_stats(&upa);
5382 done:
5383 if (ref)
5384 got_ref_close(ref);
5385 if (repo)
5386 got_repo_close(repo);
5387 if (worktree)
5388 got_worktree_close(worktree);
5389 free(cwd);
5390 free(repo_path);
5391 free(commit_id);
5392 free(commit_id_str);
5393 TAILQ_FOREACH(pe, &paths, entry)
5394 free((char *)pe->path);
5395 got_pathlist_free(&paths);
5396 return error;
5400 __dead static void
5401 usage_tag(void)
5403 fprintf(stderr,
5404 "usage: %s tag [-c commit] [-r repository] [-l] "
5405 "[-m message] name\n", getprogname());
5406 exit(1);
5409 #if 0
5410 static const struct got_error *
5411 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5413 const struct got_error *err = NULL;
5414 struct got_reflist_entry *re, *se, *new;
5415 struct got_object_id *re_id, *se_id;
5416 struct got_tag_object *re_tag, *se_tag;
5417 time_t re_time, se_time;
5419 SIMPLEQ_FOREACH(re, tags, entry) {
5420 se = SIMPLEQ_FIRST(sorted);
5421 if (se == NULL) {
5422 err = got_reflist_entry_dup(&new, re);
5423 if (err)
5424 return err;
5425 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5426 continue;
5427 } else {
5428 err = got_ref_resolve(&re_id, repo, re->ref);
5429 if (err)
5430 break;
5431 err = got_object_open_as_tag(&re_tag, repo, re_id);
5432 free(re_id);
5433 if (err)
5434 break;
5435 re_time = got_object_tag_get_tagger_time(re_tag);
5436 got_object_tag_close(re_tag);
5439 while (se) {
5440 err = got_ref_resolve(&se_id, repo, re->ref);
5441 if (err)
5442 break;
5443 err = got_object_open_as_tag(&se_tag, repo, se_id);
5444 free(se_id);
5445 if (err)
5446 break;
5447 se_time = got_object_tag_get_tagger_time(se_tag);
5448 got_object_tag_close(se_tag);
5450 if (se_time > re_time) {
5451 err = got_reflist_entry_dup(&new, re);
5452 if (err)
5453 return err;
5454 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5455 break;
5457 se = SIMPLEQ_NEXT(se, entry);
5458 continue;
5461 done:
5462 return err;
5464 #endif
5466 static const struct got_error *
5467 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5469 static const struct got_error *err = NULL;
5470 struct got_reflist_head refs;
5471 struct got_reflist_entry *re;
5473 SIMPLEQ_INIT(&refs);
5475 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5476 if (err)
5477 return err;
5479 SIMPLEQ_FOREACH(re, &refs, entry) {
5480 const char *refname;
5481 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5482 char datebuf[26];
5483 const char *tagger;
5484 time_t tagger_time;
5485 struct got_object_id *id;
5486 struct got_tag_object *tag;
5487 struct got_commit_object *commit = NULL;
5489 refname = got_ref_get_name(re->ref);
5490 if (strncmp(refname, "refs/tags/", 10) != 0)
5491 continue;
5492 refname += 10;
5493 refstr = got_ref_to_str(re->ref);
5494 if (refstr == NULL) {
5495 err = got_error_from_errno("got_ref_to_str");
5496 break;
5498 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5499 free(refstr);
5501 err = got_ref_resolve(&id, repo, re->ref);
5502 if (err)
5503 break;
5504 err = got_object_open_as_tag(&tag, repo, id);
5505 if (err) {
5506 if (err->code != GOT_ERR_OBJ_TYPE) {
5507 free(id);
5508 break;
5510 /* "lightweight" tag */
5511 err = got_object_open_as_commit(&commit, repo, id);
5512 if (err) {
5513 free(id);
5514 break;
5516 tagger = got_object_commit_get_committer(commit);
5517 tagger_time =
5518 got_object_commit_get_committer_time(commit);
5519 err = got_object_id_str(&id_str, id);
5520 free(id);
5521 if (err)
5522 break;
5523 } else {
5524 free(id);
5525 tagger = got_object_tag_get_tagger(tag);
5526 tagger_time = got_object_tag_get_tagger_time(tag);
5527 err = got_object_id_str(&id_str,
5528 got_object_tag_get_object_id(tag));
5529 if (err)
5530 break;
5532 printf("from: %s\n", tagger);
5533 datestr = get_datestr(&tagger_time, datebuf);
5534 if (datestr)
5535 printf("date: %s UTC\n", datestr);
5536 if (commit)
5537 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5538 else {
5539 switch (got_object_tag_get_object_type(tag)) {
5540 case GOT_OBJ_TYPE_BLOB:
5541 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5542 id_str);
5543 break;
5544 case GOT_OBJ_TYPE_TREE:
5545 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5546 id_str);
5547 break;
5548 case GOT_OBJ_TYPE_COMMIT:
5549 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5550 id_str);
5551 break;
5552 case GOT_OBJ_TYPE_TAG:
5553 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5554 id_str);
5555 break;
5556 default:
5557 break;
5560 free(id_str);
5561 if (commit) {
5562 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5563 if (err)
5564 break;
5565 got_object_commit_close(commit);
5566 } else {
5567 tagmsg0 = strdup(got_object_tag_get_message(tag));
5568 got_object_tag_close(tag);
5569 if (tagmsg0 == NULL) {
5570 err = got_error_from_errno("strdup");
5571 break;
5575 tagmsg = tagmsg0;
5576 do {
5577 line = strsep(&tagmsg, "\n");
5578 if (line)
5579 printf(" %s\n", line);
5580 } while (line);
5581 free(tagmsg0);
5584 got_ref_list_free(&refs);
5585 return NULL;
5588 static const struct got_error *
5589 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5590 const char *tag_name, const char *repo_path)
5592 const struct got_error *err = NULL;
5593 char *template = NULL, *initial_content = NULL;
5594 char *editor = NULL;
5595 int fd = -1;
5597 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5598 err = got_error_from_errno("asprintf");
5599 goto done;
5602 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5603 commit_id_str, tag_name) == -1) {
5604 err = got_error_from_errno("asprintf");
5605 goto done;
5608 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5609 if (err)
5610 goto done;
5612 dprintf(fd, initial_content);
5613 close(fd);
5615 err = get_editor(&editor);
5616 if (err)
5617 goto done;
5618 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5619 done:
5620 free(initial_content);
5621 free(template);
5622 free(editor);
5624 /* Editor is done; we can now apply unveil(2) */
5625 if (err == NULL) {
5626 err = apply_unveil(repo_path, 0, NULL);
5627 if (err) {
5628 free(*tagmsg);
5629 *tagmsg = NULL;
5632 return err;
5635 static const struct got_error *
5636 add_tag(struct got_repository *repo, const char *tag_name,
5637 const char *commit_arg, const char *tagmsg_arg)
5639 const struct got_error *err = NULL;
5640 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5641 char *label = NULL, *commit_id_str = NULL;
5642 struct got_reference *ref = NULL;
5643 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5644 char *tagmsg_path = NULL, *tag_id_str = NULL;
5645 int preserve_tagmsg = 0;
5648 * Don't let the user create a tag name with a leading '-'.
5649 * While technically a valid reference name, this case is usually
5650 * an unintended typo.
5652 if (tag_name[0] == '-')
5653 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5655 err = get_author(&tagger, repo);
5656 if (err)
5657 return err;
5659 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5660 GOT_OBJ_TYPE_COMMIT, 1, repo);
5661 if (err)
5662 goto done;
5664 err = got_object_id_str(&commit_id_str, commit_id);
5665 if (err)
5666 goto done;
5668 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5669 refname = strdup(tag_name);
5670 if (refname == NULL) {
5671 err = got_error_from_errno("strdup");
5672 goto done;
5674 tag_name += 10;
5675 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5676 err = got_error_from_errno("asprintf");
5677 goto done;
5680 err = got_ref_open(&ref, repo, refname, 0);
5681 if (err == NULL) {
5682 err = got_error(GOT_ERR_TAG_EXISTS);
5683 goto done;
5684 } else if (err->code != GOT_ERR_NOT_REF)
5685 goto done;
5687 if (tagmsg_arg == NULL) {
5688 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5689 tag_name, got_repo_get_path(repo));
5690 if (err) {
5691 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5692 tagmsg_path != NULL)
5693 preserve_tagmsg = 1;
5694 goto done;
5698 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5699 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5700 if (err) {
5701 if (tagmsg_path)
5702 preserve_tagmsg = 1;
5703 goto done;
5706 err = got_ref_alloc(&ref, refname, tag_id);
5707 if (err) {
5708 if (tagmsg_path)
5709 preserve_tagmsg = 1;
5710 goto done;
5713 err = got_ref_write(ref, repo);
5714 if (err) {
5715 if (tagmsg_path)
5716 preserve_tagmsg = 1;
5717 goto done;
5720 err = got_object_id_str(&tag_id_str, tag_id);
5721 if (err) {
5722 if (tagmsg_path)
5723 preserve_tagmsg = 1;
5724 goto done;
5726 printf("Created tag %s\n", tag_id_str);
5727 done:
5728 if (preserve_tagmsg) {
5729 fprintf(stderr, "%s: tag message preserved in %s\n",
5730 getprogname(), tagmsg_path);
5731 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5732 err = got_error_from_errno2("unlink", tagmsg_path);
5733 free(tag_id_str);
5734 if (ref)
5735 got_ref_close(ref);
5736 free(commit_id);
5737 free(commit_id_str);
5738 free(refname);
5739 free(tagmsg);
5740 free(tagmsg_path);
5741 free(tagger);
5742 return err;
5745 static const struct got_error *
5746 cmd_tag(int argc, char *argv[])
5748 const struct got_error *error = NULL;
5749 struct got_repository *repo = NULL;
5750 struct got_worktree *worktree = NULL;
5751 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5752 char *gitconfig_path = NULL;
5753 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5754 int ch, do_list = 0;
5756 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5757 switch (ch) {
5758 case 'c':
5759 commit_id_arg = optarg;
5760 break;
5761 case 'm':
5762 tagmsg = optarg;
5763 break;
5764 case 'r':
5765 repo_path = realpath(optarg, NULL);
5766 if (repo_path == NULL)
5767 return got_error_from_errno2("realpath",
5768 optarg);
5769 got_path_strip_trailing_slashes(repo_path);
5770 break;
5771 case 'l':
5772 do_list = 1;
5773 break;
5774 default:
5775 usage_tag();
5776 /* NOTREACHED */
5780 argc -= optind;
5781 argv += optind;
5783 if (do_list) {
5784 if (commit_id_arg != NULL)
5785 errx(1,
5786 "-c option can only be used when creating a tag");
5787 if (tagmsg)
5788 errx(1, "-l and -m options are mutually exclusive");
5789 if (argc > 0)
5790 usage_tag();
5791 } else if (argc != 1)
5792 usage_tag();
5794 tag_name = argv[0];
5796 #ifndef PROFILE
5797 if (do_list) {
5798 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5799 NULL) == -1)
5800 err(1, "pledge");
5801 } else {
5802 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5803 "sendfd unveil", NULL) == -1)
5804 err(1, "pledge");
5806 #endif
5807 cwd = getcwd(NULL, 0);
5808 if (cwd == NULL) {
5809 error = got_error_from_errno("getcwd");
5810 goto done;
5813 if (repo_path == NULL) {
5814 error = got_worktree_open(&worktree, cwd);
5815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5816 goto done;
5817 else
5818 error = NULL;
5819 if (worktree) {
5820 repo_path =
5821 strdup(got_worktree_get_repo_path(worktree));
5822 if (repo_path == NULL)
5823 error = got_error_from_errno("strdup");
5824 if (error)
5825 goto done;
5826 } else {
5827 repo_path = strdup(cwd);
5828 if (repo_path == NULL) {
5829 error = got_error_from_errno("strdup");
5830 goto done;
5835 if (do_list) {
5836 error = got_repo_open(&repo, repo_path, NULL);
5837 if (error != NULL)
5838 goto done;
5839 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5840 if (error)
5841 goto done;
5842 error = list_tags(repo, worktree);
5843 } else {
5844 error = get_gitconfig_path(&gitconfig_path);
5845 if (error)
5846 goto done;
5847 error = got_repo_open(&repo, repo_path, gitconfig_path);
5848 if (error != NULL)
5849 goto done;
5851 if (tagmsg) {
5852 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5853 if (error)
5854 goto done;
5857 if (commit_id_arg == NULL) {
5858 struct got_reference *head_ref;
5859 struct got_object_id *commit_id;
5860 error = got_ref_open(&head_ref, repo,
5861 worktree ? got_worktree_get_head_ref_name(worktree)
5862 : GOT_REF_HEAD, 0);
5863 if (error)
5864 goto done;
5865 error = got_ref_resolve(&commit_id, repo, head_ref);
5866 got_ref_close(head_ref);
5867 if (error)
5868 goto done;
5869 error = got_object_id_str(&commit_id_str, commit_id);
5870 free(commit_id);
5871 if (error)
5872 goto done;
5875 error = add_tag(repo, tag_name,
5876 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5878 done:
5879 if (repo)
5880 got_repo_close(repo);
5881 if (worktree)
5882 got_worktree_close(worktree);
5883 free(cwd);
5884 free(repo_path);
5885 free(gitconfig_path);
5886 free(commit_id_str);
5887 return error;
5890 __dead static void
5891 usage_add(void)
5893 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5894 getprogname());
5895 exit(1);
5898 static const struct got_error *
5899 add_progress(void *arg, unsigned char status, const char *path)
5901 while (path[0] == '/')
5902 path++;
5903 printf("%c %s\n", status, path);
5904 return NULL;
5907 static const struct got_error *
5908 cmd_add(int argc, char *argv[])
5910 const struct got_error *error = NULL;
5911 struct got_repository *repo = NULL;
5912 struct got_worktree *worktree = NULL;
5913 char *cwd = NULL;
5914 struct got_pathlist_head paths;
5915 struct got_pathlist_entry *pe;
5916 int ch, can_recurse = 0, no_ignores = 0;
5918 TAILQ_INIT(&paths);
5920 while ((ch = getopt(argc, argv, "IR")) != -1) {
5921 switch (ch) {
5922 case 'I':
5923 no_ignores = 1;
5924 break;
5925 case 'R':
5926 can_recurse = 1;
5927 break;
5928 default:
5929 usage_add();
5930 /* NOTREACHED */
5934 argc -= optind;
5935 argv += optind;
5937 #ifndef PROFILE
5938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5939 NULL) == -1)
5940 err(1, "pledge");
5941 #endif
5942 if (argc < 1)
5943 usage_add();
5945 cwd = getcwd(NULL, 0);
5946 if (cwd == NULL) {
5947 error = got_error_from_errno("getcwd");
5948 goto done;
5951 error = got_worktree_open(&worktree, cwd);
5952 if (error) {
5953 if (error->code == GOT_ERR_NOT_WORKTREE)
5954 error = wrap_not_worktree_error(error, "add", cwd);
5955 goto done;
5958 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5959 NULL);
5960 if (error != NULL)
5961 goto done;
5963 error = apply_unveil(got_repo_get_path(repo), 1,
5964 got_worktree_get_root_path(worktree));
5965 if (error)
5966 goto done;
5968 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5969 if (error)
5970 goto done;
5972 if (!can_recurse && no_ignores) {
5973 error = got_error_msg(GOT_ERR_BAD_PATH,
5974 "disregarding ignores requires -R option");
5975 goto done;
5979 if (!can_recurse) {
5980 char *ondisk_path;
5981 struct stat sb;
5982 TAILQ_FOREACH(pe, &paths, entry) {
5983 if (asprintf(&ondisk_path, "%s/%s",
5984 got_worktree_get_root_path(worktree),
5985 pe->path) == -1) {
5986 error = got_error_from_errno("asprintf");
5987 goto done;
5989 if (lstat(ondisk_path, &sb) == -1) {
5990 if (errno == ENOENT) {
5991 free(ondisk_path);
5992 continue;
5994 error = got_error_from_errno2("lstat",
5995 ondisk_path);
5996 free(ondisk_path);
5997 goto done;
5999 free(ondisk_path);
6000 if (S_ISDIR(sb.st_mode)) {
6001 error = got_error_msg(GOT_ERR_BAD_PATH,
6002 "adding directories requires -R option");
6003 goto done;
6008 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6009 NULL, repo, no_ignores);
6010 done:
6011 if (repo)
6012 got_repo_close(repo);
6013 if (worktree)
6014 got_worktree_close(worktree);
6015 TAILQ_FOREACH(pe, &paths, entry)
6016 free((char *)pe->path);
6017 got_pathlist_free(&paths);
6018 free(cwd);
6019 return error;
6022 __dead static void
6023 usage_remove(void)
6025 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6026 "path ...\n", getprogname());
6027 exit(1);
6030 static const struct got_error *
6031 print_remove_status(void *arg, unsigned char status,
6032 unsigned char staged_status, const char *path)
6034 while (path[0] == '/')
6035 path++;
6036 if (status == GOT_STATUS_NONEXISTENT)
6037 return NULL;
6038 if (status == staged_status && (status == GOT_STATUS_DELETE))
6039 status = GOT_STATUS_NO_CHANGE;
6040 printf("%c%c %s\n", status, staged_status, path);
6041 return NULL;
6044 static const struct got_error *
6045 cmd_remove(int argc, char *argv[])
6047 const struct got_error *error = NULL;
6048 struct got_worktree *worktree = NULL;
6049 struct got_repository *repo = NULL;
6050 const char *status_codes = NULL;
6051 char *cwd = NULL;
6052 struct got_pathlist_head paths;
6053 struct got_pathlist_entry *pe;
6054 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6056 TAILQ_INIT(&paths);
6058 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6059 switch (ch) {
6060 case 'f':
6061 delete_local_mods = 1;
6062 break;
6063 case 'k':
6064 keep_on_disk = 1;
6065 break;
6066 case 'R':
6067 can_recurse = 1;
6068 break;
6069 case 's':
6070 for (i = 0; i < strlen(optarg); i++) {
6071 switch (optarg[i]) {
6072 case GOT_STATUS_MODIFY:
6073 delete_local_mods = 1;
6074 break;
6075 case GOT_STATUS_MISSING:
6076 break;
6077 default:
6078 errx(1, "invalid status code '%c'",
6079 optarg[i]);
6082 status_codes = optarg;
6083 break;
6084 default:
6085 usage_remove();
6086 /* NOTREACHED */
6090 argc -= optind;
6091 argv += optind;
6093 #ifndef PROFILE
6094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6095 NULL) == -1)
6096 err(1, "pledge");
6097 #endif
6098 if (argc < 1)
6099 usage_remove();
6101 cwd = getcwd(NULL, 0);
6102 if (cwd == NULL) {
6103 error = got_error_from_errno("getcwd");
6104 goto done;
6106 error = got_worktree_open(&worktree, cwd);
6107 if (error) {
6108 if (error->code == GOT_ERR_NOT_WORKTREE)
6109 error = wrap_not_worktree_error(error, "remove", cwd);
6110 goto done;
6113 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6114 NULL);
6115 if (error)
6116 goto done;
6118 error = apply_unveil(got_repo_get_path(repo), 1,
6119 got_worktree_get_root_path(worktree));
6120 if (error)
6121 goto done;
6123 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6124 if (error)
6125 goto done;
6127 if (!can_recurse) {
6128 char *ondisk_path;
6129 struct stat sb;
6130 TAILQ_FOREACH(pe, &paths, entry) {
6131 if (asprintf(&ondisk_path, "%s/%s",
6132 got_worktree_get_root_path(worktree),
6133 pe->path) == -1) {
6134 error = got_error_from_errno("asprintf");
6135 goto done;
6137 if (lstat(ondisk_path, &sb) == -1) {
6138 if (errno == ENOENT) {
6139 free(ondisk_path);
6140 continue;
6142 error = got_error_from_errno2("lstat",
6143 ondisk_path);
6144 free(ondisk_path);
6145 goto done;
6147 free(ondisk_path);
6148 if (S_ISDIR(sb.st_mode)) {
6149 error = got_error_msg(GOT_ERR_BAD_PATH,
6150 "removing directories requires -R option");
6151 goto done;
6156 error = got_worktree_schedule_delete(worktree, &paths,
6157 delete_local_mods, status_codes, print_remove_status, NULL,
6158 repo, keep_on_disk);
6159 done:
6160 if (repo)
6161 got_repo_close(repo);
6162 if (worktree)
6163 got_worktree_close(worktree);
6164 TAILQ_FOREACH(pe, &paths, entry)
6165 free((char *)pe->path);
6166 got_pathlist_free(&paths);
6167 free(cwd);
6168 return error;
6171 __dead static void
6172 usage_revert(void)
6174 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6175 "path ...\n", getprogname());
6176 exit(1);
6179 static const struct got_error *
6180 revert_progress(void *arg, unsigned char status, const char *path)
6182 if (status == GOT_STATUS_UNVERSIONED)
6183 return NULL;
6185 while (path[0] == '/')
6186 path++;
6187 printf("%c %s\n", status, path);
6188 return NULL;
6191 struct choose_patch_arg {
6192 FILE *patch_script_file;
6193 const char *action;
6196 static const struct got_error *
6197 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6198 int nchanges, const char *action)
6200 char *line = NULL;
6201 size_t linesize = 0;
6202 ssize_t linelen;
6204 switch (status) {
6205 case GOT_STATUS_ADD:
6206 printf("A %s\n%s this addition? [y/n] ", path, action);
6207 break;
6208 case GOT_STATUS_DELETE:
6209 printf("D %s\n%s this deletion? [y/n] ", path, action);
6210 break;
6211 case GOT_STATUS_MODIFY:
6212 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6213 return got_error_from_errno("fseek");
6214 printf(GOT_COMMIT_SEP_STR);
6215 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6216 printf("%s", line);
6217 if (ferror(patch_file))
6218 return got_error_from_errno("getline");
6219 printf(GOT_COMMIT_SEP_STR);
6220 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6221 path, n, nchanges, action);
6222 break;
6223 default:
6224 return got_error_path(path, GOT_ERR_FILE_STATUS);
6227 return NULL;
6230 static const struct got_error *
6231 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6232 FILE *patch_file, int n, int nchanges)
6234 const struct got_error *err = NULL;
6235 char *line = NULL;
6236 size_t linesize = 0;
6237 ssize_t linelen;
6238 int resp = ' ';
6239 struct choose_patch_arg *a = arg;
6241 *choice = GOT_PATCH_CHOICE_NONE;
6243 if (a->patch_script_file) {
6244 char *nl;
6245 err = show_change(status, path, patch_file, n, nchanges,
6246 a->action);
6247 if (err)
6248 return err;
6249 linelen = getline(&line, &linesize, a->patch_script_file);
6250 if (linelen == -1) {
6251 if (ferror(a->patch_script_file))
6252 return got_error_from_errno("getline");
6253 return NULL;
6255 nl = strchr(line, '\n');
6256 if (nl)
6257 *nl = '\0';
6258 if (strcmp(line, "y") == 0) {
6259 *choice = GOT_PATCH_CHOICE_YES;
6260 printf("y\n");
6261 } else if (strcmp(line, "n") == 0) {
6262 *choice = GOT_PATCH_CHOICE_NO;
6263 printf("n\n");
6264 } else if (strcmp(line, "q") == 0 &&
6265 status == GOT_STATUS_MODIFY) {
6266 *choice = GOT_PATCH_CHOICE_QUIT;
6267 printf("q\n");
6268 } else
6269 printf("invalid response '%s'\n", line);
6270 free(line);
6271 return NULL;
6274 while (resp != 'y' && resp != 'n' && resp != 'q') {
6275 err = show_change(status, path, patch_file, n, nchanges,
6276 a->action);
6277 if (err)
6278 return err;
6279 resp = getchar();
6280 if (resp == '\n')
6281 resp = getchar();
6282 if (status == GOT_STATUS_MODIFY) {
6283 if (resp != 'y' && resp != 'n' && resp != 'q') {
6284 printf("invalid response '%c'\n", resp);
6285 resp = ' ';
6287 } else if (resp != 'y' && resp != 'n') {
6288 printf("invalid response '%c'\n", resp);
6289 resp = ' ';
6293 if (resp == 'y')
6294 *choice = GOT_PATCH_CHOICE_YES;
6295 else if (resp == 'n')
6296 *choice = GOT_PATCH_CHOICE_NO;
6297 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6298 *choice = GOT_PATCH_CHOICE_QUIT;
6300 return NULL;
6304 static const struct got_error *
6305 cmd_revert(int argc, char *argv[])
6307 const struct got_error *error = NULL;
6308 struct got_worktree *worktree = NULL;
6309 struct got_repository *repo = NULL;
6310 char *cwd = NULL, *path = NULL;
6311 struct got_pathlist_head paths;
6312 struct got_pathlist_entry *pe;
6313 int ch, can_recurse = 0, pflag = 0;
6314 FILE *patch_script_file = NULL;
6315 const char *patch_script_path = NULL;
6316 struct choose_patch_arg cpa;
6318 TAILQ_INIT(&paths);
6320 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6321 switch (ch) {
6322 case 'p':
6323 pflag = 1;
6324 break;
6325 case 'F':
6326 patch_script_path = optarg;
6327 break;
6328 case 'R':
6329 can_recurse = 1;
6330 break;
6331 default:
6332 usage_revert();
6333 /* NOTREACHED */
6337 argc -= optind;
6338 argv += optind;
6340 #ifndef PROFILE
6341 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6342 "unveil", NULL) == -1)
6343 err(1, "pledge");
6344 #endif
6345 if (argc < 1)
6346 usage_revert();
6347 if (patch_script_path && !pflag)
6348 errx(1, "-F option can only be used together with -p option");
6350 cwd = getcwd(NULL, 0);
6351 if (cwd == NULL) {
6352 error = got_error_from_errno("getcwd");
6353 goto done;
6355 error = got_worktree_open(&worktree, cwd);
6356 if (error) {
6357 if (error->code == GOT_ERR_NOT_WORKTREE)
6358 error = wrap_not_worktree_error(error, "revert", cwd);
6359 goto done;
6362 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6363 NULL);
6364 if (error != NULL)
6365 goto done;
6367 if (patch_script_path) {
6368 patch_script_file = fopen(patch_script_path, "r");
6369 if (patch_script_file == NULL) {
6370 error = got_error_from_errno2("fopen",
6371 patch_script_path);
6372 goto done;
6375 error = apply_unveil(got_repo_get_path(repo), 1,
6376 got_worktree_get_root_path(worktree));
6377 if (error)
6378 goto done;
6380 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6381 if (error)
6382 goto done;
6384 if (!can_recurse) {
6385 char *ondisk_path;
6386 struct stat sb;
6387 TAILQ_FOREACH(pe, &paths, entry) {
6388 if (asprintf(&ondisk_path, "%s/%s",
6389 got_worktree_get_root_path(worktree),
6390 pe->path) == -1) {
6391 error = got_error_from_errno("asprintf");
6392 goto done;
6394 if (lstat(ondisk_path, &sb) == -1) {
6395 if (errno == ENOENT) {
6396 free(ondisk_path);
6397 continue;
6399 error = got_error_from_errno2("lstat",
6400 ondisk_path);
6401 free(ondisk_path);
6402 goto done;
6404 free(ondisk_path);
6405 if (S_ISDIR(sb.st_mode)) {
6406 error = got_error_msg(GOT_ERR_BAD_PATH,
6407 "reverting directories requires -R option");
6408 goto done;
6413 cpa.patch_script_file = patch_script_file;
6414 cpa.action = "revert";
6415 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6416 pflag ? choose_patch : NULL, &cpa, repo);
6417 done:
6418 if (patch_script_file && fclose(patch_script_file) == EOF &&
6419 error == NULL)
6420 error = got_error_from_errno2("fclose", patch_script_path);
6421 if (repo)
6422 got_repo_close(repo);
6423 if (worktree)
6424 got_worktree_close(worktree);
6425 free(path);
6426 free(cwd);
6427 return error;
6430 __dead static void
6431 usage_commit(void)
6433 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6434 getprogname());
6435 exit(1);
6438 struct collect_commit_logmsg_arg {
6439 const char *cmdline_log;
6440 const char *editor;
6441 const char *worktree_path;
6442 const char *branch_name;
6443 const char *repo_path;
6444 char *logmsg_path;
6448 static const struct got_error *
6449 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6450 void *arg)
6452 char *initial_content = NULL;
6453 struct got_pathlist_entry *pe;
6454 const struct got_error *err = NULL;
6455 char *template = NULL;
6456 struct collect_commit_logmsg_arg *a = arg;
6457 int fd;
6458 size_t len;
6460 /* if a message was specified on the command line, just use it */
6461 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6462 len = strlen(a->cmdline_log) + 1;
6463 *logmsg = malloc(len + 1);
6464 if (*logmsg == NULL)
6465 return got_error_from_errno("malloc");
6466 strlcpy(*logmsg, a->cmdline_log, len);
6467 return NULL;
6470 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6471 return got_error_from_errno("asprintf");
6473 if (asprintf(&initial_content,
6474 "\n# changes to be committed on branch %s:\n",
6475 a->branch_name) == -1)
6476 return got_error_from_errno("asprintf");
6478 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6479 if (err)
6480 goto done;
6482 dprintf(fd, initial_content);
6484 TAILQ_FOREACH(pe, commitable_paths, entry) {
6485 struct got_commitable *ct = pe->data;
6486 dprintf(fd, "# %c %s\n",
6487 got_commitable_get_status(ct),
6488 got_commitable_get_path(ct));
6490 close(fd);
6492 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6493 done:
6494 free(initial_content);
6495 free(template);
6497 /* Editor is done; we can now apply unveil(2) */
6498 if (err == NULL) {
6499 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6500 if (err) {
6501 free(*logmsg);
6502 *logmsg = NULL;
6505 return err;
6508 static const struct got_error *
6509 cmd_commit(int argc, char *argv[])
6511 const struct got_error *error = NULL;
6512 struct got_worktree *worktree = NULL;
6513 struct got_repository *repo = NULL;
6514 char *cwd = NULL, *id_str = NULL;
6515 struct got_object_id *id = NULL;
6516 const char *logmsg = NULL;
6517 struct collect_commit_logmsg_arg cl_arg;
6518 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6519 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6520 int allow_bad_symlinks = 0;
6521 struct got_pathlist_head paths;
6523 TAILQ_INIT(&paths);
6524 cl_arg.logmsg_path = NULL;
6526 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6527 switch (ch) {
6528 case 'm':
6529 logmsg = optarg;
6530 break;
6531 case 'S':
6532 allow_bad_symlinks = 1;
6533 break;
6534 default:
6535 usage_commit();
6536 /* NOTREACHED */
6540 argc -= optind;
6541 argv += optind;
6543 #ifndef PROFILE
6544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6545 "unveil", NULL) == -1)
6546 err(1, "pledge");
6547 #endif
6548 cwd = getcwd(NULL, 0);
6549 if (cwd == NULL) {
6550 error = got_error_from_errno("getcwd");
6551 goto done;
6553 error = got_worktree_open(&worktree, cwd);
6554 if (error) {
6555 if (error->code == GOT_ERR_NOT_WORKTREE)
6556 error = wrap_not_worktree_error(error, "commit", cwd);
6557 goto done;
6560 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6561 if (error)
6562 goto done;
6563 if (rebase_in_progress) {
6564 error = got_error(GOT_ERR_REBASING);
6565 goto done;
6568 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6569 worktree);
6570 if (error)
6571 goto done;
6573 error = get_gitconfig_path(&gitconfig_path);
6574 if (error)
6575 goto done;
6576 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6577 gitconfig_path);
6578 if (error != NULL)
6579 goto done;
6581 error = get_author(&author, repo);
6582 if (error)
6583 return error;
6586 * unveil(2) traverses exec(2); if an editor is used we have
6587 * to apply unveil after the log message has been written.
6589 if (logmsg == NULL || strlen(logmsg) == 0)
6590 error = get_editor(&editor);
6591 else
6592 error = apply_unveil(got_repo_get_path(repo), 0,
6593 got_worktree_get_root_path(worktree));
6594 if (error)
6595 goto done;
6597 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6598 if (error)
6599 goto done;
6601 cl_arg.editor = editor;
6602 cl_arg.cmdline_log = logmsg;
6603 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6604 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6605 if (!histedit_in_progress) {
6606 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6607 error = got_error(GOT_ERR_COMMIT_BRANCH);
6608 goto done;
6610 cl_arg.branch_name += 11;
6612 cl_arg.repo_path = got_repo_get_path(repo);
6613 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6614 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6615 print_status, NULL, repo);
6616 if (error) {
6617 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6618 cl_arg.logmsg_path != NULL)
6619 preserve_logmsg = 1;
6620 goto done;
6623 error = got_object_id_str(&id_str, id);
6624 if (error)
6625 goto done;
6626 printf("Created commit %s\n", id_str);
6627 done:
6628 if (preserve_logmsg) {
6629 fprintf(stderr, "%s: log message preserved in %s\n",
6630 getprogname(), cl_arg.logmsg_path);
6631 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6632 error == NULL)
6633 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6634 free(cl_arg.logmsg_path);
6635 if (repo)
6636 got_repo_close(repo);
6637 if (worktree)
6638 got_worktree_close(worktree);
6639 free(cwd);
6640 free(id_str);
6641 free(gitconfig_path);
6642 free(editor);
6643 free(author);
6644 return error;
6647 __dead static void
6648 usage_cherrypick(void)
6650 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6651 exit(1);
6654 static const struct got_error *
6655 cmd_cherrypick(int argc, char *argv[])
6657 const struct got_error *error = NULL;
6658 struct got_worktree *worktree = NULL;
6659 struct got_repository *repo = NULL;
6660 char *cwd = NULL, *commit_id_str = NULL;
6661 struct got_object_id *commit_id = NULL;
6662 struct got_commit_object *commit = NULL;
6663 struct got_object_qid *pid;
6664 struct got_reference *head_ref = NULL;
6665 int ch;
6666 struct got_update_progress_arg upa;
6668 while ((ch = getopt(argc, argv, "")) != -1) {
6669 switch (ch) {
6670 default:
6671 usage_cherrypick();
6672 /* NOTREACHED */
6676 argc -= optind;
6677 argv += optind;
6679 #ifndef PROFILE
6680 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6681 "unveil", NULL) == -1)
6682 err(1, "pledge");
6683 #endif
6684 if (argc != 1)
6685 usage_cherrypick();
6687 cwd = getcwd(NULL, 0);
6688 if (cwd == NULL) {
6689 error = got_error_from_errno("getcwd");
6690 goto done;
6692 error = got_worktree_open(&worktree, cwd);
6693 if (error) {
6694 if (error->code == GOT_ERR_NOT_WORKTREE)
6695 error = wrap_not_worktree_error(error, "cherrypick",
6696 cwd);
6697 goto done;
6700 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6701 NULL);
6702 if (error != NULL)
6703 goto done;
6705 error = apply_unveil(got_repo_get_path(repo), 0,
6706 got_worktree_get_root_path(worktree));
6707 if (error)
6708 goto done;
6710 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6711 GOT_OBJ_TYPE_COMMIT, repo);
6712 if (error != NULL) {
6713 struct got_reference *ref;
6714 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6715 goto done;
6716 error = got_ref_open(&ref, repo, argv[0], 0);
6717 if (error != NULL)
6718 goto done;
6719 error = got_ref_resolve(&commit_id, repo, ref);
6720 got_ref_close(ref);
6721 if (error != NULL)
6722 goto done;
6724 error = got_object_id_str(&commit_id_str, commit_id);
6725 if (error)
6726 goto done;
6728 error = got_ref_open(&head_ref, repo,
6729 got_worktree_get_head_ref_name(worktree), 0);
6730 if (error != NULL)
6731 goto done;
6733 error = check_same_branch(commit_id, head_ref, NULL, repo);
6734 if (error) {
6735 if (error->code != GOT_ERR_ANCESTRY)
6736 goto done;
6737 error = NULL;
6738 } else {
6739 error = got_error(GOT_ERR_SAME_BRANCH);
6740 goto done;
6743 error = got_object_open_as_commit(&commit, repo, commit_id);
6744 if (error)
6745 goto done;
6746 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6747 memset(&upa, 0, sizeof(upa));
6748 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6749 commit_id, repo, update_progress, &upa, check_cancelled,
6750 NULL);
6751 if (error != NULL)
6752 goto done;
6754 if (upa.did_something)
6755 printf("Merged commit %s\n", commit_id_str);
6756 print_update_progress_stats(&upa);
6757 done:
6758 if (commit)
6759 got_object_commit_close(commit);
6760 free(commit_id_str);
6761 if (head_ref)
6762 got_ref_close(head_ref);
6763 if (worktree)
6764 got_worktree_close(worktree);
6765 if (repo)
6766 got_repo_close(repo);
6767 return error;
6770 __dead static void
6771 usage_backout(void)
6773 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6774 exit(1);
6777 static const struct got_error *
6778 cmd_backout(int argc, char *argv[])
6780 const struct got_error *error = NULL;
6781 struct got_worktree *worktree = NULL;
6782 struct got_repository *repo = NULL;
6783 char *cwd = NULL, *commit_id_str = NULL;
6784 struct got_object_id *commit_id = NULL;
6785 struct got_commit_object *commit = NULL;
6786 struct got_object_qid *pid;
6787 struct got_reference *head_ref = NULL;
6788 int ch;
6789 struct got_update_progress_arg upa;
6791 while ((ch = getopt(argc, argv, "")) != -1) {
6792 switch (ch) {
6793 default:
6794 usage_backout();
6795 /* NOTREACHED */
6799 argc -= optind;
6800 argv += optind;
6802 #ifndef PROFILE
6803 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6804 "unveil", NULL) == -1)
6805 err(1, "pledge");
6806 #endif
6807 if (argc != 1)
6808 usage_backout();
6810 cwd = getcwd(NULL, 0);
6811 if (cwd == NULL) {
6812 error = got_error_from_errno("getcwd");
6813 goto done;
6815 error = got_worktree_open(&worktree, cwd);
6816 if (error) {
6817 if (error->code == GOT_ERR_NOT_WORKTREE)
6818 error = wrap_not_worktree_error(error, "backout", cwd);
6819 goto done;
6822 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6823 NULL);
6824 if (error != NULL)
6825 goto done;
6827 error = apply_unveil(got_repo_get_path(repo), 0,
6828 got_worktree_get_root_path(worktree));
6829 if (error)
6830 goto done;
6832 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6833 GOT_OBJ_TYPE_COMMIT, repo);
6834 if (error != NULL) {
6835 struct got_reference *ref;
6836 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6837 goto done;
6838 error = got_ref_open(&ref, repo, argv[0], 0);
6839 if (error != NULL)
6840 goto done;
6841 error = got_ref_resolve(&commit_id, repo, ref);
6842 got_ref_close(ref);
6843 if (error != NULL)
6844 goto done;
6846 error = got_object_id_str(&commit_id_str, commit_id);
6847 if (error)
6848 goto done;
6850 error = got_ref_open(&head_ref, repo,
6851 got_worktree_get_head_ref_name(worktree), 0);
6852 if (error != NULL)
6853 goto done;
6855 error = check_same_branch(commit_id, head_ref, NULL, repo);
6856 if (error)
6857 goto done;
6859 error = got_object_open_as_commit(&commit, repo, commit_id);
6860 if (error)
6861 goto done;
6862 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6863 if (pid == NULL) {
6864 error = got_error(GOT_ERR_ROOT_COMMIT);
6865 goto done;
6868 memset(&upa, 0, sizeof(upa));
6869 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6870 update_progress, &upa, check_cancelled, NULL);
6871 if (error != NULL)
6872 goto done;
6874 if (upa.did_something)
6875 printf("Backed out commit %s\n", commit_id_str);
6876 print_update_progress_stats(&upa);
6877 done:
6878 if (commit)
6879 got_object_commit_close(commit);
6880 free(commit_id_str);
6881 if (head_ref)
6882 got_ref_close(head_ref);
6883 if (worktree)
6884 got_worktree_close(worktree);
6885 if (repo)
6886 got_repo_close(repo);
6887 return error;
6890 __dead static void
6891 usage_rebase(void)
6893 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6894 getprogname());
6895 exit(1);
6898 void
6899 trim_logmsg(char *logmsg, int limit)
6901 char *nl;
6902 size_t len;
6904 len = strlen(logmsg);
6905 if (len > limit)
6906 len = limit;
6907 logmsg[len] = '\0';
6908 nl = strchr(logmsg, '\n');
6909 if (nl)
6910 *nl = '\0';
6913 static const struct got_error *
6914 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6916 const struct got_error *err;
6917 char *logmsg0 = NULL;
6918 const char *s;
6920 err = got_object_commit_get_logmsg(&logmsg0, commit);
6921 if (err)
6922 return err;
6924 s = logmsg0;
6925 while (isspace((unsigned char)s[0]))
6926 s++;
6928 *logmsg = strdup(s);
6929 if (*logmsg == NULL) {
6930 err = got_error_from_errno("strdup");
6931 goto done;
6934 trim_logmsg(*logmsg, limit);
6935 done:
6936 free(logmsg0);
6937 return err;
6940 static const struct got_error *
6941 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6943 const struct got_error *err;
6944 struct got_commit_object *commit = NULL;
6945 char *id_str = NULL, *logmsg = NULL;
6947 err = got_object_open_as_commit(&commit, repo, id);
6948 if (err)
6949 return err;
6951 err = got_object_id_str(&id_str, id);
6952 if (err)
6953 goto done;
6955 id_str[12] = '\0';
6957 err = get_short_logmsg(&logmsg, 42, commit);
6958 if (err)
6959 goto done;
6961 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6962 done:
6963 free(id_str);
6964 got_object_commit_close(commit);
6965 free(logmsg);
6966 return err;
6969 static const struct got_error *
6970 show_rebase_progress(struct got_commit_object *commit,
6971 struct got_object_id *old_id, struct got_object_id *new_id)
6973 const struct got_error *err;
6974 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6976 err = got_object_id_str(&old_id_str, old_id);
6977 if (err)
6978 goto done;
6980 if (new_id) {
6981 err = got_object_id_str(&new_id_str, new_id);
6982 if (err)
6983 goto done;
6986 old_id_str[12] = '\0';
6987 if (new_id_str)
6988 new_id_str[12] = '\0';
6990 err = get_short_logmsg(&logmsg, 42, commit);
6991 if (err)
6992 goto done;
6994 printf("%s -> %s: %s\n", old_id_str,
6995 new_id_str ? new_id_str : "no-op change", logmsg);
6996 done:
6997 free(old_id_str);
6998 free(new_id_str);
6999 free(logmsg);
7000 return err;
7003 static const struct got_error *
7004 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7005 struct got_reference *branch, struct got_reference *new_base_branch,
7006 struct got_reference *tmp_branch, struct got_repository *repo)
7008 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7009 return got_worktree_rebase_complete(worktree, fileindex,
7010 new_base_branch, tmp_branch, branch, repo);
7013 static const struct got_error *
7014 rebase_commit(struct got_pathlist_head *merged_paths,
7015 struct got_worktree *worktree, struct got_fileindex *fileindex,
7016 struct got_reference *tmp_branch,
7017 struct got_object_id *commit_id, struct got_repository *repo)
7019 const struct got_error *error;
7020 struct got_commit_object *commit;
7021 struct got_object_id *new_commit_id;
7023 error = got_object_open_as_commit(&commit, repo, commit_id);
7024 if (error)
7025 return error;
7027 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7028 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7029 if (error) {
7030 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7031 goto done;
7032 error = show_rebase_progress(commit, commit_id, NULL);
7033 } else {
7034 error = show_rebase_progress(commit, commit_id, new_commit_id);
7035 free(new_commit_id);
7037 done:
7038 got_object_commit_close(commit);
7039 return error;
7042 struct check_path_prefix_arg {
7043 const char *path_prefix;
7044 size_t len;
7045 int errcode;
7048 static const struct got_error *
7049 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7050 struct got_blob_object *blob2, struct got_object_id *id1,
7051 struct got_object_id *id2, const char *path1, const char *path2,
7052 mode_t mode1, mode_t mode2, struct got_repository *repo)
7054 struct check_path_prefix_arg *a = arg;
7056 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7057 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7058 return got_error(a->errcode);
7060 return NULL;
7063 static const struct got_error *
7064 check_path_prefix(struct got_object_id *parent_id,
7065 struct got_object_id *commit_id, const char *path_prefix,
7066 int errcode, struct got_repository *repo)
7068 const struct got_error *err;
7069 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7070 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7071 struct check_path_prefix_arg cpp_arg;
7073 if (got_path_is_root_dir(path_prefix))
7074 return NULL;
7076 err = got_object_open_as_commit(&commit, repo, commit_id);
7077 if (err)
7078 goto done;
7080 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7081 if (err)
7082 goto done;
7084 err = got_object_open_as_tree(&tree1, repo,
7085 got_object_commit_get_tree_id(parent_commit));
7086 if (err)
7087 goto done;
7089 err = got_object_open_as_tree(&tree2, repo,
7090 got_object_commit_get_tree_id(commit));
7091 if (err)
7092 goto done;
7094 cpp_arg.path_prefix = path_prefix;
7095 while (cpp_arg.path_prefix[0] == '/')
7096 cpp_arg.path_prefix++;
7097 cpp_arg.len = strlen(cpp_arg.path_prefix);
7098 cpp_arg.errcode = errcode;
7099 err = got_diff_tree(tree1, tree2, "", "", repo,
7100 check_path_prefix_in_diff, &cpp_arg, 0);
7101 done:
7102 if (tree1)
7103 got_object_tree_close(tree1);
7104 if (tree2)
7105 got_object_tree_close(tree2);
7106 if (commit)
7107 got_object_commit_close(commit);
7108 if (parent_commit)
7109 got_object_commit_close(parent_commit);
7110 return err;
7113 static const struct got_error *
7114 collect_commits(struct got_object_id_queue *commits,
7115 struct got_object_id *initial_commit_id,
7116 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7117 const char *path_prefix, int path_prefix_errcode,
7118 struct got_repository *repo)
7120 const struct got_error *err = NULL;
7121 struct got_commit_graph *graph = NULL;
7122 struct got_object_id *parent_id = NULL;
7123 struct got_object_qid *qid;
7124 struct got_object_id *commit_id = initial_commit_id;
7126 err = got_commit_graph_open(&graph, "/", 1);
7127 if (err)
7128 return err;
7130 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7131 check_cancelled, NULL);
7132 if (err)
7133 goto done;
7134 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7135 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7136 check_cancelled, NULL);
7137 if (err) {
7138 if (err->code == GOT_ERR_ITER_COMPLETED) {
7139 err = got_error_msg(GOT_ERR_ANCESTRY,
7140 "ran out of commits to rebase before "
7141 "youngest common ancestor commit has "
7142 "been reached?!?");
7144 goto done;
7145 } else {
7146 err = check_path_prefix(parent_id, commit_id,
7147 path_prefix, path_prefix_errcode, repo);
7148 if (err)
7149 goto done;
7151 err = got_object_qid_alloc(&qid, commit_id);
7152 if (err)
7153 goto done;
7154 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7155 commit_id = parent_id;
7158 done:
7159 got_commit_graph_close(graph);
7160 return err;
7163 static const struct got_error *
7164 cmd_rebase(int argc, char *argv[])
7166 const struct got_error *error = NULL;
7167 struct got_worktree *worktree = NULL;
7168 struct got_repository *repo = NULL;
7169 struct got_fileindex *fileindex = NULL;
7170 char *cwd = NULL;
7171 struct got_reference *branch = NULL;
7172 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7173 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7174 struct got_object_id *resume_commit_id = NULL;
7175 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7176 struct got_commit_object *commit = NULL;
7177 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7178 int histedit_in_progress = 0;
7179 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7180 struct got_object_id_queue commits;
7181 struct got_pathlist_head merged_paths;
7182 const struct got_object_id_queue *parent_ids;
7183 struct got_object_qid *qid, *pid;
7185 SIMPLEQ_INIT(&commits);
7186 TAILQ_INIT(&merged_paths);
7188 while ((ch = getopt(argc, argv, "ac")) != -1) {
7189 switch (ch) {
7190 case 'a':
7191 abort_rebase = 1;
7192 break;
7193 case 'c':
7194 continue_rebase = 1;
7195 break;
7196 default:
7197 usage_rebase();
7198 /* NOTREACHED */
7202 argc -= optind;
7203 argv += optind;
7205 #ifndef PROFILE
7206 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7207 "unveil", NULL) == -1)
7208 err(1, "pledge");
7209 #endif
7210 if (abort_rebase && continue_rebase)
7211 usage_rebase();
7212 else if (abort_rebase || continue_rebase) {
7213 if (argc != 0)
7214 usage_rebase();
7215 } else if (argc != 1)
7216 usage_rebase();
7218 cwd = getcwd(NULL, 0);
7219 if (cwd == NULL) {
7220 error = got_error_from_errno("getcwd");
7221 goto done;
7223 error = got_worktree_open(&worktree, cwd);
7224 if (error) {
7225 if (error->code == GOT_ERR_NOT_WORKTREE)
7226 error = wrap_not_worktree_error(error, "rebase", cwd);
7227 goto done;
7230 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7231 NULL);
7232 if (error != NULL)
7233 goto done;
7235 error = apply_unveil(got_repo_get_path(repo), 0,
7236 got_worktree_get_root_path(worktree));
7237 if (error)
7238 goto done;
7240 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7241 worktree);
7242 if (error)
7243 goto done;
7244 if (histedit_in_progress) {
7245 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7246 goto done;
7249 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7250 if (error)
7251 goto done;
7253 if (abort_rebase) {
7254 struct got_update_progress_arg upa;
7255 if (!rebase_in_progress) {
7256 error = got_error(GOT_ERR_NOT_REBASING);
7257 goto done;
7259 error = got_worktree_rebase_continue(&resume_commit_id,
7260 &new_base_branch, &tmp_branch, &branch, &fileindex,
7261 worktree, repo);
7262 if (error)
7263 goto done;
7264 printf("Switching work tree to %s\n",
7265 got_ref_get_symref_target(new_base_branch));
7266 memset(&upa, 0, sizeof(upa));
7267 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7268 new_base_branch, update_progress, &upa);
7269 if (error)
7270 goto done;
7271 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7272 print_update_progress_stats(&upa);
7273 goto done; /* nothing else to do */
7276 if (continue_rebase) {
7277 if (!rebase_in_progress) {
7278 error = got_error(GOT_ERR_NOT_REBASING);
7279 goto done;
7281 error = got_worktree_rebase_continue(&resume_commit_id,
7282 &new_base_branch, &tmp_branch, &branch, &fileindex,
7283 worktree, repo);
7284 if (error)
7285 goto done;
7287 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7288 resume_commit_id, repo);
7289 if (error)
7290 goto done;
7292 yca_id = got_object_id_dup(resume_commit_id);
7293 if (yca_id == NULL) {
7294 error = got_error_from_errno("got_object_id_dup");
7295 goto done;
7297 } else {
7298 error = got_ref_open(&branch, repo, argv[0], 0);
7299 if (error != NULL)
7300 goto done;
7303 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7304 if (error)
7305 goto done;
7307 if (!continue_rebase) {
7308 struct got_object_id *base_commit_id;
7310 base_commit_id = got_worktree_get_base_commit_id(worktree);
7311 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7312 base_commit_id, branch_head_commit_id, repo,
7313 check_cancelled, NULL);
7314 if (error)
7315 goto done;
7316 if (yca_id == NULL) {
7317 error = got_error_msg(GOT_ERR_ANCESTRY,
7318 "specified branch shares no common ancestry "
7319 "with work tree's branch");
7320 goto done;
7323 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7324 if (error) {
7325 if (error->code != GOT_ERR_ANCESTRY)
7326 goto done;
7327 error = NULL;
7328 } else {
7329 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7330 "specified branch resolves to a commit which "
7331 "is already contained in work tree's branch");
7332 goto done;
7334 error = got_worktree_rebase_prepare(&new_base_branch,
7335 &tmp_branch, &fileindex, worktree, branch, repo);
7336 if (error)
7337 goto done;
7340 commit_id = branch_head_commit_id;
7341 error = got_object_open_as_commit(&commit, repo, commit_id);
7342 if (error)
7343 goto done;
7345 parent_ids = got_object_commit_get_parent_ids(commit);
7346 pid = SIMPLEQ_FIRST(parent_ids);
7347 if (pid == NULL) {
7348 if (!continue_rebase) {
7349 struct got_update_progress_arg upa;
7350 memset(&upa, 0, sizeof(upa));
7351 error = got_worktree_rebase_abort(worktree, fileindex,
7352 repo, new_base_branch, update_progress, &upa);
7353 if (error)
7354 goto done;
7355 printf("Rebase of %s aborted\n",
7356 got_ref_get_name(branch));
7357 print_update_progress_stats(&upa);
7360 error = got_error(GOT_ERR_EMPTY_REBASE);
7361 goto done;
7363 error = collect_commits(&commits, commit_id, pid->id,
7364 yca_id, got_worktree_get_path_prefix(worktree),
7365 GOT_ERR_REBASE_PATH, repo);
7366 got_object_commit_close(commit);
7367 commit = NULL;
7368 if (error)
7369 goto done;
7371 if (SIMPLEQ_EMPTY(&commits)) {
7372 if (continue_rebase) {
7373 error = rebase_complete(worktree, fileindex,
7374 branch, new_base_branch, tmp_branch, repo);
7375 goto done;
7376 } else {
7377 /* Fast-forward the reference of the branch. */
7378 struct got_object_id *new_head_commit_id;
7379 char *id_str;
7380 error = got_ref_resolve(&new_head_commit_id, repo,
7381 new_base_branch);
7382 if (error)
7383 goto done;
7384 error = got_object_id_str(&id_str, new_head_commit_id);
7385 printf("Forwarding %s to commit %s\n",
7386 got_ref_get_name(branch), id_str);
7387 free(id_str);
7388 error = got_ref_change_ref(branch,
7389 new_head_commit_id);
7390 if (error)
7391 goto done;
7395 pid = NULL;
7396 SIMPLEQ_FOREACH(qid, &commits, entry) {
7397 struct got_update_progress_arg upa;
7399 commit_id = qid->id;
7400 parent_id = pid ? pid->id : yca_id;
7401 pid = qid;
7403 memset(&upa, 0, sizeof(upa));
7404 error = got_worktree_rebase_merge_files(&merged_paths,
7405 worktree, fileindex, parent_id, commit_id, repo,
7406 update_progress, &upa, check_cancelled, NULL);
7407 if (error)
7408 goto done;
7410 print_update_progress_stats(&upa);
7411 if (upa.conflicts > 0)
7412 rebase_status = GOT_STATUS_CONFLICT;
7414 if (rebase_status == GOT_STATUS_CONFLICT) {
7415 error = show_rebase_merge_conflict(qid->id, repo);
7416 if (error)
7417 goto done;
7418 got_worktree_rebase_pathlist_free(&merged_paths);
7419 break;
7422 error = rebase_commit(&merged_paths, worktree, fileindex,
7423 tmp_branch, commit_id, repo);
7424 got_worktree_rebase_pathlist_free(&merged_paths);
7425 if (error)
7426 goto done;
7429 if (rebase_status == GOT_STATUS_CONFLICT) {
7430 error = got_worktree_rebase_postpone(worktree, fileindex);
7431 if (error)
7432 goto done;
7433 error = got_error_msg(GOT_ERR_CONFLICTS,
7434 "conflicts must be resolved before rebasing can continue");
7435 } else
7436 error = rebase_complete(worktree, fileindex, branch,
7437 new_base_branch, tmp_branch, repo);
7438 done:
7439 got_object_id_queue_free(&commits);
7440 free(branch_head_commit_id);
7441 free(resume_commit_id);
7442 free(yca_id);
7443 if (commit)
7444 got_object_commit_close(commit);
7445 if (branch)
7446 got_ref_close(branch);
7447 if (new_base_branch)
7448 got_ref_close(new_base_branch);
7449 if (tmp_branch)
7450 got_ref_close(tmp_branch);
7451 if (worktree)
7452 got_worktree_close(worktree);
7453 if (repo)
7454 got_repo_close(repo);
7455 return error;
7458 __dead static void
7459 usage_histedit(void)
7461 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7462 getprogname());
7463 exit(1);
7466 #define GOT_HISTEDIT_PICK 'p'
7467 #define GOT_HISTEDIT_EDIT 'e'
7468 #define GOT_HISTEDIT_FOLD 'f'
7469 #define GOT_HISTEDIT_DROP 'd'
7470 #define GOT_HISTEDIT_MESG 'm'
7472 static struct got_histedit_cmd {
7473 unsigned char code;
7474 const char *name;
7475 const char *desc;
7476 } got_histedit_cmds[] = {
7477 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7478 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7479 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7480 "be used" },
7481 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7482 { GOT_HISTEDIT_MESG, "mesg",
7483 "single-line log message for commit above (open editor if empty)" },
7486 struct got_histedit_list_entry {
7487 TAILQ_ENTRY(got_histedit_list_entry) entry;
7488 struct got_object_id *commit_id;
7489 const struct got_histedit_cmd *cmd;
7490 char *logmsg;
7492 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7494 static const struct got_error *
7495 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7496 FILE *f, struct got_repository *repo)
7498 const struct got_error *err = NULL;
7499 char *logmsg = NULL, *id_str = NULL;
7500 struct got_commit_object *commit = NULL;
7501 int n;
7503 err = got_object_open_as_commit(&commit, repo, commit_id);
7504 if (err)
7505 goto done;
7507 err = get_short_logmsg(&logmsg, 34, commit);
7508 if (err)
7509 goto done;
7511 err = got_object_id_str(&id_str, commit_id);
7512 if (err)
7513 goto done;
7515 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7516 if (n < 0)
7517 err = got_ferror(f, GOT_ERR_IO);
7518 done:
7519 if (commit)
7520 got_object_commit_close(commit);
7521 free(id_str);
7522 free(logmsg);
7523 return err;
7526 static const struct got_error *
7527 histedit_write_commit_list(struct got_object_id_queue *commits,
7528 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7530 const struct got_error *err = NULL;
7531 struct got_object_qid *qid;
7533 if (SIMPLEQ_EMPTY(commits))
7534 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7536 SIMPLEQ_FOREACH(qid, commits, entry) {
7537 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7538 f, repo);
7539 if (err)
7540 break;
7541 if (edit_logmsg_only) {
7542 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7543 if (n < 0) {
7544 err = got_ferror(f, GOT_ERR_IO);
7545 break;
7550 return err;
7553 static const struct got_error *
7554 write_cmd_list(FILE *f, const char *branch_name,
7555 struct got_object_id_queue *commits)
7557 const struct got_error *err = NULL;
7558 int n, i;
7559 char *id_str;
7560 struct got_object_qid *qid;
7562 qid = SIMPLEQ_FIRST(commits);
7563 err = got_object_id_str(&id_str, qid->id);
7564 if (err)
7565 return err;
7567 n = fprintf(f,
7568 "# Editing the history of branch '%s' starting at\n"
7569 "# commit %s\n"
7570 "# Commits will be processed in order from top to "
7571 "bottom of this file.\n", branch_name, id_str);
7572 if (n < 0) {
7573 err = got_ferror(f, GOT_ERR_IO);
7574 goto done;
7577 n = fprintf(f, "# Available histedit commands:\n");
7578 if (n < 0) {
7579 err = got_ferror(f, GOT_ERR_IO);
7580 goto done;
7583 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7584 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7585 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7586 cmd->desc);
7587 if (n < 0) {
7588 err = got_ferror(f, GOT_ERR_IO);
7589 break;
7592 done:
7593 free(id_str);
7594 return err;
7597 static const struct got_error *
7598 histedit_syntax_error(int lineno)
7600 static char msg[42];
7601 int ret;
7603 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7604 lineno);
7605 if (ret == -1 || ret >= sizeof(msg))
7606 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7608 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7611 static const struct got_error *
7612 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7613 char *logmsg, struct got_repository *repo)
7615 const struct got_error *err;
7616 struct got_commit_object *folded_commit = NULL;
7617 char *id_str, *folded_logmsg = NULL;
7619 err = got_object_id_str(&id_str, hle->commit_id);
7620 if (err)
7621 return err;
7623 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7624 if (err)
7625 goto done;
7627 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7628 if (err)
7629 goto done;
7630 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7631 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7632 folded_logmsg) == -1) {
7633 err = got_error_from_errno("asprintf");
7635 done:
7636 if (folded_commit)
7637 got_object_commit_close(folded_commit);
7638 free(id_str);
7639 free(folded_logmsg);
7640 return err;
7643 static struct got_histedit_list_entry *
7644 get_folded_commits(struct got_histedit_list_entry *hle)
7646 struct got_histedit_list_entry *prev, *folded = NULL;
7648 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7649 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7650 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7651 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7652 folded = prev;
7653 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7656 return folded;
7659 static const struct got_error *
7660 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7661 struct got_repository *repo)
7663 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7664 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7665 const struct got_error *err = NULL;
7666 struct got_commit_object *commit = NULL;
7667 int fd;
7668 struct got_histedit_list_entry *folded = NULL;
7670 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7671 if (err)
7672 return err;
7674 folded = get_folded_commits(hle);
7675 if (folded) {
7676 while (folded != hle) {
7677 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7678 folded = TAILQ_NEXT(folded, entry);
7679 continue;
7681 err = append_folded_commit_msg(&new_msg, folded,
7682 logmsg, repo);
7683 if (err)
7684 goto done;
7685 free(logmsg);
7686 logmsg = new_msg;
7687 folded = TAILQ_NEXT(folded, entry);
7691 err = got_object_id_str(&id_str, hle->commit_id);
7692 if (err)
7693 goto done;
7694 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7695 if (err)
7696 goto done;
7697 if (asprintf(&new_msg,
7698 "%s\n# original log message of commit %s: %s",
7699 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7700 err = got_error_from_errno("asprintf");
7701 goto done;
7703 free(logmsg);
7704 logmsg = new_msg;
7706 err = got_object_id_str(&id_str, hle->commit_id);
7707 if (err)
7708 goto done;
7710 err = got_opentemp_named_fd(&logmsg_path, &fd,
7711 GOT_TMPDIR_STR "/got-logmsg");
7712 if (err)
7713 goto done;
7715 dprintf(fd, logmsg);
7716 close(fd);
7718 err = get_editor(&editor);
7719 if (err)
7720 goto done;
7722 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7723 if (err) {
7724 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7725 goto done;
7726 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7728 done:
7729 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7730 err = got_error_from_errno2("unlink", logmsg_path);
7731 free(logmsg_path);
7732 free(logmsg);
7733 free(orig_logmsg);
7734 free(editor);
7735 if (commit)
7736 got_object_commit_close(commit);
7737 return err;
7740 static const struct got_error *
7741 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7742 FILE *f, struct got_repository *repo)
7744 const struct got_error *err = NULL;
7745 char *line = NULL, *p, *end;
7746 size_t size;
7747 ssize_t len;
7748 int lineno = 0, i;
7749 const struct got_histedit_cmd *cmd;
7750 struct got_object_id *commit_id = NULL;
7751 struct got_histedit_list_entry *hle = NULL;
7753 for (;;) {
7754 len = getline(&line, &size, f);
7755 if (len == -1) {
7756 const struct got_error *getline_err;
7757 if (feof(f))
7758 break;
7759 getline_err = got_error_from_errno("getline");
7760 err = got_ferror(f, getline_err->code);
7761 break;
7763 lineno++;
7764 p = line;
7765 while (isspace((unsigned char)p[0]))
7766 p++;
7767 if (p[0] == '#' || p[0] == '\0') {
7768 free(line);
7769 line = NULL;
7770 continue;
7772 cmd = NULL;
7773 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7774 cmd = &got_histedit_cmds[i];
7775 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7776 isspace((unsigned char)p[strlen(cmd->name)])) {
7777 p += strlen(cmd->name);
7778 break;
7780 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7781 p++;
7782 break;
7785 if (i == nitems(got_histedit_cmds)) {
7786 err = histedit_syntax_error(lineno);
7787 break;
7789 while (isspace((unsigned char)p[0]))
7790 p++;
7791 if (cmd->code == GOT_HISTEDIT_MESG) {
7792 if (hle == NULL || hle->logmsg != NULL) {
7793 err = got_error(GOT_ERR_HISTEDIT_CMD);
7794 break;
7796 if (p[0] == '\0') {
7797 err = histedit_edit_logmsg(hle, repo);
7798 if (err)
7799 break;
7800 } else {
7801 hle->logmsg = strdup(p);
7802 if (hle->logmsg == NULL) {
7803 err = got_error_from_errno("strdup");
7804 break;
7807 free(line);
7808 line = NULL;
7809 continue;
7810 } else {
7811 end = p;
7812 while (end[0] && !isspace((unsigned char)end[0]))
7813 end++;
7814 *end = '\0';
7816 err = got_object_resolve_id_str(&commit_id, repo, p);
7817 if (err) {
7818 /* override error code */
7819 err = histedit_syntax_error(lineno);
7820 break;
7823 hle = malloc(sizeof(*hle));
7824 if (hle == NULL) {
7825 err = got_error_from_errno("malloc");
7826 break;
7828 hle->cmd = cmd;
7829 hle->commit_id = commit_id;
7830 hle->logmsg = NULL;
7831 commit_id = NULL;
7832 free(line);
7833 line = NULL;
7834 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7837 free(line);
7838 free(commit_id);
7839 return err;
7842 static const struct got_error *
7843 histedit_check_script(struct got_histedit_list *histedit_cmds,
7844 struct got_object_id_queue *commits, struct got_repository *repo)
7846 const struct got_error *err = NULL;
7847 struct got_object_qid *qid;
7848 struct got_histedit_list_entry *hle;
7849 static char msg[92];
7850 char *id_str;
7852 if (TAILQ_EMPTY(histedit_cmds))
7853 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7854 "histedit script contains no commands");
7855 if (SIMPLEQ_EMPTY(commits))
7856 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7858 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7859 struct got_histedit_list_entry *hle2;
7860 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7861 if (hle == hle2)
7862 continue;
7863 if (got_object_id_cmp(hle->commit_id,
7864 hle2->commit_id) != 0)
7865 continue;
7866 err = got_object_id_str(&id_str, hle->commit_id);
7867 if (err)
7868 return err;
7869 snprintf(msg, sizeof(msg), "commit %s is listed "
7870 "more than once in histedit script", id_str);
7871 free(id_str);
7872 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7876 SIMPLEQ_FOREACH(qid, commits, entry) {
7877 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7878 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7879 break;
7881 if (hle == NULL) {
7882 err = got_object_id_str(&id_str, qid->id);
7883 if (err)
7884 return err;
7885 snprintf(msg, sizeof(msg),
7886 "commit %s missing from histedit script", id_str);
7887 free(id_str);
7888 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7892 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7893 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7894 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7895 "last commit in histedit script cannot be folded");
7897 return NULL;
7900 static const struct got_error *
7901 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7902 const char *path, struct got_object_id_queue *commits,
7903 struct got_repository *repo)
7905 const struct got_error *err = NULL;
7906 char *editor;
7907 FILE *f = NULL;
7909 err = get_editor(&editor);
7910 if (err)
7911 return err;
7913 if (spawn_editor(editor, path) == -1) {
7914 err = got_error_from_errno("failed spawning editor");
7915 goto done;
7918 f = fopen(path, "r");
7919 if (f == NULL) {
7920 err = got_error_from_errno("fopen");
7921 goto done;
7923 err = histedit_parse_list(histedit_cmds, f, repo);
7924 if (err)
7925 goto done;
7927 err = histedit_check_script(histedit_cmds, commits, repo);
7928 done:
7929 if (f && fclose(f) != 0 && err == NULL)
7930 err = got_error_from_errno("fclose");
7931 free(editor);
7932 return err;
7935 static const struct got_error *
7936 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7937 struct got_object_id_queue *, const char *, const char *,
7938 struct got_repository *);
7940 static const struct got_error *
7941 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7942 struct got_object_id_queue *commits, const char *branch_name,
7943 int edit_logmsg_only, struct got_repository *repo)
7945 const struct got_error *err;
7946 FILE *f = NULL;
7947 char *path = NULL;
7949 err = got_opentemp_named(&path, &f, "got-histedit");
7950 if (err)
7951 return err;
7953 err = write_cmd_list(f, branch_name, commits);
7954 if (err)
7955 goto done;
7957 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7958 if (err)
7959 goto done;
7961 if (edit_logmsg_only) {
7962 rewind(f);
7963 err = histedit_parse_list(histedit_cmds, f, repo);
7964 } else {
7965 if (fclose(f) != 0) {
7966 err = got_error_from_errno("fclose");
7967 goto done;
7969 f = NULL;
7970 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7971 if (err) {
7972 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7973 err->code != GOT_ERR_HISTEDIT_CMD)
7974 goto done;
7975 err = histedit_edit_list_retry(histedit_cmds, err,
7976 commits, path, branch_name, repo);
7979 done:
7980 if (f && fclose(f) != 0 && err == NULL)
7981 err = got_error_from_errno("fclose");
7982 if (path && unlink(path) != 0 && err == NULL)
7983 err = got_error_from_errno2("unlink", path);
7984 free(path);
7985 return err;
7988 static const struct got_error *
7989 histedit_save_list(struct got_histedit_list *histedit_cmds,
7990 struct got_worktree *worktree, struct got_repository *repo)
7992 const struct got_error *err = NULL;
7993 char *path = NULL;
7994 FILE *f = NULL;
7995 struct got_histedit_list_entry *hle;
7996 struct got_commit_object *commit = NULL;
7998 err = got_worktree_get_histedit_script_path(&path, worktree);
7999 if (err)
8000 return err;
8002 f = fopen(path, "w");
8003 if (f == NULL) {
8004 err = got_error_from_errno2("fopen", path);
8005 goto done;
8007 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8008 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8009 repo);
8010 if (err)
8011 break;
8013 if (hle->logmsg) {
8014 int n = fprintf(f, "%c %s\n",
8015 GOT_HISTEDIT_MESG, hle->logmsg);
8016 if (n < 0) {
8017 err = got_ferror(f, GOT_ERR_IO);
8018 break;
8022 done:
8023 if (f && fclose(f) != 0 && err == NULL)
8024 err = got_error_from_errno("fclose");
8025 free(path);
8026 if (commit)
8027 got_object_commit_close(commit);
8028 return err;
8031 void
8032 histedit_free_list(struct got_histedit_list *histedit_cmds)
8034 struct got_histedit_list_entry *hle;
8036 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8037 TAILQ_REMOVE(histedit_cmds, hle, entry);
8038 free(hle);
8042 static const struct got_error *
8043 histedit_load_list(struct got_histedit_list *histedit_cmds,
8044 const char *path, struct got_repository *repo)
8046 const struct got_error *err = NULL;
8047 FILE *f = NULL;
8049 f = fopen(path, "r");
8050 if (f == NULL) {
8051 err = got_error_from_errno2("fopen", path);
8052 goto done;
8055 err = histedit_parse_list(histedit_cmds, f, repo);
8056 done:
8057 if (f && fclose(f) != 0 && err == NULL)
8058 err = got_error_from_errno("fclose");
8059 return err;
8062 static const struct got_error *
8063 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8064 const struct got_error *edit_err, struct got_object_id_queue *commits,
8065 const char *path, const char *branch_name, struct got_repository *repo)
8067 const struct got_error *err = NULL, *prev_err = edit_err;
8068 int resp = ' ';
8070 while (resp != 'c' && resp != 'r' && resp != 'a') {
8071 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8072 "or (a)bort: ", getprogname(), prev_err->msg);
8073 resp = getchar();
8074 if (resp == '\n')
8075 resp = getchar();
8076 if (resp == 'c') {
8077 histedit_free_list(histedit_cmds);
8078 err = histedit_run_editor(histedit_cmds, path, commits,
8079 repo);
8080 if (err) {
8081 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8082 err->code != GOT_ERR_HISTEDIT_CMD)
8083 break;
8084 prev_err = err;
8085 resp = ' ';
8086 continue;
8088 break;
8089 } else if (resp == 'r') {
8090 histedit_free_list(histedit_cmds);
8091 err = histedit_edit_script(histedit_cmds,
8092 commits, branch_name, 0, repo);
8093 if (err) {
8094 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8095 err->code != GOT_ERR_HISTEDIT_CMD)
8096 break;
8097 prev_err = err;
8098 resp = ' ';
8099 continue;
8101 break;
8102 } else if (resp == 'a') {
8103 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8104 break;
8105 } else
8106 printf("invalid response '%c'\n", resp);
8109 return err;
8112 static const struct got_error *
8113 histedit_complete(struct got_worktree *worktree,
8114 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8115 struct got_reference *branch, struct got_repository *repo)
8117 printf("Switching work tree to %s\n",
8118 got_ref_get_symref_target(branch));
8119 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8120 branch, repo);
8123 static const struct got_error *
8124 show_histedit_progress(struct got_commit_object *commit,
8125 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8127 const struct got_error *err;
8128 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8130 err = got_object_id_str(&old_id_str, hle->commit_id);
8131 if (err)
8132 goto done;
8134 if (new_id) {
8135 err = got_object_id_str(&new_id_str, new_id);
8136 if (err)
8137 goto done;
8140 old_id_str[12] = '\0';
8141 if (new_id_str)
8142 new_id_str[12] = '\0';
8144 if (hle->logmsg) {
8145 logmsg = strdup(hle->logmsg);
8146 if (logmsg == NULL) {
8147 err = got_error_from_errno("strdup");
8148 goto done;
8150 trim_logmsg(logmsg, 42);
8151 } else {
8152 err = get_short_logmsg(&logmsg, 42, commit);
8153 if (err)
8154 goto done;
8157 switch (hle->cmd->code) {
8158 case GOT_HISTEDIT_PICK:
8159 case GOT_HISTEDIT_EDIT:
8160 printf("%s -> %s: %s\n", old_id_str,
8161 new_id_str ? new_id_str : "no-op change", logmsg);
8162 break;
8163 case GOT_HISTEDIT_DROP:
8164 case GOT_HISTEDIT_FOLD:
8165 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8166 logmsg);
8167 break;
8168 default:
8169 break;
8171 done:
8172 free(old_id_str);
8173 free(new_id_str);
8174 return err;
8177 static const struct got_error *
8178 histedit_commit(struct got_pathlist_head *merged_paths,
8179 struct got_worktree *worktree, struct got_fileindex *fileindex,
8180 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8181 struct got_repository *repo)
8183 const struct got_error *err;
8184 struct got_commit_object *commit;
8185 struct got_object_id *new_commit_id;
8187 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8188 && hle->logmsg == NULL) {
8189 err = histedit_edit_logmsg(hle, repo);
8190 if (err)
8191 return err;
8194 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8195 if (err)
8196 return err;
8198 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8199 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8200 hle->logmsg, repo);
8201 if (err) {
8202 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8203 goto done;
8204 err = show_histedit_progress(commit, hle, NULL);
8205 } else {
8206 err = show_histedit_progress(commit, hle, new_commit_id);
8207 free(new_commit_id);
8209 done:
8210 got_object_commit_close(commit);
8211 return err;
8214 static const struct got_error *
8215 histedit_skip_commit(struct got_histedit_list_entry *hle,
8216 struct got_worktree *worktree, struct got_repository *repo)
8218 const struct got_error *error;
8219 struct got_commit_object *commit;
8221 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8222 repo);
8223 if (error)
8224 return error;
8226 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8227 if (error)
8228 return error;
8230 error = show_histedit_progress(commit, hle, NULL);
8231 got_object_commit_close(commit);
8232 return error;
8235 static const struct got_error *
8236 check_local_changes(void *arg, unsigned char status,
8237 unsigned char staged_status, const char *path,
8238 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8239 struct got_object_id *commit_id, int dirfd, const char *de_name)
8241 int *have_local_changes = arg;
8243 switch (status) {
8244 case GOT_STATUS_ADD:
8245 case GOT_STATUS_DELETE:
8246 case GOT_STATUS_MODIFY:
8247 case GOT_STATUS_CONFLICT:
8248 *have_local_changes = 1;
8249 return got_error(GOT_ERR_CANCELLED);
8250 default:
8251 break;
8254 switch (staged_status) {
8255 case GOT_STATUS_ADD:
8256 case GOT_STATUS_DELETE:
8257 case GOT_STATUS_MODIFY:
8258 *have_local_changes = 1;
8259 return got_error(GOT_ERR_CANCELLED);
8260 default:
8261 break;
8264 return NULL;
8267 static const struct got_error *
8268 cmd_histedit(int argc, char *argv[])
8270 const struct got_error *error = NULL;
8271 struct got_worktree *worktree = NULL;
8272 struct got_fileindex *fileindex = NULL;
8273 struct got_repository *repo = NULL;
8274 char *cwd = NULL;
8275 struct got_reference *branch = NULL;
8276 struct got_reference *tmp_branch = NULL;
8277 struct got_object_id *resume_commit_id = NULL;
8278 struct got_object_id *base_commit_id = NULL;
8279 struct got_object_id *head_commit_id = NULL;
8280 struct got_commit_object *commit = NULL;
8281 int ch, rebase_in_progress = 0;
8282 struct got_update_progress_arg upa;
8283 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8284 int edit_logmsg_only = 0;
8285 const char *edit_script_path = NULL;
8286 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8287 struct got_object_id_queue commits;
8288 struct got_pathlist_head merged_paths;
8289 const struct got_object_id_queue *parent_ids;
8290 struct got_object_qid *pid;
8291 struct got_histedit_list histedit_cmds;
8292 struct got_histedit_list_entry *hle;
8294 SIMPLEQ_INIT(&commits);
8295 TAILQ_INIT(&histedit_cmds);
8296 TAILQ_INIT(&merged_paths);
8297 memset(&upa, 0, sizeof(upa));
8299 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8300 switch (ch) {
8301 case 'a':
8302 abort_edit = 1;
8303 break;
8304 case 'c':
8305 continue_edit = 1;
8306 break;
8307 case 'F':
8308 edit_script_path = optarg;
8309 break;
8310 case 'm':
8311 edit_logmsg_only = 1;
8312 break;
8313 default:
8314 usage_histedit();
8315 /* NOTREACHED */
8319 argc -= optind;
8320 argv += optind;
8322 #ifndef PROFILE
8323 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8324 "unveil", NULL) == -1)
8325 err(1, "pledge");
8326 #endif
8327 if (abort_edit && continue_edit)
8328 errx(1, "histedit's -a and -c options are mutually exclusive");
8329 if (edit_script_path && edit_logmsg_only)
8330 errx(1, "histedit's -F and -m options are mutually exclusive");
8331 if (abort_edit && edit_logmsg_only)
8332 errx(1, "histedit's -a and -m options are mutually exclusive");
8333 if (continue_edit && edit_logmsg_only)
8334 errx(1, "histedit's -c and -m options are mutually exclusive");
8335 if (argc != 0)
8336 usage_histedit();
8339 * This command cannot apply unveil(2) in all cases because the
8340 * user may choose to run an editor to edit the histedit script
8341 * and to edit individual commit log messages.
8342 * unveil(2) traverses exec(2); if an editor is used we have to
8343 * apply unveil after edit script and log messages have been written.
8344 * XXX TODO: Make use of unveil(2) where possible.
8347 cwd = getcwd(NULL, 0);
8348 if (cwd == NULL) {
8349 error = got_error_from_errno("getcwd");
8350 goto done;
8352 error = got_worktree_open(&worktree, cwd);
8353 if (error) {
8354 if (error->code == GOT_ERR_NOT_WORKTREE)
8355 error = wrap_not_worktree_error(error, "histedit", cwd);
8356 goto done;
8359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8360 NULL);
8361 if (error != NULL)
8362 goto done;
8364 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8365 if (error)
8366 goto done;
8367 if (rebase_in_progress) {
8368 error = got_error(GOT_ERR_REBASING);
8369 goto done;
8372 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8373 if (error)
8374 goto done;
8376 if (edit_in_progress && edit_logmsg_only) {
8377 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8378 "histedit operation is in progress in this "
8379 "work tree and must be continued or aborted "
8380 "before the -m option can be used");
8381 goto done;
8384 if (edit_in_progress && abort_edit) {
8385 error = got_worktree_histedit_continue(&resume_commit_id,
8386 &tmp_branch, &branch, &base_commit_id, &fileindex,
8387 worktree, repo);
8388 if (error)
8389 goto done;
8390 printf("Switching work tree to %s\n",
8391 got_ref_get_symref_target(branch));
8392 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8393 branch, base_commit_id, update_progress, &upa);
8394 if (error)
8395 goto done;
8396 printf("Histedit of %s aborted\n",
8397 got_ref_get_symref_target(branch));
8398 print_update_progress_stats(&upa);
8399 goto done; /* nothing else to do */
8400 } else if (abort_edit) {
8401 error = got_error(GOT_ERR_NOT_HISTEDIT);
8402 goto done;
8405 if (continue_edit) {
8406 char *path;
8408 if (!edit_in_progress) {
8409 error = got_error(GOT_ERR_NOT_HISTEDIT);
8410 goto done;
8413 error = got_worktree_get_histedit_script_path(&path, worktree);
8414 if (error)
8415 goto done;
8417 error = histedit_load_list(&histedit_cmds, path, repo);
8418 free(path);
8419 if (error)
8420 goto done;
8422 error = got_worktree_histedit_continue(&resume_commit_id,
8423 &tmp_branch, &branch, &base_commit_id, &fileindex,
8424 worktree, repo);
8425 if (error)
8426 goto done;
8428 error = got_ref_resolve(&head_commit_id, repo, branch);
8429 if (error)
8430 goto done;
8432 error = got_object_open_as_commit(&commit, repo,
8433 head_commit_id);
8434 if (error)
8435 goto done;
8436 parent_ids = got_object_commit_get_parent_ids(commit);
8437 pid = SIMPLEQ_FIRST(parent_ids);
8438 if (pid == NULL) {
8439 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8440 goto done;
8442 error = collect_commits(&commits, head_commit_id, pid->id,
8443 base_commit_id, got_worktree_get_path_prefix(worktree),
8444 GOT_ERR_HISTEDIT_PATH, repo);
8445 got_object_commit_close(commit);
8446 commit = NULL;
8447 if (error)
8448 goto done;
8449 } else {
8450 if (edit_in_progress) {
8451 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8452 goto done;
8455 error = got_ref_open(&branch, repo,
8456 got_worktree_get_head_ref_name(worktree), 0);
8457 if (error != NULL)
8458 goto done;
8460 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8461 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8462 "will not edit commit history of a branch outside "
8463 "the \"refs/heads/\" reference namespace");
8464 goto done;
8467 error = got_ref_resolve(&head_commit_id, repo, branch);
8468 got_ref_close(branch);
8469 branch = NULL;
8470 if (error)
8471 goto done;
8473 error = got_object_open_as_commit(&commit, repo,
8474 head_commit_id);
8475 if (error)
8476 goto done;
8477 parent_ids = got_object_commit_get_parent_ids(commit);
8478 pid = SIMPLEQ_FIRST(parent_ids);
8479 if (pid == NULL) {
8480 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8481 goto done;
8483 error = collect_commits(&commits, head_commit_id, pid->id,
8484 got_worktree_get_base_commit_id(worktree),
8485 got_worktree_get_path_prefix(worktree),
8486 GOT_ERR_HISTEDIT_PATH, repo);
8487 got_object_commit_close(commit);
8488 commit = NULL;
8489 if (error)
8490 goto done;
8492 if (SIMPLEQ_EMPTY(&commits)) {
8493 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8494 goto done;
8497 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8498 &base_commit_id, &fileindex, worktree, repo);
8499 if (error)
8500 goto done;
8502 if (edit_script_path) {
8503 error = histedit_load_list(&histedit_cmds,
8504 edit_script_path, repo);
8505 if (error) {
8506 got_worktree_histedit_abort(worktree, fileindex,
8507 repo, branch, base_commit_id,
8508 update_progress, &upa);
8509 print_update_progress_stats(&upa);
8510 goto done;
8512 } else {
8513 const char *branch_name;
8514 branch_name = got_ref_get_symref_target(branch);
8515 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8516 branch_name += 11;
8517 error = histedit_edit_script(&histedit_cmds, &commits,
8518 branch_name, edit_logmsg_only, repo);
8519 if (error) {
8520 got_worktree_histedit_abort(worktree, fileindex,
8521 repo, branch, base_commit_id,
8522 update_progress, &upa);
8523 print_update_progress_stats(&upa);
8524 goto done;
8529 error = histedit_save_list(&histedit_cmds, worktree,
8530 repo);
8531 if (error) {
8532 got_worktree_histedit_abort(worktree, fileindex,
8533 repo, branch, base_commit_id,
8534 update_progress, &upa);
8535 print_update_progress_stats(&upa);
8536 goto done;
8541 error = histedit_check_script(&histedit_cmds, &commits, repo);
8542 if (error)
8543 goto done;
8545 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8546 if (resume_commit_id) {
8547 if (got_object_id_cmp(hle->commit_id,
8548 resume_commit_id) != 0)
8549 continue;
8551 resume_commit_id = NULL;
8552 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8553 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8554 error = histedit_skip_commit(hle, worktree,
8555 repo);
8556 if (error)
8557 goto done;
8558 } else {
8559 struct got_pathlist_head paths;
8560 int have_changes = 0;
8562 TAILQ_INIT(&paths);
8563 error = got_pathlist_append(&paths, "", NULL);
8564 if (error)
8565 goto done;
8566 error = got_worktree_status(worktree, &paths,
8567 repo, check_local_changes, &have_changes,
8568 check_cancelled, NULL);
8569 got_pathlist_free(&paths);
8570 if (error) {
8571 if (error->code != GOT_ERR_CANCELLED)
8572 goto done;
8573 if (sigint_received || sigpipe_received)
8574 goto done;
8576 if (have_changes) {
8577 error = histedit_commit(NULL, worktree,
8578 fileindex, tmp_branch, hle, repo);
8579 if (error)
8580 goto done;
8581 } else {
8582 error = got_object_open_as_commit(
8583 &commit, repo, hle->commit_id);
8584 if (error)
8585 goto done;
8586 error = show_histedit_progress(commit,
8587 hle, NULL);
8588 got_object_commit_close(commit);
8589 commit = NULL;
8590 if (error)
8591 goto done;
8594 continue;
8597 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8598 error = histedit_skip_commit(hle, worktree, repo);
8599 if (error)
8600 goto done;
8601 continue;
8604 error = got_object_open_as_commit(&commit, repo,
8605 hle->commit_id);
8606 if (error)
8607 goto done;
8608 parent_ids = got_object_commit_get_parent_ids(commit);
8609 pid = SIMPLEQ_FIRST(parent_ids);
8611 error = got_worktree_histedit_merge_files(&merged_paths,
8612 worktree, fileindex, pid->id, hle->commit_id, repo,
8613 update_progress, &upa, check_cancelled, NULL);
8614 if (error)
8615 goto done;
8616 got_object_commit_close(commit);
8617 commit = NULL;
8619 print_update_progress_stats(&upa);
8620 if (upa.conflicts > 0)
8621 rebase_status = GOT_STATUS_CONFLICT;
8623 if (rebase_status == GOT_STATUS_CONFLICT) {
8624 error = show_rebase_merge_conflict(hle->commit_id,
8625 repo);
8626 if (error)
8627 goto done;
8628 got_worktree_rebase_pathlist_free(&merged_paths);
8629 break;
8632 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8633 char *id_str;
8634 error = got_object_id_str(&id_str, hle->commit_id);
8635 if (error)
8636 goto done;
8637 printf("Stopping histedit for amending commit %s\n",
8638 id_str);
8639 free(id_str);
8640 got_worktree_rebase_pathlist_free(&merged_paths);
8641 error = got_worktree_histedit_postpone(worktree,
8642 fileindex);
8643 goto done;
8646 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8647 error = histedit_skip_commit(hle, worktree, repo);
8648 if (error)
8649 goto done;
8650 continue;
8653 error = histedit_commit(&merged_paths, worktree, fileindex,
8654 tmp_branch, hle, repo);
8655 got_worktree_rebase_pathlist_free(&merged_paths);
8656 if (error)
8657 goto done;
8660 if (rebase_status == GOT_STATUS_CONFLICT) {
8661 error = got_worktree_histedit_postpone(worktree, fileindex);
8662 if (error)
8663 goto done;
8664 error = got_error_msg(GOT_ERR_CONFLICTS,
8665 "conflicts must be resolved before histedit can continue");
8666 } else
8667 error = histedit_complete(worktree, fileindex, tmp_branch,
8668 branch, repo);
8669 done:
8670 got_object_id_queue_free(&commits);
8671 histedit_free_list(&histedit_cmds);
8672 free(head_commit_id);
8673 free(base_commit_id);
8674 free(resume_commit_id);
8675 if (commit)
8676 got_object_commit_close(commit);
8677 if (branch)
8678 got_ref_close(branch);
8679 if (tmp_branch)
8680 got_ref_close(tmp_branch);
8681 if (worktree)
8682 got_worktree_close(worktree);
8683 if (repo)
8684 got_repo_close(repo);
8685 return error;
8688 __dead static void
8689 usage_integrate(void)
8691 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8692 exit(1);
8695 static const struct got_error *
8696 cmd_integrate(int argc, char *argv[])
8698 const struct got_error *error = NULL;
8699 struct got_repository *repo = NULL;
8700 struct got_worktree *worktree = NULL;
8701 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8702 const char *branch_arg = NULL;
8703 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8704 struct got_fileindex *fileindex = NULL;
8705 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8706 int ch;
8707 struct got_update_progress_arg upa;
8709 while ((ch = getopt(argc, argv, "")) != -1) {
8710 switch (ch) {
8711 default:
8712 usage_integrate();
8713 /* NOTREACHED */
8717 argc -= optind;
8718 argv += optind;
8720 if (argc != 1)
8721 usage_integrate();
8722 branch_arg = argv[0];
8724 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8725 "unveil", NULL) == -1)
8726 err(1, "pledge");
8728 cwd = getcwd(NULL, 0);
8729 if (cwd == NULL) {
8730 error = got_error_from_errno("getcwd");
8731 goto done;
8734 error = got_worktree_open(&worktree, cwd);
8735 if (error) {
8736 if (error->code == GOT_ERR_NOT_WORKTREE)
8737 error = wrap_not_worktree_error(error, "integrate",
8738 cwd);
8739 goto done;
8742 error = check_rebase_or_histedit_in_progress(worktree);
8743 if (error)
8744 goto done;
8746 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8747 NULL);
8748 if (error != NULL)
8749 goto done;
8751 error = apply_unveil(got_repo_get_path(repo), 0,
8752 got_worktree_get_root_path(worktree));
8753 if (error)
8754 goto done;
8756 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8757 error = got_error_from_errno("asprintf");
8758 goto done;
8761 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8762 &base_branch_ref, worktree, refname, repo);
8763 if (error)
8764 goto done;
8766 refname = strdup(got_ref_get_name(branch_ref));
8767 if (refname == NULL) {
8768 error = got_error_from_errno("strdup");
8769 got_worktree_integrate_abort(worktree, fileindex, repo,
8770 branch_ref, base_branch_ref);
8771 goto done;
8773 base_refname = strdup(got_ref_get_name(base_branch_ref));
8774 if (base_refname == NULL) {
8775 error = got_error_from_errno("strdup");
8776 got_worktree_integrate_abort(worktree, fileindex, repo,
8777 branch_ref, base_branch_ref);
8778 goto done;
8781 error = got_ref_resolve(&commit_id, repo, branch_ref);
8782 if (error)
8783 goto done;
8785 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8786 if (error)
8787 goto done;
8789 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8790 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8791 "specified branch has already been integrated");
8792 got_worktree_integrate_abort(worktree, fileindex, repo,
8793 branch_ref, base_branch_ref);
8794 goto done;
8797 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8798 if (error) {
8799 if (error->code == GOT_ERR_ANCESTRY)
8800 error = got_error(GOT_ERR_REBASE_REQUIRED);
8801 got_worktree_integrate_abort(worktree, fileindex, repo,
8802 branch_ref, base_branch_ref);
8803 goto done;
8806 memset(&upa, 0, sizeof(upa));
8807 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8808 branch_ref, base_branch_ref, update_progress, &upa,
8809 check_cancelled, NULL);
8810 if (error)
8811 goto done;
8813 printf("Integrated %s into %s\n", refname, base_refname);
8814 print_update_progress_stats(&upa);
8815 done:
8816 if (repo)
8817 got_repo_close(repo);
8818 if (worktree)
8819 got_worktree_close(worktree);
8820 free(cwd);
8821 free(base_commit_id);
8822 free(commit_id);
8823 free(refname);
8824 free(base_refname);
8825 return error;
8828 __dead static void
8829 usage_stage(void)
8831 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8832 "[-S] [file-path ...]\n",
8833 getprogname());
8834 exit(1);
8837 static const struct got_error *
8838 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8839 const char *path, struct got_object_id *blob_id,
8840 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8841 int dirfd, const char *de_name)
8843 const struct got_error *err = NULL;
8844 char *id_str = NULL;
8846 if (staged_status != GOT_STATUS_ADD &&
8847 staged_status != GOT_STATUS_MODIFY &&
8848 staged_status != GOT_STATUS_DELETE)
8849 return NULL;
8851 if (staged_status == GOT_STATUS_ADD ||
8852 staged_status == GOT_STATUS_MODIFY)
8853 err = got_object_id_str(&id_str, staged_blob_id);
8854 else
8855 err = got_object_id_str(&id_str, blob_id);
8856 if (err)
8857 return err;
8859 printf("%s %c %s\n", id_str, staged_status, path);
8860 free(id_str);
8861 return NULL;
8864 static const struct got_error *
8865 cmd_stage(int argc, char *argv[])
8867 const struct got_error *error = NULL;
8868 struct got_repository *repo = NULL;
8869 struct got_worktree *worktree = NULL;
8870 char *cwd = NULL;
8871 struct got_pathlist_head paths;
8872 struct got_pathlist_entry *pe;
8873 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8874 FILE *patch_script_file = NULL;
8875 const char *patch_script_path = NULL;
8876 struct choose_patch_arg cpa;
8878 TAILQ_INIT(&paths);
8880 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8881 switch (ch) {
8882 case 'l':
8883 list_stage = 1;
8884 break;
8885 case 'p':
8886 pflag = 1;
8887 break;
8888 case 'F':
8889 patch_script_path = optarg;
8890 break;
8891 case 'S':
8892 allow_bad_symlinks = 1;
8893 break;
8894 default:
8895 usage_stage();
8896 /* NOTREACHED */
8900 argc -= optind;
8901 argv += optind;
8903 #ifndef PROFILE
8904 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8905 "unveil", NULL) == -1)
8906 err(1, "pledge");
8907 #endif
8908 if (list_stage && (pflag || patch_script_path))
8909 errx(1, "-l option cannot be used with other options");
8910 if (patch_script_path && !pflag)
8911 errx(1, "-F option can only be used together with -p option");
8913 cwd = getcwd(NULL, 0);
8914 if (cwd == NULL) {
8915 error = got_error_from_errno("getcwd");
8916 goto done;
8919 error = got_worktree_open(&worktree, cwd);
8920 if (error) {
8921 if (error->code == GOT_ERR_NOT_WORKTREE)
8922 error = wrap_not_worktree_error(error, "stage", cwd);
8923 goto done;
8926 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8927 NULL);
8928 if (error != NULL)
8929 goto done;
8931 if (patch_script_path) {
8932 patch_script_file = fopen(patch_script_path, "r");
8933 if (patch_script_file == NULL) {
8934 error = got_error_from_errno2("fopen",
8935 patch_script_path);
8936 goto done;
8939 error = apply_unveil(got_repo_get_path(repo), 0,
8940 got_worktree_get_root_path(worktree));
8941 if (error)
8942 goto done;
8944 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8945 if (error)
8946 goto done;
8948 if (list_stage)
8949 error = got_worktree_status(worktree, &paths, repo,
8950 print_stage, NULL, check_cancelled, NULL);
8951 else {
8952 cpa.patch_script_file = patch_script_file;
8953 cpa.action = "stage";
8954 error = got_worktree_stage(worktree, &paths,
8955 pflag ? NULL : print_status, NULL,
8956 pflag ? choose_patch : NULL, &cpa,
8957 allow_bad_symlinks, repo);
8959 done:
8960 if (patch_script_file && fclose(patch_script_file) == EOF &&
8961 error == NULL)
8962 error = got_error_from_errno2("fclose", patch_script_path);
8963 if (repo)
8964 got_repo_close(repo);
8965 if (worktree)
8966 got_worktree_close(worktree);
8967 TAILQ_FOREACH(pe, &paths, entry)
8968 free((char *)pe->path);
8969 got_pathlist_free(&paths);
8970 free(cwd);
8971 return error;
8974 __dead static void
8975 usage_unstage(void)
8977 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8978 "[file-path ...]\n",
8979 getprogname());
8980 exit(1);
8984 static const struct got_error *
8985 cmd_unstage(int argc, char *argv[])
8987 const struct got_error *error = NULL;
8988 struct got_repository *repo = NULL;
8989 struct got_worktree *worktree = NULL;
8990 char *cwd = NULL;
8991 struct got_pathlist_head paths;
8992 struct got_pathlist_entry *pe;
8993 int ch, pflag = 0;
8994 struct got_update_progress_arg upa;
8995 FILE *patch_script_file = NULL;
8996 const char *patch_script_path = NULL;
8997 struct choose_patch_arg cpa;
8999 TAILQ_INIT(&paths);
9001 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9002 switch (ch) {
9003 case 'p':
9004 pflag = 1;
9005 break;
9006 case 'F':
9007 patch_script_path = optarg;
9008 break;
9009 default:
9010 usage_unstage();
9011 /* NOTREACHED */
9015 argc -= optind;
9016 argv += optind;
9018 #ifndef PROFILE
9019 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9020 "unveil", NULL) == -1)
9021 err(1, "pledge");
9022 #endif
9023 if (patch_script_path && !pflag)
9024 errx(1, "-F option can only be used together with -p option");
9026 cwd = getcwd(NULL, 0);
9027 if (cwd == NULL) {
9028 error = got_error_from_errno("getcwd");
9029 goto done;
9032 error = got_worktree_open(&worktree, cwd);
9033 if (error) {
9034 if (error->code == GOT_ERR_NOT_WORKTREE)
9035 error = wrap_not_worktree_error(error, "unstage", cwd);
9036 goto done;
9039 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9040 NULL);
9041 if (error != NULL)
9042 goto done;
9044 if (patch_script_path) {
9045 patch_script_file = fopen(patch_script_path, "r");
9046 if (patch_script_file == NULL) {
9047 error = got_error_from_errno2("fopen",
9048 patch_script_path);
9049 goto done;
9053 error = apply_unveil(got_repo_get_path(repo), 0,
9054 got_worktree_get_root_path(worktree));
9055 if (error)
9056 goto done;
9058 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9059 if (error)
9060 goto done;
9062 cpa.patch_script_file = patch_script_file;
9063 cpa.action = "unstage";
9064 memset(&upa, 0, sizeof(upa));
9065 error = got_worktree_unstage(worktree, &paths, update_progress,
9066 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9067 if (!error)
9068 print_update_progress_stats(&upa);
9069 done:
9070 if (patch_script_file && fclose(patch_script_file) == EOF &&
9071 error == NULL)
9072 error = got_error_from_errno2("fclose", patch_script_path);
9073 if (repo)
9074 got_repo_close(repo);
9075 if (worktree)
9076 got_worktree_close(worktree);
9077 TAILQ_FOREACH(pe, &paths, entry)
9078 free((char *)pe->path);
9079 got_pathlist_free(&paths);
9080 free(cwd);
9081 return error;
9084 __dead static void
9085 usage_cat(void)
9087 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9088 "arg1 [arg2 ...]\n", getprogname());
9089 exit(1);
9092 static const struct got_error *
9093 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9095 const struct got_error *err;
9096 struct got_blob_object *blob;
9098 err = got_object_open_as_blob(&blob, repo, id, 8192);
9099 if (err)
9100 return err;
9102 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9103 got_object_blob_close(blob);
9104 return err;
9107 static const struct got_error *
9108 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9110 const struct got_error *err;
9111 struct got_tree_object *tree;
9112 int nentries, i;
9114 err = got_object_open_as_tree(&tree, repo, id);
9115 if (err)
9116 return err;
9118 nentries = got_object_tree_get_nentries(tree);
9119 for (i = 0; i < nentries; i++) {
9120 struct got_tree_entry *te;
9121 char *id_str;
9122 if (sigint_received || sigpipe_received)
9123 break;
9124 te = got_object_tree_get_entry(tree, i);
9125 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9126 if (err)
9127 break;
9128 fprintf(outfile, "%s %.7o %s\n", id_str,
9129 got_tree_entry_get_mode(te),
9130 got_tree_entry_get_name(te));
9131 free(id_str);
9134 got_object_tree_close(tree);
9135 return err;
9138 static const struct got_error *
9139 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9141 const struct got_error *err;
9142 struct got_commit_object *commit;
9143 const struct got_object_id_queue *parent_ids;
9144 struct got_object_qid *pid;
9145 char *id_str = NULL;
9146 const char *logmsg = NULL;
9148 err = got_object_open_as_commit(&commit, repo, id);
9149 if (err)
9150 return err;
9152 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9153 if (err)
9154 goto done;
9156 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9157 parent_ids = got_object_commit_get_parent_ids(commit);
9158 fprintf(outfile, "numparents %d\n",
9159 got_object_commit_get_nparents(commit));
9160 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9161 char *pid_str;
9162 err = got_object_id_str(&pid_str, pid->id);
9163 if (err)
9164 goto done;
9165 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9166 free(pid_str);
9168 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9169 got_object_commit_get_author(commit),
9170 got_object_commit_get_author_time(commit));
9172 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9173 got_object_commit_get_author(commit),
9174 got_object_commit_get_committer_time(commit));
9176 logmsg = got_object_commit_get_logmsg_raw(commit);
9177 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9178 fprintf(outfile, "%s", logmsg);
9179 done:
9180 free(id_str);
9181 got_object_commit_close(commit);
9182 return err;
9185 static const struct got_error *
9186 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9188 const struct got_error *err;
9189 struct got_tag_object *tag;
9190 char *id_str = NULL;
9191 const char *tagmsg = NULL;
9193 err = got_object_open_as_tag(&tag, repo, id);
9194 if (err)
9195 return err;
9197 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9198 if (err)
9199 goto done;
9201 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9203 switch (got_object_tag_get_object_type(tag)) {
9204 case GOT_OBJ_TYPE_BLOB:
9205 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9206 GOT_OBJ_LABEL_BLOB);
9207 break;
9208 case GOT_OBJ_TYPE_TREE:
9209 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9210 GOT_OBJ_LABEL_TREE);
9211 break;
9212 case GOT_OBJ_TYPE_COMMIT:
9213 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9214 GOT_OBJ_LABEL_COMMIT);
9215 break;
9216 case GOT_OBJ_TYPE_TAG:
9217 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9218 GOT_OBJ_LABEL_TAG);
9219 break;
9220 default:
9221 break;
9224 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9225 got_object_tag_get_name(tag));
9227 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9228 got_object_tag_get_tagger(tag),
9229 got_object_tag_get_tagger_time(tag));
9231 tagmsg = got_object_tag_get_message(tag);
9232 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9233 fprintf(outfile, "%s", tagmsg);
9234 done:
9235 free(id_str);
9236 got_object_tag_close(tag);
9237 return err;
9240 static const struct got_error *
9241 cmd_cat(int argc, char *argv[])
9243 const struct got_error *error;
9244 struct got_repository *repo = NULL;
9245 struct got_worktree *worktree = NULL;
9246 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9247 const char *commit_id_str = NULL;
9248 struct got_object_id *id = NULL, *commit_id = NULL;
9249 int ch, obj_type, i, force_path = 0;
9251 #ifndef PROFILE
9252 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9253 NULL) == -1)
9254 err(1, "pledge");
9255 #endif
9257 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9258 switch (ch) {
9259 case 'c':
9260 commit_id_str = optarg;
9261 break;
9262 case 'r':
9263 repo_path = realpath(optarg, NULL);
9264 if (repo_path == NULL)
9265 return got_error_from_errno2("realpath",
9266 optarg);
9267 got_path_strip_trailing_slashes(repo_path);
9268 break;
9269 case 'P':
9270 force_path = 1;
9271 break;
9272 default:
9273 usage_cat();
9274 /* NOTREACHED */
9278 argc -= optind;
9279 argv += optind;
9281 cwd = getcwd(NULL, 0);
9282 if (cwd == NULL) {
9283 error = got_error_from_errno("getcwd");
9284 goto done;
9286 error = got_worktree_open(&worktree, cwd);
9287 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9288 goto done;
9289 if (worktree) {
9290 if (repo_path == NULL) {
9291 repo_path = strdup(
9292 got_worktree_get_repo_path(worktree));
9293 if (repo_path == NULL) {
9294 error = got_error_from_errno("strdup");
9295 goto done;
9300 if (repo_path == NULL) {
9301 repo_path = getcwd(NULL, 0);
9302 if (repo_path == NULL)
9303 return got_error_from_errno("getcwd");
9306 error = got_repo_open(&repo, repo_path, NULL);
9307 free(repo_path);
9308 if (error != NULL)
9309 goto done;
9311 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9312 if (error)
9313 goto done;
9315 if (commit_id_str == NULL)
9316 commit_id_str = GOT_REF_HEAD;
9317 error = got_repo_match_object_id(&commit_id, NULL,
9318 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9319 if (error)
9320 goto done;
9322 for (i = 0; i < argc; i++) {
9323 if (force_path) {
9324 error = got_object_id_by_path(&id, repo, commit_id,
9325 argv[i]);
9326 if (error)
9327 break;
9328 } else {
9329 error = got_repo_match_object_id(&id, &label, argv[i],
9330 GOT_OBJ_TYPE_ANY, 0, repo);
9331 if (error) {
9332 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9333 error->code != GOT_ERR_NOT_REF)
9334 break;
9335 error = got_object_id_by_path(&id, repo,
9336 commit_id, argv[i]);
9337 if (error)
9338 break;
9342 error = got_object_get_type(&obj_type, repo, id);
9343 if (error)
9344 break;
9346 switch (obj_type) {
9347 case GOT_OBJ_TYPE_BLOB:
9348 error = cat_blob(id, repo, stdout);
9349 break;
9350 case GOT_OBJ_TYPE_TREE:
9351 error = cat_tree(id, repo, stdout);
9352 break;
9353 case GOT_OBJ_TYPE_COMMIT:
9354 error = cat_commit(id, repo, stdout);
9355 break;
9356 case GOT_OBJ_TYPE_TAG:
9357 error = cat_tag(id, repo, stdout);
9358 break;
9359 default:
9360 error = got_error(GOT_ERR_OBJ_TYPE);
9361 break;
9363 if (error)
9364 break;
9365 free(label);
9366 label = NULL;
9367 free(id);
9368 id = NULL;
9370 done:
9371 free(label);
9372 free(id);
9373 free(commit_id);
9374 if (worktree)
9375 got_worktree_close(worktree);
9376 if (repo) {
9377 const struct got_error *repo_error;
9378 repo_error = got_repo_close(repo);
9379 if (error == NULL)
9380 error = repo_error;
9382 return error;
9385 __dead static void
9386 usage_info(void)
9388 fprintf(stderr, "usage: %s info [path ...]\n",
9389 getprogname());
9390 exit(1);
9393 static const struct got_error *
9394 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9396 struct got_object_id *commit_id)
9398 const struct got_error *err = NULL;
9399 char *id_str = NULL;
9400 char datebuf[128];
9401 struct tm mytm, *tm;
9402 struct got_pathlist_head *paths = arg;
9403 struct got_pathlist_entry *pe;
9406 * Clear error indication from any of the path arguments which
9407 * would cause this file index entry to be displayed.
9409 TAILQ_FOREACH(pe, paths, entry) {
9410 if (got_path_cmp(path, pe->path, strlen(path),
9411 pe->path_len) == 0 ||
9412 got_path_is_child(path, pe->path, pe->path_len))
9413 pe->data = NULL; /* no error */
9416 printf(GOT_COMMIT_SEP_STR);
9417 if (S_ISLNK(mode))
9418 printf("symlink: %s\n", path);
9419 else if (S_ISREG(mode)) {
9420 printf("file: %s\n", path);
9421 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9422 } else if (S_ISDIR(mode))
9423 printf("directory: %s\n", path);
9424 else
9425 printf("something: %s\n", path);
9427 tm = localtime_r(&mtime, &mytm);
9428 if (tm == NULL)
9429 return NULL;
9430 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9431 return got_error(GOT_ERR_NO_SPACE);
9432 printf("timestamp: %s\n", datebuf);
9434 if (blob_id) {
9435 err = got_object_id_str(&id_str, blob_id);
9436 if (err)
9437 return err;
9438 printf("based on blob: %s\n", id_str);
9439 free(id_str);
9442 if (staged_blob_id) {
9443 err = got_object_id_str(&id_str, staged_blob_id);
9444 if (err)
9445 return err;
9446 printf("based on staged blob: %s\n", id_str);
9447 free(id_str);
9450 if (commit_id) {
9451 err = got_object_id_str(&id_str, commit_id);
9452 if (err)
9453 return err;
9454 printf("based on commit: %s\n", id_str);
9455 free(id_str);
9458 return NULL;
9461 static const struct got_error *
9462 cmd_info(int argc, char *argv[])
9464 const struct got_error *error = NULL;
9465 struct got_worktree *worktree = NULL;
9466 char *cwd = NULL, *id_str = NULL;
9467 struct got_pathlist_head paths;
9468 struct got_pathlist_entry *pe;
9469 char *uuidstr = NULL;
9470 int ch, show_files = 0;
9472 TAILQ_INIT(&paths);
9474 while ((ch = getopt(argc, argv, "")) != -1) {
9475 switch (ch) {
9476 default:
9477 usage_info();
9478 /* NOTREACHED */
9482 argc -= optind;
9483 argv += optind;
9485 #ifndef PROFILE
9486 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9487 NULL) == -1)
9488 err(1, "pledge");
9489 #endif
9490 cwd = getcwd(NULL, 0);
9491 if (cwd == NULL) {
9492 error = got_error_from_errno("getcwd");
9493 goto done;
9496 error = got_worktree_open(&worktree, cwd);
9497 if (error) {
9498 if (error->code == GOT_ERR_NOT_WORKTREE)
9499 error = wrap_not_worktree_error(error, "status", cwd);
9500 goto done;
9503 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9504 if (error)
9505 goto done;
9507 if (argc >= 1) {
9508 error = get_worktree_paths_from_argv(&paths, argc, argv,
9509 worktree);
9510 if (error)
9511 goto done;
9512 show_files = 1;
9515 error = got_object_id_str(&id_str,
9516 got_worktree_get_base_commit_id(worktree));
9517 if (error)
9518 goto done;
9520 error = got_worktree_get_uuid(&uuidstr, worktree);
9521 if (error)
9522 goto done;
9524 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9525 printf("work tree base commit: %s\n", id_str);
9526 printf("work tree path prefix: %s\n",
9527 got_worktree_get_path_prefix(worktree));
9528 printf("work tree branch reference: %s\n",
9529 got_worktree_get_head_ref_name(worktree));
9530 printf("work tree UUID: %s\n", uuidstr);
9531 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9533 if (show_files) {
9534 struct got_pathlist_entry *pe;
9535 TAILQ_FOREACH(pe, &paths, entry) {
9536 if (pe->path_len == 0)
9537 continue;
9539 * Assume this path will fail. This will be corrected
9540 * in print_path_info() in case the path does suceeed.
9542 pe->data = (void *)got_error_path(pe->path,
9543 GOT_ERR_BAD_PATH);
9545 error = got_worktree_path_info(worktree, &paths,
9546 print_path_info, &paths, check_cancelled, NULL);
9547 if (error)
9548 goto done;
9549 TAILQ_FOREACH(pe, &paths, entry) {
9550 if (pe->data != NULL) {
9551 error = pe->data; /* bad path */
9552 break;
9556 done:
9557 TAILQ_FOREACH(pe, &paths, entry)
9558 free((char *)pe->path);
9559 got_pathlist_free(&paths);
9560 free(cwd);
9561 free(id_str);
9562 free(uuidstr);
9563 return error;