Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <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 const struct got_error *
808 checkout_progress(void *arg, unsigned char status, const char *path)
810 char *worktree_path = arg;
812 /* Base commit bump happens silently. */
813 if (status == GOT_STATUS_BUMP_BASE)
814 return NULL;
816 while (path[0] == '/')
817 path++;
819 printf("%c %s/%s\n", status, worktree_path, path);
820 return NULL;
823 static const struct got_error *
824 check_cancelled(void *arg)
826 if (sigint_received || sigpipe_received)
827 return got_error(GOT_ERR_CANCELLED);
828 return NULL;
831 static const struct got_error *
832 check_linear_ancestry(struct got_object_id *commit_id,
833 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
834 struct got_repository *repo)
836 const struct got_error *err = NULL;
837 struct got_object_id *yca_id;
839 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
840 commit_id, base_commit_id, repo, check_cancelled, NULL);
841 if (err)
842 return err;
844 if (yca_id == NULL)
845 return got_error(GOT_ERR_ANCESTRY);
847 /*
848 * Require a straight line of history between the target commit
849 * and the work tree's base commit.
851 * Non-linear situations such as this require a rebase:
853 * (commit) D F (base_commit)
854 * \ /
855 * C E
856 * \ /
857 * B (yca)
858 * |
859 * A
861 * 'got update' only handles linear cases:
862 * Update forwards in time: A (base/yca) - B - C - D (commit)
863 * Update backwards in time: D (base) - C - B - A (commit/yca)
864 */
865 if (allow_forwards_in_time_only) {
866 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
867 return got_error(GOT_ERR_ANCESTRY);
868 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
869 got_object_id_cmp(base_commit_id, yca_id) != 0)
870 return got_error(GOT_ERR_ANCESTRY);
872 free(yca_id);
873 return NULL;
876 static const struct got_error *
877 check_same_branch(struct got_object_id *commit_id,
878 struct got_reference *head_ref, struct got_object_id *yca_id,
879 struct got_repository *repo)
881 const struct got_error *err = NULL;
882 struct got_commit_graph *graph = NULL;
883 struct got_object_id *head_commit_id = NULL;
884 int is_same_branch = 0;
886 err = got_ref_resolve(&head_commit_id, repo, head_ref);
887 if (err)
888 goto done;
890 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
891 is_same_branch = 1;
892 goto done;
894 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
895 is_same_branch = 1;
896 goto done;
899 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
900 if (err)
901 goto done;
903 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
904 check_cancelled, NULL);
905 if (err)
906 goto done;
908 for (;;) {
909 struct got_object_id *id;
910 err = got_commit_graph_iter_next(&id, graph);
911 if (err) {
912 if (err->code == GOT_ERR_ITER_COMPLETED) {
913 err = NULL;
914 break;
915 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
916 break;
917 err = got_commit_graph_fetch_commits(graph, 1,
918 repo, check_cancelled, NULL);
919 if (err)
920 break;
923 if (id) {
924 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
925 break;
926 if (got_object_id_cmp(id, commit_id) == 0) {
927 is_same_branch = 1;
928 break;
932 done:
933 if (graph)
934 got_commit_graph_close(graph);
935 free(head_commit_id);
936 if (!err && !is_same_branch)
937 err = got_error(GOT_ERR_ANCESTRY);
938 return err;
941 static const struct got_error *
942 resolve_commit_arg(struct got_object_id **commit_id,
943 const char *commit_id_arg, struct got_repository *repo)
945 const struct got_error *err;
946 struct got_reference *ref;
947 struct got_tag_object *tag;
949 err = got_repo_object_match_tag(&tag, commit_id_arg,
950 GOT_OBJ_TYPE_COMMIT, repo);
951 if (err == NULL) {
952 *commit_id = got_object_id_dup(
953 got_object_tag_get_object_id(tag));
954 if (*commit_id == NULL)
955 err = got_error_from_errno("got_object_id_dup");
956 got_object_tag_close(tag);
957 return err;
958 } else if (err->code != GOT_ERR_NO_OBJ)
959 return err;
961 err = got_ref_open(&ref, repo, commit_id_arg, 0);
962 if (err == NULL) {
963 err = got_ref_resolve(commit_id, repo, ref);
964 got_ref_close(ref);
965 } else {
966 if (err->code != GOT_ERR_NOT_REF)
967 return err;
968 err = got_repo_match_object_id_prefix(commit_id,
969 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
971 return err;
974 static const struct got_error *
975 cmd_checkout(int argc, char *argv[])
977 const struct got_error *error = NULL;
978 struct got_repository *repo = NULL;
979 struct got_reference *head_ref = NULL;
980 struct got_worktree *worktree = NULL;
981 char *repo_path = NULL;
982 char *worktree_path = NULL;
983 const char *path_prefix = "";
984 const char *branch_name = GOT_REF_HEAD;
985 char *commit_id_str = NULL;
986 int ch, same_path_prefix;
987 struct got_pathlist_head paths;
989 TAILQ_INIT(&paths);
991 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
992 switch (ch) {
993 case 'b':
994 branch_name = optarg;
995 break;
996 case 'c':
997 commit_id_str = strdup(optarg);
998 if (commit_id_str == NULL)
999 return got_error_from_errno("strdup");
1000 break;
1001 case 'p':
1002 path_prefix = optarg;
1003 break;
1004 default:
1005 usage_checkout();
1006 /* NOTREACHED */
1010 argc -= optind;
1011 argv += optind;
1013 #ifndef PROFILE
1014 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1015 "unveil", NULL) == -1)
1016 err(1, "pledge");
1017 #endif
1018 if (argc == 1) {
1019 char *cwd, *base, *dotgit;
1020 repo_path = realpath(argv[0], NULL);
1021 if (repo_path == NULL)
1022 return got_error_from_errno2("realpath", argv[0]);
1023 cwd = getcwd(NULL, 0);
1024 if (cwd == NULL) {
1025 error = got_error_from_errno("getcwd");
1026 goto done;
1028 if (path_prefix[0]) {
1029 base = basename(path_prefix);
1030 if (base == NULL) {
1031 error = got_error_from_errno2("basename",
1032 path_prefix);
1033 goto done;
1035 } else {
1036 base = basename(repo_path);
1037 if (base == NULL) {
1038 error = got_error_from_errno2("basename",
1039 repo_path);
1040 goto done;
1043 dotgit = strstr(base, ".git");
1044 if (dotgit)
1045 *dotgit = '\0';
1046 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1047 error = got_error_from_errno("asprintf");
1048 free(cwd);
1049 goto done;
1051 free(cwd);
1052 } else if (argc == 2) {
1053 repo_path = realpath(argv[0], NULL);
1054 if (repo_path == NULL) {
1055 error = got_error_from_errno2("realpath", argv[0]);
1056 goto done;
1058 worktree_path = realpath(argv[1], NULL);
1059 if (worktree_path == NULL) {
1060 if (errno != ENOENT) {
1061 error = got_error_from_errno2("realpath",
1062 argv[1]);
1063 goto done;
1065 worktree_path = strdup(argv[1]);
1066 if (worktree_path == NULL) {
1067 error = got_error_from_errno("strdup");
1068 goto done;
1071 } else
1072 usage_checkout();
1074 got_path_strip_trailing_slashes(repo_path);
1075 got_path_strip_trailing_slashes(worktree_path);
1077 error = got_repo_open(&repo, repo_path, NULL);
1078 if (error != NULL)
1079 goto done;
1081 /* Pre-create work tree path for unveil(2) */
1082 error = got_path_mkdir(worktree_path);
1083 if (error) {
1084 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1085 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1086 goto done;
1087 if (!got_path_dir_is_empty(worktree_path)) {
1088 error = got_error_path(worktree_path,
1089 GOT_ERR_DIR_NOT_EMPTY);
1090 goto done;
1094 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1095 if (error)
1096 goto done;
1098 error = got_ref_open(&head_ref, repo, branch_name, 0);
1099 if (error != NULL)
1100 goto done;
1102 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1103 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1104 goto done;
1106 error = got_worktree_open(&worktree, worktree_path);
1107 if (error != NULL)
1108 goto done;
1110 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1111 path_prefix);
1112 if (error != NULL)
1113 goto done;
1114 if (!same_path_prefix) {
1115 error = got_error(GOT_ERR_PATH_PREFIX);
1116 goto done;
1119 if (commit_id_str) {
1120 struct got_object_id *commit_id;
1121 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1122 if (error)
1123 goto done;
1124 error = check_linear_ancestry(commit_id,
1125 got_worktree_get_base_commit_id(worktree), 0, repo);
1126 if (error != NULL) {
1127 free(commit_id);
1128 goto done;
1130 error = check_same_branch(commit_id, head_ref, NULL, repo);
1131 if (error)
1132 goto done;
1133 error = got_worktree_set_base_commit_id(worktree, repo,
1134 commit_id);
1135 free(commit_id);
1136 if (error)
1137 goto done;
1140 error = got_pathlist_append(&paths, "", NULL);
1141 if (error)
1142 goto done;
1143 error = got_worktree_checkout_files(worktree, &paths, repo,
1144 checkout_progress, worktree_path, check_cancelled, NULL);
1145 if (error != NULL)
1146 goto done;
1148 printf("Now shut up and hack\n");
1150 done:
1151 got_pathlist_free(&paths);
1152 free(commit_id_str);
1153 free(repo_path);
1154 free(worktree_path);
1155 return error;
1158 __dead static void
1159 usage_update(void)
1161 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1162 getprogname());
1163 exit(1);
1166 static const struct got_error *
1167 update_progress(void *arg, unsigned char status, const char *path)
1169 int *did_something = arg;
1171 if (status == GOT_STATUS_EXISTS)
1172 return NULL;
1174 *did_something = 1;
1176 /* Base commit bump happens silently. */
1177 if (status == GOT_STATUS_BUMP_BASE)
1178 return NULL;
1180 while (path[0] == '/')
1181 path++;
1182 printf("%c %s\n", status, path);
1183 return NULL;
1186 static const struct got_error *
1187 switch_head_ref(struct got_reference *head_ref,
1188 struct got_object_id *commit_id, struct got_worktree *worktree,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 char *base_id_str;
1193 int ref_has_moved = 0;
1195 /* Trivial case: switching between two different references. */
1196 if (strcmp(got_ref_get_name(head_ref),
1197 got_worktree_get_head_ref_name(worktree)) != 0) {
1198 printf("Switching work tree from %s to %s\n",
1199 got_worktree_get_head_ref_name(worktree),
1200 got_ref_get_name(head_ref));
1201 return got_worktree_set_head_ref(worktree, head_ref);
1204 err = check_linear_ancestry(commit_id,
1205 got_worktree_get_base_commit_id(worktree), 0, repo);
1206 if (err) {
1207 if (err->code != GOT_ERR_ANCESTRY)
1208 return err;
1209 ref_has_moved = 1;
1211 if (!ref_has_moved)
1212 return NULL;
1214 /* Switching to a rebased branch with the same reference name. */
1215 err = got_object_id_str(&base_id_str,
1216 got_worktree_get_base_commit_id(worktree));
1217 if (err)
1218 return err;
1219 printf("Reference %s now points at a different branch\n",
1220 got_worktree_get_head_ref_name(worktree));
1221 printf("Switching work tree from %s to %s\n", base_id_str,
1222 got_worktree_get_head_ref_name(worktree));
1223 return NULL;
1226 static const struct got_error *
1227 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1229 const struct got_error *err;
1230 int in_progress;
1232 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1233 if (err)
1234 return err;
1235 if (in_progress)
1236 return got_error(GOT_ERR_REBASING);
1238 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1239 if (err)
1240 return err;
1241 if (in_progress)
1242 return got_error(GOT_ERR_HISTEDIT_BUSY);
1244 return NULL;
1247 static const struct got_error *
1248 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1249 char *argv[], struct got_worktree *worktree)
1251 const struct got_error *err = NULL;
1252 char *path;
1253 int i;
1255 if (argc == 0) {
1256 path = strdup("");
1257 if (path == NULL)
1258 return got_error_from_errno("strdup");
1259 return got_pathlist_append(paths, path, NULL);
1262 for (i = 0; i < argc; i++) {
1263 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1264 if (err)
1265 break;
1266 err = got_pathlist_append(paths, path, NULL);
1267 if (err) {
1268 free(path);
1269 break;
1273 return err;
1276 static const struct got_error *
1277 cmd_update(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 struct got_repository *repo = NULL;
1281 struct got_worktree *worktree = NULL;
1282 char *worktree_path = NULL;
1283 struct got_object_id *commit_id = NULL;
1284 char *commit_id_str = NULL;
1285 const char *branch_name = NULL;
1286 struct got_reference *head_ref = NULL;
1287 struct got_pathlist_head paths;
1288 struct got_pathlist_entry *pe;
1289 int ch, did_something = 0;
1291 TAILQ_INIT(&paths);
1293 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1294 switch (ch) {
1295 case 'b':
1296 branch_name = optarg;
1297 break;
1298 case 'c':
1299 commit_id_str = strdup(optarg);
1300 if (commit_id_str == NULL)
1301 return got_error_from_errno("strdup");
1302 break;
1303 default:
1304 usage_update();
1305 /* NOTREACHED */
1309 argc -= optind;
1310 argv += optind;
1312 #ifndef PROFILE
1313 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1314 "unveil", NULL) == -1)
1315 err(1, "pledge");
1316 #endif
1317 worktree_path = getcwd(NULL, 0);
1318 if (worktree_path == NULL) {
1319 error = got_error_from_errno("getcwd");
1320 goto done;
1322 error = got_worktree_open(&worktree, worktree_path);
1323 if (error)
1324 goto done;
1326 error = check_rebase_or_histedit_in_progress(worktree);
1327 if (error)
1328 goto done;
1330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1331 NULL);
1332 if (error != NULL)
1333 goto done;
1335 error = apply_unveil(got_repo_get_path(repo), 0,
1336 got_worktree_get_root_path(worktree));
1337 if (error)
1338 goto done;
1340 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1341 if (error)
1342 goto done;
1344 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1345 got_worktree_get_head_ref_name(worktree), 0);
1346 if (error != NULL)
1347 goto done;
1348 if (commit_id_str == NULL) {
1349 error = got_ref_resolve(&commit_id, repo, head_ref);
1350 if (error != NULL)
1351 goto done;
1352 error = got_object_id_str(&commit_id_str, commit_id);
1353 if (error != NULL)
1354 goto done;
1355 } else {
1356 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1357 free(commit_id_str);
1358 commit_id_str = NULL;
1359 if (error)
1360 goto done;
1361 error = got_object_id_str(&commit_id_str, commit_id);
1362 if (error)
1363 goto done;
1366 if (branch_name) {
1367 struct got_object_id *head_commit_id;
1368 TAILQ_FOREACH(pe, &paths, entry) {
1369 if (pe->path_len == 0)
1370 continue;
1371 error = got_error_msg(GOT_ERR_BAD_PATH,
1372 "switching between branches requires that "
1373 "the entire work tree gets updated");
1374 goto done;
1376 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1377 if (error)
1378 goto done;
1379 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1380 repo);
1381 free(head_commit_id);
1382 if (error != NULL)
1383 goto done;
1384 error = check_same_branch(commit_id, head_ref, NULL, repo);
1385 if (error)
1386 goto done;
1387 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1388 if (error)
1389 goto done;
1390 } else {
1391 error = check_linear_ancestry(commit_id,
1392 got_worktree_get_base_commit_id(worktree), 0, repo);
1393 if (error != NULL) {
1394 if (error->code == GOT_ERR_ANCESTRY)
1395 error = got_error(GOT_ERR_BRANCH_MOVED);
1396 goto done;
1398 error = check_same_branch(commit_id, head_ref, NULL, repo);
1399 if (error)
1400 goto done;
1403 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1404 commit_id) != 0) {
1405 error = got_worktree_set_base_commit_id(worktree, repo,
1406 commit_id);
1407 if (error)
1408 goto done;
1411 error = got_worktree_checkout_files(worktree, &paths, repo,
1412 update_progress, &did_something, check_cancelled, NULL);
1413 if (error != NULL)
1414 goto done;
1416 if (did_something)
1417 printf("Updated to commit %s\n", commit_id_str);
1418 else
1419 printf("Already up-to-date\n");
1420 done:
1421 free(worktree_path);
1422 TAILQ_FOREACH(pe, &paths, entry)
1423 free((char *)pe->path);
1424 got_pathlist_free(&paths);
1425 free(commit_id);
1426 free(commit_id_str);
1427 return error;
1430 static const struct got_error *
1431 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1432 const char *path, int diff_context, int ignore_whitespace,
1433 struct got_repository *repo)
1435 const struct got_error *err = NULL;
1436 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1438 if (blob_id1) {
1439 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1440 if (err)
1441 goto done;
1444 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1445 if (err)
1446 goto done;
1448 while (path[0] == '/')
1449 path++;
1450 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1451 ignore_whitespace, stdout);
1452 done:
1453 if (blob1)
1454 got_object_blob_close(blob1);
1455 got_object_blob_close(blob2);
1456 return err;
1459 static const struct got_error *
1460 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1461 const char *path, int diff_context, int ignore_whitespace,
1462 struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1466 struct got_diff_blob_output_unidiff_arg arg;
1468 if (tree_id1) {
1469 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1470 if (err)
1471 goto done;
1474 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1475 if (err)
1476 goto done;
1478 arg.diff_context = diff_context;
1479 arg.ignore_whitespace = ignore_whitespace;
1480 arg.outfile = stdout;
1481 while (path[0] == '/')
1482 path++;
1483 err = got_diff_tree(tree1, tree2, path, path, repo,
1484 got_diff_blob_output_unidiff, &arg, 1);
1485 done:
1486 if (tree1)
1487 got_object_tree_close(tree1);
1488 if (tree2)
1489 got_object_tree_close(tree2);
1490 return err;
1493 static const struct got_error *
1494 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1495 const char *path, int diff_context, struct got_repository *repo)
1497 const struct got_error *err = NULL;
1498 struct got_commit_object *pcommit = NULL;
1499 char *id_str1 = NULL, *id_str2 = NULL;
1500 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1501 struct got_object_qid *qid;
1503 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1504 if (qid != NULL) {
1505 err = got_object_open_as_commit(&pcommit, repo,
1506 qid->id);
1507 if (err)
1508 return err;
1511 if (path && path[0] != '\0') {
1512 int obj_type;
1513 err = got_object_id_by_path(&obj_id2, repo, id, path);
1514 if (err)
1515 goto done;
1516 err = got_object_id_str(&id_str2, obj_id2);
1517 if (err) {
1518 free(obj_id2);
1519 goto done;
1521 if (pcommit) {
1522 err = got_object_id_by_path(&obj_id1, repo,
1523 qid->id, path);
1524 if (err) {
1525 free(obj_id2);
1526 goto done;
1528 err = got_object_id_str(&id_str1, obj_id1);
1529 if (err) {
1530 free(obj_id2);
1531 goto done;
1534 err = got_object_get_type(&obj_type, repo, obj_id2);
1535 if (err) {
1536 free(obj_id2);
1537 goto done;
1539 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1540 switch (obj_type) {
1541 case GOT_OBJ_TYPE_BLOB:
1542 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1543 0, repo);
1544 break;
1545 case GOT_OBJ_TYPE_TREE:
1546 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1547 0, repo);
1548 break;
1549 default:
1550 err = got_error(GOT_ERR_OBJ_TYPE);
1551 break;
1553 free(obj_id1);
1554 free(obj_id2);
1555 } else {
1556 obj_id2 = got_object_commit_get_tree_id(commit);
1557 err = got_object_id_str(&id_str2, obj_id2);
1558 if (err)
1559 goto done;
1560 obj_id1 = got_object_commit_get_tree_id(pcommit);
1561 err = got_object_id_str(&id_str1, obj_id1);
1562 if (err)
1563 goto done;
1564 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1565 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1568 done:
1569 free(id_str1);
1570 free(id_str2);
1571 if (pcommit)
1572 got_object_commit_close(pcommit);
1573 return err;
1576 static char *
1577 get_datestr(time_t *time, char *datebuf)
1579 struct tm mytm, *tm;
1580 char *p, *s;
1582 tm = gmtime_r(time, &mytm);
1583 if (tm == NULL)
1584 return NULL;
1585 s = asctime_r(tm, datebuf);
1586 if (s == NULL)
1587 return NULL;
1588 p = strchr(s, '\n');
1589 if (p)
1590 *p = '\0';
1591 return s;
1594 static const struct got_error *
1595 match_logmsg(int *have_match, struct got_object_id *id,
1596 struct got_commit_object *commit, regex_t *regex)
1598 const struct got_error *err = NULL;
1599 regmatch_t regmatch;
1600 char *id_str = NULL, *logmsg = NULL;
1602 *have_match = 0;
1604 err = got_object_id_str(&id_str, id);
1605 if (err)
1606 return err;
1608 err = got_object_commit_get_logmsg(&logmsg, commit);
1609 if (err)
1610 goto done;
1612 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1613 *have_match = 1;
1614 done:
1615 free(id_str);
1616 free(logmsg);
1617 return err;
1620 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1622 static const struct got_error *
1623 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1624 struct got_repository *repo, const char *path, int show_patch,
1625 int diff_context, struct got_reflist_head *refs)
1627 const struct got_error *err = NULL;
1628 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1629 char datebuf[26];
1630 time_t committer_time;
1631 const char *author, *committer;
1632 char *refs_str = NULL;
1633 struct got_reflist_entry *re;
1635 SIMPLEQ_FOREACH(re, refs, entry) {
1636 char *s;
1637 const char *name;
1638 struct got_tag_object *tag = NULL;
1639 int cmp;
1641 name = got_ref_get_name(re->ref);
1642 if (strcmp(name, GOT_REF_HEAD) == 0)
1643 continue;
1644 if (strncmp(name, "refs/", 5) == 0)
1645 name += 5;
1646 if (strncmp(name, "got/", 4) == 0)
1647 continue;
1648 if (strncmp(name, "heads/", 6) == 0)
1649 name += 6;
1650 if (strncmp(name, "remotes/", 8) == 0)
1651 name += 8;
1652 if (strncmp(name, "tags/", 5) == 0) {
1653 err = got_object_open_as_tag(&tag, repo, re->id);
1654 if (err) {
1655 if (err->code != GOT_ERR_OBJ_TYPE)
1656 return err;
1657 /* Ref points at something other than a tag. */
1658 err = NULL;
1659 tag = NULL;
1662 cmp = got_object_id_cmp(tag ?
1663 got_object_tag_get_object_id(tag) : re->id, id);
1664 if (tag)
1665 got_object_tag_close(tag);
1666 if (cmp != 0)
1667 continue;
1668 s = refs_str;
1669 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1670 name) == -1) {
1671 err = got_error_from_errno("asprintf");
1672 free(s);
1673 return err;
1675 free(s);
1677 err = got_object_id_str(&id_str, id);
1678 if (err)
1679 return err;
1681 printf(GOT_COMMIT_SEP_STR);
1682 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1683 refs_str ? refs_str : "", refs_str ? ")" : "");
1684 free(id_str);
1685 id_str = NULL;
1686 free(refs_str);
1687 refs_str = NULL;
1688 printf("from: %s\n", got_object_commit_get_author(commit));
1689 committer_time = got_object_commit_get_committer_time(commit);
1690 datestr = get_datestr(&committer_time, datebuf);
1691 if (datestr)
1692 printf("date: %s UTC\n", datestr);
1693 author = got_object_commit_get_author(commit);
1694 committer = got_object_commit_get_committer(commit);
1695 if (strcmp(author, committer) != 0)
1696 printf("via: %s\n", committer);
1697 if (got_object_commit_get_nparents(commit) > 1) {
1698 const struct got_object_id_queue *parent_ids;
1699 struct got_object_qid *qid;
1700 int n = 1;
1701 parent_ids = got_object_commit_get_parent_ids(commit);
1702 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1703 err = got_object_id_str(&id_str, qid->id);
1704 if (err)
1705 return err;
1706 printf("parent %d: %s\n", n++, id_str);
1707 free(id_str);
1711 err = got_object_commit_get_logmsg(&logmsg0, commit);
1712 if (err)
1713 return err;
1715 logmsg = logmsg0;
1716 do {
1717 line = strsep(&logmsg, "\n");
1718 if (line)
1719 printf(" %s\n", line);
1720 } while (line);
1721 free(logmsg0);
1723 if (show_patch) {
1724 err = print_patch(commit, id, path, diff_context, repo);
1725 if (err == 0)
1726 printf("\n");
1729 if (fflush(stdout) != 0 && err == NULL)
1730 err = got_error_from_errno("fflush");
1731 return err;
1734 static const struct got_error *
1735 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1736 const char *path, int show_patch, const char *search_pattern,
1737 int diff_context, int limit, int first_parent_traversal,
1738 struct got_reflist_head *refs)
1740 const struct got_error *err;
1741 struct got_commit_graph *graph;
1742 regex_t regex;
1743 int have_match;
1745 if (search_pattern &&
1746 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1747 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1749 err = got_commit_graph_open(&graph, root_id, path,
1750 first_parent_traversal, repo);
1751 if (err)
1752 return err;
1753 err = got_commit_graph_iter_start(graph, root_id, repo,
1754 check_cancelled, NULL);
1755 if (err)
1756 goto done;
1757 for (;;) {
1758 struct got_commit_object *commit;
1759 struct got_object_id *id;
1761 if (sigint_received || sigpipe_received)
1762 break;
1764 err = got_commit_graph_iter_next(&id, graph);
1765 if (err) {
1766 if (err->code == GOT_ERR_ITER_COMPLETED) {
1767 err = NULL;
1768 break;
1770 if (err->code != GOT_ERR_ITER_NEED_MORE)
1771 break;
1772 err = got_commit_graph_fetch_commits(graph, 1, repo,
1773 check_cancelled, NULL);
1774 if (err)
1775 break;
1776 else
1777 continue;
1779 if (id == NULL)
1780 break;
1782 err = got_object_open_as_commit(&commit, repo, id);
1783 if (err)
1784 break;
1786 if (search_pattern) {
1787 err = match_logmsg(&have_match, id, commit, &regex);
1788 if (err) {
1789 got_object_commit_close(commit);
1790 break;
1792 if (have_match == 0) {
1793 got_object_commit_close(commit);
1794 continue;
1798 err = print_commit(commit, id, repo, path, show_patch,
1799 diff_context, refs);
1800 got_object_commit_close(commit);
1801 if (err || (limit && --limit == 0))
1802 break;
1804 done:
1805 if (search_pattern)
1806 regfree(&regex);
1807 got_commit_graph_close(graph);
1808 return err;
1811 __dead static void
1812 usage_log(void)
1814 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1815 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1816 exit(1);
1819 static int
1820 get_default_log_limit(void)
1822 const char *got_default_log_limit;
1823 long long n;
1824 const char *errstr;
1826 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1827 if (got_default_log_limit == NULL)
1828 return 0;
1829 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1830 if (errstr != NULL)
1831 return 0;
1832 return n;
1835 static const struct got_error *
1836 cmd_log(int argc, char *argv[])
1838 const struct got_error *error;
1839 struct got_repository *repo = NULL;
1840 struct got_worktree *worktree = NULL;
1841 struct got_commit_object *commit = NULL;
1842 struct got_object_id *id = NULL;
1843 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1844 const char *start_commit = NULL, *search_pattern = NULL;
1845 int diff_context = -1, ch;
1846 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1847 const char *errstr;
1848 struct got_reflist_head refs;
1850 SIMPLEQ_INIT(&refs);
1852 #ifndef PROFILE
1853 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1854 NULL)
1855 == -1)
1856 err(1, "pledge");
1857 #endif
1859 limit = get_default_log_limit();
1861 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1862 switch (ch) {
1863 case 'p':
1864 show_patch = 1;
1865 break;
1866 case 'c':
1867 start_commit = optarg;
1868 break;
1869 case 'C':
1870 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1871 &errstr);
1872 if (errstr != NULL)
1873 err(1, "-C option %s", errstr);
1874 break;
1875 case 'l':
1876 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1877 if (errstr != NULL)
1878 err(1, "-l option %s", errstr);
1879 break;
1880 case 'f':
1881 first_parent_traversal = 1;
1882 break;
1883 case 'r':
1884 repo_path = realpath(optarg, NULL);
1885 if (repo_path == NULL)
1886 return got_error_from_errno2("realpath",
1887 optarg);
1888 got_path_strip_trailing_slashes(repo_path);
1889 break;
1890 case 's':
1891 search_pattern = optarg;
1892 break;
1893 default:
1894 usage_log();
1895 /* NOTREACHED */
1899 argc -= optind;
1900 argv += optind;
1902 if (diff_context == -1)
1903 diff_context = 3;
1904 else if (!show_patch)
1905 errx(1, "-C reguires -p");
1907 cwd = getcwd(NULL, 0);
1908 if (cwd == NULL) {
1909 error = got_error_from_errno("getcwd");
1910 goto done;
1913 error = got_worktree_open(&worktree, cwd);
1914 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1915 goto done;
1916 error = NULL;
1918 if (argc == 0) {
1919 path = strdup("");
1920 if (path == NULL) {
1921 error = got_error_from_errno("strdup");
1922 goto done;
1924 } else if (argc == 1) {
1925 if (worktree) {
1926 error = got_worktree_resolve_path(&path, worktree,
1927 argv[0]);
1928 if (error)
1929 goto done;
1930 } else {
1931 path = strdup(argv[0]);
1932 if (path == NULL) {
1933 error = got_error_from_errno("strdup");
1934 goto done;
1937 } else
1938 usage_log();
1940 if (repo_path == NULL) {
1941 repo_path = worktree ?
1942 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1944 if (repo_path == NULL) {
1945 error = got_error_from_errno("strdup");
1946 goto done;
1949 error = got_repo_open(&repo, repo_path, NULL);
1950 if (error != NULL)
1951 goto done;
1953 error = apply_unveil(got_repo_get_path(repo), 1,
1954 worktree ? got_worktree_get_root_path(worktree) : NULL);
1955 if (error)
1956 goto done;
1958 if (start_commit == NULL) {
1959 struct got_reference *head_ref;
1960 error = got_ref_open(&head_ref, repo,
1961 worktree ? got_worktree_get_head_ref_name(worktree)
1962 : GOT_REF_HEAD, 0);
1963 if (error != NULL)
1964 return error;
1965 error = got_ref_resolve(&id, repo, head_ref);
1966 got_ref_close(head_ref);
1967 if (error != NULL)
1968 return error;
1969 error = got_object_open_as_commit(&commit, repo, id);
1970 } else {
1971 struct got_reference *ref;
1972 error = got_ref_open(&ref, repo, start_commit, 0);
1973 if (error == NULL) {
1974 int obj_type;
1975 error = got_ref_resolve(&id, repo, ref);
1976 got_ref_close(ref);
1977 if (error != NULL)
1978 goto done;
1979 error = got_object_get_type(&obj_type, repo, id);
1980 if (error != NULL)
1981 goto done;
1982 if (obj_type == GOT_OBJ_TYPE_TAG) {
1983 struct got_tag_object *tag;
1984 error = got_object_open_as_tag(&tag, repo, id);
1985 if (error != NULL)
1986 goto done;
1987 if (got_object_tag_get_object_type(tag) !=
1988 GOT_OBJ_TYPE_COMMIT) {
1989 got_object_tag_close(tag);
1990 error = got_error(GOT_ERR_OBJ_TYPE);
1991 goto done;
1993 free(id);
1994 id = got_object_id_dup(
1995 got_object_tag_get_object_id(tag));
1996 if (id == NULL)
1997 error = got_error_from_errno(
1998 "got_object_id_dup");
1999 got_object_tag_close(tag);
2000 if (error)
2001 goto done;
2002 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2003 error = got_error(GOT_ERR_OBJ_TYPE);
2004 goto done;
2006 error = got_object_open_as_commit(&commit, repo, id);
2007 if (error != NULL)
2008 goto done;
2010 if (commit == NULL) {
2011 error = got_repo_match_object_id_prefix(&id,
2012 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2013 if (error != NULL)
2014 return error;
2017 if (error != NULL)
2018 goto done;
2020 if (worktree) {
2021 const char *prefix = got_worktree_get_path_prefix(worktree);
2022 char *p;
2023 if (asprintf(&p, "%s%s%s", prefix,
2024 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2025 error = got_error_from_errno("asprintf");
2026 goto done;
2028 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2029 free(p);
2030 } else
2031 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2032 if (error != NULL)
2033 goto done;
2034 if (in_repo_path) {
2035 free(path);
2036 path = in_repo_path;
2039 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2040 if (error)
2041 goto done;
2043 error = print_commits(id, repo, path, show_patch, search_pattern,
2044 diff_context, limit, first_parent_traversal, &refs);
2045 done:
2046 free(path);
2047 free(repo_path);
2048 free(cwd);
2049 free(id);
2050 if (worktree)
2051 got_worktree_close(worktree);
2052 if (repo) {
2053 const struct got_error *repo_error;
2054 repo_error = got_repo_close(repo);
2055 if (error == NULL)
2056 error = repo_error;
2058 got_ref_list_free(&refs);
2059 return error;
2062 __dead static void
2063 usage_diff(void)
2065 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2066 "[-w] [object1 object2 | path]\n", getprogname());
2067 exit(1);
2070 struct print_diff_arg {
2071 struct got_repository *repo;
2072 struct got_worktree *worktree;
2073 int diff_context;
2074 const char *id_str;
2075 int header_shown;
2076 int diff_staged;
2077 int ignore_whitespace;
2080 static const struct got_error *
2081 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2082 const char *path, struct got_object_id *blob_id,
2083 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2084 int dirfd, const char *de_name)
2086 struct print_diff_arg *a = arg;
2087 const struct got_error *err = NULL;
2088 struct got_blob_object *blob1 = NULL;
2089 int fd = -1;
2090 FILE *f2 = NULL;
2091 char *abspath = NULL, *label1 = NULL;
2092 struct stat sb;
2094 if (a->diff_staged) {
2095 if (staged_status != GOT_STATUS_MODIFY &&
2096 staged_status != GOT_STATUS_ADD &&
2097 staged_status != GOT_STATUS_DELETE)
2098 return NULL;
2099 } else {
2100 if (staged_status == GOT_STATUS_DELETE)
2101 return NULL;
2102 if (status == GOT_STATUS_NONEXISTENT)
2103 return got_error_set_errno(ENOENT, path);
2104 if (status != GOT_STATUS_MODIFY &&
2105 status != GOT_STATUS_ADD &&
2106 status != GOT_STATUS_DELETE &&
2107 status != GOT_STATUS_CONFLICT)
2108 return NULL;
2111 if (!a->header_shown) {
2112 printf("diff %s %s%s\n", a->id_str,
2113 got_worktree_get_root_path(a->worktree),
2114 a->diff_staged ? " (staged changes)" : "");
2115 a->header_shown = 1;
2118 if (a->diff_staged) {
2119 const char *label1 = NULL, *label2 = NULL;
2120 switch (staged_status) {
2121 case GOT_STATUS_MODIFY:
2122 label1 = path;
2123 label2 = path;
2124 break;
2125 case GOT_STATUS_ADD:
2126 label2 = path;
2127 break;
2128 case GOT_STATUS_DELETE:
2129 label1 = path;
2130 break;
2131 default:
2132 return got_error(GOT_ERR_FILE_STATUS);
2134 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2135 label1, label2, a->diff_context, a->ignore_whitespace,
2136 a->repo, stdout);
2139 if (staged_status == GOT_STATUS_ADD ||
2140 staged_status == GOT_STATUS_MODIFY) {
2141 char *id_str;
2142 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2143 8192);
2144 if (err)
2145 goto done;
2146 err = got_object_id_str(&id_str, staged_blob_id);
2147 if (err)
2148 goto done;
2149 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2150 err = got_error_from_errno("asprintf");
2151 free(id_str);
2152 goto done;
2154 free(id_str);
2155 } else if (status != GOT_STATUS_ADD) {
2156 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2157 if (err)
2158 goto done;
2161 if (status != GOT_STATUS_DELETE) {
2162 if (asprintf(&abspath, "%s/%s",
2163 got_worktree_get_root_path(a->worktree), path) == -1) {
2164 err = got_error_from_errno("asprintf");
2165 goto done;
2168 if (dirfd != -1) {
2169 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2170 if (fd == -1) {
2171 err = got_error_from_errno2("openat", abspath);
2172 goto done;
2174 } else {
2175 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2176 if (fd == -1) {
2177 err = got_error_from_errno2("open", abspath);
2178 goto done;
2181 if (fstat(fd, &sb) == -1) {
2182 err = got_error_from_errno2("fstat", abspath);
2183 goto done;
2185 f2 = fdopen(fd, "r");
2186 if (f2 == NULL) {
2187 err = got_error_from_errno2("fdopen", abspath);
2188 goto done;
2190 fd = -1;
2191 } else
2192 sb.st_size = 0;
2194 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2195 a->diff_context, a->ignore_whitespace, stdout);
2196 done:
2197 if (blob1)
2198 got_object_blob_close(blob1);
2199 if (f2 && fclose(f2) == EOF && err == NULL)
2200 err = got_error_from_errno("fclose");
2201 if (fd != -1 && close(fd) == -1 && err == NULL)
2202 err = got_error_from_errno("close");
2203 free(abspath);
2204 return err;
2207 static const struct got_error *
2208 match_object_id(struct got_object_id **id, char **label,
2209 const char *id_str, int obj_type, int resolve_tags,
2210 struct got_repository *repo)
2212 const struct got_error *err;
2213 struct got_tag_object *tag;
2214 struct got_reference *ref = NULL;
2216 *id = NULL;
2217 *label = NULL;
2219 if (resolve_tags) {
2220 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2221 repo);
2222 if (err == NULL) {
2223 *id = got_object_id_dup(
2224 got_object_tag_get_object_id(tag));
2225 if (*id == NULL)
2226 err = got_error_from_errno("got_object_id_dup");
2227 else if (asprintf(label, "refs/tags/%s",
2228 got_object_tag_get_name(tag)) == -1) {
2229 err = got_error_from_errno("asprintf");
2230 free(*id);
2231 *id = NULL;
2233 got_object_tag_close(tag);
2234 return err;
2235 } else if (err->code != GOT_ERR_NO_OBJ)
2236 return err;
2239 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2240 if (err) {
2241 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2242 return err;
2243 err = got_ref_open(&ref, repo, id_str, 0);
2244 if (err != NULL)
2245 goto done;
2246 *label = strdup(got_ref_get_name(ref));
2247 if (*label == NULL) {
2248 err = got_error_from_errno("strdup");
2249 goto done;
2251 err = got_ref_resolve(id, repo, ref);
2252 } else {
2253 err = got_object_id_str(label, *id);
2254 if (*label == NULL) {
2255 err = got_error_from_errno("strdup");
2256 goto done;
2259 done:
2260 if (ref)
2261 got_ref_close(ref);
2262 return err;
2266 static const struct got_error *
2267 cmd_diff(int argc, char *argv[])
2269 const struct got_error *error;
2270 struct got_repository *repo = NULL;
2271 struct got_worktree *worktree = NULL;
2272 char *cwd = NULL, *repo_path = NULL;
2273 struct got_object_id *id1 = NULL, *id2 = NULL;
2274 const char *id_str1 = NULL, *id_str2 = NULL;
2275 char *label1 = NULL, *label2 = NULL;
2276 int type1, type2;
2277 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2278 const char *errstr;
2279 char *path = NULL;
2281 #ifndef PROFILE
2282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2283 NULL) == -1)
2284 err(1, "pledge");
2285 #endif
2287 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2288 switch (ch) {
2289 case 'C':
2290 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2291 &errstr);
2292 if (errstr != NULL)
2293 err(1, "-C option %s", errstr);
2294 break;
2295 case 'r':
2296 repo_path = realpath(optarg, NULL);
2297 if (repo_path == NULL)
2298 return got_error_from_errno2("realpath",
2299 optarg);
2300 got_path_strip_trailing_slashes(repo_path);
2301 break;
2302 case 's':
2303 diff_staged = 1;
2304 break;
2305 case 'w':
2306 ignore_whitespace = 1;
2307 break;
2308 default:
2309 usage_diff();
2310 /* NOTREACHED */
2314 argc -= optind;
2315 argv += optind;
2317 cwd = getcwd(NULL, 0);
2318 if (cwd == NULL) {
2319 error = got_error_from_errno("getcwd");
2320 goto done;
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2324 goto done;
2325 if (argc <= 1) {
2326 if (worktree == NULL) {
2327 error = got_error(GOT_ERR_NOT_WORKTREE);
2328 goto done;
2330 if (repo_path)
2331 errx(1,
2332 "-r option can't be used when diffing a work tree");
2333 repo_path = strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL) {
2335 error = got_error_from_errno("strdup");
2336 goto done;
2338 if (argc == 1) {
2339 error = got_worktree_resolve_path(&path, worktree,
2340 argv[0]);
2341 if (error)
2342 goto done;
2343 } else {
2344 path = strdup("");
2345 if (path == NULL) {
2346 error = got_error_from_errno("strdup");
2347 goto done;
2350 } else if (argc == 2) {
2351 if (diff_staged)
2352 errx(1, "-s option can't be used when diffing "
2353 "objects in repository");
2354 id_str1 = argv[0];
2355 id_str2 = argv[1];
2356 if (worktree && repo_path == NULL) {
2357 repo_path =
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2361 goto done;
2364 } else
2365 usage_diff();
2367 if (repo_path == NULL) {
2368 repo_path = getcwd(NULL, 0);
2369 if (repo_path == NULL)
2370 return got_error_from_errno("getcwd");
2373 error = got_repo_open(&repo, repo_path, NULL);
2374 free(repo_path);
2375 if (error != NULL)
2376 goto done;
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 worktree ? got_worktree_get_root_path(worktree) : NULL);
2380 if (error)
2381 goto done;
2383 if (argc <= 1) {
2384 struct print_diff_arg arg;
2385 struct got_pathlist_head paths;
2386 char *id_str;
2388 TAILQ_INIT(&paths);
2390 error = got_object_id_str(&id_str,
2391 got_worktree_get_base_commit_id(worktree));
2392 if (error)
2393 goto done;
2394 arg.repo = repo;
2395 arg.worktree = worktree;
2396 arg.diff_context = diff_context;
2397 arg.id_str = id_str;
2398 arg.header_shown = 0;
2399 arg.diff_staged = diff_staged;
2400 arg.ignore_whitespace = ignore_whitespace;
2402 error = got_pathlist_append(&paths, path, NULL);
2403 if (error)
2404 goto done;
2406 error = got_worktree_status(worktree, &paths, repo, print_diff,
2407 &arg, check_cancelled, NULL);
2408 free(id_str);
2409 got_pathlist_free(&paths);
2410 goto done;
2413 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2414 repo);
2415 if (error)
2416 goto done;
2418 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2419 repo);
2420 if (error)
2421 goto done;
2423 error = got_object_get_type(&type1, repo, id1);
2424 if (error)
2425 goto done;
2427 error = got_object_get_type(&type2, repo, id2);
2428 if (error)
2429 goto done;
2431 if (type1 != type2) {
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2433 goto done;
2436 switch (type1) {
2437 case GOT_OBJ_TYPE_BLOB:
2438 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2439 diff_context, ignore_whitespace, repo, stdout);
2440 break;
2441 case GOT_OBJ_TYPE_TREE:
2442 error = got_diff_objects_as_trees(id1, id2, "", "",
2443 diff_context, ignore_whitespace, repo, stdout);
2444 break;
2445 case GOT_OBJ_TYPE_COMMIT:
2446 printf("diff %s %s\n", label1, label2);
2447 error = got_diff_objects_as_commits(id1, id2, diff_context,
2448 ignore_whitespace, repo, stdout);
2449 break;
2450 default:
2451 error = got_error(GOT_ERR_OBJ_TYPE);
2454 done:
2455 free(label1);
2456 free(label2);
2457 free(id1);
2458 free(id2);
2459 free(path);
2460 if (worktree)
2461 got_worktree_close(worktree);
2462 if (repo) {
2463 const struct got_error *repo_error;
2464 repo_error = got_repo_close(repo);
2465 if (error == NULL)
2466 error = repo_error;
2468 return error;
2471 __dead static void
2472 usage_blame(void)
2474 fprintf(stderr,
2475 "usage: %s blame [-c commit] [-r repository-path] path\n",
2476 getprogname());
2477 exit(1);
2480 struct blame_line {
2481 int annotated;
2482 char *id_str;
2483 char *committer;
2484 char datebuf[11]; /* YYYY-MM-DD + NUL */
2487 struct blame_cb_args {
2488 struct blame_line *lines;
2489 int nlines;
2490 int nlines_prec;
2491 int lineno_cur;
2492 off_t *line_offsets;
2493 FILE *f;
2494 struct got_repository *repo;
2497 static const struct got_error *
2498 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2500 const struct got_error *err = NULL;
2501 struct blame_cb_args *a = arg;
2502 struct blame_line *bline;
2503 char *line = NULL;
2504 size_t linesize = 0;
2505 struct got_commit_object *commit = NULL;
2506 off_t offset;
2507 struct tm tm;
2508 time_t committer_time;
2510 if (nlines != a->nlines ||
2511 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2512 return got_error(GOT_ERR_RANGE);
2514 if (sigint_received)
2515 return got_error(GOT_ERR_ITER_COMPLETED);
2517 if (lineno == -1)
2518 return NULL; /* no change in this commit */
2520 /* Annotate this line. */
2521 bline = &a->lines[lineno - 1];
2522 if (bline->annotated)
2523 return NULL;
2524 err = got_object_id_str(&bline->id_str, id);
2525 if (err)
2526 return err;
2528 err = got_object_open_as_commit(&commit, a->repo, id);
2529 if (err)
2530 goto done;
2532 bline->committer = strdup(got_object_commit_get_committer(commit));
2533 if (bline->committer == NULL) {
2534 err = got_error_from_errno("strdup");
2535 goto done;
2538 committer_time = got_object_commit_get_committer_time(commit);
2539 if (localtime_r(&committer_time, &tm) == NULL)
2540 return got_error_from_errno("localtime_r");
2541 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2542 &tm) >= sizeof(bline->datebuf)) {
2543 err = got_error(GOT_ERR_NO_SPACE);
2544 goto done;
2546 bline->annotated = 1;
2548 /* Print lines annotated so far. */
2549 bline = &a->lines[a->lineno_cur - 1];
2550 if (!bline->annotated)
2551 goto done;
2553 offset = a->line_offsets[a->lineno_cur - 1];
2554 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2555 err = got_error_from_errno("fseeko");
2556 goto done;
2559 while (bline->annotated) {
2560 char *smallerthan, *at, *nl, *committer;
2561 size_t len;
2563 if (getline(&line, &linesize, a->f) == -1) {
2564 if (ferror(a->f))
2565 err = got_error_from_errno("getline");
2566 break;
2569 committer = bline->committer;
2570 smallerthan = strchr(committer, '<');
2571 if (smallerthan && smallerthan[1] != '\0')
2572 committer = smallerthan + 1;
2573 at = strchr(committer, '@');
2574 if (at)
2575 *at = '\0';
2576 len = strlen(committer);
2577 if (len >= 9)
2578 committer[8] = '\0';
2580 nl = strchr(line, '\n');
2581 if (nl)
2582 *nl = '\0';
2583 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2584 bline->id_str, bline->datebuf, committer, line);
2586 a->lineno_cur++;
2587 bline = &a->lines[a->lineno_cur - 1];
2589 done:
2590 if (commit)
2591 got_object_commit_close(commit);
2592 free(line);
2593 return err;
2596 static const struct got_error *
2597 cmd_blame(int argc, char *argv[])
2599 const struct got_error *error;
2600 struct got_repository *repo = NULL;
2601 struct got_worktree *worktree = NULL;
2602 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2603 struct got_object_id *obj_id = NULL;
2604 struct got_object_id *commit_id = NULL;
2605 struct got_blob_object *blob = NULL;
2606 char *commit_id_str = NULL;
2607 struct blame_cb_args bca;
2608 int ch, obj_type, i;
2609 size_t filesize;
2611 memset(&bca, 0, sizeof(bca));
2613 #ifndef PROFILE
2614 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2615 NULL) == -1)
2616 err(1, "pledge");
2617 #endif
2619 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2620 switch (ch) {
2621 case 'c':
2622 commit_id_str = optarg;
2623 break;
2624 case 'r':
2625 repo_path = realpath(optarg, NULL);
2626 if (repo_path == NULL)
2627 return got_error_from_errno2("realpath",
2628 optarg);
2629 got_path_strip_trailing_slashes(repo_path);
2630 break;
2631 default:
2632 usage_blame();
2633 /* NOTREACHED */
2637 argc -= optind;
2638 argv += optind;
2640 if (argc == 1)
2641 path = argv[0];
2642 else
2643 usage_blame();
2645 cwd = getcwd(NULL, 0);
2646 if (cwd == NULL) {
2647 error = got_error_from_errno("getcwd");
2648 goto done;
2650 if (repo_path == NULL) {
2651 error = got_worktree_open(&worktree, cwd);
2652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2653 goto done;
2654 else
2655 error = NULL;
2656 if (worktree) {
2657 repo_path =
2658 strdup(got_worktree_get_repo_path(worktree));
2659 if (repo_path == NULL)
2660 error = got_error_from_errno("strdup");
2661 if (error)
2662 goto done;
2663 } else {
2664 repo_path = strdup(cwd);
2665 if (repo_path == NULL) {
2666 error = got_error_from_errno("strdup");
2667 goto done;
2672 error = got_repo_open(&repo, repo_path, NULL);
2673 if (error != NULL)
2674 goto done;
2676 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2677 if (error)
2678 goto done;
2680 if (worktree) {
2681 const char *prefix = got_worktree_get_path_prefix(worktree);
2682 char *p, *worktree_subdir = cwd +
2683 strlen(got_worktree_get_root_path(worktree));
2684 if (asprintf(&p, "%s%s%s%s%s",
2685 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2686 worktree_subdir, worktree_subdir[0] ? "/" : "",
2687 path) == -1) {
2688 error = got_error_from_errno("asprintf");
2689 goto done;
2691 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2692 free(p);
2693 } else {
2694 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2696 if (error)
2697 goto done;
2699 if (commit_id_str == NULL) {
2700 struct got_reference *head_ref;
2701 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2702 if (error != NULL)
2703 goto done;
2704 error = got_ref_resolve(&commit_id, repo, head_ref);
2705 got_ref_close(head_ref);
2706 if (error != NULL)
2707 goto done;
2708 } else {
2709 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2710 if (error)
2711 goto done;
2714 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2715 if (error)
2716 goto done;
2717 if (obj_id == NULL) {
2718 error = got_error(GOT_ERR_NO_OBJ);
2719 goto done;
2722 error = got_object_get_type(&obj_type, repo, obj_id);
2723 if (error)
2724 goto done;
2726 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2727 error = got_error(GOT_ERR_OBJ_TYPE);
2728 goto done;
2731 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2732 if (error)
2733 goto done;
2734 bca.f = got_opentemp();
2735 if (bca.f == NULL) {
2736 error = got_error_from_errno("got_opentemp");
2737 goto done;
2739 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2740 &bca.line_offsets, bca.f, blob);
2741 if (error || bca.nlines == 0)
2742 goto done;
2744 /* Don't include \n at EOF in the blame line count. */
2745 if (bca.line_offsets[bca.nlines - 1] == filesize)
2746 bca.nlines--;
2748 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2749 if (bca.lines == NULL) {
2750 error = got_error_from_errno("calloc");
2751 goto done;
2753 bca.lineno_cur = 1;
2754 bca.nlines_prec = 0;
2755 i = bca.nlines;
2756 while (i > 0) {
2757 i /= 10;
2758 bca.nlines_prec++;
2760 bca.repo = repo;
2762 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2763 check_cancelled, NULL);
2764 if (error)
2765 goto done;
2766 done:
2767 free(in_repo_path);
2768 free(repo_path);
2769 free(cwd);
2770 free(commit_id);
2771 free(obj_id);
2772 if (blob)
2773 got_object_blob_close(blob);
2774 if (worktree)
2775 got_worktree_close(worktree);
2776 if (repo) {
2777 const struct got_error *repo_error;
2778 repo_error = got_repo_close(repo);
2779 if (error == NULL)
2780 error = repo_error;
2782 if (bca.lines) {
2783 for (i = 0; i < bca.nlines; i++) {
2784 struct blame_line *bline = &bca.lines[i];
2785 free(bline->id_str);
2786 free(bline->committer);
2788 free(bca.lines);
2790 free(bca.line_offsets);
2791 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2792 error = got_error_from_errno("fclose");
2793 return error;
2796 __dead static void
2797 usage_tree(void)
2799 fprintf(stderr,
2800 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2801 getprogname());
2802 exit(1);
2805 static void
2806 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2807 const char *root_path)
2809 int is_root_path = (strcmp(path, root_path) == 0);
2810 const char *modestr = "";
2811 mode_t mode = got_tree_entry_get_mode(te);
2813 path += strlen(root_path);
2814 while (path[0] == '/')
2815 path++;
2817 if (got_object_tree_entry_is_submodule(te))
2818 modestr = "$";
2819 else if (S_ISLNK(mode))
2820 modestr = "@";
2821 else if (S_ISDIR(mode))
2822 modestr = "/";
2823 else if (mode & S_IXUSR)
2824 modestr = "*";
2826 printf("%s%s%s%s%s\n", id ? id : "", path,
2827 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2830 static const struct got_error *
2831 print_tree(const char *path, struct got_object_id *commit_id,
2832 int show_ids, int recurse, const char *root_path,
2833 struct got_repository *repo)
2835 const struct got_error *err = NULL;
2836 struct got_object_id *tree_id = NULL;
2837 struct got_tree_object *tree = NULL;
2838 int nentries, i;
2840 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2841 if (err)
2842 goto done;
2844 err = got_object_open_as_tree(&tree, repo, tree_id);
2845 if (err)
2846 goto done;
2847 nentries = got_object_tree_get_nentries(tree);
2848 for (i = 0; i < nentries; i++) {
2849 struct got_tree_entry *te;
2850 char *id = NULL;
2852 if (sigint_received || sigpipe_received)
2853 break;
2855 te = got_object_tree_get_entry(tree, i);
2856 if (show_ids) {
2857 char *id_str;
2858 err = got_object_id_str(&id_str,
2859 got_tree_entry_get_id(te));
2860 if (err)
2861 goto done;
2862 if (asprintf(&id, "%s ", id_str) == -1) {
2863 err = got_error_from_errno("asprintf");
2864 free(id_str);
2865 goto done;
2867 free(id_str);
2869 print_entry(te, id, path, root_path);
2870 free(id);
2872 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2873 char *child_path;
2874 if (asprintf(&child_path, "%s%s%s", path,
2875 path[0] == '/' && path[1] == '\0' ? "" : "/",
2876 got_tree_entry_get_name(te)) == -1) {
2877 err = got_error_from_errno("asprintf");
2878 goto done;
2880 err = print_tree(child_path, commit_id, show_ids, 1,
2881 root_path, repo);
2882 free(child_path);
2883 if (err)
2884 goto done;
2887 done:
2888 if (tree)
2889 got_object_tree_close(tree);
2890 free(tree_id);
2891 return err;
2894 static const struct got_error *
2895 cmd_tree(int argc, char *argv[])
2897 const struct got_error *error;
2898 struct got_repository *repo = NULL;
2899 struct got_worktree *worktree = NULL;
2900 const char *path;
2901 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2902 struct got_object_id *commit_id = NULL;
2903 char *commit_id_str = NULL;
2904 int show_ids = 0, recurse = 0;
2905 int ch;
2907 #ifndef PROFILE
2908 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2909 NULL) == -1)
2910 err(1, "pledge");
2911 #endif
2913 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2914 switch (ch) {
2915 case 'c':
2916 commit_id_str = optarg;
2917 break;
2918 case 'r':
2919 repo_path = realpath(optarg, NULL);
2920 if (repo_path == NULL)
2921 return got_error_from_errno2("realpath",
2922 optarg);
2923 got_path_strip_trailing_slashes(repo_path);
2924 break;
2925 case 'i':
2926 show_ids = 1;
2927 break;
2928 case 'R':
2929 recurse = 1;
2930 break;
2931 default:
2932 usage_tree();
2933 /* NOTREACHED */
2937 argc -= optind;
2938 argv += optind;
2940 if (argc == 1)
2941 path = argv[0];
2942 else if (argc > 1)
2943 usage_tree();
2944 else
2945 path = NULL;
2947 cwd = getcwd(NULL, 0);
2948 if (cwd == NULL) {
2949 error = got_error_from_errno("getcwd");
2950 goto done;
2952 if (repo_path == NULL) {
2953 error = got_worktree_open(&worktree, cwd);
2954 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2955 goto done;
2956 else
2957 error = NULL;
2958 if (worktree) {
2959 repo_path =
2960 strdup(got_worktree_get_repo_path(worktree));
2961 if (repo_path == NULL)
2962 error = got_error_from_errno("strdup");
2963 if (error)
2964 goto done;
2965 } else {
2966 repo_path = strdup(cwd);
2967 if (repo_path == NULL) {
2968 error = got_error_from_errno("strdup");
2969 goto done;
2974 error = got_repo_open(&repo, repo_path, NULL);
2975 if (error != NULL)
2976 goto done;
2978 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2979 if (error)
2980 goto done;
2982 if (path == NULL) {
2983 if (worktree) {
2984 char *p, *worktree_subdir = cwd +
2985 strlen(got_worktree_get_root_path(worktree));
2986 if (asprintf(&p, "%s/%s",
2987 got_worktree_get_path_prefix(worktree),
2988 worktree_subdir) == -1) {
2989 error = got_error_from_errno("asprintf");
2990 goto done;
2992 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2993 free(p);
2994 if (error)
2995 goto done;
2996 } else
2997 path = "/";
2999 if (in_repo_path == NULL) {
3000 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3001 if (error != NULL)
3002 goto done;
3005 if (commit_id_str == NULL) {
3006 struct got_reference *head_ref;
3007 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3008 if (error != NULL)
3009 goto done;
3010 error = got_ref_resolve(&commit_id, repo, head_ref);
3011 got_ref_close(head_ref);
3012 if (error != NULL)
3013 goto done;
3014 } else {
3015 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3016 if (error)
3017 goto done;
3020 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3021 in_repo_path, repo);
3022 done:
3023 free(in_repo_path);
3024 free(repo_path);
3025 free(cwd);
3026 free(commit_id);
3027 if (worktree)
3028 got_worktree_close(worktree);
3029 if (repo) {
3030 const struct got_error *repo_error;
3031 repo_error = got_repo_close(repo);
3032 if (error == NULL)
3033 error = repo_error;
3035 return error;
3038 __dead static void
3039 usage_status(void)
3041 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3042 exit(1);
3045 static const struct got_error *
3046 print_status(void *arg, unsigned char status, unsigned char staged_status,
3047 const char *path, struct got_object_id *blob_id,
3048 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3049 int dirfd, const char *de_name)
3051 if (status == staged_status && (status == GOT_STATUS_DELETE))
3052 status = GOT_STATUS_NO_CHANGE;
3053 printf("%c%c %s\n", status, staged_status, path);
3054 return NULL;
3057 static const struct got_error *
3058 cmd_status(int argc, char *argv[])
3060 const struct got_error *error = NULL;
3061 struct got_repository *repo = NULL;
3062 struct got_worktree *worktree = NULL;
3063 char *cwd = NULL;
3064 struct got_pathlist_head paths;
3065 struct got_pathlist_entry *pe;
3066 int ch;
3068 TAILQ_INIT(&paths);
3070 while ((ch = getopt(argc, argv, "")) != -1) {
3071 switch (ch) {
3072 default:
3073 usage_status();
3074 /* NOTREACHED */
3078 argc -= optind;
3079 argv += optind;
3081 #ifndef PROFILE
3082 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3083 NULL) == -1)
3084 err(1, "pledge");
3085 #endif
3086 cwd = getcwd(NULL, 0);
3087 if (cwd == NULL) {
3088 error = got_error_from_errno("getcwd");
3089 goto done;
3092 error = got_worktree_open(&worktree, cwd);
3093 if (error != NULL)
3094 goto done;
3096 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3097 NULL);
3098 if (error != NULL)
3099 goto done;
3101 error = apply_unveil(got_repo_get_path(repo), 1,
3102 got_worktree_get_root_path(worktree));
3103 if (error)
3104 goto done;
3106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3107 if (error)
3108 goto done;
3110 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3111 check_cancelled, NULL);
3112 done:
3113 TAILQ_FOREACH(pe, &paths, entry)
3114 free((char *)pe->path);
3115 got_pathlist_free(&paths);
3116 free(cwd);
3117 return error;
3120 __dead static void
3121 usage_ref(void)
3123 fprintf(stderr,
3124 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3125 getprogname());
3126 exit(1);
3129 static const struct got_error *
3130 list_refs(struct got_repository *repo)
3132 static const struct got_error *err = NULL;
3133 struct got_reflist_head refs;
3134 struct got_reflist_entry *re;
3136 SIMPLEQ_INIT(&refs);
3137 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3138 if (err)
3139 return err;
3141 SIMPLEQ_FOREACH(re, &refs, entry) {
3142 char *refstr;
3143 refstr = got_ref_to_str(re->ref);
3144 if (refstr == NULL)
3145 return got_error_from_errno("got_ref_to_str");
3146 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3147 free(refstr);
3150 got_ref_list_free(&refs);
3151 return NULL;
3154 static const struct got_error *
3155 delete_ref(struct got_repository *repo, const char *refname)
3157 const struct got_error *err = NULL;
3158 struct got_reference *ref;
3160 err = got_ref_open(&ref, repo, refname, 0);
3161 if (err)
3162 return err;
3164 err = got_ref_delete(ref, repo);
3165 got_ref_close(ref);
3166 return err;
3169 static const struct got_error *
3170 add_ref(struct got_repository *repo, const char *refname, const char *target)
3172 const struct got_error *err = NULL;
3173 struct got_object_id *id;
3174 struct got_reference *ref = NULL;
3177 * Don't let the user create a reference name with a leading '-'.
3178 * While technically a valid reference name, this case is usually
3179 * an unintended typo.
3181 if (refname[0] == '-')
3182 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3184 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3185 repo);
3186 if (err) {
3187 struct got_reference *target_ref;
3189 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3190 return err;
3191 err = got_ref_open(&target_ref, repo, target, 0);
3192 if (err)
3193 return err;
3194 err = got_ref_resolve(&id, repo, target_ref);
3195 got_ref_close(target_ref);
3196 if (err)
3197 return err;
3200 err = got_ref_alloc(&ref, refname, id);
3201 if (err)
3202 goto done;
3204 err = got_ref_write(ref, repo);
3205 done:
3206 if (ref)
3207 got_ref_close(ref);
3208 free(id);
3209 return err;
3212 static const struct got_error *
3213 add_symref(struct got_repository *repo, const char *refname, const char *target)
3215 const struct got_error *err = NULL;
3216 struct got_reference *ref = NULL;
3217 struct got_reference *target_ref = NULL;
3220 * Don't let the user create a reference name with a leading '-'.
3221 * While technically a valid reference name, this case is usually
3222 * an unintended typo.
3224 if (refname[0] == '-')
3225 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3227 err = got_ref_open(&target_ref, repo, target, 0);
3228 if (err)
3229 return err;
3231 err = got_ref_alloc_symref(&ref, refname, target_ref);
3232 if (err)
3233 goto done;
3235 err = got_ref_write(ref, repo);
3236 done:
3237 if (target_ref)
3238 got_ref_close(target_ref);
3239 if (ref)
3240 got_ref_close(ref);
3241 return err;
3244 static const struct got_error *
3245 cmd_ref(int argc, char *argv[])
3247 const struct got_error *error = NULL;
3248 struct got_repository *repo = NULL;
3249 struct got_worktree *worktree = NULL;
3250 char *cwd = NULL, *repo_path = NULL;
3251 int ch, do_list = 0, create_symref = 0;
3252 const char *delref = NULL;
3254 /* TODO: Add -s option for adding symbolic references. */
3255 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3256 switch (ch) {
3257 case 'd':
3258 delref = optarg;
3259 break;
3260 case 'r':
3261 repo_path = realpath(optarg, NULL);
3262 if (repo_path == NULL)
3263 return got_error_from_errno2("realpath",
3264 optarg);
3265 got_path_strip_trailing_slashes(repo_path);
3266 break;
3267 case 'l':
3268 do_list = 1;
3269 break;
3270 case 's':
3271 create_symref = 1;
3272 break;
3273 default:
3274 usage_ref();
3275 /* NOTREACHED */
3279 if (do_list && delref)
3280 errx(1, "-l and -d options are mutually exclusive\n");
3282 argc -= optind;
3283 argv += optind;
3285 if (do_list || delref) {
3286 if (create_symref)
3287 errx(1, "-s option cannot be used together with the "
3288 "-l or -d options");
3289 if (argc > 0)
3290 usage_ref();
3291 } else if (argc != 2)
3292 usage_ref();
3294 #ifndef PROFILE
3295 if (do_list) {
3296 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3297 NULL) == -1)
3298 err(1, "pledge");
3299 } else {
3300 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3301 "sendfd unveil", NULL) == -1)
3302 err(1, "pledge");
3304 #endif
3305 cwd = getcwd(NULL, 0);
3306 if (cwd == NULL) {
3307 error = got_error_from_errno("getcwd");
3308 goto done;
3311 if (repo_path == NULL) {
3312 error = got_worktree_open(&worktree, cwd);
3313 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3314 goto done;
3315 else
3316 error = NULL;
3317 if (worktree) {
3318 repo_path =
3319 strdup(got_worktree_get_repo_path(worktree));
3320 if (repo_path == NULL)
3321 error = got_error_from_errno("strdup");
3322 if (error)
3323 goto done;
3324 } else {
3325 repo_path = strdup(cwd);
3326 if (repo_path == NULL) {
3327 error = got_error_from_errno("strdup");
3328 goto done;
3333 error = got_repo_open(&repo, repo_path, NULL);
3334 if (error != NULL)
3335 goto done;
3337 error = apply_unveil(got_repo_get_path(repo), do_list,
3338 worktree ? got_worktree_get_root_path(worktree) : NULL);
3339 if (error)
3340 goto done;
3342 if (do_list)
3343 error = list_refs(repo);
3344 else if (delref)
3345 error = delete_ref(repo, delref);
3346 else if (create_symref)
3347 error = add_symref(repo, argv[0], argv[1]);
3348 else
3349 error = add_ref(repo, argv[0], argv[1]);
3350 done:
3351 if (repo)
3352 got_repo_close(repo);
3353 if (worktree)
3354 got_worktree_close(worktree);
3355 free(cwd);
3356 free(repo_path);
3357 return error;
3360 __dead static void
3361 usage_branch(void)
3363 fprintf(stderr,
3364 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3365 "[name]\n", getprogname());
3366 exit(1);
3369 static const struct got_error *
3370 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3371 struct got_reference *ref)
3373 const struct got_error *err = NULL;
3374 const char *refname, *marker = " ";
3375 char *refstr;
3377 refname = got_ref_get_name(ref);
3378 if (worktree && strcmp(refname,
3379 got_worktree_get_head_ref_name(worktree)) == 0) {
3380 struct got_object_id *id = NULL;
3382 err = got_ref_resolve(&id, repo, ref);
3383 if (err)
3384 return err;
3385 if (got_object_id_cmp(id,
3386 got_worktree_get_base_commit_id(worktree)) == 0)
3387 marker = "* ";
3388 else
3389 marker = "~ ";
3390 free(id);
3393 if (strncmp(refname, "refs/heads/", 11) == 0)
3394 refname += 11;
3395 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3396 refname += 18;
3398 refstr = got_ref_to_str(ref);
3399 if (refstr == NULL)
3400 return got_error_from_errno("got_ref_to_str");
3402 printf("%s%s: %s\n", marker, refname, refstr);
3403 free(refstr);
3404 return NULL;
3407 static const struct got_error *
3408 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3410 const char *refname;
3412 if (worktree == NULL)
3413 return got_error(GOT_ERR_NOT_WORKTREE);
3415 refname = got_worktree_get_head_ref_name(worktree);
3417 if (strncmp(refname, "refs/heads/", 11) == 0)
3418 refname += 11;
3419 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3420 refname += 18;
3422 printf("%s\n", refname);
3424 return NULL;
3427 static const struct got_error *
3428 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3430 static const struct got_error *err = NULL;
3431 struct got_reflist_head refs;
3432 struct got_reflist_entry *re;
3433 struct got_reference *temp_ref = NULL;
3434 int rebase_in_progress, histedit_in_progress;
3436 SIMPLEQ_INIT(&refs);
3438 if (worktree) {
3439 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3440 worktree);
3441 if (err)
3442 return err;
3444 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3445 worktree);
3446 if (err)
3447 return err;
3449 if (rebase_in_progress || histedit_in_progress) {
3450 err = got_ref_open(&temp_ref, repo,
3451 got_worktree_get_head_ref_name(worktree), 0);
3452 if (err)
3453 return err;
3454 list_branch(repo, worktree, temp_ref);
3455 got_ref_close(temp_ref);
3459 err = got_ref_list(&refs, repo, "refs/heads",
3460 got_ref_cmp_by_name, NULL);
3461 if (err)
3462 return err;
3464 SIMPLEQ_FOREACH(re, &refs, entry)
3465 list_branch(repo, worktree, re->ref);
3467 got_ref_list_free(&refs);
3468 return NULL;
3471 static const struct got_error *
3472 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3473 const char *branch_name)
3475 const struct got_error *err = NULL;
3476 struct got_reference *ref = NULL;
3477 char *refname;
3479 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3480 return got_error_from_errno("asprintf");
3482 err = got_ref_open(&ref, repo, refname, 0);
3483 if (err)
3484 goto done;
3486 if (worktree &&
3487 strcmp(got_worktree_get_head_ref_name(worktree),
3488 got_ref_get_name(ref)) == 0) {
3489 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3490 "will not delete this work tree's current branch");
3491 goto done;
3494 err = got_ref_delete(ref, repo);
3495 done:
3496 if (ref)
3497 got_ref_close(ref);
3498 free(refname);
3499 return err;
3502 static const struct got_error *
3503 add_branch(struct got_repository *repo, const char *branch_name,
3504 struct got_object_id *base_commit_id)
3506 const struct got_error *err = NULL;
3507 struct got_reference *ref = NULL;
3508 char *base_refname = NULL, *refname = NULL;
3511 * Don't let the user create a branch name with a leading '-'.
3512 * While technically a valid reference name, this case is usually
3513 * an unintended typo.
3515 if (branch_name[0] == '-')
3516 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3518 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3519 err = got_error_from_errno("asprintf");
3520 goto done;
3523 err = got_ref_open(&ref, repo, refname, 0);
3524 if (err == NULL) {
3525 err = got_error(GOT_ERR_BRANCH_EXISTS);
3526 goto done;
3527 } else if (err->code != GOT_ERR_NOT_REF)
3528 goto done;
3530 err = got_ref_alloc(&ref, refname, base_commit_id);
3531 if (err)
3532 goto done;
3534 err = got_ref_write(ref, repo);
3535 done:
3536 if (ref)
3537 got_ref_close(ref);
3538 free(base_refname);
3539 free(refname);
3540 return err;
3543 static const struct got_error *
3544 cmd_branch(int argc, char *argv[])
3546 const struct got_error *error = NULL;
3547 struct got_repository *repo = NULL;
3548 struct got_worktree *worktree = NULL;
3549 char *cwd = NULL, *repo_path = NULL;
3550 int ch, do_list = 0, do_show = 0;
3551 const char *delref = NULL, *commit_id_arg = NULL;
3553 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3554 switch (ch) {
3555 case 'c':
3556 commit_id_arg = optarg;
3557 break;
3558 case 'd':
3559 delref = optarg;
3560 break;
3561 case 'r':
3562 repo_path = realpath(optarg, NULL);
3563 if (repo_path == NULL)
3564 return got_error_from_errno2("realpath",
3565 optarg);
3566 got_path_strip_trailing_slashes(repo_path);
3567 break;
3568 case 'l':
3569 do_list = 1;
3570 break;
3571 default:
3572 usage_branch();
3573 /* NOTREACHED */
3577 if (do_list && delref)
3578 errx(1, "-l and -d options are mutually exclusive\n");
3580 argc -= optind;
3581 argv += optind;
3583 if (!do_list && !delref && argc == 0)
3584 do_show = 1;
3586 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3587 errx(1, "-c option can only be used when creating a branch");
3589 if (do_list || delref) {
3590 if (argc > 0)
3591 usage_branch();
3592 } else if (!do_show && argc != 1)
3593 usage_branch();
3595 #ifndef PROFILE
3596 if (do_list || do_show) {
3597 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3598 NULL) == -1)
3599 err(1, "pledge");
3600 } else {
3601 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3602 "sendfd unveil", NULL) == -1)
3603 err(1, "pledge");
3605 #endif
3606 cwd = getcwd(NULL, 0);
3607 if (cwd == NULL) {
3608 error = got_error_from_errno("getcwd");
3609 goto done;
3612 if (repo_path == NULL) {
3613 error = got_worktree_open(&worktree, cwd);
3614 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3615 goto done;
3616 else
3617 error = NULL;
3618 if (worktree) {
3619 repo_path =
3620 strdup(got_worktree_get_repo_path(worktree));
3621 if (repo_path == NULL)
3622 error = got_error_from_errno("strdup");
3623 if (error)
3624 goto done;
3625 } else {
3626 repo_path = strdup(cwd);
3627 if (repo_path == NULL) {
3628 error = got_error_from_errno("strdup");
3629 goto done;
3634 error = got_repo_open(&repo, repo_path, NULL);
3635 if (error != NULL)
3636 goto done;
3638 error = apply_unveil(got_repo_get_path(repo), do_list,
3639 worktree ? got_worktree_get_root_path(worktree) : NULL);
3640 if (error)
3641 goto done;
3643 if (do_show)
3644 error = show_current_branch(repo, worktree);
3645 else if (do_list)
3646 error = list_branches(repo, worktree);
3647 else if (delref)
3648 error = delete_branch(repo, worktree, delref);
3649 else {
3650 struct got_object_id *commit_id;
3651 if (commit_id_arg == NULL)
3652 commit_id_arg = worktree ?
3653 got_worktree_get_head_ref_name(worktree) :
3654 GOT_REF_HEAD;
3655 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3656 if (error)
3657 goto done;
3658 error = add_branch(repo, argv[0], commit_id);
3659 free(commit_id);
3661 done:
3662 if (repo)
3663 got_repo_close(repo);
3664 if (worktree)
3665 got_worktree_close(worktree);
3666 free(cwd);
3667 free(repo_path);
3668 return error;
3672 __dead static void
3673 usage_tag(void)
3675 fprintf(stderr,
3676 "usage: %s tag [-r repository] | -l | "
3677 "[-m message] name [commit]\n", getprogname());
3678 exit(1);
3681 #if 0
3682 static const struct got_error *
3683 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3685 const struct got_error *err = NULL;
3686 struct got_reflist_entry *re, *se, *new;
3687 struct got_object_id *re_id, *se_id;
3688 struct got_tag_object *re_tag, *se_tag;
3689 time_t re_time, se_time;
3691 SIMPLEQ_FOREACH(re, tags, entry) {
3692 se = SIMPLEQ_FIRST(sorted);
3693 if (se == NULL) {
3694 err = got_reflist_entry_dup(&new, re);
3695 if (err)
3696 return err;
3697 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3698 continue;
3699 } else {
3700 err = got_ref_resolve(&re_id, repo, re->ref);
3701 if (err)
3702 break;
3703 err = got_object_open_as_tag(&re_tag, repo, re_id);
3704 free(re_id);
3705 if (err)
3706 break;
3707 re_time = got_object_tag_get_tagger_time(re_tag);
3708 got_object_tag_close(re_tag);
3711 while (se) {
3712 err = got_ref_resolve(&se_id, repo, re->ref);
3713 if (err)
3714 break;
3715 err = got_object_open_as_tag(&se_tag, repo, se_id);
3716 free(se_id);
3717 if (err)
3718 break;
3719 se_time = got_object_tag_get_tagger_time(se_tag);
3720 got_object_tag_close(se_tag);
3722 if (se_time > re_time) {
3723 err = got_reflist_entry_dup(&new, re);
3724 if (err)
3725 return err;
3726 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3727 break;
3729 se = SIMPLEQ_NEXT(se, entry);
3730 continue;
3733 done:
3734 return err;
3736 #endif
3738 static const struct got_error *
3739 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3740 struct got_reference *ref2)
3742 const struct got_error *err = NULL;
3743 struct got_repository *repo = arg;
3744 struct got_object_id *id1, *id2 = NULL;
3745 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3746 time_t time1, time2;
3748 *cmp = 0;
3750 err = got_ref_resolve(&id1, repo, ref1);
3751 if (err)
3752 return err;
3753 err = got_object_open_as_tag(&tag1, repo, id1);
3754 if (err)
3755 goto done;
3757 err = got_ref_resolve(&id2, repo, ref2);
3758 if (err)
3759 goto done;
3760 err = got_object_open_as_tag(&tag2, repo, id2);
3761 if (err)
3762 goto done;
3764 time1 = got_object_tag_get_tagger_time(tag1);
3765 time2 = got_object_tag_get_tagger_time(tag2);
3767 /* Put latest tags first. */
3768 if (time1 < time2)
3769 *cmp = 1;
3770 else if (time1 > time2)
3771 *cmp = -1;
3772 else
3773 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3774 done:
3775 free(id1);
3776 free(id2);
3777 if (tag1)
3778 got_object_tag_close(tag1);
3779 if (tag2)
3780 got_object_tag_close(tag2);
3781 return err;
3784 static const struct got_error *
3785 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3787 static const struct got_error *err = NULL;
3788 struct got_reflist_head refs;
3789 struct got_reflist_entry *re;
3791 SIMPLEQ_INIT(&refs);
3793 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3794 if (err)
3795 return err;
3797 SIMPLEQ_FOREACH(re, &refs, entry) {
3798 const char *refname;
3799 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3800 char datebuf[26];
3801 time_t tagger_time;
3802 struct got_object_id *id;
3803 struct got_tag_object *tag;
3805 refname = got_ref_get_name(re->ref);
3806 if (strncmp(refname, "refs/tags/", 10) != 0)
3807 continue;
3808 refname += 10;
3809 refstr = got_ref_to_str(re->ref);
3810 if (refstr == NULL) {
3811 err = got_error_from_errno("got_ref_to_str");
3812 break;
3814 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3815 free(refstr);
3817 err = got_ref_resolve(&id, repo, re->ref);
3818 if (err)
3819 break;
3820 err = got_object_open_as_tag(&tag, repo, id);
3821 free(id);
3822 if (err)
3823 break;
3824 printf("from: %s\n", got_object_tag_get_tagger(tag));
3825 tagger_time = got_object_tag_get_tagger_time(tag);
3826 datestr = get_datestr(&tagger_time, datebuf);
3827 if (datestr)
3828 printf("date: %s UTC\n", datestr);
3829 err = got_object_id_str(&id_str,
3830 got_object_tag_get_object_id(tag));
3831 if (err)
3832 break;
3833 switch (got_object_tag_get_object_type(tag)) {
3834 case GOT_OBJ_TYPE_BLOB:
3835 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3836 break;
3837 case GOT_OBJ_TYPE_TREE:
3838 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3839 break;
3840 case GOT_OBJ_TYPE_COMMIT:
3841 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3842 break;
3843 case GOT_OBJ_TYPE_TAG:
3844 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3845 break;
3846 default:
3847 break;
3849 free(id_str);
3850 tagmsg0 = strdup(got_object_tag_get_message(tag));
3851 got_object_tag_close(tag);
3852 if (tagmsg0 == NULL) {
3853 err = got_error_from_errno("strdup");
3854 break;
3857 tagmsg = tagmsg0;
3858 do {
3859 line = strsep(&tagmsg, "\n");
3860 if (line)
3861 printf(" %s\n", line);
3862 } while (line);
3863 free(tagmsg0);
3866 got_ref_list_free(&refs);
3867 return NULL;
3870 static const struct got_error *
3871 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3872 const char *tag_name, const char *repo_path)
3874 const struct got_error *err = NULL;
3875 char *template = NULL, *initial_content = NULL;
3876 char *editor = NULL;
3877 int fd = -1;
3879 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3880 err = got_error_from_errno("asprintf");
3881 goto done;
3884 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3885 commit_id_str, tag_name) == -1) {
3886 err = got_error_from_errno("asprintf");
3887 goto done;
3890 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3891 if (err)
3892 goto done;
3894 dprintf(fd, initial_content);
3895 close(fd);
3897 err = get_editor(&editor);
3898 if (err)
3899 goto done;
3900 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3901 done:
3902 free(initial_content);
3903 free(template);
3904 free(editor);
3906 /* Editor is done; we can now apply unveil(2) */
3907 if (err == NULL) {
3908 err = apply_unveil(repo_path, 0, NULL);
3909 if (err) {
3910 free(*tagmsg);
3911 *tagmsg = NULL;
3914 return err;
3917 static const struct got_error *
3918 add_tag(struct got_repository *repo, const char *tag_name,
3919 const char *commit_arg, const char *tagmsg_arg)
3921 const struct got_error *err = NULL;
3922 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3923 char *label = NULL, *commit_id_str = NULL;
3924 struct got_reference *ref = NULL;
3925 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3926 char *tagmsg_path = NULL, *tag_id_str = NULL;
3927 int preserve_tagmsg = 0;
3930 * Don't let the user create a tag name with a leading '-'.
3931 * While technically a valid reference name, this case is usually
3932 * an unintended typo.
3934 if (tag_name[0] == '-')
3935 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3937 err = get_author(&tagger, repo);
3938 if (err)
3939 return err;
3941 err = match_object_id(&commit_id, &label, commit_arg,
3942 GOT_OBJ_TYPE_COMMIT, 1, repo);
3943 if (err)
3944 goto done;
3946 err = got_object_id_str(&commit_id_str, commit_id);
3947 if (err)
3948 goto done;
3950 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3951 refname = strdup(tag_name);
3952 if (refname == NULL) {
3953 err = got_error_from_errno("strdup");
3954 goto done;
3956 tag_name += 10;
3957 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3958 err = got_error_from_errno("asprintf");
3959 goto done;
3962 err = got_ref_open(&ref, repo, refname, 0);
3963 if (err == NULL) {
3964 err = got_error(GOT_ERR_TAG_EXISTS);
3965 goto done;
3966 } else if (err->code != GOT_ERR_NOT_REF)
3967 goto done;
3969 if (tagmsg_arg == NULL) {
3970 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3971 tag_name, got_repo_get_path(repo));
3972 if (err) {
3973 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3974 tagmsg_path != NULL)
3975 preserve_tagmsg = 1;
3976 goto done;
3980 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3981 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3982 if (err) {
3983 if (tagmsg_path)
3984 preserve_tagmsg = 1;
3985 goto done;
3988 err = got_ref_alloc(&ref, refname, tag_id);
3989 if (err) {
3990 if (tagmsg_path)
3991 preserve_tagmsg = 1;
3992 goto done;
3995 err = got_ref_write(ref, repo);
3996 if (err) {
3997 if (tagmsg_path)
3998 preserve_tagmsg = 1;
3999 goto done;
4002 err = got_object_id_str(&tag_id_str, tag_id);
4003 if (err) {
4004 if (tagmsg_path)
4005 preserve_tagmsg = 1;
4006 goto done;
4008 printf("Created tag %s\n", tag_id_str);
4009 done:
4010 if (preserve_tagmsg) {
4011 fprintf(stderr, "%s: tag message preserved in %s\n",
4012 getprogname(), tagmsg_path);
4013 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4014 err = got_error_from_errno2("unlink", tagmsg_path);
4015 free(tag_id_str);
4016 if (ref)
4017 got_ref_close(ref);
4018 free(commit_id);
4019 free(commit_id_str);
4020 free(refname);
4021 free(tagmsg);
4022 free(tagmsg_path);
4023 free(tagger);
4024 return err;
4027 static const struct got_error *
4028 cmd_tag(int argc, char *argv[])
4030 const struct got_error *error = NULL;
4031 struct got_repository *repo = NULL;
4032 struct got_worktree *worktree = NULL;
4033 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4034 char *gitconfig_path = NULL;
4035 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4036 int ch, do_list = 0;
4038 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4039 switch (ch) {
4040 case 'm':
4041 tagmsg = optarg;
4042 break;
4043 case 'r':
4044 repo_path = realpath(optarg, NULL);
4045 if (repo_path == NULL)
4046 return got_error_from_errno2("realpath",
4047 optarg);
4048 got_path_strip_trailing_slashes(repo_path);
4049 break;
4050 case 'l':
4051 do_list = 1;
4052 break;
4053 default:
4054 usage_tag();
4055 /* NOTREACHED */
4059 argc -= optind;
4060 argv += optind;
4062 if (do_list) {
4063 if (tagmsg)
4064 errx(1, "-l and -m options are mutually exclusive\n");
4065 if (argc > 0)
4066 usage_tag();
4067 } else if (argc < 1 || argc > 2)
4068 usage_tag();
4069 else if (argc > 1)
4070 commit_id_arg = argv[1];
4071 tag_name = argv[0];
4073 #ifndef PROFILE
4074 if (do_list) {
4075 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4076 NULL) == -1)
4077 err(1, "pledge");
4078 } else {
4079 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4080 "sendfd unveil", NULL) == -1)
4081 err(1, "pledge");
4083 #endif
4084 cwd = getcwd(NULL, 0);
4085 if (cwd == NULL) {
4086 error = got_error_from_errno("getcwd");
4087 goto done;
4090 if (repo_path == NULL) {
4091 error = got_worktree_open(&worktree, cwd);
4092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4093 goto done;
4094 else
4095 error = NULL;
4096 if (worktree) {
4097 repo_path =
4098 strdup(got_worktree_get_repo_path(worktree));
4099 if (repo_path == NULL)
4100 error = got_error_from_errno("strdup");
4101 if (error)
4102 goto done;
4103 } else {
4104 repo_path = strdup(cwd);
4105 if (repo_path == NULL) {
4106 error = got_error_from_errno("strdup");
4107 goto done;
4112 if (do_list) {
4113 error = got_repo_open(&repo, repo_path, NULL);
4114 if (error != NULL)
4115 goto done;
4116 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4117 if (error)
4118 goto done;
4119 error = list_tags(repo, worktree);
4120 } else {
4121 error = get_gitconfig_path(&gitconfig_path);
4122 if (error)
4123 goto done;
4124 error = got_repo_open(&repo, repo_path, gitconfig_path);
4125 if (error != NULL)
4126 goto done;
4128 if (tagmsg) {
4129 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4130 if (error)
4131 goto done;
4134 if (commit_id_arg == NULL) {
4135 struct got_reference *head_ref;
4136 struct got_object_id *commit_id;
4137 error = got_ref_open(&head_ref, repo,
4138 worktree ? got_worktree_get_head_ref_name(worktree)
4139 : GOT_REF_HEAD, 0);
4140 if (error)
4141 goto done;
4142 error = got_ref_resolve(&commit_id, repo, head_ref);
4143 got_ref_close(head_ref);
4144 if (error)
4145 goto done;
4146 error = got_object_id_str(&commit_id_str, commit_id);
4147 free(commit_id);
4148 if (error)
4149 goto done;
4152 error = add_tag(repo, tag_name,
4153 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4155 done:
4156 if (repo)
4157 got_repo_close(repo);
4158 if (worktree)
4159 got_worktree_close(worktree);
4160 free(cwd);
4161 free(repo_path);
4162 free(gitconfig_path);
4163 free(commit_id_str);
4164 return error;
4167 __dead static void
4168 usage_add(void)
4170 fprintf(stderr, "usage: %s add [-R] [-I] file-path ...\n",
4171 getprogname());
4172 exit(1);
4175 static const struct got_error *
4176 add_progress(void *arg, unsigned char status, const char *path)
4178 while (path[0] == '/')
4179 path++;
4180 printf("%c %s\n", status, path);
4181 return NULL;
4184 static const struct got_error *
4185 cmd_add(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4190 char *cwd = NULL;
4191 struct got_pathlist_head paths;
4192 struct got_pathlist_entry *pe;
4193 int ch, can_recurse = 0, no_ignores = 0;
4195 TAILQ_INIT(&paths);
4197 while ((ch = getopt(argc, argv, "IR")) != -1) {
4198 switch (ch) {
4199 case 'I':
4200 no_ignores = 1;
4201 break;
4202 case 'R':
4203 can_recurse = 1;
4204 break;
4205 default:
4206 usage_add();
4207 /* NOTREACHED */
4211 argc -= optind;
4212 argv += optind;
4214 #ifndef PROFILE
4215 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4216 NULL) == -1)
4217 err(1, "pledge");
4218 #endif
4219 if (argc < 1)
4220 usage_add();
4222 cwd = getcwd(NULL, 0);
4223 if (cwd == NULL) {
4224 error = got_error_from_errno("getcwd");
4225 goto done;
4228 error = got_worktree_open(&worktree, cwd);
4229 if (error)
4230 goto done;
4232 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4233 NULL);
4234 if (error != NULL)
4235 goto done;
4237 error = apply_unveil(got_repo_get_path(repo), 1,
4238 got_worktree_get_root_path(worktree));
4239 if (error)
4240 goto done;
4242 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4243 if (error)
4244 goto done;
4246 if (!can_recurse && no_ignores) {
4247 error = got_error_msg(GOT_ERR_BAD_PATH,
4248 "disregarding ignores requires -R option");
4249 goto done;
4253 if (!can_recurse) {
4254 char *ondisk_path;
4255 struct stat sb;
4256 TAILQ_FOREACH(pe, &paths, entry) {
4257 if (asprintf(&ondisk_path, "%s/%s",
4258 got_worktree_get_root_path(worktree),
4259 pe->path) == -1) {
4260 error = got_error_from_errno("asprintf");
4261 goto done;
4263 if (lstat(ondisk_path, &sb) == -1) {
4264 if (errno == ENOENT) {
4265 free(ondisk_path);
4266 continue;
4268 error = got_error_from_errno2("lstat",
4269 ondisk_path);
4270 free(ondisk_path);
4271 goto done;
4273 free(ondisk_path);
4274 if (S_ISDIR(sb.st_mode)) {
4275 error = got_error_msg(GOT_ERR_BAD_PATH,
4276 "adding directories requires -R option");
4277 goto done;
4282 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4283 NULL, repo, no_ignores);
4284 done:
4285 if (repo)
4286 got_repo_close(repo);
4287 if (worktree)
4288 got_worktree_close(worktree);
4289 TAILQ_FOREACH(pe, &paths, entry)
4290 free((char *)pe->path);
4291 got_pathlist_free(&paths);
4292 free(cwd);
4293 return error;
4296 __dead static void
4297 usage_remove(void)
4299 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] file-path ...\n",
4300 getprogname());
4301 exit(1);
4304 static const struct got_error *
4305 print_remove_status(void *arg, unsigned char status,
4306 unsigned char staged_status, const char *path)
4308 while (path[0] == '/')
4309 path++;
4310 if (status == GOT_STATUS_NONEXISTENT)
4311 return NULL;
4312 if (status == staged_status && (status == GOT_STATUS_DELETE))
4313 status = GOT_STATUS_NO_CHANGE;
4314 printf("%c%c %s\n", status, staged_status, path);
4315 return NULL;
4318 static const struct got_error *
4319 cmd_remove(int argc, char *argv[])
4321 const struct got_error *error = NULL;
4322 struct got_worktree *worktree = NULL;
4323 struct got_repository *repo = NULL;
4324 char *cwd = NULL;
4325 struct got_pathlist_head paths;
4326 struct got_pathlist_entry *pe;
4327 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4329 TAILQ_INIT(&paths);
4331 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4332 switch (ch) {
4333 case 'f':
4334 delete_local_mods = 1;
4335 break;
4336 case 'k':
4337 keep_on_disk = 1;
4338 break;
4339 case 'R':
4340 can_recurse = 1;
4341 break;
4342 default:
4343 usage_remove();
4344 /* NOTREACHED */
4348 argc -= optind;
4349 argv += optind;
4351 #ifndef PROFILE
4352 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4353 NULL) == -1)
4354 err(1, "pledge");
4355 #endif
4356 if (argc < 1)
4357 usage_remove();
4359 cwd = getcwd(NULL, 0);
4360 if (cwd == NULL) {
4361 error = got_error_from_errno("getcwd");
4362 goto done;
4364 error = got_worktree_open(&worktree, cwd);
4365 if (error)
4366 goto done;
4368 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4369 NULL);
4370 if (error)
4371 goto done;
4373 error = apply_unveil(got_repo_get_path(repo), 1,
4374 got_worktree_get_root_path(worktree));
4375 if (error)
4376 goto done;
4378 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4379 if (error)
4380 goto done;
4382 if (!can_recurse) {
4383 char *ondisk_path;
4384 struct stat sb;
4385 TAILQ_FOREACH(pe, &paths, entry) {
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(worktree),
4388 pe->path) == -1) {
4389 error = got_error_from_errno("asprintf");
4390 goto done;
4392 if (lstat(ondisk_path, &sb) == -1) {
4393 if (errno == ENOENT) {
4394 free(ondisk_path);
4395 continue;
4397 error = got_error_from_errno2("lstat",
4398 ondisk_path);
4399 free(ondisk_path);
4400 goto done;
4402 free(ondisk_path);
4403 if (S_ISDIR(sb.st_mode)) {
4404 error = got_error_msg(GOT_ERR_BAD_PATH,
4405 "removing directories requires -R option");
4406 goto done;
4411 error = got_worktree_schedule_delete(worktree, &paths,
4412 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4413 if (error)
4414 goto done;
4415 done:
4416 if (repo)
4417 got_repo_close(repo);
4418 if (worktree)
4419 got_worktree_close(worktree);
4420 TAILQ_FOREACH(pe, &paths, entry)
4421 free((char *)pe->path);
4422 got_pathlist_free(&paths);
4423 free(cwd);
4424 return error;
4427 __dead static void
4428 usage_revert(void)
4430 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4431 "path ...\n", getprogname());
4432 exit(1);
4435 static const struct got_error *
4436 revert_progress(void *arg, unsigned char status, const char *path)
4438 while (path[0] == '/')
4439 path++;
4440 printf("%c %s\n", status, path);
4441 return NULL;
4444 struct choose_patch_arg {
4445 FILE *patch_script_file;
4446 const char *action;
4449 static const struct got_error *
4450 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4451 int nchanges, const char *action)
4453 char *line = NULL;
4454 size_t linesize = 0;
4455 ssize_t linelen;
4457 switch (status) {
4458 case GOT_STATUS_ADD:
4459 printf("A %s\n%s this addition? [y/n] ", path, action);
4460 break;
4461 case GOT_STATUS_DELETE:
4462 printf("D %s\n%s this deletion? [y/n] ", path, action);
4463 break;
4464 case GOT_STATUS_MODIFY:
4465 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4466 return got_error_from_errno("fseek");
4467 printf(GOT_COMMIT_SEP_STR);
4468 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4469 printf("%s", line);
4470 if (ferror(patch_file))
4471 return got_error_from_errno("getline");
4472 printf(GOT_COMMIT_SEP_STR);
4473 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4474 path, n, nchanges, action);
4475 break;
4476 default:
4477 return got_error_path(path, GOT_ERR_FILE_STATUS);
4480 return NULL;
4483 static const struct got_error *
4484 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4485 FILE *patch_file, int n, int nchanges)
4487 const struct got_error *err = NULL;
4488 char *line = NULL;
4489 size_t linesize = 0;
4490 ssize_t linelen;
4491 int resp = ' ';
4492 struct choose_patch_arg *a = arg;
4494 *choice = GOT_PATCH_CHOICE_NONE;
4496 if (a->patch_script_file) {
4497 char *nl;
4498 err = show_change(status, path, patch_file, n, nchanges,
4499 a->action);
4500 if (err)
4501 return err;
4502 linelen = getline(&line, &linesize, a->patch_script_file);
4503 if (linelen == -1) {
4504 if (ferror(a->patch_script_file))
4505 return got_error_from_errno("getline");
4506 return NULL;
4508 nl = strchr(line, '\n');
4509 if (nl)
4510 *nl = '\0';
4511 if (strcmp(line, "y") == 0) {
4512 *choice = GOT_PATCH_CHOICE_YES;
4513 printf("y\n");
4514 } else if (strcmp(line, "n") == 0) {
4515 *choice = GOT_PATCH_CHOICE_NO;
4516 printf("n\n");
4517 } else if (strcmp(line, "q") == 0 &&
4518 status == GOT_STATUS_MODIFY) {
4519 *choice = GOT_PATCH_CHOICE_QUIT;
4520 printf("q\n");
4521 } else
4522 printf("invalid response '%s'\n", line);
4523 free(line);
4524 return NULL;
4527 while (resp != 'y' && resp != 'n' && resp != 'q') {
4528 err = show_change(status, path, patch_file, n, nchanges,
4529 a->action);
4530 if (err)
4531 return err;
4532 resp = getchar();
4533 if (resp == '\n')
4534 resp = getchar();
4535 if (status == GOT_STATUS_MODIFY) {
4536 if (resp != 'y' && resp != 'n' && resp != 'q') {
4537 printf("invalid response '%c'\n", resp);
4538 resp = ' ';
4540 } else if (resp != 'y' && resp != 'n') {
4541 printf("invalid response '%c'\n", resp);
4542 resp = ' ';
4546 if (resp == 'y')
4547 *choice = GOT_PATCH_CHOICE_YES;
4548 else if (resp == 'n')
4549 *choice = GOT_PATCH_CHOICE_NO;
4550 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4551 *choice = GOT_PATCH_CHOICE_QUIT;
4553 return NULL;
4557 static const struct got_error *
4558 cmd_revert(int argc, char *argv[])
4560 const struct got_error *error = NULL;
4561 struct got_worktree *worktree = NULL;
4562 struct got_repository *repo = NULL;
4563 char *cwd = NULL, *path = NULL;
4564 struct got_pathlist_head paths;
4565 struct got_pathlist_entry *pe;
4566 int ch, can_recurse = 0, pflag = 0;
4567 FILE *patch_script_file = NULL;
4568 const char *patch_script_path = NULL;
4569 struct choose_patch_arg cpa;
4571 TAILQ_INIT(&paths);
4573 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4574 switch (ch) {
4575 case 'p':
4576 pflag = 1;
4577 break;
4578 case 'F':
4579 patch_script_path = optarg;
4580 break;
4581 case 'R':
4582 can_recurse = 1;
4583 break;
4584 default:
4585 usage_revert();
4586 /* NOTREACHED */
4590 argc -= optind;
4591 argv += optind;
4593 #ifndef PROFILE
4594 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4595 "unveil", NULL) == -1)
4596 err(1, "pledge");
4597 #endif
4598 if (argc < 1)
4599 usage_revert();
4600 if (patch_script_path && !pflag)
4601 errx(1, "-F option can only be used together with -p option");
4603 cwd = getcwd(NULL, 0);
4604 if (cwd == NULL) {
4605 error = got_error_from_errno("getcwd");
4606 goto done;
4608 error = got_worktree_open(&worktree, cwd);
4609 if (error)
4610 goto done;
4612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4613 NULL);
4614 if (error != NULL)
4615 goto done;
4617 if (patch_script_path) {
4618 patch_script_file = fopen(patch_script_path, "r");
4619 if (patch_script_file == NULL) {
4620 error = got_error_from_errno2("fopen",
4621 patch_script_path);
4622 goto done;
4625 error = apply_unveil(got_repo_get_path(repo), 1,
4626 got_worktree_get_root_path(worktree));
4627 if (error)
4628 goto done;
4630 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4631 if (error)
4632 goto done;
4634 if (!can_recurse) {
4635 char *ondisk_path;
4636 struct stat sb;
4637 TAILQ_FOREACH(pe, &paths, entry) {
4638 if (asprintf(&ondisk_path, "%s/%s",
4639 got_worktree_get_root_path(worktree),
4640 pe->path) == -1) {
4641 error = got_error_from_errno("asprintf");
4642 goto done;
4644 if (lstat(ondisk_path, &sb) == -1) {
4645 if (errno == ENOENT) {
4646 free(ondisk_path);
4647 continue;
4649 error = got_error_from_errno2("lstat",
4650 ondisk_path);
4651 free(ondisk_path);
4652 goto done;
4654 free(ondisk_path);
4655 if (S_ISDIR(sb.st_mode)) {
4656 error = got_error_msg(GOT_ERR_BAD_PATH,
4657 "reverting directories requires -R option");
4658 goto done;
4663 cpa.patch_script_file = patch_script_file;
4664 cpa.action = "revert";
4665 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4666 pflag ? choose_patch : NULL, &cpa, repo);
4667 if (error)
4668 goto done;
4669 done:
4670 if (patch_script_file && fclose(patch_script_file) == EOF &&
4671 error == NULL)
4672 error = got_error_from_errno2("fclose", patch_script_path);
4673 if (repo)
4674 got_repo_close(repo);
4675 if (worktree)
4676 got_worktree_close(worktree);
4677 free(path);
4678 free(cwd);
4679 return error;
4682 __dead static void
4683 usage_commit(void)
4685 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4686 getprogname());
4687 exit(1);
4690 struct collect_commit_logmsg_arg {
4691 const char *cmdline_log;
4692 const char *editor;
4693 const char *worktree_path;
4694 const char *branch_name;
4695 const char *repo_path;
4696 char *logmsg_path;
4700 static const struct got_error *
4701 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4702 void *arg)
4704 char *initial_content = NULL;
4705 struct got_pathlist_entry *pe;
4706 const struct got_error *err = NULL;
4707 char *template = NULL;
4708 struct collect_commit_logmsg_arg *a = arg;
4709 int fd;
4710 size_t len;
4712 /* if a message was specified on the command line, just use it */
4713 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4714 len = strlen(a->cmdline_log) + 1;
4715 *logmsg = malloc(len + 1);
4716 if (*logmsg == NULL)
4717 return got_error_from_errno("malloc");
4718 strlcpy(*logmsg, a->cmdline_log, len);
4719 return NULL;
4722 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4723 return got_error_from_errno("asprintf");
4725 if (asprintf(&initial_content,
4726 "\n# changes to be committed on branch %s:\n",
4727 a->branch_name) == -1)
4728 return got_error_from_errno("asprintf");
4730 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4731 if (err)
4732 goto done;
4734 dprintf(fd, initial_content);
4736 TAILQ_FOREACH(pe, commitable_paths, entry) {
4737 struct got_commitable *ct = pe->data;
4738 dprintf(fd, "# %c %s\n",
4739 got_commitable_get_status(ct),
4740 got_commitable_get_path(ct));
4742 close(fd);
4744 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4745 done:
4746 free(initial_content);
4747 free(template);
4749 /* Editor is done; we can now apply unveil(2) */
4750 if (err == NULL) {
4751 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4752 if (err) {
4753 free(*logmsg);
4754 *logmsg = NULL;
4757 return err;
4760 static const struct got_error *
4761 cmd_commit(int argc, char *argv[])
4763 const struct got_error *error = NULL;
4764 struct got_worktree *worktree = NULL;
4765 struct got_repository *repo = NULL;
4766 char *cwd = NULL, *id_str = NULL;
4767 struct got_object_id *id = NULL;
4768 const char *logmsg = NULL;
4769 struct collect_commit_logmsg_arg cl_arg;
4770 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4771 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4772 struct got_pathlist_head paths;
4774 TAILQ_INIT(&paths);
4775 cl_arg.logmsg_path = NULL;
4777 while ((ch = getopt(argc, argv, "m:")) != -1) {
4778 switch (ch) {
4779 case 'm':
4780 logmsg = optarg;
4781 break;
4782 default:
4783 usage_commit();
4784 /* NOTREACHED */
4788 argc -= optind;
4789 argv += optind;
4791 #ifndef PROFILE
4792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4793 "unveil", NULL) == -1)
4794 err(1, "pledge");
4795 #endif
4796 cwd = getcwd(NULL, 0);
4797 if (cwd == NULL) {
4798 error = got_error_from_errno("getcwd");
4799 goto done;
4801 error = got_worktree_open(&worktree, cwd);
4802 if (error)
4803 goto done;
4805 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4806 if (error)
4807 goto done;
4808 if (rebase_in_progress) {
4809 error = got_error(GOT_ERR_REBASING);
4810 goto done;
4813 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4814 worktree);
4815 if (error)
4816 goto done;
4818 error = get_gitconfig_path(&gitconfig_path);
4819 if (error)
4820 goto done;
4821 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4822 gitconfig_path);
4823 if (error != NULL)
4824 goto done;
4826 error = get_author(&author, repo);
4827 if (error)
4828 return error;
4831 * unveil(2) traverses exec(2); if an editor is used we have
4832 * to apply unveil after the log message has been written.
4834 if (logmsg == NULL || strlen(logmsg) == 0)
4835 error = get_editor(&editor);
4836 else
4837 error = apply_unveil(got_repo_get_path(repo), 0,
4838 got_worktree_get_root_path(worktree));
4839 if (error)
4840 goto done;
4842 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4843 if (error)
4844 goto done;
4846 cl_arg.editor = editor;
4847 cl_arg.cmdline_log = logmsg;
4848 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4849 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4850 if (!histedit_in_progress) {
4851 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4852 error = got_error(GOT_ERR_COMMIT_BRANCH);
4853 goto done;
4855 cl_arg.branch_name += 11;
4857 cl_arg.repo_path = got_repo_get_path(repo);
4858 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4859 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4860 if (error) {
4861 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4862 cl_arg.logmsg_path != NULL)
4863 preserve_logmsg = 1;
4864 goto done;
4867 error = got_object_id_str(&id_str, id);
4868 if (error)
4869 goto done;
4870 printf("Created commit %s\n", id_str);
4871 done:
4872 if (preserve_logmsg) {
4873 fprintf(stderr, "%s: log message preserved in %s\n",
4874 getprogname(), cl_arg.logmsg_path);
4875 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4876 error == NULL)
4877 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4878 free(cl_arg.logmsg_path);
4879 if (repo)
4880 got_repo_close(repo);
4881 if (worktree)
4882 got_worktree_close(worktree);
4883 free(cwd);
4884 free(id_str);
4885 free(gitconfig_path);
4886 free(editor);
4887 free(author);
4888 return error;
4891 __dead static void
4892 usage_cherrypick(void)
4894 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4895 exit(1);
4898 static const struct got_error *
4899 cmd_cherrypick(int argc, char *argv[])
4901 const struct got_error *error = NULL;
4902 struct got_worktree *worktree = NULL;
4903 struct got_repository *repo = NULL;
4904 char *cwd = NULL, *commit_id_str = NULL;
4905 struct got_object_id *commit_id = NULL;
4906 struct got_commit_object *commit = NULL;
4907 struct got_object_qid *pid;
4908 struct got_reference *head_ref = NULL;
4909 int ch, did_something = 0;
4911 while ((ch = getopt(argc, argv, "")) != -1) {
4912 switch (ch) {
4913 default:
4914 usage_cherrypick();
4915 /* NOTREACHED */
4919 argc -= optind;
4920 argv += optind;
4922 #ifndef PROFILE
4923 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4924 "unveil", NULL) == -1)
4925 err(1, "pledge");
4926 #endif
4927 if (argc != 1)
4928 usage_cherrypick();
4930 cwd = getcwd(NULL, 0);
4931 if (cwd == NULL) {
4932 error = got_error_from_errno("getcwd");
4933 goto done;
4935 error = got_worktree_open(&worktree, cwd);
4936 if (error)
4937 goto done;
4939 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4940 NULL);
4941 if (error != NULL)
4942 goto done;
4944 error = apply_unveil(got_repo_get_path(repo), 0,
4945 got_worktree_get_root_path(worktree));
4946 if (error)
4947 goto done;
4949 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4950 GOT_OBJ_TYPE_COMMIT, repo);
4951 if (error != NULL) {
4952 struct got_reference *ref;
4953 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4954 goto done;
4955 error = got_ref_open(&ref, repo, argv[0], 0);
4956 if (error != NULL)
4957 goto done;
4958 error = got_ref_resolve(&commit_id, repo, ref);
4959 got_ref_close(ref);
4960 if (error != NULL)
4961 goto done;
4963 error = got_object_id_str(&commit_id_str, commit_id);
4964 if (error)
4965 goto done;
4967 error = got_ref_open(&head_ref, repo,
4968 got_worktree_get_head_ref_name(worktree), 0);
4969 if (error != NULL)
4970 goto done;
4972 error = check_same_branch(commit_id, head_ref, NULL, repo);
4973 if (error) {
4974 if (error->code != GOT_ERR_ANCESTRY)
4975 goto done;
4976 error = NULL;
4977 } else {
4978 error = got_error(GOT_ERR_SAME_BRANCH);
4979 goto done;
4982 error = got_object_open_as_commit(&commit, repo, commit_id);
4983 if (error)
4984 goto done;
4985 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4986 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4987 commit_id, repo, update_progress, &did_something, check_cancelled,
4988 NULL);
4989 if (error != NULL)
4990 goto done;
4992 if (did_something)
4993 printf("Merged commit %s\n", commit_id_str);
4994 done:
4995 if (commit)
4996 got_object_commit_close(commit);
4997 free(commit_id_str);
4998 if (head_ref)
4999 got_ref_close(head_ref);
5000 if (worktree)
5001 got_worktree_close(worktree);
5002 if (repo)
5003 got_repo_close(repo);
5004 return error;
5007 __dead static void
5008 usage_backout(void)
5010 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5011 exit(1);
5014 static const struct got_error *
5015 cmd_backout(int argc, char *argv[])
5017 const struct got_error *error = NULL;
5018 struct got_worktree *worktree = NULL;
5019 struct got_repository *repo = NULL;
5020 char *cwd = NULL, *commit_id_str = NULL;
5021 struct got_object_id *commit_id = NULL;
5022 struct got_commit_object *commit = NULL;
5023 struct got_object_qid *pid;
5024 struct got_reference *head_ref = NULL;
5025 int ch, did_something = 0;
5027 while ((ch = getopt(argc, argv, "")) != -1) {
5028 switch (ch) {
5029 default:
5030 usage_backout();
5031 /* NOTREACHED */
5035 argc -= optind;
5036 argv += optind;
5038 #ifndef PROFILE
5039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5040 "unveil", NULL) == -1)
5041 err(1, "pledge");
5042 #endif
5043 if (argc != 1)
5044 usage_backout();
5046 cwd = getcwd(NULL, 0);
5047 if (cwd == NULL) {
5048 error = got_error_from_errno("getcwd");
5049 goto done;
5051 error = got_worktree_open(&worktree, cwd);
5052 if (error)
5053 goto done;
5055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5056 NULL);
5057 if (error != NULL)
5058 goto done;
5060 error = apply_unveil(got_repo_get_path(repo), 0,
5061 got_worktree_get_root_path(worktree));
5062 if (error)
5063 goto done;
5065 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5066 GOT_OBJ_TYPE_COMMIT, repo);
5067 if (error != NULL) {
5068 struct got_reference *ref;
5069 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5070 goto done;
5071 error = got_ref_open(&ref, repo, argv[0], 0);
5072 if (error != NULL)
5073 goto done;
5074 error = got_ref_resolve(&commit_id, repo, ref);
5075 got_ref_close(ref);
5076 if (error != NULL)
5077 goto done;
5079 error = got_object_id_str(&commit_id_str, commit_id);
5080 if (error)
5081 goto done;
5083 error = got_ref_open(&head_ref, repo,
5084 got_worktree_get_head_ref_name(worktree), 0);
5085 if (error != NULL)
5086 goto done;
5088 error = check_same_branch(commit_id, head_ref, NULL, repo);
5089 if (error)
5090 goto done;
5092 error = got_object_open_as_commit(&commit, repo, commit_id);
5093 if (error)
5094 goto done;
5095 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5096 if (pid == NULL) {
5097 error = got_error(GOT_ERR_ROOT_COMMIT);
5098 goto done;
5101 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5102 update_progress, &did_something, check_cancelled, NULL);
5103 if (error != NULL)
5104 goto done;
5106 if (did_something)
5107 printf("Backed out commit %s\n", commit_id_str);
5108 done:
5109 if (commit)
5110 got_object_commit_close(commit);
5111 free(commit_id_str);
5112 if (head_ref)
5113 got_ref_close(head_ref);
5114 if (worktree)
5115 got_worktree_close(worktree);
5116 if (repo)
5117 got_repo_close(repo);
5118 return error;
5121 __dead static void
5122 usage_rebase(void)
5124 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5125 getprogname());
5126 exit(1);
5129 void
5130 trim_logmsg(char *logmsg, int limit)
5132 char *nl;
5133 size_t len;
5135 len = strlen(logmsg);
5136 if (len > limit)
5137 len = limit;
5138 logmsg[len] = '\0';
5139 nl = strchr(logmsg, '\n');
5140 if (nl)
5141 *nl = '\0';
5144 static const struct got_error *
5145 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5147 const struct got_error *err;
5148 char *logmsg0 = NULL;
5149 const char *s;
5151 err = got_object_commit_get_logmsg(&logmsg0, commit);
5152 if (err)
5153 return err;
5155 s = logmsg0;
5156 while (isspace((unsigned char)s[0]))
5157 s++;
5159 *logmsg = strdup(s);
5160 if (*logmsg == NULL) {
5161 err = got_error_from_errno("strdup");
5162 goto done;
5165 trim_logmsg(*logmsg, limit);
5166 done:
5167 free(logmsg0);
5168 return err;
5171 static const struct got_error *
5172 show_rebase_progress(struct got_commit_object *commit,
5173 struct got_object_id *old_id, struct got_object_id *new_id)
5175 const struct got_error *err;
5176 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5178 err = got_object_id_str(&old_id_str, old_id);
5179 if (err)
5180 goto done;
5182 if (new_id) {
5183 err = got_object_id_str(&new_id_str, new_id);
5184 if (err)
5185 goto done;
5188 old_id_str[12] = '\0';
5189 if (new_id_str)
5190 new_id_str[12] = '\0';
5192 err = get_short_logmsg(&logmsg, 42, commit);
5193 if (err)
5194 goto done;
5196 printf("%s -> %s: %s\n", old_id_str,
5197 new_id_str ? new_id_str : "no-op change", logmsg);
5198 done:
5199 free(old_id_str);
5200 free(new_id_str);
5201 return err;
5204 static const struct got_error *
5205 rebase_progress(void *arg, unsigned char status, const char *path)
5207 unsigned char *rebase_status = arg;
5209 while (path[0] == '/')
5210 path++;
5211 printf("%c %s\n", status, path);
5213 if (*rebase_status == GOT_STATUS_CONFLICT)
5214 return NULL;
5215 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5216 *rebase_status = status;
5217 return NULL;
5220 static const struct got_error *
5221 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5222 struct got_reference *branch, struct got_reference *new_base_branch,
5223 struct got_reference *tmp_branch, struct got_repository *repo)
5225 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5226 return got_worktree_rebase_complete(worktree, fileindex,
5227 new_base_branch, tmp_branch, branch, repo);
5230 static const struct got_error *
5231 rebase_commit(struct got_pathlist_head *merged_paths,
5232 struct got_worktree *worktree, struct got_fileindex *fileindex,
5233 struct got_reference *tmp_branch,
5234 struct got_object_id *commit_id, struct got_repository *repo)
5236 const struct got_error *error;
5237 struct got_commit_object *commit;
5238 struct got_object_id *new_commit_id;
5240 error = got_object_open_as_commit(&commit, repo, commit_id);
5241 if (error)
5242 return error;
5244 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5245 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5246 if (error) {
5247 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5248 goto done;
5249 error = show_rebase_progress(commit, commit_id, NULL);
5250 } else {
5251 error = show_rebase_progress(commit, commit_id, new_commit_id);
5252 free(new_commit_id);
5254 done:
5255 got_object_commit_close(commit);
5256 return error;
5259 struct check_path_prefix_arg {
5260 const char *path_prefix;
5261 size_t len;
5262 int errcode;
5265 static const struct got_error *
5266 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5267 struct got_blob_object *blob2, struct got_object_id *id1,
5268 struct got_object_id *id2, const char *path1, const char *path2,
5269 mode_t mode1, mode_t mode2, struct got_repository *repo)
5271 struct check_path_prefix_arg *a = arg;
5273 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5274 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5275 return got_error(a->errcode);
5277 return NULL;
5280 static const struct got_error *
5281 check_path_prefix(struct got_object_id *parent_id,
5282 struct got_object_id *commit_id, const char *path_prefix,
5283 int errcode, struct got_repository *repo)
5285 const struct got_error *err;
5286 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5287 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5288 struct check_path_prefix_arg cpp_arg;
5290 if (got_path_is_root_dir(path_prefix))
5291 return NULL;
5293 err = got_object_open_as_commit(&commit, repo, commit_id);
5294 if (err)
5295 goto done;
5297 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5298 if (err)
5299 goto done;
5301 err = got_object_open_as_tree(&tree1, repo,
5302 got_object_commit_get_tree_id(parent_commit));
5303 if (err)
5304 goto done;
5306 err = got_object_open_as_tree(&tree2, repo,
5307 got_object_commit_get_tree_id(commit));
5308 if (err)
5309 goto done;
5311 cpp_arg.path_prefix = path_prefix;
5312 while (cpp_arg.path_prefix[0] == '/')
5313 cpp_arg.path_prefix++;
5314 cpp_arg.len = strlen(cpp_arg.path_prefix);
5315 cpp_arg.errcode = errcode;
5316 err = got_diff_tree(tree1, tree2, "", "", repo,
5317 check_path_prefix_in_diff, &cpp_arg, 0);
5318 done:
5319 if (tree1)
5320 got_object_tree_close(tree1);
5321 if (tree2)
5322 got_object_tree_close(tree2);
5323 if (commit)
5324 got_object_commit_close(commit);
5325 if (parent_commit)
5326 got_object_commit_close(parent_commit);
5327 return err;
5330 static const struct got_error *
5331 collect_commits(struct got_object_id_queue *commits,
5332 struct got_object_id *initial_commit_id,
5333 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5334 const char *path_prefix, int path_prefix_errcode,
5335 struct got_repository *repo)
5337 const struct got_error *err = NULL;
5338 struct got_commit_graph *graph = NULL;
5339 struct got_object_id *parent_id = NULL;
5340 struct got_object_qid *qid;
5341 struct got_object_id *commit_id = initial_commit_id;
5343 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5344 if (err)
5345 return err;
5347 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5348 check_cancelled, NULL);
5349 if (err)
5350 goto done;
5351 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5352 err = got_commit_graph_iter_next(&parent_id, graph);
5353 if (err) {
5354 if (err->code == GOT_ERR_ITER_COMPLETED) {
5355 err = got_error_msg(GOT_ERR_ANCESTRY,
5356 "ran out of commits to rebase before "
5357 "youngest common ancestor commit has "
5358 "been reached?!?");
5359 goto done;
5360 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5361 goto done;
5362 err = got_commit_graph_fetch_commits(graph, 1, repo,
5363 check_cancelled, NULL);
5364 if (err)
5365 goto done;
5366 } else {
5367 err = check_path_prefix(parent_id, commit_id,
5368 path_prefix, path_prefix_errcode, repo);
5369 if (err)
5370 goto done;
5372 err = got_object_qid_alloc(&qid, commit_id);
5373 if (err)
5374 goto done;
5375 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5376 commit_id = parent_id;
5379 done:
5380 got_commit_graph_close(graph);
5381 return err;
5384 static const struct got_error *
5385 cmd_rebase(int argc, char *argv[])
5387 const struct got_error *error = NULL;
5388 struct got_worktree *worktree = NULL;
5389 struct got_repository *repo = NULL;
5390 struct got_fileindex *fileindex = NULL;
5391 char *cwd = NULL;
5392 struct got_reference *branch = NULL;
5393 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5394 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5395 struct got_object_id *resume_commit_id = NULL;
5396 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5397 struct got_commit_object *commit = NULL;
5398 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5399 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5400 struct got_object_id_queue commits;
5401 struct got_pathlist_head merged_paths;
5402 const struct got_object_id_queue *parent_ids;
5403 struct got_object_qid *qid, *pid;
5405 SIMPLEQ_INIT(&commits);
5406 TAILQ_INIT(&merged_paths);
5408 while ((ch = getopt(argc, argv, "ac")) != -1) {
5409 switch (ch) {
5410 case 'a':
5411 abort_rebase = 1;
5412 break;
5413 case 'c':
5414 continue_rebase = 1;
5415 break;
5416 default:
5417 usage_rebase();
5418 /* NOTREACHED */
5422 argc -= optind;
5423 argv += optind;
5425 #ifndef PROFILE
5426 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5427 "unveil", NULL) == -1)
5428 err(1, "pledge");
5429 #endif
5430 if (abort_rebase && continue_rebase)
5431 usage_rebase();
5432 else if (abort_rebase || continue_rebase) {
5433 if (argc != 0)
5434 usage_rebase();
5435 } else if (argc != 1)
5436 usage_rebase();
5438 cwd = getcwd(NULL, 0);
5439 if (cwd == NULL) {
5440 error = got_error_from_errno("getcwd");
5441 goto done;
5443 error = got_worktree_open(&worktree, cwd);
5444 if (error)
5445 goto done;
5447 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5448 NULL);
5449 if (error != NULL)
5450 goto done;
5452 error = apply_unveil(got_repo_get_path(repo), 0,
5453 got_worktree_get_root_path(worktree));
5454 if (error)
5455 goto done;
5457 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5458 if (error)
5459 goto done;
5461 if (abort_rebase) {
5462 int did_something;
5463 if (!rebase_in_progress) {
5464 error = got_error(GOT_ERR_NOT_REBASING);
5465 goto done;
5467 error = got_worktree_rebase_continue(&resume_commit_id,
5468 &new_base_branch, &tmp_branch, &branch, &fileindex,
5469 worktree, repo);
5470 if (error)
5471 goto done;
5472 printf("Switching work tree to %s\n",
5473 got_ref_get_symref_target(new_base_branch));
5474 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5475 new_base_branch, update_progress, &did_something);
5476 if (error)
5477 goto done;
5478 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5479 goto done; /* nothing else to do */
5482 if (continue_rebase) {
5483 if (!rebase_in_progress) {
5484 error = got_error(GOT_ERR_NOT_REBASING);
5485 goto done;
5487 error = got_worktree_rebase_continue(&resume_commit_id,
5488 &new_base_branch, &tmp_branch, &branch, &fileindex,
5489 worktree, repo);
5490 if (error)
5491 goto done;
5493 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5494 resume_commit_id, repo);
5495 if (error)
5496 goto done;
5498 yca_id = got_object_id_dup(resume_commit_id);
5499 if (yca_id == NULL) {
5500 error = got_error_from_errno("got_object_id_dup");
5501 goto done;
5503 } else {
5504 error = got_ref_open(&branch, repo, argv[0], 0);
5505 if (error != NULL)
5506 goto done;
5509 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5510 if (error)
5511 goto done;
5513 if (!continue_rebase) {
5514 struct got_object_id *base_commit_id;
5516 base_commit_id = got_worktree_get_base_commit_id(worktree);
5517 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5518 base_commit_id, branch_head_commit_id, repo,
5519 check_cancelled, NULL);
5520 if (error)
5521 goto done;
5522 if (yca_id == NULL) {
5523 error = got_error_msg(GOT_ERR_ANCESTRY,
5524 "specified branch shares no common ancestry "
5525 "with work tree's branch");
5526 goto done;
5529 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5530 if (error) {
5531 if (error->code != GOT_ERR_ANCESTRY)
5532 goto done;
5533 error = NULL;
5534 } else {
5535 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5536 "specified branch resolves to a commit which "
5537 "is already contained in work tree's branch");
5538 goto done;
5540 error = got_worktree_rebase_prepare(&new_base_branch,
5541 &tmp_branch, &fileindex, worktree, branch, repo);
5542 if (error)
5543 goto done;
5546 commit_id = branch_head_commit_id;
5547 error = got_object_open_as_commit(&commit, repo, commit_id);
5548 if (error)
5549 goto done;
5551 parent_ids = got_object_commit_get_parent_ids(commit);
5552 pid = SIMPLEQ_FIRST(parent_ids);
5553 if (pid == NULL) {
5554 if (!continue_rebase) {
5555 int did_something;
5556 error = got_worktree_rebase_abort(worktree, fileindex,
5557 repo, new_base_branch, update_progress,
5558 &did_something);
5559 if (error)
5560 goto done;
5561 printf("Rebase of %s aborted\n",
5562 got_ref_get_name(branch));
5564 error = got_error(GOT_ERR_EMPTY_REBASE);
5565 goto done;
5567 error = collect_commits(&commits, commit_id, pid->id,
5568 yca_id, got_worktree_get_path_prefix(worktree),
5569 GOT_ERR_REBASE_PATH, repo);
5570 got_object_commit_close(commit);
5571 commit = NULL;
5572 if (error)
5573 goto done;
5575 if (SIMPLEQ_EMPTY(&commits)) {
5576 if (continue_rebase) {
5577 error = rebase_complete(worktree, fileindex,
5578 branch, new_base_branch, tmp_branch, repo);
5579 goto done;
5580 } else {
5581 /* Fast-forward the reference of the branch. */
5582 struct got_object_id *new_head_commit_id;
5583 char *id_str;
5584 error = got_ref_resolve(&new_head_commit_id, repo,
5585 new_base_branch);
5586 if (error)
5587 goto done;
5588 error = got_object_id_str(&id_str, new_head_commit_id);
5589 printf("Forwarding %s to commit %s\n",
5590 got_ref_get_name(branch), id_str);
5591 free(id_str);
5592 error = got_ref_change_ref(branch,
5593 new_head_commit_id);
5594 if (error)
5595 goto done;
5599 pid = NULL;
5600 SIMPLEQ_FOREACH(qid, &commits, entry) {
5601 commit_id = qid->id;
5602 parent_id = pid ? pid->id : yca_id;
5603 pid = qid;
5605 error = got_worktree_rebase_merge_files(&merged_paths,
5606 worktree, fileindex, parent_id, commit_id, repo,
5607 rebase_progress, &rebase_status, check_cancelled, NULL);
5608 if (error)
5609 goto done;
5611 if (rebase_status == GOT_STATUS_CONFLICT) {
5612 got_worktree_rebase_pathlist_free(&merged_paths);
5613 break;
5616 error = rebase_commit(&merged_paths, worktree, fileindex,
5617 tmp_branch, commit_id, repo);
5618 got_worktree_rebase_pathlist_free(&merged_paths);
5619 if (error)
5620 goto done;
5623 if (rebase_status == GOT_STATUS_CONFLICT) {
5624 error = got_worktree_rebase_postpone(worktree, fileindex);
5625 if (error)
5626 goto done;
5627 error = got_error_msg(GOT_ERR_CONFLICTS,
5628 "conflicts must be resolved before rebasing can continue");
5629 } else
5630 error = rebase_complete(worktree, fileindex, branch,
5631 new_base_branch, tmp_branch, repo);
5632 done:
5633 got_object_id_queue_free(&commits);
5634 free(branch_head_commit_id);
5635 free(resume_commit_id);
5636 free(yca_id);
5637 if (commit)
5638 got_object_commit_close(commit);
5639 if (branch)
5640 got_ref_close(branch);
5641 if (new_base_branch)
5642 got_ref_close(new_base_branch);
5643 if (tmp_branch)
5644 got_ref_close(tmp_branch);
5645 if (worktree)
5646 got_worktree_close(worktree);
5647 if (repo)
5648 got_repo_close(repo);
5649 return error;
5652 __dead static void
5653 usage_histedit(void)
5655 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5656 getprogname());
5657 exit(1);
5660 #define GOT_HISTEDIT_PICK 'p'
5661 #define GOT_HISTEDIT_EDIT 'e'
5662 #define GOT_HISTEDIT_FOLD 'f'
5663 #define GOT_HISTEDIT_DROP 'd'
5664 #define GOT_HISTEDIT_MESG 'm'
5666 static struct got_histedit_cmd {
5667 unsigned char code;
5668 const char *name;
5669 const char *desc;
5670 } got_histedit_cmds[] = {
5671 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5672 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5673 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5674 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5675 { GOT_HISTEDIT_MESG, "mesg",
5676 "single-line log message for commit above (open editor if empty)" },
5679 struct got_histedit_list_entry {
5680 TAILQ_ENTRY(got_histedit_list_entry) entry;
5681 struct got_object_id *commit_id;
5682 const struct got_histedit_cmd *cmd;
5683 char *logmsg;
5685 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5687 static const struct got_error *
5688 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5689 FILE *f, struct got_repository *repo)
5691 const struct got_error *err = NULL;
5692 char *logmsg = NULL, *id_str = NULL;
5693 struct got_commit_object *commit = NULL;
5694 int n;
5696 err = got_object_open_as_commit(&commit, repo, commit_id);
5697 if (err)
5698 goto done;
5700 err = get_short_logmsg(&logmsg, 34, commit);
5701 if (err)
5702 goto done;
5704 err = got_object_id_str(&id_str, commit_id);
5705 if (err)
5706 goto done;
5708 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5709 if (n < 0)
5710 err = got_ferror(f, GOT_ERR_IO);
5711 done:
5712 if (commit)
5713 got_object_commit_close(commit);
5714 free(id_str);
5715 free(logmsg);
5716 return err;
5719 static const struct got_error *
5720 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5721 struct got_repository *repo)
5723 const struct got_error *err = NULL;
5724 struct got_object_qid *qid;
5726 if (SIMPLEQ_EMPTY(commits))
5727 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5729 SIMPLEQ_FOREACH(qid, commits, entry) {
5730 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5731 f, repo);
5732 if (err)
5733 break;
5736 return err;
5739 static const struct got_error *
5740 write_cmd_list(FILE *f)
5742 const struct got_error *err = NULL;
5743 int n, i;
5745 n = fprintf(f, "# Available histedit commands:\n");
5746 if (n < 0)
5747 return got_ferror(f, GOT_ERR_IO);
5749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5750 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5751 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5752 cmd->desc);
5753 if (n < 0) {
5754 err = got_ferror(f, GOT_ERR_IO);
5755 break;
5758 n = fprintf(f, "# Commits will be processed in order from top to "
5759 "bottom of this file.\n");
5760 if (n < 0)
5761 return got_ferror(f, GOT_ERR_IO);
5762 return err;
5765 static const struct got_error *
5766 histedit_syntax_error(int lineno)
5768 static char msg[42];
5769 int ret;
5771 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5772 lineno);
5773 if (ret == -1 || ret >= sizeof(msg))
5774 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5776 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5779 static const struct got_error *
5780 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5781 char *logmsg, struct got_repository *repo)
5783 const struct got_error *err;
5784 struct got_commit_object *folded_commit = NULL;
5785 char *id_str, *folded_logmsg = NULL;
5787 err = got_object_id_str(&id_str, hle->commit_id);
5788 if (err)
5789 return err;
5791 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5792 if (err)
5793 goto done;
5795 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5796 if (err)
5797 goto done;
5798 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5799 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5800 folded_logmsg) == -1) {
5801 err = got_error_from_errno("asprintf");
5802 goto done;
5804 done:
5805 if (folded_commit)
5806 got_object_commit_close(folded_commit);
5807 free(id_str);
5808 free(folded_logmsg);
5809 return err;
5812 static struct got_histedit_list_entry *
5813 get_folded_commits(struct got_histedit_list_entry *hle)
5815 struct got_histedit_list_entry *prev, *folded = NULL;
5817 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5818 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5819 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5820 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5821 folded = prev;
5822 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5825 return folded;
5828 static const struct got_error *
5829 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5830 struct got_repository *repo)
5832 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5833 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5834 const struct got_error *err = NULL;
5835 struct got_commit_object *commit = NULL;
5836 int fd;
5837 struct got_histedit_list_entry *folded = NULL;
5839 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5840 if (err)
5841 return err;
5843 folded = get_folded_commits(hle);
5844 if (folded) {
5845 while (folded != hle) {
5846 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5847 folded = TAILQ_NEXT(folded, entry);
5848 continue;
5850 err = append_folded_commit_msg(&new_msg, folded,
5851 logmsg, repo);
5852 if (err)
5853 goto done;
5854 free(logmsg);
5855 logmsg = new_msg;
5856 folded = TAILQ_NEXT(folded, entry);
5860 err = got_object_id_str(&id_str, hle->commit_id);
5861 if (err)
5862 goto done;
5863 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5864 if (err)
5865 goto done;
5866 if (asprintf(&new_msg,
5867 "%s\n# original log message of commit %s: %s",
5868 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5869 err = got_error_from_errno("asprintf");
5870 goto done;
5872 free(logmsg);
5873 logmsg = new_msg;
5875 err = got_object_id_str(&id_str, hle->commit_id);
5876 if (err)
5877 goto done;
5879 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5880 if (err)
5881 goto done;
5883 dprintf(fd, logmsg);
5884 close(fd);
5886 err = get_editor(&editor);
5887 if (err)
5888 goto done;
5890 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5891 if (err) {
5892 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5893 goto done;
5894 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5896 done:
5897 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5898 err = got_error_from_errno2("unlink", logmsg_path);
5899 free(logmsg_path);
5900 free(logmsg);
5901 free(orig_logmsg);
5902 free(editor);
5903 if (commit)
5904 got_object_commit_close(commit);
5905 return err;
5908 static const struct got_error *
5909 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5910 FILE *f, struct got_repository *repo)
5912 const struct got_error *err = NULL;
5913 char *line = NULL, *p, *end;
5914 size_t size;
5915 ssize_t len;
5916 int lineno = 0, i;
5917 const struct got_histedit_cmd *cmd;
5918 struct got_object_id *commit_id = NULL;
5919 struct got_histedit_list_entry *hle = NULL;
5921 for (;;) {
5922 len = getline(&line, &size, f);
5923 if (len == -1) {
5924 const struct got_error *getline_err;
5925 if (feof(f))
5926 break;
5927 getline_err = got_error_from_errno("getline");
5928 err = got_ferror(f, getline_err->code);
5929 break;
5931 lineno++;
5932 p = line;
5933 while (isspace((unsigned char)p[0]))
5934 p++;
5935 if (p[0] == '#' || p[0] == '\0') {
5936 free(line);
5937 line = NULL;
5938 continue;
5940 cmd = NULL;
5941 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5942 cmd = &got_histedit_cmds[i];
5943 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5944 isspace((unsigned char)p[strlen(cmd->name)])) {
5945 p += strlen(cmd->name);
5946 break;
5948 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5949 p++;
5950 break;
5953 if (i == nitems(got_histedit_cmds)) {
5954 err = histedit_syntax_error(lineno);
5955 break;
5957 while (isspace((unsigned char)p[0]))
5958 p++;
5959 if (cmd->code == GOT_HISTEDIT_MESG) {
5960 if (hle == NULL || hle->logmsg != NULL) {
5961 err = got_error(GOT_ERR_HISTEDIT_CMD);
5962 break;
5964 if (p[0] == '\0') {
5965 err = histedit_edit_logmsg(hle, repo);
5966 if (err)
5967 break;
5968 } else {
5969 hle->logmsg = strdup(p);
5970 if (hle->logmsg == NULL) {
5971 err = got_error_from_errno("strdup");
5972 break;
5975 free(line);
5976 line = NULL;
5977 continue;
5978 } else {
5979 end = p;
5980 while (end[0] && !isspace((unsigned char)end[0]))
5981 end++;
5982 *end = '\0';
5984 err = got_object_resolve_id_str(&commit_id, repo, p);
5985 if (err) {
5986 /* override error code */
5987 err = histedit_syntax_error(lineno);
5988 break;
5991 hle = malloc(sizeof(*hle));
5992 if (hle == NULL) {
5993 err = got_error_from_errno("malloc");
5994 break;
5996 hle->cmd = cmd;
5997 hle->commit_id = commit_id;
5998 hle->logmsg = NULL;
5999 commit_id = NULL;
6000 free(line);
6001 line = NULL;
6002 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6005 free(line);
6006 free(commit_id);
6007 return err;
6010 static const struct got_error *
6011 histedit_check_script(struct got_histedit_list *histedit_cmds,
6012 struct got_object_id_queue *commits, struct got_repository *repo)
6014 const struct got_error *err = NULL;
6015 struct got_object_qid *qid;
6016 struct got_histedit_list_entry *hle;
6017 static char msg[80];
6018 char *id_str;
6020 if (TAILQ_EMPTY(histedit_cmds))
6021 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6022 "histedit script contains no commands");
6023 if (SIMPLEQ_EMPTY(commits))
6024 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6026 SIMPLEQ_FOREACH(qid, commits, entry) {
6027 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6028 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6029 break;
6031 if (hle == NULL) {
6032 err = got_object_id_str(&id_str, qid->id);
6033 if (err)
6034 return err;
6035 snprintf(msg, sizeof(msg),
6036 "commit %s missing from histedit script", id_str);
6037 free(id_str);
6038 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6042 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6043 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6044 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6045 "last commit in histedit script cannot be folded");
6047 return NULL;
6050 static const struct got_error *
6051 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6052 const char *path, struct got_object_id_queue *commits,
6053 struct got_repository *repo)
6055 const struct got_error *err = NULL;
6056 char *editor;
6057 FILE *f = NULL;
6059 err = get_editor(&editor);
6060 if (err)
6061 return err;
6063 if (spawn_editor(editor, path) == -1) {
6064 err = got_error_from_errno("failed spawning editor");
6065 goto done;
6068 f = fopen(path, "r");
6069 if (f == NULL) {
6070 err = got_error_from_errno("fopen");
6071 goto done;
6073 err = histedit_parse_list(histedit_cmds, f, repo);
6074 if (err)
6075 goto done;
6077 err = histedit_check_script(histedit_cmds, commits, repo);
6078 done:
6079 if (f && fclose(f) != 0 && err == NULL)
6080 err = got_error_from_errno("fclose");
6081 free(editor);
6082 return err;
6085 static const struct got_error *
6086 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6087 struct got_object_id_queue *, const char *, struct got_repository *);
6089 static const struct got_error *
6090 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6091 struct got_object_id_queue *commits, struct got_repository *repo)
6093 const struct got_error *err;
6094 FILE *f = NULL;
6095 char *path = NULL;
6097 err = got_opentemp_named(&path, &f, "got-histedit");
6098 if (err)
6099 return err;
6101 err = write_cmd_list(f);
6102 if (err)
6103 goto done;
6105 err = histedit_write_commit_list(commits, f, repo);
6106 if (err)
6107 goto done;
6109 if (fclose(f) != 0) {
6110 err = got_error_from_errno("fclose");
6111 goto done;
6113 f = NULL;
6115 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6116 if (err) {
6117 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6118 err->code != GOT_ERR_HISTEDIT_CMD)
6119 goto done;
6120 err = histedit_edit_list_retry(histedit_cmds, err,
6121 commits, path, repo);
6123 done:
6124 if (f && fclose(f) != 0 && err == NULL)
6125 err = got_error_from_errno("fclose");
6126 if (path && unlink(path) != 0 && err == NULL)
6127 err = got_error_from_errno2("unlink", path);
6128 free(path);
6129 return err;
6132 static const struct got_error *
6133 histedit_save_list(struct got_histedit_list *histedit_cmds,
6134 struct got_worktree *worktree, struct got_repository *repo)
6136 const struct got_error *err = NULL;
6137 char *path = NULL;
6138 FILE *f = NULL;
6139 struct got_histedit_list_entry *hle;
6140 struct got_commit_object *commit = NULL;
6142 err = got_worktree_get_histedit_script_path(&path, worktree);
6143 if (err)
6144 return err;
6146 f = fopen(path, "w");
6147 if (f == NULL) {
6148 err = got_error_from_errno2("fopen", path);
6149 goto done;
6151 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6152 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6153 repo);
6154 if (err)
6155 break;
6157 if (hle->logmsg) {
6158 int n = fprintf(f, "%c %s\n",
6159 GOT_HISTEDIT_MESG, hle->logmsg);
6160 if (n < 0) {
6161 err = got_ferror(f, GOT_ERR_IO);
6162 break;
6166 done:
6167 if (f && fclose(f) != 0 && err == NULL)
6168 err = got_error_from_errno("fclose");
6169 free(path);
6170 if (commit)
6171 got_object_commit_close(commit);
6172 return err;
6175 void
6176 histedit_free_list(struct got_histedit_list *histedit_cmds)
6178 struct got_histedit_list_entry *hle;
6180 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6181 TAILQ_REMOVE(histedit_cmds, hle, entry);
6182 free(hle);
6186 static const struct got_error *
6187 histedit_load_list(struct got_histedit_list *histedit_cmds,
6188 const char *path, struct got_repository *repo)
6190 const struct got_error *err = NULL;
6191 FILE *f = NULL;
6193 f = fopen(path, "r");
6194 if (f == NULL) {
6195 err = got_error_from_errno2("fopen", path);
6196 goto done;
6199 err = histedit_parse_list(histedit_cmds, f, repo);
6200 done:
6201 if (f && fclose(f) != 0 && err == NULL)
6202 err = got_error_from_errno("fclose");
6203 return err;
6206 static const struct got_error *
6207 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6208 const struct got_error *edit_err, struct got_object_id_queue *commits,
6209 const char *path, struct got_repository *repo)
6211 const struct got_error *err = NULL, *prev_err = edit_err;
6212 int resp = ' ';
6214 while (resp != 'c' && resp != 'r' && resp != 'a') {
6215 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6216 "or (a)bort: ", getprogname(), prev_err->msg);
6217 resp = getchar();
6218 if (resp == '\n')
6219 resp = getchar();
6220 if (resp == 'c') {
6221 histedit_free_list(histedit_cmds);
6222 err = histedit_run_editor(histedit_cmds, path, commits,
6223 repo);
6224 if (err) {
6225 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6226 err->code != GOT_ERR_HISTEDIT_CMD)
6227 break;
6228 prev_err = err;
6229 resp = ' ';
6230 continue;
6232 break;
6233 } else if (resp == 'r') {
6234 histedit_free_list(histedit_cmds);
6235 err = histedit_edit_script(histedit_cmds,
6236 commits, repo);
6237 if (err) {
6238 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6239 err->code != GOT_ERR_HISTEDIT_CMD)
6240 break;
6241 prev_err = err;
6242 resp = ' ';
6243 continue;
6245 break;
6246 } else if (resp == 'a') {
6247 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6248 break;
6249 } else
6250 printf("invalid response '%c'\n", resp);
6253 return err;
6256 static const struct got_error *
6257 histedit_complete(struct got_worktree *worktree,
6258 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6259 struct got_reference *branch, struct got_repository *repo)
6261 printf("Switching work tree to %s\n",
6262 got_ref_get_symref_target(branch));
6263 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6264 branch, repo);
6267 static const struct got_error *
6268 show_histedit_progress(struct got_commit_object *commit,
6269 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6271 const struct got_error *err;
6272 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6274 err = got_object_id_str(&old_id_str, hle->commit_id);
6275 if (err)
6276 goto done;
6278 if (new_id) {
6279 err = got_object_id_str(&new_id_str, new_id);
6280 if (err)
6281 goto done;
6284 old_id_str[12] = '\0';
6285 if (new_id_str)
6286 new_id_str[12] = '\0';
6288 if (hle->logmsg) {
6289 logmsg = strdup(hle->logmsg);
6290 if (logmsg == NULL) {
6291 err = got_error_from_errno("strdup");
6292 goto done;
6294 trim_logmsg(logmsg, 42);
6295 } else {
6296 err = get_short_logmsg(&logmsg, 42, commit);
6297 if (err)
6298 goto done;
6301 switch (hle->cmd->code) {
6302 case GOT_HISTEDIT_PICK:
6303 case GOT_HISTEDIT_EDIT:
6304 printf("%s -> %s: %s\n", old_id_str,
6305 new_id_str ? new_id_str : "no-op change", logmsg);
6306 break;
6307 case GOT_HISTEDIT_DROP:
6308 case GOT_HISTEDIT_FOLD:
6309 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6310 logmsg);
6311 break;
6312 default:
6313 break;
6316 done:
6317 free(old_id_str);
6318 free(new_id_str);
6319 return err;
6322 static const struct got_error *
6323 histedit_commit(struct got_pathlist_head *merged_paths,
6324 struct got_worktree *worktree, struct got_fileindex *fileindex,
6325 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6326 struct got_repository *repo)
6328 const struct got_error *err;
6329 struct got_commit_object *commit;
6330 struct got_object_id *new_commit_id;
6332 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6333 && hle->logmsg == NULL) {
6334 err = histedit_edit_logmsg(hle, repo);
6335 if (err)
6336 return err;
6339 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6340 if (err)
6341 return err;
6343 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6344 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6345 hle->logmsg, repo);
6346 if (err) {
6347 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6348 goto done;
6349 err = show_histedit_progress(commit, hle, NULL);
6350 } else {
6351 err = show_histedit_progress(commit, hle, new_commit_id);
6352 free(new_commit_id);
6354 done:
6355 got_object_commit_close(commit);
6356 return err;
6359 static const struct got_error *
6360 histedit_skip_commit(struct got_histedit_list_entry *hle,
6361 struct got_worktree *worktree, struct got_repository *repo)
6363 const struct got_error *error;
6364 struct got_commit_object *commit;
6366 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6367 repo);
6368 if (error)
6369 return error;
6371 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6372 if (error)
6373 return error;
6375 error = show_histedit_progress(commit, hle, NULL);
6376 got_object_commit_close(commit);
6377 return error;
6380 static const struct got_error *
6381 cmd_histedit(int argc, char *argv[])
6383 const struct got_error *error = NULL;
6384 struct got_worktree *worktree = NULL;
6385 struct got_fileindex *fileindex = NULL;
6386 struct got_repository *repo = NULL;
6387 char *cwd = NULL;
6388 struct got_reference *branch = NULL;
6389 struct got_reference *tmp_branch = NULL;
6390 struct got_object_id *resume_commit_id = NULL;
6391 struct got_object_id *base_commit_id = NULL;
6392 struct got_object_id *head_commit_id = NULL;
6393 struct got_commit_object *commit = NULL;
6394 int ch, rebase_in_progress = 0, did_something;
6395 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6396 const char *edit_script_path = NULL;
6397 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6398 struct got_object_id_queue commits;
6399 struct got_pathlist_head merged_paths;
6400 const struct got_object_id_queue *parent_ids;
6401 struct got_object_qid *pid;
6402 struct got_histedit_list histedit_cmds;
6403 struct got_histedit_list_entry *hle;
6405 SIMPLEQ_INIT(&commits);
6406 TAILQ_INIT(&histedit_cmds);
6407 TAILQ_INIT(&merged_paths);
6409 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6410 switch (ch) {
6411 case 'a':
6412 abort_edit = 1;
6413 break;
6414 case 'c':
6415 continue_edit = 1;
6416 break;
6417 case 'F':
6418 edit_script_path = optarg;
6419 break;
6420 default:
6421 usage_histedit();
6422 /* NOTREACHED */
6426 argc -= optind;
6427 argv += optind;
6429 #ifndef PROFILE
6430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6431 "unveil", NULL) == -1)
6432 err(1, "pledge");
6433 #endif
6434 if (abort_edit && continue_edit)
6435 usage_histedit();
6436 if (argc != 0)
6437 usage_histedit();
6440 * This command cannot apply unveil(2) in all cases because the
6441 * user may choose to run an editor to edit the histedit script
6442 * and to edit individual commit log messages.
6443 * unveil(2) traverses exec(2); if an editor is used we have to
6444 * apply unveil after edit script and log messages have been written.
6445 * XXX TODO: Make use of unveil(2) where possible.
6448 cwd = getcwd(NULL, 0);
6449 if (cwd == NULL) {
6450 error = got_error_from_errno("getcwd");
6451 goto done;
6453 error = got_worktree_open(&worktree, cwd);
6454 if (error)
6455 goto done;
6457 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6458 NULL);
6459 if (error != NULL)
6460 goto done;
6462 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6463 if (error)
6464 goto done;
6465 if (rebase_in_progress) {
6466 error = got_error(GOT_ERR_REBASING);
6467 goto done;
6470 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6471 if (error)
6472 goto done;
6474 if (edit_in_progress && abort_edit) {
6475 error = got_worktree_histedit_continue(&resume_commit_id,
6476 &tmp_branch, &branch, &base_commit_id, &fileindex,
6477 worktree, repo);
6478 if (error)
6479 goto done;
6480 printf("Switching work tree to %s\n",
6481 got_ref_get_symref_target(branch));
6482 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6483 branch, base_commit_id, update_progress, &did_something);
6484 if (error)
6485 goto done;
6486 printf("Histedit of %s aborted\n",
6487 got_ref_get_symref_target(branch));
6488 goto done; /* nothing else to do */
6489 } else if (abort_edit) {
6490 error = got_error(GOT_ERR_NOT_HISTEDIT);
6491 goto done;
6494 if (continue_edit) {
6495 char *path;
6497 if (!edit_in_progress) {
6498 error = got_error(GOT_ERR_NOT_HISTEDIT);
6499 goto done;
6502 error = got_worktree_get_histedit_script_path(&path, worktree);
6503 if (error)
6504 goto done;
6506 error = histedit_load_list(&histedit_cmds, path, repo);
6507 free(path);
6508 if (error)
6509 goto done;
6511 error = got_worktree_histedit_continue(&resume_commit_id,
6512 &tmp_branch, &branch, &base_commit_id, &fileindex,
6513 worktree, repo);
6514 if (error)
6515 goto done;
6517 error = got_ref_resolve(&head_commit_id, repo, branch);
6518 if (error)
6519 goto done;
6521 error = got_object_open_as_commit(&commit, repo,
6522 head_commit_id);
6523 if (error)
6524 goto done;
6525 parent_ids = got_object_commit_get_parent_ids(commit);
6526 pid = SIMPLEQ_FIRST(parent_ids);
6527 if (pid == NULL) {
6528 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6529 goto done;
6531 error = collect_commits(&commits, head_commit_id, pid->id,
6532 base_commit_id, got_worktree_get_path_prefix(worktree),
6533 GOT_ERR_HISTEDIT_PATH, repo);
6534 got_object_commit_close(commit);
6535 commit = NULL;
6536 if (error)
6537 goto done;
6538 } else {
6539 if (edit_in_progress) {
6540 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6541 goto done;
6544 error = got_ref_open(&branch, repo,
6545 got_worktree_get_head_ref_name(worktree), 0);
6546 if (error != NULL)
6547 goto done;
6549 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6550 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6551 "will not edit commit history of a branch outside "
6552 "the \"refs/heads/\" reference namespace");
6553 goto done;
6556 error = got_ref_resolve(&head_commit_id, repo, branch);
6557 got_ref_close(branch);
6558 branch = NULL;
6559 if (error)
6560 goto done;
6562 error = got_object_open_as_commit(&commit, repo,
6563 head_commit_id);
6564 if (error)
6565 goto done;
6566 parent_ids = got_object_commit_get_parent_ids(commit);
6567 pid = SIMPLEQ_FIRST(parent_ids);
6568 if (pid == NULL) {
6569 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6570 goto done;
6572 error = collect_commits(&commits, head_commit_id, pid->id,
6573 got_worktree_get_base_commit_id(worktree),
6574 got_worktree_get_path_prefix(worktree),
6575 GOT_ERR_HISTEDIT_PATH, repo);
6576 got_object_commit_close(commit);
6577 commit = NULL;
6578 if (error)
6579 goto done;
6581 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6582 &base_commit_id, &fileindex, worktree, repo);
6583 if (error)
6584 goto done;
6586 if (edit_script_path) {
6587 error = histedit_load_list(&histedit_cmds,
6588 edit_script_path, repo);
6589 if (error) {
6590 got_worktree_histedit_abort(worktree, fileindex,
6591 repo, branch, base_commit_id,
6592 update_progress, &did_something);
6593 goto done;
6595 } else {
6596 error = histedit_edit_script(&histedit_cmds, &commits,
6597 repo);
6598 if (error) {
6599 got_worktree_histedit_abort(worktree, fileindex,
6600 repo, branch, base_commit_id,
6601 update_progress, &did_something);
6602 goto done;
6607 error = histedit_save_list(&histedit_cmds, worktree,
6608 repo);
6609 if (error) {
6610 got_worktree_histedit_abort(worktree, fileindex,
6611 repo, branch, base_commit_id,
6612 update_progress, &did_something);
6613 goto done;
6618 error = histedit_check_script(&histedit_cmds, &commits, repo);
6619 if (error)
6620 goto done;
6622 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6623 if (resume_commit_id) {
6624 if (got_object_id_cmp(hle->commit_id,
6625 resume_commit_id) != 0)
6626 continue;
6628 resume_commit_id = NULL;
6629 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6630 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6631 error = histedit_skip_commit(hle, worktree,
6632 repo);
6633 } else {
6634 error = histedit_commit(NULL, worktree,
6635 fileindex, tmp_branch, hle, repo);
6637 if (error)
6638 goto done;
6639 continue;
6642 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6643 error = histedit_skip_commit(hle, worktree, repo);
6644 if (error)
6645 goto done;
6646 continue;
6649 error = got_object_open_as_commit(&commit, repo,
6650 hle->commit_id);
6651 if (error)
6652 goto done;
6653 parent_ids = got_object_commit_get_parent_ids(commit);
6654 pid = SIMPLEQ_FIRST(parent_ids);
6656 error = got_worktree_histedit_merge_files(&merged_paths,
6657 worktree, fileindex, pid->id, hle->commit_id, repo,
6658 rebase_progress, &rebase_status, check_cancelled, NULL);
6659 if (error)
6660 goto done;
6661 got_object_commit_close(commit);
6662 commit = NULL;
6664 if (rebase_status == GOT_STATUS_CONFLICT) {
6665 got_worktree_rebase_pathlist_free(&merged_paths);
6666 break;
6669 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6670 char *id_str;
6671 error = got_object_id_str(&id_str, hle->commit_id);
6672 if (error)
6673 goto done;
6674 printf("Stopping histedit for amending commit %s\n",
6675 id_str);
6676 free(id_str);
6677 got_worktree_rebase_pathlist_free(&merged_paths);
6678 error = got_worktree_histedit_postpone(worktree,
6679 fileindex);
6680 goto done;
6683 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6684 error = histedit_skip_commit(hle, worktree, repo);
6685 if (error)
6686 goto done;
6687 continue;
6690 error = histedit_commit(&merged_paths, worktree, fileindex,
6691 tmp_branch, hle, repo);
6692 got_worktree_rebase_pathlist_free(&merged_paths);
6693 if (error)
6694 goto done;
6697 if (rebase_status == GOT_STATUS_CONFLICT) {
6698 error = got_worktree_histedit_postpone(worktree, fileindex);
6699 if (error)
6700 goto done;
6701 error = got_error_msg(GOT_ERR_CONFLICTS,
6702 "conflicts must be resolved before rebasing can continue");
6703 } else
6704 error = histedit_complete(worktree, fileindex, tmp_branch,
6705 branch, repo);
6706 done:
6707 got_object_id_queue_free(&commits);
6708 histedit_free_list(&histedit_cmds);
6709 free(head_commit_id);
6710 free(base_commit_id);
6711 free(resume_commit_id);
6712 if (commit)
6713 got_object_commit_close(commit);
6714 if (branch)
6715 got_ref_close(branch);
6716 if (tmp_branch)
6717 got_ref_close(tmp_branch);
6718 if (worktree)
6719 got_worktree_close(worktree);
6720 if (repo)
6721 got_repo_close(repo);
6722 return error;
6725 __dead static void
6726 usage_integrate(void)
6728 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6729 exit(1);
6732 static const struct got_error *
6733 cmd_integrate(int argc, char *argv[])
6735 const struct got_error *error = NULL;
6736 struct got_repository *repo = NULL;
6737 struct got_worktree *worktree = NULL;
6738 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6739 const char *branch_arg = NULL;
6740 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6741 struct got_fileindex *fileindex = NULL;
6742 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6743 int ch, did_something = 0;
6745 while ((ch = getopt(argc, argv, "")) != -1) {
6746 switch (ch) {
6747 default:
6748 usage_integrate();
6749 /* NOTREACHED */
6753 argc -= optind;
6754 argv += optind;
6756 if (argc != 1)
6757 usage_integrate();
6758 branch_arg = argv[0];
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6761 "unveil", NULL) == -1)
6762 err(1, "pledge");
6764 cwd = getcwd(NULL, 0);
6765 if (cwd == NULL) {
6766 error = got_error_from_errno("getcwd");
6767 goto done;
6770 error = got_worktree_open(&worktree, cwd);
6771 if (error)
6772 goto done;
6774 error = check_rebase_or_histedit_in_progress(worktree);
6775 if (error)
6776 goto done;
6778 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6779 NULL);
6780 if (error != NULL)
6781 goto done;
6783 error = apply_unveil(got_repo_get_path(repo), 0,
6784 got_worktree_get_root_path(worktree));
6785 if (error)
6786 goto done;
6788 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6789 error = got_error_from_errno("asprintf");
6790 goto done;
6793 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6794 &base_branch_ref, worktree, refname, repo);
6795 if (error)
6796 goto done;
6798 refname = strdup(got_ref_get_name(branch_ref));
6799 if (refname == NULL) {
6800 error = got_error_from_errno("strdup");
6801 got_worktree_integrate_abort(worktree, fileindex, repo,
6802 branch_ref, base_branch_ref);
6803 goto done;
6805 base_refname = strdup(got_ref_get_name(base_branch_ref));
6806 if (base_refname == NULL) {
6807 error = got_error_from_errno("strdup");
6808 got_worktree_integrate_abort(worktree, fileindex, repo,
6809 branch_ref, base_branch_ref);
6810 goto done;
6813 error = got_ref_resolve(&commit_id, repo, branch_ref);
6814 if (error)
6815 goto done;
6817 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6818 if (error)
6819 goto done;
6821 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6822 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6823 "specified branch has already been integrated");
6824 got_worktree_integrate_abort(worktree, fileindex, repo,
6825 branch_ref, base_branch_ref);
6826 goto done;
6829 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6830 if (error) {
6831 if (error->code == GOT_ERR_ANCESTRY)
6832 error = got_error(GOT_ERR_REBASE_REQUIRED);
6833 got_worktree_integrate_abort(worktree, fileindex, repo,
6834 branch_ref, base_branch_ref);
6835 goto done;
6838 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6839 branch_ref, base_branch_ref, update_progress, &did_something,
6840 check_cancelled, NULL);
6841 if (error)
6842 goto done;
6844 printf("Integrated %s into %s\n", refname, base_refname);
6845 done:
6846 if (repo)
6847 got_repo_close(repo);
6848 if (worktree)
6849 got_worktree_close(worktree);
6850 free(cwd);
6851 free(base_commit_id);
6852 free(commit_id);
6853 free(refname);
6854 free(base_refname);
6855 return error;
6858 __dead static void
6859 usage_stage(void)
6861 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6862 "[file-path ...]\n",
6863 getprogname());
6864 exit(1);
6867 static const struct got_error *
6868 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6869 const char *path, struct got_object_id *blob_id,
6870 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6871 int dirfd, const char *de_name)
6873 const struct got_error *err = NULL;
6874 char *id_str = NULL;
6876 if (staged_status != GOT_STATUS_ADD &&
6877 staged_status != GOT_STATUS_MODIFY &&
6878 staged_status != GOT_STATUS_DELETE)
6879 return NULL;
6881 if (staged_status == GOT_STATUS_ADD ||
6882 staged_status == GOT_STATUS_MODIFY)
6883 err = got_object_id_str(&id_str, staged_blob_id);
6884 else
6885 err = got_object_id_str(&id_str, blob_id);
6886 if (err)
6887 return err;
6889 printf("%s %c %s\n", id_str, staged_status, path);
6890 free(id_str);
6891 return NULL;
6894 static const struct got_error *
6895 cmd_stage(int argc, char *argv[])
6897 const struct got_error *error = NULL;
6898 struct got_repository *repo = NULL;
6899 struct got_worktree *worktree = NULL;
6900 char *cwd = NULL;
6901 struct got_pathlist_head paths;
6902 struct got_pathlist_entry *pe;
6903 int ch, list_stage = 0, pflag = 0;
6904 FILE *patch_script_file = NULL;
6905 const char *patch_script_path = NULL;
6906 struct choose_patch_arg cpa;
6908 TAILQ_INIT(&paths);
6910 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6911 switch (ch) {
6912 case 'l':
6913 list_stage = 1;
6914 break;
6915 case 'p':
6916 pflag = 1;
6917 break;
6918 case 'F':
6919 patch_script_path = optarg;
6920 break;
6921 default:
6922 usage_stage();
6923 /* NOTREACHED */
6927 argc -= optind;
6928 argv += optind;
6930 #ifndef PROFILE
6931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6932 "unveil", NULL) == -1)
6933 err(1, "pledge");
6934 #endif
6935 if (list_stage && (pflag || patch_script_path))
6936 errx(1, "-l option cannot be used with other options");
6937 if (patch_script_path && !pflag)
6938 errx(1, "-F option can only be used together with -p option");
6940 cwd = getcwd(NULL, 0);
6941 if (cwd == NULL) {
6942 error = got_error_from_errno("getcwd");
6943 goto done;
6946 error = got_worktree_open(&worktree, cwd);
6947 if (error)
6948 goto done;
6950 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6951 NULL);
6952 if (error != NULL)
6953 goto done;
6955 if (patch_script_path) {
6956 patch_script_file = fopen(patch_script_path, "r");
6957 if (patch_script_file == NULL) {
6958 error = got_error_from_errno2("fopen",
6959 patch_script_path);
6960 goto done;
6963 error = apply_unveil(got_repo_get_path(repo), 0,
6964 got_worktree_get_root_path(worktree));
6965 if (error)
6966 goto done;
6968 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6969 if (error)
6970 goto done;
6972 if (list_stage)
6973 error = got_worktree_status(worktree, &paths, repo,
6974 print_stage, NULL, check_cancelled, NULL);
6975 else {
6976 cpa.patch_script_file = patch_script_file;
6977 cpa.action = "stage";
6978 error = got_worktree_stage(worktree, &paths,
6979 pflag ? NULL : print_status, NULL,
6980 pflag ? choose_patch : NULL, &cpa, repo);
6982 done:
6983 if (patch_script_file && fclose(patch_script_file) == EOF &&
6984 error == NULL)
6985 error = got_error_from_errno2("fclose", patch_script_path);
6986 if (repo)
6987 got_repo_close(repo);
6988 if (worktree)
6989 got_worktree_close(worktree);
6990 TAILQ_FOREACH(pe, &paths, entry)
6991 free((char *)pe->path);
6992 got_pathlist_free(&paths);
6993 free(cwd);
6994 return error;
6997 __dead static void
6998 usage_unstage(void)
7000 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7001 "[file-path ...]\n",
7002 getprogname());
7003 exit(1);
7007 static const struct got_error *
7008 cmd_unstage(int argc, char *argv[])
7010 const struct got_error *error = NULL;
7011 struct got_repository *repo = NULL;
7012 struct got_worktree *worktree = NULL;
7013 char *cwd = NULL;
7014 struct got_pathlist_head paths;
7015 struct got_pathlist_entry *pe;
7016 int ch, did_something = 0, pflag = 0;
7017 FILE *patch_script_file = NULL;
7018 const char *patch_script_path = NULL;
7019 struct choose_patch_arg cpa;
7021 TAILQ_INIT(&paths);
7023 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7024 switch (ch) {
7025 case 'p':
7026 pflag = 1;
7027 break;
7028 case 'F':
7029 patch_script_path = optarg;
7030 break;
7031 default:
7032 usage_unstage();
7033 /* NOTREACHED */
7037 argc -= optind;
7038 argv += optind;
7040 #ifndef PROFILE
7041 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7042 "unveil", NULL) == -1)
7043 err(1, "pledge");
7044 #endif
7045 if (patch_script_path && !pflag)
7046 errx(1, "-F option can only be used together with -p option");
7048 cwd = getcwd(NULL, 0);
7049 if (cwd == NULL) {
7050 error = got_error_from_errno("getcwd");
7051 goto done;
7054 error = got_worktree_open(&worktree, cwd);
7055 if (error)
7056 goto done;
7058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7059 NULL);
7060 if (error != NULL)
7061 goto done;
7063 if (patch_script_path) {
7064 patch_script_file = fopen(patch_script_path, "r");
7065 if (patch_script_file == NULL) {
7066 error = got_error_from_errno2("fopen",
7067 patch_script_path);
7068 goto done;
7072 error = apply_unveil(got_repo_get_path(repo), 0,
7073 got_worktree_get_root_path(worktree));
7074 if (error)
7075 goto done;
7077 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7078 if (error)
7079 goto done;
7081 cpa.patch_script_file = patch_script_file;
7082 cpa.action = "unstage";
7083 error = got_worktree_unstage(worktree, &paths, update_progress,
7084 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7085 done:
7086 if (patch_script_file && fclose(patch_script_file) == EOF &&
7087 error == NULL)
7088 error = got_error_from_errno2("fclose", patch_script_path);
7089 if (repo)
7090 got_repo_close(repo);
7091 if (worktree)
7092 got_worktree_close(worktree);
7093 TAILQ_FOREACH(pe, &paths, entry)
7094 free((char *)pe->path);
7095 got_pathlist_free(&paths);
7096 free(cwd);
7097 return error;
7100 __dead static void
7101 usage_cat(void)
7103 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7104 "arg1 [arg2 ...]\n", getprogname());
7105 exit(1);
7108 static const struct got_error *
7109 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7111 const struct got_error *err;
7112 struct got_blob_object *blob;
7114 err = got_object_open_as_blob(&blob, repo, id, 8192);
7115 if (err)
7116 return err;
7118 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7119 got_object_blob_close(blob);
7120 return err;
7123 static const struct got_error *
7124 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7126 const struct got_error *err;
7127 struct got_tree_object *tree;
7128 int nentries, i;
7130 err = got_object_open_as_tree(&tree, repo, id);
7131 if (err)
7132 return err;
7134 nentries = got_object_tree_get_nentries(tree);
7135 for (i = 0; i < nentries; i++) {
7136 struct got_tree_entry *te;
7137 char *id_str;
7138 if (sigint_received || sigpipe_received)
7139 break;
7140 te = got_object_tree_get_entry(tree, i);
7141 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7142 if (err)
7143 break;
7144 fprintf(outfile, "%s %.7o %s\n", id_str,
7145 got_tree_entry_get_mode(te),
7146 got_tree_entry_get_name(te));
7147 free(id_str);
7150 got_object_tree_close(tree);
7151 return err;
7154 static const struct got_error *
7155 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7157 const struct got_error *err;
7158 struct got_commit_object *commit;
7159 const struct got_object_id_queue *parent_ids;
7160 struct got_object_qid *pid;
7161 char *id_str = NULL;
7162 const char *logmsg = NULL;
7164 err = got_object_open_as_commit(&commit, repo, id);
7165 if (err)
7166 return err;
7168 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7169 if (err)
7170 goto done;
7172 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7173 parent_ids = got_object_commit_get_parent_ids(commit);
7174 fprintf(outfile, "numparents %d\n",
7175 got_object_commit_get_nparents(commit));
7176 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7177 char *pid_str;
7178 err = got_object_id_str(&pid_str, pid->id);
7179 if (err)
7180 goto done;
7181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7182 free(pid_str);
7184 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7185 got_object_commit_get_author(commit),
7186 got_object_commit_get_author_time(commit));
7188 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7189 got_object_commit_get_author(commit),
7190 got_object_commit_get_committer_time(commit));
7192 logmsg = got_object_commit_get_logmsg_raw(commit);
7193 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7194 fprintf(outfile, "%s", logmsg);
7195 done:
7196 free(id_str);
7197 got_object_commit_close(commit);
7198 return err;
7201 static const struct got_error *
7202 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7204 const struct got_error *err;
7205 struct got_tag_object *tag;
7206 char *id_str = NULL;
7207 const char *tagmsg = NULL;
7209 err = got_object_open_as_tag(&tag, repo, id);
7210 if (err)
7211 return err;
7213 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7214 if (err)
7215 goto done;
7217 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7219 switch (got_object_tag_get_object_type(tag)) {
7220 case GOT_OBJ_TYPE_BLOB:
7221 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7222 GOT_OBJ_LABEL_BLOB);
7223 break;
7224 case GOT_OBJ_TYPE_TREE:
7225 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7226 GOT_OBJ_LABEL_TREE);
7227 break;
7228 case GOT_OBJ_TYPE_COMMIT:
7229 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7230 GOT_OBJ_LABEL_COMMIT);
7231 break;
7232 case GOT_OBJ_TYPE_TAG:
7233 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7234 GOT_OBJ_LABEL_TAG);
7235 break;
7236 default:
7237 break;
7240 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7241 got_object_tag_get_name(tag));
7243 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7244 got_object_tag_get_tagger(tag),
7245 got_object_tag_get_tagger_time(tag));
7247 tagmsg = got_object_tag_get_message(tag);
7248 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7249 fprintf(outfile, "%s", tagmsg);
7250 done:
7251 free(id_str);
7252 got_object_tag_close(tag);
7253 return err;
7256 static const struct got_error *
7257 cmd_cat(int argc, char *argv[])
7259 const struct got_error *error;
7260 struct got_repository *repo = NULL;
7261 struct got_worktree *worktree = NULL;
7262 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7263 const char *commit_id_str = NULL;
7264 struct got_object_id *id = NULL, *commit_id = NULL;
7265 int ch, obj_type, i, force_path = 0;
7267 #ifndef PROFILE
7268 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7269 NULL) == -1)
7270 err(1, "pledge");
7271 #endif
7273 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7274 switch (ch) {
7275 case 'c':
7276 commit_id_str = optarg;
7277 break;
7278 case 'r':
7279 repo_path = realpath(optarg, NULL);
7280 if (repo_path == NULL)
7281 return got_error_from_errno2("realpath",
7282 optarg);
7283 got_path_strip_trailing_slashes(repo_path);
7284 break;
7285 case 'P':
7286 force_path = 1;
7287 break;
7288 default:
7289 usage_cat();
7290 /* NOTREACHED */
7294 argc -= optind;
7295 argv += optind;
7297 cwd = getcwd(NULL, 0);
7298 if (cwd == NULL) {
7299 error = got_error_from_errno("getcwd");
7300 goto done;
7302 error = got_worktree_open(&worktree, cwd);
7303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7304 goto done;
7305 if (worktree) {
7306 if (repo_path == NULL) {
7307 repo_path = strdup(
7308 got_worktree_get_repo_path(worktree));
7309 if (repo_path == NULL) {
7310 error = got_error_from_errno("strdup");
7311 goto done;
7316 if (repo_path == NULL) {
7317 repo_path = getcwd(NULL, 0);
7318 if (repo_path == NULL)
7319 return got_error_from_errno("getcwd");
7322 error = got_repo_open(&repo, repo_path, NULL);
7323 free(repo_path);
7324 if (error != NULL)
7325 goto done;
7327 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7328 if (error)
7329 goto done;
7331 if (commit_id_str == NULL)
7332 commit_id_str = GOT_REF_HEAD;
7333 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7334 if (error)
7335 goto done;
7337 for (i = 0; i < argc; i++) {
7338 if (force_path) {
7339 error = got_object_id_by_path(&id, repo, commit_id,
7340 argv[i]);
7341 if (error)
7342 break;
7343 } else {
7344 error = match_object_id(&id, &label, argv[i],
7345 GOT_OBJ_TYPE_ANY, 0, repo);
7346 if (error) {
7347 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7348 error->code != GOT_ERR_NOT_REF)
7349 break;
7350 error = got_object_id_by_path(&id, repo,
7351 commit_id, argv[i]);
7352 if (error)
7353 break;
7357 error = got_object_get_type(&obj_type, repo, id);
7358 if (error)
7359 break;
7361 switch (obj_type) {
7362 case GOT_OBJ_TYPE_BLOB:
7363 error = cat_blob(id, repo, stdout);
7364 break;
7365 case GOT_OBJ_TYPE_TREE:
7366 error = cat_tree(id, repo, stdout);
7367 break;
7368 case GOT_OBJ_TYPE_COMMIT:
7369 error = cat_commit(id, repo, stdout);
7370 break;
7371 case GOT_OBJ_TYPE_TAG:
7372 error = cat_tag(id, repo, stdout);
7373 break;
7374 default:
7375 error = got_error(GOT_ERR_OBJ_TYPE);
7376 break;
7378 if (error)
7379 break;
7380 free(label);
7381 label = NULL;
7382 free(id);
7383 id = NULL;
7386 done:
7387 free(label);
7388 free(id);
7389 free(commit_id);
7390 if (worktree)
7391 got_worktree_close(worktree);
7392 if (repo) {
7393 const struct got_error *repo_error;
7394 repo_error = got_repo_close(repo);
7395 if (error == NULL)
7396 error = repo_error;
7398 return error;