Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
65 static void
66 catch_sigint(int signo)
67 {
68 sigint_received = 1;
69 }
71 static void
72 catch_sigpipe(int signo)
73 {
74 sigpipe_received = 1;
75 }
78 struct got_cmd {
79 const char *cmd_name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
83 };
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
167 };
169 static void
170 list_commands(void)
172 int i;
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
179 fputc('\n', stderr);
182 int
183 main(int argc, char *argv[])
185 struct got_cmd *cmd;
186 unsigned int i;
187 int ch;
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
191 { NULL, 0, NULL, 0}
192 };
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 switch (ch) {
198 case 'h':
199 hflag = 1;
200 break;
201 case 'V':
202 Vflag = 1;
203 break;
204 default:
205 usage(hflag);
206 /* NOTREACHED */
210 argc -= optind;
211 argv += optind;
212 optind = 0;
214 if (Vflag) {
215 got_version_print_str();
216 return 1;
219 if (argc <= 0)
220 usage(hflag);
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
232 continue;
234 if (hflag)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 !(sigint_received &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 return 1;
248 return 0;
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 list_commands();
253 return 1;
256 __dead static void
257 usage(int hflag)
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 getprogname());
261 if (hflag)
262 list_commands();
263 exit(1);
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
270 const char *editor;
272 *abspath = NULL;
274 editor = getenv("VISUAL");
275 if (editor == NULL)
276 editor = getenv("EDITOR");
278 if (editor) {
279 err = got_path_find_prog(abspath, editor);
280 if (err)
281 return err;
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
290 return NULL;
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
299 #ifdef PROFILE
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
302 #endif
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
313 if (err != NULL)
314 return err;
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
319 return NULL;
322 __dead static void
323 usage_init(void)
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 exit(1);
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
334 int ch;
336 while ((ch = getopt(argc, argv, "")) != -1) {
337 switch (ch) {
338 default:
339 usage_init();
340 /* NOTREACHED */
344 argc -= optind;
345 argv += optind;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 err(1, "pledge");
350 #endif
351 if (argc != 1)
352 usage_init();
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
361 if (error &&
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 goto done;
365 error = apply_unveil(repo_path, 0, NULL);
366 if (error)
367 goto done;
369 error = got_repo_init(repo_path);
370 done:
371 free(repo_path);
372 return error;
375 __dead static void
376 usage_import(void)
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
380 exit(1);
383 int
384 spawn_editor(const char *editor, const char *file)
386 pid_t pid;
387 sig_t sighup, sigint, sigquit;
388 int st = -1;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
395 case -1:
396 goto doneediting;
397 case 0:
398 execl(editor, editor, file, (char *)NULL);
399 _exit(127);
402 while (waitpid(pid, &st, 0) == -1)
403 if (errno != EINTR)
404 break;
406 doneediting:
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
412 errno = EINTR;
413 return -1;
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
424 char buf[1024];
425 struct stat st, st2;
426 FILE *fp;
427 int content_changed = 0;
428 size_t len;
430 *logmsg = NULL;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
446 if (*logmsg == NULL)
447 return got_error_from_errno("malloc");
448 (*logmsg)[0] = '\0';
449 len = 0;
451 fp = fopen(logmsg_path, "r");
452 if (fp == NULL) {
453 err = got_error_from_errno("fopen");
454 goto done;
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
458 content_changed = 1;
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
463 fclose(fp);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
467 len--;
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
473 done:
474 if (err) {
475 free(*logmsg);
476 *logmsg = NULL;
478 return err;
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
487 int fd;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
491 branch_name) == -1)
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
496 if (err)
497 goto done;
499 dprintf(fd, initial_content);
500 close(fd);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 done:
504 free(initial_content);
505 return err;
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
512 return NULL;
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
521 *author = NULL;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
525 if (name && email) {
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
528 return NULL;
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
535 if (name && email) {
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
538 return NULL;
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
545 if (*author == NULL)
546 return got_error_from_errno("strdup");
548 /*
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
551 */
552 while (*got_author && *got_author != '<')
553 got_author++;
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 goto done;
558 while (*got_author && *got_author != '@')
559 got_author++;
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 goto done;
564 while (*got_author && *got_author != '>')
565 got_author++;
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 done:
569 if (err) {
570 free(*author);
571 *author = NULL;
573 return err;
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
582 if (homedir) {
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
587 return NULL;
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
601 int ch;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 switch (ch) {
610 case 'b':
611 branch_name = optarg;
612 break;
613 case 'm':
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
617 goto done;
619 break;
620 case 'r':
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
624 optarg);
625 goto done;
627 break;
628 case 'I':
629 if (optarg[0] == '\0')
630 break;
631 error = got_pathlist_insert(&pe, &ignores, optarg,
632 NULL);
633 if (error)
634 goto done;
635 break;
636 default:
637 usage_import();
638 /* NOTREACHED */
642 argc -= optind;
643 argv += optind;
645 #ifndef PROFILE
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 "unveil",
648 NULL) == -1)
649 err(1, "pledge");
650 #endif
651 if (argc != 1)
652 usage_import();
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
661 if (error)
662 goto done;
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
664 if (error)
665 goto done;
667 error = get_author(&author, repo);
668 if (error)
669 return error;
671 /*
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
675 */
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
681 goto done;
684 error = got_ref_open(&branch_ref, repo, refname, 0);
685 if (error) {
686 if (error->code != GOT_ERR_NOT_REF)
687 goto done;
688 } else {
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
691 goto done;
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
697 goto done;
699 got_path_strip_trailing_slashes(path_dir);
701 /*
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
704 */
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
707 if (error)
708 goto done;
709 free(logmsg);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 path_dir, refname);
712 if (error) {
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 logmsg_path != NULL)
715 preserve_logmsg = 1;
716 goto done;
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
722 if (logmsg_path)
723 preserve_logmsg = 1;
724 goto done;
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 if (error) {
729 if (logmsg_path)
730 preserve_logmsg = 1;
731 goto done;
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
736 if (error) {
737 if (logmsg_path)
738 preserve_logmsg = 1;
739 goto done;
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 if (error) {
744 if (logmsg_path)
745 preserve_logmsg = 1;
746 goto done;
749 error = got_ref_write(branch_ref, repo);
750 if (error) {
751 if (logmsg_path)
752 preserve_logmsg = 1;
753 goto done;
756 error = got_object_id_str(&id_str, new_commit_id);
757 if (error) {
758 if (logmsg_path)
759 preserve_logmsg = 1;
760 goto done;
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 if (error) {
765 if (error->code != GOT_ERR_NOT_REF) {
766 if (logmsg_path)
767 preserve_logmsg = 1;
768 goto done;
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 branch_ref);
773 if (error) {
774 if (logmsg_path)
775 preserve_logmsg = 1;
776 goto done;
779 error = got_ref_write(head_ref, repo);
780 if (error) {
781 if (logmsg_path)
782 preserve_logmsg = 1;
783 goto done;
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
789 done:
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
795 free(logmsg);
796 free(logmsg_path);
797 free(repo_path);
798 free(editor);
799 free(refname);
800 free(new_commit_id);
801 free(id_str);
802 free(author);
803 free(gitconfig_path);
804 if (branch_ref)
805 got_ref_close(branch_ref);
806 if (head_ref)
807 got_ref_close(head_ref);
808 return error;
811 __dead static void
812 usage_clone(void)
814 fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
816 exit(1);
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
821 int last_p_indexed;
822 int last_p_resolved;
823 int verbosity;
824 };
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
836 return NULL;
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
840 fflush(stdout);
841 return NULL;
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 print_size = 1;
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
857 print_indexed = 1;
858 print_size = 1;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
866 print_resolved = 1;
867 print_indexed = 1;
868 print_size = 1;
873 if (print_size || print_indexed || print_resolved)
874 printf("\r");
875 if (print_size)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 if (print_indexed)
878 printf("; indexing %d%%", p_indexed);
879 if (print_resolved)
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
882 fflush(stdout);
884 return NULL;
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
905 ssize_t n;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
909 TAILQ_INIT(&refs);
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
913 switch (ch) {
914 case 'a':
915 fetch_all_branches = 1;
916 break;
917 case 'm':
918 mirror_references = 1;
919 break;
920 case 'v':
921 if (verbosity < 0)
922 verbosity = 0;
923 else if (verbosity < 3)
924 verbosity++;
925 break;
926 case 'q':
927 verbosity = -1;
928 break;
929 default:
930 usage_clone();
931 break;
934 argc -= optind;
935 argv += optind;
937 uri = argv[0];
939 if (argc == 1)
940 dirname = NULL;
941 else if (argc == 2)
942 dirname = argv[1];
943 else
944 usage_clone();
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
948 if (error)
949 goto done;
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
955 goto done;
958 if (strcmp(proto, "git") == 0) {
959 #ifndef PROFILE
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
962 err(1, "pledge");
963 #endif
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
966 #ifndef PROFILE
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
969 err(1, "pledge");
970 #endif
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
974 goto done;
975 } else {
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
977 goto done;
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
982 goto done;
984 repo_path = default_destdir;
985 } else
986 repo_path = dirname;
988 error = got_path_mkdir(repo_path);
989 if (error)
990 goto done;
992 error = got_repo_init(repo_path);
993 if (error)
994 goto done;
996 error = got_repo_open(&repo, repo_path, NULL);
997 if (error)
998 goto done;
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1004 goto done;
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1008 if (error)
1009 goto done;
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 verbosity);
1013 if (error)
1014 goto done;
1016 if (verbosity >= 0)
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1027 if (error)
1028 goto done;
1030 error = got_object_id_str(&id_str, pack_hash);
1031 if (error)
1032 goto done;
1033 if (verbosity >= 0)
1034 printf("\nFetched %s.pack\n", id_str);
1035 free(id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1045 if (error)
1046 goto done;
1047 error = got_ref_write(ref, repo);
1048 got_ref_close(ref);
1049 if (error)
1050 goto done;
1052 if (mirror_references)
1053 continue;
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1056 continue;
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1062 goto done;
1064 error = got_ref_alloc(&ref, remote_refname, id);
1065 if (error)
1066 goto done;
1067 error = got_ref_write(ref, repo);
1068 got_ref_close(ref);
1069 if (error)
1070 goto done;
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 continue;
1082 error = got_ref_open(&target_ref, repo, target, 0);
1083 if (error) {
1084 if (error->code == GOT_ERR_NOT_REF)
1085 continue;
1086 goto done;
1089 error = got_ref_alloc_symref(&head_symref,
1090 GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1092 if (error)
1093 goto done;
1095 if (verbosity >= 0)
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(head_symref));
1099 error = got_ref_write(head_symref, repo);
1100 break;
1103 /* Create a config file git-fetch(1) can understand. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 goto done;
1109 gitconfig_file = fopen(gitconfig_path, "a");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1112 goto done;
1114 if (mirror_references) {
1115 if (asprintf(&gitconfig,
1116 "[remote \"%s\"]\n"
1117 "\turl = %s\n"
1118 "\tfetch = +refs/*:refs/*\n"
1119 "\tmirror = true\n",
1120 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1121 error = got_error_from_errno("asprintf");
1122 goto done;
1124 } else if (fetch_all_branches) {
1125 if (asprintf(&gitconfig,
1126 "[remote \"%s\"]\n"
1127 "\turl = %s\n"
1128 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1129 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1130 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1131 error = got_error_from_errno("asprintf");
1132 goto done;
1134 } else {
1135 const char *branchname;
1138 * If the server specified a default branch, use just that one.
1139 * Otherwise fall back to fetching all branches on next fetch.
1141 if (head_symref) {
1142 branchname = got_ref_get_symref_target(head_symref);
1143 if (strncmp(branchname, "refs/heads/", 11) == 0)
1144 branchname += 11;
1145 } else
1146 branchname = "*"; /* fall back to all branches */
1147 if (asprintf(&gitconfig,
1148 "[remote \"%s\"]\n"
1149 "\turl = %s\n"
1150 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1151 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1152 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1153 branchname) == -1) {
1154 error = got_error_from_errno("asprintf");
1155 goto done;
1158 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1159 if (n != strlen(gitconfig)) {
1160 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1161 goto done;
1165 if (verbosity >= 0)
1166 printf("Created %s repository '%s'\n",
1167 mirror_references ? "mirrored" : "cloned", repo_path);
1168 done:
1169 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1170 error = got_error_from_errno("close");
1171 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1172 error = got_error_from_errno("fclose");
1173 if (repo)
1174 got_repo_close(repo);
1175 if (head_symref)
1176 got_ref_close(head_symref);
1177 TAILQ_FOREACH(pe, &refs, entry) {
1178 free((void *)pe->path);
1179 free(pe->data);
1181 got_pathlist_free(&refs);
1182 TAILQ_FOREACH(pe, &symrefs, entry) {
1183 free((void *)pe->path);
1184 free(pe->data);
1186 got_pathlist_free(&symrefs);
1187 free(pack_hash);
1188 free(proto);
1189 free(host);
1190 free(port);
1191 free(server_path);
1192 free(repo_name);
1193 free(default_destdir);
1194 free(gitconfig_path);
1195 free(git_url);
1196 return error;
1199 static const struct got_error *
1200 create_ref(const char *refname, struct got_object_id *id,
1201 const char *id_str, struct got_repository *repo)
1203 const struct got_error *err = NULL;
1204 struct got_reference *ref;
1206 printf("Creating %s: %s\n", refname, id_str);
1208 err = got_ref_alloc(&ref, refname, id);
1209 if (err)
1210 return err;
1212 err = got_ref_write(ref, repo);
1213 got_ref_close(ref);
1214 return err;
1217 static const struct got_error *
1218 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1222 char *new_id_str = NULL;
1223 struct got_object_id *old_id = NULL;
1225 err = got_object_id_str(&new_id_str, new_id);
1226 if (err)
1227 goto done;
1229 if (got_ref_is_symbolic(ref)) {
1230 struct got_reference *new_ref;
1231 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1232 if (err)
1233 goto done;
1234 printf("Deleting symbolic reference %s -> %s\n",
1235 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1236 err = got_ref_delete(ref, repo);
1237 if (err)
1238 goto done;
1239 printf("Setting %s to %s\n", got_ref_get_name(ref),
1240 new_id_str);
1241 err = got_ref_write(new_ref, repo);
1242 if (err)
1243 goto done;
1244 } else {
1245 err = got_ref_resolve(&old_id, repo, ref);
1246 if (err)
1247 goto done;
1248 if (got_object_id_cmp(old_id, new_id) != 0) {
1249 printf("Setting %s to %s\n",
1250 got_ref_get_name(ref), new_id_str);
1251 err = got_ref_change_ref(ref, new_id);
1252 if (err)
1253 goto done;
1254 err = got_ref_write(ref, repo);
1255 if (err)
1256 goto done;
1259 done:
1260 free(old_id);
1261 free(new_id_str);
1262 return err;
1265 __dead static void
1266 usage_fetch(void)
1268 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1269 "[remote-repository-name]\n", getprogname());
1270 exit(1);
1273 static const struct got_error *
1274 cmd_fetch(int argc, char *argv[])
1276 const struct got_error *error = NULL;
1277 char *cwd = NULL, *repo_path = NULL;
1278 const char *remote_name;
1279 char *proto = NULL, *host = NULL, *port = NULL;
1280 char *repo_name = NULL, *server_path = NULL;
1281 struct got_remote_repo *remotes, *remote = NULL;
1282 int nremotes;
1283 char *id_str = NULL;
1284 struct got_repository *repo = NULL;
1285 struct got_worktree *worktree = NULL;
1286 struct got_pathlist_head refs, symrefs;
1287 struct got_pathlist_entry *pe;
1288 struct got_object_id *pack_hash = NULL;
1289 int i, ch, fetchfd = -1;
1290 struct got_fetch_progress_arg fpa;
1291 int verbosity = 0, fetch_all_branches = 0;
1293 TAILQ_INIT(&refs);
1294 TAILQ_INIT(&symrefs);
1296 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1297 switch (ch) {
1298 case 'a':
1299 fetch_all_branches = 1;
1300 break;
1301 case 'r':
1302 repo_path = realpath(optarg, NULL);
1303 if (repo_path == NULL)
1304 return got_error_from_errno2("realpath",
1305 optarg);
1306 got_path_strip_trailing_slashes(repo_path);
1307 break;
1308 case 'v':
1309 if (verbosity < 0)
1310 verbosity = 0;
1311 else if (verbosity < 3)
1312 verbosity++;
1313 break;
1314 case 'q':
1315 verbosity = -1;
1316 break;
1317 default:
1318 usage_fetch();
1319 break;
1322 argc -= optind;
1323 argv += optind;
1325 if (argc == 0)
1326 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1327 else if (argc == 1)
1328 remote_name = argv[0];
1329 else
1330 usage_fetch();
1332 cwd = getcwd(NULL, 0);
1333 if (cwd == NULL) {
1334 error = got_error_from_errno("getcwd");
1335 goto done;
1338 if (repo_path == NULL) {
1339 error = got_worktree_open(&worktree, cwd);
1340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1341 goto done;
1342 else
1343 error = NULL;
1344 if (worktree) {
1345 repo_path =
1346 strdup(got_worktree_get_repo_path(worktree));
1347 if (repo_path == NULL)
1348 error = got_error_from_errno("strdup");
1349 if (error)
1350 goto done;
1351 } else {
1352 repo_path = strdup(cwd);
1353 if (repo_path == NULL) {
1354 error = got_error_from_errno("strdup");
1355 goto done;
1360 error = got_repo_open(&repo, repo_path, NULL);
1361 if (error)
1362 goto done;
1364 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1365 for (i = 0; i < nremotes; i++) {
1366 remote = &remotes[i];
1367 if (strcmp(remote->name, remote_name) == 0)
1368 break;
1370 if (i == nremotes) {
1371 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1372 goto done;
1375 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1376 &repo_name, remote->url);
1377 if (error)
1378 goto done;
1380 if (strcmp(proto, "git") == 0) {
1381 #ifndef PROFILE
1382 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 "sendfd dns inet unveil", NULL) == -1)
1384 err(1, "pledge");
1385 #endif
1386 } else if (strcmp(proto, "git+ssh") == 0 ||
1387 strcmp(proto, "ssh") == 0) {
1388 #ifndef PROFILE
1389 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1390 "sendfd unveil", NULL) == -1)
1391 err(1, "pledge");
1392 #endif
1393 } else if (strcmp(proto, "http") == 0 ||
1394 strcmp(proto, "git+http") == 0) {
1395 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1396 goto done;
1397 } else {
1398 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1399 goto done;
1402 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1403 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1404 error = got_error_from_errno2("unveil",
1405 GOT_FETCH_PATH_SSH);
1406 goto done;
1409 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1410 if (error)
1411 goto done;
1413 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1414 verbosity);
1415 if (error)
1416 goto done;
1418 if (verbosity >= 0)
1419 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1420 port ? ":" : "", port ? port : "");
1422 fpa.last_scaled_size[0] = '\0';
1423 fpa.last_p_indexed = -1;
1424 fpa.last_p_resolved = -1;
1425 fpa.verbosity = verbosity;
1426 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1427 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1428 fetch_progress, &fpa);
1429 if (error)
1430 goto done;
1432 if (pack_hash == NULL) {
1433 if (verbosity >= 0)
1434 printf("Already up-to-date\n");
1435 goto done;
1438 if (verbosity >= 0) {
1439 error = got_object_id_str(&id_str, pack_hash);
1440 if (error)
1441 goto done;
1442 printf("\nFetched %s.pack\n", id_str);
1443 free(id_str);
1444 id_str = NULL;
1447 /* Update references provided with the pack file. */
1448 TAILQ_FOREACH(pe, &refs, entry) {
1449 const char *refname = pe->path;
1450 struct got_object_id *id = pe->data;
1451 struct got_reference *ref;
1452 char *remote_refname;
1454 if (remote->mirror_references) {
1455 error = got_ref_alloc(&ref, refname, id);
1456 if (error)
1457 goto done;
1458 error = got_ref_write(ref, repo);
1459 got_ref_close(ref);
1460 if (error)
1461 goto done;
1462 continue;
1465 error = got_object_id_str(&id_str, id);
1466 if (error)
1467 goto done;
1469 if (strncmp("refs/tags/", refname, 10) == 0) {
1470 error = got_ref_open(&ref, repo, refname, 0);
1471 if (error) {
1472 if (error->code != GOT_ERR_NOT_REF)
1473 goto done;
1474 error = create_ref(refname, id, id_str, repo);
1475 if (error)
1476 goto done;
1477 } else {
1478 error = update_ref(ref, id, repo);
1479 got_ref_close(ref);
1480 if (error)
1481 goto done;
1483 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1484 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1485 remote_name, refname + 11) == -1) {
1486 error = got_error_from_errno("asprintf");
1487 goto done;
1490 error = got_ref_open(&ref, repo, remote_refname, 0);
1491 if (error) {
1492 if (error->code != GOT_ERR_NOT_REF)
1493 goto done;
1494 error = create_ref(remote_refname, id, id_str,
1495 repo);
1496 if (error)
1497 goto done;
1498 } else {
1499 error = update_ref(ref, id, repo);
1500 got_ref_close(ref);
1501 if (error)
1502 goto done;
1505 free(id_str);
1506 id_str = NULL;
1508 done:
1509 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1510 error = got_error_from_errno("close");
1511 if (repo)
1512 got_repo_close(repo);
1513 if (worktree)
1514 got_worktree_close(worktree);
1515 TAILQ_FOREACH(pe, &refs, entry) {
1516 free((void *)pe->path);
1517 free(pe->data);
1519 got_pathlist_free(&refs);
1520 TAILQ_FOREACH(pe, &symrefs, entry) {
1521 free((void *)pe->path);
1522 free(pe->data);
1524 got_pathlist_free(&symrefs);
1525 free(id_str);
1526 free(cwd);
1527 free(repo_path);
1528 free(pack_hash);
1529 free(proto);
1530 free(host);
1531 free(port);
1532 free(server_path);
1533 free(repo_name);
1534 return error;
1538 __dead static void
1539 usage_checkout(void)
1541 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1542 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1543 exit(1);
1546 static void
1547 show_worktree_base_ref_warning(void)
1549 fprintf(stderr, "%s: warning: could not create a reference "
1550 "to the work tree's base commit; the commit could be "
1551 "garbage-collected by Git; making the repository "
1552 "writable and running 'got update' will prevent this\n",
1553 getprogname());
1556 struct got_checkout_progress_arg {
1557 const char *worktree_path;
1558 int had_base_commit_ref_error;
1561 static const struct got_error *
1562 checkout_progress(void *arg, unsigned char status, const char *path)
1564 struct got_checkout_progress_arg *a = arg;
1566 /* Base commit bump happens silently. */
1567 if (status == GOT_STATUS_BUMP_BASE)
1568 return NULL;
1570 if (status == GOT_STATUS_BASE_REF_ERR) {
1571 a->had_base_commit_ref_error = 1;
1572 return NULL;
1575 while (path[0] == '/')
1576 path++;
1578 printf("%c %s/%s\n", status, a->worktree_path, path);
1579 return NULL;
1582 static const struct got_error *
1583 check_cancelled(void *arg)
1585 if (sigint_received || sigpipe_received)
1586 return got_error(GOT_ERR_CANCELLED);
1587 return NULL;
1590 static const struct got_error *
1591 check_linear_ancestry(struct got_object_id *commit_id,
1592 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1593 struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_object_id *yca_id;
1598 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1599 commit_id, base_commit_id, repo, check_cancelled, NULL);
1600 if (err)
1601 return err;
1603 if (yca_id == NULL)
1604 return got_error(GOT_ERR_ANCESTRY);
1607 * Require a straight line of history between the target commit
1608 * and the work tree's base commit.
1610 * Non-linear situations such as this require a rebase:
1612 * (commit) D F (base_commit)
1613 * \ /
1614 * C E
1615 * \ /
1616 * B (yca)
1617 * |
1618 * A
1620 * 'got update' only handles linear cases:
1621 * Update forwards in time: A (base/yca) - B - C - D (commit)
1622 * Update backwards in time: D (base) - C - B - A (commit/yca)
1624 if (allow_forwards_in_time_only) {
1625 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1626 return got_error(GOT_ERR_ANCESTRY);
1627 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1628 got_object_id_cmp(base_commit_id, yca_id) != 0)
1629 return got_error(GOT_ERR_ANCESTRY);
1631 free(yca_id);
1632 return NULL;
1635 static const struct got_error *
1636 check_same_branch(struct got_object_id *commit_id,
1637 struct got_reference *head_ref, struct got_object_id *yca_id,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 struct got_commit_graph *graph = NULL;
1642 struct got_object_id *head_commit_id = NULL;
1643 int is_same_branch = 0;
1645 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1646 if (err)
1647 goto done;
1649 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1650 is_same_branch = 1;
1651 goto done;
1653 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1654 is_same_branch = 1;
1655 goto done;
1658 err = got_commit_graph_open(&graph, "/", 1);
1659 if (err)
1660 goto done;
1662 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1663 check_cancelled, NULL);
1664 if (err)
1665 goto done;
1667 for (;;) {
1668 struct got_object_id *id;
1669 err = got_commit_graph_iter_next(&id, graph, repo,
1670 check_cancelled, NULL);
1671 if (err) {
1672 if (err->code == GOT_ERR_ITER_COMPLETED)
1673 err = NULL;
1674 break;
1677 if (id) {
1678 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1679 break;
1680 if (got_object_id_cmp(id, commit_id) == 0) {
1681 is_same_branch = 1;
1682 break;
1686 done:
1687 if (graph)
1688 got_commit_graph_close(graph);
1689 free(head_commit_id);
1690 if (!err && !is_same_branch)
1691 err = got_error(GOT_ERR_ANCESTRY);
1692 return err;
1695 static const struct got_error *
1696 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1698 static char msg[512];
1699 const char *branch_name;
1701 if (got_ref_is_symbolic(ref))
1702 branch_name = got_ref_get_symref_target(ref);
1703 else
1704 branch_name = got_ref_get_name(ref);
1706 if (strncmp("refs/heads/", branch_name, 11) == 0)
1707 branch_name += 11;
1709 snprintf(msg, sizeof(msg),
1710 "target commit is not contained in branch '%s'; "
1711 "the branch to use must be specified with -b; "
1712 "if necessary a new branch can be created for "
1713 "this commit with 'got branch -c %s BRANCH_NAME'",
1714 branch_name, commit_id_str);
1716 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1719 static const struct got_error *
1720 cmd_checkout(int argc, char *argv[])
1722 const struct got_error *error = NULL;
1723 struct got_repository *repo = NULL;
1724 struct got_reference *head_ref = NULL;
1725 struct got_worktree *worktree = NULL;
1726 char *repo_path = NULL;
1727 char *worktree_path = NULL;
1728 const char *path_prefix = "";
1729 const char *branch_name = GOT_REF_HEAD;
1730 char *commit_id_str = NULL;
1731 int ch, same_path_prefix, allow_nonempty = 0;
1732 struct got_pathlist_head paths;
1733 struct got_checkout_progress_arg cpa;
1735 TAILQ_INIT(&paths);
1737 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1738 switch (ch) {
1739 case 'b':
1740 branch_name = optarg;
1741 break;
1742 case 'c':
1743 commit_id_str = strdup(optarg);
1744 if (commit_id_str == NULL)
1745 return got_error_from_errno("strdup");
1746 break;
1747 case 'E':
1748 allow_nonempty = 1;
1749 break;
1750 case 'p':
1751 path_prefix = optarg;
1752 break;
1753 default:
1754 usage_checkout();
1755 /* NOTREACHED */
1759 argc -= optind;
1760 argv += optind;
1762 #ifndef PROFILE
1763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1764 "unveil", NULL) == -1)
1765 err(1, "pledge");
1766 #endif
1767 if (argc == 1) {
1768 char *cwd, *base, *dotgit;
1769 repo_path = realpath(argv[0], NULL);
1770 if (repo_path == NULL)
1771 return got_error_from_errno2("realpath", argv[0]);
1772 cwd = getcwd(NULL, 0);
1773 if (cwd == NULL) {
1774 error = got_error_from_errno("getcwd");
1775 goto done;
1777 if (path_prefix[0]) {
1778 base = basename(path_prefix);
1779 if (base == NULL) {
1780 error = got_error_from_errno2("basename",
1781 path_prefix);
1782 goto done;
1784 } else {
1785 base = basename(repo_path);
1786 if (base == NULL) {
1787 error = got_error_from_errno2("basename",
1788 repo_path);
1789 goto done;
1792 dotgit = strstr(base, ".git");
1793 if (dotgit)
1794 *dotgit = '\0';
1795 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1796 error = got_error_from_errno("asprintf");
1797 free(cwd);
1798 goto done;
1800 free(cwd);
1801 } else if (argc == 2) {
1802 repo_path = realpath(argv[0], NULL);
1803 if (repo_path == NULL) {
1804 error = got_error_from_errno2("realpath", argv[0]);
1805 goto done;
1807 worktree_path = realpath(argv[1], NULL);
1808 if (worktree_path == NULL) {
1809 if (errno != ENOENT) {
1810 error = got_error_from_errno2("realpath",
1811 argv[1]);
1812 goto done;
1814 worktree_path = strdup(argv[1]);
1815 if (worktree_path == NULL) {
1816 error = got_error_from_errno("strdup");
1817 goto done;
1820 } else
1821 usage_checkout();
1823 got_path_strip_trailing_slashes(repo_path);
1824 got_path_strip_trailing_slashes(worktree_path);
1826 error = got_repo_open(&repo, repo_path, NULL);
1827 if (error != NULL)
1828 goto done;
1830 /* Pre-create work tree path for unveil(2) */
1831 error = got_path_mkdir(worktree_path);
1832 if (error) {
1833 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1834 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1835 goto done;
1836 if (!allow_nonempty &&
1837 !got_path_dir_is_empty(worktree_path)) {
1838 error = got_error_path(worktree_path,
1839 GOT_ERR_DIR_NOT_EMPTY);
1840 goto done;
1844 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1845 if (error)
1846 goto done;
1848 error = got_ref_open(&head_ref, repo, branch_name, 0);
1849 if (error != NULL)
1850 goto done;
1852 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1853 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1854 goto done;
1856 error = got_worktree_open(&worktree, worktree_path);
1857 if (error != NULL)
1858 goto done;
1860 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1861 path_prefix);
1862 if (error != NULL)
1863 goto done;
1864 if (!same_path_prefix) {
1865 error = got_error(GOT_ERR_PATH_PREFIX);
1866 goto done;
1869 if (commit_id_str) {
1870 struct got_object_id *commit_id;
1871 error = got_repo_match_object_id(&commit_id, NULL,
1872 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1873 if (error)
1874 goto done;
1875 error = check_linear_ancestry(commit_id,
1876 got_worktree_get_base_commit_id(worktree), 0, repo);
1877 if (error != NULL) {
1878 free(commit_id);
1879 if (error->code == GOT_ERR_ANCESTRY) {
1880 error = checkout_ancestry_error(
1881 head_ref, commit_id_str);
1883 goto done;
1885 error = check_same_branch(commit_id, head_ref, NULL, repo);
1886 if (error) {
1887 if (error->code == GOT_ERR_ANCESTRY) {
1888 error = checkout_ancestry_error(
1889 head_ref, commit_id_str);
1891 goto done;
1893 error = got_worktree_set_base_commit_id(worktree, repo,
1894 commit_id);
1895 free(commit_id);
1896 if (error)
1897 goto done;
1900 error = got_pathlist_append(&paths, "", NULL);
1901 if (error)
1902 goto done;
1903 cpa.worktree_path = worktree_path;
1904 cpa.had_base_commit_ref_error = 0;
1905 error = got_worktree_checkout_files(worktree, &paths, repo,
1906 checkout_progress, &cpa, check_cancelled, NULL);
1907 if (error != NULL)
1908 goto done;
1910 printf("Now shut up and hack\n");
1911 if (cpa.had_base_commit_ref_error)
1912 show_worktree_base_ref_warning();
1913 done:
1914 got_pathlist_free(&paths);
1915 free(commit_id_str);
1916 free(repo_path);
1917 free(worktree_path);
1918 return error;
1921 __dead static void
1922 usage_update(void)
1924 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1925 getprogname());
1926 exit(1);
1929 static const struct got_error *
1930 update_progress(void *arg, unsigned char status, const char *path)
1932 int *did_something = arg;
1934 if (status == GOT_STATUS_EXISTS ||
1935 status == GOT_STATUS_BASE_REF_ERR)
1936 return NULL;
1938 *did_something = 1;
1940 /* Base commit bump happens silently. */
1941 if (status == GOT_STATUS_BUMP_BASE)
1942 return NULL;
1944 while (path[0] == '/')
1945 path++;
1946 printf("%c %s\n", status, path);
1947 return NULL;
1950 static const struct got_error *
1951 switch_head_ref(struct got_reference *head_ref,
1952 struct got_object_id *commit_id, struct got_worktree *worktree,
1953 struct got_repository *repo)
1955 const struct got_error *err = NULL;
1956 char *base_id_str;
1957 int ref_has_moved = 0;
1959 /* Trivial case: switching between two different references. */
1960 if (strcmp(got_ref_get_name(head_ref),
1961 got_worktree_get_head_ref_name(worktree)) != 0) {
1962 printf("Switching work tree from %s to %s\n",
1963 got_worktree_get_head_ref_name(worktree),
1964 got_ref_get_name(head_ref));
1965 return got_worktree_set_head_ref(worktree, head_ref);
1968 err = check_linear_ancestry(commit_id,
1969 got_worktree_get_base_commit_id(worktree), 0, repo);
1970 if (err) {
1971 if (err->code != GOT_ERR_ANCESTRY)
1972 return err;
1973 ref_has_moved = 1;
1975 if (!ref_has_moved)
1976 return NULL;
1978 /* Switching to a rebased branch with the same reference name. */
1979 err = got_object_id_str(&base_id_str,
1980 got_worktree_get_base_commit_id(worktree));
1981 if (err)
1982 return err;
1983 printf("Reference %s now points at a different branch\n",
1984 got_worktree_get_head_ref_name(worktree));
1985 printf("Switching work tree from %s to %s\n", base_id_str,
1986 got_worktree_get_head_ref_name(worktree));
1987 return NULL;
1990 static const struct got_error *
1991 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1993 const struct got_error *err;
1994 int in_progress;
1996 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1997 if (err)
1998 return err;
1999 if (in_progress)
2000 return got_error(GOT_ERR_REBASING);
2002 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2003 if (err)
2004 return err;
2005 if (in_progress)
2006 return got_error(GOT_ERR_HISTEDIT_BUSY);
2008 return NULL;
2011 static const struct got_error *
2012 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2013 char *argv[], struct got_worktree *worktree)
2015 const struct got_error *err = NULL;
2016 char *path;
2017 int i;
2019 if (argc == 0) {
2020 path = strdup("");
2021 if (path == NULL)
2022 return got_error_from_errno("strdup");
2023 return got_pathlist_append(paths, path, NULL);
2026 for (i = 0; i < argc; i++) {
2027 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2028 if (err)
2029 break;
2030 err = got_pathlist_append(paths, path, NULL);
2031 if (err) {
2032 free(path);
2033 break;
2037 return err;
2040 static const struct got_error *
2041 cmd_update(int argc, char *argv[])
2043 const struct got_error *error = NULL;
2044 struct got_repository *repo = NULL;
2045 struct got_worktree *worktree = NULL;
2046 char *worktree_path = NULL;
2047 struct got_object_id *commit_id = NULL;
2048 char *commit_id_str = NULL;
2049 const char *branch_name = NULL;
2050 struct got_reference *head_ref = NULL;
2051 struct got_pathlist_head paths;
2052 struct got_pathlist_entry *pe;
2053 int ch, did_something = 0;
2055 TAILQ_INIT(&paths);
2057 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2058 switch (ch) {
2059 case 'b':
2060 branch_name = optarg;
2061 break;
2062 case 'c':
2063 commit_id_str = strdup(optarg);
2064 if (commit_id_str == NULL)
2065 return got_error_from_errno("strdup");
2066 break;
2067 default:
2068 usage_update();
2069 /* NOTREACHED */
2073 argc -= optind;
2074 argv += optind;
2076 #ifndef PROFILE
2077 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2078 "unveil", NULL) == -1)
2079 err(1, "pledge");
2080 #endif
2081 worktree_path = getcwd(NULL, 0);
2082 if (worktree_path == NULL) {
2083 error = got_error_from_errno("getcwd");
2084 goto done;
2086 error = got_worktree_open(&worktree, worktree_path);
2087 if (error)
2088 goto done;
2090 error = check_rebase_or_histedit_in_progress(worktree);
2091 if (error)
2092 goto done;
2094 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2095 NULL);
2096 if (error != NULL)
2097 goto done;
2099 error = apply_unveil(got_repo_get_path(repo), 0,
2100 got_worktree_get_root_path(worktree));
2101 if (error)
2102 goto done;
2104 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2105 if (error)
2106 goto done;
2108 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2109 got_worktree_get_head_ref_name(worktree), 0);
2110 if (error != NULL)
2111 goto done;
2112 if (commit_id_str == NULL) {
2113 error = got_ref_resolve(&commit_id, repo, head_ref);
2114 if (error != NULL)
2115 goto done;
2116 error = got_object_id_str(&commit_id_str, commit_id);
2117 if (error != NULL)
2118 goto done;
2119 } else {
2120 error = got_repo_match_object_id(&commit_id, NULL,
2121 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2122 free(commit_id_str);
2123 commit_id_str = NULL;
2124 if (error)
2125 goto done;
2126 error = got_object_id_str(&commit_id_str, commit_id);
2127 if (error)
2128 goto done;
2131 if (branch_name) {
2132 struct got_object_id *head_commit_id;
2133 TAILQ_FOREACH(pe, &paths, entry) {
2134 if (pe->path_len == 0)
2135 continue;
2136 error = got_error_msg(GOT_ERR_BAD_PATH,
2137 "switching between branches requires that "
2138 "the entire work tree gets updated");
2139 goto done;
2141 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2142 if (error)
2143 goto done;
2144 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2145 repo);
2146 free(head_commit_id);
2147 if (error != NULL)
2148 goto done;
2149 error = check_same_branch(commit_id, head_ref, NULL, repo);
2150 if (error)
2151 goto done;
2152 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2153 if (error)
2154 goto done;
2155 } else {
2156 error = check_linear_ancestry(commit_id,
2157 got_worktree_get_base_commit_id(worktree), 0, repo);
2158 if (error != NULL) {
2159 if (error->code == GOT_ERR_ANCESTRY)
2160 error = got_error(GOT_ERR_BRANCH_MOVED);
2161 goto done;
2163 error = check_same_branch(commit_id, head_ref, NULL, repo);
2164 if (error)
2165 goto done;
2168 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2169 commit_id) != 0) {
2170 error = got_worktree_set_base_commit_id(worktree, repo,
2171 commit_id);
2172 if (error)
2173 goto done;
2176 error = got_worktree_checkout_files(worktree, &paths, repo,
2177 update_progress, &did_something, check_cancelled, NULL);
2178 if (error != NULL)
2179 goto done;
2181 if (did_something)
2182 printf("Updated to commit %s\n", commit_id_str);
2183 else
2184 printf("Already up-to-date\n");
2185 done:
2186 free(worktree_path);
2187 TAILQ_FOREACH(pe, &paths, entry)
2188 free((char *)pe->path);
2189 got_pathlist_free(&paths);
2190 free(commit_id);
2191 free(commit_id_str);
2192 return error;
2195 static const struct got_error *
2196 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2197 const char *path, int diff_context, int ignore_whitespace,
2198 struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2203 if (blob_id1) {
2204 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2205 if (err)
2206 goto done;
2209 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2210 if (err)
2211 goto done;
2213 while (path[0] == '/')
2214 path++;
2215 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2216 ignore_whitespace, stdout);
2217 done:
2218 if (blob1)
2219 got_object_blob_close(blob1);
2220 got_object_blob_close(blob2);
2221 return err;
2224 static const struct got_error *
2225 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2226 const char *path, int diff_context, int ignore_whitespace,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2231 struct got_diff_blob_output_unidiff_arg arg;
2233 if (tree_id1) {
2234 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2235 if (err)
2236 goto done;
2239 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2240 if (err)
2241 goto done;
2243 arg.diff_context = diff_context;
2244 arg.ignore_whitespace = ignore_whitespace;
2245 arg.outfile = stdout;
2246 while (path[0] == '/')
2247 path++;
2248 err = got_diff_tree(tree1, tree2, path, path, repo,
2249 got_diff_blob_output_unidiff, &arg, 1);
2250 done:
2251 if (tree1)
2252 got_object_tree_close(tree1);
2253 if (tree2)
2254 got_object_tree_close(tree2);
2255 return err;
2258 static const struct got_error *
2259 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2260 const char *path, int diff_context, struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_commit_object *pcommit = NULL;
2264 char *id_str1 = NULL, *id_str2 = NULL;
2265 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2266 struct got_object_qid *qid;
2268 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2269 if (qid != NULL) {
2270 err = got_object_open_as_commit(&pcommit, repo,
2271 qid->id);
2272 if (err)
2273 return err;
2276 if (path && path[0] != '\0') {
2277 int obj_type;
2278 err = got_object_id_by_path(&obj_id2, repo, id, path);
2279 if (err)
2280 goto done;
2281 err = got_object_id_str(&id_str2, obj_id2);
2282 if (err) {
2283 free(obj_id2);
2284 goto done;
2286 if (pcommit) {
2287 err = got_object_id_by_path(&obj_id1, repo,
2288 qid->id, path);
2289 if (err) {
2290 free(obj_id2);
2291 goto done;
2293 err = got_object_id_str(&id_str1, obj_id1);
2294 if (err) {
2295 free(obj_id2);
2296 goto done;
2299 err = got_object_get_type(&obj_type, repo, obj_id2);
2300 if (err) {
2301 free(obj_id2);
2302 goto done;
2304 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2305 switch (obj_type) {
2306 case GOT_OBJ_TYPE_BLOB:
2307 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2308 0, repo);
2309 break;
2310 case GOT_OBJ_TYPE_TREE:
2311 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2312 0, repo);
2313 break;
2314 default:
2315 err = got_error(GOT_ERR_OBJ_TYPE);
2316 break;
2318 free(obj_id1);
2319 free(obj_id2);
2320 } else {
2321 obj_id2 = got_object_commit_get_tree_id(commit);
2322 err = got_object_id_str(&id_str2, obj_id2);
2323 if (err)
2324 goto done;
2325 obj_id1 = got_object_commit_get_tree_id(pcommit);
2326 err = got_object_id_str(&id_str1, obj_id1);
2327 if (err)
2328 goto done;
2329 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2330 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2332 done:
2333 free(id_str1);
2334 free(id_str2);
2335 if (pcommit)
2336 got_object_commit_close(pcommit);
2337 return err;
2340 static char *
2341 get_datestr(time_t *time, char *datebuf)
2343 struct tm mytm, *tm;
2344 char *p, *s;
2346 tm = gmtime_r(time, &mytm);
2347 if (tm == NULL)
2348 return NULL;
2349 s = asctime_r(tm, datebuf);
2350 if (s == NULL)
2351 return NULL;
2352 p = strchr(s, '\n');
2353 if (p)
2354 *p = '\0';
2355 return s;
2358 static const struct got_error *
2359 match_logmsg(int *have_match, struct got_object_id *id,
2360 struct got_commit_object *commit, regex_t *regex)
2362 const struct got_error *err = NULL;
2363 regmatch_t regmatch;
2364 char *id_str = NULL, *logmsg = NULL;
2366 *have_match = 0;
2368 err = got_object_id_str(&id_str, id);
2369 if (err)
2370 return err;
2372 err = got_object_commit_get_logmsg(&logmsg, commit);
2373 if (err)
2374 goto done;
2376 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2377 *have_match = 1;
2378 done:
2379 free(id_str);
2380 free(logmsg);
2381 return err;
2384 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2386 static const struct got_error *
2387 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2388 struct got_repository *repo, const char *path, int show_patch,
2389 int diff_context, struct got_reflist_head *refs)
2391 const struct got_error *err = NULL;
2392 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2393 char datebuf[26];
2394 time_t committer_time;
2395 const char *author, *committer;
2396 char *refs_str = NULL;
2397 struct got_reflist_entry *re;
2399 SIMPLEQ_FOREACH(re, refs, entry) {
2400 char *s;
2401 const char *name;
2402 struct got_tag_object *tag = NULL;
2403 int cmp;
2405 name = got_ref_get_name(re->ref);
2406 if (strcmp(name, GOT_REF_HEAD) == 0)
2407 continue;
2408 if (strncmp(name, "refs/", 5) == 0)
2409 name += 5;
2410 if (strncmp(name, "got/", 4) == 0)
2411 continue;
2412 if (strncmp(name, "heads/", 6) == 0)
2413 name += 6;
2414 if (strncmp(name, "remotes/", 8) == 0)
2415 name += 8;
2416 if (strncmp(name, "tags/", 5) == 0) {
2417 err = got_object_open_as_tag(&tag, repo, re->id);
2418 if (err) {
2419 if (err->code != GOT_ERR_OBJ_TYPE)
2420 return err;
2421 /* Ref points at something other than a tag. */
2422 err = NULL;
2423 tag = NULL;
2426 cmp = got_object_id_cmp(tag ?
2427 got_object_tag_get_object_id(tag) : re->id, id);
2428 if (tag)
2429 got_object_tag_close(tag);
2430 if (cmp != 0)
2431 continue;
2432 s = refs_str;
2433 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2434 name) == -1) {
2435 err = got_error_from_errno("asprintf");
2436 free(s);
2437 return err;
2439 free(s);
2441 err = got_object_id_str(&id_str, id);
2442 if (err)
2443 return err;
2445 printf(GOT_COMMIT_SEP_STR);
2446 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2447 refs_str ? refs_str : "", refs_str ? ")" : "");
2448 free(id_str);
2449 id_str = NULL;
2450 free(refs_str);
2451 refs_str = NULL;
2452 printf("from: %s\n", got_object_commit_get_author(commit));
2453 committer_time = got_object_commit_get_committer_time(commit);
2454 datestr = get_datestr(&committer_time, datebuf);
2455 if (datestr)
2456 printf("date: %s UTC\n", datestr);
2457 author = got_object_commit_get_author(commit);
2458 committer = got_object_commit_get_committer(commit);
2459 if (strcmp(author, committer) != 0)
2460 printf("via: %s\n", committer);
2461 if (got_object_commit_get_nparents(commit) > 1) {
2462 const struct got_object_id_queue *parent_ids;
2463 struct got_object_qid *qid;
2464 int n = 1;
2465 parent_ids = got_object_commit_get_parent_ids(commit);
2466 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2467 err = got_object_id_str(&id_str, qid->id);
2468 if (err)
2469 return err;
2470 printf("parent %d: %s\n", n++, id_str);
2471 free(id_str);
2475 err = got_object_commit_get_logmsg(&logmsg0, commit);
2476 if (err)
2477 return err;
2479 logmsg = logmsg0;
2480 do {
2481 line = strsep(&logmsg, "\n");
2482 if (line)
2483 printf(" %s\n", line);
2484 } while (line);
2485 free(logmsg0);
2487 if (show_patch) {
2488 err = print_patch(commit, id, path, diff_context, repo);
2489 if (err == 0)
2490 printf("\n");
2493 if (fflush(stdout) != 0 && err == NULL)
2494 err = got_error_from_errno("fflush");
2495 return err;
2498 static const struct got_error *
2499 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2500 const char *path, int show_patch, const char *search_pattern,
2501 int diff_context, int limit, int log_branches,
2502 struct got_reflist_head *refs)
2504 const struct got_error *err;
2505 struct got_commit_graph *graph;
2506 regex_t regex;
2507 int have_match;
2509 if (search_pattern &&
2510 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2511 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2513 err = got_commit_graph_open(&graph, path, !log_branches);
2514 if (err)
2515 return err;
2516 err = got_commit_graph_iter_start(graph, root_id, repo,
2517 check_cancelled, NULL);
2518 if (err)
2519 goto done;
2520 for (;;) {
2521 struct got_commit_object *commit;
2522 struct got_object_id *id;
2524 if (sigint_received || sigpipe_received)
2525 break;
2527 err = got_commit_graph_iter_next(&id, graph, repo,
2528 check_cancelled, NULL);
2529 if (err) {
2530 if (err->code == GOT_ERR_ITER_COMPLETED)
2531 err = NULL;
2532 break;
2534 if (id == NULL)
2535 break;
2537 err = got_object_open_as_commit(&commit, repo, id);
2538 if (err)
2539 break;
2541 if (search_pattern) {
2542 err = match_logmsg(&have_match, id, commit, &regex);
2543 if (err) {
2544 got_object_commit_close(commit);
2545 break;
2547 if (have_match == 0) {
2548 got_object_commit_close(commit);
2549 continue;
2553 err = print_commit(commit, id, repo, path, show_patch,
2554 diff_context, refs);
2555 got_object_commit_close(commit);
2556 if (err || (limit && --limit == 0))
2557 break;
2559 done:
2560 if (search_pattern)
2561 regfree(&regex);
2562 got_commit_graph_close(graph);
2563 return err;
2566 __dead static void
2567 usage_log(void)
2569 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2570 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2571 exit(1);
2574 static int
2575 get_default_log_limit(void)
2577 const char *got_default_log_limit;
2578 long long n;
2579 const char *errstr;
2581 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2582 if (got_default_log_limit == NULL)
2583 return 0;
2584 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2585 if (errstr != NULL)
2586 return 0;
2587 return n;
2590 static const struct got_error *
2591 cmd_log(int argc, char *argv[])
2593 const struct got_error *error;
2594 struct got_repository *repo = NULL;
2595 struct got_worktree *worktree = NULL;
2596 struct got_commit_object *commit = NULL;
2597 struct got_object_id *id = NULL;
2598 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2599 const char *start_commit = NULL, *search_pattern = NULL;
2600 int diff_context = -1, ch;
2601 int show_patch = 0, limit = 0, log_branches = 0;
2602 const char *errstr;
2603 struct got_reflist_head refs;
2605 SIMPLEQ_INIT(&refs);
2607 #ifndef PROFILE
2608 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2609 NULL)
2610 == -1)
2611 err(1, "pledge");
2612 #endif
2614 limit = get_default_log_limit();
2616 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2617 switch (ch) {
2618 case 'p':
2619 show_patch = 1;
2620 break;
2621 case 'c':
2622 start_commit = optarg;
2623 break;
2624 case 'C':
2625 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2626 &errstr);
2627 if (errstr != NULL)
2628 err(1, "-C option %s", errstr);
2629 break;
2630 case 'l':
2631 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2632 if (errstr != NULL)
2633 err(1, "-l option %s", errstr);
2634 break;
2635 case 'b':
2636 log_branches = 1;
2637 break;
2638 case 'r':
2639 repo_path = realpath(optarg, NULL);
2640 if (repo_path == NULL)
2641 return got_error_from_errno2("realpath",
2642 optarg);
2643 got_path_strip_trailing_slashes(repo_path);
2644 break;
2645 case 's':
2646 search_pattern = optarg;
2647 break;
2648 default:
2649 usage_log();
2650 /* NOTREACHED */
2654 argc -= optind;
2655 argv += optind;
2657 if (diff_context == -1)
2658 diff_context = 3;
2659 else if (!show_patch)
2660 errx(1, "-C reguires -p");
2662 cwd = getcwd(NULL, 0);
2663 if (cwd == NULL) {
2664 error = got_error_from_errno("getcwd");
2665 goto done;
2668 error = got_worktree_open(&worktree, cwd);
2669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2670 goto done;
2671 error = NULL;
2673 if (argc == 0) {
2674 path = strdup("");
2675 if (path == NULL) {
2676 error = got_error_from_errno("strdup");
2677 goto done;
2679 } else if (argc == 1) {
2680 if (worktree) {
2681 error = got_worktree_resolve_path(&path, worktree,
2682 argv[0]);
2683 if (error)
2684 goto done;
2685 } else {
2686 path = strdup(argv[0]);
2687 if (path == NULL) {
2688 error = got_error_from_errno("strdup");
2689 goto done;
2692 } else
2693 usage_log();
2695 if (repo_path == NULL) {
2696 repo_path = worktree ?
2697 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2699 if (repo_path == NULL) {
2700 error = got_error_from_errno("strdup");
2701 goto done;
2704 error = got_repo_open(&repo, repo_path, NULL);
2705 if (error != NULL)
2706 goto done;
2708 error = apply_unveil(got_repo_get_path(repo), 1,
2709 worktree ? got_worktree_get_root_path(worktree) : NULL);
2710 if (error)
2711 goto done;
2713 if (start_commit == NULL) {
2714 struct got_reference *head_ref;
2715 error = got_ref_open(&head_ref, repo,
2716 worktree ? got_worktree_get_head_ref_name(worktree)
2717 : GOT_REF_HEAD, 0);
2718 if (error != NULL)
2719 return error;
2720 error = got_ref_resolve(&id, repo, head_ref);
2721 got_ref_close(head_ref);
2722 if (error != NULL)
2723 return error;
2724 error = got_object_open_as_commit(&commit, repo, id);
2725 } else {
2726 struct got_reference *ref;
2727 error = got_ref_open(&ref, repo, start_commit, 0);
2728 if (error == NULL) {
2729 int obj_type;
2730 error = got_ref_resolve(&id, repo, ref);
2731 got_ref_close(ref);
2732 if (error != NULL)
2733 goto done;
2734 error = got_object_get_type(&obj_type, repo, id);
2735 if (error != NULL)
2736 goto done;
2737 if (obj_type == GOT_OBJ_TYPE_TAG) {
2738 struct got_tag_object *tag;
2739 error = got_object_open_as_tag(&tag, repo, id);
2740 if (error != NULL)
2741 goto done;
2742 if (got_object_tag_get_object_type(tag) !=
2743 GOT_OBJ_TYPE_COMMIT) {
2744 got_object_tag_close(tag);
2745 error = got_error(GOT_ERR_OBJ_TYPE);
2746 goto done;
2748 free(id);
2749 id = got_object_id_dup(
2750 got_object_tag_get_object_id(tag));
2751 if (id == NULL)
2752 error = got_error_from_errno(
2753 "got_object_id_dup");
2754 got_object_tag_close(tag);
2755 if (error)
2756 goto done;
2757 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2758 error = got_error(GOT_ERR_OBJ_TYPE);
2759 goto done;
2761 error = got_object_open_as_commit(&commit, repo, id);
2762 if (error != NULL)
2763 goto done;
2765 if (commit == NULL) {
2766 error = got_repo_match_object_id_prefix(&id,
2767 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2768 if (error != NULL)
2769 return error;
2772 if (error != NULL)
2773 goto done;
2775 if (worktree) {
2776 const char *prefix = got_worktree_get_path_prefix(worktree);
2777 char *p;
2778 if (asprintf(&p, "%s%s%s", prefix,
2779 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2780 error = got_error_from_errno("asprintf");
2781 goto done;
2783 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2784 free(p);
2785 } else
2786 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2787 if (error != NULL)
2788 goto done;
2789 if (in_repo_path) {
2790 free(path);
2791 path = in_repo_path;
2794 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2795 if (error)
2796 goto done;
2798 error = print_commits(id, repo, path, show_patch, search_pattern,
2799 diff_context, limit, log_branches, &refs);
2800 done:
2801 free(path);
2802 free(repo_path);
2803 free(cwd);
2804 free(id);
2805 if (worktree)
2806 got_worktree_close(worktree);
2807 if (repo) {
2808 const struct got_error *repo_error;
2809 repo_error = got_repo_close(repo);
2810 if (error == NULL)
2811 error = repo_error;
2813 got_ref_list_free(&refs);
2814 return error;
2817 __dead static void
2818 usage_diff(void)
2820 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2821 "[-w] [object1 object2 | path]\n", getprogname());
2822 exit(1);
2825 struct print_diff_arg {
2826 struct got_repository *repo;
2827 struct got_worktree *worktree;
2828 int diff_context;
2829 const char *id_str;
2830 int header_shown;
2831 int diff_staged;
2832 int ignore_whitespace;
2835 static const struct got_error *
2836 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2837 const char *path, struct got_object_id *blob_id,
2838 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2839 int dirfd, const char *de_name)
2841 struct print_diff_arg *a = arg;
2842 const struct got_error *err = NULL;
2843 struct got_blob_object *blob1 = NULL;
2844 int fd = -1;
2845 FILE *f2 = NULL;
2846 char *abspath = NULL, *label1 = NULL;
2847 struct stat sb;
2849 if (a->diff_staged) {
2850 if (staged_status != GOT_STATUS_MODIFY &&
2851 staged_status != GOT_STATUS_ADD &&
2852 staged_status != GOT_STATUS_DELETE)
2853 return NULL;
2854 } else {
2855 if (staged_status == GOT_STATUS_DELETE)
2856 return NULL;
2857 if (status == GOT_STATUS_NONEXISTENT)
2858 return got_error_set_errno(ENOENT, path);
2859 if (status != GOT_STATUS_MODIFY &&
2860 status != GOT_STATUS_ADD &&
2861 status != GOT_STATUS_DELETE &&
2862 status != GOT_STATUS_CONFLICT)
2863 return NULL;
2866 if (!a->header_shown) {
2867 printf("diff %s %s%s\n", a->id_str,
2868 got_worktree_get_root_path(a->worktree),
2869 a->diff_staged ? " (staged changes)" : "");
2870 a->header_shown = 1;
2873 if (a->diff_staged) {
2874 const char *label1 = NULL, *label2 = NULL;
2875 switch (staged_status) {
2876 case GOT_STATUS_MODIFY:
2877 label1 = path;
2878 label2 = path;
2879 break;
2880 case GOT_STATUS_ADD:
2881 label2 = path;
2882 break;
2883 case GOT_STATUS_DELETE:
2884 label1 = path;
2885 break;
2886 default:
2887 return got_error(GOT_ERR_FILE_STATUS);
2889 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2890 label1, label2, a->diff_context, a->ignore_whitespace,
2891 a->repo, stdout);
2894 if (staged_status == GOT_STATUS_ADD ||
2895 staged_status == GOT_STATUS_MODIFY) {
2896 char *id_str;
2897 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2898 8192);
2899 if (err)
2900 goto done;
2901 err = got_object_id_str(&id_str, staged_blob_id);
2902 if (err)
2903 goto done;
2904 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2905 err = got_error_from_errno("asprintf");
2906 free(id_str);
2907 goto done;
2909 free(id_str);
2910 } else if (status != GOT_STATUS_ADD) {
2911 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2912 if (err)
2913 goto done;
2916 if (status != GOT_STATUS_DELETE) {
2917 if (asprintf(&abspath, "%s/%s",
2918 got_worktree_get_root_path(a->worktree), path) == -1) {
2919 err = got_error_from_errno("asprintf");
2920 goto done;
2923 if (dirfd != -1) {
2924 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2925 if (fd == -1) {
2926 err = got_error_from_errno2("openat", abspath);
2927 goto done;
2929 } else {
2930 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2931 if (fd == -1) {
2932 err = got_error_from_errno2("open", abspath);
2933 goto done;
2936 if (fstat(fd, &sb) == -1) {
2937 err = got_error_from_errno2("fstat", abspath);
2938 goto done;
2940 f2 = fdopen(fd, "r");
2941 if (f2 == NULL) {
2942 err = got_error_from_errno2("fdopen", abspath);
2943 goto done;
2945 fd = -1;
2946 } else
2947 sb.st_size = 0;
2949 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2950 a->diff_context, a->ignore_whitespace, stdout);
2951 done:
2952 if (blob1)
2953 got_object_blob_close(blob1);
2954 if (f2 && fclose(f2) == EOF && err == NULL)
2955 err = got_error_from_errno("fclose");
2956 if (fd != -1 && close(fd) == -1 && err == NULL)
2957 err = got_error_from_errno("close");
2958 free(abspath);
2959 return err;
2962 static const struct got_error *
2963 cmd_diff(int argc, char *argv[])
2965 const struct got_error *error;
2966 struct got_repository *repo = NULL;
2967 struct got_worktree *worktree = NULL;
2968 char *cwd = NULL, *repo_path = NULL;
2969 struct got_object_id *id1 = NULL, *id2 = NULL;
2970 const char *id_str1 = NULL, *id_str2 = NULL;
2971 char *label1 = NULL, *label2 = NULL;
2972 int type1, type2;
2973 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2974 const char *errstr;
2975 char *path = NULL;
2977 #ifndef PROFILE
2978 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2979 NULL) == -1)
2980 err(1, "pledge");
2981 #endif
2983 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2984 switch (ch) {
2985 case 'C':
2986 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2987 &errstr);
2988 if (errstr != NULL)
2989 err(1, "-C option %s", errstr);
2990 break;
2991 case 'r':
2992 repo_path = realpath(optarg, NULL);
2993 if (repo_path == NULL)
2994 return got_error_from_errno2("realpath",
2995 optarg);
2996 got_path_strip_trailing_slashes(repo_path);
2997 break;
2998 case 's':
2999 diff_staged = 1;
3000 break;
3001 case 'w':
3002 ignore_whitespace = 1;
3003 break;
3004 default:
3005 usage_diff();
3006 /* NOTREACHED */
3010 argc -= optind;
3011 argv += optind;
3013 cwd = getcwd(NULL, 0);
3014 if (cwd == NULL) {
3015 error = got_error_from_errno("getcwd");
3016 goto done;
3018 error = got_worktree_open(&worktree, cwd);
3019 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3020 goto done;
3021 if (argc <= 1) {
3022 if (worktree == NULL) {
3023 error = got_error(GOT_ERR_NOT_WORKTREE);
3024 goto done;
3026 if (repo_path)
3027 errx(1,
3028 "-r option can't be used when diffing a work tree");
3029 repo_path = strdup(got_worktree_get_repo_path(worktree));
3030 if (repo_path == NULL) {
3031 error = got_error_from_errno("strdup");
3032 goto done;
3034 if (argc == 1) {
3035 error = got_worktree_resolve_path(&path, worktree,
3036 argv[0]);
3037 if (error)
3038 goto done;
3039 } else {
3040 path = strdup("");
3041 if (path == NULL) {
3042 error = got_error_from_errno("strdup");
3043 goto done;
3046 } else if (argc == 2) {
3047 if (diff_staged)
3048 errx(1, "-s option can't be used when diffing "
3049 "objects in repository");
3050 id_str1 = argv[0];
3051 id_str2 = argv[1];
3052 if (worktree && repo_path == NULL) {
3053 repo_path =
3054 strdup(got_worktree_get_repo_path(worktree));
3055 if (repo_path == NULL) {
3056 error = got_error_from_errno("strdup");
3057 goto done;
3060 } else
3061 usage_diff();
3063 if (repo_path == NULL) {
3064 repo_path = getcwd(NULL, 0);
3065 if (repo_path == NULL)
3066 return got_error_from_errno("getcwd");
3069 error = got_repo_open(&repo, repo_path, NULL);
3070 free(repo_path);
3071 if (error != NULL)
3072 goto done;
3074 error = apply_unveil(got_repo_get_path(repo), 1,
3075 worktree ? got_worktree_get_root_path(worktree) : NULL);
3076 if (error)
3077 goto done;
3079 if (argc <= 1) {
3080 struct print_diff_arg arg;
3081 struct got_pathlist_head paths;
3082 char *id_str;
3084 TAILQ_INIT(&paths);
3086 error = got_object_id_str(&id_str,
3087 got_worktree_get_base_commit_id(worktree));
3088 if (error)
3089 goto done;
3090 arg.repo = repo;
3091 arg.worktree = worktree;
3092 arg.diff_context = diff_context;
3093 arg.id_str = id_str;
3094 arg.header_shown = 0;
3095 arg.diff_staged = diff_staged;
3096 arg.ignore_whitespace = ignore_whitespace;
3098 error = got_pathlist_append(&paths, path, NULL);
3099 if (error)
3100 goto done;
3102 error = got_worktree_status(worktree, &paths, repo, print_diff,
3103 &arg, check_cancelled, NULL);
3104 free(id_str);
3105 got_pathlist_free(&paths);
3106 goto done;
3109 error = got_repo_match_object_id(&id1, &label1, id_str1,
3110 GOT_OBJ_TYPE_ANY, 1, repo);
3111 if (error)
3112 goto done;
3114 error = got_repo_match_object_id(&id2, &label2, id_str2,
3115 GOT_OBJ_TYPE_ANY, 1, repo);
3116 if (error)
3117 goto done;
3119 error = got_object_get_type(&type1, repo, id1);
3120 if (error)
3121 goto done;
3123 error = got_object_get_type(&type2, repo, id2);
3124 if (error)
3125 goto done;
3127 if (type1 != type2) {
3128 error = got_error(GOT_ERR_OBJ_TYPE);
3129 goto done;
3132 switch (type1) {
3133 case GOT_OBJ_TYPE_BLOB:
3134 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3135 diff_context, ignore_whitespace, repo, stdout);
3136 break;
3137 case GOT_OBJ_TYPE_TREE:
3138 error = got_diff_objects_as_trees(id1, id2, "", "",
3139 diff_context, ignore_whitespace, repo, stdout);
3140 break;
3141 case GOT_OBJ_TYPE_COMMIT:
3142 printf("diff %s %s\n", label1, label2);
3143 error = got_diff_objects_as_commits(id1, id2, diff_context,
3144 ignore_whitespace, repo, stdout);
3145 break;
3146 default:
3147 error = got_error(GOT_ERR_OBJ_TYPE);
3149 done:
3150 free(label1);
3151 free(label2);
3152 free(id1);
3153 free(id2);
3154 free(path);
3155 if (worktree)
3156 got_worktree_close(worktree);
3157 if (repo) {
3158 const struct got_error *repo_error;
3159 repo_error = got_repo_close(repo);
3160 if (error == NULL)
3161 error = repo_error;
3163 return error;
3166 __dead static void
3167 usage_blame(void)
3169 fprintf(stderr,
3170 "usage: %s blame [-c commit] [-r repository-path] path\n",
3171 getprogname());
3172 exit(1);
3175 struct blame_line {
3176 int annotated;
3177 char *id_str;
3178 char *committer;
3179 char datebuf[11]; /* YYYY-MM-DD + NUL */
3182 struct blame_cb_args {
3183 struct blame_line *lines;
3184 int nlines;
3185 int nlines_prec;
3186 int lineno_cur;
3187 off_t *line_offsets;
3188 FILE *f;
3189 struct got_repository *repo;
3192 static const struct got_error *
3193 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3195 const struct got_error *err = NULL;
3196 struct blame_cb_args *a = arg;
3197 struct blame_line *bline;
3198 char *line = NULL;
3199 size_t linesize = 0;
3200 struct got_commit_object *commit = NULL;
3201 off_t offset;
3202 struct tm tm;
3203 time_t committer_time;
3205 if (nlines != a->nlines ||
3206 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3207 return got_error(GOT_ERR_RANGE);
3209 if (sigint_received)
3210 return got_error(GOT_ERR_ITER_COMPLETED);
3212 if (lineno == -1)
3213 return NULL; /* no change in this commit */
3215 /* Annotate this line. */
3216 bline = &a->lines[lineno - 1];
3217 if (bline->annotated)
3218 return NULL;
3219 err = got_object_id_str(&bline->id_str, id);
3220 if (err)
3221 return err;
3223 err = got_object_open_as_commit(&commit, a->repo, id);
3224 if (err)
3225 goto done;
3227 bline->committer = strdup(got_object_commit_get_committer(commit));
3228 if (bline->committer == NULL) {
3229 err = got_error_from_errno("strdup");
3230 goto done;
3233 committer_time = got_object_commit_get_committer_time(commit);
3234 if (localtime_r(&committer_time, &tm) == NULL)
3235 return got_error_from_errno("localtime_r");
3236 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3237 &tm) >= sizeof(bline->datebuf)) {
3238 err = got_error(GOT_ERR_NO_SPACE);
3239 goto done;
3241 bline->annotated = 1;
3243 /* Print lines annotated so far. */
3244 bline = &a->lines[a->lineno_cur - 1];
3245 if (!bline->annotated)
3246 goto done;
3248 offset = a->line_offsets[a->lineno_cur - 1];
3249 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3250 err = got_error_from_errno("fseeko");
3251 goto done;
3254 while (bline->annotated) {
3255 char *smallerthan, *at, *nl, *committer;
3256 size_t len;
3258 if (getline(&line, &linesize, a->f) == -1) {
3259 if (ferror(a->f))
3260 err = got_error_from_errno("getline");
3261 break;
3264 committer = bline->committer;
3265 smallerthan = strchr(committer, '<');
3266 if (smallerthan && smallerthan[1] != '\0')
3267 committer = smallerthan + 1;
3268 at = strchr(committer, '@');
3269 if (at)
3270 *at = '\0';
3271 len = strlen(committer);
3272 if (len >= 9)
3273 committer[8] = '\0';
3275 nl = strchr(line, '\n');
3276 if (nl)
3277 *nl = '\0';
3278 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3279 bline->id_str, bline->datebuf, committer, line);
3281 a->lineno_cur++;
3282 bline = &a->lines[a->lineno_cur - 1];
3284 done:
3285 if (commit)
3286 got_object_commit_close(commit);
3287 free(line);
3288 return err;
3291 static const struct got_error *
3292 cmd_blame(int argc, char *argv[])
3294 const struct got_error *error;
3295 struct got_repository *repo = NULL;
3296 struct got_worktree *worktree = NULL;
3297 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3298 struct got_object_id *obj_id = NULL;
3299 struct got_object_id *commit_id = NULL;
3300 struct got_blob_object *blob = NULL;
3301 char *commit_id_str = NULL;
3302 struct blame_cb_args bca;
3303 int ch, obj_type, i;
3304 size_t filesize;
3306 memset(&bca, 0, sizeof(bca));
3308 #ifndef PROFILE
3309 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3310 NULL) == -1)
3311 err(1, "pledge");
3312 #endif
3314 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3315 switch (ch) {
3316 case 'c':
3317 commit_id_str = optarg;
3318 break;
3319 case 'r':
3320 repo_path = realpath(optarg, NULL);
3321 if (repo_path == NULL)
3322 return got_error_from_errno2("realpath",
3323 optarg);
3324 got_path_strip_trailing_slashes(repo_path);
3325 break;
3326 default:
3327 usage_blame();
3328 /* NOTREACHED */
3332 argc -= optind;
3333 argv += optind;
3335 if (argc == 1)
3336 path = argv[0];
3337 else
3338 usage_blame();
3340 cwd = getcwd(NULL, 0);
3341 if (cwd == NULL) {
3342 error = got_error_from_errno("getcwd");
3343 goto done;
3345 if (repo_path == NULL) {
3346 error = got_worktree_open(&worktree, cwd);
3347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3348 goto done;
3349 else
3350 error = NULL;
3351 if (worktree) {
3352 repo_path =
3353 strdup(got_worktree_get_repo_path(worktree));
3354 if (repo_path == NULL)
3355 error = got_error_from_errno("strdup");
3356 if (error)
3357 goto done;
3358 } else {
3359 repo_path = strdup(cwd);
3360 if (repo_path == NULL) {
3361 error = got_error_from_errno("strdup");
3362 goto done;
3367 error = got_repo_open(&repo, repo_path, NULL);
3368 if (error != NULL)
3369 goto done;
3371 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3372 if (error)
3373 goto done;
3375 if (worktree) {
3376 const char *prefix = got_worktree_get_path_prefix(worktree);
3377 char *p, *worktree_subdir = cwd +
3378 strlen(got_worktree_get_root_path(worktree));
3379 if (asprintf(&p, "%s%s%s%s%s",
3380 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3381 worktree_subdir, worktree_subdir[0] ? "/" : "",
3382 path) == -1) {
3383 error = got_error_from_errno("asprintf");
3384 goto done;
3386 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3387 free(p);
3388 } else {
3389 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3391 if (error)
3392 goto done;
3394 if (commit_id_str == NULL) {
3395 struct got_reference *head_ref;
3396 error = got_ref_open(&head_ref, repo, worktree ?
3397 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3398 if (error != NULL)
3399 goto done;
3400 error = got_ref_resolve(&commit_id, repo, head_ref);
3401 got_ref_close(head_ref);
3402 if (error != NULL)
3403 goto done;
3404 } else {
3405 error = got_repo_match_object_id(&commit_id, NULL,
3406 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3407 if (error)
3408 goto done;
3411 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3412 if (error)
3413 goto done;
3415 error = got_object_get_type(&obj_type, repo, obj_id);
3416 if (error)
3417 goto done;
3419 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3420 error = got_error(GOT_ERR_OBJ_TYPE);
3421 goto done;
3424 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3425 if (error)
3426 goto done;
3427 bca.f = got_opentemp();
3428 if (bca.f == NULL) {
3429 error = got_error_from_errno("got_opentemp");
3430 goto done;
3432 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3433 &bca.line_offsets, bca.f, blob);
3434 if (error || bca.nlines == 0)
3435 goto done;
3437 /* Don't include \n at EOF in the blame line count. */
3438 if (bca.line_offsets[bca.nlines - 1] == filesize)
3439 bca.nlines--;
3441 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3442 if (bca.lines == NULL) {
3443 error = got_error_from_errno("calloc");
3444 goto done;
3446 bca.lineno_cur = 1;
3447 bca.nlines_prec = 0;
3448 i = bca.nlines;
3449 while (i > 0) {
3450 i /= 10;
3451 bca.nlines_prec++;
3453 bca.repo = repo;
3455 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3456 check_cancelled, NULL);
3457 done:
3458 free(in_repo_path);
3459 free(repo_path);
3460 free(cwd);
3461 free(commit_id);
3462 free(obj_id);
3463 if (blob)
3464 got_object_blob_close(blob);
3465 if (worktree)
3466 got_worktree_close(worktree);
3467 if (repo) {
3468 const struct got_error *repo_error;
3469 repo_error = got_repo_close(repo);
3470 if (error == NULL)
3471 error = repo_error;
3473 if (bca.lines) {
3474 for (i = 0; i < bca.nlines; i++) {
3475 struct blame_line *bline = &bca.lines[i];
3476 free(bline->id_str);
3477 free(bline->committer);
3479 free(bca.lines);
3481 free(bca.line_offsets);
3482 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3483 error = got_error_from_errno("fclose");
3484 return error;
3487 __dead static void
3488 usage_tree(void)
3490 fprintf(stderr,
3491 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3492 getprogname());
3493 exit(1);
3496 static void
3497 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3498 const char *root_path)
3500 int is_root_path = (strcmp(path, root_path) == 0);
3501 const char *modestr = "";
3502 mode_t mode = got_tree_entry_get_mode(te);
3504 path += strlen(root_path);
3505 while (path[0] == '/')
3506 path++;
3508 if (got_object_tree_entry_is_submodule(te))
3509 modestr = "$";
3510 else if (S_ISLNK(mode))
3511 modestr = "@";
3512 else if (S_ISDIR(mode))
3513 modestr = "/";
3514 else if (mode & S_IXUSR)
3515 modestr = "*";
3517 printf("%s%s%s%s%s\n", id ? id : "", path,
3518 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3521 static const struct got_error *
3522 print_tree(const char *path, struct got_object_id *commit_id,
3523 int show_ids, int recurse, const char *root_path,
3524 struct got_repository *repo)
3526 const struct got_error *err = NULL;
3527 struct got_object_id *tree_id = NULL;
3528 struct got_tree_object *tree = NULL;
3529 int nentries, i;
3531 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3532 if (err)
3533 goto done;
3535 err = got_object_open_as_tree(&tree, repo, tree_id);
3536 if (err)
3537 goto done;
3538 nentries = got_object_tree_get_nentries(tree);
3539 for (i = 0; i < nentries; i++) {
3540 struct got_tree_entry *te;
3541 char *id = NULL;
3543 if (sigint_received || sigpipe_received)
3544 break;
3546 te = got_object_tree_get_entry(tree, i);
3547 if (show_ids) {
3548 char *id_str;
3549 err = got_object_id_str(&id_str,
3550 got_tree_entry_get_id(te));
3551 if (err)
3552 goto done;
3553 if (asprintf(&id, "%s ", id_str) == -1) {
3554 err = got_error_from_errno("asprintf");
3555 free(id_str);
3556 goto done;
3558 free(id_str);
3560 print_entry(te, id, path, root_path);
3561 free(id);
3563 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3564 char *child_path;
3565 if (asprintf(&child_path, "%s%s%s", path,
3566 path[0] == '/' && path[1] == '\0' ? "" : "/",
3567 got_tree_entry_get_name(te)) == -1) {
3568 err = got_error_from_errno("asprintf");
3569 goto done;
3571 err = print_tree(child_path, commit_id, show_ids, 1,
3572 root_path, repo);
3573 free(child_path);
3574 if (err)
3575 goto done;
3578 done:
3579 if (tree)
3580 got_object_tree_close(tree);
3581 free(tree_id);
3582 return err;
3585 static const struct got_error *
3586 cmd_tree(int argc, char *argv[])
3588 const struct got_error *error;
3589 struct got_repository *repo = NULL;
3590 struct got_worktree *worktree = NULL;
3591 const char *path;
3592 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3593 struct got_object_id *commit_id = NULL;
3594 char *commit_id_str = NULL;
3595 int show_ids = 0, recurse = 0;
3596 int ch;
3598 #ifndef PROFILE
3599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3600 NULL) == -1)
3601 err(1, "pledge");
3602 #endif
3604 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3605 switch (ch) {
3606 case 'c':
3607 commit_id_str = optarg;
3608 break;
3609 case 'r':
3610 repo_path = realpath(optarg, NULL);
3611 if (repo_path == NULL)
3612 return got_error_from_errno2("realpath",
3613 optarg);
3614 got_path_strip_trailing_slashes(repo_path);
3615 break;
3616 case 'i':
3617 show_ids = 1;
3618 break;
3619 case 'R':
3620 recurse = 1;
3621 break;
3622 default:
3623 usage_tree();
3624 /* NOTREACHED */
3628 argc -= optind;
3629 argv += optind;
3631 if (argc == 1)
3632 path = argv[0];
3633 else if (argc > 1)
3634 usage_tree();
3635 else
3636 path = NULL;
3638 cwd = getcwd(NULL, 0);
3639 if (cwd == NULL) {
3640 error = got_error_from_errno("getcwd");
3641 goto done;
3643 if (repo_path == NULL) {
3644 error = got_worktree_open(&worktree, cwd);
3645 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3646 goto done;
3647 else
3648 error = NULL;
3649 if (worktree) {
3650 repo_path =
3651 strdup(got_worktree_get_repo_path(worktree));
3652 if (repo_path == NULL)
3653 error = got_error_from_errno("strdup");
3654 if (error)
3655 goto done;
3656 } else {
3657 repo_path = strdup(cwd);
3658 if (repo_path == NULL) {
3659 error = got_error_from_errno("strdup");
3660 goto done;
3665 error = got_repo_open(&repo, repo_path, NULL);
3666 if (error != NULL)
3667 goto done;
3669 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3670 if (error)
3671 goto done;
3673 if (path == NULL) {
3674 if (worktree) {
3675 char *p, *worktree_subdir = cwd +
3676 strlen(got_worktree_get_root_path(worktree));
3677 if (asprintf(&p, "%s/%s",
3678 got_worktree_get_path_prefix(worktree),
3679 worktree_subdir) == -1) {
3680 error = got_error_from_errno("asprintf");
3681 goto done;
3683 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3684 free(p);
3685 if (error)
3686 goto done;
3687 } else
3688 path = "/";
3690 if (in_repo_path == NULL) {
3691 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3692 if (error != NULL)
3693 goto done;
3696 if (commit_id_str == NULL) {
3697 struct got_reference *head_ref;
3698 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3699 if (error != NULL)
3700 goto done;
3701 error = got_ref_resolve(&commit_id, repo, head_ref);
3702 got_ref_close(head_ref);
3703 if (error != NULL)
3704 goto done;
3705 } else {
3706 error = got_repo_match_object_id(&commit_id, NULL,
3707 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3708 if (error)
3709 goto done;
3712 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3713 in_repo_path, repo);
3714 done:
3715 free(in_repo_path);
3716 free(repo_path);
3717 free(cwd);
3718 free(commit_id);
3719 if (worktree)
3720 got_worktree_close(worktree);
3721 if (repo) {
3722 const struct got_error *repo_error;
3723 repo_error = got_repo_close(repo);
3724 if (error == NULL)
3725 error = repo_error;
3727 return error;
3730 __dead static void
3731 usage_status(void)
3733 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3734 exit(1);
3737 static const struct got_error *
3738 print_status(void *arg, unsigned char status, unsigned char staged_status,
3739 const char *path, struct got_object_id *blob_id,
3740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 int dirfd, const char *de_name)
3743 if (status == staged_status && (status == GOT_STATUS_DELETE))
3744 status = GOT_STATUS_NO_CHANGE;
3745 printf("%c%c %s\n", status, staged_status, path);
3746 return NULL;
3749 static const struct got_error *
3750 cmd_status(int argc, char *argv[])
3752 const struct got_error *error = NULL;
3753 struct got_repository *repo = NULL;
3754 struct got_worktree *worktree = NULL;
3755 char *cwd = NULL;
3756 struct got_pathlist_head paths;
3757 struct got_pathlist_entry *pe;
3758 int ch;
3760 TAILQ_INIT(&paths);
3762 while ((ch = getopt(argc, argv, "")) != -1) {
3763 switch (ch) {
3764 default:
3765 usage_status();
3766 /* NOTREACHED */
3770 argc -= optind;
3771 argv += optind;
3773 #ifndef PROFILE
3774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3775 NULL) == -1)
3776 err(1, "pledge");
3777 #endif
3778 cwd = getcwd(NULL, 0);
3779 if (cwd == NULL) {
3780 error = got_error_from_errno("getcwd");
3781 goto done;
3784 error = got_worktree_open(&worktree, cwd);
3785 if (error != NULL)
3786 goto done;
3788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3789 NULL);
3790 if (error != NULL)
3791 goto done;
3793 error = apply_unveil(got_repo_get_path(repo), 1,
3794 got_worktree_get_root_path(worktree));
3795 if (error)
3796 goto done;
3798 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3799 if (error)
3800 goto done;
3802 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3803 check_cancelled, NULL);
3804 done:
3805 TAILQ_FOREACH(pe, &paths, entry)
3806 free((char *)pe->path);
3807 got_pathlist_free(&paths);
3808 free(cwd);
3809 return error;
3812 __dead static void
3813 usage_ref(void)
3815 fprintf(stderr,
3816 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3817 getprogname());
3818 exit(1);
3821 static const struct got_error *
3822 list_refs(struct got_repository *repo)
3824 static const struct got_error *err = NULL;
3825 struct got_reflist_head refs;
3826 struct got_reflist_entry *re;
3828 SIMPLEQ_INIT(&refs);
3829 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3830 if (err)
3831 return err;
3833 SIMPLEQ_FOREACH(re, &refs, entry) {
3834 char *refstr;
3835 refstr = got_ref_to_str(re->ref);
3836 if (refstr == NULL)
3837 return got_error_from_errno("got_ref_to_str");
3838 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3839 free(refstr);
3842 got_ref_list_free(&refs);
3843 return NULL;
3846 static const struct got_error *
3847 delete_ref(struct got_repository *repo, const char *refname)
3849 const struct got_error *err = NULL;
3850 struct got_reference *ref;
3852 err = got_ref_open(&ref, repo, refname, 0);
3853 if (err)
3854 return err;
3856 err = got_ref_delete(ref, repo);
3857 got_ref_close(ref);
3858 return err;
3861 static const struct got_error *
3862 add_ref(struct got_repository *repo, const char *refname, const char *target)
3864 const struct got_error *err = NULL;
3865 struct got_object_id *id;
3866 struct got_reference *ref = NULL;
3869 * Don't let the user create a reference name with a leading '-'.
3870 * While technically a valid reference name, this case is usually
3871 * an unintended typo.
3873 if (refname[0] == '-')
3874 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3876 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3877 repo);
3878 if (err) {
3879 struct got_reference *target_ref;
3881 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3882 return err;
3883 err = got_ref_open(&target_ref, repo, target, 0);
3884 if (err)
3885 return err;
3886 err = got_ref_resolve(&id, repo, target_ref);
3887 got_ref_close(target_ref);
3888 if (err)
3889 return err;
3892 err = got_ref_alloc(&ref, refname, id);
3893 if (err)
3894 goto done;
3896 err = got_ref_write(ref, repo);
3897 done:
3898 if (ref)
3899 got_ref_close(ref);
3900 free(id);
3901 return err;
3904 static const struct got_error *
3905 add_symref(struct got_repository *repo, const char *refname, const char *target)
3907 const struct got_error *err = NULL;
3908 struct got_reference *ref = NULL;
3909 struct got_reference *target_ref = NULL;
3912 * Don't let the user create a reference name with a leading '-'.
3913 * While technically a valid reference name, this case is usually
3914 * an unintended typo.
3916 if (refname[0] == '-')
3917 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3919 err = got_ref_open(&target_ref, repo, target, 0);
3920 if (err)
3921 return err;
3923 err = got_ref_alloc_symref(&ref, refname, target_ref);
3924 if (err)
3925 goto done;
3927 err = got_ref_write(ref, repo);
3928 done:
3929 if (target_ref)
3930 got_ref_close(target_ref);
3931 if (ref)
3932 got_ref_close(ref);
3933 return err;
3936 static const struct got_error *
3937 cmd_ref(int argc, char *argv[])
3939 const struct got_error *error = NULL;
3940 struct got_repository *repo = NULL;
3941 struct got_worktree *worktree = NULL;
3942 char *cwd = NULL, *repo_path = NULL;
3943 int ch, do_list = 0, create_symref = 0;
3944 const char *delref = NULL;
3946 /* TODO: Add -s option for adding symbolic references. */
3947 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3948 switch (ch) {
3949 case 'd':
3950 delref = optarg;
3951 break;
3952 case 'r':
3953 repo_path = realpath(optarg, NULL);
3954 if (repo_path == NULL)
3955 return got_error_from_errno2("realpath",
3956 optarg);
3957 got_path_strip_trailing_slashes(repo_path);
3958 break;
3959 case 'l':
3960 do_list = 1;
3961 break;
3962 case 's':
3963 create_symref = 1;
3964 break;
3965 default:
3966 usage_ref();
3967 /* NOTREACHED */
3971 if (do_list && delref)
3972 errx(1, "-l and -d options are mutually exclusive\n");
3974 argc -= optind;
3975 argv += optind;
3977 if (do_list || delref) {
3978 if (create_symref)
3979 errx(1, "-s option cannot be used together with the "
3980 "-l or -d options");
3981 if (argc > 0)
3982 usage_ref();
3983 } else if (argc != 2)
3984 usage_ref();
3986 #ifndef PROFILE
3987 if (do_list) {
3988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3989 NULL) == -1)
3990 err(1, "pledge");
3991 } else {
3992 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3993 "sendfd unveil", NULL) == -1)
3994 err(1, "pledge");
3996 #endif
3997 cwd = getcwd(NULL, 0);
3998 if (cwd == NULL) {
3999 error = got_error_from_errno("getcwd");
4000 goto done;
4003 if (repo_path == NULL) {
4004 error = got_worktree_open(&worktree, cwd);
4005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4006 goto done;
4007 else
4008 error = NULL;
4009 if (worktree) {
4010 repo_path =
4011 strdup(got_worktree_get_repo_path(worktree));
4012 if (repo_path == NULL)
4013 error = got_error_from_errno("strdup");
4014 if (error)
4015 goto done;
4016 } else {
4017 repo_path = strdup(cwd);
4018 if (repo_path == NULL) {
4019 error = got_error_from_errno("strdup");
4020 goto done;
4025 error = got_repo_open(&repo, repo_path, NULL);
4026 if (error != NULL)
4027 goto done;
4029 error = apply_unveil(got_repo_get_path(repo), do_list,
4030 worktree ? got_worktree_get_root_path(worktree) : NULL);
4031 if (error)
4032 goto done;
4034 if (do_list)
4035 error = list_refs(repo);
4036 else if (delref)
4037 error = delete_ref(repo, delref);
4038 else if (create_symref)
4039 error = add_symref(repo, argv[0], argv[1]);
4040 else
4041 error = add_ref(repo, argv[0], argv[1]);
4042 done:
4043 if (repo)
4044 got_repo_close(repo);
4045 if (worktree)
4046 got_worktree_close(worktree);
4047 free(cwd);
4048 free(repo_path);
4049 return error;
4052 __dead static void
4053 usage_branch(void)
4055 fprintf(stderr,
4056 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4057 "[name]\n", getprogname());
4058 exit(1);
4061 static const struct got_error *
4062 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4063 struct got_reference *ref)
4065 const struct got_error *err = NULL;
4066 const char *refname, *marker = " ";
4067 char *refstr;
4069 refname = got_ref_get_name(ref);
4070 if (worktree && strcmp(refname,
4071 got_worktree_get_head_ref_name(worktree)) == 0) {
4072 struct got_object_id *id = NULL;
4074 err = got_ref_resolve(&id, repo, ref);
4075 if (err)
4076 return err;
4077 if (got_object_id_cmp(id,
4078 got_worktree_get_base_commit_id(worktree)) == 0)
4079 marker = "* ";
4080 else
4081 marker = "~ ";
4082 free(id);
4085 if (strncmp(refname, "refs/heads/", 11) == 0)
4086 refname += 11;
4087 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4088 refname += 18;
4090 refstr = got_ref_to_str(ref);
4091 if (refstr == NULL)
4092 return got_error_from_errno("got_ref_to_str");
4094 printf("%s%s: %s\n", marker, refname, refstr);
4095 free(refstr);
4096 return NULL;
4099 static const struct got_error *
4100 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4102 const char *refname;
4104 if (worktree == NULL)
4105 return got_error(GOT_ERR_NOT_WORKTREE);
4107 refname = got_worktree_get_head_ref_name(worktree);
4109 if (strncmp(refname, "refs/heads/", 11) == 0)
4110 refname += 11;
4111 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4112 refname += 18;
4114 printf("%s\n", refname);
4116 return NULL;
4119 static const struct got_error *
4120 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4122 static const struct got_error *err = NULL;
4123 struct got_reflist_head refs;
4124 struct got_reflist_entry *re;
4125 struct got_reference *temp_ref = NULL;
4126 int rebase_in_progress, histedit_in_progress;
4128 SIMPLEQ_INIT(&refs);
4130 if (worktree) {
4131 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4132 worktree);
4133 if (err)
4134 return err;
4136 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4137 worktree);
4138 if (err)
4139 return err;
4141 if (rebase_in_progress || histedit_in_progress) {
4142 err = got_ref_open(&temp_ref, repo,
4143 got_worktree_get_head_ref_name(worktree), 0);
4144 if (err)
4145 return err;
4146 list_branch(repo, worktree, temp_ref);
4147 got_ref_close(temp_ref);
4151 err = got_ref_list(&refs, repo, "refs/heads",
4152 got_ref_cmp_by_name, NULL);
4153 if (err)
4154 return err;
4156 SIMPLEQ_FOREACH(re, &refs, entry)
4157 list_branch(repo, worktree, re->ref);
4159 got_ref_list_free(&refs);
4160 return NULL;
4163 static const struct got_error *
4164 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4165 const char *branch_name)
4167 const struct got_error *err = NULL;
4168 struct got_reference *ref = NULL;
4169 char *refname;
4171 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4172 return got_error_from_errno("asprintf");
4174 err = got_ref_open(&ref, repo, refname, 0);
4175 if (err)
4176 goto done;
4178 if (worktree &&
4179 strcmp(got_worktree_get_head_ref_name(worktree),
4180 got_ref_get_name(ref)) == 0) {
4181 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4182 "will not delete this work tree's current branch");
4183 goto done;
4186 err = got_ref_delete(ref, repo);
4187 done:
4188 if (ref)
4189 got_ref_close(ref);
4190 free(refname);
4191 return err;
4194 static const struct got_error *
4195 add_branch(struct got_repository *repo, const char *branch_name,
4196 struct got_object_id *base_commit_id)
4198 const struct got_error *err = NULL;
4199 struct got_reference *ref = NULL;
4200 char *base_refname = NULL, *refname = NULL;
4203 * Don't let the user create a branch name with a leading '-'.
4204 * While technically a valid reference name, this case is usually
4205 * an unintended typo.
4207 if (branch_name[0] == '-')
4208 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4210 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4211 err = got_error_from_errno("asprintf");
4212 goto done;
4215 err = got_ref_open(&ref, repo, refname, 0);
4216 if (err == NULL) {
4217 err = got_error(GOT_ERR_BRANCH_EXISTS);
4218 goto done;
4219 } else if (err->code != GOT_ERR_NOT_REF)
4220 goto done;
4222 err = got_ref_alloc(&ref, refname, base_commit_id);
4223 if (err)
4224 goto done;
4226 err = got_ref_write(ref, repo);
4227 done:
4228 if (ref)
4229 got_ref_close(ref);
4230 free(base_refname);
4231 free(refname);
4232 return err;
4235 static const struct got_error *
4236 cmd_branch(int argc, char *argv[])
4238 const struct got_error *error = NULL;
4239 struct got_repository *repo = NULL;
4240 struct got_worktree *worktree = NULL;
4241 char *cwd = NULL, *repo_path = NULL;
4242 int ch, do_list = 0, do_show = 0, do_update = 1;
4243 const char *delref = NULL, *commit_id_arg = NULL;
4244 struct got_reference *ref = NULL;
4245 struct got_pathlist_head paths;
4246 struct got_pathlist_entry *pe;
4247 struct got_object_id *commit_id = NULL;
4248 char *commit_id_str = NULL;
4250 TAILQ_INIT(&paths);
4252 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4253 switch (ch) {
4254 case 'c':
4255 commit_id_arg = optarg;
4256 break;
4257 case 'd':
4258 delref = optarg;
4259 break;
4260 case 'r':
4261 repo_path = realpath(optarg, NULL);
4262 if (repo_path == NULL)
4263 return got_error_from_errno2("realpath",
4264 optarg);
4265 got_path_strip_trailing_slashes(repo_path);
4266 break;
4267 case 'l':
4268 do_list = 1;
4269 break;
4270 case 'n':
4271 do_update = 0;
4272 break;
4273 default:
4274 usage_branch();
4275 /* NOTREACHED */
4279 if (do_list && delref)
4280 errx(1, "-l and -d options are mutually exclusive\n");
4282 argc -= optind;
4283 argv += optind;
4285 if (!do_list && !delref && argc == 0)
4286 do_show = 1;
4288 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4289 errx(1, "-c option can only be used when creating a branch");
4291 if (do_list || delref) {
4292 if (argc > 0)
4293 usage_branch();
4294 } else if (!do_show && argc != 1)
4295 usage_branch();
4297 #ifndef PROFILE
4298 if (do_list || do_show) {
4299 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4300 NULL) == -1)
4301 err(1, "pledge");
4302 } else {
4303 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4304 "sendfd unveil", NULL) == -1)
4305 err(1, "pledge");
4307 #endif
4308 cwd = getcwd(NULL, 0);
4309 if (cwd == NULL) {
4310 error = got_error_from_errno("getcwd");
4311 goto done;
4314 if (repo_path == NULL) {
4315 error = got_worktree_open(&worktree, cwd);
4316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4317 goto done;
4318 else
4319 error = NULL;
4320 if (worktree) {
4321 repo_path =
4322 strdup(got_worktree_get_repo_path(worktree));
4323 if (repo_path == NULL)
4324 error = got_error_from_errno("strdup");
4325 if (error)
4326 goto done;
4327 } else {
4328 repo_path = strdup(cwd);
4329 if (repo_path == NULL) {
4330 error = got_error_from_errno("strdup");
4331 goto done;
4336 error = got_repo_open(&repo, repo_path, NULL);
4337 if (error != NULL)
4338 goto done;
4340 error = apply_unveil(got_repo_get_path(repo), do_list,
4341 worktree ? got_worktree_get_root_path(worktree) : NULL);
4342 if (error)
4343 goto done;
4345 if (do_show)
4346 error = show_current_branch(repo, worktree);
4347 else if (do_list)
4348 error = list_branches(repo, worktree);
4349 else if (delref)
4350 error = delete_branch(repo, worktree, delref);
4351 else {
4352 if (commit_id_arg == NULL)
4353 commit_id_arg = worktree ?
4354 got_worktree_get_head_ref_name(worktree) :
4355 GOT_REF_HEAD;
4356 error = got_repo_match_object_id(&commit_id, NULL,
4357 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4358 if (error)
4359 goto done;
4360 error = add_branch(repo, argv[0], commit_id);
4361 if (error)
4362 goto done;
4363 if (worktree && do_update) {
4364 int did_something = 0;
4365 char *branch_refname = NULL;
4367 error = got_object_id_str(&commit_id_str, commit_id);
4368 if (error)
4369 goto done;
4370 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4371 worktree);
4372 if (error)
4373 goto done;
4374 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4375 == -1) {
4376 error = got_error_from_errno("asprintf");
4377 goto done;
4379 error = got_ref_open(&ref, repo, branch_refname, 0);
4380 free(branch_refname);
4381 if (error)
4382 goto done;
4383 error = switch_head_ref(ref, commit_id, worktree,
4384 repo);
4385 if (error)
4386 goto done;
4387 error = got_worktree_set_base_commit_id(worktree, repo,
4388 commit_id);
4389 if (error)
4390 goto done;
4391 error = got_worktree_checkout_files(worktree, &paths,
4392 repo, update_progress, &did_something,
4393 check_cancelled, NULL);
4394 if (error)
4395 goto done;
4396 if (did_something)
4397 printf("Updated to commit %s\n", commit_id_str);
4400 done:
4401 if (ref)
4402 got_ref_close(ref);
4403 if (repo)
4404 got_repo_close(repo);
4405 if (worktree)
4406 got_worktree_close(worktree);
4407 free(cwd);
4408 free(repo_path);
4409 free(commit_id);
4410 free(commit_id_str);
4411 TAILQ_FOREACH(pe, &paths, entry)
4412 free((char *)pe->path);
4413 got_pathlist_free(&paths);
4414 return error;
4418 __dead static void
4419 usage_tag(void)
4421 fprintf(stderr,
4422 "usage: %s tag [-c commit] [-r repository] [-l] "
4423 "[-m message] name\n", getprogname());
4424 exit(1);
4427 #if 0
4428 static const struct got_error *
4429 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4431 const struct got_error *err = NULL;
4432 struct got_reflist_entry *re, *se, *new;
4433 struct got_object_id *re_id, *se_id;
4434 struct got_tag_object *re_tag, *se_tag;
4435 time_t re_time, se_time;
4437 SIMPLEQ_FOREACH(re, tags, entry) {
4438 se = SIMPLEQ_FIRST(sorted);
4439 if (se == NULL) {
4440 err = got_reflist_entry_dup(&new, re);
4441 if (err)
4442 return err;
4443 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4444 continue;
4445 } else {
4446 err = got_ref_resolve(&re_id, repo, re->ref);
4447 if (err)
4448 break;
4449 err = got_object_open_as_tag(&re_tag, repo, re_id);
4450 free(re_id);
4451 if (err)
4452 break;
4453 re_time = got_object_tag_get_tagger_time(re_tag);
4454 got_object_tag_close(re_tag);
4457 while (se) {
4458 err = got_ref_resolve(&se_id, repo, re->ref);
4459 if (err)
4460 break;
4461 err = got_object_open_as_tag(&se_tag, repo, se_id);
4462 free(se_id);
4463 if (err)
4464 break;
4465 se_time = got_object_tag_get_tagger_time(se_tag);
4466 got_object_tag_close(se_tag);
4468 if (se_time > re_time) {
4469 err = got_reflist_entry_dup(&new, re);
4470 if (err)
4471 return err;
4472 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4473 break;
4475 se = SIMPLEQ_NEXT(se, entry);
4476 continue;
4479 done:
4480 return err;
4482 #endif
4484 static const struct got_error *
4485 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4487 static const struct got_error *err = NULL;
4488 struct got_reflist_head refs;
4489 struct got_reflist_entry *re;
4491 SIMPLEQ_INIT(&refs);
4493 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4494 if (err)
4495 return err;
4497 SIMPLEQ_FOREACH(re, &refs, entry) {
4498 const char *refname;
4499 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4500 char datebuf[26];
4501 const char *tagger;
4502 time_t tagger_time;
4503 struct got_object_id *id;
4504 struct got_tag_object *tag;
4505 struct got_commit_object *commit = NULL;
4507 refname = got_ref_get_name(re->ref);
4508 if (strncmp(refname, "refs/tags/", 10) != 0)
4509 continue;
4510 refname += 10;
4511 refstr = got_ref_to_str(re->ref);
4512 if (refstr == NULL) {
4513 err = got_error_from_errno("got_ref_to_str");
4514 break;
4516 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4517 free(refstr);
4519 err = got_ref_resolve(&id, repo, re->ref);
4520 if (err)
4521 break;
4522 err = got_object_open_as_tag(&tag, repo, id);
4523 if (err) {
4524 if (err->code != GOT_ERR_OBJ_TYPE) {
4525 free(id);
4526 break;
4528 /* "lightweight" tag */
4529 err = got_object_open_as_commit(&commit, repo, id);
4530 if (err) {
4531 free(id);
4532 break;
4534 tagger = got_object_commit_get_committer(commit);
4535 tagger_time =
4536 got_object_commit_get_committer_time(commit);
4537 err = got_object_id_str(&id_str, id);
4538 free(id);
4539 if (err)
4540 break;
4541 } else {
4542 free(id);
4543 tagger = got_object_tag_get_tagger(tag);
4544 tagger_time = got_object_tag_get_tagger_time(tag);
4545 err = got_object_id_str(&id_str,
4546 got_object_tag_get_object_id(tag));
4547 if (err)
4548 break;
4550 printf("from: %s\n", tagger);
4551 datestr = get_datestr(&tagger_time, datebuf);
4552 if (datestr)
4553 printf("date: %s UTC\n", datestr);
4554 if (commit)
4555 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4556 else {
4557 switch (got_object_tag_get_object_type(tag)) {
4558 case GOT_OBJ_TYPE_BLOB:
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4560 id_str);
4561 break;
4562 case GOT_OBJ_TYPE_TREE:
4563 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4564 id_str);
4565 break;
4566 case GOT_OBJ_TYPE_COMMIT:
4567 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4568 id_str);
4569 break;
4570 case GOT_OBJ_TYPE_TAG:
4571 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4572 id_str);
4573 break;
4574 default:
4575 break;
4578 free(id_str);
4579 if (commit) {
4580 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4581 if (err)
4582 break;
4583 got_object_commit_close(commit);
4584 } else {
4585 tagmsg0 = strdup(got_object_tag_get_message(tag));
4586 got_object_tag_close(tag);
4587 if (tagmsg0 == NULL) {
4588 err = got_error_from_errno("strdup");
4589 break;
4593 tagmsg = tagmsg0;
4594 do {
4595 line = strsep(&tagmsg, "\n");
4596 if (line)
4597 printf(" %s\n", line);
4598 } while (line);
4599 free(tagmsg0);
4602 got_ref_list_free(&refs);
4603 return NULL;
4606 static const struct got_error *
4607 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4608 const char *tag_name, const char *repo_path)
4610 const struct got_error *err = NULL;
4611 char *template = NULL, *initial_content = NULL;
4612 char *editor = NULL;
4613 int fd = -1;
4615 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4616 err = got_error_from_errno("asprintf");
4617 goto done;
4620 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4621 commit_id_str, tag_name) == -1) {
4622 err = got_error_from_errno("asprintf");
4623 goto done;
4626 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4627 if (err)
4628 goto done;
4630 dprintf(fd, initial_content);
4631 close(fd);
4633 err = get_editor(&editor);
4634 if (err)
4635 goto done;
4636 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4637 done:
4638 free(initial_content);
4639 free(template);
4640 free(editor);
4642 /* Editor is done; we can now apply unveil(2) */
4643 if (err == NULL) {
4644 err = apply_unveil(repo_path, 0, NULL);
4645 if (err) {
4646 free(*tagmsg);
4647 *tagmsg = NULL;
4650 return err;
4653 static const struct got_error *
4654 add_tag(struct got_repository *repo, const char *tag_name,
4655 const char *commit_arg, const char *tagmsg_arg)
4657 const struct got_error *err = NULL;
4658 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4659 char *label = NULL, *commit_id_str = NULL;
4660 struct got_reference *ref = NULL;
4661 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4662 char *tagmsg_path = NULL, *tag_id_str = NULL;
4663 int preserve_tagmsg = 0;
4666 * Don't let the user create a tag name with a leading '-'.
4667 * While technically a valid reference name, this case is usually
4668 * an unintended typo.
4670 if (tag_name[0] == '-')
4671 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4673 err = get_author(&tagger, repo);
4674 if (err)
4675 return err;
4677 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4678 GOT_OBJ_TYPE_COMMIT, 1, repo);
4679 if (err)
4680 goto done;
4682 err = got_object_id_str(&commit_id_str, commit_id);
4683 if (err)
4684 goto done;
4686 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4687 refname = strdup(tag_name);
4688 if (refname == NULL) {
4689 err = got_error_from_errno("strdup");
4690 goto done;
4692 tag_name += 10;
4693 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4694 err = got_error_from_errno("asprintf");
4695 goto done;
4698 err = got_ref_open(&ref, repo, refname, 0);
4699 if (err == NULL) {
4700 err = got_error(GOT_ERR_TAG_EXISTS);
4701 goto done;
4702 } else if (err->code != GOT_ERR_NOT_REF)
4703 goto done;
4705 if (tagmsg_arg == NULL) {
4706 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4707 tag_name, got_repo_get_path(repo));
4708 if (err) {
4709 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4710 tagmsg_path != NULL)
4711 preserve_tagmsg = 1;
4712 goto done;
4716 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4717 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4718 if (err) {
4719 if (tagmsg_path)
4720 preserve_tagmsg = 1;
4721 goto done;
4724 err = got_ref_alloc(&ref, refname, tag_id);
4725 if (err) {
4726 if (tagmsg_path)
4727 preserve_tagmsg = 1;
4728 goto done;
4731 err = got_ref_write(ref, repo);
4732 if (err) {
4733 if (tagmsg_path)
4734 preserve_tagmsg = 1;
4735 goto done;
4738 err = got_object_id_str(&tag_id_str, tag_id);
4739 if (err) {
4740 if (tagmsg_path)
4741 preserve_tagmsg = 1;
4742 goto done;
4744 printf("Created tag %s\n", tag_id_str);
4745 done:
4746 if (preserve_tagmsg) {
4747 fprintf(stderr, "%s: tag message preserved in %s\n",
4748 getprogname(), tagmsg_path);
4749 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4750 err = got_error_from_errno2("unlink", tagmsg_path);
4751 free(tag_id_str);
4752 if (ref)
4753 got_ref_close(ref);
4754 free(commit_id);
4755 free(commit_id_str);
4756 free(refname);
4757 free(tagmsg);
4758 free(tagmsg_path);
4759 free(tagger);
4760 return err;
4763 static const struct got_error *
4764 cmd_tag(int argc, char *argv[])
4766 const struct got_error *error = NULL;
4767 struct got_repository *repo = NULL;
4768 struct got_worktree *worktree = NULL;
4769 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4770 char *gitconfig_path = NULL;
4771 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4772 int ch, do_list = 0;
4774 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4775 switch (ch) {
4776 case 'c':
4777 commit_id_arg = optarg;
4778 break;
4779 case 'm':
4780 tagmsg = optarg;
4781 break;
4782 case 'r':
4783 repo_path = realpath(optarg, NULL);
4784 if (repo_path == NULL)
4785 return got_error_from_errno2("realpath",
4786 optarg);
4787 got_path_strip_trailing_slashes(repo_path);
4788 break;
4789 case 'l':
4790 do_list = 1;
4791 break;
4792 default:
4793 usage_tag();
4794 /* NOTREACHED */
4798 argc -= optind;
4799 argv += optind;
4801 if (do_list) {
4802 if (commit_id_arg != NULL)
4803 errx(1, "-c option can only be used when creating a tag");
4804 if (tagmsg)
4805 errx(1, "-l and -m options are mutually exclusive");
4806 if (argc > 0)
4807 usage_tag();
4808 } else if (argc != 1)
4809 usage_tag();
4811 tag_name = argv[0];
4813 #ifndef PROFILE
4814 if (do_list) {
4815 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4816 NULL) == -1)
4817 err(1, "pledge");
4818 } else {
4819 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4820 "sendfd unveil", NULL) == -1)
4821 err(1, "pledge");
4823 #endif
4824 cwd = getcwd(NULL, 0);
4825 if (cwd == NULL) {
4826 error = got_error_from_errno("getcwd");
4827 goto done;
4830 if (repo_path == NULL) {
4831 error = got_worktree_open(&worktree, cwd);
4832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4833 goto done;
4834 else
4835 error = NULL;
4836 if (worktree) {
4837 repo_path =
4838 strdup(got_worktree_get_repo_path(worktree));
4839 if (repo_path == NULL)
4840 error = got_error_from_errno("strdup");
4841 if (error)
4842 goto done;
4843 } else {
4844 repo_path = strdup(cwd);
4845 if (repo_path == NULL) {
4846 error = got_error_from_errno("strdup");
4847 goto done;
4852 if (do_list) {
4853 error = got_repo_open(&repo, repo_path, NULL);
4854 if (error != NULL)
4855 goto done;
4856 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4857 if (error)
4858 goto done;
4859 error = list_tags(repo, worktree);
4860 } else {
4861 error = get_gitconfig_path(&gitconfig_path);
4862 if (error)
4863 goto done;
4864 error = got_repo_open(&repo, repo_path, gitconfig_path);
4865 if (error != NULL)
4866 goto done;
4868 if (tagmsg) {
4869 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4870 if (error)
4871 goto done;
4874 if (commit_id_arg == NULL) {
4875 struct got_reference *head_ref;
4876 struct got_object_id *commit_id;
4877 error = got_ref_open(&head_ref, repo,
4878 worktree ? got_worktree_get_head_ref_name(worktree)
4879 : GOT_REF_HEAD, 0);
4880 if (error)
4881 goto done;
4882 error = got_ref_resolve(&commit_id, repo, head_ref);
4883 got_ref_close(head_ref);
4884 if (error)
4885 goto done;
4886 error = got_object_id_str(&commit_id_str, commit_id);
4887 free(commit_id);
4888 if (error)
4889 goto done;
4892 error = add_tag(repo, tag_name,
4893 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4895 done:
4896 if (repo)
4897 got_repo_close(repo);
4898 if (worktree)
4899 got_worktree_close(worktree);
4900 free(cwd);
4901 free(repo_path);
4902 free(gitconfig_path);
4903 free(commit_id_str);
4904 return error;
4907 __dead static void
4908 usage_add(void)
4910 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4911 getprogname());
4912 exit(1);
4915 static const struct got_error *
4916 add_progress(void *arg, unsigned char status, const char *path)
4918 while (path[0] == '/')
4919 path++;
4920 printf("%c %s\n", status, path);
4921 return NULL;
4924 static const struct got_error *
4925 cmd_add(int argc, char *argv[])
4927 const struct got_error *error = NULL;
4928 struct got_repository *repo = NULL;
4929 struct got_worktree *worktree = NULL;
4930 char *cwd = NULL;
4931 struct got_pathlist_head paths;
4932 struct got_pathlist_entry *pe;
4933 int ch, can_recurse = 0, no_ignores = 0;
4935 TAILQ_INIT(&paths);
4937 while ((ch = getopt(argc, argv, "IR")) != -1) {
4938 switch (ch) {
4939 case 'I':
4940 no_ignores = 1;
4941 break;
4942 case 'R':
4943 can_recurse = 1;
4944 break;
4945 default:
4946 usage_add();
4947 /* NOTREACHED */
4951 argc -= optind;
4952 argv += optind;
4954 #ifndef PROFILE
4955 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4956 NULL) == -1)
4957 err(1, "pledge");
4958 #endif
4959 if (argc < 1)
4960 usage_add();
4962 cwd = getcwd(NULL, 0);
4963 if (cwd == NULL) {
4964 error = got_error_from_errno("getcwd");
4965 goto done;
4968 error = got_worktree_open(&worktree, cwd);
4969 if (error)
4970 goto done;
4972 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4973 NULL);
4974 if (error != NULL)
4975 goto done;
4977 error = apply_unveil(got_repo_get_path(repo), 1,
4978 got_worktree_get_root_path(worktree));
4979 if (error)
4980 goto done;
4982 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4983 if (error)
4984 goto done;
4986 if (!can_recurse && no_ignores) {
4987 error = got_error_msg(GOT_ERR_BAD_PATH,
4988 "disregarding ignores requires -R option");
4989 goto done;
4993 if (!can_recurse) {
4994 char *ondisk_path;
4995 struct stat sb;
4996 TAILQ_FOREACH(pe, &paths, entry) {
4997 if (asprintf(&ondisk_path, "%s/%s",
4998 got_worktree_get_root_path(worktree),
4999 pe->path) == -1) {
5000 error = got_error_from_errno("asprintf");
5001 goto done;
5003 if (lstat(ondisk_path, &sb) == -1) {
5004 if (errno == ENOENT) {
5005 free(ondisk_path);
5006 continue;
5008 error = got_error_from_errno2("lstat",
5009 ondisk_path);
5010 free(ondisk_path);
5011 goto done;
5013 free(ondisk_path);
5014 if (S_ISDIR(sb.st_mode)) {
5015 error = got_error_msg(GOT_ERR_BAD_PATH,
5016 "adding directories requires -R option");
5017 goto done;
5022 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5023 NULL, repo, no_ignores);
5024 done:
5025 if (repo)
5026 got_repo_close(repo);
5027 if (worktree)
5028 got_worktree_close(worktree);
5029 TAILQ_FOREACH(pe, &paths, entry)
5030 free((char *)pe->path);
5031 got_pathlist_free(&paths);
5032 free(cwd);
5033 return error;
5036 __dead static void
5037 usage_remove(void)
5039 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5040 getprogname());
5041 exit(1);
5044 static const struct got_error *
5045 print_remove_status(void *arg, unsigned char status,
5046 unsigned char staged_status, const char *path)
5048 while (path[0] == '/')
5049 path++;
5050 if (status == GOT_STATUS_NONEXISTENT)
5051 return NULL;
5052 if (status == staged_status && (status == GOT_STATUS_DELETE))
5053 status = GOT_STATUS_NO_CHANGE;
5054 printf("%c%c %s\n", status, staged_status, path);
5055 return NULL;
5058 static const struct got_error *
5059 cmd_remove(int argc, char *argv[])
5061 const struct got_error *error = NULL;
5062 struct got_worktree *worktree = NULL;
5063 struct got_repository *repo = NULL;
5064 char *cwd = NULL;
5065 struct got_pathlist_head paths;
5066 struct got_pathlist_entry *pe;
5067 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5069 TAILQ_INIT(&paths);
5071 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5072 switch (ch) {
5073 case 'f':
5074 delete_local_mods = 1;
5075 break;
5076 case 'k':
5077 keep_on_disk = 1;
5078 break;
5079 case 'R':
5080 can_recurse = 1;
5081 break;
5082 default:
5083 usage_remove();
5084 /* NOTREACHED */
5088 argc -= optind;
5089 argv += optind;
5091 #ifndef PROFILE
5092 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5093 NULL) == -1)
5094 err(1, "pledge");
5095 #endif
5096 if (argc < 1)
5097 usage_remove();
5099 cwd = getcwd(NULL, 0);
5100 if (cwd == NULL) {
5101 error = got_error_from_errno("getcwd");
5102 goto done;
5104 error = got_worktree_open(&worktree, cwd);
5105 if (error)
5106 goto done;
5108 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5109 NULL);
5110 if (error)
5111 goto done;
5113 error = apply_unveil(got_repo_get_path(repo), 1,
5114 got_worktree_get_root_path(worktree));
5115 if (error)
5116 goto done;
5118 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5119 if (error)
5120 goto done;
5122 if (!can_recurse) {
5123 char *ondisk_path;
5124 struct stat sb;
5125 TAILQ_FOREACH(pe, &paths, entry) {
5126 if (asprintf(&ondisk_path, "%s/%s",
5127 got_worktree_get_root_path(worktree),
5128 pe->path) == -1) {
5129 error = got_error_from_errno("asprintf");
5130 goto done;
5132 if (lstat(ondisk_path, &sb) == -1) {
5133 if (errno == ENOENT) {
5134 free(ondisk_path);
5135 continue;
5137 error = got_error_from_errno2("lstat",
5138 ondisk_path);
5139 free(ondisk_path);
5140 goto done;
5142 free(ondisk_path);
5143 if (S_ISDIR(sb.st_mode)) {
5144 error = got_error_msg(GOT_ERR_BAD_PATH,
5145 "removing directories requires -R option");
5146 goto done;
5151 error = got_worktree_schedule_delete(worktree, &paths,
5152 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5153 done:
5154 if (repo)
5155 got_repo_close(repo);
5156 if (worktree)
5157 got_worktree_close(worktree);
5158 TAILQ_FOREACH(pe, &paths, entry)
5159 free((char *)pe->path);
5160 got_pathlist_free(&paths);
5161 free(cwd);
5162 return error;
5165 __dead static void
5166 usage_revert(void)
5168 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5169 "path ...\n", getprogname());
5170 exit(1);
5173 static const struct got_error *
5174 revert_progress(void *arg, unsigned char status, const char *path)
5176 if (status == GOT_STATUS_UNVERSIONED)
5177 return NULL;
5179 while (path[0] == '/')
5180 path++;
5181 printf("%c %s\n", status, path);
5182 return NULL;
5185 struct choose_patch_arg {
5186 FILE *patch_script_file;
5187 const char *action;
5190 static const struct got_error *
5191 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5192 int nchanges, const char *action)
5194 char *line = NULL;
5195 size_t linesize = 0;
5196 ssize_t linelen;
5198 switch (status) {
5199 case GOT_STATUS_ADD:
5200 printf("A %s\n%s this addition? [y/n] ", path, action);
5201 break;
5202 case GOT_STATUS_DELETE:
5203 printf("D %s\n%s this deletion? [y/n] ", path, action);
5204 break;
5205 case GOT_STATUS_MODIFY:
5206 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5207 return got_error_from_errno("fseek");
5208 printf(GOT_COMMIT_SEP_STR);
5209 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5210 printf("%s", line);
5211 if (ferror(patch_file))
5212 return got_error_from_errno("getline");
5213 printf(GOT_COMMIT_SEP_STR);
5214 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5215 path, n, nchanges, action);
5216 break;
5217 default:
5218 return got_error_path(path, GOT_ERR_FILE_STATUS);
5221 return NULL;
5224 static const struct got_error *
5225 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5226 FILE *patch_file, int n, int nchanges)
5228 const struct got_error *err = NULL;
5229 char *line = NULL;
5230 size_t linesize = 0;
5231 ssize_t linelen;
5232 int resp = ' ';
5233 struct choose_patch_arg *a = arg;
5235 *choice = GOT_PATCH_CHOICE_NONE;
5237 if (a->patch_script_file) {
5238 char *nl;
5239 err = show_change(status, path, patch_file, n, nchanges,
5240 a->action);
5241 if (err)
5242 return err;
5243 linelen = getline(&line, &linesize, a->patch_script_file);
5244 if (linelen == -1) {
5245 if (ferror(a->patch_script_file))
5246 return got_error_from_errno("getline");
5247 return NULL;
5249 nl = strchr(line, '\n');
5250 if (nl)
5251 *nl = '\0';
5252 if (strcmp(line, "y") == 0) {
5253 *choice = GOT_PATCH_CHOICE_YES;
5254 printf("y\n");
5255 } else if (strcmp(line, "n") == 0) {
5256 *choice = GOT_PATCH_CHOICE_NO;
5257 printf("n\n");
5258 } else if (strcmp(line, "q") == 0 &&
5259 status == GOT_STATUS_MODIFY) {
5260 *choice = GOT_PATCH_CHOICE_QUIT;
5261 printf("q\n");
5262 } else
5263 printf("invalid response '%s'\n", line);
5264 free(line);
5265 return NULL;
5268 while (resp != 'y' && resp != 'n' && resp != 'q') {
5269 err = show_change(status, path, patch_file, n, nchanges,
5270 a->action);
5271 if (err)
5272 return err;
5273 resp = getchar();
5274 if (resp == '\n')
5275 resp = getchar();
5276 if (status == GOT_STATUS_MODIFY) {
5277 if (resp != 'y' && resp != 'n' && resp != 'q') {
5278 printf("invalid response '%c'\n", resp);
5279 resp = ' ';
5281 } else if (resp != 'y' && resp != 'n') {
5282 printf("invalid response '%c'\n", resp);
5283 resp = ' ';
5287 if (resp == 'y')
5288 *choice = GOT_PATCH_CHOICE_YES;
5289 else if (resp == 'n')
5290 *choice = GOT_PATCH_CHOICE_NO;
5291 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5292 *choice = GOT_PATCH_CHOICE_QUIT;
5294 return NULL;
5298 static const struct got_error *
5299 cmd_revert(int argc, char *argv[])
5301 const struct got_error *error = NULL;
5302 struct got_worktree *worktree = NULL;
5303 struct got_repository *repo = NULL;
5304 char *cwd = NULL, *path = NULL;
5305 struct got_pathlist_head paths;
5306 struct got_pathlist_entry *pe;
5307 int ch, can_recurse = 0, pflag = 0;
5308 FILE *patch_script_file = NULL;
5309 const char *patch_script_path = NULL;
5310 struct choose_patch_arg cpa;
5312 TAILQ_INIT(&paths);
5314 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5315 switch (ch) {
5316 case 'p':
5317 pflag = 1;
5318 break;
5319 case 'F':
5320 patch_script_path = optarg;
5321 break;
5322 case 'R':
5323 can_recurse = 1;
5324 break;
5325 default:
5326 usage_revert();
5327 /* NOTREACHED */
5331 argc -= optind;
5332 argv += optind;
5334 #ifndef PROFILE
5335 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5336 "unveil", NULL) == -1)
5337 err(1, "pledge");
5338 #endif
5339 if (argc < 1)
5340 usage_revert();
5341 if (patch_script_path && !pflag)
5342 errx(1, "-F option can only be used together with -p option");
5344 cwd = getcwd(NULL, 0);
5345 if (cwd == NULL) {
5346 error = got_error_from_errno("getcwd");
5347 goto done;
5349 error = got_worktree_open(&worktree, cwd);
5350 if (error)
5351 goto done;
5353 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5354 NULL);
5355 if (error != NULL)
5356 goto done;
5358 if (patch_script_path) {
5359 patch_script_file = fopen(patch_script_path, "r");
5360 if (patch_script_file == NULL) {
5361 error = got_error_from_errno2("fopen",
5362 patch_script_path);
5363 goto done;
5366 error = apply_unveil(got_repo_get_path(repo), 1,
5367 got_worktree_get_root_path(worktree));
5368 if (error)
5369 goto done;
5371 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5372 if (error)
5373 goto done;
5375 if (!can_recurse) {
5376 char *ondisk_path;
5377 struct stat sb;
5378 TAILQ_FOREACH(pe, &paths, entry) {
5379 if (asprintf(&ondisk_path, "%s/%s",
5380 got_worktree_get_root_path(worktree),
5381 pe->path) == -1) {
5382 error = got_error_from_errno("asprintf");
5383 goto done;
5385 if (lstat(ondisk_path, &sb) == -1) {
5386 if (errno == ENOENT) {
5387 free(ondisk_path);
5388 continue;
5390 error = got_error_from_errno2("lstat",
5391 ondisk_path);
5392 free(ondisk_path);
5393 goto done;
5395 free(ondisk_path);
5396 if (S_ISDIR(sb.st_mode)) {
5397 error = got_error_msg(GOT_ERR_BAD_PATH,
5398 "reverting directories requires -R option");
5399 goto done;
5404 cpa.patch_script_file = patch_script_file;
5405 cpa.action = "revert";
5406 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5407 pflag ? choose_patch : NULL, &cpa, repo);
5408 done:
5409 if (patch_script_file && fclose(patch_script_file) == EOF &&
5410 error == NULL)
5411 error = got_error_from_errno2("fclose", patch_script_path);
5412 if (repo)
5413 got_repo_close(repo);
5414 if (worktree)
5415 got_worktree_close(worktree);
5416 free(path);
5417 free(cwd);
5418 return error;
5421 __dead static void
5422 usage_commit(void)
5424 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5425 getprogname());
5426 exit(1);
5429 struct collect_commit_logmsg_arg {
5430 const char *cmdline_log;
5431 const char *editor;
5432 const char *worktree_path;
5433 const char *branch_name;
5434 const char *repo_path;
5435 char *logmsg_path;
5439 static const struct got_error *
5440 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5441 void *arg)
5443 char *initial_content = NULL;
5444 struct got_pathlist_entry *pe;
5445 const struct got_error *err = NULL;
5446 char *template = NULL;
5447 struct collect_commit_logmsg_arg *a = arg;
5448 int fd;
5449 size_t len;
5451 /* if a message was specified on the command line, just use it */
5452 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5453 len = strlen(a->cmdline_log) + 1;
5454 *logmsg = malloc(len + 1);
5455 if (*logmsg == NULL)
5456 return got_error_from_errno("malloc");
5457 strlcpy(*logmsg, a->cmdline_log, len);
5458 return NULL;
5461 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5462 return got_error_from_errno("asprintf");
5464 if (asprintf(&initial_content,
5465 "\n# changes to be committed on branch %s:\n",
5466 a->branch_name) == -1)
5467 return got_error_from_errno("asprintf");
5469 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5470 if (err)
5471 goto done;
5473 dprintf(fd, initial_content);
5475 TAILQ_FOREACH(pe, commitable_paths, entry) {
5476 struct got_commitable *ct = pe->data;
5477 dprintf(fd, "# %c %s\n",
5478 got_commitable_get_status(ct),
5479 got_commitable_get_path(ct));
5481 close(fd);
5483 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5484 done:
5485 free(initial_content);
5486 free(template);
5488 /* Editor is done; we can now apply unveil(2) */
5489 if (err == NULL) {
5490 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5491 if (err) {
5492 free(*logmsg);
5493 *logmsg = NULL;
5496 return err;
5499 static const struct got_error *
5500 cmd_commit(int argc, char *argv[])
5502 const struct got_error *error = NULL;
5503 struct got_worktree *worktree = NULL;
5504 struct got_repository *repo = NULL;
5505 char *cwd = NULL, *id_str = NULL;
5506 struct got_object_id *id = NULL;
5507 const char *logmsg = NULL;
5508 struct collect_commit_logmsg_arg cl_arg;
5509 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5510 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5511 struct got_pathlist_head paths;
5513 TAILQ_INIT(&paths);
5514 cl_arg.logmsg_path = NULL;
5516 while ((ch = getopt(argc, argv, "m:")) != -1) {
5517 switch (ch) {
5518 case 'm':
5519 logmsg = optarg;
5520 break;
5521 default:
5522 usage_commit();
5523 /* NOTREACHED */
5527 argc -= optind;
5528 argv += optind;
5530 #ifndef PROFILE
5531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5532 "unveil", NULL) == -1)
5533 err(1, "pledge");
5534 #endif
5535 cwd = getcwd(NULL, 0);
5536 if (cwd == NULL) {
5537 error = got_error_from_errno("getcwd");
5538 goto done;
5540 error = got_worktree_open(&worktree, cwd);
5541 if (error)
5542 goto done;
5544 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5545 if (error)
5546 goto done;
5547 if (rebase_in_progress) {
5548 error = got_error(GOT_ERR_REBASING);
5549 goto done;
5552 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5553 worktree);
5554 if (error)
5555 goto done;
5557 error = get_gitconfig_path(&gitconfig_path);
5558 if (error)
5559 goto done;
5560 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5561 gitconfig_path);
5562 if (error != NULL)
5563 goto done;
5565 error = get_author(&author, repo);
5566 if (error)
5567 return error;
5570 * unveil(2) traverses exec(2); if an editor is used we have
5571 * to apply unveil after the log message has been written.
5573 if (logmsg == NULL || strlen(logmsg) == 0)
5574 error = get_editor(&editor);
5575 else
5576 error = apply_unveil(got_repo_get_path(repo), 0,
5577 got_worktree_get_root_path(worktree));
5578 if (error)
5579 goto done;
5581 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5582 if (error)
5583 goto done;
5585 cl_arg.editor = editor;
5586 cl_arg.cmdline_log = logmsg;
5587 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5588 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5589 if (!histedit_in_progress) {
5590 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5591 error = got_error(GOT_ERR_COMMIT_BRANCH);
5592 goto done;
5594 cl_arg.branch_name += 11;
5596 cl_arg.repo_path = got_repo_get_path(repo);
5597 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5598 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5599 if (error) {
5600 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5601 cl_arg.logmsg_path != NULL)
5602 preserve_logmsg = 1;
5603 goto done;
5606 error = got_object_id_str(&id_str, id);
5607 if (error)
5608 goto done;
5609 printf("Created commit %s\n", id_str);
5610 done:
5611 if (preserve_logmsg) {
5612 fprintf(stderr, "%s: log message preserved in %s\n",
5613 getprogname(), cl_arg.logmsg_path);
5614 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5615 error == NULL)
5616 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5617 free(cl_arg.logmsg_path);
5618 if (repo)
5619 got_repo_close(repo);
5620 if (worktree)
5621 got_worktree_close(worktree);
5622 free(cwd);
5623 free(id_str);
5624 free(gitconfig_path);
5625 free(editor);
5626 free(author);
5627 return error;
5630 __dead static void
5631 usage_cherrypick(void)
5633 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5634 exit(1);
5637 static const struct got_error *
5638 cmd_cherrypick(int argc, char *argv[])
5640 const struct got_error *error = NULL;
5641 struct got_worktree *worktree = NULL;
5642 struct got_repository *repo = NULL;
5643 char *cwd = NULL, *commit_id_str = NULL;
5644 struct got_object_id *commit_id = NULL;
5645 struct got_commit_object *commit = NULL;
5646 struct got_object_qid *pid;
5647 struct got_reference *head_ref = NULL;
5648 int ch, did_something = 0;
5650 while ((ch = getopt(argc, argv, "")) != -1) {
5651 switch (ch) {
5652 default:
5653 usage_cherrypick();
5654 /* NOTREACHED */
5658 argc -= optind;
5659 argv += optind;
5661 #ifndef PROFILE
5662 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 "unveil", NULL) == -1)
5664 err(1, "pledge");
5665 #endif
5666 if (argc != 1)
5667 usage_cherrypick();
5669 cwd = getcwd(NULL, 0);
5670 if (cwd == NULL) {
5671 error = got_error_from_errno("getcwd");
5672 goto done;
5674 error = got_worktree_open(&worktree, cwd);
5675 if (error)
5676 goto done;
5678 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5679 NULL);
5680 if (error != NULL)
5681 goto done;
5683 error = apply_unveil(got_repo_get_path(repo), 0,
5684 got_worktree_get_root_path(worktree));
5685 if (error)
5686 goto done;
5688 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5689 GOT_OBJ_TYPE_COMMIT, repo);
5690 if (error != NULL) {
5691 struct got_reference *ref;
5692 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5693 goto done;
5694 error = got_ref_open(&ref, repo, argv[0], 0);
5695 if (error != NULL)
5696 goto done;
5697 error = got_ref_resolve(&commit_id, repo, ref);
5698 got_ref_close(ref);
5699 if (error != NULL)
5700 goto done;
5702 error = got_object_id_str(&commit_id_str, commit_id);
5703 if (error)
5704 goto done;
5706 error = got_ref_open(&head_ref, repo,
5707 got_worktree_get_head_ref_name(worktree), 0);
5708 if (error != NULL)
5709 goto done;
5711 error = check_same_branch(commit_id, head_ref, NULL, repo);
5712 if (error) {
5713 if (error->code != GOT_ERR_ANCESTRY)
5714 goto done;
5715 error = NULL;
5716 } else {
5717 error = got_error(GOT_ERR_SAME_BRANCH);
5718 goto done;
5721 error = got_object_open_as_commit(&commit, repo, commit_id);
5722 if (error)
5723 goto done;
5724 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5725 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5726 commit_id, repo, update_progress, &did_something, check_cancelled,
5727 NULL);
5728 if (error != NULL)
5729 goto done;
5731 if (did_something)
5732 printf("Merged commit %s\n", commit_id_str);
5733 done:
5734 if (commit)
5735 got_object_commit_close(commit);
5736 free(commit_id_str);
5737 if (head_ref)
5738 got_ref_close(head_ref);
5739 if (worktree)
5740 got_worktree_close(worktree);
5741 if (repo)
5742 got_repo_close(repo);
5743 return error;
5746 __dead static void
5747 usage_backout(void)
5749 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5750 exit(1);
5753 static const struct got_error *
5754 cmd_backout(int argc, char *argv[])
5756 const struct got_error *error = NULL;
5757 struct got_worktree *worktree = NULL;
5758 struct got_repository *repo = NULL;
5759 char *cwd = NULL, *commit_id_str = NULL;
5760 struct got_object_id *commit_id = NULL;
5761 struct got_commit_object *commit = NULL;
5762 struct got_object_qid *pid;
5763 struct got_reference *head_ref = NULL;
5764 int ch, did_something = 0;
5766 while ((ch = getopt(argc, argv, "")) != -1) {
5767 switch (ch) {
5768 default:
5769 usage_backout();
5770 /* NOTREACHED */
5774 argc -= optind;
5775 argv += optind;
5777 #ifndef PROFILE
5778 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 "unveil", NULL) == -1)
5780 err(1, "pledge");
5781 #endif
5782 if (argc != 1)
5783 usage_backout();
5785 cwd = getcwd(NULL, 0);
5786 if (cwd == NULL) {
5787 error = got_error_from_errno("getcwd");
5788 goto done;
5790 error = got_worktree_open(&worktree, cwd);
5791 if (error)
5792 goto done;
5794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5795 NULL);
5796 if (error != NULL)
5797 goto done;
5799 error = apply_unveil(got_repo_get_path(repo), 0,
5800 got_worktree_get_root_path(worktree));
5801 if (error)
5802 goto done;
5804 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5805 GOT_OBJ_TYPE_COMMIT, repo);
5806 if (error != NULL) {
5807 struct got_reference *ref;
5808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5809 goto done;
5810 error = got_ref_open(&ref, repo, argv[0], 0);
5811 if (error != NULL)
5812 goto done;
5813 error = got_ref_resolve(&commit_id, repo, ref);
5814 got_ref_close(ref);
5815 if (error != NULL)
5816 goto done;
5818 error = got_object_id_str(&commit_id_str, commit_id);
5819 if (error)
5820 goto done;
5822 error = got_ref_open(&head_ref, repo,
5823 got_worktree_get_head_ref_name(worktree), 0);
5824 if (error != NULL)
5825 goto done;
5827 error = check_same_branch(commit_id, head_ref, NULL, repo);
5828 if (error)
5829 goto done;
5831 error = got_object_open_as_commit(&commit, repo, commit_id);
5832 if (error)
5833 goto done;
5834 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5835 if (pid == NULL) {
5836 error = got_error(GOT_ERR_ROOT_COMMIT);
5837 goto done;
5840 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5841 update_progress, &did_something, check_cancelled, NULL);
5842 if (error != NULL)
5843 goto done;
5845 if (did_something)
5846 printf("Backed out commit %s\n", commit_id_str);
5847 done:
5848 if (commit)
5849 got_object_commit_close(commit);
5850 free(commit_id_str);
5851 if (head_ref)
5852 got_ref_close(head_ref);
5853 if (worktree)
5854 got_worktree_close(worktree);
5855 if (repo)
5856 got_repo_close(repo);
5857 return error;
5860 __dead static void
5861 usage_rebase(void)
5863 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5864 getprogname());
5865 exit(1);
5868 void
5869 trim_logmsg(char *logmsg, int limit)
5871 char *nl;
5872 size_t len;
5874 len = strlen(logmsg);
5875 if (len > limit)
5876 len = limit;
5877 logmsg[len] = '\0';
5878 nl = strchr(logmsg, '\n');
5879 if (nl)
5880 *nl = '\0';
5883 static const struct got_error *
5884 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5886 const struct got_error *err;
5887 char *logmsg0 = NULL;
5888 const char *s;
5890 err = got_object_commit_get_logmsg(&logmsg0, commit);
5891 if (err)
5892 return err;
5894 s = logmsg0;
5895 while (isspace((unsigned char)s[0]))
5896 s++;
5898 *logmsg = strdup(s);
5899 if (*logmsg == NULL) {
5900 err = got_error_from_errno("strdup");
5901 goto done;
5904 trim_logmsg(*logmsg, limit);
5905 done:
5906 free(logmsg0);
5907 return err;
5910 static const struct got_error *
5911 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5913 const struct got_error *err;
5914 struct got_commit_object *commit = NULL;
5915 char *id_str = NULL, *logmsg = NULL;
5917 err = got_object_open_as_commit(&commit, repo, id);
5918 if (err)
5919 return err;
5921 err = got_object_id_str(&id_str, id);
5922 if (err)
5923 goto done;
5925 id_str[12] = '\0';
5927 err = get_short_logmsg(&logmsg, 42, commit);
5928 if (err)
5929 goto done;
5931 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5932 done:
5933 free(id_str);
5934 got_object_commit_close(commit);
5935 free(logmsg);
5936 return err;
5939 static const struct got_error *
5940 show_rebase_progress(struct got_commit_object *commit,
5941 struct got_object_id *old_id, struct got_object_id *new_id)
5943 const struct got_error *err;
5944 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5946 err = got_object_id_str(&old_id_str, old_id);
5947 if (err)
5948 goto done;
5950 if (new_id) {
5951 err = got_object_id_str(&new_id_str, new_id);
5952 if (err)
5953 goto done;
5956 old_id_str[12] = '\0';
5957 if (new_id_str)
5958 new_id_str[12] = '\0';
5960 err = get_short_logmsg(&logmsg, 42, commit);
5961 if (err)
5962 goto done;
5964 printf("%s -> %s: %s\n", old_id_str,
5965 new_id_str ? new_id_str : "no-op change", logmsg);
5966 done:
5967 free(old_id_str);
5968 free(new_id_str);
5969 free(logmsg);
5970 return err;
5973 static const struct got_error *
5974 rebase_progress(void *arg, unsigned char status, const char *path)
5976 unsigned char *rebase_status = arg;
5978 while (path[0] == '/')
5979 path++;
5980 printf("%c %s\n", status, path);
5982 if (*rebase_status == GOT_STATUS_CONFLICT)
5983 return NULL;
5984 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5985 *rebase_status = status;
5986 return NULL;
5989 static const struct got_error *
5990 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5991 struct got_reference *branch, struct got_reference *new_base_branch,
5992 struct got_reference *tmp_branch, struct got_repository *repo)
5994 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5995 return got_worktree_rebase_complete(worktree, fileindex,
5996 new_base_branch, tmp_branch, branch, repo);
5999 static const struct got_error *
6000 rebase_commit(struct got_pathlist_head *merged_paths,
6001 struct got_worktree *worktree, struct got_fileindex *fileindex,
6002 struct got_reference *tmp_branch,
6003 struct got_object_id *commit_id, struct got_repository *repo)
6005 const struct got_error *error;
6006 struct got_commit_object *commit;
6007 struct got_object_id *new_commit_id;
6009 error = got_object_open_as_commit(&commit, repo, commit_id);
6010 if (error)
6011 return error;
6013 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6014 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6015 if (error) {
6016 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6017 goto done;
6018 error = show_rebase_progress(commit, commit_id, NULL);
6019 } else {
6020 error = show_rebase_progress(commit, commit_id, new_commit_id);
6021 free(new_commit_id);
6023 done:
6024 got_object_commit_close(commit);
6025 return error;
6028 struct check_path_prefix_arg {
6029 const char *path_prefix;
6030 size_t len;
6031 int errcode;
6034 static const struct got_error *
6035 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6036 struct got_blob_object *blob2, struct got_object_id *id1,
6037 struct got_object_id *id2, const char *path1, const char *path2,
6038 mode_t mode1, mode_t mode2, struct got_repository *repo)
6040 struct check_path_prefix_arg *a = arg;
6042 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6043 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6044 return got_error(a->errcode);
6046 return NULL;
6049 static const struct got_error *
6050 check_path_prefix(struct got_object_id *parent_id,
6051 struct got_object_id *commit_id, const char *path_prefix,
6052 int errcode, struct got_repository *repo)
6054 const struct got_error *err;
6055 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6056 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6057 struct check_path_prefix_arg cpp_arg;
6059 if (got_path_is_root_dir(path_prefix))
6060 return NULL;
6062 err = got_object_open_as_commit(&commit, repo, commit_id);
6063 if (err)
6064 goto done;
6066 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6067 if (err)
6068 goto done;
6070 err = got_object_open_as_tree(&tree1, repo,
6071 got_object_commit_get_tree_id(parent_commit));
6072 if (err)
6073 goto done;
6075 err = got_object_open_as_tree(&tree2, repo,
6076 got_object_commit_get_tree_id(commit));
6077 if (err)
6078 goto done;
6080 cpp_arg.path_prefix = path_prefix;
6081 while (cpp_arg.path_prefix[0] == '/')
6082 cpp_arg.path_prefix++;
6083 cpp_arg.len = strlen(cpp_arg.path_prefix);
6084 cpp_arg.errcode = errcode;
6085 err = got_diff_tree(tree1, tree2, "", "", repo,
6086 check_path_prefix_in_diff, &cpp_arg, 0);
6087 done:
6088 if (tree1)
6089 got_object_tree_close(tree1);
6090 if (tree2)
6091 got_object_tree_close(tree2);
6092 if (commit)
6093 got_object_commit_close(commit);
6094 if (parent_commit)
6095 got_object_commit_close(parent_commit);
6096 return err;
6099 static const struct got_error *
6100 collect_commits(struct got_object_id_queue *commits,
6101 struct got_object_id *initial_commit_id,
6102 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6103 const char *path_prefix, int path_prefix_errcode,
6104 struct got_repository *repo)
6106 const struct got_error *err = NULL;
6107 struct got_commit_graph *graph = NULL;
6108 struct got_object_id *parent_id = NULL;
6109 struct got_object_qid *qid;
6110 struct got_object_id *commit_id = initial_commit_id;
6112 err = got_commit_graph_open(&graph, "/", 1);
6113 if (err)
6114 return err;
6116 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6117 check_cancelled, NULL);
6118 if (err)
6119 goto done;
6120 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6121 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6122 check_cancelled, NULL);
6123 if (err) {
6124 if (err->code == GOT_ERR_ITER_COMPLETED) {
6125 err = got_error_msg(GOT_ERR_ANCESTRY,
6126 "ran out of commits to rebase before "
6127 "youngest common ancestor commit has "
6128 "been reached?!?");
6130 goto done;
6131 } else {
6132 err = check_path_prefix(parent_id, commit_id,
6133 path_prefix, path_prefix_errcode, repo);
6134 if (err)
6135 goto done;
6137 err = got_object_qid_alloc(&qid, commit_id);
6138 if (err)
6139 goto done;
6140 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6141 commit_id = parent_id;
6144 done:
6145 got_commit_graph_close(graph);
6146 return err;
6149 static const struct got_error *
6150 cmd_rebase(int argc, char *argv[])
6152 const struct got_error *error = NULL;
6153 struct got_worktree *worktree = NULL;
6154 struct got_repository *repo = NULL;
6155 struct got_fileindex *fileindex = NULL;
6156 char *cwd = NULL;
6157 struct got_reference *branch = NULL;
6158 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6159 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6160 struct got_object_id *resume_commit_id = NULL;
6161 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6162 struct got_commit_object *commit = NULL;
6163 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6164 int histedit_in_progress = 0;
6165 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6166 struct got_object_id_queue commits;
6167 struct got_pathlist_head merged_paths;
6168 const struct got_object_id_queue *parent_ids;
6169 struct got_object_qid *qid, *pid;
6171 SIMPLEQ_INIT(&commits);
6172 TAILQ_INIT(&merged_paths);
6174 while ((ch = getopt(argc, argv, "ac")) != -1) {
6175 switch (ch) {
6176 case 'a':
6177 abort_rebase = 1;
6178 break;
6179 case 'c':
6180 continue_rebase = 1;
6181 break;
6182 default:
6183 usage_rebase();
6184 /* NOTREACHED */
6188 argc -= optind;
6189 argv += optind;
6191 #ifndef PROFILE
6192 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6193 "unveil", NULL) == -1)
6194 err(1, "pledge");
6195 #endif
6196 if (abort_rebase && continue_rebase)
6197 usage_rebase();
6198 else if (abort_rebase || continue_rebase) {
6199 if (argc != 0)
6200 usage_rebase();
6201 } else if (argc != 1)
6202 usage_rebase();
6204 cwd = getcwd(NULL, 0);
6205 if (cwd == NULL) {
6206 error = got_error_from_errno("getcwd");
6207 goto done;
6209 error = got_worktree_open(&worktree, cwd);
6210 if (error)
6211 goto done;
6213 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6214 NULL);
6215 if (error != NULL)
6216 goto done;
6218 error = apply_unveil(got_repo_get_path(repo), 0,
6219 got_worktree_get_root_path(worktree));
6220 if (error)
6221 goto done;
6223 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6224 worktree);
6225 if (error)
6226 goto done;
6227 if (histedit_in_progress) {
6228 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6229 goto done;
6232 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6233 if (error)
6234 goto done;
6236 if (abort_rebase) {
6237 int did_something;
6238 if (!rebase_in_progress) {
6239 error = got_error(GOT_ERR_NOT_REBASING);
6240 goto done;
6242 error = got_worktree_rebase_continue(&resume_commit_id,
6243 &new_base_branch, &tmp_branch, &branch, &fileindex,
6244 worktree, repo);
6245 if (error)
6246 goto done;
6247 printf("Switching work tree to %s\n",
6248 got_ref_get_symref_target(new_base_branch));
6249 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6250 new_base_branch, update_progress, &did_something);
6251 if (error)
6252 goto done;
6253 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6254 goto done; /* nothing else to do */
6257 if (continue_rebase) {
6258 if (!rebase_in_progress) {
6259 error = got_error(GOT_ERR_NOT_REBASING);
6260 goto done;
6262 error = got_worktree_rebase_continue(&resume_commit_id,
6263 &new_base_branch, &tmp_branch, &branch, &fileindex,
6264 worktree, repo);
6265 if (error)
6266 goto done;
6268 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6269 resume_commit_id, repo);
6270 if (error)
6271 goto done;
6273 yca_id = got_object_id_dup(resume_commit_id);
6274 if (yca_id == NULL) {
6275 error = got_error_from_errno("got_object_id_dup");
6276 goto done;
6278 } else {
6279 error = got_ref_open(&branch, repo, argv[0], 0);
6280 if (error != NULL)
6281 goto done;
6284 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6285 if (error)
6286 goto done;
6288 if (!continue_rebase) {
6289 struct got_object_id *base_commit_id;
6291 base_commit_id = got_worktree_get_base_commit_id(worktree);
6292 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6293 base_commit_id, branch_head_commit_id, repo,
6294 check_cancelled, NULL);
6295 if (error)
6296 goto done;
6297 if (yca_id == NULL) {
6298 error = got_error_msg(GOT_ERR_ANCESTRY,
6299 "specified branch shares no common ancestry "
6300 "with work tree's branch");
6301 goto done;
6304 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6305 if (error) {
6306 if (error->code != GOT_ERR_ANCESTRY)
6307 goto done;
6308 error = NULL;
6309 } else {
6310 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6311 "specified branch resolves to a commit which "
6312 "is already contained in work tree's branch");
6313 goto done;
6315 error = got_worktree_rebase_prepare(&new_base_branch,
6316 &tmp_branch, &fileindex, worktree, branch, repo);
6317 if (error)
6318 goto done;
6321 commit_id = branch_head_commit_id;
6322 error = got_object_open_as_commit(&commit, repo, commit_id);
6323 if (error)
6324 goto done;
6326 parent_ids = got_object_commit_get_parent_ids(commit);
6327 pid = SIMPLEQ_FIRST(parent_ids);
6328 if (pid == NULL) {
6329 if (!continue_rebase) {
6330 int did_something;
6331 error = got_worktree_rebase_abort(worktree, fileindex,
6332 repo, new_base_branch, update_progress,
6333 &did_something);
6334 if (error)
6335 goto done;
6336 printf("Rebase of %s aborted\n",
6337 got_ref_get_name(branch));
6339 error = got_error(GOT_ERR_EMPTY_REBASE);
6340 goto done;
6342 error = collect_commits(&commits, commit_id, pid->id,
6343 yca_id, got_worktree_get_path_prefix(worktree),
6344 GOT_ERR_REBASE_PATH, repo);
6345 got_object_commit_close(commit);
6346 commit = NULL;
6347 if (error)
6348 goto done;
6350 if (SIMPLEQ_EMPTY(&commits)) {
6351 if (continue_rebase) {
6352 error = rebase_complete(worktree, fileindex,
6353 branch, new_base_branch, tmp_branch, repo);
6354 goto done;
6355 } else {
6356 /* Fast-forward the reference of the branch. */
6357 struct got_object_id *new_head_commit_id;
6358 char *id_str;
6359 error = got_ref_resolve(&new_head_commit_id, repo,
6360 new_base_branch);
6361 if (error)
6362 goto done;
6363 error = got_object_id_str(&id_str, new_head_commit_id);
6364 printf("Forwarding %s to commit %s\n",
6365 got_ref_get_name(branch), id_str);
6366 free(id_str);
6367 error = got_ref_change_ref(branch,
6368 new_head_commit_id);
6369 if (error)
6370 goto done;
6374 pid = NULL;
6375 SIMPLEQ_FOREACH(qid, &commits, entry) {
6376 commit_id = qid->id;
6377 parent_id = pid ? pid->id : yca_id;
6378 pid = qid;
6380 error = got_worktree_rebase_merge_files(&merged_paths,
6381 worktree, fileindex, parent_id, commit_id, repo,
6382 rebase_progress, &rebase_status, check_cancelled, NULL);
6383 if (error)
6384 goto done;
6386 if (rebase_status == GOT_STATUS_CONFLICT) {
6387 error = show_rebase_merge_conflict(qid->id, repo);
6388 if (error)
6389 goto done;
6390 got_worktree_rebase_pathlist_free(&merged_paths);
6391 break;
6394 error = rebase_commit(&merged_paths, worktree, fileindex,
6395 tmp_branch, commit_id, repo);
6396 got_worktree_rebase_pathlist_free(&merged_paths);
6397 if (error)
6398 goto done;
6401 if (rebase_status == GOT_STATUS_CONFLICT) {
6402 error = got_worktree_rebase_postpone(worktree, fileindex);
6403 if (error)
6404 goto done;
6405 error = got_error_msg(GOT_ERR_CONFLICTS,
6406 "conflicts must be resolved before rebasing can continue");
6407 } else
6408 error = rebase_complete(worktree, fileindex, branch,
6409 new_base_branch, tmp_branch, repo);
6410 done:
6411 got_object_id_queue_free(&commits);
6412 free(branch_head_commit_id);
6413 free(resume_commit_id);
6414 free(yca_id);
6415 if (commit)
6416 got_object_commit_close(commit);
6417 if (branch)
6418 got_ref_close(branch);
6419 if (new_base_branch)
6420 got_ref_close(new_base_branch);
6421 if (tmp_branch)
6422 got_ref_close(tmp_branch);
6423 if (worktree)
6424 got_worktree_close(worktree);
6425 if (repo)
6426 got_repo_close(repo);
6427 return error;
6430 __dead static void
6431 usage_histedit(void)
6433 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6434 getprogname());
6435 exit(1);
6438 #define GOT_HISTEDIT_PICK 'p'
6439 #define GOT_HISTEDIT_EDIT 'e'
6440 #define GOT_HISTEDIT_FOLD 'f'
6441 #define GOT_HISTEDIT_DROP 'd'
6442 #define GOT_HISTEDIT_MESG 'm'
6444 static struct got_histedit_cmd {
6445 unsigned char code;
6446 const char *name;
6447 const char *desc;
6448 } got_histedit_cmds[] = {
6449 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6450 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6451 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6452 "be used" },
6453 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6454 { GOT_HISTEDIT_MESG, "mesg",
6455 "single-line log message for commit above (open editor if empty)" },
6458 struct got_histedit_list_entry {
6459 TAILQ_ENTRY(got_histedit_list_entry) entry;
6460 struct got_object_id *commit_id;
6461 const struct got_histedit_cmd *cmd;
6462 char *logmsg;
6464 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6466 static const struct got_error *
6467 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6468 FILE *f, struct got_repository *repo)
6470 const struct got_error *err = NULL;
6471 char *logmsg = NULL, *id_str = NULL;
6472 struct got_commit_object *commit = NULL;
6473 int n;
6475 err = got_object_open_as_commit(&commit, repo, commit_id);
6476 if (err)
6477 goto done;
6479 err = get_short_logmsg(&logmsg, 34, commit);
6480 if (err)
6481 goto done;
6483 err = got_object_id_str(&id_str, commit_id);
6484 if (err)
6485 goto done;
6487 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6488 if (n < 0)
6489 err = got_ferror(f, GOT_ERR_IO);
6490 done:
6491 if (commit)
6492 got_object_commit_close(commit);
6493 free(id_str);
6494 free(logmsg);
6495 return err;
6498 static const struct got_error *
6499 histedit_write_commit_list(struct got_object_id_queue *commits,
6500 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6502 const struct got_error *err = NULL;
6503 struct got_object_qid *qid;
6505 if (SIMPLEQ_EMPTY(commits))
6506 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6508 SIMPLEQ_FOREACH(qid, commits, entry) {
6509 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6510 f, repo);
6511 if (err)
6512 break;
6513 if (edit_logmsg_only) {
6514 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6515 if (n < 0) {
6516 err = got_ferror(f, GOT_ERR_IO);
6517 break;
6522 return err;
6525 static const struct got_error *
6526 write_cmd_list(FILE *f, const char *branch_name,
6527 struct got_object_id_queue *commits)
6529 const struct got_error *err = NULL;
6530 int n, i;
6531 char *id_str;
6532 struct got_object_qid *qid;
6534 qid = SIMPLEQ_FIRST(commits);
6535 err = got_object_id_str(&id_str, qid->id);
6536 if (err)
6537 return err;
6539 n = fprintf(f,
6540 "# Editing the history of branch '%s' starting at\n"
6541 "# commit %s\n"
6542 "# Commits will be processed in order from top to "
6543 "bottom of this file.\n", branch_name, id_str);
6544 if (n < 0) {
6545 err = got_ferror(f, GOT_ERR_IO);
6546 goto done;
6549 n = fprintf(f, "# Available histedit commands:\n");
6550 if (n < 0) {
6551 err = got_ferror(f, GOT_ERR_IO);
6552 goto done;
6555 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6556 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6557 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6558 cmd->desc);
6559 if (n < 0) {
6560 err = got_ferror(f, GOT_ERR_IO);
6561 break;
6564 done:
6565 free(id_str);
6566 return err;
6569 static const struct got_error *
6570 histedit_syntax_error(int lineno)
6572 static char msg[42];
6573 int ret;
6575 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6576 lineno);
6577 if (ret == -1 || ret >= sizeof(msg))
6578 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6580 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6583 static const struct got_error *
6584 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6585 char *logmsg, struct got_repository *repo)
6587 const struct got_error *err;
6588 struct got_commit_object *folded_commit = NULL;
6589 char *id_str, *folded_logmsg = NULL;
6591 err = got_object_id_str(&id_str, hle->commit_id);
6592 if (err)
6593 return err;
6595 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6596 if (err)
6597 goto done;
6599 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6600 if (err)
6601 goto done;
6602 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6603 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6604 folded_logmsg) == -1) {
6605 err = got_error_from_errno("asprintf");
6607 done:
6608 if (folded_commit)
6609 got_object_commit_close(folded_commit);
6610 free(id_str);
6611 free(folded_logmsg);
6612 return err;
6615 static struct got_histedit_list_entry *
6616 get_folded_commits(struct got_histedit_list_entry *hle)
6618 struct got_histedit_list_entry *prev, *folded = NULL;
6620 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6621 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6622 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6623 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6624 folded = prev;
6625 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6628 return folded;
6631 static const struct got_error *
6632 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6633 struct got_repository *repo)
6635 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6636 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6637 const struct got_error *err = NULL;
6638 struct got_commit_object *commit = NULL;
6639 int fd;
6640 struct got_histedit_list_entry *folded = NULL;
6642 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6643 if (err)
6644 return err;
6646 folded = get_folded_commits(hle);
6647 if (folded) {
6648 while (folded != hle) {
6649 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6650 folded = TAILQ_NEXT(folded, entry);
6651 continue;
6653 err = append_folded_commit_msg(&new_msg, folded,
6654 logmsg, repo);
6655 if (err)
6656 goto done;
6657 free(logmsg);
6658 logmsg = new_msg;
6659 folded = TAILQ_NEXT(folded, entry);
6663 err = got_object_id_str(&id_str, hle->commit_id);
6664 if (err)
6665 goto done;
6666 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6667 if (err)
6668 goto done;
6669 if (asprintf(&new_msg,
6670 "%s\n# original log message of commit %s: %s",
6671 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6672 err = got_error_from_errno("asprintf");
6673 goto done;
6675 free(logmsg);
6676 logmsg = new_msg;
6678 err = got_object_id_str(&id_str, hle->commit_id);
6679 if (err)
6680 goto done;
6682 err = got_opentemp_named_fd(&logmsg_path, &fd,
6683 GOT_TMPDIR_STR "/got-logmsg");
6684 if (err)
6685 goto done;
6687 dprintf(fd, logmsg);
6688 close(fd);
6690 err = get_editor(&editor);
6691 if (err)
6692 goto done;
6694 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6695 if (err) {
6696 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6697 goto done;
6698 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6700 done:
6701 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6702 err = got_error_from_errno2("unlink", logmsg_path);
6703 free(logmsg_path);
6704 free(logmsg);
6705 free(orig_logmsg);
6706 free(editor);
6707 if (commit)
6708 got_object_commit_close(commit);
6709 return err;
6712 static const struct got_error *
6713 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6714 FILE *f, struct got_repository *repo)
6716 const struct got_error *err = NULL;
6717 char *line = NULL, *p, *end;
6718 size_t size;
6719 ssize_t len;
6720 int lineno = 0, i;
6721 const struct got_histedit_cmd *cmd;
6722 struct got_object_id *commit_id = NULL;
6723 struct got_histedit_list_entry *hle = NULL;
6725 for (;;) {
6726 len = getline(&line, &size, f);
6727 if (len == -1) {
6728 const struct got_error *getline_err;
6729 if (feof(f))
6730 break;
6731 getline_err = got_error_from_errno("getline");
6732 err = got_ferror(f, getline_err->code);
6733 break;
6735 lineno++;
6736 p = line;
6737 while (isspace((unsigned char)p[0]))
6738 p++;
6739 if (p[0] == '#' || p[0] == '\0') {
6740 free(line);
6741 line = NULL;
6742 continue;
6744 cmd = NULL;
6745 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6746 cmd = &got_histedit_cmds[i];
6747 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6748 isspace((unsigned char)p[strlen(cmd->name)])) {
6749 p += strlen(cmd->name);
6750 break;
6752 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6753 p++;
6754 break;
6757 if (i == nitems(got_histedit_cmds)) {
6758 err = histedit_syntax_error(lineno);
6759 break;
6761 while (isspace((unsigned char)p[0]))
6762 p++;
6763 if (cmd->code == GOT_HISTEDIT_MESG) {
6764 if (hle == NULL || hle->logmsg != NULL) {
6765 err = got_error(GOT_ERR_HISTEDIT_CMD);
6766 break;
6768 if (p[0] == '\0') {
6769 err = histedit_edit_logmsg(hle, repo);
6770 if (err)
6771 break;
6772 } else {
6773 hle->logmsg = strdup(p);
6774 if (hle->logmsg == NULL) {
6775 err = got_error_from_errno("strdup");
6776 break;
6779 free(line);
6780 line = NULL;
6781 continue;
6782 } else {
6783 end = p;
6784 while (end[0] && !isspace((unsigned char)end[0]))
6785 end++;
6786 *end = '\0';
6788 err = got_object_resolve_id_str(&commit_id, repo, p);
6789 if (err) {
6790 /* override error code */
6791 err = histedit_syntax_error(lineno);
6792 break;
6795 hle = malloc(sizeof(*hle));
6796 if (hle == NULL) {
6797 err = got_error_from_errno("malloc");
6798 break;
6800 hle->cmd = cmd;
6801 hle->commit_id = commit_id;
6802 hle->logmsg = NULL;
6803 commit_id = NULL;
6804 free(line);
6805 line = NULL;
6806 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6809 free(line);
6810 free(commit_id);
6811 return err;
6814 static const struct got_error *
6815 histedit_check_script(struct got_histedit_list *histedit_cmds,
6816 struct got_object_id_queue *commits, struct got_repository *repo)
6818 const struct got_error *err = NULL;
6819 struct got_object_qid *qid;
6820 struct got_histedit_list_entry *hle;
6821 static char msg[92];
6822 char *id_str;
6824 if (TAILQ_EMPTY(histedit_cmds))
6825 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6826 "histedit script contains no commands");
6827 if (SIMPLEQ_EMPTY(commits))
6828 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6830 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6831 struct got_histedit_list_entry *hle2;
6832 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6833 if (hle == hle2)
6834 continue;
6835 if (got_object_id_cmp(hle->commit_id,
6836 hle2->commit_id) != 0)
6837 continue;
6838 err = got_object_id_str(&id_str, hle->commit_id);
6839 if (err)
6840 return err;
6841 snprintf(msg, sizeof(msg), "commit %s is listed "
6842 "more than once in histedit script", id_str);
6843 free(id_str);
6844 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6848 SIMPLEQ_FOREACH(qid, commits, entry) {
6849 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6850 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6851 break;
6853 if (hle == NULL) {
6854 err = got_object_id_str(&id_str, qid->id);
6855 if (err)
6856 return err;
6857 snprintf(msg, sizeof(msg),
6858 "commit %s missing from histedit script", id_str);
6859 free(id_str);
6860 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6864 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6865 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6866 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6867 "last commit in histedit script cannot be folded");
6869 return NULL;
6872 static const struct got_error *
6873 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6874 const char *path, struct got_object_id_queue *commits,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6878 char *editor;
6879 FILE *f = NULL;
6881 err = get_editor(&editor);
6882 if (err)
6883 return err;
6885 if (spawn_editor(editor, path) == -1) {
6886 err = got_error_from_errno("failed spawning editor");
6887 goto done;
6890 f = fopen(path, "r");
6891 if (f == NULL) {
6892 err = got_error_from_errno("fopen");
6893 goto done;
6895 err = histedit_parse_list(histedit_cmds, f, repo);
6896 if (err)
6897 goto done;
6899 err = histedit_check_script(histedit_cmds, commits, repo);
6900 done:
6901 if (f && fclose(f) != 0 && err == NULL)
6902 err = got_error_from_errno("fclose");
6903 free(editor);
6904 return err;
6907 static const struct got_error *
6908 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6909 struct got_object_id_queue *, const char *, const char *,
6910 struct got_repository *);
6912 static const struct got_error *
6913 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6914 struct got_object_id_queue *commits, const char *branch_name,
6915 int edit_logmsg_only, struct got_repository *repo)
6917 const struct got_error *err;
6918 FILE *f = NULL;
6919 char *path = NULL;
6921 err = got_opentemp_named(&path, &f, "got-histedit");
6922 if (err)
6923 return err;
6925 err = write_cmd_list(f, branch_name, commits);
6926 if (err)
6927 goto done;
6929 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6930 if (err)
6931 goto done;
6933 if (edit_logmsg_only) {
6934 rewind(f);
6935 err = histedit_parse_list(histedit_cmds, f, repo);
6936 } else {
6937 if (fclose(f) != 0) {
6938 err = got_error_from_errno("fclose");
6939 goto done;
6941 f = NULL;
6942 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6943 if (err) {
6944 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6945 err->code != GOT_ERR_HISTEDIT_CMD)
6946 goto done;
6947 err = histedit_edit_list_retry(histedit_cmds, err,
6948 commits, path, branch_name, repo);
6951 done:
6952 if (f && fclose(f) != 0 && err == NULL)
6953 err = got_error_from_errno("fclose");
6954 if (path && unlink(path) != 0 && err == NULL)
6955 err = got_error_from_errno2("unlink", path);
6956 free(path);
6957 return err;
6960 static const struct got_error *
6961 histedit_save_list(struct got_histedit_list *histedit_cmds,
6962 struct got_worktree *worktree, struct got_repository *repo)
6964 const struct got_error *err = NULL;
6965 char *path = NULL;
6966 FILE *f = NULL;
6967 struct got_histedit_list_entry *hle;
6968 struct got_commit_object *commit = NULL;
6970 err = got_worktree_get_histedit_script_path(&path, worktree);
6971 if (err)
6972 return err;
6974 f = fopen(path, "w");
6975 if (f == NULL) {
6976 err = got_error_from_errno2("fopen", path);
6977 goto done;
6979 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6980 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6981 repo);
6982 if (err)
6983 break;
6985 if (hle->logmsg) {
6986 int n = fprintf(f, "%c %s\n",
6987 GOT_HISTEDIT_MESG, hle->logmsg);
6988 if (n < 0) {
6989 err = got_ferror(f, GOT_ERR_IO);
6990 break;
6994 done:
6995 if (f && fclose(f) != 0 && err == NULL)
6996 err = got_error_from_errno("fclose");
6997 free(path);
6998 if (commit)
6999 got_object_commit_close(commit);
7000 return err;
7003 void
7004 histedit_free_list(struct got_histedit_list *histedit_cmds)
7006 struct got_histedit_list_entry *hle;
7008 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7009 TAILQ_REMOVE(histedit_cmds, hle, entry);
7010 free(hle);
7014 static const struct got_error *
7015 histedit_load_list(struct got_histedit_list *histedit_cmds,
7016 const char *path, struct got_repository *repo)
7018 const struct got_error *err = NULL;
7019 FILE *f = NULL;
7021 f = fopen(path, "r");
7022 if (f == NULL) {
7023 err = got_error_from_errno2("fopen", path);
7024 goto done;
7027 err = histedit_parse_list(histedit_cmds, f, repo);
7028 done:
7029 if (f && fclose(f) != 0 && err == NULL)
7030 err = got_error_from_errno("fclose");
7031 return err;
7034 static const struct got_error *
7035 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7036 const struct got_error *edit_err, struct got_object_id_queue *commits,
7037 const char *path, const char *branch_name, struct got_repository *repo)
7039 const struct got_error *err = NULL, *prev_err = edit_err;
7040 int resp = ' ';
7042 while (resp != 'c' && resp != 'r' && resp != 'a') {
7043 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7044 "or (a)bort: ", getprogname(), prev_err->msg);
7045 resp = getchar();
7046 if (resp == '\n')
7047 resp = getchar();
7048 if (resp == 'c') {
7049 histedit_free_list(histedit_cmds);
7050 err = histedit_run_editor(histedit_cmds, path, commits,
7051 repo);
7052 if (err) {
7053 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7054 err->code != GOT_ERR_HISTEDIT_CMD)
7055 break;
7056 prev_err = err;
7057 resp = ' ';
7058 continue;
7060 break;
7061 } else if (resp == 'r') {
7062 histedit_free_list(histedit_cmds);
7063 err = histedit_edit_script(histedit_cmds,
7064 commits, branch_name, 0, repo);
7065 if (err) {
7066 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7067 err->code != GOT_ERR_HISTEDIT_CMD)
7068 break;
7069 prev_err = err;
7070 resp = ' ';
7071 continue;
7073 break;
7074 } else if (resp == 'a') {
7075 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7076 break;
7077 } else
7078 printf("invalid response '%c'\n", resp);
7081 return err;
7084 static const struct got_error *
7085 histedit_complete(struct got_worktree *worktree,
7086 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7087 struct got_reference *branch, struct got_repository *repo)
7089 printf("Switching work tree to %s\n",
7090 got_ref_get_symref_target(branch));
7091 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7092 branch, repo);
7095 static const struct got_error *
7096 show_histedit_progress(struct got_commit_object *commit,
7097 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7099 const struct got_error *err;
7100 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7102 err = got_object_id_str(&old_id_str, hle->commit_id);
7103 if (err)
7104 goto done;
7106 if (new_id) {
7107 err = got_object_id_str(&new_id_str, new_id);
7108 if (err)
7109 goto done;
7112 old_id_str[12] = '\0';
7113 if (new_id_str)
7114 new_id_str[12] = '\0';
7116 if (hle->logmsg) {
7117 logmsg = strdup(hle->logmsg);
7118 if (logmsg == NULL) {
7119 err = got_error_from_errno("strdup");
7120 goto done;
7122 trim_logmsg(logmsg, 42);
7123 } else {
7124 err = get_short_logmsg(&logmsg, 42, commit);
7125 if (err)
7126 goto done;
7129 switch (hle->cmd->code) {
7130 case GOT_HISTEDIT_PICK:
7131 case GOT_HISTEDIT_EDIT:
7132 printf("%s -> %s: %s\n", old_id_str,
7133 new_id_str ? new_id_str : "no-op change", logmsg);
7134 break;
7135 case GOT_HISTEDIT_DROP:
7136 case GOT_HISTEDIT_FOLD:
7137 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7138 logmsg);
7139 break;
7140 default:
7141 break;
7143 done:
7144 free(old_id_str);
7145 free(new_id_str);
7146 return err;
7149 static const struct got_error *
7150 histedit_commit(struct got_pathlist_head *merged_paths,
7151 struct got_worktree *worktree, struct got_fileindex *fileindex,
7152 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7153 struct got_repository *repo)
7155 const struct got_error *err;
7156 struct got_commit_object *commit;
7157 struct got_object_id *new_commit_id;
7159 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7160 && hle->logmsg == NULL) {
7161 err = histedit_edit_logmsg(hle, repo);
7162 if (err)
7163 return err;
7166 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7167 if (err)
7168 return err;
7170 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7171 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7172 hle->logmsg, repo);
7173 if (err) {
7174 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7175 goto done;
7176 err = show_histedit_progress(commit, hle, NULL);
7177 } else {
7178 err = show_histedit_progress(commit, hle, new_commit_id);
7179 free(new_commit_id);
7181 done:
7182 got_object_commit_close(commit);
7183 return err;
7186 static const struct got_error *
7187 histedit_skip_commit(struct got_histedit_list_entry *hle,
7188 struct got_worktree *worktree, struct got_repository *repo)
7190 const struct got_error *error;
7191 struct got_commit_object *commit;
7193 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7194 repo);
7195 if (error)
7196 return error;
7198 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7199 if (error)
7200 return error;
7202 error = show_histedit_progress(commit, hle, NULL);
7203 got_object_commit_close(commit);
7204 return error;
7207 static const struct got_error *
7208 check_local_changes(void *arg, unsigned char status,
7209 unsigned char staged_status, const char *path,
7210 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7211 struct got_object_id *commit_id, int dirfd, const char *de_name)
7213 int *have_local_changes = arg;
7215 switch (status) {
7216 case GOT_STATUS_ADD:
7217 case GOT_STATUS_DELETE:
7218 case GOT_STATUS_MODIFY:
7219 case GOT_STATUS_CONFLICT:
7220 *have_local_changes = 1;
7221 return got_error(GOT_ERR_CANCELLED);
7222 default:
7223 break;
7226 switch (staged_status) {
7227 case GOT_STATUS_ADD:
7228 case GOT_STATUS_DELETE:
7229 case GOT_STATUS_MODIFY:
7230 *have_local_changes = 1;
7231 return got_error(GOT_ERR_CANCELLED);
7232 default:
7233 break;
7236 return NULL;
7239 static const struct got_error *
7240 cmd_histedit(int argc, char *argv[])
7242 const struct got_error *error = NULL;
7243 struct got_worktree *worktree = NULL;
7244 struct got_fileindex *fileindex = NULL;
7245 struct got_repository *repo = NULL;
7246 char *cwd = NULL;
7247 struct got_reference *branch = NULL;
7248 struct got_reference *tmp_branch = NULL;
7249 struct got_object_id *resume_commit_id = NULL;
7250 struct got_object_id *base_commit_id = NULL;
7251 struct got_object_id *head_commit_id = NULL;
7252 struct got_commit_object *commit = NULL;
7253 int ch, rebase_in_progress = 0, did_something;
7254 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7255 int edit_logmsg_only = 0;
7256 const char *edit_script_path = NULL;
7257 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7258 struct got_object_id_queue commits;
7259 struct got_pathlist_head merged_paths;
7260 const struct got_object_id_queue *parent_ids;
7261 struct got_object_qid *pid;
7262 struct got_histedit_list histedit_cmds;
7263 struct got_histedit_list_entry *hle;
7265 SIMPLEQ_INIT(&commits);
7266 TAILQ_INIT(&histedit_cmds);
7267 TAILQ_INIT(&merged_paths);
7269 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7270 switch (ch) {
7271 case 'a':
7272 abort_edit = 1;
7273 break;
7274 case 'c':
7275 continue_edit = 1;
7276 break;
7277 case 'F':
7278 edit_script_path = optarg;
7279 break;
7280 case 'm':
7281 edit_logmsg_only = 1;
7282 break;
7283 default:
7284 usage_histedit();
7285 /* NOTREACHED */
7289 argc -= optind;
7290 argv += optind;
7292 #ifndef PROFILE
7293 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7294 "unveil", NULL) == -1)
7295 err(1, "pledge");
7296 #endif
7297 if (abort_edit && continue_edit)
7298 errx(1, "histedit's -a and -c options are mutually exclusive");
7299 if (edit_script_path && edit_logmsg_only)
7300 errx(1, "histedit's -F and -m options are mutually exclusive");
7301 if (abort_edit && edit_logmsg_only)
7302 errx(1, "histedit's -a and -m options are mutually exclusive");
7303 if (continue_edit && edit_logmsg_only)
7304 errx(1, "histedit's -c and -m options are mutually exclusive");
7305 if (argc != 0)
7306 usage_histedit();
7309 * This command cannot apply unveil(2) in all cases because the
7310 * user may choose to run an editor to edit the histedit script
7311 * and to edit individual commit log messages.
7312 * unveil(2) traverses exec(2); if an editor is used we have to
7313 * apply unveil after edit script and log messages have been written.
7314 * XXX TODO: Make use of unveil(2) where possible.
7317 cwd = getcwd(NULL, 0);
7318 if (cwd == NULL) {
7319 error = got_error_from_errno("getcwd");
7320 goto done;
7322 error = got_worktree_open(&worktree, cwd);
7323 if (error)
7324 goto done;
7326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7327 NULL);
7328 if (error != NULL)
7329 goto done;
7331 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7332 if (error)
7333 goto done;
7334 if (rebase_in_progress) {
7335 error = got_error(GOT_ERR_REBASING);
7336 goto done;
7339 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7340 if (error)
7341 goto done;
7343 if (edit_in_progress && edit_logmsg_only) {
7344 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7345 "histedit operation is in progress in this "
7346 "work tree and must be continued or aborted "
7347 "before the -m option can be used");
7348 goto done;
7351 if (edit_in_progress && abort_edit) {
7352 error = got_worktree_histedit_continue(&resume_commit_id,
7353 &tmp_branch, &branch, &base_commit_id, &fileindex,
7354 worktree, repo);
7355 if (error)
7356 goto done;
7357 printf("Switching work tree to %s\n",
7358 got_ref_get_symref_target(branch));
7359 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7360 branch, base_commit_id, update_progress, &did_something);
7361 if (error)
7362 goto done;
7363 printf("Histedit of %s aborted\n",
7364 got_ref_get_symref_target(branch));
7365 goto done; /* nothing else to do */
7366 } else if (abort_edit) {
7367 error = got_error(GOT_ERR_NOT_HISTEDIT);
7368 goto done;
7371 if (continue_edit) {
7372 char *path;
7374 if (!edit_in_progress) {
7375 error = got_error(GOT_ERR_NOT_HISTEDIT);
7376 goto done;
7379 error = got_worktree_get_histedit_script_path(&path, worktree);
7380 if (error)
7381 goto done;
7383 error = histedit_load_list(&histedit_cmds, path, repo);
7384 free(path);
7385 if (error)
7386 goto done;
7388 error = got_worktree_histedit_continue(&resume_commit_id,
7389 &tmp_branch, &branch, &base_commit_id, &fileindex,
7390 worktree, repo);
7391 if (error)
7392 goto done;
7394 error = got_ref_resolve(&head_commit_id, repo, branch);
7395 if (error)
7396 goto done;
7398 error = got_object_open_as_commit(&commit, repo,
7399 head_commit_id);
7400 if (error)
7401 goto done;
7402 parent_ids = got_object_commit_get_parent_ids(commit);
7403 pid = SIMPLEQ_FIRST(parent_ids);
7404 if (pid == NULL) {
7405 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7406 goto done;
7408 error = collect_commits(&commits, head_commit_id, pid->id,
7409 base_commit_id, got_worktree_get_path_prefix(worktree),
7410 GOT_ERR_HISTEDIT_PATH, repo);
7411 got_object_commit_close(commit);
7412 commit = NULL;
7413 if (error)
7414 goto done;
7415 } else {
7416 if (edit_in_progress) {
7417 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7418 goto done;
7421 error = got_ref_open(&branch, repo,
7422 got_worktree_get_head_ref_name(worktree), 0);
7423 if (error != NULL)
7424 goto done;
7426 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7427 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7428 "will not edit commit history of a branch outside "
7429 "the \"refs/heads/\" reference namespace");
7430 goto done;
7433 error = got_ref_resolve(&head_commit_id, repo, branch);
7434 got_ref_close(branch);
7435 branch = NULL;
7436 if (error)
7437 goto done;
7439 error = got_object_open_as_commit(&commit, repo,
7440 head_commit_id);
7441 if (error)
7442 goto done;
7443 parent_ids = got_object_commit_get_parent_ids(commit);
7444 pid = SIMPLEQ_FIRST(parent_ids);
7445 if (pid == NULL) {
7446 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7447 goto done;
7449 error = collect_commits(&commits, head_commit_id, pid->id,
7450 got_worktree_get_base_commit_id(worktree),
7451 got_worktree_get_path_prefix(worktree),
7452 GOT_ERR_HISTEDIT_PATH, repo);
7453 got_object_commit_close(commit);
7454 commit = NULL;
7455 if (error)
7456 goto done;
7458 if (SIMPLEQ_EMPTY(&commits)) {
7459 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7460 goto done;
7463 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7464 &base_commit_id, &fileindex, worktree, repo);
7465 if (error)
7466 goto done;
7468 if (edit_script_path) {
7469 error = histedit_load_list(&histedit_cmds,
7470 edit_script_path, repo);
7471 if (error) {
7472 got_worktree_histedit_abort(worktree, fileindex,
7473 repo, branch, base_commit_id,
7474 update_progress, &did_something);
7475 goto done;
7477 } else {
7478 const char *branch_name;
7479 branch_name = got_ref_get_symref_target(branch);
7480 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7481 branch_name += 11;
7482 error = histedit_edit_script(&histedit_cmds, &commits,
7483 branch_name, edit_logmsg_only, repo);
7484 if (error) {
7485 got_worktree_histedit_abort(worktree, fileindex,
7486 repo, branch, base_commit_id,
7487 update_progress, &did_something);
7488 goto done;
7493 error = histedit_save_list(&histedit_cmds, worktree,
7494 repo);
7495 if (error) {
7496 got_worktree_histedit_abort(worktree, fileindex,
7497 repo, branch, base_commit_id,
7498 update_progress, &did_something);
7499 goto done;
7504 error = histedit_check_script(&histedit_cmds, &commits, repo);
7505 if (error)
7506 goto done;
7508 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7509 if (resume_commit_id) {
7510 if (got_object_id_cmp(hle->commit_id,
7511 resume_commit_id) != 0)
7512 continue;
7514 resume_commit_id = NULL;
7515 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7516 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7517 error = histedit_skip_commit(hle, worktree,
7518 repo);
7519 if (error)
7520 goto done;
7521 } else {
7522 struct got_pathlist_head paths;
7523 int have_changes = 0;
7525 TAILQ_INIT(&paths);
7526 error = got_pathlist_append(&paths, "", NULL);
7527 if (error)
7528 goto done;
7529 error = got_worktree_status(worktree, &paths,
7530 repo, check_local_changes, &have_changes,
7531 check_cancelled, NULL);
7532 got_pathlist_free(&paths);
7533 if (error) {
7534 if (error->code != GOT_ERR_CANCELLED)
7535 goto done;
7536 if (sigint_received || sigpipe_received)
7537 goto done;
7539 if (have_changes) {
7540 error = histedit_commit(NULL, worktree,
7541 fileindex, tmp_branch, hle, repo);
7542 if (error)
7543 goto done;
7544 } else {
7545 error = got_object_open_as_commit(
7546 &commit, repo, hle->commit_id);
7547 if (error)
7548 goto done;
7549 error = show_histedit_progress(commit,
7550 hle, NULL);
7551 got_object_commit_close(commit);
7552 commit = NULL;
7553 if (error)
7554 goto done;
7557 continue;
7560 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7561 error = histedit_skip_commit(hle, worktree, repo);
7562 if (error)
7563 goto done;
7564 continue;
7567 error = got_object_open_as_commit(&commit, repo,
7568 hle->commit_id);
7569 if (error)
7570 goto done;
7571 parent_ids = got_object_commit_get_parent_ids(commit);
7572 pid = SIMPLEQ_FIRST(parent_ids);
7574 error = got_worktree_histedit_merge_files(&merged_paths,
7575 worktree, fileindex, pid->id, hle->commit_id, repo,
7576 rebase_progress, &rebase_status, check_cancelled, NULL);
7577 if (error)
7578 goto done;
7579 got_object_commit_close(commit);
7580 commit = NULL;
7582 if (rebase_status == GOT_STATUS_CONFLICT) {
7583 error = show_rebase_merge_conflict(hle->commit_id,
7584 repo);
7585 if (error)
7586 goto done;
7587 got_worktree_rebase_pathlist_free(&merged_paths);
7588 break;
7591 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7592 char *id_str;
7593 error = got_object_id_str(&id_str, hle->commit_id);
7594 if (error)
7595 goto done;
7596 printf("Stopping histedit for amending commit %s\n",
7597 id_str);
7598 free(id_str);
7599 got_worktree_rebase_pathlist_free(&merged_paths);
7600 error = got_worktree_histedit_postpone(worktree,
7601 fileindex);
7602 goto done;
7605 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7606 error = histedit_skip_commit(hle, worktree, repo);
7607 if (error)
7608 goto done;
7609 continue;
7612 error = histedit_commit(&merged_paths, worktree, fileindex,
7613 tmp_branch, hle, repo);
7614 got_worktree_rebase_pathlist_free(&merged_paths);
7615 if (error)
7616 goto done;
7619 if (rebase_status == GOT_STATUS_CONFLICT) {
7620 error = got_worktree_histedit_postpone(worktree, fileindex);
7621 if (error)
7622 goto done;
7623 error = got_error_msg(GOT_ERR_CONFLICTS,
7624 "conflicts must be resolved before histedit can continue");
7625 } else
7626 error = histedit_complete(worktree, fileindex, tmp_branch,
7627 branch, repo);
7628 done:
7629 got_object_id_queue_free(&commits);
7630 histedit_free_list(&histedit_cmds);
7631 free(head_commit_id);
7632 free(base_commit_id);
7633 free(resume_commit_id);
7634 if (commit)
7635 got_object_commit_close(commit);
7636 if (branch)
7637 got_ref_close(branch);
7638 if (tmp_branch)
7639 got_ref_close(tmp_branch);
7640 if (worktree)
7641 got_worktree_close(worktree);
7642 if (repo)
7643 got_repo_close(repo);
7644 return error;
7647 __dead static void
7648 usage_integrate(void)
7650 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7651 exit(1);
7654 static const struct got_error *
7655 cmd_integrate(int argc, char *argv[])
7657 const struct got_error *error = NULL;
7658 struct got_repository *repo = NULL;
7659 struct got_worktree *worktree = NULL;
7660 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7661 const char *branch_arg = NULL;
7662 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7663 struct got_fileindex *fileindex = NULL;
7664 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7665 int ch, did_something = 0;
7667 while ((ch = getopt(argc, argv, "")) != -1) {
7668 switch (ch) {
7669 default:
7670 usage_integrate();
7671 /* NOTREACHED */
7675 argc -= optind;
7676 argv += optind;
7678 if (argc != 1)
7679 usage_integrate();
7680 branch_arg = argv[0];
7682 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7683 "unveil", NULL) == -1)
7684 err(1, "pledge");
7686 cwd = getcwd(NULL, 0);
7687 if (cwd == NULL) {
7688 error = got_error_from_errno("getcwd");
7689 goto done;
7692 error = got_worktree_open(&worktree, cwd);
7693 if (error)
7694 goto done;
7696 error = check_rebase_or_histedit_in_progress(worktree);
7697 if (error)
7698 goto done;
7700 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7701 NULL);
7702 if (error != NULL)
7703 goto done;
7705 error = apply_unveil(got_repo_get_path(repo), 0,
7706 got_worktree_get_root_path(worktree));
7707 if (error)
7708 goto done;
7710 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7711 error = got_error_from_errno("asprintf");
7712 goto done;
7715 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7716 &base_branch_ref, worktree, refname, repo);
7717 if (error)
7718 goto done;
7720 refname = strdup(got_ref_get_name(branch_ref));
7721 if (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;
7727 base_refname = strdup(got_ref_get_name(base_branch_ref));
7728 if (base_refname == NULL) {
7729 error = got_error_from_errno("strdup");
7730 got_worktree_integrate_abort(worktree, fileindex, repo,
7731 branch_ref, base_branch_ref);
7732 goto done;
7735 error = got_ref_resolve(&commit_id, repo, branch_ref);
7736 if (error)
7737 goto done;
7739 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7740 if (error)
7741 goto done;
7743 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7744 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7745 "specified branch has already been integrated");
7746 got_worktree_integrate_abort(worktree, fileindex, repo,
7747 branch_ref, base_branch_ref);
7748 goto done;
7751 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7752 if (error) {
7753 if (error->code == GOT_ERR_ANCESTRY)
7754 error = got_error(GOT_ERR_REBASE_REQUIRED);
7755 got_worktree_integrate_abort(worktree, fileindex, repo,
7756 branch_ref, base_branch_ref);
7757 goto done;
7760 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7761 branch_ref, base_branch_ref, update_progress, &did_something,
7762 check_cancelled, NULL);
7763 if (error)
7764 goto done;
7766 printf("Integrated %s into %s\n", refname, base_refname);
7767 done:
7768 if (repo)
7769 got_repo_close(repo);
7770 if (worktree)
7771 got_worktree_close(worktree);
7772 free(cwd);
7773 free(base_commit_id);
7774 free(commit_id);
7775 free(refname);
7776 free(base_refname);
7777 return error;
7780 __dead static void
7781 usage_stage(void)
7783 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7784 "[file-path ...]\n",
7785 getprogname());
7786 exit(1);
7789 static const struct got_error *
7790 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7791 const char *path, struct got_object_id *blob_id,
7792 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7793 int dirfd, const char *de_name)
7795 const struct got_error *err = NULL;
7796 char *id_str = NULL;
7798 if (staged_status != GOT_STATUS_ADD &&
7799 staged_status != GOT_STATUS_MODIFY &&
7800 staged_status != GOT_STATUS_DELETE)
7801 return NULL;
7803 if (staged_status == GOT_STATUS_ADD ||
7804 staged_status == GOT_STATUS_MODIFY)
7805 err = got_object_id_str(&id_str, staged_blob_id);
7806 else
7807 err = got_object_id_str(&id_str, blob_id);
7808 if (err)
7809 return err;
7811 printf("%s %c %s\n", id_str, staged_status, path);
7812 free(id_str);
7813 return NULL;
7816 static const struct got_error *
7817 cmd_stage(int argc, char *argv[])
7819 const struct got_error *error = NULL;
7820 struct got_repository *repo = NULL;
7821 struct got_worktree *worktree = NULL;
7822 char *cwd = NULL;
7823 struct got_pathlist_head paths;
7824 struct got_pathlist_entry *pe;
7825 int ch, list_stage = 0, pflag = 0;
7826 FILE *patch_script_file = NULL;
7827 const char *patch_script_path = NULL;
7828 struct choose_patch_arg cpa;
7830 TAILQ_INIT(&paths);
7832 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7833 switch (ch) {
7834 case 'l':
7835 list_stage = 1;
7836 break;
7837 case 'p':
7838 pflag = 1;
7839 break;
7840 case 'F':
7841 patch_script_path = optarg;
7842 break;
7843 default:
7844 usage_stage();
7845 /* NOTREACHED */
7849 argc -= optind;
7850 argv += optind;
7852 #ifndef PROFILE
7853 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7854 "unveil", NULL) == -1)
7855 err(1, "pledge");
7856 #endif
7857 if (list_stage && (pflag || patch_script_path))
7858 errx(1, "-l option cannot be used with other options");
7859 if (patch_script_path && !pflag)
7860 errx(1, "-F option can only be used together with -p option");
7862 cwd = getcwd(NULL, 0);
7863 if (cwd == NULL) {
7864 error = got_error_from_errno("getcwd");
7865 goto done;
7868 error = got_worktree_open(&worktree, cwd);
7869 if (error)
7870 goto done;
7872 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7873 NULL);
7874 if (error != NULL)
7875 goto done;
7877 if (patch_script_path) {
7878 patch_script_file = fopen(patch_script_path, "r");
7879 if (patch_script_file == NULL) {
7880 error = got_error_from_errno2("fopen",
7881 patch_script_path);
7882 goto done;
7885 error = apply_unveil(got_repo_get_path(repo), 0,
7886 got_worktree_get_root_path(worktree));
7887 if (error)
7888 goto done;
7890 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7891 if (error)
7892 goto done;
7894 if (list_stage)
7895 error = got_worktree_status(worktree, &paths, repo,
7896 print_stage, NULL, check_cancelled, NULL);
7897 else {
7898 cpa.patch_script_file = patch_script_file;
7899 cpa.action = "stage";
7900 error = got_worktree_stage(worktree, &paths,
7901 pflag ? NULL : print_status, NULL,
7902 pflag ? choose_patch : NULL, &cpa, repo);
7904 done:
7905 if (patch_script_file && fclose(patch_script_file) == EOF &&
7906 error == NULL)
7907 error = got_error_from_errno2("fclose", patch_script_path);
7908 if (repo)
7909 got_repo_close(repo);
7910 if (worktree)
7911 got_worktree_close(worktree);
7912 TAILQ_FOREACH(pe, &paths, entry)
7913 free((char *)pe->path);
7914 got_pathlist_free(&paths);
7915 free(cwd);
7916 return error;
7919 __dead static void
7920 usage_unstage(void)
7922 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7923 "[file-path ...]\n",
7924 getprogname());
7925 exit(1);
7929 static const struct got_error *
7930 cmd_unstage(int argc, char *argv[])
7932 const struct got_error *error = NULL;
7933 struct got_repository *repo = NULL;
7934 struct got_worktree *worktree = NULL;
7935 char *cwd = NULL;
7936 struct got_pathlist_head paths;
7937 struct got_pathlist_entry *pe;
7938 int ch, did_something = 0, pflag = 0;
7939 FILE *patch_script_file = NULL;
7940 const char *patch_script_path = NULL;
7941 struct choose_patch_arg cpa;
7943 TAILQ_INIT(&paths);
7945 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7946 switch (ch) {
7947 case 'p':
7948 pflag = 1;
7949 break;
7950 case 'F':
7951 patch_script_path = optarg;
7952 break;
7953 default:
7954 usage_unstage();
7955 /* NOTREACHED */
7959 argc -= optind;
7960 argv += optind;
7962 #ifndef PROFILE
7963 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7964 "unveil", NULL) == -1)
7965 err(1, "pledge");
7966 #endif
7967 if (patch_script_path && !pflag)
7968 errx(1, "-F option can only be used together with -p option");
7970 cwd = getcwd(NULL, 0);
7971 if (cwd == NULL) {
7972 error = got_error_from_errno("getcwd");
7973 goto done;
7976 error = got_worktree_open(&worktree, cwd);
7977 if (error)
7978 goto done;
7980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7981 NULL);
7982 if (error != NULL)
7983 goto done;
7985 if (patch_script_path) {
7986 patch_script_file = fopen(patch_script_path, "r");
7987 if (patch_script_file == NULL) {
7988 error = got_error_from_errno2("fopen",
7989 patch_script_path);
7990 goto done;
7994 error = apply_unveil(got_repo_get_path(repo), 0,
7995 got_worktree_get_root_path(worktree));
7996 if (error)
7997 goto done;
7999 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8000 if (error)
8001 goto done;
8003 cpa.patch_script_file = patch_script_file;
8004 cpa.action = "unstage";
8005 error = got_worktree_unstage(worktree, &paths, update_progress,
8006 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8007 done:
8008 if (patch_script_file && fclose(patch_script_file) == EOF &&
8009 error == NULL)
8010 error = got_error_from_errno2("fclose", patch_script_path);
8011 if (repo)
8012 got_repo_close(repo);
8013 if (worktree)
8014 got_worktree_close(worktree);
8015 TAILQ_FOREACH(pe, &paths, entry)
8016 free((char *)pe->path);
8017 got_pathlist_free(&paths);
8018 free(cwd);
8019 return error;
8022 __dead static void
8023 usage_cat(void)
8025 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8026 "arg1 [arg2 ...]\n", getprogname());
8027 exit(1);
8030 static const struct got_error *
8031 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8033 const struct got_error *err;
8034 struct got_blob_object *blob;
8036 err = got_object_open_as_blob(&blob, repo, id, 8192);
8037 if (err)
8038 return err;
8040 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8041 got_object_blob_close(blob);
8042 return err;
8045 static const struct got_error *
8046 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8048 const struct got_error *err;
8049 struct got_tree_object *tree;
8050 int nentries, i;
8052 err = got_object_open_as_tree(&tree, repo, id);
8053 if (err)
8054 return err;
8056 nentries = got_object_tree_get_nentries(tree);
8057 for (i = 0; i < nentries; i++) {
8058 struct got_tree_entry *te;
8059 char *id_str;
8060 if (sigint_received || sigpipe_received)
8061 break;
8062 te = got_object_tree_get_entry(tree, i);
8063 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8064 if (err)
8065 break;
8066 fprintf(outfile, "%s %.7o %s\n", id_str,
8067 got_tree_entry_get_mode(te),
8068 got_tree_entry_get_name(te));
8069 free(id_str);
8072 got_object_tree_close(tree);
8073 return err;
8076 static const struct got_error *
8077 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8079 const struct got_error *err;
8080 struct got_commit_object *commit;
8081 const struct got_object_id_queue *parent_ids;
8082 struct got_object_qid *pid;
8083 char *id_str = NULL;
8084 const char *logmsg = NULL;
8086 err = got_object_open_as_commit(&commit, repo, id);
8087 if (err)
8088 return err;
8090 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8091 if (err)
8092 goto done;
8094 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8095 parent_ids = got_object_commit_get_parent_ids(commit);
8096 fprintf(outfile, "numparents %d\n",
8097 got_object_commit_get_nparents(commit));
8098 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8099 char *pid_str;
8100 err = got_object_id_str(&pid_str, pid->id);
8101 if (err)
8102 goto done;
8103 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8104 free(pid_str);
8106 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8107 got_object_commit_get_author(commit),
8108 got_object_commit_get_author_time(commit));
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8111 got_object_commit_get_author(commit),
8112 got_object_commit_get_committer_time(commit));
8114 logmsg = got_object_commit_get_logmsg_raw(commit);
8115 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8116 fprintf(outfile, "%s", logmsg);
8117 done:
8118 free(id_str);
8119 got_object_commit_close(commit);
8120 return err;
8123 static const struct got_error *
8124 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8126 const struct got_error *err;
8127 struct got_tag_object *tag;
8128 char *id_str = NULL;
8129 const char *tagmsg = NULL;
8131 err = got_object_open_as_tag(&tag, repo, id);
8132 if (err)
8133 return err;
8135 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8136 if (err)
8137 goto done;
8139 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8141 switch (got_object_tag_get_object_type(tag)) {
8142 case GOT_OBJ_TYPE_BLOB:
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8144 GOT_OBJ_LABEL_BLOB);
8145 break;
8146 case GOT_OBJ_TYPE_TREE:
8147 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 GOT_OBJ_LABEL_TREE);
8149 break;
8150 case GOT_OBJ_TYPE_COMMIT:
8151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 GOT_OBJ_LABEL_COMMIT);
8153 break;
8154 case GOT_OBJ_TYPE_TAG:
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8156 GOT_OBJ_LABEL_TAG);
8157 break;
8158 default:
8159 break;
8162 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8163 got_object_tag_get_name(tag));
8165 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8166 got_object_tag_get_tagger(tag),
8167 got_object_tag_get_tagger_time(tag));
8169 tagmsg = got_object_tag_get_message(tag);
8170 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8171 fprintf(outfile, "%s", tagmsg);
8172 done:
8173 free(id_str);
8174 got_object_tag_close(tag);
8175 return err;
8178 static const struct got_error *
8179 cmd_cat(int argc, char *argv[])
8181 const struct got_error *error;
8182 struct got_repository *repo = NULL;
8183 struct got_worktree *worktree = NULL;
8184 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8185 const char *commit_id_str = NULL;
8186 struct got_object_id *id = NULL, *commit_id = NULL;
8187 int ch, obj_type, i, force_path = 0;
8189 #ifndef PROFILE
8190 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8191 NULL) == -1)
8192 err(1, "pledge");
8193 #endif
8195 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8196 switch (ch) {
8197 case 'c':
8198 commit_id_str = optarg;
8199 break;
8200 case 'r':
8201 repo_path = realpath(optarg, NULL);
8202 if (repo_path == NULL)
8203 return got_error_from_errno2("realpath",
8204 optarg);
8205 got_path_strip_trailing_slashes(repo_path);
8206 break;
8207 case 'P':
8208 force_path = 1;
8209 break;
8210 default:
8211 usage_cat();
8212 /* NOTREACHED */
8216 argc -= optind;
8217 argv += optind;
8219 cwd = getcwd(NULL, 0);
8220 if (cwd == NULL) {
8221 error = got_error_from_errno("getcwd");
8222 goto done;
8224 error = got_worktree_open(&worktree, cwd);
8225 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8226 goto done;
8227 if (worktree) {
8228 if (repo_path == NULL) {
8229 repo_path = strdup(
8230 got_worktree_get_repo_path(worktree));
8231 if (repo_path == NULL) {
8232 error = got_error_from_errno("strdup");
8233 goto done;
8238 if (repo_path == NULL) {
8239 repo_path = getcwd(NULL, 0);
8240 if (repo_path == NULL)
8241 return got_error_from_errno("getcwd");
8244 error = got_repo_open(&repo, repo_path, NULL);
8245 free(repo_path);
8246 if (error != NULL)
8247 goto done;
8249 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8250 if (error)
8251 goto done;
8253 if (commit_id_str == NULL)
8254 commit_id_str = GOT_REF_HEAD;
8255 error = got_repo_match_object_id(&commit_id, NULL,
8256 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8257 if (error)
8258 goto done;
8260 for (i = 0; i < argc; i++) {
8261 if (force_path) {
8262 error = got_object_id_by_path(&id, repo, commit_id,
8263 argv[i]);
8264 if (error)
8265 break;
8266 } else {
8267 error = got_repo_match_object_id(&id, &label, argv[i],
8268 GOT_OBJ_TYPE_ANY, 0, repo);
8269 if (error) {
8270 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8271 error->code != GOT_ERR_NOT_REF)
8272 break;
8273 error = got_object_id_by_path(&id, repo,
8274 commit_id, argv[i]);
8275 if (error)
8276 break;
8280 error = got_object_get_type(&obj_type, repo, id);
8281 if (error)
8282 break;
8284 switch (obj_type) {
8285 case GOT_OBJ_TYPE_BLOB:
8286 error = cat_blob(id, repo, stdout);
8287 break;
8288 case GOT_OBJ_TYPE_TREE:
8289 error = cat_tree(id, repo, stdout);
8290 break;
8291 case GOT_OBJ_TYPE_COMMIT:
8292 error = cat_commit(id, repo, stdout);
8293 break;
8294 case GOT_OBJ_TYPE_TAG:
8295 error = cat_tag(id, repo, stdout);
8296 break;
8297 default:
8298 error = got_error(GOT_ERR_OBJ_TYPE);
8299 break;
8301 if (error)
8302 break;
8303 free(label);
8304 label = NULL;
8305 free(id);
8306 id = NULL;
8308 done:
8309 free(label);
8310 free(id);
8311 free(commit_id);
8312 if (worktree)
8313 got_worktree_close(worktree);
8314 if (repo) {
8315 const struct got_error *repo_error;
8316 repo_error = got_repo_close(repo);
8317 if (error == NULL)
8318 error = repo_error;
8320 return error;