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 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1321 const char *path, int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1326 if (blob_id1) {
1327 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1328 if (err)
1329 goto done;
1332 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1333 if (err)
1334 goto done;
1336 while (path[0] == '/')
1337 path++;
1338 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1339 stdout);
1340 done:
1341 if (blob1)
1342 got_object_blob_close(blob1);
1343 got_object_blob_close(blob2);
1344 return err;
1347 static const struct got_error *
1348 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1349 const char *path, int diff_context, struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1353 struct got_diff_blob_output_unidiff_arg arg;
1355 if (tree_id1) {
1356 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1357 if (err)
1358 goto done;
1361 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1362 if (err)
1363 goto done;
1365 arg.diff_context = diff_context;
1366 arg.outfile = stdout;
1367 while (path[0] == '/')
1368 path++;
1369 err = got_diff_tree(tree1, tree2, path, path, repo,
1370 got_diff_blob_output_unidiff, &arg, 1);
1371 done:
1372 if (tree1)
1373 got_object_tree_close(tree1);
1374 got_object_tree_close(tree2);
1375 return err;
1378 static const struct got_error *
1379 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1380 const char *path, int diff_context, struct got_repository *repo)
1382 const struct got_error *err = NULL;
1383 struct got_commit_object *pcommit = NULL;
1384 char *id_str1 = NULL, *id_str2 = NULL;
1385 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1386 struct got_object_qid *qid;
1388 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1389 if (qid != NULL) {
1390 err = got_object_open_as_commit(&pcommit, repo,
1391 qid->id);
1392 if (err)
1393 return err;
1396 if (path && path[0] != '\0') {
1397 int obj_type;
1398 err = got_object_id_by_path(&obj_id2, repo, id, path);
1399 if (err)
1400 goto done;
1401 err = got_object_id_str(&id_str2, obj_id2);
1402 if (err) {
1403 free(obj_id2);
1404 goto done;
1406 if (pcommit) {
1407 err = got_object_id_by_path(&obj_id1, repo,
1408 qid->id, path);
1409 if (err) {
1410 free(obj_id2);
1411 goto done;
1413 err = got_object_id_str(&id_str1, obj_id1);
1414 if (err) {
1415 free(obj_id2);
1416 goto done;
1419 err = got_object_get_type(&obj_type, repo, obj_id2);
1420 if (err) {
1421 free(obj_id2);
1422 goto done;
1424 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1425 switch (obj_type) {
1426 case GOT_OBJ_TYPE_BLOB:
1427 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1428 repo);
1429 break;
1430 case GOT_OBJ_TYPE_TREE:
1431 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1432 repo);
1433 break;
1434 default:
1435 err = got_error(GOT_ERR_OBJ_TYPE);
1436 break;
1438 free(obj_id1);
1439 free(obj_id2);
1440 } else {
1441 obj_id2 = got_object_commit_get_tree_id(commit);
1442 err = got_object_id_str(&id_str2, obj_id2);
1443 if (err)
1444 goto done;
1445 obj_id1 = got_object_commit_get_tree_id(pcommit);
1446 err = got_object_id_str(&id_str1, obj_id1);
1447 if (err)
1448 goto done;
1449 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1450 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1453 done:
1454 free(id_str1);
1455 free(id_str2);
1456 if (pcommit)
1457 got_object_commit_close(pcommit);
1458 return err;
1461 static char *
1462 get_datestr(time_t *time, char *datebuf)
1464 struct tm mytm, *tm;
1465 char *p, *s;
1467 tm = gmtime_r(time, &mytm);
1468 if (tm == NULL)
1469 return NULL;
1470 s = asctime_r(tm, datebuf);
1471 if (s == NULL)
1472 return NULL;
1473 p = strchr(s, '\n');
1474 if (p)
1475 *p = '\0';
1476 return s;
1479 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1481 static const struct got_error *
1482 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1483 struct got_repository *repo, const char *path, int show_patch,
1484 int diff_context, struct got_reflist_head *refs)
1486 const struct got_error *err = NULL;
1487 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1488 char datebuf[26];
1489 time_t committer_time;
1490 const char *author, *committer;
1491 char *refs_str = NULL;
1492 struct got_reflist_entry *re;
1494 SIMPLEQ_FOREACH(re, refs, entry) {
1495 char *s;
1496 const char *name;
1497 struct got_tag_object *tag = NULL;
1498 int cmp;
1500 name = got_ref_get_name(re->ref);
1501 if (strcmp(name, GOT_REF_HEAD) == 0)
1502 continue;
1503 if (strncmp(name, "refs/", 5) == 0)
1504 name += 5;
1505 if (strncmp(name, "got/", 4) == 0)
1506 continue;
1507 if (strncmp(name, "heads/", 6) == 0)
1508 name += 6;
1509 if (strncmp(name, "remotes/", 8) == 0)
1510 name += 8;
1511 if (strncmp(name, "tags/", 5) == 0) {
1512 err = got_object_open_as_tag(&tag, repo, re->id);
1513 if (err) {
1514 if (err->code != GOT_ERR_OBJ_TYPE)
1515 return err;
1516 /* Ref points at something other than a tag. */
1517 err = NULL;
1518 tag = NULL;
1521 cmp = got_object_id_cmp(tag ?
1522 got_object_tag_get_object_id(tag) : re->id, id);
1523 if (tag)
1524 got_object_tag_close(tag);
1525 if (cmp != 0)
1526 continue;
1527 s = refs_str;
1528 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1529 name) == -1) {
1530 err = got_error_from_errno("asprintf");
1531 free(s);
1532 return err;
1534 free(s);
1536 err = got_object_id_str(&id_str, id);
1537 if (err)
1538 return err;
1540 printf(GOT_COMMIT_SEP_STR);
1541 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1542 refs_str ? refs_str : "", refs_str ? ")" : "");
1543 free(id_str);
1544 id_str = NULL;
1545 free(refs_str);
1546 refs_str = NULL;
1547 printf("from: %s\n", got_object_commit_get_author(commit));
1548 committer_time = got_object_commit_get_committer_time(commit);
1549 datestr = get_datestr(&committer_time, datebuf);
1550 if (datestr)
1551 printf("date: %s UTC\n", datestr);
1552 author = got_object_commit_get_author(commit);
1553 committer = got_object_commit_get_committer(commit);
1554 if (strcmp(author, committer) != 0)
1555 printf("via: %s\n", committer);
1556 if (got_object_commit_get_nparents(commit) > 1) {
1557 const struct got_object_id_queue *parent_ids;
1558 struct got_object_qid *qid;
1559 int n = 1;
1560 parent_ids = got_object_commit_get_parent_ids(commit);
1561 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1562 err = got_object_id_str(&id_str, qid->id);
1563 if (err)
1564 return err;
1565 printf("parent %d: %s\n", n++, id_str);
1566 free(id_str);
1570 err = got_object_commit_get_logmsg(&logmsg0, commit);
1571 if (err)
1572 return err;
1574 logmsg = logmsg0;
1575 do {
1576 line = strsep(&logmsg, "\n");
1577 if (line)
1578 printf(" %s\n", line);
1579 } while (line);
1580 free(logmsg0);
1582 if (show_patch) {
1583 err = print_patch(commit, id, path, diff_context, repo);
1584 if (err == 0)
1585 printf("\n");
1588 if (fflush(stdout) != 0 && err == NULL)
1589 err = got_error_from_errno("fflush");
1590 return err;
1593 static const struct got_error *
1594 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1595 char *path, int show_patch, int diff_context, int limit,
1596 int first_parent_traversal, struct got_reflist_head *refs)
1598 const struct got_error *err;
1599 struct got_commit_graph *graph;
1601 err = got_commit_graph_open(&graph, root_id, path,
1602 first_parent_traversal, repo);
1603 if (err)
1604 return err;
1605 err = got_commit_graph_iter_start(graph, root_id, repo,
1606 check_cancelled, NULL);
1607 if (err)
1608 goto done;
1609 for (;;) {
1610 struct got_commit_object *commit;
1611 struct got_object_id *id;
1613 if (sigint_received || sigpipe_received)
1614 break;
1616 err = got_commit_graph_iter_next(&id, graph);
1617 if (err) {
1618 if (err->code == GOT_ERR_ITER_COMPLETED) {
1619 err = NULL;
1620 break;
1622 if (err->code != GOT_ERR_ITER_NEED_MORE)
1623 break;
1624 err = got_commit_graph_fetch_commits(graph, 1, repo,
1625 check_cancelled, NULL);
1626 if (err)
1627 break;
1628 else
1629 continue;
1631 if (id == NULL)
1632 break;
1634 err = got_object_open_as_commit(&commit, repo, id);
1635 if (err)
1636 break;
1637 err = print_commit(commit, id, repo, path, show_patch,
1638 diff_context, refs);
1639 got_object_commit_close(commit);
1640 if (err || (limit && --limit == 0))
1641 break;
1643 done:
1644 got_commit_graph_close(graph);
1645 return err;
1648 __dead static void
1649 usage_log(void)
1651 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1652 "[-r repository-path] [path]\n", getprogname());
1653 exit(1);
1656 static int
1657 get_default_log_limit(void)
1659 const char *got_default_log_limit;
1660 long long n;
1661 const char *errstr;
1663 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1664 if (got_default_log_limit == NULL)
1665 return 0;
1666 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1667 if (errstr != NULL)
1668 return 0;
1669 return n;
1672 static const struct got_error *
1673 cmd_log(int argc, char *argv[])
1675 const struct got_error *error;
1676 struct got_repository *repo = NULL;
1677 struct got_worktree *worktree = NULL;
1678 struct got_commit_object *commit = NULL;
1679 struct got_object_id *id = NULL;
1680 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1681 char *start_commit = NULL;
1682 int diff_context = 3, ch;
1683 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1684 const char *errstr;
1685 struct got_reflist_head refs;
1687 SIMPLEQ_INIT(&refs);
1689 #ifndef PROFILE
1690 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1691 NULL)
1692 == -1)
1693 err(1, "pledge");
1694 #endif
1696 limit = get_default_log_limit();
1698 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1699 switch (ch) {
1700 case 'p':
1701 show_patch = 1;
1702 break;
1703 case 'c':
1704 start_commit = optarg;
1705 break;
1706 case 'C':
1707 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1708 &errstr);
1709 if (errstr != NULL)
1710 err(1, "-C option %s", errstr);
1711 break;
1712 case 'l':
1713 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1714 if (errstr != NULL)
1715 err(1, "-l option %s", errstr);
1716 break;
1717 case 'f':
1718 first_parent_traversal = 1;
1719 break;
1720 case 'r':
1721 repo_path = realpath(optarg, NULL);
1722 if (repo_path == NULL)
1723 err(1, "-r option");
1724 got_path_strip_trailing_slashes(repo_path);
1725 break;
1726 default:
1727 usage_log();
1728 /* NOTREACHED */
1732 argc -= optind;
1733 argv += optind;
1735 cwd = getcwd(NULL, 0);
1736 if (cwd == NULL) {
1737 error = got_error_from_errno("getcwd");
1738 goto done;
1741 error = got_worktree_open(&worktree, cwd);
1742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1743 goto done;
1744 error = NULL;
1746 if (argc == 0) {
1747 path = strdup("");
1748 if (path == NULL) {
1749 error = got_error_from_errno("strdup");
1750 goto done;
1752 } else if (argc == 1) {
1753 if (worktree) {
1754 error = got_worktree_resolve_path(&path, worktree,
1755 argv[0]);
1756 if (error)
1757 goto done;
1758 } else {
1759 path = strdup(argv[0]);
1760 if (path == NULL) {
1761 error = got_error_from_errno("strdup");
1762 goto done;
1765 } else
1766 usage_log();
1768 if (repo_path == NULL) {
1769 repo_path = worktree ?
1770 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1772 if (repo_path == NULL) {
1773 error = got_error_from_errno("strdup");
1774 goto done;
1777 error = got_repo_open(&repo, repo_path);
1778 if (error != NULL)
1779 goto done;
1781 error = apply_unveil(got_repo_get_path(repo), 1,
1782 worktree ? got_worktree_get_root_path(worktree) : NULL);
1783 if (error)
1784 goto done;
1786 if (start_commit == NULL) {
1787 struct got_reference *head_ref;
1788 error = got_ref_open(&head_ref, repo,
1789 worktree ? got_worktree_get_head_ref_name(worktree)
1790 : GOT_REF_HEAD, 0);
1791 if (error != NULL)
1792 return error;
1793 error = got_ref_resolve(&id, repo, head_ref);
1794 got_ref_close(head_ref);
1795 if (error != NULL)
1796 return error;
1797 error = got_object_open_as_commit(&commit, repo, id);
1798 } else {
1799 struct got_reference *ref;
1800 error = got_ref_open(&ref, repo, start_commit, 0);
1801 if (error == NULL) {
1802 int obj_type;
1803 error = got_ref_resolve(&id, repo, ref);
1804 got_ref_close(ref);
1805 if (error != NULL)
1806 goto done;
1807 error = got_object_get_type(&obj_type, repo, id);
1808 if (error != NULL)
1809 goto done;
1810 if (obj_type == GOT_OBJ_TYPE_TAG) {
1811 struct got_tag_object *tag;
1812 error = got_object_open_as_tag(&tag, repo, id);
1813 if (error != NULL)
1814 goto done;
1815 if (got_object_tag_get_object_type(tag) !=
1816 GOT_OBJ_TYPE_COMMIT) {
1817 got_object_tag_close(tag);
1818 error = got_error(GOT_ERR_OBJ_TYPE);
1819 goto done;
1821 free(id);
1822 id = got_object_id_dup(
1823 got_object_tag_get_object_id(tag));
1824 if (id == NULL)
1825 error = got_error_from_errno(
1826 "got_object_id_dup");
1827 got_object_tag_close(tag);
1828 if (error)
1829 goto done;
1830 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1831 error = got_error(GOT_ERR_OBJ_TYPE);
1832 goto done;
1834 error = got_object_open_as_commit(&commit, repo, id);
1835 if (error != NULL)
1836 goto done;
1838 if (commit == NULL) {
1839 error = got_repo_match_object_id_prefix(&id,
1840 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1841 if (error != NULL)
1842 return error;
1845 if (error != NULL)
1846 goto done;
1848 if (worktree) {
1849 const char *prefix = got_worktree_get_path_prefix(worktree);
1850 char *p;
1851 if (asprintf(&p, "%s%s%s", prefix,
1852 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1853 error = got_error_from_errno("asprintf");
1854 goto done;
1856 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1857 free(p);
1858 } else
1859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1860 if (error != NULL)
1861 goto done;
1862 if (in_repo_path) {
1863 free(path);
1864 path = in_repo_path;
1867 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1868 if (error)
1869 goto done;
1871 error = print_commits(id, repo, path, show_patch,
1872 diff_context, limit, first_parent_traversal, &refs);
1873 done:
1874 free(path);
1875 free(repo_path);
1876 free(cwd);
1877 free(id);
1878 if (worktree)
1879 got_worktree_close(worktree);
1880 if (repo) {
1881 const struct got_error *repo_error;
1882 repo_error = got_repo_close(repo);
1883 if (error == NULL)
1884 error = repo_error;
1886 got_ref_list_free(&refs);
1887 return error;
1890 __dead static void
1891 usage_diff(void)
1893 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1894 "[object1 object2 | path]\n", getprogname());
1895 exit(1);
1898 struct print_diff_arg {
1899 struct got_repository *repo;
1900 struct got_worktree *worktree;
1901 int diff_context;
1902 const char *id_str;
1903 int header_shown;
1904 int diff_staged;
1907 static const struct got_error *
1908 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1909 const char *path, struct got_object_id *blob_id,
1910 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1912 struct print_diff_arg *a = arg;
1913 const struct got_error *err = NULL;
1914 struct got_blob_object *blob1 = NULL;
1915 FILE *f2 = NULL;
1916 char *abspath = NULL, *label1 = NULL;
1917 struct stat sb;
1919 if (a->diff_staged) {
1920 if (staged_status != GOT_STATUS_MODIFY &&
1921 staged_status != GOT_STATUS_ADD &&
1922 staged_status != GOT_STATUS_DELETE)
1923 return NULL;
1924 } else {
1925 if (staged_status == GOT_STATUS_DELETE)
1926 return NULL;
1927 if (status == GOT_STATUS_NONEXISTENT)
1928 return got_error_set_errno(ENOENT, path);
1929 if (status != GOT_STATUS_MODIFY &&
1930 status != GOT_STATUS_ADD &&
1931 status != GOT_STATUS_DELETE &&
1932 status != GOT_STATUS_CONFLICT)
1933 return NULL;
1936 if (!a->header_shown) {
1937 printf("diff %s %s%s\n", a->id_str,
1938 got_worktree_get_root_path(a->worktree),
1939 a->diff_staged ? " (staged changes)" : "");
1940 a->header_shown = 1;
1943 if (a->diff_staged) {
1944 const char *label1 = NULL, *label2 = NULL;
1945 switch (staged_status) {
1946 case GOT_STATUS_MODIFY:
1947 label1 = path;
1948 label2 = path;
1949 break;
1950 case GOT_STATUS_ADD:
1951 label2 = path;
1952 break;
1953 case GOT_STATUS_DELETE:
1954 label1 = path;
1955 break;
1956 default:
1957 return got_error(GOT_ERR_FILE_STATUS);
1959 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1960 label1, label2, a->diff_context, a->repo, stdout);
1963 if (staged_status == GOT_STATUS_ADD ||
1964 staged_status == GOT_STATUS_MODIFY) {
1965 char *id_str;
1966 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1967 8192);
1968 if (err)
1969 goto done;
1970 err = got_object_id_str(&id_str, staged_blob_id);
1971 if (err)
1972 goto done;
1973 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1974 err = got_error_from_errno("asprintf");
1975 free(id_str);
1976 goto done;
1978 free(id_str);
1979 } else if (status != GOT_STATUS_ADD) {
1980 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1981 if (err)
1982 goto done;
1985 if (status != GOT_STATUS_DELETE) {
1986 if (asprintf(&abspath, "%s/%s",
1987 got_worktree_get_root_path(a->worktree), path) == -1) {
1988 err = got_error_from_errno("asprintf");
1989 goto done;
1992 f2 = fopen(abspath, "r");
1993 if (f2 == NULL) {
1994 err = got_error_from_errno2("fopen", abspath);
1995 goto done;
1997 if (lstat(abspath, &sb) == -1) {
1998 err = got_error_from_errno2("lstat", abspath);
1999 goto done;
2001 } else
2002 sb.st_size = 0;
2004 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2005 a->diff_context, stdout);
2006 done:
2007 if (blob1)
2008 got_object_blob_close(blob1);
2009 if (f2 && fclose(f2) != 0 && err == NULL)
2010 err = got_error_from_errno("fclose");
2011 free(abspath);
2012 return err;
2015 static const struct got_error *
2016 match_object_id(struct got_object_id **id, char **label,
2017 const char *id_str, int obj_type, int resolve_tags,
2018 struct got_repository *repo)
2020 const struct got_error *err;
2021 struct got_tag_object *tag;
2022 struct got_reference *ref = NULL;
2024 *id = NULL;
2025 *label = NULL;
2027 if (resolve_tags) {
2028 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2029 repo);
2030 if (err == NULL) {
2031 *id = got_object_id_dup(
2032 got_object_tag_get_object_id(tag));
2033 if (*id == NULL)
2034 err = got_error_from_errno("got_object_id_dup");
2035 else if (asprintf(label, "refs/tags/%s",
2036 got_object_tag_get_name(tag)) == -1) {
2037 err = got_error_from_errno("asprintf");
2038 free(*id);
2039 *id = NULL;
2041 got_object_tag_close(tag);
2042 return err;
2043 } else if (err->code != GOT_ERR_NO_OBJ)
2044 return err;
2047 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2048 if (err) {
2049 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2050 return err;
2051 err = got_ref_open(&ref, repo, id_str, 0);
2052 if (err != NULL)
2053 goto done;
2054 *label = strdup(got_ref_get_name(ref));
2055 if (*label == NULL) {
2056 err = got_error_from_errno("strdup");
2057 goto done;
2059 err = got_ref_resolve(id, repo, ref);
2060 } else {
2061 err = got_object_id_str(label, *id);
2062 if (*label == NULL) {
2063 err = got_error_from_errno("strdup");
2064 goto done;
2067 done:
2068 if (ref)
2069 got_ref_close(ref);
2070 return err;
2074 static const struct got_error *
2075 cmd_diff(int argc, char *argv[])
2077 const struct got_error *error;
2078 struct got_repository *repo = NULL;
2079 struct got_worktree *worktree = NULL;
2080 char *cwd = NULL, *repo_path = NULL;
2081 struct got_object_id *id1 = NULL, *id2 = NULL;
2082 const char *id_str1 = NULL, *id_str2 = NULL;
2083 char *label1 = NULL, *label2 = NULL;
2084 int type1, type2;
2085 int diff_context = 3, diff_staged = 0, ch;
2086 const char *errstr;
2087 char *path = NULL;
2089 #ifndef PROFILE
2090 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2091 NULL) == -1)
2092 err(1, "pledge");
2093 #endif
2095 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2096 switch (ch) {
2097 case 'C':
2098 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2099 if (errstr != NULL)
2100 err(1, "-C option %s", errstr);
2101 break;
2102 case 'r':
2103 repo_path = realpath(optarg, NULL);
2104 if (repo_path == NULL)
2105 err(1, "-r option");
2106 got_path_strip_trailing_slashes(repo_path);
2107 break;
2108 case 's':
2109 diff_staged = 1;
2110 break;
2111 default:
2112 usage_diff();
2113 /* NOTREACHED */
2117 argc -= optind;
2118 argv += optind;
2120 cwd = getcwd(NULL, 0);
2121 if (cwd == NULL) {
2122 error = got_error_from_errno("getcwd");
2123 goto done;
2125 error = got_worktree_open(&worktree, cwd);
2126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2127 goto done;
2128 if (argc <= 1) {
2129 if (worktree == NULL) {
2130 error = got_error(GOT_ERR_NOT_WORKTREE);
2131 goto done;
2133 if (repo_path)
2134 errx(1,
2135 "-r option can't be used when diffing a work tree");
2136 repo_path = strdup(got_worktree_get_repo_path(worktree));
2137 if (repo_path == NULL) {
2138 error = got_error_from_errno("strdup");
2139 goto done;
2141 if (argc == 1) {
2142 error = got_worktree_resolve_path(&path, worktree,
2143 argv[0]);
2144 if (error)
2145 goto done;
2146 } else {
2147 path = strdup("");
2148 if (path == NULL) {
2149 error = got_error_from_errno("strdup");
2150 goto done;
2153 } else if (argc == 2) {
2154 if (diff_staged)
2155 errx(1, "-s option can't be used when diffing "
2156 "objects in repository");
2157 id_str1 = argv[0];
2158 id_str2 = argv[1];
2159 if (worktree && repo_path == NULL) {
2160 repo_path =
2161 strdup(got_worktree_get_repo_path(worktree));
2162 if (repo_path == NULL) {
2163 error = got_error_from_errno("strdup");
2164 goto done;
2167 } else
2168 usage_diff();
2170 if (repo_path == NULL) {
2171 repo_path = getcwd(NULL, 0);
2172 if (repo_path == NULL)
2173 return got_error_from_errno("getcwd");
2176 error = got_repo_open(&repo, repo_path);
2177 free(repo_path);
2178 if (error != NULL)
2179 goto done;
2181 error = apply_unveil(got_repo_get_path(repo), 1,
2182 worktree ? got_worktree_get_root_path(worktree) : NULL);
2183 if (error)
2184 goto done;
2186 if (argc <= 1) {
2187 struct print_diff_arg arg;
2188 struct got_pathlist_head paths;
2189 char *id_str;
2191 TAILQ_INIT(&paths);
2193 error = got_object_id_str(&id_str,
2194 got_worktree_get_base_commit_id(worktree));
2195 if (error)
2196 goto done;
2197 arg.repo = repo;
2198 arg.worktree = worktree;
2199 arg.diff_context = diff_context;
2200 arg.id_str = id_str;
2201 arg.header_shown = 0;
2202 arg.diff_staged = diff_staged;
2204 error = got_pathlist_append(&paths, path, NULL);
2205 if (error)
2206 goto done;
2208 error = got_worktree_status(worktree, &paths, repo, print_diff,
2209 &arg, check_cancelled, NULL);
2210 free(id_str);
2211 got_pathlist_free(&paths);
2212 goto done;
2215 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2216 repo);
2217 if (error)
2218 goto done;
2220 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2221 repo);
2222 if (error)
2223 goto done;
2225 error = got_object_get_type(&type1, repo, id1);
2226 if (error)
2227 goto done;
2229 error = got_object_get_type(&type2, repo, id2);
2230 if (error)
2231 goto done;
2233 if (type1 != type2) {
2234 error = got_error(GOT_ERR_OBJ_TYPE);
2235 goto done;
2238 switch (type1) {
2239 case GOT_OBJ_TYPE_BLOB:
2240 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2241 diff_context, repo, stdout);
2242 break;
2243 case GOT_OBJ_TYPE_TREE:
2244 error = got_diff_objects_as_trees(id1, id2, "", "",
2245 diff_context, repo, stdout);
2246 break;
2247 case GOT_OBJ_TYPE_COMMIT:
2248 printf("diff %s %s\n", label1, label2);
2249 error = got_diff_objects_as_commits(id1, id2, diff_context,
2250 repo, stdout);
2251 break;
2252 default:
2253 error = got_error(GOT_ERR_OBJ_TYPE);
2256 done:
2257 free(label1);
2258 free(label2);
2259 free(id1);
2260 free(id2);
2261 free(path);
2262 if (worktree)
2263 got_worktree_close(worktree);
2264 if (repo) {
2265 const struct got_error *repo_error;
2266 repo_error = got_repo_close(repo);
2267 if (error == NULL)
2268 error = repo_error;
2270 return error;
2273 __dead static void
2274 usage_blame(void)
2276 fprintf(stderr,
2277 "usage: %s blame [-c commit] [-r repository-path] path\n",
2278 getprogname());
2279 exit(1);
2282 struct blame_line {
2283 int annotated;
2284 char *id_str;
2285 char *committer;
2286 char datebuf[9]; /* YY-MM-DD + NUL */
2289 struct blame_cb_args {
2290 struct blame_line *lines;
2291 int nlines;
2292 int nlines_prec;
2293 int lineno_cur;
2294 off_t *line_offsets;
2295 FILE *f;
2296 struct got_repository *repo;
2299 static const struct got_error *
2300 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2302 const struct got_error *err = NULL;
2303 struct blame_cb_args *a = arg;
2304 struct blame_line *bline;
2305 char *line = NULL;
2306 size_t linesize = 0;
2307 struct got_commit_object *commit = NULL;
2308 off_t offset;
2309 struct tm tm;
2310 time_t committer_time;
2312 if (nlines != a->nlines ||
2313 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2314 return got_error(GOT_ERR_RANGE);
2316 if (sigint_received)
2317 return got_error(GOT_ERR_ITER_COMPLETED);
2319 if (lineno == -1)
2320 return NULL; /* no change in this commit */
2322 /* Annotate this line. */
2323 bline = &a->lines[lineno - 1];
2324 if (bline->annotated)
2325 return NULL;
2326 err = got_object_id_str(&bline->id_str, id);
2327 if (err)
2328 return err;
2330 err = got_object_open_as_commit(&commit, a->repo, id);
2331 if (err)
2332 goto done;
2334 bline->committer = strdup(got_object_commit_get_committer(commit));
2335 if (bline->committer == NULL) {
2336 err = got_error_from_errno("strdup");
2337 goto done;
2340 committer_time = got_object_commit_get_committer_time(commit);
2341 if (localtime_r(&committer_time, &tm) == NULL)
2342 return got_error_from_errno("localtime_r");
2343 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2344 &tm) >= sizeof(bline->datebuf)) {
2345 err = got_error(GOT_ERR_NO_SPACE);
2346 goto done;
2348 bline->annotated = 1;
2350 /* Print lines annotated so far. */
2351 bline = &a->lines[a->lineno_cur - 1];
2352 if (!bline->annotated)
2353 goto done;
2355 offset = a->line_offsets[a->lineno_cur - 1];
2356 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2357 err = got_error_from_errno("fseeko");
2358 goto done;
2361 while (bline->annotated) {
2362 char *smallerthan, *at, *nl, *committer;
2363 size_t len;
2365 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2366 if (ferror(a->f))
2367 err = got_error_from_errno("getline");
2368 break;
2371 committer = bline->committer;
2372 smallerthan = strchr(committer, '<');
2373 if (smallerthan && smallerthan[1] != '\0')
2374 committer = smallerthan + 1;
2375 at = strchr(committer, '@');
2376 if (at)
2377 *at = '\0';
2378 len = strlen(committer);
2379 if (len >= 9)
2380 committer[8] = '\0';
2382 nl = strchr(line, '\n');
2383 if (nl)
2384 *nl = '\0';
2385 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2386 bline->id_str, bline->datebuf, committer, line);
2388 a->lineno_cur++;
2389 bline = &a->lines[a->lineno_cur - 1];
2391 done:
2392 if (commit)
2393 got_object_commit_close(commit);
2394 free(line);
2395 return err;
2398 static const struct got_error *
2399 cmd_blame(int argc, char *argv[])
2401 const struct got_error *error;
2402 struct got_repository *repo = NULL;
2403 struct got_worktree *worktree = NULL;
2404 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2405 struct got_object_id *obj_id = NULL;
2406 struct got_object_id *commit_id = NULL;
2407 struct got_blob_object *blob = NULL;
2408 char *commit_id_str = NULL;
2409 struct blame_cb_args bca;
2410 int ch, obj_type, i;
2411 size_t filesize;
2413 memset(&bca, 0, sizeof(bca));
2415 #ifndef PROFILE
2416 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2417 NULL) == -1)
2418 err(1, "pledge");
2419 #endif
2421 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2422 switch (ch) {
2423 case 'c':
2424 commit_id_str = optarg;
2425 break;
2426 case 'r':
2427 repo_path = realpath(optarg, NULL);
2428 if (repo_path == NULL)
2429 err(1, "-r option");
2430 got_path_strip_trailing_slashes(repo_path);
2431 break;
2432 default:
2433 usage_blame();
2434 /* NOTREACHED */
2438 argc -= optind;
2439 argv += optind;
2441 if (argc == 1)
2442 path = argv[0];
2443 else
2444 usage_blame();
2446 cwd = getcwd(NULL, 0);
2447 if (cwd == NULL) {
2448 error = got_error_from_errno("getcwd");
2449 goto done;
2451 if (repo_path == NULL) {
2452 error = got_worktree_open(&worktree, cwd);
2453 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2454 goto done;
2455 else
2456 error = NULL;
2457 if (worktree) {
2458 repo_path =
2459 strdup(got_worktree_get_repo_path(worktree));
2460 if (repo_path == NULL)
2461 error = got_error_from_errno("strdup");
2462 if (error)
2463 goto done;
2464 } else {
2465 repo_path = strdup(cwd);
2466 if (repo_path == NULL) {
2467 error = got_error_from_errno("strdup");
2468 goto done;
2473 error = got_repo_open(&repo, repo_path);
2474 if (error != NULL)
2475 goto done;
2477 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2478 if (error)
2479 goto done;
2481 if (worktree) {
2482 const char *prefix = got_worktree_get_path_prefix(worktree);
2483 char *p, *worktree_subdir = cwd +
2484 strlen(got_worktree_get_root_path(worktree));
2485 if (asprintf(&p, "%s%s%s%s%s",
2486 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2487 worktree_subdir, worktree_subdir[0] ? "/" : "",
2488 path) == -1) {
2489 error = got_error_from_errno("asprintf");
2490 goto done;
2492 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2493 free(p);
2494 } else {
2495 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2497 if (error)
2498 goto done;
2500 if (commit_id_str == NULL) {
2501 struct got_reference *head_ref;
2502 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2503 if (error != NULL)
2504 goto done;
2505 error = got_ref_resolve(&commit_id, repo, head_ref);
2506 got_ref_close(head_ref);
2507 if (error != NULL)
2508 goto done;
2509 } else {
2510 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2511 if (error)
2512 goto done;
2515 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2516 if (error)
2517 goto done;
2518 if (obj_id == NULL) {
2519 error = got_error(GOT_ERR_NO_OBJ);
2520 goto done;
2523 error = got_object_get_type(&obj_type, repo, obj_id);
2524 if (error)
2525 goto done;
2527 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2528 error = got_error(GOT_ERR_OBJ_TYPE);
2529 goto done;
2532 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2533 if (error)
2534 goto done;
2535 bca.f = got_opentemp();
2536 if (bca.f == NULL) {
2537 error = got_error_from_errno("got_opentemp");
2538 goto done;
2540 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2541 &bca.line_offsets, bca.f, blob);
2542 if (error || bca.nlines == 0)
2543 goto done;
2545 /* Don't include \n at EOF in the blame line count. */
2546 if (bca.line_offsets[bca.nlines - 1] == filesize)
2547 bca.nlines--;
2549 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2550 if (bca.lines == NULL) {
2551 error = got_error_from_errno("calloc");
2552 goto done;
2554 bca.lineno_cur = 1;
2555 bca.nlines_prec = 0;
2556 i = bca.nlines;
2557 while (i > 0) {
2558 i /= 10;
2559 bca.nlines_prec++;
2561 bca.repo = repo;
2563 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2564 check_cancelled, NULL);
2565 if (error)
2566 goto done;
2567 done:
2568 free(in_repo_path);
2569 free(repo_path);
2570 free(cwd);
2571 free(commit_id);
2572 free(obj_id);
2573 if (blob)
2574 got_object_blob_close(blob);
2575 if (worktree)
2576 got_worktree_close(worktree);
2577 if (repo) {
2578 const struct got_error *repo_error;
2579 repo_error = got_repo_close(repo);
2580 if (error == NULL)
2581 error = repo_error;
2583 for (i = 0; i < bca.nlines; i++) {
2584 struct blame_line *bline = &bca.lines[i];
2585 free(bline->id_str);
2586 free(bline->committer);
2588 free(bca.lines);
2589 free(bca.line_offsets);
2590 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2591 error = got_error_from_errno("fclose");
2592 return error;
2595 __dead static void
2596 usage_tree(void)
2598 fprintf(stderr,
2599 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2600 getprogname());
2601 exit(1);
2604 static void
2605 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2606 const char *root_path)
2608 int is_root_path = (strcmp(path, root_path) == 0);
2609 const char *modestr = "";
2611 path += strlen(root_path);
2612 while (path[0] == '/')
2613 path++;
2615 if (got_object_tree_entry_is_submodule(te))
2616 modestr = "$";
2617 else if (S_ISLNK(te->mode))
2618 modestr = "@";
2619 else if (S_ISDIR(te->mode))
2620 modestr = "/";
2621 else if (te->mode & S_IXUSR)
2622 modestr = "*";
2624 printf("%s%s%s%s%s\n", id ? id : "", path,
2625 is_root_path ? "" : "/", te->name, modestr);
2628 static const struct got_error *
2629 print_tree(const char *path, struct got_object_id *commit_id,
2630 int show_ids, int recurse, const char *root_path,
2631 struct got_repository *repo)
2633 const struct got_error *err = NULL;
2634 struct got_object_id *tree_id = NULL;
2635 struct got_tree_object *tree = NULL;
2636 const struct got_tree_entries *entries;
2637 struct got_tree_entry *te;
2639 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2640 if (err)
2641 goto done;
2643 err = got_object_open_as_tree(&tree, repo, tree_id);
2644 if (err)
2645 goto done;
2646 entries = got_object_tree_get_entries(tree);
2647 te = SIMPLEQ_FIRST(&entries->head);
2648 while (te) {
2649 char *id = NULL;
2651 if (sigint_received || sigpipe_received)
2652 break;
2654 if (show_ids) {
2655 char *id_str;
2656 err = got_object_id_str(&id_str, te->id);
2657 if (err)
2658 goto done;
2659 if (asprintf(&id, "%s ", id_str) == -1) {
2660 err = got_error_from_errno("asprintf");
2661 free(id_str);
2662 goto done;
2664 free(id_str);
2666 print_entry(te, id, path, root_path);
2667 free(id);
2669 if (recurse && S_ISDIR(te->mode)) {
2670 char *child_path;
2671 if (asprintf(&child_path, "%s%s%s", path,
2672 path[0] == '/' && path[1] == '\0' ? "" : "/",
2673 te->name) == -1) {
2674 err = got_error_from_errno("asprintf");
2675 goto done;
2677 err = print_tree(child_path, commit_id, show_ids, 1,
2678 root_path, repo);
2679 free(child_path);
2680 if (err)
2681 goto done;
2684 te = SIMPLEQ_NEXT(te, entry);
2686 done:
2687 if (tree)
2688 got_object_tree_close(tree);
2689 free(tree_id);
2690 return err;
2693 static const struct got_error *
2694 cmd_tree(int argc, char *argv[])
2696 const struct got_error *error;
2697 struct got_repository *repo = NULL;
2698 struct got_worktree *worktree = NULL;
2699 const char *path;
2700 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2701 struct got_object_id *commit_id = NULL;
2702 char *commit_id_str = NULL;
2703 int show_ids = 0, recurse = 0;
2704 int ch;
2706 #ifndef PROFILE
2707 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2708 NULL) == -1)
2709 err(1, "pledge");
2710 #endif
2712 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2713 switch (ch) {
2714 case 'c':
2715 commit_id_str = optarg;
2716 break;
2717 case 'r':
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2722 break;
2723 case 'i':
2724 show_ids = 1;
2725 break;
2726 case 'R':
2727 recurse = 1;
2728 break;
2729 default:
2730 usage_tree();
2731 /* NOTREACHED */
2735 argc -= optind;
2736 argv += optind;
2738 if (argc == 1)
2739 path = argv[0];
2740 else if (argc > 1)
2741 usage_tree();
2742 else
2743 path = NULL;
2745 cwd = getcwd(NULL, 0);
2746 if (cwd == NULL) {
2747 error = got_error_from_errno("getcwd");
2748 goto done;
2750 if (repo_path == NULL) {
2751 error = got_worktree_open(&worktree, cwd);
2752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2753 goto done;
2754 else
2755 error = NULL;
2756 if (worktree) {
2757 repo_path =
2758 strdup(got_worktree_get_repo_path(worktree));
2759 if (repo_path == NULL)
2760 error = got_error_from_errno("strdup");
2761 if (error)
2762 goto done;
2763 } else {
2764 repo_path = strdup(cwd);
2765 if (repo_path == NULL) {
2766 error = got_error_from_errno("strdup");
2767 goto done;
2772 error = got_repo_open(&repo, repo_path);
2773 if (error != NULL)
2774 goto done;
2776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2777 if (error)
2778 goto done;
2780 if (path == NULL) {
2781 if (worktree) {
2782 char *p, *worktree_subdir = cwd +
2783 strlen(got_worktree_get_root_path(worktree));
2784 if (asprintf(&p, "%s/%s",
2785 got_worktree_get_path_prefix(worktree),
2786 worktree_subdir) == -1) {
2787 error = got_error_from_errno("asprintf");
2788 goto done;
2790 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2791 free(p);
2792 if (error)
2793 goto done;
2794 } else
2795 path = "/";
2797 if (in_repo_path == NULL) {
2798 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2799 if (error != NULL)
2800 goto done;
2803 if (commit_id_str == NULL) {
2804 struct got_reference *head_ref;
2805 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2806 if (error != NULL)
2807 goto done;
2808 error = got_ref_resolve(&commit_id, repo, head_ref);
2809 got_ref_close(head_ref);
2810 if (error != NULL)
2811 goto done;
2812 } else {
2813 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2814 if (error)
2815 goto done;
2818 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2819 in_repo_path, repo);
2820 done:
2821 free(in_repo_path);
2822 free(repo_path);
2823 free(cwd);
2824 free(commit_id);
2825 if (worktree)
2826 got_worktree_close(worktree);
2827 if (repo) {
2828 const struct got_error *repo_error;
2829 repo_error = got_repo_close(repo);
2830 if (error == NULL)
2831 error = repo_error;
2833 return error;
2836 __dead static void
2837 usage_status(void)
2839 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2840 exit(1);
2843 static const struct got_error *
2844 print_status(void *arg, unsigned char status, unsigned char staged_status,
2845 const char *path, struct got_object_id *blob_id,
2846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2848 if (status == staged_status && (status == GOT_STATUS_DELETE))
2849 status = GOT_STATUS_NO_CHANGE;
2850 printf("%c%c %s\n", status, staged_status, path);
2851 return NULL;
2854 static const struct got_error *
2855 cmd_status(int argc, char *argv[])
2857 const struct got_error *error = NULL;
2858 struct got_repository *repo = NULL;
2859 struct got_worktree *worktree = NULL;
2860 char *cwd = NULL;
2861 struct got_pathlist_head paths;
2862 struct got_pathlist_entry *pe;
2863 int ch;
2865 TAILQ_INIT(&paths);
2867 while ((ch = getopt(argc, argv, "")) != -1) {
2868 switch (ch) {
2869 default:
2870 usage_status();
2871 /* NOTREACHED */
2875 argc -= optind;
2876 argv += optind;
2878 #ifndef PROFILE
2879 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2880 NULL) == -1)
2881 err(1, "pledge");
2882 #endif
2883 cwd = getcwd(NULL, 0);
2884 if (cwd == NULL) {
2885 error = got_error_from_errno("getcwd");
2886 goto done;
2889 error = got_worktree_open(&worktree, cwd);
2890 if (error != NULL)
2891 goto done;
2893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2894 if (error != NULL)
2895 goto done;
2897 error = apply_unveil(got_repo_get_path(repo), 1,
2898 got_worktree_get_root_path(worktree));
2899 if (error)
2900 goto done;
2902 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2903 if (error)
2904 goto done;
2906 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2907 check_cancelled, NULL);
2908 done:
2909 TAILQ_FOREACH(pe, &paths, entry)
2910 free((char *)pe->path);
2911 got_pathlist_free(&paths);
2912 free(cwd);
2913 return error;
2916 __dead static void
2917 usage_ref(void)
2919 fprintf(stderr,
2920 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2921 getprogname());
2922 exit(1);
2925 static const struct got_error *
2926 list_refs(struct got_repository *repo)
2928 static const struct got_error *err = NULL;
2929 struct got_reflist_head refs;
2930 struct got_reflist_entry *re;
2932 SIMPLEQ_INIT(&refs);
2933 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2934 if (err)
2935 return err;
2937 SIMPLEQ_FOREACH(re, &refs, entry) {
2938 char *refstr;
2939 refstr = got_ref_to_str(re->ref);
2940 if (refstr == NULL)
2941 return got_error_from_errno("got_ref_to_str");
2942 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2943 free(refstr);
2946 got_ref_list_free(&refs);
2947 return NULL;
2950 static const struct got_error *
2951 delete_ref(struct got_repository *repo, const char *refname)
2953 const struct got_error *err = NULL;
2954 struct got_reference *ref;
2956 err = got_ref_open(&ref, repo, refname, 0);
2957 if (err)
2958 return err;
2960 err = got_ref_delete(ref, repo);
2961 got_ref_close(ref);
2962 return err;
2965 static const struct got_error *
2966 add_ref(struct got_repository *repo, const char *refname, const char *target)
2968 const struct got_error *err = NULL;
2969 struct got_object_id *id;
2970 struct got_reference *ref = NULL;
2973 * Don't let the user create a reference named '-'.
2974 * While technically a valid reference name, this case is usually
2975 * an unintended typo.
2977 if (refname[0] == '-' && refname[1] == '\0')
2978 return got_error(GOT_ERR_BAD_REF_NAME);
2980 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2981 repo);
2982 if (err) {
2983 struct got_reference *target_ref;
2985 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2986 return err;
2987 err = got_ref_open(&target_ref, repo, target, 0);
2988 if (err)
2989 return err;
2990 err = got_ref_resolve(&id, repo, target_ref);
2991 got_ref_close(target_ref);
2992 if (err)
2993 return err;
2996 err = got_ref_alloc(&ref, refname, id);
2997 if (err)
2998 goto done;
3000 err = got_ref_write(ref, repo);
3001 done:
3002 if (ref)
3003 got_ref_close(ref);
3004 free(id);
3005 return err;
3008 static const struct got_error *
3009 add_symref(struct got_repository *repo, const char *refname, const char *target)
3011 const struct got_error *err = NULL;
3012 struct got_reference *ref = NULL;
3013 struct got_reference *target_ref = NULL;
3016 * Don't let the user create a reference named '-'.
3017 * While technically a valid reference name, this case is usually
3018 * an unintended typo.
3020 if (refname[0] == '-' && refname[1] == '\0')
3021 return got_error(GOT_ERR_BAD_REF_NAME);
3023 err = got_ref_open(&target_ref, repo, target, 0);
3024 if (err)
3025 return err;
3027 err = got_ref_alloc_symref(&ref, refname, target_ref);
3028 if (err)
3029 goto done;
3031 err = got_ref_write(ref, repo);
3032 done:
3033 if (target_ref)
3034 got_ref_close(target_ref);
3035 if (ref)
3036 got_ref_close(ref);
3037 return err;
3040 static const struct got_error *
3041 cmd_ref(int argc, char *argv[])
3043 const struct got_error *error = NULL;
3044 struct got_repository *repo = NULL;
3045 struct got_worktree *worktree = NULL;
3046 char *cwd = NULL, *repo_path = NULL;
3047 int ch, do_list = 0, create_symref = 0;
3048 const char *delref = NULL;
3050 /* TODO: Add -s option for adding symbolic references. */
3051 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3052 switch (ch) {
3053 case 'd':
3054 delref = optarg;
3055 break;
3056 case 'r':
3057 repo_path = realpath(optarg, NULL);
3058 if (repo_path == NULL)
3059 err(1, "-r option");
3060 got_path_strip_trailing_slashes(repo_path);
3061 break;
3062 case 'l':
3063 do_list = 1;
3064 break;
3065 case 's':
3066 create_symref = 1;
3067 break;
3068 default:
3069 usage_ref();
3070 /* NOTREACHED */
3074 if (do_list && delref)
3075 errx(1, "-l and -d options are mutually exclusive\n");
3077 argc -= optind;
3078 argv += optind;
3080 if (do_list || delref) {
3081 if (create_symref)
3082 errx(1, "-s option cannot be used together with the "
3083 "-l or -d options");
3084 if (argc > 0)
3085 usage_ref();
3086 } else if (argc != 2)
3087 usage_ref();
3089 #ifndef PROFILE
3090 if (do_list) {
3091 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3092 NULL) == -1)
3093 err(1, "pledge");
3094 } else {
3095 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3096 "sendfd unveil", NULL) == -1)
3097 err(1, "pledge");
3099 #endif
3100 cwd = getcwd(NULL, 0);
3101 if (cwd == NULL) {
3102 error = got_error_from_errno("getcwd");
3103 goto done;
3106 if (repo_path == NULL) {
3107 error = got_worktree_open(&worktree, cwd);
3108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3109 goto done;
3110 else
3111 error = NULL;
3112 if (worktree) {
3113 repo_path =
3114 strdup(got_worktree_get_repo_path(worktree));
3115 if (repo_path == NULL)
3116 error = got_error_from_errno("strdup");
3117 if (error)
3118 goto done;
3119 } else {
3120 repo_path = strdup(cwd);
3121 if (repo_path == NULL) {
3122 error = got_error_from_errno("strdup");
3123 goto done;
3128 error = got_repo_open(&repo, repo_path);
3129 if (error != NULL)
3130 goto done;
3132 error = apply_unveil(got_repo_get_path(repo), do_list,
3133 worktree ? got_worktree_get_root_path(worktree) : NULL);
3134 if (error)
3135 goto done;
3137 if (do_list)
3138 error = list_refs(repo);
3139 else if (delref)
3140 error = delete_ref(repo, delref);
3141 else if (create_symref)
3142 error = add_symref(repo, argv[0], argv[1]);
3143 else
3144 error = add_ref(repo, argv[0], argv[1]);
3145 done:
3146 if (repo)
3147 got_repo_close(repo);
3148 if (worktree)
3149 got_worktree_close(worktree);
3150 free(cwd);
3151 free(repo_path);
3152 return error;
3155 __dead static void
3156 usage_branch(void)
3158 fprintf(stderr,
3159 "usage: %s branch [-r repository] -l | -d name | "
3160 "name [commit]\n", getprogname());
3161 exit(1);
3164 static const struct got_error *
3165 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3167 static const struct got_error *err = NULL;
3168 struct got_reflist_head refs;
3169 struct got_reflist_entry *re;
3171 SIMPLEQ_INIT(&refs);
3173 err = got_ref_list(&refs, repo, "refs/heads",
3174 got_ref_cmp_by_name, NULL);
3175 if (err)
3176 return err;
3178 SIMPLEQ_FOREACH(re, &refs, entry) {
3179 const char *refname, *marker = " ";
3180 char *refstr;
3181 refname = got_ref_get_name(re->ref);
3182 if (worktree && strcmp(refname,
3183 got_worktree_get_head_ref_name(worktree)) == 0) {
3184 struct got_object_id *id = NULL;
3185 err = got_ref_resolve(&id, repo, re->ref);
3186 if (err)
3187 return err;
3188 if (got_object_id_cmp(id,
3189 got_worktree_get_base_commit_id(worktree)) == 0)
3190 marker = "* ";
3191 else
3192 marker = "~ ";
3193 free(id);
3195 refname += strlen("refs/heads/");
3196 refstr = got_ref_to_str(re->ref);
3197 if (refstr == NULL)
3198 return got_error_from_errno("got_ref_to_str");
3199 printf("%s%s: %s\n", marker, refname, refstr);
3200 free(refstr);
3203 got_ref_list_free(&refs);
3204 return NULL;
3207 static const struct got_error *
3208 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3209 const char *branch_name)
3211 const struct got_error *err = NULL;
3212 struct got_reference *ref = NULL;
3213 char *refname;
3215 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3216 return got_error_from_errno("asprintf");
3218 err = got_ref_open(&ref, repo, refname, 0);
3219 if (err)
3220 goto done;
3222 if (worktree &&
3223 strcmp(got_worktree_get_head_ref_name(worktree),
3224 got_ref_get_name(ref)) == 0) {
3225 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3226 "will not delete this work tree's current branch");
3227 goto done;
3230 err = got_ref_delete(ref, repo);
3231 done:
3232 if (ref)
3233 got_ref_close(ref);
3234 free(refname);
3235 return err;
3238 static const struct got_error *
3239 add_branch(struct got_repository *repo, const char *branch_name,
3240 const char *base_branch)
3242 const struct got_error *err = NULL;
3243 struct got_object_id *id = NULL;
3244 char *label;
3245 struct got_reference *ref = NULL;
3246 char *base_refname = NULL, *refname = NULL;
3249 * Don't let the user create a branch named '-'.
3250 * While technically a valid reference name, this case is usually
3251 * an unintended typo.
3253 if (branch_name[0] == '-' && branch_name[1] == '\0')
3254 return got_error(GOT_ERR_BAD_REF_NAME);
3256 err = match_object_id(&id, &label, base_branch,
3257 GOT_OBJ_TYPE_COMMIT, 1, repo);
3258 if (err)
3259 return err;
3261 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3262 err = got_error_from_errno("asprintf");
3263 goto done;
3266 err = got_ref_open(&ref, repo, refname, 0);
3267 if (err == NULL) {
3268 err = got_error(GOT_ERR_BRANCH_EXISTS);
3269 goto done;
3270 } else if (err->code != GOT_ERR_NOT_REF)
3271 goto done;
3273 err = got_ref_alloc(&ref, refname, id);
3274 if (err)
3275 goto done;
3277 err = got_ref_write(ref, repo);
3278 done:
3279 if (ref)
3280 got_ref_close(ref);
3281 free(id);
3282 free(base_refname);
3283 free(refname);
3284 return err;
3287 static const struct got_error *
3288 cmd_branch(int argc, char *argv[])
3290 const struct got_error *error = NULL;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3293 char *cwd = NULL, *repo_path = NULL;
3294 int ch, do_list = 0;
3295 const char *delref = NULL;
3297 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3298 switch (ch) {
3299 case 'd':
3300 delref = optarg;
3301 break;
3302 case 'r':
3303 repo_path = realpath(optarg, NULL);
3304 if (repo_path == NULL)
3305 err(1, "-r option");
3306 got_path_strip_trailing_slashes(repo_path);
3307 break;
3308 case 'l':
3309 do_list = 1;
3310 break;
3311 default:
3312 usage_branch();
3313 /* NOTREACHED */
3317 if (do_list && delref)
3318 errx(1, "-l and -d options are mutually exclusive\n");
3320 argc -= optind;
3321 argv += optind;
3323 if (do_list || delref) {
3324 if (argc > 0)
3325 usage_branch();
3326 } else if (argc < 1 || argc > 2)
3327 usage_branch();
3329 #ifndef PROFILE
3330 if (do_list) {
3331 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3332 NULL) == -1)
3333 err(1, "pledge");
3334 } else {
3335 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3336 "sendfd unveil", NULL) == -1)
3337 err(1, "pledge");
3339 #endif
3340 cwd = getcwd(NULL, 0);
3341 if (cwd == NULL) {
3342 error = got_error_from_errno("getcwd");
3343 goto done;
3346 if (repo_path == NULL) {
3347 error = got_worktree_open(&worktree, cwd);
3348 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3349 goto done;
3350 else
3351 error = NULL;
3352 if (worktree) {
3353 repo_path =
3354 strdup(got_worktree_get_repo_path(worktree));
3355 if (repo_path == NULL)
3356 error = got_error_from_errno("strdup");
3357 if (error)
3358 goto done;
3359 } else {
3360 repo_path = strdup(cwd);
3361 if (repo_path == NULL) {
3362 error = got_error_from_errno("strdup");
3363 goto done;
3368 error = got_repo_open(&repo, repo_path);
3369 if (error != NULL)
3370 goto done;
3372 error = apply_unveil(got_repo_get_path(repo), do_list,
3373 worktree ? got_worktree_get_root_path(worktree) : NULL);
3374 if (error)
3375 goto done;
3377 if (do_list)
3378 error = list_branches(repo, worktree);
3379 else if (delref)
3380 error = delete_branch(repo, worktree, delref);
3381 else {
3382 const char *base_branch;
3383 if (argc == 1) {
3384 base_branch = worktree ?
3385 got_worktree_get_head_ref_name(worktree) :
3386 GOT_REF_HEAD;
3387 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3388 base_branch += 11;
3389 } else
3390 base_branch = argv[1];
3391 error = add_branch(repo, argv[0], base_branch);
3393 done:
3394 if (repo)
3395 got_repo_close(repo);
3396 if (worktree)
3397 got_worktree_close(worktree);
3398 free(cwd);
3399 free(repo_path);
3400 return error;
3404 __dead static void
3405 usage_tag(void)
3407 fprintf(stderr,
3408 "usage: %s tag [-r repository] | -l | "
3409 "[-m message] name [commit]\n", getprogname());
3410 exit(1);
3413 #if 0
3414 static const struct got_error *
3415 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3417 const struct got_error *err = NULL;
3418 struct got_reflist_entry *re, *se, *new;
3419 struct got_object_id *re_id, *se_id;
3420 struct got_tag_object *re_tag, *se_tag;
3421 time_t re_time, se_time;
3423 SIMPLEQ_FOREACH(re, tags, entry) {
3424 se = SIMPLEQ_FIRST(sorted);
3425 if (se == NULL) {
3426 err = got_reflist_entry_dup(&new, re);
3427 if (err)
3428 return err;
3429 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3430 continue;
3431 } else {
3432 err = got_ref_resolve(&re_id, repo, re->ref);
3433 if (err)
3434 break;
3435 err = got_object_open_as_tag(&re_tag, repo, re_id);
3436 free(re_id);
3437 if (err)
3438 break;
3439 re_time = got_object_tag_get_tagger_time(re_tag);
3440 got_object_tag_close(re_tag);
3443 while (se) {
3444 err = got_ref_resolve(&se_id, repo, re->ref);
3445 if (err)
3446 break;
3447 err = got_object_open_as_tag(&se_tag, repo, se_id);
3448 free(se_id);
3449 if (err)
3450 break;
3451 se_time = got_object_tag_get_tagger_time(se_tag);
3452 got_object_tag_close(se_tag);
3454 if (se_time > re_time) {
3455 err = got_reflist_entry_dup(&new, re);
3456 if (err)
3457 return err;
3458 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3459 break;
3461 se = SIMPLEQ_NEXT(se, entry);
3462 continue;
3465 done:
3466 return err;
3468 #endif
3470 static const struct got_error *
3471 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3472 struct got_reference *ref2)
3474 const struct got_error *err = NULL;
3475 struct got_repository *repo = arg;
3476 struct got_object_id *id1, *id2 = NULL;
3477 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3478 time_t time1, time2;
3480 *cmp = 0;
3482 err = got_ref_resolve(&id1, repo, ref1);
3483 if (err)
3484 return err;
3485 err = got_object_open_as_tag(&tag1, repo, id1);
3486 if (err)
3487 goto done;
3489 err = got_ref_resolve(&id2, repo, ref2);
3490 if (err)
3491 goto done;
3492 err = got_object_open_as_tag(&tag2, repo, id2);
3493 if (err)
3494 goto done;
3496 time1 = got_object_tag_get_tagger_time(tag1);
3497 time2 = got_object_tag_get_tagger_time(tag2);
3499 /* Put latest tags first. */
3500 if (time1 < time2)
3501 *cmp = 1;
3502 else if (time1 > time2)
3503 *cmp = -1;
3504 else
3505 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3506 done:
3507 free(id1);
3508 free(id2);
3509 if (tag1)
3510 got_object_tag_close(tag1);
3511 if (tag2)
3512 got_object_tag_close(tag2);
3513 return err;
3516 static const struct got_error *
3517 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3519 static const struct got_error *err = NULL;
3520 struct got_reflist_head refs;
3521 struct got_reflist_entry *re;
3523 SIMPLEQ_INIT(&refs);
3525 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3526 if (err)
3527 return err;
3529 SIMPLEQ_FOREACH(re, &refs, entry) {
3530 const char *refname;
3531 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3532 char datebuf[26];
3533 time_t tagger_time;
3534 struct got_object_id *id;
3535 struct got_tag_object *tag;
3537 refname = got_ref_get_name(re->ref);
3538 if (strncmp(refname, "refs/tags/", 10) != 0)
3539 continue;
3540 refname += 10;
3541 refstr = got_ref_to_str(re->ref);
3542 if (refstr == NULL) {
3543 err = got_error_from_errno("got_ref_to_str");
3544 break;
3546 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3547 free(refstr);
3549 err = got_ref_resolve(&id, repo, re->ref);
3550 if (err)
3551 break;
3552 err = got_object_open_as_tag(&tag, repo, id);
3553 free(id);
3554 if (err)
3555 break;
3556 printf("from: %s\n", got_object_tag_get_tagger(tag));
3557 tagger_time = got_object_tag_get_tagger_time(tag);
3558 datestr = get_datestr(&tagger_time, datebuf);
3559 if (datestr)
3560 printf("date: %s UTC\n", datestr);
3561 err = got_object_id_str(&id_str,
3562 got_object_tag_get_object_id(tag));
3563 if (err)
3564 break;
3565 switch (got_object_tag_get_object_type(tag)) {
3566 case GOT_OBJ_TYPE_BLOB:
3567 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3568 break;
3569 case GOT_OBJ_TYPE_TREE:
3570 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3571 break;
3572 case GOT_OBJ_TYPE_COMMIT:
3573 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3574 break;
3575 case GOT_OBJ_TYPE_TAG:
3576 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3577 break;
3578 default:
3579 break;
3581 free(id_str);
3582 tagmsg0 = strdup(got_object_tag_get_message(tag));
3583 got_object_tag_close(tag);
3584 if (tagmsg0 == NULL) {
3585 err = got_error_from_errno("strdup");
3586 break;
3589 tagmsg = tagmsg0;
3590 do {
3591 line = strsep(&tagmsg, "\n");
3592 if (line)
3593 printf(" %s\n", line);
3594 } while (line);
3595 free(tagmsg0);
3598 got_ref_list_free(&refs);
3599 return NULL;
3602 static const struct got_error *
3603 get_tag_message(char **tagmsg, const char *commit_id_str,
3604 const char *tag_name, const char *repo_path)
3606 const struct got_error *err = NULL;
3607 char *template = NULL, *initial_content = NULL;
3608 char *tagmsg_path = NULL, *editor = NULL;
3609 int fd = -1;
3611 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3612 err = got_error_from_errno("asprintf");
3613 goto done;
3616 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3617 commit_id_str, tag_name) == -1) {
3618 err = got_error_from_errno("asprintf");
3619 goto done;
3622 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3623 if (err)
3624 goto done;
3626 dprintf(fd, initial_content);
3627 close(fd);
3629 err = get_editor(&editor);
3630 if (err)
3631 goto done;
3632 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3633 done:
3634 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3635 unlink(tagmsg_path);
3636 free(tagmsg_path);
3637 tagmsg_path = NULL;
3639 free(initial_content);
3640 free(template);
3641 free(editor);
3643 /* Editor is done; we can now apply unveil(2) */
3644 if (err == NULL) {
3645 err = apply_unveil(repo_path, 0, NULL);
3646 if (err) {
3647 free(*tagmsg);
3648 *tagmsg = NULL;
3651 return err;
3654 static const struct got_error *
3655 add_tag(struct got_repository *repo, const char *tag_name,
3656 const char *commit_arg, const char *tagmsg_arg)
3658 const struct got_error *err = NULL;
3659 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3660 char *label = NULL, *commit_id_str = NULL;
3661 struct got_reference *ref = NULL;
3662 char *refname = NULL, *tagmsg = NULL;
3663 const char *tagger;
3666 * Don't let the user create a tag named '-'.
3667 * While technically a valid reference name, this case is usually
3668 * an unintended typo.
3670 if (tag_name[0] == '-' && tag_name[1] == '\0')
3671 return got_error(GOT_ERR_BAD_REF_NAME);
3673 err = get_author(&tagger);
3674 if (err)
3675 return err;
3677 err = match_object_id(&commit_id, &label, commit_arg,
3678 GOT_OBJ_TYPE_COMMIT, 1, repo);
3679 if (err)
3680 goto done;
3682 err = got_object_id_str(&commit_id_str, commit_id);
3683 if (err)
3684 goto done;
3686 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3687 refname = strdup(tag_name);
3688 if (refname == NULL) {
3689 err = got_error_from_errno("strdup");
3690 goto done;
3692 tag_name += 10;
3693 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3694 err = got_error_from_errno("asprintf");
3695 goto done;
3698 err = got_ref_open(&ref, repo, refname, 0);
3699 if (err == NULL) {
3700 err = got_error(GOT_ERR_TAG_EXISTS);
3701 goto done;
3702 } else if (err->code != GOT_ERR_NOT_REF)
3703 goto done;
3705 if (tagmsg_arg == NULL) {
3706 err = get_tag_message(&tagmsg, commit_id_str,
3707 tag_name, got_repo_get_path(repo));
3708 if (err)
3709 goto done;
3712 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3713 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3714 if (err)
3715 goto done;
3717 err = got_ref_alloc(&ref, refname, tag_id);
3718 if (err)
3719 goto done;
3721 err = got_ref_write(ref, repo);
3723 if (err == NULL) {
3724 char *tag_id_str;
3725 err = got_object_id_str(&tag_id_str, tag_id);
3726 printf("Created tag %s\n", tag_id_str);
3727 free(tag_id_str);
3729 done:
3730 if (ref)
3731 got_ref_close(ref);
3732 free(commit_id);
3733 free(commit_id_str);
3734 free(refname);
3735 free(tagmsg);
3736 return err;
3739 static const struct got_error *
3740 cmd_tag(int argc, char *argv[])
3742 const struct got_error *error = NULL;
3743 struct got_repository *repo = NULL;
3744 struct got_worktree *worktree = NULL;
3745 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3746 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3747 int ch, do_list = 0;
3749 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3750 switch (ch) {
3751 case 'm':
3752 tagmsg = optarg;
3753 break;
3754 case 'r':
3755 repo_path = realpath(optarg, NULL);
3756 if (repo_path == NULL)
3757 err(1, "-r option");
3758 got_path_strip_trailing_slashes(repo_path);
3759 break;
3760 case 'l':
3761 do_list = 1;
3762 break;
3763 default:
3764 usage_tag();
3765 /* NOTREACHED */
3769 argc -= optind;
3770 argv += optind;
3772 if (do_list) {
3773 if (tagmsg)
3774 errx(1, "-l and -m options are mutually exclusive\n");
3775 if (argc > 0)
3776 usage_tag();
3777 } else if (argc < 1 || argc > 2)
3778 usage_tag();
3779 else if (argc > 1)
3780 commit_id_arg = argv[1];
3781 tag_name = argv[0];
3783 #ifndef PROFILE
3784 if (do_list) {
3785 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3786 NULL) == -1)
3787 err(1, "pledge");
3788 } else {
3789 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3790 "sendfd unveil", NULL) == -1)
3791 err(1, "pledge");
3793 #endif
3794 cwd = getcwd(NULL, 0);
3795 if (cwd == NULL) {
3796 error = got_error_from_errno("getcwd");
3797 goto done;
3800 if (repo_path == NULL) {
3801 error = got_worktree_open(&worktree, cwd);
3802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3803 goto done;
3804 else
3805 error = NULL;
3806 if (worktree) {
3807 repo_path =
3808 strdup(got_worktree_get_repo_path(worktree));
3809 if (repo_path == NULL)
3810 error = got_error_from_errno("strdup");
3811 if (error)
3812 goto done;
3813 } else {
3814 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3817 goto done;
3822 error = got_repo_open(&repo, repo_path);
3823 if (error != NULL)
3824 goto done;
3827 if (do_list) {
3828 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3829 if (error)
3830 goto done;
3831 error = list_tags(repo, worktree);
3832 } else {
3833 if (tagmsg) {
3834 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3835 if (error)
3836 goto done;
3839 if (commit_id_arg == NULL) {
3840 struct got_reference *head_ref;
3841 struct got_object_id *commit_id;
3842 error = got_ref_open(&head_ref, repo,
3843 worktree ? got_worktree_get_head_ref_name(worktree)
3844 : GOT_REF_HEAD, 0);
3845 if (error)
3846 goto done;
3847 error = got_ref_resolve(&commit_id, repo, head_ref);
3848 got_ref_close(head_ref);
3849 if (error)
3850 goto done;
3851 error = got_object_id_str(&commit_id_str, commit_id);
3852 free(commit_id);
3853 if (error)
3854 goto done;
3857 error = add_tag(repo, tag_name,
3858 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3860 done:
3861 if (repo)
3862 got_repo_close(repo);
3863 if (worktree)
3864 got_worktree_close(worktree);
3865 free(cwd);
3866 free(repo_path);
3867 free(commit_id_str);
3868 return error;
3871 __dead static void
3872 usage_add(void)
3874 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3875 exit(1);
3878 static const struct got_error *
3879 cmd_add(int argc, char *argv[])
3881 const struct got_error *error = NULL;
3882 struct got_repository *repo = NULL;
3883 struct got_worktree *worktree = NULL;
3884 char *cwd = NULL;
3885 struct got_pathlist_head paths;
3886 struct got_pathlist_entry *pe;
3887 int ch;
3889 TAILQ_INIT(&paths);
3891 while ((ch = getopt(argc, argv, "")) != -1) {
3892 switch (ch) {
3893 default:
3894 usage_add();
3895 /* NOTREACHED */
3899 argc -= optind;
3900 argv += optind;
3902 #ifndef PROFILE
3903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3904 NULL) == -1)
3905 err(1, "pledge");
3906 #endif
3907 if (argc < 1)
3908 usage_add();
3910 cwd = getcwd(NULL, 0);
3911 if (cwd == NULL) {
3912 error = got_error_from_errno("getcwd");
3913 goto done;
3916 error = got_worktree_open(&worktree, cwd);
3917 if (error)
3918 goto done;
3920 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3921 if (error != NULL)
3922 goto done;
3924 error = apply_unveil(got_repo_get_path(repo), 1,
3925 got_worktree_get_root_path(worktree));
3926 if (error)
3927 goto done;
3929 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3930 if (error)
3931 goto done;
3933 error = got_worktree_schedule_add(worktree, &paths, print_status,
3934 NULL, repo);
3935 done:
3936 if (repo)
3937 got_repo_close(repo);
3938 if (worktree)
3939 got_worktree_close(worktree);
3940 TAILQ_FOREACH(pe, &paths, entry)
3941 free((char *)pe->path);
3942 got_pathlist_free(&paths);
3943 free(cwd);
3944 return error;
3947 __dead static void
3948 usage_remove(void)
3950 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3951 exit(1);
3954 static const struct got_error *
3955 print_remove_status(void *arg, unsigned char status,
3956 unsigned char staged_status, const char *path,
3957 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3958 struct got_object_id *commit_id)
3960 if (status == GOT_STATUS_NONEXISTENT)
3961 return NULL;
3962 if (status == staged_status && (status == GOT_STATUS_DELETE))
3963 status = GOT_STATUS_NO_CHANGE;
3964 printf("%c%c %s\n", status, staged_status, path);
3965 return NULL;
3968 static const struct got_error *
3969 cmd_remove(int argc, char *argv[])
3971 const struct got_error *error = NULL;
3972 struct got_worktree *worktree = NULL;
3973 struct got_repository *repo = NULL;
3974 char *cwd = NULL;
3975 struct got_pathlist_head paths;
3976 struct got_pathlist_entry *pe;
3977 int ch, delete_local_mods = 0;
3979 TAILQ_INIT(&paths);
3981 while ((ch = getopt(argc, argv, "f")) != -1) {
3982 switch (ch) {
3983 case 'f':
3984 delete_local_mods = 1;
3985 break;
3986 default:
3987 usage_add();
3988 /* NOTREACHED */
3992 argc -= optind;
3993 argv += optind;
3995 #ifndef PROFILE
3996 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3997 NULL) == -1)
3998 err(1, "pledge");
3999 #endif
4000 if (argc < 1)
4001 usage_remove();
4003 cwd = getcwd(NULL, 0);
4004 if (cwd == NULL) {
4005 error = got_error_from_errno("getcwd");
4006 goto done;
4008 error = got_worktree_open(&worktree, cwd);
4009 if (error)
4010 goto done;
4012 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4013 if (error)
4014 goto done;
4016 error = apply_unveil(got_repo_get_path(repo), 1,
4017 got_worktree_get_root_path(worktree));
4018 if (error)
4019 goto done;
4021 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4022 if (error)
4023 goto done;
4025 error = got_worktree_schedule_delete(worktree, &paths,
4026 delete_local_mods, print_remove_status, NULL, repo);
4027 if (error)
4028 goto done;
4029 done:
4030 if (repo)
4031 got_repo_close(repo);
4032 if (worktree)
4033 got_worktree_close(worktree);
4034 TAILQ_FOREACH(pe, &paths, entry)
4035 free((char *)pe->path);
4036 got_pathlist_free(&paths);
4037 free(cwd);
4038 return error;
4041 __dead static void
4042 usage_revert(void)
4044 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4045 "path ...\n", getprogname());
4046 exit(1);
4049 static const struct got_error *
4050 revert_progress(void *arg, unsigned char status, const char *path)
4052 while (path[0] == '/')
4053 path++;
4054 printf("%c %s\n", status, path);
4055 return NULL;
4058 struct choose_patch_arg {
4059 FILE *patch_script_file;
4060 const char *action;
4063 static const struct got_error *
4064 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4065 int nchanges, const char *action)
4067 char *line = NULL;
4068 size_t linesize = 0;
4069 ssize_t linelen;
4071 switch (status) {
4072 case GOT_STATUS_ADD:
4073 printf("A %s\n%s this addition? [y/n] ", path, action);
4074 break;
4075 case GOT_STATUS_DELETE:
4076 printf("D %s\n%s this deletion? [y/n] ", path, action);
4077 break;
4078 case GOT_STATUS_MODIFY:
4079 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4080 return got_error_from_errno("fseek");
4081 printf(GOT_COMMIT_SEP_STR);
4082 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4083 printf("%s", line);
4084 if (ferror(patch_file))
4085 return got_error_from_errno("getline");
4086 printf(GOT_COMMIT_SEP_STR);
4087 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4088 path, n, nchanges, action);
4089 break;
4090 default:
4091 return got_error_path(path, GOT_ERR_FILE_STATUS);
4094 return NULL;
4097 static const struct got_error *
4098 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4099 FILE *patch_file, int n, int nchanges)
4101 const struct got_error *err = NULL;
4102 char *line = NULL;
4103 size_t linesize = 0;
4104 ssize_t linelen;
4105 int resp = ' ';
4106 struct choose_patch_arg *a = arg;
4108 *choice = GOT_PATCH_CHOICE_NONE;
4110 if (a->patch_script_file) {
4111 char *nl;
4112 err = show_change(status, path, patch_file, n, nchanges,
4113 a->action);
4114 if (err)
4115 return err;
4116 linelen = getline(&line, &linesize, a->patch_script_file);
4117 if (linelen == -1) {
4118 if (ferror(a->patch_script_file))
4119 return got_error_from_errno("getline");
4120 return NULL;
4122 nl = strchr(line, '\n');
4123 if (nl)
4124 *nl = '\0';
4125 if (strcmp(line, "y") == 0) {
4126 *choice = GOT_PATCH_CHOICE_YES;
4127 printf("y\n");
4128 } else if (strcmp(line, "n") == 0) {
4129 *choice = GOT_PATCH_CHOICE_NO;
4130 printf("n\n");
4131 } else if (strcmp(line, "q") == 0 &&
4132 status == GOT_STATUS_MODIFY) {
4133 *choice = GOT_PATCH_CHOICE_QUIT;
4134 printf("q\n");
4135 } else
4136 printf("invalid response '%s'\n", line);
4137 free(line);
4138 return NULL;
4141 while (resp != 'y' && resp != 'n' && resp != 'q') {
4142 err = show_change(status, path, patch_file, n, nchanges,
4143 a->action);
4144 if (err)
4145 return err;
4146 resp = getchar();
4147 if (resp == '\n')
4148 resp = getchar();
4149 if (status == GOT_STATUS_MODIFY) {
4150 if (resp != 'y' && resp != 'n' && resp != 'q') {
4151 printf("invalid response '%c'\n", resp);
4152 resp = ' ';
4154 } else if (resp != 'y' && resp != 'n') {
4155 printf("invalid response '%c'\n", resp);
4156 resp = ' ';
4160 if (resp == 'y')
4161 *choice = GOT_PATCH_CHOICE_YES;
4162 else if (resp == 'n')
4163 *choice = GOT_PATCH_CHOICE_NO;
4164 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4165 *choice = GOT_PATCH_CHOICE_QUIT;
4167 return NULL;
4171 static const struct got_error *
4172 cmd_revert(int argc, char *argv[])
4174 const struct got_error *error = NULL;
4175 struct got_worktree *worktree = NULL;
4176 struct got_repository *repo = NULL;
4177 char *cwd = NULL, *path = NULL;
4178 struct got_pathlist_head paths;
4179 struct got_pathlist_entry *pe;
4180 int ch, can_recurse = 0, pflag = 0;
4181 FILE *patch_script_file = NULL;
4182 const char *patch_script_path = NULL;
4183 struct choose_patch_arg cpa;
4185 TAILQ_INIT(&paths);
4187 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4188 switch (ch) {
4189 case 'p':
4190 pflag = 1;
4191 break;
4192 case 'F':
4193 patch_script_path = optarg;
4194 break;
4195 case 'R':
4196 can_recurse = 1;
4197 break;
4198 default:
4199 usage_revert();
4200 /* NOTREACHED */
4204 argc -= optind;
4205 argv += optind;
4207 #ifndef PROFILE
4208 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4209 "unveil", NULL) == -1)
4210 err(1, "pledge");
4211 #endif
4212 if (argc < 1)
4213 usage_revert();
4214 if (patch_script_path && !pflag)
4215 errx(1, "-F option can only be used together with -p option");
4217 cwd = getcwd(NULL, 0);
4218 if (cwd == NULL) {
4219 error = got_error_from_errno("getcwd");
4220 goto done;
4222 error = got_worktree_open(&worktree, cwd);
4223 if (error)
4224 goto done;
4226 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4227 if (error != NULL)
4228 goto done;
4230 if (patch_script_path) {
4231 patch_script_file = fopen(patch_script_path, "r");
4232 if (patch_script_file == NULL) {
4233 error = got_error_from_errno2("fopen",
4234 patch_script_path);
4235 goto done;
4238 error = apply_unveil(got_repo_get_path(repo), 1,
4239 got_worktree_get_root_path(worktree));
4240 if (error)
4241 goto done;
4243 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4244 if (error)
4245 goto done;
4247 if (!can_recurse) {
4248 char *ondisk_path;
4249 struct stat sb;
4250 TAILQ_FOREACH(pe, &paths, entry) {
4251 if (asprintf(&ondisk_path, "%s/%s",
4252 got_worktree_get_root_path(worktree),
4253 pe->path) == -1) {
4254 error = got_error_from_errno("asprintf");
4255 goto done;
4257 if (lstat(ondisk_path, &sb) == -1) {
4258 if (errno == ENOENT) {
4259 free(ondisk_path);
4260 continue;
4262 error = got_error_from_errno2("lstat",
4263 ondisk_path);
4264 free(ondisk_path);
4265 goto done;
4267 free(ondisk_path);
4268 if (S_ISDIR(sb.st_mode)) {
4269 error = got_error_msg(GOT_ERR_BAD_PATH,
4270 "reverting directories requires -R option");
4271 goto done;
4276 cpa.patch_script_file = patch_script_file;
4277 cpa.action = "revert";
4278 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4279 pflag ? choose_patch : NULL, &cpa, repo);
4280 if (error)
4281 goto done;
4282 done:
4283 if (patch_script_file && fclose(patch_script_file) == EOF &&
4284 error == NULL)
4285 error = got_error_from_errno2("fclose", patch_script_path);
4286 if (repo)
4287 got_repo_close(repo);
4288 if (worktree)
4289 got_worktree_close(worktree);
4290 free(path);
4291 free(cwd);
4292 return error;
4295 __dead static void
4296 usage_commit(void)
4298 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4299 getprogname());
4300 exit(1);
4303 struct collect_commit_logmsg_arg {
4304 const char *cmdline_log;
4305 const char *editor;
4306 const char *worktree_path;
4307 const char *branch_name;
4308 const char *repo_path;
4309 char *logmsg_path;
4313 static const struct got_error *
4314 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4315 void *arg)
4317 char *initial_content = NULL;
4318 struct got_pathlist_entry *pe;
4319 const struct got_error *err = NULL;
4320 char *template = NULL;
4321 struct collect_commit_logmsg_arg *a = arg;
4322 int fd;
4323 size_t len;
4325 /* if a message was specified on the command line, just use it */
4326 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4327 len = strlen(a->cmdline_log) + 1;
4328 *logmsg = malloc(len + 1);
4329 if (*logmsg == NULL)
4330 return got_error_from_errno("malloc");
4331 strlcpy(*logmsg, a->cmdline_log, len);
4332 return NULL;
4335 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4336 return got_error_from_errno("asprintf");
4338 if (asprintf(&initial_content,
4339 "\n# changes to be committed on branch %s:\n",
4340 a->branch_name) == -1)
4341 return got_error_from_errno("asprintf");
4343 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4344 if (err)
4345 goto done;
4347 dprintf(fd, initial_content);
4349 TAILQ_FOREACH(pe, commitable_paths, entry) {
4350 struct got_commitable *ct = pe->data;
4351 dprintf(fd, "# %c %s\n",
4352 got_commitable_get_status(ct),
4353 got_commitable_get_path(ct));
4355 close(fd);
4357 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4358 done:
4359 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4360 unlink(a->logmsg_path);
4361 free(a->logmsg_path);
4362 a->logmsg_path = NULL;
4364 free(initial_content);
4365 free(template);
4367 /* Editor is done; we can now apply unveil(2) */
4368 if (err == NULL) {
4369 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4370 if (err) {
4371 free(*logmsg);
4372 *logmsg = NULL;
4375 return err;
4378 static const struct got_error *
4379 cmd_commit(int argc, char *argv[])
4381 const struct got_error *error = NULL;
4382 struct got_worktree *worktree = NULL;
4383 struct got_repository *repo = NULL;
4384 char *cwd = NULL, *id_str = NULL;
4385 struct got_object_id *id = NULL;
4386 const char *logmsg = NULL;
4387 const char *author;
4388 struct collect_commit_logmsg_arg cl_arg;
4389 char *editor = NULL;
4390 int ch, rebase_in_progress, histedit_in_progress;
4391 struct got_pathlist_head paths;
4393 TAILQ_INIT(&paths);
4394 cl_arg.logmsg_path = NULL;
4396 while ((ch = getopt(argc, argv, "m:")) != -1) {
4397 switch (ch) {
4398 case 'm':
4399 logmsg = optarg;
4400 break;
4401 default:
4402 usage_commit();
4403 /* NOTREACHED */
4407 argc -= optind;
4408 argv += optind;
4410 #ifndef PROFILE
4411 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4412 "unveil", NULL) == -1)
4413 err(1, "pledge");
4414 #endif
4415 error = get_author(&author);
4416 if (error)
4417 return error;
4419 cwd = getcwd(NULL, 0);
4420 if (cwd == NULL) {
4421 error = got_error_from_errno("getcwd");
4422 goto done;
4424 error = got_worktree_open(&worktree, cwd);
4425 if (error)
4426 goto done;
4428 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4429 if (error)
4430 goto done;
4431 if (rebase_in_progress) {
4432 error = got_error(GOT_ERR_REBASING);
4433 goto done;
4436 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4437 worktree);
4438 if (error)
4439 goto done;
4441 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4442 if (error != NULL)
4443 goto done;
4446 * unveil(2) traverses exec(2); if an editor is used we have
4447 * to apply unveil after the log message has been written.
4449 if (logmsg == NULL || strlen(logmsg) == 0)
4450 error = get_editor(&editor);
4451 else
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 = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4458 if (error)
4459 goto done;
4461 cl_arg.editor = editor;
4462 cl_arg.cmdline_log = logmsg;
4463 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4464 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4465 if (!histedit_in_progress) {
4466 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4467 error = got_error(GOT_ERR_COMMIT_BRANCH);
4468 goto done;
4470 cl_arg.branch_name += 11;
4472 cl_arg.repo_path = got_repo_get_path(repo);
4473 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4474 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4475 if (error) {
4476 if (cl_arg.logmsg_path)
4477 fprintf(stderr, "%s: log message preserved in %s\n",
4478 getprogname(), cl_arg.logmsg_path);
4479 goto done;
4482 if (cl_arg.logmsg_path)
4483 unlink(cl_arg.logmsg_path);
4485 error = got_object_id_str(&id_str, id);
4486 if (error)
4487 goto done;
4488 printf("Created commit %s\n", id_str);
4489 done:
4490 free(cl_arg.logmsg_path);
4491 if (repo)
4492 got_repo_close(repo);
4493 if (worktree)
4494 got_worktree_close(worktree);
4495 free(cwd);
4496 free(id_str);
4497 free(editor);
4498 return error;
4501 __dead static void
4502 usage_cherrypick(void)
4504 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4505 exit(1);
4508 static const struct got_error *
4509 cmd_cherrypick(int argc, char *argv[])
4511 const struct got_error *error = NULL;
4512 struct got_worktree *worktree = NULL;
4513 struct got_repository *repo = NULL;
4514 char *cwd = NULL, *commit_id_str = NULL;
4515 struct got_object_id *commit_id = NULL;
4516 struct got_commit_object *commit = NULL;
4517 struct got_object_qid *pid;
4518 struct got_reference *head_ref = NULL;
4519 int ch, did_something = 0;
4521 while ((ch = getopt(argc, argv, "")) != -1) {
4522 switch (ch) {
4523 default:
4524 usage_cherrypick();
4525 /* NOTREACHED */
4529 argc -= optind;
4530 argv += optind;
4532 #ifndef PROFILE
4533 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4534 "unveil", NULL) == -1)
4535 err(1, "pledge");
4536 #endif
4537 if (argc != 1)
4538 usage_cherrypick();
4540 cwd = getcwd(NULL, 0);
4541 if (cwd == NULL) {
4542 error = got_error_from_errno("getcwd");
4543 goto done;
4545 error = got_worktree_open(&worktree, cwd);
4546 if (error)
4547 goto done;
4549 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4550 if (error != NULL)
4551 goto done;
4553 error = apply_unveil(got_repo_get_path(repo), 0,
4554 got_worktree_get_root_path(worktree));
4555 if (error)
4556 goto done;
4558 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4559 GOT_OBJ_TYPE_COMMIT, repo);
4560 if (error != NULL) {
4561 struct got_reference *ref;
4562 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4563 goto done;
4564 error = got_ref_open(&ref, repo, argv[0], 0);
4565 if (error != NULL)
4566 goto done;
4567 error = got_ref_resolve(&commit_id, repo, ref);
4568 got_ref_close(ref);
4569 if (error != NULL)
4570 goto done;
4572 error = got_object_id_str(&commit_id_str, commit_id);
4573 if (error)
4574 goto done;
4576 error = got_ref_open(&head_ref, repo,
4577 got_worktree_get_head_ref_name(worktree), 0);
4578 if (error != NULL)
4579 goto done;
4581 error = check_same_branch(commit_id, head_ref, NULL, repo);
4582 if (error) {
4583 if (error->code != GOT_ERR_ANCESTRY)
4584 goto done;
4585 error = NULL;
4586 } else {
4587 error = got_error(GOT_ERR_SAME_BRANCH);
4588 goto done;
4591 error = got_object_open_as_commit(&commit, repo, commit_id);
4592 if (error)
4593 goto done;
4594 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4595 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4596 commit_id, repo, update_progress, &did_something, check_cancelled,
4597 NULL);
4598 if (error != NULL)
4599 goto done;
4601 if (did_something)
4602 printf("Merged commit %s\n", commit_id_str);
4603 done:
4604 if (commit)
4605 got_object_commit_close(commit);
4606 free(commit_id_str);
4607 if (head_ref)
4608 got_ref_close(head_ref);
4609 if (worktree)
4610 got_worktree_close(worktree);
4611 if (repo)
4612 got_repo_close(repo);
4613 return error;
4616 __dead static void
4617 usage_backout(void)
4619 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4620 exit(1);
4623 static const struct got_error *
4624 cmd_backout(int argc, char *argv[])
4626 const struct got_error *error = NULL;
4627 struct got_worktree *worktree = NULL;
4628 struct got_repository *repo = NULL;
4629 char *cwd = NULL, *commit_id_str = NULL;
4630 struct got_object_id *commit_id = NULL;
4631 struct got_commit_object *commit = NULL;
4632 struct got_object_qid *pid;
4633 struct got_reference *head_ref = NULL;
4634 int ch, did_something = 0;
4636 while ((ch = getopt(argc, argv, "")) != -1) {
4637 switch (ch) {
4638 default:
4639 usage_backout();
4640 /* NOTREACHED */
4644 argc -= optind;
4645 argv += optind;
4647 #ifndef PROFILE
4648 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4649 "unveil", NULL) == -1)
4650 err(1, "pledge");
4651 #endif
4652 if (argc != 1)
4653 usage_backout();
4655 cwd = getcwd(NULL, 0);
4656 if (cwd == NULL) {
4657 error = got_error_from_errno("getcwd");
4658 goto done;
4660 error = got_worktree_open(&worktree, cwd);
4661 if (error)
4662 goto done;
4664 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4665 if (error != NULL)
4666 goto done;
4668 error = apply_unveil(got_repo_get_path(repo), 0,
4669 got_worktree_get_root_path(worktree));
4670 if (error)
4671 goto done;
4673 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4674 GOT_OBJ_TYPE_COMMIT, repo);
4675 if (error != NULL) {
4676 struct got_reference *ref;
4677 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4678 goto done;
4679 error = got_ref_open(&ref, repo, argv[0], 0);
4680 if (error != NULL)
4681 goto done;
4682 error = got_ref_resolve(&commit_id, repo, ref);
4683 got_ref_close(ref);
4684 if (error != NULL)
4685 goto done;
4687 error = got_object_id_str(&commit_id_str, commit_id);
4688 if (error)
4689 goto done;
4691 error = got_ref_open(&head_ref, repo,
4692 got_worktree_get_head_ref_name(worktree), 0);
4693 if (error != NULL)
4694 goto done;
4696 error = check_same_branch(commit_id, head_ref, NULL, repo);
4697 if (error)
4698 goto done;
4700 error = got_object_open_as_commit(&commit, repo, commit_id);
4701 if (error)
4702 goto done;
4703 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4704 if (pid == NULL) {
4705 error = got_error(GOT_ERR_ROOT_COMMIT);
4706 goto done;
4709 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4710 update_progress, &did_something, check_cancelled, NULL);
4711 if (error != NULL)
4712 goto done;
4714 if (did_something)
4715 printf("Backed out commit %s\n", commit_id_str);
4716 done:
4717 if (commit)
4718 got_object_commit_close(commit);
4719 free(commit_id_str);
4720 if (head_ref)
4721 got_ref_close(head_ref);
4722 if (worktree)
4723 got_worktree_close(worktree);
4724 if (repo)
4725 got_repo_close(repo);
4726 return error;
4729 __dead static void
4730 usage_rebase(void)
4732 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4733 getprogname());
4734 exit(1);
4737 void
4738 trim_logmsg(char *logmsg, int limit)
4740 char *nl;
4741 size_t len;
4743 len = strlen(logmsg);
4744 if (len > limit)
4745 len = limit;
4746 logmsg[len] = '\0';
4747 nl = strchr(logmsg, '\n');
4748 if (nl)
4749 *nl = '\0';
4752 static const struct got_error *
4753 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4755 const struct got_error *err;
4756 char *logmsg0 = NULL;
4757 const char *s;
4759 err = got_object_commit_get_logmsg(&logmsg0, commit);
4760 if (err)
4761 return err;
4763 s = logmsg0;
4764 while (isspace((unsigned char)s[0]))
4765 s++;
4767 *logmsg = strdup(s);
4768 if (*logmsg == NULL) {
4769 err = got_error_from_errno("strdup");
4770 goto done;
4773 trim_logmsg(*logmsg, limit);
4774 done:
4775 free(logmsg0);
4776 return err;
4779 static const struct got_error *
4780 show_rebase_progress(struct got_commit_object *commit,
4781 struct got_object_id *old_id, struct got_object_id *new_id)
4783 const struct got_error *err;
4784 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4786 err = got_object_id_str(&old_id_str, old_id);
4787 if (err)
4788 goto done;
4790 if (new_id) {
4791 err = got_object_id_str(&new_id_str, new_id);
4792 if (err)
4793 goto done;
4796 old_id_str[12] = '\0';
4797 if (new_id_str)
4798 new_id_str[12] = '\0';
4800 err = get_short_logmsg(&logmsg, 42, commit);
4801 if (err)
4802 goto done;
4804 printf("%s -> %s: %s\n", old_id_str,
4805 new_id_str ? new_id_str : "no-op change", logmsg);
4806 done:
4807 free(old_id_str);
4808 free(new_id_str);
4809 return err;
4812 static const struct got_error *
4813 rebase_progress(void *arg, unsigned char status, const char *path)
4815 unsigned char *rebase_status = arg;
4817 while (path[0] == '/')
4818 path++;
4819 printf("%c %s\n", status, path);
4821 if (*rebase_status == GOT_STATUS_CONFLICT)
4822 return NULL;
4823 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4824 *rebase_status = status;
4825 return NULL;
4828 static const struct got_error *
4829 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4830 struct got_reference *branch, struct got_reference *new_base_branch,
4831 struct got_reference *tmp_branch, struct got_repository *repo)
4833 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4834 return got_worktree_rebase_complete(worktree, fileindex,
4835 new_base_branch, tmp_branch, branch, repo);
4838 static const struct got_error *
4839 rebase_commit(struct got_pathlist_head *merged_paths,
4840 struct got_worktree *worktree, struct got_fileindex *fileindex,
4841 struct got_reference *tmp_branch,
4842 struct got_object_id *commit_id, struct got_repository *repo)
4844 const struct got_error *error;
4845 struct got_commit_object *commit;
4846 struct got_object_id *new_commit_id;
4848 error = got_object_open_as_commit(&commit, repo, commit_id);
4849 if (error)
4850 return error;
4852 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4853 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4854 if (error) {
4855 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4856 goto done;
4857 error = show_rebase_progress(commit, commit_id, NULL);
4858 } else {
4859 error = show_rebase_progress(commit, commit_id, new_commit_id);
4860 free(new_commit_id);
4862 done:
4863 got_object_commit_close(commit);
4864 return error;
4867 struct check_path_prefix_arg {
4868 const char *path_prefix;
4869 size_t len;
4870 int errcode;
4873 static const struct got_error *
4874 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4875 struct got_blob_object *blob2, struct got_object_id *id1,
4876 struct got_object_id *id2, const char *path1, const char *path2,
4877 struct got_repository *repo)
4879 struct check_path_prefix_arg *a = arg;
4881 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4882 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4883 return got_error(a->errcode);
4885 return NULL;
4888 static const struct got_error *
4889 check_path_prefix(struct got_object_id *parent_id,
4890 struct got_object_id *commit_id, const char *path_prefix,
4891 int errcode, struct got_repository *repo)
4893 const struct got_error *err;
4894 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4895 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4896 struct check_path_prefix_arg cpp_arg;
4898 if (got_path_is_root_dir(path_prefix))
4899 return NULL;
4901 err = got_object_open_as_commit(&commit, repo, commit_id);
4902 if (err)
4903 goto done;
4905 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4906 if (err)
4907 goto done;
4909 err = got_object_open_as_tree(&tree1, repo,
4910 got_object_commit_get_tree_id(parent_commit));
4911 if (err)
4912 goto done;
4914 err = got_object_open_as_tree(&tree2, repo,
4915 got_object_commit_get_tree_id(commit));
4916 if (err)
4917 goto done;
4919 cpp_arg.path_prefix = path_prefix;
4920 while (cpp_arg.path_prefix[0] == '/')
4921 cpp_arg.path_prefix++;
4922 cpp_arg.len = strlen(cpp_arg.path_prefix);
4923 cpp_arg.errcode = errcode;
4924 err = got_diff_tree(tree1, tree2, "", "", repo,
4925 check_path_prefix_in_diff, &cpp_arg, 0);
4926 done:
4927 if (tree1)
4928 got_object_tree_close(tree1);
4929 if (tree2)
4930 got_object_tree_close(tree2);
4931 if (commit)
4932 got_object_commit_close(commit);
4933 if (parent_commit)
4934 got_object_commit_close(parent_commit);
4935 return err;
4938 static const struct got_error *
4939 collect_commits(struct got_object_id_queue *commits,
4940 struct got_object_id *initial_commit_id,
4941 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4942 const char *path_prefix, int path_prefix_errcode,
4943 struct got_repository *repo)
4945 const struct got_error *err = NULL;
4946 struct got_commit_graph *graph = NULL;
4947 struct got_object_id *parent_id = NULL;
4948 struct got_object_qid *qid;
4949 struct got_object_id *commit_id = initial_commit_id;
4951 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4952 if (err)
4953 return err;
4955 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4956 check_cancelled, NULL);
4957 if (err)
4958 goto done;
4959 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4960 err = got_commit_graph_iter_next(&parent_id, graph);
4961 if (err) {
4962 if (err->code == GOT_ERR_ITER_COMPLETED) {
4963 err = got_error_msg(GOT_ERR_ANCESTRY,
4964 "ran out of commits to rebase before "
4965 "youngest common ancestor commit has "
4966 "been reached?!?");
4967 goto done;
4968 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4969 goto done;
4970 err = got_commit_graph_fetch_commits(graph, 1, repo,
4971 check_cancelled, NULL);
4972 if (err)
4973 goto done;
4974 } else {
4975 err = check_path_prefix(parent_id, commit_id,
4976 path_prefix, path_prefix_errcode, repo);
4977 if (err)
4978 goto done;
4980 err = got_object_qid_alloc(&qid, commit_id);
4981 if (err)
4982 goto done;
4983 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4984 commit_id = parent_id;
4987 done:
4988 got_commit_graph_close(graph);
4989 return err;
4992 static const struct got_error *
4993 cmd_rebase(int argc, char *argv[])
4995 const struct got_error *error = NULL;
4996 struct got_worktree *worktree = NULL;
4997 struct got_repository *repo = NULL;
4998 struct got_fileindex *fileindex = NULL;
4999 char *cwd = NULL;
5000 struct got_reference *branch = NULL;
5001 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5002 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5003 struct got_object_id *resume_commit_id = NULL;
5004 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5005 struct got_commit_object *commit = NULL;
5006 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5007 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5008 struct got_object_id_queue commits;
5009 struct got_pathlist_head merged_paths;
5010 const struct got_object_id_queue *parent_ids;
5011 struct got_object_qid *qid, *pid;
5013 SIMPLEQ_INIT(&commits);
5014 TAILQ_INIT(&merged_paths);
5016 while ((ch = getopt(argc, argv, "ac")) != -1) {
5017 switch (ch) {
5018 case 'a':
5019 abort_rebase = 1;
5020 break;
5021 case 'c':
5022 continue_rebase = 1;
5023 break;
5024 default:
5025 usage_rebase();
5026 /* NOTREACHED */
5030 argc -= optind;
5031 argv += optind;
5033 #ifndef PROFILE
5034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5035 "unveil", NULL) == -1)
5036 err(1, "pledge");
5037 #endif
5038 if (abort_rebase && continue_rebase)
5039 usage_rebase();
5040 else if (abort_rebase || continue_rebase) {
5041 if (argc != 0)
5042 usage_rebase();
5043 } else if (argc != 1)
5044 usage_rebase();
5046 cwd = getcwd(NULL, 0);
5047 if (cwd == NULL) {
5048 error = got_error_from_errno("getcwd");
5049 goto done;
5051 error = got_worktree_open(&worktree, cwd);
5052 if (error)
5053 goto done;
5055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5056 if (error != NULL)
5057 goto done;
5059 error = apply_unveil(got_repo_get_path(repo), 0,
5060 got_worktree_get_root_path(worktree));
5061 if (error)
5062 goto done;
5064 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5065 if (error)
5066 goto done;
5068 if (abort_rebase) {
5069 int did_something;
5070 if (!rebase_in_progress) {
5071 error = got_error(GOT_ERR_NOT_REBASING);
5072 goto done;
5074 error = got_worktree_rebase_continue(&resume_commit_id,
5075 &new_base_branch, &tmp_branch, &branch, &fileindex,
5076 worktree, repo);
5077 if (error)
5078 goto done;
5079 printf("Switching work tree to %s\n",
5080 got_ref_get_symref_target(new_base_branch));
5081 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5082 new_base_branch, update_progress, &did_something);
5083 if (error)
5084 goto done;
5085 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5086 goto done; /* nothing else to do */
5089 if (continue_rebase) {
5090 if (!rebase_in_progress) {
5091 error = got_error(GOT_ERR_NOT_REBASING);
5092 goto done;
5094 error = got_worktree_rebase_continue(&resume_commit_id,
5095 &new_base_branch, &tmp_branch, &branch, &fileindex,
5096 worktree, repo);
5097 if (error)
5098 goto done;
5100 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5101 resume_commit_id, repo);
5102 if (error)
5103 goto done;
5105 yca_id = got_object_id_dup(resume_commit_id);
5106 if (yca_id == NULL) {
5107 error = got_error_from_errno("got_object_id_dup");
5108 goto done;
5110 } else {
5111 error = got_ref_open(&branch, repo, argv[0], 0);
5112 if (error != NULL)
5113 goto done;
5116 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5117 if (error)
5118 goto done;
5120 if (!continue_rebase) {
5121 struct got_object_id *base_commit_id;
5123 base_commit_id = got_worktree_get_base_commit_id(worktree);
5124 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5125 base_commit_id, branch_head_commit_id, repo,
5126 check_cancelled, NULL);
5127 if (error)
5128 goto done;
5129 if (yca_id == NULL) {
5130 error = got_error_msg(GOT_ERR_ANCESTRY,
5131 "specified branch shares no common ancestry "
5132 "with work tree's branch");
5133 goto done;
5136 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5137 if (error) {
5138 if (error->code != GOT_ERR_ANCESTRY)
5139 goto done;
5140 error = NULL;
5141 } else {
5142 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5143 "specified branch resolves to a commit which "
5144 "is already contained in work tree's branch");
5145 goto done;
5147 error = got_worktree_rebase_prepare(&new_base_branch,
5148 &tmp_branch, &fileindex, worktree, branch, repo);
5149 if (error)
5150 goto done;
5153 commit_id = branch_head_commit_id;
5154 error = got_object_open_as_commit(&commit, repo, commit_id);
5155 if (error)
5156 goto done;
5158 parent_ids = got_object_commit_get_parent_ids(commit);
5159 pid = SIMPLEQ_FIRST(parent_ids);
5160 if (pid == NULL) {
5161 if (!continue_rebase) {
5162 int did_something;
5163 error = got_worktree_rebase_abort(worktree, fileindex,
5164 repo, new_base_branch, update_progress,
5165 &did_something);
5166 if (error)
5167 goto done;
5168 printf("Rebase of %s aborted\n",
5169 got_ref_get_name(branch));
5171 error = got_error(GOT_ERR_EMPTY_REBASE);
5172 goto done;
5174 error = collect_commits(&commits, commit_id, pid->id,
5175 yca_id, got_worktree_get_path_prefix(worktree),
5176 GOT_ERR_REBASE_PATH, repo);
5177 got_object_commit_close(commit);
5178 commit = NULL;
5179 if (error)
5180 goto done;
5182 if (SIMPLEQ_EMPTY(&commits)) {
5183 if (continue_rebase)
5184 error = rebase_complete(worktree, fileindex,
5185 branch, new_base_branch, tmp_branch, repo);
5186 else
5187 error = got_error(GOT_ERR_EMPTY_REBASE);
5188 goto done;
5191 pid = NULL;
5192 SIMPLEQ_FOREACH(qid, &commits, entry) {
5193 commit_id = qid->id;
5194 parent_id = pid ? pid->id : yca_id;
5195 pid = qid;
5197 error = got_worktree_rebase_merge_files(&merged_paths,
5198 worktree, fileindex, parent_id, commit_id, repo,
5199 rebase_progress, &rebase_status, check_cancelled, NULL);
5200 if (error)
5201 goto done;
5203 if (rebase_status == GOT_STATUS_CONFLICT) {
5204 got_worktree_rebase_pathlist_free(&merged_paths);
5205 break;
5208 error = rebase_commit(&merged_paths, worktree, fileindex,
5209 tmp_branch, commit_id, repo);
5210 got_worktree_rebase_pathlist_free(&merged_paths);
5211 if (error)
5212 goto done;
5215 if (rebase_status == GOT_STATUS_CONFLICT) {
5216 error = got_worktree_rebase_postpone(worktree, fileindex);
5217 if (error)
5218 goto done;
5219 error = got_error_msg(GOT_ERR_CONFLICTS,
5220 "conflicts must be resolved before rebasing can continue");
5221 } else
5222 error = rebase_complete(worktree, fileindex, branch,
5223 new_base_branch, tmp_branch, repo);
5224 done:
5225 got_object_id_queue_free(&commits);
5226 free(branch_head_commit_id);
5227 free(resume_commit_id);
5228 free(yca_id);
5229 if (commit)
5230 got_object_commit_close(commit);
5231 if (branch)
5232 got_ref_close(branch);
5233 if (new_base_branch)
5234 got_ref_close(new_base_branch);
5235 if (tmp_branch)
5236 got_ref_close(tmp_branch);
5237 if (worktree)
5238 got_worktree_close(worktree);
5239 if (repo)
5240 got_repo_close(repo);
5241 return error;
5244 __dead static void
5245 usage_histedit(void)
5247 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5248 getprogname());
5249 exit(1);
5252 #define GOT_HISTEDIT_PICK 'p'
5253 #define GOT_HISTEDIT_EDIT 'e'
5254 #define GOT_HISTEDIT_FOLD 'f'
5255 #define GOT_HISTEDIT_DROP 'd'
5256 #define GOT_HISTEDIT_MESG 'm'
5258 static struct got_histedit_cmd {
5259 unsigned char code;
5260 const char *name;
5261 const char *desc;
5262 } got_histedit_cmds[] = {
5263 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5264 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5265 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5266 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5267 { GOT_HISTEDIT_MESG, "mesg",
5268 "single-line log message for commit above (open editor if empty)" },
5271 struct got_histedit_list_entry {
5272 TAILQ_ENTRY(got_histedit_list_entry) entry;
5273 struct got_object_id *commit_id;
5274 const struct got_histedit_cmd *cmd;
5275 char *logmsg;
5277 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5279 static const struct got_error *
5280 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5281 FILE *f, struct got_repository *repo)
5283 const struct got_error *err = NULL;
5284 char *logmsg = NULL, *id_str = NULL;
5285 struct got_commit_object *commit = NULL;
5286 int n;
5288 err = got_object_open_as_commit(&commit, repo, commit_id);
5289 if (err)
5290 goto done;
5292 err = get_short_logmsg(&logmsg, 34, commit);
5293 if (err)
5294 goto done;
5296 err = got_object_id_str(&id_str, commit_id);
5297 if (err)
5298 goto done;
5300 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5301 if (n < 0)
5302 err = got_ferror(f, GOT_ERR_IO);
5303 done:
5304 if (commit)
5305 got_object_commit_close(commit);
5306 free(id_str);
5307 free(logmsg);
5308 return err;
5311 static const struct got_error *
5312 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5313 struct got_repository *repo)
5315 const struct got_error *err = NULL;
5316 struct got_object_qid *qid;
5318 if (SIMPLEQ_EMPTY(commits))
5319 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5321 SIMPLEQ_FOREACH(qid, commits, entry) {
5322 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5323 f, repo);
5324 if (err)
5325 break;
5328 return err;
5331 static const struct got_error *
5332 write_cmd_list(FILE *f)
5334 const struct got_error *err = NULL;
5335 int n, i;
5337 n = fprintf(f, "# Available histedit commands:\n");
5338 if (n < 0)
5339 return got_ferror(f, GOT_ERR_IO);
5341 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5342 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5343 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5344 cmd->desc);
5345 if (n < 0) {
5346 err = got_ferror(f, GOT_ERR_IO);
5347 break;
5350 n = fprintf(f, "# Commits will be processed in order from top to "
5351 "bottom of this file.\n");
5352 if (n < 0)
5353 return got_ferror(f, GOT_ERR_IO);
5354 return err;
5357 static const struct got_error *
5358 histedit_syntax_error(int lineno)
5360 static char msg[42];
5361 int ret;
5363 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5364 lineno);
5365 if (ret == -1 || ret >= sizeof(msg))
5366 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5368 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5371 static const struct got_error *
5372 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5373 char *logmsg, struct got_repository *repo)
5375 const struct got_error *err;
5376 struct got_commit_object *folded_commit = NULL;
5377 char *id_str, *folded_logmsg = NULL;
5379 err = got_object_id_str(&id_str, hle->commit_id);
5380 if (err)
5381 return err;
5383 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5384 if (err)
5385 goto done;
5387 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5388 if (err)
5389 goto done;
5390 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5391 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5392 folded_logmsg) == -1) {
5393 err = got_error_from_errno("asprintf");
5394 goto done;
5396 done:
5397 if (folded_commit)
5398 got_object_commit_close(folded_commit);
5399 free(id_str);
5400 free(folded_logmsg);
5401 return err;
5404 static struct got_histedit_list_entry *
5405 get_folded_commits(struct got_histedit_list_entry *hle)
5407 struct got_histedit_list_entry *prev, *folded = NULL;
5409 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5410 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5411 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5412 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5413 folded = prev;
5414 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5417 return folded;
5420 static const struct got_error *
5421 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5422 struct got_repository *repo)
5424 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5425 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5426 const struct got_error *err = NULL;
5427 struct got_commit_object *commit = NULL;
5428 int fd;
5429 struct got_histedit_list_entry *folded = NULL;
5431 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5432 if (err)
5433 return err;
5435 folded = get_folded_commits(hle);
5436 if (folded) {
5437 while (folded != hle) {
5438 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5439 folded = TAILQ_NEXT(folded, entry);
5440 continue;
5442 err = append_folded_commit_msg(&new_msg, folded,
5443 logmsg, repo);
5444 if (err)
5445 goto done;
5446 free(logmsg);
5447 logmsg = new_msg;
5448 folded = TAILQ_NEXT(folded, entry);
5452 err = got_object_id_str(&id_str, hle->commit_id);
5453 if (err)
5454 goto done;
5455 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5456 if (err)
5457 goto done;
5458 if (asprintf(&new_msg,
5459 "%s\n# original log message of commit %s: %s",
5460 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5461 err = got_error_from_errno("asprintf");
5462 goto done;
5464 free(logmsg);
5465 logmsg = new_msg;
5467 err = got_object_id_str(&id_str, hle->commit_id);
5468 if (err)
5469 goto done;
5471 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5472 if (err)
5473 goto done;
5475 dprintf(fd, logmsg);
5476 close(fd);
5478 err = get_editor(&editor);
5479 if (err)
5480 goto done;
5482 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5483 if (err) {
5484 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5485 goto done;
5486 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5488 done:
5489 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5490 err = got_error_from_errno2("unlink", logmsg_path);
5491 free(logmsg_path);
5492 free(logmsg);
5493 free(orig_logmsg);
5494 free(editor);
5495 if (commit)
5496 got_object_commit_close(commit);
5497 return err;
5500 static const struct got_error *
5501 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5502 FILE *f, struct got_repository *repo)
5504 const struct got_error *err = NULL;
5505 char *line = NULL, *p, *end;
5506 size_t size;
5507 ssize_t len;
5508 int lineno = 0, i;
5509 const struct got_histedit_cmd *cmd;
5510 struct got_object_id *commit_id = NULL;
5511 struct got_histedit_list_entry *hle = NULL;
5513 for (;;) {
5514 len = getline(&line, &size, f);
5515 if (len == -1) {
5516 const struct got_error *getline_err;
5517 if (feof(f))
5518 break;
5519 getline_err = got_error_from_errno("getline");
5520 err = got_ferror(f, getline_err->code);
5521 break;
5523 lineno++;
5524 p = line;
5525 while (isspace((unsigned char)p[0]))
5526 p++;
5527 if (p[0] == '#' || p[0] == '\0') {
5528 free(line);
5529 line = NULL;
5530 continue;
5532 cmd = NULL;
5533 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5534 cmd = &got_histedit_cmds[i];
5535 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5536 isspace((unsigned char)p[strlen(cmd->name)])) {
5537 p += strlen(cmd->name);
5538 break;
5540 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5541 p++;
5542 break;
5545 if (i == nitems(got_histedit_cmds)) {
5546 err = histedit_syntax_error(lineno);
5547 break;
5549 while (isspace((unsigned char)p[0]))
5550 p++;
5551 if (cmd->code == GOT_HISTEDIT_MESG) {
5552 if (hle == NULL || hle->logmsg != NULL) {
5553 err = got_error(GOT_ERR_HISTEDIT_CMD);
5554 break;
5556 if (p[0] == '\0') {
5557 err = histedit_edit_logmsg(hle, repo);
5558 if (err)
5559 break;
5560 } else {
5561 hle->logmsg = strdup(p);
5562 if (hle->logmsg == NULL) {
5563 err = got_error_from_errno("strdup");
5564 break;
5567 free(line);
5568 line = NULL;
5569 continue;
5570 } else {
5571 end = p;
5572 while (end[0] && !isspace((unsigned char)end[0]))
5573 end++;
5574 *end = '\0';
5576 err = got_object_resolve_id_str(&commit_id, repo, p);
5577 if (err) {
5578 /* override error code */
5579 err = histedit_syntax_error(lineno);
5580 break;
5583 hle = malloc(sizeof(*hle));
5584 if (hle == NULL) {
5585 err = got_error_from_errno("malloc");
5586 break;
5588 hle->cmd = cmd;
5589 hle->commit_id = commit_id;
5590 hle->logmsg = NULL;
5591 commit_id = NULL;
5592 free(line);
5593 line = NULL;
5594 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5597 free(line);
5598 free(commit_id);
5599 return err;
5602 static const struct got_error *
5603 histedit_check_script(struct got_histedit_list *histedit_cmds,
5604 struct got_object_id_queue *commits, struct got_repository *repo)
5606 const struct got_error *err = NULL;
5607 struct got_object_qid *qid;
5608 struct got_histedit_list_entry *hle;
5609 static char msg[80];
5610 char *id_str;
5612 if (TAILQ_EMPTY(histedit_cmds))
5613 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5614 "histedit script contains no commands");
5615 if (SIMPLEQ_EMPTY(commits))
5616 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5618 SIMPLEQ_FOREACH(qid, commits, entry) {
5619 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5620 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5621 break;
5623 if (hle == NULL) {
5624 err = got_object_id_str(&id_str, qid->id);
5625 if (err)
5626 return err;
5627 snprintf(msg, sizeof(msg),
5628 "commit %s missing from histedit script", id_str);
5629 free(id_str);
5630 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5634 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5635 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5636 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5637 "last commit in histedit script cannot be folded");
5639 return NULL;
5642 static const struct got_error *
5643 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5644 const char *path, struct got_object_id_queue *commits,
5645 struct got_repository *repo)
5647 const struct got_error *err = NULL;
5648 char *editor;
5649 FILE *f = NULL;
5651 err = get_editor(&editor);
5652 if (err)
5653 return err;
5655 if (spawn_editor(editor, path) == -1) {
5656 err = got_error_from_errno("failed spawning editor");
5657 goto done;
5660 f = fopen(path, "r");
5661 if (f == NULL) {
5662 err = got_error_from_errno("fopen");
5663 goto done;
5665 err = histedit_parse_list(histedit_cmds, f, repo);
5666 if (err)
5667 goto done;
5669 err = histedit_check_script(histedit_cmds, commits, repo);
5670 done:
5671 if (f && fclose(f) != 0 && err == NULL)
5672 err = got_error_from_errno("fclose");
5673 free(editor);
5674 return err;
5677 static const struct got_error *
5678 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5679 struct got_object_id_queue *, const char *, struct got_repository *);
5681 static const struct got_error *
5682 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5683 struct got_object_id_queue *commits, struct got_repository *repo)
5685 const struct got_error *err;
5686 FILE *f = NULL;
5687 char *path = NULL;
5689 err = got_opentemp_named(&path, &f, "got-histedit");
5690 if (err)
5691 return err;
5693 err = write_cmd_list(f);
5694 if (err)
5695 goto done;
5697 err = histedit_write_commit_list(commits, f, repo);
5698 if (err)
5699 goto done;
5701 if (fclose(f) != 0) {
5702 err = got_error_from_errno("fclose");
5703 goto done;
5705 f = NULL;
5707 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5708 if (err) {
5709 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5710 err->code != GOT_ERR_HISTEDIT_CMD)
5711 goto done;
5712 err = histedit_edit_list_retry(histedit_cmds, err,
5713 commits, path, repo);
5715 done:
5716 if (f && fclose(f) != 0 && err == NULL)
5717 err = got_error_from_errno("fclose");
5718 if (path && unlink(path) != 0 && err == NULL)
5719 err = got_error_from_errno2("unlink", path);
5720 free(path);
5721 return err;
5724 static const struct got_error *
5725 histedit_save_list(struct got_histedit_list *histedit_cmds,
5726 struct got_worktree *worktree, struct got_repository *repo)
5728 const struct got_error *err = NULL;
5729 char *path = NULL;
5730 FILE *f = NULL;
5731 struct got_histedit_list_entry *hle;
5732 struct got_commit_object *commit = NULL;
5734 err = got_worktree_get_histedit_script_path(&path, worktree);
5735 if (err)
5736 return err;
5738 f = fopen(path, "w");
5739 if (f == NULL) {
5740 err = got_error_from_errno2("fopen", path);
5741 goto done;
5743 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5744 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5745 repo);
5746 if (err)
5747 break;
5749 if (hle->logmsg) {
5750 int n = fprintf(f, "%c %s\n",
5751 GOT_HISTEDIT_MESG, hle->logmsg);
5752 if (n < 0) {
5753 err = got_ferror(f, GOT_ERR_IO);
5754 break;
5758 done:
5759 if (f && fclose(f) != 0 && err == NULL)
5760 err = got_error_from_errno("fclose");
5761 free(path);
5762 if (commit)
5763 got_object_commit_close(commit);
5764 return err;
5767 void
5768 histedit_free_list(struct got_histedit_list *histedit_cmds)
5770 struct got_histedit_list_entry *hle;
5772 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5773 TAILQ_REMOVE(histedit_cmds, hle, entry);
5774 free(hle);
5778 static const struct got_error *
5779 histedit_load_list(struct got_histedit_list *histedit_cmds,
5780 const char *path, struct got_repository *repo)
5782 const struct got_error *err = NULL;
5783 FILE *f = NULL;
5785 f = fopen(path, "r");
5786 if (f == NULL) {
5787 err = got_error_from_errno2("fopen", path);
5788 goto done;
5791 err = histedit_parse_list(histedit_cmds, f, repo);
5792 done:
5793 if (f && fclose(f) != 0 && err == NULL)
5794 err = got_error_from_errno("fclose");
5795 return err;
5798 static const struct got_error *
5799 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5800 const struct got_error *edit_err, struct got_object_id_queue *commits,
5801 const char *path, struct got_repository *repo)
5803 const struct got_error *err = NULL, *prev_err = edit_err;
5804 int resp = ' ';
5806 while (resp != 'c' && resp != 'r' && resp != 'a') {
5807 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5808 "or (a)bort: ", getprogname(), prev_err->msg);
5809 resp = getchar();
5810 if (resp == '\n')
5811 resp = getchar();
5812 if (resp == 'c') {
5813 histedit_free_list(histedit_cmds);
5814 err = histedit_run_editor(histedit_cmds, path, commits,
5815 repo);
5816 if (err) {
5817 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5818 err->code != GOT_ERR_HISTEDIT_CMD)
5819 break;
5820 prev_err = err;
5821 resp = ' ';
5822 continue;
5824 break;
5825 } else if (resp == 'r') {
5826 histedit_free_list(histedit_cmds);
5827 err = histedit_edit_script(histedit_cmds,
5828 commits, repo);
5829 if (err) {
5830 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5831 err->code != GOT_ERR_HISTEDIT_CMD)
5832 break;
5833 prev_err = err;
5834 resp = ' ';
5835 continue;
5837 break;
5838 } else if (resp == 'a') {
5839 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5840 break;
5841 } else
5842 printf("invalid response '%c'\n", resp);
5845 return err;
5848 static const struct got_error *
5849 histedit_complete(struct got_worktree *worktree,
5850 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5851 struct got_reference *branch, struct got_repository *repo)
5853 printf("Switching work tree to %s\n",
5854 got_ref_get_symref_target(branch));
5855 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5856 branch, repo);
5859 static const struct got_error *
5860 show_histedit_progress(struct got_commit_object *commit,
5861 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5863 const struct got_error *err;
5864 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5866 err = got_object_id_str(&old_id_str, hle->commit_id);
5867 if (err)
5868 goto done;
5870 if (new_id) {
5871 err = got_object_id_str(&new_id_str, new_id);
5872 if (err)
5873 goto done;
5876 old_id_str[12] = '\0';
5877 if (new_id_str)
5878 new_id_str[12] = '\0';
5880 if (hle->logmsg) {
5881 logmsg = strdup(hle->logmsg);
5882 if (logmsg == NULL) {
5883 err = got_error_from_errno("strdup");
5884 goto done;
5886 trim_logmsg(logmsg, 42);
5887 } else {
5888 err = get_short_logmsg(&logmsg, 42, commit);
5889 if (err)
5890 goto done;
5893 switch (hle->cmd->code) {
5894 case GOT_HISTEDIT_PICK:
5895 case GOT_HISTEDIT_EDIT:
5896 printf("%s -> %s: %s\n", old_id_str,
5897 new_id_str ? new_id_str : "no-op change", logmsg);
5898 break;
5899 case GOT_HISTEDIT_DROP:
5900 case GOT_HISTEDIT_FOLD:
5901 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5902 logmsg);
5903 break;
5904 default:
5905 break;
5908 done:
5909 free(old_id_str);
5910 free(new_id_str);
5911 return err;
5914 static const struct got_error *
5915 histedit_commit(struct got_pathlist_head *merged_paths,
5916 struct got_worktree *worktree, struct got_fileindex *fileindex,
5917 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5918 struct got_repository *repo)
5920 const struct got_error *err;
5921 struct got_commit_object *commit;
5922 struct got_object_id *new_commit_id;
5924 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5925 && hle->logmsg == NULL) {
5926 err = histedit_edit_logmsg(hle, repo);
5927 if (err)
5928 return err;
5931 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5932 if (err)
5933 return err;
5935 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5936 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5937 hle->logmsg, repo);
5938 if (err) {
5939 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5940 goto done;
5941 err = show_histedit_progress(commit, hle, NULL);
5942 } else {
5943 err = show_histedit_progress(commit, hle, new_commit_id);
5944 free(new_commit_id);
5946 done:
5947 got_object_commit_close(commit);
5948 return err;
5951 static const struct got_error *
5952 histedit_skip_commit(struct got_histedit_list_entry *hle,
5953 struct got_worktree *worktree, struct got_repository *repo)
5955 const struct got_error *error;
5956 struct got_commit_object *commit;
5958 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5959 repo);
5960 if (error)
5961 return error;
5963 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5964 if (error)
5965 return error;
5967 error = show_histedit_progress(commit, hle, NULL);
5968 got_object_commit_close(commit);
5969 return error;
5972 static const struct got_error *
5973 cmd_histedit(int argc, char *argv[])
5975 const struct got_error *error = NULL;
5976 struct got_worktree *worktree = NULL;
5977 struct got_fileindex *fileindex = NULL;
5978 struct got_repository *repo = NULL;
5979 char *cwd = NULL;
5980 struct got_reference *branch = NULL;
5981 struct got_reference *tmp_branch = NULL;
5982 struct got_object_id *resume_commit_id = NULL;
5983 struct got_object_id *base_commit_id = NULL;
5984 struct got_object_id *head_commit_id = NULL;
5985 struct got_commit_object *commit = NULL;
5986 int ch, rebase_in_progress = 0, did_something;
5987 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5988 const char *edit_script_path = NULL;
5989 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5990 struct got_object_id_queue commits;
5991 struct got_pathlist_head merged_paths;
5992 const struct got_object_id_queue *parent_ids;
5993 struct got_object_qid *pid;
5994 struct got_histedit_list histedit_cmds;
5995 struct got_histedit_list_entry *hle;
5997 SIMPLEQ_INIT(&commits);
5998 TAILQ_INIT(&histedit_cmds);
5999 TAILQ_INIT(&merged_paths);
6001 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6002 switch (ch) {
6003 case 'a':
6004 abort_edit = 1;
6005 break;
6006 case 'c':
6007 continue_edit = 1;
6008 break;
6009 case 'F':
6010 edit_script_path = optarg;
6011 break;
6012 default:
6013 usage_histedit();
6014 /* NOTREACHED */
6018 argc -= optind;
6019 argv += optind;
6021 #ifndef PROFILE
6022 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6023 "unveil", NULL) == -1)
6024 err(1, "pledge");
6025 #endif
6026 if (abort_edit && continue_edit)
6027 usage_histedit();
6028 if (argc != 0)
6029 usage_histedit();
6032 * This command cannot apply unveil(2) in all cases because the
6033 * user may choose to run an editor to edit the histedit script
6034 * and to edit individual commit log messages.
6035 * unveil(2) traverses exec(2); if an editor is used we have to
6036 * apply unveil after edit script and log messages have been written.
6037 * XXX TODO: Make use of unveil(2) where possible.
6040 cwd = getcwd(NULL, 0);
6041 if (cwd == NULL) {
6042 error = got_error_from_errno("getcwd");
6043 goto done;
6045 error = got_worktree_open(&worktree, cwd);
6046 if (error)
6047 goto done;
6049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6050 if (error != NULL)
6051 goto done;
6053 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6054 if (error)
6055 goto done;
6056 if (rebase_in_progress) {
6057 error = got_error(GOT_ERR_REBASING);
6058 goto done;
6061 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6062 if (error)
6063 goto done;
6065 if (edit_in_progress && abort_edit) {
6066 error = got_worktree_histedit_continue(&resume_commit_id,
6067 &tmp_branch, &branch, &base_commit_id, &fileindex,
6068 worktree, repo);
6069 if (error)
6070 goto done;
6071 printf("Switching work tree to %s\n",
6072 got_ref_get_symref_target(branch));
6073 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6074 branch, base_commit_id, update_progress, &did_something);
6075 if (error)
6076 goto done;
6077 printf("Histedit of %s aborted\n",
6078 got_ref_get_symref_target(branch));
6079 goto done; /* nothing else to do */
6080 } else if (abort_edit) {
6081 error = got_error(GOT_ERR_NOT_HISTEDIT);
6082 goto done;
6085 if (continue_edit) {
6086 char *path;
6088 if (!edit_in_progress) {
6089 error = got_error(GOT_ERR_NOT_HISTEDIT);
6090 goto done;
6093 error = got_worktree_get_histedit_script_path(&path, worktree);
6094 if (error)
6095 goto done;
6097 error = histedit_load_list(&histedit_cmds, path, repo);
6098 free(path);
6099 if (error)
6100 goto done;
6102 error = got_worktree_histedit_continue(&resume_commit_id,
6103 &tmp_branch, &branch, &base_commit_id, &fileindex,
6104 worktree, repo);
6105 if (error)
6106 goto done;
6108 error = got_ref_resolve(&head_commit_id, repo, branch);
6109 if (error)
6110 goto done;
6112 error = got_object_open_as_commit(&commit, repo,
6113 head_commit_id);
6114 if (error)
6115 goto done;
6116 parent_ids = got_object_commit_get_parent_ids(commit);
6117 pid = SIMPLEQ_FIRST(parent_ids);
6118 if (pid == NULL) {
6119 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6120 goto done;
6122 error = collect_commits(&commits, head_commit_id, pid->id,
6123 base_commit_id, got_worktree_get_path_prefix(worktree),
6124 GOT_ERR_HISTEDIT_PATH, repo);
6125 got_object_commit_close(commit);
6126 commit = NULL;
6127 if (error)
6128 goto done;
6129 } else {
6130 if (edit_in_progress) {
6131 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6132 goto done;
6135 error = got_ref_open(&branch, repo,
6136 got_worktree_get_head_ref_name(worktree), 0);
6137 if (error != NULL)
6138 goto done;
6140 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6141 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6142 "will not edit commit history of a branch outside "
6143 "the \"refs/heads/\" reference namespace");
6144 goto done;
6147 error = got_ref_resolve(&head_commit_id, repo, branch);
6148 got_ref_close(branch);
6149 branch = NULL;
6150 if (error)
6151 goto done;
6153 error = got_object_open_as_commit(&commit, repo,
6154 head_commit_id);
6155 if (error)
6156 goto done;
6157 parent_ids = got_object_commit_get_parent_ids(commit);
6158 pid = SIMPLEQ_FIRST(parent_ids);
6159 if (pid == NULL) {
6160 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6161 goto done;
6163 error = collect_commits(&commits, head_commit_id, pid->id,
6164 got_worktree_get_base_commit_id(worktree),
6165 got_worktree_get_path_prefix(worktree),
6166 GOT_ERR_HISTEDIT_PATH, repo);
6167 got_object_commit_close(commit);
6168 commit = NULL;
6169 if (error)
6170 goto done;
6172 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6173 &base_commit_id, &fileindex, worktree, repo);
6174 if (error)
6175 goto done;
6177 if (edit_script_path) {
6178 error = histedit_load_list(&histedit_cmds,
6179 edit_script_path, repo);
6180 if (error) {
6181 got_worktree_histedit_abort(worktree, fileindex,
6182 repo, branch, base_commit_id,
6183 update_progress, &did_something);
6184 goto done;
6186 } else {
6187 error = histedit_edit_script(&histedit_cmds, &commits,
6188 repo);
6189 if (error) {
6190 got_worktree_histedit_abort(worktree, fileindex,
6191 repo, branch, base_commit_id,
6192 update_progress, &did_something);
6193 goto done;
6198 error = histedit_save_list(&histedit_cmds, worktree,
6199 repo);
6200 if (error) {
6201 got_worktree_histedit_abort(worktree, fileindex,
6202 repo, branch, base_commit_id,
6203 update_progress, &did_something);
6204 goto done;
6209 error = histedit_check_script(&histedit_cmds, &commits, repo);
6210 if (error)
6211 goto done;
6213 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6214 if (resume_commit_id) {
6215 if (got_object_id_cmp(hle->commit_id,
6216 resume_commit_id) != 0)
6217 continue;
6219 resume_commit_id = NULL;
6220 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6221 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6222 error = histedit_skip_commit(hle, worktree,
6223 repo);
6224 } else {
6225 error = histedit_commit(NULL, worktree,
6226 fileindex, tmp_branch, hle, repo);
6228 if (error)
6229 goto done;
6230 continue;
6233 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6234 error = histedit_skip_commit(hle, worktree, repo);
6235 if (error)
6236 goto done;
6237 continue;
6240 error = got_object_open_as_commit(&commit, repo,
6241 hle->commit_id);
6242 if (error)
6243 goto done;
6244 parent_ids = got_object_commit_get_parent_ids(commit);
6245 pid = SIMPLEQ_FIRST(parent_ids);
6247 error = got_worktree_histedit_merge_files(&merged_paths,
6248 worktree, fileindex, pid->id, hle->commit_id, repo,
6249 rebase_progress, &rebase_status, check_cancelled, NULL);
6250 if (error)
6251 goto done;
6252 got_object_commit_close(commit);
6253 commit = NULL;
6255 if (rebase_status == GOT_STATUS_CONFLICT) {
6256 got_worktree_rebase_pathlist_free(&merged_paths);
6257 break;
6260 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6261 char *id_str;
6262 error = got_object_id_str(&id_str, hle->commit_id);
6263 if (error)
6264 goto done;
6265 printf("Stopping histedit for amending commit %s\n",
6266 id_str);
6267 free(id_str);
6268 got_worktree_rebase_pathlist_free(&merged_paths);
6269 error = got_worktree_histedit_postpone(worktree,
6270 fileindex);
6271 goto done;
6274 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6275 error = histedit_skip_commit(hle, worktree, repo);
6276 if (error)
6277 goto done;
6278 continue;
6281 error = histedit_commit(&merged_paths, worktree, fileindex,
6282 tmp_branch, hle, repo);
6283 got_worktree_rebase_pathlist_free(&merged_paths);
6284 if (error)
6285 goto done;
6288 if (rebase_status == GOT_STATUS_CONFLICT) {
6289 error = got_worktree_histedit_postpone(worktree, fileindex);
6290 if (error)
6291 goto done;
6292 error = got_error_msg(GOT_ERR_CONFLICTS,
6293 "conflicts must be resolved before rebasing can continue");
6294 } else
6295 error = histedit_complete(worktree, fileindex, tmp_branch,
6296 branch, repo);
6297 done:
6298 got_object_id_queue_free(&commits);
6299 histedit_free_list(&histedit_cmds);
6300 free(head_commit_id);
6301 free(base_commit_id);
6302 free(resume_commit_id);
6303 if (commit)
6304 got_object_commit_close(commit);
6305 if (branch)
6306 got_ref_close(branch);
6307 if (tmp_branch)
6308 got_ref_close(tmp_branch);
6309 if (worktree)
6310 got_worktree_close(worktree);
6311 if (repo)
6312 got_repo_close(repo);
6313 return error;
6316 __dead static void
6317 usage_stage(void)
6319 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6320 "[file-path ...]\n",
6321 getprogname());
6322 exit(1);
6325 static const struct got_error *
6326 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6327 const char *path, struct got_object_id *blob_id,
6328 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6330 const struct got_error *err = NULL;
6331 char *id_str = NULL;
6333 if (staged_status != GOT_STATUS_ADD &&
6334 staged_status != GOT_STATUS_MODIFY &&
6335 staged_status != GOT_STATUS_DELETE)
6336 return NULL;
6338 if (staged_status == GOT_STATUS_ADD ||
6339 staged_status == GOT_STATUS_MODIFY)
6340 err = got_object_id_str(&id_str, staged_blob_id);
6341 else
6342 err = got_object_id_str(&id_str, blob_id);
6343 if (err)
6344 return err;
6346 printf("%s %c %s\n", id_str, staged_status, path);
6347 free(id_str);
6348 return NULL;
6351 static const struct got_error *
6352 cmd_stage(int argc, char *argv[])
6354 const struct got_error *error = NULL;
6355 struct got_repository *repo = NULL;
6356 struct got_worktree *worktree = NULL;
6357 char *cwd = NULL;
6358 struct got_pathlist_head paths;
6359 struct got_pathlist_entry *pe;
6360 int ch, list_stage = 0, pflag = 0;
6361 FILE *patch_script_file = NULL;
6362 const char *patch_script_path = NULL;
6363 struct choose_patch_arg cpa;
6365 TAILQ_INIT(&paths);
6367 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6368 switch (ch) {
6369 case 'l':
6370 list_stage = 1;
6371 break;
6372 case 'p':
6373 pflag = 1;
6374 break;
6375 case 'F':
6376 patch_script_path = optarg;
6377 break;
6378 default:
6379 usage_stage();
6380 /* NOTREACHED */
6384 argc -= optind;
6385 argv += optind;
6387 #ifndef PROFILE
6388 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6389 "unveil", NULL) == -1)
6390 err(1, "pledge");
6391 #endif
6392 if (list_stage && (pflag || patch_script_path))
6393 errx(1, "-l option cannot be used with other options");
6394 if (patch_script_path && !pflag)
6395 errx(1, "-F option can only be used together with -p option");
6397 cwd = getcwd(NULL, 0);
6398 if (cwd == NULL) {
6399 error = got_error_from_errno("getcwd");
6400 goto done;
6403 error = got_worktree_open(&worktree, cwd);
6404 if (error)
6405 goto done;
6407 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6408 if (error != NULL)
6409 goto done;
6411 if (patch_script_path) {
6412 patch_script_file = fopen(patch_script_path, "r");
6413 if (patch_script_file == NULL) {
6414 error = got_error_from_errno2("fopen",
6415 patch_script_path);
6416 goto done;
6419 error = apply_unveil(got_repo_get_path(repo), 0,
6420 got_worktree_get_root_path(worktree));
6421 if (error)
6422 goto done;
6424 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6425 if (error)
6426 goto done;
6428 if (list_stage)
6429 error = got_worktree_status(worktree, &paths, repo,
6430 print_stage, NULL, check_cancelled, NULL);
6431 else {
6432 cpa.patch_script_file = patch_script_file;
6433 cpa.action = "stage";
6434 error = got_worktree_stage(worktree, &paths,
6435 pflag ? NULL : print_status, NULL,
6436 pflag ? choose_patch : NULL, &cpa, repo);
6438 done:
6439 if (patch_script_file && fclose(patch_script_file) == EOF &&
6440 error == NULL)
6441 error = got_error_from_errno2("fclose", patch_script_path);
6442 if (repo)
6443 got_repo_close(repo);
6444 if (worktree)
6445 got_worktree_close(worktree);
6446 TAILQ_FOREACH(pe, &paths, entry)
6447 free((char *)pe->path);
6448 got_pathlist_free(&paths);
6449 free(cwd);
6450 return error;
6453 __dead static void
6454 usage_unstage(void)
6456 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6457 "[file-path ...]\n",
6458 getprogname());
6459 exit(1);
6463 static const struct got_error *
6464 cmd_unstage(int argc, char *argv[])
6466 const struct got_error *error = NULL;
6467 struct got_repository *repo = NULL;
6468 struct got_worktree *worktree = NULL;
6469 char *cwd = NULL;
6470 struct got_pathlist_head paths;
6471 struct got_pathlist_entry *pe;
6472 int ch, did_something = 0, pflag = 0;
6473 FILE *patch_script_file = NULL;
6474 const char *patch_script_path = NULL;
6475 struct choose_patch_arg cpa;
6477 TAILQ_INIT(&paths);
6479 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6480 switch (ch) {
6481 case 'p':
6482 pflag = 1;
6483 break;
6484 case 'F':
6485 patch_script_path = optarg;
6486 break;
6487 default:
6488 usage_unstage();
6489 /* NOTREACHED */
6493 argc -= optind;
6494 argv += optind;
6496 #ifndef PROFILE
6497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6498 "unveil", NULL) == -1)
6499 err(1, "pledge");
6500 #endif
6501 if (patch_script_path && !pflag)
6502 errx(1, "-F option can only be used together with -p option");
6504 cwd = getcwd(NULL, 0);
6505 if (cwd == NULL) {
6506 error = got_error_from_errno("getcwd");
6507 goto done;
6510 error = got_worktree_open(&worktree, cwd);
6511 if (error)
6512 goto done;
6514 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6515 if (error != NULL)
6516 goto done;
6518 if (patch_script_path) {
6519 patch_script_file = fopen(patch_script_path, "r");
6520 if (patch_script_file == NULL) {
6521 error = got_error_from_errno2("fopen",
6522 patch_script_path);
6523 goto done;
6527 error = apply_unveil(got_repo_get_path(repo), 0,
6528 got_worktree_get_root_path(worktree));
6529 if (error)
6530 goto done;
6532 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6533 if (error)
6534 goto done;
6536 cpa.patch_script_file = patch_script_file;
6537 cpa.action = "unstage";
6538 error = got_worktree_unstage(worktree, &paths, update_progress,
6539 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6540 done:
6541 if (patch_script_file && fclose(patch_script_file) == EOF &&
6542 error == NULL)
6543 error = got_error_from_errno2("fclose", patch_script_path);
6544 if (repo)
6545 got_repo_close(repo);
6546 if (worktree)
6547 got_worktree_close(worktree);
6548 TAILQ_FOREACH(pe, &paths, entry)
6549 free((char *)pe->path);
6550 got_pathlist_free(&paths);
6551 free(cwd);
6552 return error;
6555 __dead static void
6556 usage_cat(void)
6558 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6559 "arg1 [arg2 ...]\n", getprogname());
6560 exit(1);
6563 static const struct got_error *
6564 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6566 const struct got_error *err;
6567 struct got_blob_object *blob;
6569 err = got_object_open_as_blob(&blob, repo, id, 8192);
6570 if (err)
6571 return err;
6573 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6574 got_object_blob_close(blob);
6575 return err;
6578 static const struct got_error *
6579 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6581 const struct got_error *err;
6582 struct got_tree_object *tree;
6583 const struct got_tree_entries *entries;
6584 struct got_tree_entry *te;
6586 err = got_object_open_as_tree(&tree, repo, id);
6587 if (err)
6588 return err;
6590 entries = got_object_tree_get_entries(tree);
6591 te = SIMPLEQ_FIRST(&entries->head);
6592 while (te) {
6593 char *id_str;
6594 if (sigint_received || sigpipe_received)
6595 break;
6596 err = got_object_id_str(&id_str, te->id);
6597 if (err)
6598 break;
6599 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6600 free(id_str);
6601 te = SIMPLEQ_NEXT(te, entry);
6604 got_object_tree_close(tree);
6605 return err;
6608 static const struct got_error *
6609 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6611 const struct got_error *err;
6612 struct got_commit_object *commit;
6613 const struct got_object_id_queue *parent_ids;
6614 struct got_object_qid *pid;
6615 char *id_str = NULL;
6616 const char *logmsg = NULL;
6618 err = got_object_open_as_commit(&commit, repo, id);
6619 if (err)
6620 return err;
6622 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6623 if (err)
6624 goto done;
6626 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6627 parent_ids = got_object_commit_get_parent_ids(commit);
6628 fprintf(outfile, "numparents %d\n",
6629 got_object_commit_get_nparents(commit));
6630 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6631 char *pid_str;
6632 err = got_object_id_str(&pid_str, pid->id);
6633 if (err)
6634 goto done;
6635 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6636 free(pid_str);
6638 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6639 got_object_commit_get_author(commit),
6640 got_object_commit_get_author_time(commit));
6642 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6643 got_object_commit_get_author(commit),
6644 got_object_commit_get_committer_time(commit));
6646 logmsg = got_object_commit_get_logmsg_raw(commit);
6647 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6648 fprintf(outfile, "%s", logmsg);
6649 done:
6650 free(id_str);
6651 got_object_commit_close(commit);
6652 return err;
6655 static const struct got_error *
6656 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6658 const struct got_error *err;
6659 struct got_tag_object *tag;
6660 char *id_str = NULL;
6661 const char *tagmsg = NULL;
6663 err = got_object_open_as_tag(&tag, repo, id);
6664 if (err)
6665 return err;
6667 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6668 if (err)
6669 goto done;
6671 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6673 switch (got_object_tag_get_object_type(tag)) {
6674 case GOT_OBJ_TYPE_BLOB:
6675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6676 GOT_OBJ_LABEL_BLOB);
6677 break;
6678 case GOT_OBJ_TYPE_TREE:
6679 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6680 GOT_OBJ_LABEL_TREE);
6681 break;
6682 case GOT_OBJ_TYPE_COMMIT:
6683 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6684 GOT_OBJ_LABEL_COMMIT);
6685 break;
6686 case GOT_OBJ_TYPE_TAG:
6687 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6688 GOT_OBJ_LABEL_TAG);
6689 break;
6690 default:
6691 break;
6694 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6695 got_object_tag_get_name(tag));
6697 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6698 got_object_tag_get_tagger(tag),
6699 got_object_tag_get_tagger_time(tag));
6701 tagmsg = got_object_tag_get_message(tag);
6702 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6703 fprintf(outfile, "%s", tagmsg);
6704 done:
6705 free(id_str);
6706 got_object_tag_close(tag);
6707 return err;
6710 static const struct got_error *
6711 cmd_cat(int argc, char *argv[])
6713 const struct got_error *error;
6714 struct got_repository *repo = NULL;
6715 struct got_worktree *worktree = NULL;
6716 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6717 const char *commit_id_str = NULL;
6718 struct got_object_id *id = NULL, *commit_id = NULL;
6719 int ch, obj_type, i, force_path = 0;
6721 #ifndef PROFILE
6722 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6723 NULL) == -1)
6724 err(1, "pledge");
6725 #endif
6727 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6728 switch (ch) {
6729 case 'c':
6730 commit_id_str = optarg;
6731 break;
6732 case 'r':
6733 repo_path = realpath(optarg, NULL);
6734 if (repo_path == NULL)
6735 err(1, "-r option");
6736 got_path_strip_trailing_slashes(repo_path);
6737 break;
6738 case 'P':
6739 force_path = 1;
6740 break;
6741 default:
6742 usage_cat();
6743 /* NOTREACHED */
6747 argc -= optind;
6748 argv += optind;
6750 cwd = getcwd(NULL, 0);
6751 if (cwd == NULL) {
6752 error = got_error_from_errno("getcwd");
6753 goto done;
6755 error = got_worktree_open(&worktree, cwd);
6756 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6757 goto done;
6758 if (worktree) {
6759 if (repo_path == NULL) {
6760 repo_path = strdup(
6761 got_worktree_get_repo_path(worktree));
6762 if (repo_path == NULL) {
6763 error = got_error_from_errno("strdup");
6764 goto done;
6769 if (repo_path == NULL) {
6770 repo_path = getcwd(NULL, 0);
6771 if (repo_path == NULL)
6772 return got_error_from_errno("getcwd");
6775 error = got_repo_open(&repo, repo_path);
6776 free(repo_path);
6777 if (error != NULL)
6778 goto done;
6780 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6781 if (error)
6782 goto done;
6784 if (commit_id_str == NULL)
6785 commit_id_str = GOT_REF_HEAD;
6786 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6787 if (error)
6788 goto done;
6790 for (i = 0; i < argc; i++) {
6791 if (force_path) {
6792 error = got_object_id_by_path(&id, repo, commit_id,
6793 argv[i]);
6794 if (error)
6795 break;
6796 } else {
6797 error = match_object_id(&id, &label, argv[i],
6798 GOT_OBJ_TYPE_ANY, 0, repo);
6799 if (error) {
6800 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6801 error->code != GOT_ERR_NOT_REF)
6802 break;
6803 error = got_object_id_by_path(&id, repo,
6804 commit_id, argv[i]);
6805 if (error)
6806 break;
6810 error = got_object_get_type(&obj_type, repo, id);
6811 if (error)
6812 break;
6814 switch (obj_type) {
6815 case GOT_OBJ_TYPE_BLOB:
6816 error = cat_blob(id, repo, stdout);
6817 break;
6818 case GOT_OBJ_TYPE_TREE:
6819 error = cat_tree(id, repo, stdout);
6820 break;
6821 case GOT_OBJ_TYPE_COMMIT:
6822 error = cat_commit(id, repo, stdout);
6823 break;
6824 case GOT_OBJ_TYPE_TAG:
6825 error = cat_tag(id, repo, stdout);
6826 break;
6827 default:
6828 error = got_error(GOT_ERR_OBJ_TYPE);
6829 break;
6831 if (error)
6832 break;
6833 free(label);
6834 label = NULL;
6835 free(id);
6836 id = NULL;
6839 done:
6840 free(label);
6841 free(id);
6842 free(commit_id);
6843 if (worktree)
6844 got_worktree_close(worktree);
6845 if (repo) {
6846 const struct got_error *repo_error;
6847 repo_error = got_repo_close(repo);
6848 if (error == NULL)
6849 error = repo_error;
6851 return error;