Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 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 <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
40 #include "got_version.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_diff.h"
49 #include "got_commit_graph.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_opentemp.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 static volatile sig_atomic_t sigint_received;
59 static volatile sig_atomic_t sigpipe_received;
61 static void
62 catch_sigint(int signo)
63 {
64 sigint_received = 1;
65 }
67 static void
68 catch_sigpipe(int signo)
69 {
70 sigpipe_received = 1;
71 }
74 struct got_cmd {
75 const char *cmd_name;
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
79 };
81 __dead static void usage(int);
82 __dead static void usage_init(void);
83 __dead static void usage_import(void);
84 __dead static void usage_checkout(void);
85 __dead static void usage_update(void);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_status(void);
91 __dead static void usage_ref(void);
92 __dead static void usage_branch(void);
93 __dead static void usage_tag(void);
94 __dead static void usage_add(void);
95 __dead static void usage_remove(void);
96 __dead static void usage_revert(void);
97 __dead static void usage_commit(void);
98 __dead static void usage_cherrypick(void);
99 __dead static void usage_backout(void);
100 __dead static void usage_rebase(void);
101 __dead static void usage_histedit(void);
102 __dead static void usage_integrate(void);
103 __dead static void usage_stage(void);
104 __dead static void usage_unstage(void);
105 __dead static void usage_cat(void);
107 static const struct got_error* cmd_init(int, char *[]);
108 static const struct got_error* cmd_import(int, char *[]);
109 static const struct got_error* cmd_checkout(int, char *[]);
110 static const struct got_error* cmd_update(int, char *[]);
111 static const struct got_error* cmd_log(int, char *[]);
112 static const struct got_error* cmd_diff(int, char *[]);
113 static const struct got_error* cmd_blame(int, char *[]);
114 static const struct got_error* cmd_tree(int, char *[]);
115 static const struct got_error* cmd_status(int, char *[]);
116 static const struct got_error* cmd_ref(int, char *[]);
117 static const struct got_error* cmd_branch(int, char *[]);
118 static const struct got_error* cmd_tag(int, char *[]);
119 static const struct got_error* cmd_add(int, char *[]);
120 static const struct got_error* cmd_remove(int, char *[]);
121 static const struct got_error* cmd_revert(int, char *[]);
122 static const struct got_error* cmd_commit(int, char *[]);
123 static const struct got_error* cmd_cherrypick(int, char *[]);
124 static const struct got_error* cmd_backout(int, char *[]);
125 static const struct got_error* cmd_rebase(int, char *[]);
126 static const struct got_error* cmd_histedit(int, char *[]);
127 static const struct got_error* cmd_integrate(int, char *[]);
128 static const struct got_error* cmd_stage(int, char *[]);
129 static const struct got_error* cmd_unstage(int, char *[]);
130 static const struct got_error* cmd_cat(int, char *[]);
132 static struct got_cmd got_commands[] = {
133 { "init", cmd_init, usage_init, "in" },
134 { "import", cmd_import, usage_import, "im" },
135 { "checkout", cmd_checkout, usage_checkout, "co" },
136 { "update", cmd_update, usage_update, "up" },
137 { "log", cmd_log, usage_log, "" },
138 { "diff", cmd_diff, usage_diff, "di" },
139 { "blame", cmd_blame, usage_blame, "bl" },
140 { "tree", cmd_tree, usage_tree, "tr" },
141 { "status", cmd_status, usage_status, "st" },
142 { "ref", cmd_ref, usage_ref, "" },
143 { "branch", cmd_branch, usage_branch, "br" },
144 { "tag", cmd_tag, usage_tag, "" },
145 { "add", cmd_add, usage_add, "" },
146 { "remove", cmd_remove, usage_remove, "rm" },
147 { "revert", cmd_revert, usage_revert, "rv" },
148 { "commit", cmd_commit, usage_commit, "ci" },
149 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 { "backout", cmd_backout, usage_backout, "bo" },
151 { "rebase", cmd_rebase, usage_rebase, "rb" },
152 { "histedit", cmd_histedit, usage_histedit, "he" },
153 { "integrate", cmd_integrate, usage_integrate,"ig" },
154 { "stage", cmd_stage, usage_stage, "sg" },
155 { "unstage", cmd_unstage, usage_unstage, "ug" },
156 { "cat", cmd_cat, usage_cat, "" },
157 };
159 static void
160 list_commands(void)
162 int i;
164 fprintf(stderr, "commands:");
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct got_cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s", cmd->cmd_name);
169 fputc('\n', stderr);
172 int
173 main(int argc, char *argv[])
175 struct got_cmd *cmd;
176 unsigned int i;
177 int ch;
178 int hflag = 0, Vflag = 0;
180 setlocale(LC_CTYPE, "");
182 while ((ch = getopt(argc, argv, "hV")) != -1) {
183 switch (ch) {
184 case 'h':
185 hflag = 1;
186 break;
187 case 'V':
188 Vflag = 1;
189 break;
190 default:
191 usage(hflag);
192 /* NOTREACHED */
196 argc -= optind;
197 argv += optind;
198 optind = 0;
200 if (Vflag) {
201 got_version_print_str();
202 return 1;
205 if (argc <= 0)
206 usage(hflag);
208 signal(SIGINT, catch_sigint);
209 signal(SIGPIPE, catch_sigpipe);
211 for (i = 0; i < nitems(got_commands); i++) {
212 const struct got_error *error;
214 cmd = &got_commands[i];
216 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 strcmp(cmd->cmd_alias, argv[0]) != 0)
218 continue;
220 if (hflag)
221 got_commands[i].cmd_usage();
223 error = got_commands[i].cmd_main(argc, argv);
224 if (error && error->code != GOT_ERR_CANCELLED &&
225 error->code != GOT_ERR_PRIVSEP_EXIT &&
226 !(sigpipe_received &&
227 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
228 !(sigint_received &&
229 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
231 return 1;
234 return 0;
237 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
238 list_commands();
239 return 1;
242 __dead static void
243 usage(int hflag)
245 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
246 getprogname());
247 if (hflag)
248 list_commands();
249 exit(1);
252 static const struct got_error *
253 get_editor(char **abspath)
255 const struct got_error *err = NULL;
256 const char *editor;
258 *abspath = NULL;
260 editor = getenv("VISUAL");
261 if (editor == NULL)
262 editor = getenv("EDITOR");
264 if (editor) {
265 err = got_path_find_prog(abspath, editor);
266 if (err)
267 return err;
270 if (*abspath == NULL) {
271 *abspath = strdup("/bin/ed");
272 if (*abspath == NULL)
273 return got_error_from_errno("strdup");
276 return NULL;
279 static const struct got_error *
280 apply_unveil(const char *repo_path, int repo_read_only,
281 const char *worktree_path)
283 const struct got_error *err;
285 #ifdef PROFILE
286 if (unveil("gmon.out", "rwc") != 0)
287 return got_error_from_errno2("unveil", "gmon.out");
288 #endif
289 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 return got_error_from_errno2("unveil", repo_path);
292 if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 return got_error_from_errno2("unveil", worktree_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
299 if (err != NULL)
300 return err;
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
305 return NULL;
308 __dead static void
309 usage_init(void)
311 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
312 exit(1);
315 static const struct got_error *
316 cmd_init(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 char *repo_path = NULL;
320 int ch;
322 while ((ch = getopt(argc, argv, "")) != -1) {
323 switch (ch) {
324 default:
325 usage_init();
326 /* NOTREACHED */
330 argc -= optind;
331 argv += optind;
333 #ifndef PROFILE
334 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
335 err(1, "pledge");
336 #endif
337 if (argc != 1)
338 usage_init();
340 repo_path = strdup(argv[0]);
341 if (repo_path == NULL)
342 return got_error_from_errno("strdup");
344 got_path_strip_trailing_slashes(repo_path);
346 error = got_path_mkdir(repo_path);
347 if (error &&
348 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
349 goto done;
351 error = apply_unveil(repo_path, 0, NULL);
352 if (error)
353 goto done;
355 error = got_repo_init(repo_path);
356 if (error != NULL)
357 goto done;
359 done:
360 free(repo_path);
361 return error;
364 __dead static void
365 usage_import(void)
367 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 "[-r repository-path] [-I pattern] path\n", getprogname());
369 exit(1);
372 int
373 spawn_editor(const char *editor, const char *file)
375 pid_t pid;
376 sig_t sighup, sigint, sigquit;
377 int st = -1;
379 sighup = signal(SIGHUP, SIG_IGN);
380 sigint = signal(SIGINT, SIG_IGN);
381 sigquit = signal(SIGQUIT, SIG_IGN);
383 switch (pid = fork()) {
384 case -1:
385 goto doneediting;
386 case 0:
387 execl(editor, editor, file, (char *)NULL);
388 _exit(127);
391 while (waitpid(pid, &st, 0) == -1)
392 if (errno != EINTR)
393 break;
395 doneediting:
396 (void)signal(SIGHUP, sighup);
397 (void)signal(SIGINT, sigint);
398 (void)signal(SIGQUIT, sigquit);
400 if (!WIFEXITED(st)) {
401 errno = EINTR;
402 return -1;
405 return WEXITSTATUS(st);
408 static const struct got_error *
409 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 const char *initial_content)
412 const struct got_error *err = NULL;
413 char buf[1024];
414 struct stat st, st2;
415 FILE *fp;
416 int content_changed = 0;
417 size_t len;
419 *logmsg = NULL;
421 if (stat(logmsg_path, &st) == -1)
422 return got_error_from_errno2("stat", logmsg_path);
424 if (spawn_editor(editor, logmsg_path) == -1)
425 return got_error_from_errno("failed spawning editor");
427 if (stat(logmsg_path, &st2) == -1)
428 return got_error_from_errno("stat");
430 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 "no changes made to commit message, aborting");
434 *logmsg = malloc(st2.st_size + 1);
435 if (*logmsg == NULL)
436 return got_error_from_errno("malloc");
437 (*logmsg)[0] = '\0';
438 len = 0;
440 fp = fopen(logmsg_path, "r");
441 if (fp == NULL) {
442 err = got_error_from_errno("fopen");
443 goto done;
445 while (fgets(buf, sizeof(buf), fp) != NULL) {
446 if (!content_changed && strcmp(buf, initial_content) != 0)
447 content_changed = 1;
448 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 continue; /* remove comments and leading empty lines */
450 len = strlcat(*logmsg, buf, st2.st_size);
452 fclose(fp);
454 while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 (*logmsg)[len - 1] = '\0';
456 len--;
459 if (len == 0 || !content_changed)
460 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 "commit message cannot be empty, aborting");
462 done:
463 if (err) {
464 free(*logmsg);
465 *logmsg = NULL;
467 return err;
470 static const struct got_error *
471 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 const char *path_dir, const char *branch_name)
474 char *initial_content = NULL;
475 const struct got_error *err = NULL;
476 int fd;
478 if (asprintf(&initial_content,
479 "\n# %s to be imported to branch %s\n", path_dir,
480 branch_name) == -1)
481 return got_error_from_errno("asprintf");
483 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
484 if (err)
485 goto done;
487 dprintf(fd, initial_content);
488 close(fd);
490 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
491 done:
492 free(initial_content);
493 return err;
496 static const struct got_error *
497 import_progress(void *arg, const char *path)
499 printf("A %s\n", path);
500 return NULL;
503 static const struct got_error *
504 get_author(char **author, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 const char *got_author, *name, *email;
509 *author = NULL;
511 name = got_repo_get_gitconfig_author_name(repo);
512 email = got_repo_get_gitconfig_author_email(repo);
513 if (name && email) {
514 if (asprintf(author, "%s <%s>", name, email) == -1)
515 return got_error_from_errno("asprintf");
516 return NULL;
519 got_author = getenv("GOT_AUTHOR");
520 if (got_author == NULL) {
521 name = got_repo_get_global_gitconfig_author_name(repo);
522 email = got_repo_get_global_gitconfig_author_email(repo);
523 if (name && email) {
524 if (asprintf(author, "%s <%s>", name, email) == -1)
525 return got_error_from_errno("asprintf");
526 return NULL;
528 /* TODO: Look up user in password database? */
529 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 *author = strdup(got_author);
533 if (*author == NULL)
534 return got_error_from_errno("strdup");
536 /*
537 * Really dumb email address check; we're only doing this to
538 * avoid git's object parser breaking on commits we create.
539 */
540 while (*got_author && *got_author != '<')
541 got_author++;
542 if (*got_author != '<') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 goto done;
546 while (*got_author && *got_author != '@')
547 got_author++;
548 if (*got_author != '@') {
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 goto done;
552 while (*got_author && *got_author != '>')
553 got_author++;
554 if (*got_author != '>')
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 done:
557 if (err) {
558 free(*author);
559 *author = NULL;
561 return err;
564 static const struct got_error *
565 get_gitconfig_path(char **gitconfig_path)
567 const char *homedir = getenv("HOME");
569 *gitconfig_path = NULL;
570 if (homedir) {
571 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 return got_error_from_errno("asprintf");
575 return NULL;
578 static const struct got_error *
579 cmd_import(int argc, char *argv[])
581 const struct got_error *error = NULL;
582 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 const char *branch_name = "main";
585 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 struct got_repository *repo = NULL;
587 struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 struct got_object_id *new_commit_id = NULL;
589 int ch;
590 struct got_pathlist_head ignores;
591 struct got_pathlist_entry *pe;
592 int preserve_logmsg = 0;
594 TAILQ_INIT(&ignores);
596 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
597 switch (ch) {
598 case 'b':
599 branch_name = optarg;
600 break;
601 case 'm':
602 logmsg = strdup(optarg);
603 if (logmsg == NULL) {
604 error = got_error_from_errno("strdup");
605 goto done;
607 break;
608 case 'r':
609 repo_path = realpath(optarg, NULL);
610 if (repo_path == NULL) {
611 error = got_error_from_errno2("realpath",
612 optarg);
613 goto done;
615 break;
616 case 'I':
617 if (optarg[0] == '\0')
618 break;
619 error = got_pathlist_insert(&pe, &ignores, optarg,
620 NULL);
621 if (error)
622 goto done;
623 break;
624 default:
625 usage_import();
626 /* NOTREACHED */
630 argc -= optind;
631 argv += optind;
633 #ifndef PROFILE
634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
635 "unveil",
636 NULL) == -1)
637 err(1, "pledge");
638 #endif
639 if (argc != 1)
640 usage_import();
642 if (repo_path == NULL) {
643 repo_path = getcwd(NULL, 0);
644 if (repo_path == NULL)
645 return got_error_from_errno("getcwd");
647 got_path_strip_trailing_slashes(repo_path);
648 error = get_gitconfig_path(&gitconfig_path);
649 if (error)
650 goto done;
651 error = got_repo_open(&repo, repo_path, gitconfig_path);
652 if (error)
653 goto done;
655 error = get_author(&author, repo);
656 if (error)
657 return error;
659 /*
660 * Don't let the user create a branch name with a leading '-'.
661 * While technically a valid reference name, this case is usually
662 * an unintended typo.
663 */
664 if (branch_name[0] == '-')
665 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
667 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 error = got_error_from_errno("asprintf");
669 goto done;
672 error = got_ref_open(&branch_ref, repo, refname, 0);
673 if (error) {
674 if (error->code != GOT_ERR_NOT_REF)
675 goto done;
676 } else {
677 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 "import target branch already exists");
679 goto done;
682 path_dir = realpath(argv[0], NULL);
683 if (path_dir == NULL) {
684 error = got_error_from_errno2("realpath", argv[0]);
685 goto done;
687 got_path_strip_trailing_slashes(path_dir);
689 /*
690 * unveil(2) traverses exec(2); if an editor is used we have
691 * to apply unveil after the log message has been written.
692 */
693 if (logmsg == NULL || strlen(logmsg) == 0) {
694 error = get_editor(&editor);
695 if (error)
696 goto done;
697 free(logmsg);
698 error = collect_import_msg(&logmsg, &logmsg_path, editor,
699 path_dir, refname);
700 if (error) {
701 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
702 logmsg_path != NULL)
703 preserve_logmsg = 1;
704 goto done;
708 if (unveil(path_dir, "r") != 0) {
709 error = got_error_from_errno2("unveil", path_dir);
710 if (logmsg_path)
711 preserve_logmsg = 1;
712 goto done;
715 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
716 if (error) {
717 if (logmsg_path)
718 preserve_logmsg = 1;
719 goto done;
722 error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 author, &ignores, repo, import_progress, NULL);
724 if (error) {
725 if (logmsg_path)
726 preserve_logmsg = 1;
727 goto done;
730 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
731 if (error) {
732 if (logmsg_path)
733 preserve_logmsg = 1;
734 goto done;
737 error = got_ref_write(branch_ref, repo);
738 if (error) {
739 if (logmsg_path)
740 preserve_logmsg = 1;
741 goto done;
744 error = got_object_id_str(&id_str, new_commit_id);
745 if (error) {
746 if (logmsg_path)
747 preserve_logmsg = 1;
748 goto done;
751 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
752 if (error) {
753 if (error->code != GOT_ERR_NOT_REF) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
759 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
760 branch_ref);
761 if (error) {
762 if (logmsg_path)
763 preserve_logmsg = 1;
764 goto done;
767 error = got_ref_write(head_ref, repo);
768 if (error) {
769 if (logmsg_path)
770 preserve_logmsg = 1;
771 goto done;
775 printf("Created branch %s with commit %s\n",
776 got_ref_get_name(branch_ref), id_str);
777 done:
778 if (preserve_logmsg) {
779 fprintf(stderr, "%s: log message preserved in %s\n",
780 getprogname(), logmsg_path);
781 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 error = got_error_from_errno2("unlink", logmsg_path);
783 free(logmsg);
784 free(logmsg_path);
785 free(repo_path);
786 free(editor);
787 free(refname);
788 free(new_commit_id);
789 free(id_str);
790 free(author);
791 free(gitconfig_path);
792 if (branch_ref)
793 got_ref_close(branch_ref);
794 if (head_ref)
795 got_ref_close(head_ref);
796 return error;
799 __dead static void
800 usage_checkout(void)
802 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 "[-p prefix] repository-path [worktree-path]\n", getprogname());
804 exit(1);
807 static void
808 show_worktree_base_ref_warning(void)
810 fprintf(stderr, "%s: warning: could not create a reference "
811 "to the work tree's base commit; the commit could be "
812 "garbage-collected by Git; making the repository "
813 "writable and running 'got update' will prevent this\n",
814 getprogname());
817 struct got_checkout_progress_arg {
818 const char *worktree_path;
819 int had_base_commit_ref_error;
820 };
822 static const struct got_error *
823 checkout_progress(void *arg, unsigned char status, const char *path)
825 struct got_checkout_progress_arg *a = arg;
827 /* Base commit bump happens silently. */
828 if (status == GOT_STATUS_BUMP_BASE)
829 return NULL;
831 if (status == GOT_STATUS_BASE_REF_ERR) {
832 a->had_base_commit_ref_error = 1;
833 return NULL;
836 while (path[0] == '/')
837 path++;
839 printf("%c %s/%s\n", status, a->worktree_path, path);
840 return NULL;
843 static const struct got_error *
844 check_cancelled(void *arg)
846 if (sigint_received || sigpipe_received)
847 return got_error(GOT_ERR_CANCELLED);
848 return NULL;
851 static const struct got_error *
852 check_linear_ancestry(struct got_object_id *commit_id,
853 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 struct got_repository *repo)
856 const struct got_error *err = NULL;
857 struct got_object_id *yca_id;
859 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 commit_id, base_commit_id, repo, check_cancelled, NULL);
861 if (err)
862 return err;
864 if (yca_id == NULL)
865 return got_error(GOT_ERR_ANCESTRY);
867 /*
868 * Require a straight line of history between the target commit
869 * and the work tree's base commit.
871 * Non-linear situations such as this require a rebase:
873 * (commit) D F (base_commit)
874 * \ /
875 * C E
876 * \ /
877 * B (yca)
878 * |
879 * A
881 * 'got update' only handles linear cases:
882 * Update forwards in time: A (base/yca) - B - C - D (commit)
883 * Update backwards in time: D (base) - C - B - A (commit/yca)
884 */
885 if (allow_forwards_in_time_only) {
886 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 return got_error(GOT_ERR_ANCESTRY);
888 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
892 free(yca_id);
893 return NULL;
896 static const struct got_error *
897 check_same_branch(struct got_object_id *commit_id,
898 struct got_reference *head_ref, struct got_object_id *yca_id,
899 struct got_repository *repo)
901 const struct got_error *err = NULL;
902 struct got_commit_graph *graph = NULL;
903 struct got_object_id *head_commit_id = NULL;
904 int is_same_branch = 0;
906 err = got_ref_resolve(&head_commit_id, repo, head_ref);
907 if (err)
908 goto done;
910 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
911 is_same_branch = 1;
912 goto done;
914 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
915 is_same_branch = 1;
916 goto done;
919 err = got_commit_graph_open(&graph, "/", 1);
920 if (err)
921 goto done;
923 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 check_cancelled, NULL);
925 if (err)
926 goto done;
928 for (;;) {
929 struct got_object_id *id;
930 err = got_commit_graph_iter_next(&id, graph, repo,
931 check_cancelled, NULL);
932 if (err) {
933 if (err->code == GOT_ERR_ITER_COMPLETED)
934 err = NULL;
935 break;
938 if (id) {
939 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
940 break;
941 if (got_object_id_cmp(id, commit_id) == 0) {
942 is_same_branch = 1;
943 break;
947 done:
948 if (graph)
949 got_commit_graph_close(graph);
950 free(head_commit_id);
951 if (!err && !is_same_branch)
952 err = got_error(GOT_ERR_ANCESTRY);
953 return err;
956 static const struct got_error *
957 resolve_commit_arg(struct got_object_id **commit_id,
958 const char *commit_id_arg, struct got_repository *repo)
960 const struct got_error *err;
961 struct got_reference *ref;
962 struct got_tag_object *tag;
964 err = got_repo_object_match_tag(&tag, commit_id_arg,
965 GOT_OBJ_TYPE_COMMIT, repo);
966 if (err == NULL) {
967 *commit_id = got_object_id_dup(
968 got_object_tag_get_object_id(tag));
969 if (*commit_id == NULL)
970 err = got_error_from_errno("got_object_id_dup");
971 got_object_tag_close(tag);
972 return err;
973 } else if (err->code != GOT_ERR_NO_OBJ)
974 return err;
976 err = got_ref_open(&ref, repo, commit_id_arg, 0);
977 if (err == NULL) {
978 err = got_ref_resolve(commit_id, repo, ref);
979 got_ref_close(ref);
980 } else {
981 if (err->code != GOT_ERR_NOT_REF)
982 return err;
983 err = got_repo_match_object_id_prefix(commit_id,
984 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
986 return err;
989 static const struct got_error *
990 cmd_checkout(int argc, char *argv[])
992 const struct got_error *error = NULL;
993 struct got_repository *repo = NULL;
994 struct got_reference *head_ref = NULL;
995 struct got_worktree *worktree = NULL;
996 char *repo_path = NULL;
997 char *worktree_path = NULL;
998 const char *path_prefix = "";
999 const char *branch_name = GOT_REF_HEAD;
1000 char *commit_id_str = NULL;
1001 int ch, same_path_prefix;
1002 struct got_pathlist_head paths;
1003 struct got_checkout_progress_arg cpa;
1005 TAILQ_INIT(&paths);
1007 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1008 switch (ch) {
1009 case 'b':
1010 branch_name = optarg;
1011 break;
1012 case 'c':
1013 commit_id_str = strdup(optarg);
1014 if (commit_id_str == NULL)
1015 return got_error_from_errno("strdup");
1016 break;
1017 case 'p':
1018 path_prefix = optarg;
1019 break;
1020 default:
1021 usage_checkout();
1022 /* NOTREACHED */
1026 argc -= optind;
1027 argv += optind;
1029 #ifndef PROFILE
1030 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1031 "unveil", NULL) == -1)
1032 err(1, "pledge");
1033 #endif
1034 if (argc == 1) {
1035 char *cwd, *base, *dotgit;
1036 repo_path = realpath(argv[0], NULL);
1037 if (repo_path == NULL)
1038 return got_error_from_errno2("realpath", argv[0]);
1039 cwd = getcwd(NULL, 0);
1040 if (cwd == NULL) {
1041 error = got_error_from_errno("getcwd");
1042 goto done;
1044 if (path_prefix[0]) {
1045 base = basename(path_prefix);
1046 if (base == NULL) {
1047 error = got_error_from_errno2("basename",
1048 path_prefix);
1049 goto done;
1051 } else {
1052 base = basename(repo_path);
1053 if (base == NULL) {
1054 error = got_error_from_errno2("basename",
1055 repo_path);
1056 goto done;
1059 dotgit = strstr(base, ".git");
1060 if (dotgit)
1061 *dotgit = '\0';
1062 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1063 error = got_error_from_errno("asprintf");
1064 free(cwd);
1065 goto done;
1067 free(cwd);
1068 } else if (argc == 2) {
1069 repo_path = realpath(argv[0], NULL);
1070 if (repo_path == NULL) {
1071 error = got_error_from_errno2("realpath", argv[0]);
1072 goto done;
1074 worktree_path = realpath(argv[1], NULL);
1075 if (worktree_path == NULL) {
1076 if (errno != ENOENT) {
1077 error = got_error_from_errno2("realpath",
1078 argv[1]);
1079 goto done;
1081 worktree_path = strdup(argv[1]);
1082 if (worktree_path == NULL) {
1083 error = got_error_from_errno("strdup");
1084 goto done;
1087 } else
1088 usage_checkout();
1090 got_path_strip_trailing_slashes(repo_path);
1091 got_path_strip_trailing_slashes(worktree_path);
1093 error = got_repo_open(&repo, repo_path, NULL);
1094 if (error != NULL)
1095 goto done;
1097 /* Pre-create work tree path for unveil(2) */
1098 error = got_path_mkdir(worktree_path);
1099 if (error) {
1100 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1101 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1102 goto done;
1103 if (!got_path_dir_is_empty(worktree_path)) {
1104 error = got_error_path(worktree_path,
1105 GOT_ERR_DIR_NOT_EMPTY);
1106 goto done;
1110 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1111 if (error)
1112 goto done;
1114 error = got_ref_open(&head_ref, repo, branch_name, 0);
1115 if (error != NULL)
1116 goto done;
1118 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1119 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1120 goto done;
1122 error = got_worktree_open(&worktree, worktree_path);
1123 if (error != NULL)
1124 goto done;
1126 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1127 path_prefix);
1128 if (error != NULL)
1129 goto done;
1130 if (!same_path_prefix) {
1131 error = got_error(GOT_ERR_PATH_PREFIX);
1132 goto done;
1135 if (commit_id_str) {
1136 struct got_object_id *commit_id;
1137 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1138 if (error)
1139 goto done;
1140 error = check_linear_ancestry(commit_id,
1141 got_worktree_get_base_commit_id(worktree), 0, repo);
1142 if (error != NULL) {
1143 free(commit_id);
1144 goto done;
1146 error = check_same_branch(commit_id, head_ref, NULL, repo);
1147 if (error)
1148 goto done;
1149 error = got_worktree_set_base_commit_id(worktree, repo,
1150 commit_id);
1151 free(commit_id);
1152 if (error)
1153 goto done;
1156 error = got_pathlist_append(&paths, "", NULL);
1157 if (error)
1158 goto done;
1159 cpa.worktree_path = worktree_path;
1160 cpa.had_base_commit_ref_error = 0;
1161 error = got_worktree_checkout_files(worktree, &paths, repo,
1162 checkout_progress, &cpa, check_cancelled, NULL);
1163 if (error != NULL)
1164 goto done;
1166 printf("Now shut up and hack\n");
1167 if (cpa.had_base_commit_ref_error)
1168 show_worktree_base_ref_warning();
1170 done:
1171 got_pathlist_free(&paths);
1172 free(commit_id_str);
1173 free(repo_path);
1174 free(worktree_path);
1175 return error;
1178 __dead static void
1179 usage_update(void)
1181 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1182 getprogname());
1183 exit(1);
1186 static const struct got_error *
1187 update_progress(void *arg, unsigned char status, const char *path)
1189 int *did_something = arg;
1191 if (status == GOT_STATUS_EXISTS ||
1192 status == GOT_STATUS_BASE_REF_ERR)
1193 return NULL;
1195 *did_something = 1;
1197 /* Base commit bump happens silently. */
1198 if (status == GOT_STATUS_BUMP_BASE)
1199 return NULL;
1201 while (path[0] == '/')
1202 path++;
1203 printf("%c %s\n", status, path);
1204 return NULL;
1207 static const struct got_error *
1208 switch_head_ref(struct got_reference *head_ref,
1209 struct got_object_id *commit_id, struct got_worktree *worktree,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1213 char *base_id_str;
1214 int ref_has_moved = 0;
1216 /* Trivial case: switching between two different references. */
1217 if (strcmp(got_ref_get_name(head_ref),
1218 got_worktree_get_head_ref_name(worktree)) != 0) {
1219 printf("Switching work tree from %s to %s\n",
1220 got_worktree_get_head_ref_name(worktree),
1221 got_ref_get_name(head_ref));
1222 return got_worktree_set_head_ref(worktree, head_ref);
1225 err = check_linear_ancestry(commit_id,
1226 got_worktree_get_base_commit_id(worktree), 0, repo);
1227 if (err) {
1228 if (err->code != GOT_ERR_ANCESTRY)
1229 return err;
1230 ref_has_moved = 1;
1232 if (!ref_has_moved)
1233 return NULL;
1235 /* Switching to a rebased branch with the same reference name. */
1236 err = got_object_id_str(&base_id_str,
1237 got_worktree_get_base_commit_id(worktree));
1238 if (err)
1239 return err;
1240 printf("Reference %s now points at a different branch\n",
1241 got_worktree_get_head_ref_name(worktree));
1242 printf("Switching work tree from %s to %s\n", base_id_str,
1243 got_worktree_get_head_ref_name(worktree));
1244 return NULL;
1247 static const struct got_error *
1248 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1250 const struct got_error *err;
1251 int in_progress;
1253 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1254 if (err)
1255 return err;
1256 if (in_progress)
1257 return got_error(GOT_ERR_REBASING);
1259 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1260 if (err)
1261 return err;
1262 if (in_progress)
1263 return got_error(GOT_ERR_HISTEDIT_BUSY);
1265 return NULL;
1268 static const struct got_error *
1269 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1270 char *argv[], struct got_worktree *worktree)
1272 const struct got_error *err = NULL;
1273 char *path;
1274 int i;
1276 if (argc == 0) {
1277 path = strdup("");
1278 if (path == NULL)
1279 return got_error_from_errno("strdup");
1280 return got_pathlist_append(paths, path, NULL);
1283 for (i = 0; i < argc; i++) {
1284 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1285 if (err)
1286 break;
1287 err = got_pathlist_append(paths, path, NULL);
1288 if (err) {
1289 free(path);
1290 break;
1294 return err;
1297 static const struct got_error *
1298 cmd_update(int argc, char *argv[])
1300 const struct got_error *error = NULL;
1301 struct got_repository *repo = NULL;
1302 struct got_worktree *worktree = NULL;
1303 char *worktree_path = NULL;
1304 struct got_object_id *commit_id = NULL;
1305 char *commit_id_str = NULL;
1306 const char *branch_name = NULL;
1307 struct got_reference *head_ref = NULL;
1308 struct got_pathlist_head paths;
1309 struct got_pathlist_entry *pe;
1310 int ch, did_something = 0;
1312 TAILQ_INIT(&paths);
1314 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1315 switch (ch) {
1316 case 'b':
1317 branch_name = optarg;
1318 break;
1319 case 'c':
1320 commit_id_str = strdup(optarg);
1321 if (commit_id_str == NULL)
1322 return got_error_from_errno("strdup");
1323 break;
1324 default:
1325 usage_update();
1326 /* NOTREACHED */
1330 argc -= optind;
1331 argv += optind;
1333 #ifndef PROFILE
1334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1335 "unveil", NULL) == -1)
1336 err(1, "pledge");
1337 #endif
1338 worktree_path = getcwd(NULL, 0);
1339 if (worktree_path == NULL) {
1340 error = got_error_from_errno("getcwd");
1341 goto done;
1343 error = got_worktree_open(&worktree, worktree_path);
1344 if (error)
1345 goto done;
1347 error = check_rebase_or_histedit_in_progress(worktree);
1348 if (error)
1349 goto done;
1351 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1352 NULL);
1353 if (error != NULL)
1354 goto done;
1356 error = apply_unveil(got_repo_get_path(repo), 0,
1357 got_worktree_get_root_path(worktree));
1358 if (error)
1359 goto done;
1361 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1362 if (error)
1363 goto done;
1365 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1366 got_worktree_get_head_ref_name(worktree), 0);
1367 if (error != NULL)
1368 goto done;
1369 if (commit_id_str == NULL) {
1370 error = got_ref_resolve(&commit_id, repo, head_ref);
1371 if (error != NULL)
1372 goto done;
1373 error = got_object_id_str(&commit_id_str, commit_id);
1374 if (error != NULL)
1375 goto done;
1376 } else {
1377 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1378 free(commit_id_str);
1379 commit_id_str = NULL;
1380 if (error)
1381 goto done;
1382 error = got_object_id_str(&commit_id_str, commit_id);
1383 if (error)
1384 goto done;
1387 if (branch_name) {
1388 struct got_object_id *head_commit_id;
1389 TAILQ_FOREACH(pe, &paths, entry) {
1390 if (pe->path_len == 0)
1391 continue;
1392 error = got_error_msg(GOT_ERR_BAD_PATH,
1393 "switching between branches requires that "
1394 "the entire work tree gets updated");
1395 goto done;
1397 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1398 if (error)
1399 goto done;
1400 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1401 repo);
1402 free(head_commit_id);
1403 if (error != NULL)
1404 goto done;
1405 error = check_same_branch(commit_id, head_ref, NULL, repo);
1406 if (error)
1407 goto done;
1408 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1409 if (error)
1410 goto done;
1411 } else {
1412 error = check_linear_ancestry(commit_id,
1413 got_worktree_get_base_commit_id(worktree), 0, repo);
1414 if (error != NULL) {
1415 if (error->code == GOT_ERR_ANCESTRY)
1416 error = got_error(GOT_ERR_BRANCH_MOVED);
1417 goto done;
1419 error = check_same_branch(commit_id, head_ref, NULL, repo);
1420 if (error)
1421 goto done;
1424 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1425 commit_id) != 0) {
1426 error = got_worktree_set_base_commit_id(worktree, repo,
1427 commit_id);
1428 if (error)
1429 goto done;
1432 error = got_worktree_checkout_files(worktree, &paths, repo,
1433 update_progress, &did_something, check_cancelled, NULL);
1434 if (error != NULL)
1435 goto done;
1437 if (did_something)
1438 printf("Updated to commit %s\n", commit_id_str);
1439 else
1440 printf("Already up-to-date\n");
1441 done:
1442 free(worktree_path);
1443 TAILQ_FOREACH(pe, &paths, entry)
1444 free((char *)pe->path);
1445 got_pathlist_free(&paths);
1446 free(commit_id);
1447 free(commit_id_str);
1448 return error;
1451 static const struct got_error *
1452 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1453 const char *path, int diff_context, int ignore_whitespace,
1454 struct got_repository *repo)
1456 const struct got_error *err = NULL;
1457 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1459 if (blob_id1) {
1460 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1461 if (err)
1462 goto done;
1465 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1466 if (err)
1467 goto done;
1469 while (path[0] == '/')
1470 path++;
1471 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1472 ignore_whitespace, stdout);
1473 done:
1474 if (blob1)
1475 got_object_blob_close(blob1);
1476 got_object_blob_close(blob2);
1477 return err;
1480 static const struct got_error *
1481 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1482 const char *path, int diff_context, int ignore_whitespace,
1483 struct got_repository *repo)
1485 const struct got_error *err = NULL;
1486 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1487 struct got_diff_blob_output_unidiff_arg arg;
1489 if (tree_id1) {
1490 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1491 if (err)
1492 goto done;
1495 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1496 if (err)
1497 goto done;
1499 arg.diff_context = diff_context;
1500 arg.ignore_whitespace = ignore_whitespace;
1501 arg.outfile = stdout;
1502 while (path[0] == '/')
1503 path++;
1504 err = got_diff_tree(tree1, tree2, path, path, repo,
1505 got_diff_blob_output_unidiff, &arg, 1);
1506 done:
1507 if (tree1)
1508 got_object_tree_close(tree1);
1509 if (tree2)
1510 got_object_tree_close(tree2);
1511 return err;
1514 static const struct got_error *
1515 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1516 const char *path, int diff_context, struct got_repository *repo)
1518 const struct got_error *err = NULL;
1519 struct got_commit_object *pcommit = NULL;
1520 char *id_str1 = NULL, *id_str2 = NULL;
1521 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1522 struct got_object_qid *qid;
1524 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1525 if (qid != NULL) {
1526 err = got_object_open_as_commit(&pcommit, repo,
1527 qid->id);
1528 if (err)
1529 return err;
1532 if (path && path[0] != '\0') {
1533 int obj_type;
1534 err = got_object_id_by_path(&obj_id2, repo, id, path);
1535 if (err)
1536 goto done;
1537 err = got_object_id_str(&id_str2, obj_id2);
1538 if (err) {
1539 free(obj_id2);
1540 goto done;
1542 if (pcommit) {
1543 err = got_object_id_by_path(&obj_id1, repo,
1544 qid->id, path);
1545 if (err) {
1546 free(obj_id2);
1547 goto done;
1549 err = got_object_id_str(&id_str1, obj_id1);
1550 if (err) {
1551 free(obj_id2);
1552 goto done;
1555 err = got_object_get_type(&obj_type, repo, obj_id2);
1556 if (err) {
1557 free(obj_id2);
1558 goto done;
1560 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 switch (obj_type) {
1562 case GOT_OBJ_TYPE_BLOB:
1563 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1564 0, repo);
1565 break;
1566 case GOT_OBJ_TYPE_TREE:
1567 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1568 0, repo);
1569 break;
1570 default:
1571 err = got_error(GOT_ERR_OBJ_TYPE);
1572 break;
1574 free(obj_id1);
1575 free(obj_id2);
1576 } else {
1577 obj_id2 = got_object_commit_get_tree_id(commit);
1578 err = got_object_id_str(&id_str2, obj_id2);
1579 if (err)
1580 goto done;
1581 obj_id1 = got_object_commit_get_tree_id(pcommit);
1582 err = got_object_id_str(&id_str1, obj_id1);
1583 if (err)
1584 goto done;
1585 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1586 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1589 done:
1590 free(id_str1);
1591 free(id_str2);
1592 if (pcommit)
1593 got_object_commit_close(pcommit);
1594 return err;
1597 static char *
1598 get_datestr(time_t *time, char *datebuf)
1600 struct tm mytm, *tm;
1601 char *p, *s;
1603 tm = gmtime_r(time, &mytm);
1604 if (tm == NULL)
1605 return NULL;
1606 s = asctime_r(tm, datebuf);
1607 if (s == NULL)
1608 return NULL;
1609 p = strchr(s, '\n');
1610 if (p)
1611 *p = '\0';
1612 return s;
1615 static const struct got_error *
1616 match_logmsg(int *have_match, struct got_object_id *id,
1617 struct got_commit_object *commit, regex_t *regex)
1619 const struct got_error *err = NULL;
1620 regmatch_t regmatch;
1621 char *id_str = NULL, *logmsg = NULL;
1623 *have_match = 0;
1625 err = got_object_id_str(&id_str, id);
1626 if (err)
1627 return err;
1629 err = got_object_commit_get_logmsg(&logmsg, commit);
1630 if (err)
1631 goto done;
1633 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1634 *have_match = 1;
1635 done:
1636 free(id_str);
1637 free(logmsg);
1638 return err;
1641 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1643 static const struct got_error *
1644 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1645 struct got_repository *repo, const char *path, int show_patch,
1646 int diff_context, struct got_reflist_head *refs)
1648 const struct got_error *err = NULL;
1649 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1650 char datebuf[26];
1651 time_t committer_time;
1652 const char *author, *committer;
1653 char *refs_str = NULL;
1654 struct got_reflist_entry *re;
1656 SIMPLEQ_FOREACH(re, refs, entry) {
1657 char *s;
1658 const char *name;
1659 struct got_tag_object *tag = NULL;
1660 int cmp;
1662 name = got_ref_get_name(re->ref);
1663 if (strcmp(name, GOT_REF_HEAD) == 0)
1664 continue;
1665 if (strncmp(name, "refs/", 5) == 0)
1666 name += 5;
1667 if (strncmp(name, "got/", 4) == 0)
1668 continue;
1669 if (strncmp(name, "heads/", 6) == 0)
1670 name += 6;
1671 if (strncmp(name, "remotes/", 8) == 0)
1672 name += 8;
1673 if (strncmp(name, "tags/", 5) == 0) {
1674 err = got_object_open_as_tag(&tag, repo, re->id);
1675 if (err) {
1676 if (err->code != GOT_ERR_OBJ_TYPE)
1677 return err;
1678 /* Ref points at something other than a tag. */
1679 err = NULL;
1680 tag = NULL;
1683 cmp = got_object_id_cmp(tag ?
1684 got_object_tag_get_object_id(tag) : re->id, id);
1685 if (tag)
1686 got_object_tag_close(tag);
1687 if (cmp != 0)
1688 continue;
1689 s = refs_str;
1690 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1691 name) == -1) {
1692 err = got_error_from_errno("asprintf");
1693 free(s);
1694 return err;
1696 free(s);
1698 err = got_object_id_str(&id_str, id);
1699 if (err)
1700 return err;
1702 printf(GOT_COMMIT_SEP_STR);
1703 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1704 refs_str ? refs_str : "", refs_str ? ")" : "");
1705 free(id_str);
1706 id_str = NULL;
1707 free(refs_str);
1708 refs_str = NULL;
1709 printf("from: %s\n", got_object_commit_get_author(commit));
1710 committer_time = got_object_commit_get_committer_time(commit);
1711 datestr = get_datestr(&committer_time, datebuf);
1712 if (datestr)
1713 printf("date: %s UTC\n", datestr);
1714 author = got_object_commit_get_author(commit);
1715 committer = got_object_commit_get_committer(commit);
1716 if (strcmp(author, committer) != 0)
1717 printf("via: %s\n", committer);
1718 if (got_object_commit_get_nparents(commit) > 1) {
1719 const struct got_object_id_queue *parent_ids;
1720 struct got_object_qid *qid;
1721 int n = 1;
1722 parent_ids = got_object_commit_get_parent_ids(commit);
1723 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1724 err = got_object_id_str(&id_str, qid->id);
1725 if (err)
1726 return err;
1727 printf("parent %d: %s\n", n++, id_str);
1728 free(id_str);
1732 err = got_object_commit_get_logmsg(&logmsg0, commit);
1733 if (err)
1734 return err;
1736 logmsg = logmsg0;
1737 do {
1738 line = strsep(&logmsg, "\n");
1739 if (line)
1740 printf(" %s\n", line);
1741 } while (line);
1742 free(logmsg0);
1744 if (show_patch) {
1745 err = print_patch(commit, id, path, diff_context, repo);
1746 if (err == 0)
1747 printf("\n");
1750 if (fflush(stdout) != 0 && err == NULL)
1751 err = got_error_from_errno("fflush");
1752 return err;
1755 static const struct got_error *
1756 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1757 const char *path, int show_patch, const char *search_pattern,
1758 int diff_context, int limit, int first_parent_traversal,
1759 struct got_reflist_head *refs)
1761 const struct got_error *err;
1762 struct got_commit_graph *graph;
1763 regex_t regex;
1764 int have_match;
1766 if (search_pattern &&
1767 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1768 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1770 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1771 if (err)
1772 return err;
1773 err = got_commit_graph_iter_start(graph, root_id, repo,
1774 check_cancelled, NULL);
1775 if (err)
1776 goto done;
1777 for (;;) {
1778 struct got_commit_object *commit;
1779 struct got_object_id *id;
1781 if (sigint_received || sigpipe_received)
1782 break;
1784 err = got_commit_graph_iter_next(&id, graph, repo,
1785 check_cancelled, NULL);
1786 if (err) {
1787 if (err->code == GOT_ERR_ITER_COMPLETED)
1788 err = NULL;
1789 break;
1791 if (id == NULL)
1792 break;
1794 err = got_object_open_as_commit(&commit, repo, id);
1795 if (err)
1796 break;
1798 if (search_pattern) {
1799 err = match_logmsg(&have_match, id, commit, &regex);
1800 if (err) {
1801 got_object_commit_close(commit);
1802 break;
1804 if (have_match == 0) {
1805 got_object_commit_close(commit);
1806 continue;
1810 err = print_commit(commit, id, repo, path, show_patch,
1811 diff_context, refs);
1812 got_object_commit_close(commit);
1813 if (err || (limit && --limit == 0))
1814 break;
1816 done:
1817 if (search_pattern)
1818 regfree(&regex);
1819 got_commit_graph_close(graph);
1820 return err;
1823 __dead static void
1824 usage_log(void)
1826 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1827 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1828 exit(1);
1831 static int
1832 get_default_log_limit(void)
1834 const char *got_default_log_limit;
1835 long long n;
1836 const char *errstr;
1838 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1839 if (got_default_log_limit == NULL)
1840 return 0;
1841 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1842 if (errstr != NULL)
1843 return 0;
1844 return n;
1847 static const struct got_error *
1848 cmd_log(int argc, char *argv[])
1850 const struct got_error *error;
1851 struct got_repository *repo = NULL;
1852 struct got_worktree *worktree = NULL;
1853 struct got_commit_object *commit = NULL;
1854 struct got_object_id *id = NULL;
1855 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1856 const char *start_commit = NULL, *search_pattern = NULL;
1857 int diff_context = -1, ch;
1858 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1859 const char *errstr;
1860 struct got_reflist_head refs;
1862 SIMPLEQ_INIT(&refs);
1864 #ifndef PROFILE
1865 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1866 NULL)
1867 == -1)
1868 err(1, "pledge");
1869 #endif
1871 limit = get_default_log_limit();
1873 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1874 switch (ch) {
1875 case 'p':
1876 show_patch = 1;
1877 break;
1878 case 'c':
1879 start_commit = optarg;
1880 break;
1881 case 'C':
1882 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1883 &errstr);
1884 if (errstr != NULL)
1885 err(1, "-C option %s", errstr);
1886 break;
1887 case 'l':
1888 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1889 if (errstr != NULL)
1890 err(1, "-l option %s", errstr);
1891 break;
1892 case 'f':
1893 first_parent_traversal = 1;
1894 break;
1895 case 'r':
1896 repo_path = realpath(optarg, NULL);
1897 if (repo_path == NULL)
1898 return got_error_from_errno2("realpath",
1899 optarg);
1900 got_path_strip_trailing_slashes(repo_path);
1901 break;
1902 case 's':
1903 search_pattern = optarg;
1904 break;
1905 default:
1906 usage_log();
1907 /* NOTREACHED */
1911 argc -= optind;
1912 argv += optind;
1914 if (diff_context == -1)
1915 diff_context = 3;
1916 else if (!show_patch)
1917 errx(1, "-C reguires -p");
1919 cwd = getcwd(NULL, 0);
1920 if (cwd == NULL) {
1921 error = got_error_from_errno("getcwd");
1922 goto done;
1925 error = got_worktree_open(&worktree, cwd);
1926 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1927 goto done;
1928 error = NULL;
1930 if (argc == 0) {
1931 path = strdup("");
1932 if (path == NULL) {
1933 error = got_error_from_errno("strdup");
1934 goto done;
1936 } else if (argc == 1) {
1937 if (worktree) {
1938 error = got_worktree_resolve_path(&path, worktree,
1939 argv[0]);
1940 if (error)
1941 goto done;
1942 } else {
1943 path = strdup(argv[0]);
1944 if (path == NULL) {
1945 error = got_error_from_errno("strdup");
1946 goto done;
1949 } else
1950 usage_log();
1952 if (repo_path == NULL) {
1953 repo_path = worktree ?
1954 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1956 if (repo_path == NULL) {
1957 error = got_error_from_errno("strdup");
1958 goto done;
1961 error = got_repo_open(&repo, repo_path, NULL);
1962 if (error != NULL)
1963 goto done;
1965 error = apply_unveil(got_repo_get_path(repo), 1,
1966 worktree ? got_worktree_get_root_path(worktree) : NULL);
1967 if (error)
1968 goto done;
1970 if (start_commit == NULL) {
1971 struct got_reference *head_ref;
1972 error = got_ref_open(&head_ref, repo,
1973 worktree ? got_worktree_get_head_ref_name(worktree)
1974 : GOT_REF_HEAD, 0);
1975 if (error != NULL)
1976 return error;
1977 error = got_ref_resolve(&id, repo, head_ref);
1978 got_ref_close(head_ref);
1979 if (error != NULL)
1980 return error;
1981 error = got_object_open_as_commit(&commit, repo, id);
1982 } else {
1983 struct got_reference *ref;
1984 error = got_ref_open(&ref, repo, start_commit, 0);
1985 if (error == NULL) {
1986 int obj_type;
1987 error = got_ref_resolve(&id, repo, ref);
1988 got_ref_close(ref);
1989 if (error != NULL)
1990 goto done;
1991 error = got_object_get_type(&obj_type, repo, id);
1992 if (error != NULL)
1993 goto done;
1994 if (obj_type == GOT_OBJ_TYPE_TAG) {
1995 struct got_tag_object *tag;
1996 error = got_object_open_as_tag(&tag, repo, id);
1997 if (error != NULL)
1998 goto done;
1999 if (got_object_tag_get_object_type(tag) !=
2000 GOT_OBJ_TYPE_COMMIT) {
2001 got_object_tag_close(tag);
2002 error = got_error(GOT_ERR_OBJ_TYPE);
2003 goto done;
2005 free(id);
2006 id = got_object_id_dup(
2007 got_object_tag_get_object_id(tag));
2008 if (id == NULL)
2009 error = got_error_from_errno(
2010 "got_object_id_dup");
2011 got_object_tag_close(tag);
2012 if (error)
2013 goto done;
2014 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2015 error = got_error(GOT_ERR_OBJ_TYPE);
2016 goto done;
2018 error = got_object_open_as_commit(&commit, repo, id);
2019 if (error != NULL)
2020 goto done;
2022 if (commit == NULL) {
2023 error = got_repo_match_object_id_prefix(&id,
2024 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2025 if (error != NULL)
2026 return error;
2029 if (error != NULL)
2030 goto done;
2032 if (worktree) {
2033 const char *prefix = got_worktree_get_path_prefix(worktree);
2034 char *p;
2035 if (asprintf(&p, "%s%s%s", prefix,
2036 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2037 error = got_error_from_errno("asprintf");
2038 goto done;
2040 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2041 free(p);
2042 } else
2043 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2044 if (error != NULL)
2045 goto done;
2046 if (in_repo_path) {
2047 free(path);
2048 path = in_repo_path;
2051 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2052 if (error)
2053 goto done;
2055 error = print_commits(id, repo, path, show_patch, search_pattern,
2056 diff_context, limit, first_parent_traversal, &refs);
2057 done:
2058 free(path);
2059 free(repo_path);
2060 free(cwd);
2061 free(id);
2062 if (worktree)
2063 got_worktree_close(worktree);
2064 if (repo) {
2065 const struct got_error *repo_error;
2066 repo_error = got_repo_close(repo);
2067 if (error == NULL)
2068 error = repo_error;
2070 got_ref_list_free(&refs);
2071 return error;
2074 __dead static void
2075 usage_diff(void)
2077 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2078 "[-w] [object1 object2 | path]\n", getprogname());
2079 exit(1);
2082 struct print_diff_arg {
2083 struct got_repository *repo;
2084 struct got_worktree *worktree;
2085 int diff_context;
2086 const char *id_str;
2087 int header_shown;
2088 int diff_staged;
2089 int ignore_whitespace;
2092 static const struct got_error *
2093 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2094 const char *path, struct got_object_id *blob_id,
2095 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2096 int dirfd, const char *de_name)
2098 struct print_diff_arg *a = arg;
2099 const struct got_error *err = NULL;
2100 struct got_blob_object *blob1 = NULL;
2101 int fd = -1;
2102 FILE *f2 = NULL;
2103 char *abspath = NULL, *label1 = NULL;
2104 struct stat sb;
2106 if (a->diff_staged) {
2107 if (staged_status != GOT_STATUS_MODIFY &&
2108 staged_status != GOT_STATUS_ADD &&
2109 staged_status != GOT_STATUS_DELETE)
2110 return NULL;
2111 } else {
2112 if (staged_status == GOT_STATUS_DELETE)
2113 return NULL;
2114 if (status == GOT_STATUS_NONEXISTENT)
2115 return got_error_set_errno(ENOENT, path);
2116 if (status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_ADD &&
2118 status != GOT_STATUS_DELETE &&
2119 status != GOT_STATUS_CONFLICT)
2120 return NULL;
2123 if (!a->header_shown) {
2124 printf("diff %s %s%s\n", a->id_str,
2125 got_worktree_get_root_path(a->worktree),
2126 a->diff_staged ? " (staged changes)" : "");
2127 a->header_shown = 1;
2130 if (a->diff_staged) {
2131 const char *label1 = NULL, *label2 = NULL;
2132 switch (staged_status) {
2133 case GOT_STATUS_MODIFY:
2134 label1 = path;
2135 label2 = path;
2136 break;
2137 case GOT_STATUS_ADD:
2138 label2 = path;
2139 break;
2140 case GOT_STATUS_DELETE:
2141 label1 = path;
2142 break;
2143 default:
2144 return got_error(GOT_ERR_FILE_STATUS);
2146 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2147 label1, label2, a->diff_context, a->ignore_whitespace,
2148 a->repo, stdout);
2151 if (staged_status == GOT_STATUS_ADD ||
2152 staged_status == GOT_STATUS_MODIFY) {
2153 char *id_str;
2154 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2155 8192);
2156 if (err)
2157 goto done;
2158 err = got_object_id_str(&id_str, staged_blob_id);
2159 if (err)
2160 goto done;
2161 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2162 err = got_error_from_errno("asprintf");
2163 free(id_str);
2164 goto done;
2166 free(id_str);
2167 } else if (status != GOT_STATUS_ADD) {
2168 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2169 if (err)
2170 goto done;
2173 if (status != GOT_STATUS_DELETE) {
2174 if (asprintf(&abspath, "%s/%s",
2175 got_worktree_get_root_path(a->worktree), path) == -1) {
2176 err = got_error_from_errno("asprintf");
2177 goto done;
2180 if (dirfd != -1) {
2181 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2182 if (fd == -1) {
2183 err = got_error_from_errno2("openat", abspath);
2184 goto done;
2186 } else {
2187 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2188 if (fd == -1) {
2189 err = got_error_from_errno2("open", abspath);
2190 goto done;
2193 if (fstat(fd, &sb) == -1) {
2194 err = got_error_from_errno2("fstat", abspath);
2195 goto done;
2197 f2 = fdopen(fd, "r");
2198 if (f2 == NULL) {
2199 err = got_error_from_errno2("fdopen", abspath);
2200 goto done;
2202 fd = -1;
2203 } else
2204 sb.st_size = 0;
2206 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2207 a->diff_context, a->ignore_whitespace, stdout);
2208 done:
2209 if (blob1)
2210 got_object_blob_close(blob1);
2211 if (f2 && fclose(f2) == EOF && err == NULL)
2212 err = got_error_from_errno("fclose");
2213 if (fd != -1 && close(fd) == -1 && err == NULL)
2214 err = got_error_from_errno("close");
2215 free(abspath);
2216 return err;
2219 static const struct got_error *
2220 match_object_id(struct got_object_id **id, char **label,
2221 const char *id_str, int obj_type, int resolve_tags,
2222 struct got_repository *repo)
2224 const struct got_error *err;
2225 struct got_tag_object *tag;
2226 struct got_reference *ref = NULL;
2228 *id = NULL;
2229 *label = NULL;
2231 if (resolve_tags) {
2232 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2233 repo);
2234 if (err == NULL) {
2235 *id = got_object_id_dup(
2236 got_object_tag_get_object_id(tag));
2237 if (*id == NULL)
2238 err = got_error_from_errno("got_object_id_dup");
2239 else if (asprintf(label, "refs/tags/%s",
2240 got_object_tag_get_name(tag)) == -1) {
2241 err = got_error_from_errno("asprintf");
2242 free(*id);
2243 *id = NULL;
2245 got_object_tag_close(tag);
2246 return err;
2247 } else if (err->code != GOT_ERR_NO_OBJ)
2248 return err;
2251 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2252 if (err) {
2253 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2254 return err;
2255 err = got_ref_open(&ref, repo, id_str, 0);
2256 if (err != NULL)
2257 goto done;
2258 *label = strdup(got_ref_get_name(ref));
2259 if (*label == NULL) {
2260 err = got_error_from_errno("strdup");
2261 goto done;
2263 err = got_ref_resolve(id, repo, ref);
2264 } else {
2265 err = got_object_id_str(label, *id);
2266 if (*label == NULL) {
2267 err = got_error_from_errno("strdup");
2268 goto done;
2271 done:
2272 if (ref)
2273 got_ref_close(ref);
2274 return err;
2278 static const struct got_error *
2279 cmd_diff(int argc, char *argv[])
2281 const struct got_error *error;
2282 struct got_repository *repo = NULL;
2283 struct got_worktree *worktree = NULL;
2284 char *cwd = NULL, *repo_path = NULL;
2285 struct got_object_id *id1 = NULL, *id2 = NULL;
2286 const char *id_str1 = NULL, *id_str2 = NULL;
2287 char *label1 = NULL, *label2 = NULL;
2288 int type1, type2;
2289 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2290 const char *errstr;
2291 char *path = NULL;
2293 #ifndef PROFILE
2294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2295 NULL) == -1)
2296 err(1, "pledge");
2297 #endif
2299 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2300 switch (ch) {
2301 case 'C':
2302 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2303 &errstr);
2304 if (errstr != NULL)
2305 err(1, "-C option %s", errstr);
2306 break;
2307 case 'r':
2308 repo_path = realpath(optarg, NULL);
2309 if (repo_path == NULL)
2310 return got_error_from_errno2("realpath",
2311 optarg);
2312 got_path_strip_trailing_slashes(repo_path);
2313 break;
2314 case 's':
2315 diff_staged = 1;
2316 break;
2317 case 'w':
2318 ignore_whitespace = 1;
2319 break;
2320 default:
2321 usage_diff();
2322 /* NOTREACHED */
2326 argc -= optind;
2327 argv += optind;
2329 cwd = getcwd(NULL, 0);
2330 if (cwd == NULL) {
2331 error = got_error_from_errno("getcwd");
2332 goto done;
2334 error = got_worktree_open(&worktree, cwd);
2335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2336 goto done;
2337 if (argc <= 1) {
2338 if (worktree == NULL) {
2339 error = got_error(GOT_ERR_NOT_WORKTREE);
2340 goto done;
2342 if (repo_path)
2343 errx(1,
2344 "-r option can't be used when diffing a work tree");
2345 repo_path = strdup(got_worktree_get_repo_path(worktree));
2346 if (repo_path == NULL) {
2347 error = got_error_from_errno("strdup");
2348 goto done;
2350 if (argc == 1) {
2351 error = got_worktree_resolve_path(&path, worktree,
2352 argv[0]);
2353 if (error)
2354 goto done;
2355 } else {
2356 path = strdup("");
2357 if (path == NULL) {
2358 error = got_error_from_errno("strdup");
2359 goto done;
2362 } else if (argc == 2) {
2363 if (diff_staged)
2364 errx(1, "-s option can't be used when diffing "
2365 "objects in repository");
2366 id_str1 = argv[0];
2367 id_str2 = argv[1];
2368 if (worktree && repo_path == NULL) {
2369 repo_path =
2370 strdup(got_worktree_get_repo_path(worktree));
2371 if (repo_path == NULL) {
2372 error = got_error_from_errno("strdup");
2373 goto done;
2376 } else
2377 usage_diff();
2379 if (repo_path == NULL) {
2380 repo_path = getcwd(NULL, 0);
2381 if (repo_path == NULL)
2382 return got_error_from_errno("getcwd");
2385 error = got_repo_open(&repo, repo_path, NULL);
2386 free(repo_path);
2387 if (error != NULL)
2388 goto done;
2390 error = apply_unveil(got_repo_get_path(repo), 1,
2391 worktree ? got_worktree_get_root_path(worktree) : NULL);
2392 if (error)
2393 goto done;
2395 if (argc <= 1) {
2396 struct print_diff_arg arg;
2397 struct got_pathlist_head paths;
2398 char *id_str;
2400 TAILQ_INIT(&paths);
2402 error = got_object_id_str(&id_str,
2403 got_worktree_get_base_commit_id(worktree));
2404 if (error)
2405 goto done;
2406 arg.repo = repo;
2407 arg.worktree = worktree;
2408 arg.diff_context = diff_context;
2409 arg.id_str = id_str;
2410 arg.header_shown = 0;
2411 arg.diff_staged = diff_staged;
2412 arg.ignore_whitespace = ignore_whitespace;
2414 error = got_pathlist_append(&paths, path, NULL);
2415 if (error)
2416 goto done;
2418 error = got_worktree_status(worktree, &paths, repo, print_diff,
2419 &arg, check_cancelled, NULL);
2420 free(id_str);
2421 got_pathlist_free(&paths);
2422 goto done;
2425 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2426 repo);
2427 if (error)
2428 goto done;
2430 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2431 repo);
2432 if (error)
2433 goto done;
2435 error = got_object_get_type(&type1, repo, id1);
2436 if (error)
2437 goto done;
2439 error = got_object_get_type(&type2, repo, id2);
2440 if (error)
2441 goto done;
2443 if (type1 != type2) {
2444 error = got_error(GOT_ERR_OBJ_TYPE);
2445 goto done;
2448 switch (type1) {
2449 case GOT_OBJ_TYPE_BLOB:
2450 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2451 diff_context, ignore_whitespace, repo, stdout);
2452 break;
2453 case GOT_OBJ_TYPE_TREE:
2454 error = got_diff_objects_as_trees(id1, id2, "", "",
2455 diff_context, ignore_whitespace, repo, stdout);
2456 break;
2457 case GOT_OBJ_TYPE_COMMIT:
2458 printf("diff %s %s\n", label1, label2);
2459 error = got_diff_objects_as_commits(id1, id2, diff_context,
2460 ignore_whitespace, repo, stdout);
2461 break;
2462 default:
2463 error = got_error(GOT_ERR_OBJ_TYPE);
2466 done:
2467 free(label1);
2468 free(label2);
2469 free(id1);
2470 free(id2);
2471 free(path);
2472 if (worktree)
2473 got_worktree_close(worktree);
2474 if (repo) {
2475 const struct got_error *repo_error;
2476 repo_error = got_repo_close(repo);
2477 if (error == NULL)
2478 error = repo_error;
2480 return error;
2483 __dead static void
2484 usage_blame(void)
2486 fprintf(stderr,
2487 "usage: %s blame [-c commit] [-r repository-path] path\n",
2488 getprogname());
2489 exit(1);
2492 struct blame_line {
2493 int annotated;
2494 char *id_str;
2495 char *committer;
2496 char datebuf[11]; /* YYYY-MM-DD + NUL */
2499 struct blame_cb_args {
2500 struct blame_line *lines;
2501 int nlines;
2502 int nlines_prec;
2503 int lineno_cur;
2504 off_t *line_offsets;
2505 FILE *f;
2506 struct got_repository *repo;
2509 static const struct got_error *
2510 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2512 const struct got_error *err = NULL;
2513 struct blame_cb_args *a = arg;
2514 struct blame_line *bline;
2515 char *line = NULL;
2516 size_t linesize = 0;
2517 struct got_commit_object *commit = NULL;
2518 off_t offset;
2519 struct tm tm;
2520 time_t committer_time;
2522 if (nlines != a->nlines ||
2523 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2524 return got_error(GOT_ERR_RANGE);
2526 if (sigint_received)
2527 return got_error(GOT_ERR_ITER_COMPLETED);
2529 if (lineno == -1)
2530 return NULL; /* no change in this commit */
2532 /* Annotate this line. */
2533 bline = &a->lines[lineno - 1];
2534 if (bline->annotated)
2535 return NULL;
2536 err = got_object_id_str(&bline->id_str, id);
2537 if (err)
2538 return err;
2540 err = got_object_open_as_commit(&commit, a->repo, id);
2541 if (err)
2542 goto done;
2544 bline->committer = strdup(got_object_commit_get_committer(commit));
2545 if (bline->committer == NULL) {
2546 err = got_error_from_errno("strdup");
2547 goto done;
2550 committer_time = got_object_commit_get_committer_time(commit);
2551 if (localtime_r(&committer_time, &tm) == NULL)
2552 return got_error_from_errno("localtime_r");
2553 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2554 &tm) >= sizeof(bline->datebuf)) {
2555 err = got_error(GOT_ERR_NO_SPACE);
2556 goto done;
2558 bline->annotated = 1;
2560 /* Print lines annotated so far. */
2561 bline = &a->lines[a->lineno_cur - 1];
2562 if (!bline->annotated)
2563 goto done;
2565 offset = a->line_offsets[a->lineno_cur - 1];
2566 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2567 err = got_error_from_errno("fseeko");
2568 goto done;
2571 while (bline->annotated) {
2572 char *smallerthan, *at, *nl, *committer;
2573 size_t len;
2575 if (getline(&line, &linesize, a->f) == -1) {
2576 if (ferror(a->f))
2577 err = got_error_from_errno("getline");
2578 break;
2581 committer = bline->committer;
2582 smallerthan = strchr(committer, '<');
2583 if (smallerthan && smallerthan[1] != '\0')
2584 committer = smallerthan + 1;
2585 at = strchr(committer, '@');
2586 if (at)
2587 *at = '\0';
2588 len = strlen(committer);
2589 if (len >= 9)
2590 committer[8] = '\0';
2592 nl = strchr(line, '\n');
2593 if (nl)
2594 *nl = '\0';
2595 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2596 bline->id_str, bline->datebuf, committer, line);
2598 a->lineno_cur++;
2599 bline = &a->lines[a->lineno_cur - 1];
2601 done:
2602 if (commit)
2603 got_object_commit_close(commit);
2604 free(line);
2605 return err;
2608 static const struct got_error *
2609 cmd_blame(int argc, char *argv[])
2611 const struct got_error *error;
2612 struct got_repository *repo = NULL;
2613 struct got_worktree *worktree = NULL;
2614 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2615 struct got_object_id *obj_id = NULL;
2616 struct got_object_id *commit_id = NULL;
2617 struct got_blob_object *blob = NULL;
2618 char *commit_id_str = NULL;
2619 struct blame_cb_args bca;
2620 int ch, obj_type, i;
2621 size_t filesize;
2623 memset(&bca, 0, sizeof(bca));
2625 #ifndef PROFILE
2626 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2627 NULL) == -1)
2628 err(1, "pledge");
2629 #endif
2631 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2632 switch (ch) {
2633 case 'c':
2634 commit_id_str = optarg;
2635 break;
2636 case 'r':
2637 repo_path = realpath(optarg, NULL);
2638 if (repo_path == NULL)
2639 return got_error_from_errno2("realpath",
2640 optarg);
2641 got_path_strip_trailing_slashes(repo_path);
2642 break;
2643 default:
2644 usage_blame();
2645 /* NOTREACHED */
2649 argc -= optind;
2650 argv += optind;
2652 if (argc == 1)
2653 path = argv[0];
2654 else
2655 usage_blame();
2657 cwd = getcwd(NULL, 0);
2658 if (cwd == NULL) {
2659 error = got_error_from_errno("getcwd");
2660 goto done;
2662 if (repo_path == NULL) {
2663 error = got_worktree_open(&worktree, cwd);
2664 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2665 goto done;
2666 else
2667 error = NULL;
2668 if (worktree) {
2669 repo_path =
2670 strdup(got_worktree_get_repo_path(worktree));
2671 if (repo_path == NULL)
2672 error = got_error_from_errno("strdup");
2673 if (error)
2674 goto done;
2675 } else {
2676 repo_path = strdup(cwd);
2677 if (repo_path == NULL) {
2678 error = got_error_from_errno("strdup");
2679 goto done;
2684 error = got_repo_open(&repo, repo_path, NULL);
2685 if (error != NULL)
2686 goto done;
2688 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2689 if (error)
2690 goto done;
2692 if (worktree) {
2693 const char *prefix = got_worktree_get_path_prefix(worktree);
2694 char *p, *worktree_subdir = cwd +
2695 strlen(got_worktree_get_root_path(worktree));
2696 if (asprintf(&p, "%s%s%s%s%s",
2697 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2698 worktree_subdir, worktree_subdir[0] ? "/" : "",
2699 path) == -1) {
2700 error = got_error_from_errno("asprintf");
2701 goto done;
2703 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2704 free(p);
2705 } else {
2706 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2708 if (error)
2709 goto done;
2711 if (commit_id_str == NULL) {
2712 struct got_reference *head_ref;
2713 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2714 if (error != NULL)
2715 goto done;
2716 error = got_ref_resolve(&commit_id, repo, head_ref);
2717 got_ref_close(head_ref);
2718 if (error != NULL)
2719 goto done;
2720 } else {
2721 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2722 if (error)
2723 goto done;
2726 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2727 if (error)
2728 goto done;
2729 if (obj_id == NULL) {
2730 error = got_error(GOT_ERR_NO_OBJ);
2731 goto done;
2734 error = got_object_get_type(&obj_type, repo, obj_id);
2735 if (error)
2736 goto done;
2738 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2739 error = got_error(GOT_ERR_OBJ_TYPE);
2740 goto done;
2743 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2744 if (error)
2745 goto done;
2746 bca.f = got_opentemp();
2747 if (bca.f == NULL) {
2748 error = got_error_from_errno("got_opentemp");
2749 goto done;
2751 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2752 &bca.line_offsets, bca.f, blob);
2753 if (error || bca.nlines == 0)
2754 goto done;
2756 /* Don't include \n at EOF in the blame line count. */
2757 if (bca.line_offsets[bca.nlines - 1] == filesize)
2758 bca.nlines--;
2760 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2761 if (bca.lines == NULL) {
2762 error = got_error_from_errno("calloc");
2763 goto done;
2765 bca.lineno_cur = 1;
2766 bca.nlines_prec = 0;
2767 i = bca.nlines;
2768 while (i > 0) {
2769 i /= 10;
2770 bca.nlines_prec++;
2772 bca.repo = repo;
2774 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2775 check_cancelled, NULL);
2776 if (error)
2777 goto done;
2778 done:
2779 free(in_repo_path);
2780 free(repo_path);
2781 free(cwd);
2782 free(commit_id);
2783 free(obj_id);
2784 if (blob)
2785 got_object_blob_close(blob);
2786 if (worktree)
2787 got_worktree_close(worktree);
2788 if (repo) {
2789 const struct got_error *repo_error;
2790 repo_error = got_repo_close(repo);
2791 if (error == NULL)
2792 error = repo_error;
2794 if (bca.lines) {
2795 for (i = 0; i < bca.nlines; i++) {
2796 struct blame_line *bline = &bca.lines[i];
2797 free(bline->id_str);
2798 free(bline->committer);
2800 free(bca.lines);
2802 free(bca.line_offsets);
2803 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2804 error = got_error_from_errno("fclose");
2805 return error;
2808 __dead static void
2809 usage_tree(void)
2811 fprintf(stderr,
2812 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2813 getprogname());
2814 exit(1);
2817 static void
2818 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2819 const char *root_path)
2821 int is_root_path = (strcmp(path, root_path) == 0);
2822 const char *modestr = "";
2823 mode_t mode = got_tree_entry_get_mode(te);
2825 path += strlen(root_path);
2826 while (path[0] == '/')
2827 path++;
2829 if (got_object_tree_entry_is_submodule(te))
2830 modestr = "$";
2831 else if (S_ISLNK(mode))
2832 modestr = "@";
2833 else if (S_ISDIR(mode))
2834 modestr = "/";
2835 else if (mode & S_IXUSR)
2836 modestr = "*";
2838 printf("%s%s%s%s%s\n", id ? id : "", path,
2839 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2842 static const struct got_error *
2843 print_tree(const char *path, struct got_object_id *commit_id,
2844 int show_ids, int recurse, const char *root_path,
2845 struct got_repository *repo)
2847 const struct got_error *err = NULL;
2848 struct got_object_id *tree_id = NULL;
2849 struct got_tree_object *tree = NULL;
2850 int nentries, i;
2852 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2853 if (err)
2854 goto done;
2856 err = got_object_open_as_tree(&tree, repo, tree_id);
2857 if (err)
2858 goto done;
2859 nentries = got_object_tree_get_nentries(tree);
2860 for (i = 0; i < nentries; i++) {
2861 struct got_tree_entry *te;
2862 char *id = NULL;
2864 if (sigint_received || sigpipe_received)
2865 break;
2867 te = got_object_tree_get_entry(tree, i);
2868 if (show_ids) {
2869 char *id_str;
2870 err = got_object_id_str(&id_str,
2871 got_tree_entry_get_id(te));
2872 if (err)
2873 goto done;
2874 if (asprintf(&id, "%s ", id_str) == -1) {
2875 err = got_error_from_errno("asprintf");
2876 free(id_str);
2877 goto done;
2879 free(id_str);
2881 print_entry(te, id, path, root_path);
2882 free(id);
2884 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2885 char *child_path;
2886 if (asprintf(&child_path, "%s%s%s", path,
2887 path[0] == '/' && path[1] == '\0' ? "" : "/",
2888 got_tree_entry_get_name(te)) == -1) {
2889 err = got_error_from_errno("asprintf");
2890 goto done;
2892 err = print_tree(child_path, commit_id, show_ids, 1,
2893 root_path, repo);
2894 free(child_path);
2895 if (err)
2896 goto done;
2899 done:
2900 if (tree)
2901 got_object_tree_close(tree);
2902 free(tree_id);
2903 return err;
2906 static const struct got_error *
2907 cmd_tree(int argc, char *argv[])
2909 const struct got_error *error;
2910 struct got_repository *repo = NULL;
2911 struct got_worktree *worktree = NULL;
2912 const char *path;
2913 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2914 struct got_object_id *commit_id = NULL;
2915 char *commit_id_str = NULL;
2916 int show_ids = 0, recurse = 0;
2917 int ch;
2919 #ifndef PROFILE
2920 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2921 NULL) == -1)
2922 err(1, "pledge");
2923 #endif
2925 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2926 switch (ch) {
2927 case 'c':
2928 commit_id_str = optarg;
2929 break;
2930 case 'r':
2931 repo_path = realpath(optarg, NULL);
2932 if (repo_path == NULL)
2933 return got_error_from_errno2("realpath",
2934 optarg);
2935 got_path_strip_trailing_slashes(repo_path);
2936 break;
2937 case 'i':
2938 show_ids = 1;
2939 break;
2940 case 'R':
2941 recurse = 1;
2942 break;
2943 default:
2944 usage_tree();
2945 /* NOTREACHED */
2949 argc -= optind;
2950 argv += optind;
2952 if (argc == 1)
2953 path = argv[0];
2954 else if (argc > 1)
2955 usage_tree();
2956 else
2957 path = NULL;
2959 cwd = getcwd(NULL, 0);
2960 if (cwd == NULL) {
2961 error = got_error_from_errno("getcwd");
2962 goto done;
2964 if (repo_path == NULL) {
2965 error = got_worktree_open(&worktree, cwd);
2966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2967 goto done;
2968 else
2969 error = NULL;
2970 if (worktree) {
2971 repo_path =
2972 strdup(got_worktree_get_repo_path(worktree));
2973 if (repo_path == NULL)
2974 error = got_error_from_errno("strdup");
2975 if (error)
2976 goto done;
2977 } else {
2978 repo_path = strdup(cwd);
2979 if (repo_path == NULL) {
2980 error = got_error_from_errno("strdup");
2981 goto done;
2986 error = got_repo_open(&repo, repo_path, NULL);
2987 if (error != NULL)
2988 goto done;
2990 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2991 if (error)
2992 goto done;
2994 if (path == NULL) {
2995 if (worktree) {
2996 char *p, *worktree_subdir = cwd +
2997 strlen(got_worktree_get_root_path(worktree));
2998 if (asprintf(&p, "%s/%s",
2999 got_worktree_get_path_prefix(worktree),
3000 worktree_subdir) == -1) {
3001 error = got_error_from_errno("asprintf");
3002 goto done;
3004 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3005 free(p);
3006 if (error)
3007 goto done;
3008 } else
3009 path = "/";
3011 if (in_repo_path == NULL) {
3012 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3013 if (error != NULL)
3014 goto done;
3017 if (commit_id_str == NULL) {
3018 struct got_reference *head_ref;
3019 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3020 if (error != NULL)
3021 goto done;
3022 error = got_ref_resolve(&commit_id, repo, head_ref);
3023 got_ref_close(head_ref);
3024 if (error != NULL)
3025 goto done;
3026 } else {
3027 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3028 if (error)
3029 goto done;
3032 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3033 in_repo_path, repo);
3034 done:
3035 free(in_repo_path);
3036 free(repo_path);
3037 free(cwd);
3038 free(commit_id);
3039 if (worktree)
3040 got_worktree_close(worktree);
3041 if (repo) {
3042 const struct got_error *repo_error;
3043 repo_error = got_repo_close(repo);
3044 if (error == NULL)
3045 error = repo_error;
3047 return error;
3050 __dead static void
3051 usage_status(void)
3053 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3054 exit(1);
3057 static const struct got_error *
3058 print_status(void *arg, unsigned char status, unsigned char staged_status,
3059 const char *path, struct got_object_id *blob_id,
3060 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3061 int dirfd, const char *de_name)
3063 if (status == staged_status && (status == GOT_STATUS_DELETE))
3064 status = GOT_STATUS_NO_CHANGE;
3065 printf("%c%c %s\n", status, staged_status, path);
3066 return NULL;
3069 static const struct got_error *
3070 cmd_status(int argc, char *argv[])
3072 const struct got_error *error = NULL;
3073 struct got_repository *repo = NULL;
3074 struct got_worktree *worktree = NULL;
3075 char *cwd = NULL;
3076 struct got_pathlist_head paths;
3077 struct got_pathlist_entry *pe;
3078 int ch;
3080 TAILQ_INIT(&paths);
3082 while ((ch = getopt(argc, argv, "")) != -1) {
3083 switch (ch) {
3084 default:
3085 usage_status();
3086 /* NOTREACHED */
3090 argc -= optind;
3091 argv += optind;
3093 #ifndef PROFILE
3094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3095 NULL) == -1)
3096 err(1, "pledge");
3097 #endif
3098 cwd = getcwd(NULL, 0);
3099 if (cwd == NULL) {
3100 error = got_error_from_errno("getcwd");
3101 goto done;
3104 error = got_worktree_open(&worktree, cwd);
3105 if (error != NULL)
3106 goto done;
3108 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3109 NULL);
3110 if (error != NULL)
3111 goto done;
3113 error = apply_unveil(got_repo_get_path(repo), 1,
3114 got_worktree_get_root_path(worktree));
3115 if (error)
3116 goto done;
3118 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3119 if (error)
3120 goto done;
3122 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3123 check_cancelled, NULL);
3124 done:
3125 TAILQ_FOREACH(pe, &paths, entry)
3126 free((char *)pe->path);
3127 got_pathlist_free(&paths);
3128 free(cwd);
3129 return error;
3132 __dead static void
3133 usage_ref(void)
3135 fprintf(stderr,
3136 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3137 getprogname());
3138 exit(1);
3141 static const struct got_error *
3142 list_refs(struct got_repository *repo)
3144 static const struct got_error *err = NULL;
3145 struct got_reflist_head refs;
3146 struct got_reflist_entry *re;
3148 SIMPLEQ_INIT(&refs);
3149 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3150 if (err)
3151 return err;
3153 SIMPLEQ_FOREACH(re, &refs, entry) {
3154 char *refstr;
3155 refstr = got_ref_to_str(re->ref);
3156 if (refstr == NULL)
3157 return got_error_from_errno("got_ref_to_str");
3158 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3159 free(refstr);
3162 got_ref_list_free(&refs);
3163 return NULL;
3166 static const struct got_error *
3167 delete_ref(struct got_repository *repo, const char *refname)
3169 const struct got_error *err = NULL;
3170 struct got_reference *ref;
3172 err = got_ref_open(&ref, repo, refname, 0);
3173 if (err)
3174 return err;
3176 err = got_ref_delete(ref, repo);
3177 got_ref_close(ref);
3178 return err;
3181 static const struct got_error *
3182 add_ref(struct got_repository *repo, const char *refname, const char *target)
3184 const struct got_error *err = NULL;
3185 struct got_object_id *id;
3186 struct got_reference *ref = NULL;
3189 * Don't let the user create a reference name with a leading '-'.
3190 * While technically a valid reference name, this case is usually
3191 * an unintended typo.
3193 if (refname[0] == '-')
3194 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3196 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3197 repo);
3198 if (err) {
3199 struct got_reference *target_ref;
3201 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3202 return err;
3203 err = got_ref_open(&target_ref, repo, target, 0);
3204 if (err)
3205 return err;
3206 err = got_ref_resolve(&id, repo, target_ref);
3207 got_ref_close(target_ref);
3208 if (err)
3209 return err;
3212 err = got_ref_alloc(&ref, refname, id);
3213 if (err)
3214 goto done;
3216 err = got_ref_write(ref, repo);
3217 done:
3218 if (ref)
3219 got_ref_close(ref);
3220 free(id);
3221 return err;
3224 static const struct got_error *
3225 add_symref(struct got_repository *repo, const char *refname, const char *target)
3227 const struct got_error *err = NULL;
3228 struct got_reference *ref = NULL;
3229 struct got_reference *target_ref = NULL;
3232 * Don't let the user create a reference name with a leading '-'.
3233 * While technically a valid reference name, this case is usually
3234 * an unintended typo.
3236 if (refname[0] == '-')
3237 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3239 err = got_ref_open(&target_ref, repo, target, 0);
3240 if (err)
3241 return err;
3243 err = got_ref_alloc_symref(&ref, refname, target_ref);
3244 if (err)
3245 goto done;
3247 err = got_ref_write(ref, repo);
3248 done:
3249 if (target_ref)
3250 got_ref_close(target_ref);
3251 if (ref)
3252 got_ref_close(ref);
3253 return err;
3256 static const struct got_error *
3257 cmd_ref(int argc, char *argv[])
3259 const struct got_error *error = NULL;
3260 struct got_repository *repo = NULL;
3261 struct got_worktree *worktree = NULL;
3262 char *cwd = NULL, *repo_path = NULL;
3263 int ch, do_list = 0, create_symref = 0;
3264 const char *delref = NULL;
3266 /* TODO: Add -s option for adding symbolic references. */
3267 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3268 switch (ch) {
3269 case 'd':
3270 delref = optarg;
3271 break;
3272 case 'r':
3273 repo_path = realpath(optarg, NULL);
3274 if (repo_path == NULL)
3275 return got_error_from_errno2("realpath",
3276 optarg);
3277 got_path_strip_trailing_slashes(repo_path);
3278 break;
3279 case 'l':
3280 do_list = 1;
3281 break;
3282 case 's':
3283 create_symref = 1;
3284 break;
3285 default:
3286 usage_ref();
3287 /* NOTREACHED */
3291 if (do_list && delref)
3292 errx(1, "-l and -d options are mutually exclusive\n");
3294 argc -= optind;
3295 argv += optind;
3297 if (do_list || delref) {
3298 if (create_symref)
3299 errx(1, "-s option cannot be used together with the "
3300 "-l or -d options");
3301 if (argc > 0)
3302 usage_ref();
3303 } else if (argc != 2)
3304 usage_ref();
3306 #ifndef PROFILE
3307 if (do_list) {
3308 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3309 NULL) == -1)
3310 err(1, "pledge");
3311 } else {
3312 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3313 "sendfd unveil", NULL) == -1)
3314 err(1, "pledge");
3316 #endif
3317 cwd = getcwd(NULL, 0);
3318 if (cwd == NULL) {
3319 error = got_error_from_errno("getcwd");
3320 goto done;
3323 if (repo_path == NULL) {
3324 error = got_worktree_open(&worktree, cwd);
3325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3326 goto done;
3327 else
3328 error = NULL;
3329 if (worktree) {
3330 repo_path =
3331 strdup(got_worktree_get_repo_path(worktree));
3332 if (repo_path == NULL)
3333 error = got_error_from_errno("strdup");
3334 if (error)
3335 goto done;
3336 } else {
3337 repo_path = strdup(cwd);
3338 if (repo_path == NULL) {
3339 error = got_error_from_errno("strdup");
3340 goto done;
3345 error = got_repo_open(&repo, repo_path, NULL);
3346 if (error != NULL)
3347 goto done;
3349 error = apply_unveil(got_repo_get_path(repo), do_list,
3350 worktree ? got_worktree_get_root_path(worktree) : NULL);
3351 if (error)
3352 goto done;
3354 if (do_list)
3355 error = list_refs(repo);
3356 else if (delref)
3357 error = delete_ref(repo, delref);
3358 else if (create_symref)
3359 error = add_symref(repo, argv[0], argv[1]);
3360 else
3361 error = add_ref(repo, argv[0], argv[1]);
3362 done:
3363 if (repo)
3364 got_repo_close(repo);
3365 if (worktree)
3366 got_worktree_close(worktree);
3367 free(cwd);
3368 free(repo_path);
3369 return error;
3372 __dead static void
3373 usage_branch(void)
3375 fprintf(stderr,
3376 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3377 "[name]\n", getprogname());
3378 exit(1);
3381 static const struct got_error *
3382 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3383 struct got_reference *ref)
3385 const struct got_error *err = NULL;
3386 const char *refname, *marker = " ";
3387 char *refstr;
3389 refname = got_ref_get_name(ref);
3390 if (worktree && strcmp(refname,
3391 got_worktree_get_head_ref_name(worktree)) == 0) {
3392 struct got_object_id *id = NULL;
3394 err = got_ref_resolve(&id, repo, ref);
3395 if (err)
3396 return err;
3397 if (got_object_id_cmp(id,
3398 got_worktree_get_base_commit_id(worktree)) == 0)
3399 marker = "* ";
3400 else
3401 marker = "~ ";
3402 free(id);
3405 if (strncmp(refname, "refs/heads/", 11) == 0)
3406 refname += 11;
3407 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3408 refname += 18;
3410 refstr = got_ref_to_str(ref);
3411 if (refstr == NULL)
3412 return got_error_from_errno("got_ref_to_str");
3414 printf("%s%s: %s\n", marker, refname, refstr);
3415 free(refstr);
3416 return NULL;
3419 static const struct got_error *
3420 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3422 const char *refname;
3424 if (worktree == NULL)
3425 return got_error(GOT_ERR_NOT_WORKTREE);
3427 refname = got_worktree_get_head_ref_name(worktree);
3429 if (strncmp(refname, "refs/heads/", 11) == 0)
3430 refname += 11;
3431 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3432 refname += 18;
3434 printf("%s\n", refname);
3436 return NULL;
3439 static const struct got_error *
3440 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3442 static const struct got_error *err = NULL;
3443 struct got_reflist_head refs;
3444 struct got_reflist_entry *re;
3445 struct got_reference *temp_ref = NULL;
3446 int rebase_in_progress, histedit_in_progress;
3448 SIMPLEQ_INIT(&refs);
3450 if (worktree) {
3451 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3452 worktree);
3453 if (err)
3454 return err;
3456 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3457 worktree);
3458 if (err)
3459 return err;
3461 if (rebase_in_progress || histedit_in_progress) {
3462 err = got_ref_open(&temp_ref, repo,
3463 got_worktree_get_head_ref_name(worktree), 0);
3464 if (err)
3465 return err;
3466 list_branch(repo, worktree, temp_ref);
3467 got_ref_close(temp_ref);
3471 err = got_ref_list(&refs, repo, "refs/heads",
3472 got_ref_cmp_by_name, NULL);
3473 if (err)
3474 return err;
3476 SIMPLEQ_FOREACH(re, &refs, entry)
3477 list_branch(repo, worktree, re->ref);
3479 got_ref_list_free(&refs);
3480 return NULL;
3483 static const struct got_error *
3484 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3485 const char *branch_name)
3487 const struct got_error *err = NULL;
3488 struct got_reference *ref = NULL;
3489 char *refname;
3491 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3492 return got_error_from_errno("asprintf");
3494 err = got_ref_open(&ref, repo, refname, 0);
3495 if (err)
3496 goto done;
3498 if (worktree &&
3499 strcmp(got_worktree_get_head_ref_name(worktree),
3500 got_ref_get_name(ref)) == 0) {
3501 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3502 "will not delete this work tree's current branch");
3503 goto done;
3506 err = got_ref_delete(ref, repo);
3507 done:
3508 if (ref)
3509 got_ref_close(ref);
3510 free(refname);
3511 return err;
3514 static const struct got_error *
3515 add_branch(struct got_repository *repo, const char *branch_name,
3516 struct got_object_id *base_commit_id)
3518 const struct got_error *err = NULL;
3519 struct got_reference *ref = NULL;
3520 char *base_refname = NULL, *refname = NULL;
3523 * Don't let the user create a branch name with a leading '-'.
3524 * While technically a valid reference name, this case is usually
3525 * an unintended typo.
3527 if (branch_name[0] == '-')
3528 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3530 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3531 err = got_error_from_errno("asprintf");
3532 goto done;
3535 err = got_ref_open(&ref, repo, refname, 0);
3536 if (err == NULL) {
3537 err = got_error(GOT_ERR_BRANCH_EXISTS);
3538 goto done;
3539 } else if (err->code != GOT_ERR_NOT_REF)
3540 goto done;
3542 err = got_ref_alloc(&ref, refname, base_commit_id);
3543 if (err)
3544 goto done;
3546 err = got_ref_write(ref, repo);
3547 done:
3548 if (ref)
3549 got_ref_close(ref);
3550 free(base_refname);
3551 free(refname);
3552 return err;
3555 static const struct got_error *
3556 cmd_branch(int argc, char *argv[])
3558 const struct got_error *error = NULL;
3559 struct got_repository *repo = NULL;
3560 struct got_worktree *worktree = NULL;
3561 char *cwd = NULL, *repo_path = NULL;
3562 int ch, do_list = 0, do_show = 0;
3563 const char *delref = NULL, *commit_id_arg = NULL;
3565 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3566 switch (ch) {
3567 case 'c':
3568 commit_id_arg = optarg;
3569 break;
3570 case 'd':
3571 delref = optarg;
3572 break;
3573 case 'r':
3574 repo_path = realpath(optarg, NULL);
3575 if (repo_path == NULL)
3576 return got_error_from_errno2("realpath",
3577 optarg);
3578 got_path_strip_trailing_slashes(repo_path);
3579 break;
3580 case 'l':
3581 do_list = 1;
3582 break;
3583 default:
3584 usage_branch();
3585 /* NOTREACHED */
3589 if (do_list && delref)
3590 errx(1, "-l and -d options are mutually exclusive\n");
3592 argc -= optind;
3593 argv += optind;
3595 if (!do_list && !delref && argc == 0)
3596 do_show = 1;
3598 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3599 errx(1, "-c option can only be used when creating a branch");
3601 if (do_list || delref) {
3602 if (argc > 0)
3603 usage_branch();
3604 } else if (!do_show && argc != 1)
3605 usage_branch();
3607 #ifndef PROFILE
3608 if (do_list || do_show) {
3609 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3610 NULL) == -1)
3611 err(1, "pledge");
3612 } else {
3613 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3614 "sendfd unveil", NULL) == -1)
3615 err(1, "pledge");
3617 #endif
3618 cwd = getcwd(NULL, 0);
3619 if (cwd == NULL) {
3620 error = got_error_from_errno("getcwd");
3621 goto done;
3624 if (repo_path == NULL) {
3625 error = got_worktree_open(&worktree, cwd);
3626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3627 goto done;
3628 else
3629 error = NULL;
3630 if (worktree) {
3631 repo_path =
3632 strdup(got_worktree_get_repo_path(worktree));
3633 if (repo_path == NULL)
3634 error = got_error_from_errno("strdup");
3635 if (error)
3636 goto done;
3637 } else {
3638 repo_path = strdup(cwd);
3639 if (repo_path == NULL) {
3640 error = got_error_from_errno("strdup");
3641 goto done;
3646 error = got_repo_open(&repo, repo_path, NULL);
3647 if (error != NULL)
3648 goto done;
3650 error = apply_unveil(got_repo_get_path(repo), do_list,
3651 worktree ? got_worktree_get_root_path(worktree) : NULL);
3652 if (error)
3653 goto done;
3655 if (do_show)
3656 error = show_current_branch(repo, worktree);
3657 else if (do_list)
3658 error = list_branches(repo, worktree);
3659 else if (delref)
3660 error = delete_branch(repo, worktree, delref);
3661 else {
3662 struct got_object_id *commit_id;
3663 if (commit_id_arg == NULL)
3664 commit_id_arg = worktree ?
3665 got_worktree_get_head_ref_name(worktree) :
3666 GOT_REF_HEAD;
3667 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3668 if (error)
3669 goto done;
3670 error = add_branch(repo, argv[0], commit_id);
3671 free(commit_id);
3673 done:
3674 if (repo)
3675 got_repo_close(repo);
3676 if (worktree)
3677 got_worktree_close(worktree);
3678 free(cwd);
3679 free(repo_path);
3680 return error;
3684 __dead static void
3685 usage_tag(void)
3687 fprintf(stderr,
3688 "usage: %s tag [-r repository] | -l | "
3689 "[-m message] name [commit]\n", getprogname());
3690 exit(1);
3693 #if 0
3694 static const struct got_error *
3695 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3697 const struct got_error *err = NULL;
3698 struct got_reflist_entry *re, *se, *new;
3699 struct got_object_id *re_id, *se_id;
3700 struct got_tag_object *re_tag, *se_tag;
3701 time_t re_time, se_time;
3703 SIMPLEQ_FOREACH(re, tags, entry) {
3704 se = SIMPLEQ_FIRST(sorted);
3705 if (se == NULL) {
3706 err = got_reflist_entry_dup(&new, re);
3707 if (err)
3708 return err;
3709 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3710 continue;
3711 } else {
3712 err = got_ref_resolve(&re_id, repo, re->ref);
3713 if (err)
3714 break;
3715 err = got_object_open_as_tag(&re_tag, repo, re_id);
3716 free(re_id);
3717 if (err)
3718 break;
3719 re_time = got_object_tag_get_tagger_time(re_tag);
3720 got_object_tag_close(re_tag);
3723 while (se) {
3724 err = got_ref_resolve(&se_id, repo, re->ref);
3725 if (err)
3726 break;
3727 err = got_object_open_as_tag(&se_tag, repo, se_id);
3728 free(se_id);
3729 if (err)
3730 break;
3731 se_time = got_object_tag_get_tagger_time(se_tag);
3732 got_object_tag_close(se_tag);
3734 if (se_time > re_time) {
3735 err = got_reflist_entry_dup(&new, re);
3736 if (err)
3737 return err;
3738 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3739 break;
3741 se = SIMPLEQ_NEXT(se, entry);
3742 continue;
3745 done:
3746 return err;
3748 #endif
3750 static const struct got_error *
3751 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3752 struct got_reference *ref2)
3754 const struct got_error *err = NULL;
3755 struct got_repository *repo = arg;
3756 struct got_object_id *id1, *id2 = NULL;
3757 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3758 time_t time1, time2;
3760 *cmp = 0;
3762 err = got_ref_resolve(&id1, repo, ref1);
3763 if (err)
3764 return err;
3765 err = got_object_open_as_tag(&tag1, repo, id1);
3766 if (err)
3767 goto done;
3769 err = got_ref_resolve(&id2, repo, ref2);
3770 if (err)
3771 goto done;
3772 err = got_object_open_as_tag(&tag2, repo, id2);
3773 if (err)
3774 goto done;
3776 time1 = got_object_tag_get_tagger_time(tag1);
3777 time2 = got_object_tag_get_tagger_time(tag2);
3779 /* Put latest tags first. */
3780 if (time1 < time2)
3781 *cmp = 1;
3782 else if (time1 > time2)
3783 *cmp = -1;
3784 else
3785 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3786 done:
3787 free(id1);
3788 free(id2);
3789 if (tag1)
3790 got_object_tag_close(tag1);
3791 if (tag2)
3792 got_object_tag_close(tag2);
3793 return err;
3796 static const struct got_error *
3797 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3799 static const struct got_error *err = NULL;
3800 struct got_reflist_head refs;
3801 struct got_reflist_entry *re;
3803 SIMPLEQ_INIT(&refs);
3805 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3806 if (err)
3807 return err;
3809 SIMPLEQ_FOREACH(re, &refs, entry) {
3810 const char *refname;
3811 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3812 char datebuf[26];
3813 time_t tagger_time;
3814 struct got_object_id *id;
3815 struct got_tag_object *tag;
3817 refname = got_ref_get_name(re->ref);
3818 if (strncmp(refname, "refs/tags/", 10) != 0)
3819 continue;
3820 refname += 10;
3821 refstr = got_ref_to_str(re->ref);
3822 if (refstr == NULL) {
3823 err = got_error_from_errno("got_ref_to_str");
3824 break;
3826 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3827 free(refstr);
3829 err = got_ref_resolve(&id, repo, re->ref);
3830 if (err)
3831 break;
3832 err = got_object_open_as_tag(&tag, repo, id);
3833 free(id);
3834 if (err)
3835 break;
3836 printf("from: %s\n", got_object_tag_get_tagger(tag));
3837 tagger_time = got_object_tag_get_tagger_time(tag);
3838 datestr = get_datestr(&tagger_time, datebuf);
3839 if (datestr)
3840 printf("date: %s UTC\n", datestr);
3841 err = got_object_id_str(&id_str,
3842 got_object_tag_get_object_id(tag));
3843 if (err)
3844 break;
3845 switch (got_object_tag_get_object_type(tag)) {
3846 case GOT_OBJ_TYPE_BLOB:
3847 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3848 break;
3849 case GOT_OBJ_TYPE_TREE:
3850 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3851 break;
3852 case GOT_OBJ_TYPE_COMMIT:
3853 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3854 break;
3855 case GOT_OBJ_TYPE_TAG:
3856 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3857 break;
3858 default:
3859 break;
3861 free(id_str);
3862 tagmsg0 = strdup(got_object_tag_get_message(tag));
3863 got_object_tag_close(tag);
3864 if (tagmsg0 == NULL) {
3865 err = got_error_from_errno("strdup");
3866 break;
3869 tagmsg = tagmsg0;
3870 do {
3871 line = strsep(&tagmsg, "\n");
3872 if (line)
3873 printf(" %s\n", line);
3874 } while (line);
3875 free(tagmsg0);
3878 got_ref_list_free(&refs);
3879 return NULL;
3882 static const struct got_error *
3883 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3884 const char *tag_name, const char *repo_path)
3886 const struct got_error *err = NULL;
3887 char *template = NULL, *initial_content = NULL;
3888 char *editor = NULL;
3889 int fd = -1;
3891 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3892 err = got_error_from_errno("asprintf");
3893 goto done;
3896 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3897 commit_id_str, tag_name) == -1) {
3898 err = got_error_from_errno("asprintf");
3899 goto done;
3902 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3903 if (err)
3904 goto done;
3906 dprintf(fd, initial_content);
3907 close(fd);
3909 err = get_editor(&editor);
3910 if (err)
3911 goto done;
3912 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3913 done:
3914 free(initial_content);
3915 free(template);
3916 free(editor);
3918 /* Editor is done; we can now apply unveil(2) */
3919 if (err == NULL) {
3920 err = apply_unveil(repo_path, 0, NULL);
3921 if (err) {
3922 free(*tagmsg);
3923 *tagmsg = NULL;
3926 return err;
3929 static const struct got_error *
3930 add_tag(struct got_repository *repo, const char *tag_name,
3931 const char *commit_arg, const char *tagmsg_arg)
3933 const struct got_error *err = NULL;
3934 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3935 char *label = NULL, *commit_id_str = NULL;
3936 struct got_reference *ref = NULL;
3937 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3938 char *tagmsg_path = NULL, *tag_id_str = NULL;
3939 int preserve_tagmsg = 0;
3942 * Don't let the user create a tag name with a leading '-'.
3943 * While technically a valid reference name, this case is usually
3944 * an unintended typo.
3946 if (tag_name[0] == '-')
3947 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3949 err = get_author(&tagger, repo);
3950 if (err)
3951 return err;
3953 err = match_object_id(&commit_id, &label, commit_arg,
3954 GOT_OBJ_TYPE_COMMIT, 1, repo);
3955 if (err)
3956 goto done;
3958 err = got_object_id_str(&commit_id_str, commit_id);
3959 if (err)
3960 goto done;
3962 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3963 refname = strdup(tag_name);
3964 if (refname == NULL) {
3965 err = got_error_from_errno("strdup");
3966 goto done;
3968 tag_name += 10;
3969 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3970 err = got_error_from_errno("asprintf");
3971 goto done;
3974 err = got_ref_open(&ref, repo, refname, 0);
3975 if (err == NULL) {
3976 err = got_error(GOT_ERR_TAG_EXISTS);
3977 goto done;
3978 } else if (err->code != GOT_ERR_NOT_REF)
3979 goto done;
3981 if (tagmsg_arg == NULL) {
3982 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3983 tag_name, got_repo_get_path(repo));
3984 if (err) {
3985 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3986 tagmsg_path != NULL)
3987 preserve_tagmsg = 1;
3988 goto done;
3992 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3993 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3994 if (err) {
3995 if (tagmsg_path)
3996 preserve_tagmsg = 1;
3997 goto done;
4000 err = got_ref_alloc(&ref, refname, tag_id);
4001 if (err) {
4002 if (tagmsg_path)
4003 preserve_tagmsg = 1;
4004 goto done;
4007 err = got_ref_write(ref, repo);
4008 if (err) {
4009 if (tagmsg_path)
4010 preserve_tagmsg = 1;
4011 goto done;
4014 err = got_object_id_str(&tag_id_str, tag_id);
4015 if (err) {
4016 if (tagmsg_path)
4017 preserve_tagmsg = 1;
4018 goto done;
4020 printf("Created tag %s\n", tag_id_str);
4021 done:
4022 if (preserve_tagmsg) {
4023 fprintf(stderr, "%s: tag message preserved in %s\n",
4024 getprogname(), tagmsg_path);
4025 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4026 err = got_error_from_errno2("unlink", tagmsg_path);
4027 free(tag_id_str);
4028 if (ref)
4029 got_ref_close(ref);
4030 free(commit_id);
4031 free(commit_id_str);
4032 free(refname);
4033 free(tagmsg);
4034 free(tagmsg_path);
4035 free(tagger);
4036 return err;
4039 static const struct got_error *
4040 cmd_tag(int argc, char *argv[])
4042 const struct got_error *error = NULL;
4043 struct got_repository *repo = NULL;
4044 struct got_worktree *worktree = NULL;
4045 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4046 char *gitconfig_path = NULL;
4047 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4048 int ch, do_list = 0;
4050 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4051 switch (ch) {
4052 case 'm':
4053 tagmsg = optarg;
4054 break;
4055 case 'r':
4056 repo_path = realpath(optarg, NULL);
4057 if (repo_path == NULL)
4058 return got_error_from_errno2("realpath",
4059 optarg);
4060 got_path_strip_trailing_slashes(repo_path);
4061 break;
4062 case 'l':
4063 do_list = 1;
4064 break;
4065 default:
4066 usage_tag();
4067 /* NOTREACHED */
4071 argc -= optind;
4072 argv += optind;
4074 if (do_list) {
4075 if (tagmsg)
4076 errx(1, "-l and -m options are mutually exclusive\n");
4077 if (argc > 0)
4078 usage_tag();
4079 } else if (argc < 1 || argc > 2)
4080 usage_tag();
4081 else if (argc > 1)
4082 commit_id_arg = argv[1];
4083 tag_name = argv[0];
4085 #ifndef PROFILE
4086 if (do_list) {
4087 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4088 NULL) == -1)
4089 err(1, "pledge");
4090 } else {
4091 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4092 "sendfd unveil", NULL) == -1)
4093 err(1, "pledge");
4095 #endif
4096 cwd = getcwd(NULL, 0);
4097 if (cwd == NULL) {
4098 error = got_error_from_errno("getcwd");
4099 goto done;
4102 if (repo_path == NULL) {
4103 error = got_worktree_open(&worktree, cwd);
4104 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4105 goto done;
4106 else
4107 error = NULL;
4108 if (worktree) {
4109 repo_path =
4110 strdup(got_worktree_get_repo_path(worktree));
4111 if (repo_path == NULL)
4112 error = got_error_from_errno("strdup");
4113 if (error)
4114 goto done;
4115 } else {
4116 repo_path = strdup(cwd);
4117 if (repo_path == NULL) {
4118 error = got_error_from_errno("strdup");
4119 goto done;
4124 if (do_list) {
4125 error = got_repo_open(&repo, repo_path, NULL);
4126 if (error != NULL)
4127 goto done;
4128 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4129 if (error)
4130 goto done;
4131 error = list_tags(repo, worktree);
4132 } else {
4133 error = get_gitconfig_path(&gitconfig_path);
4134 if (error)
4135 goto done;
4136 error = got_repo_open(&repo, repo_path, gitconfig_path);
4137 if (error != NULL)
4138 goto done;
4140 if (tagmsg) {
4141 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4142 if (error)
4143 goto done;
4146 if (commit_id_arg == NULL) {
4147 struct got_reference *head_ref;
4148 struct got_object_id *commit_id;
4149 error = got_ref_open(&head_ref, repo,
4150 worktree ? got_worktree_get_head_ref_name(worktree)
4151 : GOT_REF_HEAD, 0);
4152 if (error)
4153 goto done;
4154 error = got_ref_resolve(&commit_id, repo, head_ref);
4155 got_ref_close(head_ref);
4156 if (error)
4157 goto done;
4158 error = got_object_id_str(&commit_id_str, commit_id);
4159 free(commit_id);
4160 if (error)
4161 goto done;
4164 error = add_tag(repo, tag_name,
4165 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4167 done:
4168 if (repo)
4169 got_repo_close(repo);
4170 if (worktree)
4171 got_worktree_close(worktree);
4172 free(cwd);
4173 free(repo_path);
4174 free(gitconfig_path);
4175 free(commit_id_str);
4176 return error;
4179 __dead static void
4180 usage_add(void)
4182 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4183 getprogname());
4184 exit(1);
4187 static const struct got_error *
4188 add_progress(void *arg, unsigned char status, const char *path)
4190 while (path[0] == '/')
4191 path++;
4192 printf("%c %s\n", status, path);
4193 return NULL;
4196 static const struct got_error *
4197 cmd_add(int argc, char *argv[])
4199 const struct got_error *error = NULL;
4200 struct got_repository *repo = NULL;
4201 struct got_worktree *worktree = NULL;
4202 char *cwd = NULL;
4203 struct got_pathlist_head paths;
4204 struct got_pathlist_entry *pe;
4205 int ch, can_recurse = 0, no_ignores = 0;
4207 TAILQ_INIT(&paths);
4209 while ((ch = getopt(argc, argv, "IR")) != -1) {
4210 switch (ch) {
4211 case 'I':
4212 no_ignores = 1;
4213 break;
4214 case 'R':
4215 can_recurse = 1;
4216 break;
4217 default:
4218 usage_add();
4219 /* NOTREACHED */
4223 argc -= optind;
4224 argv += optind;
4226 #ifndef PROFILE
4227 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4228 NULL) == -1)
4229 err(1, "pledge");
4230 #endif
4231 if (argc < 1)
4232 usage_add();
4234 cwd = getcwd(NULL, 0);
4235 if (cwd == NULL) {
4236 error = got_error_from_errno("getcwd");
4237 goto done;
4240 error = got_worktree_open(&worktree, cwd);
4241 if (error)
4242 goto done;
4244 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4245 NULL);
4246 if (error != NULL)
4247 goto done;
4249 error = apply_unveil(got_repo_get_path(repo), 1,
4250 got_worktree_get_root_path(worktree));
4251 if (error)
4252 goto done;
4254 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4255 if (error)
4256 goto done;
4258 if (!can_recurse && no_ignores) {
4259 error = got_error_msg(GOT_ERR_BAD_PATH,
4260 "disregarding ignores requires -R option");
4261 goto done;
4265 if (!can_recurse) {
4266 char *ondisk_path;
4267 struct stat sb;
4268 TAILQ_FOREACH(pe, &paths, entry) {
4269 if (asprintf(&ondisk_path, "%s/%s",
4270 got_worktree_get_root_path(worktree),
4271 pe->path) == -1) {
4272 error = got_error_from_errno("asprintf");
4273 goto done;
4275 if (lstat(ondisk_path, &sb) == -1) {
4276 if (errno == ENOENT) {
4277 free(ondisk_path);
4278 continue;
4280 error = got_error_from_errno2("lstat",
4281 ondisk_path);
4282 free(ondisk_path);
4283 goto done;
4285 free(ondisk_path);
4286 if (S_ISDIR(sb.st_mode)) {
4287 error = got_error_msg(GOT_ERR_BAD_PATH,
4288 "adding directories requires -R option");
4289 goto done;
4294 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4295 NULL, repo, no_ignores);
4296 done:
4297 if (repo)
4298 got_repo_close(repo);
4299 if (worktree)
4300 got_worktree_close(worktree);
4301 TAILQ_FOREACH(pe, &paths, entry)
4302 free((char *)pe->path);
4303 got_pathlist_free(&paths);
4304 free(cwd);
4305 return error;
4308 __dead static void
4309 usage_remove(void)
4311 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4312 getprogname());
4313 exit(1);
4316 static const struct got_error *
4317 print_remove_status(void *arg, unsigned char status,
4318 unsigned char staged_status, const char *path)
4320 while (path[0] == '/')
4321 path++;
4322 if (status == GOT_STATUS_NONEXISTENT)
4323 return NULL;
4324 if (status == staged_status && (status == GOT_STATUS_DELETE))
4325 status = GOT_STATUS_NO_CHANGE;
4326 printf("%c%c %s\n", status, staged_status, path);
4327 return NULL;
4330 static const struct got_error *
4331 cmd_remove(int argc, char *argv[])
4333 const struct got_error *error = NULL;
4334 struct got_worktree *worktree = NULL;
4335 struct got_repository *repo = NULL;
4336 char *cwd = NULL;
4337 struct got_pathlist_head paths;
4338 struct got_pathlist_entry *pe;
4339 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4341 TAILQ_INIT(&paths);
4343 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4344 switch (ch) {
4345 case 'f':
4346 delete_local_mods = 1;
4347 break;
4348 case 'k':
4349 keep_on_disk = 1;
4350 break;
4351 case 'R':
4352 can_recurse = 1;
4353 break;
4354 default:
4355 usage_remove();
4356 /* NOTREACHED */
4360 argc -= optind;
4361 argv += optind;
4363 #ifndef PROFILE
4364 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4365 NULL) == -1)
4366 err(1, "pledge");
4367 #endif
4368 if (argc < 1)
4369 usage_remove();
4371 cwd = getcwd(NULL, 0);
4372 if (cwd == NULL) {
4373 error = got_error_from_errno("getcwd");
4374 goto done;
4376 error = got_worktree_open(&worktree, cwd);
4377 if (error)
4378 goto done;
4380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4381 NULL);
4382 if (error)
4383 goto done;
4385 error = apply_unveil(got_repo_get_path(repo), 1,
4386 got_worktree_get_root_path(worktree));
4387 if (error)
4388 goto done;
4390 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4391 if (error)
4392 goto done;
4394 if (!can_recurse) {
4395 char *ondisk_path;
4396 struct stat sb;
4397 TAILQ_FOREACH(pe, &paths, entry) {
4398 if (asprintf(&ondisk_path, "%s/%s",
4399 got_worktree_get_root_path(worktree),
4400 pe->path) == -1) {
4401 error = got_error_from_errno("asprintf");
4402 goto done;
4404 if (lstat(ondisk_path, &sb) == -1) {
4405 if (errno == ENOENT) {
4406 free(ondisk_path);
4407 continue;
4409 error = got_error_from_errno2("lstat",
4410 ondisk_path);
4411 free(ondisk_path);
4412 goto done;
4414 free(ondisk_path);
4415 if (S_ISDIR(sb.st_mode)) {
4416 error = got_error_msg(GOT_ERR_BAD_PATH,
4417 "removing directories requires -R option");
4418 goto done;
4423 error = got_worktree_schedule_delete(worktree, &paths,
4424 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4425 if (error)
4426 goto done;
4427 done:
4428 if (repo)
4429 got_repo_close(repo);
4430 if (worktree)
4431 got_worktree_close(worktree);
4432 TAILQ_FOREACH(pe, &paths, entry)
4433 free((char *)pe->path);
4434 got_pathlist_free(&paths);
4435 free(cwd);
4436 return error;
4439 __dead static void
4440 usage_revert(void)
4442 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4443 "path ...\n", getprogname());
4444 exit(1);
4447 static const struct got_error *
4448 revert_progress(void *arg, unsigned char status, const char *path)
4450 while (path[0] == '/')
4451 path++;
4452 printf("%c %s\n", status, path);
4453 return NULL;
4456 struct choose_patch_arg {
4457 FILE *patch_script_file;
4458 const char *action;
4461 static const struct got_error *
4462 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4463 int nchanges, const char *action)
4465 char *line = NULL;
4466 size_t linesize = 0;
4467 ssize_t linelen;
4469 switch (status) {
4470 case GOT_STATUS_ADD:
4471 printf("A %s\n%s this addition? [y/n] ", path, action);
4472 break;
4473 case GOT_STATUS_DELETE:
4474 printf("D %s\n%s this deletion? [y/n] ", path, action);
4475 break;
4476 case GOT_STATUS_MODIFY:
4477 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4478 return got_error_from_errno("fseek");
4479 printf(GOT_COMMIT_SEP_STR);
4480 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4481 printf("%s", line);
4482 if (ferror(patch_file))
4483 return got_error_from_errno("getline");
4484 printf(GOT_COMMIT_SEP_STR);
4485 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4486 path, n, nchanges, action);
4487 break;
4488 default:
4489 return got_error_path(path, GOT_ERR_FILE_STATUS);
4492 return NULL;
4495 static const struct got_error *
4496 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4497 FILE *patch_file, int n, int nchanges)
4499 const struct got_error *err = NULL;
4500 char *line = NULL;
4501 size_t linesize = 0;
4502 ssize_t linelen;
4503 int resp = ' ';
4504 struct choose_patch_arg *a = arg;
4506 *choice = GOT_PATCH_CHOICE_NONE;
4508 if (a->patch_script_file) {
4509 char *nl;
4510 err = show_change(status, path, patch_file, n, nchanges,
4511 a->action);
4512 if (err)
4513 return err;
4514 linelen = getline(&line, &linesize, a->patch_script_file);
4515 if (linelen == -1) {
4516 if (ferror(a->patch_script_file))
4517 return got_error_from_errno("getline");
4518 return NULL;
4520 nl = strchr(line, '\n');
4521 if (nl)
4522 *nl = '\0';
4523 if (strcmp(line, "y") == 0) {
4524 *choice = GOT_PATCH_CHOICE_YES;
4525 printf("y\n");
4526 } else if (strcmp(line, "n") == 0) {
4527 *choice = GOT_PATCH_CHOICE_NO;
4528 printf("n\n");
4529 } else if (strcmp(line, "q") == 0 &&
4530 status == GOT_STATUS_MODIFY) {
4531 *choice = GOT_PATCH_CHOICE_QUIT;
4532 printf("q\n");
4533 } else
4534 printf("invalid response '%s'\n", line);
4535 free(line);
4536 return NULL;
4539 while (resp != 'y' && resp != 'n' && resp != 'q') {
4540 err = show_change(status, path, patch_file, n, nchanges,
4541 a->action);
4542 if (err)
4543 return err;
4544 resp = getchar();
4545 if (resp == '\n')
4546 resp = getchar();
4547 if (status == GOT_STATUS_MODIFY) {
4548 if (resp != 'y' && resp != 'n' && resp != 'q') {
4549 printf("invalid response '%c'\n", resp);
4550 resp = ' ';
4552 } else if (resp != 'y' && resp != 'n') {
4553 printf("invalid response '%c'\n", resp);
4554 resp = ' ';
4558 if (resp == 'y')
4559 *choice = GOT_PATCH_CHOICE_YES;
4560 else if (resp == 'n')
4561 *choice = GOT_PATCH_CHOICE_NO;
4562 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4563 *choice = GOT_PATCH_CHOICE_QUIT;
4565 return NULL;
4569 static const struct got_error *
4570 cmd_revert(int argc, char *argv[])
4572 const struct got_error *error = NULL;
4573 struct got_worktree *worktree = NULL;
4574 struct got_repository *repo = NULL;
4575 char *cwd = NULL, *path = NULL;
4576 struct got_pathlist_head paths;
4577 struct got_pathlist_entry *pe;
4578 int ch, can_recurse = 0, pflag = 0;
4579 FILE *patch_script_file = NULL;
4580 const char *patch_script_path = NULL;
4581 struct choose_patch_arg cpa;
4583 TAILQ_INIT(&paths);
4585 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4586 switch (ch) {
4587 case 'p':
4588 pflag = 1;
4589 break;
4590 case 'F':
4591 patch_script_path = optarg;
4592 break;
4593 case 'R':
4594 can_recurse = 1;
4595 break;
4596 default:
4597 usage_revert();
4598 /* NOTREACHED */
4602 argc -= optind;
4603 argv += optind;
4605 #ifndef PROFILE
4606 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4607 "unveil", NULL) == -1)
4608 err(1, "pledge");
4609 #endif
4610 if (argc < 1)
4611 usage_revert();
4612 if (patch_script_path && !pflag)
4613 errx(1, "-F option can only be used together with -p option");
4615 cwd = getcwd(NULL, 0);
4616 if (cwd == NULL) {
4617 error = got_error_from_errno("getcwd");
4618 goto done;
4620 error = got_worktree_open(&worktree, cwd);
4621 if (error)
4622 goto done;
4624 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4625 NULL);
4626 if (error != NULL)
4627 goto done;
4629 if (patch_script_path) {
4630 patch_script_file = fopen(patch_script_path, "r");
4631 if (patch_script_file == NULL) {
4632 error = got_error_from_errno2("fopen",
4633 patch_script_path);
4634 goto done;
4637 error = apply_unveil(got_repo_get_path(repo), 1,
4638 got_worktree_get_root_path(worktree));
4639 if (error)
4640 goto done;
4642 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4643 if (error)
4644 goto done;
4646 if (!can_recurse) {
4647 char *ondisk_path;
4648 struct stat sb;
4649 TAILQ_FOREACH(pe, &paths, entry) {
4650 if (asprintf(&ondisk_path, "%s/%s",
4651 got_worktree_get_root_path(worktree),
4652 pe->path) == -1) {
4653 error = got_error_from_errno("asprintf");
4654 goto done;
4656 if (lstat(ondisk_path, &sb) == -1) {
4657 if (errno == ENOENT) {
4658 free(ondisk_path);
4659 continue;
4661 error = got_error_from_errno2("lstat",
4662 ondisk_path);
4663 free(ondisk_path);
4664 goto done;
4666 free(ondisk_path);
4667 if (S_ISDIR(sb.st_mode)) {
4668 error = got_error_msg(GOT_ERR_BAD_PATH,
4669 "reverting directories requires -R option");
4670 goto done;
4675 cpa.patch_script_file = patch_script_file;
4676 cpa.action = "revert";
4677 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4678 pflag ? choose_patch : NULL, &cpa, repo);
4679 if (error)
4680 goto done;
4681 done:
4682 if (patch_script_file && fclose(patch_script_file) == EOF &&
4683 error == NULL)
4684 error = got_error_from_errno2("fclose", patch_script_path);
4685 if (repo)
4686 got_repo_close(repo);
4687 if (worktree)
4688 got_worktree_close(worktree);
4689 free(path);
4690 free(cwd);
4691 return error;
4694 __dead static void
4695 usage_commit(void)
4697 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4698 getprogname());
4699 exit(1);
4702 struct collect_commit_logmsg_arg {
4703 const char *cmdline_log;
4704 const char *editor;
4705 const char *worktree_path;
4706 const char *branch_name;
4707 const char *repo_path;
4708 char *logmsg_path;
4712 static const struct got_error *
4713 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4714 void *arg)
4716 char *initial_content = NULL;
4717 struct got_pathlist_entry *pe;
4718 const struct got_error *err = NULL;
4719 char *template = NULL;
4720 struct collect_commit_logmsg_arg *a = arg;
4721 int fd;
4722 size_t len;
4724 /* if a message was specified on the command line, just use it */
4725 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4726 len = strlen(a->cmdline_log) + 1;
4727 *logmsg = malloc(len + 1);
4728 if (*logmsg == NULL)
4729 return got_error_from_errno("malloc");
4730 strlcpy(*logmsg, a->cmdline_log, len);
4731 return NULL;
4734 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4735 return got_error_from_errno("asprintf");
4737 if (asprintf(&initial_content,
4738 "\n# changes to be committed on branch %s:\n",
4739 a->branch_name) == -1)
4740 return got_error_from_errno("asprintf");
4742 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4743 if (err)
4744 goto done;
4746 dprintf(fd, initial_content);
4748 TAILQ_FOREACH(pe, commitable_paths, entry) {
4749 struct got_commitable *ct = pe->data;
4750 dprintf(fd, "# %c %s\n",
4751 got_commitable_get_status(ct),
4752 got_commitable_get_path(ct));
4754 close(fd);
4756 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4757 done:
4758 free(initial_content);
4759 free(template);
4761 /* Editor is done; we can now apply unveil(2) */
4762 if (err == NULL) {
4763 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4764 if (err) {
4765 free(*logmsg);
4766 *logmsg = NULL;
4769 return err;
4772 static const struct got_error *
4773 cmd_commit(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *id_str = NULL;
4779 struct got_object_id *id = NULL;
4780 const char *logmsg = NULL;
4781 struct collect_commit_logmsg_arg cl_arg;
4782 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4783 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4784 struct got_pathlist_head paths;
4786 TAILQ_INIT(&paths);
4787 cl_arg.logmsg_path = NULL;
4789 while ((ch = getopt(argc, argv, "m:")) != -1) {
4790 switch (ch) {
4791 case 'm':
4792 logmsg = optarg;
4793 break;
4794 default:
4795 usage_commit();
4796 /* NOTREACHED */
4800 argc -= optind;
4801 argv += optind;
4803 #ifndef PROFILE
4804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4805 "unveil", NULL) == -1)
4806 err(1, "pledge");
4807 #endif
4808 cwd = getcwd(NULL, 0);
4809 if (cwd == NULL) {
4810 error = got_error_from_errno("getcwd");
4811 goto done;
4813 error = got_worktree_open(&worktree, cwd);
4814 if (error)
4815 goto done;
4817 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4818 if (error)
4819 goto done;
4820 if (rebase_in_progress) {
4821 error = got_error(GOT_ERR_REBASING);
4822 goto done;
4825 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4826 worktree);
4827 if (error)
4828 goto done;
4830 error = get_gitconfig_path(&gitconfig_path);
4831 if (error)
4832 goto done;
4833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4834 gitconfig_path);
4835 if (error != NULL)
4836 goto done;
4838 error = get_author(&author, repo);
4839 if (error)
4840 return error;
4843 * unveil(2) traverses exec(2); if an editor is used we have
4844 * to apply unveil after the log message has been written.
4846 if (logmsg == NULL || strlen(logmsg) == 0)
4847 error = get_editor(&editor);
4848 else
4849 error = apply_unveil(got_repo_get_path(repo), 0,
4850 got_worktree_get_root_path(worktree));
4851 if (error)
4852 goto done;
4854 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4855 if (error)
4856 goto done;
4858 cl_arg.editor = editor;
4859 cl_arg.cmdline_log = logmsg;
4860 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4861 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4862 if (!histedit_in_progress) {
4863 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4864 error = got_error(GOT_ERR_COMMIT_BRANCH);
4865 goto done;
4867 cl_arg.branch_name += 11;
4869 cl_arg.repo_path = got_repo_get_path(repo);
4870 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4871 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4872 if (error) {
4873 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4874 cl_arg.logmsg_path != NULL)
4875 preserve_logmsg = 1;
4876 goto done;
4879 error = got_object_id_str(&id_str, id);
4880 if (error)
4881 goto done;
4882 printf("Created commit %s\n", id_str);
4883 done:
4884 if (preserve_logmsg) {
4885 fprintf(stderr, "%s: log message preserved in %s\n",
4886 getprogname(), cl_arg.logmsg_path);
4887 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4888 error == NULL)
4889 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4890 free(cl_arg.logmsg_path);
4891 if (repo)
4892 got_repo_close(repo);
4893 if (worktree)
4894 got_worktree_close(worktree);
4895 free(cwd);
4896 free(id_str);
4897 free(gitconfig_path);
4898 free(editor);
4899 free(author);
4900 return error;
4903 __dead static void
4904 usage_cherrypick(void)
4906 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4907 exit(1);
4910 static const struct got_error *
4911 cmd_cherrypick(int argc, char *argv[])
4913 const struct got_error *error = NULL;
4914 struct got_worktree *worktree = NULL;
4915 struct got_repository *repo = NULL;
4916 char *cwd = NULL, *commit_id_str = NULL;
4917 struct got_object_id *commit_id = NULL;
4918 struct got_commit_object *commit = NULL;
4919 struct got_object_qid *pid;
4920 struct got_reference *head_ref = NULL;
4921 int ch, did_something = 0;
4923 while ((ch = getopt(argc, argv, "")) != -1) {
4924 switch (ch) {
4925 default:
4926 usage_cherrypick();
4927 /* NOTREACHED */
4931 argc -= optind;
4932 argv += optind;
4934 #ifndef PROFILE
4935 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4936 "unveil", NULL) == -1)
4937 err(1, "pledge");
4938 #endif
4939 if (argc != 1)
4940 usage_cherrypick();
4942 cwd = getcwd(NULL, 0);
4943 if (cwd == NULL) {
4944 error = got_error_from_errno("getcwd");
4945 goto done;
4947 error = got_worktree_open(&worktree, cwd);
4948 if (error)
4949 goto done;
4951 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4952 NULL);
4953 if (error != NULL)
4954 goto done;
4956 error = apply_unveil(got_repo_get_path(repo), 0,
4957 got_worktree_get_root_path(worktree));
4958 if (error)
4959 goto done;
4961 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4962 GOT_OBJ_TYPE_COMMIT, repo);
4963 if (error != NULL) {
4964 struct got_reference *ref;
4965 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4966 goto done;
4967 error = got_ref_open(&ref, repo, argv[0], 0);
4968 if (error != NULL)
4969 goto done;
4970 error = got_ref_resolve(&commit_id, repo, ref);
4971 got_ref_close(ref);
4972 if (error != NULL)
4973 goto done;
4975 error = got_object_id_str(&commit_id_str, commit_id);
4976 if (error)
4977 goto done;
4979 error = got_ref_open(&head_ref, repo,
4980 got_worktree_get_head_ref_name(worktree), 0);
4981 if (error != NULL)
4982 goto done;
4984 error = check_same_branch(commit_id, head_ref, NULL, repo);
4985 if (error) {
4986 if (error->code != GOT_ERR_ANCESTRY)
4987 goto done;
4988 error = NULL;
4989 } else {
4990 error = got_error(GOT_ERR_SAME_BRANCH);
4991 goto done;
4994 error = got_object_open_as_commit(&commit, repo, commit_id);
4995 if (error)
4996 goto done;
4997 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4998 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4999 commit_id, repo, update_progress, &did_something, check_cancelled,
5000 NULL);
5001 if (error != NULL)
5002 goto done;
5004 if (did_something)
5005 printf("Merged commit %s\n", commit_id_str);
5006 done:
5007 if (commit)
5008 got_object_commit_close(commit);
5009 free(commit_id_str);
5010 if (head_ref)
5011 got_ref_close(head_ref);
5012 if (worktree)
5013 got_worktree_close(worktree);
5014 if (repo)
5015 got_repo_close(repo);
5016 return error;
5019 __dead static void
5020 usage_backout(void)
5022 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5023 exit(1);
5026 static const struct got_error *
5027 cmd_backout(int argc, char *argv[])
5029 const struct got_error *error = NULL;
5030 struct got_worktree *worktree = NULL;
5031 struct got_repository *repo = NULL;
5032 char *cwd = NULL, *commit_id_str = NULL;
5033 struct got_object_id *commit_id = NULL;
5034 struct got_commit_object *commit = NULL;
5035 struct got_object_qid *pid;
5036 struct got_reference *head_ref = NULL;
5037 int ch, did_something = 0;
5039 while ((ch = getopt(argc, argv, "")) != -1) {
5040 switch (ch) {
5041 default:
5042 usage_backout();
5043 /* NOTREACHED */
5047 argc -= optind;
5048 argv += optind;
5050 #ifndef PROFILE
5051 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5052 "unveil", NULL) == -1)
5053 err(1, "pledge");
5054 #endif
5055 if (argc != 1)
5056 usage_backout();
5058 cwd = getcwd(NULL, 0);
5059 if (cwd == NULL) {
5060 error = got_error_from_errno("getcwd");
5061 goto done;
5063 error = got_worktree_open(&worktree, cwd);
5064 if (error)
5065 goto done;
5067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5068 NULL);
5069 if (error != NULL)
5070 goto done;
5072 error = apply_unveil(got_repo_get_path(repo), 0,
5073 got_worktree_get_root_path(worktree));
5074 if (error)
5075 goto done;
5077 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5078 GOT_OBJ_TYPE_COMMIT, repo);
5079 if (error != NULL) {
5080 struct got_reference *ref;
5081 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5082 goto done;
5083 error = got_ref_open(&ref, repo, argv[0], 0);
5084 if (error != NULL)
5085 goto done;
5086 error = got_ref_resolve(&commit_id, repo, ref);
5087 got_ref_close(ref);
5088 if (error != NULL)
5089 goto done;
5091 error = got_object_id_str(&commit_id_str, commit_id);
5092 if (error)
5093 goto done;
5095 error = got_ref_open(&head_ref, repo,
5096 got_worktree_get_head_ref_name(worktree), 0);
5097 if (error != NULL)
5098 goto done;
5100 error = check_same_branch(commit_id, head_ref, NULL, repo);
5101 if (error)
5102 goto done;
5104 error = got_object_open_as_commit(&commit, repo, commit_id);
5105 if (error)
5106 goto done;
5107 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5108 if (pid == NULL) {
5109 error = got_error(GOT_ERR_ROOT_COMMIT);
5110 goto done;
5113 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5114 update_progress, &did_something, check_cancelled, NULL);
5115 if (error != NULL)
5116 goto done;
5118 if (did_something)
5119 printf("Backed out commit %s\n", commit_id_str);
5120 done:
5121 if (commit)
5122 got_object_commit_close(commit);
5123 free(commit_id_str);
5124 if (head_ref)
5125 got_ref_close(head_ref);
5126 if (worktree)
5127 got_worktree_close(worktree);
5128 if (repo)
5129 got_repo_close(repo);
5130 return error;
5133 __dead static void
5134 usage_rebase(void)
5136 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5137 getprogname());
5138 exit(1);
5141 void
5142 trim_logmsg(char *logmsg, int limit)
5144 char *nl;
5145 size_t len;
5147 len = strlen(logmsg);
5148 if (len > limit)
5149 len = limit;
5150 logmsg[len] = '\0';
5151 nl = strchr(logmsg, '\n');
5152 if (nl)
5153 *nl = '\0';
5156 static const struct got_error *
5157 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5159 const struct got_error *err;
5160 char *logmsg0 = NULL;
5161 const char *s;
5163 err = got_object_commit_get_logmsg(&logmsg0, commit);
5164 if (err)
5165 return err;
5167 s = logmsg0;
5168 while (isspace((unsigned char)s[0]))
5169 s++;
5171 *logmsg = strdup(s);
5172 if (*logmsg == NULL) {
5173 err = got_error_from_errno("strdup");
5174 goto done;
5177 trim_logmsg(*logmsg, limit);
5178 done:
5179 free(logmsg0);
5180 return err;
5183 static const struct got_error *
5184 show_rebase_progress(struct got_commit_object *commit,
5185 struct got_object_id *old_id, struct got_object_id *new_id)
5187 const struct got_error *err;
5188 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5190 err = got_object_id_str(&old_id_str, old_id);
5191 if (err)
5192 goto done;
5194 if (new_id) {
5195 err = got_object_id_str(&new_id_str, new_id);
5196 if (err)
5197 goto done;
5200 old_id_str[12] = '\0';
5201 if (new_id_str)
5202 new_id_str[12] = '\0';
5204 err = get_short_logmsg(&logmsg, 42, commit);
5205 if (err)
5206 goto done;
5208 printf("%s -> %s: %s\n", old_id_str,
5209 new_id_str ? new_id_str : "no-op change", logmsg);
5210 done:
5211 free(old_id_str);
5212 free(new_id_str);
5213 return err;
5216 static const struct got_error *
5217 rebase_progress(void *arg, unsigned char status, const char *path)
5219 unsigned char *rebase_status = arg;
5221 while (path[0] == '/')
5222 path++;
5223 printf("%c %s\n", status, path);
5225 if (*rebase_status == GOT_STATUS_CONFLICT)
5226 return NULL;
5227 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5228 *rebase_status = status;
5229 return NULL;
5232 static const struct got_error *
5233 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5234 struct got_reference *branch, struct got_reference *new_base_branch,
5235 struct got_reference *tmp_branch, struct got_repository *repo)
5237 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5238 return got_worktree_rebase_complete(worktree, fileindex,
5239 new_base_branch, tmp_branch, branch, repo);
5242 static const struct got_error *
5243 rebase_commit(struct got_pathlist_head *merged_paths,
5244 struct got_worktree *worktree, struct got_fileindex *fileindex,
5245 struct got_reference *tmp_branch,
5246 struct got_object_id *commit_id, struct got_repository *repo)
5248 const struct got_error *error;
5249 struct got_commit_object *commit;
5250 struct got_object_id *new_commit_id;
5252 error = got_object_open_as_commit(&commit, repo, commit_id);
5253 if (error)
5254 return error;
5256 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5257 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5258 if (error) {
5259 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5260 goto done;
5261 error = show_rebase_progress(commit, commit_id, NULL);
5262 } else {
5263 error = show_rebase_progress(commit, commit_id, new_commit_id);
5264 free(new_commit_id);
5266 done:
5267 got_object_commit_close(commit);
5268 return error;
5271 struct check_path_prefix_arg {
5272 const char *path_prefix;
5273 size_t len;
5274 int errcode;
5277 static const struct got_error *
5278 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5279 struct got_blob_object *blob2, struct got_object_id *id1,
5280 struct got_object_id *id2, const char *path1, const char *path2,
5281 mode_t mode1, mode_t mode2, struct got_repository *repo)
5283 struct check_path_prefix_arg *a = arg;
5285 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5286 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5287 return got_error(a->errcode);
5289 return NULL;
5292 static const struct got_error *
5293 check_path_prefix(struct got_object_id *parent_id,
5294 struct got_object_id *commit_id, const char *path_prefix,
5295 int errcode, struct got_repository *repo)
5297 const struct got_error *err;
5298 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5299 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5300 struct check_path_prefix_arg cpp_arg;
5302 if (got_path_is_root_dir(path_prefix))
5303 return NULL;
5305 err = got_object_open_as_commit(&commit, repo, commit_id);
5306 if (err)
5307 goto done;
5309 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5310 if (err)
5311 goto done;
5313 err = got_object_open_as_tree(&tree1, repo,
5314 got_object_commit_get_tree_id(parent_commit));
5315 if (err)
5316 goto done;
5318 err = got_object_open_as_tree(&tree2, repo,
5319 got_object_commit_get_tree_id(commit));
5320 if (err)
5321 goto done;
5323 cpp_arg.path_prefix = path_prefix;
5324 while (cpp_arg.path_prefix[0] == '/')
5325 cpp_arg.path_prefix++;
5326 cpp_arg.len = strlen(cpp_arg.path_prefix);
5327 cpp_arg.errcode = errcode;
5328 err = got_diff_tree(tree1, tree2, "", "", repo,
5329 check_path_prefix_in_diff, &cpp_arg, 0);
5330 done:
5331 if (tree1)
5332 got_object_tree_close(tree1);
5333 if (tree2)
5334 got_object_tree_close(tree2);
5335 if (commit)
5336 got_object_commit_close(commit);
5337 if (parent_commit)
5338 got_object_commit_close(parent_commit);
5339 return err;
5342 static const struct got_error *
5343 collect_commits(struct got_object_id_queue *commits,
5344 struct got_object_id *initial_commit_id,
5345 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5346 const char *path_prefix, int path_prefix_errcode,
5347 struct got_repository *repo)
5349 const struct got_error *err = NULL;
5350 struct got_commit_graph *graph = NULL;
5351 struct got_object_id *parent_id = NULL;
5352 struct got_object_qid *qid;
5353 struct got_object_id *commit_id = initial_commit_id;
5355 err = got_commit_graph_open(&graph, "/", 1);
5356 if (err)
5357 return err;
5359 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5360 check_cancelled, NULL);
5361 if (err)
5362 goto done;
5363 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5364 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5365 check_cancelled, NULL);
5366 if (err) {
5367 if (err->code == GOT_ERR_ITER_COMPLETED) {
5368 err = got_error_msg(GOT_ERR_ANCESTRY,
5369 "ran out of commits to rebase before "
5370 "youngest common ancestor commit has "
5371 "been reached?!?");
5373 goto done;
5374 } else {
5375 err = check_path_prefix(parent_id, commit_id,
5376 path_prefix, path_prefix_errcode, repo);
5377 if (err)
5378 goto done;
5380 err = got_object_qid_alloc(&qid, commit_id);
5381 if (err)
5382 goto done;
5383 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5384 commit_id = parent_id;
5387 done:
5388 got_commit_graph_close(graph);
5389 return err;
5392 static const struct got_error *
5393 cmd_rebase(int argc, char *argv[])
5395 const struct got_error *error = NULL;
5396 struct got_worktree *worktree = NULL;
5397 struct got_repository *repo = NULL;
5398 struct got_fileindex *fileindex = NULL;
5399 char *cwd = NULL;
5400 struct got_reference *branch = NULL;
5401 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5402 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5403 struct got_object_id *resume_commit_id = NULL;
5404 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5405 struct got_commit_object *commit = NULL;
5406 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5407 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5408 struct got_object_id_queue commits;
5409 struct got_pathlist_head merged_paths;
5410 const struct got_object_id_queue *parent_ids;
5411 struct got_object_qid *qid, *pid;
5413 SIMPLEQ_INIT(&commits);
5414 TAILQ_INIT(&merged_paths);
5416 while ((ch = getopt(argc, argv, "ac")) != -1) {
5417 switch (ch) {
5418 case 'a':
5419 abort_rebase = 1;
5420 break;
5421 case 'c':
5422 continue_rebase = 1;
5423 break;
5424 default:
5425 usage_rebase();
5426 /* NOTREACHED */
5430 argc -= optind;
5431 argv += optind;
5433 #ifndef PROFILE
5434 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5435 "unveil", NULL) == -1)
5436 err(1, "pledge");
5437 #endif
5438 if (abort_rebase && continue_rebase)
5439 usage_rebase();
5440 else if (abort_rebase || continue_rebase) {
5441 if (argc != 0)
5442 usage_rebase();
5443 } else if (argc != 1)
5444 usage_rebase();
5446 cwd = getcwd(NULL, 0);
5447 if (cwd == NULL) {
5448 error = got_error_from_errno("getcwd");
5449 goto done;
5451 error = got_worktree_open(&worktree, cwd);
5452 if (error)
5453 goto done;
5455 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5456 NULL);
5457 if (error != NULL)
5458 goto done;
5460 error = apply_unveil(got_repo_get_path(repo), 0,
5461 got_worktree_get_root_path(worktree));
5462 if (error)
5463 goto done;
5465 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5466 if (error)
5467 goto done;
5469 if (abort_rebase) {
5470 int did_something;
5471 if (!rebase_in_progress) {
5472 error = got_error(GOT_ERR_NOT_REBASING);
5473 goto done;
5475 error = got_worktree_rebase_continue(&resume_commit_id,
5476 &new_base_branch, &tmp_branch, &branch, &fileindex,
5477 worktree, repo);
5478 if (error)
5479 goto done;
5480 printf("Switching work tree to %s\n",
5481 got_ref_get_symref_target(new_base_branch));
5482 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5483 new_base_branch, update_progress, &did_something);
5484 if (error)
5485 goto done;
5486 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5487 goto done; /* nothing else to do */
5490 if (continue_rebase) {
5491 if (!rebase_in_progress) {
5492 error = got_error(GOT_ERR_NOT_REBASING);
5493 goto done;
5495 error = got_worktree_rebase_continue(&resume_commit_id,
5496 &new_base_branch, &tmp_branch, &branch, &fileindex,
5497 worktree, repo);
5498 if (error)
5499 goto done;
5501 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5502 resume_commit_id, repo);
5503 if (error)
5504 goto done;
5506 yca_id = got_object_id_dup(resume_commit_id);
5507 if (yca_id == NULL) {
5508 error = got_error_from_errno("got_object_id_dup");
5509 goto done;
5511 } else {
5512 error = got_ref_open(&branch, repo, argv[0], 0);
5513 if (error != NULL)
5514 goto done;
5517 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5518 if (error)
5519 goto done;
5521 if (!continue_rebase) {
5522 struct got_object_id *base_commit_id;
5524 base_commit_id = got_worktree_get_base_commit_id(worktree);
5525 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5526 base_commit_id, branch_head_commit_id, repo,
5527 check_cancelled, NULL);
5528 if (error)
5529 goto done;
5530 if (yca_id == NULL) {
5531 error = got_error_msg(GOT_ERR_ANCESTRY,
5532 "specified branch shares no common ancestry "
5533 "with work tree's branch");
5534 goto done;
5537 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5538 if (error) {
5539 if (error->code != GOT_ERR_ANCESTRY)
5540 goto done;
5541 error = NULL;
5542 } else {
5543 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5544 "specified branch resolves to a commit which "
5545 "is already contained in work tree's branch");
5546 goto done;
5548 error = got_worktree_rebase_prepare(&new_base_branch,
5549 &tmp_branch, &fileindex, worktree, branch, repo);
5550 if (error)
5551 goto done;
5554 commit_id = branch_head_commit_id;
5555 error = got_object_open_as_commit(&commit, repo, commit_id);
5556 if (error)
5557 goto done;
5559 parent_ids = got_object_commit_get_parent_ids(commit);
5560 pid = SIMPLEQ_FIRST(parent_ids);
5561 if (pid == NULL) {
5562 if (!continue_rebase) {
5563 int did_something;
5564 error = got_worktree_rebase_abort(worktree, fileindex,
5565 repo, new_base_branch, update_progress,
5566 &did_something);
5567 if (error)
5568 goto done;
5569 printf("Rebase of %s aborted\n",
5570 got_ref_get_name(branch));
5572 error = got_error(GOT_ERR_EMPTY_REBASE);
5573 goto done;
5575 error = collect_commits(&commits, commit_id, pid->id,
5576 yca_id, got_worktree_get_path_prefix(worktree),
5577 GOT_ERR_REBASE_PATH, repo);
5578 got_object_commit_close(commit);
5579 commit = NULL;
5580 if (error)
5581 goto done;
5583 if (SIMPLEQ_EMPTY(&commits)) {
5584 if (continue_rebase) {
5585 error = rebase_complete(worktree, fileindex,
5586 branch, new_base_branch, tmp_branch, repo);
5587 goto done;
5588 } else {
5589 /* Fast-forward the reference of the branch. */
5590 struct got_object_id *new_head_commit_id;
5591 char *id_str;
5592 error = got_ref_resolve(&new_head_commit_id, repo,
5593 new_base_branch);
5594 if (error)
5595 goto done;
5596 error = got_object_id_str(&id_str, new_head_commit_id);
5597 printf("Forwarding %s to commit %s\n",
5598 got_ref_get_name(branch), id_str);
5599 free(id_str);
5600 error = got_ref_change_ref(branch,
5601 new_head_commit_id);
5602 if (error)
5603 goto done;
5607 pid = NULL;
5608 SIMPLEQ_FOREACH(qid, &commits, entry) {
5609 commit_id = qid->id;
5610 parent_id = pid ? pid->id : yca_id;
5611 pid = qid;
5613 error = got_worktree_rebase_merge_files(&merged_paths,
5614 worktree, fileindex, parent_id, commit_id, repo,
5615 rebase_progress, &rebase_status, check_cancelled, NULL);
5616 if (error)
5617 goto done;
5619 if (rebase_status == GOT_STATUS_CONFLICT) {
5620 got_worktree_rebase_pathlist_free(&merged_paths);
5621 break;
5624 error = rebase_commit(&merged_paths, worktree, fileindex,
5625 tmp_branch, commit_id, repo);
5626 got_worktree_rebase_pathlist_free(&merged_paths);
5627 if (error)
5628 goto done;
5631 if (rebase_status == GOT_STATUS_CONFLICT) {
5632 error = got_worktree_rebase_postpone(worktree, fileindex);
5633 if (error)
5634 goto done;
5635 error = got_error_msg(GOT_ERR_CONFLICTS,
5636 "conflicts must be resolved before rebasing can continue");
5637 } else
5638 error = rebase_complete(worktree, fileindex, branch,
5639 new_base_branch, tmp_branch, repo);
5640 done:
5641 got_object_id_queue_free(&commits);
5642 free(branch_head_commit_id);
5643 free(resume_commit_id);
5644 free(yca_id);
5645 if (commit)
5646 got_object_commit_close(commit);
5647 if (branch)
5648 got_ref_close(branch);
5649 if (new_base_branch)
5650 got_ref_close(new_base_branch);
5651 if (tmp_branch)
5652 got_ref_close(tmp_branch);
5653 if (worktree)
5654 got_worktree_close(worktree);
5655 if (repo)
5656 got_repo_close(repo);
5657 return error;
5660 __dead static void
5661 usage_histedit(void)
5663 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5664 getprogname());
5665 exit(1);
5668 #define GOT_HISTEDIT_PICK 'p'
5669 #define GOT_HISTEDIT_EDIT 'e'
5670 #define GOT_HISTEDIT_FOLD 'f'
5671 #define GOT_HISTEDIT_DROP 'd'
5672 #define GOT_HISTEDIT_MESG 'm'
5674 static struct got_histedit_cmd {
5675 unsigned char code;
5676 const char *name;
5677 const char *desc;
5678 } got_histedit_cmds[] = {
5679 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5680 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5681 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5682 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5683 { GOT_HISTEDIT_MESG, "mesg",
5684 "single-line log message for commit above (open editor if empty)" },
5687 struct got_histedit_list_entry {
5688 TAILQ_ENTRY(got_histedit_list_entry) entry;
5689 struct got_object_id *commit_id;
5690 const struct got_histedit_cmd *cmd;
5691 char *logmsg;
5693 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5695 static const struct got_error *
5696 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5697 FILE *f, struct got_repository *repo)
5699 const struct got_error *err = NULL;
5700 char *logmsg = NULL, *id_str = NULL;
5701 struct got_commit_object *commit = NULL;
5702 int n;
5704 err = got_object_open_as_commit(&commit, repo, commit_id);
5705 if (err)
5706 goto done;
5708 err = get_short_logmsg(&logmsg, 34, commit);
5709 if (err)
5710 goto done;
5712 err = got_object_id_str(&id_str, commit_id);
5713 if (err)
5714 goto done;
5716 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5717 if (n < 0)
5718 err = got_ferror(f, GOT_ERR_IO);
5719 done:
5720 if (commit)
5721 got_object_commit_close(commit);
5722 free(id_str);
5723 free(logmsg);
5724 return err;
5727 static const struct got_error *
5728 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5729 struct got_repository *repo)
5731 const struct got_error *err = NULL;
5732 struct got_object_qid *qid;
5734 if (SIMPLEQ_EMPTY(commits))
5735 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5737 SIMPLEQ_FOREACH(qid, commits, entry) {
5738 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5739 f, repo);
5740 if (err)
5741 break;
5744 return err;
5747 static const struct got_error *
5748 write_cmd_list(FILE *f)
5750 const struct got_error *err = NULL;
5751 int n, i;
5753 n = fprintf(f, "# Available histedit commands:\n");
5754 if (n < 0)
5755 return got_ferror(f, GOT_ERR_IO);
5757 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5758 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5759 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5760 cmd->desc);
5761 if (n < 0) {
5762 err = got_ferror(f, GOT_ERR_IO);
5763 break;
5766 n = fprintf(f, "# Commits will be processed in order from top to "
5767 "bottom of this file.\n");
5768 if (n < 0)
5769 return got_ferror(f, GOT_ERR_IO);
5770 return err;
5773 static const struct got_error *
5774 histedit_syntax_error(int lineno)
5776 static char msg[42];
5777 int ret;
5779 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5780 lineno);
5781 if (ret == -1 || ret >= sizeof(msg))
5782 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5784 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5787 static const struct got_error *
5788 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5789 char *logmsg, struct got_repository *repo)
5791 const struct got_error *err;
5792 struct got_commit_object *folded_commit = NULL;
5793 char *id_str, *folded_logmsg = NULL;
5795 err = got_object_id_str(&id_str, hle->commit_id);
5796 if (err)
5797 return err;
5799 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5800 if (err)
5801 goto done;
5803 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5804 if (err)
5805 goto done;
5806 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5807 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5808 folded_logmsg) == -1) {
5809 err = got_error_from_errno("asprintf");
5810 goto done;
5812 done:
5813 if (folded_commit)
5814 got_object_commit_close(folded_commit);
5815 free(id_str);
5816 free(folded_logmsg);
5817 return err;
5820 static struct got_histedit_list_entry *
5821 get_folded_commits(struct got_histedit_list_entry *hle)
5823 struct got_histedit_list_entry *prev, *folded = NULL;
5825 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5826 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5827 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5828 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5829 folded = prev;
5830 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5833 return folded;
5836 static const struct got_error *
5837 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5838 struct got_repository *repo)
5840 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5841 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5842 const struct got_error *err = NULL;
5843 struct got_commit_object *commit = NULL;
5844 int fd;
5845 struct got_histedit_list_entry *folded = NULL;
5847 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5848 if (err)
5849 return err;
5851 folded = get_folded_commits(hle);
5852 if (folded) {
5853 while (folded != hle) {
5854 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5855 folded = TAILQ_NEXT(folded, entry);
5856 continue;
5858 err = append_folded_commit_msg(&new_msg, folded,
5859 logmsg, repo);
5860 if (err)
5861 goto done;
5862 free(logmsg);
5863 logmsg = new_msg;
5864 folded = TAILQ_NEXT(folded, entry);
5868 err = got_object_id_str(&id_str, hle->commit_id);
5869 if (err)
5870 goto done;
5871 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5872 if (err)
5873 goto done;
5874 if (asprintf(&new_msg,
5875 "%s\n# original log message of commit %s: %s",
5876 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5877 err = got_error_from_errno("asprintf");
5878 goto done;
5880 free(logmsg);
5881 logmsg = new_msg;
5883 err = got_object_id_str(&id_str, hle->commit_id);
5884 if (err)
5885 goto done;
5887 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5888 if (err)
5889 goto done;
5891 dprintf(fd, logmsg);
5892 close(fd);
5894 err = get_editor(&editor);
5895 if (err)
5896 goto done;
5898 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5899 if (err) {
5900 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5901 goto done;
5902 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5904 done:
5905 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5906 err = got_error_from_errno2("unlink", logmsg_path);
5907 free(logmsg_path);
5908 free(logmsg);
5909 free(orig_logmsg);
5910 free(editor);
5911 if (commit)
5912 got_object_commit_close(commit);
5913 return err;
5916 static const struct got_error *
5917 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5918 FILE *f, struct got_repository *repo)
5920 const struct got_error *err = NULL;
5921 char *line = NULL, *p, *end;
5922 size_t size;
5923 ssize_t len;
5924 int lineno = 0, i;
5925 const struct got_histedit_cmd *cmd;
5926 struct got_object_id *commit_id = NULL;
5927 struct got_histedit_list_entry *hle = NULL;
5929 for (;;) {
5930 len = getline(&line, &size, f);
5931 if (len == -1) {
5932 const struct got_error *getline_err;
5933 if (feof(f))
5934 break;
5935 getline_err = got_error_from_errno("getline");
5936 err = got_ferror(f, getline_err->code);
5937 break;
5939 lineno++;
5940 p = line;
5941 while (isspace((unsigned char)p[0]))
5942 p++;
5943 if (p[0] == '#' || p[0] == '\0') {
5944 free(line);
5945 line = NULL;
5946 continue;
5948 cmd = NULL;
5949 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5950 cmd = &got_histedit_cmds[i];
5951 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5952 isspace((unsigned char)p[strlen(cmd->name)])) {
5953 p += strlen(cmd->name);
5954 break;
5956 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5957 p++;
5958 break;
5961 if (i == nitems(got_histedit_cmds)) {
5962 err = histedit_syntax_error(lineno);
5963 break;
5965 while (isspace((unsigned char)p[0]))
5966 p++;
5967 if (cmd->code == GOT_HISTEDIT_MESG) {
5968 if (hle == NULL || hle->logmsg != NULL) {
5969 err = got_error(GOT_ERR_HISTEDIT_CMD);
5970 break;
5972 if (p[0] == '\0') {
5973 err = histedit_edit_logmsg(hle, repo);
5974 if (err)
5975 break;
5976 } else {
5977 hle->logmsg = strdup(p);
5978 if (hle->logmsg == NULL) {
5979 err = got_error_from_errno("strdup");
5980 break;
5983 free(line);
5984 line = NULL;
5985 continue;
5986 } else {
5987 end = p;
5988 while (end[0] && !isspace((unsigned char)end[0]))
5989 end++;
5990 *end = '\0';
5992 err = got_object_resolve_id_str(&commit_id, repo, p);
5993 if (err) {
5994 /* override error code */
5995 err = histedit_syntax_error(lineno);
5996 break;
5999 hle = malloc(sizeof(*hle));
6000 if (hle == NULL) {
6001 err = got_error_from_errno("malloc");
6002 break;
6004 hle->cmd = cmd;
6005 hle->commit_id = commit_id;
6006 hle->logmsg = NULL;
6007 commit_id = NULL;
6008 free(line);
6009 line = NULL;
6010 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6013 free(line);
6014 free(commit_id);
6015 return err;
6018 static const struct got_error *
6019 histedit_check_script(struct got_histedit_list *histedit_cmds,
6020 struct got_object_id_queue *commits, struct got_repository *repo)
6022 const struct got_error *err = NULL;
6023 struct got_object_qid *qid;
6024 struct got_histedit_list_entry *hle;
6025 static char msg[80];
6026 char *id_str;
6028 if (TAILQ_EMPTY(histedit_cmds))
6029 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6030 "histedit script contains no commands");
6031 if (SIMPLEQ_EMPTY(commits))
6032 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6034 SIMPLEQ_FOREACH(qid, commits, entry) {
6035 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6036 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6037 break;
6039 if (hle == NULL) {
6040 err = got_object_id_str(&id_str, qid->id);
6041 if (err)
6042 return err;
6043 snprintf(msg, sizeof(msg),
6044 "commit %s missing from histedit script", id_str);
6045 free(id_str);
6046 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6050 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6051 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6052 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6053 "last commit in histedit script cannot be folded");
6055 return NULL;
6058 static const struct got_error *
6059 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6060 const char *path, struct got_object_id_queue *commits,
6061 struct got_repository *repo)
6063 const struct got_error *err = NULL;
6064 char *editor;
6065 FILE *f = NULL;
6067 err = get_editor(&editor);
6068 if (err)
6069 return err;
6071 if (spawn_editor(editor, path) == -1) {
6072 err = got_error_from_errno("failed spawning editor");
6073 goto done;
6076 f = fopen(path, "r");
6077 if (f == NULL) {
6078 err = got_error_from_errno("fopen");
6079 goto done;
6081 err = histedit_parse_list(histedit_cmds, f, repo);
6082 if (err)
6083 goto done;
6085 err = histedit_check_script(histedit_cmds, commits, repo);
6086 done:
6087 if (f && fclose(f) != 0 && err == NULL)
6088 err = got_error_from_errno("fclose");
6089 free(editor);
6090 return err;
6093 static const struct got_error *
6094 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6095 struct got_object_id_queue *, const char *, struct got_repository *);
6097 static const struct got_error *
6098 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6099 struct got_object_id_queue *commits, struct got_repository *repo)
6101 const struct got_error *err;
6102 FILE *f = NULL;
6103 char *path = NULL;
6105 err = got_opentemp_named(&path, &f, "got-histedit");
6106 if (err)
6107 return err;
6109 err = write_cmd_list(f);
6110 if (err)
6111 goto done;
6113 err = histedit_write_commit_list(commits, f, repo);
6114 if (err)
6115 goto done;
6117 if (fclose(f) != 0) {
6118 err = got_error_from_errno("fclose");
6119 goto done;
6121 f = NULL;
6123 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6124 if (err) {
6125 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6126 err->code != GOT_ERR_HISTEDIT_CMD)
6127 goto done;
6128 err = histedit_edit_list_retry(histedit_cmds, err,
6129 commits, path, repo);
6131 done:
6132 if (f && fclose(f) != 0 && err == NULL)
6133 err = got_error_from_errno("fclose");
6134 if (path && unlink(path) != 0 && err == NULL)
6135 err = got_error_from_errno2("unlink", path);
6136 free(path);
6137 return err;
6140 static const struct got_error *
6141 histedit_save_list(struct got_histedit_list *histedit_cmds,
6142 struct got_worktree *worktree, struct got_repository *repo)
6144 const struct got_error *err = NULL;
6145 char *path = NULL;
6146 FILE *f = NULL;
6147 struct got_histedit_list_entry *hle;
6148 struct got_commit_object *commit = NULL;
6150 err = got_worktree_get_histedit_script_path(&path, worktree);
6151 if (err)
6152 return err;
6154 f = fopen(path, "w");
6155 if (f == NULL) {
6156 err = got_error_from_errno2("fopen", path);
6157 goto done;
6159 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6160 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6161 repo);
6162 if (err)
6163 break;
6165 if (hle->logmsg) {
6166 int n = fprintf(f, "%c %s\n",
6167 GOT_HISTEDIT_MESG, hle->logmsg);
6168 if (n < 0) {
6169 err = got_ferror(f, GOT_ERR_IO);
6170 break;
6174 done:
6175 if (f && fclose(f) != 0 && err == NULL)
6176 err = got_error_from_errno("fclose");
6177 free(path);
6178 if (commit)
6179 got_object_commit_close(commit);
6180 return err;
6183 void
6184 histedit_free_list(struct got_histedit_list *histedit_cmds)
6186 struct got_histedit_list_entry *hle;
6188 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6189 TAILQ_REMOVE(histedit_cmds, hle, entry);
6190 free(hle);
6194 static const struct got_error *
6195 histedit_load_list(struct got_histedit_list *histedit_cmds,
6196 const char *path, struct got_repository *repo)
6198 const struct got_error *err = NULL;
6199 FILE *f = NULL;
6201 f = fopen(path, "r");
6202 if (f == NULL) {
6203 err = got_error_from_errno2("fopen", path);
6204 goto done;
6207 err = histedit_parse_list(histedit_cmds, f, repo);
6208 done:
6209 if (f && fclose(f) != 0 && err == NULL)
6210 err = got_error_from_errno("fclose");
6211 return err;
6214 static const struct got_error *
6215 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6216 const struct got_error *edit_err, struct got_object_id_queue *commits,
6217 const char *path, struct got_repository *repo)
6219 const struct got_error *err = NULL, *prev_err = edit_err;
6220 int resp = ' ';
6222 while (resp != 'c' && resp != 'r' && resp != 'a') {
6223 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6224 "or (a)bort: ", getprogname(), prev_err->msg);
6225 resp = getchar();
6226 if (resp == '\n')
6227 resp = getchar();
6228 if (resp == 'c') {
6229 histedit_free_list(histedit_cmds);
6230 err = histedit_run_editor(histedit_cmds, path, commits,
6231 repo);
6232 if (err) {
6233 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6234 err->code != GOT_ERR_HISTEDIT_CMD)
6235 break;
6236 prev_err = err;
6237 resp = ' ';
6238 continue;
6240 break;
6241 } else if (resp == 'r') {
6242 histedit_free_list(histedit_cmds);
6243 err = histedit_edit_script(histedit_cmds,
6244 commits, repo);
6245 if (err) {
6246 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6247 err->code != GOT_ERR_HISTEDIT_CMD)
6248 break;
6249 prev_err = err;
6250 resp = ' ';
6251 continue;
6253 break;
6254 } else if (resp == 'a') {
6255 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6256 break;
6257 } else
6258 printf("invalid response '%c'\n", resp);
6261 return err;
6264 static const struct got_error *
6265 histedit_complete(struct got_worktree *worktree,
6266 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6267 struct got_reference *branch, struct got_repository *repo)
6269 printf("Switching work tree to %s\n",
6270 got_ref_get_symref_target(branch));
6271 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6272 branch, repo);
6275 static const struct got_error *
6276 show_histedit_progress(struct got_commit_object *commit,
6277 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6279 const struct got_error *err;
6280 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6282 err = got_object_id_str(&old_id_str, hle->commit_id);
6283 if (err)
6284 goto done;
6286 if (new_id) {
6287 err = got_object_id_str(&new_id_str, new_id);
6288 if (err)
6289 goto done;
6292 old_id_str[12] = '\0';
6293 if (new_id_str)
6294 new_id_str[12] = '\0';
6296 if (hle->logmsg) {
6297 logmsg = strdup(hle->logmsg);
6298 if (logmsg == NULL) {
6299 err = got_error_from_errno("strdup");
6300 goto done;
6302 trim_logmsg(logmsg, 42);
6303 } else {
6304 err = get_short_logmsg(&logmsg, 42, commit);
6305 if (err)
6306 goto done;
6309 switch (hle->cmd->code) {
6310 case GOT_HISTEDIT_PICK:
6311 case GOT_HISTEDIT_EDIT:
6312 printf("%s -> %s: %s\n", old_id_str,
6313 new_id_str ? new_id_str : "no-op change", logmsg);
6314 break;
6315 case GOT_HISTEDIT_DROP:
6316 case GOT_HISTEDIT_FOLD:
6317 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6318 logmsg);
6319 break;
6320 default:
6321 break;
6324 done:
6325 free(old_id_str);
6326 free(new_id_str);
6327 return err;
6330 static const struct got_error *
6331 histedit_commit(struct got_pathlist_head *merged_paths,
6332 struct got_worktree *worktree, struct got_fileindex *fileindex,
6333 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6334 struct got_repository *repo)
6336 const struct got_error *err;
6337 struct got_commit_object *commit;
6338 struct got_object_id *new_commit_id;
6340 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6341 && hle->logmsg == NULL) {
6342 err = histedit_edit_logmsg(hle, repo);
6343 if (err)
6344 return err;
6347 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6348 if (err)
6349 return err;
6351 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6352 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6353 hle->logmsg, repo);
6354 if (err) {
6355 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6356 goto done;
6357 err = show_histedit_progress(commit, hle, NULL);
6358 } else {
6359 err = show_histedit_progress(commit, hle, new_commit_id);
6360 free(new_commit_id);
6362 done:
6363 got_object_commit_close(commit);
6364 return err;
6367 static const struct got_error *
6368 histedit_skip_commit(struct got_histedit_list_entry *hle,
6369 struct got_worktree *worktree, struct got_repository *repo)
6371 const struct got_error *error;
6372 struct got_commit_object *commit;
6374 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6375 repo);
6376 if (error)
6377 return error;
6379 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6380 if (error)
6381 return error;
6383 error = show_histedit_progress(commit, hle, NULL);
6384 got_object_commit_close(commit);
6385 return error;
6388 static const struct got_error *
6389 cmd_histedit(int argc, char *argv[])
6391 const struct got_error *error = NULL;
6392 struct got_worktree *worktree = NULL;
6393 struct got_fileindex *fileindex = NULL;
6394 struct got_repository *repo = NULL;
6395 char *cwd = NULL;
6396 struct got_reference *branch = NULL;
6397 struct got_reference *tmp_branch = NULL;
6398 struct got_object_id *resume_commit_id = NULL;
6399 struct got_object_id *base_commit_id = NULL;
6400 struct got_object_id *head_commit_id = NULL;
6401 struct got_commit_object *commit = NULL;
6402 int ch, rebase_in_progress = 0, did_something;
6403 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6404 const char *edit_script_path = NULL;
6405 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6406 struct got_object_id_queue commits;
6407 struct got_pathlist_head merged_paths;
6408 const struct got_object_id_queue *parent_ids;
6409 struct got_object_qid *pid;
6410 struct got_histedit_list histedit_cmds;
6411 struct got_histedit_list_entry *hle;
6413 SIMPLEQ_INIT(&commits);
6414 TAILQ_INIT(&histedit_cmds);
6415 TAILQ_INIT(&merged_paths);
6417 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6418 switch (ch) {
6419 case 'a':
6420 abort_edit = 1;
6421 break;
6422 case 'c':
6423 continue_edit = 1;
6424 break;
6425 case 'F':
6426 edit_script_path = optarg;
6427 break;
6428 default:
6429 usage_histedit();
6430 /* NOTREACHED */
6434 argc -= optind;
6435 argv += optind;
6437 #ifndef PROFILE
6438 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6439 "unveil", NULL) == -1)
6440 err(1, "pledge");
6441 #endif
6442 if (abort_edit && continue_edit)
6443 usage_histedit();
6444 if (argc != 0)
6445 usage_histedit();
6448 * This command cannot apply unveil(2) in all cases because the
6449 * user may choose to run an editor to edit the histedit script
6450 * and to edit individual commit log messages.
6451 * unveil(2) traverses exec(2); if an editor is used we have to
6452 * apply unveil after edit script and log messages have been written.
6453 * XXX TODO: Make use of unveil(2) where possible.
6456 cwd = getcwd(NULL, 0);
6457 if (cwd == NULL) {
6458 error = got_error_from_errno("getcwd");
6459 goto done;
6461 error = got_worktree_open(&worktree, cwd);
6462 if (error)
6463 goto done;
6465 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6466 NULL);
6467 if (error != NULL)
6468 goto done;
6470 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6471 if (error)
6472 goto done;
6473 if (rebase_in_progress) {
6474 error = got_error(GOT_ERR_REBASING);
6475 goto done;
6478 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6479 if (error)
6480 goto done;
6482 if (edit_in_progress && abort_edit) {
6483 error = got_worktree_histedit_continue(&resume_commit_id,
6484 &tmp_branch, &branch, &base_commit_id, &fileindex,
6485 worktree, repo);
6486 if (error)
6487 goto done;
6488 printf("Switching work tree to %s\n",
6489 got_ref_get_symref_target(branch));
6490 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6491 branch, base_commit_id, update_progress, &did_something);
6492 if (error)
6493 goto done;
6494 printf("Histedit of %s aborted\n",
6495 got_ref_get_symref_target(branch));
6496 goto done; /* nothing else to do */
6497 } else if (abort_edit) {
6498 error = got_error(GOT_ERR_NOT_HISTEDIT);
6499 goto done;
6502 if (continue_edit) {
6503 char *path;
6505 if (!edit_in_progress) {
6506 error = got_error(GOT_ERR_NOT_HISTEDIT);
6507 goto done;
6510 error = got_worktree_get_histedit_script_path(&path, worktree);
6511 if (error)
6512 goto done;
6514 error = histedit_load_list(&histedit_cmds, path, repo);
6515 free(path);
6516 if (error)
6517 goto done;
6519 error = got_worktree_histedit_continue(&resume_commit_id,
6520 &tmp_branch, &branch, &base_commit_id, &fileindex,
6521 worktree, repo);
6522 if (error)
6523 goto done;
6525 error = got_ref_resolve(&head_commit_id, repo, branch);
6526 if (error)
6527 goto done;
6529 error = got_object_open_as_commit(&commit, repo,
6530 head_commit_id);
6531 if (error)
6532 goto done;
6533 parent_ids = got_object_commit_get_parent_ids(commit);
6534 pid = SIMPLEQ_FIRST(parent_ids);
6535 if (pid == NULL) {
6536 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6537 goto done;
6539 error = collect_commits(&commits, head_commit_id, pid->id,
6540 base_commit_id, got_worktree_get_path_prefix(worktree),
6541 GOT_ERR_HISTEDIT_PATH, repo);
6542 got_object_commit_close(commit);
6543 commit = NULL;
6544 if (error)
6545 goto done;
6546 } else {
6547 if (edit_in_progress) {
6548 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6549 goto done;
6552 error = got_ref_open(&branch, repo,
6553 got_worktree_get_head_ref_name(worktree), 0);
6554 if (error != NULL)
6555 goto done;
6557 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6558 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6559 "will not edit commit history of a branch outside "
6560 "the \"refs/heads/\" reference namespace");
6561 goto done;
6564 error = got_ref_resolve(&head_commit_id, repo, branch);
6565 got_ref_close(branch);
6566 branch = NULL;
6567 if (error)
6568 goto done;
6570 error = got_object_open_as_commit(&commit, repo,
6571 head_commit_id);
6572 if (error)
6573 goto done;
6574 parent_ids = got_object_commit_get_parent_ids(commit);
6575 pid = SIMPLEQ_FIRST(parent_ids);
6576 if (pid == NULL) {
6577 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6578 goto done;
6580 error = collect_commits(&commits, head_commit_id, pid->id,
6581 got_worktree_get_base_commit_id(worktree),
6582 got_worktree_get_path_prefix(worktree),
6583 GOT_ERR_HISTEDIT_PATH, repo);
6584 got_object_commit_close(commit);
6585 commit = NULL;
6586 if (error)
6587 goto done;
6589 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6590 &base_commit_id, &fileindex, worktree, repo);
6591 if (error)
6592 goto done;
6594 if (edit_script_path) {
6595 error = histedit_load_list(&histedit_cmds,
6596 edit_script_path, repo);
6597 if (error) {
6598 got_worktree_histedit_abort(worktree, fileindex,
6599 repo, branch, base_commit_id,
6600 update_progress, &did_something);
6601 goto done;
6603 } else {
6604 error = histedit_edit_script(&histedit_cmds, &commits,
6605 repo);
6606 if (error) {
6607 got_worktree_histedit_abort(worktree, fileindex,
6608 repo, branch, base_commit_id,
6609 update_progress, &did_something);
6610 goto done;
6615 error = histedit_save_list(&histedit_cmds, worktree,
6616 repo);
6617 if (error) {
6618 got_worktree_histedit_abort(worktree, fileindex,
6619 repo, branch, base_commit_id,
6620 update_progress, &did_something);
6621 goto done;
6626 error = histedit_check_script(&histedit_cmds, &commits, repo);
6627 if (error)
6628 goto done;
6630 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6631 if (resume_commit_id) {
6632 if (got_object_id_cmp(hle->commit_id,
6633 resume_commit_id) != 0)
6634 continue;
6636 resume_commit_id = NULL;
6637 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6638 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6639 error = histedit_skip_commit(hle, worktree,
6640 repo);
6641 } else {
6642 error = histedit_commit(NULL, worktree,
6643 fileindex, tmp_branch, hle, repo);
6645 if (error)
6646 goto done;
6647 continue;
6650 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6651 error = histedit_skip_commit(hle, worktree, repo);
6652 if (error)
6653 goto done;
6654 continue;
6657 error = got_object_open_as_commit(&commit, repo,
6658 hle->commit_id);
6659 if (error)
6660 goto done;
6661 parent_ids = got_object_commit_get_parent_ids(commit);
6662 pid = SIMPLEQ_FIRST(parent_ids);
6664 error = got_worktree_histedit_merge_files(&merged_paths,
6665 worktree, fileindex, pid->id, hle->commit_id, repo,
6666 rebase_progress, &rebase_status, check_cancelled, NULL);
6667 if (error)
6668 goto done;
6669 got_object_commit_close(commit);
6670 commit = NULL;
6672 if (rebase_status == GOT_STATUS_CONFLICT) {
6673 got_worktree_rebase_pathlist_free(&merged_paths);
6674 break;
6677 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6678 char *id_str;
6679 error = got_object_id_str(&id_str, hle->commit_id);
6680 if (error)
6681 goto done;
6682 printf("Stopping histedit for amending commit %s\n",
6683 id_str);
6684 free(id_str);
6685 got_worktree_rebase_pathlist_free(&merged_paths);
6686 error = got_worktree_histedit_postpone(worktree,
6687 fileindex);
6688 goto done;
6691 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6692 error = histedit_skip_commit(hle, worktree, repo);
6693 if (error)
6694 goto done;
6695 continue;
6698 error = histedit_commit(&merged_paths, worktree, fileindex,
6699 tmp_branch, hle, repo);
6700 got_worktree_rebase_pathlist_free(&merged_paths);
6701 if (error)
6702 goto done;
6705 if (rebase_status == GOT_STATUS_CONFLICT) {
6706 error = got_worktree_histedit_postpone(worktree, fileindex);
6707 if (error)
6708 goto done;
6709 error = got_error_msg(GOT_ERR_CONFLICTS,
6710 "conflicts must be resolved before rebasing can continue");
6711 } else
6712 error = histedit_complete(worktree, fileindex, tmp_branch,
6713 branch, repo);
6714 done:
6715 got_object_id_queue_free(&commits);
6716 histedit_free_list(&histedit_cmds);
6717 free(head_commit_id);
6718 free(base_commit_id);
6719 free(resume_commit_id);
6720 if (commit)
6721 got_object_commit_close(commit);
6722 if (branch)
6723 got_ref_close(branch);
6724 if (tmp_branch)
6725 got_ref_close(tmp_branch);
6726 if (worktree)
6727 got_worktree_close(worktree);
6728 if (repo)
6729 got_repo_close(repo);
6730 return error;
6733 __dead static void
6734 usage_integrate(void)
6736 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6737 exit(1);
6740 static const struct got_error *
6741 cmd_integrate(int argc, char *argv[])
6743 const struct got_error *error = NULL;
6744 struct got_repository *repo = NULL;
6745 struct got_worktree *worktree = NULL;
6746 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6747 const char *branch_arg = NULL;
6748 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6749 struct got_fileindex *fileindex = NULL;
6750 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6751 int ch, did_something = 0;
6753 while ((ch = getopt(argc, argv, "")) != -1) {
6754 switch (ch) {
6755 default:
6756 usage_integrate();
6757 /* NOTREACHED */
6761 argc -= optind;
6762 argv += optind;
6764 if (argc != 1)
6765 usage_integrate();
6766 branch_arg = argv[0];
6768 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6769 "unveil", NULL) == -1)
6770 err(1, "pledge");
6772 cwd = getcwd(NULL, 0);
6773 if (cwd == NULL) {
6774 error = got_error_from_errno("getcwd");
6775 goto done;
6778 error = got_worktree_open(&worktree, cwd);
6779 if (error)
6780 goto done;
6782 error = check_rebase_or_histedit_in_progress(worktree);
6783 if (error)
6784 goto done;
6786 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6787 NULL);
6788 if (error != NULL)
6789 goto done;
6791 error = apply_unveil(got_repo_get_path(repo), 0,
6792 got_worktree_get_root_path(worktree));
6793 if (error)
6794 goto done;
6796 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6797 error = got_error_from_errno("asprintf");
6798 goto done;
6801 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6802 &base_branch_ref, worktree, refname, repo);
6803 if (error)
6804 goto done;
6806 refname = strdup(got_ref_get_name(branch_ref));
6807 if (refname == NULL) {
6808 error = got_error_from_errno("strdup");
6809 got_worktree_integrate_abort(worktree, fileindex, repo,
6810 branch_ref, base_branch_ref);
6811 goto done;
6813 base_refname = strdup(got_ref_get_name(base_branch_ref));
6814 if (base_refname == NULL) {
6815 error = got_error_from_errno("strdup");
6816 got_worktree_integrate_abort(worktree, fileindex, repo,
6817 branch_ref, base_branch_ref);
6818 goto done;
6821 error = got_ref_resolve(&commit_id, repo, branch_ref);
6822 if (error)
6823 goto done;
6825 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6826 if (error)
6827 goto done;
6829 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6830 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6831 "specified branch has already been integrated");
6832 got_worktree_integrate_abort(worktree, fileindex, repo,
6833 branch_ref, base_branch_ref);
6834 goto done;
6837 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6838 if (error) {
6839 if (error->code == GOT_ERR_ANCESTRY)
6840 error = got_error(GOT_ERR_REBASE_REQUIRED);
6841 got_worktree_integrate_abort(worktree, fileindex, repo,
6842 branch_ref, base_branch_ref);
6843 goto done;
6846 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6847 branch_ref, base_branch_ref, update_progress, &did_something,
6848 check_cancelled, NULL);
6849 if (error)
6850 goto done;
6852 printf("Integrated %s into %s\n", refname, base_refname);
6853 done:
6854 if (repo)
6855 got_repo_close(repo);
6856 if (worktree)
6857 got_worktree_close(worktree);
6858 free(cwd);
6859 free(base_commit_id);
6860 free(commit_id);
6861 free(refname);
6862 free(base_refname);
6863 return error;
6866 __dead static void
6867 usage_stage(void)
6869 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6870 "[file-path ...]\n",
6871 getprogname());
6872 exit(1);
6875 static const struct got_error *
6876 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6877 const char *path, struct got_object_id *blob_id,
6878 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6879 int dirfd, const char *de_name)
6881 const struct got_error *err = NULL;
6882 char *id_str = NULL;
6884 if (staged_status != GOT_STATUS_ADD &&
6885 staged_status != GOT_STATUS_MODIFY &&
6886 staged_status != GOT_STATUS_DELETE)
6887 return NULL;
6889 if (staged_status == GOT_STATUS_ADD ||
6890 staged_status == GOT_STATUS_MODIFY)
6891 err = got_object_id_str(&id_str, staged_blob_id);
6892 else
6893 err = got_object_id_str(&id_str, blob_id);
6894 if (err)
6895 return err;
6897 printf("%s %c %s\n", id_str, staged_status, path);
6898 free(id_str);
6899 return NULL;
6902 static const struct got_error *
6903 cmd_stage(int argc, char *argv[])
6905 const struct got_error *error = NULL;
6906 struct got_repository *repo = NULL;
6907 struct got_worktree *worktree = NULL;
6908 char *cwd = NULL;
6909 struct got_pathlist_head paths;
6910 struct got_pathlist_entry *pe;
6911 int ch, list_stage = 0, pflag = 0;
6912 FILE *patch_script_file = NULL;
6913 const char *patch_script_path = NULL;
6914 struct choose_patch_arg cpa;
6916 TAILQ_INIT(&paths);
6918 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6919 switch (ch) {
6920 case 'l':
6921 list_stage = 1;
6922 break;
6923 case 'p':
6924 pflag = 1;
6925 break;
6926 case 'F':
6927 patch_script_path = optarg;
6928 break;
6929 default:
6930 usage_stage();
6931 /* NOTREACHED */
6935 argc -= optind;
6936 argv += optind;
6938 #ifndef PROFILE
6939 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6940 "unveil", NULL) == -1)
6941 err(1, "pledge");
6942 #endif
6943 if (list_stage && (pflag || patch_script_path))
6944 errx(1, "-l option cannot be used with other options");
6945 if (patch_script_path && !pflag)
6946 errx(1, "-F option can only be used together with -p option");
6948 cwd = getcwd(NULL, 0);
6949 if (cwd == NULL) {
6950 error = got_error_from_errno("getcwd");
6951 goto done;
6954 error = got_worktree_open(&worktree, cwd);
6955 if (error)
6956 goto done;
6958 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6959 NULL);
6960 if (error != NULL)
6961 goto done;
6963 if (patch_script_path) {
6964 patch_script_file = fopen(patch_script_path, "r");
6965 if (patch_script_file == NULL) {
6966 error = got_error_from_errno2("fopen",
6967 patch_script_path);
6968 goto done;
6971 error = apply_unveil(got_repo_get_path(repo), 0,
6972 got_worktree_get_root_path(worktree));
6973 if (error)
6974 goto done;
6976 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6977 if (error)
6978 goto done;
6980 if (list_stage)
6981 error = got_worktree_status(worktree, &paths, repo,
6982 print_stage, NULL, check_cancelled, NULL);
6983 else {
6984 cpa.patch_script_file = patch_script_file;
6985 cpa.action = "stage";
6986 error = got_worktree_stage(worktree, &paths,
6987 pflag ? NULL : print_status, NULL,
6988 pflag ? choose_patch : NULL, &cpa, repo);
6990 done:
6991 if (patch_script_file && fclose(patch_script_file) == EOF &&
6992 error == NULL)
6993 error = got_error_from_errno2("fclose", patch_script_path);
6994 if (repo)
6995 got_repo_close(repo);
6996 if (worktree)
6997 got_worktree_close(worktree);
6998 TAILQ_FOREACH(pe, &paths, entry)
6999 free((char *)pe->path);
7000 got_pathlist_free(&paths);
7001 free(cwd);
7002 return error;
7005 __dead static void
7006 usage_unstage(void)
7008 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7009 "[file-path ...]\n",
7010 getprogname());
7011 exit(1);
7015 static const struct got_error *
7016 cmd_unstage(int argc, char *argv[])
7018 const struct got_error *error = NULL;
7019 struct got_repository *repo = NULL;
7020 struct got_worktree *worktree = NULL;
7021 char *cwd = NULL;
7022 struct got_pathlist_head paths;
7023 struct got_pathlist_entry *pe;
7024 int ch, did_something = 0, pflag = 0;
7025 FILE *patch_script_file = NULL;
7026 const char *patch_script_path = NULL;
7027 struct choose_patch_arg cpa;
7029 TAILQ_INIT(&paths);
7031 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7032 switch (ch) {
7033 case 'p':
7034 pflag = 1;
7035 break;
7036 case 'F':
7037 patch_script_path = optarg;
7038 break;
7039 default:
7040 usage_unstage();
7041 /* NOTREACHED */
7045 argc -= optind;
7046 argv += optind;
7048 #ifndef PROFILE
7049 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7050 "unveil", NULL) == -1)
7051 err(1, "pledge");
7052 #endif
7053 if (patch_script_path && !pflag)
7054 errx(1, "-F option can only be used together with -p option");
7056 cwd = getcwd(NULL, 0);
7057 if (cwd == NULL) {
7058 error = got_error_from_errno("getcwd");
7059 goto done;
7062 error = got_worktree_open(&worktree, cwd);
7063 if (error)
7064 goto done;
7066 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7067 NULL);
7068 if (error != NULL)
7069 goto done;
7071 if (patch_script_path) {
7072 patch_script_file = fopen(patch_script_path, "r");
7073 if (patch_script_file == NULL) {
7074 error = got_error_from_errno2("fopen",
7075 patch_script_path);
7076 goto done;
7080 error = apply_unveil(got_repo_get_path(repo), 0,
7081 got_worktree_get_root_path(worktree));
7082 if (error)
7083 goto done;
7085 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7086 if (error)
7087 goto done;
7089 cpa.patch_script_file = patch_script_file;
7090 cpa.action = "unstage";
7091 error = got_worktree_unstage(worktree, &paths, update_progress,
7092 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7093 done:
7094 if (patch_script_file && fclose(patch_script_file) == EOF &&
7095 error == NULL)
7096 error = got_error_from_errno2("fclose", patch_script_path);
7097 if (repo)
7098 got_repo_close(repo);
7099 if (worktree)
7100 got_worktree_close(worktree);
7101 TAILQ_FOREACH(pe, &paths, entry)
7102 free((char *)pe->path);
7103 got_pathlist_free(&paths);
7104 free(cwd);
7105 return error;
7108 __dead static void
7109 usage_cat(void)
7111 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7112 "arg1 [arg2 ...]\n", getprogname());
7113 exit(1);
7116 static const struct got_error *
7117 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7119 const struct got_error *err;
7120 struct got_blob_object *blob;
7122 err = got_object_open_as_blob(&blob, repo, id, 8192);
7123 if (err)
7124 return err;
7126 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7127 got_object_blob_close(blob);
7128 return err;
7131 static const struct got_error *
7132 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7134 const struct got_error *err;
7135 struct got_tree_object *tree;
7136 int nentries, i;
7138 err = got_object_open_as_tree(&tree, repo, id);
7139 if (err)
7140 return err;
7142 nentries = got_object_tree_get_nentries(tree);
7143 for (i = 0; i < nentries; i++) {
7144 struct got_tree_entry *te;
7145 char *id_str;
7146 if (sigint_received || sigpipe_received)
7147 break;
7148 te = got_object_tree_get_entry(tree, i);
7149 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7150 if (err)
7151 break;
7152 fprintf(outfile, "%s %.7o %s\n", id_str,
7153 got_tree_entry_get_mode(te),
7154 got_tree_entry_get_name(te));
7155 free(id_str);
7158 got_object_tree_close(tree);
7159 return err;
7162 static const struct got_error *
7163 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7165 const struct got_error *err;
7166 struct got_commit_object *commit;
7167 const struct got_object_id_queue *parent_ids;
7168 struct got_object_qid *pid;
7169 char *id_str = NULL;
7170 const char *logmsg = NULL;
7172 err = got_object_open_as_commit(&commit, repo, id);
7173 if (err)
7174 return err;
7176 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7177 if (err)
7178 goto done;
7180 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7181 parent_ids = got_object_commit_get_parent_ids(commit);
7182 fprintf(outfile, "numparents %d\n",
7183 got_object_commit_get_nparents(commit));
7184 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7185 char *pid_str;
7186 err = got_object_id_str(&pid_str, pid->id);
7187 if (err)
7188 goto done;
7189 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7190 free(pid_str);
7192 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7193 got_object_commit_get_author(commit),
7194 got_object_commit_get_author_time(commit));
7196 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7197 got_object_commit_get_author(commit),
7198 got_object_commit_get_committer_time(commit));
7200 logmsg = got_object_commit_get_logmsg_raw(commit);
7201 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7202 fprintf(outfile, "%s", logmsg);
7203 done:
7204 free(id_str);
7205 got_object_commit_close(commit);
7206 return err;
7209 static const struct got_error *
7210 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7212 const struct got_error *err;
7213 struct got_tag_object *tag;
7214 char *id_str = NULL;
7215 const char *tagmsg = NULL;
7217 err = got_object_open_as_tag(&tag, repo, id);
7218 if (err)
7219 return err;
7221 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7222 if (err)
7223 goto done;
7225 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7227 switch (got_object_tag_get_object_type(tag)) {
7228 case GOT_OBJ_TYPE_BLOB:
7229 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7230 GOT_OBJ_LABEL_BLOB);
7231 break;
7232 case GOT_OBJ_TYPE_TREE:
7233 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7234 GOT_OBJ_LABEL_TREE);
7235 break;
7236 case GOT_OBJ_TYPE_COMMIT:
7237 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7238 GOT_OBJ_LABEL_COMMIT);
7239 break;
7240 case GOT_OBJ_TYPE_TAG:
7241 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7242 GOT_OBJ_LABEL_TAG);
7243 break;
7244 default:
7245 break;
7248 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7249 got_object_tag_get_name(tag));
7251 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7252 got_object_tag_get_tagger(tag),
7253 got_object_tag_get_tagger_time(tag));
7255 tagmsg = got_object_tag_get_message(tag);
7256 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7257 fprintf(outfile, "%s", tagmsg);
7258 done:
7259 free(id_str);
7260 got_object_tag_close(tag);
7261 return err;
7264 static const struct got_error *
7265 cmd_cat(int argc, char *argv[])
7267 const struct got_error *error;
7268 struct got_repository *repo = NULL;
7269 struct got_worktree *worktree = NULL;
7270 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7271 const char *commit_id_str = NULL;
7272 struct got_object_id *id = NULL, *commit_id = NULL;
7273 int ch, obj_type, i, force_path = 0;
7275 #ifndef PROFILE
7276 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7277 NULL) == -1)
7278 err(1, "pledge");
7279 #endif
7281 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7282 switch (ch) {
7283 case 'c':
7284 commit_id_str = optarg;
7285 break;
7286 case 'r':
7287 repo_path = realpath(optarg, NULL);
7288 if (repo_path == NULL)
7289 return got_error_from_errno2("realpath",
7290 optarg);
7291 got_path_strip_trailing_slashes(repo_path);
7292 break;
7293 case 'P':
7294 force_path = 1;
7295 break;
7296 default:
7297 usage_cat();
7298 /* NOTREACHED */
7302 argc -= optind;
7303 argv += optind;
7305 cwd = getcwd(NULL, 0);
7306 if (cwd == NULL) {
7307 error = got_error_from_errno("getcwd");
7308 goto done;
7310 error = got_worktree_open(&worktree, cwd);
7311 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7312 goto done;
7313 if (worktree) {
7314 if (repo_path == NULL) {
7315 repo_path = strdup(
7316 got_worktree_get_repo_path(worktree));
7317 if (repo_path == NULL) {
7318 error = got_error_from_errno("strdup");
7319 goto done;
7324 if (repo_path == NULL) {
7325 repo_path = getcwd(NULL, 0);
7326 if (repo_path == NULL)
7327 return got_error_from_errno("getcwd");
7330 error = got_repo_open(&repo, repo_path, NULL);
7331 free(repo_path);
7332 if (error != NULL)
7333 goto done;
7335 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7336 if (error)
7337 goto done;
7339 if (commit_id_str == NULL)
7340 commit_id_str = GOT_REF_HEAD;
7341 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7342 if (error)
7343 goto done;
7345 for (i = 0; i < argc; i++) {
7346 if (force_path) {
7347 error = got_object_id_by_path(&id, repo, commit_id,
7348 argv[i]);
7349 if (error)
7350 break;
7351 } else {
7352 error = match_object_id(&id, &label, argv[i],
7353 GOT_OBJ_TYPE_ANY, 0, repo);
7354 if (error) {
7355 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7356 error->code != GOT_ERR_NOT_REF)
7357 break;
7358 error = got_object_id_by_path(&id, repo,
7359 commit_id, argv[i]);
7360 if (error)
7361 break;
7365 error = got_object_get_type(&obj_type, repo, id);
7366 if (error)
7367 break;
7369 switch (obj_type) {
7370 case GOT_OBJ_TYPE_BLOB:
7371 error = cat_blob(id, repo, stdout);
7372 break;
7373 case GOT_OBJ_TYPE_TREE:
7374 error = cat_tree(id, repo, stdout);
7375 break;
7376 case GOT_OBJ_TYPE_COMMIT:
7377 error = cat_commit(id, repo, stdout);
7378 break;
7379 case GOT_OBJ_TYPE_TAG:
7380 error = cat_tag(id, repo, stdout);
7381 break;
7382 default:
7383 error = got_error(GOT_ERR_OBJ_TYPE);
7384 break;
7386 if (error)
7387 break;
7388 free(label);
7389 label = NULL;
7390 free(id);
7391 id = NULL;
7394 done:
7395 free(label);
7396 free(id);
7397 free(commit_id);
7398 if (worktree)
7399 got_worktree_close(worktree);
7400 if (repo) {
7401 const struct got_error *repo_error;
7402 repo_error = got_repo_close(repo);
7403 if (error == NULL)
7404 error = repo_error;
7406 return error;