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 fetch_progress(void *arg, const char *message)
974 char *servername = arg;
975 printf("\rserver %s: %s", servername, message);
976 fflush(stdout);
977 return NULL;
980 static const struct got_error *
981 cmd_clone(int argc, char *argv[])
983 const struct got_error *err = NULL;
984 const char *uri, *branch_filter, *dirname;
985 char *proto, *host, *port, *repo_name, *server_path;
986 char *default_destdir = NULL, *id_str = NULL;
987 const char *repo_path;
988 struct got_repository *repo = NULL;
989 struct got_pathlist_head refs, symrefs;
990 struct got_pathlist_entry *pe;
991 struct got_object_id *pack_hash = NULL;
992 int ch, fetchfd = -1;
994 TAILQ_INIT(&refs);
995 TAILQ_INIT(&symrefs);
997 while ((ch = getopt(argc, argv, "b:")) != -1) {
998 switch (ch) {
999 case 'b':
1000 branch_filter = optarg;
1001 break;
1002 default:
1003 usage_clone();
1004 break;
1007 argc -= optind;
1008 argv += optind;
1009 uri = argv[0];
1010 if(argc == 1)
1011 dirname = NULL;
1012 else if(argc == 2)
1013 dirname = argv[1];
1014 else
1015 usage_clone();
1017 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1018 &repo_name, argv[0]);
1019 if (err)
1020 goto done;
1022 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1023 if (err)
1024 goto done;
1026 if (dirname == NULL) {
1027 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1028 err = got_error_from_errno("asprintf");
1029 goto done;
1031 repo_path = default_destdir;
1032 } else
1033 repo_path = dirname;
1035 err = got_path_mkdir(repo_path);
1036 if (err)
1037 goto done;
1039 err = got_repo_init(repo_path);
1040 if (err != NULL)
1041 goto done;
1043 err = got_repo_open(&repo, repo_path, NULL);
1044 if (err)
1045 goto done;
1047 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1048 repo, fetch_progress, host);
1049 if (err)
1050 goto done;
1052 err = got_object_id_str(&id_str, pack_hash);
1053 if (err)
1054 goto done;
1055 printf("Fetched %s.pack\n", id_str);
1056 free(id_str);
1058 /* Set up references provided with the pack file. */
1059 TAILQ_FOREACH(pe, &refs, entry) {
1060 const char *refname = pe->path;
1061 struct got_object_id *id = pe->data;
1062 struct got_reference *ref;
1064 err = got_object_id_str(&id_str, id);
1065 if (err)
1066 goto done;
1068 err = got_ref_alloc(&ref, refname, id);
1069 if (err) {
1070 free(id_str);
1071 goto done;
1074 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1075 free(id_str);
1076 err = got_ref_write(ref, repo);
1077 got_ref_close(ref);
1078 if (err)
1079 goto done;
1082 /* Set the HEAD reference if the server provided one. */
1083 TAILQ_FOREACH(pe, &symrefs, entry) {
1084 struct got_reference *symref, *target_ref;
1085 const char *refname = pe->path;
1086 const char *target = pe->data;
1088 if (strcmp(refname, GOT_REF_HEAD) != 0)
1089 continue;
1091 err = got_ref_open(&target_ref, repo, target, 0);
1092 if (err) {
1093 if (err->code == GOT_ERR_NOT_REF)
1094 continue;
1095 goto done;
1098 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1099 got_ref_close(target_ref);
1100 if (err)
1101 goto done;
1103 printf("Setting %s to %s\n", GOT_REF_HEAD,
1104 got_ref_get_symref_target(symref));
1106 err = got_ref_write(symref, repo);
1107 got_ref_close(symref);
1108 break;
1111 done:
1112 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1113 err = got_error_from_errno("close");
1114 if (repo)
1115 got_repo_close(repo);
1116 TAILQ_FOREACH(pe, &refs, entry) {
1117 free((void *)pe->path);
1118 free(pe->data);
1120 got_pathlist_free(&refs);
1121 TAILQ_FOREACH(pe, &symrefs, entry) {
1122 free((void *)pe->path);
1123 free(pe->data);
1125 got_pathlist_free(&symrefs);
1126 free(pack_hash);
1127 free(proto);
1128 free(host);
1129 free(port);
1130 free(server_path);
1131 free(repo_name);
1132 free(default_destdir);
1133 return err;
1136 static const struct got_error *
1137 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1139 static char msg[512];
1140 const char *branch_name;
1142 if (got_ref_is_symbolic(ref))
1143 branch_name = got_ref_get_symref_target(ref);
1144 else
1145 branch_name = got_ref_get_name(ref);
1147 if (strncmp("refs/heads/", branch_name, 11) == 0)
1148 branch_name += 11;
1150 snprintf(msg, sizeof(msg),
1151 "target commit is not contained in branch '%s'; "
1152 "the branch to use must be specified with -b; "
1153 "if necessary a new branch can be created for "
1154 "this commit with 'got branch -c %s BRANCH_NAME'",
1155 branch_name, commit_id_str);
1157 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1160 static const struct got_error *
1161 cmd_checkout(int argc, char *argv[])
1163 const struct got_error *error = NULL;
1164 struct got_repository *repo = NULL;
1165 struct got_reference *head_ref = NULL;
1166 struct got_worktree *worktree = NULL;
1167 char *repo_path = NULL;
1168 char *worktree_path = NULL;
1169 const char *path_prefix = "";
1170 const char *branch_name = GOT_REF_HEAD;
1171 char *commit_id_str = NULL;
1172 int ch, same_path_prefix, allow_nonempty = 0;
1173 struct got_pathlist_head paths;
1174 struct got_checkout_progress_arg cpa;
1176 TAILQ_INIT(&paths);
1178 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1179 switch (ch) {
1180 case 'b':
1181 branch_name = optarg;
1182 break;
1183 case 'c':
1184 commit_id_str = strdup(optarg);
1185 if (commit_id_str == NULL)
1186 return got_error_from_errno("strdup");
1187 break;
1188 case 'E':
1189 allow_nonempty = 1;
1190 break;
1191 case 'p':
1192 path_prefix = optarg;
1193 break;
1194 default:
1195 usage_checkout();
1196 /* NOTREACHED */
1200 argc -= optind;
1201 argv += optind;
1203 #ifndef PROFILE
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1206 err(1, "pledge");
1207 #endif
1208 if (argc == 1) {
1209 char *cwd, *base, *dotgit;
1210 repo_path = realpath(argv[0], NULL);
1211 if (repo_path == NULL)
1212 return got_error_from_errno2("realpath", argv[0]);
1213 cwd = getcwd(NULL, 0);
1214 if (cwd == NULL) {
1215 error = got_error_from_errno("getcwd");
1216 goto done;
1218 if (path_prefix[0]) {
1219 base = basename(path_prefix);
1220 if (base == NULL) {
1221 error = got_error_from_errno2("basename",
1222 path_prefix);
1223 goto done;
1225 } else {
1226 base = basename(repo_path);
1227 if (base == NULL) {
1228 error = got_error_from_errno2("basename",
1229 repo_path);
1230 goto done;
1233 dotgit = strstr(base, ".git");
1234 if (dotgit)
1235 *dotgit = '\0';
1236 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1237 error = got_error_from_errno("asprintf");
1238 free(cwd);
1239 goto done;
1241 free(cwd);
1242 } else if (argc == 2) {
1243 repo_path = realpath(argv[0], NULL);
1244 if (repo_path == NULL) {
1245 error = got_error_from_errno2("realpath", argv[0]);
1246 goto done;
1248 worktree_path = realpath(argv[1], NULL);
1249 if (worktree_path == NULL) {
1250 if (errno != ENOENT) {
1251 error = got_error_from_errno2("realpath",
1252 argv[1]);
1253 goto done;
1255 worktree_path = strdup(argv[1]);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("strdup");
1258 goto done;
1261 } else
1262 usage_checkout();
1264 got_path_strip_trailing_slashes(repo_path);
1265 got_path_strip_trailing_slashes(worktree_path);
1267 error = got_repo_open(&repo, repo_path, NULL);
1268 if (error != NULL)
1269 goto done;
1271 /* Pre-create work tree path for unveil(2) */
1272 error = got_path_mkdir(worktree_path);
1273 if (error) {
1274 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1275 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1276 goto done;
1277 if (!allow_nonempty &&
1278 !got_path_dir_is_empty(worktree_path)) {
1279 error = got_error_path(worktree_path,
1280 GOT_ERR_DIR_NOT_EMPTY);
1281 goto done;
1285 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1286 if (error)
1287 goto done;
1289 error = got_ref_open(&head_ref, repo, branch_name, 0);
1290 if (error != NULL)
1291 goto done;
1293 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1294 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1295 goto done;
1297 error = got_worktree_open(&worktree, worktree_path);
1298 if (error != NULL)
1299 goto done;
1301 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1302 path_prefix);
1303 if (error != NULL)
1304 goto done;
1305 if (!same_path_prefix) {
1306 error = got_error(GOT_ERR_PATH_PREFIX);
1307 goto done;
1310 if (commit_id_str) {
1311 struct got_object_id *commit_id;
1312 error = got_repo_match_object_id(&commit_id, NULL,
1313 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1314 if (error)
1315 goto done;
1316 error = check_linear_ancestry(commit_id,
1317 got_worktree_get_base_commit_id(worktree), 0, repo);
1318 if (error != NULL) {
1319 free(commit_id);
1320 if (error->code == GOT_ERR_ANCESTRY) {
1321 error = checkout_ancestry_error(
1322 head_ref, commit_id_str);
1324 goto done;
1326 error = check_same_branch(commit_id, head_ref, NULL, repo);
1327 if (error) {
1328 if (error->code == GOT_ERR_ANCESTRY) {
1329 error = checkout_ancestry_error(
1330 head_ref, commit_id_str);
1332 goto done;
1334 error = got_worktree_set_base_commit_id(worktree, repo,
1335 commit_id);
1336 free(commit_id);
1337 if (error)
1338 goto done;
1341 error = got_pathlist_append(&paths, "", NULL);
1342 if (error)
1343 goto done;
1344 cpa.worktree_path = worktree_path;
1345 cpa.had_base_commit_ref_error = 0;
1346 error = got_worktree_checkout_files(worktree, &paths, repo,
1347 checkout_progress, &cpa, check_cancelled, NULL);
1348 if (error != NULL)
1349 goto done;
1351 printf("Now shut up and hack\n");
1352 if (cpa.had_base_commit_ref_error)
1353 show_worktree_base_ref_warning();
1354 done:
1355 got_pathlist_free(&paths);
1356 free(commit_id_str);
1357 free(repo_path);
1358 free(worktree_path);
1359 return error;
1362 __dead static void
1363 usage_update(void)
1365 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1366 getprogname());
1367 exit(1);
1370 static const struct got_error *
1371 update_progress(void *arg, unsigned char status, const char *path)
1373 int *did_something = arg;
1375 if (status == GOT_STATUS_EXISTS ||
1376 status == GOT_STATUS_BASE_REF_ERR)
1377 return NULL;
1379 *did_something = 1;
1381 /* Base commit bump happens silently. */
1382 if (status == GOT_STATUS_BUMP_BASE)
1383 return NULL;
1385 while (path[0] == '/')
1386 path++;
1387 printf("%c %s\n", status, path);
1388 return NULL;
1391 static const struct got_error *
1392 switch_head_ref(struct got_reference *head_ref,
1393 struct got_object_id *commit_id, struct got_worktree *worktree,
1394 struct got_repository *repo)
1396 const struct got_error *err = NULL;
1397 char *base_id_str;
1398 int ref_has_moved = 0;
1400 /* Trivial case: switching between two different references. */
1401 if (strcmp(got_ref_get_name(head_ref),
1402 got_worktree_get_head_ref_name(worktree)) != 0) {
1403 printf("Switching work tree from %s to %s\n",
1404 got_worktree_get_head_ref_name(worktree),
1405 got_ref_get_name(head_ref));
1406 return got_worktree_set_head_ref(worktree, head_ref);
1409 err = check_linear_ancestry(commit_id,
1410 got_worktree_get_base_commit_id(worktree), 0, repo);
1411 if (err) {
1412 if (err->code != GOT_ERR_ANCESTRY)
1413 return err;
1414 ref_has_moved = 1;
1416 if (!ref_has_moved)
1417 return NULL;
1419 /* Switching to a rebased branch with the same reference name. */
1420 err = got_object_id_str(&base_id_str,
1421 got_worktree_get_base_commit_id(worktree));
1422 if (err)
1423 return err;
1424 printf("Reference %s now points at a different branch\n",
1425 got_worktree_get_head_ref_name(worktree));
1426 printf("Switching work tree from %s to %s\n", base_id_str,
1427 got_worktree_get_head_ref_name(worktree));
1428 return NULL;
1431 static const struct got_error *
1432 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1434 const struct got_error *err;
1435 int in_progress;
1437 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1438 if (err)
1439 return err;
1440 if (in_progress)
1441 return got_error(GOT_ERR_REBASING);
1443 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1444 if (err)
1445 return err;
1446 if (in_progress)
1447 return got_error(GOT_ERR_HISTEDIT_BUSY);
1449 return NULL;
1452 static const struct got_error *
1453 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1454 char *argv[], struct got_worktree *worktree)
1456 const struct got_error *err = NULL;
1457 char *path;
1458 int i;
1460 if (argc == 0) {
1461 path = strdup("");
1462 if (path == NULL)
1463 return got_error_from_errno("strdup");
1464 return got_pathlist_append(paths, path, NULL);
1467 for (i = 0; i < argc; i++) {
1468 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1469 if (err)
1470 break;
1471 err = got_pathlist_append(paths, path, NULL);
1472 if (err) {
1473 free(path);
1474 break;
1478 return err;
1481 static const struct got_error *
1482 cmd_update(int argc, char *argv[])
1484 const struct got_error *error = NULL;
1485 struct got_repository *repo = NULL;
1486 struct got_worktree *worktree = NULL;
1487 char *worktree_path = NULL;
1488 struct got_object_id *commit_id = NULL;
1489 char *commit_id_str = NULL;
1490 const char *branch_name = NULL;
1491 struct got_reference *head_ref = NULL;
1492 struct got_pathlist_head paths;
1493 struct got_pathlist_entry *pe;
1494 int ch, did_something = 0;
1496 TAILQ_INIT(&paths);
1498 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1499 switch (ch) {
1500 case 'b':
1501 branch_name = optarg;
1502 break;
1503 case 'c':
1504 commit_id_str = strdup(optarg);
1505 if (commit_id_str == NULL)
1506 return got_error_from_errno("strdup");
1507 break;
1508 default:
1509 usage_update();
1510 /* NOTREACHED */
1514 argc -= optind;
1515 argv += optind;
1517 #ifndef PROFILE
1518 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1519 "unveil", NULL) == -1)
1520 err(1, "pledge");
1521 #endif
1522 worktree_path = getcwd(NULL, 0);
1523 if (worktree_path == NULL) {
1524 error = got_error_from_errno("getcwd");
1525 goto done;
1527 error = got_worktree_open(&worktree, worktree_path);
1528 if (error)
1529 goto done;
1531 error = check_rebase_or_histedit_in_progress(worktree);
1532 if (error)
1533 goto done;
1535 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1536 NULL);
1537 if (error != NULL)
1538 goto done;
1540 error = apply_unveil(got_repo_get_path(repo), 0,
1541 got_worktree_get_root_path(worktree));
1542 if (error)
1543 goto done;
1545 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1546 if (error)
1547 goto done;
1549 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1550 got_worktree_get_head_ref_name(worktree), 0);
1551 if (error != NULL)
1552 goto done;
1553 if (commit_id_str == NULL) {
1554 error = got_ref_resolve(&commit_id, repo, head_ref);
1555 if (error != NULL)
1556 goto done;
1557 error = got_object_id_str(&commit_id_str, commit_id);
1558 if (error != NULL)
1559 goto done;
1560 } else {
1561 error = got_repo_match_object_id(&commit_id, NULL,
1562 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1563 free(commit_id_str);
1564 commit_id_str = NULL;
1565 if (error)
1566 goto done;
1567 error = got_object_id_str(&commit_id_str, commit_id);
1568 if (error)
1569 goto done;
1572 if (branch_name) {
1573 struct got_object_id *head_commit_id;
1574 TAILQ_FOREACH(pe, &paths, entry) {
1575 if (pe->path_len == 0)
1576 continue;
1577 error = got_error_msg(GOT_ERR_BAD_PATH,
1578 "switching between branches requires that "
1579 "the entire work tree gets updated");
1580 goto done;
1582 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1583 if (error)
1584 goto done;
1585 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1586 repo);
1587 free(head_commit_id);
1588 if (error != NULL)
1589 goto done;
1590 error = check_same_branch(commit_id, head_ref, NULL, repo);
1591 if (error)
1592 goto done;
1593 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1594 if (error)
1595 goto done;
1596 } else {
1597 error = check_linear_ancestry(commit_id,
1598 got_worktree_get_base_commit_id(worktree), 0, repo);
1599 if (error != NULL) {
1600 if (error->code == GOT_ERR_ANCESTRY)
1601 error = got_error(GOT_ERR_BRANCH_MOVED);
1602 goto done;
1604 error = check_same_branch(commit_id, head_ref, NULL, repo);
1605 if (error)
1606 goto done;
1609 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1610 commit_id) != 0) {
1611 error = got_worktree_set_base_commit_id(worktree, repo,
1612 commit_id);
1613 if (error)
1614 goto done;
1617 error = got_worktree_checkout_files(worktree, &paths, repo,
1618 update_progress, &did_something, check_cancelled, NULL);
1619 if (error != NULL)
1620 goto done;
1622 if (did_something)
1623 printf("Updated to commit %s\n", commit_id_str);
1624 else
1625 printf("Already up-to-date\n");
1626 done:
1627 free(worktree_path);
1628 TAILQ_FOREACH(pe, &paths, entry)
1629 free((char *)pe->path);
1630 got_pathlist_free(&paths);
1631 free(commit_id);
1632 free(commit_id_str);
1633 return error;
1636 static const struct got_error *
1637 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1638 const char *path, int diff_context, int ignore_whitespace,
1639 struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1644 if (blob_id1) {
1645 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1646 if (err)
1647 goto done;
1650 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1651 if (err)
1652 goto done;
1654 while (path[0] == '/')
1655 path++;
1656 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1657 ignore_whitespace, stdout);
1658 done:
1659 if (blob1)
1660 got_object_blob_close(blob1);
1661 got_object_blob_close(blob2);
1662 return err;
1665 static const struct got_error *
1666 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1667 const char *path, int diff_context, int ignore_whitespace,
1668 struct got_repository *repo)
1670 const struct got_error *err = NULL;
1671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1672 struct got_diff_blob_output_unidiff_arg arg;
1674 if (tree_id1) {
1675 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1676 if (err)
1677 goto done;
1680 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1681 if (err)
1682 goto done;
1684 arg.diff_context = diff_context;
1685 arg.ignore_whitespace = ignore_whitespace;
1686 arg.outfile = stdout;
1687 while (path[0] == '/')
1688 path++;
1689 err = got_diff_tree(tree1, tree2, path, path, repo,
1690 got_diff_blob_output_unidiff, &arg, 1);
1691 done:
1692 if (tree1)
1693 got_object_tree_close(tree1);
1694 if (tree2)
1695 got_object_tree_close(tree2);
1696 return err;
1699 static const struct got_error *
1700 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1701 const char *path, int diff_context, struct got_repository *repo)
1703 const struct got_error *err = NULL;
1704 struct got_commit_object *pcommit = NULL;
1705 char *id_str1 = NULL, *id_str2 = NULL;
1706 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1707 struct got_object_qid *qid;
1709 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1710 if (qid != NULL) {
1711 err = got_object_open_as_commit(&pcommit, repo,
1712 qid->id);
1713 if (err)
1714 return err;
1717 if (path && path[0] != '\0') {
1718 int obj_type;
1719 err = got_object_id_by_path(&obj_id2, repo, id, path);
1720 if (err)
1721 goto done;
1722 err = got_object_id_str(&id_str2, obj_id2);
1723 if (err) {
1724 free(obj_id2);
1725 goto done;
1727 if (pcommit) {
1728 err = got_object_id_by_path(&obj_id1, repo,
1729 qid->id, path);
1730 if (err) {
1731 free(obj_id2);
1732 goto done;
1734 err = got_object_id_str(&id_str1, obj_id1);
1735 if (err) {
1736 free(obj_id2);
1737 goto done;
1740 err = got_object_get_type(&obj_type, repo, obj_id2);
1741 if (err) {
1742 free(obj_id2);
1743 goto done;
1745 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1746 switch (obj_type) {
1747 case GOT_OBJ_TYPE_BLOB:
1748 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1749 0, repo);
1750 break;
1751 case GOT_OBJ_TYPE_TREE:
1752 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1753 0, repo);
1754 break;
1755 default:
1756 err = got_error(GOT_ERR_OBJ_TYPE);
1757 break;
1759 free(obj_id1);
1760 free(obj_id2);
1761 } else {
1762 obj_id2 = got_object_commit_get_tree_id(commit);
1763 err = got_object_id_str(&id_str2, obj_id2);
1764 if (err)
1765 goto done;
1766 obj_id1 = got_object_commit_get_tree_id(pcommit);
1767 err = got_object_id_str(&id_str1, obj_id1);
1768 if (err)
1769 goto done;
1770 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1771 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1773 done:
1774 free(id_str1);
1775 free(id_str2);
1776 if (pcommit)
1777 got_object_commit_close(pcommit);
1778 return err;
1781 static char *
1782 get_datestr(time_t *time, char *datebuf)
1784 struct tm mytm, *tm;
1785 char *p, *s;
1787 tm = gmtime_r(time, &mytm);
1788 if (tm == NULL)
1789 return NULL;
1790 s = asctime_r(tm, datebuf);
1791 if (s == NULL)
1792 return NULL;
1793 p = strchr(s, '\n');
1794 if (p)
1795 *p = '\0';
1796 return s;
1799 static const struct got_error *
1800 match_logmsg(int *have_match, struct got_object_id *id,
1801 struct got_commit_object *commit, regex_t *regex)
1803 const struct got_error *err = NULL;
1804 regmatch_t regmatch;
1805 char *id_str = NULL, *logmsg = NULL;
1807 *have_match = 0;
1809 err = got_object_id_str(&id_str, id);
1810 if (err)
1811 return err;
1813 err = got_object_commit_get_logmsg(&logmsg, commit);
1814 if (err)
1815 goto done;
1817 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1818 *have_match = 1;
1819 done:
1820 free(id_str);
1821 free(logmsg);
1822 return err;
1825 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1827 static const struct got_error *
1828 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1829 struct got_repository *repo, const char *path, int show_patch,
1830 int diff_context, struct got_reflist_head *refs)
1832 const struct got_error *err = NULL;
1833 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1834 char datebuf[26];
1835 time_t committer_time;
1836 const char *author, *committer;
1837 char *refs_str = NULL;
1838 struct got_reflist_entry *re;
1840 SIMPLEQ_FOREACH(re, refs, entry) {
1841 char *s;
1842 const char *name;
1843 struct got_tag_object *tag = NULL;
1844 int cmp;
1846 name = got_ref_get_name(re->ref);
1847 if (strcmp(name, GOT_REF_HEAD) == 0)
1848 continue;
1849 if (strncmp(name, "refs/", 5) == 0)
1850 name += 5;
1851 if (strncmp(name, "got/", 4) == 0)
1852 continue;
1853 if (strncmp(name, "heads/", 6) == 0)
1854 name += 6;
1855 if (strncmp(name, "remotes/", 8) == 0)
1856 name += 8;
1857 if (strncmp(name, "tags/", 5) == 0) {
1858 err = got_object_open_as_tag(&tag, repo, re->id);
1859 if (err) {
1860 if (err->code != GOT_ERR_OBJ_TYPE)
1861 return err;
1862 /* Ref points at something other than a tag. */
1863 err = NULL;
1864 tag = NULL;
1867 cmp = got_object_id_cmp(tag ?
1868 got_object_tag_get_object_id(tag) : re->id, id);
1869 if (tag)
1870 got_object_tag_close(tag);
1871 if (cmp != 0)
1872 continue;
1873 s = refs_str;
1874 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1875 name) == -1) {
1876 err = got_error_from_errno("asprintf");
1877 free(s);
1878 return err;
1880 free(s);
1882 err = got_object_id_str(&id_str, id);
1883 if (err)
1884 return err;
1886 printf(GOT_COMMIT_SEP_STR);
1887 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1888 refs_str ? refs_str : "", refs_str ? ")" : "");
1889 free(id_str);
1890 id_str = NULL;
1891 free(refs_str);
1892 refs_str = NULL;
1893 printf("from: %s\n", got_object_commit_get_author(commit));
1894 committer_time = got_object_commit_get_committer_time(commit);
1895 datestr = get_datestr(&committer_time, datebuf);
1896 if (datestr)
1897 printf("date: %s UTC\n", datestr);
1898 author = got_object_commit_get_author(commit);
1899 committer = got_object_commit_get_committer(commit);
1900 if (strcmp(author, committer) != 0)
1901 printf("via: %s\n", committer);
1902 if (got_object_commit_get_nparents(commit) > 1) {
1903 const struct got_object_id_queue *parent_ids;
1904 struct got_object_qid *qid;
1905 int n = 1;
1906 parent_ids = got_object_commit_get_parent_ids(commit);
1907 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1908 err = got_object_id_str(&id_str, qid->id);
1909 if (err)
1910 return err;
1911 printf("parent %d: %s\n", n++, id_str);
1912 free(id_str);
1916 err = got_object_commit_get_logmsg(&logmsg0, commit);
1917 if (err)
1918 return err;
1920 logmsg = logmsg0;
1921 do {
1922 line = strsep(&logmsg, "\n");
1923 if (line)
1924 printf(" %s\n", line);
1925 } while (line);
1926 free(logmsg0);
1928 if (show_patch) {
1929 err = print_patch(commit, id, path, diff_context, repo);
1930 if (err == 0)
1931 printf("\n");
1934 if (fflush(stdout) != 0 && err == NULL)
1935 err = got_error_from_errno("fflush");
1936 return err;
1939 static const struct got_error *
1940 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1941 const char *path, int show_patch, const char *search_pattern,
1942 int diff_context, int limit, int log_branches,
1943 struct got_reflist_head *refs)
1945 const struct got_error *err;
1946 struct got_commit_graph *graph;
1947 regex_t regex;
1948 int have_match;
1950 if (search_pattern &&
1951 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1952 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1954 err = got_commit_graph_open(&graph, path, !log_branches);
1955 if (err)
1956 return err;
1957 err = got_commit_graph_iter_start(graph, root_id, repo,
1958 check_cancelled, NULL);
1959 if (err)
1960 goto done;
1961 for (;;) {
1962 struct got_commit_object *commit;
1963 struct got_object_id *id;
1965 if (sigint_received || sigpipe_received)
1966 break;
1968 err = got_commit_graph_iter_next(&id, graph, repo,
1969 check_cancelled, NULL);
1970 if (err) {
1971 if (err->code == GOT_ERR_ITER_COMPLETED)
1972 err = NULL;
1973 break;
1975 if (id == NULL)
1976 break;
1978 err = got_object_open_as_commit(&commit, repo, id);
1979 if (err)
1980 break;
1982 if (search_pattern) {
1983 err = match_logmsg(&have_match, id, commit, &regex);
1984 if (err) {
1985 got_object_commit_close(commit);
1986 break;
1988 if (have_match == 0) {
1989 got_object_commit_close(commit);
1990 continue;
1994 err = print_commit(commit, id, repo, path, show_patch,
1995 diff_context, refs);
1996 got_object_commit_close(commit);
1997 if (err || (limit && --limit == 0))
1998 break;
2000 done:
2001 if (search_pattern)
2002 regfree(&regex);
2003 got_commit_graph_close(graph);
2004 return err;
2007 __dead static void
2008 usage_log(void)
2010 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2011 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2012 exit(1);
2015 static int
2016 get_default_log_limit(void)
2018 const char *got_default_log_limit;
2019 long long n;
2020 const char *errstr;
2022 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2023 if (got_default_log_limit == NULL)
2024 return 0;
2025 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2026 if (errstr != NULL)
2027 return 0;
2028 return n;
2031 static const struct got_error *
2032 cmd_log(int argc, char *argv[])
2034 const struct got_error *error;
2035 struct got_repository *repo = NULL;
2036 struct got_worktree *worktree = NULL;
2037 struct got_commit_object *commit = NULL;
2038 struct got_object_id *id = NULL;
2039 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2040 const char *start_commit = NULL, *search_pattern = NULL;
2041 int diff_context = -1, ch;
2042 int show_patch = 0, limit = 0, log_branches = 0;
2043 const char *errstr;
2044 struct got_reflist_head refs;
2046 SIMPLEQ_INIT(&refs);
2048 #ifndef PROFILE
2049 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2050 NULL)
2051 == -1)
2052 err(1, "pledge");
2053 #endif
2055 limit = get_default_log_limit();
2057 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2058 switch (ch) {
2059 case 'p':
2060 show_patch = 1;
2061 break;
2062 case 'c':
2063 start_commit = optarg;
2064 break;
2065 case 'C':
2066 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2067 &errstr);
2068 if (errstr != NULL)
2069 err(1, "-C option %s", errstr);
2070 break;
2071 case 'l':
2072 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2073 if (errstr != NULL)
2074 err(1, "-l option %s", errstr);
2075 break;
2076 case 'b':
2077 log_branches = 1;
2078 break;
2079 case 'r':
2080 repo_path = realpath(optarg, NULL);
2081 if (repo_path == NULL)
2082 return got_error_from_errno2("realpath",
2083 optarg);
2084 got_path_strip_trailing_slashes(repo_path);
2085 break;
2086 case 's':
2087 search_pattern = optarg;
2088 break;
2089 default:
2090 usage_log();
2091 /* NOTREACHED */
2095 argc -= optind;
2096 argv += optind;
2098 if (diff_context == -1)
2099 diff_context = 3;
2100 else if (!show_patch)
2101 errx(1, "-C reguires -p");
2103 cwd = getcwd(NULL, 0);
2104 if (cwd == NULL) {
2105 error = got_error_from_errno("getcwd");
2106 goto done;
2109 error = got_worktree_open(&worktree, cwd);
2110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2111 goto done;
2112 error = NULL;
2114 if (argc == 0) {
2115 path = strdup("");
2116 if (path == NULL) {
2117 error = got_error_from_errno("strdup");
2118 goto done;
2120 } else if (argc == 1) {
2121 if (worktree) {
2122 error = got_worktree_resolve_path(&path, worktree,
2123 argv[0]);
2124 if (error)
2125 goto done;
2126 } else {
2127 path = strdup(argv[0]);
2128 if (path == NULL) {
2129 error = got_error_from_errno("strdup");
2130 goto done;
2133 } else
2134 usage_log();
2136 if (repo_path == NULL) {
2137 repo_path = worktree ?
2138 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2140 if (repo_path == NULL) {
2141 error = got_error_from_errno("strdup");
2142 goto done;
2145 error = got_repo_open(&repo, repo_path, NULL);
2146 if (error != NULL)
2147 goto done;
2149 error = apply_unveil(got_repo_get_path(repo), 1,
2150 worktree ? got_worktree_get_root_path(worktree) : NULL);
2151 if (error)
2152 goto done;
2154 if (start_commit == NULL) {
2155 struct got_reference *head_ref;
2156 error = got_ref_open(&head_ref, repo,
2157 worktree ? got_worktree_get_head_ref_name(worktree)
2158 : GOT_REF_HEAD, 0);
2159 if (error != NULL)
2160 return error;
2161 error = got_ref_resolve(&id, repo, head_ref);
2162 got_ref_close(head_ref);
2163 if (error != NULL)
2164 return error;
2165 error = got_object_open_as_commit(&commit, repo, id);
2166 } else {
2167 struct got_reference *ref;
2168 error = got_ref_open(&ref, repo, start_commit, 0);
2169 if (error == NULL) {
2170 int obj_type;
2171 error = got_ref_resolve(&id, repo, ref);
2172 got_ref_close(ref);
2173 if (error != NULL)
2174 goto done;
2175 error = got_object_get_type(&obj_type, repo, id);
2176 if (error != NULL)
2177 goto done;
2178 if (obj_type == GOT_OBJ_TYPE_TAG) {
2179 struct got_tag_object *tag;
2180 error = got_object_open_as_tag(&tag, repo, id);
2181 if (error != NULL)
2182 goto done;
2183 if (got_object_tag_get_object_type(tag) !=
2184 GOT_OBJ_TYPE_COMMIT) {
2185 got_object_tag_close(tag);
2186 error = got_error(GOT_ERR_OBJ_TYPE);
2187 goto done;
2189 free(id);
2190 id = got_object_id_dup(
2191 got_object_tag_get_object_id(tag));
2192 if (id == NULL)
2193 error = got_error_from_errno(
2194 "got_object_id_dup");
2195 got_object_tag_close(tag);
2196 if (error)
2197 goto done;
2198 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2199 error = got_error(GOT_ERR_OBJ_TYPE);
2200 goto done;
2202 error = got_object_open_as_commit(&commit, repo, id);
2203 if (error != NULL)
2204 goto done;
2206 if (commit == NULL) {
2207 error = got_repo_match_object_id_prefix(&id,
2208 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2209 if (error != NULL)
2210 return error;
2213 if (error != NULL)
2214 goto done;
2216 if (worktree) {
2217 const char *prefix = got_worktree_get_path_prefix(worktree);
2218 char *p;
2219 if (asprintf(&p, "%s%s%s", prefix,
2220 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2221 error = got_error_from_errno("asprintf");
2222 goto done;
2224 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2225 free(p);
2226 } else
2227 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2228 if (error != NULL)
2229 goto done;
2230 if (in_repo_path) {
2231 free(path);
2232 path = in_repo_path;
2235 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2236 if (error)
2237 goto done;
2239 error = print_commits(id, repo, path, show_patch, search_pattern,
2240 diff_context, limit, log_branches, &refs);
2241 done:
2242 free(path);
2243 free(repo_path);
2244 free(cwd);
2245 free(id);
2246 if (worktree)
2247 got_worktree_close(worktree);
2248 if (repo) {
2249 const struct got_error *repo_error;
2250 repo_error = got_repo_close(repo);
2251 if (error == NULL)
2252 error = repo_error;
2254 got_ref_list_free(&refs);
2255 return error;
2258 __dead static void
2259 usage_diff(void)
2261 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2262 "[-w] [object1 object2 | path]\n", getprogname());
2263 exit(1);
2266 struct print_diff_arg {
2267 struct got_repository *repo;
2268 struct got_worktree *worktree;
2269 int diff_context;
2270 const char *id_str;
2271 int header_shown;
2272 int diff_staged;
2273 int ignore_whitespace;
2276 static const struct got_error *
2277 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2278 const char *path, struct got_object_id *blob_id,
2279 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2280 int dirfd, const char *de_name)
2282 struct print_diff_arg *a = arg;
2283 const struct got_error *err = NULL;
2284 struct got_blob_object *blob1 = NULL;
2285 int fd = -1;
2286 FILE *f2 = NULL;
2287 char *abspath = NULL, *label1 = NULL;
2288 struct stat sb;
2290 if (a->diff_staged) {
2291 if (staged_status != GOT_STATUS_MODIFY &&
2292 staged_status != GOT_STATUS_ADD &&
2293 staged_status != GOT_STATUS_DELETE)
2294 return NULL;
2295 } else {
2296 if (staged_status == GOT_STATUS_DELETE)
2297 return NULL;
2298 if (status == GOT_STATUS_NONEXISTENT)
2299 return got_error_set_errno(ENOENT, path);
2300 if (status != GOT_STATUS_MODIFY &&
2301 status != GOT_STATUS_ADD &&
2302 status != GOT_STATUS_DELETE &&
2303 status != GOT_STATUS_CONFLICT)
2304 return NULL;
2307 if (!a->header_shown) {
2308 printf("diff %s %s%s\n", a->id_str,
2309 got_worktree_get_root_path(a->worktree),
2310 a->diff_staged ? " (staged changes)" : "");
2311 a->header_shown = 1;
2314 if (a->diff_staged) {
2315 const char *label1 = NULL, *label2 = NULL;
2316 switch (staged_status) {
2317 case GOT_STATUS_MODIFY:
2318 label1 = path;
2319 label2 = path;
2320 break;
2321 case GOT_STATUS_ADD:
2322 label2 = path;
2323 break;
2324 case GOT_STATUS_DELETE:
2325 label1 = path;
2326 break;
2327 default:
2328 return got_error(GOT_ERR_FILE_STATUS);
2330 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2331 label1, label2, a->diff_context, a->ignore_whitespace,
2332 a->repo, stdout);
2335 if (staged_status == GOT_STATUS_ADD ||
2336 staged_status == GOT_STATUS_MODIFY) {
2337 char *id_str;
2338 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2339 8192);
2340 if (err)
2341 goto done;
2342 err = got_object_id_str(&id_str, staged_blob_id);
2343 if (err)
2344 goto done;
2345 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2346 err = got_error_from_errno("asprintf");
2347 free(id_str);
2348 goto done;
2350 free(id_str);
2351 } else if (status != GOT_STATUS_ADD) {
2352 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2353 if (err)
2354 goto done;
2357 if (status != GOT_STATUS_DELETE) {
2358 if (asprintf(&abspath, "%s/%s",
2359 got_worktree_get_root_path(a->worktree), path) == -1) {
2360 err = got_error_from_errno("asprintf");
2361 goto done;
2364 if (dirfd != -1) {
2365 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2366 if (fd == -1) {
2367 err = got_error_from_errno2("openat", abspath);
2368 goto done;
2370 } else {
2371 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2372 if (fd == -1) {
2373 err = got_error_from_errno2("open", abspath);
2374 goto done;
2377 if (fstat(fd, &sb) == -1) {
2378 err = got_error_from_errno2("fstat", abspath);
2379 goto done;
2381 f2 = fdopen(fd, "r");
2382 if (f2 == NULL) {
2383 err = got_error_from_errno2("fdopen", abspath);
2384 goto done;
2386 fd = -1;
2387 } else
2388 sb.st_size = 0;
2390 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2391 a->diff_context, a->ignore_whitespace, stdout);
2392 done:
2393 if (blob1)
2394 got_object_blob_close(blob1);
2395 if (f2 && fclose(f2) == EOF && err == NULL)
2396 err = got_error_from_errno("fclose");
2397 if (fd != -1 && close(fd) == -1 && err == NULL)
2398 err = got_error_from_errno("close");
2399 free(abspath);
2400 return err;
2403 static const struct got_error *
2404 cmd_diff(int argc, char *argv[])
2406 const struct got_error *error;
2407 struct got_repository *repo = NULL;
2408 struct got_worktree *worktree = NULL;
2409 char *cwd = NULL, *repo_path = NULL;
2410 struct got_object_id *id1 = NULL, *id2 = NULL;
2411 const char *id_str1 = NULL, *id_str2 = NULL;
2412 char *label1 = NULL, *label2 = NULL;
2413 int type1, type2;
2414 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2415 const char *errstr;
2416 char *path = NULL;
2418 #ifndef PROFILE
2419 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2420 NULL) == -1)
2421 err(1, "pledge");
2422 #endif
2424 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2425 switch (ch) {
2426 case 'C':
2427 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2428 &errstr);
2429 if (errstr != NULL)
2430 err(1, "-C option %s", errstr);
2431 break;
2432 case 'r':
2433 repo_path = realpath(optarg, NULL);
2434 if (repo_path == NULL)
2435 return got_error_from_errno2("realpath",
2436 optarg);
2437 got_path_strip_trailing_slashes(repo_path);
2438 break;
2439 case 's':
2440 diff_staged = 1;
2441 break;
2442 case 'w':
2443 ignore_whitespace = 1;
2444 break;
2445 default:
2446 usage_diff();
2447 /* NOTREACHED */
2451 argc -= optind;
2452 argv += optind;
2454 cwd = getcwd(NULL, 0);
2455 if (cwd == NULL) {
2456 error = got_error_from_errno("getcwd");
2457 goto done;
2459 error = got_worktree_open(&worktree, cwd);
2460 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2461 goto done;
2462 if (argc <= 1) {
2463 if (worktree == NULL) {
2464 error = got_error(GOT_ERR_NOT_WORKTREE);
2465 goto done;
2467 if (repo_path)
2468 errx(1,
2469 "-r option can't be used when diffing a work tree");
2470 repo_path = strdup(got_worktree_get_repo_path(worktree));
2471 if (repo_path == NULL) {
2472 error = got_error_from_errno("strdup");
2473 goto done;
2475 if (argc == 1) {
2476 error = got_worktree_resolve_path(&path, worktree,
2477 argv[0]);
2478 if (error)
2479 goto done;
2480 } else {
2481 path = strdup("");
2482 if (path == NULL) {
2483 error = got_error_from_errno("strdup");
2484 goto done;
2487 } else if (argc == 2) {
2488 if (diff_staged)
2489 errx(1, "-s option can't be used when diffing "
2490 "objects in repository");
2491 id_str1 = argv[0];
2492 id_str2 = argv[1];
2493 if (worktree && repo_path == NULL) {
2494 repo_path =
2495 strdup(got_worktree_get_repo_path(worktree));
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2498 goto done;
2501 } else
2502 usage_diff();
2504 if (repo_path == NULL) {
2505 repo_path = getcwd(NULL, 0);
2506 if (repo_path == NULL)
2507 return got_error_from_errno("getcwd");
2510 error = got_repo_open(&repo, repo_path, NULL);
2511 free(repo_path);
2512 if (error != NULL)
2513 goto done;
2515 error = apply_unveil(got_repo_get_path(repo), 1,
2516 worktree ? got_worktree_get_root_path(worktree) : NULL);
2517 if (error)
2518 goto done;
2520 if (argc <= 1) {
2521 struct print_diff_arg arg;
2522 struct got_pathlist_head paths;
2523 char *id_str;
2525 TAILQ_INIT(&paths);
2527 error = got_object_id_str(&id_str,
2528 got_worktree_get_base_commit_id(worktree));
2529 if (error)
2530 goto done;
2531 arg.repo = repo;
2532 arg.worktree = worktree;
2533 arg.diff_context = diff_context;
2534 arg.id_str = id_str;
2535 arg.header_shown = 0;
2536 arg.diff_staged = diff_staged;
2537 arg.ignore_whitespace = ignore_whitespace;
2539 error = got_pathlist_append(&paths, path, NULL);
2540 if (error)
2541 goto done;
2543 error = got_worktree_status(worktree, &paths, repo, print_diff,
2544 &arg, check_cancelled, NULL);
2545 free(id_str);
2546 got_pathlist_free(&paths);
2547 goto done;
2550 error = got_repo_match_object_id(&id1, &label1, id_str1,
2551 GOT_OBJ_TYPE_ANY, 1, repo);
2552 if (error)
2553 goto done;
2555 error = got_repo_match_object_id(&id2, &label2, id_str2,
2556 GOT_OBJ_TYPE_ANY, 1, repo);
2557 if (error)
2558 goto done;
2560 error = got_object_get_type(&type1, repo, id1);
2561 if (error)
2562 goto done;
2564 error = got_object_get_type(&type2, repo, id2);
2565 if (error)
2566 goto done;
2568 if (type1 != type2) {
2569 error = got_error(GOT_ERR_OBJ_TYPE);
2570 goto done;
2573 switch (type1) {
2574 case GOT_OBJ_TYPE_BLOB:
2575 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2576 diff_context, ignore_whitespace, repo, stdout);
2577 break;
2578 case GOT_OBJ_TYPE_TREE:
2579 error = got_diff_objects_as_trees(id1, id2, "", "",
2580 diff_context, ignore_whitespace, repo, stdout);
2581 break;
2582 case GOT_OBJ_TYPE_COMMIT:
2583 printf("diff %s %s\n", label1, label2);
2584 error = got_diff_objects_as_commits(id1, id2, diff_context,
2585 ignore_whitespace, repo, stdout);
2586 break;
2587 default:
2588 error = got_error(GOT_ERR_OBJ_TYPE);
2590 done:
2591 free(label1);
2592 free(label2);
2593 free(id1);
2594 free(id2);
2595 free(path);
2596 if (worktree)
2597 got_worktree_close(worktree);
2598 if (repo) {
2599 const struct got_error *repo_error;
2600 repo_error = got_repo_close(repo);
2601 if (error == NULL)
2602 error = repo_error;
2604 return error;
2607 __dead static void
2608 usage_blame(void)
2610 fprintf(stderr,
2611 "usage: %s blame [-c commit] [-r repository-path] path\n",
2612 getprogname());
2613 exit(1);
2616 struct blame_line {
2617 int annotated;
2618 char *id_str;
2619 char *committer;
2620 char datebuf[11]; /* YYYY-MM-DD + NUL */
2623 struct blame_cb_args {
2624 struct blame_line *lines;
2625 int nlines;
2626 int nlines_prec;
2627 int lineno_cur;
2628 off_t *line_offsets;
2629 FILE *f;
2630 struct got_repository *repo;
2633 static const struct got_error *
2634 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2636 const struct got_error *err = NULL;
2637 struct blame_cb_args *a = arg;
2638 struct blame_line *bline;
2639 char *line = NULL;
2640 size_t linesize = 0;
2641 struct got_commit_object *commit = NULL;
2642 off_t offset;
2643 struct tm tm;
2644 time_t committer_time;
2646 if (nlines != a->nlines ||
2647 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2648 return got_error(GOT_ERR_RANGE);
2650 if (sigint_received)
2651 return got_error(GOT_ERR_ITER_COMPLETED);
2653 if (lineno == -1)
2654 return NULL; /* no change in this commit */
2656 /* Annotate this line. */
2657 bline = &a->lines[lineno - 1];
2658 if (bline->annotated)
2659 return NULL;
2660 err = got_object_id_str(&bline->id_str, id);
2661 if (err)
2662 return err;
2664 err = got_object_open_as_commit(&commit, a->repo, id);
2665 if (err)
2666 goto done;
2668 bline->committer = strdup(got_object_commit_get_committer(commit));
2669 if (bline->committer == NULL) {
2670 err = got_error_from_errno("strdup");
2671 goto done;
2674 committer_time = got_object_commit_get_committer_time(commit);
2675 if (localtime_r(&committer_time, &tm) == NULL)
2676 return got_error_from_errno("localtime_r");
2677 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2678 &tm) >= sizeof(bline->datebuf)) {
2679 err = got_error(GOT_ERR_NO_SPACE);
2680 goto done;
2682 bline->annotated = 1;
2684 /* Print lines annotated so far. */
2685 bline = &a->lines[a->lineno_cur - 1];
2686 if (!bline->annotated)
2687 goto done;
2689 offset = a->line_offsets[a->lineno_cur - 1];
2690 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2691 err = got_error_from_errno("fseeko");
2692 goto done;
2695 while (bline->annotated) {
2696 char *smallerthan, *at, *nl, *committer;
2697 size_t len;
2699 if (getline(&line, &linesize, a->f) == -1) {
2700 if (ferror(a->f))
2701 err = got_error_from_errno("getline");
2702 break;
2705 committer = bline->committer;
2706 smallerthan = strchr(committer, '<');
2707 if (smallerthan && smallerthan[1] != '\0')
2708 committer = smallerthan + 1;
2709 at = strchr(committer, '@');
2710 if (at)
2711 *at = '\0';
2712 len = strlen(committer);
2713 if (len >= 9)
2714 committer[8] = '\0';
2716 nl = strchr(line, '\n');
2717 if (nl)
2718 *nl = '\0';
2719 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2720 bline->id_str, bline->datebuf, committer, line);
2722 a->lineno_cur++;
2723 bline = &a->lines[a->lineno_cur - 1];
2725 done:
2726 if (commit)
2727 got_object_commit_close(commit);
2728 free(line);
2729 return err;
2732 static const struct got_error *
2733 cmd_blame(int argc, char *argv[])
2735 const struct got_error *error;
2736 struct got_repository *repo = NULL;
2737 struct got_worktree *worktree = NULL;
2738 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2739 struct got_object_id *obj_id = NULL;
2740 struct got_object_id *commit_id = NULL;
2741 struct got_blob_object *blob = NULL;
2742 char *commit_id_str = NULL;
2743 struct blame_cb_args bca;
2744 int ch, obj_type, i;
2745 size_t filesize;
2747 memset(&bca, 0, sizeof(bca));
2749 #ifndef PROFILE
2750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2751 NULL) == -1)
2752 err(1, "pledge");
2753 #endif
2755 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2756 switch (ch) {
2757 case 'c':
2758 commit_id_str = optarg;
2759 break;
2760 case 'r':
2761 repo_path = realpath(optarg, NULL);
2762 if (repo_path == NULL)
2763 return got_error_from_errno2("realpath",
2764 optarg);
2765 got_path_strip_trailing_slashes(repo_path);
2766 break;
2767 default:
2768 usage_blame();
2769 /* NOTREACHED */
2773 argc -= optind;
2774 argv += optind;
2776 if (argc == 1)
2777 path = argv[0];
2778 else
2779 usage_blame();
2781 cwd = getcwd(NULL, 0);
2782 if (cwd == NULL) {
2783 error = got_error_from_errno("getcwd");
2784 goto done;
2786 if (repo_path == NULL) {
2787 error = got_worktree_open(&worktree, cwd);
2788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2789 goto done;
2790 else
2791 error = NULL;
2792 if (worktree) {
2793 repo_path =
2794 strdup(got_worktree_get_repo_path(worktree));
2795 if (repo_path == NULL)
2796 error = got_error_from_errno("strdup");
2797 if (error)
2798 goto done;
2799 } else {
2800 repo_path = strdup(cwd);
2801 if (repo_path == NULL) {
2802 error = got_error_from_errno("strdup");
2803 goto done;
2808 error = got_repo_open(&repo, repo_path, NULL);
2809 if (error != NULL)
2810 goto done;
2812 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2813 if (error)
2814 goto done;
2816 if (worktree) {
2817 const char *prefix = got_worktree_get_path_prefix(worktree);
2818 char *p, *worktree_subdir = cwd +
2819 strlen(got_worktree_get_root_path(worktree));
2820 if (asprintf(&p, "%s%s%s%s%s",
2821 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2822 worktree_subdir, worktree_subdir[0] ? "/" : "",
2823 path) == -1) {
2824 error = got_error_from_errno("asprintf");
2825 goto done;
2827 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2828 free(p);
2829 } else {
2830 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2832 if (error)
2833 goto done;
2835 if (commit_id_str == NULL) {
2836 struct got_reference *head_ref;
2837 error = got_ref_open(&head_ref, repo, worktree ?
2838 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2839 if (error != NULL)
2840 goto done;
2841 error = got_ref_resolve(&commit_id, repo, head_ref);
2842 got_ref_close(head_ref);
2843 if (error != NULL)
2844 goto done;
2845 } else {
2846 error = got_repo_match_object_id(&commit_id, NULL,
2847 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2848 if (error)
2849 goto done;
2852 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2853 if (error)
2854 goto done;
2856 error = got_object_get_type(&obj_type, repo, obj_id);
2857 if (error)
2858 goto done;
2860 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2861 error = got_error(GOT_ERR_OBJ_TYPE);
2862 goto done;
2865 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2866 if (error)
2867 goto done;
2868 bca.f = got_opentemp();
2869 if (bca.f == NULL) {
2870 error = got_error_from_errno("got_opentemp");
2871 goto done;
2873 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2874 &bca.line_offsets, bca.f, blob);
2875 if (error || bca.nlines == 0)
2876 goto done;
2878 /* Don't include \n at EOF in the blame line count. */
2879 if (bca.line_offsets[bca.nlines - 1] == filesize)
2880 bca.nlines--;
2882 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2883 if (bca.lines == NULL) {
2884 error = got_error_from_errno("calloc");
2885 goto done;
2887 bca.lineno_cur = 1;
2888 bca.nlines_prec = 0;
2889 i = bca.nlines;
2890 while (i > 0) {
2891 i /= 10;
2892 bca.nlines_prec++;
2894 bca.repo = repo;
2896 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2897 check_cancelled, NULL);
2898 done:
2899 free(in_repo_path);
2900 free(repo_path);
2901 free(cwd);
2902 free(commit_id);
2903 free(obj_id);
2904 if (blob)
2905 got_object_blob_close(blob);
2906 if (worktree)
2907 got_worktree_close(worktree);
2908 if (repo) {
2909 const struct got_error *repo_error;
2910 repo_error = got_repo_close(repo);
2911 if (error == NULL)
2912 error = repo_error;
2914 if (bca.lines) {
2915 for (i = 0; i < bca.nlines; i++) {
2916 struct blame_line *bline = &bca.lines[i];
2917 free(bline->id_str);
2918 free(bline->committer);
2920 free(bca.lines);
2922 free(bca.line_offsets);
2923 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2924 error = got_error_from_errno("fclose");
2925 return error;
2928 __dead static void
2929 usage_tree(void)
2931 fprintf(stderr,
2932 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2933 getprogname());
2934 exit(1);
2937 static void
2938 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2939 const char *root_path)
2941 int is_root_path = (strcmp(path, root_path) == 0);
2942 const char *modestr = "";
2943 mode_t mode = got_tree_entry_get_mode(te);
2945 path += strlen(root_path);
2946 while (path[0] == '/')
2947 path++;
2949 if (got_object_tree_entry_is_submodule(te))
2950 modestr = "$";
2951 else if (S_ISLNK(mode))
2952 modestr = "@";
2953 else if (S_ISDIR(mode))
2954 modestr = "/";
2955 else if (mode & S_IXUSR)
2956 modestr = "*";
2958 printf("%s%s%s%s%s\n", id ? id : "", path,
2959 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2962 static const struct got_error *
2963 print_tree(const char *path, struct got_object_id *commit_id,
2964 int show_ids, int recurse, const char *root_path,
2965 struct got_repository *repo)
2967 const struct got_error *err = NULL;
2968 struct got_object_id *tree_id = NULL;
2969 struct got_tree_object *tree = NULL;
2970 int nentries, i;
2972 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2973 if (err)
2974 goto done;
2976 err = got_object_open_as_tree(&tree, repo, tree_id);
2977 if (err)
2978 goto done;
2979 nentries = got_object_tree_get_nentries(tree);
2980 for (i = 0; i < nentries; i++) {
2981 struct got_tree_entry *te;
2982 char *id = NULL;
2984 if (sigint_received || sigpipe_received)
2985 break;
2987 te = got_object_tree_get_entry(tree, i);
2988 if (show_ids) {
2989 char *id_str;
2990 err = got_object_id_str(&id_str,
2991 got_tree_entry_get_id(te));
2992 if (err)
2993 goto done;
2994 if (asprintf(&id, "%s ", id_str) == -1) {
2995 err = got_error_from_errno("asprintf");
2996 free(id_str);
2997 goto done;
2999 free(id_str);
3001 print_entry(te, id, path, root_path);
3002 free(id);
3004 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3005 char *child_path;
3006 if (asprintf(&child_path, "%s%s%s", path,
3007 path[0] == '/' && path[1] == '\0' ? "" : "/",
3008 got_tree_entry_get_name(te)) == -1) {
3009 err = got_error_from_errno("asprintf");
3010 goto done;
3012 err = print_tree(child_path, commit_id, show_ids, 1,
3013 root_path, repo);
3014 free(child_path);
3015 if (err)
3016 goto done;
3019 done:
3020 if (tree)
3021 got_object_tree_close(tree);
3022 free(tree_id);
3023 return err;
3026 static const struct got_error *
3027 cmd_tree(int argc, char *argv[])
3029 const struct got_error *error;
3030 struct got_repository *repo = NULL;
3031 struct got_worktree *worktree = NULL;
3032 const char *path;
3033 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3034 struct got_object_id *commit_id = NULL;
3035 char *commit_id_str = NULL;
3036 int show_ids = 0, recurse = 0;
3037 int ch;
3039 #ifndef PROFILE
3040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3041 NULL) == -1)
3042 err(1, "pledge");
3043 #endif
3045 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3046 switch (ch) {
3047 case 'c':
3048 commit_id_str = optarg;
3049 break;
3050 case 'r':
3051 repo_path = realpath(optarg, NULL);
3052 if (repo_path == NULL)
3053 return got_error_from_errno2("realpath",
3054 optarg);
3055 got_path_strip_trailing_slashes(repo_path);
3056 break;
3057 case 'i':
3058 show_ids = 1;
3059 break;
3060 case 'R':
3061 recurse = 1;
3062 break;
3063 default:
3064 usage_tree();
3065 /* NOTREACHED */
3069 argc -= optind;
3070 argv += optind;
3072 if (argc == 1)
3073 path = argv[0];
3074 else if (argc > 1)
3075 usage_tree();
3076 else
3077 path = NULL;
3079 cwd = getcwd(NULL, 0);
3080 if (cwd == NULL) {
3081 error = got_error_from_errno("getcwd");
3082 goto done;
3084 if (repo_path == NULL) {
3085 error = got_worktree_open(&worktree, cwd);
3086 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3087 goto done;
3088 else
3089 error = NULL;
3090 if (worktree) {
3091 repo_path =
3092 strdup(got_worktree_get_repo_path(worktree));
3093 if (repo_path == NULL)
3094 error = got_error_from_errno("strdup");
3095 if (error)
3096 goto done;
3097 } else {
3098 repo_path = strdup(cwd);
3099 if (repo_path == NULL) {
3100 error = got_error_from_errno("strdup");
3101 goto done;
3106 error = got_repo_open(&repo, repo_path, NULL);
3107 if (error != NULL)
3108 goto done;
3110 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3111 if (error)
3112 goto done;
3114 if (path == NULL) {
3115 if (worktree) {
3116 char *p, *worktree_subdir = cwd +
3117 strlen(got_worktree_get_root_path(worktree));
3118 if (asprintf(&p, "%s/%s",
3119 got_worktree_get_path_prefix(worktree),
3120 worktree_subdir) == -1) {
3121 error = got_error_from_errno("asprintf");
3122 goto done;
3124 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3125 free(p);
3126 if (error)
3127 goto done;
3128 } else
3129 path = "/";
3131 if (in_repo_path == NULL) {
3132 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3133 if (error != NULL)
3134 goto done;
3137 if (commit_id_str == NULL) {
3138 struct got_reference *head_ref;
3139 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3140 if (error != NULL)
3141 goto done;
3142 error = got_ref_resolve(&commit_id, repo, head_ref);
3143 got_ref_close(head_ref);
3144 if (error != NULL)
3145 goto done;
3146 } else {
3147 error = got_repo_match_object_id(&commit_id, NULL,
3148 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3149 if (error)
3150 goto done;
3153 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3154 in_repo_path, repo);
3155 done:
3156 free(in_repo_path);
3157 free(repo_path);
3158 free(cwd);
3159 free(commit_id);
3160 if (worktree)
3161 got_worktree_close(worktree);
3162 if (repo) {
3163 const struct got_error *repo_error;
3164 repo_error = got_repo_close(repo);
3165 if (error == NULL)
3166 error = repo_error;
3168 return error;
3171 __dead static void
3172 usage_status(void)
3174 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3175 exit(1);
3178 static const struct got_error *
3179 print_status(void *arg, unsigned char status, unsigned char staged_status,
3180 const char *path, struct got_object_id *blob_id,
3181 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3182 int dirfd, const char *de_name)
3184 if (status == staged_status && (status == GOT_STATUS_DELETE))
3185 status = GOT_STATUS_NO_CHANGE;
3186 printf("%c%c %s\n", status, staged_status, path);
3187 return NULL;
3190 static const struct got_error *
3191 cmd_status(int argc, char *argv[])
3193 const struct got_error *error = NULL;
3194 struct got_repository *repo = NULL;
3195 struct got_worktree *worktree = NULL;
3196 char *cwd = NULL;
3197 struct got_pathlist_head paths;
3198 struct got_pathlist_entry *pe;
3199 int ch;
3201 TAILQ_INIT(&paths);
3203 while ((ch = getopt(argc, argv, "")) != -1) {
3204 switch (ch) {
3205 default:
3206 usage_status();
3207 /* NOTREACHED */
3211 argc -= optind;
3212 argv += optind;
3214 #ifndef PROFILE
3215 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3216 NULL) == -1)
3217 err(1, "pledge");
3218 #endif
3219 cwd = getcwd(NULL, 0);
3220 if (cwd == NULL) {
3221 error = got_error_from_errno("getcwd");
3222 goto done;
3225 error = got_worktree_open(&worktree, cwd);
3226 if (error != NULL)
3227 goto done;
3229 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3230 NULL);
3231 if (error != NULL)
3232 goto done;
3234 error = apply_unveil(got_repo_get_path(repo), 1,
3235 got_worktree_get_root_path(worktree));
3236 if (error)
3237 goto done;
3239 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3240 if (error)
3241 goto done;
3243 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3244 check_cancelled, NULL);
3245 done:
3246 TAILQ_FOREACH(pe, &paths, entry)
3247 free((char *)pe->path);
3248 got_pathlist_free(&paths);
3249 free(cwd);
3250 return error;
3253 __dead static void
3254 usage_ref(void)
3256 fprintf(stderr,
3257 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3258 getprogname());
3259 exit(1);
3262 static const struct got_error *
3263 list_refs(struct got_repository *repo)
3265 static const struct got_error *err = NULL;
3266 struct got_reflist_head refs;
3267 struct got_reflist_entry *re;
3269 SIMPLEQ_INIT(&refs);
3270 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3271 if (err)
3272 return err;
3274 SIMPLEQ_FOREACH(re, &refs, entry) {
3275 char *refstr;
3276 refstr = got_ref_to_str(re->ref);
3277 if (refstr == NULL)
3278 return got_error_from_errno("got_ref_to_str");
3279 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3280 free(refstr);
3283 got_ref_list_free(&refs);
3284 return NULL;
3287 static const struct got_error *
3288 delete_ref(struct got_repository *repo, const char *refname)
3290 const struct got_error *err = NULL;
3291 struct got_reference *ref;
3293 err = got_ref_open(&ref, repo, refname, 0);
3294 if (err)
3295 return err;
3297 err = got_ref_delete(ref, repo);
3298 got_ref_close(ref);
3299 return err;
3302 static const struct got_error *
3303 add_ref(struct got_repository *repo, const char *refname, const char *target)
3305 const struct got_error *err = NULL;
3306 struct got_object_id *id;
3307 struct got_reference *ref = NULL;
3310 * Don't let the user create a reference name with a leading '-'.
3311 * While technically a valid reference name, this case is usually
3312 * an unintended typo.
3314 if (refname[0] == '-')
3315 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3317 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3318 repo);
3319 if (err) {
3320 struct got_reference *target_ref;
3322 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3323 return err;
3324 err = got_ref_open(&target_ref, repo, target, 0);
3325 if (err)
3326 return err;
3327 err = got_ref_resolve(&id, repo, target_ref);
3328 got_ref_close(target_ref);
3329 if (err)
3330 return err;
3333 err = got_ref_alloc(&ref, refname, id);
3334 if (err)
3335 goto done;
3337 err = got_ref_write(ref, repo);
3338 done:
3339 if (ref)
3340 got_ref_close(ref);
3341 free(id);
3342 return err;
3345 static const struct got_error *
3346 add_symref(struct got_repository *repo, const char *refname, const char *target)
3348 const struct got_error *err = NULL;
3349 struct got_reference *ref = NULL;
3350 struct got_reference *target_ref = NULL;
3353 * Don't let the user create a reference name with a leading '-'.
3354 * While technically a valid reference name, this case is usually
3355 * an unintended typo.
3357 if (refname[0] == '-')
3358 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3360 err = got_ref_open(&target_ref, repo, target, 0);
3361 if (err)
3362 return err;
3364 err = got_ref_alloc_symref(&ref, refname, target_ref);
3365 if (err)
3366 goto done;
3368 err = got_ref_write(ref, repo);
3369 done:
3370 if (target_ref)
3371 got_ref_close(target_ref);
3372 if (ref)
3373 got_ref_close(ref);
3374 return err;
3377 static const struct got_error *
3378 cmd_ref(int argc, char *argv[])
3380 const struct got_error *error = NULL;
3381 struct got_repository *repo = NULL;
3382 struct got_worktree *worktree = NULL;
3383 char *cwd = NULL, *repo_path = NULL;
3384 int ch, do_list = 0, create_symref = 0;
3385 const char *delref = NULL;
3387 /* TODO: Add -s option for adding symbolic references. */
3388 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3389 switch (ch) {
3390 case 'd':
3391 delref = optarg;
3392 break;
3393 case 'r':
3394 repo_path = realpath(optarg, NULL);
3395 if (repo_path == NULL)
3396 return got_error_from_errno2("realpath",
3397 optarg);
3398 got_path_strip_trailing_slashes(repo_path);
3399 break;
3400 case 'l':
3401 do_list = 1;
3402 break;
3403 case 's':
3404 create_symref = 1;
3405 break;
3406 default:
3407 usage_ref();
3408 /* NOTREACHED */
3412 if (do_list && delref)
3413 errx(1, "-l and -d options are mutually exclusive\n");
3415 argc -= optind;
3416 argv += optind;
3418 if (do_list || delref) {
3419 if (create_symref)
3420 errx(1, "-s option cannot be used together with the "
3421 "-l or -d options");
3422 if (argc > 0)
3423 usage_ref();
3424 } else if (argc != 2)
3425 usage_ref();
3427 #ifndef PROFILE
3428 if (do_list) {
3429 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3430 NULL) == -1)
3431 err(1, "pledge");
3432 } else {
3433 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3434 "sendfd unveil", NULL) == -1)
3435 err(1, "pledge");
3437 #endif
3438 cwd = getcwd(NULL, 0);
3439 if (cwd == NULL) {
3440 error = got_error_from_errno("getcwd");
3441 goto done;
3444 if (repo_path == NULL) {
3445 error = got_worktree_open(&worktree, cwd);
3446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3447 goto done;
3448 else
3449 error = NULL;
3450 if (worktree) {
3451 repo_path =
3452 strdup(got_worktree_get_repo_path(worktree));
3453 if (repo_path == NULL)
3454 error = got_error_from_errno("strdup");
3455 if (error)
3456 goto done;
3457 } else {
3458 repo_path = strdup(cwd);
3459 if (repo_path == NULL) {
3460 error = got_error_from_errno("strdup");
3461 goto done;
3466 error = got_repo_open(&repo, repo_path, NULL);
3467 if (error != NULL)
3468 goto done;
3470 error = apply_unveil(got_repo_get_path(repo), do_list,
3471 worktree ? got_worktree_get_root_path(worktree) : NULL);
3472 if (error)
3473 goto done;
3475 if (do_list)
3476 error = list_refs(repo);
3477 else if (delref)
3478 error = delete_ref(repo, delref);
3479 else if (create_symref)
3480 error = add_symref(repo, argv[0], argv[1]);
3481 else
3482 error = add_ref(repo, argv[0], argv[1]);
3483 done:
3484 if (repo)
3485 got_repo_close(repo);
3486 if (worktree)
3487 got_worktree_close(worktree);
3488 free(cwd);
3489 free(repo_path);
3490 return error;
3493 __dead static void
3494 usage_branch(void)
3496 fprintf(stderr,
3497 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3498 "[name]\n", getprogname());
3499 exit(1);
3502 static const struct got_error *
3503 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3504 struct got_reference *ref)
3506 const struct got_error *err = NULL;
3507 const char *refname, *marker = " ";
3508 char *refstr;
3510 refname = got_ref_get_name(ref);
3511 if (worktree && strcmp(refname,
3512 got_worktree_get_head_ref_name(worktree)) == 0) {
3513 struct got_object_id *id = NULL;
3515 err = got_ref_resolve(&id, repo, ref);
3516 if (err)
3517 return err;
3518 if (got_object_id_cmp(id,
3519 got_worktree_get_base_commit_id(worktree)) == 0)
3520 marker = "* ";
3521 else
3522 marker = "~ ";
3523 free(id);
3526 if (strncmp(refname, "refs/heads/", 11) == 0)
3527 refname += 11;
3528 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3529 refname += 18;
3531 refstr = got_ref_to_str(ref);
3532 if (refstr == NULL)
3533 return got_error_from_errno("got_ref_to_str");
3535 printf("%s%s: %s\n", marker, refname, refstr);
3536 free(refstr);
3537 return NULL;
3540 static const struct got_error *
3541 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3543 const char *refname;
3545 if (worktree == NULL)
3546 return got_error(GOT_ERR_NOT_WORKTREE);
3548 refname = got_worktree_get_head_ref_name(worktree);
3550 if (strncmp(refname, "refs/heads/", 11) == 0)
3551 refname += 11;
3552 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3553 refname += 18;
3555 printf("%s\n", refname);
3557 return NULL;
3560 static const struct got_error *
3561 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3563 static const struct got_error *err = NULL;
3564 struct got_reflist_head refs;
3565 struct got_reflist_entry *re;
3566 struct got_reference *temp_ref = NULL;
3567 int rebase_in_progress, histedit_in_progress;
3569 SIMPLEQ_INIT(&refs);
3571 if (worktree) {
3572 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3573 worktree);
3574 if (err)
3575 return err;
3577 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3578 worktree);
3579 if (err)
3580 return err;
3582 if (rebase_in_progress || histedit_in_progress) {
3583 err = got_ref_open(&temp_ref, repo,
3584 got_worktree_get_head_ref_name(worktree), 0);
3585 if (err)
3586 return err;
3587 list_branch(repo, worktree, temp_ref);
3588 got_ref_close(temp_ref);
3592 err = got_ref_list(&refs, repo, "refs/heads",
3593 got_ref_cmp_by_name, NULL);
3594 if (err)
3595 return err;
3597 SIMPLEQ_FOREACH(re, &refs, entry)
3598 list_branch(repo, worktree, re->ref);
3600 got_ref_list_free(&refs);
3601 return NULL;
3604 static const struct got_error *
3605 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3606 const char *branch_name)
3608 const struct got_error *err = NULL;
3609 struct got_reference *ref = NULL;
3610 char *refname;
3612 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3613 return got_error_from_errno("asprintf");
3615 err = got_ref_open(&ref, repo, refname, 0);
3616 if (err)
3617 goto done;
3619 if (worktree &&
3620 strcmp(got_worktree_get_head_ref_name(worktree),
3621 got_ref_get_name(ref)) == 0) {
3622 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3623 "will not delete this work tree's current branch");
3624 goto done;
3627 err = got_ref_delete(ref, repo);
3628 done:
3629 if (ref)
3630 got_ref_close(ref);
3631 free(refname);
3632 return err;
3635 static const struct got_error *
3636 add_branch(struct got_repository *repo, const char *branch_name,
3637 struct got_object_id *base_commit_id)
3639 const struct got_error *err = NULL;
3640 struct got_reference *ref = NULL;
3641 char *base_refname = NULL, *refname = NULL;
3644 * Don't let the user create a branch name with a leading '-'.
3645 * While technically a valid reference name, this case is usually
3646 * an unintended typo.
3648 if (branch_name[0] == '-')
3649 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3652 err = got_error_from_errno("asprintf");
3653 goto done;
3656 err = got_ref_open(&ref, repo, refname, 0);
3657 if (err == NULL) {
3658 err = got_error(GOT_ERR_BRANCH_EXISTS);
3659 goto done;
3660 } else if (err->code != GOT_ERR_NOT_REF)
3661 goto done;
3663 err = got_ref_alloc(&ref, refname, base_commit_id);
3664 if (err)
3665 goto done;
3667 err = got_ref_write(ref, repo);
3668 done:
3669 if (ref)
3670 got_ref_close(ref);
3671 free(base_refname);
3672 free(refname);
3673 return err;
3676 static const struct got_error *
3677 cmd_branch(int argc, char *argv[])
3679 const struct got_error *error = NULL;
3680 struct got_repository *repo = NULL;
3681 struct got_worktree *worktree = NULL;
3682 char *cwd = NULL, *repo_path = NULL;
3683 int ch, do_list = 0, do_show = 0, do_update = 1;
3684 const char *delref = NULL, *commit_id_arg = NULL;
3685 struct got_reference *ref = NULL;
3686 struct got_pathlist_head paths;
3687 struct got_pathlist_entry *pe;
3688 struct got_object_id *commit_id = NULL;
3689 char *commit_id_str = NULL;
3691 TAILQ_INIT(&paths);
3693 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3694 switch (ch) {
3695 case 'c':
3696 commit_id_arg = optarg;
3697 break;
3698 case 'd':
3699 delref = optarg;
3700 break;
3701 case 'r':
3702 repo_path = realpath(optarg, NULL);
3703 if (repo_path == NULL)
3704 return got_error_from_errno2("realpath",
3705 optarg);
3706 got_path_strip_trailing_slashes(repo_path);
3707 break;
3708 case 'l':
3709 do_list = 1;
3710 break;
3711 case 'n':
3712 do_update = 0;
3713 break;
3714 default:
3715 usage_branch();
3716 /* NOTREACHED */
3720 if (do_list && delref)
3721 errx(1, "-l and -d options are mutually exclusive\n");
3723 argc -= optind;
3724 argv += optind;
3726 if (!do_list && !delref && argc == 0)
3727 do_show = 1;
3729 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3730 errx(1, "-c option can only be used when creating a branch");
3732 if (do_list || delref) {
3733 if (argc > 0)
3734 usage_branch();
3735 } else if (!do_show && argc != 1)
3736 usage_branch();
3738 #ifndef PROFILE
3739 if (do_list || do_show) {
3740 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3741 NULL) == -1)
3742 err(1, "pledge");
3743 } else {
3744 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3745 "sendfd unveil", NULL) == -1)
3746 err(1, "pledge");
3748 #endif
3749 cwd = getcwd(NULL, 0);
3750 if (cwd == NULL) {
3751 error = got_error_from_errno("getcwd");
3752 goto done;
3755 if (repo_path == NULL) {
3756 error = got_worktree_open(&worktree, cwd);
3757 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3758 goto done;
3759 else
3760 error = NULL;
3761 if (worktree) {
3762 repo_path =
3763 strdup(got_worktree_get_repo_path(worktree));
3764 if (repo_path == NULL)
3765 error = got_error_from_errno("strdup");
3766 if (error)
3767 goto done;
3768 } else {
3769 repo_path = strdup(cwd);
3770 if (repo_path == NULL) {
3771 error = got_error_from_errno("strdup");
3772 goto done;
3777 error = got_repo_open(&repo, repo_path, NULL);
3778 if (error != NULL)
3779 goto done;
3781 error = apply_unveil(got_repo_get_path(repo), do_list,
3782 worktree ? got_worktree_get_root_path(worktree) : NULL);
3783 if (error)
3784 goto done;
3786 if (do_show)
3787 error = show_current_branch(repo, worktree);
3788 else if (do_list)
3789 error = list_branches(repo, worktree);
3790 else if (delref)
3791 error = delete_branch(repo, worktree, delref);
3792 else {
3793 if (commit_id_arg == NULL)
3794 commit_id_arg = worktree ?
3795 got_worktree_get_head_ref_name(worktree) :
3796 GOT_REF_HEAD;
3797 error = got_repo_match_object_id(&commit_id, NULL,
3798 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3799 if (error)
3800 goto done;
3801 error = add_branch(repo, argv[0], commit_id);
3802 if (error)
3803 goto done;
3804 if (worktree && do_update) {
3805 int did_something = 0;
3806 char *branch_refname = NULL;
3808 error = got_object_id_str(&commit_id_str, commit_id);
3809 if (error)
3810 goto done;
3811 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3812 worktree);
3813 if (error)
3814 goto done;
3815 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3816 == -1) {
3817 error = got_error_from_errno("asprintf");
3818 goto done;
3820 error = got_ref_open(&ref, repo, branch_refname, 0);
3821 free(branch_refname);
3822 if (error)
3823 goto done;
3824 error = switch_head_ref(ref, commit_id, worktree,
3825 repo);
3826 if (error)
3827 goto done;
3828 error = got_worktree_set_base_commit_id(worktree, repo,
3829 commit_id);
3830 if (error)
3831 goto done;
3832 error = got_worktree_checkout_files(worktree, &paths,
3833 repo, update_progress, &did_something,
3834 check_cancelled, NULL);
3835 if (error)
3836 goto done;
3837 if (did_something)
3838 printf("Updated to commit %s\n", commit_id_str);
3841 done:
3842 if (ref)
3843 got_ref_close(ref);
3844 if (repo)
3845 got_repo_close(repo);
3846 if (worktree)
3847 got_worktree_close(worktree);
3848 free(cwd);
3849 free(repo_path);
3850 free(commit_id);
3851 free(commit_id_str);
3852 TAILQ_FOREACH(pe, &paths, entry)
3853 free((char *)pe->path);
3854 got_pathlist_free(&paths);
3855 return error;
3859 __dead static void
3860 usage_tag(void)
3862 fprintf(stderr,
3863 "usage: %s tag [-c commit] [-r repository] [-l] "
3864 "[-m message] name\n", getprogname());
3865 exit(1);
3868 #if 0
3869 static const struct got_error *
3870 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3872 const struct got_error *err = NULL;
3873 struct got_reflist_entry *re, *se, *new;
3874 struct got_object_id *re_id, *se_id;
3875 struct got_tag_object *re_tag, *se_tag;
3876 time_t re_time, se_time;
3878 SIMPLEQ_FOREACH(re, tags, entry) {
3879 se = SIMPLEQ_FIRST(sorted);
3880 if (se == NULL) {
3881 err = got_reflist_entry_dup(&new, re);
3882 if (err)
3883 return err;
3884 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3885 continue;
3886 } else {
3887 err = got_ref_resolve(&re_id, repo, re->ref);
3888 if (err)
3889 break;
3890 err = got_object_open_as_tag(&re_tag, repo, re_id);
3891 free(re_id);
3892 if (err)
3893 break;
3894 re_time = got_object_tag_get_tagger_time(re_tag);
3895 got_object_tag_close(re_tag);
3898 while (se) {
3899 err = got_ref_resolve(&se_id, repo, re->ref);
3900 if (err)
3901 break;
3902 err = got_object_open_as_tag(&se_tag, repo, se_id);
3903 free(se_id);
3904 if (err)
3905 break;
3906 se_time = got_object_tag_get_tagger_time(se_tag);
3907 got_object_tag_close(se_tag);
3909 if (se_time > re_time) {
3910 err = got_reflist_entry_dup(&new, re);
3911 if (err)
3912 return err;
3913 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3914 break;
3916 se = SIMPLEQ_NEXT(se, entry);
3917 continue;
3920 done:
3921 return err;
3923 #endif
3925 static const struct got_error *
3926 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3928 static const struct got_error *err = NULL;
3929 struct got_reflist_head refs;
3930 struct got_reflist_entry *re;
3932 SIMPLEQ_INIT(&refs);
3934 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3935 if (err)
3936 return err;
3938 SIMPLEQ_FOREACH(re, &refs, entry) {
3939 const char *refname;
3940 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3941 char datebuf[26];
3942 const char *tagger;
3943 time_t tagger_time;
3944 struct got_object_id *id;
3945 struct got_tag_object *tag;
3946 struct got_commit_object *commit = NULL;
3948 refname = got_ref_get_name(re->ref);
3949 if (strncmp(refname, "refs/tags/", 10) != 0)
3950 continue;
3951 refname += 10;
3952 refstr = got_ref_to_str(re->ref);
3953 if (refstr == NULL) {
3954 err = got_error_from_errno("got_ref_to_str");
3955 break;
3957 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3958 free(refstr);
3960 err = got_ref_resolve(&id, repo, re->ref);
3961 if (err)
3962 break;
3963 err = got_object_open_as_tag(&tag, repo, id);
3964 if (err) {
3965 if (err->code != GOT_ERR_OBJ_TYPE) {
3966 free(id);
3967 break;
3969 /* "lightweight" tag */
3970 err = got_object_open_as_commit(&commit, repo, id);
3971 if (err) {
3972 free(id);
3973 break;
3975 tagger = got_object_commit_get_committer(commit);
3976 tagger_time =
3977 got_object_commit_get_committer_time(commit);
3978 err = got_object_id_str(&id_str, id);
3979 free(id);
3980 if (err)
3981 break;
3982 } else {
3983 free(id);
3984 tagger = got_object_tag_get_tagger(tag);
3985 tagger_time = got_object_tag_get_tagger_time(tag);
3986 err = got_object_id_str(&id_str,
3987 got_object_tag_get_object_id(tag));
3988 if (err)
3989 break;
3991 printf("from: %s\n", tagger);
3992 datestr = get_datestr(&tagger_time, datebuf);
3993 if (datestr)
3994 printf("date: %s UTC\n", datestr);
3995 if (commit)
3996 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3997 else {
3998 switch (got_object_tag_get_object_type(tag)) {
3999 case GOT_OBJ_TYPE_BLOB:
4000 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4001 id_str);
4002 break;
4003 case GOT_OBJ_TYPE_TREE:
4004 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4005 id_str);
4006 break;
4007 case GOT_OBJ_TYPE_COMMIT:
4008 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4009 id_str);
4010 break;
4011 case GOT_OBJ_TYPE_TAG:
4012 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4013 id_str);
4014 break;
4015 default:
4016 break;
4019 free(id_str);
4020 if (commit) {
4021 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4022 if (err)
4023 break;
4024 got_object_commit_close(commit);
4025 } else {
4026 tagmsg0 = strdup(got_object_tag_get_message(tag));
4027 got_object_tag_close(tag);
4028 if (tagmsg0 == NULL) {
4029 err = got_error_from_errno("strdup");
4030 break;
4034 tagmsg = tagmsg0;
4035 do {
4036 line = strsep(&tagmsg, "\n");
4037 if (line)
4038 printf(" %s\n", line);
4039 } while (line);
4040 free(tagmsg0);
4043 got_ref_list_free(&refs);
4044 return NULL;
4047 static const struct got_error *
4048 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4049 const char *tag_name, const char *repo_path)
4051 const struct got_error *err = NULL;
4052 char *template = NULL, *initial_content = NULL;
4053 char *editor = NULL;
4054 int fd = -1;
4056 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4057 err = got_error_from_errno("asprintf");
4058 goto done;
4061 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4062 commit_id_str, tag_name) == -1) {
4063 err = got_error_from_errno("asprintf");
4064 goto done;
4067 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4068 if (err)
4069 goto done;
4071 dprintf(fd, initial_content);
4072 close(fd);
4074 err = get_editor(&editor);
4075 if (err)
4076 goto done;
4077 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4078 done:
4079 free(initial_content);
4080 free(template);
4081 free(editor);
4083 /* Editor is done; we can now apply unveil(2) */
4084 if (err == NULL) {
4085 err = apply_unveil(repo_path, 0, NULL);
4086 if (err) {
4087 free(*tagmsg);
4088 *tagmsg = NULL;
4091 return err;
4094 static const struct got_error *
4095 add_tag(struct got_repository *repo, const char *tag_name,
4096 const char *commit_arg, const char *tagmsg_arg)
4098 const struct got_error *err = NULL;
4099 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4100 char *label = NULL, *commit_id_str = NULL;
4101 struct got_reference *ref = NULL;
4102 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4103 char *tagmsg_path = NULL, *tag_id_str = NULL;
4104 int preserve_tagmsg = 0;
4107 * Don't let the user create a tag name with a leading '-'.
4108 * While technically a valid reference name, this case is usually
4109 * an unintended typo.
4111 if (tag_name[0] == '-')
4112 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4114 err = get_author(&tagger, repo);
4115 if (err)
4116 return err;
4118 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4119 GOT_OBJ_TYPE_COMMIT, 1, repo);
4120 if (err)
4121 goto done;
4123 err = got_object_id_str(&commit_id_str, commit_id);
4124 if (err)
4125 goto done;
4127 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4128 refname = strdup(tag_name);
4129 if (refname == NULL) {
4130 err = got_error_from_errno("strdup");
4131 goto done;
4133 tag_name += 10;
4134 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4135 err = got_error_from_errno("asprintf");
4136 goto done;
4139 err = got_ref_open(&ref, repo, refname, 0);
4140 if (err == NULL) {
4141 err = got_error(GOT_ERR_TAG_EXISTS);
4142 goto done;
4143 } else if (err->code != GOT_ERR_NOT_REF)
4144 goto done;
4146 if (tagmsg_arg == NULL) {
4147 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4148 tag_name, got_repo_get_path(repo));
4149 if (err) {
4150 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4151 tagmsg_path != NULL)
4152 preserve_tagmsg = 1;
4153 goto done;
4157 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4158 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4159 if (err) {
4160 if (tagmsg_path)
4161 preserve_tagmsg = 1;
4162 goto done;
4165 err = got_ref_alloc(&ref, refname, tag_id);
4166 if (err) {
4167 if (tagmsg_path)
4168 preserve_tagmsg = 1;
4169 goto done;
4172 err = got_ref_write(ref, repo);
4173 if (err) {
4174 if (tagmsg_path)
4175 preserve_tagmsg = 1;
4176 goto done;
4179 err = got_object_id_str(&tag_id_str, tag_id);
4180 if (err) {
4181 if (tagmsg_path)
4182 preserve_tagmsg = 1;
4183 goto done;
4185 printf("Created tag %s\n", tag_id_str);
4186 done:
4187 if (preserve_tagmsg) {
4188 fprintf(stderr, "%s: tag message preserved in %s\n",
4189 getprogname(), tagmsg_path);
4190 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4191 err = got_error_from_errno2("unlink", tagmsg_path);
4192 free(tag_id_str);
4193 if (ref)
4194 got_ref_close(ref);
4195 free(commit_id);
4196 free(commit_id_str);
4197 free(refname);
4198 free(tagmsg);
4199 free(tagmsg_path);
4200 free(tagger);
4201 return err;
4204 static const struct got_error *
4205 cmd_tag(int argc, char *argv[])
4207 const struct got_error *error = NULL;
4208 struct got_repository *repo = NULL;
4209 struct got_worktree *worktree = NULL;
4210 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4211 char *gitconfig_path = NULL;
4212 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4213 int ch, do_list = 0;
4215 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4216 switch (ch) {
4217 case 'c':
4218 commit_id_arg = optarg;
4219 break;
4220 case 'm':
4221 tagmsg = optarg;
4222 break;
4223 case 'r':
4224 repo_path = realpath(optarg, NULL);
4225 if (repo_path == NULL)
4226 return got_error_from_errno2("realpath",
4227 optarg);
4228 got_path_strip_trailing_slashes(repo_path);
4229 break;
4230 case 'l':
4231 do_list = 1;
4232 break;
4233 default:
4234 usage_tag();
4235 /* NOTREACHED */
4239 argc -= optind;
4240 argv += optind;
4242 if (do_list) {
4243 if (commit_id_arg != NULL)
4244 errx(1, "-c option can only be used when creating a tag");
4245 if (tagmsg)
4246 errx(1, "-l and -m options are mutually exclusive");
4247 if (argc > 0)
4248 usage_tag();
4249 } else if (argc != 1)
4250 usage_tag();
4252 tag_name = argv[0];
4254 #ifndef PROFILE
4255 if (do_list) {
4256 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4257 NULL) == -1)
4258 err(1, "pledge");
4259 } else {
4260 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 "sendfd unveil", NULL) == -1)
4262 err(1, "pledge");
4264 #endif
4265 cwd = getcwd(NULL, 0);
4266 if (cwd == NULL) {
4267 error = got_error_from_errno("getcwd");
4268 goto done;
4271 if (repo_path == NULL) {
4272 error = got_worktree_open(&worktree, cwd);
4273 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4274 goto done;
4275 else
4276 error = NULL;
4277 if (worktree) {
4278 repo_path =
4279 strdup(got_worktree_get_repo_path(worktree));
4280 if (repo_path == NULL)
4281 error = got_error_from_errno("strdup");
4282 if (error)
4283 goto done;
4284 } else {
4285 repo_path = strdup(cwd);
4286 if (repo_path == NULL) {
4287 error = got_error_from_errno("strdup");
4288 goto done;
4293 if (do_list) {
4294 error = got_repo_open(&repo, repo_path, NULL);
4295 if (error != NULL)
4296 goto done;
4297 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4298 if (error)
4299 goto done;
4300 error = list_tags(repo, worktree);
4301 } else {
4302 error = get_gitconfig_path(&gitconfig_path);
4303 if (error)
4304 goto done;
4305 error = got_repo_open(&repo, repo_path, gitconfig_path);
4306 if (error != NULL)
4307 goto done;
4309 if (tagmsg) {
4310 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4311 if (error)
4312 goto done;
4315 if (commit_id_arg == NULL) {
4316 struct got_reference *head_ref;
4317 struct got_object_id *commit_id;
4318 error = got_ref_open(&head_ref, repo,
4319 worktree ? got_worktree_get_head_ref_name(worktree)
4320 : GOT_REF_HEAD, 0);
4321 if (error)
4322 goto done;
4323 error = got_ref_resolve(&commit_id, repo, head_ref);
4324 got_ref_close(head_ref);
4325 if (error)
4326 goto done;
4327 error = got_object_id_str(&commit_id_str, commit_id);
4328 free(commit_id);
4329 if (error)
4330 goto done;
4333 error = add_tag(repo, tag_name,
4334 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4336 done:
4337 if (repo)
4338 got_repo_close(repo);
4339 if (worktree)
4340 got_worktree_close(worktree);
4341 free(cwd);
4342 free(repo_path);
4343 free(gitconfig_path);
4344 free(commit_id_str);
4345 return error;
4348 __dead static void
4349 usage_add(void)
4351 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4352 getprogname());
4353 exit(1);
4356 static const struct got_error *
4357 add_progress(void *arg, unsigned char status, const char *path)
4359 while (path[0] == '/')
4360 path++;
4361 printf("%c %s\n", status, path);
4362 return NULL;
4365 static const struct got_error *
4366 cmd_add(int argc, char *argv[])
4368 const struct got_error *error = NULL;
4369 struct got_repository *repo = NULL;
4370 struct got_worktree *worktree = NULL;
4371 char *cwd = NULL;
4372 struct got_pathlist_head paths;
4373 struct got_pathlist_entry *pe;
4374 int ch, can_recurse = 0, no_ignores = 0;
4376 TAILQ_INIT(&paths);
4378 while ((ch = getopt(argc, argv, "IR")) != -1) {
4379 switch (ch) {
4380 case 'I':
4381 no_ignores = 1;
4382 break;
4383 case 'R':
4384 can_recurse = 1;
4385 break;
4386 default:
4387 usage_add();
4388 /* NOTREACHED */
4392 argc -= optind;
4393 argv += optind;
4395 #ifndef PROFILE
4396 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4397 NULL) == -1)
4398 err(1, "pledge");
4399 #endif
4400 if (argc < 1)
4401 usage_add();
4403 cwd = getcwd(NULL, 0);
4404 if (cwd == NULL) {
4405 error = got_error_from_errno("getcwd");
4406 goto done;
4409 error = got_worktree_open(&worktree, cwd);
4410 if (error)
4411 goto done;
4413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4414 NULL);
4415 if (error != NULL)
4416 goto done;
4418 error = apply_unveil(got_repo_get_path(repo), 1,
4419 got_worktree_get_root_path(worktree));
4420 if (error)
4421 goto done;
4423 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4424 if (error)
4425 goto done;
4427 if (!can_recurse && no_ignores) {
4428 error = got_error_msg(GOT_ERR_BAD_PATH,
4429 "disregarding ignores requires -R option");
4430 goto done;
4434 if (!can_recurse) {
4435 char *ondisk_path;
4436 struct stat sb;
4437 TAILQ_FOREACH(pe, &paths, entry) {
4438 if (asprintf(&ondisk_path, "%s/%s",
4439 got_worktree_get_root_path(worktree),
4440 pe->path) == -1) {
4441 error = got_error_from_errno("asprintf");
4442 goto done;
4444 if (lstat(ondisk_path, &sb) == -1) {
4445 if (errno == ENOENT) {
4446 free(ondisk_path);
4447 continue;
4449 error = got_error_from_errno2("lstat",
4450 ondisk_path);
4451 free(ondisk_path);
4452 goto done;
4454 free(ondisk_path);
4455 if (S_ISDIR(sb.st_mode)) {
4456 error = got_error_msg(GOT_ERR_BAD_PATH,
4457 "adding directories requires -R option");
4458 goto done;
4463 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4464 NULL, repo, no_ignores);
4465 done:
4466 if (repo)
4467 got_repo_close(repo);
4468 if (worktree)
4469 got_worktree_close(worktree);
4470 TAILQ_FOREACH(pe, &paths, entry)
4471 free((char *)pe->path);
4472 got_pathlist_free(&paths);
4473 free(cwd);
4474 return error;
4477 __dead static void
4478 usage_remove(void)
4480 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4481 getprogname());
4482 exit(1);
4485 static const struct got_error *
4486 print_remove_status(void *arg, unsigned char status,
4487 unsigned char staged_status, const char *path)
4489 while (path[0] == '/')
4490 path++;
4491 if (status == GOT_STATUS_NONEXISTENT)
4492 return NULL;
4493 if (status == staged_status && (status == GOT_STATUS_DELETE))
4494 status = GOT_STATUS_NO_CHANGE;
4495 printf("%c%c %s\n", status, staged_status, path);
4496 return NULL;
4499 static const struct got_error *
4500 cmd_remove(int argc, char *argv[])
4502 const struct got_error *error = NULL;
4503 struct got_worktree *worktree = NULL;
4504 struct got_repository *repo = NULL;
4505 char *cwd = NULL;
4506 struct got_pathlist_head paths;
4507 struct got_pathlist_entry *pe;
4508 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4510 TAILQ_INIT(&paths);
4512 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4513 switch (ch) {
4514 case 'f':
4515 delete_local_mods = 1;
4516 break;
4517 case 'k':
4518 keep_on_disk = 1;
4519 break;
4520 case 'R':
4521 can_recurse = 1;
4522 break;
4523 default:
4524 usage_remove();
4525 /* NOTREACHED */
4529 argc -= optind;
4530 argv += optind;
4532 #ifndef PROFILE
4533 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4534 NULL) == -1)
4535 err(1, "pledge");
4536 #endif
4537 if (argc < 1)
4538 usage_remove();
4540 cwd = getcwd(NULL, 0);
4541 if (cwd == NULL) {
4542 error = got_error_from_errno("getcwd");
4543 goto done;
4545 error = got_worktree_open(&worktree, cwd);
4546 if (error)
4547 goto done;
4549 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4550 NULL);
4551 if (error)
4552 goto done;
4554 error = apply_unveil(got_repo_get_path(repo), 1,
4555 got_worktree_get_root_path(worktree));
4556 if (error)
4557 goto done;
4559 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4560 if (error)
4561 goto done;
4563 if (!can_recurse) {
4564 char *ondisk_path;
4565 struct stat sb;
4566 TAILQ_FOREACH(pe, &paths, entry) {
4567 if (asprintf(&ondisk_path, "%s/%s",
4568 got_worktree_get_root_path(worktree),
4569 pe->path) == -1) {
4570 error = got_error_from_errno("asprintf");
4571 goto done;
4573 if (lstat(ondisk_path, &sb) == -1) {
4574 if (errno == ENOENT) {
4575 free(ondisk_path);
4576 continue;
4578 error = got_error_from_errno2("lstat",
4579 ondisk_path);
4580 free(ondisk_path);
4581 goto done;
4583 free(ondisk_path);
4584 if (S_ISDIR(sb.st_mode)) {
4585 error = got_error_msg(GOT_ERR_BAD_PATH,
4586 "removing directories requires -R option");
4587 goto done;
4592 error = got_worktree_schedule_delete(worktree, &paths,
4593 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4594 done:
4595 if (repo)
4596 got_repo_close(repo);
4597 if (worktree)
4598 got_worktree_close(worktree);
4599 TAILQ_FOREACH(pe, &paths, entry)
4600 free((char *)pe->path);
4601 got_pathlist_free(&paths);
4602 free(cwd);
4603 return error;
4606 __dead static void
4607 usage_revert(void)
4609 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4610 "path ...\n", getprogname());
4611 exit(1);
4614 static const struct got_error *
4615 revert_progress(void *arg, unsigned char status, const char *path)
4617 if (status == GOT_STATUS_UNVERSIONED)
4618 return NULL;
4620 while (path[0] == '/')
4621 path++;
4622 printf("%c %s\n", status, path);
4623 return NULL;
4626 struct choose_patch_arg {
4627 FILE *patch_script_file;
4628 const char *action;
4631 static const struct got_error *
4632 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4633 int nchanges, const char *action)
4635 char *line = NULL;
4636 size_t linesize = 0;
4637 ssize_t linelen;
4639 switch (status) {
4640 case GOT_STATUS_ADD:
4641 printf("A %s\n%s this addition? [y/n] ", path, action);
4642 break;
4643 case GOT_STATUS_DELETE:
4644 printf("D %s\n%s this deletion? [y/n] ", path, action);
4645 break;
4646 case GOT_STATUS_MODIFY:
4647 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4648 return got_error_from_errno("fseek");
4649 printf(GOT_COMMIT_SEP_STR);
4650 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4651 printf("%s", line);
4652 if (ferror(patch_file))
4653 return got_error_from_errno("getline");
4654 printf(GOT_COMMIT_SEP_STR);
4655 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4656 path, n, nchanges, action);
4657 break;
4658 default:
4659 return got_error_path(path, GOT_ERR_FILE_STATUS);
4662 return NULL;
4665 static const struct got_error *
4666 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4667 FILE *patch_file, int n, int nchanges)
4669 const struct got_error *err = NULL;
4670 char *line = NULL;
4671 size_t linesize = 0;
4672 ssize_t linelen;
4673 int resp = ' ';
4674 struct choose_patch_arg *a = arg;
4676 *choice = GOT_PATCH_CHOICE_NONE;
4678 if (a->patch_script_file) {
4679 char *nl;
4680 err = show_change(status, path, patch_file, n, nchanges,
4681 a->action);
4682 if (err)
4683 return err;
4684 linelen = getline(&line, &linesize, a->patch_script_file);
4685 if (linelen == -1) {
4686 if (ferror(a->patch_script_file))
4687 return got_error_from_errno("getline");
4688 return NULL;
4690 nl = strchr(line, '\n');
4691 if (nl)
4692 *nl = '\0';
4693 if (strcmp(line, "y") == 0) {
4694 *choice = GOT_PATCH_CHOICE_YES;
4695 printf("y\n");
4696 } else if (strcmp(line, "n") == 0) {
4697 *choice = GOT_PATCH_CHOICE_NO;
4698 printf("n\n");
4699 } else if (strcmp(line, "q") == 0 &&
4700 status == GOT_STATUS_MODIFY) {
4701 *choice = GOT_PATCH_CHOICE_QUIT;
4702 printf("q\n");
4703 } else
4704 printf("invalid response '%s'\n", line);
4705 free(line);
4706 return NULL;
4709 while (resp != 'y' && resp != 'n' && resp != 'q') {
4710 err = show_change(status, path, patch_file, n, nchanges,
4711 a->action);
4712 if (err)
4713 return err;
4714 resp = getchar();
4715 if (resp == '\n')
4716 resp = getchar();
4717 if (status == GOT_STATUS_MODIFY) {
4718 if (resp != 'y' && resp != 'n' && resp != 'q') {
4719 printf("invalid response '%c'\n", resp);
4720 resp = ' ';
4722 } else if (resp != 'y' && resp != 'n') {
4723 printf("invalid response '%c'\n", resp);
4724 resp = ' ';
4728 if (resp == 'y')
4729 *choice = GOT_PATCH_CHOICE_YES;
4730 else if (resp == 'n')
4731 *choice = GOT_PATCH_CHOICE_NO;
4732 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4733 *choice = GOT_PATCH_CHOICE_QUIT;
4735 return NULL;
4739 static const struct got_error *
4740 cmd_revert(int argc, char *argv[])
4742 const struct got_error *error = NULL;
4743 struct got_worktree *worktree = NULL;
4744 struct got_repository *repo = NULL;
4745 char *cwd = NULL, *path = NULL;
4746 struct got_pathlist_head paths;
4747 struct got_pathlist_entry *pe;
4748 int ch, can_recurse = 0, pflag = 0;
4749 FILE *patch_script_file = NULL;
4750 const char *patch_script_path = NULL;
4751 struct choose_patch_arg cpa;
4753 TAILQ_INIT(&paths);
4755 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4756 switch (ch) {
4757 case 'p':
4758 pflag = 1;
4759 break;
4760 case 'F':
4761 patch_script_path = optarg;
4762 break;
4763 case 'R':
4764 can_recurse = 1;
4765 break;
4766 default:
4767 usage_revert();
4768 /* NOTREACHED */
4772 argc -= optind;
4773 argv += optind;
4775 #ifndef PROFILE
4776 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4777 "unveil", NULL) == -1)
4778 err(1, "pledge");
4779 #endif
4780 if (argc < 1)
4781 usage_revert();
4782 if (patch_script_path && !pflag)
4783 errx(1, "-F option can only be used together with -p option");
4785 cwd = getcwd(NULL, 0);
4786 if (cwd == NULL) {
4787 error = got_error_from_errno("getcwd");
4788 goto done;
4790 error = got_worktree_open(&worktree, cwd);
4791 if (error)
4792 goto done;
4794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4795 NULL);
4796 if (error != NULL)
4797 goto done;
4799 if (patch_script_path) {
4800 patch_script_file = fopen(patch_script_path, "r");
4801 if (patch_script_file == NULL) {
4802 error = got_error_from_errno2("fopen",
4803 patch_script_path);
4804 goto done;
4807 error = apply_unveil(got_repo_get_path(repo), 1,
4808 got_worktree_get_root_path(worktree));
4809 if (error)
4810 goto done;
4812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4813 if (error)
4814 goto done;
4816 if (!can_recurse) {
4817 char *ondisk_path;
4818 struct stat sb;
4819 TAILQ_FOREACH(pe, &paths, entry) {
4820 if (asprintf(&ondisk_path, "%s/%s",
4821 got_worktree_get_root_path(worktree),
4822 pe->path) == -1) {
4823 error = got_error_from_errno("asprintf");
4824 goto done;
4826 if (lstat(ondisk_path, &sb) == -1) {
4827 if (errno == ENOENT) {
4828 free(ondisk_path);
4829 continue;
4831 error = got_error_from_errno2("lstat",
4832 ondisk_path);
4833 free(ondisk_path);
4834 goto done;
4836 free(ondisk_path);
4837 if (S_ISDIR(sb.st_mode)) {
4838 error = got_error_msg(GOT_ERR_BAD_PATH,
4839 "reverting directories requires -R option");
4840 goto done;
4845 cpa.patch_script_file = patch_script_file;
4846 cpa.action = "revert";
4847 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4848 pflag ? choose_patch : NULL, &cpa, repo);
4849 done:
4850 if (patch_script_file && fclose(patch_script_file) == EOF &&
4851 error == NULL)
4852 error = got_error_from_errno2("fclose", patch_script_path);
4853 if (repo)
4854 got_repo_close(repo);
4855 if (worktree)
4856 got_worktree_close(worktree);
4857 free(path);
4858 free(cwd);
4859 return error;
4862 __dead static void
4863 usage_commit(void)
4865 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4866 getprogname());
4867 exit(1);
4870 struct collect_commit_logmsg_arg {
4871 const char *cmdline_log;
4872 const char *editor;
4873 const char *worktree_path;
4874 const char *branch_name;
4875 const char *repo_path;
4876 char *logmsg_path;
4880 static const struct got_error *
4881 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4882 void *arg)
4884 char *initial_content = NULL;
4885 struct got_pathlist_entry *pe;
4886 const struct got_error *err = NULL;
4887 char *template = NULL;
4888 struct collect_commit_logmsg_arg *a = arg;
4889 int fd;
4890 size_t len;
4892 /* if a message was specified on the command line, just use it */
4893 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4894 len = strlen(a->cmdline_log) + 1;
4895 *logmsg = malloc(len + 1);
4896 if (*logmsg == NULL)
4897 return got_error_from_errno("malloc");
4898 strlcpy(*logmsg, a->cmdline_log, len);
4899 return NULL;
4902 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4903 return got_error_from_errno("asprintf");
4905 if (asprintf(&initial_content,
4906 "\n# changes to be committed on branch %s:\n",
4907 a->branch_name) == -1)
4908 return got_error_from_errno("asprintf");
4910 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4911 if (err)
4912 goto done;
4914 dprintf(fd, initial_content);
4916 TAILQ_FOREACH(pe, commitable_paths, entry) {
4917 struct got_commitable *ct = pe->data;
4918 dprintf(fd, "# %c %s\n",
4919 got_commitable_get_status(ct),
4920 got_commitable_get_path(ct));
4922 close(fd);
4924 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4925 done:
4926 free(initial_content);
4927 free(template);
4929 /* Editor is done; we can now apply unveil(2) */
4930 if (err == NULL) {
4931 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4932 if (err) {
4933 free(*logmsg);
4934 *logmsg = NULL;
4937 return err;
4940 static const struct got_error *
4941 cmd_commit(int argc, char *argv[])
4943 const struct got_error *error = NULL;
4944 struct got_worktree *worktree = NULL;
4945 struct got_repository *repo = NULL;
4946 char *cwd = NULL, *id_str = NULL;
4947 struct got_object_id *id = NULL;
4948 const char *logmsg = NULL;
4949 struct collect_commit_logmsg_arg cl_arg;
4950 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4951 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4952 struct got_pathlist_head paths;
4954 TAILQ_INIT(&paths);
4955 cl_arg.logmsg_path = NULL;
4957 while ((ch = getopt(argc, argv, "m:")) != -1) {
4958 switch (ch) {
4959 case 'm':
4960 logmsg = optarg;
4961 break;
4962 default:
4963 usage_commit();
4964 /* NOTREACHED */
4968 argc -= optind;
4969 argv += optind;
4971 #ifndef PROFILE
4972 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4973 "unveil", NULL) == -1)
4974 err(1, "pledge");
4975 #endif
4976 cwd = getcwd(NULL, 0);
4977 if (cwd == NULL) {
4978 error = got_error_from_errno("getcwd");
4979 goto done;
4981 error = got_worktree_open(&worktree, cwd);
4982 if (error)
4983 goto done;
4985 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4986 if (error)
4987 goto done;
4988 if (rebase_in_progress) {
4989 error = got_error(GOT_ERR_REBASING);
4990 goto done;
4993 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4994 worktree);
4995 if (error)
4996 goto done;
4998 error = get_gitconfig_path(&gitconfig_path);
4999 if (error)
5000 goto done;
5001 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5002 gitconfig_path);
5003 if (error != NULL)
5004 goto done;
5006 error = get_author(&author, repo);
5007 if (error)
5008 return error;
5011 * unveil(2) traverses exec(2); if an editor is used we have
5012 * to apply unveil after the log message has been written.
5014 if (logmsg == NULL || strlen(logmsg) == 0)
5015 error = get_editor(&editor);
5016 else
5017 error = apply_unveil(got_repo_get_path(repo), 0,
5018 got_worktree_get_root_path(worktree));
5019 if (error)
5020 goto done;
5022 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5023 if (error)
5024 goto done;
5026 cl_arg.editor = editor;
5027 cl_arg.cmdline_log = logmsg;
5028 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5029 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5030 if (!histedit_in_progress) {
5031 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5032 error = got_error(GOT_ERR_COMMIT_BRANCH);
5033 goto done;
5035 cl_arg.branch_name += 11;
5037 cl_arg.repo_path = got_repo_get_path(repo);
5038 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5039 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5040 if (error) {
5041 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5042 cl_arg.logmsg_path != NULL)
5043 preserve_logmsg = 1;
5044 goto done;
5047 error = got_object_id_str(&id_str, id);
5048 if (error)
5049 goto done;
5050 printf("Created commit %s\n", id_str);
5051 done:
5052 if (preserve_logmsg) {
5053 fprintf(stderr, "%s: log message preserved in %s\n",
5054 getprogname(), cl_arg.logmsg_path);
5055 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5056 error == NULL)
5057 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5058 free(cl_arg.logmsg_path);
5059 if (repo)
5060 got_repo_close(repo);
5061 if (worktree)
5062 got_worktree_close(worktree);
5063 free(cwd);
5064 free(id_str);
5065 free(gitconfig_path);
5066 free(editor);
5067 free(author);
5068 return error;
5071 __dead static void
5072 usage_cherrypick(void)
5074 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5075 exit(1);
5078 static const struct got_error *
5079 cmd_cherrypick(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_repository *repo = NULL;
5084 char *cwd = NULL, *commit_id_str = NULL;
5085 struct got_object_id *commit_id = NULL;
5086 struct got_commit_object *commit = NULL;
5087 struct got_object_qid *pid;
5088 struct got_reference *head_ref = NULL;
5089 int ch, did_something = 0;
5091 while ((ch = getopt(argc, argv, "")) != -1) {
5092 switch (ch) {
5093 default:
5094 usage_cherrypick();
5095 /* NOTREACHED */
5099 argc -= optind;
5100 argv += optind;
5102 #ifndef PROFILE
5103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 "unveil", NULL) == -1)
5105 err(1, "pledge");
5106 #endif
5107 if (argc != 1)
5108 usage_cherrypick();
5110 cwd = getcwd(NULL, 0);
5111 if (cwd == NULL) {
5112 error = got_error_from_errno("getcwd");
5113 goto done;
5115 error = got_worktree_open(&worktree, cwd);
5116 if (error)
5117 goto done;
5119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5120 NULL);
5121 if (error != NULL)
5122 goto done;
5124 error = apply_unveil(got_repo_get_path(repo), 0,
5125 got_worktree_get_root_path(worktree));
5126 if (error)
5127 goto done;
5129 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 GOT_OBJ_TYPE_COMMIT, repo);
5131 if (error != NULL) {
5132 struct got_reference *ref;
5133 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5134 goto done;
5135 error = got_ref_open(&ref, repo, argv[0], 0);
5136 if (error != NULL)
5137 goto done;
5138 error = got_ref_resolve(&commit_id, repo, ref);
5139 got_ref_close(ref);
5140 if (error != NULL)
5141 goto done;
5143 error = got_object_id_str(&commit_id_str, commit_id);
5144 if (error)
5145 goto done;
5147 error = got_ref_open(&head_ref, repo,
5148 got_worktree_get_head_ref_name(worktree), 0);
5149 if (error != NULL)
5150 goto done;
5152 error = check_same_branch(commit_id, head_ref, NULL, repo);
5153 if (error) {
5154 if (error->code != GOT_ERR_ANCESTRY)
5155 goto done;
5156 error = NULL;
5157 } else {
5158 error = got_error(GOT_ERR_SAME_BRANCH);
5159 goto done;
5162 error = got_object_open_as_commit(&commit, repo, commit_id);
5163 if (error)
5164 goto done;
5165 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5166 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5167 commit_id, repo, update_progress, &did_something, check_cancelled,
5168 NULL);
5169 if (error != NULL)
5170 goto done;
5172 if (did_something)
5173 printf("Merged commit %s\n", commit_id_str);
5174 done:
5175 if (commit)
5176 got_object_commit_close(commit);
5177 free(commit_id_str);
5178 if (head_ref)
5179 got_ref_close(head_ref);
5180 if (worktree)
5181 got_worktree_close(worktree);
5182 if (repo)
5183 got_repo_close(repo);
5184 return error;
5187 __dead static void
5188 usage_backout(void)
5190 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5191 exit(1);
5194 static const struct got_error *
5195 cmd_backout(int argc, char *argv[])
5197 const struct got_error *error = NULL;
5198 struct got_worktree *worktree = NULL;
5199 struct got_repository *repo = NULL;
5200 char *cwd = NULL, *commit_id_str = NULL;
5201 struct got_object_id *commit_id = NULL;
5202 struct got_commit_object *commit = NULL;
5203 struct got_object_qid *pid;
5204 struct got_reference *head_ref = NULL;
5205 int ch, did_something = 0;
5207 while ((ch = getopt(argc, argv, "")) != -1) {
5208 switch (ch) {
5209 default:
5210 usage_backout();
5211 /* NOTREACHED */
5215 argc -= optind;
5216 argv += optind;
5218 #ifndef PROFILE
5219 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5220 "unveil", NULL) == -1)
5221 err(1, "pledge");
5222 #endif
5223 if (argc != 1)
5224 usage_backout();
5226 cwd = getcwd(NULL, 0);
5227 if (cwd == NULL) {
5228 error = got_error_from_errno("getcwd");
5229 goto done;
5231 error = got_worktree_open(&worktree, cwd);
5232 if (error)
5233 goto done;
5235 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5236 NULL);
5237 if (error != NULL)
5238 goto done;
5240 error = apply_unveil(got_repo_get_path(repo), 0,
5241 got_worktree_get_root_path(worktree));
5242 if (error)
5243 goto done;
5245 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5246 GOT_OBJ_TYPE_COMMIT, repo);
5247 if (error != NULL) {
5248 struct got_reference *ref;
5249 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5250 goto done;
5251 error = got_ref_open(&ref, repo, argv[0], 0);
5252 if (error != NULL)
5253 goto done;
5254 error = got_ref_resolve(&commit_id, repo, ref);
5255 got_ref_close(ref);
5256 if (error != NULL)
5257 goto done;
5259 error = got_object_id_str(&commit_id_str, commit_id);
5260 if (error)
5261 goto done;
5263 error = got_ref_open(&head_ref, repo,
5264 got_worktree_get_head_ref_name(worktree), 0);
5265 if (error != NULL)
5266 goto done;
5268 error = check_same_branch(commit_id, head_ref, NULL, repo);
5269 if (error)
5270 goto done;
5272 error = got_object_open_as_commit(&commit, repo, commit_id);
5273 if (error)
5274 goto done;
5275 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5276 if (pid == NULL) {
5277 error = got_error(GOT_ERR_ROOT_COMMIT);
5278 goto done;
5281 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5282 update_progress, &did_something, check_cancelled, NULL);
5283 if (error != NULL)
5284 goto done;
5286 if (did_something)
5287 printf("Backed out commit %s\n", commit_id_str);
5288 done:
5289 if (commit)
5290 got_object_commit_close(commit);
5291 free(commit_id_str);
5292 if (head_ref)
5293 got_ref_close(head_ref);
5294 if (worktree)
5295 got_worktree_close(worktree);
5296 if (repo)
5297 got_repo_close(repo);
5298 return error;
5301 __dead static void
5302 usage_rebase(void)
5304 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5305 getprogname());
5306 exit(1);
5309 void
5310 trim_logmsg(char *logmsg, int limit)
5312 char *nl;
5313 size_t len;
5315 len = strlen(logmsg);
5316 if (len > limit)
5317 len = limit;
5318 logmsg[len] = '\0';
5319 nl = strchr(logmsg, '\n');
5320 if (nl)
5321 *nl = '\0';
5324 static const struct got_error *
5325 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5327 const struct got_error *err;
5328 char *logmsg0 = NULL;
5329 const char *s;
5331 err = got_object_commit_get_logmsg(&logmsg0, commit);
5332 if (err)
5333 return err;
5335 s = logmsg0;
5336 while (isspace((unsigned char)s[0]))
5337 s++;
5339 *logmsg = strdup(s);
5340 if (*logmsg == NULL) {
5341 err = got_error_from_errno("strdup");
5342 goto done;
5345 trim_logmsg(*logmsg, limit);
5346 done:
5347 free(logmsg0);
5348 return err;
5351 static const struct got_error *
5352 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5354 const struct got_error *err;
5355 struct got_commit_object *commit = NULL;
5356 char *id_str = NULL, *logmsg = NULL;
5358 err = got_object_open_as_commit(&commit, repo, id);
5359 if (err)
5360 return err;
5362 err = got_object_id_str(&id_str, id);
5363 if (err)
5364 goto done;
5366 id_str[12] = '\0';
5368 err = get_short_logmsg(&logmsg, 42, commit);
5369 if (err)
5370 goto done;
5372 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5373 done:
5374 free(id_str);
5375 got_object_commit_close(commit);
5376 free(logmsg);
5377 return err;
5380 static const struct got_error *
5381 show_rebase_progress(struct got_commit_object *commit,
5382 struct got_object_id *old_id, struct got_object_id *new_id)
5384 const struct got_error *err;
5385 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5387 err = got_object_id_str(&old_id_str, old_id);
5388 if (err)
5389 goto done;
5391 if (new_id) {
5392 err = got_object_id_str(&new_id_str, new_id);
5393 if (err)
5394 goto done;
5397 old_id_str[12] = '\0';
5398 if (new_id_str)
5399 new_id_str[12] = '\0';
5401 err = get_short_logmsg(&logmsg, 42, commit);
5402 if (err)
5403 goto done;
5405 printf("%s -> %s: %s\n", old_id_str,
5406 new_id_str ? new_id_str : "no-op change", logmsg);
5407 done:
5408 free(old_id_str);
5409 free(new_id_str);
5410 free(logmsg);
5411 return err;
5414 static const struct got_error *
5415 rebase_progress(void *arg, unsigned char status, const char *path)
5417 unsigned char *rebase_status = arg;
5419 while (path[0] == '/')
5420 path++;
5421 printf("%c %s\n", status, path);
5423 if (*rebase_status == GOT_STATUS_CONFLICT)
5424 return NULL;
5425 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5426 *rebase_status = status;
5427 return NULL;
5430 static const struct got_error *
5431 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5432 struct got_reference *branch, struct got_reference *new_base_branch,
5433 struct got_reference *tmp_branch, struct got_repository *repo)
5435 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5436 return got_worktree_rebase_complete(worktree, fileindex,
5437 new_base_branch, tmp_branch, branch, repo);
5440 static const struct got_error *
5441 rebase_commit(struct got_pathlist_head *merged_paths,
5442 struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 struct got_reference *tmp_branch,
5444 struct got_object_id *commit_id, struct got_repository *repo)
5446 const struct got_error *error;
5447 struct got_commit_object *commit;
5448 struct got_object_id *new_commit_id;
5450 error = got_object_open_as_commit(&commit, repo, commit_id);
5451 if (error)
5452 return error;
5454 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5455 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5456 if (error) {
5457 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5458 goto done;
5459 error = show_rebase_progress(commit, commit_id, NULL);
5460 } else {
5461 error = show_rebase_progress(commit, commit_id, new_commit_id);
5462 free(new_commit_id);
5464 done:
5465 got_object_commit_close(commit);
5466 return error;
5469 struct check_path_prefix_arg {
5470 const char *path_prefix;
5471 size_t len;
5472 int errcode;
5475 static const struct got_error *
5476 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5477 struct got_blob_object *blob2, struct got_object_id *id1,
5478 struct got_object_id *id2, const char *path1, const char *path2,
5479 mode_t mode1, mode_t mode2, struct got_repository *repo)
5481 struct check_path_prefix_arg *a = arg;
5483 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5484 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5485 return got_error(a->errcode);
5487 return NULL;
5490 static const struct got_error *
5491 check_path_prefix(struct got_object_id *parent_id,
5492 struct got_object_id *commit_id, const char *path_prefix,
5493 int errcode, struct got_repository *repo)
5495 const struct got_error *err;
5496 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5497 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5498 struct check_path_prefix_arg cpp_arg;
5500 if (got_path_is_root_dir(path_prefix))
5501 return NULL;
5503 err = got_object_open_as_commit(&commit, repo, commit_id);
5504 if (err)
5505 goto done;
5507 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5508 if (err)
5509 goto done;
5511 err = got_object_open_as_tree(&tree1, repo,
5512 got_object_commit_get_tree_id(parent_commit));
5513 if (err)
5514 goto done;
5516 err = got_object_open_as_tree(&tree2, repo,
5517 got_object_commit_get_tree_id(commit));
5518 if (err)
5519 goto done;
5521 cpp_arg.path_prefix = path_prefix;
5522 while (cpp_arg.path_prefix[0] == '/')
5523 cpp_arg.path_prefix++;
5524 cpp_arg.len = strlen(cpp_arg.path_prefix);
5525 cpp_arg.errcode = errcode;
5526 err = got_diff_tree(tree1, tree2, "", "", repo,
5527 check_path_prefix_in_diff, &cpp_arg, 0);
5528 done:
5529 if (tree1)
5530 got_object_tree_close(tree1);
5531 if (tree2)
5532 got_object_tree_close(tree2);
5533 if (commit)
5534 got_object_commit_close(commit);
5535 if (parent_commit)
5536 got_object_commit_close(parent_commit);
5537 return err;
5540 static const struct got_error *
5541 collect_commits(struct got_object_id_queue *commits,
5542 struct got_object_id *initial_commit_id,
5543 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5544 const char *path_prefix, int path_prefix_errcode,
5545 struct got_repository *repo)
5547 const struct got_error *err = NULL;
5548 struct got_commit_graph *graph = NULL;
5549 struct got_object_id *parent_id = NULL;
5550 struct got_object_qid *qid;
5551 struct got_object_id *commit_id = initial_commit_id;
5553 err = got_commit_graph_open(&graph, "/", 1);
5554 if (err)
5555 return err;
5557 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5558 check_cancelled, NULL);
5559 if (err)
5560 goto done;
5561 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5562 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5563 check_cancelled, NULL);
5564 if (err) {
5565 if (err->code == GOT_ERR_ITER_COMPLETED) {
5566 err = got_error_msg(GOT_ERR_ANCESTRY,
5567 "ran out of commits to rebase before "
5568 "youngest common ancestor commit has "
5569 "been reached?!?");
5571 goto done;
5572 } else {
5573 err = check_path_prefix(parent_id, commit_id,
5574 path_prefix, path_prefix_errcode, repo);
5575 if (err)
5576 goto done;
5578 err = got_object_qid_alloc(&qid, commit_id);
5579 if (err)
5580 goto done;
5581 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5582 commit_id = parent_id;
5585 done:
5586 got_commit_graph_close(graph);
5587 return err;
5590 static const struct got_error *
5591 cmd_rebase(int argc, char *argv[])
5593 const struct got_error *error = NULL;
5594 struct got_worktree *worktree = NULL;
5595 struct got_repository *repo = NULL;
5596 struct got_fileindex *fileindex = NULL;
5597 char *cwd = NULL;
5598 struct got_reference *branch = NULL;
5599 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5600 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5601 struct got_object_id *resume_commit_id = NULL;
5602 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5603 struct got_commit_object *commit = NULL;
5604 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5605 int histedit_in_progress = 0;
5606 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5607 struct got_object_id_queue commits;
5608 struct got_pathlist_head merged_paths;
5609 const struct got_object_id_queue *parent_ids;
5610 struct got_object_qid *qid, *pid;
5612 SIMPLEQ_INIT(&commits);
5613 TAILQ_INIT(&merged_paths);
5615 while ((ch = getopt(argc, argv, "ac")) != -1) {
5616 switch (ch) {
5617 case 'a':
5618 abort_rebase = 1;
5619 break;
5620 case 'c':
5621 continue_rebase = 1;
5622 break;
5623 default:
5624 usage_rebase();
5625 /* NOTREACHED */
5629 argc -= optind;
5630 argv += optind;
5632 #ifndef PROFILE
5633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5634 "unveil", NULL) == -1)
5635 err(1, "pledge");
5636 #endif
5637 if (abort_rebase && continue_rebase)
5638 usage_rebase();
5639 else if (abort_rebase || continue_rebase) {
5640 if (argc != 0)
5641 usage_rebase();
5642 } else if (argc != 1)
5643 usage_rebase();
5645 cwd = getcwd(NULL, 0);
5646 if (cwd == NULL) {
5647 error = got_error_from_errno("getcwd");
5648 goto done;
5650 error = got_worktree_open(&worktree, cwd);
5651 if (error)
5652 goto done;
5654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5655 NULL);
5656 if (error != NULL)
5657 goto done;
5659 error = apply_unveil(got_repo_get_path(repo), 0,
5660 got_worktree_get_root_path(worktree));
5661 if (error)
5662 goto done;
5664 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5665 worktree);
5666 if (error)
5667 goto done;
5668 if (histedit_in_progress) {
5669 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5670 goto done;
5673 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5674 if (error)
5675 goto done;
5677 if (abort_rebase) {
5678 int did_something;
5679 if (!rebase_in_progress) {
5680 error = got_error(GOT_ERR_NOT_REBASING);
5681 goto done;
5683 error = got_worktree_rebase_continue(&resume_commit_id,
5684 &new_base_branch, &tmp_branch, &branch, &fileindex,
5685 worktree, repo);
5686 if (error)
5687 goto done;
5688 printf("Switching work tree to %s\n",
5689 got_ref_get_symref_target(new_base_branch));
5690 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5691 new_base_branch, update_progress, &did_something);
5692 if (error)
5693 goto done;
5694 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5695 goto done; /* nothing else to do */
5698 if (continue_rebase) {
5699 if (!rebase_in_progress) {
5700 error = got_error(GOT_ERR_NOT_REBASING);
5701 goto done;
5703 error = got_worktree_rebase_continue(&resume_commit_id,
5704 &new_base_branch, &tmp_branch, &branch, &fileindex,
5705 worktree, repo);
5706 if (error)
5707 goto done;
5709 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5710 resume_commit_id, repo);
5711 if (error)
5712 goto done;
5714 yca_id = got_object_id_dup(resume_commit_id);
5715 if (yca_id == NULL) {
5716 error = got_error_from_errno("got_object_id_dup");
5717 goto done;
5719 } else {
5720 error = got_ref_open(&branch, repo, argv[0], 0);
5721 if (error != NULL)
5722 goto done;
5725 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5726 if (error)
5727 goto done;
5729 if (!continue_rebase) {
5730 struct got_object_id *base_commit_id;
5732 base_commit_id = got_worktree_get_base_commit_id(worktree);
5733 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5734 base_commit_id, branch_head_commit_id, repo,
5735 check_cancelled, NULL);
5736 if (error)
5737 goto done;
5738 if (yca_id == NULL) {
5739 error = got_error_msg(GOT_ERR_ANCESTRY,
5740 "specified branch shares no common ancestry "
5741 "with work tree's branch");
5742 goto done;
5745 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5746 if (error) {
5747 if (error->code != GOT_ERR_ANCESTRY)
5748 goto done;
5749 error = NULL;
5750 } else {
5751 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5752 "specified branch resolves to a commit which "
5753 "is already contained in work tree's branch");
5754 goto done;
5756 error = got_worktree_rebase_prepare(&new_base_branch,
5757 &tmp_branch, &fileindex, worktree, branch, repo);
5758 if (error)
5759 goto done;
5762 commit_id = branch_head_commit_id;
5763 error = got_object_open_as_commit(&commit, repo, commit_id);
5764 if (error)
5765 goto done;
5767 parent_ids = got_object_commit_get_parent_ids(commit);
5768 pid = SIMPLEQ_FIRST(parent_ids);
5769 if (pid == NULL) {
5770 if (!continue_rebase) {
5771 int did_something;
5772 error = got_worktree_rebase_abort(worktree, fileindex,
5773 repo, new_base_branch, update_progress,
5774 &did_something);
5775 if (error)
5776 goto done;
5777 printf("Rebase of %s aborted\n",
5778 got_ref_get_name(branch));
5780 error = got_error(GOT_ERR_EMPTY_REBASE);
5781 goto done;
5783 error = collect_commits(&commits, commit_id, pid->id,
5784 yca_id, got_worktree_get_path_prefix(worktree),
5785 GOT_ERR_REBASE_PATH, repo);
5786 got_object_commit_close(commit);
5787 commit = NULL;
5788 if (error)
5789 goto done;
5791 if (SIMPLEQ_EMPTY(&commits)) {
5792 if (continue_rebase) {
5793 error = rebase_complete(worktree, fileindex,
5794 branch, new_base_branch, tmp_branch, repo);
5795 goto done;
5796 } else {
5797 /* Fast-forward the reference of the branch. */
5798 struct got_object_id *new_head_commit_id;
5799 char *id_str;
5800 error = got_ref_resolve(&new_head_commit_id, repo,
5801 new_base_branch);
5802 if (error)
5803 goto done;
5804 error = got_object_id_str(&id_str, new_head_commit_id);
5805 printf("Forwarding %s to commit %s\n",
5806 got_ref_get_name(branch), id_str);
5807 free(id_str);
5808 error = got_ref_change_ref(branch,
5809 new_head_commit_id);
5810 if (error)
5811 goto done;
5815 pid = NULL;
5816 SIMPLEQ_FOREACH(qid, &commits, entry) {
5817 commit_id = qid->id;
5818 parent_id = pid ? pid->id : yca_id;
5819 pid = qid;
5821 error = got_worktree_rebase_merge_files(&merged_paths,
5822 worktree, fileindex, parent_id, commit_id, repo,
5823 rebase_progress, &rebase_status, check_cancelled, NULL);
5824 if (error)
5825 goto done;
5827 if (rebase_status == GOT_STATUS_CONFLICT) {
5828 error = show_rebase_merge_conflict(qid->id, repo);
5829 if (error)
5830 goto done;
5831 got_worktree_rebase_pathlist_free(&merged_paths);
5832 break;
5835 error = rebase_commit(&merged_paths, worktree, fileindex,
5836 tmp_branch, commit_id, repo);
5837 got_worktree_rebase_pathlist_free(&merged_paths);
5838 if (error)
5839 goto done;
5842 if (rebase_status == GOT_STATUS_CONFLICT) {
5843 error = got_worktree_rebase_postpone(worktree, fileindex);
5844 if (error)
5845 goto done;
5846 error = got_error_msg(GOT_ERR_CONFLICTS,
5847 "conflicts must be resolved before rebasing can continue");
5848 } else
5849 error = rebase_complete(worktree, fileindex, branch,
5850 new_base_branch, tmp_branch, repo);
5851 done:
5852 got_object_id_queue_free(&commits);
5853 free(branch_head_commit_id);
5854 free(resume_commit_id);
5855 free(yca_id);
5856 if (commit)
5857 got_object_commit_close(commit);
5858 if (branch)
5859 got_ref_close(branch);
5860 if (new_base_branch)
5861 got_ref_close(new_base_branch);
5862 if (tmp_branch)
5863 got_ref_close(tmp_branch);
5864 if (worktree)
5865 got_worktree_close(worktree);
5866 if (repo)
5867 got_repo_close(repo);
5868 return error;
5871 __dead static void
5872 usage_histedit(void)
5874 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5875 getprogname());
5876 exit(1);
5879 #define GOT_HISTEDIT_PICK 'p'
5880 #define GOT_HISTEDIT_EDIT 'e'
5881 #define GOT_HISTEDIT_FOLD 'f'
5882 #define GOT_HISTEDIT_DROP 'd'
5883 #define GOT_HISTEDIT_MESG 'm'
5885 static struct got_histedit_cmd {
5886 unsigned char code;
5887 const char *name;
5888 const char *desc;
5889 } got_histedit_cmds[] = {
5890 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5891 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5892 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5893 "be used" },
5894 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5895 { GOT_HISTEDIT_MESG, "mesg",
5896 "single-line log message for commit above (open editor if empty)" },
5899 struct got_histedit_list_entry {
5900 TAILQ_ENTRY(got_histedit_list_entry) entry;
5901 struct got_object_id *commit_id;
5902 const struct got_histedit_cmd *cmd;
5903 char *logmsg;
5905 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5907 static const struct got_error *
5908 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5909 FILE *f, struct got_repository *repo)
5911 const struct got_error *err = NULL;
5912 char *logmsg = NULL, *id_str = NULL;
5913 struct got_commit_object *commit = NULL;
5914 int n;
5916 err = got_object_open_as_commit(&commit, repo, commit_id);
5917 if (err)
5918 goto done;
5920 err = get_short_logmsg(&logmsg, 34, commit);
5921 if (err)
5922 goto done;
5924 err = got_object_id_str(&id_str, commit_id);
5925 if (err)
5926 goto done;
5928 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5929 if (n < 0)
5930 err = got_ferror(f, GOT_ERR_IO);
5931 done:
5932 if (commit)
5933 got_object_commit_close(commit);
5934 free(id_str);
5935 free(logmsg);
5936 return err;
5939 static const struct got_error *
5940 histedit_write_commit_list(struct got_object_id_queue *commits,
5941 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5943 const struct got_error *err = NULL;
5944 struct got_object_qid *qid;
5946 if (SIMPLEQ_EMPTY(commits))
5947 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5949 SIMPLEQ_FOREACH(qid, commits, entry) {
5950 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5951 f, repo);
5952 if (err)
5953 break;
5954 if (edit_logmsg_only) {
5955 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5956 if (n < 0) {
5957 err = got_ferror(f, GOT_ERR_IO);
5958 break;
5963 return err;
5966 static const struct got_error *
5967 write_cmd_list(FILE *f, const char *branch_name,
5968 struct got_object_id_queue *commits)
5970 const struct got_error *err = NULL;
5971 int n, i;
5972 char *id_str;
5973 struct got_object_qid *qid;
5975 qid = SIMPLEQ_FIRST(commits);
5976 err = got_object_id_str(&id_str, qid->id);
5977 if (err)
5978 return err;
5980 n = fprintf(f,
5981 "# Editing the history of branch '%s' starting at\n"
5982 "# commit %s\n"
5983 "# Commits will be processed in order from top to "
5984 "bottom of this file.\n", branch_name, id_str);
5985 if (n < 0) {
5986 err = got_ferror(f, GOT_ERR_IO);
5987 goto done;
5990 n = fprintf(f, "# Available histedit commands:\n");
5991 if (n < 0) {
5992 err = got_ferror(f, GOT_ERR_IO);
5993 goto done;
5996 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5997 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5998 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5999 cmd->desc);
6000 if (n < 0) {
6001 err = got_ferror(f, GOT_ERR_IO);
6002 break;
6005 done:
6006 free(id_str);
6007 return err;
6010 static const struct got_error *
6011 histedit_syntax_error(int lineno)
6013 static char msg[42];
6014 int ret;
6016 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6017 lineno);
6018 if (ret == -1 || ret >= sizeof(msg))
6019 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6021 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6024 static const struct got_error *
6025 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6026 char *logmsg, struct got_repository *repo)
6028 const struct got_error *err;
6029 struct got_commit_object *folded_commit = NULL;
6030 char *id_str, *folded_logmsg = NULL;
6032 err = got_object_id_str(&id_str, hle->commit_id);
6033 if (err)
6034 return err;
6036 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6037 if (err)
6038 goto done;
6040 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6041 if (err)
6042 goto done;
6043 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6044 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6045 folded_logmsg) == -1) {
6046 err = got_error_from_errno("asprintf");
6048 done:
6049 if (folded_commit)
6050 got_object_commit_close(folded_commit);
6051 free(id_str);
6052 free(folded_logmsg);
6053 return err;
6056 static struct got_histedit_list_entry *
6057 get_folded_commits(struct got_histedit_list_entry *hle)
6059 struct got_histedit_list_entry *prev, *folded = NULL;
6061 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6062 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6063 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6064 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6065 folded = prev;
6066 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6069 return folded;
6072 static const struct got_error *
6073 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6074 struct got_repository *repo)
6076 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6077 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6078 const struct got_error *err = NULL;
6079 struct got_commit_object *commit = NULL;
6080 int fd;
6081 struct got_histedit_list_entry *folded = NULL;
6083 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6084 if (err)
6085 return err;
6087 folded = get_folded_commits(hle);
6088 if (folded) {
6089 while (folded != hle) {
6090 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6091 folded = TAILQ_NEXT(folded, entry);
6092 continue;
6094 err = append_folded_commit_msg(&new_msg, folded,
6095 logmsg, repo);
6096 if (err)
6097 goto done;
6098 free(logmsg);
6099 logmsg = new_msg;
6100 folded = TAILQ_NEXT(folded, entry);
6104 err = got_object_id_str(&id_str, hle->commit_id);
6105 if (err)
6106 goto done;
6107 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6108 if (err)
6109 goto done;
6110 if (asprintf(&new_msg,
6111 "%s\n# original log message of commit %s: %s",
6112 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6113 err = got_error_from_errno("asprintf");
6114 goto done;
6116 free(logmsg);
6117 logmsg = new_msg;
6119 err = got_object_id_str(&id_str, hle->commit_id);
6120 if (err)
6121 goto done;
6123 err = got_opentemp_named_fd(&logmsg_path, &fd,
6124 GOT_TMPDIR_STR "/got-logmsg");
6125 if (err)
6126 goto done;
6128 dprintf(fd, logmsg);
6129 close(fd);
6131 err = get_editor(&editor);
6132 if (err)
6133 goto done;
6135 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6136 if (err) {
6137 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6138 goto done;
6139 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6141 done:
6142 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6143 err = got_error_from_errno2("unlink", logmsg_path);
6144 free(logmsg_path);
6145 free(logmsg);
6146 free(orig_logmsg);
6147 free(editor);
6148 if (commit)
6149 got_object_commit_close(commit);
6150 return err;
6153 static const struct got_error *
6154 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6155 FILE *f, struct got_repository *repo)
6157 const struct got_error *err = NULL;
6158 char *line = NULL, *p, *end;
6159 size_t size;
6160 ssize_t len;
6161 int lineno = 0, i;
6162 const struct got_histedit_cmd *cmd;
6163 struct got_object_id *commit_id = NULL;
6164 struct got_histedit_list_entry *hle = NULL;
6166 for (;;) {
6167 len = getline(&line, &size, f);
6168 if (len == -1) {
6169 const struct got_error *getline_err;
6170 if (feof(f))
6171 break;
6172 getline_err = got_error_from_errno("getline");
6173 err = got_ferror(f, getline_err->code);
6174 break;
6176 lineno++;
6177 p = line;
6178 while (isspace((unsigned char)p[0]))
6179 p++;
6180 if (p[0] == '#' || p[0] == '\0') {
6181 free(line);
6182 line = NULL;
6183 continue;
6185 cmd = NULL;
6186 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6187 cmd = &got_histedit_cmds[i];
6188 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6189 isspace((unsigned char)p[strlen(cmd->name)])) {
6190 p += strlen(cmd->name);
6191 break;
6193 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6194 p++;
6195 break;
6198 if (i == nitems(got_histedit_cmds)) {
6199 err = histedit_syntax_error(lineno);
6200 break;
6202 while (isspace((unsigned char)p[0]))
6203 p++;
6204 if (cmd->code == GOT_HISTEDIT_MESG) {
6205 if (hle == NULL || hle->logmsg != NULL) {
6206 err = got_error(GOT_ERR_HISTEDIT_CMD);
6207 break;
6209 if (p[0] == '\0') {
6210 err = histedit_edit_logmsg(hle, repo);
6211 if (err)
6212 break;
6213 } else {
6214 hle->logmsg = strdup(p);
6215 if (hle->logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6217 break;
6220 free(line);
6221 line = NULL;
6222 continue;
6223 } else {
6224 end = p;
6225 while (end[0] && !isspace((unsigned char)end[0]))
6226 end++;
6227 *end = '\0';
6229 err = got_object_resolve_id_str(&commit_id, repo, p);
6230 if (err) {
6231 /* override error code */
6232 err = histedit_syntax_error(lineno);
6233 break;
6236 hle = malloc(sizeof(*hle));
6237 if (hle == NULL) {
6238 err = got_error_from_errno("malloc");
6239 break;
6241 hle->cmd = cmd;
6242 hle->commit_id = commit_id;
6243 hle->logmsg = NULL;
6244 commit_id = NULL;
6245 free(line);
6246 line = NULL;
6247 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6250 free(line);
6251 free(commit_id);
6252 return err;
6255 static const struct got_error *
6256 histedit_check_script(struct got_histedit_list *histedit_cmds,
6257 struct got_object_id_queue *commits, struct got_repository *repo)
6259 const struct got_error *err = NULL;
6260 struct got_object_qid *qid;
6261 struct got_histedit_list_entry *hle;
6262 static char msg[92];
6263 char *id_str;
6265 if (TAILQ_EMPTY(histedit_cmds))
6266 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6267 "histedit script contains no commands");
6268 if (SIMPLEQ_EMPTY(commits))
6269 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6271 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6272 struct got_histedit_list_entry *hle2;
6273 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6274 if (hle == hle2)
6275 continue;
6276 if (got_object_id_cmp(hle->commit_id,
6277 hle2->commit_id) != 0)
6278 continue;
6279 err = got_object_id_str(&id_str, hle->commit_id);
6280 if (err)
6281 return err;
6282 snprintf(msg, sizeof(msg), "commit %s is listed "
6283 "more than once in histedit script", id_str);
6284 free(id_str);
6285 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6289 SIMPLEQ_FOREACH(qid, commits, entry) {
6290 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6291 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6292 break;
6294 if (hle == NULL) {
6295 err = got_object_id_str(&id_str, qid->id);
6296 if (err)
6297 return err;
6298 snprintf(msg, sizeof(msg),
6299 "commit %s missing from histedit script", id_str);
6300 free(id_str);
6301 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6305 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6306 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6307 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6308 "last commit in histedit script cannot be folded");
6310 return NULL;
6313 static const struct got_error *
6314 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6315 const char *path, struct got_object_id_queue *commits,
6316 struct got_repository *repo)
6318 const struct got_error *err = NULL;
6319 char *editor;
6320 FILE *f = NULL;
6322 err = get_editor(&editor);
6323 if (err)
6324 return err;
6326 if (spawn_editor(editor, path) == -1) {
6327 err = got_error_from_errno("failed spawning editor");
6328 goto done;
6331 f = fopen(path, "r");
6332 if (f == NULL) {
6333 err = got_error_from_errno("fopen");
6334 goto done;
6336 err = histedit_parse_list(histedit_cmds, f, repo);
6337 if (err)
6338 goto done;
6340 err = histedit_check_script(histedit_cmds, commits, repo);
6341 done:
6342 if (f && fclose(f) != 0 && err == NULL)
6343 err = got_error_from_errno("fclose");
6344 free(editor);
6345 return err;
6348 static const struct got_error *
6349 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6350 struct got_object_id_queue *, const char *, const char *,
6351 struct got_repository *);
6353 static const struct got_error *
6354 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6355 struct got_object_id_queue *commits, const char *branch_name,
6356 int edit_logmsg_only, struct got_repository *repo)
6358 const struct got_error *err;
6359 FILE *f = NULL;
6360 char *path = NULL;
6362 err = got_opentemp_named(&path, &f, "got-histedit");
6363 if (err)
6364 return err;
6366 err = write_cmd_list(f, branch_name, commits);
6367 if (err)
6368 goto done;
6370 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6371 if (err)
6372 goto done;
6374 if (edit_logmsg_only) {
6375 rewind(f);
6376 err = histedit_parse_list(histedit_cmds, f, repo);
6377 } else {
6378 if (fclose(f) != 0) {
6379 err = got_error_from_errno("fclose");
6380 goto done;
6382 f = NULL;
6383 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6384 if (err) {
6385 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6386 err->code != GOT_ERR_HISTEDIT_CMD)
6387 goto done;
6388 err = histedit_edit_list_retry(histedit_cmds, err,
6389 commits, path, branch_name, repo);
6392 done:
6393 if (f && fclose(f) != 0 && err == NULL)
6394 err = got_error_from_errno("fclose");
6395 if (path && unlink(path) != 0 && err == NULL)
6396 err = got_error_from_errno2("unlink", path);
6397 free(path);
6398 return err;
6401 static const struct got_error *
6402 histedit_save_list(struct got_histedit_list *histedit_cmds,
6403 struct got_worktree *worktree, struct got_repository *repo)
6405 const struct got_error *err = NULL;
6406 char *path = NULL;
6407 FILE *f = NULL;
6408 struct got_histedit_list_entry *hle;
6409 struct got_commit_object *commit = NULL;
6411 err = got_worktree_get_histedit_script_path(&path, worktree);
6412 if (err)
6413 return err;
6415 f = fopen(path, "w");
6416 if (f == NULL) {
6417 err = got_error_from_errno2("fopen", path);
6418 goto done;
6420 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6421 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6422 repo);
6423 if (err)
6424 break;
6426 if (hle->logmsg) {
6427 int n = fprintf(f, "%c %s\n",
6428 GOT_HISTEDIT_MESG, hle->logmsg);
6429 if (n < 0) {
6430 err = got_ferror(f, GOT_ERR_IO);
6431 break;
6435 done:
6436 if (f && fclose(f) != 0 && err == NULL)
6437 err = got_error_from_errno("fclose");
6438 free(path);
6439 if (commit)
6440 got_object_commit_close(commit);
6441 return err;
6444 void
6445 histedit_free_list(struct got_histedit_list *histedit_cmds)
6447 struct got_histedit_list_entry *hle;
6449 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6450 TAILQ_REMOVE(histedit_cmds, hle, entry);
6451 free(hle);
6455 static const struct got_error *
6456 histedit_load_list(struct got_histedit_list *histedit_cmds,
6457 const char *path, struct got_repository *repo)
6459 const struct got_error *err = NULL;
6460 FILE *f = NULL;
6462 f = fopen(path, "r");
6463 if (f == NULL) {
6464 err = got_error_from_errno2("fopen", path);
6465 goto done;
6468 err = histedit_parse_list(histedit_cmds, f, repo);
6469 done:
6470 if (f && fclose(f) != 0 && err == NULL)
6471 err = got_error_from_errno("fclose");
6472 return err;
6475 static const struct got_error *
6476 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6477 const struct got_error *edit_err, struct got_object_id_queue *commits,
6478 const char *path, const char *branch_name, struct got_repository *repo)
6480 const struct got_error *err = NULL, *prev_err = edit_err;
6481 int resp = ' ';
6483 while (resp != 'c' && resp != 'r' && resp != 'a') {
6484 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6485 "or (a)bort: ", getprogname(), prev_err->msg);
6486 resp = getchar();
6487 if (resp == '\n')
6488 resp = getchar();
6489 if (resp == 'c') {
6490 histedit_free_list(histedit_cmds);
6491 err = histedit_run_editor(histedit_cmds, path, commits,
6492 repo);
6493 if (err) {
6494 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6495 err->code != GOT_ERR_HISTEDIT_CMD)
6496 break;
6497 prev_err = err;
6498 resp = ' ';
6499 continue;
6501 break;
6502 } else if (resp == 'r') {
6503 histedit_free_list(histedit_cmds);
6504 err = histedit_edit_script(histedit_cmds,
6505 commits, branch_name, 0, repo);
6506 if (err) {
6507 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6508 err->code != GOT_ERR_HISTEDIT_CMD)
6509 break;
6510 prev_err = err;
6511 resp = ' ';
6512 continue;
6514 break;
6515 } else if (resp == 'a') {
6516 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6517 break;
6518 } else
6519 printf("invalid response '%c'\n", resp);
6522 return err;
6525 static const struct got_error *
6526 histedit_complete(struct got_worktree *worktree,
6527 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6528 struct got_reference *branch, struct got_repository *repo)
6530 printf("Switching work tree to %s\n",
6531 got_ref_get_symref_target(branch));
6532 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6533 branch, repo);
6536 static const struct got_error *
6537 show_histedit_progress(struct got_commit_object *commit,
6538 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6540 const struct got_error *err;
6541 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6543 err = got_object_id_str(&old_id_str, hle->commit_id);
6544 if (err)
6545 goto done;
6547 if (new_id) {
6548 err = got_object_id_str(&new_id_str, new_id);
6549 if (err)
6550 goto done;
6553 old_id_str[12] = '\0';
6554 if (new_id_str)
6555 new_id_str[12] = '\0';
6557 if (hle->logmsg) {
6558 logmsg = strdup(hle->logmsg);
6559 if (logmsg == NULL) {
6560 err = got_error_from_errno("strdup");
6561 goto done;
6563 trim_logmsg(logmsg, 42);
6564 } else {
6565 err = get_short_logmsg(&logmsg, 42, commit);
6566 if (err)
6567 goto done;
6570 switch (hle->cmd->code) {
6571 case GOT_HISTEDIT_PICK:
6572 case GOT_HISTEDIT_EDIT:
6573 printf("%s -> %s: %s\n", old_id_str,
6574 new_id_str ? new_id_str : "no-op change", logmsg);
6575 break;
6576 case GOT_HISTEDIT_DROP:
6577 case GOT_HISTEDIT_FOLD:
6578 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6579 logmsg);
6580 break;
6581 default:
6582 break;
6584 done:
6585 free(old_id_str);
6586 free(new_id_str);
6587 return err;
6590 static const struct got_error *
6591 histedit_commit(struct got_pathlist_head *merged_paths,
6592 struct got_worktree *worktree, struct got_fileindex *fileindex,
6593 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6594 struct got_repository *repo)
6596 const struct got_error *err;
6597 struct got_commit_object *commit;
6598 struct got_object_id *new_commit_id;
6600 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6601 && hle->logmsg == NULL) {
6602 err = histedit_edit_logmsg(hle, repo);
6603 if (err)
6604 return err;
6607 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6608 if (err)
6609 return err;
6611 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6612 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6613 hle->logmsg, repo);
6614 if (err) {
6615 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6616 goto done;
6617 err = show_histedit_progress(commit, hle, NULL);
6618 } else {
6619 err = show_histedit_progress(commit, hle, new_commit_id);
6620 free(new_commit_id);
6622 done:
6623 got_object_commit_close(commit);
6624 return err;
6627 static const struct got_error *
6628 histedit_skip_commit(struct got_histedit_list_entry *hle,
6629 struct got_worktree *worktree, struct got_repository *repo)
6631 const struct got_error *error;
6632 struct got_commit_object *commit;
6634 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6635 repo);
6636 if (error)
6637 return error;
6639 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6640 if (error)
6641 return error;
6643 error = show_histedit_progress(commit, hle, NULL);
6644 got_object_commit_close(commit);
6645 return error;
6648 static const struct got_error *
6649 check_local_changes(void *arg, unsigned char status,
6650 unsigned char staged_status, const char *path,
6651 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6652 struct got_object_id *commit_id, int dirfd, const char *de_name)
6654 int *have_local_changes = arg;
6656 switch (status) {
6657 case GOT_STATUS_ADD:
6658 case GOT_STATUS_DELETE:
6659 case GOT_STATUS_MODIFY:
6660 case GOT_STATUS_CONFLICT:
6661 *have_local_changes = 1;
6662 return got_error(GOT_ERR_CANCELLED);
6663 default:
6664 break;
6667 switch (staged_status) {
6668 case GOT_STATUS_ADD:
6669 case GOT_STATUS_DELETE:
6670 case GOT_STATUS_MODIFY:
6671 *have_local_changes = 1;
6672 return got_error(GOT_ERR_CANCELLED);
6673 default:
6674 break;
6677 return NULL;
6680 static const struct got_error *
6681 cmd_histedit(int argc, char *argv[])
6683 const struct got_error *error = NULL;
6684 struct got_worktree *worktree = NULL;
6685 struct got_fileindex *fileindex = NULL;
6686 struct got_repository *repo = NULL;
6687 char *cwd = NULL;
6688 struct got_reference *branch = NULL;
6689 struct got_reference *tmp_branch = NULL;
6690 struct got_object_id *resume_commit_id = NULL;
6691 struct got_object_id *base_commit_id = NULL;
6692 struct got_object_id *head_commit_id = NULL;
6693 struct got_commit_object *commit = NULL;
6694 int ch, rebase_in_progress = 0, did_something;
6695 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6696 int edit_logmsg_only = 0;
6697 const char *edit_script_path = NULL;
6698 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6699 struct got_object_id_queue commits;
6700 struct got_pathlist_head merged_paths;
6701 const struct got_object_id_queue *parent_ids;
6702 struct got_object_qid *pid;
6703 struct got_histedit_list histedit_cmds;
6704 struct got_histedit_list_entry *hle;
6706 SIMPLEQ_INIT(&commits);
6707 TAILQ_INIT(&histedit_cmds);
6708 TAILQ_INIT(&merged_paths);
6710 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6711 switch (ch) {
6712 case 'a':
6713 abort_edit = 1;
6714 break;
6715 case 'c':
6716 continue_edit = 1;
6717 break;
6718 case 'F':
6719 edit_script_path = optarg;
6720 break;
6721 case 'm':
6722 edit_logmsg_only = 1;
6723 break;
6724 default:
6725 usage_histedit();
6726 /* NOTREACHED */
6730 argc -= optind;
6731 argv += optind;
6733 #ifndef PROFILE
6734 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6735 "unveil", NULL) == -1)
6736 err(1, "pledge");
6737 #endif
6738 if (abort_edit && continue_edit)
6739 errx(1, "histedit's -a and -c options are mutually exclusive");
6740 if (edit_script_path && edit_logmsg_only)
6741 errx(1, "histedit's -F and -m options are mutually exclusive");
6742 if (abort_edit && edit_logmsg_only)
6743 errx(1, "histedit's -a and -m options are mutually exclusive");
6744 if (continue_edit && edit_logmsg_only)
6745 errx(1, "histedit's -c and -m options are mutually exclusive");
6746 if (argc != 0)
6747 usage_histedit();
6750 * This command cannot apply unveil(2) in all cases because the
6751 * user may choose to run an editor to edit the histedit script
6752 * and to edit individual commit log messages.
6753 * unveil(2) traverses exec(2); if an editor is used we have to
6754 * apply unveil after edit script and log messages have been written.
6755 * XXX TODO: Make use of unveil(2) where possible.
6758 cwd = getcwd(NULL, 0);
6759 if (cwd == NULL) {
6760 error = got_error_from_errno("getcwd");
6761 goto done;
6763 error = got_worktree_open(&worktree, cwd);
6764 if (error)
6765 goto done;
6767 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6768 NULL);
6769 if (error != NULL)
6770 goto done;
6772 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6773 if (error)
6774 goto done;
6775 if (rebase_in_progress) {
6776 error = got_error(GOT_ERR_REBASING);
6777 goto done;
6780 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6781 if (error)
6782 goto done;
6784 if (edit_in_progress && edit_logmsg_only) {
6785 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6786 "histedit operation is in progress in this "
6787 "work tree and must be continued or aborted "
6788 "before the -m option can be used");
6789 goto done;
6792 if (edit_in_progress && abort_edit) {
6793 error = got_worktree_histedit_continue(&resume_commit_id,
6794 &tmp_branch, &branch, &base_commit_id, &fileindex,
6795 worktree, repo);
6796 if (error)
6797 goto done;
6798 printf("Switching work tree to %s\n",
6799 got_ref_get_symref_target(branch));
6800 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6801 branch, base_commit_id, update_progress, &did_something);
6802 if (error)
6803 goto done;
6804 printf("Histedit of %s aborted\n",
6805 got_ref_get_symref_target(branch));
6806 goto done; /* nothing else to do */
6807 } else if (abort_edit) {
6808 error = got_error(GOT_ERR_NOT_HISTEDIT);
6809 goto done;
6812 if (continue_edit) {
6813 char *path;
6815 if (!edit_in_progress) {
6816 error = got_error(GOT_ERR_NOT_HISTEDIT);
6817 goto done;
6820 error = got_worktree_get_histedit_script_path(&path, worktree);
6821 if (error)
6822 goto done;
6824 error = histedit_load_list(&histedit_cmds, path, repo);
6825 free(path);
6826 if (error)
6827 goto done;
6829 error = got_worktree_histedit_continue(&resume_commit_id,
6830 &tmp_branch, &branch, &base_commit_id, &fileindex,
6831 worktree, repo);
6832 if (error)
6833 goto done;
6835 error = got_ref_resolve(&head_commit_id, repo, branch);
6836 if (error)
6837 goto done;
6839 error = got_object_open_as_commit(&commit, repo,
6840 head_commit_id);
6841 if (error)
6842 goto done;
6843 parent_ids = got_object_commit_get_parent_ids(commit);
6844 pid = SIMPLEQ_FIRST(parent_ids);
6845 if (pid == NULL) {
6846 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6847 goto done;
6849 error = collect_commits(&commits, head_commit_id, pid->id,
6850 base_commit_id, got_worktree_get_path_prefix(worktree),
6851 GOT_ERR_HISTEDIT_PATH, repo);
6852 got_object_commit_close(commit);
6853 commit = NULL;
6854 if (error)
6855 goto done;
6856 } else {
6857 if (edit_in_progress) {
6858 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6859 goto done;
6862 error = got_ref_open(&branch, repo,
6863 got_worktree_get_head_ref_name(worktree), 0);
6864 if (error != NULL)
6865 goto done;
6867 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6868 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6869 "will not edit commit history of a branch outside "
6870 "the \"refs/heads/\" reference namespace");
6871 goto done;
6874 error = got_ref_resolve(&head_commit_id, repo, branch);
6875 got_ref_close(branch);
6876 branch = NULL;
6877 if (error)
6878 goto done;
6880 error = got_object_open_as_commit(&commit, repo,
6881 head_commit_id);
6882 if (error)
6883 goto done;
6884 parent_ids = got_object_commit_get_parent_ids(commit);
6885 pid = SIMPLEQ_FIRST(parent_ids);
6886 if (pid == NULL) {
6887 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6888 goto done;
6890 error = collect_commits(&commits, head_commit_id, pid->id,
6891 got_worktree_get_base_commit_id(worktree),
6892 got_worktree_get_path_prefix(worktree),
6893 GOT_ERR_HISTEDIT_PATH, repo);
6894 got_object_commit_close(commit);
6895 commit = NULL;
6896 if (error)
6897 goto done;
6899 if (SIMPLEQ_EMPTY(&commits)) {
6900 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6901 goto done;
6904 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6905 &base_commit_id, &fileindex, worktree, repo);
6906 if (error)
6907 goto done;
6909 if (edit_script_path) {
6910 error = histedit_load_list(&histedit_cmds,
6911 edit_script_path, repo);
6912 if (error) {
6913 got_worktree_histedit_abort(worktree, fileindex,
6914 repo, branch, base_commit_id,
6915 update_progress, &did_something);
6916 goto done;
6918 } else {
6919 const char *branch_name;
6920 branch_name = got_ref_get_symref_target(branch);
6921 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6922 branch_name += 11;
6923 error = histedit_edit_script(&histedit_cmds, &commits,
6924 branch_name, edit_logmsg_only, repo);
6925 if (error) {
6926 got_worktree_histedit_abort(worktree, fileindex,
6927 repo, branch, base_commit_id,
6928 update_progress, &did_something);
6929 goto done;
6934 error = histedit_save_list(&histedit_cmds, worktree,
6935 repo);
6936 if (error) {
6937 got_worktree_histedit_abort(worktree, fileindex,
6938 repo, branch, base_commit_id,
6939 update_progress, &did_something);
6940 goto done;
6945 error = histedit_check_script(&histedit_cmds, &commits, repo);
6946 if (error)
6947 goto done;
6949 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6950 if (resume_commit_id) {
6951 if (got_object_id_cmp(hle->commit_id,
6952 resume_commit_id) != 0)
6953 continue;
6955 resume_commit_id = NULL;
6956 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6957 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6958 error = histedit_skip_commit(hle, worktree,
6959 repo);
6960 if (error)
6961 goto done;
6962 } else {
6963 struct got_pathlist_head paths;
6964 int have_changes = 0;
6966 TAILQ_INIT(&paths);
6967 error = got_pathlist_append(&paths, "", NULL);
6968 if (error)
6969 goto done;
6970 error = got_worktree_status(worktree, &paths,
6971 repo, check_local_changes, &have_changes,
6972 check_cancelled, NULL);
6973 got_pathlist_free(&paths);
6974 if (error) {
6975 if (error->code != GOT_ERR_CANCELLED)
6976 goto done;
6977 if (sigint_received || sigpipe_received)
6978 goto done;
6980 if (have_changes) {
6981 error = histedit_commit(NULL, worktree,
6982 fileindex, tmp_branch, hle, repo);
6983 if (error)
6984 goto done;
6985 } else {
6986 error = got_object_open_as_commit(
6987 &commit, repo, hle->commit_id);
6988 if (error)
6989 goto done;
6990 error = show_histedit_progress(commit,
6991 hle, NULL);
6992 got_object_commit_close(commit);
6993 commit = NULL;
6994 if (error)
6995 goto done;
6998 continue;
7001 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7002 error = histedit_skip_commit(hle, worktree, repo);
7003 if (error)
7004 goto done;
7005 continue;
7008 error = got_object_open_as_commit(&commit, repo,
7009 hle->commit_id);
7010 if (error)
7011 goto done;
7012 parent_ids = got_object_commit_get_parent_ids(commit);
7013 pid = SIMPLEQ_FIRST(parent_ids);
7015 error = got_worktree_histedit_merge_files(&merged_paths,
7016 worktree, fileindex, pid->id, hle->commit_id, repo,
7017 rebase_progress, &rebase_status, check_cancelled, NULL);
7018 if (error)
7019 goto done;
7020 got_object_commit_close(commit);
7021 commit = NULL;
7023 if (rebase_status == GOT_STATUS_CONFLICT) {
7024 error = show_rebase_merge_conflict(hle->commit_id,
7025 repo);
7026 if (error)
7027 goto done;
7028 got_worktree_rebase_pathlist_free(&merged_paths);
7029 break;
7032 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7033 char *id_str;
7034 error = got_object_id_str(&id_str, hle->commit_id);
7035 if (error)
7036 goto done;
7037 printf("Stopping histedit for amending commit %s\n",
7038 id_str);
7039 free(id_str);
7040 got_worktree_rebase_pathlist_free(&merged_paths);
7041 error = got_worktree_histedit_postpone(worktree,
7042 fileindex);
7043 goto done;
7046 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7047 error = histedit_skip_commit(hle, worktree, repo);
7048 if (error)
7049 goto done;
7050 continue;
7053 error = histedit_commit(&merged_paths, worktree, fileindex,
7054 tmp_branch, hle, repo);
7055 got_worktree_rebase_pathlist_free(&merged_paths);
7056 if (error)
7057 goto done;
7060 if (rebase_status == GOT_STATUS_CONFLICT) {
7061 error = got_worktree_histedit_postpone(worktree, fileindex);
7062 if (error)
7063 goto done;
7064 error = got_error_msg(GOT_ERR_CONFLICTS,
7065 "conflicts must be resolved before histedit can continue");
7066 } else
7067 error = histedit_complete(worktree, fileindex, tmp_branch,
7068 branch, repo);
7069 done:
7070 got_object_id_queue_free(&commits);
7071 histedit_free_list(&histedit_cmds);
7072 free(head_commit_id);
7073 free(base_commit_id);
7074 free(resume_commit_id);
7075 if (commit)
7076 got_object_commit_close(commit);
7077 if (branch)
7078 got_ref_close(branch);
7079 if (tmp_branch)
7080 got_ref_close(tmp_branch);
7081 if (worktree)
7082 got_worktree_close(worktree);
7083 if (repo)
7084 got_repo_close(repo);
7085 return error;
7088 __dead static void
7089 usage_integrate(void)
7091 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7092 exit(1);
7095 static const struct got_error *
7096 cmd_integrate(int argc, char *argv[])
7098 const struct got_error *error = NULL;
7099 struct got_repository *repo = NULL;
7100 struct got_worktree *worktree = NULL;
7101 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7102 const char *branch_arg = NULL;
7103 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7104 struct got_fileindex *fileindex = NULL;
7105 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7106 int ch, did_something = 0;
7108 while ((ch = getopt(argc, argv, "")) != -1) {
7109 switch (ch) {
7110 default:
7111 usage_integrate();
7112 /* NOTREACHED */
7116 argc -= optind;
7117 argv += optind;
7119 if (argc != 1)
7120 usage_integrate();
7121 branch_arg = argv[0];
7123 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 "unveil", NULL) == -1)
7125 err(1, "pledge");
7127 cwd = getcwd(NULL, 0);
7128 if (cwd == NULL) {
7129 error = got_error_from_errno("getcwd");
7130 goto done;
7133 error = got_worktree_open(&worktree, cwd);
7134 if (error)
7135 goto done;
7137 error = check_rebase_or_histedit_in_progress(worktree);
7138 if (error)
7139 goto done;
7141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7142 NULL);
7143 if (error != NULL)
7144 goto done;
7146 error = apply_unveil(got_repo_get_path(repo), 0,
7147 got_worktree_get_root_path(worktree));
7148 if (error)
7149 goto done;
7151 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7152 error = got_error_from_errno("asprintf");
7153 goto done;
7156 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7157 &base_branch_ref, worktree, refname, repo);
7158 if (error)
7159 goto done;
7161 refname = strdup(got_ref_get_name(branch_ref));
7162 if (refname == NULL) {
7163 error = got_error_from_errno("strdup");
7164 got_worktree_integrate_abort(worktree, fileindex, repo,
7165 branch_ref, base_branch_ref);
7166 goto done;
7168 base_refname = strdup(got_ref_get_name(base_branch_ref));
7169 if (base_refname == NULL) {
7170 error = got_error_from_errno("strdup");
7171 got_worktree_integrate_abort(worktree, fileindex, repo,
7172 branch_ref, base_branch_ref);
7173 goto done;
7176 error = got_ref_resolve(&commit_id, repo, branch_ref);
7177 if (error)
7178 goto done;
7180 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7181 if (error)
7182 goto done;
7184 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7185 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7186 "specified branch has already been integrated");
7187 got_worktree_integrate_abort(worktree, fileindex, repo,
7188 branch_ref, base_branch_ref);
7189 goto done;
7192 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7193 if (error) {
7194 if (error->code == GOT_ERR_ANCESTRY)
7195 error = got_error(GOT_ERR_REBASE_REQUIRED);
7196 got_worktree_integrate_abort(worktree, fileindex, repo,
7197 branch_ref, base_branch_ref);
7198 goto done;
7201 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7202 branch_ref, base_branch_ref, update_progress, &did_something,
7203 check_cancelled, NULL);
7204 if (error)
7205 goto done;
7207 printf("Integrated %s into %s\n", refname, base_refname);
7208 done:
7209 if (repo)
7210 got_repo_close(repo);
7211 if (worktree)
7212 got_worktree_close(worktree);
7213 free(cwd);
7214 free(base_commit_id);
7215 free(commit_id);
7216 free(refname);
7217 free(base_refname);
7218 return error;
7221 __dead static void
7222 usage_stage(void)
7224 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7225 "[file-path ...]\n",
7226 getprogname());
7227 exit(1);
7230 static const struct got_error *
7231 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7232 const char *path, struct got_object_id *blob_id,
7233 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7234 int dirfd, const char *de_name)
7236 const struct got_error *err = NULL;
7237 char *id_str = NULL;
7239 if (staged_status != GOT_STATUS_ADD &&
7240 staged_status != GOT_STATUS_MODIFY &&
7241 staged_status != GOT_STATUS_DELETE)
7242 return NULL;
7244 if (staged_status == GOT_STATUS_ADD ||
7245 staged_status == GOT_STATUS_MODIFY)
7246 err = got_object_id_str(&id_str, staged_blob_id);
7247 else
7248 err = got_object_id_str(&id_str, blob_id);
7249 if (err)
7250 return err;
7252 printf("%s %c %s\n", id_str, staged_status, path);
7253 free(id_str);
7254 return NULL;
7257 static const struct got_error *
7258 cmd_stage(int argc, char *argv[])
7260 const struct got_error *error = NULL;
7261 struct got_repository *repo = NULL;
7262 struct got_worktree *worktree = NULL;
7263 char *cwd = NULL;
7264 struct got_pathlist_head paths;
7265 struct got_pathlist_entry *pe;
7266 int ch, list_stage = 0, pflag = 0;
7267 FILE *patch_script_file = NULL;
7268 const char *patch_script_path = NULL;
7269 struct choose_patch_arg cpa;
7271 TAILQ_INIT(&paths);
7273 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7274 switch (ch) {
7275 case 'l':
7276 list_stage = 1;
7277 break;
7278 case 'p':
7279 pflag = 1;
7280 break;
7281 case 'F':
7282 patch_script_path = optarg;
7283 break;
7284 default:
7285 usage_stage();
7286 /* NOTREACHED */
7290 argc -= optind;
7291 argv += optind;
7293 #ifndef PROFILE
7294 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7295 "unveil", NULL) == -1)
7296 err(1, "pledge");
7297 #endif
7298 if (list_stage && (pflag || patch_script_path))
7299 errx(1, "-l option cannot be used with other options");
7300 if (patch_script_path && !pflag)
7301 errx(1, "-F option can only be used together with -p option");
7303 cwd = getcwd(NULL, 0);
7304 if (cwd == NULL) {
7305 error = got_error_from_errno("getcwd");
7306 goto done;
7309 error = got_worktree_open(&worktree, cwd);
7310 if (error)
7311 goto done;
7313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7314 NULL);
7315 if (error != NULL)
7316 goto done;
7318 if (patch_script_path) {
7319 patch_script_file = fopen(patch_script_path, "r");
7320 if (patch_script_file == NULL) {
7321 error = got_error_from_errno2("fopen",
7322 patch_script_path);
7323 goto done;
7326 error = apply_unveil(got_repo_get_path(repo), 0,
7327 got_worktree_get_root_path(worktree));
7328 if (error)
7329 goto done;
7331 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7332 if (error)
7333 goto done;
7335 if (list_stage)
7336 error = got_worktree_status(worktree, &paths, repo,
7337 print_stage, NULL, check_cancelled, NULL);
7338 else {
7339 cpa.patch_script_file = patch_script_file;
7340 cpa.action = "stage";
7341 error = got_worktree_stage(worktree, &paths,
7342 pflag ? NULL : print_status, NULL,
7343 pflag ? choose_patch : NULL, &cpa, repo);
7345 done:
7346 if (patch_script_file && fclose(patch_script_file) == EOF &&
7347 error == NULL)
7348 error = got_error_from_errno2("fclose", patch_script_path);
7349 if (repo)
7350 got_repo_close(repo);
7351 if (worktree)
7352 got_worktree_close(worktree);
7353 TAILQ_FOREACH(pe, &paths, entry)
7354 free((char *)pe->path);
7355 got_pathlist_free(&paths);
7356 free(cwd);
7357 return error;
7360 __dead static void
7361 usage_unstage(void)
7363 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7364 "[file-path ...]\n",
7365 getprogname());
7366 exit(1);
7370 static const struct got_error *
7371 cmd_unstage(int argc, char *argv[])
7373 const struct got_error *error = NULL;
7374 struct got_repository *repo = NULL;
7375 struct got_worktree *worktree = NULL;
7376 char *cwd = NULL;
7377 struct got_pathlist_head paths;
7378 struct got_pathlist_entry *pe;
7379 int ch, did_something = 0, pflag = 0;
7380 FILE *patch_script_file = NULL;
7381 const char *patch_script_path = NULL;
7382 struct choose_patch_arg cpa;
7384 TAILQ_INIT(&paths);
7386 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7387 switch (ch) {
7388 case 'p':
7389 pflag = 1;
7390 break;
7391 case 'F':
7392 patch_script_path = optarg;
7393 break;
7394 default:
7395 usage_unstage();
7396 /* NOTREACHED */
7400 argc -= optind;
7401 argv += optind;
7403 #ifndef PROFILE
7404 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7405 "unveil", NULL) == -1)
7406 err(1, "pledge");
7407 #endif
7408 if (patch_script_path && !pflag)
7409 errx(1, "-F option can only be used together with -p option");
7411 cwd = getcwd(NULL, 0);
7412 if (cwd == NULL) {
7413 error = got_error_from_errno("getcwd");
7414 goto done;
7417 error = got_worktree_open(&worktree, cwd);
7418 if (error)
7419 goto done;
7421 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7422 NULL);
7423 if (error != NULL)
7424 goto done;
7426 if (patch_script_path) {
7427 patch_script_file = fopen(patch_script_path, "r");
7428 if (patch_script_file == NULL) {
7429 error = got_error_from_errno2("fopen",
7430 patch_script_path);
7431 goto done;
7435 error = apply_unveil(got_repo_get_path(repo), 0,
7436 got_worktree_get_root_path(worktree));
7437 if (error)
7438 goto done;
7440 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7441 if (error)
7442 goto done;
7444 cpa.patch_script_file = patch_script_file;
7445 cpa.action = "unstage";
7446 error = got_worktree_unstage(worktree, &paths, update_progress,
7447 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7448 done:
7449 if (patch_script_file && fclose(patch_script_file) == EOF &&
7450 error == NULL)
7451 error = got_error_from_errno2("fclose", patch_script_path);
7452 if (repo)
7453 got_repo_close(repo);
7454 if (worktree)
7455 got_worktree_close(worktree);
7456 TAILQ_FOREACH(pe, &paths, entry)
7457 free((char *)pe->path);
7458 got_pathlist_free(&paths);
7459 free(cwd);
7460 return error;
7463 __dead static void
7464 usage_cat(void)
7466 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7467 "arg1 [arg2 ...]\n", getprogname());
7468 exit(1);
7471 static const struct got_error *
7472 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7474 const struct got_error *err;
7475 struct got_blob_object *blob;
7477 err = got_object_open_as_blob(&blob, repo, id, 8192);
7478 if (err)
7479 return err;
7481 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7482 got_object_blob_close(blob);
7483 return err;
7486 static const struct got_error *
7487 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7489 const struct got_error *err;
7490 struct got_tree_object *tree;
7491 int nentries, i;
7493 err = got_object_open_as_tree(&tree, repo, id);
7494 if (err)
7495 return err;
7497 nentries = got_object_tree_get_nentries(tree);
7498 for (i = 0; i < nentries; i++) {
7499 struct got_tree_entry *te;
7500 char *id_str;
7501 if (sigint_received || sigpipe_received)
7502 break;
7503 te = got_object_tree_get_entry(tree, i);
7504 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7505 if (err)
7506 break;
7507 fprintf(outfile, "%s %.7o %s\n", id_str,
7508 got_tree_entry_get_mode(te),
7509 got_tree_entry_get_name(te));
7510 free(id_str);
7513 got_object_tree_close(tree);
7514 return err;
7517 static const struct got_error *
7518 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7520 const struct got_error *err;
7521 struct got_commit_object *commit;
7522 const struct got_object_id_queue *parent_ids;
7523 struct got_object_qid *pid;
7524 char *id_str = NULL;
7525 const char *logmsg = NULL;
7527 err = got_object_open_as_commit(&commit, repo, id);
7528 if (err)
7529 return err;
7531 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7532 if (err)
7533 goto done;
7535 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7536 parent_ids = got_object_commit_get_parent_ids(commit);
7537 fprintf(outfile, "numparents %d\n",
7538 got_object_commit_get_nparents(commit));
7539 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7540 char *pid_str;
7541 err = got_object_id_str(&pid_str, pid->id);
7542 if (err)
7543 goto done;
7544 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7545 free(pid_str);
7547 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7548 got_object_commit_get_author(commit),
7549 got_object_commit_get_author_time(commit));
7551 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7552 got_object_commit_get_author(commit),
7553 got_object_commit_get_committer_time(commit));
7555 logmsg = got_object_commit_get_logmsg_raw(commit);
7556 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7557 fprintf(outfile, "%s", logmsg);
7558 done:
7559 free(id_str);
7560 got_object_commit_close(commit);
7561 return err;
7564 static const struct got_error *
7565 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7567 const struct got_error *err;
7568 struct got_tag_object *tag;
7569 char *id_str = NULL;
7570 const char *tagmsg = NULL;
7572 err = got_object_open_as_tag(&tag, repo, id);
7573 if (err)
7574 return err;
7576 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7577 if (err)
7578 goto done;
7580 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7582 switch (got_object_tag_get_object_type(tag)) {
7583 case GOT_OBJ_TYPE_BLOB:
7584 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7585 GOT_OBJ_LABEL_BLOB);
7586 break;
7587 case GOT_OBJ_TYPE_TREE:
7588 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7589 GOT_OBJ_LABEL_TREE);
7590 break;
7591 case GOT_OBJ_TYPE_COMMIT:
7592 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7593 GOT_OBJ_LABEL_COMMIT);
7594 break;
7595 case GOT_OBJ_TYPE_TAG:
7596 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7597 GOT_OBJ_LABEL_TAG);
7598 break;
7599 default:
7600 break;
7603 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7604 got_object_tag_get_name(tag));
7606 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7607 got_object_tag_get_tagger(tag),
7608 got_object_tag_get_tagger_time(tag));
7610 tagmsg = got_object_tag_get_message(tag);
7611 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7612 fprintf(outfile, "%s", tagmsg);
7613 done:
7614 free(id_str);
7615 got_object_tag_close(tag);
7616 return err;
7619 static const struct got_error *
7620 cmd_cat(int argc, char *argv[])
7622 const struct got_error *error;
7623 struct got_repository *repo = NULL;
7624 struct got_worktree *worktree = NULL;
7625 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7626 const char *commit_id_str = NULL;
7627 struct got_object_id *id = NULL, *commit_id = NULL;
7628 int ch, obj_type, i, force_path = 0;
7630 #ifndef PROFILE
7631 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7632 NULL) == -1)
7633 err(1, "pledge");
7634 #endif
7636 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7637 switch (ch) {
7638 case 'c':
7639 commit_id_str = optarg;
7640 break;
7641 case 'r':
7642 repo_path = realpath(optarg, NULL);
7643 if (repo_path == NULL)
7644 return got_error_from_errno2("realpath",
7645 optarg);
7646 got_path_strip_trailing_slashes(repo_path);
7647 break;
7648 case 'P':
7649 force_path = 1;
7650 break;
7651 default:
7652 usage_cat();
7653 /* NOTREACHED */
7657 argc -= optind;
7658 argv += optind;
7660 cwd = getcwd(NULL, 0);
7661 if (cwd == NULL) {
7662 error = got_error_from_errno("getcwd");
7663 goto done;
7665 error = got_worktree_open(&worktree, cwd);
7666 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7667 goto done;
7668 if (worktree) {
7669 if (repo_path == NULL) {
7670 repo_path = strdup(
7671 got_worktree_get_repo_path(worktree));
7672 if (repo_path == NULL) {
7673 error = got_error_from_errno("strdup");
7674 goto done;
7679 if (repo_path == NULL) {
7680 repo_path = getcwd(NULL, 0);
7681 if (repo_path == NULL)
7682 return got_error_from_errno("getcwd");
7685 error = got_repo_open(&repo, repo_path, NULL);
7686 free(repo_path);
7687 if (error != NULL)
7688 goto done;
7690 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7691 if (error)
7692 goto done;
7694 if (commit_id_str == NULL)
7695 commit_id_str = GOT_REF_HEAD;
7696 error = got_repo_match_object_id(&commit_id, NULL,
7697 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7698 if (error)
7699 goto done;
7701 for (i = 0; i < argc; i++) {
7702 if (force_path) {
7703 error = got_object_id_by_path(&id, repo, commit_id,
7704 argv[i]);
7705 if (error)
7706 break;
7707 } else {
7708 error = got_repo_match_object_id(&id, &label, argv[i],
7709 GOT_OBJ_TYPE_ANY, 0, repo);
7710 if (error) {
7711 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7712 error->code != GOT_ERR_NOT_REF)
7713 break;
7714 error = got_object_id_by_path(&id, repo,
7715 commit_id, argv[i]);
7716 if (error)
7717 break;
7721 error = got_object_get_type(&obj_type, repo, id);
7722 if (error)
7723 break;
7725 switch (obj_type) {
7726 case GOT_OBJ_TYPE_BLOB:
7727 error = cat_blob(id, repo, stdout);
7728 break;
7729 case GOT_OBJ_TYPE_TREE:
7730 error = cat_tree(id, repo, stdout);
7731 break;
7732 case GOT_OBJ_TYPE_COMMIT:
7733 error = cat_commit(id, repo, stdout);
7734 break;
7735 case GOT_OBJ_TYPE_TAG:
7736 error = cat_tag(id, repo, stdout);
7737 break;
7738 default:
7739 error = got_error(GOT_ERR_OBJ_TYPE);
7740 break;
7742 if (error)
7743 break;
7744 free(label);
7745 label = NULL;
7746 free(id);
7747 id = NULL;
7749 done:
7750 free(label);
7751 free(id);
7752 free(commit_id);
7753 if (worktree)
7754 got_worktree_close(worktree);
7755 if (repo) {
7756 const struct got_error *repo_error;
7757 repo_error = got_repo_close(repo);
7758 if (error == NULL)
7759 error = repo_error;
7761 return error;