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 /*
658 * Don't let the user create a branch name with a leading '-'.
659 * While technically a valid reference name, this case is usually
660 * an unintended typo.
661 */
662 if (branch_name[0] == '-')
663 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
665 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
666 error = got_error_from_errno("asprintf");
667 goto done;
670 error = got_ref_open(&branch_ref, repo, refname, 0);
671 if (error) {
672 if (error->code != GOT_ERR_NOT_REF)
673 goto done;
674 } else {
675 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
676 "import target branch already exists");
677 goto done;
680 path_dir = realpath(argv[0], NULL);
681 if (path_dir == NULL) {
682 error = got_error_from_errno2("realpath", argv[0]);
683 goto done;
685 got_path_strip_trailing_slashes(path_dir);
687 /*
688 * unveil(2) traverses exec(2); if an editor is used we have
689 * to apply unveil after the log message has been written.
690 */
691 if (logmsg == NULL || strlen(logmsg) == 0) {
692 error = get_editor(&editor);
693 if (error)
694 goto done;
695 free(logmsg);
696 error = collect_import_msg(&logmsg, &logmsg_path, editor,
697 path_dir, refname);
698 if (error) {
699 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
700 logmsg_path != NULL)
701 preserve_logmsg = 1;
702 goto done;
706 if (unveil(path_dir, "r") != 0) {
707 error = got_error_from_errno2("unveil", path_dir);
708 if (logmsg_path)
709 preserve_logmsg = 1;
710 goto done;
713 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
714 if (error) {
715 if (logmsg_path)
716 preserve_logmsg = 1;
717 goto done;
720 error = got_repo_import(&new_commit_id, path_dir, logmsg,
721 author, &ignores, repo, import_progress, NULL);
722 if (error) {
723 if (logmsg_path)
724 preserve_logmsg = 1;
725 goto done;
728 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
729 if (error) {
730 if (logmsg_path)
731 preserve_logmsg = 1;
732 goto done;
735 error = got_ref_write(branch_ref, repo);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_object_id_str(&id_str, new_commit_id);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
750 if (error) {
751 if (error->code != GOT_ERR_NOT_REF) {
752 if (logmsg_path)
753 preserve_logmsg = 1;
754 goto done;
757 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
758 branch_ref);
759 if (error) {
760 if (logmsg_path)
761 preserve_logmsg = 1;
762 goto done;
765 error = got_ref_write(head_ref, repo);
766 if (error) {
767 if (logmsg_path)
768 preserve_logmsg = 1;
769 goto done;
773 printf("Created branch %s with commit %s\n",
774 got_ref_get_name(branch_ref), id_str);
775 done:
776 if (preserve_logmsg) {
777 fprintf(stderr, "%s: log message preserved in %s\n",
778 getprogname(), logmsg_path);
779 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
780 error = got_error_from_errno2("unlink", logmsg_path);
781 free(logmsg);
782 free(logmsg_path);
783 free(repo_path);
784 free(editor);
785 free(refname);
786 free(new_commit_id);
787 free(id_str);
788 free(author);
789 free(gitconfig_path);
790 if (branch_ref)
791 got_ref_close(branch_ref);
792 if (head_ref)
793 got_ref_close(head_ref);
794 return error;
797 __dead static void
798 usage_checkout(void)
800 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
801 "[-p prefix] repository-path [worktree-path]\n", getprogname());
802 exit(1);
805 static const struct got_error *
806 checkout_progress(void *arg, unsigned char status, const char *path)
808 char *worktree_path = arg;
810 /* Base commit bump happens silently. */
811 if (status == GOT_STATUS_BUMP_BASE)
812 return NULL;
814 while (path[0] == '/')
815 path++;
817 printf("%c %s/%s\n", status, worktree_path, path);
818 return NULL;
821 static const struct got_error *
822 check_cancelled(void *arg)
824 if (sigint_received || sigpipe_received)
825 return got_error(GOT_ERR_CANCELLED);
826 return NULL;
829 static const struct got_error *
830 check_linear_ancestry(struct got_object_id *commit_id,
831 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
832 struct got_repository *repo)
834 const struct got_error *err = NULL;
835 struct got_object_id *yca_id;
837 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
838 commit_id, base_commit_id, repo, check_cancelled, NULL);
839 if (err)
840 return err;
842 if (yca_id == NULL)
843 return got_error(GOT_ERR_ANCESTRY);
845 /*
846 * Require a straight line of history between the target commit
847 * and the work tree's base commit.
849 * Non-linear situations such as this require a rebase:
851 * (commit) D F (base_commit)
852 * \ /
853 * C E
854 * \ /
855 * B (yca)
856 * |
857 * A
859 * 'got update' only handles linear cases:
860 * Update forwards in time: A (base/yca) - B - C - D (commit)
861 * Update backwards in time: D (base) - C - B - A (commit/yca)
862 */
863 if (allow_forwards_in_time_only) {
864 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
865 return got_error(GOT_ERR_ANCESTRY);
866 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
867 got_object_id_cmp(base_commit_id, yca_id) != 0)
868 return got_error(GOT_ERR_ANCESTRY);
870 free(yca_id);
871 return NULL;
874 static const struct got_error *
875 check_same_branch(struct got_object_id *commit_id,
876 struct got_reference *head_ref, struct got_object_id *yca_id,
877 struct got_repository *repo)
879 const struct got_error *err = NULL;
880 struct got_commit_graph *graph = NULL;
881 struct got_object_id *head_commit_id = NULL;
882 int is_same_branch = 0;
884 err = got_ref_resolve(&head_commit_id, repo, head_ref);
885 if (err)
886 goto done;
888 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
889 is_same_branch = 1;
890 goto done;
892 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
893 is_same_branch = 1;
894 goto done;
897 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
898 if (err)
899 goto done;
901 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
902 check_cancelled, NULL);
903 if (err)
904 goto done;
906 for (;;) {
907 struct got_object_id *id;
908 err = got_commit_graph_iter_next(&id, graph);
909 if (err) {
910 if (err->code == GOT_ERR_ITER_COMPLETED) {
911 err = NULL;
912 break;
913 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
914 break;
915 err = got_commit_graph_fetch_commits(graph, 1,
916 repo, check_cancelled, NULL);
917 if (err)
918 break;
921 if (id) {
922 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
923 break;
924 if (got_object_id_cmp(id, commit_id) == 0) {
925 is_same_branch = 1;
926 break;
930 done:
931 if (graph)
932 got_commit_graph_close(graph);
933 free(head_commit_id);
934 if (!err && !is_same_branch)
935 err = got_error(GOT_ERR_ANCESTRY);
936 return err;
939 static const struct got_error *
940 resolve_commit_arg(struct got_object_id **commit_id,
941 const char *commit_id_arg, struct got_repository *repo)
943 const struct got_error *err;
944 struct got_reference *ref;
945 struct got_tag_object *tag;
947 err = got_repo_object_match_tag(&tag, commit_id_arg,
948 GOT_OBJ_TYPE_COMMIT, repo);
949 if (err == NULL) {
950 *commit_id = got_object_id_dup(
951 got_object_tag_get_object_id(tag));
952 if (*commit_id == NULL)
953 err = got_error_from_errno("got_object_id_dup");
954 got_object_tag_close(tag);
955 return err;
956 } else if (err->code != GOT_ERR_NO_OBJ)
957 return err;
959 err = got_ref_open(&ref, repo, commit_id_arg, 0);
960 if (err == NULL) {
961 err = got_ref_resolve(commit_id, repo, ref);
962 got_ref_close(ref);
963 } else {
964 if (err->code != GOT_ERR_NOT_REF)
965 return err;
966 err = got_repo_match_object_id_prefix(commit_id,
967 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
969 return err;
972 static const struct got_error *
973 cmd_checkout(int argc, char *argv[])
975 const struct got_error *error = NULL;
976 struct got_repository *repo = NULL;
977 struct got_reference *head_ref = NULL;
978 struct got_worktree *worktree = NULL;
979 char *repo_path = NULL;
980 char *worktree_path = NULL;
981 const char *path_prefix = "";
982 const char *branch_name = GOT_REF_HEAD;
983 char *commit_id_str = NULL;
984 int ch, same_path_prefix;
985 struct got_pathlist_head paths;
987 TAILQ_INIT(&paths);
989 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
990 switch (ch) {
991 case 'b':
992 branch_name = optarg;
993 break;
994 case 'c':
995 commit_id_str = strdup(optarg);
996 if (commit_id_str == NULL)
997 return got_error_from_errno("strdup");
998 break;
999 case 'p':
1000 path_prefix = optarg;
1001 break;
1002 default:
1003 usage_checkout();
1004 /* NOTREACHED */
1008 argc -= optind;
1009 argv += optind;
1011 #ifndef PROFILE
1012 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1013 "unveil", NULL) == -1)
1014 err(1, "pledge");
1015 #endif
1016 if (argc == 1) {
1017 char *cwd, *base, *dotgit;
1018 repo_path = realpath(argv[0], NULL);
1019 if (repo_path == NULL)
1020 return got_error_from_errno2("realpath", argv[0]);
1021 cwd = getcwd(NULL, 0);
1022 if (cwd == NULL) {
1023 error = got_error_from_errno("getcwd");
1024 goto done;
1026 if (path_prefix[0]) {
1027 base = basename(path_prefix);
1028 if (base == NULL) {
1029 error = got_error_from_errno2("basename",
1030 path_prefix);
1031 goto done;
1033 } else {
1034 base = basename(repo_path);
1035 if (base == NULL) {
1036 error = got_error_from_errno2("basename",
1037 repo_path);
1038 goto done;
1041 dotgit = strstr(base, ".git");
1042 if (dotgit)
1043 *dotgit = '\0';
1044 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1045 error = got_error_from_errno("asprintf");
1046 free(cwd);
1047 goto done;
1049 free(cwd);
1050 } else if (argc == 2) {
1051 repo_path = realpath(argv[0], NULL);
1052 if (repo_path == NULL) {
1053 error = got_error_from_errno2("realpath", argv[0]);
1054 goto done;
1056 worktree_path = realpath(argv[1], NULL);
1057 if (worktree_path == NULL) {
1058 if (errno != ENOENT) {
1059 error = got_error_from_errno2("realpath",
1060 argv[1]);
1061 goto done;
1063 worktree_path = strdup(argv[1]);
1064 if (worktree_path == NULL) {
1065 error = got_error_from_errno("strdup");
1066 goto done;
1069 } else
1070 usage_checkout();
1072 got_path_strip_trailing_slashes(repo_path);
1073 got_path_strip_trailing_slashes(worktree_path);
1075 error = got_repo_open(&repo, repo_path, NULL);
1076 if (error != NULL)
1077 goto done;
1079 /* Pre-create work tree path for unveil(2) */
1080 error = got_path_mkdir(worktree_path);
1081 if (error) {
1082 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1083 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1084 goto done;
1085 if (!got_path_dir_is_empty(worktree_path)) {
1086 error = got_error_path(worktree_path,
1087 GOT_ERR_DIR_NOT_EMPTY);
1088 goto done;
1092 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1093 if (error)
1094 goto done;
1096 error = got_ref_open(&head_ref, repo, branch_name, 0);
1097 if (error != NULL)
1098 goto done;
1100 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1101 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1102 goto done;
1104 error = got_worktree_open(&worktree, worktree_path);
1105 if (error != NULL)
1106 goto done;
1108 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1109 path_prefix);
1110 if (error != NULL)
1111 goto done;
1112 if (!same_path_prefix) {
1113 error = got_error(GOT_ERR_PATH_PREFIX);
1114 goto done;
1117 if (commit_id_str) {
1118 struct got_object_id *commit_id;
1119 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1120 if (error)
1121 goto done;
1122 error = check_linear_ancestry(commit_id,
1123 got_worktree_get_base_commit_id(worktree), 0, repo);
1124 if (error != NULL) {
1125 free(commit_id);
1126 goto done;
1128 error = check_same_branch(commit_id, head_ref, NULL, repo);
1129 if (error)
1130 goto done;
1131 error = got_worktree_set_base_commit_id(worktree, repo,
1132 commit_id);
1133 free(commit_id);
1134 if (error)
1135 goto done;
1138 error = got_pathlist_append(&paths, "", NULL);
1139 if (error)
1140 goto done;
1141 error = got_worktree_checkout_files(worktree, &paths, repo,
1142 checkout_progress, worktree_path, check_cancelled, NULL);
1143 if (error != NULL)
1144 goto done;
1146 printf("Now shut up and hack\n");
1148 done:
1149 got_pathlist_free(&paths);
1150 free(commit_id_str);
1151 free(repo_path);
1152 free(worktree_path);
1153 return error;
1156 __dead static void
1157 usage_update(void)
1159 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1160 getprogname());
1161 exit(1);
1164 static const struct got_error *
1165 update_progress(void *arg, unsigned char status, const char *path)
1167 int *did_something = arg;
1169 if (status == GOT_STATUS_EXISTS)
1170 return NULL;
1172 *did_something = 1;
1174 /* Base commit bump happens silently. */
1175 if (status == GOT_STATUS_BUMP_BASE)
1176 return NULL;
1178 while (path[0] == '/')
1179 path++;
1180 printf("%c %s\n", status, path);
1181 return NULL;
1184 static const struct got_error *
1185 switch_head_ref(struct got_reference *head_ref,
1186 struct got_object_id *commit_id, struct got_worktree *worktree,
1187 struct got_repository *repo)
1189 const struct got_error *err = NULL;
1190 char *base_id_str;
1191 int ref_has_moved = 0;
1193 /* Trivial case: switching between two different references. */
1194 if (strcmp(got_ref_get_name(head_ref),
1195 got_worktree_get_head_ref_name(worktree)) != 0) {
1196 printf("Switching work tree from %s to %s\n",
1197 got_worktree_get_head_ref_name(worktree),
1198 got_ref_get_name(head_ref));
1199 return got_worktree_set_head_ref(worktree, head_ref);
1202 err = check_linear_ancestry(commit_id,
1203 got_worktree_get_base_commit_id(worktree), 0, repo);
1204 if (err) {
1205 if (err->code != GOT_ERR_ANCESTRY)
1206 return err;
1207 ref_has_moved = 1;
1209 if (!ref_has_moved)
1210 return NULL;
1212 /* Switching to a rebased branch with the same reference name. */
1213 err = got_object_id_str(&base_id_str,
1214 got_worktree_get_base_commit_id(worktree));
1215 if (err)
1216 return err;
1217 printf("Reference %s now points at a different branch\n",
1218 got_worktree_get_head_ref_name(worktree));
1219 printf("Switching work tree from %s to %s\n", base_id_str,
1220 got_worktree_get_head_ref_name(worktree));
1221 return NULL;
1224 static const struct got_error *
1225 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1227 const struct got_error *err;
1228 int in_progress;
1230 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1231 if (err)
1232 return err;
1233 if (in_progress)
1234 return got_error(GOT_ERR_REBASING);
1236 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1237 if (err)
1238 return err;
1239 if (in_progress)
1240 return got_error(GOT_ERR_HISTEDIT_BUSY);
1242 return NULL;
1245 static const struct got_error *
1246 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1247 char *argv[], struct got_worktree *worktree)
1249 const struct got_error *err = NULL;
1250 char *path;
1251 int i;
1253 if (argc == 0) {
1254 path = strdup("");
1255 if (path == NULL)
1256 return got_error_from_errno("strdup");
1257 return got_pathlist_append(paths, path, NULL);
1260 for (i = 0; i < argc; i++) {
1261 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1262 if (err)
1263 break;
1264 err = got_pathlist_append(paths, path, NULL);
1265 if (err) {
1266 free(path);
1267 break;
1271 return err;
1274 static const struct got_error *
1275 cmd_update(int argc, char *argv[])
1277 const struct got_error *error = NULL;
1278 struct got_repository *repo = NULL;
1279 struct got_worktree *worktree = NULL;
1280 char *worktree_path = NULL;
1281 struct got_object_id *commit_id = NULL;
1282 char *commit_id_str = NULL;
1283 const char *branch_name = NULL;
1284 struct got_reference *head_ref = NULL;
1285 struct got_pathlist_head paths;
1286 struct got_pathlist_entry *pe;
1287 int ch, did_something = 0;
1289 TAILQ_INIT(&paths);
1291 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1292 switch (ch) {
1293 case 'b':
1294 branch_name = optarg;
1295 break;
1296 case 'c':
1297 commit_id_str = strdup(optarg);
1298 if (commit_id_str == NULL)
1299 return got_error_from_errno("strdup");
1300 break;
1301 default:
1302 usage_update();
1303 /* NOTREACHED */
1307 argc -= optind;
1308 argv += optind;
1310 #ifndef PROFILE
1311 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1312 "unveil", NULL) == -1)
1313 err(1, "pledge");
1314 #endif
1315 worktree_path = getcwd(NULL, 0);
1316 if (worktree_path == NULL) {
1317 error = got_error_from_errno("getcwd");
1318 goto done;
1320 error = got_worktree_open(&worktree, worktree_path);
1321 if (error)
1322 goto done;
1324 error = check_rebase_or_histedit_in_progress(worktree);
1325 if (error)
1326 goto done;
1328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1329 NULL);
1330 if (error != NULL)
1331 goto done;
1333 error = apply_unveil(got_repo_get_path(repo), 0,
1334 got_worktree_get_root_path(worktree));
1335 if (error)
1336 goto done;
1338 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1339 if (error)
1340 goto done;
1342 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1343 got_worktree_get_head_ref_name(worktree), 0);
1344 if (error != NULL)
1345 goto done;
1346 if (commit_id_str == NULL) {
1347 error = got_ref_resolve(&commit_id, repo, head_ref);
1348 if (error != NULL)
1349 goto done;
1350 error = got_object_id_str(&commit_id_str, commit_id);
1351 if (error != NULL)
1352 goto done;
1353 } else {
1354 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1355 free(commit_id_str);
1356 commit_id_str = NULL;
1357 if (error)
1358 goto done;
1359 error = got_object_id_str(&commit_id_str, commit_id);
1360 if (error)
1361 goto done;
1364 if (branch_name) {
1365 struct got_object_id *head_commit_id;
1366 TAILQ_FOREACH(pe, &paths, entry) {
1367 if (pe->path_len == 0)
1368 continue;
1369 error = got_error_msg(GOT_ERR_BAD_PATH,
1370 "switching between branches requires that "
1371 "the entire work tree gets updated");
1372 goto done;
1374 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1375 if (error)
1376 goto done;
1377 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1378 repo);
1379 free(head_commit_id);
1380 if (error != NULL)
1381 goto done;
1382 error = check_same_branch(commit_id, head_ref, NULL, repo);
1383 if (error)
1384 goto done;
1385 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1386 if (error)
1387 goto done;
1388 } else {
1389 error = check_linear_ancestry(commit_id,
1390 got_worktree_get_base_commit_id(worktree), 0, repo);
1391 if (error != NULL) {
1392 if (error->code == GOT_ERR_ANCESTRY)
1393 error = got_error(GOT_ERR_BRANCH_MOVED);
1394 goto done;
1396 error = check_same_branch(commit_id, head_ref, NULL, repo);
1397 if (error)
1398 goto done;
1401 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1402 commit_id) != 0) {
1403 error = got_worktree_set_base_commit_id(worktree, repo,
1404 commit_id);
1405 if (error)
1406 goto done;
1409 error = got_worktree_checkout_files(worktree, &paths, repo,
1410 update_progress, &did_something, check_cancelled, NULL);
1411 if (error != NULL)
1412 goto done;
1414 if (did_something)
1415 printf("Updated to commit %s\n", commit_id_str);
1416 else
1417 printf("Already up-to-date\n");
1418 done:
1419 free(worktree_path);
1420 TAILQ_FOREACH(pe, &paths, entry)
1421 free((char *)pe->path);
1422 got_pathlist_free(&paths);
1423 free(commit_id);
1424 free(commit_id_str);
1425 return error;
1428 static const struct got_error *
1429 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1430 const char *path, int diff_context, int ignore_whitespace,
1431 struct got_repository *repo)
1433 const struct got_error *err = NULL;
1434 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1436 if (blob_id1) {
1437 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1438 if (err)
1439 goto done;
1442 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1443 if (err)
1444 goto done;
1446 while (path[0] == '/')
1447 path++;
1448 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1449 ignore_whitespace, stdout);
1450 done:
1451 if (blob1)
1452 got_object_blob_close(blob1);
1453 got_object_blob_close(blob2);
1454 return err;
1457 static const struct got_error *
1458 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1459 const char *path, int diff_context, int ignore_whitespace,
1460 struct got_repository *repo)
1462 const struct got_error *err = NULL;
1463 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1464 struct got_diff_blob_output_unidiff_arg arg;
1466 if (tree_id1) {
1467 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1468 if (err)
1469 goto done;
1472 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1473 if (err)
1474 goto done;
1476 arg.diff_context = diff_context;
1477 arg.ignore_whitespace = ignore_whitespace;
1478 arg.outfile = stdout;
1479 while (path[0] == '/')
1480 path++;
1481 err = got_diff_tree(tree1, tree2, path, path, repo,
1482 got_diff_blob_output_unidiff, &arg, 1);
1483 done:
1484 if (tree1)
1485 got_object_tree_close(tree1);
1486 if (tree2)
1487 got_object_tree_close(tree2);
1488 return err;
1491 static const struct got_error *
1492 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1493 const char *path, int diff_context, struct got_repository *repo)
1495 const struct got_error *err = NULL;
1496 struct got_commit_object *pcommit = NULL;
1497 char *id_str1 = NULL, *id_str2 = NULL;
1498 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1499 struct got_object_qid *qid;
1501 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1502 if (qid != NULL) {
1503 err = got_object_open_as_commit(&pcommit, repo,
1504 qid->id);
1505 if (err)
1506 return err;
1509 if (path && path[0] != '\0') {
1510 int obj_type;
1511 err = got_object_id_by_path(&obj_id2, repo, id, path);
1512 if (err)
1513 goto done;
1514 err = got_object_id_str(&id_str2, obj_id2);
1515 if (err) {
1516 free(obj_id2);
1517 goto done;
1519 if (pcommit) {
1520 err = got_object_id_by_path(&obj_id1, repo,
1521 qid->id, path);
1522 if (err) {
1523 free(obj_id2);
1524 goto done;
1526 err = got_object_id_str(&id_str1, obj_id1);
1527 if (err) {
1528 free(obj_id2);
1529 goto done;
1532 err = got_object_get_type(&obj_type, repo, obj_id2);
1533 if (err) {
1534 free(obj_id2);
1535 goto done;
1537 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1538 switch (obj_type) {
1539 case GOT_OBJ_TYPE_BLOB:
1540 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1541 0, repo);
1542 break;
1543 case GOT_OBJ_TYPE_TREE:
1544 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1545 0, repo);
1546 break;
1547 default:
1548 err = got_error(GOT_ERR_OBJ_TYPE);
1549 break;
1551 free(obj_id1);
1552 free(obj_id2);
1553 } else {
1554 obj_id2 = got_object_commit_get_tree_id(commit);
1555 err = got_object_id_str(&id_str2, obj_id2);
1556 if (err)
1557 goto done;
1558 obj_id1 = got_object_commit_get_tree_id(pcommit);
1559 err = got_object_id_str(&id_str1, obj_id1);
1560 if (err)
1561 goto done;
1562 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1563 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1566 done:
1567 free(id_str1);
1568 free(id_str2);
1569 if (pcommit)
1570 got_object_commit_close(pcommit);
1571 return err;
1574 static char *
1575 get_datestr(time_t *time, char *datebuf)
1577 struct tm mytm, *tm;
1578 char *p, *s;
1580 tm = gmtime_r(time, &mytm);
1581 if (tm == NULL)
1582 return NULL;
1583 s = asctime_r(tm, datebuf);
1584 if (s == NULL)
1585 return NULL;
1586 p = strchr(s, '\n');
1587 if (p)
1588 *p = '\0';
1589 return s;
1592 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1594 static const struct got_error *
1595 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1596 struct got_repository *repo, const char *path, int show_patch,
1597 int diff_context, struct got_reflist_head *refs)
1599 const struct got_error *err = NULL;
1600 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1601 char datebuf[26];
1602 time_t committer_time;
1603 const char *author, *committer;
1604 char *refs_str = NULL;
1605 struct got_reflist_entry *re;
1607 SIMPLEQ_FOREACH(re, refs, entry) {
1608 char *s;
1609 const char *name;
1610 struct got_tag_object *tag = NULL;
1611 int cmp;
1613 name = got_ref_get_name(re->ref);
1614 if (strcmp(name, GOT_REF_HEAD) == 0)
1615 continue;
1616 if (strncmp(name, "refs/", 5) == 0)
1617 name += 5;
1618 if (strncmp(name, "got/", 4) == 0)
1619 continue;
1620 if (strncmp(name, "heads/", 6) == 0)
1621 name += 6;
1622 if (strncmp(name, "remotes/", 8) == 0)
1623 name += 8;
1624 if (strncmp(name, "tags/", 5) == 0) {
1625 err = got_object_open_as_tag(&tag, repo, re->id);
1626 if (err) {
1627 if (err->code != GOT_ERR_OBJ_TYPE)
1628 return err;
1629 /* Ref points at something other than a tag. */
1630 err = NULL;
1631 tag = NULL;
1634 cmp = got_object_id_cmp(tag ?
1635 got_object_tag_get_object_id(tag) : re->id, id);
1636 if (tag)
1637 got_object_tag_close(tag);
1638 if (cmp != 0)
1639 continue;
1640 s = refs_str;
1641 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1642 name) == -1) {
1643 err = got_error_from_errno("asprintf");
1644 free(s);
1645 return err;
1647 free(s);
1649 err = got_object_id_str(&id_str, id);
1650 if (err)
1651 return err;
1653 printf(GOT_COMMIT_SEP_STR);
1654 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1655 refs_str ? refs_str : "", refs_str ? ")" : "");
1656 free(id_str);
1657 id_str = NULL;
1658 free(refs_str);
1659 refs_str = NULL;
1660 printf("from: %s\n", got_object_commit_get_author(commit));
1661 committer_time = got_object_commit_get_committer_time(commit);
1662 datestr = get_datestr(&committer_time, datebuf);
1663 if (datestr)
1664 printf("date: %s UTC\n", datestr);
1665 author = got_object_commit_get_author(commit);
1666 committer = got_object_commit_get_committer(commit);
1667 if (strcmp(author, committer) != 0)
1668 printf("via: %s\n", committer);
1669 if (got_object_commit_get_nparents(commit) > 1) {
1670 const struct got_object_id_queue *parent_ids;
1671 struct got_object_qid *qid;
1672 int n = 1;
1673 parent_ids = got_object_commit_get_parent_ids(commit);
1674 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1675 err = got_object_id_str(&id_str, qid->id);
1676 if (err)
1677 return err;
1678 printf("parent %d: %s\n", n++, id_str);
1679 free(id_str);
1683 err = got_object_commit_get_logmsg(&logmsg0, commit);
1684 if (err)
1685 return err;
1687 logmsg = logmsg0;
1688 do {
1689 line = strsep(&logmsg, "\n");
1690 if (line)
1691 printf(" %s\n", line);
1692 } while (line);
1693 free(logmsg0);
1695 if (show_patch) {
1696 err = print_patch(commit, id, path, diff_context, repo);
1697 if (err == 0)
1698 printf("\n");
1701 if (fflush(stdout) != 0 && err == NULL)
1702 err = got_error_from_errno("fflush");
1703 return err;
1706 static const struct got_error *
1707 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1708 char *path, int show_patch, int diff_context, int limit,
1709 int first_parent_traversal, struct got_reflist_head *refs)
1711 const struct got_error *err;
1712 struct got_commit_graph *graph;
1714 err = got_commit_graph_open(&graph, root_id, path,
1715 first_parent_traversal, repo);
1716 if (err)
1717 return err;
1718 err = got_commit_graph_iter_start(graph, root_id, repo,
1719 check_cancelled, NULL);
1720 if (err)
1721 goto done;
1722 for (;;) {
1723 struct got_commit_object *commit;
1724 struct got_object_id *id;
1726 if (sigint_received || sigpipe_received)
1727 break;
1729 err = got_commit_graph_iter_next(&id, graph);
1730 if (err) {
1731 if (err->code == GOT_ERR_ITER_COMPLETED) {
1732 err = NULL;
1733 break;
1735 if (err->code != GOT_ERR_ITER_NEED_MORE)
1736 break;
1737 err = got_commit_graph_fetch_commits(graph, 1, repo,
1738 check_cancelled, NULL);
1739 if (err)
1740 break;
1741 else
1742 continue;
1744 if (id == NULL)
1745 break;
1747 err = got_object_open_as_commit(&commit, repo, id);
1748 if (err)
1749 break;
1750 err = print_commit(commit, id, repo, path, show_patch,
1751 diff_context, refs);
1752 got_object_commit_close(commit);
1753 if (err || (limit && --limit == 0))
1754 break;
1756 done:
1757 got_commit_graph_close(graph);
1758 return err;
1761 __dead static void
1762 usage_log(void)
1764 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1765 "[-r repository-path] [path]\n", getprogname());
1766 exit(1);
1769 static int
1770 get_default_log_limit(void)
1772 const char *got_default_log_limit;
1773 long long n;
1774 const char *errstr;
1776 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1777 if (got_default_log_limit == NULL)
1778 return 0;
1779 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1780 if (errstr != NULL)
1781 return 0;
1782 return n;
1785 static const struct got_error *
1786 cmd_log(int argc, char *argv[])
1788 const struct got_error *error;
1789 struct got_repository *repo = NULL;
1790 struct got_worktree *worktree = NULL;
1791 struct got_commit_object *commit = NULL;
1792 struct got_object_id *id = NULL;
1793 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1794 char *start_commit = NULL;
1795 int diff_context = 3, ch;
1796 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1797 const char *errstr;
1798 struct got_reflist_head refs;
1800 SIMPLEQ_INIT(&refs);
1802 #ifndef PROFILE
1803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1804 NULL)
1805 == -1)
1806 err(1, "pledge");
1807 #endif
1809 limit = get_default_log_limit();
1811 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1812 switch (ch) {
1813 case 'p':
1814 show_patch = 1;
1815 break;
1816 case 'c':
1817 start_commit = optarg;
1818 break;
1819 case 'C':
1820 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1821 &errstr);
1822 if (errstr != NULL)
1823 err(1, "-C option %s", errstr);
1824 break;
1825 case 'l':
1826 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1827 if (errstr != NULL)
1828 err(1, "-l option %s", errstr);
1829 break;
1830 case 'f':
1831 first_parent_traversal = 1;
1832 break;
1833 case 'r':
1834 repo_path = realpath(optarg, NULL);
1835 if (repo_path == NULL)
1836 return got_error_from_errno2("realpath",
1837 optarg);
1838 got_path_strip_trailing_slashes(repo_path);
1839 break;
1840 default:
1841 usage_log();
1842 /* NOTREACHED */
1846 argc -= optind;
1847 argv += optind;
1849 cwd = getcwd(NULL, 0);
1850 if (cwd == NULL) {
1851 error = got_error_from_errno("getcwd");
1852 goto done;
1855 error = got_worktree_open(&worktree, cwd);
1856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1857 goto done;
1858 error = NULL;
1860 if (argc == 0) {
1861 path = strdup("");
1862 if (path == NULL) {
1863 error = got_error_from_errno("strdup");
1864 goto done;
1866 } else if (argc == 1) {
1867 if (worktree) {
1868 error = got_worktree_resolve_path(&path, worktree,
1869 argv[0]);
1870 if (error)
1871 goto done;
1872 } else {
1873 path = strdup(argv[0]);
1874 if (path == NULL) {
1875 error = got_error_from_errno("strdup");
1876 goto done;
1879 } else
1880 usage_log();
1882 if (repo_path == NULL) {
1883 repo_path = worktree ?
1884 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1886 if (repo_path == NULL) {
1887 error = got_error_from_errno("strdup");
1888 goto done;
1891 error = got_repo_open(&repo, repo_path, NULL);
1892 if (error != NULL)
1893 goto done;
1895 error = apply_unveil(got_repo_get_path(repo), 1,
1896 worktree ? got_worktree_get_root_path(worktree) : NULL);
1897 if (error)
1898 goto done;
1900 if (start_commit == NULL) {
1901 struct got_reference *head_ref;
1902 error = got_ref_open(&head_ref, repo,
1903 worktree ? got_worktree_get_head_ref_name(worktree)
1904 : GOT_REF_HEAD, 0);
1905 if (error != NULL)
1906 return error;
1907 error = got_ref_resolve(&id, repo, head_ref);
1908 got_ref_close(head_ref);
1909 if (error != NULL)
1910 return error;
1911 error = got_object_open_as_commit(&commit, repo, id);
1912 } else {
1913 struct got_reference *ref;
1914 error = got_ref_open(&ref, repo, start_commit, 0);
1915 if (error == NULL) {
1916 int obj_type;
1917 error = got_ref_resolve(&id, repo, ref);
1918 got_ref_close(ref);
1919 if (error != NULL)
1920 goto done;
1921 error = got_object_get_type(&obj_type, repo, id);
1922 if (error != NULL)
1923 goto done;
1924 if (obj_type == GOT_OBJ_TYPE_TAG) {
1925 struct got_tag_object *tag;
1926 error = got_object_open_as_tag(&tag, repo, id);
1927 if (error != NULL)
1928 goto done;
1929 if (got_object_tag_get_object_type(tag) !=
1930 GOT_OBJ_TYPE_COMMIT) {
1931 got_object_tag_close(tag);
1932 error = got_error(GOT_ERR_OBJ_TYPE);
1933 goto done;
1935 free(id);
1936 id = got_object_id_dup(
1937 got_object_tag_get_object_id(tag));
1938 if (id == NULL)
1939 error = got_error_from_errno(
1940 "got_object_id_dup");
1941 got_object_tag_close(tag);
1942 if (error)
1943 goto done;
1944 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1945 error = got_error(GOT_ERR_OBJ_TYPE);
1946 goto done;
1948 error = got_object_open_as_commit(&commit, repo, id);
1949 if (error != NULL)
1950 goto done;
1952 if (commit == NULL) {
1953 error = got_repo_match_object_id_prefix(&id,
1954 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1955 if (error != NULL)
1956 return error;
1959 if (error != NULL)
1960 goto done;
1962 if (worktree) {
1963 const char *prefix = got_worktree_get_path_prefix(worktree);
1964 char *p;
1965 if (asprintf(&p, "%s%s%s", prefix,
1966 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1967 error = got_error_from_errno("asprintf");
1968 goto done;
1970 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1971 free(p);
1972 } else
1973 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1974 if (error != NULL)
1975 goto done;
1976 if (in_repo_path) {
1977 free(path);
1978 path = in_repo_path;
1981 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1982 if (error)
1983 goto done;
1985 error = print_commits(id, repo, path, show_patch,
1986 diff_context, limit, first_parent_traversal, &refs);
1987 done:
1988 free(path);
1989 free(repo_path);
1990 free(cwd);
1991 free(id);
1992 if (worktree)
1993 got_worktree_close(worktree);
1994 if (repo) {
1995 const struct got_error *repo_error;
1996 repo_error = got_repo_close(repo);
1997 if (error == NULL)
1998 error = repo_error;
2000 got_ref_list_free(&refs);
2001 return error;
2004 __dead static void
2005 usage_diff(void)
2007 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2008 "[-w] [object1 object2 | path]\n", getprogname());
2009 exit(1);
2012 struct print_diff_arg {
2013 struct got_repository *repo;
2014 struct got_worktree *worktree;
2015 int diff_context;
2016 const char *id_str;
2017 int header_shown;
2018 int diff_staged;
2019 int ignore_whitespace;
2022 static const struct got_error *
2023 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2024 const char *path, struct got_object_id *blob_id,
2025 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2027 struct print_diff_arg *a = arg;
2028 const struct got_error *err = NULL;
2029 struct got_blob_object *blob1 = NULL;
2030 FILE *f2 = NULL;
2031 char *abspath = NULL, *label1 = NULL;
2032 struct stat sb;
2034 if (a->diff_staged) {
2035 if (staged_status != GOT_STATUS_MODIFY &&
2036 staged_status != GOT_STATUS_ADD &&
2037 staged_status != GOT_STATUS_DELETE)
2038 return NULL;
2039 } else {
2040 if (staged_status == GOT_STATUS_DELETE)
2041 return NULL;
2042 if (status == GOT_STATUS_NONEXISTENT)
2043 return got_error_set_errno(ENOENT, path);
2044 if (status != GOT_STATUS_MODIFY &&
2045 status != GOT_STATUS_ADD &&
2046 status != GOT_STATUS_DELETE &&
2047 status != GOT_STATUS_CONFLICT)
2048 return NULL;
2051 if (!a->header_shown) {
2052 printf("diff %s %s%s\n", a->id_str,
2053 got_worktree_get_root_path(a->worktree),
2054 a->diff_staged ? " (staged changes)" : "");
2055 a->header_shown = 1;
2058 if (a->diff_staged) {
2059 const char *label1 = NULL, *label2 = NULL;
2060 switch (staged_status) {
2061 case GOT_STATUS_MODIFY:
2062 label1 = path;
2063 label2 = path;
2064 break;
2065 case GOT_STATUS_ADD:
2066 label2 = path;
2067 break;
2068 case GOT_STATUS_DELETE:
2069 label1 = path;
2070 break;
2071 default:
2072 return got_error(GOT_ERR_FILE_STATUS);
2074 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2075 label1, label2, a->diff_context, a->ignore_whitespace,
2076 a->repo, stdout);
2079 if (staged_status == GOT_STATUS_ADD ||
2080 staged_status == GOT_STATUS_MODIFY) {
2081 char *id_str;
2082 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2083 8192);
2084 if (err)
2085 goto done;
2086 err = got_object_id_str(&id_str, staged_blob_id);
2087 if (err)
2088 goto done;
2089 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2090 err = got_error_from_errno("asprintf");
2091 free(id_str);
2092 goto done;
2094 free(id_str);
2095 } else if (status != GOT_STATUS_ADD) {
2096 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2097 if (err)
2098 goto done;
2101 if (status != GOT_STATUS_DELETE) {
2102 if (asprintf(&abspath, "%s/%s",
2103 got_worktree_get_root_path(a->worktree), path) == -1) {
2104 err = got_error_from_errno("asprintf");
2105 goto done;
2108 f2 = fopen(abspath, "r");
2109 if (f2 == NULL) {
2110 err = got_error_from_errno2("fopen", abspath);
2111 goto done;
2113 if (lstat(abspath, &sb) == -1) {
2114 err = got_error_from_errno2("lstat", abspath);
2115 goto done;
2117 } else
2118 sb.st_size = 0;
2120 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2121 a->diff_context, a->ignore_whitespace, stdout);
2122 done:
2123 if (blob1)
2124 got_object_blob_close(blob1);
2125 if (f2 && fclose(f2) != 0 && err == NULL)
2126 err = got_error_from_errno("fclose");
2127 free(abspath);
2128 return err;
2131 static const struct got_error *
2132 match_object_id(struct got_object_id **id, char **label,
2133 const char *id_str, int obj_type, int resolve_tags,
2134 struct got_repository *repo)
2136 const struct got_error *err;
2137 struct got_tag_object *tag;
2138 struct got_reference *ref = NULL;
2140 *id = NULL;
2141 *label = NULL;
2143 if (resolve_tags) {
2144 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2145 repo);
2146 if (err == NULL) {
2147 *id = got_object_id_dup(
2148 got_object_tag_get_object_id(tag));
2149 if (*id == NULL)
2150 err = got_error_from_errno("got_object_id_dup");
2151 else if (asprintf(label, "refs/tags/%s",
2152 got_object_tag_get_name(tag)) == -1) {
2153 err = got_error_from_errno("asprintf");
2154 free(*id);
2155 *id = NULL;
2157 got_object_tag_close(tag);
2158 return err;
2159 } else if (err->code != GOT_ERR_NO_OBJ)
2160 return err;
2163 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2164 if (err) {
2165 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2166 return err;
2167 err = got_ref_open(&ref, repo, id_str, 0);
2168 if (err != NULL)
2169 goto done;
2170 *label = strdup(got_ref_get_name(ref));
2171 if (*label == NULL) {
2172 err = got_error_from_errno("strdup");
2173 goto done;
2175 err = got_ref_resolve(id, repo, ref);
2176 } else {
2177 err = got_object_id_str(label, *id);
2178 if (*label == NULL) {
2179 err = got_error_from_errno("strdup");
2180 goto done;
2183 done:
2184 if (ref)
2185 got_ref_close(ref);
2186 return err;
2190 static const struct got_error *
2191 cmd_diff(int argc, char *argv[])
2193 const struct got_error *error;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2196 char *cwd = NULL, *repo_path = NULL;
2197 struct got_object_id *id1 = NULL, *id2 = NULL;
2198 const char *id_str1 = NULL, *id_str2 = NULL;
2199 char *label1 = NULL, *label2 = NULL;
2200 int type1, type2;
2201 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2202 const char *errstr;
2203 char *path = NULL;
2205 #ifndef PROFILE
2206 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2207 NULL) == -1)
2208 err(1, "pledge");
2209 #endif
2211 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2212 switch (ch) {
2213 case 'C':
2214 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2215 if (errstr != NULL)
2216 err(1, "-C option %s", errstr);
2217 break;
2218 case 'r':
2219 repo_path = realpath(optarg, NULL);
2220 if (repo_path == NULL)
2221 return got_error_from_errno2("realpath",
2222 optarg);
2223 got_path_strip_trailing_slashes(repo_path);
2224 break;
2225 case 's':
2226 diff_staged = 1;
2227 break;
2228 case 'w':
2229 ignore_whitespace = 1;
2230 break;
2231 default:
2232 usage_diff();
2233 /* NOTREACHED */
2237 argc -= optind;
2238 argv += optind;
2240 cwd = getcwd(NULL, 0);
2241 if (cwd == NULL) {
2242 error = got_error_from_errno("getcwd");
2243 goto done;
2245 error = got_worktree_open(&worktree, cwd);
2246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2247 goto done;
2248 if (argc <= 1) {
2249 if (worktree == NULL) {
2250 error = got_error(GOT_ERR_NOT_WORKTREE);
2251 goto done;
2253 if (repo_path)
2254 errx(1,
2255 "-r option can't be used when diffing a work tree");
2256 repo_path = strdup(got_worktree_get_repo_path(worktree));
2257 if (repo_path == NULL) {
2258 error = got_error_from_errno("strdup");
2259 goto done;
2261 if (argc == 1) {
2262 error = got_worktree_resolve_path(&path, worktree,
2263 argv[0]);
2264 if (error)
2265 goto done;
2266 } else {
2267 path = strdup("");
2268 if (path == NULL) {
2269 error = got_error_from_errno("strdup");
2270 goto done;
2273 } else if (argc == 2) {
2274 if (diff_staged)
2275 errx(1, "-s option can't be used when diffing "
2276 "objects in repository");
2277 id_str1 = argv[0];
2278 id_str2 = argv[1];
2279 if (worktree && repo_path == NULL) {
2280 repo_path =
2281 strdup(got_worktree_get_repo_path(worktree));
2282 if (repo_path == NULL) {
2283 error = got_error_from_errno("strdup");
2284 goto done;
2287 } else
2288 usage_diff();
2290 if (repo_path == NULL) {
2291 repo_path = getcwd(NULL, 0);
2292 if (repo_path == NULL)
2293 return got_error_from_errno("getcwd");
2296 error = got_repo_open(&repo, repo_path, NULL);
2297 free(repo_path);
2298 if (error != NULL)
2299 goto done;
2301 error = apply_unveil(got_repo_get_path(repo), 1,
2302 worktree ? got_worktree_get_root_path(worktree) : NULL);
2303 if (error)
2304 goto done;
2306 if (argc <= 1) {
2307 struct print_diff_arg arg;
2308 struct got_pathlist_head paths;
2309 char *id_str;
2311 TAILQ_INIT(&paths);
2313 error = got_object_id_str(&id_str,
2314 got_worktree_get_base_commit_id(worktree));
2315 if (error)
2316 goto done;
2317 arg.repo = repo;
2318 arg.worktree = worktree;
2319 arg.diff_context = diff_context;
2320 arg.id_str = id_str;
2321 arg.header_shown = 0;
2322 arg.diff_staged = diff_staged;
2323 arg.ignore_whitespace = ignore_whitespace;
2325 error = got_pathlist_append(&paths, path, NULL);
2326 if (error)
2327 goto done;
2329 error = got_worktree_status(worktree, &paths, repo, print_diff,
2330 &arg, check_cancelled, NULL);
2331 free(id_str);
2332 got_pathlist_free(&paths);
2333 goto done;
2336 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2337 repo);
2338 if (error)
2339 goto done;
2341 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2342 repo);
2343 if (error)
2344 goto done;
2346 error = got_object_get_type(&type1, repo, id1);
2347 if (error)
2348 goto done;
2350 error = got_object_get_type(&type2, repo, id2);
2351 if (error)
2352 goto done;
2354 if (type1 != type2) {
2355 error = got_error(GOT_ERR_OBJ_TYPE);
2356 goto done;
2359 switch (type1) {
2360 case GOT_OBJ_TYPE_BLOB:
2361 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2362 diff_context, ignore_whitespace, repo, stdout);
2363 break;
2364 case GOT_OBJ_TYPE_TREE:
2365 error = got_diff_objects_as_trees(id1, id2, "", "",
2366 diff_context, ignore_whitespace, repo, stdout);
2367 break;
2368 case GOT_OBJ_TYPE_COMMIT:
2369 printf("diff %s %s\n", label1, label2);
2370 error = got_diff_objects_as_commits(id1, id2, diff_context,
2371 ignore_whitespace, repo, stdout);
2372 break;
2373 default:
2374 error = got_error(GOT_ERR_OBJ_TYPE);
2377 done:
2378 free(label1);
2379 free(label2);
2380 free(id1);
2381 free(id2);
2382 free(path);
2383 if (worktree)
2384 got_worktree_close(worktree);
2385 if (repo) {
2386 const struct got_error *repo_error;
2387 repo_error = got_repo_close(repo);
2388 if (error == NULL)
2389 error = repo_error;
2391 return error;
2394 __dead static void
2395 usage_blame(void)
2397 fprintf(stderr,
2398 "usage: %s blame [-c commit] [-r repository-path] path\n",
2399 getprogname());
2400 exit(1);
2403 struct blame_line {
2404 int annotated;
2405 char *id_str;
2406 char *committer;
2407 char datebuf[11]; /* YYYY-MM-DD + NUL */
2410 struct blame_cb_args {
2411 struct blame_line *lines;
2412 int nlines;
2413 int nlines_prec;
2414 int lineno_cur;
2415 off_t *line_offsets;
2416 FILE *f;
2417 struct got_repository *repo;
2420 static const struct got_error *
2421 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2423 const struct got_error *err = NULL;
2424 struct blame_cb_args *a = arg;
2425 struct blame_line *bline;
2426 char *line = NULL;
2427 size_t linesize = 0;
2428 struct got_commit_object *commit = NULL;
2429 off_t offset;
2430 struct tm tm;
2431 time_t committer_time;
2433 if (nlines != a->nlines ||
2434 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2435 return got_error(GOT_ERR_RANGE);
2437 if (sigint_received)
2438 return got_error(GOT_ERR_ITER_COMPLETED);
2440 if (lineno == -1)
2441 return NULL; /* no change in this commit */
2443 /* Annotate this line. */
2444 bline = &a->lines[lineno - 1];
2445 if (bline->annotated)
2446 return NULL;
2447 err = got_object_id_str(&bline->id_str, id);
2448 if (err)
2449 return err;
2451 err = got_object_open_as_commit(&commit, a->repo, id);
2452 if (err)
2453 goto done;
2455 bline->committer = strdup(got_object_commit_get_committer(commit));
2456 if (bline->committer == NULL) {
2457 err = got_error_from_errno("strdup");
2458 goto done;
2461 committer_time = got_object_commit_get_committer_time(commit);
2462 if (localtime_r(&committer_time, &tm) == NULL)
2463 return got_error_from_errno("localtime_r");
2464 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2465 &tm) >= sizeof(bline->datebuf)) {
2466 err = got_error(GOT_ERR_NO_SPACE);
2467 goto done;
2469 bline->annotated = 1;
2471 /* Print lines annotated so far. */
2472 bline = &a->lines[a->lineno_cur - 1];
2473 if (!bline->annotated)
2474 goto done;
2476 offset = a->line_offsets[a->lineno_cur - 1];
2477 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2478 err = got_error_from_errno("fseeko");
2479 goto done;
2482 while (bline->annotated) {
2483 char *smallerthan, *at, *nl, *committer;
2484 size_t len;
2486 if (getline(&line, &linesize, a->f) == -1) {
2487 if (ferror(a->f))
2488 err = got_error_from_errno("getline");
2489 break;
2492 committer = bline->committer;
2493 smallerthan = strchr(committer, '<');
2494 if (smallerthan && smallerthan[1] != '\0')
2495 committer = smallerthan + 1;
2496 at = strchr(committer, '@');
2497 if (at)
2498 *at = '\0';
2499 len = strlen(committer);
2500 if (len >= 9)
2501 committer[8] = '\0';
2503 nl = strchr(line, '\n');
2504 if (nl)
2505 *nl = '\0';
2506 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2507 bline->id_str, bline->datebuf, committer, line);
2509 a->lineno_cur++;
2510 bline = &a->lines[a->lineno_cur - 1];
2512 done:
2513 if (commit)
2514 got_object_commit_close(commit);
2515 free(line);
2516 return err;
2519 static const struct got_error *
2520 cmd_blame(int argc, char *argv[])
2522 const struct got_error *error;
2523 struct got_repository *repo = NULL;
2524 struct got_worktree *worktree = NULL;
2525 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2526 struct got_object_id *obj_id = NULL;
2527 struct got_object_id *commit_id = NULL;
2528 struct got_blob_object *blob = NULL;
2529 char *commit_id_str = NULL;
2530 struct blame_cb_args bca;
2531 int ch, obj_type, i;
2532 size_t filesize;
2534 memset(&bca, 0, sizeof(bca));
2536 #ifndef PROFILE
2537 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2538 NULL) == -1)
2539 err(1, "pledge");
2540 #endif
2542 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2543 switch (ch) {
2544 case 'c':
2545 commit_id_str = optarg;
2546 break;
2547 case 'r':
2548 repo_path = realpath(optarg, NULL);
2549 if (repo_path == NULL)
2550 return got_error_from_errno2("realpath",
2551 optarg);
2552 got_path_strip_trailing_slashes(repo_path);
2553 break;
2554 default:
2555 usage_blame();
2556 /* NOTREACHED */
2560 argc -= optind;
2561 argv += optind;
2563 if (argc == 1)
2564 path = argv[0];
2565 else
2566 usage_blame();
2568 cwd = getcwd(NULL, 0);
2569 if (cwd == NULL) {
2570 error = got_error_from_errno("getcwd");
2571 goto done;
2573 if (repo_path == NULL) {
2574 error = got_worktree_open(&worktree, cwd);
2575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2576 goto done;
2577 else
2578 error = NULL;
2579 if (worktree) {
2580 repo_path =
2581 strdup(got_worktree_get_repo_path(worktree));
2582 if (repo_path == NULL)
2583 error = got_error_from_errno("strdup");
2584 if (error)
2585 goto done;
2586 } else {
2587 repo_path = strdup(cwd);
2588 if (repo_path == NULL) {
2589 error = got_error_from_errno("strdup");
2590 goto done;
2595 error = got_repo_open(&repo, repo_path, NULL);
2596 if (error != NULL)
2597 goto done;
2599 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2600 if (error)
2601 goto done;
2603 if (worktree) {
2604 const char *prefix = got_worktree_get_path_prefix(worktree);
2605 char *p, *worktree_subdir = cwd +
2606 strlen(got_worktree_get_root_path(worktree));
2607 if (asprintf(&p, "%s%s%s%s%s",
2608 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2609 worktree_subdir, worktree_subdir[0] ? "/" : "",
2610 path) == -1) {
2611 error = got_error_from_errno("asprintf");
2612 goto done;
2614 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2615 free(p);
2616 } else {
2617 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2619 if (error)
2620 goto done;
2622 if (commit_id_str == NULL) {
2623 struct got_reference *head_ref;
2624 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2625 if (error != NULL)
2626 goto done;
2627 error = got_ref_resolve(&commit_id, repo, head_ref);
2628 got_ref_close(head_ref);
2629 if (error != NULL)
2630 goto done;
2631 } else {
2632 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2633 if (error)
2634 goto done;
2637 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2638 if (error)
2639 goto done;
2640 if (obj_id == NULL) {
2641 error = got_error(GOT_ERR_NO_OBJ);
2642 goto done;
2645 error = got_object_get_type(&obj_type, repo, obj_id);
2646 if (error)
2647 goto done;
2649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2650 error = got_error(GOT_ERR_OBJ_TYPE);
2651 goto done;
2654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2655 if (error)
2656 goto done;
2657 bca.f = got_opentemp();
2658 if (bca.f == NULL) {
2659 error = got_error_from_errno("got_opentemp");
2660 goto done;
2662 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2663 &bca.line_offsets, bca.f, blob);
2664 if (error || bca.nlines == 0)
2665 goto done;
2667 /* Don't include \n at EOF in the blame line count. */
2668 if (bca.line_offsets[bca.nlines - 1] == filesize)
2669 bca.nlines--;
2671 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2672 if (bca.lines == NULL) {
2673 error = got_error_from_errno("calloc");
2674 goto done;
2676 bca.lineno_cur = 1;
2677 bca.nlines_prec = 0;
2678 i = bca.nlines;
2679 while (i > 0) {
2680 i /= 10;
2681 bca.nlines_prec++;
2683 bca.repo = repo;
2685 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2686 check_cancelled, NULL);
2687 if (error)
2688 goto done;
2689 done:
2690 free(in_repo_path);
2691 free(repo_path);
2692 free(cwd);
2693 free(commit_id);
2694 free(obj_id);
2695 if (blob)
2696 got_object_blob_close(blob);
2697 if (worktree)
2698 got_worktree_close(worktree);
2699 if (repo) {
2700 const struct got_error *repo_error;
2701 repo_error = got_repo_close(repo);
2702 if (error == NULL)
2703 error = repo_error;
2705 if (bca.lines) {
2706 for (i = 0; i < bca.nlines; i++) {
2707 struct blame_line *bline = &bca.lines[i];
2708 free(bline->id_str);
2709 free(bline->committer);
2711 free(bca.lines);
2713 free(bca.line_offsets);
2714 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2715 error = got_error_from_errno("fclose");
2716 return error;
2719 __dead static void
2720 usage_tree(void)
2722 fprintf(stderr,
2723 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2724 getprogname());
2725 exit(1);
2728 static void
2729 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2730 const char *root_path)
2732 int is_root_path = (strcmp(path, root_path) == 0);
2733 const char *modestr = "";
2734 mode_t mode = got_tree_entry_get_mode(te);
2736 path += strlen(root_path);
2737 while (path[0] == '/')
2738 path++;
2740 if (got_object_tree_entry_is_submodule(te))
2741 modestr = "$";
2742 else if (S_ISLNK(mode))
2743 modestr = "@";
2744 else if (S_ISDIR(mode))
2745 modestr = "/";
2746 else if (mode & S_IXUSR)
2747 modestr = "*";
2749 printf("%s%s%s%s%s\n", id ? id : "", path,
2750 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2753 static const struct got_error *
2754 print_tree(const char *path, struct got_object_id *commit_id,
2755 int show_ids, int recurse, const char *root_path,
2756 struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 struct got_object_id *tree_id = NULL;
2760 struct got_tree_object *tree = NULL;
2761 int nentries, i;
2763 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2764 if (err)
2765 goto done;
2767 err = got_object_open_as_tree(&tree, repo, tree_id);
2768 if (err)
2769 goto done;
2770 nentries = got_object_tree_get_nentries(tree);
2771 for (i = 0; i < nentries; i++) {
2772 struct got_tree_entry *te;
2773 char *id = NULL;
2775 if (sigint_received || sigpipe_received)
2776 break;
2778 te = got_object_tree_get_entry(tree, i);
2779 if (show_ids) {
2780 char *id_str;
2781 err = got_object_id_str(&id_str,
2782 got_tree_entry_get_id(te));
2783 if (err)
2784 goto done;
2785 if (asprintf(&id, "%s ", id_str) == -1) {
2786 err = got_error_from_errno("asprintf");
2787 free(id_str);
2788 goto done;
2790 free(id_str);
2792 print_entry(te, id, path, root_path);
2793 free(id);
2795 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2796 char *child_path;
2797 if (asprintf(&child_path, "%s%s%s", path,
2798 path[0] == '/' && path[1] == '\0' ? "" : "/",
2799 got_tree_entry_get_name(te)) == -1) {
2800 err = got_error_from_errno("asprintf");
2801 goto done;
2803 err = print_tree(child_path, commit_id, show_ids, 1,
2804 root_path, repo);
2805 free(child_path);
2806 if (err)
2807 goto done;
2810 done:
2811 if (tree)
2812 got_object_tree_close(tree);
2813 free(tree_id);
2814 return err;
2817 static const struct got_error *
2818 cmd_tree(int argc, char *argv[])
2820 const struct got_error *error;
2821 struct got_repository *repo = NULL;
2822 struct got_worktree *worktree = NULL;
2823 const char *path;
2824 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2825 struct got_object_id *commit_id = NULL;
2826 char *commit_id_str = NULL;
2827 int show_ids = 0, recurse = 0;
2828 int ch;
2830 #ifndef PROFILE
2831 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2832 NULL) == -1)
2833 err(1, "pledge");
2834 #endif
2836 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2837 switch (ch) {
2838 case 'c':
2839 commit_id_str = optarg;
2840 break;
2841 case 'r':
2842 repo_path = realpath(optarg, NULL);
2843 if (repo_path == NULL)
2844 return got_error_from_errno2("realpath",
2845 optarg);
2846 got_path_strip_trailing_slashes(repo_path);
2847 break;
2848 case 'i':
2849 show_ids = 1;
2850 break;
2851 case 'R':
2852 recurse = 1;
2853 break;
2854 default:
2855 usage_tree();
2856 /* NOTREACHED */
2860 argc -= optind;
2861 argv += optind;
2863 if (argc == 1)
2864 path = argv[0];
2865 else if (argc > 1)
2866 usage_tree();
2867 else
2868 path = NULL;
2870 cwd = getcwd(NULL, 0);
2871 if (cwd == NULL) {
2872 error = got_error_from_errno("getcwd");
2873 goto done;
2875 if (repo_path == NULL) {
2876 error = got_worktree_open(&worktree, cwd);
2877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2878 goto done;
2879 else
2880 error = NULL;
2881 if (worktree) {
2882 repo_path =
2883 strdup(got_worktree_get_repo_path(worktree));
2884 if (repo_path == NULL)
2885 error = got_error_from_errno("strdup");
2886 if (error)
2887 goto done;
2888 } else {
2889 repo_path = strdup(cwd);
2890 if (repo_path == NULL) {
2891 error = got_error_from_errno("strdup");
2892 goto done;
2897 error = got_repo_open(&repo, repo_path, NULL);
2898 if (error != NULL)
2899 goto done;
2901 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2902 if (error)
2903 goto done;
2905 if (path == NULL) {
2906 if (worktree) {
2907 char *p, *worktree_subdir = cwd +
2908 strlen(got_worktree_get_root_path(worktree));
2909 if (asprintf(&p, "%s/%s",
2910 got_worktree_get_path_prefix(worktree),
2911 worktree_subdir) == -1) {
2912 error = got_error_from_errno("asprintf");
2913 goto done;
2915 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2916 free(p);
2917 if (error)
2918 goto done;
2919 } else
2920 path = "/";
2922 if (in_repo_path == NULL) {
2923 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2924 if (error != NULL)
2925 goto done;
2928 if (commit_id_str == NULL) {
2929 struct got_reference *head_ref;
2930 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2931 if (error != NULL)
2932 goto done;
2933 error = got_ref_resolve(&commit_id, repo, head_ref);
2934 got_ref_close(head_ref);
2935 if (error != NULL)
2936 goto done;
2937 } else {
2938 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2939 if (error)
2940 goto done;
2943 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 in_repo_path, repo);
2945 done:
2946 free(in_repo_path);
2947 free(repo_path);
2948 free(cwd);
2949 free(commit_id);
2950 if (worktree)
2951 got_worktree_close(worktree);
2952 if (repo) {
2953 const struct got_error *repo_error;
2954 repo_error = got_repo_close(repo);
2955 if (error == NULL)
2956 error = repo_error;
2958 return error;
2961 __dead static void
2962 usage_status(void)
2964 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2965 exit(1);
2968 static const struct got_error *
2969 print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 const char *path, struct got_object_id *blob_id,
2971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2973 if (status == staged_status && (status == GOT_STATUS_DELETE))
2974 status = GOT_STATUS_NO_CHANGE;
2975 printf("%c%c %s\n", status, staged_status, path);
2976 return NULL;
2979 static const struct got_error *
2980 cmd_status(int argc, char *argv[])
2982 const struct got_error *error = NULL;
2983 struct got_repository *repo = NULL;
2984 struct got_worktree *worktree = NULL;
2985 char *cwd = NULL;
2986 struct got_pathlist_head paths;
2987 struct got_pathlist_entry *pe;
2988 int ch;
2990 TAILQ_INIT(&paths);
2992 while ((ch = getopt(argc, argv, "")) != -1) {
2993 switch (ch) {
2994 default:
2995 usage_status();
2996 /* NOTREACHED */
3000 argc -= optind;
3001 argv += optind;
3003 #ifndef PROFILE
3004 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3005 NULL) == -1)
3006 err(1, "pledge");
3007 #endif
3008 cwd = getcwd(NULL, 0);
3009 if (cwd == NULL) {
3010 error = got_error_from_errno("getcwd");
3011 goto done;
3014 error = got_worktree_open(&worktree, cwd);
3015 if (error != NULL)
3016 goto done;
3018 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3019 NULL);
3020 if (error != NULL)
3021 goto done;
3023 error = apply_unveil(got_repo_get_path(repo), 1,
3024 got_worktree_get_root_path(worktree));
3025 if (error)
3026 goto done;
3028 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3029 if (error)
3030 goto done;
3032 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3033 check_cancelled, NULL);
3034 done:
3035 TAILQ_FOREACH(pe, &paths, entry)
3036 free((char *)pe->path);
3037 got_pathlist_free(&paths);
3038 free(cwd);
3039 return error;
3042 __dead static void
3043 usage_ref(void)
3045 fprintf(stderr,
3046 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3047 getprogname());
3048 exit(1);
3051 static const struct got_error *
3052 list_refs(struct got_repository *repo)
3054 static const struct got_error *err = NULL;
3055 struct got_reflist_head refs;
3056 struct got_reflist_entry *re;
3058 SIMPLEQ_INIT(&refs);
3059 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3060 if (err)
3061 return err;
3063 SIMPLEQ_FOREACH(re, &refs, entry) {
3064 char *refstr;
3065 refstr = got_ref_to_str(re->ref);
3066 if (refstr == NULL)
3067 return got_error_from_errno("got_ref_to_str");
3068 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3069 free(refstr);
3072 got_ref_list_free(&refs);
3073 return NULL;
3076 static const struct got_error *
3077 delete_ref(struct got_repository *repo, const char *refname)
3079 const struct got_error *err = NULL;
3080 struct got_reference *ref;
3082 err = got_ref_open(&ref, repo, refname, 0);
3083 if (err)
3084 return err;
3086 err = got_ref_delete(ref, repo);
3087 got_ref_close(ref);
3088 return err;
3091 static const struct got_error *
3092 add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 const struct got_error *err = NULL;
3095 struct got_object_id *id;
3096 struct got_reference *ref = NULL;
3099 * Don't let the user create a reference name with a leading '-'.
3100 * While technically a valid reference name, this case is usually
3101 * an unintended typo.
3103 if (refname[0] == '-')
3104 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3106 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3107 repo);
3108 if (err) {
3109 struct got_reference *target_ref;
3111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3112 return err;
3113 err = got_ref_open(&target_ref, repo, target, 0);
3114 if (err)
3115 return err;
3116 err = got_ref_resolve(&id, repo, target_ref);
3117 got_ref_close(target_ref);
3118 if (err)
3119 return err;
3122 err = got_ref_alloc(&ref, refname, id);
3123 if (err)
3124 goto done;
3126 err = got_ref_write(ref, repo);
3127 done:
3128 if (ref)
3129 got_ref_close(ref);
3130 free(id);
3131 return err;
3134 static const struct got_error *
3135 add_symref(struct got_repository *repo, const char *refname, const char *target)
3137 const struct got_error *err = NULL;
3138 struct got_reference *ref = NULL;
3139 struct got_reference *target_ref = NULL;
3142 * Don't let the user create a reference name with a leading '-'.
3143 * While technically a valid reference name, this case is usually
3144 * an unintended typo.
3146 if (refname[0] == '-')
3147 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3149 err = got_ref_open(&target_ref, repo, target, 0);
3150 if (err)
3151 return err;
3153 err = got_ref_alloc_symref(&ref, refname, target_ref);
3154 if (err)
3155 goto done;
3157 err = got_ref_write(ref, repo);
3158 done:
3159 if (target_ref)
3160 got_ref_close(target_ref);
3161 if (ref)
3162 got_ref_close(ref);
3163 return err;
3166 static const struct got_error *
3167 cmd_ref(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct got_repository *repo = NULL;
3171 struct got_worktree *worktree = NULL;
3172 char *cwd = NULL, *repo_path = NULL;
3173 int ch, do_list = 0, create_symref = 0;
3174 const char *delref = NULL;
3176 /* TODO: Add -s option for adding symbolic references. */
3177 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3178 switch (ch) {
3179 case 'd':
3180 delref = optarg;
3181 break;
3182 case 'r':
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 return got_error_from_errno2("realpath",
3186 optarg);
3187 got_path_strip_trailing_slashes(repo_path);
3188 break;
3189 case 'l':
3190 do_list = 1;
3191 break;
3192 case 's':
3193 create_symref = 1;
3194 break;
3195 default:
3196 usage_ref();
3197 /* NOTREACHED */
3201 if (do_list && delref)
3202 errx(1, "-l and -d options are mutually exclusive\n");
3204 argc -= optind;
3205 argv += optind;
3207 if (do_list || delref) {
3208 if (create_symref)
3209 errx(1, "-s option cannot be used together with the "
3210 "-l or -d options");
3211 if (argc > 0)
3212 usage_ref();
3213 } else if (argc != 2)
3214 usage_ref();
3216 #ifndef PROFILE
3217 if (do_list) {
3218 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3219 NULL) == -1)
3220 err(1, "pledge");
3221 } else {
3222 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3223 "sendfd unveil", NULL) == -1)
3224 err(1, "pledge");
3226 #endif
3227 cwd = getcwd(NULL, 0);
3228 if (cwd == NULL) {
3229 error = got_error_from_errno("getcwd");
3230 goto done;
3233 if (repo_path == NULL) {
3234 error = got_worktree_open(&worktree, cwd);
3235 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3236 goto done;
3237 else
3238 error = NULL;
3239 if (worktree) {
3240 repo_path =
3241 strdup(got_worktree_get_repo_path(worktree));
3242 if (repo_path == NULL)
3243 error = got_error_from_errno("strdup");
3244 if (error)
3245 goto done;
3246 } else {
3247 repo_path = strdup(cwd);
3248 if (repo_path == NULL) {
3249 error = got_error_from_errno("strdup");
3250 goto done;
3255 error = got_repo_open(&repo, repo_path, NULL);
3256 if (error != NULL)
3257 goto done;
3259 error = apply_unveil(got_repo_get_path(repo), do_list,
3260 worktree ? got_worktree_get_root_path(worktree) : NULL);
3261 if (error)
3262 goto done;
3264 if (do_list)
3265 error = list_refs(repo);
3266 else if (delref)
3267 error = delete_ref(repo, delref);
3268 else if (create_symref)
3269 error = add_symref(repo, argv[0], argv[1]);
3270 else
3271 error = add_ref(repo, argv[0], argv[1]);
3272 done:
3273 if (repo)
3274 got_repo_close(repo);
3275 if (worktree)
3276 got_worktree_close(worktree);
3277 free(cwd);
3278 free(repo_path);
3279 return error;
3282 __dead static void
3283 usage_branch(void)
3285 fprintf(stderr,
3286 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3287 "[name]\n", getprogname());
3288 exit(1);
3291 static const struct got_error *
3292 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3293 struct got_reference *ref)
3295 const struct got_error *err = NULL;
3296 const char *refname, *marker = " ";
3297 char *refstr;
3299 refname = got_ref_get_name(ref);
3300 if (worktree && strcmp(refname,
3301 got_worktree_get_head_ref_name(worktree)) == 0) {
3302 struct got_object_id *id = NULL;
3304 err = got_ref_resolve(&id, repo, ref);
3305 if (err)
3306 return err;
3307 if (got_object_id_cmp(id,
3308 got_worktree_get_base_commit_id(worktree)) == 0)
3309 marker = "* ";
3310 else
3311 marker = "~ ";
3312 free(id);
3315 if (strncmp(refname, "refs/heads/", 11) == 0)
3316 refname += 11;
3317 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3318 refname += 18;
3320 refstr = got_ref_to_str(ref);
3321 if (refstr == NULL)
3322 return got_error_from_errno("got_ref_to_str");
3324 printf("%s%s: %s\n", marker, refname, refstr);
3325 free(refstr);
3326 return NULL;
3329 static const struct got_error *
3330 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3332 const char *refname;
3334 if (worktree == NULL)
3335 return got_error(GOT_ERR_NOT_WORKTREE);
3337 refname = got_worktree_get_head_ref_name(worktree);
3339 if (strncmp(refname, "refs/heads/", 11) == 0)
3340 refname += 11;
3341 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3342 refname += 18;
3344 printf("%s\n", refname);
3346 return NULL;
3349 static const struct got_error *
3350 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3352 static const struct got_error *err = NULL;
3353 struct got_reflist_head refs;
3354 struct got_reflist_entry *re;
3355 struct got_reference *temp_ref = NULL;
3356 int rebase_in_progress, histedit_in_progress;
3358 SIMPLEQ_INIT(&refs);
3360 if (worktree) {
3361 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3362 worktree);
3363 if (err)
3364 return err;
3366 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3367 worktree);
3368 if (err)
3369 return err;
3371 if (rebase_in_progress || histedit_in_progress) {
3372 err = got_ref_open(&temp_ref, repo,
3373 got_worktree_get_head_ref_name(worktree), 0);
3374 if (err)
3375 return err;
3376 list_branch(repo, worktree, temp_ref);
3377 got_ref_close(temp_ref);
3381 err = got_ref_list(&refs, repo, "refs/heads",
3382 got_ref_cmp_by_name, NULL);
3383 if (err)
3384 return err;
3386 SIMPLEQ_FOREACH(re, &refs, entry)
3387 list_branch(repo, worktree, re->ref);
3389 got_ref_list_free(&refs);
3390 return NULL;
3393 static const struct got_error *
3394 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3395 const char *branch_name)
3397 const struct got_error *err = NULL;
3398 struct got_reference *ref = NULL;
3399 char *refname;
3401 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3402 return got_error_from_errno("asprintf");
3404 err = got_ref_open(&ref, repo, refname, 0);
3405 if (err)
3406 goto done;
3408 if (worktree &&
3409 strcmp(got_worktree_get_head_ref_name(worktree),
3410 got_ref_get_name(ref)) == 0) {
3411 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3412 "will not delete this work tree's current branch");
3413 goto done;
3416 err = got_ref_delete(ref, repo);
3417 done:
3418 if (ref)
3419 got_ref_close(ref);
3420 free(refname);
3421 return err;
3424 static const struct got_error *
3425 add_branch(struct got_repository *repo, const char *branch_name,
3426 struct got_object_id *base_commit_id)
3428 const struct got_error *err = NULL;
3429 struct got_reference *ref = NULL;
3430 char *base_refname = NULL, *refname = NULL;
3433 * Don't let the user create a branch name with a leading '-'.
3434 * While technically a valid reference name, this case is usually
3435 * an unintended typo.
3437 if (branch_name[0] == '-')
3438 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3440 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3441 err = got_error_from_errno("asprintf");
3442 goto done;
3445 err = got_ref_open(&ref, repo, refname, 0);
3446 if (err == NULL) {
3447 err = got_error(GOT_ERR_BRANCH_EXISTS);
3448 goto done;
3449 } else if (err->code != GOT_ERR_NOT_REF)
3450 goto done;
3452 err = got_ref_alloc(&ref, refname, base_commit_id);
3453 if (err)
3454 goto done;
3456 err = got_ref_write(ref, repo);
3457 done:
3458 if (ref)
3459 got_ref_close(ref);
3460 free(base_refname);
3461 free(refname);
3462 return err;
3465 static const struct got_error *
3466 cmd_branch(int argc, char *argv[])
3468 const struct got_error *error = NULL;
3469 struct got_repository *repo = NULL;
3470 struct got_worktree *worktree = NULL;
3471 char *cwd = NULL, *repo_path = NULL;
3472 int ch, do_list = 0, do_show = 0;
3473 const char *delref = NULL, *commit_id_arg = NULL;
3475 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3476 switch (ch) {
3477 case 'c':
3478 commit_id_arg = optarg;
3479 break;
3480 case 'd':
3481 delref = optarg;
3482 break;
3483 case 'r':
3484 repo_path = realpath(optarg, NULL);
3485 if (repo_path == NULL)
3486 return got_error_from_errno2("realpath",
3487 optarg);
3488 got_path_strip_trailing_slashes(repo_path);
3489 break;
3490 case 'l':
3491 do_list = 1;
3492 break;
3493 default:
3494 usage_branch();
3495 /* NOTREACHED */
3499 if (do_list && delref)
3500 errx(1, "-l and -d options are mutually exclusive\n");
3502 argc -= optind;
3503 argv += optind;
3505 if (!do_list && !delref && argc == 0)
3506 do_show = 1;
3508 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3509 errx(1, "-c option can only be used when creating a branch");
3511 if (do_list || delref) {
3512 if (argc > 0)
3513 usage_branch();
3514 } else if (!do_show && argc != 1)
3515 usage_branch();
3517 #ifndef PROFILE
3518 if (do_list || do_show) {
3519 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3520 NULL) == -1)
3521 err(1, "pledge");
3522 } else {
3523 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3524 "sendfd unveil", NULL) == -1)
3525 err(1, "pledge");
3527 #endif
3528 cwd = getcwd(NULL, 0);
3529 if (cwd == NULL) {
3530 error = got_error_from_errno("getcwd");
3531 goto done;
3534 if (repo_path == NULL) {
3535 error = got_worktree_open(&worktree, cwd);
3536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3537 goto done;
3538 else
3539 error = NULL;
3540 if (worktree) {
3541 repo_path =
3542 strdup(got_worktree_get_repo_path(worktree));
3543 if (repo_path == NULL)
3544 error = got_error_from_errno("strdup");
3545 if (error)
3546 goto done;
3547 } else {
3548 repo_path = strdup(cwd);
3549 if (repo_path == NULL) {
3550 error = got_error_from_errno("strdup");
3551 goto done;
3556 error = got_repo_open(&repo, repo_path, NULL);
3557 if (error != NULL)
3558 goto done;
3560 error = apply_unveil(got_repo_get_path(repo), do_list,
3561 worktree ? got_worktree_get_root_path(worktree) : NULL);
3562 if (error)
3563 goto done;
3565 if (do_show)
3566 error = show_current_branch(repo, worktree);
3567 else if (do_list)
3568 error = list_branches(repo, worktree);
3569 else if (delref)
3570 error = delete_branch(repo, worktree, delref);
3571 else {
3572 struct got_object_id *commit_id;
3573 if (commit_id_arg == NULL)
3574 commit_id_arg = worktree ?
3575 got_worktree_get_head_ref_name(worktree) :
3576 GOT_REF_HEAD;
3577 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3578 if (error)
3579 goto done;
3580 error = add_branch(repo, argv[0], commit_id);
3581 free(commit_id);
3583 done:
3584 if (repo)
3585 got_repo_close(repo);
3586 if (worktree)
3587 got_worktree_close(worktree);
3588 free(cwd);
3589 free(repo_path);
3590 return error;
3594 __dead static void
3595 usage_tag(void)
3597 fprintf(stderr,
3598 "usage: %s tag [-r repository] | -l | "
3599 "[-m message] name [commit]\n", getprogname());
3600 exit(1);
3603 #if 0
3604 static const struct got_error *
3605 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3607 const struct got_error *err = NULL;
3608 struct got_reflist_entry *re, *se, *new;
3609 struct got_object_id *re_id, *se_id;
3610 struct got_tag_object *re_tag, *se_tag;
3611 time_t re_time, se_time;
3613 SIMPLEQ_FOREACH(re, tags, entry) {
3614 se = SIMPLEQ_FIRST(sorted);
3615 if (se == NULL) {
3616 err = got_reflist_entry_dup(&new, re);
3617 if (err)
3618 return err;
3619 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3620 continue;
3621 } else {
3622 err = got_ref_resolve(&re_id, repo, re->ref);
3623 if (err)
3624 break;
3625 err = got_object_open_as_tag(&re_tag, repo, re_id);
3626 free(re_id);
3627 if (err)
3628 break;
3629 re_time = got_object_tag_get_tagger_time(re_tag);
3630 got_object_tag_close(re_tag);
3633 while (se) {
3634 err = got_ref_resolve(&se_id, repo, re->ref);
3635 if (err)
3636 break;
3637 err = got_object_open_as_tag(&se_tag, repo, se_id);
3638 free(se_id);
3639 if (err)
3640 break;
3641 se_time = got_object_tag_get_tagger_time(se_tag);
3642 got_object_tag_close(se_tag);
3644 if (se_time > re_time) {
3645 err = got_reflist_entry_dup(&new, re);
3646 if (err)
3647 return err;
3648 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3649 break;
3651 se = SIMPLEQ_NEXT(se, entry);
3652 continue;
3655 done:
3656 return err;
3658 #endif
3660 static const struct got_error *
3661 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3662 struct got_reference *ref2)
3664 const struct got_error *err = NULL;
3665 struct got_repository *repo = arg;
3666 struct got_object_id *id1, *id2 = NULL;
3667 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3668 time_t time1, time2;
3670 *cmp = 0;
3672 err = got_ref_resolve(&id1, repo, ref1);
3673 if (err)
3674 return err;
3675 err = got_object_open_as_tag(&tag1, repo, id1);
3676 if (err)
3677 goto done;
3679 err = got_ref_resolve(&id2, repo, ref2);
3680 if (err)
3681 goto done;
3682 err = got_object_open_as_tag(&tag2, repo, id2);
3683 if (err)
3684 goto done;
3686 time1 = got_object_tag_get_tagger_time(tag1);
3687 time2 = got_object_tag_get_tagger_time(tag2);
3689 /* Put latest tags first. */
3690 if (time1 < time2)
3691 *cmp = 1;
3692 else if (time1 > time2)
3693 *cmp = -1;
3694 else
3695 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3696 done:
3697 free(id1);
3698 free(id2);
3699 if (tag1)
3700 got_object_tag_close(tag1);
3701 if (tag2)
3702 got_object_tag_close(tag2);
3703 return err;
3706 static const struct got_error *
3707 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3709 static const struct got_error *err = NULL;
3710 struct got_reflist_head refs;
3711 struct got_reflist_entry *re;
3713 SIMPLEQ_INIT(&refs);
3715 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3716 if (err)
3717 return err;
3719 SIMPLEQ_FOREACH(re, &refs, entry) {
3720 const char *refname;
3721 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3722 char datebuf[26];
3723 time_t tagger_time;
3724 struct got_object_id *id;
3725 struct got_tag_object *tag;
3727 refname = got_ref_get_name(re->ref);
3728 if (strncmp(refname, "refs/tags/", 10) != 0)
3729 continue;
3730 refname += 10;
3731 refstr = got_ref_to_str(re->ref);
3732 if (refstr == NULL) {
3733 err = got_error_from_errno("got_ref_to_str");
3734 break;
3736 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3737 free(refstr);
3739 err = got_ref_resolve(&id, repo, re->ref);
3740 if (err)
3741 break;
3742 err = got_object_open_as_tag(&tag, repo, id);
3743 free(id);
3744 if (err)
3745 break;
3746 printf("from: %s\n", got_object_tag_get_tagger(tag));
3747 tagger_time = got_object_tag_get_tagger_time(tag);
3748 datestr = get_datestr(&tagger_time, datebuf);
3749 if (datestr)
3750 printf("date: %s UTC\n", datestr);
3751 err = got_object_id_str(&id_str,
3752 got_object_tag_get_object_id(tag));
3753 if (err)
3754 break;
3755 switch (got_object_tag_get_object_type(tag)) {
3756 case GOT_OBJ_TYPE_BLOB:
3757 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3758 break;
3759 case GOT_OBJ_TYPE_TREE:
3760 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3761 break;
3762 case GOT_OBJ_TYPE_COMMIT:
3763 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3764 break;
3765 case GOT_OBJ_TYPE_TAG:
3766 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3767 break;
3768 default:
3769 break;
3771 free(id_str);
3772 tagmsg0 = strdup(got_object_tag_get_message(tag));
3773 got_object_tag_close(tag);
3774 if (tagmsg0 == NULL) {
3775 err = got_error_from_errno("strdup");
3776 break;
3779 tagmsg = tagmsg0;
3780 do {
3781 line = strsep(&tagmsg, "\n");
3782 if (line)
3783 printf(" %s\n", line);
3784 } while (line);
3785 free(tagmsg0);
3788 got_ref_list_free(&refs);
3789 return NULL;
3792 static const struct got_error *
3793 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3794 const char *tag_name, const char *repo_path)
3796 const struct got_error *err = NULL;
3797 char *template = NULL, *initial_content = NULL;
3798 char *editor = NULL;
3799 int fd = -1;
3801 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3802 err = got_error_from_errno("asprintf");
3803 goto done;
3806 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3807 commit_id_str, tag_name) == -1) {
3808 err = got_error_from_errno("asprintf");
3809 goto done;
3812 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3813 if (err)
3814 goto done;
3816 dprintf(fd, initial_content);
3817 close(fd);
3819 err = get_editor(&editor);
3820 if (err)
3821 goto done;
3822 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3823 done:
3824 free(initial_content);
3825 free(template);
3826 free(editor);
3828 /* Editor is done; we can now apply unveil(2) */
3829 if (err == NULL) {
3830 err = apply_unveil(repo_path, 0, NULL);
3831 if (err) {
3832 free(*tagmsg);
3833 *tagmsg = NULL;
3836 return err;
3839 static const struct got_error *
3840 add_tag(struct got_repository *repo, const char *tag_name,
3841 const char *commit_arg, const char *tagmsg_arg)
3843 const struct got_error *err = NULL;
3844 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3845 char *label = NULL, *commit_id_str = NULL;
3846 struct got_reference *ref = NULL;
3847 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3848 char *tagmsg_path = NULL, *tag_id_str = NULL;
3849 int preserve_tagmsg = 0;
3852 * Don't let the user create a tag name with a leading '-'.
3853 * While technically a valid reference name, this case is usually
3854 * an unintended typo.
3856 if (tag_name[0] == '-')
3857 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3859 err = get_author(&tagger, repo);
3860 if (err)
3861 return err;
3863 err = match_object_id(&commit_id, &label, commit_arg,
3864 GOT_OBJ_TYPE_COMMIT, 1, repo);
3865 if (err)
3866 goto done;
3868 err = got_object_id_str(&commit_id_str, commit_id);
3869 if (err)
3870 goto done;
3872 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3873 refname = strdup(tag_name);
3874 if (refname == NULL) {
3875 err = got_error_from_errno("strdup");
3876 goto done;
3878 tag_name += 10;
3879 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3880 err = got_error_from_errno("asprintf");
3881 goto done;
3884 err = got_ref_open(&ref, repo, refname, 0);
3885 if (err == NULL) {
3886 err = got_error(GOT_ERR_TAG_EXISTS);
3887 goto done;
3888 } else if (err->code != GOT_ERR_NOT_REF)
3889 goto done;
3891 if (tagmsg_arg == NULL) {
3892 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3893 tag_name, got_repo_get_path(repo));
3894 if (err) {
3895 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3896 tagmsg_path != NULL)
3897 preserve_tagmsg = 1;
3898 goto done;
3902 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3903 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3904 if (err) {
3905 if (tagmsg_path)
3906 preserve_tagmsg = 1;
3907 goto done;
3910 err = got_ref_alloc(&ref, refname, tag_id);
3911 if (err) {
3912 if (tagmsg_path)
3913 preserve_tagmsg = 1;
3914 goto done;
3917 err = got_ref_write(ref, repo);
3918 if (err) {
3919 if (tagmsg_path)
3920 preserve_tagmsg = 1;
3921 goto done;
3924 err = got_object_id_str(&tag_id_str, tag_id);
3925 if (err) {
3926 if (tagmsg_path)
3927 preserve_tagmsg = 1;
3928 goto done;
3930 printf("Created tag %s\n", tag_id_str);
3931 done:
3932 if (preserve_tagmsg) {
3933 fprintf(stderr, "%s: tag message preserved in %s\n",
3934 getprogname(), tagmsg_path);
3935 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3936 err = got_error_from_errno2("unlink", tagmsg_path);
3937 free(tag_id_str);
3938 if (ref)
3939 got_ref_close(ref);
3940 free(commit_id);
3941 free(commit_id_str);
3942 free(refname);
3943 free(tagmsg);
3944 free(tagmsg_path);
3945 free(tagger);
3946 return err;
3949 static const struct got_error *
3950 cmd_tag(int argc, char *argv[])
3952 const struct got_error *error = NULL;
3953 struct got_repository *repo = NULL;
3954 struct got_worktree *worktree = NULL;
3955 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3956 char *gitconfig_path = NULL;
3957 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3958 int ch, do_list = 0;
3960 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3961 switch (ch) {
3962 case 'm':
3963 tagmsg = optarg;
3964 break;
3965 case 'r':
3966 repo_path = realpath(optarg, NULL);
3967 if (repo_path == NULL)
3968 return got_error_from_errno2("realpath",
3969 optarg);
3970 got_path_strip_trailing_slashes(repo_path);
3971 break;
3972 case 'l':
3973 do_list = 1;
3974 break;
3975 default:
3976 usage_tag();
3977 /* NOTREACHED */
3981 argc -= optind;
3982 argv += optind;
3984 if (do_list) {
3985 if (tagmsg)
3986 errx(1, "-l and -m options are mutually exclusive\n");
3987 if (argc > 0)
3988 usage_tag();
3989 } else if (argc < 1 || argc > 2)
3990 usage_tag();
3991 else if (argc > 1)
3992 commit_id_arg = argv[1];
3993 tag_name = argv[0];
3995 #ifndef PROFILE
3996 if (do_list) {
3997 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3998 NULL) == -1)
3999 err(1, "pledge");
4000 } else {
4001 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4002 "sendfd unveil", NULL) == -1)
4003 err(1, "pledge");
4005 #endif
4006 cwd = getcwd(NULL, 0);
4007 if (cwd == NULL) {
4008 error = got_error_from_errno("getcwd");
4009 goto done;
4012 if (repo_path == NULL) {
4013 error = got_worktree_open(&worktree, cwd);
4014 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4015 goto done;
4016 else
4017 error = NULL;
4018 if (worktree) {
4019 repo_path =
4020 strdup(got_worktree_get_repo_path(worktree));
4021 if (repo_path == NULL)
4022 error = got_error_from_errno("strdup");
4023 if (error)
4024 goto done;
4025 } else {
4026 repo_path = strdup(cwd);
4027 if (repo_path == NULL) {
4028 error = got_error_from_errno("strdup");
4029 goto done;
4034 if (do_list) {
4035 error = got_repo_open(&repo, repo_path, NULL);
4036 if (error != NULL)
4037 goto done;
4038 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4039 if (error)
4040 goto done;
4041 error = list_tags(repo, worktree);
4042 } else {
4043 error = get_gitconfig_path(&gitconfig_path);
4044 if (error)
4045 goto done;
4046 error = got_repo_open(&repo, repo_path, gitconfig_path);
4047 if (error != NULL)
4048 goto done;
4050 if (tagmsg) {
4051 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4052 if (error)
4053 goto done;
4056 if (commit_id_arg == NULL) {
4057 struct got_reference *head_ref;
4058 struct got_object_id *commit_id;
4059 error = got_ref_open(&head_ref, repo,
4060 worktree ? got_worktree_get_head_ref_name(worktree)
4061 : GOT_REF_HEAD, 0);
4062 if (error)
4063 goto done;
4064 error = got_ref_resolve(&commit_id, repo, head_ref);
4065 got_ref_close(head_ref);
4066 if (error)
4067 goto done;
4068 error = got_object_id_str(&commit_id_str, commit_id);
4069 free(commit_id);
4070 if (error)
4071 goto done;
4074 error = add_tag(repo, tag_name,
4075 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4077 done:
4078 if (repo)
4079 got_repo_close(repo);
4080 if (worktree)
4081 got_worktree_close(worktree);
4082 free(cwd);
4083 free(repo_path);
4084 free(gitconfig_path);
4085 free(commit_id_str);
4086 return error;
4089 __dead static void
4090 usage_add(void)
4092 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4093 exit(1);
4096 static const struct got_error *
4097 add_progress(void *arg, unsigned char status, const char *path)
4099 while (path[0] == '/')
4100 path++;
4101 printf("%c %s\n", status, path);
4102 return NULL;
4105 static const struct got_error *
4106 cmd_add(int argc, char *argv[])
4108 const struct got_error *error = NULL;
4109 struct got_repository *repo = NULL;
4110 struct got_worktree *worktree = NULL;
4111 char *cwd = NULL;
4112 struct got_pathlist_head paths;
4113 struct got_pathlist_entry *pe;
4114 int ch, can_recurse = 0;
4116 TAILQ_INIT(&paths);
4118 while ((ch = getopt(argc, argv, "R")) != -1) {
4119 switch (ch) {
4120 case 'R':
4121 can_recurse = 1;
4122 break;
4123 default:
4124 usage_add();
4125 /* NOTREACHED */
4129 argc -= optind;
4130 argv += optind;
4132 #ifndef PROFILE
4133 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4134 NULL) == -1)
4135 err(1, "pledge");
4136 #endif
4137 if (argc < 1)
4138 usage_add();
4140 cwd = getcwd(NULL, 0);
4141 if (cwd == NULL) {
4142 error = got_error_from_errno("getcwd");
4143 goto done;
4146 error = got_worktree_open(&worktree, cwd);
4147 if (error)
4148 goto done;
4150 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4151 NULL);
4152 if (error != NULL)
4153 goto done;
4155 error = apply_unveil(got_repo_get_path(repo), 1,
4156 got_worktree_get_root_path(worktree));
4157 if (error)
4158 goto done;
4160 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4161 if (error)
4162 goto done;
4164 if (!can_recurse) {
4165 char *ondisk_path;
4166 struct stat sb;
4167 TAILQ_FOREACH(pe, &paths, entry) {
4168 if (asprintf(&ondisk_path, "%s/%s",
4169 got_worktree_get_root_path(worktree),
4170 pe->path) == -1) {
4171 error = got_error_from_errno("asprintf");
4172 goto done;
4174 if (lstat(ondisk_path, &sb) == -1) {
4175 if (errno == ENOENT) {
4176 free(ondisk_path);
4177 continue;
4179 error = got_error_from_errno2("lstat",
4180 ondisk_path);
4181 free(ondisk_path);
4182 goto done;
4184 free(ondisk_path);
4185 if (S_ISDIR(sb.st_mode)) {
4186 error = got_error_msg(GOT_ERR_BAD_PATH,
4187 "adding directories requires -R option");
4188 goto done;
4192 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4193 NULL, repo);
4194 done:
4195 if (repo)
4196 got_repo_close(repo);
4197 if (worktree)
4198 got_worktree_close(worktree);
4199 TAILQ_FOREACH(pe, &paths, entry)
4200 free((char *)pe->path);
4201 got_pathlist_free(&paths);
4202 free(cwd);
4203 return error;
4206 __dead static void
4207 usage_remove(void)
4209 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4210 exit(1);
4213 static const struct got_error *
4214 print_remove_status(void *arg, unsigned char status,
4215 unsigned char staged_status, const char *path,
4216 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4217 struct got_object_id *commit_id)
4219 if (status == GOT_STATUS_NONEXISTENT)
4220 return NULL;
4221 if (status == staged_status && (status == GOT_STATUS_DELETE))
4222 status = GOT_STATUS_NO_CHANGE;
4223 printf("%c%c %s\n", status, staged_status, path);
4224 return NULL;
4227 static const struct got_error *
4228 cmd_remove(int argc, char *argv[])
4230 const struct got_error *error = NULL;
4231 struct got_worktree *worktree = NULL;
4232 struct got_repository *repo = NULL;
4233 char *cwd = NULL;
4234 struct got_pathlist_head paths;
4235 struct got_pathlist_entry *pe;
4236 int ch, delete_local_mods = 0;
4238 TAILQ_INIT(&paths);
4240 while ((ch = getopt(argc, argv, "f")) != -1) {
4241 switch (ch) {
4242 case 'f':
4243 delete_local_mods = 1;
4244 break;
4245 default:
4246 usage_add();
4247 /* NOTREACHED */
4251 argc -= optind;
4252 argv += optind;
4254 #ifndef PROFILE
4255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4256 NULL) == -1)
4257 err(1, "pledge");
4258 #endif
4259 if (argc < 1)
4260 usage_remove();
4262 cwd = getcwd(NULL, 0);
4263 if (cwd == NULL) {
4264 error = got_error_from_errno("getcwd");
4265 goto done;
4267 error = got_worktree_open(&worktree, cwd);
4268 if (error)
4269 goto done;
4271 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4272 NULL);
4273 if (error)
4274 goto done;
4276 error = apply_unveil(got_repo_get_path(repo), 1,
4277 got_worktree_get_root_path(worktree));
4278 if (error)
4279 goto done;
4281 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4282 if (error)
4283 goto done;
4285 error = got_worktree_schedule_delete(worktree, &paths,
4286 delete_local_mods, print_remove_status, NULL, repo);
4287 if (error)
4288 goto done;
4289 done:
4290 if (repo)
4291 got_repo_close(repo);
4292 if (worktree)
4293 got_worktree_close(worktree);
4294 TAILQ_FOREACH(pe, &paths, entry)
4295 free((char *)pe->path);
4296 got_pathlist_free(&paths);
4297 free(cwd);
4298 return error;
4301 __dead static void
4302 usage_revert(void)
4304 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4305 "path ...\n", getprogname());
4306 exit(1);
4309 static const struct got_error *
4310 revert_progress(void *arg, unsigned char status, const char *path)
4312 while (path[0] == '/')
4313 path++;
4314 printf("%c %s\n", status, path);
4315 return NULL;
4318 struct choose_patch_arg {
4319 FILE *patch_script_file;
4320 const char *action;
4323 static const struct got_error *
4324 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4325 int nchanges, const char *action)
4327 char *line = NULL;
4328 size_t linesize = 0;
4329 ssize_t linelen;
4331 switch (status) {
4332 case GOT_STATUS_ADD:
4333 printf("A %s\n%s this addition? [y/n] ", path, action);
4334 break;
4335 case GOT_STATUS_DELETE:
4336 printf("D %s\n%s this deletion? [y/n] ", path, action);
4337 break;
4338 case GOT_STATUS_MODIFY:
4339 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4340 return got_error_from_errno("fseek");
4341 printf(GOT_COMMIT_SEP_STR);
4342 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4343 printf("%s", line);
4344 if (ferror(patch_file))
4345 return got_error_from_errno("getline");
4346 printf(GOT_COMMIT_SEP_STR);
4347 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4348 path, n, nchanges, action);
4349 break;
4350 default:
4351 return got_error_path(path, GOT_ERR_FILE_STATUS);
4354 return NULL;
4357 static const struct got_error *
4358 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4359 FILE *patch_file, int n, int nchanges)
4361 const struct got_error *err = NULL;
4362 char *line = NULL;
4363 size_t linesize = 0;
4364 ssize_t linelen;
4365 int resp = ' ';
4366 struct choose_patch_arg *a = arg;
4368 *choice = GOT_PATCH_CHOICE_NONE;
4370 if (a->patch_script_file) {
4371 char *nl;
4372 err = show_change(status, path, patch_file, n, nchanges,
4373 a->action);
4374 if (err)
4375 return err;
4376 linelen = getline(&line, &linesize, a->patch_script_file);
4377 if (linelen == -1) {
4378 if (ferror(a->patch_script_file))
4379 return got_error_from_errno("getline");
4380 return NULL;
4382 nl = strchr(line, '\n');
4383 if (nl)
4384 *nl = '\0';
4385 if (strcmp(line, "y") == 0) {
4386 *choice = GOT_PATCH_CHOICE_YES;
4387 printf("y\n");
4388 } else if (strcmp(line, "n") == 0) {
4389 *choice = GOT_PATCH_CHOICE_NO;
4390 printf("n\n");
4391 } else if (strcmp(line, "q") == 0 &&
4392 status == GOT_STATUS_MODIFY) {
4393 *choice = GOT_PATCH_CHOICE_QUIT;
4394 printf("q\n");
4395 } else
4396 printf("invalid response '%s'\n", line);
4397 free(line);
4398 return NULL;
4401 while (resp != 'y' && resp != 'n' && resp != 'q') {
4402 err = show_change(status, path, patch_file, n, nchanges,
4403 a->action);
4404 if (err)
4405 return err;
4406 resp = getchar();
4407 if (resp == '\n')
4408 resp = getchar();
4409 if (status == GOT_STATUS_MODIFY) {
4410 if (resp != 'y' && resp != 'n' && resp != 'q') {
4411 printf("invalid response '%c'\n", resp);
4412 resp = ' ';
4414 } else if (resp != 'y' && resp != 'n') {
4415 printf("invalid response '%c'\n", resp);
4416 resp = ' ';
4420 if (resp == 'y')
4421 *choice = GOT_PATCH_CHOICE_YES;
4422 else if (resp == 'n')
4423 *choice = GOT_PATCH_CHOICE_NO;
4424 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4425 *choice = GOT_PATCH_CHOICE_QUIT;
4427 return NULL;
4431 static const struct got_error *
4432 cmd_revert(int argc, char *argv[])
4434 const struct got_error *error = NULL;
4435 struct got_worktree *worktree = NULL;
4436 struct got_repository *repo = NULL;
4437 char *cwd = NULL, *path = NULL;
4438 struct got_pathlist_head paths;
4439 struct got_pathlist_entry *pe;
4440 int ch, can_recurse = 0, pflag = 0;
4441 FILE *patch_script_file = NULL;
4442 const char *patch_script_path = NULL;
4443 struct choose_patch_arg cpa;
4445 TAILQ_INIT(&paths);
4447 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4448 switch (ch) {
4449 case 'p':
4450 pflag = 1;
4451 break;
4452 case 'F':
4453 patch_script_path = optarg;
4454 break;
4455 case 'R':
4456 can_recurse = 1;
4457 break;
4458 default:
4459 usage_revert();
4460 /* NOTREACHED */
4464 argc -= optind;
4465 argv += optind;
4467 #ifndef PROFILE
4468 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4469 "unveil", NULL) == -1)
4470 err(1, "pledge");
4471 #endif
4472 if (argc < 1)
4473 usage_revert();
4474 if (patch_script_path && !pflag)
4475 errx(1, "-F option can only be used together with -p option");
4477 cwd = getcwd(NULL, 0);
4478 if (cwd == NULL) {
4479 error = got_error_from_errno("getcwd");
4480 goto done;
4482 error = got_worktree_open(&worktree, cwd);
4483 if (error)
4484 goto done;
4486 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4487 NULL);
4488 if (error != NULL)
4489 goto done;
4491 if (patch_script_path) {
4492 patch_script_file = fopen(patch_script_path, "r");
4493 if (patch_script_file == NULL) {
4494 error = got_error_from_errno2("fopen",
4495 patch_script_path);
4496 goto done;
4499 error = apply_unveil(got_repo_get_path(repo), 1,
4500 got_worktree_get_root_path(worktree));
4501 if (error)
4502 goto done;
4504 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4505 if (error)
4506 goto done;
4508 if (!can_recurse) {
4509 char *ondisk_path;
4510 struct stat sb;
4511 TAILQ_FOREACH(pe, &paths, entry) {
4512 if (asprintf(&ondisk_path, "%s/%s",
4513 got_worktree_get_root_path(worktree),
4514 pe->path) == -1) {
4515 error = got_error_from_errno("asprintf");
4516 goto done;
4518 if (lstat(ondisk_path, &sb) == -1) {
4519 if (errno == ENOENT) {
4520 free(ondisk_path);
4521 continue;
4523 error = got_error_from_errno2("lstat",
4524 ondisk_path);
4525 free(ondisk_path);
4526 goto done;
4528 free(ondisk_path);
4529 if (S_ISDIR(sb.st_mode)) {
4530 error = got_error_msg(GOT_ERR_BAD_PATH,
4531 "reverting directories requires -R option");
4532 goto done;
4537 cpa.patch_script_file = patch_script_file;
4538 cpa.action = "revert";
4539 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4540 pflag ? choose_patch : NULL, &cpa, repo);
4541 if (error)
4542 goto done;
4543 done:
4544 if (patch_script_file && fclose(patch_script_file) == EOF &&
4545 error == NULL)
4546 error = got_error_from_errno2("fclose", patch_script_path);
4547 if (repo)
4548 got_repo_close(repo);
4549 if (worktree)
4550 got_worktree_close(worktree);
4551 free(path);
4552 free(cwd);
4553 return error;
4556 __dead static void
4557 usage_commit(void)
4559 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4560 getprogname());
4561 exit(1);
4564 struct collect_commit_logmsg_arg {
4565 const char *cmdline_log;
4566 const char *editor;
4567 const char *worktree_path;
4568 const char *branch_name;
4569 const char *repo_path;
4570 char *logmsg_path;
4574 static const struct got_error *
4575 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4576 void *arg)
4578 char *initial_content = NULL;
4579 struct got_pathlist_entry *pe;
4580 const struct got_error *err = NULL;
4581 char *template = NULL;
4582 struct collect_commit_logmsg_arg *a = arg;
4583 int fd;
4584 size_t len;
4586 /* if a message was specified on the command line, just use it */
4587 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4588 len = strlen(a->cmdline_log) + 1;
4589 *logmsg = malloc(len + 1);
4590 if (*logmsg == NULL)
4591 return got_error_from_errno("malloc");
4592 strlcpy(*logmsg, a->cmdline_log, len);
4593 return NULL;
4596 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4597 return got_error_from_errno("asprintf");
4599 if (asprintf(&initial_content,
4600 "\n# changes to be committed on branch %s:\n",
4601 a->branch_name) == -1)
4602 return got_error_from_errno("asprintf");
4604 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4605 if (err)
4606 goto done;
4608 dprintf(fd, initial_content);
4610 TAILQ_FOREACH(pe, commitable_paths, entry) {
4611 struct got_commitable *ct = pe->data;
4612 dprintf(fd, "# %c %s\n",
4613 got_commitable_get_status(ct),
4614 got_commitable_get_path(ct));
4616 close(fd);
4618 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4619 done:
4620 free(initial_content);
4621 free(template);
4623 /* Editor is done; we can now apply unveil(2) */
4624 if (err == NULL) {
4625 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4626 if (err) {
4627 free(*logmsg);
4628 *logmsg = NULL;
4631 return err;
4634 static const struct got_error *
4635 cmd_commit(int argc, char *argv[])
4637 const struct got_error *error = NULL;
4638 struct got_worktree *worktree = NULL;
4639 struct got_repository *repo = NULL;
4640 char *cwd = NULL, *id_str = NULL;
4641 struct got_object_id *id = NULL;
4642 const char *logmsg = NULL;
4643 struct collect_commit_logmsg_arg cl_arg;
4644 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4645 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4646 struct got_pathlist_head paths;
4648 TAILQ_INIT(&paths);
4649 cl_arg.logmsg_path = NULL;
4651 while ((ch = getopt(argc, argv, "m:")) != -1) {
4652 switch (ch) {
4653 case 'm':
4654 logmsg = optarg;
4655 break;
4656 default:
4657 usage_commit();
4658 /* NOTREACHED */
4662 argc -= optind;
4663 argv += optind;
4665 #ifndef PROFILE
4666 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4667 "unveil", NULL) == -1)
4668 err(1, "pledge");
4669 #endif
4670 cwd = getcwd(NULL, 0);
4671 if (cwd == NULL) {
4672 error = got_error_from_errno("getcwd");
4673 goto done;
4675 error = got_worktree_open(&worktree, cwd);
4676 if (error)
4677 goto done;
4679 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4680 if (error)
4681 goto done;
4682 if (rebase_in_progress) {
4683 error = got_error(GOT_ERR_REBASING);
4684 goto done;
4687 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4688 worktree);
4689 if (error)
4690 goto done;
4692 error = get_gitconfig_path(&gitconfig_path);
4693 if (error)
4694 goto done;
4695 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4696 gitconfig_path);
4697 if (error != NULL)
4698 goto done;
4700 error = get_author(&author, repo);
4701 if (error)
4702 return error;
4705 * unveil(2) traverses exec(2); if an editor is used we have
4706 * to apply unveil after the log message has been written.
4708 if (logmsg == NULL || strlen(logmsg) == 0)
4709 error = get_editor(&editor);
4710 else
4711 error = apply_unveil(got_repo_get_path(repo), 0,
4712 got_worktree_get_root_path(worktree));
4713 if (error)
4714 goto done;
4716 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4717 if (error)
4718 goto done;
4720 cl_arg.editor = editor;
4721 cl_arg.cmdline_log = logmsg;
4722 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4723 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4724 if (!histedit_in_progress) {
4725 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4726 error = got_error(GOT_ERR_COMMIT_BRANCH);
4727 goto done;
4729 cl_arg.branch_name += 11;
4731 cl_arg.repo_path = got_repo_get_path(repo);
4732 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4733 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4734 if (error) {
4735 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4736 cl_arg.logmsg_path != NULL)
4737 preserve_logmsg = 1;
4738 goto done;
4741 error = got_object_id_str(&id_str, id);
4742 if (error)
4743 goto done;
4744 printf("Created commit %s\n", id_str);
4745 done:
4746 if (preserve_logmsg) {
4747 fprintf(stderr, "%s: log message preserved in %s\n",
4748 getprogname(), cl_arg.logmsg_path);
4749 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4750 error == NULL)
4751 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4752 free(cl_arg.logmsg_path);
4753 if (repo)
4754 got_repo_close(repo);
4755 if (worktree)
4756 got_worktree_close(worktree);
4757 free(cwd);
4758 free(id_str);
4759 free(gitconfig_path);
4760 free(editor);
4761 free(author);
4762 return error;
4765 __dead static void
4766 usage_cherrypick(void)
4768 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4769 exit(1);
4772 static const struct got_error *
4773 cmd_cherrypick(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *commit_id_str = NULL;
4779 struct got_object_id *commit_id = NULL;
4780 struct got_commit_object *commit = NULL;
4781 struct got_object_qid *pid;
4782 struct got_reference *head_ref = NULL;
4783 int ch, did_something = 0;
4785 while ((ch = getopt(argc, argv, "")) != -1) {
4786 switch (ch) {
4787 default:
4788 usage_cherrypick();
4789 /* NOTREACHED */
4793 argc -= optind;
4794 argv += optind;
4796 #ifndef PROFILE
4797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 "unveil", NULL) == -1)
4799 err(1, "pledge");
4800 #endif
4801 if (argc != 1)
4802 usage_cherrypick();
4804 cwd = getcwd(NULL, 0);
4805 if (cwd == NULL) {
4806 error = got_error_from_errno("getcwd");
4807 goto done;
4809 error = got_worktree_open(&worktree, cwd);
4810 if (error)
4811 goto done;
4813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4814 NULL);
4815 if (error != NULL)
4816 goto done;
4818 error = apply_unveil(got_repo_get_path(repo), 0,
4819 got_worktree_get_root_path(worktree));
4820 if (error)
4821 goto done;
4823 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 GOT_OBJ_TYPE_COMMIT, repo);
4825 if (error != NULL) {
4826 struct got_reference *ref;
4827 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4828 goto done;
4829 error = got_ref_open(&ref, repo, argv[0], 0);
4830 if (error != NULL)
4831 goto done;
4832 error = got_ref_resolve(&commit_id, repo, ref);
4833 got_ref_close(ref);
4834 if (error != NULL)
4835 goto done;
4837 error = got_object_id_str(&commit_id_str, commit_id);
4838 if (error)
4839 goto done;
4841 error = got_ref_open(&head_ref, repo,
4842 got_worktree_get_head_ref_name(worktree), 0);
4843 if (error != NULL)
4844 goto done;
4846 error = check_same_branch(commit_id, head_ref, NULL, repo);
4847 if (error) {
4848 if (error->code != GOT_ERR_ANCESTRY)
4849 goto done;
4850 error = NULL;
4851 } else {
4852 error = got_error(GOT_ERR_SAME_BRANCH);
4853 goto done;
4856 error = got_object_open_as_commit(&commit, repo, commit_id);
4857 if (error)
4858 goto done;
4859 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4860 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4861 commit_id, repo, update_progress, &did_something, check_cancelled,
4862 NULL);
4863 if (error != NULL)
4864 goto done;
4866 if (did_something)
4867 printf("Merged commit %s\n", commit_id_str);
4868 done:
4869 if (commit)
4870 got_object_commit_close(commit);
4871 free(commit_id_str);
4872 if (head_ref)
4873 got_ref_close(head_ref);
4874 if (worktree)
4875 got_worktree_close(worktree);
4876 if (repo)
4877 got_repo_close(repo);
4878 return error;
4881 __dead static void
4882 usage_backout(void)
4884 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4885 exit(1);
4888 static const struct got_error *
4889 cmd_backout(int argc, char *argv[])
4891 const struct got_error *error = NULL;
4892 struct got_worktree *worktree = NULL;
4893 struct got_repository *repo = NULL;
4894 char *cwd = NULL, *commit_id_str = NULL;
4895 struct got_object_id *commit_id = NULL;
4896 struct got_commit_object *commit = NULL;
4897 struct got_object_qid *pid;
4898 struct got_reference *head_ref = NULL;
4899 int ch, did_something = 0;
4901 while ((ch = getopt(argc, argv, "")) != -1) {
4902 switch (ch) {
4903 default:
4904 usage_backout();
4905 /* NOTREACHED */
4909 argc -= optind;
4910 argv += optind;
4912 #ifndef PROFILE
4913 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4914 "unveil", NULL) == -1)
4915 err(1, "pledge");
4916 #endif
4917 if (argc != 1)
4918 usage_backout();
4920 cwd = getcwd(NULL, 0);
4921 if (cwd == NULL) {
4922 error = got_error_from_errno("getcwd");
4923 goto done;
4925 error = got_worktree_open(&worktree, cwd);
4926 if (error)
4927 goto done;
4929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4930 NULL);
4931 if (error != NULL)
4932 goto done;
4934 error = apply_unveil(got_repo_get_path(repo), 0,
4935 got_worktree_get_root_path(worktree));
4936 if (error)
4937 goto done;
4939 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4940 GOT_OBJ_TYPE_COMMIT, repo);
4941 if (error != NULL) {
4942 struct got_reference *ref;
4943 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4944 goto done;
4945 error = got_ref_open(&ref, repo, argv[0], 0);
4946 if (error != NULL)
4947 goto done;
4948 error = got_ref_resolve(&commit_id, repo, ref);
4949 got_ref_close(ref);
4950 if (error != NULL)
4951 goto done;
4953 error = got_object_id_str(&commit_id_str, commit_id);
4954 if (error)
4955 goto done;
4957 error = got_ref_open(&head_ref, repo,
4958 got_worktree_get_head_ref_name(worktree), 0);
4959 if (error != NULL)
4960 goto done;
4962 error = check_same_branch(commit_id, head_ref, NULL, repo);
4963 if (error)
4964 goto done;
4966 error = got_object_open_as_commit(&commit, repo, commit_id);
4967 if (error)
4968 goto done;
4969 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4970 if (pid == NULL) {
4971 error = got_error(GOT_ERR_ROOT_COMMIT);
4972 goto done;
4975 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4976 update_progress, &did_something, check_cancelled, NULL);
4977 if (error != NULL)
4978 goto done;
4980 if (did_something)
4981 printf("Backed out commit %s\n", commit_id_str);
4982 done:
4983 if (commit)
4984 got_object_commit_close(commit);
4985 free(commit_id_str);
4986 if (head_ref)
4987 got_ref_close(head_ref);
4988 if (worktree)
4989 got_worktree_close(worktree);
4990 if (repo)
4991 got_repo_close(repo);
4992 return error;
4995 __dead static void
4996 usage_rebase(void)
4998 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4999 getprogname());
5000 exit(1);
5003 void
5004 trim_logmsg(char *logmsg, int limit)
5006 char *nl;
5007 size_t len;
5009 len = strlen(logmsg);
5010 if (len > limit)
5011 len = limit;
5012 logmsg[len] = '\0';
5013 nl = strchr(logmsg, '\n');
5014 if (nl)
5015 *nl = '\0';
5018 static const struct got_error *
5019 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5021 const struct got_error *err;
5022 char *logmsg0 = NULL;
5023 const char *s;
5025 err = got_object_commit_get_logmsg(&logmsg0, commit);
5026 if (err)
5027 return err;
5029 s = logmsg0;
5030 while (isspace((unsigned char)s[0]))
5031 s++;
5033 *logmsg = strdup(s);
5034 if (*logmsg == NULL) {
5035 err = got_error_from_errno("strdup");
5036 goto done;
5039 trim_logmsg(*logmsg, limit);
5040 done:
5041 free(logmsg0);
5042 return err;
5045 static const struct got_error *
5046 show_rebase_progress(struct got_commit_object *commit,
5047 struct got_object_id *old_id, struct got_object_id *new_id)
5049 const struct got_error *err;
5050 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5052 err = got_object_id_str(&old_id_str, old_id);
5053 if (err)
5054 goto done;
5056 if (new_id) {
5057 err = got_object_id_str(&new_id_str, new_id);
5058 if (err)
5059 goto done;
5062 old_id_str[12] = '\0';
5063 if (new_id_str)
5064 new_id_str[12] = '\0';
5066 err = get_short_logmsg(&logmsg, 42, commit);
5067 if (err)
5068 goto done;
5070 printf("%s -> %s: %s\n", old_id_str,
5071 new_id_str ? new_id_str : "no-op change", logmsg);
5072 done:
5073 free(old_id_str);
5074 free(new_id_str);
5075 return err;
5078 static const struct got_error *
5079 rebase_progress(void *arg, unsigned char status, const char *path)
5081 unsigned char *rebase_status = arg;
5083 while (path[0] == '/')
5084 path++;
5085 printf("%c %s\n", status, path);
5087 if (*rebase_status == GOT_STATUS_CONFLICT)
5088 return NULL;
5089 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5090 *rebase_status = status;
5091 return NULL;
5094 static const struct got_error *
5095 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5096 struct got_reference *branch, struct got_reference *new_base_branch,
5097 struct got_reference *tmp_branch, struct got_repository *repo)
5099 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5100 return got_worktree_rebase_complete(worktree, fileindex,
5101 new_base_branch, tmp_branch, branch, repo);
5104 static const struct got_error *
5105 rebase_commit(struct got_pathlist_head *merged_paths,
5106 struct got_worktree *worktree, struct got_fileindex *fileindex,
5107 struct got_reference *tmp_branch,
5108 struct got_object_id *commit_id, struct got_repository *repo)
5110 const struct got_error *error;
5111 struct got_commit_object *commit;
5112 struct got_object_id *new_commit_id;
5114 error = got_object_open_as_commit(&commit, repo, commit_id);
5115 if (error)
5116 return error;
5118 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5119 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5120 if (error) {
5121 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5122 goto done;
5123 error = show_rebase_progress(commit, commit_id, NULL);
5124 } else {
5125 error = show_rebase_progress(commit, commit_id, new_commit_id);
5126 free(new_commit_id);
5128 done:
5129 got_object_commit_close(commit);
5130 return error;
5133 struct check_path_prefix_arg {
5134 const char *path_prefix;
5135 size_t len;
5136 int errcode;
5139 static const struct got_error *
5140 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5141 struct got_blob_object *blob2, struct got_object_id *id1,
5142 struct got_object_id *id2, const char *path1, const char *path2,
5143 mode_t mode1, mode_t mode2, struct got_repository *repo)
5145 struct check_path_prefix_arg *a = arg;
5147 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5148 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5149 return got_error(a->errcode);
5151 return NULL;
5154 static const struct got_error *
5155 check_path_prefix(struct got_object_id *parent_id,
5156 struct got_object_id *commit_id, const char *path_prefix,
5157 int errcode, struct got_repository *repo)
5159 const struct got_error *err;
5160 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5161 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5162 struct check_path_prefix_arg cpp_arg;
5164 if (got_path_is_root_dir(path_prefix))
5165 return NULL;
5167 err = got_object_open_as_commit(&commit, repo, commit_id);
5168 if (err)
5169 goto done;
5171 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5172 if (err)
5173 goto done;
5175 err = got_object_open_as_tree(&tree1, repo,
5176 got_object_commit_get_tree_id(parent_commit));
5177 if (err)
5178 goto done;
5180 err = got_object_open_as_tree(&tree2, repo,
5181 got_object_commit_get_tree_id(commit));
5182 if (err)
5183 goto done;
5185 cpp_arg.path_prefix = path_prefix;
5186 while (cpp_arg.path_prefix[0] == '/')
5187 cpp_arg.path_prefix++;
5188 cpp_arg.len = strlen(cpp_arg.path_prefix);
5189 cpp_arg.errcode = errcode;
5190 err = got_diff_tree(tree1, tree2, "", "", repo,
5191 check_path_prefix_in_diff, &cpp_arg, 0);
5192 done:
5193 if (tree1)
5194 got_object_tree_close(tree1);
5195 if (tree2)
5196 got_object_tree_close(tree2);
5197 if (commit)
5198 got_object_commit_close(commit);
5199 if (parent_commit)
5200 got_object_commit_close(parent_commit);
5201 return err;
5204 static const struct got_error *
5205 collect_commits(struct got_object_id_queue *commits,
5206 struct got_object_id *initial_commit_id,
5207 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5208 const char *path_prefix, int path_prefix_errcode,
5209 struct got_repository *repo)
5211 const struct got_error *err = NULL;
5212 struct got_commit_graph *graph = NULL;
5213 struct got_object_id *parent_id = NULL;
5214 struct got_object_qid *qid;
5215 struct got_object_id *commit_id = initial_commit_id;
5217 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5218 if (err)
5219 return err;
5221 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5222 check_cancelled, NULL);
5223 if (err)
5224 goto done;
5225 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5226 err = got_commit_graph_iter_next(&parent_id, graph);
5227 if (err) {
5228 if (err->code == GOT_ERR_ITER_COMPLETED) {
5229 err = got_error_msg(GOT_ERR_ANCESTRY,
5230 "ran out of commits to rebase before "
5231 "youngest common ancestor commit has "
5232 "been reached?!?");
5233 goto done;
5234 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5235 goto done;
5236 err = got_commit_graph_fetch_commits(graph, 1, repo,
5237 check_cancelled, NULL);
5238 if (err)
5239 goto done;
5240 } else {
5241 err = check_path_prefix(parent_id, commit_id,
5242 path_prefix, path_prefix_errcode, repo);
5243 if (err)
5244 goto done;
5246 err = got_object_qid_alloc(&qid, commit_id);
5247 if (err)
5248 goto done;
5249 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5250 commit_id = parent_id;
5253 done:
5254 got_commit_graph_close(graph);
5255 return err;
5258 static const struct got_error *
5259 cmd_rebase(int argc, char *argv[])
5261 const struct got_error *error = NULL;
5262 struct got_worktree *worktree = NULL;
5263 struct got_repository *repo = NULL;
5264 struct got_fileindex *fileindex = NULL;
5265 char *cwd = NULL;
5266 struct got_reference *branch = NULL;
5267 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5268 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5269 struct got_object_id *resume_commit_id = NULL;
5270 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5271 struct got_commit_object *commit = NULL;
5272 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5273 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5274 struct got_object_id_queue commits;
5275 struct got_pathlist_head merged_paths;
5276 const struct got_object_id_queue *parent_ids;
5277 struct got_object_qid *qid, *pid;
5279 SIMPLEQ_INIT(&commits);
5280 TAILQ_INIT(&merged_paths);
5282 while ((ch = getopt(argc, argv, "ac")) != -1) {
5283 switch (ch) {
5284 case 'a':
5285 abort_rebase = 1;
5286 break;
5287 case 'c':
5288 continue_rebase = 1;
5289 break;
5290 default:
5291 usage_rebase();
5292 /* NOTREACHED */
5296 argc -= optind;
5297 argv += optind;
5299 #ifndef PROFILE
5300 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5301 "unveil", NULL) == -1)
5302 err(1, "pledge");
5303 #endif
5304 if (abort_rebase && continue_rebase)
5305 usage_rebase();
5306 else if (abort_rebase || continue_rebase) {
5307 if (argc != 0)
5308 usage_rebase();
5309 } else if (argc != 1)
5310 usage_rebase();
5312 cwd = getcwd(NULL, 0);
5313 if (cwd == NULL) {
5314 error = got_error_from_errno("getcwd");
5315 goto done;
5317 error = got_worktree_open(&worktree, cwd);
5318 if (error)
5319 goto done;
5321 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5322 NULL);
5323 if (error != NULL)
5324 goto done;
5326 error = apply_unveil(got_repo_get_path(repo), 0,
5327 got_worktree_get_root_path(worktree));
5328 if (error)
5329 goto done;
5331 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5332 if (error)
5333 goto done;
5335 if (abort_rebase) {
5336 int did_something;
5337 if (!rebase_in_progress) {
5338 error = got_error(GOT_ERR_NOT_REBASING);
5339 goto done;
5341 error = got_worktree_rebase_continue(&resume_commit_id,
5342 &new_base_branch, &tmp_branch, &branch, &fileindex,
5343 worktree, repo);
5344 if (error)
5345 goto done;
5346 printf("Switching work tree to %s\n",
5347 got_ref_get_symref_target(new_base_branch));
5348 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5349 new_base_branch, update_progress, &did_something);
5350 if (error)
5351 goto done;
5352 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5353 goto done; /* nothing else to do */
5356 if (continue_rebase) {
5357 if (!rebase_in_progress) {
5358 error = got_error(GOT_ERR_NOT_REBASING);
5359 goto done;
5361 error = got_worktree_rebase_continue(&resume_commit_id,
5362 &new_base_branch, &tmp_branch, &branch, &fileindex,
5363 worktree, repo);
5364 if (error)
5365 goto done;
5367 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5368 resume_commit_id, repo);
5369 if (error)
5370 goto done;
5372 yca_id = got_object_id_dup(resume_commit_id);
5373 if (yca_id == NULL) {
5374 error = got_error_from_errno("got_object_id_dup");
5375 goto done;
5377 } else {
5378 error = got_ref_open(&branch, repo, argv[0], 0);
5379 if (error != NULL)
5380 goto done;
5383 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5384 if (error)
5385 goto done;
5387 if (!continue_rebase) {
5388 struct got_object_id *base_commit_id;
5390 base_commit_id = got_worktree_get_base_commit_id(worktree);
5391 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5392 base_commit_id, branch_head_commit_id, repo,
5393 check_cancelled, NULL);
5394 if (error)
5395 goto done;
5396 if (yca_id == NULL) {
5397 error = got_error_msg(GOT_ERR_ANCESTRY,
5398 "specified branch shares no common ancestry "
5399 "with work tree's branch");
5400 goto done;
5403 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5404 if (error) {
5405 if (error->code != GOT_ERR_ANCESTRY)
5406 goto done;
5407 error = NULL;
5408 } else {
5409 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5410 "specified branch resolves to a commit which "
5411 "is already contained in work tree's branch");
5412 goto done;
5414 error = got_worktree_rebase_prepare(&new_base_branch,
5415 &tmp_branch, &fileindex, worktree, branch, repo);
5416 if (error)
5417 goto done;
5420 commit_id = branch_head_commit_id;
5421 error = got_object_open_as_commit(&commit, repo, commit_id);
5422 if (error)
5423 goto done;
5425 parent_ids = got_object_commit_get_parent_ids(commit);
5426 pid = SIMPLEQ_FIRST(parent_ids);
5427 if (pid == NULL) {
5428 if (!continue_rebase) {
5429 int did_something;
5430 error = got_worktree_rebase_abort(worktree, fileindex,
5431 repo, new_base_branch, update_progress,
5432 &did_something);
5433 if (error)
5434 goto done;
5435 printf("Rebase of %s aborted\n",
5436 got_ref_get_name(branch));
5438 error = got_error(GOT_ERR_EMPTY_REBASE);
5439 goto done;
5441 error = collect_commits(&commits, commit_id, pid->id,
5442 yca_id, got_worktree_get_path_prefix(worktree),
5443 GOT_ERR_REBASE_PATH, repo);
5444 got_object_commit_close(commit);
5445 commit = NULL;
5446 if (error)
5447 goto done;
5449 if (SIMPLEQ_EMPTY(&commits)) {
5450 if (continue_rebase)
5451 error = rebase_complete(worktree, fileindex,
5452 branch, new_base_branch, tmp_branch, repo);
5453 else
5454 error = got_error(GOT_ERR_EMPTY_REBASE);
5455 goto done;
5458 pid = NULL;
5459 SIMPLEQ_FOREACH(qid, &commits, entry) {
5460 commit_id = qid->id;
5461 parent_id = pid ? pid->id : yca_id;
5462 pid = qid;
5464 error = got_worktree_rebase_merge_files(&merged_paths,
5465 worktree, fileindex, parent_id, commit_id, repo,
5466 rebase_progress, &rebase_status, check_cancelled, NULL);
5467 if (error)
5468 goto done;
5470 if (rebase_status == GOT_STATUS_CONFLICT) {
5471 got_worktree_rebase_pathlist_free(&merged_paths);
5472 break;
5475 error = rebase_commit(&merged_paths, worktree, fileindex,
5476 tmp_branch, commit_id, repo);
5477 got_worktree_rebase_pathlist_free(&merged_paths);
5478 if (error)
5479 goto done;
5482 if (rebase_status == GOT_STATUS_CONFLICT) {
5483 error = got_worktree_rebase_postpone(worktree, fileindex);
5484 if (error)
5485 goto done;
5486 error = got_error_msg(GOT_ERR_CONFLICTS,
5487 "conflicts must be resolved before rebasing can continue");
5488 } else
5489 error = rebase_complete(worktree, fileindex, branch,
5490 new_base_branch, tmp_branch, repo);
5491 done:
5492 got_object_id_queue_free(&commits);
5493 free(branch_head_commit_id);
5494 free(resume_commit_id);
5495 free(yca_id);
5496 if (commit)
5497 got_object_commit_close(commit);
5498 if (branch)
5499 got_ref_close(branch);
5500 if (new_base_branch)
5501 got_ref_close(new_base_branch);
5502 if (tmp_branch)
5503 got_ref_close(tmp_branch);
5504 if (worktree)
5505 got_worktree_close(worktree);
5506 if (repo)
5507 got_repo_close(repo);
5508 return error;
5511 __dead static void
5512 usage_histedit(void)
5514 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5515 getprogname());
5516 exit(1);
5519 #define GOT_HISTEDIT_PICK 'p'
5520 #define GOT_HISTEDIT_EDIT 'e'
5521 #define GOT_HISTEDIT_FOLD 'f'
5522 #define GOT_HISTEDIT_DROP 'd'
5523 #define GOT_HISTEDIT_MESG 'm'
5525 static struct got_histedit_cmd {
5526 unsigned char code;
5527 const char *name;
5528 const char *desc;
5529 } got_histedit_cmds[] = {
5530 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5531 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5532 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5533 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5534 { GOT_HISTEDIT_MESG, "mesg",
5535 "single-line log message for commit above (open editor if empty)" },
5538 struct got_histedit_list_entry {
5539 TAILQ_ENTRY(got_histedit_list_entry) entry;
5540 struct got_object_id *commit_id;
5541 const struct got_histedit_cmd *cmd;
5542 char *logmsg;
5544 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5546 static const struct got_error *
5547 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5548 FILE *f, struct got_repository *repo)
5550 const struct got_error *err = NULL;
5551 char *logmsg = NULL, *id_str = NULL;
5552 struct got_commit_object *commit = NULL;
5553 int n;
5555 err = got_object_open_as_commit(&commit, repo, commit_id);
5556 if (err)
5557 goto done;
5559 err = get_short_logmsg(&logmsg, 34, commit);
5560 if (err)
5561 goto done;
5563 err = got_object_id_str(&id_str, commit_id);
5564 if (err)
5565 goto done;
5567 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5568 if (n < 0)
5569 err = got_ferror(f, GOT_ERR_IO);
5570 done:
5571 if (commit)
5572 got_object_commit_close(commit);
5573 free(id_str);
5574 free(logmsg);
5575 return err;
5578 static const struct got_error *
5579 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5580 struct got_repository *repo)
5582 const struct got_error *err = NULL;
5583 struct got_object_qid *qid;
5585 if (SIMPLEQ_EMPTY(commits))
5586 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5588 SIMPLEQ_FOREACH(qid, commits, entry) {
5589 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5590 f, repo);
5591 if (err)
5592 break;
5595 return err;
5598 static const struct got_error *
5599 write_cmd_list(FILE *f)
5601 const struct got_error *err = NULL;
5602 int n, i;
5604 n = fprintf(f, "# Available histedit commands:\n");
5605 if (n < 0)
5606 return got_ferror(f, GOT_ERR_IO);
5608 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5609 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5610 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5611 cmd->desc);
5612 if (n < 0) {
5613 err = got_ferror(f, GOT_ERR_IO);
5614 break;
5617 n = fprintf(f, "# Commits will be processed in order from top to "
5618 "bottom of this file.\n");
5619 if (n < 0)
5620 return got_ferror(f, GOT_ERR_IO);
5621 return err;
5624 static const struct got_error *
5625 histedit_syntax_error(int lineno)
5627 static char msg[42];
5628 int ret;
5630 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5631 lineno);
5632 if (ret == -1 || ret >= sizeof(msg))
5633 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5635 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5638 static const struct got_error *
5639 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5640 char *logmsg, struct got_repository *repo)
5642 const struct got_error *err;
5643 struct got_commit_object *folded_commit = NULL;
5644 char *id_str, *folded_logmsg = NULL;
5646 err = got_object_id_str(&id_str, hle->commit_id);
5647 if (err)
5648 return err;
5650 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5651 if (err)
5652 goto done;
5654 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5655 if (err)
5656 goto done;
5657 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5658 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5659 folded_logmsg) == -1) {
5660 err = got_error_from_errno("asprintf");
5661 goto done;
5663 done:
5664 if (folded_commit)
5665 got_object_commit_close(folded_commit);
5666 free(id_str);
5667 free(folded_logmsg);
5668 return err;
5671 static struct got_histedit_list_entry *
5672 get_folded_commits(struct got_histedit_list_entry *hle)
5674 struct got_histedit_list_entry *prev, *folded = NULL;
5676 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5677 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5678 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5679 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5680 folded = prev;
5681 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5684 return folded;
5687 static const struct got_error *
5688 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5689 struct got_repository *repo)
5691 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5692 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5693 const struct got_error *err = NULL;
5694 struct got_commit_object *commit = NULL;
5695 int fd;
5696 struct got_histedit_list_entry *folded = NULL;
5698 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5699 if (err)
5700 return err;
5702 folded = get_folded_commits(hle);
5703 if (folded) {
5704 while (folded != hle) {
5705 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5706 folded = TAILQ_NEXT(folded, entry);
5707 continue;
5709 err = append_folded_commit_msg(&new_msg, folded,
5710 logmsg, repo);
5711 if (err)
5712 goto done;
5713 free(logmsg);
5714 logmsg = new_msg;
5715 folded = TAILQ_NEXT(folded, entry);
5719 err = got_object_id_str(&id_str, hle->commit_id);
5720 if (err)
5721 goto done;
5722 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5723 if (err)
5724 goto done;
5725 if (asprintf(&new_msg,
5726 "%s\n# original log message of commit %s: %s",
5727 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5728 err = got_error_from_errno("asprintf");
5729 goto done;
5731 free(logmsg);
5732 logmsg = new_msg;
5734 err = got_object_id_str(&id_str, hle->commit_id);
5735 if (err)
5736 goto done;
5738 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5739 if (err)
5740 goto done;
5742 dprintf(fd, logmsg);
5743 close(fd);
5745 err = get_editor(&editor);
5746 if (err)
5747 goto done;
5749 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5750 if (err) {
5751 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5752 goto done;
5753 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5755 done:
5756 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5757 err = got_error_from_errno2("unlink", logmsg_path);
5758 free(logmsg_path);
5759 free(logmsg);
5760 free(orig_logmsg);
5761 free(editor);
5762 if (commit)
5763 got_object_commit_close(commit);
5764 return err;
5767 static const struct got_error *
5768 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5769 FILE *f, struct got_repository *repo)
5771 const struct got_error *err = NULL;
5772 char *line = NULL, *p, *end;
5773 size_t size;
5774 ssize_t len;
5775 int lineno = 0, i;
5776 const struct got_histedit_cmd *cmd;
5777 struct got_object_id *commit_id = NULL;
5778 struct got_histedit_list_entry *hle = NULL;
5780 for (;;) {
5781 len = getline(&line, &size, f);
5782 if (len == -1) {
5783 const struct got_error *getline_err;
5784 if (feof(f))
5785 break;
5786 getline_err = got_error_from_errno("getline");
5787 err = got_ferror(f, getline_err->code);
5788 break;
5790 lineno++;
5791 p = line;
5792 while (isspace((unsigned char)p[0]))
5793 p++;
5794 if (p[0] == '#' || p[0] == '\0') {
5795 free(line);
5796 line = NULL;
5797 continue;
5799 cmd = NULL;
5800 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5801 cmd = &got_histedit_cmds[i];
5802 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5803 isspace((unsigned char)p[strlen(cmd->name)])) {
5804 p += strlen(cmd->name);
5805 break;
5807 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5808 p++;
5809 break;
5812 if (i == nitems(got_histedit_cmds)) {
5813 err = histedit_syntax_error(lineno);
5814 break;
5816 while (isspace((unsigned char)p[0]))
5817 p++;
5818 if (cmd->code == GOT_HISTEDIT_MESG) {
5819 if (hle == NULL || hle->logmsg != NULL) {
5820 err = got_error(GOT_ERR_HISTEDIT_CMD);
5821 break;
5823 if (p[0] == '\0') {
5824 err = histedit_edit_logmsg(hle, repo);
5825 if (err)
5826 break;
5827 } else {
5828 hle->logmsg = strdup(p);
5829 if (hle->logmsg == NULL) {
5830 err = got_error_from_errno("strdup");
5831 break;
5834 free(line);
5835 line = NULL;
5836 continue;
5837 } else {
5838 end = p;
5839 while (end[0] && !isspace((unsigned char)end[0]))
5840 end++;
5841 *end = '\0';
5843 err = got_object_resolve_id_str(&commit_id, repo, p);
5844 if (err) {
5845 /* override error code */
5846 err = histedit_syntax_error(lineno);
5847 break;
5850 hle = malloc(sizeof(*hle));
5851 if (hle == NULL) {
5852 err = got_error_from_errno("malloc");
5853 break;
5855 hle->cmd = cmd;
5856 hle->commit_id = commit_id;
5857 hle->logmsg = NULL;
5858 commit_id = NULL;
5859 free(line);
5860 line = NULL;
5861 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5864 free(line);
5865 free(commit_id);
5866 return err;
5869 static const struct got_error *
5870 histedit_check_script(struct got_histedit_list *histedit_cmds,
5871 struct got_object_id_queue *commits, struct got_repository *repo)
5873 const struct got_error *err = NULL;
5874 struct got_object_qid *qid;
5875 struct got_histedit_list_entry *hle;
5876 static char msg[80];
5877 char *id_str;
5879 if (TAILQ_EMPTY(histedit_cmds))
5880 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5881 "histedit script contains no commands");
5882 if (SIMPLEQ_EMPTY(commits))
5883 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5885 SIMPLEQ_FOREACH(qid, commits, entry) {
5886 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5887 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5888 break;
5890 if (hle == NULL) {
5891 err = got_object_id_str(&id_str, qid->id);
5892 if (err)
5893 return err;
5894 snprintf(msg, sizeof(msg),
5895 "commit %s missing from histedit script", id_str);
5896 free(id_str);
5897 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5901 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5902 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5903 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5904 "last commit in histedit script cannot be folded");
5906 return NULL;
5909 static const struct got_error *
5910 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5911 const char *path, struct got_object_id_queue *commits,
5912 struct got_repository *repo)
5914 const struct got_error *err = NULL;
5915 char *editor;
5916 FILE *f = NULL;
5918 err = get_editor(&editor);
5919 if (err)
5920 return err;
5922 if (spawn_editor(editor, path) == -1) {
5923 err = got_error_from_errno("failed spawning editor");
5924 goto done;
5927 f = fopen(path, "r");
5928 if (f == NULL) {
5929 err = got_error_from_errno("fopen");
5930 goto done;
5932 err = histedit_parse_list(histedit_cmds, f, repo);
5933 if (err)
5934 goto done;
5936 err = histedit_check_script(histedit_cmds, commits, repo);
5937 done:
5938 if (f && fclose(f) != 0 && err == NULL)
5939 err = got_error_from_errno("fclose");
5940 free(editor);
5941 return err;
5944 static const struct got_error *
5945 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5946 struct got_object_id_queue *, const char *, struct got_repository *);
5948 static const struct got_error *
5949 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5950 struct got_object_id_queue *commits, struct got_repository *repo)
5952 const struct got_error *err;
5953 FILE *f = NULL;
5954 char *path = NULL;
5956 err = got_opentemp_named(&path, &f, "got-histedit");
5957 if (err)
5958 return err;
5960 err = write_cmd_list(f);
5961 if (err)
5962 goto done;
5964 err = histedit_write_commit_list(commits, f, repo);
5965 if (err)
5966 goto done;
5968 if (fclose(f) != 0) {
5969 err = got_error_from_errno("fclose");
5970 goto done;
5972 f = NULL;
5974 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5975 if (err) {
5976 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5977 err->code != GOT_ERR_HISTEDIT_CMD)
5978 goto done;
5979 err = histedit_edit_list_retry(histedit_cmds, err,
5980 commits, path, repo);
5982 done:
5983 if (f && fclose(f) != 0 && err == NULL)
5984 err = got_error_from_errno("fclose");
5985 if (path && unlink(path) != 0 && err == NULL)
5986 err = got_error_from_errno2("unlink", path);
5987 free(path);
5988 return err;
5991 static const struct got_error *
5992 histedit_save_list(struct got_histedit_list *histedit_cmds,
5993 struct got_worktree *worktree, struct got_repository *repo)
5995 const struct got_error *err = NULL;
5996 char *path = NULL;
5997 FILE *f = NULL;
5998 struct got_histedit_list_entry *hle;
5999 struct got_commit_object *commit = NULL;
6001 err = got_worktree_get_histedit_script_path(&path, worktree);
6002 if (err)
6003 return err;
6005 f = fopen(path, "w");
6006 if (f == NULL) {
6007 err = got_error_from_errno2("fopen", path);
6008 goto done;
6010 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6011 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6012 repo);
6013 if (err)
6014 break;
6016 if (hle->logmsg) {
6017 int n = fprintf(f, "%c %s\n",
6018 GOT_HISTEDIT_MESG, hle->logmsg);
6019 if (n < 0) {
6020 err = got_ferror(f, GOT_ERR_IO);
6021 break;
6025 done:
6026 if (f && fclose(f) != 0 && err == NULL)
6027 err = got_error_from_errno("fclose");
6028 free(path);
6029 if (commit)
6030 got_object_commit_close(commit);
6031 return err;
6034 void
6035 histedit_free_list(struct got_histedit_list *histedit_cmds)
6037 struct got_histedit_list_entry *hle;
6039 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6040 TAILQ_REMOVE(histedit_cmds, hle, entry);
6041 free(hle);
6045 static const struct got_error *
6046 histedit_load_list(struct got_histedit_list *histedit_cmds,
6047 const char *path, struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 FILE *f = NULL;
6052 f = fopen(path, "r");
6053 if (f == NULL) {
6054 err = got_error_from_errno2("fopen", path);
6055 goto done;
6058 err = histedit_parse_list(histedit_cmds, f, repo);
6059 done:
6060 if (f && fclose(f) != 0 && err == NULL)
6061 err = got_error_from_errno("fclose");
6062 return err;
6065 static const struct got_error *
6066 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6067 const struct got_error *edit_err, struct got_object_id_queue *commits,
6068 const char *path, struct got_repository *repo)
6070 const struct got_error *err = NULL, *prev_err = edit_err;
6071 int resp = ' ';
6073 while (resp != 'c' && resp != 'r' && resp != 'a') {
6074 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6075 "or (a)bort: ", getprogname(), prev_err->msg);
6076 resp = getchar();
6077 if (resp == '\n')
6078 resp = getchar();
6079 if (resp == 'c') {
6080 histedit_free_list(histedit_cmds);
6081 err = histedit_run_editor(histedit_cmds, path, commits,
6082 repo);
6083 if (err) {
6084 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6085 err->code != GOT_ERR_HISTEDIT_CMD)
6086 break;
6087 prev_err = err;
6088 resp = ' ';
6089 continue;
6091 break;
6092 } else if (resp == 'r') {
6093 histedit_free_list(histedit_cmds);
6094 err = histedit_edit_script(histedit_cmds,
6095 commits, repo);
6096 if (err) {
6097 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6098 err->code != GOT_ERR_HISTEDIT_CMD)
6099 break;
6100 prev_err = err;
6101 resp = ' ';
6102 continue;
6104 break;
6105 } else if (resp == 'a') {
6106 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6107 break;
6108 } else
6109 printf("invalid response '%c'\n", resp);
6112 return err;
6115 static const struct got_error *
6116 histedit_complete(struct got_worktree *worktree,
6117 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6118 struct got_reference *branch, struct got_repository *repo)
6120 printf("Switching work tree to %s\n",
6121 got_ref_get_symref_target(branch));
6122 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6123 branch, repo);
6126 static const struct got_error *
6127 show_histedit_progress(struct got_commit_object *commit,
6128 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6130 const struct got_error *err;
6131 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6133 err = got_object_id_str(&old_id_str, hle->commit_id);
6134 if (err)
6135 goto done;
6137 if (new_id) {
6138 err = got_object_id_str(&new_id_str, new_id);
6139 if (err)
6140 goto done;
6143 old_id_str[12] = '\0';
6144 if (new_id_str)
6145 new_id_str[12] = '\0';
6147 if (hle->logmsg) {
6148 logmsg = strdup(hle->logmsg);
6149 if (logmsg == NULL) {
6150 err = got_error_from_errno("strdup");
6151 goto done;
6153 trim_logmsg(logmsg, 42);
6154 } else {
6155 err = get_short_logmsg(&logmsg, 42, commit);
6156 if (err)
6157 goto done;
6160 switch (hle->cmd->code) {
6161 case GOT_HISTEDIT_PICK:
6162 case GOT_HISTEDIT_EDIT:
6163 printf("%s -> %s: %s\n", old_id_str,
6164 new_id_str ? new_id_str : "no-op change", logmsg);
6165 break;
6166 case GOT_HISTEDIT_DROP:
6167 case GOT_HISTEDIT_FOLD:
6168 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6169 logmsg);
6170 break;
6171 default:
6172 break;
6175 done:
6176 free(old_id_str);
6177 free(new_id_str);
6178 return err;
6181 static const struct got_error *
6182 histedit_commit(struct got_pathlist_head *merged_paths,
6183 struct got_worktree *worktree, struct got_fileindex *fileindex,
6184 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6185 struct got_repository *repo)
6187 const struct got_error *err;
6188 struct got_commit_object *commit;
6189 struct got_object_id *new_commit_id;
6191 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6192 && hle->logmsg == NULL) {
6193 err = histedit_edit_logmsg(hle, repo);
6194 if (err)
6195 return err;
6198 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6199 if (err)
6200 return err;
6202 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6203 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6204 hle->logmsg, repo);
6205 if (err) {
6206 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6207 goto done;
6208 err = show_histedit_progress(commit, hle, NULL);
6209 } else {
6210 err = show_histedit_progress(commit, hle, new_commit_id);
6211 free(new_commit_id);
6213 done:
6214 got_object_commit_close(commit);
6215 return err;
6218 static const struct got_error *
6219 histedit_skip_commit(struct got_histedit_list_entry *hle,
6220 struct got_worktree *worktree, struct got_repository *repo)
6222 const struct got_error *error;
6223 struct got_commit_object *commit;
6225 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6226 repo);
6227 if (error)
6228 return error;
6230 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6231 if (error)
6232 return error;
6234 error = show_histedit_progress(commit, hle, NULL);
6235 got_object_commit_close(commit);
6236 return error;
6239 static const struct got_error *
6240 cmd_histedit(int argc, char *argv[])
6242 const struct got_error *error = NULL;
6243 struct got_worktree *worktree = NULL;
6244 struct got_fileindex *fileindex = NULL;
6245 struct got_repository *repo = NULL;
6246 char *cwd = NULL;
6247 struct got_reference *branch = NULL;
6248 struct got_reference *tmp_branch = NULL;
6249 struct got_object_id *resume_commit_id = NULL;
6250 struct got_object_id *base_commit_id = NULL;
6251 struct got_object_id *head_commit_id = NULL;
6252 struct got_commit_object *commit = NULL;
6253 int ch, rebase_in_progress = 0, did_something;
6254 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6255 const char *edit_script_path = NULL;
6256 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6257 struct got_object_id_queue commits;
6258 struct got_pathlist_head merged_paths;
6259 const struct got_object_id_queue *parent_ids;
6260 struct got_object_qid *pid;
6261 struct got_histedit_list histedit_cmds;
6262 struct got_histedit_list_entry *hle;
6264 SIMPLEQ_INIT(&commits);
6265 TAILQ_INIT(&histedit_cmds);
6266 TAILQ_INIT(&merged_paths);
6268 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6269 switch (ch) {
6270 case 'a':
6271 abort_edit = 1;
6272 break;
6273 case 'c':
6274 continue_edit = 1;
6275 break;
6276 case 'F':
6277 edit_script_path = optarg;
6278 break;
6279 default:
6280 usage_histedit();
6281 /* NOTREACHED */
6285 argc -= optind;
6286 argv += optind;
6288 #ifndef PROFILE
6289 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6290 "unveil", NULL) == -1)
6291 err(1, "pledge");
6292 #endif
6293 if (abort_edit && continue_edit)
6294 usage_histedit();
6295 if (argc != 0)
6296 usage_histedit();
6299 * This command cannot apply unveil(2) in all cases because the
6300 * user may choose to run an editor to edit the histedit script
6301 * and to edit individual commit log messages.
6302 * unveil(2) traverses exec(2); if an editor is used we have to
6303 * apply unveil after edit script and log messages have been written.
6304 * XXX TODO: Make use of unveil(2) where possible.
6307 cwd = getcwd(NULL, 0);
6308 if (cwd == NULL) {
6309 error = got_error_from_errno("getcwd");
6310 goto done;
6312 error = got_worktree_open(&worktree, cwd);
6313 if (error)
6314 goto done;
6316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6317 NULL);
6318 if (error != NULL)
6319 goto done;
6321 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6322 if (error)
6323 goto done;
6324 if (rebase_in_progress) {
6325 error = got_error(GOT_ERR_REBASING);
6326 goto done;
6329 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6330 if (error)
6331 goto done;
6333 if (edit_in_progress && abort_edit) {
6334 error = got_worktree_histedit_continue(&resume_commit_id,
6335 &tmp_branch, &branch, &base_commit_id, &fileindex,
6336 worktree, repo);
6337 if (error)
6338 goto done;
6339 printf("Switching work tree to %s\n",
6340 got_ref_get_symref_target(branch));
6341 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6342 branch, base_commit_id, update_progress, &did_something);
6343 if (error)
6344 goto done;
6345 printf("Histedit of %s aborted\n",
6346 got_ref_get_symref_target(branch));
6347 goto done; /* nothing else to do */
6348 } else if (abort_edit) {
6349 error = got_error(GOT_ERR_NOT_HISTEDIT);
6350 goto done;
6353 if (continue_edit) {
6354 char *path;
6356 if (!edit_in_progress) {
6357 error = got_error(GOT_ERR_NOT_HISTEDIT);
6358 goto done;
6361 error = got_worktree_get_histedit_script_path(&path, worktree);
6362 if (error)
6363 goto done;
6365 error = histedit_load_list(&histedit_cmds, path, repo);
6366 free(path);
6367 if (error)
6368 goto done;
6370 error = got_worktree_histedit_continue(&resume_commit_id,
6371 &tmp_branch, &branch, &base_commit_id, &fileindex,
6372 worktree, repo);
6373 if (error)
6374 goto done;
6376 error = got_ref_resolve(&head_commit_id, repo, branch);
6377 if (error)
6378 goto done;
6380 error = got_object_open_as_commit(&commit, repo,
6381 head_commit_id);
6382 if (error)
6383 goto done;
6384 parent_ids = got_object_commit_get_parent_ids(commit);
6385 pid = SIMPLEQ_FIRST(parent_ids);
6386 if (pid == NULL) {
6387 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6388 goto done;
6390 error = collect_commits(&commits, head_commit_id, pid->id,
6391 base_commit_id, got_worktree_get_path_prefix(worktree),
6392 GOT_ERR_HISTEDIT_PATH, repo);
6393 got_object_commit_close(commit);
6394 commit = NULL;
6395 if (error)
6396 goto done;
6397 } else {
6398 if (edit_in_progress) {
6399 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6400 goto done;
6403 error = got_ref_open(&branch, repo,
6404 got_worktree_get_head_ref_name(worktree), 0);
6405 if (error != NULL)
6406 goto done;
6408 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6409 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6410 "will not edit commit history of a branch outside "
6411 "the \"refs/heads/\" reference namespace");
6412 goto done;
6415 error = got_ref_resolve(&head_commit_id, repo, branch);
6416 got_ref_close(branch);
6417 branch = NULL;
6418 if (error)
6419 goto done;
6421 error = got_object_open_as_commit(&commit, repo,
6422 head_commit_id);
6423 if (error)
6424 goto done;
6425 parent_ids = got_object_commit_get_parent_ids(commit);
6426 pid = SIMPLEQ_FIRST(parent_ids);
6427 if (pid == NULL) {
6428 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6429 goto done;
6431 error = collect_commits(&commits, head_commit_id, pid->id,
6432 got_worktree_get_base_commit_id(worktree),
6433 got_worktree_get_path_prefix(worktree),
6434 GOT_ERR_HISTEDIT_PATH, repo);
6435 got_object_commit_close(commit);
6436 commit = NULL;
6437 if (error)
6438 goto done;
6440 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6441 &base_commit_id, &fileindex, worktree, repo);
6442 if (error)
6443 goto done;
6445 if (edit_script_path) {
6446 error = histedit_load_list(&histedit_cmds,
6447 edit_script_path, repo);
6448 if (error) {
6449 got_worktree_histedit_abort(worktree, fileindex,
6450 repo, branch, base_commit_id,
6451 update_progress, &did_something);
6452 goto done;
6454 } else {
6455 error = histedit_edit_script(&histedit_cmds, &commits,
6456 repo);
6457 if (error) {
6458 got_worktree_histedit_abort(worktree, fileindex,
6459 repo, branch, base_commit_id,
6460 update_progress, &did_something);
6461 goto done;
6466 error = histedit_save_list(&histedit_cmds, worktree,
6467 repo);
6468 if (error) {
6469 got_worktree_histedit_abort(worktree, fileindex,
6470 repo, branch, base_commit_id,
6471 update_progress, &did_something);
6472 goto done;
6477 error = histedit_check_script(&histedit_cmds, &commits, repo);
6478 if (error)
6479 goto done;
6481 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6482 if (resume_commit_id) {
6483 if (got_object_id_cmp(hle->commit_id,
6484 resume_commit_id) != 0)
6485 continue;
6487 resume_commit_id = NULL;
6488 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6489 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6490 error = histedit_skip_commit(hle, worktree,
6491 repo);
6492 } else {
6493 error = histedit_commit(NULL, worktree,
6494 fileindex, tmp_branch, hle, repo);
6496 if (error)
6497 goto done;
6498 continue;
6501 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6502 error = histedit_skip_commit(hle, worktree, repo);
6503 if (error)
6504 goto done;
6505 continue;
6508 error = got_object_open_as_commit(&commit, repo,
6509 hle->commit_id);
6510 if (error)
6511 goto done;
6512 parent_ids = got_object_commit_get_parent_ids(commit);
6513 pid = SIMPLEQ_FIRST(parent_ids);
6515 error = got_worktree_histedit_merge_files(&merged_paths,
6516 worktree, fileindex, pid->id, hle->commit_id, repo,
6517 rebase_progress, &rebase_status, check_cancelled, NULL);
6518 if (error)
6519 goto done;
6520 got_object_commit_close(commit);
6521 commit = NULL;
6523 if (rebase_status == GOT_STATUS_CONFLICT) {
6524 got_worktree_rebase_pathlist_free(&merged_paths);
6525 break;
6528 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6529 char *id_str;
6530 error = got_object_id_str(&id_str, hle->commit_id);
6531 if (error)
6532 goto done;
6533 printf("Stopping histedit for amending commit %s\n",
6534 id_str);
6535 free(id_str);
6536 got_worktree_rebase_pathlist_free(&merged_paths);
6537 error = got_worktree_histedit_postpone(worktree,
6538 fileindex);
6539 goto done;
6542 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6543 error = histedit_skip_commit(hle, worktree, repo);
6544 if (error)
6545 goto done;
6546 continue;
6549 error = histedit_commit(&merged_paths, worktree, fileindex,
6550 tmp_branch, hle, repo);
6551 got_worktree_rebase_pathlist_free(&merged_paths);
6552 if (error)
6553 goto done;
6556 if (rebase_status == GOT_STATUS_CONFLICT) {
6557 error = got_worktree_histedit_postpone(worktree, fileindex);
6558 if (error)
6559 goto done;
6560 error = got_error_msg(GOT_ERR_CONFLICTS,
6561 "conflicts must be resolved before rebasing can continue");
6562 } else
6563 error = histedit_complete(worktree, fileindex, tmp_branch,
6564 branch, repo);
6565 done:
6566 got_object_id_queue_free(&commits);
6567 histedit_free_list(&histedit_cmds);
6568 free(head_commit_id);
6569 free(base_commit_id);
6570 free(resume_commit_id);
6571 if (commit)
6572 got_object_commit_close(commit);
6573 if (branch)
6574 got_ref_close(branch);
6575 if (tmp_branch)
6576 got_ref_close(tmp_branch);
6577 if (worktree)
6578 got_worktree_close(worktree);
6579 if (repo)
6580 got_repo_close(repo);
6581 return error;
6584 __dead static void
6585 usage_integrate(void)
6587 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6588 exit(1);
6591 static const struct got_error *
6592 cmd_integrate(int argc, char *argv[])
6594 const struct got_error *error = NULL;
6595 struct got_repository *repo = NULL;
6596 struct got_worktree *worktree = NULL;
6597 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6598 const char *branch_arg = NULL;
6599 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6600 struct got_fileindex *fileindex = NULL;
6601 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6602 int ch, did_something = 0;
6604 while ((ch = getopt(argc, argv, "")) != -1) {
6605 switch (ch) {
6606 default:
6607 usage_integrate();
6608 /* NOTREACHED */
6612 argc -= optind;
6613 argv += optind;
6615 if (argc != 1)
6616 usage_integrate();
6617 branch_arg = argv[0];
6619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6620 "unveil", NULL) == -1)
6621 err(1, "pledge");
6623 cwd = getcwd(NULL, 0);
6624 if (cwd == NULL) {
6625 error = got_error_from_errno("getcwd");
6626 goto done;
6629 error = got_worktree_open(&worktree, cwd);
6630 if (error)
6631 goto done;
6633 error = check_rebase_or_histedit_in_progress(worktree);
6634 if (error)
6635 goto done;
6637 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6638 NULL);
6639 if (error != NULL)
6640 goto done;
6642 error = apply_unveil(got_repo_get_path(repo), 0,
6643 got_worktree_get_root_path(worktree));
6644 if (error)
6645 goto done;
6647 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6648 error = got_error_from_errno("asprintf");
6649 goto done;
6652 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6653 &base_branch_ref, worktree, refname, repo);
6654 if (error)
6655 goto done;
6657 refname = strdup(got_ref_get_name(branch_ref));
6658 if (refname == NULL) {
6659 error = got_error_from_errno("strdup");
6660 got_worktree_integrate_abort(worktree, fileindex, repo,
6661 branch_ref, base_branch_ref);
6662 goto done;
6664 base_refname = strdup(got_ref_get_name(base_branch_ref));
6665 if (base_refname == NULL) {
6666 error = got_error_from_errno("strdup");
6667 got_worktree_integrate_abort(worktree, fileindex, repo,
6668 branch_ref, base_branch_ref);
6669 goto done;
6672 error = got_ref_resolve(&commit_id, repo, branch_ref);
6673 if (error)
6674 goto done;
6676 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6677 if (error)
6678 goto done;
6680 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6681 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6682 "specified branch has already been integrated");
6683 got_worktree_integrate_abort(worktree, fileindex, repo,
6684 branch_ref, base_branch_ref);
6685 goto done;
6688 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6689 if (error) {
6690 if (error->code == GOT_ERR_ANCESTRY)
6691 error = got_error(GOT_ERR_REBASE_REQUIRED);
6692 got_worktree_integrate_abort(worktree, fileindex, repo,
6693 branch_ref, base_branch_ref);
6694 goto done;
6697 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6698 branch_ref, base_branch_ref, update_progress, &did_something,
6699 check_cancelled, NULL);
6700 if (error)
6701 goto done;
6703 printf("Integrated %s into %s\n", refname, base_refname);
6704 done:
6705 if (repo)
6706 got_repo_close(repo);
6707 if (worktree)
6708 got_worktree_close(worktree);
6709 free(cwd);
6710 free(base_commit_id);
6711 free(commit_id);
6712 free(refname);
6713 free(base_refname);
6714 return error;
6717 __dead static void
6718 usage_stage(void)
6720 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6721 "[file-path ...]\n",
6722 getprogname());
6723 exit(1);
6726 static const struct got_error *
6727 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6728 const char *path, struct got_object_id *blob_id,
6729 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6731 const struct got_error *err = NULL;
6732 char *id_str = NULL;
6734 if (staged_status != GOT_STATUS_ADD &&
6735 staged_status != GOT_STATUS_MODIFY &&
6736 staged_status != GOT_STATUS_DELETE)
6737 return NULL;
6739 if (staged_status == GOT_STATUS_ADD ||
6740 staged_status == GOT_STATUS_MODIFY)
6741 err = got_object_id_str(&id_str, staged_blob_id);
6742 else
6743 err = got_object_id_str(&id_str, blob_id);
6744 if (err)
6745 return err;
6747 printf("%s %c %s\n", id_str, staged_status, path);
6748 free(id_str);
6749 return NULL;
6752 static const struct got_error *
6753 cmd_stage(int argc, char *argv[])
6755 const struct got_error *error = NULL;
6756 struct got_repository *repo = NULL;
6757 struct got_worktree *worktree = NULL;
6758 char *cwd = NULL;
6759 struct got_pathlist_head paths;
6760 struct got_pathlist_entry *pe;
6761 int ch, list_stage = 0, pflag = 0;
6762 FILE *patch_script_file = NULL;
6763 const char *patch_script_path = NULL;
6764 struct choose_patch_arg cpa;
6766 TAILQ_INIT(&paths);
6768 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6769 switch (ch) {
6770 case 'l':
6771 list_stage = 1;
6772 break;
6773 case 'p':
6774 pflag = 1;
6775 break;
6776 case 'F':
6777 patch_script_path = optarg;
6778 break;
6779 default:
6780 usage_stage();
6781 /* NOTREACHED */
6785 argc -= optind;
6786 argv += optind;
6788 #ifndef PROFILE
6789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6790 "unveil", NULL) == -1)
6791 err(1, "pledge");
6792 #endif
6793 if (list_stage && (pflag || patch_script_path))
6794 errx(1, "-l option cannot be used with other options");
6795 if (patch_script_path && !pflag)
6796 errx(1, "-F option can only be used together with -p option");
6798 cwd = getcwd(NULL, 0);
6799 if (cwd == NULL) {
6800 error = got_error_from_errno("getcwd");
6801 goto done;
6804 error = got_worktree_open(&worktree, cwd);
6805 if (error)
6806 goto done;
6808 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6809 NULL);
6810 if (error != NULL)
6811 goto done;
6813 if (patch_script_path) {
6814 patch_script_file = fopen(patch_script_path, "r");
6815 if (patch_script_file == NULL) {
6816 error = got_error_from_errno2("fopen",
6817 patch_script_path);
6818 goto done;
6821 error = apply_unveil(got_repo_get_path(repo), 0,
6822 got_worktree_get_root_path(worktree));
6823 if (error)
6824 goto done;
6826 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6827 if (error)
6828 goto done;
6830 if (list_stage)
6831 error = got_worktree_status(worktree, &paths, repo,
6832 print_stage, NULL, check_cancelled, NULL);
6833 else {
6834 cpa.patch_script_file = patch_script_file;
6835 cpa.action = "stage";
6836 error = got_worktree_stage(worktree, &paths,
6837 pflag ? NULL : print_status, NULL,
6838 pflag ? choose_patch : NULL, &cpa, repo);
6840 done:
6841 if (patch_script_file && fclose(patch_script_file) == EOF &&
6842 error == NULL)
6843 error = got_error_from_errno2("fclose", patch_script_path);
6844 if (repo)
6845 got_repo_close(repo);
6846 if (worktree)
6847 got_worktree_close(worktree);
6848 TAILQ_FOREACH(pe, &paths, entry)
6849 free((char *)pe->path);
6850 got_pathlist_free(&paths);
6851 free(cwd);
6852 return error;
6855 __dead static void
6856 usage_unstage(void)
6858 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6859 "[file-path ...]\n",
6860 getprogname());
6861 exit(1);
6865 static const struct got_error *
6866 cmd_unstage(int argc, char *argv[])
6868 const struct got_error *error = NULL;
6869 struct got_repository *repo = NULL;
6870 struct got_worktree *worktree = NULL;
6871 char *cwd = NULL;
6872 struct got_pathlist_head paths;
6873 struct got_pathlist_entry *pe;
6874 int ch, did_something = 0, pflag = 0;
6875 FILE *patch_script_file = NULL;
6876 const char *patch_script_path = NULL;
6877 struct choose_patch_arg cpa;
6879 TAILQ_INIT(&paths);
6881 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6882 switch (ch) {
6883 case 'p':
6884 pflag = 1;
6885 break;
6886 case 'F':
6887 patch_script_path = optarg;
6888 break;
6889 default:
6890 usage_unstage();
6891 /* NOTREACHED */
6895 argc -= optind;
6896 argv += optind;
6898 #ifndef PROFILE
6899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6900 "unveil", NULL) == -1)
6901 err(1, "pledge");
6902 #endif
6903 if (patch_script_path && !pflag)
6904 errx(1, "-F option can only be used together with -p option");
6906 cwd = getcwd(NULL, 0);
6907 if (cwd == NULL) {
6908 error = got_error_from_errno("getcwd");
6909 goto done;
6912 error = got_worktree_open(&worktree, cwd);
6913 if (error)
6914 goto done;
6916 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6917 NULL);
6918 if (error != NULL)
6919 goto done;
6921 if (patch_script_path) {
6922 patch_script_file = fopen(patch_script_path, "r");
6923 if (patch_script_file == NULL) {
6924 error = got_error_from_errno2("fopen",
6925 patch_script_path);
6926 goto done;
6930 error = apply_unveil(got_repo_get_path(repo), 0,
6931 got_worktree_get_root_path(worktree));
6932 if (error)
6933 goto done;
6935 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6936 if (error)
6937 goto done;
6939 cpa.patch_script_file = patch_script_file;
6940 cpa.action = "unstage";
6941 error = got_worktree_unstage(worktree, &paths, update_progress,
6942 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6943 done:
6944 if (patch_script_file && fclose(patch_script_file) == EOF &&
6945 error == NULL)
6946 error = got_error_from_errno2("fclose", patch_script_path);
6947 if (repo)
6948 got_repo_close(repo);
6949 if (worktree)
6950 got_worktree_close(worktree);
6951 TAILQ_FOREACH(pe, &paths, entry)
6952 free((char *)pe->path);
6953 got_pathlist_free(&paths);
6954 free(cwd);
6955 return error;
6958 __dead static void
6959 usage_cat(void)
6961 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6962 "arg1 [arg2 ...]\n", getprogname());
6963 exit(1);
6966 static const struct got_error *
6967 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6969 const struct got_error *err;
6970 struct got_blob_object *blob;
6972 err = got_object_open_as_blob(&blob, repo, id, 8192);
6973 if (err)
6974 return err;
6976 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6977 got_object_blob_close(blob);
6978 return err;
6981 static const struct got_error *
6982 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6984 const struct got_error *err;
6985 struct got_tree_object *tree;
6986 int nentries, i;
6988 err = got_object_open_as_tree(&tree, repo, id);
6989 if (err)
6990 return err;
6992 nentries = got_object_tree_get_nentries(tree);
6993 for (i = 0; i < nentries; i++) {
6994 struct got_tree_entry *te;
6995 char *id_str;
6996 if (sigint_received || sigpipe_received)
6997 break;
6998 te = got_object_tree_get_entry(tree, i);
6999 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7000 if (err)
7001 break;
7002 fprintf(outfile, "%s %.7o %s\n", id_str,
7003 got_tree_entry_get_mode(te),
7004 got_tree_entry_get_name(te));
7005 free(id_str);
7008 got_object_tree_close(tree);
7009 return err;
7012 static const struct got_error *
7013 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7015 const struct got_error *err;
7016 struct got_commit_object *commit;
7017 const struct got_object_id_queue *parent_ids;
7018 struct got_object_qid *pid;
7019 char *id_str = NULL;
7020 const char *logmsg = NULL;
7022 err = got_object_open_as_commit(&commit, repo, id);
7023 if (err)
7024 return err;
7026 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7027 if (err)
7028 goto done;
7030 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7031 parent_ids = got_object_commit_get_parent_ids(commit);
7032 fprintf(outfile, "numparents %d\n",
7033 got_object_commit_get_nparents(commit));
7034 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7035 char *pid_str;
7036 err = got_object_id_str(&pid_str, pid->id);
7037 if (err)
7038 goto done;
7039 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7040 free(pid_str);
7042 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7043 got_object_commit_get_author(commit),
7044 got_object_commit_get_author_time(commit));
7046 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7047 got_object_commit_get_author(commit),
7048 got_object_commit_get_committer_time(commit));
7050 logmsg = got_object_commit_get_logmsg_raw(commit);
7051 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7052 fprintf(outfile, "%s", logmsg);
7053 done:
7054 free(id_str);
7055 got_object_commit_close(commit);
7056 return err;
7059 static const struct got_error *
7060 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7062 const struct got_error *err;
7063 struct got_tag_object *tag;
7064 char *id_str = NULL;
7065 const char *tagmsg = NULL;
7067 err = got_object_open_as_tag(&tag, repo, id);
7068 if (err)
7069 return err;
7071 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7072 if (err)
7073 goto done;
7075 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7077 switch (got_object_tag_get_object_type(tag)) {
7078 case GOT_OBJ_TYPE_BLOB:
7079 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7080 GOT_OBJ_LABEL_BLOB);
7081 break;
7082 case GOT_OBJ_TYPE_TREE:
7083 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7084 GOT_OBJ_LABEL_TREE);
7085 break;
7086 case GOT_OBJ_TYPE_COMMIT:
7087 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7088 GOT_OBJ_LABEL_COMMIT);
7089 break;
7090 case GOT_OBJ_TYPE_TAG:
7091 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7092 GOT_OBJ_LABEL_TAG);
7093 break;
7094 default:
7095 break;
7098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7099 got_object_tag_get_name(tag));
7101 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7102 got_object_tag_get_tagger(tag),
7103 got_object_tag_get_tagger_time(tag));
7105 tagmsg = got_object_tag_get_message(tag);
7106 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7107 fprintf(outfile, "%s", tagmsg);
7108 done:
7109 free(id_str);
7110 got_object_tag_close(tag);
7111 return err;
7114 static const struct got_error *
7115 cmd_cat(int argc, char *argv[])
7117 const struct got_error *error;
7118 struct got_repository *repo = NULL;
7119 struct got_worktree *worktree = NULL;
7120 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7121 const char *commit_id_str = NULL;
7122 struct got_object_id *id = NULL, *commit_id = NULL;
7123 int ch, obj_type, i, force_path = 0;
7125 #ifndef PROFILE
7126 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7127 NULL) == -1)
7128 err(1, "pledge");
7129 #endif
7131 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7132 switch (ch) {
7133 case 'c':
7134 commit_id_str = optarg;
7135 break;
7136 case 'r':
7137 repo_path = realpath(optarg, NULL);
7138 if (repo_path == NULL)
7139 return got_error_from_errno2("realpath",
7140 optarg);
7141 got_path_strip_trailing_slashes(repo_path);
7142 break;
7143 case 'P':
7144 force_path = 1;
7145 break;
7146 default:
7147 usage_cat();
7148 /* NOTREACHED */
7152 argc -= optind;
7153 argv += optind;
7155 cwd = getcwd(NULL, 0);
7156 if (cwd == NULL) {
7157 error = got_error_from_errno("getcwd");
7158 goto done;
7160 error = got_worktree_open(&worktree, cwd);
7161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7162 goto done;
7163 if (worktree) {
7164 if (repo_path == NULL) {
7165 repo_path = strdup(
7166 got_worktree_get_repo_path(worktree));
7167 if (repo_path == NULL) {
7168 error = got_error_from_errno("strdup");
7169 goto done;
7174 if (repo_path == NULL) {
7175 repo_path = getcwd(NULL, 0);
7176 if (repo_path == NULL)
7177 return got_error_from_errno("getcwd");
7180 error = got_repo_open(&repo, repo_path, NULL);
7181 free(repo_path);
7182 if (error != NULL)
7183 goto done;
7185 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7186 if (error)
7187 goto done;
7189 if (commit_id_str == NULL)
7190 commit_id_str = GOT_REF_HEAD;
7191 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7192 if (error)
7193 goto done;
7195 for (i = 0; i < argc; i++) {
7196 if (force_path) {
7197 error = got_object_id_by_path(&id, repo, commit_id,
7198 argv[i]);
7199 if (error)
7200 break;
7201 } else {
7202 error = match_object_id(&id, &label, argv[i],
7203 GOT_OBJ_TYPE_ANY, 0, repo);
7204 if (error) {
7205 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7206 error->code != GOT_ERR_NOT_REF)
7207 break;
7208 error = got_object_id_by_path(&id, repo,
7209 commit_id, argv[i]);
7210 if (error)
7211 break;
7215 error = got_object_get_type(&obj_type, repo, id);
7216 if (error)
7217 break;
7219 switch (obj_type) {
7220 case GOT_OBJ_TYPE_BLOB:
7221 error = cat_blob(id, repo, stdout);
7222 break;
7223 case GOT_OBJ_TYPE_TREE:
7224 error = cat_tree(id, repo, stdout);
7225 break;
7226 case GOT_OBJ_TYPE_COMMIT:
7227 error = cat_commit(id, repo, stdout);
7228 break;
7229 case GOT_OBJ_TYPE_TAG:
7230 error = cat_tag(id, repo, stdout);
7231 break;
7232 default:
7233 error = got_error(GOT_ERR_OBJ_TYPE);
7234 break;
7236 if (error)
7237 break;
7238 free(label);
7239 label = NULL;
7240 free(id);
7241 id = NULL;
7244 done:
7245 free(label);
7246 free(id);
7247 free(commit_id);
7248 if (worktree)
7249 got_worktree_close(worktree);
7250 if (repo) {
7251 const struct got_error *repo_error;
7252 repo_error = got_repo_close(repo);
7253 if (error == NULL)
7254 error = repo_error;
7256 return error;