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);
931 if (err) {
932 if (err->code == GOT_ERR_ITER_COMPLETED) {
933 err = NULL;
934 break;
935 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
936 break;
937 err = got_commit_graph_fetch_commits(graph, 1,
938 repo, check_cancelled, NULL);
939 if (err)
940 break;
943 if (id) {
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
945 break;
946 if (got_object_id_cmp(id, commit_id) == 0) {
947 is_same_branch = 1;
948 break;
952 done:
953 if (graph)
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
958 return err;
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
971 if (err == NULL) {
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
977 return err;
978 } else if (err->code != GOT_ERR_NO_OBJ)
979 return err;
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
982 if (err == NULL) {
983 err = got_ref_resolve(commit_id, repo, ref);
984 got_ref_close(ref);
985 } else {
986 if (err->code != GOT_ERR_NOT_REF)
987 return err;
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
991 return err;
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1010 TAILQ_INIT(&paths);
1012 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1013 switch (ch) {
1014 case 'b':
1015 branch_name = optarg;
1016 break;
1017 case 'c':
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1021 break;
1022 case 'p':
1023 path_prefix = optarg;
1024 break;
1025 default:
1026 usage_checkout();
1027 /* NOTREACHED */
1031 argc -= optind;
1032 argv += optind;
1034 #ifndef PROFILE
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1037 err(1, "pledge");
1038 #endif
1039 if (argc == 1) {
1040 char *cwd, *base, *dotgit;
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL)
1043 return got_error_from_errno2("realpath", argv[0]);
1044 cwd = getcwd(NULL, 0);
1045 if (cwd == NULL) {
1046 error = got_error_from_errno("getcwd");
1047 goto done;
1049 if (path_prefix[0]) {
1050 base = basename(path_prefix);
1051 if (base == NULL) {
1052 error = got_error_from_errno2("basename",
1053 path_prefix);
1054 goto done;
1056 } else {
1057 base = basename(repo_path);
1058 if (base == NULL) {
1059 error = got_error_from_errno2("basename",
1060 repo_path);
1061 goto done;
1064 dotgit = strstr(base, ".git");
1065 if (dotgit)
1066 *dotgit = '\0';
1067 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1068 error = got_error_from_errno("asprintf");
1069 free(cwd);
1070 goto done;
1072 free(cwd);
1073 } else if (argc == 2) {
1074 repo_path = realpath(argv[0], NULL);
1075 if (repo_path == NULL) {
1076 error = got_error_from_errno2("realpath", argv[0]);
1077 goto done;
1079 worktree_path = realpath(argv[1], NULL);
1080 if (worktree_path == NULL) {
1081 if (errno != ENOENT) {
1082 error = got_error_from_errno2("realpath",
1083 argv[1]);
1084 goto done;
1086 worktree_path = strdup(argv[1]);
1087 if (worktree_path == NULL) {
1088 error = got_error_from_errno("strdup");
1089 goto done;
1092 } else
1093 usage_checkout();
1095 got_path_strip_trailing_slashes(repo_path);
1096 got_path_strip_trailing_slashes(worktree_path);
1098 error = got_repo_open(&repo, repo_path, NULL);
1099 if (error != NULL)
1100 goto done;
1102 /* Pre-create work tree path for unveil(2) */
1103 error = got_path_mkdir(worktree_path);
1104 if (error) {
1105 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1106 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1107 goto done;
1108 if (!got_path_dir_is_empty(worktree_path)) {
1109 error = got_error_path(worktree_path,
1110 GOT_ERR_DIR_NOT_EMPTY);
1111 goto done;
1115 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1116 if (error)
1117 goto done;
1119 error = got_ref_open(&head_ref, repo, branch_name, 0);
1120 if (error != NULL)
1121 goto done;
1123 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1124 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1125 goto done;
1127 error = got_worktree_open(&worktree, worktree_path);
1128 if (error != NULL)
1129 goto done;
1131 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1132 path_prefix);
1133 if (error != NULL)
1134 goto done;
1135 if (!same_path_prefix) {
1136 error = got_error(GOT_ERR_PATH_PREFIX);
1137 goto done;
1140 if (commit_id_str) {
1141 struct got_object_id *commit_id;
1142 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1143 if (error)
1144 goto done;
1145 error = check_linear_ancestry(commit_id,
1146 got_worktree_get_base_commit_id(worktree), 0, repo);
1147 if (error != NULL) {
1148 free(commit_id);
1149 goto done;
1151 error = check_same_branch(commit_id, head_ref, NULL, repo);
1152 if (error)
1153 goto done;
1154 error = got_worktree_set_base_commit_id(worktree, repo,
1155 commit_id);
1156 free(commit_id);
1157 if (error)
1158 goto done;
1161 error = got_pathlist_append(&paths, "", NULL);
1162 if (error)
1163 goto done;
1164 cpa.worktree_path = worktree_path;
1165 cpa.had_base_commit_ref_error = 0;
1166 error = got_worktree_checkout_files(worktree, &paths, repo,
1167 checkout_progress, &cpa, check_cancelled, NULL);
1168 if (error != NULL)
1169 goto done;
1171 printf("Now shut up and hack\n");
1172 if (cpa.had_base_commit_ref_error)
1173 show_worktree_base_ref_warning();
1175 done:
1176 got_pathlist_free(&paths);
1177 free(commit_id_str);
1178 free(repo_path);
1179 free(worktree_path);
1180 return error;
1183 __dead static void
1184 usage_update(void)
1186 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1187 getprogname());
1188 exit(1);
1191 static const struct got_error *
1192 update_progress(void *arg, unsigned char status, const char *path)
1194 int *did_something = arg;
1196 if (status == GOT_STATUS_EXISTS ||
1197 status == GOT_STATUS_BASE_REF_ERR)
1198 return NULL;
1200 *did_something = 1;
1202 /* Base commit bump happens silently. */
1203 if (status == GOT_STATUS_BUMP_BASE)
1204 return NULL;
1206 while (path[0] == '/')
1207 path++;
1208 printf("%c %s\n", status, path);
1209 return NULL;
1212 static const struct got_error *
1213 switch_head_ref(struct got_reference *head_ref,
1214 struct got_object_id *commit_id, struct got_worktree *worktree,
1215 struct got_repository *repo)
1217 const struct got_error *err = NULL;
1218 char *base_id_str;
1219 int ref_has_moved = 0;
1221 /* Trivial case: switching between two different references. */
1222 if (strcmp(got_ref_get_name(head_ref),
1223 got_worktree_get_head_ref_name(worktree)) != 0) {
1224 printf("Switching work tree from %s to %s\n",
1225 got_worktree_get_head_ref_name(worktree),
1226 got_ref_get_name(head_ref));
1227 return got_worktree_set_head_ref(worktree, head_ref);
1230 err = check_linear_ancestry(commit_id,
1231 got_worktree_get_base_commit_id(worktree), 0, repo);
1232 if (err) {
1233 if (err->code != GOT_ERR_ANCESTRY)
1234 return err;
1235 ref_has_moved = 1;
1237 if (!ref_has_moved)
1238 return NULL;
1240 /* Switching to a rebased branch with the same reference name. */
1241 err = got_object_id_str(&base_id_str,
1242 got_worktree_get_base_commit_id(worktree));
1243 if (err)
1244 return err;
1245 printf("Reference %s now points at a different branch\n",
1246 got_worktree_get_head_ref_name(worktree));
1247 printf("Switching work tree from %s to %s\n", base_id_str,
1248 got_worktree_get_head_ref_name(worktree));
1249 return NULL;
1252 static const struct got_error *
1253 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1255 const struct got_error *err;
1256 int in_progress;
1258 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1259 if (err)
1260 return err;
1261 if (in_progress)
1262 return got_error(GOT_ERR_REBASING);
1264 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1265 if (err)
1266 return err;
1267 if (in_progress)
1268 return got_error(GOT_ERR_HISTEDIT_BUSY);
1270 return NULL;
1273 static const struct got_error *
1274 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1275 char *argv[], struct got_worktree *worktree)
1277 const struct got_error *err = NULL;
1278 char *path;
1279 int i;
1281 if (argc == 0) {
1282 path = strdup("");
1283 if (path == NULL)
1284 return got_error_from_errno("strdup");
1285 return got_pathlist_append(paths, path, NULL);
1288 for (i = 0; i < argc; i++) {
1289 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1290 if (err)
1291 break;
1292 err = got_pathlist_append(paths, path, NULL);
1293 if (err) {
1294 free(path);
1295 break;
1299 return err;
1302 static const struct got_error *
1303 cmd_update(int argc, char *argv[])
1305 const struct got_error *error = NULL;
1306 struct got_repository *repo = NULL;
1307 struct got_worktree *worktree = NULL;
1308 char *worktree_path = NULL;
1309 struct got_object_id *commit_id = NULL;
1310 char *commit_id_str = NULL;
1311 const char *branch_name = NULL;
1312 struct got_reference *head_ref = NULL;
1313 struct got_pathlist_head paths;
1314 struct got_pathlist_entry *pe;
1315 int ch, did_something = 0;
1317 TAILQ_INIT(&paths);
1319 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1320 switch (ch) {
1321 case 'b':
1322 branch_name = optarg;
1323 break;
1324 case 'c':
1325 commit_id_str = strdup(optarg);
1326 if (commit_id_str == NULL)
1327 return got_error_from_errno("strdup");
1328 break;
1329 default:
1330 usage_update();
1331 /* NOTREACHED */
1335 argc -= optind;
1336 argv += optind;
1338 #ifndef PROFILE
1339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1340 "unveil", NULL) == -1)
1341 err(1, "pledge");
1342 #endif
1343 worktree_path = getcwd(NULL, 0);
1344 if (worktree_path == NULL) {
1345 error = got_error_from_errno("getcwd");
1346 goto done;
1348 error = got_worktree_open(&worktree, worktree_path);
1349 if (error)
1350 goto done;
1352 error = check_rebase_or_histedit_in_progress(worktree);
1353 if (error)
1354 goto done;
1356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1357 NULL);
1358 if (error != NULL)
1359 goto done;
1361 error = apply_unveil(got_repo_get_path(repo), 0,
1362 got_worktree_get_root_path(worktree));
1363 if (error)
1364 goto done;
1366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1367 if (error)
1368 goto done;
1370 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1371 got_worktree_get_head_ref_name(worktree), 0);
1372 if (error != NULL)
1373 goto done;
1374 if (commit_id_str == NULL) {
1375 error = got_ref_resolve(&commit_id, repo, head_ref);
1376 if (error != NULL)
1377 goto done;
1378 error = got_object_id_str(&commit_id_str, commit_id);
1379 if (error != NULL)
1380 goto done;
1381 } else {
1382 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1383 free(commit_id_str);
1384 commit_id_str = NULL;
1385 if (error)
1386 goto done;
1387 error = got_object_id_str(&commit_id_str, commit_id);
1388 if (error)
1389 goto done;
1392 if (branch_name) {
1393 struct got_object_id *head_commit_id;
1394 TAILQ_FOREACH(pe, &paths, entry) {
1395 if (pe->path_len == 0)
1396 continue;
1397 error = got_error_msg(GOT_ERR_BAD_PATH,
1398 "switching between branches requires that "
1399 "the entire work tree gets updated");
1400 goto done;
1402 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1403 if (error)
1404 goto done;
1405 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1406 repo);
1407 free(head_commit_id);
1408 if (error != NULL)
1409 goto done;
1410 error = check_same_branch(commit_id, head_ref, NULL, repo);
1411 if (error)
1412 goto done;
1413 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1414 if (error)
1415 goto done;
1416 } else {
1417 error = check_linear_ancestry(commit_id,
1418 got_worktree_get_base_commit_id(worktree), 0, repo);
1419 if (error != NULL) {
1420 if (error->code == GOT_ERR_ANCESTRY)
1421 error = got_error(GOT_ERR_BRANCH_MOVED);
1422 goto done;
1424 error = check_same_branch(commit_id, head_ref, NULL, repo);
1425 if (error)
1426 goto done;
1429 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1430 commit_id) != 0) {
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1432 commit_id);
1433 if (error)
1434 goto done;
1437 error = got_worktree_checkout_files(worktree, &paths, repo,
1438 update_progress, &did_something, check_cancelled, NULL);
1439 if (error != NULL)
1440 goto done;
1442 if (did_something)
1443 printf("Updated to commit %s\n", commit_id_str);
1444 else
1445 printf("Already up-to-date\n");
1446 done:
1447 free(worktree_path);
1448 TAILQ_FOREACH(pe, &paths, entry)
1449 free((char *)pe->path);
1450 got_pathlist_free(&paths);
1451 free(commit_id);
1452 free(commit_id_str);
1453 return error;
1456 static const struct got_error *
1457 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1458 const char *path, int diff_context, int ignore_whitespace,
1459 struct got_repository *repo)
1461 const struct got_error *err = NULL;
1462 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1464 if (blob_id1) {
1465 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1466 if (err)
1467 goto done;
1470 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1471 if (err)
1472 goto done;
1474 while (path[0] == '/')
1475 path++;
1476 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1477 ignore_whitespace, stdout);
1478 done:
1479 if (blob1)
1480 got_object_blob_close(blob1);
1481 got_object_blob_close(blob2);
1482 return err;
1485 static const struct got_error *
1486 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1487 const char *path, int diff_context, int ignore_whitespace,
1488 struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1492 struct got_diff_blob_output_unidiff_arg arg;
1494 if (tree_id1) {
1495 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1496 if (err)
1497 goto done;
1500 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1501 if (err)
1502 goto done;
1504 arg.diff_context = diff_context;
1505 arg.ignore_whitespace = ignore_whitespace;
1506 arg.outfile = stdout;
1507 while (path[0] == '/')
1508 path++;
1509 err = got_diff_tree(tree1, tree2, path, path, repo,
1510 got_diff_blob_output_unidiff, &arg, 1);
1511 done:
1512 if (tree1)
1513 got_object_tree_close(tree1);
1514 if (tree2)
1515 got_object_tree_close(tree2);
1516 return err;
1519 static const struct got_error *
1520 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1521 const char *path, int diff_context, struct got_repository *repo)
1523 const struct got_error *err = NULL;
1524 struct got_commit_object *pcommit = NULL;
1525 char *id_str1 = NULL, *id_str2 = NULL;
1526 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1527 struct got_object_qid *qid;
1529 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1530 if (qid != NULL) {
1531 err = got_object_open_as_commit(&pcommit, repo,
1532 qid->id);
1533 if (err)
1534 return err;
1537 if (path && path[0] != '\0') {
1538 int obj_type;
1539 err = got_object_id_by_path(&obj_id2, repo, id, path);
1540 if (err)
1541 goto done;
1542 err = got_object_id_str(&id_str2, obj_id2);
1543 if (err) {
1544 free(obj_id2);
1545 goto done;
1547 if (pcommit) {
1548 err = got_object_id_by_path(&obj_id1, repo,
1549 qid->id, path);
1550 if (err) {
1551 free(obj_id2);
1552 goto done;
1554 err = got_object_id_str(&id_str1, obj_id1);
1555 if (err) {
1556 free(obj_id2);
1557 goto done;
1560 err = got_object_get_type(&obj_type, repo, obj_id2);
1561 if (err) {
1562 free(obj_id2);
1563 goto done;
1565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1566 switch (obj_type) {
1567 case GOT_OBJ_TYPE_BLOB:
1568 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1569 0, repo);
1570 break;
1571 case GOT_OBJ_TYPE_TREE:
1572 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1573 0, repo);
1574 break;
1575 default:
1576 err = got_error(GOT_ERR_OBJ_TYPE);
1577 break;
1579 free(obj_id1);
1580 free(obj_id2);
1581 } else {
1582 obj_id2 = got_object_commit_get_tree_id(commit);
1583 err = got_object_id_str(&id_str2, obj_id2);
1584 if (err)
1585 goto done;
1586 obj_id1 = got_object_commit_get_tree_id(pcommit);
1587 err = got_object_id_str(&id_str1, obj_id1);
1588 if (err)
1589 goto done;
1590 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1591 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1594 done:
1595 free(id_str1);
1596 free(id_str2);
1597 if (pcommit)
1598 got_object_commit_close(pcommit);
1599 return err;
1602 static char *
1603 get_datestr(time_t *time, char *datebuf)
1605 struct tm mytm, *tm;
1606 char *p, *s;
1608 tm = gmtime_r(time, &mytm);
1609 if (tm == NULL)
1610 return NULL;
1611 s = asctime_r(tm, datebuf);
1612 if (s == NULL)
1613 return NULL;
1614 p = strchr(s, '\n');
1615 if (p)
1616 *p = '\0';
1617 return s;
1620 static const struct got_error *
1621 match_logmsg(int *have_match, struct got_object_id *id,
1622 struct got_commit_object *commit, regex_t *regex)
1624 const struct got_error *err = NULL;
1625 regmatch_t regmatch;
1626 char *id_str = NULL, *logmsg = NULL;
1628 *have_match = 0;
1630 err = got_object_id_str(&id_str, id);
1631 if (err)
1632 return err;
1634 err = got_object_commit_get_logmsg(&logmsg, commit);
1635 if (err)
1636 goto done;
1638 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1639 *have_match = 1;
1640 done:
1641 free(id_str);
1642 free(logmsg);
1643 return err;
1646 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1648 static const struct got_error *
1649 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1650 struct got_repository *repo, const char *path, int show_patch,
1651 int diff_context, struct got_reflist_head *refs)
1653 const struct got_error *err = NULL;
1654 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1655 char datebuf[26];
1656 time_t committer_time;
1657 const char *author, *committer;
1658 char *refs_str = NULL;
1659 struct got_reflist_entry *re;
1661 SIMPLEQ_FOREACH(re, refs, entry) {
1662 char *s;
1663 const char *name;
1664 struct got_tag_object *tag = NULL;
1665 int cmp;
1667 name = got_ref_get_name(re->ref);
1668 if (strcmp(name, GOT_REF_HEAD) == 0)
1669 continue;
1670 if (strncmp(name, "refs/", 5) == 0)
1671 name += 5;
1672 if (strncmp(name, "got/", 4) == 0)
1673 continue;
1674 if (strncmp(name, "heads/", 6) == 0)
1675 name += 6;
1676 if (strncmp(name, "remotes/", 8) == 0)
1677 name += 8;
1678 if (strncmp(name, "tags/", 5) == 0) {
1679 err = got_object_open_as_tag(&tag, repo, re->id);
1680 if (err) {
1681 if (err->code != GOT_ERR_OBJ_TYPE)
1682 return err;
1683 /* Ref points at something other than a tag. */
1684 err = NULL;
1685 tag = NULL;
1688 cmp = got_object_id_cmp(tag ?
1689 got_object_tag_get_object_id(tag) : re->id, id);
1690 if (tag)
1691 got_object_tag_close(tag);
1692 if (cmp != 0)
1693 continue;
1694 s = refs_str;
1695 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1696 name) == -1) {
1697 err = got_error_from_errno("asprintf");
1698 free(s);
1699 return err;
1701 free(s);
1703 err = got_object_id_str(&id_str, id);
1704 if (err)
1705 return err;
1707 printf(GOT_COMMIT_SEP_STR);
1708 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1709 refs_str ? refs_str : "", refs_str ? ")" : "");
1710 free(id_str);
1711 id_str = NULL;
1712 free(refs_str);
1713 refs_str = NULL;
1714 printf("from: %s\n", got_object_commit_get_author(commit));
1715 committer_time = got_object_commit_get_committer_time(commit);
1716 datestr = get_datestr(&committer_time, datebuf);
1717 if (datestr)
1718 printf("date: %s UTC\n", datestr);
1719 author = got_object_commit_get_author(commit);
1720 committer = got_object_commit_get_committer(commit);
1721 if (strcmp(author, committer) != 0)
1722 printf("via: %s\n", committer);
1723 if (got_object_commit_get_nparents(commit) > 1) {
1724 const struct got_object_id_queue *parent_ids;
1725 struct got_object_qid *qid;
1726 int n = 1;
1727 parent_ids = got_object_commit_get_parent_ids(commit);
1728 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1729 err = got_object_id_str(&id_str, qid->id);
1730 if (err)
1731 return err;
1732 printf("parent %d: %s\n", n++, id_str);
1733 free(id_str);
1737 err = got_object_commit_get_logmsg(&logmsg0, commit);
1738 if (err)
1739 return err;
1741 logmsg = logmsg0;
1742 do {
1743 line = strsep(&logmsg, "\n");
1744 if (line)
1745 printf(" %s\n", line);
1746 } while (line);
1747 free(logmsg0);
1749 if (show_patch) {
1750 err = print_patch(commit, id, path, diff_context, repo);
1751 if (err == 0)
1752 printf("\n");
1755 if (fflush(stdout) != 0 && err == NULL)
1756 err = got_error_from_errno("fflush");
1757 return err;
1760 static const struct got_error *
1761 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1762 const char *path, int show_patch, const char *search_pattern,
1763 int diff_context, int limit, int first_parent_traversal,
1764 struct got_reflist_head *refs)
1766 const struct got_error *err;
1767 struct got_commit_graph *graph;
1768 regex_t regex;
1769 int have_match;
1771 if (search_pattern &&
1772 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1773 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1775 err = got_commit_graph_open(&graph, root_id, path,
1776 first_parent_traversal, repo);
1777 if (err)
1778 return err;
1779 err = got_commit_graph_iter_start(graph, root_id, repo,
1780 check_cancelled, NULL);
1781 if (err)
1782 goto done;
1783 for (;;) {
1784 struct got_commit_object *commit;
1785 struct got_object_id *id;
1787 if (sigint_received || sigpipe_received)
1788 break;
1790 err = got_commit_graph_iter_next(&id, graph);
1791 if (err) {
1792 if (err->code == GOT_ERR_ITER_COMPLETED) {
1793 err = NULL;
1794 break;
1796 if (err->code != GOT_ERR_ITER_NEED_MORE)
1797 break;
1798 err = got_commit_graph_fetch_commits(graph, 1, repo,
1799 check_cancelled, NULL);
1800 if (err)
1801 break;
1802 else
1803 continue;
1805 if (id == NULL)
1806 break;
1808 err = got_object_open_as_commit(&commit, repo, id);
1809 if (err)
1810 break;
1812 if (search_pattern) {
1813 err = match_logmsg(&have_match, id, commit, &regex);
1814 if (err) {
1815 got_object_commit_close(commit);
1816 break;
1818 if (have_match == 0) {
1819 got_object_commit_close(commit);
1820 continue;
1824 err = print_commit(commit, id, repo, path, show_patch,
1825 diff_context, refs);
1826 got_object_commit_close(commit);
1827 if (err || (limit && --limit == 0))
1828 break;
1830 done:
1831 if (search_pattern)
1832 regfree(&regex);
1833 got_commit_graph_close(graph);
1834 return err;
1837 __dead static void
1838 usage_log(void)
1840 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1841 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1842 exit(1);
1845 static int
1846 get_default_log_limit(void)
1848 const char *got_default_log_limit;
1849 long long n;
1850 const char *errstr;
1852 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1853 if (got_default_log_limit == NULL)
1854 return 0;
1855 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1856 if (errstr != NULL)
1857 return 0;
1858 return n;
1861 static const struct got_error *
1862 cmd_log(int argc, char *argv[])
1864 const struct got_error *error;
1865 struct got_repository *repo = NULL;
1866 struct got_worktree *worktree = NULL;
1867 struct got_commit_object *commit = NULL;
1868 struct got_object_id *id = NULL;
1869 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1870 const char *start_commit = NULL, *search_pattern = NULL;
1871 int diff_context = -1, ch;
1872 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1873 const char *errstr;
1874 struct got_reflist_head refs;
1876 SIMPLEQ_INIT(&refs);
1878 #ifndef PROFILE
1879 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1880 NULL)
1881 == -1)
1882 err(1, "pledge");
1883 #endif
1885 limit = get_default_log_limit();
1887 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1888 switch (ch) {
1889 case 'p':
1890 show_patch = 1;
1891 break;
1892 case 'c':
1893 start_commit = optarg;
1894 break;
1895 case 'C':
1896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1897 &errstr);
1898 if (errstr != NULL)
1899 err(1, "-C option %s", errstr);
1900 break;
1901 case 'l':
1902 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1903 if (errstr != NULL)
1904 err(1, "-l option %s", errstr);
1905 break;
1906 case 'f':
1907 first_parent_traversal = 1;
1908 break;
1909 case 'r':
1910 repo_path = realpath(optarg, NULL);
1911 if (repo_path == NULL)
1912 return got_error_from_errno2("realpath",
1913 optarg);
1914 got_path_strip_trailing_slashes(repo_path);
1915 break;
1916 case 's':
1917 search_pattern = optarg;
1918 break;
1919 default:
1920 usage_log();
1921 /* NOTREACHED */
1925 argc -= optind;
1926 argv += optind;
1928 if (diff_context == -1)
1929 diff_context = 3;
1930 else if (!show_patch)
1931 errx(1, "-C reguires -p");
1933 cwd = getcwd(NULL, 0);
1934 if (cwd == NULL) {
1935 error = got_error_from_errno("getcwd");
1936 goto done;
1939 error = got_worktree_open(&worktree, cwd);
1940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1941 goto done;
1942 error = NULL;
1944 if (argc == 0) {
1945 path = strdup("");
1946 if (path == NULL) {
1947 error = got_error_from_errno("strdup");
1948 goto done;
1950 } else if (argc == 1) {
1951 if (worktree) {
1952 error = got_worktree_resolve_path(&path, worktree,
1953 argv[0]);
1954 if (error)
1955 goto done;
1956 } else {
1957 path = strdup(argv[0]);
1958 if (path == NULL) {
1959 error = got_error_from_errno("strdup");
1960 goto done;
1963 } else
1964 usage_log();
1966 if (repo_path == NULL) {
1967 repo_path = worktree ?
1968 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1970 if (repo_path == NULL) {
1971 error = got_error_from_errno("strdup");
1972 goto done;
1975 error = got_repo_open(&repo, repo_path, NULL);
1976 if (error != NULL)
1977 goto done;
1979 error = apply_unveil(got_repo_get_path(repo), 1,
1980 worktree ? got_worktree_get_root_path(worktree) : NULL);
1981 if (error)
1982 goto done;
1984 if (start_commit == NULL) {
1985 struct got_reference *head_ref;
1986 error = got_ref_open(&head_ref, repo,
1987 worktree ? got_worktree_get_head_ref_name(worktree)
1988 : GOT_REF_HEAD, 0);
1989 if (error != NULL)
1990 return error;
1991 error = got_ref_resolve(&id, repo, head_ref);
1992 got_ref_close(head_ref);
1993 if (error != NULL)
1994 return error;
1995 error = got_object_open_as_commit(&commit, repo, id);
1996 } else {
1997 struct got_reference *ref;
1998 error = got_ref_open(&ref, repo, start_commit, 0);
1999 if (error == NULL) {
2000 int obj_type;
2001 error = got_ref_resolve(&id, repo, ref);
2002 got_ref_close(ref);
2003 if (error != NULL)
2004 goto done;
2005 error = got_object_get_type(&obj_type, repo, id);
2006 if (error != NULL)
2007 goto done;
2008 if (obj_type == GOT_OBJ_TYPE_TAG) {
2009 struct got_tag_object *tag;
2010 error = got_object_open_as_tag(&tag, repo, id);
2011 if (error != NULL)
2012 goto done;
2013 if (got_object_tag_get_object_type(tag) !=
2014 GOT_OBJ_TYPE_COMMIT) {
2015 got_object_tag_close(tag);
2016 error = got_error(GOT_ERR_OBJ_TYPE);
2017 goto done;
2019 free(id);
2020 id = got_object_id_dup(
2021 got_object_tag_get_object_id(tag));
2022 if (id == NULL)
2023 error = got_error_from_errno(
2024 "got_object_id_dup");
2025 got_object_tag_close(tag);
2026 if (error)
2027 goto done;
2028 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2029 error = got_error(GOT_ERR_OBJ_TYPE);
2030 goto done;
2032 error = got_object_open_as_commit(&commit, repo, id);
2033 if (error != NULL)
2034 goto done;
2036 if (commit == NULL) {
2037 error = got_repo_match_object_id_prefix(&id,
2038 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2039 if (error != NULL)
2040 return error;
2043 if (error != NULL)
2044 goto done;
2046 if (worktree) {
2047 const char *prefix = got_worktree_get_path_prefix(worktree);
2048 char *p;
2049 if (asprintf(&p, "%s%s%s", prefix,
2050 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2051 error = got_error_from_errno("asprintf");
2052 goto done;
2054 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2055 free(p);
2056 } else
2057 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2058 if (error != NULL)
2059 goto done;
2060 if (in_repo_path) {
2061 free(path);
2062 path = in_repo_path;
2065 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2066 if (error)
2067 goto done;
2069 error = print_commits(id, repo, path, show_patch, search_pattern,
2070 diff_context, limit, first_parent_traversal, &refs);
2071 done:
2072 free(path);
2073 free(repo_path);
2074 free(cwd);
2075 free(id);
2076 if (worktree)
2077 got_worktree_close(worktree);
2078 if (repo) {
2079 const struct got_error *repo_error;
2080 repo_error = got_repo_close(repo);
2081 if (error == NULL)
2082 error = repo_error;
2084 got_ref_list_free(&refs);
2085 return error;
2088 __dead static void
2089 usage_diff(void)
2091 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2092 "[-w] [object1 object2 | path]\n", getprogname());
2093 exit(1);
2096 struct print_diff_arg {
2097 struct got_repository *repo;
2098 struct got_worktree *worktree;
2099 int diff_context;
2100 const char *id_str;
2101 int header_shown;
2102 int diff_staged;
2103 int ignore_whitespace;
2106 static const struct got_error *
2107 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2108 const char *path, struct got_object_id *blob_id,
2109 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2110 int dirfd, const char *de_name)
2112 struct print_diff_arg *a = arg;
2113 const struct got_error *err = NULL;
2114 struct got_blob_object *blob1 = NULL;
2115 int fd = -1;
2116 FILE *f2 = NULL;
2117 char *abspath = NULL, *label1 = NULL;
2118 struct stat sb;
2120 if (a->diff_staged) {
2121 if (staged_status != GOT_STATUS_MODIFY &&
2122 staged_status != GOT_STATUS_ADD &&
2123 staged_status != GOT_STATUS_DELETE)
2124 return NULL;
2125 } else {
2126 if (staged_status == GOT_STATUS_DELETE)
2127 return NULL;
2128 if (status == GOT_STATUS_NONEXISTENT)
2129 return got_error_set_errno(ENOENT, path);
2130 if (status != GOT_STATUS_MODIFY &&
2131 status != GOT_STATUS_ADD &&
2132 status != GOT_STATUS_DELETE &&
2133 status != GOT_STATUS_CONFLICT)
2134 return NULL;
2137 if (!a->header_shown) {
2138 printf("diff %s %s%s\n", a->id_str,
2139 got_worktree_get_root_path(a->worktree),
2140 a->diff_staged ? " (staged changes)" : "");
2141 a->header_shown = 1;
2144 if (a->diff_staged) {
2145 const char *label1 = NULL, *label2 = NULL;
2146 switch (staged_status) {
2147 case GOT_STATUS_MODIFY:
2148 label1 = path;
2149 label2 = path;
2150 break;
2151 case GOT_STATUS_ADD:
2152 label2 = path;
2153 break;
2154 case GOT_STATUS_DELETE:
2155 label1 = path;
2156 break;
2157 default:
2158 return got_error(GOT_ERR_FILE_STATUS);
2160 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2161 label1, label2, a->diff_context, a->ignore_whitespace,
2162 a->repo, stdout);
2165 if (staged_status == GOT_STATUS_ADD ||
2166 staged_status == GOT_STATUS_MODIFY) {
2167 char *id_str;
2168 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2169 8192);
2170 if (err)
2171 goto done;
2172 err = got_object_id_str(&id_str, staged_blob_id);
2173 if (err)
2174 goto done;
2175 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2176 err = got_error_from_errno("asprintf");
2177 free(id_str);
2178 goto done;
2180 free(id_str);
2181 } else if (status != GOT_STATUS_ADD) {
2182 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2183 if (err)
2184 goto done;
2187 if (status != GOT_STATUS_DELETE) {
2188 if (asprintf(&abspath, "%s/%s",
2189 got_worktree_get_root_path(a->worktree), path) == -1) {
2190 err = got_error_from_errno("asprintf");
2191 goto done;
2194 if (dirfd != -1) {
2195 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2196 if (fd == -1) {
2197 err = got_error_from_errno2("openat", abspath);
2198 goto done;
2200 } else {
2201 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2202 if (fd == -1) {
2203 err = got_error_from_errno2("open", abspath);
2204 goto done;
2207 if (fstat(fd, &sb) == -1) {
2208 err = got_error_from_errno2("fstat", abspath);
2209 goto done;
2211 f2 = fdopen(fd, "r");
2212 if (f2 == NULL) {
2213 err = got_error_from_errno2("fdopen", abspath);
2214 goto done;
2216 fd = -1;
2217 } else
2218 sb.st_size = 0;
2220 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2221 a->diff_context, a->ignore_whitespace, stdout);
2222 done:
2223 if (blob1)
2224 got_object_blob_close(blob1);
2225 if (f2 && fclose(f2) == EOF && err == NULL)
2226 err = got_error_from_errno("fclose");
2227 if (fd != -1 && close(fd) == -1 && err == NULL)
2228 err = got_error_from_errno("close");
2229 free(abspath);
2230 return err;
2233 static const struct got_error *
2234 match_object_id(struct got_object_id **id, char **label,
2235 const char *id_str, int obj_type, int resolve_tags,
2236 struct got_repository *repo)
2238 const struct got_error *err;
2239 struct got_tag_object *tag;
2240 struct got_reference *ref = NULL;
2242 *id = NULL;
2243 *label = NULL;
2245 if (resolve_tags) {
2246 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2247 repo);
2248 if (err == NULL) {
2249 *id = got_object_id_dup(
2250 got_object_tag_get_object_id(tag));
2251 if (*id == NULL)
2252 err = got_error_from_errno("got_object_id_dup");
2253 else if (asprintf(label, "refs/tags/%s",
2254 got_object_tag_get_name(tag)) == -1) {
2255 err = got_error_from_errno("asprintf");
2256 free(*id);
2257 *id = NULL;
2259 got_object_tag_close(tag);
2260 return err;
2261 } else if (err->code != GOT_ERR_NO_OBJ)
2262 return err;
2265 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2266 if (err) {
2267 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2268 return err;
2269 err = got_ref_open(&ref, repo, id_str, 0);
2270 if (err != NULL)
2271 goto done;
2272 *label = strdup(got_ref_get_name(ref));
2273 if (*label == NULL) {
2274 err = got_error_from_errno("strdup");
2275 goto done;
2277 err = got_ref_resolve(id, repo, ref);
2278 } else {
2279 err = got_object_id_str(label, *id);
2280 if (*label == NULL) {
2281 err = got_error_from_errno("strdup");
2282 goto done;
2285 done:
2286 if (ref)
2287 got_ref_close(ref);
2288 return err;
2292 static const struct got_error *
2293 cmd_diff(int argc, char *argv[])
2295 const struct got_error *error;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 char *cwd = NULL, *repo_path = NULL;
2299 struct got_object_id *id1 = NULL, *id2 = NULL;
2300 const char *id_str1 = NULL, *id_str2 = NULL;
2301 char *label1 = NULL, *label2 = NULL;
2302 int type1, type2;
2303 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2304 const char *errstr;
2305 char *path = NULL;
2307 #ifndef PROFILE
2308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2309 NULL) == -1)
2310 err(1, "pledge");
2311 #endif
2313 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2314 switch (ch) {
2315 case 'C':
2316 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2317 &errstr);
2318 if (errstr != NULL)
2319 err(1, "-C option %s", errstr);
2320 break;
2321 case 'r':
2322 repo_path = realpath(optarg, NULL);
2323 if (repo_path == NULL)
2324 return got_error_from_errno2("realpath",
2325 optarg);
2326 got_path_strip_trailing_slashes(repo_path);
2327 break;
2328 case 's':
2329 diff_staged = 1;
2330 break;
2331 case 'w':
2332 ignore_whitespace = 1;
2333 break;
2334 default:
2335 usage_diff();
2336 /* NOTREACHED */
2340 argc -= optind;
2341 argv += optind;
2343 cwd = getcwd(NULL, 0);
2344 if (cwd == NULL) {
2345 error = got_error_from_errno("getcwd");
2346 goto done;
2348 error = got_worktree_open(&worktree, cwd);
2349 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2350 goto done;
2351 if (argc <= 1) {
2352 if (worktree == NULL) {
2353 error = got_error(GOT_ERR_NOT_WORKTREE);
2354 goto done;
2356 if (repo_path)
2357 errx(1,
2358 "-r option can't be used when diffing a work tree");
2359 repo_path = strdup(got_worktree_get_repo_path(worktree));
2360 if (repo_path == NULL) {
2361 error = got_error_from_errno("strdup");
2362 goto done;
2364 if (argc == 1) {
2365 error = got_worktree_resolve_path(&path, worktree,
2366 argv[0]);
2367 if (error)
2368 goto done;
2369 } else {
2370 path = strdup("");
2371 if (path == NULL) {
2372 error = got_error_from_errno("strdup");
2373 goto done;
2376 } else if (argc == 2) {
2377 if (diff_staged)
2378 errx(1, "-s option can't be used when diffing "
2379 "objects in repository");
2380 id_str1 = argv[0];
2381 id_str2 = argv[1];
2382 if (worktree && repo_path == NULL) {
2383 repo_path =
2384 strdup(got_worktree_get_repo_path(worktree));
2385 if (repo_path == NULL) {
2386 error = got_error_from_errno("strdup");
2387 goto done;
2390 } else
2391 usage_diff();
2393 if (repo_path == NULL) {
2394 repo_path = getcwd(NULL, 0);
2395 if (repo_path == NULL)
2396 return got_error_from_errno("getcwd");
2399 error = got_repo_open(&repo, repo_path, NULL);
2400 free(repo_path);
2401 if (error != NULL)
2402 goto done;
2404 error = apply_unveil(got_repo_get_path(repo), 1,
2405 worktree ? got_worktree_get_root_path(worktree) : NULL);
2406 if (error)
2407 goto done;
2409 if (argc <= 1) {
2410 struct print_diff_arg arg;
2411 struct got_pathlist_head paths;
2412 char *id_str;
2414 TAILQ_INIT(&paths);
2416 error = got_object_id_str(&id_str,
2417 got_worktree_get_base_commit_id(worktree));
2418 if (error)
2419 goto done;
2420 arg.repo = repo;
2421 arg.worktree = worktree;
2422 arg.diff_context = diff_context;
2423 arg.id_str = id_str;
2424 arg.header_shown = 0;
2425 arg.diff_staged = diff_staged;
2426 arg.ignore_whitespace = ignore_whitespace;
2428 error = got_pathlist_append(&paths, path, NULL);
2429 if (error)
2430 goto done;
2432 error = got_worktree_status(worktree, &paths, repo, print_diff,
2433 &arg, check_cancelled, NULL);
2434 free(id_str);
2435 got_pathlist_free(&paths);
2436 goto done;
2439 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2440 repo);
2441 if (error)
2442 goto done;
2444 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2445 repo);
2446 if (error)
2447 goto done;
2449 error = got_object_get_type(&type1, repo, id1);
2450 if (error)
2451 goto done;
2453 error = got_object_get_type(&type2, repo, id2);
2454 if (error)
2455 goto done;
2457 if (type1 != type2) {
2458 error = got_error(GOT_ERR_OBJ_TYPE);
2459 goto done;
2462 switch (type1) {
2463 case GOT_OBJ_TYPE_BLOB:
2464 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2465 diff_context, ignore_whitespace, repo, stdout);
2466 break;
2467 case GOT_OBJ_TYPE_TREE:
2468 error = got_diff_objects_as_trees(id1, id2, "", "",
2469 diff_context, ignore_whitespace, repo, stdout);
2470 break;
2471 case GOT_OBJ_TYPE_COMMIT:
2472 printf("diff %s %s\n", label1, label2);
2473 error = got_diff_objects_as_commits(id1, id2, diff_context,
2474 ignore_whitespace, repo, stdout);
2475 break;
2476 default:
2477 error = got_error(GOT_ERR_OBJ_TYPE);
2480 done:
2481 free(label1);
2482 free(label2);
2483 free(id1);
2484 free(id2);
2485 free(path);
2486 if (worktree)
2487 got_worktree_close(worktree);
2488 if (repo) {
2489 const struct got_error *repo_error;
2490 repo_error = got_repo_close(repo);
2491 if (error == NULL)
2492 error = repo_error;
2494 return error;
2497 __dead static void
2498 usage_blame(void)
2500 fprintf(stderr,
2501 "usage: %s blame [-c commit] [-r repository-path] path\n",
2502 getprogname());
2503 exit(1);
2506 struct blame_line {
2507 int annotated;
2508 char *id_str;
2509 char *committer;
2510 char datebuf[11]; /* YYYY-MM-DD + NUL */
2513 struct blame_cb_args {
2514 struct blame_line *lines;
2515 int nlines;
2516 int nlines_prec;
2517 int lineno_cur;
2518 off_t *line_offsets;
2519 FILE *f;
2520 struct got_repository *repo;
2523 static const struct got_error *
2524 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2526 const struct got_error *err = NULL;
2527 struct blame_cb_args *a = arg;
2528 struct blame_line *bline;
2529 char *line = NULL;
2530 size_t linesize = 0;
2531 struct got_commit_object *commit = NULL;
2532 off_t offset;
2533 struct tm tm;
2534 time_t committer_time;
2536 if (nlines != a->nlines ||
2537 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2538 return got_error(GOT_ERR_RANGE);
2540 if (sigint_received)
2541 return got_error(GOT_ERR_ITER_COMPLETED);
2543 if (lineno == -1)
2544 return NULL; /* no change in this commit */
2546 /* Annotate this line. */
2547 bline = &a->lines[lineno - 1];
2548 if (bline->annotated)
2549 return NULL;
2550 err = got_object_id_str(&bline->id_str, id);
2551 if (err)
2552 return err;
2554 err = got_object_open_as_commit(&commit, a->repo, id);
2555 if (err)
2556 goto done;
2558 bline->committer = strdup(got_object_commit_get_committer(commit));
2559 if (bline->committer == NULL) {
2560 err = got_error_from_errno("strdup");
2561 goto done;
2564 committer_time = got_object_commit_get_committer_time(commit);
2565 if (localtime_r(&committer_time, &tm) == NULL)
2566 return got_error_from_errno("localtime_r");
2567 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2568 &tm) >= sizeof(bline->datebuf)) {
2569 err = got_error(GOT_ERR_NO_SPACE);
2570 goto done;
2572 bline->annotated = 1;
2574 /* Print lines annotated so far. */
2575 bline = &a->lines[a->lineno_cur - 1];
2576 if (!bline->annotated)
2577 goto done;
2579 offset = a->line_offsets[a->lineno_cur - 1];
2580 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2581 err = got_error_from_errno("fseeko");
2582 goto done;
2585 while (bline->annotated) {
2586 char *smallerthan, *at, *nl, *committer;
2587 size_t len;
2589 if (getline(&line, &linesize, a->f) == -1) {
2590 if (ferror(a->f))
2591 err = got_error_from_errno("getline");
2592 break;
2595 committer = bline->committer;
2596 smallerthan = strchr(committer, '<');
2597 if (smallerthan && smallerthan[1] != '\0')
2598 committer = smallerthan + 1;
2599 at = strchr(committer, '@');
2600 if (at)
2601 *at = '\0';
2602 len = strlen(committer);
2603 if (len >= 9)
2604 committer[8] = '\0';
2606 nl = strchr(line, '\n');
2607 if (nl)
2608 *nl = '\0';
2609 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2610 bline->id_str, bline->datebuf, committer, line);
2612 a->lineno_cur++;
2613 bline = &a->lines[a->lineno_cur - 1];
2615 done:
2616 if (commit)
2617 got_object_commit_close(commit);
2618 free(line);
2619 return err;
2622 static const struct got_error *
2623 cmd_blame(int argc, char *argv[])
2625 const struct got_error *error;
2626 struct got_repository *repo = NULL;
2627 struct got_worktree *worktree = NULL;
2628 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2629 struct got_object_id *obj_id = NULL;
2630 struct got_object_id *commit_id = NULL;
2631 struct got_blob_object *blob = NULL;
2632 char *commit_id_str = NULL;
2633 struct blame_cb_args bca;
2634 int ch, obj_type, i;
2635 size_t filesize;
2637 memset(&bca, 0, sizeof(bca));
2639 #ifndef PROFILE
2640 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2641 NULL) == -1)
2642 err(1, "pledge");
2643 #endif
2645 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2646 switch (ch) {
2647 case 'c':
2648 commit_id_str = optarg;
2649 break;
2650 case 'r':
2651 repo_path = realpath(optarg, NULL);
2652 if (repo_path == NULL)
2653 return got_error_from_errno2("realpath",
2654 optarg);
2655 got_path_strip_trailing_slashes(repo_path);
2656 break;
2657 default:
2658 usage_blame();
2659 /* NOTREACHED */
2663 argc -= optind;
2664 argv += optind;
2666 if (argc == 1)
2667 path = argv[0];
2668 else
2669 usage_blame();
2671 cwd = getcwd(NULL, 0);
2672 if (cwd == NULL) {
2673 error = got_error_from_errno("getcwd");
2674 goto done;
2676 if (repo_path == NULL) {
2677 error = got_worktree_open(&worktree, cwd);
2678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2679 goto done;
2680 else
2681 error = NULL;
2682 if (worktree) {
2683 repo_path =
2684 strdup(got_worktree_get_repo_path(worktree));
2685 if (repo_path == NULL)
2686 error = got_error_from_errno("strdup");
2687 if (error)
2688 goto done;
2689 } else {
2690 repo_path = strdup(cwd);
2691 if (repo_path == NULL) {
2692 error = got_error_from_errno("strdup");
2693 goto done;
2698 error = got_repo_open(&repo, repo_path, NULL);
2699 if (error != NULL)
2700 goto done;
2702 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2703 if (error)
2704 goto done;
2706 if (worktree) {
2707 const char *prefix = got_worktree_get_path_prefix(worktree);
2708 char *p, *worktree_subdir = cwd +
2709 strlen(got_worktree_get_root_path(worktree));
2710 if (asprintf(&p, "%s%s%s%s%s",
2711 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2712 worktree_subdir, worktree_subdir[0] ? "/" : "",
2713 path) == -1) {
2714 error = got_error_from_errno("asprintf");
2715 goto done;
2717 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2718 free(p);
2719 } else {
2720 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2722 if (error)
2723 goto done;
2725 if (commit_id_str == NULL) {
2726 struct got_reference *head_ref;
2727 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2728 if (error != NULL)
2729 goto done;
2730 error = got_ref_resolve(&commit_id, repo, head_ref);
2731 got_ref_close(head_ref);
2732 if (error != NULL)
2733 goto done;
2734 } else {
2735 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2736 if (error)
2737 goto done;
2740 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2741 if (error)
2742 goto done;
2743 if (obj_id == NULL) {
2744 error = got_error(GOT_ERR_NO_OBJ);
2745 goto done;
2748 error = got_object_get_type(&obj_type, repo, obj_id);
2749 if (error)
2750 goto done;
2752 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2753 error = got_error(GOT_ERR_OBJ_TYPE);
2754 goto done;
2757 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2758 if (error)
2759 goto done;
2760 bca.f = got_opentemp();
2761 if (bca.f == NULL) {
2762 error = got_error_from_errno("got_opentemp");
2763 goto done;
2765 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2766 &bca.line_offsets, bca.f, blob);
2767 if (error || bca.nlines == 0)
2768 goto done;
2770 /* Don't include \n at EOF in the blame line count. */
2771 if (bca.line_offsets[bca.nlines - 1] == filesize)
2772 bca.nlines--;
2774 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2775 if (bca.lines == NULL) {
2776 error = got_error_from_errno("calloc");
2777 goto done;
2779 bca.lineno_cur = 1;
2780 bca.nlines_prec = 0;
2781 i = bca.nlines;
2782 while (i > 0) {
2783 i /= 10;
2784 bca.nlines_prec++;
2786 bca.repo = repo;
2788 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2789 check_cancelled, NULL);
2790 if (error)
2791 goto done;
2792 done:
2793 free(in_repo_path);
2794 free(repo_path);
2795 free(cwd);
2796 free(commit_id);
2797 free(obj_id);
2798 if (blob)
2799 got_object_blob_close(blob);
2800 if (worktree)
2801 got_worktree_close(worktree);
2802 if (repo) {
2803 const struct got_error *repo_error;
2804 repo_error = got_repo_close(repo);
2805 if (error == NULL)
2806 error = repo_error;
2808 if (bca.lines) {
2809 for (i = 0; i < bca.nlines; i++) {
2810 struct blame_line *bline = &bca.lines[i];
2811 free(bline->id_str);
2812 free(bline->committer);
2814 free(bca.lines);
2816 free(bca.line_offsets);
2817 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2818 error = got_error_from_errno("fclose");
2819 return error;
2822 __dead static void
2823 usage_tree(void)
2825 fprintf(stderr,
2826 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2827 getprogname());
2828 exit(1);
2831 static void
2832 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2833 const char *root_path)
2835 int is_root_path = (strcmp(path, root_path) == 0);
2836 const char *modestr = "";
2837 mode_t mode = got_tree_entry_get_mode(te);
2839 path += strlen(root_path);
2840 while (path[0] == '/')
2841 path++;
2843 if (got_object_tree_entry_is_submodule(te))
2844 modestr = "$";
2845 else if (S_ISLNK(mode))
2846 modestr = "@";
2847 else if (S_ISDIR(mode))
2848 modestr = "/";
2849 else if (mode & S_IXUSR)
2850 modestr = "*";
2852 printf("%s%s%s%s%s\n", id ? id : "", path,
2853 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2856 static const struct got_error *
2857 print_tree(const char *path, struct got_object_id *commit_id,
2858 int show_ids, int recurse, const char *root_path,
2859 struct got_repository *repo)
2861 const struct got_error *err = NULL;
2862 struct got_object_id *tree_id = NULL;
2863 struct got_tree_object *tree = NULL;
2864 int nentries, i;
2866 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2867 if (err)
2868 goto done;
2870 err = got_object_open_as_tree(&tree, repo, tree_id);
2871 if (err)
2872 goto done;
2873 nentries = got_object_tree_get_nentries(tree);
2874 for (i = 0; i < nentries; i++) {
2875 struct got_tree_entry *te;
2876 char *id = NULL;
2878 if (sigint_received || sigpipe_received)
2879 break;
2881 te = got_object_tree_get_entry(tree, i);
2882 if (show_ids) {
2883 char *id_str;
2884 err = got_object_id_str(&id_str,
2885 got_tree_entry_get_id(te));
2886 if (err)
2887 goto done;
2888 if (asprintf(&id, "%s ", id_str) == -1) {
2889 err = got_error_from_errno("asprintf");
2890 free(id_str);
2891 goto done;
2893 free(id_str);
2895 print_entry(te, id, path, root_path);
2896 free(id);
2898 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2899 char *child_path;
2900 if (asprintf(&child_path, "%s%s%s", path,
2901 path[0] == '/' && path[1] == '\0' ? "" : "/",
2902 got_tree_entry_get_name(te)) == -1) {
2903 err = got_error_from_errno("asprintf");
2904 goto done;
2906 err = print_tree(child_path, commit_id, show_ids, 1,
2907 root_path, repo);
2908 free(child_path);
2909 if (err)
2910 goto done;
2913 done:
2914 if (tree)
2915 got_object_tree_close(tree);
2916 free(tree_id);
2917 return err;
2920 static const struct got_error *
2921 cmd_tree(int argc, char *argv[])
2923 const struct got_error *error;
2924 struct got_repository *repo = NULL;
2925 struct got_worktree *worktree = NULL;
2926 const char *path;
2927 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2928 struct got_object_id *commit_id = NULL;
2929 char *commit_id_str = NULL;
2930 int show_ids = 0, recurse = 0;
2931 int ch;
2933 #ifndef PROFILE
2934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2935 NULL) == -1)
2936 err(1, "pledge");
2937 #endif
2939 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2940 switch (ch) {
2941 case 'c':
2942 commit_id_str = optarg;
2943 break;
2944 case 'r':
2945 repo_path = realpath(optarg, NULL);
2946 if (repo_path == NULL)
2947 return got_error_from_errno2("realpath",
2948 optarg);
2949 got_path_strip_trailing_slashes(repo_path);
2950 break;
2951 case 'i':
2952 show_ids = 1;
2953 break;
2954 case 'R':
2955 recurse = 1;
2956 break;
2957 default:
2958 usage_tree();
2959 /* NOTREACHED */
2963 argc -= optind;
2964 argv += optind;
2966 if (argc == 1)
2967 path = argv[0];
2968 else if (argc > 1)
2969 usage_tree();
2970 else
2971 path = NULL;
2973 cwd = getcwd(NULL, 0);
2974 if (cwd == NULL) {
2975 error = got_error_from_errno("getcwd");
2976 goto done;
2978 if (repo_path == NULL) {
2979 error = got_worktree_open(&worktree, cwd);
2980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2981 goto done;
2982 else
2983 error = NULL;
2984 if (worktree) {
2985 repo_path =
2986 strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL)
2988 error = got_error_from_errno("strdup");
2989 if (error)
2990 goto done;
2991 } else {
2992 repo_path = strdup(cwd);
2993 if (repo_path == NULL) {
2994 error = got_error_from_errno("strdup");
2995 goto done;
3000 error = got_repo_open(&repo, repo_path, NULL);
3001 if (error != NULL)
3002 goto done;
3004 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3005 if (error)
3006 goto done;
3008 if (path == NULL) {
3009 if (worktree) {
3010 char *p, *worktree_subdir = cwd +
3011 strlen(got_worktree_get_root_path(worktree));
3012 if (asprintf(&p, "%s/%s",
3013 got_worktree_get_path_prefix(worktree),
3014 worktree_subdir) == -1) {
3015 error = got_error_from_errno("asprintf");
3016 goto done;
3018 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3019 free(p);
3020 if (error)
3021 goto done;
3022 } else
3023 path = "/";
3025 if (in_repo_path == NULL) {
3026 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3027 if (error != NULL)
3028 goto done;
3031 if (commit_id_str == NULL) {
3032 struct got_reference *head_ref;
3033 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3034 if (error != NULL)
3035 goto done;
3036 error = got_ref_resolve(&commit_id, repo, head_ref);
3037 got_ref_close(head_ref);
3038 if (error != NULL)
3039 goto done;
3040 } else {
3041 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3042 if (error)
3043 goto done;
3046 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3047 in_repo_path, repo);
3048 done:
3049 free(in_repo_path);
3050 free(repo_path);
3051 free(cwd);
3052 free(commit_id);
3053 if (worktree)
3054 got_worktree_close(worktree);
3055 if (repo) {
3056 const struct got_error *repo_error;
3057 repo_error = got_repo_close(repo);
3058 if (error == NULL)
3059 error = repo_error;
3061 return error;
3064 __dead static void
3065 usage_status(void)
3067 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3068 exit(1);
3071 static const struct got_error *
3072 print_status(void *arg, unsigned char status, unsigned char staged_status,
3073 const char *path, struct got_object_id *blob_id,
3074 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3075 int dirfd, const char *de_name)
3077 if (status == staged_status && (status == GOT_STATUS_DELETE))
3078 status = GOT_STATUS_NO_CHANGE;
3079 printf("%c%c %s\n", status, staged_status, path);
3080 return NULL;
3083 static const struct got_error *
3084 cmd_status(int argc, char *argv[])
3086 const struct got_error *error = NULL;
3087 struct got_repository *repo = NULL;
3088 struct got_worktree *worktree = NULL;
3089 char *cwd = NULL;
3090 struct got_pathlist_head paths;
3091 struct got_pathlist_entry *pe;
3092 int ch;
3094 TAILQ_INIT(&paths);
3096 while ((ch = getopt(argc, argv, "")) != -1) {
3097 switch (ch) {
3098 default:
3099 usage_status();
3100 /* NOTREACHED */
3104 argc -= optind;
3105 argv += optind;
3107 #ifndef PROFILE
3108 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3109 NULL) == -1)
3110 err(1, "pledge");
3111 #endif
3112 cwd = getcwd(NULL, 0);
3113 if (cwd == NULL) {
3114 error = got_error_from_errno("getcwd");
3115 goto done;
3118 error = got_worktree_open(&worktree, cwd);
3119 if (error != NULL)
3120 goto done;
3122 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3123 NULL);
3124 if (error != NULL)
3125 goto done;
3127 error = apply_unveil(got_repo_get_path(repo), 1,
3128 got_worktree_get_root_path(worktree));
3129 if (error)
3130 goto done;
3132 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3133 if (error)
3134 goto done;
3136 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3137 check_cancelled, NULL);
3138 done:
3139 TAILQ_FOREACH(pe, &paths, entry)
3140 free((char *)pe->path);
3141 got_pathlist_free(&paths);
3142 free(cwd);
3143 return error;
3146 __dead static void
3147 usage_ref(void)
3149 fprintf(stderr,
3150 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3151 getprogname());
3152 exit(1);
3155 static const struct got_error *
3156 list_refs(struct got_repository *repo)
3158 static const struct got_error *err = NULL;
3159 struct got_reflist_head refs;
3160 struct got_reflist_entry *re;
3162 SIMPLEQ_INIT(&refs);
3163 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3164 if (err)
3165 return err;
3167 SIMPLEQ_FOREACH(re, &refs, entry) {
3168 char *refstr;
3169 refstr = got_ref_to_str(re->ref);
3170 if (refstr == NULL)
3171 return got_error_from_errno("got_ref_to_str");
3172 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3173 free(refstr);
3176 got_ref_list_free(&refs);
3177 return NULL;
3180 static const struct got_error *
3181 delete_ref(struct got_repository *repo, const char *refname)
3183 const struct got_error *err = NULL;
3184 struct got_reference *ref;
3186 err = got_ref_open(&ref, repo, refname, 0);
3187 if (err)
3188 return err;
3190 err = got_ref_delete(ref, repo);
3191 got_ref_close(ref);
3192 return err;
3195 static const struct got_error *
3196 add_ref(struct got_repository *repo, const char *refname, const char *target)
3198 const struct got_error *err = NULL;
3199 struct got_object_id *id;
3200 struct got_reference *ref = NULL;
3203 * Don't let the user create a reference name with a leading '-'.
3204 * While technically a valid reference name, this case is usually
3205 * an unintended typo.
3207 if (refname[0] == '-')
3208 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3210 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3211 repo);
3212 if (err) {
3213 struct got_reference *target_ref;
3215 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3216 return err;
3217 err = got_ref_open(&target_ref, repo, target, 0);
3218 if (err)
3219 return err;
3220 err = got_ref_resolve(&id, repo, target_ref);
3221 got_ref_close(target_ref);
3222 if (err)
3223 return err;
3226 err = got_ref_alloc(&ref, refname, id);
3227 if (err)
3228 goto done;
3230 err = got_ref_write(ref, repo);
3231 done:
3232 if (ref)
3233 got_ref_close(ref);
3234 free(id);
3235 return err;
3238 static const struct got_error *
3239 add_symref(struct got_repository *repo, const char *refname, const char *target)
3241 const struct got_error *err = NULL;
3242 struct got_reference *ref = NULL;
3243 struct got_reference *target_ref = NULL;
3246 * Don't let the user create a reference name with a leading '-'.
3247 * While technically a valid reference name, this case is usually
3248 * an unintended typo.
3250 if (refname[0] == '-')
3251 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3253 err = got_ref_open(&target_ref, repo, target, 0);
3254 if (err)
3255 return err;
3257 err = got_ref_alloc_symref(&ref, refname, target_ref);
3258 if (err)
3259 goto done;
3261 err = got_ref_write(ref, repo);
3262 done:
3263 if (target_ref)
3264 got_ref_close(target_ref);
3265 if (ref)
3266 got_ref_close(ref);
3267 return err;
3270 static const struct got_error *
3271 cmd_ref(int argc, char *argv[])
3273 const struct got_error *error = NULL;
3274 struct got_repository *repo = NULL;
3275 struct got_worktree *worktree = NULL;
3276 char *cwd = NULL, *repo_path = NULL;
3277 int ch, do_list = 0, create_symref = 0;
3278 const char *delref = NULL;
3280 /* TODO: Add -s option for adding symbolic references. */
3281 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3282 switch (ch) {
3283 case 'd':
3284 delref = optarg;
3285 break;
3286 case 'r':
3287 repo_path = realpath(optarg, NULL);
3288 if (repo_path == NULL)
3289 return got_error_from_errno2("realpath",
3290 optarg);
3291 got_path_strip_trailing_slashes(repo_path);
3292 break;
3293 case 'l':
3294 do_list = 1;
3295 break;
3296 case 's':
3297 create_symref = 1;
3298 break;
3299 default:
3300 usage_ref();
3301 /* NOTREACHED */
3305 if (do_list && delref)
3306 errx(1, "-l and -d options are mutually exclusive\n");
3308 argc -= optind;
3309 argv += optind;
3311 if (do_list || delref) {
3312 if (create_symref)
3313 errx(1, "-s option cannot be used together with the "
3314 "-l or -d options");
3315 if (argc > 0)
3316 usage_ref();
3317 } else if (argc != 2)
3318 usage_ref();
3320 #ifndef PROFILE
3321 if (do_list) {
3322 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3323 NULL) == -1)
3324 err(1, "pledge");
3325 } else {
3326 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3327 "sendfd unveil", NULL) == -1)
3328 err(1, "pledge");
3330 #endif
3331 cwd = getcwd(NULL, 0);
3332 if (cwd == NULL) {
3333 error = got_error_from_errno("getcwd");
3334 goto done;
3337 if (repo_path == NULL) {
3338 error = got_worktree_open(&worktree, cwd);
3339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3340 goto done;
3341 else
3342 error = NULL;
3343 if (worktree) {
3344 repo_path =
3345 strdup(got_worktree_get_repo_path(worktree));
3346 if (repo_path == NULL)
3347 error = got_error_from_errno("strdup");
3348 if (error)
3349 goto done;
3350 } else {
3351 repo_path = strdup(cwd);
3352 if (repo_path == NULL) {
3353 error = got_error_from_errno("strdup");
3354 goto done;
3359 error = got_repo_open(&repo, repo_path, NULL);
3360 if (error != NULL)
3361 goto done;
3363 error = apply_unveil(got_repo_get_path(repo), do_list,
3364 worktree ? got_worktree_get_root_path(worktree) : NULL);
3365 if (error)
3366 goto done;
3368 if (do_list)
3369 error = list_refs(repo);
3370 else if (delref)
3371 error = delete_ref(repo, delref);
3372 else if (create_symref)
3373 error = add_symref(repo, argv[0], argv[1]);
3374 else
3375 error = add_ref(repo, argv[0], argv[1]);
3376 done:
3377 if (repo)
3378 got_repo_close(repo);
3379 if (worktree)
3380 got_worktree_close(worktree);
3381 free(cwd);
3382 free(repo_path);
3383 return error;
3386 __dead static void
3387 usage_branch(void)
3389 fprintf(stderr,
3390 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3391 "[name]\n", getprogname());
3392 exit(1);
3395 static const struct got_error *
3396 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3397 struct got_reference *ref)
3399 const struct got_error *err = NULL;
3400 const char *refname, *marker = " ";
3401 char *refstr;
3403 refname = got_ref_get_name(ref);
3404 if (worktree && strcmp(refname,
3405 got_worktree_get_head_ref_name(worktree)) == 0) {
3406 struct got_object_id *id = NULL;
3408 err = got_ref_resolve(&id, repo, ref);
3409 if (err)
3410 return err;
3411 if (got_object_id_cmp(id,
3412 got_worktree_get_base_commit_id(worktree)) == 0)
3413 marker = "* ";
3414 else
3415 marker = "~ ";
3416 free(id);
3419 if (strncmp(refname, "refs/heads/", 11) == 0)
3420 refname += 11;
3421 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3422 refname += 18;
3424 refstr = got_ref_to_str(ref);
3425 if (refstr == NULL)
3426 return got_error_from_errno("got_ref_to_str");
3428 printf("%s%s: %s\n", marker, refname, refstr);
3429 free(refstr);
3430 return NULL;
3433 static const struct got_error *
3434 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3436 const char *refname;
3438 if (worktree == NULL)
3439 return got_error(GOT_ERR_NOT_WORKTREE);
3441 refname = got_worktree_get_head_ref_name(worktree);
3443 if (strncmp(refname, "refs/heads/", 11) == 0)
3444 refname += 11;
3445 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3446 refname += 18;
3448 printf("%s\n", refname);
3450 return NULL;
3453 static const struct got_error *
3454 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3456 static const struct got_error *err = NULL;
3457 struct got_reflist_head refs;
3458 struct got_reflist_entry *re;
3459 struct got_reference *temp_ref = NULL;
3460 int rebase_in_progress, histedit_in_progress;
3462 SIMPLEQ_INIT(&refs);
3464 if (worktree) {
3465 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3466 worktree);
3467 if (err)
3468 return err;
3470 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3471 worktree);
3472 if (err)
3473 return err;
3475 if (rebase_in_progress || histedit_in_progress) {
3476 err = got_ref_open(&temp_ref, repo,
3477 got_worktree_get_head_ref_name(worktree), 0);
3478 if (err)
3479 return err;
3480 list_branch(repo, worktree, temp_ref);
3481 got_ref_close(temp_ref);
3485 err = got_ref_list(&refs, repo, "refs/heads",
3486 got_ref_cmp_by_name, NULL);
3487 if (err)
3488 return err;
3490 SIMPLEQ_FOREACH(re, &refs, entry)
3491 list_branch(repo, worktree, re->ref);
3493 got_ref_list_free(&refs);
3494 return NULL;
3497 static const struct got_error *
3498 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3499 const char *branch_name)
3501 const struct got_error *err = NULL;
3502 struct got_reference *ref = NULL;
3503 char *refname;
3505 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3506 return got_error_from_errno("asprintf");
3508 err = got_ref_open(&ref, repo, refname, 0);
3509 if (err)
3510 goto done;
3512 if (worktree &&
3513 strcmp(got_worktree_get_head_ref_name(worktree),
3514 got_ref_get_name(ref)) == 0) {
3515 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3516 "will not delete this work tree's current branch");
3517 goto done;
3520 err = got_ref_delete(ref, repo);
3521 done:
3522 if (ref)
3523 got_ref_close(ref);
3524 free(refname);
3525 return err;
3528 static const struct got_error *
3529 add_branch(struct got_repository *repo, const char *branch_name,
3530 struct got_object_id *base_commit_id)
3532 const struct got_error *err = NULL;
3533 struct got_reference *ref = NULL;
3534 char *base_refname = NULL, *refname = NULL;
3537 * Don't let the user create a branch name with a leading '-'.
3538 * While technically a valid reference name, this case is usually
3539 * an unintended typo.
3541 if (branch_name[0] == '-')
3542 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3544 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3545 err = got_error_from_errno("asprintf");
3546 goto done;
3549 err = got_ref_open(&ref, repo, refname, 0);
3550 if (err == NULL) {
3551 err = got_error(GOT_ERR_BRANCH_EXISTS);
3552 goto done;
3553 } else if (err->code != GOT_ERR_NOT_REF)
3554 goto done;
3556 err = got_ref_alloc(&ref, refname, base_commit_id);
3557 if (err)
3558 goto done;
3560 err = got_ref_write(ref, repo);
3561 done:
3562 if (ref)
3563 got_ref_close(ref);
3564 free(base_refname);
3565 free(refname);
3566 return err;
3569 static const struct got_error *
3570 cmd_branch(int argc, char *argv[])
3572 const struct got_error *error = NULL;
3573 struct got_repository *repo = NULL;
3574 struct got_worktree *worktree = NULL;
3575 char *cwd = NULL, *repo_path = NULL;
3576 int ch, do_list = 0, do_show = 0;
3577 const char *delref = NULL, *commit_id_arg = NULL;
3579 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3580 switch (ch) {
3581 case 'c':
3582 commit_id_arg = optarg;
3583 break;
3584 case 'd':
3585 delref = optarg;
3586 break;
3587 case 'r':
3588 repo_path = realpath(optarg, NULL);
3589 if (repo_path == NULL)
3590 return got_error_from_errno2("realpath",
3591 optarg);
3592 got_path_strip_trailing_slashes(repo_path);
3593 break;
3594 case 'l':
3595 do_list = 1;
3596 break;
3597 default:
3598 usage_branch();
3599 /* NOTREACHED */
3603 if (do_list && delref)
3604 errx(1, "-l and -d options are mutually exclusive\n");
3606 argc -= optind;
3607 argv += optind;
3609 if (!do_list && !delref && argc == 0)
3610 do_show = 1;
3612 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3613 errx(1, "-c option can only be used when creating a branch");
3615 if (do_list || delref) {
3616 if (argc > 0)
3617 usage_branch();
3618 } else if (!do_show && argc != 1)
3619 usage_branch();
3621 #ifndef PROFILE
3622 if (do_list || do_show) {
3623 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3624 NULL) == -1)
3625 err(1, "pledge");
3626 } else {
3627 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3628 "sendfd unveil", NULL) == -1)
3629 err(1, "pledge");
3631 #endif
3632 cwd = getcwd(NULL, 0);
3633 if (cwd == NULL) {
3634 error = got_error_from_errno("getcwd");
3635 goto done;
3638 if (repo_path == NULL) {
3639 error = got_worktree_open(&worktree, cwd);
3640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3641 goto done;
3642 else
3643 error = NULL;
3644 if (worktree) {
3645 repo_path =
3646 strdup(got_worktree_get_repo_path(worktree));
3647 if (repo_path == NULL)
3648 error = got_error_from_errno("strdup");
3649 if (error)
3650 goto done;
3651 } else {
3652 repo_path = strdup(cwd);
3653 if (repo_path == NULL) {
3654 error = got_error_from_errno("strdup");
3655 goto done;
3660 error = got_repo_open(&repo, repo_path, NULL);
3661 if (error != NULL)
3662 goto done;
3664 error = apply_unveil(got_repo_get_path(repo), do_list,
3665 worktree ? got_worktree_get_root_path(worktree) : NULL);
3666 if (error)
3667 goto done;
3669 if (do_show)
3670 error = show_current_branch(repo, worktree);
3671 else if (do_list)
3672 error = list_branches(repo, worktree);
3673 else if (delref)
3674 error = delete_branch(repo, worktree, delref);
3675 else {
3676 struct got_object_id *commit_id;
3677 if (commit_id_arg == NULL)
3678 commit_id_arg = worktree ?
3679 got_worktree_get_head_ref_name(worktree) :
3680 GOT_REF_HEAD;
3681 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3682 if (error)
3683 goto done;
3684 error = add_branch(repo, argv[0], commit_id);
3685 free(commit_id);
3687 done:
3688 if (repo)
3689 got_repo_close(repo);
3690 if (worktree)
3691 got_worktree_close(worktree);
3692 free(cwd);
3693 free(repo_path);
3694 return error;
3698 __dead static void
3699 usage_tag(void)
3701 fprintf(stderr,
3702 "usage: %s tag [-r repository] | -l | "
3703 "[-m message] name [commit]\n", getprogname());
3704 exit(1);
3707 #if 0
3708 static const struct got_error *
3709 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3711 const struct got_error *err = NULL;
3712 struct got_reflist_entry *re, *se, *new;
3713 struct got_object_id *re_id, *se_id;
3714 struct got_tag_object *re_tag, *se_tag;
3715 time_t re_time, se_time;
3717 SIMPLEQ_FOREACH(re, tags, entry) {
3718 se = SIMPLEQ_FIRST(sorted);
3719 if (se == NULL) {
3720 err = got_reflist_entry_dup(&new, re);
3721 if (err)
3722 return err;
3723 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3724 continue;
3725 } else {
3726 err = got_ref_resolve(&re_id, repo, re->ref);
3727 if (err)
3728 break;
3729 err = got_object_open_as_tag(&re_tag, repo, re_id);
3730 free(re_id);
3731 if (err)
3732 break;
3733 re_time = got_object_tag_get_tagger_time(re_tag);
3734 got_object_tag_close(re_tag);
3737 while (se) {
3738 err = got_ref_resolve(&se_id, repo, re->ref);
3739 if (err)
3740 break;
3741 err = got_object_open_as_tag(&se_tag, repo, se_id);
3742 free(se_id);
3743 if (err)
3744 break;
3745 se_time = got_object_tag_get_tagger_time(se_tag);
3746 got_object_tag_close(se_tag);
3748 if (se_time > re_time) {
3749 err = got_reflist_entry_dup(&new, re);
3750 if (err)
3751 return err;
3752 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3753 break;
3755 se = SIMPLEQ_NEXT(se, entry);
3756 continue;
3759 done:
3760 return err;
3762 #endif
3764 static const struct got_error *
3765 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3766 struct got_reference *ref2)
3768 const struct got_error *err = NULL;
3769 struct got_repository *repo = arg;
3770 struct got_object_id *id1, *id2 = NULL;
3771 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3772 time_t time1, time2;
3774 *cmp = 0;
3776 err = got_ref_resolve(&id1, repo, ref1);
3777 if (err)
3778 return err;
3779 err = got_object_open_as_tag(&tag1, repo, id1);
3780 if (err)
3781 goto done;
3783 err = got_ref_resolve(&id2, repo, ref2);
3784 if (err)
3785 goto done;
3786 err = got_object_open_as_tag(&tag2, repo, id2);
3787 if (err)
3788 goto done;
3790 time1 = got_object_tag_get_tagger_time(tag1);
3791 time2 = got_object_tag_get_tagger_time(tag2);
3793 /* Put latest tags first. */
3794 if (time1 < time2)
3795 *cmp = 1;
3796 else if (time1 > time2)
3797 *cmp = -1;
3798 else
3799 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3800 done:
3801 free(id1);
3802 free(id2);
3803 if (tag1)
3804 got_object_tag_close(tag1);
3805 if (tag2)
3806 got_object_tag_close(tag2);
3807 return err;
3810 static const struct got_error *
3811 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3813 static const struct got_error *err = NULL;
3814 struct got_reflist_head refs;
3815 struct got_reflist_entry *re;
3817 SIMPLEQ_INIT(&refs);
3819 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3820 if (err)
3821 return err;
3823 SIMPLEQ_FOREACH(re, &refs, entry) {
3824 const char *refname;
3825 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3826 char datebuf[26];
3827 time_t tagger_time;
3828 struct got_object_id *id;
3829 struct got_tag_object *tag;
3831 refname = got_ref_get_name(re->ref);
3832 if (strncmp(refname, "refs/tags/", 10) != 0)
3833 continue;
3834 refname += 10;
3835 refstr = got_ref_to_str(re->ref);
3836 if (refstr == NULL) {
3837 err = got_error_from_errno("got_ref_to_str");
3838 break;
3840 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3841 free(refstr);
3843 err = got_ref_resolve(&id, repo, re->ref);
3844 if (err)
3845 break;
3846 err = got_object_open_as_tag(&tag, repo, id);
3847 free(id);
3848 if (err)
3849 break;
3850 printf("from: %s\n", got_object_tag_get_tagger(tag));
3851 tagger_time = got_object_tag_get_tagger_time(tag);
3852 datestr = get_datestr(&tagger_time, datebuf);
3853 if (datestr)
3854 printf("date: %s UTC\n", datestr);
3855 err = got_object_id_str(&id_str,
3856 got_object_tag_get_object_id(tag));
3857 if (err)
3858 break;
3859 switch (got_object_tag_get_object_type(tag)) {
3860 case GOT_OBJ_TYPE_BLOB:
3861 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3862 break;
3863 case GOT_OBJ_TYPE_TREE:
3864 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3865 break;
3866 case GOT_OBJ_TYPE_COMMIT:
3867 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3868 break;
3869 case GOT_OBJ_TYPE_TAG:
3870 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3871 break;
3872 default:
3873 break;
3875 free(id_str);
3876 tagmsg0 = strdup(got_object_tag_get_message(tag));
3877 got_object_tag_close(tag);
3878 if (tagmsg0 == NULL) {
3879 err = got_error_from_errno("strdup");
3880 break;
3883 tagmsg = tagmsg0;
3884 do {
3885 line = strsep(&tagmsg, "\n");
3886 if (line)
3887 printf(" %s\n", line);
3888 } while (line);
3889 free(tagmsg0);
3892 got_ref_list_free(&refs);
3893 return NULL;
3896 static const struct got_error *
3897 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3898 const char *tag_name, const char *repo_path)
3900 const struct got_error *err = NULL;
3901 char *template = NULL, *initial_content = NULL;
3902 char *editor = NULL;
3903 int fd = -1;
3905 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3906 err = got_error_from_errno("asprintf");
3907 goto done;
3910 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3911 commit_id_str, tag_name) == -1) {
3912 err = got_error_from_errno("asprintf");
3913 goto done;
3916 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3917 if (err)
3918 goto done;
3920 dprintf(fd, initial_content);
3921 close(fd);
3923 err = get_editor(&editor);
3924 if (err)
3925 goto done;
3926 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3927 done:
3928 free(initial_content);
3929 free(template);
3930 free(editor);
3932 /* Editor is done; we can now apply unveil(2) */
3933 if (err == NULL) {
3934 err = apply_unveil(repo_path, 0, NULL);
3935 if (err) {
3936 free(*tagmsg);
3937 *tagmsg = NULL;
3940 return err;
3943 static const struct got_error *
3944 add_tag(struct got_repository *repo, const char *tag_name,
3945 const char *commit_arg, const char *tagmsg_arg)
3947 const struct got_error *err = NULL;
3948 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3949 char *label = NULL, *commit_id_str = NULL;
3950 struct got_reference *ref = NULL;
3951 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3952 char *tagmsg_path = NULL, *tag_id_str = NULL;
3953 int preserve_tagmsg = 0;
3956 * Don't let the user create a tag name with a leading '-'.
3957 * While technically a valid reference name, this case is usually
3958 * an unintended typo.
3960 if (tag_name[0] == '-')
3961 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3963 err = get_author(&tagger, repo);
3964 if (err)
3965 return err;
3967 err = match_object_id(&commit_id, &label, commit_arg,
3968 GOT_OBJ_TYPE_COMMIT, 1, repo);
3969 if (err)
3970 goto done;
3972 err = got_object_id_str(&commit_id_str, commit_id);
3973 if (err)
3974 goto done;
3976 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3977 refname = strdup(tag_name);
3978 if (refname == NULL) {
3979 err = got_error_from_errno("strdup");
3980 goto done;
3982 tag_name += 10;
3983 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3984 err = got_error_from_errno("asprintf");
3985 goto done;
3988 err = got_ref_open(&ref, repo, refname, 0);
3989 if (err == NULL) {
3990 err = got_error(GOT_ERR_TAG_EXISTS);
3991 goto done;
3992 } else if (err->code != GOT_ERR_NOT_REF)
3993 goto done;
3995 if (tagmsg_arg == NULL) {
3996 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3997 tag_name, got_repo_get_path(repo));
3998 if (err) {
3999 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4000 tagmsg_path != NULL)
4001 preserve_tagmsg = 1;
4002 goto done;
4006 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4007 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4008 if (err) {
4009 if (tagmsg_path)
4010 preserve_tagmsg = 1;
4011 goto done;
4014 err = got_ref_alloc(&ref, refname, tag_id);
4015 if (err) {
4016 if (tagmsg_path)
4017 preserve_tagmsg = 1;
4018 goto done;
4021 err = got_ref_write(ref, repo);
4022 if (err) {
4023 if (tagmsg_path)
4024 preserve_tagmsg = 1;
4025 goto done;
4028 err = got_object_id_str(&tag_id_str, tag_id);
4029 if (err) {
4030 if (tagmsg_path)
4031 preserve_tagmsg = 1;
4032 goto done;
4034 printf("Created tag %s\n", tag_id_str);
4035 done:
4036 if (preserve_tagmsg) {
4037 fprintf(stderr, "%s: tag message preserved in %s\n",
4038 getprogname(), tagmsg_path);
4039 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4040 err = got_error_from_errno2("unlink", tagmsg_path);
4041 free(tag_id_str);
4042 if (ref)
4043 got_ref_close(ref);
4044 free(commit_id);
4045 free(commit_id_str);
4046 free(refname);
4047 free(tagmsg);
4048 free(tagmsg_path);
4049 free(tagger);
4050 return err;
4053 static const struct got_error *
4054 cmd_tag(int argc, char *argv[])
4056 const struct got_error *error = NULL;
4057 struct got_repository *repo = NULL;
4058 struct got_worktree *worktree = NULL;
4059 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4060 char *gitconfig_path = NULL;
4061 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4062 int ch, do_list = 0;
4064 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4065 switch (ch) {
4066 case 'm':
4067 tagmsg = optarg;
4068 break;
4069 case 'r':
4070 repo_path = realpath(optarg, NULL);
4071 if (repo_path == NULL)
4072 return got_error_from_errno2("realpath",
4073 optarg);
4074 got_path_strip_trailing_slashes(repo_path);
4075 break;
4076 case 'l':
4077 do_list = 1;
4078 break;
4079 default:
4080 usage_tag();
4081 /* NOTREACHED */
4085 argc -= optind;
4086 argv += optind;
4088 if (do_list) {
4089 if (tagmsg)
4090 errx(1, "-l and -m options are mutually exclusive\n");
4091 if (argc > 0)
4092 usage_tag();
4093 } else if (argc < 1 || argc > 2)
4094 usage_tag();
4095 else if (argc > 1)
4096 commit_id_arg = argv[1];
4097 tag_name = argv[0];
4099 #ifndef PROFILE
4100 if (do_list) {
4101 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4102 NULL) == -1)
4103 err(1, "pledge");
4104 } else {
4105 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4106 "sendfd unveil", NULL) == -1)
4107 err(1, "pledge");
4109 #endif
4110 cwd = getcwd(NULL, 0);
4111 if (cwd == NULL) {
4112 error = got_error_from_errno("getcwd");
4113 goto done;
4116 if (repo_path == NULL) {
4117 error = got_worktree_open(&worktree, cwd);
4118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4119 goto done;
4120 else
4121 error = NULL;
4122 if (worktree) {
4123 repo_path =
4124 strdup(got_worktree_get_repo_path(worktree));
4125 if (repo_path == NULL)
4126 error = got_error_from_errno("strdup");
4127 if (error)
4128 goto done;
4129 } else {
4130 repo_path = strdup(cwd);
4131 if (repo_path == NULL) {
4132 error = got_error_from_errno("strdup");
4133 goto done;
4138 if (do_list) {
4139 error = got_repo_open(&repo, repo_path, NULL);
4140 if (error != NULL)
4141 goto done;
4142 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4143 if (error)
4144 goto done;
4145 error = list_tags(repo, worktree);
4146 } else {
4147 error = get_gitconfig_path(&gitconfig_path);
4148 if (error)
4149 goto done;
4150 error = got_repo_open(&repo, repo_path, gitconfig_path);
4151 if (error != NULL)
4152 goto done;
4154 if (tagmsg) {
4155 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4156 if (error)
4157 goto done;
4160 if (commit_id_arg == NULL) {
4161 struct got_reference *head_ref;
4162 struct got_object_id *commit_id;
4163 error = got_ref_open(&head_ref, repo,
4164 worktree ? got_worktree_get_head_ref_name(worktree)
4165 : GOT_REF_HEAD, 0);
4166 if (error)
4167 goto done;
4168 error = got_ref_resolve(&commit_id, repo, head_ref);
4169 got_ref_close(head_ref);
4170 if (error)
4171 goto done;
4172 error = got_object_id_str(&commit_id_str, commit_id);
4173 free(commit_id);
4174 if (error)
4175 goto done;
4178 error = add_tag(repo, tag_name,
4179 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4181 done:
4182 if (repo)
4183 got_repo_close(repo);
4184 if (worktree)
4185 got_worktree_close(worktree);
4186 free(cwd);
4187 free(repo_path);
4188 free(gitconfig_path);
4189 free(commit_id_str);
4190 return error;
4193 __dead static void
4194 usage_add(void)
4196 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4197 getprogname());
4198 exit(1);
4201 static const struct got_error *
4202 add_progress(void *arg, unsigned char status, const char *path)
4204 while (path[0] == '/')
4205 path++;
4206 printf("%c %s\n", status, path);
4207 return NULL;
4210 static const struct got_error *
4211 cmd_add(int argc, char *argv[])
4213 const struct got_error *error = NULL;
4214 struct got_repository *repo = NULL;
4215 struct got_worktree *worktree = NULL;
4216 char *cwd = NULL;
4217 struct got_pathlist_head paths;
4218 struct got_pathlist_entry *pe;
4219 int ch, can_recurse = 0, no_ignores = 0;
4221 TAILQ_INIT(&paths);
4223 while ((ch = getopt(argc, argv, "IR")) != -1) {
4224 switch (ch) {
4225 case 'I':
4226 no_ignores = 1;
4227 break;
4228 case 'R':
4229 can_recurse = 1;
4230 break;
4231 default:
4232 usage_add();
4233 /* NOTREACHED */
4237 argc -= optind;
4238 argv += optind;
4240 #ifndef PROFILE
4241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4242 NULL) == -1)
4243 err(1, "pledge");
4244 #endif
4245 if (argc < 1)
4246 usage_add();
4248 cwd = getcwd(NULL, 0);
4249 if (cwd == NULL) {
4250 error = got_error_from_errno("getcwd");
4251 goto done;
4254 error = got_worktree_open(&worktree, cwd);
4255 if (error)
4256 goto done;
4258 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4259 NULL);
4260 if (error != NULL)
4261 goto done;
4263 error = apply_unveil(got_repo_get_path(repo), 1,
4264 got_worktree_get_root_path(worktree));
4265 if (error)
4266 goto done;
4268 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4269 if (error)
4270 goto done;
4272 if (!can_recurse && no_ignores) {
4273 error = got_error_msg(GOT_ERR_BAD_PATH,
4274 "disregarding ignores requires -R option");
4275 goto done;
4279 if (!can_recurse) {
4280 char *ondisk_path;
4281 struct stat sb;
4282 TAILQ_FOREACH(pe, &paths, entry) {
4283 if (asprintf(&ondisk_path, "%s/%s",
4284 got_worktree_get_root_path(worktree),
4285 pe->path) == -1) {
4286 error = got_error_from_errno("asprintf");
4287 goto done;
4289 if (lstat(ondisk_path, &sb) == -1) {
4290 if (errno == ENOENT) {
4291 free(ondisk_path);
4292 continue;
4294 error = got_error_from_errno2("lstat",
4295 ondisk_path);
4296 free(ondisk_path);
4297 goto done;
4299 free(ondisk_path);
4300 if (S_ISDIR(sb.st_mode)) {
4301 error = got_error_msg(GOT_ERR_BAD_PATH,
4302 "adding directories requires -R option");
4303 goto done;
4308 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4309 NULL, repo, no_ignores);
4310 done:
4311 if (repo)
4312 got_repo_close(repo);
4313 if (worktree)
4314 got_worktree_close(worktree);
4315 TAILQ_FOREACH(pe, &paths, entry)
4316 free((char *)pe->path);
4317 got_pathlist_free(&paths);
4318 free(cwd);
4319 return error;
4322 __dead static void
4323 usage_remove(void)
4325 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4326 getprogname());
4327 exit(1);
4330 static const struct got_error *
4331 print_remove_status(void *arg, unsigned char status,
4332 unsigned char staged_status, const char *path)
4334 while (path[0] == '/')
4335 path++;
4336 if (status == GOT_STATUS_NONEXISTENT)
4337 return NULL;
4338 if (status == staged_status && (status == GOT_STATUS_DELETE))
4339 status = GOT_STATUS_NO_CHANGE;
4340 printf("%c%c %s\n", status, staged_status, path);
4341 return NULL;
4344 static const struct got_error *
4345 cmd_remove(int argc, char *argv[])
4347 const struct got_error *error = NULL;
4348 struct got_worktree *worktree = NULL;
4349 struct got_repository *repo = NULL;
4350 char *cwd = NULL;
4351 struct got_pathlist_head paths;
4352 struct got_pathlist_entry *pe;
4353 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4355 TAILQ_INIT(&paths);
4357 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4358 switch (ch) {
4359 case 'f':
4360 delete_local_mods = 1;
4361 break;
4362 case 'k':
4363 keep_on_disk = 1;
4364 break;
4365 case 'R':
4366 can_recurse = 1;
4367 break;
4368 default:
4369 usage_remove();
4370 /* NOTREACHED */
4374 argc -= optind;
4375 argv += optind;
4377 #ifndef PROFILE
4378 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4379 NULL) == -1)
4380 err(1, "pledge");
4381 #endif
4382 if (argc < 1)
4383 usage_remove();
4385 cwd = getcwd(NULL, 0);
4386 if (cwd == NULL) {
4387 error = got_error_from_errno("getcwd");
4388 goto done;
4390 error = got_worktree_open(&worktree, cwd);
4391 if (error)
4392 goto done;
4394 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4395 NULL);
4396 if (error)
4397 goto done;
4399 error = apply_unveil(got_repo_get_path(repo), 1,
4400 got_worktree_get_root_path(worktree));
4401 if (error)
4402 goto done;
4404 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4405 if (error)
4406 goto done;
4408 if (!can_recurse) {
4409 char *ondisk_path;
4410 struct stat sb;
4411 TAILQ_FOREACH(pe, &paths, entry) {
4412 if (asprintf(&ondisk_path, "%s/%s",
4413 got_worktree_get_root_path(worktree),
4414 pe->path) == -1) {
4415 error = got_error_from_errno("asprintf");
4416 goto done;
4418 if (lstat(ondisk_path, &sb) == -1) {
4419 if (errno == ENOENT) {
4420 free(ondisk_path);
4421 continue;
4423 error = got_error_from_errno2("lstat",
4424 ondisk_path);
4425 free(ondisk_path);
4426 goto done;
4428 free(ondisk_path);
4429 if (S_ISDIR(sb.st_mode)) {
4430 error = got_error_msg(GOT_ERR_BAD_PATH,
4431 "removing directories requires -R option");
4432 goto done;
4437 error = got_worktree_schedule_delete(worktree, &paths,
4438 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4439 if (error)
4440 goto done;
4441 done:
4442 if (repo)
4443 got_repo_close(repo);
4444 if (worktree)
4445 got_worktree_close(worktree);
4446 TAILQ_FOREACH(pe, &paths, entry)
4447 free((char *)pe->path);
4448 got_pathlist_free(&paths);
4449 free(cwd);
4450 return error;
4453 __dead static void
4454 usage_revert(void)
4456 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4457 "path ...\n", getprogname());
4458 exit(1);
4461 static const struct got_error *
4462 revert_progress(void *arg, unsigned char status, const char *path)
4464 while (path[0] == '/')
4465 path++;
4466 printf("%c %s\n", status, path);
4467 return NULL;
4470 struct choose_patch_arg {
4471 FILE *patch_script_file;
4472 const char *action;
4475 static const struct got_error *
4476 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4477 int nchanges, const char *action)
4479 char *line = NULL;
4480 size_t linesize = 0;
4481 ssize_t linelen;
4483 switch (status) {
4484 case GOT_STATUS_ADD:
4485 printf("A %s\n%s this addition? [y/n] ", path, action);
4486 break;
4487 case GOT_STATUS_DELETE:
4488 printf("D %s\n%s this deletion? [y/n] ", path, action);
4489 break;
4490 case GOT_STATUS_MODIFY:
4491 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4492 return got_error_from_errno("fseek");
4493 printf(GOT_COMMIT_SEP_STR);
4494 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4495 printf("%s", line);
4496 if (ferror(patch_file))
4497 return got_error_from_errno("getline");
4498 printf(GOT_COMMIT_SEP_STR);
4499 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4500 path, n, nchanges, action);
4501 break;
4502 default:
4503 return got_error_path(path, GOT_ERR_FILE_STATUS);
4506 return NULL;
4509 static const struct got_error *
4510 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4511 FILE *patch_file, int n, int nchanges)
4513 const struct got_error *err = NULL;
4514 char *line = NULL;
4515 size_t linesize = 0;
4516 ssize_t linelen;
4517 int resp = ' ';
4518 struct choose_patch_arg *a = arg;
4520 *choice = GOT_PATCH_CHOICE_NONE;
4522 if (a->patch_script_file) {
4523 char *nl;
4524 err = show_change(status, path, patch_file, n, nchanges,
4525 a->action);
4526 if (err)
4527 return err;
4528 linelen = getline(&line, &linesize, a->patch_script_file);
4529 if (linelen == -1) {
4530 if (ferror(a->patch_script_file))
4531 return got_error_from_errno("getline");
4532 return NULL;
4534 nl = strchr(line, '\n');
4535 if (nl)
4536 *nl = '\0';
4537 if (strcmp(line, "y") == 0) {
4538 *choice = GOT_PATCH_CHOICE_YES;
4539 printf("y\n");
4540 } else if (strcmp(line, "n") == 0) {
4541 *choice = GOT_PATCH_CHOICE_NO;
4542 printf("n\n");
4543 } else if (strcmp(line, "q") == 0 &&
4544 status == GOT_STATUS_MODIFY) {
4545 *choice = GOT_PATCH_CHOICE_QUIT;
4546 printf("q\n");
4547 } else
4548 printf("invalid response '%s'\n", line);
4549 free(line);
4550 return NULL;
4553 while (resp != 'y' && resp != 'n' && resp != 'q') {
4554 err = show_change(status, path, patch_file, n, nchanges,
4555 a->action);
4556 if (err)
4557 return err;
4558 resp = getchar();
4559 if (resp == '\n')
4560 resp = getchar();
4561 if (status == GOT_STATUS_MODIFY) {
4562 if (resp != 'y' && resp != 'n' && resp != 'q') {
4563 printf("invalid response '%c'\n", resp);
4564 resp = ' ';
4566 } else if (resp != 'y' && resp != 'n') {
4567 printf("invalid response '%c'\n", resp);
4568 resp = ' ';
4572 if (resp == 'y')
4573 *choice = GOT_PATCH_CHOICE_YES;
4574 else if (resp == 'n')
4575 *choice = GOT_PATCH_CHOICE_NO;
4576 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4577 *choice = GOT_PATCH_CHOICE_QUIT;
4579 return NULL;
4583 static const struct got_error *
4584 cmd_revert(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_worktree *worktree = NULL;
4588 struct got_repository *repo = NULL;
4589 char *cwd = NULL, *path = NULL;
4590 struct got_pathlist_head paths;
4591 struct got_pathlist_entry *pe;
4592 int ch, can_recurse = 0, pflag = 0;
4593 FILE *patch_script_file = NULL;
4594 const char *patch_script_path = NULL;
4595 struct choose_patch_arg cpa;
4597 TAILQ_INIT(&paths);
4599 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4600 switch (ch) {
4601 case 'p':
4602 pflag = 1;
4603 break;
4604 case 'F':
4605 patch_script_path = optarg;
4606 break;
4607 case 'R':
4608 can_recurse = 1;
4609 break;
4610 default:
4611 usage_revert();
4612 /* NOTREACHED */
4616 argc -= optind;
4617 argv += optind;
4619 #ifndef PROFILE
4620 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4621 "unveil", NULL) == -1)
4622 err(1, "pledge");
4623 #endif
4624 if (argc < 1)
4625 usage_revert();
4626 if (patch_script_path && !pflag)
4627 errx(1, "-F option can only be used together with -p option");
4629 cwd = getcwd(NULL, 0);
4630 if (cwd == NULL) {
4631 error = got_error_from_errno("getcwd");
4632 goto done;
4634 error = got_worktree_open(&worktree, cwd);
4635 if (error)
4636 goto done;
4638 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4639 NULL);
4640 if (error != NULL)
4641 goto done;
4643 if (patch_script_path) {
4644 patch_script_file = fopen(patch_script_path, "r");
4645 if (patch_script_file == NULL) {
4646 error = got_error_from_errno2("fopen",
4647 patch_script_path);
4648 goto done;
4651 error = apply_unveil(got_repo_get_path(repo), 1,
4652 got_worktree_get_root_path(worktree));
4653 if (error)
4654 goto done;
4656 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4657 if (error)
4658 goto done;
4660 if (!can_recurse) {
4661 char *ondisk_path;
4662 struct stat sb;
4663 TAILQ_FOREACH(pe, &paths, entry) {
4664 if (asprintf(&ondisk_path, "%s/%s",
4665 got_worktree_get_root_path(worktree),
4666 pe->path) == -1) {
4667 error = got_error_from_errno("asprintf");
4668 goto done;
4670 if (lstat(ondisk_path, &sb) == -1) {
4671 if (errno == ENOENT) {
4672 free(ondisk_path);
4673 continue;
4675 error = got_error_from_errno2("lstat",
4676 ondisk_path);
4677 free(ondisk_path);
4678 goto done;
4680 free(ondisk_path);
4681 if (S_ISDIR(sb.st_mode)) {
4682 error = got_error_msg(GOT_ERR_BAD_PATH,
4683 "reverting directories requires -R option");
4684 goto done;
4689 cpa.patch_script_file = patch_script_file;
4690 cpa.action = "revert";
4691 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4692 pflag ? choose_patch : NULL, &cpa, repo);
4693 if (error)
4694 goto done;
4695 done:
4696 if (patch_script_file && fclose(patch_script_file) == EOF &&
4697 error == NULL)
4698 error = got_error_from_errno2("fclose", patch_script_path);
4699 if (repo)
4700 got_repo_close(repo);
4701 if (worktree)
4702 got_worktree_close(worktree);
4703 free(path);
4704 free(cwd);
4705 return error;
4708 __dead static void
4709 usage_commit(void)
4711 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4712 getprogname());
4713 exit(1);
4716 struct collect_commit_logmsg_arg {
4717 const char *cmdline_log;
4718 const char *editor;
4719 const char *worktree_path;
4720 const char *branch_name;
4721 const char *repo_path;
4722 char *logmsg_path;
4726 static const struct got_error *
4727 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4728 void *arg)
4730 char *initial_content = NULL;
4731 struct got_pathlist_entry *pe;
4732 const struct got_error *err = NULL;
4733 char *template = NULL;
4734 struct collect_commit_logmsg_arg *a = arg;
4735 int fd;
4736 size_t len;
4738 /* if a message was specified on the command line, just use it */
4739 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4740 len = strlen(a->cmdline_log) + 1;
4741 *logmsg = malloc(len + 1);
4742 if (*logmsg == NULL)
4743 return got_error_from_errno("malloc");
4744 strlcpy(*logmsg, a->cmdline_log, len);
4745 return NULL;
4748 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4749 return got_error_from_errno("asprintf");
4751 if (asprintf(&initial_content,
4752 "\n# changes to be committed on branch %s:\n",
4753 a->branch_name) == -1)
4754 return got_error_from_errno("asprintf");
4756 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4757 if (err)
4758 goto done;
4760 dprintf(fd, initial_content);
4762 TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 struct got_commitable *ct = pe->data;
4764 dprintf(fd, "# %c %s\n",
4765 got_commitable_get_status(ct),
4766 got_commitable_get_path(ct));
4768 close(fd);
4770 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4771 done:
4772 free(initial_content);
4773 free(template);
4775 /* Editor is done; we can now apply unveil(2) */
4776 if (err == NULL) {
4777 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4778 if (err) {
4779 free(*logmsg);
4780 *logmsg = NULL;
4783 return err;
4786 static const struct got_error *
4787 cmd_commit(int argc, char *argv[])
4789 const struct got_error *error = NULL;
4790 struct got_worktree *worktree = NULL;
4791 struct got_repository *repo = NULL;
4792 char *cwd = NULL, *id_str = NULL;
4793 struct got_object_id *id = NULL;
4794 const char *logmsg = NULL;
4795 struct collect_commit_logmsg_arg cl_arg;
4796 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4797 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4798 struct got_pathlist_head paths;
4800 TAILQ_INIT(&paths);
4801 cl_arg.logmsg_path = NULL;
4803 while ((ch = getopt(argc, argv, "m:")) != -1) {
4804 switch (ch) {
4805 case 'm':
4806 logmsg = optarg;
4807 break;
4808 default:
4809 usage_commit();
4810 /* NOTREACHED */
4814 argc -= optind;
4815 argv += optind;
4817 #ifndef PROFILE
4818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4819 "unveil", NULL) == -1)
4820 err(1, "pledge");
4821 #endif
4822 cwd = getcwd(NULL, 0);
4823 if (cwd == NULL) {
4824 error = got_error_from_errno("getcwd");
4825 goto done;
4827 error = got_worktree_open(&worktree, cwd);
4828 if (error)
4829 goto done;
4831 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4832 if (error)
4833 goto done;
4834 if (rebase_in_progress) {
4835 error = got_error(GOT_ERR_REBASING);
4836 goto done;
4839 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4840 worktree);
4841 if (error)
4842 goto done;
4844 error = get_gitconfig_path(&gitconfig_path);
4845 if (error)
4846 goto done;
4847 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4848 gitconfig_path);
4849 if (error != NULL)
4850 goto done;
4852 error = get_author(&author, repo);
4853 if (error)
4854 return error;
4857 * unveil(2) traverses exec(2); if an editor is used we have
4858 * to apply unveil after the log message has been written.
4860 if (logmsg == NULL || strlen(logmsg) == 0)
4861 error = get_editor(&editor);
4862 else
4863 error = apply_unveil(got_repo_get_path(repo), 0,
4864 got_worktree_get_root_path(worktree));
4865 if (error)
4866 goto done;
4868 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4869 if (error)
4870 goto done;
4872 cl_arg.editor = editor;
4873 cl_arg.cmdline_log = logmsg;
4874 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4875 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4876 if (!histedit_in_progress) {
4877 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4878 error = got_error(GOT_ERR_COMMIT_BRANCH);
4879 goto done;
4881 cl_arg.branch_name += 11;
4883 cl_arg.repo_path = got_repo_get_path(repo);
4884 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4885 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4886 if (error) {
4887 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4888 cl_arg.logmsg_path != NULL)
4889 preserve_logmsg = 1;
4890 goto done;
4893 error = got_object_id_str(&id_str, id);
4894 if (error)
4895 goto done;
4896 printf("Created commit %s\n", id_str);
4897 done:
4898 if (preserve_logmsg) {
4899 fprintf(stderr, "%s: log message preserved in %s\n",
4900 getprogname(), cl_arg.logmsg_path);
4901 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4902 error == NULL)
4903 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4904 free(cl_arg.logmsg_path);
4905 if (repo)
4906 got_repo_close(repo);
4907 if (worktree)
4908 got_worktree_close(worktree);
4909 free(cwd);
4910 free(id_str);
4911 free(gitconfig_path);
4912 free(editor);
4913 free(author);
4914 return error;
4917 __dead static void
4918 usage_cherrypick(void)
4920 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4921 exit(1);
4924 static const struct got_error *
4925 cmd_cherrypick(int argc, char *argv[])
4927 const struct got_error *error = NULL;
4928 struct got_worktree *worktree = NULL;
4929 struct got_repository *repo = NULL;
4930 char *cwd = NULL, *commit_id_str = NULL;
4931 struct got_object_id *commit_id = NULL;
4932 struct got_commit_object *commit = NULL;
4933 struct got_object_qid *pid;
4934 struct got_reference *head_ref = NULL;
4935 int ch, did_something = 0;
4937 while ((ch = getopt(argc, argv, "")) != -1) {
4938 switch (ch) {
4939 default:
4940 usage_cherrypick();
4941 /* NOTREACHED */
4945 argc -= optind;
4946 argv += optind;
4948 #ifndef PROFILE
4949 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4950 "unveil", NULL) == -1)
4951 err(1, "pledge");
4952 #endif
4953 if (argc != 1)
4954 usage_cherrypick();
4956 cwd = getcwd(NULL, 0);
4957 if (cwd == NULL) {
4958 error = got_error_from_errno("getcwd");
4959 goto done;
4961 error = got_worktree_open(&worktree, cwd);
4962 if (error)
4963 goto done;
4965 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4966 NULL);
4967 if (error != NULL)
4968 goto done;
4970 error = apply_unveil(got_repo_get_path(repo), 0,
4971 got_worktree_get_root_path(worktree));
4972 if (error)
4973 goto done;
4975 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4976 GOT_OBJ_TYPE_COMMIT, repo);
4977 if (error != NULL) {
4978 struct got_reference *ref;
4979 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4980 goto done;
4981 error = got_ref_open(&ref, repo, argv[0], 0);
4982 if (error != NULL)
4983 goto done;
4984 error = got_ref_resolve(&commit_id, repo, ref);
4985 got_ref_close(ref);
4986 if (error != NULL)
4987 goto done;
4989 error = got_object_id_str(&commit_id_str, commit_id);
4990 if (error)
4991 goto done;
4993 error = got_ref_open(&head_ref, repo,
4994 got_worktree_get_head_ref_name(worktree), 0);
4995 if (error != NULL)
4996 goto done;
4998 error = check_same_branch(commit_id, head_ref, NULL, repo);
4999 if (error) {
5000 if (error->code != GOT_ERR_ANCESTRY)
5001 goto done;
5002 error = NULL;
5003 } else {
5004 error = got_error(GOT_ERR_SAME_BRANCH);
5005 goto done;
5008 error = got_object_open_as_commit(&commit, repo, commit_id);
5009 if (error)
5010 goto done;
5011 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5012 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5013 commit_id, repo, update_progress, &did_something, check_cancelled,
5014 NULL);
5015 if (error != NULL)
5016 goto done;
5018 if (did_something)
5019 printf("Merged commit %s\n", commit_id_str);
5020 done:
5021 if (commit)
5022 got_object_commit_close(commit);
5023 free(commit_id_str);
5024 if (head_ref)
5025 got_ref_close(head_ref);
5026 if (worktree)
5027 got_worktree_close(worktree);
5028 if (repo)
5029 got_repo_close(repo);
5030 return error;
5033 __dead static void
5034 usage_backout(void)
5036 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5037 exit(1);
5040 static const struct got_error *
5041 cmd_backout(int argc, char *argv[])
5043 const struct got_error *error = NULL;
5044 struct got_worktree *worktree = NULL;
5045 struct got_repository *repo = NULL;
5046 char *cwd = NULL, *commit_id_str = NULL;
5047 struct got_object_id *commit_id = NULL;
5048 struct got_commit_object *commit = NULL;
5049 struct got_object_qid *pid;
5050 struct got_reference *head_ref = NULL;
5051 int ch, did_something = 0;
5053 while ((ch = getopt(argc, argv, "")) != -1) {
5054 switch (ch) {
5055 default:
5056 usage_backout();
5057 /* NOTREACHED */
5061 argc -= optind;
5062 argv += optind;
5064 #ifndef PROFILE
5065 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5066 "unveil", NULL) == -1)
5067 err(1, "pledge");
5068 #endif
5069 if (argc != 1)
5070 usage_backout();
5072 cwd = getcwd(NULL, 0);
5073 if (cwd == NULL) {
5074 error = got_error_from_errno("getcwd");
5075 goto done;
5077 error = got_worktree_open(&worktree, cwd);
5078 if (error)
5079 goto done;
5081 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5082 NULL);
5083 if (error != NULL)
5084 goto done;
5086 error = apply_unveil(got_repo_get_path(repo), 0,
5087 got_worktree_get_root_path(worktree));
5088 if (error)
5089 goto done;
5091 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5092 GOT_OBJ_TYPE_COMMIT, repo);
5093 if (error != NULL) {
5094 struct got_reference *ref;
5095 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5096 goto done;
5097 error = got_ref_open(&ref, repo, argv[0], 0);
5098 if (error != NULL)
5099 goto done;
5100 error = got_ref_resolve(&commit_id, repo, ref);
5101 got_ref_close(ref);
5102 if (error != NULL)
5103 goto done;
5105 error = got_object_id_str(&commit_id_str, commit_id);
5106 if (error)
5107 goto done;
5109 error = got_ref_open(&head_ref, repo,
5110 got_worktree_get_head_ref_name(worktree), 0);
5111 if (error != NULL)
5112 goto done;
5114 error = check_same_branch(commit_id, head_ref, NULL, repo);
5115 if (error)
5116 goto done;
5118 error = got_object_open_as_commit(&commit, repo, commit_id);
5119 if (error)
5120 goto done;
5121 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5122 if (pid == NULL) {
5123 error = got_error(GOT_ERR_ROOT_COMMIT);
5124 goto done;
5127 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5128 update_progress, &did_something, check_cancelled, NULL);
5129 if (error != NULL)
5130 goto done;
5132 if (did_something)
5133 printf("Backed out commit %s\n", commit_id_str);
5134 done:
5135 if (commit)
5136 got_object_commit_close(commit);
5137 free(commit_id_str);
5138 if (head_ref)
5139 got_ref_close(head_ref);
5140 if (worktree)
5141 got_worktree_close(worktree);
5142 if (repo)
5143 got_repo_close(repo);
5144 return error;
5147 __dead static void
5148 usage_rebase(void)
5150 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5151 getprogname());
5152 exit(1);
5155 void
5156 trim_logmsg(char *logmsg, int limit)
5158 char *nl;
5159 size_t len;
5161 len = strlen(logmsg);
5162 if (len > limit)
5163 len = limit;
5164 logmsg[len] = '\0';
5165 nl = strchr(logmsg, '\n');
5166 if (nl)
5167 *nl = '\0';
5170 static const struct got_error *
5171 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5173 const struct got_error *err;
5174 char *logmsg0 = NULL;
5175 const char *s;
5177 err = got_object_commit_get_logmsg(&logmsg0, commit);
5178 if (err)
5179 return err;
5181 s = logmsg0;
5182 while (isspace((unsigned char)s[0]))
5183 s++;
5185 *logmsg = strdup(s);
5186 if (*logmsg == NULL) {
5187 err = got_error_from_errno("strdup");
5188 goto done;
5191 trim_logmsg(*logmsg, limit);
5192 done:
5193 free(logmsg0);
5194 return err;
5197 static const struct got_error *
5198 show_rebase_progress(struct got_commit_object *commit,
5199 struct got_object_id *old_id, struct got_object_id *new_id)
5201 const struct got_error *err;
5202 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5204 err = got_object_id_str(&old_id_str, old_id);
5205 if (err)
5206 goto done;
5208 if (new_id) {
5209 err = got_object_id_str(&new_id_str, new_id);
5210 if (err)
5211 goto done;
5214 old_id_str[12] = '\0';
5215 if (new_id_str)
5216 new_id_str[12] = '\0';
5218 err = get_short_logmsg(&logmsg, 42, commit);
5219 if (err)
5220 goto done;
5222 printf("%s -> %s: %s\n", old_id_str,
5223 new_id_str ? new_id_str : "no-op change", logmsg);
5224 done:
5225 free(old_id_str);
5226 free(new_id_str);
5227 return err;
5230 static const struct got_error *
5231 rebase_progress(void *arg, unsigned char status, const char *path)
5233 unsigned char *rebase_status = arg;
5235 while (path[0] == '/')
5236 path++;
5237 printf("%c %s\n", status, path);
5239 if (*rebase_status == GOT_STATUS_CONFLICT)
5240 return NULL;
5241 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5242 *rebase_status = status;
5243 return NULL;
5246 static const struct got_error *
5247 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5248 struct got_reference *branch, struct got_reference *new_base_branch,
5249 struct got_reference *tmp_branch, struct got_repository *repo)
5251 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5252 return got_worktree_rebase_complete(worktree, fileindex,
5253 new_base_branch, tmp_branch, branch, repo);
5256 static const struct got_error *
5257 rebase_commit(struct got_pathlist_head *merged_paths,
5258 struct got_worktree *worktree, struct got_fileindex *fileindex,
5259 struct got_reference *tmp_branch,
5260 struct got_object_id *commit_id, struct got_repository *repo)
5262 const struct got_error *error;
5263 struct got_commit_object *commit;
5264 struct got_object_id *new_commit_id;
5266 error = got_object_open_as_commit(&commit, repo, commit_id);
5267 if (error)
5268 return error;
5270 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5271 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5272 if (error) {
5273 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5274 goto done;
5275 error = show_rebase_progress(commit, commit_id, NULL);
5276 } else {
5277 error = show_rebase_progress(commit, commit_id, new_commit_id);
5278 free(new_commit_id);
5280 done:
5281 got_object_commit_close(commit);
5282 return error;
5285 struct check_path_prefix_arg {
5286 const char *path_prefix;
5287 size_t len;
5288 int errcode;
5291 static const struct got_error *
5292 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5293 struct got_blob_object *blob2, struct got_object_id *id1,
5294 struct got_object_id *id2, const char *path1, const char *path2,
5295 mode_t mode1, mode_t mode2, struct got_repository *repo)
5297 struct check_path_prefix_arg *a = arg;
5299 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5300 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5301 return got_error(a->errcode);
5303 return NULL;
5306 static const struct got_error *
5307 check_path_prefix(struct got_object_id *parent_id,
5308 struct got_object_id *commit_id, const char *path_prefix,
5309 int errcode, struct got_repository *repo)
5311 const struct got_error *err;
5312 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5313 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5314 struct check_path_prefix_arg cpp_arg;
5316 if (got_path_is_root_dir(path_prefix))
5317 return NULL;
5319 err = got_object_open_as_commit(&commit, repo, commit_id);
5320 if (err)
5321 goto done;
5323 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5324 if (err)
5325 goto done;
5327 err = got_object_open_as_tree(&tree1, repo,
5328 got_object_commit_get_tree_id(parent_commit));
5329 if (err)
5330 goto done;
5332 err = got_object_open_as_tree(&tree2, repo,
5333 got_object_commit_get_tree_id(commit));
5334 if (err)
5335 goto done;
5337 cpp_arg.path_prefix = path_prefix;
5338 while (cpp_arg.path_prefix[0] == '/')
5339 cpp_arg.path_prefix++;
5340 cpp_arg.len = strlen(cpp_arg.path_prefix);
5341 cpp_arg.errcode = errcode;
5342 err = got_diff_tree(tree1, tree2, "", "", repo,
5343 check_path_prefix_in_diff, &cpp_arg, 0);
5344 done:
5345 if (tree1)
5346 got_object_tree_close(tree1);
5347 if (tree2)
5348 got_object_tree_close(tree2);
5349 if (commit)
5350 got_object_commit_close(commit);
5351 if (parent_commit)
5352 got_object_commit_close(parent_commit);
5353 return err;
5356 static const struct got_error *
5357 collect_commits(struct got_object_id_queue *commits,
5358 struct got_object_id *initial_commit_id,
5359 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5360 const char *path_prefix, int path_prefix_errcode,
5361 struct got_repository *repo)
5363 const struct got_error *err = NULL;
5364 struct got_commit_graph *graph = NULL;
5365 struct got_object_id *parent_id = NULL;
5366 struct got_object_qid *qid;
5367 struct got_object_id *commit_id = initial_commit_id;
5369 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5370 if (err)
5371 return err;
5373 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5374 check_cancelled, NULL);
5375 if (err)
5376 goto done;
5377 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5378 err = got_commit_graph_iter_next(&parent_id, graph);
5379 if (err) {
5380 if (err->code == GOT_ERR_ITER_COMPLETED) {
5381 err = got_error_msg(GOT_ERR_ANCESTRY,
5382 "ran out of commits to rebase before "
5383 "youngest common ancestor commit has "
5384 "been reached?!?");
5385 goto done;
5386 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5387 goto done;
5388 err = got_commit_graph_fetch_commits(graph, 1, repo,
5389 check_cancelled, NULL);
5390 if (err)
5391 goto done;
5392 } else {
5393 err = check_path_prefix(parent_id, commit_id,
5394 path_prefix, path_prefix_errcode, repo);
5395 if (err)
5396 goto done;
5398 err = got_object_qid_alloc(&qid, commit_id);
5399 if (err)
5400 goto done;
5401 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5402 commit_id = parent_id;
5405 done:
5406 got_commit_graph_close(graph);
5407 return err;
5410 static const struct got_error *
5411 cmd_rebase(int argc, char *argv[])
5413 const struct got_error *error = NULL;
5414 struct got_worktree *worktree = NULL;
5415 struct got_repository *repo = NULL;
5416 struct got_fileindex *fileindex = NULL;
5417 char *cwd = NULL;
5418 struct got_reference *branch = NULL;
5419 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5420 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5421 struct got_object_id *resume_commit_id = NULL;
5422 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5423 struct got_commit_object *commit = NULL;
5424 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5425 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5426 struct got_object_id_queue commits;
5427 struct got_pathlist_head merged_paths;
5428 const struct got_object_id_queue *parent_ids;
5429 struct got_object_qid *qid, *pid;
5431 SIMPLEQ_INIT(&commits);
5432 TAILQ_INIT(&merged_paths);
5434 while ((ch = getopt(argc, argv, "ac")) != -1) {
5435 switch (ch) {
5436 case 'a':
5437 abort_rebase = 1;
5438 break;
5439 case 'c':
5440 continue_rebase = 1;
5441 break;
5442 default:
5443 usage_rebase();
5444 /* NOTREACHED */
5448 argc -= optind;
5449 argv += optind;
5451 #ifndef PROFILE
5452 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5453 "unveil", NULL) == -1)
5454 err(1, "pledge");
5455 #endif
5456 if (abort_rebase && continue_rebase)
5457 usage_rebase();
5458 else if (abort_rebase || continue_rebase) {
5459 if (argc != 0)
5460 usage_rebase();
5461 } else if (argc != 1)
5462 usage_rebase();
5464 cwd = getcwd(NULL, 0);
5465 if (cwd == NULL) {
5466 error = got_error_from_errno("getcwd");
5467 goto done;
5469 error = got_worktree_open(&worktree, cwd);
5470 if (error)
5471 goto done;
5473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5474 NULL);
5475 if (error != NULL)
5476 goto done;
5478 error = apply_unveil(got_repo_get_path(repo), 0,
5479 got_worktree_get_root_path(worktree));
5480 if (error)
5481 goto done;
5483 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5484 if (error)
5485 goto done;
5487 if (abort_rebase) {
5488 int did_something;
5489 if (!rebase_in_progress) {
5490 error = got_error(GOT_ERR_NOT_REBASING);
5491 goto done;
5493 error = got_worktree_rebase_continue(&resume_commit_id,
5494 &new_base_branch, &tmp_branch, &branch, &fileindex,
5495 worktree, repo);
5496 if (error)
5497 goto done;
5498 printf("Switching work tree to %s\n",
5499 got_ref_get_symref_target(new_base_branch));
5500 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5501 new_base_branch, update_progress, &did_something);
5502 if (error)
5503 goto done;
5504 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5505 goto done; /* nothing else to do */
5508 if (continue_rebase) {
5509 if (!rebase_in_progress) {
5510 error = got_error(GOT_ERR_NOT_REBASING);
5511 goto done;
5513 error = got_worktree_rebase_continue(&resume_commit_id,
5514 &new_base_branch, &tmp_branch, &branch, &fileindex,
5515 worktree, repo);
5516 if (error)
5517 goto done;
5519 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5520 resume_commit_id, repo);
5521 if (error)
5522 goto done;
5524 yca_id = got_object_id_dup(resume_commit_id);
5525 if (yca_id == NULL) {
5526 error = got_error_from_errno("got_object_id_dup");
5527 goto done;
5529 } else {
5530 error = got_ref_open(&branch, repo, argv[0], 0);
5531 if (error != NULL)
5532 goto done;
5535 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5536 if (error)
5537 goto done;
5539 if (!continue_rebase) {
5540 struct got_object_id *base_commit_id;
5542 base_commit_id = got_worktree_get_base_commit_id(worktree);
5543 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5544 base_commit_id, branch_head_commit_id, repo,
5545 check_cancelled, NULL);
5546 if (error)
5547 goto done;
5548 if (yca_id == NULL) {
5549 error = got_error_msg(GOT_ERR_ANCESTRY,
5550 "specified branch shares no common ancestry "
5551 "with work tree's branch");
5552 goto done;
5555 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5556 if (error) {
5557 if (error->code != GOT_ERR_ANCESTRY)
5558 goto done;
5559 error = NULL;
5560 } else {
5561 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5562 "specified branch resolves to a commit which "
5563 "is already contained in work tree's branch");
5564 goto done;
5566 error = got_worktree_rebase_prepare(&new_base_branch,
5567 &tmp_branch, &fileindex, worktree, branch, repo);
5568 if (error)
5569 goto done;
5572 commit_id = branch_head_commit_id;
5573 error = got_object_open_as_commit(&commit, repo, commit_id);
5574 if (error)
5575 goto done;
5577 parent_ids = got_object_commit_get_parent_ids(commit);
5578 pid = SIMPLEQ_FIRST(parent_ids);
5579 if (pid == NULL) {
5580 if (!continue_rebase) {
5581 int did_something;
5582 error = got_worktree_rebase_abort(worktree, fileindex,
5583 repo, new_base_branch, update_progress,
5584 &did_something);
5585 if (error)
5586 goto done;
5587 printf("Rebase of %s aborted\n",
5588 got_ref_get_name(branch));
5590 error = got_error(GOT_ERR_EMPTY_REBASE);
5591 goto done;
5593 error = collect_commits(&commits, commit_id, pid->id,
5594 yca_id, got_worktree_get_path_prefix(worktree),
5595 GOT_ERR_REBASE_PATH, repo);
5596 got_object_commit_close(commit);
5597 commit = NULL;
5598 if (error)
5599 goto done;
5601 if (SIMPLEQ_EMPTY(&commits)) {
5602 if (continue_rebase) {
5603 error = rebase_complete(worktree, fileindex,
5604 branch, new_base_branch, tmp_branch, repo);
5605 goto done;
5606 } else {
5607 /* Fast-forward the reference of the branch. */
5608 struct got_object_id *new_head_commit_id;
5609 char *id_str;
5610 error = got_ref_resolve(&new_head_commit_id, repo,
5611 new_base_branch);
5612 if (error)
5613 goto done;
5614 error = got_object_id_str(&id_str, new_head_commit_id);
5615 printf("Forwarding %s to commit %s\n",
5616 got_ref_get_name(branch), id_str);
5617 free(id_str);
5618 error = got_ref_change_ref(branch,
5619 new_head_commit_id);
5620 if (error)
5621 goto done;
5625 pid = NULL;
5626 SIMPLEQ_FOREACH(qid, &commits, entry) {
5627 commit_id = qid->id;
5628 parent_id = pid ? pid->id : yca_id;
5629 pid = qid;
5631 error = got_worktree_rebase_merge_files(&merged_paths,
5632 worktree, fileindex, parent_id, commit_id, repo,
5633 rebase_progress, &rebase_status, check_cancelled, NULL);
5634 if (error)
5635 goto done;
5637 if (rebase_status == GOT_STATUS_CONFLICT) {
5638 got_worktree_rebase_pathlist_free(&merged_paths);
5639 break;
5642 error = rebase_commit(&merged_paths, worktree, fileindex,
5643 tmp_branch, commit_id, repo);
5644 got_worktree_rebase_pathlist_free(&merged_paths);
5645 if (error)
5646 goto done;
5649 if (rebase_status == GOT_STATUS_CONFLICT) {
5650 error = got_worktree_rebase_postpone(worktree, fileindex);
5651 if (error)
5652 goto done;
5653 error = got_error_msg(GOT_ERR_CONFLICTS,
5654 "conflicts must be resolved before rebasing can continue");
5655 } else
5656 error = rebase_complete(worktree, fileindex, branch,
5657 new_base_branch, tmp_branch, repo);
5658 done:
5659 got_object_id_queue_free(&commits);
5660 free(branch_head_commit_id);
5661 free(resume_commit_id);
5662 free(yca_id);
5663 if (commit)
5664 got_object_commit_close(commit);
5665 if (branch)
5666 got_ref_close(branch);
5667 if (new_base_branch)
5668 got_ref_close(new_base_branch);
5669 if (tmp_branch)
5670 got_ref_close(tmp_branch);
5671 if (worktree)
5672 got_worktree_close(worktree);
5673 if (repo)
5674 got_repo_close(repo);
5675 return error;
5678 __dead static void
5679 usage_histedit(void)
5681 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5682 getprogname());
5683 exit(1);
5686 #define GOT_HISTEDIT_PICK 'p'
5687 #define GOT_HISTEDIT_EDIT 'e'
5688 #define GOT_HISTEDIT_FOLD 'f'
5689 #define GOT_HISTEDIT_DROP 'd'
5690 #define GOT_HISTEDIT_MESG 'm'
5692 static struct got_histedit_cmd {
5693 unsigned char code;
5694 const char *name;
5695 const char *desc;
5696 } got_histedit_cmds[] = {
5697 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5698 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5699 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5700 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5701 { GOT_HISTEDIT_MESG, "mesg",
5702 "single-line log message for commit above (open editor if empty)" },
5705 struct got_histedit_list_entry {
5706 TAILQ_ENTRY(got_histedit_list_entry) entry;
5707 struct got_object_id *commit_id;
5708 const struct got_histedit_cmd *cmd;
5709 char *logmsg;
5711 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5713 static const struct got_error *
5714 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5715 FILE *f, struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 char *logmsg = NULL, *id_str = NULL;
5719 struct got_commit_object *commit = NULL;
5720 int n;
5722 err = got_object_open_as_commit(&commit, repo, commit_id);
5723 if (err)
5724 goto done;
5726 err = get_short_logmsg(&logmsg, 34, commit);
5727 if (err)
5728 goto done;
5730 err = got_object_id_str(&id_str, commit_id);
5731 if (err)
5732 goto done;
5734 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5735 if (n < 0)
5736 err = got_ferror(f, GOT_ERR_IO);
5737 done:
5738 if (commit)
5739 got_object_commit_close(commit);
5740 free(id_str);
5741 free(logmsg);
5742 return err;
5745 static const struct got_error *
5746 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5747 struct got_repository *repo)
5749 const struct got_error *err = NULL;
5750 struct got_object_qid *qid;
5752 if (SIMPLEQ_EMPTY(commits))
5753 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5755 SIMPLEQ_FOREACH(qid, commits, entry) {
5756 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5757 f, repo);
5758 if (err)
5759 break;
5762 return err;
5765 static const struct got_error *
5766 write_cmd_list(FILE *f)
5768 const struct got_error *err = NULL;
5769 int n, i;
5771 n = fprintf(f, "# Available histedit commands:\n");
5772 if (n < 0)
5773 return got_ferror(f, GOT_ERR_IO);
5775 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5776 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5777 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5778 cmd->desc);
5779 if (n < 0) {
5780 err = got_ferror(f, GOT_ERR_IO);
5781 break;
5784 n = fprintf(f, "# Commits will be processed in order from top to "
5785 "bottom of this file.\n");
5786 if (n < 0)
5787 return got_ferror(f, GOT_ERR_IO);
5788 return err;
5791 static const struct got_error *
5792 histedit_syntax_error(int lineno)
5794 static char msg[42];
5795 int ret;
5797 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5798 lineno);
5799 if (ret == -1 || ret >= sizeof(msg))
5800 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5802 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5805 static const struct got_error *
5806 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5807 char *logmsg, struct got_repository *repo)
5809 const struct got_error *err;
5810 struct got_commit_object *folded_commit = NULL;
5811 char *id_str, *folded_logmsg = NULL;
5813 err = got_object_id_str(&id_str, hle->commit_id);
5814 if (err)
5815 return err;
5817 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5818 if (err)
5819 goto done;
5821 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5822 if (err)
5823 goto done;
5824 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5825 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5826 folded_logmsg) == -1) {
5827 err = got_error_from_errno("asprintf");
5828 goto done;
5830 done:
5831 if (folded_commit)
5832 got_object_commit_close(folded_commit);
5833 free(id_str);
5834 free(folded_logmsg);
5835 return err;
5838 static struct got_histedit_list_entry *
5839 get_folded_commits(struct got_histedit_list_entry *hle)
5841 struct got_histedit_list_entry *prev, *folded = NULL;
5843 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5844 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5845 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5846 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5847 folded = prev;
5848 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5851 return folded;
5854 static const struct got_error *
5855 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5856 struct got_repository *repo)
5858 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5859 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5860 const struct got_error *err = NULL;
5861 struct got_commit_object *commit = NULL;
5862 int fd;
5863 struct got_histedit_list_entry *folded = NULL;
5865 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5866 if (err)
5867 return err;
5869 folded = get_folded_commits(hle);
5870 if (folded) {
5871 while (folded != hle) {
5872 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5873 folded = TAILQ_NEXT(folded, entry);
5874 continue;
5876 err = append_folded_commit_msg(&new_msg, folded,
5877 logmsg, repo);
5878 if (err)
5879 goto done;
5880 free(logmsg);
5881 logmsg = new_msg;
5882 folded = TAILQ_NEXT(folded, entry);
5886 err = got_object_id_str(&id_str, hle->commit_id);
5887 if (err)
5888 goto done;
5889 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5890 if (err)
5891 goto done;
5892 if (asprintf(&new_msg,
5893 "%s\n# original log message of commit %s: %s",
5894 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5895 err = got_error_from_errno("asprintf");
5896 goto done;
5898 free(logmsg);
5899 logmsg = new_msg;
5901 err = got_object_id_str(&id_str, hle->commit_id);
5902 if (err)
5903 goto done;
5905 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5906 if (err)
5907 goto done;
5909 dprintf(fd, logmsg);
5910 close(fd);
5912 err = get_editor(&editor);
5913 if (err)
5914 goto done;
5916 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5917 if (err) {
5918 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5919 goto done;
5920 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5922 done:
5923 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5924 err = got_error_from_errno2("unlink", logmsg_path);
5925 free(logmsg_path);
5926 free(logmsg);
5927 free(orig_logmsg);
5928 free(editor);
5929 if (commit)
5930 got_object_commit_close(commit);
5931 return err;
5934 static const struct got_error *
5935 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5936 FILE *f, struct got_repository *repo)
5938 const struct got_error *err = NULL;
5939 char *line = NULL, *p, *end;
5940 size_t size;
5941 ssize_t len;
5942 int lineno = 0, i;
5943 const struct got_histedit_cmd *cmd;
5944 struct got_object_id *commit_id = NULL;
5945 struct got_histedit_list_entry *hle = NULL;
5947 for (;;) {
5948 len = getline(&line, &size, f);
5949 if (len == -1) {
5950 const struct got_error *getline_err;
5951 if (feof(f))
5952 break;
5953 getline_err = got_error_from_errno("getline");
5954 err = got_ferror(f, getline_err->code);
5955 break;
5957 lineno++;
5958 p = line;
5959 while (isspace((unsigned char)p[0]))
5960 p++;
5961 if (p[0] == '#' || p[0] == '\0') {
5962 free(line);
5963 line = NULL;
5964 continue;
5966 cmd = NULL;
5967 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5968 cmd = &got_histedit_cmds[i];
5969 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5970 isspace((unsigned char)p[strlen(cmd->name)])) {
5971 p += strlen(cmd->name);
5972 break;
5974 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5975 p++;
5976 break;
5979 if (i == nitems(got_histedit_cmds)) {
5980 err = histedit_syntax_error(lineno);
5981 break;
5983 while (isspace((unsigned char)p[0]))
5984 p++;
5985 if (cmd->code == GOT_HISTEDIT_MESG) {
5986 if (hle == NULL || hle->logmsg != NULL) {
5987 err = got_error(GOT_ERR_HISTEDIT_CMD);
5988 break;
5990 if (p[0] == '\0') {
5991 err = histedit_edit_logmsg(hle, repo);
5992 if (err)
5993 break;
5994 } else {
5995 hle->logmsg = strdup(p);
5996 if (hle->logmsg == NULL) {
5997 err = got_error_from_errno("strdup");
5998 break;
6001 free(line);
6002 line = NULL;
6003 continue;
6004 } else {
6005 end = p;
6006 while (end[0] && !isspace((unsigned char)end[0]))
6007 end++;
6008 *end = '\0';
6010 err = got_object_resolve_id_str(&commit_id, repo, p);
6011 if (err) {
6012 /* override error code */
6013 err = histedit_syntax_error(lineno);
6014 break;
6017 hle = malloc(sizeof(*hle));
6018 if (hle == NULL) {
6019 err = got_error_from_errno("malloc");
6020 break;
6022 hle->cmd = cmd;
6023 hle->commit_id = commit_id;
6024 hle->logmsg = NULL;
6025 commit_id = NULL;
6026 free(line);
6027 line = NULL;
6028 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6031 free(line);
6032 free(commit_id);
6033 return err;
6036 static const struct got_error *
6037 histedit_check_script(struct got_histedit_list *histedit_cmds,
6038 struct got_object_id_queue *commits, struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 struct got_object_qid *qid;
6042 struct got_histedit_list_entry *hle;
6043 static char msg[80];
6044 char *id_str;
6046 if (TAILQ_EMPTY(histedit_cmds))
6047 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6048 "histedit script contains no commands");
6049 if (SIMPLEQ_EMPTY(commits))
6050 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6052 SIMPLEQ_FOREACH(qid, commits, entry) {
6053 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6054 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6055 break;
6057 if (hle == NULL) {
6058 err = got_object_id_str(&id_str, qid->id);
6059 if (err)
6060 return err;
6061 snprintf(msg, sizeof(msg),
6062 "commit %s missing from histedit script", id_str);
6063 free(id_str);
6064 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6068 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6069 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6070 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6071 "last commit in histedit script cannot be folded");
6073 return NULL;
6076 static const struct got_error *
6077 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6078 const char *path, struct got_object_id_queue *commits,
6079 struct got_repository *repo)
6081 const struct got_error *err = NULL;
6082 char *editor;
6083 FILE *f = NULL;
6085 err = get_editor(&editor);
6086 if (err)
6087 return err;
6089 if (spawn_editor(editor, path) == -1) {
6090 err = got_error_from_errno("failed spawning editor");
6091 goto done;
6094 f = fopen(path, "r");
6095 if (f == NULL) {
6096 err = got_error_from_errno("fopen");
6097 goto done;
6099 err = histedit_parse_list(histedit_cmds, f, repo);
6100 if (err)
6101 goto done;
6103 err = histedit_check_script(histedit_cmds, commits, repo);
6104 done:
6105 if (f && fclose(f) != 0 && err == NULL)
6106 err = got_error_from_errno("fclose");
6107 free(editor);
6108 return err;
6111 static const struct got_error *
6112 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6113 struct got_object_id_queue *, const char *, struct got_repository *);
6115 static const struct got_error *
6116 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6117 struct got_object_id_queue *commits, struct got_repository *repo)
6119 const struct got_error *err;
6120 FILE *f = NULL;
6121 char *path = NULL;
6123 err = got_opentemp_named(&path, &f, "got-histedit");
6124 if (err)
6125 return err;
6127 err = write_cmd_list(f);
6128 if (err)
6129 goto done;
6131 err = histedit_write_commit_list(commits, f, repo);
6132 if (err)
6133 goto done;
6135 if (fclose(f) != 0) {
6136 err = got_error_from_errno("fclose");
6137 goto done;
6139 f = NULL;
6141 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6142 if (err) {
6143 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6144 err->code != GOT_ERR_HISTEDIT_CMD)
6145 goto done;
6146 err = histedit_edit_list_retry(histedit_cmds, err,
6147 commits, path, repo);
6149 done:
6150 if (f && fclose(f) != 0 && err == NULL)
6151 err = got_error_from_errno("fclose");
6152 if (path && unlink(path) != 0 && err == NULL)
6153 err = got_error_from_errno2("unlink", path);
6154 free(path);
6155 return err;
6158 static const struct got_error *
6159 histedit_save_list(struct got_histedit_list *histedit_cmds,
6160 struct got_worktree *worktree, struct got_repository *repo)
6162 const struct got_error *err = NULL;
6163 char *path = NULL;
6164 FILE *f = NULL;
6165 struct got_histedit_list_entry *hle;
6166 struct got_commit_object *commit = NULL;
6168 err = got_worktree_get_histedit_script_path(&path, worktree);
6169 if (err)
6170 return err;
6172 f = fopen(path, "w");
6173 if (f == NULL) {
6174 err = got_error_from_errno2("fopen", path);
6175 goto done;
6177 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6178 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6179 repo);
6180 if (err)
6181 break;
6183 if (hle->logmsg) {
6184 int n = fprintf(f, "%c %s\n",
6185 GOT_HISTEDIT_MESG, hle->logmsg);
6186 if (n < 0) {
6187 err = got_ferror(f, GOT_ERR_IO);
6188 break;
6192 done:
6193 if (f && fclose(f) != 0 && err == NULL)
6194 err = got_error_from_errno("fclose");
6195 free(path);
6196 if (commit)
6197 got_object_commit_close(commit);
6198 return err;
6201 void
6202 histedit_free_list(struct got_histedit_list *histedit_cmds)
6204 struct got_histedit_list_entry *hle;
6206 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6207 TAILQ_REMOVE(histedit_cmds, hle, entry);
6208 free(hle);
6212 static const struct got_error *
6213 histedit_load_list(struct got_histedit_list *histedit_cmds,
6214 const char *path, struct got_repository *repo)
6216 const struct got_error *err = NULL;
6217 FILE *f = NULL;
6219 f = fopen(path, "r");
6220 if (f == NULL) {
6221 err = got_error_from_errno2("fopen", path);
6222 goto done;
6225 err = histedit_parse_list(histedit_cmds, f, repo);
6226 done:
6227 if (f && fclose(f) != 0 && err == NULL)
6228 err = got_error_from_errno("fclose");
6229 return err;
6232 static const struct got_error *
6233 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6234 const struct got_error *edit_err, struct got_object_id_queue *commits,
6235 const char *path, struct got_repository *repo)
6237 const struct got_error *err = NULL, *prev_err = edit_err;
6238 int resp = ' ';
6240 while (resp != 'c' && resp != 'r' && resp != 'a') {
6241 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6242 "or (a)bort: ", getprogname(), prev_err->msg);
6243 resp = getchar();
6244 if (resp == '\n')
6245 resp = getchar();
6246 if (resp == 'c') {
6247 histedit_free_list(histedit_cmds);
6248 err = histedit_run_editor(histedit_cmds, path, commits,
6249 repo);
6250 if (err) {
6251 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6252 err->code != GOT_ERR_HISTEDIT_CMD)
6253 break;
6254 prev_err = err;
6255 resp = ' ';
6256 continue;
6258 break;
6259 } else if (resp == 'r') {
6260 histedit_free_list(histedit_cmds);
6261 err = histedit_edit_script(histedit_cmds,
6262 commits, repo);
6263 if (err) {
6264 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6265 err->code != GOT_ERR_HISTEDIT_CMD)
6266 break;
6267 prev_err = err;
6268 resp = ' ';
6269 continue;
6271 break;
6272 } else if (resp == 'a') {
6273 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6274 break;
6275 } else
6276 printf("invalid response '%c'\n", resp);
6279 return err;
6282 static const struct got_error *
6283 histedit_complete(struct got_worktree *worktree,
6284 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6285 struct got_reference *branch, struct got_repository *repo)
6287 printf("Switching work tree to %s\n",
6288 got_ref_get_symref_target(branch));
6289 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6290 branch, repo);
6293 static const struct got_error *
6294 show_histedit_progress(struct got_commit_object *commit,
6295 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6297 const struct got_error *err;
6298 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6300 err = got_object_id_str(&old_id_str, hle->commit_id);
6301 if (err)
6302 goto done;
6304 if (new_id) {
6305 err = got_object_id_str(&new_id_str, new_id);
6306 if (err)
6307 goto done;
6310 old_id_str[12] = '\0';
6311 if (new_id_str)
6312 new_id_str[12] = '\0';
6314 if (hle->logmsg) {
6315 logmsg = strdup(hle->logmsg);
6316 if (logmsg == NULL) {
6317 err = got_error_from_errno("strdup");
6318 goto done;
6320 trim_logmsg(logmsg, 42);
6321 } else {
6322 err = get_short_logmsg(&logmsg, 42, commit);
6323 if (err)
6324 goto done;
6327 switch (hle->cmd->code) {
6328 case GOT_HISTEDIT_PICK:
6329 case GOT_HISTEDIT_EDIT:
6330 printf("%s -> %s: %s\n", old_id_str,
6331 new_id_str ? new_id_str : "no-op change", logmsg);
6332 break;
6333 case GOT_HISTEDIT_DROP:
6334 case GOT_HISTEDIT_FOLD:
6335 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6336 logmsg);
6337 break;
6338 default:
6339 break;
6342 done:
6343 free(old_id_str);
6344 free(new_id_str);
6345 return err;
6348 static const struct got_error *
6349 histedit_commit(struct got_pathlist_head *merged_paths,
6350 struct got_worktree *worktree, struct got_fileindex *fileindex,
6351 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6352 struct got_repository *repo)
6354 const struct got_error *err;
6355 struct got_commit_object *commit;
6356 struct got_object_id *new_commit_id;
6358 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6359 && hle->logmsg == NULL) {
6360 err = histedit_edit_logmsg(hle, repo);
6361 if (err)
6362 return err;
6365 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6366 if (err)
6367 return err;
6369 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6370 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6371 hle->logmsg, repo);
6372 if (err) {
6373 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6374 goto done;
6375 err = show_histedit_progress(commit, hle, NULL);
6376 } else {
6377 err = show_histedit_progress(commit, hle, new_commit_id);
6378 free(new_commit_id);
6380 done:
6381 got_object_commit_close(commit);
6382 return err;
6385 static const struct got_error *
6386 histedit_skip_commit(struct got_histedit_list_entry *hle,
6387 struct got_worktree *worktree, struct got_repository *repo)
6389 const struct got_error *error;
6390 struct got_commit_object *commit;
6392 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6393 repo);
6394 if (error)
6395 return error;
6397 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6398 if (error)
6399 return error;
6401 error = show_histedit_progress(commit, hle, NULL);
6402 got_object_commit_close(commit);
6403 return error;
6406 static const struct got_error *
6407 cmd_histedit(int argc, char *argv[])
6409 const struct got_error *error = NULL;
6410 struct got_worktree *worktree = NULL;
6411 struct got_fileindex *fileindex = NULL;
6412 struct got_repository *repo = NULL;
6413 char *cwd = NULL;
6414 struct got_reference *branch = NULL;
6415 struct got_reference *tmp_branch = NULL;
6416 struct got_object_id *resume_commit_id = NULL;
6417 struct got_object_id *base_commit_id = NULL;
6418 struct got_object_id *head_commit_id = NULL;
6419 struct got_commit_object *commit = NULL;
6420 int ch, rebase_in_progress = 0, did_something;
6421 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6422 const char *edit_script_path = NULL;
6423 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6424 struct got_object_id_queue commits;
6425 struct got_pathlist_head merged_paths;
6426 const struct got_object_id_queue *parent_ids;
6427 struct got_object_qid *pid;
6428 struct got_histedit_list histedit_cmds;
6429 struct got_histedit_list_entry *hle;
6431 SIMPLEQ_INIT(&commits);
6432 TAILQ_INIT(&histedit_cmds);
6433 TAILQ_INIT(&merged_paths);
6435 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6436 switch (ch) {
6437 case 'a':
6438 abort_edit = 1;
6439 break;
6440 case 'c':
6441 continue_edit = 1;
6442 break;
6443 case 'F':
6444 edit_script_path = optarg;
6445 break;
6446 default:
6447 usage_histedit();
6448 /* NOTREACHED */
6452 argc -= optind;
6453 argv += optind;
6455 #ifndef PROFILE
6456 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6457 "unveil", NULL) == -1)
6458 err(1, "pledge");
6459 #endif
6460 if (abort_edit && continue_edit)
6461 usage_histedit();
6462 if (argc != 0)
6463 usage_histedit();
6466 * This command cannot apply unveil(2) in all cases because the
6467 * user may choose to run an editor to edit the histedit script
6468 * and to edit individual commit log messages.
6469 * unveil(2) traverses exec(2); if an editor is used we have to
6470 * apply unveil after edit script and log messages have been written.
6471 * XXX TODO: Make use of unveil(2) where possible.
6474 cwd = getcwd(NULL, 0);
6475 if (cwd == NULL) {
6476 error = got_error_from_errno("getcwd");
6477 goto done;
6479 error = got_worktree_open(&worktree, cwd);
6480 if (error)
6481 goto done;
6483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6484 NULL);
6485 if (error != NULL)
6486 goto done;
6488 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6489 if (error)
6490 goto done;
6491 if (rebase_in_progress) {
6492 error = got_error(GOT_ERR_REBASING);
6493 goto done;
6496 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6497 if (error)
6498 goto done;
6500 if (edit_in_progress && abort_edit) {
6501 error = got_worktree_histedit_continue(&resume_commit_id,
6502 &tmp_branch, &branch, &base_commit_id, &fileindex,
6503 worktree, repo);
6504 if (error)
6505 goto done;
6506 printf("Switching work tree to %s\n",
6507 got_ref_get_symref_target(branch));
6508 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6509 branch, base_commit_id, update_progress, &did_something);
6510 if (error)
6511 goto done;
6512 printf("Histedit of %s aborted\n",
6513 got_ref_get_symref_target(branch));
6514 goto done; /* nothing else to do */
6515 } else if (abort_edit) {
6516 error = got_error(GOT_ERR_NOT_HISTEDIT);
6517 goto done;
6520 if (continue_edit) {
6521 char *path;
6523 if (!edit_in_progress) {
6524 error = got_error(GOT_ERR_NOT_HISTEDIT);
6525 goto done;
6528 error = got_worktree_get_histedit_script_path(&path, worktree);
6529 if (error)
6530 goto done;
6532 error = histedit_load_list(&histedit_cmds, path, repo);
6533 free(path);
6534 if (error)
6535 goto done;
6537 error = got_worktree_histedit_continue(&resume_commit_id,
6538 &tmp_branch, &branch, &base_commit_id, &fileindex,
6539 worktree, repo);
6540 if (error)
6541 goto done;
6543 error = got_ref_resolve(&head_commit_id, repo, branch);
6544 if (error)
6545 goto done;
6547 error = got_object_open_as_commit(&commit, repo,
6548 head_commit_id);
6549 if (error)
6550 goto done;
6551 parent_ids = got_object_commit_get_parent_ids(commit);
6552 pid = SIMPLEQ_FIRST(parent_ids);
6553 if (pid == NULL) {
6554 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6555 goto done;
6557 error = collect_commits(&commits, head_commit_id, pid->id,
6558 base_commit_id, got_worktree_get_path_prefix(worktree),
6559 GOT_ERR_HISTEDIT_PATH, repo);
6560 got_object_commit_close(commit);
6561 commit = NULL;
6562 if (error)
6563 goto done;
6564 } else {
6565 if (edit_in_progress) {
6566 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6567 goto done;
6570 error = got_ref_open(&branch, repo,
6571 got_worktree_get_head_ref_name(worktree), 0);
6572 if (error != NULL)
6573 goto done;
6575 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6576 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6577 "will not edit commit history of a branch outside "
6578 "the \"refs/heads/\" reference namespace");
6579 goto done;
6582 error = got_ref_resolve(&head_commit_id, repo, branch);
6583 got_ref_close(branch);
6584 branch = NULL;
6585 if (error)
6586 goto done;
6588 error = got_object_open_as_commit(&commit, repo,
6589 head_commit_id);
6590 if (error)
6591 goto done;
6592 parent_ids = got_object_commit_get_parent_ids(commit);
6593 pid = SIMPLEQ_FIRST(parent_ids);
6594 if (pid == NULL) {
6595 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6596 goto done;
6598 error = collect_commits(&commits, head_commit_id, pid->id,
6599 got_worktree_get_base_commit_id(worktree),
6600 got_worktree_get_path_prefix(worktree),
6601 GOT_ERR_HISTEDIT_PATH, repo);
6602 got_object_commit_close(commit);
6603 commit = NULL;
6604 if (error)
6605 goto done;
6607 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6608 &base_commit_id, &fileindex, worktree, repo);
6609 if (error)
6610 goto done;
6612 if (edit_script_path) {
6613 error = histedit_load_list(&histedit_cmds,
6614 edit_script_path, repo);
6615 if (error) {
6616 got_worktree_histedit_abort(worktree, fileindex,
6617 repo, branch, base_commit_id,
6618 update_progress, &did_something);
6619 goto done;
6621 } else {
6622 error = histedit_edit_script(&histedit_cmds, &commits,
6623 repo);
6624 if (error) {
6625 got_worktree_histedit_abort(worktree, fileindex,
6626 repo, branch, base_commit_id,
6627 update_progress, &did_something);
6628 goto done;
6633 error = histedit_save_list(&histedit_cmds, worktree,
6634 repo);
6635 if (error) {
6636 got_worktree_histedit_abort(worktree, fileindex,
6637 repo, branch, base_commit_id,
6638 update_progress, &did_something);
6639 goto done;
6644 error = histedit_check_script(&histedit_cmds, &commits, repo);
6645 if (error)
6646 goto done;
6648 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6649 if (resume_commit_id) {
6650 if (got_object_id_cmp(hle->commit_id,
6651 resume_commit_id) != 0)
6652 continue;
6654 resume_commit_id = NULL;
6655 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6656 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6657 error = histedit_skip_commit(hle, worktree,
6658 repo);
6659 } else {
6660 error = histedit_commit(NULL, worktree,
6661 fileindex, tmp_branch, hle, repo);
6663 if (error)
6664 goto done;
6665 continue;
6668 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6669 error = histedit_skip_commit(hle, worktree, repo);
6670 if (error)
6671 goto done;
6672 continue;
6675 error = got_object_open_as_commit(&commit, repo,
6676 hle->commit_id);
6677 if (error)
6678 goto done;
6679 parent_ids = got_object_commit_get_parent_ids(commit);
6680 pid = SIMPLEQ_FIRST(parent_ids);
6682 error = got_worktree_histedit_merge_files(&merged_paths,
6683 worktree, fileindex, pid->id, hle->commit_id, repo,
6684 rebase_progress, &rebase_status, check_cancelled, NULL);
6685 if (error)
6686 goto done;
6687 got_object_commit_close(commit);
6688 commit = NULL;
6690 if (rebase_status == GOT_STATUS_CONFLICT) {
6691 got_worktree_rebase_pathlist_free(&merged_paths);
6692 break;
6695 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6696 char *id_str;
6697 error = got_object_id_str(&id_str, hle->commit_id);
6698 if (error)
6699 goto done;
6700 printf("Stopping histedit for amending commit %s\n",
6701 id_str);
6702 free(id_str);
6703 got_worktree_rebase_pathlist_free(&merged_paths);
6704 error = got_worktree_histedit_postpone(worktree,
6705 fileindex);
6706 goto done;
6709 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6710 error = histedit_skip_commit(hle, worktree, repo);
6711 if (error)
6712 goto done;
6713 continue;
6716 error = histedit_commit(&merged_paths, worktree, fileindex,
6717 tmp_branch, hle, repo);
6718 got_worktree_rebase_pathlist_free(&merged_paths);
6719 if (error)
6720 goto done;
6723 if (rebase_status == GOT_STATUS_CONFLICT) {
6724 error = got_worktree_histedit_postpone(worktree, fileindex);
6725 if (error)
6726 goto done;
6727 error = got_error_msg(GOT_ERR_CONFLICTS,
6728 "conflicts must be resolved before rebasing can continue");
6729 } else
6730 error = histedit_complete(worktree, fileindex, tmp_branch,
6731 branch, repo);
6732 done:
6733 got_object_id_queue_free(&commits);
6734 histedit_free_list(&histedit_cmds);
6735 free(head_commit_id);
6736 free(base_commit_id);
6737 free(resume_commit_id);
6738 if (commit)
6739 got_object_commit_close(commit);
6740 if (branch)
6741 got_ref_close(branch);
6742 if (tmp_branch)
6743 got_ref_close(tmp_branch);
6744 if (worktree)
6745 got_worktree_close(worktree);
6746 if (repo)
6747 got_repo_close(repo);
6748 return error;
6751 __dead static void
6752 usage_integrate(void)
6754 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6755 exit(1);
6758 static const struct got_error *
6759 cmd_integrate(int argc, char *argv[])
6761 const struct got_error *error = NULL;
6762 struct got_repository *repo = NULL;
6763 struct got_worktree *worktree = NULL;
6764 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6765 const char *branch_arg = NULL;
6766 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6767 struct got_fileindex *fileindex = NULL;
6768 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6769 int ch, did_something = 0;
6771 while ((ch = getopt(argc, argv, "")) != -1) {
6772 switch (ch) {
6773 default:
6774 usage_integrate();
6775 /* NOTREACHED */
6779 argc -= optind;
6780 argv += optind;
6782 if (argc != 1)
6783 usage_integrate();
6784 branch_arg = argv[0];
6786 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6787 "unveil", NULL) == -1)
6788 err(1, "pledge");
6790 cwd = getcwd(NULL, 0);
6791 if (cwd == NULL) {
6792 error = got_error_from_errno("getcwd");
6793 goto done;
6796 error = got_worktree_open(&worktree, cwd);
6797 if (error)
6798 goto done;
6800 error = check_rebase_or_histedit_in_progress(worktree);
6801 if (error)
6802 goto done;
6804 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6805 NULL);
6806 if (error != NULL)
6807 goto done;
6809 error = apply_unveil(got_repo_get_path(repo), 0,
6810 got_worktree_get_root_path(worktree));
6811 if (error)
6812 goto done;
6814 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6815 error = got_error_from_errno("asprintf");
6816 goto done;
6819 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6820 &base_branch_ref, worktree, refname, repo);
6821 if (error)
6822 goto done;
6824 refname = strdup(got_ref_get_name(branch_ref));
6825 if (refname == NULL) {
6826 error = got_error_from_errno("strdup");
6827 got_worktree_integrate_abort(worktree, fileindex, repo,
6828 branch_ref, base_branch_ref);
6829 goto done;
6831 base_refname = strdup(got_ref_get_name(base_branch_ref));
6832 if (base_refname == NULL) {
6833 error = got_error_from_errno("strdup");
6834 got_worktree_integrate_abort(worktree, fileindex, repo,
6835 branch_ref, base_branch_ref);
6836 goto done;
6839 error = got_ref_resolve(&commit_id, repo, branch_ref);
6840 if (error)
6841 goto done;
6843 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6844 if (error)
6845 goto done;
6847 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6848 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6849 "specified branch has already been integrated");
6850 got_worktree_integrate_abort(worktree, fileindex, repo,
6851 branch_ref, base_branch_ref);
6852 goto done;
6855 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6856 if (error) {
6857 if (error->code == GOT_ERR_ANCESTRY)
6858 error = got_error(GOT_ERR_REBASE_REQUIRED);
6859 got_worktree_integrate_abort(worktree, fileindex, repo,
6860 branch_ref, base_branch_ref);
6861 goto done;
6864 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6865 branch_ref, base_branch_ref, update_progress, &did_something,
6866 check_cancelled, NULL);
6867 if (error)
6868 goto done;
6870 printf("Integrated %s into %s\n", refname, base_refname);
6871 done:
6872 if (repo)
6873 got_repo_close(repo);
6874 if (worktree)
6875 got_worktree_close(worktree);
6876 free(cwd);
6877 free(base_commit_id);
6878 free(commit_id);
6879 free(refname);
6880 free(base_refname);
6881 return error;
6884 __dead static void
6885 usage_stage(void)
6887 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6888 "[file-path ...]\n",
6889 getprogname());
6890 exit(1);
6893 static const struct got_error *
6894 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6895 const char *path, struct got_object_id *blob_id,
6896 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6897 int dirfd, const char *de_name)
6899 const struct got_error *err = NULL;
6900 char *id_str = NULL;
6902 if (staged_status != GOT_STATUS_ADD &&
6903 staged_status != GOT_STATUS_MODIFY &&
6904 staged_status != GOT_STATUS_DELETE)
6905 return NULL;
6907 if (staged_status == GOT_STATUS_ADD ||
6908 staged_status == GOT_STATUS_MODIFY)
6909 err = got_object_id_str(&id_str, staged_blob_id);
6910 else
6911 err = got_object_id_str(&id_str, blob_id);
6912 if (err)
6913 return err;
6915 printf("%s %c %s\n", id_str, staged_status, path);
6916 free(id_str);
6917 return NULL;
6920 static const struct got_error *
6921 cmd_stage(int argc, char *argv[])
6923 const struct got_error *error = NULL;
6924 struct got_repository *repo = NULL;
6925 struct got_worktree *worktree = NULL;
6926 char *cwd = NULL;
6927 struct got_pathlist_head paths;
6928 struct got_pathlist_entry *pe;
6929 int ch, list_stage = 0, pflag = 0;
6930 FILE *patch_script_file = NULL;
6931 const char *patch_script_path = NULL;
6932 struct choose_patch_arg cpa;
6934 TAILQ_INIT(&paths);
6936 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6937 switch (ch) {
6938 case 'l':
6939 list_stage = 1;
6940 break;
6941 case 'p':
6942 pflag = 1;
6943 break;
6944 case 'F':
6945 patch_script_path = optarg;
6946 break;
6947 default:
6948 usage_stage();
6949 /* NOTREACHED */
6953 argc -= optind;
6954 argv += optind;
6956 #ifndef PROFILE
6957 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6958 "unveil", NULL) == -1)
6959 err(1, "pledge");
6960 #endif
6961 if (list_stage && (pflag || patch_script_path))
6962 errx(1, "-l option cannot be used with other options");
6963 if (patch_script_path && !pflag)
6964 errx(1, "-F option can only be used together with -p option");
6966 cwd = getcwd(NULL, 0);
6967 if (cwd == NULL) {
6968 error = got_error_from_errno("getcwd");
6969 goto done;
6972 error = got_worktree_open(&worktree, cwd);
6973 if (error)
6974 goto done;
6976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6977 NULL);
6978 if (error != NULL)
6979 goto done;
6981 if (patch_script_path) {
6982 patch_script_file = fopen(patch_script_path, "r");
6983 if (patch_script_file == NULL) {
6984 error = got_error_from_errno2("fopen",
6985 patch_script_path);
6986 goto done;
6989 error = apply_unveil(got_repo_get_path(repo), 0,
6990 got_worktree_get_root_path(worktree));
6991 if (error)
6992 goto done;
6994 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6995 if (error)
6996 goto done;
6998 if (list_stage)
6999 error = got_worktree_status(worktree, &paths, repo,
7000 print_stage, NULL, check_cancelled, NULL);
7001 else {
7002 cpa.patch_script_file = patch_script_file;
7003 cpa.action = "stage";
7004 error = got_worktree_stage(worktree, &paths,
7005 pflag ? NULL : print_status, NULL,
7006 pflag ? choose_patch : NULL, &cpa, repo);
7008 done:
7009 if (patch_script_file && fclose(patch_script_file) == EOF &&
7010 error == NULL)
7011 error = got_error_from_errno2("fclose", patch_script_path);
7012 if (repo)
7013 got_repo_close(repo);
7014 if (worktree)
7015 got_worktree_close(worktree);
7016 TAILQ_FOREACH(pe, &paths, entry)
7017 free((char *)pe->path);
7018 got_pathlist_free(&paths);
7019 free(cwd);
7020 return error;
7023 __dead static void
7024 usage_unstage(void)
7026 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7027 "[file-path ...]\n",
7028 getprogname());
7029 exit(1);
7033 static const struct got_error *
7034 cmd_unstage(int argc, char *argv[])
7036 const struct got_error *error = NULL;
7037 struct got_repository *repo = NULL;
7038 struct got_worktree *worktree = NULL;
7039 char *cwd = NULL;
7040 struct got_pathlist_head paths;
7041 struct got_pathlist_entry *pe;
7042 int ch, did_something = 0, pflag = 0;
7043 FILE *patch_script_file = NULL;
7044 const char *patch_script_path = NULL;
7045 struct choose_patch_arg cpa;
7047 TAILQ_INIT(&paths);
7049 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7050 switch (ch) {
7051 case 'p':
7052 pflag = 1;
7053 break;
7054 case 'F':
7055 patch_script_path = optarg;
7056 break;
7057 default:
7058 usage_unstage();
7059 /* NOTREACHED */
7063 argc -= optind;
7064 argv += optind;
7066 #ifndef PROFILE
7067 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7068 "unveil", NULL) == -1)
7069 err(1, "pledge");
7070 #endif
7071 if (patch_script_path && !pflag)
7072 errx(1, "-F option can only be used together with -p option");
7074 cwd = getcwd(NULL, 0);
7075 if (cwd == NULL) {
7076 error = got_error_from_errno("getcwd");
7077 goto done;
7080 error = got_worktree_open(&worktree, cwd);
7081 if (error)
7082 goto done;
7084 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7085 NULL);
7086 if (error != NULL)
7087 goto done;
7089 if (patch_script_path) {
7090 patch_script_file = fopen(patch_script_path, "r");
7091 if (patch_script_file == NULL) {
7092 error = got_error_from_errno2("fopen",
7093 patch_script_path);
7094 goto done;
7098 error = apply_unveil(got_repo_get_path(repo), 0,
7099 got_worktree_get_root_path(worktree));
7100 if (error)
7101 goto done;
7103 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7104 if (error)
7105 goto done;
7107 cpa.patch_script_file = patch_script_file;
7108 cpa.action = "unstage";
7109 error = got_worktree_unstage(worktree, &paths, update_progress,
7110 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7111 done:
7112 if (patch_script_file && fclose(patch_script_file) == EOF &&
7113 error == NULL)
7114 error = got_error_from_errno2("fclose", patch_script_path);
7115 if (repo)
7116 got_repo_close(repo);
7117 if (worktree)
7118 got_worktree_close(worktree);
7119 TAILQ_FOREACH(pe, &paths, entry)
7120 free((char *)pe->path);
7121 got_pathlist_free(&paths);
7122 free(cwd);
7123 return error;
7126 __dead static void
7127 usage_cat(void)
7129 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7130 "arg1 [arg2 ...]\n", getprogname());
7131 exit(1);
7134 static const struct got_error *
7135 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7137 const struct got_error *err;
7138 struct got_blob_object *blob;
7140 err = got_object_open_as_blob(&blob, repo, id, 8192);
7141 if (err)
7142 return err;
7144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7145 got_object_blob_close(blob);
7146 return err;
7149 static const struct got_error *
7150 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7152 const struct got_error *err;
7153 struct got_tree_object *tree;
7154 int nentries, i;
7156 err = got_object_open_as_tree(&tree, repo, id);
7157 if (err)
7158 return err;
7160 nentries = got_object_tree_get_nentries(tree);
7161 for (i = 0; i < nentries; i++) {
7162 struct got_tree_entry *te;
7163 char *id_str;
7164 if (sigint_received || sigpipe_received)
7165 break;
7166 te = got_object_tree_get_entry(tree, i);
7167 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7168 if (err)
7169 break;
7170 fprintf(outfile, "%s %.7o %s\n", id_str,
7171 got_tree_entry_get_mode(te),
7172 got_tree_entry_get_name(te));
7173 free(id_str);
7176 got_object_tree_close(tree);
7177 return err;
7180 static const struct got_error *
7181 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7183 const struct got_error *err;
7184 struct got_commit_object *commit;
7185 const struct got_object_id_queue *parent_ids;
7186 struct got_object_qid *pid;
7187 char *id_str = NULL;
7188 const char *logmsg = NULL;
7190 err = got_object_open_as_commit(&commit, repo, id);
7191 if (err)
7192 return err;
7194 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7195 if (err)
7196 goto done;
7198 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7199 parent_ids = got_object_commit_get_parent_ids(commit);
7200 fprintf(outfile, "numparents %d\n",
7201 got_object_commit_get_nparents(commit));
7202 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7203 char *pid_str;
7204 err = got_object_id_str(&pid_str, pid->id);
7205 if (err)
7206 goto done;
7207 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7208 free(pid_str);
7210 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7211 got_object_commit_get_author(commit),
7212 got_object_commit_get_author_time(commit));
7214 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7215 got_object_commit_get_author(commit),
7216 got_object_commit_get_committer_time(commit));
7218 logmsg = got_object_commit_get_logmsg_raw(commit);
7219 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7220 fprintf(outfile, "%s", logmsg);
7221 done:
7222 free(id_str);
7223 got_object_commit_close(commit);
7224 return err;
7227 static const struct got_error *
7228 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7230 const struct got_error *err;
7231 struct got_tag_object *tag;
7232 char *id_str = NULL;
7233 const char *tagmsg = NULL;
7235 err = got_object_open_as_tag(&tag, repo, id);
7236 if (err)
7237 return err;
7239 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7240 if (err)
7241 goto done;
7243 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7245 switch (got_object_tag_get_object_type(tag)) {
7246 case GOT_OBJ_TYPE_BLOB:
7247 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7248 GOT_OBJ_LABEL_BLOB);
7249 break;
7250 case GOT_OBJ_TYPE_TREE:
7251 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7252 GOT_OBJ_LABEL_TREE);
7253 break;
7254 case GOT_OBJ_TYPE_COMMIT:
7255 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7256 GOT_OBJ_LABEL_COMMIT);
7257 break;
7258 case GOT_OBJ_TYPE_TAG:
7259 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7260 GOT_OBJ_LABEL_TAG);
7261 break;
7262 default:
7263 break;
7266 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7267 got_object_tag_get_name(tag));
7269 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7270 got_object_tag_get_tagger(tag),
7271 got_object_tag_get_tagger_time(tag));
7273 tagmsg = got_object_tag_get_message(tag);
7274 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7275 fprintf(outfile, "%s", tagmsg);
7276 done:
7277 free(id_str);
7278 got_object_tag_close(tag);
7279 return err;
7282 static const struct got_error *
7283 cmd_cat(int argc, char *argv[])
7285 const struct got_error *error;
7286 struct got_repository *repo = NULL;
7287 struct got_worktree *worktree = NULL;
7288 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7289 const char *commit_id_str = NULL;
7290 struct got_object_id *id = NULL, *commit_id = NULL;
7291 int ch, obj_type, i, force_path = 0;
7293 #ifndef PROFILE
7294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7295 NULL) == -1)
7296 err(1, "pledge");
7297 #endif
7299 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7300 switch (ch) {
7301 case 'c':
7302 commit_id_str = optarg;
7303 break;
7304 case 'r':
7305 repo_path = realpath(optarg, NULL);
7306 if (repo_path == NULL)
7307 return got_error_from_errno2("realpath",
7308 optarg);
7309 got_path_strip_trailing_slashes(repo_path);
7310 break;
7311 case 'P':
7312 force_path = 1;
7313 break;
7314 default:
7315 usage_cat();
7316 /* NOTREACHED */
7320 argc -= optind;
7321 argv += optind;
7323 cwd = getcwd(NULL, 0);
7324 if (cwd == NULL) {
7325 error = got_error_from_errno("getcwd");
7326 goto done;
7328 error = got_worktree_open(&worktree, cwd);
7329 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7330 goto done;
7331 if (worktree) {
7332 if (repo_path == NULL) {
7333 repo_path = strdup(
7334 got_worktree_get_repo_path(worktree));
7335 if (repo_path == NULL) {
7336 error = got_error_from_errno("strdup");
7337 goto done;
7342 if (repo_path == NULL) {
7343 repo_path = getcwd(NULL, 0);
7344 if (repo_path == NULL)
7345 return got_error_from_errno("getcwd");
7348 error = got_repo_open(&repo, repo_path, NULL);
7349 free(repo_path);
7350 if (error != NULL)
7351 goto done;
7353 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7354 if (error)
7355 goto done;
7357 if (commit_id_str == NULL)
7358 commit_id_str = GOT_REF_HEAD;
7359 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7360 if (error)
7361 goto done;
7363 for (i = 0; i < argc; i++) {
7364 if (force_path) {
7365 error = got_object_id_by_path(&id, repo, commit_id,
7366 argv[i]);
7367 if (error)
7368 break;
7369 } else {
7370 error = match_object_id(&id, &label, argv[i],
7371 GOT_OBJ_TYPE_ANY, 0, repo);
7372 if (error) {
7373 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7374 error->code != GOT_ERR_NOT_REF)
7375 break;
7376 error = got_object_id_by_path(&id, repo,
7377 commit_id, argv[i]);
7378 if (error)
7379 break;
7383 error = got_object_get_type(&obj_type, repo, id);
7384 if (error)
7385 break;
7387 switch (obj_type) {
7388 case GOT_OBJ_TYPE_BLOB:
7389 error = cat_blob(id, repo, stdout);
7390 break;
7391 case GOT_OBJ_TYPE_TREE:
7392 error = cat_tree(id, repo, stdout);
7393 break;
7394 case GOT_OBJ_TYPE_COMMIT:
7395 error = cat_commit(id, repo, stdout);
7396 break;
7397 case GOT_OBJ_TYPE_TAG:
7398 error = cat_tag(id, repo, stdout);
7399 break;
7400 default:
7401 error = got_error(GOT_ERR_OBJ_TYPE);
7402 break;
7404 if (error)
7405 break;
7406 free(label);
7407 label = NULL;
7408 free(id);
7409 id = NULL;
7412 done:
7413 free(label);
7414 free(id);
7415 free(commit_id);
7416 if (worktree)
7417 got_worktree_close(worktree);
7418 if (repo) {
7419 const struct got_error *repo_error;
7420 repo_error = got_repo_close(repo);
7421 if (error == NULL)
7422 error = repo_error;
7424 return error;