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>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_checkout(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
164 };
166 static void
167 list_commands(void)
169 int i;
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
176 fputc('\n', stderr);
179 int
180 main(int argc, char *argv[])
182 struct got_cmd *cmd;
183 unsigned int i;
184 int ch;
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
188 { NULL, 0, NULL, 0}
189 };
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 switch (ch) {
195 case 'h':
196 hflag = 1;
197 break;
198 case 'V':
199 Vflag = 1;
200 break;
201 default:
202 usage(hflag);
203 /* NOTREACHED */
207 argc -= optind;
208 argv += optind;
209 optind = 0;
211 if (Vflag) {
212 got_version_print_str();
213 return 1;
216 if (argc <= 0)
217 usage(hflag);
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
229 continue;
231 if (hflag)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 !(sigint_received &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 return 1;
245 return 0;
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 list_commands();
250 return 1;
253 __dead static void
254 usage(int hflag)
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 getprogname());
258 if (hflag)
259 list_commands();
260 exit(1);
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
267 const char *editor;
269 *abspath = NULL;
271 editor = getenv("VISUAL");
272 if (editor == NULL)
273 editor = getenv("EDITOR");
275 if (editor) {
276 err = got_path_find_prog(abspath, editor);
277 if (err)
278 return err;
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
287 return NULL;
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
296 #ifdef PROFILE
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
299 #endif
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 __dead static void
320 usage_init(void)
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 exit(1);
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
331 int ch;
333 while ((ch = getopt(argc, argv, "")) != -1) {
334 switch (ch) {
335 default:
336 usage_init();
337 /* NOTREACHED */
341 argc -= optind;
342 argv += optind;
344 #ifndef PROFILE
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 err(1, "pledge");
347 #endif
348 if (argc != 1)
349 usage_init();
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
358 if (error &&
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 goto done;
362 error = apply_unveil(repo_path, 0, NULL);
363 if (error)
364 goto done;
366 error = got_repo_init(repo_path);
367 done:
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_import(void)
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
377 exit(1);
380 int
381 spawn_editor(const char *editor, const char *file)
383 pid_t pid;
384 sig_t sighup, sigint, sigquit;
385 int st = -1;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
392 case -1:
393 goto doneediting;
394 case 0:
395 execl(editor, editor, file, (char *)NULL);
396 _exit(127);
399 while (waitpid(pid, &st, 0) == -1)
400 if (errno != EINTR)
401 break;
403 doneediting:
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
409 errno = EINTR;
410 return -1;
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
421 char buf[1024];
422 struct stat st, st2;
423 FILE *fp;
424 int content_changed = 0;
425 size_t len;
427 *logmsg = NULL;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
443 if (*logmsg == NULL)
444 return got_error_from_errno("malloc");
445 (*logmsg)[0] = '\0';
446 len = 0;
448 fp = fopen(logmsg_path, "r");
449 if (fp == NULL) {
450 err = got_error_from_errno("fopen");
451 goto done;
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
455 content_changed = 1;
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
460 fclose(fp);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
464 len--;
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
470 done:
471 if (err) {
472 free(*logmsg);
473 *logmsg = NULL;
475 return err;
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
484 int fd;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
488 branch_name) == -1)
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
493 if (err)
494 goto done;
496 dprintf(fd, initial_content);
497 close(fd);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 done:
501 free(initial_content);
502 return err;
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
509 return NULL;
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
518 *author = NULL;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (name && email) {
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
535 return NULL;
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
542 if (*author == NULL)
543 return got_error_from_errno("strdup");
545 /*
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
548 */
549 while (*got_author && *got_author != '<')
550 got_author++;
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '@')
556 got_author++;
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 goto done;
561 while (*got_author && *got_author != '>')
562 got_author++;
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 done:
566 if (err) {
567 free(*author);
568 *author = NULL;
570 return err;
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
579 if (homedir) {
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
584 return NULL;
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
598 int ch;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 switch (ch) {
607 case 'b':
608 branch_name = optarg;
609 break;
610 case 'm':
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
614 goto done;
616 break;
617 case 'r':
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
621 optarg);
622 goto done;
624 break;
625 case 'I':
626 if (optarg[0] == '\0')
627 break;
628 error = got_pathlist_insert(&pe, &ignores, optarg,
629 NULL);
630 if (error)
631 goto done;
632 break;
633 default:
634 usage_import();
635 /* NOTREACHED */
639 argc -= optind;
640 argv += optind;
642 #ifndef PROFILE
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 "unveil",
645 NULL) == -1)
646 err(1, "pledge");
647 #endif
648 if (argc != 1)
649 usage_import();
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
658 if (error)
659 goto done;
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
661 if (error)
662 goto done;
664 error = get_author(&author, repo);
665 if (error)
666 return error;
668 /*
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
672 */
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
678 goto done;
681 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error) {
683 if (error->code != GOT_ERR_NOT_REF)
684 goto done;
685 } else {
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
688 goto done;
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
694 goto done;
696 got_path_strip_trailing_slashes(path_dir);
698 /*
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
701 */
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
704 if (error)
705 goto done;
706 free(logmsg);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 path_dir, refname);
709 if (error) {
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 logmsg_path != NULL)
712 preserve_logmsg = 1;
713 goto done;
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 if (error) {
726 if (logmsg_path)
727 preserve_logmsg = 1;
728 goto done;
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_ref_write(branch_ref, repo);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_object_id_str(&id_str, new_commit_id);
754 if (error) {
755 if (logmsg_path)
756 preserve_logmsg = 1;
757 goto done;
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error) {
762 if (error->code != GOT_ERR_NOT_REF) {
763 if (logmsg_path)
764 preserve_logmsg = 1;
765 goto done;
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 branch_ref);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
776 error = got_ref_write(head_ref, repo);
777 if (error) {
778 if (logmsg_path)
779 preserve_logmsg = 1;
780 goto done;
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
786 done:
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
792 free(logmsg);
793 free(logmsg_path);
794 free(repo_path);
795 free(editor);
796 free(refname);
797 free(new_commit_id);
798 free(id_str);
799 free(author);
800 free(gitconfig_path);
801 if (branch_ref)
802 got_ref_close(branch_ref);
803 if (head_ref)
804 got_ref_close(head_ref);
805 return error;
808 __dead static void
809 usage_clone(void)
811 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
812 exit(1);
815 __dead static void
816 usage_checkout(void)
818 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
819 "[-p prefix] repository-path [worktree-path]\n", getprogname());
820 exit(1);
823 static void
824 show_worktree_base_ref_warning(void)
826 fprintf(stderr, "%s: warning: could not create a reference "
827 "to the work tree's base commit; the commit could be "
828 "garbage-collected by Git; making the repository "
829 "writable and running 'got update' will prevent this\n",
830 getprogname());
833 struct got_checkout_progress_arg {
834 const char *worktree_path;
835 int had_base_commit_ref_error;
836 };
838 static const struct got_error *
839 checkout_progress(void *arg, unsigned char status, const char *path)
841 struct got_checkout_progress_arg *a = arg;
843 /* Base commit bump happens silently. */
844 if (status == GOT_STATUS_BUMP_BASE)
845 return NULL;
847 if (status == GOT_STATUS_BASE_REF_ERR) {
848 a->had_base_commit_ref_error = 1;
849 return NULL;
852 while (path[0] == '/')
853 path++;
855 printf("%c %s/%s\n", status, a->worktree_path, path);
856 return NULL;
859 static const struct got_error *
860 check_cancelled(void *arg)
862 if (sigint_received || sigpipe_received)
863 return got_error(GOT_ERR_CANCELLED);
864 return NULL;
867 static const struct got_error *
868 check_linear_ancestry(struct got_object_id *commit_id,
869 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
870 struct got_repository *repo)
872 const struct got_error *err = NULL;
873 struct got_object_id *yca_id;
875 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
876 commit_id, base_commit_id, repo, check_cancelled, NULL);
877 if (err)
878 return err;
880 if (yca_id == NULL)
881 return got_error(GOT_ERR_ANCESTRY);
883 /*
884 * Require a straight line of history between the target commit
885 * and the work tree's base commit.
887 * Non-linear situations such as this require a rebase:
889 * (commit) D F (base_commit)
890 * \ /
891 * C E
892 * \ /
893 * B (yca)
894 * |
895 * A
897 * 'got update' only handles linear cases:
898 * Update forwards in time: A (base/yca) - B - C - D (commit)
899 * Update backwards in time: D (base) - C - B - A (commit/yca)
900 */
901 if (allow_forwards_in_time_only) {
902 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
903 return got_error(GOT_ERR_ANCESTRY);
904 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
905 got_object_id_cmp(base_commit_id, yca_id) != 0)
906 return got_error(GOT_ERR_ANCESTRY);
908 free(yca_id);
909 return NULL;
912 static const struct got_error *
913 check_same_branch(struct got_object_id *commit_id,
914 struct got_reference *head_ref, struct got_object_id *yca_id,
915 struct got_repository *repo)
917 const struct got_error *err = NULL;
918 struct got_commit_graph *graph = NULL;
919 struct got_object_id *head_commit_id = NULL;
920 int is_same_branch = 0;
922 err = got_ref_resolve(&head_commit_id, repo, head_ref);
923 if (err)
924 goto done;
926 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
927 is_same_branch = 1;
928 goto done;
930 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
931 is_same_branch = 1;
932 goto done;
935 err = got_commit_graph_open(&graph, "/", 1);
936 if (err)
937 goto done;
939 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
940 check_cancelled, NULL);
941 if (err)
942 goto done;
944 for (;;) {
945 struct got_object_id *id;
946 err = got_commit_graph_iter_next(&id, graph, repo,
947 check_cancelled, NULL);
948 if (err) {
949 if (err->code == GOT_ERR_ITER_COMPLETED)
950 err = NULL;
951 break;
954 if (id) {
955 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 break;
957 if (got_object_id_cmp(id, commit_id) == 0) {
958 is_same_branch = 1;
959 break;
963 done:
964 if (graph)
965 got_commit_graph_close(graph);
966 free(head_commit_id);
967 if (!err && !is_same_branch)
968 err = got_error(GOT_ERR_ANCESTRY);
969 return err;
972 static const struct got_error *
973 fetch_progress(void *arg, const char *message, off_t packfile_size,
974 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
976 int *did_something = arg, p;
977 char scaled[FMT_SCALED_STRSIZE];
979 if (message) {
980 printf("\rserver: %s", message);
981 *did_something = 1;
982 } else if (packfile_size > 0 || nobj_indexed > 0) {
983 printf("\r");
984 if (fmt_scaled(packfile_size, scaled) == 0)
985 printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
986 if (nobj_indexed > 0) {
987 p = (nobj_indexed * 100) / nobj_total;
988 printf("; indexing %d%% (%d)", p, nobj_indexed);
990 if (nobj_resolved > 0) {
991 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
992 printf("; resolving deltas %d%% (%d)",
993 p, nobj_resolved);
995 *did_something = 1;
997 fflush(stdout);
998 return NULL;
1001 static const struct got_error *
1002 cmd_clone(int argc, char *argv[])
1004 const struct got_error *err = NULL;
1005 const char *uri, *branch_filter, *dirname;
1006 char *proto, *host, *port, *repo_name, *server_path;
1007 char *default_destdir = NULL, *id_str = NULL;
1008 const char *repo_path;
1009 struct got_repository *repo = NULL;
1010 struct got_pathlist_head refs, symrefs;
1011 struct got_pathlist_entry *pe;
1012 struct got_object_id *pack_hash = NULL;
1013 int ch, fetchfd = -1;
1014 int did_something = 0;
1016 TAILQ_INIT(&refs);
1017 TAILQ_INIT(&symrefs);
1019 while ((ch = getopt(argc, argv, "b:")) != -1) {
1020 switch (ch) {
1021 case 'b':
1022 branch_filter = optarg;
1023 break;
1024 default:
1025 usage_clone();
1026 break;
1029 argc -= optind;
1030 argv += optind;
1031 uri = argv[0];
1032 if(argc == 1)
1033 dirname = NULL;
1034 else if(argc == 2)
1035 dirname = argv[1];
1036 else
1037 usage_clone();
1039 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1040 &repo_name, argv[0]);
1041 if (err)
1042 goto done;
1044 if (dirname == NULL) {
1045 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1046 err = got_error_from_errno("asprintf");
1047 goto done;
1049 repo_path = default_destdir;
1050 } else
1051 repo_path = dirname;
1053 err = got_path_mkdir(repo_path);
1054 if (err)
1055 goto done;
1057 err = got_repo_init(repo_path);
1058 if (err)
1059 goto done;
1061 err = got_repo_open(&repo, repo_path, NULL);
1062 if (err)
1063 goto done;
1065 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1066 if (err)
1067 goto done;
1069 printf("Connected to %s:%s\n", host, port);
1071 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1072 repo, fetch_progress, &did_something);
1073 if (err)
1074 goto done;
1075 if (did_something)
1076 printf("\n");
1078 err = got_object_id_str(&id_str, pack_hash);
1079 if (err)
1080 goto done;
1081 printf("Fetched %s.pack\n", id_str);
1082 free(id_str);
1084 /* Set up references provided with the pack file. */
1085 TAILQ_FOREACH(pe, &refs, entry) {
1086 const char *refname = pe->path;
1087 struct got_object_id *id = pe->data;
1088 struct got_reference *ref;
1091 err = got_ref_alloc(&ref, refname, id);
1092 if (err)
1093 goto done;
1095 #if 0
1096 err = got_object_id_str(&id_str, id);
1097 if (err)
1098 goto done;
1099 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1100 free(id_str);
1101 #endif
1102 err = got_ref_write(ref, repo);
1103 got_ref_close(ref);
1104 if (err)
1105 goto done;
1108 /* Set the HEAD reference if the server provided one. */
1109 TAILQ_FOREACH(pe, &symrefs, entry) {
1110 struct got_reference *symref, *target_ref;
1111 const char *refname = pe->path;
1112 const char *target = pe->data;
1114 if (strcmp(refname, GOT_REF_HEAD) != 0)
1115 continue;
1117 err = got_ref_open(&target_ref, repo, target, 0);
1118 if (err) {
1119 if (err->code == GOT_ERR_NOT_REF)
1120 continue;
1121 goto done;
1124 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1125 got_ref_close(target_ref);
1126 if (err)
1127 goto done;
1129 printf("Setting %s to %s\n", GOT_REF_HEAD,
1130 got_ref_get_symref_target(symref));
1132 err = got_ref_write(symref, repo);
1133 got_ref_close(symref);
1134 break;
1137 done:
1138 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1139 err = got_error_from_errno("close");
1140 if (repo)
1141 got_repo_close(repo);
1142 TAILQ_FOREACH(pe, &refs, entry) {
1143 free((void *)pe->path);
1144 free(pe->data);
1146 got_pathlist_free(&refs);
1147 TAILQ_FOREACH(pe, &symrefs, entry) {
1148 free((void *)pe->path);
1149 free(pe->data);
1151 got_pathlist_free(&symrefs);
1152 free(pack_hash);
1153 free(proto);
1154 free(host);
1155 free(port);
1156 free(server_path);
1157 free(repo_name);
1158 free(default_destdir);
1159 return err;
1162 static const struct got_error *
1163 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1165 static char msg[512];
1166 const char *branch_name;
1168 if (got_ref_is_symbolic(ref))
1169 branch_name = got_ref_get_symref_target(ref);
1170 else
1171 branch_name = got_ref_get_name(ref);
1173 if (strncmp("refs/heads/", branch_name, 11) == 0)
1174 branch_name += 11;
1176 snprintf(msg, sizeof(msg),
1177 "target commit is not contained in branch '%s'; "
1178 "the branch to use must be specified with -b; "
1179 "if necessary a new branch can be created for "
1180 "this commit with 'got branch -c %s BRANCH_NAME'",
1181 branch_name, commit_id_str);
1183 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1186 static const struct got_error *
1187 cmd_checkout(int argc, char *argv[])
1189 const struct got_error *error = NULL;
1190 struct got_repository *repo = NULL;
1191 struct got_reference *head_ref = NULL;
1192 struct got_worktree *worktree = NULL;
1193 char *repo_path = NULL;
1194 char *worktree_path = NULL;
1195 const char *path_prefix = "";
1196 const char *branch_name = GOT_REF_HEAD;
1197 char *commit_id_str = NULL;
1198 int ch, same_path_prefix, allow_nonempty = 0;
1199 struct got_pathlist_head paths;
1200 struct got_checkout_progress_arg cpa;
1202 TAILQ_INIT(&paths);
1204 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1205 switch (ch) {
1206 case 'b':
1207 branch_name = optarg;
1208 break;
1209 case 'c':
1210 commit_id_str = strdup(optarg);
1211 if (commit_id_str == NULL)
1212 return got_error_from_errno("strdup");
1213 break;
1214 case 'E':
1215 allow_nonempty = 1;
1216 break;
1217 case 'p':
1218 path_prefix = optarg;
1219 break;
1220 default:
1221 usage_checkout();
1222 /* NOTREACHED */
1226 argc -= optind;
1227 argv += optind;
1229 #ifndef PROFILE
1230 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1231 "unveil", NULL) == -1)
1232 err(1, "pledge");
1233 #endif
1234 if (argc == 1) {
1235 char *cwd, *base, *dotgit;
1236 repo_path = realpath(argv[0], NULL);
1237 if (repo_path == NULL)
1238 return got_error_from_errno2("realpath", argv[0]);
1239 cwd = getcwd(NULL, 0);
1240 if (cwd == NULL) {
1241 error = got_error_from_errno("getcwd");
1242 goto done;
1244 if (path_prefix[0]) {
1245 base = basename(path_prefix);
1246 if (base == NULL) {
1247 error = got_error_from_errno2("basename",
1248 path_prefix);
1249 goto done;
1251 } else {
1252 base = basename(repo_path);
1253 if (base == NULL) {
1254 error = got_error_from_errno2("basename",
1255 repo_path);
1256 goto done;
1259 dotgit = strstr(base, ".git");
1260 if (dotgit)
1261 *dotgit = '\0';
1262 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1263 error = got_error_from_errno("asprintf");
1264 free(cwd);
1265 goto done;
1267 free(cwd);
1268 } else if (argc == 2) {
1269 repo_path = realpath(argv[0], NULL);
1270 if (repo_path == NULL) {
1271 error = got_error_from_errno2("realpath", argv[0]);
1272 goto done;
1274 worktree_path = realpath(argv[1], NULL);
1275 if (worktree_path == NULL) {
1276 if (errno != ENOENT) {
1277 error = got_error_from_errno2("realpath",
1278 argv[1]);
1279 goto done;
1281 worktree_path = strdup(argv[1]);
1282 if (worktree_path == NULL) {
1283 error = got_error_from_errno("strdup");
1284 goto done;
1287 } else
1288 usage_checkout();
1290 got_path_strip_trailing_slashes(repo_path);
1291 got_path_strip_trailing_slashes(worktree_path);
1293 error = got_repo_open(&repo, repo_path, NULL);
1294 if (error != NULL)
1295 goto done;
1297 /* Pre-create work tree path for unveil(2) */
1298 error = got_path_mkdir(worktree_path);
1299 if (error) {
1300 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1301 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1302 goto done;
1303 if (!allow_nonempty &&
1304 !got_path_dir_is_empty(worktree_path)) {
1305 error = got_error_path(worktree_path,
1306 GOT_ERR_DIR_NOT_EMPTY);
1307 goto done;
1311 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1312 if (error)
1313 goto done;
1315 error = got_ref_open(&head_ref, repo, branch_name, 0);
1316 if (error != NULL)
1317 goto done;
1319 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1320 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1321 goto done;
1323 error = got_worktree_open(&worktree, worktree_path);
1324 if (error != NULL)
1325 goto done;
1327 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1328 path_prefix);
1329 if (error != NULL)
1330 goto done;
1331 if (!same_path_prefix) {
1332 error = got_error(GOT_ERR_PATH_PREFIX);
1333 goto done;
1336 if (commit_id_str) {
1337 struct got_object_id *commit_id;
1338 error = got_repo_match_object_id(&commit_id, NULL,
1339 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1340 if (error)
1341 goto done;
1342 error = check_linear_ancestry(commit_id,
1343 got_worktree_get_base_commit_id(worktree), 0, repo);
1344 if (error != NULL) {
1345 free(commit_id);
1346 if (error->code == GOT_ERR_ANCESTRY) {
1347 error = checkout_ancestry_error(
1348 head_ref, commit_id_str);
1350 goto done;
1352 error = check_same_branch(commit_id, head_ref, NULL, repo);
1353 if (error) {
1354 if (error->code == GOT_ERR_ANCESTRY) {
1355 error = checkout_ancestry_error(
1356 head_ref, commit_id_str);
1358 goto done;
1360 error = got_worktree_set_base_commit_id(worktree, repo,
1361 commit_id);
1362 free(commit_id);
1363 if (error)
1364 goto done;
1367 error = got_pathlist_append(&paths, "", NULL);
1368 if (error)
1369 goto done;
1370 cpa.worktree_path = worktree_path;
1371 cpa.had_base_commit_ref_error = 0;
1372 error = got_worktree_checkout_files(worktree, &paths, repo,
1373 checkout_progress, &cpa, check_cancelled, NULL);
1374 if (error != NULL)
1375 goto done;
1377 printf("Now shut up and hack\n");
1378 if (cpa.had_base_commit_ref_error)
1379 show_worktree_base_ref_warning();
1380 done:
1381 got_pathlist_free(&paths);
1382 free(commit_id_str);
1383 free(repo_path);
1384 free(worktree_path);
1385 return error;
1388 __dead static void
1389 usage_update(void)
1391 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1392 getprogname());
1393 exit(1);
1396 static const struct got_error *
1397 update_progress(void *arg, unsigned char status, const char *path)
1399 int *did_something = arg;
1401 if (status == GOT_STATUS_EXISTS ||
1402 status == GOT_STATUS_BASE_REF_ERR)
1403 return NULL;
1405 *did_something = 1;
1407 /* Base commit bump happens silently. */
1408 if (status == GOT_STATUS_BUMP_BASE)
1409 return NULL;
1411 while (path[0] == '/')
1412 path++;
1413 printf("%c %s\n", status, path);
1414 return NULL;
1417 static const struct got_error *
1418 switch_head_ref(struct got_reference *head_ref,
1419 struct got_object_id *commit_id, struct got_worktree *worktree,
1420 struct got_repository *repo)
1422 const struct got_error *err = NULL;
1423 char *base_id_str;
1424 int ref_has_moved = 0;
1426 /* Trivial case: switching between two different references. */
1427 if (strcmp(got_ref_get_name(head_ref),
1428 got_worktree_get_head_ref_name(worktree)) != 0) {
1429 printf("Switching work tree from %s to %s\n",
1430 got_worktree_get_head_ref_name(worktree),
1431 got_ref_get_name(head_ref));
1432 return got_worktree_set_head_ref(worktree, head_ref);
1435 err = check_linear_ancestry(commit_id,
1436 got_worktree_get_base_commit_id(worktree), 0, repo);
1437 if (err) {
1438 if (err->code != GOT_ERR_ANCESTRY)
1439 return err;
1440 ref_has_moved = 1;
1442 if (!ref_has_moved)
1443 return NULL;
1445 /* Switching to a rebased branch with the same reference name. */
1446 err = got_object_id_str(&base_id_str,
1447 got_worktree_get_base_commit_id(worktree));
1448 if (err)
1449 return err;
1450 printf("Reference %s now points at a different branch\n",
1451 got_worktree_get_head_ref_name(worktree));
1452 printf("Switching work tree from %s to %s\n", base_id_str,
1453 got_worktree_get_head_ref_name(worktree));
1454 return NULL;
1457 static const struct got_error *
1458 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1460 const struct got_error *err;
1461 int in_progress;
1463 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1464 if (err)
1465 return err;
1466 if (in_progress)
1467 return got_error(GOT_ERR_REBASING);
1469 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1470 if (err)
1471 return err;
1472 if (in_progress)
1473 return got_error(GOT_ERR_HISTEDIT_BUSY);
1475 return NULL;
1478 static const struct got_error *
1479 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1480 char *argv[], struct got_worktree *worktree)
1482 const struct got_error *err = NULL;
1483 char *path;
1484 int i;
1486 if (argc == 0) {
1487 path = strdup("");
1488 if (path == NULL)
1489 return got_error_from_errno("strdup");
1490 return got_pathlist_append(paths, path, NULL);
1493 for (i = 0; i < argc; i++) {
1494 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1495 if (err)
1496 break;
1497 err = got_pathlist_append(paths, path, NULL);
1498 if (err) {
1499 free(path);
1500 break;
1504 return err;
1507 static const struct got_error *
1508 cmd_update(int argc, char *argv[])
1510 const struct got_error *error = NULL;
1511 struct got_repository *repo = NULL;
1512 struct got_worktree *worktree = NULL;
1513 char *worktree_path = NULL;
1514 struct got_object_id *commit_id = NULL;
1515 char *commit_id_str = NULL;
1516 const char *branch_name = NULL;
1517 struct got_reference *head_ref = NULL;
1518 struct got_pathlist_head paths;
1519 struct got_pathlist_entry *pe;
1520 int ch, did_something = 0;
1522 TAILQ_INIT(&paths);
1524 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1525 switch (ch) {
1526 case 'b':
1527 branch_name = optarg;
1528 break;
1529 case 'c':
1530 commit_id_str = strdup(optarg);
1531 if (commit_id_str == NULL)
1532 return got_error_from_errno("strdup");
1533 break;
1534 default:
1535 usage_update();
1536 /* NOTREACHED */
1540 argc -= optind;
1541 argv += optind;
1543 #ifndef PROFILE
1544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1545 "unveil", NULL) == -1)
1546 err(1, "pledge");
1547 #endif
1548 worktree_path = getcwd(NULL, 0);
1549 if (worktree_path == NULL) {
1550 error = got_error_from_errno("getcwd");
1551 goto done;
1553 error = got_worktree_open(&worktree, worktree_path);
1554 if (error)
1555 goto done;
1557 error = check_rebase_or_histedit_in_progress(worktree);
1558 if (error)
1559 goto done;
1561 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1562 NULL);
1563 if (error != NULL)
1564 goto done;
1566 error = apply_unveil(got_repo_get_path(repo), 0,
1567 got_worktree_get_root_path(worktree));
1568 if (error)
1569 goto done;
1571 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1572 if (error)
1573 goto done;
1575 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1576 got_worktree_get_head_ref_name(worktree), 0);
1577 if (error != NULL)
1578 goto done;
1579 if (commit_id_str == NULL) {
1580 error = got_ref_resolve(&commit_id, repo, head_ref);
1581 if (error != NULL)
1582 goto done;
1583 error = got_object_id_str(&commit_id_str, commit_id);
1584 if (error != NULL)
1585 goto done;
1586 } else {
1587 error = got_repo_match_object_id(&commit_id, NULL,
1588 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1589 free(commit_id_str);
1590 commit_id_str = NULL;
1591 if (error)
1592 goto done;
1593 error = got_object_id_str(&commit_id_str, commit_id);
1594 if (error)
1595 goto done;
1598 if (branch_name) {
1599 struct got_object_id *head_commit_id;
1600 TAILQ_FOREACH(pe, &paths, entry) {
1601 if (pe->path_len == 0)
1602 continue;
1603 error = got_error_msg(GOT_ERR_BAD_PATH,
1604 "switching between branches requires that "
1605 "the entire work tree gets updated");
1606 goto done;
1608 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1609 if (error)
1610 goto done;
1611 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1612 repo);
1613 free(head_commit_id);
1614 if (error != NULL)
1615 goto done;
1616 error = check_same_branch(commit_id, head_ref, NULL, repo);
1617 if (error)
1618 goto done;
1619 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1620 if (error)
1621 goto done;
1622 } else {
1623 error = check_linear_ancestry(commit_id,
1624 got_worktree_get_base_commit_id(worktree), 0, repo);
1625 if (error != NULL) {
1626 if (error->code == GOT_ERR_ANCESTRY)
1627 error = got_error(GOT_ERR_BRANCH_MOVED);
1628 goto done;
1630 error = check_same_branch(commit_id, head_ref, NULL, repo);
1631 if (error)
1632 goto done;
1635 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1636 commit_id) != 0) {
1637 error = got_worktree_set_base_commit_id(worktree, repo,
1638 commit_id);
1639 if (error)
1640 goto done;
1643 error = got_worktree_checkout_files(worktree, &paths, repo,
1644 update_progress, &did_something, check_cancelled, NULL);
1645 if (error != NULL)
1646 goto done;
1648 if (did_something)
1649 printf("Updated to commit %s\n", commit_id_str);
1650 else
1651 printf("Already up-to-date\n");
1652 done:
1653 free(worktree_path);
1654 TAILQ_FOREACH(pe, &paths, entry)
1655 free((char *)pe->path);
1656 got_pathlist_free(&paths);
1657 free(commit_id);
1658 free(commit_id_str);
1659 return error;
1662 static const struct got_error *
1663 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1664 const char *path, int diff_context, int ignore_whitespace,
1665 struct got_repository *repo)
1667 const struct got_error *err = NULL;
1668 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1670 if (blob_id1) {
1671 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1672 if (err)
1673 goto done;
1676 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1677 if (err)
1678 goto done;
1680 while (path[0] == '/')
1681 path++;
1682 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1683 ignore_whitespace, stdout);
1684 done:
1685 if (blob1)
1686 got_object_blob_close(blob1);
1687 got_object_blob_close(blob2);
1688 return err;
1691 static const struct got_error *
1692 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1693 const char *path, int diff_context, int ignore_whitespace,
1694 struct got_repository *repo)
1696 const struct got_error *err = NULL;
1697 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1698 struct got_diff_blob_output_unidiff_arg arg;
1700 if (tree_id1) {
1701 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1702 if (err)
1703 goto done;
1706 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1707 if (err)
1708 goto done;
1710 arg.diff_context = diff_context;
1711 arg.ignore_whitespace = ignore_whitespace;
1712 arg.outfile = stdout;
1713 while (path[0] == '/')
1714 path++;
1715 err = got_diff_tree(tree1, tree2, path, path, repo,
1716 got_diff_blob_output_unidiff, &arg, 1);
1717 done:
1718 if (tree1)
1719 got_object_tree_close(tree1);
1720 if (tree2)
1721 got_object_tree_close(tree2);
1722 return err;
1725 static const struct got_error *
1726 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1727 const char *path, int diff_context, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *pcommit = NULL;
1731 char *id_str1 = NULL, *id_str2 = NULL;
1732 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1733 struct got_object_qid *qid;
1735 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1736 if (qid != NULL) {
1737 err = got_object_open_as_commit(&pcommit, repo,
1738 qid->id);
1739 if (err)
1740 return err;
1743 if (path && path[0] != '\0') {
1744 int obj_type;
1745 err = got_object_id_by_path(&obj_id2, repo, id, path);
1746 if (err)
1747 goto done;
1748 err = got_object_id_str(&id_str2, obj_id2);
1749 if (err) {
1750 free(obj_id2);
1751 goto done;
1753 if (pcommit) {
1754 err = got_object_id_by_path(&obj_id1, repo,
1755 qid->id, path);
1756 if (err) {
1757 free(obj_id2);
1758 goto done;
1760 err = got_object_id_str(&id_str1, obj_id1);
1761 if (err) {
1762 free(obj_id2);
1763 goto done;
1766 err = got_object_get_type(&obj_type, repo, obj_id2);
1767 if (err) {
1768 free(obj_id2);
1769 goto done;
1771 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1772 switch (obj_type) {
1773 case GOT_OBJ_TYPE_BLOB:
1774 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1775 0, repo);
1776 break;
1777 case GOT_OBJ_TYPE_TREE:
1778 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1779 0, repo);
1780 break;
1781 default:
1782 err = got_error(GOT_ERR_OBJ_TYPE);
1783 break;
1785 free(obj_id1);
1786 free(obj_id2);
1787 } else {
1788 obj_id2 = got_object_commit_get_tree_id(commit);
1789 err = got_object_id_str(&id_str2, obj_id2);
1790 if (err)
1791 goto done;
1792 obj_id1 = got_object_commit_get_tree_id(pcommit);
1793 err = got_object_id_str(&id_str1, obj_id1);
1794 if (err)
1795 goto done;
1796 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1797 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1799 done:
1800 free(id_str1);
1801 free(id_str2);
1802 if (pcommit)
1803 got_object_commit_close(pcommit);
1804 return err;
1807 static char *
1808 get_datestr(time_t *time, char *datebuf)
1810 struct tm mytm, *tm;
1811 char *p, *s;
1813 tm = gmtime_r(time, &mytm);
1814 if (tm == NULL)
1815 return NULL;
1816 s = asctime_r(tm, datebuf);
1817 if (s == NULL)
1818 return NULL;
1819 p = strchr(s, '\n');
1820 if (p)
1821 *p = '\0';
1822 return s;
1825 static const struct got_error *
1826 match_logmsg(int *have_match, struct got_object_id *id,
1827 struct got_commit_object *commit, regex_t *regex)
1829 const struct got_error *err = NULL;
1830 regmatch_t regmatch;
1831 char *id_str = NULL, *logmsg = NULL;
1833 *have_match = 0;
1835 err = got_object_id_str(&id_str, id);
1836 if (err)
1837 return err;
1839 err = got_object_commit_get_logmsg(&logmsg, commit);
1840 if (err)
1841 goto done;
1843 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1844 *have_match = 1;
1845 done:
1846 free(id_str);
1847 free(logmsg);
1848 return err;
1851 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1853 static const struct got_error *
1854 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1855 struct got_repository *repo, const char *path, int show_patch,
1856 int diff_context, struct got_reflist_head *refs)
1858 const struct got_error *err = NULL;
1859 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1860 char datebuf[26];
1861 time_t committer_time;
1862 const char *author, *committer;
1863 char *refs_str = NULL;
1864 struct got_reflist_entry *re;
1866 SIMPLEQ_FOREACH(re, refs, entry) {
1867 char *s;
1868 const char *name;
1869 struct got_tag_object *tag = NULL;
1870 int cmp;
1872 name = got_ref_get_name(re->ref);
1873 if (strcmp(name, GOT_REF_HEAD) == 0)
1874 continue;
1875 if (strncmp(name, "refs/", 5) == 0)
1876 name += 5;
1877 if (strncmp(name, "got/", 4) == 0)
1878 continue;
1879 if (strncmp(name, "heads/", 6) == 0)
1880 name += 6;
1881 if (strncmp(name, "remotes/", 8) == 0)
1882 name += 8;
1883 if (strncmp(name, "tags/", 5) == 0) {
1884 err = got_object_open_as_tag(&tag, repo, re->id);
1885 if (err) {
1886 if (err->code != GOT_ERR_OBJ_TYPE)
1887 return err;
1888 /* Ref points at something other than a tag. */
1889 err = NULL;
1890 tag = NULL;
1893 cmp = got_object_id_cmp(tag ?
1894 got_object_tag_get_object_id(tag) : re->id, id);
1895 if (tag)
1896 got_object_tag_close(tag);
1897 if (cmp != 0)
1898 continue;
1899 s = refs_str;
1900 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1901 name) == -1) {
1902 err = got_error_from_errno("asprintf");
1903 free(s);
1904 return err;
1906 free(s);
1908 err = got_object_id_str(&id_str, id);
1909 if (err)
1910 return err;
1912 printf(GOT_COMMIT_SEP_STR);
1913 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1914 refs_str ? refs_str : "", refs_str ? ")" : "");
1915 free(id_str);
1916 id_str = NULL;
1917 free(refs_str);
1918 refs_str = NULL;
1919 printf("from: %s\n", got_object_commit_get_author(commit));
1920 committer_time = got_object_commit_get_committer_time(commit);
1921 datestr = get_datestr(&committer_time, datebuf);
1922 if (datestr)
1923 printf("date: %s UTC\n", datestr);
1924 author = got_object_commit_get_author(commit);
1925 committer = got_object_commit_get_committer(commit);
1926 if (strcmp(author, committer) != 0)
1927 printf("via: %s\n", committer);
1928 if (got_object_commit_get_nparents(commit) > 1) {
1929 const struct got_object_id_queue *parent_ids;
1930 struct got_object_qid *qid;
1931 int n = 1;
1932 parent_ids = got_object_commit_get_parent_ids(commit);
1933 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1934 err = got_object_id_str(&id_str, qid->id);
1935 if (err)
1936 return err;
1937 printf("parent %d: %s\n", n++, id_str);
1938 free(id_str);
1942 err = got_object_commit_get_logmsg(&logmsg0, commit);
1943 if (err)
1944 return err;
1946 logmsg = logmsg0;
1947 do {
1948 line = strsep(&logmsg, "\n");
1949 if (line)
1950 printf(" %s\n", line);
1951 } while (line);
1952 free(logmsg0);
1954 if (show_patch) {
1955 err = print_patch(commit, id, path, diff_context, repo);
1956 if (err == 0)
1957 printf("\n");
1960 if (fflush(stdout) != 0 && err == NULL)
1961 err = got_error_from_errno("fflush");
1962 return err;
1965 static const struct got_error *
1966 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1967 const char *path, int show_patch, const char *search_pattern,
1968 int diff_context, int limit, int log_branches,
1969 struct got_reflist_head *refs)
1971 const struct got_error *err;
1972 struct got_commit_graph *graph;
1973 regex_t regex;
1974 int have_match;
1976 if (search_pattern &&
1977 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1978 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1980 err = got_commit_graph_open(&graph, path, !log_branches);
1981 if (err)
1982 return err;
1983 err = got_commit_graph_iter_start(graph, root_id, repo,
1984 check_cancelled, NULL);
1985 if (err)
1986 goto done;
1987 for (;;) {
1988 struct got_commit_object *commit;
1989 struct got_object_id *id;
1991 if (sigint_received || sigpipe_received)
1992 break;
1994 err = got_commit_graph_iter_next(&id, graph, repo,
1995 check_cancelled, NULL);
1996 if (err) {
1997 if (err->code == GOT_ERR_ITER_COMPLETED)
1998 err = NULL;
1999 break;
2001 if (id == NULL)
2002 break;
2004 err = got_object_open_as_commit(&commit, repo, id);
2005 if (err)
2006 break;
2008 if (search_pattern) {
2009 err = match_logmsg(&have_match, id, commit, &regex);
2010 if (err) {
2011 got_object_commit_close(commit);
2012 break;
2014 if (have_match == 0) {
2015 got_object_commit_close(commit);
2016 continue;
2020 err = print_commit(commit, id, repo, path, show_patch,
2021 diff_context, refs);
2022 got_object_commit_close(commit);
2023 if (err || (limit && --limit == 0))
2024 break;
2026 done:
2027 if (search_pattern)
2028 regfree(&regex);
2029 got_commit_graph_close(graph);
2030 return err;
2033 __dead static void
2034 usage_log(void)
2036 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2037 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2038 exit(1);
2041 static int
2042 get_default_log_limit(void)
2044 const char *got_default_log_limit;
2045 long long n;
2046 const char *errstr;
2048 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2049 if (got_default_log_limit == NULL)
2050 return 0;
2051 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2052 if (errstr != NULL)
2053 return 0;
2054 return n;
2057 static const struct got_error *
2058 cmd_log(int argc, char *argv[])
2060 const struct got_error *error;
2061 struct got_repository *repo = NULL;
2062 struct got_worktree *worktree = NULL;
2063 struct got_commit_object *commit = NULL;
2064 struct got_object_id *id = NULL;
2065 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2066 const char *start_commit = NULL, *search_pattern = NULL;
2067 int diff_context = -1, ch;
2068 int show_patch = 0, limit = 0, log_branches = 0;
2069 const char *errstr;
2070 struct got_reflist_head refs;
2072 SIMPLEQ_INIT(&refs);
2074 #ifndef PROFILE
2075 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2076 NULL)
2077 == -1)
2078 err(1, "pledge");
2079 #endif
2081 limit = get_default_log_limit();
2083 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2084 switch (ch) {
2085 case 'p':
2086 show_patch = 1;
2087 break;
2088 case 'c':
2089 start_commit = optarg;
2090 break;
2091 case 'C':
2092 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2093 &errstr);
2094 if (errstr != NULL)
2095 err(1, "-C option %s", errstr);
2096 break;
2097 case 'l':
2098 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2099 if (errstr != NULL)
2100 err(1, "-l option %s", errstr);
2101 break;
2102 case 'b':
2103 log_branches = 1;
2104 break;
2105 case 'r':
2106 repo_path = realpath(optarg, NULL);
2107 if (repo_path == NULL)
2108 return got_error_from_errno2("realpath",
2109 optarg);
2110 got_path_strip_trailing_slashes(repo_path);
2111 break;
2112 case 's':
2113 search_pattern = optarg;
2114 break;
2115 default:
2116 usage_log();
2117 /* NOTREACHED */
2121 argc -= optind;
2122 argv += optind;
2124 if (diff_context == -1)
2125 diff_context = 3;
2126 else if (!show_patch)
2127 errx(1, "-C reguires -p");
2129 cwd = getcwd(NULL, 0);
2130 if (cwd == NULL) {
2131 error = got_error_from_errno("getcwd");
2132 goto done;
2135 error = got_worktree_open(&worktree, cwd);
2136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2137 goto done;
2138 error = NULL;
2140 if (argc == 0) {
2141 path = strdup("");
2142 if (path == NULL) {
2143 error = got_error_from_errno("strdup");
2144 goto done;
2146 } else if (argc == 1) {
2147 if (worktree) {
2148 error = got_worktree_resolve_path(&path, worktree,
2149 argv[0]);
2150 if (error)
2151 goto done;
2152 } else {
2153 path = strdup(argv[0]);
2154 if (path == NULL) {
2155 error = got_error_from_errno("strdup");
2156 goto done;
2159 } else
2160 usage_log();
2162 if (repo_path == NULL) {
2163 repo_path = worktree ?
2164 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2166 if (repo_path == NULL) {
2167 error = got_error_from_errno("strdup");
2168 goto done;
2171 error = got_repo_open(&repo, repo_path, NULL);
2172 if (error != NULL)
2173 goto done;
2175 error = apply_unveil(got_repo_get_path(repo), 1,
2176 worktree ? got_worktree_get_root_path(worktree) : NULL);
2177 if (error)
2178 goto done;
2180 if (start_commit == NULL) {
2181 struct got_reference *head_ref;
2182 error = got_ref_open(&head_ref, repo,
2183 worktree ? got_worktree_get_head_ref_name(worktree)
2184 : GOT_REF_HEAD, 0);
2185 if (error != NULL)
2186 return error;
2187 error = got_ref_resolve(&id, repo, head_ref);
2188 got_ref_close(head_ref);
2189 if (error != NULL)
2190 return error;
2191 error = got_object_open_as_commit(&commit, repo, id);
2192 } else {
2193 struct got_reference *ref;
2194 error = got_ref_open(&ref, repo, start_commit, 0);
2195 if (error == NULL) {
2196 int obj_type;
2197 error = got_ref_resolve(&id, repo, ref);
2198 got_ref_close(ref);
2199 if (error != NULL)
2200 goto done;
2201 error = got_object_get_type(&obj_type, repo, id);
2202 if (error != NULL)
2203 goto done;
2204 if (obj_type == GOT_OBJ_TYPE_TAG) {
2205 struct got_tag_object *tag;
2206 error = got_object_open_as_tag(&tag, repo, id);
2207 if (error != NULL)
2208 goto done;
2209 if (got_object_tag_get_object_type(tag) !=
2210 GOT_OBJ_TYPE_COMMIT) {
2211 got_object_tag_close(tag);
2212 error = got_error(GOT_ERR_OBJ_TYPE);
2213 goto done;
2215 free(id);
2216 id = got_object_id_dup(
2217 got_object_tag_get_object_id(tag));
2218 if (id == NULL)
2219 error = got_error_from_errno(
2220 "got_object_id_dup");
2221 got_object_tag_close(tag);
2222 if (error)
2223 goto done;
2224 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2225 error = got_error(GOT_ERR_OBJ_TYPE);
2226 goto done;
2228 error = got_object_open_as_commit(&commit, repo, id);
2229 if (error != NULL)
2230 goto done;
2232 if (commit == NULL) {
2233 error = got_repo_match_object_id_prefix(&id,
2234 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2235 if (error != NULL)
2236 return error;
2239 if (error != NULL)
2240 goto done;
2242 if (worktree) {
2243 const char *prefix = got_worktree_get_path_prefix(worktree);
2244 char *p;
2245 if (asprintf(&p, "%s%s%s", prefix,
2246 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2247 error = got_error_from_errno("asprintf");
2248 goto done;
2250 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2251 free(p);
2252 } else
2253 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2254 if (error != NULL)
2255 goto done;
2256 if (in_repo_path) {
2257 free(path);
2258 path = in_repo_path;
2261 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2262 if (error)
2263 goto done;
2265 error = print_commits(id, repo, path, show_patch, search_pattern,
2266 diff_context, limit, log_branches, &refs);
2267 done:
2268 free(path);
2269 free(repo_path);
2270 free(cwd);
2271 free(id);
2272 if (worktree)
2273 got_worktree_close(worktree);
2274 if (repo) {
2275 const struct got_error *repo_error;
2276 repo_error = got_repo_close(repo);
2277 if (error == NULL)
2278 error = repo_error;
2280 got_ref_list_free(&refs);
2281 return error;
2284 __dead static void
2285 usage_diff(void)
2287 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2288 "[-w] [object1 object2 | path]\n", getprogname());
2289 exit(1);
2292 struct print_diff_arg {
2293 struct got_repository *repo;
2294 struct got_worktree *worktree;
2295 int diff_context;
2296 const char *id_str;
2297 int header_shown;
2298 int diff_staged;
2299 int ignore_whitespace;
2302 static const struct got_error *
2303 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2304 const char *path, struct got_object_id *blob_id,
2305 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2306 int dirfd, const char *de_name)
2308 struct print_diff_arg *a = arg;
2309 const struct got_error *err = NULL;
2310 struct got_blob_object *blob1 = NULL;
2311 int fd = -1;
2312 FILE *f2 = NULL;
2313 char *abspath = NULL, *label1 = NULL;
2314 struct stat sb;
2316 if (a->diff_staged) {
2317 if (staged_status != GOT_STATUS_MODIFY &&
2318 staged_status != GOT_STATUS_ADD &&
2319 staged_status != GOT_STATUS_DELETE)
2320 return NULL;
2321 } else {
2322 if (staged_status == GOT_STATUS_DELETE)
2323 return NULL;
2324 if (status == GOT_STATUS_NONEXISTENT)
2325 return got_error_set_errno(ENOENT, path);
2326 if (status != GOT_STATUS_MODIFY &&
2327 status != GOT_STATUS_ADD &&
2328 status != GOT_STATUS_DELETE &&
2329 status != GOT_STATUS_CONFLICT)
2330 return NULL;
2333 if (!a->header_shown) {
2334 printf("diff %s %s%s\n", a->id_str,
2335 got_worktree_get_root_path(a->worktree),
2336 a->diff_staged ? " (staged changes)" : "");
2337 a->header_shown = 1;
2340 if (a->diff_staged) {
2341 const char *label1 = NULL, *label2 = NULL;
2342 switch (staged_status) {
2343 case GOT_STATUS_MODIFY:
2344 label1 = path;
2345 label2 = path;
2346 break;
2347 case GOT_STATUS_ADD:
2348 label2 = path;
2349 break;
2350 case GOT_STATUS_DELETE:
2351 label1 = path;
2352 break;
2353 default:
2354 return got_error(GOT_ERR_FILE_STATUS);
2356 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2357 label1, label2, a->diff_context, a->ignore_whitespace,
2358 a->repo, stdout);
2361 if (staged_status == GOT_STATUS_ADD ||
2362 staged_status == GOT_STATUS_MODIFY) {
2363 char *id_str;
2364 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2365 8192);
2366 if (err)
2367 goto done;
2368 err = got_object_id_str(&id_str, staged_blob_id);
2369 if (err)
2370 goto done;
2371 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2372 err = got_error_from_errno("asprintf");
2373 free(id_str);
2374 goto done;
2376 free(id_str);
2377 } else if (status != GOT_STATUS_ADD) {
2378 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2379 if (err)
2380 goto done;
2383 if (status != GOT_STATUS_DELETE) {
2384 if (asprintf(&abspath, "%s/%s",
2385 got_worktree_get_root_path(a->worktree), path) == -1) {
2386 err = got_error_from_errno("asprintf");
2387 goto done;
2390 if (dirfd != -1) {
2391 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2392 if (fd == -1) {
2393 err = got_error_from_errno2("openat", abspath);
2394 goto done;
2396 } else {
2397 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2398 if (fd == -1) {
2399 err = got_error_from_errno2("open", abspath);
2400 goto done;
2403 if (fstat(fd, &sb) == -1) {
2404 err = got_error_from_errno2("fstat", abspath);
2405 goto done;
2407 f2 = fdopen(fd, "r");
2408 if (f2 == NULL) {
2409 err = got_error_from_errno2("fdopen", abspath);
2410 goto done;
2412 fd = -1;
2413 } else
2414 sb.st_size = 0;
2416 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2417 a->diff_context, a->ignore_whitespace, stdout);
2418 done:
2419 if (blob1)
2420 got_object_blob_close(blob1);
2421 if (f2 && fclose(f2) == EOF && err == NULL)
2422 err = got_error_from_errno("fclose");
2423 if (fd != -1 && close(fd) == -1 && err == NULL)
2424 err = got_error_from_errno("close");
2425 free(abspath);
2426 return err;
2429 static const struct got_error *
2430 cmd_diff(int argc, char *argv[])
2432 const struct got_error *error;
2433 struct got_repository *repo = NULL;
2434 struct got_worktree *worktree = NULL;
2435 char *cwd = NULL, *repo_path = NULL;
2436 struct got_object_id *id1 = NULL, *id2 = NULL;
2437 const char *id_str1 = NULL, *id_str2 = NULL;
2438 char *label1 = NULL, *label2 = NULL;
2439 int type1, type2;
2440 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2441 const char *errstr;
2442 char *path = NULL;
2444 #ifndef PROFILE
2445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2446 NULL) == -1)
2447 err(1, "pledge");
2448 #endif
2450 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2451 switch (ch) {
2452 case 'C':
2453 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2454 &errstr);
2455 if (errstr != NULL)
2456 err(1, "-C option %s", errstr);
2457 break;
2458 case 'r':
2459 repo_path = realpath(optarg, NULL);
2460 if (repo_path == NULL)
2461 return got_error_from_errno2("realpath",
2462 optarg);
2463 got_path_strip_trailing_slashes(repo_path);
2464 break;
2465 case 's':
2466 diff_staged = 1;
2467 break;
2468 case 'w':
2469 ignore_whitespace = 1;
2470 break;
2471 default:
2472 usage_diff();
2473 /* NOTREACHED */
2477 argc -= optind;
2478 argv += optind;
2480 cwd = getcwd(NULL, 0);
2481 if (cwd == NULL) {
2482 error = got_error_from_errno("getcwd");
2483 goto done;
2485 error = got_worktree_open(&worktree, cwd);
2486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2487 goto done;
2488 if (argc <= 1) {
2489 if (worktree == NULL) {
2490 error = got_error(GOT_ERR_NOT_WORKTREE);
2491 goto done;
2493 if (repo_path)
2494 errx(1,
2495 "-r option can't be used when diffing a work tree");
2496 repo_path = strdup(got_worktree_get_repo_path(worktree));
2497 if (repo_path == NULL) {
2498 error = got_error_from_errno("strdup");
2499 goto done;
2501 if (argc == 1) {
2502 error = got_worktree_resolve_path(&path, worktree,
2503 argv[0]);
2504 if (error)
2505 goto done;
2506 } else {
2507 path = strdup("");
2508 if (path == NULL) {
2509 error = got_error_from_errno("strdup");
2510 goto done;
2513 } else if (argc == 2) {
2514 if (diff_staged)
2515 errx(1, "-s option can't be used when diffing "
2516 "objects in repository");
2517 id_str1 = argv[0];
2518 id_str2 = argv[1];
2519 if (worktree && repo_path == NULL) {
2520 repo_path =
2521 strdup(got_worktree_get_repo_path(worktree));
2522 if (repo_path == NULL) {
2523 error = got_error_from_errno("strdup");
2524 goto done;
2527 } else
2528 usage_diff();
2530 if (repo_path == NULL) {
2531 repo_path = getcwd(NULL, 0);
2532 if (repo_path == NULL)
2533 return got_error_from_errno("getcwd");
2536 error = got_repo_open(&repo, repo_path, NULL);
2537 free(repo_path);
2538 if (error != NULL)
2539 goto done;
2541 error = apply_unveil(got_repo_get_path(repo), 1,
2542 worktree ? got_worktree_get_root_path(worktree) : NULL);
2543 if (error)
2544 goto done;
2546 if (argc <= 1) {
2547 struct print_diff_arg arg;
2548 struct got_pathlist_head paths;
2549 char *id_str;
2551 TAILQ_INIT(&paths);
2553 error = got_object_id_str(&id_str,
2554 got_worktree_get_base_commit_id(worktree));
2555 if (error)
2556 goto done;
2557 arg.repo = repo;
2558 arg.worktree = worktree;
2559 arg.diff_context = diff_context;
2560 arg.id_str = id_str;
2561 arg.header_shown = 0;
2562 arg.diff_staged = diff_staged;
2563 arg.ignore_whitespace = ignore_whitespace;
2565 error = got_pathlist_append(&paths, path, NULL);
2566 if (error)
2567 goto done;
2569 error = got_worktree_status(worktree, &paths, repo, print_diff,
2570 &arg, check_cancelled, NULL);
2571 free(id_str);
2572 got_pathlist_free(&paths);
2573 goto done;
2576 error = got_repo_match_object_id(&id1, &label1, id_str1,
2577 GOT_OBJ_TYPE_ANY, 1, repo);
2578 if (error)
2579 goto done;
2581 error = got_repo_match_object_id(&id2, &label2, id_str2,
2582 GOT_OBJ_TYPE_ANY, 1, repo);
2583 if (error)
2584 goto done;
2586 error = got_object_get_type(&type1, repo, id1);
2587 if (error)
2588 goto done;
2590 error = got_object_get_type(&type2, repo, id2);
2591 if (error)
2592 goto done;
2594 if (type1 != type2) {
2595 error = got_error(GOT_ERR_OBJ_TYPE);
2596 goto done;
2599 switch (type1) {
2600 case GOT_OBJ_TYPE_BLOB:
2601 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2602 diff_context, ignore_whitespace, repo, stdout);
2603 break;
2604 case GOT_OBJ_TYPE_TREE:
2605 error = got_diff_objects_as_trees(id1, id2, "", "",
2606 diff_context, ignore_whitespace, repo, stdout);
2607 break;
2608 case GOT_OBJ_TYPE_COMMIT:
2609 printf("diff %s %s\n", label1, label2);
2610 error = got_diff_objects_as_commits(id1, id2, diff_context,
2611 ignore_whitespace, repo, stdout);
2612 break;
2613 default:
2614 error = got_error(GOT_ERR_OBJ_TYPE);
2616 done:
2617 free(label1);
2618 free(label2);
2619 free(id1);
2620 free(id2);
2621 free(path);
2622 if (worktree)
2623 got_worktree_close(worktree);
2624 if (repo) {
2625 const struct got_error *repo_error;
2626 repo_error = got_repo_close(repo);
2627 if (error == NULL)
2628 error = repo_error;
2630 return error;
2633 __dead static void
2634 usage_blame(void)
2636 fprintf(stderr,
2637 "usage: %s blame [-c commit] [-r repository-path] path\n",
2638 getprogname());
2639 exit(1);
2642 struct blame_line {
2643 int annotated;
2644 char *id_str;
2645 char *committer;
2646 char datebuf[11]; /* YYYY-MM-DD + NUL */
2649 struct blame_cb_args {
2650 struct blame_line *lines;
2651 int nlines;
2652 int nlines_prec;
2653 int lineno_cur;
2654 off_t *line_offsets;
2655 FILE *f;
2656 struct got_repository *repo;
2659 static const struct got_error *
2660 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2662 const struct got_error *err = NULL;
2663 struct blame_cb_args *a = arg;
2664 struct blame_line *bline;
2665 char *line = NULL;
2666 size_t linesize = 0;
2667 struct got_commit_object *commit = NULL;
2668 off_t offset;
2669 struct tm tm;
2670 time_t committer_time;
2672 if (nlines != a->nlines ||
2673 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2674 return got_error(GOT_ERR_RANGE);
2676 if (sigint_received)
2677 return got_error(GOT_ERR_ITER_COMPLETED);
2679 if (lineno == -1)
2680 return NULL; /* no change in this commit */
2682 /* Annotate this line. */
2683 bline = &a->lines[lineno - 1];
2684 if (bline->annotated)
2685 return NULL;
2686 err = got_object_id_str(&bline->id_str, id);
2687 if (err)
2688 return err;
2690 err = got_object_open_as_commit(&commit, a->repo, id);
2691 if (err)
2692 goto done;
2694 bline->committer = strdup(got_object_commit_get_committer(commit));
2695 if (bline->committer == NULL) {
2696 err = got_error_from_errno("strdup");
2697 goto done;
2700 committer_time = got_object_commit_get_committer_time(commit);
2701 if (localtime_r(&committer_time, &tm) == NULL)
2702 return got_error_from_errno("localtime_r");
2703 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2704 &tm) >= sizeof(bline->datebuf)) {
2705 err = got_error(GOT_ERR_NO_SPACE);
2706 goto done;
2708 bline->annotated = 1;
2710 /* Print lines annotated so far. */
2711 bline = &a->lines[a->lineno_cur - 1];
2712 if (!bline->annotated)
2713 goto done;
2715 offset = a->line_offsets[a->lineno_cur - 1];
2716 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2717 err = got_error_from_errno("fseeko");
2718 goto done;
2721 while (bline->annotated) {
2722 char *smallerthan, *at, *nl, *committer;
2723 size_t len;
2725 if (getline(&line, &linesize, a->f) == -1) {
2726 if (ferror(a->f))
2727 err = got_error_from_errno("getline");
2728 break;
2731 committer = bline->committer;
2732 smallerthan = strchr(committer, '<');
2733 if (smallerthan && smallerthan[1] != '\0')
2734 committer = smallerthan + 1;
2735 at = strchr(committer, '@');
2736 if (at)
2737 *at = '\0';
2738 len = strlen(committer);
2739 if (len >= 9)
2740 committer[8] = '\0';
2742 nl = strchr(line, '\n');
2743 if (nl)
2744 *nl = '\0';
2745 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2746 bline->id_str, bline->datebuf, committer, line);
2748 a->lineno_cur++;
2749 bline = &a->lines[a->lineno_cur - 1];
2751 done:
2752 if (commit)
2753 got_object_commit_close(commit);
2754 free(line);
2755 return err;
2758 static const struct got_error *
2759 cmd_blame(int argc, char *argv[])
2761 const struct got_error *error;
2762 struct got_repository *repo = NULL;
2763 struct got_worktree *worktree = NULL;
2764 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2765 struct got_object_id *obj_id = NULL;
2766 struct got_object_id *commit_id = NULL;
2767 struct got_blob_object *blob = NULL;
2768 char *commit_id_str = NULL;
2769 struct blame_cb_args bca;
2770 int ch, obj_type, i;
2771 size_t filesize;
2773 memset(&bca, 0, sizeof(bca));
2775 #ifndef PROFILE
2776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2777 NULL) == -1)
2778 err(1, "pledge");
2779 #endif
2781 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2782 switch (ch) {
2783 case 'c':
2784 commit_id_str = optarg;
2785 break;
2786 case 'r':
2787 repo_path = realpath(optarg, NULL);
2788 if (repo_path == NULL)
2789 return got_error_from_errno2("realpath",
2790 optarg);
2791 got_path_strip_trailing_slashes(repo_path);
2792 break;
2793 default:
2794 usage_blame();
2795 /* NOTREACHED */
2799 argc -= optind;
2800 argv += optind;
2802 if (argc == 1)
2803 path = argv[0];
2804 else
2805 usage_blame();
2807 cwd = getcwd(NULL, 0);
2808 if (cwd == NULL) {
2809 error = got_error_from_errno("getcwd");
2810 goto done;
2812 if (repo_path == NULL) {
2813 error = got_worktree_open(&worktree, cwd);
2814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2815 goto done;
2816 else
2817 error = NULL;
2818 if (worktree) {
2819 repo_path =
2820 strdup(got_worktree_get_repo_path(worktree));
2821 if (repo_path == NULL)
2822 error = got_error_from_errno("strdup");
2823 if (error)
2824 goto done;
2825 } else {
2826 repo_path = strdup(cwd);
2827 if (repo_path == NULL) {
2828 error = got_error_from_errno("strdup");
2829 goto done;
2834 error = got_repo_open(&repo, repo_path, NULL);
2835 if (error != NULL)
2836 goto done;
2838 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2839 if (error)
2840 goto done;
2842 if (worktree) {
2843 const char *prefix = got_worktree_get_path_prefix(worktree);
2844 char *p, *worktree_subdir = cwd +
2845 strlen(got_worktree_get_root_path(worktree));
2846 if (asprintf(&p, "%s%s%s%s%s",
2847 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2848 worktree_subdir, worktree_subdir[0] ? "/" : "",
2849 path) == -1) {
2850 error = got_error_from_errno("asprintf");
2851 goto done;
2853 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2854 free(p);
2855 } else {
2856 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2858 if (error)
2859 goto done;
2861 if (commit_id_str == NULL) {
2862 struct got_reference *head_ref;
2863 error = got_ref_open(&head_ref, repo, worktree ?
2864 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2865 if (error != NULL)
2866 goto done;
2867 error = got_ref_resolve(&commit_id, repo, head_ref);
2868 got_ref_close(head_ref);
2869 if (error != NULL)
2870 goto done;
2871 } else {
2872 error = got_repo_match_object_id(&commit_id, NULL,
2873 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2874 if (error)
2875 goto done;
2878 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2879 if (error)
2880 goto done;
2882 error = got_object_get_type(&obj_type, repo, obj_id);
2883 if (error)
2884 goto done;
2886 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2887 error = got_error(GOT_ERR_OBJ_TYPE);
2888 goto done;
2891 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2892 if (error)
2893 goto done;
2894 bca.f = got_opentemp();
2895 if (bca.f == NULL) {
2896 error = got_error_from_errno("got_opentemp");
2897 goto done;
2899 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2900 &bca.line_offsets, bca.f, blob);
2901 if (error || bca.nlines == 0)
2902 goto done;
2904 /* Don't include \n at EOF in the blame line count. */
2905 if (bca.line_offsets[bca.nlines - 1] == filesize)
2906 bca.nlines--;
2908 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2909 if (bca.lines == NULL) {
2910 error = got_error_from_errno("calloc");
2911 goto done;
2913 bca.lineno_cur = 1;
2914 bca.nlines_prec = 0;
2915 i = bca.nlines;
2916 while (i > 0) {
2917 i /= 10;
2918 bca.nlines_prec++;
2920 bca.repo = repo;
2922 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2923 check_cancelled, NULL);
2924 done:
2925 free(in_repo_path);
2926 free(repo_path);
2927 free(cwd);
2928 free(commit_id);
2929 free(obj_id);
2930 if (blob)
2931 got_object_blob_close(blob);
2932 if (worktree)
2933 got_worktree_close(worktree);
2934 if (repo) {
2935 const struct got_error *repo_error;
2936 repo_error = got_repo_close(repo);
2937 if (error == NULL)
2938 error = repo_error;
2940 if (bca.lines) {
2941 for (i = 0; i < bca.nlines; i++) {
2942 struct blame_line *bline = &bca.lines[i];
2943 free(bline->id_str);
2944 free(bline->committer);
2946 free(bca.lines);
2948 free(bca.line_offsets);
2949 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2950 error = got_error_from_errno("fclose");
2951 return error;
2954 __dead static void
2955 usage_tree(void)
2957 fprintf(stderr,
2958 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2959 getprogname());
2960 exit(1);
2963 static void
2964 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2965 const char *root_path)
2967 int is_root_path = (strcmp(path, root_path) == 0);
2968 const char *modestr = "";
2969 mode_t mode = got_tree_entry_get_mode(te);
2971 path += strlen(root_path);
2972 while (path[0] == '/')
2973 path++;
2975 if (got_object_tree_entry_is_submodule(te))
2976 modestr = "$";
2977 else if (S_ISLNK(mode))
2978 modestr = "@";
2979 else if (S_ISDIR(mode))
2980 modestr = "/";
2981 else if (mode & S_IXUSR)
2982 modestr = "*";
2984 printf("%s%s%s%s%s\n", id ? id : "", path,
2985 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2988 static const struct got_error *
2989 print_tree(const char *path, struct got_object_id *commit_id,
2990 int show_ids, int recurse, const char *root_path,
2991 struct got_repository *repo)
2993 const struct got_error *err = NULL;
2994 struct got_object_id *tree_id = NULL;
2995 struct got_tree_object *tree = NULL;
2996 int nentries, i;
2998 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2999 if (err)
3000 goto done;
3002 err = got_object_open_as_tree(&tree, repo, tree_id);
3003 if (err)
3004 goto done;
3005 nentries = got_object_tree_get_nentries(tree);
3006 for (i = 0; i < nentries; i++) {
3007 struct got_tree_entry *te;
3008 char *id = NULL;
3010 if (sigint_received || sigpipe_received)
3011 break;
3013 te = got_object_tree_get_entry(tree, i);
3014 if (show_ids) {
3015 char *id_str;
3016 err = got_object_id_str(&id_str,
3017 got_tree_entry_get_id(te));
3018 if (err)
3019 goto done;
3020 if (asprintf(&id, "%s ", id_str) == -1) {
3021 err = got_error_from_errno("asprintf");
3022 free(id_str);
3023 goto done;
3025 free(id_str);
3027 print_entry(te, id, path, root_path);
3028 free(id);
3030 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3031 char *child_path;
3032 if (asprintf(&child_path, "%s%s%s", path,
3033 path[0] == '/' && path[1] == '\0' ? "" : "/",
3034 got_tree_entry_get_name(te)) == -1) {
3035 err = got_error_from_errno("asprintf");
3036 goto done;
3038 err = print_tree(child_path, commit_id, show_ids, 1,
3039 root_path, repo);
3040 free(child_path);
3041 if (err)
3042 goto done;
3045 done:
3046 if (tree)
3047 got_object_tree_close(tree);
3048 free(tree_id);
3049 return err;
3052 static const struct got_error *
3053 cmd_tree(int argc, char *argv[])
3055 const struct got_error *error;
3056 struct got_repository *repo = NULL;
3057 struct got_worktree *worktree = NULL;
3058 const char *path;
3059 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3060 struct got_object_id *commit_id = NULL;
3061 char *commit_id_str = NULL;
3062 int show_ids = 0, recurse = 0;
3063 int ch;
3065 #ifndef PROFILE
3066 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3067 NULL) == -1)
3068 err(1, "pledge");
3069 #endif
3071 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3072 switch (ch) {
3073 case 'c':
3074 commit_id_str = optarg;
3075 break;
3076 case 'r':
3077 repo_path = realpath(optarg, NULL);
3078 if (repo_path == NULL)
3079 return got_error_from_errno2("realpath",
3080 optarg);
3081 got_path_strip_trailing_slashes(repo_path);
3082 break;
3083 case 'i':
3084 show_ids = 1;
3085 break;
3086 case 'R':
3087 recurse = 1;
3088 break;
3089 default:
3090 usage_tree();
3091 /* NOTREACHED */
3095 argc -= optind;
3096 argv += optind;
3098 if (argc == 1)
3099 path = argv[0];
3100 else if (argc > 1)
3101 usage_tree();
3102 else
3103 path = NULL;
3105 cwd = getcwd(NULL, 0);
3106 if (cwd == NULL) {
3107 error = got_error_from_errno("getcwd");
3108 goto done;
3110 if (repo_path == NULL) {
3111 error = got_worktree_open(&worktree, cwd);
3112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3113 goto done;
3114 else
3115 error = NULL;
3116 if (worktree) {
3117 repo_path =
3118 strdup(got_worktree_get_repo_path(worktree));
3119 if (repo_path == NULL)
3120 error = got_error_from_errno("strdup");
3121 if (error)
3122 goto done;
3123 } else {
3124 repo_path = strdup(cwd);
3125 if (repo_path == NULL) {
3126 error = got_error_from_errno("strdup");
3127 goto done;
3132 error = got_repo_open(&repo, repo_path, NULL);
3133 if (error != NULL)
3134 goto done;
3136 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3137 if (error)
3138 goto done;
3140 if (path == NULL) {
3141 if (worktree) {
3142 char *p, *worktree_subdir = cwd +
3143 strlen(got_worktree_get_root_path(worktree));
3144 if (asprintf(&p, "%s/%s",
3145 got_worktree_get_path_prefix(worktree),
3146 worktree_subdir) == -1) {
3147 error = got_error_from_errno("asprintf");
3148 goto done;
3150 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3151 free(p);
3152 if (error)
3153 goto done;
3154 } else
3155 path = "/";
3157 if (in_repo_path == NULL) {
3158 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3159 if (error != NULL)
3160 goto done;
3163 if (commit_id_str == NULL) {
3164 struct got_reference *head_ref;
3165 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3166 if (error != NULL)
3167 goto done;
3168 error = got_ref_resolve(&commit_id, repo, head_ref);
3169 got_ref_close(head_ref);
3170 if (error != NULL)
3171 goto done;
3172 } else {
3173 error = got_repo_match_object_id(&commit_id, NULL,
3174 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3175 if (error)
3176 goto done;
3179 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3180 in_repo_path, repo);
3181 done:
3182 free(in_repo_path);
3183 free(repo_path);
3184 free(cwd);
3185 free(commit_id);
3186 if (worktree)
3187 got_worktree_close(worktree);
3188 if (repo) {
3189 const struct got_error *repo_error;
3190 repo_error = got_repo_close(repo);
3191 if (error == NULL)
3192 error = repo_error;
3194 return error;
3197 __dead static void
3198 usage_status(void)
3200 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3201 exit(1);
3204 static const struct got_error *
3205 print_status(void *arg, unsigned char status, unsigned char staged_status,
3206 const char *path, struct got_object_id *blob_id,
3207 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3208 int dirfd, const char *de_name)
3210 if (status == staged_status && (status == GOT_STATUS_DELETE))
3211 status = GOT_STATUS_NO_CHANGE;
3212 printf("%c%c %s\n", status, staged_status, path);
3213 return NULL;
3216 static const struct got_error *
3217 cmd_status(int argc, char *argv[])
3219 const struct got_error *error = NULL;
3220 struct got_repository *repo = NULL;
3221 struct got_worktree *worktree = NULL;
3222 char *cwd = NULL;
3223 struct got_pathlist_head paths;
3224 struct got_pathlist_entry *pe;
3225 int ch;
3227 TAILQ_INIT(&paths);
3229 while ((ch = getopt(argc, argv, "")) != -1) {
3230 switch (ch) {
3231 default:
3232 usage_status();
3233 /* NOTREACHED */
3237 argc -= optind;
3238 argv += optind;
3240 #ifndef PROFILE
3241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3242 NULL) == -1)
3243 err(1, "pledge");
3244 #endif
3245 cwd = getcwd(NULL, 0);
3246 if (cwd == NULL) {
3247 error = got_error_from_errno("getcwd");
3248 goto done;
3251 error = got_worktree_open(&worktree, cwd);
3252 if (error != NULL)
3253 goto done;
3255 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3256 NULL);
3257 if (error != NULL)
3258 goto done;
3260 error = apply_unveil(got_repo_get_path(repo), 1,
3261 got_worktree_get_root_path(worktree));
3262 if (error)
3263 goto done;
3265 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3266 if (error)
3267 goto done;
3269 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3270 check_cancelled, NULL);
3271 done:
3272 TAILQ_FOREACH(pe, &paths, entry)
3273 free((char *)pe->path);
3274 got_pathlist_free(&paths);
3275 free(cwd);
3276 return error;
3279 __dead static void
3280 usage_ref(void)
3282 fprintf(stderr,
3283 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3284 getprogname());
3285 exit(1);
3288 static const struct got_error *
3289 list_refs(struct got_repository *repo)
3291 static const struct got_error *err = NULL;
3292 struct got_reflist_head refs;
3293 struct got_reflist_entry *re;
3295 SIMPLEQ_INIT(&refs);
3296 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3297 if (err)
3298 return err;
3300 SIMPLEQ_FOREACH(re, &refs, entry) {
3301 char *refstr;
3302 refstr = got_ref_to_str(re->ref);
3303 if (refstr == NULL)
3304 return got_error_from_errno("got_ref_to_str");
3305 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3306 free(refstr);
3309 got_ref_list_free(&refs);
3310 return NULL;
3313 static const struct got_error *
3314 delete_ref(struct got_repository *repo, const char *refname)
3316 const struct got_error *err = NULL;
3317 struct got_reference *ref;
3319 err = got_ref_open(&ref, repo, refname, 0);
3320 if (err)
3321 return err;
3323 err = got_ref_delete(ref, repo);
3324 got_ref_close(ref);
3325 return err;
3328 static const struct got_error *
3329 add_ref(struct got_repository *repo, const char *refname, const char *target)
3331 const struct got_error *err = NULL;
3332 struct got_object_id *id;
3333 struct got_reference *ref = NULL;
3336 * Don't let the user create a reference name with a leading '-'.
3337 * While technically a valid reference name, this case is usually
3338 * an unintended typo.
3340 if (refname[0] == '-')
3341 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3343 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3344 repo);
3345 if (err) {
3346 struct got_reference *target_ref;
3348 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3349 return err;
3350 err = got_ref_open(&target_ref, repo, target, 0);
3351 if (err)
3352 return err;
3353 err = got_ref_resolve(&id, repo, target_ref);
3354 got_ref_close(target_ref);
3355 if (err)
3356 return err;
3359 err = got_ref_alloc(&ref, refname, id);
3360 if (err)
3361 goto done;
3363 err = got_ref_write(ref, repo);
3364 done:
3365 if (ref)
3366 got_ref_close(ref);
3367 free(id);
3368 return err;
3371 static const struct got_error *
3372 add_symref(struct got_repository *repo, const char *refname, const char *target)
3374 const struct got_error *err = NULL;
3375 struct got_reference *ref = NULL;
3376 struct got_reference *target_ref = NULL;
3379 * Don't let the user create a reference name with a leading '-'.
3380 * While technically a valid reference name, this case is usually
3381 * an unintended typo.
3383 if (refname[0] == '-')
3384 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3386 err = got_ref_open(&target_ref, repo, target, 0);
3387 if (err)
3388 return err;
3390 err = got_ref_alloc_symref(&ref, refname, target_ref);
3391 if (err)
3392 goto done;
3394 err = got_ref_write(ref, repo);
3395 done:
3396 if (target_ref)
3397 got_ref_close(target_ref);
3398 if (ref)
3399 got_ref_close(ref);
3400 return err;
3403 static const struct got_error *
3404 cmd_ref(int argc, char *argv[])
3406 const struct got_error *error = NULL;
3407 struct got_repository *repo = NULL;
3408 struct got_worktree *worktree = NULL;
3409 char *cwd = NULL, *repo_path = NULL;
3410 int ch, do_list = 0, create_symref = 0;
3411 const char *delref = NULL;
3413 /* TODO: Add -s option for adding symbolic references. */
3414 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3415 switch (ch) {
3416 case 'd':
3417 delref = optarg;
3418 break;
3419 case 'r':
3420 repo_path = realpath(optarg, NULL);
3421 if (repo_path == NULL)
3422 return got_error_from_errno2("realpath",
3423 optarg);
3424 got_path_strip_trailing_slashes(repo_path);
3425 break;
3426 case 'l':
3427 do_list = 1;
3428 break;
3429 case 's':
3430 create_symref = 1;
3431 break;
3432 default:
3433 usage_ref();
3434 /* NOTREACHED */
3438 if (do_list && delref)
3439 errx(1, "-l and -d options are mutually exclusive\n");
3441 argc -= optind;
3442 argv += optind;
3444 if (do_list || delref) {
3445 if (create_symref)
3446 errx(1, "-s option cannot be used together with the "
3447 "-l or -d options");
3448 if (argc > 0)
3449 usage_ref();
3450 } else if (argc != 2)
3451 usage_ref();
3453 #ifndef PROFILE
3454 if (do_list) {
3455 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3456 NULL) == -1)
3457 err(1, "pledge");
3458 } else {
3459 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3460 "sendfd unveil", NULL) == -1)
3461 err(1, "pledge");
3463 #endif
3464 cwd = getcwd(NULL, 0);
3465 if (cwd == NULL) {
3466 error = got_error_from_errno("getcwd");
3467 goto done;
3470 if (repo_path == NULL) {
3471 error = got_worktree_open(&worktree, cwd);
3472 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3473 goto done;
3474 else
3475 error = NULL;
3476 if (worktree) {
3477 repo_path =
3478 strdup(got_worktree_get_repo_path(worktree));
3479 if (repo_path == NULL)
3480 error = got_error_from_errno("strdup");
3481 if (error)
3482 goto done;
3483 } else {
3484 repo_path = strdup(cwd);
3485 if (repo_path == NULL) {
3486 error = got_error_from_errno("strdup");
3487 goto done;
3492 error = got_repo_open(&repo, repo_path, NULL);
3493 if (error != NULL)
3494 goto done;
3496 error = apply_unveil(got_repo_get_path(repo), do_list,
3497 worktree ? got_worktree_get_root_path(worktree) : NULL);
3498 if (error)
3499 goto done;
3501 if (do_list)
3502 error = list_refs(repo);
3503 else if (delref)
3504 error = delete_ref(repo, delref);
3505 else if (create_symref)
3506 error = add_symref(repo, argv[0], argv[1]);
3507 else
3508 error = add_ref(repo, argv[0], argv[1]);
3509 done:
3510 if (repo)
3511 got_repo_close(repo);
3512 if (worktree)
3513 got_worktree_close(worktree);
3514 free(cwd);
3515 free(repo_path);
3516 return error;
3519 __dead static void
3520 usage_branch(void)
3522 fprintf(stderr,
3523 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3524 "[name]\n", getprogname());
3525 exit(1);
3528 static const struct got_error *
3529 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3530 struct got_reference *ref)
3532 const struct got_error *err = NULL;
3533 const char *refname, *marker = " ";
3534 char *refstr;
3536 refname = got_ref_get_name(ref);
3537 if (worktree && strcmp(refname,
3538 got_worktree_get_head_ref_name(worktree)) == 0) {
3539 struct got_object_id *id = NULL;
3541 err = got_ref_resolve(&id, repo, ref);
3542 if (err)
3543 return err;
3544 if (got_object_id_cmp(id,
3545 got_worktree_get_base_commit_id(worktree)) == 0)
3546 marker = "* ";
3547 else
3548 marker = "~ ";
3549 free(id);
3552 if (strncmp(refname, "refs/heads/", 11) == 0)
3553 refname += 11;
3554 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3555 refname += 18;
3557 refstr = got_ref_to_str(ref);
3558 if (refstr == NULL)
3559 return got_error_from_errno("got_ref_to_str");
3561 printf("%s%s: %s\n", marker, refname, refstr);
3562 free(refstr);
3563 return NULL;
3566 static const struct got_error *
3567 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3569 const char *refname;
3571 if (worktree == NULL)
3572 return got_error(GOT_ERR_NOT_WORKTREE);
3574 refname = got_worktree_get_head_ref_name(worktree);
3576 if (strncmp(refname, "refs/heads/", 11) == 0)
3577 refname += 11;
3578 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3579 refname += 18;
3581 printf("%s\n", refname);
3583 return NULL;
3586 static const struct got_error *
3587 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3589 static const struct got_error *err = NULL;
3590 struct got_reflist_head refs;
3591 struct got_reflist_entry *re;
3592 struct got_reference *temp_ref = NULL;
3593 int rebase_in_progress, histedit_in_progress;
3595 SIMPLEQ_INIT(&refs);
3597 if (worktree) {
3598 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3599 worktree);
3600 if (err)
3601 return err;
3603 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3604 worktree);
3605 if (err)
3606 return err;
3608 if (rebase_in_progress || histedit_in_progress) {
3609 err = got_ref_open(&temp_ref, repo,
3610 got_worktree_get_head_ref_name(worktree), 0);
3611 if (err)
3612 return err;
3613 list_branch(repo, worktree, temp_ref);
3614 got_ref_close(temp_ref);
3618 err = got_ref_list(&refs, repo, "refs/heads",
3619 got_ref_cmp_by_name, NULL);
3620 if (err)
3621 return err;
3623 SIMPLEQ_FOREACH(re, &refs, entry)
3624 list_branch(repo, worktree, re->ref);
3626 got_ref_list_free(&refs);
3627 return NULL;
3630 static const struct got_error *
3631 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3632 const char *branch_name)
3634 const struct got_error *err = NULL;
3635 struct got_reference *ref = NULL;
3636 char *refname;
3638 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3639 return got_error_from_errno("asprintf");
3641 err = got_ref_open(&ref, repo, refname, 0);
3642 if (err)
3643 goto done;
3645 if (worktree &&
3646 strcmp(got_worktree_get_head_ref_name(worktree),
3647 got_ref_get_name(ref)) == 0) {
3648 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3649 "will not delete this work tree's current branch");
3650 goto done;
3653 err = got_ref_delete(ref, repo);
3654 done:
3655 if (ref)
3656 got_ref_close(ref);
3657 free(refname);
3658 return err;
3661 static const struct got_error *
3662 add_branch(struct got_repository *repo, const char *branch_name,
3663 struct got_object_id *base_commit_id)
3665 const struct got_error *err = NULL;
3666 struct got_reference *ref = NULL;
3667 char *base_refname = NULL, *refname = NULL;
3670 * Don't let the user create a branch name with a leading '-'.
3671 * While technically a valid reference name, this case is usually
3672 * an unintended typo.
3674 if (branch_name[0] == '-')
3675 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3677 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3678 err = got_error_from_errno("asprintf");
3679 goto done;
3682 err = got_ref_open(&ref, repo, refname, 0);
3683 if (err == NULL) {
3684 err = got_error(GOT_ERR_BRANCH_EXISTS);
3685 goto done;
3686 } else if (err->code != GOT_ERR_NOT_REF)
3687 goto done;
3689 err = got_ref_alloc(&ref, refname, base_commit_id);
3690 if (err)
3691 goto done;
3693 err = got_ref_write(ref, repo);
3694 done:
3695 if (ref)
3696 got_ref_close(ref);
3697 free(base_refname);
3698 free(refname);
3699 return err;
3702 static const struct got_error *
3703 cmd_branch(int argc, char *argv[])
3705 const struct got_error *error = NULL;
3706 struct got_repository *repo = NULL;
3707 struct got_worktree *worktree = NULL;
3708 char *cwd = NULL, *repo_path = NULL;
3709 int ch, do_list = 0, do_show = 0, do_update = 1;
3710 const char *delref = NULL, *commit_id_arg = NULL;
3711 struct got_reference *ref = NULL;
3712 struct got_pathlist_head paths;
3713 struct got_pathlist_entry *pe;
3714 struct got_object_id *commit_id = NULL;
3715 char *commit_id_str = NULL;
3717 TAILQ_INIT(&paths);
3719 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3720 switch (ch) {
3721 case 'c':
3722 commit_id_arg = optarg;
3723 break;
3724 case 'd':
3725 delref = optarg;
3726 break;
3727 case 'r':
3728 repo_path = realpath(optarg, NULL);
3729 if (repo_path == NULL)
3730 return got_error_from_errno2("realpath",
3731 optarg);
3732 got_path_strip_trailing_slashes(repo_path);
3733 break;
3734 case 'l':
3735 do_list = 1;
3736 break;
3737 case 'n':
3738 do_update = 0;
3739 break;
3740 default:
3741 usage_branch();
3742 /* NOTREACHED */
3746 if (do_list && delref)
3747 errx(1, "-l and -d options are mutually exclusive\n");
3749 argc -= optind;
3750 argv += optind;
3752 if (!do_list && !delref && argc == 0)
3753 do_show = 1;
3755 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3756 errx(1, "-c option can only be used when creating a branch");
3758 if (do_list || delref) {
3759 if (argc > 0)
3760 usage_branch();
3761 } else if (!do_show && argc != 1)
3762 usage_branch();
3764 #ifndef PROFILE
3765 if (do_list || do_show) {
3766 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3767 NULL) == -1)
3768 err(1, "pledge");
3769 } else {
3770 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3771 "sendfd unveil", NULL) == -1)
3772 err(1, "pledge");
3774 #endif
3775 cwd = getcwd(NULL, 0);
3776 if (cwd == NULL) {
3777 error = got_error_from_errno("getcwd");
3778 goto done;
3781 if (repo_path == NULL) {
3782 error = got_worktree_open(&worktree, cwd);
3783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3784 goto done;
3785 else
3786 error = NULL;
3787 if (worktree) {
3788 repo_path =
3789 strdup(got_worktree_get_repo_path(worktree));
3790 if (repo_path == NULL)
3791 error = got_error_from_errno("strdup");
3792 if (error)
3793 goto done;
3794 } else {
3795 repo_path = strdup(cwd);
3796 if (repo_path == NULL) {
3797 error = got_error_from_errno("strdup");
3798 goto done;
3803 error = got_repo_open(&repo, repo_path, NULL);
3804 if (error != NULL)
3805 goto done;
3807 error = apply_unveil(got_repo_get_path(repo), do_list,
3808 worktree ? got_worktree_get_root_path(worktree) : NULL);
3809 if (error)
3810 goto done;
3812 if (do_show)
3813 error = show_current_branch(repo, worktree);
3814 else if (do_list)
3815 error = list_branches(repo, worktree);
3816 else if (delref)
3817 error = delete_branch(repo, worktree, delref);
3818 else {
3819 if (commit_id_arg == NULL)
3820 commit_id_arg = worktree ?
3821 got_worktree_get_head_ref_name(worktree) :
3822 GOT_REF_HEAD;
3823 error = got_repo_match_object_id(&commit_id, NULL,
3824 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3825 if (error)
3826 goto done;
3827 error = add_branch(repo, argv[0], commit_id);
3828 if (error)
3829 goto done;
3830 if (worktree && do_update) {
3831 int did_something = 0;
3832 char *branch_refname = NULL;
3834 error = got_object_id_str(&commit_id_str, commit_id);
3835 if (error)
3836 goto done;
3837 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3838 worktree);
3839 if (error)
3840 goto done;
3841 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3842 == -1) {
3843 error = got_error_from_errno("asprintf");
3844 goto done;
3846 error = got_ref_open(&ref, repo, branch_refname, 0);
3847 free(branch_refname);
3848 if (error)
3849 goto done;
3850 error = switch_head_ref(ref, commit_id, worktree,
3851 repo);
3852 if (error)
3853 goto done;
3854 error = got_worktree_set_base_commit_id(worktree, repo,
3855 commit_id);
3856 if (error)
3857 goto done;
3858 error = got_worktree_checkout_files(worktree, &paths,
3859 repo, update_progress, &did_something,
3860 check_cancelled, NULL);
3861 if (error)
3862 goto done;
3863 if (did_something)
3864 printf("Updated to commit %s\n", commit_id_str);
3867 done:
3868 if (ref)
3869 got_ref_close(ref);
3870 if (repo)
3871 got_repo_close(repo);
3872 if (worktree)
3873 got_worktree_close(worktree);
3874 free(cwd);
3875 free(repo_path);
3876 free(commit_id);
3877 free(commit_id_str);
3878 TAILQ_FOREACH(pe, &paths, entry)
3879 free((char *)pe->path);
3880 got_pathlist_free(&paths);
3881 return error;
3885 __dead static void
3886 usage_tag(void)
3888 fprintf(stderr,
3889 "usage: %s tag [-c commit] [-r repository] [-l] "
3890 "[-m message] name\n", getprogname());
3891 exit(1);
3894 #if 0
3895 static const struct got_error *
3896 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3898 const struct got_error *err = NULL;
3899 struct got_reflist_entry *re, *se, *new;
3900 struct got_object_id *re_id, *se_id;
3901 struct got_tag_object *re_tag, *se_tag;
3902 time_t re_time, se_time;
3904 SIMPLEQ_FOREACH(re, tags, entry) {
3905 se = SIMPLEQ_FIRST(sorted);
3906 if (se == NULL) {
3907 err = got_reflist_entry_dup(&new, re);
3908 if (err)
3909 return err;
3910 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3911 continue;
3912 } else {
3913 err = got_ref_resolve(&re_id, repo, re->ref);
3914 if (err)
3915 break;
3916 err = got_object_open_as_tag(&re_tag, repo, re_id);
3917 free(re_id);
3918 if (err)
3919 break;
3920 re_time = got_object_tag_get_tagger_time(re_tag);
3921 got_object_tag_close(re_tag);
3924 while (se) {
3925 err = got_ref_resolve(&se_id, repo, re->ref);
3926 if (err)
3927 break;
3928 err = got_object_open_as_tag(&se_tag, repo, se_id);
3929 free(se_id);
3930 if (err)
3931 break;
3932 se_time = got_object_tag_get_tagger_time(se_tag);
3933 got_object_tag_close(se_tag);
3935 if (se_time > re_time) {
3936 err = got_reflist_entry_dup(&new, re);
3937 if (err)
3938 return err;
3939 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3940 break;
3942 se = SIMPLEQ_NEXT(se, entry);
3943 continue;
3946 done:
3947 return err;
3949 #endif
3951 static const struct got_error *
3952 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3954 static const struct got_error *err = NULL;
3955 struct got_reflist_head refs;
3956 struct got_reflist_entry *re;
3958 SIMPLEQ_INIT(&refs);
3960 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3961 if (err)
3962 return err;
3964 SIMPLEQ_FOREACH(re, &refs, entry) {
3965 const char *refname;
3966 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3967 char datebuf[26];
3968 const char *tagger;
3969 time_t tagger_time;
3970 struct got_object_id *id;
3971 struct got_tag_object *tag;
3972 struct got_commit_object *commit = NULL;
3974 refname = got_ref_get_name(re->ref);
3975 if (strncmp(refname, "refs/tags/", 10) != 0)
3976 continue;
3977 refname += 10;
3978 refstr = got_ref_to_str(re->ref);
3979 if (refstr == NULL) {
3980 err = got_error_from_errno("got_ref_to_str");
3981 break;
3983 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3984 free(refstr);
3986 err = got_ref_resolve(&id, repo, re->ref);
3987 if (err)
3988 break;
3989 err = got_object_open_as_tag(&tag, repo, id);
3990 if (err) {
3991 if (err->code != GOT_ERR_OBJ_TYPE) {
3992 free(id);
3993 break;
3995 /* "lightweight" tag */
3996 err = got_object_open_as_commit(&commit, repo, id);
3997 if (err) {
3998 free(id);
3999 break;
4001 tagger = got_object_commit_get_committer(commit);
4002 tagger_time =
4003 got_object_commit_get_committer_time(commit);
4004 err = got_object_id_str(&id_str, id);
4005 free(id);
4006 if (err)
4007 break;
4008 } else {
4009 free(id);
4010 tagger = got_object_tag_get_tagger(tag);
4011 tagger_time = got_object_tag_get_tagger_time(tag);
4012 err = got_object_id_str(&id_str,
4013 got_object_tag_get_object_id(tag));
4014 if (err)
4015 break;
4017 printf("from: %s\n", tagger);
4018 datestr = get_datestr(&tagger_time, datebuf);
4019 if (datestr)
4020 printf("date: %s UTC\n", datestr);
4021 if (commit)
4022 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4023 else {
4024 switch (got_object_tag_get_object_type(tag)) {
4025 case GOT_OBJ_TYPE_BLOB:
4026 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4027 id_str);
4028 break;
4029 case GOT_OBJ_TYPE_TREE:
4030 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4031 id_str);
4032 break;
4033 case GOT_OBJ_TYPE_COMMIT:
4034 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4035 id_str);
4036 break;
4037 case GOT_OBJ_TYPE_TAG:
4038 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4039 id_str);
4040 break;
4041 default:
4042 break;
4045 free(id_str);
4046 if (commit) {
4047 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4048 if (err)
4049 break;
4050 got_object_commit_close(commit);
4051 } else {
4052 tagmsg0 = strdup(got_object_tag_get_message(tag));
4053 got_object_tag_close(tag);
4054 if (tagmsg0 == NULL) {
4055 err = got_error_from_errno("strdup");
4056 break;
4060 tagmsg = tagmsg0;
4061 do {
4062 line = strsep(&tagmsg, "\n");
4063 if (line)
4064 printf(" %s\n", line);
4065 } while (line);
4066 free(tagmsg0);
4069 got_ref_list_free(&refs);
4070 return NULL;
4073 static const struct got_error *
4074 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4075 const char *tag_name, const char *repo_path)
4077 const struct got_error *err = NULL;
4078 char *template = NULL, *initial_content = NULL;
4079 char *editor = NULL;
4080 int fd = -1;
4082 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4083 err = got_error_from_errno("asprintf");
4084 goto done;
4087 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4088 commit_id_str, tag_name) == -1) {
4089 err = got_error_from_errno("asprintf");
4090 goto done;
4093 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4094 if (err)
4095 goto done;
4097 dprintf(fd, initial_content);
4098 close(fd);
4100 err = get_editor(&editor);
4101 if (err)
4102 goto done;
4103 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4104 done:
4105 free(initial_content);
4106 free(template);
4107 free(editor);
4109 /* Editor is done; we can now apply unveil(2) */
4110 if (err == NULL) {
4111 err = apply_unveil(repo_path, 0, NULL);
4112 if (err) {
4113 free(*tagmsg);
4114 *tagmsg = NULL;
4117 return err;
4120 static const struct got_error *
4121 add_tag(struct got_repository *repo, const char *tag_name,
4122 const char *commit_arg, const char *tagmsg_arg)
4124 const struct got_error *err = NULL;
4125 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4126 char *label = NULL, *commit_id_str = NULL;
4127 struct got_reference *ref = NULL;
4128 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4129 char *tagmsg_path = NULL, *tag_id_str = NULL;
4130 int preserve_tagmsg = 0;
4133 * Don't let the user create a tag name with a leading '-'.
4134 * While technically a valid reference name, this case is usually
4135 * an unintended typo.
4137 if (tag_name[0] == '-')
4138 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4140 err = get_author(&tagger, repo);
4141 if (err)
4142 return err;
4144 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4145 GOT_OBJ_TYPE_COMMIT, 1, repo);
4146 if (err)
4147 goto done;
4149 err = got_object_id_str(&commit_id_str, commit_id);
4150 if (err)
4151 goto done;
4153 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4154 refname = strdup(tag_name);
4155 if (refname == NULL) {
4156 err = got_error_from_errno("strdup");
4157 goto done;
4159 tag_name += 10;
4160 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4161 err = got_error_from_errno("asprintf");
4162 goto done;
4165 err = got_ref_open(&ref, repo, refname, 0);
4166 if (err == NULL) {
4167 err = got_error(GOT_ERR_TAG_EXISTS);
4168 goto done;
4169 } else if (err->code != GOT_ERR_NOT_REF)
4170 goto done;
4172 if (tagmsg_arg == NULL) {
4173 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4174 tag_name, got_repo_get_path(repo));
4175 if (err) {
4176 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4177 tagmsg_path != NULL)
4178 preserve_tagmsg = 1;
4179 goto done;
4183 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4184 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4185 if (err) {
4186 if (tagmsg_path)
4187 preserve_tagmsg = 1;
4188 goto done;
4191 err = got_ref_alloc(&ref, refname, tag_id);
4192 if (err) {
4193 if (tagmsg_path)
4194 preserve_tagmsg = 1;
4195 goto done;
4198 err = got_ref_write(ref, repo);
4199 if (err) {
4200 if (tagmsg_path)
4201 preserve_tagmsg = 1;
4202 goto done;
4205 err = got_object_id_str(&tag_id_str, tag_id);
4206 if (err) {
4207 if (tagmsg_path)
4208 preserve_tagmsg = 1;
4209 goto done;
4211 printf("Created tag %s\n", tag_id_str);
4212 done:
4213 if (preserve_tagmsg) {
4214 fprintf(stderr, "%s: tag message preserved in %s\n",
4215 getprogname(), tagmsg_path);
4216 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4217 err = got_error_from_errno2("unlink", tagmsg_path);
4218 free(tag_id_str);
4219 if (ref)
4220 got_ref_close(ref);
4221 free(commit_id);
4222 free(commit_id_str);
4223 free(refname);
4224 free(tagmsg);
4225 free(tagmsg_path);
4226 free(tagger);
4227 return err;
4230 static const struct got_error *
4231 cmd_tag(int argc, char *argv[])
4233 const struct got_error *error = NULL;
4234 struct got_repository *repo = NULL;
4235 struct got_worktree *worktree = NULL;
4236 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4237 char *gitconfig_path = NULL;
4238 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4239 int ch, do_list = 0;
4241 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4242 switch (ch) {
4243 case 'c':
4244 commit_id_arg = optarg;
4245 break;
4246 case 'm':
4247 tagmsg = optarg;
4248 break;
4249 case 'r':
4250 repo_path = realpath(optarg, NULL);
4251 if (repo_path == NULL)
4252 return got_error_from_errno2("realpath",
4253 optarg);
4254 got_path_strip_trailing_slashes(repo_path);
4255 break;
4256 case 'l':
4257 do_list = 1;
4258 break;
4259 default:
4260 usage_tag();
4261 /* NOTREACHED */
4265 argc -= optind;
4266 argv += optind;
4268 if (do_list) {
4269 if (commit_id_arg != NULL)
4270 errx(1, "-c option can only be used when creating a tag");
4271 if (tagmsg)
4272 errx(1, "-l and -m options are mutually exclusive");
4273 if (argc > 0)
4274 usage_tag();
4275 } else if (argc != 1)
4276 usage_tag();
4278 tag_name = argv[0];
4280 #ifndef PROFILE
4281 if (do_list) {
4282 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4283 NULL) == -1)
4284 err(1, "pledge");
4285 } else {
4286 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4287 "sendfd unveil", NULL) == -1)
4288 err(1, "pledge");
4290 #endif
4291 cwd = getcwd(NULL, 0);
4292 if (cwd == NULL) {
4293 error = got_error_from_errno("getcwd");
4294 goto done;
4297 if (repo_path == NULL) {
4298 error = got_worktree_open(&worktree, cwd);
4299 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4300 goto done;
4301 else
4302 error = NULL;
4303 if (worktree) {
4304 repo_path =
4305 strdup(got_worktree_get_repo_path(worktree));
4306 if (repo_path == NULL)
4307 error = got_error_from_errno("strdup");
4308 if (error)
4309 goto done;
4310 } else {
4311 repo_path = strdup(cwd);
4312 if (repo_path == NULL) {
4313 error = got_error_from_errno("strdup");
4314 goto done;
4319 if (do_list) {
4320 error = got_repo_open(&repo, repo_path, NULL);
4321 if (error != NULL)
4322 goto done;
4323 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4324 if (error)
4325 goto done;
4326 error = list_tags(repo, worktree);
4327 } else {
4328 error = get_gitconfig_path(&gitconfig_path);
4329 if (error)
4330 goto done;
4331 error = got_repo_open(&repo, repo_path, gitconfig_path);
4332 if (error != NULL)
4333 goto done;
4335 if (tagmsg) {
4336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4337 if (error)
4338 goto done;
4341 if (commit_id_arg == NULL) {
4342 struct got_reference *head_ref;
4343 struct got_object_id *commit_id;
4344 error = got_ref_open(&head_ref, repo,
4345 worktree ? got_worktree_get_head_ref_name(worktree)
4346 : GOT_REF_HEAD, 0);
4347 if (error)
4348 goto done;
4349 error = got_ref_resolve(&commit_id, repo, head_ref);
4350 got_ref_close(head_ref);
4351 if (error)
4352 goto done;
4353 error = got_object_id_str(&commit_id_str, commit_id);
4354 free(commit_id);
4355 if (error)
4356 goto done;
4359 error = add_tag(repo, tag_name,
4360 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4362 done:
4363 if (repo)
4364 got_repo_close(repo);
4365 if (worktree)
4366 got_worktree_close(worktree);
4367 free(cwd);
4368 free(repo_path);
4369 free(gitconfig_path);
4370 free(commit_id_str);
4371 return error;
4374 __dead static void
4375 usage_add(void)
4377 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4378 getprogname());
4379 exit(1);
4382 static const struct got_error *
4383 add_progress(void *arg, unsigned char status, const char *path)
4385 while (path[0] == '/')
4386 path++;
4387 printf("%c %s\n", status, path);
4388 return NULL;
4391 static const struct got_error *
4392 cmd_add(int argc, char *argv[])
4394 const struct got_error *error = NULL;
4395 struct got_repository *repo = NULL;
4396 struct got_worktree *worktree = NULL;
4397 char *cwd = NULL;
4398 struct got_pathlist_head paths;
4399 struct got_pathlist_entry *pe;
4400 int ch, can_recurse = 0, no_ignores = 0;
4402 TAILQ_INIT(&paths);
4404 while ((ch = getopt(argc, argv, "IR")) != -1) {
4405 switch (ch) {
4406 case 'I':
4407 no_ignores = 1;
4408 break;
4409 case 'R':
4410 can_recurse = 1;
4411 break;
4412 default:
4413 usage_add();
4414 /* NOTREACHED */
4418 argc -= optind;
4419 argv += optind;
4421 #ifndef PROFILE
4422 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4423 NULL) == -1)
4424 err(1, "pledge");
4425 #endif
4426 if (argc < 1)
4427 usage_add();
4429 cwd = getcwd(NULL, 0);
4430 if (cwd == NULL) {
4431 error = got_error_from_errno("getcwd");
4432 goto done;
4435 error = got_worktree_open(&worktree, cwd);
4436 if (error)
4437 goto done;
4439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4440 NULL);
4441 if (error != NULL)
4442 goto done;
4444 error = apply_unveil(got_repo_get_path(repo), 1,
4445 got_worktree_get_root_path(worktree));
4446 if (error)
4447 goto done;
4449 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4450 if (error)
4451 goto done;
4453 if (!can_recurse && no_ignores) {
4454 error = got_error_msg(GOT_ERR_BAD_PATH,
4455 "disregarding ignores requires -R option");
4456 goto done;
4460 if (!can_recurse) {
4461 char *ondisk_path;
4462 struct stat sb;
4463 TAILQ_FOREACH(pe, &paths, entry) {
4464 if (asprintf(&ondisk_path, "%s/%s",
4465 got_worktree_get_root_path(worktree),
4466 pe->path) == -1) {
4467 error = got_error_from_errno("asprintf");
4468 goto done;
4470 if (lstat(ondisk_path, &sb) == -1) {
4471 if (errno == ENOENT) {
4472 free(ondisk_path);
4473 continue;
4475 error = got_error_from_errno2("lstat",
4476 ondisk_path);
4477 free(ondisk_path);
4478 goto done;
4480 free(ondisk_path);
4481 if (S_ISDIR(sb.st_mode)) {
4482 error = got_error_msg(GOT_ERR_BAD_PATH,
4483 "adding directories requires -R option");
4484 goto done;
4489 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4490 NULL, repo, no_ignores);
4491 done:
4492 if (repo)
4493 got_repo_close(repo);
4494 if (worktree)
4495 got_worktree_close(worktree);
4496 TAILQ_FOREACH(pe, &paths, entry)
4497 free((char *)pe->path);
4498 got_pathlist_free(&paths);
4499 free(cwd);
4500 return error;
4503 __dead static void
4504 usage_remove(void)
4506 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4507 getprogname());
4508 exit(1);
4511 static const struct got_error *
4512 print_remove_status(void *arg, unsigned char status,
4513 unsigned char staged_status, const char *path)
4515 while (path[0] == '/')
4516 path++;
4517 if (status == GOT_STATUS_NONEXISTENT)
4518 return NULL;
4519 if (status == staged_status && (status == GOT_STATUS_DELETE))
4520 status = GOT_STATUS_NO_CHANGE;
4521 printf("%c%c %s\n", status, staged_status, path);
4522 return NULL;
4525 static const struct got_error *
4526 cmd_remove(int argc, char *argv[])
4528 const struct got_error *error = NULL;
4529 struct got_worktree *worktree = NULL;
4530 struct got_repository *repo = NULL;
4531 char *cwd = NULL;
4532 struct got_pathlist_head paths;
4533 struct got_pathlist_entry *pe;
4534 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4536 TAILQ_INIT(&paths);
4538 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4539 switch (ch) {
4540 case 'f':
4541 delete_local_mods = 1;
4542 break;
4543 case 'k':
4544 keep_on_disk = 1;
4545 break;
4546 case 'R':
4547 can_recurse = 1;
4548 break;
4549 default:
4550 usage_remove();
4551 /* NOTREACHED */
4555 argc -= optind;
4556 argv += optind;
4558 #ifndef PROFILE
4559 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4560 NULL) == -1)
4561 err(1, "pledge");
4562 #endif
4563 if (argc < 1)
4564 usage_remove();
4566 cwd = getcwd(NULL, 0);
4567 if (cwd == NULL) {
4568 error = got_error_from_errno("getcwd");
4569 goto done;
4571 error = got_worktree_open(&worktree, cwd);
4572 if (error)
4573 goto done;
4575 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4576 NULL);
4577 if (error)
4578 goto done;
4580 error = apply_unveil(got_repo_get_path(repo), 1,
4581 got_worktree_get_root_path(worktree));
4582 if (error)
4583 goto done;
4585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4586 if (error)
4587 goto done;
4589 if (!can_recurse) {
4590 char *ondisk_path;
4591 struct stat sb;
4592 TAILQ_FOREACH(pe, &paths, entry) {
4593 if (asprintf(&ondisk_path, "%s/%s",
4594 got_worktree_get_root_path(worktree),
4595 pe->path) == -1) {
4596 error = got_error_from_errno("asprintf");
4597 goto done;
4599 if (lstat(ondisk_path, &sb) == -1) {
4600 if (errno == ENOENT) {
4601 free(ondisk_path);
4602 continue;
4604 error = got_error_from_errno2("lstat",
4605 ondisk_path);
4606 free(ondisk_path);
4607 goto done;
4609 free(ondisk_path);
4610 if (S_ISDIR(sb.st_mode)) {
4611 error = got_error_msg(GOT_ERR_BAD_PATH,
4612 "removing directories requires -R option");
4613 goto done;
4618 error = got_worktree_schedule_delete(worktree, &paths,
4619 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4620 done:
4621 if (repo)
4622 got_repo_close(repo);
4623 if (worktree)
4624 got_worktree_close(worktree);
4625 TAILQ_FOREACH(pe, &paths, entry)
4626 free((char *)pe->path);
4627 got_pathlist_free(&paths);
4628 free(cwd);
4629 return error;
4632 __dead static void
4633 usage_revert(void)
4635 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4636 "path ...\n", getprogname());
4637 exit(1);
4640 static const struct got_error *
4641 revert_progress(void *arg, unsigned char status, const char *path)
4643 if (status == GOT_STATUS_UNVERSIONED)
4644 return NULL;
4646 while (path[0] == '/')
4647 path++;
4648 printf("%c %s\n", status, path);
4649 return NULL;
4652 struct choose_patch_arg {
4653 FILE *patch_script_file;
4654 const char *action;
4657 static const struct got_error *
4658 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4659 int nchanges, const char *action)
4661 char *line = NULL;
4662 size_t linesize = 0;
4663 ssize_t linelen;
4665 switch (status) {
4666 case GOT_STATUS_ADD:
4667 printf("A %s\n%s this addition? [y/n] ", path, action);
4668 break;
4669 case GOT_STATUS_DELETE:
4670 printf("D %s\n%s this deletion? [y/n] ", path, action);
4671 break;
4672 case GOT_STATUS_MODIFY:
4673 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4674 return got_error_from_errno("fseek");
4675 printf(GOT_COMMIT_SEP_STR);
4676 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4677 printf("%s", line);
4678 if (ferror(patch_file))
4679 return got_error_from_errno("getline");
4680 printf(GOT_COMMIT_SEP_STR);
4681 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4682 path, n, nchanges, action);
4683 break;
4684 default:
4685 return got_error_path(path, GOT_ERR_FILE_STATUS);
4688 return NULL;
4691 static const struct got_error *
4692 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4693 FILE *patch_file, int n, int nchanges)
4695 const struct got_error *err = NULL;
4696 char *line = NULL;
4697 size_t linesize = 0;
4698 ssize_t linelen;
4699 int resp = ' ';
4700 struct choose_patch_arg *a = arg;
4702 *choice = GOT_PATCH_CHOICE_NONE;
4704 if (a->patch_script_file) {
4705 char *nl;
4706 err = show_change(status, path, patch_file, n, nchanges,
4707 a->action);
4708 if (err)
4709 return err;
4710 linelen = getline(&line, &linesize, a->patch_script_file);
4711 if (linelen == -1) {
4712 if (ferror(a->patch_script_file))
4713 return got_error_from_errno("getline");
4714 return NULL;
4716 nl = strchr(line, '\n');
4717 if (nl)
4718 *nl = '\0';
4719 if (strcmp(line, "y") == 0) {
4720 *choice = GOT_PATCH_CHOICE_YES;
4721 printf("y\n");
4722 } else if (strcmp(line, "n") == 0) {
4723 *choice = GOT_PATCH_CHOICE_NO;
4724 printf("n\n");
4725 } else if (strcmp(line, "q") == 0 &&
4726 status == GOT_STATUS_MODIFY) {
4727 *choice = GOT_PATCH_CHOICE_QUIT;
4728 printf("q\n");
4729 } else
4730 printf("invalid response '%s'\n", line);
4731 free(line);
4732 return NULL;
4735 while (resp != 'y' && resp != 'n' && resp != 'q') {
4736 err = show_change(status, path, patch_file, n, nchanges,
4737 a->action);
4738 if (err)
4739 return err;
4740 resp = getchar();
4741 if (resp == '\n')
4742 resp = getchar();
4743 if (status == GOT_STATUS_MODIFY) {
4744 if (resp != 'y' && resp != 'n' && resp != 'q') {
4745 printf("invalid response '%c'\n", resp);
4746 resp = ' ';
4748 } else if (resp != 'y' && resp != 'n') {
4749 printf("invalid response '%c'\n", resp);
4750 resp = ' ';
4754 if (resp == 'y')
4755 *choice = GOT_PATCH_CHOICE_YES;
4756 else if (resp == 'n')
4757 *choice = GOT_PATCH_CHOICE_NO;
4758 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4759 *choice = GOT_PATCH_CHOICE_QUIT;
4761 return NULL;
4765 static const struct got_error *
4766 cmd_revert(int argc, char *argv[])
4768 const struct got_error *error = NULL;
4769 struct got_worktree *worktree = NULL;
4770 struct got_repository *repo = NULL;
4771 char *cwd = NULL, *path = NULL;
4772 struct got_pathlist_head paths;
4773 struct got_pathlist_entry *pe;
4774 int ch, can_recurse = 0, pflag = 0;
4775 FILE *patch_script_file = NULL;
4776 const char *patch_script_path = NULL;
4777 struct choose_patch_arg cpa;
4779 TAILQ_INIT(&paths);
4781 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4782 switch (ch) {
4783 case 'p':
4784 pflag = 1;
4785 break;
4786 case 'F':
4787 patch_script_path = optarg;
4788 break;
4789 case 'R':
4790 can_recurse = 1;
4791 break;
4792 default:
4793 usage_revert();
4794 /* NOTREACHED */
4798 argc -= optind;
4799 argv += optind;
4801 #ifndef PROFILE
4802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4803 "unveil", NULL) == -1)
4804 err(1, "pledge");
4805 #endif
4806 if (argc < 1)
4807 usage_revert();
4808 if (patch_script_path && !pflag)
4809 errx(1, "-F option can only be used together with -p option");
4811 cwd = getcwd(NULL, 0);
4812 if (cwd == NULL) {
4813 error = got_error_from_errno("getcwd");
4814 goto done;
4816 error = got_worktree_open(&worktree, cwd);
4817 if (error)
4818 goto done;
4820 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4821 NULL);
4822 if (error != NULL)
4823 goto done;
4825 if (patch_script_path) {
4826 patch_script_file = fopen(patch_script_path, "r");
4827 if (patch_script_file == NULL) {
4828 error = got_error_from_errno2("fopen",
4829 patch_script_path);
4830 goto done;
4833 error = apply_unveil(got_repo_get_path(repo), 1,
4834 got_worktree_get_root_path(worktree));
4835 if (error)
4836 goto done;
4838 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4839 if (error)
4840 goto done;
4842 if (!can_recurse) {
4843 char *ondisk_path;
4844 struct stat sb;
4845 TAILQ_FOREACH(pe, &paths, entry) {
4846 if (asprintf(&ondisk_path, "%s/%s",
4847 got_worktree_get_root_path(worktree),
4848 pe->path) == -1) {
4849 error = got_error_from_errno("asprintf");
4850 goto done;
4852 if (lstat(ondisk_path, &sb) == -1) {
4853 if (errno == ENOENT) {
4854 free(ondisk_path);
4855 continue;
4857 error = got_error_from_errno2("lstat",
4858 ondisk_path);
4859 free(ondisk_path);
4860 goto done;
4862 free(ondisk_path);
4863 if (S_ISDIR(sb.st_mode)) {
4864 error = got_error_msg(GOT_ERR_BAD_PATH,
4865 "reverting directories requires -R option");
4866 goto done;
4871 cpa.patch_script_file = patch_script_file;
4872 cpa.action = "revert";
4873 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4874 pflag ? choose_patch : NULL, &cpa, repo);
4875 done:
4876 if (patch_script_file && fclose(patch_script_file) == EOF &&
4877 error == NULL)
4878 error = got_error_from_errno2("fclose", patch_script_path);
4879 if (repo)
4880 got_repo_close(repo);
4881 if (worktree)
4882 got_worktree_close(worktree);
4883 free(path);
4884 free(cwd);
4885 return error;
4888 __dead static void
4889 usage_commit(void)
4891 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4892 getprogname());
4893 exit(1);
4896 struct collect_commit_logmsg_arg {
4897 const char *cmdline_log;
4898 const char *editor;
4899 const char *worktree_path;
4900 const char *branch_name;
4901 const char *repo_path;
4902 char *logmsg_path;
4906 static const struct got_error *
4907 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4908 void *arg)
4910 char *initial_content = NULL;
4911 struct got_pathlist_entry *pe;
4912 const struct got_error *err = NULL;
4913 char *template = NULL;
4914 struct collect_commit_logmsg_arg *a = arg;
4915 int fd;
4916 size_t len;
4918 /* if a message was specified on the command line, just use it */
4919 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4920 len = strlen(a->cmdline_log) + 1;
4921 *logmsg = malloc(len + 1);
4922 if (*logmsg == NULL)
4923 return got_error_from_errno("malloc");
4924 strlcpy(*logmsg, a->cmdline_log, len);
4925 return NULL;
4928 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4929 return got_error_from_errno("asprintf");
4931 if (asprintf(&initial_content,
4932 "\n# changes to be committed on branch %s:\n",
4933 a->branch_name) == -1)
4934 return got_error_from_errno("asprintf");
4936 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4937 if (err)
4938 goto done;
4940 dprintf(fd, initial_content);
4942 TAILQ_FOREACH(pe, commitable_paths, entry) {
4943 struct got_commitable *ct = pe->data;
4944 dprintf(fd, "# %c %s\n",
4945 got_commitable_get_status(ct),
4946 got_commitable_get_path(ct));
4948 close(fd);
4950 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4951 done:
4952 free(initial_content);
4953 free(template);
4955 /* Editor is done; we can now apply unveil(2) */
4956 if (err == NULL) {
4957 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4958 if (err) {
4959 free(*logmsg);
4960 *logmsg = NULL;
4963 return err;
4966 static const struct got_error *
4967 cmd_commit(int argc, char *argv[])
4969 const struct got_error *error = NULL;
4970 struct got_worktree *worktree = NULL;
4971 struct got_repository *repo = NULL;
4972 char *cwd = NULL, *id_str = NULL;
4973 struct got_object_id *id = NULL;
4974 const char *logmsg = NULL;
4975 struct collect_commit_logmsg_arg cl_arg;
4976 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4977 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4978 struct got_pathlist_head paths;
4980 TAILQ_INIT(&paths);
4981 cl_arg.logmsg_path = NULL;
4983 while ((ch = getopt(argc, argv, "m:")) != -1) {
4984 switch (ch) {
4985 case 'm':
4986 logmsg = optarg;
4987 break;
4988 default:
4989 usage_commit();
4990 /* NOTREACHED */
4994 argc -= optind;
4995 argv += optind;
4997 #ifndef PROFILE
4998 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4999 "unveil", NULL) == -1)
5000 err(1, "pledge");
5001 #endif
5002 cwd = getcwd(NULL, 0);
5003 if (cwd == NULL) {
5004 error = got_error_from_errno("getcwd");
5005 goto done;
5007 error = got_worktree_open(&worktree, cwd);
5008 if (error)
5009 goto done;
5011 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5012 if (error)
5013 goto done;
5014 if (rebase_in_progress) {
5015 error = got_error(GOT_ERR_REBASING);
5016 goto done;
5019 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5020 worktree);
5021 if (error)
5022 goto done;
5024 error = get_gitconfig_path(&gitconfig_path);
5025 if (error)
5026 goto done;
5027 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5028 gitconfig_path);
5029 if (error != NULL)
5030 goto done;
5032 error = get_author(&author, repo);
5033 if (error)
5034 return error;
5037 * unveil(2) traverses exec(2); if an editor is used we have
5038 * to apply unveil after the log message has been written.
5040 if (logmsg == NULL || strlen(logmsg) == 0)
5041 error = get_editor(&editor);
5042 else
5043 error = apply_unveil(got_repo_get_path(repo), 0,
5044 got_worktree_get_root_path(worktree));
5045 if (error)
5046 goto done;
5048 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5049 if (error)
5050 goto done;
5052 cl_arg.editor = editor;
5053 cl_arg.cmdline_log = logmsg;
5054 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5055 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5056 if (!histedit_in_progress) {
5057 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5058 error = got_error(GOT_ERR_COMMIT_BRANCH);
5059 goto done;
5061 cl_arg.branch_name += 11;
5063 cl_arg.repo_path = got_repo_get_path(repo);
5064 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5065 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5066 if (error) {
5067 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5068 cl_arg.logmsg_path != NULL)
5069 preserve_logmsg = 1;
5070 goto done;
5073 error = got_object_id_str(&id_str, id);
5074 if (error)
5075 goto done;
5076 printf("Created commit %s\n", id_str);
5077 done:
5078 if (preserve_logmsg) {
5079 fprintf(stderr, "%s: log message preserved in %s\n",
5080 getprogname(), cl_arg.logmsg_path);
5081 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5082 error == NULL)
5083 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5084 free(cl_arg.logmsg_path);
5085 if (repo)
5086 got_repo_close(repo);
5087 if (worktree)
5088 got_worktree_close(worktree);
5089 free(cwd);
5090 free(id_str);
5091 free(gitconfig_path);
5092 free(editor);
5093 free(author);
5094 return error;
5097 __dead static void
5098 usage_cherrypick(void)
5100 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5101 exit(1);
5104 static const struct got_error *
5105 cmd_cherrypick(int argc, char *argv[])
5107 const struct got_error *error = NULL;
5108 struct got_worktree *worktree = NULL;
5109 struct got_repository *repo = NULL;
5110 char *cwd = NULL, *commit_id_str = NULL;
5111 struct got_object_id *commit_id = NULL;
5112 struct got_commit_object *commit = NULL;
5113 struct got_object_qid *pid;
5114 struct got_reference *head_ref = NULL;
5115 int ch, did_something = 0;
5117 while ((ch = getopt(argc, argv, "")) != -1) {
5118 switch (ch) {
5119 default:
5120 usage_cherrypick();
5121 /* NOTREACHED */
5125 argc -= optind;
5126 argv += optind;
5128 #ifndef PROFILE
5129 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5130 "unveil", NULL) == -1)
5131 err(1, "pledge");
5132 #endif
5133 if (argc != 1)
5134 usage_cherrypick();
5136 cwd = getcwd(NULL, 0);
5137 if (cwd == NULL) {
5138 error = got_error_from_errno("getcwd");
5139 goto done;
5141 error = got_worktree_open(&worktree, cwd);
5142 if (error)
5143 goto done;
5145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5146 NULL);
5147 if (error != NULL)
5148 goto done;
5150 error = apply_unveil(got_repo_get_path(repo), 0,
5151 got_worktree_get_root_path(worktree));
5152 if (error)
5153 goto done;
5155 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5156 GOT_OBJ_TYPE_COMMIT, repo);
5157 if (error != NULL) {
5158 struct got_reference *ref;
5159 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5160 goto done;
5161 error = got_ref_open(&ref, repo, argv[0], 0);
5162 if (error != NULL)
5163 goto done;
5164 error = got_ref_resolve(&commit_id, repo, ref);
5165 got_ref_close(ref);
5166 if (error != NULL)
5167 goto done;
5169 error = got_object_id_str(&commit_id_str, commit_id);
5170 if (error)
5171 goto done;
5173 error = got_ref_open(&head_ref, repo,
5174 got_worktree_get_head_ref_name(worktree), 0);
5175 if (error != NULL)
5176 goto done;
5178 error = check_same_branch(commit_id, head_ref, NULL, repo);
5179 if (error) {
5180 if (error->code != GOT_ERR_ANCESTRY)
5181 goto done;
5182 error = NULL;
5183 } else {
5184 error = got_error(GOT_ERR_SAME_BRANCH);
5185 goto done;
5188 error = got_object_open_as_commit(&commit, repo, commit_id);
5189 if (error)
5190 goto done;
5191 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5192 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5193 commit_id, repo, update_progress, &did_something, check_cancelled,
5194 NULL);
5195 if (error != NULL)
5196 goto done;
5198 if (did_something)
5199 printf("Merged commit %s\n", commit_id_str);
5200 done:
5201 if (commit)
5202 got_object_commit_close(commit);
5203 free(commit_id_str);
5204 if (head_ref)
5205 got_ref_close(head_ref);
5206 if (worktree)
5207 got_worktree_close(worktree);
5208 if (repo)
5209 got_repo_close(repo);
5210 return error;
5213 __dead static void
5214 usage_backout(void)
5216 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5217 exit(1);
5220 static const struct got_error *
5221 cmd_backout(int argc, char *argv[])
5223 const struct got_error *error = NULL;
5224 struct got_worktree *worktree = NULL;
5225 struct got_repository *repo = NULL;
5226 char *cwd = NULL, *commit_id_str = NULL;
5227 struct got_object_id *commit_id = NULL;
5228 struct got_commit_object *commit = NULL;
5229 struct got_object_qid *pid;
5230 struct got_reference *head_ref = NULL;
5231 int ch, did_something = 0;
5233 while ((ch = getopt(argc, argv, "")) != -1) {
5234 switch (ch) {
5235 default:
5236 usage_backout();
5237 /* NOTREACHED */
5241 argc -= optind;
5242 argv += optind;
5244 #ifndef PROFILE
5245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5246 "unveil", NULL) == -1)
5247 err(1, "pledge");
5248 #endif
5249 if (argc != 1)
5250 usage_backout();
5252 cwd = getcwd(NULL, 0);
5253 if (cwd == NULL) {
5254 error = got_error_from_errno("getcwd");
5255 goto done;
5257 error = got_worktree_open(&worktree, cwd);
5258 if (error)
5259 goto done;
5261 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5262 NULL);
5263 if (error != NULL)
5264 goto done;
5266 error = apply_unveil(got_repo_get_path(repo), 0,
5267 got_worktree_get_root_path(worktree));
5268 if (error)
5269 goto done;
5271 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5272 GOT_OBJ_TYPE_COMMIT, repo);
5273 if (error != NULL) {
5274 struct got_reference *ref;
5275 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5276 goto done;
5277 error = got_ref_open(&ref, repo, argv[0], 0);
5278 if (error != NULL)
5279 goto done;
5280 error = got_ref_resolve(&commit_id, repo, ref);
5281 got_ref_close(ref);
5282 if (error != NULL)
5283 goto done;
5285 error = got_object_id_str(&commit_id_str, commit_id);
5286 if (error)
5287 goto done;
5289 error = got_ref_open(&head_ref, repo,
5290 got_worktree_get_head_ref_name(worktree), 0);
5291 if (error != NULL)
5292 goto done;
5294 error = check_same_branch(commit_id, head_ref, NULL, repo);
5295 if (error)
5296 goto done;
5298 error = got_object_open_as_commit(&commit, repo, commit_id);
5299 if (error)
5300 goto done;
5301 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5302 if (pid == NULL) {
5303 error = got_error(GOT_ERR_ROOT_COMMIT);
5304 goto done;
5307 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5308 update_progress, &did_something, check_cancelled, NULL);
5309 if (error != NULL)
5310 goto done;
5312 if (did_something)
5313 printf("Backed out commit %s\n", commit_id_str);
5314 done:
5315 if (commit)
5316 got_object_commit_close(commit);
5317 free(commit_id_str);
5318 if (head_ref)
5319 got_ref_close(head_ref);
5320 if (worktree)
5321 got_worktree_close(worktree);
5322 if (repo)
5323 got_repo_close(repo);
5324 return error;
5327 __dead static void
5328 usage_rebase(void)
5330 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5331 getprogname());
5332 exit(1);
5335 void
5336 trim_logmsg(char *logmsg, int limit)
5338 char *nl;
5339 size_t len;
5341 len = strlen(logmsg);
5342 if (len > limit)
5343 len = limit;
5344 logmsg[len] = '\0';
5345 nl = strchr(logmsg, '\n');
5346 if (nl)
5347 *nl = '\0';
5350 static const struct got_error *
5351 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5353 const struct got_error *err;
5354 char *logmsg0 = NULL;
5355 const char *s;
5357 err = got_object_commit_get_logmsg(&logmsg0, commit);
5358 if (err)
5359 return err;
5361 s = logmsg0;
5362 while (isspace((unsigned char)s[0]))
5363 s++;
5365 *logmsg = strdup(s);
5366 if (*logmsg == NULL) {
5367 err = got_error_from_errno("strdup");
5368 goto done;
5371 trim_logmsg(*logmsg, limit);
5372 done:
5373 free(logmsg0);
5374 return err;
5377 static const struct got_error *
5378 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5380 const struct got_error *err;
5381 struct got_commit_object *commit = NULL;
5382 char *id_str = NULL, *logmsg = NULL;
5384 err = got_object_open_as_commit(&commit, repo, id);
5385 if (err)
5386 return err;
5388 err = got_object_id_str(&id_str, id);
5389 if (err)
5390 goto done;
5392 id_str[12] = '\0';
5394 err = get_short_logmsg(&logmsg, 42, commit);
5395 if (err)
5396 goto done;
5398 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5399 done:
5400 free(id_str);
5401 got_object_commit_close(commit);
5402 free(logmsg);
5403 return err;
5406 static const struct got_error *
5407 show_rebase_progress(struct got_commit_object *commit,
5408 struct got_object_id *old_id, struct got_object_id *new_id)
5410 const struct got_error *err;
5411 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5413 err = got_object_id_str(&old_id_str, old_id);
5414 if (err)
5415 goto done;
5417 if (new_id) {
5418 err = got_object_id_str(&new_id_str, new_id);
5419 if (err)
5420 goto done;
5423 old_id_str[12] = '\0';
5424 if (new_id_str)
5425 new_id_str[12] = '\0';
5427 err = get_short_logmsg(&logmsg, 42, commit);
5428 if (err)
5429 goto done;
5431 printf("%s -> %s: %s\n", old_id_str,
5432 new_id_str ? new_id_str : "no-op change", logmsg);
5433 done:
5434 free(old_id_str);
5435 free(new_id_str);
5436 free(logmsg);
5437 return err;
5440 static const struct got_error *
5441 rebase_progress(void *arg, unsigned char status, const char *path)
5443 unsigned char *rebase_status = arg;
5445 while (path[0] == '/')
5446 path++;
5447 printf("%c %s\n", status, path);
5449 if (*rebase_status == GOT_STATUS_CONFLICT)
5450 return NULL;
5451 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5452 *rebase_status = status;
5453 return NULL;
5456 static const struct got_error *
5457 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5458 struct got_reference *branch, struct got_reference *new_base_branch,
5459 struct got_reference *tmp_branch, struct got_repository *repo)
5461 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5462 return got_worktree_rebase_complete(worktree, fileindex,
5463 new_base_branch, tmp_branch, branch, repo);
5466 static const struct got_error *
5467 rebase_commit(struct got_pathlist_head *merged_paths,
5468 struct got_worktree *worktree, struct got_fileindex *fileindex,
5469 struct got_reference *tmp_branch,
5470 struct got_object_id *commit_id, struct got_repository *repo)
5472 const struct got_error *error;
5473 struct got_commit_object *commit;
5474 struct got_object_id *new_commit_id;
5476 error = got_object_open_as_commit(&commit, repo, commit_id);
5477 if (error)
5478 return error;
5480 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5481 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5482 if (error) {
5483 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5484 goto done;
5485 error = show_rebase_progress(commit, commit_id, NULL);
5486 } else {
5487 error = show_rebase_progress(commit, commit_id, new_commit_id);
5488 free(new_commit_id);
5490 done:
5491 got_object_commit_close(commit);
5492 return error;
5495 struct check_path_prefix_arg {
5496 const char *path_prefix;
5497 size_t len;
5498 int errcode;
5501 static const struct got_error *
5502 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5503 struct got_blob_object *blob2, struct got_object_id *id1,
5504 struct got_object_id *id2, const char *path1, const char *path2,
5505 mode_t mode1, mode_t mode2, struct got_repository *repo)
5507 struct check_path_prefix_arg *a = arg;
5509 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5510 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5511 return got_error(a->errcode);
5513 return NULL;
5516 static const struct got_error *
5517 check_path_prefix(struct got_object_id *parent_id,
5518 struct got_object_id *commit_id, const char *path_prefix,
5519 int errcode, struct got_repository *repo)
5521 const struct got_error *err;
5522 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5523 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5524 struct check_path_prefix_arg cpp_arg;
5526 if (got_path_is_root_dir(path_prefix))
5527 return NULL;
5529 err = got_object_open_as_commit(&commit, repo, commit_id);
5530 if (err)
5531 goto done;
5533 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5534 if (err)
5535 goto done;
5537 err = got_object_open_as_tree(&tree1, repo,
5538 got_object_commit_get_tree_id(parent_commit));
5539 if (err)
5540 goto done;
5542 err = got_object_open_as_tree(&tree2, repo,
5543 got_object_commit_get_tree_id(commit));
5544 if (err)
5545 goto done;
5547 cpp_arg.path_prefix = path_prefix;
5548 while (cpp_arg.path_prefix[0] == '/')
5549 cpp_arg.path_prefix++;
5550 cpp_arg.len = strlen(cpp_arg.path_prefix);
5551 cpp_arg.errcode = errcode;
5552 err = got_diff_tree(tree1, tree2, "", "", repo,
5553 check_path_prefix_in_diff, &cpp_arg, 0);
5554 done:
5555 if (tree1)
5556 got_object_tree_close(tree1);
5557 if (tree2)
5558 got_object_tree_close(tree2);
5559 if (commit)
5560 got_object_commit_close(commit);
5561 if (parent_commit)
5562 got_object_commit_close(parent_commit);
5563 return err;
5566 static const struct got_error *
5567 collect_commits(struct got_object_id_queue *commits,
5568 struct got_object_id *initial_commit_id,
5569 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5570 const char *path_prefix, int path_prefix_errcode,
5571 struct got_repository *repo)
5573 const struct got_error *err = NULL;
5574 struct got_commit_graph *graph = NULL;
5575 struct got_object_id *parent_id = NULL;
5576 struct got_object_qid *qid;
5577 struct got_object_id *commit_id = initial_commit_id;
5579 err = got_commit_graph_open(&graph, "/", 1);
5580 if (err)
5581 return err;
5583 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5584 check_cancelled, NULL);
5585 if (err)
5586 goto done;
5587 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5588 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5589 check_cancelled, NULL);
5590 if (err) {
5591 if (err->code == GOT_ERR_ITER_COMPLETED) {
5592 err = got_error_msg(GOT_ERR_ANCESTRY,
5593 "ran out of commits to rebase before "
5594 "youngest common ancestor commit has "
5595 "been reached?!?");
5597 goto done;
5598 } else {
5599 err = check_path_prefix(parent_id, commit_id,
5600 path_prefix, path_prefix_errcode, repo);
5601 if (err)
5602 goto done;
5604 err = got_object_qid_alloc(&qid, commit_id);
5605 if (err)
5606 goto done;
5607 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5608 commit_id = parent_id;
5611 done:
5612 got_commit_graph_close(graph);
5613 return err;
5616 static const struct got_error *
5617 cmd_rebase(int argc, char *argv[])
5619 const struct got_error *error = NULL;
5620 struct got_worktree *worktree = NULL;
5621 struct got_repository *repo = NULL;
5622 struct got_fileindex *fileindex = NULL;
5623 char *cwd = NULL;
5624 struct got_reference *branch = NULL;
5625 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5626 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5627 struct got_object_id *resume_commit_id = NULL;
5628 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5629 struct got_commit_object *commit = NULL;
5630 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5631 int histedit_in_progress = 0;
5632 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5633 struct got_object_id_queue commits;
5634 struct got_pathlist_head merged_paths;
5635 const struct got_object_id_queue *parent_ids;
5636 struct got_object_qid *qid, *pid;
5638 SIMPLEQ_INIT(&commits);
5639 TAILQ_INIT(&merged_paths);
5641 while ((ch = getopt(argc, argv, "ac")) != -1) {
5642 switch (ch) {
5643 case 'a':
5644 abort_rebase = 1;
5645 break;
5646 case 'c':
5647 continue_rebase = 1;
5648 break;
5649 default:
5650 usage_rebase();
5651 /* NOTREACHED */
5655 argc -= optind;
5656 argv += optind;
5658 #ifndef PROFILE
5659 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5660 "unveil", NULL) == -1)
5661 err(1, "pledge");
5662 #endif
5663 if (abort_rebase && continue_rebase)
5664 usage_rebase();
5665 else if (abort_rebase || continue_rebase) {
5666 if (argc != 0)
5667 usage_rebase();
5668 } else if (argc != 1)
5669 usage_rebase();
5671 cwd = getcwd(NULL, 0);
5672 if (cwd == NULL) {
5673 error = got_error_from_errno("getcwd");
5674 goto done;
5676 error = got_worktree_open(&worktree, cwd);
5677 if (error)
5678 goto done;
5680 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5681 NULL);
5682 if (error != NULL)
5683 goto done;
5685 error = apply_unveil(got_repo_get_path(repo), 0,
5686 got_worktree_get_root_path(worktree));
5687 if (error)
5688 goto done;
5690 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5691 worktree);
5692 if (error)
5693 goto done;
5694 if (histedit_in_progress) {
5695 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5696 goto done;
5699 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5700 if (error)
5701 goto done;
5703 if (abort_rebase) {
5704 int did_something;
5705 if (!rebase_in_progress) {
5706 error = got_error(GOT_ERR_NOT_REBASING);
5707 goto done;
5709 error = got_worktree_rebase_continue(&resume_commit_id,
5710 &new_base_branch, &tmp_branch, &branch, &fileindex,
5711 worktree, repo);
5712 if (error)
5713 goto done;
5714 printf("Switching work tree to %s\n",
5715 got_ref_get_symref_target(new_base_branch));
5716 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5717 new_base_branch, update_progress, &did_something);
5718 if (error)
5719 goto done;
5720 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5721 goto done; /* nothing else to do */
5724 if (continue_rebase) {
5725 if (!rebase_in_progress) {
5726 error = got_error(GOT_ERR_NOT_REBASING);
5727 goto done;
5729 error = got_worktree_rebase_continue(&resume_commit_id,
5730 &new_base_branch, &tmp_branch, &branch, &fileindex,
5731 worktree, repo);
5732 if (error)
5733 goto done;
5735 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5736 resume_commit_id, repo);
5737 if (error)
5738 goto done;
5740 yca_id = got_object_id_dup(resume_commit_id);
5741 if (yca_id == NULL) {
5742 error = got_error_from_errno("got_object_id_dup");
5743 goto done;
5745 } else {
5746 error = got_ref_open(&branch, repo, argv[0], 0);
5747 if (error != NULL)
5748 goto done;
5751 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5752 if (error)
5753 goto done;
5755 if (!continue_rebase) {
5756 struct got_object_id *base_commit_id;
5758 base_commit_id = got_worktree_get_base_commit_id(worktree);
5759 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5760 base_commit_id, branch_head_commit_id, repo,
5761 check_cancelled, NULL);
5762 if (error)
5763 goto done;
5764 if (yca_id == NULL) {
5765 error = got_error_msg(GOT_ERR_ANCESTRY,
5766 "specified branch shares no common ancestry "
5767 "with work tree's branch");
5768 goto done;
5771 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5772 if (error) {
5773 if (error->code != GOT_ERR_ANCESTRY)
5774 goto done;
5775 error = NULL;
5776 } else {
5777 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5778 "specified branch resolves to a commit which "
5779 "is already contained in work tree's branch");
5780 goto done;
5782 error = got_worktree_rebase_prepare(&new_base_branch,
5783 &tmp_branch, &fileindex, worktree, branch, repo);
5784 if (error)
5785 goto done;
5788 commit_id = branch_head_commit_id;
5789 error = got_object_open_as_commit(&commit, repo, commit_id);
5790 if (error)
5791 goto done;
5793 parent_ids = got_object_commit_get_parent_ids(commit);
5794 pid = SIMPLEQ_FIRST(parent_ids);
5795 if (pid == NULL) {
5796 if (!continue_rebase) {
5797 int did_something;
5798 error = got_worktree_rebase_abort(worktree, fileindex,
5799 repo, new_base_branch, update_progress,
5800 &did_something);
5801 if (error)
5802 goto done;
5803 printf("Rebase of %s aborted\n",
5804 got_ref_get_name(branch));
5806 error = got_error(GOT_ERR_EMPTY_REBASE);
5807 goto done;
5809 error = collect_commits(&commits, commit_id, pid->id,
5810 yca_id, got_worktree_get_path_prefix(worktree),
5811 GOT_ERR_REBASE_PATH, repo);
5812 got_object_commit_close(commit);
5813 commit = NULL;
5814 if (error)
5815 goto done;
5817 if (SIMPLEQ_EMPTY(&commits)) {
5818 if (continue_rebase) {
5819 error = rebase_complete(worktree, fileindex,
5820 branch, new_base_branch, tmp_branch, repo);
5821 goto done;
5822 } else {
5823 /* Fast-forward the reference of the branch. */
5824 struct got_object_id *new_head_commit_id;
5825 char *id_str;
5826 error = got_ref_resolve(&new_head_commit_id, repo,
5827 new_base_branch);
5828 if (error)
5829 goto done;
5830 error = got_object_id_str(&id_str, new_head_commit_id);
5831 printf("Forwarding %s to commit %s\n",
5832 got_ref_get_name(branch), id_str);
5833 free(id_str);
5834 error = got_ref_change_ref(branch,
5835 new_head_commit_id);
5836 if (error)
5837 goto done;
5841 pid = NULL;
5842 SIMPLEQ_FOREACH(qid, &commits, entry) {
5843 commit_id = qid->id;
5844 parent_id = pid ? pid->id : yca_id;
5845 pid = qid;
5847 error = got_worktree_rebase_merge_files(&merged_paths,
5848 worktree, fileindex, parent_id, commit_id, repo,
5849 rebase_progress, &rebase_status, check_cancelled, NULL);
5850 if (error)
5851 goto done;
5853 if (rebase_status == GOT_STATUS_CONFLICT) {
5854 error = show_rebase_merge_conflict(qid->id, repo);
5855 if (error)
5856 goto done;
5857 got_worktree_rebase_pathlist_free(&merged_paths);
5858 break;
5861 error = rebase_commit(&merged_paths, worktree, fileindex,
5862 tmp_branch, commit_id, repo);
5863 got_worktree_rebase_pathlist_free(&merged_paths);
5864 if (error)
5865 goto done;
5868 if (rebase_status == GOT_STATUS_CONFLICT) {
5869 error = got_worktree_rebase_postpone(worktree, fileindex);
5870 if (error)
5871 goto done;
5872 error = got_error_msg(GOT_ERR_CONFLICTS,
5873 "conflicts must be resolved before rebasing can continue");
5874 } else
5875 error = rebase_complete(worktree, fileindex, branch,
5876 new_base_branch, tmp_branch, repo);
5877 done:
5878 got_object_id_queue_free(&commits);
5879 free(branch_head_commit_id);
5880 free(resume_commit_id);
5881 free(yca_id);
5882 if (commit)
5883 got_object_commit_close(commit);
5884 if (branch)
5885 got_ref_close(branch);
5886 if (new_base_branch)
5887 got_ref_close(new_base_branch);
5888 if (tmp_branch)
5889 got_ref_close(tmp_branch);
5890 if (worktree)
5891 got_worktree_close(worktree);
5892 if (repo)
5893 got_repo_close(repo);
5894 return error;
5897 __dead static void
5898 usage_histedit(void)
5900 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5901 getprogname());
5902 exit(1);
5905 #define GOT_HISTEDIT_PICK 'p'
5906 #define GOT_HISTEDIT_EDIT 'e'
5907 #define GOT_HISTEDIT_FOLD 'f'
5908 #define GOT_HISTEDIT_DROP 'd'
5909 #define GOT_HISTEDIT_MESG 'm'
5911 static struct got_histedit_cmd {
5912 unsigned char code;
5913 const char *name;
5914 const char *desc;
5915 } got_histedit_cmds[] = {
5916 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5917 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5918 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5919 "be used" },
5920 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5921 { GOT_HISTEDIT_MESG, "mesg",
5922 "single-line log message for commit above (open editor if empty)" },
5925 struct got_histedit_list_entry {
5926 TAILQ_ENTRY(got_histedit_list_entry) entry;
5927 struct got_object_id *commit_id;
5928 const struct got_histedit_cmd *cmd;
5929 char *logmsg;
5931 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5933 static const struct got_error *
5934 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5935 FILE *f, struct got_repository *repo)
5937 const struct got_error *err = NULL;
5938 char *logmsg = NULL, *id_str = NULL;
5939 struct got_commit_object *commit = NULL;
5940 int n;
5942 err = got_object_open_as_commit(&commit, repo, commit_id);
5943 if (err)
5944 goto done;
5946 err = get_short_logmsg(&logmsg, 34, commit);
5947 if (err)
5948 goto done;
5950 err = got_object_id_str(&id_str, commit_id);
5951 if (err)
5952 goto done;
5954 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5955 if (n < 0)
5956 err = got_ferror(f, GOT_ERR_IO);
5957 done:
5958 if (commit)
5959 got_object_commit_close(commit);
5960 free(id_str);
5961 free(logmsg);
5962 return err;
5965 static const struct got_error *
5966 histedit_write_commit_list(struct got_object_id_queue *commits,
5967 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5969 const struct got_error *err = NULL;
5970 struct got_object_qid *qid;
5972 if (SIMPLEQ_EMPTY(commits))
5973 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5975 SIMPLEQ_FOREACH(qid, commits, entry) {
5976 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5977 f, repo);
5978 if (err)
5979 break;
5980 if (edit_logmsg_only) {
5981 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5982 if (n < 0) {
5983 err = got_ferror(f, GOT_ERR_IO);
5984 break;
5989 return err;
5992 static const struct got_error *
5993 write_cmd_list(FILE *f, const char *branch_name,
5994 struct got_object_id_queue *commits)
5996 const struct got_error *err = NULL;
5997 int n, i;
5998 char *id_str;
5999 struct got_object_qid *qid;
6001 qid = SIMPLEQ_FIRST(commits);
6002 err = got_object_id_str(&id_str, qid->id);
6003 if (err)
6004 return err;
6006 n = fprintf(f,
6007 "# Editing the history of branch '%s' starting at\n"
6008 "# commit %s\n"
6009 "# Commits will be processed in order from top to "
6010 "bottom of this file.\n", branch_name, id_str);
6011 if (n < 0) {
6012 err = got_ferror(f, GOT_ERR_IO);
6013 goto done;
6016 n = fprintf(f, "# Available histedit commands:\n");
6017 if (n < 0) {
6018 err = got_ferror(f, GOT_ERR_IO);
6019 goto done;
6022 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6023 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6024 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6025 cmd->desc);
6026 if (n < 0) {
6027 err = got_ferror(f, GOT_ERR_IO);
6028 break;
6031 done:
6032 free(id_str);
6033 return err;
6036 static const struct got_error *
6037 histedit_syntax_error(int lineno)
6039 static char msg[42];
6040 int ret;
6042 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6043 lineno);
6044 if (ret == -1 || ret >= sizeof(msg))
6045 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6047 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6050 static const struct got_error *
6051 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6052 char *logmsg, struct got_repository *repo)
6054 const struct got_error *err;
6055 struct got_commit_object *folded_commit = NULL;
6056 char *id_str, *folded_logmsg = NULL;
6058 err = got_object_id_str(&id_str, hle->commit_id);
6059 if (err)
6060 return err;
6062 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6063 if (err)
6064 goto done;
6066 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6067 if (err)
6068 goto done;
6069 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6070 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6071 folded_logmsg) == -1) {
6072 err = got_error_from_errno("asprintf");
6074 done:
6075 if (folded_commit)
6076 got_object_commit_close(folded_commit);
6077 free(id_str);
6078 free(folded_logmsg);
6079 return err;
6082 static struct got_histedit_list_entry *
6083 get_folded_commits(struct got_histedit_list_entry *hle)
6085 struct got_histedit_list_entry *prev, *folded = NULL;
6087 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6088 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6089 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6090 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6091 folded = prev;
6092 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6095 return folded;
6098 static const struct got_error *
6099 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6100 struct got_repository *repo)
6102 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6103 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6104 const struct got_error *err = NULL;
6105 struct got_commit_object *commit = NULL;
6106 int fd;
6107 struct got_histedit_list_entry *folded = NULL;
6109 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6110 if (err)
6111 return err;
6113 folded = get_folded_commits(hle);
6114 if (folded) {
6115 while (folded != hle) {
6116 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6117 folded = TAILQ_NEXT(folded, entry);
6118 continue;
6120 err = append_folded_commit_msg(&new_msg, folded,
6121 logmsg, repo);
6122 if (err)
6123 goto done;
6124 free(logmsg);
6125 logmsg = new_msg;
6126 folded = TAILQ_NEXT(folded, entry);
6130 err = got_object_id_str(&id_str, hle->commit_id);
6131 if (err)
6132 goto done;
6133 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6134 if (err)
6135 goto done;
6136 if (asprintf(&new_msg,
6137 "%s\n# original log message of commit %s: %s",
6138 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6139 err = got_error_from_errno("asprintf");
6140 goto done;
6142 free(logmsg);
6143 logmsg = new_msg;
6145 err = got_object_id_str(&id_str, hle->commit_id);
6146 if (err)
6147 goto done;
6149 err = got_opentemp_named_fd(&logmsg_path, &fd,
6150 GOT_TMPDIR_STR "/got-logmsg");
6151 if (err)
6152 goto done;
6154 dprintf(fd, logmsg);
6155 close(fd);
6157 err = get_editor(&editor);
6158 if (err)
6159 goto done;
6161 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6162 if (err) {
6163 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6164 goto done;
6165 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6167 done:
6168 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6169 err = got_error_from_errno2("unlink", logmsg_path);
6170 free(logmsg_path);
6171 free(logmsg);
6172 free(orig_logmsg);
6173 free(editor);
6174 if (commit)
6175 got_object_commit_close(commit);
6176 return err;
6179 static const struct got_error *
6180 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6181 FILE *f, struct got_repository *repo)
6183 const struct got_error *err = NULL;
6184 char *line = NULL, *p, *end;
6185 size_t size;
6186 ssize_t len;
6187 int lineno = 0, i;
6188 const struct got_histedit_cmd *cmd;
6189 struct got_object_id *commit_id = NULL;
6190 struct got_histedit_list_entry *hle = NULL;
6192 for (;;) {
6193 len = getline(&line, &size, f);
6194 if (len == -1) {
6195 const struct got_error *getline_err;
6196 if (feof(f))
6197 break;
6198 getline_err = got_error_from_errno("getline");
6199 err = got_ferror(f, getline_err->code);
6200 break;
6202 lineno++;
6203 p = line;
6204 while (isspace((unsigned char)p[0]))
6205 p++;
6206 if (p[0] == '#' || p[0] == '\0') {
6207 free(line);
6208 line = NULL;
6209 continue;
6211 cmd = NULL;
6212 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6213 cmd = &got_histedit_cmds[i];
6214 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6215 isspace((unsigned char)p[strlen(cmd->name)])) {
6216 p += strlen(cmd->name);
6217 break;
6219 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6220 p++;
6221 break;
6224 if (i == nitems(got_histedit_cmds)) {
6225 err = histedit_syntax_error(lineno);
6226 break;
6228 while (isspace((unsigned char)p[0]))
6229 p++;
6230 if (cmd->code == GOT_HISTEDIT_MESG) {
6231 if (hle == NULL || hle->logmsg != NULL) {
6232 err = got_error(GOT_ERR_HISTEDIT_CMD);
6233 break;
6235 if (p[0] == '\0') {
6236 err = histedit_edit_logmsg(hle, repo);
6237 if (err)
6238 break;
6239 } else {
6240 hle->logmsg = strdup(p);
6241 if (hle->logmsg == NULL) {
6242 err = got_error_from_errno("strdup");
6243 break;
6246 free(line);
6247 line = NULL;
6248 continue;
6249 } else {
6250 end = p;
6251 while (end[0] && !isspace((unsigned char)end[0]))
6252 end++;
6253 *end = '\0';
6255 err = got_object_resolve_id_str(&commit_id, repo, p);
6256 if (err) {
6257 /* override error code */
6258 err = histedit_syntax_error(lineno);
6259 break;
6262 hle = malloc(sizeof(*hle));
6263 if (hle == NULL) {
6264 err = got_error_from_errno("malloc");
6265 break;
6267 hle->cmd = cmd;
6268 hle->commit_id = commit_id;
6269 hle->logmsg = NULL;
6270 commit_id = NULL;
6271 free(line);
6272 line = NULL;
6273 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6276 free(line);
6277 free(commit_id);
6278 return err;
6281 static const struct got_error *
6282 histedit_check_script(struct got_histedit_list *histedit_cmds,
6283 struct got_object_id_queue *commits, struct got_repository *repo)
6285 const struct got_error *err = NULL;
6286 struct got_object_qid *qid;
6287 struct got_histedit_list_entry *hle;
6288 static char msg[92];
6289 char *id_str;
6291 if (TAILQ_EMPTY(histedit_cmds))
6292 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6293 "histedit script contains no commands");
6294 if (SIMPLEQ_EMPTY(commits))
6295 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6297 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6298 struct got_histedit_list_entry *hle2;
6299 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6300 if (hle == hle2)
6301 continue;
6302 if (got_object_id_cmp(hle->commit_id,
6303 hle2->commit_id) != 0)
6304 continue;
6305 err = got_object_id_str(&id_str, hle->commit_id);
6306 if (err)
6307 return err;
6308 snprintf(msg, sizeof(msg), "commit %s is listed "
6309 "more than once in histedit script", id_str);
6310 free(id_str);
6311 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6315 SIMPLEQ_FOREACH(qid, commits, entry) {
6316 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6317 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6318 break;
6320 if (hle == NULL) {
6321 err = got_object_id_str(&id_str, qid->id);
6322 if (err)
6323 return err;
6324 snprintf(msg, sizeof(msg),
6325 "commit %s missing from histedit script", id_str);
6326 free(id_str);
6327 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6331 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6332 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6333 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6334 "last commit in histedit script cannot be folded");
6336 return NULL;
6339 static const struct got_error *
6340 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6341 const char *path, struct got_object_id_queue *commits,
6342 struct got_repository *repo)
6344 const struct got_error *err = NULL;
6345 char *editor;
6346 FILE *f = NULL;
6348 err = get_editor(&editor);
6349 if (err)
6350 return err;
6352 if (spawn_editor(editor, path) == -1) {
6353 err = got_error_from_errno("failed spawning editor");
6354 goto done;
6357 f = fopen(path, "r");
6358 if (f == NULL) {
6359 err = got_error_from_errno("fopen");
6360 goto done;
6362 err = histedit_parse_list(histedit_cmds, f, repo);
6363 if (err)
6364 goto done;
6366 err = histedit_check_script(histedit_cmds, commits, repo);
6367 done:
6368 if (f && fclose(f) != 0 && err == NULL)
6369 err = got_error_from_errno("fclose");
6370 free(editor);
6371 return err;
6374 static const struct got_error *
6375 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6376 struct got_object_id_queue *, const char *, const char *,
6377 struct got_repository *);
6379 static const struct got_error *
6380 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6381 struct got_object_id_queue *commits, const char *branch_name,
6382 int edit_logmsg_only, struct got_repository *repo)
6384 const struct got_error *err;
6385 FILE *f = NULL;
6386 char *path = NULL;
6388 err = got_opentemp_named(&path, &f, "got-histedit");
6389 if (err)
6390 return err;
6392 err = write_cmd_list(f, branch_name, commits);
6393 if (err)
6394 goto done;
6396 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6397 if (err)
6398 goto done;
6400 if (edit_logmsg_only) {
6401 rewind(f);
6402 err = histedit_parse_list(histedit_cmds, f, repo);
6403 } else {
6404 if (fclose(f) != 0) {
6405 err = got_error_from_errno("fclose");
6406 goto done;
6408 f = NULL;
6409 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6410 if (err) {
6411 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6412 err->code != GOT_ERR_HISTEDIT_CMD)
6413 goto done;
6414 err = histedit_edit_list_retry(histedit_cmds, err,
6415 commits, path, branch_name, repo);
6418 done:
6419 if (f && fclose(f) != 0 && err == NULL)
6420 err = got_error_from_errno("fclose");
6421 if (path && unlink(path) != 0 && err == NULL)
6422 err = got_error_from_errno2("unlink", path);
6423 free(path);
6424 return err;
6427 static const struct got_error *
6428 histedit_save_list(struct got_histedit_list *histedit_cmds,
6429 struct got_worktree *worktree, struct got_repository *repo)
6431 const struct got_error *err = NULL;
6432 char *path = NULL;
6433 FILE *f = NULL;
6434 struct got_histedit_list_entry *hle;
6435 struct got_commit_object *commit = NULL;
6437 err = got_worktree_get_histedit_script_path(&path, worktree);
6438 if (err)
6439 return err;
6441 f = fopen(path, "w");
6442 if (f == NULL) {
6443 err = got_error_from_errno2("fopen", path);
6444 goto done;
6446 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6447 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6448 repo);
6449 if (err)
6450 break;
6452 if (hle->logmsg) {
6453 int n = fprintf(f, "%c %s\n",
6454 GOT_HISTEDIT_MESG, hle->logmsg);
6455 if (n < 0) {
6456 err = got_ferror(f, GOT_ERR_IO);
6457 break;
6461 done:
6462 if (f && fclose(f) != 0 && err == NULL)
6463 err = got_error_from_errno("fclose");
6464 free(path);
6465 if (commit)
6466 got_object_commit_close(commit);
6467 return err;
6470 void
6471 histedit_free_list(struct got_histedit_list *histedit_cmds)
6473 struct got_histedit_list_entry *hle;
6475 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6476 TAILQ_REMOVE(histedit_cmds, hle, entry);
6477 free(hle);
6481 static const struct got_error *
6482 histedit_load_list(struct got_histedit_list *histedit_cmds,
6483 const char *path, struct got_repository *repo)
6485 const struct got_error *err = NULL;
6486 FILE *f = NULL;
6488 f = fopen(path, "r");
6489 if (f == NULL) {
6490 err = got_error_from_errno2("fopen", path);
6491 goto done;
6494 err = histedit_parse_list(histedit_cmds, f, repo);
6495 done:
6496 if (f && fclose(f) != 0 && err == NULL)
6497 err = got_error_from_errno("fclose");
6498 return err;
6501 static const struct got_error *
6502 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6503 const struct got_error *edit_err, struct got_object_id_queue *commits,
6504 const char *path, const char *branch_name, struct got_repository *repo)
6506 const struct got_error *err = NULL, *prev_err = edit_err;
6507 int resp = ' ';
6509 while (resp != 'c' && resp != 'r' && resp != 'a') {
6510 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6511 "or (a)bort: ", getprogname(), prev_err->msg);
6512 resp = getchar();
6513 if (resp == '\n')
6514 resp = getchar();
6515 if (resp == 'c') {
6516 histedit_free_list(histedit_cmds);
6517 err = histedit_run_editor(histedit_cmds, path, commits,
6518 repo);
6519 if (err) {
6520 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6521 err->code != GOT_ERR_HISTEDIT_CMD)
6522 break;
6523 prev_err = err;
6524 resp = ' ';
6525 continue;
6527 break;
6528 } else if (resp == 'r') {
6529 histedit_free_list(histedit_cmds);
6530 err = histedit_edit_script(histedit_cmds,
6531 commits, branch_name, 0, repo);
6532 if (err) {
6533 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6534 err->code != GOT_ERR_HISTEDIT_CMD)
6535 break;
6536 prev_err = err;
6537 resp = ' ';
6538 continue;
6540 break;
6541 } else if (resp == 'a') {
6542 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6543 break;
6544 } else
6545 printf("invalid response '%c'\n", resp);
6548 return err;
6551 static const struct got_error *
6552 histedit_complete(struct got_worktree *worktree,
6553 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6554 struct got_reference *branch, struct got_repository *repo)
6556 printf("Switching work tree to %s\n",
6557 got_ref_get_symref_target(branch));
6558 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6559 branch, repo);
6562 static const struct got_error *
6563 show_histedit_progress(struct got_commit_object *commit,
6564 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6566 const struct got_error *err;
6567 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6569 err = got_object_id_str(&old_id_str, hle->commit_id);
6570 if (err)
6571 goto done;
6573 if (new_id) {
6574 err = got_object_id_str(&new_id_str, new_id);
6575 if (err)
6576 goto done;
6579 old_id_str[12] = '\0';
6580 if (new_id_str)
6581 new_id_str[12] = '\0';
6583 if (hle->logmsg) {
6584 logmsg = strdup(hle->logmsg);
6585 if (logmsg == NULL) {
6586 err = got_error_from_errno("strdup");
6587 goto done;
6589 trim_logmsg(logmsg, 42);
6590 } else {
6591 err = get_short_logmsg(&logmsg, 42, commit);
6592 if (err)
6593 goto done;
6596 switch (hle->cmd->code) {
6597 case GOT_HISTEDIT_PICK:
6598 case GOT_HISTEDIT_EDIT:
6599 printf("%s -> %s: %s\n", old_id_str,
6600 new_id_str ? new_id_str : "no-op change", logmsg);
6601 break;
6602 case GOT_HISTEDIT_DROP:
6603 case GOT_HISTEDIT_FOLD:
6604 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6605 logmsg);
6606 break;
6607 default:
6608 break;
6610 done:
6611 free(old_id_str);
6612 free(new_id_str);
6613 return err;
6616 static const struct got_error *
6617 histedit_commit(struct got_pathlist_head *merged_paths,
6618 struct got_worktree *worktree, struct got_fileindex *fileindex,
6619 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6620 struct got_repository *repo)
6622 const struct got_error *err;
6623 struct got_commit_object *commit;
6624 struct got_object_id *new_commit_id;
6626 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6627 && hle->logmsg == NULL) {
6628 err = histedit_edit_logmsg(hle, repo);
6629 if (err)
6630 return err;
6633 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6634 if (err)
6635 return err;
6637 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6638 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6639 hle->logmsg, repo);
6640 if (err) {
6641 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6642 goto done;
6643 err = show_histedit_progress(commit, hle, NULL);
6644 } else {
6645 err = show_histedit_progress(commit, hle, new_commit_id);
6646 free(new_commit_id);
6648 done:
6649 got_object_commit_close(commit);
6650 return err;
6653 static const struct got_error *
6654 histedit_skip_commit(struct got_histedit_list_entry *hle,
6655 struct got_worktree *worktree, struct got_repository *repo)
6657 const struct got_error *error;
6658 struct got_commit_object *commit;
6660 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6661 repo);
6662 if (error)
6663 return error;
6665 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6666 if (error)
6667 return error;
6669 error = show_histedit_progress(commit, hle, NULL);
6670 got_object_commit_close(commit);
6671 return error;
6674 static const struct got_error *
6675 check_local_changes(void *arg, unsigned char status,
6676 unsigned char staged_status, const char *path,
6677 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6678 struct got_object_id *commit_id, int dirfd, const char *de_name)
6680 int *have_local_changes = arg;
6682 switch (status) {
6683 case GOT_STATUS_ADD:
6684 case GOT_STATUS_DELETE:
6685 case GOT_STATUS_MODIFY:
6686 case GOT_STATUS_CONFLICT:
6687 *have_local_changes = 1;
6688 return got_error(GOT_ERR_CANCELLED);
6689 default:
6690 break;
6693 switch (staged_status) {
6694 case GOT_STATUS_ADD:
6695 case GOT_STATUS_DELETE:
6696 case GOT_STATUS_MODIFY:
6697 *have_local_changes = 1;
6698 return got_error(GOT_ERR_CANCELLED);
6699 default:
6700 break;
6703 return NULL;
6706 static const struct got_error *
6707 cmd_histedit(int argc, char *argv[])
6709 const struct got_error *error = NULL;
6710 struct got_worktree *worktree = NULL;
6711 struct got_fileindex *fileindex = NULL;
6712 struct got_repository *repo = NULL;
6713 char *cwd = NULL;
6714 struct got_reference *branch = NULL;
6715 struct got_reference *tmp_branch = NULL;
6716 struct got_object_id *resume_commit_id = NULL;
6717 struct got_object_id *base_commit_id = NULL;
6718 struct got_object_id *head_commit_id = NULL;
6719 struct got_commit_object *commit = NULL;
6720 int ch, rebase_in_progress = 0, did_something;
6721 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6722 int edit_logmsg_only = 0;
6723 const char *edit_script_path = NULL;
6724 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6725 struct got_object_id_queue commits;
6726 struct got_pathlist_head merged_paths;
6727 const struct got_object_id_queue *parent_ids;
6728 struct got_object_qid *pid;
6729 struct got_histedit_list histedit_cmds;
6730 struct got_histedit_list_entry *hle;
6732 SIMPLEQ_INIT(&commits);
6733 TAILQ_INIT(&histedit_cmds);
6734 TAILQ_INIT(&merged_paths);
6736 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6737 switch (ch) {
6738 case 'a':
6739 abort_edit = 1;
6740 break;
6741 case 'c':
6742 continue_edit = 1;
6743 break;
6744 case 'F':
6745 edit_script_path = optarg;
6746 break;
6747 case 'm':
6748 edit_logmsg_only = 1;
6749 break;
6750 default:
6751 usage_histedit();
6752 /* NOTREACHED */
6756 argc -= optind;
6757 argv += optind;
6759 #ifndef PROFILE
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6761 "unveil", NULL) == -1)
6762 err(1, "pledge");
6763 #endif
6764 if (abort_edit && continue_edit)
6765 errx(1, "histedit's -a and -c options are mutually exclusive");
6766 if (edit_script_path && edit_logmsg_only)
6767 errx(1, "histedit's -F and -m options are mutually exclusive");
6768 if (abort_edit && edit_logmsg_only)
6769 errx(1, "histedit's -a and -m options are mutually exclusive");
6770 if (continue_edit && edit_logmsg_only)
6771 errx(1, "histedit's -c and -m options are mutually exclusive");
6772 if (argc != 0)
6773 usage_histedit();
6776 * This command cannot apply unveil(2) in all cases because the
6777 * user may choose to run an editor to edit the histedit script
6778 * and to edit individual commit log messages.
6779 * unveil(2) traverses exec(2); if an editor is used we have to
6780 * apply unveil after edit script and log messages have been written.
6781 * XXX TODO: Make use of unveil(2) where possible.
6784 cwd = getcwd(NULL, 0);
6785 if (cwd == NULL) {
6786 error = got_error_from_errno("getcwd");
6787 goto done;
6789 error = got_worktree_open(&worktree, cwd);
6790 if (error)
6791 goto done;
6793 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6794 NULL);
6795 if (error != NULL)
6796 goto done;
6798 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6799 if (error)
6800 goto done;
6801 if (rebase_in_progress) {
6802 error = got_error(GOT_ERR_REBASING);
6803 goto done;
6806 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6807 if (error)
6808 goto done;
6810 if (edit_in_progress && edit_logmsg_only) {
6811 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6812 "histedit operation is in progress in this "
6813 "work tree and must be continued or aborted "
6814 "before the -m option can be used");
6815 goto done;
6818 if (edit_in_progress && abort_edit) {
6819 error = got_worktree_histedit_continue(&resume_commit_id,
6820 &tmp_branch, &branch, &base_commit_id, &fileindex,
6821 worktree, repo);
6822 if (error)
6823 goto done;
6824 printf("Switching work tree to %s\n",
6825 got_ref_get_symref_target(branch));
6826 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6827 branch, base_commit_id, update_progress, &did_something);
6828 if (error)
6829 goto done;
6830 printf("Histedit of %s aborted\n",
6831 got_ref_get_symref_target(branch));
6832 goto done; /* nothing else to do */
6833 } else if (abort_edit) {
6834 error = got_error(GOT_ERR_NOT_HISTEDIT);
6835 goto done;
6838 if (continue_edit) {
6839 char *path;
6841 if (!edit_in_progress) {
6842 error = got_error(GOT_ERR_NOT_HISTEDIT);
6843 goto done;
6846 error = got_worktree_get_histedit_script_path(&path, worktree);
6847 if (error)
6848 goto done;
6850 error = histedit_load_list(&histedit_cmds, path, repo);
6851 free(path);
6852 if (error)
6853 goto done;
6855 error = got_worktree_histedit_continue(&resume_commit_id,
6856 &tmp_branch, &branch, &base_commit_id, &fileindex,
6857 worktree, repo);
6858 if (error)
6859 goto done;
6861 error = got_ref_resolve(&head_commit_id, repo, branch);
6862 if (error)
6863 goto done;
6865 error = got_object_open_as_commit(&commit, repo,
6866 head_commit_id);
6867 if (error)
6868 goto done;
6869 parent_ids = got_object_commit_get_parent_ids(commit);
6870 pid = SIMPLEQ_FIRST(parent_ids);
6871 if (pid == NULL) {
6872 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6873 goto done;
6875 error = collect_commits(&commits, head_commit_id, pid->id,
6876 base_commit_id, got_worktree_get_path_prefix(worktree),
6877 GOT_ERR_HISTEDIT_PATH, repo);
6878 got_object_commit_close(commit);
6879 commit = NULL;
6880 if (error)
6881 goto done;
6882 } else {
6883 if (edit_in_progress) {
6884 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6885 goto done;
6888 error = got_ref_open(&branch, repo,
6889 got_worktree_get_head_ref_name(worktree), 0);
6890 if (error != NULL)
6891 goto done;
6893 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6894 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6895 "will not edit commit history of a branch outside "
6896 "the \"refs/heads/\" reference namespace");
6897 goto done;
6900 error = got_ref_resolve(&head_commit_id, repo, branch);
6901 got_ref_close(branch);
6902 branch = NULL;
6903 if (error)
6904 goto done;
6906 error = got_object_open_as_commit(&commit, repo,
6907 head_commit_id);
6908 if (error)
6909 goto done;
6910 parent_ids = got_object_commit_get_parent_ids(commit);
6911 pid = SIMPLEQ_FIRST(parent_ids);
6912 if (pid == NULL) {
6913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6914 goto done;
6916 error = collect_commits(&commits, head_commit_id, pid->id,
6917 got_worktree_get_base_commit_id(worktree),
6918 got_worktree_get_path_prefix(worktree),
6919 GOT_ERR_HISTEDIT_PATH, repo);
6920 got_object_commit_close(commit);
6921 commit = NULL;
6922 if (error)
6923 goto done;
6925 if (SIMPLEQ_EMPTY(&commits)) {
6926 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6927 goto done;
6930 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6931 &base_commit_id, &fileindex, worktree, repo);
6932 if (error)
6933 goto done;
6935 if (edit_script_path) {
6936 error = histedit_load_list(&histedit_cmds,
6937 edit_script_path, repo);
6938 if (error) {
6939 got_worktree_histedit_abort(worktree, fileindex,
6940 repo, branch, base_commit_id,
6941 update_progress, &did_something);
6942 goto done;
6944 } else {
6945 const char *branch_name;
6946 branch_name = got_ref_get_symref_target(branch);
6947 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6948 branch_name += 11;
6949 error = histedit_edit_script(&histedit_cmds, &commits,
6950 branch_name, edit_logmsg_only, repo);
6951 if (error) {
6952 got_worktree_histedit_abort(worktree, fileindex,
6953 repo, branch, base_commit_id,
6954 update_progress, &did_something);
6955 goto done;
6960 error = histedit_save_list(&histedit_cmds, worktree,
6961 repo);
6962 if (error) {
6963 got_worktree_histedit_abort(worktree, fileindex,
6964 repo, branch, base_commit_id,
6965 update_progress, &did_something);
6966 goto done;
6971 error = histedit_check_script(&histedit_cmds, &commits, repo);
6972 if (error)
6973 goto done;
6975 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6976 if (resume_commit_id) {
6977 if (got_object_id_cmp(hle->commit_id,
6978 resume_commit_id) != 0)
6979 continue;
6981 resume_commit_id = NULL;
6982 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6983 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6984 error = histedit_skip_commit(hle, worktree,
6985 repo);
6986 if (error)
6987 goto done;
6988 } else {
6989 struct got_pathlist_head paths;
6990 int have_changes = 0;
6992 TAILQ_INIT(&paths);
6993 error = got_pathlist_append(&paths, "", NULL);
6994 if (error)
6995 goto done;
6996 error = got_worktree_status(worktree, &paths,
6997 repo, check_local_changes, &have_changes,
6998 check_cancelled, NULL);
6999 got_pathlist_free(&paths);
7000 if (error) {
7001 if (error->code != GOT_ERR_CANCELLED)
7002 goto done;
7003 if (sigint_received || sigpipe_received)
7004 goto done;
7006 if (have_changes) {
7007 error = histedit_commit(NULL, worktree,
7008 fileindex, tmp_branch, hle, repo);
7009 if (error)
7010 goto done;
7011 } else {
7012 error = got_object_open_as_commit(
7013 &commit, repo, hle->commit_id);
7014 if (error)
7015 goto done;
7016 error = show_histedit_progress(commit,
7017 hle, NULL);
7018 got_object_commit_close(commit);
7019 commit = NULL;
7020 if (error)
7021 goto done;
7024 continue;
7027 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7028 error = histedit_skip_commit(hle, worktree, repo);
7029 if (error)
7030 goto done;
7031 continue;
7034 error = got_object_open_as_commit(&commit, repo,
7035 hle->commit_id);
7036 if (error)
7037 goto done;
7038 parent_ids = got_object_commit_get_parent_ids(commit);
7039 pid = SIMPLEQ_FIRST(parent_ids);
7041 error = got_worktree_histedit_merge_files(&merged_paths,
7042 worktree, fileindex, pid->id, hle->commit_id, repo,
7043 rebase_progress, &rebase_status, check_cancelled, NULL);
7044 if (error)
7045 goto done;
7046 got_object_commit_close(commit);
7047 commit = NULL;
7049 if (rebase_status == GOT_STATUS_CONFLICT) {
7050 error = show_rebase_merge_conflict(hle->commit_id,
7051 repo);
7052 if (error)
7053 goto done;
7054 got_worktree_rebase_pathlist_free(&merged_paths);
7055 break;
7058 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7059 char *id_str;
7060 error = got_object_id_str(&id_str, hle->commit_id);
7061 if (error)
7062 goto done;
7063 printf("Stopping histedit for amending commit %s\n",
7064 id_str);
7065 free(id_str);
7066 got_worktree_rebase_pathlist_free(&merged_paths);
7067 error = got_worktree_histedit_postpone(worktree,
7068 fileindex);
7069 goto done;
7072 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7073 error = histedit_skip_commit(hle, worktree, repo);
7074 if (error)
7075 goto done;
7076 continue;
7079 error = histedit_commit(&merged_paths, worktree, fileindex,
7080 tmp_branch, hle, repo);
7081 got_worktree_rebase_pathlist_free(&merged_paths);
7082 if (error)
7083 goto done;
7086 if (rebase_status == GOT_STATUS_CONFLICT) {
7087 error = got_worktree_histedit_postpone(worktree, fileindex);
7088 if (error)
7089 goto done;
7090 error = got_error_msg(GOT_ERR_CONFLICTS,
7091 "conflicts must be resolved before histedit can continue");
7092 } else
7093 error = histedit_complete(worktree, fileindex, tmp_branch,
7094 branch, repo);
7095 done:
7096 got_object_id_queue_free(&commits);
7097 histedit_free_list(&histedit_cmds);
7098 free(head_commit_id);
7099 free(base_commit_id);
7100 free(resume_commit_id);
7101 if (commit)
7102 got_object_commit_close(commit);
7103 if (branch)
7104 got_ref_close(branch);
7105 if (tmp_branch)
7106 got_ref_close(tmp_branch);
7107 if (worktree)
7108 got_worktree_close(worktree);
7109 if (repo)
7110 got_repo_close(repo);
7111 return error;
7114 __dead static void
7115 usage_integrate(void)
7117 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7118 exit(1);
7121 static const struct got_error *
7122 cmd_integrate(int argc, char *argv[])
7124 const struct got_error *error = NULL;
7125 struct got_repository *repo = NULL;
7126 struct got_worktree *worktree = NULL;
7127 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7128 const char *branch_arg = NULL;
7129 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7130 struct got_fileindex *fileindex = NULL;
7131 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7132 int ch, did_something = 0;
7134 while ((ch = getopt(argc, argv, "")) != -1) {
7135 switch (ch) {
7136 default:
7137 usage_integrate();
7138 /* NOTREACHED */
7142 argc -= optind;
7143 argv += optind;
7145 if (argc != 1)
7146 usage_integrate();
7147 branch_arg = argv[0];
7149 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7150 "unveil", NULL) == -1)
7151 err(1, "pledge");
7153 cwd = getcwd(NULL, 0);
7154 if (cwd == NULL) {
7155 error = got_error_from_errno("getcwd");
7156 goto done;
7159 error = got_worktree_open(&worktree, cwd);
7160 if (error)
7161 goto done;
7163 error = check_rebase_or_histedit_in_progress(worktree);
7164 if (error)
7165 goto done;
7167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7168 NULL);
7169 if (error != NULL)
7170 goto done;
7172 error = apply_unveil(got_repo_get_path(repo), 0,
7173 got_worktree_get_root_path(worktree));
7174 if (error)
7175 goto done;
7177 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7178 error = got_error_from_errno("asprintf");
7179 goto done;
7182 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7183 &base_branch_ref, worktree, refname, repo);
7184 if (error)
7185 goto done;
7187 refname = strdup(got_ref_get_name(branch_ref));
7188 if (refname == NULL) {
7189 error = got_error_from_errno("strdup");
7190 got_worktree_integrate_abort(worktree, fileindex, repo,
7191 branch_ref, base_branch_ref);
7192 goto done;
7194 base_refname = strdup(got_ref_get_name(base_branch_ref));
7195 if (base_refname == NULL) {
7196 error = got_error_from_errno("strdup");
7197 got_worktree_integrate_abort(worktree, fileindex, repo,
7198 branch_ref, base_branch_ref);
7199 goto done;
7202 error = got_ref_resolve(&commit_id, repo, branch_ref);
7203 if (error)
7204 goto done;
7206 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7207 if (error)
7208 goto done;
7210 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7211 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7212 "specified branch has already been integrated");
7213 got_worktree_integrate_abort(worktree, fileindex, repo,
7214 branch_ref, base_branch_ref);
7215 goto done;
7218 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7219 if (error) {
7220 if (error->code == GOT_ERR_ANCESTRY)
7221 error = got_error(GOT_ERR_REBASE_REQUIRED);
7222 got_worktree_integrate_abort(worktree, fileindex, repo,
7223 branch_ref, base_branch_ref);
7224 goto done;
7227 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7228 branch_ref, base_branch_ref, update_progress, &did_something,
7229 check_cancelled, NULL);
7230 if (error)
7231 goto done;
7233 printf("Integrated %s into %s\n", refname, base_refname);
7234 done:
7235 if (repo)
7236 got_repo_close(repo);
7237 if (worktree)
7238 got_worktree_close(worktree);
7239 free(cwd);
7240 free(base_commit_id);
7241 free(commit_id);
7242 free(refname);
7243 free(base_refname);
7244 return error;
7247 __dead static void
7248 usage_stage(void)
7250 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7251 "[file-path ...]\n",
7252 getprogname());
7253 exit(1);
7256 static const struct got_error *
7257 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7258 const char *path, struct got_object_id *blob_id,
7259 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7260 int dirfd, const char *de_name)
7262 const struct got_error *err = NULL;
7263 char *id_str = NULL;
7265 if (staged_status != GOT_STATUS_ADD &&
7266 staged_status != GOT_STATUS_MODIFY &&
7267 staged_status != GOT_STATUS_DELETE)
7268 return NULL;
7270 if (staged_status == GOT_STATUS_ADD ||
7271 staged_status == GOT_STATUS_MODIFY)
7272 err = got_object_id_str(&id_str, staged_blob_id);
7273 else
7274 err = got_object_id_str(&id_str, blob_id);
7275 if (err)
7276 return err;
7278 printf("%s %c %s\n", id_str, staged_status, path);
7279 free(id_str);
7280 return NULL;
7283 static const struct got_error *
7284 cmd_stage(int argc, char *argv[])
7286 const struct got_error *error = NULL;
7287 struct got_repository *repo = NULL;
7288 struct got_worktree *worktree = NULL;
7289 char *cwd = NULL;
7290 struct got_pathlist_head paths;
7291 struct got_pathlist_entry *pe;
7292 int ch, list_stage = 0, pflag = 0;
7293 FILE *patch_script_file = NULL;
7294 const char *patch_script_path = NULL;
7295 struct choose_patch_arg cpa;
7297 TAILQ_INIT(&paths);
7299 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7300 switch (ch) {
7301 case 'l':
7302 list_stage = 1;
7303 break;
7304 case 'p':
7305 pflag = 1;
7306 break;
7307 case 'F':
7308 patch_script_path = optarg;
7309 break;
7310 default:
7311 usage_stage();
7312 /* NOTREACHED */
7316 argc -= optind;
7317 argv += optind;
7319 #ifndef PROFILE
7320 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7321 "unveil", NULL) == -1)
7322 err(1, "pledge");
7323 #endif
7324 if (list_stage && (pflag || patch_script_path))
7325 errx(1, "-l option cannot be used with other options");
7326 if (patch_script_path && !pflag)
7327 errx(1, "-F option can only be used together with -p option");
7329 cwd = getcwd(NULL, 0);
7330 if (cwd == NULL) {
7331 error = got_error_from_errno("getcwd");
7332 goto done;
7335 error = got_worktree_open(&worktree, cwd);
7336 if (error)
7337 goto done;
7339 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7340 NULL);
7341 if (error != NULL)
7342 goto done;
7344 if (patch_script_path) {
7345 patch_script_file = fopen(patch_script_path, "r");
7346 if (patch_script_file == NULL) {
7347 error = got_error_from_errno2("fopen",
7348 patch_script_path);
7349 goto done;
7352 error = apply_unveil(got_repo_get_path(repo), 0,
7353 got_worktree_get_root_path(worktree));
7354 if (error)
7355 goto done;
7357 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7358 if (error)
7359 goto done;
7361 if (list_stage)
7362 error = got_worktree_status(worktree, &paths, repo,
7363 print_stage, NULL, check_cancelled, NULL);
7364 else {
7365 cpa.patch_script_file = patch_script_file;
7366 cpa.action = "stage";
7367 error = got_worktree_stage(worktree, &paths,
7368 pflag ? NULL : print_status, NULL,
7369 pflag ? choose_patch : NULL, &cpa, repo);
7371 done:
7372 if (patch_script_file && fclose(patch_script_file) == EOF &&
7373 error == NULL)
7374 error = got_error_from_errno2("fclose", patch_script_path);
7375 if (repo)
7376 got_repo_close(repo);
7377 if (worktree)
7378 got_worktree_close(worktree);
7379 TAILQ_FOREACH(pe, &paths, entry)
7380 free((char *)pe->path);
7381 got_pathlist_free(&paths);
7382 free(cwd);
7383 return error;
7386 __dead static void
7387 usage_unstage(void)
7389 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7390 "[file-path ...]\n",
7391 getprogname());
7392 exit(1);
7396 static const struct got_error *
7397 cmd_unstage(int argc, char *argv[])
7399 const struct got_error *error = NULL;
7400 struct got_repository *repo = NULL;
7401 struct got_worktree *worktree = NULL;
7402 char *cwd = NULL;
7403 struct got_pathlist_head paths;
7404 struct got_pathlist_entry *pe;
7405 int ch, did_something = 0, pflag = 0;
7406 FILE *patch_script_file = NULL;
7407 const char *patch_script_path = NULL;
7408 struct choose_patch_arg cpa;
7410 TAILQ_INIT(&paths);
7412 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7413 switch (ch) {
7414 case 'p':
7415 pflag = 1;
7416 break;
7417 case 'F':
7418 patch_script_path = optarg;
7419 break;
7420 default:
7421 usage_unstage();
7422 /* NOTREACHED */
7426 argc -= optind;
7427 argv += optind;
7429 #ifndef PROFILE
7430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7431 "unveil", NULL) == -1)
7432 err(1, "pledge");
7433 #endif
7434 if (patch_script_path && !pflag)
7435 errx(1, "-F option can only be used together with -p option");
7437 cwd = getcwd(NULL, 0);
7438 if (cwd == NULL) {
7439 error = got_error_from_errno("getcwd");
7440 goto done;
7443 error = got_worktree_open(&worktree, cwd);
7444 if (error)
7445 goto done;
7447 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7448 NULL);
7449 if (error != NULL)
7450 goto done;
7452 if (patch_script_path) {
7453 patch_script_file = fopen(patch_script_path, "r");
7454 if (patch_script_file == NULL) {
7455 error = got_error_from_errno2("fopen",
7456 patch_script_path);
7457 goto done;
7461 error = apply_unveil(got_repo_get_path(repo), 0,
7462 got_worktree_get_root_path(worktree));
7463 if (error)
7464 goto done;
7466 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7467 if (error)
7468 goto done;
7470 cpa.patch_script_file = patch_script_file;
7471 cpa.action = "unstage";
7472 error = got_worktree_unstage(worktree, &paths, update_progress,
7473 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7474 done:
7475 if (patch_script_file && fclose(patch_script_file) == EOF &&
7476 error == NULL)
7477 error = got_error_from_errno2("fclose", patch_script_path);
7478 if (repo)
7479 got_repo_close(repo);
7480 if (worktree)
7481 got_worktree_close(worktree);
7482 TAILQ_FOREACH(pe, &paths, entry)
7483 free((char *)pe->path);
7484 got_pathlist_free(&paths);
7485 free(cwd);
7486 return error;
7489 __dead static void
7490 usage_cat(void)
7492 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7493 "arg1 [arg2 ...]\n", getprogname());
7494 exit(1);
7497 static const struct got_error *
7498 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7500 const struct got_error *err;
7501 struct got_blob_object *blob;
7503 err = got_object_open_as_blob(&blob, repo, id, 8192);
7504 if (err)
7505 return err;
7507 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7508 got_object_blob_close(blob);
7509 return err;
7512 static const struct got_error *
7513 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7515 const struct got_error *err;
7516 struct got_tree_object *tree;
7517 int nentries, i;
7519 err = got_object_open_as_tree(&tree, repo, id);
7520 if (err)
7521 return err;
7523 nentries = got_object_tree_get_nentries(tree);
7524 for (i = 0; i < nentries; i++) {
7525 struct got_tree_entry *te;
7526 char *id_str;
7527 if (sigint_received || sigpipe_received)
7528 break;
7529 te = got_object_tree_get_entry(tree, i);
7530 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7531 if (err)
7532 break;
7533 fprintf(outfile, "%s %.7o %s\n", id_str,
7534 got_tree_entry_get_mode(te),
7535 got_tree_entry_get_name(te));
7536 free(id_str);
7539 got_object_tree_close(tree);
7540 return err;
7543 static const struct got_error *
7544 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7546 const struct got_error *err;
7547 struct got_commit_object *commit;
7548 const struct got_object_id_queue *parent_ids;
7549 struct got_object_qid *pid;
7550 char *id_str = NULL;
7551 const char *logmsg = NULL;
7553 err = got_object_open_as_commit(&commit, repo, id);
7554 if (err)
7555 return err;
7557 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7558 if (err)
7559 goto done;
7561 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7562 parent_ids = got_object_commit_get_parent_ids(commit);
7563 fprintf(outfile, "numparents %d\n",
7564 got_object_commit_get_nparents(commit));
7565 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7566 char *pid_str;
7567 err = got_object_id_str(&pid_str, pid->id);
7568 if (err)
7569 goto done;
7570 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7571 free(pid_str);
7573 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7574 got_object_commit_get_author(commit),
7575 got_object_commit_get_author_time(commit));
7577 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7578 got_object_commit_get_author(commit),
7579 got_object_commit_get_committer_time(commit));
7581 logmsg = got_object_commit_get_logmsg_raw(commit);
7582 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7583 fprintf(outfile, "%s", logmsg);
7584 done:
7585 free(id_str);
7586 got_object_commit_close(commit);
7587 return err;
7590 static const struct got_error *
7591 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7593 const struct got_error *err;
7594 struct got_tag_object *tag;
7595 char *id_str = NULL;
7596 const char *tagmsg = NULL;
7598 err = got_object_open_as_tag(&tag, repo, id);
7599 if (err)
7600 return err;
7602 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7603 if (err)
7604 goto done;
7606 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7608 switch (got_object_tag_get_object_type(tag)) {
7609 case GOT_OBJ_TYPE_BLOB:
7610 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7611 GOT_OBJ_LABEL_BLOB);
7612 break;
7613 case GOT_OBJ_TYPE_TREE:
7614 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7615 GOT_OBJ_LABEL_TREE);
7616 break;
7617 case GOT_OBJ_TYPE_COMMIT:
7618 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7619 GOT_OBJ_LABEL_COMMIT);
7620 break;
7621 case GOT_OBJ_TYPE_TAG:
7622 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7623 GOT_OBJ_LABEL_TAG);
7624 break;
7625 default:
7626 break;
7629 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7630 got_object_tag_get_name(tag));
7632 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7633 got_object_tag_get_tagger(tag),
7634 got_object_tag_get_tagger_time(tag));
7636 tagmsg = got_object_tag_get_message(tag);
7637 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7638 fprintf(outfile, "%s", tagmsg);
7639 done:
7640 free(id_str);
7641 got_object_tag_close(tag);
7642 return err;
7645 static const struct got_error *
7646 cmd_cat(int argc, char *argv[])
7648 const struct got_error *error;
7649 struct got_repository *repo = NULL;
7650 struct got_worktree *worktree = NULL;
7651 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7652 const char *commit_id_str = NULL;
7653 struct got_object_id *id = NULL, *commit_id = NULL;
7654 int ch, obj_type, i, force_path = 0;
7656 #ifndef PROFILE
7657 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7658 NULL) == -1)
7659 err(1, "pledge");
7660 #endif
7662 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7663 switch (ch) {
7664 case 'c':
7665 commit_id_str = optarg;
7666 break;
7667 case 'r':
7668 repo_path = realpath(optarg, NULL);
7669 if (repo_path == NULL)
7670 return got_error_from_errno2("realpath",
7671 optarg);
7672 got_path_strip_trailing_slashes(repo_path);
7673 break;
7674 case 'P':
7675 force_path = 1;
7676 break;
7677 default:
7678 usage_cat();
7679 /* NOTREACHED */
7683 argc -= optind;
7684 argv += optind;
7686 cwd = getcwd(NULL, 0);
7687 if (cwd == NULL) {
7688 error = got_error_from_errno("getcwd");
7689 goto done;
7691 error = got_worktree_open(&worktree, cwd);
7692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7693 goto done;
7694 if (worktree) {
7695 if (repo_path == NULL) {
7696 repo_path = strdup(
7697 got_worktree_get_repo_path(worktree));
7698 if (repo_path == NULL) {
7699 error = got_error_from_errno("strdup");
7700 goto done;
7705 if (repo_path == NULL) {
7706 repo_path = getcwd(NULL, 0);
7707 if (repo_path == NULL)
7708 return got_error_from_errno("getcwd");
7711 error = got_repo_open(&repo, repo_path, NULL);
7712 free(repo_path);
7713 if (error != NULL)
7714 goto done;
7716 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7717 if (error)
7718 goto done;
7720 if (commit_id_str == NULL)
7721 commit_id_str = GOT_REF_HEAD;
7722 error = got_repo_match_object_id(&commit_id, NULL,
7723 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7724 if (error)
7725 goto done;
7727 for (i = 0; i < argc; i++) {
7728 if (force_path) {
7729 error = got_object_id_by_path(&id, repo, commit_id,
7730 argv[i]);
7731 if (error)
7732 break;
7733 } else {
7734 error = got_repo_match_object_id(&id, &label, argv[i],
7735 GOT_OBJ_TYPE_ANY, 0, repo);
7736 if (error) {
7737 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7738 error->code != GOT_ERR_NOT_REF)
7739 break;
7740 error = got_object_id_by_path(&id, repo,
7741 commit_id, argv[i]);
7742 if (error)
7743 break;
7747 error = got_object_get_type(&obj_type, repo, id);
7748 if (error)
7749 break;
7751 switch (obj_type) {
7752 case GOT_OBJ_TYPE_BLOB:
7753 error = cat_blob(id, repo, stdout);
7754 break;
7755 case GOT_OBJ_TYPE_TREE:
7756 error = cat_tree(id, repo, stdout);
7757 break;
7758 case GOT_OBJ_TYPE_COMMIT:
7759 error = cat_commit(id, repo, stdout);
7760 break;
7761 case GOT_OBJ_TYPE_TAG:
7762 error = cat_tag(id, repo, stdout);
7763 break;
7764 default:
7765 error = got_error(GOT_ERR_OBJ_TYPE);
7766 break;
7768 if (error)
7769 break;
7770 free(label);
7771 label = NULL;
7772 free(id);
7773 id = NULL;
7775 done:
7776 free(label);
7777 free(id);
7778 free(commit_id);
7779 if (worktree)
7780 got_worktree_close(worktree);
7781 if (repo) {
7782 const struct got_error *repo_error;
7783 repo_error = got_repo_close(repo);
7784 if (error == NULL)
7785 error = repo_error;
7787 return error;