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 "[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 error = NULL;
1086 continue;
1088 goto done;
1091 error = got_ref_alloc_symref(&head_symref,
1092 GOT_REF_HEAD, target_ref);
1093 got_ref_close(target_ref);
1094 if (error)
1095 goto done;
1097 if (verbosity >= 0)
1098 printf("Setting %s to %s\n", GOT_REF_HEAD,
1099 got_ref_get_symref_target(head_symref));
1101 error = got_ref_write(head_symref, repo);
1102 if (error)
1103 goto done;
1106 /* Create a config file git-fetch(1) can understand. */
1107 gitconfig_path = got_repo_get_path_gitconfig(repo);
1108 if (gitconfig_path == NULL) {
1109 error = got_error_from_errno("got_repo_get_path_gitconfig");
1110 goto done;
1112 gitconfig_file = fopen(gitconfig_path, "a");
1113 if (gitconfig_file == NULL) {
1114 error = got_error_from_errno2("fopen", gitconfig_path);
1115 goto done;
1117 if (mirror_references) {
1118 if (asprintf(&gitconfig,
1119 "[remote \"%s\"]\n"
1120 "\turl = %s\n"
1121 "\tfetch = +refs/*:refs/*\n"
1122 "\tmirror = true\n",
1123 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1124 error = got_error_from_errno("asprintf");
1125 goto done;
1127 } else if (fetch_all_branches) {
1128 if (asprintf(&gitconfig,
1129 "[remote \"%s\"]\n"
1130 "\turl = %s\n"
1131 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1132 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1133 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1134 error = got_error_from_errno("asprintf");
1135 goto done;
1137 } else {
1138 const char *branchname;
1141 * If the server specified a default branch, use just that one.
1142 * Otherwise fall back to fetching all branches on next fetch.
1144 if (head_symref) {
1145 branchname = got_ref_get_symref_target(head_symref);
1146 if (strncmp(branchname, "refs/heads/", 11) == 0)
1147 branchname += 11;
1148 } else
1149 branchname = "*"; /* fall back to all branches */
1150 if (asprintf(&gitconfig,
1151 "[remote \"%s\"]\n"
1152 "\turl = %s\n"
1153 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1154 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1155 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1156 branchname) == -1) {
1157 error = got_error_from_errno("asprintf");
1158 goto done;
1161 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1162 if (n != strlen(gitconfig)) {
1163 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1164 goto done;
1168 if (verbosity >= 0)
1169 printf("Created %s repository '%s'\n",
1170 mirror_references ? "mirrored" : "cloned", repo_path);
1171 done:
1172 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1173 error = got_error_from_errno("close");
1174 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1175 error = got_error_from_errno("fclose");
1176 if (repo)
1177 got_repo_close(repo);
1178 if (head_symref)
1179 got_ref_close(head_symref);
1180 TAILQ_FOREACH(pe, &refs, entry) {
1181 free((void *)pe->path);
1182 free(pe->data);
1184 got_pathlist_free(&refs);
1185 TAILQ_FOREACH(pe, &symrefs, entry) {
1186 free((void *)pe->path);
1187 free(pe->data);
1189 got_pathlist_free(&symrefs);
1190 free(pack_hash);
1191 free(proto);
1192 free(host);
1193 free(port);
1194 free(server_path);
1195 free(repo_name);
1196 free(default_destdir);
1197 free(gitconfig_path);
1198 free(git_url);
1199 return error;
1202 static const struct got_error *
1203 create_ref(const char *refname, struct got_object_id *id,
1204 const char *id_str, struct got_repository *repo)
1206 const struct got_error *err = NULL;
1207 struct got_reference *ref;
1209 printf("Creating %s: %s\n", refname, id_str);
1211 err = got_ref_alloc(&ref, refname, id);
1212 if (err)
1213 return err;
1215 err = got_ref_write(ref, repo);
1216 got_ref_close(ref);
1217 return err;
1220 static const struct got_error *
1221 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 char *new_id_str = NULL;
1226 struct got_object_id *old_id = NULL;
1228 err = got_object_id_str(&new_id_str, new_id);
1229 if (err)
1230 goto done;
1232 if (got_ref_is_symbolic(ref)) {
1233 struct got_reference *new_ref;
1234 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1235 if (err)
1236 goto done;
1237 printf("Deleting symbolic reference %s -> %s\n",
1238 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1239 err = got_ref_delete(ref, repo);
1240 if (err)
1241 goto done;
1242 printf("Setting %s to %s\n", got_ref_get_name(ref),
1243 new_id_str);
1244 err = got_ref_write(new_ref, repo);
1245 if (err)
1246 goto done;
1247 } else {
1248 err = got_ref_resolve(&old_id, repo, ref);
1249 if (err)
1250 goto done;
1251 if (got_object_id_cmp(old_id, new_id) != 0) {
1252 printf("Setting %s to %s\n",
1253 got_ref_get_name(ref), new_id_str);
1254 err = got_ref_change_ref(ref, new_id);
1255 if (err)
1256 goto done;
1257 err = got_ref_write(ref, repo);
1258 if (err)
1259 goto done;
1262 done:
1263 free(old_id);
1264 free(new_id_str);
1265 return err;
1268 __dead static void
1269 usage_fetch(void)
1271 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1272 "[remote-repository-name]\n", getprogname());
1273 exit(1);
1276 static const struct got_error *
1277 cmd_fetch(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 char *cwd = NULL, *repo_path = NULL;
1281 const char *remote_name;
1282 char *proto = NULL, *host = NULL, *port = NULL;
1283 char *repo_name = NULL, *server_path = NULL;
1284 struct got_remote_repo *remotes, *remote = NULL;
1285 int nremotes;
1286 char *id_str = NULL;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 struct got_pathlist_head refs, symrefs;
1290 struct got_pathlist_entry *pe;
1291 struct got_object_id *pack_hash = NULL;
1292 int i, ch, fetchfd = -1;
1293 struct got_fetch_progress_arg fpa;
1294 int verbosity = 0, fetch_all_branches = 0;
1296 TAILQ_INIT(&refs);
1297 TAILQ_INIT(&symrefs);
1299 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1300 switch (ch) {
1301 case 'a':
1302 fetch_all_branches = 1;
1303 break;
1304 case 'r':
1305 repo_path = realpath(optarg, NULL);
1306 if (repo_path == NULL)
1307 return got_error_from_errno2("realpath",
1308 optarg);
1309 got_path_strip_trailing_slashes(repo_path);
1310 break;
1311 case 'v':
1312 if (verbosity < 0)
1313 verbosity = 0;
1314 else if (verbosity < 3)
1315 verbosity++;
1316 break;
1317 case 'q':
1318 verbosity = -1;
1319 break;
1320 default:
1321 usage_fetch();
1322 break;
1325 argc -= optind;
1326 argv += optind;
1328 if (argc == 0)
1329 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1330 else if (argc == 1)
1331 remote_name = argv[0];
1332 else
1333 usage_fetch();
1335 cwd = getcwd(NULL, 0);
1336 if (cwd == NULL) {
1337 error = got_error_from_errno("getcwd");
1338 goto done;
1341 if (repo_path == NULL) {
1342 error = got_worktree_open(&worktree, cwd);
1343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1344 goto done;
1345 else
1346 error = NULL;
1347 if (worktree) {
1348 repo_path =
1349 strdup(got_worktree_get_repo_path(worktree));
1350 if (repo_path == NULL)
1351 error = got_error_from_errno("strdup");
1352 if (error)
1353 goto done;
1354 } else {
1355 repo_path = strdup(cwd);
1356 if (repo_path == NULL) {
1357 error = got_error_from_errno("strdup");
1358 goto done;
1363 error = got_repo_open(&repo, repo_path, NULL);
1364 if (error)
1365 goto done;
1367 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1368 for (i = 0; i < nremotes; i++) {
1369 remote = &remotes[i];
1370 if (strcmp(remote->name, remote_name) == 0)
1371 break;
1373 if (i == nremotes) {
1374 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1375 goto done;
1378 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1379 &repo_name, remote->url);
1380 if (error)
1381 goto done;
1383 if (strcmp(proto, "git") == 0) {
1384 #ifndef PROFILE
1385 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1386 "sendfd dns inet unveil", NULL) == -1)
1387 err(1, "pledge");
1388 #endif
1389 } else if (strcmp(proto, "git+ssh") == 0 ||
1390 strcmp(proto, "ssh") == 0) {
1391 #ifndef PROFILE
1392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1393 "sendfd unveil", NULL) == -1)
1394 err(1, "pledge");
1395 #endif
1396 } else if (strcmp(proto, "http") == 0 ||
1397 strcmp(proto, "git+http") == 0) {
1398 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1399 goto done;
1400 } else {
1401 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1402 goto done;
1405 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1406 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1407 error = got_error_from_errno2("unveil",
1408 GOT_FETCH_PATH_SSH);
1409 goto done;
1412 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1413 if (error)
1414 goto done;
1416 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1417 verbosity);
1418 if (error)
1419 goto done;
1421 if (verbosity >= 0)
1422 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1423 port ? ":" : "", port ? port : "");
1425 fpa.last_scaled_size[0] = '\0';
1426 fpa.last_p_indexed = -1;
1427 fpa.last_p_resolved = -1;
1428 fpa.verbosity = verbosity;
1429 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1430 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1431 fetch_progress, &fpa);
1432 if (error)
1433 goto done;
1435 if (pack_hash == NULL) {
1436 if (verbosity >= 0)
1437 printf("Already up-to-date\n");
1438 goto done;
1441 if (verbosity >= 0) {
1442 error = got_object_id_str(&id_str, pack_hash);
1443 if (error)
1444 goto done;
1445 printf("\nFetched %s.pack\n", id_str);
1446 free(id_str);
1447 id_str = NULL;
1450 /* Update references provided with the pack file. */
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 const char *refname = pe->path;
1453 struct got_object_id *id = pe->data;
1454 struct got_reference *ref;
1455 char *remote_refname;
1457 error = got_object_id_str(&id_str, id);
1458 if (error)
1459 goto done;
1461 if (remote->mirror_references ||
1462 strncmp("refs/tags/", refname, 10) == 0) {
1463 error = got_ref_open(&ref, repo, refname, 0);
1464 if (error) {
1465 if (error->code != GOT_ERR_NOT_REF)
1466 goto done;
1467 error = create_ref(refname, id, id_str, repo);
1468 if (error)
1469 goto done;
1470 } else {
1471 error = update_ref(ref, id, repo);
1472 got_ref_close(ref);
1473 if (error)
1474 goto done;
1476 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1477 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1478 remote_name, refname + 11) == -1) {
1479 error = got_error_from_errno("asprintf");
1480 goto done;
1483 error = got_ref_open(&ref, repo, remote_refname, 0);
1484 if (error) {
1485 if (error->code != GOT_ERR_NOT_REF)
1486 goto done;
1487 error = create_ref(remote_refname, id, id_str,
1488 repo);
1489 if (error)
1490 goto done;
1491 } else {
1492 error = update_ref(ref, id, repo);
1493 got_ref_close(ref);
1494 if (error)
1495 goto done;
1498 free(id_str);
1499 id_str = NULL;
1501 done:
1502 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1503 error = got_error_from_errno("close");
1504 if (repo)
1505 got_repo_close(repo);
1506 if (worktree)
1507 got_worktree_close(worktree);
1508 TAILQ_FOREACH(pe, &refs, entry) {
1509 free((void *)pe->path);
1510 free(pe->data);
1512 got_pathlist_free(&refs);
1513 TAILQ_FOREACH(pe, &symrefs, entry) {
1514 free((void *)pe->path);
1515 free(pe->data);
1517 got_pathlist_free(&symrefs);
1518 free(id_str);
1519 free(cwd);
1520 free(repo_path);
1521 free(pack_hash);
1522 free(proto);
1523 free(host);
1524 free(port);
1525 free(server_path);
1526 free(repo_name);
1527 return error;
1531 __dead static void
1532 usage_checkout(void)
1534 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1535 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1536 exit(1);
1539 static void
1540 show_worktree_base_ref_warning(void)
1542 fprintf(stderr, "%s: warning: could not create a reference "
1543 "to the work tree's base commit; the commit could be "
1544 "garbage-collected by Git; making the repository "
1545 "writable and running 'got update' will prevent this\n",
1546 getprogname());
1549 struct got_checkout_progress_arg {
1550 const char *worktree_path;
1551 int had_base_commit_ref_error;
1554 static const struct got_error *
1555 checkout_progress(void *arg, unsigned char status, const char *path)
1557 struct got_checkout_progress_arg *a = arg;
1559 /* Base commit bump happens silently. */
1560 if (status == GOT_STATUS_BUMP_BASE)
1561 return NULL;
1563 if (status == GOT_STATUS_BASE_REF_ERR) {
1564 a->had_base_commit_ref_error = 1;
1565 return NULL;
1568 while (path[0] == '/')
1569 path++;
1571 printf("%c %s/%s\n", status, a->worktree_path, path);
1572 return NULL;
1575 static const struct got_error *
1576 check_cancelled(void *arg)
1578 if (sigint_received || sigpipe_received)
1579 return got_error(GOT_ERR_CANCELLED);
1580 return NULL;
1583 static const struct got_error *
1584 check_linear_ancestry(struct got_object_id *commit_id,
1585 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1586 struct got_repository *repo)
1588 const struct got_error *err = NULL;
1589 struct got_object_id *yca_id;
1591 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1592 commit_id, base_commit_id, repo, check_cancelled, NULL);
1593 if (err)
1594 return err;
1596 if (yca_id == NULL)
1597 return got_error(GOT_ERR_ANCESTRY);
1600 * Require a straight line of history between the target commit
1601 * and the work tree's base commit.
1603 * Non-linear situations such as this require a rebase:
1605 * (commit) D F (base_commit)
1606 * \ /
1607 * C E
1608 * \ /
1609 * B (yca)
1610 * |
1611 * A
1613 * 'got update' only handles linear cases:
1614 * Update forwards in time: A (base/yca) - B - C - D (commit)
1615 * Update backwards in time: D (base) - C - B - A (commit/yca)
1617 if (allow_forwards_in_time_only) {
1618 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1619 return got_error(GOT_ERR_ANCESTRY);
1620 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1621 got_object_id_cmp(base_commit_id, yca_id) != 0)
1622 return got_error(GOT_ERR_ANCESTRY);
1624 free(yca_id);
1625 return NULL;
1628 static const struct got_error *
1629 check_same_branch(struct got_object_id *commit_id,
1630 struct got_reference *head_ref, struct got_object_id *yca_id,
1631 struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_commit_graph *graph = NULL;
1635 struct got_object_id *head_commit_id = NULL;
1636 int is_same_branch = 0;
1638 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1639 if (err)
1640 goto done;
1642 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1643 is_same_branch = 1;
1644 goto done;
1646 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1647 is_same_branch = 1;
1648 goto done;
1651 err = got_commit_graph_open(&graph, "/", 1);
1652 if (err)
1653 goto done;
1655 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1656 check_cancelled, NULL);
1657 if (err)
1658 goto done;
1660 for (;;) {
1661 struct got_object_id *id;
1662 err = got_commit_graph_iter_next(&id, graph, repo,
1663 check_cancelled, NULL);
1664 if (err) {
1665 if (err->code == GOT_ERR_ITER_COMPLETED)
1666 err = NULL;
1667 break;
1670 if (id) {
1671 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1672 break;
1673 if (got_object_id_cmp(id, commit_id) == 0) {
1674 is_same_branch = 1;
1675 break;
1679 done:
1680 if (graph)
1681 got_commit_graph_close(graph);
1682 free(head_commit_id);
1683 if (!err && !is_same_branch)
1684 err = got_error(GOT_ERR_ANCESTRY);
1685 return err;
1688 static const struct got_error *
1689 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1691 static char msg[512];
1692 const char *branch_name;
1694 if (got_ref_is_symbolic(ref))
1695 branch_name = got_ref_get_symref_target(ref);
1696 else
1697 branch_name = got_ref_get_name(ref);
1699 if (strncmp("refs/heads/", branch_name, 11) == 0)
1700 branch_name += 11;
1702 snprintf(msg, sizeof(msg),
1703 "target commit is not contained in branch '%s'; "
1704 "the branch to use must be specified with -b; "
1705 "if necessary a new branch can be created for "
1706 "this commit with 'got branch -c %s BRANCH_NAME'",
1707 branch_name, commit_id_str);
1709 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1712 static const struct got_error *
1713 cmd_checkout(int argc, char *argv[])
1715 const struct got_error *error = NULL;
1716 struct got_repository *repo = NULL;
1717 struct got_reference *head_ref = NULL;
1718 struct got_worktree *worktree = NULL;
1719 char *repo_path = NULL;
1720 char *worktree_path = NULL;
1721 const char *path_prefix = "";
1722 const char *branch_name = GOT_REF_HEAD;
1723 char *commit_id_str = NULL;
1724 int ch, same_path_prefix, allow_nonempty = 0;
1725 struct got_pathlist_head paths;
1726 struct got_checkout_progress_arg cpa;
1728 TAILQ_INIT(&paths);
1730 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1731 switch (ch) {
1732 case 'b':
1733 branch_name = optarg;
1734 break;
1735 case 'c':
1736 commit_id_str = strdup(optarg);
1737 if (commit_id_str == NULL)
1738 return got_error_from_errno("strdup");
1739 break;
1740 case 'E':
1741 allow_nonempty = 1;
1742 break;
1743 case 'p':
1744 path_prefix = optarg;
1745 break;
1746 default:
1747 usage_checkout();
1748 /* NOTREACHED */
1752 argc -= optind;
1753 argv += optind;
1755 #ifndef PROFILE
1756 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1757 "unveil", NULL) == -1)
1758 err(1, "pledge");
1759 #endif
1760 if (argc == 1) {
1761 char *cwd, *base, *dotgit;
1762 repo_path = realpath(argv[0], NULL);
1763 if (repo_path == NULL)
1764 return got_error_from_errno2("realpath", argv[0]);
1765 cwd = getcwd(NULL, 0);
1766 if (cwd == NULL) {
1767 error = got_error_from_errno("getcwd");
1768 goto done;
1770 if (path_prefix[0]) {
1771 base = basename(path_prefix);
1772 if (base == NULL) {
1773 error = got_error_from_errno2("basename",
1774 path_prefix);
1775 goto done;
1777 } else {
1778 base = basename(repo_path);
1779 if (base == NULL) {
1780 error = got_error_from_errno2("basename",
1781 repo_path);
1782 goto done;
1785 dotgit = strstr(base, ".git");
1786 if (dotgit)
1787 *dotgit = '\0';
1788 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1789 error = got_error_from_errno("asprintf");
1790 free(cwd);
1791 goto done;
1793 free(cwd);
1794 } else if (argc == 2) {
1795 repo_path = realpath(argv[0], NULL);
1796 if (repo_path == NULL) {
1797 error = got_error_from_errno2("realpath", argv[0]);
1798 goto done;
1800 worktree_path = realpath(argv[1], NULL);
1801 if (worktree_path == NULL) {
1802 if (errno != ENOENT) {
1803 error = got_error_from_errno2("realpath",
1804 argv[1]);
1805 goto done;
1807 worktree_path = strdup(argv[1]);
1808 if (worktree_path == NULL) {
1809 error = got_error_from_errno("strdup");
1810 goto done;
1813 } else
1814 usage_checkout();
1816 got_path_strip_trailing_slashes(repo_path);
1817 got_path_strip_trailing_slashes(worktree_path);
1819 error = got_repo_open(&repo, repo_path, NULL);
1820 if (error != NULL)
1821 goto done;
1823 /* Pre-create work tree path for unveil(2) */
1824 error = got_path_mkdir(worktree_path);
1825 if (error) {
1826 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1827 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1828 goto done;
1829 if (!allow_nonempty &&
1830 !got_path_dir_is_empty(worktree_path)) {
1831 error = got_error_path(worktree_path,
1832 GOT_ERR_DIR_NOT_EMPTY);
1833 goto done;
1837 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1838 if (error)
1839 goto done;
1841 error = got_ref_open(&head_ref, repo, branch_name, 0);
1842 if (error != NULL)
1843 goto done;
1845 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1846 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1847 goto done;
1849 error = got_worktree_open(&worktree, worktree_path);
1850 if (error != NULL)
1851 goto done;
1853 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1854 path_prefix);
1855 if (error != NULL)
1856 goto done;
1857 if (!same_path_prefix) {
1858 error = got_error(GOT_ERR_PATH_PREFIX);
1859 goto done;
1862 if (commit_id_str) {
1863 struct got_object_id *commit_id;
1864 error = got_repo_match_object_id(&commit_id, NULL,
1865 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1866 if (error)
1867 goto done;
1868 error = check_linear_ancestry(commit_id,
1869 got_worktree_get_base_commit_id(worktree), 0, repo);
1870 if (error != NULL) {
1871 free(commit_id);
1872 if (error->code == GOT_ERR_ANCESTRY) {
1873 error = checkout_ancestry_error(
1874 head_ref, commit_id_str);
1876 goto done;
1878 error = check_same_branch(commit_id, head_ref, NULL, repo);
1879 if (error) {
1880 if (error->code == GOT_ERR_ANCESTRY) {
1881 error = checkout_ancestry_error(
1882 head_ref, commit_id_str);
1884 goto done;
1886 error = got_worktree_set_base_commit_id(worktree, repo,
1887 commit_id);
1888 free(commit_id);
1889 if (error)
1890 goto done;
1893 error = got_pathlist_append(&paths, "", NULL);
1894 if (error)
1895 goto done;
1896 cpa.worktree_path = worktree_path;
1897 cpa.had_base_commit_ref_error = 0;
1898 error = got_worktree_checkout_files(worktree, &paths, repo,
1899 checkout_progress, &cpa, check_cancelled, NULL);
1900 if (error != NULL)
1901 goto done;
1903 printf("Now shut up and hack\n");
1904 if (cpa.had_base_commit_ref_error)
1905 show_worktree_base_ref_warning();
1906 done:
1907 got_pathlist_free(&paths);
1908 free(commit_id_str);
1909 free(repo_path);
1910 free(worktree_path);
1911 return error;
1914 __dead static void
1915 usage_update(void)
1917 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1918 getprogname());
1919 exit(1);
1922 static const struct got_error *
1923 update_progress(void *arg, unsigned char status, const char *path)
1925 int *did_something = arg;
1927 if (status == GOT_STATUS_EXISTS ||
1928 status == GOT_STATUS_BASE_REF_ERR)
1929 return NULL;
1931 *did_something = 1;
1933 /* Base commit bump happens silently. */
1934 if (status == GOT_STATUS_BUMP_BASE)
1935 return NULL;
1937 while (path[0] == '/')
1938 path++;
1939 printf("%c %s\n", status, path);
1940 return NULL;
1943 static const struct got_error *
1944 switch_head_ref(struct got_reference *head_ref,
1945 struct got_object_id *commit_id, struct got_worktree *worktree,
1946 struct got_repository *repo)
1948 const struct got_error *err = NULL;
1949 char *base_id_str;
1950 int ref_has_moved = 0;
1952 /* Trivial case: switching between two different references. */
1953 if (strcmp(got_ref_get_name(head_ref),
1954 got_worktree_get_head_ref_name(worktree)) != 0) {
1955 printf("Switching work tree from %s to %s\n",
1956 got_worktree_get_head_ref_name(worktree),
1957 got_ref_get_name(head_ref));
1958 return got_worktree_set_head_ref(worktree, head_ref);
1961 err = check_linear_ancestry(commit_id,
1962 got_worktree_get_base_commit_id(worktree), 0, repo);
1963 if (err) {
1964 if (err->code != GOT_ERR_ANCESTRY)
1965 return err;
1966 ref_has_moved = 1;
1968 if (!ref_has_moved)
1969 return NULL;
1971 /* Switching to a rebased branch with the same reference name. */
1972 err = got_object_id_str(&base_id_str,
1973 got_worktree_get_base_commit_id(worktree));
1974 if (err)
1975 return err;
1976 printf("Reference %s now points at a different branch\n",
1977 got_worktree_get_head_ref_name(worktree));
1978 printf("Switching work tree from %s to %s\n", base_id_str,
1979 got_worktree_get_head_ref_name(worktree));
1980 return NULL;
1983 static const struct got_error *
1984 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1986 const struct got_error *err;
1987 int in_progress;
1989 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1990 if (err)
1991 return err;
1992 if (in_progress)
1993 return got_error(GOT_ERR_REBASING);
1995 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1996 if (err)
1997 return err;
1998 if (in_progress)
1999 return got_error(GOT_ERR_HISTEDIT_BUSY);
2001 return NULL;
2004 static const struct got_error *
2005 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2006 char *argv[], struct got_worktree *worktree)
2008 const struct got_error *err = NULL;
2009 char *path;
2010 int i;
2012 if (argc == 0) {
2013 path = strdup("");
2014 if (path == NULL)
2015 return got_error_from_errno("strdup");
2016 return got_pathlist_append(paths, path, NULL);
2019 for (i = 0; i < argc; i++) {
2020 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2021 if (err)
2022 break;
2023 err = got_pathlist_append(paths, path, NULL);
2024 if (err) {
2025 free(path);
2026 break;
2030 return err;
2033 static const struct got_error *
2034 cmd_update(int argc, char *argv[])
2036 const struct got_error *error = NULL;
2037 struct got_repository *repo = NULL;
2038 struct got_worktree *worktree = NULL;
2039 char *worktree_path = NULL;
2040 struct got_object_id *commit_id = NULL;
2041 char *commit_id_str = NULL;
2042 const char *branch_name = NULL;
2043 struct got_reference *head_ref = NULL;
2044 struct got_pathlist_head paths;
2045 struct got_pathlist_entry *pe;
2046 int ch, did_something = 0;
2048 TAILQ_INIT(&paths);
2050 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2051 switch (ch) {
2052 case 'b':
2053 branch_name = optarg;
2054 break;
2055 case 'c':
2056 commit_id_str = strdup(optarg);
2057 if (commit_id_str == NULL)
2058 return got_error_from_errno("strdup");
2059 break;
2060 default:
2061 usage_update();
2062 /* NOTREACHED */
2066 argc -= optind;
2067 argv += optind;
2069 #ifndef PROFILE
2070 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2071 "unveil", NULL) == -1)
2072 err(1, "pledge");
2073 #endif
2074 worktree_path = getcwd(NULL, 0);
2075 if (worktree_path == NULL) {
2076 error = got_error_from_errno("getcwd");
2077 goto done;
2079 error = got_worktree_open(&worktree, worktree_path);
2080 if (error)
2081 goto done;
2083 error = check_rebase_or_histedit_in_progress(worktree);
2084 if (error)
2085 goto done;
2087 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2088 NULL);
2089 if (error != NULL)
2090 goto done;
2092 error = apply_unveil(got_repo_get_path(repo), 0,
2093 got_worktree_get_root_path(worktree));
2094 if (error)
2095 goto done;
2097 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2098 if (error)
2099 goto done;
2101 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2102 got_worktree_get_head_ref_name(worktree), 0);
2103 if (error != NULL)
2104 goto done;
2105 if (commit_id_str == NULL) {
2106 error = got_ref_resolve(&commit_id, repo, head_ref);
2107 if (error != NULL)
2108 goto done;
2109 error = got_object_id_str(&commit_id_str, commit_id);
2110 if (error != NULL)
2111 goto done;
2112 } else {
2113 error = got_repo_match_object_id(&commit_id, NULL,
2114 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2115 free(commit_id_str);
2116 commit_id_str = NULL;
2117 if (error)
2118 goto done;
2119 error = got_object_id_str(&commit_id_str, commit_id);
2120 if (error)
2121 goto done;
2124 if (branch_name) {
2125 struct got_object_id *head_commit_id;
2126 TAILQ_FOREACH(pe, &paths, entry) {
2127 if (pe->path_len == 0)
2128 continue;
2129 error = got_error_msg(GOT_ERR_BAD_PATH,
2130 "switching between branches requires that "
2131 "the entire work tree gets updated");
2132 goto done;
2134 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2135 if (error)
2136 goto done;
2137 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2138 repo);
2139 free(head_commit_id);
2140 if (error != NULL)
2141 goto done;
2142 error = check_same_branch(commit_id, head_ref, NULL, repo);
2143 if (error)
2144 goto done;
2145 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2146 if (error)
2147 goto done;
2148 } else {
2149 error = check_linear_ancestry(commit_id,
2150 got_worktree_get_base_commit_id(worktree), 0, repo);
2151 if (error != NULL) {
2152 if (error->code == GOT_ERR_ANCESTRY)
2153 error = got_error(GOT_ERR_BRANCH_MOVED);
2154 goto done;
2156 error = check_same_branch(commit_id, head_ref, NULL, repo);
2157 if (error)
2158 goto done;
2161 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2162 commit_id) != 0) {
2163 error = got_worktree_set_base_commit_id(worktree, repo,
2164 commit_id);
2165 if (error)
2166 goto done;
2169 error = got_worktree_checkout_files(worktree, &paths, repo,
2170 update_progress, &did_something, check_cancelled, NULL);
2171 if (error != NULL)
2172 goto done;
2174 if (did_something)
2175 printf("Updated to commit %s\n", commit_id_str);
2176 else
2177 printf("Already up-to-date\n");
2178 done:
2179 free(worktree_path);
2180 TAILQ_FOREACH(pe, &paths, entry)
2181 free((char *)pe->path);
2182 got_pathlist_free(&paths);
2183 free(commit_id);
2184 free(commit_id_str);
2185 return error;
2188 static const struct got_error *
2189 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2190 const char *path, int diff_context, int ignore_whitespace,
2191 struct got_repository *repo)
2193 const struct got_error *err = NULL;
2194 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2196 if (blob_id1) {
2197 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2198 if (err)
2199 goto done;
2202 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2203 if (err)
2204 goto done;
2206 while (path[0] == '/')
2207 path++;
2208 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2209 ignore_whitespace, stdout);
2210 done:
2211 if (blob1)
2212 got_object_blob_close(blob1);
2213 got_object_blob_close(blob2);
2214 return err;
2217 static const struct got_error *
2218 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2219 const char *path, int diff_context, int ignore_whitespace,
2220 struct got_repository *repo)
2222 const struct got_error *err = NULL;
2223 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2224 struct got_diff_blob_output_unidiff_arg arg;
2226 if (tree_id1) {
2227 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2228 if (err)
2229 goto done;
2232 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2233 if (err)
2234 goto done;
2236 arg.diff_context = diff_context;
2237 arg.ignore_whitespace = ignore_whitespace;
2238 arg.outfile = stdout;
2239 while (path[0] == '/')
2240 path++;
2241 err = got_diff_tree(tree1, tree2, path, path, repo,
2242 got_diff_blob_output_unidiff, &arg, 1);
2243 done:
2244 if (tree1)
2245 got_object_tree_close(tree1);
2246 if (tree2)
2247 got_object_tree_close(tree2);
2248 return err;
2251 static const struct got_error *
2252 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2253 const char *path, int diff_context, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 struct got_commit_object *pcommit = NULL;
2257 char *id_str1 = NULL, *id_str2 = NULL;
2258 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2259 struct got_object_qid *qid;
2261 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2262 if (qid != NULL) {
2263 err = got_object_open_as_commit(&pcommit, repo,
2264 qid->id);
2265 if (err)
2266 return err;
2269 if (path && path[0] != '\0') {
2270 int obj_type;
2271 err = got_object_id_by_path(&obj_id2, repo, id, path);
2272 if (err)
2273 goto done;
2274 err = got_object_id_str(&id_str2, obj_id2);
2275 if (err) {
2276 free(obj_id2);
2277 goto done;
2279 if (pcommit) {
2280 err = got_object_id_by_path(&obj_id1, repo,
2281 qid->id, path);
2282 if (err) {
2283 free(obj_id2);
2284 goto done;
2286 err = got_object_id_str(&id_str1, obj_id1);
2287 if (err) {
2288 free(obj_id2);
2289 goto done;
2292 err = got_object_get_type(&obj_type, repo, obj_id2);
2293 if (err) {
2294 free(obj_id2);
2295 goto done;
2297 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2298 switch (obj_type) {
2299 case GOT_OBJ_TYPE_BLOB:
2300 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2301 0, repo);
2302 break;
2303 case GOT_OBJ_TYPE_TREE:
2304 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2305 0, repo);
2306 break;
2307 default:
2308 err = got_error(GOT_ERR_OBJ_TYPE);
2309 break;
2311 free(obj_id1);
2312 free(obj_id2);
2313 } else {
2314 obj_id2 = got_object_commit_get_tree_id(commit);
2315 err = got_object_id_str(&id_str2, obj_id2);
2316 if (err)
2317 goto done;
2318 obj_id1 = got_object_commit_get_tree_id(pcommit);
2319 err = got_object_id_str(&id_str1, obj_id1);
2320 if (err)
2321 goto done;
2322 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2323 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2325 done:
2326 free(id_str1);
2327 free(id_str2);
2328 if (pcommit)
2329 got_object_commit_close(pcommit);
2330 return err;
2333 static char *
2334 get_datestr(time_t *time, char *datebuf)
2336 struct tm mytm, *tm;
2337 char *p, *s;
2339 tm = gmtime_r(time, &mytm);
2340 if (tm == NULL)
2341 return NULL;
2342 s = asctime_r(tm, datebuf);
2343 if (s == NULL)
2344 return NULL;
2345 p = strchr(s, '\n');
2346 if (p)
2347 *p = '\0';
2348 return s;
2351 static const struct got_error *
2352 match_logmsg(int *have_match, struct got_object_id *id,
2353 struct got_commit_object *commit, regex_t *regex)
2355 const struct got_error *err = NULL;
2356 regmatch_t regmatch;
2357 char *id_str = NULL, *logmsg = NULL;
2359 *have_match = 0;
2361 err = got_object_id_str(&id_str, id);
2362 if (err)
2363 return err;
2365 err = got_object_commit_get_logmsg(&logmsg, commit);
2366 if (err)
2367 goto done;
2369 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2370 *have_match = 1;
2371 done:
2372 free(id_str);
2373 free(logmsg);
2374 return err;
2377 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2379 static const struct got_error *
2380 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2381 struct got_repository *repo, const char *path, int show_patch,
2382 int diff_context, struct got_reflist_head *refs)
2384 const struct got_error *err = NULL;
2385 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2386 char datebuf[26];
2387 time_t committer_time;
2388 const char *author, *committer;
2389 char *refs_str = NULL;
2390 struct got_reflist_entry *re;
2392 SIMPLEQ_FOREACH(re, refs, entry) {
2393 char *s;
2394 const char *name;
2395 struct got_tag_object *tag = NULL;
2396 int cmp;
2398 name = got_ref_get_name(re->ref);
2399 if (strcmp(name, GOT_REF_HEAD) == 0)
2400 continue;
2401 if (strncmp(name, "refs/", 5) == 0)
2402 name += 5;
2403 if (strncmp(name, "got/", 4) == 0)
2404 continue;
2405 if (strncmp(name, "heads/", 6) == 0)
2406 name += 6;
2407 if (strncmp(name, "remotes/", 8) == 0)
2408 name += 8;
2409 if (strncmp(name, "tags/", 5) == 0) {
2410 err = got_object_open_as_tag(&tag, repo, re->id);
2411 if (err) {
2412 if (err->code != GOT_ERR_OBJ_TYPE)
2413 return err;
2414 /* Ref points at something other than a tag. */
2415 err = NULL;
2416 tag = NULL;
2419 cmp = got_object_id_cmp(tag ?
2420 got_object_tag_get_object_id(tag) : re->id, id);
2421 if (tag)
2422 got_object_tag_close(tag);
2423 if (cmp != 0)
2424 continue;
2425 s = refs_str;
2426 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2427 name) == -1) {
2428 err = got_error_from_errno("asprintf");
2429 free(s);
2430 return err;
2432 free(s);
2434 err = got_object_id_str(&id_str, id);
2435 if (err)
2436 return err;
2438 printf(GOT_COMMIT_SEP_STR);
2439 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2440 refs_str ? refs_str : "", refs_str ? ")" : "");
2441 free(id_str);
2442 id_str = NULL;
2443 free(refs_str);
2444 refs_str = NULL;
2445 printf("from: %s\n", got_object_commit_get_author(commit));
2446 committer_time = got_object_commit_get_committer_time(commit);
2447 datestr = get_datestr(&committer_time, datebuf);
2448 if (datestr)
2449 printf("date: %s UTC\n", datestr);
2450 author = got_object_commit_get_author(commit);
2451 committer = got_object_commit_get_committer(commit);
2452 if (strcmp(author, committer) != 0)
2453 printf("via: %s\n", committer);
2454 if (got_object_commit_get_nparents(commit) > 1) {
2455 const struct got_object_id_queue *parent_ids;
2456 struct got_object_qid *qid;
2457 int n = 1;
2458 parent_ids = got_object_commit_get_parent_ids(commit);
2459 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2460 err = got_object_id_str(&id_str, qid->id);
2461 if (err)
2462 return err;
2463 printf("parent %d: %s\n", n++, id_str);
2464 free(id_str);
2468 err = got_object_commit_get_logmsg(&logmsg0, commit);
2469 if (err)
2470 return err;
2472 logmsg = logmsg0;
2473 do {
2474 line = strsep(&logmsg, "\n");
2475 if (line)
2476 printf(" %s\n", line);
2477 } while (line);
2478 free(logmsg0);
2480 if (show_patch) {
2481 err = print_patch(commit, id, path, diff_context, repo);
2482 if (err == 0)
2483 printf("\n");
2486 if (fflush(stdout) != 0 && err == NULL)
2487 err = got_error_from_errno("fflush");
2488 return err;
2491 static const struct got_error *
2492 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2493 const char *path, int show_patch, const char *search_pattern,
2494 int diff_context, int limit, int log_branches,
2495 struct got_reflist_head *refs)
2497 const struct got_error *err;
2498 struct got_commit_graph *graph;
2499 regex_t regex;
2500 int have_match;
2502 if (search_pattern &&
2503 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2504 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2506 err = got_commit_graph_open(&graph, path, !log_branches);
2507 if (err)
2508 return err;
2509 err = got_commit_graph_iter_start(graph, root_id, repo,
2510 check_cancelled, NULL);
2511 if (err)
2512 goto done;
2513 for (;;) {
2514 struct got_commit_object *commit;
2515 struct got_object_id *id;
2517 if (sigint_received || sigpipe_received)
2518 break;
2520 err = got_commit_graph_iter_next(&id, graph, repo,
2521 check_cancelled, NULL);
2522 if (err) {
2523 if (err->code == GOT_ERR_ITER_COMPLETED)
2524 err = NULL;
2525 break;
2527 if (id == NULL)
2528 break;
2530 err = got_object_open_as_commit(&commit, repo, id);
2531 if (err)
2532 break;
2534 if (search_pattern) {
2535 err = match_logmsg(&have_match, id, commit, &regex);
2536 if (err) {
2537 got_object_commit_close(commit);
2538 break;
2540 if (have_match == 0) {
2541 got_object_commit_close(commit);
2542 continue;
2546 err = print_commit(commit, id, repo, path, show_patch,
2547 diff_context, refs);
2548 got_object_commit_close(commit);
2549 if (err || (limit && --limit == 0))
2550 break;
2552 done:
2553 if (search_pattern)
2554 regfree(&regex);
2555 got_commit_graph_close(graph);
2556 return err;
2559 __dead static void
2560 usage_log(void)
2562 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2563 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2564 exit(1);
2567 static int
2568 get_default_log_limit(void)
2570 const char *got_default_log_limit;
2571 long long n;
2572 const char *errstr;
2574 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2575 if (got_default_log_limit == NULL)
2576 return 0;
2577 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2578 if (errstr != NULL)
2579 return 0;
2580 return n;
2583 static const struct got_error *
2584 cmd_log(int argc, char *argv[])
2586 const struct got_error *error;
2587 struct got_repository *repo = NULL;
2588 struct got_worktree *worktree = NULL;
2589 struct got_commit_object *commit = NULL;
2590 struct got_object_id *id = NULL;
2591 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2592 const char *start_commit = NULL, *search_pattern = NULL;
2593 int diff_context = -1, ch;
2594 int show_patch = 0, limit = 0, log_branches = 0;
2595 const char *errstr;
2596 struct got_reflist_head refs;
2598 SIMPLEQ_INIT(&refs);
2600 #ifndef PROFILE
2601 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2602 NULL)
2603 == -1)
2604 err(1, "pledge");
2605 #endif
2607 limit = get_default_log_limit();
2609 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2610 switch (ch) {
2611 case 'p':
2612 show_patch = 1;
2613 break;
2614 case 'c':
2615 start_commit = optarg;
2616 break;
2617 case 'C':
2618 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2619 &errstr);
2620 if (errstr != NULL)
2621 err(1, "-C option %s", errstr);
2622 break;
2623 case 'l':
2624 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2625 if (errstr != NULL)
2626 err(1, "-l option %s", errstr);
2627 break;
2628 case 'b':
2629 log_branches = 1;
2630 break;
2631 case 'r':
2632 repo_path = realpath(optarg, NULL);
2633 if (repo_path == NULL)
2634 return got_error_from_errno2("realpath",
2635 optarg);
2636 got_path_strip_trailing_slashes(repo_path);
2637 break;
2638 case 's':
2639 search_pattern = optarg;
2640 break;
2641 default:
2642 usage_log();
2643 /* NOTREACHED */
2647 argc -= optind;
2648 argv += optind;
2650 if (diff_context == -1)
2651 diff_context = 3;
2652 else if (!show_patch)
2653 errx(1, "-C reguires -p");
2655 cwd = getcwd(NULL, 0);
2656 if (cwd == NULL) {
2657 error = got_error_from_errno("getcwd");
2658 goto done;
2661 error = got_worktree_open(&worktree, cwd);
2662 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2663 goto done;
2664 error = NULL;
2666 if (argc == 0) {
2667 path = strdup("");
2668 if (path == NULL) {
2669 error = got_error_from_errno("strdup");
2670 goto done;
2672 } else if (argc == 1) {
2673 if (worktree) {
2674 error = got_worktree_resolve_path(&path, worktree,
2675 argv[0]);
2676 if (error)
2677 goto done;
2678 } else {
2679 path = strdup(argv[0]);
2680 if (path == NULL) {
2681 error = got_error_from_errno("strdup");
2682 goto done;
2685 } else
2686 usage_log();
2688 if (repo_path == NULL) {
2689 repo_path = worktree ?
2690 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2692 if (repo_path == NULL) {
2693 error = got_error_from_errno("strdup");
2694 goto done;
2697 error = got_repo_open(&repo, repo_path, NULL);
2698 if (error != NULL)
2699 goto done;
2701 error = apply_unveil(got_repo_get_path(repo), 1,
2702 worktree ? got_worktree_get_root_path(worktree) : NULL);
2703 if (error)
2704 goto done;
2706 if (start_commit == NULL) {
2707 struct got_reference *head_ref;
2708 error = got_ref_open(&head_ref, repo,
2709 worktree ? got_worktree_get_head_ref_name(worktree)
2710 : GOT_REF_HEAD, 0);
2711 if (error != NULL)
2712 return error;
2713 error = got_ref_resolve(&id, repo, head_ref);
2714 got_ref_close(head_ref);
2715 if (error != NULL)
2716 return error;
2717 error = got_object_open_as_commit(&commit, repo, id);
2718 } else {
2719 struct got_reference *ref;
2720 error = got_ref_open(&ref, repo, start_commit, 0);
2721 if (error == NULL) {
2722 int obj_type;
2723 error = got_ref_resolve(&id, repo, ref);
2724 got_ref_close(ref);
2725 if (error != NULL)
2726 goto done;
2727 error = got_object_get_type(&obj_type, repo, id);
2728 if (error != NULL)
2729 goto done;
2730 if (obj_type == GOT_OBJ_TYPE_TAG) {
2731 struct got_tag_object *tag;
2732 error = got_object_open_as_tag(&tag, repo, id);
2733 if (error != NULL)
2734 goto done;
2735 if (got_object_tag_get_object_type(tag) !=
2736 GOT_OBJ_TYPE_COMMIT) {
2737 got_object_tag_close(tag);
2738 error = got_error(GOT_ERR_OBJ_TYPE);
2739 goto done;
2741 free(id);
2742 id = got_object_id_dup(
2743 got_object_tag_get_object_id(tag));
2744 if (id == NULL)
2745 error = got_error_from_errno(
2746 "got_object_id_dup");
2747 got_object_tag_close(tag);
2748 if (error)
2749 goto done;
2750 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2751 error = got_error(GOT_ERR_OBJ_TYPE);
2752 goto done;
2754 error = got_object_open_as_commit(&commit, repo, id);
2755 if (error != NULL)
2756 goto done;
2758 if (commit == NULL) {
2759 error = got_repo_match_object_id_prefix(&id,
2760 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2761 if (error != NULL)
2762 return error;
2765 if (error != NULL)
2766 goto done;
2768 if (worktree) {
2769 const char *prefix = got_worktree_get_path_prefix(worktree);
2770 char *p;
2771 if (asprintf(&p, "%s%s%s", prefix,
2772 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2773 error = got_error_from_errno("asprintf");
2774 goto done;
2776 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2777 free(p);
2778 } else
2779 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2780 if (error != NULL)
2781 goto done;
2782 if (in_repo_path) {
2783 free(path);
2784 path = in_repo_path;
2787 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2788 if (error)
2789 goto done;
2791 error = print_commits(id, repo, path, show_patch, search_pattern,
2792 diff_context, limit, log_branches, &refs);
2793 done:
2794 free(path);
2795 free(repo_path);
2796 free(cwd);
2797 free(id);
2798 if (worktree)
2799 got_worktree_close(worktree);
2800 if (repo) {
2801 const struct got_error *repo_error;
2802 repo_error = got_repo_close(repo);
2803 if (error == NULL)
2804 error = repo_error;
2806 got_ref_list_free(&refs);
2807 return error;
2810 __dead static void
2811 usage_diff(void)
2813 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2814 "[-w] [object1 object2 | path]\n", getprogname());
2815 exit(1);
2818 struct print_diff_arg {
2819 struct got_repository *repo;
2820 struct got_worktree *worktree;
2821 int diff_context;
2822 const char *id_str;
2823 int header_shown;
2824 int diff_staged;
2825 int ignore_whitespace;
2828 static const struct got_error *
2829 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2830 const char *path, struct got_object_id *blob_id,
2831 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2832 int dirfd, const char *de_name)
2834 struct print_diff_arg *a = arg;
2835 const struct got_error *err = NULL;
2836 struct got_blob_object *blob1 = NULL;
2837 int fd = -1;
2838 FILE *f2 = NULL;
2839 char *abspath = NULL, *label1 = NULL;
2840 struct stat sb;
2842 if (a->diff_staged) {
2843 if (staged_status != GOT_STATUS_MODIFY &&
2844 staged_status != GOT_STATUS_ADD &&
2845 staged_status != GOT_STATUS_DELETE)
2846 return NULL;
2847 } else {
2848 if (staged_status == GOT_STATUS_DELETE)
2849 return NULL;
2850 if (status == GOT_STATUS_NONEXISTENT)
2851 return got_error_set_errno(ENOENT, path);
2852 if (status != GOT_STATUS_MODIFY &&
2853 status != GOT_STATUS_ADD &&
2854 status != GOT_STATUS_DELETE &&
2855 status != GOT_STATUS_CONFLICT)
2856 return NULL;
2859 if (!a->header_shown) {
2860 printf("diff %s %s%s\n", a->id_str,
2861 got_worktree_get_root_path(a->worktree),
2862 a->diff_staged ? " (staged changes)" : "");
2863 a->header_shown = 1;
2866 if (a->diff_staged) {
2867 const char *label1 = NULL, *label2 = NULL;
2868 switch (staged_status) {
2869 case GOT_STATUS_MODIFY:
2870 label1 = path;
2871 label2 = path;
2872 break;
2873 case GOT_STATUS_ADD:
2874 label2 = path;
2875 break;
2876 case GOT_STATUS_DELETE:
2877 label1 = path;
2878 break;
2879 default:
2880 return got_error(GOT_ERR_FILE_STATUS);
2882 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2883 label1, label2, a->diff_context, a->ignore_whitespace,
2884 a->repo, stdout);
2887 if (staged_status == GOT_STATUS_ADD ||
2888 staged_status == GOT_STATUS_MODIFY) {
2889 char *id_str;
2890 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2891 8192);
2892 if (err)
2893 goto done;
2894 err = got_object_id_str(&id_str, staged_blob_id);
2895 if (err)
2896 goto done;
2897 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2898 err = got_error_from_errno("asprintf");
2899 free(id_str);
2900 goto done;
2902 free(id_str);
2903 } else if (status != GOT_STATUS_ADD) {
2904 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2905 if (err)
2906 goto done;
2909 if (status != GOT_STATUS_DELETE) {
2910 if (asprintf(&abspath, "%s/%s",
2911 got_worktree_get_root_path(a->worktree), path) == -1) {
2912 err = got_error_from_errno("asprintf");
2913 goto done;
2916 if (dirfd != -1) {
2917 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2918 if (fd == -1) {
2919 err = got_error_from_errno2("openat", abspath);
2920 goto done;
2922 } else {
2923 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2924 if (fd == -1) {
2925 err = got_error_from_errno2("open", abspath);
2926 goto done;
2929 if (fstat(fd, &sb) == -1) {
2930 err = got_error_from_errno2("fstat", abspath);
2931 goto done;
2933 f2 = fdopen(fd, "r");
2934 if (f2 == NULL) {
2935 err = got_error_from_errno2("fdopen", abspath);
2936 goto done;
2938 fd = -1;
2939 } else
2940 sb.st_size = 0;
2942 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2943 a->diff_context, a->ignore_whitespace, stdout);
2944 done:
2945 if (blob1)
2946 got_object_blob_close(blob1);
2947 if (f2 && fclose(f2) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2949 if (fd != -1 && close(fd) == -1 && err == NULL)
2950 err = got_error_from_errno("close");
2951 free(abspath);
2952 return err;
2955 static const struct got_error *
2956 cmd_diff(int argc, char *argv[])
2958 const struct got_error *error;
2959 struct got_repository *repo = NULL;
2960 struct got_worktree *worktree = NULL;
2961 char *cwd = NULL, *repo_path = NULL;
2962 struct got_object_id *id1 = NULL, *id2 = NULL;
2963 const char *id_str1 = NULL, *id_str2 = NULL;
2964 char *label1 = NULL, *label2 = NULL;
2965 int type1, type2;
2966 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2967 const char *errstr;
2968 char *path = NULL;
2970 #ifndef PROFILE
2971 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2972 NULL) == -1)
2973 err(1, "pledge");
2974 #endif
2976 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2977 switch (ch) {
2978 case 'C':
2979 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2980 &errstr);
2981 if (errstr != NULL)
2982 err(1, "-C option %s", errstr);
2983 break;
2984 case 'r':
2985 repo_path = realpath(optarg, NULL);
2986 if (repo_path == NULL)
2987 return got_error_from_errno2("realpath",
2988 optarg);
2989 got_path_strip_trailing_slashes(repo_path);
2990 break;
2991 case 's':
2992 diff_staged = 1;
2993 break;
2994 case 'w':
2995 ignore_whitespace = 1;
2996 break;
2997 default:
2998 usage_diff();
2999 /* NOTREACHED */
3003 argc -= optind;
3004 argv += optind;
3006 cwd = getcwd(NULL, 0);
3007 if (cwd == NULL) {
3008 error = got_error_from_errno("getcwd");
3009 goto done;
3011 error = got_worktree_open(&worktree, cwd);
3012 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3013 goto done;
3014 if (argc <= 1) {
3015 if (worktree == NULL) {
3016 error = got_error(GOT_ERR_NOT_WORKTREE);
3017 goto done;
3019 if (repo_path)
3020 errx(1,
3021 "-r option can't be used when diffing a work tree");
3022 repo_path = strdup(got_worktree_get_repo_path(worktree));
3023 if (repo_path == NULL) {
3024 error = got_error_from_errno("strdup");
3025 goto done;
3027 if (argc == 1) {
3028 error = got_worktree_resolve_path(&path, worktree,
3029 argv[0]);
3030 if (error)
3031 goto done;
3032 } else {
3033 path = strdup("");
3034 if (path == NULL) {
3035 error = got_error_from_errno("strdup");
3036 goto done;
3039 } else if (argc == 2) {
3040 if (diff_staged)
3041 errx(1, "-s option can't be used when diffing "
3042 "objects in repository");
3043 id_str1 = argv[0];
3044 id_str2 = argv[1];
3045 if (worktree && repo_path == NULL) {
3046 repo_path =
3047 strdup(got_worktree_get_repo_path(worktree));
3048 if (repo_path == NULL) {
3049 error = got_error_from_errno("strdup");
3050 goto done;
3053 } else
3054 usage_diff();
3056 if (repo_path == NULL) {
3057 repo_path = getcwd(NULL, 0);
3058 if (repo_path == NULL)
3059 return got_error_from_errno("getcwd");
3062 error = got_repo_open(&repo, repo_path, NULL);
3063 free(repo_path);
3064 if (error != NULL)
3065 goto done;
3067 error = apply_unveil(got_repo_get_path(repo), 1,
3068 worktree ? got_worktree_get_root_path(worktree) : NULL);
3069 if (error)
3070 goto done;
3072 if (argc <= 1) {
3073 struct print_diff_arg arg;
3074 struct got_pathlist_head paths;
3075 char *id_str;
3077 TAILQ_INIT(&paths);
3079 error = got_object_id_str(&id_str,
3080 got_worktree_get_base_commit_id(worktree));
3081 if (error)
3082 goto done;
3083 arg.repo = repo;
3084 arg.worktree = worktree;
3085 arg.diff_context = diff_context;
3086 arg.id_str = id_str;
3087 arg.header_shown = 0;
3088 arg.diff_staged = diff_staged;
3089 arg.ignore_whitespace = ignore_whitespace;
3091 error = got_pathlist_append(&paths, path, NULL);
3092 if (error)
3093 goto done;
3095 error = got_worktree_status(worktree, &paths, repo, print_diff,
3096 &arg, check_cancelled, NULL);
3097 free(id_str);
3098 got_pathlist_free(&paths);
3099 goto done;
3102 error = got_repo_match_object_id(&id1, &label1, id_str1,
3103 GOT_OBJ_TYPE_ANY, 1, repo);
3104 if (error)
3105 goto done;
3107 error = got_repo_match_object_id(&id2, &label2, id_str2,
3108 GOT_OBJ_TYPE_ANY, 1, repo);
3109 if (error)
3110 goto done;
3112 error = got_object_get_type(&type1, repo, id1);
3113 if (error)
3114 goto done;
3116 error = got_object_get_type(&type2, repo, id2);
3117 if (error)
3118 goto done;
3120 if (type1 != type2) {
3121 error = got_error(GOT_ERR_OBJ_TYPE);
3122 goto done;
3125 switch (type1) {
3126 case GOT_OBJ_TYPE_BLOB:
3127 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3128 diff_context, ignore_whitespace, repo, stdout);
3129 break;
3130 case GOT_OBJ_TYPE_TREE:
3131 error = got_diff_objects_as_trees(id1, id2, "", "",
3132 diff_context, ignore_whitespace, repo, stdout);
3133 break;
3134 case GOT_OBJ_TYPE_COMMIT:
3135 printf("diff %s %s\n", label1, label2);
3136 error = got_diff_objects_as_commits(id1, id2, diff_context,
3137 ignore_whitespace, repo, stdout);
3138 break;
3139 default:
3140 error = got_error(GOT_ERR_OBJ_TYPE);
3142 done:
3143 free(label1);
3144 free(label2);
3145 free(id1);
3146 free(id2);
3147 free(path);
3148 if (worktree)
3149 got_worktree_close(worktree);
3150 if (repo) {
3151 const struct got_error *repo_error;
3152 repo_error = got_repo_close(repo);
3153 if (error == NULL)
3154 error = repo_error;
3156 return error;
3159 __dead static void
3160 usage_blame(void)
3162 fprintf(stderr,
3163 "usage: %s blame [-c commit] [-r repository-path] path\n",
3164 getprogname());
3165 exit(1);
3168 struct blame_line {
3169 int annotated;
3170 char *id_str;
3171 char *committer;
3172 char datebuf[11]; /* YYYY-MM-DD + NUL */
3175 struct blame_cb_args {
3176 struct blame_line *lines;
3177 int nlines;
3178 int nlines_prec;
3179 int lineno_cur;
3180 off_t *line_offsets;
3181 FILE *f;
3182 struct got_repository *repo;
3185 static const struct got_error *
3186 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3188 const struct got_error *err = NULL;
3189 struct blame_cb_args *a = arg;
3190 struct blame_line *bline;
3191 char *line = NULL;
3192 size_t linesize = 0;
3193 struct got_commit_object *commit = NULL;
3194 off_t offset;
3195 struct tm tm;
3196 time_t committer_time;
3198 if (nlines != a->nlines ||
3199 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3200 return got_error(GOT_ERR_RANGE);
3202 if (sigint_received)
3203 return got_error(GOT_ERR_ITER_COMPLETED);
3205 if (lineno == -1)
3206 return NULL; /* no change in this commit */
3208 /* Annotate this line. */
3209 bline = &a->lines[lineno - 1];
3210 if (bline->annotated)
3211 return NULL;
3212 err = got_object_id_str(&bline->id_str, id);
3213 if (err)
3214 return err;
3216 err = got_object_open_as_commit(&commit, a->repo, id);
3217 if (err)
3218 goto done;
3220 bline->committer = strdup(got_object_commit_get_committer(commit));
3221 if (bline->committer == NULL) {
3222 err = got_error_from_errno("strdup");
3223 goto done;
3226 committer_time = got_object_commit_get_committer_time(commit);
3227 if (localtime_r(&committer_time, &tm) == NULL)
3228 return got_error_from_errno("localtime_r");
3229 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3230 &tm) >= sizeof(bline->datebuf)) {
3231 err = got_error(GOT_ERR_NO_SPACE);
3232 goto done;
3234 bline->annotated = 1;
3236 /* Print lines annotated so far. */
3237 bline = &a->lines[a->lineno_cur - 1];
3238 if (!bline->annotated)
3239 goto done;
3241 offset = a->line_offsets[a->lineno_cur - 1];
3242 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3243 err = got_error_from_errno("fseeko");
3244 goto done;
3247 while (bline->annotated) {
3248 char *smallerthan, *at, *nl, *committer;
3249 size_t len;
3251 if (getline(&line, &linesize, a->f) == -1) {
3252 if (ferror(a->f))
3253 err = got_error_from_errno("getline");
3254 break;
3257 committer = bline->committer;
3258 smallerthan = strchr(committer, '<');
3259 if (smallerthan && smallerthan[1] != '\0')
3260 committer = smallerthan + 1;
3261 at = strchr(committer, '@');
3262 if (at)
3263 *at = '\0';
3264 len = strlen(committer);
3265 if (len >= 9)
3266 committer[8] = '\0';
3268 nl = strchr(line, '\n');
3269 if (nl)
3270 *nl = '\0';
3271 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3272 bline->id_str, bline->datebuf, committer, line);
3274 a->lineno_cur++;
3275 bline = &a->lines[a->lineno_cur - 1];
3277 done:
3278 if (commit)
3279 got_object_commit_close(commit);
3280 free(line);
3281 return err;
3284 static const struct got_error *
3285 cmd_blame(int argc, char *argv[])
3287 const struct got_error *error;
3288 struct got_repository *repo = NULL;
3289 struct got_worktree *worktree = NULL;
3290 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3291 struct got_object_id *obj_id = NULL;
3292 struct got_object_id *commit_id = NULL;
3293 struct got_blob_object *blob = NULL;
3294 char *commit_id_str = NULL;
3295 struct blame_cb_args bca;
3296 int ch, obj_type, i;
3297 size_t filesize;
3299 memset(&bca, 0, sizeof(bca));
3301 #ifndef PROFILE
3302 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3303 NULL) == -1)
3304 err(1, "pledge");
3305 #endif
3307 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3308 switch (ch) {
3309 case 'c':
3310 commit_id_str = optarg;
3311 break;
3312 case 'r':
3313 repo_path = realpath(optarg, NULL);
3314 if (repo_path == NULL)
3315 return got_error_from_errno2("realpath",
3316 optarg);
3317 got_path_strip_trailing_slashes(repo_path);
3318 break;
3319 default:
3320 usage_blame();
3321 /* NOTREACHED */
3325 argc -= optind;
3326 argv += optind;
3328 if (argc == 1)
3329 path = argv[0];
3330 else
3331 usage_blame();
3333 cwd = getcwd(NULL, 0);
3334 if (cwd == NULL) {
3335 error = got_error_from_errno("getcwd");
3336 goto done;
3338 if (repo_path == NULL) {
3339 error = got_worktree_open(&worktree, cwd);
3340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3341 goto done;
3342 else
3343 error = NULL;
3344 if (worktree) {
3345 repo_path =
3346 strdup(got_worktree_get_repo_path(worktree));
3347 if (repo_path == NULL)
3348 error = got_error_from_errno("strdup");
3349 if (error)
3350 goto done;
3351 } else {
3352 repo_path = strdup(cwd);
3353 if (repo_path == NULL) {
3354 error = got_error_from_errno("strdup");
3355 goto done;
3360 error = got_repo_open(&repo, repo_path, NULL);
3361 if (error != NULL)
3362 goto done;
3364 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3365 if (error)
3366 goto done;
3368 if (worktree) {
3369 const char *prefix = got_worktree_get_path_prefix(worktree);
3370 char *p, *worktree_subdir = cwd +
3371 strlen(got_worktree_get_root_path(worktree));
3372 if (asprintf(&p, "%s%s%s%s%s",
3373 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3374 worktree_subdir, worktree_subdir[0] ? "/" : "",
3375 path) == -1) {
3376 error = got_error_from_errno("asprintf");
3377 goto done;
3379 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3380 free(p);
3381 } else {
3382 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3384 if (error)
3385 goto done;
3387 if (commit_id_str == NULL) {
3388 struct got_reference *head_ref;
3389 error = got_ref_open(&head_ref, repo, worktree ?
3390 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3391 if (error != NULL)
3392 goto done;
3393 error = got_ref_resolve(&commit_id, repo, head_ref);
3394 got_ref_close(head_ref);
3395 if (error != NULL)
3396 goto done;
3397 } else {
3398 error = got_repo_match_object_id(&commit_id, NULL,
3399 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3400 if (error)
3401 goto done;
3404 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3405 if (error)
3406 goto done;
3408 error = got_object_get_type(&obj_type, repo, obj_id);
3409 if (error)
3410 goto done;
3412 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3413 error = got_error(GOT_ERR_OBJ_TYPE);
3414 goto done;
3417 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3418 if (error)
3419 goto done;
3420 bca.f = got_opentemp();
3421 if (bca.f == NULL) {
3422 error = got_error_from_errno("got_opentemp");
3423 goto done;
3425 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3426 &bca.line_offsets, bca.f, blob);
3427 if (error || bca.nlines == 0)
3428 goto done;
3430 /* Don't include \n at EOF in the blame line count. */
3431 if (bca.line_offsets[bca.nlines - 1] == filesize)
3432 bca.nlines--;
3434 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3435 if (bca.lines == NULL) {
3436 error = got_error_from_errno("calloc");
3437 goto done;
3439 bca.lineno_cur = 1;
3440 bca.nlines_prec = 0;
3441 i = bca.nlines;
3442 while (i > 0) {
3443 i /= 10;
3444 bca.nlines_prec++;
3446 bca.repo = repo;
3448 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3449 check_cancelled, NULL);
3450 done:
3451 free(in_repo_path);
3452 free(repo_path);
3453 free(cwd);
3454 free(commit_id);
3455 free(obj_id);
3456 if (blob)
3457 got_object_blob_close(blob);
3458 if (worktree)
3459 got_worktree_close(worktree);
3460 if (repo) {
3461 const struct got_error *repo_error;
3462 repo_error = got_repo_close(repo);
3463 if (error == NULL)
3464 error = repo_error;
3466 if (bca.lines) {
3467 for (i = 0; i < bca.nlines; i++) {
3468 struct blame_line *bline = &bca.lines[i];
3469 free(bline->id_str);
3470 free(bline->committer);
3472 free(bca.lines);
3474 free(bca.line_offsets);
3475 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3476 error = got_error_from_errno("fclose");
3477 return error;
3480 __dead static void
3481 usage_tree(void)
3483 fprintf(stderr,
3484 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3485 getprogname());
3486 exit(1);
3489 static void
3490 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3491 const char *root_path)
3493 int is_root_path = (strcmp(path, root_path) == 0);
3494 const char *modestr = "";
3495 mode_t mode = got_tree_entry_get_mode(te);
3497 path += strlen(root_path);
3498 while (path[0] == '/')
3499 path++;
3501 if (got_object_tree_entry_is_submodule(te))
3502 modestr = "$";
3503 else if (S_ISLNK(mode))
3504 modestr = "@";
3505 else if (S_ISDIR(mode))
3506 modestr = "/";
3507 else if (mode & S_IXUSR)
3508 modestr = "*";
3510 printf("%s%s%s%s%s\n", id ? id : "", path,
3511 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3514 static const struct got_error *
3515 print_tree(const char *path, struct got_object_id *commit_id,
3516 int show_ids, int recurse, const char *root_path,
3517 struct got_repository *repo)
3519 const struct got_error *err = NULL;
3520 struct got_object_id *tree_id = NULL;
3521 struct got_tree_object *tree = NULL;
3522 int nentries, i;
3524 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3525 if (err)
3526 goto done;
3528 err = got_object_open_as_tree(&tree, repo, tree_id);
3529 if (err)
3530 goto done;
3531 nentries = got_object_tree_get_nentries(tree);
3532 for (i = 0; i < nentries; i++) {
3533 struct got_tree_entry *te;
3534 char *id = NULL;
3536 if (sigint_received || sigpipe_received)
3537 break;
3539 te = got_object_tree_get_entry(tree, i);
3540 if (show_ids) {
3541 char *id_str;
3542 err = got_object_id_str(&id_str,
3543 got_tree_entry_get_id(te));
3544 if (err)
3545 goto done;
3546 if (asprintf(&id, "%s ", id_str) == -1) {
3547 err = got_error_from_errno("asprintf");
3548 free(id_str);
3549 goto done;
3551 free(id_str);
3553 print_entry(te, id, path, root_path);
3554 free(id);
3556 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3557 char *child_path;
3558 if (asprintf(&child_path, "%s%s%s", path,
3559 path[0] == '/' && path[1] == '\0' ? "" : "/",
3560 got_tree_entry_get_name(te)) == -1) {
3561 err = got_error_from_errno("asprintf");
3562 goto done;
3564 err = print_tree(child_path, commit_id, show_ids, 1,
3565 root_path, repo);
3566 free(child_path);
3567 if (err)
3568 goto done;
3571 done:
3572 if (tree)
3573 got_object_tree_close(tree);
3574 free(tree_id);
3575 return err;
3578 static const struct got_error *
3579 cmd_tree(int argc, char *argv[])
3581 const struct got_error *error;
3582 struct got_repository *repo = NULL;
3583 struct got_worktree *worktree = NULL;
3584 const char *path;
3585 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3586 struct got_object_id *commit_id = NULL;
3587 char *commit_id_str = NULL;
3588 int show_ids = 0, recurse = 0;
3589 int ch;
3591 #ifndef PROFILE
3592 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3593 NULL) == -1)
3594 err(1, "pledge");
3595 #endif
3597 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3598 switch (ch) {
3599 case 'c':
3600 commit_id_str = optarg;
3601 break;
3602 case 'r':
3603 repo_path = realpath(optarg, NULL);
3604 if (repo_path == NULL)
3605 return got_error_from_errno2("realpath",
3606 optarg);
3607 got_path_strip_trailing_slashes(repo_path);
3608 break;
3609 case 'i':
3610 show_ids = 1;
3611 break;
3612 case 'R':
3613 recurse = 1;
3614 break;
3615 default:
3616 usage_tree();
3617 /* NOTREACHED */
3621 argc -= optind;
3622 argv += optind;
3624 if (argc == 1)
3625 path = argv[0];
3626 else if (argc > 1)
3627 usage_tree();
3628 else
3629 path = NULL;
3631 cwd = getcwd(NULL, 0);
3632 if (cwd == NULL) {
3633 error = got_error_from_errno("getcwd");
3634 goto done;
3636 if (repo_path == NULL) {
3637 error = got_worktree_open(&worktree, cwd);
3638 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3639 goto done;
3640 else
3641 error = NULL;
3642 if (worktree) {
3643 repo_path =
3644 strdup(got_worktree_get_repo_path(worktree));
3645 if (repo_path == NULL)
3646 error = got_error_from_errno("strdup");
3647 if (error)
3648 goto done;
3649 } else {
3650 repo_path = strdup(cwd);
3651 if (repo_path == NULL) {
3652 error = got_error_from_errno("strdup");
3653 goto done;
3658 error = got_repo_open(&repo, repo_path, NULL);
3659 if (error != NULL)
3660 goto done;
3662 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3663 if (error)
3664 goto done;
3666 if (path == NULL) {
3667 if (worktree) {
3668 char *p, *worktree_subdir = cwd +
3669 strlen(got_worktree_get_root_path(worktree));
3670 if (asprintf(&p, "%s/%s",
3671 got_worktree_get_path_prefix(worktree),
3672 worktree_subdir) == -1) {
3673 error = got_error_from_errno("asprintf");
3674 goto done;
3676 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3677 free(p);
3678 if (error)
3679 goto done;
3680 } else
3681 path = "/";
3683 if (in_repo_path == NULL) {
3684 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3685 if (error != NULL)
3686 goto done;
3689 if (commit_id_str == NULL) {
3690 struct got_reference *head_ref;
3691 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3692 if (error != NULL)
3693 goto done;
3694 error = got_ref_resolve(&commit_id, repo, head_ref);
3695 got_ref_close(head_ref);
3696 if (error != NULL)
3697 goto done;
3698 } else {
3699 error = got_repo_match_object_id(&commit_id, NULL,
3700 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3701 if (error)
3702 goto done;
3705 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3706 in_repo_path, repo);
3707 done:
3708 free(in_repo_path);
3709 free(repo_path);
3710 free(cwd);
3711 free(commit_id);
3712 if (worktree)
3713 got_worktree_close(worktree);
3714 if (repo) {
3715 const struct got_error *repo_error;
3716 repo_error = got_repo_close(repo);
3717 if (error == NULL)
3718 error = repo_error;
3720 return error;
3723 __dead static void
3724 usage_status(void)
3726 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3727 exit(1);
3730 static const struct got_error *
3731 print_status(void *arg, unsigned char status, unsigned char staged_status,
3732 const char *path, struct got_object_id *blob_id,
3733 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3734 int dirfd, const char *de_name)
3736 if (status == staged_status && (status == GOT_STATUS_DELETE))
3737 status = GOT_STATUS_NO_CHANGE;
3738 printf("%c%c %s\n", status, staged_status, path);
3739 return NULL;
3742 static const struct got_error *
3743 cmd_status(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3748 char *cwd = NULL;
3749 struct got_pathlist_head paths;
3750 struct got_pathlist_entry *pe;
3751 int ch;
3753 TAILQ_INIT(&paths);
3755 while ((ch = getopt(argc, argv, "")) != -1) {
3756 switch (ch) {
3757 default:
3758 usage_status();
3759 /* NOTREACHED */
3763 argc -= optind;
3764 argv += optind;
3766 #ifndef PROFILE
3767 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3768 NULL) == -1)
3769 err(1, "pledge");
3770 #endif
3771 cwd = getcwd(NULL, 0);
3772 if (cwd == NULL) {
3773 error = got_error_from_errno("getcwd");
3774 goto done;
3777 error = got_worktree_open(&worktree, cwd);
3778 if (error != NULL)
3779 goto done;
3781 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3782 NULL);
3783 if (error != NULL)
3784 goto done;
3786 error = apply_unveil(got_repo_get_path(repo), 1,
3787 got_worktree_get_root_path(worktree));
3788 if (error)
3789 goto done;
3791 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3792 if (error)
3793 goto done;
3795 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3796 check_cancelled, NULL);
3797 done:
3798 TAILQ_FOREACH(pe, &paths, entry)
3799 free((char *)pe->path);
3800 got_pathlist_free(&paths);
3801 free(cwd);
3802 return error;
3805 __dead static void
3806 usage_ref(void)
3808 fprintf(stderr,
3809 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3810 getprogname());
3811 exit(1);
3814 static const struct got_error *
3815 list_refs(struct got_repository *repo)
3817 static const struct got_error *err = NULL;
3818 struct got_reflist_head refs;
3819 struct got_reflist_entry *re;
3821 SIMPLEQ_INIT(&refs);
3822 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3823 if (err)
3824 return err;
3826 SIMPLEQ_FOREACH(re, &refs, entry) {
3827 char *refstr;
3828 refstr = got_ref_to_str(re->ref);
3829 if (refstr == NULL)
3830 return got_error_from_errno("got_ref_to_str");
3831 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3832 free(refstr);
3835 got_ref_list_free(&refs);
3836 return NULL;
3839 static const struct got_error *
3840 delete_ref(struct got_repository *repo, const char *refname)
3842 const struct got_error *err = NULL;
3843 struct got_reference *ref;
3845 err = got_ref_open(&ref, repo, refname, 0);
3846 if (err)
3847 return err;
3849 err = got_ref_delete(ref, repo);
3850 got_ref_close(ref);
3851 return err;
3854 static const struct got_error *
3855 add_ref(struct got_repository *repo, const char *refname, const char *target)
3857 const struct got_error *err = NULL;
3858 struct got_object_id *id;
3859 struct got_reference *ref = NULL;
3862 * Don't let the user create a reference name with a leading '-'.
3863 * While technically a valid reference name, this case is usually
3864 * an unintended typo.
3866 if (refname[0] == '-')
3867 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3869 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3870 repo);
3871 if (err) {
3872 struct got_reference *target_ref;
3874 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3875 return err;
3876 err = got_ref_open(&target_ref, repo, target, 0);
3877 if (err)
3878 return err;
3879 err = got_ref_resolve(&id, repo, target_ref);
3880 got_ref_close(target_ref);
3881 if (err)
3882 return err;
3885 err = got_ref_alloc(&ref, refname, id);
3886 if (err)
3887 goto done;
3889 err = got_ref_write(ref, repo);
3890 done:
3891 if (ref)
3892 got_ref_close(ref);
3893 free(id);
3894 return err;
3897 static const struct got_error *
3898 add_symref(struct got_repository *repo, const char *refname, const char *target)
3900 const struct got_error *err = NULL;
3901 struct got_reference *ref = NULL;
3902 struct got_reference *target_ref = NULL;
3905 * Don't let the user create a reference name with a leading '-'.
3906 * While technically a valid reference name, this case is usually
3907 * an unintended typo.
3909 if (refname[0] == '-')
3910 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3912 err = got_ref_open(&target_ref, repo, target, 0);
3913 if (err)
3914 return err;
3916 err = got_ref_alloc_symref(&ref, refname, target_ref);
3917 if (err)
3918 goto done;
3920 err = got_ref_write(ref, repo);
3921 done:
3922 if (target_ref)
3923 got_ref_close(target_ref);
3924 if (ref)
3925 got_ref_close(ref);
3926 return err;
3929 static const struct got_error *
3930 cmd_ref(int argc, char *argv[])
3932 const struct got_error *error = NULL;
3933 struct got_repository *repo = NULL;
3934 struct got_worktree *worktree = NULL;
3935 char *cwd = NULL, *repo_path = NULL;
3936 int ch, do_list = 0, create_symref = 0;
3937 const char *delref = NULL;
3939 /* TODO: Add -s option for adding symbolic references. */
3940 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3941 switch (ch) {
3942 case 'd':
3943 delref = optarg;
3944 break;
3945 case 'r':
3946 repo_path = realpath(optarg, NULL);
3947 if (repo_path == NULL)
3948 return got_error_from_errno2("realpath",
3949 optarg);
3950 got_path_strip_trailing_slashes(repo_path);
3951 break;
3952 case 'l':
3953 do_list = 1;
3954 break;
3955 case 's':
3956 create_symref = 1;
3957 break;
3958 default:
3959 usage_ref();
3960 /* NOTREACHED */
3964 if (do_list && delref)
3965 errx(1, "-l and -d options are mutually exclusive\n");
3967 argc -= optind;
3968 argv += optind;
3970 if (do_list || delref) {
3971 if (create_symref)
3972 errx(1, "-s option cannot be used together with the "
3973 "-l or -d options");
3974 if (argc > 0)
3975 usage_ref();
3976 } else if (argc != 2)
3977 usage_ref();
3979 #ifndef PROFILE
3980 if (do_list) {
3981 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3982 NULL) == -1)
3983 err(1, "pledge");
3984 } else {
3985 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3986 "sendfd unveil", NULL) == -1)
3987 err(1, "pledge");
3989 #endif
3990 cwd = getcwd(NULL, 0);
3991 if (cwd == NULL) {
3992 error = got_error_from_errno("getcwd");
3993 goto done;
3996 if (repo_path == NULL) {
3997 error = got_worktree_open(&worktree, cwd);
3998 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3999 goto done;
4000 else
4001 error = NULL;
4002 if (worktree) {
4003 repo_path =
4004 strdup(got_worktree_get_repo_path(worktree));
4005 if (repo_path == NULL)
4006 error = got_error_from_errno("strdup");
4007 if (error)
4008 goto done;
4009 } else {
4010 repo_path = strdup(cwd);
4011 if (repo_path == NULL) {
4012 error = got_error_from_errno("strdup");
4013 goto done;
4018 error = got_repo_open(&repo, repo_path, NULL);
4019 if (error != NULL)
4020 goto done;
4022 error = apply_unveil(got_repo_get_path(repo), do_list,
4023 worktree ? got_worktree_get_root_path(worktree) : NULL);
4024 if (error)
4025 goto done;
4027 if (do_list)
4028 error = list_refs(repo);
4029 else if (delref)
4030 error = delete_ref(repo, delref);
4031 else if (create_symref)
4032 error = add_symref(repo, argv[0], argv[1]);
4033 else
4034 error = add_ref(repo, argv[0], argv[1]);
4035 done:
4036 if (repo)
4037 got_repo_close(repo);
4038 if (worktree)
4039 got_worktree_close(worktree);
4040 free(cwd);
4041 free(repo_path);
4042 return error;
4045 __dead static void
4046 usage_branch(void)
4048 fprintf(stderr,
4049 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4050 "[name]\n", getprogname());
4051 exit(1);
4054 static const struct got_error *
4055 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4056 struct got_reference *ref)
4058 const struct got_error *err = NULL;
4059 const char *refname, *marker = " ";
4060 char *refstr;
4062 refname = got_ref_get_name(ref);
4063 if (worktree && strcmp(refname,
4064 got_worktree_get_head_ref_name(worktree)) == 0) {
4065 struct got_object_id *id = NULL;
4067 err = got_ref_resolve(&id, repo, ref);
4068 if (err)
4069 return err;
4070 if (got_object_id_cmp(id,
4071 got_worktree_get_base_commit_id(worktree)) == 0)
4072 marker = "* ";
4073 else
4074 marker = "~ ";
4075 free(id);
4078 if (strncmp(refname, "refs/heads/", 11) == 0)
4079 refname += 11;
4080 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4081 refname += 18;
4083 refstr = got_ref_to_str(ref);
4084 if (refstr == NULL)
4085 return got_error_from_errno("got_ref_to_str");
4087 printf("%s%s: %s\n", marker, refname, refstr);
4088 free(refstr);
4089 return NULL;
4092 static const struct got_error *
4093 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4095 const char *refname;
4097 if (worktree == NULL)
4098 return got_error(GOT_ERR_NOT_WORKTREE);
4100 refname = got_worktree_get_head_ref_name(worktree);
4102 if (strncmp(refname, "refs/heads/", 11) == 0)
4103 refname += 11;
4104 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4105 refname += 18;
4107 printf("%s\n", refname);
4109 return NULL;
4112 static const struct got_error *
4113 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4115 static const struct got_error *err = NULL;
4116 struct got_reflist_head refs;
4117 struct got_reflist_entry *re;
4118 struct got_reference *temp_ref = NULL;
4119 int rebase_in_progress, histedit_in_progress;
4121 SIMPLEQ_INIT(&refs);
4123 if (worktree) {
4124 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4125 worktree);
4126 if (err)
4127 return err;
4129 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4130 worktree);
4131 if (err)
4132 return err;
4134 if (rebase_in_progress || histedit_in_progress) {
4135 err = got_ref_open(&temp_ref, repo,
4136 got_worktree_get_head_ref_name(worktree), 0);
4137 if (err)
4138 return err;
4139 list_branch(repo, worktree, temp_ref);
4140 got_ref_close(temp_ref);
4144 err = got_ref_list(&refs, repo, "refs/heads",
4145 got_ref_cmp_by_name, NULL);
4146 if (err)
4147 return err;
4149 SIMPLEQ_FOREACH(re, &refs, entry)
4150 list_branch(repo, worktree, re->ref);
4152 got_ref_list_free(&refs);
4153 return NULL;
4156 static const struct got_error *
4157 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4158 const char *branch_name)
4160 const struct got_error *err = NULL;
4161 struct got_reference *ref = NULL;
4162 char *refname;
4164 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4165 return got_error_from_errno("asprintf");
4167 err = got_ref_open(&ref, repo, refname, 0);
4168 if (err)
4169 goto done;
4171 if (worktree &&
4172 strcmp(got_worktree_get_head_ref_name(worktree),
4173 got_ref_get_name(ref)) == 0) {
4174 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4175 "will not delete this work tree's current branch");
4176 goto done;
4179 err = got_ref_delete(ref, repo);
4180 done:
4181 if (ref)
4182 got_ref_close(ref);
4183 free(refname);
4184 return err;
4187 static const struct got_error *
4188 add_branch(struct got_repository *repo, const char *branch_name,
4189 struct got_object_id *base_commit_id)
4191 const struct got_error *err = NULL;
4192 struct got_reference *ref = NULL;
4193 char *base_refname = NULL, *refname = NULL;
4196 * Don't let the user create a branch name with a leading '-'.
4197 * While technically a valid reference name, this case is usually
4198 * an unintended typo.
4200 if (branch_name[0] == '-')
4201 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4203 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4204 err = got_error_from_errno("asprintf");
4205 goto done;
4208 err = got_ref_open(&ref, repo, refname, 0);
4209 if (err == NULL) {
4210 err = got_error(GOT_ERR_BRANCH_EXISTS);
4211 goto done;
4212 } else if (err->code != GOT_ERR_NOT_REF)
4213 goto done;
4215 err = got_ref_alloc(&ref, refname, base_commit_id);
4216 if (err)
4217 goto done;
4219 err = got_ref_write(ref, repo);
4220 done:
4221 if (ref)
4222 got_ref_close(ref);
4223 free(base_refname);
4224 free(refname);
4225 return err;
4228 static const struct got_error *
4229 cmd_branch(int argc, char *argv[])
4231 const struct got_error *error = NULL;
4232 struct got_repository *repo = NULL;
4233 struct got_worktree *worktree = NULL;
4234 char *cwd = NULL, *repo_path = NULL;
4235 int ch, do_list = 0, do_show = 0, do_update = 1;
4236 const char *delref = NULL, *commit_id_arg = NULL;
4237 struct got_reference *ref = NULL;
4238 struct got_pathlist_head paths;
4239 struct got_pathlist_entry *pe;
4240 struct got_object_id *commit_id = NULL;
4241 char *commit_id_str = NULL;
4243 TAILQ_INIT(&paths);
4245 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4246 switch (ch) {
4247 case 'c':
4248 commit_id_arg = optarg;
4249 break;
4250 case 'd':
4251 delref = optarg;
4252 break;
4253 case 'r':
4254 repo_path = realpath(optarg, NULL);
4255 if (repo_path == NULL)
4256 return got_error_from_errno2("realpath",
4257 optarg);
4258 got_path_strip_trailing_slashes(repo_path);
4259 break;
4260 case 'l':
4261 do_list = 1;
4262 break;
4263 case 'n':
4264 do_update = 0;
4265 break;
4266 default:
4267 usage_branch();
4268 /* NOTREACHED */
4272 if (do_list && delref)
4273 errx(1, "-l and -d options are mutually exclusive\n");
4275 argc -= optind;
4276 argv += optind;
4278 if (!do_list && !delref && argc == 0)
4279 do_show = 1;
4281 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4282 errx(1, "-c option can only be used when creating a branch");
4284 if (do_list || delref) {
4285 if (argc > 0)
4286 usage_branch();
4287 } else if (!do_show && argc != 1)
4288 usage_branch();
4290 #ifndef PROFILE
4291 if (do_list || do_show) {
4292 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4293 NULL) == -1)
4294 err(1, "pledge");
4295 } else {
4296 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4297 "sendfd unveil", NULL) == -1)
4298 err(1, "pledge");
4300 #endif
4301 cwd = getcwd(NULL, 0);
4302 if (cwd == NULL) {
4303 error = got_error_from_errno("getcwd");
4304 goto done;
4307 if (repo_path == NULL) {
4308 error = got_worktree_open(&worktree, cwd);
4309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4310 goto done;
4311 else
4312 error = NULL;
4313 if (worktree) {
4314 repo_path =
4315 strdup(got_worktree_get_repo_path(worktree));
4316 if (repo_path == NULL)
4317 error = got_error_from_errno("strdup");
4318 if (error)
4319 goto done;
4320 } else {
4321 repo_path = strdup(cwd);
4322 if (repo_path == NULL) {
4323 error = got_error_from_errno("strdup");
4324 goto done;
4329 error = got_repo_open(&repo, repo_path, NULL);
4330 if (error != NULL)
4331 goto done;
4333 error = apply_unveil(got_repo_get_path(repo), do_list,
4334 worktree ? got_worktree_get_root_path(worktree) : NULL);
4335 if (error)
4336 goto done;
4338 if (do_show)
4339 error = show_current_branch(repo, worktree);
4340 else if (do_list)
4341 error = list_branches(repo, worktree);
4342 else if (delref)
4343 error = delete_branch(repo, worktree, delref);
4344 else {
4345 if (commit_id_arg == NULL)
4346 commit_id_arg = worktree ?
4347 got_worktree_get_head_ref_name(worktree) :
4348 GOT_REF_HEAD;
4349 error = got_repo_match_object_id(&commit_id, NULL,
4350 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4351 if (error)
4352 goto done;
4353 error = add_branch(repo, argv[0], commit_id);
4354 if (error)
4355 goto done;
4356 if (worktree && do_update) {
4357 int did_something = 0;
4358 char *branch_refname = NULL;
4360 error = got_object_id_str(&commit_id_str, commit_id);
4361 if (error)
4362 goto done;
4363 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4364 worktree);
4365 if (error)
4366 goto done;
4367 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4368 == -1) {
4369 error = got_error_from_errno("asprintf");
4370 goto done;
4372 error = got_ref_open(&ref, repo, branch_refname, 0);
4373 free(branch_refname);
4374 if (error)
4375 goto done;
4376 error = switch_head_ref(ref, commit_id, worktree,
4377 repo);
4378 if (error)
4379 goto done;
4380 error = got_worktree_set_base_commit_id(worktree, repo,
4381 commit_id);
4382 if (error)
4383 goto done;
4384 error = got_worktree_checkout_files(worktree, &paths,
4385 repo, update_progress, &did_something,
4386 check_cancelled, NULL);
4387 if (error)
4388 goto done;
4389 if (did_something)
4390 printf("Updated to commit %s\n", commit_id_str);
4393 done:
4394 if (ref)
4395 got_ref_close(ref);
4396 if (repo)
4397 got_repo_close(repo);
4398 if (worktree)
4399 got_worktree_close(worktree);
4400 free(cwd);
4401 free(repo_path);
4402 free(commit_id);
4403 free(commit_id_str);
4404 TAILQ_FOREACH(pe, &paths, entry)
4405 free((char *)pe->path);
4406 got_pathlist_free(&paths);
4407 return error;
4411 __dead static void
4412 usage_tag(void)
4414 fprintf(stderr,
4415 "usage: %s tag [-c commit] [-r repository] [-l] "
4416 "[-m message] name\n", getprogname());
4417 exit(1);
4420 #if 0
4421 static const struct got_error *
4422 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4424 const struct got_error *err = NULL;
4425 struct got_reflist_entry *re, *se, *new;
4426 struct got_object_id *re_id, *se_id;
4427 struct got_tag_object *re_tag, *se_tag;
4428 time_t re_time, se_time;
4430 SIMPLEQ_FOREACH(re, tags, entry) {
4431 se = SIMPLEQ_FIRST(sorted);
4432 if (se == NULL) {
4433 err = got_reflist_entry_dup(&new, re);
4434 if (err)
4435 return err;
4436 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4437 continue;
4438 } else {
4439 err = got_ref_resolve(&re_id, repo, re->ref);
4440 if (err)
4441 break;
4442 err = got_object_open_as_tag(&re_tag, repo, re_id);
4443 free(re_id);
4444 if (err)
4445 break;
4446 re_time = got_object_tag_get_tagger_time(re_tag);
4447 got_object_tag_close(re_tag);
4450 while (se) {
4451 err = got_ref_resolve(&se_id, repo, re->ref);
4452 if (err)
4453 break;
4454 err = got_object_open_as_tag(&se_tag, repo, se_id);
4455 free(se_id);
4456 if (err)
4457 break;
4458 se_time = got_object_tag_get_tagger_time(se_tag);
4459 got_object_tag_close(se_tag);
4461 if (se_time > re_time) {
4462 err = got_reflist_entry_dup(&new, re);
4463 if (err)
4464 return err;
4465 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4466 break;
4468 se = SIMPLEQ_NEXT(se, entry);
4469 continue;
4472 done:
4473 return err;
4475 #endif
4477 static const struct got_error *
4478 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4480 static const struct got_error *err = NULL;
4481 struct got_reflist_head refs;
4482 struct got_reflist_entry *re;
4484 SIMPLEQ_INIT(&refs);
4486 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4487 if (err)
4488 return err;
4490 SIMPLEQ_FOREACH(re, &refs, entry) {
4491 const char *refname;
4492 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4493 char datebuf[26];
4494 const char *tagger;
4495 time_t tagger_time;
4496 struct got_object_id *id;
4497 struct got_tag_object *tag;
4498 struct got_commit_object *commit = NULL;
4500 refname = got_ref_get_name(re->ref);
4501 if (strncmp(refname, "refs/tags/", 10) != 0)
4502 continue;
4503 refname += 10;
4504 refstr = got_ref_to_str(re->ref);
4505 if (refstr == NULL) {
4506 err = got_error_from_errno("got_ref_to_str");
4507 break;
4509 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4510 free(refstr);
4512 err = got_ref_resolve(&id, repo, re->ref);
4513 if (err)
4514 break;
4515 err = got_object_open_as_tag(&tag, repo, id);
4516 if (err) {
4517 if (err->code != GOT_ERR_OBJ_TYPE) {
4518 free(id);
4519 break;
4521 /* "lightweight" tag */
4522 err = got_object_open_as_commit(&commit, repo, id);
4523 if (err) {
4524 free(id);
4525 break;
4527 tagger = got_object_commit_get_committer(commit);
4528 tagger_time =
4529 got_object_commit_get_committer_time(commit);
4530 err = got_object_id_str(&id_str, id);
4531 free(id);
4532 if (err)
4533 break;
4534 } else {
4535 free(id);
4536 tagger = got_object_tag_get_tagger(tag);
4537 tagger_time = got_object_tag_get_tagger_time(tag);
4538 err = got_object_id_str(&id_str,
4539 got_object_tag_get_object_id(tag));
4540 if (err)
4541 break;
4543 printf("from: %s\n", tagger);
4544 datestr = get_datestr(&tagger_time, datebuf);
4545 if (datestr)
4546 printf("date: %s UTC\n", datestr);
4547 if (commit)
4548 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4549 else {
4550 switch (got_object_tag_get_object_type(tag)) {
4551 case GOT_OBJ_TYPE_BLOB:
4552 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4553 id_str);
4554 break;
4555 case GOT_OBJ_TYPE_TREE:
4556 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4557 id_str);
4558 break;
4559 case GOT_OBJ_TYPE_COMMIT:
4560 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4561 id_str);
4562 break;
4563 case GOT_OBJ_TYPE_TAG:
4564 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4565 id_str);
4566 break;
4567 default:
4568 break;
4571 free(id_str);
4572 if (commit) {
4573 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4574 if (err)
4575 break;
4576 got_object_commit_close(commit);
4577 } else {
4578 tagmsg0 = strdup(got_object_tag_get_message(tag));
4579 got_object_tag_close(tag);
4580 if (tagmsg0 == NULL) {
4581 err = got_error_from_errno("strdup");
4582 break;
4586 tagmsg = tagmsg0;
4587 do {
4588 line = strsep(&tagmsg, "\n");
4589 if (line)
4590 printf(" %s\n", line);
4591 } while (line);
4592 free(tagmsg0);
4595 got_ref_list_free(&refs);
4596 return NULL;
4599 static const struct got_error *
4600 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4601 const char *tag_name, const char *repo_path)
4603 const struct got_error *err = NULL;
4604 char *template = NULL, *initial_content = NULL;
4605 char *editor = NULL;
4606 int fd = -1;
4608 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4609 err = got_error_from_errno("asprintf");
4610 goto done;
4613 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4614 commit_id_str, tag_name) == -1) {
4615 err = got_error_from_errno("asprintf");
4616 goto done;
4619 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4620 if (err)
4621 goto done;
4623 dprintf(fd, initial_content);
4624 close(fd);
4626 err = get_editor(&editor);
4627 if (err)
4628 goto done;
4629 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4630 done:
4631 free(initial_content);
4632 free(template);
4633 free(editor);
4635 /* Editor is done; we can now apply unveil(2) */
4636 if (err == NULL) {
4637 err = apply_unveil(repo_path, 0, NULL);
4638 if (err) {
4639 free(*tagmsg);
4640 *tagmsg = NULL;
4643 return err;
4646 static const struct got_error *
4647 add_tag(struct got_repository *repo, const char *tag_name,
4648 const char *commit_arg, const char *tagmsg_arg)
4650 const struct got_error *err = NULL;
4651 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4652 char *label = NULL, *commit_id_str = NULL;
4653 struct got_reference *ref = NULL;
4654 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4655 char *tagmsg_path = NULL, *tag_id_str = NULL;
4656 int preserve_tagmsg = 0;
4659 * Don't let the user create a tag name with a leading '-'.
4660 * While technically a valid reference name, this case is usually
4661 * an unintended typo.
4663 if (tag_name[0] == '-')
4664 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4666 err = get_author(&tagger, repo);
4667 if (err)
4668 return err;
4670 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4671 GOT_OBJ_TYPE_COMMIT, 1, repo);
4672 if (err)
4673 goto done;
4675 err = got_object_id_str(&commit_id_str, commit_id);
4676 if (err)
4677 goto done;
4679 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4680 refname = strdup(tag_name);
4681 if (refname == NULL) {
4682 err = got_error_from_errno("strdup");
4683 goto done;
4685 tag_name += 10;
4686 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4687 err = got_error_from_errno("asprintf");
4688 goto done;
4691 err = got_ref_open(&ref, repo, refname, 0);
4692 if (err == NULL) {
4693 err = got_error(GOT_ERR_TAG_EXISTS);
4694 goto done;
4695 } else if (err->code != GOT_ERR_NOT_REF)
4696 goto done;
4698 if (tagmsg_arg == NULL) {
4699 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4700 tag_name, got_repo_get_path(repo));
4701 if (err) {
4702 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4703 tagmsg_path != NULL)
4704 preserve_tagmsg = 1;
4705 goto done;
4709 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4710 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4711 if (err) {
4712 if (tagmsg_path)
4713 preserve_tagmsg = 1;
4714 goto done;
4717 err = got_ref_alloc(&ref, refname, tag_id);
4718 if (err) {
4719 if (tagmsg_path)
4720 preserve_tagmsg = 1;
4721 goto done;
4724 err = got_ref_write(ref, repo);
4725 if (err) {
4726 if (tagmsg_path)
4727 preserve_tagmsg = 1;
4728 goto done;
4731 err = got_object_id_str(&tag_id_str, tag_id);
4732 if (err) {
4733 if (tagmsg_path)
4734 preserve_tagmsg = 1;
4735 goto done;
4737 printf("Created tag %s\n", tag_id_str);
4738 done:
4739 if (preserve_tagmsg) {
4740 fprintf(stderr, "%s: tag message preserved in %s\n",
4741 getprogname(), tagmsg_path);
4742 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4743 err = got_error_from_errno2("unlink", tagmsg_path);
4744 free(tag_id_str);
4745 if (ref)
4746 got_ref_close(ref);
4747 free(commit_id);
4748 free(commit_id_str);
4749 free(refname);
4750 free(tagmsg);
4751 free(tagmsg_path);
4752 free(tagger);
4753 return err;
4756 static const struct got_error *
4757 cmd_tag(int argc, char *argv[])
4759 const struct got_error *error = NULL;
4760 struct got_repository *repo = NULL;
4761 struct got_worktree *worktree = NULL;
4762 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4763 char *gitconfig_path = NULL;
4764 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4765 int ch, do_list = 0;
4767 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4768 switch (ch) {
4769 case 'c':
4770 commit_id_arg = optarg;
4771 break;
4772 case 'm':
4773 tagmsg = optarg;
4774 break;
4775 case 'r':
4776 repo_path = realpath(optarg, NULL);
4777 if (repo_path == NULL)
4778 return got_error_from_errno2("realpath",
4779 optarg);
4780 got_path_strip_trailing_slashes(repo_path);
4781 break;
4782 case 'l':
4783 do_list = 1;
4784 break;
4785 default:
4786 usage_tag();
4787 /* NOTREACHED */
4791 argc -= optind;
4792 argv += optind;
4794 if (do_list) {
4795 if (commit_id_arg != NULL)
4796 errx(1, "-c option can only be used when creating a tag");
4797 if (tagmsg)
4798 errx(1, "-l and -m options are mutually exclusive");
4799 if (argc > 0)
4800 usage_tag();
4801 } else if (argc != 1)
4802 usage_tag();
4804 tag_name = argv[0];
4806 #ifndef PROFILE
4807 if (do_list) {
4808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4809 NULL) == -1)
4810 err(1, "pledge");
4811 } else {
4812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4813 "sendfd unveil", NULL) == -1)
4814 err(1, "pledge");
4816 #endif
4817 cwd = getcwd(NULL, 0);
4818 if (cwd == NULL) {
4819 error = got_error_from_errno("getcwd");
4820 goto done;
4823 if (repo_path == NULL) {
4824 error = got_worktree_open(&worktree, cwd);
4825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4826 goto done;
4827 else
4828 error = NULL;
4829 if (worktree) {
4830 repo_path =
4831 strdup(got_worktree_get_repo_path(worktree));
4832 if (repo_path == NULL)
4833 error = got_error_from_errno("strdup");
4834 if (error)
4835 goto done;
4836 } else {
4837 repo_path = strdup(cwd);
4838 if (repo_path == NULL) {
4839 error = got_error_from_errno("strdup");
4840 goto done;
4845 if (do_list) {
4846 error = got_repo_open(&repo, repo_path, NULL);
4847 if (error != NULL)
4848 goto done;
4849 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4850 if (error)
4851 goto done;
4852 error = list_tags(repo, worktree);
4853 } else {
4854 error = get_gitconfig_path(&gitconfig_path);
4855 if (error)
4856 goto done;
4857 error = got_repo_open(&repo, repo_path, gitconfig_path);
4858 if (error != NULL)
4859 goto done;
4861 if (tagmsg) {
4862 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4863 if (error)
4864 goto done;
4867 if (commit_id_arg == NULL) {
4868 struct got_reference *head_ref;
4869 struct got_object_id *commit_id;
4870 error = got_ref_open(&head_ref, repo,
4871 worktree ? got_worktree_get_head_ref_name(worktree)
4872 : GOT_REF_HEAD, 0);
4873 if (error)
4874 goto done;
4875 error = got_ref_resolve(&commit_id, repo, head_ref);
4876 got_ref_close(head_ref);
4877 if (error)
4878 goto done;
4879 error = got_object_id_str(&commit_id_str, commit_id);
4880 free(commit_id);
4881 if (error)
4882 goto done;
4885 error = add_tag(repo, tag_name,
4886 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4888 done:
4889 if (repo)
4890 got_repo_close(repo);
4891 if (worktree)
4892 got_worktree_close(worktree);
4893 free(cwd);
4894 free(repo_path);
4895 free(gitconfig_path);
4896 free(commit_id_str);
4897 return error;
4900 __dead static void
4901 usage_add(void)
4903 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4904 getprogname());
4905 exit(1);
4908 static const struct got_error *
4909 add_progress(void *arg, unsigned char status, const char *path)
4911 while (path[0] == '/')
4912 path++;
4913 printf("%c %s\n", status, path);
4914 return NULL;
4917 static const struct got_error *
4918 cmd_add(int argc, char *argv[])
4920 const struct got_error *error = NULL;
4921 struct got_repository *repo = NULL;
4922 struct got_worktree *worktree = NULL;
4923 char *cwd = NULL;
4924 struct got_pathlist_head paths;
4925 struct got_pathlist_entry *pe;
4926 int ch, can_recurse = 0, no_ignores = 0;
4928 TAILQ_INIT(&paths);
4930 while ((ch = getopt(argc, argv, "IR")) != -1) {
4931 switch (ch) {
4932 case 'I':
4933 no_ignores = 1;
4934 break;
4935 case 'R':
4936 can_recurse = 1;
4937 break;
4938 default:
4939 usage_add();
4940 /* NOTREACHED */
4944 argc -= optind;
4945 argv += optind;
4947 #ifndef PROFILE
4948 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4949 NULL) == -1)
4950 err(1, "pledge");
4951 #endif
4952 if (argc < 1)
4953 usage_add();
4955 cwd = getcwd(NULL, 0);
4956 if (cwd == NULL) {
4957 error = got_error_from_errno("getcwd");
4958 goto done;
4961 error = got_worktree_open(&worktree, cwd);
4962 if (error)
4963 goto done;
4965 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4966 NULL);
4967 if (error != NULL)
4968 goto done;
4970 error = apply_unveil(got_repo_get_path(repo), 1,
4971 got_worktree_get_root_path(worktree));
4972 if (error)
4973 goto done;
4975 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4976 if (error)
4977 goto done;
4979 if (!can_recurse && no_ignores) {
4980 error = got_error_msg(GOT_ERR_BAD_PATH,
4981 "disregarding ignores requires -R option");
4982 goto done;
4986 if (!can_recurse) {
4987 char *ondisk_path;
4988 struct stat sb;
4989 TAILQ_FOREACH(pe, &paths, entry) {
4990 if (asprintf(&ondisk_path, "%s/%s",
4991 got_worktree_get_root_path(worktree),
4992 pe->path) == -1) {
4993 error = got_error_from_errno("asprintf");
4994 goto done;
4996 if (lstat(ondisk_path, &sb) == -1) {
4997 if (errno == ENOENT) {
4998 free(ondisk_path);
4999 continue;
5001 error = got_error_from_errno2("lstat",
5002 ondisk_path);
5003 free(ondisk_path);
5004 goto done;
5006 free(ondisk_path);
5007 if (S_ISDIR(sb.st_mode)) {
5008 error = got_error_msg(GOT_ERR_BAD_PATH,
5009 "adding directories requires -R option");
5010 goto done;
5015 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5016 NULL, repo, no_ignores);
5017 done:
5018 if (repo)
5019 got_repo_close(repo);
5020 if (worktree)
5021 got_worktree_close(worktree);
5022 TAILQ_FOREACH(pe, &paths, entry)
5023 free((char *)pe->path);
5024 got_pathlist_free(&paths);
5025 free(cwd);
5026 return error;
5029 __dead static void
5030 usage_remove(void)
5032 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5033 getprogname());
5034 exit(1);
5037 static const struct got_error *
5038 print_remove_status(void *arg, unsigned char status,
5039 unsigned char staged_status, const char *path)
5041 while (path[0] == '/')
5042 path++;
5043 if (status == GOT_STATUS_NONEXISTENT)
5044 return NULL;
5045 if (status == staged_status && (status == GOT_STATUS_DELETE))
5046 status = GOT_STATUS_NO_CHANGE;
5047 printf("%c%c %s\n", status, staged_status, path);
5048 return NULL;
5051 static const struct got_error *
5052 cmd_remove(int argc, char *argv[])
5054 const struct got_error *error = NULL;
5055 struct got_worktree *worktree = NULL;
5056 struct got_repository *repo = NULL;
5057 char *cwd = NULL;
5058 struct got_pathlist_head paths;
5059 struct got_pathlist_entry *pe;
5060 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5062 TAILQ_INIT(&paths);
5064 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5065 switch (ch) {
5066 case 'f':
5067 delete_local_mods = 1;
5068 break;
5069 case 'k':
5070 keep_on_disk = 1;
5071 break;
5072 case 'R':
5073 can_recurse = 1;
5074 break;
5075 default:
5076 usage_remove();
5077 /* NOTREACHED */
5081 argc -= optind;
5082 argv += optind;
5084 #ifndef PROFILE
5085 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5086 NULL) == -1)
5087 err(1, "pledge");
5088 #endif
5089 if (argc < 1)
5090 usage_remove();
5092 cwd = getcwd(NULL, 0);
5093 if (cwd == NULL) {
5094 error = got_error_from_errno("getcwd");
5095 goto done;
5097 error = got_worktree_open(&worktree, cwd);
5098 if (error)
5099 goto done;
5101 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5102 NULL);
5103 if (error)
5104 goto done;
5106 error = apply_unveil(got_repo_get_path(repo), 1,
5107 got_worktree_get_root_path(worktree));
5108 if (error)
5109 goto done;
5111 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5112 if (error)
5113 goto done;
5115 if (!can_recurse) {
5116 char *ondisk_path;
5117 struct stat sb;
5118 TAILQ_FOREACH(pe, &paths, entry) {
5119 if (asprintf(&ondisk_path, "%s/%s",
5120 got_worktree_get_root_path(worktree),
5121 pe->path) == -1) {
5122 error = got_error_from_errno("asprintf");
5123 goto done;
5125 if (lstat(ondisk_path, &sb) == -1) {
5126 if (errno == ENOENT) {
5127 free(ondisk_path);
5128 continue;
5130 error = got_error_from_errno2("lstat",
5131 ondisk_path);
5132 free(ondisk_path);
5133 goto done;
5135 free(ondisk_path);
5136 if (S_ISDIR(sb.st_mode)) {
5137 error = got_error_msg(GOT_ERR_BAD_PATH,
5138 "removing directories requires -R option");
5139 goto done;
5144 error = got_worktree_schedule_delete(worktree, &paths,
5145 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5146 done:
5147 if (repo)
5148 got_repo_close(repo);
5149 if (worktree)
5150 got_worktree_close(worktree);
5151 TAILQ_FOREACH(pe, &paths, entry)
5152 free((char *)pe->path);
5153 got_pathlist_free(&paths);
5154 free(cwd);
5155 return error;
5158 __dead static void
5159 usage_revert(void)
5161 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5162 "path ...\n", getprogname());
5163 exit(1);
5166 static const struct got_error *
5167 revert_progress(void *arg, unsigned char status, const char *path)
5169 if (status == GOT_STATUS_UNVERSIONED)
5170 return NULL;
5172 while (path[0] == '/')
5173 path++;
5174 printf("%c %s\n", status, path);
5175 return NULL;
5178 struct choose_patch_arg {
5179 FILE *patch_script_file;
5180 const char *action;
5183 static const struct got_error *
5184 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5185 int nchanges, const char *action)
5187 char *line = NULL;
5188 size_t linesize = 0;
5189 ssize_t linelen;
5191 switch (status) {
5192 case GOT_STATUS_ADD:
5193 printf("A %s\n%s this addition? [y/n] ", path, action);
5194 break;
5195 case GOT_STATUS_DELETE:
5196 printf("D %s\n%s this deletion? [y/n] ", path, action);
5197 break;
5198 case GOT_STATUS_MODIFY:
5199 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5200 return got_error_from_errno("fseek");
5201 printf(GOT_COMMIT_SEP_STR);
5202 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5203 printf("%s", line);
5204 if (ferror(patch_file))
5205 return got_error_from_errno("getline");
5206 printf(GOT_COMMIT_SEP_STR);
5207 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5208 path, n, nchanges, action);
5209 break;
5210 default:
5211 return got_error_path(path, GOT_ERR_FILE_STATUS);
5214 return NULL;
5217 static const struct got_error *
5218 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5219 FILE *patch_file, int n, int nchanges)
5221 const struct got_error *err = NULL;
5222 char *line = NULL;
5223 size_t linesize = 0;
5224 ssize_t linelen;
5225 int resp = ' ';
5226 struct choose_patch_arg *a = arg;
5228 *choice = GOT_PATCH_CHOICE_NONE;
5230 if (a->patch_script_file) {
5231 char *nl;
5232 err = show_change(status, path, patch_file, n, nchanges,
5233 a->action);
5234 if (err)
5235 return err;
5236 linelen = getline(&line, &linesize, a->patch_script_file);
5237 if (linelen == -1) {
5238 if (ferror(a->patch_script_file))
5239 return got_error_from_errno("getline");
5240 return NULL;
5242 nl = strchr(line, '\n');
5243 if (nl)
5244 *nl = '\0';
5245 if (strcmp(line, "y") == 0) {
5246 *choice = GOT_PATCH_CHOICE_YES;
5247 printf("y\n");
5248 } else if (strcmp(line, "n") == 0) {
5249 *choice = GOT_PATCH_CHOICE_NO;
5250 printf("n\n");
5251 } else if (strcmp(line, "q") == 0 &&
5252 status == GOT_STATUS_MODIFY) {
5253 *choice = GOT_PATCH_CHOICE_QUIT;
5254 printf("q\n");
5255 } else
5256 printf("invalid response '%s'\n", line);
5257 free(line);
5258 return NULL;
5261 while (resp != 'y' && resp != 'n' && resp != 'q') {
5262 err = show_change(status, path, patch_file, n, nchanges,
5263 a->action);
5264 if (err)
5265 return err;
5266 resp = getchar();
5267 if (resp == '\n')
5268 resp = getchar();
5269 if (status == GOT_STATUS_MODIFY) {
5270 if (resp != 'y' && resp != 'n' && resp != 'q') {
5271 printf("invalid response '%c'\n", resp);
5272 resp = ' ';
5274 } else if (resp != 'y' && resp != 'n') {
5275 printf("invalid response '%c'\n", resp);
5276 resp = ' ';
5280 if (resp == 'y')
5281 *choice = GOT_PATCH_CHOICE_YES;
5282 else if (resp == 'n')
5283 *choice = GOT_PATCH_CHOICE_NO;
5284 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5285 *choice = GOT_PATCH_CHOICE_QUIT;
5287 return NULL;
5291 static const struct got_error *
5292 cmd_revert(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_worktree *worktree = NULL;
5296 struct got_repository *repo = NULL;
5297 char *cwd = NULL, *path = NULL;
5298 struct got_pathlist_head paths;
5299 struct got_pathlist_entry *pe;
5300 int ch, can_recurse = 0, pflag = 0;
5301 FILE *patch_script_file = NULL;
5302 const char *patch_script_path = NULL;
5303 struct choose_patch_arg cpa;
5305 TAILQ_INIT(&paths);
5307 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5308 switch (ch) {
5309 case 'p':
5310 pflag = 1;
5311 break;
5312 case 'F':
5313 patch_script_path = optarg;
5314 break;
5315 case 'R':
5316 can_recurse = 1;
5317 break;
5318 default:
5319 usage_revert();
5320 /* NOTREACHED */
5324 argc -= optind;
5325 argv += optind;
5327 #ifndef PROFILE
5328 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5329 "unveil", NULL) == -1)
5330 err(1, "pledge");
5331 #endif
5332 if (argc < 1)
5333 usage_revert();
5334 if (patch_script_path && !pflag)
5335 errx(1, "-F option can only be used together with -p option");
5337 cwd = getcwd(NULL, 0);
5338 if (cwd == NULL) {
5339 error = got_error_from_errno("getcwd");
5340 goto done;
5342 error = got_worktree_open(&worktree, cwd);
5343 if (error)
5344 goto done;
5346 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5347 NULL);
5348 if (error != NULL)
5349 goto done;
5351 if (patch_script_path) {
5352 patch_script_file = fopen(patch_script_path, "r");
5353 if (patch_script_file == NULL) {
5354 error = got_error_from_errno2("fopen",
5355 patch_script_path);
5356 goto done;
5359 error = apply_unveil(got_repo_get_path(repo), 1,
5360 got_worktree_get_root_path(worktree));
5361 if (error)
5362 goto done;
5364 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5365 if (error)
5366 goto done;
5368 if (!can_recurse) {
5369 char *ondisk_path;
5370 struct stat sb;
5371 TAILQ_FOREACH(pe, &paths, entry) {
5372 if (asprintf(&ondisk_path, "%s/%s",
5373 got_worktree_get_root_path(worktree),
5374 pe->path) == -1) {
5375 error = got_error_from_errno("asprintf");
5376 goto done;
5378 if (lstat(ondisk_path, &sb) == -1) {
5379 if (errno == ENOENT) {
5380 free(ondisk_path);
5381 continue;
5383 error = got_error_from_errno2("lstat",
5384 ondisk_path);
5385 free(ondisk_path);
5386 goto done;
5388 free(ondisk_path);
5389 if (S_ISDIR(sb.st_mode)) {
5390 error = got_error_msg(GOT_ERR_BAD_PATH,
5391 "reverting directories requires -R option");
5392 goto done;
5397 cpa.patch_script_file = patch_script_file;
5398 cpa.action = "revert";
5399 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5400 pflag ? choose_patch : NULL, &cpa, repo);
5401 done:
5402 if (patch_script_file && fclose(patch_script_file) == EOF &&
5403 error == NULL)
5404 error = got_error_from_errno2("fclose", patch_script_path);
5405 if (repo)
5406 got_repo_close(repo);
5407 if (worktree)
5408 got_worktree_close(worktree);
5409 free(path);
5410 free(cwd);
5411 return error;
5414 __dead static void
5415 usage_commit(void)
5417 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5418 getprogname());
5419 exit(1);
5422 struct collect_commit_logmsg_arg {
5423 const char *cmdline_log;
5424 const char *editor;
5425 const char *worktree_path;
5426 const char *branch_name;
5427 const char *repo_path;
5428 char *logmsg_path;
5432 static const struct got_error *
5433 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5434 void *arg)
5436 char *initial_content = NULL;
5437 struct got_pathlist_entry *pe;
5438 const struct got_error *err = NULL;
5439 char *template = NULL;
5440 struct collect_commit_logmsg_arg *a = arg;
5441 int fd;
5442 size_t len;
5444 /* if a message was specified on the command line, just use it */
5445 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5446 len = strlen(a->cmdline_log) + 1;
5447 *logmsg = malloc(len + 1);
5448 if (*logmsg == NULL)
5449 return got_error_from_errno("malloc");
5450 strlcpy(*logmsg, a->cmdline_log, len);
5451 return NULL;
5454 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5455 return got_error_from_errno("asprintf");
5457 if (asprintf(&initial_content,
5458 "\n# changes to be committed on branch %s:\n",
5459 a->branch_name) == -1)
5460 return got_error_from_errno("asprintf");
5462 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5463 if (err)
5464 goto done;
5466 dprintf(fd, initial_content);
5468 TAILQ_FOREACH(pe, commitable_paths, entry) {
5469 struct got_commitable *ct = pe->data;
5470 dprintf(fd, "# %c %s\n",
5471 got_commitable_get_status(ct),
5472 got_commitable_get_path(ct));
5474 close(fd);
5476 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5477 done:
5478 free(initial_content);
5479 free(template);
5481 /* Editor is done; we can now apply unveil(2) */
5482 if (err == NULL) {
5483 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5484 if (err) {
5485 free(*logmsg);
5486 *logmsg = NULL;
5489 return err;
5492 static const struct got_error *
5493 cmd_commit(int argc, char *argv[])
5495 const struct got_error *error = NULL;
5496 struct got_worktree *worktree = NULL;
5497 struct got_repository *repo = NULL;
5498 char *cwd = NULL, *id_str = NULL;
5499 struct got_object_id *id = NULL;
5500 const char *logmsg = NULL;
5501 struct collect_commit_logmsg_arg cl_arg;
5502 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5503 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5504 struct got_pathlist_head paths;
5506 TAILQ_INIT(&paths);
5507 cl_arg.logmsg_path = NULL;
5509 while ((ch = getopt(argc, argv, "m:")) != -1) {
5510 switch (ch) {
5511 case 'm':
5512 logmsg = optarg;
5513 break;
5514 default:
5515 usage_commit();
5516 /* NOTREACHED */
5520 argc -= optind;
5521 argv += optind;
5523 #ifndef PROFILE
5524 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5525 "unveil", NULL) == -1)
5526 err(1, "pledge");
5527 #endif
5528 cwd = getcwd(NULL, 0);
5529 if (cwd == NULL) {
5530 error = got_error_from_errno("getcwd");
5531 goto done;
5533 error = got_worktree_open(&worktree, cwd);
5534 if (error)
5535 goto done;
5537 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5538 if (error)
5539 goto done;
5540 if (rebase_in_progress) {
5541 error = got_error(GOT_ERR_REBASING);
5542 goto done;
5545 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5546 worktree);
5547 if (error)
5548 goto done;
5550 error = get_gitconfig_path(&gitconfig_path);
5551 if (error)
5552 goto done;
5553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5554 gitconfig_path);
5555 if (error != NULL)
5556 goto done;
5558 error = get_author(&author, repo);
5559 if (error)
5560 return error;
5563 * unveil(2) traverses exec(2); if an editor is used we have
5564 * to apply unveil after the log message has been written.
5566 if (logmsg == NULL || strlen(logmsg) == 0)
5567 error = get_editor(&editor);
5568 else
5569 error = apply_unveil(got_repo_get_path(repo), 0,
5570 got_worktree_get_root_path(worktree));
5571 if (error)
5572 goto done;
5574 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5575 if (error)
5576 goto done;
5578 cl_arg.editor = editor;
5579 cl_arg.cmdline_log = logmsg;
5580 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5581 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5582 if (!histedit_in_progress) {
5583 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5584 error = got_error(GOT_ERR_COMMIT_BRANCH);
5585 goto done;
5587 cl_arg.branch_name += 11;
5589 cl_arg.repo_path = got_repo_get_path(repo);
5590 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5591 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5592 if (error) {
5593 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5594 cl_arg.logmsg_path != NULL)
5595 preserve_logmsg = 1;
5596 goto done;
5599 error = got_object_id_str(&id_str, id);
5600 if (error)
5601 goto done;
5602 printf("Created commit %s\n", id_str);
5603 done:
5604 if (preserve_logmsg) {
5605 fprintf(stderr, "%s: log message preserved in %s\n",
5606 getprogname(), cl_arg.logmsg_path);
5607 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5608 error == NULL)
5609 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5610 free(cl_arg.logmsg_path);
5611 if (repo)
5612 got_repo_close(repo);
5613 if (worktree)
5614 got_worktree_close(worktree);
5615 free(cwd);
5616 free(id_str);
5617 free(gitconfig_path);
5618 free(editor);
5619 free(author);
5620 return error;
5623 __dead static void
5624 usage_cherrypick(void)
5626 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5627 exit(1);
5630 static const struct got_error *
5631 cmd_cherrypick(int argc, char *argv[])
5633 const struct got_error *error = NULL;
5634 struct got_worktree *worktree = NULL;
5635 struct got_repository *repo = NULL;
5636 char *cwd = NULL, *commit_id_str = NULL;
5637 struct got_object_id *commit_id = NULL;
5638 struct got_commit_object *commit = NULL;
5639 struct got_object_qid *pid;
5640 struct got_reference *head_ref = NULL;
5641 int ch, did_something = 0;
5643 while ((ch = getopt(argc, argv, "")) != -1) {
5644 switch (ch) {
5645 default:
5646 usage_cherrypick();
5647 /* NOTREACHED */
5651 argc -= optind;
5652 argv += optind;
5654 #ifndef PROFILE
5655 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5656 "unveil", NULL) == -1)
5657 err(1, "pledge");
5658 #endif
5659 if (argc != 1)
5660 usage_cherrypick();
5662 cwd = getcwd(NULL, 0);
5663 if (cwd == NULL) {
5664 error = got_error_from_errno("getcwd");
5665 goto done;
5667 error = got_worktree_open(&worktree, cwd);
5668 if (error)
5669 goto done;
5671 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5672 NULL);
5673 if (error != NULL)
5674 goto done;
5676 error = apply_unveil(got_repo_get_path(repo), 0,
5677 got_worktree_get_root_path(worktree));
5678 if (error)
5679 goto done;
5681 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5682 GOT_OBJ_TYPE_COMMIT, repo);
5683 if (error != NULL) {
5684 struct got_reference *ref;
5685 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5686 goto done;
5687 error = got_ref_open(&ref, repo, argv[0], 0);
5688 if (error != NULL)
5689 goto done;
5690 error = got_ref_resolve(&commit_id, repo, ref);
5691 got_ref_close(ref);
5692 if (error != NULL)
5693 goto done;
5695 error = got_object_id_str(&commit_id_str, commit_id);
5696 if (error)
5697 goto done;
5699 error = got_ref_open(&head_ref, repo,
5700 got_worktree_get_head_ref_name(worktree), 0);
5701 if (error != NULL)
5702 goto done;
5704 error = check_same_branch(commit_id, head_ref, NULL, repo);
5705 if (error) {
5706 if (error->code != GOT_ERR_ANCESTRY)
5707 goto done;
5708 error = NULL;
5709 } else {
5710 error = got_error(GOT_ERR_SAME_BRANCH);
5711 goto done;
5714 error = got_object_open_as_commit(&commit, repo, commit_id);
5715 if (error)
5716 goto done;
5717 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5718 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5719 commit_id, repo, update_progress, &did_something, check_cancelled,
5720 NULL);
5721 if (error != NULL)
5722 goto done;
5724 if (did_something)
5725 printf("Merged commit %s\n", commit_id_str);
5726 done:
5727 if (commit)
5728 got_object_commit_close(commit);
5729 free(commit_id_str);
5730 if (head_ref)
5731 got_ref_close(head_ref);
5732 if (worktree)
5733 got_worktree_close(worktree);
5734 if (repo)
5735 got_repo_close(repo);
5736 return error;
5739 __dead static void
5740 usage_backout(void)
5742 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5743 exit(1);
5746 static const struct got_error *
5747 cmd_backout(int argc, char *argv[])
5749 const struct got_error *error = NULL;
5750 struct got_worktree *worktree = NULL;
5751 struct got_repository *repo = NULL;
5752 char *cwd = NULL, *commit_id_str = NULL;
5753 struct got_object_id *commit_id = NULL;
5754 struct got_commit_object *commit = NULL;
5755 struct got_object_qid *pid;
5756 struct got_reference *head_ref = NULL;
5757 int ch, did_something = 0;
5759 while ((ch = getopt(argc, argv, "")) != -1) {
5760 switch (ch) {
5761 default:
5762 usage_backout();
5763 /* NOTREACHED */
5767 argc -= optind;
5768 argv += optind;
5770 #ifndef PROFILE
5771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5772 "unveil", NULL) == -1)
5773 err(1, "pledge");
5774 #endif
5775 if (argc != 1)
5776 usage_backout();
5778 cwd = getcwd(NULL, 0);
5779 if (cwd == NULL) {
5780 error = got_error_from_errno("getcwd");
5781 goto done;
5783 error = got_worktree_open(&worktree, cwd);
5784 if (error)
5785 goto done;
5787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5788 NULL);
5789 if (error != NULL)
5790 goto done;
5792 error = apply_unveil(got_repo_get_path(repo), 0,
5793 got_worktree_get_root_path(worktree));
5794 if (error)
5795 goto done;
5797 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5798 GOT_OBJ_TYPE_COMMIT, repo);
5799 if (error != NULL) {
5800 struct got_reference *ref;
5801 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5802 goto done;
5803 error = got_ref_open(&ref, repo, argv[0], 0);
5804 if (error != NULL)
5805 goto done;
5806 error = got_ref_resolve(&commit_id, repo, ref);
5807 got_ref_close(ref);
5808 if (error != NULL)
5809 goto done;
5811 error = got_object_id_str(&commit_id_str, commit_id);
5812 if (error)
5813 goto done;
5815 error = got_ref_open(&head_ref, repo,
5816 got_worktree_get_head_ref_name(worktree), 0);
5817 if (error != NULL)
5818 goto done;
5820 error = check_same_branch(commit_id, head_ref, NULL, repo);
5821 if (error)
5822 goto done;
5824 error = got_object_open_as_commit(&commit, repo, commit_id);
5825 if (error)
5826 goto done;
5827 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5828 if (pid == NULL) {
5829 error = got_error(GOT_ERR_ROOT_COMMIT);
5830 goto done;
5833 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5834 update_progress, &did_something, check_cancelled, NULL);
5835 if (error != NULL)
5836 goto done;
5838 if (did_something)
5839 printf("Backed out commit %s\n", commit_id_str);
5840 done:
5841 if (commit)
5842 got_object_commit_close(commit);
5843 free(commit_id_str);
5844 if (head_ref)
5845 got_ref_close(head_ref);
5846 if (worktree)
5847 got_worktree_close(worktree);
5848 if (repo)
5849 got_repo_close(repo);
5850 return error;
5853 __dead static void
5854 usage_rebase(void)
5856 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5857 getprogname());
5858 exit(1);
5861 void
5862 trim_logmsg(char *logmsg, int limit)
5864 char *nl;
5865 size_t len;
5867 len = strlen(logmsg);
5868 if (len > limit)
5869 len = limit;
5870 logmsg[len] = '\0';
5871 nl = strchr(logmsg, '\n');
5872 if (nl)
5873 *nl = '\0';
5876 static const struct got_error *
5877 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5879 const struct got_error *err;
5880 char *logmsg0 = NULL;
5881 const char *s;
5883 err = got_object_commit_get_logmsg(&logmsg0, commit);
5884 if (err)
5885 return err;
5887 s = logmsg0;
5888 while (isspace((unsigned char)s[0]))
5889 s++;
5891 *logmsg = strdup(s);
5892 if (*logmsg == NULL) {
5893 err = got_error_from_errno("strdup");
5894 goto done;
5897 trim_logmsg(*logmsg, limit);
5898 done:
5899 free(logmsg0);
5900 return err;
5903 static const struct got_error *
5904 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5906 const struct got_error *err;
5907 struct got_commit_object *commit = NULL;
5908 char *id_str = NULL, *logmsg = NULL;
5910 err = got_object_open_as_commit(&commit, repo, id);
5911 if (err)
5912 return err;
5914 err = got_object_id_str(&id_str, id);
5915 if (err)
5916 goto done;
5918 id_str[12] = '\0';
5920 err = get_short_logmsg(&logmsg, 42, commit);
5921 if (err)
5922 goto done;
5924 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5925 done:
5926 free(id_str);
5927 got_object_commit_close(commit);
5928 free(logmsg);
5929 return err;
5932 static const struct got_error *
5933 show_rebase_progress(struct got_commit_object *commit,
5934 struct got_object_id *old_id, struct got_object_id *new_id)
5936 const struct got_error *err;
5937 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5939 err = got_object_id_str(&old_id_str, old_id);
5940 if (err)
5941 goto done;
5943 if (new_id) {
5944 err = got_object_id_str(&new_id_str, new_id);
5945 if (err)
5946 goto done;
5949 old_id_str[12] = '\0';
5950 if (new_id_str)
5951 new_id_str[12] = '\0';
5953 err = get_short_logmsg(&logmsg, 42, commit);
5954 if (err)
5955 goto done;
5957 printf("%s -> %s: %s\n", old_id_str,
5958 new_id_str ? new_id_str : "no-op change", logmsg);
5959 done:
5960 free(old_id_str);
5961 free(new_id_str);
5962 free(logmsg);
5963 return err;
5966 static const struct got_error *
5967 rebase_progress(void *arg, unsigned char status, const char *path)
5969 unsigned char *rebase_status = arg;
5971 while (path[0] == '/')
5972 path++;
5973 printf("%c %s\n", status, path);
5975 if (*rebase_status == GOT_STATUS_CONFLICT)
5976 return NULL;
5977 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5978 *rebase_status = status;
5979 return NULL;
5982 static const struct got_error *
5983 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5984 struct got_reference *branch, struct got_reference *new_base_branch,
5985 struct got_reference *tmp_branch, struct got_repository *repo)
5987 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5988 return got_worktree_rebase_complete(worktree, fileindex,
5989 new_base_branch, tmp_branch, branch, repo);
5992 static const struct got_error *
5993 rebase_commit(struct got_pathlist_head *merged_paths,
5994 struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *tmp_branch,
5996 struct got_object_id *commit_id, struct got_repository *repo)
5998 const struct got_error *error;
5999 struct got_commit_object *commit;
6000 struct got_object_id *new_commit_id;
6002 error = got_object_open_as_commit(&commit, repo, commit_id);
6003 if (error)
6004 return error;
6006 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6007 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6008 if (error) {
6009 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6010 goto done;
6011 error = show_rebase_progress(commit, commit_id, NULL);
6012 } else {
6013 error = show_rebase_progress(commit, commit_id, new_commit_id);
6014 free(new_commit_id);
6016 done:
6017 got_object_commit_close(commit);
6018 return error;
6021 struct check_path_prefix_arg {
6022 const char *path_prefix;
6023 size_t len;
6024 int errcode;
6027 static const struct got_error *
6028 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6029 struct got_blob_object *blob2, struct got_object_id *id1,
6030 struct got_object_id *id2, const char *path1, const char *path2,
6031 mode_t mode1, mode_t mode2, struct got_repository *repo)
6033 struct check_path_prefix_arg *a = arg;
6035 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6036 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6037 return got_error(a->errcode);
6039 return NULL;
6042 static const struct got_error *
6043 check_path_prefix(struct got_object_id *parent_id,
6044 struct got_object_id *commit_id, const char *path_prefix,
6045 int errcode, struct got_repository *repo)
6047 const struct got_error *err;
6048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6049 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6050 struct check_path_prefix_arg cpp_arg;
6052 if (got_path_is_root_dir(path_prefix))
6053 return NULL;
6055 err = got_object_open_as_commit(&commit, repo, commit_id);
6056 if (err)
6057 goto done;
6059 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6060 if (err)
6061 goto done;
6063 err = got_object_open_as_tree(&tree1, repo,
6064 got_object_commit_get_tree_id(parent_commit));
6065 if (err)
6066 goto done;
6068 err = got_object_open_as_tree(&tree2, repo,
6069 got_object_commit_get_tree_id(commit));
6070 if (err)
6071 goto done;
6073 cpp_arg.path_prefix = path_prefix;
6074 while (cpp_arg.path_prefix[0] == '/')
6075 cpp_arg.path_prefix++;
6076 cpp_arg.len = strlen(cpp_arg.path_prefix);
6077 cpp_arg.errcode = errcode;
6078 err = got_diff_tree(tree1, tree2, "", "", repo,
6079 check_path_prefix_in_diff, &cpp_arg, 0);
6080 done:
6081 if (tree1)
6082 got_object_tree_close(tree1);
6083 if (tree2)
6084 got_object_tree_close(tree2);
6085 if (commit)
6086 got_object_commit_close(commit);
6087 if (parent_commit)
6088 got_object_commit_close(parent_commit);
6089 return err;
6092 static const struct got_error *
6093 collect_commits(struct got_object_id_queue *commits,
6094 struct got_object_id *initial_commit_id,
6095 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6096 const char *path_prefix, int path_prefix_errcode,
6097 struct got_repository *repo)
6099 const struct got_error *err = NULL;
6100 struct got_commit_graph *graph = NULL;
6101 struct got_object_id *parent_id = NULL;
6102 struct got_object_qid *qid;
6103 struct got_object_id *commit_id = initial_commit_id;
6105 err = got_commit_graph_open(&graph, "/", 1);
6106 if (err)
6107 return err;
6109 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6110 check_cancelled, NULL);
6111 if (err)
6112 goto done;
6113 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6114 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6115 check_cancelled, NULL);
6116 if (err) {
6117 if (err->code == GOT_ERR_ITER_COMPLETED) {
6118 err = got_error_msg(GOT_ERR_ANCESTRY,
6119 "ran out of commits to rebase before "
6120 "youngest common ancestor commit has "
6121 "been reached?!?");
6123 goto done;
6124 } else {
6125 err = check_path_prefix(parent_id, commit_id,
6126 path_prefix, path_prefix_errcode, repo);
6127 if (err)
6128 goto done;
6130 err = got_object_qid_alloc(&qid, commit_id);
6131 if (err)
6132 goto done;
6133 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6134 commit_id = parent_id;
6137 done:
6138 got_commit_graph_close(graph);
6139 return err;
6142 static const struct got_error *
6143 cmd_rebase(int argc, char *argv[])
6145 const struct got_error *error = NULL;
6146 struct got_worktree *worktree = NULL;
6147 struct got_repository *repo = NULL;
6148 struct got_fileindex *fileindex = NULL;
6149 char *cwd = NULL;
6150 struct got_reference *branch = NULL;
6151 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6152 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6153 struct got_object_id *resume_commit_id = NULL;
6154 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6155 struct got_commit_object *commit = NULL;
6156 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6157 int histedit_in_progress = 0;
6158 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6159 struct got_object_id_queue commits;
6160 struct got_pathlist_head merged_paths;
6161 const struct got_object_id_queue *parent_ids;
6162 struct got_object_qid *qid, *pid;
6164 SIMPLEQ_INIT(&commits);
6165 TAILQ_INIT(&merged_paths);
6167 while ((ch = getopt(argc, argv, "ac")) != -1) {
6168 switch (ch) {
6169 case 'a':
6170 abort_rebase = 1;
6171 break;
6172 case 'c':
6173 continue_rebase = 1;
6174 break;
6175 default:
6176 usage_rebase();
6177 /* NOTREACHED */
6181 argc -= optind;
6182 argv += optind;
6184 #ifndef PROFILE
6185 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6186 "unveil", NULL) == -1)
6187 err(1, "pledge");
6188 #endif
6189 if (abort_rebase && continue_rebase)
6190 usage_rebase();
6191 else if (abort_rebase || continue_rebase) {
6192 if (argc != 0)
6193 usage_rebase();
6194 } else if (argc != 1)
6195 usage_rebase();
6197 cwd = getcwd(NULL, 0);
6198 if (cwd == NULL) {
6199 error = got_error_from_errno("getcwd");
6200 goto done;
6202 error = got_worktree_open(&worktree, cwd);
6203 if (error)
6204 goto done;
6206 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6207 NULL);
6208 if (error != NULL)
6209 goto done;
6211 error = apply_unveil(got_repo_get_path(repo), 0,
6212 got_worktree_get_root_path(worktree));
6213 if (error)
6214 goto done;
6216 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6217 worktree);
6218 if (error)
6219 goto done;
6220 if (histedit_in_progress) {
6221 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6222 goto done;
6225 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6226 if (error)
6227 goto done;
6229 if (abort_rebase) {
6230 int did_something;
6231 if (!rebase_in_progress) {
6232 error = got_error(GOT_ERR_NOT_REBASING);
6233 goto done;
6235 error = got_worktree_rebase_continue(&resume_commit_id,
6236 &new_base_branch, &tmp_branch, &branch, &fileindex,
6237 worktree, repo);
6238 if (error)
6239 goto done;
6240 printf("Switching work tree to %s\n",
6241 got_ref_get_symref_target(new_base_branch));
6242 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6243 new_base_branch, update_progress, &did_something);
6244 if (error)
6245 goto done;
6246 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6247 goto done; /* nothing else to do */
6250 if (continue_rebase) {
6251 if (!rebase_in_progress) {
6252 error = got_error(GOT_ERR_NOT_REBASING);
6253 goto done;
6255 error = got_worktree_rebase_continue(&resume_commit_id,
6256 &new_base_branch, &tmp_branch, &branch, &fileindex,
6257 worktree, repo);
6258 if (error)
6259 goto done;
6261 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6262 resume_commit_id, repo);
6263 if (error)
6264 goto done;
6266 yca_id = got_object_id_dup(resume_commit_id);
6267 if (yca_id == NULL) {
6268 error = got_error_from_errno("got_object_id_dup");
6269 goto done;
6271 } else {
6272 error = got_ref_open(&branch, repo, argv[0], 0);
6273 if (error != NULL)
6274 goto done;
6277 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6278 if (error)
6279 goto done;
6281 if (!continue_rebase) {
6282 struct got_object_id *base_commit_id;
6284 base_commit_id = got_worktree_get_base_commit_id(worktree);
6285 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6286 base_commit_id, branch_head_commit_id, repo,
6287 check_cancelled, NULL);
6288 if (error)
6289 goto done;
6290 if (yca_id == NULL) {
6291 error = got_error_msg(GOT_ERR_ANCESTRY,
6292 "specified branch shares no common ancestry "
6293 "with work tree's branch");
6294 goto done;
6297 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6298 if (error) {
6299 if (error->code != GOT_ERR_ANCESTRY)
6300 goto done;
6301 error = NULL;
6302 } else {
6303 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6304 "specified branch resolves to a commit which "
6305 "is already contained in work tree's branch");
6306 goto done;
6308 error = got_worktree_rebase_prepare(&new_base_branch,
6309 &tmp_branch, &fileindex, worktree, branch, repo);
6310 if (error)
6311 goto done;
6314 commit_id = branch_head_commit_id;
6315 error = got_object_open_as_commit(&commit, repo, commit_id);
6316 if (error)
6317 goto done;
6319 parent_ids = got_object_commit_get_parent_ids(commit);
6320 pid = SIMPLEQ_FIRST(parent_ids);
6321 if (pid == NULL) {
6322 if (!continue_rebase) {
6323 int did_something;
6324 error = got_worktree_rebase_abort(worktree, fileindex,
6325 repo, new_base_branch, update_progress,
6326 &did_something);
6327 if (error)
6328 goto done;
6329 printf("Rebase of %s aborted\n",
6330 got_ref_get_name(branch));
6332 error = got_error(GOT_ERR_EMPTY_REBASE);
6333 goto done;
6335 error = collect_commits(&commits, commit_id, pid->id,
6336 yca_id, got_worktree_get_path_prefix(worktree),
6337 GOT_ERR_REBASE_PATH, repo);
6338 got_object_commit_close(commit);
6339 commit = NULL;
6340 if (error)
6341 goto done;
6343 if (SIMPLEQ_EMPTY(&commits)) {
6344 if (continue_rebase) {
6345 error = rebase_complete(worktree, fileindex,
6346 branch, new_base_branch, tmp_branch, repo);
6347 goto done;
6348 } else {
6349 /* Fast-forward the reference of the branch. */
6350 struct got_object_id *new_head_commit_id;
6351 char *id_str;
6352 error = got_ref_resolve(&new_head_commit_id, repo,
6353 new_base_branch);
6354 if (error)
6355 goto done;
6356 error = got_object_id_str(&id_str, new_head_commit_id);
6357 printf("Forwarding %s to commit %s\n",
6358 got_ref_get_name(branch), id_str);
6359 free(id_str);
6360 error = got_ref_change_ref(branch,
6361 new_head_commit_id);
6362 if (error)
6363 goto done;
6367 pid = NULL;
6368 SIMPLEQ_FOREACH(qid, &commits, entry) {
6369 commit_id = qid->id;
6370 parent_id = pid ? pid->id : yca_id;
6371 pid = qid;
6373 error = got_worktree_rebase_merge_files(&merged_paths,
6374 worktree, fileindex, parent_id, commit_id, repo,
6375 rebase_progress, &rebase_status, check_cancelled, NULL);
6376 if (error)
6377 goto done;
6379 if (rebase_status == GOT_STATUS_CONFLICT) {
6380 error = show_rebase_merge_conflict(qid->id, repo);
6381 if (error)
6382 goto done;
6383 got_worktree_rebase_pathlist_free(&merged_paths);
6384 break;
6387 error = rebase_commit(&merged_paths, worktree, fileindex,
6388 tmp_branch, commit_id, repo);
6389 got_worktree_rebase_pathlist_free(&merged_paths);
6390 if (error)
6391 goto done;
6394 if (rebase_status == GOT_STATUS_CONFLICT) {
6395 error = got_worktree_rebase_postpone(worktree, fileindex);
6396 if (error)
6397 goto done;
6398 error = got_error_msg(GOT_ERR_CONFLICTS,
6399 "conflicts must be resolved before rebasing can continue");
6400 } else
6401 error = rebase_complete(worktree, fileindex, branch,
6402 new_base_branch, tmp_branch, repo);
6403 done:
6404 got_object_id_queue_free(&commits);
6405 free(branch_head_commit_id);
6406 free(resume_commit_id);
6407 free(yca_id);
6408 if (commit)
6409 got_object_commit_close(commit);
6410 if (branch)
6411 got_ref_close(branch);
6412 if (new_base_branch)
6413 got_ref_close(new_base_branch);
6414 if (tmp_branch)
6415 got_ref_close(tmp_branch);
6416 if (worktree)
6417 got_worktree_close(worktree);
6418 if (repo)
6419 got_repo_close(repo);
6420 return error;
6423 __dead static void
6424 usage_histedit(void)
6426 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6427 getprogname());
6428 exit(1);
6431 #define GOT_HISTEDIT_PICK 'p'
6432 #define GOT_HISTEDIT_EDIT 'e'
6433 #define GOT_HISTEDIT_FOLD 'f'
6434 #define GOT_HISTEDIT_DROP 'd'
6435 #define GOT_HISTEDIT_MESG 'm'
6437 static struct got_histedit_cmd {
6438 unsigned char code;
6439 const char *name;
6440 const char *desc;
6441 } got_histedit_cmds[] = {
6442 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6443 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6444 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6445 "be used" },
6446 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6447 { GOT_HISTEDIT_MESG, "mesg",
6448 "single-line log message for commit above (open editor if empty)" },
6451 struct got_histedit_list_entry {
6452 TAILQ_ENTRY(got_histedit_list_entry) entry;
6453 struct got_object_id *commit_id;
6454 const struct got_histedit_cmd *cmd;
6455 char *logmsg;
6457 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6459 static const struct got_error *
6460 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6461 FILE *f, struct got_repository *repo)
6463 const struct got_error *err = NULL;
6464 char *logmsg = NULL, *id_str = NULL;
6465 struct got_commit_object *commit = NULL;
6466 int n;
6468 err = got_object_open_as_commit(&commit, repo, commit_id);
6469 if (err)
6470 goto done;
6472 err = get_short_logmsg(&logmsg, 34, commit);
6473 if (err)
6474 goto done;
6476 err = got_object_id_str(&id_str, commit_id);
6477 if (err)
6478 goto done;
6480 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6481 if (n < 0)
6482 err = got_ferror(f, GOT_ERR_IO);
6483 done:
6484 if (commit)
6485 got_object_commit_close(commit);
6486 free(id_str);
6487 free(logmsg);
6488 return err;
6491 static const struct got_error *
6492 histedit_write_commit_list(struct got_object_id_queue *commits,
6493 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6495 const struct got_error *err = NULL;
6496 struct got_object_qid *qid;
6498 if (SIMPLEQ_EMPTY(commits))
6499 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6501 SIMPLEQ_FOREACH(qid, commits, entry) {
6502 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6503 f, repo);
6504 if (err)
6505 break;
6506 if (edit_logmsg_only) {
6507 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6508 if (n < 0) {
6509 err = got_ferror(f, GOT_ERR_IO);
6510 break;
6515 return err;
6518 static const struct got_error *
6519 write_cmd_list(FILE *f, const char *branch_name,
6520 struct got_object_id_queue *commits)
6522 const struct got_error *err = NULL;
6523 int n, i;
6524 char *id_str;
6525 struct got_object_qid *qid;
6527 qid = SIMPLEQ_FIRST(commits);
6528 err = got_object_id_str(&id_str, qid->id);
6529 if (err)
6530 return err;
6532 n = fprintf(f,
6533 "# Editing the history of branch '%s' starting at\n"
6534 "# commit %s\n"
6535 "# Commits will be processed in order from top to "
6536 "bottom of this file.\n", branch_name, id_str);
6537 if (n < 0) {
6538 err = got_ferror(f, GOT_ERR_IO);
6539 goto done;
6542 n = fprintf(f, "# Available histedit commands:\n");
6543 if (n < 0) {
6544 err = got_ferror(f, GOT_ERR_IO);
6545 goto done;
6548 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6549 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6550 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6551 cmd->desc);
6552 if (n < 0) {
6553 err = got_ferror(f, GOT_ERR_IO);
6554 break;
6557 done:
6558 free(id_str);
6559 return err;
6562 static const struct got_error *
6563 histedit_syntax_error(int lineno)
6565 static char msg[42];
6566 int ret;
6568 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6569 lineno);
6570 if (ret == -1 || ret >= sizeof(msg))
6571 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6573 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6576 static const struct got_error *
6577 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6578 char *logmsg, struct got_repository *repo)
6580 const struct got_error *err;
6581 struct got_commit_object *folded_commit = NULL;
6582 char *id_str, *folded_logmsg = NULL;
6584 err = got_object_id_str(&id_str, hle->commit_id);
6585 if (err)
6586 return err;
6588 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6589 if (err)
6590 goto done;
6592 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6593 if (err)
6594 goto done;
6595 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6596 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6597 folded_logmsg) == -1) {
6598 err = got_error_from_errno("asprintf");
6600 done:
6601 if (folded_commit)
6602 got_object_commit_close(folded_commit);
6603 free(id_str);
6604 free(folded_logmsg);
6605 return err;
6608 static struct got_histedit_list_entry *
6609 get_folded_commits(struct got_histedit_list_entry *hle)
6611 struct got_histedit_list_entry *prev, *folded = NULL;
6613 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6614 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6615 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6616 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6617 folded = prev;
6618 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6621 return folded;
6624 static const struct got_error *
6625 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6626 struct got_repository *repo)
6628 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6629 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6630 const struct got_error *err = NULL;
6631 struct got_commit_object *commit = NULL;
6632 int fd;
6633 struct got_histedit_list_entry *folded = NULL;
6635 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6636 if (err)
6637 return err;
6639 folded = get_folded_commits(hle);
6640 if (folded) {
6641 while (folded != hle) {
6642 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6643 folded = TAILQ_NEXT(folded, entry);
6644 continue;
6646 err = append_folded_commit_msg(&new_msg, folded,
6647 logmsg, repo);
6648 if (err)
6649 goto done;
6650 free(logmsg);
6651 logmsg = new_msg;
6652 folded = TAILQ_NEXT(folded, entry);
6656 err = got_object_id_str(&id_str, hle->commit_id);
6657 if (err)
6658 goto done;
6659 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6660 if (err)
6661 goto done;
6662 if (asprintf(&new_msg,
6663 "%s\n# original log message of commit %s: %s",
6664 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6665 err = got_error_from_errno("asprintf");
6666 goto done;
6668 free(logmsg);
6669 logmsg = new_msg;
6671 err = got_object_id_str(&id_str, hle->commit_id);
6672 if (err)
6673 goto done;
6675 err = got_opentemp_named_fd(&logmsg_path, &fd,
6676 GOT_TMPDIR_STR "/got-logmsg");
6677 if (err)
6678 goto done;
6680 dprintf(fd, logmsg);
6681 close(fd);
6683 err = get_editor(&editor);
6684 if (err)
6685 goto done;
6687 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6688 if (err) {
6689 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6690 goto done;
6691 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6693 done:
6694 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6695 err = got_error_from_errno2("unlink", logmsg_path);
6696 free(logmsg_path);
6697 free(logmsg);
6698 free(orig_logmsg);
6699 free(editor);
6700 if (commit)
6701 got_object_commit_close(commit);
6702 return err;
6705 static const struct got_error *
6706 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6707 FILE *f, struct got_repository *repo)
6709 const struct got_error *err = NULL;
6710 char *line = NULL, *p, *end;
6711 size_t size;
6712 ssize_t len;
6713 int lineno = 0, i;
6714 const struct got_histedit_cmd *cmd;
6715 struct got_object_id *commit_id = NULL;
6716 struct got_histedit_list_entry *hle = NULL;
6718 for (;;) {
6719 len = getline(&line, &size, f);
6720 if (len == -1) {
6721 const struct got_error *getline_err;
6722 if (feof(f))
6723 break;
6724 getline_err = got_error_from_errno("getline");
6725 err = got_ferror(f, getline_err->code);
6726 break;
6728 lineno++;
6729 p = line;
6730 while (isspace((unsigned char)p[0]))
6731 p++;
6732 if (p[0] == '#' || p[0] == '\0') {
6733 free(line);
6734 line = NULL;
6735 continue;
6737 cmd = NULL;
6738 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6739 cmd = &got_histedit_cmds[i];
6740 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6741 isspace((unsigned char)p[strlen(cmd->name)])) {
6742 p += strlen(cmd->name);
6743 break;
6745 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6746 p++;
6747 break;
6750 if (i == nitems(got_histedit_cmds)) {
6751 err = histedit_syntax_error(lineno);
6752 break;
6754 while (isspace((unsigned char)p[0]))
6755 p++;
6756 if (cmd->code == GOT_HISTEDIT_MESG) {
6757 if (hle == NULL || hle->logmsg != NULL) {
6758 err = got_error(GOT_ERR_HISTEDIT_CMD);
6759 break;
6761 if (p[0] == '\0') {
6762 err = histedit_edit_logmsg(hle, repo);
6763 if (err)
6764 break;
6765 } else {
6766 hle->logmsg = strdup(p);
6767 if (hle->logmsg == NULL) {
6768 err = got_error_from_errno("strdup");
6769 break;
6772 free(line);
6773 line = NULL;
6774 continue;
6775 } else {
6776 end = p;
6777 while (end[0] && !isspace((unsigned char)end[0]))
6778 end++;
6779 *end = '\0';
6781 err = got_object_resolve_id_str(&commit_id, repo, p);
6782 if (err) {
6783 /* override error code */
6784 err = histedit_syntax_error(lineno);
6785 break;
6788 hle = malloc(sizeof(*hle));
6789 if (hle == NULL) {
6790 err = got_error_from_errno("malloc");
6791 break;
6793 hle->cmd = cmd;
6794 hle->commit_id = commit_id;
6795 hle->logmsg = NULL;
6796 commit_id = NULL;
6797 free(line);
6798 line = NULL;
6799 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6802 free(line);
6803 free(commit_id);
6804 return err;
6807 static const struct got_error *
6808 histedit_check_script(struct got_histedit_list *histedit_cmds,
6809 struct got_object_id_queue *commits, struct got_repository *repo)
6811 const struct got_error *err = NULL;
6812 struct got_object_qid *qid;
6813 struct got_histedit_list_entry *hle;
6814 static char msg[92];
6815 char *id_str;
6817 if (TAILQ_EMPTY(histedit_cmds))
6818 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6819 "histedit script contains no commands");
6820 if (SIMPLEQ_EMPTY(commits))
6821 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6823 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6824 struct got_histedit_list_entry *hle2;
6825 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6826 if (hle == hle2)
6827 continue;
6828 if (got_object_id_cmp(hle->commit_id,
6829 hle2->commit_id) != 0)
6830 continue;
6831 err = got_object_id_str(&id_str, hle->commit_id);
6832 if (err)
6833 return err;
6834 snprintf(msg, sizeof(msg), "commit %s is listed "
6835 "more than once in histedit script", id_str);
6836 free(id_str);
6837 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6841 SIMPLEQ_FOREACH(qid, commits, entry) {
6842 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6843 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6844 break;
6846 if (hle == NULL) {
6847 err = got_object_id_str(&id_str, qid->id);
6848 if (err)
6849 return err;
6850 snprintf(msg, sizeof(msg),
6851 "commit %s missing from histedit script", id_str);
6852 free(id_str);
6853 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6857 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6858 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6859 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6860 "last commit in histedit script cannot be folded");
6862 return NULL;
6865 static const struct got_error *
6866 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6867 const char *path, struct got_object_id_queue *commits,
6868 struct got_repository *repo)
6870 const struct got_error *err = NULL;
6871 char *editor;
6872 FILE *f = NULL;
6874 err = get_editor(&editor);
6875 if (err)
6876 return err;
6878 if (spawn_editor(editor, path) == -1) {
6879 err = got_error_from_errno("failed spawning editor");
6880 goto done;
6883 f = fopen(path, "r");
6884 if (f == NULL) {
6885 err = got_error_from_errno("fopen");
6886 goto done;
6888 err = histedit_parse_list(histedit_cmds, f, repo);
6889 if (err)
6890 goto done;
6892 err = histedit_check_script(histedit_cmds, commits, repo);
6893 done:
6894 if (f && fclose(f) != 0 && err == NULL)
6895 err = got_error_from_errno("fclose");
6896 free(editor);
6897 return err;
6900 static const struct got_error *
6901 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6902 struct got_object_id_queue *, const char *, const char *,
6903 struct got_repository *);
6905 static const struct got_error *
6906 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6907 struct got_object_id_queue *commits, const char *branch_name,
6908 int edit_logmsg_only, struct got_repository *repo)
6910 const struct got_error *err;
6911 FILE *f = NULL;
6912 char *path = NULL;
6914 err = got_opentemp_named(&path, &f, "got-histedit");
6915 if (err)
6916 return err;
6918 err = write_cmd_list(f, branch_name, commits);
6919 if (err)
6920 goto done;
6922 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6923 if (err)
6924 goto done;
6926 if (edit_logmsg_only) {
6927 rewind(f);
6928 err = histedit_parse_list(histedit_cmds, f, repo);
6929 } else {
6930 if (fclose(f) != 0) {
6931 err = got_error_from_errno("fclose");
6932 goto done;
6934 f = NULL;
6935 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6936 if (err) {
6937 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6938 err->code != GOT_ERR_HISTEDIT_CMD)
6939 goto done;
6940 err = histedit_edit_list_retry(histedit_cmds, err,
6941 commits, path, branch_name, repo);
6944 done:
6945 if (f && fclose(f) != 0 && err == NULL)
6946 err = got_error_from_errno("fclose");
6947 if (path && unlink(path) != 0 && err == NULL)
6948 err = got_error_from_errno2("unlink", path);
6949 free(path);
6950 return err;
6953 static const struct got_error *
6954 histedit_save_list(struct got_histedit_list *histedit_cmds,
6955 struct got_worktree *worktree, struct got_repository *repo)
6957 const struct got_error *err = NULL;
6958 char *path = NULL;
6959 FILE *f = NULL;
6960 struct got_histedit_list_entry *hle;
6961 struct got_commit_object *commit = NULL;
6963 err = got_worktree_get_histedit_script_path(&path, worktree);
6964 if (err)
6965 return err;
6967 f = fopen(path, "w");
6968 if (f == NULL) {
6969 err = got_error_from_errno2("fopen", path);
6970 goto done;
6972 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6973 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6974 repo);
6975 if (err)
6976 break;
6978 if (hle->logmsg) {
6979 int n = fprintf(f, "%c %s\n",
6980 GOT_HISTEDIT_MESG, hle->logmsg);
6981 if (n < 0) {
6982 err = got_ferror(f, GOT_ERR_IO);
6983 break;
6987 done:
6988 if (f && fclose(f) != 0 && err == NULL)
6989 err = got_error_from_errno("fclose");
6990 free(path);
6991 if (commit)
6992 got_object_commit_close(commit);
6993 return err;
6996 void
6997 histedit_free_list(struct got_histedit_list *histedit_cmds)
6999 struct got_histedit_list_entry *hle;
7001 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7002 TAILQ_REMOVE(histedit_cmds, hle, entry);
7003 free(hle);
7007 static const struct got_error *
7008 histedit_load_list(struct got_histedit_list *histedit_cmds,
7009 const char *path, struct got_repository *repo)
7011 const struct got_error *err = NULL;
7012 FILE *f = NULL;
7014 f = fopen(path, "r");
7015 if (f == NULL) {
7016 err = got_error_from_errno2("fopen", path);
7017 goto done;
7020 err = histedit_parse_list(histedit_cmds, f, repo);
7021 done:
7022 if (f && fclose(f) != 0 && err == NULL)
7023 err = got_error_from_errno("fclose");
7024 return err;
7027 static const struct got_error *
7028 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7029 const struct got_error *edit_err, struct got_object_id_queue *commits,
7030 const char *path, const char *branch_name, struct got_repository *repo)
7032 const struct got_error *err = NULL, *prev_err = edit_err;
7033 int resp = ' ';
7035 while (resp != 'c' && resp != 'r' && resp != 'a') {
7036 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7037 "or (a)bort: ", getprogname(), prev_err->msg);
7038 resp = getchar();
7039 if (resp == '\n')
7040 resp = getchar();
7041 if (resp == 'c') {
7042 histedit_free_list(histedit_cmds);
7043 err = histedit_run_editor(histedit_cmds, path, commits,
7044 repo);
7045 if (err) {
7046 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7047 err->code != GOT_ERR_HISTEDIT_CMD)
7048 break;
7049 prev_err = err;
7050 resp = ' ';
7051 continue;
7053 break;
7054 } else if (resp == 'r') {
7055 histedit_free_list(histedit_cmds);
7056 err = histedit_edit_script(histedit_cmds,
7057 commits, branch_name, 0, repo);
7058 if (err) {
7059 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7060 err->code != GOT_ERR_HISTEDIT_CMD)
7061 break;
7062 prev_err = err;
7063 resp = ' ';
7064 continue;
7066 break;
7067 } else if (resp == 'a') {
7068 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7069 break;
7070 } else
7071 printf("invalid response '%c'\n", resp);
7074 return err;
7077 static const struct got_error *
7078 histedit_complete(struct got_worktree *worktree,
7079 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7080 struct got_reference *branch, struct got_repository *repo)
7082 printf("Switching work tree to %s\n",
7083 got_ref_get_symref_target(branch));
7084 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7085 branch, repo);
7088 static const struct got_error *
7089 show_histedit_progress(struct got_commit_object *commit,
7090 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7092 const struct got_error *err;
7093 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7095 err = got_object_id_str(&old_id_str, hle->commit_id);
7096 if (err)
7097 goto done;
7099 if (new_id) {
7100 err = got_object_id_str(&new_id_str, new_id);
7101 if (err)
7102 goto done;
7105 old_id_str[12] = '\0';
7106 if (new_id_str)
7107 new_id_str[12] = '\0';
7109 if (hle->logmsg) {
7110 logmsg = strdup(hle->logmsg);
7111 if (logmsg == NULL) {
7112 err = got_error_from_errno("strdup");
7113 goto done;
7115 trim_logmsg(logmsg, 42);
7116 } else {
7117 err = get_short_logmsg(&logmsg, 42, commit);
7118 if (err)
7119 goto done;
7122 switch (hle->cmd->code) {
7123 case GOT_HISTEDIT_PICK:
7124 case GOT_HISTEDIT_EDIT:
7125 printf("%s -> %s: %s\n", old_id_str,
7126 new_id_str ? new_id_str : "no-op change", logmsg);
7127 break;
7128 case GOT_HISTEDIT_DROP:
7129 case GOT_HISTEDIT_FOLD:
7130 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7131 logmsg);
7132 break;
7133 default:
7134 break;
7136 done:
7137 free(old_id_str);
7138 free(new_id_str);
7139 return err;
7142 static const struct got_error *
7143 histedit_commit(struct got_pathlist_head *merged_paths,
7144 struct got_worktree *worktree, struct got_fileindex *fileindex,
7145 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7146 struct got_repository *repo)
7148 const struct got_error *err;
7149 struct got_commit_object *commit;
7150 struct got_object_id *new_commit_id;
7152 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7153 && hle->logmsg == NULL) {
7154 err = histedit_edit_logmsg(hle, repo);
7155 if (err)
7156 return err;
7159 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7160 if (err)
7161 return err;
7163 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7164 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7165 hle->logmsg, repo);
7166 if (err) {
7167 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7168 goto done;
7169 err = show_histedit_progress(commit, hle, NULL);
7170 } else {
7171 err = show_histedit_progress(commit, hle, new_commit_id);
7172 free(new_commit_id);
7174 done:
7175 got_object_commit_close(commit);
7176 return err;
7179 static const struct got_error *
7180 histedit_skip_commit(struct got_histedit_list_entry *hle,
7181 struct got_worktree *worktree, struct got_repository *repo)
7183 const struct got_error *error;
7184 struct got_commit_object *commit;
7186 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7187 repo);
7188 if (error)
7189 return error;
7191 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7192 if (error)
7193 return error;
7195 error = show_histedit_progress(commit, hle, NULL);
7196 got_object_commit_close(commit);
7197 return error;
7200 static const struct got_error *
7201 check_local_changes(void *arg, unsigned char status,
7202 unsigned char staged_status, const char *path,
7203 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7204 struct got_object_id *commit_id, int dirfd, const char *de_name)
7206 int *have_local_changes = arg;
7208 switch (status) {
7209 case GOT_STATUS_ADD:
7210 case GOT_STATUS_DELETE:
7211 case GOT_STATUS_MODIFY:
7212 case GOT_STATUS_CONFLICT:
7213 *have_local_changes = 1;
7214 return got_error(GOT_ERR_CANCELLED);
7215 default:
7216 break;
7219 switch (staged_status) {
7220 case GOT_STATUS_ADD:
7221 case GOT_STATUS_DELETE:
7222 case GOT_STATUS_MODIFY:
7223 *have_local_changes = 1;
7224 return got_error(GOT_ERR_CANCELLED);
7225 default:
7226 break;
7229 return NULL;
7232 static const struct got_error *
7233 cmd_histedit(int argc, char *argv[])
7235 const struct got_error *error = NULL;
7236 struct got_worktree *worktree = NULL;
7237 struct got_fileindex *fileindex = NULL;
7238 struct got_repository *repo = NULL;
7239 char *cwd = NULL;
7240 struct got_reference *branch = NULL;
7241 struct got_reference *tmp_branch = NULL;
7242 struct got_object_id *resume_commit_id = NULL;
7243 struct got_object_id *base_commit_id = NULL;
7244 struct got_object_id *head_commit_id = NULL;
7245 struct got_commit_object *commit = NULL;
7246 int ch, rebase_in_progress = 0, did_something;
7247 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7248 int edit_logmsg_only = 0;
7249 const char *edit_script_path = NULL;
7250 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7251 struct got_object_id_queue commits;
7252 struct got_pathlist_head merged_paths;
7253 const struct got_object_id_queue *parent_ids;
7254 struct got_object_qid *pid;
7255 struct got_histedit_list histedit_cmds;
7256 struct got_histedit_list_entry *hle;
7258 SIMPLEQ_INIT(&commits);
7259 TAILQ_INIT(&histedit_cmds);
7260 TAILQ_INIT(&merged_paths);
7262 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7263 switch (ch) {
7264 case 'a':
7265 abort_edit = 1;
7266 break;
7267 case 'c':
7268 continue_edit = 1;
7269 break;
7270 case 'F':
7271 edit_script_path = optarg;
7272 break;
7273 case 'm':
7274 edit_logmsg_only = 1;
7275 break;
7276 default:
7277 usage_histedit();
7278 /* NOTREACHED */
7282 argc -= optind;
7283 argv += optind;
7285 #ifndef PROFILE
7286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7287 "unveil", NULL) == -1)
7288 err(1, "pledge");
7289 #endif
7290 if (abort_edit && continue_edit)
7291 errx(1, "histedit's -a and -c options are mutually exclusive");
7292 if (edit_script_path && edit_logmsg_only)
7293 errx(1, "histedit's -F and -m options are mutually exclusive");
7294 if (abort_edit && edit_logmsg_only)
7295 errx(1, "histedit's -a and -m options are mutually exclusive");
7296 if (continue_edit && edit_logmsg_only)
7297 errx(1, "histedit's -c and -m options are mutually exclusive");
7298 if (argc != 0)
7299 usage_histedit();
7302 * This command cannot apply unveil(2) in all cases because the
7303 * user may choose to run an editor to edit the histedit script
7304 * and to edit individual commit log messages.
7305 * unveil(2) traverses exec(2); if an editor is used we have to
7306 * apply unveil after edit script and log messages have been written.
7307 * XXX TODO: Make use of unveil(2) where possible.
7310 cwd = getcwd(NULL, 0);
7311 if (cwd == NULL) {
7312 error = got_error_from_errno("getcwd");
7313 goto done;
7315 error = got_worktree_open(&worktree, cwd);
7316 if (error)
7317 goto done;
7319 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7320 NULL);
7321 if (error != NULL)
7322 goto done;
7324 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7325 if (error)
7326 goto done;
7327 if (rebase_in_progress) {
7328 error = got_error(GOT_ERR_REBASING);
7329 goto done;
7332 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7333 if (error)
7334 goto done;
7336 if (edit_in_progress && edit_logmsg_only) {
7337 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7338 "histedit operation is in progress in this "
7339 "work tree and must be continued or aborted "
7340 "before the -m option can be used");
7341 goto done;
7344 if (edit_in_progress && abort_edit) {
7345 error = got_worktree_histedit_continue(&resume_commit_id,
7346 &tmp_branch, &branch, &base_commit_id, &fileindex,
7347 worktree, repo);
7348 if (error)
7349 goto done;
7350 printf("Switching work tree to %s\n",
7351 got_ref_get_symref_target(branch));
7352 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7353 branch, base_commit_id, update_progress, &did_something);
7354 if (error)
7355 goto done;
7356 printf("Histedit of %s aborted\n",
7357 got_ref_get_symref_target(branch));
7358 goto done; /* nothing else to do */
7359 } else if (abort_edit) {
7360 error = got_error(GOT_ERR_NOT_HISTEDIT);
7361 goto done;
7364 if (continue_edit) {
7365 char *path;
7367 if (!edit_in_progress) {
7368 error = got_error(GOT_ERR_NOT_HISTEDIT);
7369 goto done;
7372 error = got_worktree_get_histedit_script_path(&path, worktree);
7373 if (error)
7374 goto done;
7376 error = histedit_load_list(&histedit_cmds, path, repo);
7377 free(path);
7378 if (error)
7379 goto done;
7381 error = got_worktree_histedit_continue(&resume_commit_id,
7382 &tmp_branch, &branch, &base_commit_id, &fileindex,
7383 worktree, repo);
7384 if (error)
7385 goto done;
7387 error = got_ref_resolve(&head_commit_id, repo, branch);
7388 if (error)
7389 goto done;
7391 error = got_object_open_as_commit(&commit, repo,
7392 head_commit_id);
7393 if (error)
7394 goto done;
7395 parent_ids = got_object_commit_get_parent_ids(commit);
7396 pid = SIMPLEQ_FIRST(parent_ids);
7397 if (pid == NULL) {
7398 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7399 goto done;
7401 error = collect_commits(&commits, head_commit_id, pid->id,
7402 base_commit_id, got_worktree_get_path_prefix(worktree),
7403 GOT_ERR_HISTEDIT_PATH, repo);
7404 got_object_commit_close(commit);
7405 commit = NULL;
7406 if (error)
7407 goto done;
7408 } else {
7409 if (edit_in_progress) {
7410 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7411 goto done;
7414 error = got_ref_open(&branch, repo,
7415 got_worktree_get_head_ref_name(worktree), 0);
7416 if (error != NULL)
7417 goto done;
7419 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7420 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7421 "will not edit commit history of a branch outside "
7422 "the \"refs/heads/\" reference namespace");
7423 goto done;
7426 error = got_ref_resolve(&head_commit_id, repo, branch);
7427 got_ref_close(branch);
7428 branch = NULL;
7429 if (error)
7430 goto done;
7432 error = got_object_open_as_commit(&commit, repo,
7433 head_commit_id);
7434 if (error)
7435 goto done;
7436 parent_ids = got_object_commit_get_parent_ids(commit);
7437 pid = SIMPLEQ_FIRST(parent_ids);
7438 if (pid == NULL) {
7439 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7440 goto done;
7442 error = collect_commits(&commits, head_commit_id, pid->id,
7443 got_worktree_get_base_commit_id(worktree),
7444 got_worktree_get_path_prefix(worktree),
7445 GOT_ERR_HISTEDIT_PATH, repo);
7446 got_object_commit_close(commit);
7447 commit = NULL;
7448 if (error)
7449 goto done;
7451 if (SIMPLEQ_EMPTY(&commits)) {
7452 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7453 goto done;
7456 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7457 &base_commit_id, &fileindex, worktree, repo);
7458 if (error)
7459 goto done;
7461 if (edit_script_path) {
7462 error = histedit_load_list(&histedit_cmds,
7463 edit_script_path, repo);
7464 if (error) {
7465 got_worktree_histedit_abort(worktree, fileindex,
7466 repo, branch, base_commit_id,
7467 update_progress, &did_something);
7468 goto done;
7470 } else {
7471 const char *branch_name;
7472 branch_name = got_ref_get_symref_target(branch);
7473 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7474 branch_name += 11;
7475 error = histedit_edit_script(&histedit_cmds, &commits,
7476 branch_name, edit_logmsg_only, repo);
7477 if (error) {
7478 got_worktree_histedit_abort(worktree, fileindex,
7479 repo, branch, base_commit_id,
7480 update_progress, &did_something);
7481 goto done;
7486 error = histedit_save_list(&histedit_cmds, worktree,
7487 repo);
7488 if (error) {
7489 got_worktree_histedit_abort(worktree, fileindex,
7490 repo, branch, base_commit_id,
7491 update_progress, &did_something);
7492 goto done;
7497 error = histedit_check_script(&histedit_cmds, &commits, repo);
7498 if (error)
7499 goto done;
7501 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7502 if (resume_commit_id) {
7503 if (got_object_id_cmp(hle->commit_id,
7504 resume_commit_id) != 0)
7505 continue;
7507 resume_commit_id = NULL;
7508 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7509 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7510 error = histedit_skip_commit(hle, worktree,
7511 repo);
7512 if (error)
7513 goto done;
7514 } else {
7515 struct got_pathlist_head paths;
7516 int have_changes = 0;
7518 TAILQ_INIT(&paths);
7519 error = got_pathlist_append(&paths, "", NULL);
7520 if (error)
7521 goto done;
7522 error = got_worktree_status(worktree, &paths,
7523 repo, check_local_changes, &have_changes,
7524 check_cancelled, NULL);
7525 got_pathlist_free(&paths);
7526 if (error) {
7527 if (error->code != GOT_ERR_CANCELLED)
7528 goto done;
7529 if (sigint_received || sigpipe_received)
7530 goto done;
7532 if (have_changes) {
7533 error = histedit_commit(NULL, worktree,
7534 fileindex, tmp_branch, hle, repo);
7535 if (error)
7536 goto done;
7537 } else {
7538 error = got_object_open_as_commit(
7539 &commit, repo, hle->commit_id);
7540 if (error)
7541 goto done;
7542 error = show_histedit_progress(commit,
7543 hle, NULL);
7544 got_object_commit_close(commit);
7545 commit = NULL;
7546 if (error)
7547 goto done;
7550 continue;
7553 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7554 error = histedit_skip_commit(hle, worktree, repo);
7555 if (error)
7556 goto done;
7557 continue;
7560 error = got_object_open_as_commit(&commit, repo,
7561 hle->commit_id);
7562 if (error)
7563 goto done;
7564 parent_ids = got_object_commit_get_parent_ids(commit);
7565 pid = SIMPLEQ_FIRST(parent_ids);
7567 error = got_worktree_histedit_merge_files(&merged_paths,
7568 worktree, fileindex, pid->id, hle->commit_id, repo,
7569 rebase_progress, &rebase_status, check_cancelled, NULL);
7570 if (error)
7571 goto done;
7572 got_object_commit_close(commit);
7573 commit = NULL;
7575 if (rebase_status == GOT_STATUS_CONFLICT) {
7576 error = show_rebase_merge_conflict(hle->commit_id,
7577 repo);
7578 if (error)
7579 goto done;
7580 got_worktree_rebase_pathlist_free(&merged_paths);
7581 break;
7584 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7585 char *id_str;
7586 error = got_object_id_str(&id_str, hle->commit_id);
7587 if (error)
7588 goto done;
7589 printf("Stopping histedit for amending commit %s\n",
7590 id_str);
7591 free(id_str);
7592 got_worktree_rebase_pathlist_free(&merged_paths);
7593 error = got_worktree_histedit_postpone(worktree,
7594 fileindex);
7595 goto done;
7598 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7599 error = histedit_skip_commit(hle, worktree, repo);
7600 if (error)
7601 goto done;
7602 continue;
7605 error = histedit_commit(&merged_paths, worktree, fileindex,
7606 tmp_branch, hle, repo);
7607 got_worktree_rebase_pathlist_free(&merged_paths);
7608 if (error)
7609 goto done;
7612 if (rebase_status == GOT_STATUS_CONFLICT) {
7613 error = got_worktree_histedit_postpone(worktree, fileindex);
7614 if (error)
7615 goto done;
7616 error = got_error_msg(GOT_ERR_CONFLICTS,
7617 "conflicts must be resolved before histedit can continue");
7618 } else
7619 error = histedit_complete(worktree, fileindex, tmp_branch,
7620 branch, repo);
7621 done:
7622 got_object_id_queue_free(&commits);
7623 histedit_free_list(&histedit_cmds);
7624 free(head_commit_id);
7625 free(base_commit_id);
7626 free(resume_commit_id);
7627 if (commit)
7628 got_object_commit_close(commit);
7629 if (branch)
7630 got_ref_close(branch);
7631 if (tmp_branch)
7632 got_ref_close(tmp_branch);
7633 if (worktree)
7634 got_worktree_close(worktree);
7635 if (repo)
7636 got_repo_close(repo);
7637 return error;
7640 __dead static void
7641 usage_integrate(void)
7643 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7644 exit(1);
7647 static const struct got_error *
7648 cmd_integrate(int argc, char *argv[])
7650 const struct got_error *error = NULL;
7651 struct got_repository *repo = NULL;
7652 struct got_worktree *worktree = NULL;
7653 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7654 const char *branch_arg = NULL;
7655 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7656 struct got_fileindex *fileindex = NULL;
7657 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7658 int ch, did_something = 0;
7660 while ((ch = getopt(argc, argv, "")) != -1) {
7661 switch (ch) {
7662 default:
7663 usage_integrate();
7664 /* NOTREACHED */
7668 argc -= optind;
7669 argv += optind;
7671 if (argc != 1)
7672 usage_integrate();
7673 branch_arg = argv[0];
7675 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7676 "unveil", NULL) == -1)
7677 err(1, "pledge");
7679 cwd = getcwd(NULL, 0);
7680 if (cwd == NULL) {
7681 error = got_error_from_errno("getcwd");
7682 goto done;
7685 error = got_worktree_open(&worktree, cwd);
7686 if (error)
7687 goto done;
7689 error = check_rebase_or_histedit_in_progress(worktree);
7690 if (error)
7691 goto done;
7693 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7694 NULL);
7695 if (error != NULL)
7696 goto done;
7698 error = apply_unveil(got_repo_get_path(repo), 0,
7699 got_worktree_get_root_path(worktree));
7700 if (error)
7701 goto done;
7703 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7704 error = got_error_from_errno("asprintf");
7705 goto done;
7708 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7709 &base_branch_ref, worktree, refname, repo);
7710 if (error)
7711 goto done;
7713 refname = strdup(got_ref_get_name(branch_ref));
7714 if (refname == NULL) {
7715 error = got_error_from_errno("strdup");
7716 got_worktree_integrate_abort(worktree, fileindex, repo,
7717 branch_ref, base_branch_ref);
7718 goto done;
7720 base_refname = strdup(got_ref_get_name(base_branch_ref));
7721 if (base_refname == NULL) {
7722 error = got_error_from_errno("strdup");
7723 got_worktree_integrate_abort(worktree, fileindex, repo,
7724 branch_ref, base_branch_ref);
7725 goto done;
7728 error = got_ref_resolve(&commit_id, repo, branch_ref);
7729 if (error)
7730 goto done;
7732 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7733 if (error)
7734 goto done;
7736 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7737 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7738 "specified branch has already been integrated");
7739 got_worktree_integrate_abort(worktree, fileindex, repo,
7740 branch_ref, base_branch_ref);
7741 goto done;
7744 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7745 if (error) {
7746 if (error->code == GOT_ERR_ANCESTRY)
7747 error = got_error(GOT_ERR_REBASE_REQUIRED);
7748 got_worktree_integrate_abort(worktree, fileindex, repo,
7749 branch_ref, base_branch_ref);
7750 goto done;
7753 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7754 branch_ref, base_branch_ref, update_progress, &did_something,
7755 check_cancelled, NULL);
7756 if (error)
7757 goto done;
7759 printf("Integrated %s into %s\n", refname, base_refname);
7760 done:
7761 if (repo)
7762 got_repo_close(repo);
7763 if (worktree)
7764 got_worktree_close(worktree);
7765 free(cwd);
7766 free(base_commit_id);
7767 free(commit_id);
7768 free(refname);
7769 free(base_refname);
7770 return error;
7773 __dead static void
7774 usage_stage(void)
7776 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7777 "[file-path ...]\n",
7778 getprogname());
7779 exit(1);
7782 static const struct got_error *
7783 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7784 const char *path, struct got_object_id *blob_id,
7785 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7786 int dirfd, const char *de_name)
7788 const struct got_error *err = NULL;
7789 char *id_str = NULL;
7791 if (staged_status != GOT_STATUS_ADD &&
7792 staged_status != GOT_STATUS_MODIFY &&
7793 staged_status != GOT_STATUS_DELETE)
7794 return NULL;
7796 if (staged_status == GOT_STATUS_ADD ||
7797 staged_status == GOT_STATUS_MODIFY)
7798 err = got_object_id_str(&id_str, staged_blob_id);
7799 else
7800 err = got_object_id_str(&id_str, blob_id);
7801 if (err)
7802 return err;
7804 printf("%s %c %s\n", id_str, staged_status, path);
7805 free(id_str);
7806 return NULL;
7809 static const struct got_error *
7810 cmd_stage(int argc, char *argv[])
7812 const struct got_error *error = NULL;
7813 struct got_repository *repo = NULL;
7814 struct got_worktree *worktree = NULL;
7815 char *cwd = NULL;
7816 struct got_pathlist_head paths;
7817 struct got_pathlist_entry *pe;
7818 int ch, list_stage = 0, pflag = 0;
7819 FILE *patch_script_file = NULL;
7820 const char *patch_script_path = NULL;
7821 struct choose_patch_arg cpa;
7823 TAILQ_INIT(&paths);
7825 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7826 switch (ch) {
7827 case 'l':
7828 list_stage = 1;
7829 break;
7830 case 'p':
7831 pflag = 1;
7832 break;
7833 case 'F':
7834 patch_script_path = optarg;
7835 break;
7836 default:
7837 usage_stage();
7838 /* NOTREACHED */
7842 argc -= optind;
7843 argv += optind;
7845 #ifndef PROFILE
7846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7847 "unveil", NULL) == -1)
7848 err(1, "pledge");
7849 #endif
7850 if (list_stage && (pflag || patch_script_path))
7851 errx(1, "-l option cannot be used with other options");
7852 if (patch_script_path && !pflag)
7853 errx(1, "-F option can only be used together with -p option");
7855 cwd = getcwd(NULL, 0);
7856 if (cwd == NULL) {
7857 error = got_error_from_errno("getcwd");
7858 goto done;
7861 error = got_worktree_open(&worktree, cwd);
7862 if (error)
7863 goto done;
7865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7866 NULL);
7867 if (error != NULL)
7868 goto done;
7870 if (patch_script_path) {
7871 patch_script_file = fopen(patch_script_path, "r");
7872 if (patch_script_file == NULL) {
7873 error = got_error_from_errno2("fopen",
7874 patch_script_path);
7875 goto done;
7878 error = apply_unveil(got_repo_get_path(repo), 0,
7879 got_worktree_get_root_path(worktree));
7880 if (error)
7881 goto done;
7883 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7884 if (error)
7885 goto done;
7887 if (list_stage)
7888 error = got_worktree_status(worktree, &paths, repo,
7889 print_stage, NULL, check_cancelled, NULL);
7890 else {
7891 cpa.patch_script_file = patch_script_file;
7892 cpa.action = "stage";
7893 error = got_worktree_stage(worktree, &paths,
7894 pflag ? NULL : print_status, NULL,
7895 pflag ? choose_patch : NULL, &cpa, repo);
7897 done:
7898 if (patch_script_file && fclose(patch_script_file) == EOF &&
7899 error == NULL)
7900 error = got_error_from_errno2("fclose", patch_script_path);
7901 if (repo)
7902 got_repo_close(repo);
7903 if (worktree)
7904 got_worktree_close(worktree);
7905 TAILQ_FOREACH(pe, &paths, entry)
7906 free((char *)pe->path);
7907 got_pathlist_free(&paths);
7908 free(cwd);
7909 return error;
7912 __dead static void
7913 usage_unstage(void)
7915 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7916 "[file-path ...]\n",
7917 getprogname());
7918 exit(1);
7922 static const struct got_error *
7923 cmd_unstage(int argc, char *argv[])
7925 const struct got_error *error = NULL;
7926 struct got_repository *repo = NULL;
7927 struct got_worktree *worktree = NULL;
7928 char *cwd = NULL;
7929 struct got_pathlist_head paths;
7930 struct got_pathlist_entry *pe;
7931 int ch, did_something = 0, pflag = 0;
7932 FILE *patch_script_file = NULL;
7933 const char *patch_script_path = NULL;
7934 struct choose_patch_arg cpa;
7936 TAILQ_INIT(&paths);
7938 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7939 switch (ch) {
7940 case 'p':
7941 pflag = 1;
7942 break;
7943 case 'F':
7944 patch_script_path = optarg;
7945 break;
7946 default:
7947 usage_unstage();
7948 /* NOTREACHED */
7952 argc -= optind;
7953 argv += optind;
7955 #ifndef PROFILE
7956 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7957 "unveil", NULL) == -1)
7958 err(1, "pledge");
7959 #endif
7960 if (patch_script_path && !pflag)
7961 errx(1, "-F option can only be used together with -p option");
7963 cwd = getcwd(NULL, 0);
7964 if (cwd == NULL) {
7965 error = got_error_from_errno("getcwd");
7966 goto done;
7969 error = got_worktree_open(&worktree, cwd);
7970 if (error)
7971 goto done;
7973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7974 NULL);
7975 if (error != NULL)
7976 goto done;
7978 if (patch_script_path) {
7979 patch_script_file = fopen(patch_script_path, "r");
7980 if (patch_script_file == NULL) {
7981 error = got_error_from_errno2("fopen",
7982 patch_script_path);
7983 goto done;
7987 error = apply_unveil(got_repo_get_path(repo), 0,
7988 got_worktree_get_root_path(worktree));
7989 if (error)
7990 goto done;
7992 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7993 if (error)
7994 goto done;
7996 cpa.patch_script_file = patch_script_file;
7997 cpa.action = "unstage";
7998 error = got_worktree_unstage(worktree, &paths, update_progress,
7999 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8000 done:
8001 if (patch_script_file && fclose(patch_script_file) == EOF &&
8002 error == NULL)
8003 error = got_error_from_errno2("fclose", patch_script_path);
8004 if (repo)
8005 got_repo_close(repo);
8006 if (worktree)
8007 got_worktree_close(worktree);
8008 TAILQ_FOREACH(pe, &paths, entry)
8009 free((char *)pe->path);
8010 got_pathlist_free(&paths);
8011 free(cwd);
8012 return error;
8015 __dead static void
8016 usage_cat(void)
8018 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8019 "arg1 [arg2 ...]\n", getprogname());
8020 exit(1);
8023 static const struct got_error *
8024 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8026 const struct got_error *err;
8027 struct got_blob_object *blob;
8029 err = got_object_open_as_blob(&blob, repo, id, 8192);
8030 if (err)
8031 return err;
8033 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8034 got_object_blob_close(blob);
8035 return err;
8038 static const struct got_error *
8039 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8041 const struct got_error *err;
8042 struct got_tree_object *tree;
8043 int nentries, i;
8045 err = got_object_open_as_tree(&tree, repo, id);
8046 if (err)
8047 return err;
8049 nentries = got_object_tree_get_nentries(tree);
8050 for (i = 0; i < nentries; i++) {
8051 struct got_tree_entry *te;
8052 char *id_str;
8053 if (sigint_received || sigpipe_received)
8054 break;
8055 te = got_object_tree_get_entry(tree, i);
8056 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8057 if (err)
8058 break;
8059 fprintf(outfile, "%s %.7o %s\n", id_str,
8060 got_tree_entry_get_mode(te),
8061 got_tree_entry_get_name(te));
8062 free(id_str);
8065 got_object_tree_close(tree);
8066 return err;
8069 static const struct got_error *
8070 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8072 const struct got_error *err;
8073 struct got_commit_object *commit;
8074 const struct got_object_id_queue *parent_ids;
8075 struct got_object_qid *pid;
8076 char *id_str = NULL;
8077 const char *logmsg = NULL;
8079 err = got_object_open_as_commit(&commit, repo, id);
8080 if (err)
8081 return err;
8083 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8084 if (err)
8085 goto done;
8087 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8088 parent_ids = got_object_commit_get_parent_ids(commit);
8089 fprintf(outfile, "numparents %d\n",
8090 got_object_commit_get_nparents(commit));
8091 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8092 char *pid_str;
8093 err = got_object_id_str(&pid_str, pid->id);
8094 if (err)
8095 goto done;
8096 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8097 free(pid_str);
8099 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8100 got_object_commit_get_author(commit),
8101 got_object_commit_get_author_time(commit));
8103 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8104 got_object_commit_get_author(commit),
8105 got_object_commit_get_committer_time(commit));
8107 logmsg = got_object_commit_get_logmsg_raw(commit);
8108 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8109 fprintf(outfile, "%s", logmsg);
8110 done:
8111 free(id_str);
8112 got_object_commit_close(commit);
8113 return err;
8116 static const struct got_error *
8117 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8119 const struct got_error *err;
8120 struct got_tag_object *tag;
8121 char *id_str = NULL;
8122 const char *tagmsg = NULL;
8124 err = got_object_open_as_tag(&tag, repo, id);
8125 if (err)
8126 return err;
8128 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8129 if (err)
8130 goto done;
8132 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8134 switch (got_object_tag_get_object_type(tag)) {
8135 case GOT_OBJ_TYPE_BLOB:
8136 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8137 GOT_OBJ_LABEL_BLOB);
8138 break;
8139 case GOT_OBJ_TYPE_TREE:
8140 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8141 GOT_OBJ_LABEL_TREE);
8142 break;
8143 case GOT_OBJ_TYPE_COMMIT:
8144 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8145 GOT_OBJ_LABEL_COMMIT);
8146 break;
8147 case GOT_OBJ_TYPE_TAG:
8148 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8149 GOT_OBJ_LABEL_TAG);
8150 break;
8151 default:
8152 break;
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8156 got_object_tag_get_name(tag));
8158 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8159 got_object_tag_get_tagger(tag),
8160 got_object_tag_get_tagger_time(tag));
8162 tagmsg = got_object_tag_get_message(tag);
8163 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8164 fprintf(outfile, "%s", tagmsg);
8165 done:
8166 free(id_str);
8167 got_object_tag_close(tag);
8168 return err;
8171 static const struct got_error *
8172 cmd_cat(int argc, char *argv[])
8174 const struct got_error *error;
8175 struct got_repository *repo = NULL;
8176 struct got_worktree *worktree = NULL;
8177 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8178 const char *commit_id_str = NULL;
8179 struct got_object_id *id = NULL, *commit_id = NULL;
8180 int ch, obj_type, i, force_path = 0;
8182 #ifndef PROFILE
8183 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8184 NULL) == -1)
8185 err(1, "pledge");
8186 #endif
8188 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8189 switch (ch) {
8190 case 'c':
8191 commit_id_str = optarg;
8192 break;
8193 case 'r':
8194 repo_path = realpath(optarg, NULL);
8195 if (repo_path == NULL)
8196 return got_error_from_errno2("realpath",
8197 optarg);
8198 got_path_strip_trailing_slashes(repo_path);
8199 break;
8200 case 'P':
8201 force_path = 1;
8202 break;
8203 default:
8204 usage_cat();
8205 /* NOTREACHED */
8209 argc -= optind;
8210 argv += optind;
8212 cwd = getcwd(NULL, 0);
8213 if (cwd == NULL) {
8214 error = got_error_from_errno("getcwd");
8215 goto done;
8217 error = got_worktree_open(&worktree, cwd);
8218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8219 goto done;
8220 if (worktree) {
8221 if (repo_path == NULL) {
8222 repo_path = strdup(
8223 got_worktree_get_repo_path(worktree));
8224 if (repo_path == NULL) {
8225 error = got_error_from_errno("strdup");
8226 goto done;
8231 if (repo_path == NULL) {
8232 repo_path = getcwd(NULL, 0);
8233 if (repo_path == NULL)
8234 return got_error_from_errno("getcwd");
8237 error = got_repo_open(&repo, repo_path, NULL);
8238 free(repo_path);
8239 if (error != NULL)
8240 goto done;
8242 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8243 if (error)
8244 goto done;
8246 if (commit_id_str == NULL)
8247 commit_id_str = GOT_REF_HEAD;
8248 error = got_repo_match_object_id(&commit_id, NULL,
8249 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8250 if (error)
8251 goto done;
8253 for (i = 0; i < argc; i++) {
8254 if (force_path) {
8255 error = got_object_id_by_path(&id, repo, commit_id,
8256 argv[i]);
8257 if (error)
8258 break;
8259 } else {
8260 error = got_repo_match_object_id(&id, &label, argv[i],
8261 GOT_OBJ_TYPE_ANY, 0, repo);
8262 if (error) {
8263 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8264 error->code != GOT_ERR_NOT_REF)
8265 break;
8266 error = got_object_id_by_path(&id, repo,
8267 commit_id, argv[i]);
8268 if (error)
8269 break;
8273 error = got_object_get_type(&obj_type, repo, id);
8274 if (error)
8275 break;
8277 switch (obj_type) {
8278 case GOT_OBJ_TYPE_BLOB:
8279 error = cat_blob(id, repo, stdout);
8280 break;
8281 case GOT_OBJ_TYPE_TREE:
8282 error = cat_tree(id, repo, stdout);
8283 break;
8284 case GOT_OBJ_TYPE_COMMIT:
8285 error = cat_commit(id, repo, stdout);
8286 break;
8287 case GOT_OBJ_TYPE_TAG:
8288 error = cat_tag(id, repo, stdout);
8289 break;
8290 default:
8291 error = got_error(GOT_ERR_OBJ_TYPE);
8292 break;
8294 if (error)
8295 break;
8296 free(label);
8297 label = NULL;
8298 free(id);
8299 id = NULL;
8301 done:
8302 free(label);
8303 free(id);
8304 free(commit_id);
8305 if (worktree)
8306 got_worktree_close(worktree);
8307 if (repo) {
8308 const struct got_error *repo_error;
8309 repo_error = got_repo_close(repo);
8310 if (error == NULL)
8311 error = repo_error;
8313 return error;