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 [-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, mirror_references = 0;
908 TAILQ_INIT(&refs);
909 TAILQ_INIT(&symrefs);
911 while ((ch = getopt(argc, argv, "mvq")) != -1) {
912 switch (ch) {
913 case 'm':
914 mirror_references = 1;
915 break;
916 case 'v':
917 if (verbosity < 0)
918 verbosity = 0;
919 else if (verbosity < 3)
920 verbosity++;
921 break;
922 case 'q':
923 verbosity = -1;
924 break;
925 default:
926 usage_clone();
927 break;
930 argc -= optind;
931 argv += optind;
933 uri = argv[0];
935 if (argc == 1)
936 dirname = NULL;
937 else if (argc == 2)
938 dirname = argv[1];
939 else
940 usage_clone();
942 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
943 &repo_name, argv[0]);
944 if (error)
945 goto done;
947 if (strcmp(proto, "git") == 0) {
948 #ifndef PROFILE
949 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
950 "sendfd dns inet unveil", NULL) == -1)
951 err(1, "pledge");
952 #endif
953 git_url = strdup(argv[0]);
954 if (git_url == NULL) {
955 error = got_error_from_errno("strdup");
956 goto done;
958 } else if (strcmp(proto, "git+ssh") == 0 ||
959 strcmp(proto, "ssh") == 0) {
960 #ifndef PROFILE
961 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
962 "sendfd unveil", NULL) == -1)
963 err(1, "pledge");
964 #endif
965 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
966 error = got_error_from_errno("asprintf");
967 goto done;
969 } else if (strcmp(proto, "http") == 0 ||
970 strcmp(proto, "git+http") == 0) {
971 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
972 goto done;
973 } else {
974 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
975 goto done;
977 if (dirname == NULL) {
978 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
979 error = got_error_from_errno("asprintf");
980 goto done;
982 repo_path = default_destdir;
983 } else
984 repo_path = dirname;
986 error = got_path_mkdir(repo_path);
987 if (error)
988 goto done;
990 error = got_repo_init(repo_path);
991 if (error)
992 goto done;
994 error = got_repo_open(&repo, repo_path, NULL);
995 if (error)
996 goto done;
998 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
999 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1000 error = got_error_from_errno2("unveil",
1001 GOT_FETCH_PATH_SSH);
1002 goto done;
1005 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1006 if (error)
1007 goto done;
1009 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1010 verbosity);
1011 if (error)
1012 goto done;
1014 if (verbosity >= 0)
1015 printf("Connected to %s:%s\n", host, port);
1017 /* Create a config file git-fetch(1) can understand. */
1018 gitconfig_path = got_repo_get_path_gitconfig(repo);
1019 if (gitconfig_path == NULL) {
1020 error = got_error_from_errno("got_repo_get_path_gitconfig");
1021 goto done;
1023 gitconfig_file = fopen(gitconfig_path, "a");
1024 if (gitconfig_file == NULL) {
1025 error = got_error_from_errno2("fopen", gitconfig_path);
1026 goto done;
1028 if (mirror_references) {
1029 if (asprintf(&gitconfig,
1030 "[remote \"%s\"]\n"
1031 "\turl = %s\n"
1032 "\tmirror = true\n",
1033 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1034 error = got_error_from_errno("asprintf");
1035 goto done;
1037 } else {
1038 if (asprintf(&gitconfig,
1039 "[remote \"%s\"]\n"
1040 "\turl = %s\n"
1041 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1042 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1043 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1044 error = got_error_from_errno("asprintf");
1045 goto done;
1048 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1049 if (n != strlen(gitconfig)) {
1050 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1051 goto done;
1054 fpa.last_scaled_size[0] = '\0';
1055 fpa.last_p_indexed = -1;
1056 fpa.last_p_resolved = -1;
1057 fpa.verbosity = verbosity;
1058 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1059 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1060 fetchfd, repo, fetch_progress, &fpa);
1061 if (error)
1062 goto done;
1064 error = got_object_id_str(&id_str, pack_hash);
1065 if (error)
1066 goto done;
1067 if (verbosity >= 0)
1068 printf("\nFetched %s.pack\n", id_str);
1069 free(id_str);
1071 /* Set up references provided with the pack file. */
1072 TAILQ_FOREACH(pe, &refs, entry) {
1073 const char *refname = pe->path;
1074 struct got_object_id *id = pe->data;
1075 struct got_reference *ref;
1076 char *remote_refname;
1078 error = got_ref_alloc(&ref, refname, id);
1079 if (error)
1080 goto done;
1081 error = got_ref_write(ref, repo);
1082 got_ref_close(ref);
1083 if (error)
1084 goto done;
1086 if (mirror_references)
1087 continue;
1089 if (strncmp("refs/heads/", refname, 11) != 0)
1090 continue;
1092 if (asprintf(&remote_refname,
1093 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1094 refname + 11) == -1) {
1095 error = got_error_from_errno("asprintf");
1096 goto done;
1098 error = got_ref_alloc(&ref, remote_refname, id);
1099 if (error)
1100 goto done;
1101 error = got_ref_write(ref, repo);
1102 got_ref_close(ref);
1103 if (error)
1104 goto done;
1107 /* Set the HEAD reference if the server provided one. */
1108 TAILQ_FOREACH(pe, &symrefs, entry) {
1109 struct got_reference *symref, *target_ref;
1110 const char *refname = pe->path;
1111 const char *target = pe->data;
1113 if (strcmp(refname, GOT_REF_HEAD) != 0)
1114 continue;
1116 error = got_ref_open(&target_ref, repo, target, 0);
1117 if (error) {
1118 if (error->code == GOT_ERR_NOT_REF)
1119 continue;
1120 goto done;
1123 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1124 got_ref_close(target_ref);
1125 if (error)
1126 goto done;
1128 if (verbosity >= 0)
1129 printf("Setting %s to %s\n", GOT_REF_HEAD,
1130 got_ref_get_symref_target(symref));
1132 error = got_ref_write(symref, repo);
1133 got_ref_close(symref);
1134 break;
1137 if (verbosity >= 0)
1138 printf("Created %s repository '%s'\n",
1139 mirror_references ? "mirrored" : "cloned", repo_path);
1140 done:
1141 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1142 error = got_error_from_errno("close");
1143 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1144 error = got_error_from_errno("fclose");
1145 if (repo)
1146 got_repo_close(repo);
1147 TAILQ_FOREACH(pe, &refs, entry) {
1148 free((void *)pe->path);
1149 free(pe->data);
1151 got_pathlist_free(&refs);
1152 TAILQ_FOREACH(pe, &symrefs, entry) {
1153 free((void *)pe->path);
1154 free(pe->data);
1156 got_pathlist_free(&symrefs);
1157 free(pack_hash);
1158 free(proto);
1159 free(host);
1160 free(port);
1161 free(server_path);
1162 free(repo_name);
1163 free(default_destdir);
1164 free(gitconfig_path);
1165 free(git_url);
1166 return error;
1169 static const struct got_error *
1170 create_ref(const char *refname, struct got_object_id *id,
1171 const char *id_str, struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 struct got_reference *ref;
1176 printf("Creating %s: %s\n", refname, id_str);
1178 err = got_ref_alloc(&ref, refname, id);
1179 if (err)
1180 return err;
1182 err = got_ref_write(ref, repo);
1183 got_ref_close(ref);
1184 return err;
1187 static const struct got_error *
1188 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 char *new_id_str = NULL;
1193 struct got_object_id *old_id = NULL;
1195 err = got_object_id_str(&new_id_str, new_id);
1196 if (err)
1197 goto done;
1199 if (got_ref_is_symbolic(ref)) {
1200 struct got_reference *new_ref;
1201 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1202 if (err)
1203 goto done;
1204 printf("Deleting symbolic reference %s -> %s\n",
1205 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1206 err = got_ref_delete(ref, repo);
1207 if (err)
1208 goto done;
1209 printf("Setting %s to %s\n", got_ref_get_name(ref),
1210 new_id_str);
1211 err = got_ref_write(new_ref, repo);
1212 if (err)
1213 goto done;
1214 } else {
1215 err = got_ref_resolve(&old_id, repo, ref);
1216 if (err)
1217 goto done;
1218 if (got_object_id_cmp(old_id, new_id) != 0) {
1219 printf("Setting %s to %s\n",
1220 got_ref_get_name(ref), new_id_str);
1221 err = got_ref_change_ref(ref, new_id);
1222 if (err)
1223 goto done;
1224 err = got_ref_write(ref, repo);
1225 if (err)
1226 goto done;
1229 done:
1230 free(old_id);
1231 free(new_id_str);
1232 return err;
1235 __dead static void
1236 usage_fetch(void)
1238 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1239 "[remote-repository-name]\n", getprogname());
1240 exit(1);
1243 static const struct got_error *
1244 cmd_fetch(int argc, char *argv[])
1246 const struct got_error *error = NULL;
1247 char *cwd = NULL, *repo_path = NULL;
1248 const char *remote_name;
1249 char *proto = NULL, *host = NULL, *port = NULL;
1250 char *repo_name = NULL, *server_path = NULL;
1251 struct got_remote_repo *remotes, *remote = NULL;
1252 int nremotes;
1253 char *id_str = NULL;
1254 struct got_repository *repo = NULL;
1255 struct got_worktree *worktree = NULL;
1256 struct got_pathlist_head refs, symrefs;
1257 struct got_pathlist_entry *pe;
1258 struct got_object_id *pack_hash = NULL;
1259 int i, ch, fetchfd = -1;
1260 struct got_fetch_progress_arg fpa;
1261 int verbosity = 0;
1263 TAILQ_INIT(&refs);
1264 TAILQ_INIT(&symrefs);
1266 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1267 switch (ch) {
1268 case 'r':
1269 repo_path = realpath(optarg, NULL);
1270 if (repo_path == NULL)
1271 return got_error_from_errno2("realpath",
1272 optarg);
1273 got_path_strip_trailing_slashes(repo_path);
1274 break;
1275 case 'v':
1276 if (verbosity < 0)
1277 verbosity = 0;
1278 else if (verbosity < 3)
1279 verbosity++;
1280 break;
1281 case 'q':
1282 verbosity = -1;
1283 break;
1284 default:
1285 usage_fetch();
1286 break;
1289 argc -= optind;
1290 argv += optind;
1292 if (argc == 0)
1293 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1294 else if (argc == 1)
1295 remote_name = argv[0];
1296 else
1297 usage_fetch();
1299 cwd = getcwd(NULL, 0);
1300 if (cwd == NULL) {
1301 error = got_error_from_errno("getcwd");
1302 goto done;
1305 if (repo_path == NULL) {
1306 error = got_worktree_open(&worktree, cwd);
1307 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1308 goto done;
1309 else
1310 error = NULL;
1311 if (worktree) {
1312 repo_path =
1313 strdup(got_worktree_get_repo_path(worktree));
1314 if (repo_path == NULL)
1315 error = got_error_from_errno("strdup");
1316 if (error)
1317 goto done;
1318 } else {
1319 repo_path = strdup(cwd);
1320 if (repo_path == NULL) {
1321 error = got_error_from_errno("strdup");
1322 goto done;
1327 error = got_repo_open(&repo, repo_path, NULL);
1328 if (error)
1329 goto done;
1331 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1332 for (i = 0; i < nremotes; i++) {
1333 remote = &remotes[i];
1334 if (strcmp(remote->name, remote_name) == 0)
1335 break;
1337 if (i == nremotes) {
1338 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1339 goto done;
1342 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1343 &repo_name, remote->url);
1344 if (error)
1345 goto done;
1347 if (strcmp(proto, "git") == 0) {
1348 #ifndef PROFILE
1349 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1350 "sendfd dns inet unveil", NULL) == -1)
1351 err(1, "pledge");
1352 #endif
1353 } else if (strcmp(proto, "git+ssh") == 0 ||
1354 strcmp(proto, "ssh") == 0) {
1355 #ifndef PROFILE
1356 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1357 "sendfd unveil", NULL) == -1)
1358 err(1, "pledge");
1359 #endif
1360 } else if (strcmp(proto, "http") == 0 ||
1361 strcmp(proto, "git+http") == 0) {
1362 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1363 goto done;
1364 } else {
1365 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1366 goto done;
1369 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1370 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1371 error = got_error_from_errno2("unveil",
1372 GOT_FETCH_PATH_SSH);
1373 goto done;
1376 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1377 if (error)
1378 goto done;
1380 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1381 verbosity);
1382 if (error)
1383 goto done;
1385 if (verbosity >= 0)
1386 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1388 fpa.last_scaled_size[0] = '\0';
1389 fpa.last_p_indexed = -1;
1390 fpa.last_p_resolved = -1;
1391 fpa.verbosity = verbosity;
1392 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1393 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1394 if (error)
1395 goto done;
1397 if (pack_hash == NULL) {
1398 if (verbosity >= 0)
1399 printf("Already up-to-date\n");
1400 goto done;
1403 if (verbosity >= 0) {
1404 error = got_object_id_str(&id_str, pack_hash);
1405 if (error)
1406 goto done;
1407 printf("\nFetched %s.pack\n", id_str);
1408 free(id_str);
1409 id_str = NULL;
1412 /* Update references provided with the pack file. */
1413 TAILQ_FOREACH(pe, &refs, entry) {
1414 const char *refname = pe->path;
1415 struct got_object_id *id = pe->data;
1416 struct got_reference *ref;
1417 char *remote_refname;
1419 error = got_object_id_str(&id_str, id);
1420 if (error)
1421 goto done;
1423 if (strncmp("refs/tags/", refname, 10) == 0) {
1424 error = got_ref_open(&ref, repo, refname, 0);
1425 if (error) {
1426 if (error->code != GOT_ERR_NOT_REF)
1427 goto done;
1428 error = create_ref(refname, id, id_str, repo);
1429 if (error)
1430 goto done;
1431 } else {
1432 error = update_ref(ref, id, repo);
1433 got_ref_close(ref);
1434 if (error)
1435 goto done;
1437 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1438 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1439 remote_name, refname + 11) == -1) {
1440 error = got_error_from_errno("asprintf");
1441 goto done;
1444 error = got_ref_open(&ref, repo, remote_refname, 0);
1445 if (error) {
1446 if (error->code != GOT_ERR_NOT_REF)
1447 goto done;
1448 error = create_ref(remote_refname, id, id_str,
1449 repo);
1450 if (error)
1451 goto done;
1452 } else {
1453 error = update_ref(ref, id, repo);
1454 got_ref_close(ref);
1455 if (error)
1456 goto done;
1459 free(id_str);
1460 id_str = NULL;
1462 done:
1463 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1464 error = got_error_from_errno("close");
1465 if (repo)
1466 got_repo_close(repo);
1467 if (worktree)
1468 got_worktree_close(worktree);
1469 TAILQ_FOREACH(pe, &refs, entry) {
1470 free((void *)pe->path);
1471 free(pe->data);
1473 got_pathlist_free(&refs);
1474 TAILQ_FOREACH(pe, &symrefs, entry) {
1475 free((void *)pe->path);
1476 free(pe->data);
1478 got_pathlist_free(&symrefs);
1479 free(id_str);
1480 free(cwd);
1481 free(repo_path);
1482 free(pack_hash);
1483 free(proto);
1484 free(host);
1485 free(port);
1486 free(server_path);
1487 free(repo_name);
1488 return error;
1492 __dead static void
1493 usage_checkout(void)
1495 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1496 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1497 exit(1);
1500 static void
1501 show_worktree_base_ref_warning(void)
1503 fprintf(stderr, "%s: warning: could not create a reference "
1504 "to the work tree's base commit; the commit could be "
1505 "garbage-collected by Git; making the repository "
1506 "writable and running 'got update' will prevent this\n",
1507 getprogname());
1510 struct got_checkout_progress_arg {
1511 const char *worktree_path;
1512 int had_base_commit_ref_error;
1515 static const struct got_error *
1516 checkout_progress(void *arg, unsigned char status, const char *path)
1518 struct got_checkout_progress_arg *a = arg;
1520 /* Base commit bump happens silently. */
1521 if (status == GOT_STATUS_BUMP_BASE)
1522 return NULL;
1524 if (status == GOT_STATUS_BASE_REF_ERR) {
1525 a->had_base_commit_ref_error = 1;
1526 return NULL;
1529 while (path[0] == '/')
1530 path++;
1532 printf("%c %s/%s\n", status, a->worktree_path, path);
1533 return NULL;
1536 static const struct got_error *
1537 check_cancelled(void *arg)
1539 if (sigint_received || sigpipe_received)
1540 return got_error(GOT_ERR_CANCELLED);
1541 return NULL;
1544 static const struct got_error *
1545 check_linear_ancestry(struct got_object_id *commit_id,
1546 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1547 struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_object_id *yca_id;
1552 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1553 commit_id, base_commit_id, repo, check_cancelled, NULL);
1554 if (err)
1555 return err;
1557 if (yca_id == NULL)
1558 return got_error(GOT_ERR_ANCESTRY);
1561 * Require a straight line of history between the target commit
1562 * and the work tree's base commit.
1564 * Non-linear situations such as this require a rebase:
1566 * (commit) D F (base_commit)
1567 * \ /
1568 * C E
1569 * \ /
1570 * B (yca)
1571 * |
1572 * A
1574 * 'got update' only handles linear cases:
1575 * Update forwards in time: A (base/yca) - B - C - D (commit)
1576 * Update backwards in time: D (base) - C - B - A (commit/yca)
1578 if (allow_forwards_in_time_only) {
1579 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1580 return got_error(GOT_ERR_ANCESTRY);
1581 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1582 got_object_id_cmp(base_commit_id, yca_id) != 0)
1583 return got_error(GOT_ERR_ANCESTRY);
1585 free(yca_id);
1586 return NULL;
1589 static const struct got_error *
1590 check_same_branch(struct got_object_id *commit_id,
1591 struct got_reference *head_ref, struct got_object_id *yca_id,
1592 struct got_repository *repo)
1594 const struct got_error *err = NULL;
1595 struct got_commit_graph *graph = NULL;
1596 struct got_object_id *head_commit_id = NULL;
1597 int is_same_branch = 0;
1599 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1600 if (err)
1601 goto done;
1603 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1604 is_same_branch = 1;
1605 goto done;
1607 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1608 is_same_branch = 1;
1609 goto done;
1612 err = got_commit_graph_open(&graph, "/", 1);
1613 if (err)
1614 goto done;
1616 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1617 check_cancelled, NULL);
1618 if (err)
1619 goto done;
1621 for (;;) {
1622 struct got_object_id *id;
1623 err = got_commit_graph_iter_next(&id, graph, repo,
1624 check_cancelled, NULL);
1625 if (err) {
1626 if (err->code == GOT_ERR_ITER_COMPLETED)
1627 err = NULL;
1628 break;
1631 if (id) {
1632 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1633 break;
1634 if (got_object_id_cmp(id, commit_id) == 0) {
1635 is_same_branch = 1;
1636 break;
1640 done:
1641 if (graph)
1642 got_commit_graph_close(graph);
1643 free(head_commit_id);
1644 if (!err && !is_same_branch)
1645 err = got_error(GOT_ERR_ANCESTRY);
1646 return err;
1649 static const struct got_error *
1650 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1652 static char msg[512];
1653 const char *branch_name;
1655 if (got_ref_is_symbolic(ref))
1656 branch_name = got_ref_get_symref_target(ref);
1657 else
1658 branch_name = got_ref_get_name(ref);
1660 if (strncmp("refs/heads/", branch_name, 11) == 0)
1661 branch_name += 11;
1663 snprintf(msg, sizeof(msg),
1664 "target commit is not contained in branch '%s'; "
1665 "the branch to use must be specified with -b; "
1666 "if necessary a new branch can be created for "
1667 "this commit with 'got branch -c %s BRANCH_NAME'",
1668 branch_name, commit_id_str);
1670 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1673 static const struct got_error *
1674 cmd_checkout(int argc, char *argv[])
1676 const struct got_error *error = NULL;
1677 struct got_repository *repo = NULL;
1678 struct got_reference *head_ref = NULL;
1679 struct got_worktree *worktree = NULL;
1680 char *repo_path = NULL;
1681 char *worktree_path = NULL;
1682 const char *path_prefix = "";
1683 const char *branch_name = GOT_REF_HEAD;
1684 char *commit_id_str = NULL;
1685 int ch, same_path_prefix, allow_nonempty = 0;
1686 struct got_pathlist_head paths;
1687 struct got_checkout_progress_arg cpa;
1689 TAILQ_INIT(&paths);
1691 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1692 switch (ch) {
1693 case 'b':
1694 branch_name = optarg;
1695 break;
1696 case 'c':
1697 commit_id_str = strdup(optarg);
1698 if (commit_id_str == NULL)
1699 return got_error_from_errno("strdup");
1700 break;
1701 case 'E':
1702 allow_nonempty = 1;
1703 break;
1704 case 'p':
1705 path_prefix = optarg;
1706 break;
1707 default:
1708 usage_checkout();
1709 /* NOTREACHED */
1713 argc -= optind;
1714 argv += optind;
1716 #ifndef PROFILE
1717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1718 "unveil", NULL) == -1)
1719 err(1, "pledge");
1720 #endif
1721 if (argc == 1) {
1722 char *cwd, *base, *dotgit;
1723 repo_path = realpath(argv[0], NULL);
1724 if (repo_path == NULL)
1725 return got_error_from_errno2("realpath", argv[0]);
1726 cwd = getcwd(NULL, 0);
1727 if (cwd == NULL) {
1728 error = got_error_from_errno("getcwd");
1729 goto done;
1731 if (path_prefix[0]) {
1732 base = basename(path_prefix);
1733 if (base == NULL) {
1734 error = got_error_from_errno2("basename",
1735 path_prefix);
1736 goto done;
1738 } else {
1739 base = basename(repo_path);
1740 if (base == NULL) {
1741 error = got_error_from_errno2("basename",
1742 repo_path);
1743 goto done;
1746 dotgit = strstr(base, ".git");
1747 if (dotgit)
1748 *dotgit = '\0';
1749 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1750 error = got_error_from_errno("asprintf");
1751 free(cwd);
1752 goto done;
1754 free(cwd);
1755 } else if (argc == 2) {
1756 repo_path = realpath(argv[0], NULL);
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno2("realpath", argv[0]);
1759 goto done;
1761 worktree_path = realpath(argv[1], NULL);
1762 if (worktree_path == NULL) {
1763 if (errno != ENOENT) {
1764 error = got_error_from_errno2("realpath",
1765 argv[1]);
1766 goto done;
1768 worktree_path = strdup(argv[1]);
1769 if (worktree_path == NULL) {
1770 error = got_error_from_errno("strdup");
1771 goto done;
1774 } else
1775 usage_checkout();
1777 got_path_strip_trailing_slashes(repo_path);
1778 got_path_strip_trailing_slashes(worktree_path);
1780 error = got_repo_open(&repo, repo_path, NULL);
1781 if (error != NULL)
1782 goto done;
1784 /* Pre-create work tree path for unveil(2) */
1785 error = got_path_mkdir(worktree_path);
1786 if (error) {
1787 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1788 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1789 goto done;
1790 if (!allow_nonempty &&
1791 !got_path_dir_is_empty(worktree_path)) {
1792 error = got_error_path(worktree_path,
1793 GOT_ERR_DIR_NOT_EMPTY);
1794 goto done;
1798 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1799 if (error)
1800 goto done;
1802 error = got_ref_open(&head_ref, repo, branch_name, 0);
1803 if (error != NULL)
1804 goto done;
1806 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1807 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1808 goto done;
1810 error = got_worktree_open(&worktree, worktree_path);
1811 if (error != NULL)
1812 goto done;
1814 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1815 path_prefix);
1816 if (error != NULL)
1817 goto done;
1818 if (!same_path_prefix) {
1819 error = got_error(GOT_ERR_PATH_PREFIX);
1820 goto done;
1823 if (commit_id_str) {
1824 struct got_object_id *commit_id;
1825 error = got_repo_match_object_id(&commit_id, NULL,
1826 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1827 if (error)
1828 goto done;
1829 error = check_linear_ancestry(commit_id,
1830 got_worktree_get_base_commit_id(worktree), 0, repo);
1831 if (error != NULL) {
1832 free(commit_id);
1833 if (error->code == GOT_ERR_ANCESTRY) {
1834 error = checkout_ancestry_error(
1835 head_ref, commit_id_str);
1837 goto done;
1839 error = check_same_branch(commit_id, head_ref, NULL, repo);
1840 if (error) {
1841 if (error->code == GOT_ERR_ANCESTRY) {
1842 error = checkout_ancestry_error(
1843 head_ref, commit_id_str);
1845 goto done;
1847 error = got_worktree_set_base_commit_id(worktree, repo,
1848 commit_id);
1849 free(commit_id);
1850 if (error)
1851 goto done;
1854 error = got_pathlist_append(&paths, "", NULL);
1855 if (error)
1856 goto done;
1857 cpa.worktree_path = worktree_path;
1858 cpa.had_base_commit_ref_error = 0;
1859 error = got_worktree_checkout_files(worktree, &paths, repo,
1860 checkout_progress, &cpa, check_cancelled, NULL);
1861 if (error != NULL)
1862 goto done;
1864 printf("Now shut up and hack\n");
1865 if (cpa.had_base_commit_ref_error)
1866 show_worktree_base_ref_warning();
1867 done:
1868 got_pathlist_free(&paths);
1869 free(commit_id_str);
1870 free(repo_path);
1871 free(worktree_path);
1872 return error;
1875 __dead static void
1876 usage_update(void)
1878 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1879 getprogname());
1880 exit(1);
1883 static const struct got_error *
1884 update_progress(void *arg, unsigned char status, const char *path)
1886 int *did_something = arg;
1888 if (status == GOT_STATUS_EXISTS ||
1889 status == GOT_STATUS_BASE_REF_ERR)
1890 return NULL;
1892 *did_something = 1;
1894 /* Base commit bump happens silently. */
1895 if (status == GOT_STATUS_BUMP_BASE)
1896 return NULL;
1898 while (path[0] == '/')
1899 path++;
1900 printf("%c %s\n", status, path);
1901 return NULL;
1904 static const struct got_error *
1905 switch_head_ref(struct got_reference *head_ref,
1906 struct got_object_id *commit_id, struct got_worktree *worktree,
1907 struct got_repository *repo)
1909 const struct got_error *err = NULL;
1910 char *base_id_str;
1911 int ref_has_moved = 0;
1913 /* Trivial case: switching between two different references. */
1914 if (strcmp(got_ref_get_name(head_ref),
1915 got_worktree_get_head_ref_name(worktree)) != 0) {
1916 printf("Switching work tree from %s to %s\n",
1917 got_worktree_get_head_ref_name(worktree),
1918 got_ref_get_name(head_ref));
1919 return got_worktree_set_head_ref(worktree, head_ref);
1922 err = check_linear_ancestry(commit_id,
1923 got_worktree_get_base_commit_id(worktree), 0, repo);
1924 if (err) {
1925 if (err->code != GOT_ERR_ANCESTRY)
1926 return err;
1927 ref_has_moved = 1;
1929 if (!ref_has_moved)
1930 return NULL;
1932 /* Switching to a rebased branch with the same reference name. */
1933 err = got_object_id_str(&base_id_str,
1934 got_worktree_get_base_commit_id(worktree));
1935 if (err)
1936 return err;
1937 printf("Reference %s now points at a different branch\n",
1938 got_worktree_get_head_ref_name(worktree));
1939 printf("Switching work tree from %s to %s\n", base_id_str,
1940 got_worktree_get_head_ref_name(worktree));
1941 return NULL;
1944 static const struct got_error *
1945 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1947 const struct got_error *err;
1948 int in_progress;
1950 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1951 if (err)
1952 return err;
1953 if (in_progress)
1954 return got_error(GOT_ERR_REBASING);
1956 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1957 if (err)
1958 return err;
1959 if (in_progress)
1960 return got_error(GOT_ERR_HISTEDIT_BUSY);
1962 return NULL;
1965 static const struct got_error *
1966 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1967 char *argv[], struct got_worktree *worktree)
1969 const struct got_error *err = NULL;
1970 char *path;
1971 int i;
1973 if (argc == 0) {
1974 path = strdup("");
1975 if (path == NULL)
1976 return got_error_from_errno("strdup");
1977 return got_pathlist_append(paths, path, NULL);
1980 for (i = 0; i < argc; i++) {
1981 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1982 if (err)
1983 break;
1984 err = got_pathlist_append(paths, path, NULL);
1985 if (err) {
1986 free(path);
1987 break;
1991 return err;
1994 static const struct got_error *
1995 cmd_update(int argc, char *argv[])
1997 const struct got_error *error = NULL;
1998 struct got_repository *repo = NULL;
1999 struct got_worktree *worktree = NULL;
2000 char *worktree_path = NULL;
2001 struct got_object_id *commit_id = NULL;
2002 char *commit_id_str = NULL;
2003 const char *branch_name = NULL;
2004 struct got_reference *head_ref = NULL;
2005 struct got_pathlist_head paths;
2006 struct got_pathlist_entry *pe;
2007 int ch, did_something = 0;
2009 TAILQ_INIT(&paths);
2011 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2012 switch (ch) {
2013 case 'b':
2014 branch_name = optarg;
2015 break;
2016 case 'c':
2017 commit_id_str = strdup(optarg);
2018 if (commit_id_str == NULL)
2019 return got_error_from_errno("strdup");
2020 break;
2021 default:
2022 usage_update();
2023 /* NOTREACHED */
2027 argc -= optind;
2028 argv += optind;
2030 #ifndef PROFILE
2031 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2032 "unveil", NULL) == -1)
2033 err(1, "pledge");
2034 #endif
2035 worktree_path = getcwd(NULL, 0);
2036 if (worktree_path == NULL) {
2037 error = got_error_from_errno("getcwd");
2038 goto done;
2040 error = got_worktree_open(&worktree, worktree_path);
2041 if (error)
2042 goto done;
2044 error = check_rebase_or_histedit_in_progress(worktree);
2045 if (error)
2046 goto done;
2048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2049 NULL);
2050 if (error != NULL)
2051 goto done;
2053 error = apply_unveil(got_repo_get_path(repo), 0,
2054 got_worktree_get_root_path(worktree));
2055 if (error)
2056 goto done;
2058 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2059 if (error)
2060 goto done;
2062 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2063 got_worktree_get_head_ref_name(worktree), 0);
2064 if (error != NULL)
2065 goto done;
2066 if (commit_id_str == NULL) {
2067 error = got_ref_resolve(&commit_id, repo, head_ref);
2068 if (error != NULL)
2069 goto done;
2070 error = got_object_id_str(&commit_id_str, commit_id);
2071 if (error != NULL)
2072 goto done;
2073 } else {
2074 error = got_repo_match_object_id(&commit_id, NULL,
2075 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2076 free(commit_id_str);
2077 commit_id_str = NULL;
2078 if (error)
2079 goto done;
2080 error = got_object_id_str(&commit_id_str, commit_id);
2081 if (error)
2082 goto done;
2085 if (branch_name) {
2086 struct got_object_id *head_commit_id;
2087 TAILQ_FOREACH(pe, &paths, entry) {
2088 if (pe->path_len == 0)
2089 continue;
2090 error = got_error_msg(GOT_ERR_BAD_PATH,
2091 "switching between branches requires that "
2092 "the entire work tree gets updated");
2093 goto done;
2095 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2096 if (error)
2097 goto done;
2098 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2099 repo);
2100 free(head_commit_id);
2101 if (error != NULL)
2102 goto done;
2103 error = check_same_branch(commit_id, head_ref, NULL, repo);
2104 if (error)
2105 goto done;
2106 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2107 if (error)
2108 goto done;
2109 } else {
2110 error = check_linear_ancestry(commit_id,
2111 got_worktree_get_base_commit_id(worktree), 0, repo);
2112 if (error != NULL) {
2113 if (error->code == GOT_ERR_ANCESTRY)
2114 error = got_error(GOT_ERR_BRANCH_MOVED);
2115 goto done;
2117 error = check_same_branch(commit_id, head_ref, NULL, repo);
2118 if (error)
2119 goto done;
2122 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2123 commit_id) != 0) {
2124 error = got_worktree_set_base_commit_id(worktree, repo,
2125 commit_id);
2126 if (error)
2127 goto done;
2130 error = got_worktree_checkout_files(worktree, &paths, repo,
2131 update_progress, &did_something, check_cancelled, NULL);
2132 if (error != NULL)
2133 goto done;
2135 if (did_something)
2136 printf("Updated to commit %s\n", commit_id_str);
2137 else
2138 printf("Already up-to-date\n");
2139 done:
2140 free(worktree_path);
2141 TAILQ_FOREACH(pe, &paths, entry)
2142 free((char *)pe->path);
2143 got_pathlist_free(&paths);
2144 free(commit_id);
2145 free(commit_id_str);
2146 return error;
2149 static const struct got_error *
2150 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2151 const char *path, int diff_context, int ignore_whitespace,
2152 struct got_repository *repo)
2154 const struct got_error *err = NULL;
2155 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2157 if (blob_id1) {
2158 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2159 if (err)
2160 goto done;
2163 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2164 if (err)
2165 goto done;
2167 while (path[0] == '/')
2168 path++;
2169 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2170 ignore_whitespace, stdout);
2171 done:
2172 if (blob1)
2173 got_object_blob_close(blob1);
2174 got_object_blob_close(blob2);
2175 return err;
2178 static const struct got_error *
2179 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2180 const char *path, int diff_context, int ignore_whitespace,
2181 struct got_repository *repo)
2183 const struct got_error *err = NULL;
2184 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2185 struct got_diff_blob_output_unidiff_arg arg;
2187 if (tree_id1) {
2188 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2189 if (err)
2190 goto done;
2193 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2194 if (err)
2195 goto done;
2197 arg.diff_context = diff_context;
2198 arg.ignore_whitespace = ignore_whitespace;
2199 arg.outfile = stdout;
2200 while (path[0] == '/')
2201 path++;
2202 err = got_diff_tree(tree1, tree2, path, path, repo,
2203 got_diff_blob_output_unidiff, &arg, 1);
2204 done:
2205 if (tree1)
2206 got_object_tree_close(tree1);
2207 if (tree2)
2208 got_object_tree_close(tree2);
2209 return err;
2212 static const struct got_error *
2213 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2214 const char *path, int diff_context, struct got_repository *repo)
2216 const struct got_error *err = NULL;
2217 struct got_commit_object *pcommit = NULL;
2218 char *id_str1 = NULL, *id_str2 = NULL;
2219 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2220 struct got_object_qid *qid;
2222 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2223 if (qid != NULL) {
2224 err = got_object_open_as_commit(&pcommit, repo,
2225 qid->id);
2226 if (err)
2227 return err;
2230 if (path && path[0] != '\0') {
2231 int obj_type;
2232 err = got_object_id_by_path(&obj_id2, repo, id, path);
2233 if (err)
2234 goto done;
2235 err = got_object_id_str(&id_str2, obj_id2);
2236 if (err) {
2237 free(obj_id2);
2238 goto done;
2240 if (pcommit) {
2241 err = got_object_id_by_path(&obj_id1, repo,
2242 qid->id, path);
2243 if (err) {
2244 free(obj_id2);
2245 goto done;
2247 err = got_object_id_str(&id_str1, obj_id1);
2248 if (err) {
2249 free(obj_id2);
2250 goto done;
2253 err = got_object_get_type(&obj_type, repo, obj_id2);
2254 if (err) {
2255 free(obj_id2);
2256 goto done;
2258 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2259 switch (obj_type) {
2260 case GOT_OBJ_TYPE_BLOB:
2261 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2262 0, repo);
2263 break;
2264 case GOT_OBJ_TYPE_TREE:
2265 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2266 0, repo);
2267 break;
2268 default:
2269 err = got_error(GOT_ERR_OBJ_TYPE);
2270 break;
2272 free(obj_id1);
2273 free(obj_id2);
2274 } else {
2275 obj_id2 = got_object_commit_get_tree_id(commit);
2276 err = got_object_id_str(&id_str2, obj_id2);
2277 if (err)
2278 goto done;
2279 obj_id1 = got_object_commit_get_tree_id(pcommit);
2280 err = got_object_id_str(&id_str1, obj_id1);
2281 if (err)
2282 goto done;
2283 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2284 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2286 done:
2287 free(id_str1);
2288 free(id_str2);
2289 if (pcommit)
2290 got_object_commit_close(pcommit);
2291 return err;
2294 static char *
2295 get_datestr(time_t *time, char *datebuf)
2297 struct tm mytm, *tm;
2298 char *p, *s;
2300 tm = gmtime_r(time, &mytm);
2301 if (tm == NULL)
2302 return NULL;
2303 s = asctime_r(tm, datebuf);
2304 if (s == NULL)
2305 return NULL;
2306 p = strchr(s, '\n');
2307 if (p)
2308 *p = '\0';
2309 return s;
2312 static const struct got_error *
2313 match_logmsg(int *have_match, struct got_object_id *id,
2314 struct got_commit_object *commit, regex_t *regex)
2316 const struct got_error *err = NULL;
2317 regmatch_t regmatch;
2318 char *id_str = NULL, *logmsg = NULL;
2320 *have_match = 0;
2322 err = got_object_id_str(&id_str, id);
2323 if (err)
2324 return err;
2326 err = got_object_commit_get_logmsg(&logmsg, commit);
2327 if (err)
2328 goto done;
2330 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2331 *have_match = 1;
2332 done:
2333 free(id_str);
2334 free(logmsg);
2335 return err;
2338 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2340 static const struct got_error *
2341 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2342 struct got_repository *repo, const char *path, int show_patch,
2343 int diff_context, struct got_reflist_head *refs)
2345 const struct got_error *err = NULL;
2346 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2347 char datebuf[26];
2348 time_t committer_time;
2349 const char *author, *committer;
2350 char *refs_str = NULL;
2351 struct got_reflist_entry *re;
2353 SIMPLEQ_FOREACH(re, refs, entry) {
2354 char *s;
2355 const char *name;
2356 struct got_tag_object *tag = NULL;
2357 int cmp;
2359 name = got_ref_get_name(re->ref);
2360 if (strcmp(name, GOT_REF_HEAD) == 0)
2361 continue;
2362 if (strncmp(name, "refs/", 5) == 0)
2363 name += 5;
2364 if (strncmp(name, "got/", 4) == 0)
2365 continue;
2366 if (strncmp(name, "heads/", 6) == 0)
2367 name += 6;
2368 if (strncmp(name, "remotes/", 8) == 0)
2369 name += 8;
2370 if (strncmp(name, "tags/", 5) == 0) {
2371 err = got_object_open_as_tag(&tag, repo, re->id);
2372 if (err) {
2373 if (err->code != GOT_ERR_OBJ_TYPE)
2374 return err;
2375 /* Ref points at something other than a tag. */
2376 err = NULL;
2377 tag = NULL;
2380 cmp = got_object_id_cmp(tag ?
2381 got_object_tag_get_object_id(tag) : re->id, id);
2382 if (tag)
2383 got_object_tag_close(tag);
2384 if (cmp != 0)
2385 continue;
2386 s = refs_str;
2387 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2388 name) == -1) {
2389 err = got_error_from_errno("asprintf");
2390 free(s);
2391 return err;
2393 free(s);
2395 err = got_object_id_str(&id_str, id);
2396 if (err)
2397 return err;
2399 printf(GOT_COMMIT_SEP_STR);
2400 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2401 refs_str ? refs_str : "", refs_str ? ")" : "");
2402 free(id_str);
2403 id_str = NULL;
2404 free(refs_str);
2405 refs_str = NULL;
2406 printf("from: %s\n", got_object_commit_get_author(commit));
2407 committer_time = got_object_commit_get_committer_time(commit);
2408 datestr = get_datestr(&committer_time, datebuf);
2409 if (datestr)
2410 printf("date: %s UTC\n", datestr);
2411 author = got_object_commit_get_author(commit);
2412 committer = got_object_commit_get_committer(commit);
2413 if (strcmp(author, committer) != 0)
2414 printf("via: %s\n", committer);
2415 if (got_object_commit_get_nparents(commit) > 1) {
2416 const struct got_object_id_queue *parent_ids;
2417 struct got_object_qid *qid;
2418 int n = 1;
2419 parent_ids = got_object_commit_get_parent_ids(commit);
2420 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2421 err = got_object_id_str(&id_str, qid->id);
2422 if (err)
2423 return err;
2424 printf("parent %d: %s\n", n++, id_str);
2425 free(id_str);
2429 err = got_object_commit_get_logmsg(&logmsg0, commit);
2430 if (err)
2431 return err;
2433 logmsg = logmsg0;
2434 do {
2435 line = strsep(&logmsg, "\n");
2436 if (line)
2437 printf(" %s\n", line);
2438 } while (line);
2439 free(logmsg0);
2441 if (show_patch) {
2442 err = print_patch(commit, id, path, diff_context, repo);
2443 if (err == 0)
2444 printf("\n");
2447 if (fflush(stdout) != 0 && err == NULL)
2448 err = got_error_from_errno("fflush");
2449 return err;
2452 static const struct got_error *
2453 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2454 const char *path, int show_patch, const char *search_pattern,
2455 int diff_context, int limit, int log_branches,
2456 struct got_reflist_head *refs)
2458 const struct got_error *err;
2459 struct got_commit_graph *graph;
2460 regex_t regex;
2461 int have_match;
2463 if (search_pattern &&
2464 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2465 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2467 err = got_commit_graph_open(&graph, path, !log_branches);
2468 if (err)
2469 return err;
2470 err = got_commit_graph_iter_start(graph, root_id, repo,
2471 check_cancelled, NULL);
2472 if (err)
2473 goto done;
2474 for (;;) {
2475 struct got_commit_object *commit;
2476 struct got_object_id *id;
2478 if (sigint_received || sigpipe_received)
2479 break;
2481 err = got_commit_graph_iter_next(&id, graph, repo,
2482 check_cancelled, NULL);
2483 if (err) {
2484 if (err->code == GOT_ERR_ITER_COMPLETED)
2485 err = NULL;
2486 break;
2488 if (id == NULL)
2489 break;
2491 err = got_object_open_as_commit(&commit, repo, id);
2492 if (err)
2493 break;
2495 if (search_pattern) {
2496 err = match_logmsg(&have_match, id, commit, &regex);
2497 if (err) {
2498 got_object_commit_close(commit);
2499 break;
2501 if (have_match == 0) {
2502 got_object_commit_close(commit);
2503 continue;
2507 err = print_commit(commit, id, repo, path, show_patch,
2508 diff_context, refs);
2509 got_object_commit_close(commit);
2510 if (err || (limit && --limit == 0))
2511 break;
2513 done:
2514 if (search_pattern)
2515 regfree(&regex);
2516 got_commit_graph_close(graph);
2517 return err;
2520 __dead static void
2521 usage_log(void)
2523 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2524 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2525 exit(1);
2528 static int
2529 get_default_log_limit(void)
2531 const char *got_default_log_limit;
2532 long long n;
2533 const char *errstr;
2535 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2536 if (got_default_log_limit == NULL)
2537 return 0;
2538 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2539 if (errstr != NULL)
2540 return 0;
2541 return n;
2544 static const struct got_error *
2545 cmd_log(int argc, char *argv[])
2547 const struct got_error *error;
2548 struct got_repository *repo = NULL;
2549 struct got_worktree *worktree = NULL;
2550 struct got_commit_object *commit = NULL;
2551 struct got_object_id *id = NULL;
2552 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2553 const char *start_commit = NULL, *search_pattern = NULL;
2554 int diff_context = -1, ch;
2555 int show_patch = 0, limit = 0, log_branches = 0;
2556 const char *errstr;
2557 struct got_reflist_head refs;
2559 SIMPLEQ_INIT(&refs);
2561 #ifndef PROFILE
2562 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2563 NULL)
2564 == -1)
2565 err(1, "pledge");
2566 #endif
2568 limit = get_default_log_limit();
2570 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2571 switch (ch) {
2572 case 'p':
2573 show_patch = 1;
2574 break;
2575 case 'c':
2576 start_commit = optarg;
2577 break;
2578 case 'C':
2579 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2580 &errstr);
2581 if (errstr != NULL)
2582 err(1, "-C option %s", errstr);
2583 break;
2584 case 'l':
2585 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2586 if (errstr != NULL)
2587 err(1, "-l option %s", errstr);
2588 break;
2589 case 'b':
2590 log_branches = 1;
2591 break;
2592 case 'r':
2593 repo_path = realpath(optarg, NULL);
2594 if (repo_path == NULL)
2595 return got_error_from_errno2("realpath",
2596 optarg);
2597 got_path_strip_trailing_slashes(repo_path);
2598 break;
2599 case 's':
2600 search_pattern = optarg;
2601 break;
2602 default:
2603 usage_log();
2604 /* NOTREACHED */
2608 argc -= optind;
2609 argv += optind;
2611 if (diff_context == -1)
2612 diff_context = 3;
2613 else if (!show_patch)
2614 errx(1, "-C reguires -p");
2616 cwd = getcwd(NULL, 0);
2617 if (cwd == NULL) {
2618 error = got_error_from_errno("getcwd");
2619 goto done;
2622 error = got_worktree_open(&worktree, cwd);
2623 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2624 goto done;
2625 error = NULL;
2627 if (argc == 0) {
2628 path = strdup("");
2629 if (path == NULL) {
2630 error = got_error_from_errno("strdup");
2631 goto done;
2633 } else if (argc == 1) {
2634 if (worktree) {
2635 error = got_worktree_resolve_path(&path, worktree,
2636 argv[0]);
2637 if (error)
2638 goto done;
2639 } else {
2640 path = strdup(argv[0]);
2641 if (path == NULL) {
2642 error = got_error_from_errno("strdup");
2643 goto done;
2646 } else
2647 usage_log();
2649 if (repo_path == NULL) {
2650 repo_path = worktree ?
2651 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2653 if (repo_path == NULL) {
2654 error = got_error_from_errno("strdup");
2655 goto done;
2658 error = got_repo_open(&repo, repo_path, NULL);
2659 if (error != NULL)
2660 goto done;
2662 error = apply_unveil(got_repo_get_path(repo), 1,
2663 worktree ? got_worktree_get_root_path(worktree) : NULL);
2664 if (error)
2665 goto done;
2667 if (start_commit == NULL) {
2668 struct got_reference *head_ref;
2669 error = got_ref_open(&head_ref, repo,
2670 worktree ? got_worktree_get_head_ref_name(worktree)
2671 : GOT_REF_HEAD, 0);
2672 if (error != NULL)
2673 return error;
2674 error = got_ref_resolve(&id, repo, head_ref);
2675 got_ref_close(head_ref);
2676 if (error != NULL)
2677 return error;
2678 error = got_object_open_as_commit(&commit, repo, id);
2679 } else {
2680 struct got_reference *ref;
2681 error = got_ref_open(&ref, repo, start_commit, 0);
2682 if (error == NULL) {
2683 int obj_type;
2684 error = got_ref_resolve(&id, repo, ref);
2685 got_ref_close(ref);
2686 if (error != NULL)
2687 goto done;
2688 error = got_object_get_type(&obj_type, repo, id);
2689 if (error != NULL)
2690 goto done;
2691 if (obj_type == GOT_OBJ_TYPE_TAG) {
2692 struct got_tag_object *tag;
2693 error = got_object_open_as_tag(&tag, repo, id);
2694 if (error != NULL)
2695 goto done;
2696 if (got_object_tag_get_object_type(tag) !=
2697 GOT_OBJ_TYPE_COMMIT) {
2698 got_object_tag_close(tag);
2699 error = got_error(GOT_ERR_OBJ_TYPE);
2700 goto done;
2702 free(id);
2703 id = got_object_id_dup(
2704 got_object_tag_get_object_id(tag));
2705 if (id == NULL)
2706 error = got_error_from_errno(
2707 "got_object_id_dup");
2708 got_object_tag_close(tag);
2709 if (error)
2710 goto done;
2711 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2712 error = got_error(GOT_ERR_OBJ_TYPE);
2713 goto done;
2715 error = got_object_open_as_commit(&commit, repo, id);
2716 if (error != NULL)
2717 goto done;
2719 if (commit == NULL) {
2720 error = got_repo_match_object_id_prefix(&id,
2721 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2722 if (error != NULL)
2723 return error;
2726 if (error != NULL)
2727 goto done;
2729 if (worktree) {
2730 const char *prefix = got_worktree_get_path_prefix(worktree);
2731 char *p;
2732 if (asprintf(&p, "%s%s%s", prefix,
2733 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2734 error = got_error_from_errno("asprintf");
2735 goto done;
2737 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2738 free(p);
2739 } else
2740 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2741 if (error != NULL)
2742 goto done;
2743 if (in_repo_path) {
2744 free(path);
2745 path = in_repo_path;
2748 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2749 if (error)
2750 goto done;
2752 error = print_commits(id, repo, path, show_patch, search_pattern,
2753 diff_context, limit, log_branches, &refs);
2754 done:
2755 free(path);
2756 free(repo_path);
2757 free(cwd);
2758 free(id);
2759 if (worktree)
2760 got_worktree_close(worktree);
2761 if (repo) {
2762 const struct got_error *repo_error;
2763 repo_error = got_repo_close(repo);
2764 if (error == NULL)
2765 error = repo_error;
2767 got_ref_list_free(&refs);
2768 return error;
2771 __dead static void
2772 usage_diff(void)
2774 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2775 "[-w] [object1 object2 | path]\n", getprogname());
2776 exit(1);
2779 struct print_diff_arg {
2780 struct got_repository *repo;
2781 struct got_worktree *worktree;
2782 int diff_context;
2783 const char *id_str;
2784 int header_shown;
2785 int diff_staged;
2786 int ignore_whitespace;
2789 static const struct got_error *
2790 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2791 const char *path, struct got_object_id *blob_id,
2792 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2793 int dirfd, const char *de_name)
2795 struct print_diff_arg *a = arg;
2796 const struct got_error *err = NULL;
2797 struct got_blob_object *blob1 = NULL;
2798 int fd = -1;
2799 FILE *f2 = NULL;
2800 char *abspath = NULL, *label1 = NULL;
2801 struct stat sb;
2803 if (a->diff_staged) {
2804 if (staged_status != GOT_STATUS_MODIFY &&
2805 staged_status != GOT_STATUS_ADD &&
2806 staged_status != GOT_STATUS_DELETE)
2807 return NULL;
2808 } else {
2809 if (staged_status == GOT_STATUS_DELETE)
2810 return NULL;
2811 if (status == GOT_STATUS_NONEXISTENT)
2812 return got_error_set_errno(ENOENT, path);
2813 if (status != GOT_STATUS_MODIFY &&
2814 status != GOT_STATUS_ADD &&
2815 status != GOT_STATUS_DELETE &&
2816 status != GOT_STATUS_CONFLICT)
2817 return NULL;
2820 if (!a->header_shown) {
2821 printf("diff %s %s%s\n", a->id_str,
2822 got_worktree_get_root_path(a->worktree),
2823 a->diff_staged ? " (staged changes)" : "");
2824 a->header_shown = 1;
2827 if (a->diff_staged) {
2828 const char *label1 = NULL, *label2 = NULL;
2829 switch (staged_status) {
2830 case GOT_STATUS_MODIFY:
2831 label1 = path;
2832 label2 = path;
2833 break;
2834 case GOT_STATUS_ADD:
2835 label2 = path;
2836 break;
2837 case GOT_STATUS_DELETE:
2838 label1 = path;
2839 break;
2840 default:
2841 return got_error(GOT_ERR_FILE_STATUS);
2843 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2844 label1, label2, a->diff_context, a->ignore_whitespace,
2845 a->repo, stdout);
2848 if (staged_status == GOT_STATUS_ADD ||
2849 staged_status == GOT_STATUS_MODIFY) {
2850 char *id_str;
2851 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2852 8192);
2853 if (err)
2854 goto done;
2855 err = got_object_id_str(&id_str, staged_blob_id);
2856 if (err)
2857 goto done;
2858 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2859 err = got_error_from_errno("asprintf");
2860 free(id_str);
2861 goto done;
2863 free(id_str);
2864 } else if (status != GOT_STATUS_ADD) {
2865 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2866 if (err)
2867 goto done;
2870 if (status != GOT_STATUS_DELETE) {
2871 if (asprintf(&abspath, "%s/%s",
2872 got_worktree_get_root_path(a->worktree), path) == -1) {
2873 err = got_error_from_errno("asprintf");
2874 goto done;
2877 if (dirfd != -1) {
2878 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2879 if (fd == -1) {
2880 err = got_error_from_errno2("openat", abspath);
2881 goto done;
2883 } else {
2884 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2885 if (fd == -1) {
2886 err = got_error_from_errno2("open", abspath);
2887 goto done;
2890 if (fstat(fd, &sb) == -1) {
2891 err = got_error_from_errno2("fstat", abspath);
2892 goto done;
2894 f2 = fdopen(fd, "r");
2895 if (f2 == NULL) {
2896 err = got_error_from_errno2("fdopen", abspath);
2897 goto done;
2899 fd = -1;
2900 } else
2901 sb.st_size = 0;
2903 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2904 a->diff_context, a->ignore_whitespace, stdout);
2905 done:
2906 if (blob1)
2907 got_object_blob_close(blob1);
2908 if (f2 && fclose(f2) == EOF && err == NULL)
2909 err = got_error_from_errno("fclose");
2910 if (fd != -1 && close(fd) == -1 && err == NULL)
2911 err = got_error_from_errno("close");
2912 free(abspath);
2913 return err;
2916 static const struct got_error *
2917 cmd_diff(int argc, char *argv[])
2919 const struct got_error *error;
2920 struct got_repository *repo = NULL;
2921 struct got_worktree *worktree = NULL;
2922 char *cwd = NULL, *repo_path = NULL;
2923 struct got_object_id *id1 = NULL, *id2 = NULL;
2924 const char *id_str1 = NULL, *id_str2 = NULL;
2925 char *label1 = NULL, *label2 = NULL;
2926 int type1, type2;
2927 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2928 const char *errstr;
2929 char *path = NULL;
2931 #ifndef PROFILE
2932 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2933 NULL) == -1)
2934 err(1, "pledge");
2935 #endif
2937 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2938 switch (ch) {
2939 case 'C':
2940 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2941 &errstr);
2942 if (errstr != NULL)
2943 err(1, "-C option %s", errstr);
2944 break;
2945 case 'r':
2946 repo_path = realpath(optarg, NULL);
2947 if (repo_path == NULL)
2948 return got_error_from_errno2("realpath",
2949 optarg);
2950 got_path_strip_trailing_slashes(repo_path);
2951 break;
2952 case 's':
2953 diff_staged = 1;
2954 break;
2955 case 'w':
2956 ignore_whitespace = 1;
2957 break;
2958 default:
2959 usage_diff();
2960 /* NOTREACHED */
2964 argc -= optind;
2965 argv += optind;
2967 cwd = getcwd(NULL, 0);
2968 if (cwd == NULL) {
2969 error = got_error_from_errno("getcwd");
2970 goto done;
2972 error = got_worktree_open(&worktree, cwd);
2973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2974 goto done;
2975 if (argc <= 1) {
2976 if (worktree == NULL) {
2977 error = got_error(GOT_ERR_NOT_WORKTREE);
2978 goto done;
2980 if (repo_path)
2981 errx(1,
2982 "-r option can't be used when diffing a work tree");
2983 repo_path = strdup(got_worktree_get_repo_path(worktree));
2984 if (repo_path == NULL) {
2985 error = got_error_from_errno("strdup");
2986 goto done;
2988 if (argc == 1) {
2989 error = got_worktree_resolve_path(&path, worktree,
2990 argv[0]);
2991 if (error)
2992 goto done;
2993 } else {
2994 path = strdup("");
2995 if (path == NULL) {
2996 error = got_error_from_errno("strdup");
2997 goto done;
3000 } else if (argc == 2) {
3001 if (diff_staged)
3002 errx(1, "-s option can't be used when diffing "
3003 "objects in repository");
3004 id_str1 = argv[0];
3005 id_str2 = argv[1];
3006 if (worktree && repo_path == NULL) {
3007 repo_path =
3008 strdup(got_worktree_get_repo_path(worktree));
3009 if (repo_path == NULL) {
3010 error = got_error_from_errno("strdup");
3011 goto done;
3014 } else
3015 usage_diff();
3017 if (repo_path == NULL) {
3018 repo_path = getcwd(NULL, 0);
3019 if (repo_path == NULL)
3020 return got_error_from_errno("getcwd");
3023 error = got_repo_open(&repo, repo_path, NULL);
3024 free(repo_path);
3025 if (error != NULL)
3026 goto done;
3028 error = apply_unveil(got_repo_get_path(repo), 1,
3029 worktree ? got_worktree_get_root_path(worktree) : NULL);
3030 if (error)
3031 goto done;
3033 if (argc <= 1) {
3034 struct print_diff_arg arg;
3035 struct got_pathlist_head paths;
3036 char *id_str;
3038 TAILQ_INIT(&paths);
3040 error = got_object_id_str(&id_str,
3041 got_worktree_get_base_commit_id(worktree));
3042 if (error)
3043 goto done;
3044 arg.repo = repo;
3045 arg.worktree = worktree;
3046 arg.diff_context = diff_context;
3047 arg.id_str = id_str;
3048 arg.header_shown = 0;
3049 arg.diff_staged = diff_staged;
3050 arg.ignore_whitespace = ignore_whitespace;
3052 error = got_pathlist_append(&paths, path, NULL);
3053 if (error)
3054 goto done;
3056 error = got_worktree_status(worktree, &paths, repo, print_diff,
3057 &arg, check_cancelled, NULL);
3058 free(id_str);
3059 got_pathlist_free(&paths);
3060 goto done;
3063 error = got_repo_match_object_id(&id1, &label1, id_str1,
3064 GOT_OBJ_TYPE_ANY, 1, repo);
3065 if (error)
3066 goto done;
3068 error = got_repo_match_object_id(&id2, &label2, id_str2,
3069 GOT_OBJ_TYPE_ANY, 1, repo);
3070 if (error)
3071 goto done;
3073 error = got_object_get_type(&type1, repo, id1);
3074 if (error)
3075 goto done;
3077 error = got_object_get_type(&type2, repo, id2);
3078 if (error)
3079 goto done;
3081 if (type1 != type2) {
3082 error = got_error(GOT_ERR_OBJ_TYPE);
3083 goto done;
3086 switch (type1) {
3087 case GOT_OBJ_TYPE_BLOB:
3088 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3089 diff_context, ignore_whitespace, repo, stdout);
3090 break;
3091 case GOT_OBJ_TYPE_TREE:
3092 error = got_diff_objects_as_trees(id1, id2, "", "",
3093 diff_context, ignore_whitespace, repo, stdout);
3094 break;
3095 case GOT_OBJ_TYPE_COMMIT:
3096 printf("diff %s %s\n", label1, label2);
3097 error = got_diff_objects_as_commits(id1, id2, diff_context,
3098 ignore_whitespace, repo, stdout);
3099 break;
3100 default:
3101 error = got_error(GOT_ERR_OBJ_TYPE);
3103 done:
3104 free(label1);
3105 free(label2);
3106 free(id1);
3107 free(id2);
3108 free(path);
3109 if (worktree)
3110 got_worktree_close(worktree);
3111 if (repo) {
3112 const struct got_error *repo_error;
3113 repo_error = got_repo_close(repo);
3114 if (error == NULL)
3115 error = repo_error;
3117 return error;
3120 __dead static void
3121 usage_blame(void)
3123 fprintf(stderr,
3124 "usage: %s blame [-c commit] [-r repository-path] path\n",
3125 getprogname());
3126 exit(1);
3129 struct blame_line {
3130 int annotated;
3131 char *id_str;
3132 char *committer;
3133 char datebuf[11]; /* YYYY-MM-DD + NUL */
3136 struct blame_cb_args {
3137 struct blame_line *lines;
3138 int nlines;
3139 int nlines_prec;
3140 int lineno_cur;
3141 off_t *line_offsets;
3142 FILE *f;
3143 struct got_repository *repo;
3146 static const struct got_error *
3147 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3149 const struct got_error *err = NULL;
3150 struct blame_cb_args *a = arg;
3151 struct blame_line *bline;
3152 char *line = NULL;
3153 size_t linesize = 0;
3154 struct got_commit_object *commit = NULL;
3155 off_t offset;
3156 struct tm tm;
3157 time_t committer_time;
3159 if (nlines != a->nlines ||
3160 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3161 return got_error(GOT_ERR_RANGE);
3163 if (sigint_received)
3164 return got_error(GOT_ERR_ITER_COMPLETED);
3166 if (lineno == -1)
3167 return NULL; /* no change in this commit */
3169 /* Annotate this line. */
3170 bline = &a->lines[lineno - 1];
3171 if (bline->annotated)
3172 return NULL;
3173 err = got_object_id_str(&bline->id_str, id);
3174 if (err)
3175 return err;
3177 err = got_object_open_as_commit(&commit, a->repo, id);
3178 if (err)
3179 goto done;
3181 bline->committer = strdup(got_object_commit_get_committer(commit));
3182 if (bline->committer == NULL) {
3183 err = got_error_from_errno("strdup");
3184 goto done;
3187 committer_time = got_object_commit_get_committer_time(commit);
3188 if (localtime_r(&committer_time, &tm) == NULL)
3189 return got_error_from_errno("localtime_r");
3190 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3191 &tm) >= sizeof(bline->datebuf)) {
3192 err = got_error(GOT_ERR_NO_SPACE);
3193 goto done;
3195 bline->annotated = 1;
3197 /* Print lines annotated so far. */
3198 bline = &a->lines[a->lineno_cur - 1];
3199 if (!bline->annotated)
3200 goto done;
3202 offset = a->line_offsets[a->lineno_cur - 1];
3203 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3204 err = got_error_from_errno("fseeko");
3205 goto done;
3208 while (bline->annotated) {
3209 char *smallerthan, *at, *nl, *committer;
3210 size_t len;
3212 if (getline(&line, &linesize, a->f) == -1) {
3213 if (ferror(a->f))
3214 err = got_error_from_errno("getline");
3215 break;
3218 committer = bline->committer;
3219 smallerthan = strchr(committer, '<');
3220 if (smallerthan && smallerthan[1] != '\0')
3221 committer = smallerthan + 1;
3222 at = strchr(committer, '@');
3223 if (at)
3224 *at = '\0';
3225 len = strlen(committer);
3226 if (len >= 9)
3227 committer[8] = '\0';
3229 nl = strchr(line, '\n');
3230 if (nl)
3231 *nl = '\0';
3232 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3233 bline->id_str, bline->datebuf, committer, line);
3235 a->lineno_cur++;
3236 bline = &a->lines[a->lineno_cur - 1];
3238 done:
3239 if (commit)
3240 got_object_commit_close(commit);
3241 free(line);
3242 return err;
3245 static const struct got_error *
3246 cmd_blame(int argc, char *argv[])
3248 const struct got_error *error;
3249 struct got_repository *repo = NULL;
3250 struct got_worktree *worktree = NULL;
3251 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3252 struct got_object_id *obj_id = NULL;
3253 struct got_object_id *commit_id = NULL;
3254 struct got_blob_object *blob = NULL;
3255 char *commit_id_str = NULL;
3256 struct blame_cb_args bca;
3257 int ch, obj_type, i;
3258 size_t filesize;
3260 memset(&bca, 0, sizeof(bca));
3262 #ifndef PROFILE
3263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3264 NULL) == -1)
3265 err(1, "pledge");
3266 #endif
3268 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3269 switch (ch) {
3270 case 'c':
3271 commit_id_str = optarg;
3272 break;
3273 case 'r':
3274 repo_path = realpath(optarg, NULL);
3275 if (repo_path == NULL)
3276 return got_error_from_errno2("realpath",
3277 optarg);
3278 got_path_strip_trailing_slashes(repo_path);
3279 break;
3280 default:
3281 usage_blame();
3282 /* NOTREACHED */
3286 argc -= optind;
3287 argv += optind;
3289 if (argc == 1)
3290 path = argv[0];
3291 else
3292 usage_blame();
3294 cwd = getcwd(NULL, 0);
3295 if (cwd == NULL) {
3296 error = got_error_from_errno("getcwd");
3297 goto done;
3299 if (repo_path == NULL) {
3300 error = got_worktree_open(&worktree, cwd);
3301 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3302 goto done;
3303 else
3304 error = NULL;
3305 if (worktree) {
3306 repo_path =
3307 strdup(got_worktree_get_repo_path(worktree));
3308 if (repo_path == NULL)
3309 error = got_error_from_errno("strdup");
3310 if (error)
3311 goto done;
3312 } else {
3313 repo_path = strdup(cwd);
3314 if (repo_path == NULL) {
3315 error = got_error_from_errno("strdup");
3316 goto done;
3321 error = got_repo_open(&repo, repo_path, NULL);
3322 if (error != NULL)
3323 goto done;
3325 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3326 if (error)
3327 goto done;
3329 if (worktree) {
3330 const char *prefix = got_worktree_get_path_prefix(worktree);
3331 char *p, *worktree_subdir = cwd +
3332 strlen(got_worktree_get_root_path(worktree));
3333 if (asprintf(&p, "%s%s%s%s%s",
3334 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3335 worktree_subdir, worktree_subdir[0] ? "/" : "",
3336 path) == -1) {
3337 error = got_error_from_errno("asprintf");
3338 goto done;
3340 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3341 free(p);
3342 } else {
3343 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3345 if (error)
3346 goto done;
3348 if (commit_id_str == NULL) {
3349 struct got_reference *head_ref;
3350 error = got_ref_open(&head_ref, repo, worktree ?
3351 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3352 if (error != NULL)
3353 goto done;
3354 error = got_ref_resolve(&commit_id, repo, head_ref);
3355 got_ref_close(head_ref);
3356 if (error != NULL)
3357 goto done;
3358 } else {
3359 error = got_repo_match_object_id(&commit_id, NULL,
3360 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3361 if (error)
3362 goto done;
3365 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3366 if (error)
3367 goto done;
3369 error = got_object_get_type(&obj_type, repo, obj_id);
3370 if (error)
3371 goto done;
3373 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3374 error = got_error(GOT_ERR_OBJ_TYPE);
3375 goto done;
3378 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3379 if (error)
3380 goto done;
3381 bca.f = got_opentemp();
3382 if (bca.f == NULL) {
3383 error = got_error_from_errno("got_opentemp");
3384 goto done;
3386 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3387 &bca.line_offsets, bca.f, blob);
3388 if (error || bca.nlines == 0)
3389 goto done;
3391 /* Don't include \n at EOF in the blame line count. */
3392 if (bca.line_offsets[bca.nlines - 1] == filesize)
3393 bca.nlines--;
3395 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3396 if (bca.lines == NULL) {
3397 error = got_error_from_errno("calloc");
3398 goto done;
3400 bca.lineno_cur = 1;
3401 bca.nlines_prec = 0;
3402 i = bca.nlines;
3403 while (i > 0) {
3404 i /= 10;
3405 bca.nlines_prec++;
3407 bca.repo = repo;
3409 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3410 check_cancelled, NULL);
3411 done:
3412 free(in_repo_path);
3413 free(repo_path);
3414 free(cwd);
3415 free(commit_id);
3416 free(obj_id);
3417 if (blob)
3418 got_object_blob_close(blob);
3419 if (worktree)
3420 got_worktree_close(worktree);
3421 if (repo) {
3422 const struct got_error *repo_error;
3423 repo_error = got_repo_close(repo);
3424 if (error == NULL)
3425 error = repo_error;
3427 if (bca.lines) {
3428 for (i = 0; i < bca.nlines; i++) {
3429 struct blame_line *bline = &bca.lines[i];
3430 free(bline->id_str);
3431 free(bline->committer);
3433 free(bca.lines);
3435 free(bca.line_offsets);
3436 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3437 error = got_error_from_errno("fclose");
3438 return error;
3441 __dead static void
3442 usage_tree(void)
3444 fprintf(stderr,
3445 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3446 getprogname());
3447 exit(1);
3450 static void
3451 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3452 const char *root_path)
3454 int is_root_path = (strcmp(path, root_path) == 0);
3455 const char *modestr = "";
3456 mode_t mode = got_tree_entry_get_mode(te);
3458 path += strlen(root_path);
3459 while (path[0] == '/')
3460 path++;
3462 if (got_object_tree_entry_is_submodule(te))
3463 modestr = "$";
3464 else if (S_ISLNK(mode))
3465 modestr = "@";
3466 else if (S_ISDIR(mode))
3467 modestr = "/";
3468 else if (mode & S_IXUSR)
3469 modestr = "*";
3471 printf("%s%s%s%s%s\n", id ? id : "", path,
3472 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3475 static const struct got_error *
3476 print_tree(const char *path, struct got_object_id *commit_id,
3477 int show_ids, int recurse, const char *root_path,
3478 struct got_repository *repo)
3480 const struct got_error *err = NULL;
3481 struct got_object_id *tree_id = NULL;
3482 struct got_tree_object *tree = NULL;
3483 int nentries, i;
3485 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3486 if (err)
3487 goto done;
3489 err = got_object_open_as_tree(&tree, repo, tree_id);
3490 if (err)
3491 goto done;
3492 nentries = got_object_tree_get_nentries(tree);
3493 for (i = 0; i < nentries; i++) {
3494 struct got_tree_entry *te;
3495 char *id = NULL;
3497 if (sigint_received || sigpipe_received)
3498 break;
3500 te = got_object_tree_get_entry(tree, i);
3501 if (show_ids) {
3502 char *id_str;
3503 err = got_object_id_str(&id_str,
3504 got_tree_entry_get_id(te));
3505 if (err)
3506 goto done;
3507 if (asprintf(&id, "%s ", id_str) == -1) {
3508 err = got_error_from_errno("asprintf");
3509 free(id_str);
3510 goto done;
3512 free(id_str);
3514 print_entry(te, id, path, root_path);
3515 free(id);
3517 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3518 char *child_path;
3519 if (asprintf(&child_path, "%s%s%s", path,
3520 path[0] == '/' && path[1] == '\0' ? "" : "/",
3521 got_tree_entry_get_name(te)) == -1) {
3522 err = got_error_from_errno("asprintf");
3523 goto done;
3525 err = print_tree(child_path, commit_id, show_ids, 1,
3526 root_path, repo);
3527 free(child_path);
3528 if (err)
3529 goto done;
3532 done:
3533 if (tree)
3534 got_object_tree_close(tree);
3535 free(tree_id);
3536 return err;
3539 static const struct got_error *
3540 cmd_tree(int argc, char *argv[])
3542 const struct got_error *error;
3543 struct got_repository *repo = NULL;
3544 struct got_worktree *worktree = NULL;
3545 const char *path;
3546 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3547 struct got_object_id *commit_id = NULL;
3548 char *commit_id_str = NULL;
3549 int show_ids = 0, recurse = 0;
3550 int ch;
3552 #ifndef PROFILE
3553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3554 NULL) == -1)
3555 err(1, "pledge");
3556 #endif
3558 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3559 switch (ch) {
3560 case 'c':
3561 commit_id_str = optarg;
3562 break;
3563 case 'r':
3564 repo_path = realpath(optarg, NULL);
3565 if (repo_path == NULL)
3566 return got_error_from_errno2("realpath",
3567 optarg);
3568 got_path_strip_trailing_slashes(repo_path);
3569 break;
3570 case 'i':
3571 show_ids = 1;
3572 break;
3573 case 'R':
3574 recurse = 1;
3575 break;
3576 default:
3577 usage_tree();
3578 /* NOTREACHED */
3582 argc -= optind;
3583 argv += optind;
3585 if (argc == 1)
3586 path = argv[0];
3587 else if (argc > 1)
3588 usage_tree();
3589 else
3590 path = NULL;
3592 cwd = getcwd(NULL, 0);
3593 if (cwd == NULL) {
3594 error = got_error_from_errno("getcwd");
3595 goto done;
3597 if (repo_path == NULL) {
3598 error = got_worktree_open(&worktree, cwd);
3599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3600 goto done;
3601 else
3602 error = NULL;
3603 if (worktree) {
3604 repo_path =
3605 strdup(got_worktree_get_repo_path(worktree));
3606 if (repo_path == NULL)
3607 error = got_error_from_errno("strdup");
3608 if (error)
3609 goto done;
3610 } else {
3611 repo_path = strdup(cwd);
3612 if (repo_path == NULL) {
3613 error = got_error_from_errno("strdup");
3614 goto done;
3619 error = got_repo_open(&repo, repo_path, NULL);
3620 if (error != NULL)
3621 goto done;
3623 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3624 if (error)
3625 goto done;
3627 if (path == NULL) {
3628 if (worktree) {
3629 char *p, *worktree_subdir = cwd +
3630 strlen(got_worktree_get_root_path(worktree));
3631 if (asprintf(&p, "%s/%s",
3632 got_worktree_get_path_prefix(worktree),
3633 worktree_subdir) == -1) {
3634 error = got_error_from_errno("asprintf");
3635 goto done;
3637 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3638 free(p);
3639 if (error)
3640 goto done;
3641 } else
3642 path = "/";
3644 if (in_repo_path == NULL) {
3645 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3646 if (error != NULL)
3647 goto done;
3650 if (commit_id_str == NULL) {
3651 struct got_reference *head_ref;
3652 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3653 if (error != NULL)
3654 goto done;
3655 error = got_ref_resolve(&commit_id, repo, head_ref);
3656 got_ref_close(head_ref);
3657 if (error != NULL)
3658 goto done;
3659 } else {
3660 error = got_repo_match_object_id(&commit_id, NULL,
3661 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3662 if (error)
3663 goto done;
3666 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3667 in_repo_path, repo);
3668 done:
3669 free(in_repo_path);
3670 free(repo_path);
3671 free(cwd);
3672 free(commit_id);
3673 if (worktree)
3674 got_worktree_close(worktree);
3675 if (repo) {
3676 const struct got_error *repo_error;
3677 repo_error = got_repo_close(repo);
3678 if (error == NULL)
3679 error = repo_error;
3681 return error;
3684 __dead static void
3685 usage_status(void)
3687 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3688 exit(1);
3691 static const struct got_error *
3692 print_status(void *arg, unsigned char status, unsigned char staged_status,
3693 const char *path, struct got_object_id *blob_id,
3694 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3695 int dirfd, const char *de_name)
3697 if (status == staged_status && (status == GOT_STATUS_DELETE))
3698 status = GOT_STATUS_NO_CHANGE;
3699 printf("%c%c %s\n", status, staged_status, path);
3700 return NULL;
3703 static const struct got_error *
3704 cmd_status(int argc, char *argv[])
3706 const struct got_error *error = NULL;
3707 struct got_repository *repo = NULL;
3708 struct got_worktree *worktree = NULL;
3709 char *cwd = NULL;
3710 struct got_pathlist_head paths;
3711 struct got_pathlist_entry *pe;
3712 int ch;
3714 TAILQ_INIT(&paths);
3716 while ((ch = getopt(argc, argv, "")) != -1) {
3717 switch (ch) {
3718 default:
3719 usage_status();
3720 /* NOTREACHED */
3724 argc -= optind;
3725 argv += optind;
3727 #ifndef PROFILE
3728 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3729 NULL) == -1)
3730 err(1, "pledge");
3731 #endif
3732 cwd = getcwd(NULL, 0);
3733 if (cwd == NULL) {
3734 error = got_error_from_errno("getcwd");
3735 goto done;
3738 error = got_worktree_open(&worktree, cwd);
3739 if (error != NULL)
3740 goto done;
3742 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3743 NULL);
3744 if (error != NULL)
3745 goto done;
3747 error = apply_unveil(got_repo_get_path(repo), 1,
3748 got_worktree_get_root_path(worktree));
3749 if (error)
3750 goto done;
3752 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3753 if (error)
3754 goto done;
3756 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3757 check_cancelled, NULL);
3758 done:
3759 TAILQ_FOREACH(pe, &paths, entry)
3760 free((char *)pe->path);
3761 got_pathlist_free(&paths);
3762 free(cwd);
3763 return error;
3766 __dead static void
3767 usage_ref(void)
3769 fprintf(stderr,
3770 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3771 getprogname());
3772 exit(1);
3775 static const struct got_error *
3776 list_refs(struct got_repository *repo)
3778 static const struct got_error *err = NULL;
3779 struct got_reflist_head refs;
3780 struct got_reflist_entry *re;
3782 SIMPLEQ_INIT(&refs);
3783 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3784 if (err)
3785 return err;
3787 SIMPLEQ_FOREACH(re, &refs, entry) {
3788 char *refstr;
3789 refstr = got_ref_to_str(re->ref);
3790 if (refstr == NULL)
3791 return got_error_from_errno("got_ref_to_str");
3792 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3793 free(refstr);
3796 got_ref_list_free(&refs);
3797 return NULL;
3800 static const struct got_error *
3801 delete_ref(struct got_repository *repo, const char *refname)
3803 const struct got_error *err = NULL;
3804 struct got_reference *ref;
3806 err = got_ref_open(&ref, repo, refname, 0);
3807 if (err)
3808 return err;
3810 err = got_ref_delete(ref, repo);
3811 got_ref_close(ref);
3812 return err;
3815 static const struct got_error *
3816 add_ref(struct got_repository *repo, const char *refname, const char *target)
3818 const struct got_error *err = NULL;
3819 struct got_object_id *id;
3820 struct got_reference *ref = NULL;
3823 * Don't let the user create a reference name with a leading '-'.
3824 * While technically a valid reference name, this case is usually
3825 * an unintended typo.
3827 if (refname[0] == '-')
3828 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3830 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3831 repo);
3832 if (err) {
3833 struct got_reference *target_ref;
3835 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3836 return err;
3837 err = got_ref_open(&target_ref, repo, target, 0);
3838 if (err)
3839 return err;
3840 err = got_ref_resolve(&id, repo, target_ref);
3841 got_ref_close(target_ref);
3842 if (err)
3843 return err;
3846 err = got_ref_alloc(&ref, refname, id);
3847 if (err)
3848 goto done;
3850 err = got_ref_write(ref, repo);
3851 done:
3852 if (ref)
3853 got_ref_close(ref);
3854 free(id);
3855 return err;
3858 static const struct got_error *
3859 add_symref(struct got_repository *repo, const char *refname, const char *target)
3861 const struct got_error *err = NULL;
3862 struct got_reference *ref = NULL;
3863 struct got_reference *target_ref = NULL;
3866 * Don't let the user create a reference name with a leading '-'.
3867 * While technically a valid reference name, this case is usually
3868 * an unintended typo.
3870 if (refname[0] == '-')
3871 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3873 err = got_ref_open(&target_ref, repo, target, 0);
3874 if (err)
3875 return err;
3877 err = got_ref_alloc_symref(&ref, refname, target_ref);
3878 if (err)
3879 goto done;
3881 err = got_ref_write(ref, repo);
3882 done:
3883 if (target_ref)
3884 got_ref_close(target_ref);
3885 if (ref)
3886 got_ref_close(ref);
3887 return err;
3890 static const struct got_error *
3891 cmd_ref(int argc, char *argv[])
3893 const struct got_error *error = NULL;
3894 struct got_repository *repo = NULL;
3895 struct got_worktree *worktree = NULL;
3896 char *cwd = NULL, *repo_path = NULL;
3897 int ch, do_list = 0, create_symref = 0;
3898 const char *delref = NULL;
3900 /* TODO: Add -s option for adding symbolic references. */
3901 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3902 switch (ch) {
3903 case 'd':
3904 delref = optarg;
3905 break;
3906 case 'r':
3907 repo_path = realpath(optarg, NULL);
3908 if (repo_path == NULL)
3909 return got_error_from_errno2("realpath",
3910 optarg);
3911 got_path_strip_trailing_slashes(repo_path);
3912 break;
3913 case 'l':
3914 do_list = 1;
3915 break;
3916 case 's':
3917 create_symref = 1;
3918 break;
3919 default:
3920 usage_ref();
3921 /* NOTREACHED */
3925 if (do_list && delref)
3926 errx(1, "-l and -d options are mutually exclusive\n");
3928 argc -= optind;
3929 argv += optind;
3931 if (do_list || delref) {
3932 if (create_symref)
3933 errx(1, "-s option cannot be used together with the "
3934 "-l or -d options");
3935 if (argc > 0)
3936 usage_ref();
3937 } else if (argc != 2)
3938 usage_ref();
3940 #ifndef PROFILE
3941 if (do_list) {
3942 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3943 NULL) == -1)
3944 err(1, "pledge");
3945 } else {
3946 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3947 "sendfd unveil", NULL) == -1)
3948 err(1, "pledge");
3950 #endif
3951 cwd = getcwd(NULL, 0);
3952 if (cwd == NULL) {
3953 error = got_error_from_errno("getcwd");
3954 goto done;
3957 if (repo_path == NULL) {
3958 error = got_worktree_open(&worktree, cwd);
3959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3960 goto done;
3961 else
3962 error = NULL;
3963 if (worktree) {
3964 repo_path =
3965 strdup(got_worktree_get_repo_path(worktree));
3966 if (repo_path == NULL)
3967 error = got_error_from_errno("strdup");
3968 if (error)
3969 goto done;
3970 } else {
3971 repo_path = strdup(cwd);
3972 if (repo_path == NULL) {
3973 error = got_error_from_errno("strdup");
3974 goto done;
3979 error = got_repo_open(&repo, repo_path, NULL);
3980 if (error != NULL)
3981 goto done;
3983 error = apply_unveil(got_repo_get_path(repo), do_list,
3984 worktree ? got_worktree_get_root_path(worktree) : NULL);
3985 if (error)
3986 goto done;
3988 if (do_list)
3989 error = list_refs(repo);
3990 else if (delref)
3991 error = delete_ref(repo, delref);
3992 else if (create_symref)
3993 error = add_symref(repo, argv[0], argv[1]);
3994 else
3995 error = add_ref(repo, argv[0], argv[1]);
3996 done:
3997 if (repo)
3998 got_repo_close(repo);
3999 if (worktree)
4000 got_worktree_close(worktree);
4001 free(cwd);
4002 free(repo_path);
4003 return error;
4006 __dead static void
4007 usage_branch(void)
4009 fprintf(stderr,
4010 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4011 "[name]\n", getprogname());
4012 exit(1);
4015 static const struct got_error *
4016 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4017 struct got_reference *ref)
4019 const struct got_error *err = NULL;
4020 const char *refname, *marker = " ";
4021 char *refstr;
4023 refname = got_ref_get_name(ref);
4024 if (worktree && strcmp(refname,
4025 got_worktree_get_head_ref_name(worktree)) == 0) {
4026 struct got_object_id *id = NULL;
4028 err = got_ref_resolve(&id, repo, ref);
4029 if (err)
4030 return err;
4031 if (got_object_id_cmp(id,
4032 got_worktree_get_base_commit_id(worktree)) == 0)
4033 marker = "* ";
4034 else
4035 marker = "~ ";
4036 free(id);
4039 if (strncmp(refname, "refs/heads/", 11) == 0)
4040 refname += 11;
4041 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4042 refname += 18;
4044 refstr = got_ref_to_str(ref);
4045 if (refstr == NULL)
4046 return got_error_from_errno("got_ref_to_str");
4048 printf("%s%s: %s\n", marker, refname, refstr);
4049 free(refstr);
4050 return NULL;
4053 static const struct got_error *
4054 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4056 const char *refname;
4058 if (worktree == NULL)
4059 return got_error(GOT_ERR_NOT_WORKTREE);
4061 refname = got_worktree_get_head_ref_name(worktree);
4063 if (strncmp(refname, "refs/heads/", 11) == 0)
4064 refname += 11;
4065 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4066 refname += 18;
4068 printf("%s\n", refname);
4070 return NULL;
4073 static const struct got_error *
4074 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4076 static const struct got_error *err = NULL;
4077 struct got_reflist_head refs;
4078 struct got_reflist_entry *re;
4079 struct got_reference *temp_ref = NULL;
4080 int rebase_in_progress, histedit_in_progress;
4082 SIMPLEQ_INIT(&refs);
4084 if (worktree) {
4085 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4086 worktree);
4087 if (err)
4088 return err;
4090 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4091 worktree);
4092 if (err)
4093 return err;
4095 if (rebase_in_progress || histedit_in_progress) {
4096 err = got_ref_open(&temp_ref, repo,
4097 got_worktree_get_head_ref_name(worktree), 0);
4098 if (err)
4099 return err;
4100 list_branch(repo, worktree, temp_ref);
4101 got_ref_close(temp_ref);
4105 err = got_ref_list(&refs, repo, "refs/heads",
4106 got_ref_cmp_by_name, NULL);
4107 if (err)
4108 return err;
4110 SIMPLEQ_FOREACH(re, &refs, entry)
4111 list_branch(repo, worktree, re->ref);
4113 got_ref_list_free(&refs);
4114 return NULL;
4117 static const struct got_error *
4118 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4119 const char *branch_name)
4121 const struct got_error *err = NULL;
4122 struct got_reference *ref = NULL;
4123 char *refname;
4125 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4126 return got_error_from_errno("asprintf");
4128 err = got_ref_open(&ref, repo, refname, 0);
4129 if (err)
4130 goto done;
4132 if (worktree &&
4133 strcmp(got_worktree_get_head_ref_name(worktree),
4134 got_ref_get_name(ref)) == 0) {
4135 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4136 "will not delete this work tree's current branch");
4137 goto done;
4140 err = got_ref_delete(ref, repo);
4141 done:
4142 if (ref)
4143 got_ref_close(ref);
4144 free(refname);
4145 return err;
4148 static const struct got_error *
4149 add_branch(struct got_repository *repo, const char *branch_name,
4150 struct got_object_id *base_commit_id)
4152 const struct got_error *err = NULL;
4153 struct got_reference *ref = NULL;
4154 char *base_refname = NULL, *refname = NULL;
4157 * Don't let the user create a branch name with a leading '-'.
4158 * While technically a valid reference name, this case is usually
4159 * an unintended typo.
4161 if (branch_name[0] == '-')
4162 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4164 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4165 err = got_error_from_errno("asprintf");
4166 goto done;
4169 err = got_ref_open(&ref, repo, refname, 0);
4170 if (err == NULL) {
4171 err = got_error(GOT_ERR_BRANCH_EXISTS);
4172 goto done;
4173 } else if (err->code != GOT_ERR_NOT_REF)
4174 goto done;
4176 err = got_ref_alloc(&ref, refname, base_commit_id);
4177 if (err)
4178 goto done;
4180 err = got_ref_write(ref, repo);
4181 done:
4182 if (ref)
4183 got_ref_close(ref);
4184 free(base_refname);
4185 free(refname);
4186 return err;
4189 static const struct got_error *
4190 cmd_branch(int argc, char *argv[])
4192 const struct got_error *error = NULL;
4193 struct got_repository *repo = NULL;
4194 struct got_worktree *worktree = NULL;
4195 char *cwd = NULL, *repo_path = NULL;
4196 int ch, do_list = 0, do_show = 0, do_update = 1;
4197 const char *delref = NULL, *commit_id_arg = NULL;
4198 struct got_reference *ref = NULL;
4199 struct got_pathlist_head paths;
4200 struct got_pathlist_entry *pe;
4201 struct got_object_id *commit_id = NULL;
4202 char *commit_id_str = NULL;
4204 TAILQ_INIT(&paths);
4206 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4207 switch (ch) {
4208 case 'c':
4209 commit_id_arg = optarg;
4210 break;
4211 case 'd':
4212 delref = optarg;
4213 break;
4214 case 'r':
4215 repo_path = realpath(optarg, NULL);
4216 if (repo_path == NULL)
4217 return got_error_from_errno2("realpath",
4218 optarg);
4219 got_path_strip_trailing_slashes(repo_path);
4220 break;
4221 case 'l':
4222 do_list = 1;
4223 break;
4224 case 'n':
4225 do_update = 0;
4226 break;
4227 default:
4228 usage_branch();
4229 /* NOTREACHED */
4233 if (do_list && delref)
4234 errx(1, "-l and -d options are mutually exclusive\n");
4236 argc -= optind;
4237 argv += optind;
4239 if (!do_list && !delref && argc == 0)
4240 do_show = 1;
4242 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4243 errx(1, "-c option can only be used when creating a branch");
4245 if (do_list || delref) {
4246 if (argc > 0)
4247 usage_branch();
4248 } else if (!do_show && argc != 1)
4249 usage_branch();
4251 #ifndef PROFILE
4252 if (do_list || do_show) {
4253 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4254 NULL) == -1)
4255 err(1, "pledge");
4256 } else {
4257 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4258 "sendfd unveil", NULL) == -1)
4259 err(1, "pledge");
4261 #endif
4262 cwd = getcwd(NULL, 0);
4263 if (cwd == NULL) {
4264 error = got_error_from_errno("getcwd");
4265 goto done;
4268 if (repo_path == NULL) {
4269 error = got_worktree_open(&worktree, cwd);
4270 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4271 goto done;
4272 else
4273 error = NULL;
4274 if (worktree) {
4275 repo_path =
4276 strdup(got_worktree_get_repo_path(worktree));
4277 if (repo_path == NULL)
4278 error = got_error_from_errno("strdup");
4279 if (error)
4280 goto done;
4281 } else {
4282 repo_path = strdup(cwd);
4283 if (repo_path == NULL) {
4284 error = got_error_from_errno("strdup");
4285 goto done;
4290 error = got_repo_open(&repo, repo_path, NULL);
4291 if (error != NULL)
4292 goto done;
4294 error = apply_unveil(got_repo_get_path(repo), do_list,
4295 worktree ? got_worktree_get_root_path(worktree) : NULL);
4296 if (error)
4297 goto done;
4299 if (do_show)
4300 error = show_current_branch(repo, worktree);
4301 else if (do_list)
4302 error = list_branches(repo, worktree);
4303 else if (delref)
4304 error = delete_branch(repo, worktree, delref);
4305 else {
4306 if (commit_id_arg == NULL)
4307 commit_id_arg = worktree ?
4308 got_worktree_get_head_ref_name(worktree) :
4309 GOT_REF_HEAD;
4310 error = got_repo_match_object_id(&commit_id, NULL,
4311 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4312 if (error)
4313 goto done;
4314 error = add_branch(repo, argv[0], commit_id);
4315 if (error)
4316 goto done;
4317 if (worktree && do_update) {
4318 int did_something = 0;
4319 char *branch_refname = NULL;
4321 error = got_object_id_str(&commit_id_str, commit_id);
4322 if (error)
4323 goto done;
4324 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4325 worktree);
4326 if (error)
4327 goto done;
4328 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4329 == -1) {
4330 error = got_error_from_errno("asprintf");
4331 goto done;
4333 error = got_ref_open(&ref, repo, branch_refname, 0);
4334 free(branch_refname);
4335 if (error)
4336 goto done;
4337 error = switch_head_ref(ref, commit_id, worktree,
4338 repo);
4339 if (error)
4340 goto done;
4341 error = got_worktree_set_base_commit_id(worktree, repo,
4342 commit_id);
4343 if (error)
4344 goto done;
4345 error = got_worktree_checkout_files(worktree, &paths,
4346 repo, update_progress, &did_something,
4347 check_cancelled, NULL);
4348 if (error)
4349 goto done;
4350 if (did_something)
4351 printf("Updated to commit %s\n", commit_id_str);
4354 done:
4355 if (ref)
4356 got_ref_close(ref);
4357 if (repo)
4358 got_repo_close(repo);
4359 if (worktree)
4360 got_worktree_close(worktree);
4361 free(cwd);
4362 free(repo_path);
4363 free(commit_id);
4364 free(commit_id_str);
4365 TAILQ_FOREACH(pe, &paths, entry)
4366 free((char *)pe->path);
4367 got_pathlist_free(&paths);
4368 return error;
4372 __dead static void
4373 usage_tag(void)
4375 fprintf(stderr,
4376 "usage: %s tag [-c commit] [-r repository] [-l] "
4377 "[-m message] name\n", getprogname());
4378 exit(1);
4381 #if 0
4382 static const struct got_error *
4383 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4385 const struct got_error *err = NULL;
4386 struct got_reflist_entry *re, *se, *new;
4387 struct got_object_id *re_id, *se_id;
4388 struct got_tag_object *re_tag, *se_tag;
4389 time_t re_time, se_time;
4391 SIMPLEQ_FOREACH(re, tags, entry) {
4392 se = SIMPLEQ_FIRST(sorted);
4393 if (se == NULL) {
4394 err = got_reflist_entry_dup(&new, re);
4395 if (err)
4396 return err;
4397 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4398 continue;
4399 } else {
4400 err = got_ref_resolve(&re_id, repo, re->ref);
4401 if (err)
4402 break;
4403 err = got_object_open_as_tag(&re_tag, repo, re_id);
4404 free(re_id);
4405 if (err)
4406 break;
4407 re_time = got_object_tag_get_tagger_time(re_tag);
4408 got_object_tag_close(re_tag);
4411 while (se) {
4412 err = got_ref_resolve(&se_id, repo, re->ref);
4413 if (err)
4414 break;
4415 err = got_object_open_as_tag(&se_tag, repo, se_id);
4416 free(se_id);
4417 if (err)
4418 break;
4419 se_time = got_object_tag_get_tagger_time(se_tag);
4420 got_object_tag_close(se_tag);
4422 if (se_time > re_time) {
4423 err = got_reflist_entry_dup(&new, re);
4424 if (err)
4425 return err;
4426 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4427 break;
4429 se = SIMPLEQ_NEXT(se, entry);
4430 continue;
4433 done:
4434 return err;
4436 #endif
4438 static const struct got_error *
4439 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4441 static const struct got_error *err = NULL;
4442 struct got_reflist_head refs;
4443 struct got_reflist_entry *re;
4445 SIMPLEQ_INIT(&refs);
4447 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4448 if (err)
4449 return err;
4451 SIMPLEQ_FOREACH(re, &refs, entry) {
4452 const char *refname;
4453 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4454 char datebuf[26];
4455 const char *tagger;
4456 time_t tagger_time;
4457 struct got_object_id *id;
4458 struct got_tag_object *tag;
4459 struct got_commit_object *commit = NULL;
4461 refname = got_ref_get_name(re->ref);
4462 if (strncmp(refname, "refs/tags/", 10) != 0)
4463 continue;
4464 refname += 10;
4465 refstr = got_ref_to_str(re->ref);
4466 if (refstr == NULL) {
4467 err = got_error_from_errno("got_ref_to_str");
4468 break;
4470 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4471 free(refstr);
4473 err = got_ref_resolve(&id, repo, re->ref);
4474 if (err)
4475 break;
4476 err = got_object_open_as_tag(&tag, repo, id);
4477 if (err) {
4478 if (err->code != GOT_ERR_OBJ_TYPE) {
4479 free(id);
4480 break;
4482 /* "lightweight" tag */
4483 err = got_object_open_as_commit(&commit, repo, id);
4484 if (err) {
4485 free(id);
4486 break;
4488 tagger = got_object_commit_get_committer(commit);
4489 tagger_time =
4490 got_object_commit_get_committer_time(commit);
4491 err = got_object_id_str(&id_str, id);
4492 free(id);
4493 if (err)
4494 break;
4495 } else {
4496 free(id);
4497 tagger = got_object_tag_get_tagger(tag);
4498 tagger_time = got_object_tag_get_tagger_time(tag);
4499 err = got_object_id_str(&id_str,
4500 got_object_tag_get_object_id(tag));
4501 if (err)
4502 break;
4504 printf("from: %s\n", tagger);
4505 datestr = get_datestr(&tagger_time, datebuf);
4506 if (datestr)
4507 printf("date: %s UTC\n", datestr);
4508 if (commit)
4509 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4510 else {
4511 switch (got_object_tag_get_object_type(tag)) {
4512 case GOT_OBJ_TYPE_BLOB:
4513 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4514 id_str);
4515 break;
4516 case GOT_OBJ_TYPE_TREE:
4517 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4518 id_str);
4519 break;
4520 case GOT_OBJ_TYPE_COMMIT:
4521 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4522 id_str);
4523 break;
4524 case GOT_OBJ_TYPE_TAG:
4525 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4526 id_str);
4527 break;
4528 default:
4529 break;
4532 free(id_str);
4533 if (commit) {
4534 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4535 if (err)
4536 break;
4537 got_object_commit_close(commit);
4538 } else {
4539 tagmsg0 = strdup(got_object_tag_get_message(tag));
4540 got_object_tag_close(tag);
4541 if (tagmsg0 == NULL) {
4542 err = got_error_from_errno("strdup");
4543 break;
4547 tagmsg = tagmsg0;
4548 do {
4549 line = strsep(&tagmsg, "\n");
4550 if (line)
4551 printf(" %s\n", line);
4552 } while (line);
4553 free(tagmsg0);
4556 got_ref_list_free(&refs);
4557 return NULL;
4560 static const struct got_error *
4561 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4562 const char *tag_name, const char *repo_path)
4564 const struct got_error *err = NULL;
4565 char *template = NULL, *initial_content = NULL;
4566 char *editor = NULL;
4567 int fd = -1;
4569 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4570 err = got_error_from_errno("asprintf");
4571 goto done;
4574 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4575 commit_id_str, tag_name) == -1) {
4576 err = got_error_from_errno("asprintf");
4577 goto done;
4580 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4581 if (err)
4582 goto done;
4584 dprintf(fd, initial_content);
4585 close(fd);
4587 err = get_editor(&editor);
4588 if (err)
4589 goto done;
4590 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4591 done:
4592 free(initial_content);
4593 free(template);
4594 free(editor);
4596 /* Editor is done; we can now apply unveil(2) */
4597 if (err == NULL) {
4598 err = apply_unveil(repo_path, 0, NULL);
4599 if (err) {
4600 free(*tagmsg);
4601 *tagmsg = NULL;
4604 return err;
4607 static const struct got_error *
4608 add_tag(struct got_repository *repo, const char *tag_name,
4609 const char *commit_arg, const char *tagmsg_arg)
4611 const struct got_error *err = NULL;
4612 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4613 char *label = NULL, *commit_id_str = NULL;
4614 struct got_reference *ref = NULL;
4615 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4616 char *tagmsg_path = NULL, *tag_id_str = NULL;
4617 int preserve_tagmsg = 0;
4620 * Don't let the user create a tag name with a leading '-'.
4621 * While technically a valid reference name, this case is usually
4622 * an unintended typo.
4624 if (tag_name[0] == '-')
4625 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4627 err = get_author(&tagger, repo);
4628 if (err)
4629 return err;
4631 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4632 GOT_OBJ_TYPE_COMMIT, 1, repo);
4633 if (err)
4634 goto done;
4636 err = got_object_id_str(&commit_id_str, commit_id);
4637 if (err)
4638 goto done;
4640 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4641 refname = strdup(tag_name);
4642 if (refname == NULL) {
4643 err = got_error_from_errno("strdup");
4644 goto done;
4646 tag_name += 10;
4647 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4648 err = got_error_from_errno("asprintf");
4649 goto done;
4652 err = got_ref_open(&ref, repo, refname, 0);
4653 if (err == NULL) {
4654 err = got_error(GOT_ERR_TAG_EXISTS);
4655 goto done;
4656 } else if (err->code != GOT_ERR_NOT_REF)
4657 goto done;
4659 if (tagmsg_arg == NULL) {
4660 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4661 tag_name, got_repo_get_path(repo));
4662 if (err) {
4663 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4664 tagmsg_path != NULL)
4665 preserve_tagmsg = 1;
4666 goto done;
4670 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4671 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4672 if (err) {
4673 if (tagmsg_path)
4674 preserve_tagmsg = 1;
4675 goto done;
4678 err = got_ref_alloc(&ref, refname, tag_id);
4679 if (err) {
4680 if (tagmsg_path)
4681 preserve_tagmsg = 1;
4682 goto done;
4685 err = got_ref_write(ref, repo);
4686 if (err) {
4687 if (tagmsg_path)
4688 preserve_tagmsg = 1;
4689 goto done;
4692 err = got_object_id_str(&tag_id_str, tag_id);
4693 if (err) {
4694 if (tagmsg_path)
4695 preserve_tagmsg = 1;
4696 goto done;
4698 printf("Created tag %s\n", tag_id_str);
4699 done:
4700 if (preserve_tagmsg) {
4701 fprintf(stderr, "%s: tag message preserved in %s\n",
4702 getprogname(), tagmsg_path);
4703 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4704 err = got_error_from_errno2("unlink", tagmsg_path);
4705 free(tag_id_str);
4706 if (ref)
4707 got_ref_close(ref);
4708 free(commit_id);
4709 free(commit_id_str);
4710 free(refname);
4711 free(tagmsg);
4712 free(tagmsg_path);
4713 free(tagger);
4714 return err;
4717 static const struct got_error *
4718 cmd_tag(int argc, char *argv[])
4720 const struct got_error *error = NULL;
4721 struct got_repository *repo = NULL;
4722 struct got_worktree *worktree = NULL;
4723 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4724 char *gitconfig_path = NULL;
4725 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4726 int ch, do_list = 0;
4728 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4729 switch (ch) {
4730 case 'c':
4731 commit_id_arg = optarg;
4732 break;
4733 case 'm':
4734 tagmsg = optarg;
4735 break;
4736 case 'r':
4737 repo_path = realpath(optarg, NULL);
4738 if (repo_path == NULL)
4739 return got_error_from_errno2("realpath",
4740 optarg);
4741 got_path_strip_trailing_slashes(repo_path);
4742 break;
4743 case 'l':
4744 do_list = 1;
4745 break;
4746 default:
4747 usage_tag();
4748 /* NOTREACHED */
4752 argc -= optind;
4753 argv += optind;
4755 if (do_list) {
4756 if (commit_id_arg != NULL)
4757 errx(1, "-c option can only be used when creating a tag");
4758 if (tagmsg)
4759 errx(1, "-l and -m options are mutually exclusive");
4760 if (argc > 0)
4761 usage_tag();
4762 } else if (argc != 1)
4763 usage_tag();
4765 tag_name = argv[0];
4767 #ifndef PROFILE
4768 if (do_list) {
4769 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4770 NULL) == -1)
4771 err(1, "pledge");
4772 } else {
4773 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4774 "sendfd unveil", NULL) == -1)
4775 err(1, "pledge");
4777 #endif
4778 cwd = getcwd(NULL, 0);
4779 if (cwd == NULL) {
4780 error = got_error_from_errno("getcwd");
4781 goto done;
4784 if (repo_path == NULL) {
4785 error = got_worktree_open(&worktree, cwd);
4786 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4787 goto done;
4788 else
4789 error = NULL;
4790 if (worktree) {
4791 repo_path =
4792 strdup(got_worktree_get_repo_path(worktree));
4793 if (repo_path == NULL)
4794 error = got_error_from_errno("strdup");
4795 if (error)
4796 goto done;
4797 } else {
4798 repo_path = strdup(cwd);
4799 if (repo_path == NULL) {
4800 error = got_error_from_errno("strdup");
4801 goto done;
4806 if (do_list) {
4807 error = got_repo_open(&repo, repo_path, NULL);
4808 if (error != NULL)
4809 goto done;
4810 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4811 if (error)
4812 goto done;
4813 error = list_tags(repo, worktree);
4814 } else {
4815 error = get_gitconfig_path(&gitconfig_path);
4816 if (error)
4817 goto done;
4818 error = got_repo_open(&repo, repo_path, gitconfig_path);
4819 if (error != NULL)
4820 goto done;
4822 if (tagmsg) {
4823 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4824 if (error)
4825 goto done;
4828 if (commit_id_arg == NULL) {
4829 struct got_reference *head_ref;
4830 struct got_object_id *commit_id;
4831 error = got_ref_open(&head_ref, repo,
4832 worktree ? got_worktree_get_head_ref_name(worktree)
4833 : GOT_REF_HEAD, 0);
4834 if (error)
4835 goto done;
4836 error = got_ref_resolve(&commit_id, repo, head_ref);
4837 got_ref_close(head_ref);
4838 if (error)
4839 goto done;
4840 error = got_object_id_str(&commit_id_str, commit_id);
4841 free(commit_id);
4842 if (error)
4843 goto done;
4846 error = add_tag(repo, tag_name,
4847 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4849 done:
4850 if (repo)
4851 got_repo_close(repo);
4852 if (worktree)
4853 got_worktree_close(worktree);
4854 free(cwd);
4855 free(repo_path);
4856 free(gitconfig_path);
4857 free(commit_id_str);
4858 return error;
4861 __dead static void
4862 usage_add(void)
4864 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4865 getprogname());
4866 exit(1);
4869 static const struct got_error *
4870 add_progress(void *arg, unsigned char status, const char *path)
4872 while (path[0] == '/')
4873 path++;
4874 printf("%c %s\n", status, path);
4875 return NULL;
4878 static const struct got_error *
4879 cmd_add(int argc, char *argv[])
4881 const struct got_error *error = NULL;
4882 struct got_repository *repo = NULL;
4883 struct got_worktree *worktree = NULL;
4884 char *cwd = NULL;
4885 struct got_pathlist_head paths;
4886 struct got_pathlist_entry *pe;
4887 int ch, can_recurse = 0, no_ignores = 0;
4889 TAILQ_INIT(&paths);
4891 while ((ch = getopt(argc, argv, "IR")) != -1) {
4892 switch (ch) {
4893 case 'I':
4894 no_ignores = 1;
4895 break;
4896 case 'R':
4897 can_recurse = 1;
4898 break;
4899 default:
4900 usage_add();
4901 /* NOTREACHED */
4905 argc -= optind;
4906 argv += optind;
4908 #ifndef PROFILE
4909 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4910 NULL) == -1)
4911 err(1, "pledge");
4912 #endif
4913 if (argc < 1)
4914 usage_add();
4916 cwd = getcwd(NULL, 0);
4917 if (cwd == NULL) {
4918 error = got_error_from_errno("getcwd");
4919 goto done;
4922 error = got_worktree_open(&worktree, cwd);
4923 if (error)
4924 goto done;
4926 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4927 NULL);
4928 if (error != NULL)
4929 goto done;
4931 error = apply_unveil(got_repo_get_path(repo), 1,
4932 got_worktree_get_root_path(worktree));
4933 if (error)
4934 goto done;
4936 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4937 if (error)
4938 goto done;
4940 if (!can_recurse && no_ignores) {
4941 error = got_error_msg(GOT_ERR_BAD_PATH,
4942 "disregarding ignores requires -R option");
4943 goto done;
4947 if (!can_recurse) {
4948 char *ondisk_path;
4949 struct stat sb;
4950 TAILQ_FOREACH(pe, &paths, entry) {
4951 if (asprintf(&ondisk_path, "%s/%s",
4952 got_worktree_get_root_path(worktree),
4953 pe->path) == -1) {
4954 error = got_error_from_errno("asprintf");
4955 goto done;
4957 if (lstat(ondisk_path, &sb) == -1) {
4958 if (errno == ENOENT) {
4959 free(ondisk_path);
4960 continue;
4962 error = got_error_from_errno2("lstat",
4963 ondisk_path);
4964 free(ondisk_path);
4965 goto done;
4967 free(ondisk_path);
4968 if (S_ISDIR(sb.st_mode)) {
4969 error = got_error_msg(GOT_ERR_BAD_PATH,
4970 "adding directories requires -R option");
4971 goto done;
4976 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4977 NULL, repo, no_ignores);
4978 done:
4979 if (repo)
4980 got_repo_close(repo);
4981 if (worktree)
4982 got_worktree_close(worktree);
4983 TAILQ_FOREACH(pe, &paths, entry)
4984 free((char *)pe->path);
4985 got_pathlist_free(&paths);
4986 free(cwd);
4987 return error;
4990 __dead static void
4991 usage_remove(void)
4993 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4994 getprogname());
4995 exit(1);
4998 static const struct got_error *
4999 print_remove_status(void *arg, unsigned char status,
5000 unsigned char staged_status, const char *path)
5002 while (path[0] == '/')
5003 path++;
5004 if (status == GOT_STATUS_NONEXISTENT)
5005 return NULL;
5006 if (status == staged_status && (status == GOT_STATUS_DELETE))
5007 status = GOT_STATUS_NO_CHANGE;
5008 printf("%c%c %s\n", status, staged_status, path);
5009 return NULL;
5012 static const struct got_error *
5013 cmd_remove(int argc, char *argv[])
5015 const struct got_error *error = NULL;
5016 struct got_worktree *worktree = NULL;
5017 struct got_repository *repo = NULL;
5018 char *cwd = NULL;
5019 struct got_pathlist_head paths;
5020 struct got_pathlist_entry *pe;
5021 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5023 TAILQ_INIT(&paths);
5025 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5026 switch (ch) {
5027 case 'f':
5028 delete_local_mods = 1;
5029 break;
5030 case 'k':
5031 keep_on_disk = 1;
5032 break;
5033 case 'R':
5034 can_recurse = 1;
5035 break;
5036 default:
5037 usage_remove();
5038 /* NOTREACHED */
5042 argc -= optind;
5043 argv += optind;
5045 #ifndef PROFILE
5046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5047 NULL) == -1)
5048 err(1, "pledge");
5049 #endif
5050 if (argc < 1)
5051 usage_remove();
5053 cwd = getcwd(NULL, 0);
5054 if (cwd == NULL) {
5055 error = got_error_from_errno("getcwd");
5056 goto done;
5058 error = got_worktree_open(&worktree, cwd);
5059 if (error)
5060 goto done;
5062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5063 NULL);
5064 if (error)
5065 goto done;
5067 error = apply_unveil(got_repo_get_path(repo), 1,
5068 got_worktree_get_root_path(worktree));
5069 if (error)
5070 goto done;
5072 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5073 if (error)
5074 goto done;
5076 if (!can_recurse) {
5077 char *ondisk_path;
5078 struct stat sb;
5079 TAILQ_FOREACH(pe, &paths, entry) {
5080 if (asprintf(&ondisk_path, "%s/%s",
5081 got_worktree_get_root_path(worktree),
5082 pe->path) == -1) {
5083 error = got_error_from_errno("asprintf");
5084 goto done;
5086 if (lstat(ondisk_path, &sb) == -1) {
5087 if (errno == ENOENT) {
5088 free(ondisk_path);
5089 continue;
5091 error = got_error_from_errno2("lstat",
5092 ondisk_path);
5093 free(ondisk_path);
5094 goto done;
5096 free(ondisk_path);
5097 if (S_ISDIR(sb.st_mode)) {
5098 error = got_error_msg(GOT_ERR_BAD_PATH,
5099 "removing directories requires -R option");
5100 goto done;
5105 error = got_worktree_schedule_delete(worktree, &paths,
5106 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5107 done:
5108 if (repo)
5109 got_repo_close(repo);
5110 if (worktree)
5111 got_worktree_close(worktree);
5112 TAILQ_FOREACH(pe, &paths, entry)
5113 free((char *)pe->path);
5114 got_pathlist_free(&paths);
5115 free(cwd);
5116 return error;
5119 __dead static void
5120 usage_revert(void)
5122 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5123 "path ...\n", getprogname());
5124 exit(1);
5127 static const struct got_error *
5128 revert_progress(void *arg, unsigned char status, const char *path)
5130 if (status == GOT_STATUS_UNVERSIONED)
5131 return NULL;
5133 while (path[0] == '/')
5134 path++;
5135 printf("%c %s\n", status, path);
5136 return NULL;
5139 struct choose_patch_arg {
5140 FILE *patch_script_file;
5141 const char *action;
5144 static const struct got_error *
5145 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5146 int nchanges, const char *action)
5148 char *line = NULL;
5149 size_t linesize = 0;
5150 ssize_t linelen;
5152 switch (status) {
5153 case GOT_STATUS_ADD:
5154 printf("A %s\n%s this addition? [y/n] ", path, action);
5155 break;
5156 case GOT_STATUS_DELETE:
5157 printf("D %s\n%s this deletion? [y/n] ", path, action);
5158 break;
5159 case GOT_STATUS_MODIFY:
5160 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5161 return got_error_from_errno("fseek");
5162 printf(GOT_COMMIT_SEP_STR);
5163 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5164 printf("%s", line);
5165 if (ferror(patch_file))
5166 return got_error_from_errno("getline");
5167 printf(GOT_COMMIT_SEP_STR);
5168 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5169 path, n, nchanges, action);
5170 break;
5171 default:
5172 return got_error_path(path, GOT_ERR_FILE_STATUS);
5175 return NULL;
5178 static const struct got_error *
5179 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5180 FILE *patch_file, int n, int nchanges)
5182 const struct got_error *err = NULL;
5183 char *line = NULL;
5184 size_t linesize = 0;
5185 ssize_t linelen;
5186 int resp = ' ';
5187 struct choose_patch_arg *a = arg;
5189 *choice = GOT_PATCH_CHOICE_NONE;
5191 if (a->patch_script_file) {
5192 char *nl;
5193 err = show_change(status, path, patch_file, n, nchanges,
5194 a->action);
5195 if (err)
5196 return err;
5197 linelen = getline(&line, &linesize, a->patch_script_file);
5198 if (linelen == -1) {
5199 if (ferror(a->patch_script_file))
5200 return got_error_from_errno("getline");
5201 return NULL;
5203 nl = strchr(line, '\n');
5204 if (nl)
5205 *nl = '\0';
5206 if (strcmp(line, "y") == 0) {
5207 *choice = GOT_PATCH_CHOICE_YES;
5208 printf("y\n");
5209 } else if (strcmp(line, "n") == 0) {
5210 *choice = GOT_PATCH_CHOICE_NO;
5211 printf("n\n");
5212 } else if (strcmp(line, "q") == 0 &&
5213 status == GOT_STATUS_MODIFY) {
5214 *choice = GOT_PATCH_CHOICE_QUIT;
5215 printf("q\n");
5216 } else
5217 printf("invalid response '%s'\n", line);
5218 free(line);
5219 return NULL;
5222 while (resp != 'y' && resp != 'n' && resp != 'q') {
5223 err = show_change(status, path, patch_file, n, nchanges,
5224 a->action);
5225 if (err)
5226 return err;
5227 resp = getchar();
5228 if (resp == '\n')
5229 resp = getchar();
5230 if (status == GOT_STATUS_MODIFY) {
5231 if (resp != 'y' && resp != 'n' && resp != 'q') {
5232 printf("invalid response '%c'\n", resp);
5233 resp = ' ';
5235 } else if (resp != 'y' && resp != 'n') {
5236 printf("invalid response '%c'\n", resp);
5237 resp = ' ';
5241 if (resp == 'y')
5242 *choice = GOT_PATCH_CHOICE_YES;
5243 else if (resp == 'n')
5244 *choice = GOT_PATCH_CHOICE_NO;
5245 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5246 *choice = GOT_PATCH_CHOICE_QUIT;
5248 return NULL;
5252 static const struct got_error *
5253 cmd_revert(int argc, char *argv[])
5255 const struct got_error *error = NULL;
5256 struct got_worktree *worktree = NULL;
5257 struct got_repository *repo = NULL;
5258 char *cwd = NULL, *path = NULL;
5259 struct got_pathlist_head paths;
5260 struct got_pathlist_entry *pe;
5261 int ch, can_recurse = 0, pflag = 0;
5262 FILE *patch_script_file = NULL;
5263 const char *patch_script_path = NULL;
5264 struct choose_patch_arg cpa;
5266 TAILQ_INIT(&paths);
5268 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5269 switch (ch) {
5270 case 'p':
5271 pflag = 1;
5272 break;
5273 case 'F':
5274 patch_script_path = optarg;
5275 break;
5276 case 'R':
5277 can_recurse = 1;
5278 break;
5279 default:
5280 usage_revert();
5281 /* NOTREACHED */
5285 argc -= optind;
5286 argv += optind;
5288 #ifndef PROFILE
5289 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5290 "unveil", NULL) == -1)
5291 err(1, "pledge");
5292 #endif
5293 if (argc < 1)
5294 usage_revert();
5295 if (patch_script_path && !pflag)
5296 errx(1, "-F option can only be used together with -p option");
5298 cwd = getcwd(NULL, 0);
5299 if (cwd == NULL) {
5300 error = got_error_from_errno("getcwd");
5301 goto done;
5303 error = got_worktree_open(&worktree, cwd);
5304 if (error)
5305 goto done;
5307 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5308 NULL);
5309 if (error != NULL)
5310 goto done;
5312 if (patch_script_path) {
5313 patch_script_file = fopen(patch_script_path, "r");
5314 if (patch_script_file == NULL) {
5315 error = got_error_from_errno2("fopen",
5316 patch_script_path);
5317 goto done;
5320 error = apply_unveil(got_repo_get_path(repo), 1,
5321 got_worktree_get_root_path(worktree));
5322 if (error)
5323 goto done;
5325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5326 if (error)
5327 goto done;
5329 if (!can_recurse) {
5330 char *ondisk_path;
5331 struct stat sb;
5332 TAILQ_FOREACH(pe, &paths, entry) {
5333 if (asprintf(&ondisk_path, "%s/%s",
5334 got_worktree_get_root_path(worktree),
5335 pe->path) == -1) {
5336 error = got_error_from_errno("asprintf");
5337 goto done;
5339 if (lstat(ondisk_path, &sb) == -1) {
5340 if (errno == ENOENT) {
5341 free(ondisk_path);
5342 continue;
5344 error = got_error_from_errno2("lstat",
5345 ondisk_path);
5346 free(ondisk_path);
5347 goto done;
5349 free(ondisk_path);
5350 if (S_ISDIR(sb.st_mode)) {
5351 error = got_error_msg(GOT_ERR_BAD_PATH,
5352 "reverting directories requires -R option");
5353 goto done;
5358 cpa.patch_script_file = patch_script_file;
5359 cpa.action = "revert";
5360 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5361 pflag ? choose_patch : NULL, &cpa, repo);
5362 done:
5363 if (patch_script_file && fclose(patch_script_file) == EOF &&
5364 error == NULL)
5365 error = got_error_from_errno2("fclose", patch_script_path);
5366 if (repo)
5367 got_repo_close(repo);
5368 if (worktree)
5369 got_worktree_close(worktree);
5370 free(path);
5371 free(cwd);
5372 return error;
5375 __dead static void
5376 usage_commit(void)
5378 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5379 getprogname());
5380 exit(1);
5383 struct collect_commit_logmsg_arg {
5384 const char *cmdline_log;
5385 const char *editor;
5386 const char *worktree_path;
5387 const char *branch_name;
5388 const char *repo_path;
5389 char *logmsg_path;
5393 static const struct got_error *
5394 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5395 void *arg)
5397 char *initial_content = NULL;
5398 struct got_pathlist_entry *pe;
5399 const struct got_error *err = NULL;
5400 char *template = NULL;
5401 struct collect_commit_logmsg_arg *a = arg;
5402 int fd;
5403 size_t len;
5405 /* if a message was specified on the command line, just use it */
5406 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5407 len = strlen(a->cmdline_log) + 1;
5408 *logmsg = malloc(len + 1);
5409 if (*logmsg == NULL)
5410 return got_error_from_errno("malloc");
5411 strlcpy(*logmsg, a->cmdline_log, len);
5412 return NULL;
5415 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5416 return got_error_from_errno("asprintf");
5418 if (asprintf(&initial_content,
5419 "\n# changes to be committed on branch %s:\n",
5420 a->branch_name) == -1)
5421 return got_error_from_errno("asprintf");
5423 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5424 if (err)
5425 goto done;
5427 dprintf(fd, initial_content);
5429 TAILQ_FOREACH(pe, commitable_paths, entry) {
5430 struct got_commitable *ct = pe->data;
5431 dprintf(fd, "# %c %s\n",
5432 got_commitable_get_status(ct),
5433 got_commitable_get_path(ct));
5435 close(fd);
5437 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5438 done:
5439 free(initial_content);
5440 free(template);
5442 /* Editor is done; we can now apply unveil(2) */
5443 if (err == NULL) {
5444 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5445 if (err) {
5446 free(*logmsg);
5447 *logmsg = NULL;
5450 return err;
5453 static const struct got_error *
5454 cmd_commit(int argc, char *argv[])
5456 const struct got_error *error = NULL;
5457 struct got_worktree *worktree = NULL;
5458 struct got_repository *repo = NULL;
5459 char *cwd = NULL, *id_str = NULL;
5460 struct got_object_id *id = NULL;
5461 const char *logmsg = NULL;
5462 struct collect_commit_logmsg_arg cl_arg;
5463 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5464 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5465 struct got_pathlist_head paths;
5467 TAILQ_INIT(&paths);
5468 cl_arg.logmsg_path = NULL;
5470 while ((ch = getopt(argc, argv, "m:")) != -1) {
5471 switch (ch) {
5472 case 'm':
5473 logmsg = optarg;
5474 break;
5475 default:
5476 usage_commit();
5477 /* NOTREACHED */
5481 argc -= optind;
5482 argv += optind;
5484 #ifndef PROFILE
5485 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5486 "unveil", NULL) == -1)
5487 err(1, "pledge");
5488 #endif
5489 cwd = getcwd(NULL, 0);
5490 if (cwd == NULL) {
5491 error = got_error_from_errno("getcwd");
5492 goto done;
5494 error = got_worktree_open(&worktree, cwd);
5495 if (error)
5496 goto done;
5498 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5499 if (error)
5500 goto done;
5501 if (rebase_in_progress) {
5502 error = got_error(GOT_ERR_REBASING);
5503 goto done;
5506 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5507 worktree);
5508 if (error)
5509 goto done;
5511 error = get_gitconfig_path(&gitconfig_path);
5512 if (error)
5513 goto done;
5514 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5515 gitconfig_path);
5516 if (error != NULL)
5517 goto done;
5519 error = get_author(&author, repo);
5520 if (error)
5521 return error;
5524 * unveil(2) traverses exec(2); if an editor is used we have
5525 * to apply unveil after the log message has been written.
5527 if (logmsg == NULL || strlen(logmsg) == 0)
5528 error = get_editor(&editor);
5529 else
5530 error = apply_unveil(got_repo_get_path(repo), 0,
5531 got_worktree_get_root_path(worktree));
5532 if (error)
5533 goto done;
5535 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5536 if (error)
5537 goto done;
5539 cl_arg.editor = editor;
5540 cl_arg.cmdline_log = logmsg;
5541 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5542 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5543 if (!histedit_in_progress) {
5544 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5545 error = got_error(GOT_ERR_COMMIT_BRANCH);
5546 goto done;
5548 cl_arg.branch_name += 11;
5550 cl_arg.repo_path = got_repo_get_path(repo);
5551 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5552 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5553 if (error) {
5554 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5555 cl_arg.logmsg_path != NULL)
5556 preserve_logmsg = 1;
5557 goto done;
5560 error = got_object_id_str(&id_str, id);
5561 if (error)
5562 goto done;
5563 printf("Created commit %s\n", id_str);
5564 done:
5565 if (preserve_logmsg) {
5566 fprintf(stderr, "%s: log message preserved in %s\n",
5567 getprogname(), cl_arg.logmsg_path);
5568 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5569 error == NULL)
5570 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5571 free(cl_arg.logmsg_path);
5572 if (repo)
5573 got_repo_close(repo);
5574 if (worktree)
5575 got_worktree_close(worktree);
5576 free(cwd);
5577 free(id_str);
5578 free(gitconfig_path);
5579 free(editor);
5580 free(author);
5581 return error;
5584 __dead static void
5585 usage_cherrypick(void)
5587 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5588 exit(1);
5591 static const struct got_error *
5592 cmd_cherrypick(int argc, char *argv[])
5594 const struct got_error *error = NULL;
5595 struct got_worktree *worktree = NULL;
5596 struct got_repository *repo = NULL;
5597 char *cwd = NULL, *commit_id_str = NULL;
5598 struct got_object_id *commit_id = NULL;
5599 struct got_commit_object *commit = NULL;
5600 struct got_object_qid *pid;
5601 struct got_reference *head_ref = NULL;
5602 int ch, did_something = 0;
5604 while ((ch = getopt(argc, argv, "")) != -1) {
5605 switch (ch) {
5606 default:
5607 usage_cherrypick();
5608 /* NOTREACHED */
5612 argc -= optind;
5613 argv += optind;
5615 #ifndef PROFILE
5616 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5617 "unveil", NULL) == -1)
5618 err(1, "pledge");
5619 #endif
5620 if (argc != 1)
5621 usage_cherrypick();
5623 cwd = getcwd(NULL, 0);
5624 if (cwd == NULL) {
5625 error = got_error_from_errno("getcwd");
5626 goto done;
5628 error = got_worktree_open(&worktree, cwd);
5629 if (error)
5630 goto done;
5632 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5633 NULL);
5634 if (error != NULL)
5635 goto done;
5637 error = apply_unveil(got_repo_get_path(repo), 0,
5638 got_worktree_get_root_path(worktree));
5639 if (error)
5640 goto done;
5642 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5643 GOT_OBJ_TYPE_COMMIT, repo);
5644 if (error != NULL) {
5645 struct got_reference *ref;
5646 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5647 goto done;
5648 error = got_ref_open(&ref, repo, argv[0], 0);
5649 if (error != NULL)
5650 goto done;
5651 error = got_ref_resolve(&commit_id, repo, ref);
5652 got_ref_close(ref);
5653 if (error != NULL)
5654 goto done;
5656 error = got_object_id_str(&commit_id_str, commit_id);
5657 if (error)
5658 goto done;
5660 error = got_ref_open(&head_ref, repo,
5661 got_worktree_get_head_ref_name(worktree), 0);
5662 if (error != NULL)
5663 goto done;
5665 error = check_same_branch(commit_id, head_ref, NULL, repo);
5666 if (error) {
5667 if (error->code != GOT_ERR_ANCESTRY)
5668 goto done;
5669 error = NULL;
5670 } else {
5671 error = got_error(GOT_ERR_SAME_BRANCH);
5672 goto done;
5675 error = got_object_open_as_commit(&commit, repo, commit_id);
5676 if (error)
5677 goto done;
5678 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5679 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5680 commit_id, repo, update_progress, &did_something, check_cancelled,
5681 NULL);
5682 if (error != NULL)
5683 goto done;
5685 if (did_something)
5686 printf("Merged commit %s\n", commit_id_str);
5687 done:
5688 if (commit)
5689 got_object_commit_close(commit);
5690 free(commit_id_str);
5691 if (head_ref)
5692 got_ref_close(head_ref);
5693 if (worktree)
5694 got_worktree_close(worktree);
5695 if (repo)
5696 got_repo_close(repo);
5697 return error;
5700 __dead static void
5701 usage_backout(void)
5703 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5704 exit(1);
5707 static const struct got_error *
5708 cmd_backout(int argc, char *argv[])
5710 const struct got_error *error = NULL;
5711 struct got_worktree *worktree = NULL;
5712 struct got_repository *repo = NULL;
5713 char *cwd = NULL, *commit_id_str = NULL;
5714 struct got_object_id *commit_id = NULL;
5715 struct got_commit_object *commit = NULL;
5716 struct got_object_qid *pid;
5717 struct got_reference *head_ref = NULL;
5718 int ch, did_something = 0;
5720 while ((ch = getopt(argc, argv, "")) != -1) {
5721 switch (ch) {
5722 default:
5723 usage_backout();
5724 /* NOTREACHED */
5728 argc -= optind;
5729 argv += optind;
5731 #ifndef PROFILE
5732 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5733 "unveil", NULL) == -1)
5734 err(1, "pledge");
5735 #endif
5736 if (argc != 1)
5737 usage_backout();
5739 cwd = getcwd(NULL, 0);
5740 if (cwd == NULL) {
5741 error = got_error_from_errno("getcwd");
5742 goto done;
5744 error = got_worktree_open(&worktree, cwd);
5745 if (error)
5746 goto done;
5748 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5749 NULL);
5750 if (error != NULL)
5751 goto done;
5753 error = apply_unveil(got_repo_get_path(repo), 0,
5754 got_worktree_get_root_path(worktree));
5755 if (error)
5756 goto done;
5758 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5759 GOT_OBJ_TYPE_COMMIT, repo);
5760 if (error != NULL) {
5761 struct got_reference *ref;
5762 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5763 goto done;
5764 error = got_ref_open(&ref, repo, argv[0], 0);
5765 if (error != NULL)
5766 goto done;
5767 error = got_ref_resolve(&commit_id, repo, ref);
5768 got_ref_close(ref);
5769 if (error != NULL)
5770 goto done;
5772 error = got_object_id_str(&commit_id_str, commit_id);
5773 if (error)
5774 goto done;
5776 error = got_ref_open(&head_ref, repo,
5777 got_worktree_get_head_ref_name(worktree), 0);
5778 if (error != NULL)
5779 goto done;
5781 error = check_same_branch(commit_id, head_ref, NULL, repo);
5782 if (error)
5783 goto done;
5785 error = got_object_open_as_commit(&commit, repo, commit_id);
5786 if (error)
5787 goto done;
5788 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5789 if (pid == NULL) {
5790 error = got_error(GOT_ERR_ROOT_COMMIT);
5791 goto done;
5794 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5795 update_progress, &did_something, check_cancelled, NULL);
5796 if (error != NULL)
5797 goto done;
5799 if (did_something)
5800 printf("Backed out commit %s\n", commit_id_str);
5801 done:
5802 if (commit)
5803 got_object_commit_close(commit);
5804 free(commit_id_str);
5805 if (head_ref)
5806 got_ref_close(head_ref);
5807 if (worktree)
5808 got_worktree_close(worktree);
5809 if (repo)
5810 got_repo_close(repo);
5811 return error;
5814 __dead static void
5815 usage_rebase(void)
5817 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5818 getprogname());
5819 exit(1);
5822 void
5823 trim_logmsg(char *logmsg, int limit)
5825 char *nl;
5826 size_t len;
5828 len = strlen(logmsg);
5829 if (len > limit)
5830 len = limit;
5831 logmsg[len] = '\0';
5832 nl = strchr(logmsg, '\n');
5833 if (nl)
5834 *nl = '\0';
5837 static const struct got_error *
5838 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5840 const struct got_error *err;
5841 char *logmsg0 = NULL;
5842 const char *s;
5844 err = got_object_commit_get_logmsg(&logmsg0, commit);
5845 if (err)
5846 return err;
5848 s = logmsg0;
5849 while (isspace((unsigned char)s[0]))
5850 s++;
5852 *logmsg = strdup(s);
5853 if (*logmsg == NULL) {
5854 err = got_error_from_errno("strdup");
5855 goto done;
5858 trim_logmsg(*logmsg, limit);
5859 done:
5860 free(logmsg0);
5861 return err;
5864 static const struct got_error *
5865 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5867 const struct got_error *err;
5868 struct got_commit_object *commit = NULL;
5869 char *id_str = NULL, *logmsg = NULL;
5871 err = got_object_open_as_commit(&commit, repo, id);
5872 if (err)
5873 return err;
5875 err = got_object_id_str(&id_str, id);
5876 if (err)
5877 goto done;
5879 id_str[12] = '\0';
5881 err = get_short_logmsg(&logmsg, 42, commit);
5882 if (err)
5883 goto done;
5885 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5886 done:
5887 free(id_str);
5888 got_object_commit_close(commit);
5889 free(logmsg);
5890 return err;
5893 static const struct got_error *
5894 show_rebase_progress(struct got_commit_object *commit,
5895 struct got_object_id *old_id, struct got_object_id *new_id)
5897 const struct got_error *err;
5898 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5900 err = got_object_id_str(&old_id_str, old_id);
5901 if (err)
5902 goto done;
5904 if (new_id) {
5905 err = got_object_id_str(&new_id_str, new_id);
5906 if (err)
5907 goto done;
5910 old_id_str[12] = '\0';
5911 if (new_id_str)
5912 new_id_str[12] = '\0';
5914 err = get_short_logmsg(&logmsg, 42, commit);
5915 if (err)
5916 goto done;
5918 printf("%s -> %s: %s\n", old_id_str,
5919 new_id_str ? new_id_str : "no-op change", logmsg);
5920 done:
5921 free(old_id_str);
5922 free(new_id_str);
5923 free(logmsg);
5924 return err;
5927 static const struct got_error *
5928 rebase_progress(void *arg, unsigned char status, const char *path)
5930 unsigned char *rebase_status = arg;
5932 while (path[0] == '/')
5933 path++;
5934 printf("%c %s\n", status, path);
5936 if (*rebase_status == GOT_STATUS_CONFLICT)
5937 return NULL;
5938 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5939 *rebase_status = status;
5940 return NULL;
5943 static const struct got_error *
5944 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5945 struct got_reference *branch, struct got_reference *new_base_branch,
5946 struct got_reference *tmp_branch, struct got_repository *repo)
5948 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5949 return got_worktree_rebase_complete(worktree, fileindex,
5950 new_base_branch, tmp_branch, branch, repo);
5953 static const struct got_error *
5954 rebase_commit(struct got_pathlist_head *merged_paths,
5955 struct got_worktree *worktree, struct got_fileindex *fileindex,
5956 struct got_reference *tmp_branch,
5957 struct got_object_id *commit_id, struct got_repository *repo)
5959 const struct got_error *error;
5960 struct got_commit_object *commit;
5961 struct got_object_id *new_commit_id;
5963 error = got_object_open_as_commit(&commit, repo, commit_id);
5964 if (error)
5965 return error;
5967 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5968 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5969 if (error) {
5970 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5971 goto done;
5972 error = show_rebase_progress(commit, commit_id, NULL);
5973 } else {
5974 error = show_rebase_progress(commit, commit_id, new_commit_id);
5975 free(new_commit_id);
5977 done:
5978 got_object_commit_close(commit);
5979 return error;
5982 struct check_path_prefix_arg {
5983 const char *path_prefix;
5984 size_t len;
5985 int errcode;
5988 static const struct got_error *
5989 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5990 struct got_blob_object *blob2, struct got_object_id *id1,
5991 struct got_object_id *id2, const char *path1, const char *path2,
5992 mode_t mode1, mode_t mode2, struct got_repository *repo)
5994 struct check_path_prefix_arg *a = arg;
5996 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5997 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5998 return got_error(a->errcode);
6000 return NULL;
6003 static const struct got_error *
6004 check_path_prefix(struct got_object_id *parent_id,
6005 struct got_object_id *commit_id, const char *path_prefix,
6006 int errcode, struct got_repository *repo)
6008 const struct got_error *err;
6009 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6010 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6011 struct check_path_prefix_arg cpp_arg;
6013 if (got_path_is_root_dir(path_prefix))
6014 return NULL;
6016 err = got_object_open_as_commit(&commit, repo, commit_id);
6017 if (err)
6018 goto done;
6020 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6021 if (err)
6022 goto done;
6024 err = got_object_open_as_tree(&tree1, repo,
6025 got_object_commit_get_tree_id(parent_commit));
6026 if (err)
6027 goto done;
6029 err = got_object_open_as_tree(&tree2, repo,
6030 got_object_commit_get_tree_id(commit));
6031 if (err)
6032 goto done;
6034 cpp_arg.path_prefix = path_prefix;
6035 while (cpp_arg.path_prefix[0] == '/')
6036 cpp_arg.path_prefix++;
6037 cpp_arg.len = strlen(cpp_arg.path_prefix);
6038 cpp_arg.errcode = errcode;
6039 err = got_diff_tree(tree1, tree2, "", "", repo,
6040 check_path_prefix_in_diff, &cpp_arg, 0);
6041 done:
6042 if (tree1)
6043 got_object_tree_close(tree1);
6044 if (tree2)
6045 got_object_tree_close(tree2);
6046 if (commit)
6047 got_object_commit_close(commit);
6048 if (parent_commit)
6049 got_object_commit_close(parent_commit);
6050 return err;
6053 static const struct got_error *
6054 collect_commits(struct got_object_id_queue *commits,
6055 struct got_object_id *initial_commit_id,
6056 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6057 const char *path_prefix, int path_prefix_errcode,
6058 struct got_repository *repo)
6060 const struct got_error *err = NULL;
6061 struct got_commit_graph *graph = NULL;
6062 struct got_object_id *parent_id = NULL;
6063 struct got_object_qid *qid;
6064 struct got_object_id *commit_id = initial_commit_id;
6066 err = got_commit_graph_open(&graph, "/", 1);
6067 if (err)
6068 return err;
6070 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6071 check_cancelled, NULL);
6072 if (err)
6073 goto done;
6074 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6075 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6076 check_cancelled, NULL);
6077 if (err) {
6078 if (err->code == GOT_ERR_ITER_COMPLETED) {
6079 err = got_error_msg(GOT_ERR_ANCESTRY,
6080 "ran out of commits to rebase before "
6081 "youngest common ancestor commit has "
6082 "been reached?!?");
6084 goto done;
6085 } else {
6086 err = check_path_prefix(parent_id, commit_id,
6087 path_prefix, path_prefix_errcode, repo);
6088 if (err)
6089 goto done;
6091 err = got_object_qid_alloc(&qid, commit_id);
6092 if (err)
6093 goto done;
6094 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6095 commit_id = parent_id;
6098 done:
6099 got_commit_graph_close(graph);
6100 return err;
6103 static const struct got_error *
6104 cmd_rebase(int argc, char *argv[])
6106 const struct got_error *error = NULL;
6107 struct got_worktree *worktree = NULL;
6108 struct got_repository *repo = NULL;
6109 struct got_fileindex *fileindex = NULL;
6110 char *cwd = NULL;
6111 struct got_reference *branch = NULL;
6112 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6113 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6114 struct got_object_id *resume_commit_id = NULL;
6115 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6116 struct got_commit_object *commit = NULL;
6117 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6118 int histedit_in_progress = 0;
6119 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6120 struct got_object_id_queue commits;
6121 struct got_pathlist_head merged_paths;
6122 const struct got_object_id_queue *parent_ids;
6123 struct got_object_qid *qid, *pid;
6125 SIMPLEQ_INIT(&commits);
6126 TAILQ_INIT(&merged_paths);
6128 while ((ch = getopt(argc, argv, "ac")) != -1) {
6129 switch (ch) {
6130 case 'a':
6131 abort_rebase = 1;
6132 break;
6133 case 'c':
6134 continue_rebase = 1;
6135 break;
6136 default:
6137 usage_rebase();
6138 /* NOTREACHED */
6142 argc -= optind;
6143 argv += optind;
6145 #ifndef PROFILE
6146 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6147 "unveil", NULL) == -1)
6148 err(1, "pledge");
6149 #endif
6150 if (abort_rebase && continue_rebase)
6151 usage_rebase();
6152 else if (abort_rebase || continue_rebase) {
6153 if (argc != 0)
6154 usage_rebase();
6155 } else if (argc != 1)
6156 usage_rebase();
6158 cwd = getcwd(NULL, 0);
6159 if (cwd == NULL) {
6160 error = got_error_from_errno("getcwd");
6161 goto done;
6163 error = got_worktree_open(&worktree, cwd);
6164 if (error)
6165 goto done;
6167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6168 NULL);
6169 if (error != NULL)
6170 goto done;
6172 error = apply_unveil(got_repo_get_path(repo), 0,
6173 got_worktree_get_root_path(worktree));
6174 if (error)
6175 goto done;
6177 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6178 worktree);
6179 if (error)
6180 goto done;
6181 if (histedit_in_progress) {
6182 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6183 goto done;
6186 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6187 if (error)
6188 goto done;
6190 if (abort_rebase) {
6191 int did_something;
6192 if (!rebase_in_progress) {
6193 error = got_error(GOT_ERR_NOT_REBASING);
6194 goto done;
6196 error = got_worktree_rebase_continue(&resume_commit_id,
6197 &new_base_branch, &tmp_branch, &branch, &fileindex,
6198 worktree, repo);
6199 if (error)
6200 goto done;
6201 printf("Switching work tree to %s\n",
6202 got_ref_get_symref_target(new_base_branch));
6203 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6204 new_base_branch, update_progress, &did_something);
6205 if (error)
6206 goto done;
6207 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6208 goto done; /* nothing else to do */
6211 if (continue_rebase) {
6212 if (!rebase_in_progress) {
6213 error = got_error(GOT_ERR_NOT_REBASING);
6214 goto done;
6216 error = got_worktree_rebase_continue(&resume_commit_id,
6217 &new_base_branch, &tmp_branch, &branch, &fileindex,
6218 worktree, repo);
6219 if (error)
6220 goto done;
6222 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6223 resume_commit_id, repo);
6224 if (error)
6225 goto done;
6227 yca_id = got_object_id_dup(resume_commit_id);
6228 if (yca_id == NULL) {
6229 error = got_error_from_errno("got_object_id_dup");
6230 goto done;
6232 } else {
6233 error = got_ref_open(&branch, repo, argv[0], 0);
6234 if (error != NULL)
6235 goto done;
6238 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6239 if (error)
6240 goto done;
6242 if (!continue_rebase) {
6243 struct got_object_id *base_commit_id;
6245 base_commit_id = got_worktree_get_base_commit_id(worktree);
6246 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6247 base_commit_id, branch_head_commit_id, repo,
6248 check_cancelled, NULL);
6249 if (error)
6250 goto done;
6251 if (yca_id == NULL) {
6252 error = got_error_msg(GOT_ERR_ANCESTRY,
6253 "specified branch shares no common ancestry "
6254 "with work tree's branch");
6255 goto done;
6258 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6259 if (error) {
6260 if (error->code != GOT_ERR_ANCESTRY)
6261 goto done;
6262 error = NULL;
6263 } else {
6264 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6265 "specified branch resolves to a commit which "
6266 "is already contained in work tree's branch");
6267 goto done;
6269 error = got_worktree_rebase_prepare(&new_base_branch,
6270 &tmp_branch, &fileindex, worktree, branch, repo);
6271 if (error)
6272 goto done;
6275 commit_id = branch_head_commit_id;
6276 error = got_object_open_as_commit(&commit, repo, commit_id);
6277 if (error)
6278 goto done;
6280 parent_ids = got_object_commit_get_parent_ids(commit);
6281 pid = SIMPLEQ_FIRST(parent_ids);
6282 if (pid == NULL) {
6283 if (!continue_rebase) {
6284 int did_something;
6285 error = got_worktree_rebase_abort(worktree, fileindex,
6286 repo, new_base_branch, update_progress,
6287 &did_something);
6288 if (error)
6289 goto done;
6290 printf("Rebase of %s aborted\n",
6291 got_ref_get_name(branch));
6293 error = got_error(GOT_ERR_EMPTY_REBASE);
6294 goto done;
6296 error = collect_commits(&commits, commit_id, pid->id,
6297 yca_id, got_worktree_get_path_prefix(worktree),
6298 GOT_ERR_REBASE_PATH, repo);
6299 got_object_commit_close(commit);
6300 commit = NULL;
6301 if (error)
6302 goto done;
6304 if (SIMPLEQ_EMPTY(&commits)) {
6305 if (continue_rebase) {
6306 error = rebase_complete(worktree, fileindex,
6307 branch, new_base_branch, tmp_branch, repo);
6308 goto done;
6309 } else {
6310 /* Fast-forward the reference of the branch. */
6311 struct got_object_id *new_head_commit_id;
6312 char *id_str;
6313 error = got_ref_resolve(&new_head_commit_id, repo,
6314 new_base_branch);
6315 if (error)
6316 goto done;
6317 error = got_object_id_str(&id_str, new_head_commit_id);
6318 printf("Forwarding %s to commit %s\n",
6319 got_ref_get_name(branch), id_str);
6320 free(id_str);
6321 error = got_ref_change_ref(branch,
6322 new_head_commit_id);
6323 if (error)
6324 goto done;
6328 pid = NULL;
6329 SIMPLEQ_FOREACH(qid, &commits, entry) {
6330 commit_id = qid->id;
6331 parent_id = pid ? pid->id : yca_id;
6332 pid = qid;
6334 error = got_worktree_rebase_merge_files(&merged_paths,
6335 worktree, fileindex, parent_id, commit_id, repo,
6336 rebase_progress, &rebase_status, check_cancelled, NULL);
6337 if (error)
6338 goto done;
6340 if (rebase_status == GOT_STATUS_CONFLICT) {
6341 error = show_rebase_merge_conflict(qid->id, repo);
6342 if (error)
6343 goto done;
6344 got_worktree_rebase_pathlist_free(&merged_paths);
6345 break;
6348 error = rebase_commit(&merged_paths, worktree, fileindex,
6349 tmp_branch, commit_id, repo);
6350 got_worktree_rebase_pathlist_free(&merged_paths);
6351 if (error)
6352 goto done;
6355 if (rebase_status == GOT_STATUS_CONFLICT) {
6356 error = got_worktree_rebase_postpone(worktree, fileindex);
6357 if (error)
6358 goto done;
6359 error = got_error_msg(GOT_ERR_CONFLICTS,
6360 "conflicts must be resolved before rebasing can continue");
6361 } else
6362 error = rebase_complete(worktree, fileindex, branch,
6363 new_base_branch, tmp_branch, repo);
6364 done:
6365 got_object_id_queue_free(&commits);
6366 free(branch_head_commit_id);
6367 free(resume_commit_id);
6368 free(yca_id);
6369 if (commit)
6370 got_object_commit_close(commit);
6371 if (branch)
6372 got_ref_close(branch);
6373 if (new_base_branch)
6374 got_ref_close(new_base_branch);
6375 if (tmp_branch)
6376 got_ref_close(tmp_branch);
6377 if (worktree)
6378 got_worktree_close(worktree);
6379 if (repo)
6380 got_repo_close(repo);
6381 return error;
6384 __dead static void
6385 usage_histedit(void)
6387 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6388 getprogname());
6389 exit(1);
6392 #define GOT_HISTEDIT_PICK 'p'
6393 #define GOT_HISTEDIT_EDIT 'e'
6394 #define GOT_HISTEDIT_FOLD 'f'
6395 #define GOT_HISTEDIT_DROP 'd'
6396 #define GOT_HISTEDIT_MESG 'm'
6398 static struct got_histedit_cmd {
6399 unsigned char code;
6400 const char *name;
6401 const char *desc;
6402 } got_histedit_cmds[] = {
6403 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6404 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6405 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6406 "be used" },
6407 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6408 { GOT_HISTEDIT_MESG, "mesg",
6409 "single-line log message for commit above (open editor if empty)" },
6412 struct got_histedit_list_entry {
6413 TAILQ_ENTRY(got_histedit_list_entry) entry;
6414 struct got_object_id *commit_id;
6415 const struct got_histedit_cmd *cmd;
6416 char *logmsg;
6418 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6420 static const struct got_error *
6421 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6422 FILE *f, struct got_repository *repo)
6424 const struct got_error *err = NULL;
6425 char *logmsg = NULL, *id_str = NULL;
6426 struct got_commit_object *commit = NULL;
6427 int n;
6429 err = got_object_open_as_commit(&commit, repo, commit_id);
6430 if (err)
6431 goto done;
6433 err = get_short_logmsg(&logmsg, 34, commit);
6434 if (err)
6435 goto done;
6437 err = got_object_id_str(&id_str, commit_id);
6438 if (err)
6439 goto done;
6441 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6442 if (n < 0)
6443 err = got_ferror(f, GOT_ERR_IO);
6444 done:
6445 if (commit)
6446 got_object_commit_close(commit);
6447 free(id_str);
6448 free(logmsg);
6449 return err;
6452 static const struct got_error *
6453 histedit_write_commit_list(struct got_object_id_queue *commits,
6454 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6456 const struct got_error *err = NULL;
6457 struct got_object_qid *qid;
6459 if (SIMPLEQ_EMPTY(commits))
6460 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6462 SIMPLEQ_FOREACH(qid, commits, entry) {
6463 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6464 f, repo);
6465 if (err)
6466 break;
6467 if (edit_logmsg_only) {
6468 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6469 if (n < 0) {
6470 err = got_ferror(f, GOT_ERR_IO);
6471 break;
6476 return err;
6479 static const struct got_error *
6480 write_cmd_list(FILE *f, const char *branch_name,
6481 struct got_object_id_queue *commits)
6483 const struct got_error *err = NULL;
6484 int n, i;
6485 char *id_str;
6486 struct got_object_qid *qid;
6488 qid = SIMPLEQ_FIRST(commits);
6489 err = got_object_id_str(&id_str, qid->id);
6490 if (err)
6491 return err;
6493 n = fprintf(f,
6494 "# Editing the history of branch '%s' starting at\n"
6495 "# commit %s\n"
6496 "# Commits will be processed in order from top to "
6497 "bottom of this file.\n", branch_name, id_str);
6498 if (n < 0) {
6499 err = got_ferror(f, GOT_ERR_IO);
6500 goto done;
6503 n = fprintf(f, "# Available histedit commands:\n");
6504 if (n < 0) {
6505 err = got_ferror(f, GOT_ERR_IO);
6506 goto done;
6509 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6510 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6511 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6512 cmd->desc);
6513 if (n < 0) {
6514 err = got_ferror(f, GOT_ERR_IO);
6515 break;
6518 done:
6519 free(id_str);
6520 return err;
6523 static const struct got_error *
6524 histedit_syntax_error(int lineno)
6526 static char msg[42];
6527 int ret;
6529 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6530 lineno);
6531 if (ret == -1 || ret >= sizeof(msg))
6532 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6534 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6537 static const struct got_error *
6538 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6539 char *logmsg, struct got_repository *repo)
6541 const struct got_error *err;
6542 struct got_commit_object *folded_commit = NULL;
6543 char *id_str, *folded_logmsg = NULL;
6545 err = got_object_id_str(&id_str, hle->commit_id);
6546 if (err)
6547 return err;
6549 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6550 if (err)
6551 goto done;
6553 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6554 if (err)
6555 goto done;
6556 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6557 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6558 folded_logmsg) == -1) {
6559 err = got_error_from_errno("asprintf");
6561 done:
6562 if (folded_commit)
6563 got_object_commit_close(folded_commit);
6564 free(id_str);
6565 free(folded_logmsg);
6566 return err;
6569 static struct got_histedit_list_entry *
6570 get_folded_commits(struct got_histedit_list_entry *hle)
6572 struct got_histedit_list_entry *prev, *folded = NULL;
6574 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6575 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6576 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6577 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6578 folded = prev;
6579 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6582 return folded;
6585 static const struct got_error *
6586 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6587 struct got_repository *repo)
6589 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6590 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6591 const struct got_error *err = NULL;
6592 struct got_commit_object *commit = NULL;
6593 int fd;
6594 struct got_histedit_list_entry *folded = NULL;
6596 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6597 if (err)
6598 return err;
6600 folded = get_folded_commits(hle);
6601 if (folded) {
6602 while (folded != hle) {
6603 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6604 folded = TAILQ_NEXT(folded, entry);
6605 continue;
6607 err = append_folded_commit_msg(&new_msg, folded,
6608 logmsg, repo);
6609 if (err)
6610 goto done;
6611 free(logmsg);
6612 logmsg = new_msg;
6613 folded = TAILQ_NEXT(folded, entry);
6617 err = got_object_id_str(&id_str, hle->commit_id);
6618 if (err)
6619 goto done;
6620 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6621 if (err)
6622 goto done;
6623 if (asprintf(&new_msg,
6624 "%s\n# original log message of commit %s: %s",
6625 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6626 err = got_error_from_errno("asprintf");
6627 goto done;
6629 free(logmsg);
6630 logmsg = new_msg;
6632 err = got_object_id_str(&id_str, hle->commit_id);
6633 if (err)
6634 goto done;
6636 err = got_opentemp_named_fd(&logmsg_path, &fd,
6637 GOT_TMPDIR_STR "/got-logmsg");
6638 if (err)
6639 goto done;
6641 dprintf(fd, logmsg);
6642 close(fd);
6644 err = get_editor(&editor);
6645 if (err)
6646 goto done;
6648 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6649 if (err) {
6650 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6651 goto done;
6652 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6654 done:
6655 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6656 err = got_error_from_errno2("unlink", logmsg_path);
6657 free(logmsg_path);
6658 free(logmsg);
6659 free(orig_logmsg);
6660 free(editor);
6661 if (commit)
6662 got_object_commit_close(commit);
6663 return err;
6666 static const struct got_error *
6667 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6668 FILE *f, struct got_repository *repo)
6670 const struct got_error *err = NULL;
6671 char *line = NULL, *p, *end;
6672 size_t size;
6673 ssize_t len;
6674 int lineno = 0, i;
6675 const struct got_histedit_cmd *cmd;
6676 struct got_object_id *commit_id = NULL;
6677 struct got_histedit_list_entry *hle = NULL;
6679 for (;;) {
6680 len = getline(&line, &size, f);
6681 if (len == -1) {
6682 const struct got_error *getline_err;
6683 if (feof(f))
6684 break;
6685 getline_err = got_error_from_errno("getline");
6686 err = got_ferror(f, getline_err->code);
6687 break;
6689 lineno++;
6690 p = line;
6691 while (isspace((unsigned char)p[0]))
6692 p++;
6693 if (p[0] == '#' || p[0] == '\0') {
6694 free(line);
6695 line = NULL;
6696 continue;
6698 cmd = NULL;
6699 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6700 cmd = &got_histedit_cmds[i];
6701 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6702 isspace((unsigned char)p[strlen(cmd->name)])) {
6703 p += strlen(cmd->name);
6704 break;
6706 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6707 p++;
6708 break;
6711 if (i == nitems(got_histedit_cmds)) {
6712 err = histedit_syntax_error(lineno);
6713 break;
6715 while (isspace((unsigned char)p[0]))
6716 p++;
6717 if (cmd->code == GOT_HISTEDIT_MESG) {
6718 if (hle == NULL || hle->logmsg != NULL) {
6719 err = got_error(GOT_ERR_HISTEDIT_CMD);
6720 break;
6722 if (p[0] == '\0') {
6723 err = histedit_edit_logmsg(hle, repo);
6724 if (err)
6725 break;
6726 } else {
6727 hle->logmsg = strdup(p);
6728 if (hle->logmsg == NULL) {
6729 err = got_error_from_errno("strdup");
6730 break;
6733 free(line);
6734 line = NULL;
6735 continue;
6736 } else {
6737 end = p;
6738 while (end[0] && !isspace((unsigned char)end[0]))
6739 end++;
6740 *end = '\0';
6742 err = got_object_resolve_id_str(&commit_id, repo, p);
6743 if (err) {
6744 /* override error code */
6745 err = histedit_syntax_error(lineno);
6746 break;
6749 hle = malloc(sizeof(*hle));
6750 if (hle == NULL) {
6751 err = got_error_from_errno("malloc");
6752 break;
6754 hle->cmd = cmd;
6755 hle->commit_id = commit_id;
6756 hle->logmsg = NULL;
6757 commit_id = NULL;
6758 free(line);
6759 line = NULL;
6760 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6763 free(line);
6764 free(commit_id);
6765 return err;
6768 static const struct got_error *
6769 histedit_check_script(struct got_histedit_list *histedit_cmds,
6770 struct got_object_id_queue *commits, struct got_repository *repo)
6772 const struct got_error *err = NULL;
6773 struct got_object_qid *qid;
6774 struct got_histedit_list_entry *hle;
6775 static char msg[92];
6776 char *id_str;
6778 if (TAILQ_EMPTY(histedit_cmds))
6779 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6780 "histedit script contains no commands");
6781 if (SIMPLEQ_EMPTY(commits))
6782 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6784 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6785 struct got_histedit_list_entry *hle2;
6786 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6787 if (hle == hle2)
6788 continue;
6789 if (got_object_id_cmp(hle->commit_id,
6790 hle2->commit_id) != 0)
6791 continue;
6792 err = got_object_id_str(&id_str, hle->commit_id);
6793 if (err)
6794 return err;
6795 snprintf(msg, sizeof(msg), "commit %s is listed "
6796 "more than once in histedit script", id_str);
6797 free(id_str);
6798 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6802 SIMPLEQ_FOREACH(qid, commits, entry) {
6803 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6804 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6805 break;
6807 if (hle == NULL) {
6808 err = got_object_id_str(&id_str, qid->id);
6809 if (err)
6810 return err;
6811 snprintf(msg, sizeof(msg),
6812 "commit %s missing from histedit script", id_str);
6813 free(id_str);
6814 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6818 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6819 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6820 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6821 "last commit in histedit script cannot be folded");
6823 return NULL;
6826 static const struct got_error *
6827 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6828 const char *path, struct got_object_id_queue *commits,
6829 struct got_repository *repo)
6831 const struct got_error *err = NULL;
6832 char *editor;
6833 FILE *f = NULL;
6835 err = get_editor(&editor);
6836 if (err)
6837 return err;
6839 if (spawn_editor(editor, path) == -1) {
6840 err = got_error_from_errno("failed spawning editor");
6841 goto done;
6844 f = fopen(path, "r");
6845 if (f == NULL) {
6846 err = got_error_from_errno("fopen");
6847 goto done;
6849 err = histedit_parse_list(histedit_cmds, f, repo);
6850 if (err)
6851 goto done;
6853 err = histedit_check_script(histedit_cmds, commits, repo);
6854 done:
6855 if (f && fclose(f) != 0 && err == NULL)
6856 err = got_error_from_errno("fclose");
6857 free(editor);
6858 return err;
6861 static const struct got_error *
6862 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6863 struct got_object_id_queue *, const char *, const char *,
6864 struct got_repository *);
6866 static const struct got_error *
6867 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6868 struct got_object_id_queue *commits, const char *branch_name,
6869 int edit_logmsg_only, struct got_repository *repo)
6871 const struct got_error *err;
6872 FILE *f = NULL;
6873 char *path = NULL;
6875 err = got_opentemp_named(&path, &f, "got-histedit");
6876 if (err)
6877 return err;
6879 err = write_cmd_list(f, branch_name, commits);
6880 if (err)
6881 goto done;
6883 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6884 if (err)
6885 goto done;
6887 if (edit_logmsg_only) {
6888 rewind(f);
6889 err = histedit_parse_list(histedit_cmds, f, repo);
6890 } else {
6891 if (fclose(f) != 0) {
6892 err = got_error_from_errno("fclose");
6893 goto done;
6895 f = NULL;
6896 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6897 if (err) {
6898 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6899 err->code != GOT_ERR_HISTEDIT_CMD)
6900 goto done;
6901 err = histedit_edit_list_retry(histedit_cmds, err,
6902 commits, path, branch_name, repo);
6905 done:
6906 if (f && fclose(f) != 0 && err == NULL)
6907 err = got_error_from_errno("fclose");
6908 if (path && unlink(path) != 0 && err == NULL)
6909 err = got_error_from_errno2("unlink", path);
6910 free(path);
6911 return err;
6914 static const struct got_error *
6915 histedit_save_list(struct got_histedit_list *histedit_cmds,
6916 struct got_worktree *worktree, struct got_repository *repo)
6918 const struct got_error *err = NULL;
6919 char *path = NULL;
6920 FILE *f = NULL;
6921 struct got_histedit_list_entry *hle;
6922 struct got_commit_object *commit = NULL;
6924 err = got_worktree_get_histedit_script_path(&path, worktree);
6925 if (err)
6926 return err;
6928 f = fopen(path, "w");
6929 if (f == NULL) {
6930 err = got_error_from_errno2("fopen", path);
6931 goto done;
6933 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6934 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6935 repo);
6936 if (err)
6937 break;
6939 if (hle->logmsg) {
6940 int n = fprintf(f, "%c %s\n",
6941 GOT_HISTEDIT_MESG, hle->logmsg);
6942 if (n < 0) {
6943 err = got_ferror(f, GOT_ERR_IO);
6944 break;
6948 done:
6949 if (f && fclose(f) != 0 && err == NULL)
6950 err = got_error_from_errno("fclose");
6951 free(path);
6952 if (commit)
6953 got_object_commit_close(commit);
6954 return err;
6957 void
6958 histedit_free_list(struct got_histedit_list *histedit_cmds)
6960 struct got_histedit_list_entry *hle;
6962 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6963 TAILQ_REMOVE(histedit_cmds, hle, entry);
6964 free(hle);
6968 static const struct got_error *
6969 histedit_load_list(struct got_histedit_list *histedit_cmds,
6970 const char *path, struct got_repository *repo)
6972 const struct got_error *err = NULL;
6973 FILE *f = NULL;
6975 f = fopen(path, "r");
6976 if (f == NULL) {
6977 err = got_error_from_errno2("fopen", path);
6978 goto done;
6981 err = histedit_parse_list(histedit_cmds, f, repo);
6982 done:
6983 if (f && fclose(f) != 0 && err == NULL)
6984 err = got_error_from_errno("fclose");
6985 return err;
6988 static const struct got_error *
6989 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6990 const struct got_error *edit_err, struct got_object_id_queue *commits,
6991 const char *path, const char *branch_name, struct got_repository *repo)
6993 const struct got_error *err = NULL, *prev_err = edit_err;
6994 int resp = ' ';
6996 while (resp != 'c' && resp != 'r' && resp != 'a') {
6997 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6998 "or (a)bort: ", getprogname(), prev_err->msg);
6999 resp = getchar();
7000 if (resp == '\n')
7001 resp = getchar();
7002 if (resp == 'c') {
7003 histedit_free_list(histedit_cmds);
7004 err = histedit_run_editor(histedit_cmds, path, commits,
7005 repo);
7006 if (err) {
7007 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7008 err->code != GOT_ERR_HISTEDIT_CMD)
7009 break;
7010 prev_err = err;
7011 resp = ' ';
7012 continue;
7014 break;
7015 } else if (resp == 'r') {
7016 histedit_free_list(histedit_cmds);
7017 err = histedit_edit_script(histedit_cmds,
7018 commits, branch_name, 0, repo);
7019 if (err) {
7020 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7021 err->code != GOT_ERR_HISTEDIT_CMD)
7022 break;
7023 prev_err = err;
7024 resp = ' ';
7025 continue;
7027 break;
7028 } else if (resp == 'a') {
7029 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7030 break;
7031 } else
7032 printf("invalid response '%c'\n", resp);
7035 return err;
7038 static const struct got_error *
7039 histedit_complete(struct got_worktree *worktree,
7040 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7041 struct got_reference *branch, struct got_repository *repo)
7043 printf("Switching work tree to %s\n",
7044 got_ref_get_symref_target(branch));
7045 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7046 branch, repo);
7049 static const struct got_error *
7050 show_histedit_progress(struct got_commit_object *commit,
7051 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7053 const struct got_error *err;
7054 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7056 err = got_object_id_str(&old_id_str, hle->commit_id);
7057 if (err)
7058 goto done;
7060 if (new_id) {
7061 err = got_object_id_str(&new_id_str, new_id);
7062 if (err)
7063 goto done;
7066 old_id_str[12] = '\0';
7067 if (new_id_str)
7068 new_id_str[12] = '\0';
7070 if (hle->logmsg) {
7071 logmsg = strdup(hle->logmsg);
7072 if (logmsg == NULL) {
7073 err = got_error_from_errno("strdup");
7074 goto done;
7076 trim_logmsg(logmsg, 42);
7077 } else {
7078 err = get_short_logmsg(&logmsg, 42, commit);
7079 if (err)
7080 goto done;
7083 switch (hle->cmd->code) {
7084 case GOT_HISTEDIT_PICK:
7085 case GOT_HISTEDIT_EDIT:
7086 printf("%s -> %s: %s\n", old_id_str,
7087 new_id_str ? new_id_str : "no-op change", logmsg);
7088 break;
7089 case GOT_HISTEDIT_DROP:
7090 case GOT_HISTEDIT_FOLD:
7091 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7092 logmsg);
7093 break;
7094 default:
7095 break;
7097 done:
7098 free(old_id_str);
7099 free(new_id_str);
7100 return err;
7103 static const struct got_error *
7104 histedit_commit(struct got_pathlist_head *merged_paths,
7105 struct got_worktree *worktree, struct got_fileindex *fileindex,
7106 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7107 struct got_repository *repo)
7109 const struct got_error *err;
7110 struct got_commit_object *commit;
7111 struct got_object_id *new_commit_id;
7113 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7114 && hle->logmsg == NULL) {
7115 err = histedit_edit_logmsg(hle, repo);
7116 if (err)
7117 return err;
7120 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7121 if (err)
7122 return err;
7124 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7125 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7126 hle->logmsg, repo);
7127 if (err) {
7128 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7129 goto done;
7130 err = show_histedit_progress(commit, hle, NULL);
7131 } else {
7132 err = show_histedit_progress(commit, hle, new_commit_id);
7133 free(new_commit_id);
7135 done:
7136 got_object_commit_close(commit);
7137 return err;
7140 static const struct got_error *
7141 histedit_skip_commit(struct got_histedit_list_entry *hle,
7142 struct got_worktree *worktree, struct got_repository *repo)
7144 const struct got_error *error;
7145 struct got_commit_object *commit;
7147 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7148 repo);
7149 if (error)
7150 return error;
7152 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7153 if (error)
7154 return error;
7156 error = show_histedit_progress(commit, hle, NULL);
7157 got_object_commit_close(commit);
7158 return error;
7161 static const struct got_error *
7162 check_local_changes(void *arg, unsigned char status,
7163 unsigned char staged_status, const char *path,
7164 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7165 struct got_object_id *commit_id, int dirfd, const char *de_name)
7167 int *have_local_changes = arg;
7169 switch (status) {
7170 case GOT_STATUS_ADD:
7171 case GOT_STATUS_DELETE:
7172 case GOT_STATUS_MODIFY:
7173 case GOT_STATUS_CONFLICT:
7174 *have_local_changes = 1;
7175 return got_error(GOT_ERR_CANCELLED);
7176 default:
7177 break;
7180 switch (staged_status) {
7181 case GOT_STATUS_ADD:
7182 case GOT_STATUS_DELETE:
7183 case GOT_STATUS_MODIFY:
7184 *have_local_changes = 1;
7185 return got_error(GOT_ERR_CANCELLED);
7186 default:
7187 break;
7190 return NULL;
7193 static const struct got_error *
7194 cmd_histedit(int argc, char *argv[])
7196 const struct got_error *error = NULL;
7197 struct got_worktree *worktree = NULL;
7198 struct got_fileindex *fileindex = NULL;
7199 struct got_repository *repo = NULL;
7200 char *cwd = NULL;
7201 struct got_reference *branch = NULL;
7202 struct got_reference *tmp_branch = NULL;
7203 struct got_object_id *resume_commit_id = NULL;
7204 struct got_object_id *base_commit_id = NULL;
7205 struct got_object_id *head_commit_id = NULL;
7206 struct got_commit_object *commit = NULL;
7207 int ch, rebase_in_progress = 0, did_something;
7208 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7209 int edit_logmsg_only = 0;
7210 const char *edit_script_path = NULL;
7211 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7212 struct got_object_id_queue commits;
7213 struct got_pathlist_head merged_paths;
7214 const struct got_object_id_queue *parent_ids;
7215 struct got_object_qid *pid;
7216 struct got_histedit_list histedit_cmds;
7217 struct got_histedit_list_entry *hle;
7219 SIMPLEQ_INIT(&commits);
7220 TAILQ_INIT(&histedit_cmds);
7221 TAILQ_INIT(&merged_paths);
7223 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7224 switch (ch) {
7225 case 'a':
7226 abort_edit = 1;
7227 break;
7228 case 'c':
7229 continue_edit = 1;
7230 break;
7231 case 'F':
7232 edit_script_path = optarg;
7233 break;
7234 case 'm':
7235 edit_logmsg_only = 1;
7236 break;
7237 default:
7238 usage_histedit();
7239 /* NOTREACHED */
7243 argc -= optind;
7244 argv += optind;
7246 #ifndef PROFILE
7247 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7248 "unveil", NULL) == -1)
7249 err(1, "pledge");
7250 #endif
7251 if (abort_edit && continue_edit)
7252 errx(1, "histedit's -a and -c options are mutually exclusive");
7253 if (edit_script_path && edit_logmsg_only)
7254 errx(1, "histedit's -F and -m options are mutually exclusive");
7255 if (abort_edit && edit_logmsg_only)
7256 errx(1, "histedit's -a and -m options are mutually exclusive");
7257 if (continue_edit && edit_logmsg_only)
7258 errx(1, "histedit's -c and -m options are mutually exclusive");
7259 if (argc != 0)
7260 usage_histedit();
7263 * This command cannot apply unveil(2) in all cases because the
7264 * user may choose to run an editor to edit the histedit script
7265 * and to edit individual commit log messages.
7266 * unveil(2) traverses exec(2); if an editor is used we have to
7267 * apply unveil after edit script and log messages have been written.
7268 * XXX TODO: Make use of unveil(2) where possible.
7271 cwd = getcwd(NULL, 0);
7272 if (cwd == NULL) {
7273 error = got_error_from_errno("getcwd");
7274 goto done;
7276 error = got_worktree_open(&worktree, cwd);
7277 if (error)
7278 goto done;
7280 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7281 NULL);
7282 if (error != NULL)
7283 goto done;
7285 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7286 if (error)
7287 goto done;
7288 if (rebase_in_progress) {
7289 error = got_error(GOT_ERR_REBASING);
7290 goto done;
7293 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7294 if (error)
7295 goto done;
7297 if (edit_in_progress && edit_logmsg_only) {
7298 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7299 "histedit operation is in progress in this "
7300 "work tree and must be continued or aborted "
7301 "before the -m option can be used");
7302 goto done;
7305 if (edit_in_progress && abort_edit) {
7306 error = got_worktree_histedit_continue(&resume_commit_id,
7307 &tmp_branch, &branch, &base_commit_id, &fileindex,
7308 worktree, repo);
7309 if (error)
7310 goto done;
7311 printf("Switching work tree to %s\n",
7312 got_ref_get_symref_target(branch));
7313 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7314 branch, base_commit_id, update_progress, &did_something);
7315 if (error)
7316 goto done;
7317 printf("Histedit of %s aborted\n",
7318 got_ref_get_symref_target(branch));
7319 goto done; /* nothing else to do */
7320 } else if (abort_edit) {
7321 error = got_error(GOT_ERR_NOT_HISTEDIT);
7322 goto done;
7325 if (continue_edit) {
7326 char *path;
7328 if (!edit_in_progress) {
7329 error = got_error(GOT_ERR_NOT_HISTEDIT);
7330 goto done;
7333 error = got_worktree_get_histedit_script_path(&path, worktree);
7334 if (error)
7335 goto done;
7337 error = histedit_load_list(&histedit_cmds, path, repo);
7338 free(path);
7339 if (error)
7340 goto done;
7342 error = got_worktree_histedit_continue(&resume_commit_id,
7343 &tmp_branch, &branch, &base_commit_id, &fileindex,
7344 worktree, repo);
7345 if (error)
7346 goto done;
7348 error = got_ref_resolve(&head_commit_id, repo, branch);
7349 if (error)
7350 goto done;
7352 error = got_object_open_as_commit(&commit, repo,
7353 head_commit_id);
7354 if (error)
7355 goto done;
7356 parent_ids = got_object_commit_get_parent_ids(commit);
7357 pid = SIMPLEQ_FIRST(parent_ids);
7358 if (pid == NULL) {
7359 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7360 goto done;
7362 error = collect_commits(&commits, head_commit_id, pid->id,
7363 base_commit_id, got_worktree_get_path_prefix(worktree),
7364 GOT_ERR_HISTEDIT_PATH, repo);
7365 got_object_commit_close(commit);
7366 commit = NULL;
7367 if (error)
7368 goto done;
7369 } else {
7370 if (edit_in_progress) {
7371 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7372 goto done;
7375 error = got_ref_open(&branch, repo,
7376 got_worktree_get_head_ref_name(worktree), 0);
7377 if (error != NULL)
7378 goto done;
7380 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7381 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7382 "will not edit commit history of a branch outside "
7383 "the \"refs/heads/\" reference namespace");
7384 goto done;
7387 error = got_ref_resolve(&head_commit_id, repo, branch);
7388 got_ref_close(branch);
7389 branch = NULL;
7390 if (error)
7391 goto done;
7393 error = got_object_open_as_commit(&commit, repo,
7394 head_commit_id);
7395 if (error)
7396 goto done;
7397 parent_ids = got_object_commit_get_parent_ids(commit);
7398 pid = SIMPLEQ_FIRST(parent_ids);
7399 if (pid == NULL) {
7400 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7401 goto done;
7403 error = collect_commits(&commits, head_commit_id, pid->id,
7404 got_worktree_get_base_commit_id(worktree),
7405 got_worktree_get_path_prefix(worktree),
7406 GOT_ERR_HISTEDIT_PATH, repo);
7407 got_object_commit_close(commit);
7408 commit = NULL;
7409 if (error)
7410 goto done;
7412 if (SIMPLEQ_EMPTY(&commits)) {
7413 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7414 goto done;
7417 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7418 &base_commit_id, &fileindex, worktree, repo);
7419 if (error)
7420 goto done;
7422 if (edit_script_path) {
7423 error = histedit_load_list(&histedit_cmds,
7424 edit_script_path, repo);
7425 if (error) {
7426 got_worktree_histedit_abort(worktree, fileindex,
7427 repo, branch, base_commit_id,
7428 update_progress, &did_something);
7429 goto done;
7431 } else {
7432 const char *branch_name;
7433 branch_name = got_ref_get_symref_target(branch);
7434 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7435 branch_name += 11;
7436 error = histedit_edit_script(&histedit_cmds, &commits,
7437 branch_name, edit_logmsg_only, repo);
7438 if (error) {
7439 got_worktree_histedit_abort(worktree, fileindex,
7440 repo, branch, base_commit_id,
7441 update_progress, &did_something);
7442 goto done;
7447 error = histedit_save_list(&histedit_cmds, worktree,
7448 repo);
7449 if (error) {
7450 got_worktree_histedit_abort(worktree, fileindex,
7451 repo, branch, base_commit_id,
7452 update_progress, &did_something);
7453 goto done;
7458 error = histedit_check_script(&histedit_cmds, &commits, repo);
7459 if (error)
7460 goto done;
7462 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7463 if (resume_commit_id) {
7464 if (got_object_id_cmp(hle->commit_id,
7465 resume_commit_id) != 0)
7466 continue;
7468 resume_commit_id = NULL;
7469 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7470 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7471 error = histedit_skip_commit(hle, worktree,
7472 repo);
7473 if (error)
7474 goto done;
7475 } else {
7476 struct got_pathlist_head paths;
7477 int have_changes = 0;
7479 TAILQ_INIT(&paths);
7480 error = got_pathlist_append(&paths, "", NULL);
7481 if (error)
7482 goto done;
7483 error = got_worktree_status(worktree, &paths,
7484 repo, check_local_changes, &have_changes,
7485 check_cancelled, NULL);
7486 got_pathlist_free(&paths);
7487 if (error) {
7488 if (error->code != GOT_ERR_CANCELLED)
7489 goto done;
7490 if (sigint_received || sigpipe_received)
7491 goto done;
7493 if (have_changes) {
7494 error = histedit_commit(NULL, worktree,
7495 fileindex, tmp_branch, hle, repo);
7496 if (error)
7497 goto done;
7498 } else {
7499 error = got_object_open_as_commit(
7500 &commit, repo, hle->commit_id);
7501 if (error)
7502 goto done;
7503 error = show_histedit_progress(commit,
7504 hle, NULL);
7505 got_object_commit_close(commit);
7506 commit = NULL;
7507 if (error)
7508 goto done;
7511 continue;
7514 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7515 error = histedit_skip_commit(hle, worktree, repo);
7516 if (error)
7517 goto done;
7518 continue;
7521 error = got_object_open_as_commit(&commit, repo,
7522 hle->commit_id);
7523 if (error)
7524 goto done;
7525 parent_ids = got_object_commit_get_parent_ids(commit);
7526 pid = SIMPLEQ_FIRST(parent_ids);
7528 error = got_worktree_histedit_merge_files(&merged_paths,
7529 worktree, fileindex, pid->id, hle->commit_id, repo,
7530 rebase_progress, &rebase_status, check_cancelled, NULL);
7531 if (error)
7532 goto done;
7533 got_object_commit_close(commit);
7534 commit = NULL;
7536 if (rebase_status == GOT_STATUS_CONFLICT) {
7537 error = show_rebase_merge_conflict(hle->commit_id,
7538 repo);
7539 if (error)
7540 goto done;
7541 got_worktree_rebase_pathlist_free(&merged_paths);
7542 break;
7545 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7546 char *id_str;
7547 error = got_object_id_str(&id_str, hle->commit_id);
7548 if (error)
7549 goto done;
7550 printf("Stopping histedit for amending commit %s\n",
7551 id_str);
7552 free(id_str);
7553 got_worktree_rebase_pathlist_free(&merged_paths);
7554 error = got_worktree_histedit_postpone(worktree,
7555 fileindex);
7556 goto done;
7559 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7560 error = histedit_skip_commit(hle, worktree, repo);
7561 if (error)
7562 goto done;
7563 continue;
7566 error = histedit_commit(&merged_paths, worktree, fileindex,
7567 tmp_branch, hle, repo);
7568 got_worktree_rebase_pathlist_free(&merged_paths);
7569 if (error)
7570 goto done;
7573 if (rebase_status == GOT_STATUS_CONFLICT) {
7574 error = got_worktree_histedit_postpone(worktree, fileindex);
7575 if (error)
7576 goto done;
7577 error = got_error_msg(GOT_ERR_CONFLICTS,
7578 "conflicts must be resolved before histedit can continue");
7579 } else
7580 error = histedit_complete(worktree, fileindex, tmp_branch,
7581 branch, repo);
7582 done:
7583 got_object_id_queue_free(&commits);
7584 histedit_free_list(&histedit_cmds);
7585 free(head_commit_id);
7586 free(base_commit_id);
7587 free(resume_commit_id);
7588 if (commit)
7589 got_object_commit_close(commit);
7590 if (branch)
7591 got_ref_close(branch);
7592 if (tmp_branch)
7593 got_ref_close(tmp_branch);
7594 if (worktree)
7595 got_worktree_close(worktree);
7596 if (repo)
7597 got_repo_close(repo);
7598 return error;
7601 __dead static void
7602 usage_integrate(void)
7604 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7605 exit(1);
7608 static const struct got_error *
7609 cmd_integrate(int argc, char *argv[])
7611 const struct got_error *error = NULL;
7612 struct got_repository *repo = NULL;
7613 struct got_worktree *worktree = NULL;
7614 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7615 const char *branch_arg = NULL;
7616 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7617 struct got_fileindex *fileindex = NULL;
7618 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7619 int ch, did_something = 0;
7621 while ((ch = getopt(argc, argv, "")) != -1) {
7622 switch (ch) {
7623 default:
7624 usage_integrate();
7625 /* NOTREACHED */
7629 argc -= optind;
7630 argv += optind;
7632 if (argc != 1)
7633 usage_integrate();
7634 branch_arg = argv[0];
7636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7637 "unveil", NULL) == -1)
7638 err(1, "pledge");
7640 cwd = getcwd(NULL, 0);
7641 if (cwd == NULL) {
7642 error = got_error_from_errno("getcwd");
7643 goto done;
7646 error = got_worktree_open(&worktree, cwd);
7647 if (error)
7648 goto done;
7650 error = check_rebase_or_histedit_in_progress(worktree);
7651 if (error)
7652 goto done;
7654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7655 NULL);
7656 if (error != NULL)
7657 goto done;
7659 error = apply_unveil(got_repo_get_path(repo), 0,
7660 got_worktree_get_root_path(worktree));
7661 if (error)
7662 goto done;
7664 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7665 error = got_error_from_errno("asprintf");
7666 goto done;
7669 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7670 &base_branch_ref, worktree, refname, repo);
7671 if (error)
7672 goto done;
7674 refname = strdup(got_ref_get_name(branch_ref));
7675 if (refname == NULL) {
7676 error = got_error_from_errno("strdup");
7677 got_worktree_integrate_abort(worktree, fileindex, repo,
7678 branch_ref, base_branch_ref);
7679 goto done;
7681 base_refname = strdup(got_ref_get_name(base_branch_ref));
7682 if (base_refname == NULL) {
7683 error = got_error_from_errno("strdup");
7684 got_worktree_integrate_abort(worktree, fileindex, repo,
7685 branch_ref, base_branch_ref);
7686 goto done;
7689 error = got_ref_resolve(&commit_id, repo, branch_ref);
7690 if (error)
7691 goto done;
7693 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7694 if (error)
7695 goto done;
7697 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7698 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7699 "specified branch has already been integrated");
7700 got_worktree_integrate_abort(worktree, fileindex, repo,
7701 branch_ref, base_branch_ref);
7702 goto done;
7705 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7706 if (error) {
7707 if (error->code == GOT_ERR_ANCESTRY)
7708 error = got_error(GOT_ERR_REBASE_REQUIRED);
7709 got_worktree_integrate_abort(worktree, fileindex, repo,
7710 branch_ref, base_branch_ref);
7711 goto done;
7714 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7715 branch_ref, base_branch_ref, update_progress, &did_something,
7716 check_cancelled, NULL);
7717 if (error)
7718 goto done;
7720 printf("Integrated %s into %s\n", refname, base_refname);
7721 done:
7722 if (repo)
7723 got_repo_close(repo);
7724 if (worktree)
7725 got_worktree_close(worktree);
7726 free(cwd);
7727 free(base_commit_id);
7728 free(commit_id);
7729 free(refname);
7730 free(base_refname);
7731 return error;
7734 __dead static void
7735 usage_stage(void)
7737 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7738 "[file-path ...]\n",
7739 getprogname());
7740 exit(1);
7743 static const struct got_error *
7744 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7745 const char *path, struct got_object_id *blob_id,
7746 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7747 int dirfd, const char *de_name)
7749 const struct got_error *err = NULL;
7750 char *id_str = NULL;
7752 if (staged_status != GOT_STATUS_ADD &&
7753 staged_status != GOT_STATUS_MODIFY &&
7754 staged_status != GOT_STATUS_DELETE)
7755 return NULL;
7757 if (staged_status == GOT_STATUS_ADD ||
7758 staged_status == GOT_STATUS_MODIFY)
7759 err = got_object_id_str(&id_str, staged_blob_id);
7760 else
7761 err = got_object_id_str(&id_str, blob_id);
7762 if (err)
7763 return err;
7765 printf("%s %c %s\n", id_str, staged_status, path);
7766 free(id_str);
7767 return NULL;
7770 static const struct got_error *
7771 cmd_stage(int argc, char *argv[])
7773 const struct got_error *error = NULL;
7774 struct got_repository *repo = NULL;
7775 struct got_worktree *worktree = NULL;
7776 char *cwd = NULL;
7777 struct got_pathlist_head paths;
7778 struct got_pathlist_entry *pe;
7779 int ch, list_stage = 0, pflag = 0;
7780 FILE *patch_script_file = NULL;
7781 const char *patch_script_path = NULL;
7782 struct choose_patch_arg cpa;
7784 TAILQ_INIT(&paths);
7786 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7787 switch (ch) {
7788 case 'l':
7789 list_stage = 1;
7790 break;
7791 case 'p':
7792 pflag = 1;
7793 break;
7794 case 'F':
7795 patch_script_path = optarg;
7796 break;
7797 default:
7798 usage_stage();
7799 /* NOTREACHED */
7803 argc -= optind;
7804 argv += optind;
7806 #ifndef PROFILE
7807 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7808 "unveil", NULL) == -1)
7809 err(1, "pledge");
7810 #endif
7811 if (list_stage && (pflag || patch_script_path))
7812 errx(1, "-l option cannot be used with other options");
7813 if (patch_script_path && !pflag)
7814 errx(1, "-F option can only be used together with -p option");
7816 cwd = getcwd(NULL, 0);
7817 if (cwd == NULL) {
7818 error = got_error_from_errno("getcwd");
7819 goto done;
7822 error = got_worktree_open(&worktree, cwd);
7823 if (error)
7824 goto done;
7826 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7827 NULL);
7828 if (error != NULL)
7829 goto done;
7831 if (patch_script_path) {
7832 patch_script_file = fopen(patch_script_path, "r");
7833 if (patch_script_file == NULL) {
7834 error = got_error_from_errno2("fopen",
7835 patch_script_path);
7836 goto done;
7839 error = apply_unveil(got_repo_get_path(repo), 0,
7840 got_worktree_get_root_path(worktree));
7841 if (error)
7842 goto done;
7844 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7845 if (error)
7846 goto done;
7848 if (list_stage)
7849 error = got_worktree_status(worktree, &paths, repo,
7850 print_stage, NULL, check_cancelled, NULL);
7851 else {
7852 cpa.patch_script_file = patch_script_file;
7853 cpa.action = "stage";
7854 error = got_worktree_stage(worktree, &paths,
7855 pflag ? NULL : print_status, NULL,
7856 pflag ? choose_patch : NULL, &cpa, repo);
7858 done:
7859 if (patch_script_file && fclose(patch_script_file) == EOF &&
7860 error == NULL)
7861 error = got_error_from_errno2("fclose", patch_script_path);
7862 if (repo)
7863 got_repo_close(repo);
7864 if (worktree)
7865 got_worktree_close(worktree);
7866 TAILQ_FOREACH(pe, &paths, entry)
7867 free((char *)pe->path);
7868 got_pathlist_free(&paths);
7869 free(cwd);
7870 return error;
7873 __dead static void
7874 usage_unstage(void)
7876 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7877 "[file-path ...]\n",
7878 getprogname());
7879 exit(1);
7883 static const struct got_error *
7884 cmd_unstage(int argc, char *argv[])
7886 const struct got_error *error = NULL;
7887 struct got_repository *repo = NULL;
7888 struct got_worktree *worktree = NULL;
7889 char *cwd = NULL;
7890 struct got_pathlist_head paths;
7891 struct got_pathlist_entry *pe;
7892 int ch, did_something = 0, pflag = 0;
7893 FILE *patch_script_file = NULL;
7894 const char *patch_script_path = NULL;
7895 struct choose_patch_arg cpa;
7897 TAILQ_INIT(&paths);
7899 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7900 switch (ch) {
7901 case 'p':
7902 pflag = 1;
7903 break;
7904 case 'F':
7905 patch_script_path = optarg;
7906 break;
7907 default:
7908 usage_unstage();
7909 /* NOTREACHED */
7913 argc -= optind;
7914 argv += optind;
7916 #ifndef PROFILE
7917 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7918 "unveil", NULL) == -1)
7919 err(1, "pledge");
7920 #endif
7921 if (patch_script_path && !pflag)
7922 errx(1, "-F option can only be used together with -p option");
7924 cwd = getcwd(NULL, 0);
7925 if (cwd == NULL) {
7926 error = got_error_from_errno("getcwd");
7927 goto done;
7930 error = got_worktree_open(&worktree, cwd);
7931 if (error)
7932 goto done;
7934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7935 NULL);
7936 if (error != NULL)
7937 goto done;
7939 if (patch_script_path) {
7940 patch_script_file = fopen(patch_script_path, "r");
7941 if (patch_script_file == NULL) {
7942 error = got_error_from_errno2("fopen",
7943 patch_script_path);
7944 goto done;
7948 error = apply_unveil(got_repo_get_path(repo), 0,
7949 got_worktree_get_root_path(worktree));
7950 if (error)
7951 goto done;
7953 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7954 if (error)
7955 goto done;
7957 cpa.patch_script_file = patch_script_file;
7958 cpa.action = "unstage";
7959 error = got_worktree_unstage(worktree, &paths, update_progress,
7960 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7961 done:
7962 if (patch_script_file && fclose(patch_script_file) == EOF &&
7963 error == NULL)
7964 error = got_error_from_errno2("fclose", patch_script_path);
7965 if (repo)
7966 got_repo_close(repo);
7967 if (worktree)
7968 got_worktree_close(worktree);
7969 TAILQ_FOREACH(pe, &paths, entry)
7970 free((char *)pe->path);
7971 got_pathlist_free(&paths);
7972 free(cwd);
7973 return error;
7976 __dead static void
7977 usage_cat(void)
7979 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7980 "arg1 [arg2 ...]\n", getprogname());
7981 exit(1);
7984 static const struct got_error *
7985 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7987 const struct got_error *err;
7988 struct got_blob_object *blob;
7990 err = got_object_open_as_blob(&blob, repo, id, 8192);
7991 if (err)
7992 return err;
7994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7995 got_object_blob_close(blob);
7996 return err;
7999 static const struct got_error *
8000 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8002 const struct got_error *err;
8003 struct got_tree_object *tree;
8004 int nentries, i;
8006 err = got_object_open_as_tree(&tree, repo, id);
8007 if (err)
8008 return err;
8010 nentries = got_object_tree_get_nentries(tree);
8011 for (i = 0; i < nentries; i++) {
8012 struct got_tree_entry *te;
8013 char *id_str;
8014 if (sigint_received || sigpipe_received)
8015 break;
8016 te = got_object_tree_get_entry(tree, i);
8017 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8018 if (err)
8019 break;
8020 fprintf(outfile, "%s %.7o %s\n", id_str,
8021 got_tree_entry_get_mode(te),
8022 got_tree_entry_get_name(te));
8023 free(id_str);
8026 got_object_tree_close(tree);
8027 return err;
8030 static const struct got_error *
8031 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8033 const struct got_error *err;
8034 struct got_commit_object *commit;
8035 const struct got_object_id_queue *parent_ids;
8036 struct got_object_qid *pid;
8037 char *id_str = NULL;
8038 const char *logmsg = NULL;
8040 err = got_object_open_as_commit(&commit, repo, id);
8041 if (err)
8042 return err;
8044 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8045 if (err)
8046 goto done;
8048 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8049 parent_ids = got_object_commit_get_parent_ids(commit);
8050 fprintf(outfile, "numparents %d\n",
8051 got_object_commit_get_nparents(commit));
8052 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8053 char *pid_str;
8054 err = got_object_id_str(&pid_str, pid->id);
8055 if (err)
8056 goto done;
8057 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8058 free(pid_str);
8060 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8061 got_object_commit_get_author(commit),
8062 got_object_commit_get_author_time(commit));
8064 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8065 got_object_commit_get_author(commit),
8066 got_object_commit_get_committer_time(commit));
8068 logmsg = got_object_commit_get_logmsg_raw(commit);
8069 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8070 fprintf(outfile, "%s", logmsg);
8071 done:
8072 free(id_str);
8073 got_object_commit_close(commit);
8074 return err;
8077 static const struct got_error *
8078 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8080 const struct got_error *err;
8081 struct got_tag_object *tag;
8082 char *id_str = NULL;
8083 const char *tagmsg = NULL;
8085 err = got_object_open_as_tag(&tag, repo, id);
8086 if (err)
8087 return err;
8089 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8090 if (err)
8091 goto done;
8093 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8095 switch (got_object_tag_get_object_type(tag)) {
8096 case GOT_OBJ_TYPE_BLOB:
8097 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8098 GOT_OBJ_LABEL_BLOB);
8099 break;
8100 case GOT_OBJ_TYPE_TREE:
8101 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8102 GOT_OBJ_LABEL_TREE);
8103 break;
8104 case GOT_OBJ_TYPE_COMMIT:
8105 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8106 GOT_OBJ_LABEL_COMMIT);
8107 break;
8108 case GOT_OBJ_TYPE_TAG:
8109 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8110 GOT_OBJ_LABEL_TAG);
8111 break;
8112 default:
8113 break;
8116 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8117 got_object_tag_get_name(tag));
8119 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8120 got_object_tag_get_tagger(tag),
8121 got_object_tag_get_tagger_time(tag));
8123 tagmsg = got_object_tag_get_message(tag);
8124 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8125 fprintf(outfile, "%s", tagmsg);
8126 done:
8127 free(id_str);
8128 got_object_tag_close(tag);
8129 return err;
8132 static const struct got_error *
8133 cmd_cat(int argc, char *argv[])
8135 const struct got_error *error;
8136 struct got_repository *repo = NULL;
8137 struct got_worktree *worktree = NULL;
8138 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8139 const char *commit_id_str = NULL;
8140 struct got_object_id *id = NULL, *commit_id = NULL;
8141 int ch, obj_type, i, force_path = 0;
8143 #ifndef PROFILE
8144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8145 NULL) == -1)
8146 err(1, "pledge");
8147 #endif
8149 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8150 switch (ch) {
8151 case 'c':
8152 commit_id_str = optarg;
8153 break;
8154 case 'r':
8155 repo_path = realpath(optarg, NULL);
8156 if (repo_path == NULL)
8157 return got_error_from_errno2("realpath",
8158 optarg);
8159 got_path_strip_trailing_slashes(repo_path);
8160 break;
8161 case 'P':
8162 force_path = 1;
8163 break;
8164 default:
8165 usage_cat();
8166 /* NOTREACHED */
8170 argc -= optind;
8171 argv += optind;
8173 cwd = getcwd(NULL, 0);
8174 if (cwd == NULL) {
8175 error = got_error_from_errno("getcwd");
8176 goto done;
8178 error = got_worktree_open(&worktree, cwd);
8179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8180 goto done;
8181 if (worktree) {
8182 if (repo_path == NULL) {
8183 repo_path = strdup(
8184 got_worktree_get_repo_path(worktree));
8185 if (repo_path == NULL) {
8186 error = got_error_from_errno("strdup");
8187 goto done;
8192 if (repo_path == NULL) {
8193 repo_path = getcwd(NULL, 0);
8194 if (repo_path == NULL)
8195 return got_error_from_errno("getcwd");
8198 error = got_repo_open(&repo, repo_path, NULL);
8199 free(repo_path);
8200 if (error != NULL)
8201 goto done;
8203 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8204 if (error)
8205 goto done;
8207 if (commit_id_str == NULL)
8208 commit_id_str = GOT_REF_HEAD;
8209 error = got_repo_match_object_id(&commit_id, NULL,
8210 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8211 if (error)
8212 goto done;
8214 for (i = 0; i < argc; i++) {
8215 if (force_path) {
8216 error = got_object_id_by_path(&id, repo, commit_id,
8217 argv[i]);
8218 if (error)
8219 break;
8220 } else {
8221 error = got_repo_match_object_id(&id, &label, argv[i],
8222 GOT_OBJ_TYPE_ANY, 0, repo);
8223 if (error) {
8224 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8225 error->code != GOT_ERR_NOT_REF)
8226 break;
8227 error = got_object_id_by_path(&id, repo,
8228 commit_id, argv[i]);
8229 if (error)
8230 break;
8234 error = got_object_get_type(&obj_type, repo, id);
8235 if (error)
8236 break;
8238 switch (obj_type) {
8239 case GOT_OBJ_TYPE_BLOB:
8240 error = cat_blob(id, repo, stdout);
8241 break;
8242 case GOT_OBJ_TYPE_TREE:
8243 error = cat_tree(id, repo, stdout);
8244 break;
8245 case GOT_OBJ_TYPE_COMMIT:
8246 error = cat_commit(id, repo, stdout);
8247 break;
8248 case GOT_OBJ_TYPE_TAG:
8249 error = cat_tag(id, repo, stdout);
8250 break;
8251 default:
8252 error = got_error(GOT_ERR_OBJ_TYPE);
8253 break;
8255 if (error)
8256 break;
8257 free(label);
8258 label = NULL;
8259 free(id);
8260 id = NULL;
8262 done:
8263 free(label);
8264 free(id);
8265 free(commit_id);
8266 if (worktree)
8267 got_worktree_close(worktree);
8268 if (repo) {
8269 const struct got_error *repo_error;
8270 repo_error = got_repo_close(repo);
8271 if (error == NULL)
8272 error = repo_error;
8274 return error;