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 p;
977 char scaled[FMT_SCALED_STRSIZE];
979 if (message && message[0] != '\0' && message[0] != '\n') {
980 printf("\rserver: %s", message);
981 if (strchr(message, '\n') == 0)
982 printf("\n");
985 if (packfile_size > 0 || nobj_indexed > 0) {
986 printf("\r");
987 if (fmt_scaled(packfile_size, scaled) == 0)
988 printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
989 if (nobj_indexed > 0) {
990 p = (nobj_indexed * 100) / nobj_total;
991 printf("; indexing %d%% (%d)", p, nobj_indexed);
993 if (nobj_resolved > 0) {
994 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
995 printf("; resolving deltas %d%% (%d)",
996 p, nobj_resolved);
998 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
999 nobj_resolved == nobj_total - nobj_loose)
1000 printf("\nWriting pack index...\n");
1003 fflush(stdout);
1004 return NULL;
1007 static const struct got_error *
1008 cmd_clone(int argc, char *argv[])
1010 const struct got_error *err = NULL;
1011 const char *uri, *branch_filter, *dirname;
1012 char *proto, *host, *port, *repo_name, *server_path;
1013 char *default_destdir = NULL, *id_str = NULL;
1014 const char *repo_path;
1015 struct got_repository *repo = NULL;
1016 struct got_pathlist_head refs, symrefs;
1017 struct got_pathlist_entry *pe;
1018 struct got_object_id *pack_hash = NULL;
1019 int ch, fetchfd = -1;
1021 TAILQ_INIT(&refs);
1022 TAILQ_INIT(&symrefs);
1024 while ((ch = getopt(argc, argv, "b:")) != -1) {
1025 switch (ch) {
1026 case 'b':
1027 branch_filter = optarg;
1028 break;
1029 default:
1030 usage_clone();
1031 break;
1034 argc -= optind;
1035 argv += optind;
1036 uri = argv[0];
1037 if(argc == 1)
1038 dirname = NULL;
1039 else if(argc == 2)
1040 dirname = argv[1];
1041 else
1042 usage_clone();
1044 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1045 &repo_name, argv[0]);
1046 if (err)
1047 goto done;
1049 if (dirname == NULL) {
1050 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1051 err = got_error_from_errno("asprintf");
1052 goto done;
1054 repo_path = default_destdir;
1055 } else
1056 repo_path = dirname;
1058 err = got_path_mkdir(repo_path);
1059 if (err)
1060 goto done;
1062 err = got_repo_init(repo_path);
1063 if (err)
1064 goto done;
1066 err = got_repo_open(&repo, repo_path, NULL);
1067 if (err)
1068 goto done;
1070 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1071 if (err)
1072 goto done;
1074 printf("Connected to %s:%s\n", host, port);
1076 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1077 repo, fetch_progress, NULL);
1078 if (err)
1079 goto done;
1081 err = got_object_id_str(&id_str, pack_hash);
1082 if (err)
1083 goto done;
1084 printf("Fetched %s.pack\n", id_str);
1085 free(id_str);
1087 /* Set up references provided with the pack file. */
1088 TAILQ_FOREACH(pe, &refs, entry) {
1089 const char *refname = pe->path;
1090 struct got_object_id *id = pe->data;
1091 struct got_reference *ref;
1094 err = got_ref_alloc(&ref, refname, id);
1095 if (err)
1096 goto done;
1098 #if 0
1099 err = got_object_id_str(&id_str, id);
1100 if (err)
1101 goto done;
1102 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1103 free(id_str);
1104 #endif
1105 err = got_ref_write(ref, repo);
1106 got_ref_close(ref);
1107 if (err)
1108 goto done;
1111 /* Set the HEAD reference if the server provided one. */
1112 TAILQ_FOREACH(pe, &symrefs, entry) {
1113 struct got_reference *symref, *target_ref;
1114 const char *refname = pe->path;
1115 const char *target = pe->data;
1117 if (strcmp(refname, GOT_REF_HEAD) != 0)
1118 continue;
1120 err = got_ref_open(&target_ref, repo, target, 0);
1121 if (err) {
1122 if (err->code == GOT_ERR_NOT_REF)
1123 continue;
1124 goto done;
1127 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1128 got_ref_close(target_ref);
1129 if (err)
1130 goto done;
1132 printf("Setting %s to %s\n", GOT_REF_HEAD,
1133 got_ref_get_symref_target(symref));
1135 err = got_ref_write(symref, repo);
1136 got_ref_close(symref);
1137 break;
1140 done:
1141 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1142 err = got_error_from_errno("close");
1143 if (repo)
1144 got_repo_close(repo);
1145 TAILQ_FOREACH(pe, &refs, entry) {
1146 free((void *)pe->path);
1147 free(pe->data);
1149 got_pathlist_free(&refs);
1150 TAILQ_FOREACH(pe, &symrefs, entry) {
1151 free((void *)pe->path);
1152 free(pe->data);
1154 got_pathlist_free(&symrefs);
1155 free(pack_hash);
1156 free(proto);
1157 free(host);
1158 free(port);
1159 free(server_path);
1160 free(repo_name);
1161 free(default_destdir);
1162 return err;
1165 static const struct got_error *
1166 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1168 static char msg[512];
1169 const char *branch_name;
1171 if (got_ref_is_symbolic(ref))
1172 branch_name = got_ref_get_symref_target(ref);
1173 else
1174 branch_name = got_ref_get_name(ref);
1176 if (strncmp("refs/heads/", branch_name, 11) == 0)
1177 branch_name += 11;
1179 snprintf(msg, sizeof(msg),
1180 "target commit is not contained in branch '%s'; "
1181 "the branch to use must be specified with -b; "
1182 "if necessary a new branch can be created for "
1183 "this commit with 'got branch -c %s BRANCH_NAME'",
1184 branch_name, commit_id_str);
1186 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1189 static const struct got_error *
1190 cmd_checkout(int argc, char *argv[])
1192 const struct got_error *error = NULL;
1193 struct got_repository *repo = NULL;
1194 struct got_reference *head_ref = NULL;
1195 struct got_worktree *worktree = NULL;
1196 char *repo_path = NULL;
1197 char *worktree_path = NULL;
1198 const char *path_prefix = "";
1199 const char *branch_name = GOT_REF_HEAD;
1200 char *commit_id_str = NULL;
1201 int ch, same_path_prefix, allow_nonempty = 0;
1202 struct got_pathlist_head paths;
1203 struct got_checkout_progress_arg cpa;
1205 TAILQ_INIT(&paths);
1207 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1208 switch (ch) {
1209 case 'b':
1210 branch_name = optarg;
1211 break;
1212 case 'c':
1213 commit_id_str = strdup(optarg);
1214 if (commit_id_str == NULL)
1215 return got_error_from_errno("strdup");
1216 break;
1217 case 'E':
1218 allow_nonempty = 1;
1219 break;
1220 case 'p':
1221 path_prefix = optarg;
1222 break;
1223 default:
1224 usage_checkout();
1225 /* NOTREACHED */
1229 argc -= optind;
1230 argv += optind;
1232 #ifndef PROFILE
1233 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1234 "unveil", NULL) == -1)
1235 err(1, "pledge");
1236 #endif
1237 if (argc == 1) {
1238 char *cwd, *base, *dotgit;
1239 repo_path = realpath(argv[0], NULL);
1240 if (repo_path == NULL)
1241 return got_error_from_errno2("realpath", argv[0]);
1242 cwd = getcwd(NULL, 0);
1243 if (cwd == NULL) {
1244 error = got_error_from_errno("getcwd");
1245 goto done;
1247 if (path_prefix[0]) {
1248 base = basename(path_prefix);
1249 if (base == NULL) {
1250 error = got_error_from_errno2("basename",
1251 path_prefix);
1252 goto done;
1254 } else {
1255 base = basename(repo_path);
1256 if (base == NULL) {
1257 error = got_error_from_errno2("basename",
1258 repo_path);
1259 goto done;
1262 dotgit = strstr(base, ".git");
1263 if (dotgit)
1264 *dotgit = '\0';
1265 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1266 error = got_error_from_errno("asprintf");
1267 free(cwd);
1268 goto done;
1270 free(cwd);
1271 } else if (argc == 2) {
1272 repo_path = realpath(argv[0], NULL);
1273 if (repo_path == NULL) {
1274 error = got_error_from_errno2("realpath", argv[0]);
1275 goto done;
1277 worktree_path = realpath(argv[1], NULL);
1278 if (worktree_path == NULL) {
1279 if (errno != ENOENT) {
1280 error = got_error_from_errno2("realpath",
1281 argv[1]);
1282 goto done;
1284 worktree_path = strdup(argv[1]);
1285 if (worktree_path == NULL) {
1286 error = got_error_from_errno("strdup");
1287 goto done;
1290 } else
1291 usage_checkout();
1293 got_path_strip_trailing_slashes(repo_path);
1294 got_path_strip_trailing_slashes(worktree_path);
1296 error = got_repo_open(&repo, repo_path, NULL);
1297 if (error != NULL)
1298 goto done;
1300 /* Pre-create work tree path for unveil(2) */
1301 error = got_path_mkdir(worktree_path);
1302 if (error) {
1303 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1304 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1305 goto done;
1306 if (!allow_nonempty &&
1307 !got_path_dir_is_empty(worktree_path)) {
1308 error = got_error_path(worktree_path,
1309 GOT_ERR_DIR_NOT_EMPTY);
1310 goto done;
1314 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1315 if (error)
1316 goto done;
1318 error = got_ref_open(&head_ref, repo, branch_name, 0);
1319 if (error != NULL)
1320 goto done;
1322 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1323 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1324 goto done;
1326 error = got_worktree_open(&worktree, worktree_path);
1327 if (error != NULL)
1328 goto done;
1330 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1331 path_prefix);
1332 if (error != NULL)
1333 goto done;
1334 if (!same_path_prefix) {
1335 error = got_error(GOT_ERR_PATH_PREFIX);
1336 goto done;
1339 if (commit_id_str) {
1340 struct got_object_id *commit_id;
1341 error = got_repo_match_object_id(&commit_id, NULL,
1342 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1343 if (error)
1344 goto done;
1345 error = check_linear_ancestry(commit_id,
1346 got_worktree_get_base_commit_id(worktree), 0, repo);
1347 if (error != NULL) {
1348 free(commit_id);
1349 if (error->code == GOT_ERR_ANCESTRY) {
1350 error = checkout_ancestry_error(
1351 head_ref, commit_id_str);
1353 goto done;
1355 error = check_same_branch(commit_id, head_ref, NULL, repo);
1356 if (error) {
1357 if (error->code == GOT_ERR_ANCESTRY) {
1358 error = checkout_ancestry_error(
1359 head_ref, commit_id_str);
1361 goto done;
1363 error = got_worktree_set_base_commit_id(worktree, repo,
1364 commit_id);
1365 free(commit_id);
1366 if (error)
1367 goto done;
1370 error = got_pathlist_append(&paths, "", NULL);
1371 if (error)
1372 goto done;
1373 cpa.worktree_path = worktree_path;
1374 cpa.had_base_commit_ref_error = 0;
1375 error = got_worktree_checkout_files(worktree, &paths, repo,
1376 checkout_progress, &cpa, check_cancelled, NULL);
1377 if (error != NULL)
1378 goto done;
1380 printf("Now shut up and hack\n");
1381 if (cpa.had_base_commit_ref_error)
1382 show_worktree_base_ref_warning();
1383 done:
1384 got_pathlist_free(&paths);
1385 free(commit_id_str);
1386 free(repo_path);
1387 free(worktree_path);
1388 return error;
1391 __dead static void
1392 usage_update(void)
1394 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1395 getprogname());
1396 exit(1);
1399 static const struct got_error *
1400 update_progress(void *arg, unsigned char status, const char *path)
1402 int *did_something = arg;
1404 if (status == GOT_STATUS_EXISTS ||
1405 status == GOT_STATUS_BASE_REF_ERR)
1406 return NULL;
1408 *did_something = 1;
1410 /* Base commit bump happens silently. */
1411 if (status == GOT_STATUS_BUMP_BASE)
1412 return NULL;
1414 while (path[0] == '/')
1415 path++;
1416 printf("%c %s\n", status, path);
1417 return NULL;
1420 static const struct got_error *
1421 switch_head_ref(struct got_reference *head_ref,
1422 struct got_object_id *commit_id, struct got_worktree *worktree,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 char *base_id_str;
1427 int ref_has_moved = 0;
1429 /* Trivial case: switching between two different references. */
1430 if (strcmp(got_ref_get_name(head_ref),
1431 got_worktree_get_head_ref_name(worktree)) != 0) {
1432 printf("Switching work tree from %s to %s\n",
1433 got_worktree_get_head_ref_name(worktree),
1434 got_ref_get_name(head_ref));
1435 return got_worktree_set_head_ref(worktree, head_ref);
1438 err = check_linear_ancestry(commit_id,
1439 got_worktree_get_base_commit_id(worktree), 0, repo);
1440 if (err) {
1441 if (err->code != GOT_ERR_ANCESTRY)
1442 return err;
1443 ref_has_moved = 1;
1445 if (!ref_has_moved)
1446 return NULL;
1448 /* Switching to a rebased branch with the same reference name. */
1449 err = got_object_id_str(&base_id_str,
1450 got_worktree_get_base_commit_id(worktree));
1451 if (err)
1452 return err;
1453 printf("Reference %s now points at a different branch\n",
1454 got_worktree_get_head_ref_name(worktree));
1455 printf("Switching work tree from %s to %s\n", base_id_str,
1456 got_worktree_get_head_ref_name(worktree));
1457 return NULL;
1460 static const struct got_error *
1461 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1463 const struct got_error *err;
1464 int in_progress;
1466 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1467 if (err)
1468 return err;
1469 if (in_progress)
1470 return got_error(GOT_ERR_REBASING);
1472 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1473 if (err)
1474 return err;
1475 if (in_progress)
1476 return got_error(GOT_ERR_HISTEDIT_BUSY);
1478 return NULL;
1481 static const struct got_error *
1482 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1483 char *argv[], struct got_worktree *worktree)
1485 const struct got_error *err = NULL;
1486 char *path;
1487 int i;
1489 if (argc == 0) {
1490 path = strdup("");
1491 if (path == NULL)
1492 return got_error_from_errno("strdup");
1493 return got_pathlist_append(paths, path, NULL);
1496 for (i = 0; i < argc; i++) {
1497 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1498 if (err)
1499 break;
1500 err = got_pathlist_append(paths, path, NULL);
1501 if (err) {
1502 free(path);
1503 break;
1507 return err;
1510 static const struct got_error *
1511 cmd_update(int argc, char *argv[])
1513 const struct got_error *error = NULL;
1514 struct got_repository *repo = NULL;
1515 struct got_worktree *worktree = NULL;
1516 char *worktree_path = NULL;
1517 struct got_object_id *commit_id = NULL;
1518 char *commit_id_str = NULL;
1519 const char *branch_name = NULL;
1520 struct got_reference *head_ref = NULL;
1521 struct got_pathlist_head paths;
1522 struct got_pathlist_entry *pe;
1523 int ch, did_something = 0;
1525 TAILQ_INIT(&paths);
1527 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1528 switch (ch) {
1529 case 'b':
1530 branch_name = optarg;
1531 break;
1532 case 'c':
1533 commit_id_str = strdup(optarg);
1534 if (commit_id_str == NULL)
1535 return got_error_from_errno("strdup");
1536 break;
1537 default:
1538 usage_update();
1539 /* NOTREACHED */
1543 argc -= optind;
1544 argv += optind;
1546 #ifndef PROFILE
1547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1548 "unveil", NULL) == -1)
1549 err(1, "pledge");
1550 #endif
1551 worktree_path = getcwd(NULL, 0);
1552 if (worktree_path == NULL) {
1553 error = got_error_from_errno("getcwd");
1554 goto done;
1556 error = got_worktree_open(&worktree, worktree_path);
1557 if (error)
1558 goto done;
1560 error = check_rebase_or_histedit_in_progress(worktree);
1561 if (error)
1562 goto done;
1564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1565 NULL);
1566 if (error != NULL)
1567 goto done;
1569 error = apply_unveil(got_repo_get_path(repo), 0,
1570 got_worktree_get_root_path(worktree));
1571 if (error)
1572 goto done;
1574 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1575 if (error)
1576 goto done;
1578 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1579 got_worktree_get_head_ref_name(worktree), 0);
1580 if (error != NULL)
1581 goto done;
1582 if (commit_id_str == NULL) {
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1584 if (error != NULL)
1585 goto done;
1586 error = got_object_id_str(&commit_id_str, commit_id);
1587 if (error != NULL)
1588 goto done;
1589 } else {
1590 error = got_repo_match_object_id(&commit_id, NULL,
1591 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1592 free(commit_id_str);
1593 commit_id_str = NULL;
1594 if (error)
1595 goto done;
1596 error = got_object_id_str(&commit_id_str, commit_id);
1597 if (error)
1598 goto done;
1601 if (branch_name) {
1602 struct got_object_id *head_commit_id;
1603 TAILQ_FOREACH(pe, &paths, entry) {
1604 if (pe->path_len == 0)
1605 continue;
1606 error = got_error_msg(GOT_ERR_BAD_PATH,
1607 "switching between branches requires that "
1608 "the entire work tree gets updated");
1609 goto done;
1611 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1612 if (error)
1613 goto done;
1614 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1615 repo);
1616 free(head_commit_id);
1617 if (error != NULL)
1618 goto done;
1619 error = check_same_branch(commit_id, head_ref, NULL, repo);
1620 if (error)
1621 goto done;
1622 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1623 if (error)
1624 goto done;
1625 } else {
1626 error = check_linear_ancestry(commit_id,
1627 got_worktree_get_base_commit_id(worktree), 0, repo);
1628 if (error != NULL) {
1629 if (error->code == GOT_ERR_ANCESTRY)
1630 error = got_error(GOT_ERR_BRANCH_MOVED);
1631 goto done;
1633 error = check_same_branch(commit_id, head_ref, NULL, repo);
1634 if (error)
1635 goto done;
1638 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1639 commit_id) != 0) {
1640 error = got_worktree_set_base_commit_id(worktree, repo,
1641 commit_id);
1642 if (error)
1643 goto done;
1646 error = got_worktree_checkout_files(worktree, &paths, repo,
1647 update_progress, &did_something, check_cancelled, NULL);
1648 if (error != NULL)
1649 goto done;
1651 if (did_something)
1652 printf("Updated to commit %s\n", commit_id_str);
1653 else
1654 printf("Already up-to-date\n");
1655 done:
1656 free(worktree_path);
1657 TAILQ_FOREACH(pe, &paths, entry)
1658 free((char *)pe->path);
1659 got_pathlist_free(&paths);
1660 free(commit_id);
1661 free(commit_id_str);
1662 return error;
1665 static const struct got_error *
1666 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1667 const char *path, int diff_context, int ignore_whitespace,
1668 struct got_repository *repo)
1670 const struct got_error *err = NULL;
1671 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1673 if (blob_id1) {
1674 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1675 if (err)
1676 goto done;
1679 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1680 if (err)
1681 goto done;
1683 while (path[0] == '/')
1684 path++;
1685 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1686 ignore_whitespace, stdout);
1687 done:
1688 if (blob1)
1689 got_object_blob_close(blob1);
1690 got_object_blob_close(blob2);
1691 return err;
1694 static const struct got_error *
1695 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1696 const char *path, int diff_context, int ignore_whitespace,
1697 struct got_repository *repo)
1699 const struct got_error *err = NULL;
1700 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1701 struct got_diff_blob_output_unidiff_arg arg;
1703 if (tree_id1) {
1704 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1705 if (err)
1706 goto done;
1709 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1710 if (err)
1711 goto done;
1713 arg.diff_context = diff_context;
1714 arg.ignore_whitespace = ignore_whitespace;
1715 arg.outfile = stdout;
1716 while (path[0] == '/')
1717 path++;
1718 err = got_diff_tree(tree1, tree2, path, path, repo,
1719 got_diff_blob_output_unidiff, &arg, 1);
1720 done:
1721 if (tree1)
1722 got_object_tree_close(tree1);
1723 if (tree2)
1724 got_object_tree_close(tree2);
1725 return err;
1728 static const struct got_error *
1729 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1730 const char *path, int diff_context, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_commit_object *pcommit = NULL;
1734 char *id_str1 = NULL, *id_str2 = NULL;
1735 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1736 struct got_object_qid *qid;
1738 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1739 if (qid != NULL) {
1740 err = got_object_open_as_commit(&pcommit, repo,
1741 qid->id);
1742 if (err)
1743 return err;
1746 if (path && path[0] != '\0') {
1747 int obj_type;
1748 err = got_object_id_by_path(&obj_id2, repo, id, path);
1749 if (err)
1750 goto done;
1751 err = got_object_id_str(&id_str2, obj_id2);
1752 if (err) {
1753 free(obj_id2);
1754 goto done;
1756 if (pcommit) {
1757 err = got_object_id_by_path(&obj_id1, repo,
1758 qid->id, path);
1759 if (err) {
1760 free(obj_id2);
1761 goto done;
1763 err = got_object_id_str(&id_str1, obj_id1);
1764 if (err) {
1765 free(obj_id2);
1766 goto done;
1769 err = got_object_get_type(&obj_type, repo, obj_id2);
1770 if (err) {
1771 free(obj_id2);
1772 goto done;
1774 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1775 switch (obj_type) {
1776 case GOT_OBJ_TYPE_BLOB:
1777 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1778 0, repo);
1779 break;
1780 case GOT_OBJ_TYPE_TREE:
1781 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1782 0, repo);
1783 break;
1784 default:
1785 err = got_error(GOT_ERR_OBJ_TYPE);
1786 break;
1788 free(obj_id1);
1789 free(obj_id2);
1790 } else {
1791 obj_id2 = got_object_commit_get_tree_id(commit);
1792 err = got_object_id_str(&id_str2, obj_id2);
1793 if (err)
1794 goto done;
1795 obj_id1 = got_object_commit_get_tree_id(pcommit);
1796 err = got_object_id_str(&id_str1, obj_id1);
1797 if (err)
1798 goto done;
1799 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1800 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1802 done:
1803 free(id_str1);
1804 free(id_str2);
1805 if (pcommit)
1806 got_object_commit_close(pcommit);
1807 return err;
1810 static char *
1811 get_datestr(time_t *time, char *datebuf)
1813 struct tm mytm, *tm;
1814 char *p, *s;
1816 tm = gmtime_r(time, &mytm);
1817 if (tm == NULL)
1818 return NULL;
1819 s = asctime_r(tm, datebuf);
1820 if (s == NULL)
1821 return NULL;
1822 p = strchr(s, '\n');
1823 if (p)
1824 *p = '\0';
1825 return s;
1828 static const struct got_error *
1829 match_logmsg(int *have_match, struct got_object_id *id,
1830 struct got_commit_object *commit, regex_t *regex)
1832 const struct got_error *err = NULL;
1833 regmatch_t regmatch;
1834 char *id_str = NULL, *logmsg = NULL;
1836 *have_match = 0;
1838 err = got_object_id_str(&id_str, id);
1839 if (err)
1840 return err;
1842 err = got_object_commit_get_logmsg(&logmsg, commit);
1843 if (err)
1844 goto done;
1846 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1847 *have_match = 1;
1848 done:
1849 free(id_str);
1850 free(logmsg);
1851 return err;
1854 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1856 static const struct got_error *
1857 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1858 struct got_repository *repo, const char *path, int show_patch,
1859 int diff_context, struct got_reflist_head *refs)
1861 const struct got_error *err = NULL;
1862 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1863 char datebuf[26];
1864 time_t committer_time;
1865 const char *author, *committer;
1866 char *refs_str = NULL;
1867 struct got_reflist_entry *re;
1869 SIMPLEQ_FOREACH(re, refs, entry) {
1870 char *s;
1871 const char *name;
1872 struct got_tag_object *tag = NULL;
1873 int cmp;
1875 name = got_ref_get_name(re->ref);
1876 if (strcmp(name, GOT_REF_HEAD) == 0)
1877 continue;
1878 if (strncmp(name, "refs/", 5) == 0)
1879 name += 5;
1880 if (strncmp(name, "got/", 4) == 0)
1881 continue;
1882 if (strncmp(name, "heads/", 6) == 0)
1883 name += 6;
1884 if (strncmp(name, "remotes/", 8) == 0)
1885 name += 8;
1886 if (strncmp(name, "tags/", 5) == 0) {
1887 err = got_object_open_as_tag(&tag, repo, re->id);
1888 if (err) {
1889 if (err->code != GOT_ERR_OBJ_TYPE)
1890 return err;
1891 /* Ref points at something other than a tag. */
1892 err = NULL;
1893 tag = NULL;
1896 cmp = got_object_id_cmp(tag ?
1897 got_object_tag_get_object_id(tag) : re->id, id);
1898 if (tag)
1899 got_object_tag_close(tag);
1900 if (cmp != 0)
1901 continue;
1902 s = refs_str;
1903 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1904 name) == -1) {
1905 err = got_error_from_errno("asprintf");
1906 free(s);
1907 return err;
1909 free(s);
1911 err = got_object_id_str(&id_str, id);
1912 if (err)
1913 return err;
1915 printf(GOT_COMMIT_SEP_STR);
1916 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1917 refs_str ? refs_str : "", refs_str ? ")" : "");
1918 free(id_str);
1919 id_str = NULL;
1920 free(refs_str);
1921 refs_str = NULL;
1922 printf("from: %s\n", got_object_commit_get_author(commit));
1923 committer_time = got_object_commit_get_committer_time(commit);
1924 datestr = get_datestr(&committer_time, datebuf);
1925 if (datestr)
1926 printf("date: %s UTC\n", datestr);
1927 author = got_object_commit_get_author(commit);
1928 committer = got_object_commit_get_committer(commit);
1929 if (strcmp(author, committer) != 0)
1930 printf("via: %s\n", committer);
1931 if (got_object_commit_get_nparents(commit) > 1) {
1932 const struct got_object_id_queue *parent_ids;
1933 struct got_object_qid *qid;
1934 int n = 1;
1935 parent_ids = got_object_commit_get_parent_ids(commit);
1936 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1937 err = got_object_id_str(&id_str, qid->id);
1938 if (err)
1939 return err;
1940 printf("parent %d: %s\n", n++, id_str);
1941 free(id_str);
1945 err = got_object_commit_get_logmsg(&logmsg0, commit);
1946 if (err)
1947 return err;
1949 logmsg = logmsg0;
1950 do {
1951 line = strsep(&logmsg, "\n");
1952 if (line)
1953 printf(" %s\n", line);
1954 } while (line);
1955 free(logmsg0);
1957 if (show_patch) {
1958 err = print_patch(commit, id, path, diff_context, repo);
1959 if (err == 0)
1960 printf("\n");
1963 if (fflush(stdout) != 0 && err == NULL)
1964 err = got_error_from_errno("fflush");
1965 return err;
1968 static const struct got_error *
1969 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1970 const char *path, int show_patch, const char *search_pattern,
1971 int diff_context, int limit, int log_branches,
1972 struct got_reflist_head *refs)
1974 const struct got_error *err;
1975 struct got_commit_graph *graph;
1976 regex_t regex;
1977 int have_match;
1979 if (search_pattern &&
1980 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1981 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1983 err = got_commit_graph_open(&graph, path, !log_branches);
1984 if (err)
1985 return err;
1986 err = got_commit_graph_iter_start(graph, root_id, repo,
1987 check_cancelled, NULL);
1988 if (err)
1989 goto done;
1990 for (;;) {
1991 struct got_commit_object *commit;
1992 struct got_object_id *id;
1994 if (sigint_received || sigpipe_received)
1995 break;
1997 err = got_commit_graph_iter_next(&id, graph, repo,
1998 check_cancelled, NULL);
1999 if (err) {
2000 if (err->code == GOT_ERR_ITER_COMPLETED)
2001 err = NULL;
2002 break;
2004 if (id == NULL)
2005 break;
2007 err = got_object_open_as_commit(&commit, repo, id);
2008 if (err)
2009 break;
2011 if (search_pattern) {
2012 err = match_logmsg(&have_match, id, commit, &regex);
2013 if (err) {
2014 got_object_commit_close(commit);
2015 break;
2017 if (have_match == 0) {
2018 got_object_commit_close(commit);
2019 continue;
2023 err = print_commit(commit, id, repo, path, show_patch,
2024 diff_context, refs);
2025 got_object_commit_close(commit);
2026 if (err || (limit && --limit == 0))
2027 break;
2029 done:
2030 if (search_pattern)
2031 regfree(&regex);
2032 got_commit_graph_close(graph);
2033 return err;
2036 __dead static void
2037 usage_log(void)
2039 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2040 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2041 exit(1);
2044 static int
2045 get_default_log_limit(void)
2047 const char *got_default_log_limit;
2048 long long n;
2049 const char *errstr;
2051 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2052 if (got_default_log_limit == NULL)
2053 return 0;
2054 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2055 if (errstr != NULL)
2056 return 0;
2057 return n;
2060 static const struct got_error *
2061 cmd_log(int argc, char *argv[])
2063 const struct got_error *error;
2064 struct got_repository *repo = NULL;
2065 struct got_worktree *worktree = NULL;
2066 struct got_commit_object *commit = NULL;
2067 struct got_object_id *id = NULL;
2068 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2069 const char *start_commit = NULL, *search_pattern = NULL;
2070 int diff_context = -1, ch;
2071 int show_patch = 0, limit = 0, log_branches = 0;
2072 const char *errstr;
2073 struct got_reflist_head refs;
2075 SIMPLEQ_INIT(&refs);
2077 #ifndef PROFILE
2078 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2079 NULL)
2080 == -1)
2081 err(1, "pledge");
2082 #endif
2084 limit = get_default_log_limit();
2086 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2087 switch (ch) {
2088 case 'p':
2089 show_patch = 1;
2090 break;
2091 case 'c':
2092 start_commit = optarg;
2093 break;
2094 case 'C':
2095 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2096 &errstr);
2097 if (errstr != NULL)
2098 err(1, "-C option %s", errstr);
2099 break;
2100 case 'l':
2101 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2102 if (errstr != NULL)
2103 err(1, "-l option %s", errstr);
2104 break;
2105 case 'b':
2106 log_branches = 1;
2107 break;
2108 case 'r':
2109 repo_path = realpath(optarg, NULL);
2110 if (repo_path == NULL)
2111 return got_error_from_errno2("realpath",
2112 optarg);
2113 got_path_strip_trailing_slashes(repo_path);
2114 break;
2115 case 's':
2116 search_pattern = optarg;
2117 break;
2118 default:
2119 usage_log();
2120 /* NOTREACHED */
2124 argc -= optind;
2125 argv += optind;
2127 if (diff_context == -1)
2128 diff_context = 3;
2129 else if (!show_patch)
2130 errx(1, "-C reguires -p");
2132 cwd = getcwd(NULL, 0);
2133 if (cwd == NULL) {
2134 error = got_error_from_errno("getcwd");
2135 goto done;
2138 error = got_worktree_open(&worktree, cwd);
2139 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2140 goto done;
2141 error = NULL;
2143 if (argc == 0) {
2144 path = strdup("");
2145 if (path == NULL) {
2146 error = got_error_from_errno("strdup");
2147 goto done;
2149 } else if (argc == 1) {
2150 if (worktree) {
2151 error = got_worktree_resolve_path(&path, worktree,
2152 argv[0]);
2153 if (error)
2154 goto done;
2155 } else {
2156 path = strdup(argv[0]);
2157 if (path == NULL) {
2158 error = got_error_from_errno("strdup");
2159 goto done;
2162 } else
2163 usage_log();
2165 if (repo_path == NULL) {
2166 repo_path = worktree ?
2167 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2169 if (repo_path == NULL) {
2170 error = got_error_from_errno("strdup");
2171 goto done;
2174 error = got_repo_open(&repo, repo_path, NULL);
2175 if (error != NULL)
2176 goto done;
2178 error = apply_unveil(got_repo_get_path(repo), 1,
2179 worktree ? got_worktree_get_root_path(worktree) : NULL);
2180 if (error)
2181 goto done;
2183 if (start_commit == NULL) {
2184 struct got_reference *head_ref;
2185 error = got_ref_open(&head_ref, repo,
2186 worktree ? got_worktree_get_head_ref_name(worktree)
2187 : GOT_REF_HEAD, 0);
2188 if (error != NULL)
2189 return error;
2190 error = got_ref_resolve(&id, repo, head_ref);
2191 got_ref_close(head_ref);
2192 if (error != NULL)
2193 return error;
2194 error = got_object_open_as_commit(&commit, repo, id);
2195 } else {
2196 struct got_reference *ref;
2197 error = got_ref_open(&ref, repo, start_commit, 0);
2198 if (error == NULL) {
2199 int obj_type;
2200 error = got_ref_resolve(&id, repo, ref);
2201 got_ref_close(ref);
2202 if (error != NULL)
2203 goto done;
2204 error = got_object_get_type(&obj_type, repo, id);
2205 if (error != NULL)
2206 goto done;
2207 if (obj_type == GOT_OBJ_TYPE_TAG) {
2208 struct got_tag_object *tag;
2209 error = got_object_open_as_tag(&tag, repo, id);
2210 if (error != NULL)
2211 goto done;
2212 if (got_object_tag_get_object_type(tag) !=
2213 GOT_OBJ_TYPE_COMMIT) {
2214 got_object_tag_close(tag);
2215 error = got_error(GOT_ERR_OBJ_TYPE);
2216 goto done;
2218 free(id);
2219 id = got_object_id_dup(
2220 got_object_tag_get_object_id(tag));
2221 if (id == NULL)
2222 error = got_error_from_errno(
2223 "got_object_id_dup");
2224 got_object_tag_close(tag);
2225 if (error)
2226 goto done;
2227 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2228 error = got_error(GOT_ERR_OBJ_TYPE);
2229 goto done;
2231 error = got_object_open_as_commit(&commit, repo, id);
2232 if (error != NULL)
2233 goto done;
2235 if (commit == NULL) {
2236 error = got_repo_match_object_id_prefix(&id,
2237 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2238 if (error != NULL)
2239 return error;
2242 if (error != NULL)
2243 goto done;
2245 if (worktree) {
2246 const char *prefix = got_worktree_get_path_prefix(worktree);
2247 char *p;
2248 if (asprintf(&p, "%s%s%s", prefix,
2249 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2250 error = got_error_from_errno("asprintf");
2251 goto done;
2253 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2254 free(p);
2255 } else
2256 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2257 if (error != NULL)
2258 goto done;
2259 if (in_repo_path) {
2260 free(path);
2261 path = in_repo_path;
2264 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2265 if (error)
2266 goto done;
2268 error = print_commits(id, repo, path, show_patch, search_pattern,
2269 diff_context, limit, log_branches, &refs);
2270 done:
2271 free(path);
2272 free(repo_path);
2273 free(cwd);
2274 free(id);
2275 if (worktree)
2276 got_worktree_close(worktree);
2277 if (repo) {
2278 const struct got_error *repo_error;
2279 repo_error = got_repo_close(repo);
2280 if (error == NULL)
2281 error = repo_error;
2283 got_ref_list_free(&refs);
2284 return error;
2287 __dead static void
2288 usage_diff(void)
2290 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2291 "[-w] [object1 object2 | path]\n", getprogname());
2292 exit(1);
2295 struct print_diff_arg {
2296 struct got_repository *repo;
2297 struct got_worktree *worktree;
2298 int diff_context;
2299 const char *id_str;
2300 int header_shown;
2301 int diff_staged;
2302 int ignore_whitespace;
2305 static const struct got_error *
2306 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2307 const char *path, struct got_object_id *blob_id,
2308 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2309 int dirfd, const char *de_name)
2311 struct print_diff_arg *a = arg;
2312 const struct got_error *err = NULL;
2313 struct got_blob_object *blob1 = NULL;
2314 int fd = -1;
2315 FILE *f2 = NULL;
2316 char *abspath = NULL, *label1 = NULL;
2317 struct stat sb;
2319 if (a->diff_staged) {
2320 if (staged_status != GOT_STATUS_MODIFY &&
2321 staged_status != GOT_STATUS_ADD &&
2322 staged_status != GOT_STATUS_DELETE)
2323 return NULL;
2324 } else {
2325 if (staged_status == GOT_STATUS_DELETE)
2326 return NULL;
2327 if (status == GOT_STATUS_NONEXISTENT)
2328 return got_error_set_errno(ENOENT, path);
2329 if (status != GOT_STATUS_MODIFY &&
2330 status != GOT_STATUS_ADD &&
2331 status != GOT_STATUS_DELETE &&
2332 status != GOT_STATUS_CONFLICT)
2333 return NULL;
2336 if (!a->header_shown) {
2337 printf("diff %s %s%s\n", a->id_str,
2338 got_worktree_get_root_path(a->worktree),
2339 a->diff_staged ? " (staged changes)" : "");
2340 a->header_shown = 1;
2343 if (a->diff_staged) {
2344 const char *label1 = NULL, *label2 = NULL;
2345 switch (staged_status) {
2346 case GOT_STATUS_MODIFY:
2347 label1 = path;
2348 label2 = path;
2349 break;
2350 case GOT_STATUS_ADD:
2351 label2 = path;
2352 break;
2353 case GOT_STATUS_DELETE:
2354 label1 = path;
2355 break;
2356 default:
2357 return got_error(GOT_ERR_FILE_STATUS);
2359 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2360 label1, label2, a->diff_context, a->ignore_whitespace,
2361 a->repo, stdout);
2364 if (staged_status == GOT_STATUS_ADD ||
2365 staged_status == GOT_STATUS_MODIFY) {
2366 char *id_str;
2367 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2368 8192);
2369 if (err)
2370 goto done;
2371 err = got_object_id_str(&id_str, staged_blob_id);
2372 if (err)
2373 goto done;
2374 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2375 err = got_error_from_errno("asprintf");
2376 free(id_str);
2377 goto done;
2379 free(id_str);
2380 } else if (status != GOT_STATUS_ADD) {
2381 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2382 if (err)
2383 goto done;
2386 if (status != GOT_STATUS_DELETE) {
2387 if (asprintf(&abspath, "%s/%s",
2388 got_worktree_get_root_path(a->worktree), path) == -1) {
2389 err = got_error_from_errno("asprintf");
2390 goto done;
2393 if (dirfd != -1) {
2394 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2395 if (fd == -1) {
2396 err = got_error_from_errno2("openat", abspath);
2397 goto done;
2399 } else {
2400 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2401 if (fd == -1) {
2402 err = got_error_from_errno2("open", abspath);
2403 goto done;
2406 if (fstat(fd, &sb) == -1) {
2407 err = got_error_from_errno2("fstat", abspath);
2408 goto done;
2410 f2 = fdopen(fd, "r");
2411 if (f2 == NULL) {
2412 err = got_error_from_errno2("fdopen", abspath);
2413 goto done;
2415 fd = -1;
2416 } else
2417 sb.st_size = 0;
2419 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2420 a->diff_context, a->ignore_whitespace, stdout);
2421 done:
2422 if (blob1)
2423 got_object_blob_close(blob1);
2424 if (f2 && fclose(f2) == EOF && err == NULL)
2425 err = got_error_from_errno("fclose");
2426 if (fd != -1 && close(fd) == -1 && err == NULL)
2427 err = got_error_from_errno("close");
2428 free(abspath);
2429 return err;
2432 static const struct got_error *
2433 cmd_diff(int argc, char *argv[])
2435 const struct got_error *error;
2436 struct got_repository *repo = NULL;
2437 struct got_worktree *worktree = NULL;
2438 char *cwd = NULL, *repo_path = NULL;
2439 struct got_object_id *id1 = NULL, *id2 = NULL;
2440 const char *id_str1 = NULL, *id_str2 = NULL;
2441 char *label1 = NULL, *label2 = NULL;
2442 int type1, type2;
2443 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2444 const char *errstr;
2445 char *path = NULL;
2447 #ifndef PROFILE
2448 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2449 NULL) == -1)
2450 err(1, "pledge");
2451 #endif
2453 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2454 switch (ch) {
2455 case 'C':
2456 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2457 &errstr);
2458 if (errstr != NULL)
2459 err(1, "-C option %s", errstr);
2460 break;
2461 case 'r':
2462 repo_path = realpath(optarg, NULL);
2463 if (repo_path == NULL)
2464 return got_error_from_errno2("realpath",
2465 optarg);
2466 got_path_strip_trailing_slashes(repo_path);
2467 break;
2468 case 's':
2469 diff_staged = 1;
2470 break;
2471 case 'w':
2472 ignore_whitespace = 1;
2473 break;
2474 default:
2475 usage_diff();
2476 /* NOTREACHED */
2480 argc -= optind;
2481 argv += optind;
2483 cwd = getcwd(NULL, 0);
2484 if (cwd == NULL) {
2485 error = got_error_from_errno("getcwd");
2486 goto done;
2488 error = got_worktree_open(&worktree, cwd);
2489 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2490 goto done;
2491 if (argc <= 1) {
2492 if (worktree == NULL) {
2493 error = got_error(GOT_ERR_NOT_WORKTREE);
2494 goto done;
2496 if (repo_path)
2497 errx(1,
2498 "-r option can't be used when diffing a work tree");
2499 repo_path = strdup(got_worktree_get_repo_path(worktree));
2500 if (repo_path == NULL) {
2501 error = got_error_from_errno("strdup");
2502 goto done;
2504 if (argc == 1) {
2505 error = got_worktree_resolve_path(&path, worktree,
2506 argv[0]);
2507 if (error)
2508 goto done;
2509 } else {
2510 path = strdup("");
2511 if (path == NULL) {
2512 error = got_error_from_errno("strdup");
2513 goto done;
2516 } else if (argc == 2) {
2517 if (diff_staged)
2518 errx(1, "-s option can't be used when diffing "
2519 "objects in repository");
2520 id_str1 = argv[0];
2521 id_str2 = argv[1];
2522 if (worktree && repo_path == NULL) {
2523 repo_path =
2524 strdup(got_worktree_get_repo_path(worktree));
2525 if (repo_path == NULL) {
2526 error = got_error_from_errno("strdup");
2527 goto done;
2530 } else
2531 usage_diff();
2533 if (repo_path == NULL) {
2534 repo_path = getcwd(NULL, 0);
2535 if (repo_path == NULL)
2536 return got_error_from_errno("getcwd");
2539 error = got_repo_open(&repo, repo_path, NULL);
2540 free(repo_path);
2541 if (error != NULL)
2542 goto done;
2544 error = apply_unveil(got_repo_get_path(repo), 1,
2545 worktree ? got_worktree_get_root_path(worktree) : NULL);
2546 if (error)
2547 goto done;
2549 if (argc <= 1) {
2550 struct print_diff_arg arg;
2551 struct got_pathlist_head paths;
2552 char *id_str;
2554 TAILQ_INIT(&paths);
2556 error = got_object_id_str(&id_str,
2557 got_worktree_get_base_commit_id(worktree));
2558 if (error)
2559 goto done;
2560 arg.repo = repo;
2561 arg.worktree = worktree;
2562 arg.diff_context = diff_context;
2563 arg.id_str = id_str;
2564 arg.header_shown = 0;
2565 arg.diff_staged = diff_staged;
2566 arg.ignore_whitespace = ignore_whitespace;
2568 error = got_pathlist_append(&paths, path, NULL);
2569 if (error)
2570 goto done;
2572 error = got_worktree_status(worktree, &paths, repo, print_diff,
2573 &arg, check_cancelled, NULL);
2574 free(id_str);
2575 got_pathlist_free(&paths);
2576 goto done;
2579 error = got_repo_match_object_id(&id1, &label1, id_str1,
2580 GOT_OBJ_TYPE_ANY, 1, repo);
2581 if (error)
2582 goto done;
2584 error = got_repo_match_object_id(&id2, &label2, id_str2,
2585 GOT_OBJ_TYPE_ANY, 1, repo);
2586 if (error)
2587 goto done;
2589 error = got_object_get_type(&type1, repo, id1);
2590 if (error)
2591 goto done;
2593 error = got_object_get_type(&type2, repo, id2);
2594 if (error)
2595 goto done;
2597 if (type1 != type2) {
2598 error = got_error(GOT_ERR_OBJ_TYPE);
2599 goto done;
2602 switch (type1) {
2603 case GOT_OBJ_TYPE_BLOB:
2604 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2605 diff_context, ignore_whitespace, repo, stdout);
2606 break;
2607 case GOT_OBJ_TYPE_TREE:
2608 error = got_diff_objects_as_trees(id1, id2, "", "",
2609 diff_context, ignore_whitespace, repo, stdout);
2610 break;
2611 case GOT_OBJ_TYPE_COMMIT:
2612 printf("diff %s %s\n", label1, label2);
2613 error = got_diff_objects_as_commits(id1, id2, diff_context,
2614 ignore_whitespace, repo, stdout);
2615 break;
2616 default:
2617 error = got_error(GOT_ERR_OBJ_TYPE);
2619 done:
2620 free(label1);
2621 free(label2);
2622 free(id1);
2623 free(id2);
2624 free(path);
2625 if (worktree)
2626 got_worktree_close(worktree);
2627 if (repo) {
2628 const struct got_error *repo_error;
2629 repo_error = got_repo_close(repo);
2630 if (error == NULL)
2631 error = repo_error;
2633 return error;
2636 __dead static void
2637 usage_blame(void)
2639 fprintf(stderr,
2640 "usage: %s blame [-c commit] [-r repository-path] path\n",
2641 getprogname());
2642 exit(1);
2645 struct blame_line {
2646 int annotated;
2647 char *id_str;
2648 char *committer;
2649 char datebuf[11]; /* YYYY-MM-DD + NUL */
2652 struct blame_cb_args {
2653 struct blame_line *lines;
2654 int nlines;
2655 int nlines_prec;
2656 int lineno_cur;
2657 off_t *line_offsets;
2658 FILE *f;
2659 struct got_repository *repo;
2662 static const struct got_error *
2663 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2665 const struct got_error *err = NULL;
2666 struct blame_cb_args *a = arg;
2667 struct blame_line *bline;
2668 char *line = NULL;
2669 size_t linesize = 0;
2670 struct got_commit_object *commit = NULL;
2671 off_t offset;
2672 struct tm tm;
2673 time_t committer_time;
2675 if (nlines != a->nlines ||
2676 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2677 return got_error(GOT_ERR_RANGE);
2679 if (sigint_received)
2680 return got_error(GOT_ERR_ITER_COMPLETED);
2682 if (lineno == -1)
2683 return NULL; /* no change in this commit */
2685 /* Annotate this line. */
2686 bline = &a->lines[lineno - 1];
2687 if (bline->annotated)
2688 return NULL;
2689 err = got_object_id_str(&bline->id_str, id);
2690 if (err)
2691 return err;
2693 err = got_object_open_as_commit(&commit, a->repo, id);
2694 if (err)
2695 goto done;
2697 bline->committer = strdup(got_object_commit_get_committer(commit));
2698 if (bline->committer == NULL) {
2699 err = got_error_from_errno("strdup");
2700 goto done;
2703 committer_time = got_object_commit_get_committer_time(commit);
2704 if (localtime_r(&committer_time, &tm) == NULL)
2705 return got_error_from_errno("localtime_r");
2706 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2707 &tm) >= sizeof(bline->datebuf)) {
2708 err = got_error(GOT_ERR_NO_SPACE);
2709 goto done;
2711 bline->annotated = 1;
2713 /* Print lines annotated so far. */
2714 bline = &a->lines[a->lineno_cur - 1];
2715 if (!bline->annotated)
2716 goto done;
2718 offset = a->line_offsets[a->lineno_cur - 1];
2719 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2720 err = got_error_from_errno("fseeko");
2721 goto done;
2724 while (bline->annotated) {
2725 char *smallerthan, *at, *nl, *committer;
2726 size_t len;
2728 if (getline(&line, &linesize, a->f) == -1) {
2729 if (ferror(a->f))
2730 err = got_error_from_errno("getline");
2731 break;
2734 committer = bline->committer;
2735 smallerthan = strchr(committer, '<');
2736 if (smallerthan && smallerthan[1] != '\0')
2737 committer = smallerthan + 1;
2738 at = strchr(committer, '@');
2739 if (at)
2740 *at = '\0';
2741 len = strlen(committer);
2742 if (len >= 9)
2743 committer[8] = '\0';
2745 nl = strchr(line, '\n');
2746 if (nl)
2747 *nl = '\0';
2748 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2749 bline->id_str, bline->datebuf, committer, line);
2751 a->lineno_cur++;
2752 bline = &a->lines[a->lineno_cur - 1];
2754 done:
2755 if (commit)
2756 got_object_commit_close(commit);
2757 free(line);
2758 return err;
2761 static const struct got_error *
2762 cmd_blame(int argc, char *argv[])
2764 const struct got_error *error;
2765 struct got_repository *repo = NULL;
2766 struct got_worktree *worktree = NULL;
2767 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2768 struct got_object_id *obj_id = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 struct got_blob_object *blob = NULL;
2771 char *commit_id_str = NULL;
2772 struct blame_cb_args bca;
2773 int ch, obj_type, i;
2774 size_t filesize;
2776 memset(&bca, 0, sizeof(bca));
2778 #ifndef PROFILE
2779 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2780 NULL) == -1)
2781 err(1, "pledge");
2782 #endif
2784 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2785 switch (ch) {
2786 case 'c':
2787 commit_id_str = optarg;
2788 break;
2789 case 'r':
2790 repo_path = realpath(optarg, NULL);
2791 if (repo_path == NULL)
2792 return got_error_from_errno2("realpath",
2793 optarg);
2794 got_path_strip_trailing_slashes(repo_path);
2795 break;
2796 default:
2797 usage_blame();
2798 /* NOTREACHED */
2802 argc -= optind;
2803 argv += optind;
2805 if (argc == 1)
2806 path = argv[0];
2807 else
2808 usage_blame();
2810 cwd = getcwd(NULL, 0);
2811 if (cwd == NULL) {
2812 error = got_error_from_errno("getcwd");
2813 goto done;
2815 if (repo_path == NULL) {
2816 error = got_worktree_open(&worktree, cwd);
2817 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2818 goto done;
2819 else
2820 error = NULL;
2821 if (worktree) {
2822 repo_path =
2823 strdup(got_worktree_get_repo_path(worktree));
2824 if (repo_path == NULL)
2825 error = got_error_from_errno("strdup");
2826 if (error)
2827 goto done;
2828 } else {
2829 repo_path = strdup(cwd);
2830 if (repo_path == NULL) {
2831 error = got_error_from_errno("strdup");
2832 goto done;
2837 error = got_repo_open(&repo, repo_path, NULL);
2838 if (error != NULL)
2839 goto done;
2841 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2842 if (error)
2843 goto done;
2845 if (worktree) {
2846 const char *prefix = got_worktree_get_path_prefix(worktree);
2847 char *p, *worktree_subdir = cwd +
2848 strlen(got_worktree_get_root_path(worktree));
2849 if (asprintf(&p, "%s%s%s%s%s",
2850 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2851 worktree_subdir, worktree_subdir[0] ? "/" : "",
2852 path) == -1) {
2853 error = got_error_from_errno("asprintf");
2854 goto done;
2856 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2857 free(p);
2858 } else {
2859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2861 if (error)
2862 goto done;
2864 if (commit_id_str == NULL) {
2865 struct got_reference *head_ref;
2866 error = got_ref_open(&head_ref, repo, worktree ?
2867 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2868 if (error != NULL)
2869 goto done;
2870 error = got_ref_resolve(&commit_id, repo, head_ref);
2871 got_ref_close(head_ref);
2872 if (error != NULL)
2873 goto done;
2874 } else {
2875 error = got_repo_match_object_id(&commit_id, NULL,
2876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2877 if (error)
2878 goto done;
2881 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2882 if (error)
2883 goto done;
2885 error = got_object_get_type(&obj_type, repo, obj_id);
2886 if (error)
2887 goto done;
2889 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2890 error = got_error(GOT_ERR_OBJ_TYPE);
2891 goto done;
2894 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2895 if (error)
2896 goto done;
2897 bca.f = got_opentemp();
2898 if (bca.f == NULL) {
2899 error = got_error_from_errno("got_opentemp");
2900 goto done;
2902 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2903 &bca.line_offsets, bca.f, blob);
2904 if (error || bca.nlines == 0)
2905 goto done;
2907 /* Don't include \n at EOF in the blame line count. */
2908 if (bca.line_offsets[bca.nlines - 1] == filesize)
2909 bca.nlines--;
2911 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2912 if (bca.lines == NULL) {
2913 error = got_error_from_errno("calloc");
2914 goto done;
2916 bca.lineno_cur = 1;
2917 bca.nlines_prec = 0;
2918 i = bca.nlines;
2919 while (i > 0) {
2920 i /= 10;
2921 bca.nlines_prec++;
2923 bca.repo = repo;
2925 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2926 check_cancelled, NULL);
2927 done:
2928 free(in_repo_path);
2929 free(repo_path);
2930 free(cwd);
2931 free(commit_id);
2932 free(obj_id);
2933 if (blob)
2934 got_object_blob_close(blob);
2935 if (worktree)
2936 got_worktree_close(worktree);
2937 if (repo) {
2938 const struct got_error *repo_error;
2939 repo_error = got_repo_close(repo);
2940 if (error == NULL)
2941 error = repo_error;
2943 if (bca.lines) {
2944 for (i = 0; i < bca.nlines; i++) {
2945 struct blame_line *bline = &bca.lines[i];
2946 free(bline->id_str);
2947 free(bline->committer);
2949 free(bca.lines);
2951 free(bca.line_offsets);
2952 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2953 error = got_error_from_errno("fclose");
2954 return error;
2957 __dead static void
2958 usage_tree(void)
2960 fprintf(stderr,
2961 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2962 getprogname());
2963 exit(1);
2966 static void
2967 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2968 const char *root_path)
2970 int is_root_path = (strcmp(path, root_path) == 0);
2971 const char *modestr = "";
2972 mode_t mode = got_tree_entry_get_mode(te);
2974 path += strlen(root_path);
2975 while (path[0] == '/')
2976 path++;
2978 if (got_object_tree_entry_is_submodule(te))
2979 modestr = "$";
2980 else if (S_ISLNK(mode))
2981 modestr = "@";
2982 else if (S_ISDIR(mode))
2983 modestr = "/";
2984 else if (mode & S_IXUSR)
2985 modestr = "*";
2987 printf("%s%s%s%s%s\n", id ? id : "", path,
2988 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2991 static const struct got_error *
2992 print_tree(const char *path, struct got_object_id *commit_id,
2993 int show_ids, int recurse, const char *root_path,
2994 struct got_repository *repo)
2996 const struct got_error *err = NULL;
2997 struct got_object_id *tree_id = NULL;
2998 struct got_tree_object *tree = NULL;
2999 int nentries, i;
3001 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3002 if (err)
3003 goto done;
3005 err = got_object_open_as_tree(&tree, repo, tree_id);
3006 if (err)
3007 goto done;
3008 nentries = got_object_tree_get_nentries(tree);
3009 for (i = 0; i < nentries; i++) {
3010 struct got_tree_entry *te;
3011 char *id = NULL;
3013 if (sigint_received || sigpipe_received)
3014 break;
3016 te = got_object_tree_get_entry(tree, i);
3017 if (show_ids) {
3018 char *id_str;
3019 err = got_object_id_str(&id_str,
3020 got_tree_entry_get_id(te));
3021 if (err)
3022 goto done;
3023 if (asprintf(&id, "%s ", id_str) == -1) {
3024 err = got_error_from_errno("asprintf");
3025 free(id_str);
3026 goto done;
3028 free(id_str);
3030 print_entry(te, id, path, root_path);
3031 free(id);
3033 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3034 char *child_path;
3035 if (asprintf(&child_path, "%s%s%s", path,
3036 path[0] == '/' && path[1] == '\0' ? "" : "/",
3037 got_tree_entry_get_name(te)) == -1) {
3038 err = got_error_from_errno("asprintf");
3039 goto done;
3041 err = print_tree(child_path, commit_id, show_ids, 1,
3042 root_path, repo);
3043 free(child_path);
3044 if (err)
3045 goto done;
3048 done:
3049 if (tree)
3050 got_object_tree_close(tree);
3051 free(tree_id);
3052 return err;
3055 static const struct got_error *
3056 cmd_tree(int argc, char *argv[])
3058 const struct got_error *error;
3059 struct got_repository *repo = NULL;
3060 struct got_worktree *worktree = NULL;
3061 const char *path;
3062 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3063 struct got_object_id *commit_id = NULL;
3064 char *commit_id_str = NULL;
3065 int show_ids = 0, recurse = 0;
3066 int ch;
3068 #ifndef PROFILE
3069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3070 NULL) == -1)
3071 err(1, "pledge");
3072 #endif
3074 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3075 switch (ch) {
3076 case 'c':
3077 commit_id_str = optarg;
3078 break;
3079 case 'r':
3080 repo_path = realpath(optarg, NULL);
3081 if (repo_path == NULL)
3082 return got_error_from_errno2("realpath",
3083 optarg);
3084 got_path_strip_trailing_slashes(repo_path);
3085 break;
3086 case 'i':
3087 show_ids = 1;
3088 break;
3089 case 'R':
3090 recurse = 1;
3091 break;
3092 default:
3093 usage_tree();
3094 /* NOTREACHED */
3098 argc -= optind;
3099 argv += optind;
3101 if (argc == 1)
3102 path = argv[0];
3103 else if (argc > 1)
3104 usage_tree();
3105 else
3106 path = NULL;
3108 cwd = getcwd(NULL, 0);
3109 if (cwd == NULL) {
3110 error = got_error_from_errno("getcwd");
3111 goto done;
3113 if (repo_path == NULL) {
3114 error = got_worktree_open(&worktree, cwd);
3115 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3116 goto done;
3117 else
3118 error = NULL;
3119 if (worktree) {
3120 repo_path =
3121 strdup(got_worktree_get_repo_path(worktree));
3122 if (repo_path == NULL)
3123 error = got_error_from_errno("strdup");
3124 if (error)
3125 goto done;
3126 } else {
3127 repo_path = strdup(cwd);
3128 if (repo_path == NULL) {
3129 error = got_error_from_errno("strdup");
3130 goto done;
3135 error = got_repo_open(&repo, repo_path, NULL);
3136 if (error != NULL)
3137 goto done;
3139 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3140 if (error)
3141 goto done;
3143 if (path == NULL) {
3144 if (worktree) {
3145 char *p, *worktree_subdir = cwd +
3146 strlen(got_worktree_get_root_path(worktree));
3147 if (asprintf(&p, "%s/%s",
3148 got_worktree_get_path_prefix(worktree),
3149 worktree_subdir) == -1) {
3150 error = got_error_from_errno("asprintf");
3151 goto done;
3153 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3154 free(p);
3155 if (error)
3156 goto done;
3157 } else
3158 path = "/";
3160 if (in_repo_path == NULL) {
3161 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3162 if (error != NULL)
3163 goto done;
3166 if (commit_id_str == NULL) {
3167 struct got_reference *head_ref;
3168 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3169 if (error != NULL)
3170 goto done;
3171 error = got_ref_resolve(&commit_id, repo, head_ref);
3172 got_ref_close(head_ref);
3173 if (error != NULL)
3174 goto done;
3175 } else {
3176 error = got_repo_match_object_id(&commit_id, NULL,
3177 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3178 if (error)
3179 goto done;
3182 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3183 in_repo_path, repo);
3184 done:
3185 free(in_repo_path);
3186 free(repo_path);
3187 free(cwd);
3188 free(commit_id);
3189 if (worktree)
3190 got_worktree_close(worktree);
3191 if (repo) {
3192 const struct got_error *repo_error;
3193 repo_error = got_repo_close(repo);
3194 if (error == NULL)
3195 error = repo_error;
3197 return error;
3200 __dead static void
3201 usage_status(void)
3203 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3204 exit(1);
3207 static const struct got_error *
3208 print_status(void *arg, unsigned char status, unsigned char staged_status,
3209 const char *path, struct got_object_id *blob_id,
3210 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3211 int dirfd, const char *de_name)
3213 if (status == staged_status && (status == GOT_STATUS_DELETE))
3214 status = GOT_STATUS_NO_CHANGE;
3215 printf("%c%c %s\n", status, staged_status, path);
3216 return NULL;
3219 static const struct got_error *
3220 cmd_status(int argc, char *argv[])
3222 const struct got_error *error = NULL;
3223 struct got_repository *repo = NULL;
3224 struct got_worktree *worktree = NULL;
3225 char *cwd = NULL;
3226 struct got_pathlist_head paths;
3227 struct got_pathlist_entry *pe;
3228 int ch;
3230 TAILQ_INIT(&paths);
3232 while ((ch = getopt(argc, argv, "")) != -1) {
3233 switch (ch) {
3234 default:
3235 usage_status();
3236 /* NOTREACHED */
3240 argc -= optind;
3241 argv += optind;
3243 #ifndef PROFILE
3244 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3245 NULL) == -1)
3246 err(1, "pledge");
3247 #endif
3248 cwd = getcwd(NULL, 0);
3249 if (cwd == NULL) {
3250 error = got_error_from_errno("getcwd");
3251 goto done;
3254 error = got_worktree_open(&worktree, cwd);
3255 if (error != NULL)
3256 goto done;
3258 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3259 NULL);
3260 if (error != NULL)
3261 goto done;
3263 error = apply_unveil(got_repo_get_path(repo), 1,
3264 got_worktree_get_root_path(worktree));
3265 if (error)
3266 goto done;
3268 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3269 if (error)
3270 goto done;
3272 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3273 check_cancelled, NULL);
3274 done:
3275 TAILQ_FOREACH(pe, &paths, entry)
3276 free((char *)pe->path);
3277 got_pathlist_free(&paths);
3278 free(cwd);
3279 return error;
3282 __dead static void
3283 usage_ref(void)
3285 fprintf(stderr,
3286 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3287 getprogname());
3288 exit(1);
3291 static const struct got_error *
3292 list_refs(struct got_repository *repo)
3294 static const struct got_error *err = NULL;
3295 struct got_reflist_head refs;
3296 struct got_reflist_entry *re;
3298 SIMPLEQ_INIT(&refs);
3299 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3300 if (err)
3301 return err;
3303 SIMPLEQ_FOREACH(re, &refs, entry) {
3304 char *refstr;
3305 refstr = got_ref_to_str(re->ref);
3306 if (refstr == NULL)
3307 return got_error_from_errno("got_ref_to_str");
3308 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3309 free(refstr);
3312 got_ref_list_free(&refs);
3313 return NULL;
3316 static const struct got_error *
3317 delete_ref(struct got_repository *repo, const char *refname)
3319 const struct got_error *err = NULL;
3320 struct got_reference *ref;
3322 err = got_ref_open(&ref, repo, refname, 0);
3323 if (err)
3324 return err;
3326 err = got_ref_delete(ref, repo);
3327 got_ref_close(ref);
3328 return err;
3331 static const struct got_error *
3332 add_ref(struct got_repository *repo, const char *refname, const char *target)
3334 const struct got_error *err = NULL;
3335 struct got_object_id *id;
3336 struct got_reference *ref = NULL;
3339 * Don't let the user create a reference name with a leading '-'.
3340 * While technically a valid reference name, this case is usually
3341 * an unintended typo.
3343 if (refname[0] == '-')
3344 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3346 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3347 repo);
3348 if (err) {
3349 struct got_reference *target_ref;
3351 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3352 return err;
3353 err = got_ref_open(&target_ref, repo, target, 0);
3354 if (err)
3355 return err;
3356 err = got_ref_resolve(&id, repo, target_ref);
3357 got_ref_close(target_ref);
3358 if (err)
3359 return err;
3362 err = got_ref_alloc(&ref, refname, id);
3363 if (err)
3364 goto done;
3366 err = got_ref_write(ref, repo);
3367 done:
3368 if (ref)
3369 got_ref_close(ref);
3370 free(id);
3371 return err;
3374 static const struct got_error *
3375 add_symref(struct got_repository *repo, const char *refname, const char *target)
3377 const struct got_error *err = NULL;
3378 struct got_reference *ref = NULL;
3379 struct got_reference *target_ref = NULL;
3382 * Don't let the user create a reference name with a leading '-'.
3383 * While technically a valid reference name, this case is usually
3384 * an unintended typo.
3386 if (refname[0] == '-')
3387 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3389 err = got_ref_open(&target_ref, repo, target, 0);
3390 if (err)
3391 return err;
3393 err = got_ref_alloc_symref(&ref, refname, target_ref);
3394 if (err)
3395 goto done;
3397 err = got_ref_write(ref, repo);
3398 done:
3399 if (target_ref)
3400 got_ref_close(target_ref);
3401 if (ref)
3402 got_ref_close(ref);
3403 return err;
3406 static const struct got_error *
3407 cmd_ref(int argc, char *argv[])
3409 const struct got_error *error = NULL;
3410 struct got_repository *repo = NULL;
3411 struct got_worktree *worktree = NULL;
3412 char *cwd = NULL, *repo_path = NULL;
3413 int ch, do_list = 0, create_symref = 0;
3414 const char *delref = NULL;
3416 /* TODO: Add -s option for adding symbolic references. */
3417 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3418 switch (ch) {
3419 case 'd':
3420 delref = optarg;
3421 break;
3422 case 'r':
3423 repo_path = realpath(optarg, NULL);
3424 if (repo_path == NULL)
3425 return got_error_from_errno2("realpath",
3426 optarg);
3427 got_path_strip_trailing_slashes(repo_path);
3428 break;
3429 case 'l':
3430 do_list = 1;
3431 break;
3432 case 's':
3433 create_symref = 1;
3434 break;
3435 default:
3436 usage_ref();
3437 /* NOTREACHED */
3441 if (do_list && delref)
3442 errx(1, "-l and -d options are mutually exclusive\n");
3444 argc -= optind;
3445 argv += optind;
3447 if (do_list || delref) {
3448 if (create_symref)
3449 errx(1, "-s option cannot be used together with the "
3450 "-l or -d options");
3451 if (argc > 0)
3452 usage_ref();
3453 } else if (argc != 2)
3454 usage_ref();
3456 #ifndef PROFILE
3457 if (do_list) {
3458 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3459 NULL) == -1)
3460 err(1, "pledge");
3461 } else {
3462 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3463 "sendfd unveil", NULL) == -1)
3464 err(1, "pledge");
3466 #endif
3467 cwd = getcwd(NULL, 0);
3468 if (cwd == NULL) {
3469 error = got_error_from_errno("getcwd");
3470 goto done;
3473 if (repo_path == NULL) {
3474 error = got_worktree_open(&worktree, cwd);
3475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3476 goto done;
3477 else
3478 error = NULL;
3479 if (worktree) {
3480 repo_path =
3481 strdup(got_worktree_get_repo_path(worktree));
3482 if (repo_path == NULL)
3483 error = got_error_from_errno("strdup");
3484 if (error)
3485 goto done;
3486 } else {
3487 repo_path = strdup(cwd);
3488 if (repo_path == NULL) {
3489 error = got_error_from_errno("strdup");
3490 goto done;
3495 error = got_repo_open(&repo, repo_path, NULL);
3496 if (error != NULL)
3497 goto done;
3499 error = apply_unveil(got_repo_get_path(repo), do_list,
3500 worktree ? got_worktree_get_root_path(worktree) : NULL);
3501 if (error)
3502 goto done;
3504 if (do_list)
3505 error = list_refs(repo);
3506 else if (delref)
3507 error = delete_ref(repo, delref);
3508 else if (create_symref)
3509 error = add_symref(repo, argv[0], argv[1]);
3510 else
3511 error = add_ref(repo, argv[0], argv[1]);
3512 done:
3513 if (repo)
3514 got_repo_close(repo);
3515 if (worktree)
3516 got_worktree_close(worktree);
3517 free(cwd);
3518 free(repo_path);
3519 return error;
3522 __dead static void
3523 usage_branch(void)
3525 fprintf(stderr,
3526 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3527 "[name]\n", getprogname());
3528 exit(1);
3531 static const struct got_error *
3532 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3533 struct got_reference *ref)
3535 const struct got_error *err = NULL;
3536 const char *refname, *marker = " ";
3537 char *refstr;
3539 refname = got_ref_get_name(ref);
3540 if (worktree && strcmp(refname,
3541 got_worktree_get_head_ref_name(worktree)) == 0) {
3542 struct got_object_id *id = NULL;
3544 err = got_ref_resolve(&id, repo, ref);
3545 if (err)
3546 return err;
3547 if (got_object_id_cmp(id,
3548 got_worktree_get_base_commit_id(worktree)) == 0)
3549 marker = "* ";
3550 else
3551 marker = "~ ";
3552 free(id);
3555 if (strncmp(refname, "refs/heads/", 11) == 0)
3556 refname += 11;
3557 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3558 refname += 18;
3560 refstr = got_ref_to_str(ref);
3561 if (refstr == NULL)
3562 return got_error_from_errno("got_ref_to_str");
3564 printf("%s%s: %s\n", marker, refname, refstr);
3565 free(refstr);
3566 return NULL;
3569 static const struct got_error *
3570 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3572 const char *refname;
3574 if (worktree == NULL)
3575 return got_error(GOT_ERR_NOT_WORKTREE);
3577 refname = got_worktree_get_head_ref_name(worktree);
3579 if (strncmp(refname, "refs/heads/", 11) == 0)
3580 refname += 11;
3581 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3582 refname += 18;
3584 printf("%s\n", refname);
3586 return NULL;
3589 static const struct got_error *
3590 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3592 static const struct got_error *err = NULL;
3593 struct got_reflist_head refs;
3594 struct got_reflist_entry *re;
3595 struct got_reference *temp_ref = NULL;
3596 int rebase_in_progress, histedit_in_progress;
3598 SIMPLEQ_INIT(&refs);
3600 if (worktree) {
3601 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3602 worktree);
3603 if (err)
3604 return err;
3606 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3607 worktree);
3608 if (err)
3609 return err;
3611 if (rebase_in_progress || histedit_in_progress) {
3612 err = got_ref_open(&temp_ref, repo,
3613 got_worktree_get_head_ref_name(worktree), 0);
3614 if (err)
3615 return err;
3616 list_branch(repo, worktree, temp_ref);
3617 got_ref_close(temp_ref);
3621 err = got_ref_list(&refs, repo, "refs/heads",
3622 got_ref_cmp_by_name, NULL);
3623 if (err)
3624 return err;
3626 SIMPLEQ_FOREACH(re, &refs, entry)
3627 list_branch(repo, worktree, re->ref);
3629 got_ref_list_free(&refs);
3630 return NULL;
3633 static const struct got_error *
3634 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3635 const char *branch_name)
3637 const struct got_error *err = NULL;
3638 struct got_reference *ref = NULL;
3639 char *refname;
3641 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3642 return got_error_from_errno("asprintf");
3644 err = got_ref_open(&ref, repo, refname, 0);
3645 if (err)
3646 goto done;
3648 if (worktree &&
3649 strcmp(got_worktree_get_head_ref_name(worktree),
3650 got_ref_get_name(ref)) == 0) {
3651 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3652 "will not delete this work tree's current branch");
3653 goto done;
3656 err = got_ref_delete(ref, repo);
3657 done:
3658 if (ref)
3659 got_ref_close(ref);
3660 free(refname);
3661 return err;
3664 static const struct got_error *
3665 add_branch(struct got_repository *repo, const char *branch_name,
3666 struct got_object_id *base_commit_id)
3668 const struct got_error *err = NULL;
3669 struct got_reference *ref = NULL;
3670 char *base_refname = NULL, *refname = NULL;
3673 * Don't let the user create a branch name with a leading '-'.
3674 * While technically a valid reference name, this case is usually
3675 * an unintended typo.
3677 if (branch_name[0] == '-')
3678 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3680 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3681 err = got_error_from_errno("asprintf");
3682 goto done;
3685 err = got_ref_open(&ref, repo, refname, 0);
3686 if (err == NULL) {
3687 err = got_error(GOT_ERR_BRANCH_EXISTS);
3688 goto done;
3689 } else if (err->code != GOT_ERR_NOT_REF)
3690 goto done;
3692 err = got_ref_alloc(&ref, refname, base_commit_id);
3693 if (err)
3694 goto done;
3696 err = got_ref_write(ref, repo);
3697 done:
3698 if (ref)
3699 got_ref_close(ref);
3700 free(base_refname);
3701 free(refname);
3702 return err;
3705 static const struct got_error *
3706 cmd_branch(int argc, char *argv[])
3708 const struct got_error *error = NULL;
3709 struct got_repository *repo = NULL;
3710 struct got_worktree *worktree = NULL;
3711 char *cwd = NULL, *repo_path = NULL;
3712 int ch, do_list = 0, do_show = 0, do_update = 1;
3713 const char *delref = NULL, *commit_id_arg = NULL;
3714 struct got_reference *ref = NULL;
3715 struct got_pathlist_head paths;
3716 struct got_pathlist_entry *pe;
3717 struct got_object_id *commit_id = NULL;
3718 char *commit_id_str = NULL;
3720 TAILQ_INIT(&paths);
3722 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3723 switch (ch) {
3724 case 'c':
3725 commit_id_arg = optarg;
3726 break;
3727 case 'd':
3728 delref = optarg;
3729 break;
3730 case 'r':
3731 repo_path = realpath(optarg, NULL);
3732 if (repo_path == NULL)
3733 return got_error_from_errno2("realpath",
3734 optarg);
3735 got_path_strip_trailing_slashes(repo_path);
3736 break;
3737 case 'l':
3738 do_list = 1;
3739 break;
3740 case 'n':
3741 do_update = 0;
3742 break;
3743 default:
3744 usage_branch();
3745 /* NOTREACHED */
3749 if (do_list && delref)
3750 errx(1, "-l and -d options are mutually exclusive\n");
3752 argc -= optind;
3753 argv += optind;
3755 if (!do_list && !delref && argc == 0)
3756 do_show = 1;
3758 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3759 errx(1, "-c option can only be used when creating a branch");
3761 if (do_list || delref) {
3762 if (argc > 0)
3763 usage_branch();
3764 } else if (!do_show && argc != 1)
3765 usage_branch();
3767 #ifndef PROFILE
3768 if (do_list || do_show) {
3769 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3770 NULL) == -1)
3771 err(1, "pledge");
3772 } else {
3773 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3774 "sendfd unveil", NULL) == -1)
3775 err(1, "pledge");
3777 #endif
3778 cwd = getcwd(NULL, 0);
3779 if (cwd == NULL) {
3780 error = got_error_from_errno("getcwd");
3781 goto done;
3784 if (repo_path == NULL) {
3785 error = got_worktree_open(&worktree, cwd);
3786 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3787 goto done;
3788 else
3789 error = NULL;
3790 if (worktree) {
3791 repo_path =
3792 strdup(got_worktree_get_repo_path(worktree));
3793 if (repo_path == NULL)
3794 error = got_error_from_errno("strdup");
3795 if (error)
3796 goto done;
3797 } else {
3798 repo_path = strdup(cwd);
3799 if (repo_path == NULL) {
3800 error = got_error_from_errno("strdup");
3801 goto done;
3806 error = got_repo_open(&repo, repo_path, NULL);
3807 if (error != NULL)
3808 goto done;
3810 error = apply_unveil(got_repo_get_path(repo), do_list,
3811 worktree ? got_worktree_get_root_path(worktree) : NULL);
3812 if (error)
3813 goto done;
3815 if (do_show)
3816 error = show_current_branch(repo, worktree);
3817 else if (do_list)
3818 error = list_branches(repo, worktree);
3819 else if (delref)
3820 error = delete_branch(repo, worktree, delref);
3821 else {
3822 if (commit_id_arg == NULL)
3823 commit_id_arg = worktree ?
3824 got_worktree_get_head_ref_name(worktree) :
3825 GOT_REF_HEAD;
3826 error = got_repo_match_object_id(&commit_id, NULL,
3827 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3828 if (error)
3829 goto done;
3830 error = add_branch(repo, argv[0], commit_id);
3831 if (error)
3832 goto done;
3833 if (worktree && do_update) {
3834 int did_something = 0;
3835 char *branch_refname = NULL;
3837 error = got_object_id_str(&commit_id_str, commit_id);
3838 if (error)
3839 goto done;
3840 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3841 worktree);
3842 if (error)
3843 goto done;
3844 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3845 == -1) {
3846 error = got_error_from_errno("asprintf");
3847 goto done;
3849 error = got_ref_open(&ref, repo, branch_refname, 0);
3850 free(branch_refname);
3851 if (error)
3852 goto done;
3853 error = switch_head_ref(ref, commit_id, worktree,
3854 repo);
3855 if (error)
3856 goto done;
3857 error = got_worktree_set_base_commit_id(worktree, repo,
3858 commit_id);
3859 if (error)
3860 goto done;
3861 error = got_worktree_checkout_files(worktree, &paths,
3862 repo, update_progress, &did_something,
3863 check_cancelled, NULL);
3864 if (error)
3865 goto done;
3866 if (did_something)
3867 printf("Updated to commit %s\n", commit_id_str);
3870 done:
3871 if (ref)
3872 got_ref_close(ref);
3873 if (repo)
3874 got_repo_close(repo);
3875 if (worktree)
3876 got_worktree_close(worktree);
3877 free(cwd);
3878 free(repo_path);
3879 free(commit_id);
3880 free(commit_id_str);
3881 TAILQ_FOREACH(pe, &paths, entry)
3882 free((char *)pe->path);
3883 got_pathlist_free(&paths);
3884 return error;
3888 __dead static void
3889 usage_tag(void)
3891 fprintf(stderr,
3892 "usage: %s tag [-c commit] [-r repository] [-l] "
3893 "[-m message] name\n", getprogname());
3894 exit(1);
3897 #if 0
3898 static const struct got_error *
3899 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3901 const struct got_error *err = NULL;
3902 struct got_reflist_entry *re, *se, *new;
3903 struct got_object_id *re_id, *se_id;
3904 struct got_tag_object *re_tag, *se_tag;
3905 time_t re_time, se_time;
3907 SIMPLEQ_FOREACH(re, tags, entry) {
3908 se = SIMPLEQ_FIRST(sorted);
3909 if (se == NULL) {
3910 err = got_reflist_entry_dup(&new, re);
3911 if (err)
3912 return err;
3913 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3914 continue;
3915 } else {
3916 err = got_ref_resolve(&re_id, repo, re->ref);
3917 if (err)
3918 break;
3919 err = got_object_open_as_tag(&re_tag, repo, re_id);
3920 free(re_id);
3921 if (err)
3922 break;
3923 re_time = got_object_tag_get_tagger_time(re_tag);
3924 got_object_tag_close(re_tag);
3927 while (se) {
3928 err = got_ref_resolve(&se_id, repo, re->ref);
3929 if (err)
3930 break;
3931 err = got_object_open_as_tag(&se_tag, repo, se_id);
3932 free(se_id);
3933 if (err)
3934 break;
3935 se_time = got_object_tag_get_tagger_time(se_tag);
3936 got_object_tag_close(se_tag);
3938 if (se_time > re_time) {
3939 err = got_reflist_entry_dup(&new, re);
3940 if (err)
3941 return err;
3942 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3943 break;
3945 se = SIMPLEQ_NEXT(se, entry);
3946 continue;
3949 done:
3950 return err;
3952 #endif
3954 static const struct got_error *
3955 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3957 static const struct got_error *err = NULL;
3958 struct got_reflist_head refs;
3959 struct got_reflist_entry *re;
3961 SIMPLEQ_INIT(&refs);
3963 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3964 if (err)
3965 return err;
3967 SIMPLEQ_FOREACH(re, &refs, entry) {
3968 const char *refname;
3969 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3970 char datebuf[26];
3971 const char *tagger;
3972 time_t tagger_time;
3973 struct got_object_id *id;
3974 struct got_tag_object *tag;
3975 struct got_commit_object *commit = NULL;
3977 refname = got_ref_get_name(re->ref);
3978 if (strncmp(refname, "refs/tags/", 10) != 0)
3979 continue;
3980 refname += 10;
3981 refstr = got_ref_to_str(re->ref);
3982 if (refstr == NULL) {
3983 err = got_error_from_errno("got_ref_to_str");
3984 break;
3986 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3987 free(refstr);
3989 err = got_ref_resolve(&id, repo, re->ref);
3990 if (err)
3991 break;
3992 err = got_object_open_as_tag(&tag, repo, id);
3993 if (err) {
3994 if (err->code != GOT_ERR_OBJ_TYPE) {
3995 free(id);
3996 break;
3998 /* "lightweight" tag */
3999 err = got_object_open_as_commit(&commit, repo, id);
4000 if (err) {
4001 free(id);
4002 break;
4004 tagger = got_object_commit_get_committer(commit);
4005 tagger_time =
4006 got_object_commit_get_committer_time(commit);
4007 err = got_object_id_str(&id_str, id);
4008 free(id);
4009 if (err)
4010 break;
4011 } else {
4012 free(id);
4013 tagger = got_object_tag_get_tagger(tag);
4014 tagger_time = got_object_tag_get_tagger_time(tag);
4015 err = got_object_id_str(&id_str,
4016 got_object_tag_get_object_id(tag));
4017 if (err)
4018 break;
4020 printf("from: %s\n", tagger);
4021 datestr = get_datestr(&tagger_time, datebuf);
4022 if (datestr)
4023 printf("date: %s UTC\n", datestr);
4024 if (commit)
4025 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4026 else {
4027 switch (got_object_tag_get_object_type(tag)) {
4028 case GOT_OBJ_TYPE_BLOB:
4029 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4030 id_str);
4031 break;
4032 case GOT_OBJ_TYPE_TREE:
4033 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4034 id_str);
4035 break;
4036 case GOT_OBJ_TYPE_COMMIT:
4037 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4038 id_str);
4039 break;
4040 case GOT_OBJ_TYPE_TAG:
4041 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4042 id_str);
4043 break;
4044 default:
4045 break;
4048 free(id_str);
4049 if (commit) {
4050 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4051 if (err)
4052 break;
4053 got_object_commit_close(commit);
4054 } else {
4055 tagmsg0 = strdup(got_object_tag_get_message(tag));
4056 got_object_tag_close(tag);
4057 if (tagmsg0 == NULL) {
4058 err = got_error_from_errno("strdup");
4059 break;
4063 tagmsg = tagmsg0;
4064 do {
4065 line = strsep(&tagmsg, "\n");
4066 if (line)
4067 printf(" %s\n", line);
4068 } while (line);
4069 free(tagmsg0);
4072 got_ref_list_free(&refs);
4073 return NULL;
4076 static const struct got_error *
4077 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4078 const char *tag_name, const char *repo_path)
4080 const struct got_error *err = NULL;
4081 char *template = NULL, *initial_content = NULL;
4082 char *editor = NULL;
4083 int fd = -1;
4085 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4086 err = got_error_from_errno("asprintf");
4087 goto done;
4090 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4091 commit_id_str, tag_name) == -1) {
4092 err = got_error_from_errno("asprintf");
4093 goto done;
4096 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4097 if (err)
4098 goto done;
4100 dprintf(fd, initial_content);
4101 close(fd);
4103 err = get_editor(&editor);
4104 if (err)
4105 goto done;
4106 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4107 done:
4108 free(initial_content);
4109 free(template);
4110 free(editor);
4112 /* Editor is done; we can now apply unveil(2) */
4113 if (err == NULL) {
4114 err = apply_unveil(repo_path, 0, NULL);
4115 if (err) {
4116 free(*tagmsg);
4117 *tagmsg = NULL;
4120 return err;
4123 static const struct got_error *
4124 add_tag(struct got_repository *repo, const char *tag_name,
4125 const char *commit_arg, const char *tagmsg_arg)
4127 const struct got_error *err = NULL;
4128 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4129 char *label = NULL, *commit_id_str = NULL;
4130 struct got_reference *ref = NULL;
4131 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4132 char *tagmsg_path = NULL, *tag_id_str = NULL;
4133 int preserve_tagmsg = 0;
4136 * Don't let the user create a tag name with a leading '-'.
4137 * While technically a valid reference name, this case is usually
4138 * an unintended typo.
4140 if (tag_name[0] == '-')
4141 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4143 err = get_author(&tagger, repo);
4144 if (err)
4145 return err;
4147 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4148 GOT_OBJ_TYPE_COMMIT, 1, repo);
4149 if (err)
4150 goto done;
4152 err = got_object_id_str(&commit_id_str, commit_id);
4153 if (err)
4154 goto done;
4156 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4157 refname = strdup(tag_name);
4158 if (refname == NULL) {
4159 err = got_error_from_errno("strdup");
4160 goto done;
4162 tag_name += 10;
4163 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4164 err = got_error_from_errno("asprintf");
4165 goto done;
4168 err = got_ref_open(&ref, repo, refname, 0);
4169 if (err == NULL) {
4170 err = got_error(GOT_ERR_TAG_EXISTS);
4171 goto done;
4172 } else if (err->code != GOT_ERR_NOT_REF)
4173 goto done;
4175 if (tagmsg_arg == NULL) {
4176 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4177 tag_name, got_repo_get_path(repo));
4178 if (err) {
4179 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4180 tagmsg_path != NULL)
4181 preserve_tagmsg = 1;
4182 goto done;
4186 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4187 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4188 if (err) {
4189 if (tagmsg_path)
4190 preserve_tagmsg = 1;
4191 goto done;
4194 err = got_ref_alloc(&ref, refname, tag_id);
4195 if (err) {
4196 if (tagmsg_path)
4197 preserve_tagmsg = 1;
4198 goto done;
4201 err = got_ref_write(ref, repo);
4202 if (err) {
4203 if (tagmsg_path)
4204 preserve_tagmsg = 1;
4205 goto done;
4208 err = got_object_id_str(&tag_id_str, tag_id);
4209 if (err) {
4210 if (tagmsg_path)
4211 preserve_tagmsg = 1;
4212 goto done;
4214 printf("Created tag %s\n", tag_id_str);
4215 done:
4216 if (preserve_tagmsg) {
4217 fprintf(stderr, "%s: tag message preserved in %s\n",
4218 getprogname(), tagmsg_path);
4219 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4220 err = got_error_from_errno2("unlink", tagmsg_path);
4221 free(tag_id_str);
4222 if (ref)
4223 got_ref_close(ref);
4224 free(commit_id);
4225 free(commit_id_str);
4226 free(refname);
4227 free(tagmsg);
4228 free(tagmsg_path);
4229 free(tagger);
4230 return err;
4233 static const struct got_error *
4234 cmd_tag(int argc, char *argv[])
4236 const struct got_error *error = NULL;
4237 struct got_repository *repo = NULL;
4238 struct got_worktree *worktree = NULL;
4239 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4240 char *gitconfig_path = NULL;
4241 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4242 int ch, do_list = 0;
4244 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4245 switch (ch) {
4246 case 'c':
4247 commit_id_arg = optarg;
4248 break;
4249 case 'm':
4250 tagmsg = optarg;
4251 break;
4252 case 'r':
4253 repo_path = realpath(optarg, NULL);
4254 if (repo_path == NULL)
4255 return got_error_from_errno2("realpath",
4256 optarg);
4257 got_path_strip_trailing_slashes(repo_path);
4258 break;
4259 case 'l':
4260 do_list = 1;
4261 break;
4262 default:
4263 usage_tag();
4264 /* NOTREACHED */
4268 argc -= optind;
4269 argv += optind;
4271 if (do_list) {
4272 if (commit_id_arg != NULL)
4273 errx(1, "-c option can only be used when creating a tag");
4274 if (tagmsg)
4275 errx(1, "-l and -m options are mutually exclusive");
4276 if (argc > 0)
4277 usage_tag();
4278 } else if (argc != 1)
4279 usage_tag();
4281 tag_name = argv[0];
4283 #ifndef PROFILE
4284 if (do_list) {
4285 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4286 NULL) == -1)
4287 err(1, "pledge");
4288 } else {
4289 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4290 "sendfd unveil", NULL) == -1)
4291 err(1, "pledge");
4293 #endif
4294 cwd = getcwd(NULL, 0);
4295 if (cwd == NULL) {
4296 error = got_error_from_errno("getcwd");
4297 goto done;
4300 if (repo_path == NULL) {
4301 error = got_worktree_open(&worktree, cwd);
4302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4303 goto done;
4304 else
4305 error = NULL;
4306 if (worktree) {
4307 repo_path =
4308 strdup(got_worktree_get_repo_path(worktree));
4309 if (repo_path == NULL)
4310 error = got_error_from_errno("strdup");
4311 if (error)
4312 goto done;
4313 } else {
4314 repo_path = strdup(cwd);
4315 if (repo_path == NULL) {
4316 error = got_error_from_errno("strdup");
4317 goto done;
4322 if (do_list) {
4323 error = got_repo_open(&repo, repo_path, NULL);
4324 if (error != NULL)
4325 goto done;
4326 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4327 if (error)
4328 goto done;
4329 error = list_tags(repo, worktree);
4330 } else {
4331 error = get_gitconfig_path(&gitconfig_path);
4332 if (error)
4333 goto done;
4334 error = got_repo_open(&repo, repo_path, gitconfig_path);
4335 if (error != NULL)
4336 goto done;
4338 if (tagmsg) {
4339 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4340 if (error)
4341 goto done;
4344 if (commit_id_arg == NULL) {
4345 struct got_reference *head_ref;
4346 struct got_object_id *commit_id;
4347 error = got_ref_open(&head_ref, repo,
4348 worktree ? got_worktree_get_head_ref_name(worktree)
4349 : GOT_REF_HEAD, 0);
4350 if (error)
4351 goto done;
4352 error = got_ref_resolve(&commit_id, repo, head_ref);
4353 got_ref_close(head_ref);
4354 if (error)
4355 goto done;
4356 error = got_object_id_str(&commit_id_str, commit_id);
4357 free(commit_id);
4358 if (error)
4359 goto done;
4362 error = add_tag(repo, tag_name,
4363 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4365 done:
4366 if (repo)
4367 got_repo_close(repo);
4368 if (worktree)
4369 got_worktree_close(worktree);
4370 free(cwd);
4371 free(repo_path);
4372 free(gitconfig_path);
4373 free(commit_id_str);
4374 return error;
4377 __dead static void
4378 usage_add(void)
4380 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4381 getprogname());
4382 exit(1);
4385 static const struct got_error *
4386 add_progress(void *arg, unsigned char status, const char *path)
4388 while (path[0] == '/')
4389 path++;
4390 printf("%c %s\n", status, path);
4391 return NULL;
4394 static const struct got_error *
4395 cmd_add(int argc, char *argv[])
4397 const struct got_error *error = NULL;
4398 struct got_repository *repo = NULL;
4399 struct got_worktree *worktree = NULL;
4400 char *cwd = NULL;
4401 struct got_pathlist_head paths;
4402 struct got_pathlist_entry *pe;
4403 int ch, can_recurse = 0, no_ignores = 0;
4405 TAILQ_INIT(&paths);
4407 while ((ch = getopt(argc, argv, "IR")) != -1) {
4408 switch (ch) {
4409 case 'I':
4410 no_ignores = 1;
4411 break;
4412 case 'R':
4413 can_recurse = 1;
4414 break;
4415 default:
4416 usage_add();
4417 /* NOTREACHED */
4421 argc -= optind;
4422 argv += optind;
4424 #ifndef PROFILE
4425 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4426 NULL) == -1)
4427 err(1, "pledge");
4428 #endif
4429 if (argc < 1)
4430 usage_add();
4432 cwd = getcwd(NULL, 0);
4433 if (cwd == NULL) {
4434 error = got_error_from_errno("getcwd");
4435 goto done;
4438 error = got_worktree_open(&worktree, cwd);
4439 if (error)
4440 goto done;
4442 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4443 NULL);
4444 if (error != NULL)
4445 goto done;
4447 error = apply_unveil(got_repo_get_path(repo), 1,
4448 got_worktree_get_root_path(worktree));
4449 if (error)
4450 goto done;
4452 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4453 if (error)
4454 goto done;
4456 if (!can_recurse && no_ignores) {
4457 error = got_error_msg(GOT_ERR_BAD_PATH,
4458 "disregarding ignores requires -R option");
4459 goto done;
4463 if (!can_recurse) {
4464 char *ondisk_path;
4465 struct stat sb;
4466 TAILQ_FOREACH(pe, &paths, entry) {
4467 if (asprintf(&ondisk_path, "%s/%s",
4468 got_worktree_get_root_path(worktree),
4469 pe->path) == -1) {
4470 error = got_error_from_errno("asprintf");
4471 goto done;
4473 if (lstat(ondisk_path, &sb) == -1) {
4474 if (errno == ENOENT) {
4475 free(ondisk_path);
4476 continue;
4478 error = got_error_from_errno2("lstat",
4479 ondisk_path);
4480 free(ondisk_path);
4481 goto done;
4483 free(ondisk_path);
4484 if (S_ISDIR(sb.st_mode)) {
4485 error = got_error_msg(GOT_ERR_BAD_PATH,
4486 "adding directories requires -R option");
4487 goto done;
4492 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4493 NULL, repo, no_ignores);
4494 done:
4495 if (repo)
4496 got_repo_close(repo);
4497 if (worktree)
4498 got_worktree_close(worktree);
4499 TAILQ_FOREACH(pe, &paths, entry)
4500 free((char *)pe->path);
4501 got_pathlist_free(&paths);
4502 free(cwd);
4503 return error;
4506 __dead static void
4507 usage_remove(void)
4509 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4510 getprogname());
4511 exit(1);
4514 static const struct got_error *
4515 print_remove_status(void *arg, unsigned char status,
4516 unsigned char staged_status, const char *path)
4518 while (path[0] == '/')
4519 path++;
4520 if (status == GOT_STATUS_NONEXISTENT)
4521 return NULL;
4522 if (status == staged_status && (status == GOT_STATUS_DELETE))
4523 status = GOT_STATUS_NO_CHANGE;
4524 printf("%c%c %s\n", status, staged_status, path);
4525 return NULL;
4528 static const struct got_error *
4529 cmd_remove(int argc, char *argv[])
4531 const struct got_error *error = NULL;
4532 struct got_worktree *worktree = NULL;
4533 struct got_repository *repo = NULL;
4534 char *cwd = NULL;
4535 struct got_pathlist_head paths;
4536 struct got_pathlist_entry *pe;
4537 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4539 TAILQ_INIT(&paths);
4541 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4542 switch (ch) {
4543 case 'f':
4544 delete_local_mods = 1;
4545 break;
4546 case 'k':
4547 keep_on_disk = 1;
4548 break;
4549 case 'R':
4550 can_recurse = 1;
4551 break;
4552 default:
4553 usage_remove();
4554 /* NOTREACHED */
4558 argc -= optind;
4559 argv += optind;
4561 #ifndef PROFILE
4562 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4563 NULL) == -1)
4564 err(1, "pledge");
4565 #endif
4566 if (argc < 1)
4567 usage_remove();
4569 cwd = getcwd(NULL, 0);
4570 if (cwd == NULL) {
4571 error = got_error_from_errno("getcwd");
4572 goto done;
4574 error = got_worktree_open(&worktree, cwd);
4575 if (error)
4576 goto done;
4578 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4579 NULL);
4580 if (error)
4581 goto done;
4583 error = apply_unveil(got_repo_get_path(repo), 1,
4584 got_worktree_get_root_path(worktree));
4585 if (error)
4586 goto done;
4588 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4589 if (error)
4590 goto done;
4592 if (!can_recurse) {
4593 char *ondisk_path;
4594 struct stat sb;
4595 TAILQ_FOREACH(pe, &paths, entry) {
4596 if (asprintf(&ondisk_path, "%s/%s",
4597 got_worktree_get_root_path(worktree),
4598 pe->path) == -1) {
4599 error = got_error_from_errno("asprintf");
4600 goto done;
4602 if (lstat(ondisk_path, &sb) == -1) {
4603 if (errno == ENOENT) {
4604 free(ondisk_path);
4605 continue;
4607 error = got_error_from_errno2("lstat",
4608 ondisk_path);
4609 free(ondisk_path);
4610 goto done;
4612 free(ondisk_path);
4613 if (S_ISDIR(sb.st_mode)) {
4614 error = got_error_msg(GOT_ERR_BAD_PATH,
4615 "removing directories requires -R option");
4616 goto done;
4621 error = got_worktree_schedule_delete(worktree, &paths,
4622 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4623 done:
4624 if (repo)
4625 got_repo_close(repo);
4626 if (worktree)
4627 got_worktree_close(worktree);
4628 TAILQ_FOREACH(pe, &paths, entry)
4629 free((char *)pe->path);
4630 got_pathlist_free(&paths);
4631 free(cwd);
4632 return error;
4635 __dead static void
4636 usage_revert(void)
4638 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4639 "path ...\n", getprogname());
4640 exit(1);
4643 static const struct got_error *
4644 revert_progress(void *arg, unsigned char status, const char *path)
4646 if (status == GOT_STATUS_UNVERSIONED)
4647 return NULL;
4649 while (path[0] == '/')
4650 path++;
4651 printf("%c %s\n", status, path);
4652 return NULL;
4655 struct choose_patch_arg {
4656 FILE *patch_script_file;
4657 const char *action;
4660 static const struct got_error *
4661 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4662 int nchanges, const char *action)
4664 char *line = NULL;
4665 size_t linesize = 0;
4666 ssize_t linelen;
4668 switch (status) {
4669 case GOT_STATUS_ADD:
4670 printf("A %s\n%s this addition? [y/n] ", path, action);
4671 break;
4672 case GOT_STATUS_DELETE:
4673 printf("D %s\n%s this deletion? [y/n] ", path, action);
4674 break;
4675 case GOT_STATUS_MODIFY:
4676 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4677 return got_error_from_errno("fseek");
4678 printf(GOT_COMMIT_SEP_STR);
4679 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4680 printf("%s", line);
4681 if (ferror(patch_file))
4682 return got_error_from_errno("getline");
4683 printf(GOT_COMMIT_SEP_STR);
4684 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4685 path, n, nchanges, action);
4686 break;
4687 default:
4688 return got_error_path(path, GOT_ERR_FILE_STATUS);
4691 return NULL;
4694 static const struct got_error *
4695 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4696 FILE *patch_file, int n, int nchanges)
4698 const struct got_error *err = NULL;
4699 char *line = NULL;
4700 size_t linesize = 0;
4701 ssize_t linelen;
4702 int resp = ' ';
4703 struct choose_patch_arg *a = arg;
4705 *choice = GOT_PATCH_CHOICE_NONE;
4707 if (a->patch_script_file) {
4708 char *nl;
4709 err = show_change(status, path, patch_file, n, nchanges,
4710 a->action);
4711 if (err)
4712 return err;
4713 linelen = getline(&line, &linesize, a->patch_script_file);
4714 if (linelen == -1) {
4715 if (ferror(a->patch_script_file))
4716 return got_error_from_errno("getline");
4717 return NULL;
4719 nl = strchr(line, '\n');
4720 if (nl)
4721 *nl = '\0';
4722 if (strcmp(line, "y") == 0) {
4723 *choice = GOT_PATCH_CHOICE_YES;
4724 printf("y\n");
4725 } else if (strcmp(line, "n") == 0) {
4726 *choice = GOT_PATCH_CHOICE_NO;
4727 printf("n\n");
4728 } else if (strcmp(line, "q") == 0 &&
4729 status == GOT_STATUS_MODIFY) {
4730 *choice = GOT_PATCH_CHOICE_QUIT;
4731 printf("q\n");
4732 } else
4733 printf("invalid response '%s'\n", line);
4734 free(line);
4735 return NULL;
4738 while (resp != 'y' && resp != 'n' && resp != 'q') {
4739 err = show_change(status, path, patch_file, n, nchanges,
4740 a->action);
4741 if (err)
4742 return err;
4743 resp = getchar();
4744 if (resp == '\n')
4745 resp = getchar();
4746 if (status == GOT_STATUS_MODIFY) {
4747 if (resp != 'y' && resp != 'n' && resp != 'q') {
4748 printf("invalid response '%c'\n", resp);
4749 resp = ' ';
4751 } else if (resp != 'y' && resp != 'n') {
4752 printf("invalid response '%c'\n", resp);
4753 resp = ' ';
4757 if (resp == 'y')
4758 *choice = GOT_PATCH_CHOICE_YES;
4759 else if (resp == 'n')
4760 *choice = GOT_PATCH_CHOICE_NO;
4761 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4762 *choice = GOT_PATCH_CHOICE_QUIT;
4764 return NULL;
4768 static const struct got_error *
4769 cmd_revert(int argc, char *argv[])
4771 const struct got_error *error = NULL;
4772 struct got_worktree *worktree = NULL;
4773 struct got_repository *repo = NULL;
4774 char *cwd = NULL, *path = NULL;
4775 struct got_pathlist_head paths;
4776 struct got_pathlist_entry *pe;
4777 int ch, can_recurse = 0, pflag = 0;
4778 FILE *patch_script_file = NULL;
4779 const char *patch_script_path = NULL;
4780 struct choose_patch_arg cpa;
4782 TAILQ_INIT(&paths);
4784 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4785 switch (ch) {
4786 case 'p':
4787 pflag = 1;
4788 break;
4789 case 'F':
4790 patch_script_path = optarg;
4791 break;
4792 case 'R':
4793 can_recurse = 1;
4794 break;
4795 default:
4796 usage_revert();
4797 /* NOTREACHED */
4801 argc -= optind;
4802 argv += optind;
4804 #ifndef PROFILE
4805 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 "unveil", NULL) == -1)
4807 err(1, "pledge");
4808 #endif
4809 if (argc < 1)
4810 usage_revert();
4811 if (patch_script_path && !pflag)
4812 errx(1, "-F option can only be used together with -p option");
4814 cwd = getcwd(NULL, 0);
4815 if (cwd == NULL) {
4816 error = got_error_from_errno("getcwd");
4817 goto done;
4819 error = got_worktree_open(&worktree, cwd);
4820 if (error)
4821 goto done;
4823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4824 NULL);
4825 if (error != NULL)
4826 goto done;
4828 if (patch_script_path) {
4829 patch_script_file = fopen(patch_script_path, "r");
4830 if (patch_script_file == NULL) {
4831 error = got_error_from_errno2("fopen",
4832 patch_script_path);
4833 goto done;
4836 error = apply_unveil(got_repo_get_path(repo), 1,
4837 got_worktree_get_root_path(worktree));
4838 if (error)
4839 goto done;
4841 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4842 if (error)
4843 goto done;
4845 if (!can_recurse) {
4846 char *ondisk_path;
4847 struct stat sb;
4848 TAILQ_FOREACH(pe, &paths, entry) {
4849 if (asprintf(&ondisk_path, "%s/%s",
4850 got_worktree_get_root_path(worktree),
4851 pe->path) == -1) {
4852 error = got_error_from_errno("asprintf");
4853 goto done;
4855 if (lstat(ondisk_path, &sb) == -1) {
4856 if (errno == ENOENT) {
4857 free(ondisk_path);
4858 continue;
4860 error = got_error_from_errno2("lstat",
4861 ondisk_path);
4862 free(ondisk_path);
4863 goto done;
4865 free(ondisk_path);
4866 if (S_ISDIR(sb.st_mode)) {
4867 error = got_error_msg(GOT_ERR_BAD_PATH,
4868 "reverting directories requires -R option");
4869 goto done;
4874 cpa.patch_script_file = patch_script_file;
4875 cpa.action = "revert";
4876 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4877 pflag ? choose_patch : NULL, &cpa, repo);
4878 done:
4879 if (patch_script_file && fclose(patch_script_file) == EOF &&
4880 error == NULL)
4881 error = got_error_from_errno2("fclose", patch_script_path);
4882 if (repo)
4883 got_repo_close(repo);
4884 if (worktree)
4885 got_worktree_close(worktree);
4886 free(path);
4887 free(cwd);
4888 return error;
4891 __dead static void
4892 usage_commit(void)
4894 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4895 getprogname());
4896 exit(1);
4899 struct collect_commit_logmsg_arg {
4900 const char *cmdline_log;
4901 const char *editor;
4902 const char *worktree_path;
4903 const char *branch_name;
4904 const char *repo_path;
4905 char *logmsg_path;
4909 static const struct got_error *
4910 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4911 void *arg)
4913 char *initial_content = NULL;
4914 struct got_pathlist_entry *pe;
4915 const struct got_error *err = NULL;
4916 char *template = NULL;
4917 struct collect_commit_logmsg_arg *a = arg;
4918 int fd;
4919 size_t len;
4921 /* if a message was specified on the command line, just use it */
4922 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4923 len = strlen(a->cmdline_log) + 1;
4924 *logmsg = malloc(len + 1);
4925 if (*logmsg == NULL)
4926 return got_error_from_errno("malloc");
4927 strlcpy(*logmsg, a->cmdline_log, len);
4928 return NULL;
4931 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4932 return got_error_from_errno("asprintf");
4934 if (asprintf(&initial_content,
4935 "\n# changes to be committed on branch %s:\n",
4936 a->branch_name) == -1)
4937 return got_error_from_errno("asprintf");
4939 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4940 if (err)
4941 goto done;
4943 dprintf(fd, initial_content);
4945 TAILQ_FOREACH(pe, commitable_paths, entry) {
4946 struct got_commitable *ct = pe->data;
4947 dprintf(fd, "# %c %s\n",
4948 got_commitable_get_status(ct),
4949 got_commitable_get_path(ct));
4951 close(fd);
4953 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4954 done:
4955 free(initial_content);
4956 free(template);
4958 /* Editor is done; we can now apply unveil(2) */
4959 if (err == NULL) {
4960 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4961 if (err) {
4962 free(*logmsg);
4963 *logmsg = NULL;
4966 return err;
4969 static const struct got_error *
4970 cmd_commit(int argc, char *argv[])
4972 const struct got_error *error = NULL;
4973 struct got_worktree *worktree = NULL;
4974 struct got_repository *repo = NULL;
4975 char *cwd = NULL, *id_str = NULL;
4976 struct got_object_id *id = NULL;
4977 const char *logmsg = NULL;
4978 struct collect_commit_logmsg_arg cl_arg;
4979 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4980 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4981 struct got_pathlist_head paths;
4983 TAILQ_INIT(&paths);
4984 cl_arg.logmsg_path = NULL;
4986 while ((ch = getopt(argc, argv, "m:")) != -1) {
4987 switch (ch) {
4988 case 'm':
4989 logmsg = optarg;
4990 break;
4991 default:
4992 usage_commit();
4993 /* NOTREACHED */
4997 argc -= optind;
4998 argv += optind;
5000 #ifndef PROFILE
5001 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5002 "unveil", NULL) == -1)
5003 err(1, "pledge");
5004 #endif
5005 cwd = getcwd(NULL, 0);
5006 if (cwd == NULL) {
5007 error = got_error_from_errno("getcwd");
5008 goto done;
5010 error = got_worktree_open(&worktree, cwd);
5011 if (error)
5012 goto done;
5014 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5015 if (error)
5016 goto done;
5017 if (rebase_in_progress) {
5018 error = got_error(GOT_ERR_REBASING);
5019 goto done;
5022 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5023 worktree);
5024 if (error)
5025 goto done;
5027 error = get_gitconfig_path(&gitconfig_path);
5028 if (error)
5029 goto done;
5030 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5031 gitconfig_path);
5032 if (error != NULL)
5033 goto done;
5035 error = get_author(&author, repo);
5036 if (error)
5037 return error;
5040 * unveil(2) traverses exec(2); if an editor is used we have
5041 * to apply unveil after the log message has been written.
5043 if (logmsg == NULL || strlen(logmsg) == 0)
5044 error = get_editor(&editor);
5045 else
5046 error = apply_unveil(got_repo_get_path(repo), 0,
5047 got_worktree_get_root_path(worktree));
5048 if (error)
5049 goto done;
5051 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5052 if (error)
5053 goto done;
5055 cl_arg.editor = editor;
5056 cl_arg.cmdline_log = logmsg;
5057 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5058 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5059 if (!histedit_in_progress) {
5060 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5061 error = got_error(GOT_ERR_COMMIT_BRANCH);
5062 goto done;
5064 cl_arg.branch_name += 11;
5066 cl_arg.repo_path = got_repo_get_path(repo);
5067 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5068 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5069 if (error) {
5070 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5071 cl_arg.logmsg_path != NULL)
5072 preserve_logmsg = 1;
5073 goto done;
5076 error = got_object_id_str(&id_str, id);
5077 if (error)
5078 goto done;
5079 printf("Created commit %s\n", id_str);
5080 done:
5081 if (preserve_logmsg) {
5082 fprintf(stderr, "%s: log message preserved in %s\n",
5083 getprogname(), cl_arg.logmsg_path);
5084 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5085 error == NULL)
5086 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5087 free(cl_arg.logmsg_path);
5088 if (repo)
5089 got_repo_close(repo);
5090 if (worktree)
5091 got_worktree_close(worktree);
5092 free(cwd);
5093 free(id_str);
5094 free(gitconfig_path);
5095 free(editor);
5096 free(author);
5097 return error;
5100 __dead static void
5101 usage_cherrypick(void)
5103 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5104 exit(1);
5107 static const struct got_error *
5108 cmd_cherrypick(int argc, char *argv[])
5110 const struct got_error *error = NULL;
5111 struct got_worktree *worktree = NULL;
5112 struct got_repository *repo = NULL;
5113 char *cwd = NULL, *commit_id_str = NULL;
5114 struct got_object_id *commit_id = NULL;
5115 struct got_commit_object *commit = NULL;
5116 struct got_object_qid *pid;
5117 struct got_reference *head_ref = NULL;
5118 int ch, did_something = 0;
5120 while ((ch = getopt(argc, argv, "")) != -1) {
5121 switch (ch) {
5122 default:
5123 usage_cherrypick();
5124 /* NOTREACHED */
5128 argc -= optind;
5129 argv += optind;
5131 #ifndef PROFILE
5132 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5133 "unveil", NULL) == -1)
5134 err(1, "pledge");
5135 #endif
5136 if (argc != 1)
5137 usage_cherrypick();
5139 cwd = getcwd(NULL, 0);
5140 if (cwd == NULL) {
5141 error = got_error_from_errno("getcwd");
5142 goto done;
5144 error = got_worktree_open(&worktree, cwd);
5145 if (error)
5146 goto done;
5148 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5149 NULL);
5150 if (error != NULL)
5151 goto done;
5153 error = apply_unveil(got_repo_get_path(repo), 0,
5154 got_worktree_get_root_path(worktree));
5155 if (error)
5156 goto done;
5158 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5159 GOT_OBJ_TYPE_COMMIT, repo);
5160 if (error != NULL) {
5161 struct got_reference *ref;
5162 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5163 goto done;
5164 error = got_ref_open(&ref, repo, argv[0], 0);
5165 if (error != NULL)
5166 goto done;
5167 error = got_ref_resolve(&commit_id, repo, ref);
5168 got_ref_close(ref);
5169 if (error != NULL)
5170 goto done;
5172 error = got_object_id_str(&commit_id_str, commit_id);
5173 if (error)
5174 goto done;
5176 error = got_ref_open(&head_ref, repo,
5177 got_worktree_get_head_ref_name(worktree), 0);
5178 if (error != NULL)
5179 goto done;
5181 error = check_same_branch(commit_id, head_ref, NULL, repo);
5182 if (error) {
5183 if (error->code != GOT_ERR_ANCESTRY)
5184 goto done;
5185 error = NULL;
5186 } else {
5187 error = got_error(GOT_ERR_SAME_BRANCH);
5188 goto done;
5191 error = got_object_open_as_commit(&commit, repo, commit_id);
5192 if (error)
5193 goto done;
5194 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5195 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5196 commit_id, repo, update_progress, &did_something, check_cancelled,
5197 NULL);
5198 if (error != NULL)
5199 goto done;
5201 if (did_something)
5202 printf("Merged commit %s\n", commit_id_str);
5203 done:
5204 if (commit)
5205 got_object_commit_close(commit);
5206 free(commit_id_str);
5207 if (head_ref)
5208 got_ref_close(head_ref);
5209 if (worktree)
5210 got_worktree_close(worktree);
5211 if (repo)
5212 got_repo_close(repo);
5213 return error;
5216 __dead static void
5217 usage_backout(void)
5219 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5220 exit(1);
5223 static const struct got_error *
5224 cmd_backout(int argc, char *argv[])
5226 const struct got_error *error = NULL;
5227 struct got_worktree *worktree = NULL;
5228 struct got_repository *repo = NULL;
5229 char *cwd = NULL, *commit_id_str = NULL;
5230 struct got_object_id *commit_id = NULL;
5231 struct got_commit_object *commit = NULL;
5232 struct got_object_qid *pid;
5233 struct got_reference *head_ref = NULL;
5234 int ch, did_something = 0;
5236 while ((ch = getopt(argc, argv, "")) != -1) {
5237 switch (ch) {
5238 default:
5239 usage_backout();
5240 /* NOTREACHED */
5244 argc -= optind;
5245 argv += optind;
5247 #ifndef PROFILE
5248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5249 "unveil", NULL) == -1)
5250 err(1, "pledge");
5251 #endif
5252 if (argc != 1)
5253 usage_backout();
5255 cwd = getcwd(NULL, 0);
5256 if (cwd == NULL) {
5257 error = got_error_from_errno("getcwd");
5258 goto done;
5260 error = got_worktree_open(&worktree, cwd);
5261 if (error)
5262 goto done;
5264 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5265 NULL);
5266 if (error != NULL)
5267 goto done;
5269 error = apply_unveil(got_repo_get_path(repo), 0,
5270 got_worktree_get_root_path(worktree));
5271 if (error)
5272 goto done;
5274 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5275 GOT_OBJ_TYPE_COMMIT, repo);
5276 if (error != NULL) {
5277 struct got_reference *ref;
5278 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5279 goto done;
5280 error = got_ref_open(&ref, repo, argv[0], 0);
5281 if (error != NULL)
5282 goto done;
5283 error = got_ref_resolve(&commit_id, repo, ref);
5284 got_ref_close(ref);
5285 if (error != NULL)
5286 goto done;
5288 error = got_object_id_str(&commit_id_str, commit_id);
5289 if (error)
5290 goto done;
5292 error = got_ref_open(&head_ref, repo,
5293 got_worktree_get_head_ref_name(worktree), 0);
5294 if (error != NULL)
5295 goto done;
5297 error = check_same_branch(commit_id, head_ref, NULL, repo);
5298 if (error)
5299 goto done;
5301 error = got_object_open_as_commit(&commit, repo, commit_id);
5302 if (error)
5303 goto done;
5304 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5305 if (pid == NULL) {
5306 error = got_error(GOT_ERR_ROOT_COMMIT);
5307 goto done;
5310 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5311 update_progress, &did_something, check_cancelled, NULL);
5312 if (error != NULL)
5313 goto done;
5315 if (did_something)
5316 printf("Backed out commit %s\n", commit_id_str);
5317 done:
5318 if (commit)
5319 got_object_commit_close(commit);
5320 free(commit_id_str);
5321 if (head_ref)
5322 got_ref_close(head_ref);
5323 if (worktree)
5324 got_worktree_close(worktree);
5325 if (repo)
5326 got_repo_close(repo);
5327 return error;
5330 __dead static void
5331 usage_rebase(void)
5333 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5334 getprogname());
5335 exit(1);
5338 void
5339 trim_logmsg(char *logmsg, int limit)
5341 char *nl;
5342 size_t len;
5344 len = strlen(logmsg);
5345 if (len > limit)
5346 len = limit;
5347 logmsg[len] = '\0';
5348 nl = strchr(logmsg, '\n');
5349 if (nl)
5350 *nl = '\0';
5353 static const struct got_error *
5354 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5356 const struct got_error *err;
5357 char *logmsg0 = NULL;
5358 const char *s;
5360 err = got_object_commit_get_logmsg(&logmsg0, commit);
5361 if (err)
5362 return err;
5364 s = logmsg0;
5365 while (isspace((unsigned char)s[0]))
5366 s++;
5368 *logmsg = strdup(s);
5369 if (*logmsg == NULL) {
5370 err = got_error_from_errno("strdup");
5371 goto done;
5374 trim_logmsg(*logmsg, limit);
5375 done:
5376 free(logmsg0);
5377 return err;
5380 static const struct got_error *
5381 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5383 const struct got_error *err;
5384 struct got_commit_object *commit = NULL;
5385 char *id_str = NULL, *logmsg = NULL;
5387 err = got_object_open_as_commit(&commit, repo, id);
5388 if (err)
5389 return err;
5391 err = got_object_id_str(&id_str, id);
5392 if (err)
5393 goto done;
5395 id_str[12] = '\0';
5397 err = get_short_logmsg(&logmsg, 42, commit);
5398 if (err)
5399 goto done;
5401 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5402 done:
5403 free(id_str);
5404 got_object_commit_close(commit);
5405 free(logmsg);
5406 return err;
5409 static const struct got_error *
5410 show_rebase_progress(struct got_commit_object *commit,
5411 struct got_object_id *old_id, struct got_object_id *new_id)
5413 const struct got_error *err;
5414 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5416 err = got_object_id_str(&old_id_str, old_id);
5417 if (err)
5418 goto done;
5420 if (new_id) {
5421 err = got_object_id_str(&new_id_str, new_id);
5422 if (err)
5423 goto done;
5426 old_id_str[12] = '\0';
5427 if (new_id_str)
5428 new_id_str[12] = '\0';
5430 err = get_short_logmsg(&logmsg, 42, commit);
5431 if (err)
5432 goto done;
5434 printf("%s -> %s: %s\n", old_id_str,
5435 new_id_str ? new_id_str : "no-op change", logmsg);
5436 done:
5437 free(old_id_str);
5438 free(new_id_str);
5439 free(logmsg);
5440 return err;
5443 static const struct got_error *
5444 rebase_progress(void *arg, unsigned char status, const char *path)
5446 unsigned char *rebase_status = arg;
5448 while (path[0] == '/')
5449 path++;
5450 printf("%c %s\n", status, path);
5452 if (*rebase_status == GOT_STATUS_CONFLICT)
5453 return NULL;
5454 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5455 *rebase_status = status;
5456 return NULL;
5459 static const struct got_error *
5460 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5461 struct got_reference *branch, struct got_reference *new_base_branch,
5462 struct got_reference *tmp_branch, struct got_repository *repo)
5464 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5465 return got_worktree_rebase_complete(worktree, fileindex,
5466 new_base_branch, tmp_branch, branch, repo);
5469 static const struct got_error *
5470 rebase_commit(struct got_pathlist_head *merged_paths,
5471 struct got_worktree *worktree, struct got_fileindex *fileindex,
5472 struct got_reference *tmp_branch,
5473 struct got_object_id *commit_id, struct got_repository *repo)
5475 const struct got_error *error;
5476 struct got_commit_object *commit;
5477 struct got_object_id *new_commit_id;
5479 error = got_object_open_as_commit(&commit, repo, commit_id);
5480 if (error)
5481 return error;
5483 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5484 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5485 if (error) {
5486 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5487 goto done;
5488 error = show_rebase_progress(commit, commit_id, NULL);
5489 } else {
5490 error = show_rebase_progress(commit, commit_id, new_commit_id);
5491 free(new_commit_id);
5493 done:
5494 got_object_commit_close(commit);
5495 return error;
5498 struct check_path_prefix_arg {
5499 const char *path_prefix;
5500 size_t len;
5501 int errcode;
5504 static const struct got_error *
5505 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5506 struct got_blob_object *blob2, struct got_object_id *id1,
5507 struct got_object_id *id2, const char *path1, const char *path2,
5508 mode_t mode1, mode_t mode2, struct got_repository *repo)
5510 struct check_path_prefix_arg *a = arg;
5512 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5513 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5514 return got_error(a->errcode);
5516 return NULL;
5519 static const struct got_error *
5520 check_path_prefix(struct got_object_id *parent_id,
5521 struct got_object_id *commit_id, const char *path_prefix,
5522 int errcode, struct got_repository *repo)
5524 const struct got_error *err;
5525 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5526 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5527 struct check_path_prefix_arg cpp_arg;
5529 if (got_path_is_root_dir(path_prefix))
5530 return NULL;
5532 err = got_object_open_as_commit(&commit, repo, commit_id);
5533 if (err)
5534 goto done;
5536 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5537 if (err)
5538 goto done;
5540 err = got_object_open_as_tree(&tree1, repo,
5541 got_object_commit_get_tree_id(parent_commit));
5542 if (err)
5543 goto done;
5545 err = got_object_open_as_tree(&tree2, repo,
5546 got_object_commit_get_tree_id(commit));
5547 if (err)
5548 goto done;
5550 cpp_arg.path_prefix = path_prefix;
5551 while (cpp_arg.path_prefix[0] == '/')
5552 cpp_arg.path_prefix++;
5553 cpp_arg.len = strlen(cpp_arg.path_prefix);
5554 cpp_arg.errcode = errcode;
5555 err = got_diff_tree(tree1, tree2, "", "", repo,
5556 check_path_prefix_in_diff, &cpp_arg, 0);
5557 done:
5558 if (tree1)
5559 got_object_tree_close(tree1);
5560 if (tree2)
5561 got_object_tree_close(tree2);
5562 if (commit)
5563 got_object_commit_close(commit);
5564 if (parent_commit)
5565 got_object_commit_close(parent_commit);
5566 return err;
5569 static const struct got_error *
5570 collect_commits(struct got_object_id_queue *commits,
5571 struct got_object_id *initial_commit_id,
5572 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5573 const char *path_prefix, int path_prefix_errcode,
5574 struct got_repository *repo)
5576 const struct got_error *err = NULL;
5577 struct got_commit_graph *graph = NULL;
5578 struct got_object_id *parent_id = NULL;
5579 struct got_object_qid *qid;
5580 struct got_object_id *commit_id = initial_commit_id;
5582 err = got_commit_graph_open(&graph, "/", 1);
5583 if (err)
5584 return err;
5586 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5587 check_cancelled, NULL);
5588 if (err)
5589 goto done;
5590 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5591 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5592 check_cancelled, NULL);
5593 if (err) {
5594 if (err->code == GOT_ERR_ITER_COMPLETED) {
5595 err = got_error_msg(GOT_ERR_ANCESTRY,
5596 "ran out of commits to rebase before "
5597 "youngest common ancestor commit has "
5598 "been reached?!?");
5600 goto done;
5601 } else {
5602 err = check_path_prefix(parent_id, commit_id,
5603 path_prefix, path_prefix_errcode, repo);
5604 if (err)
5605 goto done;
5607 err = got_object_qid_alloc(&qid, commit_id);
5608 if (err)
5609 goto done;
5610 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5611 commit_id = parent_id;
5614 done:
5615 got_commit_graph_close(graph);
5616 return err;
5619 static const struct got_error *
5620 cmd_rebase(int argc, char *argv[])
5622 const struct got_error *error = NULL;
5623 struct got_worktree *worktree = NULL;
5624 struct got_repository *repo = NULL;
5625 struct got_fileindex *fileindex = NULL;
5626 char *cwd = NULL;
5627 struct got_reference *branch = NULL;
5628 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5629 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5630 struct got_object_id *resume_commit_id = NULL;
5631 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5632 struct got_commit_object *commit = NULL;
5633 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5634 int histedit_in_progress = 0;
5635 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5636 struct got_object_id_queue commits;
5637 struct got_pathlist_head merged_paths;
5638 const struct got_object_id_queue *parent_ids;
5639 struct got_object_qid *qid, *pid;
5641 SIMPLEQ_INIT(&commits);
5642 TAILQ_INIT(&merged_paths);
5644 while ((ch = getopt(argc, argv, "ac")) != -1) {
5645 switch (ch) {
5646 case 'a':
5647 abort_rebase = 1;
5648 break;
5649 case 'c':
5650 continue_rebase = 1;
5651 break;
5652 default:
5653 usage_rebase();
5654 /* NOTREACHED */
5658 argc -= optind;
5659 argv += optind;
5661 #ifndef PROFILE
5662 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 "unveil", NULL) == -1)
5664 err(1, "pledge");
5665 #endif
5666 if (abort_rebase && continue_rebase)
5667 usage_rebase();
5668 else if (abort_rebase || continue_rebase) {
5669 if (argc != 0)
5670 usage_rebase();
5671 } else if (argc != 1)
5672 usage_rebase();
5674 cwd = getcwd(NULL, 0);
5675 if (cwd == NULL) {
5676 error = got_error_from_errno("getcwd");
5677 goto done;
5679 error = got_worktree_open(&worktree, cwd);
5680 if (error)
5681 goto done;
5683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5684 NULL);
5685 if (error != NULL)
5686 goto done;
5688 error = apply_unveil(got_repo_get_path(repo), 0,
5689 got_worktree_get_root_path(worktree));
5690 if (error)
5691 goto done;
5693 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5694 worktree);
5695 if (error)
5696 goto done;
5697 if (histedit_in_progress) {
5698 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5699 goto done;
5702 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5703 if (error)
5704 goto done;
5706 if (abort_rebase) {
5707 int did_something;
5708 if (!rebase_in_progress) {
5709 error = got_error(GOT_ERR_NOT_REBASING);
5710 goto done;
5712 error = got_worktree_rebase_continue(&resume_commit_id,
5713 &new_base_branch, &tmp_branch, &branch, &fileindex,
5714 worktree, repo);
5715 if (error)
5716 goto done;
5717 printf("Switching work tree to %s\n",
5718 got_ref_get_symref_target(new_base_branch));
5719 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5720 new_base_branch, update_progress, &did_something);
5721 if (error)
5722 goto done;
5723 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5724 goto done; /* nothing else to do */
5727 if (continue_rebase) {
5728 if (!rebase_in_progress) {
5729 error = got_error(GOT_ERR_NOT_REBASING);
5730 goto done;
5732 error = got_worktree_rebase_continue(&resume_commit_id,
5733 &new_base_branch, &tmp_branch, &branch, &fileindex,
5734 worktree, repo);
5735 if (error)
5736 goto done;
5738 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5739 resume_commit_id, repo);
5740 if (error)
5741 goto done;
5743 yca_id = got_object_id_dup(resume_commit_id);
5744 if (yca_id == NULL) {
5745 error = got_error_from_errno("got_object_id_dup");
5746 goto done;
5748 } else {
5749 error = got_ref_open(&branch, repo, argv[0], 0);
5750 if (error != NULL)
5751 goto done;
5754 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5755 if (error)
5756 goto done;
5758 if (!continue_rebase) {
5759 struct got_object_id *base_commit_id;
5761 base_commit_id = got_worktree_get_base_commit_id(worktree);
5762 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5763 base_commit_id, branch_head_commit_id, repo,
5764 check_cancelled, NULL);
5765 if (error)
5766 goto done;
5767 if (yca_id == NULL) {
5768 error = got_error_msg(GOT_ERR_ANCESTRY,
5769 "specified branch shares no common ancestry "
5770 "with work tree's branch");
5771 goto done;
5774 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5775 if (error) {
5776 if (error->code != GOT_ERR_ANCESTRY)
5777 goto done;
5778 error = NULL;
5779 } else {
5780 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5781 "specified branch resolves to a commit which "
5782 "is already contained in work tree's branch");
5783 goto done;
5785 error = got_worktree_rebase_prepare(&new_base_branch,
5786 &tmp_branch, &fileindex, worktree, branch, repo);
5787 if (error)
5788 goto done;
5791 commit_id = branch_head_commit_id;
5792 error = got_object_open_as_commit(&commit, repo, commit_id);
5793 if (error)
5794 goto done;
5796 parent_ids = got_object_commit_get_parent_ids(commit);
5797 pid = SIMPLEQ_FIRST(parent_ids);
5798 if (pid == NULL) {
5799 if (!continue_rebase) {
5800 int did_something;
5801 error = got_worktree_rebase_abort(worktree, fileindex,
5802 repo, new_base_branch, update_progress,
5803 &did_something);
5804 if (error)
5805 goto done;
5806 printf("Rebase of %s aborted\n",
5807 got_ref_get_name(branch));
5809 error = got_error(GOT_ERR_EMPTY_REBASE);
5810 goto done;
5812 error = collect_commits(&commits, commit_id, pid->id,
5813 yca_id, got_worktree_get_path_prefix(worktree),
5814 GOT_ERR_REBASE_PATH, repo);
5815 got_object_commit_close(commit);
5816 commit = NULL;
5817 if (error)
5818 goto done;
5820 if (SIMPLEQ_EMPTY(&commits)) {
5821 if (continue_rebase) {
5822 error = rebase_complete(worktree, fileindex,
5823 branch, new_base_branch, tmp_branch, repo);
5824 goto done;
5825 } else {
5826 /* Fast-forward the reference of the branch. */
5827 struct got_object_id *new_head_commit_id;
5828 char *id_str;
5829 error = got_ref_resolve(&new_head_commit_id, repo,
5830 new_base_branch);
5831 if (error)
5832 goto done;
5833 error = got_object_id_str(&id_str, new_head_commit_id);
5834 printf("Forwarding %s to commit %s\n",
5835 got_ref_get_name(branch), id_str);
5836 free(id_str);
5837 error = got_ref_change_ref(branch,
5838 new_head_commit_id);
5839 if (error)
5840 goto done;
5844 pid = NULL;
5845 SIMPLEQ_FOREACH(qid, &commits, entry) {
5846 commit_id = qid->id;
5847 parent_id = pid ? pid->id : yca_id;
5848 pid = qid;
5850 error = got_worktree_rebase_merge_files(&merged_paths,
5851 worktree, fileindex, parent_id, commit_id, repo,
5852 rebase_progress, &rebase_status, check_cancelled, NULL);
5853 if (error)
5854 goto done;
5856 if (rebase_status == GOT_STATUS_CONFLICT) {
5857 error = show_rebase_merge_conflict(qid->id, repo);
5858 if (error)
5859 goto done;
5860 got_worktree_rebase_pathlist_free(&merged_paths);
5861 break;
5864 error = rebase_commit(&merged_paths, worktree, fileindex,
5865 tmp_branch, commit_id, repo);
5866 got_worktree_rebase_pathlist_free(&merged_paths);
5867 if (error)
5868 goto done;
5871 if (rebase_status == GOT_STATUS_CONFLICT) {
5872 error = got_worktree_rebase_postpone(worktree, fileindex);
5873 if (error)
5874 goto done;
5875 error = got_error_msg(GOT_ERR_CONFLICTS,
5876 "conflicts must be resolved before rebasing can continue");
5877 } else
5878 error = rebase_complete(worktree, fileindex, branch,
5879 new_base_branch, tmp_branch, repo);
5880 done:
5881 got_object_id_queue_free(&commits);
5882 free(branch_head_commit_id);
5883 free(resume_commit_id);
5884 free(yca_id);
5885 if (commit)
5886 got_object_commit_close(commit);
5887 if (branch)
5888 got_ref_close(branch);
5889 if (new_base_branch)
5890 got_ref_close(new_base_branch);
5891 if (tmp_branch)
5892 got_ref_close(tmp_branch);
5893 if (worktree)
5894 got_worktree_close(worktree);
5895 if (repo)
5896 got_repo_close(repo);
5897 return error;
5900 __dead static void
5901 usage_histedit(void)
5903 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5904 getprogname());
5905 exit(1);
5908 #define GOT_HISTEDIT_PICK 'p'
5909 #define GOT_HISTEDIT_EDIT 'e'
5910 #define GOT_HISTEDIT_FOLD 'f'
5911 #define GOT_HISTEDIT_DROP 'd'
5912 #define GOT_HISTEDIT_MESG 'm'
5914 static struct got_histedit_cmd {
5915 unsigned char code;
5916 const char *name;
5917 const char *desc;
5918 } got_histedit_cmds[] = {
5919 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5920 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5921 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5922 "be used" },
5923 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5924 { GOT_HISTEDIT_MESG, "mesg",
5925 "single-line log message for commit above (open editor if empty)" },
5928 struct got_histedit_list_entry {
5929 TAILQ_ENTRY(got_histedit_list_entry) entry;
5930 struct got_object_id *commit_id;
5931 const struct got_histedit_cmd *cmd;
5932 char *logmsg;
5934 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5936 static const struct got_error *
5937 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5938 FILE *f, struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 char *logmsg = NULL, *id_str = NULL;
5942 struct got_commit_object *commit = NULL;
5943 int n;
5945 err = got_object_open_as_commit(&commit, repo, commit_id);
5946 if (err)
5947 goto done;
5949 err = get_short_logmsg(&logmsg, 34, commit);
5950 if (err)
5951 goto done;
5953 err = got_object_id_str(&id_str, commit_id);
5954 if (err)
5955 goto done;
5957 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5958 if (n < 0)
5959 err = got_ferror(f, GOT_ERR_IO);
5960 done:
5961 if (commit)
5962 got_object_commit_close(commit);
5963 free(id_str);
5964 free(logmsg);
5965 return err;
5968 static const struct got_error *
5969 histedit_write_commit_list(struct got_object_id_queue *commits,
5970 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5972 const struct got_error *err = NULL;
5973 struct got_object_qid *qid;
5975 if (SIMPLEQ_EMPTY(commits))
5976 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5978 SIMPLEQ_FOREACH(qid, commits, entry) {
5979 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5980 f, repo);
5981 if (err)
5982 break;
5983 if (edit_logmsg_only) {
5984 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5985 if (n < 0) {
5986 err = got_ferror(f, GOT_ERR_IO);
5987 break;
5992 return err;
5995 static const struct got_error *
5996 write_cmd_list(FILE *f, const char *branch_name,
5997 struct got_object_id_queue *commits)
5999 const struct got_error *err = NULL;
6000 int n, i;
6001 char *id_str;
6002 struct got_object_qid *qid;
6004 qid = SIMPLEQ_FIRST(commits);
6005 err = got_object_id_str(&id_str, qid->id);
6006 if (err)
6007 return err;
6009 n = fprintf(f,
6010 "# Editing the history of branch '%s' starting at\n"
6011 "# commit %s\n"
6012 "# Commits will be processed in order from top to "
6013 "bottom of this file.\n", branch_name, id_str);
6014 if (n < 0) {
6015 err = got_ferror(f, GOT_ERR_IO);
6016 goto done;
6019 n = fprintf(f, "# Available histedit commands:\n");
6020 if (n < 0) {
6021 err = got_ferror(f, GOT_ERR_IO);
6022 goto done;
6025 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6026 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6027 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6028 cmd->desc);
6029 if (n < 0) {
6030 err = got_ferror(f, GOT_ERR_IO);
6031 break;
6034 done:
6035 free(id_str);
6036 return err;
6039 static const struct got_error *
6040 histedit_syntax_error(int lineno)
6042 static char msg[42];
6043 int ret;
6045 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6046 lineno);
6047 if (ret == -1 || ret >= sizeof(msg))
6048 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6050 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6053 static const struct got_error *
6054 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6055 char *logmsg, struct got_repository *repo)
6057 const struct got_error *err;
6058 struct got_commit_object *folded_commit = NULL;
6059 char *id_str, *folded_logmsg = NULL;
6061 err = got_object_id_str(&id_str, hle->commit_id);
6062 if (err)
6063 return err;
6065 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6066 if (err)
6067 goto done;
6069 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6070 if (err)
6071 goto done;
6072 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6073 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6074 folded_logmsg) == -1) {
6075 err = got_error_from_errno("asprintf");
6077 done:
6078 if (folded_commit)
6079 got_object_commit_close(folded_commit);
6080 free(id_str);
6081 free(folded_logmsg);
6082 return err;
6085 static struct got_histedit_list_entry *
6086 get_folded_commits(struct got_histedit_list_entry *hle)
6088 struct got_histedit_list_entry *prev, *folded = NULL;
6090 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6091 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6092 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6093 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6094 folded = prev;
6095 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6098 return folded;
6101 static const struct got_error *
6102 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6103 struct got_repository *repo)
6105 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6106 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6107 const struct got_error *err = NULL;
6108 struct got_commit_object *commit = NULL;
6109 int fd;
6110 struct got_histedit_list_entry *folded = NULL;
6112 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6113 if (err)
6114 return err;
6116 folded = get_folded_commits(hle);
6117 if (folded) {
6118 while (folded != hle) {
6119 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6120 folded = TAILQ_NEXT(folded, entry);
6121 continue;
6123 err = append_folded_commit_msg(&new_msg, folded,
6124 logmsg, repo);
6125 if (err)
6126 goto done;
6127 free(logmsg);
6128 logmsg = new_msg;
6129 folded = TAILQ_NEXT(folded, entry);
6133 err = got_object_id_str(&id_str, hle->commit_id);
6134 if (err)
6135 goto done;
6136 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6137 if (err)
6138 goto done;
6139 if (asprintf(&new_msg,
6140 "%s\n# original log message of commit %s: %s",
6141 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6142 err = got_error_from_errno("asprintf");
6143 goto done;
6145 free(logmsg);
6146 logmsg = new_msg;
6148 err = got_object_id_str(&id_str, hle->commit_id);
6149 if (err)
6150 goto done;
6152 err = got_opentemp_named_fd(&logmsg_path, &fd,
6153 GOT_TMPDIR_STR "/got-logmsg");
6154 if (err)
6155 goto done;
6157 dprintf(fd, logmsg);
6158 close(fd);
6160 err = get_editor(&editor);
6161 if (err)
6162 goto done;
6164 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6165 if (err) {
6166 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6167 goto done;
6168 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6170 done:
6171 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6172 err = got_error_from_errno2("unlink", logmsg_path);
6173 free(logmsg_path);
6174 free(logmsg);
6175 free(orig_logmsg);
6176 free(editor);
6177 if (commit)
6178 got_object_commit_close(commit);
6179 return err;
6182 static const struct got_error *
6183 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6184 FILE *f, struct got_repository *repo)
6186 const struct got_error *err = NULL;
6187 char *line = NULL, *p, *end;
6188 size_t size;
6189 ssize_t len;
6190 int lineno = 0, i;
6191 const struct got_histedit_cmd *cmd;
6192 struct got_object_id *commit_id = NULL;
6193 struct got_histedit_list_entry *hle = NULL;
6195 for (;;) {
6196 len = getline(&line, &size, f);
6197 if (len == -1) {
6198 const struct got_error *getline_err;
6199 if (feof(f))
6200 break;
6201 getline_err = got_error_from_errno("getline");
6202 err = got_ferror(f, getline_err->code);
6203 break;
6205 lineno++;
6206 p = line;
6207 while (isspace((unsigned char)p[0]))
6208 p++;
6209 if (p[0] == '#' || p[0] == '\0') {
6210 free(line);
6211 line = NULL;
6212 continue;
6214 cmd = NULL;
6215 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6216 cmd = &got_histedit_cmds[i];
6217 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6218 isspace((unsigned char)p[strlen(cmd->name)])) {
6219 p += strlen(cmd->name);
6220 break;
6222 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6223 p++;
6224 break;
6227 if (i == nitems(got_histedit_cmds)) {
6228 err = histedit_syntax_error(lineno);
6229 break;
6231 while (isspace((unsigned char)p[0]))
6232 p++;
6233 if (cmd->code == GOT_HISTEDIT_MESG) {
6234 if (hle == NULL || hle->logmsg != NULL) {
6235 err = got_error(GOT_ERR_HISTEDIT_CMD);
6236 break;
6238 if (p[0] == '\0') {
6239 err = histedit_edit_logmsg(hle, repo);
6240 if (err)
6241 break;
6242 } else {
6243 hle->logmsg = strdup(p);
6244 if (hle->logmsg == NULL) {
6245 err = got_error_from_errno("strdup");
6246 break;
6249 free(line);
6250 line = NULL;
6251 continue;
6252 } else {
6253 end = p;
6254 while (end[0] && !isspace((unsigned char)end[0]))
6255 end++;
6256 *end = '\0';
6258 err = got_object_resolve_id_str(&commit_id, repo, p);
6259 if (err) {
6260 /* override error code */
6261 err = histedit_syntax_error(lineno);
6262 break;
6265 hle = malloc(sizeof(*hle));
6266 if (hle == NULL) {
6267 err = got_error_from_errno("malloc");
6268 break;
6270 hle->cmd = cmd;
6271 hle->commit_id = commit_id;
6272 hle->logmsg = NULL;
6273 commit_id = NULL;
6274 free(line);
6275 line = NULL;
6276 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6279 free(line);
6280 free(commit_id);
6281 return err;
6284 static const struct got_error *
6285 histedit_check_script(struct got_histedit_list *histedit_cmds,
6286 struct got_object_id_queue *commits, struct got_repository *repo)
6288 const struct got_error *err = NULL;
6289 struct got_object_qid *qid;
6290 struct got_histedit_list_entry *hle;
6291 static char msg[92];
6292 char *id_str;
6294 if (TAILQ_EMPTY(histedit_cmds))
6295 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6296 "histedit script contains no commands");
6297 if (SIMPLEQ_EMPTY(commits))
6298 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6300 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6301 struct got_histedit_list_entry *hle2;
6302 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6303 if (hle == hle2)
6304 continue;
6305 if (got_object_id_cmp(hle->commit_id,
6306 hle2->commit_id) != 0)
6307 continue;
6308 err = got_object_id_str(&id_str, hle->commit_id);
6309 if (err)
6310 return err;
6311 snprintf(msg, sizeof(msg), "commit %s is listed "
6312 "more than once in histedit script", id_str);
6313 free(id_str);
6314 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6318 SIMPLEQ_FOREACH(qid, commits, entry) {
6319 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6320 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6321 break;
6323 if (hle == NULL) {
6324 err = got_object_id_str(&id_str, qid->id);
6325 if (err)
6326 return err;
6327 snprintf(msg, sizeof(msg),
6328 "commit %s missing from histedit script", id_str);
6329 free(id_str);
6330 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6334 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6335 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6336 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6337 "last commit in histedit script cannot be folded");
6339 return NULL;
6342 static const struct got_error *
6343 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6344 const char *path, struct got_object_id_queue *commits,
6345 struct got_repository *repo)
6347 const struct got_error *err = NULL;
6348 char *editor;
6349 FILE *f = NULL;
6351 err = get_editor(&editor);
6352 if (err)
6353 return err;
6355 if (spawn_editor(editor, path) == -1) {
6356 err = got_error_from_errno("failed spawning editor");
6357 goto done;
6360 f = fopen(path, "r");
6361 if (f == NULL) {
6362 err = got_error_from_errno("fopen");
6363 goto done;
6365 err = histedit_parse_list(histedit_cmds, f, repo);
6366 if (err)
6367 goto done;
6369 err = histedit_check_script(histedit_cmds, commits, repo);
6370 done:
6371 if (f && fclose(f) != 0 && err == NULL)
6372 err = got_error_from_errno("fclose");
6373 free(editor);
6374 return err;
6377 static const struct got_error *
6378 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6379 struct got_object_id_queue *, const char *, const char *,
6380 struct got_repository *);
6382 static const struct got_error *
6383 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6384 struct got_object_id_queue *commits, const char *branch_name,
6385 int edit_logmsg_only, struct got_repository *repo)
6387 const struct got_error *err;
6388 FILE *f = NULL;
6389 char *path = NULL;
6391 err = got_opentemp_named(&path, &f, "got-histedit");
6392 if (err)
6393 return err;
6395 err = write_cmd_list(f, branch_name, commits);
6396 if (err)
6397 goto done;
6399 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6400 if (err)
6401 goto done;
6403 if (edit_logmsg_only) {
6404 rewind(f);
6405 err = histedit_parse_list(histedit_cmds, f, repo);
6406 } else {
6407 if (fclose(f) != 0) {
6408 err = got_error_from_errno("fclose");
6409 goto done;
6411 f = NULL;
6412 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6413 if (err) {
6414 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6415 err->code != GOT_ERR_HISTEDIT_CMD)
6416 goto done;
6417 err = histedit_edit_list_retry(histedit_cmds, err,
6418 commits, path, branch_name, repo);
6421 done:
6422 if (f && fclose(f) != 0 && err == NULL)
6423 err = got_error_from_errno("fclose");
6424 if (path && unlink(path) != 0 && err == NULL)
6425 err = got_error_from_errno2("unlink", path);
6426 free(path);
6427 return err;
6430 static const struct got_error *
6431 histedit_save_list(struct got_histedit_list *histedit_cmds,
6432 struct got_worktree *worktree, struct got_repository *repo)
6434 const struct got_error *err = NULL;
6435 char *path = NULL;
6436 FILE *f = NULL;
6437 struct got_histedit_list_entry *hle;
6438 struct got_commit_object *commit = NULL;
6440 err = got_worktree_get_histedit_script_path(&path, worktree);
6441 if (err)
6442 return err;
6444 f = fopen(path, "w");
6445 if (f == NULL) {
6446 err = got_error_from_errno2("fopen", path);
6447 goto done;
6449 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6450 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6451 repo);
6452 if (err)
6453 break;
6455 if (hle->logmsg) {
6456 int n = fprintf(f, "%c %s\n",
6457 GOT_HISTEDIT_MESG, hle->logmsg);
6458 if (n < 0) {
6459 err = got_ferror(f, GOT_ERR_IO);
6460 break;
6464 done:
6465 if (f && fclose(f) != 0 && err == NULL)
6466 err = got_error_from_errno("fclose");
6467 free(path);
6468 if (commit)
6469 got_object_commit_close(commit);
6470 return err;
6473 void
6474 histedit_free_list(struct got_histedit_list *histedit_cmds)
6476 struct got_histedit_list_entry *hle;
6478 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6479 TAILQ_REMOVE(histedit_cmds, hle, entry);
6480 free(hle);
6484 static const struct got_error *
6485 histedit_load_list(struct got_histedit_list *histedit_cmds,
6486 const char *path, struct got_repository *repo)
6488 const struct got_error *err = NULL;
6489 FILE *f = NULL;
6491 f = fopen(path, "r");
6492 if (f == NULL) {
6493 err = got_error_from_errno2("fopen", path);
6494 goto done;
6497 err = histedit_parse_list(histedit_cmds, f, repo);
6498 done:
6499 if (f && fclose(f) != 0 && err == NULL)
6500 err = got_error_from_errno("fclose");
6501 return err;
6504 static const struct got_error *
6505 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6506 const struct got_error *edit_err, struct got_object_id_queue *commits,
6507 const char *path, const char *branch_name, struct got_repository *repo)
6509 const struct got_error *err = NULL, *prev_err = edit_err;
6510 int resp = ' ';
6512 while (resp != 'c' && resp != 'r' && resp != 'a') {
6513 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6514 "or (a)bort: ", getprogname(), prev_err->msg);
6515 resp = getchar();
6516 if (resp == '\n')
6517 resp = getchar();
6518 if (resp == 'c') {
6519 histedit_free_list(histedit_cmds);
6520 err = histedit_run_editor(histedit_cmds, path, commits,
6521 repo);
6522 if (err) {
6523 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6524 err->code != GOT_ERR_HISTEDIT_CMD)
6525 break;
6526 prev_err = err;
6527 resp = ' ';
6528 continue;
6530 break;
6531 } else if (resp == 'r') {
6532 histedit_free_list(histedit_cmds);
6533 err = histedit_edit_script(histedit_cmds,
6534 commits, branch_name, 0, repo);
6535 if (err) {
6536 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6537 err->code != GOT_ERR_HISTEDIT_CMD)
6538 break;
6539 prev_err = err;
6540 resp = ' ';
6541 continue;
6543 break;
6544 } else if (resp == 'a') {
6545 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6546 break;
6547 } else
6548 printf("invalid response '%c'\n", resp);
6551 return err;
6554 static const struct got_error *
6555 histedit_complete(struct got_worktree *worktree,
6556 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6557 struct got_reference *branch, struct got_repository *repo)
6559 printf("Switching work tree to %s\n",
6560 got_ref_get_symref_target(branch));
6561 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6562 branch, repo);
6565 static const struct got_error *
6566 show_histedit_progress(struct got_commit_object *commit,
6567 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6569 const struct got_error *err;
6570 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6572 err = got_object_id_str(&old_id_str, hle->commit_id);
6573 if (err)
6574 goto done;
6576 if (new_id) {
6577 err = got_object_id_str(&new_id_str, new_id);
6578 if (err)
6579 goto done;
6582 old_id_str[12] = '\0';
6583 if (new_id_str)
6584 new_id_str[12] = '\0';
6586 if (hle->logmsg) {
6587 logmsg = strdup(hle->logmsg);
6588 if (logmsg == NULL) {
6589 err = got_error_from_errno("strdup");
6590 goto done;
6592 trim_logmsg(logmsg, 42);
6593 } else {
6594 err = get_short_logmsg(&logmsg, 42, commit);
6595 if (err)
6596 goto done;
6599 switch (hle->cmd->code) {
6600 case GOT_HISTEDIT_PICK:
6601 case GOT_HISTEDIT_EDIT:
6602 printf("%s -> %s: %s\n", old_id_str,
6603 new_id_str ? new_id_str : "no-op change", logmsg);
6604 break;
6605 case GOT_HISTEDIT_DROP:
6606 case GOT_HISTEDIT_FOLD:
6607 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6608 logmsg);
6609 break;
6610 default:
6611 break;
6613 done:
6614 free(old_id_str);
6615 free(new_id_str);
6616 return err;
6619 static const struct got_error *
6620 histedit_commit(struct got_pathlist_head *merged_paths,
6621 struct got_worktree *worktree, struct got_fileindex *fileindex,
6622 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6623 struct got_repository *repo)
6625 const struct got_error *err;
6626 struct got_commit_object *commit;
6627 struct got_object_id *new_commit_id;
6629 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6630 && hle->logmsg == NULL) {
6631 err = histedit_edit_logmsg(hle, repo);
6632 if (err)
6633 return err;
6636 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6637 if (err)
6638 return err;
6640 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6641 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6642 hle->logmsg, repo);
6643 if (err) {
6644 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6645 goto done;
6646 err = show_histedit_progress(commit, hle, NULL);
6647 } else {
6648 err = show_histedit_progress(commit, hle, new_commit_id);
6649 free(new_commit_id);
6651 done:
6652 got_object_commit_close(commit);
6653 return err;
6656 static const struct got_error *
6657 histedit_skip_commit(struct got_histedit_list_entry *hle,
6658 struct got_worktree *worktree, struct got_repository *repo)
6660 const struct got_error *error;
6661 struct got_commit_object *commit;
6663 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6664 repo);
6665 if (error)
6666 return error;
6668 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6669 if (error)
6670 return error;
6672 error = show_histedit_progress(commit, hle, NULL);
6673 got_object_commit_close(commit);
6674 return error;
6677 static const struct got_error *
6678 check_local_changes(void *arg, unsigned char status,
6679 unsigned char staged_status, const char *path,
6680 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6681 struct got_object_id *commit_id, int dirfd, const char *de_name)
6683 int *have_local_changes = arg;
6685 switch (status) {
6686 case GOT_STATUS_ADD:
6687 case GOT_STATUS_DELETE:
6688 case GOT_STATUS_MODIFY:
6689 case GOT_STATUS_CONFLICT:
6690 *have_local_changes = 1;
6691 return got_error(GOT_ERR_CANCELLED);
6692 default:
6693 break;
6696 switch (staged_status) {
6697 case GOT_STATUS_ADD:
6698 case GOT_STATUS_DELETE:
6699 case GOT_STATUS_MODIFY:
6700 *have_local_changes = 1;
6701 return got_error(GOT_ERR_CANCELLED);
6702 default:
6703 break;
6706 return NULL;
6709 static const struct got_error *
6710 cmd_histedit(int argc, char *argv[])
6712 const struct got_error *error = NULL;
6713 struct got_worktree *worktree = NULL;
6714 struct got_fileindex *fileindex = NULL;
6715 struct got_repository *repo = NULL;
6716 char *cwd = NULL;
6717 struct got_reference *branch = NULL;
6718 struct got_reference *tmp_branch = NULL;
6719 struct got_object_id *resume_commit_id = NULL;
6720 struct got_object_id *base_commit_id = NULL;
6721 struct got_object_id *head_commit_id = NULL;
6722 struct got_commit_object *commit = NULL;
6723 int ch, rebase_in_progress = 0, did_something;
6724 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6725 int edit_logmsg_only = 0;
6726 const char *edit_script_path = NULL;
6727 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6728 struct got_object_id_queue commits;
6729 struct got_pathlist_head merged_paths;
6730 const struct got_object_id_queue *parent_ids;
6731 struct got_object_qid *pid;
6732 struct got_histedit_list histedit_cmds;
6733 struct got_histedit_list_entry *hle;
6735 SIMPLEQ_INIT(&commits);
6736 TAILQ_INIT(&histedit_cmds);
6737 TAILQ_INIT(&merged_paths);
6739 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6740 switch (ch) {
6741 case 'a':
6742 abort_edit = 1;
6743 break;
6744 case 'c':
6745 continue_edit = 1;
6746 break;
6747 case 'F':
6748 edit_script_path = optarg;
6749 break;
6750 case 'm':
6751 edit_logmsg_only = 1;
6752 break;
6753 default:
6754 usage_histedit();
6755 /* NOTREACHED */
6759 argc -= optind;
6760 argv += optind;
6762 #ifndef PROFILE
6763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6764 "unveil", NULL) == -1)
6765 err(1, "pledge");
6766 #endif
6767 if (abort_edit && continue_edit)
6768 errx(1, "histedit's -a and -c options are mutually exclusive");
6769 if (edit_script_path && edit_logmsg_only)
6770 errx(1, "histedit's -F and -m options are mutually exclusive");
6771 if (abort_edit && edit_logmsg_only)
6772 errx(1, "histedit's -a and -m options are mutually exclusive");
6773 if (continue_edit && edit_logmsg_only)
6774 errx(1, "histedit's -c and -m options are mutually exclusive");
6775 if (argc != 0)
6776 usage_histedit();
6779 * This command cannot apply unveil(2) in all cases because the
6780 * user may choose to run an editor to edit the histedit script
6781 * and to edit individual commit log messages.
6782 * unveil(2) traverses exec(2); if an editor is used we have to
6783 * apply unveil after edit script and log messages have been written.
6784 * XXX TODO: Make use of unveil(2) where possible.
6787 cwd = getcwd(NULL, 0);
6788 if (cwd == NULL) {
6789 error = got_error_from_errno("getcwd");
6790 goto done;
6792 error = got_worktree_open(&worktree, cwd);
6793 if (error)
6794 goto done;
6796 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6797 NULL);
6798 if (error != NULL)
6799 goto done;
6801 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6802 if (error)
6803 goto done;
6804 if (rebase_in_progress) {
6805 error = got_error(GOT_ERR_REBASING);
6806 goto done;
6809 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6810 if (error)
6811 goto done;
6813 if (edit_in_progress && edit_logmsg_only) {
6814 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6815 "histedit operation is in progress in this "
6816 "work tree and must be continued or aborted "
6817 "before the -m option can be used");
6818 goto done;
6821 if (edit_in_progress && abort_edit) {
6822 error = got_worktree_histedit_continue(&resume_commit_id,
6823 &tmp_branch, &branch, &base_commit_id, &fileindex,
6824 worktree, repo);
6825 if (error)
6826 goto done;
6827 printf("Switching work tree to %s\n",
6828 got_ref_get_symref_target(branch));
6829 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6830 branch, base_commit_id, update_progress, &did_something);
6831 if (error)
6832 goto done;
6833 printf("Histedit of %s aborted\n",
6834 got_ref_get_symref_target(branch));
6835 goto done; /* nothing else to do */
6836 } else if (abort_edit) {
6837 error = got_error(GOT_ERR_NOT_HISTEDIT);
6838 goto done;
6841 if (continue_edit) {
6842 char *path;
6844 if (!edit_in_progress) {
6845 error = got_error(GOT_ERR_NOT_HISTEDIT);
6846 goto done;
6849 error = got_worktree_get_histedit_script_path(&path, worktree);
6850 if (error)
6851 goto done;
6853 error = histedit_load_list(&histedit_cmds, path, repo);
6854 free(path);
6855 if (error)
6856 goto done;
6858 error = got_worktree_histedit_continue(&resume_commit_id,
6859 &tmp_branch, &branch, &base_commit_id, &fileindex,
6860 worktree, repo);
6861 if (error)
6862 goto done;
6864 error = got_ref_resolve(&head_commit_id, repo, branch);
6865 if (error)
6866 goto done;
6868 error = got_object_open_as_commit(&commit, repo,
6869 head_commit_id);
6870 if (error)
6871 goto done;
6872 parent_ids = got_object_commit_get_parent_ids(commit);
6873 pid = SIMPLEQ_FIRST(parent_ids);
6874 if (pid == NULL) {
6875 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6876 goto done;
6878 error = collect_commits(&commits, head_commit_id, pid->id,
6879 base_commit_id, got_worktree_get_path_prefix(worktree),
6880 GOT_ERR_HISTEDIT_PATH, repo);
6881 got_object_commit_close(commit);
6882 commit = NULL;
6883 if (error)
6884 goto done;
6885 } else {
6886 if (edit_in_progress) {
6887 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6888 goto done;
6891 error = got_ref_open(&branch, repo,
6892 got_worktree_get_head_ref_name(worktree), 0);
6893 if (error != NULL)
6894 goto done;
6896 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6897 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6898 "will not edit commit history of a branch outside "
6899 "the \"refs/heads/\" reference namespace");
6900 goto done;
6903 error = got_ref_resolve(&head_commit_id, repo, branch);
6904 got_ref_close(branch);
6905 branch = NULL;
6906 if (error)
6907 goto done;
6909 error = got_object_open_as_commit(&commit, repo,
6910 head_commit_id);
6911 if (error)
6912 goto done;
6913 parent_ids = got_object_commit_get_parent_ids(commit);
6914 pid = SIMPLEQ_FIRST(parent_ids);
6915 if (pid == NULL) {
6916 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6917 goto done;
6919 error = collect_commits(&commits, head_commit_id, pid->id,
6920 got_worktree_get_base_commit_id(worktree),
6921 got_worktree_get_path_prefix(worktree),
6922 GOT_ERR_HISTEDIT_PATH, repo);
6923 got_object_commit_close(commit);
6924 commit = NULL;
6925 if (error)
6926 goto done;
6928 if (SIMPLEQ_EMPTY(&commits)) {
6929 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6930 goto done;
6933 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6934 &base_commit_id, &fileindex, worktree, repo);
6935 if (error)
6936 goto done;
6938 if (edit_script_path) {
6939 error = histedit_load_list(&histedit_cmds,
6940 edit_script_path, repo);
6941 if (error) {
6942 got_worktree_histedit_abort(worktree, fileindex,
6943 repo, branch, base_commit_id,
6944 update_progress, &did_something);
6945 goto done;
6947 } else {
6948 const char *branch_name;
6949 branch_name = got_ref_get_symref_target(branch);
6950 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6951 branch_name += 11;
6952 error = histedit_edit_script(&histedit_cmds, &commits,
6953 branch_name, edit_logmsg_only, repo);
6954 if (error) {
6955 got_worktree_histedit_abort(worktree, fileindex,
6956 repo, branch, base_commit_id,
6957 update_progress, &did_something);
6958 goto done;
6963 error = histedit_save_list(&histedit_cmds, worktree,
6964 repo);
6965 if (error) {
6966 got_worktree_histedit_abort(worktree, fileindex,
6967 repo, branch, base_commit_id,
6968 update_progress, &did_something);
6969 goto done;
6974 error = histedit_check_script(&histedit_cmds, &commits, repo);
6975 if (error)
6976 goto done;
6978 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6979 if (resume_commit_id) {
6980 if (got_object_id_cmp(hle->commit_id,
6981 resume_commit_id) != 0)
6982 continue;
6984 resume_commit_id = NULL;
6985 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6986 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6987 error = histedit_skip_commit(hle, worktree,
6988 repo);
6989 if (error)
6990 goto done;
6991 } else {
6992 struct got_pathlist_head paths;
6993 int have_changes = 0;
6995 TAILQ_INIT(&paths);
6996 error = got_pathlist_append(&paths, "", NULL);
6997 if (error)
6998 goto done;
6999 error = got_worktree_status(worktree, &paths,
7000 repo, check_local_changes, &have_changes,
7001 check_cancelled, NULL);
7002 got_pathlist_free(&paths);
7003 if (error) {
7004 if (error->code != GOT_ERR_CANCELLED)
7005 goto done;
7006 if (sigint_received || sigpipe_received)
7007 goto done;
7009 if (have_changes) {
7010 error = histedit_commit(NULL, worktree,
7011 fileindex, tmp_branch, hle, repo);
7012 if (error)
7013 goto done;
7014 } else {
7015 error = got_object_open_as_commit(
7016 &commit, repo, hle->commit_id);
7017 if (error)
7018 goto done;
7019 error = show_histedit_progress(commit,
7020 hle, NULL);
7021 got_object_commit_close(commit);
7022 commit = NULL;
7023 if (error)
7024 goto done;
7027 continue;
7030 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7031 error = histedit_skip_commit(hle, worktree, repo);
7032 if (error)
7033 goto done;
7034 continue;
7037 error = got_object_open_as_commit(&commit, repo,
7038 hle->commit_id);
7039 if (error)
7040 goto done;
7041 parent_ids = got_object_commit_get_parent_ids(commit);
7042 pid = SIMPLEQ_FIRST(parent_ids);
7044 error = got_worktree_histedit_merge_files(&merged_paths,
7045 worktree, fileindex, pid->id, hle->commit_id, repo,
7046 rebase_progress, &rebase_status, check_cancelled, NULL);
7047 if (error)
7048 goto done;
7049 got_object_commit_close(commit);
7050 commit = NULL;
7052 if (rebase_status == GOT_STATUS_CONFLICT) {
7053 error = show_rebase_merge_conflict(hle->commit_id,
7054 repo);
7055 if (error)
7056 goto done;
7057 got_worktree_rebase_pathlist_free(&merged_paths);
7058 break;
7061 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7062 char *id_str;
7063 error = got_object_id_str(&id_str, hle->commit_id);
7064 if (error)
7065 goto done;
7066 printf("Stopping histedit for amending commit %s\n",
7067 id_str);
7068 free(id_str);
7069 got_worktree_rebase_pathlist_free(&merged_paths);
7070 error = got_worktree_histedit_postpone(worktree,
7071 fileindex);
7072 goto done;
7075 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7076 error = histedit_skip_commit(hle, worktree, repo);
7077 if (error)
7078 goto done;
7079 continue;
7082 error = histedit_commit(&merged_paths, worktree, fileindex,
7083 tmp_branch, hle, repo);
7084 got_worktree_rebase_pathlist_free(&merged_paths);
7085 if (error)
7086 goto done;
7089 if (rebase_status == GOT_STATUS_CONFLICT) {
7090 error = got_worktree_histedit_postpone(worktree, fileindex);
7091 if (error)
7092 goto done;
7093 error = got_error_msg(GOT_ERR_CONFLICTS,
7094 "conflicts must be resolved before histedit can continue");
7095 } else
7096 error = histedit_complete(worktree, fileindex, tmp_branch,
7097 branch, repo);
7098 done:
7099 got_object_id_queue_free(&commits);
7100 histedit_free_list(&histedit_cmds);
7101 free(head_commit_id);
7102 free(base_commit_id);
7103 free(resume_commit_id);
7104 if (commit)
7105 got_object_commit_close(commit);
7106 if (branch)
7107 got_ref_close(branch);
7108 if (tmp_branch)
7109 got_ref_close(tmp_branch);
7110 if (worktree)
7111 got_worktree_close(worktree);
7112 if (repo)
7113 got_repo_close(repo);
7114 return error;
7117 __dead static void
7118 usage_integrate(void)
7120 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7121 exit(1);
7124 static const struct got_error *
7125 cmd_integrate(int argc, char *argv[])
7127 const struct got_error *error = NULL;
7128 struct got_repository *repo = NULL;
7129 struct got_worktree *worktree = NULL;
7130 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7131 const char *branch_arg = NULL;
7132 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7133 struct got_fileindex *fileindex = NULL;
7134 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7135 int ch, did_something = 0;
7137 while ((ch = getopt(argc, argv, "")) != -1) {
7138 switch (ch) {
7139 default:
7140 usage_integrate();
7141 /* NOTREACHED */
7145 argc -= optind;
7146 argv += optind;
7148 if (argc != 1)
7149 usage_integrate();
7150 branch_arg = argv[0];
7152 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7153 "unveil", NULL) == -1)
7154 err(1, "pledge");
7156 cwd = getcwd(NULL, 0);
7157 if (cwd == NULL) {
7158 error = got_error_from_errno("getcwd");
7159 goto done;
7162 error = got_worktree_open(&worktree, cwd);
7163 if (error)
7164 goto done;
7166 error = check_rebase_or_histedit_in_progress(worktree);
7167 if (error)
7168 goto done;
7170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7171 NULL);
7172 if (error != NULL)
7173 goto done;
7175 error = apply_unveil(got_repo_get_path(repo), 0,
7176 got_worktree_get_root_path(worktree));
7177 if (error)
7178 goto done;
7180 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7181 error = got_error_from_errno("asprintf");
7182 goto done;
7185 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7186 &base_branch_ref, worktree, refname, repo);
7187 if (error)
7188 goto done;
7190 refname = strdup(got_ref_get_name(branch_ref));
7191 if (refname == NULL) {
7192 error = got_error_from_errno("strdup");
7193 got_worktree_integrate_abort(worktree, fileindex, repo,
7194 branch_ref, base_branch_ref);
7195 goto done;
7197 base_refname = strdup(got_ref_get_name(base_branch_ref));
7198 if (base_refname == NULL) {
7199 error = got_error_from_errno("strdup");
7200 got_worktree_integrate_abort(worktree, fileindex, repo,
7201 branch_ref, base_branch_ref);
7202 goto done;
7205 error = got_ref_resolve(&commit_id, repo, branch_ref);
7206 if (error)
7207 goto done;
7209 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7210 if (error)
7211 goto done;
7213 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7214 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7215 "specified branch has already been integrated");
7216 got_worktree_integrate_abort(worktree, fileindex, repo,
7217 branch_ref, base_branch_ref);
7218 goto done;
7221 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7222 if (error) {
7223 if (error->code == GOT_ERR_ANCESTRY)
7224 error = got_error(GOT_ERR_REBASE_REQUIRED);
7225 got_worktree_integrate_abort(worktree, fileindex, repo,
7226 branch_ref, base_branch_ref);
7227 goto done;
7230 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7231 branch_ref, base_branch_ref, update_progress, &did_something,
7232 check_cancelled, NULL);
7233 if (error)
7234 goto done;
7236 printf("Integrated %s into %s\n", refname, base_refname);
7237 done:
7238 if (repo)
7239 got_repo_close(repo);
7240 if (worktree)
7241 got_worktree_close(worktree);
7242 free(cwd);
7243 free(base_commit_id);
7244 free(commit_id);
7245 free(refname);
7246 free(base_refname);
7247 return error;
7250 __dead static void
7251 usage_stage(void)
7253 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7254 "[file-path ...]\n",
7255 getprogname());
7256 exit(1);
7259 static const struct got_error *
7260 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7261 const char *path, struct got_object_id *blob_id,
7262 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7263 int dirfd, const char *de_name)
7265 const struct got_error *err = NULL;
7266 char *id_str = NULL;
7268 if (staged_status != GOT_STATUS_ADD &&
7269 staged_status != GOT_STATUS_MODIFY &&
7270 staged_status != GOT_STATUS_DELETE)
7271 return NULL;
7273 if (staged_status == GOT_STATUS_ADD ||
7274 staged_status == GOT_STATUS_MODIFY)
7275 err = got_object_id_str(&id_str, staged_blob_id);
7276 else
7277 err = got_object_id_str(&id_str, blob_id);
7278 if (err)
7279 return err;
7281 printf("%s %c %s\n", id_str, staged_status, path);
7282 free(id_str);
7283 return NULL;
7286 static const struct got_error *
7287 cmd_stage(int argc, char *argv[])
7289 const struct got_error *error = NULL;
7290 struct got_repository *repo = NULL;
7291 struct got_worktree *worktree = NULL;
7292 char *cwd = NULL;
7293 struct got_pathlist_head paths;
7294 struct got_pathlist_entry *pe;
7295 int ch, list_stage = 0, pflag = 0;
7296 FILE *patch_script_file = NULL;
7297 const char *patch_script_path = NULL;
7298 struct choose_patch_arg cpa;
7300 TAILQ_INIT(&paths);
7302 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7303 switch (ch) {
7304 case 'l':
7305 list_stage = 1;
7306 break;
7307 case 'p':
7308 pflag = 1;
7309 break;
7310 case 'F':
7311 patch_script_path = optarg;
7312 break;
7313 default:
7314 usage_stage();
7315 /* NOTREACHED */
7319 argc -= optind;
7320 argv += optind;
7322 #ifndef PROFILE
7323 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7324 "unveil", NULL) == -1)
7325 err(1, "pledge");
7326 #endif
7327 if (list_stage && (pflag || patch_script_path))
7328 errx(1, "-l option cannot be used with other options");
7329 if (patch_script_path && !pflag)
7330 errx(1, "-F option can only be used together with -p option");
7332 cwd = getcwd(NULL, 0);
7333 if (cwd == NULL) {
7334 error = got_error_from_errno("getcwd");
7335 goto done;
7338 error = got_worktree_open(&worktree, cwd);
7339 if (error)
7340 goto done;
7342 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7343 NULL);
7344 if (error != NULL)
7345 goto done;
7347 if (patch_script_path) {
7348 patch_script_file = fopen(patch_script_path, "r");
7349 if (patch_script_file == NULL) {
7350 error = got_error_from_errno2("fopen",
7351 patch_script_path);
7352 goto done;
7355 error = apply_unveil(got_repo_get_path(repo), 0,
7356 got_worktree_get_root_path(worktree));
7357 if (error)
7358 goto done;
7360 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7361 if (error)
7362 goto done;
7364 if (list_stage)
7365 error = got_worktree_status(worktree, &paths, repo,
7366 print_stage, NULL, check_cancelled, NULL);
7367 else {
7368 cpa.patch_script_file = patch_script_file;
7369 cpa.action = "stage";
7370 error = got_worktree_stage(worktree, &paths,
7371 pflag ? NULL : print_status, NULL,
7372 pflag ? choose_patch : NULL, &cpa, repo);
7374 done:
7375 if (patch_script_file && fclose(patch_script_file) == EOF &&
7376 error == NULL)
7377 error = got_error_from_errno2("fclose", patch_script_path);
7378 if (repo)
7379 got_repo_close(repo);
7380 if (worktree)
7381 got_worktree_close(worktree);
7382 TAILQ_FOREACH(pe, &paths, entry)
7383 free((char *)pe->path);
7384 got_pathlist_free(&paths);
7385 free(cwd);
7386 return error;
7389 __dead static void
7390 usage_unstage(void)
7392 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7393 "[file-path ...]\n",
7394 getprogname());
7395 exit(1);
7399 static const struct got_error *
7400 cmd_unstage(int argc, char *argv[])
7402 const struct got_error *error = NULL;
7403 struct got_repository *repo = NULL;
7404 struct got_worktree *worktree = NULL;
7405 char *cwd = NULL;
7406 struct got_pathlist_head paths;
7407 struct got_pathlist_entry *pe;
7408 int ch, did_something = 0, pflag = 0;
7409 FILE *patch_script_file = NULL;
7410 const char *patch_script_path = NULL;
7411 struct choose_patch_arg cpa;
7413 TAILQ_INIT(&paths);
7415 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7416 switch (ch) {
7417 case 'p':
7418 pflag = 1;
7419 break;
7420 case 'F':
7421 patch_script_path = optarg;
7422 break;
7423 default:
7424 usage_unstage();
7425 /* NOTREACHED */
7429 argc -= optind;
7430 argv += optind;
7432 #ifndef PROFILE
7433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7434 "unveil", NULL) == -1)
7435 err(1, "pledge");
7436 #endif
7437 if (patch_script_path && !pflag)
7438 errx(1, "-F option can only be used together with -p option");
7440 cwd = getcwd(NULL, 0);
7441 if (cwd == NULL) {
7442 error = got_error_from_errno("getcwd");
7443 goto done;
7446 error = got_worktree_open(&worktree, cwd);
7447 if (error)
7448 goto done;
7450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7451 NULL);
7452 if (error != NULL)
7453 goto done;
7455 if (patch_script_path) {
7456 patch_script_file = fopen(patch_script_path, "r");
7457 if (patch_script_file == NULL) {
7458 error = got_error_from_errno2("fopen",
7459 patch_script_path);
7460 goto done;
7464 error = apply_unveil(got_repo_get_path(repo), 0,
7465 got_worktree_get_root_path(worktree));
7466 if (error)
7467 goto done;
7469 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7470 if (error)
7471 goto done;
7473 cpa.patch_script_file = patch_script_file;
7474 cpa.action = "unstage";
7475 error = got_worktree_unstage(worktree, &paths, update_progress,
7476 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7477 done:
7478 if (patch_script_file && fclose(patch_script_file) == EOF &&
7479 error == NULL)
7480 error = got_error_from_errno2("fclose", patch_script_path);
7481 if (repo)
7482 got_repo_close(repo);
7483 if (worktree)
7484 got_worktree_close(worktree);
7485 TAILQ_FOREACH(pe, &paths, entry)
7486 free((char *)pe->path);
7487 got_pathlist_free(&paths);
7488 free(cwd);
7489 return error;
7492 __dead static void
7493 usage_cat(void)
7495 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7496 "arg1 [arg2 ...]\n", getprogname());
7497 exit(1);
7500 static const struct got_error *
7501 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7503 const struct got_error *err;
7504 struct got_blob_object *blob;
7506 err = got_object_open_as_blob(&blob, repo, id, 8192);
7507 if (err)
7508 return err;
7510 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7511 got_object_blob_close(blob);
7512 return err;
7515 static const struct got_error *
7516 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7518 const struct got_error *err;
7519 struct got_tree_object *tree;
7520 int nentries, i;
7522 err = got_object_open_as_tree(&tree, repo, id);
7523 if (err)
7524 return err;
7526 nentries = got_object_tree_get_nentries(tree);
7527 for (i = 0; i < nentries; i++) {
7528 struct got_tree_entry *te;
7529 char *id_str;
7530 if (sigint_received || sigpipe_received)
7531 break;
7532 te = got_object_tree_get_entry(tree, i);
7533 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7534 if (err)
7535 break;
7536 fprintf(outfile, "%s %.7o %s\n", id_str,
7537 got_tree_entry_get_mode(te),
7538 got_tree_entry_get_name(te));
7539 free(id_str);
7542 got_object_tree_close(tree);
7543 return err;
7546 static const struct got_error *
7547 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7549 const struct got_error *err;
7550 struct got_commit_object *commit;
7551 const struct got_object_id_queue *parent_ids;
7552 struct got_object_qid *pid;
7553 char *id_str = NULL;
7554 const char *logmsg = NULL;
7556 err = got_object_open_as_commit(&commit, repo, id);
7557 if (err)
7558 return err;
7560 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7561 if (err)
7562 goto done;
7564 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7565 parent_ids = got_object_commit_get_parent_ids(commit);
7566 fprintf(outfile, "numparents %d\n",
7567 got_object_commit_get_nparents(commit));
7568 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7569 char *pid_str;
7570 err = got_object_id_str(&pid_str, pid->id);
7571 if (err)
7572 goto done;
7573 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7574 free(pid_str);
7576 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7577 got_object_commit_get_author(commit),
7578 got_object_commit_get_author_time(commit));
7580 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7581 got_object_commit_get_author(commit),
7582 got_object_commit_get_committer_time(commit));
7584 logmsg = got_object_commit_get_logmsg_raw(commit);
7585 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7586 fprintf(outfile, "%s", logmsg);
7587 done:
7588 free(id_str);
7589 got_object_commit_close(commit);
7590 return err;
7593 static const struct got_error *
7594 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7596 const struct got_error *err;
7597 struct got_tag_object *tag;
7598 char *id_str = NULL;
7599 const char *tagmsg = NULL;
7601 err = got_object_open_as_tag(&tag, repo, id);
7602 if (err)
7603 return err;
7605 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7606 if (err)
7607 goto done;
7609 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7611 switch (got_object_tag_get_object_type(tag)) {
7612 case GOT_OBJ_TYPE_BLOB:
7613 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7614 GOT_OBJ_LABEL_BLOB);
7615 break;
7616 case GOT_OBJ_TYPE_TREE:
7617 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7618 GOT_OBJ_LABEL_TREE);
7619 break;
7620 case GOT_OBJ_TYPE_COMMIT:
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7622 GOT_OBJ_LABEL_COMMIT);
7623 break;
7624 case GOT_OBJ_TYPE_TAG:
7625 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7626 GOT_OBJ_LABEL_TAG);
7627 break;
7628 default:
7629 break;
7632 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7633 got_object_tag_get_name(tag));
7635 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7636 got_object_tag_get_tagger(tag),
7637 got_object_tag_get_tagger_time(tag));
7639 tagmsg = got_object_tag_get_message(tag);
7640 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7641 fprintf(outfile, "%s", tagmsg);
7642 done:
7643 free(id_str);
7644 got_object_tag_close(tag);
7645 return err;
7648 static const struct got_error *
7649 cmd_cat(int argc, char *argv[])
7651 const struct got_error *error;
7652 struct got_repository *repo = NULL;
7653 struct got_worktree *worktree = NULL;
7654 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7655 const char *commit_id_str = NULL;
7656 struct got_object_id *id = NULL, *commit_id = NULL;
7657 int ch, obj_type, i, force_path = 0;
7659 #ifndef PROFILE
7660 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7661 NULL) == -1)
7662 err(1, "pledge");
7663 #endif
7665 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7666 switch (ch) {
7667 case 'c':
7668 commit_id_str = optarg;
7669 break;
7670 case 'r':
7671 repo_path = realpath(optarg, NULL);
7672 if (repo_path == NULL)
7673 return got_error_from_errno2("realpath",
7674 optarg);
7675 got_path_strip_trailing_slashes(repo_path);
7676 break;
7677 case 'P':
7678 force_path = 1;
7679 break;
7680 default:
7681 usage_cat();
7682 /* NOTREACHED */
7686 argc -= optind;
7687 argv += optind;
7689 cwd = getcwd(NULL, 0);
7690 if (cwd == NULL) {
7691 error = got_error_from_errno("getcwd");
7692 goto done;
7694 error = got_worktree_open(&worktree, cwd);
7695 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7696 goto done;
7697 if (worktree) {
7698 if (repo_path == NULL) {
7699 repo_path = strdup(
7700 got_worktree_get_repo_path(worktree));
7701 if (repo_path == NULL) {
7702 error = got_error_from_errno("strdup");
7703 goto done;
7708 if (repo_path == NULL) {
7709 repo_path = getcwd(NULL, 0);
7710 if (repo_path == NULL)
7711 return got_error_from_errno("getcwd");
7714 error = got_repo_open(&repo, repo_path, NULL);
7715 free(repo_path);
7716 if (error != NULL)
7717 goto done;
7719 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7720 if (error)
7721 goto done;
7723 if (commit_id_str == NULL)
7724 commit_id_str = GOT_REF_HEAD;
7725 error = got_repo_match_object_id(&commit_id, NULL,
7726 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7727 if (error)
7728 goto done;
7730 for (i = 0; i < argc; i++) {
7731 if (force_path) {
7732 error = got_object_id_by_path(&id, repo, commit_id,
7733 argv[i]);
7734 if (error)
7735 break;
7736 } else {
7737 error = got_repo_match_object_id(&id, &label, argv[i],
7738 GOT_OBJ_TYPE_ANY, 0, repo);
7739 if (error) {
7740 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7741 error->code != GOT_ERR_NOT_REF)
7742 break;
7743 error = got_object_id_by_path(&id, repo,
7744 commit_id, argv[i]);
7745 if (error)
7746 break;
7750 error = got_object_get_type(&obj_type, repo, id);
7751 if (error)
7752 break;
7754 switch (obj_type) {
7755 case GOT_OBJ_TYPE_BLOB:
7756 error = cat_blob(id, repo, stdout);
7757 break;
7758 case GOT_OBJ_TYPE_TREE:
7759 error = cat_tree(id, repo, stdout);
7760 break;
7761 case GOT_OBJ_TYPE_COMMIT:
7762 error = cat_commit(id, repo, stdout);
7763 break;
7764 case GOT_OBJ_TYPE_TAG:
7765 error = cat_tag(id, repo, stdout);
7766 break;
7767 default:
7768 error = got_error(GOT_ERR_OBJ_TYPE);
7769 break;
7771 if (error)
7772 break;
7773 free(label);
7774 label = NULL;
7775 free(id);
7776 id = NULL;
7778 done:
7779 free(label);
7780 free(id);
7781 free(commit_id);
7782 if (worktree)
7783 got_worktree_close(worktree);
7784 if (repo) {
7785 const struct got_error *repo_error;
7786 repo_error = got_repo_close(repo);
7787 if (error == NULL)
7788 error = repo_error;
7790 return error;