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, got_ref_cmp_by_name, 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_NONEXISTENT)
1827 return got_error_set_errno(ENOENT, path);
1828 if (status != GOT_STATUS_MODIFY &&
1829 status != GOT_STATUS_ADD &&
1830 status != GOT_STATUS_DELETE &&
1831 status != GOT_STATUS_CONFLICT)
1832 return NULL;
1835 if (!a->header_shown) {
1836 printf("diff %s %s%s\n", a->id_str,
1837 got_worktree_get_root_path(a->worktree),
1838 a->diff_staged ? " (staged changes)" : "");
1839 a->header_shown = 1;
1842 if (a->diff_staged) {
1843 const char *label1 = NULL, *label2 = NULL;
1844 switch (staged_status) {
1845 case GOT_STATUS_MODIFY:
1846 label1 = path;
1847 label2 = path;
1848 break;
1849 case GOT_STATUS_ADD:
1850 label2 = path;
1851 break;
1852 case GOT_STATUS_DELETE:
1853 label1 = path;
1854 break;
1855 default:
1856 return got_error(GOT_ERR_FILE_STATUS);
1858 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1859 label1, label2, a->diff_context, a->repo, stdout);
1862 if (staged_status == GOT_STATUS_ADD ||
1863 staged_status == GOT_STATUS_MODIFY) {
1864 char *id_str;
1865 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1866 8192);
1867 if (err)
1868 goto done;
1869 err = got_object_id_str(&id_str, staged_blob_id);
1870 if (err)
1871 goto done;
1872 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1873 err = got_error_from_errno("asprintf");
1874 free(id_str);
1875 goto done;
1877 free(id_str);
1878 } else if (status != GOT_STATUS_ADD) {
1879 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1880 if (err)
1881 goto done;
1884 if (status != GOT_STATUS_DELETE) {
1885 if (asprintf(&abspath, "%s/%s",
1886 got_worktree_get_root_path(a->worktree), path) == -1) {
1887 err = got_error_from_errno("asprintf");
1888 goto done;
1891 f2 = fopen(abspath, "r");
1892 if (f2 == NULL) {
1893 err = got_error_from_errno2("fopen", abspath);
1894 goto done;
1896 if (lstat(abspath, &sb) == -1) {
1897 err = got_error_from_errno2("lstat", abspath);
1898 goto done;
1900 } else
1901 sb.st_size = 0;
1903 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1904 a->diff_context, stdout);
1905 done:
1906 if (blob1)
1907 got_object_blob_close(blob1);
1908 if (f2 && fclose(f2) != 0 && err == NULL)
1909 err = got_error_from_errno("fclose");
1910 free(abspath);
1911 return err;
1914 static const struct got_error *
1915 match_object_id(struct got_object_id **id, char **label,
1916 const char *id_str, int obj_type, int resolve_tags,
1917 struct got_repository *repo)
1919 const struct got_error *err;
1920 struct got_tag_object *tag;
1921 struct got_reference *ref = NULL;
1923 *id = NULL;
1924 *label = NULL;
1926 if (resolve_tags) {
1927 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1928 repo);
1929 if (err == NULL) {
1930 *id = got_object_id_dup(
1931 got_object_tag_get_object_id(tag));
1932 if (*id == NULL)
1933 err = got_error_from_errno("got_object_id_dup");
1934 else if (asprintf(label, "refs/tags/%s",
1935 got_object_tag_get_name(tag)) == -1) {
1936 err = got_error_from_errno("asprintf");
1937 free(id);
1938 *id = NULL;
1940 got_object_tag_close(tag);
1941 return err;
1942 } else if (err->code != GOT_ERR_NO_OBJ)
1943 return err;
1946 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1947 if (err) {
1948 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1949 return err;
1950 err = got_ref_open(&ref, repo, id_str, 0);
1951 if (err != NULL)
1952 goto done;
1953 *label = strdup(got_ref_get_name(ref));
1954 if (*label == NULL) {
1955 err = got_error_from_errno("strdup");
1956 goto done;
1958 err = got_ref_resolve(id, repo, ref);
1959 } else {
1960 err = got_object_id_str(label, *id);
1961 if (*label == NULL) {
1962 err = got_error_from_errno("strdup");
1963 goto done;
1966 done:
1967 if (ref)
1968 got_ref_close(ref);
1969 return err;
1973 static const struct got_error *
1974 cmd_diff(int argc, char *argv[])
1976 const struct got_error *error;
1977 struct got_repository *repo = NULL;
1978 struct got_worktree *worktree = NULL;
1979 char *cwd = NULL, *repo_path = NULL;
1980 struct got_object_id *id1 = NULL, *id2 = NULL;
1981 const char *id_str1 = NULL, *id_str2 = NULL;
1982 char *label1 = NULL, *label2 = NULL;
1983 int type1, type2;
1984 int diff_context = 3, diff_staged = 0, ch;
1985 const char *errstr;
1986 char *path = NULL;
1988 #ifndef PROFILE
1989 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1990 NULL) == -1)
1991 err(1, "pledge");
1992 #endif
1994 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1995 switch (ch) {
1996 case 'C':
1997 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1998 if (errstr != NULL)
1999 err(1, "-C option %s", errstr);
2000 break;
2001 case 'r':
2002 repo_path = realpath(optarg, NULL);
2003 if (repo_path == NULL)
2004 err(1, "-r option");
2005 got_path_strip_trailing_slashes(repo_path);
2006 break;
2007 case 's':
2008 diff_staged = 1;
2009 break;
2010 default:
2011 usage_diff();
2012 /* NOTREACHED */
2016 argc -= optind;
2017 argv += optind;
2019 cwd = getcwd(NULL, 0);
2020 if (cwd == NULL) {
2021 error = got_error_from_errno("getcwd");
2022 goto done;
2024 error = got_worktree_open(&worktree, cwd);
2025 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2026 goto done;
2027 if (argc <= 1) {
2028 if (worktree == NULL) {
2029 error = got_error(GOT_ERR_NOT_WORKTREE);
2030 goto done;
2032 if (repo_path)
2033 errx(1,
2034 "-r option can't be used when diffing a work tree");
2035 repo_path = strdup(got_worktree_get_repo_path(worktree));
2036 if (repo_path == NULL) {
2037 error = got_error_from_errno("strdup");
2038 goto done;
2040 if (argc == 1) {
2041 error = got_worktree_resolve_path(&path, worktree,
2042 argv[0]);
2043 if (error)
2044 goto done;
2045 } else {
2046 path = strdup("");
2047 if (path == NULL) {
2048 error = got_error_from_errno("strdup");
2049 goto done;
2052 } else if (argc == 2) {
2053 if (diff_staged)
2054 errx(1, "-s option can't be used when diffing "
2055 "objects in repository");
2056 id_str1 = argv[0];
2057 id_str2 = argv[1];
2058 if (worktree && repo_path == NULL) {
2059 repo_path =
2060 strdup(got_worktree_get_repo_path(worktree));
2061 if (repo_path == NULL) {
2062 error = got_error_from_errno("strdup");
2063 goto done;
2066 } else
2067 usage_diff();
2069 if (repo_path == NULL) {
2070 repo_path = getcwd(NULL, 0);
2071 if (repo_path == NULL)
2072 return got_error_from_errno("getcwd");
2075 error = got_repo_open(&repo, repo_path);
2076 free(repo_path);
2077 if (error != NULL)
2078 goto done;
2080 error = apply_unveil(got_repo_get_path(repo), 1,
2081 worktree ? got_worktree_get_root_path(worktree) : NULL);
2082 if (error)
2083 goto done;
2085 if (argc <= 1) {
2086 struct print_diff_arg arg;
2087 struct got_pathlist_head paths;
2088 char *id_str;
2090 TAILQ_INIT(&paths);
2092 error = got_object_id_str(&id_str,
2093 got_worktree_get_base_commit_id(worktree));
2094 if (error)
2095 goto done;
2096 arg.repo = repo;
2097 arg.worktree = worktree;
2098 arg.diff_context = diff_context;
2099 arg.id_str = id_str;
2100 arg.header_shown = 0;
2101 arg.diff_staged = diff_staged;
2103 error = got_pathlist_append(&paths, path, NULL);
2104 if (error)
2105 goto done;
2107 error = got_worktree_status(worktree, &paths, repo, print_diff,
2108 &arg, check_cancelled, NULL);
2109 free(id_str);
2110 got_pathlist_free(&paths);
2111 goto done;
2114 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2115 repo);
2116 if (error)
2117 goto done;
2119 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2120 repo);
2121 if (error)
2122 goto done;
2124 error = got_object_get_type(&type1, repo, id1);
2125 if (error)
2126 goto done;
2128 error = got_object_get_type(&type2, repo, id2);
2129 if (error)
2130 goto done;
2132 if (type1 != type2) {
2133 error = got_error(GOT_ERR_OBJ_TYPE);
2134 goto done;
2137 switch (type1) {
2138 case GOT_OBJ_TYPE_BLOB:
2139 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2140 diff_context, repo, stdout);
2141 break;
2142 case GOT_OBJ_TYPE_TREE:
2143 error = got_diff_objects_as_trees(id1, id2, "", "",
2144 diff_context, repo, stdout);
2145 break;
2146 case GOT_OBJ_TYPE_COMMIT:
2147 printf("diff %s %s\n", label1, label2);
2148 error = got_diff_objects_as_commits(id1, id2, diff_context,
2149 repo, stdout);
2150 break;
2151 default:
2152 error = got_error(GOT_ERR_OBJ_TYPE);
2155 done:
2156 free(label1);
2157 free(label2);
2158 free(id1);
2159 free(id2);
2160 free(path);
2161 if (worktree)
2162 got_worktree_close(worktree);
2163 if (repo) {
2164 const struct got_error *repo_error;
2165 repo_error = got_repo_close(repo);
2166 if (error == NULL)
2167 error = repo_error;
2169 return error;
2172 __dead static void
2173 usage_blame(void)
2175 fprintf(stderr,
2176 "usage: %s blame [-c commit] [-r repository-path] path\n",
2177 getprogname());
2178 exit(1);
2181 struct blame_line {
2182 int annotated;
2183 char *id_str;
2184 char *committer;
2185 char datebuf[9]; /* YY-MM-DD + NUL */
2188 struct blame_cb_args {
2189 struct blame_line *lines;
2190 int nlines;
2191 int nlines_prec;
2192 int lineno_cur;
2193 off_t *line_offsets;
2194 FILE *f;
2195 struct got_repository *repo;
2198 static const struct got_error *
2199 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2201 const struct got_error *err = NULL;
2202 struct blame_cb_args *a = arg;
2203 struct blame_line *bline;
2204 char *line = NULL;
2205 size_t linesize = 0;
2206 struct got_commit_object *commit = NULL;
2207 off_t offset;
2208 struct tm tm;
2209 time_t committer_time;
2211 if (nlines != a->nlines ||
2212 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2213 return got_error(GOT_ERR_RANGE);
2215 if (sigint_received)
2216 return got_error(GOT_ERR_ITER_COMPLETED);
2218 if (lineno == -1)
2219 return NULL; /* no change in this commit */
2221 /* Annotate this line. */
2222 bline = &a->lines[lineno - 1];
2223 if (bline->annotated)
2224 return NULL;
2225 err = got_object_id_str(&bline->id_str, id);
2226 if (err)
2227 return err;
2229 err = got_object_open_as_commit(&commit, a->repo, id);
2230 if (err)
2231 goto done;
2233 bline->committer = strdup(got_object_commit_get_committer(commit));
2234 if (bline->committer == NULL) {
2235 err = got_error_from_errno("strdup");
2236 goto done;
2239 committer_time = got_object_commit_get_committer_time(commit);
2240 if (localtime_r(&committer_time, &tm) == NULL)
2241 return got_error_from_errno("localtime_r");
2242 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2243 &tm) >= sizeof(bline->datebuf)) {
2244 err = got_error(GOT_ERR_NO_SPACE);
2245 goto done;
2247 bline->annotated = 1;
2249 /* Print lines annotated so far. */
2250 bline = &a->lines[a->lineno_cur - 1];
2251 if (!bline->annotated)
2252 goto done;
2254 offset = a->line_offsets[a->lineno_cur - 1];
2255 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2256 err = got_error_from_errno("fseeko");
2257 goto done;
2260 while (bline->annotated) {
2261 char *smallerthan, *at, *nl, *committer;
2262 size_t len;
2264 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2265 if (ferror(a->f))
2266 err = got_error_from_errno("getline");
2267 break;
2270 committer = bline->committer;
2271 smallerthan = strchr(committer, '<');
2272 if (smallerthan && smallerthan[1] != '\0')
2273 committer = smallerthan + 1;
2274 at = strchr(committer, '@');
2275 if (at)
2276 *at = '\0';
2277 len = strlen(committer);
2278 if (len >= 9)
2279 committer[8] = '\0';
2281 nl = strchr(line, '\n');
2282 if (nl)
2283 *nl = '\0';
2284 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2285 bline->id_str, bline->datebuf, committer, line);
2287 a->lineno_cur++;
2288 bline = &a->lines[a->lineno_cur - 1];
2290 done:
2291 if (commit)
2292 got_object_commit_close(commit);
2293 free(line);
2294 return err;
2297 static const struct got_error *
2298 cmd_blame(int argc, char *argv[])
2300 const struct got_error *error;
2301 struct got_repository *repo = NULL;
2302 struct got_worktree *worktree = NULL;
2303 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2304 struct got_object_id *obj_id = NULL;
2305 struct got_object_id *commit_id = NULL;
2306 struct got_blob_object *blob = NULL;
2307 char *commit_id_str = NULL;
2308 struct blame_cb_args bca;
2309 int ch, obj_type, i;
2310 size_t filesize;
2312 memset(&bca, 0, sizeof(bca));
2314 #ifndef PROFILE
2315 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2316 NULL) == -1)
2317 err(1, "pledge");
2318 #endif
2320 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2321 switch (ch) {
2322 case 'c':
2323 commit_id_str = optarg;
2324 break;
2325 case 'r':
2326 repo_path = realpath(optarg, NULL);
2327 if (repo_path == NULL)
2328 err(1, "-r option");
2329 got_path_strip_trailing_slashes(repo_path);
2330 break;
2331 default:
2332 usage_blame();
2333 /* NOTREACHED */
2337 argc -= optind;
2338 argv += optind;
2340 if (argc == 1)
2341 path = argv[0];
2342 else
2343 usage_blame();
2345 cwd = getcwd(NULL, 0);
2346 if (cwd == NULL) {
2347 error = got_error_from_errno("getcwd");
2348 goto done;
2350 if (repo_path == NULL) {
2351 error = got_worktree_open(&worktree, cwd);
2352 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2353 goto done;
2354 else
2355 error = NULL;
2356 if (worktree) {
2357 repo_path =
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL)
2360 error = got_error_from_errno("strdup");
2361 if (error)
2362 goto done;
2363 } else {
2364 repo_path = strdup(cwd);
2365 if (repo_path == NULL) {
2366 error = got_error_from_errno("strdup");
2367 goto done;
2372 error = got_repo_open(&repo, repo_path);
2373 if (error != NULL)
2374 goto done;
2376 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2377 if (error)
2378 goto done;
2380 if (worktree) {
2381 const char *prefix = got_worktree_get_path_prefix(worktree);
2382 char *p, *worktree_subdir = cwd +
2383 strlen(got_worktree_get_root_path(worktree));
2384 if (asprintf(&p, "%s%s%s%s%s",
2385 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2386 worktree_subdir, worktree_subdir[0] ? "/" : "",
2387 path) == -1) {
2388 error = got_error_from_errno("asprintf");
2389 goto done;
2391 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2392 free(p);
2393 } else {
2394 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2396 if (error)
2397 goto done;
2399 if (commit_id_str == NULL) {
2400 struct got_reference *head_ref;
2401 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2402 if (error != NULL)
2403 goto done;
2404 error = got_ref_resolve(&commit_id, repo, head_ref);
2405 got_ref_close(head_ref);
2406 if (error != NULL)
2407 goto done;
2408 } else {
2409 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2410 if (error)
2411 goto done;
2414 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2415 if (error)
2416 goto done;
2417 if (obj_id == NULL) {
2418 error = got_error(GOT_ERR_NO_OBJ);
2419 goto done;
2422 error = got_object_get_type(&obj_type, repo, obj_id);
2423 if (error)
2424 goto done;
2426 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2427 error = got_error(GOT_ERR_OBJ_TYPE);
2428 goto done;
2431 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2432 if (error)
2433 goto done;
2434 bca.f = got_opentemp();
2435 if (bca.f == NULL) {
2436 error = got_error_from_errno("got_opentemp");
2437 goto done;
2439 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2440 &bca.line_offsets, bca.f, blob);
2441 if (error || bca.nlines == 0)
2442 goto done;
2444 /* Don't include \n at EOF in the blame line count. */
2445 if (bca.line_offsets[bca.nlines - 1] == filesize)
2446 bca.nlines--;
2448 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2449 if (bca.lines == NULL) {
2450 error = got_error_from_errno("calloc");
2451 goto done;
2453 bca.lineno_cur = 1;
2454 bca.nlines_prec = 0;
2455 i = bca.nlines;
2456 while (i > 0) {
2457 i /= 10;
2458 bca.nlines_prec++;
2460 bca.repo = repo;
2462 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2463 check_cancelled, NULL);
2464 if (error)
2465 goto done;
2466 done:
2467 free(in_repo_path);
2468 free(repo_path);
2469 free(cwd);
2470 free(commit_id);
2471 free(obj_id);
2472 if (blob)
2473 got_object_blob_close(blob);
2474 if (worktree)
2475 got_worktree_close(worktree);
2476 if (repo) {
2477 const struct got_error *repo_error;
2478 repo_error = got_repo_close(repo);
2479 if (error == NULL)
2480 error = repo_error;
2482 for (i = 0; i < bca.nlines; i++) {
2483 struct blame_line *bline = &bca.lines[i];
2484 free(bline->id_str);
2485 free(bline->committer);
2487 free(bca.lines);
2488 free(bca.line_offsets);
2489 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2490 error = got_error_from_errno("fclose");
2491 return error;
2494 __dead static void
2495 usage_tree(void)
2497 fprintf(stderr,
2498 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2499 getprogname());
2500 exit(1);
2503 static void
2504 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2505 const char *root_path)
2507 int is_root_path = (strcmp(path, root_path) == 0);
2508 const char *modestr = "";
2510 path += strlen(root_path);
2511 while (path[0] == '/')
2512 path++;
2514 if (got_object_tree_entry_is_submodule(te))
2515 modestr = "$";
2516 else if (S_ISLNK(te->mode))
2517 modestr = "@";
2518 else if (S_ISDIR(te->mode))
2519 modestr = "/";
2520 else if (te->mode & S_IXUSR)
2521 modestr = "*";
2523 printf("%s%s%s%s%s\n", id ? id : "", path,
2524 is_root_path ? "" : "/", te->name, modestr);
2527 static const struct got_error *
2528 print_tree(const char *path, struct got_object_id *commit_id,
2529 int show_ids, int recurse, const char *root_path,
2530 struct got_repository *repo)
2532 const struct got_error *err = NULL;
2533 struct got_object_id *tree_id = NULL;
2534 struct got_tree_object *tree = NULL;
2535 const struct got_tree_entries *entries;
2536 struct got_tree_entry *te;
2538 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2539 if (err)
2540 goto done;
2542 err = got_object_open_as_tree(&tree, repo, tree_id);
2543 if (err)
2544 goto done;
2545 entries = got_object_tree_get_entries(tree);
2546 te = SIMPLEQ_FIRST(&entries->head);
2547 while (te) {
2548 char *id = NULL;
2550 if (sigint_received || sigpipe_received)
2551 break;
2553 if (show_ids) {
2554 char *id_str;
2555 err = got_object_id_str(&id_str, te->id);
2556 if (err)
2557 goto done;
2558 if (asprintf(&id, "%s ", id_str) == -1) {
2559 err = got_error_from_errno("asprintf");
2560 free(id_str);
2561 goto done;
2563 free(id_str);
2565 print_entry(te, id, path, root_path);
2566 free(id);
2568 if (recurse && S_ISDIR(te->mode)) {
2569 char *child_path;
2570 if (asprintf(&child_path, "%s%s%s", path,
2571 path[0] == '/' && path[1] == '\0' ? "" : "/",
2572 te->name) == -1) {
2573 err = got_error_from_errno("asprintf");
2574 goto done;
2576 err = print_tree(child_path, commit_id, show_ids, 1,
2577 root_path, repo);
2578 free(child_path);
2579 if (err)
2580 goto done;
2583 te = SIMPLEQ_NEXT(te, entry);
2585 done:
2586 if (tree)
2587 got_object_tree_close(tree);
2588 free(tree_id);
2589 return err;
2592 static const struct got_error *
2593 cmd_tree(int argc, char *argv[])
2595 const struct got_error *error;
2596 struct got_repository *repo = NULL;
2597 struct got_worktree *worktree = NULL;
2598 const char *path;
2599 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2600 struct got_object_id *commit_id = NULL;
2601 char *commit_id_str = NULL;
2602 int show_ids = 0, recurse = 0;
2603 int ch;
2605 #ifndef PROFILE
2606 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2607 NULL) == -1)
2608 err(1, "pledge");
2609 #endif
2611 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2612 switch (ch) {
2613 case 'c':
2614 commit_id_str = optarg;
2615 break;
2616 case 'r':
2617 repo_path = realpath(optarg, NULL);
2618 if (repo_path == NULL)
2619 err(1, "-r option");
2620 got_path_strip_trailing_slashes(repo_path);
2621 break;
2622 case 'i':
2623 show_ids = 1;
2624 break;
2625 case 'R':
2626 recurse = 1;
2627 break;
2628 default:
2629 usage_tree();
2630 /* NOTREACHED */
2634 argc -= optind;
2635 argv += optind;
2637 if (argc == 1)
2638 path = argv[0];
2639 else if (argc > 1)
2640 usage_tree();
2641 else
2642 path = NULL;
2644 cwd = getcwd(NULL, 0);
2645 if (cwd == NULL) {
2646 error = got_error_from_errno("getcwd");
2647 goto done;
2649 if (repo_path == NULL) {
2650 error = got_worktree_open(&worktree, cwd);
2651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2652 goto done;
2653 else
2654 error = NULL;
2655 if (worktree) {
2656 repo_path =
2657 strdup(got_worktree_get_repo_path(worktree));
2658 if (repo_path == NULL)
2659 error = got_error_from_errno("strdup");
2660 if (error)
2661 goto done;
2662 } else {
2663 repo_path = strdup(cwd);
2664 if (repo_path == NULL) {
2665 error = got_error_from_errno("strdup");
2666 goto done;
2671 error = got_repo_open(&repo, repo_path);
2672 if (error != NULL)
2673 goto done;
2675 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2676 if (error)
2677 goto done;
2679 if (path == NULL) {
2680 if (worktree) {
2681 char *p, *worktree_subdir = cwd +
2682 strlen(got_worktree_get_root_path(worktree));
2683 if (asprintf(&p, "%s/%s",
2684 got_worktree_get_path_prefix(worktree),
2685 worktree_subdir) == -1) {
2686 error = got_error_from_errno("asprintf");
2687 goto done;
2689 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2690 free(p);
2691 if (error)
2692 goto done;
2693 } else
2694 path = "/";
2696 if (in_repo_path == NULL) {
2697 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2698 if (error != NULL)
2699 goto done;
2702 if (commit_id_str == NULL) {
2703 struct got_reference *head_ref;
2704 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2705 if (error != NULL)
2706 goto done;
2707 error = got_ref_resolve(&commit_id, repo, head_ref);
2708 got_ref_close(head_ref);
2709 if (error != NULL)
2710 goto done;
2711 } else {
2712 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2713 if (error)
2714 goto done;
2717 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2718 in_repo_path, repo);
2719 done:
2720 free(in_repo_path);
2721 free(repo_path);
2722 free(cwd);
2723 free(commit_id);
2724 if (worktree)
2725 got_worktree_close(worktree);
2726 if (repo) {
2727 const struct got_error *repo_error;
2728 repo_error = got_repo_close(repo);
2729 if (error == NULL)
2730 error = repo_error;
2732 return error;
2735 __dead static void
2736 usage_status(void)
2738 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2739 exit(1);
2742 static const struct got_error *
2743 print_status(void *arg, unsigned char status, unsigned char staged_status,
2744 const char *path, struct got_object_id *blob_id,
2745 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2747 if (status == staged_status && (status == GOT_STATUS_DELETE))
2748 status = GOT_STATUS_NO_CHANGE;
2749 printf("%c%c %s\n", status, staged_status, path);
2750 return NULL;
2753 static const struct got_error *
2754 cmd_status(int argc, char *argv[])
2756 const struct got_error *error = NULL;
2757 struct got_repository *repo = NULL;
2758 struct got_worktree *worktree = NULL;
2759 char *cwd = NULL;
2760 struct got_pathlist_head paths;
2761 struct got_pathlist_entry *pe;
2762 int ch;
2764 TAILQ_INIT(&paths);
2766 while ((ch = getopt(argc, argv, "")) != -1) {
2767 switch (ch) {
2768 default:
2769 usage_status();
2770 /* NOTREACHED */
2774 argc -= optind;
2775 argv += optind;
2777 #ifndef PROFILE
2778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2779 NULL) == -1)
2780 err(1, "pledge");
2781 #endif
2782 cwd = getcwd(NULL, 0);
2783 if (cwd == NULL) {
2784 error = got_error_from_errno("getcwd");
2785 goto done;
2788 error = got_worktree_open(&worktree, cwd);
2789 if (error != NULL)
2790 goto done;
2792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2793 if (error != NULL)
2794 goto done;
2796 error = apply_unveil(got_repo_get_path(repo), 1,
2797 got_worktree_get_root_path(worktree));
2798 if (error)
2799 goto done;
2801 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2802 if (error)
2803 goto done;
2805 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2806 check_cancelled, NULL);
2807 done:
2808 TAILQ_FOREACH(pe, &paths, entry)
2809 free((char *)pe->path);
2810 got_pathlist_free(&paths);
2811 free(cwd);
2812 return error;
2815 __dead static void
2816 usage_ref(void)
2818 fprintf(stderr,
2819 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2820 getprogname());
2821 exit(1);
2824 static const struct got_error *
2825 list_refs(struct got_repository *repo)
2827 static const struct got_error *err = NULL;
2828 struct got_reflist_head refs;
2829 struct got_reflist_entry *re;
2831 SIMPLEQ_INIT(&refs);
2832 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2833 if (err)
2834 return err;
2836 SIMPLEQ_FOREACH(re, &refs, entry) {
2837 char *refstr;
2838 refstr = got_ref_to_str(re->ref);
2839 if (refstr == NULL)
2840 return got_error_from_errno("got_ref_to_str");
2841 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2842 free(refstr);
2845 got_ref_list_free(&refs);
2846 return NULL;
2849 static const struct got_error *
2850 delete_ref(struct got_repository *repo, const char *refname)
2852 const struct got_error *err = NULL;
2853 struct got_reference *ref;
2855 err = got_ref_open(&ref, repo, refname, 0);
2856 if (err)
2857 return err;
2859 err = got_ref_delete(ref, repo);
2860 got_ref_close(ref);
2861 return err;
2864 static const struct got_error *
2865 add_ref(struct got_repository *repo, const char *refname, const char *target)
2867 const struct got_error *err = NULL;
2868 struct got_object_id *id;
2869 struct got_reference *ref = NULL;
2872 * Don't let the user create a reference named '-'.
2873 * While technically a valid reference name, this case is usually
2874 * an unintended typo.
2876 if (refname[0] == '-' && refname[1] == '\0')
2877 return got_error(GOT_ERR_BAD_REF_NAME);
2879 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2880 repo);
2881 if (err) {
2882 struct got_reference *target_ref;
2884 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2885 return err;
2886 err = got_ref_open(&target_ref, repo, target, 0);
2887 if (err)
2888 return err;
2889 err = got_ref_resolve(&id, repo, target_ref);
2890 got_ref_close(target_ref);
2891 if (err)
2892 return err;
2895 err = got_ref_alloc(&ref, refname, id);
2896 if (err)
2897 goto done;
2899 err = got_ref_write(ref, repo);
2900 done:
2901 if (ref)
2902 got_ref_close(ref);
2903 free(id);
2904 return err;
2907 static const struct got_error *
2908 add_symref(struct got_repository *repo, const char *refname, const char *target)
2910 const struct got_error *err = NULL;
2911 struct got_reference *ref = NULL;
2912 struct got_reference *target_ref = NULL;
2915 * Don't let the user create a reference named '-'.
2916 * While technically a valid reference name, this case is usually
2917 * an unintended typo.
2919 if (refname[0] == '-' && refname[1] == '\0')
2920 return got_error(GOT_ERR_BAD_REF_NAME);
2922 err = got_ref_open(&target_ref, repo, target, 0);
2923 if (err)
2924 return err;
2926 err = got_ref_alloc_symref(&ref, refname, target_ref);
2927 if (err)
2928 goto done;
2930 err = got_ref_write(ref, repo);
2931 done:
2932 if (target_ref)
2933 got_ref_close(target_ref);
2934 if (ref)
2935 got_ref_close(ref);
2936 return err;
2939 static const struct got_error *
2940 cmd_ref(int argc, char *argv[])
2942 const struct got_error *error = NULL;
2943 struct got_repository *repo = NULL;
2944 struct got_worktree *worktree = NULL;
2945 char *cwd = NULL, *repo_path = NULL;
2946 int ch, do_list = 0, create_symref = 0;
2947 const char *delref = NULL;
2949 /* TODO: Add -s option for adding symbolic references. */
2950 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2951 switch (ch) {
2952 case 'd':
2953 delref = optarg;
2954 break;
2955 case 'r':
2956 repo_path = realpath(optarg, NULL);
2957 if (repo_path == NULL)
2958 err(1, "-r option");
2959 got_path_strip_trailing_slashes(repo_path);
2960 break;
2961 case 'l':
2962 do_list = 1;
2963 break;
2964 case 's':
2965 create_symref = 1;
2966 break;
2967 default:
2968 usage_ref();
2969 /* NOTREACHED */
2973 if (do_list && delref)
2974 errx(1, "-l and -d options are mutually exclusive\n");
2976 argc -= optind;
2977 argv += optind;
2979 if (do_list || delref) {
2980 if (create_symref)
2981 errx(1, "-s option cannot be used together with the "
2982 "-l or -d options");
2983 if (argc > 0)
2984 usage_ref();
2985 } else if (argc != 2)
2986 usage_ref();
2988 #ifndef PROFILE
2989 if (do_list) {
2990 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2991 NULL) == -1)
2992 err(1, "pledge");
2993 } else {
2994 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2995 "sendfd unveil", NULL) == -1)
2996 err(1, "pledge");
2998 #endif
2999 cwd = getcwd(NULL, 0);
3000 if (cwd == NULL) {
3001 error = got_error_from_errno("getcwd");
3002 goto done;
3005 if (repo_path == NULL) {
3006 error = got_worktree_open(&worktree, cwd);
3007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3008 goto done;
3009 else
3010 error = NULL;
3011 if (worktree) {
3012 repo_path =
3013 strdup(got_worktree_get_repo_path(worktree));
3014 if (repo_path == NULL)
3015 error = got_error_from_errno("strdup");
3016 if (error)
3017 goto done;
3018 } else {
3019 repo_path = strdup(cwd);
3020 if (repo_path == NULL) {
3021 error = got_error_from_errno("strdup");
3022 goto done;
3027 error = got_repo_open(&repo, repo_path);
3028 if (error != NULL)
3029 goto done;
3031 error = apply_unveil(got_repo_get_path(repo), do_list,
3032 worktree ? got_worktree_get_root_path(worktree) : NULL);
3033 if (error)
3034 goto done;
3036 if (do_list)
3037 error = list_refs(repo);
3038 else if (delref)
3039 error = delete_ref(repo, delref);
3040 else if (create_symref)
3041 error = add_symref(repo, argv[0], argv[1]);
3042 else
3043 error = add_ref(repo, argv[0], argv[1]);
3044 done:
3045 if (repo)
3046 got_repo_close(repo);
3047 if (worktree)
3048 got_worktree_close(worktree);
3049 free(cwd);
3050 free(repo_path);
3051 return error;
3054 __dead static void
3055 usage_branch(void)
3057 fprintf(stderr,
3058 "usage: %s branch [-r repository] -l | -d name | "
3059 "name [base-branch]\n", getprogname());
3060 exit(1);
3063 static const struct got_error *
3064 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3066 static const struct got_error *err = NULL;
3067 struct got_reflist_head refs;
3068 struct got_reflist_entry *re;
3070 SIMPLEQ_INIT(&refs);
3072 err = got_ref_list(&refs, repo, "refs/heads",
3073 got_ref_cmp_by_name, NULL);
3074 if (err)
3075 return err;
3077 SIMPLEQ_FOREACH(re, &refs, entry) {
3078 const char *refname, *marker = " ";
3079 char *refstr;
3080 refname = got_ref_get_name(re->ref);
3081 if (worktree && strcmp(refname,
3082 got_worktree_get_head_ref_name(worktree)) == 0) {
3083 struct got_object_id *id = NULL;
3084 err = got_ref_resolve(&id, repo, re->ref);
3085 if (err)
3086 return err;
3087 if (got_object_id_cmp(id,
3088 got_worktree_get_base_commit_id(worktree)) == 0)
3089 marker = "* ";
3090 else
3091 marker = "~ ";
3092 free(id);
3094 refname += strlen("refs/heads/");
3095 refstr = got_ref_to_str(re->ref);
3096 if (refstr == NULL)
3097 return got_error_from_errno("got_ref_to_str");
3098 printf("%s%s: %s\n", marker, refname, refstr);
3099 free(refstr);
3102 got_ref_list_free(&refs);
3103 return NULL;
3106 static const struct got_error *
3107 delete_branch(struct got_repository *repo, const char *branch_name)
3109 const struct got_error *err = NULL;
3110 struct got_reference *ref;
3111 char *refname;
3113 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3114 return got_error_from_errno("asprintf");
3116 err = got_ref_open(&ref, repo, refname, 0);
3117 if (err)
3118 goto done;
3120 err = got_ref_delete(ref, repo);
3121 got_ref_close(ref);
3122 done:
3123 free(refname);
3124 return err;
3127 static const struct got_error *
3128 add_branch(struct got_repository *repo, const char *branch_name,
3129 const char *base_branch)
3131 const struct got_error *err = NULL;
3132 struct got_object_id *id = NULL;
3133 struct got_reference *ref = NULL;
3134 char *base_refname = NULL, *refname = NULL;
3135 struct got_reference *base_ref;
3138 * Don't let the user create a branch named '-'.
3139 * While technically a valid reference name, this case is usually
3140 * an unintended typo.
3142 if (branch_name[0] == '-' && branch_name[1] == '\0')
3143 return got_error(GOT_ERR_BAD_REF_NAME);
3145 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3146 base_refname = strdup(GOT_REF_HEAD);
3147 if (base_refname == NULL)
3148 return got_error_from_errno("strdup");
3149 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3150 return got_error_from_errno("asprintf");
3152 err = got_ref_open(&base_ref, repo, base_refname, 0);
3153 if (err)
3154 goto done;
3155 err = got_ref_resolve(&id, repo, base_ref);
3156 got_ref_close(base_ref);
3157 if (err)
3158 goto done;
3160 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3161 err = got_error_from_errno("asprintf");
3162 goto done;
3165 err = got_ref_open(&ref, repo, refname, 0);
3166 if (err == NULL) {
3167 err = got_error(GOT_ERR_BRANCH_EXISTS);
3168 goto done;
3169 } else if (err->code != GOT_ERR_NOT_REF)
3170 goto done;
3172 err = got_ref_alloc(&ref, refname, id);
3173 if (err)
3174 goto done;
3176 err = got_ref_write(ref, repo);
3177 done:
3178 if (ref)
3179 got_ref_close(ref);
3180 free(id);
3181 free(base_refname);
3182 free(refname);
3183 return err;
3186 static const struct got_error *
3187 cmd_branch(int argc, char *argv[])
3189 const struct got_error *error = NULL;
3190 struct got_repository *repo = NULL;
3191 struct got_worktree *worktree = NULL;
3192 char *cwd = NULL, *repo_path = NULL;
3193 int ch, do_list = 0;
3194 const char *delref = NULL;
3196 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3197 switch (ch) {
3198 case 'd':
3199 delref = optarg;
3200 break;
3201 case 'r':
3202 repo_path = realpath(optarg, NULL);
3203 if (repo_path == NULL)
3204 err(1, "-r option");
3205 got_path_strip_trailing_slashes(repo_path);
3206 break;
3207 case 'l':
3208 do_list = 1;
3209 break;
3210 default:
3211 usage_branch();
3212 /* NOTREACHED */
3216 if (do_list && delref)
3217 errx(1, "-l and -d options are mutually exclusive\n");
3219 argc -= optind;
3220 argv += optind;
3222 if (do_list || delref) {
3223 if (argc > 0)
3224 usage_branch();
3225 } else if (argc < 1 || argc > 2)
3226 usage_branch();
3228 #ifndef PROFILE
3229 if (do_list) {
3230 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3231 NULL) == -1)
3232 err(1, "pledge");
3233 } else {
3234 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3235 "sendfd unveil", NULL) == -1)
3236 err(1, "pledge");
3238 #endif
3239 cwd = getcwd(NULL, 0);
3240 if (cwd == NULL) {
3241 error = got_error_from_errno("getcwd");
3242 goto done;
3245 if (repo_path == NULL) {
3246 error = got_worktree_open(&worktree, cwd);
3247 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3248 goto done;
3249 else
3250 error = NULL;
3251 if (worktree) {
3252 repo_path =
3253 strdup(got_worktree_get_repo_path(worktree));
3254 if (repo_path == NULL)
3255 error = got_error_from_errno("strdup");
3256 if (error)
3257 goto done;
3258 } else {
3259 repo_path = strdup(cwd);
3260 if (repo_path == NULL) {
3261 error = got_error_from_errno("strdup");
3262 goto done;
3267 error = got_repo_open(&repo, repo_path);
3268 if (error != NULL)
3269 goto done;
3271 error = apply_unveil(got_repo_get_path(repo), do_list,
3272 worktree ? got_worktree_get_root_path(worktree) : NULL);
3273 if (error)
3274 goto done;
3276 if (do_list)
3277 error = list_branches(repo, worktree);
3278 else if (delref)
3279 error = delete_branch(repo, delref);
3280 else {
3281 const char *base_branch;
3282 if (argc == 1) {
3283 base_branch = worktree ?
3284 got_worktree_get_head_ref_name(worktree) :
3285 GOT_REF_HEAD;
3286 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3287 base_branch += 11;
3288 } else
3289 base_branch = argv[1];
3290 error = add_branch(repo, argv[0], base_branch);
3292 done:
3293 if (repo)
3294 got_repo_close(repo);
3295 if (worktree)
3296 got_worktree_close(worktree);
3297 free(cwd);
3298 free(repo_path);
3299 return error;
3303 __dead static void
3304 usage_tag(void)
3306 fprintf(stderr,
3307 "usage: %s tag [-r repository] | -l | "
3308 "[-m message] name [commit]\n", getprogname());
3309 exit(1);
3312 #if 0
3313 static const struct got_error *
3314 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3316 const struct got_error *err = NULL;
3317 struct got_reflist_entry *re, *se, *new;
3318 struct got_object_id *re_id, *se_id;
3319 struct got_tag_object *re_tag, *se_tag;
3320 time_t re_time, se_time;
3322 SIMPLEQ_FOREACH(re, tags, entry) {
3323 se = SIMPLEQ_FIRST(sorted);
3324 if (se == NULL) {
3325 err = got_reflist_entry_dup(&new, re);
3326 if (err)
3327 return err;
3328 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3329 continue;
3330 } else {
3331 err = got_ref_resolve(&re_id, repo, re->ref);
3332 if (err)
3333 break;
3334 err = got_object_open_as_tag(&re_tag, repo, re_id);
3335 free(re_id);
3336 if (err)
3337 break;
3338 re_time = got_object_tag_get_tagger_time(re_tag);
3339 got_object_tag_close(re_tag);
3342 while (se) {
3343 err = got_ref_resolve(&se_id, repo, re->ref);
3344 if (err)
3345 break;
3346 err = got_object_open_as_tag(&se_tag, repo, se_id);
3347 free(se_id);
3348 if (err)
3349 break;
3350 se_time = got_object_tag_get_tagger_time(se_tag);
3351 got_object_tag_close(se_tag);
3353 if (se_time > re_time) {
3354 err = got_reflist_entry_dup(&new, re);
3355 if (err)
3356 return err;
3357 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3358 break;
3360 se = SIMPLEQ_NEXT(se, entry);
3361 continue;
3364 done:
3365 return err;
3367 #endif
3369 static const struct got_error *
3370 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3371 struct got_reference *ref2)
3373 const struct got_error *err = NULL;
3374 struct got_repository *repo = arg;
3375 struct got_object_id *id1, *id2 = NULL;
3376 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3377 time_t time1, time2;
3379 *cmp = 0;
3381 err = got_ref_resolve(&id1, repo, ref1);
3382 if (err)
3383 return err;
3384 err = got_object_open_as_tag(&tag1, repo, id1);
3385 if (err)
3386 goto done;
3388 err = got_ref_resolve(&id2, repo, ref2);
3389 if (err)
3390 goto done;
3391 err = got_object_open_as_tag(&tag2, repo, id2);
3392 if (err)
3393 goto done;
3395 time1 = got_object_tag_get_tagger_time(tag1);
3396 time2 = got_object_tag_get_tagger_time(tag2);
3398 /* Put latest tags first. */
3399 if (time1 < time2)
3400 *cmp = 1;
3401 else if (time1 > time2)
3402 *cmp = -1;
3403 else
3404 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3405 done:
3406 free(id1);
3407 free(id2);
3408 if (tag1)
3409 got_object_tag_close(tag1);
3410 if (tag2)
3411 got_object_tag_close(tag2);
3412 return err;
3415 static const struct got_error *
3416 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3418 static const struct got_error *err = NULL;
3419 struct got_reflist_head refs;
3420 struct got_reflist_entry *re;
3422 SIMPLEQ_INIT(&refs);
3424 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3425 if (err)
3426 return err;
3428 SIMPLEQ_FOREACH(re, &refs, entry) {
3429 const char *refname;
3430 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3431 char datebuf[26];
3432 time_t tagger_time;
3433 struct got_object_id *id;
3434 struct got_tag_object *tag;
3436 refname = got_ref_get_name(re->ref);
3437 if (strncmp(refname, "refs/tags/", 10) != 0)
3438 continue;
3439 refname += 10;
3440 refstr = got_ref_to_str(re->ref);
3441 if (refstr == NULL) {
3442 err = got_error_from_errno("got_ref_to_str");
3443 break;
3445 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3446 free(refstr);
3448 err = got_ref_resolve(&id, repo, re->ref);
3449 if (err)
3450 break;
3451 err = got_object_open_as_tag(&tag, repo, id);
3452 free(id);
3453 if (err)
3454 break;
3455 printf("from: %s\n", got_object_tag_get_tagger(tag));
3456 tagger_time = got_object_tag_get_tagger_time(tag);
3457 datestr = get_datestr(&tagger_time, datebuf);
3458 if (datestr)
3459 printf("date: %s UTC\n", datestr);
3460 err = got_object_id_str(&id_str,
3461 got_object_tag_get_object_id(tag));
3462 if (err)
3463 break;
3464 switch (got_object_tag_get_object_type(tag)) {
3465 case GOT_OBJ_TYPE_BLOB:
3466 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3467 break;
3468 case GOT_OBJ_TYPE_TREE:
3469 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3470 break;
3471 case GOT_OBJ_TYPE_COMMIT:
3472 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3473 break;
3474 case GOT_OBJ_TYPE_TAG:
3475 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3476 break;
3477 default:
3478 break;
3480 free(id_str);
3481 tagmsg0 = strdup(got_object_tag_get_message(tag));
3482 got_object_tag_close(tag);
3483 if (tagmsg0 == NULL) {
3484 err = got_error_from_errno("strdup");
3485 break;
3488 tagmsg = tagmsg0;
3489 do {
3490 line = strsep(&tagmsg, "\n");
3491 if (line)
3492 printf(" %s\n", line);
3493 } while (line);
3494 free(tagmsg0);
3497 got_ref_list_free(&refs);
3498 return NULL;
3501 static const struct got_error *
3502 get_tag_message(char **tagmsg, const char *commit_id_str,
3503 const char *tag_name, const char *repo_path)
3505 const struct got_error *err = NULL;
3506 char *template = NULL, *initial_content = NULL;
3507 char *tagmsg_path = NULL, *editor = NULL;
3508 int fd = -1;
3510 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3511 err = got_error_from_errno("asprintf");
3512 goto done;
3515 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3516 commit_id_str, tag_name) == -1) {
3517 err = got_error_from_errno("asprintf");
3518 goto done;
3521 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3522 if (err)
3523 goto done;
3525 dprintf(fd, initial_content);
3526 close(fd);
3528 err = get_editor(&editor);
3529 if (err)
3530 goto done;
3531 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3532 done:
3533 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3534 unlink(tagmsg_path);
3535 free(tagmsg_path);
3536 tagmsg_path = NULL;
3538 free(initial_content);
3539 free(template);
3540 free(editor);
3542 /* Editor is done; we can now apply unveil(2) */
3543 if (err == NULL) {
3544 err = apply_unveil(repo_path, 0, NULL);
3545 if (err) {
3546 free(*tagmsg);
3547 *tagmsg = NULL;
3550 return err;
3553 static const struct got_error *
3554 add_tag(struct got_repository *repo, const char *tag_name,
3555 const char *commit_arg, const char *tagmsg_arg)
3557 const struct got_error *err = NULL;
3558 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3559 char *label = NULL, *commit_id_str = NULL;
3560 struct got_reference *ref = NULL;
3561 char *refname = NULL, *tagmsg = NULL;
3562 const char *tagger;
3565 * Don't let the user create a tag named '-'.
3566 * While technically a valid reference name, this case is usually
3567 * an unintended typo.
3569 if (tag_name[0] == '-' && tag_name[1] == '\0')
3570 return got_error(GOT_ERR_BAD_REF_NAME);
3572 err = get_author(&tagger);
3573 if (err)
3574 return err;
3576 err = match_object_id(&commit_id, &label, commit_arg,
3577 GOT_OBJ_TYPE_COMMIT, 1, repo);
3578 if (err)
3579 goto done;
3581 err = got_object_id_str(&commit_id_str, commit_id);
3582 if (err)
3583 goto done;
3585 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3586 refname = strdup(tag_name);
3587 if (refname == NULL) {
3588 err = got_error_from_errno("strdup");
3589 goto done;
3591 tag_name += 10;
3592 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3593 err = got_error_from_errno("asprintf");
3594 goto done;
3597 err = got_ref_open(&ref, repo, refname, 0);
3598 if (err == NULL) {
3599 err = got_error(GOT_ERR_TAG_EXISTS);
3600 goto done;
3601 } else if (err->code != GOT_ERR_NOT_REF)
3602 goto done;
3604 if (tagmsg_arg == NULL) {
3605 err = get_tag_message(&tagmsg, commit_id_str,
3606 tag_name, got_repo_get_path(repo));
3607 if (err)
3608 goto done;
3611 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3612 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3613 if (err)
3614 goto done;
3616 err = got_ref_alloc(&ref, refname, tag_id);
3617 if (err)
3618 goto done;
3620 err = got_ref_write(ref, repo);
3622 if (err == NULL) {
3623 char *tag_id_str;
3624 err = got_object_id_str(&tag_id_str, tag_id);
3625 printf("Created tag %s\n", tag_id_str);
3626 free(tag_id_str);
3628 done:
3629 if (ref)
3630 got_ref_close(ref);
3631 free(commit_id);
3632 free(commit_id_str);
3633 free(refname);
3634 free(tagmsg);
3635 return err;
3638 static const struct got_error *
3639 cmd_tag(int argc, char *argv[])
3641 const struct got_error *error = NULL;
3642 struct got_repository *repo = NULL;
3643 struct got_worktree *worktree = NULL;
3644 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3645 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3646 int ch, do_list = 0;
3648 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3649 switch (ch) {
3650 case 'm':
3651 tagmsg = optarg;
3652 break;
3653 case 'r':
3654 repo_path = realpath(optarg, NULL);
3655 if (repo_path == NULL)
3656 err(1, "-r option");
3657 got_path_strip_trailing_slashes(repo_path);
3658 break;
3659 case 'l':
3660 do_list = 1;
3661 break;
3662 default:
3663 usage_tag();
3664 /* NOTREACHED */
3668 argc -= optind;
3669 argv += optind;
3671 if (do_list) {
3672 if (tagmsg)
3673 errx(1, "-l and -m options are mutually exclusive\n");
3674 if (argc > 0)
3675 usage_tag();
3676 } else if (argc < 1 || argc > 2)
3677 usage_tag();
3678 else if (argc > 1)
3679 commit_id_arg = argv[1];
3680 tag_name = argv[0];
3682 #ifndef PROFILE
3683 if (do_list) {
3684 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3685 NULL) == -1)
3686 err(1, "pledge");
3687 } else {
3688 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3689 "sendfd unveil", NULL) == -1)
3690 err(1, "pledge");
3692 #endif
3693 cwd = getcwd(NULL, 0);
3694 if (cwd == NULL) {
3695 error = got_error_from_errno("getcwd");
3696 goto done;
3699 if (repo_path == NULL) {
3700 error = got_worktree_open(&worktree, cwd);
3701 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3702 goto done;
3703 else
3704 error = NULL;
3705 if (worktree) {
3706 repo_path =
3707 strdup(got_worktree_get_repo_path(worktree));
3708 if (repo_path == NULL)
3709 error = got_error_from_errno("strdup");
3710 if (error)
3711 goto done;
3712 } else {
3713 repo_path = strdup(cwd);
3714 if (repo_path == NULL) {
3715 error = got_error_from_errno("strdup");
3716 goto done;
3721 error = got_repo_open(&repo, repo_path);
3722 if (error != NULL)
3723 goto done;
3726 if (do_list) {
3727 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3728 if (error)
3729 goto done;
3730 error = list_tags(repo, worktree);
3731 } else {
3732 if (tagmsg) {
3733 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3734 if (error)
3735 goto done;
3738 if (commit_id_arg == NULL) {
3739 struct got_reference *head_ref;
3740 struct got_object_id *commit_id;
3741 error = got_ref_open(&head_ref, repo,
3742 worktree ? got_worktree_get_head_ref_name(worktree)
3743 : GOT_REF_HEAD, 0);
3744 if (error)
3745 goto done;
3746 error = got_ref_resolve(&commit_id, repo, head_ref);
3747 got_ref_close(head_ref);
3748 if (error)
3749 goto done;
3750 error = got_object_id_str(&commit_id_str, commit_id);
3751 free(commit_id);
3752 if (error)
3753 goto done;
3756 error = add_tag(repo, tag_name,
3757 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3759 done:
3760 if (repo)
3761 got_repo_close(repo);
3762 if (worktree)
3763 got_worktree_close(worktree);
3764 free(cwd);
3765 free(repo_path);
3766 free(commit_id_str);
3767 return error;
3770 __dead static void
3771 usage_add(void)
3773 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3774 exit(1);
3777 static const struct got_error *
3778 cmd_add(int argc, char *argv[])
3780 const struct got_error *error = NULL;
3781 struct got_repository *repo = NULL;
3782 struct got_worktree *worktree = NULL;
3783 char *cwd = NULL;
3784 struct got_pathlist_head paths;
3785 struct got_pathlist_entry *pe;
3786 int ch;
3788 TAILQ_INIT(&paths);
3790 while ((ch = getopt(argc, argv, "")) != -1) {
3791 switch (ch) {
3792 default:
3793 usage_add();
3794 /* NOTREACHED */
3798 argc -= optind;
3799 argv += optind;
3801 #ifndef PROFILE
3802 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3803 NULL) == -1)
3804 err(1, "pledge");
3805 #endif
3806 if (argc < 1)
3807 usage_add();
3809 cwd = getcwd(NULL, 0);
3810 if (cwd == NULL) {
3811 error = got_error_from_errno("getcwd");
3812 goto done;
3815 error = got_worktree_open(&worktree, cwd);
3816 if (error)
3817 goto done;
3819 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3820 if (error != NULL)
3821 goto done;
3823 error = apply_unveil(got_repo_get_path(repo), 1,
3824 got_worktree_get_root_path(worktree));
3825 if (error)
3826 goto done;
3828 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3829 if (error)
3830 goto done;
3832 error = got_worktree_schedule_add(worktree, &paths, print_status,
3833 NULL, repo);
3834 done:
3835 if (repo)
3836 got_repo_close(repo);
3837 if (worktree)
3838 got_worktree_close(worktree);
3839 TAILQ_FOREACH(pe, &paths, entry)
3840 free((char *)pe->path);
3841 got_pathlist_free(&paths);
3842 free(cwd);
3843 return error;
3846 __dead static void
3847 usage_remove(void)
3849 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3850 exit(1);
3853 static const struct got_error *
3854 print_remove_status(void *arg, unsigned char status,
3855 unsigned char staged_status, const char *path,
3856 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3857 struct got_object_id *commit_id)
3859 if (status == GOT_STATUS_NONEXISTENT)
3860 return NULL;
3861 if (status == staged_status && (status == GOT_STATUS_DELETE))
3862 status = GOT_STATUS_NO_CHANGE;
3863 printf("%c%c %s\n", status, staged_status, path);
3864 return NULL;
3867 static const struct got_error *
3868 cmd_remove(int argc, char *argv[])
3870 const struct got_error *error = NULL;
3871 struct got_worktree *worktree = NULL;
3872 struct got_repository *repo = NULL;
3873 char *cwd = NULL;
3874 struct got_pathlist_head paths;
3875 struct got_pathlist_entry *pe;
3876 int ch, delete_local_mods = 0;
3878 TAILQ_INIT(&paths);
3880 while ((ch = getopt(argc, argv, "f")) != -1) {
3881 switch (ch) {
3882 case 'f':
3883 delete_local_mods = 1;
3884 break;
3885 default:
3886 usage_add();
3887 /* NOTREACHED */
3891 argc -= optind;
3892 argv += optind;
3894 #ifndef PROFILE
3895 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3896 NULL) == -1)
3897 err(1, "pledge");
3898 #endif
3899 if (argc < 1)
3900 usage_remove();
3902 cwd = getcwd(NULL, 0);
3903 if (cwd == NULL) {
3904 error = got_error_from_errno("getcwd");
3905 goto done;
3907 error = got_worktree_open(&worktree, cwd);
3908 if (error)
3909 goto done;
3911 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3912 if (error)
3913 goto done;
3915 error = apply_unveil(got_repo_get_path(repo), 1,
3916 got_worktree_get_root_path(worktree));
3917 if (error)
3918 goto done;
3920 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3921 if (error)
3922 goto done;
3924 error = got_worktree_schedule_delete(worktree, &paths,
3925 delete_local_mods, print_remove_status, NULL, repo);
3926 if (error)
3927 goto done;
3928 done:
3929 if (repo)
3930 got_repo_close(repo);
3931 if (worktree)
3932 got_worktree_close(worktree);
3933 TAILQ_FOREACH(pe, &paths, entry)
3934 free((char *)pe->path);
3935 got_pathlist_free(&paths);
3936 free(cwd);
3937 return error;
3940 __dead static void
3941 usage_revert(void)
3943 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3944 "path ...\n", getprogname());
3945 exit(1);
3948 static const struct got_error *
3949 revert_progress(void *arg, unsigned char status, const char *path)
3951 while (path[0] == '/')
3952 path++;
3953 printf("%c %s\n", status, path);
3954 return NULL;
3957 struct choose_patch_arg {
3958 FILE *patch_script_file;
3959 const char *action;
3962 static const struct got_error *
3963 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3964 int nchanges, const char *action)
3966 char *line = NULL;
3967 size_t linesize = 0;
3968 ssize_t linelen;
3970 switch (status) {
3971 case GOT_STATUS_ADD:
3972 printf("A %s\n%s this addition? [y/n] ", path, action);
3973 break;
3974 case GOT_STATUS_DELETE:
3975 printf("D %s\n%s this deletion? [y/n] ", path, action);
3976 break;
3977 case GOT_STATUS_MODIFY:
3978 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3979 return got_error_from_errno("fseek");
3980 printf(GOT_COMMIT_SEP_STR);
3981 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3982 printf("%s", line);
3983 if (ferror(patch_file))
3984 return got_error_from_errno("getline");
3985 printf(GOT_COMMIT_SEP_STR);
3986 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3987 path, n, nchanges, action);
3988 break;
3989 default:
3990 return got_error_path(path, GOT_ERR_FILE_STATUS);
3993 return NULL;
3996 static const struct got_error *
3997 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3998 FILE *patch_file, int n, int nchanges)
4000 const struct got_error *err = NULL;
4001 char *line = NULL;
4002 size_t linesize = 0;
4003 ssize_t linelen;
4004 int resp = ' ';
4005 struct choose_patch_arg *a = arg;
4007 *choice = GOT_PATCH_CHOICE_NONE;
4009 if (a->patch_script_file) {
4010 char *nl;
4011 err = show_change(status, path, patch_file, n, nchanges,
4012 a->action);
4013 if (err)
4014 return err;
4015 linelen = getline(&line, &linesize, a->patch_script_file);
4016 if (linelen == -1) {
4017 if (ferror(a->patch_script_file))
4018 return got_error_from_errno("getline");
4019 return NULL;
4021 nl = strchr(line, '\n');
4022 if (nl)
4023 *nl = '\0';
4024 if (strcmp(line, "y") == 0) {
4025 *choice = GOT_PATCH_CHOICE_YES;
4026 printf("y\n");
4027 } else if (strcmp(line, "n") == 0) {
4028 *choice = GOT_PATCH_CHOICE_NO;
4029 printf("n\n");
4030 } else if (strcmp(line, "q") == 0 &&
4031 status == GOT_STATUS_MODIFY) {
4032 *choice = GOT_PATCH_CHOICE_QUIT;
4033 printf("q\n");
4034 } else
4035 printf("invalid response '%s'\n", line);
4036 free(line);
4037 return NULL;
4040 while (resp != 'y' && resp != 'n' && resp != 'q') {
4041 err = show_change(status, path, patch_file, n, nchanges,
4042 a->action);
4043 if (err)
4044 return err;
4045 resp = getchar();
4046 if (resp == '\n')
4047 resp = getchar();
4048 if (status == GOT_STATUS_MODIFY) {
4049 if (resp != 'y' && resp != 'n' && resp != 'q') {
4050 printf("invalid response '%c'\n", resp);
4051 resp = ' ';
4053 } else if (resp != 'y' && resp != 'n') {
4054 printf("invalid response '%c'\n", resp);
4055 resp = ' ';
4059 if (resp == 'y')
4060 *choice = GOT_PATCH_CHOICE_YES;
4061 else if (resp == 'n')
4062 *choice = GOT_PATCH_CHOICE_NO;
4063 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4064 *choice = GOT_PATCH_CHOICE_QUIT;
4066 return NULL;
4070 static const struct got_error *
4071 cmd_revert(int argc, char *argv[])
4073 const struct got_error *error = NULL;
4074 struct got_worktree *worktree = NULL;
4075 struct got_repository *repo = NULL;
4076 char *cwd = NULL, *path = NULL;
4077 struct got_pathlist_head paths;
4078 struct got_pathlist_entry *pe;
4079 int ch, can_recurse = 0, pflag = 0;
4080 FILE *patch_script_file = NULL;
4081 const char *patch_script_path = NULL;
4082 struct choose_patch_arg cpa;
4084 TAILQ_INIT(&paths);
4086 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4087 switch (ch) {
4088 case 'p':
4089 pflag = 1;
4090 break;
4091 case 'F':
4092 patch_script_path = optarg;
4093 break;
4094 case 'R':
4095 can_recurse = 1;
4096 break;
4097 default:
4098 usage_revert();
4099 /* NOTREACHED */
4103 argc -= optind;
4104 argv += optind;
4106 #ifndef PROFILE
4107 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4108 "unveil", NULL) == -1)
4109 err(1, "pledge");
4110 #endif
4111 if (argc < 1)
4112 usage_revert();
4113 if (patch_script_path && !pflag)
4114 errx(1, "-F option can only be used together with -p option");
4116 cwd = getcwd(NULL, 0);
4117 if (cwd == NULL) {
4118 error = got_error_from_errno("getcwd");
4119 goto done;
4121 error = got_worktree_open(&worktree, cwd);
4122 if (error)
4123 goto done;
4125 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4126 if (error != NULL)
4127 goto done;
4129 if (patch_script_path) {
4130 patch_script_file = fopen(patch_script_path, "r");
4131 if (patch_script_file == NULL) {
4132 error = got_error_from_errno2("fopen",
4133 patch_script_path);
4134 goto done;
4137 error = apply_unveil(got_repo_get_path(repo), 1,
4138 got_worktree_get_root_path(worktree));
4139 if (error)
4140 goto done;
4142 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4143 if (error)
4144 goto done;
4146 if (!can_recurse) {
4147 char *ondisk_path;
4148 struct stat sb;
4149 TAILQ_FOREACH(pe, &paths, entry) {
4150 if (asprintf(&ondisk_path, "%s/%s",
4151 got_worktree_get_root_path(worktree),
4152 pe->path) == -1) {
4153 error = got_error_from_errno("asprintf");
4154 goto done;
4156 if (lstat(ondisk_path, &sb) == -1) {
4157 if (errno == ENOENT) {
4158 free(ondisk_path);
4159 continue;
4161 error = got_error_from_errno2("lstat",
4162 ondisk_path);
4163 free(ondisk_path);
4164 goto done;
4166 free(ondisk_path);
4167 if (S_ISDIR(sb.st_mode)) {
4168 error = got_error_msg(GOT_ERR_BAD_PATH,
4169 "reverting directories requires -R option");
4170 goto done;
4175 cpa.patch_script_file = patch_script_file;
4176 cpa.action = "revert";
4177 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4178 pflag ? choose_patch : NULL, &cpa, repo);
4179 if (error)
4180 goto done;
4181 done:
4182 if (patch_script_file && fclose(patch_script_file) == EOF &&
4183 error == NULL)
4184 error = got_error_from_errno2("fclose", patch_script_path);
4185 if (repo)
4186 got_repo_close(repo);
4187 if (worktree)
4188 got_worktree_close(worktree);
4189 free(path);
4190 free(cwd);
4191 return error;
4194 __dead static void
4195 usage_commit(void)
4197 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4198 getprogname());
4199 exit(1);
4202 struct collect_commit_logmsg_arg {
4203 const char *cmdline_log;
4204 const char *editor;
4205 const char *worktree_path;
4206 const char *branch_name;
4207 const char *repo_path;
4208 char *logmsg_path;
4212 static const struct got_error *
4213 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4214 void *arg)
4216 char *initial_content = NULL;
4217 struct got_pathlist_entry *pe;
4218 const struct got_error *err = NULL;
4219 char *template = NULL;
4220 struct collect_commit_logmsg_arg *a = arg;
4221 int fd;
4222 size_t len;
4224 /* if a message was specified on the command line, just use it */
4225 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4226 len = strlen(a->cmdline_log) + 1;
4227 *logmsg = malloc(len + 1);
4228 if (*logmsg == NULL)
4229 return got_error_from_errno("malloc");
4230 strlcpy(*logmsg, a->cmdline_log, len);
4231 return NULL;
4234 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4235 return got_error_from_errno("asprintf");
4237 if (asprintf(&initial_content,
4238 "\n# changes to be committed on branch %s:\n",
4239 a->branch_name) == -1)
4240 return got_error_from_errno("asprintf");
4242 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4243 if (err)
4244 goto done;
4246 dprintf(fd, initial_content);
4248 TAILQ_FOREACH(pe, commitable_paths, entry) {
4249 struct got_commitable *ct = pe->data;
4250 dprintf(fd, "# %c %s\n",
4251 got_commitable_get_status(ct),
4252 got_commitable_get_path(ct));
4254 close(fd);
4256 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4257 done:
4258 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4259 unlink(a->logmsg_path);
4260 free(a->logmsg_path);
4261 a->logmsg_path = NULL;
4263 free(initial_content);
4264 free(template);
4266 /* Editor is done; we can now apply unveil(2) */
4267 if (err == NULL) {
4268 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4269 if (err) {
4270 free(*logmsg);
4271 *logmsg = NULL;
4274 return err;
4277 static const struct got_error *
4278 cmd_commit(int argc, char *argv[])
4280 const struct got_error *error = NULL;
4281 struct got_worktree *worktree = NULL;
4282 struct got_repository *repo = NULL;
4283 char *cwd = NULL, *id_str = NULL;
4284 struct got_object_id *id = NULL;
4285 const char *logmsg = NULL;
4286 const char *author;
4287 struct collect_commit_logmsg_arg cl_arg;
4288 char *editor = NULL;
4289 int ch, rebase_in_progress, histedit_in_progress;
4290 struct got_pathlist_head paths;
4292 TAILQ_INIT(&paths);
4293 cl_arg.logmsg_path = NULL;
4295 while ((ch = getopt(argc, argv, "m:")) != -1) {
4296 switch (ch) {
4297 case 'm':
4298 logmsg = optarg;
4299 break;
4300 default:
4301 usage_commit();
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 error = get_author(&author);
4315 if (error)
4316 return error;
4318 cwd = getcwd(NULL, 0);
4319 if (cwd == NULL) {
4320 error = got_error_from_errno("getcwd");
4321 goto done;
4323 error = got_worktree_open(&worktree, cwd);
4324 if (error)
4325 goto done;
4327 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4328 if (error)
4329 goto done;
4330 if (rebase_in_progress) {
4331 error = got_error(GOT_ERR_REBASING);
4332 goto done;
4335 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4336 worktree);
4337 if (error)
4338 goto done;
4340 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4341 if (error != NULL)
4342 goto done;
4345 * unveil(2) traverses exec(2); if an editor is used we have
4346 * to apply unveil after the log message has been written.
4348 if (logmsg == NULL || strlen(logmsg) == 0)
4349 error = get_editor(&editor);
4350 else
4351 error = apply_unveil(got_repo_get_path(repo), 0,
4352 got_worktree_get_root_path(worktree));
4353 if (error)
4354 goto done;
4356 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4357 if (error)
4358 goto done;
4360 cl_arg.editor = editor;
4361 cl_arg.cmdline_log = logmsg;
4362 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4363 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4364 if (!histedit_in_progress) {
4365 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4366 error = got_error(GOT_ERR_COMMIT_BRANCH);
4367 goto done;
4369 cl_arg.branch_name += 11;
4371 cl_arg.repo_path = got_repo_get_path(repo);
4372 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4373 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4374 if (error) {
4375 if (cl_arg.logmsg_path)
4376 fprintf(stderr, "%s: log message preserved in %s\n",
4377 getprogname(), cl_arg.logmsg_path);
4378 goto done;
4381 if (cl_arg.logmsg_path)
4382 unlink(cl_arg.logmsg_path);
4384 error = got_object_id_str(&id_str, id);
4385 if (error)
4386 goto done;
4387 printf("Created commit %s\n", id_str);
4388 done:
4389 free(cl_arg.logmsg_path);
4390 if (repo)
4391 got_repo_close(repo);
4392 if (worktree)
4393 got_worktree_close(worktree);
4394 free(cwd);
4395 free(id_str);
4396 free(editor);
4397 return error;
4400 __dead static void
4401 usage_cherrypick(void)
4403 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4404 exit(1);
4407 static const struct got_error *
4408 cmd_cherrypick(int argc, char *argv[])
4410 const struct got_error *error = NULL;
4411 struct got_worktree *worktree = NULL;
4412 struct got_repository *repo = NULL;
4413 char *cwd = NULL, *commit_id_str = NULL;
4414 struct got_object_id *commit_id = NULL;
4415 struct got_commit_object *commit = NULL;
4416 struct got_object_qid *pid;
4417 struct got_reference *head_ref = NULL;
4418 int ch, did_something = 0;
4420 while ((ch = getopt(argc, argv, "")) != -1) {
4421 switch (ch) {
4422 default:
4423 usage_cherrypick();
4424 /* NOTREACHED */
4428 argc -= optind;
4429 argv += optind;
4431 #ifndef PROFILE
4432 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4433 "unveil", NULL) == -1)
4434 err(1, "pledge");
4435 #endif
4436 if (argc != 1)
4437 usage_cherrypick();
4439 cwd = getcwd(NULL, 0);
4440 if (cwd == NULL) {
4441 error = got_error_from_errno("getcwd");
4442 goto done;
4444 error = got_worktree_open(&worktree, cwd);
4445 if (error)
4446 goto done;
4448 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4449 if (error != NULL)
4450 goto done;
4452 error = apply_unveil(got_repo_get_path(repo), 0,
4453 got_worktree_get_root_path(worktree));
4454 if (error)
4455 goto done;
4457 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4458 GOT_OBJ_TYPE_COMMIT, repo);
4459 if (error != NULL) {
4460 struct got_reference *ref;
4461 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4462 goto done;
4463 error = got_ref_open(&ref, repo, argv[0], 0);
4464 if (error != NULL)
4465 goto done;
4466 error = got_ref_resolve(&commit_id, repo, ref);
4467 got_ref_close(ref);
4468 if (error != NULL)
4469 goto done;
4471 error = got_object_id_str(&commit_id_str, commit_id);
4472 if (error)
4473 goto done;
4475 error = got_ref_open(&head_ref, repo,
4476 got_worktree_get_head_ref_name(worktree), 0);
4477 if (error != NULL)
4478 goto done;
4480 error = check_same_branch(commit_id, head_ref, NULL, repo);
4481 if (error) {
4482 if (error->code != GOT_ERR_ANCESTRY)
4483 goto done;
4484 error = NULL;
4485 } else {
4486 error = got_error(GOT_ERR_SAME_BRANCH);
4487 goto done;
4490 error = got_object_open_as_commit(&commit, repo, commit_id);
4491 if (error)
4492 goto done;
4493 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4494 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4495 commit_id, repo, update_progress, &did_something, check_cancelled,
4496 NULL);
4497 if (error != NULL)
4498 goto done;
4500 if (did_something)
4501 printf("Merged commit %s\n", commit_id_str);
4502 done:
4503 if (commit)
4504 got_object_commit_close(commit);
4505 free(commit_id_str);
4506 if (head_ref)
4507 got_ref_close(head_ref);
4508 if (worktree)
4509 got_worktree_close(worktree);
4510 if (repo)
4511 got_repo_close(repo);
4512 return error;
4515 __dead static void
4516 usage_backout(void)
4518 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4519 exit(1);
4522 static const struct got_error *
4523 cmd_backout(int argc, char *argv[])
4525 const struct got_error *error = NULL;
4526 struct got_worktree *worktree = NULL;
4527 struct got_repository *repo = NULL;
4528 char *cwd = NULL, *commit_id_str = NULL;
4529 struct got_object_id *commit_id = NULL;
4530 struct got_commit_object *commit = NULL;
4531 struct got_object_qid *pid;
4532 struct got_reference *head_ref = NULL;
4533 int ch, did_something = 0;
4535 while ((ch = getopt(argc, argv, "")) != -1) {
4536 switch (ch) {
4537 default:
4538 usage_backout();
4539 /* NOTREACHED */
4543 argc -= optind;
4544 argv += optind;
4546 #ifndef PROFILE
4547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4548 "unveil", NULL) == -1)
4549 err(1, "pledge");
4550 #endif
4551 if (argc != 1)
4552 usage_backout();
4554 cwd = getcwd(NULL, 0);
4555 if (cwd == NULL) {
4556 error = got_error_from_errno("getcwd");
4557 goto done;
4559 error = got_worktree_open(&worktree, cwd);
4560 if (error)
4561 goto done;
4563 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4564 if (error != NULL)
4565 goto done;
4567 error = apply_unveil(got_repo_get_path(repo), 0,
4568 got_worktree_get_root_path(worktree));
4569 if (error)
4570 goto done;
4572 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4573 GOT_OBJ_TYPE_COMMIT, repo);
4574 if (error != NULL) {
4575 struct got_reference *ref;
4576 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4577 goto done;
4578 error = got_ref_open(&ref, repo, argv[0], 0);
4579 if (error != NULL)
4580 goto done;
4581 error = got_ref_resolve(&commit_id, repo, ref);
4582 got_ref_close(ref);
4583 if (error != NULL)
4584 goto done;
4586 error = got_object_id_str(&commit_id_str, commit_id);
4587 if (error)
4588 goto done;
4590 error = got_ref_open(&head_ref, repo,
4591 got_worktree_get_head_ref_name(worktree), 0);
4592 if (error != NULL)
4593 goto done;
4595 error = check_same_branch(commit_id, head_ref, NULL, repo);
4596 if (error)
4597 goto done;
4599 error = got_object_open_as_commit(&commit, repo, commit_id);
4600 if (error)
4601 goto done;
4602 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4603 if (pid == NULL) {
4604 error = got_error(GOT_ERR_ROOT_COMMIT);
4605 goto done;
4608 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4609 update_progress, &did_something, check_cancelled, NULL);
4610 if (error != NULL)
4611 goto done;
4613 if (did_something)
4614 printf("Backed out commit %s\n", commit_id_str);
4615 done:
4616 if (commit)
4617 got_object_commit_close(commit);
4618 free(commit_id_str);
4619 if (head_ref)
4620 got_ref_close(head_ref);
4621 if (worktree)
4622 got_worktree_close(worktree);
4623 if (repo)
4624 got_repo_close(repo);
4625 return error;
4628 __dead static void
4629 usage_rebase(void)
4631 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4632 getprogname());
4633 exit(1);
4636 void
4637 trim_logmsg(char *logmsg, int limit)
4639 char *nl;
4640 size_t len;
4642 len = strlen(logmsg);
4643 if (len > limit)
4644 len = limit;
4645 logmsg[len] = '\0';
4646 nl = strchr(logmsg, '\n');
4647 if (nl)
4648 *nl = '\0';
4651 static const struct got_error *
4652 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4654 const struct got_error *err;
4655 char *logmsg0 = NULL;
4656 const char *s;
4658 err = got_object_commit_get_logmsg(&logmsg0, commit);
4659 if (err)
4660 return err;
4662 s = logmsg0;
4663 while (isspace((unsigned char)s[0]))
4664 s++;
4666 *logmsg = strdup(s);
4667 if (*logmsg == NULL) {
4668 err = got_error_from_errno("strdup");
4669 goto done;
4672 trim_logmsg(*logmsg, limit);
4673 done:
4674 free(logmsg0);
4675 return err;
4678 static const struct got_error *
4679 show_rebase_progress(struct got_commit_object *commit,
4680 struct got_object_id *old_id, struct got_object_id *new_id)
4682 const struct got_error *err;
4683 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4685 err = got_object_id_str(&old_id_str, old_id);
4686 if (err)
4687 goto done;
4689 if (new_id) {
4690 err = got_object_id_str(&new_id_str, new_id);
4691 if (err)
4692 goto done;
4695 old_id_str[12] = '\0';
4696 if (new_id_str)
4697 new_id_str[12] = '\0';
4699 err = get_short_logmsg(&logmsg, 42, commit);
4700 if (err)
4701 goto done;
4703 printf("%s -> %s: %s\n", old_id_str,
4704 new_id_str ? new_id_str : "no-op change", logmsg);
4705 done:
4706 free(old_id_str);
4707 free(new_id_str);
4708 return err;
4711 static const struct got_error *
4712 rebase_progress(void *arg, unsigned char status, const char *path)
4714 unsigned char *rebase_status = arg;
4716 while (path[0] == '/')
4717 path++;
4718 printf("%c %s\n", status, path);
4720 if (*rebase_status == GOT_STATUS_CONFLICT)
4721 return NULL;
4722 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4723 *rebase_status = status;
4724 return NULL;
4727 static const struct got_error *
4728 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4729 struct got_reference *branch, struct got_reference *new_base_branch,
4730 struct got_reference *tmp_branch, struct got_repository *repo)
4732 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4733 return got_worktree_rebase_complete(worktree, fileindex,
4734 new_base_branch, tmp_branch, branch, repo);
4737 static const struct got_error *
4738 rebase_commit(struct got_pathlist_head *merged_paths,
4739 struct got_worktree *worktree, struct got_fileindex *fileindex,
4740 struct got_reference *tmp_branch,
4741 struct got_object_id *commit_id, struct got_repository *repo)
4743 const struct got_error *error;
4744 struct got_commit_object *commit;
4745 struct got_object_id *new_commit_id;
4747 error = got_object_open_as_commit(&commit, repo, commit_id);
4748 if (error)
4749 return error;
4751 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4752 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4753 if (error) {
4754 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4755 goto done;
4756 error = show_rebase_progress(commit, commit_id, NULL);
4757 } else {
4758 error = show_rebase_progress(commit, commit_id, new_commit_id);
4759 free(new_commit_id);
4761 done:
4762 got_object_commit_close(commit);
4763 return error;
4766 struct check_path_prefix_arg {
4767 const char *path_prefix;
4768 size_t len;
4769 int errcode;
4772 static const struct got_error *
4773 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4774 struct got_blob_object *blob2, struct got_object_id *id1,
4775 struct got_object_id *id2, const char *path1, const char *path2,
4776 struct got_repository *repo)
4778 struct check_path_prefix_arg *a = arg;
4780 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4781 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4782 return got_error(a->errcode);
4784 return NULL;
4787 static const struct got_error *
4788 check_path_prefix(struct got_object_id *parent_id,
4789 struct got_object_id *commit_id, const char *path_prefix,
4790 int errcode, struct got_repository *repo)
4792 const struct got_error *err;
4793 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4794 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4795 struct check_path_prefix_arg cpp_arg;
4797 if (got_path_is_root_dir(path_prefix))
4798 return NULL;
4800 err = got_object_open_as_commit(&commit, repo, commit_id);
4801 if (err)
4802 goto done;
4804 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4805 if (err)
4806 goto done;
4808 err = got_object_open_as_tree(&tree1, repo,
4809 got_object_commit_get_tree_id(parent_commit));
4810 if (err)
4811 goto done;
4813 err = got_object_open_as_tree(&tree2, repo,
4814 got_object_commit_get_tree_id(commit));
4815 if (err)
4816 goto done;
4818 cpp_arg.path_prefix = path_prefix;
4819 while (cpp_arg.path_prefix[0] == '/')
4820 cpp_arg.path_prefix++;
4821 cpp_arg.len = strlen(cpp_arg.path_prefix);
4822 cpp_arg.errcode = errcode;
4823 err = got_diff_tree(tree1, tree2, "", "", repo,
4824 check_path_prefix_in_diff, &cpp_arg, 0);
4825 done:
4826 if (tree1)
4827 got_object_tree_close(tree1);
4828 if (tree2)
4829 got_object_tree_close(tree2);
4830 if (commit)
4831 got_object_commit_close(commit);
4832 if (parent_commit)
4833 got_object_commit_close(parent_commit);
4834 return err;
4837 static const struct got_error *
4838 collect_commits(struct got_object_id_queue *commits,
4839 struct got_object_id *initial_commit_id,
4840 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4841 const char *path_prefix, int path_prefix_errcode,
4842 struct got_repository *repo)
4844 const struct got_error *err = NULL;
4845 struct got_commit_graph *graph = NULL;
4846 struct got_object_id *parent_id = NULL;
4847 struct got_object_qid *qid;
4848 struct got_object_id *commit_id = initial_commit_id;
4850 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4851 if (err)
4852 return err;
4854 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4855 check_cancelled, NULL);
4856 if (err)
4857 goto done;
4858 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4859 err = got_commit_graph_iter_next(&parent_id, graph);
4860 if (err) {
4861 if (err->code == GOT_ERR_ITER_COMPLETED) {
4862 err = got_error_msg(GOT_ERR_ANCESTRY,
4863 "ran out of commits to rebase before "
4864 "youngest common ancestor commit has "
4865 "been reached?!?");
4866 goto done;
4867 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4868 goto done;
4869 err = got_commit_graph_fetch_commits(graph, 1, repo,
4870 check_cancelled, NULL);
4871 if (err)
4872 goto done;
4873 } else {
4874 err = check_path_prefix(parent_id, commit_id,
4875 path_prefix, path_prefix_errcode, repo);
4876 if (err)
4877 goto done;
4879 err = got_object_qid_alloc(&qid, commit_id);
4880 if (err)
4881 goto done;
4882 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4883 commit_id = parent_id;
4886 done:
4887 got_commit_graph_close(graph);
4888 return err;
4891 static const struct got_error *
4892 cmd_rebase(int argc, char *argv[])
4894 const struct got_error *error = NULL;
4895 struct got_worktree *worktree = NULL;
4896 struct got_repository *repo = NULL;
4897 struct got_fileindex *fileindex = NULL;
4898 char *cwd = NULL;
4899 struct got_reference *branch = NULL;
4900 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4901 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4902 struct got_object_id *resume_commit_id = NULL;
4903 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4904 struct got_commit_object *commit = NULL;
4905 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4906 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4907 struct got_object_id_queue commits;
4908 struct got_pathlist_head merged_paths;
4909 const struct got_object_id_queue *parent_ids;
4910 struct got_object_qid *qid, *pid;
4912 SIMPLEQ_INIT(&commits);
4913 TAILQ_INIT(&merged_paths);
4915 while ((ch = getopt(argc, argv, "ac")) != -1) {
4916 switch (ch) {
4917 case 'a':
4918 abort_rebase = 1;
4919 break;
4920 case 'c':
4921 continue_rebase = 1;
4922 break;
4923 default:
4924 usage_rebase();
4925 /* NOTREACHED */
4929 argc -= optind;
4930 argv += optind;
4932 #ifndef PROFILE
4933 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4934 "unveil", NULL) == -1)
4935 err(1, "pledge");
4936 #endif
4937 if (abort_rebase && continue_rebase)
4938 usage_rebase();
4939 else if (abort_rebase || continue_rebase) {
4940 if (argc != 0)
4941 usage_rebase();
4942 } else if (argc != 1)
4943 usage_rebase();
4945 cwd = getcwd(NULL, 0);
4946 if (cwd == NULL) {
4947 error = got_error_from_errno("getcwd");
4948 goto done;
4950 error = got_worktree_open(&worktree, cwd);
4951 if (error)
4952 goto done;
4954 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4955 if (error != NULL)
4956 goto done;
4958 error = apply_unveil(got_repo_get_path(repo), 0,
4959 got_worktree_get_root_path(worktree));
4960 if (error)
4961 goto done;
4963 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4964 if (error)
4965 goto done;
4967 if (abort_rebase) {
4968 int did_something;
4969 if (!rebase_in_progress) {
4970 error = got_error(GOT_ERR_NOT_REBASING);
4971 goto done;
4973 error = got_worktree_rebase_continue(&resume_commit_id,
4974 &new_base_branch, &tmp_branch, &branch, &fileindex,
4975 worktree, repo);
4976 if (error)
4977 goto done;
4978 printf("Switching work tree to %s\n",
4979 got_ref_get_symref_target(new_base_branch));
4980 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4981 new_base_branch, update_progress, &did_something);
4982 if (error)
4983 goto done;
4984 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4985 goto done; /* nothing else to do */
4988 if (continue_rebase) {
4989 if (!rebase_in_progress) {
4990 error = got_error(GOT_ERR_NOT_REBASING);
4991 goto done;
4993 error = got_worktree_rebase_continue(&resume_commit_id,
4994 &new_base_branch, &tmp_branch, &branch, &fileindex,
4995 worktree, repo);
4996 if (error)
4997 goto done;
4999 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5000 resume_commit_id, repo);
5001 if (error)
5002 goto done;
5004 yca_id = got_object_id_dup(resume_commit_id);
5005 if (yca_id == NULL) {
5006 error = got_error_from_errno("got_object_id_dup");
5007 goto done;
5009 } else {
5010 error = got_ref_open(&branch, repo, argv[0], 0);
5011 if (error != NULL)
5012 goto done;
5015 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5016 if (error)
5017 goto done;
5019 if (!continue_rebase) {
5020 struct got_object_id *base_commit_id;
5022 base_commit_id = got_worktree_get_base_commit_id(worktree);
5023 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5024 base_commit_id, branch_head_commit_id, repo,
5025 check_cancelled, NULL);
5026 if (error)
5027 goto done;
5028 if (yca_id == NULL) {
5029 error = got_error_msg(GOT_ERR_ANCESTRY,
5030 "specified branch shares no common ancestry "
5031 "with work tree's branch");
5032 goto done;
5035 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5036 if (error) {
5037 if (error->code != GOT_ERR_ANCESTRY)
5038 goto done;
5039 error = NULL;
5040 } else {
5041 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5042 "specified branch resolves to a commit which "
5043 "is already contained in work tree's branch");
5044 goto done;
5046 error = got_worktree_rebase_prepare(&new_base_branch,
5047 &tmp_branch, &fileindex, worktree, branch, repo);
5048 if (error)
5049 goto done;
5052 commit_id = branch_head_commit_id;
5053 error = got_object_open_as_commit(&commit, repo, commit_id);
5054 if (error)
5055 goto done;
5057 parent_ids = got_object_commit_get_parent_ids(commit);
5058 pid = SIMPLEQ_FIRST(parent_ids);
5059 if (pid == NULL) {
5060 if (!continue_rebase) {
5061 int did_something;
5062 error = got_worktree_rebase_abort(worktree, fileindex,
5063 repo, new_base_branch, update_progress,
5064 &did_something);
5065 if (error)
5066 goto done;
5067 printf("Rebase of %s aborted\n",
5068 got_ref_get_name(branch));
5070 error = got_error(GOT_ERR_EMPTY_REBASE);
5071 goto done;
5073 error = collect_commits(&commits, commit_id, pid->id,
5074 yca_id, got_worktree_get_path_prefix(worktree),
5075 GOT_ERR_REBASE_PATH, repo);
5076 got_object_commit_close(commit);
5077 commit = NULL;
5078 if (error)
5079 goto done;
5081 if (SIMPLEQ_EMPTY(&commits)) {
5082 if (continue_rebase)
5083 error = rebase_complete(worktree, fileindex,
5084 branch, new_base_branch, tmp_branch, repo);
5085 else
5086 error = got_error(GOT_ERR_EMPTY_REBASE);
5087 goto done;
5090 pid = NULL;
5091 SIMPLEQ_FOREACH(qid, &commits, entry) {
5092 commit_id = qid->id;
5093 parent_id = pid ? pid->id : yca_id;
5094 pid = qid;
5096 error = got_worktree_rebase_merge_files(&merged_paths,
5097 worktree, fileindex, parent_id, commit_id, repo,
5098 rebase_progress, &rebase_status, check_cancelled, NULL);
5099 if (error)
5100 goto done;
5102 if (rebase_status == GOT_STATUS_CONFLICT) {
5103 got_worktree_rebase_pathlist_free(&merged_paths);
5104 break;
5107 error = rebase_commit(&merged_paths, worktree, fileindex,
5108 tmp_branch, commit_id, repo);
5109 got_worktree_rebase_pathlist_free(&merged_paths);
5110 if (error)
5111 goto done;
5114 if (rebase_status == GOT_STATUS_CONFLICT) {
5115 error = got_worktree_rebase_postpone(worktree, fileindex);
5116 if (error)
5117 goto done;
5118 error = got_error_msg(GOT_ERR_CONFLICTS,
5119 "conflicts must be resolved before rebasing can continue");
5120 } else
5121 error = rebase_complete(worktree, fileindex, branch,
5122 new_base_branch, tmp_branch, repo);
5123 done:
5124 got_object_id_queue_free(&commits);
5125 free(branch_head_commit_id);
5126 free(resume_commit_id);
5127 free(yca_id);
5128 if (commit)
5129 got_object_commit_close(commit);
5130 if (branch)
5131 got_ref_close(branch);
5132 if (new_base_branch)
5133 got_ref_close(new_base_branch);
5134 if (tmp_branch)
5135 got_ref_close(tmp_branch);
5136 if (worktree)
5137 got_worktree_close(worktree);
5138 if (repo)
5139 got_repo_close(repo);
5140 return error;
5143 __dead static void
5144 usage_histedit(void)
5146 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5147 getprogname());
5148 exit(1);
5151 #define GOT_HISTEDIT_PICK 'p'
5152 #define GOT_HISTEDIT_EDIT 'e'
5153 #define GOT_HISTEDIT_FOLD 'f'
5154 #define GOT_HISTEDIT_DROP 'd'
5155 #define GOT_HISTEDIT_MESG 'm'
5157 static struct got_histedit_cmd {
5158 unsigned char code;
5159 const char *name;
5160 const char *desc;
5161 } got_histedit_cmds[] = {
5162 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5163 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5164 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5165 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5166 { GOT_HISTEDIT_MESG, "mesg",
5167 "single-line log message for commit above (open editor if empty)" },
5170 struct got_histedit_list_entry {
5171 TAILQ_ENTRY(got_histedit_list_entry) entry;
5172 struct got_object_id *commit_id;
5173 const struct got_histedit_cmd *cmd;
5174 char *logmsg;
5176 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5178 static const struct got_error *
5179 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5180 FILE *f, struct got_repository *repo)
5182 const struct got_error *err = NULL;
5183 char *logmsg = NULL, *id_str = NULL;
5184 struct got_commit_object *commit = NULL;
5185 int n;
5187 err = got_object_open_as_commit(&commit, repo, commit_id);
5188 if (err)
5189 goto done;
5191 err = get_short_logmsg(&logmsg, 34, commit);
5192 if (err)
5193 goto done;
5195 err = got_object_id_str(&id_str, commit_id);
5196 if (err)
5197 goto done;
5199 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5200 if (n < 0)
5201 err = got_ferror(f, GOT_ERR_IO);
5202 done:
5203 if (commit)
5204 got_object_commit_close(commit);
5205 free(id_str);
5206 free(logmsg);
5207 return err;
5210 static const struct got_error *
5211 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5212 struct got_repository *repo)
5214 const struct got_error *err = NULL;
5215 struct got_object_qid *qid;
5217 if (SIMPLEQ_EMPTY(commits))
5218 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5220 SIMPLEQ_FOREACH(qid, commits, entry) {
5221 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5222 f, repo);
5223 if (err)
5224 break;
5227 return err;
5230 static const struct got_error *
5231 write_cmd_list(FILE *f)
5233 const struct got_error *err = NULL;
5234 int n, i;
5236 n = fprintf(f, "# Available histedit commands:\n");
5237 if (n < 0)
5238 return got_ferror(f, GOT_ERR_IO);
5240 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5241 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5242 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5243 cmd->desc);
5244 if (n < 0) {
5245 err = got_ferror(f, GOT_ERR_IO);
5246 break;
5249 n = fprintf(f, "# Commits will be processed in order from top to "
5250 "bottom of this file.\n");
5251 if (n < 0)
5252 return got_ferror(f, GOT_ERR_IO);
5253 return err;
5256 static const struct got_error *
5257 histedit_syntax_error(int lineno)
5259 static char msg[42];
5260 int ret;
5262 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5263 lineno);
5264 if (ret == -1 || ret >= sizeof(msg))
5265 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5267 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5270 static const struct got_error *
5271 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5272 char *logmsg, struct got_repository *repo)
5274 const struct got_error *err;
5275 struct got_commit_object *folded_commit = NULL;
5276 char *id_str, *folded_logmsg = NULL;
5278 err = got_object_id_str(&id_str, hle->commit_id);
5279 if (err)
5280 return err;
5282 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5283 if (err)
5284 goto done;
5286 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5287 if (err)
5288 goto done;
5289 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5290 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5291 folded_logmsg) == -1) {
5292 err = got_error_from_errno("asprintf");
5293 goto done;
5295 done:
5296 if (folded_commit)
5297 got_object_commit_close(folded_commit);
5298 free(id_str);
5299 free(folded_logmsg);
5300 return err;
5303 static struct got_histedit_list_entry *
5304 get_folded_commits(struct got_histedit_list_entry *hle)
5306 struct got_histedit_list_entry *prev, *folded = NULL;
5308 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5309 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5310 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5311 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5312 folded = prev;
5313 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5316 return folded;
5319 static const struct got_error *
5320 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5321 struct got_repository *repo)
5323 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5324 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5325 const struct got_error *err = NULL;
5326 struct got_commit_object *commit = NULL;
5327 int fd;
5328 struct got_histedit_list_entry *folded = NULL;
5330 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5331 if (err)
5332 return err;
5334 folded = get_folded_commits(hle);
5335 if (folded) {
5336 while (folded != hle) {
5337 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5338 folded = TAILQ_NEXT(folded, entry);
5339 continue;
5341 err = append_folded_commit_msg(&new_msg, folded,
5342 logmsg, repo);
5343 if (err)
5344 goto done;
5345 free(logmsg);
5346 logmsg = new_msg;
5347 folded = TAILQ_NEXT(folded, entry);
5351 err = got_object_id_str(&id_str, hle->commit_id);
5352 if (err)
5353 goto done;
5354 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5355 if (err)
5356 goto done;
5357 if (asprintf(&new_msg,
5358 "%s\n# original log message of commit %s: %s",
5359 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5360 err = got_error_from_errno("asprintf");
5361 goto done;
5363 free(logmsg);
5364 logmsg = new_msg;
5366 err = got_object_id_str(&id_str, hle->commit_id);
5367 if (err)
5368 goto done;
5370 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5371 if (err)
5372 goto done;
5374 dprintf(fd, logmsg);
5375 close(fd);
5377 err = get_editor(&editor);
5378 if (err)
5379 goto done;
5381 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5382 if (err) {
5383 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5384 goto done;
5385 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5387 done:
5388 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5389 err = got_error_from_errno2("unlink", logmsg_path);
5390 free(logmsg_path);
5391 free(logmsg);
5392 free(orig_logmsg);
5393 free(editor);
5394 if (commit)
5395 got_object_commit_close(commit);
5396 return err;
5399 static const struct got_error *
5400 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5401 FILE *f, struct got_repository *repo)
5403 const struct got_error *err = NULL;
5404 char *line = NULL, *p, *end;
5405 size_t size;
5406 ssize_t len;
5407 int lineno = 0, i;
5408 const struct got_histedit_cmd *cmd;
5409 struct got_object_id *commit_id = NULL;
5410 struct got_histedit_list_entry *hle = NULL;
5412 for (;;) {
5413 len = getline(&line, &size, f);
5414 if (len == -1) {
5415 const struct got_error *getline_err;
5416 if (feof(f))
5417 break;
5418 getline_err = got_error_from_errno("getline");
5419 err = got_ferror(f, getline_err->code);
5420 break;
5422 lineno++;
5423 p = line;
5424 while (isspace((unsigned char)p[0]))
5425 p++;
5426 if (p[0] == '#' || p[0] == '\0') {
5427 free(line);
5428 line = NULL;
5429 continue;
5431 cmd = NULL;
5432 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5433 cmd = &got_histedit_cmds[i];
5434 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5435 isspace((unsigned char)p[strlen(cmd->name)])) {
5436 p += strlen(cmd->name);
5437 break;
5439 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5440 p++;
5441 break;
5444 if (i == nitems(got_histedit_cmds)) {
5445 err = histedit_syntax_error(lineno);
5446 break;
5448 while (isspace((unsigned char)p[0]))
5449 p++;
5450 if (cmd->code == GOT_HISTEDIT_MESG) {
5451 if (hle == NULL || hle->logmsg != NULL) {
5452 err = got_error(GOT_ERR_HISTEDIT_CMD);
5453 break;
5455 if (p[0] == '\0') {
5456 err = histedit_edit_logmsg(hle, repo);
5457 if (err)
5458 break;
5459 } else {
5460 hle->logmsg = strdup(p);
5461 if (hle->logmsg == NULL) {
5462 err = got_error_from_errno("strdup");
5463 break;
5466 free(line);
5467 line = NULL;
5468 continue;
5469 } else {
5470 end = p;
5471 while (end[0] && !isspace((unsigned char)end[0]))
5472 end++;
5473 *end = '\0';
5475 err = got_object_resolve_id_str(&commit_id, repo, p);
5476 if (err) {
5477 /* override error code */
5478 err = histedit_syntax_error(lineno);
5479 break;
5482 hle = malloc(sizeof(*hle));
5483 if (hle == NULL) {
5484 err = got_error_from_errno("malloc");
5485 break;
5487 hle->cmd = cmd;
5488 hle->commit_id = commit_id;
5489 hle->logmsg = NULL;
5490 commit_id = NULL;
5491 free(line);
5492 line = NULL;
5493 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5496 free(line);
5497 free(commit_id);
5498 return err;
5501 static const struct got_error *
5502 histedit_check_script(struct got_histedit_list *histedit_cmds,
5503 struct got_object_id_queue *commits, struct got_repository *repo)
5505 const struct got_error *err = NULL;
5506 struct got_object_qid *qid;
5507 struct got_histedit_list_entry *hle;
5508 static char msg[80];
5509 char *id_str;
5511 if (TAILQ_EMPTY(histedit_cmds))
5512 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5513 "histedit script contains no commands");
5514 if (SIMPLEQ_EMPTY(commits))
5515 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5517 SIMPLEQ_FOREACH(qid, commits, entry) {
5518 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5519 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5520 break;
5522 if (hle == NULL) {
5523 err = got_object_id_str(&id_str, qid->id);
5524 if (err)
5525 return err;
5526 snprintf(msg, sizeof(msg),
5527 "commit %s missing from histedit script", id_str);
5528 free(id_str);
5529 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5533 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5534 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5535 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5536 "last commit in histedit script cannot be folded");
5538 return NULL;
5541 static const struct got_error *
5542 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5543 const char *path, struct got_object_id_queue *commits,
5544 struct got_repository *repo)
5546 const struct got_error *err = NULL;
5547 char *editor;
5548 FILE *f = NULL;
5550 err = get_editor(&editor);
5551 if (err)
5552 return err;
5554 if (spawn_editor(editor, path) == -1) {
5555 err = got_error_from_errno("failed spawning editor");
5556 goto done;
5559 f = fopen(path, "r");
5560 if (f == NULL) {
5561 err = got_error_from_errno("fopen");
5562 goto done;
5564 err = histedit_parse_list(histedit_cmds, f, repo);
5565 if (err)
5566 goto done;
5568 err = histedit_check_script(histedit_cmds, commits, repo);
5569 done:
5570 if (f && fclose(f) != 0 && err == NULL)
5571 err = got_error_from_errno("fclose");
5572 free(editor);
5573 return err;
5576 static const struct got_error *
5577 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5578 struct got_object_id_queue *, const char *, struct got_repository *);
5580 static const struct got_error *
5581 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5582 struct got_object_id_queue *commits, struct got_repository *repo)
5584 const struct got_error *err;
5585 FILE *f = NULL;
5586 char *path = NULL;
5588 err = got_opentemp_named(&path, &f, "got-histedit");
5589 if (err)
5590 return err;
5592 err = write_cmd_list(f);
5593 if (err)
5594 goto done;
5596 err = histedit_write_commit_list(commits, f, repo);
5597 if (err)
5598 goto done;
5600 if (fclose(f) != 0) {
5601 err = got_error_from_errno("fclose");
5602 goto done;
5604 f = NULL;
5606 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5607 if (err) {
5608 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5609 err->code != GOT_ERR_HISTEDIT_CMD)
5610 goto done;
5611 err = histedit_edit_list_retry(histedit_cmds, err,
5612 commits, path, repo);
5614 done:
5615 if (f && fclose(f) != 0 && err == NULL)
5616 err = got_error_from_errno("fclose");
5617 if (path && unlink(path) != 0 && err == NULL)
5618 err = got_error_from_errno2("unlink", path);
5619 free(path);
5620 return err;
5623 static const struct got_error *
5624 histedit_save_list(struct got_histedit_list *histedit_cmds,
5625 struct got_worktree *worktree, struct got_repository *repo)
5627 const struct got_error *err = NULL;
5628 char *path = NULL;
5629 FILE *f = NULL;
5630 struct got_histedit_list_entry *hle;
5631 struct got_commit_object *commit = NULL;
5633 err = got_worktree_get_histedit_script_path(&path, worktree);
5634 if (err)
5635 return err;
5637 f = fopen(path, "w");
5638 if (f == NULL) {
5639 err = got_error_from_errno2("fopen", path);
5640 goto done;
5642 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5643 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5644 repo);
5645 if (err)
5646 break;
5648 if (hle->logmsg) {
5649 int n = fprintf(f, "%c %s\n",
5650 GOT_HISTEDIT_MESG, hle->logmsg);
5651 if (n < 0) {
5652 err = got_ferror(f, GOT_ERR_IO);
5653 break;
5657 done:
5658 if (f && fclose(f) != 0 && err == NULL)
5659 err = got_error_from_errno("fclose");
5660 free(path);
5661 if (commit)
5662 got_object_commit_close(commit);
5663 return err;
5666 void
5667 histedit_free_list(struct got_histedit_list *histedit_cmds)
5669 struct got_histedit_list_entry *hle;
5671 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5672 TAILQ_REMOVE(histedit_cmds, hle, entry);
5673 free(hle);
5677 static const struct got_error *
5678 histedit_load_list(struct got_histedit_list *histedit_cmds,
5679 const char *path, struct got_repository *repo)
5681 const struct got_error *err = NULL;
5682 FILE *f = NULL;
5684 f = fopen(path, "r");
5685 if (f == NULL) {
5686 err = got_error_from_errno2("fopen", path);
5687 goto done;
5690 err = histedit_parse_list(histedit_cmds, f, repo);
5691 done:
5692 if (f && fclose(f) != 0 && err == NULL)
5693 err = got_error_from_errno("fclose");
5694 return err;
5697 static const struct got_error *
5698 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5699 const struct got_error *edit_err, struct got_object_id_queue *commits,
5700 const char *path, struct got_repository *repo)
5702 const struct got_error *err = NULL, *prev_err = edit_err;
5703 int resp = ' ';
5705 while (resp != 'c' && resp != 'r' && resp != 'a') {
5706 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5707 "or (a)bort: ", getprogname(), prev_err->msg);
5708 resp = getchar();
5709 if (resp == '\n')
5710 resp = getchar();
5711 if (resp == 'c') {
5712 histedit_free_list(histedit_cmds);
5713 err = histedit_run_editor(histedit_cmds, path, commits,
5714 repo);
5715 if (err) {
5716 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5717 err->code != GOT_ERR_HISTEDIT_CMD)
5718 break;
5719 prev_err = err;
5720 resp = ' ';
5721 continue;
5723 break;
5724 } else if (resp == 'r') {
5725 histedit_free_list(histedit_cmds);
5726 err = histedit_edit_script(histedit_cmds,
5727 commits, repo);
5728 if (err) {
5729 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5730 err->code != GOT_ERR_HISTEDIT_CMD)
5731 break;
5732 prev_err = err;
5733 resp = ' ';
5734 continue;
5736 break;
5737 } else if (resp == 'a') {
5738 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5739 break;
5740 } else
5741 printf("invalid response '%c'\n", resp);
5744 return err;
5747 static const struct got_error *
5748 histedit_complete(struct got_worktree *worktree,
5749 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5750 struct got_reference *branch, struct got_repository *repo)
5752 printf("Switching work tree to %s\n",
5753 got_ref_get_symref_target(branch));
5754 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5755 branch, repo);
5758 static const struct got_error *
5759 show_histedit_progress(struct got_commit_object *commit,
5760 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5762 const struct got_error *err;
5763 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5765 err = got_object_id_str(&old_id_str, hle->commit_id);
5766 if (err)
5767 goto done;
5769 if (new_id) {
5770 err = got_object_id_str(&new_id_str, new_id);
5771 if (err)
5772 goto done;
5775 old_id_str[12] = '\0';
5776 if (new_id_str)
5777 new_id_str[12] = '\0';
5779 if (hle->logmsg) {
5780 logmsg = strdup(hle->logmsg);
5781 if (logmsg == NULL) {
5782 err = got_error_from_errno("strdup");
5783 goto done;
5785 trim_logmsg(logmsg, 42);
5786 } else {
5787 err = get_short_logmsg(&logmsg, 42, commit);
5788 if (err)
5789 goto done;
5792 switch (hle->cmd->code) {
5793 case GOT_HISTEDIT_PICK:
5794 case GOT_HISTEDIT_EDIT:
5795 printf("%s -> %s: %s\n", old_id_str,
5796 new_id_str ? new_id_str : "no-op change", logmsg);
5797 break;
5798 case GOT_HISTEDIT_DROP:
5799 case GOT_HISTEDIT_FOLD:
5800 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5801 logmsg);
5802 break;
5803 default:
5804 break;
5807 done:
5808 free(old_id_str);
5809 free(new_id_str);
5810 return err;
5813 static const struct got_error *
5814 histedit_commit(struct got_pathlist_head *merged_paths,
5815 struct got_worktree *worktree, struct got_fileindex *fileindex,
5816 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5817 struct got_repository *repo)
5819 const struct got_error *err;
5820 struct got_commit_object *commit;
5821 struct got_object_id *new_commit_id;
5823 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5824 && hle->logmsg == NULL) {
5825 err = histedit_edit_logmsg(hle, repo);
5826 if (err)
5827 return err;
5830 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5831 if (err)
5832 return err;
5834 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5835 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5836 hle->logmsg, repo);
5837 if (err) {
5838 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5839 goto done;
5840 err = show_histedit_progress(commit, hle, NULL);
5841 } else {
5842 err = show_histedit_progress(commit, hle, new_commit_id);
5843 free(new_commit_id);
5845 done:
5846 got_object_commit_close(commit);
5847 return err;
5850 static const struct got_error *
5851 histedit_skip_commit(struct got_histedit_list_entry *hle,
5852 struct got_worktree *worktree, struct got_repository *repo)
5854 const struct got_error *error;
5855 struct got_commit_object *commit;
5857 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5858 repo);
5859 if (error)
5860 return error;
5862 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5863 if (error)
5864 return error;
5866 error = show_histedit_progress(commit, hle, NULL);
5867 got_object_commit_close(commit);
5868 return error;
5871 static const struct got_error *
5872 cmd_histedit(int argc, char *argv[])
5874 const struct got_error *error = NULL;
5875 struct got_worktree *worktree = NULL;
5876 struct got_fileindex *fileindex = NULL;
5877 struct got_repository *repo = NULL;
5878 char *cwd = NULL;
5879 struct got_reference *branch = NULL;
5880 struct got_reference *tmp_branch = NULL;
5881 struct got_object_id *resume_commit_id = NULL;
5882 struct got_object_id *base_commit_id = NULL;
5883 struct got_object_id *head_commit_id = NULL;
5884 struct got_commit_object *commit = NULL;
5885 int ch, rebase_in_progress = 0, did_something;
5886 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5887 const char *edit_script_path = NULL;
5888 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5889 struct got_object_id_queue commits;
5890 struct got_pathlist_head merged_paths;
5891 const struct got_object_id_queue *parent_ids;
5892 struct got_object_qid *pid;
5893 struct got_histedit_list histedit_cmds;
5894 struct got_histedit_list_entry *hle;
5896 SIMPLEQ_INIT(&commits);
5897 TAILQ_INIT(&histedit_cmds);
5898 TAILQ_INIT(&merged_paths);
5900 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5901 switch (ch) {
5902 case 'a':
5903 abort_edit = 1;
5904 break;
5905 case 'c':
5906 continue_edit = 1;
5907 break;
5908 case 'F':
5909 edit_script_path = optarg;
5910 break;
5911 default:
5912 usage_histedit();
5913 /* NOTREACHED */
5917 argc -= optind;
5918 argv += optind;
5920 #ifndef PROFILE
5921 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5922 "unveil", NULL) == -1)
5923 err(1, "pledge");
5924 #endif
5925 if (abort_edit && continue_edit)
5926 usage_histedit();
5927 if (argc != 0)
5928 usage_histedit();
5931 * This command cannot apply unveil(2) in all cases because the
5932 * user may choose to run an editor to edit the histedit script
5933 * and to edit individual commit log messages.
5934 * unveil(2) traverses exec(2); if an editor is used we have to
5935 * apply unveil after edit script and log messages have been written.
5936 * XXX TODO: Make use of unveil(2) where possible.
5939 cwd = getcwd(NULL, 0);
5940 if (cwd == NULL) {
5941 error = got_error_from_errno("getcwd");
5942 goto done;
5944 error = got_worktree_open(&worktree, cwd);
5945 if (error)
5946 goto done;
5948 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5949 if (error != NULL)
5950 goto done;
5952 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5953 if (error)
5954 goto done;
5955 if (rebase_in_progress) {
5956 error = got_error(GOT_ERR_REBASING);
5957 goto done;
5960 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5961 if (error)
5962 goto done;
5964 if (edit_in_progress && abort_edit) {
5965 error = got_worktree_histedit_continue(&resume_commit_id,
5966 &tmp_branch, &branch, &base_commit_id, &fileindex,
5967 worktree, repo);
5968 if (error)
5969 goto done;
5970 printf("Switching work tree to %s\n",
5971 got_ref_get_symref_target(branch));
5972 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5973 branch, base_commit_id, update_progress, &did_something);
5974 if (error)
5975 goto done;
5976 printf("Histedit of %s aborted\n",
5977 got_ref_get_symref_target(branch));
5978 goto done; /* nothing else to do */
5979 } else if (abort_edit) {
5980 error = got_error(GOT_ERR_NOT_HISTEDIT);
5981 goto done;
5984 if (continue_edit) {
5985 char *path;
5987 if (!edit_in_progress) {
5988 error = got_error(GOT_ERR_NOT_HISTEDIT);
5989 goto done;
5992 error = got_worktree_get_histedit_script_path(&path, worktree);
5993 if (error)
5994 goto done;
5996 error = histedit_load_list(&histedit_cmds, path, repo);
5997 free(path);
5998 if (error)
5999 goto done;
6001 error = got_worktree_histedit_continue(&resume_commit_id,
6002 &tmp_branch, &branch, &base_commit_id, &fileindex,
6003 worktree, repo);
6004 if (error)
6005 goto done;
6007 error = got_ref_resolve(&head_commit_id, repo, branch);
6008 if (error)
6009 goto done;
6011 error = got_object_open_as_commit(&commit, repo,
6012 head_commit_id);
6013 if (error)
6014 goto done;
6015 parent_ids = got_object_commit_get_parent_ids(commit);
6016 pid = SIMPLEQ_FIRST(parent_ids);
6017 if (pid == NULL) {
6018 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6019 goto done;
6021 error = collect_commits(&commits, head_commit_id, pid->id,
6022 base_commit_id, got_worktree_get_path_prefix(worktree),
6023 GOT_ERR_HISTEDIT_PATH, repo);
6024 got_object_commit_close(commit);
6025 commit = NULL;
6026 if (error)
6027 goto done;
6028 } else {
6029 if (edit_in_progress) {
6030 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6031 goto done;
6034 error = got_ref_open(&branch, repo,
6035 got_worktree_get_head_ref_name(worktree), 0);
6036 if (error != NULL)
6037 goto done;
6039 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6040 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6041 "will not edit commit history of a branch outside "
6042 "the \"refs/heads/\" reference namespace");
6043 goto done;
6046 error = got_ref_resolve(&head_commit_id, repo, branch);
6047 got_ref_close(branch);
6048 branch = NULL;
6049 if (error)
6050 goto done;
6052 error = got_object_open_as_commit(&commit, repo,
6053 head_commit_id);
6054 if (error)
6055 goto done;
6056 parent_ids = got_object_commit_get_parent_ids(commit);
6057 pid = SIMPLEQ_FIRST(parent_ids);
6058 if (pid == NULL) {
6059 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6060 goto done;
6062 error = collect_commits(&commits, head_commit_id, pid->id,
6063 got_worktree_get_base_commit_id(worktree),
6064 got_worktree_get_path_prefix(worktree),
6065 GOT_ERR_HISTEDIT_PATH, repo);
6066 got_object_commit_close(commit);
6067 commit = NULL;
6068 if (error)
6069 goto done;
6071 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6072 &base_commit_id, &fileindex, worktree, repo);
6073 if (error)
6074 goto done;
6076 if (edit_script_path) {
6077 error = histedit_load_list(&histedit_cmds,
6078 edit_script_path, repo);
6079 if (error) {
6080 got_worktree_histedit_abort(worktree, fileindex,
6081 repo, branch, base_commit_id,
6082 update_progress, &did_something);
6083 goto done;
6085 } else {
6086 error = histedit_edit_script(&histedit_cmds, &commits,
6087 repo);
6088 if (error) {
6089 got_worktree_histedit_abort(worktree, fileindex,
6090 repo, branch, base_commit_id,
6091 update_progress, &did_something);
6092 goto done;
6097 error = histedit_save_list(&histedit_cmds, worktree,
6098 repo);
6099 if (error) {
6100 got_worktree_histedit_abort(worktree, fileindex,
6101 repo, branch, base_commit_id,
6102 update_progress, &did_something);
6103 goto done;
6108 error = histedit_check_script(&histedit_cmds, &commits, repo);
6109 if (error)
6110 goto done;
6112 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6113 if (resume_commit_id) {
6114 if (got_object_id_cmp(hle->commit_id,
6115 resume_commit_id) != 0)
6116 continue;
6118 resume_commit_id = NULL;
6119 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6120 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6121 error = histedit_skip_commit(hle, worktree,
6122 repo);
6123 } else {
6124 error = histedit_commit(NULL, worktree,
6125 fileindex, tmp_branch, hle, repo);
6127 if (error)
6128 goto done;
6129 continue;
6132 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6133 error = histedit_skip_commit(hle, worktree, repo);
6134 if (error)
6135 goto done;
6136 continue;
6139 error = got_object_open_as_commit(&commit, repo,
6140 hle->commit_id);
6141 if (error)
6142 goto done;
6143 parent_ids = got_object_commit_get_parent_ids(commit);
6144 pid = SIMPLEQ_FIRST(parent_ids);
6146 error = got_worktree_histedit_merge_files(&merged_paths,
6147 worktree, fileindex, pid->id, hle->commit_id, repo,
6148 rebase_progress, &rebase_status, check_cancelled, NULL);
6149 if (error)
6150 goto done;
6151 got_object_commit_close(commit);
6152 commit = NULL;
6154 if (rebase_status == GOT_STATUS_CONFLICT) {
6155 got_worktree_rebase_pathlist_free(&merged_paths);
6156 break;
6159 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6160 char *id_str;
6161 error = got_object_id_str(&id_str, hle->commit_id);
6162 if (error)
6163 goto done;
6164 printf("Stopping histedit for amending commit %s\n",
6165 id_str);
6166 free(id_str);
6167 got_worktree_rebase_pathlist_free(&merged_paths);
6168 error = got_worktree_histedit_postpone(worktree,
6169 fileindex);
6170 goto done;
6173 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6174 error = histedit_skip_commit(hle, worktree, repo);
6175 if (error)
6176 goto done;
6177 continue;
6180 error = histedit_commit(&merged_paths, worktree, fileindex,
6181 tmp_branch, hle, repo);
6182 got_worktree_rebase_pathlist_free(&merged_paths);
6183 if (error)
6184 goto done;
6187 if (rebase_status == GOT_STATUS_CONFLICT) {
6188 error = got_worktree_histedit_postpone(worktree, fileindex);
6189 if (error)
6190 goto done;
6191 error = got_error_msg(GOT_ERR_CONFLICTS,
6192 "conflicts must be resolved before rebasing can continue");
6193 } else
6194 error = histedit_complete(worktree, fileindex, tmp_branch,
6195 branch, repo);
6196 done:
6197 got_object_id_queue_free(&commits);
6198 histedit_free_list(&histedit_cmds);
6199 free(head_commit_id);
6200 free(base_commit_id);
6201 free(resume_commit_id);
6202 if (commit)
6203 got_object_commit_close(commit);
6204 if (branch)
6205 got_ref_close(branch);
6206 if (tmp_branch)
6207 got_ref_close(tmp_branch);
6208 if (worktree)
6209 got_worktree_close(worktree);
6210 if (repo)
6211 got_repo_close(repo);
6212 return error;
6215 __dead static void
6216 usage_stage(void)
6218 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6219 "[file-path ...]\n",
6220 getprogname());
6221 exit(1);
6224 static const struct got_error *
6225 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6226 const char *path, struct got_object_id *blob_id,
6227 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6229 const struct got_error *err = NULL;
6230 char *id_str = NULL;
6232 if (staged_status != GOT_STATUS_ADD &&
6233 staged_status != GOT_STATUS_MODIFY &&
6234 staged_status != GOT_STATUS_DELETE)
6235 return NULL;
6237 if (staged_status == GOT_STATUS_ADD ||
6238 staged_status == GOT_STATUS_MODIFY)
6239 err = got_object_id_str(&id_str, staged_blob_id);
6240 else
6241 err = got_object_id_str(&id_str, blob_id);
6242 if (err)
6243 return err;
6245 printf("%s %c %s\n", id_str, staged_status, path);
6246 free(id_str);
6247 return NULL;
6250 static const struct got_error *
6251 cmd_stage(int argc, char *argv[])
6253 const struct got_error *error = NULL;
6254 struct got_repository *repo = NULL;
6255 struct got_worktree *worktree = NULL;
6256 char *cwd = NULL;
6257 struct got_pathlist_head paths;
6258 struct got_pathlist_entry *pe;
6259 int ch, list_stage = 0, pflag = 0;
6260 FILE *patch_script_file = NULL;
6261 const char *patch_script_path = NULL;
6262 struct choose_patch_arg cpa;
6264 TAILQ_INIT(&paths);
6266 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6267 switch (ch) {
6268 case 'l':
6269 list_stage = 1;
6270 break;
6271 case 'p':
6272 pflag = 1;
6273 break;
6274 case 'F':
6275 patch_script_path = optarg;
6276 break;
6277 default:
6278 usage_stage();
6279 /* NOTREACHED */
6283 argc -= optind;
6284 argv += optind;
6286 #ifndef PROFILE
6287 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6288 "unveil", NULL) == -1)
6289 err(1, "pledge");
6290 #endif
6291 if (list_stage && (pflag || patch_script_path))
6292 errx(1, "-l option cannot be used with other options");
6293 if (patch_script_path && !pflag)
6294 errx(1, "-F option can only be used together with -p option");
6296 cwd = getcwd(NULL, 0);
6297 if (cwd == NULL) {
6298 error = got_error_from_errno("getcwd");
6299 goto done;
6302 error = got_worktree_open(&worktree, cwd);
6303 if (error)
6304 goto done;
6306 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6307 if (error != NULL)
6308 goto done;
6310 if (patch_script_path) {
6311 patch_script_file = fopen(patch_script_path, "r");
6312 if (patch_script_file == NULL) {
6313 error = got_error_from_errno2("fopen",
6314 patch_script_path);
6315 goto done;
6318 error = apply_unveil(got_repo_get_path(repo), 1,
6319 got_worktree_get_root_path(worktree));
6320 if (error)
6321 goto done;
6323 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6324 if (error)
6325 goto done;
6327 if (list_stage)
6328 error = got_worktree_status(worktree, &paths, repo,
6329 print_stage, NULL, check_cancelled, NULL);
6330 else {
6331 cpa.patch_script_file = patch_script_file;
6332 cpa.action = "stage";
6333 error = got_worktree_stage(worktree, &paths,
6334 pflag ? NULL : print_status, NULL,
6335 pflag ? choose_patch : NULL, &cpa, repo);
6337 done:
6338 if (patch_script_file && fclose(patch_script_file) == EOF &&
6339 error == NULL)
6340 error = got_error_from_errno2("fclose", patch_script_path);
6341 if (repo)
6342 got_repo_close(repo);
6343 if (worktree)
6344 got_worktree_close(worktree);
6345 TAILQ_FOREACH(pe, &paths, entry)
6346 free((char *)pe->path);
6347 got_pathlist_free(&paths);
6348 free(cwd);
6349 return error;
6352 __dead static void
6353 usage_unstage(void)
6355 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6356 "[file-path ...]\n",
6357 getprogname());
6358 exit(1);
6362 static const struct got_error *
6363 cmd_unstage(int argc, char *argv[])
6365 const struct got_error *error = NULL;
6366 struct got_repository *repo = NULL;
6367 struct got_worktree *worktree = NULL;
6368 char *cwd = NULL;
6369 struct got_pathlist_head paths;
6370 struct got_pathlist_entry *pe;
6371 int ch, did_something = 0, pflag = 0;
6372 FILE *patch_script_file = NULL;
6373 const char *patch_script_path = NULL;
6374 struct choose_patch_arg cpa;
6376 TAILQ_INIT(&paths);
6378 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6379 switch (ch) {
6380 case 'p':
6381 pflag = 1;
6382 break;
6383 case 'F':
6384 patch_script_path = optarg;
6385 break;
6386 default:
6387 usage_unstage();
6388 /* NOTREACHED */
6392 argc -= optind;
6393 argv += optind;
6395 #ifndef PROFILE
6396 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6397 "unveil", NULL) == -1)
6398 err(1, "pledge");
6399 #endif
6400 if (patch_script_path && !pflag)
6401 errx(1, "-F option can only be used together with -p option");
6403 cwd = getcwd(NULL, 0);
6404 if (cwd == NULL) {
6405 error = got_error_from_errno("getcwd");
6406 goto done;
6409 error = got_worktree_open(&worktree, cwd);
6410 if (error)
6411 goto done;
6413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6414 if (error != NULL)
6415 goto done;
6417 if (patch_script_path) {
6418 patch_script_file = fopen(patch_script_path, "r");
6419 if (patch_script_file == NULL) {
6420 error = got_error_from_errno2("fopen",
6421 patch_script_path);
6422 goto done;
6426 error = apply_unveil(got_repo_get_path(repo), 1,
6427 got_worktree_get_root_path(worktree));
6428 if (error)
6429 goto done;
6431 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6432 if (error)
6433 goto done;
6435 cpa.patch_script_file = patch_script_file;
6436 cpa.action = "unstage";
6437 error = got_worktree_unstage(worktree, &paths, update_progress,
6438 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6439 done:
6440 if (patch_script_file && fclose(patch_script_file) == EOF &&
6441 error == NULL)
6442 error = got_error_from_errno2("fclose", patch_script_path);
6443 if (repo)
6444 got_repo_close(repo);
6445 if (worktree)
6446 got_worktree_close(worktree);
6447 TAILQ_FOREACH(pe, &paths, entry)
6448 free((char *)pe->path);
6449 got_pathlist_free(&paths);
6450 free(cwd);
6451 return error;
6454 __dead static void
6455 usage_cat(void)
6457 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6458 "[object2 ...]\n", getprogname());
6459 exit(1);
6462 static const struct got_error *
6463 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6465 const struct got_error *err;
6466 struct got_blob_object *blob;
6468 err = got_object_open_as_blob(&blob, repo, id, 8192);
6469 if (err)
6470 return err;
6472 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6473 got_object_blob_close(blob);
6474 return err;
6477 static const struct got_error *
6478 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6480 const struct got_error *err;
6481 struct got_tree_object *tree;
6482 const struct got_tree_entries *entries;
6483 struct got_tree_entry *te;
6485 err = got_object_open_as_tree(&tree, repo, id);
6486 if (err)
6487 return err;
6489 entries = got_object_tree_get_entries(tree);
6490 te = SIMPLEQ_FIRST(&entries->head);
6491 while (te) {
6492 char *id_str;
6493 if (sigint_received || sigpipe_received)
6494 break;
6495 err = got_object_id_str(&id_str, te->id);
6496 if (err)
6497 break;
6498 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6499 free(id_str);
6500 te = SIMPLEQ_NEXT(te, entry);
6503 got_object_tree_close(tree);
6504 return err;
6507 static const struct got_error *
6508 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6510 const struct got_error *err;
6511 struct got_commit_object *commit;
6512 const struct got_object_id_queue *parent_ids;
6513 struct got_object_qid *pid;
6514 char *id_str = NULL;
6515 const char *logmsg = NULL;
6517 err = got_object_open_as_commit(&commit, repo, id);
6518 if (err)
6519 return err;
6521 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6522 if (err)
6523 goto done;
6525 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6526 parent_ids = got_object_commit_get_parent_ids(commit);
6527 fprintf(outfile, "numparents %d\n",
6528 got_object_commit_get_nparents(commit));
6529 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6530 char *pid_str;
6531 err = got_object_id_str(&pid_str, pid->id);
6532 if (err)
6533 goto done;
6534 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6535 free(pid_str);
6537 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6538 got_object_commit_get_author(commit),
6539 got_object_commit_get_author_time(commit));
6541 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6542 got_object_commit_get_author(commit),
6543 got_object_commit_get_committer_time(commit));
6545 logmsg = got_object_commit_get_logmsg_raw(commit);
6546 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6547 fprintf(outfile, "%s", logmsg);
6548 done:
6549 free(id_str);
6550 got_object_commit_close(commit);
6551 return err;
6554 static const struct got_error *
6555 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6557 const struct got_error *err;
6558 struct got_tag_object *tag;
6559 char *id_str = NULL;
6560 const char *tagmsg = NULL;
6562 err = got_object_open_as_tag(&tag, repo, id);
6563 if (err)
6564 return err;
6566 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6567 if (err)
6568 goto done;
6570 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6572 switch (got_object_tag_get_object_type(tag)) {
6573 case GOT_OBJ_TYPE_BLOB:
6574 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6575 GOT_OBJ_LABEL_BLOB);
6576 break;
6577 case GOT_OBJ_TYPE_TREE:
6578 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6579 GOT_OBJ_LABEL_TREE);
6580 break;
6581 case GOT_OBJ_TYPE_COMMIT:
6582 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6583 GOT_OBJ_LABEL_COMMIT);
6584 break;
6585 case GOT_OBJ_TYPE_TAG:
6586 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6587 GOT_OBJ_LABEL_TAG);
6588 break;
6589 default:
6590 break;
6593 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6594 got_object_tag_get_name(tag));
6596 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6597 got_object_tag_get_tagger(tag),
6598 got_object_tag_get_tagger_time(tag));
6600 tagmsg = got_object_tag_get_message(tag);
6601 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6602 fprintf(outfile, "%s", tagmsg);
6603 done:
6604 free(id_str);
6605 got_object_tag_close(tag);
6606 return err;
6609 static const struct got_error *
6610 cmd_cat(int argc, char *argv[])
6612 const struct got_error *error;
6613 struct got_repository *repo = NULL;
6614 struct got_worktree *worktree = NULL;
6615 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6616 struct got_object_id *id = NULL;
6617 int ch, obj_type, i;
6619 #ifndef PROFILE
6620 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6621 NULL) == -1)
6622 err(1, "pledge");
6623 #endif
6625 while ((ch = getopt(argc, argv, "r:")) != -1) {
6626 switch (ch) {
6627 case 'r':
6628 repo_path = realpath(optarg, NULL);
6629 if (repo_path == NULL)
6630 err(1, "-r option");
6631 got_path_strip_trailing_slashes(repo_path);
6632 break;
6633 default:
6634 usage_cat();
6635 /* NOTREACHED */
6639 argc -= optind;
6640 argv += optind;
6642 cwd = getcwd(NULL, 0);
6643 if (cwd == NULL) {
6644 error = got_error_from_errno("getcwd");
6645 goto done;
6647 error = got_worktree_open(&worktree, cwd);
6648 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6649 goto done;
6650 if (worktree) {
6651 if (repo_path == NULL) {
6652 repo_path = strdup(
6653 got_worktree_get_repo_path(worktree));
6654 if (repo_path == NULL) {
6655 error = got_error_from_errno("strdup");
6656 goto done;
6661 if (repo_path == NULL) {
6662 repo_path = getcwd(NULL, 0);
6663 if (repo_path == NULL)
6664 return got_error_from_errno("getcwd");
6667 error = got_repo_open(&repo, repo_path);
6668 free(repo_path);
6669 if (error != NULL)
6670 goto done;
6672 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6673 if (error)
6674 goto done;
6676 for (i = 0; i < argc; i++) {
6677 error = match_object_id(&id, &label, argv[i],
6678 GOT_OBJ_TYPE_ANY, 0, repo);
6679 if (error)
6680 break;
6682 error = got_object_get_type(&obj_type, repo, id);
6683 if (error)
6684 break;
6686 switch (obj_type) {
6687 case GOT_OBJ_TYPE_BLOB:
6688 error = cat_blob(id, repo, stdout);
6689 break;
6690 case GOT_OBJ_TYPE_TREE:
6691 error = cat_tree(id, repo, stdout);
6692 break;
6693 case GOT_OBJ_TYPE_COMMIT:
6694 error = cat_commit(id, repo, stdout);
6695 break;
6696 case GOT_OBJ_TYPE_TAG:
6697 error = cat_tag(id, repo, stdout);
6698 break;
6699 default:
6700 error = got_error(GOT_ERR_OBJ_TYPE);
6701 break;
6703 if (error)
6704 break;
6705 free(label);
6706 label = NULL;
6707 free(id);
6708 id = NULL;
6711 done:
6712 free(label);
6713 free(id);
6714 if (worktree)
6715 got_worktree_close(worktree);
6716 if (repo) {
6717 const struct got_error *repo_error;
6718 repo_error = got_repo_close(repo);
6719 if (error == NULL)
6720 error = repo_error;
6722 return error;