Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_diff.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_opentemp.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static volatile sig_atomic_t sigint_received;
62 static volatile sig_atomic_t sigpipe_received;
64 static void
65 catch_sigint(int signo)
66 {
67 sigint_received = 1;
68 }
70 static void
71 catch_sigpipe(int signo)
72 {
73 sigpipe_received = 1;
74 }
77 struct got_cmd {
78 const char *cmd_name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
82 };
84 __dead static void usage(int);
85 __dead static void usage_init(void);
86 __dead static void usage_import(void);
87 __dead static void usage_checkout(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_update(void);
90 __dead static void usage_log(void);
91 __dead static void usage_diff(void);
92 __dead static void usage_blame(void);
93 __dead static void usage_tree(void);
94 __dead static void usage_status(void);
95 __dead static void usage_ref(void);
96 __dead static void usage_branch(void);
97 __dead static void usage_tag(void);
98 __dead static void usage_add(void);
99 __dead static void usage_remove(void);
100 __dead static void usage_revert(void);
101 __dead static void usage_commit(void);
102 __dead static void usage_cherrypick(void);
103 __dead static void usage_backout(void);
104 __dead static void usage_rebase(void);
105 __dead static void usage_histedit(void);
106 __dead static void usage_integrate(void);
107 __dead static void usage_stage(void);
108 __dead static void usage_unstage(void);
109 __dead static void usage_cat(void);
111 static const struct got_error* cmd_init(int, char *[]);
112 static const struct got_error* cmd_import(int, char *[]);
113 static const struct got_error* cmd_clone(int, char *[]);
114 static const struct got_error* cmd_checkout(int, char *[]);
115 static const struct got_error* cmd_update(int, char *[]);
116 static const struct got_error* cmd_log(int, char *[]);
117 static const struct got_error* cmd_diff(int, char *[]);
118 static const struct got_error* cmd_blame(int, char *[]);
119 static const struct got_error* cmd_tree(int, char *[]);
120 static const struct got_error* cmd_status(int, char *[]);
121 static const struct got_error* cmd_ref(int, char *[]);
122 static const struct got_error* cmd_branch(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_revert(int, char *[]);
127 static const struct got_error* cmd_commit(int, char *[]);
128 static const struct got_error* cmd_cherrypick(int, char *[]);
129 static const struct got_error* cmd_backout(int, char *[]);
130 static const struct got_error* cmd_rebase(int, char *[]);
131 static const struct got_error* cmd_histedit(int, char *[]);
132 static const struct got_error* cmd_integrate(int, char *[]);
133 static const struct got_error* cmd_stage(int, char *[]);
134 static const struct got_error* cmd_unstage(int, char *[]);
135 static const struct got_error* cmd_cat(int, char *[]);
137 static struct got_cmd got_commands[] = {
138 { "init", cmd_init, usage_init, "in" },
139 { "import", cmd_import, usage_import, "im" },
140 { "checkout", cmd_checkout, usage_checkout, "co" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "update", cmd_update, usage_update, "up" },
143 { "log", cmd_log, usage_log, "" },
144 { "diff", cmd_diff, usage_diff, "di" },
145 { "blame", cmd_blame, usage_blame, "bl" },
146 { "tree", cmd_tree, usage_tree, "tr" },
147 { "status", cmd_status, usage_status, "st" },
148 { "ref", cmd_ref, usage_ref, "" },
149 { "branch", cmd_branch, usage_branch, "br" },
150 { "tag", cmd_tag, usage_tag, "" },
151 { "add", cmd_add, usage_add, "" },
152 { "remove", cmd_remove, usage_remove, "rm" },
153 { "revert", cmd_revert, usage_revert, "rv" },
154 { "commit", cmd_commit, usage_commit, "ci" },
155 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 { "backout", cmd_backout, usage_backout, "bo" },
157 { "rebase", cmd_rebase, usage_rebase, "rb" },
158 { "histedit", cmd_histedit, usage_histedit, "he" },
159 { "integrate", cmd_integrate, usage_integrate,"ig" },
160 { "stage", cmd_stage, usage_stage, "sg" },
161 { "unstage", cmd_unstage, usage_unstage, "ug" },
162 { "cat", cmd_cat, usage_cat, "" },
163 };
165 static void
166 list_commands(void)
168 int i;
170 fprintf(stderr, "commands:");
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct got_cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s", cmd->cmd_name);
175 fputc('\n', stderr);
178 int
179 main(int argc, char *argv[])
181 struct got_cmd *cmd;
182 unsigned int i;
183 int ch;
184 int hflag = 0, Vflag = 0;
185 static struct option longopts[] = {
186 { "version", no_argument, NULL, 'V' },
187 { NULL, 0, NULL, 0}
188 };
190 setlocale(LC_CTYPE, "");
192 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 switch (ch) {
194 case 'h':
195 hflag = 1;
196 break;
197 case 'V':
198 Vflag = 1;
199 break;
200 default:
201 usage(hflag);
202 /* NOTREACHED */
206 argc -= optind;
207 argv += optind;
208 optind = 0;
210 if (Vflag) {
211 got_version_print_str();
212 return 1;
215 if (argc <= 0)
216 usage(hflag);
218 signal(SIGINT, catch_sigint);
219 signal(SIGPIPE, catch_sigpipe);
221 for (i = 0; i < nitems(got_commands); i++) {
222 const struct got_error *error;
224 cmd = &got_commands[i];
226 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 strcmp(cmd->cmd_alias, argv[0]) != 0)
228 continue;
230 if (hflag)
231 got_commands[i].cmd_usage();
233 error = got_commands[i].cmd_main(argc, argv);
234 if (error && error->code != GOT_ERR_CANCELLED &&
235 error->code != GOT_ERR_PRIVSEP_EXIT &&
236 !(sigpipe_received &&
237 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 !(sigint_received &&
239 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 return 1;
244 return 0;
247 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 list_commands();
249 return 1;
252 __dead static void
253 usage(int hflag)
255 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 getprogname());
257 if (hflag)
258 list_commands();
259 exit(1);
262 static const struct got_error *
263 get_editor(char **abspath)
265 const struct got_error *err = NULL;
266 const char *editor;
268 *abspath = NULL;
270 editor = getenv("VISUAL");
271 if (editor == NULL)
272 editor = getenv("EDITOR");
274 if (editor) {
275 err = got_path_find_prog(abspath, editor);
276 if (err)
277 return err;
280 if (*abspath == NULL) {
281 *abspath = strdup("/bin/ed");
282 if (*abspath == NULL)
283 return got_error_from_errno("strdup");
286 return NULL;
289 static const struct got_error *
290 apply_unveil(const char *repo_path, int repo_read_only,
291 const char *worktree_path)
293 const struct got_error *err;
295 #ifdef PROFILE
296 if (unveil("gmon.out", "rwc") != 0)
297 return got_error_from_errno2("unveil", "gmon.out");
298 #endif
299 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 return got_error_from_errno2("unveil", repo_path);
302 if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 return got_error_from_errno2("unveil", worktree_path);
305 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 err = got_privsep_unveil_exec_helpers();
309 if (err != NULL)
310 return err;
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
315 return NULL;
318 __dead static void
319 usage_init(void)
321 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 exit(1);
325 static const struct got_error *
326 cmd_init(int argc, char *argv[])
328 const struct got_error *error = NULL;
329 char *repo_path = NULL;
330 int ch;
332 while ((ch = getopt(argc, argv, "")) != -1) {
333 switch (ch) {
334 default:
335 usage_init();
336 /* NOTREACHED */
340 argc -= optind;
341 argv += optind;
343 #ifndef PROFILE
344 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 err(1, "pledge");
346 #endif
347 if (argc != 1)
348 usage_init();
350 repo_path = strdup(argv[0]);
351 if (repo_path == NULL)
352 return got_error_from_errno("strdup");
354 got_path_strip_trailing_slashes(repo_path);
356 error = got_path_mkdir(repo_path);
357 if (error &&
358 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 goto done;
361 error = apply_unveil(repo_path, 0, NULL);
362 if (error)
363 goto done;
365 error = got_repo_init(repo_path);
366 done:
367 free(repo_path);
368 return error;
371 __dead static void
372 usage_import(void)
374 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 "[-r repository-path] [-I pattern] path\n", getprogname());
376 exit(1);
379 int
380 spawn_editor(const char *editor, const char *file)
382 pid_t pid;
383 sig_t sighup, sigint, sigquit;
384 int st = -1;
386 sighup = signal(SIGHUP, SIG_IGN);
387 sigint = signal(SIGINT, SIG_IGN);
388 sigquit = signal(SIGQUIT, SIG_IGN);
390 switch (pid = fork()) {
391 case -1:
392 goto doneediting;
393 case 0:
394 execl(editor, editor, file, (char *)NULL);
395 _exit(127);
398 while (waitpid(pid, &st, 0) == -1)
399 if (errno != EINTR)
400 break;
402 doneediting:
403 (void)signal(SIGHUP, sighup);
404 (void)signal(SIGINT, sigint);
405 (void)signal(SIGQUIT, sigquit);
407 if (!WIFEXITED(st)) {
408 errno = EINTR;
409 return -1;
412 return WEXITSTATUS(st);
415 static const struct got_error *
416 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 const char *initial_content)
419 const struct got_error *err = NULL;
420 char buf[1024];
421 struct stat st, st2;
422 FILE *fp;
423 int content_changed = 0;
424 size_t len;
426 *logmsg = NULL;
428 if (stat(logmsg_path, &st) == -1)
429 return got_error_from_errno2("stat", logmsg_path);
431 if (spawn_editor(editor, logmsg_path) == -1)
432 return got_error_from_errno("failed spawning editor");
434 if (stat(logmsg_path, &st2) == -1)
435 return got_error_from_errno("stat");
437 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "no changes made to commit message, aborting");
441 *logmsg = malloc(st2.st_size + 1);
442 if (*logmsg == NULL)
443 return got_error_from_errno("malloc");
444 (*logmsg)[0] = '\0';
445 len = 0;
447 fp = fopen(logmsg_path, "r");
448 if (fp == NULL) {
449 err = got_error_from_errno("fopen");
450 goto done;
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (!content_changed && strcmp(buf, initial_content) != 0)
454 content_changed = 1;
455 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 continue; /* remove comments and leading empty lines */
457 len = strlcat(*logmsg, buf, st2.st_size);
459 fclose(fp);
461 while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 (*logmsg)[len - 1] = '\0';
463 len--;
466 if (len == 0 || !content_changed)
467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 "commit message cannot be empty, aborting");
469 done:
470 if (err) {
471 free(*logmsg);
472 *logmsg = NULL;
474 return err;
477 static const struct got_error *
478 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 const char *path_dir, const char *branch_name)
481 char *initial_content = NULL;
482 const struct got_error *err = NULL;
483 int fd;
485 if (asprintf(&initial_content,
486 "\n# %s to be imported to branch %s\n", path_dir,
487 branch_name) == -1)
488 return got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(logmsg_path, &fd,
491 GOT_TMPDIR_STR "/got-importmsg");
492 if (err)
493 goto done;
495 dprintf(fd, initial_content);
496 close(fd);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 done:
500 free(initial_content);
501 return err;
504 static const struct got_error *
505 import_progress(void *arg, const char *path)
507 printf("A %s\n", path);
508 return NULL;
511 static const struct got_error *
512 get_author(char **author, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 const char *got_author, *name, *email;
517 *author = NULL;
519 name = got_repo_get_gitconfig_author_name(repo);
520 email = got_repo_get_gitconfig_author_email(repo);
521 if (name && email) {
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
524 return NULL;
527 got_author = getenv("GOT_AUTHOR");
528 if (got_author == NULL) {
529 name = got_repo_get_global_gitconfig_author_name(repo);
530 email = got_repo_get_global_gitconfig_author_email(repo);
531 if (name && email) {
532 if (asprintf(author, "%s <%s>", name, email) == -1)
533 return got_error_from_errno("asprintf");
534 return NULL;
536 /* TODO: Look up user in password database? */
537 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 *author = strdup(got_author);
541 if (*author == NULL)
542 return got_error_from_errno("strdup");
544 /*
545 * Really dumb email address check; we're only doing this to
546 * avoid git's object parser breaking on commits we create.
547 */
548 while (*got_author && *got_author != '<')
549 got_author++;
550 if (*got_author != '<') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 goto done;
554 while (*got_author && *got_author != '@')
555 got_author++;
556 if (*got_author != '@') {
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 goto done;
560 while (*got_author && *got_author != '>')
561 got_author++;
562 if (*got_author != '>')
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 done:
565 if (err) {
566 free(*author);
567 *author = NULL;
569 return err;
572 static const struct got_error *
573 get_gitconfig_path(char **gitconfig_path)
575 const char *homedir = getenv("HOME");
577 *gitconfig_path = NULL;
578 if (homedir) {
579 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 return got_error_from_errno("asprintf");
583 return NULL;
586 static const struct got_error *
587 cmd_import(int argc, char *argv[])
589 const struct got_error *error = NULL;
590 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 const char *branch_name = "main";
593 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 struct got_repository *repo = NULL;
595 struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 struct got_object_id *new_commit_id = NULL;
597 int ch;
598 struct got_pathlist_head ignores;
599 struct got_pathlist_entry *pe;
600 int preserve_logmsg = 0;
602 TAILQ_INIT(&ignores);
604 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 switch (ch) {
606 case 'b':
607 branch_name = optarg;
608 break;
609 case 'm':
610 logmsg = strdup(optarg);
611 if (logmsg == NULL) {
612 error = got_error_from_errno("strdup");
613 goto done;
615 break;
616 case 'r':
617 repo_path = realpath(optarg, NULL);
618 if (repo_path == NULL) {
619 error = got_error_from_errno2("realpath",
620 optarg);
621 goto done;
623 break;
624 case 'I':
625 if (optarg[0] == '\0')
626 break;
627 error = got_pathlist_insert(&pe, &ignores, optarg,
628 NULL);
629 if (error)
630 goto done;
631 break;
632 default:
633 usage_import();
634 /* NOTREACHED */
638 argc -= optind;
639 argv += optind;
641 #ifndef PROFILE
642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 "unveil",
644 NULL) == -1)
645 err(1, "pledge");
646 #endif
647 if (argc != 1)
648 usage_import();
650 if (repo_path == NULL) {
651 repo_path = getcwd(NULL, 0);
652 if (repo_path == NULL)
653 return got_error_from_errno("getcwd");
655 got_path_strip_trailing_slashes(repo_path);
656 error = get_gitconfig_path(&gitconfig_path);
657 if (error)
658 goto done;
659 error = got_repo_open(&repo, repo_path, gitconfig_path);
660 if (error)
661 goto done;
663 error = get_author(&author, repo);
664 if (error)
665 return error;
667 /*
668 * Don't let the user create a branch name with a leading '-'.
669 * While technically a valid reference name, this case is usually
670 * an unintended typo.
671 */
672 if (branch_name[0] == '-')
673 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 error = got_error_from_errno("asprintf");
677 goto done;
680 error = got_ref_open(&branch_ref, repo, refname, 0);
681 if (error) {
682 if (error->code != GOT_ERR_NOT_REF)
683 goto done;
684 } else {
685 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 "import target branch already exists");
687 goto done;
690 path_dir = realpath(argv[0], NULL);
691 if (path_dir == NULL) {
692 error = got_error_from_errno2("realpath", argv[0]);
693 goto done;
695 got_path_strip_trailing_slashes(path_dir);
697 /*
698 * unveil(2) traverses exec(2); if an editor is used we have
699 * to apply unveil after the log message has been written.
700 */
701 if (logmsg == NULL || strlen(logmsg) == 0) {
702 error = get_editor(&editor);
703 if (error)
704 goto done;
705 free(logmsg);
706 error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 path_dir, refname);
708 if (error) {
709 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 logmsg_path != NULL)
711 preserve_logmsg = 1;
712 goto done;
716 if (unveil(path_dir, "r") != 0) {
717 error = got_error_from_errno2("unveil", path_dir);
718 if (logmsg_path)
719 preserve_logmsg = 1;
720 goto done;
723 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 if (error) {
725 if (logmsg_path)
726 preserve_logmsg = 1;
727 goto done;
730 error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 author, &ignores, repo, import_progress, NULL);
732 if (error) {
733 if (logmsg_path)
734 preserve_logmsg = 1;
735 goto done;
738 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 if (error) {
740 if (logmsg_path)
741 preserve_logmsg = 1;
742 goto done;
745 error = got_ref_write(branch_ref, repo);
746 if (error) {
747 if (logmsg_path)
748 preserve_logmsg = 1;
749 goto done;
752 error = got_object_id_str(&id_str, new_commit_id);
753 if (error) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 if (error) {
761 if (error->code != GOT_ERR_NOT_REF) {
762 if (logmsg_path)
763 preserve_logmsg = 1;
764 goto done;
767 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 branch_ref);
769 if (error) {
770 if (logmsg_path)
771 preserve_logmsg = 1;
772 goto done;
775 error = got_ref_write(head_ref, repo);
776 if (error) {
777 if (logmsg_path)
778 preserve_logmsg = 1;
779 goto done;
783 printf("Created branch %s with commit %s\n",
784 got_ref_get_name(branch_ref), id_str);
785 done:
786 if (preserve_logmsg) {
787 fprintf(stderr, "%s: log message preserved in %s\n",
788 getprogname(), logmsg_path);
789 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 error = got_error_from_errno2("unlink", logmsg_path);
791 free(logmsg);
792 free(logmsg_path);
793 free(repo_path);
794 free(editor);
795 free(refname);
796 free(new_commit_id);
797 free(id_str);
798 free(author);
799 free(gitconfig_path);
800 if (branch_ref)
801 got_ref_close(branch_ref);
802 if (head_ref)
803 got_ref_close(head_ref);
804 return error;
807 __dead static void
808 usage_clone(void)
810 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 exit(1);
814 __dead static void
815 usage_checkout(void)
817 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 exit(1);
822 static void
823 show_worktree_base_ref_warning(void)
825 fprintf(stderr, "%s: warning: could not create a reference "
826 "to the work tree's base commit; the commit could be "
827 "garbage-collected by Git; making the repository "
828 "writable and running 'got update' will prevent this\n",
829 getprogname());
832 struct got_checkout_progress_arg {
833 const char *worktree_path;
834 int had_base_commit_ref_error;
835 };
837 static const struct got_error *
838 checkout_progress(void *arg, unsigned char status, const char *path)
840 struct got_checkout_progress_arg *a = arg;
842 /* Base commit bump happens silently. */
843 if (status == GOT_STATUS_BUMP_BASE)
844 return NULL;
846 if (status == GOT_STATUS_BASE_REF_ERR) {
847 a->had_base_commit_ref_error = 1;
848 return NULL;
851 while (path[0] == '/')
852 path++;
854 printf("%c %s/%s\n", status, a->worktree_path, path);
855 return NULL;
858 static const struct got_error *
859 check_cancelled(void *arg)
861 if (sigint_received || sigpipe_received)
862 return got_error(GOT_ERR_CANCELLED);
863 return NULL;
866 static const struct got_error *
867 check_linear_ancestry(struct got_object_id *commit_id,
868 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_object_id *yca_id;
874 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 commit_id, base_commit_id, repo, check_cancelled, NULL);
876 if (err)
877 return err;
879 if (yca_id == NULL)
880 return got_error(GOT_ERR_ANCESTRY);
882 /*
883 * Require a straight line of history between the target commit
884 * and the work tree's base commit.
886 * Non-linear situations such as this require a rebase:
888 * (commit) D F (base_commit)
889 * \ /
890 * C E
891 * \ /
892 * B (yca)
893 * |
894 * A
896 * 'got update' only handles linear cases:
897 * Update forwards in time: A (base/yca) - B - C - D (commit)
898 * Update backwards in time: D (base) - C - B - A (commit/yca)
899 */
900 if (allow_forwards_in_time_only) {
901 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 return got_error(GOT_ERR_ANCESTRY);
903 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 got_object_id_cmp(base_commit_id, yca_id) != 0)
905 return got_error(GOT_ERR_ANCESTRY);
907 free(yca_id);
908 return NULL;
911 static const struct got_error *
912 check_same_branch(struct got_object_id *commit_id,
913 struct got_reference *head_ref, struct got_object_id *yca_id,
914 struct got_repository *repo)
916 const struct got_error *err = NULL;
917 struct got_commit_graph *graph = NULL;
918 struct got_object_id *head_commit_id = NULL;
919 int is_same_branch = 0;
921 err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 if (err)
923 goto done;
925 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 is_same_branch = 1;
927 goto done;
929 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 is_same_branch = 1;
931 goto done;
934 err = got_commit_graph_open(&graph, "/", 1);
935 if (err)
936 goto done;
938 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 check_cancelled, NULL);
940 if (err)
941 goto done;
943 for (;;) {
944 struct got_object_id *id;
945 err = got_commit_graph_iter_next(&id, graph, repo,
946 check_cancelled, NULL);
947 if (err) {
948 if (err->code == GOT_ERR_ITER_COMPLETED)
949 err = NULL;
950 break;
953 if (id) {
954 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 break;
956 if (got_object_id_cmp(id, commit_id) == 0) {
957 is_same_branch = 1;
958 break;
962 done:
963 if (graph)
964 got_commit_graph_close(graph);
965 free(head_commit_id);
966 if (!err && !is_same_branch)
967 err = got_error(GOT_ERR_ANCESTRY);
968 return err;
971 static const struct got_error *
972 cmd_clone(int argc, char *argv[])
974 const struct got_error *err = NULL;
975 const char *uri, *branch_filter, *dirname;
976 char *proto, *host, *port, *repo_name, *server_path;
977 char *default_destdir = NULL, *id_str = NULL;
978 const char *repo_path;
979 struct got_repository *repo = NULL;
980 struct got_pathlist_head refs, symrefs;
981 struct got_pathlist_entry *pe;
982 struct got_object_id *pack_hash = NULL;
983 int ch, fetchfd = -1;
985 TAILQ_INIT(&refs);
986 TAILQ_INIT(&symrefs);
988 while ((ch = getopt(argc, argv, "b:")) != -1) {
989 switch (ch) {
990 case 'b':
991 branch_filter = optarg;
992 break;
993 default:
994 usage_clone();
995 break;
998 argc -= optind;
999 argv += optind;
1000 uri = argv[0];
1001 if(argc == 1)
1002 dirname = NULL;
1003 else if(argc == 2)
1004 dirname = argv[1];
1005 else
1006 usage_clone();
1008 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1009 &repo_name, argv[0]);
1010 if (err)
1011 goto done;
1013 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1014 if (err)
1015 goto done;
1017 if (dirname == NULL) {
1018 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 goto done;
1022 repo_path = default_destdir;
1023 } else
1024 repo_path = dirname;
1026 err = got_path_mkdir(repo_path);
1027 if (err)
1028 goto done;
1030 err = got_repo_init(repo_path);
1031 if (err != NULL)
1032 goto done;
1034 err = got_repo_open(&repo, repo_path, NULL);
1035 if (err)
1036 goto done;
1038 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1039 repo);
1040 if (err)
1041 goto done;
1043 err = got_object_id_str(&id_str, pack_hash);
1044 if (err)
1045 goto done;
1046 printf("Fetched %s.pack\n", id_str);
1047 free(id_str);
1049 /* Set up references provided with the pack file. */
1050 TAILQ_FOREACH(pe, &refs, entry) {
1051 const char *refname = pe->path;
1052 struct got_object_id *id = pe->data;
1053 struct got_reference *ref;
1055 err = got_object_id_str(&id_str, id);
1056 if (err)
1057 goto done;
1059 err = got_ref_alloc(&ref, refname, id);
1060 if (err) {
1061 free(id_str);
1062 goto done;
1065 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1066 free(id_str);
1067 err = got_ref_write(ref, repo);
1068 got_ref_close(ref);
1069 if (err)
1070 goto done;
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *symref, *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 continue;
1082 err = got_ref_open(&target_ref, repo, target, 0);
1083 if (err) {
1084 if (err->code == GOT_ERR_NOT_REF)
1085 continue;
1086 goto done;
1089 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1090 got_ref_close(target_ref);
1091 if (err)
1092 goto done;
1094 printf("Setting %s to %s\n", GOT_REF_HEAD,
1095 got_ref_get_symref_target(symref));
1097 err = got_ref_write(symref, repo);
1098 got_ref_close(symref);
1099 break;
1102 done:
1103 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1104 err = got_error_from_errno("close");
1105 if (repo)
1106 got_repo_close(repo);
1107 TAILQ_FOREACH(pe, &refs, entry) {
1108 free((void *)pe->path);
1109 free(pe->data);
1111 got_pathlist_free(&refs);
1112 TAILQ_FOREACH(pe, &symrefs, entry) {
1113 free((void *)pe->path);
1114 free(pe->data);
1116 got_pathlist_free(&symrefs);
1117 free(pack_hash);
1118 free(proto);
1119 free(host);
1120 free(port);
1121 free(server_path);
1122 free(repo_name);
1123 free(default_destdir);
1124 return err;
1127 static const struct got_error *
1128 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1130 static char msg[512];
1131 const char *branch_name;
1133 if (got_ref_is_symbolic(ref))
1134 branch_name = got_ref_get_symref_target(ref);
1135 else
1136 branch_name = got_ref_get_name(ref);
1138 if (strncmp("refs/heads/", branch_name, 11) == 0)
1139 branch_name += 11;
1141 snprintf(msg, sizeof(msg),
1142 "target commit is not contained in branch '%s'; "
1143 "the branch to use must be specified with -b; "
1144 "if necessary a new branch can be created for "
1145 "this commit with 'got branch -c %s BRANCH_NAME'",
1146 branch_name, commit_id_str);
1148 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1151 static const struct got_error *
1152 cmd_checkout(int argc, char *argv[])
1154 const struct got_error *error = NULL;
1155 struct got_repository *repo = NULL;
1156 struct got_reference *head_ref = NULL;
1157 struct got_worktree *worktree = NULL;
1158 char *repo_path = NULL;
1159 char *worktree_path = NULL;
1160 const char *path_prefix = "";
1161 const char *branch_name = GOT_REF_HEAD;
1162 char *commit_id_str = NULL;
1163 int ch, same_path_prefix, allow_nonempty = 0;
1164 struct got_pathlist_head paths;
1165 struct got_checkout_progress_arg cpa;
1167 TAILQ_INIT(&paths);
1169 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1170 switch (ch) {
1171 case 'b':
1172 branch_name = optarg;
1173 break;
1174 case 'c':
1175 commit_id_str = strdup(optarg);
1176 if (commit_id_str == NULL)
1177 return got_error_from_errno("strdup");
1178 break;
1179 case 'E':
1180 allow_nonempty = 1;
1181 break;
1182 case 'p':
1183 path_prefix = optarg;
1184 break;
1185 default:
1186 usage_checkout();
1187 /* NOTREACHED */
1191 argc -= optind;
1192 argv += optind;
1194 #ifndef PROFILE
1195 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1196 "unveil", NULL) == -1)
1197 err(1, "pledge");
1198 #endif
1199 if (argc == 1) {
1200 char *cwd, *base, *dotgit;
1201 repo_path = realpath(argv[0], NULL);
1202 if (repo_path == NULL)
1203 return got_error_from_errno2("realpath", argv[0]);
1204 cwd = getcwd(NULL, 0);
1205 if (cwd == NULL) {
1206 error = got_error_from_errno("getcwd");
1207 goto done;
1209 if (path_prefix[0]) {
1210 base = basename(path_prefix);
1211 if (base == NULL) {
1212 error = got_error_from_errno2("basename",
1213 path_prefix);
1214 goto done;
1216 } else {
1217 base = basename(repo_path);
1218 if (base == NULL) {
1219 error = got_error_from_errno2("basename",
1220 repo_path);
1221 goto done;
1224 dotgit = strstr(base, ".git");
1225 if (dotgit)
1226 *dotgit = '\0';
1227 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1228 error = got_error_from_errno("asprintf");
1229 free(cwd);
1230 goto done;
1232 free(cwd);
1233 } else if (argc == 2) {
1234 repo_path = realpath(argv[0], NULL);
1235 if (repo_path == NULL) {
1236 error = got_error_from_errno2("realpath", argv[0]);
1237 goto done;
1239 worktree_path = realpath(argv[1], NULL);
1240 if (worktree_path == NULL) {
1241 if (errno != ENOENT) {
1242 error = got_error_from_errno2("realpath",
1243 argv[1]);
1244 goto done;
1246 worktree_path = strdup(argv[1]);
1247 if (worktree_path == NULL) {
1248 error = got_error_from_errno("strdup");
1249 goto done;
1252 } else
1253 usage_checkout();
1255 got_path_strip_trailing_slashes(repo_path);
1256 got_path_strip_trailing_slashes(worktree_path);
1258 error = got_repo_open(&repo, repo_path, NULL);
1259 if (error != NULL)
1260 goto done;
1262 /* Pre-create work tree path for unveil(2) */
1263 error = got_path_mkdir(worktree_path);
1264 if (error) {
1265 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1266 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1267 goto done;
1268 if (!allow_nonempty &&
1269 !got_path_dir_is_empty(worktree_path)) {
1270 error = got_error_path(worktree_path,
1271 GOT_ERR_DIR_NOT_EMPTY);
1272 goto done;
1276 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1277 if (error)
1278 goto done;
1280 error = got_ref_open(&head_ref, repo, branch_name, 0);
1281 if (error != NULL)
1282 goto done;
1284 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1285 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1286 goto done;
1288 error = got_worktree_open(&worktree, worktree_path);
1289 if (error != NULL)
1290 goto done;
1292 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1293 path_prefix);
1294 if (error != NULL)
1295 goto done;
1296 if (!same_path_prefix) {
1297 error = got_error(GOT_ERR_PATH_PREFIX);
1298 goto done;
1301 if (commit_id_str) {
1302 struct got_object_id *commit_id;
1303 error = got_repo_match_object_id(&commit_id, NULL,
1304 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1305 if (error)
1306 goto done;
1307 error = check_linear_ancestry(commit_id,
1308 got_worktree_get_base_commit_id(worktree), 0, repo);
1309 if (error != NULL) {
1310 free(commit_id);
1311 if (error->code == GOT_ERR_ANCESTRY) {
1312 error = checkout_ancestry_error(
1313 head_ref, commit_id_str);
1315 goto done;
1317 error = check_same_branch(commit_id, head_ref, NULL, repo);
1318 if (error) {
1319 if (error->code == GOT_ERR_ANCESTRY) {
1320 error = checkout_ancestry_error(
1321 head_ref, commit_id_str);
1323 goto done;
1325 error = got_worktree_set_base_commit_id(worktree, repo,
1326 commit_id);
1327 free(commit_id);
1328 if (error)
1329 goto done;
1332 error = got_pathlist_append(&paths, "", NULL);
1333 if (error)
1334 goto done;
1335 cpa.worktree_path = worktree_path;
1336 cpa.had_base_commit_ref_error = 0;
1337 error = got_worktree_checkout_files(worktree, &paths, repo,
1338 checkout_progress, &cpa, check_cancelled, NULL);
1339 if (error != NULL)
1340 goto done;
1342 printf("Now shut up and hack\n");
1343 if (cpa.had_base_commit_ref_error)
1344 show_worktree_base_ref_warning();
1345 done:
1346 got_pathlist_free(&paths);
1347 free(commit_id_str);
1348 free(repo_path);
1349 free(worktree_path);
1350 return error;
1353 __dead static void
1354 usage_update(void)
1356 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1357 getprogname());
1358 exit(1);
1361 static const struct got_error *
1362 update_progress(void *arg, unsigned char status, const char *path)
1364 int *did_something = arg;
1366 if (status == GOT_STATUS_EXISTS ||
1367 status == GOT_STATUS_BASE_REF_ERR)
1368 return NULL;
1370 *did_something = 1;
1372 /* Base commit bump happens silently. */
1373 if (status == GOT_STATUS_BUMP_BASE)
1374 return NULL;
1376 while (path[0] == '/')
1377 path++;
1378 printf("%c %s\n", status, path);
1379 return NULL;
1382 static const struct got_error *
1383 switch_head_ref(struct got_reference *head_ref,
1384 struct got_object_id *commit_id, struct got_worktree *worktree,
1385 struct got_repository *repo)
1387 const struct got_error *err = NULL;
1388 char *base_id_str;
1389 int ref_has_moved = 0;
1391 /* Trivial case: switching between two different references. */
1392 if (strcmp(got_ref_get_name(head_ref),
1393 got_worktree_get_head_ref_name(worktree)) != 0) {
1394 printf("Switching work tree from %s to %s\n",
1395 got_worktree_get_head_ref_name(worktree),
1396 got_ref_get_name(head_ref));
1397 return got_worktree_set_head_ref(worktree, head_ref);
1400 err = check_linear_ancestry(commit_id,
1401 got_worktree_get_base_commit_id(worktree), 0, repo);
1402 if (err) {
1403 if (err->code != GOT_ERR_ANCESTRY)
1404 return err;
1405 ref_has_moved = 1;
1407 if (!ref_has_moved)
1408 return NULL;
1410 /* Switching to a rebased branch with the same reference name. */
1411 err = got_object_id_str(&base_id_str,
1412 got_worktree_get_base_commit_id(worktree));
1413 if (err)
1414 return err;
1415 printf("Reference %s now points at a different branch\n",
1416 got_worktree_get_head_ref_name(worktree));
1417 printf("Switching work tree from %s to %s\n", base_id_str,
1418 got_worktree_get_head_ref_name(worktree));
1419 return NULL;
1422 static const struct got_error *
1423 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1425 const struct got_error *err;
1426 int in_progress;
1428 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1429 if (err)
1430 return err;
1431 if (in_progress)
1432 return got_error(GOT_ERR_REBASING);
1434 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1435 if (err)
1436 return err;
1437 if (in_progress)
1438 return got_error(GOT_ERR_HISTEDIT_BUSY);
1440 return NULL;
1443 static const struct got_error *
1444 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1445 char *argv[], struct got_worktree *worktree)
1447 const struct got_error *err = NULL;
1448 char *path;
1449 int i;
1451 if (argc == 0) {
1452 path = strdup("");
1453 if (path == NULL)
1454 return got_error_from_errno("strdup");
1455 return got_pathlist_append(paths, path, NULL);
1458 for (i = 0; i < argc; i++) {
1459 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1460 if (err)
1461 break;
1462 err = got_pathlist_append(paths, path, NULL);
1463 if (err) {
1464 free(path);
1465 break;
1469 return err;
1472 static const struct got_error *
1473 cmd_update(int argc, char *argv[])
1475 const struct got_error *error = NULL;
1476 struct got_repository *repo = NULL;
1477 struct got_worktree *worktree = NULL;
1478 char *worktree_path = NULL;
1479 struct got_object_id *commit_id = NULL;
1480 char *commit_id_str = NULL;
1481 const char *branch_name = NULL;
1482 struct got_reference *head_ref = NULL;
1483 struct got_pathlist_head paths;
1484 struct got_pathlist_entry *pe;
1485 int ch, did_something = 0;
1487 TAILQ_INIT(&paths);
1489 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1490 switch (ch) {
1491 case 'b':
1492 branch_name = optarg;
1493 break;
1494 case 'c':
1495 commit_id_str = strdup(optarg);
1496 if (commit_id_str == NULL)
1497 return got_error_from_errno("strdup");
1498 break;
1499 default:
1500 usage_update();
1501 /* NOTREACHED */
1505 argc -= optind;
1506 argv += optind;
1508 #ifndef PROFILE
1509 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1510 "unveil", NULL) == -1)
1511 err(1, "pledge");
1512 #endif
1513 worktree_path = getcwd(NULL, 0);
1514 if (worktree_path == NULL) {
1515 error = got_error_from_errno("getcwd");
1516 goto done;
1518 error = got_worktree_open(&worktree, worktree_path);
1519 if (error)
1520 goto done;
1522 error = check_rebase_or_histedit_in_progress(worktree);
1523 if (error)
1524 goto done;
1526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1527 NULL);
1528 if (error != NULL)
1529 goto done;
1531 error = apply_unveil(got_repo_get_path(repo), 0,
1532 got_worktree_get_root_path(worktree));
1533 if (error)
1534 goto done;
1536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1537 if (error)
1538 goto done;
1540 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1541 got_worktree_get_head_ref_name(worktree), 0);
1542 if (error != NULL)
1543 goto done;
1544 if (commit_id_str == NULL) {
1545 error = got_ref_resolve(&commit_id, repo, head_ref);
1546 if (error != NULL)
1547 goto done;
1548 error = got_object_id_str(&commit_id_str, commit_id);
1549 if (error != NULL)
1550 goto done;
1551 } else {
1552 error = got_repo_match_object_id(&commit_id, NULL,
1553 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1554 free(commit_id_str);
1555 commit_id_str = NULL;
1556 if (error)
1557 goto done;
1558 error = got_object_id_str(&commit_id_str, commit_id);
1559 if (error)
1560 goto done;
1563 if (branch_name) {
1564 struct got_object_id *head_commit_id;
1565 TAILQ_FOREACH(pe, &paths, entry) {
1566 if (pe->path_len == 0)
1567 continue;
1568 error = got_error_msg(GOT_ERR_BAD_PATH,
1569 "switching between branches requires that "
1570 "the entire work tree gets updated");
1571 goto done;
1573 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1574 if (error)
1575 goto done;
1576 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1577 repo);
1578 free(head_commit_id);
1579 if (error != NULL)
1580 goto done;
1581 error = check_same_branch(commit_id, head_ref, NULL, repo);
1582 if (error)
1583 goto done;
1584 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1585 if (error)
1586 goto done;
1587 } else {
1588 error = check_linear_ancestry(commit_id,
1589 got_worktree_get_base_commit_id(worktree), 0, repo);
1590 if (error != NULL) {
1591 if (error->code == GOT_ERR_ANCESTRY)
1592 error = got_error(GOT_ERR_BRANCH_MOVED);
1593 goto done;
1595 error = check_same_branch(commit_id, head_ref, NULL, repo);
1596 if (error)
1597 goto done;
1600 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1601 commit_id) != 0) {
1602 error = got_worktree_set_base_commit_id(worktree, repo,
1603 commit_id);
1604 if (error)
1605 goto done;
1608 error = got_worktree_checkout_files(worktree, &paths, repo,
1609 update_progress, &did_something, check_cancelled, NULL);
1610 if (error != NULL)
1611 goto done;
1613 if (did_something)
1614 printf("Updated to commit %s\n", commit_id_str);
1615 else
1616 printf("Already up-to-date\n");
1617 done:
1618 free(worktree_path);
1619 TAILQ_FOREACH(pe, &paths, entry)
1620 free((char *)pe->path);
1621 got_pathlist_free(&paths);
1622 free(commit_id);
1623 free(commit_id_str);
1624 return error;
1627 static const struct got_error *
1628 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1629 const char *path, int diff_context, int ignore_whitespace,
1630 struct got_repository *repo)
1632 const struct got_error *err = NULL;
1633 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1635 if (blob_id1) {
1636 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1637 if (err)
1638 goto done;
1641 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1642 if (err)
1643 goto done;
1645 while (path[0] == '/')
1646 path++;
1647 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1648 ignore_whitespace, stdout);
1649 done:
1650 if (blob1)
1651 got_object_blob_close(blob1);
1652 got_object_blob_close(blob2);
1653 return err;
1656 static const struct got_error *
1657 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1658 const char *path, int diff_context, int ignore_whitespace,
1659 struct got_repository *repo)
1661 const struct got_error *err = NULL;
1662 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1663 struct got_diff_blob_output_unidiff_arg arg;
1665 if (tree_id1) {
1666 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1667 if (err)
1668 goto done;
1671 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1672 if (err)
1673 goto done;
1675 arg.diff_context = diff_context;
1676 arg.ignore_whitespace = ignore_whitespace;
1677 arg.outfile = stdout;
1678 while (path[0] == '/')
1679 path++;
1680 err = got_diff_tree(tree1, tree2, path, path, repo,
1681 got_diff_blob_output_unidiff, &arg, 1);
1682 done:
1683 if (tree1)
1684 got_object_tree_close(tree1);
1685 if (tree2)
1686 got_object_tree_close(tree2);
1687 return err;
1690 static const struct got_error *
1691 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1692 const char *path, int diff_context, struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_commit_object *pcommit = NULL;
1696 char *id_str1 = NULL, *id_str2 = NULL;
1697 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1698 struct got_object_qid *qid;
1700 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1701 if (qid != NULL) {
1702 err = got_object_open_as_commit(&pcommit, repo,
1703 qid->id);
1704 if (err)
1705 return err;
1708 if (path && path[0] != '\0') {
1709 int obj_type;
1710 err = got_object_id_by_path(&obj_id2, repo, id, path);
1711 if (err)
1712 goto done;
1713 err = got_object_id_str(&id_str2, obj_id2);
1714 if (err) {
1715 free(obj_id2);
1716 goto done;
1718 if (pcommit) {
1719 err = got_object_id_by_path(&obj_id1, repo,
1720 qid->id, path);
1721 if (err) {
1722 free(obj_id2);
1723 goto done;
1725 err = got_object_id_str(&id_str1, obj_id1);
1726 if (err) {
1727 free(obj_id2);
1728 goto done;
1731 err = got_object_get_type(&obj_type, repo, obj_id2);
1732 if (err) {
1733 free(obj_id2);
1734 goto done;
1736 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1737 switch (obj_type) {
1738 case GOT_OBJ_TYPE_BLOB:
1739 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1740 0, repo);
1741 break;
1742 case GOT_OBJ_TYPE_TREE:
1743 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1744 0, repo);
1745 break;
1746 default:
1747 err = got_error(GOT_ERR_OBJ_TYPE);
1748 break;
1750 free(obj_id1);
1751 free(obj_id2);
1752 } else {
1753 obj_id2 = got_object_commit_get_tree_id(commit);
1754 err = got_object_id_str(&id_str2, obj_id2);
1755 if (err)
1756 goto done;
1757 obj_id1 = got_object_commit_get_tree_id(pcommit);
1758 err = got_object_id_str(&id_str1, obj_id1);
1759 if (err)
1760 goto done;
1761 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1762 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1764 done:
1765 free(id_str1);
1766 free(id_str2);
1767 if (pcommit)
1768 got_object_commit_close(pcommit);
1769 return err;
1772 static char *
1773 get_datestr(time_t *time, char *datebuf)
1775 struct tm mytm, *tm;
1776 char *p, *s;
1778 tm = gmtime_r(time, &mytm);
1779 if (tm == NULL)
1780 return NULL;
1781 s = asctime_r(tm, datebuf);
1782 if (s == NULL)
1783 return NULL;
1784 p = strchr(s, '\n');
1785 if (p)
1786 *p = '\0';
1787 return s;
1790 static const struct got_error *
1791 match_logmsg(int *have_match, struct got_object_id *id,
1792 struct got_commit_object *commit, regex_t *regex)
1794 const struct got_error *err = NULL;
1795 regmatch_t regmatch;
1796 char *id_str = NULL, *logmsg = NULL;
1798 *have_match = 0;
1800 err = got_object_id_str(&id_str, id);
1801 if (err)
1802 return err;
1804 err = got_object_commit_get_logmsg(&logmsg, commit);
1805 if (err)
1806 goto done;
1808 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1809 *have_match = 1;
1810 done:
1811 free(id_str);
1812 free(logmsg);
1813 return err;
1816 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1818 static const struct got_error *
1819 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1820 struct got_repository *repo, const char *path, int show_patch,
1821 int diff_context, struct got_reflist_head *refs)
1823 const struct got_error *err = NULL;
1824 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1825 char datebuf[26];
1826 time_t committer_time;
1827 const char *author, *committer;
1828 char *refs_str = NULL;
1829 struct got_reflist_entry *re;
1831 SIMPLEQ_FOREACH(re, refs, entry) {
1832 char *s;
1833 const char *name;
1834 struct got_tag_object *tag = NULL;
1835 int cmp;
1837 name = got_ref_get_name(re->ref);
1838 if (strcmp(name, GOT_REF_HEAD) == 0)
1839 continue;
1840 if (strncmp(name, "refs/", 5) == 0)
1841 name += 5;
1842 if (strncmp(name, "got/", 4) == 0)
1843 continue;
1844 if (strncmp(name, "heads/", 6) == 0)
1845 name += 6;
1846 if (strncmp(name, "remotes/", 8) == 0)
1847 name += 8;
1848 if (strncmp(name, "tags/", 5) == 0) {
1849 err = got_object_open_as_tag(&tag, repo, re->id);
1850 if (err) {
1851 if (err->code != GOT_ERR_OBJ_TYPE)
1852 return err;
1853 /* Ref points at something other than a tag. */
1854 err = NULL;
1855 tag = NULL;
1858 cmp = got_object_id_cmp(tag ?
1859 got_object_tag_get_object_id(tag) : re->id, id);
1860 if (tag)
1861 got_object_tag_close(tag);
1862 if (cmp != 0)
1863 continue;
1864 s = refs_str;
1865 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1866 name) == -1) {
1867 err = got_error_from_errno("asprintf");
1868 free(s);
1869 return err;
1871 free(s);
1873 err = got_object_id_str(&id_str, id);
1874 if (err)
1875 return err;
1877 printf(GOT_COMMIT_SEP_STR);
1878 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1879 refs_str ? refs_str : "", refs_str ? ")" : "");
1880 free(id_str);
1881 id_str = NULL;
1882 free(refs_str);
1883 refs_str = NULL;
1884 printf("from: %s\n", got_object_commit_get_author(commit));
1885 committer_time = got_object_commit_get_committer_time(commit);
1886 datestr = get_datestr(&committer_time, datebuf);
1887 if (datestr)
1888 printf("date: %s UTC\n", datestr);
1889 author = got_object_commit_get_author(commit);
1890 committer = got_object_commit_get_committer(commit);
1891 if (strcmp(author, committer) != 0)
1892 printf("via: %s\n", committer);
1893 if (got_object_commit_get_nparents(commit) > 1) {
1894 const struct got_object_id_queue *parent_ids;
1895 struct got_object_qid *qid;
1896 int n = 1;
1897 parent_ids = got_object_commit_get_parent_ids(commit);
1898 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1899 err = got_object_id_str(&id_str, qid->id);
1900 if (err)
1901 return err;
1902 printf("parent %d: %s\n", n++, id_str);
1903 free(id_str);
1907 err = got_object_commit_get_logmsg(&logmsg0, commit);
1908 if (err)
1909 return err;
1911 logmsg = logmsg0;
1912 do {
1913 line = strsep(&logmsg, "\n");
1914 if (line)
1915 printf(" %s\n", line);
1916 } while (line);
1917 free(logmsg0);
1919 if (show_patch) {
1920 err = print_patch(commit, id, path, diff_context, repo);
1921 if (err == 0)
1922 printf("\n");
1925 if (fflush(stdout) != 0 && err == NULL)
1926 err = got_error_from_errno("fflush");
1927 return err;
1930 static const struct got_error *
1931 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1932 const char *path, int show_patch, const char *search_pattern,
1933 int diff_context, int limit, int log_branches,
1934 struct got_reflist_head *refs)
1936 const struct got_error *err;
1937 struct got_commit_graph *graph;
1938 regex_t regex;
1939 int have_match;
1941 if (search_pattern &&
1942 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1943 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1945 err = got_commit_graph_open(&graph, path, !log_branches);
1946 if (err)
1947 return err;
1948 err = got_commit_graph_iter_start(graph, root_id, repo,
1949 check_cancelled, NULL);
1950 if (err)
1951 goto done;
1952 for (;;) {
1953 struct got_commit_object *commit;
1954 struct got_object_id *id;
1956 if (sigint_received || sigpipe_received)
1957 break;
1959 err = got_commit_graph_iter_next(&id, graph, repo,
1960 check_cancelled, NULL);
1961 if (err) {
1962 if (err->code == GOT_ERR_ITER_COMPLETED)
1963 err = NULL;
1964 break;
1966 if (id == NULL)
1967 break;
1969 err = got_object_open_as_commit(&commit, repo, id);
1970 if (err)
1971 break;
1973 if (search_pattern) {
1974 err = match_logmsg(&have_match, id, commit, &regex);
1975 if (err) {
1976 got_object_commit_close(commit);
1977 break;
1979 if (have_match == 0) {
1980 got_object_commit_close(commit);
1981 continue;
1985 err = print_commit(commit, id, repo, path, show_patch,
1986 diff_context, refs);
1987 got_object_commit_close(commit);
1988 if (err || (limit && --limit == 0))
1989 break;
1991 done:
1992 if (search_pattern)
1993 regfree(&regex);
1994 got_commit_graph_close(graph);
1995 return err;
1998 __dead static void
1999 usage_log(void)
2001 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2002 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2003 exit(1);
2006 static int
2007 get_default_log_limit(void)
2009 const char *got_default_log_limit;
2010 long long n;
2011 const char *errstr;
2013 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2014 if (got_default_log_limit == NULL)
2015 return 0;
2016 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2017 if (errstr != NULL)
2018 return 0;
2019 return n;
2022 static const struct got_error *
2023 cmd_log(int argc, char *argv[])
2025 const struct got_error *error;
2026 struct got_repository *repo = NULL;
2027 struct got_worktree *worktree = NULL;
2028 struct got_commit_object *commit = NULL;
2029 struct got_object_id *id = NULL;
2030 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2031 const char *start_commit = NULL, *search_pattern = NULL;
2032 int diff_context = -1, ch;
2033 int show_patch = 0, limit = 0, log_branches = 0;
2034 const char *errstr;
2035 struct got_reflist_head refs;
2037 SIMPLEQ_INIT(&refs);
2039 #ifndef PROFILE
2040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2041 NULL)
2042 == -1)
2043 err(1, "pledge");
2044 #endif
2046 limit = get_default_log_limit();
2048 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2049 switch (ch) {
2050 case 'p':
2051 show_patch = 1;
2052 break;
2053 case 'c':
2054 start_commit = optarg;
2055 break;
2056 case 'C':
2057 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2058 &errstr);
2059 if (errstr != NULL)
2060 err(1, "-C option %s", errstr);
2061 break;
2062 case 'l':
2063 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2064 if (errstr != NULL)
2065 err(1, "-l option %s", errstr);
2066 break;
2067 case 'b':
2068 log_branches = 1;
2069 break;
2070 case 'r':
2071 repo_path = realpath(optarg, NULL);
2072 if (repo_path == NULL)
2073 return got_error_from_errno2("realpath",
2074 optarg);
2075 got_path_strip_trailing_slashes(repo_path);
2076 break;
2077 case 's':
2078 search_pattern = optarg;
2079 break;
2080 default:
2081 usage_log();
2082 /* NOTREACHED */
2086 argc -= optind;
2087 argv += optind;
2089 if (diff_context == -1)
2090 diff_context = 3;
2091 else if (!show_patch)
2092 errx(1, "-C reguires -p");
2094 cwd = getcwd(NULL, 0);
2095 if (cwd == NULL) {
2096 error = got_error_from_errno("getcwd");
2097 goto done;
2100 error = got_worktree_open(&worktree, cwd);
2101 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2102 goto done;
2103 error = NULL;
2105 if (argc == 0) {
2106 path = strdup("");
2107 if (path == NULL) {
2108 error = got_error_from_errno("strdup");
2109 goto done;
2111 } else if (argc == 1) {
2112 if (worktree) {
2113 error = got_worktree_resolve_path(&path, worktree,
2114 argv[0]);
2115 if (error)
2116 goto done;
2117 } else {
2118 path = strdup(argv[0]);
2119 if (path == NULL) {
2120 error = got_error_from_errno("strdup");
2121 goto done;
2124 } else
2125 usage_log();
2127 if (repo_path == NULL) {
2128 repo_path = worktree ?
2129 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2131 if (repo_path == NULL) {
2132 error = got_error_from_errno("strdup");
2133 goto done;
2136 error = got_repo_open(&repo, repo_path, NULL);
2137 if (error != NULL)
2138 goto done;
2140 error = apply_unveil(got_repo_get_path(repo), 1,
2141 worktree ? got_worktree_get_root_path(worktree) : NULL);
2142 if (error)
2143 goto done;
2145 if (start_commit == NULL) {
2146 struct got_reference *head_ref;
2147 error = got_ref_open(&head_ref, repo,
2148 worktree ? got_worktree_get_head_ref_name(worktree)
2149 : GOT_REF_HEAD, 0);
2150 if (error != NULL)
2151 return error;
2152 error = got_ref_resolve(&id, repo, head_ref);
2153 got_ref_close(head_ref);
2154 if (error != NULL)
2155 return error;
2156 error = got_object_open_as_commit(&commit, repo, id);
2157 } else {
2158 struct got_reference *ref;
2159 error = got_ref_open(&ref, repo, start_commit, 0);
2160 if (error == NULL) {
2161 int obj_type;
2162 error = got_ref_resolve(&id, repo, ref);
2163 got_ref_close(ref);
2164 if (error != NULL)
2165 goto done;
2166 error = got_object_get_type(&obj_type, repo, id);
2167 if (error != NULL)
2168 goto done;
2169 if (obj_type == GOT_OBJ_TYPE_TAG) {
2170 struct got_tag_object *tag;
2171 error = got_object_open_as_tag(&tag, repo, id);
2172 if (error != NULL)
2173 goto done;
2174 if (got_object_tag_get_object_type(tag) !=
2175 GOT_OBJ_TYPE_COMMIT) {
2176 got_object_tag_close(tag);
2177 error = got_error(GOT_ERR_OBJ_TYPE);
2178 goto done;
2180 free(id);
2181 id = got_object_id_dup(
2182 got_object_tag_get_object_id(tag));
2183 if (id == NULL)
2184 error = got_error_from_errno(
2185 "got_object_id_dup");
2186 got_object_tag_close(tag);
2187 if (error)
2188 goto done;
2189 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2190 error = got_error(GOT_ERR_OBJ_TYPE);
2191 goto done;
2193 error = got_object_open_as_commit(&commit, repo, id);
2194 if (error != NULL)
2195 goto done;
2197 if (commit == NULL) {
2198 error = got_repo_match_object_id_prefix(&id,
2199 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2200 if (error != NULL)
2201 return error;
2204 if (error != NULL)
2205 goto done;
2207 if (worktree) {
2208 const char *prefix = got_worktree_get_path_prefix(worktree);
2209 char *p;
2210 if (asprintf(&p, "%s%s%s", prefix,
2211 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2212 error = got_error_from_errno("asprintf");
2213 goto done;
2215 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2216 free(p);
2217 } else
2218 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2219 if (error != NULL)
2220 goto done;
2221 if (in_repo_path) {
2222 free(path);
2223 path = in_repo_path;
2226 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2227 if (error)
2228 goto done;
2230 error = print_commits(id, repo, path, show_patch, search_pattern,
2231 diff_context, limit, log_branches, &refs);
2232 done:
2233 free(path);
2234 free(repo_path);
2235 free(cwd);
2236 free(id);
2237 if (worktree)
2238 got_worktree_close(worktree);
2239 if (repo) {
2240 const struct got_error *repo_error;
2241 repo_error = got_repo_close(repo);
2242 if (error == NULL)
2243 error = repo_error;
2245 got_ref_list_free(&refs);
2246 return error;
2249 __dead static void
2250 usage_diff(void)
2252 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2253 "[-w] [object1 object2 | path]\n", getprogname());
2254 exit(1);
2257 struct print_diff_arg {
2258 struct got_repository *repo;
2259 struct got_worktree *worktree;
2260 int diff_context;
2261 const char *id_str;
2262 int header_shown;
2263 int diff_staged;
2264 int ignore_whitespace;
2267 static const struct got_error *
2268 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2269 const char *path, struct got_object_id *blob_id,
2270 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2271 int dirfd, const char *de_name)
2273 struct print_diff_arg *a = arg;
2274 const struct got_error *err = NULL;
2275 struct got_blob_object *blob1 = NULL;
2276 int fd = -1;
2277 FILE *f2 = NULL;
2278 char *abspath = NULL, *label1 = NULL;
2279 struct stat sb;
2281 if (a->diff_staged) {
2282 if (staged_status != GOT_STATUS_MODIFY &&
2283 staged_status != GOT_STATUS_ADD &&
2284 staged_status != GOT_STATUS_DELETE)
2285 return NULL;
2286 } else {
2287 if (staged_status == GOT_STATUS_DELETE)
2288 return NULL;
2289 if (status == GOT_STATUS_NONEXISTENT)
2290 return got_error_set_errno(ENOENT, path);
2291 if (status != GOT_STATUS_MODIFY &&
2292 status != GOT_STATUS_ADD &&
2293 status != GOT_STATUS_DELETE &&
2294 status != GOT_STATUS_CONFLICT)
2295 return NULL;
2298 if (!a->header_shown) {
2299 printf("diff %s %s%s\n", a->id_str,
2300 got_worktree_get_root_path(a->worktree),
2301 a->diff_staged ? " (staged changes)" : "");
2302 a->header_shown = 1;
2305 if (a->diff_staged) {
2306 const char *label1 = NULL, *label2 = NULL;
2307 switch (staged_status) {
2308 case GOT_STATUS_MODIFY:
2309 label1 = path;
2310 label2 = path;
2311 break;
2312 case GOT_STATUS_ADD:
2313 label2 = path;
2314 break;
2315 case GOT_STATUS_DELETE:
2316 label1 = path;
2317 break;
2318 default:
2319 return got_error(GOT_ERR_FILE_STATUS);
2321 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2322 label1, label2, a->diff_context, a->ignore_whitespace,
2323 a->repo, stdout);
2326 if (staged_status == GOT_STATUS_ADD ||
2327 staged_status == GOT_STATUS_MODIFY) {
2328 char *id_str;
2329 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2330 8192);
2331 if (err)
2332 goto done;
2333 err = got_object_id_str(&id_str, staged_blob_id);
2334 if (err)
2335 goto done;
2336 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2337 err = got_error_from_errno("asprintf");
2338 free(id_str);
2339 goto done;
2341 free(id_str);
2342 } else if (status != GOT_STATUS_ADD) {
2343 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2344 if (err)
2345 goto done;
2348 if (status != GOT_STATUS_DELETE) {
2349 if (asprintf(&abspath, "%s/%s",
2350 got_worktree_get_root_path(a->worktree), path) == -1) {
2351 err = got_error_from_errno("asprintf");
2352 goto done;
2355 if (dirfd != -1) {
2356 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2357 if (fd == -1) {
2358 err = got_error_from_errno2("openat", abspath);
2359 goto done;
2361 } else {
2362 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2363 if (fd == -1) {
2364 err = got_error_from_errno2("open", abspath);
2365 goto done;
2368 if (fstat(fd, &sb) == -1) {
2369 err = got_error_from_errno2("fstat", abspath);
2370 goto done;
2372 f2 = fdopen(fd, "r");
2373 if (f2 == NULL) {
2374 err = got_error_from_errno2("fdopen", abspath);
2375 goto done;
2377 fd = -1;
2378 } else
2379 sb.st_size = 0;
2381 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2382 a->diff_context, a->ignore_whitespace, stdout);
2383 done:
2384 if (blob1)
2385 got_object_blob_close(blob1);
2386 if (f2 && fclose(f2) == EOF && err == NULL)
2387 err = got_error_from_errno("fclose");
2388 if (fd != -1 && close(fd) == -1 && err == NULL)
2389 err = got_error_from_errno("close");
2390 free(abspath);
2391 return err;
2394 static const struct got_error *
2395 cmd_diff(int argc, char *argv[])
2397 const struct got_error *error;
2398 struct got_repository *repo = NULL;
2399 struct got_worktree *worktree = NULL;
2400 char *cwd = NULL, *repo_path = NULL;
2401 struct got_object_id *id1 = NULL, *id2 = NULL;
2402 const char *id_str1 = NULL, *id_str2 = NULL;
2403 char *label1 = NULL, *label2 = NULL;
2404 int type1, type2;
2405 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2406 const char *errstr;
2407 char *path = NULL;
2409 #ifndef PROFILE
2410 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2411 NULL) == -1)
2412 err(1, "pledge");
2413 #endif
2415 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2416 switch (ch) {
2417 case 'C':
2418 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2419 &errstr);
2420 if (errstr != NULL)
2421 err(1, "-C option %s", errstr);
2422 break;
2423 case 'r':
2424 repo_path = realpath(optarg, NULL);
2425 if (repo_path == NULL)
2426 return got_error_from_errno2("realpath",
2427 optarg);
2428 got_path_strip_trailing_slashes(repo_path);
2429 break;
2430 case 's':
2431 diff_staged = 1;
2432 break;
2433 case 'w':
2434 ignore_whitespace = 1;
2435 break;
2436 default:
2437 usage_diff();
2438 /* NOTREACHED */
2442 argc -= optind;
2443 argv += optind;
2445 cwd = getcwd(NULL, 0);
2446 if (cwd == NULL) {
2447 error = got_error_from_errno("getcwd");
2448 goto done;
2450 error = got_worktree_open(&worktree, cwd);
2451 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2452 goto done;
2453 if (argc <= 1) {
2454 if (worktree == NULL) {
2455 error = got_error(GOT_ERR_NOT_WORKTREE);
2456 goto done;
2458 if (repo_path)
2459 errx(1,
2460 "-r option can't be used when diffing a work tree");
2461 repo_path = strdup(got_worktree_get_repo_path(worktree));
2462 if (repo_path == NULL) {
2463 error = got_error_from_errno("strdup");
2464 goto done;
2466 if (argc == 1) {
2467 error = got_worktree_resolve_path(&path, worktree,
2468 argv[0]);
2469 if (error)
2470 goto done;
2471 } else {
2472 path = strdup("");
2473 if (path == NULL) {
2474 error = got_error_from_errno("strdup");
2475 goto done;
2478 } else if (argc == 2) {
2479 if (diff_staged)
2480 errx(1, "-s option can't be used when diffing "
2481 "objects in repository");
2482 id_str1 = argv[0];
2483 id_str2 = argv[1];
2484 if (worktree && repo_path == NULL) {
2485 repo_path =
2486 strdup(got_worktree_get_repo_path(worktree));
2487 if (repo_path == NULL) {
2488 error = got_error_from_errno("strdup");
2489 goto done;
2492 } else
2493 usage_diff();
2495 if (repo_path == NULL) {
2496 repo_path = getcwd(NULL, 0);
2497 if (repo_path == NULL)
2498 return got_error_from_errno("getcwd");
2501 error = got_repo_open(&repo, repo_path, NULL);
2502 free(repo_path);
2503 if (error != NULL)
2504 goto done;
2506 error = apply_unveil(got_repo_get_path(repo), 1,
2507 worktree ? got_worktree_get_root_path(worktree) : NULL);
2508 if (error)
2509 goto done;
2511 if (argc <= 1) {
2512 struct print_diff_arg arg;
2513 struct got_pathlist_head paths;
2514 char *id_str;
2516 TAILQ_INIT(&paths);
2518 error = got_object_id_str(&id_str,
2519 got_worktree_get_base_commit_id(worktree));
2520 if (error)
2521 goto done;
2522 arg.repo = repo;
2523 arg.worktree = worktree;
2524 arg.diff_context = diff_context;
2525 arg.id_str = id_str;
2526 arg.header_shown = 0;
2527 arg.diff_staged = diff_staged;
2528 arg.ignore_whitespace = ignore_whitespace;
2530 error = got_pathlist_append(&paths, path, NULL);
2531 if (error)
2532 goto done;
2534 error = got_worktree_status(worktree, &paths, repo, print_diff,
2535 &arg, check_cancelled, NULL);
2536 free(id_str);
2537 got_pathlist_free(&paths);
2538 goto done;
2541 error = got_repo_match_object_id(&id1, &label1, id_str1,
2542 GOT_OBJ_TYPE_ANY, 1, repo);
2543 if (error)
2544 goto done;
2546 error = got_repo_match_object_id(&id2, &label2, id_str2,
2547 GOT_OBJ_TYPE_ANY, 1, repo);
2548 if (error)
2549 goto done;
2551 error = got_object_get_type(&type1, repo, id1);
2552 if (error)
2553 goto done;
2555 error = got_object_get_type(&type2, repo, id2);
2556 if (error)
2557 goto done;
2559 if (type1 != type2) {
2560 error = got_error(GOT_ERR_OBJ_TYPE);
2561 goto done;
2564 switch (type1) {
2565 case GOT_OBJ_TYPE_BLOB:
2566 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2567 diff_context, ignore_whitespace, repo, stdout);
2568 break;
2569 case GOT_OBJ_TYPE_TREE:
2570 error = got_diff_objects_as_trees(id1, id2, "", "",
2571 diff_context, ignore_whitespace, repo, stdout);
2572 break;
2573 case GOT_OBJ_TYPE_COMMIT:
2574 printf("diff %s %s\n", label1, label2);
2575 error = got_diff_objects_as_commits(id1, id2, diff_context,
2576 ignore_whitespace, repo, stdout);
2577 break;
2578 default:
2579 error = got_error(GOT_ERR_OBJ_TYPE);
2581 done:
2582 free(label1);
2583 free(label2);
2584 free(id1);
2585 free(id2);
2586 free(path);
2587 if (worktree)
2588 got_worktree_close(worktree);
2589 if (repo) {
2590 const struct got_error *repo_error;
2591 repo_error = got_repo_close(repo);
2592 if (error == NULL)
2593 error = repo_error;
2595 return error;
2598 __dead static void
2599 usage_blame(void)
2601 fprintf(stderr,
2602 "usage: %s blame [-c commit] [-r repository-path] path\n",
2603 getprogname());
2604 exit(1);
2607 struct blame_line {
2608 int annotated;
2609 char *id_str;
2610 char *committer;
2611 char datebuf[11]; /* YYYY-MM-DD + NUL */
2614 struct blame_cb_args {
2615 struct blame_line *lines;
2616 int nlines;
2617 int nlines_prec;
2618 int lineno_cur;
2619 off_t *line_offsets;
2620 FILE *f;
2621 struct got_repository *repo;
2624 static const struct got_error *
2625 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2627 const struct got_error *err = NULL;
2628 struct blame_cb_args *a = arg;
2629 struct blame_line *bline;
2630 char *line = NULL;
2631 size_t linesize = 0;
2632 struct got_commit_object *commit = NULL;
2633 off_t offset;
2634 struct tm tm;
2635 time_t committer_time;
2637 if (nlines != a->nlines ||
2638 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2639 return got_error(GOT_ERR_RANGE);
2641 if (sigint_received)
2642 return got_error(GOT_ERR_ITER_COMPLETED);
2644 if (lineno == -1)
2645 return NULL; /* no change in this commit */
2647 /* Annotate this line. */
2648 bline = &a->lines[lineno - 1];
2649 if (bline->annotated)
2650 return NULL;
2651 err = got_object_id_str(&bline->id_str, id);
2652 if (err)
2653 return err;
2655 err = got_object_open_as_commit(&commit, a->repo, id);
2656 if (err)
2657 goto done;
2659 bline->committer = strdup(got_object_commit_get_committer(commit));
2660 if (bline->committer == NULL) {
2661 err = got_error_from_errno("strdup");
2662 goto done;
2665 committer_time = got_object_commit_get_committer_time(commit);
2666 if (localtime_r(&committer_time, &tm) == NULL)
2667 return got_error_from_errno("localtime_r");
2668 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2669 &tm) >= sizeof(bline->datebuf)) {
2670 err = got_error(GOT_ERR_NO_SPACE);
2671 goto done;
2673 bline->annotated = 1;
2675 /* Print lines annotated so far. */
2676 bline = &a->lines[a->lineno_cur - 1];
2677 if (!bline->annotated)
2678 goto done;
2680 offset = a->line_offsets[a->lineno_cur - 1];
2681 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2682 err = got_error_from_errno("fseeko");
2683 goto done;
2686 while (bline->annotated) {
2687 char *smallerthan, *at, *nl, *committer;
2688 size_t len;
2690 if (getline(&line, &linesize, a->f) == -1) {
2691 if (ferror(a->f))
2692 err = got_error_from_errno("getline");
2693 break;
2696 committer = bline->committer;
2697 smallerthan = strchr(committer, '<');
2698 if (smallerthan && smallerthan[1] != '\0')
2699 committer = smallerthan + 1;
2700 at = strchr(committer, '@');
2701 if (at)
2702 *at = '\0';
2703 len = strlen(committer);
2704 if (len >= 9)
2705 committer[8] = '\0';
2707 nl = strchr(line, '\n');
2708 if (nl)
2709 *nl = '\0';
2710 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2711 bline->id_str, bline->datebuf, committer, line);
2713 a->lineno_cur++;
2714 bline = &a->lines[a->lineno_cur - 1];
2716 done:
2717 if (commit)
2718 got_object_commit_close(commit);
2719 free(line);
2720 return err;
2723 static const struct got_error *
2724 cmd_blame(int argc, char *argv[])
2726 const struct got_error *error;
2727 struct got_repository *repo = NULL;
2728 struct got_worktree *worktree = NULL;
2729 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2730 struct got_object_id *obj_id = NULL;
2731 struct got_object_id *commit_id = NULL;
2732 struct got_blob_object *blob = NULL;
2733 char *commit_id_str = NULL;
2734 struct blame_cb_args bca;
2735 int ch, obj_type, i;
2736 size_t filesize;
2738 memset(&bca, 0, sizeof(bca));
2740 #ifndef PROFILE
2741 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2742 NULL) == -1)
2743 err(1, "pledge");
2744 #endif
2746 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2747 switch (ch) {
2748 case 'c':
2749 commit_id_str = optarg;
2750 break;
2751 case 'r':
2752 repo_path = realpath(optarg, NULL);
2753 if (repo_path == NULL)
2754 return got_error_from_errno2("realpath",
2755 optarg);
2756 got_path_strip_trailing_slashes(repo_path);
2757 break;
2758 default:
2759 usage_blame();
2760 /* NOTREACHED */
2764 argc -= optind;
2765 argv += optind;
2767 if (argc == 1)
2768 path = argv[0];
2769 else
2770 usage_blame();
2772 cwd = getcwd(NULL, 0);
2773 if (cwd == NULL) {
2774 error = got_error_from_errno("getcwd");
2775 goto done;
2777 if (repo_path == NULL) {
2778 error = got_worktree_open(&worktree, cwd);
2779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2780 goto done;
2781 else
2782 error = NULL;
2783 if (worktree) {
2784 repo_path =
2785 strdup(got_worktree_get_repo_path(worktree));
2786 if (repo_path == NULL)
2787 error = got_error_from_errno("strdup");
2788 if (error)
2789 goto done;
2790 } else {
2791 repo_path = strdup(cwd);
2792 if (repo_path == NULL) {
2793 error = got_error_from_errno("strdup");
2794 goto done;
2799 error = got_repo_open(&repo, repo_path, NULL);
2800 if (error != NULL)
2801 goto done;
2803 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2804 if (error)
2805 goto done;
2807 if (worktree) {
2808 const char *prefix = got_worktree_get_path_prefix(worktree);
2809 char *p, *worktree_subdir = cwd +
2810 strlen(got_worktree_get_root_path(worktree));
2811 if (asprintf(&p, "%s%s%s%s%s",
2812 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2813 worktree_subdir, worktree_subdir[0] ? "/" : "",
2814 path) == -1) {
2815 error = got_error_from_errno("asprintf");
2816 goto done;
2818 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2819 free(p);
2820 } else {
2821 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2823 if (error)
2824 goto done;
2826 if (commit_id_str == NULL) {
2827 struct got_reference *head_ref;
2828 error = got_ref_open(&head_ref, repo, worktree ?
2829 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2830 if (error != NULL)
2831 goto done;
2832 error = got_ref_resolve(&commit_id, repo, head_ref);
2833 got_ref_close(head_ref);
2834 if (error != NULL)
2835 goto done;
2836 } else {
2837 error = got_repo_match_object_id(&commit_id, NULL,
2838 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2839 if (error)
2840 goto done;
2843 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2844 if (error)
2845 goto done;
2847 error = got_object_get_type(&obj_type, repo, obj_id);
2848 if (error)
2849 goto done;
2851 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2852 error = got_error(GOT_ERR_OBJ_TYPE);
2853 goto done;
2856 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2857 if (error)
2858 goto done;
2859 bca.f = got_opentemp();
2860 if (bca.f == NULL) {
2861 error = got_error_from_errno("got_opentemp");
2862 goto done;
2864 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2865 &bca.line_offsets, bca.f, blob);
2866 if (error || bca.nlines == 0)
2867 goto done;
2869 /* Don't include \n at EOF in the blame line count. */
2870 if (bca.line_offsets[bca.nlines - 1] == filesize)
2871 bca.nlines--;
2873 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2874 if (bca.lines == NULL) {
2875 error = got_error_from_errno("calloc");
2876 goto done;
2878 bca.lineno_cur = 1;
2879 bca.nlines_prec = 0;
2880 i = bca.nlines;
2881 while (i > 0) {
2882 i /= 10;
2883 bca.nlines_prec++;
2885 bca.repo = repo;
2887 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2888 check_cancelled, NULL);
2889 done:
2890 free(in_repo_path);
2891 free(repo_path);
2892 free(cwd);
2893 free(commit_id);
2894 free(obj_id);
2895 if (blob)
2896 got_object_blob_close(blob);
2897 if (worktree)
2898 got_worktree_close(worktree);
2899 if (repo) {
2900 const struct got_error *repo_error;
2901 repo_error = got_repo_close(repo);
2902 if (error == NULL)
2903 error = repo_error;
2905 if (bca.lines) {
2906 for (i = 0; i < bca.nlines; i++) {
2907 struct blame_line *bline = &bca.lines[i];
2908 free(bline->id_str);
2909 free(bline->committer);
2911 free(bca.lines);
2913 free(bca.line_offsets);
2914 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2915 error = got_error_from_errno("fclose");
2916 return error;
2919 __dead static void
2920 usage_tree(void)
2922 fprintf(stderr,
2923 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2924 getprogname());
2925 exit(1);
2928 static void
2929 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2930 const char *root_path)
2932 int is_root_path = (strcmp(path, root_path) == 0);
2933 const char *modestr = "";
2934 mode_t mode = got_tree_entry_get_mode(te);
2936 path += strlen(root_path);
2937 while (path[0] == '/')
2938 path++;
2940 if (got_object_tree_entry_is_submodule(te))
2941 modestr = "$";
2942 else if (S_ISLNK(mode))
2943 modestr = "@";
2944 else if (S_ISDIR(mode))
2945 modestr = "/";
2946 else if (mode & S_IXUSR)
2947 modestr = "*";
2949 printf("%s%s%s%s%s\n", id ? id : "", path,
2950 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2953 static const struct got_error *
2954 print_tree(const char *path, struct got_object_id *commit_id,
2955 int show_ids, int recurse, const char *root_path,
2956 struct got_repository *repo)
2958 const struct got_error *err = NULL;
2959 struct got_object_id *tree_id = NULL;
2960 struct got_tree_object *tree = NULL;
2961 int nentries, i;
2963 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2964 if (err)
2965 goto done;
2967 err = got_object_open_as_tree(&tree, repo, tree_id);
2968 if (err)
2969 goto done;
2970 nentries = got_object_tree_get_nentries(tree);
2971 for (i = 0; i < nentries; i++) {
2972 struct got_tree_entry *te;
2973 char *id = NULL;
2975 if (sigint_received || sigpipe_received)
2976 break;
2978 te = got_object_tree_get_entry(tree, i);
2979 if (show_ids) {
2980 char *id_str;
2981 err = got_object_id_str(&id_str,
2982 got_tree_entry_get_id(te));
2983 if (err)
2984 goto done;
2985 if (asprintf(&id, "%s ", id_str) == -1) {
2986 err = got_error_from_errno("asprintf");
2987 free(id_str);
2988 goto done;
2990 free(id_str);
2992 print_entry(te, id, path, root_path);
2993 free(id);
2995 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2996 char *child_path;
2997 if (asprintf(&child_path, "%s%s%s", path,
2998 path[0] == '/' && path[1] == '\0' ? "" : "/",
2999 got_tree_entry_get_name(te)) == -1) {
3000 err = got_error_from_errno("asprintf");
3001 goto done;
3003 err = print_tree(child_path, commit_id, show_ids, 1,
3004 root_path, repo);
3005 free(child_path);
3006 if (err)
3007 goto done;
3010 done:
3011 if (tree)
3012 got_object_tree_close(tree);
3013 free(tree_id);
3014 return err;
3017 static const struct got_error *
3018 cmd_tree(int argc, char *argv[])
3020 const struct got_error *error;
3021 struct got_repository *repo = NULL;
3022 struct got_worktree *worktree = NULL;
3023 const char *path;
3024 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3025 struct got_object_id *commit_id = NULL;
3026 char *commit_id_str = NULL;
3027 int show_ids = 0, recurse = 0;
3028 int ch;
3030 #ifndef PROFILE
3031 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3032 NULL) == -1)
3033 err(1, "pledge");
3034 #endif
3036 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3037 switch (ch) {
3038 case 'c':
3039 commit_id_str = optarg;
3040 break;
3041 case 'r':
3042 repo_path = realpath(optarg, NULL);
3043 if (repo_path == NULL)
3044 return got_error_from_errno2("realpath",
3045 optarg);
3046 got_path_strip_trailing_slashes(repo_path);
3047 break;
3048 case 'i':
3049 show_ids = 1;
3050 break;
3051 case 'R':
3052 recurse = 1;
3053 break;
3054 default:
3055 usage_tree();
3056 /* NOTREACHED */
3060 argc -= optind;
3061 argv += optind;
3063 if (argc == 1)
3064 path = argv[0];
3065 else if (argc > 1)
3066 usage_tree();
3067 else
3068 path = NULL;
3070 cwd = getcwd(NULL, 0);
3071 if (cwd == NULL) {
3072 error = got_error_from_errno("getcwd");
3073 goto done;
3075 if (repo_path == NULL) {
3076 error = got_worktree_open(&worktree, cwd);
3077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3078 goto done;
3079 else
3080 error = NULL;
3081 if (worktree) {
3082 repo_path =
3083 strdup(got_worktree_get_repo_path(worktree));
3084 if (repo_path == NULL)
3085 error = got_error_from_errno("strdup");
3086 if (error)
3087 goto done;
3088 } else {
3089 repo_path = strdup(cwd);
3090 if (repo_path == NULL) {
3091 error = got_error_from_errno("strdup");
3092 goto done;
3097 error = got_repo_open(&repo, repo_path, NULL);
3098 if (error != NULL)
3099 goto done;
3101 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3102 if (error)
3103 goto done;
3105 if (path == NULL) {
3106 if (worktree) {
3107 char *p, *worktree_subdir = cwd +
3108 strlen(got_worktree_get_root_path(worktree));
3109 if (asprintf(&p, "%s/%s",
3110 got_worktree_get_path_prefix(worktree),
3111 worktree_subdir) == -1) {
3112 error = got_error_from_errno("asprintf");
3113 goto done;
3115 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3116 free(p);
3117 if (error)
3118 goto done;
3119 } else
3120 path = "/";
3122 if (in_repo_path == NULL) {
3123 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3124 if (error != NULL)
3125 goto done;
3128 if (commit_id_str == NULL) {
3129 struct got_reference *head_ref;
3130 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3131 if (error != NULL)
3132 goto done;
3133 error = got_ref_resolve(&commit_id, repo, head_ref);
3134 got_ref_close(head_ref);
3135 if (error != NULL)
3136 goto done;
3137 } else {
3138 error = got_repo_match_object_id(&commit_id, NULL,
3139 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3140 if (error)
3141 goto done;
3144 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3145 in_repo_path, repo);
3146 done:
3147 free(in_repo_path);
3148 free(repo_path);
3149 free(cwd);
3150 free(commit_id);
3151 if (worktree)
3152 got_worktree_close(worktree);
3153 if (repo) {
3154 const struct got_error *repo_error;
3155 repo_error = got_repo_close(repo);
3156 if (error == NULL)
3157 error = repo_error;
3159 return error;
3162 __dead static void
3163 usage_status(void)
3165 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3166 exit(1);
3169 static const struct got_error *
3170 print_status(void *arg, unsigned char status, unsigned char staged_status,
3171 const char *path, struct got_object_id *blob_id,
3172 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3173 int dirfd, const char *de_name)
3175 if (status == staged_status && (status == GOT_STATUS_DELETE))
3176 status = GOT_STATUS_NO_CHANGE;
3177 printf("%c%c %s\n", status, staged_status, path);
3178 return NULL;
3181 static const struct got_error *
3182 cmd_status(int argc, char *argv[])
3184 const struct got_error *error = NULL;
3185 struct got_repository *repo = NULL;
3186 struct got_worktree *worktree = NULL;
3187 char *cwd = NULL;
3188 struct got_pathlist_head paths;
3189 struct got_pathlist_entry *pe;
3190 int ch;
3192 TAILQ_INIT(&paths);
3194 while ((ch = getopt(argc, argv, "")) != -1) {
3195 switch (ch) {
3196 default:
3197 usage_status();
3198 /* NOTREACHED */
3202 argc -= optind;
3203 argv += optind;
3205 #ifndef PROFILE
3206 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3207 NULL) == -1)
3208 err(1, "pledge");
3209 #endif
3210 cwd = getcwd(NULL, 0);
3211 if (cwd == NULL) {
3212 error = got_error_from_errno("getcwd");
3213 goto done;
3216 error = got_worktree_open(&worktree, cwd);
3217 if (error != NULL)
3218 goto done;
3220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3221 NULL);
3222 if (error != NULL)
3223 goto done;
3225 error = apply_unveil(got_repo_get_path(repo), 1,
3226 got_worktree_get_root_path(worktree));
3227 if (error)
3228 goto done;
3230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3231 if (error)
3232 goto done;
3234 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3235 check_cancelled, NULL);
3236 done:
3237 TAILQ_FOREACH(pe, &paths, entry)
3238 free((char *)pe->path);
3239 got_pathlist_free(&paths);
3240 free(cwd);
3241 return error;
3244 __dead static void
3245 usage_ref(void)
3247 fprintf(stderr,
3248 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3249 getprogname());
3250 exit(1);
3253 static const struct got_error *
3254 list_refs(struct got_repository *repo)
3256 static const struct got_error *err = NULL;
3257 struct got_reflist_head refs;
3258 struct got_reflist_entry *re;
3260 SIMPLEQ_INIT(&refs);
3261 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3262 if (err)
3263 return err;
3265 SIMPLEQ_FOREACH(re, &refs, entry) {
3266 char *refstr;
3267 refstr = got_ref_to_str(re->ref);
3268 if (refstr == NULL)
3269 return got_error_from_errno("got_ref_to_str");
3270 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3271 free(refstr);
3274 got_ref_list_free(&refs);
3275 return NULL;
3278 static const struct got_error *
3279 delete_ref(struct got_repository *repo, const char *refname)
3281 const struct got_error *err = NULL;
3282 struct got_reference *ref;
3284 err = got_ref_open(&ref, repo, refname, 0);
3285 if (err)
3286 return err;
3288 err = got_ref_delete(ref, repo);
3289 got_ref_close(ref);
3290 return err;
3293 static const struct got_error *
3294 add_ref(struct got_repository *repo, const char *refname, const char *target)
3296 const struct got_error *err = NULL;
3297 struct got_object_id *id;
3298 struct got_reference *ref = NULL;
3301 * Don't let the user create a reference name with a leading '-'.
3302 * While technically a valid reference name, this case is usually
3303 * an unintended typo.
3305 if (refname[0] == '-')
3306 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3308 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3309 repo);
3310 if (err) {
3311 struct got_reference *target_ref;
3313 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3314 return err;
3315 err = got_ref_open(&target_ref, repo, target, 0);
3316 if (err)
3317 return err;
3318 err = got_ref_resolve(&id, repo, target_ref);
3319 got_ref_close(target_ref);
3320 if (err)
3321 return err;
3324 err = got_ref_alloc(&ref, refname, id);
3325 if (err)
3326 goto done;
3328 err = got_ref_write(ref, repo);
3329 done:
3330 if (ref)
3331 got_ref_close(ref);
3332 free(id);
3333 return err;
3336 static const struct got_error *
3337 add_symref(struct got_repository *repo, const char *refname, const char *target)
3339 const struct got_error *err = NULL;
3340 struct got_reference *ref = NULL;
3341 struct got_reference *target_ref = NULL;
3344 * Don't let the user create a reference name with a leading '-'.
3345 * While technically a valid reference name, this case is usually
3346 * an unintended typo.
3348 if (refname[0] == '-')
3349 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3351 err = got_ref_open(&target_ref, repo, target, 0);
3352 if (err)
3353 return err;
3355 err = got_ref_alloc_symref(&ref, refname, target_ref);
3356 if (err)
3357 goto done;
3359 err = got_ref_write(ref, repo);
3360 done:
3361 if (target_ref)
3362 got_ref_close(target_ref);
3363 if (ref)
3364 got_ref_close(ref);
3365 return err;
3368 static const struct got_error *
3369 cmd_ref(int argc, char *argv[])
3371 const struct got_error *error = NULL;
3372 struct got_repository *repo = NULL;
3373 struct got_worktree *worktree = NULL;
3374 char *cwd = NULL, *repo_path = NULL;
3375 int ch, do_list = 0, create_symref = 0;
3376 const char *delref = NULL;
3378 /* TODO: Add -s option for adding symbolic references. */
3379 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3380 switch (ch) {
3381 case 'd':
3382 delref = optarg;
3383 break;
3384 case 'r':
3385 repo_path = realpath(optarg, NULL);
3386 if (repo_path == NULL)
3387 return got_error_from_errno2("realpath",
3388 optarg);
3389 got_path_strip_trailing_slashes(repo_path);
3390 break;
3391 case 'l':
3392 do_list = 1;
3393 break;
3394 case 's':
3395 create_symref = 1;
3396 break;
3397 default:
3398 usage_ref();
3399 /* NOTREACHED */
3403 if (do_list && delref)
3404 errx(1, "-l and -d options are mutually exclusive\n");
3406 argc -= optind;
3407 argv += optind;
3409 if (do_list || delref) {
3410 if (create_symref)
3411 errx(1, "-s option cannot be used together with the "
3412 "-l or -d options");
3413 if (argc > 0)
3414 usage_ref();
3415 } else if (argc != 2)
3416 usage_ref();
3418 #ifndef PROFILE
3419 if (do_list) {
3420 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3421 NULL) == -1)
3422 err(1, "pledge");
3423 } else {
3424 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3425 "sendfd unveil", NULL) == -1)
3426 err(1, "pledge");
3428 #endif
3429 cwd = getcwd(NULL, 0);
3430 if (cwd == NULL) {
3431 error = got_error_from_errno("getcwd");
3432 goto done;
3435 if (repo_path == NULL) {
3436 error = got_worktree_open(&worktree, cwd);
3437 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3438 goto done;
3439 else
3440 error = NULL;
3441 if (worktree) {
3442 repo_path =
3443 strdup(got_worktree_get_repo_path(worktree));
3444 if (repo_path == NULL)
3445 error = got_error_from_errno("strdup");
3446 if (error)
3447 goto done;
3448 } else {
3449 repo_path = strdup(cwd);
3450 if (repo_path == NULL) {
3451 error = got_error_from_errno("strdup");
3452 goto done;
3457 error = got_repo_open(&repo, repo_path, NULL);
3458 if (error != NULL)
3459 goto done;
3461 error = apply_unveil(got_repo_get_path(repo), do_list,
3462 worktree ? got_worktree_get_root_path(worktree) : NULL);
3463 if (error)
3464 goto done;
3466 if (do_list)
3467 error = list_refs(repo);
3468 else if (delref)
3469 error = delete_ref(repo, delref);
3470 else if (create_symref)
3471 error = add_symref(repo, argv[0], argv[1]);
3472 else
3473 error = add_ref(repo, argv[0], argv[1]);
3474 done:
3475 if (repo)
3476 got_repo_close(repo);
3477 if (worktree)
3478 got_worktree_close(worktree);
3479 free(cwd);
3480 free(repo_path);
3481 return error;
3484 __dead static void
3485 usage_branch(void)
3487 fprintf(stderr,
3488 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3489 "[name]\n", getprogname());
3490 exit(1);
3493 static const struct got_error *
3494 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 struct got_reference *ref)
3497 const struct got_error *err = NULL;
3498 const char *refname, *marker = " ";
3499 char *refstr;
3501 refname = got_ref_get_name(ref);
3502 if (worktree && strcmp(refname,
3503 got_worktree_get_head_ref_name(worktree)) == 0) {
3504 struct got_object_id *id = NULL;
3506 err = got_ref_resolve(&id, repo, ref);
3507 if (err)
3508 return err;
3509 if (got_object_id_cmp(id,
3510 got_worktree_get_base_commit_id(worktree)) == 0)
3511 marker = "* ";
3512 else
3513 marker = "~ ";
3514 free(id);
3517 if (strncmp(refname, "refs/heads/", 11) == 0)
3518 refname += 11;
3519 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3520 refname += 18;
3522 refstr = got_ref_to_str(ref);
3523 if (refstr == NULL)
3524 return got_error_from_errno("got_ref_to_str");
3526 printf("%s%s: %s\n", marker, refname, refstr);
3527 free(refstr);
3528 return NULL;
3531 static const struct got_error *
3532 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3534 const char *refname;
3536 if (worktree == NULL)
3537 return got_error(GOT_ERR_NOT_WORKTREE);
3539 refname = got_worktree_get_head_ref_name(worktree);
3541 if (strncmp(refname, "refs/heads/", 11) == 0)
3542 refname += 11;
3543 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3544 refname += 18;
3546 printf("%s\n", refname);
3548 return NULL;
3551 static const struct got_error *
3552 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3554 static const struct got_error *err = NULL;
3555 struct got_reflist_head refs;
3556 struct got_reflist_entry *re;
3557 struct got_reference *temp_ref = NULL;
3558 int rebase_in_progress, histedit_in_progress;
3560 SIMPLEQ_INIT(&refs);
3562 if (worktree) {
3563 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3564 worktree);
3565 if (err)
3566 return err;
3568 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3569 worktree);
3570 if (err)
3571 return err;
3573 if (rebase_in_progress || histedit_in_progress) {
3574 err = got_ref_open(&temp_ref, repo,
3575 got_worktree_get_head_ref_name(worktree), 0);
3576 if (err)
3577 return err;
3578 list_branch(repo, worktree, temp_ref);
3579 got_ref_close(temp_ref);
3583 err = got_ref_list(&refs, repo, "refs/heads",
3584 got_ref_cmp_by_name, NULL);
3585 if (err)
3586 return err;
3588 SIMPLEQ_FOREACH(re, &refs, entry)
3589 list_branch(repo, worktree, re->ref);
3591 got_ref_list_free(&refs);
3592 return NULL;
3595 static const struct got_error *
3596 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3597 const char *branch_name)
3599 const struct got_error *err = NULL;
3600 struct got_reference *ref = NULL;
3601 char *refname;
3603 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3604 return got_error_from_errno("asprintf");
3606 err = got_ref_open(&ref, repo, refname, 0);
3607 if (err)
3608 goto done;
3610 if (worktree &&
3611 strcmp(got_worktree_get_head_ref_name(worktree),
3612 got_ref_get_name(ref)) == 0) {
3613 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3614 "will not delete this work tree's current branch");
3615 goto done;
3618 err = got_ref_delete(ref, repo);
3619 done:
3620 if (ref)
3621 got_ref_close(ref);
3622 free(refname);
3623 return err;
3626 static const struct got_error *
3627 add_branch(struct got_repository *repo, const char *branch_name,
3628 struct got_object_id *base_commit_id)
3630 const struct got_error *err = NULL;
3631 struct got_reference *ref = NULL;
3632 char *base_refname = NULL, *refname = NULL;
3635 * Don't let the user create a branch name with a leading '-'.
3636 * While technically a valid reference name, this case is usually
3637 * an unintended typo.
3639 if (branch_name[0] == '-')
3640 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3642 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3643 err = got_error_from_errno("asprintf");
3644 goto done;
3647 err = got_ref_open(&ref, repo, refname, 0);
3648 if (err == NULL) {
3649 err = got_error(GOT_ERR_BRANCH_EXISTS);
3650 goto done;
3651 } else if (err->code != GOT_ERR_NOT_REF)
3652 goto done;
3654 err = got_ref_alloc(&ref, refname, base_commit_id);
3655 if (err)
3656 goto done;
3658 err = got_ref_write(ref, repo);
3659 done:
3660 if (ref)
3661 got_ref_close(ref);
3662 free(base_refname);
3663 free(refname);
3664 return err;
3667 static const struct got_error *
3668 cmd_branch(int argc, char *argv[])
3670 const struct got_error *error = NULL;
3671 struct got_repository *repo = NULL;
3672 struct got_worktree *worktree = NULL;
3673 char *cwd = NULL, *repo_path = NULL;
3674 int ch, do_list = 0, do_show = 0, do_update = 1;
3675 const char *delref = NULL, *commit_id_arg = NULL;
3676 struct got_reference *ref = NULL;
3677 struct got_pathlist_head paths;
3678 struct got_pathlist_entry *pe;
3679 struct got_object_id *commit_id = NULL;
3680 char *commit_id_str = NULL;
3682 TAILQ_INIT(&paths);
3684 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3685 switch (ch) {
3686 case 'c':
3687 commit_id_arg = optarg;
3688 break;
3689 case 'd':
3690 delref = optarg;
3691 break;
3692 case 'r':
3693 repo_path = realpath(optarg, NULL);
3694 if (repo_path == NULL)
3695 return got_error_from_errno2("realpath",
3696 optarg);
3697 got_path_strip_trailing_slashes(repo_path);
3698 break;
3699 case 'l':
3700 do_list = 1;
3701 break;
3702 case 'n':
3703 do_update = 0;
3704 break;
3705 default:
3706 usage_branch();
3707 /* NOTREACHED */
3711 if (do_list && delref)
3712 errx(1, "-l and -d options are mutually exclusive\n");
3714 argc -= optind;
3715 argv += optind;
3717 if (!do_list && !delref && argc == 0)
3718 do_show = 1;
3720 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3721 errx(1, "-c option can only be used when creating a branch");
3723 if (do_list || delref) {
3724 if (argc > 0)
3725 usage_branch();
3726 } else if (!do_show && argc != 1)
3727 usage_branch();
3729 #ifndef PROFILE
3730 if (do_list || do_show) {
3731 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3732 NULL) == -1)
3733 err(1, "pledge");
3734 } else {
3735 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3736 "sendfd unveil", NULL) == -1)
3737 err(1, "pledge");
3739 #endif
3740 cwd = getcwd(NULL, 0);
3741 if (cwd == NULL) {
3742 error = got_error_from_errno("getcwd");
3743 goto done;
3746 if (repo_path == NULL) {
3747 error = got_worktree_open(&worktree, cwd);
3748 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3749 goto done;
3750 else
3751 error = NULL;
3752 if (worktree) {
3753 repo_path =
3754 strdup(got_worktree_get_repo_path(worktree));
3755 if (repo_path == NULL)
3756 error = got_error_from_errno("strdup");
3757 if (error)
3758 goto done;
3759 } else {
3760 repo_path = strdup(cwd);
3761 if (repo_path == NULL) {
3762 error = got_error_from_errno("strdup");
3763 goto done;
3768 error = got_repo_open(&repo, repo_path, NULL);
3769 if (error != NULL)
3770 goto done;
3772 error = apply_unveil(got_repo_get_path(repo), do_list,
3773 worktree ? got_worktree_get_root_path(worktree) : NULL);
3774 if (error)
3775 goto done;
3777 if (do_show)
3778 error = show_current_branch(repo, worktree);
3779 else if (do_list)
3780 error = list_branches(repo, worktree);
3781 else if (delref)
3782 error = delete_branch(repo, worktree, delref);
3783 else {
3784 if (commit_id_arg == NULL)
3785 commit_id_arg = worktree ?
3786 got_worktree_get_head_ref_name(worktree) :
3787 GOT_REF_HEAD;
3788 error = got_repo_match_object_id(&commit_id, NULL,
3789 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3790 if (error)
3791 goto done;
3792 error = add_branch(repo, argv[0], commit_id);
3793 if (error)
3794 goto done;
3795 if (worktree && do_update) {
3796 int did_something = 0;
3797 char *branch_refname = NULL;
3799 error = got_object_id_str(&commit_id_str, commit_id);
3800 if (error)
3801 goto done;
3802 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3803 worktree);
3804 if (error)
3805 goto done;
3806 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3807 == -1) {
3808 error = got_error_from_errno("asprintf");
3809 goto done;
3811 error = got_ref_open(&ref, repo, branch_refname, 0);
3812 free(branch_refname);
3813 if (error)
3814 goto done;
3815 error = switch_head_ref(ref, commit_id, worktree,
3816 repo);
3817 if (error)
3818 goto done;
3819 error = got_worktree_set_base_commit_id(worktree, repo,
3820 commit_id);
3821 if (error)
3822 goto done;
3823 error = got_worktree_checkout_files(worktree, &paths,
3824 repo, update_progress, &did_something,
3825 check_cancelled, NULL);
3826 if (error)
3827 goto done;
3828 if (did_something)
3829 printf("Updated to commit %s\n", commit_id_str);
3832 done:
3833 if (ref)
3834 got_ref_close(ref);
3835 if (repo)
3836 got_repo_close(repo);
3837 if (worktree)
3838 got_worktree_close(worktree);
3839 free(cwd);
3840 free(repo_path);
3841 free(commit_id);
3842 free(commit_id_str);
3843 TAILQ_FOREACH(pe, &paths, entry)
3844 free((char *)pe->path);
3845 got_pathlist_free(&paths);
3846 return error;
3850 __dead static void
3851 usage_tag(void)
3853 fprintf(stderr,
3854 "usage: %s tag [-c commit] [-r repository] [-l] "
3855 "[-m message] name\n", getprogname());
3856 exit(1);
3859 #if 0
3860 static const struct got_error *
3861 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3863 const struct got_error *err = NULL;
3864 struct got_reflist_entry *re, *se, *new;
3865 struct got_object_id *re_id, *se_id;
3866 struct got_tag_object *re_tag, *se_tag;
3867 time_t re_time, se_time;
3869 SIMPLEQ_FOREACH(re, tags, entry) {
3870 se = SIMPLEQ_FIRST(sorted);
3871 if (se == NULL) {
3872 err = got_reflist_entry_dup(&new, re);
3873 if (err)
3874 return err;
3875 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3876 continue;
3877 } else {
3878 err = got_ref_resolve(&re_id, repo, re->ref);
3879 if (err)
3880 break;
3881 err = got_object_open_as_tag(&re_tag, repo, re_id);
3882 free(re_id);
3883 if (err)
3884 break;
3885 re_time = got_object_tag_get_tagger_time(re_tag);
3886 got_object_tag_close(re_tag);
3889 while (se) {
3890 err = got_ref_resolve(&se_id, repo, re->ref);
3891 if (err)
3892 break;
3893 err = got_object_open_as_tag(&se_tag, repo, se_id);
3894 free(se_id);
3895 if (err)
3896 break;
3897 se_time = got_object_tag_get_tagger_time(se_tag);
3898 got_object_tag_close(se_tag);
3900 if (se_time > re_time) {
3901 err = got_reflist_entry_dup(&new, re);
3902 if (err)
3903 return err;
3904 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3905 break;
3907 se = SIMPLEQ_NEXT(se, entry);
3908 continue;
3911 done:
3912 return err;
3914 #endif
3916 static const struct got_error *
3917 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3919 static const struct got_error *err = NULL;
3920 struct got_reflist_head refs;
3921 struct got_reflist_entry *re;
3923 SIMPLEQ_INIT(&refs);
3925 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3926 if (err)
3927 return err;
3929 SIMPLEQ_FOREACH(re, &refs, entry) {
3930 const char *refname;
3931 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3932 char datebuf[26];
3933 const char *tagger;
3934 time_t tagger_time;
3935 struct got_object_id *id;
3936 struct got_tag_object *tag;
3937 struct got_commit_object *commit = NULL;
3939 refname = got_ref_get_name(re->ref);
3940 if (strncmp(refname, "refs/tags/", 10) != 0)
3941 continue;
3942 refname += 10;
3943 refstr = got_ref_to_str(re->ref);
3944 if (refstr == NULL) {
3945 err = got_error_from_errno("got_ref_to_str");
3946 break;
3948 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3949 free(refstr);
3951 err = got_ref_resolve(&id, repo, re->ref);
3952 if (err)
3953 break;
3954 err = got_object_open_as_tag(&tag, repo, id);
3955 if (err) {
3956 if (err->code != GOT_ERR_OBJ_TYPE) {
3957 free(id);
3958 break;
3960 /* "lightweight" tag */
3961 err = got_object_open_as_commit(&commit, repo, id);
3962 if (err) {
3963 free(id);
3964 break;
3966 tagger = got_object_commit_get_committer(commit);
3967 tagger_time =
3968 got_object_commit_get_committer_time(commit);
3969 err = got_object_id_str(&id_str, id);
3970 free(id);
3971 if (err)
3972 break;
3973 } else {
3974 free(id);
3975 tagger = got_object_tag_get_tagger(tag);
3976 tagger_time = got_object_tag_get_tagger_time(tag);
3977 err = got_object_id_str(&id_str,
3978 got_object_tag_get_object_id(tag));
3979 if (err)
3980 break;
3982 printf("from: %s\n", tagger);
3983 datestr = get_datestr(&tagger_time, datebuf);
3984 if (datestr)
3985 printf("date: %s UTC\n", datestr);
3986 if (commit)
3987 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3988 else {
3989 switch (got_object_tag_get_object_type(tag)) {
3990 case GOT_OBJ_TYPE_BLOB:
3991 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3992 id_str);
3993 break;
3994 case GOT_OBJ_TYPE_TREE:
3995 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3996 id_str);
3997 break;
3998 case GOT_OBJ_TYPE_COMMIT:
3999 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4000 id_str);
4001 break;
4002 case GOT_OBJ_TYPE_TAG:
4003 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4004 id_str);
4005 break;
4006 default:
4007 break;
4010 free(id_str);
4011 if (commit) {
4012 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4013 if (err)
4014 break;
4015 got_object_commit_close(commit);
4016 } else {
4017 tagmsg0 = strdup(got_object_tag_get_message(tag));
4018 got_object_tag_close(tag);
4019 if (tagmsg0 == NULL) {
4020 err = got_error_from_errno("strdup");
4021 break;
4025 tagmsg = tagmsg0;
4026 do {
4027 line = strsep(&tagmsg, "\n");
4028 if (line)
4029 printf(" %s\n", line);
4030 } while (line);
4031 free(tagmsg0);
4034 got_ref_list_free(&refs);
4035 return NULL;
4038 static const struct got_error *
4039 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4040 const char *tag_name, const char *repo_path)
4042 const struct got_error *err = NULL;
4043 char *template = NULL, *initial_content = NULL;
4044 char *editor = NULL;
4045 int fd = -1;
4047 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4048 err = got_error_from_errno("asprintf");
4049 goto done;
4052 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4053 commit_id_str, tag_name) == -1) {
4054 err = got_error_from_errno("asprintf");
4055 goto done;
4058 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4059 if (err)
4060 goto done;
4062 dprintf(fd, initial_content);
4063 close(fd);
4065 err = get_editor(&editor);
4066 if (err)
4067 goto done;
4068 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4069 done:
4070 free(initial_content);
4071 free(template);
4072 free(editor);
4074 /* Editor is done; we can now apply unveil(2) */
4075 if (err == NULL) {
4076 err = apply_unveil(repo_path, 0, NULL);
4077 if (err) {
4078 free(*tagmsg);
4079 *tagmsg = NULL;
4082 return err;
4085 static const struct got_error *
4086 add_tag(struct got_repository *repo, const char *tag_name,
4087 const char *commit_arg, const char *tagmsg_arg)
4089 const struct got_error *err = NULL;
4090 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4091 char *label = NULL, *commit_id_str = NULL;
4092 struct got_reference *ref = NULL;
4093 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4094 char *tagmsg_path = NULL, *tag_id_str = NULL;
4095 int preserve_tagmsg = 0;
4098 * Don't let the user create a tag name with a leading '-'.
4099 * While technically a valid reference name, this case is usually
4100 * an unintended typo.
4102 if (tag_name[0] == '-')
4103 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4105 err = get_author(&tagger, repo);
4106 if (err)
4107 return err;
4109 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4110 GOT_OBJ_TYPE_COMMIT, 1, repo);
4111 if (err)
4112 goto done;
4114 err = got_object_id_str(&commit_id_str, commit_id);
4115 if (err)
4116 goto done;
4118 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4119 refname = strdup(tag_name);
4120 if (refname == NULL) {
4121 err = got_error_from_errno("strdup");
4122 goto done;
4124 tag_name += 10;
4125 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4126 err = got_error_from_errno("asprintf");
4127 goto done;
4130 err = got_ref_open(&ref, repo, refname, 0);
4131 if (err == NULL) {
4132 err = got_error(GOT_ERR_TAG_EXISTS);
4133 goto done;
4134 } else if (err->code != GOT_ERR_NOT_REF)
4135 goto done;
4137 if (tagmsg_arg == NULL) {
4138 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4139 tag_name, got_repo_get_path(repo));
4140 if (err) {
4141 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4142 tagmsg_path != NULL)
4143 preserve_tagmsg = 1;
4144 goto done;
4148 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4149 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4150 if (err) {
4151 if (tagmsg_path)
4152 preserve_tagmsg = 1;
4153 goto done;
4156 err = got_ref_alloc(&ref, refname, tag_id);
4157 if (err) {
4158 if (tagmsg_path)
4159 preserve_tagmsg = 1;
4160 goto done;
4163 err = got_ref_write(ref, repo);
4164 if (err) {
4165 if (tagmsg_path)
4166 preserve_tagmsg = 1;
4167 goto done;
4170 err = got_object_id_str(&tag_id_str, tag_id);
4171 if (err) {
4172 if (tagmsg_path)
4173 preserve_tagmsg = 1;
4174 goto done;
4176 printf("Created tag %s\n", tag_id_str);
4177 done:
4178 if (preserve_tagmsg) {
4179 fprintf(stderr, "%s: tag message preserved in %s\n",
4180 getprogname(), tagmsg_path);
4181 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4182 err = got_error_from_errno2("unlink", tagmsg_path);
4183 free(tag_id_str);
4184 if (ref)
4185 got_ref_close(ref);
4186 free(commit_id);
4187 free(commit_id_str);
4188 free(refname);
4189 free(tagmsg);
4190 free(tagmsg_path);
4191 free(tagger);
4192 return err;
4195 static const struct got_error *
4196 cmd_tag(int argc, char *argv[])
4198 const struct got_error *error = NULL;
4199 struct got_repository *repo = NULL;
4200 struct got_worktree *worktree = NULL;
4201 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4202 char *gitconfig_path = NULL;
4203 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4204 int ch, do_list = 0;
4206 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4207 switch (ch) {
4208 case 'c':
4209 commit_id_arg = optarg;
4210 break;
4211 case 'm':
4212 tagmsg = optarg;
4213 break;
4214 case 'r':
4215 repo_path = realpath(optarg, NULL);
4216 if (repo_path == NULL)
4217 return got_error_from_errno2("realpath",
4218 optarg);
4219 got_path_strip_trailing_slashes(repo_path);
4220 break;
4221 case 'l':
4222 do_list = 1;
4223 break;
4224 default:
4225 usage_tag();
4226 /* NOTREACHED */
4230 argc -= optind;
4231 argv += optind;
4233 if (do_list) {
4234 if (commit_id_arg != NULL)
4235 errx(1, "-c option can only be used when creating a tag");
4236 if (tagmsg)
4237 errx(1, "-l and -m options are mutually exclusive");
4238 if (argc > 0)
4239 usage_tag();
4240 } else if (argc != 1)
4241 usage_tag();
4243 tag_name = argv[0];
4245 #ifndef PROFILE
4246 if (do_list) {
4247 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4248 NULL) == -1)
4249 err(1, "pledge");
4250 } else {
4251 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4252 "sendfd unveil", NULL) == -1)
4253 err(1, "pledge");
4255 #endif
4256 cwd = getcwd(NULL, 0);
4257 if (cwd == NULL) {
4258 error = got_error_from_errno("getcwd");
4259 goto done;
4262 if (repo_path == NULL) {
4263 error = got_worktree_open(&worktree, cwd);
4264 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4265 goto done;
4266 else
4267 error = NULL;
4268 if (worktree) {
4269 repo_path =
4270 strdup(got_worktree_get_repo_path(worktree));
4271 if (repo_path == NULL)
4272 error = got_error_from_errno("strdup");
4273 if (error)
4274 goto done;
4275 } else {
4276 repo_path = strdup(cwd);
4277 if (repo_path == NULL) {
4278 error = got_error_from_errno("strdup");
4279 goto done;
4284 if (do_list) {
4285 error = got_repo_open(&repo, repo_path, NULL);
4286 if (error != NULL)
4287 goto done;
4288 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4289 if (error)
4290 goto done;
4291 error = list_tags(repo, worktree);
4292 } else {
4293 error = get_gitconfig_path(&gitconfig_path);
4294 if (error)
4295 goto done;
4296 error = got_repo_open(&repo, repo_path, gitconfig_path);
4297 if (error != NULL)
4298 goto done;
4300 if (tagmsg) {
4301 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4302 if (error)
4303 goto done;
4306 if (commit_id_arg == NULL) {
4307 struct got_reference *head_ref;
4308 struct got_object_id *commit_id;
4309 error = got_ref_open(&head_ref, repo,
4310 worktree ? got_worktree_get_head_ref_name(worktree)
4311 : GOT_REF_HEAD, 0);
4312 if (error)
4313 goto done;
4314 error = got_ref_resolve(&commit_id, repo, head_ref);
4315 got_ref_close(head_ref);
4316 if (error)
4317 goto done;
4318 error = got_object_id_str(&commit_id_str, commit_id);
4319 free(commit_id);
4320 if (error)
4321 goto done;
4324 error = add_tag(repo, tag_name,
4325 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4327 done:
4328 if (repo)
4329 got_repo_close(repo);
4330 if (worktree)
4331 got_worktree_close(worktree);
4332 free(cwd);
4333 free(repo_path);
4334 free(gitconfig_path);
4335 free(commit_id_str);
4336 return error;
4339 __dead static void
4340 usage_add(void)
4342 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4343 getprogname());
4344 exit(1);
4347 static const struct got_error *
4348 add_progress(void *arg, unsigned char status, const char *path)
4350 while (path[0] == '/')
4351 path++;
4352 printf("%c %s\n", status, path);
4353 return NULL;
4356 static const struct got_error *
4357 cmd_add(int argc, char *argv[])
4359 const struct got_error *error = NULL;
4360 struct got_repository *repo = NULL;
4361 struct got_worktree *worktree = NULL;
4362 char *cwd = NULL;
4363 struct got_pathlist_head paths;
4364 struct got_pathlist_entry *pe;
4365 int ch, can_recurse = 0, no_ignores = 0;
4367 TAILQ_INIT(&paths);
4369 while ((ch = getopt(argc, argv, "IR")) != -1) {
4370 switch (ch) {
4371 case 'I':
4372 no_ignores = 1;
4373 break;
4374 case 'R':
4375 can_recurse = 1;
4376 break;
4377 default:
4378 usage_add();
4379 /* NOTREACHED */
4383 argc -= optind;
4384 argv += optind;
4386 #ifndef PROFILE
4387 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4388 NULL) == -1)
4389 err(1, "pledge");
4390 #endif
4391 if (argc < 1)
4392 usage_add();
4394 cwd = getcwd(NULL, 0);
4395 if (cwd == NULL) {
4396 error = got_error_from_errno("getcwd");
4397 goto done;
4400 error = got_worktree_open(&worktree, cwd);
4401 if (error)
4402 goto done;
4404 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4405 NULL);
4406 if (error != NULL)
4407 goto done;
4409 error = apply_unveil(got_repo_get_path(repo), 1,
4410 got_worktree_get_root_path(worktree));
4411 if (error)
4412 goto done;
4414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4415 if (error)
4416 goto done;
4418 if (!can_recurse && no_ignores) {
4419 error = got_error_msg(GOT_ERR_BAD_PATH,
4420 "disregarding ignores requires -R option");
4421 goto done;
4425 if (!can_recurse) {
4426 char *ondisk_path;
4427 struct stat sb;
4428 TAILQ_FOREACH(pe, &paths, entry) {
4429 if (asprintf(&ondisk_path, "%s/%s",
4430 got_worktree_get_root_path(worktree),
4431 pe->path) == -1) {
4432 error = got_error_from_errno("asprintf");
4433 goto done;
4435 if (lstat(ondisk_path, &sb) == -1) {
4436 if (errno == ENOENT) {
4437 free(ondisk_path);
4438 continue;
4440 error = got_error_from_errno2("lstat",
4441 ondisk_path);
4442 free(ondisk_path);
4443 goto done;
4445 free(ondisk_path);
4446 if (S_ISDIR(sb.st_mode)) {
4447 error = got_error_msg(GOT_ERR_BAD_PATH,
4448 "adding directories requires -R option");
4449 goto done;
4454 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4455 NULL, repo, no_ignores);
4456 done:
4457 if (repo)
4458 got_repo_close(repo);
4459 if (worktree)
4460 got_worktree_close(worktree);
4461 TAILQ_FOREACH(pe, &paths, entry)
4462 free((char *)pe->path);
4463 got_pathlist_free(&paths);
4464 free(cwd);
4465 return error;
4468 __dead static void
4469 usage_remove(void)
4471 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4472 getprogname());
4473 exit(1);
4476 static const struct got_error *
4477 print_remove_status(void *arg, unsigned char status,
4478 unsigned char staged_status, const char *path)
4480 while (path[0] == '/')
4481 path++;
4482 if (status == GOT_STATUS_NONEXISTENT)
4483 return NULL;
4484 if (status == staged_status && (status == GOT_STATUS_DELETE))
4485 status = GOT_STATUS_NO_CHANGE;
4486 printf("%c%c %s\n", status, staged_status, path);
4487 return NULL;
4490 static const struct got_error *
4491 cmd_remove(int argc, char *argv[])
4493 const struct got_error *error = NULL;
4494 struct got_worktree *worktree = NULL;
4495 struct got_repository *repo = NULL;
4496 char *cwd = NULL;
4497 struct got_pathlist_head paths;
4498 struct got_pathlist_entry *pe;
4499 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4501 TAILQ_INIT(&paths);
4503 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4504 switch (ch) {
4505 case 'f':
4506 delete_local_mods = 1;
4507 break;
4508 case 'k':
4509 keep_on_disk = 1;
4510 break;
4511 case 'R':
4512 can_recurse = 1;
4513 break;
4514 default:
4515 usage_remove();
4516 /* NOTREACHED */
4520 argc -= optind;
4521 argv += optind;
4523 #ifndef PROFILE
4524 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4525 NULL) == -1)
4526 err(1, "pledge");
4527 #endif
4528 if (argc < 1)
4529 usage_remove();
4531 cwd = getcwd(NULL, 0);
4532 if (cwd == NULL) {
4533 error = got_error_from_errno("getcwd");
4534 goto done;
4536 error = got_worktree_open(&worktree, cwd);
4537 if (error)
4538 goto done;
4540 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4541 NULL);
4542 if (error)
4543 goto done;
4545 error = apply_unveil(got_repo_get_path(repo), 1,
4546 got_worktree_get_root_path(worktree));
4547 if (error)
4548 goto done;
4550 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4551 if (error)
4552 goto done;
4554 if (!can_recurse) {
4555 char *ondisk_path;
4556 struct stat sb;
4557 TAILQ_FOREACH(pe, &paths, entry) {
4558 if (asprintf(&ondisk_path, "%s/%s",
4559 got_worktree_get_root_path(worktree),
4560 pe->path) == -1) {
4561 error = got_error_from_errno("asprintf");
4562 goto done;
4564 if (lstat(ondisk_path, &sb) == -1) {
4565 if (errno == ENOENT) {
4566 free(ondisk_path);
4567 continue;
4569 error = got_error_from_errno2("lstat",
4570 ondisk_path);
4571 free(ondisk_path);
4572 goto done;
4574 free(ondisk_path);
4575 if (S_ISDIR(sb.st_mode)) {
4576 error = got_error_msg(GOT_ERR_BAD_PATH,
4577 "removing directories requires -R option");
4578 goto done;
4583 error = got_worktree_schedule_delete(worktree, &paths,
4584 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4585 done:
4586 if (repo)
4587 got_repo_close(repo);
4588 if (worktree)
4589 got_worktree_close(worktree);
4590 TAILQ_FOREACH(pe, &paths, entry)
4591 free((char *)pe->path);
4592 got_pathlist_free(&paths);
4593 free(cwd);
4594 return error;
4597 __dead static void
4598 usage_revert(void)
4600 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4601 "path ...\n", getprogname());
4602 exit(1);
4605 static const struct got_error *
4606 revert_progress(void *arg, unsigned char status, const char *path)
4608 if (status == GOT_STATUS_UNVERSIONED)
4609 return NULL;
4611 while (path[0] == '/')
4612 path++;
4613 printf("%c %s\n", status, path);
4614 return NULL;
4617 struct choose_patch_arg {
4618 FILE *patch_script_file;
4619 const char *action;
4622 static const struct got_error *
4623 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4624 int nchanges, const char *action)
4626 char *line = NULL;
4627 size_t linesize = 0;
4628 ssize_t linelen;
4630 switch (status) {
4631 case GOT_STATUS_ADD:
4632 printf("A %s\n%s this addition? [y/n] ", path, action);
4633 break;
4634 case GOT_STATUS_DELETE:
4635 printf("D %s\n%s this deletion? [y/n] ", path, action);
4636 break;
4637 case GOT_STATUS_MODIFY:
4638 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4639 return got_error_from_errno("fseek");
4640 printf(GOT_COMMIT_SEP_STR);
4641 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4642 printf("%s", line);
4643 if (ferror(patch_file))
4644 return got_error_from_errno("getline");
4645 printf(GOT_COMMIT_SEP_STR);
4646 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4647 path, n, nchanges, action);
4648 break;
4649 default:
4650 return got_error_path(path, GOT_ERR_FILE_STATUS);
4653 return NULL;
4656 static const struct got_error *
4657 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4658 FILE *patch_file, int n, int nchanges)
4660 const struct got_error *err = NULL;
4661 char *line = NULL;
4662 size_t linesize = 0;
4663 ssize_t linelen;
4664 int resp = ' ';
4665 struct choose_patch_arg *a = arg;
4667 *choice = GOT_PATCH_CHOICE_NONE;
4669 if (a->patch_script_file) {
4670 char *nl;
4671 err = show_change(status, path, patch_file, n, nchanges,
4672 a->action);
4673 if (err)
4674 return err;
4675 linelen = getline(&line, &linesize, a->patch_script_file);
4676 if (linelen == -1) {
4677 if (ferror(a->patch_script_file))
4678 return got_error_from_errno("getline");
4679 return NULL;
4681 nl = strchr(line, '\n');
4682 if (nl)
4683 *nl = '\0';
4684 if (strcmp(line, "y") == 0) {
4685 *choice = GOT_PATCH_CHOICE_YES;
4686 printf("y\n");
4687 } else if (strcmp(line, "n") == 0) {
4688 *choice = GOT_PATCH_CHOICE_NO;
4689 printf("n\n");
4690 } else if (strcmp(line, "q") == 0 &&
4691 status == GOT_STATUS_MODIFY) {
4692 *choice = GOT_PATCH_CHOICE_QUIT;
4693 printf("q\n");
4694 } else
4695 printf("invalid response '%s'\n", line);
4696 free(line);
4697 return NULL;
4700 while (resp != 'y' && resp != 'n' && resp != 'q') {
4701 err = show_change(status, path, patch_file, n, nchanges,
4702 a->action);
4703 if (err)
4704 return err;
4705 resp = getchar();
4706 if (resp == '\n')
4707 resp = getchar();
4708 if (status == GOT_STATUS_MODIFY) {
4709 if (resp != 'y' && resp != 'n' && resp != 'q') {
4710 printf("invalid response '%c'\n", resp);
4711 resp = ' ';
4713 } else if (resp != 'y' && resp != 'n') {
4714 printf("invalid response '%c'\n", resp);
4715 resp = ' ';
4719 if (resp == 'y')
4720 *choice = GOT_PATCH_CHOICE_YES;
4721 else if (resp == 'n')
4722 *choice = GOT_PATCH_CHOICE_NO;
4723 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4724 *choice = GOT_PATCH_CHOICE_QUIT;
4726 return NULL;
4730 static const struct got_error *
4731 cmd_revert(int argc, char *argv[])
4733 const struct got_error *error = NULL;
4734 struct got_worktree *worktree = NULL;
4735 struct got_repository *repo = NULL;
4736 char *cwd = NULL, *path = NULL;
4737 struct got_pathlist_head paths;
4738 struct got_pathlist_entry *pe;
4739 int ch, can_recurse = 0, pflag = 0;
4740 FILE *patch_script_file = NULL;
4741 const char *patch_script_path = NULL;
4742 struct choose_patch_arg cpa;
4744 TAILQ_INIT(&paths);
4746 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4747 switch (ch) {
4748 case 'p':
4749 pflag = 1;
4750 break;
4751 case 'F':
4752 patch_script_path = optarg;
4753 break;
4754 case 'R':
4755 can_recurse = 1;
4756 break;
4757 default:
4758 usage_revert();
4759 /* NOTREACHED */
4763 argc -= optind;
4764 argv += optind;
4766 #ifndef PROFILE
4767 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4768 "unveil", NULL) == -1)
4769 err(1, "pledge");
4770 #endif
4771 if (argc < 1)
4772 usage_revert();
4773 if (patch_script_path && !pflag)
4774 errx(1, "-F option can only be used together with -p option");
4776 cwd = getcwd(NULL, 0);
4777 if (cwd == NULL) {
4778 error = got_error_from_errno("getcwd");
4779 goto done;
4781 error = got_worktree_open(&worktree, cwd);
4782 if (error)
4783 goto done;
4785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4786 NULL);
4787 if (error != NULL)
4788 goto done;
4790 if (patch_script_path) {
4791 patch_script_file = fopen(patch_script_path, "r");
4792 if (patch_script_file == NULL) {
4793 error = got_error_from_errno2("fopen",
4794 patch_script_path);
4795 goto done;
4798 error = apply_unveil(got_repo_get_path(repo), 1,
4799 got_worktree_get_root_path(worktree));
4800 if (error)
4801 goto done;
4803 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4804 if (error)
4805 goto done;
4807 if (!can_recurse) {
4808 char *ondisk_path;
4809 struct stat sb;
4810 TAILQ_FOREACH(pe, &paths, entry) {
4811 if (asprintf(&ondisk_path, "%s/%s",
4812 got_worktree_get_root_path(worktree),
4813 pe->path) == -1) {
4814 error = got_error_from_errno("asprintf");
4815 goto done;
4817 if (lstat(ondisk_path, &sb) == -1) {
4818 if (errno == ENOENT) {
4819 free(ondisk_path);
4820 continue;
4822 error = got_error_from_errno2("lstat",
4823 ondisk_path);
4824 free(ondisk_path);
4825 goto done;
4827 free(ondisk_path);
4828 if (S_ISDIR(sb.st_mode)) {
4829 error = got_error_msg(GOT_ERR_BAD_PATH,
4830 "reverting directories requires -R option");
4831 goto done;
4836 cpa.patch_script_file = patch_script_file;
4837 cpa.action = "revert";
4838 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4839 pflag ? choose_patch : NULL, &cpa, repo);
4840 done:
4841 if (patch_script_file && fclose(patch_script_file) == EOF &&
4842 error == NULL)
4843 error = got_error_from_errno2("fclose", patch_script_path);
4844 if (repo)
4845 got_repo_close(repo);
4846 if (worktree)
4847 got_worktree_close(worktree);
4848 free(path);
4849 free(cwd);
4850 return error;
4853 __dead static void
4854 usage_commit(void)
4856 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4857 getprogname());
4858 exit(1);
4861 struct collect_commit_logmsg_arg {
4862 const char *cmdline_log;
4863 const char *editor;
4864 const char *worktree_path;
4865 const char *branch_name;
4866 const char *repo_path;
4867 char *logmsg_path;
4871 static const struct got_error *
4872 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4873 void *arg)
4875 char *initial_content = NULL;
4876 struct got_pathlist_entry *pe;
4877 const struct got_error *err = NULL;
4878 char *template = NULL;
4879 struct collect_commit_logmsg_arg *a = arg;
4880 int fd;
4881 size_t len;
4883 /* if a message was specified on the command line, just use it */
4884 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4885 len = strlen(a->cmdline_log) + 1;
4886 *logmsg = malloc(len + 1);
4887 if (*logmsg == NULL)
4888 return got_error_from_errno("malloc");
4889 strlcpy(*logmsg, a->cmdline_log, len);
4890 return NULL;
4893 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4894 return got_error_from_errno("asprintf");
4896 if (asprintf(&initial_content,
4897 "\n# changes to be committed on branch %s:\n",
4898 a->branch_name) == -1)
4899 return got_error_from_errno("asprintf");
4901 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4902 if (err)
4903 goto done;
4905 dprintf(fd, initial_content);
4907 TAILQ_FOREACH(pe, commitable_paths, entry) {
4908 struct got_commitable *ct = pe->data;
4909 dprintf(fd, "# %c %s\n",
4910 got_commitable_get_status(ct),
4911 got_commitable_get_path(ct));
4913 close(fd);
4915 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4916 done:
4917 free(initial_content);
4918 free(template);
4920 /* Editor is done; we can now apply unveil(2) */
4921 if (err == NULL) {
4922 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4923 if (err) {
4924 free(*logmsg);
4925 *logmsg = NULL;
4928 return err;
4931 static const struct got_error *
4932 cmd_commit(int argc, char *argv[])
4934 const struct got_error *error = NULL;
4935 struct got_worktree *worktree = NULL;
4936 struct got_repository *repo = NULL;
4937 char *cwd = NULL, *id_str = NULL;
4938 struct got_object_id *id = NULL;
4939 const char *logmsg = NULL;
4940 struct collect_commit_logmsg_arg cl_arg;
4941 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4942 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4943 struct got_pathlist_head paths;
4945 TAILQ_INIT(&paths);
4946 cl_arg.logmsg_path = NULL;
4948 while ((ch = getopt(argc, argv, "m:")) != -1) {
4949 switch (ch) {
4950 case 'm':
4951 logmsg = optarg;
4952 break;
4953 default:
4954 usage_commit();
4955 /* NOTREACHED */
4959 argc -= optind;
4960 argv += optind;
4962 #ifndef PROFILE
4963 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4964 "unveil", NULL) == -1)
4965 err(1, "pledge");
4966 #endif
4967 cwd = getcwd(NULL, 0);
4968 if (cwd == NULL) {
4969 error = got_error_from_errno("getcwd");
4970 goto done;
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error)
4974 goto done;
4976 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4977 if (error)
4978 goto done;
4979 if (rebase_in_progress) {
4980 error = got_error(GOT_ERR_REBASING);
4981 goto done;
4984 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4985 worktree);
4986 if (error)
4987 goto done;
4989 error = get_gitconfig_path(&gitconfig_path);
4990 if (error)
4991 goto done;
4992 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4993 gitconfig_path);
4994 if (error != NULL)
4995 goto done;
4997 error = get_author(&author, repo);
4998 if (error)
4999 return error;
5002 * unveil(2) traverses exec(2); if an editor is used we have
5003 * to apply unveil after the log message has been written.
5005 if (logmsg == NULL || strlen(logmsg) == 0)
5006 error = get_editor(&editor);
5007 else
5008 error = apply_unveil(got_repo_get_path(repo), 0,
5009 got_worktree_get_root_path(worktree));
5010 if (error)
5011 goto done;
5013 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5014 if (error)
5015 goto done;
5017 cl_arg.editor = editor;
5018 cl_arg.cmdline_log = logmsg;
5019 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5020 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5021 if (!histedit_in_progress) {
5022 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5023 error = got_error(GOT_ERR_COMMIT_BRANCH);
5024 goto done;
5026 cl_arg.branch_name += 11;
5028 cl_arg.repo_path = got_repo_get_path(repo);
5029 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5030 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5031 if (error) {
5032 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5033 cl_arg.logmsg_path != NULL)
5034 preserve_logmsg = 1;
5035 goto done;
5038 error = got_object_id_str(&id_str, id);
5039 if (error)
5040 goto done;
5041 printf("Created commit %s\n", id_str);
5042 done:
5043 if (preserve_logmsg) {
5044 fprintf(stderr, "%s: log message preserved in %s\n",
5045 getprogname(), cl_arg.logmsg_path);
5046 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5047 error == NULL)
5048 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5049 free(cl_arg.logmsg_path);
5050 if (repo)
5051 got_repo_close(repo);
5052 if (worktree)
5053 got_worktree_close(worktree);
5054 free(cwd);
5055 free(id_str);
5056 free(gitconfig_path);
5057 free(editor);
5058 free(author);
5059 return error;
5062 __dead static void
5063 usage_cherrypick(void)
5065 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5066 exit(1);
5069 static const struct got_error *
5070 cmd_cherrypick(int argc, char *argv[])
5072 const struct got_error *error = NULL;
5073 struct got_worktree *worktree = NULL;
5074 struct got_repository *repo = NULL;
5075 char *cwd = NULL, *commit_id_str = NULL;
5076 struct got_object_id *commit_id = NULL;
5077 struct got_commit_object *commit = NULL;
5078 struct got_object_qid *pid;
5079 struct got_reference *head_ref = NULL;
5080 int ch, did_something = 0;
5082 while ((ch = getopt(argc, argv, "")) != -1) {
5083 switch (ch) {
5084 default:
5085 usage_cherrypick();
5086 /* NOTREACHED */
5090 argc -= optind;
5091 argv += optind;
5093 #ifndef PROFILE
5094 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5095 "unveil", NULL) == -1)
5096 err(1, "pledge");
5097 #endif
5098 if (argc != 1)
5099 usage_cherrypick();
5101 cwd = getcwd(NULL, 0);
5102 if (cwd == NULL) {
5103 error = got_error_from_errno("getcwd");
5104 goto done;
5106 error = got_worktree_open(&worktree, cwd);
5107 if (error)
5108 goto done;
5110 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5111 NULL);
5112 if (error != NULL)
5113 goto done;
5115 error = apply_unveil(got_repo_get_path(repo), 0,
5116 got_worktree_get_root_path(worktree));
5117 if (error)
5118 goto done;
5120 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5121 GOT_OBJ_TYPE_COMMIT, repo);
5122 if (error != NULL) {
5123 struct got_reference *ref;
5124 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5125 goto done;
5126 error = got_ref_open(&ref, repo, argv[0], 0);
5127 if (error != NULL)
5128 goto done;
5129 error = got_ref_resolve(&commit_id, repo, ref);
5130 got_ref_close(ref);
5131 if (error != NULL)
5132 goto done;
5134 error = got_object_id_str(&commit_id_str, commit_id);
5135 if (error)
5136 goto done;
5138 error = got_ref_open(&head_ref, repo,
5139 got_worktree_get_head_ref_name(worktree), 0);
5140 if (error != NULL)
5141 goto done;
5143 error = check_same_branch(commit_id, head_ref, NULL, repo);
5144 if (error) {
5145 if (error->code != GOT_ERR_ANCESTRY)
5146 goto done;
5147 error = NULL;
5148 } else {
5149 error = got_error(GOT_ERR_SAME_BRANCH);
5150 goto done;
5153 error = got_object_open_as_commit(&commit, repo, commit_id);
5154 if (error)
5155 goto done;
5156 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5157 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5158 commit_id, repo, update_progress, &did_something, check_cancelled,
5159 NULL);
5160 if (error != NULL)
5161 goto done;
5163 if (did_something)
5164 printf("Merged commit %s\n", commit_id_str);
5165 done:
5166 if (commit)
5167 got_object_commit_close(commit);
5168 free(commit_id_str);
5169 if (head_ref)
5170 got_ref_close(head_ref);
5171 if (worktree)
5172 got_worktree_close(worktree);
5173 if (repo)
5174 got_repo_close(repo);
5175 return error;
5178 __dead static void
5179 usage_backout(void)
5181 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5182 exit(1);
5185 static const struct got_error *
5186 cmd_backout(int argc, char *argv[])
5188 const struct got_error *error = NULL;
5189 struct got_worktree *worktree = NULL;
5190 struct got_repository *repo = NULL;
5191 char *cwd = NULL, *commit_id_str = NULL;
5192 struct got_object_id *commit_id = NULL;
5193 struct got_commit_object *commit = NULL;
5194 struct got_object_qid *pid;
5195 struct got_reference *head_ref = NULL;
5196 int ch, did_something = 0;
5198 while ((ch = getopt(argc, argv, "")) != -1) {
5199 switch (ch) {
5200 default:
5201 usage_backout();
5202 /* NOTREACHED */
5206 argc -= optind;
5207 argv += optind;
5209 #ifndef PROFILE
5210 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5211 "unveil", NULL) == -1)
5212 err(1, "pledge");
5213 #endif
5214 if (argc != 1)
5215 usage_backout();
5217 cwd = getcwd(NULL, 0);
5218 if (cwd == NULL) {
5219 error = got_error_from_errno("getcwd");
5220 goto done;
5222 error = got_worktree_open(&worktree, cwd);
5223 if (error)
5224 goto done;
5226 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5227 NULL);
5228 if (error != NULL)
5229 goto done;
5231 error = apply_unveil(got_repo_get_path(repo), 0,
5232 got_worktree_get_root_path(worktree));
5233 if (error)
5234 goto done;
5236 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5237 GOT_OBJ_TYPE_COMMIT, repo);
5238 if (error != NULL) {
5239 struct got_reference *ref;
5240 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5241 goto done;
5242 error = got_ref_open(&ref, repo, argv[0], 0);
5243 if (error != NULL)
5244 goto done;
5245 error = got_ref_resolve(&commit_id, repo, ref);
5246 got_ref_close(ref);
5247 if (error != NULL)
5248 goto done;
5250 error = got_object_id_str(&commit_id_str, commit_id);
5251 if (error)
5252 goto done;
5254 error = got_ref_open(&head_ref, repo,
5255 got_worktree_get_head_ref_name(worktree), 0);
5256 if (error != NULL)
5257 goto done;
5259 error = check_same_branch(commit_id, head_ref, NULL, repo);
5260 if (error)
5261 goto done;
5263 error = got_object_open_as_commit(&commit, repo, commit_id);
5264 if (error)
5265 goto done;
5266 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5267 if (pid == NULL) {
5268 error = got_error(GOT_ERR_ROOT_COMMIT);
5269 goto done;
5272 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5273 update_progress, &did_something, check_cancelled, NULL);
5274 if (error != NULL)
5275 goto done;
5277 if (did_something)
5278 printf("Backed out commit %s\n", commit_id_str);
5279 done:
5280 if (commit)
5281 got_object_commit_close(commit);
5282 free(commit_id_str);
5283 if (head_ref)
5284 got_ref_close(head_ref);
5285 if (worktree)
5286 got_worktree_close(worktree);
5287 if (repo)
5288 got_repo_close(repo);
5289 return error;
5292 __dead static void
5293 usage_rebase(void)
5295 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5296 getprogname());
5297 exit(1);
5300 void
5301 trim_logmsg(char *logmsg, int limit)
5303 char *nl;
5304 size_t len;
5306 len = strlen(logmsg);
5307 if (len > limit)
5308 len = limit;
5309 logmsg[len] = '\0';
5310 nl = strchr(logmsg, '\n');
5311 if (nl)
5312 *nl = '\0';
5315 static const struct got_error *
5316 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5318 const struct got_error *err;
5319 char *logmsg0 = NULL;
5320 const char *s;
5322 err = got_object_commit_get_logmsg(&logmsg0, commit);
5323 if (err)
5324 return err;
5326 s = logmsg0;
5327 while (isspace((unsigned char)s[0]))
5328 s++;
5330 *logmsg = strdup(s);
5331 if (*logmsg == NULL) {
5332 err = got_error_from_errno("strdup");
5333 goto done;
5336 trim_logmsg(*logmsg, limit);
5337 done:
5338 free(logmsg0);
5339 return err;
5342 static const struct got_error *
5343 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5345 const struct got_error *err;
5346 struct got_commit_object *commit = NULL;
5347 char *id_str = NULL, *logmsg = NULL;
5349 err = got_object_open_as_commit(&commit, repo, id);
5350 if (err)
5351 return err;
5353 err = got_object_id_str(&id_str, id);
5354 if (err)
5355 goto done;
5357 id_str[12] = '\0';
5359 err = get_short_logmsg(&logmsg, 42, commit);
5360 if (err)
5361 goto done;
5363 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5364 done:
5365 free(id_str);
5366 got_object_commit_close(commit);
5367 free(logmsg);
5368 return err;
5371 static const struct got_error *
5372 show_rebase_progress(struct got_commit_object *commit,
5373 struct got_object_id *old_id, struct got_object_id *new_id)
5375 const struct got_error *err;
5376 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5378 err = got_object_id_str(&old_id_str, old_id);
5379 if (err)
5380 goto done;
5382 if (new_id) {
5383 err = got_object_id_str(&new_id_str, new_id);
5384 if (err)
5385 goto done;
5388 old_id_str[12] = '\0';
5389 if (new_id_str)
5390 new_id_str[12] = '\0';
5392 err = get_short_logmsg(&logmsg, 42, commit);
5393 if (err)
5394 goto done;
5396 printf("%s -> %s: %s\n", old_id_str,
5397 new_id_str ? new_id_str : "no-op change", logmsg);
5398 done:
5399 free(old_id_str);
5400 free(new_id_str);
5401 free(logmsg);
5402 return err;
5405 static const struct got_error *
5406 rebase_progress(void *arg, unsigned char status, const char *path)
5408 unsigned char *rebase_status = arg;
5410 while (path[0] == '/')
5411 path++;
5412 printf("%c %s\n", status, path);
5414 if (*rebase_status == GOT_STATUS_CONFLICT)
5415 return NULL;
5416 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5417 *rebase_status = status;
5418 return NULL;
5421 static const struct got_error *
5422 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5423 struct got_reference *branch, struct got_reference *new_base_branch,
5424 struct got_reference *tmp_branch, struct got_repository *repo)
5426 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5427 return got_worktree_rebase_complete(worktree, fileindex,
5428 new_base_branch, tmp_branch, branch, repo);
5431 static const struct got_error *
5432 rebase_commit(struct got_pathlist_head *merged_paths,
5433 struct got_worktree *worktree, struct got_fileindex *fileindex,
5434 struct got_reference *tmp_branch,
5435 struct got_object_id *commit_id, struct got_repository *repo)
5437 const struct got_error *error;
5438 struct got_commit_object *commit;
5439 struct got_object_id *new_commit_id;
5441 error = got_object_open_as_commit(&commit, repo, commit_id);
5442 if (error)
5443 return error;
5445 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5446 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5447 if (error) {
5448 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5449 goto done;
5450 error = show_rebase_progress(commit, commit_id, NULL);
5451 } else {
5452 error = show_rebase_progress(commit, commit_id, new_commit_id);
5453 free(new_commit_id);
5455 done:
5456 got_object_commit_close(commit);
5457 return error;
5460 struct check_path_prefix_arg {
5461 const char *path_prefix;
5462 size_t len;
5463 int errcode;
5466 static const struct got_error *
5467 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5468 struct got_blob_object *blob2, struct got_object_id *id1,
5469 struct got_object_id *id2, const char *path1, const char *path2,
5470 mode_t mode1, mode_t mode2, struct got_repository *repo)
5472 struct check_path_prefix_arg *a = arg;
5474 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5475 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5476 return got_error(a->errcode);
5478 return NULL;
5481 static const struct got_error *
5482 check_path_prefix(struct got_object_id *parent_id,
5483 struct got_object_id *commit_id, const char *path_prefix,
5484 int errcode, struct got_repository *repo)
5486 const struct got_error *err;
5487 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5488 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5489 struct check_path_prefix_arg cpp_arg;
5491 if (got_path_is_root_dir(path_prefix))
5492 return NULL;
5494 err = got_object_open_as_commit(&commit, repo, commit_id);
5495 if (err)
5496 goto done;
5498 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5499 if (err)
5500 goto done;
5502 err = got_object_open_as_tree(&tree1, repo,
5503 got_object_commit_get_tree_id(parent_commit));
5504 if (err)
5505 goto done;
5507 err = got_object_open_as_tree(&tree2, repo,
5508 got_object_commit_get_tree_id(commit));
5509 if (err)
5510 goto done;
5512 cpp_arg.path_prefix = path_prefix;
5513 while (cpp_arg.path_prefix[0] == '/')
5514 cpp_arg.path_prefix++;
5515 cpp_arg.len = strlen(cpp_arg.path_prefix);
5516 cpp_arg.errcode = errcode;
5517 err = got_diff_tree(tree1, tree2, "", "", repo,
5518 check_path_prefix_in_diff, &cpp_arg, 0);
5519 done:
5520 if (tree1)
5521 got_object_tree_close(tree1);
5522 if (tree2)
5523 got_object_tree_close(tree2);
5524 if (commit)
5525 got_object_commit_close(commit);
5526 if (parent_commit)
5527 got_object_commit_close(parent_commit);
5528 return err;
5531 static const struct got_error *
5532 collect_commits(struct got_object_id_queue *commits,
5533 struct got_object_id *initial_commit_id,
5534 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5535 const char *path_prefix, int path_prefix_errcode,
5536 struct got_repository *repo)
5538 const struct got_error *err = NULL;
5539 struct got_commit_graph *graph = NULL;
5540 struct got_object_id *parent_id = NULL;
5541 struct got_object_qid *qid;
5542 struct got_object_id *commit_id = initial_commit_id;
5544 err = got_commit_graph_open(&graph, "/", 1);
5545 if (err)
5546 return err;
5548 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5549 check_cancelled, NULL);
5550 if (err)
5551 goto done;
5552 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5553 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5554 check_cancelled, NULL);
5555 if (err) {
5556 if (err->code == GOT_ERR_ITER_COMPLETED) {
5557 err = got_error_msg(GOT_ERR_ANCESTRY,
5558 "ran out of commits to rebase before "
5559 "youngest common ancestor commit has "
5560 "been reached?!?");
5562 goto done;
5563 } else {
5564 err = check_path_prefix(parent_id, commit_id,
5565 path_prefix, path_prefix_errcode, repo);
5566 if (err)
5567 goto done;
5569 err = got_object_qid_alloc(&qid, commit_id);
5570 if (err)
5571 goto done;
5572 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5573 commit_id = parent_id;
5576 done:
5577 got_commit_graph_close(graph);
5578 return err;
5581 static const struct got_error *
5582 cmd_rebase(int argc, char *argv[])
5584 const struct got_error *error = NULL;
5585 struct got_worktree *worktree = NULL;
5586 struct got_repository *repo = NULL;
5587 struct got_fileindex *fileindex = NULL;
5588 char *cwd = NULL;
5589 struct got_reference *branch = NULL;
5590 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5591 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5592 struct got_object_id *resume_commit_id = NULL;
5593 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5594 struct got_commit_object *commit = NULL;
5595 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5596 int histedit_in_progress = 0;
5597 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5598 struct got_object_id_queue commits;
5599 struct got_pathlist_head merged_paths;
5600 const struct got_object_id_queue *parent_ids;
5601 struct got_object_qid *qid, *pid;
5603 SIMPLEQ_INIT(&commits);
5604 TAILQ_INIT(&merged_paths);
5606 while ((ch = getopt(argc, argv, "ac")) != -1) {
5607 switch (ch) {
5608 case 'a':
5609 abort_rebase = 1;
5610 break;
5611 case 'c':
5612 continue_rebase = 1;
5613 break;
5614 default:
5615 usage_rebase();
5616 /* NOTREACHED */
5620 argc -= optind;
5621 argv += optind;
5623 #ifndef PROFILE
5624 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5625 "unveil", NULL) == -1)
5626 err(1, "pledge");
5627 #endif
5628 if (abort_rebase && continue_rebase)
5629 usage_rebase();
5630 else if (abort_rebase || continue_rebase) {
5631 if (argc != 0)
5632 usage_rebase();
5633 } else if (argc != 1)
5634 usage_rebase();
5636 cwd = getcwd(NULL, 0);
5637 if (cwd == NULL) {
5638 error = got_error_from_errno("getcwd");
5639 goto done;
5641 error = got_worktree_open(&worktree, cwd);
5642 if (error)
5643 goto done;
5645 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5646 NULL);
5647 if (error != NULL)
5648 goto done;
5650 error = apply_unveil(got_repo_get_path(repo), 0,
5651 got_worktree_get_root_path(worktree));
5652 if (error)
5653 goto done;
5655 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5656 worktree);
5657 if (error)
5658 goto done;
5659 if (histedit_in_progress) {
5660 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5661 goto done;
5664 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5665 if (error)
5666 goto done;
5668 if (abort_rebase) {
5669 int did_something;
5670 if (!rebase_in_progress) {
5671 error = got_error(GOT_ERR_NOT_REBASING);
5672 goto done;
5674 error = got_worktree_rebase_continue(&resume_commit_id,
5675 &new_base_branch, &tmp_branch, &branch, &fileindex,
5676 worktree, repo);
5677 if (error)
5678 goto done;
5679 printf("Switching work tree to %s\n",
5680 got_ref_get_symref_target(new_base_branch));
5681 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5682 new_base_branch, update_progress, &did_something);
5683 if (error)
5684 goto done;
5685 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5686 goto done; /* nothing else to do */
5689 if (continue_rebase) {
5690 if (!rebase_in_progress) {
5691 error = got_error(GOT_ERR_NOT_REBASING);
5692 goto done;
5694 error = got_worktree_rebase_continue(&resume_commit_id,
5695 &new_base_branch, &tmp_branch, &branch, &fileindex,
5696 worktree, repo);
5697 if (error)
5698 goto done;
5700 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5701 resume_commit_id, repo);
5702 if (error)
5703 goto done;
5705 yca_id = got_object_id_dup(resume_commit_id);
5706 if (yca_id == NULL) {
5707 error = got_error_from_errno("got_object_id_dup");
5708 goto done;
5710 } else {
5711 error = got_ref_open(&branch, repo, argv[0], 0);
5712 if (error != NULL)
5713 goto done;
5716 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5717 if (error)
5718 goto done;
5720 if (!continue_rebase) {
5721 struct got_object_id *base_commit_id;
5723 base_commit_id = got_worktree_get_base_commit_id(worktree);
5724 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5725 base_commit_id, branch_head_commit_id, repo,
5726 check_cancelled, NULL);
5727 if (error)
5728 goto done;
5729 if (yca_id == NULL) {
5730 error = got_error_msg(GOT_ERR_ANCESTRY,
5731 "specified branch shares no common ancestry "
5732 "with work tree's branch");
5733 goto done;
5736 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5737 if (error) {
5738 if (error->code != GOT_ERR_ANCESTRY)
5739 goto done;
5740 error = NULL;
5741 } else {
5742 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5743 "specified branch resolves to a commit which "
5744 "is already contained in work tree's branch");
5745 goto done;
5747 error = got_worktree_rebase_prepare(&new_base_branch,
5748 &tmp_branch, &fileindex, worktree, branch, repo);
5749 if (error)
5750 goto done;
5753 commit_id = branch_head_commit_id;
5754 error = got_object_open_as_commit(&commit, repo, commit_id);
5755 if (error)
5756 goto done;
5758 parent_ids = got_object_commit_get_parent_ids(commit);
5759 pid = SIMPLEQ_FIRST(parent_ids);
5760 if (pid == NULL) {
5761 if (!continue_rebase) {
5762 int did_something;
5763 error = got_worktree_rebase_abort(worktree, fileindex,
5764 repo, new_base_branch, update_progress,
5765 &did_something);
5766 if (error)
5767 goto done;
5768 printf("Rebase of %s aborted\n",
5769 got_ref_get_name(branch));
5771 error = got_error(GOT_ERR_EMPTY_REBASE);
5772 goto done;
5774 error = collect_commits(&commits, commit_id, pid->id,
5775 yca_id, got_worktree_get_path_prefix(worktree),
5776 GOT_ERR_REBASE_PATH, repo);
5777 got_object_commit_close(commit);
5778 commit = NULL;
5779 if (error)
5780 goto done;
5782 if (SIMPLEQ_EMPTY(&commits)) {
5783 if (continue_rebase) {
5784 error = rebase_complete(worktree, fileindex,
5785 branch, new_base_branch, tmp_branch, repo);
5786 goto done;
5787 } else {
5788 /* Fast-forward the reference of the branch. */
5789 struct got_object_id *new_head_commit_id;
5790 char *id_str;
5791 error = got_ref_resolve(&new_head_commit_id, repo,
5792 new_base_branch);
5793 if (error)
5794 goto done;
5795 error = got_object_id_str(&id_str, new_head_commit_id);
5796 printf("Forwarding %s to commit %s\n",
5797 got_ref_get_name(branch), id_str);
5798 free(id_str);
5799 error = got_ref_change_ref(branch,
5800 new_head_commit_id);
5801 if (error)
5802 goto done;
5806 pid = NULL;
5807 SIMPLEQ_FOREACH(qid, &commits, entry) {
5808 commit_id = qid->id;
5809 parent_id = pid ? pid->id : yca_id;
5810 pid = qid;
5812 error = got_worktree_rebase_merge_files(&merged_paths,
5813 worktree, fileindex, parent_id, commit_id, repo,
5814 rebase_progress, &rebase_status, check_cancelled, NULL);
5815 if (error)
5816 goto done;
5818 if (rebase_status == GOT_STATUS_CONFLICT) {
5819 error = show_rebase_merge_conflict(qid->id, repo);
5820 if (error)
5821 goto done;
5822 got_worktree_rebase_pathlist_free(&merged_paths);
5823 break;
5826 error = rebase_commit(&merged_paths, worktree, fileindex,
5827 tmp_branch, commit_id, repo);
5828 got_worktree_rebase_pathlist_free(&merged_paths);
5829 if (error)
5830 goto done;
5833 if (rebase_status == GOT_STATUS_CONFLICT) {
5834 error = got_worktree_rebase_postpone(worktree, fileindex);
5835 if (error)
5836 goto done;
5837 error = got_error_msg(GOT_ERR_CONFLICTS,
5838 "conflicts must be resolved before rebasing can continue");
5839 } else
5840 error = rebase_complete(worktree, fileindex, branch,
5841 new_base_branch, tmp_branch, repo);
5842 done:
5843 got_object_id_queue_free(&commits);
5844 free(branch_head_commit_id);
5845 free(resume_commit_id);
5846 free(yca_id);
5847 if (commit)
5848 got_object_commit_close(commit);
5849 if (branch)
5850 got_ref_close(branch);
5851 if (new_base_branch)
5852 got_ref_close(new_base_branch);
5853 if (tmp_branch)
5854 got_ref_close(tmp_branch);
5855 if (worktree)
5856 got_worktree_close(worktree);
5857 if (repo)
5858 got_repo_close(repo);
5859 return error;
5862 __dead static void
5863 usage_histedit(void)
5865 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5866 getprogname());
5867 exit(1);
5870 #define GOT_HISTEDIT_PICK 'p'
5871 #define GOT_HISTEDIT_EDIT 'e'
5872 #define GOT_HISTEDIT_FOLD 'f'
5873 #define GOT_HISTEDIT_DROP 'd'
5874 #define GOT_HISTEDIT_MESG 'm'
5876 static struct got_histedit_cmd {
5877 unsigned char code;
5878 const char *name;
5879 const char *desc;
5880 } got_histedit_cmds[] = {
5881 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5882 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5883 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5884 "be used" },
5885 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5886 { GOT_HISTEDIT_MESG, "mesg",
5887 "single-line log message for commit above (open editor if empty)" },
5890 struct got_histedit_list_entry {
5891 TAILQ_ENTRY(got_histedit_list_entry) entry;
5892 struct got_object_id *commit_id;
5893 const struct got_histedit_cmd *cmd;
5894 char *logmsg;
5896 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5898 static const struct got_error *
5899 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5900 FILE *f, struct got_repository *repo)
5902 const struct got_error *err = NULL;
5903 char *logmsg = NULL, *id_str = NULL;
5904 struct got_commit_object *commit = NULL;
5905 int n;
5907 err = got_object_open_as_commit(&commit, repo, commit_id);
5908 if (err)
5909 goto done;
5911 err = get_short_logmsg(&logmsg, 34, commit);
5912 if (err)
5913 goto done;
5915 err = got_object_id_str(&id_str, commit_id);
5916 if (err)
5917 goto done;
5919 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5920 if (n < 0)
5921 err = got_ferror(f, GOT_ERR_IO);
5922 done:
5923 if (commit)
5924 got_object_commit_close(commit);
5925 free(id_str);
5926 free(logmsg);
5927 return err;
5930 static const struct got_error *
5931 histedit_write_commit_list(struct got_object_id_queue *commits,
5932 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5934 const struct got_error *err = NULL;
5935 struct got_object_qid *qid;
5937 if (SIMPLEQ_EMPTY(commits))
5938 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5940 SIMPLEQ_FOREACH(qid, commits, entry) {
5941 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5942 f, repo);
5943 if (err)
5944 break;
5945 if (edit_logmsg_only) {
5946 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5947 if (n < 0) {
5948 err = got_ferror(f, GOT_ERR_IO);
5949 break;
5954 return err;
5957 static const struct got_error *
5958 write_cmd_list(FILE *f, const char *branch_name,
5959 struct got_object_id_queue *commits)
5961 const struct got_error *err = NULL;
5962 int n, i;
5963 char *id_str;
5964 struct got_object_qid *qid;
5966 qid = SIMPLEQ_FIRST(commits);
5967 err = got_object_id_str(&id_str, qid->id);
5968 if (err)
5969 return err;
5971 n = fprintf(f,
5972 "# Editing the history of branch '%s' starting at\n"
5973 "# commit %s\n"
5974 "# Commits will be processed in order from top to "
5975 "bottom of this file.\n", branch_name, id_str);
5976 if (n < 0) {
5977 err = got_ferror(f, GOT_ERR_IO);
5978 goto done;
5981 n = fprintf(f, "# Available histedit commands:\n");
5982 if (n < 0) {
5983 err = got_ferror(f, GOT_ERR_IO);
5984 goto done;
5987 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5988 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5989 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5990 cmd->desc);
5991 if (n < 0) {
5992 err = got_ferror(f, GOT_ERR_IO);
5993 break;
5996 done:
5997 free(id_str);
5998 return err;
6001 static const struct got_error *
6002 histedit_syntax_error(int lineno)
6004 static char msg[42];
6005 int ret;
6007 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6008 lineno);
6009 if (ret == -1 || ret >= sizeof(msg))
6010 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6012 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6015 static const struct got_error *
6016 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6017 char *logmsg, struct got_repository *repo)
6019 const struct got_error *err;
6020 struct got_commit_object *folded_commit = NULL;
6021 char *id_str, *folded_logmsg = NULL;
6023 err = got_object_id_str(&id_str, hle->commit_id);
6024 if (err)
6025 return err;
6027 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6028 if (err)
6029 goto done;
6031 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6032 if (err)
6033 goto done;
6034 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6035 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6036 folded_logmsg) == -1) {
6037 err = got_error_from_errno("asprintf");
6039 done:
6040 if (folded_commit)
6041 got_object_commit_close(folded_commit);
6042 free(id_str);
6043 free(folded_logmsg);
6044 return err;
6047 static struct got_histedit_list_entry *
6048 get_folded_commits(struct got_histedit_list_entry *hle)
6050 struct got_histedit_list_entry *prev, *folded = NULL;
6052 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6053 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6054 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6055 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6056 folded = prev;
6057 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6060 return folded;
6063 static const struct got_error *
6064 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6065 struct got_repository *repo)
6067 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6068 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6069 const struct got_error *err = NULL;
6070 struct got_commit_object *commit = NULL;
6071 int fd;
6072 struct got_histedit_list_entry *folded = NULL;
6074 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6075 if (err)
6076 return err;
6078 folded = get_folded_commits(hle);
6079 if (folded) {
6080 while (folded != hle) {
6081 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6082 folded = TAILQ_NEXT(folded, entry);
6083 continue;
6085 err = append_folded_commit_msg(&new_msg, folded,
6086 logmsg, repo);
6087 if (err)
6088 goto done;
6089 free(logmsg);
6090 logmsg = new_msg;
6091 folded = TAILQ_NEXT(folded, entry);
6095 err = got_object_id_str(&id_str, hle->commit_id);
6096 if (err)
6097 goto done;
6098 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6099 if (err)
6100 goto done;
6101 if (asprintf(&new_msg,
6102 "%s\n# original log message of commit %s: %s",
6103 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6104 err = got_error_from_errno("asprintf");
6105 goto done;
6107 free(logmsg);
6108 logmsg = new_msg;
6110 err = got_object_id_str(&id_str, hle->commit_id);
6111 if (err)
6112 goto done;
6114 err = got_opentemp_named_fd(&logmsg_path, &fd,
6115 GOT_TMPDIR_STR "/got-logmsg");
6116 if (err)
6117 goto done;
6119 dprintf(fd, logmsg);
6120 close(fd);
6122 err = get_editor(&editor);
6123 if (err)
6124 goto done;
6126 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6127 if (err) {
6128 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6129 goto done;
6130 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6132 done:
6133 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6134 err = got_error_from_errno2("unlink", logmsg_path);
6135 free(logmsg_path);
6136 free(logmsg);
6137 free(orig_logmsg);
6138 free(editor);
6139 if (commit)
6140 got_object_commit_close(commit);
6141 return err;
6144 static const struct got_error *
6145 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6146 FILE *f, struct got_repository *repo)
6148 const struct got_error *err = NULL;
6149 char *line = NULL, *p, *end;
6150 size_t size;
6151 ssize_t len;
6152 int lineno = 0, i;
6153 const struct got_histedit_cmd *cmd;
6154 struct got_object_id *commit_id = NULL;
6155 struct got_histedit_list_entry *hle = NULL;
6157 for (;;) {
6158 len = getline(&line, &size, f);
6159 if (len == -1) {
6160 const struct got_error *getline_err;
6161 if (feof(f))
6162 break;
6163 getline_err = got_error_from_errno("getline");
6164 err = got_ferror(f, getline_err->code);
6165 break;
6167 lineno++;
6168 p = line;
6169 while (isspace((unsigned char)p[0]))
6170 p++;
6171 if (p[0] == '#' || p[0] == '\0') {
6172 free(line);
6173 line = NULL;
6174 continue;
6176 cmd = NULL;
6177 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6178 cmd = &got_histedit_cmds[i];
6179 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6180 isspace((unsigned char)p[strlen(cmd->name)])) {
6181 p += strlen(cmd->name);
6182 break;
6184 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6185 p++;
6186 break;
6189 if (i == nitems(got_histedit_cmds)) {
6190 err = histedit_syntax_error(lineno);
6191 break;
6193 while (isspace((unsigned char)p[0]))
6194 p++;
6195 if (cmd->code == GOT_HISTEDIT_MESG) {
6196 if (hle == NULL || hle->logmsg != NULL) {
6197 err = got_error(GOT_ERR_HISTEDIT_CMD);
6198 break;
6200 if (p[0] == '\0') {
6201 err = histedit_edit_logmsg(hle, repo);
6202 if (err)
6203 break;
6204 } else {
6205 hle->logmsg = strdup(p);
6206 if (hle->logmsg == NULL) {
6207 err = got_error_from_errno("strdup");
6208 break;
6211 free(line);
6212 line = NULL;
6213 continue;
6214 } else {
6215 end = p;
6216 while (end[0] && !isspace((unsigned char)end[0]))
6217 end++;
6218 *end = '\0';
6220 err = got_object_resolve_id_str(&commit_id, repo, p);
6221 if (err) {
6222 /* override error code */
6223 err = histedit_syntax_error(lineno);
6224 break;
6227 hle = malloc(sizeof(*hle));
6228 if (hle == NULL) {
6229 err = got_error_from_errno("malloc");
6230 break;
6232 hle->cmd = cmd;
6233 hle->commit_id = commit_id;
6234 hle->logmsg = NULL;
6235 commit_id = NULL;
6236 free(line);
6237 line = NULL;
6238 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6241 free(line);
6242 free(commit_id);
6243 return err;
6246 static const struct got_error *
6247 histedit_check_script(struct got_histedit_list *histedit_cmds,
6248 struct got_object_id_queue *commits, struct got_repository *repo)
6250 const struct got_error *err = NULL;
6251 struct got_object_qid *qid;
6252 struct got_histedit_list_entry *hle;
6253 static char msg[92];
6254 char *id_str;
6256 if (TAILQ_EMPTY(histedit_cmds))
6257 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6258 "histedit script contains no commands");
6259 if (SIMPLEQ_EMPTY(commits))
6260 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6262 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6263 struct got_histedit_list_entry *hle2;
6264 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6265 if (hle == hle2)
6266 continue;
6267 if (got_object_id_cmp(hle->commit_id,
6268 hle2->commit_id) != 0)
6269 continue;
6270 err = got_object_id_str(&id_str, hle->commit_id);
6271 if (err)
6272 return err;
6273 snprintf(msg, sizeof(msg), "commit %s is listed "
6274 "more than once in histedit script", id_str);
6275 free(id_str);
6276 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6280 SIMPLEQ_FOREACH(qid, commits, entry) {
6281 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6282 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6283 break;
6285 if (hle == NULL) {
6286 err = got_object_id_str(&id_str, qid->id);
6287 if (err)
6288 return err;
6289 snprintf(msg, sizeof(msg),
6290 "commit %s missing from histedit script", id_str);
6291 free(id_str);
6292 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6296 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6297 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6298 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6299 "last commit in histedit script cannot be folded");
6301 return NULL;
6304 static const struct got_error *
6305 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6306 const char *path, struct got_object_id_queue *commits,
6307 struct got_repository *repo)
6309 const struct got_error *err = NULL;
6310 char *editor;
6311 FILE *f = NULL;
6313 err = get_editor(&editor);
6314 if (err)
6315 return err;
6317 if (spawn_editor(editor, path) == -1) {
6318 err = got_error_from_errno("failed spawning editor");
6319 goto done;
6322 f = fopen(path, "r");
6323 if (f == NULL) {
6324 err = got_error_from_errno("fopen");
6325 goto done;
6327 err = histedit_parse_list(histedit_cmds, f, repo);
6328 if (err)
6329 goto done;
6331 err = histedit_check_script(histedit_cmds, commits, repo);
6332 done:
6333 if (f && fclose(f) != 0 && err == NULL)
6334 err = got_error_from_errno("fclose");
6335 free(editor);
6336 return err;
6339 static const struct got_error *
6340 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6341 struct got_object_id_queue *, const char *, const char *,
6342 struct got_repository *);
6344 static const struct got_error *
6345 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6346 struct got_object_id_queue *commits, const char *branch_name,
6347 int edit_logmsg_only, struct got_repository *repo)
6349 const struct got_error *err;
6350 FILE *f = NULL;
6351 char *path = NULL;
6353 err = got_opentemp_named(&path, &f, "got-histedit");
6354 if (err)
6355 return err;
6357 err = write_cmd_list(f, branch_name, commits);
6358 if (err)
6359 goto done;
6361 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6362 if (err)
6363 goto done;
6365 if (edit_logmsg_only) {
6366 rewind(f);
6367 err = histedit_parse_list(histedit_cmds, f, repo);
6368 } else {
6369 if (fclose(f) != 0) {
6370 err = got_error_from_errno("fclose");
6371 goto done;
6373 f = NULL;
6374 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6375 if (err) {
6376 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6377 err->code != GOT_ERR_HISTEDIT_CMD)
6378 goto done;
6379 err = histedit_edit_list_retry(histedit_cmds, err,
6380 commits, path, branch_name, repo);
6383 done:
6384 if (f && fclose(f) != 0 && err == NULL)
6385 err = got_error_from_errno("fclose");
6386 if (path && unlink(path) != 0 && err == NULL)
6387 err = got_error_from_errno2("unlink", path);
6388 free(path);
6389 return err;
6392 static const struct got_error *
6393 histedit_save_list(struct got_histedit_list *histedit_cmds,
6394 struct got_worktree *worktree, struct got_repository *repo)
6396 const struct got_error *err = NULL;
6397 char *path = NULL;
6398 FILE *f = NULL;
6399 struct got_histedit_list_entry *hle;
6400 struct got_commit_object *commit = NULL;
6402 err = got_worktree_get_histedit_script_path(&path, worktree);
6403 if (err)
6404 return err;
6406 f = fopen(path, "w");
6407 if (f == NULL) {
6408 err = got_error_from_errno2("fopen", path);
6409 goto done;
6411 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6412 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6413 repo);
6414 if (err)
6415 break;
6417 if (hle->logmsg) {
6418 int n = fprintf(f, "%c %s\n",
6419 GOT_HISTEDIT_MESG, hle->logmsg);
6420 if (n < 0) {
6421 err = got_ferror(f, GOT_ERR_IO);
6422 break;
6426 done:
6427 if (f && fclose(f) != 0 && err == NULL)
6428 err = got_error_from_errno("fclose");
6429 free(path);
6430 if (commit)
6431 got_object_commit_close(commit);
6432 return err;
6435 void
6436 histedit_free_list(struct got_histedit_list *histedit_cmds)
6438 struct got_histedit_list_entry *hle;
6440 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6441 TAILQ_REMOVE(histedit_cmds, hle, entry);
6442 free(hle);
6446 static const struct got_error *
6447 histedit_load_list(struct got_histedit_list *histedit_cmds,
6448 const char *path, struct got_repository *repo)
6450 const struct got_error *err = NULL;
6451 FILE *f = NULL;
6453 f = fopen(path, "r");
6454 if (f == NULL) {
6455 err = got_error_from_errno2("fopen", path);
6456 goto done;
6459 err = histedit_parse_list(histedit_cmds, f, repo);
6460 done:
6461 if (f && fclose(f) != 0 && err == NULL)
6462 err = got_error_from_errno("fclose");
6463 return err;
6466 static const struct got_error *
6467 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6468 const struct got_error *edit_err, struct got_object_id_queue *commits,
6469 const char *path, const char *branch_name, struct got_repository *repo)
6471 const struct got_error *err = NULL, *prev_err = edit_err;
6472 int resp = ' ';
6474 while (resp != 'c' && resp != 'r' && resp != 'a') {
6475 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6476 "or (a)bort: ", getprogname(), prev_err->msg);
6477 resp = getchar();
6478 if (resp == '\n')
6479 resp = getchar();
6480 if (resp == 'c') {
6481 histedit_free_list(histedit_cmds);
6482 err = histedit_run_editor(histedit_cmds, path, commits,
6483 repo);
6484 if (err) {
6485 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6486 err->code != GOT_ERR_HISTEDIT_CMD)
6487 break;
6488 prev_err = err;
6489 resp = ' ';
6490 continue;
6492 break;
6493 } else if (resp == 'r') {
6494 histedit_free_list(histedit_cmds);
6495 err = histedit_edit_script(histedit_cmds,
6496 commits, branch_name, 0, repo);
6497 if (err) {
6498 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6499 err->code != GOT_ERR_HISTEDIT_CMD)
6500 break;
6501 prev_err = err;
6502 resp = ' ';
6503 continue;
6505 break;
6506 } else if (resp == 'a') {
6507 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6508 break;
6509 } else
6510 printf("invalid response '%c'\n", resp);
6513 return err;
6516 static const struct got_error *
6517 histedit_complete(struct got_worktree *worktree,
6518 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 struct got_reference *branch, struct got_repository *repo)
6521 printf("Switching work tree to %s\n",
6522 got_ref_get_symref_target(branch));
6523 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6524 branch, repo);
6527 static const struct got_error *
6528 show_histedit_progress(struct got_commit_object *commit,
6529 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6531 const struct got_error *err;
6532 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6534 err = got_object_id_str(&old_id_str, hle->commit_id);
6535 if (err)
6536 goto done;
6538 if (new_id) {
6539 err = got_object_id_str(&new_id_str, new_id);
6540 if (err)
6541 goto done;
6544 old_id_str[12] = '\0';
6545 if (new_id_str)
6546 new_id_str[12] = '\0';
6548 if (hle->logmsg) {
6549 logmsg = strdup(hle->logmsg);
6550 if (logmsg == NULL) {
6551 err = got_error_from_errno("strdup");
6552 goto done;
6554 trim_logmsg(logmsg, 42);
6555 } else {
6556 err = get_short_logmsg(&logmsg, 42, commit);
6557 if (err)
6558 goto done;
6561 switch (hle->cmd->code) {
6562 case GOT_HISTEDIT_PICK:
6563 case GOT_HISTEDIT_EDIT:
6564 printf("%s -> %s: %s\n", old_id_str,
6565 new_id_str ? new_id_str : "no-op change", logmsg);
6566 break;
6567 case GOT_HISTEDIT_DROP:
6568 case GOT_HISTEDIT_FOLD:
6569 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6570 logmsg);
6571 break;
6572 default:
6573 break;
6575 done:
6576 free(old_id_str);
6577 free(new_id_str);
6578 return err;
6581 static const struct got_error *
6582 histedit_commit(struct got_pathlist_head *merged_paths,
6583 struct got_worktree *worktree, struct got_fileindex *fileindex,
6584 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6585 struct got_repository *repo)
6587 const struct got_error *err;
6588 struct got_commit_object *commit;
6589 struct got_object_id *new_commit_id;
6591 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6592 && hle->logmsg == NULL) {
6593 err = histedit_edit_logmsg(hle, repo);
6594 if (err)
6595 return err;
6598 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6599 if (err)
6600 return err;
6602 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6603 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6604 hle->logmsg, repo);
6605 if (err) {
6606 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6607 goto done;
6608 err = show_histedit_progress(commit, hle, NULL);
6609 } else {
6610 err = show_histedit_progress(commit, hle, new_commit_id);
6611 free(new_commit_id);
6613 done:
6614 got_object_commit_close(commit);
6615 return err;
6618 static const struct got_error *
6619 histedit_skip_commit(struct got_histedit_list_entry *hle,
6620 struct got_worktree *worktree, struct got_repository *repo)
6622 const struct got_error *error;
6623 struct got_commit_object *commit;
6625 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6626 repo);
6627 if (error)
6628 return error;
6630 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6631 if (error)
6632 return error;
6634 error = show_histedit_progress(commit, hle, NULL);
6635 got_object_commit_close(commit);
6636 return error;
6639 static const struct got_error *
6640 check_local_changes(void *arg, unsigned char status,
6641 unsigned char staged_status, const char *path,
6642 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6643 struct got_object_id *commit_id, int dirfd, const char *de_name)
6645 int *have_local_changes = arg;
6647 switch (status) {
6648 case GOT_STATUS_ADD:
6649 case GOT_STATUS_DELETE:
6650 case GOT_STATUS_MODIFY:
6651 case GOT_STATUS_CONFLICT:
6652 *have_local_changes = 1;
6653 return got_error(GOT_ERR_CANCELLED);
6654 default:
6655 break;
6658 switch (staged_status) {
6659 case GOT_STATUS_ADD:
6660 case GOT_STATUS_DELETE:
6661 case GOT_STATUS_MODIFY:
6662 *have_local_changes = 1;
6663 return got_error(GOT_ERR_CANCELLED);
6664 default:
6665 break;
6668 return NULL;
6671 static const struct got_error *
6672 cmd_histedit(int argc, char *argv[])
6674 const struct got_error *error = NULL;
6675 struct got_worktree *worktree = NULL;
6676 struct got_fileindex *fileindex = NULL;
6677 struct got_repository *repo = NULL;
6678 char *cwd = NULL;
6679 struct got_reference *branch = NULL;
6680 struct got_reference *tmp_branch = NULL;
6681 struct got_object_id *resume_commit_id = NULL;
6682 struct got_object_id *base_commit_id = NULL;
6683 struct got_object_id *head_commit_id = NULL;
6684 struct got_commit_object *commit = NULL;
6685 int ch, rebase_in_progress = 0, did_something;
6686 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6687 int edit_logmsg_only = 0;
6688 const char *edit_script_path = NULL;
6689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 struct got_object_id_queue commits;
6691 struct got_pathlist_head merged_paths;
6692 const struct got_object_id_queue *parent_ids;
6693 struct got_object_qid *pid;
6694 struct got_histedit_list histedit_cmds;
6695 struct got_histedit_list_entry *hle;
6697 SIMPLEQ_INIT(&commits);
6698 TAILQ_INIT(&histedit_cmds);
6699 TAILQ_INIT(&merged_paths);
6701 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6702 switch (ch) {
6703 case 'a':
6704 abort_edit = 1;
6705 break;
6706 case 'c':
6707 continue_edit = 1;
6708 break;
6709 case 'F':
6710 edit_script_path = optarg;
6711 break;
6712 case 'm':
6713 edit_logmsg_only = 1;
6714 break;
6715 default:
6716 usage_histedit();
6717 /* NOTREACHED */
6721 argc -= optind;
6722 argv += optind;
6724 #ifndef PROFILE
6725 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6726 "unveil", NULL) == -1)
6727 err(1, "pledge");
6728 #endif
6729 if (abort_edit && continue_edit)
6730 errx(1, "histedit's -a and -c options are mutually exclusive");
6731 if (edit_script_path && edit_logmsg_only)
6732 errx(1, "histedit's -F and -m options are mutually exclusive");
6733 if (abort_edit && edit_logmsg_only)
6734 errx(1, "histedit's -a and -m options are mutually exclusive");
6735 if (continue_edit && edit_logmsg_only)
6736 errx(1, "histedit's -c and -m options are mutually exclusive");
6737 if (argc != 0)
6738 usage_histedit();
6741 * This command cannot apply unveil(2) in all cases because the
6742 * user may choose to run an editor to edit the histedit script
6743 * and to edit individual commit log messages.
6744 * unveil(2) traverses exec(2); if an editor is used we have to
6745 * apply unveil after edit script and log messages have been written.
6746 * XXX TODO: Make use of unveil(2) where possible.
6749 cwd = getcwd(NULL, 0);
6750 if (cwd == NULL) {
6751 error = got_error_from_errno("getcwd");
6752 goto done;
6754 error = got_worktree_open(&worktree, cwd);
6755 if (error)
6756 goto done;
6758 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6759 NULL);
6760 if (error != NULL)
6761 goto done;
6763 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6764 if (error)
6765 goto done;
6766 if (rebase_in_progress) {
6767 error = got_error(GOT_ERR_REBASING);
6768 goto done;
6771 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6772 if (error)
6773 goto done;
6775 if (edit_in_progress && edit_logmsg_only) {
6776 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6777 "histedit operation is in progress in this "
6778 "work tree and must be continued or aborted "
6779 "before the -m option can be used");
6780 goto done;
6783 if (edit_in_progress && abort_edit) {
6784 error = got_worktree_histedit_continue(&resume_commit_id,
6785 &tmp_branch, &branch, &base_commit_id, &fileindex,
6786 worktree, repo);
6787 if (error)
6788 goto done;
6789 printf("Switching work tree to %s\n",
6790 got_ref_get_symref_target(branch));
6791 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6792 branch, base_commit_id, update_progress, &did_something);
6793 if (error)
6794 goto done;
6795 printf("Histedit of %s aborted\n",
6796 got_ref_get_symref_target(branch));
6797 goto done; /* nothing else to do */
6798 } else if (abort_edit) {
6799 error = got_error(GOT_ERR_NOT_HISTEDIT);
6800 goto done;
6803 if (continue_edit) {
6804 char *path;
6806 if (!edit_in_progress) {
6807 error = got_error(GOT_ERR_NOT_HISTEDIT);
6808 goto done;
6811 error = got_worktree_get_histedit_script_path(&path, worktree);
6812 if (error)
6813 goto done;
6815 error = histedit_load_list(&histedit_cmds, path, repo);
6816 free(path);
6817 if (error)
6818 goto done;
6820 error = got_worktree_histedit_continue(&resume_commit_id,
6821 &tmp_branch, &branch, &base_commit_id, &fileindex,
6822 worktree, repo);
6823 if (error)
6824 goto done;
6826 error = got_ref_resolve(&head_commit_id, repo, branch);
6827 if (error)
6828 goto done;
6830 error = got_object_open_as_commit(&commit, repo,
6831 head_commit_id);
6832 if (error)
6833 goto done;
6834 parent_ids = got_object_commit_get_parent_ids(commit);
6835 pid = SIMPLEQ_FIRST(parent_ids);
6836 if (pid == NULL) {
6837 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6838 goto done;
6840 error = collect_commits(&commits, head_commit_id, pid->id,
6841 base_commit_id, got_worktree_get_path_prefix(worktree),
6842 GOT_ERR_HISTEDIT_PATH, repo);
6843 got_object_commit_close(commit);
6844 commit = NULL;
6845 if (error)
6846 goto done;
6847 } else {
6848 if (edit_in_progress) {
6849 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6850 goto done;
6853 error = got_ref_open(&branch, repo,
6854 got_worktree_get_head_ref_name(worktree), 0);
6855 if (error != NULL)
6856 goto done;
6858 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6859 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6860 "will not edit commit history of a branch outside "
6861 "the \"refs/heads/\" reference namespace");
6862 goto done;
6865 error = got_ref_resolve(&head_commit_id, repo, branch);
6866 got_ref_close(branch);
6867 branch = NULL;
6868 if (error)
6869 goto done;
6871 error = got_object_open_as_commit(&commit, repo,
6872 head_commit_id);
6873 if (error)
6874 goto done;
6875 parent_ids = got_object_commit_get_parent_ids(commit);
6876 pid = SIMPLEQ_FIRST(parent_ids);
6877 if (pid == NULL) {
6878 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6879 goto done;
6881 error = collect_commits(&commits, head_commit_id, pid->id,
6882 got_worktree_get_base_commit_id(worktree),
6883 got_worktree_get_path_prefix(worktree),
6884 GOT_ERR_HISTEDIT_PATH, repo);
6885 got_object_commit_close(commit);
6886 commit = NULL;
6887 if (error)
6888 goto done;
6890 if (SIMPLEQ_EMPTY(&commits)) {
6891 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6892 goto done;
6895 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6896 &base_commit_id, &fileindex, worktree, repo);
6897 if (error)
6898 goto done;
6900 if (edit_script_path) {
6901 error = histedit_load_list(&histedit_cmds,
6902 edit_script_path, repo);
6903 if (error) {
6904 got_worktree_histedit_abort(worktree, fileindex,
6905 repo, branch, base_commit_id,
6906 update_progress, &did_something);
6907 goto done;
6909 } else {
6910 const char *branch_name;
6911 branch_name = got_ref_get_symref_target(branch);
6912 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6913 branch_name += 11;
6914 error = histedit_edit_script(&histedit_cmds, &commits,
6915 branch_name, edit_logmsg_only, repo);
6916 if (error) {
6917 got_worktree_histedit_abort(worktree, fileindex,
6918 repo, branch, base_commit_id,
6919 update_progress, &did_something);
6920 goto done;
6925 error = histedit_save_list(&histedit_cmds, worktree,
6926 repo);
6927 if (error) {
6928 got_worktree_histedit_abort(worktree, fileindex,
6929 repo, branch, base_commit_id,
6930 update_progress, &did_something);
6931 goto done;
6936 error = histedit_check_script(&histedit_cmds, &commits, repo);
6937 if (error)
6938 goto done;
6940 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6941 if (resume_commit_id) {
6942 if (got_object_id_cmp(hle->commit_id,
6943 resume_commit_id) != 0)
6944 continue;
6946 resume_commit_id = NULL;
6947 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6948 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6949 error = histedit_skip_commit(hle, worktree,
6950 repo);
6951 if (error)
6952 goto done;
6953 } else {
6954 struct got_pathlist_head paths;
6955 int have_changes = 0;
6957 TAILQ_INIT(&paths);
6958 error = got_pathlist_append(&paths, "", NULL);
6959 if (error)
6960 goto done;
6961 error = got_worktree_status(worktree, &paths,
6962 repo, check_local_changes, &have_changes,
6963 check_cancelled, NULL);
6964 got_pathlist_free(&paths);
6965 if (error) {
6966 if (error->code != GOT_ERR_CANCELLED)
6967 goto done;
6968 if (sigint_received || sigpipe_received)
6969 goto done;
6971 if (have_changes) {
6972 error = histedit_commit(NULL, worktree,
6973 fileindex, tmp_branch, hle, repo);
6974 if (error)
6975 goto done;
6976 } else {
6977 error = got_object_open_as_commit(
6978 &commit, repo, hle->commit_id);
6979 if (error)
6980 goto done;
6981 error = show_histedit_progress(commit,
6982 hle, NULL);
6983 got_object_commit_close(commit);
6984 commit = NULL;
6985 if (error)
6986 goto done;
6989 continue;
6992 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6993 error = histedit_skip_commit(hle, worktree, repo);
6994 if (error)
6995 goto done;
6996 continue;
6999 error = got_object_open_as_commit(&commit, repo,
7000 hle->commit_id);
7001 if (error)
7002 goto done;
7003 parent_ids = got_object_commit_get_parent_ids(commit);
7004 pid = SIMPLEQ_FIRST(parent_ids);
7006 error = got_worktree_histedit_merge_files(&merged_paths,
7007 worktree, fileindex, pid->id, hle->commit_id, repo,
7008 rebase_progress, &rebase_status, check_cancelled, NULL);
7009 if (error)
7010 goto done;
7011 got_object_commit_close(commit);
7012 commit = NULL;
7014 if (rebase_status == GOT_STATUS_CONFLICT) {
7015 error = show_rebase_merge_conflict(hle->commit_id,
7016 repo);
7017 if (error)
7018 goto done;
7019 got_worktree_rebase_pathlist_free(&merged_paths);
7020 break;
7023 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7024 char *id_str;
7025 error = got_object_id_str(&id_str, hle->commit_id);
7026 if (error)
7027 goto done;
7028 printf("Stopping histedit for amending commit %s\n",
7029 id_str);
7030 free(id_str);
7031 got_worktree_rebase_pathlist_free(&merged_paths);
7032 error = got_worktree_histedit_postpone(worktree,
7033 fileindex);
7034 goto done;
7037 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7038 error = histedit_skip_commit(hle, worktree, repo);
7039 if (error)
7040 goto done;
7041 continue;
7044 error = histedit_commit(&merged_paths, worktree, fileindex,
7045 tmp_branch, hle, repo);
7046 got_worktree_rebase_pathlist_free(&merged_paths);
7047 if (error)
7048 goto done;
7051 if (rebase_status == GOT_STATUS_CONFLICT) {
7052 error = got_worktree_histedit_postpone(worktree, fileindex);
7053 if (error)
7054 goto done;
7055 error = got_error_msg(GOT_ERR_CONFLICTS,
7056 "conflicts must be resolved before histedit can continue");
7057 } else
7058 error = histedit_complete(worktree, fileindex, tmp_branch,
7059 branch, repo);
7060 done:
7061 got_object_id_queue_free(&commits);
7062 histedit_free_list(&histedit_cmds);
7063 free(head_commit_id);
7064 free(base_commit_id);
7065 free(resume_commit_id);
7066 if (commit)
7067 got_object_commit_close(commit);
7068 if (branch)
7069 got_ref_close(branch);
7070 if (tmp_branch)
7071 got_ref_close(tmp_branch);
7072 if (worktree)
7073 got_worktree_close(worktree);
7074 if (repo)
7075 got_repo_close(repo);
7076 return error;
7079 __dead static void
7080 usage_integrate(void)
7082 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7083 exit(1);
7086 static const struct got_error *
7087 cmd_integrate(int argc, char *argv[])
7089 const struct got_error *error = NULL;
7090 struct got_repository *repo = NULL;
7091 struct got_worktree *worktree = NULL;
7092 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7093 const char *branch_arg = NULL;
7094 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7095 struct got_fileindex *fileindex = NULL;
7096 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7097 int ch, did_something = 0;
7099 while ((ch = getopt(argc, argv, "")) != -1) {
7100 switch (ch) {
7101 default:
7102 usage_integrate();
7103 /* NOTREACHED */
7107 argc -= optind;
7108 argv += optind;
7110 if (argc != 1)
7111 usage_integrate();
7112 branch_arg = argv[0];
7114 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7115 "unveil", NULL) == -1)
7116 err(1, "pledge");
7118 cwd = getcwd(NULL, 0);
7119 if (cwd == NULL) {
7120 error = got_error_from_errno("getcwd");
7121 goto done;
7124 error = got_worktree_open(&worktree, cwd);
7125 if (error)
7126 goto done;
7128 error = check_rebase_or_histedit_in_progress(worktree);
7129 if (error)
7130 goto done;
7132 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7133 NULL);
7134 if (error != NULL)
7135 goto done;
7137 error = apply_unveil(got_repo_get_path(repo), 0,
7138 got_worktree_get_root_path(worktree));
7139 if (error)
7140 goto done;
7142 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7143 error = got_error_from_errno("asprintf");
7144 goto done;
7147 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7148 &base_branch_ref, worktree, refname, repo);
7149 if (error)
7150 goto done;
7152 refname = strdup(got_ref_get_name(branch_ref));
7153 if (refname == NULL) {
7154 error = got_error_from_errno("strdup");
7155 got_worktree_integrate_abort(worktree, fileindex, repo,
7156 branch_ref, base_branch_ref);
7157 goto done;
7159 base_refname = strdup(got_ref_get_name(base_branch_ref));
7160 if (base_refname == NULL) {
7161 error = got_error_from_errno("strdup");
7162 got_worktree_integrate_abort(worktree, fileindex, repo,
7163 branch_ref, base_branch_ref);
7164 goto done;
7167 error = got_ref_resolve(&commit_id, repo, branch_ref);
7168 if (error)
7169 goto done;
7171 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7172 if (error)
7173 goto done;
7175 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7176 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7177 "specified branch has already been integrated");
7178 got_worktree_integrate_abort(worktree, fileindex, repo,
7179 branch_ref, base_branch_ref);
7180 goto done;
7183 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7184 if (error) {
7185 if (error->code == GOT_ERR_ANCESTRY)
7186 error = got_error(GOT_ERR_REBASE_REQUIRED);
7187 got_worktree_integrate_abort(worktree, fileindex, repo,
7188 branch_ref, base_branch_ref);
7189 goto done;
7192 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7193 branch_ref, base_branch_ref, update_progress, &did_something,
7194 check_cancelled, NULL);
7195 if (error)
7196 goto done;
7198 printf("Integrated %s into %s\n", refname, base_refname);
7199 done:
7200 if (repo)
7201 got_repo_close(repo);
7202 if (worktree)
7203 got_worktree_close(worktree);
7204 free(cwd);
7205 free(base_commit_id);
7206 free(commit_id);
7207 free(refname);
7208 free(base_refname);
7209 return error;
7212 __dead static void
7213 usage_stage(void)
7215 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7216 "[file-path ...]\n",
7217 getprogname());
7218 exit(1);
7221 static const struct got_error *
7222 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7223 const char *path, struct got_object_id *blob_id,
7224 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7225 int dirfd, const char *de_name)
7227 const struct got_error *err = NULL;
7228 char *id_str = NULL;
7230 if (staged_status != GOT_STATUS_ADD &&
7231 staged_status != GOT_STATUS_MODIFY &&
7232 staged_status != GOT_STATUS_DELETE)
7233 return NULL;
7235 if (staged_status == GOT_STATUS_ADD ||
7236 staged_status == GOT_STATUS_MODIFY)
7237 err = got_object_id_str(&id_str, staged_blob_id);
7238 else
7239 err = got_object_id_str(&id_str, blob_id);
7240 if (err)
7241 return err;
7243 printf("%s %c %s\n", id_str, staged_status, path);
7244 free(id_str);
7245 return NULL;
7248 static const struct got_error *
7249 cmd_stage(int argc, char *argv[])
7251 const struct got_error *error = NULL;
7252 struct got_repository *repo = NULL;
7253 struct got_worktree *worktree = NULL;
7254 char *cwd = NULL;
7255 struct got_pathlist_head paths;
7256 struct got_pathlist_entry *pe;
7257 int ch, list_stage = 0, pflag = 0;
7258 FILE *patch_script_file = NULL;
7259 const char *patch_script_path = NULL;
7260 struct choose_patch_arg cpa;
7262 TAILQ_INIT(&paths);
7264 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7265 switch (ch) {
7266 case 'l':
7267 list_stage = 1;
7268 break;
7269 case 'p':
7270 pflag = 1;
7271 break;
7272 case 'F':
7273 patch_script_path = optarg;
7274 break;
7275 default:
7276 usage_stage();
7277 /* NOTREACHED */
7281 argc -= optind;
7282 argv += optind;
7284 #ifndef PROFILE
7285 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7286 "unveil", NULL) == -1)
7287 err(1, "pledge");
7288 #endif
7289 if (list_stage && (pflag || patch_script_path))
7290 errx(1, "-l option cannot be used with other options");
7291 if (patch_script_path && !pflag)
7292 errx(1, "-F option can only be used together with -p option");
7294 cwd = getcwd(NULL, 0);
7295 if (cwd == NULL) {
7296 error = got_error_from_errno("getcwd");
7297 goto done;
7300 error = got_worktree_open(&worktree, cwd);
7301 if (error)
7302 goto done;
7304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7305 NULL);
7306 if (error != NULL)
7307 goto done;
7309 if (patch_script_path) {
7310 patch_script_file = fopen(patch_script_path, "r");
7311 if (patch_script_file == NULL) {
7312 error = got_error_from_errno2("fopen",
7313 patch_script_path);
7314 goto done;
7317 error = apply_unveil(got_repo_get_path(repo), 0,
7318 got_worktree_get_root_path(worktree));
7319 if (error)
7320 goto done;
7322 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7323 if (error)
7324 goto done;
7326 if (list_stage)
7327 error = got_worktree_status(worktree, &paths, repo,
7328 print_stage, NULL, check_cancelled, NULL);
7329 else {
7330 cpa.patch_script_file = patch_script_file;
7331 cpa.action = "stage";
7332 error = got_worktree_stage(worktree, &paths,
7333 pflag ? NULL : print_status, NULL,
7334 pflag ? choose_patch : NULL, &cpa, repo);
7336 done:
7337 if (patch_script_file && fclose(patch_script_file) == EOF &&
7338 error == NULL)
7339 error = got_error_from_errno2("fclose", patch_script_path);
7340 if (repo)
7341 got_repo_close(repo);
7342 if (worktree)
7343 got_worktree_close(worktree);
7344 TAILQ_FOREACH(pe, &paths, entry)
7345 free((char *)pe->path);
7346 got_pathlist_free(&paths);
7347 free(cwd);
7348 return error;
7351 __dead static void
7352 usage_unstage(void)
7354 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7355 "[file-path ...]\n",
7356 getprogname());
7357 exit(1);
7361 static const struct got_error *
7362 cmd_unstage(int argc, char *argv[])
7364 const struct got_error *error = NULL;
7365 struct got_repository *repo = NULL;
7366 struct got_worktree *worktree = NULL;
7367 char *cwd = NULL;
7368 struct got_pathlist_head paths;
7369 struct got_pathlist_entry *pe;
7370 int ch, did_something = 0, pflag = 0;
7371 FILE *patch_script_file = NULL;
7372 const char *patch_script_path = NULL;
7373 struct choose_patch_arg cpa;
7375 TAILQ_INIT(&paths);
7377 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7378 switch (ch) {
7379 case 'p':
7380 pflag = 1;
7381 break;
7382 case 'F':
7383 patch_script_path = optarg;
7384 break;
7385 default:
7386 usage_unstage();
7387 /* NOTREACHED */
7391 argc -= optind;
7392 argv += optind;
7394 #ifndef PROFILE
7395 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7396 "unveil", NULL) == -1)
7397 err(1, "pledge");
7398 #endif
7399 if (patch_script_path && !pflag)
7400 errx(1, "-F option can only be used together with -p option");
7402 cwd = getcwd(NULL, 0);
7403 if (cwd == NULL) {
7404 error = got_error_from_errno("getcwd");
7405 goto done;
7408 error = got_worktree_open(&worktree, cwd);
7409 if (error)
7410 goto done;
7412 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7413 NULL);
7414 if (error != NULL)
7415 goto done;
7417 if (patch_script_path) {
7418 patch_script_file = fopen(patch_script_path, "r");
7419 if (patch_script_file == NULL) {
7420 error = got_error_from_errno2("fopen",
7421 patch_script_path);
7422 goto done;
7426 error = apply_unveil(got_repo_get_path(repo), 0,
7427 got_worktree_get_root_path(worktree));
7428 if (error)
7429 goto done;
7431 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7432 if (error)
7433 goto done;
7435 cpa.patch_script_file = patch_script_file;
7436 cpa.action = "unstage";
7437 error = got_worktree_unstage(worktree, &paths, update_progress,
7438 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7439 done:
7440 if (patch_script_file && fclose(patch_script_file) == EOF &&
7441 error == NULL)
7442 error = got_error_from_errno2("fclose", patch_script_path);
7443 if (repo)
7444 got_repo_close(repo);
7445 if (worktree)
7446 got_worktree_close(worktree);
7447 TAILQ_FOREACH(pe, &paths, entry)
7448 free((char *)pe->path);
7449 got_pathlist_free(&paths);
7450 free(cwd);
7451 return error;
7454 __dead static void
7455 usage_cat(void)
7457 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7458 "arg1 [arg2 ...]\n", getprogname());
7459 exit(1);
7462 static const struct got_error *
7463 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7465 const struct got_error *err;
7466 struct got_blob_object *blob;
7468 err = got_object_open_as_blob(&blob, repo, id, 8192);
7469 if (err)
7470 return err;
7472 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7473 got_object_blob_close(blob);
7474 return err;
7477 static const struct got_error *
7478 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7480 const struct got_error *err;
7481 struct got_tree_object *tree;
7482 int nentries, i;
7484 err = got_object_open_as_tree(&tree, repo, id);
7485 if (err)
7486 return err;
7488 nentries = got_object_tree_get_nentries(tree);
7489 for (i = 0; i < nentries; i++) {
7490 struct got_tree_entry *te;
7491 char *id_str;
7492 if (sigint_received || sigpipe_received)
7493 break;
7494 te = got_object_tree_get_entry(tree, i);
7495 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7496 if (err)
7497 break;
7498 fprintf(outfile, "%s %.7o %s\n", id_str,
7499 got_tree_entry_get_mode(te),
7500 got_tree_entry_get_name(te));
7501 free(id_str);
7504 got_object_tree_close(tree);
7505 return err;
7508 static const struct got_error *
7509 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7511 const struct got_error *err;
7512 struct got_commit_object *commit;
7513 const struct got_object_id_queue *parent_ids;
7514 struct got_object_qid *pid;
7515 char *id_str = NULL;
7516 const char *logmsg = NULL;
7518 err = got_object_open_as_commit(&commit, repo, id);
7519 if (err)
7520 return err;
7522 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7523 if (err)
7524 goto done;
7526 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7527 parent_ids = got_object_commit_get_parent_ids(commit);
7528 fprintf(outfile, "numparents %d\n",
7529 got_object_commit_get_nparents(commit));
7530 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7531 char *pid_str;
7532 err = got_object_id_str(&pid_str, pid->id);
7533 if (err)
7534 goto done;
7535 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7536 free(pid_str);
7538 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7539 got_object_commit_get_author(commit),
7540 got_object_commit_get_author_time(commit));
7542 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7543 got_object_commit_get_author(commit),
7544 got_object_commit_get_committer_time(commit));
7546 logmsg = got_object_commit_get_logmsg_raw(commit);
7547 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7548 fprintf(outfile, "%s", logmsg);
7549 done:
7550 free(id_str);
7551 got_object_commit_close(commit);
7552 return err;
7555 static const struct got_error *
7556 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7558 const struct got_error *err;
7559 struct got_tag_object *tag;
7560 char *id_str = NULL;
7561 const char *tagmsg = NULL;
7563 err = got_object_open_as_tag(&tag, repo, id);
7564 if (err)
7565 return err;
7567 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7568 if (err)
7569 goto done;
7571 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7573 switch (got_object_tag_get_object_type(tag)) {
7574 case GOT_OBJ_TYPE_BLOB:
7575 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7576 GOT_OBJ_LABEL_BLOB);
7577 break;
7578 case GOT_OBJ_TYPE_TREE:
7579 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7580 GOT_OBJ_LABEL_TREE);
7581 break;
7582 case GOT_OBJ_TYPE_COMMIT:
7583 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7584 GOT_OBJ_LABEL_COMMIT);
7585 break;
7586 case GOT_OBJ_TYPE_TAG:
7587 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7588 GOT_OBJ_LABEL_TAG);
7589 break;
7590 default:
7591 break;
7594 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7595 got_object_tag_get_name(tag));
7597 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7598 got_object_tag_get_tagger(tag),
7599 got_object_tag_get_tagger_time(tag));
7601 tagmsg = got_object_tag_get_message(tag);
7602 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7603 fprintf(outfile, "%s", tagmsg);
7604 done:
7605 free(id_str);
7606 got_object_tag_close(tag);
7607 return err;
7610 static const struct got_error *
7611 cmd_cat(int argc, char *argv[])
7613 const struct got_error *error;
7614 struct got_repository *repo = NULL;
7615 struct got_worktree *worktree = NULL;
7616 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7617 const char *commit_id_str = NULL;
7618 struct got_object_id *id = NULL, *commit_id = NULL;
7619 int ch, obj_type, i, force_path = 0;
7621 #ifndef PROFILE
7622 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7623 NULL) == -1)
7624 err(1, "pledge");
7625 #endif
7627 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7628 switch (ch) {
7629 case 'c':
7630 commit_id_str = optarg;
7631 break;
7632 case 'r':
7633 repo_path = realpath(optarg, NULL);
7634 if (repo_path == NULL)
7635 return got_error_from_errno2("realpath",
7636 optarg);
7637 got_path_strip_trailing_slashes(repo_path);
7638 break;
7639 case 'P':
7640 force_path = 1;
7641 break;
7642 default:
7643 usage_cat();
7644 /* NOTREACHED */
7648 argc -= optind;
7649 argv += optind;
7651 cwd = getcwd(NULL, 0);
7652 if (cwd == NULL) {
7653 error = got_error_from_errno("getcwd");
7654 goto done;
7656 error = got_worktree_open(&worktree, cwd);
7657 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7658 goto done;
7659 if (worktree) {
7660 if (repo_path == NULL) {
7661 repo_path = strdup(
7662 got_worktree_get_repo_path(worktree));
7663 if (repo_path == NULL) {
7664 error = got_error_from_errno("strdup");
7665 goto done;
7670 if (repo_path == NULL) {
7671 repo_path = getcwd(NULL, 0);
7672 if (repo_path == NULL)
7673 return got_error_from_errno("getcwd");
7676 error = got_repo_open(&repo, repo_path, NULL);
7677 free(repo_path);
7678 if (error != NULL)
7679 goto done;
7681 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7682 if (error)
7683 goto done;
7685 if (commit_id_str == NULL)
7686 commit_id_str = GOT_REF_HEAD;
7687 error = got_repo_match_object_id(&commit_id, NULL,
7688 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7689 if (error)
7690 goto done;
7692 for (i = 0; i < argc; i++) {
7693 if (force_path) {
7694 error = got_object_id_by_path(&id, repo, commit_id,
7695 argv[i]);
7696 if (error)
7697 break;
7698 } else {
7699 error = got_repo_match_object_id(&id, &label, argv[i],
7700 GOT_OBJ_TYPE_ANY, 0, repo);
7701 if (error) {
7702 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7703 error->code != GOT_ERR_NOT_REF)
7704 break;
7705 error = got_object_id_by_path(&id, repo,
7706 commit_id, argv[i]);
7707 if (error)
7708 break;
7712 error = got_object_get_type(&obj_type, repo, id);
7713 if (error)
7714 break;
7716 switch (obj_type) {
7717 case GOT_OBJ_TYPE_BLOB:
7718 error = cat_blob(id, repo, stdout);
7719 break;
7720 case GOT_OBJ_TYPE_TREE:
7721 error = cat_tree(id, repo, stdout);
7722 break;
7723 case GOT_OBJ_TYPE_COMMIT:
7724 error = cat_commit(id, repo, stdout);
7725 break;
7726 case GOT_OBJ_TYPE_TAG:
7727 error = cat_tag(id, repo, stdout);
7728 break;
7729 default:
7730 error = got_error(GOT_ERR_OBJ_TYPE);
7731 break;
7733 if (error)
7734 break;
7735 free(label);
7736 label = NULL;
7737 free(id);
7738 id = NULL;
7740 done:
7741 free(label);
7742 free(id);
7743 free(commit_id);
7744 if (worktree)
7745 got_worktree_close(worktree);
7746 if (repo) {
7747 const struct got_error *repo_error;
7748 repo_error = got_repo_close(repo);
7749 if (error == NULL)
7750 error = repo_error;
7752 return error;