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>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_diff.h"
51 #include "got_commit_graph.h"
52 #include "got_lib_fetch.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_opentemp.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static volatile sig_atomic_t sigint_received;
62 static volatile sig_atomic_t sigpipe_received;
64 static void
65 catch_sigint(int signo)
66 {
67 sigint_received = 1;
68 }
70 static void
71 catch_sigpipe(int signo)
72 {
73 sigpipe_received = 1;
74 }
77 struct got_cmd {
78 const char *cmd_name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
82 };
84 __dead static void usage(int);
85 __dead static void usage_init(void);
86 __dead static void usage_import(void);
87 __dead static void usage_checkout(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_update(void);
90 __dead static void usage_log(void);
91 __dead static void usage_diff(void);
92 __dead static void usage_blame(void);
93 __dead static void usage_tree(void);
94 __dead static void usage_status(void);
95 __dead static void usage_ref(void);
96 __dead static void usage_branch(void);
97 __dead static void usage_tag(void);
98 __dead static void usage_add(void);
99 __dead static void usage_remove(void);
100 __dead static void usage_revert(void);
101 __dead static void usage_commit(void);
102 __dead static void usage_cherrypick(void);
103 __dead static void usage_backout(void);
104 __dead static void usage_rebase(void);
105 __dead static void usage_histedit(void);
106 __dead static void usage_integrate(void);
107 __dead static void usage_stage(void);
108 __dead static void usage_unstage(void);
109 __dead static void usage_cat(void);
111 static const struct got_error* cmd_init(int, char *[]);
112 static const struct got_error* cmd_import(int, char *[]);
113 static const struct got_error* cmd_clone(int, char *[]);
114 static const struct got_error* cmd_checkout(int, char *[]);
115 static const struct got_error* cmd_update(int, char *[]);
116 static const struct got_error* cmd_log(int, char *[]);
117 static const struct got_error* cmd_diff(int, char *[]);
118 static const struct got_error* cmd_blame(int, char *[]);
119 static const struct got_error* cmd_tree(int, char *[]);
120 static const struct got_error* cmd_status(int, char *[]);
121 static const struct got_error* cmd_ref(int, char *[]);
122 static const struct got_error* cmd_branch(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_revert(int, char *[]);
127 static const struct got_error* cmd_commit(int, char *[]);
128 static const struct got_error* cmd_cherrypick(int, char *[]);
129 static const struct got_error* cmd_backout(int, char *[]);
130 static const struct got_error* cmd_rebase(int, char *[]);
131 static const struct got_error* cmd_histedit(int, char *[]);
132 static const struct got_error* cmd_integrate(int, char *[]);
133 static const struct got_error* cmd_stage(int, char *[]);
134 static const struct got_error* cmd_unstage(int, char *[]);
135 static const struct got_error* cmd_cat(int, char *[]);
137 static struct got_cmd got_commands[] = {
138 { "init", cmd_init, usage_init, "in" },
139 { "import", cmd_import, usage_import, "im" },
140 { "checkout", cmd_checkout, usage_checkout, "co" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "update", cmd_update, usage_update, "up" },
143 { "log", cmd_log, usage_log, "" },
144 { "diff", cmd_diff, usage_diff, "di" },
145 { "blame", cmd_blame, usage_blame, "bl" },
146 { "tree", cmd_tree, usage_tree, "tr" },
147 { "status", cmd_status, usage_status, "st" },
148 { "ref", cmd_ref, usage_ref, "" },
149 { "branch", cmd_branch, usage_branch, "br" },
150 { "tag", cmd_tag, usage_tag, "" },
151 { "add", cmd_add, usage_add, "" },
152 { "remove", cmd_remove, usage_remove, "rm" },
153 { "revert", cmd_revert, usage_revert, "rv" },
154 { "commit", cmd_commit, usage_commit, "ci" },
155 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 { "backout", cmd_backout, usage_backout, "bo" },
157 { "rebase", cmd_rebase, usage_rebase, "rb" },
158 { "histedit", cmd_histedit, usage_histedit, "he" },
159 { "integrate", cmd_integrate, usage_integrate,"ig" },
160 { "stage", cmd_stage, usage_stage, "sg" },
161 { "unstage", cmd_unstage, usage_unstage, "ug" },
162 { "cat", cmd_cat, usage_cat, "" },
163 };
165 static void
166 list_commands(void)
168 int i;
170 fprintf(stderr, "commands:");
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct got_cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s", cmd->cmd_name);
175 fputc('\n', stderr);
178 int
179 main(int argc, char *argv[])
181 struct got_cmd *cmd;
182 unsigned int i;
183 int ch;
184 int hflag = 0, Vflag = 0;
185 static struct option longopts[] = {
186 { "version", no_argument, NULL, 'V' },
187 { NULL, 0, NULL, 0}
188 };
190 setlocale(LC_CTYPE, "");
192 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 switch (ch) {
194 case 'h':
195 hflag = 1;
196 break;
197 case 'V':
198 Vflag = 1;
199 break;
200 default:
201 usage(hflag);
202 /* NOTREACHED */
206 argc -= optind;
207 argv += optind;
208 optind = 0;
210 if (Vflag) {
211 got_version_print_str();
212 return 1;
215 if (argc <= 0)
216 usage(hflag);
218 signal(SIGINT, catch_sigint);
219 signal(SIGPIPE, catch_sigpipe);
221 for (i = 0; i < nitems(got_commands); i++) {
222 const struct got_error *error;
224 cmd = &got_commands[i];
226 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 strcmp(cmd->cmd_alias, argv[0]) != 0)
228 continue;
230 if (hflag)
231 got_commands[i].cmd_usage();
233 error = got_commands[i].cmd_main(argc, argv);
234 if (error && error->code != GOT_ERR_CANCELLED &&
235 error->code != GOT_ERR_PRIVSEP_EXIT &&
236 !(sigpipe_received &&
237 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 !(sigint_received &&
239 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 return 1;
244 return 0;
247 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 list_commands();
249 return 1;
252 __dead static void
253 usage(int hflag)
255 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 getprogname());
257 if (hflag)
258 list_commands();
259 exit(1);
262 static const struct got_error *
263 get_editor(char **abspath)
265 const struct got_error *err = NULL;
266 const char *editor;
268 *abspath = NULL;
270 editor = getenv("VISUAL");
271 if (editor == NULL)
272 editor = getenv("EDITOR");
274 if (editor) {
275 err = got_path_find_prog(abspath, editor);
276 if (err)
277 return err;
280 if (*abspath == NULL) {
281 *abspath = strdup("/bin/ed");
282 if (*abspath == NULL)
283 return got_error_from_errno("strdup");
286 return NULL;
289 static const struct got_error *
290 apply_unveil(const char *repo_path, int repo_read_only,
291 const char *worktree_path)
293 const struct got_error *err;
295 #ifdef PROFILE
296 if (unveil("gmon.out", "rwc") != 0)
297 return got_error_from_errno2("unveil", "gmon.out");
298 #endif
299 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 return got_error_from_errno2("unveil", repo_path);
302 if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 return got_error_from_errno2("unveil", worktree_path);
305 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 err = got_privsep_unveil_exec_helpers();
309 if (err != NULL)
310 return err;
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
315 return NULL;
318 __dead static void
319 usage_init(void)
321 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 exit(1);
325 static const struct got_error *
326 cmd_init(int argc, char *argv[])
328 const struct got_error *error = NULL;
329 char *repo_path = NULL;
330 int ch;
332 while ((ch = getopt(argc, argv, "")) != -1) {
333 switch (ch) {
334 default:
335 usage_init();
336 /* NOTREACHED */
340 argc -= optind;
341 argv += optind;
343 #ifndef PROFILE
344 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 err(1, "pledge");
346 #endif
347 if (argc != 1)
348 usage_init();
350 repo_path = strdup(argv[0]);
351 if (repo_path == NULL)
352 return got_error_from_errno("strdup");
354 got_path_strip_trailing_slashes(repo_path);
356 error = got_path_mkdir(repo_path);
357 if (error &&
358 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 goto done;
361 error = apply_unveil(repo_path, 0, NULL);
362 if (error)
363 goto done;
365 error = got_repo_init(repo_path);
366 done:
367 free(repo_path);
368 return error;
371 __dead static void
372 usage_import(void)
374 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 "[-r repository-path] [-I pattern] path\n", getprogname());
376 exit(1);
379 int
380 spawn_editor(const char *editor, const char *file)
382 pid_t pid;
383 sig_t sighup, sigint, sigquit;
384 int st = -1;
386 sighup = signal(SIGHUP, SIG_IGN);
387 sigint = signal(SIGINT, SIG_IGN);
388 sigquit = signal(SIGQUIT, SIG_IGN);
390 switch (pid = fork()) {
391 case -1:
392 goto doneediting;
393 case 0:
394 execl(editor, editor, file, (char *)NULL);
395 _exit(127);
398 while (waitpid(pid, &st, 0) == -1)
399 if (errno != EINTR)
400 break;
402 doneediting:
403 (void)signal(SIGHUP, sighup);
404 (void)signal(SIGINT, sigint);
405 (void)signal(SIGQUIT, sigquit);
407 if (!WIFEXITED(st)) {
408 errno = EINTR;
409 return -1;
412 return WEXITSTATUS(st);
415 static const struct got_error *
416 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 const char *initial_content)
419 const struct got_error *err = NULL;
420 char buf[1024];
421 struct stat st, st2;
422 FILE *fp;
423 int content_changed = 0;
424 size_t len;
426 *logmsg = NULL;
428 if (stat(logmsg_path, &st) == -1)
429 return got_error_from_errno2("stat", logmsg_path);
431 if (spawn_editor(editor, logmsg_path) == -1)
432 return got_error_from_errno("failed spawning editor");
434 if (stat(logmsg_path, &st2) == -1)
435 return got_error_from_errno("stat");
437 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "no changes made to commit message, aborting");
441 *logmsg = malloc(st2.st_size + 1);
442 if (*logmsg == NULL)
443 return got_error_from_errno("malloc");
444 (*logmsg)[0] = '\0';
445 len = 0;
447 fp = fopen(logmsg_path, "r");
448 if (fp == NULL) {
449 err = got_error_from_errno("fopen");
450 goto done;
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (!content_changed && strcmp(buf, initial_content) != 0)
454 content_changed = 1;
455 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 continue; /* remove comments and leading empty lines */
457 len = strlcat(*logmsg, buf, st2.st_size);
459 fclose(fp);
461 while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 (*logmsg)[len - 1] = '\0';
463 len--;
466 if (len == 0 || !content_changed)
467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 "commit message cannot be empty, aborting");
469 done:
470 if (err) {
471 free(*logmsg);
472 *logmsg = NULL;
474 return err;
477 static const struct got_error *
478 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 const char *path_dir, const char *branch_name)
481 char *initial_content = NULL;
482 const struct got_error *err = NULL;
483 int fd;
485 if (asprintf(&initial_content,
486 "\n# %s to be imported to branch %s\n", path_dir,
487 branch_name) == -1)
488 return got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(logmsg_path, &fd,
491 GOT_TMPDIR_STR "/got-importmsg");
492 if (err)
493 goto done;
495 dprintf(fd, initial_content);
496 close(fd);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 done:
500 free(initial_content);
501 return err;
504 static const struct got_error *
505 import_progress(void *arg, const char *path)
507 printf("A %s\n", path);
508 return NULL;
511 static const struct got_error *
512 get_author(char **author, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 const char *got_author, *name, *email;
517 *author = NULL;
519 name = got_repo_get_gitconfig_author_name(repo);
520 email = got_repo_get_gitconfig_author_email(repo);
521 if (name && email) {
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
524 return NULL;
527 got_author = getenv("GOT_AUTHOR");
528 if (got_author == NULL) {
529 name = got_repo_get_global_gitconfig_author_name(repo);
530 email = got_repo_get_global_gitconfig_author_email(repo);
531 if (name && email) {
532 if (asprintf(author, "%s <%s>", name, email) == -1)
533 return got_error_from_errno("asprintf");
534 return NULL;
536 /* TODO: Look up user in password database? */
537 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 *author = strdup(got_author);
541 if (*author == NULL)
542 return got_error_from_errno("strdup");
544 /*
545 * Really dumb email address check; we're only doing this to
546 * avoid git's object parser breaking on commits we create.
547 */
548 while (*got_author && *got_author != '<')
549 got_author++;
550 if (*got_author != '<') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 goto done;
554 while (*got_author && *got_author != '@')
555 got_author++;
556 if (*got_author != '@') {
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 goto done;
560 while (*got_author && *got_author != '>')
561 got_author++;
562 if (*got_author != '>')
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 done:
565 if (err) {
566 free(*author);
567 *author = NULL;
569 return err;
572 static const struct got_error *
573 get_gitconfig_path(char **gitconfig_path)
575 const char *homedir = getenv("HOME");
577 *gitconfig_path = NULL;
578 if (homedir) {
579 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 return got_error_from_errno("asprintf");
583 return NULL;
586 static const struct got_error *
587 cmd_import(int argc, char *argv[])
589 const struct got_error *error = NULL;
590 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 const char *branch_name = "main";
593 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 struct got_repository *repo = NULL;
595 struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 struct got_object_id *new_commit_id = NULL;
597 int ch;
598 struct got_pathlist_head ignores;
599 struct got_pathlist_entry *pe;
600 int preserve_logmsg = 0;
602 TAILQ_INIT(&ignores);
604 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 switch (ch) {
606 case 'b':
607 branch_name = optarg;
608 break;
609 case 'm':
610 logmsg = strdup(optarg);
611 if (logmsg == NULL) {
612 error = got_error_from_errno("strdup");
613 goto done;
615 break;
616 case 'r':
617 repo_path = realpath(optarg, NULL);
618 if (repo_path == NULL) {
619 error = got_error_from_errno2("realpath",
620 optarg);
621 goto done;
623 break;
624 case 'I':
625 if (optarg[0] == '\0')
626 break;
627 error = got_pathlist_insert(&pe, &ignores, optarg,
628 NULL);
629 if (error)
630 goto done;
631 break;
632 default:
633 usage_import();
634 /* NOTREACHED */
638 argc -= optind;
639 argv += optind;
641 #ifndef PROFILE
642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 "unveil",
644 NULL) == -1)
645 err(1, "pledge");
646 #endif
647 if (argc != 1)
648 usage_import();
650 if (repo_path == NULL) {
651 repo_path = getcwd(NULL, 0);
652 if (repo_path == NULL)
653 return got_error_from_errno("getcwd");
655 got_path_strip_trailing_slashes(repo_path);
656 error = get_gitconfig_path(&gitconfig_path);
657 if (error)
658 goto done;
659 error = got_repo_open(&repo, repo_path, gitconfig_path);
660 if (error)
661 goto done;
663 error = get_author(&author, repo);
664 if (error)
665 return error;
667 /*
668 * Don't let the user create a branch name with a leading '-'.
669 * While technically a valid reference name, this case is usually
670 * an unintended typo.
671 */
672 if (branch_name[0] == '-')
673 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 error = got_error_from_errno("asprintf");
677 goto done;
680 error = got_ref_open(&branch_ref, repo, refname, 0);
681 if (error) {
682 if (error->code != GOT_ERR_NOT_REF)
683 goto done;
684 } else {
685 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 "import target branch already exists");
687 goto done;
690 path_dir = realpath(argv[0], NULL);
691 if (path_dir == NULL) {
692 error = got_error_from_errno2("realpath", argv[0]);
693 goto done;
695 got_path_strip_trailing_slashes(path_dir);
697 /*
698 * unveil(2) traverses exec(2); if an editor is used we have
699 * to apply unveil after the log message has been written.
700 */
701 if (logmsg == NULL || strlen(logmsg) == 0) {
702 error = get_editor(&editor);
703 if (error)
704 goto done;
705 free(logmsg);
706 error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 path_dir, refname);
708 if (error) {
709 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 logmsg_path != NULL)
711 preserve_logmsg = 1;
712 goto done;
716 if (unveil(path_dir, "r") != 0) {
717 error = got_error_from_errno2("unveil", path_dir);
718 if (logmsg_path)
719 preserve_logmsg = 1;
720 goto done;
723 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 if (error) {
725 if (logmsg_path)
726 preserve_logmsg = 1;
727 goto done;
730 error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 author, &ignores, repo, import_progress, NULL);
732 if (error) {
733 if (logmsg_path)
734 preserve_logmsg = 1;
735 goto done;
738 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 if (error) {
740 if (logmsg_path)
741 preserve_logmsg = 1;
742 goto done;
745 error = got_ref_write(branch_ref, repo);
746 if (error) {
747 if (logmsg_path)
748 preserve_logmsg = 1;
749 goto done;
752 error = got_object_id_str(&id_str, new_commit_id);
753 if (error) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 if (error) {
761 if (error->code != GOT_ERR_NOT_REF) {
762 if (logmsg_path)
763 preserve_logmsg = 1;
764 goto done;
767 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 branch_ref);
769 if (error) {
770 if (logmsg_path)
771 preserve_logmsg = 1;
772 goto done;
775 error = got_ref_write(head_ref, repo);
776 if (error) {
777 if (logmsg_path)
778 preserve_logmsg = 1;
779 goto done;
783 printf("Created branch %s with commit %s\n",
784 got_ref_get_name(branch_ref), id_str);
785 done:
786 if (preserve_logmsg) {
787 fprintf(stderr, "%s: log message preserved in %s\n",
788 getprogname(), logmsg_path);
789 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 error = got_error_from_errno2("unlink", logmsg_path);
791 free(logmsg);
792 free(logmsg_path);
793 free(repo_path);
794 free(editor);
795 free(refname);
796 free(new_commit_id);
797 free(id_str);
798 free(author);
799 free(gitconfig_path);
800 if (branch_ref)
801 got_ref_close(branch_ref);
802 if (head_ref)
803 got_ref_close(head_ref);
804 return error;
807 __dead static void
808 usage_clone(void)
810 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 exit(1);
814 __dead static void
815 usage_checkout(void)
817 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 exit(1);
822 static void
823 show_worktree_base_ref_warning(void)
825 fprintf(stderr, "%s: warning: could not create a reference "
826 "to the work tree's base commit; the commit could be "
827 "garbage-collected by Git; making the repository "
828 "writable and running 'got update' will prevent this\n",
829 getprogname());
832 struct got_checkout_progress_arg {
833 const char *worktree_path;
834 int had_base_commit_ref_error;
835 };
837 static const struct got_error *
838 checkout_progress(void *arg, unsigned char status, const char *path)
840 struct got_checkout_progress_arg *a = arg;
842 /* Base commit bump happens silently. */
843 if (status == GOT_STATUS_BUMP_BASE)
844 return NULL;
846 if (status == GOT_STATUS_BASE_REF_ERR) {
847 a->had_base_commit_ref_error = 1;
848 return NULL;
851 while (path[0] == '/')
852 path++;
854 printf("%c %s/%s\n", status, a->worktree_path, path);
855 return NULL;
858 static const struct got_error *
859 check_cancelled(void *arg)
861 if (sigint_received || sigpipe_received)
862 return got_error(GOT_ERR_CANCELLED);
863 return NULL;
866 static const struct got_error *
867 check_linear_ancestry(struct got_object_id *commit_id,
868 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_object_id *yca_id;
874 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 commit_id, base_commit_id, repo, check_cancelled, NULL);
876 if (err)
877 return err;
879 if (yca_id == NULL)
880 return got_error(GOT_ERR_ANCESTRY);
882 /*
883 * Require a straight line of history between the target commit
884 * and the work tree's base commit.
886 * Non-linear situations such as this require a rebase:
888 * (commit) D F (base_commit)
889 * \ /
890 * C E
891 * \ /
892 * B (yca)
893 * |
894 * A
896 * 'got update' only handles linear cases:
897 * Update forwards in time: A (base/yca) - B - C - D (commit)
898 * Update backwards in time: D (base) - C - B - A (commit/yca)
899 */
900 if (allow_forwards_in_time_only) {
901 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 return got_error(GOT_ERR_ANCESTRY);
903 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 got_object_id_cmp(base_commit_id, yca_id) != 0)
905 return got_error(GOT_ERR_ANCESTRY);
907 free(yca_id);
908 return NULL;
911 static const struct got_error *
912 check_same_branch(struct got_object_id *commit_id,
913 struct got_reference *head_ref, struct got_object_id *yca_id,
914 struct got_repository *repo)
916 const struct got_error *err = NULL;
917 struct got_commit_graph *graph = NULL;
918 struct got_object_id *head_commit_id = NULL;
919 int is_same_branch = 0;
921 err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 if (err)
923 goto done;
925 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 is_same_branch = 1;
927 goto done;
929 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 is_same_branch = 1;
931 goto done;
934 err = got_commit_graph_open(&graph, "/", 1);
935 if (err)
936 goto done;
938 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 check_cancelled, NULL);
940 if (err)
941 goto done;
943 for (;;) {
944 struct got_object_id *id;
945 err = got_commit_graph_iter_next(&id, graph, repo,
946 check_cancelled, NULL);
947 if (err) {
948 if (err->code == GOT_ERR_ITER_COMPLETED)
949 err = NULL;
950 break;
953 if (id) {
954 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 break;
956 if (got_object_id_cmp(id, commit_id) == 0) {
957 is_same_branch = 1;
958 break;
962 done:
963 if (graph)
964 got_commit_graph_close(graph);
965 free(head_commit_id);
966 if (!err && !is_same_branch)
967 err = got_error(GOT_ERR_ANCESTRY);
968 return err;
971 static const struct got_error *
972 cmd_clone(int argc, char *argv[])
974 char *uri, *branch_filter, *dirname;
975 int ch;
977 while ((ch = getopt(argc, argv, "b:")) != -1) {
978 switch (ch) {
979 case 'b':
980 branch_filter = optarg;
981 break;
982 default:
983 usage_clone();
984 break;
987 argc -= optind;
988 argv += optind;
989 uri = argv[0];
990 if(argc == 1)
991 dirname = NULL;
992 else if(argc == 2)
993 dirname = argv[1];
994 else
995 usage_clone();
996 return got_clone(argv[0], branch_filter, dirname);
999 static const struct got_error *
1000 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1002 static char msg[512];
1003 const char *branch_name;
1005 if (got_ref_is_symbolic(ref))
1006 branch_name = got_ref_get_symref_target(ref);
1007 else
1008 branch_name = got_ref_get_name(ref);
1010 if (strncmp("refs/heads/", branch_name, 11) == 0)
1011 branch_name += 11;
1013 snprintf(msg, sizeof(msg),
1014 "target commit is not contained in branch '%s'; "
1015 "the branch to use must be specified with -b; "
1016 "if necessary a new branch can be created for "
1017 "this commit with 'got branch -c %s BRANCH_NAME'",
1018 branch_name, commit_id_str);
1020 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1023 static const struct got_error *
1024 cmd_checkout(int argc, char *argv[])
1026 const struct got_error *error = NULL;
1027 struct got_repository *repo = NULL;
1028 struct got_reference *head_ref = NULL;
1029 struct got_worktree *worktree = NULL;
1030 char *repo_path = NULL;
1031 char *worktree_path = NULL;
1032 const char *path_prefix = "";
1033 const char *branch_name = GOT_REF_HEAD;
1034 char *commit_id_str = NULL;
1035 int ch, same_path_prefix, allow_nonempty = 0;
1036 struct got_pathlist_head paths;
1037 struct got_checkout_progress_arg cpa;
1039 TAILQ_INIT(&paths);
1041 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1042 switch (ch) {
1043 case 'b':
1044 branch_name = optarg;
1045 break;
1046 case 'c':
1047 commit_id_str = strdup(optarg);
1048 if (commit_id_str == NULL)
1049 return got_error_from_errno("strdup");
1050 break;
1051 case 'E':
1052 allow_nonempty = 1;
1053 break;
1054 case 'p':
1055 path_prefix = optarg;
1056 break;
1057 default:
1058 usage_checkout();
1059 /* NOTREACHED */
1063 argc -= optind;
1064 argv += optind;
1066 #ifndef PROFILE
1067 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1068 "unveil", NULL) == -1)
1069 err(1, "pledge");
1070 #endif
1071 if (argc == 1) {
1072 char *cwd, *base, *dotgit;
1073 repo_path = realpath(argv[0], NULL);
1074 if (repo_path == NULL)
1075 return got_error_from_errno2("realpath", argv[0]);
1076 cwd = getcwd(NULL, 0);
1077 if (cwd == NULL) {
1078 error = got_error_from_errno("getcwd");
1079 goto done;
1081 if (path_prefix[0]) {
1082 base = basename(path_prefix);
1083 if (base == NULL) {
1084 error = got_error_from_errno2("basename",
1085 path_prefix);
1086 goto done;
1088 } else {
1089 base = basename(repo_path);
1090 if (base == NULL) {
1091 error = got_error_from_errno2("basename",
1092 repo_path);
1093 goto done;
1096 dotgit = strstr(base, ".git");
1097 if (dotgit)
1098 *dotgit = '\0';
1099 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1100 error = got_error_from_errno("asprintf");
1101 free(cwd);
1102 goto done;
1104 free(cwd);
1105 } else if (argc == 2) {
1106 repo_path = realpath(argv[0], NULL);
1107 if (repo_path == NULL) {
1108 error = got_error_from_errno2("realpath", argv[0]);
1109 goto done;
1111 worktree_path = realpath(argv[1], NULL);
1112 if (worktree_path == NULL) {
1113 if (errno != ENOENT) {
1114 error = got_error_from_errno2("realpath",
1115 argv[1]);
1116 goto done;
1118 worktree_path = strdup(argv[1]);
1119 if (worktree_path == NULL) {
1120 error = got_error_from_errno("strdup");
1121 goto done;
1124 } else
1125 usage_checkout();
1127 got_path_strip_trailing_slashes(repo_path);
1128 got_path_strip_trailing_slashes(worktree_path);
1130 error = got_repo_open(&repo, repo_path, NULL);
1131 if (error != NULL)
1132 goto done;
1134 /* Pre-create work tree path for unveil(2) */
1135 error = got_path_mkdir(worktree_path);
1136 if (error) {
1137 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1138 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1139 goto done;
1140 if (!allow_nonempty &&
1141 !got_path_dir_is_empty(worktree_path)) {
1142 error = got_error_path(worktree_path,
1143 GOT_ERR_DIR_NOT_EMPTY);
1144 goto done;
1148 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1149 if (error)
1150 goto done;
1152 error = got_ref_open(&head_ref, repo, branch_name, 0);
1153 if (error != NULL)
1154 goto done;
1156 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1157 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1158 goto done;
1160 error = got_worktree_open(&worktree, worktree_path);
1161 if (error != NULL)
1162 goto done;
1164 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1165 path_prefix);
1166 if (error != NULL)
1167 goto done;
1168 if (!same_path_prefix) {
1169 error = got_error(GOT_ERR_PATH_PREFIX);
1170 goto done;
1173 if (commit_id_str) {
1174 struct got_object_id *commit_id;
1175 error = got_repo_match_object_id(&commit_id, NULL,
1176 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1177 if (error)
1178 goto done;
1179 error = check_linear_ancestry(commit_id,
1180 got_worktree_get_base_commit_id(worktree), 0, repo);
1181 if (error != NULL) {
1182 free(commit_id);
1183 if (error->code == GOT_ERR_ANCESTRY) {
1184 error = checkout_ancestry_error(
1185 head_ref, commit_id_str);
1187 goto done;
1189 error = check_same_branch(commit_id, head_ref, NULL, repo);
1190 if (error) {
1191 if (error->code == GOT_ERR_ANCESTRY) {
1192 error = checkout_ancestry_error(
1193 head_ref, commit_id_str);
1195 goto done;
1197 error = got_worktree_set_base_commit_id(worktree, repo,
1198 commit_id);
1199 free(commit_id);
1200 if (error)
1201 goto done;
1204 error = got_pathlist_append(&paths, "", NULL);
1205 if (error)
1206 goto done;
1207 cpa.worktree_path = worktree_path;
1208 cpa.had_base_commit_ref_error = 0;
1209 error = got_worktree_checkout_files(worktree, &paths, repo,
1210 checkout_progress, &cpa, check_cancelled, NULL);
1211 if (error != NULL)
1212 goto done;
1214 printf("Now shut up and hack\n");
1215 if (cpa.had_base_commit_ref_error)
1216 show_worktree_base_ref_warning();
1217 done:
1218 got_pathlist_free(&paths);
1219 free(commit_id_str);
1220 free(repo_path);
1221 free(worktree_path);
1222 return error;
1225 __dead static void
1226 usage_update(void)
1228 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1229 getprogname());
1230 exit(1);
1233 static const struct got_error *
1234 update_progress(void *arg, unsigned char status, const char *path)
1236 int *did_something = arg;
1238 if (status == GOT_STATUS_EXISTS ||
1239 status == GOT_STATUS_BASE_REF_ERR)
1240 return NULL;
1242 *did_something = 1;
1244 /* Base commit bump happens silently. */
1245 if (status == GOT_STATUS_BUMP_BASE)
1246 return NULL;
1248 while (path[0] == '/')
1249 path++;
1250 printf("%c %s\n", status, path);
1251 return NULL;
1254 static const struct got_error *
1255 switch_head_ref(struct got_reference *head_ref,
1256 struct got_object_id *commit_id, struct got_worktree *worktree,
1257 struct got_repository *repo)
1259 const struct got_error *err = NULL;
1260 char *base_id_str;
1261 int ref_has_moved = 0;
1263 /* Trivial case: switching between two different references. */
1264 if (strcmp(got_ref_get_name(head_ref),
1265 got_worktree_get_head_ref_name(worktree)) != 0) {
1266 printf("Switching work tree from %s to %s\n",
1267 got_worktree_get_head_ref_name(worktree),
1268 got_ref_get_name(head_ref));
1269 return got_worktree_set_head_ref(worktree, head_ref);
1272 err = check_linear_ancestry(commit_id,
1273 got_worktree_get_base_commit_id(worktree), 0, repo);
1274 if (err) {
1275 if (err->code != GOT_ERR_ANCESTRY)
1276 return err;
1277 ref_has_moved = 1;
1279 if (!ref_has_moved)
1280 return NULL;
1282 /* Switching to a rebased branch with the same reference name. */
1283 err = got_object_id_str(&base_id_str,
1284 got_worktree_get_base_commit_id(worktree));
1285 if (err)
1286 return err;
1287 printf("Reference %s now points at a different branch\n",
1288 got_worktree_get_head_ref_name(worktree));
1289 printf("Switching work tree from %s to %s\n", base_id_str,
1290 got_worktree_get_head_ref_name(worktree));
1291 return NULL;
1294 static const struct got_error *
1295 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1297 const struct got_error *err;
1298 int in_progress;
1300 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1301 if (err)
1302 return err;
1303 if (in_progress)
1304 return got_error(GOT_ERR_REBASING);
1306 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1307 if (err)
1308 return err;
1309 if (in_progress)
1310 return got_error(GOT_ERR_HISTEDIT_BUSY);
1312 return NULL;
1315 static const struct got_error *
1316 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1317 char *argv[], struct got_worktree *worktree)
1319 const struct got_error *err = NULL;
1320 char *path;
1321 int i;
1323 if (argc == 0) {
1324 path = strdup("");
1325 if (path == NULL)
1326 return got_error_from_errno("strdup");
1327 return got_pathlist_append(paths, path, NULL);
1330 for (i = 0; i < argc; i++) {
1331 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1332 if (err)
1333 break;
1334 err = got_pathlist_append(paths, path, NULL);
1335 if (err) {
1336 free(path);
1337 break;
1341 return err;
1344 static const struct got_error *
1345 cmd_update(int argc, char *argv[])
1347 const struct got_error *error = NULL;
1348 struct got_repository *repo = NULL;
1349 struct got_worktree *worktree = NULL;
1350 char *worktree_path = NULL;
1351 struct got_object_id *commit_id = NULL;
1352 char *commit_id_str = NULL;
1353 const char *branch_name = NULL;
1354 struct got_reference *head_ref = NULL;
1355 struct got_pathlist_head paths;
1356 struct got_pathlist_entry *pe;
1357 int ch, did_something = 0;
1359 TAILQ_INIT(&paths);
1361 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1362 switch (ch) {
1363 case 'b':
1364 branch_name = optarg;
1365 break;
1366 case 'c':
1367 commit_id_str = strdup(optarg);
1368 if (commit_id_str == NULL)
1369 return got_error_from_errno("strdup");
1370 break;
1371 default:
1372 usage_update();
1373 /* NOTREACHED */
1377 argc -= optind;
1378 argv += optind;
1380 #ifndef PROFILE
1381 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1382 "unveil", NULL) == -1)
1383 err(1, "pledge");
1384 #endif
1385 worktree_path = getcwd(NULL, 0);
1386 if (worktree_path == NULL) {
1387 error = got_error_from_errno("getcwd");
1388 goto done;
1390 error = got_worktree_open(&worktree, worktree_path);
1391 if (error)
1392 goto done;
1394 error = check_rebase_or_histedit_in_progress(worktree);
1395 if (error)
1396 goto done;
1398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1399 NULL);
1400 if (error != NULL)
1401 goto done;
1403 error = apply_unveil(got_repo_get_path(repo), 0,
1404 got_worktree_get_root_path(worktree));
1405 if (error)
1406 goto done;
1408 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1409 if (error)
1410 goto done;
1412 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1413 got_worktree_get_head_ref_name(worktree), 0);
1414 if (error != NULL)
1415 goto done;
1416 if (commit_id_str == NULL) {
1417 error = got_ref_resolve(&commit_id, repo, head_ref);
1418 if (error != NULL)
1419 goto done;
1420 error = got_object_id_str(&commit_id_str, commit_id);
1421 if (error != NULL)
1422 goto done;
1423 } else {
1424 error = got_repo_match_object_id(&commit_id, NULL,
1425 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1426 free(commit_id_str);
1427 commit_id_str = NULL;
1428 if (error)
1429 goto done;
1430 error = got_object_id_str(&commit_id_str, commit_id);
1431 if (error)
1432 goto done;
1435 if (branch_name) {
1436 struct got_object_id *head_commit_id;
1437 TAILQ_FOREACH(pe, &paths, entry) {
1438 if (pe->path_len == 0)
1439 continue;
1440 error = got_error_msg(GOT_ERR_BAD_PATH,
1441 "switching between branches requires that "
1442 "the entire work tree gets updated");
1443 goto done;
1445 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1446 if (error)
1447 goto done;
1448 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1449 repo);
1450 free(head_commit_id);
1451 if (error != NULL)
1452 goto done;
1453 error = check_same_branch(commit_id, head_ref, NULL, repo);
1454 if (error)
1455 goto done;
1456 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1457 if (error)
1458 goto done;
1459 } else {
1460 error = check_linear_ancestry(commit_id,
1461 got_worktree_get_base_commit_id(worktree), 0, repo);
1462 if (error != NULL) {
1463 if (error->code == GOT_ERR_ANCESTRY)
1464 error = got_error(GOT_ERR_BRANCH_MOVED);
1465 goto done;
1467 error = check_same_branch(commit_id, head_ref, NULL, repo);
1468 if (error)
1469 goto done;
1472 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1473 commit_id) != 0) {
1474 error = got_worktree_set_base_commit_id(worktree, repo,
1475 commit_id);
1476 if (error)
1477 goto done;
1480 error = got_worktree_checkout_files(worktree, &paths, repo,
1481 update_progress, &did_something, check_cancelled, NULL);
1482 if (error != NULL)
1483 goto done;
1485 if (did_something)
1486 printf("Updated to commit %s\n", commit_id_str);
1487 else
1488 printf("Already up-to-date\n");
1489 done:
1490 free(worktree_path);
1491 TAILQ_FOREACH(pe, &paths, entry)
1492 free((char *)pe->path);
1493 got_pathlist_free(&paths);
1494 free(commit_id);
1495 free(commit_id_str);
1496 return error;
1499 static const struct got_error *
1500 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1501 const char *path, int diff_context, int ignore_whitespace,
1502 struct got_repository *repo)
1504 const struct got_error *err = NULL;
1505 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1507 if (blob_id1) {
1508 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1509 if (err)
1510 goto done;
1513 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1514 if (err)
1515 goto done;
1517 while (path[0] == '/')
1518 path++;
1519 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1520 ignore_whitespace, stdout);
1521 done:
1522 if (blob1)
1523 got_object_blob_close(blob1);
1524 got_object_blob_close(blob2);
1525 return err;
1528 static const struct got_error *
1529 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1530 const char *path, int diff_context, int ignore_whitespace,
1531 struct got_repository *repo)
1533 const struct got_error *err = NULL;
1534 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1535 struct got_diff_blob_output_unidiff_arg arg;
1537 if (tree_id1) {
1538 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1539 if (err)
1540 goto done;
1543 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1544 if (err)
1545 goto done;
1547 arg.diff_context = diff_context;
1548 arg.ignore_whitespace = ignore_whitespace;
1549 arg.outfile = stdout;
1550 while (path[0] == '/')
1551 path++;
1552 err = got_diff_tree(tree1, tree2, path, path, repo,
1553 got_diff_blob_output_unidiff, &arg, 1);
1554 done:
1555 if (tree1)
1556 got_object_tree_close(tree1);
1557 if (tree2)
1558 got_object_tree_close(tree2);
1559 return err;
1562 static const struct got_error *
1563 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1564 const char *path, int diff_context, struct got_repository *repo)
1566 const struct got_error *err = NULL;
1567 struct got_commit_object *pcommit = NULL;
1568 char *id_str1 = NULL, *id_str2 = NULL;
1569 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1570 struct got_object_qid *qid;
1572 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1573 if (qid != NULL) {
1574 err = got_object_open_as_commit(&pcommit, repo,
1575 qid->id);
1576 if (err)
1577 return err;
1580 if (path && path[0] != '\0') {
1581 int obj_type;
1582 err = got_object_id_by_path(&obj_id2, repo, id, path);
1583 if (err)
1584 goto done;
1585 err = got_object_id_str(&id_str2, obj_id2);
1586 if (err) {
1587 free(obj_id2);
1588 goto done;
1590 if (pcommit) {
1591 err = got_object_id_by_path(&obj_id1, repo,
1592 qid->id, path);
1593 if (err) {
1594 free(obj_id2);
1595 goto done;
1597 err = got_object_id_str(&id_str1, obj_id1);
1598 if (err) {
1599 free(obj_id2);
1600 goto done;
1603 err = got_object_get_type(&obj_type, repo, obj_id2);
1604 if (err) {
1605 free(obj_id2);
1606 goto done;
1608 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1609 switch (obj_type) {
1610 case GOT_OBJ_TYPE_BLOB:
1611 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1612 0, repo);
1613 break;
1614 case GOT_OBJ_TYPE_TREE:
1615 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1616 0, repo);
1617 break;
1618 default:
1619 err = got_error(GOT_ERR_OBJ_TYPE);
1620 break;
1622 free(obj_id1);
1623 free(obj_id2);
1624 } else {
1625 obj_id2 = got_object_commit_get_tree_id(commit);
1626 err = got_object_id_str(&id_str2, obj_id2);
1627 if (err)
1628 goto done;
1629 obj_id1 = got_object_commit_get_tree_id(pcommit);
1630 err = got_object_id_str(&id_str1, obj_id1);
1631 if (err)
1632 goto done;
1633 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1634 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1636 done:
1637 free(id_str1);
1638 free(id_str2);
1639 if (pcommit)
1640 got_object_commit_close(pcommit);
1641 return err;
1644 static char *
1645 get_datestr(time_t *time, char *datebuf)
1647 struct tm mytm, *tm;
1648 char *p, *s;
1650 tm = gmtime_r(time, &mytm);
1651 if (tm == NULL)
1652 return NULL;
1653 s = asctime_r(tm, datebuf);
1654 if (s == NULL)
1655 return NULL;
1656 p = strchr(s, '\n');
1657 if (p)
1658 *p = '\0';
1659 return s;
1662 static const struct got_error *
1663 match_logmsg(int *have_match, struct got_object_id *id,
1664 struct got_commit_object *commit, regex_t *regex)
1666 const struct got_error *err = NULL;
1667 regmatch_t regmatch;
1668 char *id_str = NULL, *logmsg = NULL;
1670 *have_match = 0;
1672 err = got_object_id_str(&id_str, id);
1673 if (err)
1674 return err;
1676 err = got_object_commit_get_logmsg(&logmsg, commit);
1677 if (err)
1678 goto done;
1680 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1681 *have_match = 1;
1682 done:
1683 free(id_str);
1684 free(logmsg);
1685 return err;
1688 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1690 static const struct got_error *
1691 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1692 struct got_repository *repo, const char *path, int show_patch,
1693 int diff_context, struct got_reflist_head *refs)
1695 const struct got_error *err = NULL;
1696 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1697 char datebuf[26];
1698 time_t committer_time;
1699 const char *author, *committer;
1700 char *refs_str = NULL;
1701 struct got_reflist_entry *re;
1703 SIMPLEQ_FOREACH(re, refs, entry) {
1704 char *s;
1705 const char *name;
1706 struct got_tag_object *tag = NULL;
1707 int cmp;
1709 name = got_ref_get_name(re->ref);
1710 if (strcmp(name, GOT_REF_HEAD) == 0)
1711 continue;
1712 if (strncmp(name, "refs/", 5) == 0)
1713 name += 5;
1714 if (strncmp(name, "got/", 4) == 0)
1715 continue;
1716 if (strncmp(name, "heads/", 6) == 0)
1717 name += 6;
1718 if (strncmp(name, "remotes/", 8) == 0)
1719 name += 8;
1720 if (strncmp(name, "tags/", 5) == 0) {
1721 err = got_object_open_as_tag(&tag, repo, re->id);
1722 if (err) {
1723 if (err->code != GOT_ERR_OBJ_TYPE)
1724 return err;
1725 /* Ref points at something other than a tag. */
1726 err = NULL;
1727 tag = NULL;
1730 cmp = got_object_id_cmp(tag ?
1731 got_object_tag_get_object_id(tag) : re->id, id);
1732 if (tag)
1733 got_object_tag_close(tag);
1734 if (cmp != 0)
1735 continue;
1736 s = refs_str;
1737 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1738 name) == -1) {
1739 err = got_error_from_errno("asprintf");
1740 free(s);
1741 return err;
1743 free(s);
1745 err = got_object_id_str(&id_str, id);
1746 if (err)
1747 return err;
1749 printf(GOT_COMMIT_SEP_STR);
1750 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1751 refs_str ? refs_str : "", refs_str ? ")" : "");
1752 free(id_str);
1753 id_str = NULL;
1754 free(refs_str);
1755 refs_str = NULL;
1756 printf("from: %s\n", got_object_commit_get_author(commit));
1757 committer_time = got_object_commit_get_committer_time(commit);
1758 datestr = get_datestr(&committer_time, datebuf);
1759 if (datestr)
1760 printf("date: %s UTC\n", datestr);
1761 author = got_object_commit_get_author(commit);
1762 committer = got_object_commit_get_committer(commit);
1763 if (strcmp(author, committer) != 0)
1764 printf("via: %s\n", committer);
1765 if (got_object_commit_get_nparents(commit) > 1) {
1766 const struct got_object_id_queue *parent_ids;
1767 struct got_object_qid *qid;
1768 int n = 1;
1769 parent_ids = got_object_commit_get_parent_ids(commit);
1770 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1771 err = got_object_id_str(&id_str, qid->id);
1772 if (err)
1773 return err;
1774 printf("parent %d: %s\n", n++, id_str);
1775 free(id_str);
1779 err = got_object_commit_get_logmsg(&logmsg0, commit);
1780 if (err)
1781 return err;
1783 logmsg = logmsg0;
1784 do {
1785 line = strsep(&logmsg, "\n");
1786 if (line)
1787 printf(" %s\n", line);
1788 } while (line);
1789 free(logmsg0);
1791 if (show_patch) {
1792 err = print_patch(commit, id, path, diff_context, repo);
1793 if (err == 0)
1794 printf("\n");
1797 if (fflush(stdout) != 0 && err == NULL)
1798 err = got_error_from_errno("fflush");
1799 return err;
1802 static const struct got_error *
1803 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1804 const char *path, int show_patch, const char *search_pattern,
1805 int diff_context, int limit, int log_branches,
1806 struct got_reflist_head *refs)
1808 const struct got_error *err;
1809 struct got_commit_graph *graph;
1810 regex_t regex;
1811 int have_match;
1813 if (search_pattern &&
1814 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1815 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1817 err = got_commit_graph_open(&graph, path, !log_branches);
1818 if (err)
1819 return err;
1820 err = got_commit_graph_iter_start(graph, root_id, repo,
1821 check_cancelled, NULL);
1822 if (err)
1823 goto done;
1824 for (;;) {
1825 struct got_commit_object *commit;
1826 struct got_object_id *id;
1828 if (sigint_received || sigpipe_received)
1829 break;
1831 err = got_commit_graph_iter_next(&id, graph, repo,
1832 check_cancelled, NULL);
1833 if (err) {
1834 if (err->code == GOT_ERR_ITER_COMPLETED)
1835 err = NULL;
1836 break;
1838 if (id == NULL)
1839 break;
1841 err = got_object_open_as_commit(&commit, repo, id);
1842 if (err)
1843 break;
1845 if (search_pattern) {
1846 err = match_logmsg(&have_match, id, commit, &regex);
1847 if (err) {
1848 got_object_commit_close(commit);
1849 break;
1851 if (have_match == 0) {
1852 got_object_commit_close(commit);
1853 continue;
1857 err = print_commit(commit, id, repo, path, show_patch,
1858 diff_context, refs);
1859 got_object_commit_close(commit);
1860 if (err || (limit && --limit == 0))
1861 break;
1863 done:
1864 if (search_pattern)
1865 regfree(&regex);
1866 got_commit_graph_close(graph);
1867 return err;
1870 __dead static void
1871 usage_log(void)
1873 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1874 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1875 exit(1);
1878 static int
1879 get_default_log_limit(void)
1881 const char *got_default_log_limit;
1882 long long n;
1883 const char *errstr;
1885 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1886 if (got_default_log_limit == NULL)
1887 return 0;
1888 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1889 if (errstr != NULL)
1890 return 0;
1891 return n;
1894 static const struct got_error *
1895 cmd_log(int argc, char *argv[])
1897 const struct got_error *error;
1898 struct got_repository *repo = NULL;
1899 struct got_worktree *worktree = NULL;
1900 struct got_commit_object *commit = NULL;
1901 struct got_object_id *id = NULL;
1902 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1903 const char *start_commit = NULL, *search_pattern = NULL;
1904 int diff_context = -1, ch;
1905 int show_patch = 0, limit = 0, log_branches = 0;
1906 const char *errstr;
1907 struct got_reflist_head refs;
1909 SIMPLEQ_INIT(&refs);
1911 #ifndef PROFILE
1912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1913 NULL)
1914 == -1)
1915 err(1, "pledge");
1916 #endif
1918 limit = get_default_log_limit();
1920 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1921 switch (ch) {
1922 case 'p':
1923 show_patch = 1;
1924 break;
1925 case 'c':
1926 start_commit = optarg;
1927 break;
1928 case 'C':
1929 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1930 &errstr);
1931 if (errstr != NULL)
1932 err(1, "-C option %s", errstr);
1933 break;
1934 case 'l':
1935 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1936 if (errstr != NULL)
1937 err(1, "-l option %s", errstr);
1938 break;
1939 case 'b':
1940 log_branches = 1;
1941 break;
1942 case 'r':
1943 repo_path = realpath(optarg, NULL);
1944 if (repo_path == NULL)
1945 return got_error_from_errno2("realpath",
1946 optarg);
1947 got_path_strip_trailing_slashes(repo_path);
1948 break;
1949 case 's':
1950 search_pattern = optarg;
1951 break;
1952 default:
1953 usage_log();
1954 /* NOTREACHED */
1958 argc -= optind;
1959 argv += optind;
1961 if (diff_context == -1)
1962 diff_context = 3;
1963 else if (!show_patch)
1964 errx(1, "-C reguires -p");
1966 cwd = getcwd(NULL, 0);
1967 if (cwd == NULL) {
1968 error = got_error_from_errno("getcwd");
1969 goto done;
1972 error = got_worktree_open(&worktree, cwd);
1973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1974 goto done;
1975 error = NULL;
1977 if (argc == 0) {
1978 path = strdup("");
1979 if (path == NULL) {
1980 error = got_error_from_errno("strdup");
1981 goto done;
1983 } else if (argc == 1) {
1984 if (worktree) {
1985 error = got_worktree_resolve_path(&path, worktree,
1986 argv[0]);
1987 if (error)
1988 goto done;
1989 } else {
1990 path = strdup(argv[0]);
1991 if (path == NULL) {
1992 error = got_error_from_errno("strdup");
1993 goto done;
1996 } else
1997 usage_log();
1999 if (repo_path == NULL) {
2000 repo_path = worktree ?
2001 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2003 if (repo_path == NULL) {
2004 error = got_error_from_errno("strdup");
2005 goto done;
2008 error = got_repo_open(&repo, repo_path, NULL);
2009 if (error != NULL)
2010 goto done;
2012 error = apply_unveil(got_repo_get_path(repo), 1,
2013 worktree ? got_worktree_get_root_path(worktree) : NULL);
2014 if (error)
2015 goto done;
2017 if (start_commit == NULL) {
2018 struct got_reference *head_ref;
2019 error = got_ref_open(&head_ref, repo,
2020 worktree ? got_worktree_get_head_ref_name(worktree)
2021 : GOT_REF_HEAD, 0);
2022 if (error != NULL)
2023 return error;
2024 error = got_ref_resolve(&id, repo, head_ref);
2025 got_ref_close(head_ref);
2026 if (error != NULL)
2027 return error;
2028 error = got_object_open_as_commit(&commit, repo, id);
2029 } else {
2030 struct got_reference *ref;
2031 error = got_ref_open(&ref, repo, start_commit, 0);
2032 if (error == NULL) {
2033 int obj_type;
2034 error = got_ref_resolve(&id, repo, ref);
2035 got_ref_close(ref);
2036 if (error != NULL)
2037 goto done;
2038 error = got_object_get_type(&obj_type, repo, id);
2039 if (error != NULL)
2040 goto done;
2041 if (obj_type == GOT_OBJ_TYPE_TAG) {
2042 struct got_tag_object *tag;
2043 error = got_object_open_as_tag(&tag, repo, id);
2044 if (error != NULL)
2045 goto done;
2046 if (got_object_tag_get_object_type(tag) !=
2047 GOT_OBJ_TYPE_COMMIT) {
2048 got_object_tag_close(tag);
2049 error = got_error(GOT_ERR_OBJ_TYPE);
2050 goto done;
2052 free(id);
2053 id = got_object_id_dup(
2054 got_object_tag_get_object_id(tag));
2055 if (id == NULL)
2056 error = got_error_from_errno(
2057 "got_object_id_dup");
2058 got_object_tag_close(tag);
2059 if (error)
2060 goto done;
2061 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2062 error = got_error(GOT_ERR_OBJ_TYPE);
2063 goto done;
2065 error = got_object_open_as_commit(&commit, repo, id);
2066 if (error != NULL)
2067 goto done;
2069 if (commit == NULL) {
2070 error = got_repo_match_object_id_prefix(&id,
2071 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2072 if (error != NULL)
2073 return error;
2076 if (error != NULL)
2077 goto done;
2079 if (worktree) {
2080 const char *prefix = got_worktree_get_path_prefix(worktree);
2081 char *p;
2082 if (asprintf(&p, "%s%s%s", prefix,
2083 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2084 error = got_error_from_errno("asprintf");
2085 goto done;
2087 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2088 free(p);
2089 } else
2090 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2091 if (error != NULL)
2092 goto done;
2093 if (in_repo_path) {
2094 free(path);
2095 path = in_repo_path;
2098 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2099 if (error)
2100 goto done;
2102 error = print_commits(id, repo, path, show_patch, search_pattern,
2103 diff_context, limit, log_branches, &refs);
2104 done:
2105 free(path);
2106 free(repo_path);
2107 free(cwd);
2108 free(id);
2109 if (worktree)
2110 got_worktree_close(worktree);
2111 if (repo) {
2112 const struct got_error *repo_error;
2113 repo_error = got_repo_close(repo);
2114 if (error == NULL)
2115 error = repo_error;
2117 got_ref_list_free(&refs);
2118 return error;
2121 __dead static void
2122 usage_diff(void)
2124 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2125 "[-w] [object1 object2 | path]\n", getprogname());
2126 exit(1);
2129 struct print_diff_arg {
2130 struct got_repository *repo;
2131 struct got_worktree *worktree;
2132 int diff_context;
2133 const char *id_str;
2134 int header_shown;
2135 int diff_staged;
2136 int ignore_whitespace;
2139 static const struct got_error *
2140 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2141 const char *path, struct got_object_id *blob_id,
2142 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2143 int dirfd, const char *de_name)
2145 struct print_diff_arg *a = arg;
2146 const struct got_error *err = NULL;
2147 struct got_blob_object *blob1 = NULL;
2148 int fd = -1;
2149 FILE *f2 = NULL;
2150 char *abspath = NULL, *label1 = NULL;
2151 struct stat sb;
2153 if (a->diff_staged) {
2154 if (staged_status != GOT_STATUS_MODIFY &&
2155 staged_status != GOT_STATUS_ADD &&
2156 staged_status != GOT_STATUS_DELETE)
2157 return NULL;
2158 } else {
2159 if (staged_status == GOT_STATUS_DELETE)
2160 return NULL;
2161 if (status == GOT_STATUS_NONEXISTENT)
2162 return got_error_set_errno(ENOENT, path);
2163 if (status != GOT_STATUS_MODIFY &&
2164 status != GOT_STATUS_ADD &&
2165 status != GOT_STATUS_DELETE &&
2166 status != GOT_STATUS_CONFLICT)
2167 return NULL;
2170 if (!a->header_shown) {
2171 printf("diff %s %s%s\n", a->id_str,
2172 got_worktree_get_root_path(a->worktree),
2173 a->diff_staged ? " (staged changes)" : "");
2174 a->header_shown = 1;
2177 if (a->diff_staged) {
2178 const char *label1 = NULL, *label2 = NULL;
2179 switch (staged_status) {
2180 case GOT_STATUS_MODIFY:
2181 label1 = path;
2182 label2 = path;
2183 break;
2184 case GOT_STATUS_ADD:
2185 label2 = path;
2186 break;
2187 case GOT_STATUS_DELETE:
2188 label1 = path;
2189 break;
2190 default:
2191 return got_error(GOT_ERR_FILE_STATUS);
2193 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2194 label1, label2, a->diff_context, a->ignore_whitespace,
2195 a->repo, stdout);
2198 if (staged_status == GOT_STATUS_ADD ||
2199 staged_status == GOT_STATUS_MODIFY) {
2200 char *id_str;
2201 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2202 8192);
2203 if (err)
2204 goto done;
2205 err = got_object_id_str(&id_str, staged_blob_id);
2206 if (err)
2207 goto done;
2208 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2209 err = got_error_from_errno("asprintf");
2210 free(id_str);
2211 goto done;
2213 free(id_str);
2214 } else if (status != GOT_STATUS_ADD) {
2215 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2216 if (err)
2217 goto done;
2220 if (status != GOT_STATUS_DELETE) {
2221 if (asprintf(&abspath, "%s/%s",
2222 got_worktree_get_root_path(a->worktree), path) == -1) {
2223 err = got_error_from_errno("asprintf");
2224 goto done;
2227 if (dirfd != -1) {
2228 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2229 if (fd == -1) {
2230 err = got_error_from_errno2("openat", abspath);
2231 goto done;
2233 } else {
2234 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2235 if (fd == -1) {
2236 err = got_error_from_errno2("open", abspath);
2237 goto done;
2240 if (fstat(fd, &sb) == -1) {
2241 err = got_error_from_errno2("fstat", abspath);
2242 goto done;
2244 f2 = fdopen(fd, "r");
2245 if (f2 == NULL) {
2246 err = got_error_from_errno2("fdopen", abspath);
2247 goto done;
2249 fd = -1;
2250 } else
2251 sb.st_size = 0;
2253 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2254 a->diff_context, a->ignore_whitespace, stdout);
2255 done:
2256 if (blob1)
2257 got_object_blob_close(blob1);
2258 if (f2 && fclose(f2) == EOF && err == NULL)
2259 err = got_error_from_errno("fclose");
2260 if (fd != -1 && close(fd) == -1 && err == NULL)
2261 err = got_error_from_errno("close");
2262 free(abspath);
2263 return err;
2266 static const struct got_error *
2267 cmd_diff(int argc, char *argv[])
2269 const struct got_error *error;
2270 struct got_repository *repo = NULL;
2271 struct got_worktree *worktree = NULL;
2272 char *cwd = NULL, *repo_path = NULL;
2273 struct got_object_id *id1 = NULL, *id2 = NULL;
2274 const char *id_str1 = NULL, *id_str2 = NULL;
2275 char *label1 = NULL, *label2 = NULL;
2276 int type1, type2;
2277 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2278 const char *errstr;
2279 char *path = NULL;
2281 #ifndef PROFILE
2282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2283 NULL) == -1)
2284 err(1, "pledge");
2285 #endif
2287 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2288 switch (ch) {
2289 case 'C':
2290 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2291 &errstr);
2292 if (errstr != NULL)
2293 err(1, "-C option %s", errstr);
2294 break;
2295 case 'r':
2296 repo_path = realpath(optarg, NULL);
2297 if (repo_path == NULL)
2298 return got_error_from_errno2("realpath",
2299 optarg);
2300 got_path_strip_trailing_slashes(repo_path);
2301 break;
2302 case 's':
2303 diff_staged = 1;
2304 break;
2305 case 'w':
2306 ignore_whitespace = 1;
2307 break;
2308 default:
2309 usage_diff();
2310 /* NOTREACHED */
2314 argc -= optind;
2315 argv += optind;
2317 cwd = getcwd(NULL, 0);
2318 if (cwd == NULL) {
2319 error = got_error_from_errno("getcwd");
2320 goto done;
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2324 goto done;
2325 if (argc <= 1) {
2326 if (worktree == NULL) {
2327 error = got_error(GOT_ERR_NOT_WORKTREE);
2328 goto done;
2330 if (repo_path)
2331 errx(1,
2332 "-r option can't be used when diffing a work tree");
2333 repo_path = strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL) {
2335 error = got_error_from_errno("strdup");
2336 goto done;
2338 if (argc == 1) {
2339 error = got_worktree_resolve_path(&path, worktree,
2340 argv[0]);
2341 if (error)
2342 goto done;
2343 } else {
2344 path = strdup("");
2345 if (path == NULL) {
2346 error = got_error_from_errno("strdup");
2347 goto done;
2350 } else if (argc == 2) {
2351 if (diff_staged)
2352 errx(1, "-s option can't be used when diffing "
2353 "objects in repository");
2354 id_str1 = argv[0];
2355 id_str2 = argv[1];
2356 if (worktree && repo_path == NULL) {
2357 repo_path =
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2361 goto done;
2364 } else
2365 usage_diff();
2367 if (repo_path == NULL) {
2368 repo_path = getcwd(NULL, 0);
2369 if (repo_path == NULL)
2370 return got_error_from_errno("getcwd");
2373 error = got_repo_open(&repo, repo_path, NULL);
2374 free(repo_path);
2375 if (error != NULL)
2376 goto done;
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 worktree ? got_worktree_get_root_path(worktree) : NULL);
2380 if (error)
2381 goto done;
2383 if (argc <= 1) {
2384 struct print_diff_arg arg;
2385 struct got_pathlist_head paths;
2386 char *id_str;
2388 TAILQ_INIT(&paths);
2390 error = got_object_id_str(&id_str,
2391 got_worktree_get_base_commit_id(worktree));
2392 if (error)
2393 goto done;
2394 arg.repo = repo;
2395 arg.worktree = worktree;
2396 arg.diff_context = diff_context;
2397 arg.id_str = id_str;
2398 arg.header_shown = 0;
2399 arg.diff_staged = diff_staged;
2400 arg.ignore_whitespace = ignore_whitespace;
2402 error = got_pathlist_append(&paths, path, NULL);
2403 if (error)
2404 goto done;
2406 error = got_worktree_status(worktree, &paths, repo, print_diff,
2407 &arg, check_cancelled, NULL);
2408 free(id_str);
2409 got_pathlist_free(&paths);
2410 goto done;
2413 error = got_repo_match_object_id(&id1, &label1, id_str1,
2414 GOT_OBJ_TYPE_ANY, 1, repo);
2415 if (error)
2416 goto done;
2418 error = got_repo_match_object_id(&id2, &label2, id_str2,
2419 GOT_OBJ_TYPE_ANY, 1, repo);
2420 if (error)
2421 goto done;
2423 error = got_object_get_type(&type1, repo, id1);
2424 if (error)
2425 goto done;
2427 error = got_object_get_type(&type2, repo, id2);
2428 if (error)
2429 goto done;
2431 if (type1 != type2) {
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2433 goto done;
2436 switch (type1) {
2437 case GOT_OBJ_TYPE_BLOB:
2438 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2439 diff_context, ignore_whitespace, repo, stdout);
2440 break;
2441 case GOT_OBJ_TYPE_TREE:
2442 error = got_diff_objects_as_trees(id1, id2, "", "",
2443 diff_context, ignore_whitespace, repo, stdout);
2444 break;
2445 case GOT_OBJ_TYPE_COMMIT:
2446 printf("diff %s %s\n", label1, label2);
2447 error = got_diff_objects_as_commits(id1, id2, diff_context,
2448 ignore_whitespace, repo, stdout);
2449 break;
2450 default:
2451 error = got_error(GOT_ERR_OBJ_TYPE);
2453 done:
2454 free(label1);
2455 free(label2);
2456 free(id1);
2457 free(id2);
2458 free(path);
2459 if (worktree)
2460 got_worktree_close(worktree);
2461 if (repo) {
2462 const struct got_error *repo_error;
2463 repo_error = got_repo_close(repo);
2464 if (error == NULL)
2465 error = repo_error;
2467 return error;
2470 __dead static void
2471 usage_blame(void)
2473 fprintf(stderr,
2474 "usage: %s blame [-c commit] [-r repository-path] path\n",
2475 getprogname());
2476 exit(1);
2479 struct blame_line {
2480 int annotated;
2481 char *id_str;
2482 char *committer;
2483 char datebuf[11]; /* YYYY-MM-DD + NUL */
2486 struct blame_cb_args {
2487 struct blame_line *lines;
2488 int nlines;
2489 int nlines_prec;
2490 int lineno_cur;
2491 off_t *line_offsets;
2492 FILE *f;
2493 struct got_repository *repo;
2496 static const struct got_error *
2497 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2499 const struct got_error *err = NULL;
2500 struct blame_cb_args *a = arg;
2501 struct blame_line *bline;
2502 char *line = NULL;
2503 size_t linesize = 0;
2504 struct got_commit_object *commit = NULL;
2505 off_t offset;
2506 struct tm tm;
2507 time_t committer_time;
2509 if (nlines != a->nlines ||
2510 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2511 return got_error(GOT_ERR_RANGE);
2513 if (sigint_received)
2514 return got_error(GOT_ERR_ITER_COMPLETED);
2516 if (lineno == -1)
2517 return NULL; /* no change in this commit */
2519 /* Annotate this line. */
2520 bline = &a->lines[lineno - 1];
2521 if (bline->annotated)
2522 return NULL;
2523 err = got_object_id_str(&bline->id_str, id);
2524 if (err)
2525 return err;
2527 err = got_object_open_as_commit(&commit, a->repo, id);
2528 if (err)
2529 goto done;
2531 bline->committer = strdup(got_object_commit_get_committer(commit));
2532 if (bline->committer == NULL) {
2533 err = got_error_from_errno("strdup");
2534 goto done;
2537 committer_time = got_object_commit_get_committer_time(commit);
2538 if (localtime_r(&committer_time, &tm) == NULL)
2539 return got_error_from_errno("localtime_r");
2540 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2541 &tm) >= sizeof(bline->datebuf)) {
2542 err = got_error(GOT_ERR_NO_SPACE);
2543 goto done;
2545 bline->annotated = 1;
2547 /* Print lines annotated so far. */
2548 bline = &a->lines[a->lineno_cur - 1];
2549 if (!bline->annotated)
2550 goto done;
2552 offset = a->line_offsets[a->lineno_cur - 1];
2553 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2554 err = got_error_from_errno("fseeko");
2555 goto done;
2558 while (bline->annotated) {
2559 char *smallerthan, *at, *nl, *committer;
2560 size_t len;
2562 if (getline(&line, &linesize, a->f) == -1) {
2563 if (ferror(a->f))
2564 err = got_error_from_errno("getline");
2565 break;
2568 committer = bline->committer;
2569 smallerthan = strchr(committer, '<');
2570 if (smallerthan && smallerthan[1] != '\0')
2571 committer = smallerthan + 1;
2572 at = strchr(committer, '@');
2573 if (at)
2574 *at = '\0';
2575 len = strlen(committer);
2576 if (len >= 9)
2577 committer[8] = '\0';
2579 nl = strchr(line, '\n');
2580 if (nl)
2581 *nl = '\0';
2582 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2583 bline->id_str, bline->datebuf, committer, line);
2585 a->lineno_cur++;
2586 bline = &a->lines[a->lineno_cur - 1];
2588 done:
2589 if (commit)
2590 got_object_commit_close(commit);
2591 free(line);
2592 return err;
2595 static const struct got_error *
2596 cmd_blame(int argc, char *argv[])
2598 const struct got_error *error;
2599 struct got_repository *repo = NULL;
2600 struct got_worktree *worktree = NULL;
2601 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2602 struct got_object_id *obj_id = NULL;
2603 struct got_object_id *commit_id = NULL;
2604 struct got_blob_object *blob = NULL;
2605 char *commit_id_str = NULL;
2606 struct blame_cb_args bca;
2607 int ch, obj_type, i;
2608 size_t filesize;
2610 memset(&bca, 0, sizeof(bca));
2612 #ifndef PROFILE
2613 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2614 NULL) == -1)
2615 err(1, "pledge");
2616 #endif
2618 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2619 switch (ch) {
2620 case 'c':
2621 commit_id_str = optarg;
2622 break;
2623 case 'r':
2624 repo_path = realpath(optarg, NULL);
2625 if (repo_path == NULL)
2626 return got_error_from_errno2("realpath",
2627 optarg);
2628 got_path_strip_trailing_slashes(repo_path);
2629 break;
2630 default:
2631 usage_blame();
2632 /* NOTREACHED */
2636 argc -= optind;
2637 argv += optind;
2639 if (argc == 1)
2640 path = argv[0];
2641 else
2642 usage_blame();
2644 cwd = getcwd(NULL, 0);
2645 if (cwd == NULL) {
2646 error = got_error_from_errno("getcwd");
2647 goto done;
2649 if (repo_path == NULL) {
2650 error = got_worktree_open(&worktree, cwd);
2651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2652 goto done;
2653 else
2654 error = NULL;
2655 if (worktree) {
2656 repo_path =
2657 strdup(got_worktree_get_repo_path(worktree));
2658 if (repo_path == NULL)
2659 error = got_error_from_errno("strdup");
2660 if (error)
2661 goto done;
2662 } else {
2663 repo_path = strdup(cwd);
2664 if (repo_path == NULL) {
2665 error = got_error_from_errno("strdup");
2666 goto done;
2671 error = got_repo_open(&repo, repo_path, NULL);
2672 if (error != NULL)
2673 goto done;
2675 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2676 if (error)
2677 goto done;
2679 if (worktree) {
2680 const char *prefix = got_worktree_get_path_prefix(worktree);
2681 char *p, *worktree_subdir = cwd +
2682 strlen(got_worktree_get_root_path(worktree));
2683 if (asprintf(&p, "%s%s%s%s%s",
2684 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2685 worktree_subdir, worktree_subdir[0] ? "/" : "",
2686 path) == -1) {
2687 error = got_error_from_errno("asprintf");
2688 goto done;
2690 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2691 free(p);
2692 } else {
2693 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2695 if (error)
2696 goto done;
2698 if (commit_id_str == NULL) {
2699 struct got_reference *head_ref;
2700 error = got_ref_open(&head_ref, repo, worktree ?
2701 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2702 if (error != NULL)
2703 goto done;
2704 error = got_ref_resolve(&commit_id, repo, head_ref);
2705 got_ref_close(head_ref);
2706 if (error != NULL)
2707 goto done;
2708 } else {
2709 error = got_repo_match_object_id(&commit_id, NULL,
2710 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2711 if (error)
2712 goto done;
2715 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2716 if (error)
2717 goto done;
2719 error = got_object_get_type(&obj_type, repo, obj_id);
2720 if (error)
2721 goto done;
2723 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2724 error = got_error(GOT_ERR_OBJ_TYPE);
2725 goto done;
2728 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2729 if (error)
2730 goto done;
2731 bca.f = got_opentemp();
2732 if (bca.f == NULL) {
2733 error = got_error_from_errno("got_opentemp");
2734 goto done;
2736 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2737 &bca.line_offsets, bca.f, blob);
2738 if (error || bca.nlines == 0)
2739 goto done;
2741 /* Don't include \n at EOF in the blame line count. */
2742 if (bca.line_offsets[bca.nlines - 1] == filesize)
2743 bca.nlines--;
2745 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2746 if (bca.lines == NULL) {
2747 error = got_error_from_errno("calloc");
2748 goto done;
2750 bca.lineno_cur = 1;
2751 bca.nlines_prec = 0;
2752 i = bca.nlines;
2753 while (i > 0) {
2754 i /= 10;
2755 bca.nlines_prec++;
2757 bca.repo = repo;
2759 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2760 check_cancelled, NULL);
2761 done:
2762 free(in_repo_path);
2763 free(repo_path);
2764 free(cwd);
2765 free(commit_id);
2766 free(obj_id);
2767 if (blob)
2768 got_object_blob_close(blob);
2769 if (worktree)
2770 got_worktree_close(worktree);
2771 if (repo) {
2772 const struct got_error *repo_error;
2773 repo_error = got_repo_close(repo);
2774 if (error == NULL)
2775 error = repo_error;
2777 if (bca.lines) {
2778 for (i = 0; i < bca.nlines; i++) {
2779 struct blame_line *bline = &bca.lines[i];
2780 free(bline->id_str);
2781 free(bline->committer);
2783 free(bca.lines);
2785 free(bca.line_offsets);
2786 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2787 error = got_error_from_errno("fclose");
2788 return error;
2791 __dead static void
2792 usage_tree(void)
2794 fprintf(stderr,
2795 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2796 getprogname());
2797 exit(1);
2800 static void
2801 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2802 const char *root_path)
2804 int is_root_path = (strcmp(path, root_path) == 0);
2805 const char *modestr = "";
2806 mode_t mode = got_tree_entry_get_mode(te);
2808 path += strlen(root_path);
2809 while (path[0] == '/')
2810 path++;
2812 if (got_object_tree_entry_is_submodule(te))
2813 modestr = "$";
2814 else if (S_ISLNK(mode))
2815 modestr = "@";
2816 else if (S_ISDIR(mode))
2817 modestr = "/";
2818 else if (mode & S_IXUSR)
2819 modestr = "*";
2821 printf("%s%s%s%s%s\n", id ? id : "", path,
2822 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2825 static const struct got_error *
2826 print_tree(const char *path, struct got_object_id *commit_id,
2827 int show_ids, int recurse, const char *root_path,
2828 struct got_repository *repo)
2830 const struct got_error *err = NULL;
2831 struct got_object_id *tree_id = NULL;
2832 struct got_tree_object *tree = NULL;
2833 int nentries, i;
2835 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2836 if (err)
2837 goto done;
2839 err = got_object_open_as_tree(&tree, repo, tree_id);
2840 if (err)
2841 goto done;
2842 nentries = got_object_tree_get_nentries(tree);
2843 for (i = 0; i < nentries; i++) {
2844 struct got_tree_entry *te;
2845 char *id = NULL;
2847 if (sigint_received || sigpipe_received)
2848 break;
2850 te = got_object_tree_get_entry(tree, i);
2851 if (show_ids) {
2852 char *id_str;
2853 err = got_object_id_str(&id_str,
2854 got_tree_entry_get_id(te));
2855 if (err)
2856 goto done;
2857 if (asprintf(&id, "%s ", id_str) == -1) {
2858 err = got_error_from_errno("asprintf");
2859 free(id_str);
2860 goto done;
2862 free(id_str);
2864 print_entry(te, id, path, root_path);
2865 free(id);
2867 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2868 char *child_path;
2869 if (asprintf(&child_path, "%s%s%s", path,
2870 path[0] == '/' && path[1] == '\0' ? "" : "/",
2871 got_tree_entry_get_name(te)) == -1) {
2872 err = got_error_from_errno("asprintf");
2873 goto done;
2875 err = print_tree(child_path, commit_id, show_ids, 1,
2876 root_path, repo);
2877 free(child_path);
2878 if (err)
2879 goto done;
2882 done:
2883 if (tree)
2884 got_object_tree_close(tree);
2885 free(tree_id);
2886 return err;
2889 static const struct got_error *
2890 cmd_tree(int argc, char *argv[])
2892 const struct got_error *error;
2893 struct got_repository *repo = NULL;
2894 struct got_worktree *worktree = NULL;
2895 const char *path;
2896 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2897 struct got_object_id *commit_id = NULL;
2898 char *commit_id_str = NULL;
2899 int show_ids = 0, recurse = 0;
2900 int ch;
2902 #ifndef PROFILE
2903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2904 NULL) == -1)
2905 err(1, "pledge");
2906 #endif
2908 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2909 switch (ch) {
2910 case 'c':
2911 commit_id_str = optarg;
2912 break;
2913 case 'r':
2914 repo_path = realpath(optarg, NULL);
2915 if (repo_path == NULL)
2916 return got_error_from_errno2("realpath",
2917 optarg);
2918 got_path_strip_trailing_slashes(repo_path);
2919 break;
2920 case 'i':
2921 show_ids = 1;
2922 break;
2923 case 'R':
2924 recurse = 1;
2925 break;
2926 default:
2927 usage_tree();
2928 /* NOTREACHED */
2932 argc -= optind;
2933 argv += optind;
2935 if (argc == 1)
2936 path = argv[0];
2937 else if (argc > 1)
2938 usage_tree();
2939 else
2940 path = NULL;
2942 cwd = getcwd(NULL, 0);
2943 if (cwd == NULL) {
2944 error = got_error_from_errno("getcwd");
2945 goto done;
2947 if (repo_path == NULL) {
2948 error = got_worktree_open(&worktree, cwd);
2949 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2950 goto done;
2951 else
2952 error = NULL;
2953 if (worktree) {
2954 repo_path =
2955 strdup(got_worktree_get_repo_path(worktree));
2956 if (repo_path == NULL)
2957 error = got_error_from_errno("strdup");
2958 if (error)
2959 goto done;
2960 } else {
2961 repo_path = strdup(cwd);
2962 if (repo_path == NULL) {
2963 error = got_error_from_errno("strdup");
2964 goto done;
2969 error = got_repo_open(&repo, repo_path, NULL);
2970 if (error != NULL)
2971 goto done;
2973 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2974 if (error)
2975 goto done;
2977 if (path == NULL) {
2978 if (worktree) {
2979 char *p, *worktree_subdir = cwd +
2980 strlen(got_worktree_get_root_path(worktree));
2981 if (asprintf(&p, "%s/%s",
2982 got_worktree_get_path_prefix(worktree),
2983 worktree_subdir) == -1) {
2984 error = got_error_from_errno("asprintf");
2985 goto done;
2987 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2988 free(p);
2989 if (error)
2990 goto done;
2991 } else
2992 path = "/";
2994 if (in_repo_path == NULL) {
2995 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2996 if (error != NULL)
2997 goto done;
3000 if (commit_id_str == NULL) {
3001 struct got_reference *head_ref;
3002 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3003 if (error != NULL)
3004 goto done;
3005 error = got_ref_resolve(&commit_id, repo, head_ref);
3006 got_ref_close(head_ref);
3007 if (error != NULL)
3008 goto done;
3009 } else {
3010 error = got_repo_match_object_id(&commit_id, NULL,
3011 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3012 if (error)
3013 goto done;
3016 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3017 in_repo_path, repo);
3018 done:
3019 free(in_repo_path);
3020 free(repo_path);
3021 free(cwd);
3022 free(commit_id);
3023 if (worktree)
3024 got_worktree_close(worktree);
3025 if (repo) {
3026 const struct got_error *repo_error;
3027 repo_error = got_repo_close(repo);
3028 if (error == NULL)
3029 error = repo_error;
3031 return error;
3034 __dead static void
3035 usage_status(void)
3037 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3038 exit(1);
3041 static const struct got_error *
3042 print_status(void *arg, unsigned char status, unsigned char staged_status,
3043 const char *path, struct got_object_id *blob_id,
3044 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3045 int dirfd, const char *de_name)
3047 if (status == staged_status && (status == GOT_STATUS_DELETE))
3048 status = GOT_STATUS_NO_CHANGE;
3049 printf("%c%c %s\n", status, staged_status, path);
3050 return NULL;
3053 static const struct got_error *
3054 cmd_status(int argc, char *argv[])
3056 const struct got_error *error = NULL;
3057 struct got_repository *repo = NULL;
3058 struct got_worktree *worktree = NULL;
3059 char *cwd = NULL;
3060 struct got_pathlist_head paths;
3061 struct got_pathlist_entry *pe;
3062 int ch;
3064 TAILQ_INIT(&paths);
3066 while ((ch = getopt(argc, argv, "")) != -1) {
3067 switch (ch) {
3068 default:
3069 usage_status();
3070 /* NOTREACHED */
3074 argc -= optind;
3075 argv += optind;
3077 #ifndef PROFILE
3078 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3079 NULL) == -1)
3080 err(1, "pledge");
3081 #endif
3082 cwd = getcwd(NULL, 0);
3083 if (cwd == NULL) {
3084 error = got_error_from_errno("getcwd");
3085 goto done;
3088 error = got_worktree_open(&worktree, cwd);
3089 if (error != NULL)
3090 goto done;
3092 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3093 NULL);
3094 if (error != NULL)
3095 goto done;
3097 error = apply_unveil(got_repo_get_path(repo), 1,
3098 got_worktree_get_root_path(worktree));
3099 if (error)
3100 goto done;
3102 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3103 if (error)
3104 goto done;
3106 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3107 check_cancelled, NULL);
3108 done:
3109 TAILQ_FOREACH(pe, &paths, entry)
3110 free((char *)pe->path);
3111 got_pathlist_free(&paths);
3112 free(cwd);
3113 return error;
3116 __dead static void
3117 usage_ref(void)
3119 fprintf(stderr,
3120 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3121 getprogname());
3122 exit(1);
3125 static const struct got_error *
3126 list_refs(struct got_repository *repo)
3128 static const struct got_error *err = NULL;
3129 struct got_reflist_head refs;
3130 struct got_reflist_entry *re;
3132 SIMPLEQ_INIT(&refs);
3133 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3134 if (err)
3135 return err;
3137 SIMPLEQ_FOREACH(re, &refs, entry) {
3138 char *refstr;
3139 refstr = got_ref_to_str(re->ref);
3140 if (refstr == NULL)
3141 return got_error_from_errno("got_ref_to_str");
3142 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3143 free(refstr);
3146 got_ref_list_free(&refs);
3147 return NULL;
3150 static const struct got_error *
3151 delete_ref(struct got_repository *repo, const char *refname)
3153 const struct got_error *err = NULL;
3154 struct got_reference *ref;
3156 err = got_ref_open(&ref, repo, refname, 0);
3157 if (err)
3158 return err;
3160 err = got_ref_delete(ref, repo);
3161 got_ref_close(ref);
3162 return err;
3165 static const struct got_error *
3166 add_ref(struct got_repository *repo, const char *refname, const char *target)
3168 const struct got_error *err = NULL;
3169 struct got_object_id *id;
3170 struct got_reference *ref = NULL;
3173 * Don't let the user create a reference name with a leading '-'.
3174 * While technically a valid reference name, this case is usually
3175 * an unintended typo.
3177 if (refname[0] == '-')
3178 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3180 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3181 repo);
3182 if (err) {
3183 struct got_reference *target_ref;
3185 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3186 return err;
3187 err = got_ref_open(&target_ref, repo, target, 0);
3188 if (err)
3189 return err;
3190 err = got_ref_resolve(&id, repo, target_ref);
3191 got_ref_close(target_ref);
3192 if (err)
3193 return err;
3196 err = got_ref_alloc(&ref, refname, id);
3197 if (err)
3198 goto done;
3200 err = got_ref_write(ref, repo);
3201 done:
3202 if (ref)
3203 got_ref_close(ref);
3204 free(id);
3205 return err;
3208 static const struct got_error *
3209 add_symref(struct got_repository *repo, const char *refname, const char *target)
3211 const struct got_error *err = NULL;
3212 struct got_reference *ref = NULL;
3213 struct got_reference *target_ref = NULL;
3216 * Don't let the user create a reference name with a leading '-'.
3217 * While technically a valid reference name, this case is usually
3218 * an unintended typo.
3220 if (refname[0] == '-')
3221 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3223 err = got_ref_open(&target_ref, repo, target, 0);
3224 if (err)
3225 return err;
3227 err = got_ref_alloc_symref(&ref, refname, target_ref);
3228 if (err)
3229 goto done;
3231 err = got_ref_write(ref, repo);
3232 done:
3233 if (target_ref)
3234 got_ref_close(target_ref);
3235 if (ref)
3236 got_ref_close(ref);
3237 return err;
3240 static const struct got_error *
3241 cmd_ref(int argc, char *argv[])
3243 const struct got_error *error = NULL;
3244 struct got_repository *repo = NULL;
3245 struct got_worktree *worktree = NULL;
3246 char *cwd = NULL, *repo_path = NULL;
3247 int ch, do_list = 0, create_symref = 0;
3248 const char *delref = NULL;
3250 /* TODO: Add -s option for adding symbolic references. */
3251 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3252 switch (ch) {
3253 case 'd':
3254 delref = optarg;
3255 break;
3256 case 'r':
3257 repo_path = realpath(optarg, NULL);
3258 if (repo_path == NULL)
3259 return got_error_from_errno2("realpath",
3260 optarg);
3261 got_path_strip_trailing_slashes(repo_path);
3262 break;
3263 case 'l':
3264 do_list = 1;
3265 break;
3266 case 's':
3267 create_symref = 1;
3268 break;
3269 default:
3270 usage_ref();
3271 /* NOTREACHED */
3275 if (do_list && delref)
3276 errx(1, "-l and -d options are mutually exclusive\n");
3278 argc -= optind;
3279 argv += optind;
3281 if (do_list || delref) {
3282 if (create_symref)
3283 errx(1, "-s option cannot be used together with the "
3284 "-l or -d options");
3285 if (argc > 0)
3286 usage_ref();
3287 } else if (argc != 2)
3288 usage_ref();
3290 #ifndef PROFILE
3291 if (do_list) {
3292 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3293 NULL) == -1)
3294 err(1, "pledge");
3295 } else {
3296 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3297 "sendfd unveil", NULL) == -1)
3298 err(1, "pledge");
3300 #endif
3301 cwd = getcwd(NULL, 0);
3302 if (cwd == NULL) {
3303 error = got_error_from_errno("getcwd");
3304 goto done;
3307 if (repo_path == NULL) {
3308 error = got_worktree_open(&worktree, cwd);
3309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3310 goto done;
3311 else
3312 error = NULL;
3313 if (worktree) {
3314 repo_path =
3315 strdup(got_worktree_get_repo_path(worktree));
3316 if (repo_path == NULL)
3317 error = got_error_from_errno("strdup");
3318 if (error)
3319 goto done;
3320 } else {
3321 repo_path = strdup(cwd);
3322 if (repo_path == NULL) {
3323 error = got_error_from_errno("strdup");
3324 goto done;
3329 error = got_repo_open(&repo, repo_path, NULL);
3330 if (error != NULL)
3331 goto done;
3333 error = apply_unveil(got_repo_get_path(repo), do_list,
3334 worktree ? got_worktree_get_root_path(worktree) : NULL);
3335 if (error)
3336 goto done;
3338 if (do_list)
3339 error = list_refs(repo);
3340 else if (delref)
3341 error = delete_ref(repo, delref);
3342 else if (create_symref)
3343 error = add_symref(repo, argv[0], argv[1]);
3344 else
3345 error = add_ref(repo, argv[0], argv[1]);
3346 done:
3347 if (repo)
3348 got_repo_close(repo);
3349 if (worktree)
3350 got_worktree_close(worktree);
3351 free(cwd);
3352 free(repo_path);
3353 return error;
3356 __dead static void
3357 usage_branch(void)
3359 fprintf(stderr,
3360 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3361 "[name]\n", getprogname());
3362 exit(1);
3365 static const struct got_error *
3366 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3367 struct got_reference *ref)
3369 const struct got_error *err = NULL;
3370 const char *refname, *marker = " ";
3371 char *refstr;
3373 refname = got_ref_get_name(ref);
3374 if (worktree && strcmp(refname,
3375 got_worktree_get_head_ref_name(worktree)) == 0) {
3376 struct got_object_id *id = NULL;
3378 err = got_ref_resolve(&id, repo, ref);
3379 if (err)
3380 return err;
3381 if (got_object_id_cmp(id,
3382 got_worktree_get_base_commit_id(worktree)) == 0)
3383 marker = "* ";
3384 else
3385 marker = "~ ";
3386 free(id);
3389 if (strncmp(refname, "refs/heads/", 11) == 0)
3390 refname += 11;
3391 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3392 refname += 18;
3394 refstr = got_ref_to_str(ref);
3395 if (refstr == NULL)
3396 return got_error_from_errno("got_ref_to_str");
3398 printf("%s%s: %s\n", marker, refname, refstr);
3399 free(refstr);
3400 return NULL;
3403 static const struct got_error *
3404 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3406 const char *refname;
3408 if (worktree == NULL)
3409 return got_error(GOT_ERR_NOT_WORKTREE);
3411 refname = got_worktree_get_head_ref_name(worktree);
3413 if (strncmp(refname, "refs/heads/", 11) == 0)
3414 refname += 11;
3415 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3416 refname += 18;
3418 printf("%s\n", refname);
3420 return NULL;
3423 static const struct got_error *
3424 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3426 static const struct got_error *err = NULL;
3427 struct got_reflist_head refs;
3428 struct got_reflist_entry *re;
3429 struct got_reference *temp_ref = NULL;
3430 int rebase_in_progress, histedit_in_progress;
3432 SIMPLEQ_INIT(&refs);
3434 if (worktree) {
3435 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3436 worktree);
3437 if (err)
3438 return err;
3440 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3441 worktree);
3442 if (err)
3443 return err;
3445 if (rebase_in_progress || histedit_in_progress) {
3446 err = got_ref_open(&temp_ref, repo,
3447 got_worktree_get_head_ref_name(worktree), 0);
3448 if (err)
3449 return err;
3450 list_branch(repo, worktree, temp_ref);
3451 got_ref_close(temp_ref);
3455 err = got_ref_list(&refs, repo, "refs/heads",
3456 got_ref_cmp_by_name, NULL);
3457 if (err)
3458 return err;
3460 SIMPLEQ_FOREACH(re, &refs, entry)
3461 list_branch(repo, worktree, re->ref);
3463 got_ref_list_free(&refs);
3464 return NULL;
3467 static const struct got_error *
3468 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3469 const char *branch_name)
3471 const struct got_error *err = NULL;
3472 struct got_reference *ref = NULL;
3473 char *refname;
3475 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3476 return got_error_from_errno("asprintf");
3478 err = got_ref_open(&ref, repo, refname, 0);
3479 if (err)
3480 goto done;
3482 if (worktree &&
3483 strcmp(got_worktree_get_head_ref_name(worktree),
3484 got_ref_get_name(ref)) == 0) {
3485 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3486 "will not delete this work tree's current branch");
3487 goto done;
3490 err = got_ref_delete(ref, repo);
3491 done:
3492 if (ref)
3493 got_ref_close(ref);
3494 free(refname);
3495 return err;
3498 static const struct got_error *
3499 add_branch(struct got_repository *repo, const char *branch_name,
3500 struct got_object_id *base_commit_id)
3502 const struct got_error *err = NULL;
3503 struct got_reference *ref = NULL;
3504 char *base_refname = NULL, *refname = NULL;
3507 * Don't let the user create a branch name with a leading '-'.
3508 * While technically a valid reference name, this case is usually
3509 * an unintended typo.
3511 if (branch_name[0] == '-')
3512 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3514 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3515 err = got_error_from_errno("asprintf");
3516 goto done;
3519 err = got_ref_open(&ref, repo, refname, 0);
3520 if (err == NULL) {
3521 err = got_error(GOT_ERR_BRANCH_EXISTS);
3522 goto done;
3523 } else if (err->code != GOT_ERR_NOT_REF)
3524 goto done;
3526 err = got_ref_alloc(&ref, refname, base_commit_id);
3527 if (err)
3528 goto done;
3530 err = got_ref_write(ref, repo);
3531 done:
3532 if (ref)
3533 got_ref_close(ref);
3534 free(base_refname);
3535 free(refname);
3536 return err;
3539 static const struct got_error *
3540 cmd_branch(int argc, char *argv[])
3542 const struct got_error *error = NULL;
3543 struct got_repository *repo = NULL;
3544 struct got_worktree *worktree = NULL;
3545 char *cwd = NULL, *repo_path = NULL;
3546 int ch, do_list = 0, do_show = 0, do_update = 1;
3547 const char *delref = NULL, *commit_id_arg = NULL;
3548 struct got_reference *ref = NULL;
3549 struct got_pathlist_head paths;
3550 struct got_pathlist_entry *pe;
3551 struct got_object_id *commit_id = NULL;
3552 char *commit_id_str = NULL;
3554 TAILQ_INIT(&paths);
3556 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3557 switch (ch) {
3558 case 'c':
3559 commit_id_arg = optarg;
3560 break;
3561 case 'd':
3562 delref = optarg;
3563 break;
3564 case 'r':
3565 repo_path = realpath(optarg, NULL);
3566 if (repo_path == NULL)
3567 return got_error_from_errno2("realpath",
3568 optarg);
3569 got_path_strip_trailing_slashes(repo_path);
3570 break;
3571 case 'l':
3572 do_list = 1;
3573 break;
3574 case 'n':
3575 do_update = 0;
3576 break;
3577 default:
3578 usage_branch();
3579 /* NOTREACHED */
3583 if (do_list && delref)
3584 errx(1, "-l and -d options are mutually exclusive\n");
3586 argc -= optind;
3587 argv += optind;
3589 if (!do_list && !delref && argc == 0)
3590 do_show = 1;
3592 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3593 errx(1, "-c option can only be used when creating a branch");
3595 if (do_list || delref) {
3596 if (argc > 0)
3597 usage_branch();
3598 } else if (!do_show && argc != 1)
3599 usage_branch();
3601 #ifndef PROFILE
3602 if (do_list || do_show) {
3603 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3604 NULL) == -1)
3605 err(1, "pledge");
3606 } else {
3607 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3608 "sendfd unveil", NULL) == -1)
3609 err(1, "pledge");
3611 #endif
3612 cwd = getcwd(NULL, 0);
3613 if (cwd == NULL) {
3614 error = got_error_from_errno("getcwd");
3615 goto done;
3618 if (repo_path == NULL) {
3619 error = got_worktree_open(&worktree, cwd);
3620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 goto done;
3622 else
3623 error = NULL;
3624 if (worktree) {
3625 repo_path =
3626 strdup(got_worktree_get_repo_path(worktree));
3627 if (repo_path == NULL)
3628 error = got_error_from_errno("strdup");
3629 if (error)
3630 goto done;
3631 } else {
3632 repo_path = strdup(cwd);
3633 if (repo_path == NULL) {
3634 error = got_error_from_errno("strdup");
3635 goto done;
3640 error = got_repo_open(&repo, repo_path, NULL);
3641 if (error != NULL)
3642 goto done;
3644 error = apply_unveil(got_repo_get_path(repo), do_list,
3645 worktree ? got_worktree_get_root_path(worktree) : NULL);
3646 if (error)
3647 goto done;
3649 if (do_show)
3650 error = show_current_branch(repo, worktree);
3651 else if (do_list)
3652 error = list_branches(repo, worktree);
3653 else if (delref)
3654 error = delete_branch(repo, worktree, delref);
3655 else {
3656 if (commit_id_arg == NULL)
3657 commit_id_arg = worktree ?
3658 got_worktree_get_head_ref_name(worktree) :
3659 GOT_REF_HEAD;
3660 error = got_repo_match_object_id(&commit_id, NULL,
3661 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3662 if (error)
3663 goto done;
3664 error = add_branch(repo, argv[0], commit_id);
3665 if (error)
3666 goto done;
3667 if (worktree && do_update) {
3668 int did_something = 0;
3669 char *branch_refname = NULL;
3671 error = got_object_id_str(&commit_id_str, commit_id);
3672 if (error)
3673 goto done;
3674 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3675 worktree);
3676 if (error)
3677 goto done;
3678 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3679 == -1) {
3680 error = got_error_from_errno("asprintf");
3681 goto done;
3683 error = got_ref_open(&ref, repo, branch_refname, 0);
3684 free(branch_refname);
3685 if (error)
3686 goto done;
3687 error = switch_head_ref(ref, commit_id, worktree,
3688 repo);
3689 if (error)
3690 goto done;
3691 error = got_worktree_set_base_commit_id(worktree, repo,
3692 commit_id);
3693 if (error)
3694 goto done;
3695 error = got_worktree_checkout_files(worktree, &paths,
3696 repo, update_progress, &did_something,
3697 check_cancelled, NULL);
3698 if (error)
3699 goto done;
3700 if (did_something)
3701 printf("Updated to commit %s\n", commit_id_str);
3704 done:
3705 if (ref)
3706 got_ref_close(ref);
3707 if (repo)
3708 got_repo_close(repo);
3709 if (worktree)
3710 got_worktree_close(worktree);
3711 free(cwd);
3712 free(repo_path);
3713 free(commit_id);
3714 free(commit_id_str);
3715 TAILQ_FOREACH(pe, &paths, entry)
3716 free((char *)pe->path);
3717 got_pathlist_free(&paths);
3718 return error;
3722 __dead static void
3723 usage_tag(void)
3725 fprintf(stderr,
3726 "usage: %s tag [-c commit] [-r repository] [-l] "
3727 "[-m message] name\n", getprogname());
3728 exit(1);
3731 #if 0
3732 static const struct got_error *
3733 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3735 const struct got_error *err = NULL;
3736 struct got_reflist_entry *re, *se, *new;
3737 struct got_object_id *re_id, *se_id;
3738 struct got_tag_object *re_tag, *se_tag;
3739 time_t re_time, se_time;
3741 SIMPLEQ_FOREACH(re, tags, entry) {
3742 se = SIMPLEQ_FIRST(sorted);
3743 if (se == NULL) {
3744 err = got_reflist_entry_dup(&new, re);
3745 if (err)
3746 return err;
3747 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3748 continue;
3749 } else {
3750 err = got_ref_resolve(&re_id, repo, re->ref);
3751 if (err)
3752 break;
3753 err = got_object_open_as_tag(&re_tag, repo, re_id);
3754 free(re_id);
3755 if (err)
3756 break;
3757 re_time = got_object_tag_get_tagger_time(re_tag);
3758 got_object_tag_close(re_tag);
3761 while (se) {
3762 err = got_ref_resolve(&se_id, repo, re->ref);
3763 if (err)
3764 break;
3765 err = got_object_open_as_tag(&se_tag, repo, se_id);
3766 free(se_id);
3767 if (err)
3768 break;
3769 se_time = got_object_tag_get_tagger_time(se_tag);
3770 got_object_tag_close(se_tag);
3772 if (se_time > re_time) {
3773 err = got_reflist_entry_dup(&new, re);
3774 if (err)
3775 return err;
3776 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3777 break;
3779 se = SIMPLEQ_NEXT(se, entry);
3780 continue;
3783 done:
3784 return err;
3786 #endif
3788 static const struct got_error *
3789 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3791 static const struct got_error *err = NULL;
3792 struct got_reflist_head refs;
3793 struct got_reflist_entry *re;
3795 SIMPLEQ_INIT(&refs);
3797 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3798 if (err)
3799 return err;
3801 SIMPLEQ_FOREACH(re, &refs, entry) {
3802 const char *refname;
3803 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3804 char datebuf[26];
3805 const char *tagger;
3806 time_t tagger_time;
3807 struct got_object_id *id;
3808 struct got_tag_object *tag;
3809 struct got_commit_object *commit = NULL;
3811 refname = got_ref_get_name(re->ref);
3812 if (strncmp(refname, "refs/tags/", 10) != 0)
3813 continue;
3814 refname += 10;
3815 refstr = got_ref_to_str(re->ref);
3816 if (refstr == NULL) {
3817 err = got_error_from_errno("got_ref_to_str");
3818 break;
3820 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3821 free(refstr);
3823 err = got_ref_resolve(&id, repo, re->ref);
3824 if (err)
3825 break;
3826 err = got_object_open_as_tag(&tag, repo, id);
3827 if (err) {
3828 if (err->code != GOT_ERR_OBJ_TYPE) {
3829 free(id);
3830 break;
3832 /* "lightweight" tag */
3833 err = got_object_open_as_commit(&commit, repo, id);
3834 if (err) {
3835 free(id);
3836 break;
3838 tagger = got_object_commit_get_committer(commit);
3839 tagger_time =
3840 got_object_commit_get_committer_time(commit);
3841 err = got_object_id_str(&id_str, id);
3842 free(id);
3843 if (err)
3844 break;
3845 } else {
3846 free(id);
3847 tagger = got_object_tag_get_tagger(tag);
3848 tagger_time = got_object_tag_get_tagger_time(tag);
3849 err = got_object_id_str(&id_str,
3850 got_object_tag_get_object_id(tag));
3851 if (err)
3852 break;
3854 printf("from: %s\n", tagger);
3855 datestr = get_datestr(&tagger_time, datebuf);
3856 if (datestr)
3857 printf("date: %s UTC\n", datestr);
3858 if (commit)
3859 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3860 else {
3861 switch (got_object_tag_get_object_type(tag)) {
3862 case GOT_OBJ_TYPE_BLOB:
3863 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3864 id_str);
3865 break;
3866 case GOT_OBJ_TYPE_TREE:
3867 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3868 id_str);
3869 break;
3870 case GOT_OBJ_TYPE_COMMIT:
3871 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3872 id_str);
3873 break;
3874 case GOT_OBJ_TYPE_TAG:
3875 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3876 id_str);
3877 break;
3878 default:
3879 break;
3882 free(id_str);
3883 if (commit) {
3884 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3885 if (err)
3886 break;
3887 got_object_commit_close(commit);
3888 } else {
3889 tagmsg0 = strdup(got_object_tag_get_message(tag));
3890 got_object_tag_close(tag);
3891 if (tagmsg0 == NULL) {
3892 err = got_error_from_errno("strdup");
3893 break;
3897 tagmsg = tagmsg0;
3898 do {
3899 line = strsep(&tagmsg, "\n");
3900 if (line)
3901 printf(" %s\n", line);
3902 } while (line);
3903 free(tagmsg0);
3906 got_ref_list_free(&refs);
3907 return NULL;
3910 static const struct got_error *
3911 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3912 const char *tag_name, const char *repo_path)
3914 const struct got_error *err = NULL;
3915 char *template = NULL, *initial_content = NULL;
3916 char *editor = NULL;
3917 int fd = -1;
3919 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3920 err = got_error_from_errno("asprintf");
3921 goto done;
3924 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3925 commit_id_str, tag_name) == -1) {
3926 err = got_error_from_errno("asprintf");
3927 goto done;
3930 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3931 if (err)
3932 goto done;
3934 dprintf(fd, initial_content);
3935 close(fd);
3937 err = get_editor(&editor);
3938 if (err)
3939 goto done;
3940 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3941 done:
3942 free(initial_content);
3943 free(template);
3944 free(editor);
3946 /* Editor is done; we can now apply unveil(2) */
3947 if (err == NULL) {
3948 err = apply_unveil(repo_path, 0, NULL);
3949 if (err) {
3950 free(*tagmsg);
3951 *tagmsg = NULL;
3954 return err;
3957 static const struct got_error *
3958 add_tag(struct got_repository *repo, const char *tag_name,
3959 const char *commit_arg, const char *tagmsg_arg)
3961 const struct got_error *err = NULL;
3962 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3963 char *label = NULL, *commit_id_str = NULL;
3964 struct got_reference *ref = NULL;
3965 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3966 char *tagmsg_path = NULL, *tag_id_str = NULL;
3967 int preserve_tagmsg = 0;
3970 * Don't let the user create a tag name with a leading '-'.
3971 * While technically a valid reference name, this case is usually
3972 * an unintended typo.
3974 if (tag_name[0] == '-')
3975 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3977 err = get_author(&tagger, repo);
3978 if (err)
3979 return err;
3981 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3982 GOT_OBJ_TYPE_COMMIT, 1, repo);
3983 if (err)
3984 goto done;
3986 err = got_object_id_str(&commit_id_str, commit_id);
3987 if (err)
3988 goto done;
3990 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3991 refname = strdup(tag_name);
3992 if (refname == NULL) {
3993 err = got_error_from_errno("strdup");
3994 goto done;
3996 tag_name += 10;
3997 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3998 err = got_error_from_errno("asprintf");
3999 goto done;
4002 err = got_ref_open(&ref, repo, refname, 0);
4003 if (err == NULL) {
4004 err = got_error(GOT_ERR_TAG_EXISTS);
4005 goto done;
4006 } else if (err->code != GOT_ERR_NOT_REF)
4007 goto done;
4009 if (tagmsg_arg == NULL) {
4010 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4011 tag_name, got_repo_get_path(repo));
4012 if (err) {
4013 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4014 tagmsg_path != NULL)
4015 preserve_tagmsg = 1;
4016 goto done;
4020 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4021 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4022 if (err) {
4023 if (tagmsg_path)
4024 preserve_tagmsg = 1;
4025 goto done;
4028 err = got_ref_alloc(&ref, refname, tag_id);
4029 if (err) {
4030 if (tagmsg_path)
4031 preserve_tagmsg = 1;
4032 goto done;
4035 err = got_ref_write(ref, repo);
4036 if (err) {
4037 if (tagmsg_path)
4038 preserve_tagmsg = 1;
4039 goto done;
4042 err = got_object_id_str(&tag_id_str, tag_id);
4043 if (err) {
4044 if (tagmsg_path)
4045 preserve_tagmsg = 1;
4046 goto done;
4048 printf("Created tag %s\n", tag_id_str);
4049 done:
4050 if (preserve_tagmsg) {
4051 fprintf(stderr, "%s: tag message preserved in %s\n",
4052 getprogname(), tagmsg_path);
4053 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4054 err = got_error_from_errno2("unlink", tagmsg_path);
4055 free(tag_id_str);
4056 if (ref)
4057 got_ref_close(ref);
4058 free(commit_id);
4059 free(commit_id_str);
4060 free(refname);
4061 free(tagmsg);
4062 free(tagmsg_path);
4063 free(tagger);
4064 return err;
4067 static const struct got_error *
4068 cmd_tag(int argc, char *argv[])
4070 const struct got_error *error = NULL;
4071 struct got_repository *repo = NULL;
4072 struct got_worktree *worktree = NULL;
4073 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4074 char *gitconfig_path = NULL;
4075 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4076 int ch, do_list = 0;
4078 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4079 switch (ch) {
4080 case 'c':
4081 commit_id_arg = optarg;
4082 break;
4083 case 'm':
4084 tagmsg = optarg;
4085 break;
4086 case 'r':
4087 repo_path = realpath(optarg, NULL);
4088 if (repo_path == NULL)
4089 return got_error_from_errno2("realpath",
4090 optarg);
4091 got_path_strip_trailing_slashes(repo_path);
4092 break;
4093 case 'l':
4094 do_list = 1;
4095 break;
4096 default:
4097 usage_tag();
4098 /* NOTREACHED */
4102 argc -= optind;
4103 argv += optind;
4105 if (do_list) {
4106 if (commit_id_arg != NULL)
4107 errx(1, "-c option can only be used when creating a tag");
4108 if (tagmsg)
4109 errx(1, "-l and -m options are mutually exclusive");
4110 if (argc > 0)
4111 usage_tag();
4112 } else if (argc != 1)
4113 usage_tag();
4115 tag_name = argv[0];
4117 #ifndef PROFILE
4118 if (do_list) {
4119 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4120 NULL) == -1)
4121 err(1, "pledge");
4122 } else {
4123 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4124 "sendfd unveil", NULL) == -1)
4125 err(1, "pledge");
4127 #endif
4128 cwd = getcwd(NULL, 0);
4129 if (cwd == NULL) {
4130 error = got_error_from_errno("getcwd");
4131 goto done;
4134 if (repo_path == NULL) {
4135 error = got_worktree_open(&worktree, cwd);
4136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4137 goto done;
4138 else
4139 error = NULL;
4140 if (worktree) {
4141 repo_path =
4142 strdup(got_worktree_get_repo_path(worktree));
4143 if (repo_path == NULL)
4144 error = got_error_from_errno("strdup");
4145 if (error)
4146 goto done;
4147 } else {
4148 repo_path = strdup(cwd);
4149 if (repo_path == NULL) {
4150 error = got_error_from_errno("strdup");
4151 goto done;
4156 if (do_list) {
4157 error = got_repo_open(&repo, repo_path, NULL);
4158 if (error != NULL)
4159 goto done;
4160 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4161 if (error)
4162 goto done;
4163 error = list_tags(repo, worktree);
4164 } else {
4165 error = get_gitconfig_path(&gitconfig_path);
4166 if (error)
4167 goto done;
4168 error = got_repo_open(&repo, repo_path, gitconfig_path);
4169 if (error != NULL)
4170 goto done;
4172 if (tagmsg) {
4173 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4174 if (error)
4175 goto done;
4178 if (commit_id_arg == NULL) {
4179 struct got_reference *head_ref;
4180 struct got_object_id *commit_id;
4181 error = got_ref_open(&head_ref, repo,
4182 worktree ? got_worktree_get_head_ref_name(worktree)
4183 : GOT_REF_HEAD, 0);
4184 if (error)
4185 goto done;
4186 error = got_ref_resolve(&commit_id, repo, head_ref);
4187 got_ref_close(head_ref);
4188 if (error)
4189 goto done;
4190 error = got_object_id_str(&commit_id_str, commit_id);
4191 free(commit_id);
4192 if (error)
4193 goto done;
4196 error = add_tag(repo, tag_name,
4197 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4199 done:
4200 if (repo)
4201 got_repo_close(repo);
4202 if (worktree)
4203 got_worktree_close(worktree);
4204 free(cwd);
4205 free(repo_path);
4206 free(gitconfig_path);
4207 free(commit_id_str);
4208 return error;
4211 __dead static void
4212 usage_add(void)
4214 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4215 getprogname());
4216 exit(1);
4219 static const struct got_error *
4220 add_progress(void *arg, unsigned char status, const char *path)
4222 while (path[0] == '/')
4223 path++;
4224 printf("%c %s\n", status, path);
4225 return NULL;
4228 static const struct got_error *
4229 cmd_add(int argc, char *argv[])
4231 const struct got_error *error = NULL;
4232 struct got_repository *repo = NULL;
4233 struct got_worktree *worktree = NULL;
4234 char *cwd = NULL;
4235 struct got_pathlist_head paths;
4236 struct got_pathlist_entry *pe;
4237 int ch, can_recurse = 0, no_ignores = 0;
4239 TAILQ_INIT(&paths);
4241 while ((ch = getopt(argc, argv, "IR")) != -1) {
4242 switch (ch) {
4243 case 'I':
4244 no_ignores = 1;
4245 break;
4246 case 'R':
4247 can_recurse = 1;
4248 break;
4249 default:
4250 usage_add();
4251 /* NOTREACHED */
4255 argc -= optind;
4256 argv += optind;
4258 #ifndef PROFILE
4259 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4260 NULL) == -1)
4261 err(1, "pledge");
4262 #endif
4263 if (argc < 1)
4264 usage_add();
4266 cwd = getcwd(NULL, 0);
4267 if (cwd == NULL) {
4268 error = got_error_from_errno("getcwd");
4269 goto done;
4272 error = got_worktree_open(&worktree, cwd);
4273 if (error)
4274 goto done;
4276 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4277 NULL);
4278 if (error != NULL)
4279 goto done;
4281 error = apply_unveil(got_repo_get_path(repo), 1,
4282 got_worktree_get_root_path(worktree));
4283 if (error)
4284 goto done;
4286 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4287 if (error)
4288 goto done;
4290 if (!can_recurse && no_ignores) {
4291 error = got_error_msg(GOT_ERR_BAD_PATH,
4292 "disregarding ignores requires -R option");
4293 goto done;
4297 if (!can_recurse) {
4298 char *ondisk_path;
4299 struct stat sb;
4300 TAILQ_FOREACH(pe, &paths, entry) {
4301 if (asprintf(&ondisk_path, "%s/%s",
4302 got_worktree_get_root_path(worktree),
4303 pe->path) == -1) {
4304 error = got_error_from_errno("asprintf");
4305 goto done;
4307 if (lstat(ondisk_path, &sb) == -1) {
4308 if (errno == ENOENT) {
4309 free(ondisk_path);
4310 continue;
4312 error = got_error_from_errno2("lstat",
4313 ondisk_path);
4314 free(ondisk_path);
4315 goto done;
4317 free(ondisk_path);
4318 if (S_ISDIR(sb.st_mode)) {
4319 error = got_error_msg(GOT_ERR_BAD_PATH,
4320 "adding directories requires -R option");
4321 goto done;
4326 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4327 NULL, repo, no_ignores);
4328 done:
4329 if (repo)
4330 got_repo_close(repo);
4331 if (worktree)
4332 got_worktree_close(worktree);
4333 TAILQ_FOREACH(pe, &paths, entry)
4334 free((char *)pe->path);
4335 got_pathlist_free(&paths);
4336 free(cwd);
4337 return error;
4340 __dead static void
4341 usage_remove(void)
4343 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4344 getprogname());
4345 exit(1);
4348 static const struct got_error *
4349 print_remove_status(void *arg, unsigned char status,
4350 unsigned char staged_status, const char *path)
4352 while (path[0] == '/')
4353 path++;
4354 if (status == GOT_STATUS_NONEXISTENT)
4355 return NULL;
4356 if (status == staged_status && (status == GOT_STATUS_DELETE))
4357 status = GOT_STATUS_NO_CHANGE;
4358 printf("%c%c %s\n", status, staged_status, path);
4359 return NULL;
4362 static const struct got_error *
4363 cmd_remove(int argc, char *argv[])
4365 const struct got_error *error = NULL;
4366 struct got_worktree *worktree = NULL;
4367 struct got_repository *repo = NULL;
4368 char *cwd = NULL;
4369 struct got_pathlist_head paths;
4370 struct got_pathlist_entry *pe;
4371 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4373 TAILQ_INIT(&paths);
4375 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4376 switch (ch) {
4377 case 'f':
4378 delete_local_mods = 1;
4379 break;
4380 case 'k':
4381 keep_on_disk = 1;
4382 break;
4383 case 'R':
4384 can_recurse = 1;
4385 break;
4386 default:
4387 usage_remove();
4388 /* NOTREACHED */
4392 argc -= optind;
4393 argv += optind;
4395 #ifndef PROFILE
4396 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4397 NULL) == -1)
4398 err(1, "pledge");
4399 #endif
4400 if (argc < 1)
4401 usage_remove();
4403 cwd = getcwd(NULL, 0);
4404 if (cwd == NULL) {
4405 error = got_error_from_errno("getcwd");
4406 goto done;
4408 error = got_worktree_open(&worktree, cwd);
4409 if (error)
4410 goto done;
4412 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4413 NULL);
4414 if (error)
4415 goto done;
4417 error = apply_unveil(got_repo_get_path(repo), 1,
4418 got_worktree_get_root_path(worktree));
4419 if (error)
4420 goto done;
4422 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4423 if (error)
4424 goto done;
4426 if (!can_recurse) {
4427 char *ondisk_path;
4428 struct stat sb;
4429 TAILQ_FOREACH(pe, &paths, entry) {
4430 if (asprintf(&ondisk_path, "%s/%s",
4431 got_worktree_get_root_path(worktree),
4432 pe->path) == -1) {
4433 error = got_error_from_errno("asprintf");
4434 goto done;
4436 if (lstat(ondisk_path, &sb) == -1) {
4437 if (errno == ENOENT) {
4438 free(ondisk_path);
4439 continue;
4441 error = got_error_from_errno2("lstat",
4442 ondisk_path);
4443 free(ondisk_path);
4444 goto done;
4446 free(ondisk_path);
4447 if (S_ISDIR(sb.st_mode)) {
4448 error = got_error_msg(GOT_ERR_BAD_PATH,
4449 "removing directories requires -R option");
4450 goto done;
4455 error = got_worktree_schedule_delete(worktree, &paths,
4456 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4457 done:
4458 if (repo)
4459 got_repo_close(repo);
4460 if (worktree)
4461 got_worktree_close(worktree);
4462 TAILQ_FOREACH(pe, &paths, entry)
4463 free((char *)pe->path);
4464 got_pathlist_free(&paths);
4465 free(cwd);
4466 return error;
4469 __dead static void
4470 usage_revert(void)
4472 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4473 "path ...\n", getprogname());
4474 exit(1);
4477 static const struct got_error *
4478 revert_progress(void *arg, unsigned char status, const char *path)
4480 if (status == GOT_STATUS_UNVERSIONED)
4481 return NULL;
4483 while (path[0] == '/')
4484 path++;
4485 printf("%c %s\n", status, path);
4486 return NULL;
4489 struct choose_patch_arg {
4490 FILE *patch_script_file;
4491 const char *action;
4494 static const struct got_error *
4495 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4496 int nchanges, const char *action)
4498 char *line = NULL;
4499 size_t linesize = 0;
4500 ssize_t linelen;
4502 switch (status) {
4503 case GOT_STATUS_ADD:
4504 printf("A %s\n%s this addition? [y/n] ", path, action);
4505 break;
4506 case GOT_STATUS_DELETE:
4507 printf("D %s\n%s this deletion? [y/n] ", path, action);
4508 break;
4509 case GOT_STATUS_MODIFY:
4510 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4511 return got_error_from_errno("fseek");
4512 printf(GOT_COMMIT_SEP_STR);
4513 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4514 printf("%s", line);
4515 if (ferror(patch_file))
4516 return got_error_from_errno("getline");
4517 printf(GOT_COMMIT_SEP_STR);
4518 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4519 path, n, nchanges, action);
4520 break;
4521 default:
4522 return got_error_path(path, GOT_ERR_FILE_STATUS);
4525 return NULL;
4528 static const struct got_error *
4529 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4530 FILE *patch_file, int n, int nchanges)
4532 const struct got_error *err = NULL;
4533 char *line = NULL;
4534 size_t linesize = 0;
4535 ssize_t linelen;
4536 int resp = ' ';
4537 struct choose_patch_arg *a = arg;
4539 *choice = GOT_PATCH_CHOICE_NONE;
4541 if (a->patch_script_file) {
4542 char *nl;
4543 err = show_change(status, path, patch_file, n, nchanges,
4544 a->action);
4545 if (err)
4546 return err;
4547 linelen = getline(&line, &linesize, a->patch_script_file);
4548 if (linelen == -1) {
4549 if (ferror(a->patch_script_file))
4550 return got_error_from_errno("getline");
4551 return NULL;
4553 nl = strchr(line, '\n');
4554 if (nl)
4555 *nl = '\0';
4556 if (strcmp(line, "y") == 0) {
4557 *choice = GOT_PATCH_CHOICE_YES;
4558 printf("y\n");
4559 } else if (strcmp(line, "n") == 0) {
4560 *choice = GOT_PATCH_CHOICE_NO;
4561 printf("n\n");
4562 } else if (strcmp(line, "q") == 0 &&
4563 status == GOT_STATUS_MODIFY) {
4564 *choice = GOT_PATCH_CHOICE_QUIT;
4565 printf("q\n");
4566 } else
4567 printf("invalid response '%s'\n", line);
4568 free(line);
4569 return NULL;
4572 while (resp != 'y' && resp != 'n' && resp != 'q') {
4573 err = show_change(status, path, patch_file, n, nchanges,
4574 a->action);
4575 if (err)
4576 return err;
4577 resp = getchar();
4578 if (resp == '\n')
4579 resp = getchar();
4580 if (status == GOT_STATUS_MODIFY) {
4581 if (resp != 'y' && resp != 'n' && resp != 'q') {
4582 printf("invalid response '%c'\n", resp);
4583 resp = ' ';
4585 } else if (resp != 'y' && resp != 'n') {
4586 printf("invalid response '%c'\n", resp);
4587 resp = ' ';
4591 if (resp == 'y')
4592 *choice = GOT_PATCH_CHOICE_YES;
4593 else if (resp == 'n')
4594 *choice = GOT_PATCH_CHOICE_NO;
4595 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4596 *choice = GOT_PATCH_CHOICE_QUIT;
4598 return NULL;
4602 static const struct got_error *
4603 cmd_revert(int argc, char *argv[])
4605 const struct got_error *error = NULL;
4606 struct got_worktree *worktree = NULL;
4607 struct got_repository *repo = NULL;
4608 char *cwd = NULL, *path = NULL;
4609 struct got_pathlist_head paths;
4610 struct got_pathlist_entry *pe;
4611 int ch, can_recurse = 0, pflag = 0;
4612 FILE *patch_script_file = NULL;
4613 const char *patch_script_path = NULL;
4614 struct choose_patch_arg cpa;
4616 TAILQ_INIT(&paths);
4618 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4619 switch (ch) {
4620 case 'p':
4621 pflag = 1;
4622 break;
4623 case 'F':
4624 patch_script_path = optarg;
4625 break;
4626 case 'R':
4627 can_recurse = 1;
4628 break;
4629 default:
4630 usage_revert();
4631 /* NOTREACHED */
4635 argc -= optind;
4636 argv += optind;
4638 #ifndef PROFILE
4639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4640 "unveil", NULL) == -1)
4641 err(1, "pledge");
4642 #endif
4643 if (argc < 1)
4644 usage_revert();
4645 if (patch_script_path && !pflag)
4646 errx(1, "-F option can only be used together with -p option");
4648 cwd = getcwd(NULL, 0);
4649 if (cwd == NULL) {
4650 error = got_error_from_errno("getcwd");
4651 goto done;
4653 error = got_worktree_open(&worktree, cwd);
4654 if (error)
4655 goto done;
4657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4658 NULL);
4659 if (error != NULL)
4660 goto done;
4662 if (patch_script_path) {
4663 patch_script_file = fopen(patch_script_path, "r");
4664 if (patch_script_file == NULL) {
4665 error = got_error_from_errno2("fopen",
4666 patch_script_path);
4667 goto done;
4670 error = apply_unveil(got_repo_get_path(repo), 1,
4671 got_worktree_get_root_path(worktree));
4672 if (error)
4673 goto done;
4675 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4676 if (error)
4677 goto done;
4679 if (!can_recurse) {
4680 char *ondisk_path;
4681 struct stat sb;
4682 TAILQ_FOREACH(pe, &paths, entry) {
4683 if (asprintf(&ondisk_path, "%s/%s",
4684 got_worktree_get_root_path(worktree),
4685 pe->path) == -1) {
4686 error = got_error_from_errno("asprintf");
4687 goto done;
4689 if (lstat(ondisk_path, &sb) == -1) {
4690 if (errno == ENOENT) {
4691 free(ondisk_path);
4692 continue;
4694 error = got_error_from_errno2("lstat",
4695 ondisk_path);
4696 free(ondisk_path);
4697 goto done;
4699 free(ondisk_path);
4700 if (S_ISDIR(sb.st_mode)) {
4701 error = got_error_msg(GOT_ERR_BAD_PATH,
4702 "reverting directories requires -R option");
4703 goto done;
4708 cpa.patch_script_file = patch_script_file;
4709 cpa.action = "revert";
4710 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4711 pflag ? choose_patch : NULL, &cpa, repo);
4712 done:
4713 if (patch_script_file && fclose(patch_script_file) == EOF &&
4714 error == NULL)
4715 error = got_error_from_errno2("fclose", patch_script_path);
4716 if (repo)
4717 got_repo_close(repo);
4718 if (worktree)
4719 got_worktree_close(worktree);
4720 free(path);
4721 free(cwd);
4722 return error;
4725 __dead static void
4726 usage_commit(void)
4728 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4729 getprogname());
4730 exit(1);
4733 struct collect_commit_logmsg_arg {
4734 const char *cmdline_log;
4735 const char *editor;
4736 const char *worktree_path;
4737 const char *branch_name;
4738 const char *repo_path;
4739 char *logmsg_path;
4743 static const struct got_error *
4744 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4745 void *arg)
4747 char *initial_content = NULL;
4748 struct got_pathlist_entry *pe;
4749 const struct got_error *err = NULL;
4750 char *template = NULL;
4751 struct collect_commit_logmsg_arg *a = arg;
4752 int fd;
4753 size_t len;
4755 /* if a message was specified on the command line, just use it */
4756 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4757 len = strlen(a->cmdline_log) + 1;
4758 *logmsg = malloc(len + 1);
4759 if (*logmsg == NULL)
4760 return got_error_from_errno("malloc");
4761 strlcpy(*logmsg, a->cmdline_log, len);
4762 return NULL;
4765 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4766 return got_error_from_errno("asprintf");
4768 if (asprintf(&initial_content,
4769 "\n# changes to be committed on branch %s:\n",
4770 a->branch_name) == -1)
4771 return got_error_from_errno("asprintf");
4773 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4774 if (err)
4775 goto done;
4777 dprintf(fd, initial_content);
4779 TAILQ_FOREACH(pe, commitable_paths, entry) {
4780 struct got_commitable *ct = pe->data;
4781 dprintf(fd, "# %c %s\n",
4782 got_commitable_get_status(ct),
4783 got_commitable_get_path(ct));
4785 close(fd);
4787 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4788 done:
4789 free(initial_content);
4790 free(template);
4792 /* Editor is done; we can now apply unveil(2) */
4793 if (err == NULL) {
4794 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4795 if (err) {
4796 free(*logmsg);
4797 *logmsg = NULL;
4800 return err;
4803 static const struct got_error *
4804 cmd_commit(int argc, char *argv[])
4806 const struct got_error *error = NULL;
4807 struct got_worktree *worktree = NULL;
4808 struct got_repository *repo = NULL;
4809 char *cwd = NULL, *id_str = NULL;
4810 struct got_object_id *id = NULL;
4811 const char *logmsg = NULL;
4812 struct collect_commit_logmsg_arg cl_arg;
4813 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4814 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4815 struct got_pathlist_head paths;
4817 TAILQ_INIT(&paths);
4818 cl_arg.logmsg_path = NULL;
4820 while ((ch = getopt(argc, argv, "m:")) != -1) {
4821 switch (ch) {
4822 case 'm':
4823 logmsg = optarg;
4824 break;
4825 default:
4826 usage_commit();
4827 /* NOTREACHED */
4831 argc -= optind;
4832 argv += optind;
4834 #ifndef PROFILE
4835 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4836 "unveil", NULL) == -1)
4837 err(1, "pledge");
4838 #endif
4839 cwd = getcwd(NULL, 0);
4840 if (cwd == NULL) {
4841 error = got_error_from_errno("getcwd");
4842 goto done;
4844 error = got_worktree_open(&worktree, cwd);
4845 if (error)
4846 goto done;
4848 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4849 if (error)
4850 goto done;
4851 if (rebase_in_progress) {
4852 error = got_error(GOT_ERR_REBASING);
4853 goto done;
4856 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4857 worktree);
4858 if (error)
4859 goto done;
4861 error = get_gitconfig_path(&gitconfig_path);
4862 if (error)
4863 goto done;
4864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4865 gitconfig_path);
4866 if (error != NULL)
4867 goto done;
4869 error = get_author(&author, repo);
4870 if (error)
4871 return error;
4874 * unveil(2) traverses exec(2); if an editor is used we have
4875 * to apply unveil after the log message has been written.
4877 if (logmsg == NULL || strlen(logmsg) == 0)
4878 error = get_editor(&editor);
4879 else
4880 error = apply_unveil(got_repo_get_path(repo), 0,
4881 got_worktree_get_root_path(worktree));
4882 if (error)
4883 goto done;
4885 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4886 if (error)
4887 goto done;
4889 cl_arg.editor = editor;
4890 cl_arg.cmdline_log = logmsg;
4891 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4892 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4893 if (!histedit_in_progress) {
4894 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4895 error = got_error(GOT_ERR_COMMIT_BRANCH);
4896 goto done;
4898 cl_arg.branch_name += 11;
4900 cl_arg.repo_path = got_repo_get_path(repo);
4901 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4902 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4903 if (error) {
4904 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4905 cl_arg.logmsg_path != NULL)
4906 preserve_logmsg = 1;
4907 goto done;
4910 error = got_object_id_str(&id_str, id);
4911 if (error)
4912 goto done;
4913 printf("Created commit %s\n", id_str);
4914 done:
4915 if (preserve_logmsg) {
4916 fprintf(stderr, "%s: log message preserved in %s\n",
4917 getprogname(), cl_arg.logmsg_path);
4918 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4919 error == NULL)
4920 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4921 free(cl_arg.logmsg_path);
4922 if (repo)
4923 got_repo_close(repo);
4924 if (worktree)
4925 got_worktree_close(worktree);
4926 free(cwd);
4927 free(id_str);
4928 free(gitconfig_path);
4929 free(editor);
4930 free(author);
4931 return error;
4934 __dead static void
4935 usage_cherrypick(void)
4937 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4938 exit(1);
4941 static const struct got_error *
4942 cmd_cherrypick(int argc, char *argv[])
4944 const struct got_error *error = NULL;
4945 struct got_worktree *worktree = NULL;
4946 struct got_repository *repo = NULL;
4947 char *cwd = NULL, *commit_id_str = NULL;
4948 struct got_object_id *commit_id = NULL;
4949 struct got_commit_object *commit = NULL;
4950 struct got_object_qid *pid;
4951 struct got_reference *head_ref = NULL;
4952 int ch, did_something = 0;
4954 while ((ch = getopt(argc, argv, "")) != -1) {
4955 switch (ch) {
4956 default:
4957 usage_cherrypick();
4958 /* NOTREACHED */
4962 argc -= optind;
4963 argv += optind;
4965 #ifndef PROFILE
4966 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4967 "unveil", NULL) == -1)
4968 err(1, "pledge");
4969 #endif
4970 if (argc != 1)
4971 usage_cherrypick();
4973 cwd = getcwd(NULL, 0);
4974 if (cwd == NULL) {
4975 error = got_error_from_errno("getcwd");
4976 goto done;
4978 error = got_worktree_open(&worktree, cwd);
4979 if (error)
4980 goto done;
4982 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4983 NULL);
4984 if (error != NULL)
4985 goto done;
4987 error = apply_unveil(got_repo_get_path(repo), 0,
4988 got_worktree_get_root_path(worktree));
4989 if (error)
4990 goto done;
4992 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4993 GOT_OBJ_TYPE_COMMIT, repo);
4994 if (error != NULL) {
4995 struct got_reference *ref;
4996 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4997 goto done;
4998 error = got_ref_open(&ref, repo, argv[0], 0);
4999 if (error != NULL)
5000 goto done;
5001 error = got_ref_resolve(&commit_id, repo, ref);
5002 got_ref_close(ref);
5003 if (error != NULL)
5004 goto done;
5006 error = got_object_id_str(&commit_id_str, commit_id);
5007 if (error)
5008 goto done;
5010 error = got_ref_open(&head_ref, repo,
5011 got_worktree_get_head_ref_name(worktree), 0);
5012 if (error != NULL)
5013 goto done;
5015 error = check_same_branch(commit_id, head_ref, NULL, repo);
5016 if (error) {
5017 if (error->code != GOT_ERR_ANCESTRY)
5018 goto done;
5019 error = NULL;
5020 } else {
5021 error = got_error(GOT_ERR_SAME_BRANCH);
5022 goto done;
5025 error = got_object_open_as_commit(&commit, repo, commit_id);
5026 if (error)
5027 goto done;
5028 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5029 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5030 commit_id, repo, update_progress, &did_something, check_cancelled,
5031 NULL);
5032 if (error != NULL)
5033 goto done;
5035 if (did_something)
5036 printf("Merged commit %s\n", commit_id_str);
5037 done:
5038 if (commit)
5039 got_object_commit_close(commit);
5040 free(commit_id_str);
5041 if (head_ref)
5042 got_ref_close(head_ref);
5043 if (worktree)
5044 got_worktree_close(worktree);
5045 if (repo)
5046 got_repo_close(repo);
5047 return error;
5050 __dead static void
5051 usage_backout(void)
5053 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5054 exit(1);
5057 static const struct got_error *
5058 cmd_backout(int argc, char *argv[])
5060 const struct got_error *error = NULL;
5061 struct got_worktree *worktree = NULL;
5062 struct got_repository *repo = NULL;
5063 char *cwd = NULL, *commit_id_str = NULL;
5064 struct got_object_id *commit_id = NULL;
5065 struct got_commit_object *commit = NULL;
5066 struct got_object_qid *pid;
5067 struct got_reference *head_ref = NULL;
5068 int ch, did_something = 0;
5070 while ((ch = getopt(argc, argv, "")) != -1) {
5071 switch (ch) {
5072 default:
5073 usage_backout();
5074 /* NOTREACHED */
5078 argc -= optind;
5079 argv += optind;
5081 #ifndef PROFILE
5082 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5083 "unveil", NULL) == -1)
5084 err(1, "pledge");
5085 #endif
5086 if (argc != 1)
5087 usage_backout();
5089 cwd = getcwd(NULL, 0);
5090 if (cwd == NULL) {
5091 error = got_error_from_errno("getcwd");
5092 goto done;
5094 error = got_worktree_open(&worktree, cwd);
5095 if (error)
5096 goto done;
5098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5099 NULL);
5100 if (error != NULL)
5101 goto done;
5103 error = apply_unveil(got_repo_get_path(repo), 0,
5104 got_worktree_get_root_path(worktree));
5105 if (error)
5106 goto done;
5108 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5109 GOT_OBJ_TYPE_COMMIT, repo);
5110 if (error != NULL) {
5111 struct got_reference *ref;
5112 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5113 goto done;
5114 error = got_ref_open(&ref, repo, argv[0], 0);
5115 if (error != NULL)
5116 goto done;
5117 error = got_ref_resolve(&commit_id, repo, ref);
5118 got_ref_close(ref);
5119 if (error != NULL)
5120 goto done;
5122 error = got_object_id_str(&commit_id_str, commit_id);
5123 if (error)
5124 goto done;
5126 error = got_ref_open(&head_ref, repo,
5127 got_worktree_get_head_ref_name(worktree), 0);
5128 if (error != NULL)
5129 goto done;
5131 error = check_same_branch(commit_id, head_ref, NULL, repo);
5132 if (error)
5133 goto done;
5135 error = got_object_open_as_commit(&commit, repo, commit_id);
5136 if (error)
5137 goto done;
5138 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5139 if (pid == NULL) {
5140 error = got_error(GOT_ERR_ROOT_COMMIT);
5141 goto done;
5144 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5145 update_progress, &did_something, check_cancelled, NULL);
5146 if (error != NULL)
5147 goto done;
5149 if (did_something)
5150 printf("Backed out commit %s\n", commit_id_str);
5151 done:
5152 if (commit)
5153 got_object_commit_close(commit);
5154 free(commit_id_str);
5155 if (head_ref)
5156 got_ref_close(head_ref);
5157 if (worktree)
5158 got_worktree_close(worktree);
5159 if (repo)
5160 got_repo_close(repo);
5161 return error;
5164 __dead static void
5165 usage_rebase(void)
5167 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5168 getprogname());
5169 exit(1);
5172 void
5173 trim_logmsg(char *logmsg, int limit)
5175 char *nl;
5176 size_t len;
5178 len = strlen(logmsg);
5179 if (len > limit)
5180 len = limit;
5181 logmsg[len] = '\0';
5182 nl = strchr(logmsg, '\n');
5183 if (nl)
5184 *nl = '\0';
5187 static const struct got_error *
5188 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5190 const struct got_error *err;
5191 char *logmsg0 = NULL;
5192 const char *s;
5194 err = got_object_commit_get_logmsg(&logmsg0, commit);
5195 if (err)
5196 return err;
5198 s = logmsg0;
5199 while (isspace((unsigned char)s[0]))
5200 s++;
5202 *logmsg = strdup(s);
5203 if (*logmsg == NULL) {
5204 err = got_error_from_errno("strdup");
5205 goto done;
5208 trim_logmsg(*logmsg, limit);
5209 done:
5210 free(logmsg0);
5211 return err;
5214 static const struct got_error *
5215 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5217 const struct got_error *err;
5218 struct got_commit_object *commit = NULL;
5219 char *id_str = NULL, *logmsg = NULL;
5221 err = got_object_open_as_commit(&commit, repo, id);
5222 if (err)
5223 return err;
5225 err = got_object_id_str(&id_str, id);
5226 if (err)
5227 goto done;
5229 id_str[12] = '\0';
5231 err = get_short_logmsg(&logmsg, 42, commit);
5232 if (err)
5233 goto done;
5235 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5236 done:
5237 free(id_str);
5238 got_object_commit_close(commit);
5239 free(logmsg);
5240 return err;
5243 static const struct got_error *
5244 show_rebase_progress(struct got_commit_object *commit,
5245 struct got_object_id *old_id, struct got_object_id *new_id)
5247 const struct got_error *err;
5248 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5250 err = got_object_id_str(&old_id_str, old_id);
5251 if (err)
5252 goto done;
5254 if (new_id) {
5255 err = got_object_id_str(&new_id_str, new_id);
5256 if (err)
5257 goto done;
5260 old_id_str[12] = '\0';
5261 if (new_id_str)
5262 new_id_str[12] = '\0';
5264 err = get_short_logmsg(&logmsg, 42, commit);
5265 if (err)
5266 goto done;
5268 printf("%s -> %s: %s\n", old_id_str,
5269 new_id_str ? new_id_str : "no-op change", logmsg);
5270 done:
5271 free(old_id_str);
5272 free(new_id_str);
5273 free(logmsg);
5274 return err;
5277 static const struct got_error *
5278 rebase_progress(void *arg, unsigned char status, const char *path)
5280 unsigned char *rebase_status = arg;
5282 while (path[0] == '/')
5283 path++;
5284 printf("%c %s\n", status, path);
5286 if (*rebase_status == GOT_STATUS_CONFLICT)
5287 return NULL;
5288 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5289 *rebase_status = status;
5290 return NULL;
5293 static const struct got_error *
5294 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5295 struct got_reference *branch, struct got_reference *new_base_branch,
5296 struct got_reference *tmp_branch, struct got_repository *repo)
5298 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5299 return got_worktree_rebase_complete(worktree, fileindex,
5300 new_base_branch, tmp_branch, branch, repo);
5303 static const struct got_error *
5304 rebase_commit(struct got_pathlist_head *merged_paths,
5305 struct got_worktree *worktree, struct got_fileindex *fileindex,
5306 struct got_reference *tmp_branch,
5307 struct got_object_id *commit_id, struct got_repository *repo)
5309 const struct got_error *error;
5310 struct got_commit_object *commit;
5311 struct got_object_id *new_commit_id;
5313 error = got_object_open_as_commit(&commit, repo, commit_id);
5314 if (error)
5315 return error;
5317 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5318 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5319 if (error) {
5320 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5321 goto done;
5322 error = show_rebase_progress(commit, commit_id, NULL);
5323 } else {
5324 error = show_rebase_progress(commit, commit_id, new_commit_id);
5325 free(new_commit_id);
5327 done:
5328 got_object_commit_close(commit);
5329 return error;
5332 struct check_path_prefix_arg {
5333 const char *path_prefix;
5334 size_t len;
5335 int errcode;
5338 static const struct got_error *
5339 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5340 struct got_blob_object *blob2, struct got_object_id *id1,
5341 struct got_object_id *id2, const char *path1, const char *path2,
5342 mode_t mode1, mode_t mode2, struct got_repository *repo)
5344 struct check_path_prefix_arg *a = arg;
5346 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5347 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5348 return got_error(a->errcode);
5350 return NULL;
5353 static const struct got_error *
5354 check_path_prefix(struct got_object_id *parent_id,
5355 struct got_object_id *commit_id, const char *path_prefix,
5356 int errcode, struct got_repository *repo)
5358 const struct got_error *err;
5359 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5360 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5361 struct check_path_prefix_arg cpp_arg;
5363 if (got_path_is_root_dir(path_prefix))
5364 return NULL;
5366 err = got_object_open_as_commit(&commit, repo, commit_id);
5367 if (err)
5368 goto done;
5370 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5371 if (err)
5372 goto done;
5374 err = got_object_open_as_tree(&tree1, repo,
5375 got_object_commit_get_tree_id(parent_commit));
5376 if (err)
5377 goto done;
5379 err = got_object_open_as_tree(&tree2, repo,
5380 got_object_commit_get_tree_id(commit));
5381 if (err)
5382 goto done;
5384 cpp_arg.path_prefix = path_prefix;
5385 while (cpp_arg.path_prefix[0] == '/')
5386 cpp_arg.path_prefix++;
5387 cpp_arg.len = strlen(cpp_arg.path_prefix);
5388 cpp_arg.errcode = errcode;
5389 err = got_diff_tree(tree1, tree2, "", "", repo,
5390 check_path_prefix_in_diff, &cpp_arg, 0);
5391 done:
5392 if (tree1)
5393 got_object_tree_close(tree1);
5394 if (tree2)
5395 got_object_tree_close(tree2);
5396 if (commit)
5397 got_object_commit_close(commit);
5398 if (parent_commit)
5399 got_object_commit_close(parent_commit);
5400 return err;
5403 static const struct got_error *
5404 collect_commits(struct got_object_id_queue *commits,
5405 struct got_object_id *initial_commit_id,
5406 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5407 const char *path_prefix, int path_prefix_errcode,
5408 struct got_repository *repo)
5410 const struct got_error *err = NULL;
5411 struct got_commit_graph *graph = NULL;
5412 struct got_object_id *parent_id = NULL;
5413 struct got_object_qid *qid;
5414 struct got_object_id *commit_id = initial_commit_id;
5416 err = got_commit_graph_open(&graph, "/", 1);
5417 if (err)
5418 return err;
5420 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5421 check_cancelled, NULL);
5422 if (err)
5423 goto done;
5424 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5425 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5426 check_cancelled, NULL);
5427 if (err) {
5428 if (err->code == GOT_ERR_ITER_COMPLETED) {
5429 err = got_error_msg(GOT_ERR_ANCESTRY,
5430 "ran out of commits to rebase before "
5431 "youngest common ancestor commit has "
5432 "been reached?!?");
5434 goto done;
5435 } else {
5436 err = check_path_prefix(parent_id, commit_id,
5437 path_prefix, path_prefix_errcode, repo);
5438 if (err)
5439 goto done;
5441 err = got_object_qid_alloc(&qid, commit_id);
5442 if (err)
5443 goto done;
5444 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5445 commit_id = parent_id;
5448 done:
5449 got_commit_graph_close(graph);
5450 return err;
5453 static const struct got_error *
5454 cmd_rebase(int argc, char *argv[])
5456 const struct got_error *error = NULL;
5457 struct got_worktree *worktree = NULL;
5458 struct got_repository *repo = NULL;
5459 struct got_fileindex *fileindex = NULL;
5460 char *cwd = NULL;
5461 struct got_reference *branch = NULL;
5462 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5463 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5464 struct got_object_id *resume_commit_id = NULL;
5465 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5466 struct got_commit_object *commit = NULL;
5467 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5468 int histedit_in_progress = 0;
5469 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5470 struct got_object_id_queue commits;
5471 struct got_pathlist_head merged_paths;
5472 const struct got_object_id_queue *parent_ids;
5473 struct got_object_qid *qid, *pid;
5475 SIMPLEQ_INIT(&commits);
5476 TAILQ_INIT(&merged_paths);
5478 while ((ch = getopt(argc, argv, "ac")) != -1) {
5479 switch (ch) {
5480 case 'a':
5481 abort_rebase = 1;
5482 break;
5483 case 'c':
5484 continue_rebase = 1;
5485 break;
5486 default:
5487 usage_rebase();
5488 /* NOTREACHED */
5492 argc -= optind;
5493 argv += optind;
5495 #ifndef PROFILE
5496 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5497 "unveil", NULL) == -1)
5498 err(1, "pledge");
5499 #endif
5500 if (abort_rebase && continue_rebase)
5501 usage_rebase();
5502 else if (abort_rebase || continue_rebase) {
5503 if (argc != 0)
5504 usage_rebase();
5505 } else if (argc != 1)
5506 usage_rebase();
5508 cwd = getcwd(NULL, 0);
5509 if (cwd == NULL) {
5510 error = got_error_from_errno("getcwd");
5511 goto done;
5513 error = got_worktree_open(&worktree, cwd);
5514 if (error)
5515 goto done;
5517 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5518 NULL);
5519 if (error != NULL)
5520 goto done;
5522 error = apply_unveil(got_repo_get_path(repo), 0,
5523 got_worktree_get_root_path(worktree));
5524 if (error)
5525 goto done;
5527 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5528 worktree);
5529 if (error)
5530 goto done;
5531 if (histedit_in_progress) {
5532 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5533 goto done;
5536 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5537 if (error)
5538 goto done;
5540 if (abort_rebase) {
5541 int did_something;
5542 if (!rebase_in_progress) {
5543 error = got_error(GOT_ERR_NOT_REBASING);
5544 goto done;
5546 error = got_worktree_rebase_continue(&resume_commit_id,
5547 &new_base_branch, &tmp_branch, &branch, &fileindex,
5548 worktree, repo);
5549 if (error)
5550 goto done;
5551 printf("Switching work tree to %s\n",
5552 got_ref_get_symref_target(new_base_branch));
5553 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5554 new_base_branch, update_progress, &did_something);
5555 if (error)
5556 goto done;
5557 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5558 goto done; /* nothing else to do */
5561 if (continue_rebase) {
5562 if (!rebase_in_progress) {
5563 error = got_error(GOT_ERR_NOT_REBASING);
5564 goto done;
5566 error = got_worktree_rebase_continue(&resume_commit_id,
5567 &new_base_branch, &tmp_branch, &branch, &fileindex,
5568 worktree, repo);
5569 if (error)
5570 goto done;
5572 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5573 resume_commit_id, repo);
5574 if (error)
5575 goto done;
5577 yca_id = got_object_id_dup(resume_commit_id);
5578 if (yca_id == NULL) {
5579 error = got_error_from_errno("got_object_id_dup");
5580 goto done;
5582 } else {
5583 error = got_ref_open(&branch, repo, argv[0], 0);
5584 if (error != NULL)
5585 goto done;
5588 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5589 if (error)
5590 goto done;
5592 if (!continue_rebase) {
5593 struct got_object_id *base_commit_id;
5595 base_commit_id = got_worktree_get_base_commit_id(worktree);
5596 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5597 base_commit_id, branch_head_commit_id, repo,
5598 check_cancelled, NULL);
5599 if (error)
5600 goto done;
5601 if (yca_id == NULL) {
5602 error = got_error_msg(GOT_ERR_ANCESTRY,
5603 "specified branch shares no common ancestry "
5604 "with work tree's branch");
5605 goto done;
5608 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5609 if (error) {
5610 if (error->code != GOT_ERR_ANCESTRY)
5611 goto done;
5612 error = NULL;
5613 } else {
5614 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5615 "specified branch resolves to a commit which "
5616 "is already contained in work tree's branch");
5617 goto done;
5619 error = got_worktree_rebase_prepare(&new_base_branch,
5620 &tmp_branch, &fileindex, worktree, branch, repo);
5621 if (error)
5622 goto done;
5625 commit_id = branch_head_commit_id;
5626 error = got_object_open_as_commit(&commit, repo, commit_id);
5627 if (error)
5628 goto done;
5630 parent_ids = got_object_commit_get_parent_ids(commit);
5631 pid = SIMPLEQ_FIRST(parent_ids);
5632 if (pid == NULL) {
5633 if (!continue_rebase) {
5634 int did_something;
5635 error = got_worktree_rebase_abort(worktree, fileindex,
5636 repo, new_base_branch, update_progress,
5637 &did_something);
5638 if (error)
5639 goto done;
5640 printf("Rebase of %s aborted\n",
5641 got_ref_get_name(branch));
5643 error = got_error(GOT_ERR_EMPTY_REBASE);
5644 goto done;
5646 error = collect_commits(&commits, commit_id, pid->id,
5647 yca_id, got_worktree_get_path_prefix(worktree),
5648 GOT_ERR_REBASE_PATH, repo);
5649 got_object_commit_close(commit);
5650 commit = NULL;
5651 if (error)
5652 goto done;
5654 if (SIMPLEQ_EMPTY(&commits)) {
5655 if (continue_rebase) {
5656 error = rebase_complete(worktree, fileindex,
5657 branch, new_base_branch, tmp_branch, repo);
5658 goto done;
5659 } else {
5660 /* Fast-forward the reference of the branch. */
5661 struct got_object_id *new_head_commit_id;
5662 char *id_str;
5663 error = got_ref_resolve(&new_head_commit_id, repo,
5664 new_base_branch);
5665 if (error)
5666 goto done;
5667 error = got_object_id_str(&id_str, new_head_commit_id);
5668 printf("Forwarding %s to commit %s\n",
5669 got_ref_get_name(branch), id_str);
5670 free(id_str);
5671 error = got_ref_change_ref(branch,
5672 new_head_commit_id);
5673 if (error)
5674 goto done;
5678 pid = NULL;
5679 SIMPLEQ_FOREACH(qid, &commits, entry) {
5680 commit_id = qid->id;
5681 parent_id = pid ? pid->id : yca_id;
5682 pid = qid;
5684 error = got_worktree_rebase_merge_files(&merged_paths,
5685 worktree, fileindex, parent_id, commit_id, repo,
5686 rebase_progress, &rebase_status, check_cancelled, NULL);
5687 if (error)
5688 goto done;
5690 if (rebase_status == GOT_STATUS_CONFLICT) {
5691 error = show_rebase_merge_conflict(qid->id, repo);
5692 if (error)
5693 goto done;
5694 got_worktree_rebase_pathlist_free(&merged_paths);
5695 break;
5698 error = rebase_commit(&merged_paths, worktree, fileindex,
5699 tmp_branch, commit_id, repo);
5700 got_worktree_rebase_pathlist_free(&merged_paths);
5701 if (error)
5702 goto done;
5705 if (rebase_status == GOT_STATUS_CONFLICT) {
5706 error = got_worktree_rebase_postpone(worktree, fileindex);
5707 if (error)
5708 goto done;
5709 error = got_error_msg(GOT_ERR_CONFLICTS,
5710 "conflicts must be resolved before rebasing can continue");
5711 } else
5712 error = rebase_complete(worktree, fileindex, branch,
5713 new_base_branch, tmp_branch, repo);
5714 done:
5715 got_object_id_queue_free(&commits);
5716 free(branch_head_commit_id);
5717 free(resume_commit_id);
5718 free(yca_id);
5719 if (commit)
5720 got_object_commit_close(commit);
5721 if (branch)
5722 got_ref_close(branch);
5723 if (new_base_branch)
5724 got_ref_close(new_base_branch);
5725 if (tmp_branch)
5726 got_ref_close(tmp_branch);
5727 if (worktree)
5728 got_worktree_close(worktree);
5729 if (repo)
5730 got_repo_close(repo);
5731 return error;
5734 __dead static void
5735 usage_histedit(void)
5737 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5738 getprogname());
5739 exit(1);
5742 #define GOT_HISTEDIT_PICK 'p'
5743 #define GOT_HISTEDIT_EDIT 'e'
5744 #define GOT_HISTEDIT_FOLD 'f'
5745 #define GOT_HISTEDIT_DROP 'd'
5746 #define GOT_HISTEDIT_MESG 'm'
5748 static struct got_histedit_cmd {
5749 unsigned char code;
5750 const char *name;
5751 const char *desc;
5752 } got_histedit_cmds[] = {
5753 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5754 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5755 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5756 "be used" },
5757 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5758 { GOT_HISTEDIT_MESG, "mesg",
5759 "single-line log message for commit above (open editor if empty)" },
5762 struct got_histedit_list_entry {
5763 TAILQ_ENTRY(got_histedit_list_entry) entry;
5764 struct got_object_id *commit_id;
5765 const struct got_histedit_cmd *cmd;
5766 char *logmsg;
5768 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5770 static const struct got_error *
5771 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5772 FILE *f, struct got_repository *repo)
5774 const struct got_error *err = NULL;
5775 char *logmsg = NULL, *id_str = NULL;
5776 struct got_commit_object *commit = NULL;
5777 int n;
5779 err = got_object_open_as_commit(&commit, repo, commit_id);
5780 if (err)
5781 goto done;
5783 err = get_short_logmsg(&logmsg, 34, commit);
5784 if (err)
5785 goto done;
5787 err = got_object_id_str(&id_str, commit_id);
5788 if (err)
5789 goto done;
5791 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5792 if (n < 0)
5793 err = got_ferror(f, GOT_ERR_IO);
5794 done:
5795 if (commit)
5796 got_object_commit_close(commit);
5797 free(id_str);
5798 free(logmsg);
5799 return err;
5802 static const struct got_error *
5803 histedit_write_commit_list(struct got_object_id_queue *commits,
5804 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5806 const struct got_error *err = NULL;
5807 struct got_object_qid *qid;
5809 if (SIMPLEQ_EMPTY(commits))
5810 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5812 SIMPLEQ_FOREACH(qid, commits, entry) {
5813 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5814 f, repo);
5815 if (err)
5816 break;
5817 if (edit_logmsg_only) {
5818 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5819 if (n < 0) {
5820 err = got_ferror(f, GOT_ERR_IO);
5821 break;
5826 return err;
5829 static const struct got_error *
5830 write_cmd_list(FILE *f, const char *branch_name,
5831 struct got_object_id_queue *commits)
5833 const struct got_error *err = NULL;
5834 int n, i;
5835 char *id_str;
5836 struct got_object_qid *qid;
5838 qid = SIMPLEQ_FIRST(commits);
5839 err = got_object_id_str(&id_str, qid->id);
5840 if (err)
5841 return err;
5843 n = fprintf(f,
5844 "# Editing the history of branch '%s' starting at\n"
5845 "# commit %s\n"
5846 "# Commits will be processed in order from top to "
5847 "bottom of this file.\n", branch_name, id_str);
5848 if (n < 0) {
5849 err = got_ferror(f, GOT_ERR_IO);
5850 goto done;
5853 n = fprintf(f, "# Available histedit commands:\n");
5854 if (n < 0) {
5855 err = got_ferror(f, GOT_ERR_IO);
5856 goto done;
5859 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5860 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5861 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5862 cmd->desc);
5863 if (n < 0) {
5864 err = got_ferror(f, GOT_ERR_IO);
5865 break;
5868 done:
5869 free(id_str);
5870 return err;
5873 static const struct got_error *
5874 histedit_syntax_error(int lineno)
5876 static char msg[42];
5877 int ret;
5879 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5880 lineno);
5881 if (ret == -1 || ret >= sizeof(msg))
5882 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5884 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5887 static const struct got_error *
5888 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5889 char *logmsg, struct got_repository *repo)
5891 const struct got_error *err;
5892 struct got_commit_object *folded_commit = NULL;
5893 char *id_str, *folded_logmsg = NULL;
5895 err = got_object_id_str(&id_str, hle->commit_id);
5896 if (err)
5897 return err;
5899 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5900 if (err)
5901 goto done;
5903 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5904 if (err)
5905 goto done;
5906 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5907 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5908 folded_logmsg) == -1) {
5909 err = got_error_from_errno("asprintf");
5911 done:
5912 if (folded_commit)
5913 got_object_commit_close(folded_commit);
5914 free(id_str);
5915 free(folded_logmsg);
5916 return err;
5919 static struct got_histedit_list_entry *
5920 get_folded_commits(struct got_histedit_list_entry *hle)
5922 struct got_histedit_list_entry *prev, *folded = NULL;
5924 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5925 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5926 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5927 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5928 folded = prev;
5929 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5932 return folded;
5935 static const struct got_error *
5936 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5937 struct got_repository *repo)
5939 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5940 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5941 const struct got_error *err = NULL;
5942 struct got_commit_object *commit = NULL;
5943 int fd;
5944 struct got_histedit_list_entry *folded = NULL;
5946 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5947 if (err)
5948 return err;
5950 folded = get_folded_commits(hle);
5951 if (folded) {
5952 while (folded != hle) {
5953 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5954 folded = TAILQ_NEXT(folded, entry);
5955 continue;
5957 err = append_folded_commit_msg(&new_msg, folded,
5958 logmsg, repo);
5959 if (err)
5960 goto done;
5961 free(logmsg);
5962 logmsg = new_msg;
5963 folded = TAILQ_NEXT(folded, entry);
5967 err = got_object_id_str(&id_str, hle->commit_id);
5968 if (err)
5969 goto done;
5970 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5971 if (err)
5972 goto done;
5973 if (asprintf(&new_msg,
5974 "%s\n# original log message of commit %s: %s",
5975 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5976 err = got_error_from_errno("asprintf");
5977 goto done;
5979 free(logmsg);
5980 logmsg = new_msg;
5982 err = got_object_id_str(&id_str, hle->commit_id);
5983 if (err)
5984 goto done;
5986 err = got_opentemp_named_fd(&logmsg_path, &fd,
5987 GOT_TMPDIR_STR "/got-logmsg");
5988 if (err)
5989 goto done;
5991 dprintf(fd, logmsg);
5992 close(fd);
5994 err = get_editor(&editor);
5995 if (err)
5996 goto done;
5998 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5999 if (err) {
6000 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6001 goto done;
6002 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6004 done:
6005 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6006 err = got_error_from_errno2("unlink", logmsg_path);
6007 free(logmsg_path);
6008 free(logmsg);
6009 free(orig_logmsg);
6010 free(editor);
6011 if (commit)
6012 got_object_commit_close(commit);
6013 return err;
6016 static const struct got_error *
6017 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6018 FILE *f, struct got_repository *repo)
6020 const struct got_error *err = NULL;
6021 char *line = NULL, *p, *end;
6022 size_t size;
6023 ssize_t len;
6024 int lineno = 0, i;
6025 const struct got_histedit_cmd *cmd;
6026 struct got_object_id *commit_id = NULL;
6027 struct got_histedit_list_entry *hle = NULL;
6029 for (;;) {
6030 len = getline(&line, &size, f);
6031 if (len == -1) {
6032 const struct got_error *getline_err;
6033 if (feof(f))
6034 break;
6035 getline_err = got_error_from_errno("getline");
6036 err = got_ferror(f, getline_err->code);
6037 break;
6039 lineno++;
6040 p = line;
6041 while (isspace((unsigned char)p[0]))
6042 p++;
6043 if (p[0] == '#' || p[0] == '\0') {
6044 free(line);
6045 line = NULL;
6046 continue;
6048 cmd = NULL;
6049 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6050 cmd = &got_histedit_cmds[i];
6051 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6052 isspace((unsigned char)p[strlen(cmd->name)])) {
6053 p += strlen(cmd->name);
6054 break;
6056 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6057 p++;
6058 break;
6061 if (i == nitems(got_histedit_cmds)) {
6062 err = histedit_syntax_error(lineno);
6063 break;
6065 while (isspace((unsigned char)p[0]))
6066 p++;
6067 if (cmd->code == GOT_HISTEDIT_MESG) {
6068 if (hle == NULL || hle->logmsg != NULL) {
6069 err = got_error(GOT_ERR_HISTEDIT_CMD);
6070 break;
6072 if (p[0] == '\0') {
6073 err = histedit_edit_logmsg(hle, repo);
6074 if (err)
6075 break;
6076 } else {
6077 hle->logmsg = strdup(p);
6078 if (hle->logmsg == NULL) {
6079 err = got_error_from_errno("strdup");
6080 break;
6083 free(line);
6084 line = NULL;
6085 continue;
6086 } else {
6087 end = p;
6088 while (end[0] && !isspace((unsigned char)end[0]))
6089 end++;
6090 *end = '\0';
6092 err = got_object_resolve_id_str(&commit_id, repo, p);
6093 if (err) {
6094 /* override error code */
6095 err = histedit_syntax_error(lineno);
6096 break;
6099 hle = malloc(sizeof(*hle));
6100 if (hle == NULL) {
6101 err = got_error_from_errno("malloc");
6102 break;
6104 hle->cmd = cmd;
6105 hle->commit_id = commit_id;
6106 hle->logmsg = NULL;
6107 commit_id = NULL;
6108 free(line);
6109 line = NULL;
6110 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6113 free(line);
6114 free(commit_id);
6115 return err;
6118 static const struct got_error *
6119 histedit_check_script(struct got_histedit_list *histedit_cmds,
6120 struct got_object_id_queue *commits, struct got_repository *repo)
6122 const struct got_error *err = NULL;
6123 struct got_object_qid *qid;
6124 struct got_histedit_list_entry *hle;
6125 static char msg[92];
6126 char *id_str;
6128 if (TAILQ_EMPTY(histedit_cmds))
6129 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6130 "histedit script contains no commands");
6131 if (SIMPLEQ_EMPTY(commits))
6132 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6134 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6135 struct got_histedit_list_entry *hle2;
6136 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6137 if (hle == hle2)
6138 continue;
6139 if (got_object_id_cmp(hle->commit_id,
6140 hle2->commit_id) != 0)
6141 continue;
6142 err = got_object_id_str(&id_str, hle->commit_id);
6143 if (err)
6144 return err;
6145 snprintf(msg, sizeof(msg), "commit %s is listed "
6146 "more than once in histedit script", id_str);
6147 free(id_str);
6148 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6152 SIMPLEQ_FOREACH(qid, commits, entry) {
6153 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6154 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6155 break;
6157 if (hle == NULL) {
6158 err = got_object_id_str(&id_str, qid->id);
6159 if (err)
6160 return err;
6161 snprintf(msg, sizeof(msg),
6162 "commit %s missing from histedit script", id_str);
6163 free(id_str);
6164 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6168 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6169 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6170 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6171 "last commit in histedit script cannot be folded");
6173 return NULL;
6176 static const struct got_error *
6177 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6178 const char *path, struct got_object_id_queue *commits,
6179 struct got_repository *repo)
6181 const struct got_error *err = NULL;
6182 char *editor;
6183 FILE *f = NULL;
6185 err = get_editor(&editor);
6186 if (err)
6187 return err;
6189 if (spawn_editor(editor, path) == -1) {
6190 err = got_error_from_errno("failed spawning editor");
6191 goto done;
6194 f = fopen(path, "r");
6195 if (f == NULL) {
6196 err = got_error_from_errno("fopen");
6197 goto done;
6199 err = histedit_parse_list(histedit_cmds, f, repo);
6200 if (err)
6201 goto done;
6203 err = histedit_check_script(histedit_cmds, commits, repo);
6204 done:
6205 if (f && fclose(f) != 0 && err == NULL)
6206 err = got_error_from_errno("fclose");
6207 free(editor);
6208 return err;
6211 static const struct got_error *
6212 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6213 struct got_object_id_queue *, const char *, const char *,
6214 struct got_repository *);
6216 static const struct got_error *
6217 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6218 struct got_object_id_queue *commits, const char *branch_name,
6219 int edit_logmsg_only, struct got_repository *repo)
6221 const struct got_error *err;
6222 FILE *f = NULL;
6223 char *path = NULL;
6225 err = got_opentemp_named(&path, &f, "got-histedit");
6226 if (err)
6227 return err;
6229 err = write_cmd_list(f, branch_name, commits);
6230 if (err)
6231 goto done;
6233 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6234 if (err)
6235 goto done;
6237 if (edit_logmsg_only) {
6238 rewind(f);
6239 err = histedit_parse_list(histedit_cmds, f, repo);
6240 } else {
6241 if (fclose(f) != 0) {
6242 err = got_error_from_errno("fclose");
6243 goto done;
6245 f = NULL;
6246 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6247 if (err) {
6248 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6249 err->code != GOT_ERR_HISTEDIT_CMD)
6250 goto done;
6251 err = histedit_edit_list_retry(histedit_cmds, err,
6252 commits, path, branch_name, repo);
6255 done:
6256 if (f && fclose(f) != 0 && err == NULL)
6257 err = got_error_from_errno("fclose");
6258 if (path && unlink(path) != 0 && err == NULL)
6259 err = got_error_from_errno2("unlink", path);
6260 free(path);
6261 return err;
6264 static const struct got_error *
6265 histedit_save_list(struct got_histedit_list *histedit_cmds,
6266 struct got_worktree *worktree, struct got_repository *repo)
6268 const struct got_error *err = NULL;
6269 char *path = NULL;
6270 FILE *f = NULL;
6271 struct got_histedit_list_entry *hle;
6272 struct got_commit_object *commit = NULL;
6274 err = got_worktree_get_histedit_script_path(&path, worktree);
6275 if (err)
6276 return err;
6278 f = fopen(path, "w");
6279 if (f == NULL) {
6280 err = got_error_from_errno2("fopen", path);
6281 goto done;
6283 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6284 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6285 repo);
6286 if (err)
6287 break;
6289 if (hle->logmsg) {
6290 int n = fprintf(f, "%c %s\n",
6291 GOT_HISTEDIT_MESG, hle->logmsg);
6292 if (n < 0) {
6293 err = got_ferror(f, GOT_ERR_IO);
6294 break;
6298 done:
6299 if (f && fclose(f) != 0 && err == NULL)
6300 err = got_error_from_errno("fclose");
6301 free(path);
6302 if (commit)
6303 got_object_commit_close(commit);
6304 return err;
6307 void
6308 histedit_free_list(struct got_histedit_list *histedit_cmds)
6310 struct got_histedit_list_entry *hle;
6312 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6313 TAILQ_REMOVE(histedit_cmds, hle, entry);
6314 free(hle);
6318 static const struct got_error *
6319 histedit_load_list(struct got_histedit_list *histedit_cmds,
6320 const char *path, struct got_repository *repo)
6322 const struct got_error *err = NULL;
6323 FILE *f = NULL;
6325 f = fopen(path, "r");
6326 if (f == NULL) {
6327 err = got_error_from_errno2("fopen", path);
6328 goto done;
6331 err = histedit_parse_list(histedit_cmds, f, repo);
6332 done:
6333 if (f && fclose(f) != 0 && err == NULL)
6334 err = got_error_from_errno("fclose");
6335 return err;
6338 static const struct got_error *
6339 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6340 const struct got_error *edit_err, struct got_object_id_queue *commits,
6341 const char *path, const char *branch_name, struct got_repository *repo)
6343 const struct got_error *err = NULL, *prev_err = edit_err;
6344 int resp = ' ';
6346 while (resp != 'c' && resp != 'r' && resp != 'a') {
6347 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6348 "or (a)bort: ", getprogname(), prev_err->msg);
6349 resp = getchar();
6350 if (resp == '\n')
6351 resp = getchar();
6352 if (resp == 'c') {
6353 histedit_free_list(histedit_cmds);
6354 err = histedit_run_editor(histedit_cmds, path, commits,
6355 repo);
6356 if (err) {
6357 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6358 err->code != GOT_ERR_HISTEDIT_CMD)
6359 break;
6360 prev_err = err;
6361 resp = ' ';
6362 continue;
6364 break;
6365 } else if (resp == 'r') {
6366 histedit_free_list(histedit_cmds);
6367 err = histedit_edit_script(histedit_cmds,
6368 commits, branch_name, 0, repo);
6369 if (err) {
6370 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6371 err->code != GOT_ERR_HISTEDIT_CMD)
6372 break;
6373 prev_err = err;
6374 resp = ' ';
6375 continue;
6377 break;
6378 } else if (resp == 'a') {
6379 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6380 break;
6381 } else
6382 printf("invalid response '%c'\n", resp);
6385 return err;
6388 static const struct got_error *
6389 histedit_complete(struct got_worktree *worktree,
6390 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6391 struct got_reference *branch, struct got_repository *repo)
6393 printf("Switching work tree to %s\n",
6394 got_ref_get_symref_target(branch));
6395 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6396 branch, repo);
6399 static const struct got_error *
6400 show_histedit_progress(struct got_commit_object *commit,
6401 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6403 const struct got_error *err;
6404 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6406 err = got_object_id_str(&old_id_str, hle->commit_id);
6407 if (err)
6408 goto done;
6410 if (new_id) {
6411 err = got_object_id_str(&new_id_str, new_id);
6412 if (err)
6413 goto done;
6416 old_id_str[12] = '\0';
6417 if (new_id_str)
6418 new_id_str[12] = '\0';
6420 if (hle->logmsg) {
6421 logmsg = strdup(hle->logmsg);
6422 if (logmsg == NULL) {
6423 err = got_error_from_errno("strdup");
6424 goto done;
6426 trim_logmsg(logmsg, 42);
6427 } else {
6428 err = get_short_logmsg(&logmsg, 42, commit);
6429 if (err)
6430 goto done;
6433 switch (hle->cmd->code) {
6434 case GOT_HISTEDIT_PICK:
6435 case GOT_HISTEDIT_EDIT:
6436 printf("%s -> %s: %s\n", old_id_str,
6437 new_id_str ? new_id_str : "no-op change", logmsg);
6438 break;
6439 case GOT_HISTEDIT_DROP:
6440 case GOT_HISTEDIT_FOLD:
6441 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6442 logmsg);
6443 break;
6444 default:
6445 break;
6447 done:
6448 free(old_id_str);
6449 free(new_id_str);
6450 return err;
6453 static const struct got_error *
6454 histedit_commit(struct got_pathlist_head *merged_paths,
6455 struct got_worktree *worktree, struct got_fileindex *fileindex,
6456 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6457 struct got_repository *repo)
6459 const struct got_error *err;
6460 struct got_commit_object *commit;
6461 struct got_object_id *new_commit_id;
6463 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6464 && hle->logmsg == NULL) {
6465 err = histedit_edit_logmsg(hle, repo);
6466 if (err)
6467 return err;
6470 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6471 if (err)
6472 return err;
6474 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6475 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6476 hle->logmsg, repo);
6477 if (err) {
6478 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6479 goto done;
6480 err = show_histedit_progress(commit, hle, NULL);
6481 } else {
6482 err = show_histedit_progress(commit, hle, new_commit_id);
6483 free(new_commit_id);
6485 done:
6486 got_object_commit_close(commit);
6487 return err;
6490 static const struct got_error *
6491 histedit_skip_commit(struct got_histedit_list_entry *hle,
6492 struct got_worktree *worktree, struct got_repository *repo)
6494 const struct got_error *error;
6495 struct got_commit_object *commit;
6497 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6498 repo);
6499 if (error)
6500 return error;
6502 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6503 if (error)
6504 return error;
6506 error = show_histedit_progress(commit, hle, NULL);
6507 got_object_commit_close(commit);
6508 return error;
6511 static const struct got_error *
6512 check_local_changes(void *arg, unsigned char status,
6513 unsigned char staged_status, const char *path,
6514 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6515 struct got_object_id *commit_id, int dirfd, const char *de_name)
6517 int *have_local_changes = arg;
6519 switch (status) {
6520 case GOT_STATUS_ADD:
6521 case GOT_STATUS_DELETE:
6522 case GOT_STATUS_MODIFY:
6523 case GOT_STATUS_CONFLICT:
6524 *have_local_changes = 1;
6525 return got_error(GOT_ERR_CANCELLED);
6526 default:
6527 break;
6530 switch (staged_status) {
6531 case GOT_STATUS_ADD:
6532 case GOT_STATUS_DELETE:
6533 case GOT_STATUS_MODIFY:
6534 *have_local_changes = 1;
6535 return got_error(GOT_ERR_CANCELLED);
6536 default:
6537 break;
6540 return NULL;
6543 static const struct got_error *
6544 cmd_histedit(int argc, char *argv[])
6546 const struct got_error *error = NULL;
6547 struct got_worktree *worktree = NULL;
6548 struct got_fileindex *fileindex = NULL;
6549 struct got_repository *repo = NULL;
6550 char *cwd = NULL;
6551 struct got_reference *branch = NULL;
6552 struct got_reference *tmp_branch = NULL;
6553 struct got_object_id *resume_commit_id = NULL;
6554 struct got_object_id *base_commit_id = NULL;
6555 struct got_object_id *head_commit_id = NULL;
6556 struct got_commit_object *commit = NULL;
6557 int ch, rebase_in_progress = 0, did_something;
6558 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6559 int edit_logmsg_only = 0;
6560 const char *edit_script_path = NULL;
6561 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6562 struct got_object_id_queue commits;
6563 struct got_pathlist_head merged_paths;
6564 const struct got_object_id_queue *parent_ids;
6565 struct got_object_qid *pid;
6566 struct got_histedit_list histedit_cmds;
6567 struct got_histedit_list_entry *hle;
6569 SIMPLEQ_INIT(&commits);
6570 TAILQ_INIT(&histedit_cmds);
6571 TAILQ_INIT(&merged_paths);
6573 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6574 switch (ch) {
6575 case 'a':
6576 abort_edit = 1;
6577 break;
6578 case 'c':
6579 continue_edit = 1;
6580 break;
6581 case 'F':
6582 edit_script_path = optarg;
6583 break;
6584 case 'm':
6585 edit_logmsg_only = 1;
6586 break;
6587 default:
6588 usage_histedit();
6589 /* NOTREACHED */
6593 argc -= optind;
6594 argv += optind;
6596 #ifndef PROFILE
6597 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6598 "unveil", NULL) == -1)
6599 err(1, "pledge");
6600 #endif
6601 if (abort_edit && continue_edit)
6602 errx(1, "histedit's -a and -c options are mutually exclusive");
6603 if (edit_script_path && edit_logmsg_only)
6604 errx(1, "histedit's -F and -m options are mutually exclusive");
6605 if (abort_edit && edit_logmsg_only)
6606 errx(1, "histedit's -a and -m options are mutually exclusive");
6607 if (continue_edit && edit_logmsg_only)
6608 errx(1, "histedit's -c and -m options are mutually exclusive");
6609 if (argc != 0)
6610 usage_histedit();
6613 * This command cannot apply unveil(2) in all cases because the
6614 * user may choose to run an editor to edit the histedit script
6615 * and to edit individual commit log messages.
6616 * unveil(2) traverses exec(2); if an editor is used we have to
6617 * apply unveil after edit script and log messages have been written.
6618 * XXX TODO: Make use of unveil(2) where possible.
6621 cwd = getcwd(NULL, 0);
6622 if (cwd == NULL) {
6623 error = got_error_from_errno("getcwd");
6624 goto done;
6626 error = got_worktree_open(&worktree, cwd);
6627 if (error)
6628 goto done;
6630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6631 NULL);
6632 if (error != NULL)
6633 goto done;
6635 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6636 if (error)
6637 goto done;
6638 if (rebase_in_progress) {
6639 error = got_error(GOT_ERR_REBASING);
6640 goto done;
6643 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6644 if (error)
6645 goto done;
6647 if (edit_in_progress && edit_logmsg_only) {
6648 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6649 "histedit operation is in progress in this "
6650 "work tree and must be continued or aborted "
6651 "before the -m option can be used");
6652 goto done;
6655 if (edit_in_progress && abort_edit) {
6656 error = got_worktree_histedit_continue(&resume_commit_id,
6657 &tmp_branch, &branch, &base_commit_id, &fileindex,
6658 worktree, repo);
6659 if (error)
6660 goto done;
6661 printf("Switching work tree to %s\n",
6662 got_ref_get_symref_target(branch));
6663 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6664 branch, base_commit_id, update_progress, &did_something);
6665 if (error)
6666 goto done;
6667 printf("Histedit of %s aborted\n",
6668 got_ref_get_symref_target(branch));
6669 goto done; /* nothing else to do */
6670 } else if (abort_edit) {
6671 error = got_error(GOT_ERR_NOT_HISTEDIT);
6672 goto done;
6675 if (continue_edit) {
6676 char *path;
6678 if (!edit_in_progress) {
6679 error = got_error(GOT_ERR_NOT_HISTEDIT);
6680 goto done;
6683 error = got_worktree_get_histedit_script_path(&path, worktree);
6684 if (error)
6685 goto done;
6687 error = histedit_load_list(&histedit_cmds, path, repo);
6688 free(path);
6689 if (error)
6690 goto done;
6692 error = got_worktree_histedit_continue(&resume_commit_id,
6693 &tmp_branch, &branch, &base_commit_id, &fileindex,
6694 worktree, repo);
6695 if (error)
6696 goto done;
6698 error = got_ref_resolve(&head_commit_id, repo, branch);
6699 if (error)
6700 goto done;
6702 error = got_object_open_as_commit(&commit, repo,
6703 head_commit_id);
6704 if (error)
6705 goto done;
6706 parent_ids = got_object_commit_get_parent_ids(commit);
6707 pid = SIMPLEQ_FIRST(parent_ids);
6708 if (pid == NULL) {
6709 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6710 goto done;
6712 error = collect_commits(&commits, head_commit_id, pid->id,
6713 base_commit_id, got_worktree_get_path_prefix(worktree),
6714 GOT_ERR_HISTEDIT_PATH, repo);
6715 got_object_commit_close(commit);
6716 commit = NULL;
6717 if (error)
6718 goto done;
6719 } else {
6720 if (edit_in_progress) {
6721 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6722 goto done;
6725 error = got_ref_open(&branch, repo,
6726 got_worktree_get_head_ref_name(worktree), 0);
6727 if (error != NULL)
6728 goto done;
6730 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6731 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6732 "will not edit commit history of a branch outside "
6733 "the \"refs/heads/\" reference namespace");
6734 goto done;
6737 error = got_ref_resolve(&head_commit_id, repo, branch);
6738 got_ref_close(branch);
6739 branch = NULL;
6740 if (error)
6741 goto done;
6743 error = got_object_open_as_commit(&commit, repo,
6744 head_commit_id);
6745 if (error)
6746 goto done;
6747 parent_ids = got_object_commit_get_parent_ids(commit);
6748 pid = SIMPLEQ_FIRST(parent_ids);
6749 if (pid == NULL) {
6750 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6751 goto done;
6753 error = collect_commits(&commits, head_commit_id, pid->id,
6754 got_worktree_get_base_commit_id(worktree),
6755 got_worktree_get_path_prefix(worktree),
6756 GOT_ERR_HISTEDIT_PATH, repo);
6757 got_object_commit_close(commit);
6758 commit = NULL;
6759 if (error)
6760 goto done;
6762 if (SIMPLEQ_EMPTY(&commits)) {
6763 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6764 goto done;
6767 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6768 &base_commit_id, &fileindex, worktree, repo);
6769 if (error)
6770 goto done;
6772 if (edit_script_path) {
6773 error = histedit_load_list(&histedit_cmds,
6774 edit_script_path, repo);
6775 if (error) {
6776 got_worktree_histedit_abort(worktree, fileindex,
6777 repo, branch, base_commit_id,
6778 update_progress, &did_something);
6779 goto done;
6781 } else {
6782 const char *branch_name;
6783 branch_name = got_ref_get_symref_target(branch);
6784 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6785 branch_name += 11;
6786 error = histedit_edit_script(&histedit_cmds, &commits,
6787 branch_name, edit_logmsg_only, repo);
6788 if (error) {
6789 got_worktree_histedit_abort(worktree, fileindex,
6790 repo, branch, base_commit_id,
6791 update_progress, &did_something);
6792 goto done;
6797 error = histedit_save_list(&histedit_cmds, worktree,
6798 repo);
6799 if (error) {
6800 got_worktree_histedit_abort(worktree, fileindex,
6801 repo, branch, base_commit_id,
6802 update_progress, &did_something);
6803 goto done;
6808 error = histedit_check_script(&histedit_cmds, &commits, repo);
6809 if (error)
6810 goto done;
6812 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6813 if (resume_commit_id) {
6814 if (got_object_id_cmp(hle->commit_id,
6815 resume_commit_id) != 0)
6816 continue;
6818 resume_commit_id = NULL;
6819 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6820 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6821 error = histedit_skip_commit(hle, worktree,
6822 repo);
6823 if (error)
6824 goto done;
6825 } else {
6826 struct got_pathlist_head paths;
6827 int have_changes = 0;
6829 TAILQ_INIT(&paths);
6830 error = got_pathlist_append(&paths, "", NULL);
6831 if (error)
6832 goto done;
6833 error = got_worktree_status(worktree, &paths,
6834 repo, check_local_changes, &have_changes,
6835 check_cancelled, NULL);
6836 got_pathlist_free(&paths);
6837 if (error) {
6838 if (error->code != GOT_ERR_CANCELLED)
6839 goto done;
6840 if (sigint_received || sigpipe_received)
6841 goto done;
6843 if (have_changes) {
6844 error = histedit_commit(NULL, worktree,
6845 fileindex, tmp_branch, hle, repo);
6846 if (error)
6847 goto done;
6848 } else {
6849 error = got_object_open_as_commit(
6850 &commit, repo, hle->commit_id);
6851 if (error)
6852 goto done;
6853 error = show_histedit_progress(commit,
6854 hle, NULL);
6855 got_object_commit_close(commit);
6856 commit = NULL;
6857 if (error)
6858 goto done;
6861 continue;
6864 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6865 error = histedit_skip_commit(hle, worktree, repo);
6866 if (error)
6867 goto done;
6868 continue;
6871 error = got_object_open_as_commit(&commit, repo,
6872 hle->commit_id);
6873 if (error)
6874 goto done;
6875 parent_ids = got_object_commit_get_parent_ids(commit);
6876 pid = SIMPLEQ_FIRST(parent_ids);
6878 error = got_worktree_histedit_merge_files(&merged_paths,
6879 worktree, fileindex, pid->id, hle->commit_id, repo,
6880 rebase_progress, &rebase_status, check_cancelled, NULL);
6881 if (error)
6882 goto done;
6883 got_object_commit_close(commit);
6884 commit = NULL;
6886 if (rebase_status == GOT_STATUS_CONFLICT) {
6887 error = show_rebase_merge_conflict(hle->commit_id,
6888 repo);
6889 if (error)
6890 goto done;
6891 got_worktree_rebase_pathlist_free(&merged_paths);
6892 break;
6895 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6896 char *id_str;
6897 error = got_object_id_str(&id_str, hle->commit_id);
6898 if (error)
6899 goto done;
6900 printf("Stopping histedit for amending commit %s\n",
6901 id_str);
6902 free(id_str);
6903 got_worktree_rebase_pathlist_free(&merged_paths);
6904 error = got_worktree_histedit_postpone(worktree,
6905 fileindex);
6906 goto done;
6909 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6910 error = histedit_skip_commit(hle, worktree, repo);
6911 if (error)
6912 goto done;
6913 continue;
6916 error = histedit_commit(&merged_paths, worktree, fileindex,
6917 tmp_branch, hle, repo);
6918 got_worktree_rebase_pathlist_free(&merged_paths);
6919 if (error)
6920 goto done;
6923 if (rebase_status == GOT_STATUS_CONFLICT) {
6924 error = got_worktree_histedit_postpone(worktree, fileindex);
6925 if (error)
6926 goto done;
6927 error = got_error_msg(GOT_ERR_CONFLICTS,
6928 "conflicts must be resolved before histedit can continue");
6929 } else
6930 error = histedit_complete(worktree, fileindex, tmp_branch,
6931 branch, repo);
6932 done:
6933 got_object_id_queue_free(&commits);
6934 histedit_free_list(&histedit_cmds);
6935 free(head_commit_id);
6936 free(base_commit_id);
6937 free(resume_commit_id);
6938 if (commit)
6939 got_object_commit_close(commit);
6940 if (branch)
6941 got_ref_close(branch);
6942 if (tmp_branch)
6943 got_ref_close(tmp_branch);
6944 if (worktree)
6945 got_worktree_close(worktree);
6946 if (repo)
6947 got_repo_close(repo);
6948 return error;
6951 __dead static void
6952 usage_integrate(void)
6954 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6955 exit(1);
6958 static const struct got_error *
6959 cmd_integrate(int argc, char *argv[])
6961 const struct got_error *error = NULL;
6962 struct got_repository *repo = NULL;
6963 struct got_worktree *worktree = NULL;
6964 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6965 const char *branch_arg = NULL;
6966 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6967 struct got_fileindex *fileindex = NULL;
6968 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6969 int ch, did_something = 0;
6971 while ((ch = getopt(argc, argv, "")) != -1) {
6972 switch (ch) {
6973 default:
6974 usage_integrate();
6975 /* NOTREACHED */
6979 argc -= optind;
6980 argv += optind;
6982 if (argc != 1)
6983 usage_integrate();
6984 branch_arg = argv[0];
6986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6987 "unveil", NULL) == -1)
6988 err(1, "pledge");
6990 cwd = getcwd(NULL, 0);
6991 if (cwd == NULL) {
6992 error = got_error_from_errno("getcwd");
6993 goto done;
6996 error = got_worktree_open(&worktree, cwd);
6997 if (error)
6998 goto done;
7000 error = check_rebase_or_histedit_in_progress(worktree);
7001 if (error)
7002 goto done;
7004 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7005 NULL);
7006 if (error != NULL)
7007 goto done;
7009 error = apply_unveil(got_repo_get_path(repo), 0,
7010 got_worktree_get_root_path(worktree));
7011 if (error)
7012 goto done;
7014 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7015 error = got_error_from_errno("asprintf");
7016 goto done;
7019 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7020 &base_branch_ref, worktree, refname, repo);
7021 if (error)
7022 goto done;
7024 refname = strdup(got_ref_get_name(branch_ref));
7025 if (refname == NULL) {
7026 error = got_error_from_errno("strdup");
7027 got_worktree_integrate_abort(worktree, fileindex, repo,
7028 branch_ref, base_branch_ref);
7029 goto done;
7031 base_refname = strdup(got_ref_get_name(base_branch_ref));
7032 if (base_refname == NULL) {
7033 error = got_error_from_errno("strdup");
7034 got_worktree_integrate_abort(worktree, fileindex, repo,
7035 branch_ref, base_branch_ref);
7036 goto done;
7039 error = got_ref_resolve(&commit_id, repo, branch_ref);
7040 if (error)
7041 goto done;
7043 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7044 if (error)
7045 goto done;
7047 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7048 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7049 "specified branch has already been integrated");
7050 got_worktree_integrate_abort(worktree, fileindex, repo,
7051 branch_ref, base_branch_ref);
7052 goto done;
7055 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7056 if (error) {
7057 if (error->code == GOT_ERR_ANCESTRY)
7058 error = got_error(GOT_ERR_REBASE_REQUIRED);
7059 got_worktree_integrate_abort(worktree, fileindex, repo,
7060 branch_ref, base_branch_ref);
7061 goto done;
7064 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7065 branch_ref, base_branch_ref, update_progress, &did_something,
7066 check_cancelled, NULL);
7067 if (error)
7068 goto done;
7070 printf("Integrated %s into %s\n", refname, base_refname);
7071 done:
7072 if (repo)
7073 got_repo_close(repo);
7074 if (worktree)
7075 got_worktree_close(worktree);
7076 free(cwd);
7077 free(base_commit_id);
7078 free(commit_id);
7079 free(refname);
7080 free(base_refname);
7081 return error;
7084 __dead static void
7085 usage_stage(void)
7087 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7088 "[file-path ...]\n",
7089 getprogname());
7090 exit(1);
7093 static const struct got_error *
7094 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7095 const char *path, struct got_object_id *blob_id,
7096 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7097 int dirfd, const char *de_name)
7099 const struct got_error *err = NULL;
7100 char *id_str = NULL;
7102 if (staged_status != GOT_STATUS_ADD &&
7103 staged_status != GOT_STATUS_MODIFY &&
7104 staged_status != GOT_STATUS_DELETE)
7105 return NULL;
7107 if (staged_status == GOT_STATUS_ADD ||
7108 staged_status == GOT_STATUS_MODIFY)
7109 err = got_object_id_str(&id_str, staged_blob_id);
7110 else
7111 err = got_object_id_str(&id_str, blob_id);
7112 if (err)
7113 return err;
7115 printf("%s %c %s\n", id_str, staged_status, path);
7116 free(id_str);
7117 return NULL;
7120 static const struct got_error *
7121 cmd_stage(int argc, char *argv[])
7123 const struct got_error *error = NULL;
7124 struct got_repository *repo = NULL;
7125 struct got_worktree *worktree = NULL;
7126 char *cwd = NULL;
7127 struct got_pathlist_head paths;
7128 struct got_pathlist_entry *pe;
7129 int ch, list_stage = 0, pflag = 0;
7130 FILE *patch_script_file = NULL;
7131 const char *patch_script_path = NULL;
7132 struct choose_patch_arg cpa;
7134 TAILQ_INIT(&paths);
7136 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7137 switch (ch) {
7138 case 'l':
7139 list_stage = 1;
7140 break;
7141 case 'p':
7142 pflag = 1;
7143 break;
7144 case 'F':
7145 patch_script_path = optarg;
7146 break;
7147 default:
7148 usage_stage();
7149 /* NOTREACHED */
7153 argc -= optind;
7154 argv += optind;
7156 #ifndef PROFILE
7157 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7158 "unveil", NULL) == -1)
7159 err(1, "pledge");
7160 #endif
7161 if (list_stage && (pflag || patch_script_path))
7162 errx(1, "-l option cannot be used with other options");
7163 if (patch_script_path && !pflag)
7164 errx(1, "-F option can only be used together with -p option");
7166 cwd = getcwd(NULL, 0);
7167 if (cwd == NULL) {
7168 error = got_error_from_errno("getcwd");
7169 goto done;
7172 error = got_worktree_open(&worktree, cwd);
7173 if (error)
7174 goto done;
7176 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7177 NULL);
7178 if (error != NULL)
7179 goto done;
7181 if (patch_script_path) {
7182 patch_script_file = fopen(patch_script_path, "r");
7183 if (patch_script_file == NULL) {
7184 error = got_error_from_errno2("fopen",
7185 patch_script_path);
7186 goto done;
7189 error = apply_unveil(got_repo_get_path(repo), 0,
7190 got_worktree_get_root_path(worktree));
7191 if (error)
7192 goto done;
7194 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7195 if (error)
7196 goto done;
7198 if (list_stage)
7199 error = got_worktree_status(worktree, &paths, repo,
7200 print_stage, NULL, check_cancelled, NULL);
7201 else {
7202 cpa.patch_script_file = patch_script_file;
7203 cpa.action = "stage";
7204 error = got_worktree_stage(worktree, &paths,
7205 pflag ? NULL : print_status, NULL,
7206 pflag ? choose_patch : NULL, &cpa, repo);
7208 done:
7209 if (patch_script_file && fclose(patch_script_file) == EOF &&
7210 error == NULL)
7211 error = got_error_from_errno2("fclose", patch_script_path);
7212 if (repo)
7213 got_repo_close(repo);
7214 if (worktree)
7215 got_worktree_close(worktree);
7216 TAILQ_FOREACH(pe, &paths, entry)
7217 free((char *)pe->path);
7218 got_pathlist_free(&paths);
7219 free(cwd);
7220 return error;
7223 __dead static void
7224 usage_unstage(void)
7226 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7227 "[file-path ...]\n",
7228 getprogname());
7229 exit(1);
7233 static const struct got_error *
7234 cmd_unstage(int argc, char *argv[])
7236 const struct got_error *error = NULL;
7237 struct got_repository *repo = NULL;
7238 struct got_worktree *worktree = NULL;
7239 char *cwd = NULL;
7240 struct got_pathlist_head paths;
7241 struct got_pathlist_entry *pe;
7242 int ch, did_something = 0, pflag = 0;
7243 FILE *patch_script_file = NULL;
7244 const char *patch_script_path = NULL;
7245 struct choose_patch_arg cpa;
7247 TAILQ_INIT(&paths);
7249 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7250 switch (ch) {
7251 case 'p':
7252 pflag = 1;
7253 break;
7254 case 'F':
7255 patch_script_path = optarg;
7256 break;
7257 default:
7258 usage_unstage();
7259 /* NOTREACHED */
7263 argc -= optind;
7264 argv += optind;
7266 #ifndef PROFILE
7267 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7268 "unveil", NULL) == -1)
7269 err(1, "pledge");
7270 #endif
7271 if (patch_script_path && !pflag)
7272 errx(1, "-F option can only be used together with -p option");
7274 cwd = getcwd(NULL, 0);
7275 if (cwd == NULL) {
7276 error = got_error_from_errno("getcwd");
7277 goto done;
7280 error = got_worktree_open(&worktree, cwd);
7281 if (error)
7282 goto done;
7284 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7285 NULL);
7286 if (error != NULL)
7287 goto done;
7289 if (patch_script_path) {
7290 patch_script_file = fopen(patch_script_path, "r");
7291 if (patch_script_file == NULL) {
7292 error = got_error_from_errno2("fopen",
7293 patch_script_path);
7294 goto done;
7298 error = apply_unveil(got_repo_get_path(repo), 0,
7299 got_worktree_get_root_path(worktree));
7300 if (error)
7301 goto done;
7303 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7304 if (error)
7305 goto done;
7307 cpa.patch_script_file = patch_script_file;
7308 cpa.action = "unstage";
7309 error = got_worktree_unstage(worktree, &paths, update_progress,
7310 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7311 done:
7312 if (patch_script_file && fclose(patch_script_file) == EOF &&
7313 error == NULL)
7314 error = got_error_from_errno2("fclose", patch_script_path);
7315 if (repo)
7316 got_repo_close(repo);
7317 if (worktree)
7318 got_worktree_close(worktree);
7319 TAILQ_FOREACH(pe, &paths, entry)
7320 free((char *)pe->path);
7321 got_pathlist_free(&paths);
7322 free(cwd);
7323 return error;
7326 __dead static void
7327 usage_cat(void)
7329 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7330 "arg1 [arg2 ...]\n", getprogname());
7331 exit(1);
7334 static const struct got_error *
7335 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7337 const struct got_error *err;
7338 struct got_blob_object *blob;
7340 err = got_object_open_as_blob(&blob, repo, id, 8192);
7341 if (err)
7342 return err;
7344 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7345 got_object_blob_close(blob);
7346 return err;
7349 static const struct got_error *
7350 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7352 const struct got_error *err;
7353 struct got_tree_object *tree;
7354 int nentries, i;
7356 err = got_object_open_as_tree(&tree, repo, id);
7357 if (err)
7358 return err;
7360 nentries = got_object_tree_get_nentries(tree);
7361 for (i = 0; i < nentries; i++) {
7362 struct got_tree_entry *te;
7363 char *id_str;
7364 if (sigint_received || sigpipe_received)
7365 break;
7366 te = got_object_tree_get_entry(tree, i);
7367 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7368 if (err)
7369 break;
7370 fprintf(outfile, "%s %.7o %s\n", id_str,
7371 got_tree_entry_get_mode(te),
7372 got_tree_entry_get_name(te));
7373 free(id_str);
7376 got_object_tree_close(tree);
7377 return err;
7380 static const struct got_error *
7381 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7383 const struct got_error *err;
7384 struct got_commit_object *commit;
7385 const struct got_object_id_queue *parent_ids;
7386 struct got_object_qid *pid;
7387 char *id_str = NULL;
7388 const char *logmsg = NULL;
7390 err = got_object_open_as_commit(&commit, repo, id);
7391 if (err)
7392 return err;
7394 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7395 if (err)
7396 goto done;
7398 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7399 parent_ids = got_object_commit_get_parent_ids(commit);
7400 fprintf(outfile, "numparents %d\n",
7401 got_object_commit_get_nparents(commit));
7402 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7403 char *pid_str;
7404 err = got_object_id_str(&pid_str, pid->id);
7405 if (err)
7406 goto done;
7407 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7408 free(pid_str);
7410 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7411 got_object_commit_get_author(commit),
7412 got_object_commit_get_author_time(commit));
7414 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7415 got_object_commit_get_author(commit),
7416 got_object_commit_get_committer_time(commit));
7418 logmsg = got_object_commit_get_logmsg_raw(commit);
7419 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7420 fprintf(outfile, "%s", logmsg);
7421 done:
7422 free(id_str);
7423 got_object_commit_close(commit);
7424 return err;
7427 static const struct got_error *
7428 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7430 const struct got_error *err;
7431 struct got_tag_object *tag;
7432 char *id_str = NULL;
7433 const char *tagmsg = NULL;
7435 err = got_object_open_as_tag(&tag, repo, id);
7436 if (err)
7437 return err;
7439 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7440 if (err)
7441 goto done;
7443 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7445 switch (got_object_tag_get_object_type(tag)) {
7446 case GOT_OBJ_TYPE_BLOB:
7447 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7448 GOT_OBJ_LABEL_BLOB);
7449 break;
7450 case GOT_OBJ_TYPE_TREE:
7451 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7452 GOT_OBJ_LABEL_TREE);
7453 break;
7454 case GOT_OBJ_TYPE_COMMIT:
7455 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7456 GOT_OBJ_LABEL_COMMIT);
7457 break;
7458 case GOT_OBJ_TYPE_TAG:
7459 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7460 GOT_OBJ_LABEL_TAG);
7461 break;
7462 default:
7463 break;
7466 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7467 got_object_tag_get_name(tag));
7469 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7470 got_object_tag_get_tagger(tag),
7471 got_object_tag_get_tagger_time(tag));
7473 tagmsg = got_object_tag_get_message(tag);
7474 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7475 fprintf(outfile, "%s", tagmsg);
7476 done:
7477 free(id_str);
7478 got_object_tag_close(tag);
7479 return err;
7482 static const struct got_error *
7483 cmd_cat(int argc, char *argv[])
7485 const struct got_error *error;
7486 struct got_repository *repo = NULL;
7487 struct got_worktree *worktree = NULL;
7488 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7489 const char *commit_id_str = NULL;
7490 struct got_object_id *id = NULL, *commit_id = NULL;
7491 int ch, obj_type, i, force_path = 0;
7493 #ifndef PROFILE
7494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7495 NULL) == -1)
7496 err(1, "pledge");
7497 #endif
7499 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7500 switch (ch) {
7501 case 'c':
7502 commit_id_str = optarg;
7503 break;
7504 case 'r':
7505 repo_path = realpath(optarg, NULL);
7506 if (repo_path == NULL)
7507 return got_error_from_errno2("realpath",
7508 optarg);
7509 got_path_strip_trailing_slashes(repo_path);
7510 break;
7511 case 'P':
7512 force_path = 1;
7513 break;
7514 default:
7515 usage_cat();
7516 /* NOTREACHED */
7520 argc -= optind;
7521 argv += optind;
7523 cwd = getcwd(NULL, 0);
7524 if (cwd == NULL) {
7525 error = got_error_from_errno("getcwd");
7526 goto done;
7528 error = got_worktree_open(&worktree, cwd);
7529 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7530 goto done;
7531 if (worktree) {
7532 if (repo_path == NULL) {
7533 repo_path = strdup(
7534 got_worktree_get_repo_path(worktree));
7535 if (repo_path == NULL) {
7536 error = got_error_from_errno("strdup");
7537 goto done;
7542 if (repo_path == NULL) {
7543 repo_path = getcwd(NULL, 0);
7544 if (repo_path == NULL)
7545 return got_error_from_errno("getcwd");
7548 error = got_repo_open(&repo, repo_path, NULL);
7549 free(repo_path);
7550 if (error != NULL)
7551 goto done;
7553 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7554 if (error)
7555 goto done;
7557 if (commit_id_str == NULL)
7558 commit_id_str = GOT_REF_HEAD;
7559 error = got_repo_match_object_id(&commit_id, NULL,
7560 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7561 if (error)
7562 goto done;
7564 for (i = 0; i < argc; i++) {
7565 if (force_path) {
7566 error = got_object_id_by_path(&id, repo, commit_id,
7567 argv[i]);
7568 if (error)
7569 break;
7570 } else {
7571 error = got_repo_match_object_id(&id, &label, argv[i],
7572 GOT_OBJ_TYPE_ANY, 0, repo);
7573 if (error) {
7574 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7575 error->code != GOT_ERR_NOT_REF)
7576 break;
7577 error = got_object_id_by_path(&id, repo,
7578 commit_id, argv[i]);
7579 if (error)
7580 break;
7584 error = got_object_get_type(&obj_type, repo, id);
7585 if (error)
7586 break;
7588 switch (obj_type) {
7589 case GOT_OBJ_TYPE_BLOB:
7590 error = cat_blob(id, repo, stdout);
7591 break;
7592 case GOT_OBJ_TYPE_TREE:
7593 error = cat_tree(id, repo, stdout);
7594 break;
7595 case GOT_OBJ_TYPE_COMMIT:
7596 error = cat_commit(id, repo, stdout);
7597 break;
7598 case GOT_OBJ_TYPE_TAG:
7599 error = cat_tag(id, repo, stdout);
7600 break;
7601 default:
7602 error = got_error(GOT_ERR_OBJ_TYPE);
7603 break;
7605 if (error)
7606 break;
7607 free(label);
7608 label = NULL;
7609 free(id);
7610 id = NULL;
7612 done:
7613 free(label);
7614 free(id);
7615 free(commit_id);
7616 if (worktree)
7617 got_worktree_close(worktree);
7618 if (repo) {
7619 const struct got_error *repo_error;
7620 repo_error = got_repo_close(repo);
7621 if (error == NULL)
7622 error = repo_error;
7624 return error;