Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_diff.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_opentemp.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static volatile sig_atomic_t sigint_received;
62 static volatile sig_atomic_t sigpipe_received;
64 static void
65 catch_sigint(int signo)
66 {
67 sigint_received = 1;
68 }
70 static void
71 catch_sigpipe(int signo)
72 {
73 sigpipe_received = 1;
74 }
77 struct got_cmd {
78 const char *cmd_name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
82 };
84 __dead static void usage(int);
85 __dead static void usage_init(void);
86 __dead static void usage_import(void);
87 __dead static void usage_checkout(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_update(void);
90 __dead static void usage_log(void);
91 __dead static void usage_diff(void);
92 __dead static void usage_blame(void);
93 __dead static void usage_tree(void);
94 __dead static void usage_status(void);
95 __dead static void usage_ref(void);
96 __dead static void usage_branch(void);
97 __dead static void usage_tag(void);
98 __dead static void usage_add(void);
99 __dead static void usage_remove(void);
100 __dead static void usage_revert(void);
101 __dead static void usage_commit(void);
102 __dead static void usage_cherrypick(void);
103 __dead static void usage_backout(void);
104 __dead static void usage_rebase(void);
105 __dead static void usage_histedit(void);
106 __dead static void usage_integrate(void);
107 __dead static void usage_stage(void);
108 __dead static void usage_unstage(void);
109 __dead static void usage_cat(void);
111 static const struct got_error* cmd_init(int, char *[]);
112 static const struct got_error* cmd_import(int, char *[]);
113 static const struct got_error* cmd_clone(int, char *[]);
114 static const struct got_error* cmd_checkout(int, char *[]);
115 static const struct got_error* cmd_update(int, char *[]);
116 static const struct got_error* cmd_log(int, char *[]);
117 static const struct got_error* cmd_diff(int, char *[]);
118 static const struct got_error* cmd_blame(int, char *[]);
119 static const struct got_error* cmd_tree(int, char *[]);
120 static const struct got_error* cmd_status(int, char *[]);
121 static const struct got_error* cmd_ref(int, char *[]);
122 static const struct got_error* cmd_branch(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_revert(int, char *[]);
127 static const struct got_error* cmd_commit(int, char *[]);
128 static const struct got_error* cmd_cherrypick(int, char *[]);
129 static const struct got_error* cmd_backout(int, char *[]);
130 static const struct got_error* cmd_rebase(int, char *[]);
131 static const struct got_error* cmd_histedit(int, char *[]);
132 static const struct got_error* cmd_integrate(int, char *[]);
133 static const struct got_error* cmd_stage(int, char *[]);
134 static const struct got_error* cmd_unstage(int, char *[]);
135 static const struct got_error* cmd_cat(int, char *[]);
137 static struct got_cmd got_commands[] = {
138 { "init", cmd_init, usage_init, "in" },
139 { "import", cmd_import, usage_import, "im" },
140 { "checkout", cmd_checkout, usage_checkout, "co" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "update", cmd_update, usage_update, "up" },
143 { "log", cmd_log, usage_log, "" },
144 { "diff", cmd_diff, usage_diff, "di" },
145 { "blame", cmd_blame, usage_blame, "bl" },
146 { "tree", cmd_tree, usage_tree, "tr" },
147 { "status", cmd_status, usage_status, "st" },
148 { "ref", cmd_ref, usage_ref, "" },
149 { "branch", cmd_branch, usage_branch, "br" },
150 { "tag", cmd_tag, usage_tag, "" },
151 { "add", cmd_add, usage_add, "" },
152 { "remove", cmd_remove, usage_remove, "rm" },
153 { "revert", cmd_revert, usage_revert, "rv" },
154 { "commit", cmd_commit, usage_commit, "ci" },
155 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 { "backout", cmd_backout, usage_backout, "bo" },
157 { "rebase", cmd_rebase, usage_rebase, "rb" },
158 { "histedit", cmd_histedit, usage_histedit, "he" },
159 { "integrate", cmd_integrate, usage_integrate,"ig" },
160 { "stage", cmd_stage, usage_stage, "sg" },
161 { "unstage", cmd_unstage, usage_unstage, "ug" },
162 { "cat", cmd_cat, usage_cat, "" },
163 };
165 static void
166 list_commands(void)
168 int i;
170 fprintf(stderr, "commands:");
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct got_cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s", cmd->cmd_name);
175 fputc('\n', stderr);
178 int
179 main(int argc, char *argv[])
181 struct got_cmd *cmd;
182 unsigned int i;
183 int ch;
184 int hflag = 0, Vflag = 0;
185 static struct option longopts[] = {
186 { "version", no_argument, NULL, 'V' },
187 { NULL, 0, NULL, 0}
188 };
190 setlocale(LC_CTYPE, "");
192 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 switch (ch) {
194 case 'h':
195 hflag = 1;
196 break;
197 case 'V':
198 Vflag = 1;
199 break;
200 default:
201 usage(hflag);
202 /* NOTREACHED */
206 argc -= optind;
207 argv += optind;
208 optind = 0;
210 if (Vflag) {
211 got_version_print_str();
212 return 1;
215 if (argc <= 0)
216 usage(hflag);
218 signal(SIGINT, catch_sigint);
219 signal(SIGPIPE, catch_sigpipe);
221 for (i = 0; i < nitems(got_commands); i++) {
222 const struct got_error *error;
224 cmd = &got_commands[i];
226 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 strcmp(cmd->cmd_alias, argv[0]) != 0)
228 continue;
230 if (hflag)
231 got_commands[i].cmd_usage();
233 error = got_commands[i].cmd_main(argc, argv);
234 if (error && error->code != GOT_ERR_CANCELLED &&
235 error->code != GOT_ERR_PRIVSEP_EXIT &&
236 !(sigpipe_received &&
237 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 !(sigint_received &&
239 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 return 1;
244 return 0;
247 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 list_commands();
249 return 1;
252 __dead static void
253 usage(int hflag)
255 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 getprogname());
257 if (hflag)
258 list_commands();
259 exit(1);
262 static const struct got_error *
263 get_editor(char **abspath)
265 const struct got_error *err = NULL;
266 const char *editor;
268 *abspath = NULL;
270 editor = getenv("VISUAL");
271 if (editor == NULL)
272 editor = getenv("EDITOR");
274 if (editor) {
275 err = got_path_find_prog(abspath, editor);
276 if (err)
277 return err;
280 if (*abspath == NULL) {
281 *abspath = strdup("/bin/ed");
282 if (*abspath == NULL)
283 return got_error_from_errno("strdup");
286 return NULL;
289 static const struct got_error *
290 apply_unveil(const char *repo_path, int repo_read_only,
291 const char *worktree_path)
293 const struct got_error *err;
295 #ifdef PROFILE
296 if (unveil("gmon.out", "rwc") != 0)
297 return got_error_from_errno2("unveil", "gmon.out");
298 #endif
299 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 return got_error_from_errno2("unveil", repo_path);
302 if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 return got_error_from_errno2("unveil", worktree_path);
305 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 err = got_privsep_unveil_exec_helpers();
309 if (err != NULL)
310 return err;
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
315 return NULL;
318 __dead static void
319 usage_init(void)
321 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 exit(1);
325 static const struct got_error *
326 cmd_init(int argc, char *argv[])
328 const struct got_error *error = NULL;
329 char *repo_path = NULL;
330 int ch;
332 while ((ch = getopt(argc, argv, "")) != -1) {
333 switch (ch) {
334 default:
335 usage_init();
336 /* NOTREACHED */
340 argc -= optind;
341 argv += optind;
343 #ifndef PROFILE
344 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 err(1, "pledge");
346 #endif
347 if (argc != 1)
348 usage_init();
350 repo_path = strdup(argv[0]);
351 if (repo_path == NULL)
352 return got_error_from_errno("strdup");
354 got_path_strip_trailing_slashes(repo_path);
356 error = got_path_mkdir(repo_path);
357 if (error &&
358 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 goto done;
361 error = apply_unveil(repo_path, 0, NULL);
362 if (error)
363 goto done;
365 error = got_repo_init(repo_path);
366 done:
367 free(repo_path);
368 return error;
371 __dead static void
372 usage_import(void)
374 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 "[-r repository-path] [-I pattern] path\n", getprogname());
376 exit(1);
379 int
380 spawn_editor(const char *editor, const char *file)
382 pid_t pid;
383 sig_t sighup, sigint, sigquit;
384 int st = -1;
386 sighup = signal(SIGHUP, SIG_IGN);
387 sigint = signal(SIGINT, SIG_IGN);
388 sigquit = signal(SIGQUIT, SIG_IGN);
390 switch (pid = fork()) {
391 case -1:
392 goto doneediting;
393 case 0:
394 execl(editor, editor, file, (char *)NULL);
395 _exit(127);
398 while (waitpid(pid, &st, 0) == -1)
399 if (errno != EINTR)
400 break;
402 doneediting:
403 (void)signal(SIGHUP, sighup);
404 (void)signal(SIGINT, sigint);
405 (void)signal(SIGQUIT, sigquit);
407 if (!WIFEXITED(st)) {
408 errno = EINTR;
409 return -1;
412 return WEXITSTATUS(st);
415 static const struct got_error *
416 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 const char *initial_content)
419 const struct got_error *err = NULL;
420 char buf[1024];
421 struct stat st, st2;
422 FILE *fp;
423 int content_changed = 0;
424 size_t len;
426 *logmsg = NULL;
428 if (stat(logmsg_path, &st) == -1)
429 return got_error_from_errno2("stat", logmsg_path);
431 if (spawn_editor(editor, logmsg_path) == -1)
432 return got_error_from_errno("failed spawning editor");
434 if (stat(logmsg_path, &st2) == -1)
435 return got_error_from_errno("stat");
437 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "no changes made to commit message, aborting");
441 *logmsg = malloc(st2.st_size + 1);
442 if (*logmsg == NULL)
443 return got_error_from_errno("malloc");
444 (*logmsg)[0] = '\0';
445 len = 0;
447 fp = fopen(logmsg_path, "r");
448 if (fp == NULL) {
449 err = got_error_from_errno("fopen");
450 goto done;
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (!content_changed && strcmp(buf, initial_content) != 0)
454 content_changed = 1;
455 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 continue; /* remove comments and leading empty lines */
457 len = strlcat(*logmsg, buf, st2.st_size);
459 fclose(fp);
461 while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 (*logmsg)[len - 1] = '\0';
463 len--;
466 if (len == 0 || !content_changed)
467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 "commit message cannot be empty, aborting");
469 done:
470 if (err) {
471 free(*logmsg);
472 *logmsg = NULL;
474 return err;
477 static const struct got_error *
478 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 const char *path_dir, const char *branch_name)
481 char *initial_content = NULL;
482 const struct got_error *err = NULL;
483 int fd;
485 if (asprintf(&initial_content,
486 "\n# %s to be imported to branch %s\n", path_dir,
487 branch_name) == -1)
488 return got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(logmsg_path, &fd,
491 GOT_TMPDIR_STR "/got-importmsg");
492 if (err)
493 goto done;
495 dprintf(fd, initial_content);
496 close(fd);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 done:
500 free(initial_content);
501 return err;
504 static const struct got_error *
505 import_progress(void *arg, const char *path)
507 printf("A %s\n", path);
508 return NULL;
511 static const struct got_error *
512 get_author(char **author, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 const char *got_author, *name, *email;
517 *author = NULL;
519 name = got_repo_get_gitconfig_author_name(repo);
520 email = got_repo_get_gitconfig_author_email(repo);
521 if (name && email) {
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
524 return NULL;
527 got_author = getenv("GOT_AUTHOR");
528 if (got_author == NULL) {
529 name = got_repo_get_global_gitconfig_author_name(repo);
530 email = got_repo_get_global_gitconfig_author_email(repo);
531 if (name && email) {
532 if (asprintf(author, "%s <%s>", name, email) == -1)
533 return got_error_from_errno("asprintf");
534 return NULL;
536 /* TODO: Look up user in password database? */
537 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 *author = strdup(got_author);
541 if (*author == NULL)
542 return got_error_from_errno("strdup");
544 /*
545 * Really dumb email address check; we're only doing this to
546 * avoid git's object parser breaking on commits we create.
547 */
548 while (*got_author && *got_author != '<')
549 got_author++;
550 if (*got_author != '<') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 goto done;
554 while (*got_author && *got_author != '@')
555 got_author++;
556 if (*got_author != '@') {
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 goto done;
560 while (*got_author && *got_author != '>')
561 got_author++;
562 if (*got_author != '>')
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 done:
565 if (err) {
566 free(*author);
567 *author = NULL;
569 return err;
572 static const struct got_error *
573 get_gitconfig_path(char **gitconfig_path)
575 const char *homedir = getenv("HOME");
577 *gitconfig_path = NULL;
578 if (homedir) {
579 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 return got_error_from_errno("asprintf");
583 return NULL;
586 static const struct got_error *
587 cmd_import(int argc, char *argv[])
589 const struct got_error *error = NULL;
590 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 const char *branch_name = "main";
593 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 struct got_repository *repo = NULL;
595 struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 struct got_object_id *new_commit_id = NULL;
597 int ch;
598 struct got_pathlist_head ignores;
599 struct got_pathlist_entry *pe;
600 int preserve_logmsg = 0;
602 TAILQ_INIT(&ignores);
604 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 switch (ch) {
606 case 'b':
607 branch_name = optarg;
608 break;
609 case 'm':
610 logmsg = strdup(optarg);
611 if (logmsg == NULL) {
612 error = got_error_from_errno("strdup");
613 goto done;
615 break;
616 case 'r':
617 repo_path = realpath(optarg, NULL);
618 if (repo_path == NULL) {
619 error = got_error_from_errno2("realpath",
620 optarg);
621 goto done;
623 break;
624 case 'I':
625 if (optarg[0] == '\0')
626 break;
627 error = got_pathlist_insert(&pe, &ignores, optarg,
628 NULL);
629 if (error)
630 goto done;
631 break;
632 default:
633 usage_import();
634 /* NOTREACHED */
638 argc -= optind;
639 argv += optind;
641 #ifndef PROFILE
642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 "unveil",
644 NULL) == -1)
645 err(1, "pledge");
646 #endif
647 if (argc != 1)
648 usage_import();
650 if (repo_path == NULL) {
651 repo_path = getcwd(NULL, 0);
652 if (repo_path == NULL)
653 return got_error_from_errno("getcwd");
655 got_path_strip_trailing_slashes(repo_path);
656 error = get_gitconfig_path(&gitconfig_path);
657 if (error)
658 goto done;
659 error = got_repo_open(&repo, repo_path, gitconfig_path);
660 if (error)
661 goto done;
663 error = get_author(&author, repo);
664 if (error)
665 return error;
667 /*
668 * Don't let the user create a branch name with a leading '-'.
669 * While technically a valid reference name, this case is usually
670 * an unintended typo.
671 */
672 if (branch_name[0] == '-')
673 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 error = got_error_from_errno("asprintf");
677 goto done;
680 error = got_ref_open(&branch_ref, repo, refname, 0);
681 if (error) {
682 if (error->code != GOT_ERR_NOT_REF)
683 goto done;
684 } else {
685 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 "import target branch already exists");
687 goto done;
690 path_dir = realpath(argv[0], NULL);
691 if (path_dir == NULL) {
692 error = got_error_from_errno2("realpath", argv[0]);
693 goto done;
695 got_path_strip_trailing_slashes(path_dir);
697 /*
698 * unveil(2) traverses exec(2); if an editor is used we have
699 * to apply unveil after the log message has been written.
700 */
701 if (logmsg == NULL || strlen(logmsg) == 0) {
702 error = get_editor(&editor);
703 if (error)
704 goto done;
705 free(logmsg);
706 error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 path_dir, refname);
708 if (error) {
709 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 logmsg_path != NULL)
711 preserve_logmsg = 1;
712 goto done;
716 if (unveil(path_dir, "r") != 0) {
717 error = got_error_from_errno2("unveil", path_dir);
718 if (logmsg_path)
719 preserve_logmsg = 1;
720 goto done;
723 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 if (error) {
725 if (logmsg_path)
726 preserve_logmsg = 1;
727 goto done;
730 error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 author, &ignores, repo, import_progress, NULL);
732 if (error) {
733 if (logmsg_path)
734 preserve_logmsg = 1;
735 goto done;
738 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 if (error) {
740 if (logmsg_path)
741 preserve_logmsg = 1;
742 goto done;
745 error = got_ref_write(branch_ref, repo);
746 if (error) {
747 if (logmsg_path)
748 preserve_logmsg = 1;
749 goto done;
752 error = got_object_id_str(&id_str, new_commit_id);
753 if (error) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 if (error) {
761 if (error->code != GOT_ERR_NOT_REF) {
762 if (logmsg_path)
763 preserve_logmsg = 1;
764 goto done;
767 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 branch_ref);
769 if (error) {
770 if (logmsg_path)
771 preserve_logmsg = 1;
772 goto done;
775 error = got_ref_write(head_ref, repo);
776 if (error) {
777 if (logmsg_path)
778 preserve_logmsg = 1;
779 goto done;
783 printf("Created branch %s with commit %s\n",
784 got_ref_get_name(branch_ref), id_str);
785 done:
786 if (preserve_logmsg) {
787 fprintf(stderr, "%s: log message preserved in %s\n",
788 getprogname(), logmsg_path);
789 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 error = got_error_from_errno2("unlink", logmsg_path);
791 free(logmsg);
792 free(logmsg_path);
793 free(repo_path);
794 free(editor);
795 free(refname);
796 free(new_commit_id);
797 free(id_str);
798 free(author);
799 free(gitconfig_path);
800 if (branch_ref)
801 got_ref_close(branch_ref);
802 if (head_ref)
803 got_ref_close(head_ref);
804 return error;
807 __dead static void
808 usage_clone(void)
810 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 exit(1);
814 __dead static void
815 usage_checkout(void)
817 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 exit(1);
822 static void
823 show_worktree_base_ref_warning(void)
825 fprintf(stderr, "%s: warning: could not create a reference "
826 "to the work tree's base commit; the commit could be "
827 "garbage-collected by Git; making the repository "
828 "writable and running 'got update' will prevent this\n",
829 getprogname());
832 struct got_checkout_progress_arg {
833 const char *worktree_path;
834 int had_base_commit_ref_error;
835 };
837 static const struct got_error *
838 checkout_progress(void *arg, unsigned char status, const char *path)
840 struct got_checkout_progress_arg *a = arg;
842 /* Base commit bump happens silently. */
843 if (status == GOT_STATUS_BUMP_BASE)
844 return NULL;
846 if (status == GOT_STATUS_BASE_REF_ERR) {
847 a->had_base_commit_ref_error = 1;
848 return NULL;
851 while (path[0] == '/')
852 path++;
854 printf("%c %s/%s\n", status, a->worktree_path, path);
855 return NULL;
858 static const struct got_error *
859 check_cancelled(void *arg)
861 if (sigint_received || sigpipe_received)
862 return got_error(GOT_ERR_CANCELLED);
863 return NULL;
866 static const struct got_error *
867 check_linear_ancestry(struct got_object_id *commit_id,
868 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_object_id *yca_id;
874 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 commit_id, base_commit_id, repo, check_cancelled, NULL);
876 if (err)
877 return err;
879 if (yca_id == NULL)
880 return got_error(GOT_ERR_ANCESTRY);
882 /*
883 * Require a straight line of history between the target commit
884 * and the work tree's base commit.
886 * Non-linear situations such as this require a rebase:
888 * (commit) D F (base_commit)
889 * \ /
890 * C E
891 * \ /
892 * B (yca)
893 * |
894 * A
896 * 'got update' only handles linear cases:
897 * Update forwards in time: A (base/yca) - B - C - D (commit)
898 * Update backwards in time: D (base) - C - B - A (commit/yca)
899 */
900 if (allow_forwards_in_time_only) {
901 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 return got_error(GOT_ERR_ANCESTRY);
903 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 got_object_id_cmp(base_commit_id, yca_id) != 0)
905 return got_error(GOT_ERR_ANCESTRY);
907 free(yca_id);
908 return NULL;
911 static const struct got_error *
912 check_same_branch(struct got_object_id *commit_id,
913 struct got_reference *head_ref, struct got_object_id *yca_id,
914 struct got_repository *repo)
916 const struct got_error *err = NULL;
917 struct got_commit_graph *graph = NULL;
918 struct got_object_id *head_commit_id = NULL;
919 int is_same_branch = 0;
921 err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 if (err)
923 goto done;
925 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 is_same_branch = 1;
927 goto done;
929 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 is_same_branch = 1;
931 goto done;
934 err = got_commit_graph_open(&graph, "/", 1);
935 if (err)
936 goto done;
938 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 check_cancelled, NULL);
940 if (err)
941 goto done;
943 for (;;) {
944 struct got_object_id *id;
945 err = got_commit_graph_iter_next(&id, graph, repo,
946 check_cancelled, NULL);
947 if (err) {
948 if (err->code == GOT_ERR_ITER_COMPLETED)
949 err = NULL;
950 break;
953 if (id) {
954 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 break;
956 if (got_object_id_cmp(id, commit_id) == 0) {
957 is_same_branch = 1;
958 break;
962 done:
963 if (graph)
964 got_commit_graph_close(graph);
965 free(head_commit_id);
966 if (!err && !is_same_branch)
967 err = got_error(GOT_ERR_ANCESTRY);
968 return err;
971 static const struct got_error *
972 cmd_clone(int argc, char *argv[])
974 const struct got_error *err = NULL;
975 const char *uri, *branch_filter, *dirname;
976 char *proto, *host, *port, *repo_name, *server_path;
977 char *default_destdir = NULL, *id_str = NULL;
978 const char *repo_path;
979 struct got_repository *repo = NULL;
980 struct got_pathlist_head refs, symrefs;
981 struct got_pathlist_entry *pe;
982 struct got_object_id *pack_hash = NULL;
983 int ch;
985 TAILQ_INIT(&refs);
986 TAILQ_INIT(&symrefs);
988 while ((ch = getopt(argc, argv, "b:")) != -1) {
989 switch (ch) {
990 case 'b':
991 branch_filter = optarg;
992 break;
993 default:
994 usage_clone();
995 break;
998 argc -= optind;
999 argv += optind;
1000 uri = argv[0];
1001 if(argc == 1)
1002 dirname = NULL;
1003 else if(argc == 2)
1004 dirname = argv[1];
1005 else
1006 usage_clone();
1008 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1009 &repo_name, argv[0]);
1010 if (err)
1011 goto done;
1013 if (dirname == NULL) {
1014 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 goto done;
1018 repo_path = default_destdir;
1019 } else
1020 repo_path = dirname;
1022 err = got_path_mkdir(repo_path);
1023 if (err)
1024 goto done;
1026 err = got_repo_init(repo_path);
1027 if (err != NULL)
1028 goto done;
1030 err = got_repo_open(&repo, repo_path, NULL);
1031 if (err)
1032 goto done;
1034 err = got_fetch(&pack_hash, &refs, &symrefs,
1035 proto, host, port, server_path, repo_name, branch_filter, repo);
1036 if (err)
1037 goto done;
1039 err = got_object_id_str(&id_str, pack_hash);
1040 if (err)
1041 goto done;
1042 printf("Fetched %s.pack\n", id_str);
1043 free(id_str);
1045 /* Set up references provided with the pack file. */
1046 TAILQ_FOREACH(pe, &refs, entry) {
1047 const char *refname = pe->path;
1048 struct got_object_id *id = pe->data;
1049 struct got_reference *ref;
1051 err = got_object_id_str(&id_str, id);
1052 if (err)
1053 goto done;
1055 err = got_ref_alloc(&ref, refname, id);
1056 if (err) {
1057 free(id_str);
1058 goto done;
1061 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1062 free(id_str);
1063 err = got_ref_write(ref, repo);
1064 got_ref_close(ref);
1065 if (err)
1066 goto done;
1069 /* Set the HEAD reference if the server provided one. */
1070 TAILQ_FOREACH(pe, &symrefs, entry) {
1071 struct got_reference *symref, *target_ref;
1072 const char *refname = pe->path;
1073 const char *target = pe->data;
1075 if (strcmp(refname, GOT_REF_HEAD) != 0)
1076 continue;
1078 err = got_ref_open(&target_ref, repo, target, 0);
1079 if (err) {
1080 if (err->code == GOT_ERR_NOT_REF)
1081 continue;
1082 goto done;
1085 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1086 got_ref_close(target_ref);
1087 if (err)
1088 goto done;
1090 printf("Setting %s to %s\n", GOT_REF_HEAD,
1091 got_ref_get_symref_target(symref));
1093 err = got_ref_write(symref, repo);
1094 got_ref_close(symref);
1095 break;
1098 done:
1099 if (repo)
1100 got_repo_close(repo);
1101 TAILQ_FOREACH(pe, &refs, entry) {
1102 free((void *)pe->path);
1103 free(pe->data);
1105 got_pathlist_free(&refs);
1106 TAILQ_FOREACH(pe, &symrefs, entry) {
1107 free((void *)pe->path);
1108 free(pe->data);
1110 got_pathlist_free(&symrefs);
1111 free(pack_hash);
1112 free(proto);
1113 free(host);
1114 free(port);
1115 free(server_path);
1116 free(repo_name);
1117 free(default_destdir);
1118 return err;
1121 static const struct got_error *
1122 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1124 static char msg[512];
1125 const char *branch_name;
1127 if (got_ref_is_symbolic(ref))
1128 branch_name = got_ref_get_symref_target(ref);
1129 else
1130 branch_name = got_ref_get_name(ref);
1132 if (strncmp("refs/heads/", branch_name, 11) == 0)
1133 branch_name += 11;
1135 snprintf(msg, sizeof(msg),
1136 "target commit is not contained in branch '%s'; "
1137 "the branch to use must be specified with -b; "
1138 "if necessary a new branch can be created for "
1139 "this commit with 'got branch -c %s BRANCH_NAME'",
1140 branch_name, commit_id_str);
1142 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1145 static const struct got_error *
1146 cmd_checkout(int argc, char *argv[])
1148 const struct got_error *error = NULL;
1149 struct got_repository *repo = NULL;
1150 struct got_reference *head_ref = NULL;
1151 struct got_worktree *worktree = NULL;
1152 char *repo_path = NULL;
1153 char *worktree_path = NULL;
1154 const char *path_prefix = "";
1155 const char *branch_name = GOT_REF_HEAD;
1156 char *commit_id_str = NULL;
1157 int ch, same_path_prefix, allow_nonempty = 0;
1158 struct got_pathlist_head paths;
1159 struct got_checkout_progress_arg cpa;
1161 TAILQ_INIT(&paths);
1163 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1164 switch (ch) {
1165 case 'b':
1166 branch_name = optarg;
1167 break;
1168 case 'c':
1169 commit_id_str = strdup(optarg);
1170 if (commit_id_str == NULL)
1171 return got_error_from_errno("strdup");
1172 break;
1173 case 'E':
1174 allow_nonempty = 1;
1175 break;
1176 case 'p':
1177 path_prefix = optarg;
1178 break;
1179 default:
1180 usage_checkout();
1181 /* NOTREACHED */
1185 argc -= optind;
1186 argv += optind;
1188 #ifndef PROFILE
1189 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1190 "unveil", NULL) == -1)
1191 err(1, "pledge");
1192 #endif
1193 if (argc == 1) {
1194 char *cwd, *base, *dotgit;
1195 repo_path = realpath(argv[0], NULL);
1196 if (repo_path == NULL)
1197 return got_error_from_errno2("realpath", argv[0]);
1198 cwd = getcwd(NULL, 0);
1199 if (cwd == NULL) {
1200 error = got_error_from_errno("getcwd");
1201 goto done;
1203 if (path_prefix[0]) {
1204 base = basename(path_prefix);
1205 if (base == NULL) {
1206 error = got_error_from_errno2("basename",
1207 path_prefix);
1208 goto done;
1210 } else {
1211 base = basename(repo_path);
1212 if (base == NULL) {
1213 error = got_error_from_errno2("basename",
1214 repo_path);
1215 goto done;
1218 dotgit = strstr(base, ".git");
1219 if (dotgit)
1220 *dotgit = '\0';
1221 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1222 error = got_error_from_errno("asprintf");
1223 free(cwd);
1224 goto done;
1226 free(cwd);
1227 } else if (argc == 2) {
1228 repo_path = realpath(argv[0], NULL);
1229 if (repo_path == NULL) {
1230 error = got_error_from_errno2("realpath", argv[0]);
1231 goto done;
1233 worktree_path = realpath(argv[1], NULL);
1234 if (worktree_path == NULL) {
1235 if (errno != ENOENT) {
1236 error = got_error_from_errno2("realpath",
1237 argv[1]);
1238 goto done;
1240 worktree_path = strdup(argv[1]);
1241 if (worktree_path == NULL) {
1242 error = got_error_from_errno("strdup");
1243 goto done;
1246 } else
1247 usage_checkout();
1249 got_path_strip_trailing_slashes(repo_path);
1250 got_path_strip_trailing_slashes(worktree_path);
1252 error = got_repo_open(&repo, repo_path, NULL);
1253 if (error != NULL)
1254 goto done;
1256 /* Pre-create work tree path for unveil(2) */
1257 error = got_path_mkdir(worktree_path);
1258 if (error) {
1259 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1260 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1261 goto done;
1262 if (!allow_nonempty &&
1263 !got_path_dir_is_empty(worktree_path)) {
1264 error = got_error_path(worktree_path,
1265 GOT_ERR_DIR_NOT_EMPTY);
1266 goto done;
1270 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1271 if (error)
1272 goto done;
1274 error = got_ref_open(&head_ref, repo, branch_name, 0);
1275 if (error != NULL)
1276 goto done;
1278 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1279 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1280 goto done;
1282 error = got_worktree_open(&worktree, worktree_path);
1283 if (error != NULL)
1284 goto done;
1286 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1287 path_prefix);
1288 if (error != NULL)
1289 goto done;
1290 if (!same_path_prefix) {
1291 error = got_error(GOT_ERR_PATH_PREFIX);
1292 goto done;
1295 if (commit_id_str) {
1296 struct got_object_id *commit_id;
1297 error = got_repo_match_object_id(&commit_id, NULL,
1298 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1299 if (error)
1300 goto done;
1301 error = check_linear_ancestry(commit_id,
1302 got_worktree_get_base_commit_id(worktree), 0, repo);
1303 if (error != NULL) {
1304 free(commit_id);
1305 if (error->code == GOT_ERR_ANCESTRY) {
1306 error = checkout_ancestry_error(
1307 head_ref, commit_id_str);
1309 goto done;
1311 error = check_same_branch(commit_id, head_ref, NULL, repo);
1312 if (error) {
1313 if (error->code == GOT_ERR_ANCESTRY) {
1314 error = checkout_ancestry_error(
1315 head_ref, commit_id_str);
1317 goto done;
1319 error = got_worktree_set_base_commit_id(worktree, repo,
1320 commit_id);
1321 free(commit_id);
1322 if (error)
1323 goto done;
1326 error = got_pathlist_append(&paths, "", NULL);
1327 if (error)
1328 goto done;
1329 cpa.worktree_path = worktree_path;
1330 cpa.had_base_commit_ref_error = 0;
1331 error = got_worktree_checkout_files(worktree, &paths, repo,
1332 checkout_progress, &cpa, check_cancelled, NULL);
1333 if (error != NULL)
1334 goto done;
1336 printf("Now shut up and hack\n");
1337 if (cpa.had_base_commit_ref_error)
1338 show_worktree_base_ref_warning();
1339 done:
1340 got_pathlist_free(&paths);
1341 free(commit_id_str);
1342 free(repo_path);
1343 free(worktree_path);
1344 return error;
1347 __dead static void
1348 usage_update(void)
1350 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1351 getprogname());
1352 exit(1);
1355 static const struct got_error *
1356 update_progress(void *arg, unsigned char status, const char *path)
1358 int *did_something = arg;
1360 if (status == GOT_STATUS_EXISTS ||
1361 status == GOT_STATUS_BASE_REF_ERR)
1362 return NULL;
1364 *did_something = 1;
1366 /* Base commit bump happens silently. */
1367 if (status == GOT_STATUS_BUMP_BASE)
1368 return NULL;
1370 while (path[0] == '/')
1371 path++;
1372 printf("%c %s\n", status, path);
1373 return NULL;
1376 static const struct got_error *
1377 switch_head_ref(struct got_reference *head_ref,
1378 struct got_object_id *commit_id, struct got_worktree *worktree,
1379 struct got_repository *repo)
1381 const struct got_error *err = NULL;
1382 char *base_id_str;
1383 int ref_has_moved = 0;
1385 /* Trivial case: switching between two different references. */
1386 if (strcmp(got_ref_get_name(head_ref),
1387 got_worktree_get_head_ref_name(worktree)) != 0) {
1388 printf("Switching work tree from %s to %s\n",
1389 got_worktree_get_head_ref_name(worktree),
1390 got_ref_get_name(head_ref));
1391 return got_worktree_set_head_ref(worktree, head_ref);
1394 err = check_linear_ancestry(commit_id,
1395 got_worktree_get_base_commit_id(worktree), 0, repo);
1396 if (err) {
1397 if (err->code != GOT_ERR_ANCESTRY)
1398 return err;
1399 ref_has_moved = 1;
1401 if (!ref_has_moved)
1402 return NULL;
1404 /* Switching to a rebased branch with the same reference name. */
1405 err = got_object_id_str(&base_id_str,
1406 got_worktree_get_base_commit_id(worktree));
1407 if (err)
1408 return err;
1409 printf("Reference %s now points at a different branch\n",
1410 got_worktree_get_head_ref_name(worktree));
1411 printf("Switching work tree from %s to %s\n", base_id_str,
1412 got_worktree_get_head_ref_name(worktree));
1413 return NULL;
1416 static const struct got_error *
1417 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1419 const struct got_error *err;
1420 int in_progress;
1422 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1423 if (err)
1424 return err;
1425 if (in_progress)
1426 return got_error(GOT_ERR_REBASING);
1428 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1429 if (err)
1430 return err;
1431 if (in_progress)
1432 return got_error(GOT_ERR_HISTEDIT_BUSY);
1434 return NULL;
1437 static const struct got_error *
1438 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1439 char *argv[], struct got_worktree *worktree)
1441 const struct got_error *err = NULL;
1442 char *path;
1443 int i;
1445 if (argc == 0) {
1446 path = strdup("");
1447 if (path == NULL)
1448 return got_error_from_errno("strdup");
1449 return got_pathlist_append(paths, path, NULL);
1452 for (i = 0; i < argc; i++) {
1453 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1454 if (err)
1455 break;
1456 err = got_pathlist_append(paths, path, NULL);
1457 if (err) {
1458 free(path);
1459 break;
1463 return err;
1466 static const struct got_error *
1467 cmd_update(int argc, char *argv[])
1469 const struct got_error *error = NULL;
1470 struct got_repository *repo = NULL;
1471 struct got_worktree *worktree = NULL;
1472 char *worktree_path = NULL;
1473 struct got_object_id *commit_id = NULL;
1474 char *commit_id_str = NULL;
1475 const char *branch_name = NULL;
1476 struct got_reference *head_ref = NULL;
1477 struct got_pathlist_head paths;
1478 struct got_pathlist_entry *pe;
1479 int ch, did_something = 0;
1481 TAILQ_INIT(&paths);
1483 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1484 switch (ch) {
1485 case 'b':
1486 branch_name = optarg;
1487 break;
1488 case 'c':
1489 commit_id_str = strdup(optarg);
1490 if (commit_id_str == NULL)
1491 return got_error_from_errno("strdup");
1492 break;
1493 default:
1494 usage_update();
1495 /* NOTREACHED */
1499 argc -= optind;
1500 argv += optind;
1502 #ifndef PROFILE
1503 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1504 "unveil", NULL) == -1)
1505 err(1, "pledge");
1506 #endif
1507 worktree_path = getcwd(NULL, 0);
1508 if (worktree_path == NULL) {
1509 error = got_error_from_errno("getcwd");
1510 goto done;
1512 error = got_worktree_open(&worktree, worktree_path);
1513 if (error)
1514 goto done;
1516 error = check_rebase_or_histedit_in_progress(worktree);
1517 if (error)
1518 goto done;
1520 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1521 NULL);
1522 if (error != NULL)
1523 goto done;
1525 error = apply_unveil(got_repo_get_path(repo), 0,
1526 got_worktree_get_root_path(worktree));
1527 if (error)
1528 goto done;
1530 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1531 if (error)
1532 goto done;
1534 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1535 got_worktree_get_head_ref_name(worktree), 0);
1536 if (error != NULL)
1537 goto done;
1538 if (commit_id_str == NULL) {
1539 error = got_ref_resolve(&commit_id, repo, head_ref);
1540 if (error != NULL)
1541 goto done;
1542 error = got_object_id_str(&commit_id_str, commit_id);
1543 if (error != NULL)
1544 goto done;
1545 } else {
1546 error = got_repo_match_object_id(&commit_id, NULL,
1547 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1548 free(commit_id_str);
1549 commit_id_str = NULL;
1550 if (error)
1551 goto done;
1552 error = got_object_id_str(&commit_id_str, commit_id);
1553 if (error)
1554 goto done;
1557 if (branch_name) {
1558 struct got_object_id *head_commit_id;
1559 TAILQ_FOREACH(pe, &paths, entry) {
1560 if (pe->path_len == 0)
1561 continue;
1562 error = got_error_msg(GOT_ERR_BAD_PATH,
1563 "switching between branches requires that "
1564 "the entire work tree gets updated");
1565 goto done;
1567 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1568 if (error)
1569 goto done;
1570 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1571 repo);
1572 free(head_commit_id);
1573 if (error != NULL)
1574 goto done;
1575 error = check_same_branch(commit_id, head_ref, NULL, repo);
1576 if (error)
1577 goto done;
1578 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1579 if (error)
1580 goto done;
1581 } else {
1582 error = check_linear_ancestry(commit_id,
1583 got_worktree_get_base_commit_id(worktree), 0, repo);
1584 if (error != NULL) {
1585 if (error->code == GOT_ERR_ANCESTRY)
1586 error = got_error(GOT_ERR_BRANCH_MOVED);
1587 goto done;
1589 error = check_same_branch(commit_id, head_ref, NULL, repo);
1590 if (error)
1591 goto done;
1594 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1595 commit_id) != 0) {
1596 error = got_worktree_set_base_commit_id(worktree, repo,
1597 commit_id);
1598 if (error)
1599 goto done;
1602 error = got_worktree_checkout_files(worktree, &paths, repo,
1603 update_progress, &did_something, check_cancelled, NULL);
1604 if (error != NULL)
1605 goto done;
1607 if (did_something)
1608 printf("Updated to commit %s\n", commit_id_str);
1609 else
1610 printf("Already up-to-date\n");
1611 done:
1612 free(worktree_path);
1613 TAILQ_FOREACH(pe, &paths, entry)
1614 free((char *)pe->path);
1615 got_pathlist_free(&paths);
1616 free(commit_id);
1617 free(commit_id_str);
1618 return error;
1621 static const struct got_error *
1622 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1623 const char *path, int diff_context, int ignore_whitespace,
1624 struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1629 if (blob_id1) {
1630 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1631 if (err)
1632 goto done;
1635 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1636 if (err)
1637 goto done;
1639 while (path[0] == '/')
1640 path++;
1641 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1642 ignore_whitespace, stdout);
1643 done:
1644 if (blob1)
1645 got_object_blob_close(blob1);
1646 got_object_blob_close(blob2);
1647 return err;
1650 static const struct got_error *
1651 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1652 const char *path, int diff_context, int ignore_whitespace,
1653 struct got_repository *repo)
1655 const struct got_error *err = NULL;
1656 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1657 struct got_diff_blob_output_unidiff_arg arg;
1659 if (tree_id1) {
1660 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1661 if (err)
1662 goto done;
1665 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1666 if (err)
1667 goto done;
1669 arg.diff_context = diff_context;
1670 arg.ignore_whitespace = ignore_whitespace;
1671 arg.outfile = stdout;
1672 while (path[0] == '/')
1673 path++;
1674 err = got_diff_tree(tree1, tree2, path, path, repo,
1675 got_diff_blob_output_unidiff, &arg, 1);
1676 done:
1677 if (tree1)
1678 got_object_tree_close(tree1);
1679 if (tree2)
1680 got_object_tree_close(tree2);
1681 return err;
1684 static const struct got_error *
1685 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1686 const char *path, int diff_context, struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_commit_object *pcommit = NULL;
1690 char *id_str1 = NULL, *id_str2 = NULL;
1691 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1692 struct got_object_qid *qid;
1694 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1695 if (qid != NULL) {
1696 err = got_object_open_as_commit(&pcommit, repo,
1697 qid->id);
1698 if (err)
1699 return err;
1702 if (path && path[0] != '\0') {
1703 int obj_type;
1704 err = got_object_id_by_path(&obj_id2, repo, id, path);
1705 if (err)
1706 goto done;
1707 err = got_object_id_str(&id_str2, obj_id2);
1708 if (err) {
1709 free(obj_id2);
1710 goto done;
1712 if (pcommit) {
1713 err = got_object_id_by_path(&obj_id1, repo,
1714 qid->id, path);
1715 if (err) {
1716 free(obj_id2);
1717 goto done;
1719 err = got_object_id_str(&id_str1, obj_id1);
1720 if (err) {
1721 free(obj_id2);
1722 goto done;
1725 err = got_object_get_type(&obj_type, repo, obj_id2);
1726 if (err) {
1727 free(obj_id2);
1728 goto done;
1730 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1731 switch (obj_type) {
1732 case GOT_OBJ_TYPE_BLOB:
1733 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1734 0, repo);
1735 break;
1736 case GOT_OBJ_TYPE_TREE:
1737 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1738 0, repo);
1739 break;
1740 default:
1741 err = got_error(GOT_ERR_OBJ_TYPE);
1742 break;
1744 free(obj_id1);
1745 free(obj_id2);
1746 } else {
1747 obj_id2 = got_object_commit_get_tree_id(commit);
1748 err = got_object_id_str(&id_str2, obj_id2);
1749 if (err)
1750 goto done;
1751 obj_id1 = got_object_commit_get_tree_id(pcommit);
1752 err = got_object_id_str(&id_str1, obj_id1);
1753 if (err)
1754 goto done;
1755 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1756 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1758 done:
1759 free(id_str1);
1760 free(id_str2);
1761 if (pcommit)
1762 got_object_commit_close(pcommit);
1763 return err;
1766 static char *
1767 get_datestr(time_t *time, char *datebuf)
1769 struct tm mytm, *tm;
1770 char *p, *s;
1772 tm = gmtime_r(time, &mytm);
1773 if (tm == NULL)
1774 return NULL;
1775 s = asctime_r(tm, datebuf);
1776 if (s == NULL)
1777 return NULL;
1778 p = strchr(s, '\n');
1779 if (p)
1780 *p = '\0';
1781 return s;
1784 static const struct got_error *
1785 match_logmsg(int *have_match, struct got_object_id *id,
1786 struct got_commit_object *commit, regex_t *regex)
1788 const struct got_error *err = NULL;
1789 regmatch_t regmatch;
1790 char *id_str = NULL, *logmsg = NULL;
1792 *have_match = 0;
1794 err = got_object_id_str(&id_str, id);
1795 if (err)
1796 return err;
1798 err = got_object_commit_get_logmsg(&logmsg, commit);
1799 if (err)
1800 goto done;
1802 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1803 *have_match = 1;
1804 done:
1805 free(id_str);
1806 free(logmsg);
1807 return err;
1810 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1812 static const struct got_error *
1813 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1814 struct got_repository *repo, const char *path, int show_patch,
1815 int diff_context, struct got_reflist_head *refs)
1817 const struct got_error *err = NULL;
1818 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1819 char datebuf[26];
1820 time_t committer_time;
1821 const char *author, *committer;
1822 char *refs_str = NULL;
1823 struct got_reflist_entry *re;
1825 SIMPLEQ_FOREACH(re, refs, entry) {
1826 char *s;
1827 const char *name;
1828 struct got_tag_object *tag = NULL;
1829 int cmp;
1831 name = got_ref_get_name(re->ref);
1832 if (strcmp(name, GOT_REF_HEAD) == 0)
1833 continue;
1834 if (strncmp(name, "refs/", 5) == 0)
1835 name += 5;
1836 if (strncmp(name, "got/", 4) == 0)
1837 continue;
1838 if (strncmp(name, "heads/", 6) == 0)
1839 name += 6;
1840 if (strncmp(name, "remotes/", 8) == 0)
1841 name += 8;
1842 if (strncmp(name, "tags/", 5) == 0) {
1843 err = got_object_open_as_tag(&tag, repo, re->id);
1844 if (err) {
1845 if (err->code != GOT_ERR_OBJ_TYPE)
1846 return err;
1847 /* Ref points at something other than a tag. */
1848 err = NULL;
1849 tag = NULL;
1852 cmp = got_object_id_cmp(tag ?
1853 got_object_tag_get_object_id(tag) : re->id, id);
1854 if (tag)
1855 got_object_tag_close(tag);
1856 if (cmp != 0)
1857 continue;
1858 s = refs_str;
1859 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1860 name) == -1) {
1861 err = got_error_from_errno("asprintf");
1862 free(s);
1863 return err;
1865 free(s);
1867 err = got_object_id_str(&id_str, id);
1868 if (err)
1869 return err;
1871 printf(GOT_COMMIT_SEP_STR);
1872 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1873 refs_str ? refs_str : "", refs_str ? ")" : "");
1874 free(id_str);
1875 id_str = NULL;
1876 free(refs_str);
1877 refs_str = NULL;
1878 printf("from: %s\n", got_object_commit_get_author(commit));
1879 committer_time = got_object_commit_get_committer_time(commit);
1880 datestr = get_datestr(&committer_time, datebuf);
1881 if (datestr)
1882 printf("date: %s UTC\n", datestr);
1883 author = got_object_commit_get_author(commit);
1884 committer = got_object_commit_get_committer(commit);
1885 if (strcmp(author, committer) != 0)
1886 printf("via: %s\n", committer);
1887 if (got_object_commit_get_nparents(commit) > 1) {
1888 const struct got_object_id_queue *parent_ids;
1889 struct got_object_qid *qid;
1890 int n = 1;
1891 parent_ids = got_object_commit_get_parent_ids(commit);
1892 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1893 err = got_object_id_str(&id_str, qid->id);
1894 if (err)
1895 return err;
1896 printf("parent %d: %s\n", n++, id_str);
1897 free(id_str);
1901 err = got_object_commit_get_logmsg(&logmsg0, commit);
1902 if (err)
1903 return err;
1905 logmsg = logmsg0;
1906 do {
1907 line = strsep(&logmsg, "\n");
1908 if (line)
1909 printf(" %s\n", line);
1910 } while (line);
1911 free(logmsg0);
1913 if (show_patch) {
1914 err = print_patch(commit, id, path, diff_context, repo);
1915 if (err == 0)
1916 printf("\n");
1919 if (fflush(stdout) != 0 && err == NULL)
1920 err = got_error_from_errno("fflush");
1921 return err;
1924 static const struct got_error *
1925 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1926 const char *path, int show_patch, const char *search_pattern,
1927 int diff_context, int limit, int log_branches,
1928 struct got_reflist_head *refs)
1930 const struct got_error *err;
1931 struct got_commit_graph *graph;
1932 regex_t regex;
1933 int have_match;
1935 if (search_pattern &&
1936 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1937 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1939 err = got_commit_graph_open(&graph, path, !log_branches);
1940 if (err)
1941 return err;
1942 err = got_commit_graph_iter_start(graph, root_id, repo,
1943 check_cancelled, NULL);
1944 if (err)
1945 goto done;
1946 for (;;) {
1947 struct got_commit_object *commit;
1948 struct got_object_id *id;
1950 if (sigint_received || sigpipe_received)
1951 break;
1953 err = got_commit_graph_iter_next(&id, graph, repo,
1954 check_cancelled, NULL);
1955 if (err) {
1956 if (err->code == GOT_ERR_ITER_COMPLETED)
1957 err = NULL;
1958 break;
1960 if (id == NULL)
1961 break;
1963 err = got_object_open_as_commit(&commit, repo, id);
1964 if (err)
1965 break;
1967 if (search_pattern) {
1968 err = match_logmsg(&have_match, id, commit, &regex);
1969 if (err) {
1970 got_object_commit_close(commit);
1971 break;
1973 if (have_match == 0) {
1974 got_object_commit_close(commit);
1975 continue;
1979 err = print_commit(commit, id, repo, path, show_patch,
1980 diff_context, refs);
1981 got_object_commit_close(commit);
1982 if (err || (limit && --limit == 0))
1983 break;
1985 done:
1986 if (search_pattern)
1987 regfree(&regex);
1988 got_commit_graph_close(graph);
1989 return err;
1992 __dead static void
1993 usage_log(void)
1995 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1996 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1997 exit(1);
2000 static int
2001 get_default_log_limit(void)
2003 const char *got_default_log_limit;
2004 long long n;
2005 const char *errstr;
2007 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2008 if (got_default_log_limit == NULL)
2009 return 0;
2010 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2011 if (errstr != NULL)
2012 return 0;
2013 return n;
2016 static const struct got_error *
2017 cmd_log(int argc, char *argv[])
2019 const struct got_error *error;
2020 struct got_repository *repo = NULL;
2021 struct got_worktree *worktree = NULL;
2022 struct got_commit_object *commit = NULL;
2023 struct got_object_id *id = NULL;
2024 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2025 const char *start_commit = NULL, *search_pattern = NULL;
2026 int diff_context = -1, ch;
2027 int show_patch = 0, limit = 0, log_branches = 0;
2028 const char *errstr;
2029 struct got_reflist_head refs;
2031 SIMPLEQ_INIT(&refs);
2033 #ifndef PROFILE
2034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2035 NULL)
2036 == -1)
2037 err(1, "pledge");
2038 #endif
2040 limit = get_default_log_limit();
2042 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2043 switch (ch) {
2044 case 'p':
2045 show_patch = 1;
2046 break;
2047 case 'c':
2048 start_commit = optarg;
2049 break;
2050 case 'C':
2051 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2052 &errstr);
2053 if (errstr != NULL)
2054 err(1, "-C option %s", errstr);
2055 break;
2056 case 'l':
2057 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2058 if (errstr != NULL)
2059 err(1, "-l option %s", errstr);
2060 break;
2061 case 'b':
2062 log_branches = 1;
2063 break;
2064 case 'r':
2065 repo_path = realpath(optarg, NULL);
2066 if (repo_path == NULL)
2067 return got_error_from_errno2("realpath",
2068 optarg);
2069 got_path_strip_trailing_slashes(repo_path);
2070 break;
2071 case 's':
2072 search_pattern = optarg;
2073 break;
2074 default:
2075 usage_log();
2076 /* NOTREACHED */
2080 argc -= optind;
2081 argv += optind;
2083 if (diff_context == -1)
2084 diff_context = 3;
2085 else if (!show_patch)
2086 errx(1, "-C reguires -p");
2088 cwd = getcwd(NULL, 0);
2089 if (cwd == NULL) {
2090 error = got_error_from_errno("getcwd");
2091 goto done;
2094 error = got_worktree_open(&worktree, cwd);
2095 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2096 goto done;
2097 error = NULL;
2099 if (argc == 0) {
2100 path = strdup("");
2101 if (path == NULL) {
2102 error = got_error_from_errno("strdup");
2103 goto done;
2105 } else if (argc == 1) {
2106 if (worktree) {
2107 error = got_worktree_resolve_path(&path, worktree,
2108 argv[0]);
2109 if (error)
2110 goto done;
2111 } else {
2112 path = strdup(argv[0]);
2113 if (path == NULL) {
2114 error = got_error_from_errno("strdup");
2115 goto done;
2118 } else
2119 usage_log();
2121 if (repo_path == NULL) {
2122 repo_path = worktree ?
2123 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2125 if (repo_path == NULL) {
2126 error = got_error_from_errno("strdup");
2127 goto done;
2130 error = got_repo_open(&repo, repo_path, NULL);
2131 if (error != NULL)
2132 goto done;
2134 error = apply_unveil(got_repo_get_path(repo), 1,
2135 worktree ? got_worktree_get_root_path(worktree) : NULL);
2136 if (error)
2137 goto done;
2139 if (start_commit == NULL) {
2140 struct got_reference *head_ref;
2141 error = got_ref_open(&head_ref, repo,
2142 worktree ? got_worktree_get_head_ref_name(worktree)
2143 : GOT_REF_HEAD, 0);
2144 if (error != NULL)
2145 return error;
2146 error = got_ref_resolve(&id, repo, head_ref);
2147 got_ref_close(head_ref);
2148 if (error != NULL)
2149 return error;
2150 error = got_object_open_as_commit(&commit, repo, id);
2151 } else {
2152 struct got_reference *ref;
2153 error = got_ref_open(&ref, repo, start_commit, 0);
2154 if (error == NULL) {
2155 int obj_type;
2156 error = got_ref_resolve(&id, repo, ref);
2157 got_ref_close(ref);
2158 if (error != NULL)
2159 goto done;
2160 error = got_object_get_type(&obj_type, repo, id);
2161 if (error != NULL)
2162 goto done;
2163 if (obj_type == GOT_OBJ_TYPE_TAG) {
2164 struct got_tag_object *tag;
2165 error = got_object_open_as_tag(&tag, repo, id);
2166 if (error != NULL)
2167 goto done;
2168 if (got_object_tag_get_object_type(tag) !=
2169 GOT_OBJ_TYPE_COMMIT) {
2170 got_object_tag_close(tag);
2171 error = got_error(GOT_ERR_OBJ_TYPE);
2172 goto done;
2174 free(id);
2175 id = got_object_id_dup(
2176 got_object_tag_get_object_id(tag));
2177 if (id == NULL)
2178 error = got_error_from_errno(
2179 "got_object_id_dup");
2180 got_object_tag_close(tag);
2181 if (error)
2182 goto done;
2183 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2184 error = got_error(GOT_ERR_OBJ_TYPE);
2185 goto done;
2187 error = got_object_open_as_commit(&commit, repo, id);
2188 if (error != NULL)
2189 goto done;
2191 if (commit == NULL) {
2192 error = got_repo_match_object_id_prefix(&id,
2193 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2194 if (error != NULL)
2195 return error;
2198 if (error != NULL)
2199 goto done;
2201 if (worktree) {
2202 const char *prefix = got_worktree_get_path_prefix(worktree);
2203 char *p;
2204 if (asprintf(&p, "%s%s%s", prefix,
2205 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2206 error = got_error_from_errno("asprintf");
2207 goto done;
2209 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2210 free(p);
2211 } else
2212 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2213 if (error != NULL)
2214 goto done;
2215 if (in_repo_path) {
2216 free(path);
2217 path = in_repo_path;
2220 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2221 if (error)
2222 goto done;
2224 error = print_commits(id, repo, path, show_patch, search_pattern,
2225 diff_context, limit, log_branches, &refs);
2226 done:
2227 free(path);
2228 free(repo_path);
2229 free(cwd);
2230 free(id);
2231 if (worktree)
2232 got_worktree_close(worktree);
2233 if (repo) {
2234 const struct got_error *repo_error;
2235 repo_error = got_repo_close(repo);
2236 if (error == NULL)
2237 error = repo_error;
2239 got_ref_list_free(&refs);
2240 return error;
2243 __dead static void
2244 usage_diff(void)
2246 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2247 "[-w] [object1 object2 | path]\n", getprogname());
2248 exit(1);
2251 struct print_diff_arg {
2252 struct got_repository *repo;
2253 struct got_worktree *worktree;
2254 int diff_context;
2255 const char *id_str;
2256 int header_shown;
2257 int diff_staged;
2258 int ignore_whitespace;
2261 static const struct got_error *
2262 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2263 const char *path, struct got_object_id *blob_id,
2264 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2265 int dirfd, const char *de_name)
2267 struct print_diff_arg *a = arg;
2268 const struct got_error *err = NULL;
2269 struct got_blob_object *blob1 = NULL;
2270 int fd = -1;
2271 FILE *f2 = NULL;
2272 char *abspath = NULL, *label1 = NULL;
2273 struct stat sb;
2275 if (a->diff_staged) {
2276 if (staged_status != GOT_STATUS_MODIFY &&
2277 staged_status != GOT_STATUS_ADD &&
2278 staged_status != GOT_STATUS_DELETE)
2279 return NULL;
2280 } else {
2281 if (staged_status == GOT_STATUS_DELETE)
2282 return NULL;
2283 if (status == GOT_STATUS_NONEXISTENT)
2284 return got_error_set_errno(ENOENT, path);
2285 if (status != GOT_STATUS_MODIFY &&
2286 status != GOT_STATUS_ADD &&
2287 status != GOT_STATUS_DELETE &&
2288 status != GOT_STATUS_CONFLICT)
2289 return NULL;
2292 if (!a->header_shown) {
2293 printf("diff %s %s%s\n", a->id_str,
2294 got_worktree_get_root_path(a->worktree),
2295 a->diff_staged ? " (staged changes)" : "");
2296 a->header_shown = 1;
2299 if (a->diff_staged) {
2300 const char *label1 = NULL, *label2 = NULL;
2301 switch (staged_status) {
2302 case GOT_STATUS_MODIFY:
2303 label1 = path;
2304 label2 = path;
2305 break;
2306 case GOT_STATUS_ADD:
2307 label2 = path;
2308 break;
2309 case GOT_STATUS_DELETE:
2310 label1 = path;
2311 break;
2312 default:
2313 return got_error(GOT_ERR_FILE_STATUS);
2315 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2316 label1, label2, a->diff_context, a->ignore_whitespace,
2317 a->repo, stdout);
2320 if (staged_status == GOT_STATUS_ADD ||
2321 staged_status == GOT_STATUS_MODIFY) {
2322 char *id_str;
2323 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2324 8192);
2325 if (err)
2326 goto done;
2327 err = got_object_id_str(&id_str, staged_blob_id);
2328 if (err)
2329 goto done;
2330 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2331 err = got_error_from_errno("asprintf");
2332 free(id_str);
2333 goto done;
2335 free(id_str);
2336 } else if (status != GOT_STATUS_ADD) {
2337 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2338 if (err)
2339 goto done;
2342 if (status != GOT_STATUS_DELETE) {
2343 if (asprintf(&abspath, "%s/%s",
2344 got_worktree_get_root_path(a->worktree), path) == -1) {
2345 err = got_error_from_errno("asprintf");
2346 goto done;
2349 if (dirfd != -1) {
2350 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2351 if (fd == -1) {
2352 err = got_error_from_errno2("openat", abspath);
2353 goto done;
2355 } else {
2356 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2357 if (fd == -1) {
2358 err = got_error_from_errno2("open", abspath);
2359 goto done;
2362 if (fstat(fd, &sb) == -1) {
2363 err = got_error_from_errno2("fstat", abspath);
2364 goto done;
2366 f2 = fdopen(fd, "r");
2367 if (f2 == NULL) {
2368 err = got_error_from_errno2("fdopen", abspath);
2369 goto done;
2371 fd = -1;
2372 } else
2373 sb.st_size = 0;
2375 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2376 a->diff_context, a->ignore_whitespace, stdout);
2377 done:
2378 if (blob1)
2379 got_object_blob_close(blob1);
2380 if (f2 && fclose(f2) == EOF && err == NULL)
2381 err = got_error_from_errno("fclose");
2382 if (fd != -1 && close(fd) == -1 && err == NULL)
2383 err = got_error_from_errno("close");
2384 free(abspath);
2385 return err;
2388 static const struct got_error *
2389 cmd_diff(int argc, char *argv[])
2391 const struct got_error *error;
2392 struct got_repository *repo = NULL;
2393 struct got_worktree *worktree = NULL;
2394 char *cwd = NULL, *repo_path = NULL;
2395 struct got_object_id *id1 = NULL, *id2 = NULL;
2396 const char *id_str1 = NULL, *id_str2 = NULL;
2397 char *label1 = NULL, *label2 = NULL;
2398 int type1, type2;
2399 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2400 const char *errstr;
2401 char *path = NULL;
2403 #ifndef PROFILE
2404 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2405 NULL) == -1)
2406 err(1, "pledge");
2407 #endif
2409 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2410 switch (ch) {
2411 case 'C':
2412 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2413 &errstr);
2414 if (errstr != NULL)
2415 err(1, "-C option %s", errstr);
2416 break;
2417 case 'r':
2418 repo_path = realpath(optarg, NULL);
2419 if (repo_path == NULL)
2420 return got_error_from_errno2("realpath",
2421 optarg);
2422 got_path_strip_trailing_slashes(repo_path);
2423 break;
2424 case 's':
2425 diff_staged = 1;
2426 break;
2427 case 'w':
2428 ignore_whitespace = 1;
2429 break;
2430 default:
2431 usage_diff();
2432 /* NOTREACHED */
2436 argc -= optind;
2437 argv += optind;
2439 cwd = getcwd(NULL, 0);
2440 if (cwd == NULL) {
2441 error = got_error_from_errno("getcwd");
2442 goto done;
2444 error = got_worktree_open(&worktree, cwd);
2445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2446 goto done;
2447 if (argc <= 1) {
2448 if (worktree == NULL) {
2449 error = got_error(GOT_ERR_NOT_WORKTREE);
2450 goto done;
2452 if (repo_path)
2453 errx(1,
2454 "-r option can't be used when diffing a work tree");
2455 repo_path = strdup(got_worktree_get_repo_path(worktree));
2456 if (repo_path == NULL) {
2457 error = got_error_from_errno("strdup");
2458 goto done;
2460 if (argc == 1) {
2461 error = got_worktree_resolve_path(&path, worktree,
2462 argv[0]);
2463 if (error)
2464 goto done;
2465 } else {
2466 path = strdup("");
2467 if (path == NULL) {
2468 error = got_error_from_errno("strdup");
2469 goto done;
2472 } else if (argc == 2) {
2473 if (diff_staged)
2474 errx(1, "-s option can't be used when diffing "
2475 "objects in repository");
2476 id_str1 = argv[0];
2477 id_str2 = argv[1];
2478 if (worktree && repo_path == NULL) {
2479 repo_path =
2480 strdup(got_worktree_get_repo_path(worktree));
2481 if (repo_path == NULL) {
2482 error = got_error_from_errno("strdup");
2483 goto done;
2486 } else
2487 usage_diff();
2489 if (repo_path == NULL) {
2490 repo_path = getcwd(NULL, 0);
2491 if (repo_path == NULL)
2492 return got_error_from_errno("getcwd");
2495 error = got_repo_open(&repo, repo_path, NULL);
2496 free(repo_path);
2497 if (error != NULL)
2498 goto done;
2500 error = apply_unveil(got_repo_get_path(repo), 1,
2501 worktree ? got_worktree_get_root_path(worktree) : NULL);
2502 if (error)
2503 goto done;
2505 if (argc <= 1) {
2506 struct print_diff_arg arg;
2507 struct got_pathlist_head paths;
2508 char *id_str;
2510 TAILQ_INIT(&paths);
2512 error = got_object_id_str(&id_str,
2513 got_worktree_get_base_commit_id(worktree));
2514 if (error)
2515 goto done;
2516 arg.repo = repo;
2517 arg.worktree = worktree;
2518 arg.diff_context = diff_context;
2519 arg.id_str = id_str;
2520 arg.header_shown = 0;
2521 arg.diff_staged = diff_staged;
2522 arg.ignore_whitespace = ignore_whitespace;
2524 error = got_pathlist_append(&paths, path, NULL);
2525 if (error)
2526 goto done;
2528 error = got_worktree_status(worktree, &paths, repo, print_diff,
2529 &arg, check_cancelled, NULL);
2530 free(id_str);
2531 got_pathlist_free(&paths);
2532 goto done;
2535 error = got_repo_match_object_id(&id1, &label1, id_str1,
2536 GOT_OBJ_TYPE_ANY, 1, repo);
2537 if (error)
2538 goto done;
2540 error = got_repo_match_object_id(&id2, &label2, id_str2,
2541 GOT_OBJ_TYPE_ANY, 1, repo);
2542 if (error)
2543 goto done;
2545 error = got_object_get_type(&type1, repo, id1);
2546 if (error)
2547 goto done;
2549 error = got_object_get_type(&type2, repo, id2);
2550 if (error)
2551 goto done;
2553 if (type1 != type2) {
2554 error = got_error(GOT_ERR_OBJ_TYPE);
2555 goto done;
2558 switch (type1) {
2559 case GOT_OBJ_TYPE_BLOB:
2560 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2561 diff_context, ignore_whitespace, repo, stdout);
2562 break;
2563 case GOT_OBJ_TYPE_TREE:
2564 error = got_diff_objects_as_trees(id1, id2, "", "",
2565 diff_context, ignore_whitespace, repo, stdout);
2566 break;
2567 case GOT_OBJ_TYPE_COMMIT:
2568 printf("diff %s %s\n", label1, label2);
2569 error = got_diff_objects_as_commits(id1, id2, diff_context,
2570 ignore_whitespace, repo, stdout);
2571 break;
2572 default:
2573 error = got_error(GOT_ERR_OBJ_TYPE);
2575 done:
2576 free(label1);
2577 free(label2);
2578 free(id1);
2579 free(id2);
2580 free(path);
2581 if (worktree)
2582 got_worktree_close(worktree);
2583 if (repo) {
2584 const struct got_error *repo_error;
2585 repo_error = got_repo_close(repo);
2586 if (error == NULL)
2587 error = repo_error;
2589 return error;
2592 __dead static void
2593 usage_blame(void)
2595 fprintf(stderr,
2596 "usage: %s blame [-c commit] [-r repository-path] path\n",
2597 getprogname());
2598 exit(1);
2601 struct blame_line {
2602 int annotated;
2603 char *id_str;
2604 char *committer;
2605 char datebuf[11]; /* YYYY-MM-DD + NUL */
2608 struct blame_cb_args {
2609 struct blame_line *lines;
2610 int nlines;
2611 int nlines_prec;
2612 int lineno_cur;
2613 off_t *line_offsets;
2614 FILE *f;
2615 struct got_repository *repo;
2618 static const struct got_error *
2619 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2621 const struct got_error *err = NULL;
2622 struct blame_cb_args *a = arg;
2623 struct blame_line *bline;
2624 char *line = NULL;
2625 size_t linesize = 0;
2626 struct got_commit_object *commit = NULL;
2627 off_t offset;
2628 struct tm tm;
2629 time_t committer_time;
2631 if (nlines != a->nlines ||
2632 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2633 return got_error(GOT_ERR_RANGE);
2635 if (sigint_received)
2636 return got_error(GOT_ERR_ITER_COMPLETED);
2638 if (lineno == -1)
2639 return NULL; /* no change in this commit */
2641 /* Annotate this line. */
2642 bline = &a->lines[lineno - 1];
2643 if (bline->annotated)
2644 return NULL;
2645 err = got_object_id_str(&bline->id_str, id);
2646 if (err)
2647 return err;
2649 err = got_object_open_as_commit(&commit, a->repo, id);
2650 if (err)
2651 goto done;
2653 bline->committer = strdup(got_object_commit_get_committer(commit));
2654 if (bline->committer == NULL) {
2655 err = got_error_from_errno("strdup");
2656 goto done;
2659 committer_time = got_object_commit_get_committer_time(commit);
2660 if (localtime_r(&committer_time, &tm) == NULL)
2661 return got_error_from_errno("localtime_r");
2662 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2663 &tm) >= sizeof(bline->datebuf)) {
2664 err = got_error(GOT_ERR_NO_SPACE);
2665 goto done;
2667 bline->annotated = 1;
2669 /* Print lines annotated so far. */
2670 bline = &a->lines[a->lineno_cur - 1];
2671 if (!bline->annotated)
2672 goto done;
2674 offset = a->line_offsets[a->lineno_cur - 1];
2675 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2676 err = got_error_from_errno("fseeko");
2677 goto done;
2680 while (bline->annotated) {
2681 char *smallerthan, *at, *nl, *committer;
2682 size_t len;
2684 if (getline(&line, &linesize, a->f) == -1) {
2685 if (ferror(a->f))
2686 err = got_error_from_errno("getline");
2687 break;
2690 committer = bline->committer;
2691 smallerthan = strchr(committer, '<');
2692 if (smallerthan && smallerthan[1] != '\0')
2693 committer = smallerthan + 1;
2694 at = strchr(committer, '@');
2695 if (at)
2696 *at = '\0';
2697 len = strlen(committer);
2698 if (len >= 9)
2699 committer[8] = '\0';
2701 nl = strchr(line, '\n');
2702 if (nl)
2703 *nl = '\0';
2704 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2705 bline->id_str, bline->datebuf, committer, line);
2707 a->lineno_cur++;
2708 bline = &a->lines[a->lineno_cur - 1];
2710 done:
2711 if (commit)
2712 got_object_commit_close(commit);
2713 free(line);
2714 return err;
2717 static const struct got_error *
2718 cmd_blame(int argc, char *argv[])
2720 const struct got_error *error;
2721 struct got_repository *repo = NULL;
2722 struct got_worktree *worktree = NULL;
2723 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2724 struct got_object_id *obj_id = NULL;
2725 struct got_object_id *commit_id = NULL;
2726 struct got_blob_object *blob = NULL;
2727 char *commit_id_str = NULL;
2728 struct blame_cb_args bca;
2729 int ch, obj_type, i;
2730 size_t filesize;
2732 memset(&bca, 0, sizeof(bca));
2734 #ifndef PROFILE
2735 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2736 NULL) == -1)
2737 err(1, "pledge");
2738 #endif
2740 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2741 switch (ch) {
2742 case 'c':
2743 commit_id_str = optarg;
2744 break;
2745 case 'r':
2746 repo_path = realpath(optarg, NULL);
2747 if (repo_path == NULL)
2748 return got_error_from_errno2("realpath",
2749 optarg);
2750 got_path_strip_trailing_slashes(repo_path);
2751 break;
2752 default:
2753 usage_blame();
2754 /* NOTREACHED */
2758 argc -= optind;
2759 argv += optind;
2761 if (argc == 1)
2762 path = argv[0];
2763 else
2764 usage_blame();
2766 cwd = getcwd(NULL, 0);
2767 if (cwd == NULL) {
2768 error = got_error_from_errno("getcwd");
2769 goto done;
2771 if (repo_path == NULL) {
2772 error = got_worktree_open(&worktree, cwd);
2773 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2774 goto done;
2775 else
2776 error = NULL;
2777 if (worktree) {
2778 repo_path =
2779 strdup(got_worktree_get_repo_path(worktree));
2780 if (repo_path == NULL)
2781 error = got_error_from_errno("strdup");
2782 if (error)
2783 goto done;
2784 } else {
2785 repo_path = strdup(cwd);
2786 if (repo_path == NULL) {
2787 error = got_error_from_errno("strdup");
2788 goto done;
2793 error = got_repo_open(&repo, repo_path, NULL);
2794 if (error != NULL)
2795 goto done;
2797 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2798 if (error)
2799 goto done;
2801 if (worktree) {
2802 const char *prefix = got_worktree_get_path_prefix(worktree);
2803 char *p, *worktree_subdir = cwd +
2804 strlen(got_worktree_get_root_path(worktree));
2805 if (asprintf(&p, "%s%s%s%s%s",
2806 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2807 worktree_subdir, worktree_subdir[0] ? "/" : "",
2808 path) == -1) {
2809 error = got_error_from_errno("asprintf");
2810 goto done;
2812 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2813 free(p);
2814 } else {
2815 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2817 if (error)
2818 goto done;
2820 if (commit_id_str == NULL) {
2821 struct got_reference *head_ref;
2822 error = got_ref_open(&head_ref, repo, worktree ?
2823 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2824 if (error != NULL)
2825 goto done;
2826 error = got_ref_resolve(&commit_id, repo, head_ref);
2827 got_ref_close(head_ref);
2828 if (error != NULL)
2829 goto done;
2830 } else {
2831 error = got_repo_match_object_id(&commit_id, NULL,
2832 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2833 if (error)
2834 goto done;
2837 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2838 if (error)
2839 goto done;
2841 error = got_object_get_type(&obj_type, repo, obj_id);
2842 if (error)
2843 goto done;
2845 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2846 error = got_error(GOT_ERR_OBJ_TYPE);
2847 goto done;
2850 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2851 if (error)
2852 goto done;
2853 bca.f = got_opentemp();
2854 if (bca.f == NULL) {
2855 error = got_error_from_errno("got_opentemp");
2856 goto done;
2858 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2859 &bca.line_offsets, bca.f, blob);
2860 if (error || bca.nlines == 0)
2861 goto done;
2863 /* Don't include \n at EOF in the blame line count. */
2864 if (bca.line_offsets[bca.nlines - 1] == filesize)
2865 bca.nlines--;
2867 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2868 if (bca.lines == NULL) {
2869 error = got_error_from_errno("calloc");
2870 goto done;
2872 bca.lineno_cur = 1;
2873 bca.nlines_prec = 0;
2874 i = bca.nlines;
2875 while (i > 0) {
2876 i /= 10;
2877 bca.nlines_prec++;
2879 bca.repo = repo;
2881 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2882 check_cancelled, NULL);
2883 done:
2884 free(in_repo_path);
2885 free(repo_path);
2886 free(cwd);
2887 free(commit_id);
2888 free(obj_id);
2889 if (blob)
2890 got_object_blob_close(blob);
2891 if (worktree)
2892 got_worktree_close(worktree);
2893 if (repo) {
2894 const struct got_error *repo_error;
2895 repo_error = got_repo_close(repo);
2896 if (error == NULL)
2897 error = repo_error;
2899 if (bca.lines) {
2900 for (i = 0; i < bca.nlines; i++) {
2901 struct blame_line *bline = &bca.lines[i];
2902 free(bline->id_str);
2903 free(bline->committer);
2905 free(bca.lines);
2907 free(bca.line_offsets);
2908 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2909 error = got_error_from_errno("fclose");
2910 return error;
2913 __dead static void
2914 usage_tree(void)
2916 fprintf(stderr,
2917 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2918 getprogname());
2919 exit(1);
2922 static void
2923 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2924 const char *root_path)
2926 int is_root_path = (strcmp(path, root_path) == 0);
2927 const char *modestr = "";
2928 mode_t mode = got_tree_entry_get_mode(te);
2930 path += strlen(root_path);
2931 while (path[0] == '/')
2932 path++;
2934 if (got_object_tree_entry_is_submodule(te))
2935 modestr = "$";
2936 else if (S_ISLNK(mode))
2937 modestr = "@";
2938 else if (S_ISDIR(mode))
2939 modestr = "/";
2940 else if (mode & S_IXUSR)
2941 modestr = "*";
2943 printf("%s%s%s%s%s\n", id ? id : "", path,
2944 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2947 static const struct got_error *
2948 print_tree(const char *path, struct got_object_id *commit_id,
2949 int show_ids, int recurse, const char *root_path,
2950 struct got_repository *repo)
2952 const struct got_error *err = NULL;
2953 struct got_object_id *tree_id = NULL;
2954 struct got_tree_object *tree = NULL;
2955 int nentries, i;
2957 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2958 if (err)
2959 goto done;
2961 err = got_object_open_as_tree(&tree, repo, tree_id);
2962 if (err)
2963 goto done;
2964 nentries = got_object_tree_get_nentries(tree);
2965 for (i = 0; i < nentries; i++) {
2966 struct got_tree_entry *te;
2967 char *id = NULL;
2969 if (sigint_received || sigpipe_received)
2970 break;
2972 te = got_object_tree_get_entry(tree, i);
2973 if (show_ids) {
2974 char *id_str;
2975 err = got_object_id_str(&id_str,
2976 got_tree_entry_get_id(te));
2977 if (err)
2978 goto done;
2979 if (asprintf(&id, "%s ", id_str) == -1) {
2980 err = got_error_from_errno("asprintf");
2981 free(id_str);
2982 goto done;
2984 free(id_str);
2986 print_entry(te, id, path, root_path);
2987 free(id);
2989 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2990 char *child_path;
2991 if (asprintf(&child_path, "%s%s%s", path,
2992 path[0] == '/' && path[1] == '\0' ? "" : "/",
2993 got_tree_entry_get_name(te)) == -1) {
2994 err = got_error_from_errno("asprintf");
2995 goto done;
2997 err = print_tree(child_path, commit_id, show_ids, 1,
2998 root_path, repo);
2999 free(child_path);
3000 if (err)
3001 goto done;
3004 done:
3005 if (tree)
3006 got_object_tree_close(tree);
3007 free(tree_id);
3008 return err;
3011 static const struct got_error *
3012 cmd_tree(int argc, char *argv[])
3014 const struct got_error *error;
3015 struct got_repository *repo = NULL;
3016 struct got_worktree *worktree = NULL;
3017 const char *path;
3018 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3019 struct got_object_id *commit_id = NULL;
3020 char *commit_id_str = NULL;
3021 int show_ids = 0, recurse = 0;
3022 int ch;
3024 #ifndef PROFILE
3025 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3026 NULL) == -1)
3027 err(1, "pledge");
3028 #endif
3030 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3031 switch (ch) {
3032 case 'c':
3033 commit_id_str = optarg;
3034 break;
3035 case 'r':
3036 repo_path = realpath(optarg, NULL);
3037 if (repo_path == NULL)
3038 return got_error_from_errno2("realpath",
3039 optarg);
3040 got_path_strip_trailing_slashes(repo_path);
3041 break;
3042 case 'i':
3043 show_ids = 1;
3044 break;
3045 case 'R':
3046 recurse = 1;
3047 break;
3048 default:
3049 usage_tree();
3050 /* NOTREACHED */
3054 argc -= optind;
3055 argv += optind;
3057 if (argc == 1)
3058 path = argv[0];
3059 else if (argc > 1)
3060 usage_tree();
3061 else
3062 path = NULL;
3064 cwd = getcwd(NULL, 0);
3065 if (cwd == NULL) {
3066 error = got_error_from_errno("getcwd");
3067 goto done;
3069 if (repo_path == NULL) {
3070 error = got_worktree_open(&worktree, cwd);
3071 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3072 goto done;
3073 else
3074 error = NULL;
3075 if (worktree) {
3076 repo_path =
3077 strdup(got_worktree_get_repo_path(worktree));
3078 if (repo_path == NULL)
3079 error = got_error_from_errno("strdup");
3080 if (error)
3081 goto done;
3082 } else {
3083 repo_path = strdup(cwd);
3084 if (repo_path == NULL) {
3085 error = got_error_from_errno("strdup");
3086 goto done;
3091 error = got_repo_open(&repo, repo_path, NULL);
3092 if (error != NULL)
3093 goto done;
3095 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3096 if (error)
3097 goto done;
3099 if (path == NULL) {
3100 if (worktree) {
3101 char *p, *worktree_subdir = cwd +
3102 strlen(got_worktree_get_root_path(worktree));
3103 if (asprintf(&p, "%s/%s",
3104 got_worktree_get_path_prefix(worktree),
3105 worktree_subdir) == -1) {
3106 error = got_error_from_errno("asprintf");
3107 goto done;
3109 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3110 free(p);
3111 if (error)
3112 goto done;
3113 } else
3114 path = "/";
3116 if (in_repo_path == NULL) {
3117 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3118 if (error != NULL)
3119 goto done;
3122 if (commit_id_str == NULL) {
3123 struct got_reference *head_ref;
3124 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3125 if (error != NULL)
3126 goto done;
3127 error = got_ref_resolve(&commit_id, repo, head_ref);
3128 got_ref_close(head_ref);
3129 if (error != NULL)
3130 goto done;
3131 } else {
3132 error = got_repo_match_object_id(&commit_id, NULL,
3133 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3134 if (error)
3135 goto done;
3138 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3139 in_repo_path, repo);
3140 done:
3141 free(in_repo_path);
3142 free(repo_path);
3143 free(cwd);
3144 free(commit_id);
3145 if (worktree)
3146 got_worktree_close(worktree);
3147 if (repo) {
3148 const struct got_error *repo_error;
3149 repo_error = got_repo_close(repo);
3150 if (error == NULL)
3151 error = repo_error;
3153 return error;
3156 __dead static void
3157 usage_status(void)
3159 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3160 exit(1);
3163 static const struct got_error *
3164 print_status(void *arg, unsigned char status, unsigned char staged_status,
3165 const char *path, struct got_object_id *blob_id,
3166 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3167 int dirfd, const char *de_name)
3169 if (status == staged_status && (status == GOT_STATUS_DELETE))
3170 status = GOT_STATUS_NO_CHANGE;
3171 printf("%c%c %s\n", status, staged_status, path);
3172 return NULL;
3175 static const struct got_error *
3176 cmd_status(int argc, char *argv[])
3178 const struct got_error *error = NULL;
3179 struct got_repository *repo = NULL;
3180 struct got_worktree *worktree = NULL;
3181 char *cwd = NULL;
3182 struct got_pathlist_head paths;
3183 struct got_pathlist_entry *pe;
3184 int ch;
3186 TAILQ_INIT(&paths);
3188 while ((ch = getopt(argc, argv, "")) != -1) {
3189 switch (ch) {
3190 default:
3191 usage_status();
3192 /* NOTREACHED */
3196 argc -= optind;
3197 argv += optind;
3199 #ifndef PROFILE
3200 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3201 NULL) == -1)
3202 err(1, "pledge");
3203 #endif
3204 cwd = getcwd(NULL, 0);
3205 if (cwd == NULL) {
3206 error = got_error_from_errno("getcwd");
3207 goto done;
3210 error = got_worktree_open(&worktree, cwd);
3211 if (error != NULL)
3212 goto done;
3214 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3215 NULL);
3216 if (error != NULL)
3217 goto done;
3219 error = apply_unveil(got_repo_get_path(repo), 1,
3220 got_worktree_get_root_path(worktree));
3221 if (error)
3222 goto done;
3224 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3225 if (error)
3226 goto done;
3228 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3229 check_cancelled, NULL);
3230 done:
3231 TAILQ_FOREACH(pe, &paths, entry)
3232 free((char *)pe->path);
3233 got_pathlist_free(&paths);
3234 free(cwd);
3235 return error;
3238 __dead static void
3239 usage_ref(void)
3241 fprintf(stderr,
3242 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3243 getprogname());
3244 exit(1);
3247 static const struct got_error *
3248 list_refs(struct got_repository *repo)
3250 static const struct got_error *err = NULL;
3251 struct got_reflist_head refs;
3252 struct got_reflist_entry *re;
3254 SIMPLEQ_INIT(&refs);
3255 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3256 if (err)
3257 return err;
3259 SIMPLEQ_FOREACH(re, &refs, entry) {
3260 char *refstr;
3261 refstr = got_ref_to_str(re->ref);
3262 if (refstr == NULL)
3263 return got_error_from_errno("got_ref_to_str");
3264 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3265 free(refstr);
3268 got_ref_list_free(&refs);
3269 return NULL;
3272 static const struct got_error *
3273 delete_ref(struct got_repository *repo, const char *refname)
3275 const struct got_error *err = NULL;
3276 struct got_reference *ref;
3278 err = got_ref_open(&ref, repo, refname, 0);
3279 if (err)
3280 return err;
3282 err = got_ref_delete(ref, repo);
3283 got_ref_close(ref);
3284 return err;
3287 static const struct got_error *
3288 add_ref(struct got_repository *repo, const char *refname, const char *target)
3290 const struct got_error *err = NULL;
3291 struct got_object_id *id;
3292 struct got_reference *ref = NULL;
3295 * Don't let the user create a reference name with a leading '-'.
3296 * While technically a valid reference name, this case is usually
3297 * an unintended typo.
3299 if (refname[0] == '-')
3300 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3302 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3303 repo);
3304 if (err) {
3305 struct got_reference *target_ref;
3307 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3308 return err;
3309 err = got_ref_open(&target_ref, repo, target, 0);
3310 if (err)
3311 return err;
3312 err = got_ref_resolve(&id, repo, target_ref);
3313 got_ref_close(target_ref);
3314 if (err)
3315 return err;
3318 err = got_ref_alloc(&ref, refname, id);
3319 if (err)
3320 goto done;
3322 err = got_ref_write(ref, repo);
3323 done:
3324 if (ref)
3325 got_ref_close(ref);
3326 free(id);
3327 return err;
3330 static const struct got_error *
3331 add_symref(struct got_repository *repo, const char *refname, const char *target)
3333 const struct got_error *err = NULL;
3334 struct got_reference *ref = NULL;
3335 struct got_reference *target_ref = NULL;
3338 * Don't let the user create a reference name with a leading '-'.
3339 * While technically a valid reference name, this case is usually
3340 * an unintended typo.
3342 if (refname[0] == '-')
3343 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3345 err = got_ref_open(&target_ref, repo, target, 0);
3346 if (err)
3347 return err;
3349 err = got_ref_alloc_symref(&ref, refname, target_ref);
3350 if (err)
3351 goto done;
3353 err = got_ref_write(ref, repo);
3354 done:
3355 if (target_ref)
3356 got_ref_close(target_ref);
3357 if (ref)
3358 got_ref_close(ref);
3359 return err;
3362 static const struct got_error *
3363 cmd_ref(int argc, char *argv[])
3365 const struct got_error *error = NULL;
3366 struct got_repository *repo = NULL;
3367 struct got_worktree *worktree = NULL;
3368 char *cwd = NULL, *repo_path = NULL;
3369 int ch, do_list = 0, create_symref = 0;
3370 const char *delref = NULL;
3372 /* TODO: Add -s option for adding symbolic references. */
3373 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3374 switch (ch) {
3375 case 'd':
3376 delref = optarg;
3377 break;
3378 case 'r':
3379 repo_path = realpath(optarg, NULL);
3380 if (repo_path == NULL)
3381 return got_error_from_errno2("realpath",
3382 optarg);
3383 got_path_strip_trailing_slashes(repo_path);
3384 break;
3385 case 'l':
3386 do_list = 1;
3387 break;
3388 case 's':
3389 create_symref = 1;
3390 break;
3391 default:
3392 usage_ref();
3393 /* NOTREACHED */
3397 if (do_list && delref)
3398 errx(1, "-l and -d options are mutually exclusive\n");
3400 argc -= optind;
3401 argv += optind;
3403 if (do_list || delref) {
3404 if (create_symref)
3405 errx(1, "-s option cannot be used together with the "
3406 "-l or -d options");
3407 if (argc > 0)
3408 usage_ref();
3409 } else if (argc != 2)
3410 usage_ref();
3412 #ifndef PROFILE
3413 if (do_list) {
3414 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3415 NULL) == -1)
3416 err(1, "pledge");
3417 } else {
3418 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3419 "sendfd unveil", NULL) == -1)
3420 err(1, "pledge");
3422 #endif
3423 cwd = getcwd(NULL, 0);
3424 if (cwd == NULL) {
3425 error = got_error_from_errno("getcwd");
3426 goto done;
3429 if (repo_path == NULL) {
3430 error = got_worktree_open(&worktree, cwd);
3431 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3432 goto done;
3433 else
3434 error = NULL;
3435 if (worktree) {
3436 repo_path =
3437 strdup(got_worktree_get_repo_path(worktree));
3438 if (repo_path == NULL)
3439 error = got_error_from_errno("strdup");
3440 if (error)
3441 goto done;
3442 } else {
3443 repo_path = strdup(cwd);
3444 if (repo_path == NULL) {
3445 error = got_error_from_errno("strdup");
3446 goto done;
3451 error = got_repo_open(&repo, repo_path, NULL);
3452 if (error != NULL)
3453 goto done;
3455 error = apply_unveil(got_repo_get_path(repo), do_list,
3456 worktree ? got_worktree_get_root_path(worktree) : NULL);
3457 if (error)
3458 goto done;
3460 if (do_list)
3461 error = list_refs(repo);
3462 else if (delref)
3463 error = delete_ref(repo, delref);
3464 else if (create_symref)
3465 error = add_symref(repo, argv[0], argv[1]);
3466 else
3467 error = add_ref(repo, argv[0], argv[1]);
3468 done:
3469 if (repo)
3470 got_repo_close(repo);
3471 if (worktree)
3472 got_worktree_close(worktree);
3473 free(cwd);
3474 free(repo_path);
3475 return error;
3478 __dead static void
3479 usage_branch(void)
3481 fprintf(stderr,
3482 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3483 "[name]\n", getprogname());
3484 exit(1);
3487 static const struct got_error *
3488 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3489 struct got_reference *ref)
3491 const struct got_error *err = NULL;
3492 const char *refname, *marker = " ";
3493 char *refstr;
3495 refname = got_ref_get_name(ref);
3496 if (worktree && strcmp(refname,
3497 got_worktree_get_head_ref_name(worktree)) == 0) {
3498 struct got_object_id *id = NULL;
3500 err = got_ref_resolve(&id, repo, ref);
3501 if (err)
3502 return err;
3503 if (got_object_id_cmp(id,
3504 got_worktree_get_base_commit_id(worktree)) == 0)
3505 marker = "* ";
3506 else
3507 marker = "~ ";
3508 free(id);
3511 if (strncmp(refname, "refs/heads/", 11) == 0)
3512 refname += 11;
3513 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3514 refname += 18;
3516 refstr = got_ref_to_str(ref);
3517 if (refstr == NULL)
3518 return got_error_from_errno("got_ref_to_str");
3520 printf("%s%s: %s\n", marker, refname, refstr);
3521 free(refstr);
3522 return NULL;
3525 static const struct got_error *
3526 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3528 const char *refname;
3530 if (worktree == NULL)
3531 return got_error(GOT_ERR_NOT_WORKTREE);
3533 refname = got_worktree_get_head_ref_name(worktree);
3535 if (strncmp(refname, "refs/heads/", 11) == 0)
3536 refname += 11;
3537 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3538 refname += 18;
3540 printf("%s\n", refname);
3542 return NULL;
3545 static const struct got_error *
3546 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3548 static const struct got_error *err = NULL;
3549 struct got_reflist_head refs;
3550 struct got_reflist_entry *re;
3551 struct got_reference *temp_ref = NULL;
3552 int rebase_in_progress, histedit_in_progress;
3554 SIMPLEQ_INIT(&refs);
3556 if (worktree) {
3557 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3558 worktree);
3559 if (err)
3560 return err;
3562 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3563 worktree);
3564 if (err)
3565 return err;
3567 if (rebase_in_progress || histedit_in_progress) {
3568 err = got_ref_open(&temp_ref, repo,
3569 got_worktree_get_head_ref_name(worktree), 0);
3570 if (err)
3571 return err;
3572 list_branch(repo, worktree, temp_ref);
3573 got_ref_close(temp_ref);
3577 err = got_ref_list(&refs, repo, "refs/heads",
3578 got_ref_cmp_by_name, NULL);
3579 if (err)
3580 return err;
3582 SIMPLEQ_FOREACH(re, &refs, entry)
3583 list_branch(repo, worktree, re->ref);
3585 got_ref_list_free(&refs);
3586 return NULL;
3589 static const struct got_error *
3590 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3591 const char *branch_name)
3593 const struct got_error *err = NULL;
3594 struct got_reference *ref = NULL;
3595 char *refname;
3597 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3598 return got_error_from_errno("asprintf");
3600 err = got_ref_open(&ref, repo, refname, 0);
3601 if (err)
3602 goto done;
3604 if (worktree &&
3605 strcmp(got_worktree_get_head_ref_name(worktree),
3606 got_ref_get_name(ref)) == 0) {
3607 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3608 "will not delete this work tree's current branch");
3609 goto done;
3612 err = got_ref_delete(ref, repo);
3613 done:
3614 if (ref)
3615 got_ref_close(ref);
3616 free(refname);
3617 return err;
3620 static const struct got_error *
3621 add_branch(struct got_repository *repo, const char *branch_name,
3622 struct got_object_id *base_commit_id)
3624 const struct got_error *err = NULL;
3625 struct got_reference *ref = NULL;
3626 char *base_refname = NULL, *refname = NULL;
3629 * Don't let the user create a branch name with a leading '-'.
3630 * While technically a valid reference name, this case is usually
3631 * an unintended typo.
3633 if (branch_name[0] == '-')
3634 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3636 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3637 err = got_error_from_errno("asprintf");
3638 goto done;
3641 err = got_ref_open(&ref, repo, refname, 0);
3642 if (err == NULL) {
3643 err = got_error(GOT_ERR_BRANCH_EXISTS);
3644 goto done;
3645 } else if (err->code != GOT_ERR_NOT_REF)
3646 goto done;
3648 err = got_ref_alloc(&ref, refname, base_commit_id);
3649 if (err)
3650 goto done;
3652 err = got_ref_write(ref, repo);
3653 done:
3654 if (ref)
3655 got_ref_close(ref);
3656 free(base_refname);
3657 free(refname);
3658 return err;
3661 static const struct got_error *
3662 cmd_branch(int argc, char *argv[])
3664 const struct got_error *error = NULL;
3665 struct got_repository *repo = NULL;
3666 struct got_worktree *worktree = NULL;
3667 char *cwd = NULL, *repo_path = NULL;
3668 int ch, do_list = 0, do_show = 0, do_update = 1;
3669 const char *delref = NULL, *commit_id_arg = NULL;
3670 struct got_reference *ref = NULL;
3671 struct got_pathlist_head paths;
3672 struct got_pathlist_entry *pe;
3673 struct got_object_id *commit_id = NULL;
3674 char *commit_id_str = NULL;
3676 TAILQ_INIT(&paths);
3678 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3679 switch (ch) {
3680 case 'c':
3681 commit_id_arg = optarg;
3682 break;
3683 case 'd':
3684 delref = optarg;
3685 break;
3686 case 'r':
3687 repo_path = realpath(optarg, NULL);
3688 if (repo_path == NULL)
3689 return got_error_from_errno2("realpath",
3690 optarg);
3691 got_path_strip_trailing_slashes(repo_path);
3692 break;
3693 case 'l':
3694 do_list = 1;
3695 break;
3696 case 'n':
3697 do_update = 0;
3698 break;
3699 default:
3700 usage_branch();
3701 /* NOTREACHED */
3705 if (do_list && delref)
3706 errx(1, "-l and -d options are mutually exclusive\n");
3708 argc -= optind;
3709 argv += optind;
3711 if (!do_list && !delref && argc == 0)
3712 do_show = 1;
3714 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3715 errx(1, "-c option can only be used when creating a branch");
3717 if (do_list || delref) {
3718 if (argc > 0)
3719 usage_branch();
3720 } else if (!do_show && argc != 1)
3721 usage_branch();
3723 #ifndef PROFILE
3724 if (do_list || do_show) {
3725 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3726 NULL) == -1)
3727 err(1, "pledge");
3728 } else {
3729 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3730 "sendfd unveil", NULL) == -1)
3731 err(1, "pledge");
3733 #endif
3734 cwd = getcwd(NULL, 0);
3735 if (cwd == NULL) {
3736 error = got_error_from_errno("getcwd");
3737 goto done;
3740 if (repo_path == NULL) {
3741 error = got_worktree_open(&worktree, cwd);
3742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3743 goto done;
3744 else
3745 error = NULL;
3746 if (worktree) {
3747 repo_path =
3748 strdup(got_worktree_get_repo_path(worktree));
3749 if (repo_path == NULL)
3750 error = got_error_from_errno("strdup");
3751 if (error)
3752 goto done;
3753 } else {
3754 repo_path = strdup(cwd);
3755 if (repo_path == NULL) {
3756 error = got_error_from_errno("strdup");
3757 goto done;
3762 error = got_repo_open(&repo, repo_path, NULL);
3763 if (error != NULL)
3764 goto done;
3766 error = apply_unveil(got_repo_get_path(repo), do_list,
3767 worktree ? got_worktree_get_root_path(worktree) : NULL);
3768 if (error)
3769 goto done;
3771 if (do_show)
3772 error = show_current_branch(repo, worktree);
3773 else if (do_list)
3774 error = list_branches(repo, worktree);
3775 else if (delref)
3776 error = delete_branch(repo, worktree, delref);
3777 else {
3778 if (commit_id_arg == NULL)
3779 commit_id_arg = worktree ?
3780 got_worktree_get_head_ref_name(worktree) :
3781 GOT_REF_HEAD;
3782 error = got_repo_match_object_id(&commit_id, NULL,
3783 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3784 if (error)
3785 goto done;
3786 error = add_branch(repo, argv[0], commit_id);
3787 if (error)
3788 goto done;
3789 if (worktree && do_update) {
3790 int did_something = 0;
3791 char *branch_refname = NULL;
3793 error = got_object_id_str(&commit_id_str, commit_id);
3794 if (error)
3795 goto done;
3796 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3797 worktree);
3798 if (error)
3799 goto done;
3800 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3801 == -1) {
3802 error = got_error_from_errno("asprintf");
3803 goto done;
3805 error = got_ref_open(&ref, repo, branch_refname, 0);
3806 free(branch_refname);
3807 if (error)
3808 goto done;
3809 error = switch_head_ref(ref, commit_id, worktree,
3810 repo);
3811 if (error)
3812 goto done;
3813 error = got_worktree_set_base_commit_id(worktree, repo,
3814 commit_id);
3815 if (error)
3816 goto done;
3817 error = got_worktree_checkout_files(worktree, &paths,
3818 repo, update_progress, &did_something,
3819 check_cancelled, NULL);
3820 if (error)
3821 goto done;
3822 if (did_something)
3823 printf("Updated to commit %s\n", commit_id_str);
3826 done:
3827 if (ref)
3828 got_ref_close(ref);
3829 if (repo)
3830 got_repo_close(repo);
3831 if (worktree)
3832 got_worktree_close(worktree);
3833 free(cwd);
3834 free(repo_path);
3835 free(commit_id);
3836 free(commit_id_str);
3837 TAILQ_FOREACH(pe, &paths, entry)
3838 free((char *)pe->path);
3839 got_pathlist_free(&paths);
3840 return error;
3844 __dead static void
3845 usage_tag(void)
3847 fprintf(stderr,
3848 "usage: %s tag [-c commit] [-r repository] [-l] "
3849 "[-m message] name\n", getprogname());
3850 exit(1);
3853 #if 0
3854 static const struct got_error *
3855 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3857 const struct got_error *err = NULL;
3858 struct got_reflist_entry *re, *se, *new;
3859 struct got_object_id *re_id, *se_id;
3860 struct got_tag_object *re_tag, *se_tag;
3861 time_t re_time, se_time;
3863 SIMPLEQ_FOREACH(re, tags, entry) {
3864 se = SIMPLEQ_FIRST(sorted);
3865 if (se == NULL) {
3866 err = got_reflist_entry_dup(&new, re);
3867 if (err)
3868 return err;
3869 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3870 continue;
3871 } else {
3872 err = got_ref_resolve(&re_id, repo, re->ref);
3873 if (err)
3874 break;
3875 err = got_object_open_as_tag(&re_tag, repo, re_id);
3876 free(re_id);
3877 if (err)
3878 break;
3879 re_time = got_object_tag_get_tagger_time(re_tag);
3880 got_object_tag_close(re_tag);
3883 while (se) {
3884 err = got_ref_resolve(&se_id, repo, re->ref);
3885 if (err)
3886 break;
3887 err = got_object_open_as_tag(&se_tag, repo, se_id);
3888 free(se_id);
3889 if (err)
3890 break;
3891 se_time = got_object_tag_get_tagger_time(se_tag);
3892 got_object_tag_close(se_tag);
3894 if (se_time > re_time) {
3895 err = got_reflist_entry_dup(&new, re);
3896 if (err)
3897 return err;
3898 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3899 break;
3901 se = SIMPLEQ_NEXT(se, entry);
3902 continue;
3905 done:
3906 return err;
3908 #endif
3910 static const struct got_error *
3911 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3913 static const struct got_error *err = NULL;
3914 struct got_reflist_head refs;
3915 struct got_reflist_entry *re;
3917 SIMPLEQ_INIT(&refs);
3919 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3920 if (err)
3921 return err;
3923 SIMPLEQ_FOREACH(re, &refs, entry) {
3924 const char *refname;
3925 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3926 char datebuf[26];
3927 const char *tagger;
3928 time_t tagger_time;
3929 struct got_object_id *id;
3930 struct got_tag_object *tag;
3931 struct got_commit_object *commit = NULL;
3933 refname = got_ref_get_name(re->ref);
3934 if (strncmp(refname, "refs/tags/", 10) != 0)
3935 continue;
3936 refname += 10;
3937 refstr = got_ref_to_str(re->ref);
3938 if (refstr == NULL) {
3939 err = got_error_from_errno("got_ref_to_str");
3940 break;
3942 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3943 free(refstr);
3945 err = got_ref_resolve(&id, repo, re->ref);
3946 if (err)
3947 break;
3948 err = got_object_open_as_tag(&tag, repo, id);
3949 if (err) {
3950 if (err->code != GOT_ERR_OBJ_TYPE) {
3951 free(id);
3952 break;
3954 /* "lightweight" tag */
3955 err = got_object_open_as_commit(&commit, repo, id);
3956 if (err) {
3957 free(id);
3958 break;
3960 tagger = got_object_commit_get_committer(commit);
3961 tagger_time =
3962 got_object_commit_get_committer_time(commit);
3963 err = got_object_id_str(&id_str, id);
3964 free(id);
3965 if (err)
3966 break;
3967 } else {
3968 free(id);
3969 tagger = got_object_tag_get_tagger(tag);
3970 tagger_time = got_object_tag_get_tagger_time(tag);
3971 err = got_object_id_str(&id_str,
3972 got_object_tag_get_object_id(tag));
3973 if (err)
3974 break;
3976 printf("from: %s\n", tagger);
3977 datestr = get_datestr(&tagger_time, datebuf);
3978 if (datestr)
3979 printf("date: %s UTC\n", datestr);
3980 if (commit)
3981 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3982 else {
3983 switch (got_object_tag_get_object_type(tag)) {
3984 case GOT_OBJ_TYPE_BLOB:
3985 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3986 id_str);
3987 break;
3988 case GOT_OBJ_TYPE_TREE:
3989 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3990 id_str);
3991 break;
3992 case GOT_OBJ_TYPE_COMMIT:
3993 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3994 id_str);
3995 break;
3996 case GOT_OBJ_TYPE_TAG:
3997 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3998 id_str);
3999 break;
4000 default:
4001 break;
4004 free(id_str);
4005 if (commit) {
4006 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4007 if (err)
4008 break;
4009 got_object_commit_close(commit);
4010 } else {
4011 tagmsg0 = strdup(got_object_tag_get_message(tag));
4012 got_object_tag_close(tag);
4013 if (tagmsg0 == NULL) {
4014 err = got_error_from_errno("strdup");
4015 break;
4019 tagmsg = tagmsg0;
4020 do {
4021 line = strsep(&tagmsg, "\n");
4022 if (line)
4023 printf(" %s\n", line);
4024 } while (line);
4025 free(tagmsg0);
4028 got_ref_list_free(&refs);
4029 return NULL;
4032 static const struct got_error *
4033 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4034 const char *tag_name, const char *repo_path)
4036 const struct got_error *err = NULL;
4037 char *template = NULL, *initial_content = NULL;
4038 char *editor = NULL;
4039 int fd = -1;
4041 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4042 err = got_error_from_errno("asprintf");
4043 goto done;
4046 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4047 commit_id_str, tag_name) == -1) {
4048 err = got_error_from_errno("asprintf");
4049 goto done;
4052 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4053 if (err)
4054 goto done;
4056 dprintf(fd, initial_content);
4057 close(fd);
4059 err = get_editor(&editor);
4060 if (err)
4061 goto done;
4062 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4063 done:
4064 free(initial_content);
4065 free(template);
4066 free(editor);
4068 /* Editor is done; we can now apply unveil(2) */
4069 if (err == NULL) {
4070 err = apply_unveil(repo_path, 0, NULL);
4071 if (err) {
4072 free(*tagmsg);
4073 *tagmsg = NULL;
4076 return err;
4079 static const struct got_error *
4080 add_tag(struct got_repository *repo, const char *tag_name,
4081 const char *commit_arg, const char *tagmsg_arg)
4083 const struct got_error *err = NULL;
4084 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4085 char *label = NULL, *commit_id_str = NULL;
4086 struct got_reference *ref = NULL;
4087 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4088 char *tagmsg_path = NULL, *tag_id_str = NULL;
4089 int preserve_tagmsg = 0;
4092 * Don't let the user create a tag name with a leading '-'.
4093 * While technically a valid reference name, this case is usually
4094 * an unintended typo.
4096 if (tag_name[0] == '-')
4097 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4099 err = get_author(&tagger, repo);
4100 if (err)
4101 return err;
4103 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4104 GOT_OBJ_TYPE_COMMIT, 1, repo);
4105 if (err)
4106 goto done;
4108 err = got_object_id_str(&commit_id_str, commit_id);
4109 if (err)
4110 goto done;
4112 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4113 refname = strdup(tag_name);
4114 if (refname == NULL) {
4115 err = got_error_from_errno("strdup");
4116 goto done;
4118 tag_name += 10;
4119 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4120 err = got_error_from_errno("asprintf");
4121 goto done;
4124 err = got_ref_open(&ref, repo, refname, 0);
4125 if (err == NULL) {
4126 err = got_error(GOT_ERR_TAG_EXISTS);
4127 goto done;
4128 } else if (err->code != GOT_ERR_NOT_REF)
4129 goto done;
4131 if (tagmsg_arg == NULL) {
4132 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4133 tag_name, got_repo_get_path(repo));
4134 if (err) {
4135 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4136 tagmsg_path != NULL)
4137 preserve_tagmsg = 1;
4138 goto done;
4142 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4143 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4144 if (err) {
4145 if (tagmsg_path)
4146 preserve_tagmsg = 1;
4147 goto done;
4150 err = got_ref_alloc(&ref, refname, tag_id);
4151 if (err) {
4152 if (tagmsg_path)
4153 preserve_tagmsg = 1;
4154 goto done;
4157 err = got_ref_write(ref, repo);
4158 if (err) {
4159 if (tagmsg_path)
4160 preserve_tagmsg = 1;
4161 goto done;
4164 err = got_object_id_str(&tag_id_str, tag_id);
4165 if (err) {
4166 if (tagmsg_path)
4167 preserve_tagmsg = 1;
4168 goto done;
4170 printf("Created tag %s\n", tag_id_str);
4171 done:
4172 if (preserve_tagmsg) {
4173 fprintf(stderr, "%s: tag message preserved in %s\n",
4174 getprogname(), tagmsg_path);
4175 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4176 err = got_error_from_errno2("unlink", tagmsg_path);
4177 free(tag_id_str);
4178 if (ref)
4179 got_ref_close(ref);
4180 free(commit_id);
4181 free(commit_id_str);
4182 free(refname);
4183 free(tagmsg);
4184 free(tagmsg_path);
4185 free(tagger);
4186 return err;
4189 static const struct got_error *
4190 cmd_tag(int argc, char *argv[])
4192 const struct got_error *error = NULL;
4193 struct got_repository *repo = NULL;
4194 struct got_worktree *worktree = NULL;
4195 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4196 char *gitconfig_path = NULL;
4197 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4198 int ch, do_list = 0;
4200 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4201 switch (ch) {
4202 case 'c':
4203 commit_id_arg = optarg;
4204 break;
4205 case 'm':
4206 tagmsg = optarg;
4207 break;
4208 case 'r':
4209 repo_path = realpath(optarg, NULL);
4210 if (repo_path == NULL)
4211 return got_error_from_errno2("realpath",
4212 optarg);
4213 got_path_strip_trailing_slashes(repo_path);
4214 break;
4215 case 'l':
4216 do_list = 1;
4217 break;
4218 default:
4219 usage_tag();
4220 /* NOTREACHED */
4224 argc -= optind;
4225 argv += optind;
4227 if (do_list) {
4228 if (commit_id_arg != NULL)
4229 errx(1, "-c option can only be used when creating a tag");
4230 if (tagmsg)
4231 errx(1, "-l and -m options are mutually exclusive");
4232 if (argc > 0)
4233 usage_tag();
4234 } else if (argc != 1)
4235 usage_tag();
4237 tag_name = argv[0];
4239 #ifndef PROFILE
4240 if (do_list) {
4241 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4242 NULL) == -1)
4243 err(1, "pledge");
4244 } else {
4245 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4246 "sendfd unveil", NULL) == -1)
4247 err(1, "pledge");
4249 #endif
4250 cwd = getcwd(NULL, 0);
4251 if (cwd == NULL) {
4252 error = got_error_from_errno("getcwd");
4253 goto done;
4256 if (repo_path == NULL) {
4257 error = got_worktree_open(&worktree, cwd);
4258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4259 goto done;
4260 else
4261 error = NULL;
4262 if (worktree) {
4263 repo_path =
4264 strdup(got_worktree_get_repo_path(worktree));
4265 if (repo_path == NULL)
4266 error = got_error_from_errno("strdup");
4267 if (error)
4268 goto done;
4269 } else {
4270 repo_path = strdup(cwd);
4271 if (repo_path == NULL) {
4272 error = got_error_from_errno("strdup");
4273 goto done;
4278 if (do_list) {
4279 error = got_repo_open(&repo, repo_path, NULL);
4280 if (error != NULL)
4281 goto done;
4282 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4283 if (error)
4284 goto done;
4285 error = list_tags(repo, worktree);
4286 } else {
4287 error = get_gitconfig_path(&gitconfig_path);
4288 if (error)
4289 goto done;
4290 error = got_repo_open(&repo, repo_path, gitconfig_path);
4291 if (error != NULL)
4292 goto done;
4294 if (tagmsg) {
4295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4296 if (error)
4297 goto done;
4300 if (commit_id_arg == NULL) {
4301 struct got_reference *head_ref;
4302 struct got_object_id *commit_id;
4303 error = got_ref_open(&head_ref, repo,
4304 worktree ? got_worktree_get_head_ref_name(worktree)
4305 : GOT_REF_HEAD, 0);
4306 if (error)
4307 goto done;
4308 error = got_ref_resolve(&commit_id, repo, head_ref);
4309 got_ref_close(head_ref);
4310 if (error)
4311 goto done;
4312 error = got_object_id_str(&commit_id_str, commit_id);
4313 free(commit_id);
4314 if (error)
4315 goto done;
4318 error = add_tag(repo, tag_name,
4319 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4321 done:
4322 if (repo)
4323 got_repo_close(repo);
4324 if (worktree)
4325 got_worktree_close(worktree);
4326 free(cwd);
4327 free(repo_path);
4328 free(gitconfig_path);
4329 free(commit_id_str);
4330 return error;
4333 __dead static void
4334 usage_add(void)
4336 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4337 getprogname());
4338 exit(1);
4341 static const struct got_error *
4342 add_progress(void *arg, unsigned char status, const char *path)
4344 while (path[0] == '/')
4345 path++;
4346 printf("%c %s\n", status, path);
4347 return NULL;
4350 static const struct got_error *
4351 cmd_add(int argc, char *argv[])
4353 const struct got_error *error = NULL;
4354 struct got_repository *repo = NULL;
4355 struct got_worktree *worktree = NULL;
4356 char *cwd = NULL;
4357 struct got_pathlist_head paths;
4358 struct got_pathlist_entry *pe;
4359 int ch, can_recurse = 0, no_ignores = 0;
4361 TAILQ_INIT(&paths);
4363 while ((ch = getopt(argc, argv, "IR")) != -1) {
4364 switch (ch) {
4365 case 'I':
4366 no_ignores = 1;
4367 break;
4368 case 'R':
4369 can_recurse = 1;
4370 break;
4371 default:
4372 usage_add();
4373 /* NOTREACHED */
4377 argc -= optind;
4378 argv += optind;
4380 #ifndef PROFILE
4381 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4382 NULL) == -1)
4383 err(1, "pledge");
4384 #endif
4385 if (argc < 1)
4386 usage_add();
4388 cwd = getcwd(NULL, 0);
4389 if (cwd == NULL) {
4390 error = got_error_from_errno("getcwd");
4391 goto done;
4394 error = got_worktree_open(&worktree, cwd);
4395 if (error)
4396 goto done;
4398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4399 NULL);
4400 if (error != NULL)
4401 goto done;
4403 error = apply_unveil(got_repo_get_path(repo), 1,
4404 got_worktree_get_root_path(worktree));
4405 if (error)
4406 goto done;
4408 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4409 if (error)
4410 goto done;
4412 if (!can_recurse && no_ignores) {
4413 error = got_error_msg(GOT_ERR_BAD_PATH,
4414 "disregarding ignores requires -R option");
4415 goto done;
4419 if (!can_recurse) {
4420 char *ondisk_path;
4421 struct stat sb;
4422 TAILQ_FOREACH(pe, &paths, entry) {
4423 if (asprintf(&ondisk_path, "%s/%s",
4424 got_worktree_get_root_path(worktree),
4425 pe->path) == -1) {
4426 error = got_error_from_errno("asprintf");
4427 goto done;
4429 if (lstat(ondisk_path, &sb) == -1) {
4430 if (errno == ENOENT) {
4431 free(ondisk_path);
4432 continue;
4434 error = got_error_from_errno2("lstat",
4435 ondisk_path);
4436 free(ondisk_path);
4437 goto done;
4439 free(ondisk_path);
4440 if (S_ISDIR(sb.st_mode)) {
4441 error = got_error_msg(GOT_ERR_BAD_PATH,
4442 "adding directories requires -R option");
4443 goto done;
4448 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4449 NULL, repo, no_ignores);
4450 done:
4451 if (repo)
4452 got_repo_close(repo);
4453 if (worktree)
4454 got_worktree_close(worktree);
4455 TAILQ_FOREACH(pe, &paths, entry)
4456 free((char *)pe->path);
4457 got_pathlist_free(&paths);
4458 free(cwd);
4459 return error;
4462 __dead static void
4463 usage_remove(void)
4465 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4466 getprogname());
4467 exit(1);
4470 static const struct got_error *
4471 print_remove_status(void *arg, unsigned char status,
4472 unsigned char staged_status, const char *path)
4474 while (path[0] == '/')
4475 path++;
4476 if (status == GOT_STATUS_NONEXISTENT)
4477 return NULL;
4478 if (status == staged_status && (status == GOT_STATUS_DELETE))
4479 status = GOT_STATUS_NO_CHANGE;
4480 printf("%c%c %s\n", status, staged_status, path);
4481 return NULL;
4484 static const struct got_error *
4485 cmd_remove(int argc, char *argv[])
4487 const struct got_error *error = NULL;
4488 struct got_worktree *worktree = NULL;
4489 struct got_repository *repo = NULL;
4490 char *cwd = NULL;
4491 struct got_pathlist_head paths;
4492 struct got_pathlist_entry *pe;
4493 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4495 TAILQ_INIT(&paths);
4497 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4498 switch (ch) {
4499 case 'f':
4500 delete_local_mods = 1;
4501 break;
4502 case 'k':
4503 keep_on_disk = 1;
4504 break;
4505 case 'R':
4506 can_recurse = 1;
4507 break;
4508 default:
4509 usage_remove();
4510 /* NOTREACHED */
4514 argc -= optind;
4515 argv += optind;
4517 #ifndef PROFILE
4518 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4519 NULL) == -1)
4520 err(1, "pledge");
4521 #endif
4522 if (argc < 1)
4523 usage_remove();
4525 cwd = getcwd(NULL, 0);
4526 if (cwd == NULL) {
4527 error = got_error_from_errno("getcwd");
4528 goto done;
4530 error = got_worktree_open(&worktree, cwd);
4531 if (error)
4532 goto done;
4534 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4535 NULL);
4536 if (error)
4537 goto done;
4539 error = apply_unveil(got_repo_get_path(repo), 1,
4540 got_worktree_get_root_path(worktree));
4541 if (error)
4542 goto done;
4544 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4545 if (error)
4546 goto done;
4548 if (!can_recurse) {
4549 char *ondisk_path;
4550 struct stat sb;
4551 TAILQ_FOREACH(pe, &paths, entry) {
4552 if (asprintf(&ondisk_path, "%s/%s",
4553 got_worktree_get_root_path(worktree),
4554 pe->path) == -1) {
4555 error = got_error_from_errno("asprintf");
4556 goto done;
4558 if (lstat(ondisk_path, &sb) == -1) {
4559 if (errno == ENOENT) {
4560 free(ondisk_path);
4561 continue;
4563 error = got_error_from_errno2("lstat",
4564 ondisk_path);
4565 free(ondisk_path);
4566 goto done;
4568 free(ondisk_path);
4569 if (S_ISDIR(sb.st_mode)) {
4570 error = got_error_msg(GOT_ERR_BAD_PATH,
4571 "removing directories requires -R option");
4572 goto done;
4577 error = got_worktree_schedule_delete(worktree, &paths,
4578 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4579 done:
4580 if (repo)
4581 got_repo_close(repo);
4582 if (worktree)
4583 got_worktree_close(worktree);
4584 TAILQ_FOREACH(pe, &paths, entry)
4585 free((char *)pe->path);
4586 got_pathlist_free(&paths);
4587 free(cwd);
4588 return error;
4591 __dead static void
4592 usage_revert(void)
4594 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4595 "path ...\n", getprogname());
4596 exit(1);
4599 static const struct got_error *
4600 revert_progress(void *arg, unsigned char status, const char *path)
4602 if (status == GOT_STATUS_UNVERSIONED)
4603 return NULL;
4605 while (path[0] == '/')
4606 path++;
4607 printf("%c %s\n", status, path);
4608 return NULL;
4611 struct choose_patch_arg {
4612 FILE *patch_script_file;
4613 const char *action;
4616 static const struct got_error *
4617 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4618 int nchanges, const char *action)
4620 char *line = NULL;
4621 size_t linesize = 0;
4622 ssize_t linelen;
4624 switch (status) {
4625 case GOT_STATUS_ADD:
4626 printf("A %s\n%s this addition? [y/n] ", path, action);
4627 break;
4628 case GOT_STATUS_DELETE:
4629 printf("D %s\n%s this deletion? [y/n] ", path, action);
4630 break;
4631 case GOT_STATUS_MODIFY:
4632 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4633 return got_error_from_errno("fseek");
4634 printf(GOT_COMMIT_SEP_STR);
4635 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4636 printf("%s", line);
4637 if (ferror(patch_file))
4638 return got_error_from_errno("getline");
4639 printf(GOT_COMMIT_SEP_STR);
4640 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4641 path, n, nchanges, action);
4642 break;
4643 default:
4644 return got_error_path(path, GOT_ERR_FILE_STATUS);
4647 return NULL;
4650 static const struct got_error *
4651 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4652 FILE *patch_file, int n, int nchanges)
4654 const struct got_error *err = NULL;
4655 char *line = NULL;
4656 size_t linesize = 0;
4657 ssize_t linelen;
4658 int resp = ' ';
4659 struct choose_patch_arg *a = arg;
4661 *choice = GOT_PATCH_CHOICE_NONE;
4663 if (a->patch_script_file) {
4664 char *nl;
4665 err = show_change(status, path, patch_file, n, nchanges,
4666 a->action);
4667 if (err)
4668 return err;
4669 linelen = getline(&line, &linesize, a->patch_script_file);
4670 if (linelen == -1) {
4671 if (ferror(a->patch_script_file))
4672 return got_error_from_errno("getline");
4673 return NULL;
4675 nl = strchr(line, '\n');
4676 if (nl)
4677 *nl = '\0';
4678 if (strcmp(line, "y") == 0) {
4679 *choice = GOT_PATCH_CHOICE_YES;
4680 printf("y\n");
4681 } else if (strcmp(line, "n") == 0) {
4682 *choice = GOT_PATCH_CHOICE_NO;
4683 printf("n\n");
4684 } else if (strcmp(line, "q") == 0 &&
4685 status == GOT_STATUS_MODIFY) {
4686 *choice = GOT_PATCH_CHOICE_QUIT;
4687 printf("q\n");
4688 } else
4689 printf("invalid response '%s'\n", line);
4690 free(line);
4691 return NULL;
4694 while (resp != 'y' && resp != 'n' && resp != 'q') {
4695 err = show_change(status, path, patch_file, n, nchanges,
4696 a->action);
4697 if (err)
4698 return err;
4699 resp = getchar();
4700 if (resp == '\n')
4701 resp = getchar();
4702 if (status == GOT_STATUS_MODIFY) {
4703 if (resp != 'y' && resp != 'n' && resp != 'q') {
4704 printf("invalid response '%c'\n", resp);
4705 resp = ' ';
4707 } else if (resp != 'y' && resp != 'n') {
4708 printf("invalid response '%c'\n", resp);
4709 resp = ' ';
4713 if (resp == 'y')
4714 *choice = GOT_PATCH_CHOICE_YES;
4715 else if (resp == 'n')
4716 *choice = GOT_PATCH_CHOICE_NO;
4717 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4718 *choice = GOT_PATCH_CHOICE_QUIT;
4720 return NULL;
4724 static const struct got_error *
4725 cmd_revert(int argc, char *argv[])
4727 const struct got_error *error = NULL;
4728 struct got_worktree *worktree = NULL;
4729 struct got_repository *repo = NULL;
4730 char *cwd = NULL, *path = NULL;
4731 struct got_pathlist_head paths;
4732 struct got_pathlist_entry *pe;
4733 int ch, can_recurse = 0, pflag = 0;
4734 FILE *patch_script_file = NULL;
4735 const char *patch_script_path = NULL;
4736 struct choose_patch_arg cpa;
4738 TAILQ_INIT(&paths);
4740 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4741 switch (ch) {
4742 case 'p':
4743 pflag = 1;
4744 break;
4745 case 'F':
4746 patch_script_path = optarg;
4747 break;
4748 case 'R':
4749 can_recurse = 1;
4750 break;
4751 default:
4752 usage_revert();
4753 /* NOTREACHED */
4757 argc -= optind;
4758 argv += optind;
4760 #ifndef PROFILE
4761 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4762 "unveil", NULL) == -1)
4763 err(1, "pledge");
4764 #endif
4765 if (argc < 1)
4766 usage_revert();
4767 if (patch_script_path && !pflag)
4768 errx(1, "-F option can only be used together with -p option");
4770 cwd = getcwd(NULL, 0);
4771 if (cwd == NULL) {
4772 error = got_error_from_errno("getcwd");
4773 goto done;
4775 error = got_worktree_open(&worktree, cwd);
4776 if (error)
4777 goto done;
4779 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4780 NULL);
4781 if (error != NULL)
4782 goto done;
4784 if (patch_script_path) {
4785 patch_script_file = fopen(patch_script_path, "r");
4786 if (patch_script_file == NULL) {
4787 error = got_error_from_errno2("fopen",
4788 patch_script_path);
4789 goto done;
4792 error = apply_unveil(got_repo_get_path(repo), 1,
4793 got_worktree_get_root_path(worktree));
4794 if (error)
4795 goto done;
4797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4798 if (error)
4799 goto done;
4801 if (!can_recurse) {
4802 char *ondisk_path;
4803 struct stat sb;
4804 TAILQ_FOREACH(pe, &paths, entry) {
4805 if (asprintf(&ondisk_path, "%s/%s",
4806 got_worktree_get_root_path(worktree),
4807 pe->path) == -1) {
4808 error = got_error_from_errno("asprintf");
4809 goto done;
4811 if (lstat(ondisk_path, &sb) == -1) {
4812 if (errno == ENOENT) {
4813 free(ondisk_path);
4814 continue;
4816 error = got_error_from_errno2("lstat",
4817 ondisk_path);
4818 free(ondisk_path);
4819 goto done;
4821 free(ondisk_path);
4822 if (S_ISDIR(sb.st_mode)) {
4823 error = got_error_msg(GOT_ERR_BAD_PATH,
4824 "reverting directories requires -R option");
4825 goto done;
4830 cpa.patch_script_file = patch_script_file;
4831 cpa.action = "revert";
4832 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4833 pflag ? choose_patch : NULL, &cpa, repo);
4834 done:
4835 if (patch_script_file && fclose(patch_script_file) == EOF &&
4836 error == NULL)
4837 error = got_error_from_errno2("fclose", patch_script_path);
4838 if (repo)
4839 got_repo_close(repo);
4840 if (worktree)
4841 got_worktree_close(worktree);
4842 free(path);
4843 free(cwd);
4844 return error;
4847 __dead static void
4848 usage_commit(void)
4850 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4851 getprogname());
4852 exit(1);
4855 struct collect_commit_logmsg_arg {
4856 const char *cmdline_log;
4857 const char *editor;
4858 const char *worktree_path;
4859 const char *branch_name;
4860 const char *repo_path;
4861 char *logmsg_path;
4865 static const struct got_error *
4866 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4867 void *arg)
4869 char *initial_content = NULL;
4870 struct got_pathlist_entry *pe;
4871 const struct got_error *err = NULL;
4872 char *template = NULL;
4873 struct collect_commit_logmsg_arg *a = arg;
4874 int fd;
4875 size_t len;
4877 /* if a message was specified on the command line, just use it */
4878 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4879 len = strlen(a->cmdline_log) + 1;
4880 *logmsg = malloc(len + 1);
4881 if (*logmsg == NULL)
4882 return got_error_from_errno("malloc");
4883 strlcpy(*logmsg, a->cmdline_log, len);
4884 return NULL;
4887 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4888 return got_error_from_errno("asprintf");
4890 if (asprintf(&initial_content,
4891 "\n# changes to be committed on branch %s:\n",
4892 a->branch_name) == -1)
4893 return got_error_from_errno("asprintf");
4895 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4896 if (err)
4897 goto done;
4899 dprintf(fd, initial_content);
4901 TAILQ_FOREACH(pe, commitable_paths, entry) {
4902 struct got_commitable *ct = pe->data;
4903 dprintf(fd, "# %c %s\n",
4904 got_commitable_get_status(ct),
4905 got_commitable_get_path(ct));
4907 close(fd);
4909 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4910 done:
4911 free(initial_content);
4912 free(template);
4914 /* Editor is done; we can now apply unveil(2) */
4915 if (err == NULL) {
4916 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4917 if (err) {
4918 free(*logmsg);
4919 *logmsg = NULL;
4922 return err;
4925 static const struct got_error *
4926 cmd_commit(int argc, char *argv[])
4928 const struct got_error *error = NULL;
4929 struct got_worktree *worktree = NULL;
4930 struct got_repository *repo = NULL;
4931 char *cwd = NULL, *id_str = NULL;
4932 struct got_object_id *id = NULL;
4933 const char *logmsg = NULL;
4934 struct collect_commit_logmsg_arg cl_arg;
4935 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4936 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4937 struct got_pathlist_head paths;
4939 TAILQ_INIT(&paths);
4940 cl_arg.logmsg_path = NULL;
4942 while ((ch = getopt(argc, argv, "m:")) != -1) {
4943 switch (ch) {
4944 case 'm':
4945 logmsg = optarg;
4946 break;
4947 default:
4948 usage_commit();
4949 /* NOTREACHED */
4953 argc -= optind;
4954 argv += optind;
4956 #ifndef PROFILE
4957 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4958 "unveil", NULL) == -1)
4959 err(1, "pledge");
4960 #endif
4961 cwd = getcwd(NULL, 0);
4962 if (cwd == NULL) {
4963 error = got_error_from_errno("getcwd");
4964 goto done;
4966 error = got_worktree_open(&worktree, cwd);
4967 if (error)
4968 goto done;
4970 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4971 if (error)
4972 goto done;
4973 if (rebase_in_progress) {
4974 error = got_error(GOT_ERR_REBASING);
4975 goto done;
4978 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4979 worktree);
4980 if (error)
4981 goto done;
4983 error = get_gitconfig_path(&gitconfig_path);
4984 if (error)
4985 goto done;
4986 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4987 gitconfig_path);
4988 if (error != NULL)
4989 goto done;
4991 error = get_author(&author, repo);
4992 if (error)
4993 return error;
4996 * unveil(2) traverses exec(2); if an editor is used we have
4997 * to apply unveil after the log message has been written.
4999 if (logmsg == NULL || strlen(logmsg) == 0)
5000 error = get_editor(&editor);
5001 else
5002 error = apply_unveil(got_repo_get_path(repo), 0,
5003 got_worktree_get_root_path(worktree));
5004 if (error)
5005 goto done;
5007 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5008 if (error)
5009 goto done;
5011 cl_arg.editor = editor;
5012 cl_arg.cmdline_log = logmsg;
5013 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5014 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5015 if (!histedit_in_progress) {
5016 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5017 error = got_error(GOT_ERR_COMMIT_BRANCH);
5018 goto done;
5020 cl_arg.branch_name += 11;
5022 cl_arg.repo_path = got_repo_get_path(repo);
5023 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5024 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5025 if (error) {
5026 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5027 cl_arg.logmsg_path != NULL)
5028 preserve_logmsg = 1;
5029 goto done;
5032 error = got_object_id_str(&id_str, id);
5033 if (error)
5034 goto done;
5035 printf("Created commit %s\n", id_str);
5036 done:
5037 if (preserve_logmsg) {
5038 fprintf(stderr, "%s: log message preserved in %s\n",
5039 getprogname(), cl_arg.logmsg_path);
5040 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5041 error == NULL)
5042 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5043 free(cl_arg.logmsg_path);
5044 if (repo)
5045 got_repo_close(repo);
5046 if (worktree)
5047 got_worktree_close(worktree);
5048 free(cwd);
5049 free(id_str);
5050 free(gitconfig_path);
5051 free(editor);
5052 free(author);
5053 return error;
5056 __dead static void
5057 usage_cherrypick(void)
5059 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5060 exit(1);
5063 static const struct got_error *
5064 cmd_cherrypick(int argc, char *argv[])
5066 const struct got_error *error = NULL;
5067 struct got_worktree *worktree = NULL;
5068 struct got_repository *repo = NULL;
5069 char *cwd = NULL, *commit_id_str = NULL;
5070 struct got_object_id *commit_id = NULL;
5071 struct got_commit_object *commit = NULL;
5072 struct got_object_qid *pid;
5073 struct got_reference *head_ref = NULL;
5074 int ch, did_something = 0;
5076 while ((ch = getopt(argc, argv, "")) != -1) {
5077 switch (ch) {
5078 default:
5079 usage_cherrypick();
5080 /* NOTREACHED */
5084 argc -= optind;
5085 argv += optind;
5087 #ifndef PROFILE
5088 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5089 "unveil", NULL) == -1)
5090 err(1, "pledge");
5091 #endif
5092 if (argc != 1)
5093 usage_cherrypick();
5095 cwd = getcwd(NULL, 0);
5096 if (cwd == NULL) {
5097 error = got_error_from_errno("getcwd");
5098 goto done;
5100 error = got_worktree_open(&worktree, cwd);
5101 if (error)
5102 goto done;
5104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5105 NULL);
5106 if (error != NULL)
5107 goto done;
5109 error = apply_unveil(got_repo_get_path(repo), 0,
5110 got_worktree_get_root_path(worktree));
5111 if (error)
5112 goto done;
5114 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5115 GOT_OBJ_TYPE_COMMIT, repo);
5116 if (error != NULL) {
5117 struct got_reference *ref;
5118 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5119 goto done;
5120 error = got_ref_open(&ref, repo, argv[0], 0);
5121 if (error != NULL)
5122 goto done;
5123 error = got_ref_resolve(&commit_id, repo, ref);
5124 got_ref_close(ref);
5125 if (error != NULL)
5126 goto done;
5128 error = got_object_id_str(&commit_id_str, commit_id);
5129 if (error)
5130 goto done;
5132 error = got_ref_open(&head_ref, repo,
5133 got_worktree_get_head_ref_name(worktree), 0);
5134 if (error != NULL)
5135 goto done;
5137 error = check_same_branch(commit_id, head_ref, NULL, repo);
5138 if (error) {
5139 if (error->code != GOT_ERR_ANCESTRY)
5140 goto done;
5141 error = NULL;
5142 } else {
5143 error = got_error(GOT_ERR_SAME_BRANCH);
5144 goto done;
5147 error = got_object_open_as_commit(&commit, repo, commit_id);
5148 if (error)
5149 goto done;
5150 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5151 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5152 commit_id, repo, update_progress, &did_something, check_cancelled,
5153 NULL);
5154 if (error != NULL)
5155 goto done;
5157 if (did_something)
5158 printf("Merged commit %s\n", commit_id_str);
5159 done:
5160 if (commit)
5161 got_object_commit_close(commit);
5162 free(commit_id_str);
5163 if (head_ref)
5164 got_ref_close(head_ref);
5165 if (worktree)
5166 got_worktree_close(worktree);
5167 if (repo)
5168 got_repo_close(repo);
5169 return error;
5172 __dead static void
5173 usage_backout(void)
5175 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5176 exit(1);
5179 static const struct got_error *
5180 cmd_backout(int argc, char *argv[])
5182 const struct got_error *error = NULL;
5183 struct got_worktree *worktree = NULL;
5184 struct got_repository *repo = NULL;
5185 char *cwd = NULL, *commit_id_str = NULL;
5186 struct got_object_id *commit_id = NULL;
5187 struct got_commit_object *commit = NULL;
5188 struct got_object_qid *pid;
5189 struct got_reference *head_ref = NULL;
5190 int ch, did_something = 0;
5192 while ((ch = getopt(argc, argv, "")) != -1) {
5193 switch (ch) {
5194 default:
5195 usage_backout();
5196 /* NOTREACHED */
5200 argc -= optind;
5201 argv += optind;
5203 #ifndef PROFILE
5204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5205 "unveil", NULL) == -1)
5206 err(1, "pledge");
5207 #endif
5208 if (argc != 1)
5209 usage_backout();
5211 cwd = getcwd(NULL, 0);
5212 if (cwd == NULL) {
5213 error = got_error_from_errno("getcwd");
5214 goto done;
5216 error = got_worktree_open(&worktree, cwd);
5217 if (error)
5218 goto done;
5220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5221 NULL);
5222 if (error != NULL)
5223 goto done;
5225 error = apply_unveil(got_repo_get_path(repo), 0,
5226 got_worktree_get_root_path(worktree));
5227 if (error)
5228 goto done;
5230 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5231 GOT_OBJ_TYPE_COMMIT, repo);
5232 if (error != NULL) {
5233 struct got_reference *ref;
5234 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5235 goto done;
5236 error = got_ref_open(&ref, repo, argv[0], 0);
5237 if (error != NULL)
5238 goto done;
5239 error = got_ref_resolve(&commit_id, repo, ref);
5240 got_ref_close(ref);
5241 if (error != NULL)
5242 goto done;
5244 error = got_object_id_str(&commit_id_str, commit_id);
5245 if (error)
5246 goto done;
5248 error = got_ref_open(&head_ref, repo,
5249 got_worktree_get_head_ref_name(worktree), 0);
5250 if (error != NULL)
5251 goto done;
5253 error = check_same_branch(commit_id, head_ref, NULL, repo);
5254 if (error)
5255 goto done;
5257 error = got_object_open_as_commit(&commit, repo, commit_id);
5258 if (error)
5259 goto done;
5260 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5261 if (pid == NULL) {
5262 error = got_error(GOT_ERR_ROOT_COMMIT);
5263 goto done;
5266 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5267 update_progress, &did_something, check_cancelled, NULL);
5268 if (error != NULL)
5269 goto done;
5271 if (did_something)
5272 printf("Backed out commit %s\n", commit_id_str);
5273 done:
5274 if (commit)
5275 got_object_commit_close(commit);
5276 free(commit_id_str);
5277 if (head_ref)
5278 got_ref_close(head_ref);
5279 if (worktree)
5280 got_worktree_close(worktree);
5281 if (repo)
5282 got_repo_close(repo);
5283 return error;
5286 __dead static void
5287 usage_rebase(void)
5289 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5290 getprogname());
5291 exit(1);
5294 void
5295 trim_logmsg(char *logmsg, int limit)
5297 char *nl;
5298 size_t len;
5300 len = strlen(logmsg);
5301 if (len > limit)
5302 len = limit;
5303 logmsg[len] = '\0';
5304 nl = strchr(logmsg, '\n');
5305 if (nl)
5306 *nl = '\0';
5309 static const struct got_error *
5310 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5312 const struct got_error *err;
5313 char *logmsg0 = NULL;
5314 const char *s;
5316 err = got_object_commit_get_logmsg(&logmsg0, commit);
5317 if (err)
5318 return err;
5320 s = logmsg0;
5321 while (isspace((unsigned char)s[0]))
5322 s++;
5324 *logmsg = strdup(s);
5325 if (*logmsg == NULL) {
5326 err = got_error_from_errno("strdup");
5327 goto done;
5330 trim_logmsg(*logmsg, limit);
5331 done:
5332 free(logmsg0);
5333 return err;
5336 static const struct got_error *
5337 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5339 const struct got_error *err;
5340 struct got_commit_object *commit = NULL;
5341 char *id_str = NULL, *logmsg = NULL;
5343 err = got_object_open_as_commit(&commit, repo, id);
5344 if (err)
5345 return err;
5347 err = got_object_id_str(&id_str, id);
5348 if (err)
5349 goto done;
5351 id_str[12] = '\0';
5353 err = get_short_logmsg(&logmsg, 42, commit);
5354 if (err)
5355 goto done;
5357 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5358 done:
5359 free(id_str);
5360 got_object_commit_close(commit);
5361 free(logmsg);
5362 return err;
5365 static const struct got_error *
5366 show_rebase_progress(struct got_commit_object *commit,
5367 struct got_object_id *old_id, struct got_object_id *new_id)
5369 const struct got_error *err;
5370 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5372 err = got_object_id_str(&old_id_str, old_id);
5373 if (err)
5374 goto done;
5376 if (new_id) {
5377 err = got_object_id_str(&new_id_str, new_id);
5378 if (err)
5379 goto done;
5382 old_id_str[12] = '\0';
5383 if (new_id_str)
5384 new_id_str[12] = '\0';
5386 err = get_short_logmsg(&logmsg, 42, commit);
5387 if (err)
5388 goto done;
5390 printf("%s -> %s: %s\n", old_id_str,
5391 new_id_str ? new_id_str : "no-op change", logmsg);
5392 done:
5393 free(old_id_str);
5394 free(new_id_str);
5395 free(logmsg);
5396 return err;
5399 static const struct got_error *
5400 rebase_progress(void *arg, unsigned char status, const char *path)
5402 unsigned char *rebase_status = arg;
5404 while (path[0] == '/')
5405 path++;
5406 printf("%c %s\n", status, path);
5408 if (*rebase_status == GOT_STATUS_CONFLICT)
5409 return NULL;
5410 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5411 *rebase_status = status;
5412 return NULL;
5415 static const struct got_error *
5416 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5417 struct got_reference *branch, struct got_reference *new_base_branch,
5418 struct got_reference *tmp_branch, struct got_repository *repo)
5420 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5421 return got_worktree_rebase_complete(worktree, fileindex,
5422 new_base_branch, tmp_branch, branch, repo);
5425 static const struct got_error *
5426 rebase_commit(struct got_pathlist_head *merged_paths,
5427 struct got_worktree *worktree, struct got_fileindex *fileindex,
5428 struct got_reference *tmp_branch,
5429 struct got_object_id *commit_id, struct got_repository *repo)
5431 const struct got_error *error;
5432 struct got_commit_object *commit;
5433 struct got_object_id *new_commit_id;
5435 error = got_object_open_as_commit(&commit, repo, commit_id);
5436 if (error)
5437 return error;
5439 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5440 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5441 if (error) {
5442 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5443 goto done;
5444 error = show_rebase_progress(commit, commit_id, NULL);
5445 } else {
5446 error = show_rebase_progress(commit, commit_id, new_commit_id);
5447 free(new_commit_id);
5449 done:
5450 got_object_commit_close(commit);
5451 return error;
5454 struct check_path_prefix_arg {
5455 const char *path_prefix;
5456 size_t len;
5457 int errcode;
5460 static const struct got_error *
5461 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5462 struct got_blob_object *blob2, struct got_object_id *id1,
5463 struct got_object_id *id2, const char *path1, const char *path2,
5464 mode_t mode1, mode_t mode2, struct got_repository *repo)
5466 struct check_path_prefix_arg *a = arg;
5468 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5469 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5470 return got_error(a->errcode);
5472 return NULL;
5475 static const struct got_error *
5476 check_path_prefix(struct got_object_id *parent_id,
5477 struct got_object_id *commit_id, const char *path_prefix,
5478 int errcode, struct got_repository *repo)
5480 const struct got_error *err;
5481 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5482 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5483 struct check_path_prefix_arg cpp_arg;
5485 if (got_path_is_root_dir(path_prefix))
5486 return NULL;
5488 err = got_object_open_as_commit(&commit, repo, commit_id);
5489 if (err)
5490 goto done;
5492 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5493 if (err)
5494 goto done;
5496 err = got_object_open_as_tree(&tree1, repo,
5497 got_object_commit_get_tree_id(parent_commit));
5498 if (err)
5499 goto done;
5501 err = got_object_open_as_tree(&tree2, repo,
5502 got_object_commit_get_tree_id(commit));
5503 if (err)
5504 goto done;
5506 cpp_arg.path_prefix = path_prefix;
5507 while (cpp_arg.path_prefix[0] == '/')
5508 cpp_arg.path_prefix++;
5509 cpp_arg.len = strlen(cpp_arg.path_prefix);
5510 cpp_arg.errcode = errcode;
5511 err = got_diff_tree(tree1, tree2, "", "", repo,
5512 check_path_prefix_in_diff, &cpp_arg, 0);
5513 done:
5514 if (tree1)
5515 got_object_tree_close(tree1);
5516 if (tree2)
5517 got_object_tree_close(tree2);
5518 if (commit)
5519 got_object_commit_close(commit);
5520 if (parent_commit)
5521 got_object_commit_close(parent_commit);
5522 return err;
5525 static const struct got_error *
5526 collect_commits(struct got_object_id_queue *commits,
5527 struct got_object_id *initial_commit_id,
5528 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5529 const char *path_prefix, int path_prefix_errcode,
5530 struct got_repository *repo)
5532 const struct got_error *err = NULL;
5533 struct got_commit_graph *graph = NULL;
5534 struct got_object_id *parent_id = NULL;
5535 struct got_object_qid *qid;
5536 struct got_object_id *commit_id = initial_commit_id;
5538 err = got_commit_graph_open(&graph, "/", 1);
5539 if (err)
5540 return err;
5542 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5543 check_cancelled, NULL);
5544 if (err)
5545 goto done;
5546 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5547 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5548 check_cancelled, NULL);
5549 if (err) {
5550 if (err->code == GOT_ERR_ITER_COMPLETED) {
5551 err = got_error_msg(GOT_ERR_ANCESTRY,
5552 "ran out of commits to rebase before "
5553 "youngest common ancestor commit has "
5554 "been reached?!?");
5556 goto done;
5557 } else {
5558 err = check_path_prefix(parent_id, commit_id,
5559 path_prefix, path_prefix_errcode, repo);
5560 if (err)
5561 goto done;
5563 err = got_object_qid_alloc(&qid, commit_id);
5564 if (err)
5565 goto done;
5566 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5567 commit_id = parent_id;
5570 done:
5571 got_commit_graph_close(graph);
5572 return err;
5575 static const struct got_error *
5576 cmd_rebase(int argc, char *argv[])
5578 const struct got_error *error = NULL;
5579 struct got_worktree *worktree = NULL;
5580 struct got_repository *repo = NULL;
5581 struct got_fileindex *fileindex = NULL;
5582 char *cwd = NULL;
5583 struct got_reference *branch = NULL;
5584 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5585 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5586 struct got_object_id *resume_commit_id = NULL;
5587 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5588 struct got_commit_object *commit = NULL;
5589 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5590 int histedit_in_progress = 0;
5591 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5592 struct got_object_id_queue commits;
5593 struct got_pathlist_head merged_paths;
5594 const struct got_object_id_queue *parent_ids;
5595 struct got_object_qid *qid, *pid;
5597 SIMPLEQ_INIT(&commits);
5598 TAILQ_INIT(&merged_paths);
5600 while ((ch = getopt(argc, argv, "ac")) != -1) {
5601 switch (ch) {
5602 case 'a':
5603 abort_rebase = 1;
5604 break;
5605 case 'c':
5606 continue_rebase = 1;
5607 break;
5608 default:
5609 usage_rebase();
5610 /* NOTREACHED */
5614 argc -= optind;
5615 argv += optind;
5617 #ifndef PROFILE
5618 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5619 "unveil", NULL) == -1)
5620 err(1, "pledge");
5621 #endif
5622 if (abort_rebase && continue_rebase)
5623 usage_rebase();
5624 else if (abort_rebase || continue_rebase) {
5625 if (argc != 0)
5626 usage_rebase();
5627 } else if (argc != 1)
5628 usage_rebase();
5630 cwd = getcwd(NULL, 0);
5631 if (cwd == NULL) {
5632 error = got_error_from_errno("getcwd");
5633 goto done;
5635 error = got_worktree_open(&worktree, cwd);
5636 if (error)
5637 goto done;
5639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5640 NULL);
5641 if (error != NULL)
5642 goto done;
5644 error = apply_unveil(got_repo_get_path(repo), 0,
5645 got_worktree_get_root_path(worktree));
5646 if (error)
5647 goto done;
5649 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5650 worktree);
5651 if (error)
5652 goto done;
5653 if (histedit_in_progress) {
5654 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5655 goto done;
5658 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5659 if (error)
5660 goto done;
5662 if (abort_rebase) {
5663 int did_something;
5664 if (!rebase_in_progress) {
5665 error = got_error(GOT_ERR_NOT_REBASING);
5666 goto done;
5668 error = got_worktree_rebase_continue(&resume_commit_id,
5669 &new_base_branch, &tmp_branch, &branch, &fileindex,
5670 worktree, repo);
5671 if (error)
5672 goto done;
5673 printf("Switching work tree to %s\n",
5674 got_ref_get_symref_target(new_base_branch));
5675 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5676 new_base_branch, update_progress, &did_something);
5677 if (error)
5678 goto done;
5679 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5680 goto done; /* nothing else to do */
5683 if (continue_rebase) {
5684 if (!rebase_in_progress) {
5685 error = got_error(GOT_ERR_NOT_REBASING);
5686 goto done;
5688 error = got_worktree_rebase_continue(&resume_commit_id,
5689 &new_base_branch, &tmp_branch, &branch, &fileindex,
5690 worktree, repo);
5691 if (error)
5692 goto done;
5694 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5695 resume_commit_id, repo);
5696 if (error)
5697 goto done;
5699 yca_id = got_object_id_dup(resume_commit_id);
5700 if (yca_id == NULL) {
5701 error = got_error_from_errno("got_object_id_dup");
5702 goto done;
5704 } else {
5705 error = got_ref_open(&branch, repo, argv[0], 0);
5706 if (error != NULL)
5707 goto done;
5710 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5711 if (error)
5712 goto done;
5714 if (!continue_rebase) {
5715 struct got_object_id *base_commit_id;
5717 base_commit_id = got_worktree_get_base_commit_id(worktree);
5718 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5719 base_commit_id, branch_head_commit_id, repo,
5720 check_cancelled, NULL);
5721 if (error)
5722 goto done;
5723 if (yca_id == NULL) {
5724 error = got_error_msg(GOT_ERR_ANCESTRY,
5725 "specified branch shares no common ancestry "
5726 "with work tree's branch");
5727 goto done;
5730 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5731 if (error) {
5732 if (error->code != GOT_ERR_ANCESTRY)
5733 goto done;
5734 error = NULL;
5735 } else {
5736 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5737 "specified branch resolves to a commit which "
5738 "is already contained in work tree's branch");
5739 goto done;
5741 error = got_worktree_rebase_prepare(&new_base_branch,
5742 &tmp_branch, &fileindex, worktree, branch, repo);
5743 if (error)
5744 goto done;
5747 commit_id = branch_head_commit_id;
5748 error = got_object_open_as_commit(&commit, repo, commit_id);
5749 if (error)
5750 goto done;
5752 parent_ids = got_object_commit_get_parent_ids(commit);
5753 pid = SIMPLEQ_FIRST(parent_ids);
5754 if (pid == NULL) {
5755 if (!continue_rebase) {
5756 int did_something;
5757 error = got_worktree_rebase_abort(worktree, fileindex,
5758 repo, new_base_branch, update_progress,
5759 &did_something);
5760 if (error)
5761 goto done;
5762 printf("Rebase of %s aborted\n",
5763 got_ref_get_name(branch));
5765 error = got_error(GOT_ERR_EMPTY_REBASE);
5766 goto done;
5768 error = collect_commits(&commits, commit_id, pid->id,
5769 yca_id, got_worktree_get_path_prefix(worktree),
5770 GOT_ERR_REBASE_PATH, repo);
5771 got_object_commit_close(commit);
5772 commit = NULL;
5773 if (error)
5774 goto done;
5776 if (SIMPLEQ_EMPTY(&commits)) {
5777 if (continue_rebase) {
5778 error = rebase_complete(worktree, fileindex,
5779 branch, new_base_branch, tmp_branch, repo);
5780 goto done;
5781 } else {
5782 /* Fast-forward the reference of the branch. */
5783 struct got_object_id *new_head_commit_id;
5784 char *id_str;
5785 error = got_ref_resolve(&new_head_commit_id, repo,
5786 new_base_branch);
5787 if (error)
5788 goto done;
5789 error = got_object_id_str(&id_str, new_head_commit_id);
5790 printf("Forwarding %s to commit %s\n",
5791 got_ref_get_name(branch), id_str);
5792 free(id_str);
5793 error = got_ref_change_ref(branch,
5794 new_head_commit_id);
5795 if (error)
5796 goto done;
5800 pid = NULL;
5801 SIMPLEQ_FOREACH(qid, &commits, entry) {
5802 commit_id = qid->id;
5803 parent_id = pid ? pid->id : yca_id;
5804 pid = qid;
5806 error = got_worktree_rebase_merge_files(&merged_paths,
5807 worktree, fileindex, parent_id, commit_id, repo,
5808 rebase_progress, &rebase_status, check_cancelled, NULL);
5809 if (error)
5810 goto done;
5812 if (rebase_status == GOT_STATUS_CONFLICT) {
5813 error = show_rebase_merge_conflict(qid->id, repo);
5814 if (error)
5815 goto done;
5816 got_worktree_rebase_pathlist_free(&merged_paths);
5817 break;
5820 error = rebase_commit(&merged_paths, worktree, fileindex,
5821 tmp_branch, commit_id, repo);
5822 got_worktree_rebase_pathlist_free(&merged_paths);
5823 if (error)
5824 goto done;
5827 if (rebase_status == GOT_STATUS_CONFLICT) {
5828 error = got_worktree_rebase_postpone(worktree, fileindex);
5829 if (error)
5830 goto done;
5831 error = got_error_msg(GOT_ERR_CONFLICTS,
5832 "conflicts must be resolved before rebasing can continue");
5833 } else
5834 error = rebase_complete(worktree, fileindex, branch,
5835 new_base_branch, tmp_branch, repo);
5836 done:
5837 got_object_id_queue_free(&commits);
5838 free(branch_head_commit_id);
5839 free(resume_commit_id);
5840 free(yca_id);
5841 if (commit)
5842 got_object_commit_close(commit);
5843 if (branch)
5844 got_ref_close(branch);
5845 if (new_base_branch)
5846 got_ref_close(new_base_branch);
5847 if (tmp_branch)
5848 got_ref_close(tmp_branch);
5849 if (worktree)
5850 got_worktree_close(worktree);
5851 if (repo)
5852 got_repo_close(repo);
5853 return error;
5856 __dead static void
5857 usage_histedit(void)
5859 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5860 getprogname());
5861 exit(1);
5864 #define GOT_HISTEDIT_PICK 'p'
5865 #define GOT_HISTEDIT_EDIT 'e'
5866 #define GOT_HISTEDIT_FOLD 'f'
5867 #define GOT_HISTEDIT_DROP 'd'
5868 #define GOT_HISTEDIT_MESG 'm'
5870 static struct got_histedit_cmd {
5871 unsigned char code;
5872 const char *name;
5873 const char *desc;
5874 } got_histedit_cmds[] = {
5875 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5876 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5877 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5878 "be used" },
5879 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5880 { GOT_HISTEDIT_MESG, "mesg",
5881 "single-line log message for commit above (open editor if empty)" },
5884 struct got_histedit_list_entry {
5885 TAILQ_ENTRY(got_histedit_list_entry) entry;
5886 struct got_object_id *commit_id;
5887 const struct got_histedit_cmd *cmd;
5888 char *logmsg;
5890 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5892 static const struct got_error *
5893 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5894 FILE *f, struct got_repository *repo)
5896 const struct got_error *err = NULL;
5897 char *logmsg = NULL, *id_str = NULL;
5898 struct got_commit_object *commit = NULL;
5899 int n;
5901 err = got_object_open_as_commit(&commit, repo, commit_id);
5902 if (err)
5903 goto done;
5905 err = get_short_logmsg(&logmsg, 34, commit);
5906 if (err)
5907 goto done;
5909 err = got_object_id_str(&id_str, commit_id);
5910 if (err)
5911 goto done;
5913 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5914 if (n < 0)
5915 err = got_ferror(f, GOT_ERR_IO);
5916 done:
5917 if (commit)
5918 got_object_commit_close(commit);
5919 free(id_str);
5920 free(logmsg);
5921 return err;
5924 static const struct got_error *
5925 histedit_write_commit_list(struct got_object_id_queue *commits,
5926 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5928 const struct got_error *err = NULL;
5929 struct got_object_qid *qid;
5931 if (SIMPLEQ_EMPTY(commits))
5932 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5934 SIMPLEQ_FOREACH(qid, commits, entry) {
5935 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5936 f, repo);
5937 if (err)
5938 break;
5939 if (edit_logmsg_only) {
5940 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5941 if (n < 0) {
5942 err = got_ferror(f, GOT_ERR_IO);
5943 break;
5948 return err;
5951 static const struct got_error *
5952 write_cmd_list(FILE *f, const char *branch_name,
5953 struct got_object_id_queue *commits)
5955 const struct got_error *err = NULL;
5956 int n, i;
5957 char *id_str;
5958 struct got_object_qid *qid;
5960 qid = SIMPLEQ_FIRST(commits);
5961 err = got_object_id_str(&id_str, qid->id);
5962 if (err)
5963 return err;
5965 n = fprintf(f,
5966 "# Editing the history of branch '%s' starting at\n"
5967 "# commit %s\n"
5968 "# Commits will be processed in order from top to "
5969 "bottom of this file.\n", branch_name, id_str);
5970 if (n < 0) {
5971 err = got_ferror(f, GOT_ERR_IO);
5972 goto done;
5975 n = fprintf(f, "# Available histedit commands:\n");
5976 if (n < 0) {
5977 err = got_ferror(f, GOT_ERR_IO);
5978 goto done;
5981 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5982 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5983 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5984 cmd->desc);
5985 if (n < 0) {
5986 err = got_ferror(f, GOT_ERR_IO);
5987 break;
5990 done:
5991 free(id_str);
5992 return err;
5995 static const struct got_error *
5996 histedit_syntax_error(int lineno)
5998 static char msg[42];
5999 int ret;
6001 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6002 lineno);
6003 if (ret == -1 || ret >= sizeof(msg))
6004 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6006 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6009 static const struct got_error *
6010 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6011 char *logmsg, struct got_repository *repo)
6013 const struct got_error *err;
6014 struct got_commit_object *folded_commit = NULL;
6015 char *id_str, *folded_logmsg = NULL;
6017 err = got_object_id_str(&id_str, hle->commit_id);
6018 if (err)
6019 return err;
6021 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6022 if (err)
6023 goto done;
6025 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6026 if (err)
6027 goto done;
6028 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6029 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6030 folded_logmsg) == -1) {
6031 err = got_error_from_errno("asprintf");
6033 done:
6034 if (folded_commit)
6035 got_object_commit_close(folded_commit);
6036 free(id_str);
6037 free(folded_logmsg);
6038 return err;
6041 static struct got_histedit_list_entry *
6042 get_folded_commits(struct got_histedit_list_entry *hle)
6044 struct got_histedit_list_entry *prev, *folded = NULL;
6046 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6047 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6048 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6049 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6050 folded = prev;
6051 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6054 return folded;
6057 static const struct got_error *
6058 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6059 struct got_repository *repo)
6061 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6062 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6063 const struct got_error *err = NULL;
6064 struct got_commit_object *commit = NULL;
6065 int fd;
6066 struct got_histedit_list_entry *folded = NULL;
6068 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6069 if (err)
6070 return err;
6072 folded = get_folded_commits(hle);
6073 if (folded) {
6074 while (folded != hle) {
6075 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6076 folded = TAILQ_NEXT(folded, entry);
6077 continue;
6079 err = append_folded_commit_msg(&new_msg, folded,
6080 logmsg, repo);
6081 if (err)
6082 goto done;
6083 free(logmsg);
6084 logmsg = new_msg;
6085 folded = TAILQ_NEXT(folded, entry);
6089 err = got_object_id_str(&id_str, hle->commit_id);
6090 if (err)
6091 goto done;
6092 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6093 if (err)
6094 goto done;
6095 if (asprintf(&new_msg,
6096 "%s\n# original log message of commit %s: %s",
6097 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6098 err = got_error_from_errno("asprintf");
6099 goto done;
6101 free(logmsg);
6102 logmsg = new_msg;
6104 err = got_object_id_str(&id_str, hle->commit_id);
6105 if (err)
6106 goto done;
6108 err = got_opentemp_named_fd(&logmsg_path, &fd,
6109 GOT_TMPDIR_STR "/got-logmsg");
6110 if (err)
6111 goto done;
6113 dprintf(fd, logmsg);
6114 close(fd);
6116 err = get_editor(&editor);
6117 if (err)
6118 goto done;
6120 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6121 if (err) {
6122 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6123 goto done;
6124 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6126 done:
6127 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6128 err = got_error_from_errno2("unlink", logmsg_path);
6129 free(logmsg_path);
6130 free(logmsg);
6131 free(orig_logmsg);
6132 free(editor);
6133 if (commit)
6134 got_object_commit_close(commit);
6135 return err;
6138 static const struct got_error *
6139 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6140 FILE *f, struct got_repository *repo)
6142 const struct got_error *err = NULL;
6143 char *line = NULL, *p, *end;
6144 size_t size;
6145 ssize_t len;
6146 int lineno = 0, i;
6147 const struct got_histedit_cmd *cmd;
6148 struct got_object_id *commit_id = NULL;
6149 struct got_histedit_list_entry *hle = NULL;
6151 for (;;) {
6152 len = getline(&line, &size, f);
6153 if (len == -1) {
6154 const struct got_error *getline_err;
6155 if (feof(f))
6156 break;
6157 getline_err = got_error_from_errno("getline");
6158 err = got_ferror(f, getline_err->code);
6159 break;
6161 lineno++;
6162 p = line;
6163 while (isspace((unsigned char)p[0]))
6164 p++;
6165 if (p[0] == '#' || p[0] == '\0') {
6166 free(line);
6167 line = NULL;
6168 continue;
6170 cmd = NULL;
6171 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6172 cmd = &got_histedit_cmds[i];
6173 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6174 isspace((unsigned char)p[strlen(cmd->name)])) {
6175 p += strlen(cmd->name);
6176 break;
6178 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6179 p++;
6180 break;
6183 if (i == nitems(got_histedit_cmds)) {
6184 err = histedit_syntax_error(lineno);
6185 break;
6187 while (isspace((unsigned char)p[0]))
6188 p++;
6189 if (cmd->code == GOT_HISTEDIT_MESG) {
6190 if (hle == NULL || hle->logmsg != NULL) {
6191 err = got_error(GOT_ERR_HISTEDIT_CMD);
6192 break;
6194 if (p[0] == '\0') {
6195 err = histedit_edit_logmsg(hle, repo);
6196 if (err)
6197 break;
6198 } else {
6199 hle->logmsg = strdup(p);
6200 if (hle->logmsg == NULL) {
6201 err = got_error_from_errno("strdup");
6202 break;
6205 free(line);
6206 line = NULL;
6207 continue;
6208 } else {
6209 end = p;
6210 while (end[0] && !isspace((unsigned char)end[0]))
6211 end++;
6212 *end = '\0';
6214 err = got_object_resolve_id_str(&commit_id, repo, p);
6215 if (err) {
6216 /* override error code */
6217 err = histedit_syntax_error(lineno);
6218 break;
6221 hle = malloc(sizeof(*hle));
6222 if (hle == NULL) {
6223 err = got_error_from_errno("malloc");
6224 break;
6226 hle->cmd = cmd;
6227 hle->commit_id = commit_id;
6228 hle->logmsg = NULL;
6229 commit_id = NULL;
6230 free(line);
6231 line = NULL;
6232 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6235 free(line);
6236 free(commit_id);
6237 return err;
6240 static const struct got_error *
6241 histedit_check_script(struct got_histedit_list *histedit_cmds,
6242 struct got_object_id_queue *commits, struct got_repository *repo)
6244 const struct got_error *err = NULL;
6245 struct got_object_qid *qid;
6246 struct got_histedit_list_entry *hle;
6247 static char msg[92];
6248 char *id_str;
6250 if (TAILQ_EMPTY(histedit_cmds))
6251 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6252 "histedit script contains no commands");
6253 if (SIMPLEQ_EMPTY(commits))
6254 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6256 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6257 struct got_histedit_list_entry *hle2;
6258 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6259 if (hle == hle2)
6260 continue;
6261 if (got_object_id_cmp(hle->commit_id,
6262 hle2->commit_id) != 0)
6263 continue;
6264 err = got_object_id_str(&id_str, hle->commit_id);
6265 if (err)
6266 return err;
6267 snprintf(msg, sizeof(msg), "commit %s is listed "
6268 "more than once in histedit script", id_str);
6269 free(id_str);
6270 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6274 SIMPLEQ_FOREACH(qid, commits, entry) {
6275 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6276 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6277 break;
6279 if (hle == NULL) {
6280 err = got_object_id_str(&id_str, qid->id);
6281 if (err)
6282 return err;
6283 snprintf(msg, sizeof(msg),
6284 "commit %s missing from histedit script", id_str);
6285 free(id_str);
6286 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6290 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6291 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6292 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6293 "last commit in histedit script cannot be folded");
6295 return NULL;
6298 static const struct got_error *
6299 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6300 const char *path, struct got_object_id_queue *commits,
6301 struct got_repository *repo)
6303 const struct got_error *err = NULL;
6304 char *editor;
6305 FILE *f = NULL;
6307 err = get_editor(&editor);
6308 if (err)
6309 return err;
6311 if (spawn_editor(editor, path) == -1) {
6312 err = got_error_from_errno("failed spawning editor");
6313 goto done;
6316 f = fopen(path, "r");
6317 if (f == NULL) {
6318 err = got_error_from_errno("fopen");
6319 goto done;
6321 err = histedit_parse_list(histedit_cmds, f, repo);
6322 if (err)
6323 goto done;
6325 err = histedit_check_script(histedit_cmds, commits, repo);
6326 done:
6327 if (f && fclose(f) != 0 && err == NULL)
6328 err = got_error_from_errno("fclose");
6329 free(editor);
6330 return err;
6333 static const struct got_error *
6334 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6335 struct got_object_id_queue *, const char *, const char *,
6336 struct got_repository *);
6338 static const struct got_error *
6339 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6340 struct got_object_id_queue *commits, const char *branch_name,
6341 int edit_logmsg_only, struct got_repository *repo)
6343 const struct got_error *err;
6344 FILE *f = NULL;
6345 char *path = NULL;
6347 err = got_opentemp_named(&path, &f, "got-histedit");
6348 if (err)
6349 return err;
6351 err = write_cmd_list(f, branch_name, commits);
6352 if (err)
6353 goto done;
6355 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6356 if (err)
6357 goto done;
6359 if (edit_logmsg_only) {
6360 rewind(f);
6361 err = histedit_parse_list(histedit_cmds, f, repo);
6362 } else {
6363 if (fclose(f) != 0) {
6364 err = got_error_from_errno("fclose");
6365 goto done;
6367 f = NULL;
6368 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6369 if (err) {
6370 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6371 err->code != GOT_ERR_HISTEDIT_CMD)
6372 goto done;
6373 err = histedit_edit_list_retry(histedit_cmds, err,
6374 commits, path, branch_name, repo);
6377 done:
6378 if (f && fclose(f) != 0 && err == NULL)
6379 err = got_error_from_errno("fclose");
6380 if (path && unlink(path) != 0 && err == NULL)
6381 err = got_error_from_errno2("unlink", path);
6382 free(path);
6383 return err;
6386 static const struct got_error *
6387 histedit_save_list(struct got_histedit_list *histedit_cmds,
6388 struct got_worktree *worktree, struct got_repository *repo)
6390 const struct got_error *err = NULL;
6391 char *path = NULL;
6392 FILE *f = NULL;
6393 struct got_histedit_list_entry *hle;
6394 struct got_commit_object *commit = NULL;
6396 err = got_worktree_get_histedit_script_path(&path, worktree);
6397 if (err)
6398 return err;
6400 f = fopen(path, "w");
6401 if (f == NULL) {
6402 err = got_error_from_errno2("fopen", path);
6403 goto done;
6405 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6406 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6407 repo);
6408 if (err)
6409 break;
6411 if (hle->logmsg) {
6412 int n = fprintf(f, "%c %s\n",
6413 GOT_HISTEDIT_MESG, hle->logmsg);
6414 if (n < 0) {
6415 err = got_ferror(f, GOT_ERR_IO);
6416 break;
6420 done:
6421 if (f && fclose(f) != 0 && err == NULL)
6422 err = got_error_from_errno("fclose");
6423 free(path);
6424 if (commit)
6425 got_object_commit_close(commit);
6426 return err;
6429 void
6430 histedit_free_list(struct got_histedit_list *histedit_cmds)
6432 struct got_histedit_list_entry *hle;
6434 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6435 TAILQ_REMOVE(histedit_cmds, hle, entry);
6436 free(hle);
6440 static const struct got_error *
6441 histedit_load_list(struct got_histedit_list *histedit_cmds,
6442 const char *path, struct got_repository *repo)
6444 const struct got_error *err = NULL;
6445 FILE *f = NULL;
6447 f = fopen(path, "r");
6448 if (f == NULL) {
6449 err = got_error_from_errno2("fopen", path);
6450 goto done;
6453 err = histedit_parse_list(histedit_cmds, f, repo);
6454 done:
6455 if (f && fclose(f) != 0 && err == NULL)
6456 err = got_error_from_errno("fclose");
6457 return err;
6460 static const struct got_error *
6461 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6462 const struct got_error *edit_err, struct got_object_id_queue *commits,
6463 const char *path, const char *branch_name, struct got_repository *repo)
6465 const struct got_error *err = NULL, *prev_err = edit_err;
6466 int resp = ' ';
6468 while (resp != 'c' && resp != 'r' && resp != 'a') {
6469 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6470 "or (a)bort: ", getprogname(), prev_err->msg);
6471 resp = getchar();
6472 if (resp == '\n')
6473 resp = getchar();
6474 if (resp == 'c') {
6475 histedit_free_list(histedit_cmds);
6476 err = histedit_run_editor(histedit_cmds, path, commits,
6477 repo);
6478 if (err) {
6479 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6480 err->code != GOT_ERR_HISTEDIT_CMD)
6481 break;
6482 prev_err = err;
6483 resp = ' ';
6484 continue;
6486 break;
6487 } else if (resp == 'r') {
6488 histedit_free_list(histedit_cmds);
6489 err = histedit_edit_script(histedit_cmds,
6490 commits, branch_name, 0, repo);
6491 if (err) {
6492 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6493 err->code != GOT_ERR_HISTEDIT_CMD)
6494 break;
6495 prev_err = err;
6496 resp = ' ';
6497 continue;
6499 break;
6500 } else if (resp == 'a') {
6501 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6502 break;
6503 } else
6504 printf("invalid response '%c'\n", resp);
6507 return err;
6510 static const struct got_error *
6511 histedit_complete(struct got_worktree *worktree,
6512 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6513 struct got_reference *branch, struct got_repository *repo)
6515 printf("Switching work tree to %s\n",
6516 got_ref_get_symref_target(branch));
6517 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6518 branch, repo);
6521 static const struct got_error *
6522 show_histedit_progress(struct got_commit_object *commit,
6523 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6525 const struct got_error *err;
6526 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6528 err = got_object_id_str(&old_id_str, hle->commit_id);
6529 if (err)
6530 goto done;
6532 if (new_id) {
6533 err = got_object_id_str(&new_id_str, new_id);
6534 if (err)
6535 goto done;
6538 old_id_str[12] = '\0';
6539 if (new_id_str)
6540 new_id_str[12] = '\0';
6542 if (hle->logmsg) {
6543 logmsg = strdup(hle->logmsg);
6544 if (logmsg == NULL) {
6545 err = got_error_from_errno("strdup");
6546 goto done;
6548 trim_logmsg(logmsg, 42);
6549 } else {
6550 err = get_short_logmsg(&logmsg, 42, commit);
6551 if (err)
6552 goto done;
6555 switch (hle->cmd->code) {
6556 case GOT_HISTEDIT_PICK:
6557 case GOT_HISTEDIT_EDIT:
6558 printf("%s -> %s: %s\n", old_id_str,
6559 new_id_str ? new_id_str : "no-op change", logmsg);
6560 break;
6561 case GOT_HISTEDIT_DROP:
6562 case GOT_HISTEDIT_FOLD:
6563 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6564 logmsg);
6565 break;
6566 default:
6567 break;
6569 done:
6570 free(old_id_str);
6571 free(new_id_str);
6572 return err;
6575 static const struct got_error *
6576 histedit_commit(struct got_pathlist_head *merged_paths,
6577 struct got_worktree *worktree, struct got_fileindex *fileindex,
6578 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6579 struct got_repository *repo)
6581 const struct got_error *err;
6582 struct got_commit_object *commit;
6583 struct got_object_id *new_commit_id;
6585 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6586 && hle->logmsg == NULL) {
6587 err = histedit_edit_logmsg(hle, repo);
6588 if (err)
6589 return err;
6592 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6593 if (err)
6594 return err;
6596 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6597 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6598 hle->logmsg, repo);
6599 if (err) {
6600 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6601 goto done;
6602 err = show_histedit_progress(commit, hle, NULL);
6603 } else {
6604 err = show_histedit_progress(commit, hle, new_commit_id);
6605 free(new_commit_id);
6607 done:
6608 got_object_commit_close(commit);
6609 return err;
6612 static const struct got_error *
6613 histedit_skip_commit(struct got_histedit_list_entry *hle,
6614 struct got_worktree *worktree, struct got_repository *repo)
6616 const struct got_error *error;
6617 struct got_commit_object *commit;
6619 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6620 repo);
6621 if (error)
6622 return error;
6624 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6625 if (error)
6626 return error;
6628 error = show_histedit_progress(commit, hle, NULL);
6629 got_object_commit_close(commit);
6630 return error;
6633 static const struct got_error *
6634 check_local_changes(void *arg, unsigned char status,
6635 unsigned char staged_status, const char *path,
6636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6637 struct got_object_id *commit_id, int dirfd, const char *de_name)
6639 int *have_local_changes = arg;
6641 switch (status) {
6642 case GOT_STATUS_ADD:
6643 case GOT_STATUS_DELETE:
6644 case GOT_STATUS_MODIFY:
6645 case GOT_STATUS_CONFLICT:
6646 *have_local_changes = 1;
6647 return got_error(GOT_ERR_CANCELLED);
6648 default:
6649 break;
6652 switch (staged_status) {
6653 case GOT_STATUS_ADD:
6654 case GOT_STATUS_DELETE:
6655 case GOT_STATUS_MODIFY:
6656 *have_local_changes = 1;
6657 return got_error(GOT_ERR_CANCELLED);
6658 default:
6659 break;
6662 return NULL;
6665 static const struct got_error *
6666 cmd_histedit(int argc, char *argv[])
6668 const struct got_error *error = NULL;
6669 struct got_worktree *worktree = NULL;
6670 struct got_fileindex *fileindex = NULL;
6671 struct got_repository *repo = NULL;
6672 char *cwd = NULL;
6673 struct got_reference *branch = NULL;
6674 struct got_reference *tmp_branch = NULL;
6675 struct got_object_id *resume_commit_id = NULL;
6676 struct got_object_id *base_commit_id = NULL;
6677 struct got_object_id *head_commit_id = NULL;
6678 struct got_commit_object *commit = NULL;
6679 int ch, rebase_in_progress = 0, did_something;
6680 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6681 int edit_logmsg_only = 0;
6682 const char *edit_script_path = NULL;
6683 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6684 struct got_object_id_queue commits;
6685 struct got_pathlist_head merged_paths;
6686 const struct got_object_id_queue *parent_ids;
6687 struct got_object_qid *pid;
6688 struct got_histedit_list histedit_cmds;
6689 struct got_histedit_list_entry *hle;
6691 SIMPLEQ_INIT(&commits);
6692 TAILQ_INIT(&histedit_cmds);
6693 TAILQ_INIT(&merged_paths);
6695 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6696 switch (ch) {
6697 case 'a':
6698 abort_edit = 1;
6699 break;
6700 case 'c':
6701 continue_edit = 1;
6702 break;
6703 case 'F':
6704 edit_script_path = optarg;
6705 break;
6706 case 'm':
6707 edit_logmsg_only = 1;
6708 break;
6709 default:
6710 usage_histedit();
6711 /* NOTREACHED */
6715 argc -= optind;
6716 argv += optind;
6718 #ifndef PROFILE
6719 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6720 "unveil", NULL) == -1)
6721 err(1, "pledge");
6722 #endif
6723 if (abort_edit && continue_edit)
6724 errx(1, "histedit's -a and -c options are mutually exclusive");
6725 if (edit_script_path && edit_logmsg_only)
6726 errx(1, "histedit's -F and -m options are mutually exclusive");
6727 if (abort_edit && edit_logmsg_only)
6728 errx(1, "histedit's -a and -m options are mutually exclusive");
6729 if (continue_edit && edit_logmsg_only)
6730 errx(1, "histedit's -c and -m options are mutually exclusive");
6731 if (argc != 0)
6732 usage_histedit();
6735 * This command cannot apply unveil(2) in all cases because the
6736 * user may choose to run an editor to edit the histedit script
6737 * and to edit individual commit log messages.
6738 * unveil(2) traverses exec(2); if an editor is used we have to
6739 * apply unveil after edit script and log messages have been written.
6740 * XXX TODO: Make use of unveil(2) where possible.
6743 cwd = getcwd(NULL, 0);
6744 if (cwd == NULL) {
6745 error = got_error_from_errno("getcwd");
6746 goto done;
6748 error = got_worktree_open(&worktree, cwd);
6749 if (error)
6750 goto done;
6752 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6753 NULL);
6754 if (error != NULL)
6755 goto done;
6757 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6758 if (error)
6759 goto done;
6760 if (rebase_in_progress) {
6761 error = got_error(GOT_ERR_REBASING);
6762 goto done;
6765 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6766 if (error)
6767 goto done;
6769 if (edit_in_progress && edit_logmsg_only) {
6770 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6771 "histedit operation is in progress in this "
6772 "work tree and must be continued or aborted "
6773 "before the -m option can be used");
6774 goto done;
6777 if (edit_in_progress && abort_edit) {
6778 error = got_worktree_histedit_continue(&resume_commit_id,
6779 &tmp_branch, &branch, &base_commit_id, &fileindex,
6780 worktree, repo);
6781 if (error)
6782 goto done;
6783 printf("Switching work tree to %s\n",
6784 got_ref_get_symref_target(branch));
6785 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6786 branch, base_commit_id, update_progress, &did_something);
6787 if (error)
6788 goto done;
6789 printf("Histedit of %s aborted\n",
6790 got_ref_get_symref_target(branch));
6791 goto done; /* nothing else to do */
6792 } else if (abort_edit) {
6793 error = got_error(GOT_ERR_NOT_HISTEDIT);
6794 goto done;
6797 if (continue_edit) {
6798 char *path;
6800 if (!edit_in_progress) {
6801 error = got_error(GOT_ERR_NOT_HISTEDIT);
6802 goto done;
6805 error = got_worktree_get_histedit_script_path(&path, worktree);
6806 if (error)
6807 goto done;
6809 error = histedit_load_list(&histedit_cmds, path, repo);
6810 free(path);
6811 if (error)
6812 goto done;
6814 error = got_worktree_histedit_continue(&resume_commit_id,
6815 &tmp_branch, &branch, &base_commit_id, &fileindex,
6816 worktree, repo);
6817 if (error)
6818 goto done;
6820 error = got_ref_resolve(&head_commit_id, repo, branch);
6821 if (error)
6822 goto done;
6824 error = got_object_open_as_commit(&commit, repo,
6825 head_commit_id);
6826 if (error)
6827 goto done;
6828 parent_ids = got_object_commit_get_parent_ids(commit);
6829 pid = SIMPLEQ_FIRST(parent_ids);
6830 if (pid == NULL) {
6831 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6832 goto done;
6834 error = collect_commits(&commits, head_commit_id, pid->id,
6835 base_commit_id, got_worktree_get_path_prefix(worktree),
6836 GOT_ERR_HISTEDIT_PATH, repo);
6837 got_object_commit_close(commit);
6838 commit = NULL;
6839 if (error)
6840 goto done;
6841 } else {
6842 if (edit_in_progress) {
6843 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6844 goto done;
6847 error = got_ref_open(&branch, repo,
6848 got_worktree_get_head_ref_name(worktree), 0);
6849 if (error != NULL)
6850 goto done;
6852 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6853 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6854 "will not edit commit history of a branch outside "
6855 "the \"refs/heads/\" reference namespace");
6856 goto done;
6859 error = got_ref_resolve(&head_commit_id, repo, branch);
6860 got_ref_close(branch);
6861 branch = NULL;
6862 if (error)
6863 goto done;
6865 error = got_object_open_as_commit(&commit, repo,
6866 head_commit_id);
6867 if (error)
6868 goto done;
6869 parent_ids = got_object_commit_get_parent_ids(commit);
6870 pid = SIMPLEQ_FIRST(parent_ids);
6871 if (pid == NULL) {
6872 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6873 goto done;
6875 error = collect_commits(&commits, head_commit_id, pid->id,
6876 got_worktree_get_base_commit_id(worktree),
6877 got_worktree_get_path_prefix(worktree),
6878 GOT_ERR_HISTEDIT_PATH, repo);
6879 got_object_commit_close(commit);
6880 commit = NULL;
6881 if (error)
6882 goto done;
6884 if (SIMPLEQ_EMPTY(&commits)) {
6885 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6886 goto done;
6889 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6890 &base_commit_id, &fileindex, worktree, repo);
6891 if (error)
6892 goto done;
6894 if (edit_script_path) {
6895 error = histedit_load_list(&histedit_cmds,
6896 edit_script_path, repo);
6897 if (error) {
6898 got_worktree_histedit_abort(worktree, fileindex,
6899 repo, branch, base_commit_id,
6900 update_progress, &did_something);
6901 goto done;
6903 } else {
6904 const char *branch_name;
6905 branch_name = got_ref_get_symref_target(branch);
6906 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6907 branch_name += 11;
6908 error = histedit_edit_script(&histedit_cmds, &commits,
6909 branch_name, edit_logmsg_only, repo);
6910 if (error) {
6911 got_worktree_histedit_abort(worktree, fileindex,
6912 repo, branch, base_commit_id,
6913 update_progress, &did_something);
6914 goto done;
6919 error = histedit_save_list(&histedit_cmds, worktree,
6920 repo);
6921 if (error) {
6922 got_worktree_histedit_abort(worktree, fileindex,
6923 repo, branch, base_commit_id,
6924 update_progress, &did_something);
6925 goto done;
6930 error = histedit_check_script(&histedit_cmds, &commits, repo);
6931 if (error)
6932 goto done;
6934 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6935 if (resume_commit_id) {
6936 if (got_object_id_cmp(hle->commit_id,
6937 resume_commit_id) != 0)
6938 continue;
6940 resume_commit_id = NULL;
6941 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6942 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6943 error = histedit_skip_commit(hle, worktree,
6944 repo);
6945 if (error)
6946 goto done;
6947 } else {
6948 struct got_pathlist_head paths;
6949 int have_changes = 0;
6951 TAILQ_INIT(&paths);
6952 error = got_pathlist_append(&paths, "", NULL);
6953 if (error)
6954 goto done;
6955 error = got_worktree_status(worktree, &paths,
6956 repo, check_local_changes, &have_changes,
6957 check_cancelled, NULL);
6958 got_pathlist_free(&paths);
6959 if (error) {
6960 if (error->code != GOT_ERR_CANCELLED)
6961 goto done;
6962 if (sigint_received || sigpipe_received)
6963 goto done;
6965 if (have_changes) {
6966 error = histedit_commit(NULL, worktree,
6967 fileindex, tmp_branch, hle, repo);
6968 if (error)
6969 goto done;
6970 } else {
6971 error = got_object_open_as_commit(
6972 &commit, repo, hle->commit_id);
6973 if (error)
6974 goto done;
6975 error = show_histedit_progress(commit,
6976 hle, NULL);
6977 got_object_commit_close(commit);
6978 commit = NULL;
6979 if (error)
6980 goto done;
6983 continue;
6986 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6987 error = histedit_skip_commit(hle, worktree, repo);
6988 if (error)
6989 goto done;
6990 continue;
6993 error = got_object_open_as_commit(&commit, repo,
6994 hle->commit_id);
6995 if (error)
6996 goto done;
6997 parent_ids = got_object_commit_get_parent_ids(commit);
6998 pid = SIMPLEQ_FIRST(parent_ids);
7000 error = got_worktree_histedit_merge_files(&merged_paths,
7001 worktree, fileindex, pid->id, hle->commit_id, repo,
7002 rebase_progress, &rebase_status, check_cancelled, NULL);
7003 if (error)
7004 goto done;
7005 got_object_commit_close(commit);
7006 commit = NULL;
7008 if (rebase_status == GOT_STATUS_CONFLICT) {
7009 error = show_rebase_merge_conflict(hle->commit_id,
7010 repo);
7011 if (error)
7012 goto done;
7013 got_worktree_rebase_pathlist_free(&merged_paths);
7014 break;
7017 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7018 char *id_str;
7019 error = got_object_id_str(&id_str, hle->commit_id);
7020 if (error)
7021 goto done;
7022 printf("Stopping histedit for amending commit %s\n",
7023 id_str);
7024 free(id_str);
7025 got_worktree_rebase_pathlist_free(&merged_paths);
7026 error = got_worktree_histedit_postpone(worktree,
7027 fileindex);
7028 goto done;
7031 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7032 error = histedit_skip_commit(hle, worktree, repo);
7033 if (error)
7034 goto done;
7035 continue;
7038 error = histedit_commit(&merged_paths, worktree, fileindex,
7039 tmp_branch, hle, repo);
7040 got_worktree_rebase_pathlist_free(&merged_paths);
7041 if (error)
7042 goto done;
7045 if (rebase_status == GOT_STATUS_CONFLICT) {
7046 error = got_worktree_histedit_postpone(worktree, fileindex);
7047 if (error)
7048 goto done;
7049 error = got_error_msg(GOT_ERR_CONFLICTS,
7050 "conflicts must be resolved before histedit can continue");
7051 } else
7052 error = histedit_complete(worktree, fileindex, tmp_branch,
7053 branch, repo);
7054 done:
7055 got_object_id_queue_free(&commits);
7056 histedit_free_list(&histedit_cmds);
7057 free(head_commit_id);
7058 free(base_commit_id);
7059 free(resume_commit_id);
7060 if (commit)
7061 got_object_commit_close(commit);
7062 if (branch)
7063 got_ref_close(branch);
7064 if (tmp_branch)
7065 got_ref_close(tmp_branch);
7066 if (worktree)
7067 got_worktree_close(worktree);
7068 if (repo)
7069 got_repo_close(repo);
7070 return error;
7073 __dead static void
7074 usage_integrate(void)
7076 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7077 exit(1);
7080 static const struct got_error *
7081 cmd_integrate(int argc, char *argv[])
7083 const struct got_error *error = NULL;
7084 struct got_repository *repo = NULL;
7085 struct got_worktree *worktree = NULL;
7086 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7087 const char *branch_arg = NULL;
7088 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7089 struct got_fileindex *fileindex = NULL;
7090 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7091 int ch, did_something = 0;
7093 while ((ch = getopt(argc, argv, "")) != -1) {
7094 switch (ch) {
7095 default:
7096 usage_integrate();
7097 /* NOTREACHED */
7101 argc -= optind;
7102 argv += optind;
7104 if (argc != 1)
7105 usage_integrate();
7106 branch_arg = argv[0];
7108 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7109 "unveil", NULL) == -1)
7110 err(1, "pledge");
7112 cwd = getcwd(NULL, 0);
7113 if (cwd == NULL) {
7114 error = got_error_from_errno("getcwd");
7115 goto done;
7118 error = got_worktree_open(&worktree, cwd);
7119 if (error)
7120 goto done;
7122 error = check_rebase_or_histedit_in_progress(worktree);
7123 if (error)
7124 goto done;
7126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7127 NULL);
7128 if (error != NULL)
7129 goto done;
7131 error = apply_unveil(got_repo_get_path(repo), 0,
7132 got_worktree_get_root_path(worktree));
7133 if (error)
7134 goto done;
7136 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7137 error = got_error_from_errno("asprintf");
7138 goto done;
7141 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7142 &base_branch_ref, worktree, refname, repo);
7143 if (error)
7144 goto done;
7146 refname = strdup(got_ref_get_name(branch_ref));
7147 if (refname == NULL) {
7148 error = got_error_from_errno("strdup");
7149 got_worktree_integrate_abort(worktree, fileindex, repo,
7150 branch_ref, base_branch_ref);
7151 goto done;
7153 base_refname = strdup(got_ref_get_name(base_branch_ref));
7154 if (base_refname == NULL) {
7155 error = got_error_from_errno("strdup");
7156 got_worktree_integrate_abort(worktree, fileindex, repo,
7157 branch_ref, base_branch_ref);
7158 goto done;
7161 error = got_ref_resolve(&commit_id, repo, branch_ref);
7162 if (error)
7163 goto done;
7165 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7166 if (error)
7167 goto done;
7169 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7170 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7171 "specified branch has already been integrated");
7172 got_worktree_integrate_abort(worktree, fileindex, repo,
7173 branch_ref, base_branch_ref);
7174 goto done;
7177 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7178 if (error) {
7179 if (error->code == GOT_ERR_ANCESTRY)
7180 error = got_error(GOT_ERR_REBASE_REQUIRED);
7181 got_worktree_integrate_abort(worktree, fileindex, repo,
7182 branch_ref, base_branch_ref);
7183 goto done;
7186 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7187 branch_ref, base_branch_ref, update_progress, &did_something,
7188 check_cancelled, NULL);
7189 if (error)
7190 goto done;
7192 printf("Integrated %s into %s\n", refname, base_refname);
7193 done:
7194 if (repo)
7195 got_repo_close(repo);
7196 if (worktree)
7197 got_worktree_close(worktree);
7198 free(cwd);
7199 free(base_commit_id);
7200 free(commit_id);
7201 free(refname);
7202 free(base_refname);
7203 return error;
7206 __dead static void
7207 usage_stage(void)
7209 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7210 "[file-path ...]\n",
7211 getprogname());
7212 exit(1);
7215 static const struct got_error *
7216 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7217 const char *path, struct got_object_id *blob_id,
7218 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7219 int dirfd, const char *de_name)
7221 const struct got_error *err = NULL;
7222 char *id_str = NULL;
7224 if (staged_status != GOT_STATUS_ADD &&
7225 staged_status != GOT_STATUS_MODIFY &&
7226 staged_status != GOT_STATUS_DELETE)
7227 return NULL;
7229 if (staged_status == GOT_STATUS_ADD ||
7230 staged_status == GOT_STATUS_MODIFY)
7231 err = got_object_id_str(&id_str, staged_blob_id);
7232 else
7233 err = got_object_id_str(&id_str, blob_id);
7234 if (err)
7235 return err;
7237 printf("%s %c %s\n", id_str, staged_status, path);
7238 free(id_str);
7239 return NULL;
7242 static const struct got_error *
7243 cmd_stage(int argc, char *argv[])
7245 const struct got_error *error = NULL;
7246 struct got_repository *repo = NULL;
7247 struct got_worktree *worktree = NULL;
7248 char *cwd = NULL;
7249 struct got_pathlist_head paths;
7250 struct got_pathlist_entry *pe;
7251 int ch, list_stage = 0, pflag = 0;
7252 FILE *patch_script_file = NULL;
7253 const char *patch_script_path = NULL;
7254 struct choose_patch_arg cpa;
7256 TAILQ_INIT(&paths);
7258 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7259 switch (ch) {
7260 case 'l':
7261 list_stage = 1;
7262 break;
7263 case 'p':
7264 pflag = 1;
7265 break;
7266 case 'F':
7267 patch_script_path = optarg;
7268 break;
7269 default:
7270 usage_stage();
7271 /* NOTREACHED */
7275 argc -= optind;
7276 argv += optind;
7278 #ifndef PROFILE
7279 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7280 "unveil", NULL) == -1)
7281 err(1, "pledge");
7282 #endif
7283 if (list_stage && (pflag || patch_script_path))
7284 errx(1, "-l option cannot be used with other options");
7285 if (patch_script_path && !pflag)
7286 errx(1, "-F option can only be used together with -p option");
7288 cwd = getcwd(NULL, 0);
7289 if (cwd == NULL) {
7290 error = got_error_from_errno("getcwd");
7291 goto done;
7294 error = got_worktree_open(&worktree, cwd);
7295 if (error)
7296 goto done;
7298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7299 NULL);
7300 if (error != NULL)
7301 goto done;
7303 if (patch_script_path) {
7304 patch_script_file = fopen(patch_script_path, "r");
7305 if (patch_script_file == NULL) {
7306 error = got_error_from_errno2("fopen",
7307 patch_script_path);
7308 goto done;
7311 error = apply_unveil(got_repo_get_path(repo), 0,
7312 got_worktree_get_root_path(worktree));
7313 if (error)
7314 goto done;
7316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7317 if (error)
7318 goto done;
7320 if (list_stage)
7321 error = got_worktree_status(worktree, &paths, repo,
7322 print_stage, NULL, check_cancelled, NULL);
7323 else {
7324 cpa.patch_script_file = patch_script_file;
7325 cpa.action = "stage";
7326 error = got_worktree_stage(worktree, &paths,
7327 pflag ? NULL : print_status, NULL,
7328 pflag ? choose_patch : NULL, &cpa, repo);
7330 done:
7331 if (patch_script_file && fclose(patch_script_file) == EOF &&
7332 error == NULL)
7333 error = got_error_from_errno2("fclose", patch_script_path);
7334 if (repo)
7335 got_repo_close(repo);
7336 if (worktree)
7337 got_worktree_close(worktree);
7338 TAILQ_FOREACH(pe, &paths, entry)
7339 free((char *)pe->path);
7340 got_pathlist_free(&paths);
7341 free(cwd);
7342 return error;
7345 __dead static void
7346 usage_unstage(void)
7348 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7349 "[file-path ...]\n",
7350 getprogname());
7351 exit(1);
7355 static const struct got_error *
7356 cmd_unstage(int argc, char *argv[])
7358 const struct got_error *error = NULL;
7359 struct got_repository *repo = NULL;
7360 struct got_worktree *worktree = NULL;
7361 char *cwd = NULL;
7362 struct got_pathlist_head paths;
7363 struct got_pathlist_entry *pe;
7364 int ch, did_something = 0, pflag = 0;
7365 FILE *patch_script_file = NULL;
7366 const char *patch_script_path = NULL;
7367 struct choose_patch_arg cpa;
7369 TAILQ_INIT(&paths);
7371 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7372 switch (ch) {
7373 case 'p':
7374 pflag = 1;
7375 break;
7376 case 'F':
7377 patch_script_path = optarg;
7378 break;
7379 default:
7380 usage_unstage();
7381 /* NOTREACHED */
7385 argc -= optind;
7386 argv += optind;
7388 #ifndef PROFILE
7389 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7390 "unveil", NULL) == -1)
7391 err(1, "pledge");
7392 #endif
7393 if (patch_script_path && !pflag)
7394 errx(1, "-F option can only be used together with -p option");
7396 cwd = getcwd(NULL, 0);
7397 if (cwd == NULL) {
7398 error = got_error_from_errno("getcwd");
7399 goto done;
7402 error = got_worktree_open(&worktree, cwd);
7403 if (error)
7404 goto done;
7406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7407 NULL);
7408 if (error != NULL)
7409 goto done;
7411 if (patch_script_path) {
7412 patch_script_file = fopen(patch_script_path, "r");
7413 if (patch_script_file == NULL) {
7414 error = got_error_from_errno2("fopen",
7415 patch_script_path);
7416 goto done;
7420 error = apply_unveil(got_repo_get_path(repo), 0,
7421 got_worktree_get_root_path(worktree));
7422 if (error)
7423 goto done;
7425 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7426 if (error)
7427 goto done;
7429 cpa.patch_script_file = patch_script_file;
7430 cpa.action = "unstage";
7431 error = got_worktree_unstage(worktree, &paths, update_progress,
7432 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7433 done:
7434 if (patch_script_file && fclose(patch_script_file) == EOF &&
7435 error == NULL)
7436 error = got_error_from_errno2("fclose", patch_script_path);
7437 if (repo)
7438 got_repo_close(repo);
7439 if (worktree)
7440 got_worktree_close(worktree);
7441 TAILQ_FOREACH(pe, &paths, entry)
7442 free((char *)pe->path);
7443 got_pathlist_free(&paths);
7444 free(cwd);
7445 return error;
7448 __dead static void
7449 usage_cat(void)
7451 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7452 "arg1 [arg2 ...]\n", getprogname());
7453 exit(1);
7456 static const struct got_error *
7457 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7459 const struct got_error *err;
7460 struct got_blob_object *blob;
7462 err = got_object_open_as_blob(&blob, repo, id, 8192);
7463 if (err)
7464 return err;
7466 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7467 got_object_blob_close(blob);
7468 return err;
7471 static const struct got_error *
7472 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7474 const struct got_error *err;
7475 struct got_tree_object *tree;
7476 int nentries, i;
7478 err = got_object_open_as_tree(&tree, repo, id);
7479 if (err)
7480 return err;
7482 nentries = got_object_tree_get_nentries(tree);
7483 for (i = 0; i < nentries; i++) {
7484 struct got_tree_entry *te;
7485 char *id_str;
7486 if (sigint_received || sigpipe_received)
7487 break;
7488 te = got_object_tree_get_entry(tree, i);
7489 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7490 if (err)
7491 break;
7492 fprintf(outfile, "%s %.7o %s\n", id_str,
7493 got_tree_entry_get_mode(te),
7494 got_tree_entry_get_name(te));
7495 free(id_str);
7498 got_object_tree_close(tree);
7499 return err;
7502 static const struct got_error *
7503 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7505 const struct got_error *err;
7506 struct got_commit_object *commit;
7507 const struct got_object_id_queue *parent_ids;
7508 struct got_object_qid *pid;
7509 char *id_str = NULL;
7510 const char *logmsg = NULL;
7512 err = got_object_open_as_commit(&commit, repo, id);
7513 if (err)
7514 return err;
7516 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7517 if (err)
7518 goto done;
7520 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7521 parent_ids = got_object_commit_get_parent_ids(commit);
7522 fprintf(outfile, "numparents %d\n",
7523 got_object_commit_get_nparents(commit));
7524 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7525 char *pid_str;
7526 err = got_object_id_str(&pid_str, pid->id);
7527 if (err)
7528 goto done;
7529 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7530 free(pid_str);
7532 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7533 got_object_commit_get_author(commit),
7534 got_object_commit_get_author_time(commit));
7536 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7537 got_object_commit_get_author(commit),
7538 got_object_commit_get_committer_time(commit));
7540 logmsg = got_object_commit_get_logmsg_raw(commit);
7541 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7542 fprintf(outfile, "%s", logmsg);
7543 done:
7544 free(id_str);
7545 got_object_commit_close(commit);
7546 return err;
7549 static const struct got_error *
7550 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7552 const struct got_error *err;
7553 struct got_tag_object *tag;
7554 char *id_str = NULL;
7555 const char *tagmsg = NULL;
7557 err = got_object_open_as_tag(&tag, repo, id);
7558 if (err)
7559 return err;
7561 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7562 if (err)
7563 goto done;
7565 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7567 switch (got_object_tag_get_object_type(tag)) {
7568 case GOT_OBJ_TYPE_BLOB:
7569 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7570 GOT_OBJ_LABEL_BLOB);
7571 break;
7572 case GOT_OBJ_TYPE_TREE:
7573 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7574 GOT_OBJ_LABEL_TREE);
7575 break;
7576 case GOT_OBJ_TYPE_COMMIT:
7577 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7578 GOT_OBJ_LABEL_COMMIT);
7579 break;
7580 case GOT_OBJ_TYPE_TAG:
7581 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7582 GOT_OBJ_LABEL_TAG);
7583 break;
7584 default:
7585 break;
7588 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7589 got_object_tag_get_name(tag));
7591 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7592 got_object_tag_get_tagger(tag),
7593 got_object_tag_get_tagger_time(tag));
7595 tagmsg = got_object_tag_get_message(tag);
7596 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7597 fprintf(outfile, "%s", tagmsg);
7598 done:
7599 free(id_str);
7600 got_object_tag_close(tag);
7601 return err;
7604 static const struct got_error *
7605 cmd_cat(int argc, char *argv[])
7607 const struct got_error *error;
7608 struct got_repository *repo = NULL;
7609 struct got_worktree *worktree = NULL;
7610 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7611 const char *commit_id_str = NULL;
7612 struct got_object_id *id = NULL, *commit_id = NULL;
7613 int ch, obj_type, i, force_path = 0;
7615 #ifndef PROFILE
7616 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7617 NULL) == -1)
7618 err(1, "pledge");
7619 #endif
7621 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7622 switch (ch) {
7623 case 'c':
7624 commit_id_str = optarg;
7625 break;
7626 case 'r':
7627 repo_path = realpath(optarg, NULL);
7628 if (repo_path == NULL)
7629 return got_error_from_errno2("realpath",
7630 optarg);
7631 got_path_strip_trailing_slashes(repo_path);
7632 break;
7633 case 'P':
7634 force_path = 1;
7635 break;
7636 default:
7637 usage_cat();
7638 /* NOTREACHED */
7642 argc -= optind;
7643 argv += optind;
7645 cwd = getcwd(NULL, 0);
7646 if (cwd == NULL) {
7647 error = got_error_from_errno("getcwd");
7648 goto done;
7650 error = got_worktree_open(&worktree, cwd);
7651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7652 goto done;
7653 if (worktree) {
7654 if (repo_path == NULL) {
7655 repo_path = strdup(
7656 got_worktree_get_repo_path(worktree));
7657 if (repo_path == NULL) {
7658 error = got_error_from_errno("strdup");
7659 goto done;
7664 if (repo_path == NULL) {
7665 repo_path = getcwd(NULL, 0);
7666 if (repo_path == NULL)
7667 return got_error_from_errno("getcwd");
7670 error = got_repo_open(&repo, repo_path, NULL);
7671 free(repo_path);
7672 if (error != NULL)
7673 goto done;
7675 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7676 if (error)
7677 goto done;
7679 if (commit_id_str == NULL)
7680 commit_id_str = GOT_REF_HEAD;
7681 error = got_repo_match_object_id(&commit_id, NULL,
7682 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7683 if (error)
7684 goto done;
7686 for (i = 0; i < argc; i++) {
7687 if (force_path) {
7688 error = got_object_id_by_path(&id, repo, commit_id,
7689 argv[i]);
7690 if (error)
7691 break;
7692 } else {
7693 error = got_repo_match_object_id(&id, &label, argv[i],
7694 GOT_OBJ_TYPE_ANY, 0, repo);
7695 if (error) {
7696 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7697 error->code != GOT_ERR_NOT_REF)
7698 break;
7699 error = got_object_id_by_path(&id, repo,
7700 commit_id, argv[i]);
7701 if (error)
7702 break;
7706 error = got_object_get_type(&obj_type, repo, id);
7707 if (error)
7708 break;
7710 switch (obj_type) {
7711 case GOT_OBJ_TYPE_BLOB:
7712 error = cat_blob(id, repo, stdout);
7713 break;
7714 case GOT_OBJ_TYPE_TREE:
7715 error = cat_tree(id, repo, stdout);
7716 break;
7717 case GOT_OBJ_TYPE_COMMIT:
7718 error = cat_commit(id, repo, stdout);
7719 break;
7720 case GOT_OBJ_TYPE_TAG:
7721 error = cat_tag(id, repo, stdout);
7722 break;
7723 default:
7724 error = got_error(GOT_ERR_OBJ_TYPE);
7725 break;
7727 if (error)
7728 break;
7729 free(label);
7730 label = NULL;
7731 free(id);
7732 id = NULL;
7734 done:
7735 free(label);
7736 free(id);
7737 free(commit_id);
7738 if (worktree)
7739 got_worktree_close(worktree);
7740 if (repo) {
7741 const struct got_error *repo_error;
7742 repo_error = got_repo_close(repo);
7743 if (error == NULL)
7744 error = repo_error;
7746 return error;