Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
155 };
157 static void
158 list_commands(void)
160 int i;
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
167 fputc('\n', stderr);
170 int
171 main(int argc, char *argv[])
173 struct got_cmd *cmd;
174 unsigned int i;
175 int ch;
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
181 switch (ch) {
182 case 'h':
183 hflag = 1;
184 break;
185 case 'V':
186 Vflag = 1;
187 break;
188 default:
189 usage(hflag);
190 /* NOTREACHED */
194 argc -= optind;
195 argv += optind;
196 optind = 0;
198 if (Vflag) {
199 got_version_print_str();
200 return 1;
203 if (argc <= 0)
204 usage(hflag);
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 continue;
218 if (hflag)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && error->code != GOT_ERR_CANCELLED &&
223 error->code != GOT_ERR_PRIVSEP_EXIT &&
224 !(sigpipe_received &&
225 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
226 !(sigint_received &&
227 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
228 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
229 return 1;
232 return 0;
235 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
236 list_commands();
237 return 1;
240 __dead static void
241 usage(int hflag)
243 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
244 getprogname());
245 if (hflag)
246 list_commands();
247 exit(1);
250 static const struct got_error *
251 get_editor(char **abspath)
253 const struct got_error *err = NULL;
254 const char *editor;
256 *abspath = NULL;
258 editor = getenv("VISUAL");
259 if (editor == NULL)
260 editor = getenv("EDITOR");
262 if (editor) {
263 err = got_path_find_prog(abspath, editor);
264 if (err)
265 return err;
268 if (*abspath == NULL) {
269 *abspath = strdup("/bin/ed");
270 if (*abspath == NULL)
271 return got_error_from_errno("strdup");
274 return NULL;
277 static const struct got_error *
278 apply_unveil(const char *repo_path, int repo_read_only,
279 const char *worktree_path)
281 const struct got_error *err;
283 #ifdef PROFILE
284 if (unveil("gmon.out", "rwc") != 0)
285 return got_error_from_errno2("unveil", "gmon.out");
286 #endif
287 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
288 return got_error_from_errno2("unveil", repo_path);
290 if (worktree_path && unveil(worktree_path, "rwc") != 0)
291 return got_error_from_errno2("unveil", worktree_path);
293 if (unveil("/tmp", "rwc") != 0)
294 return got_error_from_errno2("unveil", "/tmp");
296 err = got_privsep_unveil_exec_helpers();
297 if (err != NULL)
298 return err;
300 if (unveil(NULL, NULL) != 0)
301 return got_error_from_errno("unveil");
303 return NULL;
306 __dead static void
307 usage_init(void)
309 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
310 exit(1);
313 static const struct got_error *
314 cmd_init(int argc, char *argv[])
316 const struct got_error *error = NULL;
317 char *repo_path = NULL;
318 int ch;
320 while ((ch = getopt(argc, argv, "")) != -1) {
321 switch (ch) {
322 default:
323 usage_init();
324 /* NOTREACHED */
328 argc -= optind;
329 argv += optind;
331 #ifndef PROFILE
332 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
333 err(1, "pledge");
334 #endif
335 if (argc != 1)
336 usage_init();
338 repo_path = strdup(argv[0]);
339 if (repo_path == NULL)
340 return got_error_from_errno("strdup");
342 got_path_strip_trailing_slashes(repo_path);
344 error = got_path_mkdir(repo_path);
345 if (error &&
346 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
347 goto done;
349 error = apply_unveil(repo_path, 0, NULL);
350 if (error)
351 goto done;
353 error = got_repo_init(repo_path);
354 if (error != NULL)
355 goto done;
357 done:
358 free(repo_path);
359 return error;
362 __dead static void
363 usage_import(void)
365 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
366 "[-r repository-path] [-I pattern] path\n", getprogname());
367 exit(1);
370 int
371 spawn_editor(const char *editor, const char *file)
373 pid_t pid;
374 sig_t sighup, sigint, sigquit;
375 int st = -1;
377 sighup = signal(SIGHUP, SIG_IGN);
378 sigint = signal(SIGINT, SIG_IGN);
379 sigquit = signal(SIGQUIT, SIG_IGN);
381 switch (pid = fork()) {
382 case -1:
383 goto doneediting;
384 case 0:
385 execl(editor, editor, file, (char *)NULL);
386 _exit(127);
389 while (waitpid(pid, &st, 0) == -1)
390 if (errno != EINTR)
391 break;
393 doneediting:
394 (void)signal(SIGHUP, sighup);
395 (void)signal(SIGINT, sigint);
396 (void)signal(SIGQUIT, sigquit);
398 if (!WIFEXITED(st)) {
399 errno = EINTR;
400 return -1;
403 return WEXITSTATUS(st);
406 static const struct got_error *
407 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
408 const char *initial_content)
410 const struct got_error *err = NULL;
411 char buf[1024];
412 struct stat st, st2;
413 FILE *fp;
414 int content_changed = 0;
415 size_t len;
417 *logmsg = NULL;
419 if (stat(logmsg_path, &st) == -1)
420 return got_error_from_errno2("stat", logmsg_path);
422 if (spawn_editor(editor, logmsg_path) == -1)
423 return got_error_from_errno("failed spawning editor");
425 if (stat(logmsg_path, &st2) == -1)
426 return got_error_from_errno("stat");
428 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
429 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
430 "no changes made to commit message, aborting");
432 *logmsg = malloc(st2.st_size + 1);
433 if (*logmsg == NULL)
434 return got_error_from_errno("malloc");
435 (*logmsg)[0] = '\0';
436 len = 0;
438 fp = fopen(logmsg_path, "r");
439 if (fp == NULL) {
440 err = got_error_from_errno("fopen");
441 goto done;
443 while (fgets(buf, sizeof(buf), fp) != NULL) {
444 if (!content_changed && strcmp(buf, initial_content) != 0)
445 content_changed = 1;
446 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
447 continue; /* remove comments and leading empty lines */
448 len = strlcat(*logmsg, buf, st2.st_size);
450 fclose(fp);
452 while (len > 0 && (*logmsg)[len - 1] == '\n') {
453 (*logmsg)[len - 1] = '\0';
454 len--;
457 if (len == 0 || !content_changed)
458 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 "commit message cannot be empty, aborting");
460 done:
461 if (err) {
462 free(*logmsg);
463 *logmsg = NULL;
465 return err;
468 static const struct got_error *
469 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 const char *path_dir, const char *branch_name)
472 char *initial_content = NULL;
473 const struct got_error *err = NULL;
474 int fd;
476 if (asprintf(&initial_content,
477 "\n# %s to be imported to branch %s\n", path_dir,
478 branch_name) == -1)
479 return got_error_from_errno("asprintf");
481 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
482 if (err)
483 goto done;
485 dprintf(fd, initial_content);
486 close(fd);
488 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
489 done:
490 free(initial_content);
491 return err;
494 static const struct got_error *
495 import_progress(void *arg, const char *path)
497 printf("A %s\n", path);
498 return NULL;
501 static const struct got_error *
502 get_author(char **author, struct got_repository *repo)
504 const struct got_error *err = NULL;
505 const char *got_author, *name, *email;
507 *author = NULL;
509 name = got_repo_get_gitconfig_author_name(repo);
510 email = got_repo_get_gitconfig_author_email(repo);
511 if (name && email) {
512 if (asprintf(author, "%s <%s>", name, email) == -1)
513 return got_error_from_errno("asprintf");
514 return NULL;
517 got_author = getenv("GOT_AUTHOR");
518 if (got_author == NULL) {
519 name = got_repo_get_global_gitconfig_author_name(repo);
520 email = got_repo_get_global_gitconfig_author_email(repo);
521 if (name && email) {
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
524 return NULL;
526 /* TODO: Look up user in password database? */
527 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
530 *author = strdup(got_author);
531 if (*author == NULL)
532 return got_error_from_errno("strdup");
534 /*
535 * Really dumb email address check; we're only doing this to
536 * avoid git's object parser breaking on commits we create.
537 */
538 while (*got_author && *got_author != '<')
539 got_author++;
540 if (*got_author != '<') {
541 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
542 goto done;
544 while (*got_author && *got_author != '@')
545 got_author++;
546 if (*got_author != '@') {
547 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
548 goto done;
550 while (*got_author && *got_author != '>')
551 got_author++;
552 if (*got_author != '>')
553 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 done:
555 if (err) {
556 free(*author);
557 *author = NULL;
559 return err;
562 static const struct got_error *
563 get_gitconfig_path(char **gitconfig_path)
565 const char *homedir = getenv("HOME");
567 *gitconfig_path = NULL;
568 if (homedir) {
569 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
570 return got_error_from_errno("asprintf");
573 return NULL;
576 static const struct got_error *
577 cmd_import(int argc, char *argv[])
579 const struct got_error *error = NULL;
580 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
581 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
582 const char *branch_name = "main";
583 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
584 struct got_repository *repo = NULL;
585 struct got_reference *branch_ref = NULL, *head_ref = NULL;
586 struct got_object_id *new_commit_id = NULL;
587 int ch;
588 struct got_pathlist_head ignores;
589 struct got_pathlist_entry *pe;
590 int preserve_logmsg = 0;
592 TAILQ_INIT(&ignores);
594 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
595 switch (ch) {
596 case 'b':
597 branch_name = optarg;
598 break;
599 case 'm':
600 logmsg = strdup(optarg);
601 if (logmsg == NULL) {
602 error = got_error_from_errno("strdup");
603 goto done;
605 break;
606 case 'r':
607 repo_path = realpath(optarg, NULL);
608 if (repo_path == NULL) {
609 error = got_error_from_errno2("realpath",
610 optarg);
611 goto done;
613 break;
614 case 'I':
615 if (optarg[0] == '\0')
616 break;
617 error = got_pathlist_insert(&pe, &ignores, optarg,
618 NULL);
619 if (error)
620 goto done;
621 break;
622 default:
623 usage_import();
624 /* NOTREACHED */
628 argc -= optind;
629 argv += optind;
631 #ifndef PROFILE
632 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
633 "unveil",
634 NULL) == -1)
635 err(1, "pledge");
636 #endif
637 if (argc != 1)
638 usage_import();
640 if (repo_path == NULL) {
641 repo_path = getcwd(NULL, 0);
642 if (repo_path == NULL)
643 return got_error_from_errno("getcwd");
645 got_path_strip_trailing_slashes(repo_path);
646 error = get_gitconfig_path(&gitconfig_path);
647 if (error)
648 goto done;
649 error = got_repo_open(&repo, repo_path, gitconfig_path);
650 if (error)
651 goto done;
653 error = get_author(&author, repo);
654 if (error)
655 return error;
657 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
658 error = got_error_from_errno("asprintf");
659 goto done;
662 error = got_ref_open(&branch_ref, repo, refname, 0);
663 if (error) {
664 if (error->code != GOT_ERR_NOT_REF)
665 goto done;
666 } else {
667 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
668 "import target branch already exists");
669 goto done;
672 path_dir = realpath(argv[0], NULL);
673 if (path_dir == NULL) {
674 error = got_error_from_errno2("realpath", argv[0]);
675 goto done;
677 got_path_strip_trailing_slashes(path_dir);
679 /*
680 * unveil(2) traverses exec(2); if an editor is used we have
681 * to apply unveil after the log message has been written.
682 */
683 if (logmsg == NULL || strlen(logmsg) == 0) {
684 error = get_editor(&editor);
685 if (error)
686 goto done;
687 free(logmsg);
688 error = collect_import_msg(&logmsg, &logmsg_path, editor,
689 path_dir, refname);
690 if (error) {
691 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
692 logmsg_path != NULL)
693 preserve_logmsg = 1;
694 goto done;
698 if (unveil(path_dir, "r") != 0) {
699 error = got_error_from_errno2("unveil", path_dir);
700 if (logmsg_path)
701 preserve_logmsg = 1;
702 goto done;
705 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
706 if (error) {
707 if (logmsg_path)
708 preserve_logmsg = 1;
709 goto done;
712 error = got_repo_import(&new_commit_id, path_dir, logmsg,
713 author, &ignores, repo, import_progress, NULL);
714 if (error) {
715 if (logmsg_path)
716 preserve_logmsg = 1;
717 goto done;
720 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
721 if (error) {
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = got_ref_write(branch_ref, repo);
728 if (error) {
729 if (logmsg_path)
730 preserve_logmsg = 1;
731 goto done;
734 error = got_object_id_str(&id_str, new_commit_id);
735 if (error) {
736 if (logmsg_path)
737 preserve_logmsg = 1;
738 goto done;
741 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
742 if (error) {
743 if (error->code != GOT_ERR_NOT_REF) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
750 branch_ref);
751 if (error) {
752 if (logmsg_path)
753 preserve_logmsg = 1;
754 goto done;
757 error = got_ref_write(head_ref, repo);
758 if (error) {
759 if (logmsg_path)
760 preserve_logmsg = 1;
761 goto done;
765 printf("Created branch %s with commit %s\n",
766 got_ref_get_name(branch_ref), id_str);
767 done:
768 if (preserve_logmsg) {
769 fprintf(stderr, "%s: log message preserved in %s\n",
770 getprogname(), logmsg_path);
771 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
772 error = got_error_from_errno2("unlink", logmsg_path);
773 free(logmsg);
774 free(logmsg_path);
775 free(repo_path);
776 free(editor);
777 free(refname);
778 free(new_commit_id);
779 free(id_str);
780 free(author);
781 free(gitconfig_path);
782 if (branch_ref)
783 got_ref_close(branch_ref);
784 if (head_ref)
785 got_ref_close(head_ref);
786 return error;
789 __dead static void
790 usage_checkout(void)
792 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
793 "[-p prefix] repository-path [worktree-path]\n", getprogname());
794 exit(1);
797 static const struct got_error *
798 checkout_progress(void *arg, unsigned char status, const char *path)
800 char *worktree_path = arg;
802 /* Base commit bump happens silently. */
803 if (status == GOT_STATUS_BUMP_BASE)
804 return NULL;
806 while (path[0] == '/')
807 path++;
809 printf("%c %s/%s\n", status, worktree_path, path);
810 return NULL;
813 static const struct got_error *
814 check_cancelled(void *arg)
816 if (sigint_received || sigpipe_received)
817 return got_error(GOT_ERR_CANCELLED);
818 return NULL;
821 static const struct got_error *
822 check_linear_ancestry(struct got_object_id *commit_id,
823 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_object_id *yca_id;
829 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
830 commit_id, base_commit_id, repo, check_cancelled, NULL);
831 if (err)
832 return err;
834 if (yca_id == NULL)
835 return got_error(GOT_ERR_ANCESTRY);
837 /*
838 * Require a straight line of history between the target commit
839 * and the work tree's base commit.
841 * Non-linear situations such as this require a rebase:
843 * (commit) D F (base_commit)
844 * \ /
845 * C E
846 * \ /
847 * B (yca)
848 * |
849 * A
851 * 'got update' only handles linear cases:
852 * Update forwards in time: A (base/yca) - B - C - D (commit)
853 * Update backwards in time: D (base) - C - B - A (commit/yca)
854 */
855 if (allow_forwards_in_time_only) {
856 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
857 return got_error(GOT_ERR_ANCESTRY);
858 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
859 got_object_id_cmp(base_commit_id, yca_id) != 0)
860 return got_error(GOT_ERR_ANCESTRY);
862 free(yca_id);
863 return NULL;
866 static const struct got_error *
867 check_same_branch(struct got_object_id *commit_id,
868 struct got_reference *head_ref, struct got_object_id *yca_id,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_commit_graph *graph = NULL;
873 struct got_object_id *head_commit_id = NULL;
874 int is_same_branch = 0;
876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
877 if (err)
878 goto done;
880 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
881 is_same_branch = 1;
882 goto done;
884 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
885 is_same_branch = 1;
886 goto done;
889 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
890 if (err)
891 goto done;
893 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
894 check_cancelled, NULL);
895 if (err)
896 goto done;
898 for (;;) {
899 struct got_object_id *id;
900 err = got_commit_graph_iter_next(&id, graph);
901 if (err) {
902 if (err->code == GOT_ERR_ITER_COMPLETED) {
903 err = NULL;
904 break;
905 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
906 break;
907 err = got_commit_graph_fetch_commits(graph, 1,
908 repo, check_cancelled, NULL);
909 if (err)
910 break;
913 if (id) {
914 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
915 break;
916 if (got_object_id_cmp(id, commit_id) == 0) {
917 is_same_branch = 1;
918 break;
922 done:
923 if (graph)
924 got_commit_graph_close(graph);
925 free(head_commit_id);
926 if (!err && !is_same_branch)
927 err = got_error(GOT_ERR_ANCESTRY);
928 return err;
931 static const struct got_error *
932 resolve_commit_arg(struct got_object_id **commit_id,
933 const char *commit_id_arg, struct got_repository *repo)
935 const struct got_error *err;
936 struct got_reference *ref;
937 struct got_tag_object *tag;
939 err = got_repo_object_match_tag(&tag, commit_id_arg,
940 GOT_OBJ_TYPE_COMMIT, repo);
941 if (err == NULL) {
942 *commit_id = got_object_id_dup(
943 got_object_tag_get_object_id(tag));
944 if (*commit_id == NULL)
945 err = got_error_from_errno("got_object_id_dup");
946 got_object_tag_close(tag);
947 return err;
948 } else if (err->code != GOT_ERR_NO_OBJ)
949 return err;
951 err = got_ref_open(&ref, repo, commit_id_arg, 0);
952 if (err == NULL) {
953 err = got_ref_resolve(commit_id, repo, ref);
954 got_ref_close(ref);
955 } else {
956 if (err->code != GOT_ERR_NOT_REF)
957 return err;
958 err = got_repo_match_object_id_prefix(commit_id,
959 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
961 return err;
964 static const struct got_error *
965 cmd_checkout(int argc, char *argv[])
967 const struct got_error *error = NULL;
968 struct got_repository *repo = NULL;
969 struct got_reference *head_ref = NULL;
970 struct got_worktree *worktree = NULL;
971 char *repo_path = NULL;
972 char *worktree_path = NULL;
973 const char *path_prefix = "";
974 const char *branch_name = GOT_REF_HEAD;
975 char *commit_id_str = NULL;
976 int ch, same_path_prefix;
977 struct got_pathlist_head paths;
979 TAILQ_INIT(&paths);
981 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
982 switch (ch) {
983 case 'b':
984 branch_name = optarg;
985 break;
986 case 'c':
987 commit_id_str = strdup(optarg);
988 if (commit_id_str == NULL)
989 return got_error_from_errno("strdup");
990 break;
991 case 'p':
992 path_prefix = optarg;
993 break;
994 default:
995 usage_checkout();
996 /* NOTREACHED */
1000 argc -= optind;
1001 argv += optind;
1003 #ifndef PROFILE
1004 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1005 "unveil", NULL) == -1)
1006 err(1, "pledge");
1007 #endif
1008 if (argc == 1) {
1009 char *cwd, *base, *dotgit;
1010 repo_path = realpath(argv[0], NULL);
1011 if (repo_path == NULL)
1012 return got_error_from_errno2("realpath", argv[0]);
1013 cwd = getcwd(NULL, 0);
1014 if (cwd == NULL) {
1015 error = got_error_from_errno("getcwd");
1016 goto done;
1018 if (path_prefix[0]) {
1019 base = basename(path_prefix);
1020 if (base == NULL) {
1021 error = got_error_from_errno2("basename",
1022 path_prefix);
1023 goto done;
1025 } else {
1026 base = basename(repo_path);
1027 if (base == NULL) {
1028 error = got_error_from_errno2("basename",
1029 repo_path);
1030 goto done;
1033 dotgit = strstr(base, ".git");
1034 if (dotgit)
1035 *dotgit = '\0';
1036 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1037 error = got_error_from_errno("asprintf");
1038 free(cwd);
1039 goto done;
1041 free(cwd);
1042 } else if (argc == 2) {
1043 repo_path = realpath(argv[0], NULL);
1044 if (repo_path == NULL) {
1045 error = got_error_from_errno2("realpath", argv[0]);
1046 goto done;
1048 worktree_path = realpath(argv[1], NULL);
1049 if (worktree_path == NULL) {
1050 if (errno != ENOENT) {
1051 error = got_error_from_errno2("realpath",
1052 argv[1]);
1053 goto done;
1055 worktree_path = strdup(argv[1]);
1056 if (worktree_path == NULL) {
1057 error = got_error_from_errno("strdup");
1058 goto done;
1061 } else
1062 usage_checkout();
1064 got_path_strip_trailing_slashes(repo_path);
1065 got_path_strip_trailing_slashes(worktree_path);
1067 error = got_repo_open(&repo, repo_path, NULL);
1068 if (error != NULL)
1069 goto done;
1071 /* Pre-create work tree path for unveil(2) */
1072 error = got_path_mkdir(worktree_path);
1073 if (error) {
1074 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1075 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1076 goto done;
1077 if (!got_path_dir_is_empty(worktree_path)) {
1078 error = got_error_path(worktree_path,
1079 GOT_ERR_DIR_NOT_EMPTY);
1080 goto done;
1084 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1085 if (error)
1086 goto done;
1088 error = got_ref_open(&head_ref, repo, branch_name, 0);
1089 if (error != NULL)
1090 goto done;
1092 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1094 goto done;
1096 error = got_worktree_open(&worktree, worktree_path);
1097 if (error != NULL)
1098 goto done;
1100 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1101 path_prefix);
1102 if (error != NULL)
1103 goto done;
1104 if (!same_path_prefix) {
1105 error = got_error(GOT_ERR_PATH_PREFIX);
1106 goto done;
1109 if (commit_id_str) {
1110 struct got_object_id *commit_id;
1111 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1112 if (error)
1113 goto done;
1114 error = check_linear_ancestry(commit_id,
1115 got_worktree_get_base_commit_id(worktree), 0, repo);
1116 if (error != NULL) {
1117 free(commit_id);
1118 goto done;
1120 error = check_same_branch(commit_id, head_ref, NULL, repo);
1121 if (error)
1122 goto done;
1123 error = got_worktree_set_base_commit_id(worktree, repo,
1124 commit_id);
1125 free(commit_id);
1126 if (error)
1127 goto done;
1130 error = got_pathlist_append(&paths, "", NULL);
1131 if (error)
1132 goto done;
1133 error = got_worktree_checkout_files(worktree, &paths, repo,
1134 checkout_progress, worktree_path, check_cancelled, NULL);
1135 if (error != NULL)
1136 goto done;
1138 printf("Now shut up and hack\n");
1140 done:
1141 got_pathlist_free(&paths);
1142 free(commit_id_str);
1143 free(repo_path);
1144 free(worktree_path);
1145 return error;
1148 __dead static void
1149 usage_update(void)
1151 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1152 getprogname());
1153 exit(1);
1156 static const struct got_error *
1157 update_progress(void *arg, unsigned char status, const char *path)
1159 int *did_something = arg;
1161 if (status == GOT_STATUS_EXISTS)
1162 return NULL;
1164 *did_something = 1;
1166 /* Base commit bump happens silently. */
1167 if (status == GOT_STATUS_BUMP_BASE)
1168 return NULL;
1170 while (path[0] == '/')
1171 path++;
1172 printf("%c %s\n", status, path);
1173 return NULL;
1176 static const struct got_error *
1177 switch_head_ref(struct got_reference *head_ref,
1178 struct got_object_id *commit_id, struct got_worktree *worktree,
1179 struct got_repository *repo)
1181 const struct got_error *err = NULL;
1182 char *base_id_str;
1183 int ref_has_moved = 0;
1185 /* Trivial case: switching between two different references. */
1186 if (strcmp(got_ref_get_name(head_ref),
1187 got_worktree_get_head_ref_name(worktree)) != 0) {
1188 printf("Switching work tree from %s to %s\n",
1189 got_worktree_get_head_ref_name(worktree),
1190 got_ref_get_name(head_ref));
1191 return got_worktree_set_head_ref(worktree, head_ref);
1194 err = check_linear_ancestry(commit_id,
1195 got_worktree_get_base_commit_id(worktree), 0, repo);
1196 if (err) {
1197 if (err->code != GOT_ERR_ANCESTRY)
1198 return err;
1199 ref_has_moved = 1;
1201 if (!ref_has_moved)
1202 return NULL;
1204 /* Switching to a rebased branch with the same reference name. */
1205 err = got_object_id_str(&base_id_str,
1206 got_worktree_get_base_commit_id(worktree));
1207 if (err)
1208 return err;
1209 printf("Reference %s now points at a different branch\n",
1210 got_worktree_get_head_ref_name(worktree));
1211 printf("Switching work tree from %s to %s\n", base_id_str,
1212 got_worktree_get_head_ref_name(worktree));
1213 return NULL;
1216 static const struct got_error *
1217 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1219 const struct got_error *err;
1220 int in_progress;
1222 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1223 if (err)
1224 return err;
1225 if (in_progress)
1226 return got_error(GOT_ERR_REBASING);
1228 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1229 if (err)
1230 return err;
1231 if (in_progress)
1232 return got_error(GOT_ERR_HISTEDIT_BUSY);
1234 return NULL;
1237 static const struct got_error *
1238 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1239 char *argv[], struct got_worktree *worktree)
1241 const struct got_error *err = NULL;
1242 char *path;
1243 int i;
1245 if (argc == 0) {
1246 path = strdup("");
1247 if (path == NULL)
1248 return got_error_from_errno("strdup");
1249 return got_pathlist_append(paths, path, NULL);
1252 for (i = 0; i < argc; i++) {
1253 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1254 if (err)
1255 break;
1256 err = got_pathlist_append(paths, path, NULL);
1257 if (err) {
1258 free(path);
1259 break;
1263 return err;
1266 static const struct got_error *
1267 cmd_update(int argc, char *argv[])
1269 const struct got_error *error = NULL;
1270 struct got_repository *repo = NULL;
1271 struct got_worktree *worktree = NULL;
1272 char *worktree_path = NULL;
1273 struct got_object_id *commit_id = NULL;
1274 char *commit_id_str = NULL;
1275 const char *branch_name = NULL;
1276 struct got_reference *head_ref = NULL;
1277 struct got_pathlist_head paths;
1278 struct got_pathlist_entry *pe;
1279 int ch, did_something = 0;
1281 TAILQ_INIT(&paths);
1283 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1284 switch (ch) {
1285 case 'b':
1286 branch_name = optarg;
1287 break;
1288 case 'c':
1289 commit_id_str = strdup(optarg);
1290 if (commit_id_str == NULL)
1291 return got_error_from_errno("strdup");
1292 break;
1293 default:
1294 usage_update();
1295 /* NOTREACHED */
1299 argc -= optind;
1300 argv += optind;
1302 #ifndef PROFILE
1303 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1304 "unveil", NULL) == -1)
1305 err(1, "pledge");
1306 #endif
1307 worktree_path = getcwd(NULL, 0);
1308 if (worktree_path == NULL) {
1309 error = got_error_from_errno("getcwd");
1310 goto done;
1312 error = got_worktree_open(&worktree, worktree_path);
1313 if (error)
1314 goto done;
1316 error = check_rebase_or_histedit_in_progress(worktree);
1317 if (error)
1318 goto done;
1320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1321 NULL);
1322 if (error != NULL)
1323 goto done;
1325 error = apply_unveil(got_repo_get_path(repo), 0,
1326 got_worktree_get_root_path(worktree));
1327 if (error)
1328 goto done;
1330 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1331 if (error)
1332 goto done;
1334 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1335 got_worktree_get_head_ref_name(worktree), 0);
1336 if (error != NULL)
1337 goto done;
1338 if (commit_id_str == NULL) {
1339 error = got_ref_resolve(&commit_id, repo, head_ref);
1340 if (error != NULL)
1341 goto done;
1342 error = got_object_id_str(&commit_id_str, commit_id);
1343 if (error != NULL)
1344 goto done;
1345 } else {
1346 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1347 free(commit_id_str);
1348 commit_id_str = NULL;
1349 if (error)
1350 goto done;
1351 error = got_object_id_str(&commit_id_str, commit_id);
1352 if (error)
1353 goto done;
1356 if (branch_name) {
1357 struct got_object_id *head_commit_id;
1358 TAILQ_FOREACH(pe, &paths, entry) {
1359 if (pe->path_len == 0)
1360 continue;
1361 error = got_error_msg(GOT_ERR_BAD_PATH,
1362 "switching between branches requires that "
1363 "the entire work tree gets updated");
1364 goto done;
1366 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1367 if (error)
1368 goto done;
1369 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1370 repo);
1371 free(head_commit_id);
1372 if (error != NULL)
1373 goto done;
1374 error = check_same_branch(commit_id, head_ref, NULL, repo);
1375 if (error)
1376 goto done;
1377 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1378 if (error)
1379 goto done;
1380 } else {
1381 error = check_linear_ancestry(commit_id,
1382 got_worktree_get_base_commit_id(worktree), 0, repo);
1383 if (error != NULL) {
1384 if (error->code == GOT_ERR_ANCESTRY)
1385 error = got_error(GOT_ERR_BRANCH_MOVED);
1386 goto done;
1388 error = check_same_branch(commit_id, head_ref, NULL, repo);
1389 if (error)
1390 goto done;
1393 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1394 commit_id) != 0) {
1395 error = got_worktree_set_base_commit_id(worktree, repo,
1396 commit_id);
1397 if (error)
1398 goto done;
1401 error = got_worktree_checkout_files(worktree, &paths, repo,
1402 update_progress, &did_something, check_cancelled, NULL);
1403 if (error != NULL)
1404 goto done;
1406 if (did_something)
1407 printf("Updated to commit %s\n", commit_id_str);
1408 else
1409 printf("Already up-to-date\n");
1410 done:
1411 free(worktree_path);
1412 TAILQ_FOREACH(pe, &paths, entry)
1413 free((char *)pe->path);
1414 got_pathlist_free(&paths);
1415 free(commit_id);
1416 free(commit_id_str);
1417 return error;
1420 static const struct got_error *
1421 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1422 const char *path, int diff_context, int ignore_whitespace,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1428 if (blob_id1) {
1429 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1430 if (err)
1431 goto done;
1434 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1435 if (err)
1436 goto done;
1438 while (path[0] == '/')
1439 path++;
1440 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1441 ignore_whitespace, stdout);
1442 done:
1443 if (blob1)
1444 got_object_blob_close(blob1);
1445 got_object_blob_close(blob2);
1446 return err;
1449 static const struct got_error *
1450 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1451 const char *path, int diff_context, int ignore_whitespace,
1452 struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1456 struct got_diff_blob_output_unidiff_arg arg;
1458 if (tree_id1) {
1459 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1460 if (err)
1461 goto done;
1464 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1465 if (err)
1466 goto done;
1468 arg.diff_context = diff_context;
1469 arg.ignore_whitespace = ignore_whitespace;
1470 arg.outfile = stdout;
1471 while (path[0] == '/')
1472 path++;
1473 err = got_diff_tree(tree1, tree2, path, path, repo,
1474 got_diff_blob_output_unidiff, &arg, 1);
1475 done:
1476 if (tree1)
1477 got_object_tree_close(tree1);
1478 if (tree2)
1479 got_object_tree_close(tree2);
1480 return err;
1483 static const struct got_error *
1484 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1485 const char *path, int diff_context, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_commit_object *pcommit = NULL;
1489 char *id_str1 = NULL, *id_str2 = NULL;
1490 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1491 struct got_object_qid *qid;
1493 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1494 if (qid != NULL) {
1495 err = got_object_open_as_commit(&pcommit, repo,
1496 qid->id);
1497 if (err)
1498 return err;
1501 if (path && path[0] != '\0') {
1502 int obj_type;
1503 err = got_object_id_by_path(&obj_id2, repo, id, path);
1504 if (err)
1505 goto done;
1506 err = got_object_id_str(&id_str2, obj_id2);
1507 if (err) {
1508 free(obj_id2);
1509 goto done;
1511 if (pcommit) {
1512 err = got_object_id_by_path(&obj_id1, repo,
1513 qid->id, path);
1514 if (err) {
1515 free(obj_id2);
1516 goto done;
1518 err = got_object_id_str(&id_str1, obj_id1);
1519 if (err) {
1520 free(obj_id2);
1521 goto done;
1524 err = got_object_get_type(&obj_type, repo, obj_id2);
1525 if (err) {
1526 free(obj_id2);
1527 goto done;
1529 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1530 switch (obj_type) {
1531 case GOT_OBJ_TYPE_BLOB:
1532 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1533 0, repo);
1534 break;
1535 case GOT_OBJ_TYPE_TREE:
1536 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1537 0, repo);
1538 break;
1539 default:
1540 err = got_error(GOT_ERR_OBJ_TYPE);
1541 break;
1543 free(obj_id1);
1544 free(obj_id2);
1545 } else {
1546 obj_id2 = got_object_commit_get_tree_id(commit);
1547 err = got_object_id_str(&id_str2, obj_id2);
1548 if (err)
1549 goto done;
1550 obj_id1 = got_object_commit_get_tree_id(pcommit);
1551 err = got_object_id_str(&id_str1, obj_id1);
1552 if (err)
1553 goto done;
1554 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1555 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1558 done:
1559 free(id_str1);
1560 free(id_str2);
1561 if (pcommit)
1562 got_object_commit_close(pcommit);
1563 return err;
1566 static char *
1567 get_datestr(time_t *time, char *datebuf)
1569 struct tm mytm, *tm;
1570 char *p, *s;
1572 tm = gmtime_r(time, &mytm);
1573 if (tm == NULL)
1574 return NULL;
1575 s = asctime_r(tm, datebuf);
1576 if (s == NULL)
1577 return NULL;
1578 p = strchr(s, '\n');
1579 if (p)
1580 *p = '\0';
1581 return s;
1584 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1586 static const struct got_error *
1587 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1588 struct got_repository *repo, const char *path, int show_patch,
1589 int diff_context, struct got_reflist_head *refs)
1591 const struct got_error *err = NULL;
1592 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1593 char datebuf[26];
1594 time_t committer_time;
1595 const char *author, *committer;
1596 char *refs_str = NULL;
1597 struct got_reflist_entry *re;
1599 SIMPLEQ_FOREACH(re, refs, entry) {
1600 char *s;
1601 const char *name;
1602 struct got_tag_object *tag = NULL;
1603 int cmp;
1605 name = got_ref_get_name(re->ref);
1606 if (strcmp(name, GOT_REF_HEAD) == 0)
1607 continue;
1608 if (strncmp(name, "refs/", 5) == 0)
1609 name += 5;
1610 if (strncmp(name, "got/", 4) == 0)
1611 continue;
1612 if (strncmp(name, "heads/", 6) == 0)
1613 name += 6;
1614 if (strncmp(name, "remotes/", 8) == 0)
1615 name += 8;
1616 if (strncmp(name, "tags/", 5) == 0) {
1617 err = got_object_open_as_tag(&tag, repo, re->id);
1618 if (err) {
1619 if (err->code != GOT_ERR_OBJ_TYPE)
1620 return err;
1621 /* Ref points at something other than a tag. */
1622 err = NULL;
1623 tag = NULL;
1626 cmp = got_object_id_cmp(tag ?
1627 got_object_tag_get_object_id(tag) : re->id, id);
1628 if (tag)
1629 got_object_tag_close(tag);
1630 if (cmp != 0)
1631 continue;
1632 s = refs_str;
1633 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1634 name) == -1) {
1635 err = got_error_from_errno("asprintf");
1636 free(s);
1637 return err;
1639 free(s);
1641 err = got_object_id_str(&id_str, id);
1642 if (err)
1643 return err;
1645 printf(GOT_COMMIT_SEP_STR);
1646 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1647 refs_str ? refs_str : "", refs_str ? ")" : "");
1648 free(id_str);
1649 id_str = NULL;
1650 free(refs_str);
1651 refs_str = NULL;
1652 printf("from: %s\n", got_object_commit_get_author(commit));
1653 committer_time = got_object_commit_get_committer_time(commit);
1654 datestr = get_datestr(&committer_time, datebuf);
1655 if (datestr)
1656 printf("date: %s UTC\n", datestr);
1657 author = got_object_commit_get_author(commit);
1658 committer = got_object_commit_get_committer(commit);
1659 if (strcmp(author, committer) != 0)
1660 printf("via: %s\n", committer);
1661 if (got_object_commit_get_nparents(commit) > 1) {
1662 const struct got_object_id_queue *parent_ids;
1663 struct got_object_qid *qid;
1664 int n = 1;
1665 parent_ids = got_object_commit_get_parent_ids(commit);
1666 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1667 err = got_object_id_str(&id_str, qid->id);
1668 if (err)
1669 return err;
1670 printf("parent %d: %s\n", n++, id_str);
1671 free(id_str);
1675 err = got_object_commit_get_logmsg(&logmsg0, commit);
1676 if (err)
1677 return err;
1679 logmsg = logmsg0;
1680 do {
1681 line = strsep(&logmsg, "\n");
1682 if (line)
1683 printf(" %s\n", line);
1684 } while (line);
1685 free(logmsg0);
1687 if (show_patch) {
1688 err = print_patch(commit, id, path, diff_context, repo);
1689 if (err == 0)
1690 printf("\n");
1693 if (fflush(stdout) != 0 && err == NULL)
1694 err = got_error_from_errno("fflush");
1695 return err;
1698 static const struct got_error *
1699 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1700 char *path, int show_patch, int diff_context, int limit,
1701 int first_parent_traversal, struct got_reflist_head *refs)
1703 const struct got_error *err;
1704 struct got_commit_graph *graph;
1706 err = got_commit_graph_open(&graph, root_id, path,
1707 first_parent_traversal, repo);
1708 if (err)
1709 return err;
1710 err = got_commit_graph_iter_start(graph, root_id, repo,
1711 check_cancelled, NULL);
1712 if (err)
1713 goto done;
1714 for (;;) {
1715 struct got_commit_object *commit;
1716 struct got_object_id *id;
1718 if (sigint_received || sigpipe_received)
1719 break;
1721 err = got_commit_graph_iter_next(&id, graph);
1722 if (err) {
1723 if (err->code == GOT_ERR_ITER_COMPLETED) {
1724 err = NULL;
1725 break;
1727 if (err->code != GOT_ERR_ITER_NEED_MORE)
1728 break;
1729 err = got_commit_graph_fetch_commits(graph, 1, repo,
1730 check_cancelled, NULL);
1731 if (err)
1732 break;
1733 else
1734 continue;
1736 if (id == NULL)
1737 break;
1739 err = got_object_open_as_commit(&commit, repo, id);
1740 if (err)
1741 break;
1742 err = print_commit(commit, id, repo, path, show_patch,
1743 diff_context, refs);
1744 got_object_commit_close(commit);
1745 if (err || (limit && --limit == 0))
1746 break;
1748 done:
1749 got_commit_graph_close(graph);
1750 return err;
1753 __dead static void
1754 usage_log(void)
1756 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1757 "[-r repository-path] [path]\n", getprogname());
1758 exit(1);
1761 static int
1762 get_default_log_limit(void)
1764 const char *got_default_log_limit;
1765 long long n;
1766 const char *errstr;
1768 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1769 if (got_default_log_limit == NULL)
1770 return 0;
1771 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1772 if (errstr != NULL)
1773 return 0;
1774 return n;
1777 static const struct got_error *
1778 cmd_log(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1783 struct got_commit_object *commit = NULL;
1784 struct got_object_id *id = NULL;
1785 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1786 char *start_commit = NULL;
1787 int diff_context = 3, ch;
1788 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1789 const char *errstr;
1790 struct got_reflist_head refs;
1792 SIMPLEQ_INIT(&refs);
1794 #ifndef PROFILE
1795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 NULL)
1797 == -1)
1798 err(1, "pledge");
1799 #endif
1801 limit = get_default_log_limit();
1803 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1804 switch (ch) {
1805 case 'p':
1806 show_patch = 1;
1807 break;
1808 case 'c':
1809 start_commit = optarg;
1810 break;
1811 case 'C':
1812 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1813 &errstr);
1814 if (errstr != NULL)
1815 err(1, "-C option %s", errstr);
1816 break;
1817 case 'l':
1818 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1819 if (errstr != NULL)
1820 err(1, "-l option %s", errstr);
1821 break;
1822 case 'f':
1823 first_parent_traversal = 1;
1824 break;
1825 case 'r':
1826 repo_path = realpath(optarg, NULL);
1827 if (repo_path == NULL)
1828 return got_error_from_errno2("realpath",
1829 optarg);
1830 got_path_strip_trailing_slashes(repo_path);
1831 break;
1832 default:
1833 usage_log();
1834 /* NOTREACHED */
1838 argc -= optind;
1839 argv += optind;
1841 cwd = getcwd(NULL, 0);
1842 if (cwd == NULL) {
1843 error = got_error_from_errno("getcwd");
1844 goto done;
1847 error = got_worktree_open(&worktree, cwd);
1848 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 goto done;
1850 error = NULL;
1852 if (argc == 0) {
1853 path = strdup("");
1854 if (path == NULL) {
1855 error = got_error_from_errno("strdup");
1856 goto done;
1858 } else if (argc == 1) {
1859 if (worktree) {
1860 error = got_worktree_resolve_path(&path, worktree,
1861 argv[0]);
1862 if (error)
1863 goto done;
1864 } else {
1865 path = strdup(argv[0]);
1866 if (path == NULL) {
1867 error = got_error_from_errno("strdup");
1868 goto done;
1871 } else
1872 usage_log();
1874 if (repo_path == NULL) {
1875 repo_path = worktree ?
1876 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1878 if (repo_path == NULL) {
1879 error = got_error_from_errno("strdup");
1880 goto done;
1883 error = got_repo_open(&repo, repo_path, NULL);
1884 if (error != NULL)
1885 goto done;
1887 error = apply_unveil(got_repo_get_path(repo), 1,
1888 worktree ? got_worktree_get_root_path(worktree) : NULL);
1889 if (error)
1890 goto done;
1892 if (start_commit == NULL) {
1893 struct got_reference *head_ref;
1894 error = got_ref_open(&head_ref, repo,
1895 worktree ? got_worktree_get_head_ref_name(worktree)
1896 : GOT_REF_HEAD, 0);
1897 if (error != NULL)
1898 return error;
1899 error = got_ref_resolve(&id, repo, head_ref);
1900 got_ref_close(head_ref);
1901 if (error != NULL)
1902 return error;
1903 error = got_object_open_as_commit(&commit, repo, id);
1904 } else {
1905 struct got_reference *ref;
1906 error = got_ref_open(&ref, repo, start_commit, 0);
1907 if (error == NULL) {
1908 int obj_type;
1909 error = got_ref_resolve(&id, repo, ref);
1910 got_ref_close(ref);
1911 if (error != NULL)
1912 goto done;
1913 error = got_object_get_type(&obj_type, repo, id);
1914 if (error != NULL)
1915 goto done;
1916 if (obj_type == GOT_OBJ_TYPE_TAG) {
1917 struct got_tag_object *tag;
1918 error = got_object_open_as_tag(&tag, repo, id);
1919 if (error != NULL)
1920 goto done;
1921 if (got_object_tag_get_object_type(tag) !=
1922 GOT_OBJ_TYPE_COMMIT) {
1923 got_object_tag_close(tag);
1924 error = got_error(GOT_ERR_OBJ_TYPE);
1925 goto done;
1927 free(id);
1928 id = got_object_id_dup(
1929 got_object_tag_get_object_id(tag));
1930 if (id == NULL)
1931 error = got_error_from_errno(
1932 "got_object_id_dup");
1933 got_object_tag_close(tag);
1934 if (error)
1935 goto done;
1936 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1937 error = got_error(GOT_ERR_OBJ_TYPE);
1938 goto done;
1940 error = got_object_open_as_commit(&commit, repo, id);
1941 if (error != NULL)
1942 goto done;
1944 if (commit == NULL) {
1945 error = got_repo_match_object_id_prefix(&id,
1946 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1947 if (error != NULL)
1948 return error;
1951 if (error != NULL)
1952 goto done;
1954 if (worktree) {
1955 const char *prefix = got_worktree_get_path_prefix(worktree);
1956 char *p;
1957 if (asprintf(&p, "%s%s%s", prefix,
1958 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1959 error = got_error_from_errno("asprintf");
1960 goto done;
1962 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1963 free(p);
1964 } else
1965 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1966 if (error != NULL)
1967 goto done;
1968 if (in_repo_path) {
1969 free(path);
1970 path = in_repo_path;
1973 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1974 if (error)
1975 goto done;
1977 error = print_commits(id, repo, path, show_patch,
1978 diff_context, limit, first_parent_traversal, &refs);
1979 done:
1980 free(path);
1981 free(repo_path);
1982 free(cwd);
1983 free(id);
1984 if (worktree)
1985 got_worktree_close(worktree);
1986 if (repo) {
1987 const struct got_error *repo_error;
1988 repo_error = got_repo_close(repo);
1989 if (error == NULL)
1990 error = repo_error;
1992 got_ref_list_free(&refs);
1993 return error;
1996 __dead static void
1997 usage_diff(void)
1999 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2000 "[-w] [object1 object2 | path]\n", getprogname());
2001 exit(1);
2004 struct print_diff_arg {
2005 struct got_repository *repo;
2006 struct got_worktree *worktree;
2007 int diff_context;
2008 const char *id_str;
2009 int header_shown;
2010 int diff_staged;
2011 int ignore_whitespace;
2014 static const struct got_error *
2015 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2016 const char *path, struct got_object_id *blob_id,
2017 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2019 struct print_diff_arg *a = arg;
2020 const struct got_error *err = NULL;
2021 struct got_blob_object *blob1 = NULL;
2022 FILE *f2 = NULL;
2023 char *abspath = NULL, *label1 = NULL;
2024 struct stat sb;
2026 if (a->diff_staged) {
2027 if (staged_status != GOT_STATUS_MODIFY &&
2028 staged_status != GOT_STATUS_ADD &&
2029 staged_status != GOT_STATUS_DELETE)
2030 return NULL;
2031 } else {
2032 if (staged_status == GOT_STATUS_DELETE)
2033 return NULL;
2034 if (status == GOT_STATUS_NONEXISTENT)
2035 return got_error_set_errno(ENOENT, path);
2036 if (status != GOT_STATUS_MODIFY &&
2037 status != GOT_STATUS_ADD &&
2038 status != GOT_STATUS_DELETE &&
2039 status != GOT_STATUS_CONFLICT)
2040 return NULL;
2043 if (!a->header_shown) {
2044 printf("diff %s %s%s\n", a->id_str,
2045 got_worktree_get_root_path(a->worktree),
2046 a->diff_staged ? " (staged changes)" : "");
2047 a->header_shown = 1;
2050 if (a->diff_staged) {
2051 const char *label1 = NULL, *label2 = NULL;
2052 switch (staged_status) {
2053 case GOT_STATUS_MODIFY:
2054 label1 = path;
2055 label2 = path;
2056 break;
2057 case GOT_STATUS_ADD:
2058 label2 = path;
2059 break;
2060 case GOT_STATUS_DELETE:
2061 label1 = path;
2062 break;
2063 default:
2064 return got_error(GOT_ERR_FILE_STATUS);
2066 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2067 label1, label2, a->diff_context, a->ignore_whitespace,
2068 a->repo, stdout);
2071 if (staged_status == GOT_STATUS_ADD ||
2072 staged_status == GOT_STATUS_MODIFY) {
2073 char *id_str;
2074 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2075 8192);
2076 if (err)
2077 goto done;
2078 err = got_object_id_str(&id_str, staged_blob_id);
2079 if (err)
2080 goto done;
2081 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2082 err = got_error_from_errno("asprintf");
2083 free(id_str);
2084 goto done;
2086 free(id_str);
2087 } else if (status != GOT_STATUS_ADD) {
2088 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2089 if (err)
2090 goto done;
2093 if (status != GOT_STATUS_DELETE) {
2094 if (asprintf(&abspath, "%s/%s",
2095 got_worktree_get_root_path(a->worktree), path) == -1) {
2096 err = got_error_from_errno("asprintf");
2097 goto done;
2100 f2 = fopen(abspath, "r");
2101 if (f2 == NULL) {
2102 err = got_error_from_errno2("fopen", abspath);
2103 goto done;
2105 if (lstat(abspath, &sb) == -1) {
2106 err = got_error_from_errno2("lstat", abspath);
2107 goto done;
2109 } else
2110 sb.st_size = 0;
2112 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2113 a->diff_context, a->ignore_whitespace, stdout);
2114 done:
2115 if (blob1)
2116 got_object_blob_close(blob1);
2117 if (f2 && fclose(f2) != 0 && err == NULL)
2118 err = got_error_from_errno("fclose");
2119 free(abspath);
2120 return err;
2123 static const struct got_error *
2124 match_object_id(struct got_object_id **id, char **label,
2125 const char *id_str, int obj_type, int resolve_tags,
2126 struct got_repository *repo)
2128 const struct got_error *err;
2129 struct got_tag_object *tag;
2130 struct got_reference *ref = NULL;
2132 *id = NULL;
2133 *label = NULL;
2135 if (resolve_tags) {
2136 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2137 repo);
2138 if (err == NULL) {
2139 *id = got_object_id_dup(
2140 got_object_tag_get_object_id(tag));
2141 if (*id == NULL)
2142 err = got_error_from_errno("got_object_id_dup");
2143 else if (asprintf(label, "refs/tags/%s",
2144 got_object_tag_get_name(tag)) == -1) {
2145 err = got_error_from_errno("asprintf");
2146 free(*id);
2147 *id = NULL;
2149 got_object_tag_close(tag);
2150 return err;
2151 } else if (err->code != GOT_ERR_NO_OBJ)
2152 return err;
2155 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2156 if (err) {
2157 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2158 return err;
2159 err = got_ref_open(&ref, repo, id_str, 0);
2160 if (err != NULL)
2161 goto done;
2162 *label = strdup(got_ref_get_name(ref));
2163 if (*label == NULL) {
2164 err = got_error_from_errno("strdup");
2165 goto done;
2167 err = got_ref_resolve(id, repo, ref);
2168 } else {
2169 err = got_object_id_str(label, *id);
2170 if (*label == NULL) {
2171 err = got_error_from_errno("strdup");
2172 goto done;
2175 done:
2176 if (ref)
2177 got_ref_close(ref);
2178 return err;
2182 static const struct got_error *
2183 cmd_diff(int argc, char *argv[])
2185 const struct got_error *error;
2186 struct got_repository *repo = NULL;
2187 struct got_worktree *worktree = NULL;
2188 char *cwd = NULL, *repo_path = NULL;
2189 struct got_object_id *id1 = NULL, *id2 = NULL;
2190 const char *id_str1 = NULL, *id_str2 = NULL;
2191 char *label1 = NULL, *label2 = NULL;
2192 int type1, type2;
2193 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2194 const char *errstr;
2195 char *path = NULL;
2197 #ifndef PROFILE
2198 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2199 NULL) == -1)
2200 err(1, "pledge");
2201 #endif
2203 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2204 switch (ch) {
2205 case 'C':
2206 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2207 if (errstr != NULL)
2208 err(1, "-C option %s", errstr);
2209 break;
2210 case 'r':
2211 repo_path = realpath(optarg, NULL);
2212 if (repo_path == NULL)
2213 return got_error_from_errno2("realpath",
2214 optarg);
2215 got_path_strip_trailing_slashes(repo_path);
2216 break;
2217 case 's':
2218 diff_staged = 1;
2219 break;
2220 case 'w':
2221 ignore_whitespace = 1;
2222 break;
2223 default:
2224 usage_diff();
2225 /* NOTREACHED */
2229 argc -= optind;
2230 argv += optind;
2232 cwd = getcwd(NULL, 0);
2233 if (cwd == NULL) {
2234 error = got_error_from_errno("getcwd");
2235 goto done;
2237 error = got_worktree_open(&worktree, cwd);
2238 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2239 goto done;
2240 if (argc <= 1) {
2241 if (worktree == NULL) {
2242 error = got_error(GOT_ERR_NOT_WORKTREE);
2243 goto done;
2245 if (repo_path)
2246 errx(1,
2247 "-r option can't be used when diffing a work tree");
2248 repo_path = strdup(got_worktree_get_repo_path(worktree));
2249 if (repo_path == NULL) {
2250 error = got_error_from_errno("strdup");
2251 goto done;
2253 if (argc == 1) {
2254 error = got_worktree_resolve_path(&path, worktree,
2255 argv[0]);
2256 if (error)
2257 goto done;
2258 } else {
2259 path = strdup("");
2260 if (path == NULL) {
2261 error = got_error_from_errno("strdup");
2262 goto done;
2265 } else if (argc == 2) {
2266 if (diff_staged)
2267 errx(1, "-s option can't be used when diffing "
2268 "objects in repository");
2269 id_str1 = argv[0];
2270 id_str2 = argv[1];
2271 if (worktree && repo_path == NULL) {
2272 repo_path =
2273 strdup(got_worktree_get_repo_path(worktree));
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2276 goto done;
2279 } else
2280 usage_diff();
2282 if (repo_path == NULL) {
2283 repo_path = getcwd(NULL, 0);
2284 if (repo_path == NULL)
2285 return got_error_from_errno("getcwd");
2288 error = got_repo_open(&repo, repo_path, NULL);
2289 free(repo_path);
2290 if (error != NULL)
2291 goto done;
2293 error = apply_unveil(got_repo_get_path(repo), 1,
2294 worktree ? got_worktree_get_root_path(worktree) : NULL);
2295 if (error)
2296 goto done;
2298 if (argc <= 1) {
2299 struct print_diff_arg arg;
2300 struct got_pathlist_head paths;
2301 char *id_str;
2303 TAILQ_INIT(&paths);
2305 error = got_object_id_str(&id_str,
2306 got_worktree_get_base_commit_id(worktree));
2307 if (error)
2308 goto done;
2309 arg.repo = repo;
2310 arg.worktree = worktree;
2311 arg.diff_context = diff_context;
2312 arg.id_str = id_str;
2313 arg.header_shown = 0;
2314 arg.diff_staged = diff_staged;
2315 arg.ignore_whitespace = ignore_whitespace;
2317 error = got_pathlist_append(&paths, path, NULL);
2318 if (error)
2319 goto done;
2321 error = got_worktree_status(worktree, &paths, repo, print_diff,
2322 &arg, check_cancelled, NULL);
2323 free(id_str);
2324 got_pathlist_free(&paths);
2325 goto done;
2328 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2329 repo);
2330 if (error)
2331 goto done;
2333 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2334 repo);
2335 if (error)
2336 goto done;
2338 error = got_object_get_type(&type1, repo, id1);
2339 if (error)
2340 goto done;
2342 error = got_object_get_type(&type2, repo, id2);
2343 if (error)
2344 goto done;
2346 if (type1 != type2) {
2347 error = got_error(GOT_ERR_OBJ_TYPE);
2348 goto done;
2351 switch (type1) {
2352 case GOT_OBJ_TYPE_BLOB:
2353 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2354 diff_context, ignore_whitespace, repo, stdout);
2355 break;
2356 case GOT_OBJ_TYPE_TREE:
2357 error = got_diff_objects_as_trees(id1, id2, "", "",
2358 diff_context, ignore_whitespace, repo, stdout);
2359 break;
2360 case GOT_OBJ_TYPE_COMMIT:
2361 printf("diff %s %s\n", label1, label2);
2362 error = got_diff_objects_as_commits(id1, id2, diff_context,
2363 ignore_whitespace, repo, stdout);
2364 break;
2365 default:
2366 error = got_error(GOT_ERR_OBJ_TYPE);
2369 done:
2370 free(label1);
2371 free(label2);
2372 free(id1);
2373 free(id2);
2374 free(path);
2375 if (worktree)
2376 got_worktree_close(worktree);
2377 if (repo) {
2378 const struct got_error *repo_error;
2379 repo_error = got_repo_close(repo);
2380 if (error == NULL)
2381 error = repo_error;
2383 return error;
2386 __dead static void
2387 usage_blame(void)
2389 fprintf(stderr,
2390 "usage: %s blame [-c commit] [-r repository-path] path\n",
2391 getprogname());
2392 exit(1);
2395 struct blame_line {
2396 int annotated;
2397 char *id_str;
2398 char *committer;
2399 char datebuf[11]; /* YYYY-MM-DD + NUL */
2402 struct blame_cb_args {
2403 struct blame_line *lines;
2404 int nlines;
2405 int nlines_prec;
2406 int lineno_cur;
2407 off_t *line_offsets;
2408 FILE *f;
2409 struct got_repository *repo;
2412 static const struct got_error *
2413 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2415 const struct got_error *err = NULL;
2416 struct blame_cb_args *a = arg;
2417 struct blame_line *bline;
2418 char *line = NULL;
2419 size_t linesize = 0;
2420 struct got_commit_object *commit = NULL;
2421 off_t offset;
2422 struct tm tm;
2423 time_t committer_time;
2425 if (nlines != a->nlines ||
2426 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2427 return got_error(GOT_ERR_RANGE);
2429 if (sigint_received)
2430 return got_error(GOT_ERR_ITER_COMPLETED);
2432 if (lineno == -1)
2433 return NULL; /* no change in this commit */
2435 /* Annotate this line. */
2436 bline = &a->lines[lineno - 1];
2437 if (bline->annotated)
2438 return NULL;
2439 err = got_object_id_str(&bline->id_str, id);
2440 if (err)
2441 return err;
2443 err = got_object_open_as_commit(&commit, a->repo, id);
2444 if (err)
2445 goto done;
2447 bline->committer = strdup(got_object_commit_get_committer(commit));
2448 if (bline->committer == NULL) {
2449 err = got_error_from_errno("strdup");
2450 goto done;
2453 committer_time = got_object_commit_get_committer_time(commit);
2454 if (localtime_r(&committer_time, &tm) == NULL)
2455 return got_error_from_errno("localtime_r");
2456 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2457 &tm) >= sizeof(bline->datebuf)) {
2458 err = got_error(GOT_ERR_NO_SPACE);
2459 goto done;
2461 bline->annotated = 1;
2463 /* Print lines annotated so far. */
2464 bline = &a->lines[a->lineno_cur - 1];
2465 if (!bline->annotated)
2466 goto done;
2468 offset = a->line_offsets[a->lineno_cur - 1];
2469 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2470 err = got_error_from_errno("fseeko");
2471 goto done;
2474 while (bline->annotated) {
2475 char *smallerthan, *at, *nl, *committer;
2476 size_t len;
2478 if (getline(&line, &linesize, a->f) == -1) {
2479 if (ferror(a->f))
2480 err = got_error_from_errno("getline");
2481 break;
2484 committer = bline->committer;
2485 smallerthan = strchr(committer, '<');
2486 if (smallerthan && smallerthan[1] != '\0')
2487 committer = smallerthan + 1;
2488 at = strchr(committer, '@');
2489 if (at)
2490 *at = '\0';
2491 len = strlen(committer);
2492 if (len >= 9)
2493 committer[8] = '\0';
2495 nl = strchr(line, '\n');
2496 if (nl)
2497 *nl = '\0';
2498 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2499 bline->id_str, bline->datebuf, committer, line);
2501 a->lineno_cur++;
2502 bline = &a->lines[a->lineno_cur - 1];
2504 done:
2505 if (commit)
2506 got_object_commit_close(commit);
2507 free(line);
2508 return err;
2511 static const struct got_error *
2512 cmd_blame(int argc, char *argv[])
2514 const struct got_error *error;
2515 struct got_repository *repo = NULL;
2516 struct got_worktree *worktree = NULL;
2517 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2518 struct got_object_id *obj_id = NULL;
2519 struct got_object_id *commit_id = NULL;
2520 struct got_blob_object *blob = NULL;
2521 char *commit_id_str = NULL;
2522 struct blame_cb_args bca;
2523 int ch, obj_type, i;
2524 size_t filesize;
2526 memset(&bca, 0, sizeof(bca));
2528 #ifndef PROFILE
2529 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2530 NULL) == -1)
2531 err(1, "pledge");
2532 #endif
2534 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2535 switch (ch) {
2536 case 'c':
2537 commit_id_str = optarg;
2538 break;
2539 case 'r':
2540 repo_path = realpath(optarg, NULL);
2541 if (repo_path == NULL)
2542 return got_error_from_errno2("realpath",
2543 optarg);
2544 got_path_strip_trailing_slashes(repo_path);
2545 break;
2546 default:
2547 usage_blame();
2548 /* NOTREACHED */
2552 argc -= optind;
2553 argv += optind;
2555 if (argc == 1)
2556 path = argv[0];
2557 else
2558 usage_blame();
2560 cwd = getcwd(NULL, 0);
2561 if (cwd == NULL) {
2562 error = got_error_from_errno("getcwd");
2563 goto done;
2565 if (repo_path == NULL) {
2566 error = got_worktree_open(&worktree, cwd);
2567 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2568 goto done;
2569 else
2570 error = NULL;
2571 if (worktree) {
2572 repo_path =
2573 strdup(got_worktree_get_repo_path(worktree));
2574 if (repo_path == NULL)
2575 error = got_error_from_errno("strdup");
2576 if (error)
2577 goto done;
2578 } else {
2579 repo_path = strdup(cwd);
2580 if (repo_path == NULL) {
2581 error = got_error_from_errno("strdup");
2582 goto done;
2587 error = got_repo_open(&repo, repo_path, NULL);
2588 if (error != NULL)
2589 goto done;
2591 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2592 if (error)
2593 goto done;
2595 if (worktree) {
2596 const char *prefix = got_worktree_get_path_prefix(worktree);
2597 char *p, *worktree_subdir = cwd +
2598 strlen(got_worktree_get_root_path(worktree));
2599 if (asprintf(&p, "%s%s%s%s%s",
2600 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2601 worktree_subdir, worktree_subdir[0] ? "/" : "",
2602 path) == -1) {
2603 error = got_error_from_errno("asprintf");
2604 goto done;
2606 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2607 free(p);
2608 } else {
2609 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2611 if (error)
2612 goto done;
2614 if (commit_id_str == NULL) {
2615 struct got_reference *head_ref;
2616 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2617 if (error != NULL)
2618 goto done;
2619 error = got_ref_resolve(&commit_id, repo, head_ref);
2620 got_ref_close(head_ref);
2621 if (error != NULL)
2622 goto done;
2623 } else {
2624 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2625 if (error)
2626 goto done;
2629 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2630 if (error)
2631 goto done;
2632 if (obj_id == NULL) {
2633 error = got_error(GOT_ERR_NO_OBJ);
2634 goto done;
2637 error = got_object_get_type(&obj_type, repo, obj_id);
2638 if (error)
2639 goto done;
2641 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2642 error = got_error(GOT_ERR_OBJ_TYPE);
2643 goto done;
2646 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2647 if (error)
2648 goto done;
2649 bca.f = got_opentemp();
2650 if (bca.f == NULL) {
2651 error = got_error_from_errno("got_opentemp");
2652 goto done;
2654 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2655 &bca.line_offsets, bca.f, blob);
2656 if (error || bca.nlines == 0)
2657 goto done;
2659 /* Don't include \n at EOF in the blame line count. */
2660 if (bca.line_offsets[bca.nlines - 1] == filesize)
2661 bca.nlines--;
2663 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2664 if (bca.lines == NULL) {
2665 error = got_error_from_errno("calloc");
2666 goto done;
2668 bca.lineno_cur = 1;
2669 bca.nlines_prec = 0;
2670 i = bca.nlines;
2671 while (i > 0) {
2672 i /= 10;
2673 bca.nlines_prec++;
2675 bca.repo = repo;
2677 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2678 check_cancelled, NULL);
2679 if (error)
2680 goto done;
2681 done:
2682 free(in_repo_path);
2683 free(repo_path);
2684 free(cwd);
2685 free(commit_id);
2686 free(obj_id);
2687 if (blob)
2688 got_object_blob_close(blob);
2689 if (worktree)
2690 got_worktree_close(worktree);
2691 if (repo) {
2692 const struct got_error *repo_error;
2693 repo_error = got_repo_close(repo);
2694 if (error == NULL)
2695 error = repo_error;
2697 if (bca.lines) {
2698 for (i = 0; i < bca.nlines; i++) {
2699 struct blame_line *bline = &bca.lines[i];
2700 free(bline->id_str);
2701 free(bline->committer);
2703 free(bca.lines);
2705 free(bca.line_offsets);
2706 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2707 error = got_error_from_errno("fclose");
2708 return error;
2711 __dead static void
2712 usage_tree(void)
2714 fprintf(stderr,
2715 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2716 getprogname());
2717 exit(1);
2720 static void
2721 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2722 const char *root_path)
2724 int is_root_path = (strcmp(path, root_path) == 0);
2725 const char *modestr = "";
2727 path += strlen(root_path);
2728 while (path[0] == '/')
2729 path++;
2731 if (got_object_tree_entry_is_submodule(te))
2732 modestr = "$";
2733 else if (S_ISLNK(te->mode))
2734 modestr = "@";
2735 else if (S_ISDIR(te->mode))
2736 modestr = "/";
2737 else if (te->mode & S_IXUSR)
2738 modestr = "*";
2740 printf("%s%s%s%s%s\n", id ? id : "", path,
2741 is_root_path ? "" : "/", te->name, modestr);
2744 static const struct got_error *
2745 print_tree(const char *path, struct got_object_id *commit_id,
2746 int show_ids, int recurse, const char *root_path,
2747 struct got_repository *repo)
2749 const struct got_error *err = NULL;
2750 struct got_object_id *tree_id = NULL;
2751 struct got_tree_object *tree = NULL;
2752 const struct got_tree_entries *entries;
2753 struct got_tree_entry *te;
2755 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2756 if (err)
2757 goto done;
2759 err = got_object_open_as_tree(&tree, repo, tree_id);
2760 if (err)
2761 goto done;
2762 entries = got_object_tree_get_entries(tree);
2763 te = SIMPLEQ_FIRST(&entries->head);
2764 while (te) {
2765 char *id = NULL;
2767 if (sigint_received || sigpipe_received)
2768 break;
2770 if (show_ids) {
2771 char *id_str;
2772 err = got_object_id_str(&id_str, te->id);
2773 if (err)
2774 goto done;
2775 if (asprintf(&id, "%s ", id_str) == -1) {
2776 err = got_error_from_errno("asprintf");
2777 free(id_str);
2778 goto done;
2780 free(id_str);
2782 print_entry(te, id, path, root_path);
2783 free(id);
2785 if (recurse && S_ISDIR(te->mode)) {
2786 char *child_path;
2787 if (asprintf(&child_path, "%s%s%s", path,
2788 path[0] == '/' && path[1] == '\0' ? "" : "/",
2789 te->name) == -1) {
2790 err = got_error_from_errno("asprintf");
2791 goto done;
2793 err = print_tree(child_path, commit_id, show_ids, 1,
2794 root_path, repo);
2795 free(child_path);
2796 if (err)
2797 goto done;
2800 te = SIMPLEQ_NEXT(te, entry);
2802 done:
2803 if (tree)
2804 got_object_tree_close(tree);
2805 free(tree_id);
2806 return err;
2809 static const struct got_error *
2810 cmd_tree(int argc, char *argv[])
2812 const struct got_error *error;
2813 struct got_repository *repo = NULL;
2814 struct got_worktree *worktree = NULL;
2815 const char *path;
2816 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2817 struct got_object_id *commit_id = NULL;
2818 char *commit_id_str = NULL;
2819 int show_ids = 0, recurse = 0;
2820 int ch;
2822 #ifndef PROFILE
2823 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2824 NULL) == -1)
2825 err(1, "pledge");
2826 #endif
2828 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2829 switch (ch) {
2830 case 'c':
2831 commit_id_str = optarg;
2832 break;
2833 case 'r':
2834 repo_path = realpath(optarg, NULL);
2835 if (repo_path == NULL)
2836 return got_error_from_errno2("realpath",
2837 optarg);
2838 got_path_strip_trailing_slashes(repo_path);
2839 break;
2840 case 'i':
2841 show_ids = 1;
2842 break;
2843 case 'R':
2844 recurse = 1;
2845 break;
2846 default:
2847 usage_tree();
2848 /* NOTREACHED */
2852 argc -= optind;
2853 argv += optind;
2855 if (argc == 1)
2856 path = argv[0];
2857 else if (argc > 1)
2858 usage_tree();
2859 else
2860 path = NULL;
2862 cwd = getcwd(NULL, 0);
2863 if (cwd == NULL) {
2864 error = got_error_from_errno("getcwd");
2865 goto done;
2867 if (repo_path == NULL) {
2868 error = got_worktree_open(&worktree, cwd);
2869 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2870 goto done;
2871 else
2872 error = NULL;
2873 if (worktree) {
2874 repo_path =
2875 strdup(got_worktree_get_repo_path(worktree));
2876 if (repo_path == NULL)
2877 error = got_error_from_errno("strdup");
2878 if (error)
2879 goto done;
2880 } else {
2881 repo_path = strdup(cwd);
2882 if (repo_path == NULL) {
2883 error = got_error_from_errno("strdup");
2884 goto done;
2889 error = got_repo_open(&repo, repo_path, NULL);
2890 if (error != NULL)
2891 goto done;
2893 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2894 if (error)
2895 goto done;
2897 if (path == NULL) {
2898 if (worktree) {
2899 char *p, *worktree_subdir = cwd +
2900 strlen(got_worktree_get_root_path(worktree));
2901 if (asprintf(&p, "%s/%s",
2902 got_worktree_get_path_prefix(worktree),
2903 worktree_subdir) == -1) {
2904 error = got_error_from_errno("asprintf");
2905 goto done;
2907 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2908 free(p);
2909 if (error)
2910 goto done;
2911 } else
2912 path = "/";
2914 if (in_repo_path == NULL) {
2915 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2916 if (error != NULL)
2917 goto done;
2920 if (commit_id_str == NULL) {
2921 struct got_reference *head_ref;
2922 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2923 if (error != NULL)
2924 goto done;
2925 error = got_ref_resolve(&commit_id, repo, head_ref);
2926 got_ref_close(head_ref);
2927 if (error != NULL)
2928 goto done;
2929 } else {
2930 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2931 if (error)
2932 goto done;
2935 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2936 in_repo_path, repo);
2937 done:
2938 free(in_repo_path);
2939 free(repo_path);
2940 free(cwd);
2941 free(commit_id);
2942 if (worktree)
2943 got_worktree_close(worktree);
2944 if (repo) {
2945 const struct got_error *repo_error;
2946 repo_error = got_repo_close(repo);
2947 if (error == NULL)
2948 error = repo_error;
2950 return error;
2953 __dead static void
2954 usage_status(void)
2956 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2957 exit(1);
2960 static const struct got_error *
2961 print_status(void *arg, unsigned char status, unsigned char staged_status,
2962 const char *path, struct got_object_id *blob_id,
2963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2965 if (status == staged_status && (status == GOT_STATUS_DELETE))
2966 status = GOT_STATUS_NO_CHANGE;
2967 printf("%c%c %s\n", status, staged_status, path);
2968 return NULL;
2971 static const struct got_error *
2972 cmd_status(int argc, char *argv[])
2974 const struct got_error *error = NULL;
2975 struct got_repository *repo = NULL;
2976 struct got_worktree *worktree = NULL;
2977 char *cwd = NULL;
2978 struct got_pathlist_head paths;
2979 struct got_pathlist_entry *pe;
2980 int ch;
2982 TAILQ_INIT(&paths);
2984 while ((ch = getopt(argc, argv, "")) != -1) {
2985 switch (ch) {
2986 default:
2987 usage_status();
2988 /* NOTREACHED */
2992 argc -= optind;
2993 argv += optind;
2995 #ifndef PROFILE
2996 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2997 NULL) == -1)
2998 err(1, "pledge");
2999 #endif
3000 cwd = getcwd(NULL, 0);
3001 if (cwd == NULL) {
3002 error = got_error_from_errno("getcwd");
3003 goto done;
3006 error = got_worktree_open(&worktree, cwd);
3007 if (error != NULL)
3008 goto done;
3010 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3011 NULL);
3012 if (error != NULL)
3013 goto done;
3015 error = apply_unveil(got_repo_get_path(repo), 1,
3016 got_worktree_get_root_path(worktree));
3017 if (error)
3018 goto done;
3020 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3021 if (error)
3022 goto done;
3024 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3025 check_cancelled, NULL);
3026 done:
3027 TAILQ_FOREACH(pe, &paths, entry)
3028 free((char *)pe->path);
3029 got_pathlist_free(&paths);
3030 free(cwd);
3031 return error;
3034 __dead static void
3035 usage_ref(void)
3037 fprintf(stderr,
3038 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3039 getprogname());
3040 exit(1);
3043 static const struct got_error *
3044 list_refs(struct got_repository *repo)
3046 static const struct got_error *err = NULL;
3047 struct got_reflist_head refs;
3048 struct got_reflist_entry *re;
3050 SIMPLEQ_INIT(&refs);
3051 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3052 if (err)
3053 return err;
3055 SIMPLEQ_FOREACH(re, &refs, entry) {
3056 char *refstr;
3057 refstr = got_ref_to_str(re->ref);
3058 if (refstr == NULL)
3059 return got_error_from_errno("got_ref_to_str");
3060 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3061 free(refstr);
3064 got_ref_list_free(&refs);
3065 return NULL;
3068 static const struct got_error *
3069 delete_ref(struct got_repository *repo, const char *refname)
3071 const struct got_error *err = NULL;
3072 struct got_reference *ref;
3074 err = got_ref_open(&ref, repo, refname, 0);
3075 if (err)
3076 return err;
3078 err = got_ref_delete(ref, repo);
3079 got_ref_close(ref);
3080 return err;
3083 static const struct got_error *
3084 add_ref(struct got_repository *repo, const char *refname, const char *target)
3086 const struct got_error *err = NULL;
3087 struct got_object_id *id;
3088 struct got_reference *ref = NULL;
3091 * Don't let the user create a reference named '-'.
3092 * While technically a valid reference name, this case is usually
3093 * an unintended typo.
3095 if (refname[0] == '-' && refname[1] == '\0')
3096 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3098 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3099 repo);
3100 if (err) {
3101 struct got_reference *target_ref;
3103 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3104 return err;
3105 err = got_ref_open(&target_ref, repo, target, 0);
3106 if (err)
3107 return err;
3108 err = got_ref_resolve(&id, repo, target_ref);
3109 got_ref_close(target_ref);
3110 if (err)
3111 return err;
3114 err = got_ref_alloc(&ref, refname, id);
3115 if (err)
3116 goto done;
3118 err = got_ref_write(ref, repo);
3119 done:
3120 if (ref)
3121 got_ref_close(ref);
3122 free(id);
3123 return err;
3126 static const struct got_error *
3127 add_symref(struct got_repository *repo, const char *refname, const char *target)
3129 const struct got_error *err = NULL;
3130 struct got_reference *ref = NULL;
3131 struct got_reference *target_ref = NULL;
3134 * Don't let the user create a reference named '-'.
3135 * While technically a valid reference name, this case is usually
3136 * an unintended typo.
3138 if (refname[0] == '-' && refname[1] == '\0')
3139 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3141 err = got_ref_open(&target_ref, repo, target, 0);
3142 if (err)
3143 return err;
3145 err = got_ref_alloc_symref(&ref, refname, target_ref);
3146 if (err)
3147 goto done;
3149 err = got_ref_write(ref, repo);
3150 done:
3151 if (target_ref)
3152 got_ref_close(target_ref);
3153 if (ref)
3154 got_ref_close(ref);
3155 return err;
3158 static const struct got_error *
3159 cmd_ref(int argc, char *argv[])
3161 const struct got_error *error = NULL;
3162 struct got_repository *repo = NULL;
3163 struct got_worktree *worktree = NULL;
3164 char *cwd = NULL, *repo_path = NULL;
3165 int ch, do_list = 0, create_symref = 0;
3166 const char *delref = NULL;
3168 /* TODO: Add -s option for adding symbolic references. */
3169 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3170 switch (ch) {
3171 case 'd':
3172 delref = optarg;
3173 break;
3174 case 'r':
3175 repo_path = realpath(optarg, NULL);
3176 if (repo_path == NULL)
3177 return got_error_from_errno2("realpath",
3178 optarg);
3179 got_path_strip_trailing_slashes(repo_path);
3180 break;
3181 case 'l':
3182 do_list = 1;
3183 break;
3184 case 's':
3185 create_symref = 1;
3186 break;
3187 default:
3188 usage_ref();
3189 /* NOTREACHED */
3193 if (do_list && delref)
3194 errx(1, "-l and -d options are mutually exclusive\n");
3196 argc -= optind;
3197 argv += optind;
3199 if (do_list || delref) {
3200 if (create_symref)
3201 errx(1, "-s option cannot be used together with the "
3202 "-l or -d options");
3203 if (argc > 0)
3204 usage_ref();
3205 } else if (argc != 2)
3206 usage_ref();
3208 #ifndef PROFILE
3209 if (do_list) {
3210 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3211 NULL) == -1)
3212 err(1, "pledge");
3213 } else {
3214 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3215 "sendfd unveil", NULL) == -1)
3216 err(1, "pledge");
3218 #endif
3219 cwd = getcwd(NULL, 0);
3220 if (cwd == NULL) {
3221 error = got_error_from_errno("getcwd");
3222 goto done;
3225 if (repo_path == NULL) {
3226 error = got_worktree_open(&worktree, cwd);
3227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3228 goto done;
3229 else
3230 error = NULL;
3231 if (worktree) {
3232 repo_path =
3233 strdup(got_worktree_get_repo_path(worktree));
3234 if (repo_path == NULL)
3235 error = got_error_from_errno("strdup");
3236 if (error)
3237 goto done;
3238 } else {
3239 repo_path = strdup(cwd);
3240 if (repo_path == NULL) {
3241 error = got_error_from_errno("strdup");
3242 goto done;
3247 error = got_repo_open(&repo, repo_path, NULL);
3248 if (error != NULL)
3249 goto done;
3251 error = apply_unveil(got_repo_get_path(repo), do_list,
3252 worktree ? got_worktree_get_root_path(worktree) : NULL);
3253 if (error)
3254 goto done;
3256 if (do_list)
3257 error = list_refs(repo);
3258 else if (delref)
3259 error = delete_ref(repo, delref);
3260 else if (create_symref)
3261 error = add_symref(repo, argv[0], argv[1]);
3262 else
3263 error = add_ref(repo, argv[0], argv[1]);
3264 done:
3265 if (repo)
3266 got_repo_close(repo);
3267 if (worktree)
3268 got_worktree_close(worktree);
3269 free(cwd);
3270 free(repo_path);
3271 return error;
3274 __dead static void
3275 usage_branch(void)
3277 fprintf(stderr,
3278 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3279 "[name]\n", getprogname());
3280 exit(1);
3283 static const struct got_error *
3284 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3285 struct got_reference *ref)
3287 const struct got_error *err = NULL;
3288 const char *refname, *marker = " ";
3289 char *refstr;
3291 refname = got_ref_get_name(ref);
3292 if (worktree && strcmp(refname,
3293 got_worktree_get_head_ref_name(worktree)) == 0) {
3294 struct got_object_id *id = NULL;
3296 err = got_ref_resolve(&id, repo, ref);
3297 if (err)
3298 return err;
3299 if (got_object_id_cmp(id,
3300 got_worktree_get_base_commit_id(worktree)) == 0)
3301 marker = "* ";
3302 else
3303 marker = "~ ";
3304 free(id);
3307 if (strncmp(refname, "refs/heads/", 11) == 0)
3308 refname += 11;
3309 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3310 refname += 18;
3312 refstr = got_ref_to_str(ref);
3313 if (refstr == NULL)
3314 return got_error_from_errno("got_ref_to_str");
3316 printf("%s%s: %s\n", marker, refname, refstr);
3317 free(refstr);
3318 return NULL;
3321 static const struct got_error *
3322 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3324 const char *refname;
3326 if (worktree == NULL)
3327 return got_error(GOT_ERR_NOT_WORKTREE);
3329 refname = got_worktree_get_head_ref_name(worktree);
3331 if (strncmp(refname, "refs/heads/", 11) == 0)
3332 refname += 11;
3333 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3334 refname += 18;
3336 printf("%s\n", refname);
3338 return NULL;
3341 static const struct got_error *
3342 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3344 static const struct got_error *err = NULL;
3345 struct got_reflist_head refs;
3346 struct got_reflist_entry *re;
3347 struct got_reference *temp_ref = NULL;
3348 int rebase_in_progress, histedit_in_progress;
3350 SIMPLEQ_INIT(&refs);
3352 if (worktree) {
3353 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3354 worktree);
3355 if (err)
3356 return err;
3358 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3359 worktree);
3360 if (err)
3361 return err;
3363 if (rebase_in_progress || histedit_in_progress) {
3364 err = got_ref_open(&temp_ref, repo,
3365 got_worktree_get_head_ref_name(worktree), 0);
3366 if (err)
3367 return err;
3368 list_branch(repo, worktree, temp_ref);
3369 got_ref_close(temp_ref);
3373 err = got_ref_list(&refs, repo, "refs/heads",
3374 got_ref_cmp_by_name, NULL);
3375 if (err)
3376 return err;
3378 SIMPLEQ_FOREACH(re, &refs, entry)
3379 list_branch(repo, worktree, re->ref);
3381 got_ref_list_free(&refs);
3382 return NULL;
3385 static const struct got_error *
3386 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3387 const char *branch_name)
3389 const struct got_error *err = NULL;
3390 struct got_reference *ref = NULL;
3391 char *refname;
3393 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3394 return got_error_from_errno("asprintf");
3396 err = got_ref_open(&ref, repo, refname, 0);
3397 if (err)
3398 goto done;
3400 if (worktree &&
3401 strcmp(got_worktree_get_head_ref_name(worktree),
3402 got_ref_get_name(ref)) == 0) {
3403 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3404 "will not delete this work tree's current branch");
3405 goto done;
3408 err = got_ref_delete(ref, repo);
3409 done:
3410 if (ref)
3411 got_ref_close(ref);
3412 free(refname);
3413 return err;
3416 static const struct got_error *
3417 add_branch(struct got_repository *repo, const char *branch_name,
3418 struct got_object_id *base_commit_id)
3420 const struct got_error *err = NULL;
3421 struct got_reference *ref = NULL;
3422 char *base_refname = NULL, *refname = NULL;
3425 * Don't let the user create a branch named '-'.
3426 * While technically a valid reference name, this case is usually
3427 * an unintended typo.
3429 if (branch_name[0] == '-' && branch_name[1] == '\0')
3430 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3432 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto done;
3437 err = got_ref_open(&ref, repo, refname, 0);
3438 if (err == NULL) {
3439 err = got_error(GOT_ERR_BRANCH_EXISTS);
3440 goto done;
3441 } else if (err->code != GOT_ERR_NOT_REF)
3442 goto done;
3444 err = got_ref_alloc(&ref, refname, base_commit_id);
3445 if (err)
3446 goto done;
3448 err = got_ref_write(ref, repo);
3449 done:
3450 if (ref)
3451 got_ref_close(ref);
3452 free(base_refname);
3453 free(refname);
3454 return err;
3457 static const struct got_error *
3458 cmd_branch(int argc, char *argv[])
3460 const struct got_error *error = NULL;
3461 struct got_repository *repo = NULL;
3462 struct got_worktree *worktree = NULL;
3463 char *cwd = NULL, *repo_path = NULL;
3464 int ch, do_list = 0, do_show = 0;
3465 const char *delref = NULL, *commit_id_arg = NULL;
3467 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3468 switch (ch) {
3469 case 'c':
3470 commit_id_arg = optarg;
3471 break;
3472 case 'd':
3473 delref = optarg;
3474 break;
3475 case 'r':
3476 repo_path = realpath(optarg, NULL);
3477 if (repo_path == NULL)
3478 return got_error_from_errno2("realpath",
3479 optarg);
3480 got_path_strip_trailing_slashes(repo_path);
3481 break;
3482 case 'l':
3483 do_list = 1;
3484 break;
3485 default:
3486 usage_branch();
3487 /* NOTREACHED */
3491 if (do_list && delref)
3492 errx(1, "-l and -d options are mutually exclusive\n");
3494 argc -= optind;
3495 argv += optind;
3497 if (!do_list && !delref && argc == 0)
3498 do_show = 1;
3500 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3501 errx(1, "-c option can only be used when creating a branch");
3503 if (do_list || delref) {
3504 if (argc > 0)
3505 usage_branch();
3506 } else if (!do_show && argc != 1)
3507 usage_branch();
3509 #ifndef PROFILE
3510 if (do_list || do_show) {
3511 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3512 NULL) == -1)
3513 err(1, "pledge");
3514 } else {
3515 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3516 "sendfd unveil", NULL) == -1)
3517 err(1, "pledge");
3519 #endif
3520 cwd = getcwd(NULL, 0);
3521 if (cwd == NULL) {
3522 error = got_error_from_errno("getcwd");
3523 goto done;
3526 if (repo_path == NULL) {
3527 error = got_worktree_open(&worktree, cwd);
3528 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3529 goto done;
3530 else
3531 error = NULL;
3532 if (worktree) {
3533 repo_path =
3534 strdup(got_worktree_get_repo_path(worktree));
3535 if (repo_path == NULL)
3536 error = got_error_from_errno("strdup");
3537 if (error)
3538 goto done;
3539 } else {
3540 repo_path = strdup(cwd);
3541 if (repo_path == NULL) {
3542 error = got_error_from_errno("strdup");
3543 goto done;
3548 error = got_repo_open(&repo, repo_path, NULL);
3549 if (error != NULL)
3550 goto done;
3552 error = apply_unveil(got_repo_get_path(repo), do_list,
3553 worktree ? got_worktree_get_root_path(worktree) : NULL);
3554 if (error)
3555 goto done;
3557 if (do_show)
3558 error = show_current_branch(repo, worktree);
3559 else if (do_list)
3560 error = list_branches(repo, worktree);
3561 else if (delref)
3562 error = delete_branch(repo, worktree, delref);
3563 else {
3564 struct got_object_id *commit_id;
3565 if (commit_id_arg == NULL)
3566 commit_id_arg = worktree ?
3567 got_worktree_get_head_ref_name(worktree) :
3568 GOT_REF_HEAD;
3569 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3570 if (error)
3571 goto done;
3572 error = add_branch(repo, argv[0], commit_id);
3573 free(commit_id);
3575 done:
3576 if (repo)
3577 got_repo_close(repo);
3578 if (worktree)
3579 got_worktree_close(worktree);
3580 free(cwd);
3581 free(repo_path);
3582 return error;
3586 __dead static void
3587 usage_tag(void)
3589 fprintf(stderr,
3590 "usage: %s tag [-r repository] | -l | "
3591 "[-m message] name [commit]\n", getprogname());
3592 exit(1);
3595 #if 0
3596 static const struct got_error *
3597 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3599 const struct got_error *err = NULL;
3600 struct got_reflist_entry *re, *se, *new;
3601 struct got_object_id *re_id, *se_id;
3602 struct got_tag_object *re_tag, *se_tag;
3603 time_t re_time, se_time;
3605 SIMPLEQ_FOREACH(re, tags, entry) {
3606 se = SIMPLEQ_FIRST(sorted);
3607 if (se == NULL) {
3608 err = got_reflist_entry_dup(&new, re);
3609 if (err)
3610 return err;
3611 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3612 continue;
3613 } else {
3614 err = got_ref_resolve(&re_id, repo, re->ref);
3615 if (err)
3616 break;
3617 err = got_object_open_as_tag(&re_tag, repo, re_id);
3618 free(re_id);
3619 if (err)
3620 break;
3621 re_time = got_object_tag_get_tagger_time(re_tag);
3622 got_object_tag_close(re_tag);
3625 while (se) {
3626 err = got_ref_resolve(&se_id, repo, re->ref);
3627 if (err)
3628 break;
3629 err = got_object_open_as_tag(&se_tag, repo, se_id);
3630 free(se_id);
3631 if (err)
3632 break;
3633 se_time = got_object_tag_get_tagger_time(se_tag);
3634 got_object_tag_close(se_tag);
3636 if (se_time > re_time) {
3637 err = got_reflist_entry_dup(&new, re);
3638 if (err)
3639 return err;
3640 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3641 break;
3643 se = SIMPLEQ_NEXT(se, entry);
3644 continue;
3647 done:
3648 return err;
3650 #endif
3652 static const struct got_error *
3653 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3654 struct got_reference *ref2)
3656 const struct got_error *err = NULL;
3657 struct got_repository *repo = arg;
3658 struct got_object_id *id1, *id2 = NULL;
3659 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3660 time_t time1, time2;
3662 *cmp = 0;
3664 err = got_ref_resolve(&id1, repo, ref1);
3665 if (err)
3666 return err;
3667 err = got_object_open_as_tag(&tag1, repo, id1);
3668 if (err)
3669 goto done;
3671 err = got_ref_resolve(&id2, repo, ref2);
3672 if (err)
3673 goto done;
3674 err = got_object_open_as_tag(&tag2, repo, id2);
3675 if (err)
3676 goto done;
3678 time1 = got_object_tag_get_tagger_time(tag1);
3679 time2 = got_object_tag_get_tagger_time(tag2);
3681 /* Put latest tags first. */
3682 if (time1 < time2)
3683 *cmp = 1;
3684 else if (time1 > time2)
3685 *cmp = -1;
3686 else
3687 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3688 done:
3689 free(id1);
3690 free(id2);
3691 if (tag1)
3692 got_object_tag_close(tag1);
3693 if (tag2)
3694 got_object_tag_close(tag2);
3695 return err;
3698 static const struct got_error *
3699 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3701 static const struct got_error *err = NULL;
3702 struct got_reflist_head refs;
3703 struct got_reflist_entry *re;
3705 SIMPLEQ_INIT(&refs);
3707 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3708 if (err)
3709 return err;
3711 SIMPLEQ_FOREACH(re, &refs, entry) {
3712 const char *refname;
3713 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3714 char datebuf[26];
3715 time_t tagger_time;
3716 struct got_object_id *id;
3717 struct got_tag_object *tag;
3719 refname = got_ref_get_name(re->ref);
3720 if (strncmp(refname, "refs/tags/", 10) != 0)
3721 continue;
3722 refname += 10;
3723 refstr = got_ref_to_str(re->ref);
3724 if (refstr == NULL) {
3725 err = got_error_from_errno("got_ref_to_str");
3726 break;
3728 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3729 free(refstr);
3731 err = got_ref_resolve(&id, repo, re->ref);
3732 if (err)
3733 break;
3734 err = got_object_open_as_tag(&tag, repo, id);
3735 free(id);
3736 if (err)
3737 break;
3738 printf("from: %s\n", got_object_tag_get_tagger(tag));
3739 tagger_time = got_object_tag_get_tagger_time(tag);
3740 datestr = get_datestr(&tagger_time, datebuf);
3741 if (datestr)
3742 printf("date: %s UTC\n", datestr);
3743 err = got_object_id_str(&id_str,
3744 got_object_tag_get_object_id(tag));
3745 if (err)
3746 break;
3747 switch (got_object_tag_get_object_type(tag)) {
3748 case GOT_OBJ_TYPE_BLOB:
3749 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3750 break;
3751 case GOT_OBJ_TYPE_TREE:
3752 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3753 break;
3754 case GOT_OBJ_TYPE_COMMIT:
3755 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3756 break;
3757 case GOT_OBJ_TYPE_TAG:
3758 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3759 break;
3760 default:
3761 break;
3763 free(id_str);
3764 tagmsg0 = strdup(got_object_tag_get_message(tag));
3765 got_object_tag_close(tag);
3766 if (tagmsg0 == NULL) {
3767 err = got_error_from_errno("strdup");
3768 break;
3771 tagmsg = tagmsg0;
3772 do {
3773 line = strsep(&tagmsg, "\n");
3774 if (line)
3775 printf(" %s\n", line);
3776 } while (line);
3777 free(tagmsg0);
3780 got_ref_list_free(&refs);
3781 return NULL;
3784 static const struct got_error *
3785 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3786 const char *tag_name, const char *repo_path)
3788 const struct got_error *err = NULL;
3789 char *template = NULL, *initial_content = NULL;
3790 char *editor = NULL;
3791 int fd = -1;
3793 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3794 err = got_error_from_errno("asprintf");
3795 goto done;
3798 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3799 commit_id_str, tag_name) == -1) {
3800 err = got_error_from_errno("asprintf");
3801 goto done;
3804 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3805 if (err)
3806 goto done;
3808 dprintf(fd, initial_content);
3809 close(fd);
3811 err = get_editor(&editor);
3812 if (err)
3813 goto done;
3814 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3815 done:
3816 free(initial_content);
3817 free(template);
3818 free(editor);
3820 /* Editor is done; we can now apply unveil(2) */
3821 if (err == NULL) {
3822 err = apply_unveil(repo_path, 0, NULL);
3823 if (err) {
3824 free(*tagmsg);
3825 *tagmsg = NULL;
3828 return err;
3831 static const struct got_error *
3832 add_tag(struct got_repository *repo, const char *tag_name,
3833 const char *commit_arg, const char *tagmsg_arg)
3835 const struct got_error *err = NULL;
3836 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3837 char *label = NULL, *commit_id_str = NULL;
3838 struct got_reference *ref = NULL;
3839 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3840 char *tagmsg_path = NULL, *tag_id_str = NULL;
3841 int preserve_tagmsg = 0;
3844 * Don't let the user create a tag named '-'.
3845 * While technically a valid reference name, this case is usually
3846 * an unintended typo.
3848 if (tag_name[0] == '-' && tag_name[1] == '\0')
3849 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3851 err = get_author(&tagger, repo);
3852 if (err)
3853 return err;
3855 err = match_object_id(&commit_id, &label, commit_arg,
3856 GOT_OBJ_TYPE_COMMIT, 1, repo);
3857 if (err)
3858 goto done;
3860 err = got_object_id_str(&commit_id_str, commit_id);
3861 if (err)
3862 goto done;
3864 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3865 refname = strdup(tag_name);
3866 if (refname == NULL) {
3867 err = got_error_from_errno("strdup");
3868 goto done;
3870 tag_name += 10;
3871 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3872 err = got_error_from_errno("asprintf");
3873 goto done;
3876 err = got_ref_open(&ref, repo, refname, 0);
3877 if (err == NULL) {
3878 err = got_error(GOT_ERR_TAG_EXISTS);
3879 goto done;
3880 } else if (err->code != GOT_ERR_NOT_REF)
3881 goto done;
3883 if (tagmsg_arg == NULL) {
3884 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3885 tag_name, got_repo_get_path(repo));
3886 if (err) {
3887 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3888 tagmsg_path != NULL)
3889 preserve_tagmsg = 1;
3890 goto done;
3894 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3895 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3896 if (err) {
3897 if (tagmsg_path)
3898 preserve_tagmsg = 1;
3899 goto done;
3902 err = got_ref_alloc(&ref, refname, tag_id);
3903 if (err) {
3904 if (tagmsg_path)
3905 preserve_tagmsg = 1;
3906 goto done;
3909 err = got_ref_write(ref, repo);
3910 if (err) {
3911 if (tagmsg_path)
3912 preserve_tagmsg = 1;
3913 goto done;
3916 err = got_object_id_str(&tag_id_str, tag_id);
3917 if (err) {
3918 if (tagmsg_path)
3919 preserve_tagmsg = 1;
3920 goto done;
3922 printf("Created tag %s\n", tag_id_str);
3923 done:
3924 if (preserve_tagmsg) {
3925 fprintf(stderr, "%s: tag message preserved in %s\n",
3926 getprogname(), tagmsg_path);
3927 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3928 err = got_error_from_errno2("unlink", tagmsg_path);
3929 free(tag_id_str);
3930 if (ref)
3931 got_ref_close(ref);
3932 free(commit_id);
3933 free(commit_id_str);
3934 free(refname);
3935 free(tagmsg);
3936 free(tagmsg_path);
3937 free(tagger);
3938 return err;
3941 static const struct got_error *
3942 cmd_tag(int argc, char *argv[])
3944 const struct got_error *error = NULL;
3945 struct got_repository *repo = NULL;
3946 struct got_worktree *worktree = NULL;
3947 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3948 char *gitconfig_path = NULL;
3949 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3950 int ch, do_list = 0;
3952 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3953 switch (ch) {
3954 case 'm':
3955 tagmsg = optarg;
3956 break;
3957 case 'r':
3958 repo_path = realpath(optarg, NULL);
3959 if (repo_path == NULL)
3960 return got_error_from_errno2("realpath",
3961 optarg);
3962 got_path_strip_trailing_slashes(repo_path);
3963 break;
3964 case 'l':
3965 do_list = 1;
3966 break;
3967 default:
3968 usage_tag();
3969 /* NOTREACHED */
3973 argc -= optind;
3974 argv += optind;
3976 if (do_list) {
3977 if (tagmsg)
3978 errx(1, "-l and -m options are mutually exclusive\n");
3979 if (argc > 0)
3980 usage_tag();
3981 } else if (argc < 1 || argc > 2)
3982 usage_tag();
3983 else if (argc > 1)
3984 commit_id_arg = argv[1];
3985 tag_name = argv[0];
3987 #ifndef PROFILE
3988 if (do_list) {
3989 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3990 NULL) == -1)
3991 err(1, "pledge");
3992 } else {
3993 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3994 "sendfd unveil", NULL) == -1)
3995 err(1, "pledge");
3997 #endif
3998 cwd = getcwd(NULL, 0);
3999 if (cwd == NULL) {
4000 error = got_error_from_errno("getcwd");
4001 goto done;
4004 if (repo_path == NULL) {
4005 error = got_worktree_open(&worktree, cwd);
4006 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4007 goto done;
4008 else
4009 error = NULL;
4010 if (worktree) {
4011 repo_path =
4012 strdup(got_worktree_get_repo_path(worktree));
4013 if (repo_path == NULL)
4014 error = got_error_from_errno("strdup");
4015 if (error)
4016 goto done;
4017 } else {
4018 repo_path = strdup(cwd);
4019 if (repo_path == NULL) {
4020 error = got_error_from_errno("strdup");
4021 goto done;
4026 if (do_list) {
4027 error = got_repo_open(&repo, repo_path, NULL);
4028 if (error != NULL)
4029 goto done;
4030 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4031 if (error)
4032 goto done;
4033 error = list_tags(repo, worktree);
4034 } else {
4035 error = get_gitconfig_path(&gitconfig_path);
4036 if (error)
4037 goto done;
4038 error = got_repo_open(&repo, repo_path, gitconfig_path);
4039 if (error != NULL)
4040 goto done;
4042 if (tagmsg) {
4043 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4044 if (error)
4045 goto done;
4048 if (commit_id_arg == NULL) {
4049 struct got_reference *head_ref;
4050 struct got_object_id *commit_id;
4051 error = got_ref_open(&head_ref, repo,
4052 worktree ? got_worktree_get_head_ref_name(worktree)
4053 : GOT_REF_HEAD, 0);
4054 if (error)
4055 goto done;
4056 error = got_ref_resolve(&commit_id, repo, head_ref);
4057 got_ref_close(head_ref);
4058 if (error)
4059 goto done;
4060 error = got_object_id_str(&commit_id_str, commit_id);
4061 free(commit_id);
4062 if (error)
4063 goto done;
4066 error = add_tag(repo, tag_name,
4067 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4069 done:
4070 if (repo)
4071 got_repo_close(repo);
4072 if (worktree)
4073 got_worktree_close(worktree);
4074 free(cwd);
4075 free(repo_path);
4076 free(gitconfig_path);
4077 free(commit_id_str);
4078 return error;
4081 __dead static void
4082 usage_add(void)
4084 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4085 exit(1);
4088 static const struct got_error *
4089 add_progress(void *arg, unsigned char status, const char *path)
4091 while (path[0] == '/')
4092 path++;
4093 printf("%c %s\n", status, path);
4094 return NULL;
4097 static const struct got_error *
4098 cmd_add(int argc, char *argv[])
4100 const struct got_error *error = NULL;
4101 struct got_repository *repo = NULL;
4102 struct got_worktree *worktree = NULL;
4103 char *cwd = NULL;
4104 struct got_pathlist_head paths;
4105 struct got_pathlist_entry *pe;
4106 int ch, can_recurse = 0;
4108 TAILQ_INIT(&paths);
4110 while ((ch = getopt(argc, argv, "R")) != -1) {
4111 switch (ch) {
4112 case 'R':
4113 can_recurse = 1;
4114 break;
4115 default:
4116 usage_add();
4117 /* NOTREACHED */
4121 argc -= optind;
4122 argv += optind;
4124 #ifndef PROFILE
4125 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4126 NULL) == -1)
4127 err(1, "pledge");
4128 #endif
4129 if (argc < 1)
4130 usage_add();
4132 cwd = getcwd(NULL, 0);
4133 if (cwd == NULL) {
4134 error = got_error_from_errno("getcwd");
4135 goto done;
4138 error = got_worktree_open(&worktree, cwd);
4139 if (error)
4140 goto done;
4142 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4143 NULL);
4144 if (error != NULL)
4145 goto done;
4147 error = apply_unveil(got_repo_get_path(repo), 1,
4148 got_worktree_get_root_path(worktree));
4149 if (error)
4150 goto done;
4152 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4153 if (error)
4154 goto done;
4156 if (!can_recurse) {
4157 char *ondisk_path;
4158 struct stat sb;
4159 TAILQ_FOREACH(pe, &paths, entry) {
4160 if (asprintf(&ondisk_path, "%s/%s",
4161 got_worktree_get_root_path(worktree),
4162 pe->path) == -1) {
4163 error = got_error_from_errno("asprintf");
4164 goto done;
4166 if (lstat(ondisk_path, &sb) == -1) {
4167 if (errno == ENOENT) {
4168 free(ondisk_path);
4169 continue;
4171 error = got_error_from_errno2("lstat",
4172 ondisk_path);
4173 free(ondisk_path);
4174 goto done;
4176 free(ondisk_path);
4177 if (S_ISDIR(sb.st_mode)) {
4178 error = got_error_msg(GOT_ERR_BAD_PATH,
4179 "adding directories requires -R option");
4180 goto done;
4184 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4185 NULL, repo);
4186 done:
4187 if (repo)
4188 got_repo_close(repo);
4189 if (worktree)
4190 got_worktree_close(worktree);
4191 TAILQ_FOREACH(pe, &paths, entry)
4192 free((char *)pe->path);
4193 got_pathlist_free(&paths);
4194 free(cwd);
4195 return error;
4198 __dead static void
4199 usage_remove(void)
4201 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4202 exit(1);
4205 static const struct got_error *
4206 print_remove_status(void *arg, unsigned char status,
4207 unsigned char staged_status, const char *path,
4208 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4209 struct got_object_id *commit_id)
4211 if (status == GOT_STATUS_NONEXISTENT)
4212 return NULL;
4213 if (status == staged_status && (status == GOT_STATUS_DELETE))
4214 status = GOT_STATUS_NO_CHANGE;
4215 printf("%c%c %s\n", status, staged_status, path);
4216 return NULL;
4219 static const struct got_error *
4220 cmd_remove(int argc, char *argv[])
4222 const struct got_error *error = NULL;
4223 struct got_worktree *worktree = NULL;
4224 struct got_repository *repo = NULL;
4225 char *cwd = NULL;
4226 struct got_pathlist_head paths;
4227 struct got_pathlist_entry *pe;
4228 int ch, delete_local_mods = 0;
4230 TAILQ_INIT(&paths);
4232 while ((ch = getopt(argc, argv, "f")) != -1) {
4233 switch (ch) {
4234 case 'f':
4235 delete_local_mods = 1;
4236 break;
4237 default:
4238 usage_add();
4239 /* NOTREACHED */
4243 argc -= optind;
4244 argv += optind;
4246 #ifndef PROFILE
4247 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4248 NULL) == -1)
4249 err(1, "pledge");
4250 #endif
4251 if (argc < 1)
4252 usage_remove();
4254 cwd = getcwd(NULL, 0);
4255 if (cwd == NULL) {
4256 error = got_error_from_errno("getcwd");
4257 goto done;
4259 error = got_worktree_open(&worktree, cwd);
4260 if (error)
4261 goto done;
4263 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4264 NULL);
4265 if (error)
4266 goto done;
4268 error = apply_unveil(got_repo_get_path(repo), 1,
4269 got_worktree_get_root_path(worktree));
4270 if (error)
4271 goto done;
4273 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4274 if (error)
4275 goto done;
4277 error = got_worktree_schedule_delete(worktree, &paths,
4278 delete_local_mods, print_remove_status, NULL, repo);
4279 if (error)
4280 goto done;
4281 done:
4282 if (repo)
4283 got_repo_close(repo);
4284 if (worktree)
4285 got_worktree_close(worktree);
4286 TAILQ_FOREACH(pe, &paths, entry)
4287 free((char *)pe->path);
4288 got_pathlist_free(&paths);
4289 free(cwd);
4290 return error;
4293 __dead static void
4294 usage_revert(void)
4296 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4297 "path ...\n", getprogname());
4298 exit(1);
4301 static const struct got_error *
4302 revert_progress(void *arg, unsigned char status, const char *path)
4304 while (path[0] == '/')
4305 path++;
4306 printf("%c %s\n", status, path);
4307 return NULL;
4310 struct choose_patch_arg {
4311 FILE *patch_script_file;
4312 const char *action;
4315 static const struct got_error *
4316 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4317 int nchanges, const char *action)
4319 char *line = NULL;
4320 size_t linesize = 0;
4321 ssize_t linelen;
4323 switch (status) {
4324 case GOT_STATUS_ADD:
4325 printf("A %s\n%s this addition? [y/n] ", path, action);
4326 break;
4327 case GOT_STATUS_DELETE:
4328 printf("D %s\n%s this deletion? [y/n] ", path, action);
4329 break;
4330 case GOT_STATUS_MODIFY:
4331 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4332 return got_error_from_errno("fseek");
4333 printf(GOT_COMMIT_SEP_STR);
4334 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4335 printf("%s", line);
4336 if (ferror(patch_file))
4337 return got_error_from_errno("getline");
4338 printf(GOT_COMMIT_SEP_STR);
4339 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4340 path, n, nchanges, action);
4341 break;
4342 default:
4343 return got_error_path(path, GOT_ERR_FILE_STATUS);
4346 return NULL;
4349 static const struct got_error *
4350 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4351 FILE *patch_file, int n, int nchanges)
4353 const struct got_error *err = NULL;
4354 char *line = NULL;
4355 size_t linesize = 0;
4356 ssize_t linelen;
4357 int resp = ' ';
4358 struct choose_patch_arg *a = arg;
4360 *choice = GOT_PATCH_CHOICE_NONE;
4362 if (a->patch_script_file) {
4363 char *nl;
4364 err = show_change(status, path, patch_file, n, nchanges,
4365 a->action);
4366 if (err)
4367 return err;
4368 linelen = getline(&line, &linesize, a->patch_script_file);
4369 if (linelen == -1) {
4370 if (ferror(a->patch_script_file))
4371 return got_error_from_errno("getline");
4372 return NULL;
4374 nl = strchr(line, '\n');
4375 if (nl)
4376 *nl = '\0';
4377 if (strcmp(line, "y") == 0) {
4378 *choice = GOT_PATCH_CHOICE_YES;
4379 printf("y\n");
4380 } else if (strcmp(line, "n") == 0) {
4381 *choice = GOT_PATCH_CHOICE_NO;
4382 printf("n\n");
4383 } else if (strcmp(line, "q") == 0 &&
4384 status == GOT_STATUS_MODIFY) {
4385 *choice = GOT_PATCH_CHOICE_QUIT;
4386 printf("q\n");
4387 } else
4388 printf("invalid response '%s'\n", line);
4389 free(line);
4390 return NULL;
4393 while (resp != 'y' && resp != 'n' && resp != 'q') {
4394 err = show_change(status, path, patch_file, n, nchanges,
4395 a->action);
4396 if (err)
4397 return err;
4398 resp = getchar();
4399 if (resp == '\n')
4400 resp = getchar();
4401 if (status == GOT_STATUS_MODIFY) {
4402 if (resp != 'y' && resp != 'n' && resp != 'q') {
4403 printf("invalid response '%c'\n", resp);
4404 resp = ' ';
4406 } else if (resp != 'y' && resp != 'n') {
4407 printf("invalid response '%c'\n", resp);
4408 resp = ' ';
4412 if (resp == 'y')
4413 *choice = GOT_PATCH_CHOICE_YES;
4414 else if (resp == 'n')
4415 *choice = GOT_PATCH_CHOICE_NO;
4416 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4417 *choice = GOT_PATCH_CHOICE_QUIT;
4419 return NULL;
4423 static const struct got_error *
4424 cmd_revert(int argc, char *argv[])
4426 const struct got_error *error = NULL;
4427 struct got_worktree *worktree = NULL;
4428 struct got_repository *repo = NULL;
4429 char *cwd = NULL, *path = NULL;
4430 struct got_pathlist_head paths;
4431 struct got_pathlist_entry *pe;
4432 int ch, can_recurse = 0, pflag = 0;
4433 FILE *patch_script_file = NULL;
4434 const char *patch_script_path = NULL;
4435 struct choose_patch_arg cpa;
4437 TAILQ_INIT(&paths);
4439 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4440 switch (ch) {
4441 case 'p':
4442 pflag = 1;
4443 break;
4444 case 'F':
4445 patch_script_path = optarg;
4446 break;
4447 case 'R':
4448 can_recurse = 1;
4449 break;
4450 default:
4451 usage_revert();
4452 /* NOTREACHED */
4456 argc -= optind;
4457 argv += optind;
4459 #ifndef PROFILE
4460 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4461 "unveil", NULL) == -1)
4462 err(1, "pledge");
4463 #endif
4464 if (argc < 1)
4465 usage_revert();
4466 if (patch_script_path && !pflag)
4467 errx(1, "-F option can only be used together with -p option");
4469 cwd = getcwd(NULL, 0);
4470 if (cwd == NULL) {
4471 error = got_error_from_errno("getcwd");
4472 goto done;
4474 error = got_worktree_open(&worktree, cwd);
4475 if (error)
4476 goto done;
4478 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4479 NULL);
4480 if (error != NULL)
4481 goto done;
4483 if (patch_script_path) {
4484 patch_script_file = fopen(patch_script_path, "r");
4485 if (patch_script_file == NULL) {
4486 error = got_error_from_errno2("fopen",
4487 patch_script_path);
4488 goto done;
4491 error = apply_unveil(got_repo_get_path(repo), 1,
4492 got_worktree_get_root_path(worktree));
4493 if (error)
4494 goto done;
4496 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4497 if (error)
4498 goto done;
4500 if (!can_recurse) {
4501 char *ondisk_path;
4502 struct stat sb;
4503 TAILQ_FOREACH(pe, &paths, entry) {
4504 if (asprintf(&ondisk_path, "%s/%s",
4505 got_worktree_get_root_path(worktree),
4506 pe->path) == -1) {
4507 error = got_error_from_errno("asprintf");
4508 goto done;
4510 if (lstat(ondisk_path, &sb) == -1) {
4511 if (errno == ENOENT) {
4512 free(ondisk_path);
4513 continue;
4515 error = got_error_from_errno2("lstat",
4516 ondisk_path);
4517 free(ondisk_path);
4518 goto done;
4520 free(ondisk_path);
4521 if (S_ISDIR(sb.st_mode)) {
4522 error = got_error_msg(GOT_ERR_BAD_PATH,
4523 "reverting directories requires -R option");
4524 goto done;
4529 cpa.patch_script_file = patch_script_file;
4530 cpa.action = "revert";
4531 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4532 pflag ? choose_patch : NULL, &cpa, repo);
4533 if (error)
4534 goto done;
4535 done:
4536 if (patch_script_file && fclose(patch_script_file) == EOF &&
4537 error == NULL)
4538 error = got_error_from_errno2("fclose", patch_script_path);
4539 if (repo)
4540 got_repo_close(repo);
4541 if (worktree)
4542 got_worktree_close(worktree);
4543 free(path);
4544 free(cwd);
4545 return error;
4548 __dead static void
4549 usage_commit(void)
4551 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4552 getprogname());
4553 exit(1);
4556 struct collect_commit_logmsg_arg {
4557 const char *cmdline_log;
4558 const char *editor;
4559 const char *worktree_path;
4560 const char *branch_name;
4561 const char *repo_path;
4562 char *logmsg_path;
4566 static const struct got_error *
4567 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4568 void *arg)
4570 char *initial_content = NULL;
4571 struct got_pathlist_entry *pe;
4572 const struct got_error *err = NULL;
4573 char *template = NULL;
4574 struct collect_commit_logmsg_arg *a = arg;
4575 int fd;
4576 size_t len;
4578 /* if a message was specified on the command line, just use it */
4579 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4580 len = strlen(a->cmdline_log) + 1;
4581 *logmsg = malloc(len + 1);
4582 if (*logmsg == NULL)
4583 return got_error_from_errno("malloc");
4584 strlcpy(*logmsg, a->cmdline_log, len);
4585 return NULL;
4588 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4589 return got_error_from_errno("asprintf");
4591 if (asprintf(&initial_content,
4592 "\n# changes to be committed on branch %s:\n",
4593 a->branch_name) == -1)
4594 return got_error_from_errno("asprintf");
4596 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4597 if (err)
4598 goto done;
4600 dprintf(fd, initial_content);
4602 TAILQ_FOREACH(pe, commitable_paths, entry) {
4603 struct got_commitable *ct = pe->data;
4604 dprintf(fd, "# %c %s\n",
4605 got_commitable_get_status(ct),
4606 got_commitable_get_path(ct));
4608 close(fd);
4610 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4611 done:
4612 free(initial_content);
4613 free(template);
4615 /* Editor is done; we can now apply unveil(2) */
4616 if (err == NULL) {
4617 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4618 if (err) {
4619 free(*logmsg);
4620 *logmsg = NULL;
4623 return err;
4626 static const struct got_error *
4627 cmd_commit(int argc, char *argv[])
4629 const struct got_error *error = NULL;
4630 struct got_worktree *worktree = NULL;
4631 struct got_repository *repo = NULL;
4632 char *cwd = NULL, *id_str = NULL;
4633 struct got_object_id *id = NULL;
4634 const char *logmsg = NULL;
4635 struct collect_commit_logmsg_arg cl_arg;
4636 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4637 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4638 struct got_pathlist_head paths;
4640 TAILQ_INIT(&paths);
4641 cl_arg.logmsg_path = NULL;
4643 while ((ch = getopt(argc, argv, "m:")) != -1) {
4644 switch (ch) {
4645 case 'm':
4646 logmsg = optarg;
4647 break;
4648 default:
4649 usage_commit();
4650 /* NOTREACHED */
4654 argc -= optind;
4655 argv += optind;
4657 #ifndef PROFILE
4658 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4659 "unveil", NULL) == -1)
4660 err(1, "pledge");
4661 #endif
4662 cwd = getcwd(NULL, 0);
4663 if (cwd == NULL) {
4664 error = got_error_from_errno("getcwd");
4665 goto done;
4667 error = got_worktree_open(&worktree, cwd);
4668 if (error)
4669 goto done;
4671 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4672 if (error)
4673 goto done;
4674 if (rebase_in_progress) {
4675 error = got_error(GOT_ERR_REBASING);
4676 goto done;
4679 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4680 worktree);
4681 if (error)
4682 goto done;
4684 error = get_gitconfig_path(&gitconfig_path);
4685 if (error)
4686 goto done;
4687 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4688 gitconfig_path);
4689 if (error != NULL)
4690 goto done;
4692 error = get_author(&author, repo);
4693 if (error)
4694 return error;
4697 * unveil(2) traverses exec(2); if an editor is used we have
4698 * to apply unveil after the log message has been written.
4700 if (logmsg == NULL || strlen(logmsg) == 0)
4701 error = get_editor(&editor);
4702 else
4703 error = apply_unveil(got_repo_get_path(repo), 0,
4704 got_worktree_get_root_path(worktree));
4705 if (error)
4706 goto done;
4708 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4709 if (error)
4710 goto done;
4712 cl_arg.editor = editor;
4713 cl_arg.cmdline_log = logmsg;
4714 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4715 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4716 if (!histedit_in_progress) {
4717 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4718 error = got_error(GOT_ERR_COMMIT_BRANCH);
4719 goto done;
4721 cl_arg.branch_name += 11;
4723 cl_arg.repo_path = got_repo_get_path(repo);
4724 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4725 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4726 if (error) {
4727 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4728 cl_arg.logmsg_path != NULL)
4729 preserve_logmsg = 1;
4730 goto done;
4733 error = got_object_id_str(&id_str, id);
4734 if (error)
4735 goto done;
4736 printf("Created commit %s\n", id_str);
4737 done:
4738 if (preserve_logmsg) {
4739 fprintf(stderr, "%s: log message preserved in %s\n",
4740 getprogname(), cl_arg.logmsg_path);
4741 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4742 error == NULL)
4743 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4744 free(cl_arg.logmsg_path);
4745 if (repo)
4746 got_repo_close(repo);
4747 if (worktree)
4748 got_worktree_close(worktree);
4749 free(cwd);
4750 free(id_str);
4751 free(gitconfig_path);
4752 free(editor);
4753 free(author);
4754 return error;
4757 __dead static void
4758 usage_cherrypick(void)
4760 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4761 exit(1);
4764 static const struct got_error *
4765 cmd_cherrypick(int argc, char *argv[])
4767 const struct got_error *error = NULL;
4768 struct got_worktree *worktree = NULL;
4769 struct got_repository *repo = NULL;
4770 char *cwd = NULL, *commit_id_str = NULL;
4771 struct got_object_id *commit_id = NULL;
4772 struct got_commit_object *commit = NULL;
4773 struct got_object_qid *pid;
4774 struct got_reference *head_ref = NULL;
4775 int ch, did_something = 0;
4777 while ((ch = getopt(argc, argv, "")) != -1) {
4778 switch (ch) {
4779 default:
4780 usage_cherrypick();
4781 /* NOTREACHED */
4785 argc -= optind;
4786 argv += optind;
4788 #ifndef PROFILE
4789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4790 "unveil", NULL) == -1)
4791 err(1, "pledge");
4792 #endif
4793 if (argc != 1)
4794 usage_cherrypick();
4796 cwd = getcwd(NULL, 0);
4797 if (cwd == NULL) {
4798 error = got_error_from_errno("getcwd");
4799 goto done;
4801 error = got_worktree_open(&worktree, cwd);
4802 if (error)
4803 goto done;
4805 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4806 NULL);
4807 if (error != NULL)
4808 goto done;
4810 error = apply_unveil(got_repo_get_path(repo), 0,
4811 got_worktree_get_root_path(worktree));
4812 if (error)
4813 goto done;
4815 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4816 GOT_OBJ_TYPE_COMMIT, repo);
4817 if (error != NULL) {
4818 struct got_reference *ref;
4819 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4820 goto done;
4821 error = got_ref_open(&ref, repo, argv[0], 0);
4822 if (error != NULL)
4823 goto done;
4824 error = got_ref_resolve(&commit_id, repo, ref);
4825 got_ref_close(ref);
4826 if (error != NULL)
4827 goto done;
4829 error = got_object_id_str(&commit_id_str, commit_id);
4830 if (error)
4831 goto done;
4833 error = got_ref_open(&head_ref, repo,
4834 got_worktree_get_head_ref_name(worktree), 0);
4835 if (error != NULL)
4836 goto done;
4838 error = check_same_branch(commit_id, head_ref, NULL, repo);
4839 if (error) {
4840 if (error->code != GOT_ERR_ANCESTRY)
4841 goto done;
4842 error = NULL;
4843 } else {
4844 error = got_error(GOT_ERR_SAME_BRANCH);
4845 goto done;
4848 error = got_object_open_as_commit(&commit, repo, commit_id);
4849 if (error)
4850 goto done;
4851 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4852 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4853 commit_id, repo, update_progress, &did_something, check_cancelled,
4854 NULL);
4855 if (error != NULL)
4856 goto done;
4858 if (did_something)
4859 printf("Merged commit %s\n", commit_id_str);
4860 done:
4861 if (commit)
4862 got_object_commit_close(commit);
4863 free(commit_id_str);
4864 if (head_ref)
4865 got_ref_close(head_ref);
4866 if (worktree)
4867 got_worktree_close(worktree);
4868 if (repo)
4869 got_repo_close(repo);
4870 return error;
4873 __dead static void
4874 usage_backout(void)
4876 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4877 exit(1);
4880 static const struct got_error *
4881 cmd_backout(int argc, char *argv[])
4883 const struct got_error *error = NULL;
4884 struct got_worktree *worktree = NULL;
4885 struct got_repository *repo = NULL;
4886 char *cwd = NULL, *commit_id_str = NULL;
4887 struct got_object_id *commit_id = NULL;
4888 struct got_commit_object *commit = NULL;
4889 struct got_object_qid *pid;
4890 struct got_reference *head_ref = NULL;
4891 int ch, did_something = 0;
4893 while ((ch = getopt(argc, argv, "")) != -1) {
4894 switch (ch) {
4895 default:
4896 usage_backout();
4897 /* NOTREACHED */
4901 argc -= optind;
4902 argv += optind;
4904 #ifndef PROFILE
4905 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4906 "unveil", NULL) == -1)
4907 err(1, "pledge");
4908 #endif
4909 if (argc != 1)
4910 usage_backout();
4912 cwd = getcwd(NULL, 0);
4913 if (cwd == NULL) {
4914 error = got_error_from_errno("getcwd");
4915 goto done;
4917 error = got_worktree_open(&worktree, cwd);
4918 if (error)
4919 goto done;
4921 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4922 NULL);
4923 if (error != NULL)
4924 goto done;
4926 error = apply_unveil(got_repo_get_path(repo), 0,
4927 got_worktree_get_root_path(worktree));
4928 if (error)
4929 goto done;
4931 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4932 GOT_OBJ_TYPE_COMMIT, repo);
4933 if (error != NULL) {
4934 struct got_reference *ref;
4935 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4936 goto done;
4937 error = got_ref_open(&ref, repo, argv[0], 0);
4938 if (error != NULL)
4939 goto done;
4940 error = got_ref_resolve(&commit_id, repo, ref);
4941 got_ref_close(ref);
4942 if (error != NULL)
4943 goto done;
4945 error = got_object_id_str(&commit_id_str, commit_id);
4946 if (error)
4947 goto done;
4949 error = got_ref_open(&head_ref, repo,
4950 got_worktree_get_head_ref_name(worktree), 0);
4951 if (error != NULL)
4952 goto done;
4954 error = check_same_branch(commit_id, head_ref, NULL, repo);
4955 if (error)
4956 goto done;
4958 error = got_object_open_as_commit(&commit, repo, commit_id);
4959 if (error)
4960 goto done;
4961 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4962 if (pid == NULL) {
4963 error = got_error(GOT_ERR_ROOT_COMMIT);
4964 goto done;
4967 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4968 update_progress, &did_something, check_cancelled, NULL);
4969 if (error != NULL)
4970 goto done;
4972 if (did_something)
4973 printf("Backed out commit %s\n", commit_id_str);
4974 done:
4975 if (commit)
4976 got_object_commit_close(commit);
4977 free(commit_id_str);
4978 if (head_ref)
4979 got_ref_close(head_ref);
4980 if (worktree)
4981 got_worktree_close(worktree);
4982 if (repo)
4983 got_repo_close(repo);
4984 return error;
4987 __dead static void
4988 usage_rebase(void)
4990 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4991 getprogname());
4992 exit(1);
4995 void
4996 trim_logmsg(char *logmsg, int limit)
4998 char *nl;
4999 size_t len;
5001 len = strlen(logmsg);
5002 if (len > limit)
5003 len = limit;
5004 logmsg[len] = '\0';
5005 nl = strchr(logmsg, '\n');
5006 if (nl)
5007 *nl = '\0';
5010 static const struct got_error *
5011 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5013 const struct got_error *err;
5014 char *logmsg0 = NULL;
5015 const char *s;
5017 err = got_object_commit_get_logmsg(&logmsg0, commit);
5018 if (err)
5019 return err;
5021 s = logmsg0;
5022 while (isspace((unsigned char)s[0]))
5023 s++;
5025 *logmsg = strdup(s);
5026 if (*logmsg == NULL) {
5027 err = got_error_from_errno("strdup");
5028 goto done;
5031 trim_logmsg(*logmsg, limit);
5032 done:
5033 free(logmsg0);
5034 return err;
5037 static const struct got_error *
5038 show_rebase_progress(struct got_commit_object *commit,
5039 struct got_object_id *old_id, struct got_object_id *new_id)
5041 const struct got_error *err;
5042 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5044 err = got_object_id_str(&old_id_str, old_id);
5045 if (err)
5046 goto done;
5048 if (new_id) {
5049 err = got_object_id_str(&new_id_str, new_id);
5050 if (err)
5051 goto done;
5054 old_id_str[12] = '\0';
5055 if (new_id_str)
5056 new_id_str[12] = '\0';
5058 err = get_short_logmsg(&logmsg, 42, commit);
5059 if (err)
5060 goto done;
5062 printf("%s -> %s: %s\n", old_id_str,
5063 new_id_str ? new_id_str : "no-op change", logmsg);
5064 done:
5065 free(old_id_str);
5066 free(new_id_str);
5067 return err;
5070 static const struct got_error *
5071 rebase_progress(void *arg, unsigned char status, const char *path)
5073 unsigned char *rebase_status = arg;
5075 while (path[0] == '/')
5076 path++;
5077 printf("%c %s\n", status, path);
5079 if (*rebase_status == GOT_STATUS_CONFLICT)
5080 return NULL;
5081 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5082 *rebase_status = status;
5083 return NULL;
5086 static const struct got_error *
5087 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5088 struct got_reference *branch, struct got_reference *new_base_branch,
5089 struct got_reference *tmp_branch, struct got_repository *repo)
5091 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5092 return got_worktree_rebase_complete(worktree, fileindex,
5093 new_base_branch, tmp_branch, branch, repo);
5096 static const struct got_error *
5097 rebase_commit(struct got_pathlist_head *merged_paths,
5098 struct got_worktree *worktree, struct got_fileindex *fileindex,
5099 struct got_reference *tmp_branch,
5100 struct got_object_id *commit_id, struct got_repository *repo)
5102 const struct got_error *error;
5103 struct got_commit_object *commit;
5104 struct got_object_id *new_commit_id;
5106 error = got_object_open_as_commit(&commit, repo, commit_id);
5107 if (error)
5108 return error;
5110 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5111 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5112 if (error) {
5113 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5114 goto done;
5115 error = show_rebase_progress(commit, commit_id, NULL);
5116 } else {
5117 error = show_rebase_progress(commit, commit_id, new_commit_id);
5118 free(new_commit_id);
5120 done:
5121 got_object_commit_close(commit);
5122 return error;
5125 struct check_path_prefix_arg {
5126 const char *path_prefix;
5127 size_t len;
5128 int errcode;
5131 static const struct got_error *
5132 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5133 struct got_blob_object *blob2, struct got_object_id *id1,
5134 struct got_object_id *id2, const char *path1, const char *path2,
5135 mode_t mode1, mode_t mode2, struct got_repository *repo)
5137 struct check_path_prefix_arg *a = arg;
5139 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5140 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5141 return got_error(a->errcode);
5143 return NULL;
5146 static const struct got_error *
5147 check_path_prefix(struct got_object_id *parent_id,
5148 struct got_object_id *commit_id, const char *path_prefix,
5149 int errcode, struct got_repository *repo)
5151 const struct got_error *err;
5152 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5153 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5154 struct check_path_prefix_arg cpp_arg;
5156 if (got_path_is_root_dir(path_prefix))
5157 return NULL;
5159 err = got_object_open_as_commit(&commit, repo, commit_id);
5160 if (err)
5161 goto done;
5163 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5164 if (err)
5165 goto done;
5167 err = got_object_open_as_tree(&tree1, repo,
5168 got_object_commit_get_tree_id(parent_commit));
5169 if (err)
5170 goto done;
5172 err = got_object_open_as_tree(&tree2, repo,
5173 got_object_commit_get_tree_id(commit));
5174 if (err)
5175 goto done;
5177 cpp_arg.path_prefix = path_prefix;
5178 while (cpp_arg.path_prefix[0] == '/')
5179 cpp_arg.path_prefix++;
5180 cpp_arg.len = strlen(cpp_arg.path_prefix);
5181 cpp_arg.errcode = errcode;
5182 err = got_diff_tree(tree1, tree2, "", "", repo,
5183 check_path_prefix_in_diff, &cpp_arg, 0);
5184 done:
5185 if (tree1)
5186 got_object_tree_close(tree1);
5187 if (tree2)
5188 got_object_tree_close(tree2);
5189 if (commit)
5190 got_object_commit_close(commit);
5191 if (parent_commit)
5192 got_object_commit_close(parent_commit);
5193 return err;
5196 static const struct got_error *
5197 collect_commits(struct got_object_id_queue *commits,
5198 struct got_object_id *initial_commit_id,
5199 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5200 const char *path_prefix, int path_prefix_errcode,
5201 struct got_repository *repo)
5203 const struct got_error *err = NULL;
5204 struct got_commit_graph *graph = NULL;
5205 struct got_object_id *parent_id = NULL;
5206 struct got_object_qid *qid;
5207 struct got_object_id *commit_id = initial_commit_id;
5209 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5210 if (err)
5211 return err;
5213 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5214 check_cancelled, NULL);
5215 if (err)
5216 goto done;
5217 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5218 err = got_commit_graph_iter_next(&parent_id, graph);
5219 if (err) {
5220 if (err->code == GOT_ERR_ITER_COMPLETED) {
5221 err = got_error_msg(GOT_ERR_ANCESTRY,
5222 "ran out of commits to rebase before "
5223 "youngest common ancestor commit has "
5224 "been reached?!?");
5225 goto done;
5226 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5227 goto done;
5228 err = got_commit_graph_fetch_commits(graph, 1, repo,
5229 check_cancelled, NULL);
5230 if (err)
5231 goto done;
5232 } else {
5233 err = check_path_prefix(parent_id, commit_id,
5234 path_prefix, path_prefix_errcode, repo);
5235 if (err)
5236 goto done;
5238 err = got_object_qid_alloc(&qid, commit_id);
5239 if (err)
5240 goto done;
5241 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5242 commit_id = parent_id;
5245 done:
5246 got_commit_graph_close(graph);
5247 return err;
5250 static const struct got_error *
5251 cmd_rebase(int argc, char *argv[])
5253 const struct got_error *error = NULL;
5254 struct got_worktree *worktree = NULL;
5255 struct got_repository *repo = NULL;
5256 struct got_fileindex *fileindex = NULL;
5257 char *cwd = NULL;
5258 struct got_reference *branch = NULL;
5259 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5260 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5261 struct got_object_id *resume_commit_id = NULL;
5262 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5263 struct got_commit_object *commit = NULL;
5264 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5265 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5266 struct got_object_id_queue commits;
5267 struct got_pathlist_head merged_paths;
5268 const struct got_object_id_queue *parent_ids;
5269 struct got_object_qid *qid, *pid;
5271 SIMPLEQ_INIT(&commits);
5272 TAILQ_INIT(&merged_paths);
5274 while ((ch = getopt(argc, argv, "ac")) != -1) {
5275 switch (ch) {
5276 case 'a':
5277 abort_rebase = 1;
5278 break;
5279 case 'c':
5280 continue_rebase = 1;
5281 break;
5282 default:
5283 usage_rebase();
5284 /* NOTREACHED */
5288 argc -= optind;
5289 argv += optind;
5291 #ifndef PROFILE
5292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5293 "unveil", NULL) == -1)
5294 err(1, "pledge");
5295 #endif
5296 if (abort_rebase && continue_rebase)
5297 usage_rebase();
5298 else if (abort_rebase || continue_rebase) {
5299 if (argc != 0)
5300 usage_rebase();
5301 } else if (argc != 1)
5302 usage_rebase();
5304 cwd = getcwd(NULL, 0);
5305 if (cwd == NULL) {
5306 error = got_error_from_errno("getcwd");
5307 goto done;
5309 error = got_worktree_open(&worktree, cwd);
5310 if (error)
5311 goto done;
5313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5314 NULL);
5315 if (error != NULL)
5316 goto done;
5318 error = apply_unveil(got_repo_get_path(repo), 0,
5319 got_worktree_get_root_path(worktree));
5320 if (error)
5321 goto done;
5323 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5324 if (error)
5325 goto done;
5327 if (abort_rebase) {
5328 int did_something;
5329 if (!rebase_in_progress) {
5330 error = got_error(GOT_ERR_NOT_REBASING);
5331 goto done;
5333 error = got_worktree_rebase_continue(&resume_commit_id,
5334 &new_base_branch, &tmp_branch, &branch, &fileindex,
5335 worktree, repo);
5336 if (error)
5337 goto done;
5338 printf("Switching work tree to %s\n",
5339 got_ref_get_symref_target(new_base_branch));
5340 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5341 new_base_branch, update_progress, &did_something);
5342 if (error)
5343 goto done;
5344 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5345 goto done; /* nothing else to do */
5348 if (continue_rebase) {
5349 if (!rebase_in_progress) {
5350 error = got_error(GOT_ERR_NOT_REBASING);
5351 goto done;
5353 error = got_worktree_rebase_continue(&resume_commit_id,
5354 &new_base_branch, &tmp_branch, &branch, &fileindex,
5355 worktree, repo);
5356 if (error)
5357 goto done;
5359 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5360 resume_commit_id, repo);
5361 if (error)
5362 goto done;
5364 yca_id = got_object_id_dup(resume_commit_id);
5365 if (yca_id == NULL) {
5366 error = got_error_from_errno("got_object_id_dup");
5367 goto done;
5369 } else {
5370 error = got_ref_open(&branch, repo, argv[0], 0);
5371 if (error != NULL)
5372 goto done;
5375 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5376 if (error)
5377 goto done;
5379 if (!continue_rebase) {
5380 struct got_object_id *base_commit_id;
5382 base_commit_id = got_worktree_get_base_commit_id(worktree);
5383 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5384 base_commit_id, branch_head_commit_id, repo,
5385 check_cancelled, NULL);
5386 if (error)
5387 goto done;
5388 if (yca_id == NULL) {
5389 error = got_error_msg(GOT_ERR_ANCESTRY,
5390 "specified branch shares no common ancestry "
5391 "with work tree's branch");
5392 goto done;
5395 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5396 if (error) {
5397 if (error->code != GOT_ERR_ANCESTRY)
5398 goto done;
5399 error = NULL;
5400 } else {
5401 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5402 "specified branch resolves to a commit which "
5403 "is already contained in work tree's branch");
5404 goto done;
5406 error = got_worktree_rebase_prepare(&new_base_branch,
5407 &tmp_branch, &fileindex, worktree, branch, repo);
5408 if (error)
5409 goto done;
5412 commit_id = branch_head_commit_id;
5413 error = got_object_open_as_commit(&commit, repo, commit_id);
5414 if (error)
5415 goto done;
5417 parent_ids = got_object_commit_get_parent_ids(commit);
5418 pid = SIMPLEQ_FIRST(parent_ids);
5419 if (pid == NULL) {
5420 if (!continue_rebase) {
5421 int did_something;
5422 error = got_worktree_rebase_abort(worktree, fileindex,
5423 repo, new_base_branch, update_progress,
5424 &did_something);
5425 if (error)
5426 goto done;
5427 printf("Rebase of %s aborted\n",
5428 got_ref_get_name(branch));
5430 error = got_error(GOT_ERR_EMPTY_REBASE);
5431 goto done;
5433 error = collect_commits(&commits, commit_id, pid->id,
5434 yca_id, got_worktree_get_path_prefix(worktree),
5435 GOT_ERR_REBASE_PATH, repo);
5436 got_object_commit_close(commit);
5437 commit = NULL;
5438 if (error)
5439 goto done;
5441 if (SIMPLEQ_EMPTY(&commits)) {
5442 if (continue_rebase)
5443 error = rebase_complete(worktree, fileindex,
5444 branch, new_base_branch, tmp_branch, repo);
5445 else
5446 error = got_error(GOT_ERR_EMPTY_REBASE);
5447 goto done;
5450 pid = NULL;
5451 SIMPLEQ_FOREACH(qid, &commits, entry) {
5452 commit_id = qid->id;
5453 parent_id = pid ? pid->id : yca_id;
5454 pid = qid;
5456 error = got_worktree_rebase_merge_files(&merged_paths,
5457 worktree, fileindex, parent_id, commit_id, repo,
5458 rebase_progress, &rebase_status, check_cancelled, NULL);
5459 if (error)
5460 goto done;
5462 if (rebase_status == GOT_STATUS_CONFLICT) {
5463 got_worktree_rebase_pathlist_free(&merged_paths);
5464 break;
5467 error = rebase_commit(&merged_paths, worktree, fileindex,
5468 tmp_branch, commit_id, repo);
5469 got_worktree_rebase_pathlist_free(&merged_paths);
5470 if (error)
5471 goto done;
5474 if (rebase_status == GOT_STATUS_CONFLICT) {
5475 error = got_worktree_rebase_postpone(worktree, fileindex);
5476 if (error)
5477 goto done;
5478 error = got_error_msg(GOT_ERR_CONFLICTS,
5479 "conflicts must be resolved before rebasing can continue");
5480 } else
5481 error = rebase_complete(worktree, fileindex, branch,
5482 new_base_branch, tmp_branch, repo);
5483 done:
5484 got_object_id_queue_free(&commits);
5485 free(branch_head_commit_id);
5486 free(resume_commit_id);
5487 free(yca_id);
5488 if (commit)
5489 got_object_commit_close(commit);
5490 if (branch)
5491 got_ref_close(branch);
5492 if (new_base_branch)
5493 got_ref_close(new_base_branch);
5494 if (tmp_branch)
5495 got_ref_close(tmp_branch);
5496 if (worktree)
5497 got_worktree_close(worktree);
5498 if (repo)
5499 got_repo_close(repo);
5500 return error;
5503 __dead static void
5504 usage_histedit(void)
5506 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5507 getprogname());
5508 exit(1);
5511 #define GOT_HISTEDIT_PICK 'p'
5512 #define GOT_HISTEDIT_EDIT 'e'
5513 #define GOT_HISTEDIT_FOLD 'f'
5514 #define GOT_HISTEDIT_DROP 'd'
5515 #define GOT_HISTEDIT_MESG 'm'
5517 static struct got_histedit_cmd {
5518 unsigned char code;
5519 const char *name;
5520 const char *desc;
5521 } got_histedit_cmds[] = {
5522 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5523 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5524 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5525 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5526 { GOT_HISTEDIT_MESG, "mesg",
5527 "single-line log message for commit above (open editor if empty)" },
5530 struct got_histedit_list_entry {
5531 TAILQ_ENTRY(got_histedit_list_entry) entry;
5532 struct got_object_id *commit_id;
5533 const struct got_histedit_cmd *cmd;
5534 char *logmsg;
5536 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5538 static const struct got_error *
5539 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5540 FILE *f, struct got_repository *repo)
5542 const struct got_error *err = NULL;
5543 char *logmsg = NULL, *id_str = NULL;
5544 struct got_commit_object *commit = NULL;
5545 int n;
5547 err = got_object_open_as_commit(&commit, repo, commit_id);
5548 if (err)
5549 goto done;
5551 err = get_short_logmsg(&logmsg, 34, commit);
5552 if (err)
5553 goto done;
5555 err = got_object_id_str(&id_str, commit_id);
5556 if (err)
5557 goto done;
5559 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5560 if (n < 0)
5561 err = got_ferror(f, GOT_ERR_IO);
5562 done:
5563 if (commit)
5564 got_object_commit_close(commit);
5565 free(id_str);
5566 free(logmsg);
5567 return err;
5570 static const struct got_error *
5571 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5572 struct got_repository *repo)
5574 const struct got_error *err = NULL;
5575 struct got_object_qid *qid;
5577 if (SIMPLEQ_EMPTY(commits))
5578 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5580 SIMPLEQ_FOREACH(qid, commits, entry) {
5581 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5582 f, repo);
5583 if (err)
5584 break;
5587 return err;
5590 static const struct got_error *
5591 write_cmd_list(FILE *f)
5593 const struct got_error *err = NULL;
5594 int n, i;
5596 n = fprintf(f, "# Available histedit commands:\n");
5597 if (n < 0)
5598 return got_ferror(f, GOT_ERR_IO);
5600 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5601 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5602 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5603 cmd->desc);
5604 if (n < 0) {
5605 err = got_ferror(f, GOT_ERR_IO);
5606 break;
5609 n = fprintf(f, "# Commits will be processed in order from top to "
5610 "bottom of this file.\n");
5611 if (n < 0)
5612 return got_ferror(f, GOT_ERR_IO);
5613 return err;
5616 static const struct got_error *
5617 histedit_syntax_error(int lineno)
5619 static char msg[42];
5620 int ret;
5622 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5623 lineno);
5624 if (ret == -1 || ret >= sizeof(msg))
5625 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5627 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5630 static const struct got_error *
5631 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5632 char *logmsg, struct got_repository *repo)
5634 const struct got_error *err;
5635 struct got_commit_object *folded_commit = NULL;
5636 char *id_str, *folded_logmsg = NULL;
5638 err = got_object_id_str(&id_str, hle->commit_id);
5639 if (err)
5640 return err;
5642 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5643 if (err)
5644 goto done;
5646 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5647 if (err)
5648 goto done;
5649 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5650 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5651 folded_logmsg) == -1) {
5652 err = got_error_from_errno("asprintf");
5653 goto done;
5655 done:
5656 if (folded_commit)
5657 got_object_commit_close(folded_commit);
5658 free(id_str);
5659 free(folded_logmsg);
5660 return err;
5663 static struct got_histedit_list_entry *
5664 get_folded_commits(struct got_histedit_list_entry *hle)
5666 struct got_histedit_list_entry *prev, *folded = NULL;
5668 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5669 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5670 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5671 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5672 folded = prev;
5673 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5676 return folded;
5679 static const struct got_error *
5680 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5681 struct got_repository *repo)
5683 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5684 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5685 const struct got_error *err = NULL;
5686 struct got_commit_object *commit = NULL;
5687 int fd;
5688 struct got_histedit_list_entry *folded = NULL;
5690 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5691 if (err)
5692 return err;
5694 folded = get_folded_commits(hle);
5695 if (folded) {
5696 while (folded != hle) {
5697 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5698 folded = TAILQ_NEXT(folded, entry);
5699 continue;
5701 err = append_folded_commit_msg(&new_msg, folded,
5702 logmsg, repo);
5703 if (err)
5704 goto done;
5705 free(logmsg);
5706 logmsg = new_msg;
5707 folded = TAILQ_NEXT(folded, entry);
5711 err = got_object_id_str(&id_str, hle->commit_id);
5712 if (err)
5713 goto done;
5714 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5715 if (err)
5716 goto done;
5717 if (asprintf(&new_msg,
5718 "%s\n# original log message of commit %s: %s",
5719 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5720 err = got_error_from_errno("asprintf");
5721 goto done;
5723 free(logmsg);
5724 logmsg = new_msg;
5726 err = got_object_id_str(&id_str, hle->commit_id);
5727 if (err)
5728 goto done;
5730 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5731 if (err)
5732 goto done;
5734 dprintf(fd, logmsg);
5735 close(fd);
5737 err = get_editor(&editor);
5738 if (err)
5739 goto done;
5741 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5742 if (err) {
5743 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5744 goto done;
5745 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5747 done:
5748 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5749 err = got_error_from_errno2("unlink", logmsg_path);
5750 free(logmsg_path);
5751 free(logmsg);
5752 free(orig_logmsg);
5753 free(editor);
5754 if (commit)
5755 got_object_commit_close(commit);
5756 return err;
5759 static const struct got_error *
5760 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5761 FILE *f, struct got_repository *repo)
5763 const struct got_error *err = NULL;
5764 char *line = NULL, *p, *end;
5765 size_t size;
5766 ssize_t len;
5767 int lineno = 0, i;
5768 const struct got_histedit_cmd *cmd;
5769 struct got_object_id *commit_id = NULL;
5770 struct got_histedit_list_entry *hle = NULL;
5772 for (;;) {
5773 len = getline(&line, &size, f);
5774 if (len == -1) {
5775 const struct got_error *getline_err;
5776 if (feof(f))
5777 break;
5778 getline_err = got_error_from_errno("getline");
5779 err = got_ferror(f, getline_err->code);
5780 break;
5782 lineno++;
5783 p = line;
5784 while (isspace((unsigned char)p[0]))
5785 p++;
5786 if (p[0] == '#' || p[0] == '\0') {
5787 free(line);
5788 line = NULL;
5789 continue;
5791 cmd = NULL;
5792 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5793 cmd = &got_histedit_cmds[i];
5794 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5795 isspace((unsigned char)p[strlen(cmd->name)])) {
5796 p += strlen(cmd->name);
5797 break;
5799 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5800 p++;
5801 break;
5804 if (i == nitems(got_histedit_cmds)) {
5805 err = histedit_syntax_error(lineno);
5806 break;
5808 while (isspace((unsigned char)p[0]))
5809 p++;
5810 if (cmd->code == GOT_HISTEDIT_MESG) {
5811 if (hle == NULL || hle->logmsg != NULL) {
5812 err = got_error(GOT_ERR_HISTEDIT_CMD);
5813 break;
5815 if (p[0] == '\0') {
5816 err = histedit_edit_logmsg(hle, repo);
5817 if (err)
5818 break;
5819 } else {
5820 hle->logmsg = strdup(p);
5821 if (hle->logmsg == NULL) {
5822 err = got_error_from_errno("strdup");
5823 break;
5826 free(line);
5827 line = NULL;
5828 continue;
5829 } else {
5830 end = p;
5831 while (end[0] && !isspace((unsigned char)end[0]))
5832 end++;
5833 *end = '\0';
5835 err = got_object_resolve_id_str(&commit_id, repo, p);
5836 if (err) {
5837 /* override error code */
5838 err = histedit_syntax_error(lineno);
5839 break;
5842 hle = malloc(sizeof(*hle));
5843 if (hle == NULL) {
5844 err = got_error_from_errno("malloc");
5845 break;
5847 hle->cmd = cmd;
5848 hle->commit_id = commit_id;
5849 hle->logmsg = NULL;
5850 commit_id = NULL;
5851 free(line);
5852 line = NULL;
5853 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5856 free(line);
5857 free(commit_id);
5858 return err;
5861 static const struct got_error *
5862 histedit_check_script(struct got_histedit_list *histedit_cmds,
5863 struct got_object_id_queue *commits, struct got_repository *repo)
5865 const struct got_error *err = NULL;
5866 struct got_object_qid *qid;
5867 struct got_histedit_list_entry *hle;
5868 static char msg[80];
5869 char *id_str;
5871 if (TAILQ_EMPTY(histedit_cmds))
5872 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5873 "histedit script contains no commands");
5874 if (SIMPLEQ_EMPTY(commits))
5875 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5877 SIMPLEQ_FOREACH(qid, commits, entry) {
5878 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5879 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5880 break;
5882 if (hle == NULL) {
5883 err = got_object_id_str(&id_str, qid->id);
5884 if (err)
5885 return err;
5886 snprintf(msg, sizeof(msg),
5887 "commit %s missing from histedit script", id_str);
5888 free(id_str);
5889 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5893 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5894 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5895 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5896 "last commit in histedit script cannot be folded");
5898 return NULL;
5901 static const struct got_error *
5902 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5903 const char *path, struct got_object_id_queue *commits,
5904 struct got_repository *repo)
5906 const struct got_error *err = NULL;
5907 char *editor;
5908 FILE *f = NULL;
5910 err = get_editor(&editor);
5911 if (err)
5912 return err;
5914 if (spawn_editor(editor, path) == -1) {
5915 err = got_error_from_errno("failed spawning editor");
5916 goto done;
5919 f = fopen(path, "r");
5920 if (f == NULL) {
5921 err = got_error_from_errno("fopen");
5922 goto done;
5924 err = histedit_parse_list(histedit_cmds, f, repo);
5925 if (err)
5926 goto done;
5928 err = histedit_check_script(histedit_cmds, commits, repo);
5929 done:
5930 if (f && fclose(f) != 0 && err == NULL)
5931 err = got_error_from_errno("fclose");
5932 free(editor);
5933 return err;
5936 static const struct got_error *
5937 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5938 struct got_object_id_queue *, const char *, struct got_repository *);
5940 static const struct got_error *
5941 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5942 struct got_object_id_queue *commits, struct got_repository *repo)
5944 const struct got_error *err;
5945 FILE *f = NULL;
5946 char *path = NULL;
5948 err = got_opentemp_named(&path, &f, "got-histedit");
5949 if (err)
5950 return err;
5952 err = write_cmd_list(f);
5953 if (err)
5954 goto done;
5956 err = histedit_write_commit_list(commits, f, repo);
5957 if (err)
5958 goto done;
5960 if (fclose(f) != 0) {
5961 err = got_error_from_errno("fclose");
5962 goto done;
5964 f = NULL;
5966 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5967 if (err) {
5968 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5969 err->code != GOT_ERR_HISTEDIT_CMD)
5970 goto done;
5971 err = histedit_edit_list_retry(histedit_cmds, err,
5972 commits, path, repo);
5974 done:
5975 if (f && fclose(f) != 0 && err == NULL)
5976 err = got_error_from_errno("fclose");
5977 if (path && unlink(path) != 0 && err == NULL)
5978 err = got_error_from_errno2("unlink", path);
5979 free(path);
5980 return err;
5983 static const struct got_error *
5984 histedit_save_list(struct got_histedit_list *histedit_cmds,
5985 struct got_worktree *worktree, struct got_repository *repo)
5987 const struct got_error *err = NULL;
5988 char *path = NULL;
5989 FILE *f = NULL;
5990 struct got_histedit_list_entry *hle;
5991 struct got_commit_object *commit = NULL;
5993 err = got_worktree_get_histedit_script_path(&path, worktree);
5994 if (err)
5995 return err;
5997 f = fopen(path, "w");
5998 if (f == NULL) {
5999 err = got_error_from_errno2("fopen", path);
6000 goto done;
6002 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6003 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6004 repo);
6005 if (err)
6006 break;
6008 if (hle->logmsg) {
6009 int n = fprintf(f, "%c %s\n",
6010 GOT_HISTEDIT_MESG, hle->logmsg);
6011 if (n < 0) {
6012 err = got_ferror(f, GOT_ERR_IO);
6013 break;
6017 done:
6018 if (f && fclose(f) != 0 && err == NULL)
6019 err = got_error_from_errno("fclose");
6020 free(path);
6021 if (commit)
6022 got_object_commit_close(commit);
6023 return err;
6026 void
6027 histedit_free_list(struct got_histedit_list *histedit_cmds)
6029 struct got_histedit_list_entry *hle;
6031 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6032 TAILQ_REMOVE(histedit_cmds, hle, entry);
6033 free(hle);
6037 static const struct got_error *
6038 histedit_load_list(struct got_histedit_list *histedit_cmds,
6039 const char *path, struct got_repository *repo)
6041 const struct got_error *err = NULL;
6042 FILE *f = NULL;
6044 f = fopen(path, "r");
6045 if (f == NULL) {
6046 err = got_error_from_errno2("fopen", path);
6047 goto done;
6050 err = histedit_parse_list(histedit_cmds, f, repo);
6051 done:
6052 if (f && fclose(f) != 0 && err == NULL)
6053 err = got_error_from_errno("fclose");
6054 return err;
6057 static const struct got_error *
6058 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6059 const struct got_error *edit_err, struct got_object_id_queue *commits,
6060 const char *path, struct got_repository *repo)
6062 const struct got_error *err = NULL, *prev_err = edit_err;
6063 int resp = ' ';
6065 while (resp != 'c' && resp != 'r' && resp != 'a') {
6066 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6067 "or (a)bort: ", getprogname(), prev_err->msg);
6068 resp = getchar();
6069 if (resp == '\n')
6070 resp = getchar();
6071 if (resp == 'c') {
6072 histedit_free_list(histedit_cmds);
6073 err = histedit_run_editor(histedit_cmds, path, commits,
6074 repo);
6075 if (err) {
6076 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6077 err->code != GOT_ERR_HISTEDIT_CMD)
6078 break;
6079 prev_err = err;
6080 resp = ' ';
6081 continue;
6083 break;
6084 } else if (resp == 'r') {
6085 histedit_free_list(histedit_cmds);
6086 err = histedit_edit_script(histedit_cmds,
6087 commits, repo);
6088 if (err) {
6089 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6090 err->code != GOT_ERR_HISTEDIT_CMD)
6091 break;
6092 prev_err = err;
6093 resp = ' ';
6094 continue;
6096 break;
6097 } else if (resp == 'a') {
6098 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6099 break;
6100 } else
6101 printf("invalid response '%c'\n", resp);
6104 return err;
6107 static const struct got_error *
6108 histedit_complete(struct got_worktree *worktree,
6109 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6110 struct got_reference *branch, struct got_repository *repo)
6112 printf("Switching work tree to %s\n",
6113 got_ref_get_symref_target(branch));
6114 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6115 branch, repo);
6118 static const struct got_error *
6119 show_histedit_progress(struct got_commit_object *commit,
6120 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6122 const struct got_error *err;
6123 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6125 err = got_object_id_str(&old_id_str, hle->commit_id);
6126 if (err)
6127 goto done;
6129 if (new_id) {
6130 err = got_object_id_str(&new_id_str, new_id);
6131 if (err)
6132 goto done;
6135 old_id_str[12] = '\0';
6136 if (new_id_str)
6137 new_id_str[12] = '\0';
6139 if (hle->logmsg) {
6140 logmsg = strdup(hle->logmsg);
6141 if (logmsg == NULL) {
6142 err = got_error_from_errno("strdup");
6143 goto done;
6145 trim_logmsg(logmsg, 42);
6146 } else {
6147 err = get_short_logmsg(&logmsg, 42, commit);
6148 if (err)
6149 goto done;
6152 switch (hle->cmd->code) {
6153 case GOT_HISTEDIT_PICK:
6154 case GOT_HISTEDIT_EDIT:
6155 printf("%s -> %s: %s\n", old_id_str,
6156 new_id_str ? new_id_str : "no-op change", logmsg);
6157 break;
6158 case GOT_HISTEDIT_DROP:
6159 case GOT_HISTEDIT_FOLD:
6160 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6161 logmsg);
6162 break;
6163 default:
6164 break;
6167 done:
6168 free(old_id_str);
6169 free(new_id_str);
6170 return err;
6173 static const struct got_error *
6174 histedit_commit(struct got_pathlist_head *merged_paths,
6175 struct got_worktree *worktree, struct got_fileindex *fileindex,
6176 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6177 struct got_repository *repo)
6179 const struct got_error *err;
6180 struct got_commit_object *commit;
6181 struct got_object_id *new_commit_id;
6183 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6184 && hle->logmsg == NULL) {
6185 err = histedit_edit_logmsg(hle, repo);
6186 if (err)
6187 return err;
6190 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6191 if (err)
6192 return err;
6194 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6195 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6196 hle->logmsg, repo);
6197 if (err) {
6198 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6199 goto done;
6200 err = show_histedit_progress(commit, hle, NULL);
6201 } else {
6202 err = show_histedit_progress(commit, hle, new_commit_id);
6203 free(new_commit_id);
6205 done:
6206 got_object_commit_close(commit);
6207 return err;
6210 static const struct got_error *
6211 histedit_skip_commit(struct got_histedit_list_entry *hle,
6212 struct got_worktree *worktree, struct got_repository *repo)
6214 const struct got_error *error;
6215 struct got_commit_object *commit;
6217 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6218 repo);
6219 if (error)
6220 return error;
6222 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6223 if (error)
6224 return error;
6226 error = show_histedit_progress(commit, hle, NULL);
6227 got_object_commit_close(commit);
6228 return error;
6231 static const struct got_error *
6232 cmd_histedit(int argc, char *argv[])
6234 const struct got_error *error = NULL;
6235 struct got_worktree *worktree = NULL;
6236 struct got_fileindex *fileindex = NULL;
6237 struct got_repository *repo = NULL;
6238 char *cwd = NULL;
6239 struct got_reference *branch = NULL;
6240 struct got_reference *tmp_branch = NULL;
6241 struct got_object_id *resume_commit_id = NULL;
6242 struct got_object_id *base_commit_id = NULL;
6243 struct got_object_id *head_commit_id = NULL;
6244 struct got_commit_object *commit = NULL;
6245 int ch, rebase_in_progress = 0, did_something;
6246 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6247 const char *edit_script_path = NULL;
6248 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6249 struct got_object_id_queue commits;
6250 struct got_pathlist_head merged_paths;
6251 const struct got_object_id_queue *parent_ids;
6252 struct got_object_qid *pid;
6253 struct got_histedit_list histedit_cmds;
6254 struct got_histedit_list_entry *hle;
6256 SIMPLEQ_INIT(&commits);
6257 TAILQ_INIT(&histedit_cmds);
6258 TAILQ_INIT(&merged_paths);
6260 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6261 switch (ch) {
6262 case 'a':
6263 abort_edit = 1;
6264 break;
6265 case 'c':
6266 continue_edit = 1;
6267 break;
6268 case 'F':
6269 edit_script_path = optarg;
6270 break;
6271 default:
6272 usage_histedit();
6273 /* NOTREACHED */
6277 argc -= optind;
6278 argv += optind;
6280 #ifndef PROFILE
6281 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6282 "unveil", NULL) == -1)
6283 err(1, "pledge");
6284 #endif
6285 if (abort_edit && continue_edit)
6286 usage_histedit();
6287 if (argc != 0)
6288 usage_histedit();
6291 * This command cannot apply unveil(2) in all cases because the
6292 * user may choose to run an editor to edit the histedit script
6293 * and to edit individual commit log messages.
6294 * unveil(2) traverses exec(2); if an editor is used we have to
6295 * apply unveil after edit script and log messages have been written.
6296 * XXX TODO: Make use of unveil(2) where possible.
6299 cwd = getcwd(NULL, 0);
6300 if (cwd == NULL) {
6301 error = got_error_from_errno("getcwd");
6302 goto done;
6304 error = got_worktree_open(&worktree, cwd);
6305 if (error)
6306 goto done;
6308 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6309 NULL);
6310 if (error != NULL)
6311 goto done;
6313 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6314 if (error)
6315 goto done;
6316 if (rebase_in_progress) {
6317 error = got_error(GOT_ERR_REBASING);
6318 goto done;
6321 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6322 if (error)
6323 goto done;
6325 if (edit_in_progress && abort_edit) {
6326 error = got_worktree_histedit_continue(&resume_commit_id,
6327 &tmp_branch, &branch, &base_commit_id, &fileindex,
6328 worktree, repo);
6329 if (error)
6330 goto done;
6331 printf("Switching work tree to %s\n",
6332 got_ref_get_symref_target(branch));
6333 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6334 branch, base_commit_id, update_progress, &did_something);
6335 if (error)
6336 goto done;
6337 printf("Histedit of %s aborted\n",
6338 got_ref_get_symref_target(branch));
6339 goto done; /* nothing else to do */
6340 } else if (abort_edit) {
6341 error = got_error(GOT_ERR_NOT_HISTEDIT);
6342 goto done;
6345 if (continue_edit) {
6346 char *path;
6348 if (!edit_in_progress) {
6349 error = got_error(GOT_ERR_NOT_HISTEDIT);
6350 goto done;
6353 error = got_worktree_get_histedit_script_path(&path, worktree);
6354 if (error)
6355 goto done;
6357 error = histedit_load_list(&histedit_cmds, path, repo);
6358 free(path);
6359 if (error)
6360 goto done;
6362 error = got_worktree_histedit_continue(&resume_commit_id,
6363 &tmp_branch, &branch, &base_commit_id, &fileindex,
6364 worktree, repo);
6365 if (error)
6366 goto done;
6368 error = got_ref_resolve(&head_commit_id, repo, branch);
6369 if (error)
6370 goto done;
6372 error = got_object_open_as_commit(&commit, repo,
6373 head_commit_id);
6374 if (error)
6375 goto done;
6376 parent_ids = got_object_commit_get_parent_ids(commit);
6377 pid = SIMPLEQ_FIRST(parent_ids);
6378 if (pid == NULL) {
6379 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6380 goto done;
6382 error = collect_commits(&commits, head_commit_id, pid->id,
6383 base_commit_id, got_worktree_get_path_prefix(worktree),
6384 GOT_ERR_HISTEDIT_PATH, repo);
6385 got_object_commit_close(commit);
6386 commit = NULL;
6387 if (error)
6388 goto done;
6389 } else {
6390 if (edit_in_progress) {
6391 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6392 goto done;
6395 error = got_ref_open(&branch, repo,
6396 got_worktree_get_head_ref_name(worktree), 0);
6397 if (error != NULL)
6398 goto done;
6400 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6401 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6402 "will not edit commit history of a branch outside "
6403 "the \"refs/heads/\" reference namespace");
6404 goto done;
6407 error = got_ref_resolve(&head_commit_id, repo, branch);
6408 got_ref_close(branch);
6409 branch = NULL;
6410 if (error)
6411 goto done;
6413 error = got_object_open_as_commit(&commit, repo,
6414 head_commit_id);
6415 if (error)
6416 goto done;
6417 parent_ids = got_object_commit_get_parent_ids(commit);
6418 pid = SIMPLEQ_FIRST(parent_ids);
6419 if (pid == NULL) {
6420 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6421 goto done;
6423 error = collect_commits(&commits, head_commit_id, pid->id,
6424 got_worktree_get_base_commit_id(worktree),
6425 got_worktree_get_path_prefix(worktree),
6426 GOT_ERR_HISTEDIT_PATH, repo);
6427 got_object_commit_close(commit);
6428 commit = NULL;
6429 if (error)
6430 goto done;
6432 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6433 &base_commit_id, &fileindex, worktree, repo);
6434 if (error)
6435 goto done;
6437 if (edit_script_path) {
6438 error = histedit_load_list(&histedit_cmds,
6439 edit_script_path, repo);
6440 if (error) {
6441 got_worktree_histedit_abort(worktree, fileindex,
6442 repo, branch, base_commit_id,
6443 update_progress, &did_something);
6444 goto done;
6446 } else {
6447 error = histedit_edit_script(&histedit_cmds, &commits,
6448 repo);
6449 if (error) {
6450 got_worktree_histedit_abort(worktree, fileindex,
6451 repo, branch, base_commit_id,
6452 update_progress, &did_something);
6453 goto done;
6458 error = histedit_save_list(&histedit_cmds, worktree,
6459 repo);
6460 if (error) {
6461 got_worktree_histedit_abort(worktree, fileindex,
6462 repo, branch, base_commit_id,
6463 update_progress, &did_something);
6464 goto done;
6469 error = histedit_check_script(&histedit_cmds, &commits, repo);
6470 if (error)
6471 goto done;
6473 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6474 if (resume_commit_id) {
6475 if (got_object_id_cmp(hle->commit_id,
6476 resume_commit_id) != 0)
6477 continue;
6479 resume_commit_id = NULL;
6480 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6481 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6482 error = histedit_skip_commit(hle, worktree,
6483 repo);
6484 } else {
6485 error = histedit_commit(NULL, worktree,
6486 fileindex, tmp_branch, hle, repo);
6488 if (error)
6489 goto done;
6490 continue;
6493 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6494 error = histedit_skip_commit(hle, worktree, repo);
6495 if (error)
6496 goto done;
6497 continue;
6500 error = got_object_open_as_commit(&commit, repo,
6501 hle->commit_id);
6502 if (error)
6503 goto done;
6504 parent_ids = got_object_commit_get_parent_ids(commit);
6505 pid = SIMPLEQ_FIRST(parent_ids);
6507 error = got_worktree_histedit_merge_files(&merged_paths,
6508 worktree, fileindex, pid->id, hle->commit_id, repo,
6509 rebase_progress, &rebase_status, check_cancelled, NULL);
6510 if (error)
6511 goto done;
6512 got_object_commit_close(commit);
6513 commit = NULL;
6515 if (rebase_status == GOT_STATUS_CONFLICT) {
6516 got_worktree_rebase_pathlist_free(&merged_paths);
6517 break;
6520 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6521 char *id_str;
6522 error = got_object_id_str(&id_str, hle->commit_id);
6523 if (error)
6524 goto done;
6525 printf("Stopping histedit for amending commit %s\n",
6526 id_str);
6527 free(id_str);
6528 got_worktree_rebase_pathlist_free(&merged_paths);
6529 error = got_worktree_histedit_postpone(worktree,
6530 fileindex);
6531 goto done;
6534 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6535 error = histedit_skip_commit(hle, worktree, repo);
6536 if (error)
6537 goto done;
6538 continue;
6541 error = histedit_commit(&merged_paths, worktree, fileindex,
6542 tmp_branch, hle, repo);
6543 got_worktree_rebase_pathlist_free(&merged_paths);
6544 if (error)
6545 goto done;
6548 if (rebase_status == GOT_STATUS_CONFLICT) {
6549 error = got_worktree_histedit_postpone(worktree, fileindex);
6550 if (error)
6551 goto done;
6552 error = got_error_msg(GOT_ERR_CONFLICTS,
6553 "conflicts must be resolved before rebasing can continue");
6554 } else
6555 error = histedit_complete(worktree, fileindex, tmp_branch,
6556 branch, repo);
6557 done:
6558 got_object_id_queue_free(&commits);
6559 histedit_free_list(&histedit_cmds);
6560 free(head_commit_id);
6561 free(base_commit_id);
6562 free(resume_commit_id);
6563 if (commit)
6564 got_object_commit_close(commit);
6565 if (branch)
6566 got_ref_close(branch);
6567 if (tmp_branch)
6568 got_ref_close(tmp_branch);
6569 if (worktree)
6570 got_worktree_close(worktree);
6571 if (repo)
6572 got_repo_close(repo);
6573 return error;
6576 __dead static void
6577 usage_integrate(void)
6579 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6580 exit(1);
6583 static const struct got_error *
6584 cmd_integrate(int argc, char *argv[])
6586 const struct got_error *error = NULL;
6587 struct got_repository *repo = NULL;
6588 struct got_worktree *worktree = NULL;
6589 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6590 const char *branch_arg = NULL;
6591 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6592 struct got_fileindex *fileindex = NULL;
6593 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6594 int ch, did_something = 0;
6596 while ((ch = getopt(argc, argv, "")) != -1) {
6597 switch (ch) {
6598 default:
6599 usage_integrate();
6600 /* NOTREACHED */
6604 argc -= optind;
6605 argv += optind;
6607 if (argc != 1)
6608 usage_integrate();
6609 branch_arg = argv[0];
6611 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6612 "unveil", NULL) == -1)
6613 err(1, "pledge");
6615 cwd = getcwd(NULL, 0);
6616 if (cwd == NULL) {
6617 error = got_error_from_errno("getcwd");
6618 goto done;
6621 error = got_worktree_open(&worktree, cwd);
6622 if (error)
6623 goto done;
6625 error = check_rebase_or_histedit_in_progress(worktree);
6626 if (error)
6627 goto done;
6629 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6630 NULL);
6631 if (error != NULL)
6632 goto done;
6634 error = apply_unveil(got_repo_get_path(repo), 0,
6635 got_worktree_get_root_path(worktree));
6636 if (error)
6637 goto done;
6639 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6640 error = got_error_from_errno("asprintf");
6641 goto done;
6644 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6645 &base_branch_ref, worktree, refname, repo);
6646 if (error)
6647 goto done;
6649 refname = strdup(got_ref_get_name(branch_ref));
6650 if (refname == NULL) {
6651 error = got_error_from_errno("strdup");
6652 got_worktree_integrate_abort(worktree, fileindex, repo,
6653 branch_ref, base_branch_ref);
6654 goto done;
6656 base_refname = strdup(got_ref_get_name(base_branch_ref));
6657 if (base_refname == NULL) {
6658 error = got_error_from_errno("strdup");
6659 got_worktree_integrate_abort(worktree, fileindex, repo,
6660 branch_ref, base_branch_ref);
6661 goto done;
6664 error = got_ref_resolve(&commit_id, repo, branch_ref);
6665 if (error)
6666 goto done;
6668 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6669 if (error)
6670 goto done;
6672 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6673 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6674 "specified branch has already been integrated");
6675 got_worktree_integrate_abort(worktree, fileindex, repo,
6676 branch_ref, base_branch_ref);
6677 goto done;
6680 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6681 if (error) {
6682 if (error->code == GOT_ERR_ANCESTRY)
6683 error = got_error(GOT_ERR_REBASE_REQUIRED);
6684 got_worktree_integrate_abort(worktree, fileindex, repo,
6685 branch_ref, base_branch_ref);
6686 goto done;
6689 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6690 branch_ref, base_branch_ref, update_progress, &did_something,
6691 check_cancelled, NULL);
6692 if (error)
6693 goto done;
6695 printf("Integrated %s into %s\n", refname, base_refname);
6696 done:
6697 if (repo)
6698 got_repo_close(repo);
6699 if (worktree)
6700 got_worktree_close(worktree);
6701 free(cwd);
6702 free(base_commit_id);
6703 free(commit_id);
6704 free(refname);
6705 free(base_refname);
6706 return error;
6709 __dead static void
6710 usage_stage(void)
6712 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6713 "[file-path ...]\n",
6714 getprogname());
6715 exit(1);
6718 static const struct got_error *
6719 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6720 const char *path, struct got_object_id *blob_id,
6721 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6723 const struct got_error *err = NULL;
6724 char *id_str = NULL;
6726 if (staged_status != GOT_STATUS_ADD &&
6727 staged_status != GOT_STATUS_MODIFY &&
6728 staged_status != GOT_STATUS_DELETE)
6729 return NULL;
6731 if (staged_status == GOT_STATUS_ADD ||
6732 staged_status == GOT_STATUS_MODIFY)
6733 err = got_object_id_str(&id_str, staged_blob_id);
6734 else
6735 err = got_object_id_str(&id_str, blob_id);
6736 if (err)
6737 return err;
6739 printf("%s %c %s\n", id_str, staged_status, path);
6740 free(id_str);
6741 return NULL;
6744 static const struct got_error *
6745 cmd_stage(int argc, char *argv[])
6747 const struct got_error *error = NULL;
6748 struct got_repository *repo = NULL;
6749 struct got_worktree *worktree = NULL;
6750 char *cwd = NULL;
6751 struct got_pathlist_head paths;
6752 struct got_pathlist_entry *pe;
6753 int ch, list_stage = 0, pflag = 0;
6754 FILE *patch_script_file = NULL;
6755 const char *patch_script_path = NULL;
6756 struct choose_patch_arg cpa;
6758 TAILQ_INIT(&paths);
6760 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6761 switch (ch) {
6762 case 'l':
6763 list_stage = 1;
6764 break;
6765 case 'p':
6766 pflag = 1;
6767 break;
6768 case 'F':
6769 patch_script_path = optarg;
6770 break;
6771 default:
6772 usage_stage();
6773 /* NOTREACHED */
6777 argc -= optind;
6778 argv += optind;
6780 #ifndef PROFILE
6781 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6782 "unveil", NULL) == -1)
6783 err(1, "pledge");
6784 #endif
6785 if (list_stage && (pflag || patch_script_path))
6786 errx(1, "-l option cannot be used with other options");
6787 if (patch_script_path && !pflag)
6788 errx(1, "-F option can only be used together with -p option");
6790 cwd = getcwd(NULL, 0);
6791 if (cwd == NULL) {
6792 error = got_error_from_errno("getcwd");
6793 goto done;
6796 error = got_worktree_open(&worktree, cwd);
6797 if (error)
6798 goto done;
6800 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6801 NULL);
6802 if (error != NULL)
6803 goto done;
6805 if (patch_script_path) {
6806 patch_script_file = fopen(patch_script_path, "r");
6807 if (patch_script_file == NULL) {
6808 error = got_error_from_errno2("fopen",
6809 patch_script_path);
6810 goto done;
6813 error = apply_unveil(got_repo_get_path(repo), 0,
6814 got_worktree_get_root_path(worktree));
6815 if (error)
6816 goto done;
6818 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6819 if (error)
6820 goto done;
6822 if (list_stage)
6823 error = got_worktree_status(worktree, &paths, repo,
6824 print_stage, NULL, check_cancelled, NULL);
6825 else {
6826 cpa.patch_script_file = patch_script_file;
6827 cpa.action = "stage";
6828 error = got_worktree_stage(worktree, &paths,
6829 pflag ? NULL : print_status, NULL,
6830 pflag ? choose_patch : NULL, &cpa, repo);
6832 done:
6833 if (patch_script_file && fclose(patch_script_file) == EOF &&
6834 error == NULL)
6835 error = got_error_from_errno2("fclose", patch_script_path);
6836 if (repo)
6837 got_repo_close(repo);
6838 if (worktree)
6839 got_worktree_close(worktree);
6840 TAILQ_FOREACH(pe, &paths, entry)
6841 free((char *)pe->path);
6842 got_pathlist_free(&paths);
6843 free(cwd);
6844 return error;
6847 __dead static void
6848 usage_unstage(void)
6850 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6851 "[file-path ...]\n",
6852 getprogname());
6853 exit(1);
6857 static const struct got_error *
6858 cmd_unstage(int argc, char *argv[])
6860 const struct got_error *error = NULL;
6861 struct got_repository *repo = NULL;
6862 struct got_worktree *worktree = NULL;
6863 char *cwd = NULL;
6864 struct got_pathlist_head paths;
6865 struct got_pathlist_entry *pe;
6866 int ch, did_something = 0, pflag = 0;
6867 FILE *patch_script_file = NULL;
6868 const char *patch_script_path = NULL;
6869 struct choose_patch_arg cpa;
6871 TAILQ_INIT(&paths);
6873 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6874 switch (ch) {
6875 case 'p':
6876 pflag = 1;
6877 break;
6878 case 'F':
6879 patch_script_path = optarg;
6880 break;
6881 default:
6882 usage_unstage();
6883 /* NOTREACHED */
6887 argc -= optind;
6888 argv += optind;
6890 #ifndef PROFILE
6891 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6892 "unveil", NULL) == -1)
6893 err(1, "pledge");
6894 #endif
6895 if (patch_script_path && !pflag)
6896 errx(1, "-F option can only be used together with -p option");
6898 cwd = getcwd(NULL, 0);
6899 if (cwd == NULL) {
6900 error = got_error_from_errno("getcwd");
6901 goto done;
6904 error = got_worktree_open(&worktree, cwd);
6905 if (error)
6906 goto done;
6908 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6909 NULL);
6910 if (error != NULL)
6911 goto done;
6913 if (patch_script_path) {
6914 patch_script_file = fopen(patch_script_path, "r");
6915 if (patch_script_file == NULL) {
6916 error = got_error_from_errno2("fopen",
6917 patch_script_path);
6918 goto done;
6922 error = apply_unveil(got_repo_get_path(repo), 0,
6923 got_worktree_get_root_path(worktree));
6924 if (error)
6925 goto done;
6927 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6928 if (error)
6929 goto done;
6931 cpa.patch_script_file = patch_script_file;
6932 cpa.action = "unstage";
6933 error = got_worktree_unstage(worktree, &paths, update_progress,
6934 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6935 done:
6936 if (patch_script_file && fclose(patch_script_file) == EOF &&
6937 error == NULL)
6938 error = got_error_from_errno2("fclose", patch_script_path);
6939 if (repo)
6940 got_repo_close(repo);
6941 if (worktree)
6942 got_worktree_close(worktree);
6943 TAILQ_FOREACH(pe, &paths, entry)
6944 free((char *)pe->path);
6945 got_pathlist_free(&paths);
6946 free(cwd);
6947 return error;
6950 __dead static void
6951 usage_cat(void)
6953 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6954 "arg1 [arg2 ...]\n", getprogname());
6955 exit(1);
6958 static const struct got_error *
6959 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6961 const struct got_error *err;
6962 struct got_blob_object *blob;
6964 err = got_object_open_as_blob(&blob, repo, id, 8192);
6965 if (err)
6966 return err;
6968 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6969 got_object_blob_close(blob);
6970 return err;
6973 static const struct got_error *
6974 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6976 const struct got_error *err;
6977 struct got_tree_object *tree;
6978 const struct got_tree_entries *entries;
6979 struct got_tree_entry *te;
6981 err = got_object_open_as_tree(&tree, repo, id);
6982 if (err)
6983 return err;
6985 entries = got_object_tree_get_entries(tree);
6986 te = SIMPLEQ_FIRST(&entries->head);
6987 while (te) {
6988 char *id_str;
6989 if (sigint_received || sigpipe_received)
6990 break;
6991 err = got_object_id_str(&id_str, te->id);
6992 if (err)
6993 break;
6994 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6995 free(id_str);
6996 te = SIMPLEQ_NEXT(te, entry);
6999 got_object_tree_close(tree);
7000 return err;
7003 static const struct got_error *
7004 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7006 const struct got_error *err;
7007 struct got_commit_object *commit;
7008 const struct got_object_id_queue *parent_ids;
7009 struct got_object_qid *pid;
7010 char *id_str = NULL;
7011 const char *logmsg = NULL;
7013 err = got_object_open_as_commit(&commit, repo, id);
7014 if (err)
7015 return err;
7017 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7018 if (err)
7019 goto done;
7021 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7022 parent_ids = got_object_commit_get_parent_ids(commit);
7023 fprintf(outfile, "numparents %d\n",
7024 got_object_commit_get_nparents(commit));
7025 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7026 char *pid_str;
7027 err = got_object_id_str(&pid_str, pid->id);
7028 if (err)
7029 goto done;
7030 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7031 free(pid_str);
7033 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7034 got_object_commit_get_author(commit),
7035 got_object_commit_get_author_time(commit));
7037 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7038 got_object_commit_get_author(commit),
7039 got_object_commit_get_committer_time(commit));
7041 logmsg = got_object_commit_get_logmsg_raw(commit);
7042 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7043 fprintf(outfile, "%s", logmsg);
7044 done:
7045 free(id_str);
7046 got_object_commit_close(commit);
7047 return err;
7050 static const struct got_error *
7051 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7053 const struct got_error *err;
7054 struct got_tag_object *tag;
7055 char *id_str = NULL;
7056 const char *tagmsg = NULL;
7058 err = got_object_open_as_tag(&tag, repo, id);
7059 if (err)
7060 return err;
7062 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7063 if (err)
7064 goto done;
7066 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7068 switch (got_object_tag_get_object_type(tag)) {
7069 case GOT_OBJ_TYPE_BLOB:
7070 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7071 GOT_OBJ_LABEL_BLOB);
7072 break;
7073 case GOT_OBJ_TYPE_TREE:
7074 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7075 GOT_OBJ_LABEL_TREE);
7076 break;
7077 case GOT_OBJ_TYPE_COMMIT:
7078 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7079 GOT_OBJ_LABEL_COMMIT);
7080 break;
7081 case GOT_OBJ_TYPE_TAG:
7082 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7083 GOT_OBJ_LABEL_TAG);
7084 break;
7085 default:
7086 break;
7089 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7090 got_object_tag_get_name(tag));
7092 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7093 got_object_tag_get_tagger(tag),
7094 got_object_tag_get_tagger_time(tag));
7096 tagmsg = got_object_tag_get_message(tag);
7097 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7098 fprintf(outfile, "%s", tagmsg);
7099 done:
7100 free(id_str);
7101 got_object_tag_close(tag);
7102 return err;
7105 static const struct got_error *
7106 cmd_cat(int argc, char *argv[])
7108 const struct got_error *error;
7109 struct got_repository *repo = NULL;
7110 struct got_worktree *worktree = NULL;
7111 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7112 const char *commit_id_str = NULL;
7113 struct got_object_id *id = NULL, *commit_id = NULL;
7114 int ch, obj_type, i, force_path = 0;
7116 #ifndef PROFILE
7117 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7118 NULL) == -1)
7119 err(1, "pledge");
7120 #endif
7122 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7123 switch (ch) {
7124 case 'c':
7125 commit_id_str = optarg;
7126 break;
7127 case 'r':
7128 repo_path = realpath(optarg, NULL);
7129 if (repo_path == NULL)
7130 return got_error_from_errno2("realpath",
7131 optarg);
7132 got_path_strip_trailing_slashes(repo_path);
7133 break;
7134 case 'P':
7135 force_path = 1;
7136 break;
7137 default:
7138 usage_cat();
7139 /* NOTREACHED */
7143 argc -= optind;
7144 argv += optind;
7146 cwd = getcwd(NULL, 0);
7147 if (cwd == NULL) {
7148 error = got_error_from_errno("getcwd");
7149 goto done;
7151 error = got_worktree_open(&worktree, cwd);
7152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7153 goto done;
7154 if (worktree) {
7155 if (repo_path == NULL) {
7156 repo_path = strdup(
7157 got_worktree_get_repo_path(worktree));
7158 if (repo_path == NULL) {
7159 error = got_error_from_errno("strdup");
7160 goto done;
7165 if (repo_path == NULL) {
7166 repo_path = getcwd(NULL, 0);
7167 if (repo_path == NULL)
7168 return got_error_from_errno("getcwd");
7171 error = got_repo_open(&repo, repo_path, NULL);
7172 free(repo_path);
7173 if (error != NULL)
7174 goto done;
7176 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7177 if (error)
7178 goto done;
7180 if (commit_id_str == NULL)
7181 commit_id_str = GOT_REF_HEAD;
7182 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7183 if (error)
7184 goto done;
7186 for (i = 0; i < argc; i++) {
7187 if (force_path) {
7188 error = got_object_id_by_path(&id, repo, commit_id,
7189 argv[i]);
7190 if (error)
7191 break;
7192 } else {
7193 error = match_object_id(&id, &label, argv[i],
7194 GOT_OBJ_TYPE_ANY, 0, repo);
7195 if (error) {
7196 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7197 error->code != GOT_ERR_NOT_REF)
7198 break;
7199 error = got_object_id_by_path(&id, repo,
7200 commit_id, argv[i]);
7201 if (error)
7202 break;
7206 error = got_object_get_type(&obj_type, repo, id);
7207 if (error)
7208 break;
7210 switch (obj_type) {
7211 case GOT_OBJ_TYPE_BLOB:
7212 error = cat_blob(id, repo, stdout);
7213 break;
7214 case GOT_OBJ_TYPE_TREE:
7215 error = cat_tree(id, repo, stdout);
7216 break;
7217 case GOT_OBJ_TYPE_COMMIT:
7218 error = cat_commit(id, repo, stdout);
7219 break;
7220 case GOT_OBJ_TYPE_TAG:
7221 error = cat_tag(id, repo, stdout);
7222 break;
7223 default:
7224 error = got_error(GOT_ERR_OBJ_TYPE);
7225 break;
7227 if (error)
7228 break;
7229 free(label);
7230 label = NULL;
7231 free(id);
7232 id = NULL;
7235 done:
7236 free(label);
7237 free(id);
7238 free(commit_id);
7239 if (worktree)
7240 got_worktree_close(worktree);
7241 if (repo) {
7242 const struct got_error *repo_error;
7243 repo_error = got_repo_close(repo);
7244 if (error == NULL)
7245 error = repo_error;
7247 return error;