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_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
152 };
154 static void
155 list_commands(void)
157 int i;
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
164 fputc('\n', stderr);
167 int
168 main(int argc, char *argv[])
170 struct got_cmd *cmd;
171 unsigned int i;
172 int ch;
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
178 switch (ch) {
179 case 'h':
180 hflag = 1;
181 break;
182 case 'V':
183 Vflag = 1;
184 break;
185 default:
186 usage(hflag);
187 /* NOTREACHED */
191 argc -= optind;
192 argv += optind;
193 optind = 0;
195 if (Vflag) {
196 got_version_print_str();
197 return 1;
200 if (argc <= 0)
201 usage(hflag);
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
213 continue;
215 if (hflag)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 return 1;
224 return 0;
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 list_commands();
229 return 1;
232 __dead static void
233 usage(int hflag)
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 getprogname());
237 if (hflag)
238 list_commands();
239 exit(1);
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
246 const char *editor;
248 *abspath = NULL;
250 editor = getenv("VISUAL");
251 if (editor == NULL)
252 editor = getenv("EDITOR");
254 if (editor) {
255 err = got_path_find_prog(abspath, editor);
256 if (err)
257 return err;
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
266 return NULL;
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
275 #ifdef PROFILE
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
278 #endif
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
289 if (err != NULL)
290 return err;
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
295 return NULL;
298 __dead static void
299 usage_init(void)
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 exit(1);
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
310 int ch;
312 while ((ch = getopt(argc, argv, "")) != -1) {
313 switch (ch) {
314 default:
315 usage_init();
316 /* NOTREACHED */
320 argc -= optind;
321 argv += optind;
323 #ifndef PROFILE
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
325 err(1, "pledge");
326 #endif
327 if (argc != 1)
328 usage_init();
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
337 if (error &&
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
339 goto done;
341 error = apply_unveil(repo_path, 0, NULL);
342 if (error)
343 goto done;
345 error = got_repo_init(repo_path);
346 if (error != NULL)
347 goto done;
349 done:
350 free(repo_path);
351 return error;
354 __dead static void
355 usage_import(void)
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
359 exit(1);
362 int
363 spawn_editor(const char *editor, const char *file)
365 pid_t pid;
366 sig_t sighup, sigint, sigquit;
367 int st = -1;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
374 case -1:
375 goto doneediting;
376 case 0:
377 execl(editor, editor, file, (char *)NULL);
378 _exit(127);
381 while (waitpid(pid, &st, 0) == -1)
382 if (errno != EINTR)
383 break;
385 doneediting:
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
391 errno = EINTR;
392 return -1;
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
403 char buf[1024];
404 struct stat st, st2;
405 FILE *fp;
406 int content_changed = 0;
407 size_t len;
409 *logmsg = NULL;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
425 if (*logmsg == NULL)
426 return got_error_from_errno("malloc");
427 (*logmsg)[0] = '\0';
428 len = 0;
430 fp = fopen(logmsg_path, "r");
431 if (fp == NULL) {
432 err = got_error_from_errno("fopen");
433 goto done;
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
437 content_changed = 1;
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
442 fclose(fp);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
446 len--;
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
452 done:
453 if (err) {
454 free(*logmsg);
455 *logmsg = NULL;
457 return err;
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
466 int fd;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
470 branch_name) == -1)
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 if (err)
475 goto done;
477 dprintf(fd, initial_content);
478 close(fd);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
481 done:
482 free(initial_content);
483 free(logmsg_path);
484 return err;
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
491 return NULL;
494 static const struct got_error *
495 get_author(const char **author)
497 const char *got_author;
499 *author = NULL;
501 got_author = getenv("GOT_AUTHOR");
502 if (got_author == NULL) {
503 /* TODO: Look up user in password database? */
504 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
507 *author = got_author;
509 /*
510 * Really dumb email address check; we're only doing this to
511 * avoid git's object parser breaking on commits we create.
512 */
513 while (*got_author && *got_author != '<')
514 got_author++;
515 if (*got_author != '<')
516 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 while (*got_author && *got_author != '@')
518 got_author++;
519 if (*got_author != '@')
520 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 while (*got_author && *got_author != '>')
522 got_author++;
523 if (*got_author != '>')
524 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
526 return NULL;
529 static const struct got_error *
530 cmd_import(int argc, char *argv[])
532 const struct got_error *error = NULL;
533 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
534 char *editor = NULL;
535 const char *author;
536 const char *branch_name = "master";
537 char *refname = NULL, *id_str = NULL;
538 struct got_repository *repo = NULL;
539 struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 struct got_object_id *new_commit_id = NULL;
541 int ch;
542 struct got_pathlist_head ignores;
543 struct got_pathlist_entry *pe;
545 TAILQ_INIT(&ignores);
547 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
548 switch (ch) {
549 case 'b':
550 branch_name = optarg;
551 break;
552 case 'm':
553 logmsg = strdup(optarg);
554 if (logmsg == NULL) {
555 error = got_error_from_errno("strdup");
556 goto done;
558 break;
559 case 'r':
560 repo_path = realpath(optarg, NULL);
561 if (repo_path == NULL) {
562 error = got_error_from_errno("realpath");
563 goto done;
565 break;
566 case 'I':
567 if (optarg[0] == '\0')
568 break;
569 error = got_pathlist_insert(&pe, &ignores, optarg,
570 NULL);
571 if (error)
572 goto done;
573 break;
574 default:
575 usage_import();
576 /* NOTREACHED */
580 argc -= optind;
581 argv += optind;
583 #ifndef PROFILE
584 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
585 NULL) == -1)
586 err(1, "pledge");
587 #endif
588 if (argc != 1)
589 usage_import();
591 error = get_author(&author);
592 if (error)
593 return error;
595 if (repo_path == NULL) {
596 repo_path = getcwd(NULL, 0);
597 if (repo_path == NULL)
598 return got_error_from_errno("getcwd");
600 got_path_strip_trailing_slashes(repo_path);
601 error = got_repo_open(&repo, repo_path);
602 if (error)
603 goto done;
605 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 error = got_error_from_errno("asprintf");
607 goto done;
610 error = got_ref_open(&branch_ref, repo, refname, 0);
611 if (error) {
612 if (error->code != GOT_ERR_NOT_REF)
613 goto done;
614 } else {
615 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 "import target branch already exists");
617 goto done;
620 path_dir = realpath(argv[0], NULL);
621 if (path_dir == NULL) {
622 error = got_error_from_errno("realpath");
623 goto done;
625 got_path_strip_trailing_slashes(path_dir);
627 /*
628 * unveil(2) traverses exec(2); if an editor is used we have
629 * to apply unveil after the log message has been written.
630 */
631 if (logmsg == NULL || strlen(logmsg) == 0) {
632 error = get_editor(&editor);
633 if (error)
634 goto done;
635 error = collect_import_msg(&logmsg, editor, path_dir, refname);
636 if (error)
637 goto done;
640 if (unveil(path_dir, "r") != 0)
641 return got_error_from_errno2("unveil", path_dir);
643 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
644 if (error)
645 goto done;
647 error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 author, &ignores, repo, import_progress, NULL);
649 if (error)
650 goto done;
652 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
653 if (error)
654 goto done;
656 error = got_ref_write(branch_ref, repo);
657 if (error)
658 goto done;
660 error = got_object_id_str(&id_str, new_commit_id);
661 if (error)
662 goto done;
664 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
665 if (error) {
666 if (error->code != GOT_ERR_NOT_REF)
667 goto done;
669 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
670 branch_ref);
671 if (error)
672 goto done;
674 error = got_ref_write(head_ref, repo);
675 if (error)
676 goto done;
679 printf("Created branch %s with commit %s\n",
680 got_ref_get_name(branch_ref), id_str);
681 done:
682 free(repo_path);
683 free(editor);
684 free(refname);
685 free(new_commit_id);
686 free(id_str);
687 if (branch_ref)
688 got_ref_close(branch_ref);
689 if (head_ref)
690 got_ref_close(head_ref);
691 return error;
694 __dead static void
695 usage_checkout(void)
697 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 "[-p prefix] repository-path [worktree-path]\n", getprogname());
699 exit(1);
702 static const struct got_error *
703 checkout_progress(void *arg, unsigned char status, const char *path)
705 char *worktree_path = arg;
707 /* Base commit bump happens silently. */
708 if (status == GOT_STATUS_BUMP_BASE)
709 return NULL;
711 while (path[0] == '/')
712 path++;
714 printf("%c %s/%s\n", status, worktree_path, path);
715 return NULL;
718 static const struct got_error *
719 check_cancelled(void *arg)
721 if (sigint_received || sigpipe_received)
722 return got_error(GOT_ERR_CANCELLED);
723 return NULL;
726 static const struct got_error *
727 check_linear_ancestry(struct got_object_id *commit_id,
728 struct got_object_id *base_commit_id, struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_object_id *yca_id;
733 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 commit_id, base_commit_id, repo, check_cancelled, NULL);
735 if (err)
736 return err;
738 if (yca_id == NULL)
739 return got_error(GOT_ERR_ANCESTRY);
741 /*
742 * Require a straight line of history between the target commit
743 * and the work tree's base commit.
745 * Non-linear situations such as this require a rebase:
747 * (commit) D F (base_commit)
748 * \ /
749 * C E
750 * \ /
751 * B (yca)
752 * |
753 * A
755 * 'got update' only handles linear cases:
756 * Update forwards in time: A (base/yca) - B - C - D (commit)
757 * Update backwards in time: D (base) - C - B - A (commit/yca)
758 */
759 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 got_object_id_cmp(base_commit_id, yca_id) != 0)
761 return got_error(GOT_ERR_ANCESTRY);
763 free(yca_id);
764 return NULL;
767 static const struct got_error *
768 check_same_branch(struct got_object_id *commit_id,
769 struct got_reference *head_ref, struct got_object_id *yca_id,
770 struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_commit_graph *graph = NULL;
774 struct got_object_id *head_commit_id = NULL;
775 int is_same_branch = 0;
777 err = got_ref_resolve(&head_commit_id, repo, head_ref);
778 if (err)
779 goto done;
781 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
782 is_same_branch = 1;
783 goto done;
785 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
786 is_same_branch = 1;
787 goto done;
790 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
791 if (err)
792 goto done;
794 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 check_cancelled, NULL);
796 if (err)
797 goto done;
799 for (;;) {
800 struct got_object_id *id;
801 err = got_commit_graph_iter_next(&id, graph);
802 if (err) {
803 if (err->code == GOT_ERR_ITER_COMPLETED) {
804 err = NULL;
805 break;
806 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
807 break;
808 err = got_commit_graph_fetch_commits(graph, 1,
809 repo, check_cancelled, NULL);
810 if (err)
811 break;
814 if (id) {
815 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
816 break;
817 if (got_object_id_cmp(id, commit_id) == 0) {
818 is_same_branch = 1;
819 break;
823 done:
824 if (graph)
825 got_commit_graph_close(graph);
826 free(head_commit_id);
827 if (!err && !is_same_branch)
828 err = got_error(GOT_ERR_ANCESTRY);
829 return err;
832 static const struct got_error *
833 resolve_commit_arg(struct got_object_id **commit_id,
834 const char *commit_id_arg, struct got_repository *repo)
836 const struct got_error *err;
837 struct got_reference *ref;
838 struct got_tag_object *tag;
840 err = got_repo_object_match_tag(&tag, commit_id_arg,
841 GOT_OBJ_TYPE_COMMIT, repo);
842 if (err == NULL) {
843 *commit_id = got_object_id_dup(
844 got_object_tag_get_object_id(tag));
845 if (*commit_id == NULL)
846 err = got_error_from_errno("got_object_id_dup");
847 got_object_tag_close(tag);
848 return err;
849 } else if (err->code != GOT_ERR_NO_OBJ)
850 return err;
852 err = got_ref_open(&ref, repo, commit_id_arg, 0);
853 if (err == NULL) {
854 err = got_ref_resolve(commit_id, repo, ref);
855 got_ref_close(ref);
856 } else {
857 if (err->code != GOT_ERR_NOT_REF)
858 return err;
859 err = got_repo_match_object_id_prefix(commit_id,
860 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
862 return err;
865 static const struct got_error *
866 cmd_checkout(int argc, char *argv[])
868 const struct got_error *error = NULL;
869 struct got_repository *repo = NULL;
870 struct got_reference *head_ref = NULL;
871 struct got_worktree *worktree = NULL;
872 char *repo_path = NULL;
873 char *worktree_path = NULL;
874 const char *path_prefix = "";
875 const char *branch_name = GOT_REF_HEAD;
876 char *commit_id_str = NULL;
877 int ch, same_path_prefix;
878 struct got_pathlist_head paths;
880 TAILQ_INIT(&paths);
882 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
883 switch (ch) {
884 case 'b':
885 branch_name = optarg;
886 break;
887 case 'c':
888 commit_id_str = strdup(optarg);
889 if (commit_id_str == NULL)
890 return got_error_from_errno("strdup");
891 break;
892 case 'p':
893 path_prefix = optarg;
894 break;
895 default:
896 usage_checkout();
897 /* NOTREACHED */
901 argc -= optind;
902 argv += optind;
904 #ifndef PROFILE
905 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 "unveil", NULL) == -1)
907 err(1, "pledge");
908 #endif
909 if (argc == 1) {
910 char *cwd, *base, *dotgit;
911 repo_path = realpath(argv[0], NULL);
912 if (repo_path == NULL)
913 return got_error_from_errno2("realpath", argv[0]);
914 cwd = getcwd(NULL, 0);
915 if (cwd == NULL) {
916 error = got_error_from_errno("getcwd");
917 goto done;
919 if (path_prefix[0]) {
920 base = basename(path_prefix);
921 if (base == NULL) {
922 error = got_error_from_errno2("basename",
923 path_prefix);
924 goto done;
926 } else {
927 base = basename(repo_path);
928 if (base == NULL) {
929 error = got_error_from_errno2("basename",
930 repo_path);
931 goto done;
934 dotgit = strstr(base, ".git");
935 if (dotgit)
936 *dotgit = '\0';
937 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 error = got_error_from_errno("asprintf");
939 free(cwd);
940 goto done;
942 free(cwd);
943 } else if (argc == 2) {
944 repo_path = realpath(argv[0], NULL);
945 if (repo_path == NULL) {
946 error = got_error_from_errno2("realpath", argv[0]);
947 goto done;
949 worktree_path = realpath(argv[1], NULL);
950 if (worktree_path == NULL) {
951 if (errno != ENOENT) {
952 error = got_error_from_errno2("realpath",
953 argv[1]);
954 goto done;
956 worktree_path = strdup(argv[1]);
957 if (worktree_path == NULL) {
958 error = got_error_from_errno("strdup");
959 goto done;
962 } else
963 usage_checkout();
965 got_path_strip_trailing_slashes(repo_path);
966 got_path_strip_trailing_slashes(worktree_path);
968 error = got_repo_open(&repo, repo_path);
969 if (error != NULL)
970 goto done;
972 /* Pre-create work tree path for unveil(2) */
973 error = got_path_mkdir(worktree_path);
974 if (error) {
975 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
977 goto done;
978 if (!got_path_dir_is_empty(worktree_path)) {
979 error = got_error_path(worktree_path,
980 GOT_ERR_DIR_NOT_EMPTY);
981 goto done;
985 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
986 if (error)
987 goto done;
989 error = got_ref_open(&head_ref, repo, branch_name, 0);
990 if (error != NULL)
991 goto done;
993 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
995 goto done;
997 error = got_worktree_open(&worktree, worktree_path);
998 if (error != NULL)
999 goto done;
1001 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1002 path_prefix);
1003 if (error != NULL)
1004 goto done;
1005 if (!same_path_prefix) {
1006 error = got_error(GOT_ERR_PATH_PREFIX);
1007 goto done;
1010 if (commit_id_str) {
1011 struct got_object_id *commit_id;
1012 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1013 if (error)
1014 goto done;
1015 error = check_linear_ancestry(commit_id,
1016 got_worktree_get_base_commit_id(worktree), repo);
1017 if (error != NULL) {
1018 free(commit_id);
1019 goto done;
1021 error = check_same_branch(commit_id, head_ref, NULL, repo);
1022 if (error)
1023 goto done;
1024 error = got_worktree_set_base_commit_id(worktree, repo,
1025 commit_id);
1026 free(commit_id);
1027 if (error)
1028 goto done;
1031 error = got_pathlist_append(&paths, "", NULL);
1032 if (error)
1033 goto done;
1034 error = got_worktree_checkout_files(worktree, &paths, repo,
1035 checkout_progress, worktree_path, check_cancelled, NULL);
1036 if (error != NULL)
1037 goto done;
1039 printf("Now shut up and hack\n");
1041 done:
1042 got_pathlist_free(&paths);
1043 free(commit_id_str);
1044 free(repo_path);
1045 free(worktree_path);
1046 return error;
1049 __dead static void
1050 usage_update(void)
1052 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1053 getprogname());
1054 exit(1);
1057 static const struct got_error *
1058 update_progress(void *arg, unsigned char status, const char *path)
1060 int *did_something = arg;
1062 if (status == GOT_STATUS_EXISTS)
1063 return NULL;
1065 *did_something = 1;
1067 /* Base commit bump happens silently. */
1068 if (status == GOT_STATUS_BUMP_BASE)
1069 return NULL;
1071 while (path[0] == '/')
1072 path++;
1073 printf("%c %s\n", status, path);
1074 return NULL;
1077 static const struct got_error *
1078 switch_head_ref(struct got_reference *head_ref,
1079 struct got_object_id *commit_id, struct got_worktree *worktree,
1080 struct got_repository *repo)
1082 const struct got_error *err = NULL;
1083 char *base_id_str;
1084 int ref_has_moved = 0;
1086 /* Trivial case: switching between two different references. */
1087 if (strcmp(got_ref_get_name(head_ref),
1088 got_worktree_get_head_ref_name(worktree)) != 0) {
1089 printf("Switching work tree from %s to %s\n",
1090 got_worktree_get_head_ref_name(worktree),
1091 got_ref_get_name(head_ref));
1092 return got_worktree_set_head_ref(worktree, head_ref);
1095 err = check_linear_ancestry(commit_id,
1096 got_worktree_get_base_commit_id(worktree), repo);
1097 if (err) {
1098 if (err->code != GOT_ERR_ANCESTRY)
1099 return err;
1100 ref_has_moved = 1;
1102 if (!ref_has_moved)
1103 return NULL;
1105 /* Switching to a rebased branch with the same reference name. */
1106 err = got_object_id_str(&base_id_str,
1107 got_worktree_get_base_commit_id(worktree));
1108 if (err)
1109 return err;
1110 printf("Reference %s now points at a different branch\n",
1111 got_worktree_get_head_ref_name(worktree));
1112 printf("Switching work tree from %s to %s\n", base_id_str,
1113 got_worktree_get_head_ref_name(worktree));
1114 return NULL;
1117 static const struct got_error *
1118 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1120 const struct got_error *err;
1121 int in_progress;
1123 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1124 if (err)
1125 return err;
1126 if (in_progress)
1127 return got_error(GOT_ERR_REBASING);
1129 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1130 if (err)
1131 return err;
1132 if (in_progress)
1133 return got_error(GOT_ERR_HISTEDIT_BUSY);
1135 return NULL;
1138 static const struct got_error *
1139 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 char *argv[], struct got_worktree *worktree)
1142 const struct got_error *err = NULL;
1143 char *path;
1144 int i;
1146 if (argc == 0) {
1147 path = strdup("");
1148 if (path == NULL)
1149 return got_error_from_errno("strdup");
1150 return got_pathlist_append(paths, path, NULL);
1153 for (i = 0; i < argc; i++) {
1154 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1155 if (err)
1156 break;
1157 err = got_pathlist_append(paths, path, NULL);
1158 if (err) {
1159 free(path);
1160 break;
1164 return err;
1167 static const struct got_error *
1168 cmd_update(int argc, char *argv[])
1170 const struct got_error *error = NULL;
1171 struct got_repository *repo = NULL;
1172 struct got_worktree *worktree = NULL;
1173 char *worktree_path = NULL;
1174 struct got_object_id *commit_id = NULL;
1175 char *commit_id_str = NULL;
1176 const char *branch_name = NULL;
1177 struct got_reference *head_ref = NULL;
1178 struct got_pathlist_head paths;
1179 struct got_pathlist_entry *pe;
1180 int ch, did_something = 0;
1182 TAILQ_INIT(&paths);
1184 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1185 switch (ch) {
1186 case 'b':
1187 branch_name = optarg;
1188 break;
1189 case 'c':
1190 commit_id_str = strdup(optarg);
1191 if (commit_id_str == NULL)
1192 return got_error_from_errno("strdup");
1193 break;
1194 default:
1195 usage_update();
1196 /* NOTREACHED */
1200 argc -= optind;
1201 argv += optind;
1203 #ifndef PROFILE
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1206 err(1, "pledge");
1207 #endif
1208 worktree_path = getcwd(NULL, 0);
1209 if (worktree_path == NULL) {
1210 error = got_error_from_errno("getcwd");
1211 goto done;
1213 error = got_worktree_open(&worktree, worktree_path);
1214 if (error)
1215 goto done;
1217 error = check_rebase_or_histedit_in_progress(worktree);
1218 if (error)
1219 goto done;
1221 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1222 if (error != NULL)
1223 goto done;
1225 error = apply_unveil(got_repo_get_path(repo), 0,
1226 got_worktree_get_root_path(worktree));
1227 if (error)
1228 goto done;
1230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1231 if (error)
1232 goto done;
1234 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 got_worktree_get_head_ref_name(worktree), 0);
1236 if (error != NULL)
1237 goto done;
1238 if (commit_id_str == NULL) {
1239 error = got_ref_resolve(&commit_id, repo, head_ref);
1240 if (error != NULL)
1241 goto done;
1242 error = got_object_id_str(&commit_id_str, commit_id);
1243 if (error != NULL)
1244 goto done;
1245 } else {
1246 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 free(commit_id_str);
1248 commit_id_str = NULL;
1249 if (error)
1250 goto done;
1251 error = got_object_id_str(&commit_id_str, commit_id);
1252 if (error)
1253 goto done;
1256 if (branch_name) {
1257 struct got_object_id *head_commit_id;
1258 TAILQ_FOREACH(pe, &paths, entry) {
1259 if (pe->path_len == 0)
1260 continue;
1261 error = got_error_msg(GOT_ERR_BAD_PATH,
1262 "switching between branches requires that "
1263 "the entire work tree gets updated");
1264 goto done;
1266 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1267 if (error)
1268 goto done;
1269 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 free(head_commit_id);
1271 if (error != NULL)
1272 goto done;
1273 error = check_same_branch(commit_id, head_ref, NULL, repo);
1274 if (error)
1275 goto done;
1276 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1277 if (error)
1278 goto done;
1279 } else {
1280 error = check_linear_ancestry(commit_id,
1281 got_worktree_get_base_commit_id(worktree), repo);
1282 if (error != NULL) {
1283 if (error->code == GOT_ERR_ANCESTRY)
1284 error = got_error(GOT_ERR_BRANCH_MOVED);
1285 goto done;
1287 error = check_same_branch(commit_id, head_ref, NULL, repo);
1288 if (error)
1289 goto done;
1292 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1293 commit_id) != 0) {
1294 error = got_worktree_set_base_commit_id(worktree, repo,
1295 commit_id);
1296 if (error)
1297 goto done;
1300 error = got_worktree_checkout_files(worktree, &paths, repo,
1301 update_progress, &did_something, check_cancelled, NULL);
1302 if (error != NULL)
1303 goto done;
1305 if (did_something)
1306 printf("Updated to commit %s\n", commit_id_str);
1307 else
1308 printf("Already up-to-date\n");
1309 done:
1310 free(worktree_path);
1311 TAILQ_FOREACH(pe, &paths, entry)
1312 free((char *)pe->path);
1313 got_pathlist_free(&paths);
1314 free(commit_id);
1315 free(commit_id_str);
1316 return error;
1319 static const struct got_error *
1320 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1321 int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_tree_object *tree1 = NULL, *tree2;
1325 struct got_object_qid *qid;
1326 char *id_str1 = NULL, *id_str2;
1327 struct got_diff_blob_output_unidiff_arg arg;
1329 err = got_object_open_as_tree(&tree2, repo,
1330 got_object_commit_get_tree_id(commit));
1331 if (err)
1332 return err;
1334 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1335 if (qid != NULL) {
1336 struct got_commit_object *pcommit;
1338 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1339 if (err)
1340 return err;
1342 err = got_object_open_as_tree(&tree1, repo,
1343 got_object_commit_get_tree_id(pcommit));
1344 got_object_commit_close(pcommit);
1345 if (err)
1346 return err;
1348 err = got_object_id_str(&id_str1, qid->id);
1349 if (err)
1350 return err;
1353 err = got_object_id_str(&id_str2, id);
1354 if (err)
1355 goto done;
1357 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1358 arg.diff_context = diff_context;
1359 arg.outfile = stdout;
1360 err = got_diff_tree(tree1, tree2, "", "", repo,
1361 got_diff_blob_output_unidiff, &arg, 1);
1362 done:
1363 if (tree1)
1364 got_object_tree_close(tree1);
1365 got_object_tree_close(tree2);
1366 free(id_str1);
1367 free(id_str2);
1368 return err;
1371 static char *
1372 get_datestr(time_t *time, char *datebuf)
1374 struct tm mytm, *tm;
1375 char *p, *s;
1377 tm = gmtime_r(time, &mytm);
1378 if (tm == NULL)
1379 return NULL;
1380 s = asctime_r(tm, datebuf);
1381 if (s == NULL)
1382 return NULL;
1383 p = strchr(s, '\n');
1384 if (p)
1385 *p = '\0';
1386 return s;
1389 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1391 static const struct got_error *
1392 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1393 struct got_repository *repo, int show_patch, int diff_context,
1394 struct got_reflist_head *refs)
1396 const struct got_error *err = NULL;
1397 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1398 char datebuf[26];
1399 time_t committer_time;
1400 const char *author, *committer;
1401 char *refs_str = NULL;
1402 struct got_reflist_entry *re;
1404 SIMPLEQ_FOREACH(re, refs, entry) {
1405 char *s;
1406 const char *name;
1407 struct got_tag_object *tag = NULL;
1408 int cmp;
1410 name = got_ref_get_name(re->ref);
1411 if (strcmp(name, GOT_REF_HEAD) == 0)
1412 continue;
1413 if (strncmp(name, "refs/", 5) == 0)
1414 name += 5;
1415 if (strncmp(name, "got/", 4) == 0)
1416 continue;
1417 if (strncmp(name, "heads/", 6) == 0)
1418 name += 6;
1419 if (strncmp(name, "remotes/", 8) == 0)
1420 name += 8;
1421 if (strncmp(name, "tags/", 5) == 0) {
1422 err = got_object_open_as_tag(&tag, repo, re->id);
1423 if (err) {
1424 if (err->code != GOT_ERR_OBJ_TYPE)
1425 return err;
1426 /* Ref points at something other than a tag. */
1427 err = NULL;
1428 tag = NULL;
1431 cmp = got_object_id_cmp(tag ?
1432 got_object_tag_get_object_id(tag) : re->id, id);
1433 if (tag)
1434 got_object_tag_close(tag);
1435 if (cmp != 0)
1436 continue;
1437 s = refs_str;
1438 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1439 name) == -1) {
1440 err = got_error_from_errno("asprintf");
1441 free(s);
1442 return err;
1444 free(s);
1446 err = got_object_id_str(&id_str, id);
1447 if (err)
1448 return err;
1450 printf(GOT_COMMIT_SEP_STR);
1451 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1452 refs_str ? refs_str : "", refs_str ? ")" : "");
1453 free(id_str);
1454 id_str = NULL;
1455 free(refs_str);
1456 refs_str = NULL;
1457 printf("from: %s\n", got_object_commit_get_author(commit));
1458 committer_time = got_object_commit_get_committer_time(commit);
1459 datestr = get_datestr(&committer_time, datebuf);
1460 if (datestr)
1461 printf("date: %s UTC\n", datestr);
1462 author = got_object_commit_get_author(commit);
1463 committer = got_object_commit_get_committer(commit);
1464 if (strcmp(author, committer) != 0)
1465 printf("via: %s\n", committer);
1466 if (got_object_commit_get_nparents(commit) > 1) {
1467 const struct got_object_id_queue *parent_ids;
1468 struct got_object_qid *qid;
1469 int n = 1;
1470 parent_ids = got_object_commit_get_parent_ids(commit);
1471 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1472 err = got_object_id_str(&id_str, qid->id);
1473 if (err)
1474 return err;
1475 printf("parent %d: %s\n", n++, id_str);
1476 free(id_str);
1480 err = got_object_commit_get_logmsg(&logmsg0, commit);
1481 if (err)
1482 return err;
1484 logmsg = logmsg0;
1485 do {
1486 line = strsep(&logmsg, "\n");
1487 if (line)
1488 printf(" %s\n", line);
1489 } while (line);
1490 free(logmsg0);
1492 if (show_patch) {
1493 err = print_patch(commit, id, diff_context, repo);
1494 if (err == 0)
1495 printf("\n");
1498 if (fflush(stdout) != 0 && err == NULL)
1499 err = got_error_from_errno("fflush");
1500 return err;
1503 static const struct got_error *
1504 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1505 char *path, int show_patch, int diff_context, int limit,
1506 int first_parent_traversal, struct got_reflist_head *refs)
1508 const struct got_error *err;
1509 struct got_commit_graph *graph;
1511 err = got_commit_graph_open(&graph, root_id, path,
1512 first_parent_traversal, repo);
1513 if (err)
1514 return err;
1515 err = got_commit_graph_iter_start(graph, root_id, repo,
1516 check_cancelled, NULL);
1517 if (err)
1518 goto done;
1519 for (;;) {
1520 struct got_commit_object *commit;
1521 struct got_object_id *id;
1523 if (sigint_received || sigpipe_received)
1524 break;
1526 err = got_commit_graph_iter_next(&id, graph);
1527 if (err) {
1528 if (err->code == GOT_ERR_ITER_COMPLETED) {
1529 err = NULL;
1530 break;
1532 if (err->code != GOT_ERR_ITER_NEED_MORE)
1533 break;
1534 err = got_commit_graph_fetch_commits(graph, 1, repo,
1535 check_cancelled, NULL);
1536 if (err)
1537 break;
1538 else
1539 continue;
1541 if (id == NULL)
1542 break;
1544 err = got_object_open_as_commit(&commit, repo, id);
1545 if (err)
1546 break;
1547 err = print_commit(commit, id, repo, show_patch, diff_context,
1548 refs);
1549 got_object_commit_close(commit);
1550 if (err || (limit && --limit == 0))
1551 break;
1553 done:
1554 got_commit_graph_close(graph);
1555 return err;
1558 __dead static void
1559 usage_log(void)
1561 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1562 "[-r repository-path] [path]\n", getprogname());
1563 exit(1);
1566 static int
1567 get_default_log_limit(void)
1569 const char *got_default_log_limit;
1570 long long n;
1571 const char *errstr;
1573 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1574 if (got_default_log_limit == NULL)
1575 return 0;
1576 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1577 if (errstr != NULL)
1578 return 0;
1579 return n;
1582 static const struct got_error *
1583 cmd_log(int argc, char *argv[])
1585 const struct got_error *error;
1586 struct got_repository *repo = NULL;
1587 struct got_worktree *worktree = NULL;
1588 struct got_commit_object *commit = NULL;
1589 struct got_object_id *id = NULL;
1590 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1591 char *start_commit = NULL;
1592 int diff_context = 3, ch;
1593 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1594 const char *errstr;
1595 struct got_reflist_head refs;
1597 SIMPLEQ_INIT(&refs);
1599 #ifndef PROFILE
1600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1601 NULL)
1602 == -1)
1603 err(1, "pledge");
1604 #endif
1606 limit = get_default_log_limit();
1608 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1609 switch (ch) {
1610 case 'p':
1611 show_patch = 1;
1612 break;
1613 case 'c':
1614 start_commit = optarg;
1615 break;
1616 case 'C':
1617 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1618 &errstr);
1619 if (errstr != NULL)
1620 err(1, "-C option %s", errstr);
1621 break;
1622 case 'l':
1623 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1624 if (errstr != NULL)
1625 err(1, "-l option %s", errstr);
1626 break;
1627 case 'f':
1628 first_parent_traversal = 1;
1629 break;
1630 case 'r':
1631 repo_path = realpath(optarg, NULL);
1632 if (repo_path == NULL)
1633 err(1, "-r option");
1634 got_path_strip_trailing_slashes(repo_path);
1635 break;
1636 default:
1637 usage_log();
1638 /* NOTREACHED */
1642 argc -= optind;
1643 argv += optind;
1645 cwd = getcwd(NULL, 0);
1646 if (cwd == NULL) {
1647 error = got_error_from_errno("getcwd");
1648 goto done;
1651 error = got_worktree_open(&worktree, cwd);
1652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1653 goto done;
1654 error = NULL;
1656 if (argc == 0) {
1657 path = strdup("");
1658 if (path == NULL) {
1659 error = got_error_from_errno("strdup");
1660 goto done;
1662 } else if (argc == 1) {
1663 if (worktree) {
1664 error = got_worktree_resolve_path(&path, worktree,
1665 argv[0]);
1666 if (error)
1667 goto done;
1668 } else {
1669 path = strdup(argv[0]);
1670 if (path == NULL) {
1671 error = got_error_from_errno("strdup");
1672 goto done;
1675 } else
1676 usage_log();
1678 if (repo_path == NULL) {
1679 repo_path = worktree ?
1680 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno("strdup");
1684 goto done;
1687 error = got_repo_open(&repo, repo_path);
1688 if (error != NULL)
1689 goto done;
1691 error = apply_unveil(got_repo_get_path(repo), 1,
1692 worktree ? got_worktree_get_root_path(worktree) : NULL);
1693 if (error)
1694 goto done;
1696 if (start_commit == NULL) {
1697 struct got_reference *head_ref;
1698 error = got_ref_open(&head_ref, repo,
1699 worktree ? got_worktree_get_head_ref_name(worktree)
1700 : GOT_REF_HEAD, 0);
1701 if (error != NULL)
1702 return error;
1703 error = got_ref_resolve(&id, repo, head_ref);
1704 got_ref_close(head_ref);
1705 if (error != NULL)
1706 return error;
1707 error = got_object_open_as_commit(&commit, repo, id);
1708 } else {
1709 struct got_reference *ref;
1710 error = got_ref_open(&ref, repo, start_commit, 0);
1711 if (error == NULL) {
1712 int obj_type;
1713 error = got_ref_resolve(&id, repo, ref);
1714 got_ref_close(ref);
1715 if (error != NULL)
1716 goto done;
1717 error = got_object_get_type(&obj_type, repo, id);
1718 if (error != NULL)
1719 goto done;
1720 if (obj_type == GOT_OBJ_TYPE_TAG) {
1721 struct got_tag_object *tag;
1722 error = got_object_open_as_tag(&tag, repo, id);
1723 if (error != NULL)
1724 goto done;
1725 if (got_object_tag_get_object_type(tag) !=
1726 GOT_OBJ_TYPE_COMMIT) {
1727 got_object_tag_close(tag);
1728 error = got_error(GOT_ERR_OBJ_TYPE);
1729 goto done;
1731 free(id);
1732 id = got_object_id_dup(
1733 got_object_tag_get_object_id(tag));
1734 if (id == NULL)
1735 error = got_error_from_errno(
1736 "got_object_id_dup");
1737 got_object_tag_close(tag);
1738 if (error)
1739 goto done;
1740 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1741 error = got_error(GOT_ERR_OBJ_TYPE);
1742 goto done;
1744 error = got_object_open_as_commit(&commit, repo, id);
1745 if (error != NULL)
1746 goto done;
1748 if (commit == NULL) {
1749 error = got_repo_match_object_id_prefix(&id,
1750 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1751 if (error != NULL)
1752 return error;
1755 if (error != NULL)
1756 goto done;
1758 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1759 if (error != NULL)
1760 goto done;
1761 if (in_repo_path) {
1762 free(path);
1763 path = in_repo_path;
1766 error = got_ref_list(&refs, repo, NULL);
1767 if (error)
1768 goto done;
1770 error = print_commits(id, repo, path, show_patch,
1771 diff_context, limit, first_parent_traversal, &refs);
1772 done:
1773 free(path);
1774 free(repo_path);
1775 free(cwd);
1776 free(id);
1777 if (worktree)
1778 got_worktree_close(worktree);
1779 if (repo) {
1780 const struct got_error *repo_error;
1781 repo_error = got_repo_close(repo);
1782 if (error == NULL)
1783 error = repo_error;
1785 got_ref_list_free(&refs);
1786 return error;
1789 __dead static void
1790 usage_diff(void)
1792 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1793 "[object1 object2 | path]\n", getprogname());
1794 exit(1);
1797 struct print_diff_arg {
1798 struct got_repository *repo;
1799 struct got_worktree *worktree;
1800 int diff_context;
1801 const char *id_str;
1802 int header_shown;
1803 int diff_staged;
1806 static const struct got_error *
1807 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1808 const char *path, struct got_object_id *blob_id,
1809 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1811 struct print_diff_arg *a = arg;
1812 const struct got_error *err = NULL;
1813 struct got_blob_object *blob1 = NULL;
1814 FILE *f2 = NULL;
1815 char *abspath = NULL, *label1 = NULL;
1816 struct stat sb;
1818 if (a->diff_staged) {
1819 if (staged_status != GOT_STATUS_MODIFY &&
1820 staged_status != GOT_STATUS_ADD &&
1821 staged_status != GOT_STATUS_DELETE)
1822 return NULL;
1823 } else {
1824 if (staged_status == GOT_STATUS_DELETE)
1825 return NULL;
1826 if (status != GOT_STATUS_MODIFY &&
1827 status != GOT_STATUS_ADD &&
1828 status != GOT_STATUS_DELETE &&
1829 status != GOT_STATUS_CONFLICT)
1830 return NULL;
1833 if (!a->header_shown) {
1834 printf("diff %s %s%s\n", a->id_str,
1835 got_worktree_get_root_path(a->worktree),
1836 a->diff_staged ? " (staged changes)" : "");
1837 a->header_shown = 1;
1840 if (a->diff_staged) {
1841 const char *label1 = NULL, *label2 = NULL;
1842 switch (staged_status) {
1843 case GOT_STATUS_MODIFY:
1844 label1 = path;
1845 label2 = path;
1846 break;
1847 case GOT_STATUS_ADD:
1848 label2 = path;
1849 break;
1850 case GOT_STATUS_DELETE:
1851 label1 = path;
1852 break;
1853 default:
1854 return got_error(GOT_ERR_FILE_STATUS);
1856 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1857 label1, label2, a->diff_context, a->repo, stdout);
1860 if (staged_status == GOT_STATUS_ADD ||
1861 staged_status == GOT_STATUS_MODIFY) {
1862 char *id_str;
1863 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1864 8192);
1865 if (err)
1866 goto done;
1867 err = got_object_id_str(&id_str, staged_blob_id);
1868 if (err)
1869 goto done;
1870 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1871 err = got_error_from_errno("asprintf");
1872 free(id_str);
1873 goto done;
1875 free(id_str);
1876 } else if (status != GOT_STATUS_ADD) {
1877 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1878 if (err)
1879 goto done;
1882 if (status != GOT_STATUS_DELETE) {
1883 if (asprintf(&abspath, "%s/%s",
1884 got_worktree_get_root_path(a->worktree), path) == -1) {
1885 err = got_error_from_errno("asprintf");
1886 goto done;
1889 f2 = fopen(abspath, "r");
1890 if (f2 == NULL) {
1891 err = got_error_from_errno2("fopen", abspath);
1892 goto done;
1894 if (lstat(abspath, &sb) == -1) {
1895 err = got_error_from_errno2("lstat", abspath);
1896 goto done;
1898 } else
1899 sb.st_size = 0;
1901 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1902 a->diff_context, stdout);
1903 done:
1904 if (blob1)
1905 got_object_blob_close(blob1);
1906 if (f2 && fclose(f2) != 0 && err == NULL)
1907 err = got_error_from_errno("fclose");
1908 free(abspath);
1909 return err;
1912 static const struct got_error *
1913 match_object_id(struct got_object_id **id, char **label,
1914 const char *id_str, int obj_type, int resolve_tags,
1915 struct got_repository *repo)
1917 const struct got_error *err;
1918 struct got_tag_object *tag;
1919 struct got_reference *ref = NULL;
1921 *id = NULL;
1922 *label = NULL;
1924 if (resolve_tags) {
1925 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1926 repo);
1927 if (err == NULL) {
1928 *id = got_object_id_dup(
1929 got_object_tag_get_object_id(tag));
1930 if (*id == NULL)
1931 err = got_error_from_errno("got_object_id_dup");
1932 else if (asprintf(label, "refs/tags/%s",
1933 got_object_tag_get_name(tag)) == -1) {
1934 err = got_error_from_errno("asprintf");
1935 free(id);
1936 *id = NULL;
1938 got_object_tag_close(tag);
1939 return err;
1940 } else if (err->code != GOT_ERR_NO_OBJ)
1941 return err;
1944 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1945 if (err) {
1946 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1947 return err;
1948 err = got_ref_open(&ref, repo, id_str, 0);
1949 if (err != NULL)
1950 goto done;
1951 *label = strdup(got_ref_get_name(ref));
1952 if (*label == NULL) {
1953 err = got_error_from_errno("strdup");
1954 goto done;
1956 err = got_ref_resolve(id, repo, ref);
1957 } else {
1958 err = got_object_id_str(label, *id);
1959 if (*label == NULL) {
1960 err = got_error_from_errno("strdup");
1961 goto done;
1964 done:
1965 if (ref)
1966 got_ref_close(ref);
1967 return err;
1971 static const struct got_error *
1972 cmd_diff(int argc, char *argv[])
1974 const struct got_error *error;
1975 struct got_repository *repo = NULL;
1976 struct got_worktree *worktree = NULL;
1977 char *cwd = NULL, *repo_path = NULL;
1978 struct got_object_id *id1 = NULL, *id2 = NULL;
1979 const char *id_str1 = NULL, *id_str2 = NULL;
1980 char *label1 = NULL, *label2 = NULL;
1981 int type1, type2;
1982 int diff_context = 3, diff_staged = 0, ch;
1983 const char *errstr;
1984 char *path = NULL;
1986 #ifndef PROFILE
1987 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1988 NULL) == -1)
1989 err(1, "pledge");
1990 #endif
1992 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1993 switch (ch) {
1994 case 'C':
1995 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1996 if (errstr != NULL)
1997 err(1, "-C option %s", errstr);
1998 break;
1999 case 'r':
2000 repo_path = realpath(optarg, NULL);
2001 if (repo_path == NULL)
2002 err(1, "-r option");
2003 got_path_strip_trailing_slashes(repo_path);
2004 break;
2005 case 's':
2006 diff_staged = 1;
2007 break;
2008 default:
2009 usage_diff();
2010 /* NOTREACHED */
2014 argc -= optind;
2015 argv += optind;
2017 cwd = getcwd(NULL, 0);
2018 if (cwd == NULL) {
2019 error = got_error_from_errno("getcwd");
2020 goto done;
2022 error = got_worktree_open(&worktree, cwd);
2023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2024 goto done;
2025 if (argc <= 1) {
2026 if (worktree == NULL) {
2027 error = got_error(GOT_ERR_NOT_WORKTREE);
2028 goto done;
2030 if (repo_path)
2031 errx(1,
2032 "-r option can't be used when diffing a work tree");
2033 repo_path = strdup(got_worktree_get_repo_path(worktree));
2034 if (repo_path == NULL) {
2035 error = got_error_from_errno("strdup");
2036 goto done;
2038 if (argc == 1) {
2039 error = got_worktree_resolve_path(&path, worktree,
2040 argv[0]);
2041 if (error)
2042 goto done;
2043 } else {
2044 path = strdup("");
2045 if (path == NULL) {
2046 error = got_error_from_errno("strdup");
2047 goto done;
2050 } else if (argc == 2) {
2051 if (diff_staged)
2052 errx(1, "-s option can't be used when diffing "
2053 "objects in repository");
2054 id_str1 = argv[0];
2055 id_str2 = argv[1];
2056 if (worktree && repo_path == NULL) {
2057 repo_path =
2058 strdup(got_worktree_get_repo_path(worktree));
2059 if (repo_path == NULL) {
2060 error = got_error_from_errno("strdup");
2061 goto done;
2064 } else
2065 usage_diff();
2067 if (repo_path == NULL) {
2068 repo_path = getcwd(NULL, 0);
2069 if (repo_path == NULL)
2070 return got_error_from_errno("getcwd");
2073 error = got_repo_open(&repo, repo_path);
2074 free(repo_path);
2075 if (error != NULL)
2076 goto done;
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 worktree ? got_worktree_get_root_path(worktree) : NULL);
2080 if (error)
2081 goto done;
2083 if (argc <= 1) {
2084 struct print_diff_arg arg;
2085 struct got_pathlist_head paths;
2086 char *id_str;
2088 TAILQ_INIT(&paths);
2090 error = got_object_id_str(&id_str,
2091 got_worktree_get_base_commit_id(worktree));
2092 if (error)
2093 goto done;
2094 arg.repo = repo;
2095 arg.worktree = worktree;
2096 arg.diff_context = diff_context;
2097 arg.id_str = id_str;
2098 arg.header_shown = 0;
2099 arg.diff_staged = diff_staged;
2101 error = got_pathlist_append(&paths, path, NULL);
2102 if (error)
2103 goto done;
2105 error = got_worktree_status(worktree, &paths, repo, print_diff,
2106 &arg, check_cancelled, NULL);
2107 free(id_str);
2108 got_pathlist_free(&paths);
2109 goto done;
2112 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2113 repo);
2114 if (error)
2115 goto done;
2117 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2118 repo);
2119 if (error)
2120 goto done;
2122 error = got_object_get_type(&type1, repo, id1);
2123 if (error)
2124 goto done;
2126 error = got_object_get_type(&type2, repo, id2);
2127 if (error)
2128 goto done;
2130 if (type1 != type2) {
2131 error = got_error(GOT_ERR_OBJ_TYPE);
2132 goto done;
2135 switch (type1) {
2136 case GOT_OBJ_TYPE_BLOB:
2137 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2138 diff_context, repo, stdout);
2139 break;
2140 case GOT_OBJ_TYPE_TREE:
2141 error = got_diff_objects_as_trees(id1, id2, "", "",
2142 diff_context, repo, stdout);
2143 break;
2144 case GOT_OBJ_TYPE_COMMIT:
2145 printf("diff %s %s\n", label1, label2);
2146 error = got_diff_objects_as_commits(id1, id2, diff_context,
2147 repo, stdout);
2148 break;
2149 default:
2150 error = got_error(GOT_ERR_OBJ_TYPE);
2153 done:
2154 free(label1);
2155 free(label2);
2156 free(id1);
2157 free(id2);
2158 free(path);
2159 if (worktree)
2160 got_worktree_close(worktree);
2161 if (repo) {
2162 const struct got_error *repo_error;
2163 repo_error = got_repo_close(repo);
2164 if (error == NULL)
2165 error = repo_error;
2167 return error;
2170 __dead static void
2171 usage_blame(void)
2173 fprintf(stderr,
2174 "usage: %s blame [-c commit] [-r repository-path] path\n",
2175 getprogname());
2176 exit(1);
2179 struct blame_line {
2180 int annotated;
2181 char *id_str;
2182 char *committer;
2183 char datebuf[9]; /* YY-MM-DD + NUL */
2186 struct blame_cb_args {
2187 struct blame_line *lines;
2188 int nlines;
2189 int nlines_prec;
2190 int lineno_cur;
2191 off_t *line_offsets;
2192 FILE *f;
2193 struct got_repository *repo;
2196 static const struct got_error *
2197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2199 const struct got_error *err = NULL;
2200 struct blame_cb_args *a = arg;
2201 struct blame_line *bline;
2202 char *line = NULL;
2203 size_t linesize = 0;
2204 struct got_commit_object *commit = NULL;
2205 off_t offset;
2206 struct tm tm;
2207 time_t committer_time;
2209 if (nlines != a->nlines ||
2210 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2211 return got_error(GOT_ERR_RANGE);
2213 if (sigint_received)
2214 return got_error(GOT_ERR_ITER_COMPLETED);
2216 if (lineno == -1)
2217 return NULL; /* no change in this commit */
2219 /* Annotate this line. */
2220 bline = &a->lines[lineno - 1];
2221 if (bline->annotated)
2222 return NULL;
2223 err = got_object_id_str(&bline->id_str, id);
2224 if (err)
2225 return err;
2227 err = got_object_open_as_commit(&commit, a->repo, id);
2228 if (err)
2229 goto done;
2231 bline->committer = strdup(got_object_commit_get_committer(commit));
2232 if (bline->committer == NULL) {
2233 err = got_error_from_errno("strdup");
2234 goto done;
2237 committer_time = got_object_commit_get_committer_time(commit);
2238 if (localtime_r(&committer_time, &tm) == NULL)
2239 return got_error_from_errno("localtime_r");
2240 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2241 &tm) >= sizeof(bline->datebuf)) {
2242 err = got_error(GOT_ERR_NO_SPACE);
2243 goto done;
2245 bline->annotated = 1;
2247 /* Print lines annotated so far. */
2248 bline = &a->lines[a->lineno_cur - 1];
2249 if (!bline->annotated)
2250 goto done;
2252 offset = a->line_offsets[a->lineno_cur - 1];
2253 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2254 err = got_error_from_errno("fseeko");
2255 goto done;
2258 while (bline->annotated) {
2259 char *smallerthan, *at, *nl, *committer;
2260 size_t len;
2262 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2263 if (ferror(a->f))
2264 err = got_error_from_errno("getline");
2265 break;
2268 committer = bline->committer;
2269 smallerthan = strchr(committer, '<');
2270 if (smallerthan && smallerthan[1] != '\0')
2271 committer = smallerthan + 1;
2272 at = strchr(committer, '@');
2273 if (at)
2274 *at = '\0';
2275 len = strlen(committer);
2276 if (len >= 9)
2277 committer[8] = '\0';
2279 nl = strchr(line, '\n');
2280 if (nl)
2281 *nl = '\0';
2282 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2283 bline->id_str, bline->datebuf, committer, line);
2285 a->lineno_cur++;
2286 bline = &a->lines[a->lineno_cur - 1];
2288 done:
2289 if (commit)
2290 got_object_commit_close(commit);
2291 free(line);
2292 return err;
2295 static const struct got_error *
2296 cmd_blame(int argc, char *argv[])
2298 const struct got_error *error;
2299 struct got_repository *repo = NULL;
2300 struct got_worktree *worktree = NULL;
2301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2302 struct got_object_id *obj_id = NULL;
2303 struct got_object_id *commit_id = NULL;
2304 struct got_blob_object *blob = NULL;
2305 char *commit_id_str = NULL;
2306 struct blame_cb_args bca;
2307 int ch, obj_type, i;
2308 size_t filesize;
2310 memset(&bca, 0, sizeof(bca));
2312 #ifndef PROFILE
2313 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2314 NULL) == -1)
2315 err(1, "pledge");
2316 #endif
2318 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2319 switch (ch) {
2320 case 'c':
2321 commit_id_str = optarg;
2322 break;
2323 case 'r':
2324 repo_path = realpath(optarg, NULL);
2325 if (repo_path == NULL)
2326 err(1, "-r option");
2327 got_path_strip_trailing_slashes(repo_path);
2328 break;
2329 default:
2330 usage_blame();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 if (argc == 1)
2339 path = argv[0];
2340 else
2341 usage_blame();
2343 cwd = getcwd(NULL, 0);
2344 if (cwd == NULL) {
2345 error = got_error_from_errno("getcwd");
2346 goto done;
2348 if (repo_path == NULL) {
2349 error = got_worktree_open(&worktree, cwd);
2350 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2351 goto done;
2352 else
2353 error = NULL;
2354 if (worktree) {
2355 repo_path =
2356 strdup(got_worktree_get_repo_path(worktree));
2357 if (repo_path == NULL)
2358 error = got_error_from_errno("strdup");
2359 if (error)
2360 goto done;
2361 } else {
2362 repo_path = strdup(cwd);
2363 if (repo_path == NULL) {
2364 error = got_error_from_errno("strdup");
2365 goto done;
2370 error = got_repo_open(&repo, repo_path);
2371 if (error != NULL)
2372 goto done;
2374 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2375 if (error)
2376 goto done;
2378 if (worktree) {
2379 const char *prefix = got_worktree_get_path_prefix(worktree);
2380 char *p, *worktree_subdir = cwd +
2381 strlen(got_worktree_get_root_path(worktree));
2382 if (asprintf(&p, "%s%s%s%s%s",
2383 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2384 worktree_subdir, worktree_subdir[0] ? "/" : "",
2385 path) == -1) {
2386 error = got_error_from_errno("asprintf");
2387 goto done;
2389 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2390 free(p);
2391 } else {
2392 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2394 if (error)
2395 goto done;
2397 if (commit_id_str == NULL) {
2398 struct got_reference *head_ref;
2399 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2400 if (error != NULL)
2401 goto done;
2402 error = got_ref_resolve(&commit_id, repo, head_ref);
2403 got_ref_close(head_ref);
2404 if (error != NULL)
2405 goto done;
2406 } else {
2407 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2408 if (error)
2409 goto done;
2412 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2413 if (error)
2414 goto done;
2415 if (obj_id == NULL) {
2416 error = got_error(GOT_ERR_NO_OBJ);
2417 goto done;
2420 error = got_object_get_type(&obj_type, repo, obj_id);
2421 if (error)
2422 goto done;
2424 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2425 error = got_error(GOT_ERR_OBJ_TYPE);
2426 goto done;
2429 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2430 if (error)
2431 goto done;
2432 bca.f = got_opentemp();
2433 if (bca.f == NULL) {
2434 error = got_error_from_errno("got_opentemp");
2435 goto done;
2437 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2438 &bca.line_offsets, bca.f, blob);
2439 if (error || bca.nlines == 0)
2440 goto done;
2442 /* Don't include \n at EOF in the blame line count. */
2443 if (bca.line_offsets[bca.nlines - 1] == filesize)
2444 bca.nlines--;
2446 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2447 if (bca.lines == NULL) {
2448 error = got_error_from_errno("calloc");
2449 goto done;
2451 bca.lineno_cur = 1;
2452 bca.nlines_prec = 0;
2453 i = bca.nlines;
2454 while (i > 0) {
2455 i /= 10;
2456 bca.nlines_prec++;
2458 bca.repo = repo;
2460 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2461 check_cancelled, NULL);
2462 if (error)
2463 goto done;
2464 done:
2465 free(in_repo_path);
2466 free(repo_path);
2467 free(cwd);
2468 free(commit_id);
2469 free(obj_id);
2470 if (blob)
2471 got_object_blob_close(blob);
2472 if (worktree)
2473 got_worktree_close(worktree);
2474 if (repo) {
2475 const struct got_error *repo_error;
2476 repo_error = got_repo_close(repo);
2477 if (error == NULL)
2478 error = repo_error;
2480 for (i = 0; i < bca.nlines; i++) {
2481 struct blame_line *bline = &bca.lines[i];
2482 free(bline->id_str);
2483 free(bline->committer);
2485 free(bca.lines);
2486 free(bca.line_offsets);
2487 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2488 error = got_error_from_errno("fclose");
2489 return error;
2492 __dead static void
2493 usage_tree(void)
2495 fprintf(stderr,
2496 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2497 getprogname());
2498 exit(1);
2501 static void
2502 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2503 const char *root_path)
2505 int is_root_path = (strcmp(path, root_path) == 0);
2506 const char *modestr = "";
2508 path += strlen(root_path);
2509 while (path[0] == '/')
2510 path++;
2512 if (S_ISLNK(te->mode))
2513 modestr = "@";
2514 else if (S_ISDIR(te->mode))
2515 modestr = "/";
2516 else if (te->mode & S_IXUSR)
2517 modestr = "*";
2519 printf("%s%s%s%s%s\n", id ? id : "", path,
2520 is_root_path ? "" : "/", te->name, modestr);
2523 static const struct got_error *
2524 print_tree(const char *path, struct got_object_id *commit_id,
2525 int show_ids, int recurse, const char *root_path,
2526 struct got_repository *repo)
2528 const struct got_error *err = NULL;
2529 struct got_object_id *tree_id = NULL;
2530 struct got_tree_object *tree = NULL;
2531 const struct got_tree_entries *entries;
2532 struct got_tree_entry *te;
2534 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2535 if (err)
2536 goto done;
2538 err = got_object_open_as_tree(&tree, repo, tree_id);
2539 if (err)
2540 goto done;
2541 entries = got_object_tree_get_entries(tree);
2542 te = SIMPLEQ_FIRST(&entries->head);
2543 while (te) {
2544 char *id = NULL;
2546 if (sigint_received || sigpipe_received)
2547 break;
2549 if (show_ids) {
2550 char *id_str;
2551 err = got_object_id_str(&id_str, te->id);
2552 if (err)
2553 goto done;
2554 if (asprintf(&id, "%s ", id_str) == -1) {
2555 err = got_error_from_errno("asprintf");
2556 free(id_str);
2557 goto done;
2559 free(id_str);
2561 print_entry(te, id, path, root_path);
2562 free(id);
2564 if (recurse && S_ISDIR(te->mode)) {
2565 char *child_path;
2566 if (asprintf(&child_path, "%s%s%s", path,
2567 path[0] == '/' && path[1] == '\0' ? "" : "/",
2568 te->name) == -1) {
2569 err = got_error_from_errno("asprintf");
2570 goto done;
2572 err = print_tree(child_path, commit_id, show_ids, 1,
2573 root_path, repo);
2574 free(child_path);
2575 if (err)
2576 goto done;
2579 te = SIMPLEQ_NEXT(te, entry);
2581 done:
2582 if (tree)
2583 got_object_tree_close(tree);
2584 free(tree_id);
2585 return err;
2588 static const struct got_error *
2589 cmd_tree(int argc, char *argv[])
2591 const struct got_error *error;
2592 struct got_repository *repo = NULL;
2593 struct got_worktree *worktree = NULL;
2594 const char *path;
2595 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2596 struct got_object_id *commit_id = NULL;
2597 char *commit_id_str = NULL;
2598 int show_ids = 0, recurse = 0;
2599 int ch;
2601 #ifndef PROFILE
2602 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2603 NULL) == -1)
2604 err(1, "pledge");
2605 #endif
2607 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2608 switch (ch) {
2609 case 'c':
2610 commit_id_str = optarg;
2611 break;
2612 case 'r':
2613 repo_path = realpath(optarg, NULL);
2614 if (repo_path == NULL)
2615 err(1, "-r option");
2616 got_path_strip_trailing_slashes(repo_path);
2617 break;
2618 case 'i':
2619 show_ids = 1;
2620 break;
2621 case 'R':
2622 recurse = 1;
2623 break;
2624 default:
2625 usage_tree();
2626 /* NOTREACHED */
2630 argc -= optind;
2631 argv += optind;
2633 if (argc == 1)
2634 path = argv[0];
2635 else if (argc > 1)
2636 usage_tree();
2637 else
2638 path = NULL;
2640 cwd = getcwd(NULL, 0);
2641 if (cwd == NULL) {
2642 error = got_error_from_errno("getcwd");
2643 goto done;
2645 if (repo_path == NULL) {
2646 error = got_worktree_open(&worktree, cwd);
2647 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2648 goto done;
2649 else
2650 error = NULL;
2651 if (worktree) {
2652 repo_path =
2653 strdup(got_worktree_get_repo_path(worktree));
2654 if (repo_path == NULL)
2655 error = got_error_from_errno("strdup");
2656 if (error)
2657 goto done;
2658 } else {
2659 repo_path = strdup(cwd);
2660 if (repo_path == NULL) {
2661 error = got_error_from_errno("strdup");
2662 goto done;
2667 error = got_repo_open(&repo, repo_path);
2668 if (error != NULL)
2669 goto done;
2671 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2672 if (error)
2673 goto done;
2675 if (path == NULL) {
2676 if (worktree) {
2677 char *p, *worktree_subdir = cwd +
2678 strlen(got_worktree_get_root_path(worktree));
2679 if (asprintf(&p, "%s/%s",
2680 got_worktree_get_path_prefix(worktree),
2681 worktree_subdir) == -1) {
2682 error = got_error_from_errno("asprintf");
2683 goto done;
2685 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2686 free(p);
2687 if (error)
2688 goto done;
2689 } else
2690 path = "/";
2692 if (in_repo_path == NULL) {
2693 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2694 if (error != NULL)
2695 goto done;
2698 if (commit_id_str == NULL) {
2699 struct got_reference *head_ref;
2700 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2701 if (error != NULL)
2702 goto done;
2703 error = got_ref_resolve(&commit_id, repo, head_ref);
2704 got_ref_close(head_ref);
2705 if (error != NULL)
2706 goto done;
2707 } else {
2708 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2709 if (error)
2710 goto done;
2713 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2714 in_repo_path, repo);
2715 done:
2716 free(in_repo_path);
2717 free(repo_path);
2718 free(cwd);
2719 free(commit_id);
2720 if (worktree)
2721 got_worktree_close(worktree);
2722 if (repo) {
2723 const struct got_error *repo_error;
2724 repo_error = got_repo_close(repo);
2725 if (error == NULL)
2726 error = repo_error;
2728 return error;
2731 __dead static void
2732 usage_status(void)
2734 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2735 exit(1);
2738 static const struct got_error *
2739 print_status(void *arg, unsigned char status, unsigned char staged_status,
2740 const char *path, struct got_object_id *blob_id,
2741 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2743 if (status == staged_status && (status == GOT_STATUS_DELETE))
2744 status = GOT_STATUS_NO_CHANGE;
2745 printf("%c%c %s\n", status, staged_status, path);
2746 return NULL;
2749 static const struct got_error *
2750 cmd_status(int argc, char *argv[])
2752 const struct got_error *error = NULL;
2753 struct got_repository *repo = NULL;
2754 struct got_worktree *worktree = NULL;
2755 char *cwd = NULL;
2756 struct got_pathlist_head paths;
2757 struct got_pathlist_entry *pe;
2758 int ch;
2760 TAILQ_INIT(&paths);
2762 while ((ch = getopt(argc, argv, "")) != -1) {
2763 switch (ch) {
2764 default:
2765 usage_status();
2766 /* NOTREACHED */
2770 argc -= optind;
2771 argv += optind;
2773 #ifndef PROFILE
2774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2775 NULL) == -1)
2776 err(1, "pledge");
2777 #endif
2778 cwd = getcwd(NULL, 0);
2779 if (cwd == NULL) {
2780 error = got_error_from_errno("getcwd");
2781 goto done;
2784 error = got_worktree_open(&worktree, cwd);
2785 if (error != NULL)
2786 goto done;
2788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2789 if (error != NULL)
2790 goto done;
2792 error = apply_unveil(got_repo_get_path(repo), 1,
2793 got_worktree_get_root_path(worktree));
2794 if (error)
2795 goto done;
2797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2798 if (error)
2799 goto done;
2801 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2802 check_cancelled, NULL);
2803 done:
2804 TAILQ_FOREACH(pe, &paths, entry)
2805 free((char *)pe->path);
2806 got_pathlist_free(&paths);
2807 free(cwd);
2808 return error;
2811 __dead static void
2812 usage_ref(void)
2814 fprintf(stderr,
2815 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2816 getprogname());
2817 exit(1);
2820 static const struct got_error *
2821 list_refs(struct got_repository *repo)
2823 static const struct got_error *err = NULL;
2824 struct got_reflist_head refs;
2825 struct got_reflist_entry *re;
2827 SIMPLEQ_INIT(&refs);
2828 err = got_ref_list(&refs, repo, NULL);
2829 if (err)
2830 return err;
2832 SIMPLEQ_FOREACH(re, &refs, entry) {
2833 char *refstr;
2834 refstr = got_ref_to_str(re->ref);
2835 if (refstr == NULL)
2836 return got_error_from_errno("got_ref_to_str");
2837 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2838 free(refstr);
2841 got_ref_list_free(&refs);
2842 return NULL;
2845 static const struct got_error *
2846 delete_ref(struct got_repository *repo, const char *refname)
2848 const struct got_error *err = NULL;
2849 struct got_reference *ref;
2851 err = got_ref_open(&ref, repo, refname, 0);
2852 if (err)
2853 return err;
2855 err = got_ref_delete(ref, repo);
2856 got_ref_close(ref);
2857 return err;
2860 static const struct got_error *
2861 add_ref(struct got_repository *repo, const char *refname, const char *target)
2863 const struct got_error *err = NULL;
2864 struct got_object_id *id;
2865 struct got_reference *ref = NULL;
2868 * Don't let the user create a reference named '-'.
2869 * While technically a valid reference name, this case is usually
2870 * an unintended typo.
2872 if (refname[0] == '-' && refname[1] == '\0')
2873 return got_error(GOT_ERR_BAD_REF_NAME);
2875 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2876 repo);
2877 if (err) {
2878 struct got_reference *target_ref;
2880 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2881 return err;
2882 err = got_ref_open(&target_ref, repo, target, 0);
2883 if (err)
2884 return err;
2885 err = got_ref_resolve(&id, repo, target_ref);
2886 got_ref_close(target_ref);
2887 if (err)
2888 return err;
2891 err = got_ref_alloc(&ref, refname, id);
2892 if (err)
2893 goto done;
2895 err = got_ref_write(ref, repo);
2896 done:
2897 if (ref)
2898 got_ref_close(ref);
2899 free(id);
2900 return err;
2903 static const struct got_error *
2904 add_symref(struct got_repository *repo, const char *refname, const char *target)
2906 const struct got_error *err = NULL;
2907 struct got_reference *ref = NULL;
2908 struct got_reference *target_ref = NULL;
2911 * Don't let the user create a reference named '-'.
2912 * While technically a valid reference name, this case is usually
2913 * an unintended typo.
2915 if (refname[0] == '-' && refname[1] == '\0')
2916 return got_error(GOT_ERR_BAD_REF_NAME);
2918 err = got_ref_open(&target_ref, repo, target, 0);
2919 if (err)
2920 return err;
2922 err = got_ref_alloc_symref(&ref, refname, target_ref);
2923 if (err)
2924 goto done;
2926 err = got_ref_write(ref, repo);
2927 done:
2928 if (target_ref)
2929 got_ref_close(target_ref);
2930 if (ref)
2931 got_ref_close(ref);
2932 return err;
2935 static const struct got_error *
2936 cmd_ref(int argc, char *argv[])
2938 const struct got_error *error = NULL;
2939 struct got_repository *repo = NULL;
2940 struct got_worktree *worktree = NULL;
2941 char *cwd = NULL, *repo_path = NULL;
2942 int ch, do_list = 0, create_symref = 0;
2943 const char *delref = NULL;
2945 /* TODO: Add -s option for adding symbolic references. */
2946 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2947 switch (ch) {
2948 case 'd':
2949 delref = optarg;
2950 break;
2951 case 'r':
2952 repo_path = realpath(optarg, NULL);
2953 if (repo_path == NULL)
2954 err(1, "-r option");
2955 got_path_strip_trailing_slashes(repo_path);
2956 break;
2957 case 'l':
2958 do_list = 1;
2959 break;
2960 case 's':
2961 create_symref = 1;
2962 break;
2963 default:
2964 usage_ref();
2965 /* NOTREACHED */
2969 if (do_list && delref)
2970 errx(1, "-l and -d options are mutually exclusive\n");
2972 argc -= optind;
2973 argv += optind;
2975 if (do_list || delref) {
2976 if (create_symref)
2977 errx(1, "-s option cannot be used together with the "
2978 "-l or -d options");
2979 if (argc > 0)
2980 usage_ref();
2981 } else if (argc != 2)
2982 usage_ref();
2984 #ifndef PROFILE
2985 if (do_list) {
2986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2987 NULL) == -1)
2988 err(1, "pledge");
2989 } else {
2990 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2991 "sendfd unveil", NULL) == -1)
2992 err(1, "pledge");
2994 #endif
2995 cwd = getcwd(NULL, 0);
2996 if (cwd == NULL) {
2997 error = got_error_from_errno("getcwd");
2998 goto done;
3001 if (repo_path == NULL) {
3002 error = got_worktree_open(&worktree, cwd);
3003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3004 goto done;
3005 else
3006 error = NULL;
3007 if (worktree) {
3008 repo_path =
3009 strdup(got_worktree_get_repo_path(worktree));
3010 if (repo_path == NULL)
3011 error = got_error_from_errno("strdup");
3012 if (error)
3013 goto done;
3014 } else {
3015 repo_path = strdup(cwd);
3016 if (repo_path == NULL) {
3017 error = got_error_from_errno("strdup");
3018 goto done;
3023 error = got_repo_open(&repo, repo_path);
3024 if (error != NULL)
3025 goto done;
3027 error = apply_unveil(got_repo_get_path(repo), do_list,
3028 worktree ? got_worktree_get_root_path(worktree) : NULL);
3029 if (error)
3030 goto done;
3032 if (do_list)
3033 error = list_refs(repo);
3034 else if (delref)
3035 error = delete_ref(repo, delref);
3036 else if (create_symref)
3037 error = add_symref(repo, argv[0], argv[1]);
3038 else
3039 error = add_ref(repo, argv[0], argv[1]);
3040 done:
3041 if (repo)
3042 got_repo_close(repo);
3043 if (worktree)
3044 got_worktree_close(worktree);
3045 free(cwd);
3046 free(repo_path);
3047 return error;
3050 __dead static void
3051 usage_branch(void)
3053 fprintf(stderr,
3054 "usage: %s branch [-r repository] -l | -d name | "
3055 "name [base-branch]\n", getprogname());
3056 exit(1);
3059 static const struct got_error *
3060 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3062 static const struct got_error *err = NULL;
3063 struct got_reflist_head refs;
3064 struct got_reflist_entry *re;
3066 SIMPLEQ_INIT(&refs);
3068 err = got_ref_list(&refs, repo, "refs/heads");
3069 if (err)
3070 return err;
3072 SIMPLEQ_FOREACH(re, &refs, entry) {
3073 const char *refname, *marker = " ";
3074 char *refstr;
3075 refname = got_ref_get_name(re->ref);
3076 if (worktree && strcmp(refname,
3077 got_worktree_get_head_ref_name(worktree)) == 0) {
3078 struct got_object_id *id = NULL;
3079 err = got_ref_resolve(&id, repo, re->ref);
3080 if (err)
3081 return err;
3082 if (got_object_id_cmp(id,
3083 got_worktree_get_base_commit_id(worktree)) == 0)
3084 marker = "* ";
3085 else
3086 marker = "~ ";
3087 free(id);
3089 refname += strlen("refs/heads/");
3090 refstr = got_ref_to_str(re->ref);
3091 if (refstr == NULL)
3092 return got_error_from_errno("got_ref_to_str");
3093 printf("%s%s: %s\n", marker, refname, refstr);
3094 free(refstr);
3097 got_ref_list_free(&refs);
3098 return NULL;
3101 static const struct got_error *
3102 delete_branch(struct got_repository *repo, const char *branch_name)
3104 const struct got_error *err = NULL;
3105 struct got_reference *ref;
3106 char *refname;
3108 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3109 return got_error_from_errno("asprintf");
3111 err = got_ref_open(&ref, repo, refname, 0);
3112 if (err)
3113 goto done;
3115 err = got_ref_delete(ref, repo);
3116 got_ref_close(ref);
3117 done:
3118 free(refname);
3119 return err;
3122 static const struct got_error *
3123 add_branch(struct got_repository *repo, const char *branch_name,
3124 const char *base_branch)
3126 const struct got_error *err = NULL;
3127 struct got_object_id *id = NULL;
3128 struct got_reference *ref = NULL;
3129 char *base_refname = NULL, *refname = NULL;
3130 struct got_reference *base_ref;
3133 * Don't let the user create a branch named '-'.
3134 * While technically a valid reference name, this case is usually
3135 * an unintended typo.
3137 if (branch_name[0] == '-' && branch_name[1] == '\0')
3138 return got_error(GOT_ERR_BAD_REF_NAME);
3140 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3141 base_refname = strdup(GOT_REF_HEAD);
3142 if (base_refname == NULL)
3143 return got_error_from_errno("strdup");
3144 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3145 return got_error_from_errno("asprintf");
3147 err = got_ref_open(&base_ref, repo, base_refname, 0);
3148 if (err)
3149 goto done;
3150 err = got_ref_resolve(&id, repo, base_ref);
3151 got_ref_close(base_ref);
3152 if (err)
3153 goto done;
3155 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3156 err = got_error_from_errno("asprintf");
3157 goto done;
3160 err = got_ref_open(&ref, repo, refname, 0);
3161 if (err == NULL) {
3162 err = got_error(GOT_ERR_BRANCH_EXISTS);
3163 goto done;
3164 } else if (err->code != GOT_ERR_NOT_REF)
3165 goto done;
3167 err = got_ref_alloc(&ref, refname, id);
3168 if (err)
3169 goto done;
3171 err = got_ref_write(ref, repo);
3172 done:
3173 if (ref)
3174 got_ref_close(ref);
3175 free(id);
3176 free(base_refname);
3177 free(refname);
3178 return err;
3181 static const struct got_error *
3182 cmd_branch(int argc, char *argv[])
3184 const struct got_error *error = NULL;
3185 struct got_repository *repo = NULL;
3186 struct got_worktree *worktree = NULL;
3187 char *cwd = NULL, *repo_path = NULL;
3188 int ch, do_list = 0;
3189 const char *delref = NULL;
3191 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3192 switch (ch) {
3193 case 'd':
3194 delref = optarg;
3195 break;
3196 case 'r':
3197 repo_path = realpath(optarg, NULL);
3198 if (repo_path == NULL)
3199 err(1, "-r option");
3200 got_path_strip_trailing_slashes(repo_path);
3201 break;
3202 case 'l':
3203 do_list = 1;
3204 break;
3205 default:
3206 usage_branch();
3207 /* NOTREACHED */
3211 if (do_list && delref)
3212 errx(1, "-l and -d options are mutually exclusive\n");
3214 argc -= optind;
3215 argv += optind;
3217 if (do_list || delref) {
3218 if (argc > 0)
3219 usage_branch();
3220 } else if (argc < 1 || argc > 2)
3221 usage_branch();
3223 #ifndef PROFILE
3224 if (do_list) {
3225 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3226 NULL) == -1)
3227 err(1, "pledge");
3228 } else {
3229 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3230 "sendfd unveil", NULL) == -1)
3231 err(1, "pledge");
3233 #endif
3234 cwd = getcwd(NULL, 0);
3235 if (cwd == NULL) {
3236 error = got_error_from_errno("getcwd");
3237 goto done;
3240 if (repo_path == NULL) {
3241 error = got_worktree_open(&worktree, cwd);
3242 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3243 goto done;
3244 else
3245 error = NULL;
3246 if (worktree) {
3247 repo_path =
3248 strdup(got_worktree_get_repo_path(worktree));
3249 if (repo_path == NULL)
3250 error = got_error_from_errno("strdup");
3251 if (error)
3252 goto done;
3253 } else {
3254 repo_path = strdup(cwd);
3255 if (repo_path == NULL) {
3256 error = got_error_from_errno("strdup");
3257 goto done;
3262 error = got_repo_open(&repo, repo_path);
3263 if (error != NULL)
3264 goto done;
3266 error = apply_unveil(got_repo_get_path(repo), do_list,
3267 worktree ? got_worktree_get_root_path(worktree) : NULL);
3268 if (error)
3269 goto done;
3271 if (do_list)
3272 error = list_branches(repo, worktree);
3273 else if (delref)
3274 error = delete_branch(repo, delref);
3275 else {
3276 const char *base_branch;
3277 if (argc == 1) {
3278 base_branch = worktree ?
3279 got_worktree_get_head_ref_name(worktree) :
3280 GOT_REF_HEAD;
3281 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3282 base_branch += 11;
3283 } else
3284 base_branch = argv[1];
3285 error = add_branch(repo, argv[0], base_branch);
3287 done:
3288 if (repo)
3289 got_repo_close(repo);
3290 if (worktree)
3291 got_worktree_close(worktree);
3292 free(cwd);
3293 free(repo_path);
3294 return error;
3298 __dead static void
3299 usage_tag(void)
3301 fprintf(stderr,
3302 "usage: %s tag [-r repository] | -l | "
3303 "[-m message] name [commit]\n", getprogname());
3304 exit(1);
3307 static const struct got_error *
3308 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3310 static const struct got_error *err = NULL;
3311 struct got_reflist_head refs;
3312 struct got_reflist_entry *re;
3314 SIMPLEQ_INIT(&refs);
3316 err = got_ref_list(&refs, repo, "refs/tags");
3317 if (err)
3318 return err;
3320 SIMPLEQ_FOREACH(re, &refs, entry) {
3321 const char *refname;
3322 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3323 char datebuf[26];
3324 time_t tagger_time;
3325 struct got_object_id *id;
3326 struct got_tag_object *tag;
3328 refname = got_ref_get_name(re->ref);
3329 if (strncmp(refname, "refs/tags/", 10) != 0)
3330 continue;
3331 refname += 10;
3332 refstr = got_ref_to_str(re->ref);
3333 if (refstr == NULL) {
3334 err = got_error_from_errno("got_ref_to_str");
3335 break;
3337 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3338 free(refstr);
3340 err = got_ref_resolve(&id, repo, re->ref);
3341 if (err)
3342 break;
3343 err = got_object_open_as_tag(&tag, repo, id);
3344 free(id);
3345 if (err)
3346 break;
3347 printf("from: %s\n", got_object_tag_get_tagger(tag));
3348 tagger_time = got_object_tag_get_tagger_time(tag);
3349 datestr = get_datestr(&tagger_time, datebuf);
3350 if (datestr)
3351 printf("date: %s UTC\n", datestr);
3352 err = got_object_id_str(&id_str,
3353 got_object_tag_get_object_id(tag));
3354 if (err)
3355 break;
3356 switch (got_object_tag_get_object_type(tag)) {
3357 case GOT_OBJ_TYPE_BLOB:
3358 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3359 break;
3360 case GOT_OBJ_TYPE_TREE:
3361 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3362 break;
3363 case GOT_OBJ_TYPE_COMMIT:
3364 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3365 break;
3366 case GOT_OBJ_TYPE_TAG:
3367 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3368 break;
3369 default:
3370 break;
3372 free(id_str);
3373 tagmsg0 = strdup(got_object_tag_get_message(tag));
3374 got_object_tag_close(tag);
3375 if (tagmsg0 == NULL) {
3376 err = got_error_from_errno("strdup");
3377 break;
3380 tagmsg = tagmsg0;
3381 do {
3382 line = strsep(&tagmsg, "\n");
3383 if (line)
3384 printf(" %s\n", line);
3385 } while (line);
3386 free(tagmsg0);
3389 got_ref_list_free(&refs);
3390 return NULL;
3393 static const struct got_error *
3394 get_tag_message(char **tagmsg, const char *commit_id_str,
3395 const char *tag_name, const char *repo_path)
3397 const struct got_error *err = NULL;
3398 char *template = NULL, *initial_content = NULL;
3399 char *tagmsg_path = NULL, *editor = NULL;
3400 int fd = -1;
3402 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3403 err = got_error_from_errno("asprintf");
3404 goto done;
3407 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3408 commit_id_str, tag_name) == -1) {
3409 err = got_error_from_errno("asprintf");
3410 goto done;
3413 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3414 if (err)
3415 goto done;
3417 dprintf(fd, initial_content);
3418 close(fd);
3420 err = get_editor(&editor);
3421 if (err)
3422 goto done;
3423 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3424 done:
3425 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3426 unlink(tagmsg_path);
3427 free(tagmsg_path);
3428 tagmsg_path = NULL;
3430 free(initial_content);
3431 free(template);
3432 free(editor);
3434 /* Editor is done; we can now apply unveil(2) */
3435 if (err == NULL) {
3436 err = apply_unveil(repo_path, 0, NULL);
3437 if (err) {
3438 free(*tagmsg);
3439 *tagmsg = NULL;
3442 return err;
3445 static const struct got_error *
3446 add_tag(struct got_repository *repo, const char *tag_name,
3447 const char *commit_arg, const char *tagmsg_arg)
3449 const struct got_error *err = NULL;
3450 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3451 char *label = NULL, *commit_id_str = NULL;
3452 struct got_reference *ref = NULL;
3453 char *refname = NULL, *tagmsg = NULL;
3454 const char *tagger;
3457 * Don't let the user create a tag named '-'.
3458 * While technically a valid reference name, this case is usually
3459 * an unintended typo.
3461 if (tag_name[0] == '-' && tag_name[1] == '\0')
3462 return got_error(GOT_ERR_BAD_REF_NAME);
3464 err = get_author(&tagger);
3465 if (err)
3466 return err;
3468 err = match_object_id(&commit_id, &label, commit_arg,
3469 GOT_OBJ_TYPE_COMMIT, 1, repo);
3470 if (err)
3471 goto done;
3473 err = got_object_id_str(&commit_id_str, commit_id);
3474 if (err)
3475 goto done;
3477 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3478 refname = strdup(tag_name);
3479 if (refname == NULL) {
3480 err = got_error_from_errno("strdup");
3481 goto done;
3483 tag_name += 10;
3484 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3485 err = got_error_from_errno("asprintf");
3486 goto done;
3489 err = got_ref_open(&ref, repo, refname, 0);
3490 if (err == NULL) {
3491 err = got_error(GOT_ERR_TAG_EXISTS);
3492 goto done;
3493 } else if (err->code != GOT_ERR_NOT_REF)
3494 goto done;
3496 if (tagmsg_arg == NULL) {
3497 err = get_tag_message(&tagmsg, commit_id_str,
3498 tag_name, got_repo_get_path(repo));
3499 if (err)
3500 goto done;
3503 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3504 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3505 if (err)
3506 goto done;
3508 err = got_ref_alloc(&ref, refname, tag_id);
3509 if (err)
3510 goto done;
3512 err = got_ref_write(ref, repo);
3514 if (err == NULL) {
3515 char *tag_id_str;
3516 err = got_object_id_str(&tag_id_str, tag_id);
3517 printf("Created tag %s\n", tag_id_str);
3518 free(tag_id_str);
3520 done:
3521 if (ref)
3522 got_ref_close(ref);
3523 free(commit_id);
3524 free(commit_id_str);
3525 free(refname);
3526 free(tagmsg);
3527 return err;
3530 static const struct got_error *
3531 cmd_tag(int argc, char *argv[])
3533 const struct got_error *error = NULL;
3534 struct got_repository *repo = NULL;
3535 struct got_worktree *worktree = NULL;
3536 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3537 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3538 int ch, do_list = 0;
3540 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3541 switch (ch) {
3542 case 'm':
3543 tagmsg = optarg;
3544 break;
3545 case 'r':
3546 repo_path = realpath(optarg, NULL);
3547 if (repo_path == NULL)
3548 err(1, "-r option");
3549 got_path_strip_trailing_slashes(repo_path);
3550 break;
3551 case 'l':
3552 do_list = 1;
3553 break;
3554 default:
3555 usage_tag();
3556 /* NOTREACHED */
3560 argc -= optind;
3561 argv += optind;
3563 if (do_list) {
3564 if (tagmsg)
3565 errx(1, "-l and -m options are mutually exclusive\n");
3566 if (argc > 0)
3567 usage_tag();
3568 } else if (argc < 1 || argc > 2)
3569 usage_tag();
3570 else if (argc > 1)
3571 commit_id_arg = argv[1];
3572 tag_name = argv[0];
3574 #ifndef PROFILE
3575 if (do_list) {
3576 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3577 NULL) == -1)
3578 err(1, "pledge");
3579 } else {
3580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3581 "sendfd unveil", NULL) == -1)
3582 err(1, "pledge");
3584 #endif
3585 cwd = getcwd(NULL, 0);
3586 if (cwd == NULL) {
3587 error = got_error_from_errno("getcwd");
3588 goto done;
3591 if (repo_path == NULL) {
3592 error = got_worktree_open(&worktree, cwd);
3593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3594 goto done;
3595 else
3596 error = NULL;
3597 if (worktree) {
3598 repo_path =
3599 strdup(got_worktree_get_repo_path(worktree));
3600 if (repo_path == NULL)
3601 error = got_error_from_errno("strdup");
3602 if (error)
3603 goto done;
3604 } else {
3605 repo_path = strdup(cwd);
3606 if (repo_path == NULL) {
3607 error = got_error_from_errno("strdup");
3608 goto done;
3613 error = got_repo_open(&repo, repo_path);
3614 if (error != NULL)
3615 goto done;
3618 if (do_list) {
3619 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3620 if (error)
3621 goto done;
3622 error = list_tags(repo, worktree);
3623 } else {
3624 if (tagmsg) {
3625 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3626 if (error)
3627 goto done;
3630 if (commit_id_arg == NULL) {
3631 struct got_reference *head_ref;
3632 struct got_object_id *commit_id;
3633 error = got_ref_open(&head_ref, repo,
3634 worktree ? got_worktree_get_head_ref_name(worktree)
3635 : GOT_REF_HEAD, 0);
3636 if (error)
3637 goto done;
3638 error = got_ref_resolve(&commit_id, repo, head_ref);
3639 got_ref_close(head_ref);
3640 if (error)
3641 goto done;
3642 error = got_object_id_str(&commit_id_str, commit_id);
3643 free(commit_id);
3644 if (error)
3645 goto done;
3648 error = add_tag(repo, tag_name,
3649 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3651 done:
3652 if (repo)
3653 got_repo_close(repo);
3654 if (worktree)
3655 got_worktree_close(worktree);
3656 free(cwd);
3657 free(repo_path);
3658 free(commit_id_str);
3659 return error;
3662 __dead static void
3663 usage_add(void)
3665 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3666 exit(1);
3669 static const struct got_error *
3670 cmd_add(int argc, char *argv[])
3672 const struct got_error *error = NULL;
3673 struct got_repository *repo = NULL;
3674 struct got_worktree *worktree = NULL;
3675 char *cwd = NULL;
3676 struct got_pathlist_head paths;
3677 struct got_pathlist_entry *pe;
3678 int ch;
3680 TAILQ_INIT(&paths);
3682 while ((ch = getopt(argc, argv, "")) != -1) {
3683 switch (ch) {
3684 default:
3685 usage_add();
3686 /* NOTREACHED */
3690 argc -= optind;
3691 argv += optind;
3693 #ifndef PROFILE
3694 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3695 NULL) == -1)
3696 err(1, "pledge");
3697 #endif
3698 if (argc < 1)
3699 usage_add();
3701 cwd = getcwd(NULL, 0);
3702 if (cwd == NULL) {
3703 error = got_error_from_errno("getcwd");
3704 goto done;
3707 error = got_worktree_open(&worktree, cwd);
3708 if (error)
3709 goto done;
3711 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3712 if (error != NULL)
3713 goto done;
3715 error = apply_unveil(got_repo_get_path(repo), 1,
3716 got_worktree_get_root_path(worktree));
3717 if (error)
3718 goto done;
3720 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3721 if (error)
3722 goto done;
3724 error = got_worktree_schedule_add(worktree, &paths, print_status,
3725 NULL, repo);
3726 done:
3727 if (repo)
3728 got_repo_close(repo);
3729 if (worktree)
3730 got_worktree_close(worktree);
3731 TAILQ_FOREACH(pe, &paths, entry)
3732 free((char *)pe->path);
3733 got_pathlist_free(&paths);
3734 free(cwd);
3735 return error;
3738 __dead static void
3739 usage_remove(void)
3741 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3742 exit(1);
3745 static const struct got_error *
3746 cmd_remove(int argc, char *argv[])
3748 const struct got_error *error = NULL;
3749 struct got_worktree *worktree = NULL;
3750 struct got_repository *repo = NULL;
3751 char *cwd = NULL;
3752 struct got_pathlist_head paths;
3753 struct got_pathlist_entry *pe;
3754 int ch, delete_local_mods = 0;
3756 TAILQ_INIT(&paths);
3758 while ((ch = getopt(argc, argv, "f")) != -1) {
3759 switch (ch) {
3760 case 'f':
3761 delete_local_mods = 1;
3762 break;
3763 default:
3764 usage_add();
3765 /* NOTREACHED */
3769 argc -= optind;
3770 argv += optind;
3772 #ifndef PROFILE
3773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3774 NULL) == -1)
3775 err(1, "pledge");
3776 #endif
3777 if (argc < 1)
3778 usage_remove();
3780 cwd = getcwd(NULL, 0);
3781 if (cwd == NULL) {
3782 error = got_error_from_errno("getcwd");
3783 goto done;
3785 error = got_worktree_open(&worktree, cwd);
3786 if (error)
3787 goto done;
3789 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3790 if (error)
3791 goto done;
3793 error = apply_unveil(got_repo_get_path(repo), 1,
3794 got_worktree_get_root_path(worktree));
3795 if (error)
3796 goto done;
3798 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3799 if (error)
3800 goto done;
3802 error = got_worktree_schedule_delete(worktree, &paths,
3803 delete_local_mods, print_status, NULL, repo);
3804 if (error)
3805 goto done;
3806 done:
3807 if (repo)
3808 got_repo_close(repo);
3809 if (worktree)
3810 got_worktree_close(worktree);
3811 TAILQ_FOREACH(pe, &paths, entry)
3812 free((char *)pe->path);
3813 got_pathlist_free(&paths);
3814 free(cwd);
3815 return error;
3818 __dead static void
3819 usage_revert(void)
3821 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3822 "path ...\n", getprogname());
3823 exit(1);
3826 static const struct got_error *
3827 revert_progress(void *arg, unsigned char status, const char *path)
3829 while (path[0] == '/')
3830 path++;
3831 printf("%c %s\n", status, path);
3832 return NULL;
3835 struct choose_patch_arg {
3836 FILE *patch_script_file;
3837 const char *action;
3840 static const struct got_error *
3841 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3842 int nchanges, const char *action)
3844 char *line = NULL;
3845 size_t linesize = 0;
3846 ssize_t linelen;
3848 switch (status) {
3849 case GOT_STATUS_ADD:
3850 printf("A %s\n%s this addition? [y/n] ", path, action);
3851 break;
3852 case GOT_STATUS_DELETE:
3853 printf("D %s\n%s this deletion? [y/n] ", path, action);
3854 break;
3855 case GOT_STATUS_MODIFY:
3856 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3857 return got_error_from_errno("fseek");
3858 printf(GOT_COMMIT_SEP_STR);
3859 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3860 printf("%s", line);
3861 if (ferror(patch_file))
3862 return got_error_from_errno("getline");
3863 printf(GOT_COMMIT_SEP_STR);
3864 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3865 path, n, nchanges, action);
3866 break;
3867 default:
3868 return got_error_path(path, GOT_ERR_FILE_STATUS);
3871 return NULL;
3874 static const struct got_error *
3875 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3876 FILE *patch_file, int n, int nchanges)
3878 const struct got_error *err = NULL;
3879 char *line = NULL;
3880 size_t linesize = 0;
3881 ssize_t linelen;
3882 int resp = ' ';
3883 struct choose_patch_arg *a = arg;
3885 *choice = GOT_PATCH_CHOICE_NONE;
3887 if (a->patch_script_file) {
3888 char *nl;
3889 err = show_change(status, path, patch_file, n, nchanges,
3890 a->action);
3891 if (err)
3892 return err;
3893 linelen = getline(&line, &linesize, a->patch_script_file);
3894 if (linelen == -1) {
3895 if (ferror(a->patch_script_file))
3896 return got_error_from_errno("getline");
3897 return NULL;
3899 nl = strchr(line, '\n');
3900 if (nl)
3901 *nl = '\0';
3902 if (strcmp(line, "y") == 0) {
3903 *choice = GOT_PATCH_CHOICE_YES;
3904 printf("y\n");
3905 } else if (strcmp(line, "n") == 0) {
3906 *choice = GOT_PATCH_CHOICE_NO;
3907 printf("n\n");
3908 } else if (strcmp(line, "q") == 0 &&
3909 status == GOT_STATUS_MODIFY) {
3910 *choice = GOT_PATCH_CHOICE_QUIT;
3911 printf("q\n");
3912 } else
3913 printf("invalid response '%s'\n", line);
3914 free(line);
3915 return NULL;
3918 while (resp != 'y' && resp != 'n' && resp != 'q') {
3919 err = show_change(status, path, patch_file, n, nchanges,
3920 a->action);
3921 if (err)
3922 return err;
3923 resp = getchar();
3924 if (resp == '\n')
3925 resp = getchar();
3926 if (status == GOT_STATUS_MODIFY) {
3927 if (resp != 'y' && resp != 'n' && resp != 'q') {
3928 printf("invalid response '%c'\n", resp);
3929 resp = ' ';
3931 } else if (resp != 'y' && resp != 'n') {
3932 printf("invalid response '%c'\n", resp);
3933 resp = ' ';
3937 if (resp == 'y')
3938 *choice = GOT_PATCH_CHOICE_YES;
3939 else if (resp == 'n')
3940 *choice = GOT_PATCH_CHOICE_NO;
3941 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3942 *choice = GOT_PATCH_CHOICE_QUIT;
3944 return NULL;
3948 static const struct got_error *
3949 cmd_revert(int argc, char *argv[])
3951 const struct got_error *error = NULL;
3952 struct got_worktree *worktree = NULL;
3953 struct got_repository *repo = NULL;
3954 char *cwd = NULL, *path = NULL;
3955 struct got_pathlist_head paths;
3956 struct got_pathlist_entry *pe;
3957 int ch, can_recurse = 0, pflag = 0;
3958 FILE *patch_script_file = NULL;
3959 const char *patch_script_path = NULL;
3960 struct choose_patch_arg cpa;
3962 TAILQ_INIT(&paths);
3964 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3965 switch (ch) {
3966 case 'p':
3967 pflag = 1;
3968 break;
3969 case 'F':
3970 patch_script_path = optarg;
3971 break;
3972 case 'R':
3973 can_recurse = 1;
3974 break;
3975 default:
3976 usage_revert();
3977 /* NOTREACHED */
3981 argc -= optind;
3982 argv += optind;
3984 #ifndef PROFILE
3985 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3986 "unveil", NULL) == -1)
3987 err(1, "pledge");
3988 #endif
3989 if (argc < 1)
3990 usage_revert();
3991 if (patch_script_path && !pflag)
3992 errx(1, "-F option can only be used together with -p option");
3994 cwd = getcwd(NULL, 0);
3995 if (cwd == NULL) {
3996 error = got_error_from_errno("getcwd");
3997 goto done;
3999 error = got_worktree_open(&worktree, cwd);
4000 if (error)
4001 goto done;
4003 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4004 if (error != NULL)
4005 goto done;
4007 if (patch_script_path) {
4008 patch_script_file = fopen(patch_script_path, "r");
4009 if (patch_script_file == NULL) {
4010 error = got_error_from_errno2("fopen",
4011 patch_script_path);
4012 goto done;
4015 error = apply_unveil(got_repo_get_path(repo), 1,
4016 got_worktree_get_root_path(worktree));
4017 if (error)
4018 goto done;
4020 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4021 if (error)
4022 goto done;
4024 if (!can_recurse) {
4025 char *ondisk_path;
4026 struct stat sb;
4027 TAILQ_FOREACH(pe, &paths, entry) {
4028 if (asprintf(&ondisk_path, "%s/%s",
4029 got_worktree_get_root_path(worktree),
4030 pe->path) == -1) {
4031 error = got_error_from_errno("asprintf");
4032 goto done;
4034 if (lstat(ondisk_path, &sb) == -1) {
4035 if (errno == ENOENT) {
4036 free(ondisk_path);
4037 continue;
4039 error = got_error_from_errno2("lstat",
4040 ondisk_path);
4041 free(ondisk_path);
4042 goto done;
4044 free(ondisk_path);
4045 if (S_ISDIR(sb.st_mode)) {
4046 error = got_error_msg(GOT_ERR_BAD_PATH,
4047 "reverting directories requires -R option");
4048 goto done;
4053 cpa.patch_script_file = patch_script_file;
4054 cpa.action = "revert";
4055 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4056 pflag ? choose_patch : NULL, &cpa, repo);
4057 if (error)
4058 goto done;
4059 done:
4060 if (patch_script_file && fclose(patch_script_file) == EOF &&
4061 error == NULL)
4062 error = got_error_from_errno2("fclose", patch_script_path);
4063 if (repo)
4064 got_repo_close(repo);
4065 if (worktree)
4066 got_worktree_close(worktree);
4067 free(path);
4068 free(cwd);
4069 return error;
4072 __dead static void
4073 usage_commit(void)
4075 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4076 getprogname());
4077 exit(1);
4080 struct collect_commit_logmsg_arg {
4081 const char *cmdline_log;
4082 const char *editor;
4083 const char *worktree_path;
4084 const char *branch_name;
4085 const char *repo_path;
4086 char *logmsg_path;
4090 static const struct got_error *
4091 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4092 void *arg)
4094 char *initial_content = NULL;
4095 struct got_pathlist_entry *pe;
4096 const struct got_error *err = NULL;
4097 char *template = NULL;
4098 struct collect_commit_logmsg_arg *a = arg;
4099 int fd;
4100 size_t len;
4102 /* if a message was specified on the command line, just use it */
4103 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4104 len = strlen(a->cmdline_log) + 1;
4105 *logmsg = malloc(len + 1);
4106 if (*logmsg == NULL)
4107 return got_error_from_errno("malloc");
4108 strlcpy(*logmsg, a->cmdline_log, len);
4109 return NULL;
4112 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4113 return got_error_from_errno("asprintf");
4115 if (asprintf(&initial_content,
4116 "\n# changes to be committed on branch %s:\n",
4117 a->branch_name) == -1)
4118 return got_error_from_errno("asprintf");
4120 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4121 if (err)
4122 goto done;
4124 dprintf(fd, initial_content);
4126 TAILQ_FOREACH(pe, commitable_paths, entry) {
4127 struct got_commitable *ct = pe->data;
4128 dprintf(fd, "# %c %s\n",
4129 got_commitable_get_status(ct),
4130 got_commitable_get_path(ct));
4132 close(fd);
4134 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4135 done:
4136 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4137 unlink(a->logmsg_path);
4138 free(a->logmsg_path);
4139 a->logmsg_path = NULL;
4141 free(initial_content);
4142 free(template);
4144 /* Editor is done; we can now apply unveil(2) */
4145 if (err == NULL) {
4146 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4147 if (err) {
4148 free(*logmsg);
4149 *logmsg = NULL;
4152 return err;
4155 static const struct got_error *
4156 cmd_commit(int argc, char *argv[])
4158 const struct got_error *error = NULL;
4159 struct got_worktree *worktree = NULL;
4160 struct got_repository *repo = NULL;
4161 char *cwd = NULL, *id_str = NULL;
4162 struct got_object_id *id = NULL;
4163 const char *logmsg = NULL;
4164 const char *author;
4165 struct collect_commit_logmsg_arg cl_arg;
4166 char *editor = NULL;
4167 int ch, rebase_in_progress, histedit_in_progress;
4168 struct got_pathlist_head paths;
4170 TAILQ_INIT(&paths);
4171 cl_arg.logmsg_path = NULL;
4173 while ((ch = getopt(argc, argv, "m:")) != -1) {
4174 switch (ch) {
4175 case 'm':
4176 logmsg = optarg;
4177 break;
4178 default:
4179 usage_commit();
4180 /* NOTREACHED */
4184 argc -= optind;
4185 argv += optind;
4187 #ifndef PROFILE
4188 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4189 "unveil", NULL) == -1)
4190 err(1, "pledge");
4191 #endif
4192 error = get_author(&author);
4193 if (error)
4194 return error;
4196 cwd = getcwd(NULL, 0);
4197 if (cwd == NULL) {
4198 error = got_error_from_errno("getcwd");
4199 goto done;
4201 error = got_worktree_open(&worktree, cwd);
4202 if (error)
4203 goto done;
4205 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4206 if (error)
4207 goto done;
4208 if (rebase_in_progress) {
4209 error = got_error(GOT_ERR_REBASING);
4210 goto done;
4213 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4214 worktree);
4215 if (error)
4216 goto done;
4218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4219 if (error != NULL)
4220 goto done;
4223 * unveil(2) traverses exec(2); if an editor is used we have
4224 * to apply unveil after the log message has been written.
4226 if (logmsg == NULL || strlen(logmsg) == 0)
4227 error = get_editor(&editor);
4228 else
4229 error = apply_unveil(got_repo_get_path(repo), 0,
4230 got_worktree_get_root_path(worktree));
4231 if (error)
4232 goto done;
4234 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4235 if (error)
4236 goto done;
4238 cl_arg.editor = editor;
4239 cl_arg.cmdline_log = logmsg;
4240 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4241 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4242 if (!histedit_in_progress) {
4243 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4244 error = got_error(GOT_ERR_COMMIT_BRANCH);
4245 goto done;
4247 cl_arg.branch_name += 11;
4249 cl_arg.repo_path = got_repo_get_path(repo);
4250 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4251 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4252 if (error) {
4253 if (cl_arg.logmsg_path)
4254 fprintf(stderr, "%s: log message preserved in %s\n",
4255 getprogname(), cl_arg.logmsg_path);
4256 goto done;
4259 if (cl_arg.logmsg_path)
4260 unlink(cl_arg.logmsg_path);
4262 error = got_object_id_str(&id_str, id);
4263 if (error)
4264 goto done;
4265 printf("Created commit %s\n", id_str);
4266 done:
4267 free(cl_arg.logmsg_path);
4268 if (repo)
4269 got_repo_close(repo);
4270 if (worktree)
4271 got_worktree_close(worktree);
4272 free(cwd);
4273 free(id_str);
4274 free(editor);
4275 return error;
4278 __dead static void
4279 usage_cherrypick(void)
4281 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4282 exit(1);
4285 static const struct got_error *
4286 cmd_cherrypick(int argc, char *argv[])
4288 const struct got_error *error = NULL;
4289 struct got_worktree *worktree = NULL;
4290 struct got_repository *repo = NULL;
4291 char *cwd = NULL, *commit_id_str = NULL;
4292 struct got_object_id *commit_id = NULL;
4293 struct got_commit_object *commit = NULL;
4294 struct got_object_qid *pid;
4295 struct got_reference *head_ref = NULL;
4296 int ch, did_something = 0;
4298 while ((ch = getopt(argc, argv, "")) != -1) {
4299 switch (ch) {
4300 default:
4301 usage_cherrypick();
4302 /* NOTREACHED */
4306 argc -= optind;
4307 argv += optind;
4309 #ifndef PROFILE
4310 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4311 "unveil", NULL) == -1)
4312 err(1, "pledge");
4313 #endif
4314 if (argc != 1)
4315 usage_cherrypick();
4317 cwd = getcwd(NULL, 0);
4318 if (cwd == NULL) {
4319 error = got_error_from_errno("getcwd");
4320 goto done;
4322 error = got_worktree_open(&worktree, cwd);
4323 if (error)
4324 goto done;
4326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4327 if (error != NULL)
4328 goto done;
4330 error = apply_unveil(got_repo_get_path(repo), 0,
4331 got_worktree_get_root_path(worktree));
4332 if (error)
4333 goto done;
4335 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4336 GOT_OBJ_TYPE_COMMIT, repo);
4337 if (error != NULL) {
4338 struct got_reference *ref;
4339 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4340 goto done;
4341 error = got_ref_open(&ref, repo, argv[0], 0);
4342 if (error != NULL)
4343 goto done;
4344 error = got_ref_resolve(&commit_id, repo, ref);
4345 got_ref_close(ref);
4346 if (error != NULL)
4347 goto done;
4349 error = got_object_id_str(&commit_id_str, commit_id);
4350 if (error)
4351 goto done;
4353 error = got_ref_open(&head_ref, repo,
4354 got_worktree_get_head_ref_name(worktree), 0);
4355 if (error != NULL)
4356 goto done;
4358 error = check_same_branch(commit_id, head_ref, NULL, repo);
4359 if (error) {
4360 if (error->code != GOT_ERR_ANCESTRY)
4361 goto done;
4362 error = NULL;
4363 } else {
4364 error = got_error(GOT_ERR_SAME_BRANCH);
4365 goto done;
4368 error = got_object_open_as_commit(&commit, repo, commit_id);
4369 if (error)
4370 goto done;
4371 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4372 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4373 commit_id, repo, update_progress, &did_something, check_cancelled,
4374 NULL);
4375 if (error != NULL)
4376 goto done;
4378 if (did_something)
4379 printf("Merged commit %s\n", commit_id_str);
4380 done:
4381 if (commit)
4382 got_object_commit_close(commit);
4383 free(commit_id_str);
4384 if (head_ref)
4385 got_ref_close(head_ref);
4386 if (worktree)
4387 got_worktree_close(worktree);
4388 if (repo)
4389 got_repo_close(repo);
4390 return error;
4393 __dead static void
4394 usage_backout(void)
4396 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4397 exit(1);
4400 static const struct got_error *
4401 cmd_backout(int argc, char *argv[])
4403 const struct got_error *error = NULL;
4404 struct got_worktree *worktree = NULL;
4405 struct got_repository *repo = NULL;
4406 char *cwd = NULL, *commit_id_str = NULL;
4407 struct got_object_id *commit_id = NULL;
4408 struct got_commit_object *commit = NULL;
4409 struct got_object_qid *pid;
4410 struct got_reference *head_ref = NULL;
4411 int ch, did_something = 0;
4413 while ((ch = getopt(argc, argv, "")) != -1) {
4414 switch (ch) {
4415 default:
4416 usage_backout();
4417 /* NOTREACHED */
4421 argc -= optind;
4422 argv += optind;
4424 #ifndef PROFILE
4425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4426 "unveil", NULL) == -1)
4427 err(1, "pledge");
4428 #endif
4429 if (argc != 1)
4430 usage_backout();
4432 cwd = getcwd(NULL, 0);
4433 if (cwd == NULL) {
4434 error = got_error_from_errno("getcwd");
4435 goto done;
4437 error = got_worktree_open(&worktree, cwd);
4438 if (error)
4439 goto done;
4441 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4442 if (error != NULL)
4443 goto done;
4445 error = apply_unveil(got_repo_get_path(repo), 0,
4446 got_worktree_get_root_path(worktree));
4447 if (error)
4448 goto done;
4450 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4451 GOT_OBJ_TYPE_COMMIT, repo);
4452 if (error != NULL) {
4453 struct got_reference *ref;
4454 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4455 goto done;
4456 error = got_ref_open(&ref, repo, argv[0], 0);
4457 if (error != NULL)
4458 goto done;
4459 error = got_ref_resolve(&commit_id, repo, ref);
4460 got_ref_close(ref);
4461 if (error != NULL)
4462 goto done;
4464 error = got_object_id_str(&commit_id_str, commit_id);
4465 if (error)
4466 goto done;
4468 error = got_ref_open(&head_ref, repo,
4469 got_worktree_get_head_ref_name(worktree), 0);
4470 if (error != NULL)
4471 goto done;
4473 error = check_same_branch(commit_id, head_ref, NULL, repo);
4474 if (error)
4475 goto done;
4477 error = got_object_open_as_commit(&commit, repo, commit_id);
4478 if (error)
4479 goto done;
4480 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4481 if (pid == NULL) {
4482 error = got_error(GOT_ERR_ROOT_COMMIT);
4483 goto done;
4486 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4487 update_progress, &did_something, check_cancelled, NULL);
4488 if (error != NULL)
4489 goto done;
4491 if (did_something)
4492 printf("Backed out commit %s\n", commit_id_str);
4493 done:
4494 if (commit)
4495 got_object_commit_close(commit);
4496 free(commit_id_str);
4497 if (head_ref)
4498 got_ref_close(head_ref);
4499 if (worktree)
4500 got_worktree_close(worktree);
4501 if (repo)
4502 got_repo_close(repo);
4503 return error;
4506 __dead static void
4507 usage_rebase(void)
4509 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4510 getprogname());
4511 exit(1);
4514 void
4515 trim_logmsg(char *logmsg, int limit)
4517 char *nl;
4518 size_t len;
4520 len = strlen(logmsg);
4521 if (len > limit)
4522 len = limit;
4523 logmsg[len] = '\0';
4524 nl = strchr(logmsg, '\n');
4525 if (nl)
4526 *nl = '\0';
4529 static const struct got_error *
4530 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4532 const struct got_error *err;
4533 char *logmsg0 = NULL;
4534 const char *s;
4536 err = got_object_commit_get_logmsg(&logmsg0, commit);
4537 if (err)
4538 return err;
4540 s = logmsg0;
4541 while (isspace((unsigned char)s[0]))
4542 s++;
4544 *logmsg = strdup(s);
4545 if (*logmsg == NULL) {
4546 err = got_error_from_errno("strdup");
4547 goto done;
4550 trim_logmsg(*logmsg, limit);
4551 done:
4552 free(logmsg0);
4553 return err;
4556 static const struct got_error *
4557 show_rebase_progress(struct got_commit_object *commit,
4558 struct got_object_id *old_id, struct got_object_id *new_id)
4560 const struct got_error *err;
4561 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4563 err = got_object_id_str(&old_id_str, old_id);
4564 if (err)
4565 goto done;
4567 if (new_id) {
4568 err = got_object_id_str(&new_id_str, new_id);
4569 if (err)
4570 goto done;
4573 old_id_str[12] = '\0';
4574 if (new_id_str)
4575 new_id_str[12] = '\0';
4577 err = get_short_logmsg(&logmsg, 42, commit);
4578 if (err)
4579 goto done;
4581 printf("%s -> %s: %s\n", old_id_str,
4582 new_id_str ? new_id_str : "no-op change", logmsg);
4583 done:
4584 free(old_id_str);
4585 free(new_id_str);
4586 return err;
4589 static const struct got_error *
4590 rebase_progress(void *arg, unsigned char status, const char *path)
4592 unsigned char *rebase_status = arg;
4594 while (path[0] == '/')
4595 path++;
4596 printf("%c %s\n", status, path);
4598 if (*rebase_status == GOT_STATUS_CONFLICT)
4599 return NULL;
4600 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4601 *rebase_status = status;
4602 return NULL;
4605 static const struct got_error *
4606 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4607 struct got_reference *branch, struct got_reference *new_base_branch,
4608 struct got_reference *tmp_branch, struct got_repository *repo)
4610 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4611 return got_worktree_rebase_complete(worktree, fileindex,
4612 new_base_branch, tmp_branch, branch, repo);
4615 static const struct got_error *
4616 rebase_commit(struct got_pathlist_head *merged_paths,
4617 struct got_worktree *worktree, struct got_fileindex *fileindex,
4618 struct got_reference *tmp_branch,
4619 struct got_object_id *commit_id, struct got_repository *repo)
4621 const struct got_error *error;
4622 struct got_commit_object *commit;
4623 struct got_object_id *new_commit_id;
4625 error = got_object_open_as_commit(&commit, repo, commit_id);
4626 if (error)
4627 return error;
4629 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4630 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4631 if (error) {
4632 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4633 goto done;
4634 error = show_rebase_progress(commit, commit_id, NULL);
4635 } else {
4636 error = show_rebase_progress(commit, commit_id, new_commit_id);
4637 free(new_commit_id);
4639 done:
4640 got_object_commit_close(commit);
4641 return error;
4644 struct check_path_prefix_arg {
4645 const char *path_prefix;
4646 size_t len;
4647 int errcode;
4650 static const struct got_error *
4651 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4652 struct got_blob_object *blob2, struct got_object_id *id1,
4653 struct got_object_id *id2, const char *path1, const char *path2,
4654 struct got_repository *repo)
4656 struct check_path_prefix_arg *a = arg;
4658 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4659 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4660 return got_error(a->errcode);
4662 return NULL;
4665 static const struct got_error *
4666 check_path_prefix(struct got_object_id *parent_id,
4667 struct got_object_id *commit_id, const char *path_prefix,
4668 int errcode, struct got_repository *repo)
4670 const struct got_error *err;
4671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4672 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4673 struct check_path_prefix_arg cpp_arg;
4675 if (got_path_is_root_dir(path_prefix))
4676 return NULL;
4678 err = got_object_open_as_commit(&commit, repo, commit_id);
4679 if (err)
4680 goto done;
4682 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4683 if (err)
4684 goto done;
4686 err = got_object_open_as_tree(&tree1, repo,
4687 got_object_commit_get_tree_id(parent_commit));
4688 if (err)
4689 goto done;
4691 err = got_object_open_as_tree(&tree2, repo,
4692 got_object_commit_get_tree_id(commit));
4693 if (err)
4694 goto done;
4696 cpp_arg.path_prefix = path_prefix;
4697 while (cpp_arg.path_prefix[0] == '/')
4698 cpp_arg.path_prefix++;
4699 cpp_arg.len = strlen(cpp_arg.path_prefix);
4700 cpp_arg.errcode = errcode;
4701 err = got_diff_tree(tree1, tree2, "", "", repo,
4702 check_path_prefix_in_diff, &cpp_arg, 0);
4703 done:
4704 if (tree1)
4705 got_object_tree_close(tree1);
4706 if (tree2)
4707 got_object_tree_close(tree2);
4708 if (commit)
4709 got_object_commit_close(commit);
4710 if (parent_commit)
4711 got_object_commit_close(parent_commit);
4712 return err;
4715 static const struct got_error *
4716 collect_commits(struct got_object_id_queue *commits,
4717 struct got_object_id *initial_commit_id,
4718 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4719 const char *path_prefix, int path_prefix_errcode,
4720 struct got_repository *repo)
4722 const struct got_error *err = NULL;
4723 struct got_commit_graph *graph = NULL;
4724 struct got_object_id *parent_id = NULL;
4725 struct got_object_qid *qid;
4726 struct got_object_id *commit_id = initial_commit_id;
4728 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4729 if (err)
4730 return err;
4732 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4733 check_cancelled, NULL);
4734 if (err)
4735 goto done;
4736 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4737 err = got_commit_graph_iter_next(&parent_id, graph);
4738 if (err) {
4739 if (err->code == GOT_ERR_ITER_COMPLETED) {
4740 err = got_error_msg(GOT_ERR_ANCESTRY,
4741 "ran out of commits to rebase before "
4742 "youngest common ancestor commit has "
4743 "been reached?!?");
4744 goto done;
4745 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4746 goto done;
4747 err = got_commit_graph_fetch_commits(graph, 1, repo,
4748 check_cancelled, NULL);
4749 if (err)
4750 goto done;
4751 } else {
4752 err = check_path_prefix(parent_id, commit_id,
4753 path_prefix, path_prefix_errcode, repo);
4754 if (err)
4755 goto done;
4757 err = got_object_qid_alloc(&qid, commit_id);
4758 if (err)
4759 goto done;
4760 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4761 commit_id = parent_id;
4764 done:
4765 got_commit_graph_close(graph);
4766 return err;
4769 static const struct got_error *
4770 cmd_rebase(int argc, char *argv[])
4772 const struct got_error *error = NULL;
4773 struct got_worktree *worktree = NULL;
4774 struct got_repository *repo = NULL;
4775 struct got_fileindex *fileindex = NULL;
4776 char *cwd = NULL;
4777 struct got_reference *branch = NULL;
4778 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4779 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4780 struct got_object_id *resume_commit_id = NULL;
4781 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4782 struct got_commit_object *commit = NULL;
4783 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4784 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4785 struct got_object_id_queue commits;
4786 struct got_pathlist_head merged_paths;
4787 const struct got_object_id_queue *parent_ids;
4788 struct got_object_qid *qid, *pid;
4790 SIMPLEQ_INIT(&commits);
4791 TAILQ_INIT(&merged_paths);
4793 while ((ch = getopt(argc, argv, "ac")) != -1) {
4794 switch (ch) {
4795 case 'a':
4796 abort_rebase = 1;
4797 break;
4798 case 'c':
4799 continue_rebase = 1;
4800 break;
4801 default:
4802 usage_rebase();
4803 /* NOTREACHED */
4807 argc -= optind;
4808 argv += optind;
4810 #ifndef PROFILE
4811 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4812 "unveil", NULL) == -1)
4813 err(1, "pledge");
4814 #endif
4815 if (abort_rebase && continue_rebase)
4816 usage_rebase();
4817 else if (abort_rebase || continue_rebase) {
4818 if (argc != 0)
4819 usage_rebase();
4820 } else if (argc != 1)
4821 usage_rebase();
4823 cwd = getcwd(NULL, 0);
4824 if (cwd == NULL) {
4825 error = got_error_from_errno("getcwd");
4826 goto done;
4828 error = got_worktree_open(&worktree, cwd);
4829 if (error)
4830 goto done;
4832 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4833 if (error != NULL)
4834 goto done;
4836 error = apply_unveil(got_repo_get_path(repo), 0,
4837 got_worktree_get_root_path(worktree));
4838 if (error)
4839 goto done;
4841 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4842 if (error)
4843 goto done;
4845 if (abort_rebase) {
4846 int did_something;
4847 if (!rebase_in_progress) {
4848 error = got_error(GOT_ERR_NOT_REBASING);
4849 goto done;
4851 error = got_worktree_rebase_continue(&resume_commit_id,
4852 &new_base_branch, &tmp_branch, &branch, &fileindex,
4853 worktree, repo);
4854 if (error)
4855 goto done;
4856 printf("Switching work tree to %s\n",
4857 got_ref_get_symref_target(new_base_branch));
4858 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4859 new_base_branch, update_progress, &did_something);
4860 if (error)
4861 goto done;
4862 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4863 goto done; /* nothing else to do */
4866 if (continue_rebase) {
4867 if (!rebase_in_progress) {
4868 error = got_error(GOT_ERR_NOT_REBASING);
4869 goto done;
4871 error = got_worktree_rebase_continue(&resume_commit_id,
4872 &new_base_branch, &tmp_branch, &branch, &fileindex,
4873 worktree, repo);
4874 if (error)
4875 goto done;
4877 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4878 resume_commit_id, repo);
4879 if (error)
4880 goto done;
4882 yca_id = got_object_id_dup(resume_commit_id);
4883 if (yca_id == NULL) {
4884 error = got_error_from_errno("got_object_id_dup");
4885 goto done;
4887 } else {
4888 error = got_ref_open(&branch, repo, argv[0], 0);
4889 if (error != NULL)
4890 goto done;
4893 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4894 if (error)
4895 goto done;
4897 if (!continue_rebase) {
4898 struct got_object_id *base_commit_id;
4900 base_commit_id = got_worktree_get_base_commit_id(worktree);
4901 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4902 base_commit_id, branch_head_commit_id, repo,
4903 check_cancelled, NULL);
4904 if (error)
4905 goto done;
4906 if (yca_id == NULL) {
4907 error = got_error_msg(GOT_ERR_ANCESTRY,
4908 "specified branch shares no common ancestry "
4909 "with work tree's branch");
4910 goto done;
4913 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4914 if (error) {
4915 if (error->code != GOT_ERR_ANCESTRY)
4916 goto done;
4917 error = NULL;
4918 } else {
4919 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4920 "specified branch resolves to a commit which "
4921 "is already contained in work tree's branch");
4922 goto done;
4924 error = got_worktree_rebase_prepare(&new_base_branch,
4925 &tmp_branch, &fileindex, worktree, branch, repo);
4926 if (error)
4927 goto done;
4930 commit_id = branch_head_commit_id;
4931 error = got_object_open_as_commit(&commit, repo, commit_id);
4932 if (error)
4933 goto done;
4935 parent_ids = got_object_commit_get_parent_ids(commit);
4936 pid = SIMPLEQ_FIRST(parent_ids);
4937 if (pid == NULL) {
4938 if (!continue_rebase) {
4939 int did_something;
4940 error = got_worktree_rebase_abort(worktree, fileindex,
4941 repo, new_base_branch, update_progress,
4942 &did_something);
4943 if (error)
4944 goto done;
4945 printf("Rebase of %s aborted\n",
4946 got_ref_get_name(branch));
4948 error = got_error(GOT_ERR_EMPTY_REBASE);
4949 goto done;
4951 error = collect_commits(&commits, commit_id, pid->id,
4952 yca_id, got_worktree_get_path_prefix(worktree),
4953 GOT_ERR_REBASE_PATH, repo);
4954 got_object_commit_close(commit);
4955 commit = NULL;
4956 if (error)
4957 goto done;
4959 if (SIMPLEQ_EMPTY(&commits)) {
4960 if (continue_rebase)
4961 error = rebase_complete(worktree, fileindex,
4962 branch, new_base_branch, tmp_branch, repo);
4963 else
4964 error = got_error(GOT_ERR_EMPTY_REBASE);
4965 goto done;
4968 pid = NULL;
4969 SIMPLEQ_FOREACH(qid, &commits, entry) {
4970 commit_id = qid->id;
4971 parent_id = pid ? pid->id : yca_id;
4972 pid = qid;
4974 error = got_worktree_rebase_merge_files(&merged_paths,
4975 worktree, fileindex, parent_id, commit_id, repo,
4976 rebase_progress, &rebase_status, check_cancelled, NULL);
4977 if (error)
4978 goto done;
4980 if (rebase_status == GOT_STATUS_CONFLICT) {
4981 got_worktree_rebase_pathlist_free(&merged_paths);
4982 break;
4985 error = rebase_commit(&merged_paths, worktree, fileindex,
4986 tmp_branch, commit_id, repo);
4987 got_worktree_rebase_pathlist_free(&merged_paths);
4988 if (error)
4989 goto done;
4992 if (rebase_status == GOT_STATUS_CONFLICT) {
4993 error = got_worktree_rebase_postpone(worktree, fileindex);
4994 if (error)
4995 goto done;
4996 error = got_error_msg(GOT_ERR_CONFLICTS,
4997 "conflicts must be resolved before rebasing can continue");
4998 } else
4999 error = rebase_complete(worktree, fileindex, branch,
5000 new_base_branch, tmp_branch, repo);
5001 done:
5002 got_object_id_queue_free(&commits);
5003 free(branch_head_commit_id);
5004 free(resume_commit_id);
5005 free(yca_id);
5006 if (commit)
5007 got_object_commit_close(commit);
5008 if (branch)
5009 got_ref_close(branch);
5010 if (new_base_branch)
5011 got_ref_close(new_base_branch);
5012 if (tmp_branch)
5013 got_ref_close(tmp_branch);
5014 if (worktree)
5015 got_worktree_close(worktree);
5016 if (repo)
5017 got_repo_close(repo);
5018 return error;
5021 __dead static void
5022 usage_histedit(void)
5024 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5025 getprogname());
5026 exit(1);
5029 #define GOT_HISTEDIT_PICK 'p'
5030 #define GOT_HISTEDIT_EDIT 'e'
5031 #define GOT_HISTEDIT_FOLD 'f'
5032 #define GOT_HISTEDIT_DROP 'd'
5033 #define GOT_HISTEDIT_MESG 'm'
5035 static struct got_histedit_cmd {
5036 unsigned char code;
5037 const char *name;
5038 const char *desc;
5039 } got_histedit_cmds[] = {
5040 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5041 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5042 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5043 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5044 { GOT_HISTEDIT_MESG, "mesg",
5045 "single-line log message for commit above (open editor if empty)" },
5048 struct got_histedit_list_entry {
5049 TAILQ_ENTRY(got_histedit_list_entry) entry;
5050 struct got_object_id *commit_id;
5051 const struct got_histedit_cmd *cmd;
5052 char *logmsg;
5054 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5056 static const struct got_error *
5057 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5058 FILE *f, struct got_repository *repo)
5060 const struct got_error *err = NULL;
5061 char *logmsg = NULL, *id_str = NULL;
5062 struct got_commit_object *commit = NULL;
5063 int n;
5065 err = got_object_open_as_commit(&commit, repo, commit_id);
5066 if (err)
5067 goto done;
5069 err = get_short_logmsg(&logmsg, 34, commit);
5070 if (err)
5071 goto done;
5073 err = got_object_id_str(&id_str, commit_id);
5074 if (err)
5075 goto done;
5077 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5078 if (n < 0)
5079 err = got_ferror(f, GOT_ERR_IO);
5080 done:
5081 if (commit)
5082 got_object_commit_close(commit);
5083 free(id_str);
5084 free(logmsg);
5085 return err;
5088 static const struct got_error *
5089 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5090 struct got_repository *repo)
5092 const struct got_error *err = NULL;
5093 struct got_object_qid *qid;
5095 if (SIMPLEQ_EMPTY(commits))
5096 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5098 SIMPLEQ_FOREACH(qid, commits, entry) {
5099 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5100 f, repo);
5101 if (err)
5102 break;
5105 return err;
5108 static const struct got_error *
5109 write_cmd_list(FILE *f)
5111 const struct got_error *err = NULL;
5112 int n, i;
5114 n = fprintf(f, "# Available histedit commands:\n");
5115 if (n < 0)
5116 return got_ferror(f, GOT_ERR_IO);
5118 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5119 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5120 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5121 cmd->desc);
5122 if (n < 0) {
5123 err = got_ferror(f, GOT_ERR_IO);
5124 break;
5127 n = fprintf(f, "# Commits will be processed in order from top to "
5128 "bottom of this file.\n");
5129 if (n < 0)
5130 return got_ferror(f, GOT_ERR_IO);
5131 return err;
5134 static const struct got_error *
5135 histedit_syntax_error(int lineno)
5137 static char msg[42];
5138 int ret;
5140 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5141 lineno);
5142 if (ret == -1 || ret >= sizeof(msg))
5143 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5145 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5148 static const struct got_error *
5149 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5150 char *logmsg, struct got_repository *repo)
5152 const struct got_error *err;
5153 struct got_commit_object *folded_commit = NULL;
5154 char *id_str, *folded_logmsg = NULL;
5156 err = got_object_id_str(&id_str, hle->commit_id);
5157 if (err)
5158 return err;
5160 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5161 if (err)
5162 goto done;
5164 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5165 if (err)
5166 goto done;
5167 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5168 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5169 folded_logmsg) == -1) {
5170 err = got_error_from_errno("asprintf");
5171 goto done;
5173 done:
5174 if (folded_commit)
5175 got_object_commit_close(folded_commit);
5176 free(id_str);
5177 free(folded_logmsg);
5178 return err;
5181 static struct got_histedit_list_entry *
5182 get_folded_commits(struct got_histedit_list_entry *hle)
5184 struct got_histedit_list_entry *prev, *folded = NULL;
5186 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5187 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5188 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5189 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5190 folded = prev;
5191 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5194 return folded;
5197 static const struct got_error *
5198 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5199 struct got_repository *repo)
5201 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5202 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5203 const struct got_error *err = NULL;
5204 struct got_commit_object *commit = NULL;
5205 int fd;
5206 struct got_histedit_list_entry *folded = NULL;
5208 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5209 if (err)
5210 return err;
5212 folded = get_folded_commits(hle);
5213 if (folded) {
5214 while (folded != hle) {
5215 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5216 folded = TAILQ_NEXT(folded, entry);
5217 continue;
5219 err = append_folded_commit_msg(&new_msg, folded,
5220 logmsg, repo);
5221 if (err)
5222 goto done;
5223 free(logmsg);
5224 logmsg = new_msg;
5225 folded = TAILQ_NEXT(folded, entry);
5229 err = got_object_id_str(&id_str, hle->commit_id);
5230 if (err)
5231 goto done;
5232 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5233 if (err)
5234 goto done;
5235 if (asprintf(&new_msg,
5236 "%s\n# original log message of commit %s: %s",
5237 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5238 err = got_error_from_errno("asprintf");
5239 goto done;
5241 free(logmsg);
5242 logmsg = new_msg;
5244 err = got_object_id_str(&id_str, hle->commit_id);
5245 if (err)
5246 goto done;
5248 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5249 if (err)
5250 goto done;
5252 dprintf(fd, logmsg);
5253 close(fd);
5255 err = get_editor(&editor);
5256 if (err)
5257 goto done;
5259 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5260 if (err) {
5261 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5262 goto done;
5263 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5265 done:
5266 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5267 err = got_error_from_errno2("unlink", logmsg_path);
5268 free(logmsg_path);
5269 free(logmsg);
5270 free(orig_logmsg);
5271 free(editor);
5272 if (commit)
5273 got_object_commit_close(commit);
5274 return err;
5277 static const struct got_error *
5278 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5279 FILE *f, struct got_repository *repo)
5281 const struct got_error *err = NULL;
5282 char *line = NULL, *p, *end;
5283 size_t size;
5284 ssize_t len;
5285 int lineno = 0, i;
5286 const struct got_histedit_cmd *cmd;
5287 struct got_object_id *commit_id = NULL;
5288 struct got_histedit_list_entry *hle = NULL;
5290 for (;;) {
5291 len = getline(&line, &size, f);
5292 if (len == -1) {
5293 const struct got_error *getline_err;
5294 if (feof(f))
5295 break;
5296 getline_err = got_error_from_errno("getline");
5297 err = got_ferror(f, getline_err->code);
5298 break;
5300 lineno++;
5301 p = line;
5302 while (isspace((unsigned char)p[0]))
5303 p++;
5304 if (p[0] == '#' || p[0] == '\0') {
5305 free(line);
5306 line = NULL;
5307 continue;
5309 cmd = NULL;
5310 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5311 cmd = &got_histedit_cmds[i];
5312 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5313 isspace((unsigned char)p[strlen(cmd->name)])) {
5314 p += strlen(cmd->name);
5315 break;
5317 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5318 p++;
5319 break;
5322 if (i == nitems(got_histedit_cmds)) {
5323 err = histedit_syntax_error(lineno);
5324 break;
5326 while (isspace((unsigned char)p[0]))
5327 p++;
5328 if (cmd->code == GOT_HISTEDIT_MESG) {
5329 if (hle == NULL || hle->logmsg != NULL) {
5330 err = got_error(GOT_ERR_HISTEDIT_CMD);
5331 break;
5333 if (p[0] == '\0') {
5334 err = histedit_edit_logmsg(hle, repo);
5335 if (err)
5336 break;
5337 } else {
5338 hle->logmsg = strdup(p);
5339 if (hle->logmsg == NULL) {
5340 err = got_error_from_errno("strdup");
5341 break;
5344 free(line);
5345 line = NULL;
5346 continue;
5347 } else {
5348 end = p;
5349 while (end[0] && !isspace((unsigned char)end[0]))
5350 end++;
5351 *end = '\0';
5353 err = got_object_resolve_id_str(&commit_id, repo, p);
5354 if (err) {
5355 /* override error code */
5356 err = histedit_syntax_error(lineno);
5357 break;
5360 hle = malloc(sizeof(*hle));
5361 if (hle == NULL) {
5362 err = got_error_from_errno("malloc");
5363 break;
5365 hle->cmd = cmd;
5366 hle->commit_id = commit_id;
5367 hle->logmsg = NULL;
5368 commit_id = NULL;
5369 free(line);
5370 line = NULL;
5371 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5374 free(line);
5375 free(commit_id);
5376 return err;
5379 static const struct got_error *
5380 histedit_check_script(struct got_histedit_list *histedit_cmds,
5381 struct got_object_id_queue *commits, struct got_repository *repo)
5383 const struct got_error *err = NULL;
5384 struct got_object_qid *qid;
5385 struct got_histedit_list_entry *hle;
5386 static char msg[80];
5387 char *id_str;
5389 if (TAILQ_EMPTY(histedit_cmds))
5390 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5391 "histedit script contains no commands");
5392 if (SIMPLEQ_EMPTY(commits))
5393 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5395 SIMPLEQ_FOREACH(qid, commits, entry) {
5396 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5397 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5398 break;
5400 if (hle == NULL) {
5401 err = got_object_id_str(&id_str, qid->id);
5402 if (err)
5403 return err;
5404 snprintf(msg, sizeof(msg),
5405 "commit %s missing from histedit script", id_str);
5406 free(id_str);
5407 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5411 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5412 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5413 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5414 "last commit in histedit script cannot be folded");
5416 return NULL;
5419 static const struct got_error *
5420 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5421 const char *path, struct got_object_id_queue *commits,
5422 struct got_repository *repo)
5424 const struct got_error *err = NULL;
5425 char *editor;
5426 FILE *f = NULL;
5428 err = get_editor(&editor);
5429 if (err)
5430 return err;
5432 if (spawn_editor(editor, path) == -1) {
5433 err = got_error_from_errno("failed spawning editor");
5434 goto done;
5437 f = fopen(path, "r");
5438 if (f == NULL) {
5439 err = got_error_from_errno("fopen");
5440 goto done;
5442 err = histedit_parse_list(histedit_cmds, f, repo);
5443 if (err)
5444 goto done;
5446 err = histedit_check_script(histedit_cmds, commits, repo);
5447 done:
5448 if (f && fclose(f) != 0 && err == NULL)
5449 err = got_error_from_errno("fclose");
5450 free(editor);
5451 return err;
5454 static const struct got_error *
5455 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5456 struct got_object_id_queue *, const char *, struct got_repository *);
5458 static const struct got_error *
5459 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5460 struct got_object_id_queue *commits, struct got_repository *repo)
5462 const struct got_error *err;
5463 FILE *f = NULL;
5464 char *path = NULL;
5466 err = got_opentemp_named(&path, &f, "got-histedit");
5467 if (err)
5468 return err;
5470 err = write_cmd_list(f);
5471 if (err)
5472 goto done;
5474 err = histedit_write_commit_list(commits, f, repo);
5475 if (err)
5476 goto done;
5478 if (fclose(f) != 0) {
5479 err = got_error_from_errno("fclose");
5480 goto done;
5482 f = NULL;
5484 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5485 if (err) {
5486 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5487 err->code != GOT_ERR_HISTEDIT_CMD)
5488 goto done;
5489 err = histedit_edit_list_retry(histedit_cmds, err,
5490 commits, path, repo);
5492 done:
5493 if (f && fclose(f) != 0 && err == NULL)
5494 err = got_error_from_errno("fclose");
5495 if (path && unlink(path) != 0 && err == NULL)
5496 err = got_error_from_errno2("unlink", path);
5497 free(path);
5498 return err;
5501 static const struct got_error *
5502 histedit_save_list(struct got_histedit_list *histedit_cmds,
5503 struct got_worktree *worktree, struct got_repository *repo)
5505 const struct got_error *err = NULL;
5506 char *path = NULL;
5507 FILE *f = NULL;
5508 struct got_histedit_list_entry *hle;
5509 struct got_commit_object *commit = NULL;
5511 err = got_worktree_get_histedit_script_path(&path, worktree);
5512 if (err)
5513 return err;
5515 f = fopen(path, "w");
5516 if (f == NULL) {
5517 err = got_error_from_errno2("fopen", path);
5518 goto done;
5520 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5521 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5522 repo);
5523 if (err)
5524 break;
5526 if (hle->logmsg) {
5527 int n = fprintf(f, "%c %s\n",
5528 GOT_HISTEDIT_MESG, hle->logmsg);
5529 if (n < 0) {
5530 err = got_ferror(f, GOT_ERR_IO);
5531 break;
5535 done:
5536 if (f && fclose(f) != 0 && err == NULL)
5537 err = got_error_from_errno("fclose");
5538 free(path);
5539 if (commit)
5540 got_object_commit_close(commit);
5541 return err;
5544 void
5545 histedit_free_list(struct got_histedit_list *histedit_cmds)
5547 struct got_histedit_list_entry *hle;
5549 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5550 TAILQ_REMOVE(histedit_cmds, hle, entry);
5551 free(hle);
5555 static const struct got_error *
5556 histedit_load_list(struct got_histedit_list *histedit_cmds,
5557 const char *path, struct got_repository *repo)
5559 const struct got_error *err = NULL;
5560 FILE *f = NULL;
5562 f = fopen(path, "r");
5563 if (f == NULL) {
5564 err = got_error_from_errno2("fopen", path);
5565 goto done;
5568 err = histedit_parse_list(histedit_cmds, f, repo);
5569 done:
5570 if (f && fclose(f) != 0 && err == NULL)
5571 err = got_error_from_errno("fclose");
5572 return err;
5575 static const struct got_error *
5576 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5577 const struct got_error *edit_err, struct got_object_id_queue *commits,
5578 const char *path, struct got_repository *repo)
5580 const struct got_error *err = NULL, *prev_err = edit_err;
5581 int resp = ' ';
5583 while (resp != 'c' && resp != 'r' && resp != 'a') {
5584 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5585 "or (a)bort: ", getprogname(), prev_err->msg);
5586 resp = getchar();
5587 if (resp == '\n')
5588 resp = getchar();
5589 if (resp == 'c') {
5590 histedit_free_list(histedit_cmds);
5591 err = histedit_run_editor(histedit_cmds, path, commits,
5592 repo);
5593 if (err) {
5594 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5595 err->code != GOT_ERR_HISTEDIT_CMD)
5596 break;
5597 prev_err = err;
5598 resp = ' ';
5599 continue;
5601 break;
5602 } else if (resp == 'r') {
5603 histedit_free_list(histedit_cmds);
5604 err = histedit_edit_script(histedit_cmds,
5605 commits, repo);
5606 if (err) {
5607 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5608 err->code != GOT_ERR_HISTEDIT_CMD)
5609 break;
5610 prev_err = err;
5611 resp = ' ';
5612 continue;
5614 break;
5615 } else if (resp == 'a') {
5616 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5617 break;
5618 } else
5619 printf("invalid response '%c'\n", resp);
5622 return err;
5625 static const struct got_error *
5626 histedit_complete(struct got_worktree *worktree,
5627 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5628 struct got_reference *branch, struct got_repository *repo)
5630 printf("Switching work tree to %s\n",
5631 got_ref_get_symref_target(branch));
5632 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5633 branch, repo);
5636 static const struct got_error *
5637 show_histedit_progress(struct got_commit_object *commit,
5638 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5640 const struct got_error *err;
5641 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5643 err = got_object_id_str(&old_id_str, hle->commit_id);
5644 if (err)
5645 goto done;
5647 if (new_id) {
5648 err = got_object_id_str(&new_id_str, new_id);
5649 if (err)
5650 goto done;
5653 old_id_str[12] = '\0';
5654 if (new_id_str)
5655 new_id_str[12] = '\0';
5657 if (hle->logmsg) {
5658 logmsg = strdup(hle->logmsg);
5659 if (logmsg == NULL) {
5660 err = got_error_from_errno("strdup");
5661 goto done;
5663 trim_logmsg(logmsg, 42);
5664 } else {
5665 err = get_short_logmsg(&logmsg, 42, commit);
5666 if (err)
5667 goto done;
5670 switch (hle->cmd->code) {
5671 case GOT_HISTEDIT_PICK:
5672 case GOT_HISTEDIT_EDIT:
5673 printf("%s -> %s: %s\n", old_id_str,
5674 new_id_str ? new_id_str : "no-op change", logmsg);
5675 break;
5676 case GOT_HISTEDIT_DROP:
5677 case GOT_HISTEDIT_FOLD:
5678 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5679 logmsg);
5680 break;
5681 default:
5682 break;
5685 done:
5686 free(old_id_str);
5687 free(new_id_str);
5688 return err;
5691 static const struct got_error *
5692 histedit_commit(struct got_pathlist_head *merged_paths,
5693 struct got_worktree *worktree, struct got_fileindex *fileindex,
5694 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5695 struct got_repository *repo)
5697 const struct got_error *err;
5698 struct got_commit_object *commit;
5699 struct got_object_id *new_commit_id;
5701 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5702 && hle->logmsg == NULL) {
5703 err = histedit_edit_logmsg(hle, repo);
5704 if (err)
5705 return err;
5708 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5709 if (err)
5710 return err;
5712 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5713 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5714 hle->logmsg, repo);
5715 if (err) {
5716 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5717 goto done;
5718 err = show_histedit_progress(commit, hle, NULL);
5719 } else {
5720 err = show_histedit_progress(commit, hle, new_commit_id);
5721 free(new_commit_id);
5723 done:
5724 got_object_commit_close(commit);
5725 return err;
5728 static const struct got_error *
5729 histedit_skip_commit(struct got_histedit_list_entry *hle,
5730 struct got_worktree *worktree, struct got_repository *repo)
5732 const struct got_error *error;
5733 struct got_commit_object *commit;
5735 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5736 repo);
5737 if (error)
5738 return error;
5740 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5741 if (error)
5742 return error;
5744 error = show_histedit_progress(commit, hle, NULL);
5745 got_object_commit_close(commit);
5746 return error;
5749 static const struct got_error *
5750 cmd_histedit(int argc, char *argv[])
5752 const struct got_error *error = NULL;
5753 struct got_worktree *worktree = NULL;
5754 struct got_fileindex *fileindex = NULL;
5755 struct got_repository *repo = NULL;
5756 char *cwd = NULL;
5757 struct got_reference *branch = NULL;
5758 struct got_reference *tmp_branch = NULL;
5759 struct got_object_id *resume_commit_id = NULL;
5760 struct got_object_id *base_commit_id = NULL;
5761 struct got_object_id *head_commit_id = NULL;
5762 struct got_commit_object *commit = NULL;
5763 int ch, rebase_in_progress = 0, did_something;
5764 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5765 const char *edit_script_path = NULL;
5766 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5767 struct got_object_id_queue commits;
5768 struct got_pathlist_head merged_paths;
5769 const struct got_object_id_queue *parent_ids;
5770 struct got_object_qid *pid;
5771 struct got_histedit_list histedit_cmds;
5772 struct got_histedit_list_entry *hle;
5774 SIMPLEQ_INIT(&commits);
5775 TAILQ_INIT(&histedit_cmds);
5776 TAILQ_INIT(&merged_paths);
5778 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5779 switch (ch) {
5780 case 'a':
5781 abort_edit = 1;
5782 break;
5783 case 'c':
5784 continue_edit = 1;
5785 break;
5786 case 'F':
5787 edit_script_path = optarg;
5788 break;
5789 default:
5790 usage_histedit();
5791 /* NOTREACHED */
5795 argc -= optind;
5796 argv += optind;
5798 #ifndef PROFILE
5799 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5800 "unveil", NULL) == -1)
5801 err(1, "pledge");
5802 #endif
5803 if (abort_edit && continue_edit)
5804 usage_histedit();
5805 if (argc != 0)
5806 usage_histedit();
5809 * This command cannot apply unveil(2) in all cases because the
5810 * user may choose to run an editor to edit the histedit script
5811 * and to edit individual commit log messages.
5812 * unveil(2) traverses exec(2); if an editor is used we have to
5813 * apply unveil after edit script and log messages have been written.
5814 * XXX TODO: Make use of unveil(2) where possible.
5817 cwd = getcwd(NULL, 0);
5818 if (cwd == NULL) {
5819 error = got_error_from_errno("getcwd");
5820 goto done;
5822 error = got_worktree_open(&worktree, cwd);
5823 if (error)
5824 goto done;
5826 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5827 if (error != NULL)
5828 goto done;
5830 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5831 if (error)
5832 goto done;
5833 if (rebase_in_progress) {
5834 error = got_error(GOT_ERR_REBASING);
5835 goto done;
5838 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5839 if (error)
5840 goto done;
5842 if (edit_in_progress && abort_edit) {
5843 error = got_worktree_histedit_continue(&resume_commit_id,
5844 &tmp_branch, &branch, &base_commit_id, &fileindex,
5845 worktree, repo);
5846 if (error)
5847 goto done;
5848 printf("Switching work tree to %s\n",
5849 got_ref_get_symref_target(branch));
5850 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5851 branch, base_commit_id, update_progress, &did_something);
5852 if (error)
5853 goto done;
5854 printf("Histedit of %s aborted\n",
5855 got_ref_get_symref_target(branch));
5856 goto done; /* nothing else to do */
5857 } else if (abort_edit) {
5858 error = got_error(GOT_ERR_NOT_HISTEDIT);
5859 goto done;
5862 if (continue_edit) {
5863 char *path;
5865 if (!edit_in_progress) {
5866 error = got_error(GOT_ERR_NOT_HISTEDIT);
5867 goto done;
5870 error = got_worktree_get_histedit_script_path(&path, worktree);
5871 if (error)
5872 goto done;
5874 error = histedit_load_list(&histedit_cmds, path, repo);
5875 free(path);
5876 if (error)
5877 goto done;
5879 error = got_worktree_histedit_continue(&resume_commit_id,
5880 &tmp_branch, &branch, &base_commit_id, &fileindex,
5881 worktree, repo);
5882 if (error)
5883 goto done;
5885 error = got_ref_resolve(&head_commit_id, repo, branch);
5886 if (error)
5887 goto done;
5889 error = got_object_open_as_commit(&commit, repo,
5890 head_commit_id);
5891 if (error)
5892 goto done;
5893 parent_ids = got_object_commit_get_parent_ids(commit);
5894 pid = SIMPLEQ_FIRST(parent_ids);
5895 if (pid == NULL) {
5896 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5897 goto done;
5899 error = collect_commits(&commits, head_commit_id, pid->id,
5900 base_commit_id, got_worktree_get_path_prefix(worktree),
5901 GOT_ERR_HISTEDIT_PATH, repo);
5902 got_object_commit_close(commit);
5903 commit = NULL;
5904 if (error)
5905 goto done;
5906 } else {
5907 if (edit_in_progress) {
5908 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5909 goto done;
5912 error = got_ref_open(&branch, repo,
5913 got_worktree_get_head_ref_name(worktree), 0);
5914 if (error != NULL)
5915 goto done;
5917 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5918 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5919 "will not edit commit history of a branch outside "
5920 "the \"refs/heads/\" reference namespace");
5921 goto done;
5924 error = got_ref_resolve(&head_commit_id, repo, branch);
5925 got_ref_close(branch);
5926 branch = NULL;
5927 if (error)
5928 goto done;
5930 error = got_object_open_as_commit(&commit, repo,
5931 head_commit_id);
5932 if (error)
5933 goto done;
5934 parent_ids = got_object_commit_get_parent_ids(commit);
5935 pid = SIMPLEQ_FIRST(parent_ids);
5936 if (pid == NULL) {
5937 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5938 goto done;
5940 error = collect_commits(&commits, head_commit_id, pid->id,
5941 got_worktree_get_base_commit_id(worktree),
5942 got_worktree_get_path_prefix(worktree),
5943 GOT_ERR_HISTEDIT_PATH, repo);
5944 got_object_commit_close(commit);
5945 commit = NULL;
5946 if (error)
5947 goto done;
5949 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5950 &base_commit_id, &fileindex, worktree, repo);
5951 if (error)
5952 goto done;
5954 if (edit_script_path) {
5955 error = histedit_load_list(&histedit_cmds,
5956 edit_script_path, repo);
5957 if (error) {
5958 got_worktree_histedit_abort(worktree, fileindex,
5959 repo, branch, base_commit_id,
5960 update_progress, &did_something);
5961 goto done;
5963 } else {
5964 error = histedit_edit_script(&histedit_cmds, &commits,
5965 repo);
5966 if (error) {
5967 got_worktree_histedit_abort(worktree, fileindex,
5968 repo, branch, base_commit_id,
5969 update_progress, &did_something);
5970 goto done;
5975 error = histedit_save_list(&histedit_cmds, worktree,
5976 repo);
5977 if (error) {
5978 got_worktree_histedit_abort(worktree, fileindex,
5979 repo, branch, base_commit_id,
5980 update_progress, &did_something);
5981 goto done;
5986 error = histedit_check_script(&histedit_cmds, &commits, repo);
5987 if (error)
5988 goto done;
5990 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5991 if (resume_commit_id) {
5992 if (got_object_id_cmp(hle->commit_id,
5993 resume_commit_id) != 0)
5994 continue;
5996 resume_commit_id = NULL;
5997 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5998 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5999 error = histedit_skip_commit(hle, worktree,
6000 repo);
6001 } else {
6002 error = histedit_commit(NULL, worktree,
6003 fileindex, tmp_branch, hle, repo);
6005 if (error)
6006 goto done;
6007 continue;
6010 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6011 error = histedit_skip_commit(hle, worktree, repo);
6012 if (error)
6013 goto done;
6014 continue;
6017 error = got_object_open_as_commit(&commit, repo,
6018 hle->commit_id);
6019 if (error)
6020 goto done;
6021 parent_ids = got_object_commit_get_parent_ids(commit);
6022 pid = SIMPLEQ_FIRST(parent_ids);
6024 error = got_worktree_histedit_merge_files(&merged_paths,
6025 worktree, fileindex, pid->id, hle->commit_id, repo,
6026 rebase_progress, &rebase_status, check_cancelled, NULL);
6027 if (error)
6028 goto done;
6029 got_object_commit_close(commit);
6030 commit = NULL;
6032 if (rebase_status == GOT_STATUS_CONFLICT) {
6033 got_worktree_rebase_pathlist_free(&merged_paths);
6034 break;
6037 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6038 char *id_str;
6039 error = got_object_id_str(&id_str, hle->commit_id);
6040 if (error)
6041 goto done;
6042 printf("Stopping histedit for amending commit %s\n",
6043 id_str);
6044 free(id_str);
6045 got_worktree_rebase_pathlist_free(&merged_paths);
6046 error = got_worktree_histedit_postpone(worktree,
6047 fileindex);
6048 goto done;
6051 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6052 error = histedit_skip_commit(hle, worktree, repo);
6053 if (error)
6054 goto done;
6055 continue;
6058 error = histedit_commit(&merged_paths, worktree, fileindex,
6059 tmp_branch, hle, repo);
6060 got_worktree_rebase_pathlist_free(&merged_paths);
6061 if (error)
6062 goto done;
6065 if (rebase_status == GOT_STATUS_CONFLICT) {
6066 error = got_worktree_histedit_postpone(worktree, fileindex);
6067 if (error)
6068 goto done;
6069 error = got_error_msg(GOT_ERR_CONFLICTS,
6070 "conflicts must be resolved before rebasing can continue");
6071 } else
6072 error = histedit_complete(worktree, fileindex, tmp_branch,
6073 branch, repo);
6074 done:
6075 got_object_id_queue_free(&commits);
6076 histedit_free_list(&histedit_cmds);
6077 free(head_commit_id);
6078 free(base_commit_id);
6079 free(resume_commit_id);
6080 if (commit)
6081 got_object_commit_close(commit);
6082 if (branch)
6083 got_ref_close(branch);
6084 if (tmp_branch)
6085 got_ref_close(tmp_branch);
6086 if (worktree)
6087 got_worktree_close(worktree);
6088 if (repo)
6089 got_repo_close(repo);
6090 return error;
6093 __dead static void
6094 usage_stage(void)
6096 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6097 "[file-path ...]\n",
6098 getprogname());
6099 exit(1);
6102 static const struct got_error *
6103 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6104 const char *path, struct got_object_id *blob_id,
6105 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6107 const struct got_error *err = NULL;
6108 char *id_str = NULL;
6110 if (staged_status != GOT_STATUS_ADD &&
6111 staged_status != GOT_STATUS_MODIFY &&
6112 staged_status != GOT_STATUS_DELETE)
6113 return NULL;
6115 if (staged_status == GOT_STATUS_ADD ||
6116 staged_status == GOT_STATUS_MODIFY)
6117 err = got_object_id_str(&id_str, staged_blob_id);
6118 else
6119 err = got_object_id_str(&id_str, blob_id);
6120 if (err)
6121 return err;
6123 printf("%s %c %s\n", id_str, staged_status, path);
6124 free(id_str);
6125 return NULL;
6128 static const struct got_error *
6129 cmd_stage(int argc, char *argv[])
6131 const struct got_error *error = NULL;
6132 struct got_repository *repo = NULL;
6133 struct got_worktree *worktree = NULL;
6134 char *cwd = NULL;
6135 struct got_pathlist_head paths;
6136 struct got_pathlist_entry *pe;
6137 int ch, list_stage = 0, pflag = 0;
6138 FILE *patch_script_file = NULL;
6139 const char *patch_script_path = NULL;
6140 struct choose_patch_arg cpa;
6142 TAILQ_INIT(&paths);
6144 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6145 switch (ch) {
6146 case 'l':
6147 list_stage = 1;
6148 break;
6149 case 'p':
6150 pflag = 1;
6151 break;
6152 case 'F':
6153 patch_script_path = optarg;
6154 break;
6155 default:
6156 usage_stage();
6157 /* NOTREACHED */
6161 argc -= optind;
6162 argv += optind;
6164 #ifndef PROFILE
6165 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6166 "unveil", NULL) == -1)
6167 err(1, "pledge");
6168 #endif
6169 if (list_stage && (pflag || patch_script_path))
6170 errx(1, "-l option cannot be used with other options");
6171 if (patch_script_path && !pflag)
6172 errx(1, "-F option can only be used together with -p option");
6174 cwd = getcwd(NULL, 0);
6175 if (cwd == NULL) {
6176 error = got_error_from_errno("getcwd");
6177 goto done;
6180 error = got_worktree_open(&worktree, cwd);
6181 if (error)
6182 goto done;
6184 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6185 if (error != NULL)
6186 goto done;
6188 if (patch_script_path) {
6189 patch_script_file = fopen(patch_script_path, "r");
6190 if (patch_script_file == NULL) {
6191 error = got_error_from_errno2("fopen",
6192 patch_script_path);
6193 goto done;
6196 error = apply_unveil(got_repo_get_path(repo), 1,
6197 got_worktree_get_root_path(worktree));
6198 if (error)
6199 goto done;
6201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6202 if (error)
6203 goto done;
6205 if (list_stage)
6206 error = got_worktree_status(worktree, &paths, repo,
6207 print_stage, NULL, check_cancelled, NULL);
6208 else {
6209 cpa.patch_script_file = patch_script_file;
6210 cpa.action = "stage";
6211 error = got_worktree_stage(worktree, &paths,
6212 pflag ? NULL : print_status, NULL,
6213 pflag ? choose_patch : NULL, &cpa, repo);
6215 done:
6216 if (patch_script_file && fclose(patch_script_file) == EOF &&
6217 error == NULL)
6218 error = got_error_from_errno2("fclose", patch_script_path);
6219 if (repo)
6220 got_repo_close(repo);
6221 if (worktree)
6222 got_worktree_close(worktree);
6223 TAILQ_FOREACH(pe, &paths, entry)
6224 free((char *)pe->path);
6225 got_pathlist_free(&paths);
6226 free(cwd);
6227 return error;
6230 __dead static void
6231 usage_unstage(void)
6233 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6234 "[file-path ...]\n",
6235 getprogname());
6236 exit(1);
6240 static const struct got_error *
6241 cmd_unstage(int argc, char *argv[])
6243 const struct got_error *error = NULL;
6244 struct got_repository *repo = NULL;
6245 struct got_worktree *worktree = NULL;
6246 char *cwd = NULL;
6247 struct got_pathlist_head paths;
6248 struct got_pathlist_entry *pe;
6249 int ch, did_something = 0, pflag = 0;
6250 FILE *patch_script_file = NULL;
6251 const char *patch_script_path = NULL;
6252 struct choose_patch_arg cpa;
6254 TAILQ_INIT(&paths);
6256 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6257 switch (ch) {
6258 case 'p':
6259 pflag = 1;
6260 break;
6261 case 'F':
6262 patch_script_path = optarg;
6263 break;
6264 default:
6265 usage_unstage();
6266 /* NOTREACHED */
6270 argc -= optind;
6271 argv += optind;
6273 #ifndef PROFILE
6274 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6275 "unveil", NULL) == -1)
6276 err(1, "pledge");
6277 #endif
6278 if (patch_script_path && !pflag)
6279 errx(1, "-F option can only be used together with -p option");
6281 cwd = getcwd(NULL, 0);
6282 if (cwd == NULL) {
6283 error = got_error_from_errno("getcwd");
6284 goto done;
6287 error = got_worktree_open(&worktree, cwd);
6288 if (error)
6289 goto done;
6291 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6292 if (error != NULL)
6293 goto done;
6295 if (patch_script_path) {
6296 patch_script_file = fopen(patch_script_path, "r");
6297 if (patch_script_file == NULL) {
6298 error = got_error_from_errno2("fopen",
6299 patch_script_path);
6300 goto done;
6304 error = apply_unveil(got_repo_get_path(repo), 1,
6305 got_worktree_get_root_path(worktree));
6306 if (error)
6307 goto done;
6309 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6310 if (error)
6311 goto done;
6313 cpa.patch_script_file = patch_script_file;
6314 cpa.action = "unstage";
6315 error = got_worktree_unstage(worktree, &paths, update_progress,
6316 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6317 done:
6318 if (patch_script_file && fclose(patch_script_file) == EOF &&
6319 error == NULL)
6320 error = got_error_from_errno2("fclose", patch_script_path);
6321 if (repo)
6322 got_repo_close(repo);
6323 if (worktree)
6324 got_worktree_close(worktree);
6325 TAILQ_FOREACH(pe, &paths, entry)
6326 free((char *)pe->path);
6327 got_pathlist_free(&paths);
6328 free(cwd);
6329 return error;
6332 __dead static void
6333 usage_cat(void)
6335 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6336 "[object2 ...]\n", getprogname());
6337 exit(1);
6340 static const struct got_error *
6341 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6343 const struct got_error *err;
6344 struct got_blob_object *blob;
6346 err = got_object_open_as_blob(&blob, repo, id, 8192);
6347 if (err)
6348 return err;
6350 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6351 got_object_blob_close(blob);
6352 return err;
6355 static const struct got_error *
6356 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6358 const struct got_error *err;
6359 struct got_tree_object *tree;
6360 const struct got_tree_entries *entries;
6361 struct got_tree_entry *te;
6363 err = got_object_open_as_tree(&tree, repo, id);
6364 if (err)
6365 return err;
6367 entries = got_object_tree_get_entries(tree);
6368 te = SIMPLEQ_FIRST(&entries->head);
6369 while (te) {
6370 char *id_str;
6371 if (sigint_received || sigpipe_received)
6372 break;
6373 err = got_object_id_str(&id_str, te->id);
6374 if (err)
6375 break;
6376 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6377 free(id_str);
6378 te = SIMPLEQ_NEXT(te, entry);
6381 got_object_tree_close(tree);
6382 return err;
6385 static const struct got_error *
6386 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6388 const struct got_error *err;
6389 struct got_commit_object *commit;
6390 const struct got_object_id_queue *parent_ids;
6391 struct got_object_qid *pid;
6392 char *id_str = NULL;
6393 const char *logmsg = NULL;
6395 err = got_object_open_as_commit(&commit, repo, id);
6396 if (err)
6397 return err;
6399 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6400 if (err)
6401 goto done;
6403 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6404 parent_ids = got_object_commit_get_parent_ids(commit);
6405 fprintf(outfile, "numparents %d\n",
6406 got_object_commit_get_nparents(commit));
6407 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6408 char *pid_str;
6409 err = got_object_id_str(&pid_str, pid->id);
6410 if (err)
6411 goto done;
6412 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6413 free(pid_str);
6415 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6416 got_object_commit_get_author(commit),
6417 got_object_commit_get_author_time(commit));
6419 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6420 got_object_commit_get_author(commit),
6421 got_object_commit_get_committer_time(commit));
6423 logmsg = got_object_commit_get_logmsg_raw(commit);
6424 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6425 fprintf(outfile, "%s", logmsg);
6426 done:
6427 free(id_str);
6428 got_object_commit_close(commit);
6429 return err;
6432 static const struct got_error *
6433 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6435 const struct got_error *err;
6436 struct got_tag_object *tag;
6437 char *id_str = NULL;
6438 const char *tagmsg = NULL;
6440 err = got_object_open_as_tag(&tag, repo, id);
6441 if (err)
6442 return err;
6444 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6445 if (err)
6446 goto done;
6448 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6450 switch (got_object_tag_get_object_type(tag)) {
6451 case GOT_OBJ_TYPE_BLOB:
6452 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6453 GOT_OBJ_LABEL_BLOB);
6454 break;
6455 case GOT_OBJ_TYPE_TREE:
6456 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6457 GOT_OBJ_LABEL_TREE);
6458 break;
6459 case GOT_OBJ_TYPE_COMMIT:
6460 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6461 GOT_OBJ_LABEL_COMMIT);
6462 break;
6463 case GOT_OBJ_TYPE_TAG:
6464 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6465 GOT_OBJ_LABEL_TAG);
6466 break;
6467 default:
6468 break;
6471 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6472 got_object_tag_get_name(tag));
6474 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6475 got_object_tag_get_tagger(tag),
6476 got_object_tag_get_tagger_time(tag));
6478 tagmsg = got_object_tag_get_message(tag);
6479 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6480 fprintf(outfile, "%s", tagmsg);
6481 done:
6482 free(id_str);
6483 got_object_tag_close(tag);
6484 return err;
6487 static const struct got_error *
6488 cmd_cat(int argc, char *argv[])
6490 const struct got_error *error;
6491 struct got_repository *repo = NULL;
6492 struct got_worktree *worktree = NULL;
6493 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6494 struct got_object_id *id = NULL;
6495 int ch, obj_type, i;
6497 #ifndef PROFILE
6498 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6499 NULL) == -1)
6500 err(1, "pledge");
6501 #endif
6503 while ((ch = getopt(argc, argv, "r:")) != -1) {
6504 switch (ch) {
6505 case 'r':
6506 repo_path = realpath(optarg, NULL);
6507 if (repo_path == NULL)
6508 err(1, "-r option");
6509 got_path_strip_trailing_slashes(repo_path);
6510 break;
6511 default:
6512 usage_cat();
6513 /* NOTREACHED */
6517 argc -= optind;
6518 argv += optind;
6520 cwd = getcwd(NULL, 0);
6521 if (cwd == NULL) {
6522 error = got_error_from_errno("getcwd");
6523 goto done;
6525 error = got_worktree_open(&worktree, cwd);
6526 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6527 goto done;
6528 if (worktree) {
6529 if (repo_path == NULL) {
6530 repo_path = strdup(
6531 got_worktree_get_repo_path(worktree));
6532 if (repo_path == NULL) {
6533 error = got_error_from_errno("strdup");
6534 goto done;
6539 if (repo_path == NULL) {
6540 repo_path = getcwd(NULL, 0);
6541 if (repo_path == NULL)
6542 return got_error_from_errno("getcwd");
6545 error = got_repo_open(&repo, repo_path);
6546 free(repo_path);
6547 if (error != NULL)
6548 goto done;
6550 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6551 if (error)
6552 goto done;
6554 for (i = 0; i < argc; i++) {
6555 error = match_object_id(&id, &label, argv[i],
6556 GOT_OBJ_TYPE_ANY, 0, repo);
6557 if (error)
6558 break;
6560 error = got_object_get_type(&obj_type, repo, id);
6561 if (error)
6562 break;
6564 switch (obj_type) {
6565 case GOT_OBJ_TYPE_BLOB:
6566 error = cat_blob(id, repo, stdout);
6567 break;
6568 case GOT_OBJ_TYPE_TREE:
6569 error = cat_tree(id, repo, stdout);
6570 break;
6571 case GOT_OBJ_TYPE_COMMIT:
6572 error = cat_commit(id, repo, stdout);
6573 break;
6574 case GOT_OBJ_TYPE_TAG:
6575 error = cat_tag(id, repo, stdout);
6576 break;
6577 default:
6578 error = got_error(GOT_ERR_OBJ_TYPE);
6579 break;
6581 if (error)
6582 break;
6583 free(label);
6584 label = NULL;
6585 free(id);
6586 id = NULL;
6589 done:
6590 free(label);
6591 free(id);
6592 if (worktree)
6593 got_worktree_close(worktree);
6594 if (repo) {
6595 const struct got_error *repo_error;
6596 repo_error = got_repo_close(repo);
6597 if (error == NULL)
6598 error = repo_error;
6600 return error;