Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
301 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 done:
362 free(repo_path);
363 return error;
366 __dead static void
367 usage_import(void)
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
371 exit(1);
374 int
375 spawn_editor(const char *editor, const char *file)
377 pid_t pid;
378 sig_t sighup, sigint, sigquit;
379 int st = -1;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
386 case -1:
387 goto doneediting;
388 case 0:
389 execl(editor, editor, file, (char *)NULL);
390 _exit(127);
393 while (waitpid(pid, &st, 0) == -1)
394 if (errno != EINTR)
395 break;
397 doneediting:
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
403 errno = EINTR;
404 return -1;
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
415 char buf[1024];
416 struct stat st, st2;
417 FILE *fp;
418 int content_changed = 0;
419 size_t len;
421 *logmsg = NULL;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
437 if (*logmsg == NULL)
438 return got_error_from_errno("malloc");
439 (*logmsg)[0] = '\0';
440 len = 0;
442 fp = fopen(logmsg_path, "r");
443 if (fp == NULL) {
444 err = got_error_from_errno("fopen");
445 goto done;
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
449 content_changed = 1;
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
454 fclose(fp);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
458 len--;
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
464 done:
465 if (err) {
466 free(*logmsg);
467 *logmsg = NULL;
469 return err;
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
478 int fd;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
482 branch_name) == -1)
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd,
486 GOT_TMPDIR_STR "/got-importmsg");
487 if (err)
488 goto done;
490 dprintf(fd, initial_content);
491 close(fd);
493 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 done:
495 free(initial_content);
496 return err;
499 static const struct got_error *
500 import_progress(void *arg, const char *path)
502 printf("A %s\n", path);
503 return NULL;
506 static const struct got_error *
507 get_author(char **author, struct got_repository *repo)
509 const struct got_error *err = NULL;
510 const char *got_author, *name, *email;
512 *author = NULL;
514 name = got_repo_get_gitconfig_author_name(repo);
515 email = got_repo_get_gitconfig_author_email(repo);
516 if (name && email) {
517 if (asprintf(author, "%s <%s>", name, email) == -1)
518 return got_error_from_errno("asprintf");
519 return NULL;
522 got_author = getenv("GOT_AUTHOR");
523 if (got_author == NULL) {
524 name = got_repo_get_global_gitconfig_author_name(repo);
525 email = got_repo_get_global_gitconfig_author_email(repo);
526 if (name && email) {
527 if (asprintf(author, "%s <%s>", name, email) == -1)
528 return got_error_from_errno("asprintf");
529 return NULL;
531 /* TODO: Look up user in password database? */
532 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
535 *author = strdup(got_author);
536 if (*author == NULL)
537 return got_error_from_errno("strdup");
539 /*
540 * Really dumb email address check; we're only doing this to
541 * avoid git's object parser breaking on commits we create.
542 */
543 while (*got_author && *got_author != '<')
544 got_author++;
545 if (*got_author != '<') {
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 goto done;
549 while (*got_author && *got_author != '@')
550 got_author++;
551 if (*got_author != '@') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '>')
556 got_author++;
557 if (*got_author != '>')
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 done:
560 if (err) {
561 free(*author);
562 *author = NULL;
564 return err;
567 static const struct got_error *
568 get_gitconfig_path(char **gitconfig_path)
570 const char *homedir = getenv("HOME");
572 *gitconfig_path = NULL;
573 if (homedir) {
574 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
575 return got_error_from_errno("asprintf");
578 return NULL;
581 static const struct got_error *
582 cmd_import(int argc, char *argv[])
584 const struct got_error *error = NULL;
585 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
586 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
587 const char *branch_name = "main";
588 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
589 struct got_repository *repo = NULL;
590 struct got_reference *branch_ref = NULL, *head_ref = NULL;
591 struct got_object_id *new_commit_id = NULL;
592 int ch;
593 struct got_pathlist_head ignores;
594 struct got_pathlist_entry *pe;
595 int preserve_logmsg = 0;
597 TAILQ_INIT(&ignores);
599 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
600 switch (ch) {
601 case 'b':
602 branch_name = optarg;
603 break;
604 case 'm':
605 logmsg = strdup(optarg);
606 if (logmsg == NULL) {
607 error = got_error_from_errno("strdup");
608 goto done;
610 break;
611 case 'r':
612 repo_path = realpath(optarg, NULL);
613 if (repo_path == NULL) {
614 error = got_error_from_errno2("realpath",
615 optarg);
616 goto done;
618 break;
619 case 'I':
620 if (optarg[0] == '\0')
621 break;
622 error = got_pathlist_insert(&pe, &ignores, optarg,
623 NULL);
624 if (error)
625 goto done;
626 break;
627 default:
628 usage_import();
629 /* NOTREACHED */
633 argc -= optind;
634 argv += optind;
636 #ifndef PROFILE
637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
638 "unveil",
639 NULL) == -1)
640 err(1, "pledge");
641 #endif
642 if (argc != 1)
643 usage_import();
645 if (repo_path == NULL) {
646 repo_path = getcwd(NULL, 0);
647 if (repo_path == NULL)
648 return got_error_from_errno("getcwd");
650 got_path_strip_trailing_slashes(repo_path);
651 error = get_gitconfig_path(&gitconfig_path);
652 if (error)
653 goto done;
654 error = got_repo_open(&repo, repo_path, gitconfig_path);
655 if (error)
656 goto done;
658 error = get_author(&author, repo);
659 if (error)
660 return error;
662 /*
663 * Don't let the user create a branch name with a leading '-'.
664 * While technically a valid reference name, this case is usually
665 * an unintended typo.
666 */
667 if (branch_name[0] == '-')
668 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
670 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
671 error = got_error_from_errno("asprintf");
672 goto done;
675 error = got_ref_open(&branch_ref, repo, refname, 0);
676 if (error) {
677 if (error->code != GOT_ERR_NOT_REF)
678 goto done;
679 } else {
680 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
681 "import target branch already exists");
682 goto done;
685 path_dir = realpath(argv[0], NULL);
686 if (path_dir == NULL) {
687 error = got_error_from_errno2("realpath", argv[0]);
688 goto done;
690 got_path_strip_trailing_slashes(path_dir);
692 /*
693 * unveil(2) traverses exec(2); if an editor is used we have
694 * to apply unveil after the log message has been written.
695 */
696 if (logmsg == NULL || strlen(logmsg) == 0) {
697 error = get_editor(&editor);
698 if (error)
699 goto done;
700 free(logmsg);
701 error = collect_import_msg(&logmsg, &logmsg_path, editor,
702 path_dir, refname);
703 if (error) {
704 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
705 logmsg_path != NULL)
706 preserve_logmsg = 1;
707 goto done;
711 if (unveil(path_dir, "r") != 0) {
712 error = got_error_from_errno2("unveil", path_dir);
713 if (logmsg_path)
714 preserve_logmsg = 1;
715 goto done;
718 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
719 if (error) {
720 if (logmsg_path)
721 preserve_logmsg = 1;
722 goto done;
725 error = got_repo_import(&new_commit_id, path_dir, logmsg,
726 author, &ignores, repo, import_progress, NULL);
727 if (error) {
728 if (logmsg_path)
729 preserve_logmsg = 1;
730 goto done;
733 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
734 if (error) {
735 if (logmsg_path)
736 preserve_logmsg = 1;
737 goto done;
740 error = got_ref_write(branch_ref, repo);
741 if (error) {
742 if (logmsg_path)
743 preserve_logmsg = 1;
744 goto done;
747 error = got_object_id_str(&id_str, new_commit_id);
748 if (error) {
749 if (logmsg_path)
750 preserve_logmsg = 1;
751 goto done;
754 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 if (error) {
756 if (error->code != GOT_ERR_NOT_REF) {
757 if (logmsg_path)
758 preserve_logmsg = 1;
759 goto done;
762 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
763 branch_ref);
764 if (error) {
765 if (logmsg_path)
766 preserve_logmsg = 1;
767 goto done;
770 error = got_ref_write(head_ref, repo);
771 if (error) {
772 if (logmsg_path)
773 preserve_logmsg = 1;
774 goto done;
778 printf("Created branch %s with commit %s\n",
779 got_ref_get_name(branch_ref), id_str);
780 done:
781 if (preserve_logmsg) {
782 fprintf(stderr, "%s: log message preserved in %s\n",
783 getprogname(), logmsg_path);
784 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
785 error = got_error_from_errno2("unlink", logmsg_path);
786 free(logmsg);
787 free(logmsg_path);
788 free(repo_path);
789 free(editor);
790 free(refname);
791 free(new_commit_id);
792 free(id_str);
793 free(author);
794 free(gitconfig_path);
795 if (branch_ref)
796 got_ref_close(branch_ref);
797 if (head_ref)
798 got_ref_close(head_ref);
799 return error;
802 __dead static void
803 usage_checkout(void)
805 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
806 "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 exit(1);
810 static void
811 show_worktree_base_ref_warning(void)
813 fprintf(stderr, "%s: warning: could not create a reference "
814 "to the work tree's base commit; the commit could be "
815 "garbage-collected by Git; making the repository "
816 "writable and running 'got update' will prevent this\n",
817 getprogname());
820 struct got_checkout_progress_arg {
821 const char *worktree_path;
822 int had_base_commit_ref_error;
823 };
825 static const struct got_error *
826 checkout_progress(void *arg, unsigned char status, const char *path)
828 struct got_checkout_progress_arg *a = arg;
830 /* Base commit bump happens silently. */
831 if (status == GOT_STATUS_BUMP_BASE)
832 return NULL;
834 if (status == GOT_STATUS_BASE_REF_ERR) {
835 a->had_base_commit_ref_error = 1;
836 return NULL;
839 while (path[0] == '/')
840 path++;
842 printf("%c %s/%s\n", status, a->worktree_path, path);
843 return NULL;
846 static const struct got_error *
847 check_cancelled(void *arg)
849 if (sigint_received || sigpipe_received)
850 return got_error(GOT_ERR_CANCELLED);
851 return NULL;
854 static const struct got_error *
855 check_linear_ancestry(struct got_object_id *commit_id,
856 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
857 struct got_repository *repo)
859 const struct got_error *err = NULL;
860 struct got_object_id *yca_id;
862 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
863 commit_id, base_commit_id, repo, check_cancelled, NULL);
864 if (err)
865 return err;
867 if (yca_id == NULL)
868 return got_error(GOT_ERR_ANCESTRY);
870 /*
871 * Require a straight line of history between the target commit
872 * and the work tree's base commit.
874 * Non-linear situations such as this require a rebase:
876 * (commit) D F (base_commit)
877 * \ /
878 * C E
879 * \ /
880 * B (yca)
881 * |
882 * A
884 * 'got update' only handles linear cases:
885 * Update forwards in time: A (base/yca) - B - C - D (commit)
886 * Update backwards in time: D (base) - C - B - A (commit/yca)
887 */
888 if (allow_forwards_in_time_only) {
889 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
891 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
892 got_object_id_cmp(base_commit_id, yca_id) != 0)
893 return got_error(GOT_ERR_ANCESTRY);
895 free(yca_id);
896 return NULL;
899 static const struct got_error *
900 check_same_branch(struct got_object_id *commit_id,
901 struct got_reference *head_ref, struct got_object_id *yca_id,
902 struct got_repository *repo)
904 const struct got_error *err = NULL;
905 struct got_commit_graph *graph = NULL;
906 struct got_object_id *head_commit_id = NULL;
907 int is_same_branch = 0;
909 err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 if (err)
911 goto done;
913 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 is_same_branch = 1;
915 goto done;
917 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
918 is_same_branch = 1;
919 goto done;
922 err = got_commit_graph_open(&graph, "/", 1);
923 if (err)
924 goto done;
926 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
927 check_cancelled, NULL);
928 if (err)
929 goto done;
931 for (;;) {
932 struct got_object_id *id;
933 err = got_commit_graph_iter_next(&id, graph, repo,
934 check_cancelled, NULL);
935 if (err) {
936 if (err->code == GOT_ERR_ITER_COMPLETED)
937 err = NULL;
938 break;
941 if (id) {
942 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 break;
944 if (got_object_id_cmp(id, commit_id) == 0) {
945 is_same_branch = 1;
946 break;
950 done:
951 if (graph)
952 got_commit_graph_close(graph);
953 free(head_commit_id);
954 if (!err && !is_same_branch)
955 err = got_error(GOT_ERR_ANCESTRY);
956 return err;
959 static const struct got_error *
960 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
962 static char msg[512];
963 const char *branch_name;
965 if (got_ref_is_symbolic(ref))
966 branch_name = got_ref_get_symref_target(ref);
967 else
968 branch_name = got_ref_get_name(ref);
970 if (strncmp("refs/heads/", branch_name, 11) == 0)
971 branch_name += 11;
973 snprintf(msg, sizeof(msg),
974 "target commit is not contained in branch '%s'; "
975 "the branch to use must be specified with -b; "
976 "if necessary a new branch can be created for "
977 "this commit with 'got branch -c %s BRANCH_NAME'",
978 branch_name, commit_id_str);
980 return got_error_msg(GOT_ERR_ANCESTRY, msg);
983 static const struct got_error *
984 cmd_checkout(int argc, char *argv[])
986 const struct got_error *error = NULL;
987 struct got_repository *repo = NULL;
988 struct got_reference *head_ref = NULL;
989 struct got_worktree *worktree = NULL;
990 char *repo_path = NULL;
991 char *worktree_path = NULL;
992 const char *path_prefix = "";
993 const char *branch_name = GOT_REF_HEAD;
994 char *commit_id_str = NULL;
995 int ch, same_path_prefix, allow_nonempty = 0;
996 struct got_pathlist_head paths;
997 struct got_checkout_progress_arg cpa;
999 TAILQ_INIT(&paths);
1001 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1002 switch (ch) {
1003 case 'b':
1004 branch_name = optarg;
1005 break;
1006 case 'c':
1007 commit_id_str = strdup(optarg);
1008 if (commit_id_str == NULL)
1009 return got_error_from_errno("strdup");
1010 break;
1011 case 'E':
1012 allow_nonempty = 1;
1013 break;
1014 case 'p':
1015 path_prefix = optarg;
1016 break;
1017 default:
1018 usage_checkout();
1019 /* NOTREACHED */
1023 argc -= optind;
1024 argv += optind;
1026 #ifndef PROFILE
1027 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1028 "unveil", NULL) == -1)
1029 err(1, "pledge");
1030 #endif
1031 if (argc == 1) {
1032 char *cwd, *base, *dotgit;
1033 repo_path = realpath(argv[0], NULL);
1034 if (repo_path == NULL)
1035 return got_error_from_errno2("realpath", argv[0]);
1036 cwd = getcwd(NULL, 0);
1037 if (cwd == NULL) {
1038 error = got_error_from_errno("getcwd");
1039 goto done;
1041 if (path_prefix[0]) {
1042 base = basename(path_prefix);
1043 if (base == NULL) {
1044 error = got_error_from_errno2("basename",
1045 path_prefix);
1046 goto done;
1048 } else {
1049 base = basename(repo_path);
1050 if (base == NULL) {
1051 error = got_error_from_errno2("basename",
1052 repo_path);
1053 goto done;
1056 dotgit = strstr(base, ".git");
1057 if (dotgit)
1058 *dotgit = '\0';
1059 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1060 error = got_error_from_errno("asprintf");
1061 free(cwd);
1062 goto done;
1064 free(cwd);
1065 } else if (argc == 2) {
1066 repo_path = realpath(argv[0], NULL);
1067 if (repo_path == NULL) {
1068 error = got_error_from_errno2("realpath", argv[0]);
1069 goto done;
1071 worktree_path = realpath(argv[1], NULL);
1072 if (worktree_path == NULL) {
1073 if (errno != ENOENT) {
1074 error = got_error_from_errno2("realpath",
1075 argv[1]);
1076 goto done;
1078 worktree_path = strdup(argv[1]);
1079 if (worktree_path == NULL) {
1080 error = got_error_from_errno("strdup");
1081 goto done;
1084 } else
1085 usage_checkout();
1087 got_path_strip_trailing_slashes(repo_path);
1088 got_path_strip_trailing_slashes(worktree_path);
1090 error = got_repo_open(&repo, repo_path, NULL);
1091 if (error != NULL)
1092 goto done;
1094 /* Pre-create work tree path for unveil(2) */
1095 error = got_path_mkdir(worktree_path);
1096 if (error) {
1097 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1098 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1099 goto done;
1100 if (!allow_nonempty &&
1101 !got_path_dir_is_empty(worktree_path)) {
1102 error = got_error_path(worktree_path,
1103 GOT_ERR_DIR_NOT_EMPTY);
1104 goto done;
1108 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1109 if (error)
1110 goto done;
1112 error = got_ref_open(&head_ref, repo, branch_name, 0);
1113 if (error != NULL)
1114 goto done;
1116 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1117 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1118 goto done;
1120 error = got_worktree_open(&worktree, worktree_path);
1121 if (error != NULL)
1122 goto done;
1124 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1125 path_prefix);
1126 if (error != NULL)
1127 goto done;
1128 if (!same_path_prefix) {
1129 error = got_error(GOT_ERR_PATH_PREFIX);
1130 goto done;
1133 if (commit_id_str) {
1134 struct got_object_id *commit_id;
1135 error = got_repo_match_object_id(&commit_id, NULL,
1136 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1137 if (error)
1138 goto done;
1139 error = check_linear_ancestry(commit_id,
1140 got_worktree_get_base_commit_id(worktree), 0, repo);
1141 if (error != NULL) {
1142 free(commit_id);
1143 if (error->code == GOT_ERR_ANCESTRY) {
1144 error = checkout_ancestry_error(
1145 head_ref, commit_id_str);
1147 goto done;
1149 error = check_same_branch(commit_id, head_ref, NULL, repo);
1150 if (error) {
1151 if (error->code == GOT_ERR_ANCESTRY) {
1152 error = checkout_ancestry_error(
1153 head_ref, commit_id_str);
1155 goto done;
1157 error = got_worktree_set_base_commit_id(worktree, repo,
1158 commit_id);
1159 free(commit_id);
1160 if (error)
1161 goto done;
1164 error = got_pathlist_append(&paths, "", NULL);
1165 if (error)
1166 goto done;
1167 cpa.worktree_path = worktree_path;
1168 cpa.had_base_commit_ref_error = 0;
1169 error = got_worktree_checkout_files(worktree, &paths, repo,
1170 checkout_progress, &cpa, check_cancelled, NULL);
1171 if (error != NULL)
1172 goto done;
1174 printf("Now shut up and hack\n");
1175 if (cpa.had_base_commit_ref_error)
1176 show_worktree_base_ref_warning();
1177 done:
1178 got_pathlist_free(&paths);
1179 free(commit_id_str);
1180 free(repo_path);
1181 free(worktree_path);
1182 return error;
1185 __dead static void
1186 usage_update(void)
1188 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1189 getprogname());
1190 exit(1);
1193 static const struct got_error *
1194 update_progress(void *arg, unsigned char status, const char *path)
1196 int *did_something = arg;
1198 if (status == GOT_STATUS_EXISTS ||
1199 status == GOT_STATUS_BASE_REF_ERR)
1200 return NULL;
1202 *did_something = 1;
1204 /* Base commit bump happens silently. */
1205 if (status == GOT_STATUS_BUMP_BASE)
1206 return NULL;
1208 while (path[0] == '/')
1209 path++;
1210 printf("%c %s\n", status, path);
1211 return NULL;
1214 static const struct got_error *
1215 switch_head_ref(struct got_reference *head_ref,
1216 struct got_object_id *commit_id, struct got_worktree *worktree,
1217 struct got_repository *repo)
1219 const struct got_error *err = NULL;
1220 char *base_id_str;
1221 int ref_has_moved = 0;
1223 /* Trivial case: switching between two different references. */
1224 if (strcmp(got_ref_get_name(head_ref),
1225 got_worktree_get_head_ref_name(worktree)) != 0) {
1226 printf("Switching work tree from %s to %s\n",
1227 got_worktree_get_head_ref_name(worktree),
1228 got_ref_get_name(head_ref));
1229 return got_worktree_set_head_ref(worktree, head_ref);
1232 err = check_linear_ancestry(commit_id,
1233 got_worktree_get_base_commit_id(worktree), 0, repo);
1234 if (err) {
1235 if (err->code != GOT_ERR_ANCESTRY)
1236 return err;
1237 ref_has_moved = 1;
1239 if (!ref_has_moved)
1240 return NULL;
1242 /* Switching to a rebased branch with the same reference name. */
1243 err = got_object_id_str(&base_id_str,
1244 got_worktree_get_base_commit_id(worktree));
1245 if (err)
1246 return err;
1247 printf("Reference %s now points at a different branch\n",
1248 got_worktree_get_head_ref_name(worktree));
1249 printf("Switching work tree from %s to %s\n", base_id_str,
1250 got_worktree_get_head_ref_name(worktree));
1251 return NULL;
1254 static const struct got_error *
1255 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1257 const struct got_error *err;
1258 int in_progress;
1260 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1261 if (err)
1262 return err;
1263 if (in_progress)
1264 return got_error(GOT_ERR_REBASING);
1266 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1267 if (err)
1268 return err;
1269 if (in_progress)
1270 return got_error(GOT_ERR_HISTEDIT_BUSY);
1272 return NULL;
1275 static const struct got_error *
1276 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1277 char *argv[], struct got_worktree *worktree)
1279 const struct got_error *err = NULL;
1280 char *path;
1281 int i;
1283 if (argc == 0) {
1284 path = strdup("");
1285 if (path == NULL)
1286 return got_error_from_errno("strdup");
1287 return got_pathlist_append(paths, path, NULL);
1290 for (i = 0; i < argc; i++) {
1291 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1292 if (err)
1293 break;
1294 err = got_pathlist_append(paths, path, NULL);
1295 if (err) {
1296 free(path);
1297 break;
1301 return err;
1304 static const struct got_error *
1305 cmd_update(int argc, char *argv[])
1307 const struct got_error *error = NULL;
1308 struct got_repository *repo = NULL;
1309 struct got_worktree *worktree = NULL;
1310 char *worktree_path = NULL;
1311 struct got_object_id *commit_id = NULL;
1312 char *commit_id_str = NULL;
1313 const char *branch_name = NULL;
1314 struct got_reference *head_ref = NULL;
1315 struct got_pathlist_head paths;
1316 struct got_pathlist_entry *pe;
1317 int ch, did_something = 0;
1319 TAILQ_INIT(&paths);
1321 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1322 switch (ch) {
1323 case 'b':
1324 branch_name = optarg;
1325 break;
1326 case 'c':
1327 commit_id_str = strdup(optarg);
1328 if (commit_id_str == NULL)
1329 return got_error_from_errno("strdup");
1330 break;
1331 default:
1332 usage_update();
1333 /* NOTREACHED */
1337 argc -= optind;
1338 argv += optind;
1340 #ifndef PROFILE
1341 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1342 "unveil", NULL) == -1)
1343 err(1, "pledge");
1344 #endif
1345 worktree_path = getcwd(NULL, 0);
1346 if (worktree_path == NULL) {
1347 error = got_error_from_errno("getcwd");
1348 goto done;
1350 error = got_worktree_open(&worktree, worktree_path);
1351 if (error)
1352 goto done;
1354 error = check_rebase_or_histedit_in_progress(worktree);
1355 if (error)
1356 goto done;
1358 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1359 NULL);
1360 if (error != NULL)
1361 goto done;
1363 error = apply_unveil(got_repo_get_path(repo), 0,
1364 got_worktree_get_root_path(worktree));
1365 if (error)
1366 goto done;
1368 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1369 if (error)
1370 goto done;
1372 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1373 got_worktree_get_head_ref_name(worktree), 0);
1374 if (error != NULL)
1375 goto done;
1376 if (commit_id_str == NULL) {
1377 error = got_ref_resolve(&commit_id, repo, head_ref);
1378 if (error != NULL)
1379 goto done;
1380 error = got_object_id_str(&commit_id_str, commit_id);
1381 if (error != NULL)
1382 goto done;
1383 } else {
1384 error = got_repo_match_object_id(&commit_id, NULL,
1385 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1386 free(commit_id_str);
1387 commit_id_str = NULL;
1388 if (error)
1389 goto done;
1390 error = got_object_id_str(&commit_id_str, commit_id);
1391 if (error)
1392 goto done;
1395 if (branch_name) {
1396 struct got_object_id *head_commit_id;
1397 TAILQ_FOREACH(pe, &paths, entry) {
1398 if (pe->path_len == 0)
1399 continue;
1400 error = got_error_msg(GOT_ERR_BAD_PATH,
1401 "switching between branches requires that "
1402 "the entire work tree gets updated");
1403 goto done;
1405 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1406 if (error)
1407 goto done;
1408 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1409 repo);
1410 free(head_commit_id);
1411 if (error != NULL)
1412 goto done;
1413 error = check_same_branch(commit_id, head_ref, NULL, repo);
1414 if (error)
1415 goto done;
1416 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1417 if (error)
1418 goto done;
1419 } else {
1420 error = check_linear_ancestry(commit_id,
1421 got_worktree_get_base_commit_id(worktree), 0, repo);
1422 if (error != NULL) {
1423 if (error->code == GOT_ERR_ANCESTRY)
1424 error = got_error(GOT_ERR_BRANCH_MOVED);
1425 goto done;
1427 error = check_same_branch(commit_id, head_ref, NULL, repo);
1428 if (error)
1429 goto done;
1432 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1433 commit_id) != 0) {
1434 error = got_worktree_set_base_commit_id(worktree, repo,
1435 commit_id);
1436 if (error)
1437 goto done;
1440 error = got_worktree_checkout_files(worktree, &paths, repo,
1441 update_progress, &did_something, check_cancelled, NULL);
1442 if (error != NULL)
1443 goto done;
1445 if (did_something)
1446 printf("Updated to commit %s\n", commit_id_str);
1447 else
1448 printf("Already up-to-date\n");
1449 done:
1450 free(worktree_path);
1451 TAILQ_FOREACH(pe, &paths, entry)
1452 free((char *)pe->path);
1453 got_pathlist_free(&paths);
1454 free(commit_id);
1455 free(commit_id_str);
1456 return error;
1459 static const struct got_error *
1460 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1461 const char *path, int diff_context, int ignore_whitespace,
1462 struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1467 if (blob_id1) {
1468 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1469 if (err)
1470 goto done;
1473 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1474 if (err)
1475 goto done;
1477 while (path[0] == '/')
1478 path++;
1479 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1480 ignore_whitespace, stdout);
1481 done:
1482 if (blob1)
1483 got_object_blob_close(blob1);
1484 got_object_blob_close(blob2);
1485 return err;
1488 static const struct got_error *
1489 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1490 const char *path, int diff_context, int ignore_whitespace,
1491 struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1495 struct got_diff_blob_output_unidiff_arg arg;
1497 if (tree_id1) {
1498 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1499 if (err)
1500 goto done;
1503 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1504 if (err)
1505 goto done;
1507 arg.diff_context = diff_context;
1508 arg.ignore_whitespace = ignore_whitespace;
1509 arg.outfile = stdout;
1510 while (path[0] == '/')
1511 path++;
1512 err = got_diff_tree(tree1, tree2, path, path, repo,
1513 got_diff_blob_output_unidiff, &arg, 1);
1514 done:
1515 if (tree1)
1516 got_object_tree_close(tree1);
1517 if (tree2)
1518 got_object_tree_close(tree2);
1519 return err;
1522 static const struct got_error *
1523 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1524 const char *path, int diff_context, struct got_repository *repo)
1526 const struct got_error *err = NULL;
1527 struct got_commit_object *pcommit = NULL;
1528 char *id_str1 = NULL, *id_str2 = NULL;
1529 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1530 struct got_object_qid *qid;
1532 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1533 if (qid != NULL) {
1534 err = got_object_open_as_commit(&pcommit, repo,
1535 qid->id);
1536 if (err)
1537 return err;
1540 if (path && path[0] != '\0') {
1541 int obj_type;
1542 err = got_object_id_by_path(&obj_id2, repo, id, path);
1543 if (err)
1544 goto done;
1545 err = got_object_id_str(&id_str2, obj_id2);
1546 if (err) {
1547 free(obj_id2);
1548 goto done;
1550 if (pcommit) {
1551 err = got_object_id_by_path(&obj_id1, repo,
1552 qid->id, path);
1553 if (err) {
1554 free(obj_id2);
1555 goto done;
1557 err = got_object_id_str(&id_str1, obj_id1);
1558 if (err) {
1559 free(obj_id2);
1560 goto done;
1563 err = got_object_get_type(&obj_type, repo, obj_id2);
1564 if (err) {
1565 free(obj_id2);
1566 goto done;
1568 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1569 switch (obj_type) {
1570 case GOT_OBJ_TYPE_BLOB:
1571 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1572 0, repo);
1573 break;
1574 case GOT_OBJ_TYPE_TREE:
1575 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1576 0, repo);
1577 break;
1578 default:
1579 err = got_error(GOT_ERR_OBJ_TYPE);
1580 break;
1582 free(obj_id1);
1583 free(obj_id2);
1584 } else {
1585 obj_id2 = got_object_commit_get_tree_id(commit);
1586 err = got_object_id_str(&id_str2, obj_id2);
1587 if (err)
1588 goto done;
1589 obj_id1 = got_object_commit_get_tree_id(pcommit);
1590 err = got_object_id_str(&id_str1, obj_id1);
1591 if (err)
1592 goto done;
1593 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1594 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1596 done:
1597 free(id_str1);
1598 free(id_str2);
1599 if (pcommit)
1600 got_object_commit_close(pcommit);
1601 return err;
1604 static char *
1605 get_datestr(time_t *time, char *datebuf)
1607 struct tm mytm, *tm;
1608 char *p, *s;
1610 tm = gmtime_r(time, &mytm);
1611 if (tm == NULL)
1612 return NULL;
1613 s = asctime_r(tm, datebuf);
1614 if (s == NULL)
1615 return NULL;
1616 p = strchr(s, '\n');
1617 if (p)
1618 *p = '\0';
1619 return s;
1622 static const struct got_error *
1623 match_logmsg(int *have_match, struct got_object_id *id,
1624 struct got_commit_object *commit, regex_t *regex)
1626 const struct got_error *err = NULL;
1627 regmatch_t regmatch;
1628 char *id_str = NULL, *logmsg = NULL;
1630 *have_match = 0;
1632 err = got_object_id_str(&id_str, id);
1633 if (err)
1634 return err;
1636 err = got_object_commit_get_logmsg(&logmsg, commit);
1637 if (err)
1638 goto done;
1640 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1641 *have_match = 1;
1642 done:
1643 free(id_str);
1644 free(logmsg);
1645 return err;
1648 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1650 static const struct got_error *
1651 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1652 struct got_repository *repo, const char *path, int show_patch,
1653 int diff_context, struct got_reflist_head *refs)
1655 const struct got_error *err = NULL;
1656 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1657 char datebuf[26];
1658 time_t committer_time;
1659 const char *author, *committer;
1660 char *refs_str = NULL;
1661 struct got_reflist_entry *re;
1663 SIMPLEQ_FOREACH(re, refs, entry) {
1664 char *s;
1665 const char *name;
1666 struct got_tag_object *tag = NULL;
1667 int cmp;
1669 name = got_ref_get_name(re->ref);
1670 if (strcmp(name, GOT_REF_HEAD) == 0)
1671 continue;
1672 if (strncmp(name, "refs/", 5) == 0)
1673 name += 5;
1674 if (strncmp(name, "got/", 4) == 0)
1675 continue;
1676 if (strncmp(name, "heads/", 6) == 0)
1677 name += 6;
1678 if (strncmp(name, "remotes/", 8) == 0)
1679 name += 8;
1680 if (strncmp(name, "tags/", 5) == 0) {
1681 err = got_object_open_as_tag(&tag, repo, re->id);
1682 if (err) {
1683 if (err->code != GOT_ERR_OBJ_TYPE)
1684 return err;
1685 /* Ref points at something other than a tag. */
1686 err = NULL;
1687 tag = NULL;
1690 cmp = got_object_id_cmp(tag ?
1691 got_object_tag_get_object_id(tag) : re->id, id);
1692 if (tag)
1693 got_object_tag_close(tag);
1694 if (cmp != 0)
1695 continue;
1696 s = refs_str;
1697 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1698 name) == -1) {
1699 err = got_error_from_errno("asprintf");
1700 free(s);
1701 return err;
1703 free(s);
1705 err = got_object_id_str(&id_str, id);
1706 if (err)
1707 return err;
1709 printf(GOT_COMMIT_SEP_STR);
1710 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1711 refs_str ? refs_str : "", refs_str ? ")" : "");
1712 free(id_str);
1713 id_str = NULL;
1714 free(refs_str);
1715 refs_str = NULL;
1716 printf("from: %s\n", got_object_commit_get_author(commit));
1717 committer_time = got_object_commit_get_committer_time(commit);
1718 datestr = get_datestr(&committer_time, datebuf);
1719 if (datestr)
1720 printf("date: %s UTC\n", datestr);
1721 author = got_object_commit_get_author(commit);
1722 committer = got_object_commit_get_committer(commit);
1723 if (strcmp(author, committer) != 0)
1724 printf("via: %s\n", committer);
1725 if (got_object_commit_get_nparents(commit) > 1) {
1726 const struct got_object_id_queue *parent_ids;
1727 struct got_object_qid *qid;
1728 int n = 1;
1729 parent_ids = got_object_commit_get_parent_ids(commit);
1730 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1731 err = got_object_id_str(&id_str, qid->id);
1732 if (err)
1733 return err;
1734 printf("parent %d: %s\n", n++, id_str);
1735 free(id_str);
1739 err = got_object_commit_get_logmsg(&logmsg0, commit);
1740 if (err)
1741 return err;
1743 logmsg = logmsg0;
1744 do {
1745 line = strsep(&logmsg, "\n");
1746 if (line)
1747 printf(" %s\n", line);
1748 } while (line);
1749 free(logmsg0);
1751 if (show_patch) {
1752 err = print_patch(commit, id, path, diff_context, repo);
1753 if (err == 0)
1754 printf("\n");
1757 if (fflush(stdout) != 0 && err == NULL)
1758 err = got_error_from_errno("fflush");
1759 return err;
1762 static const struct got_error *
1763 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1764 const char *path, int show_patch, const char *search_pattern,
1765 int diff_context, int limit, int log_branches,
1766 struct got_reflist_head *refs)
1768 const struct got_error *err;
1769 struct got_commit_graph *graph;
1770 regex_t regex;
1771 int have_match;
1773 if (search_pattern &&
1774 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1775 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1777 err = got_commit_graph_open(&graph, path, !log_branches);
1778 if (err)
1779 return err;
1780 err = got_commit_graph_iter_start(graph, root_id, repo,
1781 check_cancelled, NULL);
1782 if (err)
1783 goto done;
1784 for (;;) {
1785 struct got_commit_object *commit;
1786 struct got_object_id *id;
1788 if (sigint_received || sigpipe_received)
1789 break;
1791 err = got_commit_graph_iter_next(&id, graph, repo,
1792 check_cancelled, NULL);
1793 if (err) {
1794 if (err->code == GOT_ERR_ITER_COMPLETED)
1795 err = NULL;
1796 break;
1798 if (id == NULL)
1799 break;
1801 err = got_object_open_as_commit(&commit, repo, id);
1802 if (err)
1803 break;
1805 if (search_pattern) {
1806 err = match_logmsg(&have_match, id, commit, &regex);
1807 if (err) {
1808 got_object_commit_close(commit);
1809 break;
1811 if (have_match == 0) {
1812 got_object_commit_close(commit);
1813 continue;
1817 err = print_commit(commit, id, repo, path, show_patch,
1818 diff_context, refs);
1819 got_object_commit_close(commit);
1820 if (err || (limit && --limit == 0))
1821 break;
1823 done:
1824 if (search_pattern)
1825 regfree(&regex);
1826 got_commit_graph_close(graph);
1827 return err;
1830 __dead static void
1831 usage_log(void)
1833 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1834 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1835 exit(1);
1838 static int
1839 get_default_log_limit(void)
1841 const char *got_default_log_limit;
1842 long long n;
1843 const char *errstr;
1845 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1846 if (got_default_log_limit == NULL)
1847 return 0;
1848 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1849 if (errstr != NULL)
1850 return 0;
1851 return n;
1854 static const struct got_error *
1855 cmd_log(int argc, char *argv[])
1857 const struct got_error *error;
1858 struct got_repository *repo = NULL;
1859 struct got_worktree *worktree = NULL;
1860 struct got_commit_object *commit = NULL;
1861 struct got_object_id *id = NULL;
1862 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1863 const char *start_commit = NULL, *search_pattern = NULL;
1864 int diff_context = -1, ch;
1865 int show_patch = 0, limit = 0, log_branches = 0;
1866 const char *errstr;
1867 struct got_reflist_head refs;
1869 SIMPLEQ_INIT(&refs);
1871 #ifndef PROFILE
1872 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1873 NULL)
1874 == -1)
1875 err(1, "pledge");
1876 #endif
1878 limit = get_default_log_limit();
1880 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1881 switch (ch) {
1882 case 'p':
1883 show_patch = 1;
1884 break;
1885 case 'c':
1886 start_commit = optarg;
1887 break;
1888 case 'C':
1889 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1890 &errstr);
1891 if (errstr != NULL)
1892 err(1, "-C option %s", errstr);
1893 break;
1894 case 'l':
1895 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1896 if (errstr != NULL)
1897 err(1, "-l option %s", errstr);
1898 break;
1899 case 'b':
1900 log_branches = 1;
1901 break;
1902 case 'r':
1903 repo_path = realpath(optarg, NULL);
1904 if (repo_path == NULL)
1905 return got_error_from_errno2("realpath",
1906 optarg);
1907 got_path_strip_trailing_slashes(repo_path);
1908 break;
1909 case 's':
1910 search_pattern = optarg;
1911 break;
1912 default:
1913 usage_log();
1914 /* NOTREACHED */
1918 argc -= optind;
1919 argv += optind;
1921 if (diff_context == -1)
1922 diff_context = 3;
1923 else if (!show_patch)
1924 errx(1, "-C reguires -p");
1926 cwd = getcwd(NULL, 0);
1927 if (cwd == NULL) {
1928 error = got_error_from_errno("getcwd");
1929 goto done;
1932 error = got_worktree_open(&worktree, cwd);
1933 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1934 goto done;
1935 error = NULL;
1937 if (argc == 0) {
1938 path = strdup("");
1939 if (path == NULL) {
1940 error = got_error_from_errno("strdup");
1941 goto done;
1943 } else if (argc == 1) {
1944 if (worktree) {
1945 error = got_worktree_resolve_path(&path, worktree,
1946 argv[0]);
1947 if (error)
1948 goto done;
1949 } else {
1950 path = strdup(argv[0]);
1951 if (path == NULL) {
1952 error = got_error_from_errno("strdup");
1953 goto done;
1956 } else
1957 usage_log();
1959 if (repo_path == NULL) {
1960 repo_path = worktree ?
1961 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1963 if (repo_path == NULL) {
1964 error = got_error_from_errno("strdup");
1965 goto done;
1968 error = got_repo_open(&repo, repo_path, NULL);
1969 if (error != NULL)
1970 goto done;
1972 error = apply_unveil(got_repo_get_path(repo), 1,
1973 worktree ? got_worktree_get_root_path(worktree) : NULL);
1974 if (error)
1975 goto done;
1977 if (start_commit == NULL) {
1978 struct got_reference *head_ref;
1979 error = got_ref_open(&head_ref, repo,
1980 worktree ? got_worktree_get_head_ref_name(worktree)
1981 : GOT_REF_HEAD, 0);
1982 if (error != NULL)
1983 return error;
1984 error = got_ref_resolve(&id, repo, head_ref);
1985 got_ref_close(head_ref);
1986 if (error != NULL)
1987 return error;
1988 error = got_object_open_as_commit(&commit, repo, id);
1989 } else {
1990 struct got_reference *ref;
1991 error = got_ref_open(&ref, repo, start_commit, 0);
1992 if (error == NULL) {
1993 int obj_type;
1994 error = got_ref_resolve(&id, repo, ref);
1995 got_ref_close(ref);
1996 if (error != NULL)
1997 goto done;
1998 error = got_object_get_type(&obj_type, repo, id);
1999 if (error != NULL)
2000 goto done;
2001 if (obj_type == GOT_OBJ_TYPE_TAG) {
2002 struct got_tag_object *tag;
2003 error = got_object_open_as_tag(&tag, repo, id);
2004 if (error != NULL)
2005 goto done;
2006 if (got_object_tag_get_object_type(tag) !=
2007 GOT_OBJ_TYPE_COMMIT) {
2008 got_object_tag_close(tag);
2009 error = got_error(GOT_ERR_OBJ_TYPE);
2010 goto done;
2012 free(id);
2013 id = got_object_id_dup(
2014 got_object_tag_get_object_id(tag));
2015 if (id == NULL)
2016 error = got_error_from_errno(
2017 "got_object_id_dup");
2018 got_object_tag_close(tag);
2019 if (error)
2020 goto done;
2021 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2022 error = got_error(GOT_ERR_OBJ_TYPE);
2023 goto done;
2025 error = got_object_open_as_commit(&commit, repo, id);
2026 if (error != NULL)
2027 goto done;
2029 if (commit == NULL) {
2030 error = got_repo_match_object_id_prefix(&id,
2031 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2032 if (error != NULL)
2033 return error;
2036 if (error != NULL)
2037 goto done;
2039 if (worktree) {
2040 const char *prefix = got_worktree_get_path_prefix(worktree);
2041 char *p;
2042 if (asprintf(&p, "%s%s%s", prefix,
2043 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2044 error = got_error_from_errno("asprintf");
2045 goto done;
2047 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2048 free(p);
2049 } else
2050 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2051 if (error != NULL)
2052 goto done;
2053 if (in_repo_path) {
2054 free(path);
2055 path = in_repo_path;
2058 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2059 if (error)
2060 goto done;
2062 error = print_commits(id, repo, path, show_patch, search_pattern,
2063 diff_context, limit, log_branches, &refs);
2064 done:
2065 free(path);
2066 free(repo_path);
2067 free(cwd);
2068 free(id);
2069 if (worktree)
2070 got_worktree_close(worktree);
2071 if (repo) {
2072 const struct got_error *repo_error;
2073 repo_error = got_repo_close(repo);
2074 if (error == NULL)
2075 error = repo_error;
2077 got_ref_list_free(&refs);
2078 return error;
2081 __dead static void
2082 usage_diff(void)
2084 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2085 "[-w] [object1 object2 | path]\n", getprogname());
2086 exit(1);
2089 struct print_diff_arg {
2090 struct got_repository *repo;
2091 struct got_worktree *worktree;
2092 int diff_context;
2093 const char *id_str;
2094 int header_shown;
2095 int diff_staged;
2096 int ignore_whitespace;
2099 static const struct got_error *
2100 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2101 const char *path, struct got_object_id *blob_id,
2102 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2103 int dirfd, const char *de_name)
2105 struct print_diff_arg *a = arg;
2106 const struct got_error *err = NULL;
2107 struct got_blob_object *blob1 = NULL;
2108 int fd = -1;
2109 FILE *f2 = NULL;
2110 char *abspath = NULL, *label1 = NULL;
2111 struct stat sb;
2113 if (a->diff_staged) {
2114 if (staged_status != GOT_STATUS_MODIFY &&
2115 staged_status != GOT_STATUS_ADD &&
2116 staged_status != GOT_STATUS_DELETE)
2117 return NULL;
2118 } else {
2119 if (staged_status == GOT_STATUS_DELETE)
2120 return NULL;
2121 if (status == GOT_STATUS_NONEXISTENT)
2122 return got_error_set_errno(ENOENT, path);
2123 if (status != GOT_STATUS_MODIFY &&
2124 status != GOT_STATUS_ADD &&
2125 status != GOT_STATUS_DELETE &&
2126 status != GOT_STATUS_CONFLICT)
2127 return NULL;
2130 if (!a->header_shown) {
2131 printf("diff %s %s%s\n", a->id_str,
2132 got_worktree_get_root_path(a->worktree),
2133 a->diff_staged ? " (staged changes)" : "");
2134 a->header_shown = 1;
2137 if (a->diff_staged) {
2138 const char *label1 = NULL, *label2 = NULL;
2139 switch (staged_status) {
2140 case GOT_STATUS_MODIFY:
2141 label1 = path;
2142 label2 = path;
2143 break;
2144 case GOT_STATUS_ADD:
2145 label2 = path;
2146 break;
2147 case GOT_STATUS_DELETE:
2148 label1 = path;
2149 break;
2150 default:
2151 return got_error(GOT_ERR_FILE_STATUS);
2153 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2154 label1, label2, a->diff_context, a->ignore_whitespace,
2155 a->repo, stdout);
2158 if (staged_status == GOT_STATUS_ADD ||
2159 staged_status == GOT_STATUS_MODIFY) {
2160 char *id_str;
2161 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2162 8192);
2163 if (err)
2164 goto done;
2165 err = got_object_id_str(&id_str, staged_blob_id);
2166 if (err)
2167 goto done;
2168 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2169 err = got_error_from_errno("asprintf");
2170 free(id_str);
2171 goto done;
2173 free(id_str);
2174 } else if (status != GOT_STATUS_ADD) {
2175 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2176 if (err)
2177 goto done;
2180 if (status != GOT_STATUS_DELETE) {
2181 if (asprintf(&abspath, "%s/%s",
2182 got_worktree_get_root_path(a->worktree), path) == -1) {
2183 err = got_error_from_errno("asprintf");
2184 goto done;
2187 if (dirfd != -1) {
2188 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2189 if (fd == -1) {
2190 err = got_error_from_errno2("openat", abspath);
2191 goto done;
2193 } else {
2194 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2195 if (fd == -1) {
2196 err = got_error_from_errno2("open", abspath);
2197 goto done;
2200 if (fstat(fd, &sb) == -1) {
2201 err = got_error_from_errno2("fstat", abspath);
2202 goto done;
2204 f2 = fdopen(fd, "r");
2205 if (f2 == NULL) {
2206 err = got_error_from_errno2("fdopen", abspath);
2207 goto done;
2209 fd = -1;
2210 } else
2211 sb.st_size = 0;
2213 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2214 a->diff_context, a->ignore_whitespace, stdout);
2215 done:
2216 if (blob1)
2217 got_object_blob_close(blob1);
2218 if (f2 && fclose(f2) == EOF && err == NULL)
2219 err = got_error_from_errno("fclose");
2220 if (fd != -1 && close(fd) == -1 && err == NULL)
2221 err = got_error_from_errno("close");
2222 free(abspath);
2223 return err;
2226 static const struct got_error *
2227 cmd_diff(int argc, char *argv[])
2229 const struct got_error *error;
2230 struct got_repository *repo = NULL;
2231 struct got_worktree *worktree = NULL;
2232 char *cwd = NULL, *repo_path = NULL;
2233 struct got_object_id *id1 = NULL, *id2 = NULL;
2234 const char *id_str1 = NULL, *id_str2 = NULL;
2235 char *label1 = NULL, *label2 = NULL;
2236 int type1, type2;
2237 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2238 const char *errstr;
2239 char *path = NULL;
2241 #ifndef PROFILE
2242 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2243 NULL) == -1)
2244 err(1, "pledge");
2245 #endif
2247 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2248 switch (ch) {
2249 case 'C':
2250 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2251 &errstr);
2252 if (errstr != NULL)
2253 err(1, "-C option %s", errstr);
2254 break;
2255 case 'r':
2256 repo_path = realpath(optarg, NULL);
2257 if (repo_path == NULL)
2258 return got_error_from_errno2("realpath",
2259 optarg);
2260 got_path_strip_trailing_slashes(repo_path);
2261 break;
2262 case 's':
2263 diff_staged = 1;
2264 break;
2265 case 'w':
2266 ignore_whitespace = 1;
2267 break;
2268 default:
2269 usage_diff();
2270 /* NOTREACHED */
2274 argc -= optind;
2275 argv += optind;
2277 cwd = getcwd(NULL, 0);
2278 if (cwd == NULL) {
2279 error = got_error_from_errno("getcwd");
2280 goto done;
2282 error = got_worktree_open(&worktree, cwd);
2283 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2284 goto done;
2285 if (argc <= 1) {
2286 if (worktree == NULL) {
2287 error = got_error(GOT_ERR_NOT_WORKTREE);
2288 goto done;
2290 if (repo_path)
2291 errx(1,
2292 "-r option can't be used when diffing a work tree");
2293 repo_path = strdup(got_worktree_get_repo_path(worktree));
2294 if (repo_path == NULL) {
2295 error = got_error_from_errno("strdup");
2296 goto done;
2298 if (argc == 1) {
2299 error = got_worktree_resolve_path(&path, worktree,
2300 argv[0]);
2301 if (error)
2302 goto done;
2303 } else {
2304 path = strdup("");
2305 if (path == NULL) {
2306 error = got_error_from_errno("strdup");
2307 goto done;
2310 } else if (argc == 2) {
2311 if (diff_staged)
2312 errx(1, "-s option can't be used when diffing "
2313 "objects in repository");
2314 id_str1 = argv[0];
2315 id_str2 = argv[1];
2316 if (worktree && repo_path == NULL) {
2317 repo_path =
2318 strdup(got_worktree_get_repo_path(worktree));
2319 if (repo_path == NULL) {
2320 error = got_error_from_errno("strdup");
2321 goto done;
2324 } else
2325 usage_diff();
2327 if (repo_path == NULL) {
2328 repo_path = getcwd(NULL, 0);
2329 if (repo_path == NULL)
2330 return got_error_from_errno("getcwd");
2333 error = got_repo_open(&repo, repo_path, NULL);
2334 free(repo_path);
2335 if (error != NULL)
2336 goto done;
2338 error = apply_unveil(got_repo_get_path(repo), 1,
2339 worktree ? got_worktree_get_root_path(worktree) : NULL);
2340 if (error)
2341 goto done;
2343 if (argc <= 1) {
2344 struct print_diff_arg arg;
2345 struct got_pathlist_head paths;
2346 char *id_str;
2348 TAILQ_INIT(&paths);
2350 error = got_object_id_str(&id_str,
2351 got_worktree_get_base_commit_id(worktree));
2352 if (error)
2353 goto done;
2354 arg.repo = repo;
2355 arg.worktree = worktree;
2356 arg.diff_context = diff_context;
2357 arg.id_str = id_str;
2358 arg.header_shown = 0;
2359 arg.diff_staged = diff_staged;
2360 arg.ignore_whitespace = ignore_whitespace;
2362 error = got_pathlist_append(&paths, path, NULL);
2363 if (error)
2364 goto done;
2366 error = got_worktree_status(worktree, &paths, repo, print_diff,
2367 &arg, check_cancelled, NULL);
2368 free(id_str);
2369 got_pathlist_free(&paths);
2370 goto done;
2373 error = got_repo_match_object_id(&id1, &label1, id_str1,
2374 GOT_OBJ_TYPE_ANY, 1, repo);
2375 if (error)
2376 goto done;
2378 error = got_repo_match_object_id(&id2, &label2, id_str2,
2379 GOT_OBJ_TYPE_ANY, 1, repo);
2380 if (error)
2381 goto done;
2383 error = got_object_get_type(&type1, repo, id1);
2384 if (error)
2385 goto done;
2387 error = got_object_get_type(&type2, repo, id2);
2388 if (error)
2389 goto done;
2391 if (type1 != type2) {
2392 error = got_error(GOT_ERR_OBJ_TYPE);
2393 goto done;
2396 switch (type1) {
2397 case GOT_OBJ_TYPE_BLOB:
2398 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2399 diff_context, ignore_whitespace, repo, stdout);
2400 break;
2401 case GOT_OBJ_TYPE_TREE:
2402 error = got_diff_objects_as_trees(id1, id2, "", "",
2403 diff_context, ignore_whitespace, repo, stdout);
2404 break;
2405 case GOT_OBJ_TYPE_COMMIT:
2406 printf("diff %s %s\n", label1, label2);
2407 error = got_diff_objects_as_commits(id1, id2, diff_context,
2408 ignore_whitespace, repo, stdout);
2409 break;
2410 default:
2411 error = got_error(GOT_ERR_OBJ_TYPE);
2413 done:
2414 free(label1);
2415 free(label2);
2416 free(id1);
2417 free(id2);
2418 free(path);
2419 if (worktree)
2420 got_worktree_close(worktree);
2421 if (repo) {
2422 const struct got_error *repo_error;
2423 repo_error = got_repo_close(repo);
2424 if (error == NULL)
2425 error = repo_error;
2427 return error;
2430 __dead static void
2431 usage_blame(void)
2433 fprintf(stderr,
2434 "usage: %s blame [-c commit] [-r repository-path] path\n",
2435 getprogname());
2436 exit(1);
2439 struct blame_line {
2440 int annotated;
2441 char *id_str;
2442 char *committer;
2443 char datebuf[11]; /* YYYY-MM-DD + NUL */
2446 struct blame_cb_args {
2447 struct blame_line *lines;
2448 int nlines;
2449 int nlines_prec;
2450 int lineno_cur;
2451 off_t *line_offsets;
2452 FILE *f;
2453 struct got_repository *repo;
2456 static const struct got_error *
2457 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2459 const struct got_error *err = NULL;
2460 struct blame_cb_args *a = arg;
2461 struct blame_line *bline;
2462 char *line = NULL;
2463 size_t linesize = 0;
2464 struct got_commit_object *commit = NULL;
2465 off_t offset;
2466 struct tm tm;
2467 time_t committer_time;
2469 if (nlines != a->nlines ||
2470 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2471 return got_error(GOT_ERR_RANGE);
2473 if (sigint_received)
2474 return got_error(GOT_ERR_ITER_COMPLETED);
2476 if (lineno == -1)
2477 return NULL; /* no change in this commit */
2479 /* Annotate this line. */
2480 bline = &a->lines[lineno - 1];
2481 if (bline->annotated)
2482 return NULL;
2483 err = got_object_id_str(&bline->id_str, id);
2484 if (err)
2485 return err;
2487 err = got_object_open_as_commit(&commit, a->repo, id);
2488 if (err)
2489 goto done;
2491 bline->committer = strdup(got_object_commit_get_committer(commit));
2492 if (bline->committer == NULL) {
2493 err = got_error_from_errno("strdup");
2494 goto done;
2497 committer_time = got_object_commit_get_committer_time(commit);
2498 if (localtime_r(&committer_time, &tm) == NULL)
2499 return got_error_from_errno("localtime_r");
2500 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2501 &tm) >= sizeof(bline->datebuf)) {
2502 err = got_error(GOT_ERR_NO_SPACE);
2503 goto done;
2505 bline->annotated = 1;
2507 /* Print lines annotated so far. */
2508 bline = &a->lines[a->lineno_cur - 1];
2509 if (!bline->annotated)
2510 goto done;
2512 offset = a->line_offsets[a->lineno_cur - 1];
2513 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2514 err = got_error_from_errno("fseeko");
2515 goto done;
2518 while (bline->annotated) {
2519 char *smallerthan, *at, *nl, *committer;
2520 size_t len;
2522 if (getline(&line, &linesize, a->f) == -1) {
2523 if (ferror(a->f))
2524 err = got_error_from_errno("getline");
2525 break;
2528 committer = bline->committer;
2529 smallerthan = strchr(committer, '<');
2530 if (smallerthan && smallerthan[1] != '\0')
2531 committer = smallerthan + 1;
2532 at = strchr(committer, '@');
2533 if (at)
2534 *at = '\0';
2535 len = strlen(committer);
2536 if (len >= 9)
2537 committer[8] = '\0';
2539 nl = strchr(line, '\n');
2540 if (nl)
2541 *nl = '\0';
2542 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2543 bline->id_str, bline->datebuf, committer, line);
2545 a->lineno_cur++;
2546 bline = &a->lines[a->lineno_cur - 1];
2548 done:
2549 if (commit)
2550 got_object_commit_close(commit);
2551 free(line);
2552 return err;
2555 static const struct got_error *
2556 cmd_blame(int argc, char *argv[])
2558 const struct got_error *error;
2559 struct got_repository *repo = NULL;
2560 struct got_worktree *worktree = NULL;
2561 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2562 struct got_object_id *obj_id = NULL;
2563 struct got_object_id *commit_id = NULL;
2564 struct got_blob_object *blob = NULL;
2565 char *commit_id_str = NULL;
2566 struct blame_cb_args bca;
2567 int ch, obj_type, i;
2568 size_t filesize;
2570 memset(&bca, 0, sizeof(bca));
2572 #ifndef PROFILE
2573 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2574 NULL) == -1)
2575 err(1, "pledge");
2576 #endif
2578 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2579 switch (ch) {
2580 case 'c':
2581 commit_id_str = optarg;
2582 break;
2583 case 'r':
2584 repo_path = realpath(optarg, NULL);
2585 if (repo_path == NULL)
2586 return got_error_from_errno2("realpath",
2587 optarg);
2588 got_path_strip_trailing_slashes(repo_path);
2589 break;
2590 default:
2591 usage_blame();
2592 /* NOTREACHED */
2596 argc -= optind;
2597 argv += optind;
2599 if (argc == 1)
2600 path = argv[0];
2601 else
2602 usage_blame();
2604 cwd = getcwd(NULL, 0);
2605 if (cwd == NULL) {
2606 error = got_error_from_errno("getcwd");
2607 goto done;
2609 if (repo_path == NULL) {
2610 error = got_worktree_open(&worktree, cwd);
2611 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2612 goto done;
2613 else
2614 error = NULL;
2615 if (worktree) {
2616 repo_path =
2617 strdup(got_worktree_get_repo_path(worktree));
2618 if (repo_path == NULL)
2619 error = got_error_from_errno("strdup");
2620 if (error)
2621 goto done;
2622 } else {
2623 repo_path = strdup(cwd);
2624 if (repo_path == NULL) {
2625 error = got_error_from_errno("strdup");
2626 goto done;
2631 error = got_repo_open(&repo, repo_path, NULL);
2632 if (error != NULL)
2633 goto done;
2635 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2636 if (error)
2637 goto done;
2639 if (worktree) {
2640 const char *prefix = got_worktree_get_path_prefix(worktree);
2641 char *p, *worktree_subdir = cwd +
2642 strlen(got_worktree_get_root_path(worktree));
2643 if (asprintf(&p, "%s%s%s%s%s",
2644 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2645 worktree_subdir, worktree_subdir[0] ? "/" : "",
2646 path) == -1) {
2647 error = got_error_from_errno("asprintf");
2648 goto done;
2650 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2651 free(p);
2652 } else {
2653 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2655 if (error)
2656 goto done;
2658 if (commit_id_str == NULL) {
2659 struct got_reference *head_ref;
2660 error = got_ref_open(&head_ref, repo, worktree ?
2661 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2662 if (error != NULL)
2663 goto done;
2664 error = got_ref_resolve(&commit_id, repo, head_ref);
2665 got_ref_close(head_ref);
2666 if (error != NULL)
2667 goto done;
2668 } else {
2669 error = got_repo_match_object_id(&commit_id, NULL,
2670 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2671 if (error)
2672 goto done;
2675 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2676 if (error)
2677 goto done;
2679 error = got_object_get_type(&obj_type, repo, obj_id);
2680 if (error)
2681 goto done;
2683 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2684 error = got_error(GOT_ERR_OBJ_TYPE);
2685 goto done;
2688 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2689 if (error)
2690 goto done;
2691 bca.f = got_opentemp();
2692 if (bca.f == NULL) {
2693 error = got_error_from_errno("got_opentemp");
2694 goto done;
2696 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2697 &bca.line_offsets, bca.f, blob);
2698 if (error || bca.nlines == 0)
2699 goto done;
2701 /* Don't include \n at EOF in the blame line count. */
2702 if (bca.line_offsets[bca.nlines - 1] == filesize)
2703 bca.nlines--;
2705 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2706 if (bca.lines == NULL) {
2707 error = got_error_from_errno("calloc");
2708 goto done;
2710 bca.lineno_cur = 1;
2711 bca.nlines_prec = 0;
2712 i = bca.nlines;
2713 while (i > 0) {
2714 i /= 10;
2715 bca.nlines_prec++;
2717 bca.repo = repo;
2719 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2720 check_cancelled, NULL);
2721 done:
2722 free(in_repo_path);
2723 free(repo_path);
2724 free(cwd);
2725 free(commit_id);
2726 free(obj_id);
2727 if (blob)
2728 got_object_blob_close(blob);
2729 if (worktree)
2730 got_worktree_close(worktree);
2731 if (repo) {
2732 const struct got_error *repo_error;
2733 repo_error = got_repo_close(repo);
2734 if (error == NULL)
2735 error = repo_error;
2737 if (bca.lines) {
2738 for (i = 0; i < bca.nlines; i++) {
2739 struct blame_line *bline = &bca.lines[i];
2740 free(bline->id_str);
2741 free(bline->committer);
2743 free(bca.lines);
2745 free(bca.line_offsets);
2746 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2747 error = got_error_from_errno("fclose");
2748 return error;
2751 __dead static void
2752 usage_tree(void)
2754 fprintf(stderr,
2755 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2756 getprogname());
2757 exit(1);
2760 static void
2761 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2762 const char *root_path)
2764 int is_root_path = (strcmp(path, root_path) == 0);
2765 const char *modestr = "";
2766 mode_t mode = got_tree_entry_get_mode(te);
2768 path += strlen(root_path);
2769 while (path[0] == '/')
2770 path++;
2772 if (got_object_tree_entry_is_submodule(te))
2773 modestr = "$";
2774 else if (S_ISLNK(mode))
2775 modestr = "@";
2776 else if (S_ISDIR(mode))
2777 modestr = "/";
2778 else if (mode & S_IXUSR)
2779 modestr = "*";
2781 printf("%s%s%s%s%s\n", id ? id : "", path,
2782 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2785 static const struct got_error *
2786 print_tree(const char *path, struct got_object_id *commit_id,
2787 int show_ids, int recurse, const char *root_path,
2788 struct got_repository *repo)
2790 const struct got_error *err = NULL;
2791 struct got_object_id *tree_id = NULL;
2792 struct got_tree_object *tree = NULL;
2793 int nentries, i;
2795 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2796 if (err)
2797 goto done;
2799 err = got_object_open_as_tree(&tree, repo, tree_id);
2800 if (err)
2801 goto done;
2802 nentries = got_object_tree_get_nentries(tree);
2803 for (i = 0; i < nentries; i++) {
2804 struct got_tree_entry *te;
2805 char *id = NULL;
2807 if (sigint_received || sigpipe_received)
2808 break;
2810 te = got_object_tree_get_entry(tree, i);
2811 if (show_ids) {
2812 char *id_str;
2813 err = got_object_id_str(&id_str,
2814 got_tree_entry_get_id(te));
2815 if (err)
2816 goto done;
2817 if (asprintf(&id, "%s ", id_str) == -1) {
2818 err = got_error_from_errno("asprintf");
2819 free(id_str);
2820 goto done;
2822 free(id_str);
2824 print_entry(te, id, path, root_path);
2825 free(id);
2827 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2828 char *child_path;
2829 if (asprintf(&child_path, "%s%s%s", path,
2830 path[0] == '/' && path[1] == '\0' ? "" : "/",
2831 got_tree_entry_get_name(te)) == -1) {
2832 err = got_error_from_errno("asprintf");
2833 goto done;
2835 err = print_tree(child_path, commit_id, show_ids, 1,
2836 root_path, repo);
2837 free(child_path);
2838 if (err)
2839 goto done;
2842 done:
2843 if (tree)
2844 got_object_tree_close(tree);
2845 free(tree_id);
2846 return err;
2849 static const struct got_error *
2850 cmd_tree(int argc, char *argv[])
2852 const struct got_error *error;
2853 struct got_repository *repo = NULL;
2854 struct got_worktree *worktree = NULL;
2855 const char *path;
2856 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2857 struct got_object_id *commit_id = NULL;
2858 char *commit_id_str = NULL;
2859 int show_ids = 0, recurse = 0;
2860 int ch;
2862 #ifndef PROFILE
2863 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2864 NULL) == -1)
2865 err(1, "pledge");
2866 #endif
2868 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2869 switch (ch) {
2870 case 'c':
2871 commit_id_str = optarg;
2872 break;
2873 case 'r':
2874 repo_path = realpath(optarg, NULL);
2875 if (repo_path == NULL)
2876 return got_error_from_errno2("realpath",
2877 optarg);
2878 got_path_strip_trailing_slashes(repo_path);
2879 break;
2880 case 'i':
2881 show_ids = 1;
2882 break;
2883 case 'R':
2884 recurse = 1;
2885 break;
2886 default:
2887 usage_tree();
2888 /* NOTREACHED */
2892 argc -= optind;
2893 argv += optind;
2895 if (argc == 1)
2896 path = argv[0];
2897 else if (argc > 1)
2898 usage_tree();
2899 else
2900 path = NULL;
2902 cwd = getcwd(NULL, 0);
2903 if (cwd == NULL) {
2904 error = got_error_from_errno("getcwd");
2905 goto done;
2907 if (repo_path == NULL) {
2908 error = got_worktree_open(&worktree, cwd);
2909 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2910 goto done;
2911 else
2912 error = NULL;
2913 if (worktree) {
2914 repo_path =
2915 strdup(got_worktree_get_repo_path(worktree));
2916 if (repo_path == NULL)
2917 error = got_error_from_errno("strdup");
2918 if (error)
2919 goto done;
2920 } else {
2921 repo_path = strdup(cwd);
2922 if (repo_path == NULL) {
2923 error = got_error_from_errno("strdup");
2924 goto done;
2929 error = got_repo_open(&repo, repo_path, NULL);
2930 if (error != NULL)
2931 goto done;
2933 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2934 if (error)
2935 goto done;
2937 if (path == NULL) {
2938 if (worktree) {
2939 char *p, *worktree_subdir = cwd +
2940 strlen(got_worktree_get_root_path(worktree));
2941 if (asprintf(&p, "%s/%s",
2942 got_worktree_get_path_prefix(worktree),
2943 worktree_subdir) == -1) {
2944 error = got_error_from_errno("asprintf");
2945 goto done;
2947 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2948 free(p);
2949 if (error)
2950 goto done;
2951 } else
2952 path = "/";
2954 if (in_repo_path == NULL) {
2955 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2956 if (error != NULL)
2957 goto done;
2960 if (commit_id_str == NULL) {
2961 struct got_reference *head_ref;
2962 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2963 if (error != NULL)
2964 goto done;
2965 error = got_ref_resolve(&commit_id, repo, head_ref);
2966 got_ref_close(head_ref);
2967 if (error != NULL)
2968 goto done;
2969 } else {
2970 error = got_repo_match_object_id(&commit_id, NULL,
2971 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2972 if (error)
2973 goto done;
2976 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2977 in_repo_path, repo);
2978 done:
2979 free(in_repo_path);
2980 free(repo_path);
2981 free(cwd);
2982 free(commit_id);
2983 if (worktree)
2984 got_worktree_close(worktree);
2985 if (repo) {
2986 const struct got_error *repo_error;
2987 repo_error = got_repo_close(repo);
2988 if (error == NULL)
2989 error = repo_error;
2991 return error;
2994 __dead static void
2995 usage_status(void)
2997 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2998 exit(1);
3001 static const struct got_error *
3002 print_status(void *arg, unsigned char status, unsigned char staged_status,
3003 const char *path, struct got_object_id *blob_id,
3004 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3005 int dirfd, const char *de_name)
3007 if (status == staged_status && (status == GOT_STATUS_DELETE))
3008 status = GOT_STATUS_NO_CHANGE;
3009 printf("%c%c %s\n", status, staged_status, path);
3010 return NULL;
3013 static const struct got_error *
3014 cmd_status(int argc, char *argv[])
3016 const struct got_error *error = NULL;
3017 struct got_repository *repo = NULL;
3018 struct got_worktree *worktree = NULL;
3019 char *cwd = NULL;
3020 struct got_pathlist_head paths;
3021 struct got_pathlist_entry *pe;
3022 int ch;
3024 TAILQ_INIT(&paths);
3026 while ((ch = getopt(argc, argv, "")) != -1) {
3027 switch (ch) {
3028 default:
3029 usage_status();
3030 /* NOTREACHED */
3034 argc -= optind;
3035 argv += optind;
3037 #ifndef PROFILE
3038 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3039 NULL) == -1)
3040 err(1, "pledge");
3041 #endif
3042 cwd = getcwd(NULL, 0);
3043 if (cwd == NULL) {
3044 error = got_error_from_errno("getcwd");
3045 goto done;
3048 error = got_worktree_open(&worktree, cwd);
3049 if (error != NULL)
3050 goto done;
3052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3053 NULL);
3054 if (error != NULL)
3055 goto done;
3057 error = apply_unveil(got_repo_get_path(repo), 1,
3058 got_worktree_get_root_path(worktree));
3059 if (error)
3060 goto done;
3062 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3063 if (error)
3064 goto done;
3066 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3067 check_cancelled, NULL);
3068 done:
3069 TAILQ_FOREACH(pe, &paths, entry)
3070 free((char *)pe->path);
3071 got_pathlist_free(&paths);
3072 free(cwd);
3073 return error;
3076 __dead static void
3077 usage_ref(void)
3079 fprintf(stderr,
3080 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3081 getprogname());
3082 exit(1);
3085 static const struct got_error *
3086 list_refs(struct got_repository *repo)
3088 static const struct got_error *err = NULL;
3089 struct got_reflist_head refs;
3090 struct got_reflist_entry *re;
3092 SIMPLEQ_INIT(&refs);
3093 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3094 if (err)
3095 return err;
3097 SIMPLEQ_FOREACH(re, &refs, entry) {
3098 char *refstr;
3099 refstr = got_ref_to_str(re->ref);
3100 if (refstr == NULL)
3101 return got_error_from_errno("got_ref_to_str");
3102 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3103 free(refstr);
3106 got_ref_list_free(&refs);
3107 return NULL;
3110 static const struct got_error *
3111 delete_ref(struct got_repository *repo, const char *refname)
3113 const struct got_error *err = NULL;
3114 struct got_reference *ref;
3116 err = got_ref_open(&ref, repo, refname, 0);
3117 if (err)
3118 return err;
3120 err = got_ref_delete(ref, repo);
3121 got_ref_close(ref);
3122 return err;
3125 static const struct got_error *
3126 add_ref(struct got_repository *repo, const char *refname, const char *target)
3128 const struct got_error *err = NULL;
3129 struct got_object_id *id;
3130 struct got_reference *ref = NULL;
3133 * Don't let the user create a reference name with a leading '-'.
3134 * While technically a valid reference name, this case is usually
3135 * an unintended typo.
3137 if (refname[0] == '-')
3138 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3140 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3141 repo);
3142 if (err) {
3143 struct got_reference *target_ref;
3145 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3146 return err;
3147 err = got_ref_open(&target_ref, repo, target, 0);
3148 if (err)
3149 return err;
3150 err = got_ref_resolve(&id, repo, target_ref);
3151 got_ref_close(target_ref);
3152 if (err)
3153 return err;
3156 err = got_ref_alloc(&ref, refname, id);
3157 if (err)
3158 goto done;
3160 err = got_ref_write(ref, repo);
3161 done:
3162 if (ref)
3163 got_ref_close(ref);
3164 free(id);
3165 return err;
3168 static const struct got_error *
3169 add_symref(struct got_repository *repo, const char *refname, const char *target)
3171 const struct got_error *err = NULL;
3172 struct got_reference *ref = NULL;
3173 struct got_reference *target_ref = NULL;
3176 * Don't let the user create a reference name with a leading '-'.
3177 * While technically a valid reference name, this case is usually
3178 * an unintended typo.
3180 if (refname[0] == '-')
3181 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3183 err = got_ref_open(&target_ref, repo, target, 0);
3184 if (err)
3185 return err;
3187 err = got_ref_alloc_symref(&ref, refname, target_ref);
3188 if (err)
3189 goto done;
3191 err = got_ref_write(ref, repo);
3192 done:
3193 if (target_ref)
3194 got_ref_close(target_ref);
3195 if (ref)
3196 got_ref_close(ref);
3197 return err;
3200 static const struct got_error *
3201 cmd_ref(int argc, char *argv[])
3203 const struct got_error *error = NULL;
3204 struct got_repository *repo = NULL;
3205 struct got_worktree *worktree = NULL;
3206 char *cwd = NULL, *repo_path = NULL;
3207 int ch, do_list = 0, create_symref = 0;
3208 const char *delref = NULL;
3210 /* TODO: Add -s option for adding symbolic references. */
3211 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3212 switch (ch) {
3213 case 'd':
3214 delref = optarg;
3215 break;
3216 case 'r':
3217 repo_path = realpath(optarg, NULL);
3218 if (repo_path == NULL)
3219 return got_error_from_errno2("realpath",
3220 optarg);
3221 got_path_strip_trailing_slashes(repo_path);
3222 break;
3223 case 'l':
3224 do_list = 1;
3225 break;
3226 case 's':
3227 create_symref = 1;
3228 break;
3229 default:
3230 usage_ref();
3231 /* NOTREACHED */
3235 if (do_list && delref)
3236 errx(1, "-l and -d options are mutually exclusive\n");
3238 argc -= optind;
3239 argv += optind;
3241 if (do_list || delref) {
3242 if (create_symref)
3243 errx(1, "-s option cannot be used together with the "
3244 "-l or -d options");
3245 if (argc > 0)
3246 usage_ref();
3247 } else if (argc != 2)
3248 usage_ref();
3250 #ifndef PROFILE
3251 if (do_list) {
3252 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3253 NULL) == -1)
3254 err(1, "pledge");
3255 } else {
3256 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3257 "sendfd unveil", NULL) == -1)
3258 err(1, "pledge");
3260 #endif
3261 cwd = getcwd(NULL, 0);
3262 if (cwd == NULL) {
3263 error = got_error_from_errno("getcwd");
3264 goto done;
3267 if (repo_path == NULL) {
3268 error = got_worktree_open(&worktree, cwd);
3269 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3270 goto done;
3271 else
3272 error = NULL;
3273 if (worktree) {
3274 repo_path =
3275 strdup(got_worktree_get_repo_path(worktree));
3276 if (repo_path == NULL)
3277 error = got_error_from_errno("strdup");
3278 if (error)
3279 goto done;
3280 } else {
3281 repo_path = strdup(cwd);
3282 if (repo_path == NULL) {
3283 error = got_error_from_errno("strdup");
3284 goto done;
3289 error = got_repo_open(&repo, repo_path, NULL);
3290 if (error != NULL)
3291 goto done;
3293 error = apply_unveil(got_repo_get_path(repo), do_list,
3294 worktree ? got_worktree_get_root_path(worktree) : NULL);
3295 if (error)
3296 goto done;
3298 if (do_list)
3299 error = list_refs(repo);
3300 else if (delref)
3301 error = delete_ref(repo, delref);
3302 else if (create_symref)
3303 error = add_symref(repo, argv[0], argv[1]);
3304 else
3305 error = add_ref(repo, argv[0], argv[1]);
3306 done:
3307 if (repo)
3308 got_repo_close(repo);
3309 if (worktree)
3310 got_worktree_close(worktree);
3311 free(cwd);
3312 free(repo_path);
3313 return error;
3316 __dead static void
3317 usage_branch(void)
3319 fprintf(stderr,
3320 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3321 "[name]\n", getprogname());
3322 exit(1);
3325 static const struct got_error *
3326 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3327 struct got_reference *ref)
3329 const struct got_error *err = NULL;
3330 const char *refname, *marker = " ";
3331 char *refstr;
3333 refname = got_ref_get_name(ref);
3334 if (worktree && strcmp(refname,
3335 got_worktree_get_head_ref_name(worktree)) == 0) {
3336 struct got_object_id *id = NULL;
3338 err = got_ref_resolve(&id, repo, ref);
3339 if (err)
3340 return err;
3341 if (got_object_id_cmp(id,
3342 got_worktree_get_base_commit_id(worktree)) == 0)
3343 marker = "* ";
3344 else
3345 marker = "~ ";
3346 free(id);
3349 if (strncmp(refname, "refs/heads/", 11) == 0)
3350 refname += 11;
3351 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3352 refname += 18;
3354 refstr = got_ref_to_str(ref);
3355 if (refstr == NULL)
3356 return got_error_from_errno("got_ref_to_str");
3358 printf("%s%s: %s\n", marker, refname, refstr);
3359 free(refstr);
3360 return NULL;
3363 static const struct got_error *
3364 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3366 const char *refname;
3368 if (worktree == NULL)
3369 return got_error(GOT_ERR_NOT_WORKTREE);
3371 refname = got_worktree_get_head_ref_name(worktree);
3373 if (strncmp(refname, "refs/heads/", 11) == 0)
3374 refname += 11;
3375 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3376 refname += 18;
3378 printf("%s\n", refname);
3380 return NULL;
3383 static const struct got_error *
3384 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3386 static const struct got_error *err = NULL;
3387 struct got_reflist_head refs;
3388 struct got_reflist_entry *re;
3389 struct got_reference *temp_ref = NULL;
3390 int rebase_in_progress, histedit_in_progress;
3392 SIMPLEQ_INIT(&refs);
3394 if (worktree) {
3395 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3396 worktree);
3397 if (err)
3398 return err;
3400 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3401 worktree);
3402 if (err)
3403 return err;
3405 if (rebase_in_progress || histedit_in_progress) {
3406 err = got_ref_open(&temp_ref, repo,
3407 got_worktree_get_head_ref_name(worktree), 0);
3408 if (err)
3409 return err;
3410 list_branch(repo, worktree, temp_ref);
3411 got_ref_close(temp_ref);
3415 err = got_ref_list(&refs, repo, "refs/heads",
3416 got_ref_cmp_by_name, NULL);
3417 if (err)
3418 return err;
3420 SIMPLEQ_FOREACH(re, &refs, entry)
3421 list_branch(repo, worktree, re->ref);
3423 got_ref_list_free(&refs);
3424 return NULL;
3427 static const struct got_error *
3428 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3429 const char *branch_name)
3431 const struct got_error *err = NULL;
3432 struct got_reference *ref = NULL;
3433 char *refname;
3435 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3436 return got_error_from_errno("asprintf");
3438 err = got_ref_open(&ref, repo, refname, 0);
3439 if (err)
3440 goto done;
3442 if (worktree &&
3443 strcmp(got_worktree_get_head_ref_name(worktree),
3444 got_ref_get_name(ref)) == 0) {
3445 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3446 "will not delete this work tree's current branch");
3447 goto done;
3450 err = got_ref_delete(ref, repo);
3451 done:
3452 if (ref)
3453 got_ref_close(ref);
3454 free(refname);
3455 return err;
3458 static const struct got_error *
3459 add_branch(struct got_repository *repo, const char *branch_name,
3460 struct got_object_id *base_commit_id)
3462 const struct got_error *err = NULL;
3463 struct got_reference *ref = NULL;
3464 char *base_refname = NULL, *refname = NULL;
3467 * Don't let the user create a branch name with a leading '-'.
3468 * While technically a valid reference name, this case is usually
3469 * an unintended typo.
3471 if (branch_name[0] == '-')
3472 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3474 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3475 err = got_error_from_errno("asprintf");
3476 goto done;
3479 err = got_ref_open(&ref, repo, refname, 0);
3480 if (err == NULL) {
3481 err = got_error(GOT_ERR_BRANCH_EXISTS);
3482 goto done;
3483 } else if (err->code != GOT_ERR_NOT_REF)
3484 goto done;
3486 err = got_ref_alloc(&ref, refname, base_commit_id);
3487 if (err)
3488 goto done;
3490 err = got_ref_write(ref, repo);
3491 done:
3492 if (ref)
3493 got_ref_close(ref);
3494 free(base_refname);
3495 free(refname);
3496 return err;
3499 static const struct got_error *
3500 cmd_branch(int argc, char *argv[])
3502 const struct got_error *error = NULL;
3503 struct got_repository *repo = NULL;
3504 struct got_worktree *worktree = NULL;
3505 char *cwd = NULL, *repo_path = NULL;
3506 int ch, do_list = 0, do_show = 0, do_update = 1;
3507 const char *delref = NULL, *commit_id_arg = NULL;
3508 struct got_reference *ref = NULL;
3509 struct got_pathlist_head paths;
3510 struct got_pathlist_entry *pe;
3511 struct got_object_id *commit_id = NULL;
3512 char *commit_id_str = NULL;
3514 TAILQ_INIT(&paths);
3516 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3517 switch (ch) {
3518 case 'c':
3519 commit_id_arg = optarg;
3520 break;
3521 case 'd':
3522 delref = optarg;
3523 break;
3524 case 'r':
3525 repo_path = realpath(optarg, NULL);
3526 if (repo_path == NULL)
3527 return got_error_from_errno2("realpath",
3528 optarg);
3529 got_path_strip_trailing_slashes(repo_path);
3530 break;
3531 case 'l':
3532 do_list = 1;
3533 break;
3534 case 'n':
3535 do_update = 0;
3536 break;
3537 default:
3538 usage_branch();
3539 /* NOTREACHED */
3543 if (do_list && delref)
3544 errx(1, "-l and -d options are mutually exclusive\n");
3546 argc -= optind;
3547 argv += optind;
3549 if (!do_list && !delref && argc == 0)
3550 do_show = 1;
3552 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3553 errx(1, "-c option can only be used when creating a branch");
3555 if (do_list || delref) {
3556 if (argc > 0)
3557 usage_branch();
3558 } else if (!do_show && argc != 1)
3559 usage_branch();
3561 #ifndef PROFILE
3562 if (do_list || do_show) {
3563 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3564 NULL) == -1)
3565 err(1, "pledge");
3566 } else {
3567 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3568 "sendfd unveil", NULL) == -1)
3569 err(1, "pledge");
3571 #endif
3572 cwd = getcwd(NULL, 0);
3573 if (cwd == NULL) {
3574 error = got_error_from_errno("getcwd");
3575 goto done;
3578 if (repo_path == NULL) {
3579 error = got_worktree_open(&worktree, cwd);
3580 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3581 goto done;
3582 else
3583 error = NULL;
3584 if (worktree) {
3585 repo_path =
3586 strdup(got_worktree_get_repo_path(worktree));
3587 if (repo_path == NULL)
3588 error = got_error_from_errno("strdup");
3589 if (error)
3590 goto done;
3591 } else {
3592 repo_path = strdup(cwd);
3593 if (repo_path == NULL) {
3594 error = got_error_from_errno("strdup");
3595 goto done;
3600 error = got_repo_open(&repo, repo_path, NULL);
3601 if (error != NULL)
3602 goto done;
3604 error = apply_unveil(got_repo_get_path(repo), do_list,
3605 worktree ? got_worktree_get_root_path(worktree) : NULL);
3606 if (error)
3607 goto done;
3609 if (do_show)
3610 error = show_current_branch(repo, worktree);
3611 else if (do_list)
3612 error = list_branches(repo, worktree);
3613 else if (delref)
3614 error = delete_branch(repo, worktree, delref);
3615 else {
3616 if (commit_id_arg == NULL)
3617 commit_id_arg = worktree ?
3618 got_worktree_get_head_ref_name(worktree) :
3619 GOT_REF_HEAD;
3620 error = got_repo_match_object_id(&commit_id, NULL,
3621 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3622 if (error)
3623 goto done;
3624 error = add_branch(repo, argv[0], commit_id);
3625 if (error)
3626 goto done;
3627 if (worktree && do_update) {
3628 int did_something = 0;
3629 char *branch_refname = NULL;
3631 error = got_object_id_str(&commit_id_str, commit_id);
3632 if (error)
3633 goto done;
3634 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3635 worktree);
3636 if (error)
3637 goto done;
3638 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3639 == -1) {
3640 error = got_error_from_errno("asprintf");
3641 goto done;
3643 error = got_ref_open(&ref, repo, branch_refname, 0);
3644 free(branch_refname);
3645 if (error)
3646 goto done;
3647 error = switch_head_ref(ref, commit_id, worktree,
3648 repo);
3649 if (error)
3650 goto done;
3651 error = got_worktree_set_base_commit_id(worktree, repo,
3652 commit_id);
3653 if (error)
3654 goto done;
3655 error = got_worktree_checkout_files(worktree, &paths,
3656 repo, update_progress, &did_something,
3657 check_cancelled, NULL);
3658 if (error)
3659 goto done;
3660 if (did_something)
3661 printf("Updated to commit %s\n", commit_id_str);
3664 done:
3665 if (ref)
3666 got_ref_close(ref);
3667 if (repo)
3668 got_repo_close(repo);
3669 if (worktree)
3670 got_worktree_close(worktree);
3671 free(cwd);
3672 free(repo_path);
3673 free(commit_id);
3674 free(commit_id_str);
3675 TAILQ_FOREACH(pe, &paths, entry)
3676 free((char *)pe->path);
3677 got_pathlist_free(&paths);
3678 return error;
3682 __dead static void
3683 usage_tag(void)
3685 fprintf(stderr,
3686 "usage: %s tag [-c commit] [-r repository] [-l] "
3687 "[-m message] name\n", getprogname());
3688 exit(1);
3691 #if 0
3692 static const struct got_error *
3693 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3695 const struct got_error *err = NULL;
3696 struct got_reflist_entry *re, *se, *new;
3697 struct got_object_id *re_id, *se_id;
3698 struct got_tag_object *re_tag, *se_tag;
3699 time_t re_time, se_time;
3701 SIMPLEQ_FOREACH(re, tags, entry) {
3702 se = SIMPLEQ_FIRST(sorted);
3703 if (se == NULL) {
3704 err = got_reflist_entry_dup(&new, re);
3705 if (err)
3706 return err;
3707 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3708 continue;
3709 } else {
3710 err = got_ref_resolve(&re_id, repo, re->ref);
3711 if (err)
3712 break;
3713 err = got_object_open_as_tag(&re_tag, repo, re_id);
3714 free(re_id);
3715 if (err)
3716 break;
3717 re_time = got_object_tag_get_tagger_time(re_tag);
3718 got_object_tag_close(re_tag);
3721 while (se) {
3722 err = got_ref_resolve(&se_id, repo, re->ref);
3723 if (err)
3724 break;
3725 err = got_object_open_as_tag(&se_tag, repo, se_id);
3726 free(se_id);
3727 if (err)
3728 break;
3729 se_time = got_object_tag_get_tagger_time(se_tag);
3730 got_object_tag_close(se_tag);
3732 if (se_time > re_time) {
3733 err = got_reflist_entry_dup(&new, re);
3734 if (err)
3735 return err;
3736 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3737 break;
3739 se = SIMPLEQ_NEXT(se, entry);
3740 continue;
3743 done:
3744 return err;
3746 #endif
3748 static const struct got_error *
3749 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3751 static const struct got_error *err = NULL;
3752 struct got_reflist_head refs;
3753 struct got_reflist_entry *re;
3755 SIMPLEQ_INIT(&refs);
3757 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3758 if (err)
3759 return err;
3761 SIMPLEQ_FOREACH(re, &refs, entry) {
3762 const char *refname;
3763 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3764 char datebuf[26];
3765 const char *tagger;
3766 time_t tagger_time;
3767 struct got_object_id *id;
3768 struct got_tag_object *tag;
3769 struct got_commit_object *commit = NULL;
3771 refname = got_ref_get_name(re->ref);
3772 if (strncmp(refname, "refs/tags/", 10) != 0)
3773 continue;
3774 refname += 10;
3775 refstr = got_ref_to_str(re->ref);
3776 if (refstr == NULL) {
3777 err = got_error_from_errno("got_ref_to_str");
3778 break;
3780 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3781 free(refstr);
3783 err = got_ref_resolve(&id, repo, re->ref);
3784 if (err)
3785 break;
3786 err = got_object_open_as_tag(&tag, repo, id);
3787 if (err) {
3788 if (err->code != GOT_ERR_OBJ_TYPE) {
3789 free(id);
3790 break;
3792 /* "lightweight" tag */
3793 err = got_object_open_as_commit(&commit, repo, id);
3794 if (err) {
3795 free(id);
3796 break;
3798 tagger = got_object_commit_get_committer(commit);
3799 tagger_time =
3800 got_object_commit_get_committer_time(commit);
3801 err = got_object_id_str(&id_str, id);
3802 free(id);
3803 if (err)
3804 break;
3805 } else {
3806 free(id);
3807 tagger = got_object_tag_get_tagger(tag);
3808 tagger_time = got_object_tag_get_tagger_time(tag);
3809 err = got_object_id_str(&id_str,
3810 got_object_tag_get_object_id(tag));
3811 if (err)
3812 break;
3814 printf("from: %s\n", tagger);
3815 datestr = get_datestr(&tagger_time, datebuf);
3816 if (datestr)
3817 printf("date: %s UTC\n", datestr);
3818 if (commit)
3819 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3820 else {
3821 switch (got_object_tag_get_object_type(tag)) {
3822 case GOT_OBJ_TYPE_BLOB:
3823 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3824 id_str);
3825 break;
3826 case GOT_OBJ_TYPE_TREE:
3827 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3828 id_str);
3829 break;
3830 case GOT_OBJ_TYPE_COMMIT:
3831 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3832 id_str);
3833 break;
3834 case GOT_OBJ_TYPE_TAG:
3835 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3836 id_str);
3837 break;
3838 default:
3839 break;
3842 free(id_str);
3843 if (commit) {
3844 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3845 if (err)
3846 break;
3847 got_object_commit_close(commit);
3848 } else {
3849 tagmsg0 = strdup(got_object_tag_get_message(tag));
3850 got_object_tag_close(tag);
3851 if (tagmsg0 == NULL) {
3852 err = got_error_from_errno("strdup");
3853 break;
3857 tagmsg = tagmsg0;
3858 do {
3859 line = strsep(&tagmsg, "\n");
3860 if (line)
3861 printf(" %s\n", line);
3862 } while (line);
3863 free(tagmsg0);
3866 got_ref_list_free(&refs);
3867 return NULL;
3870 static const struct got_error *
3871 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3872 const char *tag_name, const char *repo_path)
3874 const struct got_error *err = NULL;
3875 char *template = NULL, *initial_content = NULL;
3876 char *editor = NULL;
3877 int fd = -1;
3879 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3880 err = got_error_from_errno("asprintf");
3881 goto done;
3884 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3885 commit_id_str, tag_name) == -1) {
3886 err = got_error_from_errno("asprintf");
3887 goto done;
3890 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3891 if (err)
3892 goto done;
3894 dprintf(fd, initial_content);
3895 close(fd);
3897 err = get_editor(&editor);
3898 if (err)
3899 goto done;
3900 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3901 done:
3902 free(initial_content);
3903 free(template);
3904 free(editor);
3906 /* Editor is done; we can now apply unveil(2) */
3907 if (err == NULL) {
3908 err = apply_unveil(repo_path, 0, NULL);
3909 if (err) {
3910 free(*tagmsg);
3911 *tagmsg = NULL;
3914 return err;
3917 static const struct got_error *
3918 add_tag(struct got_repository *repo, const char *tag_name,
3919 const char *commit_arg, const char *tagmsg_arg)
3921 const struct got_error *err = NULL;
3922 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3923 char *label = NULL, *commit_id_str = NULL;
3924 struct got_reference *ref = NULL;
3925 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3926 char *tagmsg_path = NULL, *tag_id_str = NULL;
3927 int preserve_tagmsg = 0;
3930 * Don't let the user create a tag name with a leading '-'.
3931 * While technically a valid reference name, this case is usually
3932 * an unintended typo.
3934 if (tag_name[0] == '-')
3935 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3937 err = get_author(&tagger, repo);
3938 if (err)
3939 return err;
3941 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3942 GOT_OBJ_TYPE_COMMIT, 1, repo);
3943 if (err)
3944 goto done;
3946 err = got_object_id_str(&commit_id_str, commit_id);
3947 if (err)
3948 goto done;
3950 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3951 refname = strdup(tag_name);
3952 if (refname == NULL) {
3953 err = got_error_from_errno("strdup");
3954 goto done;
3956 tag_name += 10;
3957 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3958 err = got_error_from_errno("asprintf");
3959 goto done;
3962 err = got_ref_open(&ref, repo, refname, 0);
3963 if (err == NULL) {
3964 err = got_error(GOT_ERR_TAG_EXISTS);
3965 goto done;
3966 } else if (err->code != GOT_ERR_NOT_REF)
3967 goto done;
3969 if (tagmsg_arg == NULL) {
3970 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3971 tag_name, got_repo_get_path(repo));
3972 if (err) {
3973 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3974 tagmsg_path != NULL)
3975 preserve_tagmsg = 1;
3976 goto done;
3980 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3981 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3982 if (err) {
3983 if (tagmsg_path)
3984 preserve_tagmsg = 1;
3985 goto done;
3988 err = got_ref_alloc(&ref, refname, tag_id);
3989 if (err) {
3990 if (tagmsg_path)
3991 preserve_tagmsg = 1;
3992 goto done;
3995 err = got_ref_write(ref, repo);
3996 if (err) {
3997 if (tagmsg_path)
3998 preserve_tagmsg = 1;
3999 goto done;
4002 err = got_object_id_str(&tag_id_str, tag_id);
4003 if (err) {
4004 if (tagmsg_path)
4005 preserve_tagmsg = 1;
4006 goto done;
4008 printf("Created tag %s\n", tag_id_str);
4009 done:
4010 if (preserve_tagmsg) {
4011 fprintf(stderr, "%s: tag message preserved in %s\n",
4012 getprogname(), tagmsg_path);
4013 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4014 err = got_error_from_errno2("unlink", tagmsg_path);
4015 free(tag_id_str);
4016 if (ref)
4017 got_ref_close(ref);
4018 free(commit_id);
4019 free(commit_id_str);
4020 free(refname);
4021 free(tagmsg);
4022 free(tagmsg_path);
4023 free(tagger);
4024 return err;
4027 static const struct got_error *
4028 cmd_tag(int argc, char *argv[])
4030 const struct got_error *error = NULL;
4031 struct got_repository *repo = NULL;
4032 struct got_worktree *worktree = NULL;
4033 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4034 char *gitconfig_path = NULL;
4035 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4036 int ch, do_list = 0;
4038 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4039 switch (ch) {
4040 case 'c':
4041 commit_id_arg = optarg;
4042 break;
4043 case 'm':
4044 tagmsg = optarg;
4045 break;
4046 case 'r':
4047 repo_path = realpath(optarg, NULL);
4048 if (repo_path == NULL)
4049 return got_error_from_errno2("realpath",
4050 optarg);
4051 got_path_strip_trailing_slashes(repo_path);
4052 break;
4053 case 'l':
4054 do_list = 1;
4055 break;
4056 default:
4057 usage_tag();
4058 /* NOTREACHED */
4062 argc -= optind;
4063 argv += optind;
4065 if (do_list) {
4066 if (commit_id_arg != NULL)
4067 errx(1, "-c option can only be used when creating a tag");
4068 if (tagmsg)
4069 errx(1, "-l and -m options are mutually exclusive");
4070 if (argc > 0)
4071 usage_tag();
4072 } else if (argc != 1)
4073 usage_tag();
4075 tag_name = argv[0];
4077 #ifndef PROFILE
4078 if (do_list) {
4079 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4080 NULL) == -1)
4081 err(1, "pledge");
4082 } else {
4083 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4084 "sendfd unveil", NULL) == -1)
4085 err(1, "pledge");
4087 #endif
4088 cwd = getcwd(NULL, 0);
4089 if (cwd == NULL) {
4090 error = got_error_from_errno("getcwd");
4091 goto done;
4094 if (repo_path == NULL) {
4095 error = got_worktree_open(&worktree, cwd);
4096 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4097 goto done;
4098 else
4099 error = NULL;
4100 if (worktree) {
4101 repo_path =
4102 strdup(got_worktree_get_repo_path(worktree));
4103 if (repo_path == NULL)
4104 error = got_error_from_errno("strdup");
4105 if (error)
4106 goto done;
4107 } else {
4108 repo_path = strdup(cwd);
4109 if (repo_path == NULL) {
4110 error = got_error_from_errno("strdup");
4111 goto done;
4116 if (do_list) {
4117 error = got_repo_open(&repo, repo_path, NULL);
4118 if (error != NULL)
4119 goto done;
4120 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4121 if (error)
4122 goto done;
4123 error = list_tags(repo, worktree);
4124 } else {
4125 error = get_gitconfig_path(&gitconfig_path);
4126 if (error)
4127 goto done;
4128 error = got_repo_open(&repo, repo_path, gitconfig_path);
4129 if (error != NULL)
4130 goto done;
4132 if (tagmsg) {
4133 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4134 if (error)
4135 goto done;
4138 if (commit_id_arg == NULL) {
4139 struct got_reference *head_ref;
4140 struct got_object_id *commit_id;
4141 error = got_ref_open(&head_ref, repo,
4142 worktree ? got_worktree_get_head_ref_name(worktree)
4143 : GOT_REF_HEAD, 0);
4144 if (error)
4145 goto done;
4146 error = got_ref_resolve(&commit_id, repo, head_ref);
4147 got_ref_close(head_ref);
4148 if (error)
4149 goto done;
4150 error = got_object_id_str(&commit_id_str, commit_id);
4151 free(commit_id);
4152 if (error)
4153 goto done;
4156 error = add_tag(repo, tag_name,
4157 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4159 done:
4160 if (repo)
4161 got_repo_close(repo);
4162 if (worktree)
4163 got_worktree_close(worktree);
4164 free(cwd);
4165 free(repo_path);
4166 free(gitconfig_path);
4167 free(commit_id_str);
4168 return error;
4171 __dead static void
4172 usage_add(void)
4174 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4175 getprogname());
4176 exit(1);
4179 static const struct got_error *
4180 add_progress(void *arg, unsigned char status, const char *path)
4182 while (path[0] == '/')
4183 path++;
4184 printf("%c %s\n", status, path);
4185 return NULL;
4188 static const struct got_error *
4189 cmd_add(int argc, char *argv[])
4191 const struct got_error *error = NULL;
4192 struct got_repository *repo = NULL;
4193 struct got_worktree *worktree = NULL;
4194 char *cwd = NULL;
4195 struct got_pathlist_head paths;
4196 struct got_pathlist_entry *pe;
4197 int ch, can_recurse = 0, no_ignores = 0;
4199 TAILQ_INIT(&paths);
4201 while ((ch = getopt(argc, argv, "IR")) != -1) {
4202 switch (ch) {
4203 case 'I':
4204 no_ignores = 1;
4205 break;
4206 case 'R':
4207 can_recurse = 1;
4208 break;
4209 default:
4210 usage_add();
4211 /* NOTREACHED */
4215 argc -= optind;
4216 argv += optind;
4218 #ifndef PROFILE
4219 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4220 NULL) == -1)
4221 err(1, "pledge");
4222 #endif
4223 if (argc < 1)
4224 usage_add();
4226 cwd = getcwd(NULL, 0);
4227 if (cwd == NULL) {
4228 error = got_error_from_errno("getcwd");
4229 goto done;
4232 error = got_worktree_open(&worktree, cwd);
4233 if (error)
4234 goto done;
4236 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4237 NULL);
4238 if (error != NULL)
4239 goto done;
4241 error = apply_unveil(got_repo_get_path(repo), 1,
4242 got_worktree_get_root_path(worktree));
4243 if (error)
4244 goto done;
4246 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4247 if (error)
4248 goto done;
4250 if (!can_recurse && no_ignores) {
4251 error = got_error_msg(GOT_ERR_BAD_PATH,
4252 "disregarding ignores requires -R option");
4253 goto done;
4257 if (!can_recurse) {
4258 char *ondisk_path;
4259 struct stat sb;
4260 TAILQ_FOREACH(pe, &paths, entry) {
4261 if (asprintf(&ondisk_path, "%s/%s",
4262 got_worktree_get_root_path(worktree),
4263 pe->path) == -1) {
4264 error = got_error_from_errno("asprintf");
4265 goto done;
4267 if (lstat(ondisk_path, &sb) == -1) {
4268 if (errno == ENOENT) {
4269 free(ondisk_path);
4270 continue;
4272 error = got_error_from_errno2("lstat",
4273 ondisk_path);
4274 free(ondisk_path);
4275 goto done;
4277 free(ondisk_path);
4278 if (S_ISDIR(sb.st_mode)) {
4279 error = got_error_msg(GOT_ERR_BAD_PATH,
4280 "adding directories requires -R option");
4281 goto done;
4286 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4287 NULL, repo, no_ignores);
4288 done:
4289 if (repo)
4290 got_repo_close(repo);
4291 if (worktree)
4292 got_worktree_close(worktree);
4293 TAILQ_FOREACH(pe, &paths, entry)
4294 free((char *)pe->path);
4295 got_pathlist_free(&paths);
4296 free(cwd);
4297 return error;
4300 __dead static void
4301 usage_remove(void)
4303 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4304 getprogname());
4305 exit(1);
4308 static const struct got_error *
4309 print_remove_status(void *arg, unsigned char status,
4310 unsigned char staged_status, const char *path)
4312 while (path[0] == '/')
4313 path++;
4314 if (status == GOT_STATUS_NONEXISTENT)
4315 return NULL;
4316 if (status == staged_status && (status == GOT_STATUS_DELETE))
4317 status = GOT_STATUS_NO_CHANGE;
4318 printf("%c%c %s\n", status, staged_status, path);
4319 return NULL;
4322 static const struct got_error *
4323 cmd_remove(int argc, char *argv[])
4325 const struct got_error *error = NULL;
4326 struct got_worktree *worktree = NULL;
4327 struct got_repository *repo = NULL;
4328 char *cwd = NULL;
4329 struct got_pathlist_head paths;
4330 struct got_pathlist_entry *pe;
4331 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4333 TAILQ_INIT(&paths);
4335 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4336 switch (ch) {
4337 case 'f':
4338 delete_local_mods = 1;
4339 break;
4340 case 'k':
4341 keep_on_disk = 1;
4342 break;
4343 case 'R':
4344 can_recurse = 1;
4345 break;
4346 default:
4347 usage_remove();
4348 /* NOTREACHED */
4352 argc -= optind;
4353 argv += optind;
4355 #ifndef PROFILE
4356 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4357 NULL) == -1)
4358 err(1, "pledge");
4359 #endif
4360 if (argc < 1)
4361 usage_remove();
4363 cwd = getcwd(NULL, 0);
4364 if (cwd == NULL) {
4365 error = got_error_from_errno("getcwd");
4366 goto done;
4368 error = got_worktree_open(&worktree, cwd);
4369 if (error)
4370 goto done;
4372 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4373 NULL);
4374 if (error)
4375 goto done;
4377 error = apply_unveil(got_repo_get_path(repo), 1,
4378 got_worktree_get_root_path(worktree));
4379 if (error)
4380 goto done;
4382 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4383 if (error)
4384 goto done;
4386 if (!can_recurse) {
4387 char *ondisk_path;
4388 struct stat sb;
4389 TAILQ_FOREACH(pe, &paths, entry) {
4390 if (asprintf(&ondisk_path, "%s/%s",
4391 got_worktree_get_root_path(worktree),
4392 pe->path) == -1) {
4393 error = got_error_from_errno("asprintf");
4394 goto done;
4396 if (lstat(ondisk_path, &sb) == -1) {
4397 if (errno == ENOENT) {
4398 free(ondisk_path);
4399 continue;
4401 error = got_error_from_errno2("lstat",
4402 ondisk_path);
4403 free(ondisk_path);
4404 goto done;
4406 free(ondisk_path);
4407 if (S_ISDIR(sb.st_mode)) {
4408 error = got_error_msg(GOT_ERR_BAD_PATH,
4409 "removing directories requires -R option");
4410 goto done;
4415 error = got_worktree_schedule_delete(worktree, &paths,
4416 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4417 done:
4418 if (repo)
4419 got_repo_close(repo);
4420 if (worktree)
4421 got_worktree_close(worktree);
4422 TAILQ_FOREACH(pe, &paths, entry)
4423 free((char *)pe->path);
4424 got_pathlist_free(&paths);
4425 free(cwd);
4426 return error;
4429 __dead static void
4430 usage_revert(void)
4432 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4433 "path ...\n", getprogname());
4434 exit(1);
4437 static const struct got_error *
4438 revert_progress(void *arg, unsigned char status, const char *path)
4440 if (status == GOT_STATUS_UNVERSIONED)
4441 return NULL;
4443 while (path[0] == '/')
4444 path++;
4445 printf("%c %s\n", status, path);
4446 return NULL;
4449 struct choose_patch_arg {
4450 FILE *patch_script_file;
4451 const char *action;
4454 static const struct got_error *
4455 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4456 int nchanges, const char *action)
4458 char *line = NULL;
4459 size_t linesize = 0;
4460 ssize_t linelen;
4462 switch (status) {
4463 case GOT_STATUS_ADD:
4464 printf("A %s\n%s this addition? [y/n] ", path, action);
4465 break;
4466 case GOT_STATUS_DELETE:
4467 printf("D %s\n%s this deletion? [y/n] ", path, action);
4468 break;
4469 case GOT_STATUS_MODIFY:
4470 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4471 return got_error_from_errno("fseek");
4472 printf(GOT_COMMIT_SEP_STR);
4473 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4474 printf("%s", line);
4475 if (ferror(patch_file))
4476 return got_error_from_errno("getline");
4477 printf(GOT_COMMIT_SEP_STR);
4478 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4479 path, n, nchanges, action);
4480 break;
4481 default:
4482 return got_error_path(path, GOT_ERR_FILE_STATUS);
4485 return NULL;
4488 static const struct got_error *
4489 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4490 FILE *patch_file, int n, int nchanges)
4492 const struct got_error *err = NULL;
4493 char *line = NULL;
4494 size_t linesize = 0;
4495 ssize_t linelen;
4496 int resp = ' ';
4497 struct choose_patch_arg *a = arg;
4499 *choice = GOT_PATCH_CHOICE_NONE;
4501 if (a->patch_script_file) {
4502 char *nl;
4503 err = show_change(status, path, patch_file, n, nchanges,
4504 a->action);
4505 if (err)
4506 return err;
4507 linelen = getline(&line, &linesize, a->patch_script_file);
4508 if (linelen == -1) {
4509 if (ferror(a->patch_script_file))
4510 return got_error_from_errno("getline");
4511 return NULL;
4513 nl = strchr(line, '\n');
4514 if (nl)
4515 *nl = '\0';
4516 if (strcmp(line, "y") == 0) {
4517 *choice = GOT_PATCH_CHOICE_YES;
4518 printf("y\n");
4519 } else if (strcmp(line, "n") == 0) {
4520 *choice = GOT_PATCH_CHOICE_NO;
4521 printf("n\n");
4522 } else if (strcmp(line, "q") == 0 &&
4523 status == GOT_STATUS_MODIFY) {
4524 *choice = GOT_PATCH_CHOICE_QUIT;
4525 printf("q\n");
4526 } else
4527 printf("invalid response '%s'\n", line);
4528 free(line);
4529 return NULL;
4532 while (resp != 'y' && resp != 'n' && resp != 'q') {
4533 err = show_change(status, path, patch_file, n, nchanges,
4534 a->action);
4535 if (err)
4536 return err;
4537 resp = getchar();
4538 if (resp == '\n')
4539 resp = getchar();
4540 if (status == GOT_STATUS_MODIFY) {
4541 if (resp != 'y' && resp != 'n' && resp != 'q') {
4542 printf("invalid response '%c'\n", resp);
4543 resp = ' ';
4545 } else if (resp != 'y' && resp != 'n') {
4546 printf("invalid response '%c'\n", resp);
4547 resp = ' ';
4551 if (resp == 'y')
4552 *choice = GOT_PATCH_CHOICE_YES;
4553 else if (resp == 'n')
4554 *choice = GOT_PATCH_CHOICE_NO;
4555 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4556 *choice = GOT_PATCH_CHOICE_QUIT;
4558 return NULL;
4562 static const struct got_error *
4563 cmd_revert(int argc, char *argv[])
4565 const struct got_error *error = NULL;
4566 struct got_worktree *worktree = NULL;
4567 struct got_repository *repo = NULL;
4568 char *cwd = NULL, *path = NULL;
4569 struct got_pathlist_head paths;
4570 struct got_pathlist_entry *pe;
4571 int ch, can_recurse = 0, pflag = 0;
4572 FILE *patch_script_file = NULL;
4573 const char *patch_script_path = NULL;
4574 struct choose_patch_arg cpa;
4576 TAILQ_INIT(&paths);
4578 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4579 switch (ch) {
4580 case 'p':
4581 pflag = 1;
4582 break;
4583 case 'F':
4584 patch_script_path = optarg;
4585 break;
4586 case 'R':
4587 can_recurse = 1;
4588 break;
4589 default:
4590 usage_revert();
4591 /* NOTREACHED */
4595 argc -= optind;
4596 argv += optind;
4598 #ifndef PROFILE
4599 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4600 "unveil", NULL) == -1)
4601 err(1, "pledge");
4602 #endif
4603 if (argc < 1)
4604 usage_revert();
4605 if (patch_script_path && !pflag)
4606 errx(1, "-F option can only be used together with -p option");
4608 cwd = getcwd(NULL, 0);
4609 if (cwd == NULL) {
4610 error = got_error_from_errno("getcwd");
4611 goto done;
4613 error = got_worktree_open(&worktree, cwd);
4614 if (error)
4615 goto done;
4617 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4618 NULL);
4619 if (error != NULL)
4620 goto done;
4622 if (patch_script_path) {
4623 patch_script_file = fopen(patch_script_path, "r");
4624 if (patch_script_file == NULL) {
4625 error = got_error_from_errno2("fopen",
4626 patch_script_path);
4627 goto done;
4630 error = apply_unveil(got_repo_get_path(repo), 1,
4631 got_worktree_get_root_path(worktree));
4632 if (error)
4633 goto done;
4635 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4636 if (error)
4637 goto done;
4639 if (!can_recurse) {
4640 char *ondisk_path;
4641 struct stat sb;
4642 TAILQ_FOREACH(pe, &paths, entry) {
4643 if (asprintf(&ondisk_path, "%s/%s",
4644 got_worktree_get_root_path(worktree),
4645 pe->path) == -1) {
4646 error = got_error_from_errno("asprintf");
4647 goto done;
4649 if (lstat(ondisk_path, &sb) == -1) {
4650 if (errno == ENOENT) {
4651 free(ondisk_path);
4652 continue;
4654 error = got_error_from_errno2("lstat",
4655 ondisk_path);
4656 free(ondisk_path);
4657 goto done;
4659 free(ondisk_path);
4660 if (S_ISDIR(sb.st_mode)) {
4661 error = got_error_msg(GOT_ERR_BAD_PATH,
4662 "reverting directories requires -R option");
4663 goto done;
4668 cpa.patch_script_file = patch_script_file;
4669 cpa.action = "revert";
4670 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4671 pflag ? choose_patch : NULL, &cpa, repo);
4672 done:
4673 if (patch_script_file && fclose(patch_script_file) == EOF &&
4674 error == NULL)
4675 error = got_error_from_errno2("fclose", patch_script_path);
4676 if (repo)
4677 got_repo_close(repo);
4678 if (worktree)
4679 got_worktree_close(worktree);
4680 free(path);
4681 free(cwd);
4682 return error;
4685 __dead static void
4686 usage_commit(void)
4688 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4689 getprogname());
4690 exit(1);
4693 struct collect_commit_logmsg_arg {
4694 const char *cmdline_log;
4695 const char *editor;
4696 const char *worktree_path;
4697 const char *branch_name;
4698 const char *repo_path;
4699 char *logmsg_path;
4703 static const struct got_error *
4704 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4705 void *arg)
4707 char *initial_content = NULL;
4708 struct got_pathlist_entry *pe;
4709 const struct got_error *err = NULL;
4710 char *template = NULL;
4711 struct collect_commit_logmsg_arg *a = arg;
4712 int fd;
4713 size_t len;
4715 /* if a message was specified on the command line, just use it */
4716 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4717 len = strlen(a->cmdline_log) + 1;
4718 *logmsg = malloc(len + 1);
4719 if (*logmsg == NULL)
4720 return got_error_from_errno("malloc");
4721 strlcpy(*logmsg, a->cmdline_log, len);
4722 return NULL;
4725 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4726 return got_error_from_errno("asprintf");
4728 if (asprintf(&initial_content,
4729 "\n# changes to be committed on branch %s:\n",
4730 a->branch_name) == -1)
4731 return got_error_from_errno("asprintf");
4733 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4734 if (err)
4735 goto done;
4737 dprintf(fd, initial_content);
4739 TAILQ_FOREACH(pe, commitable_paths, entry) {
4740 struct got_commitable *ct = pe->data;
4741 dprintf(fd, "# %c %s\n",
4742 got_commitable_get_status(ct),
4743 got_commitable_get_path(ct));
4745 close(fd);
4747 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4748 done:
4749 free(initial_content);
4750 free(template);
4752 /* Editor is done; we can now apply unveil(2) */
4753 if (err == NULL) {
4754 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4755 if (err) {
4756 free(*logmsg);
4757 *logmsg = NULL;
4760 return err;
4763 static const struct got_error *
4764 cmd_commit(int argc, char *argv[])
4766 const struct got_error *error = NULL;
4767 struct got_worktree *worktree = NULL;
4768 struct got_repository *repo = NULL;
4769 char *cwd = NULL, *id_str = NULL;
4770 struct got_object_id *id = NULL;
4771 const char *logmsg = NULL;
4772 struct collect_commit_logmsg_arg cl_arg;
4773 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4774 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4775 struct got_pathlist_head paths;
4777 TAILQ_INIT(&paths);
4778 cl_arg.logmsg_path = NULL;
4780 while ((ch = getopt(argc, argv, "m:")) != -1) {
4781 switch (ch) {
4782 case 'm':
4783 logmsg = optarg;
4784 break;
4785 default:
4786 usage_commit();
4787 /* NOTREACHED */
4791 argc -= optind;
4792 argv += optind;
4794 #ifndef PROFILE
4795 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4796 "unveil", NULL) == -1)
4797 err(1, "pledge");
4798 #endif
4799 cwd = getcwd(NULL, 0);
4800 if (cwd == NULL) {
4801 error = got_error_from_errno("getcwd");
4802 goto done;
4804 error = got_worktree_open(&worktree, cwd);
4805 if (error)
4806 goto done;
4808 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4809 if (error)
4810 goto done;
4811 if (rebase_in_progress) {
4812 error = got_error(GOT_ERR_REBASING);
4813 goto done;
4816 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4817 worktree);
4818 if (error)
4819 goto done;
4821 error = get_gitconfig_path(&gitconfig_path);
4822 if (error)
4823 goto done;
4824 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4825 gitconfig_path);
4826 if (error != NULL)
4827 goto done;
4829 error = get_author(&author, repo);
4830 if (error)
4831 return error;
4834 * unveil(2) traverses exec(2); if an editor is used we have
4835 * to apply unveil after the log message has been written.
4837 if (logmsg == NULL || strlen(logmsg) == 0)
4838 error = get_editor(&editor);
4839 else
4840 error = apply_unveil(got_repo_get_path(repo), 0,
4841 got_worktree_get_root_path(worktree));
4842 if (error)
4843 goto done;
4845 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4846 if (error)
4847 goto done;
4849 cl_arg.editor = editor;
4850 cl_arg.cmdline_log = logmsg;
4851 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4852 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4853 if (!histedit_in_progress) {
4854 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4855 error = got_error(GOT_ERR_COMMIT_BRANCH);
4856 goto done;
4858 cl_arg.branch_name += 11;
4860 cl_arg.repo_path = got_repo_get_path(repo);
4861 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4862 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4863 if (error) {
4864 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4865 cl_arg.logmsg_path != NULL)
4866 preserve_logmsg = 1;
4867 goto done;
4870 error = got_object_id_str(&id_str, id);
4871 if (error)
4872 goto done;
4873 printf("Created commit %s\n", id_str);
4874 done:
4875 if (preserve_logmsg) {
4876 fprintf(stderr, "%s: log message preserved in %s\n",
4877 getprogname(), cl_arg.logmsg_path);
4878 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4879 error == NULL)
4880 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4881 free(cl_arg.logmsg_path);
4882 if (repo)
4883 got_repo_close(repo);
4884 if (worktree)
4885 got_worktree_close(worktree);
4886 free(cwd);
4887 free(id_str);
4888 free(gitconfig_path);
4889 free(editor);
4890 free(author);
4891 return error;
4894 __dead static void
4895 usage_cherrypick(void)
4897 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4898 exit(1);
4901 static const struct got_error *
4902 cmd_cherrypick(int argc, char *argv[])
4904 const struct got_error *error = NULL;
4905 struct got_worktree *worktree = NULL;
4906 struct got_repository *repo = NULL;
4907 char *cwd = NULL, *commit_id_str = NULL;
4908 struct got_object_id *commit_id = NULL;
4909 struct got_commit_object *commit = NULL;
4910 struct got_object_qid *pid;
4911 struct got_reference *head_ref = NULL;
4912 int ch, did_something = 0;
4914 while ((ch = getopt(argc, argv, "")) != -1) {
4915 switch (ch) {
4916 default:
4917 usage_cherrypick();
4918 /* NOTREACHED */
4922 argc -= optind;
4923 argv += optind;
4925 #ifndef PROFILE
4926 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4927 "unveil", NULL) == -1)
4928 err(1, "pledge");
4929 #endif
4930 if (argc != 1)
4931 usage_cherrypick();
4933 cwd = getcwd(NULL, 0);
4934 if (cwd == NULL) {
4935 error = got_error_from_errno("getcwd");
4936 goto done;
4938 error = got_worktree_open(&worktree, cwd);
4939 if (error)
4940 goto done;
4942 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4943 NULL);
4944 if (error != NULL)
4945 goto done;
4947 error = apply_unveil(got_repo_get_path(repo), 0,
4948 got_worktree_get_root_path(worktree));
4949 if (error)
4950 goto done;
4952 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4953 GOT_OBJ_TYPE_COMMIT, repo);
4954 if (error != NULL) {
4955 struct got_reference *ref;
4956 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4957 goto done;
4958 error = got_ref_open(&ref, repo, argv[0], 0);
4959 if (error != NULL)
4960 goto done;
4961 error = got_ref_resolve(&commit_id, repo, ref);
4962 got_ref_close(ref);
4963 if (error != NULL)
4964 goto done;
4966 error = got_object_id_str(&commit_id_str, commit_id);
4967 if (error)
4968 goto done;
4970 error = got_ref_open(&head_ref, repo,
4971 got_worktree_get_head_ref_name(worktree), 0);
4972 if (error != NULL)
4973 goto done;
4975 error = check_same_branch(commit_id, head_ref, NULL, repo);
4976 if (error) {
4977 if (error->code != GOT_ERR_ANCESTRY)
4978 goto done;
4979 error = NULL;
4980 } else {
4981 error = got_error(GOT_ERR_SAME_BRANCH);
4982 goto done;
4985 error = got_object_open_as_commit(&commit, repo, commit_id);
4986 if (error)
4987 goto done;
4988 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4989 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4990 commit_id, repo, update_progress, &did_something, check_cancelled,
4991 NULL);
4992 if (error != NULL)
4993 goto done;
4995 if (did_something)
4996 printf("Merged commit %s\n", commit_id_str);
4997 done:
4998 if (commit)
4999 got_object_commit_close(commit);
5000 free(commit_id_str);
5001 if (head_ref)
5002 got_ref_close(head_ref);
5003 if (worktree)
5004 got_worktree_close(worktree);
5005 if (repo)
5006 got_repo_close(repo);
5007 return error;
5010 __dead static void
5011 usage_backout(void)
5013 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5014 exit(1);
5017 static const struct got_error *
5018 cmd_backout(int argc, char *argv[])
5020 const struct got_error *error = NULL;
5021 struct got_worktree *worktree = NULL;
5022 struct got_repository *repo = NULL;
5023 char *cwd = NULL, *commit_id_str = NULL;
5024 struct got_object_id *commit_id = NULL;
5025 struct got_commit_object *commit = NULL;
5026 struct got_object_qid *pid;
5027 struct got_reference *head_ref = NULL;
5028 int ch, did_something = 0;
5030 while ((ch = getopt(argc, argv, "")) != -1) {
5031 switch (ch) {
5032 default:
5033 usage_backout();
5034 /* NOTREACHED */
5038 argc -= optind;
5039 argv += optind;
5041 #ifndef PROFILE
5042 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5043 "unveil", NULL) == -1)
5044 err(1, "pledge");
5045 #endif
5046 if (argc != 1)
5047 usage_backout();
5049 cwd = getcwd(NULL, 0);
5050 if (cwd == NULL) {
5051 error = got_error_from_errno("getcwd");
5052 goto done;
5054 error = got_worktree_open(&worktree, cwd);
5055 if (error)
5056 goto done;
5058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5059 NULL);
5060 if (error != NULL)
5061 goto done;
5063 error = apply_unveil(got_repo_get_path(repo), 0,
5064 got_worktree_get_root_path(worktree));
5065 if (error)
5066 goto done;
5068 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5069 GOT_OBJ_TYPE_COMMIT, repo);
5070 if (error != NULL) {
5071 struct got_reference *ref;
5072 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5073 goto done;
5074 error = got_ref_open(&ref, repo, argv[0], 0);
5075 if (error != NULL)
5076 goto done;
5077 error = got_ref_resolve(&commit_id, repo, ref);
5078 got_ref_close(ref);
5079 if (error != NULL)
5080 goto done;
5082 error = got_object_id_str(&commit_id_str, commit_id);
5083 if (error)
5084 goto done;
5086 error = got_ref_open(&head_ref, repo,
5087 got_worktree_get_head_ref_name(worktree), 0);
5088 if (error != NULL)
5089 goto done;
5091 error = check_same_branch(commit_id, head_ref, NULL, repo);
5092 if (error)
5093 goto done;
5095 error = got_object_open_as_commit(&commit, repo, commit_id);
5096 if (error)
5097 goto done;
5098 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5099 if (pid == NULL) {
5100 error = got_error(GOT_ERR_ROOT_COMMIT);
5101 goto done;
5104 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5105 update_progress, &did_something, check_cancelled, NULL);
5106 if (error != NULL)
5107 goto done;
5109 if (did_something)
5110 printf("Backed out commit %s\n", commit_id_str);
5111 done:
5112 if (commit)
5113 got_object_commit_close(commit);
5114 free(commit_id_str);
5115 if (head_ref)
5116 got_ref_close(head_ref);
5117 if (worktree)
5118 got_worktree_close(worktree);
5119 if (repo)
5120 got_repo_close(repo);
5121 return error;
5124 __dead static void
5125 usage_rebase(void)
5127 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5128 getprogname());
5129 exit(1);
5132 void
5133 trim_logmsg(char *logmsg, int limit)
5135 char *nl;
5136 size_t len;
5138 len = strlen(logmsg);
5139 if (len > limit)
5140 len = limit;
5141 logmsg[len] = '\0';
5142 nl = strchr(logmsg, '\n');
5143 if (nl)
5144 *nl = '\0';
5147 static const struct got_error *
5148 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5150 const struct got_error *err;
5151 char *logmsg0 = NULL;
5152 const char *s;
5154 err = got_object_commit_get_logmsg(&logmsg0, commit);
5155 if (err)
5156 return err;
5158 s = logmsg0;
5159 while (isspace((unsigned char)s[0]))
5160 s++;
5162 *logmsg = strdup(s);
5163 if (*logmsg == NULL) {
5164 err = got_error_from_errno("strdup");
5165 goto done;
5168 trim_logmsg(*logmsg, limit);
5169 done:
5170 free(logmsg0);
5171 return err;
5174 static const struct got_error *
5175 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5177 const struct got_error *err;
5178 struct got_commit_object *commit = NULL;
5179 char *id_str = NULL, *logmsg = NULL;
5181 err = got_object_open_as_commit(&commit, repo, id);
5182 if (err)
5183 return err;
5185 err = got_object_id_str(&id_str, id);
5186 if (err)
5187 goto done;
5189 id_str[12] = '\0';
5191 err = get_short_logmsg(&logmsg, 42, commit);
5192 if (err)
5193 goto done;
5195 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5196 done:
5197 free(id_str);
5198 got_object_commit_close(commit);
5199 free(logmsg);
5200 return err;
5203 static const struct got_error *
5204 show_rebase_progress(struct got_commit_object *commit,
5205 struct got_object_id *old_id, struct got_object_id *new_id)
5207 const struct got_error *err;
5208 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5210 err = got_object_id_str(&old_id_str, old_id);
5211 if (err)
5212 goto done;
5214 if (new_id) {
5215 err = got_object_id_str(&new_id_str, new_id);
5216 if (err)
5217 goto done;
5220 old_id_str[12] = '\0';
5221 if (new_id_str)
5222 new_id_str[12] = '\0';
5224 err = get_short_logmsg(&logmsg, 42, commit);
5225 if (err)
5226 goto done;
5228 printf("%s -> %s: %s\n", old_id_str,
5229 new_id_str ? new_id_str : "no-op change", logmsg);
5230 done:
5231 free(old_id_str);
5232 free(new_id_str);
5233 free(logmsg);
5234 return err;
5237 static const struct got_error *
5238 rebase_progress(void *arg, unsigned char status, const char *path)
5240 unsigned char *rebase_status = arg;
5242 while (path[0] == '/')
5243 path++;
5244 printf("%c %s\n", status, path);
5246 if (*rebase_status == GOT_STATUS_CONFLICT)
5247 return NULL;
5248 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5249 *rebase_status = status;
5250 return NULL;
5253 static const struct got_error *
5254 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5255 struct got_reference *branch, struct got_reference *new_base_branch,
5256 struct got_reference *tmp_branch, struct got_repository *repo)
5258 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5259 return got_worktree_rebase_complete(worktree, fileindex,
5260 new_base_branch, tmp_branch, branch, repo);
5263 static const struct got_error *
5264 rebase_commit(struct got_pathlist_head *merged_paths,
5265 struct got_worktree *worktree, struct got_fileindex *fileindex,
5266 struct got_reference *tmp_branch,
5267 struct got_object_id *commit_id, struct got_repository *repo)
5269 const struct got_error *error;
5270 struct got_commit_object *commit;
5271 struct got_object_id *new_commit_id;
5273 error = got_object_open_as_commit(&commit, repo, commit_id);
5274 if (error)
5275 return error;
5277 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5278 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5279 if (error) {
5280 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5281 goto done;
5282 error = show_rebase_progress(commit, commit_id, NULL);
5283 } else {
5284 error = show_rebase_progress(commit, commit_id, new_commit_id);
5285 free(new_commit_id);
5287 done:
5288 got_object_commit_close(commit);
5289 return error;
5292 struct check_path_prefix_arg {
5293 const char *path_prefix;
5294 size_t len;
5295 int errcode;
5298 static const struct got_error *
5299 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5300 struct got_blob_object *blob2, struct got_object_id *id1,
5301 struct got_object_id *id2, const char *path1, const char *path2,
5302 mode_t mode1, mode_t mode2, struct got_repository *repo)
5304 struct check_path_prefix_arg *a = arg;
5306 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5307 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5308 return got_error(a->errcode);
5310 return NULL;
5313 static const struct got_error *
5314 check_path_prefix(struct got_object_id *parent_id,
5315 struct got_object_id *commit_id, const char *path_prefix,
5316 int errcode, struct got_repository *repo)
5318 const struct got_error *err;
5319 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5320 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5321 struct check_path_prefix_arg cpp_arg;
5323 if (got_path_is_root_dir(path_prefix))
5324 return NULL;
5326 err = got_object_open_as_commit(&commit, repo, commit_id);
5327 if (err)
5328 goto done;
5330 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5331 if (err)
5332 goto done;
5334 err = got_object_open_as_tree(&tree1, repo,
5335 got_object_commit_get_tree_id(parent_commit));
5336 if (err)
5337 goto done;
5339 err = got_object_open_as_tree(&tree2, repo,
5340 got_object_commit_get_tree_id(commit));
5341 if (err)
5342 goto done;
5344 cpp_arg.path_prefix = path_prefix;
5345 while (cpp_arg.path_prefix[0] == '/')
5346 cpp_arg.path_prefix++;
5347 cpp_arg.len = strlen(cpp_arg.path_prefix);
5348 cpp_arg.errcode = errcode;
5349 err = got_diff_tree(tree1, tree2, "", "", repo,
5350 check_path_prefix_in_diff, &cpp_arg, 0);
5351 done:
5352 if (tree1)
5353 got_object_tree_close(tree1);
5354 if (tree2)
5355 got_object_tree_close(tree2);
5356 if (commit)
5357 got_object_commit_close(commit);
5358 if (parent_commit)
5359 got_object_commit_close(parent_commit);
5360 return err;
5363 static const struct got_error *
5364 collect_commits(struct got_object_id_queue *commits,
5365 struct got_object_id *initial_commit_id,
5366 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5367 const char *path_prefix, int path_prefix_errcode,
5368 struct got_repository *repo)
5370 const struct got_error *err = NULL;
5371 struct got_commit_graph *graph = NULL;
5372 struct got_object_id *parent_id = NULL;
5373 struct got_object_qid *qid;
5374 struct got_object_id *commit_id = initial_commit_id;
5376 err = got_commit_graph_open(&graph, "/", 1);
5377 if (err)
5378 return err;
5380 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5381 check_cancelled, NULL);
5382 if (err)
5383 goto done;
5384 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5385 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5386 check_cancelled, NULL);
5387 if (err) {
5388 if (err->code == GOT_ERR_ITER_COMPLETED) {
5389 err = got_error_msg(GOT_ERR_ANCESTRY,
5390 "ran out of commits to rebase before "
5391 "youngest common ancestor commit has "
5392 "been reached?!?");
5394 goto done;
5395 } else {
5396 err = check_path_prefix(parent_id, commit_id,
5397 path_prefix, path_prefix_errcode, repo);
5398 if (err)
5399 goto done;
5401 err = got_object_qid_alloc(&qid, commit_id);
5402 if (err)
5403 goto done;
5404 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5405 commit_id = parent_id;
5408 done:
5409 got_commit_graph_close(graph);
5410 return err;
5413 static const struct got_error *
5414 cmd_rebase(int argc, char *argv[])
5416 const struct got_error *error = NULL;
5417 struct got_worktree *worktree = NULL;
5418 struct got_repository *repo = NULL;
5419 struct got_fileindex *fileindex = NULL;
5420 char *cwd = NULL;
5421 struct got_reference *branch = NULL;
5422 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5423 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5424 struct got_object_id *resume_commit_id = NULL;
5425 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5426 struct got_commit_object *commit = NULL;
5427 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5428 int histedit_in_progress = 0;
5429 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5430 struct got_object_id_queue commits;
5431 struct got_pathlist_head merged_paths;
5432 const struct got_object_id_queue *parent_ids;
5433 struct got_object_qid *qid, *pid;
5435 SIMPLEQ_INIT(&commits);
5436 TAILQ_INIT(&merged_paths);
5438 while ((ch = getopt(argc, argv, "ac")) != -1) {
5439 switch (ch) {
5440 case 'a':
5441 abort_rebase = 1;
5442 break;
5443 case 'c':
5444 continue_rebase = 1;
5445 break;
5446 default:
5447 usage_rebase();
5448 /* NOTREACHED */
5452 argc -= optind;
5453 argv += optind;
5455 #ifndef PROFILE
5456 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5457 "unveil", NULL) == -1)
5458 err(1, "pledge");
5459 #endif
5460 if (abort_rebase && continue_rebase)
5461 usage_rebase();
5462 else if (abort_rebase || continue_rebase) {
5463 if (argc != 0)
5464 usage_rebase();
5465 } else if (argc != 1)
5466 usage_rebase();
5468 cwd = getcwd(NULL, 0);
5469 if (cwd == NULL) {
5470 error = got_error_from_errno("getcwd");
5471 goto done;
5473 error = got_worktree_open(&worktree, cwd);
5474 if (error)
5475 goto done;
5477 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5478 NULL);
5479 if (error != NULL)
5480 goto done;
5482 error = apply_unveil(got_repo_get_path(repo), 0,
5483 got_worktree_get_root_path(worktree));
5484 if (error)
5485 goto done;
5487 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5488 worktree);
5489 if (error)
5490 goto done;
5491 if (histedit_in_progress) {
5492 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5493 goto done;
5496 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5497 if (error)
5498 goto done;
5500 if (abort_rebase) {
5501 int did_something;
5502 if (!rebase_in_progress) {
5503 error = got_error(GOT_ERR_NOT_REBASING);
5504 goto done;
5506 error = got_worktree_rebase_continue(&resume_commit_id,
5507 &new_base_branch, &tmp_branch, &branch, &fileindex,
5508 worktree, repo);
5509 if (error)
5510 goto done;
5511 printf("Switching work tree to %s\n",
5512 got_ref_get_symref_target(new_base_branch));
5513 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5514 new_base_branch, update_progress, &did_something);
5515 if (error)
5516 goto done;
5517 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5518 goto done; /* nothing else to do */
5521 if (continue_rebase) {
5522 if (!rebase_in_progress) {
5523 error = got_error(GOT_ERR_NOT_REBASING);
5524 goto done;
5526 error = got_worktree_rebase_continue(&resume_commit_id,
5527 &new_base_branch, &tmp_branch, &branch, &fileindex,
5528 worktree, repo);
5529 if (error)
5530 goto done;
5532 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5533 resume_commit_id, repo);
5534 if (error)
5535 goto done;
5537 yca_id = got_object_id_dup(resume_commit_id);
5538 if (yca_id == NULL) {
5539 error = got_error_from_errno("got_object_id_dup");
5540 goto done;
5542 } else {
5543 error = got_ref_open(&branch, repo, argv[0], 0);
5544 if (error != NULL)
5545 goto done;
5548 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5549 if (error)
5550 goto done;
5552 if (!continue_rebase) {
5553 struct got_object_id *base_commit_id;
5555 base_commit_id = got_worktree_get_base_commit_id(worktree);
5556 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5557 base_commit_id, branch_head_commit_id, repo,
5558 check_cancelled, NULL);
5559 if (error)
5560 goto done;
5561 if (yca_id == NULL) {
5562 error = got_error_msg(GOT_ERR_ANCESTRY,
5563 "specified branch shares no common ancestry "
5564 "with work tree's branch");
5565 goto done;
5568 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5569 if (error) {
5570 if (error->code != GOT_ERR_ANCESTRY)
5571 goto done;
5572 error = NULL;
5573 } else {
5574 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5575 "specified branch resolves to a commit which "
5576 "is already contained in work tree's branch");
5577 goto done;
5579 error = got_worktree_rebase_prepare(&new_base_branch,
5580 &tmp_branch, &fileindex, worktree, branch, repo);
5581 if (error)
5582 goto done;
5585 commit_id = branch_head_commit_id;
5586 error = got_object_open_as_commit(&commit, repo, commit_id);
5587 if (error)
5588 goto done;
5590 parent_ids = got_object_commit_get_parent_ids(commit);
5591 pid = SIMPLEQ_FIRST(parent_ids);
5592 if (pid == NULL) {
5593 if (!continue_rebase) {
5594 int did_something;
5595 error = got_worktree_rebase_abort(worktree, fileindex,
5596 repo, new_base_branch, update_progress,
5597 &did_something);
5598 if (error)
5599 goto done;
5600 printf("Rebase of %s aborted\n",
5601 got_ref_get_name(branch));
5603 error = got_error(GOT_ERR_EMPTY_REBASE);
5604 goto done;
5606 error = collect_commits(&commits, commit_id, pid->id,
5607 yca_id, got_worktree_get_path_prefix(worktree),
5608 GOT_ERR_REBASE_PATH, repo);
5609 got_object_commit_close(commit);
5610 commit = NULL;
5611 if (error)
5612 goto done;
5614 if (SIMPLEQ_EMPTY(&commits)) {
5615 if (continue_rebase) {
5616 error = rebase_complete(worktree, fileindex,
5617 branch, new_base_branch, tmp_branch, repo);
5618 goto done;
5619 } else {
5620 /* Fast-forward the reference of the branch. */
5621 struct got_object_id *new_head_commit_id;
5622 char *id_str;
5623 error = got_ref_resolve(&new_head_commit_id, repo,
5624 new_base_branch);
5625 if (error)
5626 goto done;
5627 error = got_object_id_str(&id_str, new_head_commit_id);
5628 printf("Forwarding %s to commit %s\n",
5629 got_ref_get_name(branch), id_str);
5630 free(id_str);
5631 error = got_ref_change_ref(branch,
5632 new_head_commit_id);
5633 if (error)
5634 goto done;
5638 pid = NULL;
5639 SIMPLEQ_FOREACH(qid, &commits, entry) {
5640 commit_id = qid->id;
5641 parent_id = pid ? pid->id : yca_id;
5642 pid = qid;
5644 error = got_worktree_rebase_merge_files(&merged_paths,
5645 worktree, fileindex, parent_id, commit_id, repo,
5646 rebase_progress, &rebase_status, check_cancelled, NULL);
5647 if (error)
5648 goto done;
5650 if (rebase_status == GOT_STATUS_CONFLICT) {
5651 error = show_rebase_merge_conflict(qid->id, repo);
5652 if (error)
5653 goto done;
5654 got_worktree_rebase_pathlist_free(&merged_paths);
5655 break;
5658 error = rebase_commit(&merged_paths, worktree, fileindex,
5659 tmp_branch, commit_id, repo);
5660 got_worktree_rebase_pathlist_free(&merged_paths);
5661 if (error)
5662 goto done;
5665 if (rebase_status == GOT_STATUS_CONFLICT) {
5666 error = got_worktree_rebase_postpone(worktree, fileindex);
5667 if (error)
5668 goto done;
5669 error = got_error_msg(GOT_ERR_CONFLICTS,
5670 "conflicts must be resolved before rebasing can continue");
5671 } else
5672 error = rebase_complete(worktree, fileindex, branch,
5673 new_base_branch, tmp_branch, repo);
5674 done:
5675 got_object_id_queue_free(&commits);
5676 free(branch_head_commit_id);
5677 free(resume_commit_id);
5678 free(yca_id);
5679 if (commit)
5680 got_object_commit_close(commit);
5681 if (branch)
5682 got_ref_close(branch);
5683 if (new_base_branch)
5684 got_ref_close(new_base_branch);
5685 if (tmp_branch)
5686 got_ref_close(tmp_branch);
5687 if (worktree)
5688 got_worktree_close(worktree);
5689 if (repo)
5690 got_repo_close(repo);
5691 return error;
5694 __dead static void
5695 usage_histedit(void)
5697 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5698 getprogname());
5699 exit(1);
5702 #define GOT_HISTEDIT_PICK 'p'
5703 #define GOT_HISTEDIT_EDIT 'e'
5704 #define GOT_HISTEDIT_FOLD 'f'
5705 #define GOT_HISTEDIT_DROP 'd'
5706 #define GOT_HISTEDIT_MESG 'm'
5708 static struct got_histedit_cmd {
5709 unsigned char code;
5710 const char *name;
5711 const char *desc;
5712 } got_histedit_cmds[] = {
5713 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5714 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5715 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5716 "be used" },
5717 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5718 { GOT_HISTEDIT_MESG, "mesg",
5719 "single-line log message for commit above (open editor if empty)" },
5722 struct got_histedit_list_entry {
5723 TAILQ_ENTRY(got_histedit_list_entry) entry;
5724 struct got_object_id *commit_id;
5725 const struct got_histedit_cmd *cmd;
5726 char *logmsg;
5728 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5730 static const struct got_error *
5731 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5732 FILE *f, struct got_repository *repo)
5734 const struct got_error *err = NULL;
5735 char *logmsg = NULL, *id_str = NULL;
5736 struct got_commit_object *commit = NULL;
5737 int n;
5739 err = got_object_open_as_commit(&commit, repo, commit_id);
5740 if (err)
5741 goto done;
5743 err = get_short_logmsg(&logmsg, 34, commit);
5744 if (err)
5745 goto done;
5747 err = got_object_id_str(&id_str, commit_id);
5748 if (err)
5749 goto done;
5751 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5752 if (n < 0)
5753 err = got_ferror(f, GOT_ERR_IO);
5754 done:
5755 if (commit)
5756 got_object_commit_close(commit);
5757 free(id_str);
5758 free(logmsg);
5759 return err;
5762 static const struct got_error *
5763 histedit_write_commit_list(struct got_object_id_queue *commits,
5764 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5766 const struct got_error *err = NULL;
5767 struct got_object_qid *qid;
5769 if (SIMPLEQ_EMPTY(commits))
5770 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5772 SIMPLEQ_FOREACH(qid, commits, entry) {
5773 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5774 f, repo);
5775 if (err)
5776 break;
5777 if (edit_logmsg_only) {
5778 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5779 if (n < 0) {
5780 err = got_ferror(f, GOT_ERR_IO);
5781 break;
5786 return err;
5789 static const struct got_error *
5790 write_cmd_list(FILE *f, const char *branch_name,
5791 struct got_object_id_queue *commits)
5793 const struct got_error *err = NULL;
5794 int n, i;
5795 char *id_str;
5796 struct got_object_qid *qid;
5798 qid = SIMPLEQ_FIRST(commits);
5799 err = got_object_id_str(&id_str, qid->id);
5800 if (err)
5801 return err;
5803 n = fprintf(f,
5804 "# Editing the history of branch '%s' starting at\n"
5805 "# commit %s\n"
5806 "# Commits will be processed in order from top to "
5807 "bottom of this file.\n", branch_name, id_str);
5808 if (n < 0) {
5809 err = got_ferror(f, GOT_ERR_IO);
5810 goto done;
5813 n = fprintf(f, "# Available histedit commands:\n");
5814 if (n < 0) {
5815 err = got_ferror(f, GOT_ERR_IO);
5816 goto done;
5819 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5820 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5821 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5822 cmd->desc);
5823 if (n < 0) {
5824 err = got_ferror(f, GOT_ERR_IO);
5825 break;
5828 done:
5829 free(id_str);
5830 return err;
5833 static const struct got_error *
5834 histedit_syntax_error(int lineno)
5836 static char msg[42];
5837 int ret;
5839 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5840 lineno);
5841 if (ret == -1 || ret >= sizeof(msg))
5842 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5844 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5847 static const struct got_error *
5848 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5849 char *logmsg, struct got_repository *repo)
5851 const struct got_error *err;
5852 struct got_commit_object *folded_commit = NULL;
5853 char *id_str, *folded_logmsg = NULL;
5855 err = got_object_id_str(&id_str, hle->commit_id);
5856 if (err)
5857 return err;
5859 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5860 if (err)
5861 goto done;
5863 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5864 if (err)
5865 goto done;
5866 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5867 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5868 folded_logmsg) == -1) {
5869 err = got_error_from_errno("asprintf");
5871 done:
5872 if (folded_commit)
5873 got_object_commit_close(folded_commit);
5874 free(id_str);
5875 free(folded_logmsg);
5876 return err;
5879 static struct got_histedit_list_entry *
5880 get_folded_commits(struct got_histedit_list_entry *hle)
5882 struct got_histedit_list_entry *prev, *folded = NULL;
5884 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5885 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5886 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5887 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5888 folded = prev;
5889 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5892 return folded;
5895 static const struct got_error *
5896 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5897 struct got_repository *repo)
5899 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5900 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5901 const struct got_error *err = NULL;
5902 struct got_commit_object *commit = NULL;
5903 int fd;
5904 struct got_histedit_list_entry *folded = NULL;
5906 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5907 if (err)
5908 return err;
5910 folded = get_folded_commits(hle);
5911 if (folded) {
5912 while (folded != hle) {
5913 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5914 folded = TAILQ_NEXT(folded, entry);
5915 continue;
5917 err = append_folded_commit_msg(&new_msg, folded,
5918 logmsg, repo);
5919 if (err)
5920 goto done;
5921 free(logmsg);
5922 logmsg = new_msg;
5923 folded = TAILQ_NEXT(folded, entry);
5927 err = got_object_id_str(&id_str, hle->commit_id);
5928 if (err)
5929 goto done;
5930 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5931 if (err)
5932 goto done;
5933 if (asprintf(&new_msg,
5934 "%s\n# original log message of commit %s: %s",
5935 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5936 err = got_error_from_errno("asprintf");
5937 goto done;
5939 free(logmsg);
5940 logmsg = new_msg;
5942 err = got_object_id_str(&id_str, hle->commit_id);
5943 if (err)
5944 goto done;
5946 err = got_opentemp_named_fd(&logmsg_path, &fd,
5947 GOT_TMPDIR_STR "/got-logmsg");
5948 if (err)
5949 goto done;
5951 dprintf(fd, logmsg);
5952 close(fd);
5954 err = get_editor(&editor);
5955 if (err)
5956 goto done;
5958 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5959 if (err) {
5960 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5961 goto done;
5962 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5964 done:
5965 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5966 err = got_error_from_errno2("unlink", logmsg_path);
5967 free(logmsg_path);
5968 free(logmsg);
5969 free(orig_logmsg);
5970 free(editor);
5971 if (commit)
5972 got_object_commit_close(commit);
5973 return err;
5976 static const struct got_error *
5977 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5978 FILE *f, struct got_repository *repo)
5980 const struct got_error *err = NULL;
5981 char *line = NULL, *p, *end;
5982 size_t size;
5983 ssize_t len;
5984 int lineno = 0, i;
5985 const struct got_histedit_cmd *cmd;
5986 struct got_object_id *commit_id = NULL;
5987 struct got_histedit_list_entry *hle = NULL;
5989 for (;;) {
5990 len = getline(&line, &size, f);
5991 if (len == -1) {
5992 const struct got_error *getline_err;
5993 if (feof(f))
5994 break;
5995 getline_err = got_error_from_errno("getline");
5996 err = got_ferror(f, getline_err->code);
5997 break;
5999 lineno++;
6000 p = line;
6001 while (isspace((unsigned char)p[0]))
6002 p++;
6003 if (p[0] == '#' || p[0] == '\0') {
6004 free(line);
6005 line = NULL;
6006 continue;
6008 cmd = NULL;
6009 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6010 cmd = &got_histedit_cmds[i];
6011 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6012 isspace((unsigned char)p[strlen(cmd->name)])) {
6013 p += strlen(cmd->name);
6014 break;
6016 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6017 p++;
6018 break;
6021 if (i == nitems(got_histedit_cmds)) {
6022 err = histedit_syntax_error(lineno);
6023 break;
6025 while (isspace((unsigned char)p[0]))
6026 p++;
6027 if (cmd->code == GOT_HISTEDIT_MESG) {
6028 if (hle == NULL || hle->logmsg != NULL) {
6029 err = got_error(GOT_ERR_HISTEDIT_CMD);
6030 break;
6032 if (p[0] == '\0') {
6033 err = histedit_edit_logmsg(hle, repo);
6034 if (err)
6035 break;
6036 } else {
6037 hle->logmsg = strdup(p);
6038 if (hle->logmsg == NULL) {
6039 err = got_error_from_errno("strdup");
6040 break;
6043 free(line);
6044 line = NULL;
6045 continue;
6046 } else {
6047 end = p;
6048 while (end[0] && !isspace((unsigned char)end[0]))
6049 end++;
6050 *end = '\0';
6052 err = got_object_resolve_id_str(&commit_id, repo, p);
6053 if (err) {
6054 /* override error code */
6055 err = histedit_syntax_error(lineno);
6056 break;
6059 hle = malloc(sizeof(*hle));
6060 if (hle == NULL) {
6061 err = got_error_from_errno("malloc");
6062 break;
6064 hle->cmd = cmd;
6065 hle->commit_id = commit_id;
6066 hle->logmsg = NULL;
6067 commit_id = NULL;
6068 free(line);
6069 line = NULL;
6070 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6073 free(line);
6074 free(commit_id);
6075 return err;
6078 static const struct got_error *
6079 histedit_check_script(struct got_histedit_list *histedit_cmds,
6080 struct got_object_id_queue *commits, struct got_repository *repo)
6082 const struct got_error *err = NULL;
6083 struct got_object_qid *qid;
6084 struct got_histedit_list_entry *hle;
6085 static char msg[92];
6086 char *id_str;
6088 if (TAILQ_EMPTY(histedit_cmds))
6089 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6090 "histedit script contains no commands");
6091 if (SIMPLEQ_EMPTY(commits))
6092 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6094 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6095 struct got_histedit_list_entry *hle2;
6096 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6097 if (hle == hle2)
6098 continue;
6099 if (got_object_id_cmp(hle->commit_id,
6100 hle2->commit_id) != 0)
6101 continue;
6102 err = got_object_id_str(&id_str, hle->commit_id);
6103 if (err)
6104 return err;
6105 snprintf(msg, sizeof(msg), "commit %s is listed "
6106 "more than once in histedit script", id_str);
6107 free(id_str);
6108 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6112 SIMPLEQ_FOREACH(qid, commits, entry) {
6113 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6114 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6115 break;
6117 if (hle == NULL) {
6118 err = got_object_id_str(&id_str, qid->id);
6119 if (err)
6120 return err;
6121 snprintf(msg, sizeof(msg),
6122 "commit %s missing from histedit script", id_str);
6123 free(id_str);
6124 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6128 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6129 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6130 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6131 "last commit in histedit script cannot be folded");
6133 return NULL;
6136 static const struct got_error *
6137 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6138 const char *path, struct got_object_id_queue *commits,
6139 struct got_repository *repo)
6141 const struct got_error *err = NULL;
6142 char *editor;
6143 FILE *f = NULL;
6145 err = get_editor(&editor);
6146 if (err)
6147 return err;
6149 if (spawn_editor(editor, path) == -1) {
6150 err = got_error_from_errno("failed spawning editor");
6151 goto done;
6154 f = fopen(path, "r");
6155 if (f == NULL) {
6156 err = got_error_from_errno("fopen");
6157 goto done;
6159 err = histedit_parse_list(histedit_cmds, f, repo);
6160 if (err)
6161 goto done;
6163 err = histedit_check_script(histedit_cmds, commits, repo);
6164 done:
6165 if (f && fclose(f) != 0 && err == NULL)
6166 err = got_error_from_errno("fclose");
6167 free(editor);
6168 return err;
6171 static const struct got_error *
6172 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6173 struct got_object_id_queue *, const char *, const char *,
6174 struct got_repository *);
6176 static const struct got_error *
6177 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6178 struct got_object_id_queue *commits, const char *branch_name,
6179 int edit_logmsg_only, struct got_repository *repo)
6181 const struct got_error *err;
6182 FILE *f = NULL;
6183 char *path = NULL;
6185 err = got_opentemp_named(&path, &f, "got-histedit");
6186 if (err)
6187 return err;
6189 err = write_cmd_list(f, branch_name, commits);
6190 if (err)
6191 goto done;
6193 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6194 if (err)
6195 goto done;
6197 if (edit_logmsg_only) {
6198 rewind(f);
6199 err = histedit_parse_list(histedit_cmds, f, repo);
6200 } else {
6201 if (fclose(f) != 0) {
6202 err = got_error_from_errno("fclose");
6203 goto done;
6205 f = NULL;
6206 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6207 if (err) {
6208 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6209 err->code != GOT_ERR_HISTEDIT_CMD)
6210 goto done;
6211 err = histedit_edit_list_retry(histedit_cmds, err,
6212 commits, path, branch_name, repo);
6215 done:
6216 if (f && fclose(f) != 0 && err == NULL)
6217 err = got_error_from_errno("fclose");
6218 if (path && unlink(path) != 0 && err == NULL)
6219 err = got_error_from_errno2("unlink", path);
6220 free(path);
6221 return err;
6224 static const struct got_error *
6225 histedit_save_list(struct got_histedit_list *histedit_cmds,
6226 struct got_worktree *worktree, struct got_repository *repo)
6228 const struct got_error *err = NULL;
6229 char *path = NULL;
6230 FILE *f = NULL;
6231 struct got_histedit_list_entry *hle;
6232 struct got_commit_object *commit = NULL;
6234 err = got_worktree_get_histedit_script_path(&path, worktree);
6235 if (err)
6236 return err;
6238 f = fopen(path, "w");
6239 if (f == NULL) {
6240 err = got_error_from_errno2("fopen", path);
6241 goto done;
6243 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6244 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6245 repo);
6246 if (err)
6247 break;
6249 if (hle->logmsg) {
6250 int n = fprintf(f, "%c %s\n",
6251 GOT_HISTEDIT_MESG, hle->logmsg);
6252 if (n < 0) {
6253 err = got_ferror(f, GOT_ERR_IO);
6254 break;
6258 done:
6259 if (f && fclose(f) != 0 && err == NULL)
6260 err = got_error_from_errno("fclose");
6261 free(path);
6262 if (commit)
6263 got_object_commit_close(commit);
6264 return err;
6267 void
6268 histedit_free_list(struct got_histedit_list *histedit_cmds)
6270 struct got_histedit_list_entry *hle;
6272 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6273 TAILQ_REMOVE(histedit_cmds, hle, entry);
6274 free(hle);
6278 static const struct got_error *
6279 histedit_load_list(struct got_histedit_list *histedit_cmds,
6280 const char *path, struct got_repository *repo)
6282 const struct got_error *err = NULL;
6283 FILE *f = NULL;
6285 f = fopen(path, "r");
6286 if (f == NULL) {
6287 err = got_error_from_errno2("fopen", path);
6288 goto done;
6291 err = histedit_parse_list(histedit_cmds, f, repo);
6292 done:
6293 if (f && fclose(f) != 0 && err == NULL)
6294 err = got_error_from_errno("fclose");
6295 return err;
6298 static const struct got_error *
6299 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6300 const struct got_error *edit_err, struct got_object_id_queue *commits,
6301 const char *path, const char *branch_name, struct got_repository *repo)
6303 const struct got_error *err = NULL, *prev_err = edit_err;
6304 int resp = ' ';
6306 while (resp != 'c' && resp != 'r' && resp != 'a') {
6307 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6308 "or (a)bort: ", getprogname(), prev_err->msg);
6309 resp = getchar();
6310 if (resp == '\n')
6311 resp = getchar();
6312 if (resp == 'c') {
6313 histedit_free_list(histedit_cmds);
6314 err = histedit_run_editor(histedit_cmds, path, commits,
6315 repo);
6316 if (err) {
6317 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6318 err->code != GOT_ERR_HISTEDIT_CMD)
6319 break;
6320 prev_err = err;
6321 resp = ' ';
6322 continue;
6324 break;
6325 } else if (resp == 'r') {
6326 histedit_free_list(histedit_cmds);
6327 err = histedit_edit_script(histedit_cmds,
6328 commits, branch_name, 0, repo);
6329 if (err) {
6330 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6331 err->code != GOT_ERR_HISTEDIT_CMD)
6332 break;
6333 prev_err = err;
6334 resp = ' ';
6335 continue;
6337 break;
6338 } else if (resp == 'a') {
6339 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6340 break;
6341 } else
6342 printf("invalid response '%c'\n", resp);
6345 return err;
6348 static const struct got_error *
6349 histedit_complete(struct got_worktree *worktree,
6350 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6351 struct got_reference *branch, struct got_repository *repo)
6353 printf("Switching work tree to %s\n",
6354 got_ref_get_symref_target(branch));
6355 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6356 branch, repo);
6359 static const struct got_error *
6360 show_histedit_progress(struct got_commit_object *commit,
6361 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6363 const struct got_error *err;
6364 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6366 err = got_object_id_str(&old_id_str, hle->commit_id);
6367 if (err)
6368 goto done;
6370 if (new_id) {
6371 err = got_object_id_str(&new_id_str, new_id);
6372 if (err)
6373 goto done;
6376 old_id_str[12] = '\0';
6377 if (new_id_str)
6378 new_id_str[12] = '\0';
6380 if (hle->logmsg) {
6381 logmsg = strdup(hle->logmsg);
6382 if (logmsg == NULL) {
6383 err = got_error_from_errno("strdup");
6384 goto done;
6386 trim_logmsg(logmsg, 42);
6387 } else {
6388 err = get_short_logmsg(&logmsg, 42, commit);
6389 if (err)
6390 goto done;
6393 switch (hle->cmd->code) {
6394 case GOT_HISTEDIT_PICK:
6395 case GOT_HISTEDIT_EDIT:
6396 printf("%s -> %s: %s\n", old_id_str,
6397 new_id_str ? new_id_str : "no-op change", logmsg);
6398 break;
6399 case GOT_HISTEDIT_DROP:
6400 case GOT_HISTEDIT_FOLD:
6401 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6402 logmsg);
6403 break;
6404 default:
6405 break;
6407 done:
6408 free(old_id_str);
6409 free(new_id_str);
6410 return err;
6413 static const struct got_error *
6414 histedit_commit(struct got_pathlist_head *merged_paths,
6415 struct got_worktree *worktree, struct got_fileindex *fileindex,
6416 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6417 struct got_repository *repo)
6419 const struct got_error *err;
6420 struct got_commit_object *commit;
6421 struct got_object_id *new_commit_id;
6423 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6424 && hle->logmsg == NULL) {
6425 err = histedit_edit_logmsg(hle, repo);
6426 if (err)
6427 return err;
6430 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6431 if (err)
6432 return err;
6434 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6435 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6436 hle->logmsg, repo);
6437 if (err) {
6438 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6439 goto done;
6440 err = show_histedit_progress(commit, hle, NULL);
6441 } else {
6442 err = show_histedit_progress(commit, hle, new_commit_id);
6443 free(new_commit_id);
6445 done:
6446 got_object_commit_close(commit);
6447 return err;
6450 static const struct got_error *
6451 histedit_skip_commit(struct got_histedit_list_entry *hle,
6452 struct got_worktree *worktree, struct got_repository *repo)
6454 const struct got_error *error;
6455 struct got_commit_object *commit;
6457 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6458 repo);
6459 if (error)
6460 return error;
6462 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6463 if (error)
6464 return error;
6466 error = show_histedit_progress(commit, hle, NULL);
6467 got_object_commit_close(commit);
6468 return error;
6471 static const struct got_error *
6472 check_local_changes(void *arg, unsigned char status,
6473 unsigned char staged_status, const char *path,
6474 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6475 struct got_object_id *commit_id, int dirfd, const char *de_name)
6477 int *have_local_changes = arg;
6479 switch (status) {
6480 case GOT_STATUS_ADD:
6481 case GOT_STATUS_DELETE:
6482 case GOT_STATUS_MODIFY:
6483 case GOT_STATUS_CONFLICT:
6484 *have_local_changes = 1;
6485 return got_error(GOT_ERR_CANCELLED);
6486 default:
6487 break;
6490 switch (staged_status) {
6491 case GOT_STATUS_ADD:
6492 case GOT_STATUS_DELETE:
6493 case GOT_STATUS_MODIFY:
6494 *have_local_changes = 1;
6495 return got_error(GOT_ERR_CANCELLED);
6496 default:
6497 break;
6500 return NULL;
6503 static const struct got_error *
6504 cmd_histedit(int argc, char *argv[])
6506 const struct got_error *error = NULL;
6507 struct got_worktree *worktree = NULL;
6508 struct got_fileindex *fileindex = NULL;
6509 struct got_repository *repo = NULL;
6510 char *cwd = NULL;
6511 struct got_reference *branch = NULL;
6512 struct got_reference *tmp_branch = NULL;
6513 struct got_object_id *resume_commit_id = NULL;
6514 struct got_object_id *base_commit_id = NULL;
6515 struct got_object_id *head_commit_id = NULL;
6516 struct got_commit_object *commit = NULL;
6517 int ch, rebase_in_progress = 0, did_something;
6518 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6519 int edit_logmsg_only = 0;
6520 const char *edit_script_path = NULL;
6521 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6522 struct got_object_id_queue commits;
6523 struct got_pathlist_head merged_paths;
6524 const struct got_object_id_queue *parent_ids;
6525 struct got_object_qid *pid;
6526 struct got_histedit_list histedit_cmds;
6527 struct got_histedit_list_entry *hle;
6529 SIMPLEQ_INIT(&commits);
6530 TAILQ_INIT(&histedit_cmds);
6531 TAILQ_INIT(&merged_paths);
6533 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6534 switch (ch) {
6535 case 'a':
6536 abort_edit = 1;
6537 break;
6538 case 'c':
6539 continue_edit = 1;
6540 break;
6541 case 'F':
6542 edit_script_path = optarg;
6543 break;
6544 case 'm':
6545 edit_logmsg_only = 1;
6546 break;
6547 default:
6548 usage_histedit();
6549 /* NOTREACHED */
6553 argc -= optind;
6554 argv += optind;
6556 #ifndef PROFILE
6557 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6558 "unveil", NULL) == -1)
6559 err(1, "pledge");
6560 #endif
6561 if (abort_edit && continue_edit)
6562 errx(1, "histedit's -a and -c options are mutually exclusive");
6563 if (edit_script_path && edit_logmsg_only)
6564 errx(1, "histedit's -F and -m options are mutually exclusive");
6565 if (abort_edit && edit_logmsg_only)
6566 errx(1, "histedit's -a and -m options are mutually exclusive");
6567 if (continue_edit && edit_logmsg_only)
6568 errx(1, "histedit's -c and -m options are mutually exclusive");
6569 if (argc != 0)
6570 usage_histedit();
6573 * This command cannot apply unveil(2) in all cases because the
6574 * user may choose to run an editor to edit the histedit script
6575 * and to edit individual commit log messages.
6576 * unveil(2) traverses exec(2); if an editor is used we have to
6577 * apply unveil after edit script and log messages have been written.
6578 * XXX TODO: Make use of unveil(2) where possible.
6581 cwd = getcwd(NULL, 0);
6582 if (cwd == NULL) {
6583 error = got_error_from_errno("getcwd");
6584 goto done;
6586 error = got_worktree_open(&worktree, cwd);
6587 if (error)
6588 goto done;
6590 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6591 NULL);
6592 if (error != NULL)
6593 goto done;
6595 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6596 if (error)
6597 goto done;
6598 if (rebase_in_progress) {
6599 error = got_error(GOT_ERR_REBASING);
6600 goto done;
6603 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6604 if (error)
6605 goto done;
6607 if (edit_in_progress && edit_logmsg_only) {
6608 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6609 "histedit operation is in progress in this "
6610 "work tree and must be continued or aborted "
6611 "before the -m option can be used");
6612 goto done;
6615 if (edit_in_progress && abort_edit) {
6616 error = got_worktree_histedit_continue(&resume_commit_id,
6617 &tmp_branch, &branch, &base_commit_id, &fileindex,
6618 worktree, repo);
6619 if (error)
6620 goto done;
6621 printf("Switching work tree to %s\n",
6622 got_ref_get_symref_target(branch));
6623 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6624 branch, base_commit_id, update_progress, &did_something);
6625 if (error)
6626 goto done;
6627 printf("Histedit of %s aborted\n",
6628 got_ref_get_symref_target(branch));
6629 goto done; /* nothing else to do */
6630 } else if (abort_edit) {
6631 error = got_error(GOT_ERR_NOT_HISTEDIT);
6632 goto done;
6635 if (continue_edit) {
6636 char *path;
6638 if (!edit_in_progress) {
6639 error = got_error(GOT_ERR_NOT_HISTEDIT);
6640 goto done;
6643 error = got_worktree_get_histedit_script_path(&path, worktree);
6644 if (error)
6645 goto done;
6647 error = histedit_load_list(&histedit_cmds, path, repo);
6648 free(path);
6649 if (error)
6650 goto done;
6652 error = got_worktree_histedit_continue(&resume_commit_id,
6653 &tmp_branch, &branch, &base_commit_id, &fileindex,
6654 worktree, repo);
6655 if (error)
6656 goto done;
6658 error = got_ref_resolve(&head_commit_id, repo, branch);
6659 if (error)
6660 goto done;
6662 error = got_object_open_as_commit(&commit, repo,
6663 head_commit_id);
6664 if (error)
6665 goto done;
6666 parent_ids = got_object_commit_get_parent_ids(commit);
6667 pid = SIMPLEQ_FIRST(parent_ids);
6668 if (pid == NULL) {
6669 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6670 goto done;
6672 error = collect_commits(&commits, head_commit_id, pid->id,
6673 base_commit_id, got_worktree_get_path_prefix(worktree),
6674 GOT_ERR_HISTEDIT_PATH, repo);
6675 got_object_commit_close(commit);
6676 commit = NULL;
6677 if (error)
6678 goto done;
6679 } else {
6680 if (edit_in_progress) {
6681 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6682 goto done;
6685 error = got_ref_open(&branch, repo,
6686 got_worktree_get_head_ref_name(worktree), 0);
6687 if (error != NULL)
6688 goto done;
6690 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6691 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6692 "will not edit commit history of a branch outside "
6693 "the \"refs/heads/\" reference namespace");
6694 goto done;
6697 error = got_ref_resolve(&head_commit_id, repo, branch);
6698 got_ref_close(branch);
6699 branch = NULL;
6700 if (error)
6701 goto done;
6703 error = got_object_open_as_commit(&commit, repo,
6704 head_commit_id);
6705 if (error)
6706 goto done;
6707 parent_ids = got_object_commit_get_parent_ids(commit);
6708 pid = SIMPLEQ_FIRST(parent_ids);
6709 if (pid == NULL) {
6710 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6711 goto done;
6713 error = collect_commits(&commits, head_commit_id, pid->id,
6714 got_worktree_get_base_commit_id(worktree),
6715 got_worktree_get_path_prefix(worktree),
6716 GOT_ERR_HISTEDIT_PATH, repo);
6717 got_object_commit_close(commit);
6718 commit = NULL;
6719 if (error)
6720 goto done;
6722 if (SIMPLEQ_EMPTY(&commits)) {
6723 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6724 goto done;
6727 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6728 &base_commit_id, &fileindex, worktree, repo);
6729 if (error)
6730 goto done;
6732 if (edit_script_path) {
6733 error = histedit_load_list(&histedit_cmds,
6734 edit_script_path, repo);
6735 if (error) {
6736 got_worktree_histedit_abort(worktree, fileindex,
6737 repo, branch, base_commit_id,
6738 update_progress, &did_something);
6739 goto done;
6741 } else {
6742 const char *branch_name;
6743 branch_name = got_ref_get_symref_target(branch);
6744 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6745 branch_name += 11;
6746 error = histedit_edit_script(&histedit_cmds, &commits,
6747 branch_name, edit_logmsg_only, repo);
6748 if (error) {
6749 got_worktree_histedit_abort(worktree, fileindex,
6750 repo, branch, base_commit_id,
6751 update_progress, &did_something);
6752 goto done;
6757 error = histedit_save_list(&histedit_cmds, worktree,
6758 repo);
6759 if (error) {
6760 got_worktree_histedit_abort(worktree, fileindex,
6761 repo, branch, base_commit_id,
6762 update_progress, &did_something);
6763 goto done;
6768 error = histedit_check_script(&histedit_cmds, &commits, repo);
6769 if (error)
6770 goto done;
6772 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6773 if (resume_commit_id) {
6774 if (got_object_id_cmp(hle->commit_id,
6775 resume_commit_id) != 0)
6776 continue;
6778 resume_commit_id = NULL;
6779 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6780 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6781 error = histedit_skip_commit(hle, worktree,
6782 repo);
6783 if (error)
6784 goto done;
6785 } else {
6786 struct got_pathlist_head paths;
6787 int have_changes = 0;
6789 TAILQ_INIT(&paths);
6790 error = got_pathlist_append(&paths, "", NULL);
6791 if (error)
6792 goto done;
6793 error = got_worktree_status(worktree, &paths,
6794 repo, check_local_changes, &have_changes,
6795 check_cancelled, NULL);
6796 got_pathlist_free(&paths);
6797 if (error) {
6798 if (error->code != GOT_ERR_CANCELLED)
6799 goto done;
6800 if (sigint_received || sigpipe_received)
6801 goto done;
6803 if (have_changes) {
6804 error = histedit_commit(NULL, worktree,
6805 fileindex, tmp_branch, hle, repo);
6806 if (error)
6807 goto done;
6808 } else {
6809 error = got_object_open_as_commit(
6810 &commit, repo, hle->commit_id);
6811 if (error)
6812 goto done;
6813 error = show_histedit_progress(commit,
6814 hle, NULL);
6815 got_object_commit_close(commit);
6816 commit = NULL;
6817 if (error)
6818 goto done;
6821 continue;
6824 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6825 error = histedit_skip_commit(hle, worktree, repo);
6826 if (error)
6827 goto done;
6828 continue;
6831 error = got_object_open_as_commit(&commit, repo,
6832 hle->commit_id);
6833 if (error)
6834 goto done;
6835 parent_ids = got_object_commit_get_parent_ids(commit);
6836 pid = SIMPLEQ_FIRST(parent_ids);
6838 error = got_worktree_histedit_merge_files(&merged_paths,
6839 worktree, fileindex, pid->id, hle->commit_id, repo,
6840 rebase_progress, &rebase_status, check_cancelled, NULL);
6841 if (error)
6842 goto done;
6843 got_object_commit_close(commit);
6844 commit = NULL;
6846 if (rebase_status == GOT_STATUS_CONFLICT) {
6847 error = show_rebase_merge_conflict(hle->commit_id,
6848 repo);
6849 if (error)
6850 goto done;
6851 got_worktree_rebase_pathlist_free(&merged_paths);
6852 break;
6855 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6856 char *id_str;
6857 error = got_object_id_str(&id_str, hle->commit_id);
6858 if (error)
6859 goto done;
6860 printf("Stopping histedit for amending commit %s\n",
6861 id_str);
6862 free(id_str);
6863 got_worktree_rebase_pathlist_free(&merged_paths);
6864 error = got_worktree_histedit_postpone(worktree,
6865 fileindex);
6866 goto done;
6869 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6870 error = histedit_skip_commit(hle, worktree, repo);
6871 if (error)
6872 goto done;
6873 continue;
6876 error = histedit_commit(&merged_paths, worktree, fileindex,
6877 tmp_branch, hle, repo);
6878 got_worktree_rebase_pathlist_free(&merged_paths);
6879 if (error)
6880 goto done;
6883 if (rebase_status == GOT_STATUS_CONFLICT) {
6884 error = got_worktree_histedit_postpone(worktree, fileindex);
6885 if (error)
6886 goto done;
6887 error = got_error_msg(GOT_ERR_CONFLICTS,
6888 "conflicts must be resolved before histedit can continue");
6889 } else
6890 error = histedit_complete(worktree, fileindex, tmp_branch,
6891 branch, repo);
6892 done:
6893 got_object_id_queue_free(&commits);
6894 histedit_free_list(&histedit_cmds);
6895 free(head_commit_id);
6896 free(base_commit_id);
6897 free(resume_commit_id);
6898 if (commit)
6899 got_object_commit_close(commit);
6900 if (branch)
6901 got_ref_close(branch);
6902 if (tmp_branch)
6903 got_ref_close(tmp_branch);
6904 if (worktree)
6905 got_worktree_close(worktree);
6906 if (repo)
6907 got_repo_close(repo);
6908 return error;
6911 __dead static void
6912 usage_integrate(void)
6914 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6915 exit(1);
6918 static const struct got_error *
6919 cmd_integrate(int argc, char *argv[])
6921 const struct got_error *error = NULL;
6922 struct got_repository *repo = NULL;
6923 struct got_worktree *worktree = NULL;
6924 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6925 const char *branch_arg = NULL;
6926 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6927 struct got_fileindex *fileindex = NULL;
6928 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6929 int ch, did_something = 0;
6931 while ((ch = getopt(argc, argv, "")) != -1) {
6932 switch (ch) {
6933 default:
6934 usage_integrate();
6935 /* NOTREACHED */
6939 argc -= optind;
6940 argv += optind;
6942 if (argc != 1)
6943 usage_integrate();
6944 branch_arg = argv[0];
6946 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6947 "unveil", NULL) == -1)
6948 err(1, "pledge");
6950 cwd = getcwd(NULL, 0);
6951 if (cwd == NULL) {
6952 error = got_error_from_errno("getcwd");
6953 goto done;
6956 error = got_worktree_open(&worktree, cwd);
6957 if (error)
6958 goto done;
6960 error = check_rebase_or_histedit_in_progress(worktree);
6961 if (error)
6962 goto done;
6964 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6965 NULL);
6966 if (error != NULL)
6967 goto done;
6969 error = apply_unveil(got_repo_get_path(repo), 0,
6970 got_worktree_get_root_path(worktree));
6971 if (error)
6972 goto done;
6974 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6975 error = got_error_from_errno("asprintf");
6976 goto done;
6979 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6980 &base_branch_ref, worktree, refname, repo);
6981 if (error)
6982 goto done;
6984 refname = strdup(got_ref_get_name(branch_ref));
6985 if (refname == NULL) {
6986 error = got_error_from_errno("strdup");
6987 got_worktree_integrate_abort(worktree, fileindex, repo,
6988 branch_ref, base_branch_ref);
6989 goto done;
6991 base_refname = strdup(got_ref_get_name(base_branch_ref));
6992 if (base_refname == NULL) {
6993 error = got_error_from_errno("strdup");
6994 got_worktree_integrate_abort(worktree, fileindex, repo,
6995 branch_ref, base_branch_ref);
6996 goto done;
6999 error = got_ref_resolve(&commit_id, repo, branch_ref);
7000 if (error)
7001 goto done;
7003 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7004 if (error)
7005 goto done;
7007 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7008 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7009 "specified branch has already been integrated");
7010 got_worktree_integrate_abort(worktree, fileindex, repo,
7011 branch_ref, base_branch_ref);
7012 goto done;
7015 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7016 if (error) {
7017 if (error->code == GOT_ERR_ANCESTRY)
7018 error = got_error(GOT_ERR_REBASE_REQUIRED);
7019 got_worktree_integrate_abort(worktree, fileindex, repo,
7020 branch_ref, base_branch_ref);
7021 goto done;
7024 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7025 branch_ref, base_branch_ref, update_progress, &did_something,
7026 check_cancelled, NULL);
7027 if (error)
7028 goto done;
7030 printf("Integrated %s into %s\n", refname, base_refname);
7031 done:
7032 if (repo)
7033 got_repo_close(repo);
7034 if (worktree)
7035 got_worktree_close(worktree);
7036 free(cwd);
7037 free(base_commit_id);
7038 free(commit_id);
7039 free(refname);
7040 free(base_refname);
7041 return error;
7044 __dead static void
7045 usage_stage(void)
7047 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7048 "[file-path ...]\n",
7049 getprogname());
7050 exit(1);
7053 static const struct got_error *
7054 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7055 const char *path, struct got_object_id *blob_id,
7056 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7057 int dirfd, const char *de_name)
7059 const struct got_error *err = NULL;
7060 char *id_str = NULL;
7062 if (staged_status != GOT_STATUS_ADD &&
7063 staged_status != GOT_STATUS_MODIFY &&
7064 staged_status != GOT_STATUS_DELETE)
7065 return NULL;
7067 if (staged_status == GOT_STATUS_ADD ||
7068 staged_status == GOT_STATUS_MODIFY)
7069 err = got_object_id_str(&id_str, staged_blob_id);
7070 else
7071 err = got_object_id_str(&id_str, blob_id);
7072 if (err)
7073 return err;
7075 printf("%s %c %s\n", id_str, staged_status, path);
7076 free(id_str);
7077 return NULL;
7080 static const struct got_error *
7081 cmd_stage(int argc, char *argv[])
7083 const struct got_error *error = NULL;
7084 struct got_repository *repo = NULL;
7085 struct got_worktree *worktree = NULL;
7086 char *cwd = NULL;
7087 struct got_pathlist_head paths;
7088 struct got_pathlist_entry *pe;
7089 int ch, list_stage = 0, pflag = 0;
7090 FILE *patch_script_file = NULL;
7091 const char *patch_script_path = NULL;
7092 struct choose_patch_arg cpa;
7094 TAILQ_INIT(&paths);
7096 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7097 switch (ch) {
7098 case 'l':
7099 list_stage = 1;
7100 break;
7101 case 'p':
7102 pflag = 1;
7103 break;
7104 case 'F':
7105 patch_script_path = optarg;
7106 break;
7107 default:
7108 usage_stage();
7109 /* NOTREACHED */
7113 argc -= optind;
7114 argv += optind;
7116 #ifndef PROFILE
7117 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7118 "unveil", NULL) == -1)
7119 err(1, "pledge");
7120 #endif
7121 if (list_stage && (pflag || patch_script_path))
7122 errx(1, "-l option cannot be used with other options");
7123 if (patch_script_path && !pflag)
7124 errx(1, "-F option can only be used together with -p option");
7126 cwd = getcwd(NULL, 0);
7127 if (cwd == NULL) {
7128 error = got_error_from_errno("getcwd");
7129 goto done;
7132 error = got_worktree_open(&worktree, cwd);
7133 if (error)
7134 goto done;
7136 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7137 NULL);
7138 if (error != NULL)
7139 goto done;
7141 if (patch_script_path) {
7142 patch_script_file = fopen(patch_script_path, "r");
7143 if (patch_script_file == NULL) {
7144 error = got_error_from_errno2("fopen",
7145 patch_script_path);
7146 goto done;
7149 error = apply_unveil(got_repo_get_path(repo), 0,
7150 got_worktree_get_root_path(worktree));
7151 if (error)
7152 goto done;
7154 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7155 if (error)
7156 goto done;
7158 if (list_stage)
7159 error = got_worktree_status(worktree, &paths, repo,
7160 print_stage, NULL, check_cancelled, NULL);
7161 else {
7162 cpa.patch_script_file = patch_script_file;
7163 cpa.action = "stage";
7164 error = got_worktree_stage(worktree, &paths,
7165 pflag ? NULL : print_status, NULL,
7166 pflag ? choose_patch : NULL, &cpa, repo);
7168 done:
7169 if (patch_script_file && fclose(patch_script_file) == EOF &&
7170 error == NULL)
7171 error = got_error_from_errno2("fclose", patch_script_path);
7172 if (repo)
7173 got_repo_close(repo);
7174 if (worktree)
7175 got_worktree_close(worktree);
7176 TAILQ_FOREACH(pe, &paths, entry)
7177 free((char *)pe->path);
7178 got_pathlist_free(&paths);
7179 free(cwd);
7180 return error;
7183 __dead static void
7184 usage_unstage(void)
7186 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7187 "[file-path ...]\n",
7188 getprogname());
7189 exit(1);
7193 static const struct got_error *
7194 cmd_unstage(int argc, char *argv[])
7196 const struct got_error *error = NULL;
7197 struct got_repository *repo = NULL;
7198 struct got_worktree *worktree = NULL;
7199 char *cwd = NULL;
7200 struct got_pathlist_head paths;
7201 struct got_pathlist_entry *pe;
7202 int ch, did_something = 0, pflag = 0;
7203 FILE *patch_script_file = NULL;
7204 const char *patch_script_path = NULL;
7205 struct choose_patch_arg cpa;
7207 TAILQ_INIT(&paths);
7209 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7210 switch (ch) {
7211 case 'p':
7212 pflag = 1;
7213 break;
7214 case 'F':
7215 patch_script_path = optarg;
7216 break;
7217 default:
7218 usage_unstage();
7219 /* NOTREACHED */
7223 argc -= optind;
7224 argv += optind;
7226 #ifndef PROFILE
7227 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7228 "unveil", NULL) == -1)
7229 err(1, "pledge");
7230 #endif
7231 if (patch_script_path && !pflag)
7232 errx(1, "-F option can only be used together with -p option");
7234 cwd = getcwd(NULL, 0);
7235 if (cwd == NULL) {
7236 error = got_error_from_errno("getcwd");
7237 goto done;
7240 error = got_worktree_open(&worktree, cwd);
7241 if (error)
7242 goto done;
7244 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7245 NULL);
7246 if (error != NULL)
7247 goto done;
7249 if (patch_script_path) {
7250 patch_script_file = fopen(patch_script_path, "r");
7251 if (patch_script_file == NULL) {
7252 error = got_error_from_errno2("fopen",
7253 patch_script_path);
7254 goto done;
7258 error = apply_unveil(got_repo_get_path(repo), 0,
7259 got_worktree_get_root_path(worktree));
7260 if (error)
7261 goto done;
7263 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7264 if (error)
7265 goto done;
7267 cpa.patch_script_file = patch_script_file;
7268 cpa.action = "unstage";
7269 error = got_worktree_unstage(worktree, &paths, update_progress,
7270 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7271 done:
7272 if (patch_script_file && fclose(patch_script_file) == EOF &&
7273 error == NULL)
7274 error = got_error_from_errno2("fclose", patch_script_path);
7275 if (repo)
7276 got_repo_close(repo);
7277 if (worktree)
7278 got_worktree_close(worktree);
7279 TAILQ_FOREACH(pe, &paths, entry)
7280 free((char *)pe->path);
7281 got_pathlist_free(&paths);
7282 free(cwd);
7283 return error;
7286 __dead static void
7287 usage_cat(void)
7289 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7290 "arg1 [arg2 ...]\n", getprogname());
7291 exit(1);
7294 static const struct got_error *
7295 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7297 const struct got_error *err;
7298 struct got_blob_object *blob;
7300 err = got_object_open_as_blob(&blob, repo, id, 8192);
7301 if (err)
7302 return err;
7304 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7305 got_object_blob_close(blob);
7306 return err;
7309 static const struct got_error *
7310 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7312 const struct got_error *err;
7313 struct got_tree_object *tree;
7314 int nentries, i;
7316 err = got_object_open_as_tree(&tree, repo, id);
7317 if (err)
7318 return err;
7320 nentries = got_object_tree_get_nentries(tree);
7321 for (i = 0; i < nentries; i++) {
7322 struct got_tree_entry *te;
7323 char *id_str;
7324 if (sigint_received || sigpipe_received)
7325 break;
7326 te = got_object_tree_get_entry(tree, i);
7327 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7328 if (err)
7329 break;
7330 fprintf(outfile, "%s %.7o %s\n", id_str,
7331 got_tree_entry_get_mode(te),
7332 got_tree_entry_get_name(te));
7333 free(id_str);
7336 got_object_tree_close(tree);
7337 return err;
7340 static const struct got_error *
7341 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7343 const struct got_error *err;
7344 struct got_commit_object *commit;
7345 const struct got_object_id_queue *parent_ids;
7346 struct got_object_qid *pid;
7347 char *id_str = NULL;
7348 const char *logmsg = NULL;
7350 err = got_object_open_as_commit(&commit, repo, id);
7351 if (err)
7352 return err;
7354 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7355 if (err)
7356 goto done;
7358 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7359 parent_ids = got_object_commit_get_parent_ids(commit);
7360 fprintf(outfile, "numparents %d\n",
7361 got_object_commit_get_nparents(commit));
7362 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7363 char *pid_str;
7364 err = got_object_id_str(&pid_str, pid->id);
7365 if (err)
7366 goto done;
7367 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7368 free(pid_str);
7370 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7371 got_object_commit_get_author(commit),
7372 got_object_commit_get_author_time(commit));
7374 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7375 got_object_commit_get_author(commit),
7376 got_object_commit_get_committer_time(commit));
7378 logmsg = got_object_commit_get_logmsg_raw(commit);
7379 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7380 fprintf(outfile, "%s", logmsg);
7381 done:
7382 free(id_str);
7383 got_object_commit_close(commit);
7384 return err;
7387 static const struct got_error *
7388 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7390 const struct got_error *err;
7391 struct got_tag_object *tag;
7392 char *id_str = NULL;
7393 const char *tagmsg = NULL;
7395 err = got_object_open_as_tag(&tag, repo, id);
7396 if (err)
7397 return err;
7399 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7400 if (err)
7401 goto done;
7403 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7405 switch (got_object_tag_get_object_type(tag)) {
7406 case GOT_OBJ_TYPE_BLOB:
7407 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7408 GOT_OBJ_LABEL_BLOB);
7409 break;
7410 case GOT_OBJ_TYPE_TREE:
7411 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7412 GOT_OBJ_LABEL_TREE);
7413 break;
7414 case GOT_OBJ_TYPE_COMMIT:
7415 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7416 GOT_OBJ_LABEL_COMMIT);
7417 break;
7418 case GOT_OBJ_TYPE_TAG:
7419 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7420 GOT_OBJ_LABEL_TAG);
7421 break;
7422 default:
7423 break;
7426 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7427 got_object_tag_get_name(tag));
7429 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7430 got_object_tag_get_tagger(tag),
7431 got_object_tag_get_tagger_time(tag));
7433 tagmsg = got_object_tag_get_message(tag);
7434 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7435 fprintf(outfile, "%s", tagmsg);
7436 done:
7437 free(id_str);
7438 got_object_tag_close(tag);
7439 return err;
7442 static const struct got_error *
7443 cmd_cat(int argc, char *argv[])
7445 const struct got_error *error;
7446 struct got_repository *repo = NULL;
7447 struct got_worktree *worktree = NULL;
7448 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7449 const char *commit_id_str = NULL;
7450 struct got_object_id *id = NULL, *commit_id = NULL;
7451 int ch, obj_type, i, force_path = 0;
7453 #ifndef PROFILE
7454 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7455 NULL) == -1)
7456 err(1, "pledge");
7457 #endif
7459 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7460 switch (ch) {
7461 case 'c':
7462 commit_id_str = optarg;
7463 break;
7464 case 'r':
7465 repo_path = realpath(optarg, NULL);
7466 if (repo_path == NULL)
7467 return got_error_from_errno2("realpath",
7468 optarg);
7469 got_path_strip_trailing_slashes(repo_path);
7470 break;
7471 case 'P':
7472 force_path = 1;
7473 break;
7474 default:
7475 usage_cat();
7476 /* NOTREACHED */
7480 argc -= optind;
7481 argv += optind;
7483 cwd = getcwd(NULL, 0);
7484 if (cwd == NULL) {
7485 error = got_error_from_errno("getcwd");
7486 goto done;
7488 error = got_worktree_open(&worktree, cwd);
7489 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7490 goto done;
7491 if (worktree) {
7492 if (repo_path == NULL) {
7493 repo_path = strdup(
7494 got_worktree_get_repo_path(worktree));
7495 if (repo_path == NULL) {
7496 error = got_error_from_errno("strdup");
7497 goto done;
7502 if (repo_path == NULL) {
7503 repo_path = getcwd(NULL, 0);
7504 if (repo_path == NULL)
7505 return got_error_from_errno("getcwd");
7508 error = got_repo_open(&repo, repo_path, NULL);
7509 free(repo_path);
7510 if (error != NULL)
7511 goto done;
7513 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7514 if (error)
7515 goto done;
7517 if (commit_id_str == NULL)
7518 commit_id_str = GOT_REF_HEAD;
7519 error = got_repo_match_object_id(&commit_id, NULL,
7520 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7521 if (error)
7522 goto done;
7524 for (i = 0; i < argc; i++) {
7525 if (force_path) {
7526 error = got_object_id_by_path(&id, repo, commit_id,
7527 argv[i]);
7528 if (error)
7529 break;
7530 } else {
7531 error = got_repo_match_object_id(&id, &label, argv[i],
7532 GOT_OBJ_TYPE_ANY, 0, repo);
7533 if (error) {
7534 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7535 error->code != GOT_ERR_NOT_REF)
7536 break;
7537 error = got_object_id_by_path(&id, repo,
7538 commit_id, argv[i]);
7539 if (error)
7540 break;
7544 error = got_object_get_type(&obj_type, repo, id);
7545 if (error)
7546 break;
7548 switch (obj_type) {
7549 case GOT_OBJ_TYPE_BLOB:
7550 error = cat_blob(id, repo, stdout);
7551 break;
7552 case GOT_OBJ_TYPE_TREE:
7553 error = cat_tree(id, repo, stdout);
7554 break;
7555 case GOT_OBJ_TYPE_COMMIT:
7556 error = cat_commit(id, repo, stdout);
7557 break;
7558 case GOT_OBJ_TYPE_TAG:
7559 error = cat_tag(id, repo, stdout);
7560 break;
7561 default:
7562 error = got_error(GOT_ERR_OBJ_TYPE);
7563 break;
7565 if (error)
7566 break;
7567 free(label);
7568 label = NULL;
7569 free(id);
7570 id = NULL;
7572 done:
7573 free(label);
7574 free(id);
7575 free(commit_id);
7576 if (worktree)
7577 got_worktree_close(worktree);
7578 if (repo) {
7579 const struct got_error *repo_error;
7580 repo_error = got_repo_close(repo);
7581 if (error == NULL)
7582 error = repo_error;
7584 return error;