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 void
808 show_worktree_base_ref_warning(void)
810 fprintf(stderr, "%s: warning: could not create a reference "
811 "to the work tree's base commit; the commit could be "
812 "garbage-collected by Git; making the repository "
813 "writable and running 'got update' will prevent this\n",
814 getprogname());
817 struct got_checkout_progress_arg {
818 const char *worktree_path;
819 int had_base_commit_ref_error;
820 };
822 static const struct got_error *
823 checkout_progress(void *arg, unsigned char status, const char *path)
825 struct got_checkout_progress_arg *a = arg;
827 /* Base commit bump happens silently. */
828 if (status == GOT_STATUS_BUMP_BASE)
829 return NULL;
831 if (status == GOT_STATUS_BASE_REF_ERR) {
832 a->had_base_commit_ref_error = 1;
833 return NULL;
836 while (path[0] == '/')
837 path++;
839 printf("%c %s/%s\n", status, a->worktree_path, path);
840 return NULL;
843 static const struct got_error *
844 check_cancelled(void *arg)
846 if (sigint_received || sigpipe_received)
847 return got_error(GOT_ERR_CANCELLED);
848 return NULL;
851 static const struct got_error *
852 check_linear_ancestry(struct got_object_id *commit_id,
853 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 struct got_repository *repo)
856 const struct got_error *err = NULL;
857 struct got_object_id *yca_id;
859 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 commit_id, base_commit_id, repo, check_cancelled, NULL);
861 if (err)
862 return err;
864 if (yca_id == NULL)
865 return got_error(GOT_ERR_ANCESTRY);
867 /*
868 * Require a straight line of history between the target commit
869 * and the work tree's base commit.
871 * Non-linear situations such as this require a rebase:
873 * (commit) D F (base_commit)
874 * \ /
875 * C E
876 * \ /
877 * B (yca)
878 * |
879 * A
881 * 'got update' only handles linear cases:
882 * Update forwards in time: A (base/yca) - B - C - D (commit)
883 * Update backwards in time: D (base) - C - B - A (commit/yca)
884 */
885 if (allow_forwards_in_time_only) {
886 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 return got_error(GOT_ERR_ANCESTRY);
888 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
892 free(yca_id);
893 return NULL;
896 static const struct got_error *
897 check_same_branch(struct got_object_id *commit_id,
898 struct got_reference *head_ref, struct got_object_id *yca_id,
899 struct got_repository *repo)
901 const struct got_error *err = NULL;
902 struct got_commit_graph *graph = NULL;
903 struct got_object_id *head_commit_id = NULL;
904 int is_same_branch = 0;
906 err = got_ref_resolve(&head_commit_id, repo, head_ref);
907 if (err)
908 goto done;
910 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
911 is_same_branch = 1;
912 goto done;
914 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
915 is_same_branch = 1;
916 goto done;
919 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
920 if (err)
921 goto done;
923 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 check_cancelled, NULL);
925 if (err)
926 goto done;
928 for (;;) {
929 struct got_object_id *id;
930 err = got_commit_graph_iter_next(&id, graph, repo,
931 check_cancelled, NULL);
932 if (err) {
933 if (err->code == GOT_ERR_ITER_COMPLETED)
934 err = NULL;
935 break;
938 if (id) {
939 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
940 break;
941 if (got_object_id_cmp(id, commit_id) == 0) {
942 is_same_branch = 1;
943 break;
947 done:
948 if (graph)
949 got_commit_graph_close(graph);
950 free(head_commit_id);
951 if (!err && !is_same_branch)
952 err = got_error(GOT_ERR_ANCESTRY);
953 return err;
956 static const struct got_error *
957 resolve_commit_arg(struct got_object_id **commit_id,
958 const char *commit_id_arg, struct got_repository *repo)
960 const struct got_error *err;
961 struct got_reference *ref;
962 struct got_tag_object *tag;
964 err = got_repo_object_match_tag(&tag, commit_id_arg,
965 GOT_OBJ_TYPE_COMMIT, repo);
966 if (err == NULL) {
967 *commit_id = got_object_id_dup(
968 got_object_tag_get_object_id(tag));
969 if (*commit_id == NULL)
970 err = got_error_from_errno("got_object_id_dup");
971 got_object_tag_close(tag);
972 return err;
973 } else if (err->code != GOT_ERR_NO_OBJ)
974 return err;
976 err = got_ref_open(&ref, repo, commit_id_arg, 0);
977 if (err == NULL) {
978 err = got_ref_resolve(commit_id, repo, ref);
979 got_ref_close(ref);
980 } else {
981 if (err->code != GOT_ERR_NOT_REF)
982 return err;
983 err = got_repo_match_object_id_prefix(commit_id,
984 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
986 return err;
989 static const struct got_error *
990 cmd_checkout(int argc, char *argv[])
992 const struct got_error *error = NULL;
993 struct got_repository *repo = NULL;
994 struct got_reference *head_ref = NULL;
995 struct got_worktree *worktree = NULL;
996 char *repo_path = NULL;
997 char *worktree_path = NULL;
998 const char *path_prefix = "";
999 const char *branch_name = GOT_REF_HEAD;
1000 char *commit_id_str = NULL;
1001 int ch, same_path_prefix;
1002 struct got_pathlist_head paths;
1003 struct got_checkout_progress_arg cpa;
1005 TAILQ_INIT(&paths);
1007 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1008 switch (ch) {
1009 case 'b':
1010 branch_name = optarg;
1011 break;
1012 case 'c':
1013 commit_id_str = strdup(optarg);
1014 if (commit_id_str == NULL)
1015 return got_error_from_errno("strdup");
1016 break;
1017 case 'p':
1018 path_prefix = optarg;
1019 break;
1020 default:
1021 usage_checkout();
1022 /* NOTREACHED */
1026 argc -= optind;
1027 argv += optind;
1029 #ifndef PROFILE
1030 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1031 "unveil", NULL) == -1)
1032 err(1, "pledge");
1033 #endif
1034 if (argc == 1) {
1035 char *cwd, *base, *dotgit;
1036 repo_path = realpath(argv[0], NULL);
1037 if (repo_path == NULL)
1038 return got_error_from_errno2("realpath", argv[0]);
1039 cwd = getcwd(NULL, 0);
1040 if (cwd == NULL) {
1041 error = got_error_from_errno("getcwd");
1042 goto done;
1044 if (path_prefix[0]) {
1045 base = basename(path_prefix);
1046 if (base == NULL) {
1047 error = got_error_from_errno2("basename",
1048 path_prefix);
1049 goto done;
1051 } else {
1052 base = basename(repo_path);
1053 if (base == NULL) {
1054 error = got_error_from_errno2("basename",
1055 repo_path);
1056 goto done;
1059 dotgit = strstr(base, ".git");
1060 if (dotgit)
1061 *dotgit = '\0';
1062 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1063 error = got_error_from_errno("asprintf");
1064 free(cwd);
1065 goto done;
1067 free(cwd);
1068 } else if (argc == 2) {
1069 repo_path = realpath(argv[0], NULL);
1070 if (repo_path == NULL) {
1071 error = got_error_from_errno2("realpath", argv[0]);
1072 goto done;
1074 worktree_path = realpath(argv[1], NULL);
1075 if (worktree_path == NULL) {
1076 if (errno != ENOENT) {
1077 error = got_error_from_errno2("realpath",
1078 argv[1]);
1079 goto done;
1081 worktree_path = strdup(argv[1]);
1082 if (worktree_path == NULL) {
1083 error = got_error_from_errno("strdup");
1084 goto done;
1087 } else
1088 usage_checkout();
1090 got_path_strip_trailing_slashes(repo_path);
1091 got_path_strip_trailing_slashes(worktree_path);
1093 error = got_repo_open(&repo, repo_path, NULL);
1094 if (error != NULL)
1095 goto done;
1097 /* Pre-create work tree path for unveil(2) */
1098 error = got_path_mkdir(worktree_path);
1099 if (error) {
1100 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1101 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1102 goto done;
1103 if (!got_path_dir_is_empty(worktree_path)) {
1104 error = got_error_path(worktree_path,
1105 GOT_ERR_DIR_NOT_EMPTY);
1106 goto done;
1110 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1111 if (error)
1112 goto done;
1114 error = got_ref_open(&head_ref, repo, branch_name, 0);
1115 if (error != NULL)
1116 goto done;
1118 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1119 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1120 goto done;
1122 error = got_worktree_open(&worktree, worktree_path);
1123 if (error != NULL)
1124 goto done;
1126 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1127 path_prefix);
1128 if (error != NULL)
1129 goto done;
1130 if (!same_path_prefix) {
1131 error = got_error(GOT_ERR_PATH_PREFIX);
1132 goto done;
1135 if (commit_id_str) {
1136 struct got_object_id *commit_id;
1137 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1138 if (error)
1139 goto done;
1140 error = check_linear_ancestry(commit_id,
1141 got_worktree_get_base_commit_id(worktree), 0, repo);
1142 if (error != NULL) {
1143 free(commit_id);
1144 goto done;
1146 error = check_same_branch(commit_id, head_ref, NULL, repo);
1147 if (error)
1148 goto done;
1149 error = got_worktree_set_base_commit_id(worktree, repo,
1150 commit_id);
1151 free(commit_id);
1152 if (error)
1153 goto done;
1156 error = got_pathlist_append(&paths, "", NULL);
1157 if (error)
1158 goto done;
1159 cpa.worktree_path = worktree_path;
1160 cpa.had_base_commit_ref_error = 0;
1161 error = got_worktree_checkout_files(worktree, &paths, repo,
1162 checkout_progress, &cpa, check_cancelled, NULL);
1163 if (error != NULL)
1164 goto done;
1166 printf("Now shut up and hack\n");
1167 if (cpa.had_base_commit_ref_error)
1168 show_worktree_base_ref_warning();
1170 done:
1171 got_pathlist_free(&paths);
1172 free(commit_id_str);
1173 free(repo_path);
1174 free(worktree_path);
1175 return error;
1178 __dead static void
1179 usage_update(void)
1181 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1182 getprogname());
1183 exit(1);
1186 static const struct got_error *
1187 update_progress(void *arg, unsigned char status, const char *path)
1189 int *did_something = arg;
1191 if (status == GOT_STATUS_EXISTS ||
1192 status == GOT_STATUS_BASE_REF_ERR)
1193 return NULL;
1195 *did_something = 1;
1197 /* Base commit bump happens silently. */
1198 if (status == GOT_STATUS_BUMP_BASE)
1199 return NULL;
1201 while (path[0] == '/')
1202 path++;
1203 printf("%c %s\n", status, path);
1204 return NULL;
1207 static const struct got_error *
1208 switch_head_ref(struct got_reference *head_ref,
1209 struct got_object_id *commit_id, struct got_worktree *worktree,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1213 char *base_id_str;
1214 int ref_has_moved = 0;
1216 /* Trivial case: switching between two different references. */
1217 if (strcmp(got_ref_get_name(head_ref),
1218 got_worktree_get_head_ref_name(worktree)) != 0) {
1219 printf("Switching work tree from %s to %s\n",
1220 got_worktree_get_head_ref_name(worktree),
1221 got_ref_get_name(head_ref));
1222 return got_worktree_set_head_ref(worktree, head_ref);
1225 err = check_linear_ancestry(commit_id,
1226 got_worktree_get_base_commit_id(worktree), 0, repo);
1227 if (err) {
1228 if (err->code != GOT_ERR_ANCESTRY)
1229 return err;
1230 ref_has_moved = 1;
1232 if (!ref_has_moved)
1233 return NULL;
1235 /* Switching to a rebased branch with the same reference name. */
1236 err = got_object_id_str(&base_id_str,
1237 got_worktree_get_base_commit_id(worktree));
1238 if (err)
1239 return err;
1240 printf("Reference %s now points at a different branch\n",
1241 got_worktree_get_head_ref_name(worktree));
1242 printf("Switching work tree from %s to %s\n", base_id_str,
1243 got_worktree_get_head_ref_name(worktree));
1244 return NULL;
1247 static const struct got_error *
1248 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1250 const struct got_error *err;
1251 int in_progress;
1253 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1254 if (err)
1255 return err;
1256 if (in_progress)
1257 return got_error(GOT_ERR_REBASING);
1259 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1260 if (err)
1261 return err;
1262 if (in_progress)
1263 return got_error(GOT_ERR_HISTEDIT_BUSY);
1265 return NULL;
1268 static const struct got_error *
1269 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1270 char *argv[], struct got_worktree *worktree)
1272 const struct got_error *err = NULL;
1273 char *path;
1274 int i;
1276 if (argc == 0) {
1277 path = strdup("");
1278 if (path == NULL)
1279 return got_error_from_errno("strdup");
1280 return got_pathlist_append(paths, path, NULL);
1283 for (i = 0; i < argc; i++) {
1284 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1285 if (err)
1286 break;
1287 err = got_pathlist_append(paths, path, NULL);
1288 if (err) {
1289 free(path);
1290 break;
1294 return err;
1297 static const struct got_error *
1298 cmd_update(int argc, char *argv[])
1300 const struct got_error *error = NULL;
1301 struct got_repository *repo = NULL;
1302 struct got_worktree *worktree = NULL;
1303 char *worktree_path = NULL;
1304 struct got_object_id *commit_id = NULL;
1305 char *commit_id_str = NULL;
1306 const char *branch_name = NULL;
1307 struct got_reference *head_ref = NULL;
1308 struct got_pathlist_head paths;
1309 struct got_pathlist_entry *pe;
1310 int ch, did_something = 0;
1312 TAILQ_INIT(&paths);
1314 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1315 switch (ch) {
1316 case 'b':
1317 branch_name = optarg;
1318 break;
1319 case 'c':
1320 commit_id_str = strdup(optarg);
1321 if (commit_id_str == NULL)
1322 return got_error_from_errno("strdup");
1323 break;
1324 default:
1325 usage_update();
1326 /* NOTREACHED */
1330 argc -= optind;
1331 argv += optind;
1333 #ifndef PROFILE
1334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1335 "unveil", NULL) == -1)
1336 err(1, "pledge");
1337 #endif
1338 worktree_path = getcwd(NULL, 0);
1339 if (worktree_path == NULL) {
1340 error = got_error_from_errno("getcwd");
1341 goto done;
1343 error = got_worktree_open(&worktree, worktree_path);
1344 if (error)
1345 goto done;
1347 error = check_rebase_or_histedit_in_progress(worktree);
1348 if (error)
1349 goto done;
1351 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1352 NULL);
1353 if (error != NULL)
1354 goto done;
1356 error = apply_unveil(got_repo_get_path(repo), 0,
1357 got_worktree_get_root_path(worktree));
1358 if (error)
1359 goto done;
1361 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1362 if (error)
1363 goto done;
1365 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1366 got_worktree_get_head_ref_name(worktree), 0);
1367 if (error != NULL)
1368 goto done;
1369 if (commit_id_str == NULL) {
1370 error = got_ref_resolve(&commit_id, repo, head_ref);
1371 if (error != NULL)
1372 goto done;
1373 error = got_object_id_str(&commit_id_str, commit_id);
1374 if (error != NULL)
1375 goto done;
1376 } else {
1377 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1378 free(commit_id_str);
1379 commit_id_str = NULL;
1380 if (error)
1381 goto done;
1382 error = got_object_id_str(&commit_id_str, commit_id);
1383 if (error)
1384 goto done;
1387 if (branch_name) {
1388 struct got_object_id *head_commit_id;
1389 TAILQ_FOREACH(pe, &paths, entry) {
1390 if (pe->path_len == 0)
1391 continue;
1392 error = got_error_msg(GOT_ERR_BAD_PATH,
1393 "switching between branches requires that "
1394 "the entire work tree gets updated");
1395 goto done;
1397 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1398 if (error)
1399 goto done;
1400 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1401 repo);
1402 free(head_commit_id);
1403 if (error != NULL)
1404 goto done;
1405 error = check_same_branch(commit_id, head_ref, NULL, repo);
1406 if (error)
1407 goto done;
1408 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1409 if (error)
1410 goto done;
1411 } else {
1412 error = check_linear_ancestry(commit_id,
1413 got_worktree_get_base_commit_id(worktree), 0, repo);
1414 if (error != NULL) {
1415 if (error->code == GOT_ERR_ANCESTRY)
1416 error = got_error(GOT_ERR_BRANCH_MOVED);
1417 goto done;
1419 error = check_same_branch(commit_id, head_ref, NULL, repo);
1420 if (error)
1421 goto done;
1424 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1425 commit_id) != 0) {
1426 error = got_worktree_set_base_commit_id(worktree, repo,
1427 commit_id);
1428 if (error)
1429 goto done;
1432 error = got_worktree_checkout_files(worktree, &paths, repo,
1433 update_progress, &did_something, check_cancelled, NULL);
1434 if (error != NULL)
1435 goto done;
1437 if (did_something)
1438 printf("Updated to commit %s\n", commit_id_str);
1439 else
1440 printf("Already up-to-date\n");
1441 done:
1442 free(worktree_path);
1443 TAILQ_FOREACH(pe, &paths, entry)
1444 free((char *)pe->path);
1445 got_pathlist_free(&paths);
1446 free(commit_id);
1447 free(commit_id_str);
1448 return error;
1451 static const struct got_error *
1452 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1453 const char *path, int diff_context, int ignore_whitespace,
1454 struct got_repository *repo)
1456 const struct got_error *err = NULL;
1457 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1459 if (blob_id1) {
1460 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1461 if (err)
1462 goto done;
1465 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1466 if (err)
1467 goto done;
1469 while (path[0] == '/')
1470 path++;
1471 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1472 ignore_whitespace, stdout);
1473 done:
1474 if (blob1)
1475 got_object_blob_close(blob1);
1476 got_object_blob_close(blob2);
1477 return err;
1480 static const struct got_error *
1481 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1482 const char *path, int diff_context, int ignore_whitespace,
1483 struct got_repository *repo)
1485 const struct got_error *err = NULL;
1486 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1487 struct got_diff_blob_output_unidiff_arg arg;
1489 if (tree_id1) {
1490 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1491 if (err)
1492 goto done;
1495 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1496 if (err)
1497 goto done;
1499 arg.diff_context = diff_context;
1500 arg.ignore_whitespace = ignore_whitespace;
1501 arg.outfile = stdout;
1502 while (path[0] == '/')
1503 path++;
1504 err = got_diff_tree(tree1, tree2, path, path, repo,
1505 got_diff_blob_output_unidiff, &arg, 1);
1506 done:
1507 if (tree1)
1508 got_object_tree_close(tree1);
1509 if (tree2)
1510 got_object_tree_close(tree2);
1511 return err;
1514 static const struct got_error *
1515 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1516 const char *path, int diff_context, struct got_repository *repo)
1518 const struct got_error *err = NULL;
1519 struct got_commit_object *pcommit = NULL;
1520 char *id_str1 = NULL, *id_str2 = NULL;
1521 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1522 struct got_object_qid *qid;
1524 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1525 if (qid != NULL) {
1526 err = got_object_open_as_commit(&pcommit, repo,
1527 qid->id);
1528 if (err)
1529 return err;
1532 if (path && path[0] != '\0') {
1533 int obj_type;
1534 err = got_object_id_by_path(&obj_id2, repo, id, path);
1535 if (err)
1536 goto done;
1537 err = got_object_id_str(&id_str2, obj_id2);
1538 if (err) {
1539 free(obj_id2);
1540 goto done;
1542 if (pcommit) {
1543 err = got_object_id_by_path(&obj_id1, repo,
1544 qid->id, path);
1545 if (err) {
1546 free(obj_id2);
1547 goto done;
1549 err = got_object_id_str(&id_str1, obj_id1);
1550 if (err) {
1551 free(obj_id2);
1552 goto done;
1555 err = got_object_get_type(&obj_type, repo, obj_id2);
1556 if (err) {
1557 free(obj_id2);
1558 goto done;
1560 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 switch (obj_type) {
1562 case GOT_OBJ_TYPE_BLOB:
1563 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1564 0, repo);
1565 break;
1566 case GOT_OBJ_TYPE_TREE:
1567 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1568 0, repo);
1569 break;
1570 default:
1571 err = got_error(GOT_ERR_OBJ_TYPE);
1572 break;
1574 free(obj_id1);
1575 free(obj_id2);
1576 } else {
1577 obj_id2 = got_object_commit_get_tree_id(commit);
1578 err = got_object_id_str(&id_str2, obj_id2);
1579 if (err)
1580 goto done;
1581 obj_id1 = got_object_commit_get_tree_id(pcommit);
1582 err = got_object_id_str(&id_str1, obj_id1);
1583 if (err)
1584 goto done;
1585 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1586 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1589 done:
1590 free(id_str1);
1591 free(id_str2);
1592 if (pcommit)
1593 got_object_commit_close(pcommit);
1594 return err;
1597 static char *
1598 get_datestr(time_t *time, char *datebuf)
1600 struct tm mytm, *tm;
1601 char *p, *s;
1603 tm = gmtime_r(time, &mytm);
1604 if (tm == NULL)
1605 return NULL;
1606 s = asctime_r(tm, datebuf);
1607 if (s == NULL)
1608 return NULL;
1609 p = strchr(s, '\n');
1610 if (p)
1611 *p = '\0';
1612 return s;
1615 static const struct got_error *
1616 match_logmsg(int *have_match, struct got_object_id *id,
1617 struct got_commit_object *commit, regex_t *regex)
1619 const struct got_error *err = NULL;
1620 regmatch_t regmatch;
1621 char *id_str = NULL, *logmsg = NULL;
1623 *have_match = 0;
1625 err = got_object_id_str(&id_str, id);
1626 if (err)
1627 return err;
1629 err = got_object_commit_get_logmsg(&logmsg, commit);
1630 if (err)
1631 goto done;
1633 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1634 *have_match = 1;
1635 done:
1636 free(id_str);
1637 free(logmsg);
1638 return err;
1641 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1643 static const struct got_error *
1644 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1645 struct got_repository *repo, const char *path, int show_patch,
1646 int diff_context, struct got_reflist_head *refs)
1648 const struct got_error *err = NULL;
1649 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1650 char datebuf[26];
1651 time_t committer_time;
1652 const char *author, *committer;
1653 char *refs_str = NULL;
1654 struct got_reflist_entry *re;
1656 SIMPLEQ_FOREACH(re, refs, entry) {
1657 char *s;
1658 const char *name;
1659 struct got_tag_object *tag = NULL;
1660 int cmp;
1662 name = got_ref_get_name(re->ref);
1663 if (strcmp(name, GOT_REF_HEAD) == 0)
1664 continue;
1665 if (strncmp(name, "refs/", 5) == 0)
1666 name += 5;
1667 if (strncmp(name, "got/", 4) == 0)
1668 continue;
1669 if (strncmp(name, "heads/", 6) == 0)
1670 name += 6;
1671 if (strncmp(name, "remotes/", 8) == 0)
1672 name += 8;
1673 if (strncmp(name, "tags/", 5) == 0) {
1674 err = got_object_open_as_tag(&tag, repo, re->id);
1675 if (err) {
1676 if (err->code != GOT_ERR_OBJ_TYPE)
1677 return err;
1678 /* Ref points at something other than a tag. */
1679 err = NULL;
1680 tag = NULL;
1683 cmp = got_object_id_cmp(tag ?
1684 got_object_tag_get_object_id(tag) : re->id, id);
1685 if (tag)
1686 got_object_tag_close(tag);
1687 if (cmp != 0)
1688 continue;
1689 s = refs_str;
1690 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1691 name) == -1) {
1692 err = got_error_from_errno("asprintf");
1693 free(s);
1694 return err;
1696 free(s);
1698 err = got_object_id_str(&id_str, id);
1699 if (err)
1700 return err;
1702 printf(GOT_COMMIT_SEP_STR);
1703 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1704 refs_str ? refs_str : "", refs_str ? ")" : "");
1705 free(id_str);
1706 id_str = NULL;
1707 free(refs_str);
1708 refs_str = NULL;
1709 printf("from: %s\n", got_object_commit_get_author(commit));
1710 committer_time = got_object_commit_get_committer_time(commit);
1711 datestr = get_datestr(&committer_time, datebuf);
1712 if (datestr)
1713 printf("date: %s UTC\n", datestr);
1714 author = got_object_commit_get_author(commit);
1715 committer = got_object_commit_get_committer(commit);
1716 if (strcmp(author, committer) != 0)
1717 printf("via: %s\n", committer);
1718 if (got_object_commit_get_nparents(commit) > 1) {
1719 const struct got_object_id_queue *parent_ids;
1720 struct got_object_qid *qid;
1721 int n = 1;
1722 parent_ids = got_object_commit_get_parent_ids(commit);
1723 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1724 err = got_object_id_str(&id_str, qid->id);
1725 if (err)
1726 return err;
1727 printf("parent %d: %s\n", n++, id_str);
1728 free(id_str);
1732 err = got_object_commit_get_logmsg(&logmsg0, commit);
1733 if (err)
1734 return err;
1736 logmsg = logmsg0;
1737 do {
1738 line = strsep(&logmsg, "\n");
1739 if (line)
1740 printf(" %s\n", line);
1741 } while (line);
1742 free(logmsg0);
1744 if (show_patch) {
1745 err = print_patch(commit, id, path, diff_context, repo);
1746 if (err == 0)
1747 printf("\n");
1750 if (fflush(stdout) != 0 && err == NULL)
1751 err = got_error_from_errno("fflush");
1752 return err;
1755 static const struct got_error *
1756 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1757 const char *path, int show_patch, const char *search_pattern,
1758 int diff_context, int limit, int first_parent_traversal,
1759 struct got_reflist_head *refs)
1761 const struct got_error *err;
1762 struct got_commit_graph *graph;
1763 regex_t regex;
1764 int have_match;
1766 if (search_pattern &&
1767 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1768 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1770 err = got_commit_graph_open(&graph, root_id, path,
1771 first_parent_traversal, repo);
1772 if (err)
1773 return err;
1774 err = got_commit_graph_iter_start(graph, root_id, repo,
1775 check_cancelled, NULL);
1776 if (err)
1777 goto done;
1778 for (;;) {
1779 struct got_commit_object *commit;
1780 struct got_object_id *id;
1782 if (sigint_received || sigpipe_received)
1783 break;
1785 err = got_commit_graph_iter_next(&id, graph, repo,
1786 check_cancelled, NULL);
1787 if (err) {
1788 if (err->code == GOT_ERR_ITER_COMPLETED)
1789 err = NULL;
1790 break;
1792 if (id == NULL)
1793 break;
1795 err = got_object_open_as_commit(&commit, repo, id);
1796 if (err)
1797 break;
1799 if (search_pattern) {
1800 err = match_logmsg(&have_match, id, commit, &regex);
1801 if (err) {
1802 got_object_commit_close(commit);
1803 break;
1805 if (have_match == 0) {
1806 got_object_commit_close(commit);
1807 continue;
1811 err = print_commit(commit, id, repo, path, show_patch,
1812 diff_context, refs);
1813 got_object_commit_close(commit);
1814 if (err || (limit && --limit == 0))
1815 break;
1817 done:
1818 if (search_pattern)
1819 regfree(&regex);
1820 got_commit_graph_close(graph);
1821 return err;
1824 __dead static void
1825 usage_log(void)
1827 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1828 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1829 exit(1);
1832 static int
1833 get_default_log_limit(void)
1835 const char *got_default_log_limit;
1836 long long n;
1837 const char *errstr;
1839 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1840 if (got_default_log_limit == NULL)
1841 return 0;
1842 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1843 if (errstr != NULL)
1844 return 0;
1845 return n;
1848 static const struct got_error *
1849 cmd_log(int argc, char *argv[])
1851 const struct got_error *error;
1852 struct got_repository *repo = NULL;
1853 struct got_worktree *worktree = NULL;
1854 struct got_commit_object *commit = NULL;
1855 struct got_object_id *id = NULL;
1856 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1857 const char *start_commit = NULL, *search_pattern = NULL;
1858 int diff_context = -1, ch;
1859 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1860 const char *errstr;
1861 struct got_reflist_head refs;
1863 SIMPLEQ_INIT(&refs);
1865 #ifndef PROFILE
1866 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1867 NULL)
1868 == -1)
1869 err(1, "pledge");
1870 #endif
1872 limit = get_default_log_limit();
1874 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1875 switch (ch) {
1876 case 'p':
1877 show_patch = 1;
1878 break;
1879 case 'c':
1880 start_commit = optarg;
1881 break;
1882 case 'C':
1883 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1884 &errstr);
1885 if (errstr != NULL)
1886 err(1, "-C option %s", errstr);
1887 break;
1888 case 'l':
1889 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1890 if (errstr != NULL)
1891 err(1, "-l option %s", errstr);
1892 break;
1893 case 'f':
1894 first_parent_traversal = 1;
1895 break;
1896 case 'r':
1897 repo_path = realpath(optarg, NULL);
1898 if (repo_path == NULL)
1899 return got_error_from_errno2("realpath",
1900 optarg);
1901 got_path_strip_trailing_slashes(repo_path);
1902 break;
1903 case 's':
1904 search_pattern = optarg;
1905 break;
1906 default:
1907 usage_log();
1908 /* NOTREACHED */
1912 argc -= optind;
1913 argv += optind;
1915 if (diff_context == -1)
1916 diff_context = 3;
1917 else if (!show_patch)
1918 errx(1, "-C reguires -p");
1920 cwd = getcwd(NULL, 0);
1921 if (cwd == NULL) {
1922 error = got_error_from_errno("getcwd");
1923 goto done;
1926 error = got_worktree_open(&worktree, cwd);
1927 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1928 goto done;
1929 error = NULL;
1931 if (argc == 0) {
1932 path = strdup("");
1933 if (path == NULL) {
1934 error = got_error_from_errno("strdup");
1935 goto done;
1937 } else if (argc == 1) {
1938 if (worktree) {
1939 error = got_worktree_resolve_path(&path, worktree,
1940 argv[0]);
1941 if (error)
1942 goto done;
1943 } else {
1944 path = strdup(argv[0]);
1945 if (path == NULL) {
1946 error = got_error_from_errno("strdup");
1947 goto done;
1950 } else
1951 usage_log();
1953 if (repo_path == NULL) {
1954 repo_path = worktree ?
1955 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1957 if (repo_path == NULL) {
1958 error = got_error_from_errno("strdup");
1959 goto done;
1962 error = got_repo_open(&repo, repo_path, NULL);
1963 if (error != NULL)
1964 goto done;
1966 error = apply_unveil(got_repo_get_path(repo), 1,
1967 worktree ? got_worktree_get_root_path(worktree) : NULL);
1968 if (error)
1969 goto done;
1971 if (start_commit == NULL) {
1972 struct got_reference *head_ref;
1973 error = got_ref_open(&head_ref, repo,
1974 worktree ? got_worktree_get_head_ref_name(worktree)
1975 : GOT_REF_HEAD, 0);
1976 if (error != NULL)
1977 return error;
1978 error = got_ref_resolve(&id, repo, head_ref);
1979 got_ref_close(head_ref);
1980 if (error != NULL)
1981 return error;
1982 error = got_object_open_as_commit(&commit, repo, id);
1983 } else {
1984 struct got_reference *ref;
1985 error = got_ref_open(&ref, repo, start_commit, 0);
1986 if (error == NULL) {
1987 int obj_type;
1988 error = got_ref_resolve(&id, repo, ref);
1989 got_ref_close(ref);
1990 if (error != NULL)
1991 goto done;
1992 error = got_object_get_type(&obj_type, repo, id);
1993 if (error != NULL)
1994 goto done;
1995 if (obj_type == GOT_OBJ_TYPE_TAG) {
1996 struct got_tag_object *tag;
1997 error = got_object_open_as_tag(&tag, repo, id);
1998 if (error != NULL)
1999 goto done;
2000 if (got_object_tag_get_object_type(tag) !=
2001 GOT_OBJ_TYPE_COMMIT) {
2002 got_object_tag_close(tag);
2003 error = got_error(GOT_ERR_OBJ_TYPE);
2004 goto done;
2006 free(id);
2007 id = got_object_id_dup(
2008 got_object_tag_get_object_id(tag));
2009 if (id == NULL)
2010 error = got_error_from_errno(
2011 "got_object_id_dup");
2012 got_object_tag_close(tag);
2013 if (error)
2014 goto done;
2015 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2016 error = got_error(GOT_ERR_OBJ_TYPE);
2017 goto done;
2019 error = got_object_open_as_commit(&commit, repo, id);
2020 if (error != NULL)
2021 goto done;
2023 if (commit == NULL) {
2024 error = got_repo_match_object_id_prefix(&id,
2025 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2026 if (error != NULL)
2027 return error;
2030 if (error != NULL)
2031 goto done;
2033 if (worktree) {
2034 const char *prefix = got_worktree_get_path_prefix(worktree);
2035 char *p;
2036 if (asprintf(&p, "%s%s%s", prefix,
2037 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2038 error = got_error_from_errno("asprintf");
2039 goto done;
2041 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2042 free(p);
2043 } else
2044 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2045 if (error != NULL)
2046 goto done;
2047 if (in_repo_path) {
2048 free(path);
2049 path = in_repo_path;
2052 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2053 if (error)
2054 goto done;
2056 error = print_commits(id, repo, path, show_patch, search_pattern,
2057 diff_context, limit, first_parent_traversal, &refs);
2058 done:
2059 free(path);
2060 free(repo_path);
2061 free(cwd);
2062 free(id);
2063 if (worktree)
2064 got_worktree_close(worktree);
2065 if (repo) {
2066 const struct got_error *repo_error;
2067 repo_error = got_repo_close(repo);
2068 if (error == NULL)
2069 error = repo_error;
2071 got_ref_list_free(&refs);
2072 return error;
2075 __dead static void
2076 usage_diff(void)
2078 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2079 "[-w] [object1 object2 | path]\n", getprogname());
2080 exit(1);
2083 struct print_diff_arg {
2084 struct got_repository *repo;
2085 struct got_worktree *worktree;
2086 int diff_context;
2087 const char *id_str;
2088 int header_shown;
2089 int diff_staged;
2090 int ignore_whitespace;
2093 static const struct got_error *
2094 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2095 const char *path, struct got_object_id *blob_id,
2096 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2097 int dirfd, const char *de_name)
2099 struct print_diff_arg *a = arg;
2100 const struct got_error *err = NULL;
2101 struct got_blob_object *blob1 = NULL;
2102 int fd = -1;
2103 FILE *f2 = NULL;
2104 char *abspath = NULL, *label1 = NULL;
2105 struct stat sb;
2107 if (a->diff_staged) {
2108 if (staged_status != GOT_STATUS_MODIFY &&
2109 staged_status != GOT_STATUS_ADD &&
2110 staged_status != GOT_STATUS_DELETE)
2111 return NULL;
2112 } else {
2113 if (staged_status == GOT_STATUS_DELETE)
2114 return NULL;
2115 if (status == GOT_STATUS_NONEXISTENT)
2116 return got_error_set_errno(ENOENT, path);
2117 if (status != GOT_STATUS_MODIFY &&
2118 status != GOT_STATUS_ADD &&
2119 status != GOT_STATUS_DELETE &&
2120 status != GOT_STATUS_CONFLICT)
2121 return NULL;
2124 if (!a->header_shown) {
2125 printf("diff %s %s%s\n", a->id_str,
2126 got_worktree_get_root_path(a->worktree),
2127 a->diff_staged ? " (staged changes)" : "");
2128 a->header_shown = 1;
2131 if (a->diff_staged) {
2132 const char *label1 = NULL, *label2 = NULL;
2133 switch (staged_status) {
2134 case GOT_STATUS_MODIFY:
2135 label1 = path;
2136 label2 = path;
2137 break;
2138 case GOT_STATUS_ADD:
2139 label2 = path;
2140 break;
2141 case GOT_STATUS_DELETE:
2142 label1 = path;
2143 break;
2144 default:
2145 return got_error(GOT_ERR_FILE_STATUS);
2147 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2148 label1, label2, a->diff_context, a->ignore_whitespace,
2149 a->repo, stdout);
2152 if (staged_status == GOT_STATUS_ADD ||
2153 staged_status == GOT_STATUS_MODIFY) {
2154 char *id_str;
2155 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2156 8192);
2157 if (err)
2158 goto done;
2159 err = got_object_id_str(&id_str, staged_blob_id);
2160 if (err)
2161 goto done;
2162 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2163 err = got_error_from_errno("asprintf");
2164 free(id_str);
2165 goto done;
2167 free(id_str);
2168 } else if (status != GOT_STATUS_ADD) {
2169 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2170 if (err)
2171 goto done;
2174 if (status != GOT_STATUS_DELETE) {
2175 if (asprintf(&abspath, "%s/%s",
2176 got_worktree_get_root_path(a->worktree), path) == -1) {
2177 err = got_error_from_errno("asprintf");
2178 goto done;
2181 if (dirfd != -1) {
2182 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2183 if (fd == -1) {
2184 err = got_error_from_errno2("openat", abspath);
2185 goto done;
2187 } else {
2188 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2189 if (fd == -1) {
2190 err = got_error_from_errno2("open", abspath);
2191 goto done;
2194 if (fstat(fd, &sb) == -1) {
2195 err = got_error_from_errno2("fstat", abspath);
2196 goto done;
2198 f2 = fdopen(fd, "r");
2199 if (f2 == NULL) {
2200 err = got_error_from_errno2("fdopen", abspath);
2201 goto done;
2203 fd = -1;
2204 } else
2205 sb.st_size = 0;
2207 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2208 a->diff_context, a->ignore_whitespace, stdout);
2209 done:
2210 if (blob1)
2211 got_object_blob_close(blob1);
2212 if (f2 && fclose(f2) == EOF && err == NULL)
2213 err = got_error_from_errno("fclose");
2214 if (fd != -1 && close(fd) == -1 && err == NULL)
2215 err = got_error_from_errno("close");
2216 free(abspath);
2217 return err;
2220 static const struct got_error *
2221 match_object_id(struct got_object_id **id, char **label,
2222 const char *id_str, int obj_type, int resolve_tags,
2223 struct got_repository *repo)
2225 const struct got_error *err;
2226 struct got_tag_object *tag;
2227 struct got_reference *ref = NULL;
2229 *id = NULL;
2230 *label = NULL;
2232 if (resolve_tags) {
2233 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2234 repo);
2235 if (err == NULL) {
2236 *id = got_object_id_dup(
2237 got_object_tag_get_object_id(tag));
2238 if (*id == NULL)
2239 err = got_error_from_errno("got_object_id_dup");
2240 else if (asprintf(label, "refs/tags/%s",
2241 got_object_tag_get_name(tag)) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 free(*id);
2244 *id = NULL;
2246 got_object_tag_close(tag);
2247 return err;
2248 } else if (err->code != GOT_ERR_NO_OBJ)
2249 return err;
2252 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2253 if (err) {
2254 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2255 return err;
2256 err = got_ref_open(&ref, repo, id_str, 0);
2257 if (err != NULL)
2258 goto done;
2259 *label = strdup(got_ref_get_name(ref));
2260 if (*label == NULL) {
2261 err = got_error_from_errno("strdup");
2262 goto done;
2264 err = got_ref_resolve(id, repo, ref);
2265 } else {
2266 err = got_object_id_str(label, *id);
2267 if (*label == NULL) {
2268 err = got_error_from_errno("strdup");
2269 goto done;
2272 done:
2273 if (ref)
2274 got_ref_close(ref);
2275 return err;
2279 static const struct got_error *
2280 cmd_diff(int argc, char *argv[])
2282 const struct got_error *error;
2283 struct got_repository *repo = NULL;
2284 struct got_worktree *worktree = NULL;
2285 char *cwd = NULL, *repo_path = NULL;
2286 struct got_object_id *id1 = NULL, *id2 = NULL;
2287 const char *id_str1 = NULL, *id_str2 = NULL;
2288 char *label1 = NULL, *label2 = NULL;
2289 int type1, type2;
2290 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2291 const char *errstr;
2292 char *path = NULL;
2294 #ifndef PROFILE
2295 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2296 NULL) == -1)
2297 err(1, "pledge");
2298 #endif
2300 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2301 switch (ch) {
2302 case 'C':
2303 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2304 &errstr);
2305 if (errstr != NULL)
2306 err(1, "-C option %s", errstr);
2307 break;
2308 case 'r':
2309 repo_path = realpath(optarg, NULL);
2310 if (repo_path == NULL)
2311 return got_error_from_errno2("realpath",
2312 optarg);
2313 got_path_strip_trailing_slashes(repo_path);
2314 break;
2315 case 's':
2316 diff_staged = 1;
2317 break;
2318 case 'w':
2319 ignore_whitespace = 1;
2320 break;
2321 default:
2322 usage_diff();
2323 /* NOTREACHED */
2327 argc -= optind;
2328 argv += optind;
2330 cwd = getcwd(NULL, 0);
2331 if (cwd == NULL) {
2332 error = got_error_from_errno("getcwd");
2333 goto done;
2335 error = got_worktree_open(&worktree, cwd);
2336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2337 goto done;
2338 if (argc <= 1) {
2339 if (worktree == NULL) {
2340 error = got_error(GOT_ERR_NOT_WORKTREE);
2341 goto done;
2343 if (repo_path)
2344 errx(1,
2345 "-r option can't be used when diffing a work tree");
2346 repo_path = strdup(got_worktree_get_repo_path(worktree));
2347 if (repo_path == NULL) {
2348 error = got_error_from_errno("strdup");
2349 goto done;
2351 if (argc == 1) {
2352 error = got_worktree_resolve_path(&path, worktree,
2353 argv[0]);
2354 if (error)
2355 goto done;
2356 } else {
2357 path = strdup("");
2358 if (path == NULL) {
2359 error = got_error_from_errno("strdup");
2360 goto done;
2363 } else if (argc == 2) {
2364 if (diff_staged)
2365 errx(1, "-s option can't be used when diffing "
2366 "objects in repository");
2367 id_str1 = argv[0];
2368 id_str2 = argv[1];
2369 if (worktree && repo_path == NULL) {
2370 repo_path =
2371 strdup(got_worktree_get_repo_path(worktree));
2372 if (repo_path == NULL) {
2373 error = got_error_from_errno("strdup");
2374 goto done;
2377 } else
2378 usage_diff();
2380 if (repo_path == NULL) {
2381 repo_path = getcwd(NULL, 0);
2382 if (repo_path == NULL)
2383 return got_error_from_errno("getcwd");
2386 error = got_repo_open(&repo, repo_path, NULL);
2387 free(repo_path);
2388 if (error != NULL)
2389 goto done;
2391 error = apply_unveil(got_repo_get_path(repo), 1,
2392 worktree ? got_worktree_get_root_path(worktree) : NULL);
2393 if (error)
2394 goto done;
2396 if (argc <= 1) {
2397 struct print_diff_arg arg;
2398 struct got_pathlist_head paths;
2399 char *id_str;
2401 TAILQ_INIT(&paths);
2403 error = got_object_id_str(&id_str,
2404 got_worktree_get_base_commit_id(worktree));
2405 if (error)
2406 goto done;
2407 arg.repo = repo;
2408 arg.worktree = worktree;
2409 arg.diff_context = diff_context;
2410 arg.id_str = id_str;
2411 arg.header_shown = 0;
2412 arg.diff_staged = diff_staged;
2413 arg.ignore_whitespace = ignore_whitespace;
2415 error = got_pathlist_append(&paths, path, NULL);
2416 if (error)
2417 goto done;
2419 error = got_worktree_status(worktree, &paths, repo, print_diff,
2420 &arg, check_cancelled, NULL);
2421 free(id_str);
2422 got_pathlist_free(&paths);
2423 goto done;
2426 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2427 repo);
2428 if (error)
2429 goto done;
2431 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2432 repo);
2433 if (error)
2434 goto done;
2436 error = got_object_get_type(&type1, repo, id1);
2437 if (error)
2438 goto done;
2440 error = got_object_get_type(&type2, repo, id2);
2441 if (error)
2442 goto done;
2444 if (type1 != type2) {
2445 error = got_error(GOT_ERR_OBJ_TYPE);
2446 goto done;
2449 switch (type1) {
2450 case GOT_OBJ_TYPE_BLOB:
2451 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2452 diff_context, ignore_whitespace, repo, stdout);
2453 break;
2454 case GOT_OBJ_TYPE_TREE:
2455 error = got_diff_objects_as_trees(id1, id2, "", "",
2456 diff_context, ignore_whitespace, repo, stdout);
2457 break;
2458 case GOT_OBJ_TYPE_COMMIT:
2459 printf("diff %s %s\n", label1, label2);
2460 error = got_diff_objects_as_commits(id1, id2, diff_context,
2461 ignore_whitespace, repo, stdout);
2462 break;
2463 default:
2464 error = got_error(GOT_ERR_OBJ_TYPE);
2467 done:
2468 free(label1);
2469 free(label2);
2470 free(id1);
2471 free(id2);
2472 free(path);
2473 if (worktree)
2474 got_worktree_close(worktree);
2475 if (repo) {
2476 const struct got_error *repo_error;
2477 repo_error = got_repo_close(repo);
2478 if (error == NULL)
2479 error = repo_error;
2481 return error;
2484 __dead static void
2485 usage_blame(void)
2487 fprintf(stderr,
2488 "usage: %s blame [-c commit] [-r repository-path] path\n",
2489 getprogname());
2490 exit(1);
2493 struct blame_line {
2494 int annotated;
2495 char *id_str;
2496 char *committer;
2497 char datebuf[11]; /* YYYY-MM-DD + NUL */
2500 struct blame_cb_args {
2501 struct blame_line *lines;
2502 int nlines;
2503 int nlines_prec;
2504 int lineno_cur;
2505 off_t *line_offsets;
2506 FILE *f;
2507 struct got_repository *repo;
2510 static const struct got_error *
2511 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2513 const struct got_error *err = NULL;
2514 struct blame_cb_args *a = arg;
2515 struct blame_line *bline;
2516 char *line = NULL;
2517 size_t linesize = 0;
2518 struct got_commit_object *commit = NULL;
2519 off_t offset;
2520 struct tm tm;
2521 time_t committer_time;
2523 if (nlines != a->nlines ||
2524 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2525 return got_error(GOT_ERR_RANGE);
2527 if (sigint_received)
2528 return got_error(GOT_ERR_ITER_COMPLETED);
2530 if (lineno == -1)
2531 return NULL; /* no change in this commit */
2533 /* Annotate this line. */
2534 bline = &a->lines[lineno - 1];
2535 if (bline->annotated)
2536 return NULL;
2537 err = got_object_id_str(&bline->id_str, id);
2538 if (err)
2539 return err;
2541 err = got_object_open_as_commit(&commit, a->repo, id);
2542 if (err)
2543 goto done;
2545 bline->committer = strdup(got_object_commit_get_committer(commit));
2546 if (bline->committer == NULL) {
2547 err = got_error_from_errno("strdup");
2548 goto done;
2551 committer_time = got_object_commit_get_committer_time(commit);
2552 if (localtime_r(&committer_time, &tm) == NULL)
2553 return got_error_from_errno("localtime_r");
2554 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2555 &tm) >= sizeof(bline->datebuf)) {
2556 err = got_error(GOT_ERR_NO_SPACE);
2557 goto done;
2559 bline->annotated = 1;
2561 /* Print lines annotated so far. */
2562 bline = &a->lines[a->lineno_cur - 1];
2563 if (!bline->annotated)
2564 goto done;
2566 offset = a->line_offsets[a->lineno_cur - 1];
2567 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2568 err = got_error_from_errno("fseeko");
2569 goto done;
2572 while (bline->annotated) {
2573 char *smallerthan, *at, *nl, *committer;
2574 size_t len;
2576 if (getline(&line, &linesize, a->f) == -1) {
2577 if (ferror(a->f))
2578 err = got_error_from_errno("getline");
2579 break;
2582 committer = bline->committer;
2583 smallerthan = strchr(committer, '<');
2584 if (smallerthan && smallerthan[1] != '\0')
2585 committer = smallerthan + 1;
2586 at = strchr(committer, '@');
2587 if (at)
2588 *at = '\0';
2589 len = strlen(committer);
2590 if (len >= 9)
2591 committer[8] = '\0';
2593 nl = strchr(line, '\n');
2594 if (nl)
2595 *nl = '\0';
2596 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2597 bline->id_str, bline->datebuf, committer, line);
2599 a->lineno_cur++;
2600 bline = &a->lines[a->lineno_cur - 1];
2602 done:
2603 if (commit)
2604 got_object_commit_close(commit);
2605 free(line);
2606 return err;
2609 static const struct got_error *
2610 cmd_blame(int argc, char *argv[])
2612 const struct got_error *error;
2613 struct got_repository *repo = NULL;
2614 struct got_worktree *worktree = NULL;
2615 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2616 struct got_object_id *obj_id = NULL;
2617 struct got_object_id *commit_id = NULL;
2618 struct got_blob_object *blob = NULL;
2619 char *commit_id_str = NULL;
2620 struct blame_cb_args bca;
2621 int ch, obj_type, i;
2622 size_t filesize;
2624 memset(&bca, 0, sizeof(bca));
2626 #ifndef PROFILE
2627 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2628 NULL) == -1)
2629 err(1, "pledge");
2630 #endif
2632 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2633 switch (ch) {
2634 case 'c':
2635 commit_id_str = optarg;
2636 break;
2637 case 'r':
2638 repo_path = realpath(optarg, NULL);
2639 if (repo_path == NULL)
2640 return got_error_from_errno2("realpath",
2641 optarg);
2642 got_path_strip_trailing_slashes(repo_path);
2643 break;
2644 default:
2645 usage_blame();
2646 /* NOTREACHED */
2650 argc -= optind;
2651 argv += optind;
2653 if (argc == 1)
2654 path = argv[0];
2655 else
2656 usage_blame();
2658 cwd = getcwd(NULL, 0);
2659 if (cwd == NULL) {
2660 error = got_error_from_errno("getcwd");
2661 goto done;
2663 if (repo_path == NULL) {
2664 error = got_worktree_open(&worktree, cwd);
2665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2666 goto done;
2667 else
2668 error = NULL;
2669 if (worktree) {
2670 repo_path =
2671 strdup(got_worktree_get_repo_path(worktree));
2672 if (repo_path == NULL)
2673 error = got_error_from_errno("strdup");
2674 if (error)
2675 goto done;
2676 } else {
2677 repo_path = strdup(cwd);
2678 if (repo_path == NULL) {
2679 error = got_error_from_errno("strdup");
2680 goto done;
2685 error = got_repo_open(&repo, repo_path, NULL);
2686 if (error != NULL)
2687 goto done;
2689 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2690 if (error)
2691 goto done;
2693 if (worktree) {
2694 const char *prefix = got_worktree_get_path_prefix(worktree);
2695 char *p, *worktree_subdir = cwd +
2696 strlen(got_worktree_get_root_path(worktree));
2697 if (asprintf(&p, "%s%s%s%s%s",
2698 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2699 worktree_subdir, worktree_subdir[0] ? "/" : "",
2700 path) == -1) {
2701 error = got_error_from_errno("asprintf");
2702 goto done;
2704 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2705 free(p);
2706 } else {
2707 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2709 if (error)
2710 goto done;
2712 if (commit_id_str == NULL) {
2713 struct got_reference *head_ref;
2714 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2715 if (error != NULL)
2716 goto done;
2717 error = got_ref_resolve(&commit_id, repo, head_ref);
2718 got_ref_close(head_ref);
2719 if (error != NULL)
2720 goto done;
2721 } else {
2722 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2723 if (error)
2724 goto done;
2727 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2728 if (error)
2729 goto done;
2730 if (obj_id == NULL) {
2731 error = got_error(GOT_ERR_NO_OBJ);
2732 goto done;
2735 error = got_object_get_type(&obj_type, repo, obj_id);
2736 if (error)
2737 goto done;
2739 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2740 error = got_error(GOT_ERR_OBJ_TYPE);
2741 goto done;
2744 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2745 if (error)
2746 goto done;
2747 bca.f = got_opentemp();
2748 if (bca.f == NULL) {
2749 error = got_error_from_errno("got_opentemp");
2750 goto done;
2752 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2753 &bca.line_offsets, bca.f, blob);
2754 if (error || bca.nlines == 0)
2755 goto done;
2757 /* Don't include \n at EOF in the blame line count. */
2758 if (bca.line_offsets[bca.nlines - 1] == filesize)
2759 bca.nlines--;
2761 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2762 if (bca.lines == NULL) {
2763 error = got_error_from_errno("calloc");
2764 goto done;
2766 bca.lineno_cur = 1;
2767 bca.nlines_prec = 0;
2768 i = bca.nlines;
2769 while (i > 0) {
2770 i /= 10;
2771 bca.nlines_prec++;
2773 bca.repo = repo;
2775 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2776 check_cancelled, NULL);
2777 if (error)
2778 goto done;
2779 done:
2780 free(in_repo_path);
2781 free(repo_path);
2782 free(cwd);
2783 free(commit_id);
2784 free(obj_id);
2785 if (blob)
2786 got_object_blob_close(blob);
2787 if (worktree)
2788 got_worktree_close(worktree);
2789 if (repo) {
2790 const struct got_error *repo_error;
2791 repo_error = got_repo_close(repo);
2792 if (error == NULL)
2793 error = repo_error;
2795 if (bca.lines) {
2796 for (i = 0; i < bca.nlines; i++) {
2797 struct blame_line *bline = &bca.lines[i];
2798 free(bline->id_str);
2799 free(bline->committer);
2801 free(bca.lines);
2803 free(bca.line_offsets);
2804 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2805 error = got_error_from_errno("fclose");
2806 return error;
2809 __dead static void
2810 usage_tree(void)
2812 fprintf(stderr,
2813 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2814 getprogname());
2815 exit(1);
2818 static void
2819 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2820 const char *root_path)
2822 int is_root_path = (strcmp(path, root_path) == 0);
2823 const char *modestr = "";
2824 mode_t mode = got_tree_entry_get_mode(te);
2826 path += strlen(root_path);
2827 while (path[0] == '/')
2828 path++;
2830 if (got_object_tree_entry_is_submodule(te))
2831 modestr = "$";
2832 else if (S_ISLNK(mode))
2833 modestr = "@";
2834 else if (S_ISDIR(mode))
2835 modestr = "/";
2836 else if (mode & S_IXUSR)
2837 modestr = "*";
2839 printf("%s%s%s%s%s\n", id ? id : "", path,
2840 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2843 static const struct got_error *
2844 print_tree(const char *path, struct got_object_id *commit_id,
2845 int show_ids, int recurse, const char *root_path,
2846 struct got_repository *repo)
2848 const struct got_error *err = NULL;
2849 struct got_object_id *tree_id = NULL;
2850 struct got_tree_object *tree = NULL;
2851 int nentries, i;
2853 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2854 if (err)
2855 goto done;
2857 err = got_object_open_as_tree(&tree, repo, tree_id);
2858 if (err)
2859 goto done;
2860 nentries = got_object_tree_get_nentries(tree);
2861 for (i = 0; i < nentries; i++) {
2862 struct got_tree_entry *te;
2863 char *id = NULL;
2865 if (sigint_received || sigpipe_received)
2866 break;
2868 te = got_object_tree_get_entry(tree, i);
2869 if (show_ids) {
2870 char *id_str;
2871 err = got_object_id_str(&id_str,
2872 got_tree_entry_get_id(te));
2873 if (err)
2874 goto done;
2875 if (asprintf(&id, "%s ", id_str) == -1) {
2876 err = got_error_from_errno("asprintf");
2877 free(id_str);
2878 goto done;
2880 free(id_str);
2882 print_entry(te, id, path, root_path);
2883 free(id);
2885 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2886 char *child_path;
2887 if (asprintf(&child_path, "%s%s%s", path,
2888 path[0] == '/' && path[1] == '\0' ? "" : "/",
2889 got_tree_entry_get_name(te)) == -1) {
2890 err = got_error_from_errno("asprintf");
2891 goto done;
2893 err = print_tree(child_path, commit_id, show_ids, 1,
2894 root_path, repo);
2895 free(child_path);
2896 if (err)
2897 goto done;
2900 done:
2901 if (tree)
2902 got_object_tree_close(tree);
2903 free(tree_id);
2904 return err;
2907 static const struct got_error *
2908 cmd_tree(int argc, char *argv[])
2910 const struct got_error *error;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2913 const char *path;
2914 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2915 struct got_object_id *commit_id = NULL;
2916 char *commit_id_str = NULL;
2917 int show_ids = 0, recurse = 0;
2918 int ch;
2920 #ifndef PROFILE
2921 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2922 NULL) == -1)
2923 err(1, "pledge");
2924 #endif
2926 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2927 switch (ch) {
2928 case 'c':
2929 commit_id_str = optarg;
2930 break;
2931 case 'r':
2932 repo_path = realpath(optarg, NULL);
2933 if (repo_path == NULL)
2934 return got_error_from_errno2("realpath",
2935 optarg);
2936 got_path_strip_trailing_slashes(repo_path);
2937 break;
2938 case 'i':
2939 show_ids = 1;
2940 break;
2941 case 'R':
2942 recurse = 1;
2943 break;
2944 default:
2945 usage_tree();
2946 /* NOTREACHED */
2950 argc -= optind;
2951 argv += optind;
2953 if (argc == 1)
2954 path = argv[0];
2955 else if (argc > 1)
2956 usage_tree();
2957 else
2958 path = NULL;
2960 cwd = getcwd(NULL, 0);
2961 if (cwd == NULL) {
2962 error = got_error_from_errno("getcwd");
2963 goto done;
2965 if (repo_path == NULL) {
2966 error = got_worktree_open(&worktree, cwd);
2967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2968 goto done;
2969 else
2970 error = NULL;
2971 if (worktree) {
2972 repo_path =
2973 strdup(got_worktree_get_repo_path(worktree));
2974 if (repo_path == NULL)
2975 error = got_error_from_errno("strdup");
2976 if (error)
2977 goto done;
2978 } else {
2979 repo_path = strdup(cwd);
2980 if (repo_path == NULL) {
2981 error = got_error_from_errno("strdup");
2982 goto done;
2987 error = got_repo_open(&repo, repo_path, NULL);
2988 if (error != NULL)
2989 goto done;
2991 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2992 if (error)
2993 goto done;
2995 if (path == NULL) {
2996 if (worktree) {
2997 char *p, *worktree_subdir = cwd +
2998 strlen(got_worktree_get_root_path(worktree));
2999 if (asprintf(&p, "%s/%s",
3000 got_worktree_get_path_prefix(worktree),
3001 worktree_subdir) == -1) {
3002 error = got_error_from_errno("asprintf");
3003 goto done;
3005 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3006 free(p);
3007 if (error)
3008 goto done;
3009 } else
3010 path = "/";
3012 if (in_repo_path == NULL) {
3013 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3014 if (error != NULL)
3015 goto done;
3018 if (commit_id_str == NULL) {
3019 struct got_reference *head_ref;
3020 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3021 if (error != NULL)
3022 goto done;
3023 error = got_ref_resolve(&commit_id, repo, head_ref);
3024 got_ref_close(head_ref);
3025 if (error != NULL)
3026 goto done;
3027 } else {
3028 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3029 if (error)
3030 goto done;
3033 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3034 in_repo_path, repo);
3035 done:
3036 free(in_repo_path);
3037 free(repo_path);
3038 free(cwd);
3039 free(commit_id);
3040 if (worktree)
3041 got_worktree_close(worktree);
3042 if (repo) {
3043 const struct got_error *repo_error;
3044 repo_error = got_repo_close(repo);
3045 if (error == NULL)
3046 error = repo_error;
3048 return error;
3051 __dead static void
3052 usage_status(void)
3054 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3055 exit(1);
3058 static const struct got_error *
3059 print_status(void *arg, unsigned char status, unsigned char staged_status,
3060 const char *path, struct got_object_id *blob_id,
3061 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3062 int dirfd, const char *de_name)
3064 if (status == staged_status && (status == GOT_STATUS_DELETE))
3065 status = GOT_STATUS_NO_CHANGE;
3066 printf("%c%c %s\n", status, staged_status, path);
3067 return NULL;
3070 static const struct got_error *
3071 cmd_status(int argc, char *argv[])
3073 const struct got_error *error = NULL;
3074 struct got_repository *repo = NULL;
3075 struct got_worktree *worktree = NULL;
3076 char *cwd = NULL;
3077 struct got_pathlist_head paths;
3078 struct got_pathlist_entry *pe;
3079 int ch;
3081 TAILQ_INIT(&paths);
3083 while ((ch = getopt(argc, argv, "")) != -1) {
3084 switch (ch) {
3085 default:
3086 usage_status();
3087 /* NOTREACHED */
3091 argc -= optind;
3092 argv += optind;
3094 #ifndef PROFILE
3095 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3096 NULL) == -1)
3097 err(1, "pledge");
3098 #endif
3099 cwd = getcwd(NULL, 0);
3100 if (cwd == NULL) {
3101 error = got_error_from_errno("getcwd");
3102 goto done;
3105 error = got_worktree_open(&worktree, cwd);
3106 if (error != NULL)
3107 goto done;
3109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3110 NULL);
3111 if (error != NULL)
3112 goto done;
3114 error = apply_unveil(got_repo_get_path(repo), 1,
3115 got_worktree_get_root_path(worktree));
3116 if (error)
3117 goto done;
3119 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3120 if (error)
3121 goto done;
3123 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3124 check_cancelled, NULL);
3125 done:
3126 TAILQ_FOREACH(pe, &paths, entry)
3127 free((char *)pe->path);
3128 got_pathlist_free(&paths);
3129 free(cwd);
3130 return error;
3133 __dead static void
3134 usage_ref(void)
3136 fprintf(stderr,
3137 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3138 getprogname());
3139 exit(1);
3142 static const struct got_error *
3143 list_refs(struct got_repository *repo)
3145 static const struct got_error *err = NULL;
3146 struct got_reflist_head refs;
3147 struct got_reflist_entry *re;
3149 SIMPLEQ_INIT(&refs);
3150 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3151 if (err)
3152 return err;
3154 SIMPLEQ_FOREACH(re, &refs, entry) {
3155 char *refstr;
3156 refstr = got_ref_to_str(re->ref);
3157 if (refstr == NULL)
3158 return got_error_from_errno("got_ref_to_str");
3159 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3160 free(refstr);
3163 got_ref_list_free(&refs);
3164 return NULL;
3167 static const struct got_error *
3168 delete_ref(struct got_repository *repo, const char *refname)
3170 const struct got_error *err = NULL;
3171 struct got_reference *ref;
3173 err = got_ref_open(&ref, repo, refname, 0);
3174 if (err)
3175 return err;
3177 err = got_ref_delete(ref, repo);
3178 got_ref_close(ref);
3179 return err;
3182 static const struct got_error *
3183 add_ref(struct got_repository *repo, const char *refname, const char *target)
3185 const struct got_error *err = NULL;
3186 struct got_object_id *id;
3187 struct got_reference *ref = NULL;
3190 * Don't let the user create a reference name with a leading '-'.
3191 * While technically a valid reference name, this case is usually
3192 * an unintended typo.
3194 if (refname[0] == '-')
3195 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3197 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3198 repo);
3199 if (err) {
3200 struct got_reference *target_ref;
3202 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3203 return err;
3204 err = got_ref_open(&target_ref, repo, target, 0);
3205 if (err)
3206 return err;
3207 err = got_ref_resolve(&id, repo, target_ref);
3208 got_ref_close(target_ref);
3209 if (err)
3210 return err;
3213 err = got_ref_alloc(&ref, refname, id);
3214 if (err)
3215 goto done;
3217 err = got_ref_write(ref, repo);
3218 done:
3219 if (ref)
3220 got_ref_close(ref);
3221 free(id);
3222 return err;
3225 static const struct got_error *
3226 add_symref(struct got_repository *repo, const char *refname, const char *target)
3228 const struct got_error *err = NULL;
3229 struct got_reference *ref = NULL;
3230 struct got_reference *target_ref = NULL;
3233 * Don't let the user create a reference name with a leading '-'.
3234 * While technically a valid reference name, this case is usually
3235 * an unintended typo.
3237 if (refname[0] == '-')
3238 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3240 err = got_ref_open(&target_ref, repo, target, 0);
3241 if (err)
3242 return err;
3244 err = got_ref_alloc_symref(&ref, refname, target_ref);
3245 if (err)
3246 goto done;
3248 err = got_ref_write(ref, repo);
3249 done:
3250 if (target_ref)
3251 got_ref_close(target_ref);
3252 if (ref)
3253 got_ref_close(ref);
3254 return err;
3257 static const struct got_error *
3258 cmd_ref(int argc, char *argv[])
3260 const struct got_error *error = NULL;
3261 struct got_repository *repo = NULL;
3262 struct got_worktree *worktree = NULL;
3263 char *cwd = NULL, *repo_path = NULL;
3264 int ch, do_list = 0, create_symref = 0;
3265 const char *delref = NULL;
3267 /* TODO: Add -s option for adding symbolic references. */
3268 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3269 switch (ch) {
3270 case 'd':
3271 delref = optarg;
3272 break;
3273 case 'r':
3274 repo_path = realpath(optarg, NULL);
3275 if (repo_path == NULL)
3276 return got_error_from_errno2("realpath",
3277 optarg);
3278 got_path_strip_trailing_slashes(repo_path);
3279 break;
3280 case 'l':
3281 do_list = 1;
3282 break;
3283 case 's':
3284 create_symref = 1;
3285 break;
3286 default:
3287 usage_ref();
3288 /* NOTREACHED */
3292 if (do_list && delref)
3293 errx(1, "-l and -d options are mutually exclusive\n");
3295 argc -= optind;
3296 argv += optind;
3298 if (do_list || delref) {
3299 if (create_symref)
3300 errx(1, "-s option cannot be used together with the "
3301 "-l or -d options");
3302 if (argc > 0)
3303 usage_ref();
3304 } else if (argc != 2)
3305 usage_ref();
3307 #ifndef PROFILE
3308 if (do_list) {
3309 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3310 NULL) == -1)
3311 err(1, "pledge");
3312 } else {
3313 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3314 "sendfd unveil", NULL) == -1)
3315 err(1, "pledge");
3317 #endif
3318 cwd = getcwd(NULL, 0);
3319 if (cwd == NULL) {
3320 error = got_error_from_errno("getcwd");
3321 goto done;
3324 if (repo_path == NULL) {
3325 error = got_worktree_open(&worktree, cwd);
3326 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3327 goto done;
3328 else
3329 error = NULL;
3330 if (worktree) {
3331 repo_path =
3332 strdup(got_worktree_get_repo_path(worktree));
3333 if (repo_path == NULL)
3334 error = got_error_from_errno("strdup");
3335 if (error)
3336 goto done;
3337 } else {
3338 repo_path = strdup(cwd);
3339 if (repo_path == NULL) {
3340 error = got_error_from_errno("strdup");
3341 goto done;
3346 error = got_repo_open(&repo, repo_path, NULL);
3347 if (error != NULL)
3348 goto done;
3350 error = apply_unveil(got_repo_get_path(repo), do_list,
3351 worktree ? got_worktree_get_root_path(worktree) : NULL);
3352 if (error)
3353 goto done;
3355 if (do_list)
3356 error = list_refs(repo);
3357 else if (delref)
3358 error = delete_ref(repo, delref);
3359 else if (create_symref)
3360 error = add_symref(repo, argv[0], argv[1]);
3361 else
3362 error = add_ref(repo, argv[0], argv[1]);
3363 done:
3364 if (repo)
3365 got_repo_close(repo);
3366 if (worktree)
3367 got_worktree_close(worktree);
3368 free(cwd);
3369 free(repo_path);
3370 return error;
3373 __dead static void
3374 usage_branch(void)
3376 fprintf(stderr,
3377 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3378 "[name]\n", getprogname());
3379 exit(1);
3382 static const struct got_error *
3383 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3384 struct got_reference *ref)
3386 const struct got_error *err = NULL;
3387 const char *refname, *marker = " ";
3388 char *refstr;
3390 refname = got_ref_get_name(ref);
3391 if (worktree && strcmp(refname,
3392 got_worktree_get_head_ref_name(worktree)) == 0) {
3393 struct got_object_id *id = NULL;
3395 err = got_ref_resolve(&id, repo, ref);
3396 if (err)
3397 return err;
3398 if (got_object_id_cmp(id,
3399 got_worktree_get_base_commit_id(worktree)) == 0)
3400 marker = "* ";
3401 else
3402 marker = "~ ";
3403 free(id);
3406 if (strncmp(refname, "refs/heads/", 11) == 0)
3407 refname += 11;
3408 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3409 refname += 18;
3411 refstr = got_ref_to_str(ref);
3412 if (refstr == NULL)
3413 return got_error_from_errno("got_ref_to_str");
3415 printf("%s%s: %s\n", marker, refname, refstr);
3416 free(refstr);
3417 return NULL;
3420 static const struct got_error *
3421 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3423 const char *refname;
3425 if (worktree == NULL)
3426 return got_error(GOT_ERR_NOT_WORKTREE);
3428 refname = got_worktree_get_head_ref_name(worktree);
3430 if (strncmp(refname, "refs/heads/", 11) == 0)
3431 refname += 11;
3432 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3433 refname += 18;
3435 printf("%s\n", refname);
3437 return NULL;
3440 static const struct got_error *
3441 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3443 static const struct got_error *err = NULL;
3444 struct got_reflist_head refs;
3445 struct got_reflist_entry *re;
3446 struct got_reference *temp_ref = NULL;
3447 int rebase_in_progress, histedit_in_progress;
3449 SIMPLEQ_INIT(&refs);
3451 if (worktree) {
3452 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3453 worktree);
3454 if (err)
3455 return err;
3457 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3458 worktree);
3459 if (err)
3460 return err;
3462 if (rebase_in_progress || histedit_in_progress) {
3463 err = got_ref_open(&temp_ref, repo,
3464 got_worktree_get_head_ref_name(worktree), 0);
3465 if (err)
3466 return err;
3467 list_branch(repo, worktree, temp_ref);
3468 got_ref_close(temp_ref);
3472 err = got_ref_list(&refs, repo, "refs/heads",
3473 got_ref_cmp_by_name, NULL);
3474 if (err)
3475 return err;
3477 SIMPLEQ_FOREACH(re, &refs, entry)
3478 list_branch(repo, worktree, re->ref);
3480 got_ref_list_free(&refs);
3481 return NULL;
3484 static const struct got_error *
3485 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3486 const char *branch_name)
3488 const struct got_error *err = NULL;
3489 struct got_reference *ref = NULL;
3490 char *refname;
3492 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3493 return got_error_from_errno("asprintf");
3495 err = got_ref_open(&ref, repo, refname, 0);
3496 if (err)
3497 goto done;
3499 if (worktree &&
3500 strcmp(got_worktree_get_head_ref_name(worktree),
3501 got_ref_get_name(ref)) == 0) {
3502 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3503 "will not delete this work tree's current branch");
3504 goto done;
3507 err = got_ref_delete(ref, repo);
3508 done:
3509 if (ref)
3510 got_ref_close(ref);
3511 free(refname);
3512 return err;
3515 static const struct got_error *
3516 add_branch(struct got_repository *repo, const char *branch_name,
3517 struct got_object_id *base_commit_id)
3519 const struct got_error *err = NULL;
3520 struct got_reference *ref = NULL;
3521 char *base_refname = NULL, *refname = NULL;
3524 * Don't let the user create a branch name with a leading '-'.
3525 * While technically a valid reference name, this case is usually
3526 * an unintended typo.
3528 if (branch_name[0] == '-')
3529 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3531 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3532 err = got_error_from_errno("asprintf");
3533 goto done;
3536 err = got_ref_open(&ref, repo, refname, 0);
3537 if (err == NULL) {
3538 err = got_error(GOT_ERR_BRANCH_EXISTS);
3539 goto done;
3540 } else if (err->code != GOT_ERR_NOT_REF)
3541 goto done;
3543 err = got_ref_alloc(&ref, refname, base_commit_id);
3544 if (err)
3545 goto done;
3547 err = got_ref_write(ref, repo);
3548 done:
3549 if (ref)
3550 got_ref_close(ref);
3551 free(base_refname);
3552 free(refname);
3553 return err;
3556 static const struct got_error *
3557 cmd_branch(int argc, char *argv[])
3559 const struct got_error *error = NULL;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 char *cwd = NULL, *repo_path = NULL;
3563 int ch, do_list = 0, do_show = 0;
3564 const char *delref = NULL, *commit_id_arg = NULL;
3566 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3567 switch (ch) {
3568 case 'c':
3569 commit_id_arg = optarg;
3570 break;
3571 case 'd':
3572 delref = optarg;
3573 break;
3574 case 'r':
3575 repo_path = realpath(optarg, NULL);
3576 if (repo_path == NULL)
3577 return got_error_from_errno2("realpath",
3578 optarg);
3579 got_path_strip_trailing_slashes(repo_path);
3580 break;
3581 case 'l':
3582 do_list = 1;
3583 break;
3584 default:
3585 usage_branch();
3586 /* NOTREACHED */
3590 if (do_list && delref)
3591 errx(1, "-l and -d options are mutually exclusive\n");
3593 argc -= optind;
3594 argv += optind;
3596 if (!do_list && !delref && argc == 0)
3597 do_show = 1;
3599 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3600 errx(1, "-c option can only be used when creating a branch");
3602 if (do_list || delref) {
3603 if (argc > 0)
3604 usage_branch();
3605 } else if (!do_show && argc != 1)
3606 usage_branch();
3608 #ifndef PROFILE
3609 if (do_list || do_show) {
3610 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3611 NULL) == -1)
3612 err(1, "pledge");
3613 } else {
3614 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3615 "sendfd unveil", NULL) == -1)
3616 err(1, "pledge");
3618 #endif
3619 cwd = getcwd(NULL, 0);
3620 if (cwd == NULL) {
3621 error = got_error_from_errno("getcwd");
3622 goto done;
3625 if (repo_path == NULL) {
3626 error = got_worktree_open(&worktree, cwd);
3627 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3628 goto done;
3629 else
3630 error = NULL;
3631 if (worktree) {
3632 repo_path =
3633 strdup(got_worktree_get_repo_path(worktree));
3634 if (repo_path == NULL)
3635 error = got_error_from_errno("strdup");
3636 if (error)
3637 goto done;
3638 } else {
3639 repo_path = strdup(cwd);
3640 if (repo_path == NULL) {
3641 error = got_error_from_errno("strdup");
3642 goto done;
3647 error = got_repo_open(&repo, repo_path, NULL);
3648 if (error != NULL)
3649 goto done;
3651 error = apply_unveil(got_repo_get_path(repo), do_list,
3652 worktree ? got_worktree_get_root_path(worktree) : NULL);
3653 if (error)
3654 goto done;
3656 if (do_show)
3657 error = show_current_branch(repo, worktree);
3658 else if (do_list)
3659 error = list_branches(repo, worktree);
3660 else if (delref)
3661 error = delete_branch(repo, worktree, delref);
3662 else {
3663 struct got_object_id *commit_id;
3664 if (commit_id_arg == NULL)
3665 commit_id_arg = worktree ?
3666 got_worktree_get_head_ref_name(worktree) :
3667 GOT_REF_HEAD;
3668 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3669 if (error)
3670 goto done;
3671 error = add_branch(repo, argv[0], commit_id);
3672 free(commit_id);
3674 done:
3675 if (repo)
3676 got_repo_close(repo);
3677 if (worktree)
3678 got_worktree_close(worktree);
3679 free(cwd);
3680 free(repo_path);
3681 return error;
3685 __dead static void
3686 usage_tag(void)
3688 fprintf(stderr,
3689 "usage: %s tag [-r repository] | -l | "
3690 "[-m message] name [commit]\n", getprogname());
3691 exit(1);
3694 #if 0
3695 static const struct got_error *
3696 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3698 const struct got_error *err = NULL;
3699 struct got_reflist_entry *re, *se, *new;
3700 struct got_object_id *re_id, *se_id;
3701 struct got_tag_object *re_tag, *se_tag;
3702 time_t re_time, se_time;
3704 SIMPLEQ_FOREACH(re, tags, entry) {
3705 se = SIMPLEQ_FIRST(sorted);
3706 if (se == NULL) {
3707 err = got_reflist_entry_dup(&new, re);
3708 if (err)
3709 return err;
3710 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3711 continue;
3712 } else {
3713 err = got_ref_resolve(&re_id, repo, re->ref);
3714 if (err)
3715 break;
3716 err = got_object_open_as_tag(&re_tag, repo, re_id);
3717 free(re_id);
3718 if (err)
3719 break;
3720 re_time = got_object_tag_get_tagger_time(re_tag);
3721 got_object_tag_close(re_tag);
3724 while (se) {
3725 err = got_ref_resolve(&se_id, repo, re->ref);
3726 if (err)
3727 break;
3728 err = got_object_open_as_tag(&se_tag, repo, se_id);
3729 free(se_id);
3730 if (err)
3731 break;
3732 se_time = got_object_tag_get_tagger_time(se_tag);
3733 got_object_tag_close(se_tag);
3735 if (se_time > re_time) {
3736 err = got_reflist_entry_dup(&new, re);
3737 if (err)
3738 return err;
3739 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3740 break;
3742 se = SIMPLEQ_NEXT(se, entry);
3743 continue;
3746 done:
3747 return err;
3749 #endif
3751 static const struct got_error *
3752 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3753 struct got_reference *ref2)
3755 const struct got_error *err = NULL;
3756 struct got_repository *repo = arg;
3757 struct got_object_id *id1, *id2 = NULL;
3758 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3759 time_t time1, time2;
3761 *cmp = 0;
3763 err = got_ref_resolve(&id1, repo, ref1);
3764 if (err)
3765 return err;
3766 err = got_object_open_as_tag(&tag1, repo, id1);
3767 if (err)
3768 goto done;
3770 err = got_ref_resolve(&id2, repo, ref2);
3771 if (err)
3772 goto done;
3773 err = got_object_open_as_tag(&tag2, repo, id2);
3774 if (err)
3775 goto done;
3777 time1 = got_object_tag_get_tagger_time(tag1);
3778 time2 = got_object_tag_get_tagger_time(tag2);
3780 /* Put latest tags first. */
3781 if (time1 < time2)
3782 *cmp = 1;
3783 else if (time1 > time2)
3784 *cmp = -1;
3785 else
3786 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3787 done:
3788 free(id1);
3789 free(id2);
3790 if (tag1)
3791 got_object_tag_close(tag1);
3792 if (tag2)
3793 got_object_tag_close(tag2);
3794 return err;
3797 static const struct got_error *
3798 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3800 static const struct got_error *err = NULL;
3801 struct got_reflist_head refs;
3802 struct got_reflist_entry *re;
3804 SIMPLEQ_INIT(&refs);
3806 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3807 if (err)
3808 return err;
3810 SIMPLEQ_FOREACH(re, &refs, entry) {
3811 const char *refname;
3812 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3813 char datebuf[26];
3814 time_t tagger_time;
3815 struct got_object_id *id;
3816 struct got_tag_object *tag;
3818 refname = got_ref_get_name(re->ref);
3819 if (strncmp(refname, "refs/tags/", 10) != 0)
3820 continue;
3821 refname += 10;
3822 refstr = got_ref_to_str(re->ref);
3823 if (refstr == NULL) {
3824 err = got_error_from_errno("got_ref_to_str");
3825 break;
3827 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3828 free(refstr);
3830 err = got_ref_resolve(&id, repo, re->ref);
3831 if (err)
3832 break;
3833 err = got_object_open_as_tag(&tag, repo, id);
3834 free(id);
3835 if (err)
3836 break;
3837 printf("from: %s\n", got_object_tag_get_tagger(tag));
3838 tagger_time = got_object_tag_get_tagger_time(tag);
3839 datestr = get_datestr(&tagger_time, datebuf);
3840 if (datestr)
3841 printf("date: %s UTC\n", datestr);
3842 err = got_object_id_str(&id_str,
3843 got_object_tag_get_object_id(tag));
3844 if (err)
3845 break;
3846 switch (got_object_tag_get_object_type(tag)) {
3847 case GOT_OBJ_TYPE_BLOB:
3848 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3849 break;
3850 case GOT_OBJ_TYPE_TREE:
3851 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3852 break;
3853 case GOT_OBJ_TYPE_COMMIT:
3854 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3855 break;
3856 case GOT_OBJ_TYPE_TAG:
3857 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3858 break;
3859 default:
3860 break;
3862 free(id_str);
3863 tagmsg0 = strdup(got_object_tag_get_message(tag));
3864 got_object_tag_close(tag);
3865 if (tagmsg0 == NULL) {
3866 err = got_error_from_errno("strdup");
3867 break;
3870 tagmsg = tagmsg0;
3871 do {
3872 line = strsep(&tagmsg, "\n");
3873 if (line)
3874 printf(" %s\n", line);
3875 } while (line);
3876 free(tagmsg0);
3879 got_ref_list_free(&refs);
3880 return NULL;
3883 static const struct got_error *
3884 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3885 const char *tag_name, const char *repo_path)
3887 const struct got_error *err = NULL;
3888 char *template = NULL, *initial_content = NULL;
3889 char *editor = NULL;
3890 int fd = -1;
3892 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3893 err = got_error_from_errno("asprintf");
3894 goto done;
3897 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3898 commit_id_str, tag_name) == -1) {
3899 err = got_error_from_errno("asprintf");
3900 goto done;
3903 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3904 if (err)
3905 goto done;
3907 dprintf(fd, initial_content);
3908 close(fd);
3910 err = get_editor(&editor);
3911 if (err)
3912 goto done;
3913 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3914 done:
3915 free(initial_content);
3916 free(template);
3917 free(editor);
3919 /* Editor is done; we can now apply unveil(2) */
3920 if (err == NULL) {
3921 err = apply_unveil(repo_path, 0, NULL);
3922 if (err) {
3923 free(*tagmsg);
3924 *tagmsg = NULL;
3927 return err;
3930 static const struct got_error *
3931 add_tag(struct got_repository *repo, const char *tag_name,
3932 const char *commit_arg, const char *tagmsg_arg)
3934 const struct got_error *err = NULL;
3935 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3936 char *label = NULL, *commit_id_str = NULL;
3937 struct got_reference *ref = NULL;
3938 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3939 char *tagmsg_path = NULL, *tag_id_str = NULL;
3940 int preserve_tagmsg = 0;
3943 * Don't let the user create a tag name with a leading '-'.
3944 * While technically a valid reference name, this case is usually
3945 * an unintended typo.
3947 if (tag_name[0] == '-')
3948 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3950 err = get_author(&tagger, repo);
3951 if (err)
3952 return err;
3954 err = match_object_id(&commit_id, &label, commit_arg,
3955 GOT_OBJ_TYPE_COMMIT, 1, repo);
3956 if (err)
3957 goto done;
3959 err = got_object_id_str(&commit_id_str, commit_id);
3960 if (err)
3961 goto done;
3963 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3964 refname = strdup(tag_name);
3965 if (refname == NULL) {
3966 err = got_error_from_errno("strdup");
3967 goto done;
3969 tag_name += 10;
3970 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3971 err = got_error_from_errno("asprintf");
3972 goto done;
3975 err = got_ref_open(&ref, repo, refname, 0);
3976 if (err == NULL) {
3977 err = got_error(GOT_ERR_TAG_EXISTS);
3978 goto done;
3979 } else if (err->code != GOT_ERR_NOT_REF)
3980 goto done;
3982 if (tagmsg_arg == NULL) {
3983 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3984 tag_name, got_repo_get_path(repo));
3985 if (err) {
3986 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3987 tagmsg_path != NULL)
3988 preserve_tagmsg = 1;
3989 goto done;
3993 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3994 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3995 if (err) {
3996 if (tagmsg_path)
3997 preserve_tagmsg = 1;
3998 goto done;
4001 err = got_ref_alloc(&ref, refname, tag_id);
4002 if (err) {
4003 if (tagmsg_path)
4004 preserve_tagmsg = 1;
4005 goto done;
4008 err = got_ref_write(ref, repo);
4009 if (err) {
4010 if (tagmsg_path)
4011 preserve_tagmsg = 1;
4012 goto done;
4015 err = got_object_id_str(&tag_id_str, tag_id);
4016 if (err) {
4017 if (tagmsg_path)
4018 preserve_tagmsg = 1;
4019 goto done;
4021 printf("Created tag %s\n", tag_id_str);
4022 done:
4023 if (preserve_tagmsg) {
4024 fprintf(stderr, "%s: tag message preserved in %s\n",
4025 getprogname(), tagmsg_path);
4026 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4027 err = got_error_from_errno2("unlink", tagmsg_path);
4028 free(tag_id_str);
4029 if (ref)
4030 got_ref_close(ref);
4031 free(commit_id);
4032 free(commit_id_str);
4033 free(refname);
4034 free(tagmsg);
4035 free(tagmsg_path);
4036 free(tagger);
4037 return err;
4040 static const struct got_error *
4041 cmd_tag(int argc, char *argv[])
4043 const struct got_error *error = NULL;
4044 struct got_repository *repo = NULL;
4045 struct got_worktree *worktree = NULL;
4046 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4047 char *gitconfig_path = NULL;
4048 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4049 int ch, do_list = 0;
4051 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4052 switch (ch) {
4053 case 'm':
4054 tagmsg = optarg;
4055 break;
4056 case 'r':
4057 repo_path = realpath(optarg, NULL);
4058 if (repo_path == NULL)
4059 return got_error_from_errno2("realpath",
4060 optarg);
4061 got_path_strip_trailing_slashes(repo_path);
4062 break;
4063 case 'l':
4064 do_list = 1;
4065 break;
4066 default:
4067 usage_tag();
4068 /* NOTREACHED */
4072 argc -= optind;
4073 argv += optind;
4075 if (do_list) {
4076 if (tagmsg)
4077 errx(1, "-l and -m options are mutually exclusive\n");
4078 if (argc > 0)
4079 usage_tag();
4080 } else if (argc < 1 || argc > 2)
4081 usage_tag();
4082 else if (argc > 1)
4083 commit_id_arg = argv[1];
4084 tag_name = argv[0];
4086 #ifndef PROFILE
4087 if (do_list) {
4088 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4089 NULL) == -1)
4090 err(1, "pledge");
4091 } else {
4092 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4093 "sendfd unveil", NULL) == -1)
4094 err(1, "pledge");
4096 #endif
4097 cwd = getcwd(NULL, 0);
4098 if (cwd == NULL) {
4099 error = got_error_from_errno("getcwd");
4100 goto done;
4103 if (repo_path == NULL) {
4104 error = got_worktree_open(&worktree, cwd);
4105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4106 goto done;
4107 else
4108 error = NULL;
4109 if (worktree) {
4110 repo_path =
4111 strdup(got_worktree_get_repo_path(worktree));
4112 if (repo_path == NULL)
4113 error = got_error_from_errno("strdup");
4114 if (error)
4115 goto done;
4116 } else {
4117 repo_path = strdup(cwd);
4118 if (repo_path == NULL) {
4119 error = got_error_from_errno("strdup");
4120 goto done;
4125 if (do_list) {
4126 error = got_repo_open(&repo, repo_path, NULL);
4127 if (error != NULL)
4128 goto done;
4129 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4130 if (error)
4131 goto done;
4132 error = list_tags(repo, worktree);
4133 } else {
4134 error = get_gitconfig_path(&gitconfig_path);
4135 if (error)
4136 goto done;
4137 error = got_repo_open(&repo, repo_path, gitconfig_path);
4138 if (error != NULL)
4139 goto done;
4141 if (tagmsg) {
4142 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4143 if (error)
4144 goto done;
4147 if (commit_id_arg == NULL) {
4148 struct got_reference *head_ref;
4149 struct got_object_id *commit_id;
4150 error = got_ref_open(&head_ref, repo,
4151 worktree ? got_worktree_get_head_ref_name(worktree)
4152 : GOT_REF_HEAD, 0);
4153 if (error)
4154 goto done;
4155 error = got_ref_resolve(&commit_id, repo, head_ref);
4156 got_ref_close(head_ref);
4157 if (error)
4158 goto done;
4159 error = got_object_id_str(&commit_id_str, commit_id);
4160 free(commit_id);
4161 if (error)
4162 goto done;
4165 error = add_tag(repo, tag_name,
4166 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4168 done:
4169 if (repo)
4170 got_repo_close(repo);
4171 if (worktree)
4172 got_worktree_close(worktree);
4173 free(cwd);
4174 free(repo_path);
4175 free(gitconfig_path);
4176 free(commit_id_str);
4177 return error;
4180 __dead static void
4181 usage_add(void)
4183 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4184 getprogname());
4185 exit(1);
4188 static const struct got_error *
4189 add_progress(void *arg, unsigned char status, const char *path)
4191 while (path[0] == '/')
4192 path++;
4193 printf("%c %s\n", status, path);
4194 return NULL;
4197 static const struct got_error *
4198 cmd_add(int argc, char *argv[])
4200 const struct got_error *error = NULL;
4201 struct got_repository *repo = NULL;
4202 struct got_worktree *worktree = NULL;
4203 char *cwd = NULL;
4204 struct got_pathlist_head paths;
4205 struct got_pathlist_entry *pe;
4206 int ch, can_recurse = 0, no_ignores = 0;
4208 TAILQ_INIT(&paths);
4210 while ((ch = getopt(argc, argv, "IR")) != -1) {
4211 switch (ch) {
4212 case 'I':
4213 no_ignores = 1;
4214 break;
4215 case 'R':
4216 can_recurse = 1;
4217 break;
4218 default:
4219 usage_add();
4220 /* NOTREACHED */
4224 argc -= optind;
4225 argv += optind;
4227 #ifndef PROFILE
4228 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4229 NULL) == -1)
4230 err(1, "pledge");
4231 #endif
4232 if (argc < 1)
4233 usage_add();
4235 cwd = getcwd(NULL, 0);
4236 if (cwd == NULL) {
4237 error = got_error_from_errno("getcwd");
4238 goto done;
4241 error = got_worktree_open(&worktree, cwd);
4242 if (error)
4243 goto done;
4245 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4246 NULL);
4247 if (error != NULL)
4248 goto done;
4250 error = apply_unveil(got_repo_get_path(repo), 1,
4251 got_worktree_get_root_path(worktree));
4252 if (error)
4253 goto done;
4255 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4256 if (error)
4257 goto done;
4259 if (!can_recurse && no_ignores) {
4260 error = got_error_msg(GOT_ERR_BAD_PATH,
4261 "disregarding ignores requires -R option");
4262 goto done;
4266 if (!can_recurse) {
4267 char *ondisk_path;
4268 struct stat sb;
4269 TAILQ_FOREACH(pe, &paths, entry) {
4270 if (asprintf(&ondisk_path, "%s/%s",
4271 got_worktree_get_root_path(worktree),
4272 pe->path) == -1) {
4273 error = got_error_from_errno("asprintf");
4274 goto done;
4276 if (lstat(ondisk_path, &sb) == -1) {
4277 if (errno == ENOENT) {
4278 free(ondisk_path);
4279 continue;
4281 error = got_error_from_errno2("lstat",
4282 ondisk_path);
4283 free(ondisk_path);
4284 goto done;
4286 free(ondisk_path);
4287 if (S_ISDIR(sb.st_mode)) {
4288 error = got_error_msg(GOT_ERR_BAD_PATH,
4289 "adding directories requires -R option");
4290 goto done;
4295 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4296 NULL, repo, no_ignores);
4297 done:
4298 if (repo)
4299 got_repo_close(repo);
4300 if (worktree)
4301 got_worktree_close(worktree);
4302 TAILQ_FOREACH(pe, &paths, entry)
4303 free((char *)pe->path);
4304 got_pathlist_free(&paths);
4305 free(cwd);
4306 return error;
4309 __dead static void
4310 usage_remove(void)
4312 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4313 getprogname());
4314 exit(1);
4317 static const struct got_error *
4318 print_remove_status(void *arg, unsigned char status,
4319 unsigned char staged_status, const char *path)
4321 while (path[0] == '/')
4322 path++;
4323 if (status == GOT_STATUS_NONEXISTENT)
4324 return NULL;
4325 if (status == staged_status && (status == GOT_STATUS_DELETE))
4326 status = GOT_STATUS_NO_CHANGE;
4327 printf("%c%c %s\n", status, staged_status, path);
4328 return NULL;
4331 static const struct got_error *
4332 cmd_remove(int argc, char *argv[])
4334 const struct got_error *error = NULL;
4335 struct got_worktree *worktree = NULL;
4336 struct got_repository *repo = NULL;
4337 char *cwd = NULL;
4338 struct got_pathlist_head paths;
4339 struct got_pathlist_entry *pe;
4340 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4342 TAILQ_INIT(&paths);
4344 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4345 switch (ch) {
4346 case 'f':
4347 delete_local_mods = 1;
4348 break;
4349 case 'k':
4350 keep_on_disk = 1;
4351 break;
4352 case 'R':
4353 can_recurse = 1;
4354 break;
4355 default:
4356 usage_remove();
4357 /* NOTREACHED */
4361 argc -= optind;
4362 argv += optind;
4364 #ifndef PROFILE
4365 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4366 NULL) == -1)
4367 err(1, "pledge");
4368 #endif
4369 if (argc < 1)
4370 usage_remove();
4372 cwd = getcwd(NULL, 0);
4373 if (cwd == NULL) {
4374 error = got_error_from_errno("getcwd");
4375 goto done;
4377 error = got_worktree_open(&worktree, cwd);
4378 if (error)
4379 goto done;
4381 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4382 NULL);
4383 if (error)
4384 goto done;
4386 error = apply_unveil(got_repo_get_path(repo), 1,
4387 got_worktree_get_root_path(worktree));
4388 if (error)
4389 goto done;
4391 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4392 if (error)
4393 goto done;
4395 if (!can_recurse) {
4396 char *ondisk_path;
4397 struct stat sb;
4398 TAILQ_FOREACH(pe, &paths, entry) {
4399 if (asprintf(&ondisk_path, "%s/%s",
4400 got_worktree_get_root_path(worktree),
4401 pe->path) == -1) {
4402 error = got_error_from_errno("asprintf");
4403 goto done;
4405 if (lstat(ondisk_path, &sb) == -1) {
4406 if (errno == ENOENT) {
4407 free(ondisk_path);
4408 continue;
4410 error = got_error_from_errno2("lstat",
4411 ondisk_path);
4412 free(ondisk_path);
4413 goto done;
4415 free(ondisk_path);
4416 if (S_ISDIR(sb.st_mode)) {
4417 error = got_error_msg(GOT_ERR_BAD_PATH,
4418 "removing directories requires -R option");
4419 goto done;
4424 error = got_worktree_schedule_delete(worktree, &paths,
4425 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4426 if (error)
4427 goto done;
4428 done:
4429 if (repo)
4430 got_repo_close(repo);
4431 if (worktree)
4432 got_worktree_close(worktree);
4433 TAILQ_FOREACH(pe, &paths, entry)
4434 free((char *)pe->path);
4435 got_pathlist_free(&paths);
4436 free(cwd);
4437 return error;
4440 __dead static void
4441 usage_revert(void)
4443 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4444 "path ...\n", getprogname());
4445 exit(1);
4448 static const struct got_error *
4449 revert_progress(void *arg, unsigned char status, const char *path)
4451 while (path[0] == '/')
4452 path++;
4453 printf("%c %s\n", status, path);
4454 return NULL;
4457 struct choose_patch_arg {
4458 FILE *patch_script_file;
4459 const char *action;
4462 static const struct got_error *
4463 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4464 int nchanges, const char *action)
4466 char *line = NULL;
4467 size_t linesize = 0;
4468 ssize_t linelen;
4470 switch (status) {
4471 case GOT_STATUS_ADD:
4472 printf("A %s\n%s this addition? [y/n] ", path, action);
4473 break;
4474 case GOT_STATUS_DELETE:
4475 printf("D %s\n%s this deletion? [y/n] ", path, action);
4476 break;
4477 case GOT_STATUS_MODIFY:
4478 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4479 return got_error_from_errno("fseek");
4480 printf(GOT_COMMIT_SEP_STR);
4481 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4482 printf("%s", line);
4483 if (ferror(patch_file))
4484 return got_error_from_errno("getline");
4485 printf(GOT_COMMIT_SEP_STR);
4486 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4487 path, n, nchanges, action);
4488 break;
4489 default:
4490 return got_error_path(path, GOT_ERR_FILE_STATUS);
4493 return NULL;
4496 static const struct got_error *
4497 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4498 FILE *patch_file, int n, int nchanges)
4500 const struct got_error *err = NULL;
4501 char *line = NULL;
4502 size_t linesize = 0;
4503 ssize_t linelen;
4504 int resp = ' ';
4505 struct choose_patch_arg *a = arg;
4507 *choice = GOT_PATCH_CHOICE_NONE;
4509 if (a->patch_script_file) {
4510 char *nl;
4511 err = show_change(status, path, patch_file, n, nchanges,
4512 a->action);
4513 if (err)
4514 return err;
4515 linelen = getline(&line, &linesize, a->patch_script_file);
4516 if (linelen == -1) {
4517 if (ferror(a->patch_script_file))
4518 return got_error_from_errno("getline");
4519 return NULL;
4521 nl = strchr(line, '\n');
4522 if (nl)
4523 *nl = '\0';
4524 if (strcmp(line, "y") == 0) {
4525 *choice = GOT_PATCH_CHOICE_YES;
4526 printf("y\n");
4527 } else if (strcmp(line, "n") == 0) {
4528 *choice = GOT_PATCH_CHOICE_NO;
4529 printf("n\n");
4530 } else if (strcmp(line, "q") == 0 &&
4531 status == GOT_STATUS_MODIFY) {
4532 *choice = GOT_PATCH_CHOICE_QUIT;
4533 printf("q\n");
4534 } else
4535 printf("invalid response '%s'\n", line);
4536 free(line);
4537 return NULL;
4540 while (resp != 'y' && resp != 'n' && resp != 'q') {
4541 err = show_change(status, path, patch_file, n, nchanges,
4542 a->action);
4543 if (err)
4544 return err;
4545 resp = getchar();
4546 if (resp == '\n')
4547 resp = getchar();
4548 if (status == GOT_STATUS_MODIFY) {
4549 if (resp != 'y' && resp != 'n' && resp != 'q') {
4550 printf("invalid response '%c'\n", resp);
4551 resp = ' ';
4553 } else if (resp != 'y' && resp != 'n') {
4554 printf("invalid response '%c'\n", resp);
4555 resp = ' ';
4559 if (resp == 'y')
4560 *choice = GOT_PATCH_CHOICE_YES;
4561 else if (resp == 'n')
4562 *choice = GOT_PATCH_CHOICE_NO;
4563 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4564 *choice = GOT_PATCH_CHOICE_QUIT;
4566 return NULL;
4570 static const struct got_error *
4571 cmd_revert(int argc, char *argv[])
4573 const struct got_error *error = NULL;
4574 struct got_worktree *worktree = NULL;
4575 struct got_repository *repo = NULL;
4576 char *cwd = NULL, *path = NULL;
4577 struct got_pathlist_head paths;
4578 struct got_pathlist_entry *pe;
4579 int ch, can_recurse = 0, pflag = 0;
4580 FILE *patch_script_file = NULL;
4581 const char *patch_script_path = NULL;
4582 struct choose_patch_arg cpa;
4584 TAILQ_INIT(&paths);
4586 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4587 switch (ch) {
4588 case 'p':
4589 pflag = 1;
4590 break;
4591 case 'F':
4592 patch_script_path = optarg;
4593 break;
4594 case 'R':
4595 can_recurse = 1;
4596 break;
4597 default:
4598 usage_revert();
4599 /* NOTREACHED */
4603 argc -= optind;
4604 argv += optind;
4606 #ifndef PROFILE
4607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4608 "unveil", NULL) == -1)
4609 err(1, "pledge");
4610 #endif
4611 if (argc < 1)
4612 usage_revert();
4613 if (patch_script_path && !pflag)
4614 errx(1, "-F option can only be used together with -p option");
4616 cwd = getcwd(NULL, 0);
4617 if (cwd == NULL) {
4618 error = got_error_from_errno("getcwd");
4619 goto done;
4621 error = got_worktree_open(&worktree, cwd);
4622 if (error)
4623 goto done;
4625 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4626 NULL);
4627 if (error != NULL)
4628 goto done;
4630 if (patch_script_path) {
4631 patch_script_file = fopen(patch_script_path, "r");
4632 if (patch_script_file == NULL) {
4633 error = got_error_from_errno2("fopen",
4634 patch_script_path);
4635 goto done;
4638 error = apply_unveil(got_repo_get_path(repo), 1,
4639 got_worktree_get_root_path(worktree));
4640 if (error)
4641 goto done;
4643 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4644 if (error)
4645 goto done;
4647 if (!can_recurse) {
4648 char *ondisk_path;
4649 struct stat sb;
4650 TAILQ_FOREACH(pe, &paths, entry) {
4651 if (asprintf(&ondisk_path, "%s/%s",
4652 got_worktree_get_root_path(worktree),
4653 pe->path) == -1) {
4654 error = got_error_from_errno("asprintf");
4655 goto done;
4657 if (lstat(ondisk_path, &sb) == -1) {
4658 if (errno == ENOENT) {
4659 free(ondisk_path);
4660 continue;
4662 error = got_error_from_errno2("lstat",
4663 ondisk_path);
4664 free(ondisk_path);
4665 goto done;
4667 free(ondisk_path);
4668 if (S_ISDIR(sb.st_mode)) {
4669 error = got_error_msg(GOT_ERR_BAD_PATH,
4670 "reverting directories requires -R option");
4671 goto done;
4676 cpa.patch_script_file = patch_script_file;
4677 cpa.action = "revert";
4678 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4679 pflag ? choose_patch : NULL, &cpa, repo);
4680 if (error)
4681 goto done;
4682 done:
4683 if (patch_script_file && fclose(patch_script_file) == EOF &&
4684 error == NULL)
4685 error = got_error_from_errno2("fclose", patch_script_path);
4686 if (repo)
4687 got_repo_close(repo);
4688 if (worktree)
4689 got_worktree_close(worktree);
4690 free(path);
4691 free(cwd);
4692 return error;
4695 __dead static void
4696 usage_commit(void)
4698 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4699 getprogname());
4700 exit(1);
4703 struct collect_commit_logmsg_arg {
4704 const char *cmdline_log;
4705 const char *editor;
4706 const char *worktree_path;
4707 const char *branch_name;
4708 const char *repo_path;
4709 char *logmsg_path;
4713 static const struct got_error *
4714 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4715 void *arg)
4717 char *initial_content = NULL;
4718 struct got_pathlist_entry *pe;
4719 const struct got_error *err = NULL;
4720 char *template = NULL;
4721 struct collect_commit_logmsg_arg *a = arg;
4722 int fd;
4723 size_t len;
4725 /* if a message was specified on the command line, just use it */
4726 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4727 len = strlen(a->cmdline_log) + 1;
4728 *logmsg = malloc(len + 1);
4729 if (*logmsg == NULL)
4730 return got_error_from_errno("malloc");
4731 strlcpy(*logmsg, a->cmdline_log, len);
4732 return NULL;
4735 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4736 return got_error_from_errno("asprintf");
4738 if (asprintf(&initial_content,
4739 "\n# changes to be committed on branch %s:\n",
4740 a->branch_name) == -1)
4741 return got_error_from_errno("asprintf");
4743 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4744 if (err)
4745 goto done;
4747 dprintf(fd, initial_content);
4749 TAILQ_FOREACH(pe, commitable_paths, entry) {
4750 struct got_commitable *ct = pe->data;
4751 dprintf(fd, "# %c %s\n",
4752 got_commitable_get_status(ct),
4753 got_commitable_get_path(ct));
4755 close(fd);
4757 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4758 done:
4759 free(initial_content);
4760 free(template);
4762 /* Editor is done; we can now apply unveil(2) */
4763 if (err == NULL) {
4764 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4765 if (err) {
4766 free(*logmsg);
4767 *logmsg = NULL;
4770 return err;
4773 static const struct got_error *
4774 cmd_commit(int argc, char *argv[])
4776 const struct got_error *error = NULL;
4777 struct got_worktree *worktree = NULL;
4778 struct got_repository *repo = NULL;
4779 char *cwd = NULL, *id_str = NULL;
4780 struct got_object_id *id = NULL;
4781 const char *logmsg = NULL;
4782 struct collect_commit_logmsg_arg cl_arg;
4783 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4784 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4785 struct got_pathlist_head paths;
4787 TAILQ_INIT(&paths);
4788 cl_arg.logmsg_path = NULL;
4790 while ((ch = getopt(argc, argv, "m:")) != -1) {
4791 switch (ch) {
4792 case 'm':
4793 logmsg = optarg;
4794 break;
4795 default:
4796 usage_commit();
4797 /* NOTREACHED */
4801 argc -= optind;
4802 argv += optind;
4804 #ifndef PROFILE
4805 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 "unveil", NULL) == -1)
4807 err(1, "pledge");
4808 #endif
4809 cwd = getcwd(NULL, 0);
4810 if (cwd == NULL) {
4811 error = got_error_from_errno("getcwd");
4812 goto done;
4814 error = got_worktree_open(&worktree, cwd);
4815 if (error)
4816 goto done;
4818 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4819 if (error)
4820 goto done;
4821 if (rebase_in_progress) {
4822 error = got_error(GOT_ERR_REBASING);
4823 goto done;
4826 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4827 worktree);
4828 if (error)
4829 goto done;
4831 error = get_gitconfig_path(&gitconfig_path);
4832 if (error)
4833 goto done;
4834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4835 gitconfig_path);
4836 if (error != NULL)
4837 goto done;
4839 error = get_author(&author, repo);
4840 if (error)
4841 return error;
4844 * unveil(2) traverses exec(2); if an editor is used we have
4845 * to apply unveil after the log message has been written.
4847 if (logmsg == NULL || strlen(logmsg) == 0)
4848 error = get_editor(&editor);
4849 else
4850 error = apply_unveil(got_repo_get_path(repo), 0,
4851 got_worktree_get_root_path(worktree));
4852 if (error)
4853 goto done;
4855 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4856 if (error)
4857 goto done;
4859 cl_arg.editor = editor;
4860 cl_arg.cmdline_log = logmsg;
4861 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4862 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4863 if (!histedit_in_progress) {
4864 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4865 error = got_error(GOT_ERR_COMMIT_BRANCH);
4866 goto done;
4868 cl_arg.branch_name += 11;
4870 cl_arg.repo_path = got_repo_get_path(repo);
4871 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4872 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4873 if (error) {
4874 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4875 cl_arg.logmsg_path != NULL)
4876 preserve_logmsg = 1;
4877 goto done;
4880 error = got_object_id_str(&id_str, id);
4881 if (error)
4882 goto done;
4883 printf("Created commit %s\n", id_str);
4884 done:
4885 if (preserve_logmsg) {
4886 fprintf(stderr, "%s: log message preserved in %s\n",
4887 getprogname(), cl_arg.logmsg_path);
4888 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4889 error == NULL)
4890 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4891 free(cl_arg.logmsg_path);
4892 if (repo)
4893 got_repo_close(repo);
4894 if (worktree)
4895 got_worktree_close(worktree);
4896 free(cwd);
4897 free(id_str);
4898 free(gitconfig_path);
4899 free(editor);
4900 free(author);
4901 return error;
4904 __dead static void
4905 usage_cherrypick(void)
4907 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4908 exit(1);
4911 static const struct got_error *
4912 cmd_cherrypick(int argc, char *argv[])
4914 const struct got_error *error = NULL;
4915 struct got_worktree *worktree = NULL;
4916 struct got_repository *repo = NULL;
4917 char *cwd = NULL, *commit_id_str = NULL;
4918 struct got_object_id *commit_id = NULL;
4919 struct got_commit_object *commit = NULL;
4920 struct got_object_qid *pid;
4921 struct got_reference *head_ref = NULL;
4922 int ch, did_something = 0;
4924 while ((ch = getopt(argc, argv, "")) != -1) {
4925 switch (ch) {
4926 default:
4927 usage_cherrypick();
4928 /* NOTREACHED */
4932 argc -= optind;
4933 argv += optind;
4935 #ifndef PROFILE
4936 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4937 "unveil", NULL) == -1)
4938 err(1, "pledge");
4939 #endif
4940 if (argc != 1)
4941 usage_cherrypick();
4943 cwd = getcwd(NULL, 0);
4944 if (cwd == NULL) {
4945 error = got_error_from_errno("getcwd");
4946 goto done;
4948 error = got_worktree_open(&worktree, cwd);
4949 if (error)
4950 goto done;
4952 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4953 NULL);
4954 if (error != NULL)
4955 goto done;
4957 error = apply_unveil(got_repo_get_path(repo), 0,
4958 got_worktree_get_root_path(worktree));
4959 if (error)
4960 goto done;
4962 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4963 GOT_OBJ_TYPE_COMMIT, repo);
4964 if (error != NULL) {
4965 struct got_reference *ref;
4966 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4967 goto done;
4968 error = got_ref_open(&ref, repo, argv[0], 0);
4969 if (error != NULL)
4970 goto done;
4971 error = got_ref_resolve(&commit_id, repo, ref);
4972 got_ref_close(ref);
4973 if (error != NULL)
4974 goto done;
4976 error = got_object_id_str(&commit_id_str, commit_id);
4977 if (error)
4978 goto done;
4980 error = got_ref_open(&head_ref, repo,
4981 got_worktree_get_head_ref_name(worktree), 0);
4982 if (error != NULL)
4983 goto done;
4985 error = check_same_branch(commit_id, head_ref, NULL, repo);
4986 if (error) {
4987 if (error->code != GOT_ERR_ANCESTRY)
4988 goto done;
4989 error = NULL;
4990 } else {
4991 error = got_error(GOT_ERR_SAME_BRANCH);
4992 goto done;
4995 error = got_object_open_as_commit(&commit, repo, commit_id);
4996 if (error)
4997 goto done;
4998 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4999 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5000 commit_id, repo, update_progress, &did_something, check_cancelled,
5001 NULL);
5002 if (error != NULL)
5003 goto done;
5005 if (did_something)
5006 printf("Merged commit %s\n", commit_id_str);
5007 done:
5008 if (commit)
5009 got_object_commit_close(commit);
5010 free(commit_id_str);
5011 if (head_ref)
5012 got_ref_close(head_ref);
5013 if (worktree)
5014 got_worktree_close(worktree);
5015 if (repo)
5016 got_repo_close(repo);
5017 return error;
5020 __dead static void
5021 usage_backout(void)
5023 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5024 exit(1);
5027 static const struct got_error *
5028 cmd_backout(int argc, char *argv[])
5030 const struct got_error *error = NULL;
5031 struct got_worktree *worktree = NULL;
5032 struct got_repository *repo = NULL;
5033 char *cwd = NULL, *commit_id_str = NULL;
5034 struct got_object_id *commit_id = NULL;
5035 struct got_commit_object *commit = NULL;
5036 struct got_object_qid *pid;
5037 struct got_reference *head_ref = NULL;
5038 int ch, did_something = 0;
5040 while ((ch = getopt(argc, argv, "")) != -1) {
5041 switch (ch) {
5042 default:
5043 usage_backout();
5044 /* NOTREACHED */
5048 argc -= optind;
5049 argv += optind;
5051 #ifndef PROFILE
5052 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5053 "unveil", NULL) == -1)
5054 err(1, "pledge");
5055 #endif
5056 if (argc != 1)
5057 usage_backout();
5059 cwd = getcwd(NULL, 0);
5060 if (cwd == NULL) {
5061 error = got_error_from_errno("getcwd");
5062 goto done;
5064 error = got_worktree_open(&worktree, cwd);
5065 if (error)
5066 goto done;
5068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5069 NULL);
5070 if (error != NULL)
5071 goto done;
5073 error = apply_unveil(got_repo_get_path(repo), 0,
5074 got_worktree_get_root_path(worktree));
5075 if (error)
5076 goto done;
5078 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5079 GOT_OBJ_TYPE_COMMIT, repo);
5080 if (error != NULL) {
5081 struct got_reference *ref;
5082 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5083 goto done;
5084 error = got_ref_open(&ref, repo, argv[0], 0);
5085 if (error != NULL)
5086 goto done;
5087 error = got_ref_resolve(&commit_id, repo, ref);
5088 got_ref_close(ref);
5089 if (error != NULL)
5090 goto done;
5092 error = got_object_id_str(&commit_id_str, commit_id);
5093 if (error)
5094 goto done;
5096 error = got_ref_open(&head_ref, repo,
5097 got_worktree_get_head_ref_name(worktree), 0);
5098 if (error != NULL)
5099 goto done;
5101 error = check_same_branch(commit_id, head_ref, NULL, repo);
5102 if (error)
5103 goto done;
5105 error = got_object_open_as_commit(&commit, repo, commit_id);
5106 if (error)
5107 goto done;
5108 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5109 if (pid == NULL) {
5110 error = got_error(GOT_ERR_ROOT_COMMIT);
5111 goto done;
5114 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5115 update_progress, &did_something, check_cancelled, NULL);
5116 if (error != NULL)
5117 goto done;
5119 if (did_something)
5120 printf("Backed out commit %s\n", commit_id_str);
5121 done:
5122 if (commit)
5123 got_object_commit_close(commit);
5124 free(commit_id_str);
5125 if (head_ref)
5126 got_ref_close(head_ref);
5127 if (worktree)
5128 got_worktree_close(worktree);
5129 if (repo)
5130 got_repo_close(repo);
5131 return error;
5134 __dead static void
5135 usage_rebase(void)
5137 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5138 getprogname());
5139 exit(1);
5142 void
5143 trim_logmsg(char *logmsg, int limit)
5145 char *nl;
5146 size_t len;
5148 len = strlen(logmsg);
5149 if (len > limit)
5150 len = limit;
5151 logmsg[len] = '\0';
5152 nl = strchr(logmsg, '\n');
5153 if (nl)
5154 *nl = '\0';
5157 static const struct got_error *
5158 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5160 const struct got_error *err;
5161 char *logmsg0 = NULL;
5162 const char *s;
5164 err = got_object_commit_get_logmsg(&logmsg0, commit);
5165 if (err)
5166 return err;
5168 s = logmsg0;
5169 while (isspace((unsigned char)s[0]))
5170 s++;
5172 *logmsg = strdup(s);
5173 if (*logmsg == NULL) {
5174 err = got_error_from_errno("strdup");
5175 goto done;
5178 trim_logmsg(*logmsg, limit);
5179 done:
5180 free(logmsg0);
5181 return err;
5184 static const struct got_error *
5185 show_rebase_progress(struct got_commit_object *commit,
5186 struct got_object_id *old_id, struct got_object_id *new_id)
5188 const struct got_error *err;
5189 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5191 err = got_object_id_str(&old_id_str, old_id);
5192 if (err)
5193 goto done;
5195 if (new_id) {
5196 err = got_object_id_str(&new_id_str, new_id);
5197 if (err)
5198 goto done;
5201 old_id_str[12] = '\0';
5202 if (new_id_str)
5203 new_id_str[12] = '\0';
5205 err = get_short_logmsg(&logmsg, 42, commit);
5206 if (err)
5207 goto done;
5209 printf("%s -> %s: %s\n", old_id_str,
5210 new_id_str ? new_id_str : "no-op change", logmsg);
5211 done:
5212 free(old_id_str);
5213 free(new_id_str);
5214 return err;
5217 static const struct got_error *
5218 rebase_progress(void *arg, unsigned char status, const char *path)
5220 unsigned char *rebase_status = arg;
5222 while (path[0] == '/')
5223 path++;
5224 printf("%c %s\n", status, path);
5226 if (*rebase_status == GOT_STATUS_CONFLICT)
5227 return NULL;
5228 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5229 *rebase_status = status;
5230 return NULL;
5233 static const struct got_error *
5234 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5235 struct got_reference *branch, struct got_reference *new_base_branch,
5236 struct got_reference *tmp_branch, struct got_repository *repo)
5238 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5239 return got_worktree_rebase_complete(worktree, fileindex,
5240 new_base_branch, tmp_branch, branch, repo);
5243 static const struct got_error *
5244 rebase_commit(struct got_pathlist_head *merged_paths,
5245 struct got_worktree *worktree, struct got_fileindex *fileindex,
5246 struct got_reference *tmp_branch,
5247 struct got_object_id *commit_id, struct got_repository *repo)
5249 const struct got_error *error;
5250 struct got_commit_object *commit;
5251 struct got_object_id *new_commit_id;
5253 error = got_object_open_as_commit(&commit, repo, commit_id);
5254 if (error)
5255 return error;
5257 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5258 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5259 if (error) {
5260 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5261 goto done;
5262 error = show_rebase_progress(commit, commit_id, NULL);
5263 } else {
5264 error = show_rebase_progress(commit, commit_id, new_commit_id);
5265 free(new_commit_id);
5267 done:
5268 got_object_commit_close(commit);
5269 return error;
5272 struct check_path_prefix_arg {
5273 const char *path_prefix;
5274 size_t len;
5275 int errcode;
5278 static const struct got_error *
5279 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5280 struct got_blob_object *blob2, struct got_object_id *id1,
5281 struct got_object_id *id2, const char *path1, const char *path2,
5282 mode_t mode1, mode_t mode2, struct got_repository *repo)
5284 struct check_path_prefix_arg *a = arg;
5286 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5287 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5288 return got_error(a->errcode);
5290 return NULL;
5293 static const struct got_error *
5294 check_path_prefix(struct got_object_id *parent_id,
5295 struct got_object_id *commit_id, const char *path_prefix,
5296 int errcode, struct got_repository *repo)
5298 const struct got_error *err;
5299 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5300 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5301 struct check_path_prefix_arg cpp_arg;
5303 if (got_path_is_root_dir(path_prefix))
5304 return NULL;
5306 err = got_object_open_as_commit(&commit, repo, commit_id);
5307 if (err)
5308 goto done;
5310 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5311 if (err)
5312 goto done;
5314 err = got_object_open_as_tree(&tree1, repo,
5315 got_object_commit_get_tree_id(parent_commit));
5316 if (err)
5317 goto done;
5319 err = got_object_open_as_tree(&tree2, repo,
5320 got_object_commit_get_tree_id(commit));
5321 if (err)
5322 goto done;
5324 cpp_arg.path_prefix = path_prefix;
5325 while (cpp_arg.path_prefix[0] == '/')
5326 cpp_arg.path_prefix++;
5327 cpp_arg.len = strlen(cpp_arg.path_prefix);
5328 cpp_arg.errcode = errcode;
5329 err = got_diff_tree(tree1, tree2, "", "", repo,
5330 check_path_prefix_in_diff, &cpp_arg, 0);
5331 done:
5332 if (tree1)
5333 got_object_tree_close(tree1);
5334 if (tree2)
5335 got_object_tree_close(tree2);
5336 if (commit)
5337 got_object_commit_close(commit);
5338 if (parent_commit)
5339 got_object_commit_close(parent_commit);
5340 return err;
5343 static const struct got_error *
5344 collect_commits(struct got_object_id_queue *commits,
5345 struct got_object_id *initial_commit_id,
5346 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5347 const char *path_prefix, int path_prefix_errcode,
5348 struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 struct got_commit_graph *graph = NULL;
5352 struct got_object_id *parent_id = NULL;
5353 struct got_object_qid *qid;
5354 struct got_object_id *commit_id = initial_commit_id;
5356 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5357 if (err)
5358 return err;
5360 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5361 check_cancelled, NULL);
5362 if (err)
5363 goto done;
5364 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5365 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5366 check_cancelled, NULL);
5367 if (err) {
5368 if (err->code == GOT_ERR_ITER_COMPLETED) {
5369 err = got_error_msg(GOT_ERR_ANCESTRY,
5370 "ran out of commits to rebase before "
5371 "youngest common ancestor commit has "
5372 "been reached?!?");
5374 goto done;
5375 } else {
5376 err = check_path_prefix(parent_id, commit_id,
5377 path_prefix, path_prefix_errcode, repo);
5378 if (err)
5379 goto done;
5381 err = got_object_qid_alloc(&qid, commit_id);
5382 if (err)
5383 goto done;
5384 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5385 commit_id = parent_id;
5388 done:
5389 got_commit_graph_close(graph);
5390 return err;
5393 static const struct got_error *
5394 cmd_rebase(int argc, char *argv[])
5396 const struct got_error *error = NULL;
5397 struct got_worktree *worktree = NULL;
5398 struct got_repository *repo = NULL;
5399 struct got_fileindex *fileindex = NULL;
5400 char *cwd = NULL;
5401 struct got_reference *branch = NULL;
5402 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5403 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5404 struct got_object_id *resume_commit_id = NULL;
5405 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5406 struct got_commit_object *commit = NULL;
5407 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5408 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5409 struct got_object_id_queue commits;
5410 struct got_pathlist_head merged_paths;
5411 const struct got_object_id_queue *parent_ids;
5412 struct got_object_qid *qid, *pid;
5414 SIMPLEQ_INIT(&commits);
5415 TAILQ_INIT(&merged_paths);
5417 while ((ch = getopt(argc, argv, "ac")) != -1) {
5418 switch (ch) {
5419 case 'a':
5420 abort_rebase = 1;
5421 break;
5422 case 'c':
5423 continue_rebase = 1;
5424 break;
5425 default:
5426 usage_rebase();
5427 /* NOTREACHED */
5431 argc -= optind;
5432 argv += optind;
5434 #ifndef PROFILE
5435 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5436 "unveil", NULL) == -1)
5437 err(1, "pledge");
5438 #endif
5439 if (abort_rebase && continue_rebase)
5440 usage_rebase();
5441 else if (abort_rebase || continue_rebase) {
5442 if (argc != 0)
5443 usage_rebase();
5444 } else if (argc != 1)
5445 usage_rebase();
5447 cwd = getcwd(NULL, 0);
5448 if (cwd == NULL) {
5449 error = got_error_from_errno("getcwd");
5450 goto done;
5452 error = got_worktree_open(&worktree, cwd);
5453 if (error)
5454 goto done;
5456 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5457 NULL);
5458 if (error != NULL)
5459 goto done;
5461 error = apply_unveil(got_repo_get_path(repo), 0,
5462 got_worktree_get_root_path(worktree));
5463 if (error)
5464 goto done;
5466 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5467 if (error)
5468 goto done;
5470 if (abort_rebase) {
5471 int did_something;
5472 if (!rebase_in_progress) {
5473 error = got_error(GOT_ERR_NOT_REBASING);
5474 goto done;
5476 error = got_worktree_rebase_continue(&resume_commit_id,
5477 &new_base_branch, &tmp_branch, &branch, &fileindex,
5478 worktree, repo);
5479 if (error)
5480 goto done;
5481 printf("Switching work tree to %s\n",
5482 got_ref_get_symref_target(new_base_branch));
5483 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5484 new_base_branch, update_progress, &did_something);
5485 if (error)
5486 goto done;
5487 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5488 goto done; /* nothing else to do */
5491 if (continue_rebase) {
5492 if (!rebase_in_progress) {
5493 error = got_error(GOT_ERR_NOT_REBASING);
5494 goto done;
5496 error = got_worktree_rebase_continue(&resume_commit_id,
5497 &new_base_branch, &tmp_branch, &branch, &fileindex,
5498 worktree, repo);
5499 if (error)
5500 goto done;
5502 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5503 resume_commit_id, repo);
5504 if (error)
5505 goto done;
5507 yca_id = got_object_id_dup(resume_commit_id);
5508 if (yca_id == NULL) {
5509 error = got_error_from_errno("got_object_id_dup");
5510 goto done;
5512 } else {
5513 error = got_ref_open(&branch, repo, argv[0], 0);
5514 if (error != NULL)
5515 goto done;
5518 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5519 if (error)
5520 goto done;
5522 if (!continue_rebase) {
5523 struct got_object_id *base_commit_id;
5525 base_commit_id = got_worktree_get_base_commit_id(worktree);
5526 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5527 base_commit_id, branch_head_commit_id, repo,
5528 check_cancelled, NULL);
5529 if (error)
5530 goto done;
5531 if (yca_id == NULL) {
5532 error = got_error_msg(GOT_ERR_ANCESTRY,
5533 "specified branch shares no common ancestry "
5534 "with work tree's branch");
5535 goto done;
5538 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5539 if (error) {
5540 if (error->code != GOT_ERR_ANCESTRY)
5541 goto done;
5542 error = NULL;
5543 } else {
5544 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5545 "specified branch resolves to a commit which "
5546 "is already contained in work tree's branch");
5547 goto done;
5549 error = got_worktree_rebase_prepare(&new_base_branch,
5550 &tmp_branch, &fileindex, worktree, branch, repo);
5551 if (error)
5552 goto done;
5555 commit_id = branch_head_commit_id;
5556 error = got_object_open_as_commit(&commit, repo, commit_id);
5557 if (error)
5558 goto done;
5560 parent_ids = got_object_commit_get_parent_ids(commit);
5561 pid = SIMPLEQ_FIRST(parent_ids);
5562 if (pid == NULL) {
5563 if (!continue_rebase) {
5564 int did_something;
5565 error = got_worktree_rebase_abort(worktree, fileindex,
5566 repo, new_base_branch, update_progress,
5567 &did_something);
5568 if (error)
5569 goto done;
5570 printf("Rebase of %s aborted\n",
5571 got_ref_get_name(branch));
5573 error = got_error(GOT_ERR_EMPTY_REBASE);
5574 goto done;
5576 error = collect_commits(&commits, commit_id, pid->id,
5577 yca_id, got_worktree_get_path_prefix(worktree),
5578 GOT_ERR_REBASE_PATH, repo);
5579 got_object_commit_close(commit);
5580 commit = NULL;
5581 if (error)
5582 goto done;
5584 if (SIMPLEQ_EMPTY(&commits)) {
5585 if (continue_rebase) {
5586 error = rebase_complete(worktree, fileindex,
5587 branch, new_base_branch, tmp_branch, repo);
5588 goto done;
5589 } else {
5590 /* Fast-forward the reference of the branch. */
5591 struct got_object_id *new_head_commit_id;
5592 char *id_str;
5593 error = got_ref_resolve(&new_head_commit_id, repo,
5594 new_base_branch);
5595 if (error)
5596 goto done;
5597 error = got_object_id_str(&id_str, new_head_commit_id);
5598 printf("Forwarding %s to commit %s\n",
5599 got_ref_get_name(branch), id_str);
5600 free(id_str);
5601 error = got_ref_change_ref(branch,
5602 new_head_commit_id);
5603 if (error)
5604 goto done;
5608 pid = NULL;
5609 SIMPLEQ_FOREACH(qid, &commits, entry) {
5610 commit_id = qid->id;
5611 parent_id = pid ? pid->id : yca_id;
5612 pid = qid;
5614 error = got_worktree_rebase_merge_files(&merged_paths,
5615 worktree, fileindex, parent_id, commit_id, repo,
5616 rebase_progress, &rebase_status, check_cancelled, NULL);
5617 if (error)
5618 goto done;
5620 if (rebase_status == GOT_STATUS_CONFLICT) {
5621 got_worktree_rebase_pathlist_free(&merged_paths);
5622 break;
5625 error = rebase_commit(&merged_paths, worktree, fileindex,
5626 tmp_branch, commit_id, repo);
5627 got_worktree_rebase_pathlist_free(&merged_paths);
5628 if (error)
5629 goto done;
5632 if (rebase_status == GOT_STATUS_CONFLICT) {
5633 error = got_worktree_rebase_postpone(worktree, fileindex);
5634 if (error)
5635 goto done;
5636 error = got_error_msg(GOT_ERR_CONFLICTS,
5637 "conflicts must be resolved before rebasing can continue");
5638 } else
5639 error = rebase_complete(worktree, fileindex, branch,
5640 new_base_branch, tmp_branch, repo);
5641 done:
5642 got_object_id_queue_free(&commits);
5643 free(branch_head_commit_id);
5644 free(resume_commit_id);
5645 free(yca_id);
5646 if (commit)
5647 got_object_commit_close(commit);
5648 if (branch)
5649 got_ref_close(branch);
5650 if (new_base_branch)
5651 got_ref_close(new_base_branch);
5652 if (tmp_branch)
5653 got_ref_close(tmp_branch);
5654 if (worktree)
5655 got_worktree_close(worktree);
5656 if (repo)
5657 got_repo_close(repo);
5658 return error;
5661 __dead static void
5662 usage_histedit(void)
5664 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5665 getprogname());
5666 exit(1);
5669 #define GOT_HISTEDIT_PICK 'p'
5670 #define GOT_HISTEDIT_EDIT 'e'
5671 #define GOT_HISTEDIT_FOLD 'f'
5672 #define GOT_HISTEDIT_DROP 'd'
5673 #define GOT_HISTEDIT_MESG 'm'
5675 static struct got_histedit_cmd {
5676 unsigned char code;
5677 const char *name;
5678 const char *desc;
5679 } got_histedit_cmds[] = {
5680 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5681 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5682 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5683 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5684 { GOT_HISTEDIT_MESG, "mesg",
5685 "single-line log message for commit above (open editor if empty)" },
5688 struct got_histedit_list_entry {
5689 TAILQ_ENTRY(got_histedit_list_entry) entry;
5690 struct got_object_id *commit_id;
5691 const struct got_histedit_cmd *cmd;
5692 char *logmsg;
5694 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5696 static const struct got_error *
5697 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5698 FILE *f, struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 char *logmsg = NULL, *id_str = NULL;
5702 struct got_commit_object *commit = NULL;
5703 int n;
5705 err = got_object_open_as_commit(&commit, repo, commit_id);
5706 if (err)
5707 goto done;
5709 err = get_short_logmsg(&logmsg, 34, commit);
5710 if (err)
5711 goto done;
5713 err = got_object_id_str(&id_str, commit_id);
5714 if (err)
5715 goto done;
5717 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5718 if (n < 0)
5719 err = got_ferror(f, GOT_ERR_IO);
5720 done:
5721 if (commit)
5722 got_object_commit_close(commit);
5723 free(id_str);
5724 free(logmsg);
5725 return err;
5728 static const struct got_error *
5729 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5730 struct got_repository *repo)
5732 const struct got_error *err = NULL;
5733 struct got_object_qid *qid;
5735 if (SIMPLEQ_EMPTY(commits))
5736 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5738 SIMPLEQ_FOREACH(qid, commits, entry) {
5739 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5740 f, repo);
5741 if (err)
5742 break;
5745 return err;
5748 static const struct got_error *
5749 write_cmd_list(FILE *f)
5751 const struct got_error *err = NULL;
5752 int n, i;
5754 n = fprintf(f, "# Available histedit commands:\n");
5755 if (n < 0)
5756 return got_ferror(f, GOT_ERR_IO);
5758 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5759 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5760 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5761 cmd->desc);
5762 if (n < 0) {
5763 err = got_ferror(f, GOT_ERR_IO);
5764 break;
5767 n = fprintf(f, "# Commits will be processed in order from top to "
5768 "bottom of this file.\n");
5769 if (n < 0)
5770 return got_ferror(f, GOT_ERR_IO);
5771 return err;
5774 static const struct got_error *
5775 histedit_syntax_error(int lineno)
5777 static char msg[42];
5778 int ret;
5780 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5781 lineno);
5782 if (ret == -1 || ret >= sizeof(msg))
5783 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5785 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5788 static const struct got_error *
5789 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5790 char *logmsg, struct got_repository *repo)
5792 const struct got_error *err;
5793 struct got_commit_object *folded_commit = NULL;
5794 char *id_str, *folded_logmsg = NULL;
5796 err = got_object_id_str(&id_str, hle->commit_id);
5797 if (err)
5798 return err;
5800 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5801 if (err)
5802 goto done;
5804 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5805 if (err)
5806 goto done;
5807 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5808 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5809 folded_logmsg) == -1) {
5810 err = got_error_from_errno("asprintf");
5811 goto done;
5813 done:
5814 if (folded_commit)
5815 got_object_commit_close(folded_commit);
5816 free(id_str);
5817 free(folded_logmsg);
5818 return err;
5821 static struct got_histedit_list_entry *
5822 get_folded_commits(struct got_histedit_list_entry *hle)
5824 struct got_histedit_list_entry *prev, *folded = NULL;
5826 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5827 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5828 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5829 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5830 folded = prev;
5831 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5834 return folded;
5837 static const struct got_error *
5838 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5839 struct got_repository *repo)
5841 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5842 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5843 const struct got_error *err = NULL;
5844 struct got_commit_object *commit = NULL;
5845 int fd;
5846 struct got_histedit_list_entry *folded = NULL;
5848 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5849 if (err)
5850 return err;
5852 folded = get_folded_commits(hle);
5853 if (folded) {
5854 while (folded != hle) {
5855 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5856 folded = TAILQ_NEXT(folded, entry);
5857 continue;
5859 err = append_folded_commit_msg(&new_msg, folded,
5860 logmsg, repo);
5861 if (err)
5862 goto done;
5863 free(logmsg);
5864 logmsg = new_msg;
5865 folded = TAILQ_NEXT(folded, entry);
5869 err = got_object_id_str(&id_str, hle->commit_id);
5870 if (err)
5871 goto done;
5872 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5873 if (err)
5874 goto done;
5875 if (asprintf(&new_msg,
5876 "%s\n# original log message of commit %s: %s",
5877 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5878 err = got_error_from_errno("asprintf");
5879 goto done;
5881 free(logmsg);
5882 logmsg = new_msg;
5884 err = got_object_id_str(&id_str, hle->commit_id);
5885 if (err)
5886 goto done;
5888 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5889 if (err)
5890 goto done;
5892 dprintf(fd, logmsg);
5893 close(fd);
5895 err = get_editor(&editor);
5896 if (err)
5897 goto done;
5899 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5900 if (err) {
5901 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5902 goto done;
5903 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5905 done:
5906 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5907 err = got_error_from_errno2("unlink", logmsg_path);
5908 free(logmsg_path);
5909 free(logmsg);
5910 free(orig_logmsg);
5911 free(editor);
5912 if (commit)
5913 got_object_commit_close(commit);
5914 return err;
5917 static const struct got_error *
5918 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5919 FILE *f, struct got_repository *repo)
5921 const struct got_error *err = NULL;
5922 char *line = NULL, *p, *end;
5923 size_t size;
5924 ssize_t len;
5925 int lineno = 0, i;
5926 const struct got_histedit_cmd *cmd;
5927 struct got_object_id *commit_id = NULL;
5928 struct got_histedit_list_entry *hle = NULL;
5930 for (;;) {
5931 len = getline(&line, &size, f);
5932 if (len == -1) {
5933 const struct got_error *getline_err;
5934 if (feof(f))
5935 break;
5936 getline_err = got_error_from_errno("getline");
5937 err = got_ferror(f, getline_err->code);
5938 break;
5940 lineno++;
5941 p = line;
5942 while (isspace((unsigned char)p[0]))
5943 p++;
5944 if (p[0] == '#' || p[0] == '\0') {
5945 free(line);
5946 line = NULL;
5947 continue;
5949 cmd = NULL;
5950 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5951 cmd = &got_histedit_cmds[i];
5952 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5953 isspace((unsigned char)p[strlen(cmd->name)])) {
5954 p += strlen(cmd->name);
5955 break;
5957 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5958 p++;
5959 break;
5962 if (i == nitems(got_histedit_cmds)) {
5963 err = histedit_syntax_error(lineno);
5964 break;
5966 while (isspace((unsigned char)p[0]))
5967 p++;
5968 if (cmd->code == GOT_HISTEDIT_MESG) {
5969 if (hle == NULL || hle->logmsg != NULL) {
5970 err = got_error(GOT_ERR_HISTEDIT_CMD);
5971 break;
5973 if (p[0] == '\0') {
5974 err = histedit_edit_logmsg(hle, repo);
5975 if (err)
5976 break;
5977 } else {
5978 hle->logmsg = strdup(p);
5979 if (hle->logmsg == NULL) {
5980 err = got_error_from_errno("strdup");
5981 break;
5984 free(line);
5985 line = NULL;
5986 continue;
5987 } else {
5988 end = p;
5989 while (end[0] && !isspace((unsigned char)end[0]))
5990 end++;
5991 *end = '\0';
5993 err = got_object_resolve_id_str(&commit_id, repo, p);
5994 if (err) {
5995 /* override error code */
5996 err = histedit_syntax_error(lineno);
5997 break;
6000 hle = malloc(sizeof(*hle));
6001 if (hle == NULL) {
6002 err = got_error_from_errno("malloc");
6003 break;
6005 hle->cmd = cmd;
6006 hle->commit_id = commit_id;
6007 hle->logmsg = NULL;
6008 commit_id = NULL;
6009 free(line);
6010 line = NULL;
6011 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6014 free(line);
6015 free(commit_id);
6016 return err;
6019 static const struct got_error *
6020 histedit_check_script(struct got_histedit_list *histedit_cmds,
6021 struct got_object_id_queue *commits, struct got_repository *repo)
6023 const struct got_error *err = NULL;
6024 struct got_object_qid *qid;
6025 struct got_histedit_list_entry *hle;
6026 static char msg[80];
6027 char *id_str;
6029 if (TAILQ_EMPTY(histedit_cmds))
6030 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6031 "histedit script contains no commands");
6032 if (SIMPLEQ_EMPTY(commits))
6033 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6035 SIMPLEQ_FOREACH(qid, commits, entry) {
6036 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6037 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6038 break;
6040 if (hle == NULL) {
6041 err = got_object_id_str(&id_str, qid->id);
6042 if (err)
6043 return err;
6044 snprintf(msg, sizeof(msg),
6045 "commit %s missing from histedit script", id_str);
6046 free(id_str);
6047 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6051 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6052 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6053 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6054 "last commit in histedit script cannot be folded");
6056 return NULL;
6059 static const struct got_error *
6060 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6061 const char *path, struct got_object_id_queue *commits,
6062 struct got_repository *repo)
6064 const struct got_error *err = NULL;
6065 char *editor;
6066 FILE *f = NULL;
6068 err = get_editor(&editor);
6069 if (err)
6070 return err;
6072 if (spawn_editor(editor, path) == -1) {
6073 err = got_error_from_errno("failed spawning editor");
6074 goto done;
6077 f = fopen(path, "r");
6078 if (f == NULL) {
6079 err = got_error_from_errno("fopen");
6080 goto done;
6082 err = histedit_parse_list(histedit_cmds, f, repo);
6083 if (err)
6084 goto done;
6086 err = histedit_check_script(histedit_cmds, commits, repo);
6087 done:
6088 if (f && fclose(f) != 0 && err == NULL)
6089 err = got_error_from_errno("fclose");
6090 free(editor);
6091 return err;
6094 static const struct got_error *
6095 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6096 struct got_object_id_queue *, const char *, struct got_repository *);
6098 static const struct got_error *
6099 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6100 struct got_object_id_queue *commits, struct got_repository *repo)
6102 const struct got_error *err;
6103 FILE *f = NULL;
6104 char *path = NULL;
6106 err = got_opentemp_named(&path, &f, "got-histedit");
6107 if (err)
6108 return err;
6110 err = write_cmd_list(f);
6111 if (err)
6112 goto done;
6114 err = histedit_write_commit_list(commits, f, repo);
6115 if (err)
6116 goto done;
6118 if (fclose(f) != 0) {
6119 err = got_error_from_errno("fclose");
6120 goto done;
6122 f = NULL;
6124 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6125 if (err) {
6126 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6127 err->code != GOT_ERR_HISTEDIT_CMD)
6128 goto done;
6129 err = histedit_edit_list_retry(histedit_cmds, err,
6130 commits, path, repo);
6132 done:
6133 if (f && fclose(f) != 0 && err == NULL)
6134 err = got_error_from_errno("fclose");
6135 if (path && unlink(path) != 0 && err == NULL)
6136 err = got_error_from_errno2("unlink", path);
6137 free(path);
6138 return err;
6141 static const struct got_error *
6142 histedit_save_list(struct got_histedit_list *histedit_cmds,
6143 struct got_worktree *worktree, struct got_repository *repo)
6145 const struct got_error *err = NULL;
6146 char *path = NULL;
6147 FILE *f = NULL;
6148 struct got_histedit_list_entry *hle;
6149 struct got_commit_object *commit = NULL;
6151 err = got_worktree_get_histedit_script_path(&path, worktree);
6152 if (err)
6153 return err;
6155 f = fopen(path, "w");
6156 if (f == NULL) {
6157 err = got_error_from_errno2("fopen", path);
6158 goto done;
6160 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6161 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6162 repo);
6163 if (err)
6164 break;
6166 if (hle->logmsg) {
6167 int n = fprintf(f, "%c %s\n",
6168 GOT_HISTEDIT_MESG, hle->logmsg);
6169 if (n < 0) {
6170 err = got_ferror(f, GOT_ERR_IO);
6171 break;
6175 done:
6176 if (f && fclose(f) != 0 && err == NULL)
6177 err = got_error_from_errno("fclose");
6178 free(path);
6179 if (commit)
6180 got_object_commit_close(commit);
6181 return err;
6184 void
6185 histedit_free_list(struct got_histedit_list *histedit_cmds)
6187 struct got_histedit_list_entry *hle;
6189 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6190 TAILQ_REMOVE(histedit_cmds, hle, entry);
6191 free(hle);
6195 static const struct got_error *
6196 histedit_load_list(struct got_histedit_list *histedit_cmds,
6197 const char *path, struct got_repository *repo)
6199 const struct got_error *err = NULL;
6200 FILE *f = NULL;
6202 f = fopen(path, "r");
6203 if (f == NULL) {
6204 err = got_error_from_errno2("fopen", path);
6205 goto done;
6208 err = histedit_parse_list(histedit_cmds, f, repo);
6209 done:
6210 if (f && fclose(f) != 0 && err == NULL)
6211 err = got_error_from_errno("fclose");
6212 return err;
6215 static const struct got_error *
6216 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6217 const struct got_error *edit_err, struct got_object_id_queue *commits,
6218 const char *path, struct got_repository *repo)
6220 const struct got_error *err = NULL, *prev_err = edit_err;
6221 int resp = ' ';
6223 while (resp != 'c' && resp != 'r' && resp != 'a') {
6224 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6225 "or (a)bort: ", getprogname(), prev_err->msg);
6226 resp = getchar();
6227 if (resp == '\n')
6228 resp = getchar();
6229 if (resp == 'c') {
6230 histedit_free_list(histedit_cmds);
6231 err = histedit_run_editor(histedit_cmds, path, commits,
6232 repo);
6233 if (err) {
6234 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6235 err->code != GOT_ERR_HISTEDIT_CMD)
6236 break;
6237 prev_err = err;
6238 resp = ' ';
6239 continue;
6241 break;
6242 } else if (resp == 'r') {
6243 histedit_free_list(histedit_cmds);
6244 err = histedit_edit_script(histedit_cmds,
6245 commits, repo);
6246 if (err) {
6247 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6248 err->code != GOT_ERR_HISTEDIT_CMD)
6249 break;
6250 prev_err = err;
6251 resp = ' ';
6252 continue;
6254 break;
6255 } else if (resp == 'a') {
6256 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6257 break;
6258 } else
6259 printf("invalid response '%c'\n", resp);
6262 return err;
6265 static const struct got_error *
6266 histedit_complete(struct got_worktree *worktree,
6267 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6268 struct got_reference *branch, struct got_repository *repo)
6270 printf("Switching work tree to %s\n",
6271 got_ref_get_symref_target(branch));
6272 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6273 branch, repo);
6276 static const struct got_error *
6277 show_histedit_progress(struct got_commit_object *commit,
6278 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6280 const struct got_error *err;
6281 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6283 err = got_object_id_str(&old_id_str, hle->commit_id);
6284 if (err)
6285 goto done;
6287 if (new_id) {
6288 err = got_object_id_str(&new_id_str, new_id);
6289 if (err)
6290 goto done;
6293 old_id_str[12] = '\0';
6294 if (new_id_str)
6295 new_id_str[12] = '\0';
6297 if (hle->logmsg) {
6298 logmsg = strdup(hle->logmsg);
6299 if (logmsg == NULL) {
6300 err = got_error_from_errno("strdup");
6301 goto done;
6303 trim_logmsg(logmsg, 42);
6304 } else {
6305 err = get_short_logmsg(&logmsg, 42, commit);
6306 if (err)
6307 goto done;
6310 switch (hle->cmd->code) {
6311 case GOT_HISTEDIT_PICK:
6312 case GOT_HISTEDIT_EDIT:
6313 printf("%s -> %s: %s\n", old_id_str,
6314 new_id_str ? new_id_str : "no-op change", logmsg);
6315 break;
6316 case GOT_HISTEDIT_DROP:
6317 case GOT_HISTEDIT_FOLD:
6318 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6319 logmsg);
6320 break;
6321 default:
6322 break;
6325 done:
6326 free(old_id_str);
6327 free(new_id_str);
6328 return err;
6331 static const struct got_error *
6332 histedit_commit(struct got_pathlist_head *merged_paths,
6333 struct got_worktree *worktree, struct got_fileindex *fileindex,
6334 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6335 struct got_repository *repo)
6337 const struct got_error *err;
6338 struct got_commit_object *commit;
6339 struct got_object_id *new_commit_id;
6341 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6342 && hle->logmsg == NULL) {
6343 err = histedit_edit_logmsg(hle, repo);
6344 if (err)
6345 return err;
6348 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6349 if (err)
6350 return err;
6352 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6353 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6354 hle->logmsg, repo);
6355 if (err) {
6356 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6357 goto done;
6358 err = show_histedit_progress(commit, hle, NULL);
6359 } else {
6360 err = show_histedit_progress(commit, hle, new_commit_id);
6361 free(new_commit_id);
6363 done:
6364 got_object_commit_close(commit);
6365 return err;
6368 static const struct got_error *
6369 histedit_skip_commit(struct got_histedit_list_entry *hle,
6370 struct got_worktree *worktree, struct got_repository *repo)
6372 const struct got_error *error;
6373 struct got_commit_object *commit;
6375 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6376 repo);
6377 if (error)
6378 return error;
6380 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6381 if (error)
6382 return error;
6384 error = show_histedit_progress(commit, hle, NULL);
6385 got_object_commit_close(commit);
6386 return error;
6389 static const struct got_error *
6390 cmd_histedit(int argc, char *argv[])
6392 const struct got_error *error = NULL;
6393 struct got_worktree *worktree = NULL;
6394 struct got_fileindex *fileindex = NULL;
6395 struct got_repository *repo = NULL;
6396 char *cwd = NULL;
6397 struct got_reference *branch = NULL;
6398 struct got_reference *tmp_branch = NULL;
6399 struct got_object_id *resume_commit_id = NULL;
6400 struct got_object_id *base_commit_id = NULL;
6401 struct got_object_id *head_commit_id = NULL;
6402 struct got_commit_object *commit = NULL;
6403 int ch, rebase_in_progress = 0, did_something;
6404 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6405 const char *edit_script_path = NULL;
6406 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6407 struct got_object_id_queue commits;
6408 struct got_pathlist_head merged_paths;
6409 const struct got_object_id_queue *parent_ids;
6410 struct got_object_qid *pid;
6411 struct got_histedit_list histedit_cmds;
6412 struct got_histedit_list_entry *hle;
6414 SIMPLEQ_INIT(&commits);
6415 TAILQ_INIT(&histedit_cmds);
6416 TAILQ_INIT(&merged_paths);
6418 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6419 switch (ch) {
6420 case 'a':
6421 abort_edit = 1;
6422 break;
6423 case 'c':
6424 continue_edit = 1;
6425 break;
6426 case 'F':
6427 edit_script_path = optarg;
6428 break;
6429 default:
6430 usage_histedit();
6431 /* NOTREACHED */
6435 argc -= optind;
6436 argv += optind;
6438 #ifndef PROFILE
6439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6440 "unveil", NULL) == -1)
6441 err(1, "pledge");
6442 #endif
6443 if (abort_edit && continue_edit)
6444 usage_histedit();
6445 if (argc != 0)
6446 usage_histedit();
6449 * This command cannot apply unveil(2) in all cases because the
6450 * user may choose to run an editor to edit the histedit script
6451 * and to edit individual commit log messages.
6452 * unveil(2) traverses exec(2); if an editor is used we have to
6453 * apply unveil after edit script and log messages have been written.
6454 * XXX TODO: Make use of unveil(2) where possible.
6457 cwd = getcwd(NULL, 0);
6458 if (cwd == NULL) {
6459 error = got_error_from_errno("getcwd");
6460 goto done;
6462 error = got_worktree_open(&worktree, cwd);
6463 if (error)
6464 goto done;
6466 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6467 NULL);
6468 if (error != NULL)
6469 goto done;
6471 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6472 if (error)
6473 goto done;
6474 if (rebase_in_progress) {
6475 error = got_error(GOT_ERR_REBASING);
6476 goto done;
6479 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6480 if (error)
6481 goto done;
6483 if (edit_in_progress && abort_edit) {
6484 error = got_worktree_histedit_continue(&resume_commit_id,
6485 &tmp_branch, &branch, &base_commit_id, &fileindex,
6486 worktree, repo);
6487 if (error)
6488 goto done;
6489 printf("Switching work tree to %s\n",
6490 got_ref_get_symref_target(branch));
6491 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6492 branch, base_commit_id, update_progress, &did_something);
6493 if (error)
6494 goto done;
6495 printf("Histedit of %s aborted\n",
6496 got_ref_get_symref_target(branch));
6497 goto done; /* nothing else to do */
6498 } else if (abort_edit) {
6499 error = got_error(GOT_ERR_NOT_HISTEDIT);
6500 goto done;
6503 if (continue_edit) {
6504 char *path;
6506 if (!edit_in_progress) {
6507 error = got_error(GOT_ERR_NOT_HISTEDIT);
6508 goto done;
6511 error = got_worktree_get_histedit_script_path(&path, worktree);
6512 if (error)
6513 goto done;
6515 error = histedit_load_list(&histedit_cmds, path, repo);
6516 free(path);
6517 if (error)
6518 goto done;
6520 error = got_worktree_histedit_continue(&resume_commit_id,
6521 &tmp_branch, &branch, &base_commit_id, &fileindex,
6522 worktree, repo);
6523 if (error)
6524 goto done;
6526 error = got_ref_resolve(&head_commit_id, repo, branch);
6527 if (error)
6528 goto done;
6530 error = got_object_open_as_commit(&commit, repo,
6531 head_commit_id);
6532 if (error)
6533 goto done;
6534 parent_ids = got_object_commit_get_parent_ids(commit);
6535 pid = SIMPLEQ_FIRST(parent_ids);
6536 if (pid == NULL) {
6537 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6538 goto done;
6540 error = collect_commits(&commits, head_commit_id, pid->id,
6541 base_commit_id, got_worktree_get_path_prefix(worktree),
6542 GOT_ERR_HISTEDIT_PATH, repo);
6543 got_object_commit_close(commit);
6544 commit = NULL;
6545 if (error)
6546 goto done;
6547 } else {
6548 if (edit_in_progress) {
6549 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6550 goto done;
6553 error = got_ref_open(&branch, repo,
6554 got_worktree_get_head_ref_name(worktree), 0);
6555 if (error != NULL)
6556 goto done;
6558 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6559 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6560 "will not edit commit history of a branch outside "
6561 "the \"refs/heads/\" reference namespace");
6562 goto done;
6565 error = got_ref_resolve(&head_commit_id, repo, branch);
6566 got_ref_close(branch);
6567 branch = NULL;
6568 if (error)
6569 goto done;
6571 error = got_object_open_as_commit(&commit, repo,
6572 head_commit_id);
6573 if (error)
6574 goto done;
6575 parent_ids = got_object_commit_get_parent_ids(commit);
6576 pid = SIMPLEQ_FIRST(parent_ids);
6577 if (pid == NULL) {
6578 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6579 goto done;
6581 error = collect_commits(&commits, head_commit_id, pid->id,
6582 got_worktree_get_base_commit_id(worktree),
6583 got_worktree_get_path_prefix(worktree),
6584 GOT_ERR_HISTEDIT_PATH, repo);
6585 got_object_commit_close(commit);
6586 commit = NULL;
6587 if (error)
6588 goto done;
6590 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6591 &base_commit_id, &fileindex, worktree, repo);
6592 if (error)
6593 goto done;
6595 if (edit_script_path) {
6596 error = histedit_load_list(&histedit_cmds,
6597 edit_script_path, 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;
6604 } else {
6605 error = histedit_edit_script(&histedit_cmds, &commits,
6606 repo);
6607 if (error) {
6608 got_worktree_histedit_abort(worktree, fileindex,
6609 repo, branch, base_commit_id,
6610 update_progress, &did_something);
6611 goto done;
6616 error = histedit_save_list(&histedit_cmds, worktree,
6617 repo);
6618 if (error) {
6619 got_worktree_histedit_abort(worktree, fileindex,
6620 repo, branch, base_commit_id,
6621 update_progress, &did_something);
6622 goto done;
6627 error = histedit_check_script(&histedit_cmds, &commits, repo);
6628 if (error)
6629 goto done;
6631 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6632 if (resume_commit_id) {
6633 if (got_object_id_cmp(hle->commit_id,
6634 resume_commit_id) != 0)
6635 continue;
6637 resume_commit_id = NULL;
6638 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6639 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6640 error = histedit_skip_commit(hle, worktree,
6641 repo);
6642 } else {
6643 error = histedit_commit(NULL, worktree,
6644 fileindex, tmp_branch, hle, repo);
6646 if (error)
6647 goto done;
6648 continue;
6651 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6652 error = histedit_skip_commit(hle, worktree, repo);
6653 if (error)
6654 goto done;
6655 continue;
6658 error = got_object_open_as_commit(&commit, repo,
6659 hle->commit_id);
6660 if (error)
6661 goto done;
6662 parent_ids = got_object_commit_get_parent_ids(commit);
6663 pid = SIMPLEQ_FIRST(parent_ids);
6665 error = got_worktree_histedit_merge_files(&merged_paths,
6666 worktree, fileindex, pid->id, hle->commit_id, repo,
6667 rebase_progress, &rebase_status, check_cancelled, NULL);
6668 if (error)
6669 goto done;
6670 got_object_commit_close(commit);
6671 commit = NULL;
6673 if (rebase_status == GOT_STATUS_CONFLICT) {
6674 got_worktree_rebase_pathlist_free(&merged_paths);
6675 break;
6678 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6679 char *id_str;
6680 error = got_object_id_str(&id_str, hle->commit_id);
6681 if (error)
6682 goto done;
6683 printf("Stopping histedit for amending commit %s\n",
6684 id_str);
6685 free(id_str);
6686 got_worktree_rebase_pathlist_free(&merged_paths);
6687 error = got_worktree_histedit_postpone(worktree,
6688 fileindex);
6689 goto done;
6692 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6693 error = histedit_skip_commit(hle, worktree, repo);
6694 if (error)
6695 goto done;
6696 continue;
6699 error = histedit_commit(&merged_paths, worktree, fileindex,
6700 tmp_branch, hle, repo);
6701 got_worktree_rebase_pathlist_free(&merged_paths);
6702 if (error)
6703 goto done;
6706 if (rebase_status == GOT_STATUS_CONFLICT) {
6707 error = got_worktree_histedit_postpone(worktree, fileindex);
6708 if (error)
6709 goto done;
6710 error = got_error_msg(GOT_ERR_CONFLICTS,
6711 "conflicts must be resolved before rebasing can continue");
6712 } else
6713 error = histedit_complete(worktree, fileindex, tmp_branch,
6714 branch, repo);
6715 done:
6716 got_object_id_queue_free(&commits);
6717 histedit_free_list(&histedit_cmds);
6718 free(head_commit_id);
6719 free(base_commit_id);
6720 free(resume_commit_id);
6721 if (commit)
6722 got_object_commit_close(commit);
6723 if (branch)
6724 got_ref_close(branch);
6725 if (tmp_branch)
6726 got_ref_close(tmp_branch);
6727 if (worktree)
6728 got_worktree_close(worktree);
6729 if (repo)
6730 got_repo_close(repo);
6731 return error;
6734 __dead static void
6735 usage_integrate(void)
6737 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6738 exit(1);
6741 static const struct got_error *
6742 cmd_integrate(int argc, char *argv[])
6744 const struct got_error *error = NULL;
6745 struct got_repository *repo = NULL;
6746 struct got_worktree *worktree = NULL;
6747 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6748 const char *branch_arg = NULL;
6749 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6750 struct got_fileindex *fileindex = NULL;
6751 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6752 int ch, did_something = 0;
6754 while ((ch = getopt(argc, argv, "")) != -1) {
6755 switch (ch) {
6756 default:
6757 usage_integrate();
6758 /* NOTREACHED */
6762 argc -= optind;
6763 argv += optind;
6765 if (argc != 1)
6766 usage_integrate();
6767 branch_arg = argv[0];
6769 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6770 "unveil", NULL) == -1)
6771 err(1, "pledge");
6773 cwd = getcwd(NULL, 0);
6774 if (cwd == NULL) {
6775 error = got_error_from_errno("getcwd");
6776 goto done;
6779 error = got_worktree_open(&worktree, cwd);
6780 if (error)
6781 goto done;
6783 error = check_rebase_or_histedit_in_progress(worktree);
6784 if (error)
6785 goto done;
6787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6788 NULL);
6789 if (error != NULL)
6790 goto done;
6792 error = apply_unveil(got_repo_get_path(repo), 0,
6793 got_worktree_get_root_path(worktree));
6794 if (error)
6795 goto done;
6797 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6798 error = got_error_from_errno("asprintf");
6799 goto done;
6802 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6803 &base_branch_ref, worktree, refname, repo);
6804 if (error)
6805 goto done;
6807 refname = strdup(got_ref_get_name(branch_ref));
6808 if (refname == NULL) {
6809 error = got_error_from_errno("strdup");
6810 got_worktree_integrate_abort(worktree, fileindex, repo,
6811 branch_ref, base_branch_ref);
6812 goto done;
6814 base_refname = strdup(got_ref_get_name(base_branch_ref));
6815 if (base_refname == NULL) {
6816 error = got_error_from_errno("strdup");
6817 got_worktree_integrate_abort(worktree, fileindex, repo,
6818 branch_ref, base_branch_ref);
6819 goto done;
6822 error = got_ref_resolve(&commit_id, repo, branch_ref);
6823 if (error)
6824 goto done;
6826 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6827 if (error)
6828 goto done;
6830 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6831 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6832 "specified branch has already been integrated");
6833 got_worktree_integrate_abort(worktree, fileindex, repo,
6834 branch_ref, base_branch_ref);
6835 goto done;
6838 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6839 if (error) {
6840 if (error->code == GOT_ERR_ANCESTRY)
6841 error = got_error(GOT_ERR_REBASE_REQUIRED);
6842 got_worktree_integrate_abort(worktree, fileindex, repo,
6843 branch_ref, base_branch_ref);
6844 goto done;
6847 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6848 branch_ref, base_branch_ref, update_progress, &did_something,
6849 check_cancelled, NULL);
6850 if (error)
6851 goto done;
6853 printf("Integrated %s into %s\n", refname, base_refname);
6854 done:
6855 if (repo)
6856 got_repo_close(repo);
6857 if (worktree)
6858 got_worktree_close(worktree);
6859 free(cwd);
6860 free(base_commit_id);
6861 free(commit_id);
6862 free(refname);
6863 free(base_refname);
6864 return error;
6867 __dead static void
6868 usage_stage(void)
6870 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6871 "[file-path ...]\n",
6872 getprogname());
6873 exit(1);
6876 static const struct got_error *
6877 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6878 const char *path, struct got_object_id *blob_id,
6879 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6880 int dirfd, const char *de_name)
6882 const struct got_error *err = NULL;
6883 char *id_str = NULL;
6885 if (staged_status != GOT_STATUS_ADD &&
6886 staged_status != GOT_STATUS_MODIFY &&
6887 staged_status != GOT_STATUS_DELETE)
6888 return NULL;
6890 if (staged_status == GOT_STATUS_ADD ||
6891 staged_status == GOT_STATUS_MODIFY)
6892 err = got_object_id_str(&id_str, staged_blob_id);
6893 else
6894 err = got_object_id_str(&id_str, blob_id);
6895 if (err)
6896 return err;
6898 printf("%s %c %s\n", id_str, staged_status, path);
6899 free(id_str);
6900 return NULL;
6903 static const struct got_error *
6904 cmd_stage(int argc, char *argv[])
6906 const struct got_error *error = NULL;
6907 struct got_repository *repo = NULL;
6908 struct got_worktree *worktree = NULL;
6909 char *cwd = NULL;
6910 struct got_pathlist_head paths;
6911 struct got_pathlist_entry *pe;
6912 int ch, list_stage = 0, pflag = 0;
6913 FILE *patch_script_file = NULL;
6914 const char *patch_script_path = NULL;
6915 struct choose_patch_arg cpa;
6917 TAILQ_INIT(&paths);
6919 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6920 switch (ch) {
6921 case 'l':
6922 list_stage = 1;
6923 break;
6924 case 'p':
6925 pflag = 1;
6926 break;
6927 case 'F':
6928 patch_script_path = optarg;
6929 break;
6930 default:
6931 usage_stage();
6932 /* NOTREACHED */
6936 argc -= optind;
6937 argv += optind;
6939 #ifndef PROFILE
6940 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6941 "unveil", NULL) == -1)
6942 err(1, "pledge");
6943 #endif
6944 if (list_stage && (pflag || patch_script_path))
6945 errx(1, "-l option cannot be used with other options");
6946 if (patch_script_path && !pflag)
6947 errx(1, "-F option can only be used together with -p option");
6949 cwd = getcwd(NULL, 0);
6950 if (cwd == NULL) {
6951 error = got_error_from_errno("getcwd");
6952 goto done;
6955 error = got_worktree_open(&worktree, cwd);
6956 if (error)
6957 goto done;
6959 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6960 NULL);
6961 if (error != NULL)
6962 goto done;
6964 if (patch_script_path) {
6965 patch_script_file = fopen(patch_script_path, "r");
6966 if (patch_script_file == NULL) {
6967 error = got_error_from_errno2("fopen",
6968 patch_script_path);
6969 goto done;
6972 error = apply_unveil(got_repo_get_path(repo), 0,
6973 got_worktree_get_root_path(worktree));
6974 if (error)
6975 goto done;
6977 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6978 if (error)
6979 goto done;
6981 if (list_stage)
6982 error = got_worktree_status(worktree, &paths, repo,
6983 print_stage, NULL, check_cancelled, NULL);
6984 else {
6985 cpa.patch_script_file = patch_script_file;
6986 cpa.action = "stage";
6987 error = got_worktree_stage(worktree, &paths,
6988 pflag ? NULL : print_status, NULL,
6989 pflag ? choose_patch : NULL, &cpa, repo);
6991 done:
6992 if (patch_script_file && fclose(patch_script_file) == EOF &&
6993 error == NULL)
6994 error = got_error_from_errno2("fclose", patch_script_path);
6995 if (repo)
6996 got_repo_close(repo);
6997 if (worktree)
6998 got_worktree_close(worktree);
6999 TAILQ_FOREACH(pe, &paths, entry)
7000 free((char *)pe->path);
7001 got_pathlist_free(&paths);
7002 free(cwd);
7003 return error;
7006 __dead static void
7007 usage_unstage(void)
7009 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7010 "[file-path ...]\n",
7011 getprogname());
7012 exit(1);
7016 static const struct got_error *
7017 cmd_unstage(int argc, char *argv[])
7019 const struct got_error *error = NULL;
7020 struct got_repository *repo = NULL;
7021 struct got_worktree *worktree = NULL;
7022 char *cwd = NULL;
7023 struct got_pathlist_head paths;
7024 struct got_pathlist_entry *pe;
7025 int ch, did_something = 0, pflag = 0;
7026 FILE *patch_script_file = NULL;
7027 const char *patch_script_path = NULL;
7028 struct choose_patch_arg cpa;
7030 TAILQ_INIT(&paths);
7032 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7033 switch (ch) {
7034 case 'p':
7035 pflag = 1;
7036 break;
7037 case 'F':
7038 patch_script_path = optarg;
7039 break;
7040 default:
7041 usage_unstage();
7042 /* NOTREACHED */
7046 argc -= optind;
7047 argv += optind;
7049 #ifndef PROFILE
7050 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7051 "unveil", NULL) == -1)
7052 err(1, "pledge");
7053 #endif
7054 if (patch_script_path && !pflag)
7055 errx(1, "-F option can only be used together with -p option");
7057 cwd = getcwd(NULL, 0);
7058 if (cwd == NULL) {
7059 error = got_error_from_errno("getcwd");
7060 goto done;
7063 error = got_worktree_open(&worktree, cwd);
7064 if (error)
7065 goto done;
7067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7068 NULL);
7069 if (error != NULL)
7070 goto done;
7072 if (patch_script_path) {
7073 patch_script_file = fopen(patch_script_path, "r");
7074 if (patch_script_file == NULL) {
7075 error = got_error_from_errno2("fopen",
7076 patch_script_path);
7077 goto done;
7081 error = apply_unveil(got_repo_get_path(repo), 0,
7082 got_worktree_get_root_path(worktree));
7083 if (error)
7084 goto done;
7086 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7087 if (error)
7088 goto done;
7090 cpa.patch_script_file = patch_script_file;
7091 cpa.action = "unstage";
7092 error = got_worktree_unstage(worktree, &paths, update_progress,
7093 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7094 done:
7095 if (patch_script_file && fclose(patch_script_file) == EOF &&
7096 error == NULL)
7097 error = got_error_from_errno2("fclose", patch_script_path);
7098 if (repo)
7099 got_repo_close(repo);
7100 if (worktree)
7101 got_worktree_close(worktree);
7102 TAILQ_FOREACH(pe, &paths, entry)
7103 free((char *)pe->path);
7104 got_pathlist_free(&paths);
7105 free(cwd);
7106 return error;
7109 __dead static void
7110 usage_cat(void)
7112 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7113 "arg1 [arg2 ...]\n", getprogname());
7114 exit(1);
7117 static const struct got_error *
7118 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7120 const struct got_error *err;
7121 struct got_blob_object *blob;
7123 err = got_object_open_as_blob(&blob, repo, id, 8192);
7124 if (err)
7125 return err;
7127 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7128 got_object_blob_close(blob);
7129 return err;
7132 static const struct got_error *
7133 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7135 const struct got_error *err;
7136 struct got_tree_object *tree;
7137 int nentries, i;
7139 err = got_object_open_as_tree(&tree, repo, id);
7140 if (err)
7141 return err;
7143 nentries = got_object_tree_get_nentries(tree);
7144 for (i = 0; i < nentries; i++) {
7145 struct got_tree_entry *te;
7146 char *id_str;
7147 if (sigint_received || sigpipe_received)
7148 break;
7149 te = got_object_tree_get_entry(tree, i);
7150 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7151 if (err)
7152 break;
7153 fprintf(outfile, "%s %.7o %s\n", id_str,
7154 got_tree_entry_get_mode(te),
7155 got_tree_entry_get_name(te));
7156 free(id_str);
7159 got_object_tree_close(tree);
7160 return err;
7163 static const struct got_error *
7164 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7166 const struct got_error *err;
7167 struct got_commit_object *commit;
7168 const struct got_object_id_queue *parent_ids;
7169 struct got_object_qid *pid;
7170 char *id_str = NULL;
7171 const char *logmsg = NULL;
7173 err = got_object_open_as_commit(&commit, repo, id);
7174 if (err)
7175 return err;
7177 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7178 if (err)
7179 goto done;
7181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7182 parent_ids = got_object_commit_get_parent_ids(commit);
7183 fprintf(outfile, "numparents %d\n",
7184 got_object_commit_get_nparents(commit));
7185 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7186 char *pid_str;
7187 err = got_object_id_str(&pid_str, pid->id);
7188 if (err)
7189 goto done;
7190 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7191 free(pid_str);
7193 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7194 got_object_commit_get_author(commit),
7195 got_object_commit_get_author_time(commit));
7197 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7198 got_object_commit_get_author(commit),
7199 got_object_commit_get_committer_time(commit));
7201 logmsg = got_object_commit_get_logmsg_raw(commit);
7202 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7203 fprintf(outfile, "%s", logmsg);
7204 done:
7205 free(id_str);
7206 got_object_commit_close(commit);
7207 return err;
7210 static const struct got_error *
7211 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7213 const struct got_error *err;
7214 struct got_tag_object *tag;
7215 char *id_str = NULL;
7216 const char *tagmsg = NULL;
7218 err = got_object_open_as_tag(&tag, repo, id);
7219 if (err)
7220 return err;
7222 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7223 if (err)
7224 goto done;
7226 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7228 switch (got_object_tag_get_object_type(tag)) {
7229 case GOT_OBJ_TYPE_BLOB:
7230 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7231 GOT_OBJ_LABEL_BLOB);
7232 break;
7233 case GOT_OBJ_TYPE_TREE:
7234 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7235 GOT_OBJ_LABEL_TREE);
7236 break;
7237 case GOT_OBJ_TYPE_COMMIT:
7238 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7239 GOT_OBJ_LABEL_COMMIT);
7240 break;
7241 case GOT_OBJ_TYPE_TAG:
7242 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7243 GOT_OBJ_LABEL_TAG);
7244 break;
7245 default:
7246 break;
7249 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7250 got_object_tag_get_name(tag));
7252 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7253 got_object_tag_get_tagger(tag),
7254 got_object_tag_get_tagger_time(tag));
7256 tagmsg = got_object_tag_get_message(tag);
7257 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7258 fprintf(outfile, "%s", tagmsg);
7259 done:
7260 free(id_str);
7261 got_object_tag_close(tag);
7262 return err;
7265 static const struct got_error *
7266 cmd_cat(int argc, char *argv[])
7268 const struct got_error *error;
7269 struct got_repository *repo = NULL;
7270 struct got_worktree *worktree = NULL;
7271 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7272 const char *commit_id_str = NULL;
7273 struct got_object_id *id = NULL, *commit_id = NULL;
7274 int ch, obj_type, i, force_path = 0;
7276 #ifndef PROFILE
7277 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7278 NULL) == -1)
7279 err(1, "pledge");
7280 #endif
7282 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7283 switch (ch) {
7284 case 'c':
7285 commit_id_str = optarg;
7286 break;
7287 case 'r':
7288 repo_path = realpath(optarg, NULL);
7289 if (repo_path == NULL)
7290 return got_error_from_errno2("realpath",
7291 optarg);
7292 got_path_strip_trailing_slashes(repo_path);
7293 break;
7294 case 'P':
7295 force_path = 1;
7296 break;
7297 default:
7298 usage_cat();
7299 /* NOTREACHED */
7303 argc -= optind;
7304 argv += optind;
7306 cwd = getcwd(NULL, 0);
7307 if (cwd == NULL) {
7308 error = got_error_from_errno("getcwd");
7309 goto done;
7311 error = got_worktree_open(&worktree, cwd);
7312 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7313 goto done;
7314 if (worktree) {
7315 if (repo_path == NULL) {
7316 repo_path = strdup(
7317 got_worktree_get_repo_path(worktree));
7318 if (repo_path == NULL) {
7319 error = got_error_from_errno("strdup");
7320 goto done;
7325 if (repo_path == NULL) {
7326 repo_path = getcwd(NULL, 0);
7327 if (repo_path == NULL)
7328 return got_error_from_errno("getcwd");
7331 error = got_repo_open(&repo, repo_path, NULL);
7332 free(repo_path);
7333 if (error != NULL)
7334 goto done;
7336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7337 if (error)
7338 goto done;
7340 if (commit_id_str == NULL)
7341 commit_id_str = GOT_REF_HEAD;
7342 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7343 if (error)
7344 goto done;
7346 for (i = 0; i < argc; i++) {
7347 if (force_path) {
7348 error = got_object_id_by_path(&id, repo, commit_id,
7349 argv[i]);
7350 if (error)
7351 break;
7352 } else {
7353 error = match_object_id(&id, &label, argv[i],
7354 GOT_OBJ_TYPE_ANY, 0, repo);
7355 if (error) {
7356 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7357 error->code != GOT_ERR_NOT_REF)
7358 break;
7359 error = got_object_id_by_path(&id, repo,
7360 commit_id, argv[i]);
7361 if (error)
7362 break;
7366 error = got_object_get_type(&obj_type, repo, id);
7367 if (error)
7368 break;
7370 switch (obj_type) {
7371 case GOT_OBJ_TYPE_BLOB:
7372 error = cat_blob(id, repo, stdout);
7373 break;
7374 case GOT_OBJ_TYPE_TREE:
7375 error = cat_tree(id, repo, stdout);
7376 break;
7377 case GOT_OBJ_TYPE_COMMIT:
7378 error = cat_commit(id, repo, stdout);
7379 break;
7380 case GOT_OBJ_TYPE_TAG:
7381 error = cat_tag(id, repo, stdout);
7382 break;
7383 default:
7384 error = got_error(GOT_ERR_OBJ_TYPE);
7385 break;
7387 if (error)
7388 break;
7389 free(label);
7390 label = NULL;
7391 free(id);
7392 id = NULL;
7395 done:
7396 free(label);
7397 free(id);
7398 free(commit_id);
7399 if (worktree)
7400 got_worktree_close(worktree);
7401 if (repo) {
7402 const struct got_error *repo_error;
7403 repo_error = got_repo_close(repo);
7404 if (error == NULL)
7405 error = repo_error;
7407 return error;