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 goto done;
5454 } else {
5455 /* Fast-forward the reference of the branch. */
5456 struct got_object_id *new_head_commit_id;
5457 char *id_str;
5458 error = got_ref_resolve(&new_head_commit_id, repo,
5459 new_base_branch);
5460 if (error)
5461 goto done;
5462 error = got_object_id_str(&id_str, new_head_commit_id);
5463 printf("Forwarding %s to commit %s\n",
5464 got_ref_get_name(branch), id_str);
5465 free(id_str);
5466 error = got_ref_change_ref(branch,
5467 new_head_commit_id);
5468 if (error)
5469 goto done;
5473 pid = NULL;
5474 SIMPLEQ_FOREACH(qid, &commits, entry) {
5475 commit_id = qid->id;
5476 parent_id = pid ? pid->id : yca_id;
5477 pid = qid;
5479 error = got_worktree_rebase_merge_files(&merged_paths,
5480 worktree, fileindex, parent_id, commit_id, repo,
5481 rebase_progress, &rebase_status, check_cancelled, NULL);
5482 if (error)
5483 goto done;
5485 if (rebase_status == GOT_STATUS_CONFLICT) {
5486 got_worktree_rebase_pathlist_free(&merged_paths);
5487 break;
5490 error = rebase_commit(&merged_paths, worktree, fileindex,
5491 tmp_branch, commit_id, repo);
5492 got_worktree_rebase_pathlist_free(&merged_paths);
5493 if (error)
5494 goto done;
5497 if (rebase_status == GOT_STATUS_CONFLICT) {
5498 error = got_worktree_rebase_postpone(worktree, fileindex);
5499 if (error)
5500 goto done;
5501 error = got_error_msg(GOT_ERR_CONFLICTS,
5502 "conflicts must be resolved before rebasing can continue");
5503 } else
5504 error = rebase_complete(worktree, fileindex, branch,
5505 new_base_branch, tmp_branch, repo);
5506 done:
5507 got_object_id_queue_free(&commits);
5508 free(branch_head_commit_id);
5509 free(resume_commit_id);
5510 free(yca_id);
5511 if (commit)
5512 got_object_commit_close(commit);
5513 if (branch)
5514 got_ref_close(branch);
5515 if (new_base_branch)
5516 got_ref_close(new_base_branch);
5517 if (tmp_branch)
5518 got_ref_close(tmp_branch);
5519 if (worktree)
5520 got_worktree_close(worktree);
5521 if (repo)
5522 got_repo_close(repo);
5523 return error;
5526 __dead static void
5527 usage_histedit(void)
5529 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5530 getprogname());
5531 exit(1);
5534 #define GOT_HISTEDIT_PICK 'p'
5535 #define GOT_HISTEDIT_EDIT 'e'
5536 #define GOT_HISTEDIT_FOLD 'f'
5537 #define GOT_HISTEDIT_DROP 'd'
5538 #define GOT_HISTEDIT_MESG 'm'
5540 static struct got_histedit_cmd {
5541 unsigned char code;
5542 const char *name;
5543 const char *desc;
5544 } got_histedit_cmds[] = {
5545 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5546 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5547 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5548 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5549 { GOT_HISTEDIT_MESG, "mesg",
5550 "single-line log message for commit above (open editor if empty)" },
5553 struct got_histedit_list_entry {
5554 TAILQ_ENTRY(got_histedit_list_entry) entry;
5555 struct got_object_id *commit_id;
5556 const struct got_histedit_cmd *cmd;
5557 char *logmsg;
5559 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5561 static const struct got_error *
5562 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5563 FILE *f, struct got_repository *repo)
5565 const struct got_error *err = NULL;
5566 char *logmsg = NULL, *id_str = NULL;
5567 struct got_commit_object *commit = NULL;
5568 int n;
5570 err = got_object_open_as_commit(&commit, repo, commit_id);
5571 if (err)
5572 goto done;
5574 err = get_short_logmsg(&logmsg, 34, commit);
5575 if (err)
5576 goto done;
5578 err = got_object_id_str(&id_str, commit_id);
5579 if (err)
5580 goto done;
5582 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5583 if (n < 0)
5584 err = got_ferror(f, GOT_ERR_IO);
5585 done:
5586 if (commit)
5587 got_object_commit_close(commit);
5588 free(id_str);
5589 free(logmsg);
5590 return err;
5593 static const struct got_error *
5594 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5595 struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 struct got_object_qid *qid;
5600 if (SIMPLEQ_EMPTY(commits))
5601 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5603 SIMPLEQ_FOREACH(qid, commits, entry) {
5604 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5605 f, repo);
5606 if (err)
5607 break;
5610 return err;
5613 static const struct got_error *
5614 write_cmd_list(FILE *f)
5616 const struct got_error *err = NULL;
5617 int n, i;
5619 n = fprintf(f, "# Available histedit commands:\n");
5620 if (n < 0)
5621 return got_ferror(f, GOT_ERR_IO);
5623 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5624 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5625 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5626 cmd->desc);
5627 if (n < 0) {
5628 err = got_ferror(f, GOT_ERR_IO);
5629 break;
5632 n = fprintf(f, "# Commits will be processed in order from top to "
5633 "bottom of this file.\n");
5634 if (n < 0)
5635 return got_ferror(f, GOT_ERR_IO);
5636 return err;
5639 static const struct got_error *
5640 histedit_syntax_error(int lineno)
5642 static char msg[42];
5643 int ret;
5645 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5646 lineno);
5647 if (ret == -1 || ret >= sizeof(msg))
5648 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5650 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5653 static const struct got_error *
5654 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5655 char *logmsg, struct got_repository *repo)
5657 const struct got_error *err;
5658 struct got_commit_object *folded_commit = NULL;
5659 char *id_str, *folded_logmsg = NULL;
5661 err = got_object_id_str(&id_str, hle->commit_id);
5662 if (err)
5663 return err;
5665 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5666 if (err)
5667 goto done;
5669 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5670 if (err)
5671 goto done;
5672 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5673 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5674 folded_logmsg) == -1) {
5675 err = got_error_from_errno("asprintf");
5676 goto done;
5678 done:
5679 if (folded_commit)
5680 got_object_commit_close(folded_commit);
5681 free(id_str);
5682 free(folded_logmsg);
5683 return err;
5686 static struct got_histedit_list_entry *
5687 get_folded_commits(struct got_histedit_list_entry *hle)
5689 struct got_histedit_list_entry *prev, *folded = NULL;
5691 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5692 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5693 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5694 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5695 folded = prev;
5696 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5699 return folded;
5702 static const struct got_error *
5703 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5704 struct got_repository *repo)
5706 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5707 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5708 const struct got_error *err = NULL;
5709 struct got_commit_object *commit = NULL;
5710 int fd;
5711 struct got_histedit_list_entry *folded = NULL;
5713 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5714 if (err)
5715 return err;
5717 folded = get_folded_commits(hle);
5718 if (folded) {
5719 while (folded != hle) {
5720 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5721 folded = TAILQ_NEXT(folded, entry);
5722 continue;
5724 err = append_folded_commit_msg(&new_msg, folded,
5725 logmsg, repo);
5726 if (err)
5727 goto done;
5728 free(logmsg);
5729 logmsg = new_msg;
5730 folded = TAILQ_NEXT(folded, entry);
5734 err = got_object_id_str(&id_str, hle->commit_id);
5735 if (err)
5736 goto done;
5737 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5738 if (err)
5739 goto done;
5740 if (asprintf(&new_msg,
5741 "%s\n# original log message of commit %s: %s",
5742 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5743 err = got_error_from_errno("asprintf");
5744 goto done;
5746 free(logmsg);
5747 logmsg = new_msg;
5749 err = got_object_id_str(&id_str, hle->commit_id);
5750 if (err)
5751 goto done;
5753 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5754 if (err)
5755 goto done;
5757 dprintf(fd, logmsg);
5758 close(fd);
5760 err = get_editor(&editor);
5761 if (err)
5762 goto done;
5764 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5765 if (err) {
5766 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5767 goto done;
5768 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5770 done:
5771 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5772 err = got_error_from_errno2("unlink", logmsg_path);
5773 free(logmsg_path);
5774 free(logmsg);
5775 free(orig_logmsg);
5776 free(editor);
5777 if (commit)
5778 got_object_commit_close(commit);
5779 return err;
5782 static const struct got_error *
5783 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5784 FILE *f, struct got_repository *repo)
5786 const struct got_error *err = NULL;
5787 char *line = NULL, *p, *end;
5788 size_t size;
5789 ssize_t len;
5790 int lineno = 0, i;
5791 const struct got_histedit_cmd *cmd;
5792 struct got_object_id *commit_id = NULL;
5793 struct got_histedit_list_entry *hle = NULL;
5795 for (;;) {
5796 len = getline(&line, &size, f);
5797 if (len == -1) {
5798 const struct got_error *getline_err;
5799 if (feof(f))
5800 break;
5801 getline_err = got_error_from_errno("getline");
5802 err = got_ferror(f, getline_err->code);
5803 break;
5805 lineno++;
5806 p = line;
5807 while (isspace((unsigned char)p[0]))
5808 p++;
5809 if (p[0] == '#' || p[0] == '\0') {
5810 free(line);
5811 line = NULL;
5812 continue;
5814 cmd = NULL;
5815 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5816 cmd = &got_histedit_cmds[i];
5817 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5818 isspace((unsigned char)p[strlen(cmd->name)])) {
5819 p += strlen(cmd->name);
5820 break;
5822 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5823 p++;
5824 break;
5827 if (i == nitems(got_histedit_cmds)) {
5828 err = histedit_syntax_error(lineno);
5829 break;
5831 while (isspace((unsigned char)p[0]))
5832 p++;
5833 if (cmd->code == GOT_HISTEDIT_MESG) {
5834 if (hle == NULL || hle->logmsg != NULL) {
5835 err = got_error(GOT_ERR_HISTEDIT_CMD);
5836 break;
5838 if (p[0] == '\0') {
5839 err = histedit_edit_logmsg(hle, repo);
5840 if (err)
5841 break;
5842 } else {
5843 hle->logmsg = strdup(p);
5844 if (hle->logmsg == NULL) {
5845 err = got_error_from_errno("strdup");
5846 break;
5849 free(line);
5850 line = NULL;
5851 continue;
5852 } else {
5853 end = p;
5854 while (end[0] && !isspace((unsigned char)end[0]))
5855 end++;
5856 *end = '\0';
5858 err = got_object_resolve_id_str(&commit_id, repo, p);
5859 if (err) {
5860 /* override error code */
5861 err = histedit_syntax_error(lineno);
5862 break;
5865 hle = malloc(sizeof(*hle));
5866 if (hle == NULL) {
5867 err = got_error_from_errno("malloc");
5868 break;
5870 hle->cmd = cmd;
5871 hle->commit_id = commit_id;
5872 hle->logmsg = NULL;
5873 commit_id = NULL;
5874 free(line);
5875 line = NULL;
5876 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5879 free(line);
5880 free(commit_id);
5881 return err;
5884 static const struct got_error *
5885 histedit_check_script(struct got_histedit_list *histedit_cmds,
5886 struct got_object_id_queue *commits, struct got_repository *repo)
5888 const struct got_error *err = NULL;
5889 struct got_object_qid *qid;
5890 struct got_histedit_list_entry *hle;
5891 static char msg[80];
5892 char *id_str;
5894 if (TAILQ_EMPTY(histedit_cmds))
5895 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5896 "histedit script contains no commands");
5897 if (SIMPLEQ_EMPTY(commits))
5898 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5900 SIMPLEQ_FOREACH(qid, commits, entry) {
5901 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5902 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5903 break;
5905 if (hle == NULL) {
5906 err = got_object_id_str(&id_str, qid->id);
5907 if (err)
5908 return err;
5909 snprintf(msg, sizeof(msg),
5910 "commit %s missing from histedit script", id_str);
5911 free(id_str);
5912 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5916 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5917 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5918 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5919 "last commit in histedit script cannot be folded");
5921 return NULL;
5924 static const struct got_error *
5925 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5926 const char *path, struct got_object_id_queue *commits,
5927 struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 char *editor;
5931 FILE *f = NULL;
5933 err = get_editor(&editor);
5934 if (err)
5935 return err;
5937 if (spawn_editor(editor, path) == -1) {
5938 err = got_error_from_errno("failed spawning editor");
5939 goto done;
5942 f = fopen(path, "r");
5943 if (f == NULL) {
5944 err = got_error_from_errno("fopen");
5945 goto done;
5947 err = histedit_parse_list(histedit_cmds, f, repo);
5948 if (err)
5949 goto done;
5951 err = histedit_check_script(histedit_cmds, commits, repo);
5952 done:
5953 if (f && fclose(f) != 0 && err == NULL)
5954 err = got_error_from_errno("fclose");
5955 free(editor);
5956 return err;
5959 static const struct got_error *
5960 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5961 struct got_object_id_queue *, const char *, struct got_repository *);
5963 static const struct got_error *
5964 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5965 struct got_object_id_queue *commits, struct got_repository *repo)
5967 const struct got_error *err;
5968 FILE *f = NULL;
5969 char *path = NULL;
5971 err = got_opentemp_named(&path, &f, "got-histedit");
5972 if (err)
5973 return err;
5975 err = write_cmd_list(f);
5976 if (err)
5977 goto done;
5979 err = histedit_write_commit_list(commits, f, repo);
5980 if (err)
5981 goto done;
5983 if (fclose(f) != 0) {
5984 err = got_error_from_errno("fclose");
5985 goto done;
5987 f = NULL;
5989 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5990 if (err) {
5991 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5992 err->code != GOT_ERR_HISTEDIT_CMD)
5993 goto done;
5994 err = histedit_edit_list_retry(histedit_cmds, err,
5995 commits, path, repo);
5997 done:
5998 if (f && fclose(f) != 0 && err == NULL)
5999 err = got_error_from_errno("fclose");
6000 if (path && unlink(path) != 0 && err == NULL)
6001 err = got_error_from_errno2("unlink", path);
6002 free(path);
6003 return err;
6006 static const struct got_error *
6007 histedit_save_list(struct got_histedit_list *histedit_cmds,
6008 struct got_worktree *worktree, struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 char *path = NULL;
6012 FILE *f = NULL;
6013 struct got_histedit_list_entry *hle;
6014 struct got_commit_object *commit = NULL;
6016 err = got_worktree_get_histedit_script_path(&path, worktree);
6017 if (err)
6018 return err;
6020 f = fopen(path, "w");
6021 if (f == NULL) {
6022 err = got_error_from_errno2("fopen", path);
6023 goto done;
6025 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6026 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6027 repo);
6028 if (err)
6029 break;
6031 if (hle->logmsg) {
6032 int n = fprintf(f, "%c %s\n",
6033 GOT_HISTEDIT_MESG, hle->logmsg);
6034 if (n < 0) {
6035 err = got_ferror(f, GOT_ERR_IO);
6036 break;
6040 done:
6041 if (f && fclose(f) != 0 && err == NULL)
6042 err = got_error_from_errno("fclose");
6043 free(path);
6044 if (commit)
6045 got_object_commit_close(commit);
6046 return err;
6049 void
6050 histedit_free_list(struct got_histedit_list *histedit_cmds)
6052 struct got_histedit_list_entry *hle;
6054 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6055 TAILQ_REMOVE(histedit_cmds, hle, entry);
6056 free(hle);
6060 static const struct got_error *
6061 histedit_load_list(struct got_histedit_list *histedit_cmds,
6062 const char *path, struct got_repository *repo)
6064 const struct got_error *err = NULL;
6065 FILE *f = NULL;
6067 f = fopen(path, "r");
6068 if (f == NULL) {
6069 err = got_error_from_errno2("fopen", path);
6070 goto done;
6073 err = histedit_parse_list(histedit_cmds, f, repo);
6074 done:
6075 if (f && fclose(f) != 0 && err == NULL)
6076 err = got_error_from_errno("fclose");
6077 return err;
6080 static const struct got_error *
6081 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6082 const struct got_error *edit_err, struct got_object_id_queue *commits,
6083 const char *path, struct got_repository *repo)
6085 const struct got_error *err = NULL, *prev_err = edit_err;
6086 int resp = ' ';
6088 while (resp != 'c' && resp != 'r' && resp != 'a') {
6089 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6090 "or (a)bort: ", getprogname(), prev_err->msg);
6091 resp = getchar();
6092 if (resp == '\n')
6093 resp = getchar();
6094 if (resp == 'c') {
6095 histedit_free_list(histedit_cmds);
6096 err = histedit_run_editor(histedit_cmds, path, commits,
6097 repo);
6098 if (err) {
6099 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6100 err->code != GOT_ERR_HISTEDIT_CMD)
6101 break;
6102 prev_err = err;
6103 resp = ' ';
6104 continue;
6106 break;
6107 } else if (resp == 'r') {
6108 histedit_free_list(histedit_cmds);
6109 err = histedit_edit_script(histedit_cmds,
6110 commits, repo);
6111 if (err) {
6112 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6113 err->code != GOT_ERR_HISTEDIT_CMD)
6114 break;
6115 prev_err = err;
6116 resp = ' ';
6117 continue;
6119 break;
6120 } else if (resp == 'a') {
6121 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6122 break;
6123 } else
6124 printf("invalid response '%c'\n", resp);
6127 return err;
6130 static const struct got_error *
6131 histedit_complete(struct got_worktree *worktree,
6132 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6133 struct got_reference *branch, struct got_repository *repo)
6135 printf("Switching work tree to %s\n",
6136 got_ref_get_symref_target(branch));
6137 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6138 branch, repo);
6141 static const struct got_error *
6142 show_histedit_progress(struct got_commit_object *commit,
6143 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6145 const struct got_error *err;
6146 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6148 err = got_object_id_str(&old_id_str, hle->commit_id);
6149 if (err)
6150 goto done;
6152 if (new_id) {
6153 err = got_object_id_str(&new_id_str, new_id);
6154 if (err)
6155 goto done;
6158 old_id_str[12] = '\0';
6159 if (new_id_str)
6160 new_id_str[12] = '\0';
6162 if (hle->logmsg) {
6163 logmsg = strdup(hle->logmsg);
6164 if (logmsg == NULL) {
6165 err = got_error_from_errno("strdup");
6166 goto done;
6168 trim_logmsg(logmsg, 42);
6169 } else {
6170 err = get_short_logmsg(&logmsg, 42, commit);
6171 if (err)
6172 goto done;
6175 switch (hle->cmd->code) {
6176 case GOT_HISTEDIT_PICK:
6177 case GOT_HISTEDIT_EDIT:
6178 printf("%s -> %s: %s\n", old_id_str,
6179 new_id_str ? new_id_str : "no-op change", logmsg);
6180 break;
6181 case GOT_HISTEDIT_DROP:
6182 case GOT_HISTEDIT_FOLD:
6183 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6184 logmsg);
6185 break;
6186 default:
6187 break;
6190 done:
6191 free(old_id_str);
6192 free(new_id_str);
6193 return err;
6196 static const struct got_error *
6197 histedit_commit(struct got_pathlist_head *merged_paths,
6198 struct got_worktree *worktree, struct got_fileindex *fileindex,
6199 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6200 struct got_repository *repo)
6202 const struct got_error *err;
6203 struct got_commit_object *commit;
6204 struct got_object_id *new_commit_id;
6206 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6207 && hle->logmsg == NULL) {
6208 err = histedit_edit_logmsg(hle, repo);
6209 if (err)
6210 return err;
6213 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6214 if (err)
6215 return err;
6217 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6218 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6219 hle->logmsg, repo);
6220 if (err) {
6221 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6222 goto done;
6223 err = show_histedit_progress(commit, hle, NULL);
6224 } else {
6225 err = show_histedit_progress(commit, hle, new_commit_id);
6226 free(new_commit_id);
6228 done:
6229 got_object_commit_close(commit);
6230 return err;
6233 static const struct got_error *
6234 histedit_skip_commit(struct got_histedit_list_entry *hle,
6235 struct got_worktree *worktree, struct got_repository *repo)
6237 const struct got_error *error;
6238 struct got_commit_object *commit;
6240 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6241 repo);
6242 if (error)
6243 return error;
6245 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6246 if (error)
6247 return error;
6249 error = show_histedit_progress(commit, hle, NULL);
6250 got_object_commit_close(commit);
6251 return error;
6254 static const struct got_error *
6255 cmd_histedit(int argc, char *argv[])
6257 const struct got_error *error = NULL;
6258 struct got_worktree *worktree = NULL;
6259 struct got_fileindex *fileindex = NULL;
6260 struct got_repository *repo = NULL;
6261 char *cwd = NULL;
6262 struct got_reference *branch = NULL;
6263 struct got_reference *tmp_branch = NULL;
6264 struct got_object_id *resume_commit_id = NULL;
6265 struct got_object_id *base_commit_id = NULL;
6266 struct got_object_id *head_commit_id = NULL;
6267 struct got_commit_object *commit = NULL;
6268 int ch, rebase_in_progress = 0, did_something;
6269 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6270 const char *edit_script_path = NULL;
6271 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6272 struct got_object_id_queue commits;
6273 struct got_pathlist_head merged_paths;
6274 const struct got_object_id_queue *parent_ids;
6275 struct got_object_qid *pid;
6276 struct got_histedit_list histedit_cmds;
6277 struct got_histedit_list_entry *hle;
6279 SIMPLEQ_INIT(&commits);
6280 TAILQ_INIT(&histedit_cmds);
6281 TAILQ_INIT(&merged_paths);
6283 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6284 switch (ch) {
6285 case 'a':
6286 abort_edit = 1;
6287 break;
6288 case 'c':
6289 continue_edit = 1;
6290 break;
6291 case 'F':
6292 edit_script_path = optarg;
6293 break;
6294 default:
6295 usage_histedit();
6296 /* NOTREACHED */
6300 argc -= optind;
6301 argv += optind;
6303 #ifndef PROFILE
6304 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6305 "unveil", NULL) == -1)
6306 err(1, "pledge");
6307 #endif
6308 if (abort_edit && continue_edit)
6309 usage_histedit();
6310 if (argc != 0)
6311 usage_histedit();
6314 * This command cannot apply unveil(2) in all cases because the
6315 * user may choose to run an editor to edit the histedit script
6316 * and to edit individual commit log messages.
6317 * unveil(2) traverses exec(2); if an editor is used we have to
6318 * apply unveil after edit script and log messages have been written.
6319 * XXX TODO: Make use of unveil(2) where possible.
6322 cwd = getcwd(NULL, 0);
6323 if (cwd == NULL) {
6324 error = got_error_from_errno("getcwd");
6325 goto done;
6327 error = got_worktree_open(&worktree, cwd);
6328 if (error)
6329 goto done;
6331 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6332 NULL);
6333 if (error != NULL)
6334 goto done;
6336 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6337 if (error)
6338 goto done;
6339 if (rebase_in_progress) {
6340 error = got_error(GOT_ERR_REBASING);
6341 goto done;
6344 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6345 if (error)
6346 goto done;
6348 if (edit_in_progress && abort_edit) {
6349 error = got_worktree_histedit_continue(&resume_commit_id,
6350 &tmp_branch, &branch, &base_commit_id, &fileindex,
6351 worktree, repo);
6352 if (error)
6353 goto done;
6354 printf("Switching work tree to %s\n",
6355 got_ref_get_symref_target(branch));
6356 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6357 branch, base_commit_id, update_progress, &did_something);
6358 if (error)
6359 goto done;
6360 printf("Histedit of %s aborted\n",
6361 got_ref_get_symref_target(branch));
6362 goto done; /* nothing else to do */
6363 } else if (abort_edit) {
6364 error = got_error(GOT_ERR_NOT_HISTEDIT);
6365 goto done;
6368 if (continue_edit) {
6369 char *path;
6371 if (!edit_in_progress) {
6372 error = got_error(GOT_ERR_NOT_HISTEDIT);
6373 goto done;
6376 error = got_worktree_get_histedit_script_path(&path, worktree);
6377 if (error)
6378 goto done;
6380 error = histedit_load_list(&histedit_cmds, path, repo);
6381 free(path);
6382 if (error)
6383 goto done;
6385 error = got_worktree_histedit_continue(&resume_commit_id,
6386 &tmp_branch, &branch, &base_commit_id, &fileindex,
6387 worktree, repo);
6388 if (error)
6389 goto done;
6391 error = got_ref_resolve(&head_commit_id, repo, branch);
6392 if (error)
6393 goto done;
6395 error = got_object_open_as_commit(&commit, repo,
6396 head_commit_id);
6397 if (error)
6398 goto done;
6399 parent_ids = got_object_commit_get_parent_ids(commit);
6400 pid = SIMPLEQ_FIRST(parent_ids);
6401 if (pid == NULL) {
6402 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6403 goto done;
6405 error = collect_commits(&commits, head_commit_id, pid->id,
6406 base_commit_id, got_worktree_get_path_prefix(worktree),
6407 GOT_ERR_HISTEDIT_PATH, repo);
6408 got_object_commit_close(commit);
6409 commit = NULL;
6410 if (error)
6411 goto done;
6412 } else {
6413 if (edit_in_progress) {
6414 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6415 goto done;
6418 error = got_ref_open(&branch, repo,
6419 got_worktree_get_head_ref_name(worktree), 0);
6420 if (error != NULL)
6421 goto done;
6423 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6424 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6425 "will not edit commit history of a branch outside "
6426 "the \"refs/heads/\" reference namespace");
6427 goto done;
6430 error = got_ref_resolve(&head_commit_id, repo, branch);
6431 got_ref_close(branch);
6432 branch = NULL;
6433 if (error)
6434 goto done;
6436 error = got_object_open_as_commit(&commit, repo,
6437 head_commit_id);
6438 if (error)
6439 goto done;
6440 parent_ids = got_object_commit_get_parent_ids(commit);
6441 pid = SIMPLEQ_FIRST(parent_ids);
6442 if (pid == NULL) {
6443 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6444 goto done;
6446 error = collect_commits(&commits, head_commit_id, pid->id,
6447 got_worktree_get_base_commit_id(worktree),
6448 got_worktree_get_path_prefix(worktree),
6449 GOT_ERR_HISTEDIT_PATH, repo);
6450 got_object_commit_close(commit);
6451 commit = NULL;
6452 if (error)
6453 goto done;
6455 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6456 &base_commit_id, &fileindex, worktree, repo);
6457 if (error)
6458 goto done;
6460 if (edit_script_path) {
6461 error = histedit_load_list(&histedit_cmds,
6462 edit_script_path, repo);
6463 if (error) {
6464 got_worktree_histedit_abort(worktree, fileindex,
6465 repo, branch, base_commit_id,
6466 update_progress, &did_something);
6467 goto done;
6469 } else {
6470 error = histedit_edit_script(&histedit_cmds, &commits,
6471 repo);
6472 if (error) {
6473 got_worktree_histedit_abort(worktree, fileindex,
6474 repo, branch, base_commit_id,
6475 update_progress, &did_something);
6476 goto done;
6481 error = histedit_save_list(&histedit_cmds, worktree,
6482 repo);
6483 if (error) {
6484 got_worktree_histedit_abort(worktree, fileindex,
6485 repo, branch, base_commit_id,
6486 update_progress, &did_something);
6487 goto done;
6492 error = histedit_check_script(&histedit_cmds, &commits, repo);
6493 if (error)
6494 goto done;
6496 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6497 if (resume_commit_id) {
6498 if (got_object_id_cmp(hle->commit_id,
6499 resume_commit_id) != 0)
6500 continue;
6502 resume_commit_id = NULL;
6503 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6504 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6505 error = histedit_skip_commit(hle, worktree,
6506 repo);
6507 } else {
6508 error = histedit_commit(NULL, worktree,
6509 fileindex, tmp_branch, hle, repo);
6511 if (error)
6512 goto done;
6513 continue;
6516 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6517 error = histedit_skip_commit(hle, worktree, repo);
6518 if (error)
6519 goto done;
6520 continue;
6523 error = got_object_open_as_commit(&commit, repo,
6524 hle->commit_id);
6525 if (error)
6526 goto done;
6527 parent_ids = got_object_commit_get_parent_ids(commit);
6528 pid = SIMPLEQ_FIRST(parent_ids);
6530 error = got_worktree_histedit_merge_files(&merged_paths,
6531 worktree, fileindex, pid->id, hle->commit_id, repo,
6532 rebase_progress, &rebase_status, check_cancelled, NULL);
6533 if (error)
6534 goto done;
6535 got_object_commit_close(commit);
6536 commit = NULL;
6538 if (rebase_status == GOT_STATUS_CONFLICT) {
6539 got_worktree_rebase_pathlist_free(&merged_paths);
6540 break;
6543 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6544 char *id_str;
6545 error = got_object_id_str(&id_str, hle->commit_id);
6546 if (error)
6547 goto done;
6548 printf("Stopping histedit for amending commit %s\n",
6549 id_str);
6550 free(id_str);
6551 got_worktree_rebase_pathlist_free(&merged_paths);
6552 error = got_worktree_histedit_postpone(worktree,
6553 fileindex);
6554 goto done;
6557 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6558 error = histedit_skip_commit(hle, worktree, repo);
6559 if (error)
6560 goto done;
6561 continue;
6564 error = histedit_commit(&merged_paths, worktree, fileindex,
6565 tmp_branch, hle, repo);
6566 got_worktree_rebase_pathlist_free(&merged_paths);
6567 if (error)
6568 goto done;
6571 if (rebase_status == GOT_STATUS_CONFLICT) {
6572 error = got_worktree_histedit_postpone(worktree, fileindex);
6573 if (error)
6574 goto done;
6575 error = got_error_msg(GOT_ERR_CONFLICTS,
6576 "conflicts must be resolved before rebasing can continue");
6577 } else
6578 error = histedit_complete(worktree, fileindex, tmp_branch,
6579 branch, repo);
6580 done:
6581 got_object_id_queue_free(&commits);
6582 histedit_free_list(&histedit_cmds);
6583 free(head_commit_id);
6584 free(base_commit_id);
6585 free(resume_commit_id);
6586 if (commit)
6587 got_object_commit_close(commit);
6588 if (branch)
6589 got_ref_close(branch);
6590 if (tmp_branch)
6591 got_ref_close(tmp_branch);
6592 if (worktree)
6593 got_worktree_close(worktree);
6594 if (repo)
6595 got_repo_close(repo);
6596 return error;
6599 __dead static void
6600 usage_integrate(void)
6602 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6603 exit(1);
6606 static const struct got_error *
6607 cmd_integrate(int argc, char *argv[])
6609 const struct got_error *error = NULL;
6610 struct got_repository *repo = NULL;
6611 struct got_worktree *worktree = NULL;
6612 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6613 const char *branch_arg = NULL;
6614 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6615 struct got_fileindex *fileindex = NULL;
6616 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6617 int ch, did_something = 0;
6619 while ((ch = getopt(argc, argv, "")) != -1) {
6620 switch (ch) {
6621 default:
6622 usage_integrate();
6623 /* NOTREACHED */
6627 argc -= optind;
6628 argv += optind;
6630 if (argc != 1)
6631 usage_integrate();
6632 branch_arg = argv[0];
6634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6635 "unveil", NULL) == -1)
6636 err(1, "pledge");
6638 cwd = getcwd(NULL, 0);
6639 if (cwd == NULL) {
6640 error = got_error_from_errno("getcwd");
6641 goto done;
6644 error = got_worktree_open(&worktree, cwd);
6645 if (error)
6646 goto done;
6648 error = check_rebase_or_histedit_in_progress(worktree);
6649 if (error)
6650 goto done;
6652 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6653 NULL);
6654 if (error != NULL)
6655 goto done;
6657 error = apply_unveil(got_repo_get_path(repo), 0,
6658 got_worktree_get_root_path(worktree));
6659 if (error)
6660 goto done;
6662 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6663 error = got_error_from_errno("asprintf");
6664 goto done;
6667 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6668 &base_branch_ref, worktree, refname, repo);
6669 if (error)
6670 goto done;
6672 refname = strdup(got_ref_get_name(branch_ref));
6673 if (refname == NULL) {
6674 error = got_error_from_errno("strdup");
6675 got_worktree_integrate_abort(worktree, fileindex, repo,
6676 branch_ref, base_branch_ref);
6677 goto done;
6679 base_refname = strdup(got_ref_get_name(base_branch_ref));
6680 if (base_refname == NULL) {
6681 error = got_error_from_errno("strdup");
6682 got_worktree_integrate_abort(worktree, fileindex, repo,
6683 branch_ref, base_branch_ref);
6684 goto done;
6687 error = got_ref_resolve(&commit_id, repo, branch_ref);
6688 if (error)
6689 goto done;
6691 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6692 if (error)
6693 goto done;
6695 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6696 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6697 "specified branch has already been integrated");
6698 got_worktree_integrate_abort(worktree, fileindex, repo,
6699 branch_ref, base_branch_ref);
6700 goto done;
6703 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6704 if (error) {
6705 if (error->code == GOT_ERR_ANCESTRY)
6706 error = got_error(GOT_ERR_REBASE_REQUIRED);
6707 got_worktree_integrate_abort(worktree, fileindex, repo,
6708 branch_ref, base_branch_ref);
6709 goto done;
6712 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6713 branch_ref, base_branch_ref, update_progress, &did_something,
6714 check_cancelled, NULL);
6715 if (error)
6716 goto done;
6718 printf("Integrated %s into %s\n", refname, base_refname);
6719 done:
6720 if (repo)
6721 got_repo_close(repo);
6722 if (worktree)
6723 got_worktree_close(worktree);
6724 free(cwd);
6725 free(base_commit_id);
6726 free(commit_id);
6727 free(refname);
6728 free(base_refname);
6729 return error;
6732 __dead static void
6733 usage_stage(void)
6735 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6736 "[file-path ...]\n",
6737 getprogname());
6738 exit(1);
6741 static const struct got_error *
6742 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6743 const char *path, struct got_object_id *blob_id,
6744 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6746 const struct got_error *err = NULL;
6747 char *id_str = NULL;
6749 if (staged_status != GOT_STATUS_ADD &&
6750 staged_status != GOT_STATUS_MODIFY &&
6751 staged_status != GOT_STATUS_DELETE)
6752 return NULL;
6754 if (staged_status == GOT_STATUS_ADD ||
6755 staged_status == GOT_STATUS_MODIFY)
6756 err = got_object_id_str(&id_str, staged_blob_id);
6757 else
6758 err = got_object_id_str(&id_str, blob_id);
6759 if (err)
6760 return err;
6762 printf("%s %c %s\n", id_str, staged_status, path);
6763 free(id_str);
6764 return NULL;
6767 static const struct got_error *
6768 cmd_stage(int argc, char *argv[])
6770 const struct got_error *error = NULL;
6771 struct got_repository *repo = NULL;
6772 struct got_worktree *worktree = NULL;
6773 char *cwd = NULL;
6774 struct got_pathlist_head paths;
6775 struct got_pathlist_entry *pe;
6776 int ch, list_stage = 0, pflag = 0;
6777 FILE *patch_script_file = NULL;
6778 const char *patch_script_path = NULL;
6779 struct choose_patch_arg cpa;
6781 TAILQ_INIT(&paths);
6783 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6784 switch (ch) {
6785 case 'l':
6786 list_stage = 1;
6787 break;
6788 case 'p':
6789 pflag = 1;
6790 break;
6791 case 'F':
6792 patch_script_path = optarg;
6793 break;
6794 default:
6795 usage_stage();
6796 /* NOTREACHED */
6800 argc -= optind;
6801 argv += optind;
6803 #ifndef PROFILE
6804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6805 "unveil", NULL) == -1)
6806 err(1, "pledge");
6807 #endif
6808 if (list_stage && (pflag || patch_script_path))
6809 errx(1, "-l option cannot be used with other options");
6810 if (patch_script_path && !pflag)
6811 errx(1, "-F option can only be used together with -p option");
6813 cwd = getcwd(NULL, 0);
6814 if (cwd == NULL) {
6815 error = got_error_from_errno("getcwd");
6816 goto done;
6819 error = got_worktree_open(&worktree, cwd);
6820 if (error)
6821 goto done;
6823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6824 NULL);
6825 if (error != NULL)
6826 goto done;
6828 if (patch_script_path) {
6829 patch_script_file = fopen(patch_script_path, "r");
6830 if (patch_script_file == NULL) {
6831 error = got_error_from_errno2("fopen",
6832 patch_script_path);
6833 goto done;
6836 error = apply_unveil(got_repo_get_path(repo), 0,
6837 got_worktree_get_root_path(worktree));
6838 if (error)
6839 goto done;
6841 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6842 if (error)
6843 goto done;
6845 if (list_stage)
6846 error = got_worktree_status(worktree, &paths, repo,
6847 print_stage, NULL, check_cancelled, NULL);
6848 else {
6849 cpa.patch_script_file = patch_script_file;
6850 cpa.action = "stage";
6851 error = got_worktree_stage(worktree, &paths,
6852 pflag ? NULL : print_status, NULL,
6853 pflag ? choose_patch : NULL, &cpa, repo);
6855 done:
6856 if (patch_script_file && fclose(patch_script_file) == EOF &&
6857 error == NULL)
6858 error = got_error_from_errno2("fclose", patch_script_path);
6859 if (repo)
6860 got_repo_close(repo);
6861 if (worktree)
6862 got_worktree_close(worktree);
6863 TAILQ_FOREACH(pe, &paths, entry)
6864 free((char *)pe->path);
6865 got_pathlist_free(&paths);
6866 free(cwd);
6867 return error;
6870 __dead static void
6871 usage_unstage(void)
6873 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6874 "[file-path ...]\n",
6875 getprogname());
6876 exit(1);
6880 static const struct got_error *
6881 cmd_unstage(int argc, char *argv[])
6883 const struct got_error *error = NULL;
6884 struct got_repository *repo = NULL;
6885 struct got_worktree *worktree = NULL;
6886 char *cwd = NULL;
6887 struct got_pathlist_head paths;
6888 struct got_pathlist_entry *pe;
6889 int ch, did_something = 0, pflag = 0;
6890 FILE *patch_script_file = NULL;
6891 const char *patch_script_path = NULL;
6892 struct choose_patch_arg cpa;
6894 TAILQ_INIT(&paths);
6896 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6897 switch (ch) {
6898 case 'p':
6899 pflag = 1;
6900 break;
6901 case 'F':
6902 patch_script_path = optarg;
6903 break;
6904 default:
6905 usage_unstage();
6906 /* NOTREACHED */
6910 argc -= optind;
6911 argv += optind;
6913 #ifndef PROFILE
6914 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6915 "unveil", NULL) == -1)
6916 err(1, "pledge");
6917 #endif
6918 if (patch_script_path && !pflag)
6919 errx(1, "-F option can only be used together with -p option");
6921 cwd = getcwd(NULL, 0);
6922 if (cwd == NULL) {
6923 error = got_error_from_errno("getcwd");
6924 goto done;
6927 error = got_worktree_open(&worktree, cwd);
6928 if (error)
6929 goto done;
6931 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6932 NULL);
6933 if (error != NULL)
6934 goto done;
6936 if (patch_script_path) {
6937 patch_script_file = fopen(patch_script_path, "r");
6938 if (patch_script_file == NULL) {
6939 error = got_error_from_errno2("fopen",
6940 patch_script_path);
6941 goto done;
6945 error = apply_unveil(got_repo_get_path(repo), 0,
6946 got_worktree_get_root_path(worktree));
6947 if (error)
6948 goto done;
6950 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6951 if (error)
6952 goto done;
6954 cpa.patch_script_file = patch_script_file;
6955 cpa.action = "unstage";
6956 error = got_worktree_unstage(worktree, &paths, update_progress,
6957 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6958 done:
6959 if (patch_script_file && fclose(patch_script_file) == EOF &&
6960 error == NULL)
6961 error = got_error_from_errno2("fclose", patch_script_path);
6962 if (repo)
6963 got_repo_close(repo);
6964 if (worktree)
6965 got_worktree_close(worktree);
6966 TAILQ_FOREACH(pe, &paths, entry)
6967 free((char *)pe->path);
6968 got_pathlist_free(&paths);
6969 free(cwd);
6970 return error;
6973 __dead static void
6974 usage_cat(void)
6976 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6977 "arg1 [arg2 ...]\n", getprogname());
6978 exit(1);
6981 static const struct got_error *
6982 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6984 const struct got_error *err;
6985 struct got_blob_object *blob;
6987 err = got_object_open_as_blob(&blob, repo, id, 8192);
6988 if (err)
6989 return err;
6991 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6992 got_object_blob_close(blob);
6993 return err;
6996 static const struct got_error *
6997 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6999 const struct got_error *err;
7000 struct got_tree_object *tree;
7001 int nentries, i;
7003 err = got_object_open_as_tree(&tree, repo, id);
7004 if (err)
7005 return err;
7007 nentries = got_object_tree_get_nentries(tree);
7008 for (i = 0; i < nentries; i++) {
7009 struct got_tree_entry *te;
7010 char *id_str;
7011 if (sigint_received || sigpipe_received)
7012 break;
7013 te = got_object_tree_get_entry(tree, i);
7014 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7015 if (err)
7016 break;
7017 fprintf(outfile, "%s %.7o %s\n", id_str,
7018 got_tree_entry_get_mode(te),
7019 got_tree_entry_get_name(te));
7020 free(id_str);
7023 got_object_tree_close(tree);
7024 return err;
7027 static const struct got_error *
7028 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7030 const struct got_error *err;
7031 struct got_commit_object *commit;
7032 const struct got_object_id_queue *parent_ids;
7033 struct got_object_qid *pid;
7034 char *id_str = NULL;
7035 const char *logmsg = NULL;
7037 err = got_object_open_as_commit(&commit, repo, id);
7038 if (err)
7039 return err;
7041 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7042 if (err)
7043 goto done;
7045 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7046 parent_ids = got_object_commit_get_parent_ids(commit);
7047 fprintf(outfile, "numparents %d\n",
7048 got_object_commit_get_nparents(commit));
7049 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7050 char *pid_str;
7051 err = got_object_id_str(&pid_str, pid->id);
7052 if (err)
7053 goto done;
7054 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7055 free(pid_str);
7057 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7058 got_object_commit_get_author(commit),
7059 got_object_commit_get_author_time(commit));
7061 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7062 got_object_commit_get_author(commit),
7063 got_object_commit_get_committer_time(commit));
7065 logmsg = got_object_commit_get_logmsg_raw(commit);
7066 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7067 fprintf(outfile, "%s", logmsg);
7068 done:
7069 free(id_str);
7070 got_object_commit_close(commit);
7071 return err;
7074 static const struct got_error *
7075 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7077 const struct got_error *err;
7078 struct got_tag_object *tag;
7079 char *id_str = NULL;
7080 const char *tagmsg = NULL;
7082 err = got_object_open_as_tag(&tag, repo, id);
7083 if (err)
7084 return err;
7086 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7087 if (err)
7088 goto done;
7090 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7092 switch (got_object_tag_get_object_type(tag)) {
7093 case GOT_OBJ_TYPE_BLOB:
7094 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7095 GOT_OBJ_LABEL_BLOB);
7096 break;
7097 case GOT_OBJ_TYPE_TREE:
7098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7099 GOT_OBJ_LABEL_TREE);
7100 break;
7101 case GOT_OBJ_TYPE_COMMIT:
7102 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7103 GOT_OBJ_LABEL_COMMIT);
7104 break;
7105 case GOT_OBJ_TYPE_TAG:
7106 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7107 GOT_OBJ_LABEL_TAG);
7108 break;
7109 default:
7110 break;
7113 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7114 got_object_tag_get_name(tag));
7116 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7117 got_object_tag_get_tagger(tag),
7118 got_object_tag_get_tagger_time(tag));
7120 tagmsg = got_object_tag_get_message(tag);
7121 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7122 fprintf(outfile, "%s", tagmsg);
7123 done:
7124 free(id_str);
7125 got_object_tag_close(tag);
7126 return err;
7129 static const struct got_error *
7130 cmd_cat(int argc, char *argv[])
7132 const struct got_error *error;
7133 struct got_repository *repo = NULL;
7134 struct got_worktree *worktree = NULL;
7135 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7136 const char *commit_id_str = NULL;
7137 struct got_object_id *id = NULL, *commit_id = NULL;
7138 int ch, obj_type, i, force_path = 0;
7140 #ifndef PROFILE
7141 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7142 NULL) == -1)
7143 err(1, "pledge");
7144 #endif
7146 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7147 switch (ch) {
7148 case 'c':
7149 commit_id_str = optarg;
7150 break;
7151 case 'r':
7152 repo_path = realpath(optarg, NULL);
7153 if (repo_path == NULL)
7154 return got_error_from_errno2("realpath",
7155 optarg);
7156 got_path_strip_trailing_slashes(repo_path);
7157 break;
7158 case 'P':
7159 force_path = 1;
7160 break;
7161 default:
7162 usage_cat();
7163 /* NOTREACHED */
7167 argc -= optind;
7168 argv += optind;
7170 cwd = getcwd(NULL, 0);
7171 if (cwd == NULL) {
7172 error = got_error_from_errno("getcwd");
7173 goto done;
7175 error = got_worktree_open(&worktree, cwd);
7176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7177 goto done;
7178 if (worktree) {
7179 if (repo_path == NULL) {
7180 repo_path = strdup(
7181 got_worktree_get_repo_path(worktree));
7182 if (repo_path == NULL) {
7183 error = got_error_from_errno("strdup");
7184 goto done;
7189 if (repo_path == NULL) {
7190 repo_path = getcwd(NULL, 0);
7191 if (repo_path == NULL)
7192 return got_error_from_errno("getcwd");
7195 error = got_repo_open(&repo, repo_path, NULL);
7196 free(repo_path);
7197 if (error != NULL)
7198 goto done;
7200 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7201 if (error)
7202 goto done;
7204 if (commit_id_str == NULL)
7205 commit_id_str = GOT_REF_HEAD;
7206 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7207 if (error)
7208 goto done;
7210 for (i = 0; i < argc; i++) {
7211 if (force_path) {
7212 error = got_object_id_by_path(&id, repo, commit_id,
7213 argv[i]);
7214 if (error)
7215 break;
7216 } else {
7217 error = match_object_id(&id, &label, argv[i],
7218 GOT_OBJ_TYPE_ANY, 0, repo);
7219 if (error) {
7220 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7221 error->code != GOT_ERR_NOT_REF)
7222 break;
7223 error = got_object_id_by_path(&id, repo,
7224 commit_id, argv[i]);
7225 if (error)
7226 break;
7230 error = got_object_get_type(&obj_type, repo, id);
7231 if (error)
7232 break;
7234 switch (obj_type) {
7235 case GOT_OBJ_TYPE_BLOB:
7236 error = cat_blob(id, repo, stdout);
7237 break;
7238 case GOT_OBJ_TYPE_TREE:
7239 error = cat_tree(id, repo, stdout);
7240 break;
7241 case GOT_OBJ_TYPE_COMMIT:
7242 error = cat_commit(id, repo, stdout);
7243 break;
7244 case GOT_OBJ_TYPE_TAG:
7245 error = cat_tag(id, repo, stdout);
7246 break;
7247 default:
7248 error = got_error(GOT_ERR_OBJ_TYPE);
7249 break;
7251 if (error)
7252 break;
7253 free(label);
7254 label = NULL;
7255 free(id);
7256 id = NULL;
7259 done:
7260 free(label);
7261 free(id);
7262 free(commit_id);
7263 if (worktree)
7264 got_worktree_close(worktree);
7265 if (repo) {
7266 const struct got_error *repo_error;
7267 repo_error = got_repo_close(repo);
7268 if (error == NULL)
7269 error = repo_error;
7271 return error;