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_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
167 };
169 static void
170 list_commands(void)
172 int i;
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
179 fputc('\n', stderr);
182 int
183 main(int argc, char *argv[])
185 struct got_cmd *cmd;
186 unsigned int i;
187 int ch;
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
191 { NULL, 0, NULL, 0}
192 };
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 switch (ch) {
198 case 'h':
199 hflag = 1;
200 break;
201 case 'V':
202 Vflag = 1;
203 break;
204 default:
205 usage(hflag);
206 /* NOTREACHED */
210 argc -= optind;
211 argv += optind;
212 optind = 0;
214 if (Vflag) {
215 got_version_print_str();
216 return 1;
219 if (argc <= 0)
220 usage(hflag);
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
232 continue;
234 if (hflag)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 !(sigint_received &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 return 1;
248 return 0;
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 list_commands();
253 return 1;
256 __dead static void
257 usage(int hflag)
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 getprogname());
261 if (hflag)
262 list_commands();
263 exit(1);
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
270 const char *editor;
272 *abspath = NULL;
274 editor = getenv("VISUAL");
275 if (editor == NULL)
276 editor = getenv("EDITOR");
278 if (editor) {
279 err = got_path_find_prog(abspath, editor);
280 if (err)
281 return err;
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
290 return NULL;
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
299 #ifdef PROFILE
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
302 #endif
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
313 if (err != NULL)
314 return err;
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
319 return NULL;
322 __dead static void
323 usage_init(void)
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 exit(1);
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
334 int ch;
336 while ((ch = getopt(argc, argv, "")) != -1) {
337 switch (ch) {
338 default:
339 usage_init();
340 /* NOTREACHED */
344 argc -= optind;
345 argv += optind;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 err(1, "pledge");
350 #endif
351 if (argc != 1)
352 usage_init();
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
361 if (error &&
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 goto done;
365 error = apply_unveil(repo_path, 0, NULL);
366 if (error)
367 goto done;
369 error = got_repo_init(repo_path);
370 done:
371 free(repo_path);
372 return error;
375 __dead static void
376 usage_import(void)
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
380 exit(1);
383 int
384 spawn_editor(const char *editor, const char *file)
386 pid_t pid;
387 sig_t sighup, sigint, sigquit;
388 int st = -1;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
395 case -1:
396 goto doneediting;
397 case 0:
398 execl(editor, editor, file, (char *)NULL);
399 _exit(127);
402 while (waitpid(pid, &st, 0) == -1)
403 if (errno != EINTR)
404 break;
406 doneediting:
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
412 errno = EINTR;
413 return -1;
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
424 char buf[1024];
425 struct stat st, st2;
426 FILE *fp;
427 int content_changed = 0;
428 size_t len;
430 *logmsg = NULL;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
446 if (*logmsg == NULL)
447 return got_error_from_errno("malloc");
448 (*logmsg)[0] = '\0';
449 len = 0;
451 fp = fopen(logmsg_path, "r");
452 if (fp == NULL) {
453 err = got_error_from_errno("fopen");
454 goto done;
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
458 content_changed = 1;
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
463 fclose(fp);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
467 len--;
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
473 done:
474 if (err) {
475 free(*logmsg);
476 *logmsg = NULL;
478 return err;
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
487 int fd;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
491 branch_name) == -1)
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
496 if (err)
497 goto done;
499 dprintf(fd, initial_content);
500 close(fd);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 done:
504 free(initial_content);
505 return err;
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
512 return NULL;
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
521 *author = NULL;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
525 if (name && email) {
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
528 return NULL;
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
535 if (name && email) {
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
538 return NULL;
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
545 if (*author == NULL)
546 return got_error_from_errno("strdup");
548 /*
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
551 */
552 while (*got_author && *got_author != '<')
553 got_author++;
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 goto done;
558 while (*got_author && *got_author != '@')
559 got_author++;
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 goto done;
564 while (*got_author && *got_author != '>')
565 got_author++;
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 done:
569 if (err) {
570 free(*author);
571 *author = NULL;
573 return err;
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
582 if (homedir) {
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
587 return NULL;
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
601 int ch;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 switch (ch) {
610 case 'b':
611 branch_name = optarg;
612 break;
613 case 'm':
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
617 goto done;
619 break;
620 case 'r':
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
624 optarg);
625 goto done;
627 break;
628 case 'I':
629 if (optarg[0] == '\0')
630 break;
631 error = got_pathlist_insert(&pe, &ignores, optarg,
632 NULL);
633 if (error)
634 goto done;
635 break;
636 default:
637 usage_import();
638 /* NOTREACHED */
642 argc -= optind;
643 argv += optind;
645 #ifndef PROFILE
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 "unveil",
648 NULL) == -1)
649 err(1, "pledge");
650 #endif
651 if (argc != 1)
652 usage_import();
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
661 if (error)
662 goto done;
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
664 if (error)
665 goto done;
667 error = get_author(&author, repo);
668 if (error)
669 return error;
671 /*
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
675 */
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
681 goto done;
684 error = got_ref_open(&branch_ref, repo, refname, 0);
685 if (error) {
686 if (error->code != GOT_ERR_NOT_REF)
687 goto done;
688 } else {
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
691 goto done;
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
697 goto done;
699 got_path_strip_trailing_slashes(path_dir);
701 /*
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
704 */
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
707 if (error)
708 goto done;
709 free(logmsg);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 path_dir, refname);
712 if (error) {
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 logmsg_path != NULL)
715 preserve_logmsg = 1;
716 goto done;
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 if (error) {
729 if (logmsg_path)
730 preserve_logmsg = 1;
731 goto done;
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_ref_write(branch_ref, repo);
750 if (error) {
751 if (logmsg_path)
752 preserve_logmsg = 1;
753 goto done;
756 error = got_object_id_str(&id_str, new_commit_id);
757 if (error) {
758 if (logmsg_path)
759 preserve_logmsg = 1;
760 goto done;
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 if (error) {
765 if (error->code != GOT_ERR_NOT_REF) {
766 if (logmsg_path)
767 preserve_logmsg = 1;
768 goto done;
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 branch_ref);
773 if (error) {
774 if (logmsg_path)
775 preserve_logmsg = 1;
776 goto done;
779 error = got_ref_write(head_ref, repo);
780 if (error) {
781 if (logmsg_path)
782 preserve_logmsg = 1;
783 goto done;
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
789 done:
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
795 free(logmsg);
796 free(logmsg_path);
797 free(repo_path);
798 free(editor);
799 free(refname);
800 free(new_commit_id);
801 free(id_str);
802 free(author);
803 free(gitconfig_path);
804 if (branch_ref)
805 got_ref_close(branch_ref);
806 if (head_ref)
807 got_ref_close(head_ref);
808 return error;
811 __dead static void
812 usage_clone(void)
814 fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
816 exit(1);
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
821 int last_p_indexed;
822 int last_p_resolved;
823 int verbosity;
824 };
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
836 return NULL;
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
840 fflush(stdout);
841 return NULL;
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 print_size = 1;
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
857 print_indexed = 1;
858 print_size = 1;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
866 print_resolved = 1;
867 print_indexed = 1;
868 print_size = 1;
873 if (print_size || print_indexed || print_resolved)
874 printf("\r");
875 if (print_size)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 if (print_indexed)
878 printf("; indexing %d%%", p_indexed);
879 if (print_resolved)
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
882 fflush(stdout);
884 return NULL;
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
905 ssize_t n;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
909 TAILQ_INIT(&refs);
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
913 switch (ch) {
914 case 'a':
915 fetch_all_branches = 1;
916 break;
917 case 'm':
918 mirror_references = 1;
919 break;
920 case 'v':
921 if (verbosity < 0)
922 verbosity = 0;
923 else if (verbosity < 3)
924 verbosity++;
925 break;
926 case 'q':
927 verbosity = -1;
928 break;
929 default:
930 usage_clone();
931 break;
934 argc -= optind;
935 argv += optind;
937 uri = argv[0];
939 if (argc == 1)
940 dirname = NULL;
941 else if (argc == 2)
942 dirname = argv[1];
943 else
944 usage_clone();
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
948 if (error)
949 goto done;
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
955 goto done;
958 if (strcmp(proto, "git") == 0) {
959 #ifndef PROFILE
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
962 err(1, "pledge");
963 #endif
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
966 #ifndef PROFILE
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
969 err(1, "pledge");
970 #endif
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
974 goto done;
975 } else {
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
977 goto done;
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
982 goto done;
984 repo_path = default_destdir;
985 } else
986 repo_path = dirname;
988 error = got_path_mkdir(repo_path);
989 if (error)
990 goto done;
992 error = got_repo_init(repo_path);
993 if (error)
994 goto done;
996 error = got_repo_open(&repo, repo_path, NULL);
997 if (error)
998 goto done;
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1004 goto done;
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1008 if (error)
1009 goto done;
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 verbosity);
1013 if (error)
1014 goto done;
1016 if (verbosity >= 0)
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1027 if (error)
1028 goto done;
1030 error = got_object_id_str(&id_str, pack_hash);
1031 if (error)
1032 goto done;
1033 if (verbosity >= 0)
1034 printf("\nFetched %s.pack\n", id_str);
1035 free(id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1045 if (error)
1046 goto done;
1047 error = got_ref_write(ref, repo);
1048 got_ref_close(ref);
1049 if (error)
1050 goto done;
1052 if (mirror_references)
1053 continue;
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1056 continue;
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1062 goto done;
1064 error = got_ref_alloc(&ref, remote_refname, id);
1065 if (error)
1066 goto done;
1067 error = got_ref_write(ref, repo);
1068 got_ref_close(ref);
1069 if (error)
1070 goto done;
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 continue;
1082 error = got_ref_open(&target_ref, repo, target, 0);
1083 if (error) {
1084 if (error->code == GOT_ERR_NOT_REF)
1085 continue;
1086 goto done;
1089 error = got_ref_alloc_symref(&head_symref,
1090 GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1092 if (error)
1093 goto done;
1095 if (verbosity >= 0)
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(head_symref));
1099 error = got_ref_write(head_symref, repo);
1100 break;
1103 /* Create a config file git-fetch(1) can understand. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 goto done;
1109 gitconfig_file = fopen(gitconfig_path, "a");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1112 goto done;
1114 if (mirror_references) {
1115 if (asprintf(&gitconfig,
1116 "[remote \"%s\"]\n"
1117 "\turl = %s\n"
1118 "\tmirror = true\n",
1119 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1120 error = got_error_from_errno("asprintf");
1121 goto done;
1123 } else if (fetch_all_branches) {
1124 if (asprintf(&gitconfig,
1125 "[remote \"%s\"]\n"
1126 "\turl = %s\n"
1127 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1128 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1129 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1130 error = got_error_from_errno("asprintf");
1131 goto done;
1133 } else {
1134 const char *branchname;
1137 * If the server specified a default branch, use just that one.
1138 * Otherwise fall back to fetching all branches on next fetch.
1140 if (head_symref) {
1141 branchname = got_ref_get_symref_target(head_symref);
1142 if (strncmp(branchname, "refs/heads/", 11) == 0)
1143 branchname += 11;
1144 } else
1145 branchname = "*"; /* fall back to all branches */
1146 if (asprintf(&gitconfig,
1147 "[remote \"%s\"]\n"
1148 "\turl = %s\n"
1149 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1150 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1151 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1152 branchname) == -1) {
1153 error = got_error_from_errno("asprintf");
1154 goto done;
1157 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1158 if (n != strlen(gitconfig)) {
1159 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1160 goto done;
1164 if (verbosity >= 0)
1165 printf("Created %s repository '%s'\n",
1166 mirror_references ? "mirrored" : "cloned", repo_path);
1167 done:
1168 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1169 error = got_error_from_errno("close");
1170 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1171 error = got_error_from_errno("fclose");
1172 if (repo)
1173 got_repo_close(repo);
1174 if (head_symref)
1175 got_ref_close(head_symref);
1176 TAILQ_FOREACH(pe, &refs, entry) {
1177 free((void *)pe->path);
1178 free(pe->data);
1180 got_pathlist_free(&refs);
1181 TAILQ_FOREACH(pe, &symrefs, entry) {
1182 free((void *)pe->path);
1183 free(pe->data);
1185 got_pathlist_free(&symrefs);
1186 free(pack_hash);
1187 free(proto);
1188 free(host);
1189 free(port);
1190 free(server_path);
1191 free(repo_name);
1192 free(default_destdir);
1193 free(gitconfig_path);
1194 free(git_url);
1195 return error;
1198 static const struct got_error *
1199 create_ref(const char *refname, struct got_object_id *id,
1200 const char *id_str, struct got_repository *repo)
1202 const struct got_error *err = NULL;
1203 struct got_reference *ref;
1205 printf("Creating %s: %s\n", refname, id_str);
1207 err = got_ref_alloc(&ref, refname, id);
1208 if (err)
1209 return err;
1211 err = got_ref_write(ref, repo);
1212 got_ref_close(ref);
1213 return err;
1216 static const struct got_error *
1217 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1218 struct got_repository *repo)
1220 const struct got_error *err = NULL;
1221 char *new_id_str = NULL;
1222 struct got_object_id *old_id = NULL;
1224 err = got_object_id_str(&new_id_str, new_id);
1225 if (err)
1226 goto done;
1228 if (got_ref_is_symbolic(ref)) {
1229 struct got_reference *new_ref;
1230 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1231 if (err)
1232 goto done;
1233 printf("Deleting symbolic reference %s -> %s\n",
1234 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1235 err = got_ref_delete(ref, repo);
1236 if (err)
1237 goto done;
1238 printf("Setting %s to %s\n", got_ref_get_name(ref),
1239 new_id_str);
1240 err = got_ref_write(new_ref, repo);
1241 if (err)
1242 goto done;
1243 } else {
1244 err = got_ref_resolve(&old_id, repo, ref);
1245 if (err)
1246 goto done;
1247 if (got_object_id_cmp(old_id, new_id) != 0) {
1248 printf("Setting %s to %s\n",
1249 got_ref_get_name(ref), new_id_str);
1250 err = got_ref_change_ref(ref, new_id);
1251 if (err)
1252 goto done;
1253 err = got_ref_write(ref, repo);
1254 if (err)
1255 goto done;
1258 done:
1259 free(old_id);
1260 free(new_id_str);
1261 return err;
1264 __dead static void
1265 usage_fetch(void)
1267 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1268 "[remote-repository-name]\n", getprogname());
1269 exit(1);
1272 static const struct got_error *
1273 cmd_fetch(int argc, char *argv[])
1275 const struct got_error *error = NULL;
1276 char *cwd = NULL, *repo_path = NULL;
1277 const char *remote_name;
1278 char *proto = NULL, *host = NULL, *port = NULL;
1279 char *repo_name = NULL, *server_path = NULL;
1280 struct got_remote_repo *remotes, *remote = NULL;
1281 int nremotes;
1282 char *id_str = NULL;
1283 struct got_repository *repo = NULL;
1284 struct got_worktree *worktree = NULL;
1285 struct got_pathlist_head refs, symrefs;
1286 struct got_pathlist_entry *pe;
1287 struct got_object_id *pack_hash = NULL;
1288 int i, ch, fetchfd = -1;
1289 struct got_fetch_progress_arg fpa;
1290 int verbosity = 0, fetch_all_branches = 0;
1292 TAILQ_INIT(&refs);
1293 TAILQ_INIT(&symrefs);
1295 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1296 switch (ch) {
1297 case 'a':
1298 fetch_all_branches = 1;
1299 break;
1300 case 'r':
1301 repo_path = realpath(optarg, NULL);
1302 if (repo_path == NULL)
1303 return got_error_from_errno2("realpath",
1304 optarg);
1305 got_path_strip_trailing_slashes(repo_path);
1306 break;
1307 case 'v':
1308 if (verbosity < 0)
1309 verbosity = 0;
1310 else if (verbosity < 3)
1311 verbosity++;
1312 break;
1313 case 'q':
1314 verbosity = -1;
1315 break;
1316 default:
1317 usage_fetch();
1318 break;
1321 argc -= optind;
1322 argv += optind;
1324 if (argc == 0)
1325 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1326 else if (argc == 1)
1327 remote_name = argv[0];
1328 else
1329 usage_fetch();
1331 cwd = getcwd(NULL, 0);
1332 if (cwd == NULL) {
1333 error = got_error_from_errno("getcwd");
1334 goto done;
1337 if (repo_path == NULL) {
1338 error = got_worktree_open(&worktree, cwd);
1339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1340 goto done;
1341 else
1342 error = NULL;
1343 if (worktree) {
1344 repo_path =
1345 strdup(got_worktree_get_repo_path(worktree));
1346 if (repo_path == NULL)
1347 error = got_error_from_errno("strdup");
1348 if (error)
1349 goto done;
1350 } else {
1351 repo_path = strdup(cwd);
1352 if (repo_path == NULL) {
1353 error = got_error_from_errno("strdup");
1354 goto done;
1359 error = got_repo_open(&repo, repo_path, NULL);
1360 if (error)
1361 goto done;
1363 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1364 for (i = 0; i < nremotes; i++) {
1365 remote = &remotes[i];
1366 if (strcmp(remote->name, remote_name) == 0)
1367 break;
1369 if (i == nremotes) {
1370 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1371 goto done;
1374 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1375 &repo_name, remote->url);
1376 if (error)
1377 goto done;
1379 if (strcmp(proto, "git") == 0) {
1380 #ifndef PROFILE
1381 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1382 "sendfd dns inet unveil", NULL) == -1)
1383 err(1, "pledge");
1384 #endif
1385 } else if (strcmp(proto, "git+ssh") == 0 ||
1386 strcmp(proto, "ssh") == 0) {
1387 #ifndef PROFILE
1388 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1389 "sendfd unveil", NULL) == -1)
1390 err(1, "pledge");
1391 #endif
1392 } else if (strcmp(proto, "http") == 0 ||
1393 strcmp(proto, "git+http") == 0) {
1394 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1395 goto done;
1396 } else {
1397 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1398 goto done;
1401 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1402 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1403 error = got_error_from_errno2("unveil",
1404 GOT_FETCH_PATH_SSH);
1405 goto done;
1408 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1409 if (error)
1410 goto done;
1412 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1413 verbosity);
1414 if (error)
1415 goto done;
1417 if (verbosity >= 0)
1418 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1419 port ? ":" : "", port ? port : "");
1421 fpa.last_scaled_size[0] = '\0';
1422 fpa.last_p_indexed = -1;
1423 fpa.last_p_resolved = -1;
1424 fpa.verbosity = verbosity;
1425 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1426 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1427 fetch_progress, &fpa);
1428 if (error)
1429 goto done;
1431 if (pack_hash == NULL) {
1432 if (verbosity >= 0)
1433 printf("Already up-to-date\n");
1434 goto done;
1437 if (verbosity >= 0) {
1438 error = got_object_id_str(&id_str, pack_hash);
1439 if (error)
1440 goto done;
1441 printf("\nFetched %s.pack\n", id_str);
1442 free(id_str);
1443 id_str = NULL;
1446 /* Update references provided with the pack file. */
1447 TAILQ_FOREACH(pe, &refs, entry) {
1448 const char *refname = pe->path;
1449 struct got_object_id *id = pe->data;
1450 struct got_reference *ref;
1451 char *remote_refname;
1453 error = got_object_id_str(&id_str, id);
1454 if (error)
1455 goto done;
1457 if (strncmp("refs/tags/", refname, 10) == 0) {
1458 error = got_ref_open(&ref, repo, refname, 0);
1459 if (error) {
1460 if (error->code != GOT_ERR_NOT_REF)
1461 goto done;
1462 error = create_ref(refname, id, id_str, repo);
1463 if (error)
1464 goto done;
1465 } else {
1466 error = update_ref(ref, id, repo);
1467 got_ref_close(ref);
1468 if (error)
1469 goto done;
1471 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1472 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1473 remote_name, refname + 11) == -1) {
1474 error = got_error_from_errno("asprintf");
1475 goto done;
1478 error = got_ref_open(&ref, repo, remote_refname, 0);
1479 if (error) {
1480 if (error->code != GOT_ERR_NOT_REF)
1481 goto done;
1482 error = create_ref(remote_refname, id, id_str,
1483 repo);
1484 if (error)
1485 goto done;
1486 } else {
1487 error = update_ref(ref, id, repo);
1488 got_ref_close(ref);
1489 if (error)
1490 goto done;
1493 free(id_str);
1494 id_str = NULL;
1496 done:
1497 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1498 error = got_error_from_errno("close");
1499 if (repo)
1500 got_repo_close(repo);
1501 if (worktree)
1502 got_worktree_close(worktree);
1503 TAILQ_FOREACH(pe, &refs, entry) {
1504 free((void *)pe->path);
1505 free(pe->data);
1507 got_pathlist_free(&refs);
1508 TAILQ_FOREACH(pe, &symrefs, entry) {
1509 free((void *)pe->path);
1510 free(pe->data);
1512 got_pathlist_free(&symrefs);
1513 free(id_str);
1514 free(cwd);
1515 free(repo_path);
1516 free(pack_hash);
1517 free(proto);
1518 free(host);
1519 free(port);
1520 free(server_path);
1521 free(repo_name);
1522 return error;
1526 __dead static void
1527 usage_checkout(void)
1529 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1530 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1531 exit(1);
1534 static void
1535 show_worktree_base_ref_warning(void)
1537 fprintf(stderr, "%s: warning: could not create a reference "
1538 "to the work tree's base commit; the commit could be "
1539 "garbage-collected by Git; making the repository "
1540 "writable and running 'got update' will prevent this\n",
1541 getprogname());
1544 struct got_checkout_progress_arg {
1545 const char *worktree_path;
1546 int had_base_commit_ref_error;
1549 static const struct got_error *
1550 checkout_progress(void *arg, unsigned char status, const char *path)
1552 struct got_checkout_progress_arg *a = arg;
1554 /* Base commit bump happens silently. */
1555 if (status == GOT_STATUS_BUMP_BASE)
1556 return NULL;
1558 if (status == GOT_STATUS_BASE_REF_ERR) {
1559 a->had_base_commit_ref_error = 1;
1560 return NULL;
1563 while (path[0] == '/')
1564 path++;
1566 printf("%c %s/%s\n", status, a->worktree_path, path);
1567 return NULL;
1570 static const struct got_error *
1571 check_cancelled(void *arg)
1573 if (sigint_received || sigpipe_received)
1574 return got_error(GOT_ERR_CANCELLED);
1575 return NULL;
1578 static const struct got_error *
1579 check_linear_ancestry(struct got_object_id *commit_id,
1580 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1581 struct got_repository *repo)
1583 const struct got_error *err = NULL;
1584 struct got_object_id *yca_id;
1586 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1587 commit_id, base_commit_id, repo, check_cancelled, NULL);
1588 if (err)
1589 return err;
1591 if (yca_id == NULL)
1592 return got_error(GOT_ERR_ANCESTRY);
1595 * Require a straight line of history between the target commit
1596 * and the work tree's base commit.
1598 * Non-linear situations such as this require a rebase:
1600 * (commit) D F (base_commit)
1601 * \ /
1602 * C E
1603 * \ /
1604 * B (yca)
1605 * |
1606 * A
1608 * 'got update' only handles linear cases:
1609 * Update forwards in time: A (base/yca) - B - C - D (commit)
1610 * Update backwards in time: D (base) - C - B - A (commit/yca)
1612 if (allow_forwards_in_time_only) {
1613 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1614 return got_error(GOT_ERR_ANCESTRY);
1615 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1616 got_object_id_cmp(base_commit_id, yca_id) != 0)
1617 return got_error(GOT_ERR_ANCESTRY);
1619 free(yca_id);
1620 return NULL;
1623 static const struct got_error *
1624 check_same_branch(struct got_object_id *commit_id,
1625 struct got_reference *head_ref, struct got_object_id *yca_id,
1626 struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_commit_graph *graph = NULL;
1630 struct got_object_id *head_commit_id = NULL;
1631 int is_same_branch = 0;
1633 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1634 if (err)
1635 goto done;
1637 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1638 is_same_branch = 1;
1639 goto done;
1641 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1642 is_same_branch = 1;
1643 goto done;
1646 err = got_commit_graph_open(&graph, "/", 1);
1647 if (err)
1648 goto done;
1650 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1651 check_cancelled, NULL);
1652 if (err)
1653 goto done;
1655 for (;;) {
1656 struct got_object_id *id;
1657 err = got_commit_graph_iter_next(&id, graph, repo,
1658 check_cancelled, NULL);
1659 if (err) {
1660 if (err->code == GOT_ERR_ITER_COMPLETED)
1661 err = NULL;
1662 break;
1665 if (id) {
1666 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1667 break;
1668 if (got_object_id_cmp(id, commit_id) == 0) {
1669 is_same_branch = 1;
1670 break;
1674 done:
1675 if (graph)
1676 got_commit_graph_close(graph);
1677 free(head_commit_id);
1678 if (!err && !is_same_branch)
1679 err = got_error(GOT_ERR_ANCESTRY);
1680 return err;
1683 static const struct got_error *
1684 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1686 static char msg[512];
1687 const char *branch_name;
1689 if (got_ref_is_symbolic(ref))
1690 branch_name = got_ref_get_symref_target(ref);
1691 else
1692 branch_name = got_ref_get_name(ref);
1694 if (strncmp("refs/heads/", branch_name, 11) == 0)
1695 branch_name += 11;
1697 snprintf(msg, sizeof(msg),
1698 "target commit is not contained in branch '%s'; "
1699 "the branch to use must be specified with -b; "
1700 "if necessary a new branch can be created for "
1701 "this commit with 'got branch -c %s BRANCH_NAME'",
1702 branch_name, commit_id_str);
1704 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1707 static const struct got_error *
1708 cmd_checkout(int argc, char *argv[])
1710 const struct got_error *error = NULL;
1711 struct got_repository *repo = NULL;
1712 struct got_reference *head_ref = NULL;
1713 struct got_worktree *worktree = NULL;
1714 char *repo_path = NULL;
1715 char *worktree_path = NULL;
1716 const char *path_prefix = "";
1717 const char *branch_name = GOT_REF_HEAD;
1718 char *commit_id_str = NULL;
1719 int ch, same_path_prefix, allow_nonempty = 0;
1720 struct got_pathlist_head paths;
1721 struct got_checkout_progress_arg cpa;
1723 TAILQ_INIT(&paths);
1725 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1726 switch (ch) {
1727 case 'b':
1728 branch_name = optarg;
1729 break;
1730 case 'c':
1731 commit_id_str = strdup(optarg);
1732 if (commit_id_str == NULL)
1733 return got_error_from_errno("strdup");
1734 break;
1735 case 'E':
1736 allow_nonempty = 1;
1737 break;
1738 case 'p':
1739 path_prefix = optarg;
1740 break;
1741 default:
1742 usage_checkout();
1743 /* NOTREACHED */
1747 argc -= optind;
1748 argv += optind;
1750 #ifndef PROFILE
1751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1752 "unveil", NULL) == -1)
1753 err(1, "pledge");
1754 #endif
1755 if (argc == 1) {
1756 char *cwd, *base, *dotgit;
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL)
1759 return got_error_from_errno2("realpath", argv[0]);
1760 cwd = getcwd(NULL, 0);
1761 if (cwd == NULL) {
1762 error = got_error_from_errno("getcwd");
1763 goto done;
1765 if (path_prefix[0]) {
1766 base = basename(path_prefix);
1767 if (base == NULL) {
1768 error = got_error_from_errno2("basename",
1769 path_prefix);
1770 goto done;
1772 } else {
1773 base = basename(repo_path);
1774 if (base == NULL) {
1775 error = got_error_from_errno2("basename",
1776 repo_path);
1777 goto done;
1780 dotgit = strstr(base, ".git");
1781 if (dotgit)
1782 *dotgit = '\0';
1783 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1784 error = got_error_from_errno("asprintf");
1785 free(cwd);
1786 goto done;
1788 free(cwd);
1789 } else if (argc == 2) {
1790 repo_path = realpath(argv[0], NULL);
1791 if (repo_path == NULL) {
1792 error = got_error_from_errno2("realpath", argv[0]);
1793 goto done;
1795 worktree_path = realpath(argv[1], NULL);
1796 if (worktree_path == NULL) {
1797 if (errno != ENOENT) {
1798 error = got_error_from_errno2("realpath",
1799 argv[1]);
1800 goto done;
1802 worktree_path = strdup(argv[1]);
1803 if (worktree_path == NULL) {
1804 error = got_error_from_errno("strdup");
1805 goto done;
1808 } else
1809 usage_checkout();
1811 got_path_strip_trailing_slashes(repo_path);
1812 got_path_strip_trailing_slashes(worktree_path);
1814 error = got_repo_open(&repo, repo_path, NULL);
1815 if (error != NULL)
1816 goto done;
1818 /* Pre-create work tree path for unveil(2) */
1819 error = got_path_mkdir(worktree_path);
1820 if (error) {
1821 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1822 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1823 goto done;
1824 if (!allow_nonempty &&
1825 !got_path_dir_is_empty(worktree_path)) {
1826 error = got_error_path(worktree_path,
1827 GOT_ERR_DIR_NOT_EMPTY);
1828 goto done;
1832 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1833 if (error)
1834 goto done;
1836 error = got_ref_open(&head_ref, repo, branch_name, 0);
1837 if (error != NULL)
1838 goto done;
1840 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1841 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1842 goto done;
1844 error = got_worktree_open(&worktree, worktree_path);
1845 if (error != NULL)
1846 goto done;
1848 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1849 path_prefix);
1850 if (error != NULL)
1851 goto done;
1852 if (!same_path_prefix) {
1853 error = got_error(GOT_ERR_PATH_PREFIX);
1854 goto done;
1857 if (commit_id_str) {
1858 struct got_object_id *commit_id;
1859 error = got_repo_match_object_id(&commit_id, NULL,
1860 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1861 if (error)
1862 goto done;
1863 error = check_linear_ancestry(commit_id,
1864 got_worktree_get_base_commit_id(worktree), 0, repo);
1865 if (error != NULL) {
1866 free(commit_id);
1867 if (error->code == GOT_ERR_ANCESTRY) {
1868 error = checkout_ancestry_error(
1869 head_ref, commit_id_str);
1871 goto done;
1873 error = check_same_branch(commit_id, head_ref, NULL, repo);
1874 if (error) {
1875 if (error->code == GOT_ERR_ANCESTRY) {
1876 error = checkout_ancestry_error(
1877 head_ref, commit_id_str);
1879 goto done;
1881 error = got_worktree_set_base_commit_id(worktree, repo,
1882 commit_id);
1883 free(commit_id);
1884 if (error)
1885 goto done;
1888 error = got_pathlist_append(&paths, "", NULL);
1889 if (error)
1890 goto done;
1891 cpa.worktree_path = worktree_path;
1892 cpa.had_base_commit_ref_error = 0;
1893 error = got_worktree_checkout_files(worktree, &paths, repo,
1894 checkout_progress, &cpa, check_cancelled, NULL);
1895 if (error != NULL)
1896 goto done;
1898 printf("Now shut up and hack\n");
1899 if (cpa.had_base_commit_ref_error)
1900 show_worktree_base_ref_warning();
1901 done:
1902 got_pathlist_free(&paths);
1903 free(commit_id_str);
1904 free(repo_path);
1905 free(worktree_path);
1906 return error;
1909 __dead static void
1910 usage_update(void)
1912 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1913 getprogname());
1914 exit(1);
1917 static const struct got_error *
1918 update_progress(void *arg, unsigned char status, const char *path)
1920 int *did_something = arg;
1922 if (status == GOT_STATUS_EXISTS ||
1923 status == GOT_STATUS_BASE_REF_ERR)
1924 return NULL;
1926 *did_something = 1;
1928 /* Base commit bump happens silently. */
1929 if (status == GOT_STATUS_BUMP_BASE)
1930 return NULL;
1932 while (path[0] == '/')
1933 path++;
1934 printf("%c %s\n", status, path);
1935 return NULL;
1938 static const struct got_error *
1939 switch_head_ref(struct got_reference *head_ref,
1940 struct got_object_id *commit_id, struct got_worktree *worktree,
1941 struct got_repository *repo)
1943 const struct got_error *err = NULL;
1944 char *base_id_str;
1945 int ref_has_moved = 0;
1947 /* Trivial case: switching between two different references. */
1948 if (strcmp(got_ref_get_name(head_ref),
1949 got_worktree_get_head_ref_name(worktree)) != 0) {
1950 printf("Switching work tree from %s to %s\n",
1951 got_worktree_get_head_ref_name(worktree),
1952 got_ref_get_name(head_ref));
1953 return got_worktree_set_head_ref(worktree, head_ref);
1956 err = check_linear_ancestry(commit_id,
1957 got_worktree_get_base_commit_id(worktree), 0, repo);
1958 if (err) {
1959 if (err->code != GOT_ERR_ANCESTRY)
1960 return err;
1961 ref_has_moved = 1;
1963 if (!ref_has_moved)
1964 return NULL;
1966 /* Switching to a rebased branch with the same reference name. */
1967 err = got_object_id_str(&base_id_str,
1968 got_worktree_get_base_commit_id(worktree));
1969 if (err)
1970 return err;
1971 printf("Reference %s now points at a different branch\n",
1972 got_worktree_get_head_ref_name(worktree));
1973 printf("Switching work tree from %s to %s\n", base_id_str,
1974 got_worktree_get_head_ref_name(worktree));
1975 return NULL;
1978 static const struct got_error *
1979 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1981 const struct got_error *err;
1982 int in_progress;
1984 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1985 if (err)
1986 return err;
1987 if (in_progress)
1988 return got_error(GOT_ERR_REBASING);
1990 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1991 if (err)
1992 return err;
1993 if (in_progress)
1994 return got_error(GOT_ERR_HISTEDIT_BUSY);
1996 return NULL;
1999 static const struct got_error *
2000 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2001 char *argv[], struct got_worktree *worktree)
2003 const struct got_error *err = NULL;
2004 char *path;
2005 int i;
2007 if (argc == 0) {
2008 path = strdup("");
2009 if (path == NULL)
2010 return got_error_from_errno("strdup");
2011 return got_pathlist_append(paths, path, NULL);
2014 for (i = 0; i < argc; i++) {
2015 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2016 if (err)
2017 break;
2018 err = got_pathlist_append(paths, path, NULL);
2019 if (err) {
2020 free(path);
2021 break;
2025 return err;
2028 static const struct got_error *
2029 cmd_update(int argc, char *argv[])
2031 const struct got_error *error = NULL;
2032 struct got_repository *repo = NULL;
2033 struct got_worktree *worktree = NULL;
2034 char *worktree_path = NULL;
2035 struct got_object_id *commit_id = NULL;
2036 char *commit_id_str = NULL;
2037 const char *branch_name = NULL;
2038 struct got_reference *head_ref = NULL;
2039 struct got_pathlist_head paths;
2040 struct got_pathlist_entry *pe;
2041 int ch, did_something = 0;
2043 TAILQ_INIT(&paths);
2045 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2046 switch (ch) {
2047 case 'b':
2048 branch_name = optarg;
2049 break;
2050 case 'c':
2051 commit_id_str = strdup(optarg);
2052 if (commit_id_str == NULL)
2053 return got_error_from_errno("strdup");
2054 break;
2055 default:
2056 usage_update();
2057 /* NOTREACHED */
2061 argc -= optind;
2062 argv += optind;
2064 #ifndef PROFILE
2065 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2066 "unveil", NULL) == -1)
2067 err(1, "pledge");
2068 #endif
2069 worktree_path = getcwd(NULL, 0);
2070 if (worktree_path == NULL) {
2071 error = got_error_from_errno("getcwd");
2072 goto done;
2074 error = got_worktree_open(&worktree, worktree_path);
2075 if (error)
2076 goto done;
2078 error = check_rebase_or_histedit_in_progress(worktree);
2079 if (error)
2080 goto done;
2082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2083 NULL);
2084 if (error != NULL)
2085 goto done;
2087 error = apply_unveil(got_repo_get_path(repo), 0,
2088 got_worktree_get_root_path(worktree));
2089 if (error)
2090 goto done;
2092 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2093 if (error)
2094 goto done;
2096 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2097 got_worktree_get_head_ref_name(worktree), 0);
2098 if (error != NULL)
2099 goto done;
2100 if (commit_id_str == NULL) {
2101 error = got_ref_resolve(&commit_id, repo, head_ref);
2102 if (error != NULL)
2103 goto done;
2104 error = got_object_id_str(&commit_id_str, commit_id);
2105 if (error != NULL)
2106 goto done;
2107 } else {
2108 error = got_repo_match_object_id(&commit_id, NULL,
2109 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2110 free(commit_id_str);
2111 commit_id_str = NULL;
2112 if (error)
2113 goto done;
2114 error = got_object_id_str(&commit_id_str, commit_id);
2115 if (error)
2116 goto done;
2119 if (branch_name) {
2120 struct got_object_id *head_commit_id;
2121 TAILQ_FOREACH(pe, &paths, entry) {
2122 if (pe->path_len == 0)
2123 continue;
2124 error = got_error_msg(GOT_ERR_BAD_PATH,
2125 "switching between branches requires that "
2126 "the entire work tree gets updated");
2127 goto done;
2129 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2130 if (error)
2131 goto done;
2132 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2133 repo);
2134 free(head_commit_id);
2135 if (error != NULL)
2136 goto done;
2137 error = check_same_branch(commit_id, head_ref, NULL, repo);
2138 if (error)
2139 goto done;
2140 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2141 if (error)
2142 goto done;
2143 } else {
2144 error = check_linear_ancestry(commit_id,
2145 got_worktree_get_base_commit_id(worktree), 0, repo);
2146 if (error != NULL) {
2147 if (error->code == GOT_ERR_ANCESTRY)
2148 error = got_error(GOT_ERR_BRANCH_MOVED);
2149 goto done;
2151 error = check_same_branch(commit_id, head_ref, NULL, repo);
2152 if (error)
2153 goto done;
2156 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2157 commit_id) != 0) {
2158 error = got_worktree_set_base_commit_id(worktree, repo,
2159 commit_id);
2160 if (error)
2161 goto done;
2164 error = got_worktree_checkout_files(worktree, &paths, repo,
2165 update_progress, &did_something, check_cancelled, NULL);
2166 if (error != NULL)
2167 goto done;
2169 if (did_something)
2170 printf("Updated to commit %s\n", commit_id_str);
2171 else
2172 printf("Already up-to-date\n");
2173 done:
2174 free(worktree_path);
2175 TAILQ_FOREACH(pe, &paths, entry)
2176 free((char *)pe->path);
2177 got_pathlist_free(&paths);
2178 free(commit_id);
2179 free(commit_id_str);
2180 return error;
2183 static const struct got_error *
2184 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2185 const char *path, int diff_context, int ignore_whitespace,
2186 struct got_repository *repo)
2188 const struct got_error *err = NULL;
2189 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2191 if (blob_id1) {
2192 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2193 if (err)
2194 goto done;
2197 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2198 if (err)
2199 goto done;
2201 while (path[0] == '/')
2202 path++;
2203 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2204 ignore_whitespace, stdout);
2205 done:
2206 if (blob1)
2207 got_object_blob_close(blob1);
2208 got_object_blob_close(blob2);
2209 return err;
2212 static const struct got_error *
2213 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2214 const char *path, int diff_context, int ignore_whitespace,
2215 struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2219 struct got_diff_blob_output_unidiff_arg arg;
2221 if (tree_id1) {
2222 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2223 if (err)
2224 goto done;
2227 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2228 if (err)
2229 goto done;
2231 arg.diff_context = diff_context;
2232 arg.ignore_whitespace = ignore_whitespace;
2233 arg.outfile = stdout;
2234 while (path[0] == '/')
2235 path++;
2236 err = got_diff_tree(tree1, tree2, path, path, repo,
2237 got_diff_blob_output_unidiff, &arg, 1);
2238 done:
2239 if (tree1)
2240 got_object_tree_close(tree1);
2241 if (tree2)
2242 got_object_tree_close(tree2);
2243 return err;
2246 static const struct got_error *
2247 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2248 const char *path, int diff_context, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_commit_object *pcommit = NULL;
2252 char *id_str1 = NULL, *id_str2 = NULL;
2253 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2254 struct got_object_qid *qid;
2256 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2257 if (qid != NULL) {
2258 err = got_object_open_as_commit(&pcommit, repo,
2259 qid->id);
2260 if (err)
2261 return err;
2264 if (path && path[0] != '\0') {
2265 int obj_type;
2266 err = got_object_id_by_path(&obj_id2, repo, id, path);
2267 if (err)
2268 goto done;
2269 err = got_object_id_str(&id_str2, obj_id2);
2270 if (err) {
2271 free(obj_id2);
2272 goto done;
2274 if (pcommit) {
2275 err = got_object_id_by_path(&obj_id1, repo,
2276 qid->id, path);
2277 if (err) {
2278 free(obj_id2);
2279 goto done;
2281 err = got_object_id_str(&id_str1, obj_id1);
2282 if (err) {
2283 free(obj_id2);
2284 goto done;
2287 err = got_object_get_type(&obj_type, repo, obj_id2);
2288 if (err) {
2289 free(obj_id2);
2290 goto done;
2292 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2293 switch (obj_type) {
2294 case GOT_OBJ_TYPE_BLOB:
2295 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2296 0, repo);
2297 break;
2298 case GOT_OBJ_TYPE_TREE:
2299 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2300 0, repo);
2301 break;
2302 default:
2303 err = got_error(GOT_ERR_OBJ_TYPE);
2304 break;
2306 free(obj_id1);
2307 free(obj_id2);
2308 } else {
2309 obj_id2 = got_object_commit_get_tree_id(commit);
2310 err = got_object_id_str(&id_str2, obj_id2);
2311 if (err)
2312 goto done;
2313 obj_id1 = got_object_commit_get_tree_id(pcommit);
2314 err = got_object_id_str(&id_str1, obj_id1);
2315 if (err)
2316 goto done;
2317 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2318 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2320 done:
2321 free(id_str1);
2322 free(id_str2);
2323 if (pcommit)
2324 got_object_commit_close(pcommit);
2325 return err;
2328 static char *
2329 get_datestr(time_t *time, char *datebuf)
2331 struct tm mytm, *tm;
2332 char *p, *s;
2334 tm = gmtime_r(time, &mytm);
2335 if (tm == NULL)
2336 return NULL;
2337 s = asctime_r(tm, datebuf);
2338 if (s == NULL)
2339 return NULL;
2340 p = strchr(s, '\n');
2341 if (p)
2342 *p = '\0';
2343 return s;
2346 static const struct got_error *
2347 match_logmsg(int *have_match, struct got_object_id *id,
2348 struct got_commit_object *commit, regex_t *regex)
2350 const struct got_error *err = NULL;
2351 regmatch_t regmatch;
2352 char *id_str = NULL, *logmsg = NULL;
2354 *have_match = 0;
2356 err = got_object_id_str(&id_str, id);
2357 if (err)
2358 return err;
2360 err = got_object_commit_get_logmsg(&logmsg, commit);
2361 if (err)
2362 goto done;
2364 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2365 *have_match = 1;
2366 done:
2367 free(id_str);
2368 free(logmsg);
2369 return err;
2372 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2374 static const struct got_error *
2375 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2376 struct got_repository *repo, const char *path, int show_patch,
2377 int diff_context, struct got_reflist_head *refs)
2379 const struct got_error *err = NULL;
2380 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2381 char datebuf[26];
2382 time_t committer_time;
2383 const char *author, *committer;
2384 char *refs_str = NULL;
2385 struct got_reflist_entry *re;
2387 SIMPLEQ_FOREACH(re, refs, entry) {
2388 char *s;
2389 const char *name;
2390 struct got_tag_object *tag = NULL;
2391 int cmp;
2393 name = got_ref_get_name(re->ref);
2394 if (strcmp(name, GOT_REF_HEAD) == 0)
2395 continue;
2396 if (strncmp(name, "refs/", 5) == 0)
2397 name += 5;
2398 if (strncmp(name, "got/", 4) == 0)
2399 continue;
2400 if (strncmp(name, "heads/", 6) == 0)
2401 name += 6;
2402 if (strncmp(name, "remotes/", 8) == 0)
2403 name += 8;
2404 if (strncmp(name, "tags/", 5) == 0) {
2405 err = got_object_open_as_tag(&tag, repo, re->id);
2406 if (err) {
2407 if (err->code != GOT_ERR_OBJ_TYPE)
2408 return err;
2409 /* Ref points at something other than a tag. */
2410 err = NULL;
2411 tag = NULL;
2414 cmp = got_object_id_cmp(tag ?
2415 got_object_tag_get_object_id(tag) : re->id, id);
2416 if (tag)
2417 got_object_tag_close(tag);
2418 if (cmp != 0)
2419 continue;
2420 s = refs_str;
2421 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2422 name) == -1) {
2423 err = got_error_from_errno("asprintf");
2424 free(s);
2425 return err;
2427 free(s);
2429 err = got_object_id_str(&id_str, id);
2430 if (err)
2431 return err;
2433 printf(GOT_COMMIT_SEP_STR);
2434 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2435 refs_str ? refs_str : "", refs_str ? ")" : "");
2436 free(id_str);
2437 id_str = NULL;
2438 free(refs_str);
2439 refs_str = NULL;
2440 printf("from: %s\n", got_object_commit_get_author(commit));
2441 committer_time = got_object_commit_get_committer_time(commit);
2442 datestr = get_datestr(&committer_time, datebuf);
2443 if (datestr)
2444 printf("date: %s UTC\n", datestr);
2445 author = got_object_commit_get_author(commit);
2446 committer = got_object_commit_get_committer(commit);
2447 if (strcmp(author, committer) != 0)
2448 printf("via: %s\n", committer);
2449 if (got_object_commit_get_nparents(commit) > 1) {
2450 const struct got_object_id_queue *parent_ids;
2451 struct got_object_qid *qid;
2452 int n = 1;
2453 parent_ids = got_object_commit_get_parent_ids(commit);
2454 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2455 err = got_object_id_str(&id_str, qid->id);
2456 if (err)
2457 return err;
2458 printf("parent %d: %s\n", n++, id_str);
2459 free(id_str);
2463 err = got_object_commit_get_logmsg(&logmsg0, commit);
2464 if (err)
2465 return err;
2467 logmsg = logmsg0;
2468 do {
2469 line = strsep(&logmsg, "\n");
2470 if (line)
2471 printf(" %s\n", line);
2472 } while (line);
2473 free(logmsg0);
2475 if (show_patch) {
2476 err = print_patch(commit, id, path, diff_context, repo);
2477 if (err == 0)
2478 printf("\n");
2481 if (fflush(stdout) != 0 && err == NULL)
2482 err = got_error_from_errno("fflush");
2483 return err;
2486 static const struct got_error *
2487 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2488 const char *path, int show_patch, const char *search_pattern,
2489 int diff_context, int limit, int log_branches,
2490 struct got_reflist_head *refs)
2492 const struct got_error *err;
2493 struct got_commit_graph *graph;
2494 regex_t regex;
2495 int have_match;
2497 if (search_pattern &&
2498 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2499 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2501 err = got_commit_graph_open(&graph, path, !log_branches);
2502 if (err)
2503 return err;
2504 err = got_commit_graph_iter_start(graph, root_id, repo,
2505 check_cancelled, NULL);
2506 if (err)
2507 goto done;
2508 for (;;) {
2509 struct got_commit_object *commit;
2510 struct got_object_id *id;
2512 if (sigint_received || sigpipe_received)
2513 break;
2515 err = got_commit_graph_iter_next(&id, graph, repo,
2516 check_cancelled, NULL);
2517 if (err) {
2518 if (err->code == GOT_ERR_ITER_COMPLETED)
2519 err = NULL;
2520 break;
2522 if (id == NULL)
2523 break;
2525 err = got_object_open_as_commit(&commit, repo, id);
2526 if (err)
2527 break;
2529 if (search_pattern) {
2530 err = match_logmsg(&have_match, id, commit, &regex);
2531 if (err) {
2532 got_object_commit_close(commit);
2533 break;
2535 if (have_match == 0) {
2536 got_object_commit_close(commit);
2537 continue;
2541 err = print_commit(commit, id, repo, path, show_patch,
2542 diff_context, refs);
2543 got_object_commit_close(commit);
2544 if (err || (limit && --limit == 0))
2545 break;
2547 done:
2548 if (search_pattern)
2549 regfree(&regex);
2550 got_commit_graph_close(graph);
2551 return err;
2554 __dead static void
2555 usage_log(void)
2557 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2558 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2559 exit(1);
2562 static int
2563 get_default_log_limit(void)
2565 const char *got_default_log_limit;
2566 long long n;
2567 const char *errstr;
2569 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2570 if (got_default_log_limit == NULL)
2571 return 0;
2572 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2573 if (errstr != NULL)
2574 return 0;
2575 return n;
2578 static const struct got_error *
2579 cmd_log(int argc, char *argv[])
2581 const struct got_error *error;
2582 struct got_repository *repo = NULL;
2583 struct got_worktree *worktree = NULL;
2584 struct got_commit_object *commit = NULL;
2585 struct got_object_id *id = NULL;
2586 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2587 const char *start_commit = NULL, *search_pattern = NULL;
2588 int diff_context = -1, ch;
2589 int show_patch = 0, limit = 0, log_branches = 0;
2590 const char *errstr;
2591 struct got_reflist_head refs;
2593 SIMPLEQ_INIT(&refs);
2595 #ifndef PROFILE
2596 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2597 NULL)
2598 == -1)
2599 err(1, "pledge");
2600 #endif
2602 limit = get_default_log_limit();
2604 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2605 switch (ch) {
2606 case 'p':
2607 show_patch = 1;
2608 break;
2609 case 'c':
2610 start_commit = optarg;
2611 break;
2612 case 'C':
2613 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2614 &errstr);
2615 if (errstr != NULL)
2616 err(1, "-C option %s", errstr);
2617 break;
2618 case 'l':
2619 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2620 if (errstr != NULL)
2621 err(1, "-l option %s", errstr);
2622 break;
2623 case 'b':
2624 log_branches = 1;
2625 break;
2626 case 'r':
2627 repo_path = realpath(optarg, NULL);
2628 if (repo_path == NULL)
2629 return got_error_from_errno2("realpath",
2630 optarg);
2631 got_path_strip_trailing_slashes(repo_path);
2632 break;
2633 case 's':
2634 search_pattern = optarg;
2635 break;
2636 default:
2637 usage_log();
2638 /* NOTREACHED */
2642 argc -= optind;
2643 argv += optind;
2645 if (diff_context == -1)
2646 diff_context = 3;
2647 else if (!show_patch)
2648 errx(1, "-C reguires -p");
2650 cwd = getcwd(NULL, 0);
2651 if (cwd == NULL) {
2652 error = got_error_from_errno("getcwd");
2653 goto done;
2656 error = got_worktree_open(&worktree, cwd);
2657 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2658 goto done;
2659 error = NULL;
2661 if (argc == 0) {
2662 path = strdup("");
2663 if (path == NULL) {
2664 error = got_error_from_errno("strdup");
2665 goto done;
2667 } else if (argc == 1) {
2668 if (worktree) {
2669 error = got_worktree_resolve_path(&path, worktree,
2670 argv[0]);
2671 if (error)
2672 goto done;
2673 } else {
2674 path = strdup(argv[0]);
2675 if (path == NULL) {
2676 error = got_error_from_errno("strdup");
2677 goto done;
2680 } else
2681 usage_log();
2683 if (repo_path == NULL) {
2684 repo_path = worktree ?
2685 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2687 if (repo_path == NULL) {
2688 error = got_error_from_errno("strdup");
2689 goto done;
2692 error = got_repo_open(&repo, repo_path, NULL);
2693 if (error != NULL)
2694 goto done;
2696 error = apply_unveil(got_repo_get_path(repo), 1,
2697 worktree ? got_worktree_get_root_path(worktree) : NULL);
2698 if (error)
2699 goto done;
2701 if (start_commit == NULL) {
2702 struct got_reference *head_ref;
2703 error = got_ref_open(&head_ref, repo,
2704 worktree ? got_worktree_get_head_ref_name(worktree)
2705 : GOT_REF_HEAD, 0);
2706 if (error != NULL)
2707 return error;
2708 error = got_ref_resolve(&id, repo, head_ref);
2709 got_ref_close(head_ref);
2710 if (error != NULL)
2711 return error;
2712 error = got_object_open_as_commit(&commit, repo, id);
2713 } else {
2714 struct got_reference *ref;
2715 error = got_ref_open(&ref, repo, start_commit, 0);
2716 if (error == NULL) {
2717 int obj_type;
2718 error = got_ref_resolve(&id, repo, ref);
2719 got_ref_close(ref);
2720 if (error != NULL)
2721 goto done;
2722 error = got_object_get_type(&obj_type, repo, id);
2723 if (error != NULL)
2724 goto done;
2725 if (obj_type == GOT_OBJ_TYPE_TAG) {
2726 struct got_tag_object *tag;
2727 error = got_object_open_as_tag(&tag, repo, id);
2728 if (error != NULL)
2729 goto done;
2730 if (got_object_tag_get_object_type(tag) !=
2731 GOT_OBJ_TYPE_COMMIT) {
2732 got_object_tag_close(tag);
2733 error = got_error(GOT_ERR_OBJ_TYPE);
2734 goto done;
2736 free(id);
2737 id = got_object_id_dup(
2738 got_object_tag_get_object_id(tag));
2739 if (id == NULL)
2740 error = got_error_from_errno(
2741 "got_object_id_dup");
2742 got_object_tag_close(tag);
2743 if (error)
2744 goto done;
2745 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2746 error = got_error(GOT_ERR_OBJ_TYPE);
2747 goto done;
2749 error = got_object_open_as_commit(&commit, repo, id);
2750 if (error != NULL)
2751 goto done;
2753 if (commit == NULL) {
2754 error = got_repo_match_object_id_prefix(&id,
2755 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2756 if (error != NULL)
2757 return error;
2760 if (error != NULL)
2761 goto done;
2763 if (worktree) {
2764 const char *prefix = got_worktree_get_path_prefix(worktree);
2765 char *p;
2766 if (asprintf(&p, "%s%s%s", prefix,
2767 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2768 error = got_error_from_errno("asprintf");
2769 goto done;
2771 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2772 free(p);
2773 } else
2774 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2775 if (error != NULL)
2776 goto done;
2777 if (in_repo_path) {
2778 free(path);
2779 path = in_repo_path;
2782 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2783 if (error)
2784 goto done;
2786 error = print_commits(id, repo, path, show_patch, search_pattern,
2787 diff_context, limit, log_branches, &refs);
2788 done:
2789 free(path);
2790 free(repo_path);
2791 free(cwd);
2792 free(id);
2793 if (worktree)
2794 got_worktree_close(worktree);
2795 if (repo) {
2796 const struct got_error *repo_error;
2797 repo_error = got_repo_close(repo);
2798 if (error == NULL)
2799 error = repo_error;
2801 got_ref_list_free(&refs);
2802 return error;
2805 __dead static void
2806 usage_diff(void)
2808 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2809 "[-w] [object1 object2 | path]\n", getprogname());
2810 exit(1);
2813 struct print_diff_arg {
2814 struct got_repository *repo;
2815 struct got_worktree *worktree;
2816 int diff_context;
2817 const char *id_str;
2818 int header_shown;
2819 int diff_staged;
2820 int ignore_whitespace;
2823 static const struct got_error *
2824 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2825 const char *path, struct got_object_id *blob_id,
2826 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2827 int dirfd, const char *de_name)
2829 struct print_diff_arg *a = arg;
2830 const struct got_error *err = NULL;
2831 struct got_blob_object *blob1 = NULL;
2832 int fd = -1;
2833 FILE *f2 = NULL;
2834 char *abspath = NULL, *label1 = NULL;
2835 struct stat sb;
2837 if (a->diff_staged) {
2838 if (staged_status != GOT_STATUS_MODIFY &&
2839 staged_status != GOT_STATUS_ADD &&
2840 staged_status != GOT_STATUS_DELETE)
2841 return NULL;
2842 } else {
2843 if (staged_status == GOT_STATUS_DELETE)
2844 return NULL;
2845 if (status == GOT_STATUS_NONEXISTENT)
2846 return got_error_set_errno(ENOENT, path);
2847 if (status != GOT_STATUS_MODIFY &&
2848 status != GOT_STATUS_ADD &&
2849 status != GOT_STATUS_DELETE &&
2850 status != GOT_STATUS_CONFLICT)
2851 return NULL;
2854 if (!a->header_shown) {
2855 printf("diff %s %s%s\n", a->id_str,
2856 got_worktree_get_root_path(a->worktree),
2857 a->diff_staged ? " (staged changes)" : "");
2858 a->header_shown = 1;
2861 if (a->diff_staged) {
2862 const char *label1 = NULL, *label2 = NULL;
2863 switch (staged_status) {
2864 case GOT_STATUS_MODIFY:
2865 label1 = path;
2866 label2 = path;
2867 break;
2868 case GOT_STATUS_ADD:
2869 label2 = path;
2870 break;
2871 case GOT_STATUS_DELETE:
2872 label1 = path;
2873 break;
2874 default:
2875 return got_error(GOT_ERR_FILE_STATUS);
2877 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2878 label1, label2, a->diff_context, a->ignore_whitespace,
2879 a->repo, stdout);
2882 if (staged_status == GOT_STATUS_ADD ||
2883 staged_status == GOT_STATUS_MODIFY) {
2884 char *id_str;
2885 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2886 8192);
2887 if (err)
2888 goto done;
2889 err = got_object_id_str(&id_str, staged_blob_id);
2890 if (err)
2891 goto done;
2892 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2893 err = got_error_from_errno("asprintf");
2894 free(id_str);
2895 goto done;
2897 free(id_str);
2898 } else if (status != GOT_STATUS_ADD) {
2899 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2900 if (err)
2901 goto done;
2904 if (status != GOT_STATUS_DELETE) {
2905 if (asprintf(&abspath, "%s/%s",
2906 got_worktree_get_root_path(a->worktree), path) == -1) {
2907 err = got_error_from_errno("asprintf");
2908 goto done;
2911 if (dirfd != -1) {
2912 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2913 if (fd == -1) {
2914 err = got_error_from_errno2("openat", abspath);
2915 goto done;
2917 } else {
2918 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2919 if (fd == -1) {
2920 err = got_error_from_errno2("open", abspath);
2921 goto done;
2924 if (fstat(fd, &sb) == -1) {
2925 err = got_error_from_errno2("fstat", abspath);
2926 goto done;
2928 f2 = fdopen(fd, "r");
2929 if (f2 == NULL) {
2930 err = got_error_from_errno2("fdopen", abspath);
2931 goto done;
2933 fd = -1;
2934 } else
2935 sb.st_size = 0;
2937 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2938 a->diff_context, a->ignore_whitespace, stdout);
2939 done:
2940 if (blob1)
2941 got_object_blob_close(blob1);
2942 if (f2 && fclose(f2) == EOF && err == NULL)
2943 err = got_error_from_errno("fclose");
2944 if (fd != -1 && close(fd) == -1 && err == NULL)
2945 err = got_error_from_errno("close");
2946 free(abspath);
2947 return err;
2950 static const struct got_error *
2951 cmd_diff(int argc, char *argv[])
2953 const struct got_error *error;
2954 struct got_repository *repo = NULL;
2955 struct got_worktree *worktree = NULL;
2956 char *cwd = NULL, *repo_path = NULL;
2957 struct got_object_id *id1 = NULL, *id2 = NULL;
2958 const char *id_str1 = NULL, *id_str2 = NULL;
2959 char *label1 = NULL, *label2 = NULL;
2960 int type1, type2;
2961 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2962 const char *errstr;
2963 char *path = NULL;
2965 #ifndef PROFILE
2966 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2967 NULL) == -1)
2968 err(1, "pledge");
2969 #endif
2971 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2972 switch (ch) {
2973 case 'C':
2974 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2975 &errstr);
2976 if (errstr != NULL)
2977 err(1, "-C option %s", errstr);
2978 break;
2979 case 'r':
2980 repo_path = realpath(optarg, NULL);
2981 if (repo_path == NULL)
2982 return got_error_from_errno2("realpath",
2983 optarg);
2984 got_path_strip_trailing_slashes(repo_path);
2985 break;
2986 case 's':
2987 diff_staged = 1;
2988 break;
2989 case 'w':
2990 ignore_whitespace = 1;
2991 break;
2992 default:
2993 usage_diff();
2994 /* NOTREACHED */
2998 argc -= optind;
2999 argv += optind;
3001 cwd = getcwd(NULL, 0);
3002 if (cwd == NULL) {
3003 error = got_error_from_errno("getcwd");
3004 goto done;
3006 error = got_worktree_open(&worktree, cwd);
3007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3008 goto done;
3009 if (argc <= 1) {
3010 if (worktree == NULL) {
3011 error = got_error(GOT_ERR_NOT_WORKTREE);
3012 goto done;
3014 if (repo_path)
3015 errx(1,
3016 "-r option can't be used when diffing a work tree");
3017 repo_path = strdup(got_worktree_get_repo_path(worktree));
3018 if (repo_path == NULL) {
3019 error = got_error_from_errno("strdup");
3020 goto done;
3022 if (argc == 1) {
3023 error = got_worktree_resolve_path(&path, worktree,
3024 argv[0]);
3025 if (error)
3026 goto done;
3027 } else {
3028 path = strdup("");
3029 if (path == NULL) {
3030 error = got_error_from_errno("strdup");
3031 goto done;
3034 } else if (argc == 2) {
3035 if (diff_staged)
3036 errx(1, "-s option can't be used when diffing "
3037 "objects in repository");
3038 id_str1 = argv[0];
3039 id_str2 = argv[1];
3040 if (worktree && repo_path == NULL) {
3041 repo_path =
3042 strdup(got_worktree_get_repo_path(worktree));
3043 if (repo_path == NULL) {
3044 error = got_error_from_errno("strdup");
3045 goto done;
3048 } else
3049 usage_diff();
3051 if (repo_path == NULL) {
3052 repo_path = getcwd(NULL, 0);
3053 if (repo_path == NULL)
3054 return got_error_from_errno("getcwd");
3057 error = got_repo_open(&repo, repo_path, NULL);
3058 free(repo_path);
3059 if (error != NULL)
3060 goto done;
3062 error = apply_unveil(got_repo_get_path(repo), 1,
3063 worktree ? got_worktree_get_root_path(worktree) : NULL);
3064 if (error)
3065 goto done;
3067 if (argc <= 1) {
3068 struct print_diff_arg arg;
3069 struct got_pathlist_head paths;
3070 char *id_str;
3072 TAILQ_INIT(&paths);
3074 error = got_object_id_str(&id_str,
3075 got_worktree_get_base_commit_id(worktree));
3076 if (error)
3077 goto done;
3078 arg.repo = repo;
3079 arg.worktree = worktree;
3080 arg.diff_context = diff_context;
3081 arg.id_str = id_str;
3082 arg.header_shown = 0;
3083 arg.diff_staged = diff_staged;
3084 arg.ignore_whitespace = ignore_whitespace;
3086 error = got_pathlist_append(&paths, path, NULL);
3087 if (error)
3088 goto done;
3090 error = got_worktree_status(worktree, &paths, repo, print_diff,
3091 &arg, check_cancelled, NULL);
3092 free(id_str);
3093 got_pathlist_free(&paths);
3094 goto done;
3097 error = got_repo_match_object_id(&id1, &label1, id_str1,
3098 GOT_OBJ_TYPE_ANY, 1, repo);
3099 if (error)
3100 goto done;
3102 error = got_repo_match_object_id(&id2, &label2, id_str2,
3103 GOT_OBJ_TYPE_ANY, 1, repo);
3104 if (error)
3105 goto done;
3107 error = got_object_get_type(&type1, repo, id1);
3108 if (error)
3109 goto done;
3111 error = got_object_get_type(&type2, repo, id2);
3112 if (error)
3113 goto done;
3115 if (type1 != type2) {
3116 error = got_error(GOT_ERR_OBJ_TYPE);
3117 goto done;
3120 switch (type1) {
3121 case GOT_OBJ_TYPE_BLOB:
3122 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3123 diff_context, ignore_whitespace, repo, stdout);
3124 break;
3125 case GOT_OBJ_TYPE_TREE:
3126 error = got_diff_objects_as_trees(id1, id2, "", "",
3127 diff_context, ignore_whitespace, repo, stdout);
3128 break;
3129 case GOT_OBJ_TYPE_COMMIT:
3130 printf("diff %s %s\n", label1, label2);
3131 error = got_diff_objects_as_commits(id1, id2, diff_context,
3132 ignore_whitespace, repo, stdout);
3133 break;
3134 default:
3135 error = got_error(GOT_ERR_OBJ_TYPE);
3137 done:
3138 free(label1);
3139 free(label2);
3140 free(id1);
3141 free(id2);
3142 free(path);
3143 if (worktree)
3144 got_worktree_close(worktree);
3145 if (repo) {
3146 const struct got_error *repo_error;
3147 repo_error = got_repo_close(repo);
3148 if (error == NULL)
3149 error = repo_error;
3151 return error;
3154 __dead static void
3155 usage_blame(void)
3157 fprintf(stderr,
3158 "usage: %s blame [-c commit] [-r repository-path] path\n",
3159 getprogname());
3160 exit(1);
3163 struct blame_line {
3164 int annotated;
3165 char *id_str;
3166 char *committer;
3167 char datebuf[11]; /* YYYY-MM-DD + NUL */
3170 struct blame_cb_args {
3171 struct blame_line *lines;
3172 int nlines;
3173 int nlines_prec;
3174 int lineno_cur;
3175 off_t *line_offsets;
3176 FILE *f;
3177 struct got_repository *repo;
3180 static const struct got_error *
3181 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3183 const struct got_error *err = NULL;
3184 struct blame_cb_args *a = arg;
3185 struct blame_line *bline;
3186 char *line = NULL;
3187 size_t linesize = 0;
3188 struct got_commit_object *commit = NULL;
3189 off_t offset;
3190 struct tm tm;
3191 time_t committer_time;
3193 if (nlines != a->nlines ||
3194 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3195 return got_error(GOT_ERR_RANGE);
3197 if (sigint_received)
3198 return got_error(GOT_ERR_ITER_COMPLETED);
3200 if (lineno == -1)
3201 return NULL; /* no change in this commit */
3203 /* Annotate this line. */
3204 bline = &a->lines[lineno - 1];
3205 if (bline->annotated)
3206 return NULL;
3207 err = got_object_id_str(&bline->id_str, id);
3208 if (err)
3209 return err;
3211 err = got_object_open_as_commit(&commit, a->repo, id);
3212 if (err)
3213 goto done;
3215 bline->committer = strdup(got_object_commit_get_committer(commit));
3216 if (bline->committer == NULL) {
3217 err = got_error_from_errno("strdup");
3218 goto done;
3221 committer_time = got_object_commit_get_committer_time(commit);
3222 if (localtime_r(&committer_time, &tm) == NULL)
3223 return got_error_from_errno("localtime_r");
3224 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3225 &tm) >= sizeof(bline->datebuf)) {
3226 err = got_error(GOT_ERR_NO_SPACE);
3227 goto done;
3229 bline->annotated = 1;
3231 /* Print lines annotated so far. */
3232 bline = &a->lines[a->lineno_cur - 1];
3233 if (!bline->annotated)
3234 goto done;
3236 offset = a->line_offsets[a->lineno_cur - 1];
3237 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3238 err = got_error_from_errno("fseeko");
3239 goto done;
3242 while (bline->annotated) {
3243 char *smallerthan, *at, *nl, *committer;
3244 size_t len;
3246 if (getline(&line, &linesize, a->f) == -1) {
3247 if (ferror(a->f))
3248 err = got_error_from_errno("getline");
3249 break;
3252 committer = bline->committer;
3253 smallerthan = strchr(committer, '<');
3254 if (smallerthan && smallerthan[1] != '\0')
3255 committer = smallerthan + 1;
3256 at = strchr(committer, '@');
3257 if (at)
3258 *at = '\0';
3259 len = strlen(committer);
3260 if (len >= 9)
3261 committer[8] = '\0';
3263 nl = strchr(line, '\n');
3264 if (nl)
3265 *nl = '\0';
3266 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3267 bline->id_str, bline->datebuf, committer, line);
3269 a->lineno_cur++;
3270 bline = &a->lines[a->lineno_cur - 1];
3272 done:
3273 if (commit)
3274 got_object_commit_close(commit);
3275 free(line);
3276 return err;
3279 static const struct got_error *
3280 cmd_blame(int argc, char *argv[])
3282 const struct got_error *error;
3283 struct got_repository *repo = NULL;
3284 struct got_worktree *worktree = NULL;
3285 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3286 struct got_object_id *obj_id = NULL;
3287 struct got_object_id *commit_id = NULL;
3288 struct got_blob_object *blob = NULL;
3289 char *commit_id_str = NULL;
3290 struct blame_cb_args bca;
3291 int ch, obj_type, i;
3292 size_t filesize;
3294 memset(&bca, 0, sizeof(bca));
3296 #ifndef PROFILE
3297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3298 NULL) == -1)
3299 err(1, "pledge");
3300 #endif
3302 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3303 switch (ch) {
3304 case 'c':
3305 commit_id_str = optarg;
3306 break;
3307 case 'r':
3308 repo_path = realpath(optarg, NULL);
3309 if (repo_path == NULL)
3310 return got_error_from_errno2("realpath",
3311 optarg);
3312 got_path_strip_trailing_slashes(repo_path);
3313 break;
3314 default:
3315 usage_blame();
3316 /* NOTREACHED */
3320 argc -= optind;
3321 argv += optind;
3323 if (argc == 1)
3324 path = argv[0];
3325 else
3326 usage_blame();
3328 cwd = getcwd(NULL, 0);
3329 if (cwd == NULL) {
3330 error = got_error_from_errno("getcwd");
3331 goto done;
3333 if (repo_path == NULL) {
3334 error = got_worktree_open(&worktree, cwd);
3335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3336 goto done;
3337 else
3338 error = NULL;
3339 if (worktree) {
3340 repo_path =
3341 strdup(got_worktree_get_repo_path(worktree));
3342 if (repo_path == NULL)
3343 error = got_error_from_errno("strdup");
3344 if (error)
3345 goto done;
3346 } else {
3347 repo_path = strdup(cwd);
3348 if (repo_path == NULL) {
3349 error = got_error_from_errno("strdup");
3350 goto done;
3355 error = got_repo_open(&repo, repo_path, NULL);
3356 if (error != NULL)
3357 goto done;
3359 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3360 if (error)
3361 goto done;
3363 if (worktree) {
3364 const char *prefix = got_worktree_get_path_prefix(worktree);
3365 char *p, *worktree_subdir = cwd +
3366 strlen(got_worktree_get_root_path(worktree));
3367 if (asprintf(&p, "%s%s%s%s%s",
3368 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3369 worktree_subdir, worktree_subdir[0] ? "/" : "",
3370 path) == -1) {
3371 error = got_error_from_errno("asprintf");
3372 goto done;
3374 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3375 free(p);
3376 } else {
3377 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3379 if (error)
3380 goto done;
3382 if (commit_id_str == NULL) {
3383 struct got_reference *head_ref;
3384 error = got_ref_open(&head_ref, repo, worktree ?
3385 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3386 if (error != NULL)
3387 goto done;
3388 error = got_ref_resolve(&commit_id, repo, head_ref);
3389 got_ref_close(head_ref);
3390 if (error != NULL)
3391 goto done;
3392 } else {
3393 error = got_repo_match_object_id(&commit_id, NULL,
3394 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3395 if (error)
3396 goto done;
3399 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3400 if (error)
3401 goto done;
3403 error = got_object_get_type(&obj_type, repo, obj_id);
3404 if (error)
3405 goto done;
3407 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3408 error = got_error(GOT_ERR_OBJ_TYPE);
3409 goto done;
3412 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3413 if (error)
3414 goto done;
3415 bca.f = got_opentemp();
3416 if (bca.f == NULL) {
3417 error = got_error_from_errno("got_opentemp");
3418 goto done;
3420 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3421 &bca.line_offsets, bca.f, blob);
3422 if (error || bca.nlines == 0)
3423 goto done;
3425 /* Don't include \n at EOF in the blame line count. */
3426 if (bca.line_offsets[bca.nlines - 1] == filesize)
3427 bca.nlines--;
3429 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3430 if (bca.lines == NULL) {
3431 error = got_error_from_errno("calloc");
3432 goto done;
3434 bca.lineno_cur = 1;
3435 bca.nlines_prec = 0;
3436 i = bca.nlines;
3437 while (i > 0) {
3438 i /= 10;
3439 bca.nlines_prec++;
3441 bca.repo = repo;
3443 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3444 check_cancelled, NULL);
3445 done:
3446 free(in_repo_path);
3447 free(repo_path);
3448 free(cwd);
3449 free(commit_id);
3450 free(obj_id);
3451 if (blob)
3452 got_object_blob_close(blob);
3453 if (worktree)
3454 got_worktree_close(worktree);
3455 if (repo) {
3456 const struct got_error *repo_error;
3457 repo_error = got_repo_close(repo);
3458 if (error == NULL)
3459 error = repo_error;
3461 if (bca.lines) {
3462 for (i = 0; i < bca.nlines; i++) {
3463 struct blame_line *bline = &bca.lines[i];
3464 free(bline->id_str);
3465 free(bline->committer);
3467 free(bca.lines);
3469 free(bca.line_offsets);
3470 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3471 error = got_error_from_errno("fclose");
3472 return error;
3475 __dead static void
3476 usage_tree(void)
3478 fprintf(stderr,
3479 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3480 getprogname());
3481 exit(1);
3484 static void
3485 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3486 const char *root_path)
3488 int is_root_path = (strcmp(path, root_path) == 0);
3489 const char *modestr = "";
3490 mode_t mode = got_tree_entry_get_mode(te);
3492 path += strlen(root_path);
3493 while (path[0] == '/')
3494 path++;
3496 if (got_object_tree_entry_is_submodule(te))
3497 modestr = "$";
3498 else if (S_ISLNK(mode))
3499 modestr = "@";
3500 else if (S_ISDIR(mode))
3501 modestr = "/";
3502 else if (mode & S_IXUSR)
3503 modestr = "*";
3505 printf("%s%s%s%s%s\n", id ? id : "", path,
3506 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3509 static const struct got_error *
3510 print_tree(const char *path, struct got_object_id *commit_id,
3511 int show_ids, int recurse, const char *root_path,
3512 struct got_repository *repo)
3514 const struct got_error *err = NULL;
3515 struct got_object_id *tree_id = NULL;
3516 struct got_tree_object *tree = NULL;
3517 int nentries, i;
3519 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3520 if (err)
3521 goto done;
3523 err = got_object_open_as_tree(&tree, repo, tree_id);
3524 if (err)
3525 goto done;
3526 nentries = got_object_tree_get_nentries(tree);
3527 for (i = 0; i < nentries; i++) {
3528 struct got_tree_entry *te;
3529 char *id = NULL;
3531 if (sigint_received || sigpipe_received)
3532 break;
3534 te = got_object_tree_get_entry(tree, i);
3535 if (show_ids) {
3536 char *id_str;
3537 err = got_object_id_str(&id_str,
3538 got_tree_entry_get_id(te));
3539 if (err)
3540 goto done;
3541 if (asprintf(&id, "%s ", id_str) == -1) {
3542 err = got_error_from_errno("asprintf");
3543 free(id_str);
3544 goto done;
3546 free(id_str);
3548 print_entry(te, id, path, root_path);
3549 free(id);
3551 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3552 char *child_path;
3553 if (asprintf(&child_path, "%s%s%s", path,
3554 path[0] == '/' && path[1] == '\0' ? "" : "/",
3555 got_tree_entry_get_name(te)) == -1) {
3556 err = got_error_from_errno("asprintf");
3557 goto done;
3559 err = print_tree(child_path, commit_id, show_ids, 1,
3560 root_path, repo);
3561 free(child_path);
3562 if (err)
3563 goto done;
3566 done:
3567 if (tree)
3568 got_object_tree_close(tree);
3569 free(tree_id);
3570 return err;
3573 static const struct got_error *
3574 cmd_tree(int argc, char *argv[])
3576 const struct got_error *error;
3577 struct got_repository *repo = NULL;
3578 struct got_worktree *worktree = NULL;
3579 const char *path;
3580 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3581 struct got_object_id *commit_id = NULL;
3582 char *commit_id_str = NULL;
3583 int show_ids = 0, recurse = 0;
3584 int ch;
3586 #ifndef PROFILE
3587 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3588 NULL) == -1)
3589 err(1, "pledge");
3590 #endif
3592 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3593 switch (ch) {
3594 case 'c':
3595 commit_id_str = optarg;
3596 break;
3597 case 'r':
3598 repo_path = realpath(optarg, NULL);
3599 if (repo_path == NULL)
3600 return got_error_from_errno2("realpath",
3601 optarg);
3602 got_path_strip_trailing_slashes(repo_path);
3603 break;
3604 case 'i':
3605 show_ids = 1;
3606 break;
3607 case 'R':
3608 recurse = 1;
3609 break;
3610 default:
3611 usage_tree();
3612 /* NOTREACHED */
3616 argc -= optind;
3617 argv += optind;
3619 if (argc == 1)
3620 path = argv[0];
3621 else if (argc > 1)
3622 usage_tree();
3623 else
3624 path = NULL;
3626 cwd = getcwd(NULL, 0);
3627 if (cwd == NULL) {
3628 error = got_error_from_errno("getcwd");
3629 goto done;
3631 if (repo_path == NULL) {
3632 error = got_worktree_open(&worktree, cwd);
3633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3634 goto done;
3635 else
3636 error = NULL;
3637 if (worktree) {
3638 repo_path =
3639 strdup(got_worktree_get_repo_path(worktree));
3640 if (repo_path == NULL)
3641 error = got_error_from_errno("strdup");
3642 if (error)
3643 goto done;
3644 } else {
3645 repo_path = strdup(cwd);
3646 if (repo_path == NULL) {
3647 error = got_error_from_errno("strdup");
3648 goto done;
3653 error = got_repo_open(&repo, repo_path, NULL);
3654 if (error != NULL)
3655 goto done;
3657 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3658 if (error)
3659 goto done;
3661 if (path == NULL) {
3662 if (worktree) {
3663 char *p, *worktree_subdir = cwd +
3664 strlen(got_worktree_get_root_path(worktree));
3665 if (asprintf(&p, "%s/%s",
3666 got_worktree_get_path_prefix(worktree),
3667 worktree_subdir) == -1) {
3668 error = got_error_from_errno("asprintf");
3669 goto done;
3671 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3672 free(p);
3673 if (error)
3674 goto done;
3675 } else
3676 path = "/";
3678 if (in_repo_path == NULL) {
3679 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3680 if (error != NULL)
3681 goto done;
3684 if (commit_id_str == NULL) {
3685 struct got_reference *head_ref;
3686 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3687 if (error != NULL)
3688 goto done;
3689 error = got_ref_resolve(&commit_id, repo, head_ref);
3690 got_ref_close(head_ref);
3691 if (error != NULL)
3692 goto done;
3693 } else {
3694 error = got_repo_match_object_id(&commit_id, NULL,
3695 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3696 if (error)
3697 goto done;
3700 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3701 in_repo_path, repo);
3702 done:
3703 free(in_repo_path);
3704 free(repo_path);
3705 free(cwd);
3706 free(commit_id);
3707 if (worktree)
3708 got_worktree_close(worktree);
3709 if (repo) {
3710 const struct got_error *repo_error;
3711 repo_error = got_repo_close(repo);
3712 if (error == NULL)
3713 error = repo_error;
3715 return error;
3718 __dead static void
3719 usage_status(void)
3721 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3722 exit(1);
3725 static const struct got_error *
3726 print_status(void *arg, unsigned char status, unsigned char staged_status,
3727 const char *path, struct got_object_id *blob_id,
3728 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3729 int dirfd, const char *de_name)
3731 if (status == staged_status && (status == GOT_STATUS_DELETE))
3732 status = GOT_STATUS_NO_CHANGE;
3733 printf("%c%c %s\n", status, staged_status, path);
3734 return NULL;
3737 static const struct got_error *
3738 cmd_status(int argc, char *argv[])
3740 const struct got_error *error = NULL;
3741 struct got_repository *repo = NULL;
3742 struct got_worktree *worktree = NULL;
3743 char *cwd = NULL;
3744 struct got_pathlist_head paths;
3745 struct got_pathlist_entry *pe;
3746 int ch;
3748 TAILQ_INIT(&paths);
3750 while ((ch = getopt(argc, argv, "")) != -1) {
3751 switch (ch) {
3752 default:
3753 usage_status();
3754 /* NOTREACHED */
3758 argc -= optind;
3759 argv += optind;
3761 #ifndef PROFILE
3762 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3763 NULL) == -1)
3764 err(1, "pledge");
3765 #endif
3766 cwd = getcwd(NULL, 0);
3767 if (cwd == NULL) {
3768 error = got_error_from_errno("getcwd");
3769 goto done;
3772 error = got_worktree_open(&worktree, cwd);
3773 if (error != NULL)
3774 goto done;
3776 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3777 NULL);
3778 if (error != NULL)
3779 goto done;
3781 error = apply_unveil(got_repo_get_path(repo), 1,
3782 got_worktree_get_root_path(worktree));
3783 if (error)
3784 goto done;
3786 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3787 if (error)
3788 goto done;
3790 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3791 check_cancelled, NULL);
3792 done:
3793 TAILQ_FOREACH(pe, &paths, entry)
3794 free((char *)pe->path);
3795 got_pathlist_free(&paths);
3796 free(cwd);
3797 return error;
3800 __dead static void
3801 usage_ref(void)
3803 fprintf(stderr,
3804 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3805 getprogname());
3806 exit(1);
3809 static const struct got_error *
3810 list_refs(struct got_repository *repo)
3812 static const struct got_error *err = NULL;
3813 struct got_reflist_head refs;
3814 struct got_reflist_entry *re;
3816 SIMPLEQ_INIT(&refs);
3817 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3818 if (err)
3819 return err;
3821 SIMPLEQ_FOREACH(re, &refs, entry) {
3822 char *refstr;
3823 refstr = got_ref_to_str(re->ref);
3824 if (refstr == NULL)
3825 return got_error_from_errno("got_ref_to_str");
3826 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3827 free(refstr);
3830 got_ref_list_free(&refs);
3831 return NULL;
3834 static const struct got_error *
3835 delete_ref(struct got_repository *repo, const char *refname)
3837 const struct got_error *err = NULL;
3838 struct got_reference *ref;
3840 err = got_ref_open(&ref, repo, refname, 0);
3841 if (err)
3842 return err;
3844 err = got_ref_delete(ref, repo);
3845 got_ref_close(ref);
3846 return err;
3849 static const struct got_error *
3850 add_ref(struct got_repository *repo, const char *refname, const char *target)
3852 const struct got_error *err = NULL;
3853 struct got_object_id *id;
3854 struct got_reference *ref = NULL;
3857 * Don't let the user create a reference name with a leading '-'.
3858 * While technically a valid reference name, this case is usually
3859 * an unintended typo.
3861 if (refname[0] == '-')
3862 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3864 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3865 repo);
3866 if (err) {
3867 struct got_reference *target_ref;
3869 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3870 return err;
3871 err = got_ref_open(&target_ref, repo, target, 0);
3872 if (err)
3873 return err;
3874 err = got_ref_resolve(&id, repo, target_ref);
3875 got_ref_close(target_ref);
3876 if (err)
3877 return err;
3880 err = got_ref_alloc(&ref, refname, id);
3881 if (err)
3882 goto done;
3884 err = got_ref_write(ref, repo);
3885 done:
3886 if (ref)
3887 got_ref_close(ref);
3888 free(id);
3889 return err;
3892 static const struct got_error *
3893 add_symref(struct got_repository *repo, const char *refname, const char *target)
3895 const struct got_error *err = NULL;
3896 struct got_reference *ref = NULL;
3897 struct got_reference *target_ref = NULL;
3900 * Don't let the user create a reference name with a leading '-'.
3901 * While technically a valid reference name, this case is usually
3902 * an unintended typo.
3904 if (refname[0] == '-')
3905 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3907 err = got_ref_open(&target_ref, repo, target, 0);
3908 if (err)
3909 return err;
3911 err = got_ref_alloc_symref(&ref, refname, target_ref);
3912 if (err)
3913 goto done;
3915 err = got_ref_write(ref, repo);
3916 done:
3917 if (target_ref)
3918 got_ref_close(target_ref);
3919 if (ref)
3920 got_ref_close(ref);
3921 return err;
3924 static const struct got_error *
3925 cmd_ref(int argc, char *argv[])
3927 const struct got_error *error = NULL;
3928 struct got_repository *repo = NULL;
3929 struct got_worktree *worktree = NULL;
3930 char *cwd = NULL, *repo_path = NULL;
3931 int ch, do_list = 0, create_symref = 0;
3932 const char *delref = NULL;
3934 /* TODO: Add -s option for adding symbolic references. */
3935 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3936 switch (ch) {
3937 case 'd':
3938 delref = optarg;
3939 break;
3940 case 'r':
3941 repo_path = realpath(optarg, NULL);
3942 if (repo_path == NULL)
3943 return got_error_from_errno2("realpath",
3944 optarg);
3945 got_path_strip_trailing_slashes(repo_path);
3946 break;
3947 case 'l':
3948 do_list = 1;
3949 break;
3950 case 's':
3951 create_symref = 1;
3952 break;
3953 default:
3954 usage_ref();
3955 /* NOTREACHED */
3959 if (do_list && delref)
3960 errx(1, "-l and -d options are mutually exclusive\n");
3962 argc -= optind;
3963 argv += optind;
3965 if (do_list || delref) {
3966 if (create_symref)
3967 errx(1, "-s option cannot be used together with the "
3968 "-l or -d options");
3969 if (argc > 0)
3970 usage_ref();
3971 } else if (argc != 2)
3972 usage_ref();
3974 #ifndef PROFILE
3975 if (do_list) {
3976 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3977 NULL) == -1)
3978 err(1, "pledge");
3979 } else {
3980 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3981 "sendfd unveil", NULL) == -1)
3982 err(1, "pledge");
3984 #endif
3985 cwd = getcwd(NULL, 0);
3986 if (cwd == NULL) {
3987 error = got_error_from_errno("getcwd");
3988 goto done;
3991 if (repo_path == NULL) {
3992 error = got_worktree_open(&worktree, cwd);
3993 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3994 goto done;
3995 else
3996 error = NULL;
3997 if (worktree) {
3998 repo_path =
3999 strdup(got_worktree_get_repo_path(worktree));
4000 if (repo_path == NULL)
4001 error = got_error_from_errno("strdup");
4002 if (error)
4003 goto done;
4004 } else {
4005 repo_path = strdup(cwd);
4006 if (repo_path == NULL) {
4007 error = got_error_from_errno("strdup");
4008 goto done;
4013 error = got_repo_open(&repo, repo_path, NULL);
4014 if (error != NULL)
4015 goto done;
4017 error = apply_unveil(got_repo_get_path(repo), do_list,
4018 worktree ? got_worktree_get_root_path(worktree) : NULL);
4019 if (error)
4020 goto done;
4022 if (do_list)
4023 error = list_refs(repo);
4024 else if (delref)
4025 error = delete_ref(repo, delref);
4026 else if (create_symref)
4027 error = add_symref(repo, argv[0], argv[1]);
4028 else
4029 error = add_ref(repo, argv[0], argv[1]);
4030 done:
4031 if (repo)
4032 got_repo_close(repo);
4033 if (worktree)
4034 got_worktree_close(worktree);
4035 free(cwd);
4036 free(repo_path);
4037 return error;
4040 __dead static void
4041 usage_branch(void)
4043 fprintf(stderr,
4044 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4045 "[name]\n", getprogname());
4046 exit(1);
4049 static const struct got_error *
4050 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4051 struct got_reference *ref)
4053 const struct got_error *err = NULL;
4054 const char *refname, *marker = " ";
4055 char *refstr;
4057 refname = got_ref_get_name(ref);
4058 if (worktree && strcmp(refname,
4059 got_worktree_get_head_ref_name(worktree)) == 0) {
4060 struct got_object_id *id = NULL;
4062 err = got_ref_resolve(&id, repo, ref);
4063 if (err)
4064 return err;
4065 if (got_object_id_cmp(id,
4066 got_worktree_get_base_commit_id(worktree)) == 0)
4067 marker = "* ";
4068 else
4069 marker = "~ ";
4070 free(id);
4073 if (strncmp(refname, "refs/heads/", 11) == 0)
4074 refname += 11;
4075 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4076 refname += 18;
4078 refstr = got_ref_to_str(ref);
4079 if (refstr == NULL)
4080 return got_error_from_errno("got_ref_to_str");
4082 printf("%s%s: %s\n", marker, refname, refstr);
4083 free(refstr);
4084 return NULL;
4087 static const struct got_error *
4088 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4090 const char *refname;
4092 if (worktree == NULL)
4093 return got_error(GOT_ERR_NOT_WORKTREE);
4095 refname = got_worktree_get_head_ref_name(worktree);
4097 if (strncmp(refname, "refs/heads/", 11) == 0)
4098 refname += 11;
4099 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4100 refname += 18;
4102 printf("%s\n", refname);
4104 return NULL;
4107 static const struct got_error *
4108 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4110 static const struct got_error *err = NULL;
4111 struct got_reflist_head refs;
4112 struct got_reflist_entry *re;
4113 struct got_reference *temp_ref = NULL;
4114 int rebase_in_progress, histedit_in_progress;
4116 SIMPLEQ_INIT(&refs);
4118 if (worktree) {
4119 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4120 worktree);
4121 if (err)
4122 return err;
4124 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4125 worktree);
4126 if (err)
4127 return err;
4129 if (rebase_in_progress || histedit_in_progress) {
4130 err = got_ref_open(&temp_ref, repo,
4131 got_worktree_get_head_ref_name(worktree), 0);
4132 if (err)
4133 return err;
4134 list_branch(repo, worktree, temp_ref);
4135 got_ref_close(temp_ref);
4139 err = got_ref_list(&refs, repo, "refs/heads",
4140 got_ref_cmp_by_name, NULL);
4141 if (err)
4142 return err;
4144 SIMPLEQ_FOREACH(re, &refs, entry)
4145 list_branch(repo, worktree, re->ref);
4147 got_ref_list_free(&refs);
4148 return NULL;
4151 static const struct got_error *
4152 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4153 const char *branch_name)
4155 const struct got_error *err = NULL;
4156 struct got_reference *ref = NULL;
4157 char *refname;
4159 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4160 return got_error_from_errno("asprintf");
4162 err = got_ref_open(&ref, repo, refname, 0);
4163 if (err)
4164 goto done;
4166 if (worktree &&
4167 strcmp(got_worktree_get_head_ref_name(worktree),
4168 got_ref_get_name(ref)) == 0) {
4169 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4170 "will not delete this work tree's current branch");
4171 goto done;
4174 err = got_ref_delete(ref, repo);
4175 done:
4176 if (ref)
4177 got_ref_close(ref);
4178 free(refname);
4179 return err;
4182 static const struct got_error *
4183 add_branch(struct got_repository *repo, const char *branch_name,
4184 struct got_object_id *base_commit_id)
4186 const struct got_error *err = NULL;
4187 struct got_reference *ref = NULL;
4188 char *base_refname = NULL, *refname = NULL;
4191 * Don't let the user create a branch name with a leading '-'.
4192 * While technically a valid reference name, this case is usually
4193 * an unintended typo.
4195 if (branch_name[0] == '-')
4196 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4198 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4199 err = got_error_from_errno("asprintf");
4200 goto done;
4203 err = got_ref_open(&ref, repo, refname, 0);
4204 if (err == NULL) {
4205 err = got_error(GOT_ERR_BRANCH_EXISTS);
4206 goto done;
4207 } else if (err->code != GOT_ERR_NOT_REF)
4208 goto done;
4210 err = got_ref_alloc(&ref, refname, base_commit_id);
4211 if (err)
4212 goto done;
4214 err = got_ref_write(ref, repo);
4215 done:
4216 if (ref)
4217 got_ref_close(ref);
4218 free(base_refname);
4219 free(refname);
4220 return err;
4223 static const struct got_error *
4224 cmd_branch(int argc, char *argv[])
4226 const struct got_error *error = NULL;
4227 struct got_repository *repo = NULL;
4228 struct got_worktree *worktree = NULL;
4229 char *cwd = NULL, *repo_path = NULL;
4230 int ch, do_list = 0, do_show = 0, do_update = 1;
4231 const char *delref = NULL, *commit_id_arg = NULL;
4232 struct got_reference *ref = NULL;
4233 struct got_pathlist_head paths;
4234 struct got_pathlist_entry *pe;
4235 struct got_object_id *commit_id = NULL;
4236 char *commit_id_str = NULL;
4238 TAILQ_INIT(&paths);
4240 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4241 switch (ch) {
4242 case 'c':
4243 commit_id_arg = optarg;
4244 break;
4245 case 'd':
4246 delref = optarg;
4247 break;
4248 case 'r':
4249 repo_path = realpath(optarg, NULL);
4250 if (repo_path == NULL)
4251 return got_error_from_errno2("realpath",
4252 optarg);
4253 got_path_strip_trailing_slashes(repo_path);
4254 break;
4255 case 'l':
4256 do_list = 1;
4257 break;
4258 case 'n':
4259 do_update = 0;
4260 break;
4261 default:
4262 usage_branch();
4263 /* NOTREACHED */
4267 if (do_list && delref)
4268 errx(1, "-l and -d options are mutually exclusive\n");
4270 argc -= optind;
4271 argv += optind;
4273 if (!do_list && !delref && argc == 0)
4274 do_show = 1;
4276 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4277 errx(1, "-c option can only be used when creating a branch");
4279 if (do_list || delref) {
4280 if (argc > 0)
4281 usage_branch();
4282 } else if (!do_show && argc != 1)
4283 usage_branch();
4285 #ifndef PROFILE
4286 if (do_list || do_show) {
4287 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4288 NULL) == -1)
4289 err(1, "pledge");
4290 } else {
4291 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4292 "sendfd unveil", NULL) == -1)
4293 err(1, "pledge");
4295 #endif
4296 cwd = getcwd(NULL, 0);
4297 if (cwd == NULL) {
4298 error = got_error_from_errno("getcwd");
4299 goto done;
4302 if (repo_path == NULL) {
4303 error = got_worktree_open(&worktree, cwd);
4304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4305 goto done;
4306 else
4307 error = NULL;
4308 if (worktree) {
4309 repo_path =
4310 strdup(got_worktree_get_repo_path(worktree));
4311 if (repo_path == NULL)
4312 error = got_error_from_errno("strdup");
4313 if (error)
4314 goto done;
4315 } else {
4316 repo_path = strdup(cwd);
4317 if (repo_path == NULL) {
4318 error = got_error_from_errno("strdup");
4319 goto done;
4324 error = got_repo_open(&repo, repo_path, NULL);
4325 if (error != NULL)
4326 goto done;
4328 error = apply_unveil(got_repo_get_path(repo), do_list,
4329 worktree ? got_worktree_get_root_path(worktree) : NULL);
4330 if (error)
4331 goto done;
4333 if (do_show)
4334 error = show_current_branch(repo, worktree);
4335 else if (do_list)
4336 error = list_branches(repo, worktree);
4337 else if (delref)
4338 error = delete_branch(repo, worktree, delref);
4339 else {
4340 if (commit_id_arg == NULL)
4341 commit_id_arg = worktree ?
4342 got_worktree_get_head_ref_name(worktree) :
4343 GOT_REF_HEAD;
4344 error = got_repo_match_object_id(&commit_id, NULL,
4345 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4346 if (error)
4347 goto done;
4348 error = add_branch(repo, argv[0], commit_id);
4349 if (error)
4350 goto done;
4351 if (worktree && do_update) {
4352 int did_something = 0;
4353 char *branch_refname = NULL;
4355 error = got_object_id_str(&commit_id_str, commit_id);
4356 if (error)
4357 goto done;
4358 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4359 worktree);
4360 if (error)
4361 goto done;
4362 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4363 == -1) {
4364 error = got_error_from_errno("asprintf");
4365 goto done;
4367 error = got_ref_open(&ref, repo, branch_refname, 0);
4368 free(branch_refname);
4369 if (error)
4370 goto done;
4371 error = switch_head_ref(ref, commit_id, worktree,
4372 repo);
4373 if (error)
4374 goto done;
4375 error = got_worktree_set_base_commit_id(worktree, repo,
4376 commit_id);
4377 if (error)
4378 goto done;
4379 error = got_worktree_checkout_files(worktree, &paths,
4380 repo, update_progress, &did_something,
4381 check_cancelled, NULL);
4382 if (error)
4383 goto done;
4384 if (did_something)
4385 printf("Updated to commit %s\n", commit_id_str);
4388 done:
4389 if (ref)
4390 got_ref_close(ref);
4391 if (repo)
4392 got_repo_close(repo);
4393 if (worktree)
4394 got_worktree_close(worktree);
4395 free(cwd);
4396 free(repo_path);
4397 free(commit_id);
4398 free(commit_id_str);
4399 TAILQ_FOREACH(pe, &paths, entry)
4400 free((char *)pe->path);
4401 got_pathlist_free(&paths);
4402 return error;
4406 __dead static void
4407 usage_tag(void)
4409 fprintf(stderr,
4410 "usage: %s tag [-c commit] [-r repository] [-l] "
4411 "[-m message] name\n", getprogname());
4412 exit(1);
4415 #if 0
4416 static const struct got_error *
4417 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4419 const struct got_error *err = NULL;
4420 struct got_reflist_entry *re, *se, *new;
4421 struct got_object_id *re_id, *se_id;
4422 struct got_tag_object *re_tag, *se_tag;
4423 time_t re_time, se_time;
4425 SIMPLEQ_FOREACH(re, tags, entry) {
4426 se = SIMPLEQ_FIRST(sorted);
4427 if (se == NULL) {
4428 err = got_reflist_entry_dup(&new, re);
4429 if (err)
4430 return err;
4431 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4432 continue;
4433 } else {
4434 err = got_ref_resolve(&re_id, repo, re->ref);
4435 if (err)
4436 break;
4437 err = got_object_open_as_tag(&re_tag, repo, re_id);
4438 free(re_id);
4439 if (err)
4440 break;
4441 re_time = got_object_tag_get_tagger_time(re_tag);
4442 got_object_tag_close(re_tag);
4445 while (se) {
4446 err = got_ref_resolve(&se_id, repo, re->ref);
4447 if (err)
4448 break;
4449 err = got_object_open_as_tag(&se_tag, repo, se_id);
4450 free(se_id);
4451 if (err)
4452 break;
4453 se_time = got_object_tag_get_tagger_time(se_tag);
4454 got_object_tag_close(se_tag);
4456 if (se_time > re_time) {
4457 err = got_reflist_entry_dup(&new, re);
4458 if (err)
4459 return err;
4460 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4461 break;
4463 se = SIMPLEQ_NEXT(se, entry);
4464 continue;
4467 done:
4468 return err;
4470 #endif
4472 static const struct got_error *
4473 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4475 static const struct got_error *err = NULL;
4476 struct got_reflist_head refs;
4477 struct got_reflist_entry *re;
4479 SIMPLEQ_INIT(&refs);
4481 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4482 if (err)
4483 return err;
4485 SIMPLEQ_FOREACH(re, &refs, entry) {
4486 const char *refname;
4487 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4488 char datebuf[26];
4489 const char *tagger;
4490 time_t tagger_time;
4491 struct got_object_id *id;
4492 struct got_tag_object *tag;
4493 struct got_commit_object *commit = NULL;
4495 refname = got_ref_get_name(re->ref);
4496 if (strncmp(refname, "refs/tags/", 10) != 0)
4497 continue;
4498 refname += 10;
4499 refstr = got_ref_to_str(re->ref);
4500 if (refstr == NULL) {
4501 err = got_error_from_errno("got_ref_to_str");
4502 break;
4504 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4505 free(refstr);
4507 err = got_ref_resolve(&id, repo, re->ref);
4508 if (err)
4509 break;
4510 err = got_object_open_as_tag(&tag, repo, id);
4511 if (err) {
4512 if (err->code != GOT_ERR_OBJ_TYPE) {
4513 free(id);
4514 break;
4516 /* "lightweight" tag */
4517 err = got_object_open_as_commit(&commit, repo, id);
4518 if (err) {
4519 free(id);
4520 break;
4522 tagger = got_object_commit_get_committer(commit);
4523 tagger_time =
4524 got_object_commit_get_committer_time(commit);
4525 err = got_object_id_str(&id_str, id);
4526 free(id);
4527 if (err)
4528 break;
4529 } else {
4530 free(id);
4531 tagger = got_object_tag_get_tagger(tag);
4532 tagger_time = got_object_tag_get_tagger_time(tag);
4533 err = got_object_id_str(&id_str,
4534 got_object_tag_get_object_id(tag));
4535 if (err)
4536 break;
4538 printf("from: %s\n", tagger);
4539 datestr = get_datestr(&tagger_time, datebuf);
4540 if (datestr)
4541 printf("date: %s UTC\n", datestr);
4542 if (commit)
4543 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4544 else {
4545 switch (got_object_tag_get_object_type(tag)) {
4546 case GOT_OBJ_TYPE_BLOB:
4547 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4548 id_str);
4549 break;
4550 case GOT_OBJ_TYPE_TREE:
4551 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4552 id_str);
4553 break;
4554 case GOT_OBJ_TYPE_COMMIT:
4555 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4556 id_str);
4557 break;
4558 case GOT_OBJ_TYPE_TAG:
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4560 id_str);
4561 break;
4562 default:
4563 break;
4566 free(id_str);
4567 if (commit) {
4568 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4569 if (err)
4570 break;
4571 got_object_commit_close(commit);
4572 } else {
4573 tagmsg0 = strdup(got_object_tag_get_message(tag));
4574 got_object_tag_close(tag);
4575 if (tagmsg0 == NULL) {
4576 err = got_error_from_errno("strdup");
4577 break;
4581 tagmsg = tagmsg0;
4582 do {
4583 line = strsep(&tagmsg, "\n");
4584 if (line)
4585 printf(" %s\n", line);
4586 } while (line);
4587 free(tagmsg0);
4590 got_ref_list_free(&refs);
4591 return NULL;
4594 static const struct got_error *
4595 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4596 const char *tag_name, const char *repo_path)
4598 const struct got_error *err = NULL;
4599 char *template = NULL, *initial_content = NULL;
4600 char *editor = NULL;
4601 int fd = -1;
4603 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4604 err = got_error_from_errno("asprintf");
4605 goto done;
4608 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4609 commit_id_str, tag_name) == -1) {
4610 err = got_error_from_errno("asprintf");
4611 goto done;
4614 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4615 if (err)
4616 goto done;
4618 dprintf(fd, initial_content);
4619 close(fd);
4621 err = get_editor(&editor);
4622 if (err)
4623 goto done;
4624 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4625 done:
4626 free(initial_content);
4627 free(template);
4628 free(editor);
4630 /* Editor is done; we can now apply unveil(2) */
4631 if (err == NULL) {
4632 err = apply_unveil(repo_path, 0, NULL);
4633 if (err) {
4634 free(*tagmsg);
4635 *tagmsg = NULL;
4638 return err;
4641 static const struct got_error *
4642 add_tag(struct got_repository *repo, const char *tag_name,
4643 const char *commit_arg, const char *tagmsg_arg)
4645 const struct got_error *err = NULL;
4646 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4647 char *label = NULL, *commit_id_str = NULL;
4648 struct got_reference *ref = NULL;
4649 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4650 char *tagmsg_path = NULL, *tag_id_str = NULL;
4651 int preserve_tagmsg = 0;
4654 * Don't let the user create a tag name with a leading '-'.
4655 * While technically a valid reference name, this case is usually
4656 * an unintended typo.
4658 if (tag_name[0] == '-')
4659 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4661 err = get_author(&tagger, repo);
4662 if (err)
4663 return err;
4665 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4666 GOT_OBJ_TYPE_COMMIT, 1, repo);
4667 if (err)
4668 goto done;
4670 err = got_object_id_str(&commit_id_str, commit_id);
4671 if (err)
4672 goto done;
4674 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4675 refname = strdup(tag_name);
4676 if (refname == NULL) {
4677 err = got_error_from_errno("strdup");
4678 goto done;
4680 tag_name += 10;
4681 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4682 err = got_error_from_errno("asprintf");
4683 goto done;
4686 err = got_ref_open(&ref, repo, refname, 0);
4687 if (err == NULL) {
4688 err = got_error(GOT_ERR_TAG_EXISTS);
4689 goto done;
4690 } else if (err->code != GOT_ERR_NOT_REF)
4691 goto done;
4693 if (tagmsg_arg == NULL) {
4694 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4695 tag_name, got_repo_get_path(repo));
4696 if (err) {
4697 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4698 tagmsg_path != NULL)
4699 preserve_tagmsg = 1;
4700 goto done;
4704 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4705 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4706 if (err) {
4707 if (tagmsg_path)
4708 preserve_tagmsg = 1;
4709 goto done;
4712 err = got_ref_alloc(&ref, refname, tag_id);
4713 if (err) {
4714 if (tagmsg_path)
4715 preserve_tagmsg = 1;
4716 goto done;
4719 err = got_ref_write(ref, repo);
4720 if (err) {
4721 if (tagmsg_path)
4722 preserve_tagmsg = 1;
4723 goto done;
4726 err = got_object_id_str(&tag_id_str, tag_id);
4727 if (err) {
4728 if (tagmsg_path)
4729 preserve_tagmsg = 1;
4730 goto done;
4732 printf("Created tag %s\n", tag_id_str);
4733 done:
4734 if (preserve_tagmsg) {
4735 fprintf(stderr, "%s: tag message preserved in %s\n",
4736 getprogname(), tagmsg_path);
4737 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4738 err = got_error_from_errno2("unlink", tagmsg_path);
4739 free(tag_id_str);
4740 if (ref)
4741 got_ref_close(ref);
4742 free(commit_id);
4743 free(commit_id_str);
4744 free(refname);
4745 free(tagmsg);
4746 free(tagmsg_path);
4747 free(tagger);
4748 return err;
4751 static const struct got_error *
4752 cmd_tag(int argc, char *argv[])
4754 const struct got_error *error = NULL;
4755 struct got_repository *repo = NULL;
4756 struct got_worktree *worktree = NULL;
4757 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4758 char *gitconfig_path = NULL;
4759 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4760 int ch, do_list = 0;
4762 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4763 switch (ch) {
4764 case 'c':
4765 commit_id_arg = optarg;
4766 break;
4767 case 'm':
4768 tagmsg = optarg;
4769 break;
4770 case 'r':
4771 repo_path = realpath(optarg, NULL);
4772 if (repo_path == NULL)
4773 return got_error_from_errno2("realpath",
4774 optarg);
4775 got_path_strip_trailing_slashes(repo_path);
4776 break;
4777 case 'l':
4778 do_list = 1;
4779 break;
4780 default:
4781 usage_tag();
4782 /* NOTREACHED */
4786 argc -= optind;
4787 argv += optind;
4789 if (do_list) {
4790 if (commit_id_arg != NULL)
4791 errx(1, "-c option can only be used when creating a tag");
4792 if (tagmsg)
4793 errx(1, "-l and -m options are mutually exclusive");
4794 if (argc > 0)
4795 usage_tag();
4796 } else if (argc != 1)
4797 usage_tag();
4799 tag_name = argv[0];
4801 #ifndef PROFILE
4802 if (do_list) {
4803 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4804 NULL) == -1)
4805 err(1, "pledge");
4806 } else {
4807 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4808 "sendfd unveil", NULL) == -1)
4809 err(1, "pledge");
4811 #endif
4812 cwd = getcwd(NULL, 0);
4813 if (cwd == NULL) {
4814 error = got_error_from_errno("getcwd");
4815 goto done;
4818 if (repo_path == NULL) {
4819 error = got_worktree_open(&worktree, cwd);
4820 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4821 goto done;
4822 else
4823 error = NULL;
4824 if (worktree) {
4825 repo_path =
4826 strdup(got_worktree_get_repo_path(worktree));
4827 if (repo_path == NULL)
4828 error = got_error_from_errno("strdup");
4829 if (error)
4830 goto done;
4831 } else {
4832 repo_path = strdup(cwd);
4833 if (repo_path == NULL) {
4834 error = got_error_from_errno("strdup");
4835 goto done;
4840 if (do_list) {
4841 error = got_repo_open(&repo, repo_path, NULL);
4842 if (error != NULL)
4843 goto done;
4844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4845 if (error)
4846 goto done;
4847 error = list_tags(repo, worktree);
4848 } else {
4849 error = get_gitconfig_path(&gitconfig_path);
4850 if (error)
4851 goto done;
4852 error = got_repo_open(&repo, repo_path, gitconfig_path);
4853 if (error != NULL)
4854 goto done;
4856 if (tagmsg) {
4857 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4858 if (error)
4859 goto done;
4862 if (commit_id_arg == NULL) {
4863 struct got_reference *head_ref;
4864 struct got_object_id *commit_id;
4865 error = got_ref_open(&head_ref, repo,
4866 worktree ? got_worktree_get_head_ref_name(worktree)
4867 : GOT_REF_HEAD, 0);
4868 if (error)
4869 goto done;
4870 error = got_ref_resolve(&commit_id, repo, head_ref);
4871 got_ref_close(head_ref);
4872 if (error)
4873 goto done;
4874 error = got_object_id_str(&commit_id_str, commit_id);
4875 free(commit_id);
4876 if (error)
4877 goto done;
4880 error = add_tag(repo, tag_name,
4881 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4883 done:
4884 if (repo)
4885 got_repo_close(repo);
4886 if (worktree)
4887 got_worktree_close(worktree);
4888 free(cwd);
4889 free(repo_path);
4890 free(gitconfig_path);
4891 free(commit_id_str);
4892 return error;
4895 __dead static void
4896 usage_add(void)
4898 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4899 getprogname());
4900 exit(1);
4903 static const struct got_error *
4904 add_progress(void *arg, unsigned char status, const char *path)
4906 while (path[0] == '/')
4907 path++;
4908 printf("%c %s\n", status, path);
4909 return NULL;
4912 static const struct got_error *
4913 cmd_add(int argc, char *argv[])
4915 const struct got_error *error = NULL;
4916 struct got_repository *repo = NULL;
4917 struct got_worktree *worktree = NULL;
4918 char *cwd = NULL;
4919 struct got_pathlist_head paths;
4920 struct got_pathlist_entry *pe;
4921 int ch, can_recurse = 0, no_ignores = 0;
4923 TAILQ_INIT(&paths);
4925 while ((ch = getopt(argc, argv, "IR")) != -1) {
4926 switch (ch) {
4927 case 'I':
4928 no_ignores = 1;
4929 break;
4930 case 'R':
4931 can_recurse = 1;
4932 break;
4933 default:
4934 usage_add();
4935 /* NOTREACHED */
4939 argc -= optind;
4940 argv += optind;
4942 #ifndef PROFILE
4943 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4944 NULL) == -1)
4945 err(1, "pledge");
4946 #endif
4947 if (argc < 1)
4948 usage_add();
4950 cwd = getcwd(NULL, 0);
4951 if (cwd == NULL) {
4952 error = got_error_from_errno("getcwd");
4953 goto done;
4956 error = got_worktree_open(&worktree, cwd);
4957 if (error)
4958 goto done;
4960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4961 NULL);
4962 if (error != NULL)
4963 goto done;
4965 error = apply_unveil(got_repo_get_path(repo), 1,
4966 got_worktree_get_root_path(worktree));
4967 if (error)
4968 goto done;
4970 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4971 if (error)
4972 goto done;
4974 if (!can_recurse && no_ignores) {
4975 error = got_error_msg(GOT_ERR_BAD_PATH,
4976 "disregarding ignores requires -R option");
4977 goto done;
4981 if (!can_recurse) {
4982 char *ondisk_path;
4983 struct stat sb;
4984 TAILQ_FOREACH(pe, &paths, entry) {
4985 if (asprintf(&ondisk_path, "%s/%s",
4986 got_worktree_get_root_path(worktree),
4987 pe->path) == -1) {
4988 error = got_error_from_errno("asprintf");
4989 goto done;
4991 if (lstat(ondisk_path, &sb) == -1) {
4992 if (errno == ENOENT) {
4993 free(ondisk_path);
4994 continue;
4996 error = got_error_from_errno2("lstat",
4997 ondisk_path);
4998 free(ondisk_path);
4999 goto done;
5001 free(ondisk_path);
5002 if (S_ISDIR(sb.st_mode)) {
5003 error = got_error_msg(GOT_ERR_BAD_PATH,
5004 "adding directories requires -R option");
5005 goto done;
5010 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5011 NULL, repo, no_ignores);
5012 done:
5013 if (repo)
5014 got_repo_close(repo);
5015 if (worktree)
5016 got_worktree_close(worktree);
5017 TAILQ_FOREACH(pe, &paths, entry)
5018 free((char *)pe->path);
5019 got_pathlist_free(&paths);
5020 free(cwd);
5021 return error;
5024 __dead static void
5025 usage_remove(void)
5027 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5028 getprogname());
5029 exit(1);
5032 static const struct got_error *
5033 print_remove_status(void *arg, unsigned char status,
5034 unsigned char staged_status, const char *path)
5036 while (path[0] == '/')
5037 path++;
5038 if (status == GOT_STATUS_NONEXISTENT)
5039 return NULL;
5040 if (status == staged_status && (status == GOT_STATUS_DELETE))
5041 status = GOT_STATUS_NO_CHANGE;
5042 printf("%c%c %s\n", status, staged_status, path);
5043 return NULL;
5046 static const struct got_error *
5047 cmd_remove(int argc, char *argv[])
5049 const struct got_error *error = NULL;
5050 struct got_worktree *worktree = NULL;
5051 struct got_repository *repo = NULL;
5052 char *cwd = NULL;
5053 struct got_pathlist_head paths;
5054 struct got_pathlist_entry *pe;
5055 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5057 TAILQ_INIT(&paths);
5059 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5060 switch (ch) {
5061 case 'f':
5062 delete_local_mods = 1;
5063 break;
5064 case 'k':
5065 keep_on_disk = 1;
5066 break;
5067 case 'R':
5068 can_recurse = 1;
5069 break;
5070 default:
5071 usage_remove();
5072 /* NOTREACHED */
5076 argc -= optind;
5077 argv += optind;
5079 #ifndef PROFILE
5080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5081 NULL) == -1)
5082 err(1, "pledge");
5083 #endif
5084 if (argc < 1)
5085 usage_remove();
5087 cwd = getcwd(NULL, 0);
5088 if (cwd == NULL) {
5089 error = got_error_from_errno("getcwd");
5090 goto done;
5092 error = got_worktree_open(&worktree, cwd);
5093 if (error)
5094 goto done;
5096 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5097 NULL);
5098 if (error)
5099 goto done;
5101 error = apply_unveil(got_repo_get_path(repo), 1,
5102 got_worktree_get_root_path(worktree));
5103 if (error)
5104 goto done;
5106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5107 if (error)
5108 goto done;
5110 if (!can_recurse) {
5111 char *ondisk_path;
5112 struct stat sb;
5113 TAILQ_FOREACH(pe, &paths, entry) {
5114 if (asprintf(&ondisk_path, "%s/%s",
5115 got_worktree_get_root_path(worktree),
5116 pe->path) == -1) {
5117 error = got_error_from_errno("asprintf");
5118 goto done;
5120 if (lstat(ondisk_path, &sb) == -1) {
5121 if (errno == ENOENT) {
5122 free(ondisk_path);
5123 continue;
5125 error = got_error_from_errno2("lstat",
5126 ondisk_path);
5127 free(ondisk_path);
5128 goto done;
5130 free(ondisk_path);
5131 if (S_ISDIR(sb.st_mode)) {
5132 error = got_error_msg(GOT_ERR_BAD_PATH,
5133 "removing directories requires -R option");
5134 goto done;
5139 error = got_worktree_schedule_delete(worktree, &paths,
5140 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5141 done:
5142 if (repo)
5143 got_repo_close(repo);
5144 if (worktree)
5145 got_worktree_close(worktree);
5146 TAILQ_FOREACH(pe, &paths, entry)
5147 free((char *)pe->path);
5148 got_pathlist_free(&paths);
5149 free(cwd);
5150 return error;
5153 __dead static void
5154 usage_revert(void)
5156 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5157 "path ...\n", getprogname());
5158 exit(1);
5161 static const struct got_error *
5162 revert_progress(void *arg, unsigned char status, const char *path)
5164 if (status == GOT_STATUS_UNVERSIONED)
5165 return NULL;
5167 while (path[0] == '/')
5168 path++;
5169 printf("%c %s\n", status, path);
5170 return NULL;
5173 struct choose_patch_arg {
5174 FILE *patch_script_file;
5175 const char *action;
5178 static const struct got_error *
5179 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5180 int nchanges, const char *action)
5182 char *line = NULL;
5183 size_t linesize = 0;
5184 ssize_t linelen;
5186 switch (status) {
5187 case GOT_STATUS_ADD:
5188 printf("A %s\n%s this addition? [y/n] ", path, action);
5189 break;
5190 case GOT_STATUS_DELETE:
5191 printf("D %s\n%s this deletion? [y/n] ", path, action);
5192 break;
5193 case GOT_STATUS_MODIFY:
5194 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5195 return got_error_from_errno("fseek");
5196 printf(GOT_COMMIT_SEP_STR);
5197 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5198 printf("%s", line);
5199 if (ferror(patch_file))
5200 return got_error_from_errno("getline");
5201 printf(GOT_COMMIT_SEP_STR);
5202 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5203 path, n, nchanges, action);
5204 break;
5205 default:
5206 return got_error_path(path, GOT_ERR_FILE_STATUS);
5209 return NULL;
5212 static const struct got_error *
5213 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5214 FILE *patch_file, int n, int nchanges)
5216 const struct got_error *err = NULL;
5217 char *line = NULL;
5218 size_t linesize = 0;
5219 ssize_t linelen;
5220 int resp = ' ';
5221 struct choose_patch_arg *a = arg;
5223 *choice = GOT_PATCH_CHOICE_NONE;
5225 if (a->patch_script_file) {
5226 char *nl;
5227 err = show_change(status, path, patch_file, n, nchanges,
5228 a->action);
5229 if (err)
5230 return err;
5231 linelen = getline(&line, &linesize, a->patch_script_file);
5232 if (linelen == -1) {
5233 if (ferror(a->patch_script_file))
5234 return got_error_from_errno("getline");
5235 return NULL;
5237 nl = strchr(line, '\n');
5238 if (nl)
5239 *nl = '\0';
5240 if (strcmp(line, "y") == 0) {
5241 *choice = GOT_PATCH_CHOICE_YES;
5242 printf("y\n");
5243 } else if (strcmp(line, "n") == 0) {
5244 *choice = GOT_PATCH_CHOICE_NO;
5245 printf("n\n");
5246 } else if (strcmp(line, "q") == 0 &&
5247 status == GOT_STATUS_MODIFY) {
5248 *choice = GOT_PATCH_CHOICE_QUIT;
5249 printf("q\n");
5250 } else
5251 printf("invalid response '%s'\n", line);
5252 free(line);
5253 return NULL;
5256 while (resp != 'y' && resp != 'n' && resp != 'q') {
5257 err = show_change(status, path, patch_file, n, nchanges,
5258 a->action);
5259 if (err)
5260 return err;
5261 resp = getchar();
5262 if (resp == '\n')
5263 resp = getchar();
5264 if (status == GOT_STATUS_MODIFY) {
5265 if (resp != 'y' && resp != 'n' && resp != 'q') {
5266 printf("invalid response '%c'\n", resp);
5267 resp = ' ';
5269 } else if (resp != 'y' && resp != 'n') {
5270 printf("invalid response '%c'\n", resp);
5271 resp = ' ';
5275 if (resp == 'y')
5276 *choice = GOT_PATCH_CHOICE_YES;
5277 else if (resp == 'n')
5278 *choice = GOT_PATCH_CHOICE_NO;
5279 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5280 *choice = GOT_PATCH_CHOICE_QUIT;
5282 return NULL;
5286 static const struct got_error *
5287 cmd_revert(int argc, char *argv[])
5289 const struct got_error *error = NULL;
5290 struct got_worktree *worktree = NULL;
5291 struct got_repository *repo = NULL;
5292 char *cwd = NULL, *path = NULL;
5293 struct got_pathlist_head paths;
5294 struct got_pathlist_entry *pe;
5295 int ch, can_recurse = 0, pflag = 0;
5296 FILE *patch_script_file = NULL;
5297 const char *patch_script_path = NULL;
5298 struct choose_patch_arg cpa;
5300 TAILQ_INIT(&paths);
5302 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5303 switch (ch) {
5304 case 'p':
5305 pflag = 1;
5306 break;
5307 case 'F':
5308 patch_script_path = optarg;
5309 break;
5310 case 'R':
5311 can_recurse = 1;
5312 break;
5313 default:
5314 usage_revert();
5315 /* NOTREACHED */
5319 argc -= optind;
5320 argv += optind;
5322 #ifndef PROFILE
5323 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5324 "unveil", NULL) == -1)
5325 err(1, "pledge");
5326 #endif
5327 if (argc < 1)
5328 usage_revert();
5329 if (patch_script_path && !pflag)
5330 errx(1, "-F option can only be used together with -p option");
5332 cwd = getcwd(NULL, 0);
5333 if (cwd == NULL) {
5334 error = got_error_from_errno("getcwd");
5335 goto done;
5337 error = got_worktree_open(&worktree, cwd);
5338 if (error)
5339 goto done;
5341 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5342 NULL);
5343 if (error != NULL)
5344 goto done;
5346 if (patch_script_path) {
5347 patch_script_file = fopen(patch_script_path, "r");
5348 if (patch_script_file == NULL) {
5349 error = got_error_from_errno2("fopen",
5350 patch_script_path);
5351 goto done;
5354 error = apply_unveil(got_repo_get_path(repo), 1,
5355 got_worktree_get_root_path(worktree));
5356 if (error)
5357 goto done;
5359 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5360 if (error)
5361 goto done;
5363 if (!can_recurse) {
5364 char *ondisk_path;
5365 struct stat sb;
5366 TAILQ_FOREACH(pe, &paths, entry) {
5367 if (asprintf(&ondisk_path, "%s/%s",
5368 got_worktree_get_root_path(worktree),
5369 pe->path) == -1) {
5370 error = got_error_from_errno("asprintf");
5371 goto done;
5373 if (lstat(ondisk_path, &sb) == -1) {
5374 if (errno == ENOENT) {
5375 free(ondisk_path);
5376 continue;
5378 error = got_error_from_errno2("lstat",
5379 ondisk_path);
5380 free(ondisk_path);
5381 goto done;
5383 free(ondisk_path);
5384 if (S_ISDIR(sb.st_mode)) {
5385 error = got_error_msg(GOT_ERR_BAD_PATH,
5386 "reverting directories requires -R option");
5387 goto done;
5392 cpa.patch_script_file = patch_script_file;
5393 cpa.action = "revert";
5394 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5395 pflag ? choose_patch : NULL, &cpa, repo);
5396 done:
5397 if (patch_script_file && fclose(patch_script_file) == EOF &&
5398 error == NULL)
5399 error = got_error_from_errno2("fclose", patch_script_path);
5400 if (repo)
5401 got_repo_close(repo);
5402 if (worktree)
5403 got_worktree_close(worktree);
5404 free(path);
5405 free(cwd);
5406 return error;
5409 __dead static void
5410 usage_commit(void)
5412 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5413 getprogname());
5414 exit(1);
5417 struct collect_commit_logmsg_arg {
5418 const char *cmdline_log;
5419 const char *editor;
5420 const char *worktree_path;
5421 const char *branch_name;
5422 const char *repo_path;
5423 char *logmsg_path;
5427 static const struct got_error *
5428 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5429 void *arg)
5431 char *initial_content = NULL;
5432 struct got_pathlist_entry *pe;
5433 const struct got_error *err = NULL;
5434 char *template = NULL;
5435 struct collect_commit_logmsg_arg *a = arg;
5436 int fd;
5437 size_t len;
5439 /* if a message was specified on the command line, just use it */
5440 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5441 len = strlen(a->cmdline_log) + 1;
5442 *logmsg = malloc(len + 1);
5443 if (*logmsg == NULL)
5444 return got_error_from_errno("malloc");
5445 strlcpy(*logmsg, a->cmdline_log, len);
5446 return NULL;
5449 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5450 return got_error_from_errno("asprintf");
5452 if (asprintf(&initial_content,
5453 "\n# changes to be committed on branch %s:\n",
5454 a->branch_name) == -1)
5455 return got_error_from_errno("asprintf");
5457 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5458 if (err)
5459 goto done;
5461 dprintf(fd, initial_content);
5463 TAILQ_FOREACH(pe, commitable_paths, entry) {
5464 struct got_commitable *ct = pe->data;
5465 dprintf(fd, "# %c %s\n",
5466 got_commitable_get_status(ct),
5467 got_commitable_get_path(ct));
5469 close(fd);
5471 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5472 done:
5473 free(initial_content);
5474 free(template);
5476 /* Editor is done; we can now apply unveil(2) */
5477 if (err == NULL) {
5478 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5479 if (err) {
5480 free(*logmsg);
5481 *logmsg = NULL;
5484 return err;
5487 static const struct got_error *
5488 cmd_commit(int argc, char *argv[])
5490 const struct got_error *error = NULL;
5491 struct got_worktree *worktree = NULL;
5492 struct got_repository *repo = NULL;
5493 char *cwd = NULL, *id_str = NULL;
5494 struct got_object_id *id = NULL;
5495 const char *logmsg = NULL;
5496 struct collect_commit_logmsg_arg cl_arg;
5497 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5498 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5499 struct got_pathlist_head paths;
5501 TAILQ_INIT(&paths);
5502 cl_arg.logmsg_path = NULL;
5504 while ((ch = getopt(argc, argv, "m:")) != -1) {
5505 switch (ch) {
5506 case 'm':
5507 logmsg = optarg;
5508 break;
5509 default:
5510 usage_commit();
5511 /* NOTREACHED */
5515 argc -= optind;
5516 argv += optind;
5518 #ifndef PROFILE
5519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5520 "unveil", NULL) == -1)
5521 err(1, "pledge");
5522 #endif
5523 cwd = getcwd(NULL, 0);
5524 if (cwd == NULL) {
5525 error = got_error_from_errno("getcwd");
5526 goto done;
5528 error = got_worktree_open(&worktree, cwd);
5529 if (error)
5530 goto done;
5532 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5533 if (error)
5534 goto done;
5535 if (rebase_in_progress) {
5536 error = got_error(GOT_ERR_REBASING);
5537 goto done;
5540 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5541 worktree);
5542 if (error)
5543 goto done;
5545 error = get_gitconfig_path(&gitconfig_path);
5546 if (error)
5547 goto done;
5548 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5549 gitconfig_path);
5550 if (error != NULL)
5551 goto done;
5553 error = get_author(&author, repo);
5554 if (error)
5555 return error;
5558 * unveil(2) traverses exec(2); if an editor is used we have
5559 * to apply unveil after the log message has been written.
5561 if (logmsg == NULL || strlen(logmsg) == 0)
5562 error = get_editor(&editor);
5563 else
5564 error = apply_unveil(got_repo_get_path(repo), 0,
5565 got_worktree_get_root_path(worktree));
5566 if (error)
5567 goto done;
5569 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5570 if (error)
5571 goto done;
5573 cl_arg.editor = editor;
5574 cl_arg.cmdline_log = logmsg;
5575 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5576 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5577 if (!histedit_in_progress) {
5578 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5579 error = got_error(GOT_ERR_COMMIT_BRANCH);
5580 goto done;
5582 cl_arg.branch_name += 11;
5584 cl_arg.repo_path = got_repo_get_path(repo);
5585 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5586 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5587 if (error) {
5588 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5589 cl_arg.logmsg_path != NULL)
5590 preserve_logmsg = 1;
5591 goto done;
5594 error = got_object_id_str(&id_str, id);
5595 if (error)
5596 goto done;
5597 printf("Created commit %s\n", id_str);
5598 done:
5599 if (preserve_logmsg) {
5600 fprintf(stderr, "%s: log message preserved in %s\n",
5601 getprogname(), cl_arg.logmsg_path);
5602 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5603 error == NULL)
5604 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5605 free(cl_arg.logmsg_path);
5606 if (repo)
5607 got_repo_close(repo);
5608 if (worktree)
5609 got_worktree_close(worktree);
5610 free(cwd);
5611 free(id_str);
5612 free(gitconfig_path);
5613 free(editor);
5614 free(author);
5615 return error;
5618 __dead static void
5619 usage_cherrypick(void)
5621 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5622 exit(1);
5625 static const struct got_error *
5626 cmd_cherrypick(int argc, char *argv[])
5628 const struct got_error *error = NULL;
5629 struct got_worktree *worktree = NULL;
5630 struct got_repository *repo = NULL;
5631 char *cwd = NULL, *commit_id_str = NULL;
5632 struct got_object_id *commit_id = NULL;
5633 struct got_commit_object *commit = NULL;
5634 struct got_object_qid *pid;
5635 struct got_reference *head_ref = NULL;
5636 int ch, did_something = 0;
5638 while ((ch = getopt(argc, argv, "")) != -1) {
5639 switch (ch) {
5640 default:
5641 usage_cherrypick();
5642 /* NOTREACHED */
5646 argc -= optind;
5647 argv += optind;
5649 #ifndef PROFILE
5650 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5651 "unveil", NULL) == -1)
5652 err(1, "pledge");
5653 #endif
5654 if (argc != 1)
5655 usage_cherrypick();
5657 cwd = getcwd(NULL, 0);
5658 if (cwd == NULL) {
5659 error = got_error_from_errno("getcwd");
5660 goto done;
5662 error = got_worktree_open(&worktree, cwd);
5663 if (error)
5664 goto done;
5666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5667 NULL);
5668 if (error != NULL)
5669 goto done;
5671 error = apply_unveil(got_repo_get_path(repo), 0,
5672 got_worktree_get_root_path(worktree));
5673 if (error)
5674 goto done;
5676 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5677 GOT_OBJ_TYPE_COMMIT, repo);
5678 if (error != NULL) {
5679 struct got_reference *ref;
5680 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5681 goto done;
5682 error = got_ref_open(&ref, repo, argv[0], 0);
5683 if (error != NULL)
5684 goto done;
5685 error = got_ref_resolve(&commit_id, repo, ref);
5686 got_ref_close(ref);
5687 if (error != NULL)
5688 goto done;
5690 error = got_object_id_str(&commit_id_str, commit_id);
5691 if (error)
5692 goto done;
5694 error = got_ref_open(&head_ref, repo,
5695 got_worktree_get_head_ref_name(worktree), 0);
5696 if (error != NULL)
5697 goto done;
5699 error = check_same_branch(commit_id, head_ref, NULL, repo);
5700 if (error) {
5701 if (error->code != GOT_ERR_ANCESTRY)
5702 goto done;
5703 error = NULL;
5704 } else {
5705 error = got_error(GOT_ERR_SAME_BRANCH);
5706 goto done;
5709 error = got_object_open_as_commit(&commit, repo, commit_id);
5710 if (error)
5711 goto done;
5712 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5713 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5714 commit_id, repo, update_progress, &did_something, check_cancelled,
5715 NULL);
5716 if (error != NULL)
5717 goto done;
5719 if (did_something)
5720 printf("Merged commit %s\n", commit_id_str);
5721 done:
5722 if (commit)
5723 got_object_commit_close(commit);
5724 free(commit_id_str);
5725 if (head_ref)
5726 got_ref_close(head_ref);
5727 if (worktree)
5728 got_worktree_close(worktree);
5729 if (repo)
5730 got_repo_close(repo);
5731 return error;
5734 __dead static void
5735 usage_backout(void)
5737 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5738 exit(1);
5741 static const struct got_error *
5742 cmd_backout(int argc, char *argv[])
5744 const struct got_error *error = NULL;
5745 struct got_worktree *worktree = NULL;
5746 struct got_repository *repo = NULL;
5747 char *cwd = NULL, *commit_id_str = NULL;
5748 struct got_object_id *commit_id = NULL;
5749 struct got_commit_object *commit = NULL;
5750 struct got_object_qid *pid;
5751 struct got_reference *head_ref = NULL;
5752 int ch, did_something = 0;
5754 while ((ch = getopt(argc, argv, "")) != -1) {
5755 switch (ch) {
5756 default:
5757 usage_backout();
5758 /* NOTREACHED */
5762 argc -= optind;
5763 argv += optind;
5765 #ifndef PROFILE
5766 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5767 "unveil", NULL) == -1)
5768 err(1, "pledge");
5769 #endif
5770 if (argc != 1)
5771 usage_backout();
5773 cwd = getcwd(NULL, 0);
5774 if (cwd == NULL) {
5775 error = got_error_from_errno("getcwd");
5776 goto done;
5778 error = got_worktree_open(&worktree, cwd);
5779 if (error)
5780 goto done;
5782 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5783 NULL);
5784 if (error != NULL)
5785 goto done;
5787 error = apply_unveil(got_repo_get_path(repo), 0,
5788 got_worktree_get_root_path(worktree));
5789 if (error)
5790 goto done;
5792 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5793 GOT_OBJ_TYPE_COMMIT, repo);
5794 if (error != NULL) {
5795 struct got_reference *ref;
5796 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5797 goto done;
5798 error = got_ref_open(&ref, repo, argv[0], 0);
5799 if (error != NULL)
5800 goto done;
5801 error = got_ref_resolve(&commit_id, repo, ref);
5802 got_ref_close(ref);
5803 if (error != NULL)
5804 goto done;
5806 error = got_object_id_str(&commit_id_str, commit_id);
5807 if (error)
5808 goto done;
5810 error = got_ref_open(&head_ref, repo,
5811 got_worktree_get_head_ref_name(worktree), 0);
5812 if (error != NULL)
5813 goto done;
5815 error = check_same_branch(commit_id, head_ref, NULL, repo);
5816 if (error)
5817 goto done;
5819 error = got_object_open_as_commit(&commit, repo, commit_id);
5820 if (error)
5821 goto done;
5822 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5823 if (pid == NULL) {
5824 error = got_error(GOT_ERR_ROOT_COMMIT);
5825 goto done;
5828 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5829 update_progress, &did_something, check_cancelled, NULL);
5830 if (error != NULL)
5831 goto done;
5833 if (did_something)
5834 printf("Backed out commit %s\n", commit_id_str);
5835 done:
5836 if (commit)
5837 got_object_commit_close(commit);
5838 free(commit_id_str);
5839 if (head_ref)
5840 got_ref_close(head_ref);
5841 if (worktree)
5842 got_worktree_close(worktree);
5843 if (repo)
5844 got_repo_close(repo);
5845 return error;
5848 __dead static void
5849 usage_rebase(void)
5851 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5852 getprogname());
5853 exit(1);
5856 void
5857 trim_logmsg(char *logmsg, int limit)
5859 char *nl;
5860 size_t len;
5862 len = strlen(logmsg);
5863 if (len > limit)
5864 len = limit;
5865 logmsg[len] = '\0';
5866 nl = strchr(logmsg, '\n');
5867 if (nl)
5868 *nl = '\0';
5871 static const struct got_error *
5872 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5874 const struct got_error *err;
5875 char *logmsg0 = NULL;
5876 const char *s;
5878 err = got_object_commit_get_logmsg(&logmsg0, commit);
5879 if (err)
5880 return err;
5882 s = logmsg0;
5883 while (isspace((unsigned char)s[0]))
5884 s++;
5886 *logmsg = strdup(s);
5887 if (*logmsg == NULL) {
5888 err = got_error_from_errno("strdup");
5889 goto done;
5892 trim_logmsg(*logmsg, limit);
5893 done:
5894 free(logmsg0);
5895 return err;
5898 static const struct got_error *
5899 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5901 const struct got_error *err;
5902 struct got_commit_object *commit = NULL;
5903 char *id_str = NULL, *logmsg = NULL;
5905 err = got_object_open_as_commit(&commit, repo, id);
5906 if (err)
5907 return err;
5909 err = got_object_id_str(&id_str, id);
5910 if (err)
5911 goto done;
5913 id_str[12] = '\0';
5915 err = get_short_logmsg(&logmsg, 42, commit);
5916 if (err)
5917 goto done;
5919 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5920 done:
5921 free(id_str);
5922 got_object_commit_close(commit);
5923 free(logmsg);
5924 return err;
5927 static const struct got_error *
5928 show_rebase_progress(struct got_commit_object *commit,
5929 struct got_object_id *old_id, struct got_object_id *new_id)
5931 const struct got_error *err;
5932 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5934 err = got_object_id_str(&old_id_str, old_id);
5935 if (err)
5936 goto done;
5938 if (new_id) {
5939 err = got_object_id_str(&new_id_str, new_id);
5940 if (err)
5941 goto done;
5944 old_id_str[12] = '\0';
5945 if (new_id_str)
5946 new_id_str[12] = '\0';
5948 err = get_short_logmsg(&logmsg, 42, commit);
5949 if (err)
5950 goto done;
5952 printf("%s -> %s: %s\n", old_id_str,
5953 new_id_str ? new_id_str : "no-op change", logmsg);
5954 done:
5955 free(old_id_str);
5956 free(new_id_str);
5957 free(logmsg);
5958 return err;
5961 static const struct got_error *
5962 rebase_progress(void *arg, unsigned char status, const char *path)
5964 unsigned char *rebase_status = arg;
5966 while (path[0] == '/')
5967 path++;
5968 printf("%c %s\n", status, path);
5970 if (*rebase_status == GOT_STATUS_CONFLICT)
5971 return NULL;
5972 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5973 *rebase_status = status;
5974 return NULL;
5977 static const struct got_error *
5978 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5979 struct got_reference *branch, struct got_reference *new_base_branch,
5980 struct got_reference *tmp_branch, struct got_repository *repo)
5982 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5983 return got_worktree_rebase_complete(worktree, fileindex,
5984 new_base_branch, tmp_branch, branch, repo);
5987 static const struct got_error *
5988 rebase_commit(struct got_pathlist_head *merged_paths,
5989 struct got_worktree *worktree, struct got_fileindex *fileindex,
5990 struct got_reference *tmp_branch,
5991 struct got_object_id *commit_id, struct got_repository *repo)
5993 const struct got_error *error;
5994 struct got_commit_object *commit;
5995 struct got_object_id *new_commit_id;
5997 error = got_object_open_as_commit(&commit, repo, commit_id);
5998 if (error)
5999 return error;
6001 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6002 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6003 if (error) {
6004 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6005 goto done;
6006 error = show_rebase_progress(commit, commit_id, NULL);
6007 } else {
6008 error = show_rebase_progress(commit, commit_id, new_commit_id);
6009 free(new_commit_id);
6011 done:
6012 got_object_commit_close(commit);
6013 return error;
6016 struct check_path_prefix_arg {
6017 const char *path_prefix;
6018 size_t len;
6019 int errcode;
6022 static const struct got_error *
6023 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6024 struct got_blob_object *blob2, struct got_object_id *id1,
6025 struct got_object_id *id2, const char *path1, const char *path2,
6026 mode_t mode1, mode_t mode2, struct got_repository *repo)
6028 struct check_path_prefix_arg *a = arg;
6030 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6031 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6032 return got_error(a->errcode);
6034 return NULL;
6037 static const struct got_error *
6038 check_path_prefix(struct got_object_id *parent_id,
6039 struct got_object_id *commit_id, const char *path_prefix,
6040 int errcode, struct got_repository *repo)
6042 const struct got_error *err;
6043 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6044 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6045 struct check_path_prefix_arg cpp_arg;
6047 if (got_path_is_root_dir(path_prefix))
6048 return NULL;
6050 err = got_object_open_as_commit(&commit, repo, commit_id);
6051 if (err)
6052 goto done;
6054 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6055 if (err)
6056 goto done;
6058 err = got_object_open_as_tree(&tree1, repo,
6059 got_object_commit_get_tree_id(parent_commit));
6060 if (err)
6061 goto done;
6063 err = got_object_open_as_tree(&tree2, repo,
6064 got_object_commit_get_tree_id(commit));
6065 if (err)
6066 goto done;
6068 cpp_arg.path_prefix = path_prefix;
6069 while (cpp_arg.path_prefix[0] == '/')
6070 cpp_arg.path_prefix++;
6071 cpp_arg.len = strlen(cpp_arg.path_prefix);
6072 cpp_arg.errcode = errcode;
6073 err = got_diff_tree(tree1, tree2, "", "", repo,
6074 check_path_prefix_in_diff, &cpp_arg, 0);
6075 done:
6076 if (tree1)
6077 got_object_tree_close(tree1);
6078 if (tree2)
6079 got_object_tree_close(tree2);
6080 if (commit)
6081 got_object_commit_close(commit);
6082 if (parent_commit)
6083 got_object_commit_close(parent_commit);
6084 return err;
6087 static const struct got_error *
6088 collect_commits(struct got_object_id_queue *commits,
6089 struct got_object_id *initial_commit_id,
6090 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6091 const char *path_prefix, int path_prefix_errcode,
6092 struct got_repository *repo)
6094 const struct got_error *err = NULL;
6095 struct got_commit_graph *graph = NULL;
6096 struct got_object_id *parent_id = NULL;
6097 struct got_object_qid *qid;
6098 struct got_object_id *commit_id = initial_commit_id;
6100 err = got_commit_graph_open(&graph, "/", 1);
6101 if (err)
6102 return err;
6104 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6105 check_cancelled, NULL);
6106 if (err)
6107 goto done;
6108 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6109 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6110 check_cancelled, NULL);
6111 if (err) {
6112 if (err->code == GOT_ERR_ITER_COMPLETED) {
6113 err = got_error_msg(GOT_ERR_ANCESTRY,
6114 "ran out of commits to rebase before "
6115 "youngest common ancestor commit has "
6116 "been reached?!?");
6118 goto done;
6119 } else {
6120 err = check_path_prefix(parent_id, commit_id,
6121 path_prefix, path_prefix_errcode, repo);
6122 if (err)
6123 goto done;
6125 err = got_object_qid_alloc(&qid, commit_id);
6126 if (err)
6127 goto done;
6128 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6129 commit_id = parent_id;
6132 done:
6133 got_commit_graph_close(graph);
6134 return err;
6137 static const struct got_error *
6138 cmd_rebase(int argc, char *argv[])
6140 const struct got_error *error = NULL;
6141 struct got_worktree *worktree = NULL;
6142 struct got_repository *repo = NULL;
6143 struct got_fileindex *fileindex = NULL;
6144 char *cwd = NULL;
6145 struct got_reference *branch = NULL;
6146 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6147 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6148 struct got_object_id *resume_commit_id = NULL;
6149 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6150 struct got_commit_object *commit = NULL;
6151 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6152 int histedit_in_progress = 0;
6153 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6154 struct got_object_id_queue commits;
6155 struct got_pathlist_head merged_paths;
6156 const struct got_object_id_queue *parent_ids;
6157 struct got_object_qid *qid, *pid;
6159 SIMPLEQ_INIT(&commits);
6160 TAILQ_INIT(&merged_paths);
6162 while ((ch = getopt(argc, argv, "ac")) != -1) {
6163 switch (ch) {
6164 case 'a':
6165 abort_rebase = 1;
6166 break;
6167 case 'c':
6168 continue_rebase = 1;
6169 break;
6170 default:
6171 usage_rebase();
6172 /* NOTREACHED */
6176 argc -= optind;
6177 argv += optind;
6179 #ifndef PROFILE
6180 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6181 "unveil", NULL) == -1)
6182 err(1, "pledge");
6183 #endif
6184 if (abort_rebase && continue_rebase)
6185 usage_rebase();
6186 else if (abort_rebase || continue_rebase) {
6187 if (argc != 0)
6188 usage_rebase();
6189 } else if (argc != 1)
6190 usage_rebase();
6192 cwd = getcwd(NULL, 0);
6193 if (cwd == NULL) {
6194 error = got_error_from_errno("getcwd");
6195 goto done;
6197 error = got_worktree_open(&worktree, cwd);
6198 if (error)
6199 goto done;
6201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6202 NULL);
6203 if (error != NULL)
6204 goto done;
6206 error = apply_unveil(got_repo_get_path(repo), 0,
6207 got_worktree_get_root_path(worktree));
6208 if (error)
6209 goto done;
6211 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6212 worktree);
6213 if (error)
6214 goto done;
6215 if (histedit_in_progress) {
6216 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6217 goto done;
6220 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6221 if (error)
6222 goto done;
6224 if (abort_rebase) {
6225 int did_something;
6226 if (!rebase_in_progress) {
6227 error = got_error(GOT_ERR_NOT_REBASING);
6228 goto done;
6230 error = got_worktree_rebase_continue(&resume_commit_id,
6231 &new_base_branch, &tmp_branch, &branch, &fileindex,
6232 worktree, repo);
6233 if (error)
6234 goto done;
6235 printf("Switching work tree to %s\n",
6236 got_ref_get_symref_target(new_base_branch));
6237 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6238 new_base_branch, update_progress, &did_something);
6239 if (error)
6240 goto done;
6241 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6242 goto done; /* nothing else to do */
6245 if (continue_rebase) {
6246 if (!rebase_in_progress) {
6247 error = got_error(GOT_ERR_NOT_REBASING);
6248 goto done;
6250 error = got_worktree_rebase_continue(&resume_commit_id,
6251 &new_base_branch, &tmp_branch, &branch, &fileindex,
6252 worktree, repo);
6253 if (error)
6254 goto done;
6256 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6257 resume_commit_id, repo);
6258 if (error)
6259 goto done;
6261 yca_id = got_object_id_dup(resume_commit_id);
6262 if (yca_id == NULL) {
6263 error = got_error_from_errno("got_object_id_dup");
6264 goto done;
6266 } else {
6267 error = got_ref_open(&branch, repo, argv[0], 0);
6268 if (error != NULL)
6269 goto done;
6272 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6273 if (error)
6274 goto done;
6276 if (!continue_rebase) {
6277 struct got_object_id *base_commit_id;
6279 base_commit_id = got_worktree_get_base_commit_id(worktree);
6280 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6281 base_commit_id, branch_head_commit_id, repo,
6282 check_cancelled, NULL);
6283 if (error)
6284 goto done;
6285 if (yca_id == NULL) {
6286 error = got_error_msg(GOT_ERR_ANCESTRY,
6287 "specified branch shares no common ancestry "
6288 "with work tree's branch");
6289 goto done;
6292 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6293 if (error) {
6294 if (error->code != GOT_ERR_ANCESTRY)
6295 goto done;
6296 error = NULL;
6297 } else {
6298 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6299 "specified branch resolves to a commit which "
6300 "is already contained in work tree's branch");
6301 goto done;
6303 error = got_worktree_rebase_prepare(&new_base_branch,
6304 &tmp_branch, &fileindex, worktree, branch, repo);
6305 if (error)
6306 goto done;
6309 commit_id = branch_head_commit_id;
6310 error = got_object_open_as_commit(&commit, repo, commit_id);
6311 if (error)
6312 goto done;
6314 parent_ids = got_object_commit_get_parent_ids(commit);
6315 pid = SIMPLEQ_FIRST(parent_ids);
6316 if (pid == NULL) {
6317 if (!continue_rebase) {
6318 int did_something;
6319 error = got_worktree_rebase_abort(worktree, fileindex,
6320 repo, new_base_branch, update_progress,
6321 &did_something);
6322 if (error)
6323 goto done;
6324 printf("Rebase of %s aborted\n",
6325 got_ref_get_name(branch));
6327 error = got_error(GOT_ERR_EMPTY_REBASE);
6328 goto done;
6330 error = collect_commits(&commits, commit_id, pid->id,
6331 yca_id, got_worktree_get_path_prefix(worktree),
6332 GOT_ERR_REBASE_PATH, repo);
6333 got_object_commit_close(commit);
6334 commit = NULL;
6335 if (error)
6336 goto done;
6338 if (SIMPLEQ_EMPTY(&commits)) {
6339 if (continue_rebase) {
6340 error = rebase_complete(worktree, fileindex,
6341 branch, new_base_branch, tmp_branch, repo);
6342 goto done;
6343 } else {
6344 /* Fast-forward the reference of the branch. */
6345 struct got_object_id *new_head_commit_id;
6346 char *id_str;
6347 error = got_ref_resolve(&new_head_commit_id, repo,
6348 new_base_branch);
6349 if (error)
6350 goto done;
6351 error = got_object_id_str(&id_str, new_head_commit_id);
6352 printf("Forwarding %s to commit %s\n",
6353 got_ref_get_name(branch), id_str);
6354 free(id_str);
6355 error = got_ref_change_ref(branch,
6356 new_head_commit_id);
6357 if (error)
6358 goto done;
6362 pid = NULL;
6363 SIMPLEQ_FOREACH(qid, &commits, entry) {
6364 commit_id = qid->id;
6365 parent_id = pid ? pid->id : yca_id;
6366 pid = qid;
6368 error = got_worktree_rebase_merge_files(&merged_paths,
6369 worktree, fileindex, parent_id, commit_id, repo,
6370 rebase_progress, &rebase_status, check_cancelled, NULL);
6371 if (error)
6372 goto done;
6374 if (rebase_status == GOT_STATUS_CONFLICT) {
6375 error = show_rebase_merge_conflict(qid->id, repo);
6376 if (error)
6377 goto done;
6378 got_worktree_rebase_pathlist_free(&merged_paths);
6379 break;
6382 error = rebase_commit(&merged_paths, worktree, fileindex,
6383 tmp_branch, commit_id, repo);
6384 got_worktree_rebase_pathlist_free(&merged_paths);
6385 if (error)
6386 goto done;
6389 if (rebase_status == GOT_STATUS_CONFLICT) {
6390 error = got_worktree_rebase_postpone(worktree, fileindex);
6391 if (error)
6392 goto done;
6393 error = got_error_msg(GOT_ERR_CONFLICTS,
6394 "conflicts must be resolved before rebasing can continue");
6395 } else
6396 error = rebase_complete(worktree, fileindex, branch,
6397 new_base_branch, tmp_branch, repo);
6398 done:
6399 got_object_id_queue_free(&commits);
6400 free(branch_head_commit_id);
6401 free(resume_commit_id);
6402 free(yca_id);
6403 if (commit)
6404 got_object_commit_close(commit);
6405 if (branch)
6406 got_ref_close(branch);
6407 if (new_base_branch)
6408 got_ref_close(new_base_branch);
6409 if (tmp_branch)
6410 got_ref_close(tmp_branch);
6411 if (worktree)
6412 got_worktree_close(worktree);
6413 if (repo)
6414 got_repo_close(repo);
6415 return error;
6418 __dead static void
6419 usage_histedit(void)
6421 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6422 getprogname());
6423 exit(1);
6426 #define GOT_HISTEDIT_PICK 'p'
6427 #define GOT_HISTEDIT_EDIT 'e'
6428 #define GOT_HISTEDIT_FOLD 'f'
6429 #define GOT_HISTEDIT_DROP 'd'
6430 #define GOT_HISTEDIT_MESG 'm'
6432 static struct got_histedit_cmd {
6433 unsigned char code;
6434 const char *name;
6435 const char *desc;
6436 } got_histedit_cmds[] = {
6437 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6438 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6439 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6440 "be used" },
6441 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6442 { GOT_HISTEDIT_MESG, "mesg",
6443 "single-line log message for commit above (open editor if empty)" },
6446 struct got_histedit_list_entry {
6447 TAILQ_ENTRY(got_histedit_list_entry) entry;
6448 struct got_object_id *commit_id;
6449 const struct got_histedit_cmd *cmd;
6450 char *logmsg;
6452 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6454 static const struct got_error *
6455 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6456 FILE *f, struct got_repository *repo)
6458 const struct got_error *err = NULL;
6459 char *logmsg = NULL, *id_str = NULL;
6460 struct got_commit_object *commit = NULL;
6461 int n;
6463 err = got_object_open_as_commit(&commit, repo, commit_id);
6464 if (err)
6465 goto done;
6467 err = get_short_logmsg(&logmsg, 34, commit);
6468 if (err)
6469 goto done;
6471 err = got_object_id_str(&id_str, commit_id);
6472 if (err)
6473 goto done;
6475 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6476 if (n < 0)
6477 err = got_ferror(f, GOT_ERR_IO);
6478 done:
6479 if (commit)
6480 got_object_commit_close(commit);
6481 free(id_str);
6482 free(logmsg);
6483 return err;
6486 static const struct got_error *
6487 histedit_write_commit_list(struct got_object_id_queue *commits,
6488 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6490 const struct got_error *err = NULL;
6491 struct got_object_qid *qid;
6493 if (SIMPLEQ_EMPTY(commits))
6494 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6496 SIMPLEQ_FOREACH(qid, commits, entry) {
6497 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6498 f, repo);
6499 if (err)
6500 break;
6501 if (edit_logmsg_only) {
6502 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6503 if (n < 0) {
6504 err = got_ferror(f, GOT_ERR_IO);
6505 break;
6510 return err;
6513 static const struct got_error *
6514 write_cmd_list(FILE *f, const char *branch_name,
6515 struct got_object_id_queue *commits)
6517 const struct got_error *err = NULL;
6518 int n, i;
6519 char *id_str;
6520 struct got_object_qid *qid;
6522 qid = SIMPLEQ_FIRST(commits);
6523 err = got_object_id_str(&id_str, qid->id);
6524 if (err)
6525 return err;
6527 n = fprintf(f,
6528 "# Editing the history of branch '%s' starting at\n"
6529 "# commit %s\n"
6530 "# Commits will be processed in order from top to "
6531 "bottom of this file.\n", branch_name, id_str);
6532 if (n < 0) {
6533 err = got_ferror(f, GOT_ERR_IO);
6534 goto done;
6537 n = fprintf(f, "# Available histedit commands:\n");
6538 if (n < 0) {
6539 err = got_ferror(f, GOT_ERR_IO);
6540 goto done;
6543 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6544 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6545 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6546 cmd->desc);
6547 if (n < 0) {
6548 err = got_ferror(f, GOT_ERR_IO);
6549 break;
6552 done:
6553 free(id_str);
6554 return err;
6557 static const struct got_error *
6558 histedit_syntax_error(int lineno)
6560 static char msg[42];
6561 int ret;
6563 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6564 lineno);
6565 if (ret == -1 || ret >= sizeof(msg))
6566 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6568 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6571 static const struct got_error *
6572 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6573 char *logmsg, struct got_repository *repo)
6575 const struct got_error *err;
6576 struct got_commit_object *folded_commit = NULL;
6577 char *id_str, *folded_logmsg = NULL;
6579 err = got_object_id_str(&id_str, hle->commit_id);
6580 if (err)
6581 return err;
6583 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6584 if (err)
6585 goto done;
6587 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6588 if (err)
6589 goto done;
6590 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6591 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6592 folded_logmsg) == -1) {
6593 err = got_error_from_errno("asprintf");
6595 done:
6596 if (folded_commit)
6597 got_object_commit_close(folded_commit);
6598 free(id_str);
6599 free(folded_logmsg);
6600 return err;
6603 static struct got_histedit_list_entry *
6604 get_folded_commits(struct got_histedit_list_entry *hle)
6606 struct got_histedit_list_entry *prev, *folded = NULL;
6608 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6609 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6610 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6611 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6612 folded = prev;
6613 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6616 return folded;
6619 static const struct got_error *
6620 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6621 struct got_repository *repo)
6623 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6624 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6625 const struct got_error *err = NULL;
6626 struct got_commit_object *commit = NULL;
6627 int fd;
6628 struct got_histedit_list_entry *folded = NULL;
6630 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6631 if (err)
6632 return err;
6634 folded = get_folded_commits(hle);
6635 if (folded) {
6636 while (folded != hle) {
6637 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6638 folded = TAILQ_NEXT(folded, entry);
6639 continue;
6641 err = append_folded_commit_msg(&new_msg, folded,
6642 logmsg, repo);
6643 if (err)
6644 goto done;
6645 free(logmsg);
6646 logmsg = new_msg;
6647 folded = TAILQ_NEXT(folded, entry);
6651 err = got_object_id_str(&id_str, hle->commit_id);
6652 if (err)
6653 goto done;
6654 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6655 if (err)
6656 goto done;
6657 if (asprintf(&new_msg,
6658 "%s\n# original log message of commit %s: %s",
6659 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6660 err = got_error_from_errno("asprintf");
6661 goto done;
6663 free(logmsg);
6664 logmsg = new_msg;
6666 err = got_object_id_str(&id_str, hle->commit_id);
6667 if (err)
6668 goto done;
6670 err = got_opentemp_named_fd(&logmsg_path, &fd,
6671 GOT_TMPDIR_STR "/got-logmsg");
6672 if (err)
6673 goto done;
6675 dprintf(fd, logmsg);
6676 close(fd);
6678 err = get_editor(&editor);
6679 if (err)
6680 goto done;
6682 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6683 if (err) {
6684 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6685 goto done;
6686 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6688 done:
6689 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6690 err = got_error_from_errno2("unlink", logmsg_path);
6691 free(logmsg_path);
6692 free(logmsg);
6693 free(orig_logmsg);
6694 free(editor);
6695 if (commit)
6696 got_object_commit_close(commit);
6697 return err;
6700 static const struct got_error *
6701 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6702 FILE *f, struct got_repository *repo)
6704 const struct got_error *err = NULL;
6705 char *line = NULL, *p, *end;
6706 size_t size;
6707 ssize_t len;
6708 int lineno = 0, i;
6709 const struct got_histedit_cmd *cmd;
6710 struct got_object_id *commit_id = NULL;
6711 struct got_histedit_list_entry *hle = NULL;
6713 for (;;) {
6714 len = getline(&line, &size, f);
6715 if (len == -1) {
6716 const struct got_error *getline_err;
6717 if (feof(f))
6718 break;
6719 getline_err = got_error_from_errno("getline");
6720 err = got_ferror(f, getline_err->code);
6721 break;
6723 lineno++;
6724 p = line;
6725 while (isspace((unsigned char)p[0]))
6726 p++;
6727 if (p[0] == '#' || p[0] == '\0') {
6728 free(line);
6729 line = NULL;
6730 continue;
6732 cmd = NULL;
6733 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6734 cmd = &got_histedit_cmds[i];
6735 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6736 isspace((unsigned char)p[strlen(cmd->name)])) {
6737 p += strlen(cmd->name);
6738 break;
6740 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6741 p++;
6742 break;
6745 if (i == nitems(got_histedit_cmds)) {
6746 err = histedit_syntax_error(lineno);
6747 break;
6749 while (isspace((unsigned char)p[0]))
6750 p++;
6751 if (cmd->code == GOT_HISTEDIT_MESG) {
6752 if (hle == NULL || hle->logmsg != NULL) {
6753 err = got_error(GOT_ERR_HISTEDIT_CMD);
6754 break;
6756 if (p[0] == '\0') {
6757 err = histedit_edit_logmsg(hle, repo);
6758 if (err)
6759 break;
6760 } else {
6761 hle->logmsg = strdup(p);
6762 if (hle->logmsg == NULL) {
6763 err = got_error_from_errno("strdup");
6764 break;
6767 free(line);
6768 line = NULL;
6769 continue;
6770 } else {
6771 end = p;
6772 while (end[0] && !isspace((unsigned char)end[0]))
6773 end++;
6774 *end = '\0';
6776 err = got_object_resolve_id_str(&commit_id, repo, p);
6777 if (err) {
6778 /* override error code */
6779 err = histedit_syntax_error(lineno);
6780 break;
6783 hle = malloc(sizeof(*hle));
6784 if (hle == NULL) {
6785 err = got_error_from_errno("malloc");
6786 break;
6788 hle->cmd = cmd;
6789 hle->commit_id = commit_id;
6790 hle->logmsg = NULL;
6791 commit_id = NULL;
6792 free(line);
6793 line = NULL;
6794 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6797 free(line);
6798 free(commit_id);
6799 return err;
6802 static const struct got_error *
6803 histedit_check_script(struct got_histedit_list *histedit_cmds,
6804 struct got_object_id_queue *commits, struct got_repository *repo)
6806 const struct got_error *err = NULL;
6807 struct got_object_qid *qid;
6808 struct got_histedit_list_entry *hle;
6809 static char msg[92];
6810 char *id_str;
6812 if (TAILQ_EMPTY(histedit_cmds))
6813 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6814 "histedit script contains no commands");
6815 if (SIMPLEQ_EMPTY(commits))
6816 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6818 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6819 struct got_histedit_list_entry *hle2;
6820 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6821 if (hle == hle2)
6822 continue;
6823 if (got_object_id_cmp(hle->commit_id,
6824 hle2->commit_id) != 0)
6825 continue;
6826 err = got_object_id_str(&id_str, hle->commit_id);
6827 if (err)
6828 return err;
6829 snprintf(msg, sizeof(msg), "commit %s is listed "
6830 "more than once in histedit script", id_str);
6831 free(id_str);
6832 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6836 SIMPLEQ_FOREACH(qid, commits, entry) {
6837 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6838 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6839 break;
6841 if (hle == NULL) {
6842 err = got_object_id_str(&id_str, qid->id);
6843 if (err)
6844 return err;
6845 snprintf(msg, sizeof(msg),
6846 "commit %s missing from histedit script", id_str);
6847 free(id_str);
6848 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6852 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6853 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6854 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6855 "last commit in histedit script cannot be folded");
6857 return NULL;
6860 static const struct got_error *
6861 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6862 const char *path, struct got_object_id_queue *commits,
6863 struct got_repository *repo)
6865 const struct got_error *err = NULL;
6866 char *editor;
6867 FILE *f = NULL;
6869 err = get_editor(&editor);
6870 if (err)
6871 return err;
6873 if (spawn_editor(editor, path) == -1) {
6874 err = got_error_from_errno("failed spawning editor");
6875 goto done;
6878 f = fopen(path, "r");
6879 if (f == NULL) {
6880 err = got_error_from_errno("fopen");
6881 goto done;
6883 err = histedit_parse_list(histedit_cmds, f, repo);
6884 if (err)
6885 goto done;
6887 err = histedit_check_script(histedit_cmds, commits, repo);
6888 done:
6889 if (f && fclose(f) != 0 && err == NULL)
6890 err = got_error_from_errno("fclose");
6891 free(editor);
6892 return err;
6895 static const struct got_error *
6896 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6897 struct got_object_id_queue *, const char *, const char *,
6898 struct got_repository *);
6900 static const struct got_error *
6901 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6902 struct got_object_id_queue *commits, const char *branch_name,
6903 int edit_logmsg_only, struct got_repository *repo)
6905 const struct got_error *err;
6906 FILE *f = NULL;
6907 char *path = NULL;
6909 err = got_opentemp_named(&path, &f, "got-histedit");
6910 if (err)
6911 return err;
6913 err = write_cmd_list(f, branch_name, commits);
6914 if (err)
6915 goto done;
6917 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6918 if (err)
6919 goto done;
6921 if (edit_logmsg_only) {
6922 rewind(f);
6923 err = histedit_parse_list(histedit_cmds, f, repo);
6924 } else {
6925 if (fclose(f) != 0) {
6926 err = got_error_from_errno("fclose");
6927 goto done;
6929 f = NULL;
6930 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6931 if (err) {
6932 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6933 err->code != GOT_ERR_HISTEDIT_CMD)
6934 goto done;
6935 err = histedit_edit_list_retry(histedit_cmds, err,
6936 commits, path, branch_name, repo);
6939 done:
6940 if (f && fclose(f) != 0 && err == NULL)
6941 err = got_error_from_errno("fclose");
6942 if (path && unlink(path) != 0 && err == NULL)
6943 err = got_error_from_errno2("unlink", path);
6944 free(path);
6945 return err;
6948 static const struct got_error *
6949 histedit_save_list(struct got_histedit_list *histedit_cmds,
6950 struct got_worktree *worktree, struct got_repository *repo)
6952 const struct got_error *err = NULL;
6953 char *path = NULL;
6954 FILE *f = NULL;
6955 struct got_histedit_list_entry *hle;
6956 struct got_commit_object *commit = NULL;
6958 err = got_worktree_get_histedit_script_path(&path, worktree);
6959 if (err)
6960 return err;
6962 f = fopen(path, "w");
6963 if (f == NULL) {
6964 err = got_error_from_errno2("fopen", path);
6965 goto done;
6967 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6968 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6969 repo);
6970 if (err)
6971 break;
6973 if (hle->logmsg) {
6974 int n = fprintf(f, "%c %s\n",
6975 GOT_HISTEDIT_MESG, hle->logmsg);
6976 if (n < 0) {
6977 err = got_ferror(f, GOT_ERR_IO);
6978 break;
6982 done:
6983 if (f && fclose(f) != 0 && err == NULL)
6984 err = got_error_from_errno("fclose");
6985 free(path);
6986 if (commit)
6987 got_object_commit_close(commit);
6988 return err;
6991 void
6992 histedit_free_list(struct got_histedit_list *histedit_cmds)
6994 struct got_histedit_list_entry *hle;
6996 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6997 TAILQ_REMOVE(histedit_cmds, hle, entry);
6998 free(hle);
7002 static const struct got_error *
7003 histedit_load_list(struct got_histedit_list *histedit_cmds,
7004 const char *path, struct got_repository *repo)
7006 const struct got_error *err = NULL;
7007 FILE *f = NULL;
7009 f = fopen(path, "r");
7010 if (f == NULL) {
7011 err = got_error_from_errno2("fopen", path);
7012 goto done;
7015 err = histedit_parse_list(histedit_cmds, f, repo);
7016 done:
7017 if (f && fclose(f) != 0 && err == NULL)
7018 err = got_error_from_errno("fclose");
7019 return err;
7022 static const struct got_error *
7023 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7024 const struct got_error *edit_err, struct got_object_id_queue *commits,
7025 const char *path, const char *branch_name, struct got_repository *repo)
7027 const struct got_error *err = NULL, *prev_err = edit_err;
7028 int resp = ' ';
7030 while (resp != 'c' && resp != 'r' && resp != 'a') {
7031 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7032 "or (a)bort: ", getprogname(), prev_err->msg);
7033 resp = getchar();
7034 if (resp == '\n')
7035 resp = getchar();
7036 if (resp == 'c') {
7037 histedit_free_list(histedit_cmds);
7038 err = histedit_run_editor(histedit_cmds, path, commits,
7039 repo);
7040 if (err) {
7041 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7042 err->code != GOT_ERR_HISTEDIT_CMD)
7043 break;
7044 prev_err = err;
7045 resp = ' ';
7046 continue;
7048 break;
7049 } else if (resp == 'r') {
7050 histedit_free_list(histedit_cmds);
7051 err = histedit_edit_script(histedit_cmds,
7052 commits, branch_name, 0, repo);
7053 if (err) {
7054 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7055 err->code != GOT_ERR_HISTEDIT_CMD)
7056 break;
7057 prev_err = err;
7058 resp = ' ';
7059 continue;
7061 break;
7062 } else if (resp == 'a') {
7063 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7064 break;
7065 } else
7066 printf("invalid response '%c'\n", resp);
7069 return err;
7072 static const struct got_error *
7073 histedit_complete(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7075 struct got_reference *branch, struct got_repository *repo)
7077 printf("Switching work tree to %s\n",
7078 got_ref_get_symref_target(branch));
7079 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7080 branch, repo);
7083 static const struct got_error *
7084 show_histedit_progress(struct got_commit_object *commit,
7085 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7087 const struct got_error *err;
7088 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7090 err = got_object_id_str(&old_id_str, hle->commit_id);
7091 if (err)
7092 goto done;
7094 if (new_id) {
7095 err = got_object_id_str(&new_id_str, new_id);
7096 if (err)
7097 goto done;
7100 old_id_str[12] = '\0';
7101 if (new_id_str)
7102 new_id_str[12] = '\0';
7104 if (hle->logmsg) {
7105 logmsg = strdup(hle->logmsg);
7106 if (logmsg == NULL) {
7107 err = got_error_from_errno("strdup");
7108 goto done;
7110 trim_logmsg(logmsg, 42);
7111 } else {
7112 err = get_short_logmsg(&logmsg, 42, commit);
7113 if (err)
7114 goto done;
7117 switch (hle->cmd->code) {
7118 case GOT_HISTEDIT_PICK:
7119 case GOT_HISTEDIT_EDIT:
7120 printf("%s -> %s: %s\n", old_id_str,
7121 new_id_str ? new_id_str : "no-op change", logmsg);
7122 break;
7123 case GOT_HISTEDIT_DROP:
7124 case GOT_HISTEDIT_FOLD:
7125 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7126 logmsg);
7127 break;
7128 default:
7129 break;
7131 done:
7132 free(old_id_str);
7133 free(new_id_str);
7134 return err;
7137 static const struct got_error *
7138 histedit_commit(struct got_pathlist_head *merged_paths,
7139 struct got_worktree *worktree, struct got_fileindex *fileindex,
7140 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7141 struct got_repository *repo)
7143 const struct got_error *err;
7144 struct got_commit_object *commit;
7145 struct got_object_id *new_commit_id;
7147 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7148 && hle->logmsg == NULL) {
7149 err = histedit_edit_logmsg(hle, repo);
7150 if (err)
7151 return err;
7154 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7155 if (err)
7156 return err;
7158 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7159 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7160 hle->logmsg, repo);
7161 if (err) {
7162 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7163 goto done;
7164 err = show_histedit_progress(commit, hle, NULL);
7165 } else {
7166 err = show_histedit_progress(commit, hle, new_commit_id);
7167 free(new_commit_id);
7169 done:
7170 got_object_commit_close(commit);
7171 return err;
7174 static const struct got_error *
7175 histedit_skip_commit(struct got_histedit_list_entry *hle,
7176 struct got_worktree *worktree, struct got_repository *repo)
7178 const struct got_error *error;
7179 struct got_commit_object *commit;
7181 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7182 repo);
7183 if (error)
7184 return error;
7186 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7187 if (error)
7188 return error;
7190 error = show_histedit_progress(commit, hle, NULL);
7191 got_object_commit_close(commit);
7192 return error;
7195 static const struct got_error *
7196 check_local_changes(void *arg, unsigned char status,
7197 unsigned char staged_status, const char *path,
7198 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7199 struct got_object_id *commit_id, int dirfd, const char *de_name)
7201 int *have_local_changes = arg;
7203 switch (status) {
7204 case GOT_STATUS_ADD:
7205 case GOT_STATUS_DELETE:
7206 case GOT_STATUS_MODIFY:
7207 case GOT_STATUS_CONFLICT:
7208 *have_local_changes = 1;
7209 return got_error(GOT_ERR_CANCELLED);
7210 default:
7211 break;
7214 switch (staged_status) {
7215 case GOT_STATUS_ADD:
7216 case GOT_STATUS_DELETE:
7217 case GOT_STATUS_MODIFY:
7218 *have_local_changes = 1;
7219 return got_error(GOT_ERR_CANCELLED);
7220 default:
7221 break;
7224 return NULL;
7227 static const struct got_error *
7228 cmd_histedit(int argc, char *argv[])
7230 const struct got_error *error = NULL;
7231 struct got_worktree *worktree = NULL;
7232 struct got_fileindex *fileindex = NULL;
7233 struct got_repository *repo = NULL;
7234 char *cwd = NULL;
7235 struct got_reference *branch = NULL;
7236 struct got_reference *tmp_branch = NULL;
7237 struct got_object_id *resume_commit_id = NULL;
7238 struct got_object_id *base_commit_id = NULL;
7239 struct got_object_id *head_commit_id = NULL;
7240 struct got_commit_object *commit = NULL;
7241 int ch, rebase_in_progress = 0, did_something;
7242 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7243 int edit_logmsg_only = 0;
7244 const char *edit_script_path = NULL;
7245 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7246 struct got_object_id_queue commits;
7247 struct got_pathlist_head merged_paths;
7248 const struct got_object_id_queue *parent_ids;
7249 struct got_object_qid *pid;
7250 struct got_histedit_list histedit_cmds;
7251 struct got_histedit_list_entry *hle;
7253 SIMPLEQ_INIT(&commits);
7254 TAILQ_INIT(&histedit_cmds);
7255 TAILQ_INIT(&merged_paths);
7257 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7258 switch (ch) {
7259 case 'a':
7260 abort_edit = 1;
7261 break;
7262 case 'c':
7263 continue_edit = 1;
7264 break;
7265 case 'F':
7266 edit_script_path = optarg;
7267 break;
7268 case 'm':
7269 edit_logmsg_only = 1;
7270 break;
7271 default:
7272 usage_histedit();
7273 /* NOTREACHED */
7277 argc -= optind;
7278 argv += optind;
7280 #ifndef PROFILE
7281 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7282 "unveil", NULL) == -1)
7283 err(1, "pledge");
7284 #endif
7285 if (abort_edit && continue_edit)
7286 errx(1, "histedit's -a and -c options are mutually exclusive");
7287 if (edit_script_path && edit_logmsg_only)
7288 errx(1, "histedit's -F and -m options are mutually exclusive");
7289 if (abort_edit && edit_logmsg_only)
7290 errx(1, "histedit's -a and -m options are mutually exclusive");
7291 if (continue_edit && edit_logmsg_only)
7292 errx(1, "histedit's -c and -m options are mutually exclusive");
7293 if (argc != 0)
7294 usage_histedit();
7297 * This command cannot apply unveil(2) in all cases because the
7298 * user may choose to run an editor to edit the histedit script
7299 * and to edit individual commit log messages.
7300 * unveil(2) traverses exec(2); if an editor is used we have to
7301 * apply unveil after edit script and log messages have been written.
7302 * XXX TODO: Make use of unveil(2) where possible.
7305 cwd = getcwd(NULL, 0);
7306 if (cwd == NULL) {
7307 error = got_error_from_errno("getcwd");
7308 goto done;
7310 error = got_worktree_open(&worktree, cwd);
7311 if (error)
7312 goto done;
7314 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7315 NULL);
7316 if (error != NULL)
7317 goto done;
7319 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7320 if (error)
7321 goto done;
7322 if (rebase_in_progress) {
7323 error = got_error(GOT_ERR_REBASING);
7324 goto done;
7327 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7328 if (error)
7329 goto done;
7331 if (edit_in_progress && edit_logmsg_only) {
7332 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7333 "histedit operation is in progress in this "
7334 "work tree and must be continued or aborted "
7335 "before the -m option can be used");
7336 goto done;
7339 if (edit_in_progress && abort_edit) {
7340 error = got_worktree_histedit_continue(&resume_commit_id,
7341 &tmp_branch, &branch, &base_commit_id, &fileindex,
7342 worktree, repo);
7343 if (error)
7344 goto done;
7345 printf("Switching work tree to %s\n",
7346 got_ref_get_symref_target(branch));
7347 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7348 branch, base_commit_id, update_progress, &did_something);
7349 if (error)
7350 goto done;
7351 printf("Histedit of %s aborted\n",
7352 got_ref_get_symref_target(branch));
7353 goto done; /* nothing else to do */
7354 } else if (abort_edit) {
7355 error = got_error(GOT_ERR_NOT_HISTEDIT);
7356 goto done;
7359 if (continue_edit) {
7360 char *path;
7362 if (!edit_in_progress) {
7363 error = got_error(GOT_ERR_NOT_HISTEDIT);
7364 goto done;
7367 error = got_worktree_get_histedit_script_path(&path, worktree);
7368 if (error)
7369 goto done;
7371 error = histedit_load_list(&histedit_cmds, path, repo);
7372 free(path);
7373 if (error)
7374 goto done;
7376 error = got_worktree_histedit_continue(&resume_commit_id,
7377 &tmp_branch, &branch, &base_commit_id, &fileindex,
7378 worktree, repo);
7379 if (error)
7380 goto done;
7382 error = got_ref_resolve(&head_commit_id, repo, branch);
7383 if (error)
7384 goto done;
7386 error = got_object_open_as_commit(&commit, repo,
7387 head_commit_id);
7388 if (error)
7389 goto done;
7390 parent_ids = got_object_commit_get_parent_ids(commit);
7391 pid = SIMPLEQ_FIRST(parent_ids);
7392 if (pid == NULL) {
7393 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7394 goto done;
7396 error = collect_commits(&commits, head_commit_id, pid->id,
7397 base_commit_id, got_worktree_get_path_prefix(worktree),
7398 GOT_ERR_HISTEDIT_PATH, repo);
7399 got_object_commit_close(commit);
7400 commit = NULL;
7401 if (error)
7402 goto done;
7403 } else {
7404 if (edit_in_progress) {
7405 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7406 goto done;
7409 error = got_ref_open(&branch, repo,
7410 got_worktree_get_head_ref_name(worktree), 0);
7411 if (error != NULL)
7412 goto done;
7414 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7415 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7416 "will not edit commit history of a branch outside "
7417 "the \"refs/heads/\" reference namespace");
7418 goto done;
7421 error = got_ref_resolve(&head_commit_id, repo, branch);
7422 got_ref_close(branch);
7423 branch = NULL;
7424 if (error)
7425 goto done;
7427 error = got_object_open_as_commit(&commit, repo,
7428 head_commit_id);
7429 if (error)
7430 goto done;
7431 parent_ids = got_object_commit_get_parent_ids(commit);
7432 pid = SIMPLEQ_FIRST(parent_ids);
7433 if (pid == NULL) {
7434 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7435 goto done;
7437 error = collect_commits(&commits, head_commit_id, pid->id,
7438 got_worktree_get_base_commit_id(worktree),
7439 got_worktree_get_path_prefix(worktree),
7440 GOT_ERR_HISTEDIT_PATH, repo);
7441 got_object_commit_close(commit);
7442 commit = NULL;
7443 if (error)
7444 goto done;
7446 if (SIMPLEQ_EMPTY(&commits)) {
7447 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7448 goto done;
7451 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7452 &base_commit_id, &fileindex, worktree, repo);
7453 if (error)
7454 goto done;
7456 if (edit_script_path) {
7457 error = histedit_load_list(&histedit_cmds,
7458 edit_script_path, repo);
7459 if (error) {
7460 got_worktree_histedit_abort(worktree, fileindex,
7461 repo, branch, base_commit_id,
7462 update_progress, &did_something);
7463 goto done;
7465 } else {
7466 const char *branch_name;
7467 branch_name = got_ref_get_symref_target(branch);
7468 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7469 branch_name += 11;
7470 error = histedit_edit_script(&histedit_cmds, &commits,
7471 branch_name, edit_logmsg_only, repo);
7472 if (error) {
7473 got_worktree_histedit_abort(worktree, fileindex,
7474 repo, branch, base_commit_id,
7475 update_progress, &did_something);
7476 goto done;
7481 error = histedit_save_list(&histedit_cmds, worktree,
7482 repo);
7483 if (error) {
7484 got_worktree_histedit_abort(worktree, fileindex,
7485 repo, branch, base_commit_id,
7486 update_progress, &did_something);
7487 goto done;
7492 error = histedit_check_script(&histedit_cmds, &commits, repo);
7493 if (error)
7494 goto done;
7496 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7497 if (resume_commit_id) {
7498 if (got_object_id_cmp(hle->commit_id,
7499 resume_commit_id) != 0)
7500 continue;
7502 resume_commit_id = NULL;
7503 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7504 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7505 error = histedit_skip_commit(hle, worktree,
7506 repo);
7507 if (error)
7508 goto done;
7509 } else {
7510 struct got_pathlist_head paths;
7511 int have_changes = 0;
7513 TAILQ_INIT(&paths);
7514 error = got_pathlist_append(&paths, "", NULL);
7515 if (error)
7516 goto done;
7517 error = got_worktree_status(worktree, &paths,
7518 repo, check_local_changes, &have_changes,
7519 check_cancelled, NULL);
7520 got_pathlist_free(&paths);
7521 if (error) {
7522 if (error->code != GOT_ERR_CANCELLED)
7523 goto done;
7524 if (sigint_received || sigpipe_received)
7525 goto done;
7527 if (have_changes) {
7528 error = histedit_commit(NULL, worktree,
7529 fileindex, tmp_branch, hle, repo);
7530 if (error)
7531 goto done;
7532 } else {
7533 error = got_object_open_as_commit(
7534 &commit, repo, hle->commit_id);
7535 if (error)
7536 goto done;
7537 error = show_histedit_progress(commit,
7538 hle, NULL);
7539 got_object_commit_close(commit);
7540 commit = NULL;
7541 if (error)
7542 goto done;
7545 continue;
7548 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7549 error = histedit_skip_commit(hle, worktree, repo);
7550 if (error)
7551 goto done;
7552 continue;
7555 error = got_object_open_as_commit(&commit, repo,
7556 hle->commit_id);
7557 if (error)
7558 goto done;
7559 parent_ids = got_object_commit_get_parent_ids(commit);
7560 pid = SIMPLEQ_FIRST(parent_ids);
7562 error = got_worktree_histedit_merge_files(&merged_paths,
7563 worktree, fileindex, pid->id, hle->commit_id, repo,
7564 rebase_progress, &rebase_status, check_cancelled, NULL);
7565 if (error)
7566 goto done;
7567 got_object_commit_close(commit);
7568 commit = NULL;
7570 if (rebase_status == GOT_STATUS_CONFLICT) {
7571 error = show_rebase_merge_conflict(hle->commit_id,
7572 repo);
7573 if (error)
7574 goto done;
7575 got_worktree_rebase_pathlist_free(&merged_paths);
7576 break;
7579 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7580 char *id_str;
7581 error = got_object_id_str(&id_str, hle->commit_id);
7582 if (error)
7583 goto done;
7584 printf("Stopping histedit for amending commit %s\n",
7585 id_str);
7586 free(id_str);
7587 got_worktree_rebase_pathlist_free(&merged_paths);
7588 error = got_worktree_histedit_postpone(worktree,
7589 fileindex);
7590 goto done;
7593 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7594 error = histedit_skip_commit(hle, worktree, repo);
7595 if (error)
7596 goto done;
7597 continue;
7600 error = histedit_commit(&merged_paths, worktree, fileindex,
7601 tmp_branch, hle, repo);
7602 got_worktree_rebase_pathlist_free(&merged_paths);
7603 if (error)
7604 goto done;
7607 if (rebase_status == GOT_STATUS_CONFLICT) {
7608 error = got_worktree_histedit_postpone(worktree, fileindex);
7609 if (error)
7610 goto done;
7611 error = got_error_msg(GOT_ERR_CONFLICTS,
7612 "conflicts must be resolved before histedit can continue");
7613 } else
7614 error = histedit_complete(worktree, fileindex, tmp_branch,
7615 branch, repo);
7616 done:
7617 got_object_id_queue_free(&commits);
7618 histedit_free_list(&histedit_cmds);
7619 free(head_commit_id);
7620 free(base_commit_id);
7621 free(resume_commit_id);
7622 if (commit)
7623 got_object_commit_close(commit);
7624 if (branch)
7625 got_ref_close(branch);
7626 if (tmp_branch)
7627 got_ref_close(tmp_branch);
7628 if (worktree)
7629 got_worktree_close(worktree);
7630 if (repo)
7631 got_repo_close(repo);
7632 return error;
7635 __dead static void
7636 usage_integrate(void)
7638 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7639 exit(1);
7642 static const struct got_error *
7643 cmd_integrate(int argc, char *argv[])
7645 const struct got_error *error = NULL;
7646 struct got_repository *repo = NULL;
7647 struct got_worktree *worktree = NULL;
7648 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7649 const char *branch_arg = NULL;
7650 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7651 struct got_fileindex *fileindex = NULL;
7652 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7653 int ch, did_something = 0;
7655 while ((ch = getopt(argc, argv, "")) != -1) {
7656 switch (ch) {
7657 default:
7658 usage_integrate();
7659 /* NOTREACHED */
7663 argc -= optind;
7664 argv += optind;
7666 if (argc != 1)
7667 usage_integrate();
7668 branch_arg = argv[0];
7670 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7671 "unveil", NULL) == -1)
7672 err(1, "pledge");
7674 cwd = getcwd(NULL, 0);
7675 if (cwd == NULL) {
7676 error = got_error_from_errno("getcwd");
7677 goto done;
7680 error = got_worktree_open(&worktree, cwd);
7681 if (error)
7682 goto done;
7684 error = check_rebase_or_histedit_in_progress(worktree);
7685 if (error)
7686 goto done;
7688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7689 NULL);
7690 if (error != NULL)
7691 goto done;
7693 error = apply_unveil(got_repo_get_path(repo), 0,
7694 got_worktree_get_root_path(worktree));
7695 if (error)
7696 goto done;
7698 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7699 error = got_error_from_errno("asprintf");
7700 goto done;
7703 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7704 &base_branch_ref, worktree, refname, repo);
7705 if (error)
7706 goto done;
7708 refname = strdup(got_ref_get_name(branch_ref));
7709 if (refname == NULL) {
7710 error = got_error_from_errno("strdup");
7711 got_worktree_integrate_abort(worktree, fileindex, repo,
7712 branch_ref, base_branch_ref);
7713 goto done;
7715 base_refname = strdup(got_ref_get_name(base_branch_ref));
7716 if (base_refname == NULL) {
7717 error = got_error_from_errno("strdup");
7718 got_worktree_integrate_abort(worktree, fileindex, repo,
7719 branch_ref, base_branch_ref);
7720 goto done;
7723 error = got_ref_resolve(&commit_id, repo, branch_ref);
7724 if (error)
7725 goto done;
7727 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7728 if (error)
7729 goto done;
7731 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7732 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7733 "specified branch has already been integrated");
7734 got_worktree_integrate_abort(worktree, fileindex, repo,
7735 branch_ref, base_branch_ref);
7736 goto done;
7739 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7740 if (error) {
7741 if (error->code == GOT_ERR_ANCESTRY)
7742 error = got_error(GOT_ERR_REBASE_REQUIRED);
7743 got_worktree_integrate_abort(worktree, fileindex, repo,
7744 branch_ref, base_branch_ref);
7745 goto done;
7748 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7749 branch_ref, base_branch_ref, update_progress, &did_something,
7750 check_cancelled, NULL);
7751 if (error)
7752 goto done;
7754 printf("Integrated %s into %s\n", refname, base_refname);
7755 done:
7756 if (repo)
7757 got_repo_close(repo);
7758 if (worktree)
7759 got_worktree_close(worktree);
7760 free(cwd);
7761 free(base_commit_id);
7762 free(commit_id);
7763 free(refname);
7764 free(base_refname);
7765 return error;
7768 __dead static void
7769 usage_stage(void)
7771 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7772 "[file-path ...]\n",
7773 getprogname());
7774 exit(1);
7777 static const struct got_error *
7778 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7779 const char *path, struct got_object_id *blob_id,
7780 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7781 int dirfd, const char *de_name)
7783 const struct got_error *err = NULL;
7784 char *id_str = NULL;
7786 if (staged_status != GOT_STATUS_ADD &&
7787 staged_status != GOT_STATUS_MODIFY &&
7788 staged_status != GOT_STATUS_DELETE)
7789 return NULL;
7791 if (staged_status == GOT_STATUS_ADD ||
7792 staged_status == GOT_STATUS_MODIFY)
7793 err = got_object_id_str(&id_str, staged_blob_id);
7794 else
7795 err = got_object_id_str(&id_str, blob_id);
7796 if (err)
7797 return err;
7799 printf("%s %c %s\n", id_str, staged_status, path);
7800 free(id_str);
7801 return NULL;
7804 static const struct got_error *
7805 cmd_stage(int argc, char *argv[])
7807 const struct got_error *error = NULL;
7808 struct got_repository *repo = NULL;
7809 struct got_worktree *worktree = NULL;
7810 char *cwd = NULL;
7811 struct got_pathlist_head paths;
7812 struct got_pathlist_entry *pe;
7813 int ch, list_stage = 0, pflag = 0;
7814 FILE *patch_script_file = NULL;
7815 const char *patch_script_path = NULL;
7816 struct choose_patch_arg cpa;
7818 TAILQ_INIT(&paths);
7820 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7821 switch (ch) {
7822 case 'l':
7823 list_stage = 1;
7824 break;
7825 case 'p':
7826 pflag = 1;
7827 break;
7828 case 'F':
7829 patch_script_path = optarg;
7830 break;
7831 default:
7832 usage_stage();
7833 /* NOTREACHED */
7837 argc -= optind;
7838 argv += optind;
7840 #ifndef PROFILE
7841 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7842 "unveil", NULL) == -1)
7843 err(1, "pledge");
7844 #endif
7845 if (list_stage && (pflag || patch_script_path))
7846 errx(1, "-l option cannot be used with other options");
7847 if (patch_script_path && !pflag)
7848 errx(1, "-F option can only be used together with -p option");
7850 cwd = getcwd(NULL, 0);
7851 if (cwd == NULL) {
7852 error = got_error_from_errno("getcwd");
7853 goto done;
7856 error = got_worktree_open(&worktree, cwd);
7857 if (error)
7858 goto done;
7860 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7861 NULL);
7862 if (error != NULL)
7863 goto done;
7865 if (patch_script_path) {
7866 patch_script_file = fopen(patch_script_path, "r");
7867 if (patch_script_file == NULL) {
7868 error = got_error_from_errno2("fopen",
7869 patch_script_path);
7870 goto done;
7873 error = apply_unveil(got_repo_get_path(repo), 0,
7874 got_worktree_get_root_path(worktree));
7875 if (error)
7876 goto done;
7878 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7879 if (error)
7880 goto done;
7882 if (list_stage)
7883 error = got_worktree_status(worktree, &paths, repo,
7884 print_stage, NULL, check_cancelled, NULL);
7885 else {
7886 cpa.patch_script_file = patch_script_file;
7887 cpa.action = "stage";
7888 error = got_worktree_stage(worktree, &paths,
7889 pflag ? NULL : print_status, NULL,
7890 pflag ? choose_patch : NULL, &cpa, repo);
7892 done:
7893 if (patch_script_file && fclose(patch_script_file) == EOF &&
7894 error == NULL)
7895 error = got_error_from_errno2("fclose", patch_script_path);
7896 if (repo)
7897 got_repo_close(repo);
7898 if (worktree)
7899 got_worktree_close(worktree);
7900 TAILQ_FOREACH(pe, &paths, entry)
7901 free((char *)pe->path);
7902 got_pathlist_free(&paths);
7903 free(cwd);
7904 return error;
7907 __dead static void
7908 usage_unstage(void)
7910 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7911 "[file-path ...]\n",
7912 getprogname());
7913 exit(1);
7917 static const struct got_error *
7918 cmd_unstage(int argc, char *argv[])
7920 const struct got_error *error = NULL;
7921 struct got_repository *repo = NULL;
7922 struct got_worktree *worktree = NULL;
7923 char *cwd = NULL;
7924 struct got_pathlist_head paths;
7925 struct got_pathlist_entry *pe;
7926 int ch, did_something = 0, pflag = 0;
7927 FILE *patch_script_file = NULL;
7928 const char *patch_script_path = NULL;
7929 struct choose_patch_arg cpa;
7931 TAILQ_INIT(&paths);
7933 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7934 switch (ch) {
7935 case 'p':
7936 pflag = 1;
7937 break;
7938 case 'F':
7939 patch_script_path = optarg;
7940 break;
7941 default:
7942 usage_unstage();
7943 /* NOTREACHED */
7947 argc -= optind;
7948 argv += optind;
7950 #ifndef PROFILE
7951 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7952 "unveil", NULL) == -1)
7953 err(1, "pledge");
7954 #endif
7955 if (patch_script_path && !pflag)
7956 errx(1, "-F option can only be used together with -p option");
7958 cwd = getcwd(NULL, 0);
7959 if (cwd == NULL) {
7960 error = got_error_from_errno("getcwd");
7961 goto done;
7964 error = got_worktree_open(&worktree, cwd);
7965 if (error)
7966 goto done;
7968 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7969 NULL);
7970 if (error != NULL)
7971 goto done;
7973 if (patch_script_path) {
7974 patch_script_file = fopen(patch_script_path, "r");
7975 if (patch_script_file == NULL) {
7976 error = got_error_from_errno2("fopen",
7977 patch_script_path);
7978 goto done;
7982 error = apply_unveil(got_repo_get_path(repo), 0,
7983 got_worktree_get_root_path(worktree));
7984 if (error)
7985 goto done;
7987 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7988 if (error)
7989 goto done;
7991 cpa.patch_script_file = patch_script_file;
7992 cpa.action = "unstage";
7993 error = got_worktree_unstage(worktree, &paths, update_progress,
7994 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7995 done:
7996 if (patch_script_file && fclose(patch_script_file) == EOF &&
7997 error == NULL)
7998 error = got_error_from_errno2("fclose", patch_script_path);
7999 if (repo)
8000 got_repo_close(repo);
8001 if (worktree)
8002 got_worktree_close(worktree);
8003 TAILQ_FOREACH(pe, &paths, entry)
8004 free((char *)pe->path);
8005 got_pathlist_free(&paths);
8006 free(cwd);
8007 return error;
8010 __dead static void
8011 usage_cat(void)
8013 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8014 "arg1 [arg2 ...]\n", getprogname());
8015 exit(1);
8018 static const struct got_error *
8019 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8021 const struct got_error *err;
8022 struct got_blob_object *blob;
8024 err = got_object_open_as_blob(&blob, repo, id, 8192);
8025 if (err)
8026 return err;
8028 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8029 got_object_blob_close(blob);
8030 return err;
8033 static const struct got_error *
8034 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8036 const struct got_error *err;
8037 struct got_tree_object *tree;
8038 int nentries, i;
8040 err = got_object_open_as_tree(&tree, repo, id);
8041 if (err)
8042 return err;
8044 nentries = got_object_tree_get_nentries(tree);
8045 for (i = 0; i < nentries; i++) {
8046 struct got_tree_entry *te;
8047 char *id_str;
8048 if (sigint_received || sigpipe_received)
8049 break;
8050 te = got_object_tree_get_entry(tree, i);
8051 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8052 if (err)
8053 break;
8054 fprintf(outfile, "%s %.7o %s\n", id_str,
8055 got_tree_entry_get_mode(te),
8056 got_tree_entry_get_name(te));
8057 free(id_str);
8060 got_object_tree_close(tree);
8061 return err;
8064 static const struct got_error *
8065 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8067 const struct got_error *err;
8068 struct got_commit_object *commit;
8069 const struct got_object_id_queue *parent_ids;
8070 struct got_object_qid *pid;
8071 char *id_str = NULL;
8072 const char *logmsg = NULL;
8074 err = got_object_open_as_commit(&commit, repo, id);
8075 if (err)
8076 return err;
8078 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8079 if (err)
8080 goto done;
8082 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8083 parent_ids = got_object_commit_get_parent_ids(commit);
8084 fprintf(outfile, "numparents %d\n",
8085 got_object_commit_get_nparents(commit));
8086 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8087 char *pid_str;
8088 err = got_object_id_str(&pid_str, pid->id);
8089 if (err)
8090 goto done;
8091 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8092 free(pid_str);
8094 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8095 got_object_commit_get_author(commit),
8096 got_object_commit_get_author_time(commit));
8098 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8099 got_object_commit_get_author(commit),
8100 got_object_commit_get_committer_time(commit));
8102 logmsg = got_object_commit_get_logmsg_raw(commit);
8103 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8104 fprintf(outfile, "%s", logmsg);
8105 done:
8106 free(id_str);
8107 got_object_commit_close(commit);
8108 return err;
8111 static const struct got_error *
8112 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8114 const struct got_error *err;
8115 struct got_tag_object *tag;
8116 char *id_str = NULL;
8117 const char *tagmsg = NULL;
8119 err = got_object_open_as_tag(&tag, repo, id);
8120 if (err)
8121 return err;
8123 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8124 if (err)
8125 goto done;
8127 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8129 switch (got_object_tag_get_object_type(tag)) {
8130 case GOT_OBJ_TYPE_BLOB:
8131 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8132 GOT_OBJ_LABEL_BLOB);
8133 break;
8134 case GOT_OBJ_TYPE_TREE:
8135 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8136 GOT_OBJ_LABEL_TREE);
8137 break;
8138 case GOT_OBJ_TYPE_COMMIT:
8139 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8140 GOT_OBJ_LABEL_COMMIT);
8141 break;
8142 case GOT_OBJ_TYPE_TAG:
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8144 GOT_OBJ_LABEL_TAG);
8145 break;
8146 default:
8147 break;
8150 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8151 got_object_tag_get_name(tag));
8153 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8154 got_object_tag_get_tagger(tag),
8155 got_object_tag_get_tagger_time(tag));
8157 tagmsg = got_object_tag_get_message(tag);
8158 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8159 fprintf(outfile, "%s", tagmsg);
8160 done:
8161 free(id_str);
8162 got_object_tag_close(tag);
8163 return err;
8166 static const struct got_error *
8167 cmd_cat(int argc, char *argv[])
8169 const struct got_error *error;
8170 struct got_repository *repo = NULL;
8171 struct got_worktree *worktree = NULL;
8172 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8173 const char *commit_id_str = NULL;
8174 struct got_object_id *id = NULL, *commit_id = NULL;
8175 int ch, obj_type, i, force_path = 0;
8177 #ifndef PROFILE
8178 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8179 NULL) == -1)
8180 err(1, "pledge");
8181 #endif
8183 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8184 switch (ch) {
8185 case 'c':
8186 commit_id_str = optarg;
8187 break;
8188 case 'r':
8189 repo_path = realpath(optarg, NULL);
8190 if (repo_path == NULL)
8191 return got_error_from_errno2("realpath",
8192 optarg);
8193 got_path_strip_trailing_slashes(repo_path);
8194 break;
8195 case 'P':
8196 force_path = 1;
8197 break;
8198 default:
8199 usage_cat();
8200 /* NOTREACHED */
8204 argc -= optind;
8205 argv += optind;
8207 cwd = getcwd(NULL, 0);
8208 if (cwd == NULL) {
8209 error = got_error_from_errno("getcwd");
8210 goto done;
8212 error = got_worktree_open(&worktree, cwd);
8213 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8214 goto done;
8215 if (worktree) {
8216 if (repo_path == NULL) {
8217 repo_path = strdup(
8218 got_worktree_get_repo_path(worktree));
8219 if (repo_path == NULL) {
8220 error = got_error_from_errno("strdup");
8221 goto done;
8226 if (repo_path == NULL) {
8227 repo_path = getcwd(NULL, 0);
8228 if (repo_path == NULL)
8229 return got_error_from_errno("getcwd");
8232 error = got_repo_open(&repo, repo_path, NULL);
8233 free(repo_path);
8234 if (error != NULL)
8235 goto done;
8237 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8238 if (error)
8239 goto done;
8241 if (commit_id_str == NULL)
8242 commit_id_str = GOT_REF_HEAD;
8243 error = got_repo_match_object_id(&commit_id, NULL,
8244 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8245 if (error)
8246 goto done;
8248 for (i = 0; i < argc; i++) {
8249 if (force_path) {
8250 error = got_object_id_by_path(&id, repo, commit_id,
8251 argv[i]);
8252 if (error)
8253 break;
8254 } else {
8255 error = got_repo_match_object_id(&id, &label, argv[i],
8256 GOT_OBJ_TYPE_ANY, 0, repo);
8257 if (error) {
8258 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8259 error->code != GOT_ERR_NOT_REF)
8260 break;
8261 error = got_object_id_by_path(&id, repo,
8262 commit_id, argv[i]);
8263 if (error)
8264 break;
8268 error = got_object_get_type(&obj_type, repo, id);
8269 if (error)
8270 break;
8272 switch (obj_type) {
8273 case GOT_OBJ_TYPE_BLOB:
8274 error = cat_blob(id, repo, stdout);
8275 break;
8276 case GOT_OBJ_TYPE_TREE:
8277 error = cat_tree(id, repo, stdout);
8278 break;
8279 case GOT_OBJ_TYPE_COMMIT:
8280 error = cat_commit(id, repo, stdout);
8281 break;
8282 case GOT_OBJ_TYPE_TAG:
8283 error = cat_tag(id, repo, stdout);
8284 break;
8285 default:
8286 error = got_error(GOT_ERR_OBJ_TYPE);
8287 break;
8289 if (error)
8290 break;
8291 free(label);
8292 label = NULL;
8293 free(id);
8294 id = NULL;
8296 done:
8297 free(label);
8298 free(id);
8299 free(commit_id);
8300 if (worktree)
8301 got_worktree_close(worktree);
8302 if (repo) {
8303 const struct got_error *repo_error;
8304 repo_error = got_repo_close(repo);
8305 if (error == NULL)
8306 error = repo_error;
8308 return error;