2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 * Copyright (C) 2023 Josh Rickmar <jrick@zettaport.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "got_compat.h"
23 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_worktree_cvg.h"
54 #include "got_commit_graph.h"
55 #include "got_fetch.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_opentemp.h"
60 #include "got_gotconfig.h"
62 #include "got_patch.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #ifndef GOT_DEFAULT_EDITOR
71 #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
74 static volatile sig_atomic_t sigint_received
;
75 static volatile sig_atomic_t sigpipe_received
;
78 catch_sigint(int signo
)
84 catch_sigpipe(int signo
)
92 const struct got_error
*(*cmd_main
)(int, char *[]);
93 void (*cmd_usage
)(void);
94 const char *cmd_alias
;
97 __dead
static void usage(int, int);
98 __dead
static void usage_import(void);
99 __dead
static void usage_clone(void);
100 __dead
static void usage_checkout(void);
101 __dead
static void usage_update(void);
102 __dead
static void usage_log(void);
103 __dead
static void usage_diff(void);
104 __dead
static void usage_blame(void);
105 __dead
static void usage_tree(void);
106 __dead
static void usage_status(void);
107 __dead
static void usage_tag(void);
108 __dead
static void usage_add(void);
109 __dead
static void usage_remove(void);
110 __dead
static void usage_patch(void);
111 __dead
static void usage_revert(void);
112 __dead
static void usage_commit(void);
113 __dead
static void usage_cherrypick(void);
114 __dead
static void usage_backout(void);
115 __dead
static void usage_cat(void);
116 __dead
static void usage_info(void);
118 static const struct got_error
* cmd_import(int, char *[]);
119 static const struct got_error
* cmd_clone(int, char *[]);
120 static const struct got_error
* cmd_checkout(int, char *[]);
121 static const struct got_error
* cmd_update(int, char *[]);
122 static const struct got_error
* cmd_log(int, char *[]);
123 static const struct got_error
* cmd_diff(int, char *[]);
124 static const struct got_error
* cmd_blame(int, char *[]);
125 static const struct got_error
* cmd_tree(int, char *[]);
126 static const struct got_error
* cmd_status(int, char *[]);
127 static const struct got_error
* cmd_tag(int, char *[]);
128 static const struct got_error
* cmd_add(int, char *[]);
129 static const struct got_error
* cmd_remove(int, char *[]);
130 static const struct got_error
* cmd_patch(int, char *[]);
131 static const struct got_error
* cmd_revert(int, char *[]);
132 static const struct got_error
* cmd_commit(int, char *[]);
133 static const struct got_error
* cmd_cherrypick(int, char *[]);
134 static const struct got_error
* cmd_backout(int, char *[]);
135 static const struct got_error
* cmd_cat(int, char *[]);
136 static const struct got_error
* cmd_info(int, char *[]);
138 static const struct got_cmd got_commands
[] = {
139 { "import", cmd_import
, usage_import
, "im" },
140 { "clone", cmd_clone
, usage_clone
, "cl" },
141 { "checkout", cmd_checkout
, usage_checkout
, "co" },
142 { "update", cmd_update
, usage_update
, "up" },
143 { "log", cmd_log
, usage_log
, "" },
144 { "diff", cmd_diff
, usage_diff
, "di" },
145 { "blame", cmd_blame
, usage_blame
, "bl" },
146 { "tree", cmd_tree
, usage_tree
, "tr" },
147 { "status", cmd_status
, usage_status
, "st" },
148 { "tag", cmd_tag
, usage_tag
, "" },
149 { "add", cmd_add
, usage_add
, "" },
150 { "remove", cmd_remove
, usage_remove
, "rm" },
151 { "patch", cmd_patch
, usage_patch
, "pa" },
152 { "revert", cmd_revert
, usage_revert
, "rv" },
153 { "commit", cmd_commit
, usage_commit
, "ci" },
154 { "cherrypick", cmd_cherrypick
, usage_cherrypick
, "cy" },
155 { "backout", cmd_backout
, usage_backout
, "bo" },
156 { "cat", cmd_cat
, usage_cat
, "" },
157 { "info", cmd_info
, usage_info
, "" },
161 list_commands(FILE *fp
)
165 fprintf(fp
, "commands:");
166 for (i
= 0; i
< nitems(got_commands
); i
++) {
167 const struct got_cmd
*cmd
= &got_commands
[i
];
168 fprintf(fp
, " %s", cmd
->cmd_name
);
174 option_conflict(char a
, char b
)
176 errx(1, "-%c and -%c options are mutually exclusive", a
, b
);
180 main(int argc
, char *argv
[])
182 const struct got_cmd
*cmd
;
185 int hflag
= 0, Vflag
= 0;
186 static const struct option longopts
[] = {
187 { "version", no_argument
, NULL
, 'V' },
191 setlocale(LC_CTYPE
, "");
193 while ((ch
= getopt_long(argc
, argv
, "+hV", longopts
, NULL
)) != -1) {
213 got_version_print_str();
218 usage(hflag
, hflag
? 0 : 1);
220 signal(SIGINT
, catch_sigint
);
221 signal(SIGPIPE
, catch_sigpipe
);
223 for (i
= 0; i
< nitems(got_commands
); i
++) {
224 const struct got_error
*error
;
226 cmd
= &got_commands
[i
];
228 if (strcmp(cmd
->cmd_name
, argv
[0]) != 0 &&
229 strcmp(cmd
->cmd_alias
, argv
[0]) != 0)
235 error
= cmd
->cmd_main(argc
, argv
);
236 if (error
&& error
->code
!= GOT_ERR_CANCELLED
&&
237 error
->code
!= GOT_ERR_PRIVSEP_EXIT
&&
238 !(sigpipe_received
&&
239 error
->code
== GOT_ERR_ERRNO
&& errno
== EPIPE
) &&
241 error
->code
== GOT_ERR_ERRNO
&& errno
== EINTR
)) {
243 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
250 fprintf(stderr
, "%s: unknown command '%s'\n", getprogname(), argv
[0]);
251 list_commands(stderr
);
256 usage(int hflag
, int status
)
258 FILE *fp
= (status
== 0) ? stdout
: stderr
;
260 fprintf(fp
, "usage: %s [-hV] command [arg ...]\n",
267 static const struct got_error
*
268 get_editor(char **abspath
)
270 const struct got_error
*err
= NULL
;
275 editor
= getenv("VISUAL");
277 editor
= getenv("EDITOR");
280 err
= got_path_find_prog(abspath
, editor
);
285 if (*abspath
== NULL
) {
286 *abspath
= strdup(GOT_DEFAULT_EDITOR
);
287 if (*abspath
== NULL
)
288 return got_error_from_errno("strdup");
294 static const struct got_error
*
295 apply_unveil(const char *repo_path
, int repo_read_only
,
296 const char *worktree_path
)
298 const struct got_error
*err
;
301 if (unveil("gmon.out", "rwc") != 0)
302 return got_error_from_errno2("unveil", "gmon.out");
304 if (repo_path
&& unveil(repo_path
, repo_read_only
? "r" : "rwc") != 0)
305 return got_error_from_errno2("unveil", repo_path
);
307 if (worktree_path
&& unveil(worktree_path
, "rwc") != 0)
308 return got_error_from_errno2("unveil", worktree_path
);
310 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR
);
313 err
= got_privsep_unveil_exec_helpers();
317 if (unveil(NULL
, NULL
) != 0)
318 return got_error_from_errno("unveil");
326 fprintf(stderr
, "usage: %s import [-b branch] [-I pattern] [-m message] "
327 "[-r repository-path] directory\n", getprogname());
332 spawn_editor(const char *editor
, const char *file
)
335 sig_t sighup
, sigint
, sigquit
;
338 sighup
= signal(SIGHUP
, SIG_IGN
);
339 sigint
= signal(SIGINT
, SIG_IGN
);
340 sigquit
= signal(SIGQUIT
, SIG_IGN
);
342 switch (pid
= fork()) {
346 execl(editor
, editor
, file
, (char *)NULL
);
350 while (waitpid(pid
, &st
, 0) == -1)
355 (void)signal(SIGHUP
, sighup
);
356 (void)signal(SIGINT
, sigint
);
357 (void)signal(SIGQUIT
, sigquit
);
359 if (!WIFEXITED(st
)) {
364 return WEXITSTATUS(st
);
367 static const struct got_error
*
368 read_logmsg(char **logmsg
, size_t *len
, FILE *fp
, size_t filesize
)
370 const struct got_error
*err
= NULL
;
377 if (fseeko(fp
, 0L, SEEK_SET
) == -1)
378 return got_error_from_errno("fseeko");
380 *logmsg
= malloc(filesize
+ 1);
382 return got_error_from_errno("malloc");
385 while (getline(&line
, &linesize
, fp
) != -1) {
386 if (line
[0] == '#' || (*len
== 0 && line
[0] == '\n'))
387 continue; /* remove comments and leading empty lines */
388 *len
= strlcat(*logmsg
, line
, filesize
+ 1);
389 if (*len
>= filesize
+ 1) {
390 err
= got_error(GOT_ERR_NO_SPACE
);
395 err
= got_ferror(fp
, GOT_ERR_IO
);
399 while (*len
> 0 && (*logmsg
)[*len
- 1] == '\n') {
400 (*logmsg
)[*len
- 1] = '\0';
413 static const struct got_error
*
414 edit_logmsg(char **logmsg
, const char *editor
, const char *logmsg_path
,
415 const char *initial_content
, size_t initial_content_len
,
416 int require_modification
)
418 const struct got_error
*err
= NULL
;
425 if (stat(logmsg_path
, &st
) == -1)
426 return got_error_from_errno2("stat", logmsg_path
);
428 if (spawn_editor(editor
, logmsg_path
) == -1)
429 return got_error_from_errno("failed spawning editor");
431 if (require_modification
) {
432 struct timespec timeout
;
436 nanosleep(&timeout
, NULL
);
439 if (stat(logmsg_path
, &st2
) == -1)
440 return got_error_from_errno2("stat", logmsg_path
);
442 if (require_modification
&& st
.st_size
== st2
.st_size
&&
443 timespeccmp(&st
.st_mtim
, &st2
.st_mtim
, ==))
444 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
445 "no changes made to commit message, aborting");
447 fp
= fopen(logmsg_path
, "re");
449 err
= got_error_from_errno("fopen");
453 /* strip comments and leading/trailing newlines */
454 err
= read_logmsg(logmsg
, &logmsg_len
, fp
, st2
.st_size
);
457 if (logmsg_len
== 0) {
458 err
= got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
459 "commit message cannot be empty, aborting");
463 if (fp
&& fclose(fp
) == EOF
&& err
== NULL
)
464 err
= got_error_from_errno("fclose");
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 initial_content_len
;
481 initial_content_len
= asprintf(&initial_content
,
482 "\n# %s to be imported to branch %s\n", path_dir
,
484 if (initial_content_len
== -1)
485 return got_error_from_errno("asprintf");
487 err
= got_opentemp_named_fd(logmsg_path
, &fd
,
488 GOT_TMPDIR_STR
"/got-importmsg", "");
492 if (write(fd
, initial_content
, initial_content_len
) == -1) {
493 err
= got_error_from_errno2("write", *logmsg_path
);
496 if (close(fd
) == -1) {
497 err
= got_error_from_errno2("close", *logmsg_path
);
502 err
= edit_logmsg(logmsg
, editor
, *logmsg_path
, initial_content
,
503 initial_content_len
, 1);
505 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
506 err
= got_error_from_errno2("close", *logmsg_path
);
507 free(initial_content
);
515 static const struct got_error
*
516 import_progress(void *arg
, const char *path
)
518 printf("A %s\n", path
);
522 static const struct got_error
*
523 valid_author(const char *author
)
525 const char *email
= author
;
528 * Git' expects the author (or committer) to be in the form
529 * "name <email>", which are mostly free form (see the
530 * "committer" description in git-fast-import(1)). We're only
531 * doing this to avoid git's object parser breaking on commits
535 while (*author
&& *author
!= '\n' && *author
!= '<' && *author
!= '>')
537 if (author
!= email
&& *author
== '<' && *(author
- 1) != ' ')
538 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR
, "%s: space "
539 "between author name and email required", email
);
540 if (*author
++ != '<')
541 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL
, "%s", email
);
542 while (*author
&& *author
!= '\n' && *author
!= '<' && *author
!= '>')
544 if (strcmp(author
, ">") != 0)
545 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL
, "%s", email
);
549 static const struct got_error
*
550 get_author(char **author
, struct got_repository
*repo
,
551 struct got_worktree
*worktree
)
553 const struct got_error
*err
= NULL
;
554 const char *got_author
= NULL
, *name
, *email
;
555 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
560 worktree_conf
= got_worktree_get_gotconfig(worktree
);
561 repo_conf
= got_repo_get_gotconfig(repo
);
564 * Priority of potential author information sources, from most
565 * significant to least significant:
566 * 1) work tree's .got/got.conf file
567 * 2) repository's got.conf file
568 * 3) repository's git config file
569 * 4) environment variables
570 * 5) global git config files (in user's home directory or /etc)
574 got_author
= got_gotconfig_get_author(worktree_conf
);
575 if (got_author
== NULL
)
576 got_author
= got_gotconfig_get_author(repo_conf
);
577 if (got_author
== NULL
) {
578 name
= got_repo_get_gitconfig_author_name(repo
);
579 email
= got_repo_get_gitconfig_author_email(repo
);
581 if (asprintf(author
, "%s <%s>", name
, email
) == -1)
582 return got_error_from_errno("asprintf");
586 got_author
= getenv("GOT_AUTHOR");
587 if (got_author
== NULL
) {
588 name
= got_repo_get_global_gitconfig_author_name(repo
);
589 email
= got_repo_get_global_gitconfig_author_email(
592 if (asprintf(author
, "%s <%s>", name
, email
)
594 return got_error_from_errno("asprintf");
597 /* TODO: Look up user in password database? */
598 return got_error(GOT_ERR_COMMIT_NO_AUTHOR
);
602 *author
= strdup(got_author
);
604 return got_error_from_errno("strdup");
606 err
= valid_author(*author
);
614 static const struct got_error
*
615 get_allowed_signers(char **allowed_signers
, struct got_repository
*repo
,
616 struct got_worktree
*worktree
)
618 const char *got_allowed_signers
= NULL
;
619 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
621 *allowed_signers
= NULL
;
624 worktree_conf
= got_worktree_get_gotconfig(worktree
);
625 repo_conf
= got_repo_get_gotconfig(repo
);
628 * Priority of potential author information sources, from most
629 * significant to least significant:
630 * 1) work tree's .got/got.conf file
631 * 2) repository's got.conf file
635 got_allowed_signers
= got_gotconfig_get_allowed_signers_file(
637 if (got_allowed_signers
== NULL
)
638 got_allowed_signers
= got_gotconfig_get_allowed_signers_file(
641 if (got_allowed_signers
) {
642 *allowed_signers
= strdup(got_allowed_signers
);
643 if (*allowed_signers
== NULL
)
644 return got_error_from_errno("strdup");
649 static const struct got_error
*
650 get_revoked_signers(char **revoked_signers
, struct got_repository
*repo
,
651 struct got_worktree
*worktree
)
653 const char *got_revoked_signers
= NULL
;
654 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
656 *revoked_signers
= NULL
;
659 worktree_conf
= got_worktree_get_gotconfig(worktree
);
660 repo_conf
= got_repo_get_gotconfig(repo
);
663 * Priority of potential author information sources, from most
664 * significant to least significant:
665 * 1) work tree's .got/got.conf file
666 * 2) repository's got.conf file
670 got_revoked_signers
= got_gotconfig_get_revoked_signers_file(
672 if (got_revoked_signers
== NULL
)
673 got_revoked_signers
= got_gotconfig_get_revoked_signers_file(
676 if (got_revoked_signers
) {
677 *revoked_signers
= strdup(got_revoked_signers
);
678 if (*revoked_signers
== NULL
)
679 return got_error_from_errno("strdup");
685 get_signer_id(struct got_repository
*repo
, struct got_worktree
*worktree
)
687 const char *got_signer_id
= NULL
;
688 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
691 worktree_conf
= got_worktree_get_gotconfig(worktree
);
692 repo_conf
= got_repo_get_gotconfig(repo
);
695 * Priority of potential author information sources, from most
696 * significant to least significant:
697 * 1) work tree's .got/got.conf file
698 * 2) repository's got.conf file
702 got_signer_id
= got_gotconfig_get_signer_id(worktree_conf
);
703 if (got_signer_id
== NULL
)
704 got_signer_id
= got_gotconfig_get_signer_id(repo_conf
);
706 return got_signer_id
;
709 static const struct got_error
*
710 get_gitconfig_path(char **gitconfig_path
)
712 const char *homedir
= getenv("HOME");
714 *gitconfig_path
= NULL
;
716 if (asprintf(gitconfig_path
, "%s/.gitconfig", homedir
) == -1)
717 return got_error_from_errno("asprintf");
723 static const struct got_error
*
724 cmd_import(int argc
, char *argv
[])
726 const struct got_error
*error
= NULL
;
727 char *path_dir
= NULL
, *repo_path
= NULL
, *logmsg
= NULL
;
728 char *gitconfig_path
= NULL
, *editor
= NULL
, *author
= NULL
;
729 const char *branch_name
= NULL
;
730 char *id_str
= NULL
, *logmsg_path
= NULL
;
731 char refname
[PATH_MAX
] = "refs/heads/";
732 struct got_repository
*repo
= NULL
;
733 struct got_reference
*branch_ref
= NULL
, *head_ref
= NULL
;
734 struct got_object_id
*new_commit_id
= NULL
;
736 struct got_pathlist_head ignores
;
737 struct got_pathlist_entry
*pe
;
738 int preserve_logmsg
= 0;
739 int *pack_fds
= NULL
;
741 TAILQ_INIT(&ignores
);
744 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
750 while ((ch
= getopt(argc
, argv
, "b:I:m:r:")) != -1) {
753 branch_name
= optarg
;
756 if (optarg
[0] == '\0')
758 error
= got_pathlist_insert(&pe
, &ignores
, optarg
,
764 logmsg
= strdup(optarg
);
765 if (logmsg
== NULL
) {
766 error
= got_error_from_errno("strdup");
771 repo_path
= realpath(optarg
, NULL
);
772 if (repo_path
== NULL
) {
773 error
= got_error_from_errno2("realpath",
790 if (repo_path
== NULL
) {
791 repo_path
= getcwd(NULL
, 0);
792 if (repo_path
== NULL
)
793 return got_error_from_errno("getcwd");
795 got_path_strip_trailing_slashes(repo_path
);
796 error
= get_gitconfig_path(&gitconfig_path
);
799 error
= got_repo_pack_fds_open(&pack_fds
);
802 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
, pack_fds
);
806 error
= get_author(&author
, repo
, NULL
);
811 * Don't let the user create a branch name with a leading '-'.
812 * While technically a valid reference name, this case is usually
813 * an unintended typo.
815 if (branch_name
&& branch_name
[0] == '-')
816 return got_error_path(branch_name
, GOT_ERR_REF_NAME_MINUS
);
818 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
819 if (error
&& error
->code
!= GOT_ERR_NOT_REF
)
823 n
= strlcat(refname
, branch_name
, sizeof(refname
));
824 else if (head_ref
&& got_ref_is_symbolic(head_ref
))
825 n
= strlcpy(refname
, got_ref_get_symref_target(head_ref
),
828 n
= strlcat(refname
, "main", sizeof(refname
));
829 if (n
>= sizeof(refname
)) {
830 error
= got_error(GOT_ERR_NO_SPACE
);
834 error
= got_ref_open(&branch_ref
, repo
, refname
, 0);
836 if (error
->code
!= GOT_ERR_NOT_REF
)
839 error
= got_error_msg(GOT_ERR_BRANCH_EXISTS
,
840 "import target branch already exists");
844 path_dir
= realpath(argv
[0], NULL
);
845 if (path_dir
== NULL
) {
846 error
= got_error_from_errno2("realpath", argv
[0]);
849 got_path_strip_trailing_slashes(path_dir
);
852 * unveil(2) traverses exec(2); if an editor is used we have
853 * to apply unveil after the log message has been written.
855 if (logmsg
== NULL
|| *logmsg
== '\0') {
856 error
= get_editor(&editor
);
860 error
= collect_import_msg(&logmsg
, &logmsg_path
, editor
,
863 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
870 if (unveil(path_dir
, "r") != 0) {
871 error
= got_error_from_errno2("unveil", path_dir
);
877 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
884 error
= got_repo_import(&new_commit_id
, path_dir
, logmsg
,
885 author
, &ignores
, repo
, import_progress
, NULL
);
892 error
= got_ref_alloc(&branch_ref
, refname
, new_commit_id
);
899 error
= got_ref_write(branch_ref
, repo
);
906 error
= got_object_id_str(&id_str
, new_commit_id
);
913 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
915 if (error
->code
!= GOT_ERR_NOT_REF
) {
921 error
= got_ref_alloc_symref(&head_ref
, GOT_REF_HEAD
,
929 error
= got_ref_write(head_ref
, repo
);
937 printf("Created branch %s with commit %s\n",
938 got_ref_get_name(branch_ref
), id_str
);
941 const struct got_error
*pack_err
=
942 got_repo_pack_fds_close(pack_fds
);
947 const struct got_error
*close_err
= got_repo_close(repo
);
951 if (preserve_logmsg
) {
952 fprintf(stderr
, "%s: log message preserved in %s\n",
953 getprogname(), logmsg_path
);
954 } else if (logmsg_path
&& unlink(logmsg_path
) == -1 && error
== NULL
)
955 error
= got_error_from_errno2("unlink", logmsg_path
);
963 free(gitconfig_path
);
965 got_ref_close(branch_ref
);
967 got_ref_close(head_ref
);
974 fprintf(stderr
, "usage: %s clone [-almqv] [-b branch] [-R reference] "
975 "repository-URL [directory]\n", getprogname());
979 struct got_fetch_progress_arg
{
980 char last_scaled_size
[FMT_SCALED_STRSIZE
];
985 struct got_repository
*repo
;
990 struct got_pathlist_head
*symrefs
;
991 struct got_pathlist_head
*wanted_branches
;
992 struct got_pathlist_head
*wanted_refs
;
996 const char *remote_repo_path
;
998 int fetch_all_branches
;
999 int mirror_references
;
1003 /* XXX forward declaration */
1004 static const struct got_error
*
1005 create_config_files(const char *proto
, const char *host
, const char *port
,
1006 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
1007 int mirror_references
, struct got_pathlist_head
*symrefs
,
1008 struct got_pathlist_head
*wanted_branches
,
1009 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
);
1011 static const struct got_error
*
1012 fetch_progress(void *arg
, const char *message
, off_t packfile_size
,
1013 int nobj_total
, int nobj_indexed
, int nobj_loose
, int nobj_resolved
)
1015 const struct got_error
*err
= NULL
;
1016 struct got_fetch_progress_arg
*a
= arg
;
1017 char scaled_size
[FMT_SCALED_STRSIZE
];
1018 int p_indexed
, p_resolved
;
1019 int print_size
= 0, print_indexed
= 0, print_resolved
= 0;
1022 * In order to allow a failed clone to be resumed with 'got fetch'
1023 * we try to create configuration files as soon as possible.
1024 * Once the server has sent information about its default branch
1025 * we have all required information.
1027 if (a
->create_configs
&& !a
->configs_created
&&
1028 !TAILQ_EMPTY(a
->config_info
.symrefs
)) {
1029 err
= create_config_files(a
->config_info
.proto
,
1030 a
->config_info
.host
, a
->config_info
.port
,
1031 a
->config_info
.remote_repo_path
,
1032 a
->config_info
.git_url
,
1033 a
->config_info
.fetch_all_branches
,
1034 a
->config_info
.mirror_references
,
1035 a
->config_info
.symrefs
,
1036 a
->config_info
.wanted_branches
,
1037 a
->config_info
.wanted_refs
, a
->repo
);
1040 a
->configs_created
= 1;
1043 if (a
->verbosity
< 0)
1046 if (message
&& message
[0] != '\0') {
1047 printf("\rserver: %s", message
);
1052 if (packfile_size
> 0 || nobj_indexed
> 0) {
1053 if (fmt_scaled(packfile_size
, scaled_size
) == 0 &&
1054 (a
->last_scaled_size
[0] == '\0' ||
1055 strcmp(scaled_size
, a
->last_scaled_size
)) != 0) {
1057 if (strlcpy(a
->last_scaled_size
, scaled_size
,
1058 FMT_SCALED_STRSIZE
) >= FMT_SCALED_STRSIZE
)
1059 return got_error(GOT_ERR_NO_SPACE
);
1061 if (nobj_indexed
> 0) {
1062 p_indexed
= (nobj_indexed
* 100) / nobj_total
;
1063 if (p_indexed
!= a
->last_p_indexed
) {
1064 a
->last_p_indexed
= p_indexed
;
1069 if (nobj_resolved
> 0) {
1070 p_resolved
= (nobj_resolved
* 100) /
1071 (nobj_total
- nobj_loose
);
1072 if (p_resolved
!= a
->last_p_resolved
) {
1073 a
->last_p_resolved
= p_resolved
;
1081 if (print_size
|| print_indexed
|| print_resolved
)
1084 printf("%*s fetched", FMT_SCALED_STRSIZE
- 2, scaled_size
);
1086 printf("; indexing %d%%", p_indexed
);
1088 printf("; resolving deltas %d%%", p_resolved
);
1089 if (print_size
|| print_indexed
|| print_resolved
)
1095 static const struct got_error
*
1096 create_symref(const char *refname
, struct got_reference
*target_ref
,
1097 int verbosity
, struct got_repository
*repo
)
1099 const struct got_error
*err
;
1100 struct got_reference
*head_symref
;
1102 err
= got_ref_alloc_symref(&head_symref
, refname
, target_ref
);
1106 err
= got_ref_write(head_symref
, repo
);
1107 if (err
== NULL
&& verbosity
> 0) {
1108 printf("Created reference %s: %s\n", GOT_REF_HEAD
,
1109 got_ref_get_name(target_ref
));
1111 got_ref_close(head_symref
);
1115 static const struct got_error
*
1116 list_remote_refs(struct got_pathlist_head
*symrefs
,
1117 struct got_pathlist_head
*refs
)
1119 const struct got_error
*err
;
1120 struct got_pathlist_entry
*pe
;
1122 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1123 const char *refname
= pe
->path
;
1124 const char *targetref
= pe
->data
;
1126 printf("%s: %s\n", refname
, targetref
);
1129 TAILQ_FOREACH(pe
, refs
, entry
) {
1130 const char *refname
= pe
->path
;
1131 struct got_object_id
*id
= pe
->data
;
1134 err
= got_object_id_str(&id_str
, id
);
1137 printf("%s: %s\n", refname
, id_str
);
1144 static const struct got_error
*
1145 create_ref(const char *refname
, struct got_object_id
*id
,
1146 int verbosity
, struct got_repository
*repo
)
1148 const struct got_error
*err
= NULL
;
1149 struct got_reference
*ref
;
1152 err
= got_object_id_str(&id_str
, id
);
1156 err
= got_ref_alloc(&ref
, refname
, id
);
1160 err
= got_ref_write(ref
, repo
);
1163 if (err
== NULL
&& verbosity
>= 0)
1164 printf("Created reference %s: %s\n", refname
, id_str
);
1171 match_wanted_ref(const char *refname
, const char *wanted_ref
)
1173 if (strncmp(refname
, "refs/", 5) != 0)
1178 * Prevent fetching of references that won't make any
1179 * sense outside of the remote repository's context.
1181 if (strncmp(refname
, "got/", 4) == 0)
1183 if (strncmp(refname
, "remotes/", 8) == 0)
1186 if (strncmp(wanted_ref
, "refs/", 5) == 0)
1189 /* Allow prefix match. */
1190 if (got_path_is_child(refname
, wanted_ref
, strlen(wanted_ref
)))
1193 /* Allow exact match. */
1194 return (strcmp(refname
, wanted_ref
) == 0);
1198 is_wanted_ref(struct got_pathlist_head
*wanted_refs
, const char *refname
)
1200 struct got_pathlist_entry
*pe
;
1202 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1203 if (match_wanted_ref(refname
, pe
->path
))
1210 static const struct got_error
*
1211 create_wanted_ref(const char *refname
, struct got_object_id
*id
,
1212 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
1214 const struct got_error
*err
;
1215 char *remote_refname
;
1217 if (strncmp("refs/", refname
, 5) == 0)
1220 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
1221 remote_repo_name
, refname
) == -1)
1222 return got_error_from_errno("asprintf");
1224 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
1225 free(remote_refname
);
1229 static const struct got_error
*
1230 create_gotconfig(const char *proto
, const char *host
, const char *port
,
1231 const char *remote_repo_path
, const char *default_branch
,
1232 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1233 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1234 struct got_repository
*repo
)
1236 const struct got_error
*err
= NULL
;
1237 char *gotconfig_path
= NULL
;
1238 char *gotconfig
= NULL
;
1239 FILE *gotconfig_file
= NULL
;
1240 const char *branchname
= NULL
;
1241 char *branches
= NULL
, *refs
= NULL
;
1244 if (!fetch_all_branches
&& !TAILQ_EMPTY(wanted_branches
)) {
1245 struct got_pathlist_entry
*pe
;
1246 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1248 branchname
= pe
->path
;
1249 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1251 if (asprintf(&s
, "%s\"%s\" ",
1252 branches
? branches
: "", branchname
) == -1) {
1253 err
= got_error_from_errno("asprintf");
1259 } else if (!fetch_all_branches
&& default_branch
) {
1260 branchname
= default_branch
;
1261 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1263 if (asprintf(&branches
, "\"%s\" ", branchname
) == -1) {
1264 err
= got_error_from_errno("asprintf");
1268 if (!TAILQ_EMPTY(wanted_refs
)) {
1269 struct got_pathlist_entry
*pe
;
1270 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1272 const char *refname
= pe
->path
;
1273 if (strncmp(refname
, "refs/", 5) == 0)
1275 if (asprintf(&s
, "%s\"%s\" ",
1276 refs
? refs
: "", refname
) == -1) {
1277 err
= got_error_from_errno("asprintf");
1285 /* Create got.conf(5). */
1286 gotconfig_path
= got_repo_get_path_gotconfig(repo
);
1287 if (gotconfig_path
== NULL
) {
1288 err
= got_error_from_errno("got_repo_get_path_gotconfig");
1291 gotconfig_file
= fopen(gotconfig_path
, "ae");
1292 if (gotconfig_file
== NULL
) {
1293 err
= got_error_from_errno2("fopen", gotconfig_path
);
1296 if (asprintf(&gotconfig
,
1301 "\trepository \"%s\"\n"
1307 GOT_FETCH_DEFAULT_REMOTE_NAME
, host
, proto
,
1308 port
? "\tport " : "", port
? port
: "", port
? "\n" : "",
1309 remote_repo_path
, branches
? "\tbranch { " : "",
1310 branches
? branches
: "", branches
? "}\n" : "",
1311 refs
? "\treference { " : "", refs
? refs
: "", refs
? "}\n" : "",
1312 mirror_references
? "\tmirror_references yes\n" : "",
1313 fetch_all_branches
? "\tfetch_all_branches yes\n" : "") == -1) {
1314 err
= got_error_from_errno("asprintf");
1317 n
= fwrite(gotconfig
, 1, strlen(gotconfig
), gotconfig_file
);
1318 if (n
!= strlen(gotconfig
)) {
1319 err
= got_ferror(gotconfig_file
, GOT_ERR_IO
);
1324 if (gotconfig_file
&& fclose(gotconfig_file
) == EOF
&& err
== NULL
)
1325 err
= got_error_from_errno2("fclose", gotconfig_path
);
1326 free(gotconfig_path
);
1331 static const struct got_error
*
1332 create_gitconfig(const char *git_url
, const char *default_branch
,
1333 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1334 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1335 struct got_repository
*repo
)
1337 const struct got_error
*err
= NULL
;
1338 char *gitconfig_path
= NULL
;
1339 char *gitconfig
= NULL
;
1340 FILE *gitconfig_file
= NULL
;
1341 char *branches
= NULL
, *refs
= NULL
;
1342 const char *branchname
;
1345 /* Create a config file Git can understand. */
1346 gitconfig_path
= got_repo_get_path_gitconfig(repo
);
1347 if (gitconfig_path
== NULL
) {
1348 err
= got_error_from_errno("got_repo_get_path_gitconfig");
1351 gitconfig_file
= fopen(gitconfig_path
, "ae");
1352 if (gitconfig_file
== NULL
) {
1353 err
= got_error_from_errno2("fopen", gitconfig_path
);
1356 if (fetch_all_branches
) {
1357 if (mirror_references
) {
1358 if (asprintf(&branches
,
1359 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1360 err
= got_error_from_errno("asprintf");
1363 } else if (asprintf(&branches
,
1364 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1365 GOT_FETCH_DEFAULT_REMOTE_NAME
) == -1) {
1366 err
= got_error_from_errno("asprintf");
1369 } else if (!TAILQ_EMPTY(wanted_branches
)) {
1370 struct got_pathlist_entry
*pe
;
1371 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1373 branchname
= pe
->path
;
1374 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1376 if (mirror_references
) {
1378 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1379 branches
? branches
: "",
1380 branchname
, branchname
) == -1) {
1381 err
= got_error_from_errno("asprintf");
1384 } else if (asprintf(&s
,
1385 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1386 branches
? branches
: "",
1387 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1388 branchname
) == -1) {
1389 err
= got_error_from_errno("asprintf");
1397 * If the server specified a default branch, use just that one.
1398 * Otherwise fall back to fetching all branches on next fetch.
1400 if (default_branch
) {
1401 branchname
= default_branch
;
1402 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1405 branchname
= "*"; /* fall back to all branches */
1406 if (mirror_references
) {
1407 if (asprintf(&branches
,
1408 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1409 branchname
, branchname
) == -1) {
1410 err
= got_error_from_errno("asprintf");
1413 } else if (asprintf(&branches
,
1414 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1415 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1416 branchname
) == -1) {
1417 err
= got_error_from_errno("asprintf");
1421 if (!TAILQ_EMPTY(wanted_refs
)) {
1422 struct got_pathlist_entry
*pe
;
1423 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1425 const char *refname
= pe
->path
;
1426 if (strncmp(refname
, "refs/", 5) == 0)
1428 if (mirror_references
) {
1430 "%s\tfetch = refs/%s:refs/%s\n",
1431 refs
? refs
: "", refname
, refname
) == -1) {
1432 err
= got_error_from_errno("asprintf");
1435 } else if (asprintf(&s
,
1436 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1438 refname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1440 err
= got_error_from_errno("asprintf");
1448 if (asprintf(&gitconfig
,
1453 "\tfetch = refs/tags/*:refs/tags/*\n",
1454 GOT_FETCH_DEFAULT_REMOTE_NAME
, git_url
, branches
? branches
: "",
1455 refs
? refs
: "") == -1) {
1456 err
= got_error_from_errno("asprintf");
1459 n
= fwrite(gitconfig
, 1, strlen(gitconfig
), gitconfig_file
);
1460 if (n
!= strlen(gitconfig
)) {
1461 err
= got_ferror(gitconfig_file
, GOT_ERR_IO
);
1465 if (gitconfig_file
&& fclose(gitconfig_file
) == EOF
&& err
== NULL
)
1466 err
= got_error_from_errno2("fclose", gitconfig_path
);
1467 free(gitconfig_path
);
1472 static const struct got_error
*
1473 create_config_files(const char *proto
, const char *host
, const char *port
,
1474 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
1475 int mirror_references
, struct got_pathlist_head
*symrefs
,
1476 struct got_pathlist_head
*wanted_branches
,
1477 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
)
1479 const struct got_error
*err
= NULL
;
1480 const char *default_branch
= NULL
;
1481 struct got_pathlist_entry
*pe
;
1484 * If we asked for a set of wanted branches then use the first
1487 if (!TAILQ_EMPTY(wanted_branches
)) {
1488 pe
= TAILQ_FIRST(wanted_branches
);
1489 default_branch
= pe
->path
;
1491 /* First HEAD ref listed by server is the default branch. */
1492 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1493 const char *refname
= pe
->path
;
1494 const char *target
= pe
->data
;
1496 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1499 default_branch
= target
;
1504 /* Create got.conf(5). */
1505 err
= create_gotconfig(proto
, host
, port
, remote_repo_path
,
1506 default_branch
, fetch_all_branches
, wanted_branches
,
1507 wanted_refs
, mirror_references
, repo
);
1511 /* Create a config file Git can understand. */
1512 return create_gitconfig(git_url
, default_branch
, fetch_all_branches
,
1513 wanted_branches
, wanted_refs
, mirror_references
, repo
);
1516 static const struct got_error
*
1517 cmd_clone(int argc
, char *argv
[])
1519 const struct got_error
*error
= NULL
;
1520 const char *uri
, *dirname
;
1521 char *proto
, *host
, *port
, *repo_name
, *server_path
;
1522 char *default_destdir
= NULL
, *id_str
= NULL
;
1523 const char *repo_path
;
1524 struct got_repository
*repo
= NULL
;
1525 struct got_pathlist_head refs
, symrefs
, wanted_branches
, wanted_refs
;
1526 struct got_pathlist_entry
*pe
;
1527 struct got_object_id
*pack_hash
= NULL
;
1528 int ch
, fetchfd
= -1, fetchstatus
;
1529 pid_t fetchpid
= -1;
1530 struct got_fetch_progress_arg fpa
;
1531 char *git_url
= NULL
;
1532 int verbosity
= 0, fetch_all_branches
= 0, mirror_references
= 0;
1533 int bflag
= 0, list_refs_only
= 0;
1534 int *pack_fds
= NULL
;
1537 TAILQ_INIT(&symrefs
);
1538 TAILQ_INIT(&wanted_branches
);
1539 TAILQ_INIT(&wanted_refs
);
1541 while ((ch
= getopt(argc
, argv
, "ab:lmqR:v")) != -1) {
1544 fetch_all_branches
= 1;
1547 error
= got_pathlist_append(&wanted_branches
,
1557 mirror_references
= 1;
1563 error
= got_pathlist_append(&wanted_refs
,
1571 else if (verbosity
< 3)
1582 if (fetch_all_branches
&& !TAILQ_EMPTY(&wanted_branches
))
1583 option_conflict('a', 'b');
1584 if (list_refs_only
) {
1585 if (!TAILQ_EMPTY(&wanted_branches
))
1586 option_conflict('l', 'b');
1587 if (fetch_all_branches
)
1588 option_conflict('l', 'a');
1589 if (mirror_references
)
1590 option_conflict('l', 'm');
1591 if (!TAILQ_EMPTY(&wanted_refs
))
1592 option_conflict('l', 'R');
1604 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
1609 if (asprintf(&git_url
, "%s://%s%s%s%s%s", proto
,
1610 host
, port
? ":" : "", port
? port
: "",
1611 server_path
[0] != '/' ? "/" : "", server_path
) == -1) {
1612 error
= got_error_from_errno("asprintf");
1616 if (strcmp(proto
, "git") == 0) {
1618 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1619 "sendfd dns inet unveil", NULL
) == -1)
1622 } else if (strcmp(proto
, "git+ssh") == 0 ||
1623 strcmp(proto
, "ssh") == 0) {
1625 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1626 "sendfd unveil", NULL
) == -1)
1629 } else if (strcmp(proto
, "http") == 0 ||
1630 strcmp(proto
, "git+http") == 0) {
1631 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
1634 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
1637 if (dirname
== NULL
) {
1638 if (asprintf(&default_destdir
, "%s.git", repo_name
) == -1) {
1639 error
= got_error_from_errno("asprintf");
1642 repo_path
= default_destdir
;
1644 repo_path
= dirname
;
1646 if (!list_refs_only
) {
1647 error
= got_path_mkdir(repo_path
);
1649 (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
1650 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
)))
1652 if (!got_path_dir_is_empty(repo_path
)) {
1653 error
= got_error_path(repo_path
,
1654 GOT_ERR_DIR_NOT_EMPTY
);
1659 error
= got_dial_apply_unveil(proto
);
1663 error
= apply_unveil(repo_path
, 0, NULL
);
1668 printf("Connecting to %s\n", git_url
);
1670 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
1671 server_path
, verbosity
);
1675 if (!list_refs_only
) {
1676 error
= got_repo_init(repo_path
, NULL
, GOT_HASH_SHA1
);
1679 error
= got_repo_pack_fds_open(&pack_fds
);
1682 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
1687 fpa
.last_scaled_size
[0] = '\0';
1688 fpa
.last_p_indexed
= -1;
1689 fpa
.last_p_resolved
= -1;
1690 fpa
.verbosity
= verbosity
;
1691 fpa
.create_configs
= 1;
1692 fpa
.configs_created
= 0;
1694 fpa
.config_info
.symrefs
= &symrefs
;
1695 fpa
.config_info
.wanted_branches
= &wanted_branches
;
1696 fpa
.config_info
.wanted_refs
= &wanted_refs
;
1697 fpa
.config_info
.proto
= proto
;
1698 fpa
.config_info
.host
= host
;
1699 fpa
.config_info
.port
= port
;
1700 fpa
.config_info
.remote_repo_path
= server_path
;
1701 fpa
.config_info
.git_url
= git_url
;
1702 fpa
.config_info
.fetch_all_branches
= fetch_all_branches
;
1703 fpa
.config_info
.mirror_references
= mirror_references
;
1704 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
,
1705 GOT_FETCH_DEFAULT_REMOTE_NAME
, mirror_references
,
1706 fetch_all_branches
, &wanted_branches
, &wanted_refs
,
1707 list_refs_only
, verbosity
, fetchfd
, repo
, NULL
, NULL
, bflag
,
1708 fetch_progress
, &fpa
);
1712 if (list_refs_only
) {
1713 error
= list_remote_refs(&symrefs
, &refs
);
1717 if (pack_hash
== NULL
) {
1718 error
= got_error_fmt(GOT_ERR_FETCH_FAILED
, "%s",
1719 "server sent an empty pack file");
1722 error
= got_object_id_str(&id_str
, pack_hash
);
1726 printf("\nFetched %s.pack\n", id_str
);
1729 /* Set up references provided with the pack file. */
1730 TAILQ_FOREACH(pe
, &refs
, entry
) {
1731 const char *refname
= pe
->path
;
1732 struct got_object_id
*id
= pe
->data
;
1733 char *remote_refname
;
1735 if (is_wanted_ref(&wanted_refs
, refname
) &&
1736 !mirror_references
) {
1737 error
= create_wanted_ref(refname
, id
,
1738 GOT_FETCH_DEFAULT_REMOTE_NAME
,
1739 verbosity
- 1, repo
);
1745 error
= create_ref(refname
, id
, verbosity
- 1, repo
);
1749 if (mirror_references
)
1752 if (strncmp("refs/heads/", refname
, 11) != 0)
1755 if (asprintf(&remote_refname
,
1756 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1757 refname
+ 11) == -1) {
1758 error
= got_error_from_errno("asprintf");
1761 error
= create_ref(remote_refname
, id
, verbosity
- 1, repo
);
1762 free(remote_refname
);
1767 /* Set the HEAD reference if the server provided one. */
1768 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
1769 struct got_reference
*target_ref
;
1770 const char *refname
= pe
->path
;
1771 const char *target
= pe
->data
;
1772 char *remote_refname
= NULL
, *remote_target
= NULL
;
1774 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1777 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1779 if (error
->code
== GOT_ERR_NOT_REF
) {
1786 error
= create_symref(refname
, target_ref
, verbosity
, repo
);
1787 got_ref_close(target_ref
);
1791 if (mirror_references
)
1794 if (strncmp("refs/heads/", target
, 11) != 0)
1797 if (asprintf(&remote_refname
,
1798 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1800 error
= got_error_from_errno("asprintf");
1803 if (asprintf(&remote_target
,
1804 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1805 target
+ 11) == -1) {
1806 error
= got_error_from_errno("asprintf");
1807 free(remote_refname
);
1810 error
= got_ref_open(&target_ref
, repo
, remote_target
, 0);
1812 free(remote_refname
);
1813 free(remote_target
);
1814 if (error
->code
== GOT_ERR_NOT_REF
) {
1820 error
= create_symref(remote_refname
, target_ref
,
1821 verbosity
- 1, repo
);
1822 free(remote_refname
);
1823 free(remote_target
);
1824 got_ref_close(target_ref
);
1830 * We failed to set the HEAD reference. If we asked for
1831 * a set of wanted branches use the first of one of those
1832 * which could be fetched instead.
1834 TAILQ_FOREACH(pe
, &wanted_branches
, entry
) {
1835 const char *target
= pe
->path
;
1836 struct got_reference
*target_ref
;
1838 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1840 if (error
->code
== GOT_ERR_NOT_REF
) {
1847 error
= create_symref(GOT_REF_HEAD
, target_ref
,
1849 got_ref_close(target_ref
);
1855 if (!fpa
.configs_created
&& pe
!= NULL
) {
1856 error
= create_config_files(fpa
.config_info
.proto
,
1857 fpa
.config_info
.host
, fpa
.config_info
.port
,
1858 fpa
.config_info
.remote_repo_path
,
1859 fpa
.config_info
.git_url
,
1860 fpa
.config_info
.fetch_all_branches
,
1861 fpa
.config_info
.mirror_references
,
1862 fpa
.config_info
.symrefs
,
1863 fpa
.config_info
.wanted_branches
,
1864 fpa
.config_info
.wanted_refs
, fpa
.repo
);
1871 printf("Created %s repository '%s'\n",
1872 mirror_references
? "mirrored" : "cloned", repo_path
);
1875 const struct got_error
*pack_err
=
1876 got_repo_pack_fds_close(pack_fds
);
1881 if (kill(fetchpid
, SIGTERM
) == -1)
1882 error
= got_error_from_errno("kill");
1883 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
1884 error
= got_error_from_errno("waitpid");
1886 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
1887 error
= got_error_from_errno("close");
1889 const struct got_error
*close_err
= got_repo_close(repo
);
1893 got_pathlist_free(&refs
, GOT_PATHLIST_FREE_ALL
);
1894 got_pathlist_free(&symrefs
, GOT_PATHLIST_FREE_ALL
);
1895 got_pathlist_free(&wanted_branches
, GOT_PATHLIST_FREE_NONE
);
1896 got_pathlist_free(&wanted_refs
, GOT_PATHLIST_FREE_NONE
);
1903 free(default_destdir
);
1908 static const struct got_error
*
1909 update_ref(struct got_reference
*ref
, struct got_object_id
*new_id
,
1910 int replace_tags
, int verbosity
, struct got_repository
*repo
)
1912 const struct got_error
*err
= NULL
;
1913 char *new_id_str
= NULL
;
1914 struct got_object_id
*old_id
= NULL
;
1916 err
= got_object_id_str(&new_id_str
, new_id
);
1920 if (!replace_tags
&&
1921 strncmp(got_ref_get_name(ref
), "refs/tags/", 10) == 0) {
1922 err
= got_ref_resolve(&old_id
, repo
, ref
);
1925 if (got_object_id_cmp(old_id
, new_id
) == 0)
1927 if (verbosity
>= 0) {
1928 printf("Rejecting update of existing tag %s: %s\n",
1929 got_ref_get_name(ref
), new_id_str
);
1934 if (got_ref_is_symbolic(ref
)) {
1935 if (verbosity
>= 0) {
1936 printf("Replacing reference %s: %s\n",
1937 got_ref_get_name(ref
),
1938 got_ref_get_symref_target(ref
));
1940 err
= got_ref_change_symref_to_ref(ref
, new_id
);
1943 err
= got_ref_write(ref
, repo
);
1947 err
= got_ref_resolve(&old_id
, repo
, ref
);
1950 if (got_object_id_cmp(old_id
, new_id
) == 0)
1953 err
= got_ref_change_ref(ref
, new_id
);
1956 err
= got_ref_write(ref
, repo
);
1962 printf("Updated %s: %s\n", got_ref_get_name(ref
),
1970 static const struct got_error
*
1971 update_wanted_ref(const char *refname
, struct got_object_id
*id
,
1972 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
1974 const struct got_error
*err
, *unlock_err
;
1975 char *remote_refname
;
1976 struct got_reference
*ref
;
1978 if (strncmp("refs/", refname
, 5) == 0)
1981 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
1982 remote_repo_name
, refname
) == -1)
1983 return got_error_from_errno("asprintf");
1985 err
= got_ref_open(&ref
, repo
, remote_refname
, 1);
1987 if (err
->code
!= GOT_ERR_NOT_REF
)
1989 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
1991 err
= update_ref(ref
, id
, 0, verbosity
, repo
);
1992 unlock_err
= got_ref_unlock(ref
);
1993 if (unlock_err
&& err
== NULL
)
1998 free(remote_refname
);
2003 usage_checkout(void)
2005 fprintf(stderr
, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2006 "[-p path-prefix] repository-path [work-tree-path]\n",
2012 show_worktree_base_ref_warning(void)
2014 fprintf(stderr
, "%s: warning: could not create a reference "
2015 "to the work tree's base commit; the commit could be "
2016 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2017 "repository writable and running 'got update' will prevent this\n",
2021 struct got_checkout_progress_arg
{
2022 const char *worktree_path
;
2023 int had_base_commit_ref_error
;
2027 static const struct got_error
*
2028 checkout_progress(void *arg
, unsigned char status
, const char *path
)
2030 struct got_checkout_progress_arg
*a
= arg
;
2032 /* Base commit bump happens silently. */
2033 if (status
== GOT_STATUS_BUMP_BASE
)
2036 if (status
== GOT_STATUS_BASE_REF_ERR
) {
2037 a
->had_base_commit_ref_error
= 1;
2041 while (path
[0] == '/')
2044 if (a
->verbosity
>= 0)
2045 printf("%c %s/%s\n", status
, a
->worktree_path
, path
);
2050 static const struct got_error
*
2051 check_cancelled(void *arg
)
2053 if (sigint_received
|| sigpipe_received
)
2054 return got_error(GOT_ERR_CANCELLED
);
2058 static const struct got_error
*
2059 check_linear_ancestry(struct got_object_id
*commit_id
,
2060 struct got_object_id
*base_commit_id
, int allow_forwards_in_time_only
,
2061 struct got_repository
*repo
)
2063 const struct got_error
*err
= NULL
;
2064 struct got_object_id
*yca_id
;
2066 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
2067 commit_id
, base_commit_id
, 1, 0, repo
, check_cancelled
, NULL
);
2072 return got_error(GOT_ERR_ANCESTRY
);
2075 * Require a straight line of history between the target commit
2076 * and the work tree's base commit.
2078 * Non-linear situations such as this require a rebase:
2080 * (commit) D F (base_commit)
2088 * 'got update' only handles linear cases:
2089 * Update forwards in time: A (base/yca) - B - C - D (commit)
2090 * Update backwards in time: D (base) - C - B - A (commit/yca)
2092 if (allow_forwards_in_time_only
) {
2093 if (got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2094 return got_error(GOT_ERR_ANCESTRY
);
2095 } else if (got_object_id_cmp(commit_id
, yca_id
) != 0 &&
2096 got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2097 return got_error(GOT_ERR_ANCESTRY
);
2103 static const struct got_error
*
2104 check_same_branch(struct got_object_id
*commit_id
,
2105 struct got_reference
*head_ref
, struct got_repository
*repo
)
2107 const struct got_error
*err
= NULL
;
2108 struct got_commit_graph
*graph
= NULL
;
2109 struct got_object_id
*head_commit_id
= NULL
;
2111 err
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
2115 if (got_object_id_cmp(head_commit_id
, commit_id
) == 0)
2118 err
= got_commit_graph_open(&graph
, "/", 1);
2122 err
= got_commit_graph_bfsort(graph
, head_commit_id
, repo
,
2123 check_cancelled
, NULL
);
2128 struct got_object_id id
;
2130 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
2131 check_cancelled
, NULL
);
2133 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
2134 err
= got_error(GOT_ERR_ANCESTRY
);
2138 if (got_object_id_cmp(&id
, commit_id
) == 0)
2143 got_commit_graph_close(graph
);
2144 free(head_commit_id
);
2148 static const struct got_error
*
2149 checkout_ancestry_error(struct got_reference
*ref
, const char *commit_id_str
)
2151 static char msg
[512];
2152 const char *branch_name
;
2154 if (got_ref_is_symbolic(ref
))
2155 branch_name
= got_ref_get_symref_target(ref
);
2157 branch_name
= got_ref_get_name(ref
);
2159 if (strncmp("refs/heads/", branch_name
, 11) == 0)
2162 snprintf(msg
, sizeof(msg
),
2163 "target commit is not contained in branch '%s'; "
2164 "the branch to use must be specified with -b; "
2165 "if necessary a new branch can be created for "
2166 "this commit with 'got branch -c %s BRANCH_NAME'",
2167 branch_name
, commit_id_str
);
2169 return got_error_msg(GOT_ERR_ANCESTRY
, msg
);
2172 static const struct got_error
*
2173 cmd_checkout(int argc
, char *argv
[])
2175 const struct got_error
*error
= NULL
;
2176 struct got_repository
*repo
= NULL
;
2177 struct got_reference
*head_ref
= NULL
, *ref
= NULL
;
2178 struct got_worktree
*worktree
= NULL
;
2179 char *repo_path
= NULL
;
2180 char *worktree_path
= NULL
;
2181 const char *path_prefix
= "";
2182 const char *branch_name
= GOT_REF_HEAD
, *refname
= NULL
;
2183 char *commit_id_str
= NULL
;
2184 struct got_object_id
*commit_id
= NULL
;
2186 int ch
, same_path_prefix
, allow_nonempty
= 0, verbosity
= 0;
2187 struct got_pathlist_head paths
;
2188 struct got_checkout_progress_arg cpa
;
2189 int *pack_fds
= NULL
;
2194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2195 "unveil", NULL
) == -1)
2199 while ((ch
= getopt(argc
, argv
, "b:c:Ep:q")) != -1) {
2202 branch_name
= optarg
;
2205 commit_id_str
= strdup(optarg
);
2206 if (commit_id_str
== NULL
)
2207 return got_error_from_errno("strdup");
2213 path_prefix
= optarg
;
2228 char *base
, *dotgit
;
2230 repo_path
= realpath(argv
[0], NULL
);
2231 if (repo_path
== NULL
)
2232 return got_error_from_errno2("realpath", argv
[0]);
2233 cwd
= getcwd(NULL
, 0);
2235 error
= got_error_from_errno("getcwd");
2242 error
= got_path_basename(&base
, path
);
2245 dotgit
= strstr(base
, ".git");
2248 if (asprintf(&worktree_path
, "%s/%s", cwd
, base
) == -1) {
2249 error
= got_error_from_errno("asprintf");
2254 } else if (argc
== 2) {
2255 repo_path
= realpath(argv
[0], NULL
);
2256 if (repo_path
== NULL
) {
2257 error
= got_error_from_errno2("realpath", argv
[0]);
2260 worktree_path
= realpath(argv
[1], NULL
);
2261 if (worktree_path
== NULL
) {
2262 if (errno
!= ENOENT
) {
2263 error
= got_error_from_errno2("realpath",
2267 worktree_path
= strdup(argv
[1]);
2268 if (worktree_path
== NULL
) {
2269 error
= got_error_from_errno("strdup");
2276 got_path_strip_trailing_slashes(repo_path
);
2277 got_path_strip_trailing_slashes(worktree_path
);
2279 error
= got_repo_pack_fds_open(&pack_fds
);
2283 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
2287 /* Pre-create work tree path for unveil(2) */
2288 error
= got_path_mkdir(worktree_path
);
2290 if (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
2291 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
2293 if (!allow_nonempty
&&
2294 !got_path_dir_is_empty(worktree_path
)) {
2295 error
= got_error_path(worktree_path
,
2296 GOT_ERR_DIR_NOT_EMPTY
);
2301 error
= apply_unveil(got_repo_get_path(repo
), 0, worktree_path
);
2305 error
= got_ref_open(&head_ref
, repo
, branch_name
, 0);
2309 error
= got_worktree_init(worktree_path
, head_ref
, path_prefix
,
2310 GOT_WORKTREE_CVG_DIR
, repo
);
2311 if (error
!= NULL
&& !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
2314 error
= got_worktree_open(&worktree
, worktree_path
, GOT_WORKTREE_CVG_DIR
);
2318 error
= got_worktree_match_path_prefix(&same_path_prefix
, worktree
,
2322 if (!same_path_prefix
) {
2323 error
= got_error(GOT_ERR_PATH_PREFIX
);
2327 if (commit_id_str
) {
2328 struct got_reflist_head refs
;
2330 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
2334 error
= got_repo_match_object_id(&commit_id
, NULL
,
2335 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
2336 got_ref_list_free(&refs
);
2339 error
= check_linear_ancestry(commit_id
,
2340 got_worktree_get_base_commit_id(worktree
), 0, repo
);
2341 if (error
!= NULL
) {
2342 if (error
->code
== GOT_ERR_ANCESTRY
) {
2343 error
= checkout_ancestry_error(
2344 head_ref
, commit_id_str
);
2348 error
= check_same_branch(commit_id
, head_ref
, repo
);
2350 if (error
->code
== GOT_ERR_ANCESTRY
) {
2351 error
= checkout_ancestry_error(
2352 head_ref
, commit_id_str
);
2356 error
= got_worktree_set_base_commit_id(worktree
, repo
,
2360 /* Expand potentially abbreviated commit ID string. */
2361 free(commit_id_str
);
2362 error
= got_object_id_str(&commit_id_str
, commit_id
);
2366 commit_id
= got_object_id_dup(
2367 got_worktree_get_base_commit_id(worktree
));
2368 if (commit_id
== NULL
) {
2369 error
= got_error_from_errno("got_object_id_dup");
2372 error
= got_object_id_str(&commit_id_str
, commit_id
);
2377 error
= got_pathlist_append(&paths
, "", NULL
);
2380 cpa
.worktree_path
= worktree_path
;
2381 cpa
.had_base_commit_ref_error
= 0;
2382 cpa
.verbosity
= verbosity
;
2383 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
2384 checkout_progress
, &cpa
, check_cancelled
, NULL
);
2388 if (got_ref_is_symbolic(head_ref
)) {
2389 error
= got_ref_resolve_symbolic(&ref
, repo
, head_ref
);
2392 refname
= got_ref_get_name(ref
);
2394 refname
= got_ref_get_name(head_ref
);
2395 printf("Checked out %s: %s\n", refname
, commit_id_str
);
2396 printf("Now shut up and hack\n");
2397 if (cpa
.had_base_commit_ref_error
)
2398 show_worktree_base_ref_warning();
2401 const struct got_error
*pack_err
=
2402 got_repo_pack_fds_close(pack_fds
);
2407 got_ref_close(head_ref
);
2411 const struct got_error
*close_err
= got_repo_close(repo
);
2415 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
2416 free(commit_id_str
);
2419 free(worktree_path
);
2424 struct got_update_progress_arg
{
2436 print_update_progress_stats(struct got_update_progress_arg
*upa
)
2438 if (!upa
->did_something
)
2441 if (upa
->conflicts
> 0)
2442 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
2443 if (upa
->obstructed
> 0)
2444 printf("File paths obstructed by a non-regular file: %d\n",
2446 if (upa
->not_updated
> 0)
2447 printf("Files not updated because of existing merge "
2448 "conflicts: %d\n", upa
->not_updated
);
2452 * The meaning of some status codes differs between merge-style operations and
2453 * update operations. For example, the ! status code means "file was missing"
2454 * if changes were merged into the work tree, and "missing file was restored"
2455 * if the work tree was updated. This function should be used by any operation
2456 * which merges changes into the work tree without updating the work tree.
2459 print_merge_progress_stats(struct got_update_progress_arg
*upa
)
2461 if (!upa
->did_something
)
2464 if (upa
->conflicts
> 0)
2465 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
2466 if (upa
->obstructed
> 0)
2467 printf("File paths obstructed by a non-regular file: %d\n",
2469 if (upa
->missing
> 0)
2470 printf("Files which had incoming changes but could not be "
2471 "found in the work tree: %d\n", upa
->missing
);
2472 if (upa
->not_deleted
> 0)
2473 printf("Files not deleted due to differences in deleted "
2474 "content: %d\n", upa
->not_deleted
);
2475 if (upa
->unversioned
> 0)
2476 printf("Files not merged because an unversioned file was "
2477 "found in the work tree: %d\n", upa
->unversioned
);
2483 fprintf(stderr
, "usage: %s update [-qtvX] [-c commit] [-r remote] "
2484 "[path ...]\n", getprogname());
2488 static const struct got_error
*
2489 update_progress(void *arg
, unsigned char status
, const char *path
)
2491 struct got_update_progress_arg
*upa
= arg
;
2493 if (status
== GOT_STATUS_EXISTS
||
2494 status
== GOT_STATUS_BASE_REF_ERR
)
2497 upa
->did_something
= 1;
2499 /* Base commit bump happens silently. */
2500 if (status
== GOT_STATUS_BUMP_BASE
)
2503 if (status
== GOT_STATUS_CONFLICT
)
2505 if (status
== GOT_STATUS_OBSTRUCTED
)
2507 if (status
== GOT_STATUS_CANNOT_UPDATE
)
2509 if (status
== GOT_STATUS_MISSING
)
2511 if (status
== GOT_STATUS_CANNOT_DELETE
)
2513 if (status
== GOT_STATUS_UNVERSIONED
)
2516 while (path
[0] == '/')
2518 if (upa
->verbosity
>= 0)
2519 printf("%c %s\n", status
, path
);
2524 static const struct got_error
*
2525 check_rebase_or_histedit_in_progress(struct got_worktree
*worktree
)
2527 const struct got_error
*err
;
2530 err
= got_worktree_rebase_in_progress(&in_progress
, worktree
);
2534 return got_error(GOT_ERR_REBASING
);
2536 err
= got_worktree_histedit_in_progress(&in_progress
, worktree
);
2540 return got_error(GOT_ERR_HISTEDIT_BUSY
);
2545 static const struct got_error
*
2546 check_merge_in_progress(struct got_worktree
*worktree
,
2547 struct got_repository
*repo
)
2549 const struct got_error
*err
;
2552 err
= got_worktree_merge_in_progress(&in_progress
, worktree
, repo
);
2556 return got_error(GOT_ERR_MERGE_BUSY
);
2561 static const struct got_error
*
2562 get_worktree_paths_from_argv(struct got_pathlist_head
*paths
, int argc
,
2563 char *argv
[], struct got_worktree
*worktree
)
2565 const struct got_error
*err
= NULL
;
2567 struct got_pathlist_entry
*new;
2573 return got_error_from_errno("strdup");
2574 return got_pathlist_append(paths
, path
, NULL
);
2577 for (i
= 0; i
< argc
; i
++) {
2578 err
= got_worktree_resolve_path(&path
, worktree
, argv
[i
]);
2581 err
= got_pathlist_insert(&new, paths
, path
, NULL
);
2582 if (err
|| new == NULL
/* duplicate */) {
2592 static const struct got_error
*
2593 wrap_not_worktree_error(const struct got_error
*orig_err
,
2594 const char *cmdname
, const char *path
)
2596 const struct got_error
*err
;
2597 struct got_repository
*repo
;
2598 static char msg
[512];
2599 int *pack_fds
= NULL
;
2601 err
= got_repo_pack_fds_open(&pack_fds
);
2605 err
= got_repo_open(&repo
, path
, NULL
, pack_fds
);
2609 snprintf(msg
, sizeof(msg
),
2610 "'got %s' needs a work tree in addition to a git repository\n"
2611 "Work trees can be checked out from this Git repository with "
2613 "The got(1) manual page contains more information.", cmdname
);
2614 err
= got_error_msg(GOT_ERR_NOT_WORKTREE
, msg
);
2616 const struct got_error
*close_err
= got_repo_close(repo
);
2621 const struct got_error
*pack_err
=
2622 got_repo_pack_fds_close(pack_fds
);
2629 static const struct got_error
*
2630 cmd_update(int argc
, char *argv
[])
2632 const struct got_error
*error
= NULL
, *unlock_err
;
2633 char *worktree_path
= NULL
;
2634 const char *repo_path
= NULL
;
2635 const char *remote_name
= NULL
;
2636 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
2637 char *repo_name
= NULL
, *server_path
= NULL
;
2638 const struct got_remote_repo
*remotes
, *remote
= NULL
;
2640 char *id_str
= NULL
;
2641 struct got_repository
*repo
= NULL
;
2642 struct got_worktree
*worktree
= NULL
;
2643 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
2644 struct got_pathlist_head paths
, refs
, symrefs
;
2645 struct got_pathlist_head wanted_branches
, wanted_refs
;
2646 struct got_pathlist_entry
*pe
;
2647 struct got_reflist_head remote_refs
;
2648 struct got_reflist_entry
*re
;
2649 struct got_object_id
*pack_hash
= NULL
;
2650 int i
, ch
, fetchfd
= -1, fetchstatus
;
2651 pid_t fetchpid
= -1;
2652 struct got_fetch_progress_arg fpa
;
2653 struct got_update_progress_arg upa
;
2655 int delete_remote
= 0;
2656 int replace_tags
= 0;
2657 int *pack_fds
= NULL
;
2658 const char *remote_head
= NULL
, *worktree_branch
= NULL
;
2659 struct got_object_id
*commit_id
= NULL
;
2660 char *commit_id_str
= NULL
;
2661 const char *refname
;
2662 struct got_reference
*head_ref
= NULL
;
2666 TAILQ_INIT(&symrefs
);
2667 TAILQ_INIT(&remote_refs
);
2668 TAILQ_INIT(&wanted_branches
);
2669 TAILQ_INIT(&wanted_refs
);
2671 while ((ch
= getopt(argc
, argv
, "c:qr:vX")) != -1) {
2674 commit_id_str
= strdup(optarg
);
2675 if (commit_id_str
== NULL
)
2676 return got_error_from_errno("strdup");
2685 remote_name
= optarg
;
2690 else if (verbosity
< 3)
2704 if (delete_remote
) {
2706 option_conflict('X', 't');
2707 if (remote_name
== NULL
)
2708 errx(1, "-X option requires a remote name");
2710 if (remote_name
== NULL
)
2711 remote_name
= GOT_FETCH_DEFAULT_REMOTE_NAME
;
2713 worktree_path
= getcwd(NULL
, 0);
2714 if (worktree_path
== NULL
) {
2715 error
= got_error_from_errno("getcwd");
2718 error
= got_worktree_open(&worktree
, worktree_path
, GOT_WORKTREE_CVG_DIR
);
2720 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
2721 error
= wrap_not_worktree_error(error
, "update",
2725 repo_path
= got_worktree_get_repo_path(worktree
);
2727 error
= got_repo_pack_fds_open(&pack_fds
);
2731 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
2735 error
= check_rebase_or_histedit_in_progress(worktree
);
2738 error
= check_merge_in_progress(worktree
, repo
);
2742 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
2746 worktree_conf
= got_worktree_get_gotconfig(worktree
);
2747 if (worktree_conf
) {
2748 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2750 for (i
= 0; i
< nremotes
; i
++) {
2751 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2752 remote
= &remotes
[i
];
2758 if (remote
== NULL
) {
2759 repo_conf
= got_repo_get_gotconfig(repo
);
2761 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2763 for (i
= 0; i
< nremotes
; i
++) {
2764 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2765 remote
= &remotes
[i
];
2771 if (remote
== NULL
) {
2772 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
2773 for (i
= 0; i
< nremotes
; i
++) {
2774 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2775 remote
= &remotes
[i
];
2780 if (remote
== NULL
) {
2781 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
2785 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
2786 &repo_name
, remote
->fetch_url
);
2790 if (strcmp(proto
, "git") == 0) {
2792 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2793 "sendfd dns inet unveil", NULL
) == -1)
2796 } else if (strcmp(proto
, "git+ssh") == 0 ||
2797 strcmp(proto
, "ssh") == 0) {
2799 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2800 "sendfd unveil", NULL
) == -1)
2803 } else if (strcmp(proto
, "http") == 0 ||
2804 strcmp(proto
, "git+http") == 0) {
2805 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
2808 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
2812 error
= got_dial_apply_unveil(proto
);
2816 error
= apply_unveil(got_repo_get_path(repo
), 0,
2817 got_worktree_get_root_path(worktree
));
2821 if (verbosity
>= 0) {
2822 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2823 remote
->name
, proto
, host
,
2824 port
? ":" : "", port
? port
: "",
2825 *server_path
== '/' ? "" : "/", server_path
);
2828 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
2829 server_path
, verbosity
);
2834 * If set, get this remote's HEAD ref target so
2835 * if it has changed on the server we can fetch it.
2837 error
= got_ref_list(&remote_refs
, repo
, "refs/remotes",
2838 got_ref_cmp_by_name
, repo
);
2842 TAILQ_FOREACH(re
, &remote_refs
, entry
) {
2843 const char *remote_refname
, *remote_target
;
2844 size_t remote_name_len
;
2846 if (!got_ref_is_symbolic(re
->ref
))
2849 remote_name_len
= strlen(remote
->name
);
2850 remote_refname
= got_ref_get_name(re
->ref
);
2852 /* we only want refs/remotes/$remote->name/HEAD */
2853 if (strncmp(remote_refname
+ 13, remote
->name
,
2854 remote_name_len
) != 0)
2857 if (strcmp(remote_refname
+ remote_name_len
+ 14,
2862 * Take the name itself because we already
2863 * only match with refs/heads/ in fetch_pack().
2865 remote_target
= got_ref_get_symref_target(re
->ref
);
2866 remote_head
= remote_target
+ remote_name_len
+ 14;
2870 refname
= got_worktree_get_head_ref_name(worktree
);
2871 if (strncmp(refname
, "refs/heads/", 11) == 0)
2872 worktree_branch
= refname
;
2874 fpa
.last_scaled_size
[0] = '\0';
2875 fpa
.last_p_indexed
= -1;
2876 fpa
.last_p_resolved
= -1;
2877 fpa
.verbosity
= verbosity
;
2879 fpa
.create_configs
= 0;
2880 fpa
.configs_created
= 0;
2881 memset(&fpa
.config_info
, 0, sizeof(fpa
.config_info
));
2883 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
, remote
->name
,
2884 remote
->mirror_references
, 0, &wanted_branches
, &wanted_refs
,
2885 0, verbosity
, fetchfd
, repo
, worktree_branch
, remote_head
,
2886 0, fetch_progress
, &fpa
);
2890 if (pack_hash
!= NULL
&& verbosity
>= 0) {
2891 error
= got_object_id_str(&id_str
, pack_hash
);
2894 printf("\nFetched %s.pack\n", id_str
);
2899 /* Update references provided with the pack file. */
2900 TAILQ_FOREACH(pe
, &refs
, entry
) {
2901 const char *refname
= pe
->path
;
2902 struct got_object_id
*id
= pe
->data
;
2903 struct got_reference
*ref
;
2905 if (is_wanted_ref(&wanted_refs
, refname
)) {
2906 error
= update_wanted_ref(refname
, id
,
2907 remote
->name
, verbosity
, repo
);
2913 error
= got_ref_open(&ref
, repo
, refname
, 1);
2915 if (error
->code
!= GOT_ERR_NOT_REF
)
2917 error
= create_ref(refname
, id
, verbosity
,
2922 error
= update_ref(ref
, id
, replace_tags
,
2924 unlock_err
= got_ref_unlock(ref
);
2925 if (unlock_err
&& error
== NULL
)
2933 /* Update worktree */
2934 error
= got_ref_open(&head_ref
, repo
,
2935 got_worktree_get_head_ref_name(worktree
), 0);
2938 if (commit_id_str
== NULL
) {
2939 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
2942 error
= got_object_id_str(&commit_id_str
, commit_id
);
2946 struct got_reflist_head refs
;
2948 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
2952 error
= got_repo_match_object_id(&commit_id
, NULL
,
2953 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
2954 got_ref_list_free(&refs
);
2955 free(commit_id_str
);
2956 commit_id_str
= NULL
;
2959 error
= got_object_id_str(&commit_id_str
, commit_id
);
2965 error
= check_linear_ancestry(commit_id
,
2966 got_worktree_get_base_commit_id(worktree
), 0, repo
);
2967 if (error
!= NULL
) {
2968 if (error
->code
== GOT_ERR_ANCESTRY
)
2969 error
= got_error(GOT_ERR_BRANCH_MOVED
);
2972 error
= check_same_branch(commit_id
, head_ref
, repo
);
2976 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree
),
2978 error
= got_worktree_set_base_commit_id(worktree
, repo
,
2984 memset(&upa
, 0, sizeof(upa
));
2985 upa
.verbosity
= verbosity
;
2986 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
2987 update_progress
, &upa
, check_cancelled
, NULL
);
2991 if (upa
.did_something
) {
2992 printf("Updated to %s: %s\n",
2993 got_worktree_get_head_ref_name(worktree
), commit_id_str
);
2995 printf("Already up-to-date\n");
2997 print_update_progress_stats(&upa
);
3000 if (kill(fetchpid
, SIGTERM
) == -1)
3001 error
= got_error_from_errno("kill");
3002 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
3003 error
= got_error_from_errno("waitpid");
3005 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
3006 error
= got_error_from_errno("close");
3008 const struct got_error
*close_err
= got_repo_close(repo
);
3013 got_worktree_close(worktree
);
3015 const struct got_error
*pack_err
=
3016 got_repo_pack_fds_close(pack_fds
);
3020 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
3021 got_pathlist_free(&refs
, GOT_PATHLIST_FREE_ALL
);
3022 got_pathlist_free(&symrefs
, GOT_PATHLIST_FREE_ALL
);
3023 got_pathlist_free(&wanted_branches
, GOT_PATHLIST_FREE_NONE
);
3024 got_pathlist_free(&wanted_refs
, GOT_PATHLIST_FREE_NONE
);
3025 got_ref_list_free(&remote_refs
);
3027 free(worktree_path
);
3035 free(commit_id_str
);
3039 static const struct got_error
*
3040 diff_blobs(struct got_object_id
*blob_id1
, struct got_object_id
*blob_id2
,
3041 const char *path
, int diff_context
, int ignore_whitespace
,
3042 int force_text_diff
, struct got_diffstat_cb_arg
*dsa
,
3043 struct got_repository
*repo
, FILE *outfile
)
3045 const struct got_error
*err
= NULL
;
3046 struct got_blob_object
*blob1
= NULL
, *blob2
= NULL
;
3047 FILE *f1
= NULL
, *f2
= NULL
;
3048 int fd1
= -1, fd2
= -1;
3050 fd1
= got_opentempfd();
3052 return got_error_from_errno("got_opentempfd");
3053 fd2
= got_opentempfd();
3055 err
= got_error_from_errno("got_opentempfd");
3060 err
= got_object_open_as_blob(&blob1
, repo
, blob_id1
, 8192,
3066 err
= got_object_open_as_blob(&blob2
, repo
, blob_id2
, 8192, fd2
);
3070 f1
= got_opentemp();
3072 err
= got_error_from_errno("got_opentemp");
3075 f2
= got_opentemp();
3077 err
= got_error_from_errno("got_opentemp");
3081 while (path
[0] == '/')
3083 err
= got_diff_blob(NULL
, NULL
, blob1
, blob2
, f1
, f2
, path
, path
,
3084 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
, ignore_whitespace
,
3085 force_text_diff
, dsa
, outfile
);
3087 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3088 err
= got_error_from_errno("close");
3090 got_object_blob_close(blob1
);
3091 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3092 err
= got_error_from_errno("close");
3094 got_object_blob_close(blob2
);
3095 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3096 err
= got_error_from_errno("fclose");
3097 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3098 err
= got_error_from_errno("fclose");
3102 static const struct got_error
*
3103 diff_trees(struct got_object_id
*tree_id1
, struct got_object_id
*tree_id2
,
3104 const char *path
, int diff_context
, int ignore_whitespace
,
3105 int force_text_diff
, struct got_diffstat_cb_arg
*dsa
,
3106 struct got_repository
*repo
, FILE *outfile
)
3108 const struct got_error
*err
= NULL
;
3109 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3110 struct got_diff_blob_output_unidiff_arg arg
;
3111 FILE *f1
= NULL
, *f2
= NULL
;
3112 int fd1
= -1, fd2
= -1;
3115 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3118 fd1
= got_opentempfd();
3120 err
= got_error_from_errno("got_opentempfd");
3125 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3129 f1
= got_opentemp();
3131 err
= got_error_from_errno("got_opentemp");
3135 f2
= got_opentemp();
3137 err
= got_error_from_errno("got_opentemp");
3140 fd2
= got_opentempfd();
3142 err
= got_error_from_errno("got_opentempfd");
3145 arg
.diff_context
= diff_context
;
3146 arg
.ignore_whitespace
= ignore_whitespace
;
3147 arg
.force_text_diff
= force_text_diff
;
3149 arg
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
3150 arg
.outfile
= outfile
;
3153 while (path
[0] == '/')
3155 err
= got_diff_tree(tree1
, tree2
, f1
, f2
, fd1
, fd2
, path
, path
, repo
,
3156 got_diff_blob_output_unidiff
, &arg
, 1);
3159 got_object_tree_close(tree1
);
3161 got_object_tree_close(tree2
);
3162 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3163 err
= got_error_from_errno("fclose");
3164 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3165 err
= got_error_from_errno("fclose");
3166 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3167 err
= got_error_from_errno("close");
3168 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3169 err
= got_error_from_errno("close");
3173 static const struct got_error
*
3174 get_changed_paths(struct got_pathlist_head
*paths
,
3175 struct got_commit_object
*commit
, struct got_repository
*repo
,
3176 struct got_diffstat_cb_arg
*dsa
)
3178 const struct got_error
*err
= NULL
;
3179 struct got_object_id
*tree_id1
= NULL
, *tree_id2
= NULL
;
3180 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3181 struct got_object_qid
*qid
;
3182 got_diff_blob_cb cb
= got_diff_tree_collect_changed_paths
;
3183 FILE *f1
= NULL
, *f2
= NULL
;
3184 int fd1
= -1, fd2
= -1;
3187 cb
= got_diff_tree_compute_diffstat
;
3189 f1
= got_opentemp();
3191 err
= got_error_from_errno("got_opentemp");
3194 f2
= got_opentemp();
3196 err
= got_error_from_errno("got_opentemp");
3199 fd1
= got_opentempfd();
3201 err
= got_error_from_errno("got_opentempfd");
3204 fd2
= got_opentempfd();
3206 err
= got_error_from_errno("got_opentempfd");
3211 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3213 struct got_commit_object
*pcommit
;
3214 err
= got_object_open_as_commit(&pcommit
, repo
,
3219 tree_id1
= got_object_id_dup(
3220 got_object_commit_get_tree_id(pcommit
));
3221 if (tree_id1
== NULL
) {
3222 got_object_commit_close(pcommit
);
3223 return got_error_from_errno("got_object_id_dup");
3225 got_object_commit_close(pcommit
);
3230 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3235 tree_id2
= got_object_commit_get_tree_id(commit
);
3236 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3240 err
= got_diff_tree(tree1
, tree2
, f1
, f2
, fd1
, fd2
, "", "", repo
,
3241 cb
, dsa
? (void *)dsa
: paths
, dsa
? 1 : 0);
3244 got_object_tree_close(tree1
);
3246 got_object_tree_close(tree2
);
3247 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3248 err
= got_error_from_errno("close");
3249 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3250 err
= got_error_from_errno("close");
3251 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3252 err
= got_error_from_errno("fclose");
3253 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3254 err
= got_error_from_errno("fclose");
3259 static const struct got_error
*
3260 print_patch(struct got_commit_object
*commit
, struct got_object_id
*id
,
3261 const char *path
, int diff_context
, struct got_diffstat_cb_arg
*dsa
,
3262 struct got_repository
*repo
, FILE *outfile
)
3264 const struct got_error
*err
= NULL
;
3265 struct got_commit_object
*pcommit
= NULL
;
3266 char *id_str1
= NULL
, *id_str2
= NULL
;
3267 struct got_object_id
*obj_id1
= NULL
, *obj_id2
= NULL
;
3268 struct got_object_qid
*qid
;
3270 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3272 err
= got_object_open_as_commit(&pcommit
, repo
,
3276 err
= got_object_id_str(&id_str1
, &qid
->id
);
3281 err
= got_object_id_str(&id_str2
, id
);
3285 if (path
&& path
[0] != '\0') {
3287 err
= got_object_id_by_path(&obj_id2
, repo
, commit
, path
);
3291 err
= got_object_id_by_path(&obj_id1
, repo
,
3294 if (err
->code
!= GOT_ERR_NO_TREE_ENTRY
) {
3300 err
= got_object_get_type(&obj_type
, repo
, obj_id2
);
3306 "diff %s %s\n", id_str1
? id_str1
: "/dev/null", id_str2
);
3307 fprintf(outfile
, "commit - %s\n",
3308 id_str1
? id_str1
: "/dev/null");
3309 fprintf(outfile
, "commit + %s\n", id_str2
);
3311 case GOT_OBJ_TYPE_BLOB
:
3312 err
= diff_blobs(obj_id1
, obj_id2
, path
, diff_context
,
3313 0, 0, dsa
, repo
, outfile
);
3315 case GOT_OBJ_TYPE_TREE
:
3316 err
= diff_trees(obj_id1
, obj_id2
, path
, diff_context
,
3317 0, 0, dsa
, repo
, outfile
);
3320 err
= got_error(GOT_ERR_OBJ_TYPE
);
3326 obj_id2
= got_object_commit_get_tree_id(commit
);
3328 obj_id1
= got_object_commit_get_tree_id(pcommit
);
3330 "diff %s %s\n", id_str1
? id_str1
: "/dev/null", id_str2
);
3331 fprintf(outfile
, "commit - %s\n",
3332 id_str1
? id_str1
: "/dev/null");
3333 fprintf(outfile
, "commit + %s\n", id_str2
);
3334 err
= diff_trees(obj_id1
, obj_id2
, "", diff_context
, 0, 0,
3335 dsa
, repo
, outfile
);
3341 got_object_commit_close(pcommit
);
3346 get_datestr(time_t *time
, char *datebuf
)
3348 struct tm mytm
, *tm
;
3351 tm
= gmtime_r(time
, &mytm
);
3354 s
= asctime_r(tm
, datebuf
);
3357 p
= strchr(s
, '\n');
3363 static const struct got_error
*
3364 match_commit(int *have_match
, struct got_object_id
*id
,
3365 struct got_commit_object
*commit
, regex_t
*regex
)
3367 const struct got_error
*err
= NULL
;
3368 regmatch_t regmatch
;
3369 char *id_str
= NULL
, *logmsg
= NULL
;
3373 err
= got_object_id_str(&id_str
, id
);
3377 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
3381 if (regexec(regex
, got_object_commit_get_author(commit
), 1,
3382 ®match
, 0) == 0 ||
3383 regexec(regex
, got_object_commit_get_committer(commit
), 1,
3384 ®match
, 0) == 0 ||
3385 regexec(regex
, id_str
, 1, ®match
, 0) == 0 ||
3386 regexec(regex
, logmsg
, 1, ®match
, 0) == 0)
3395 match_changed_paths(int *have_match
, struct got_pathlist_head
*changed_paths
,
3398 regmatch_t regmatch
;
3399 struct got_pathlist_entry
*pe
;
3403 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
3404 if (regexec(regex
, pe
->path
, 1, ®match
, 0) == 0) {
3411 static const struct got_error
*
3412 match_patch(int *have_match
, struct got_commit_object
*commit
,
3413 struct got_object_id
*id
, const char *path
, int diff_context
,
3414 struct got_repository
*repo
, regex_t
*regex
, FILE *f
)
3416 const struct got_error
*err
= NULL
;
3418 size_t linesize
= 0;
3419 regmatch_t regmatch
;
3423 err
= got_opentemp_truncate(f
);
3427 err
= print_patch(commit
, id
, path
, diff_context
, NULL
, repo
, f
);
3431 if (fseeko(f
, 0L, SEEK_SET
) == -1) {
3432 err
= got_error_from_errno("fseeko");
3436 while (getline(&line
, &linesize
, f
) != -1) {
3437 if (regexec(regex
, line
, 1, ®match
, 0) == 0) {
3447 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3449 static const struct got_error
*
3450 build_refs_str(char **refs_str
, struct got_reflist_head
*refs
,
3451 struct got_object_id
*id
, struct got_repository
*repo
,
3454 static const struct got_error
*err
= NULL
;
3455 struct got_reflist_entry
*re
;
3461 TAILQ_FOREACH(re
, refs
, entry
) {
3462 struct got_tag_object
*tag
= NULL
;
3463 struct got_object_id
*ref_id
;
3466 name
= got_ref_get_name(re
->ref
);
3467 if (strcmp(name
, GOT_REF_HEAD
) == 0)
3469 if (strncmp(name
, "refs/", 5) == 0)
3471 if (strncmp(name
, "got/", 4) == 0)
3473 if (strncmp(name
, "heads/", 6) == 0)
3475 if (strncmp(name
, "remotes/", 8) == 0) {
3479 s
= strstr(name
, "/" GOT_REF_HEAD
);
3480 if (s
!= NULL
&& strcmp(s
, "/" GOT_REF_HEAD
) == 0)
3483 err
= got_ref_resolve(&ref_id
, repo
, re
->ref
);
3486 if (strncmp(name
, "tags/", 5) == 0) {
3487 err
= got_object_open_as_tag(&tag
, repo
, ref_id
);
3489 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
3493 /* Ref points at something other than a tag. */
3498 cmp
= got_object_id_cmp(tag
?
3499 got_object_tag_get_object_id(tag
) : ref_id
, id
);
3502 got_object_tag_close(tag
);
3506 if (asprintf(refs_str
, "%s%s%s", s
? s
: "",
3507 s
? ", " : "", name
) == -1) {
3508 err
= got_error_from_errno("asprintf");
3519 static const struct got_error
*
3520 print_commit_oneline(struct got_commit_object
*commit
, struct got_object_id
*id
,
3521 struct got_repository
*repo
, struct got_reflist_object_id_map
*refs_idmap
)
3523 const struct got_error
*err
= NULL
;
3524 char *ref_str
= NULL
, *id_str
= NULL
, *logmsg0
= NULL
;
3525 char *comma
, *s
, *nl
;
3526 struct got_reflist_head
*refs
;
3527 char datebuf
[12]; /* YYYY-MM-DD + SPACE + NUL */
3529 time_t committer_time
;
3531 refs
= got_reflist_object_id_map_lookup(refs_idmap
, id
);
3533 err
= build_refs_str(&ref_str
, refs
, id
, repo
, 1);
3537 /* Display the first matching ref only. */
3538 if (ref_str
&& (comma
= strchr(ref_str
, ',')) != NULL
)
3542 if (ref_str
== NULL
) {
3543 err
= got_object_id_str(&id_str
, id
);
3548 committer_time
= got_object_commit_get_committer_time(commit
);
3549 if (gmtime_r(&committer_time
, &tm
) == NULL
) {
3550 err
= got_error_from_errno("gmtime_r");
3553 if (strftime(datebuf
, sizeof(datebuf
), "%F ", &tm
) == 0) {
3554 err
= got_error(GOT_ERR_NO_SPACE
);
3558 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
3563 while (isspace((unsigned char)s
[0]))
3566 nl
= strchr(s
, '\n');
3572 printf("%s%-7s %s\n", datebuf
, ref_str
, s
);
3574 printf("%s%.7s %s\n", datebuf
, id_str
, s
);
3576 if (fflush(stdout
) != 0 && err
== NULL
)
3577 err
= got_error_from_errno("fflush");
3585 static const struct got_error
*
3586 print_diffstat(struct got_diffstat_cb_arg
*dsa
, const char *header
)
3588 struct got_pathlist_entry
*pe
;
3591 printf("%s\n", header
);
3593 TAILQ_FOREACH(pe
, dsa
->paths
, entry
) {
3594 struct got_diff_changed_path
*cp
= pe
->data
;
3595 int pad
= dsa
->max_path_len
- pe
->path_len
+ 1;
3597 printf(" %c %s%*c | %*d+ %*d-\n", cp
->status
, pe
->path
, pad
,
3598 ' ', dsa
->add_cols
+ 1, cp
->add
, dsa
->rm_cols
+ 1, cp
->rm
);
3600 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
3601 dsa
->nfiles
, dsa
->nfiles
> 1 ? "s" : "", dsa
->ins
,
3602 dsa
->ins
!= 1 ? "s" : "", dsa
->del
, dsa
->del
!= 1 ? "s" : "");
3604 if (fflush(stdout
) != 0)
3605 return got_error_from_errno("fflush");
3610 static const struct got_error
*
3616 if (fseeko(f
, 0L, SEEK_SET
) == -1)
3617 return got_error_from_errno("fseek");
3620 r
= fread(buf
, 1, sizeof(buf
), f
);
3623 return got_error_from_errno("fread");
3627 if (fwrite(buf
, 1, r
, stdout
) != r
)
3628 return got_ferror(stdout
, GOT_ERR_IO
);
3634 static const struct got_error
*
3635 print_commit(struct got_commit_object
*commit
, struct got_object_id
*id
,
3636 struct got_repository
*repo
, const char *path
,
3637 struct got_pathlist_head
*changed_paths
,
3638 struct got_diffstat_cb_arg
*diffstat
, int show_patch
, int diff_context
,
3639 struct got_reflist_object_id_map
*refs_idmap
, const char *custom_refs_str
,
3642 const struct got_error
*err
= NULL
;
3644 char *id_str
, *datestr
, *logmsg0
, *logmsg
, *line
;
3646 time_t committer_time
;
3647 const char *author
, *committer
;
3648 char *refs_str
= NULL
;
3650 err
= got_object_id_str(&id_str
, id
);
3654 if (custom_refs_str
== NULL
) {
3655 struct got_reflist_head
*refs
;
3656 refs
= got_reflist_object_id_map_lookup(refs_idmap
, id
);
3658 err
= build_refs_str(&refs_str
, refs
, id
, repo
, 0);
3664 printf(GOT_COMMIT_SEP_STR
);
3665 if (custom_refs_str
)
3666 printf("%s %s (%s)\n", prefix
? prefix
: "commit", id_str
,
3669 printf("%s %s%s%s%s\n", prefix
? prefix
: "commit", id_str
,
3670 refs_str
? " (" : "", refs_str
? refs_str
: "",
3671 refs_str
? ")" : "");
3676 printf("from: %s\n", got_object_commit_get_author(commit
));
3677 author
= got_object_commit_get_author(commit
);
3678 committer
= got_object_commit_get_committer(commit
);
3679 if (strcmp(author
, committer
) != 0)
3680 printf("via: %s\n", committer
);
3681 committer_time
= got_object_commit_get_committer_time(commit
);
3682 datestr
= get_datestr(&committer_time
, datebuf
);
3684 printf("date: %s UTC\n", datestr
);
3685 if (got_object_commit_get_nparents(commit
) > 1) {
3686 const struct got_object_id_queue
*parent_ids
;
3687 struct got_object_qid
*qid
;
3689 parent_ids
= got_object_commit_get_parent_ids(commit
);
3690 STAILQ_FOREACH(qid
, parent_ids
, entry
) {
3691 err
= got_object_id_str(&id_str
, &qid
->id
);
3694 printf("parent %d: %s\n", n
++, id_str
);
3700 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
3706 line
= strsep(&logmsg
, "\n");
3708 printf(" %s\n", line
);
3712 if (changed_paths
&& diffstat
== NULL
) {
3713 struct got_pathlist_entry
*pe
;
3715 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
3716 struct got_diff_changed_path
*cp
= pe
->data
;
3718 printf(" %c %s\n", cp
->status
, pe
->path
);
3726 err
= got_error_from_errno("got_opentemp");
3731 err
= print_patch(commit
, id
, path
, diff_context
, diffstat
,
3732 repo
, diffstat
== NULL
? stdout
: f
);
3737 err
= print_diffstat(diffstat
, NULL
);
3749 if (fflush(stdout
) != 0 && err
== NULL
)
3750 err
= got_error_from_errno("fflush");
3752 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
3753 err
= got_error_from_errno("fclose");
3759 static const struct got_error
*
3760 print_commits(struct got_object_id
*root_id
, struct got_object_id
*end_id
,
3761 struct got_repository
*repo
, const char *path
, int show_changed_paths
,
3762 int show_diffstat
, int show_patch
, const char *search_pattern
,
3763 int diff_context
, int limit
, int log_branches
, int reverse_display_order
,
3764 struct got_reflist_object_id_map
*refs_idmap
, int one_line
,
3767 const struct got_error
*err
;
3768 struct got_commit_graph
*graph
;
3771 struct got_object_id_queue reversed_commits
;
3772 struct got_object_qid
*qid
;
3773 struct got_commit_object
*commit
;
3774 struct got_pathlist_head changed_paths
;
3776 STAILQ_INIT(&reversed_commits
);
3777 TAILQ_INIT(&changed_paths
);
3779 if (search_pattern
&& regcomp(®ex
, search_pattern
,
3780 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
))
3781 return got_error_msg(GOT_ERR_REGEX
, search_pattern
);
3783 err
= got_commit_graph_open(&graph
, path
, !log_branches
);
3786 err
= got_commit_graph_bfsort(graph
, root_id
, repo
,
3787 check_cancelled
, NULL
);
3791 struct got_object_id id
;
3792 struct got_diffstat_cb_arg dsa
= { 0, 0, 0, 0, 0, 0,
3793 &changed_paths
, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE
};
3795 if (sigint_received
|| sigpipe_received
)
3798 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
3799 check_cancelled
, NULL
);
3801 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
3806 err
= got_object_open_as_commit(&commit
, repo
, &id
);
3810 if ((show_changed_paths
|| (show_diffstat
&& !show_patch
))
3811 && !reverse_display_order
) {
3812 err
= get_changed_paths(&changed_paths
, commit
, repo
,
3813 show_diffstat
? &dsa
: NULL
);
3818 if (search_pattern
) {
3819 err
= match_commit(&have_match
, &id
, commit
, ®ex
);
3821 got_object_commit_close(commit
);
3824 if (have_match
== 0 && show_changed_paths
)
3825 match_changed_paths(&have_match
,
3826 &changed_paths
, ®ex
);
3827 if (have_match
== 0 && show_patch
) {
3828 err
= match_patch(&have_match
, commit
, &id
,
3829 path
, diff_context
, repo
, ®ex
, tmpfile
);
3833 if (have_match
== 0) {
3834 got_object_commit_close(commit
);
3835 got_pathlist_free(&changed_paths
,
3836 GOT_PATHLIST_FREE_ALL
);
3841 if (reverse_display_order
) {
3842 err
= got_object_qid_alloc(&qid
, &id
);
3845 STAILQ_INSERT_HEAD(&reversed_commits
, qid
, entry
);
3846 got_object_commit_close(commit
);
3849 err
= print_commit_oneline(commit
, &id
,
3852 err
= print_commit(commit
, &id
, repo
, path
,
3853 (show_changed_paths
|| show_diffstat
) ?
3854 &changed_paths
: NULL
,
3855 show_diffstat
? &dsa
: NULL
, show_patch
,
3856 diff_context
, refs_idmap
, NULL
, NULL
);
3857 got_object_commit_close(commit
);
3861 if ((limit
&& --limit
== 0) ||
3862 (end_id
&& got_object_id_cmp(&id
, end_id
) == 0))
3865 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
3867 if (reverse_display_order
) {
3868 STAILQ_FOREACH(qid
, &reversed_commits
, entry
) {
3869 struct got_diffstat_cb_arg dsa
= { 0, 0, 0, 0, 0, 0,
3870 &changed_paths
, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE
};
3872 err
= got_object_open_as_commit(&commit
, repo
,
3876 if (show_changed_paths
||
3877 (show_diffstat
&& !show_patch
)) {
3878 err
= get_changed_paths(&changed_paths
, commit
,
3879 repo
, show_diffstat
? &dsa
: NULL
);
3884 err
= print_commit_oneline(commit
, &qid
->id
,
3887 err
= print_commit(commit
, &qid
->id
, repo
, path
,
3888 (show_changed_paths
|| show_diffstat
) ?
3889 &changed_paths
: NULL
,
3890 show_diffstat
? &dsa
: NULL
, show_patch
,
3891 diff_context
, refs_idmap
, NULL
, NULL
);
3892 got_object_commit_close(commit
);
3895 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
3899 while (!STAILQ_EMPTY(&reversed_commits
)) {
3900 qid
= STAILQ_FIRST(&reversed_commits
);
3901 STAILQ_REMOVE_HEAD(&reversed_commits
, entry
);
3902 got_object_qid_free(qid
);
3904 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
3907 got_commit_graph_close(graph
);
3914 fprintf(stderr
, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
3915 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
3916 "[path]\n", getprogname());
3921 get_default_log_limit(void)
3923 const char *got_default_log_limit
;
3927 got_default_log_limit
= getenv("GOT_LOG_DEFAULT_LIMIT");
3928 if (got_default_log_limit
== NULL
)
3930 n
= strtonum(got_default_log_limit
, 0, INT_MAX
, &errstr
);
3936 static const struct got_error
*
3937 cmd_log(int argc
, char *argv
[])
3939 const struct got_error
*error
;
3940 struct got_repository
*repo
= NULL
;
3941 struct got_worktree
*worktree
= NULL
;
3942 struct got_object_id
*start_id
= NULL
, *end_id
= NULL
;
3943 char *repo_path
= NULL
, *path
= NULL
, *cwd
= NULL
, *in_repo_path
= NULL
;
3944 const char *start_commit
= NULL
, *end_commit
= NULL
;
3945 const char *search_pattern
= NULL
;
3946 int diff_context
= -1, ch
;
3947 int show_changed_paths
= 0, show_patch
= 0, limit
= 0, log_branches
= 0;
3948 int show_diffstat
= 0, reverse_display_order
= 0, one_line
= 0;
3950 struct got_reflist_head refs
;
3951 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
3952 FILE *tmpfile
= NULL
;
3953 int *pack_fds
= NULL
;
3958 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3964 limit
= get_default_log_limit();
3966 while ((ch
= getopt(argc
, argv
, "bC:c:dl:PpRr:S:sx:")) != -1) {
3972 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
3975 errx(1, "number of context lines is %s: %s",
3979 start_commit
= optarg
;
3985 limit
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
3987 errx(1, "number of commits is %s: %s",
3991 show_changed_paths
= 1;
3997 reverse_display_order
= 1;
4000 repo_path
= realpath(optarg
, NULL
);
4001 if (repo_path
== NULL
)
4002 return got_error_from_errno2("realpath",
4004 got_path_strip_trailing_slashes(repo_path
);
4007 search_pattern
= optarg
;
4013 end_commit
= optarg
;
4024 if (diff_context
== -1)
4026 else if (!show_patch
)
4027 errx(1, "-C requires -p");
4029 if (one_line
&& (show_patch
|| show_changed_paths
|| show_diffstat
))
4030 errx(1, "cannot use -s with -d, -p or -P");
4032 cwd
= getcwd(NULL
, 0);
4034 error
= got_error_from_errno("getcwd");
4038 error
= got_repo_pack_fds_open(&pack_fds
);
4042 if (repo_path
== NULL
) {
4043 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
4044 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4051 error
= got_worktree_resolve_path(&path
, worktree
,
4056 path
= strdup(argv
[0]);
4058 error
= got_error_from_errno("strdup");
4062 } else if (argc
!= 0)
4065 if (repo_path
== NULL
) {
4066 repo_path
= worktree
?
4067 strdup(got_worktree_get_repo_path(worktree
)) : strdup(cwd
);
4069 if (repo_path
== NULL
) {
4070 error
= got_error_from_errno("strdup");
4074 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
4078 error
= apply_unveil(got_repo_get_path(repo
), 1,
4079 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4083 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
4087 error
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
4091 if (start_commit
== NULL
) {
4092 struct got_reference
*head_ref
;
4093 struct got_commit_object
*commit
= NULL
;
4094 error
= got_ref_open(&head_ref
, repo
,
4095 worktree
? got_worktree_get_head_ref_name(worktree
)
4099 error
= got_ref_resolve(&start_id
, repo
, head_ref
);
4100 got_ref_close(head_ref
);
4103 error
= got_object_open_as_commit(&commit
, repo
,
4107 got_object_commit_close(commit
);
4109 error
= got_repo_match_object_id(&start_id
, NULL
,
4110 start_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4114 if (end_commit
!= NULL
) {
4115 error
= got_repo_match_object_id(&end_id
, NULL
,
4116 end_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4123 * If a path was specified on the command line it was resolved
4124 * to a path in the work tree above. Prepend the work tree's
4125 * path prefix to obtain the corresponding in-repository path.
4129 prefix
= got_worktree_get_path_prefix(worktree
);
4130 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
4131 (path
[0] != '\0') ? "/" : "", path
) == -1) {
4132 error
= got_error_from_errno("asprintf");
4137 error
= got_repo_map_path(&in_repo_path
, repo
,
4143 path
= in_repo_path
;
4147 /* Release work tree lock. */
4148 got_worktree_close(worktree
);
4152 if (search_pattern
&& show_patch
) {
4153 tmpfile
= got_opentemp();
4154 if (tmpfile
== NULL
) {
4155 error
= got_error_from_errno("got_opentemp");
4160 error
= print_commits(start_id
, end_id
, repo
, path
? path
: "",
4161 show_changed_paths
, show_diffstat
, show_patch
, search_pattern
,
4162 diff_context
, limit
, log_branches
, reverse_display_order
,
4163 refs_idmap
, one_line
, tmpfile
);
4171 got_worktree_close(worktree
);
4173 const struct got_error
*close_err
= got_repo_close(repo
);
4178 const struct got_error
*pack_err
=
4179 got_repo_pack_fds_close(pack_fds
);
4184 got_reflist_object_id_map_free(refs_idmap
);
4185 if (tmpfile
&& fclose(tmpfile
) == EOF
&& error
== NULL
)
4186 error
= got_error_from_errno("fclose");
4187 got_ref_list_free(&refs
);
4194 fprintf(stderr
, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4195 "[-r repository-path] [object1 object2 | path ...]\n",
4200 struct print_diff_arg
{
4201 struct got_repository
*repo
;
4202 struct got_worktree
*worktree
;
4203 struct got_diffstat_cb_arg
*diffstat
;
4208 enum got_diff_algorithm diff_algo
;
4209 int ignore_whitespace
;
4210 int force_text_diff
;
4217 * Create a file which contains the target path of a symlink so we can feed
4218 * it as content to the diff engine.
4220 static const struct got_error
*
4221 get_symlink_target_file(int *fd
, int dirfd
, const char *de_name
,
4222 const char *abspath
)
4224 const struct got_error
*err
= NULL
;
4225 char target_path
[PATH_MAX
];
4226 ssize_t target_len
, outlen
;
4231 target_len
= readlinkat(dirfd
, de_name
, target_path
, PATH_MAX
);
4232 if (target_len
== -1)
4233 return got_error_from_errno2("readlinkat", abspath
);
4235 target_len
= readlink(abspath
, target_path
, PATH_MAX
);
4236 if (target_len
== -1)
4237 return got_error_from_errno2("readlink", abspath
);
4240 *fd
= got_opentempfd();
4242 return got_error_from_errno("got_opentempfd");
4244 outlen
= write(*fd
, target_path
, target_len
);
4246 err
= got_error_from_errno("got_opentempfd");
4250 if (lseek(*fd
, 0, SEEK_SET
) == -1) {
4251 err
= got_error_from_errno2("lseek", abspath
);
4262 static const struct got_error
*
4263 print_diff(void *arg
, unsigned char status
, unsigned char staged_status
,
4264 const char *path
, struct got_object_id
*blob_id
,
4265 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
4266 int dirfd
, const char *de_name
)
4268 struct print_diff_arg
*a
= arg
;
4269 const struct got_error
*err
= NULL
;
4270 struct got_blob_object
*blob1
= NULL
;
4271 int fd
= -1, fd1
= -1, fd2
= -1;
4273 char *abspath
= NULL
, *label1
= NULL
;
4278 memset(&sb
, 0, sizeof(sb
));
4280 if (a
->diff_staged
) {
4281 if (staged_status
!= GOT_STATUS_MODIFY
&&
4282 staged_status
!= GOT_STATUS_ADD
&&
4283 staged_status
!= GOT_STATUS_DELETE
)
4286 if (staged_status
== GOT_STATUS_DELETE
)
4288 if (status
== GOT_STATUS_NONEXISTENT
)
4289 return got_error_set_errno(ENOENT
, path
);
4290 if (status
!= GOT_STATUS_MODIFY
&&
4291 status
!= GOT_STATUS_ADD
&&
4292 status
!= GOT_STATUS_DELETE
&&
4293 status
!= GOT_STATUS_CONFLICT
)
4297 err
= got_opentemp_truncate(a
->f1
);
4299 return got_error_from_errno("got_opentemp_truncate");
4300 err
= got_opentemp_truncate(a
->f2
);
4302 return got_error_from_errno("got_opentemp_truncate");
4304 if (!a
->header_shown
) {
4305 if (fprintf(a
->outfile
, "diff %s%s\n",
4306 a
->diff_staged
? "-s " : "",
4307 got_worktree_get_root_path(a
->worktree
)) < 0) {
4308 err
= got_error_from_errno("fprintf");
4311 if (fprintf(a
->outfile
, "commit - %s\n", a
->id_str
) < 0) {
4312 err
= got_error_from_errno("fprintf");
4315 if (fprintf(a
->outfile
, "path + %s%s\n",
4316 got_worktree_get_root_path(a
->worktree
),
4317 a
->diff_staged
? " (staged changes)" : "") < 0) {
4318 err
= got_error_from_errno("fprintf");
4321 a
->header_shown
= 1;
4324 if (a
->diff_staged
) {
4325 const char *label1
= NULL
, *label2
= NULL
;
4326 switch (staged_status
) {
4327 case GOT_STATUS_MODIFY
:
4331 case GOT_STATUS_ADD
:
4334 case GOT_STATUS_DELETE
:
4338 return got_error(GOT_ERR_FILE_STATUS
);
4340 fd1
= got_opentempfd();
4342 err
= got_error_from_errno("got_opentempfd");
4345 fd2
= got_opentempfd();
4347 err
= got_error_from_errno("got_opentempfd");
4350 err
= got_diff_objects_as_blobs(NULL
, NULL
, a
->f1
, a
->f2
,
4351 fd1
, fd2
, blob_id
, staged_blob_id
, label1
, label2
,
4352 a
->diff_algo
, a
->diff_context
, a
->ignore_whitespace
,
4353 a
->force_text_diff
, a
->diffstat
, a
->repo
, a
->outfile
);
4357 fd1
= got_opentempfd();
4359 err
= got_error_from_errno("got_opentempfd");
4363 if (staged_status
== GOT_STATUS_ADD
||
4364 staged_status
== GOT_STATUS_MODIFY
) {
4366 err
= got_object_open_as_blob(&blob1
, a
->repo
, staged_blob_id
,
4370 err
= got_object_id_str(&id_str
, staged_blob_id
);
4373 if (asprintf(&label1
, "%s (staged)", id_str
) == -1) {
4374 err
= got_error_from_errno("asprintf");
4379 } else if (status
!= GOT_STATUS_ADD
) {
4380 err
= got_object_open_as_blob(&blob1
, a
->repo
, blob_id
, 8192,
4386 if (status
!= GOT_STATUS_DELETE
) {
4387 if (asprintf(&abspath
, "%s/%s",
4388 got_worktree_get_root_path(a
->worktree
), path
) == -1) {
4389 err
= got_error_from_errno("asprintf");
4394 fd
= openat(dirfd
, de_name
,
4395 O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
4397 if (!got_err_open_nofollow_on_symlink()) {
4398 err
= got_error_from_errno2("openat",
4402 err
= get_symlink_target_file(&fd
, dirfd
,
4408 fd
= open(abspath
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
4410 if (!got_err_open_nofollow_on_symlink()) {
4411 err
= got_error_from_errno2("open",
4415 err
= get_symlink_target_file(&fd
, dirfd
,
4421 if (fstatat(fd
, abspath
, &sb
, AT_SYMLINK_NOFOLLOW
) == -1) {
4422 err
= got_error_from_errno2("fstatat", abspath
);
4425 f2
= fdopen(fd
, "r");
4427 err
= got_error_from_errno2("fdopen", abspath
);
4435 err
= got_object_blob_dump_to_file(&size1
, NULL
, NULL
,
4441 err
= got_diff_blob_file(blob1
, a
->f1
, size1
, label1
, f2
? f2
: a
->f2
,
4442 f2_exists
, &sb
, path
, GOT_DIFF_ALGORITHM_PATIENCE
, a
->diff_context
,
4443 a
->ignore_whitespace
, a
->force_text_diff
, a
->diffstat
, a
->outfile
);
4445 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
4446 err
= got_error_from_errno("close");
4447 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
4448 err
= got_error_from_errno("close");
4450 got_object_blob_close(blob1
);
4451 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
4452 err
= got_error_from_errno("close");
4453 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
4454 err
= got_error_from_errno("fclose");
4459 static const struct got_error
*
4460 cmd_diff(int argc
, char *argv
[])
4462 const struct got_error
*error
;
4463 struct got_repository
*repo
= NULL
;
4464 struct got_worktree
*worktree
= NULL
;
4465 char *cwd
= NULL
, *repo_path
= NULL
;
4466 const char *commit_args
[2] = { NULL
, NULL
};
4467 int ncommit_args
= 0;
4468 struct got_object_id
*ids
[2] = { NULL
, NULL
};
4469 char *labels
[2] = { NULL
, NULL
};
4470 int type1
= GOT_OBJ_TYPE_ANY
, type2
= GOT_OBJ_TYPE_ANY
;
4471 int diff_context
= 3, diff_staged
= 0, ignore_whitespace
= 0, ch
, i
;
4472 int force_text_diff
= 0, force_path
= 0, rflag
= 0, show_diffstat
= 0;
4474 struct got_reflist_head refs
;
4475 struct got_pathlist_head diffstat_paths
, paths
;
4476 FILE *f1
= NULL
, *f2
= NULL
, *outfile
= NULL
;
4477 int fd1
= -1, fd2
= -1;
4478 int *pack_fds
= NULL
;
4479 struct got_diffstat_cb_arg dsa
;
4481 memset(&dsa
, 0, sizeof(dsa
));
4485 TAILQ_INIT(&diffstat_paths
);
4488 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4493 while ((ch
= getopt(argc
, argv
, "aC:c:dPr:sw")) != -1) {
4496 force_text_diff
= 1;
4499 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
4502 errx(1, "number of context lines is %s: %s",
4506 if (ncommit_args
>= 2)
4507 errx(1, "too many -c options used");
4508 commit_args
[ncommit_args
++] = optarg
;
4517 repo_path
= realpath(optarg
, NULL
);
4518 if (repo_path
== NULL
)
4519 return got_error_from_errno2("realpath",
4521 got_path_strip_trailing_slashes(repo_path
);
4528 ignore_whitespace
= 1;
4539 cwd
= getcwd(NULL
, 0);
4541 error
= got_error_from_errno("getcwd");
4545 error
= got_repo_pack_fds_open(&pack_fds
);
4549 if (repo_path
== NULL
) {
4550 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
4551 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4557 strdup(got_worktree_get_repo_path(worktree
));
4558 if (repo_path
== NULL
) {
4559 error
= got_error_from_errno("strdup");
4563 repo_path
= strdup(cwd
);
4564 if (repo_path
== NULL
) {
4565 error
= got_error_from_errno("strdup");
4571 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
4576 if (show_diffstat
) {
4577 dsa
.paths
= &diffstat_paths
;
4578 dsa
.force_text
= force_text_diff
;
4579 dsa
.ignore_ws
= ignore_whitespace
;
4580 dsa
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
4583 if (rflag
|| worktree
== NULL
|| ncommit_args
> 0) {
4585 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
4586 "-P option can only be used when diffing "
4591 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
4592 "-s option can only be used when diffing "
4598 error
= apply_unveil(got_repo_get_path(repo
), 1,
4599 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4603 if ((!force_path
&& argc
== 2) || ncommit_args
> 0) {
4604 int obj_type
= (ncommit_args
> 0 ?
4605 GOT_OBJ_TYPE_COMMIT
: GOT_OBJ_TYPE_ANY
);
4606 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
4610 for (i
= 0; i
< (ncommit_args
> 0 ? ncommit_args
: argc
); i
++) {
4612 if (ncommit_args
> 0)
4613 arg
= commit_args
[i
];
4616 error
= got_repo_match_object_id(&ids
[i
], &labels
[i
],
4617 arg
, obj_type
, &refs
, repo
);
4619 if (error
->code
!= GOT_ERR_NOT_REF
&&
4620 error
->code
!= GOT_ERR_NO_OBJ
)
4622 if (ncommit_args
> 0)
4630 f1
= got_opentemp();
4632 error
= got_error_from_errno("got_opentemp");
4636 f2
= got_opentemp();
4638 error
= got_error_from_errno("got_opentemp");
4642 outfile
= got_opentemp();
4643 if (outfile
== NULL
) {
4644 error
= got_error_from_errno("got_opentemp");
4648 if (ncommit_args
== 0 && (ids
[0] == NULL
|| ids
[1] == NULL
)) {
4649 struct print_diff_arg arg
;
4652 if (worktree
== NULL
) {
4653 if (argc
== 2 && ids
[0] == NULL
) {
4654 error
= got_error_path(argv
[0], GOT_ERR_NO_OBJ
);
4656 } else if (argc
== 2 && ids
[1] == NULL
) {
4657 error
= got_error_path(argv
[1], GOT_ERR_NO_OBJ
);
4659 } else if (argc
> 0) {
4660 error
= got_error_fmt(GOT_ERR_NOT_WORKTREE
,
4661 "%s", "specified paths cannot be resolved");
4664 error
= got_error(GOT_ERR_NOT_WORKTREE
);
4669 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
4674 error
= got_object_id_str(&id_str
,
4675 got_worktree_get_base_commit_id(worktree
));
4679 arg
.worktree
= worktree
;
4680 arg
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
4681 arg
.diff_context
= diff_context
;
4682 arg
.id_str
= id_str
;
4683 arg
.header_shown
= 0;
4684 arg
.diff_staged
= diff_staged
;
4685 arg
.ignore_whitespace
= ignore_whitespace
;
4686 arg
.force_text_diff
= force_text_diff
;
4687 arg
.diffstat
= show_diffstat
? &dsa
: NULL
;
4690 arg
.outfile
= outfile
;
4692 error
= got_worktree_status(worktree
, &paths
, repo
, 0,
4693 print_diff
, &arg
, check_cancelled
, NULL
);
4698 if (show_diffstat
&& dsa
.nfiles
> 0) {
4701 if (asprintf(&header
, "diffstat %s%s",
4702 diff_staged
? "-s " : "",
4703 got_worktree_get_root_path(worktree
)) == -1) {
4704 error
= got_error_from_errno("asprintf");
4708 error
= print_diffstat(&dsa
, header
);
4714 error
= printfile(outfile
);
4718 if (ncommit_args
== 1) {
4719 struct got_commit_object
*commit
;
4720 error
= got_object_open_as_commit(&commit
, repo
, ids
[0]);
4724 labels
[1] = labels
[0];
4726 if (got_object_commit_get_nparents(commit
) > 0) {
4727 const struct got_object_id_queue
*pids
;
4728 struct got_object_qid
*pid
;
4729 pids
= got_object_commit_get_parent_ids(commit
);
4730 pid
= STAILQ_FIRST(pids
);
4731 ids
[0] = got_object_id_dup(&pid
->id
);
4732 if (ids
[0] == NULL
) {
4733 error
= got_error_from_errno(
4734 "got_object_id_dup");
4735 got_object_commit_close(commit
);
4738 error
= got_object_id_str(&labels
[0], ids
[0]);
4740 got_object_commit_close(commit
);
4745 labels
[0] = strdup("/dev/null");
4746 if (labels
[0] == NULL
) {
4747 error
= got_error_from_errno("strdup");
4748 got_object_commit_close(commit
);
4753 got_object_commit_close(commit
);
4756 if (ncommit_args
== 0 && argc
> 2) {
4757 error
= got_error_msg(GOT_ERR_BAD_PATH
,
4758 "path arguments cannot be used when diffing two objects");
4763 error
= got_object_get_type(&type1
, repo
, ids
[0]);
4768 error
= got_object_get_type(&type2
, repo
, ids
[1]);
4771 if (type1
!= GOT_OBJ_TYPE_ANY
&& type1
!= type2
) {
4772 error
= got_error(GOT_ERR_OBJ_TYPE
);
4775 if (type1
== GOT_OBJ_TYPE_BLOB
&& argc
> 2) {
4776 error
= got_error_msg(GOT_ERR_OBJ_TYPE
,
4777 "path arguments cannot be used when diffing blobs");
4781 for (i
= 0; ncommit_args
> 0 && i
< argc
; i
++) {
4783 struct got_pathlist_entry
*new;
4787 error
= got_worktree_resolve_path(&p
, worktree
,
4791 prefix
= got_worktree_get_path_prefix(worktree
);
4792 while (prefix
[0] == '/')
4794 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
4795 (p
[0] != '\0' && prefix
[0] != '\0') ? "/" : "",
4797 error
= got_error_from_errno("asprintf");
4803 char *mapped_path
, *s
;
4804 error
= got_repo_map_path(&mapped_path
, repo
, argv
[i
]);
4810 in_repo_path
= strdup(s
);
4811 if (in_repo_path
== NULL
) {
4812 error
= got_error_from_errno("asprintf");
4819 error
= got_pathlist_insert(&new, &paths
, in_repo_path
, NULL
);
4820 if (error
|| new == NULL
/* duplicate */)
4827 /* Release work tree lock. */
4828 got_worktree_close(worktree
);
4832 fd1
= got_opentempfd();
4834 error
= got_error_from_errno("got_opentempfd");
4838 fd2
= got_opentempfd();
4840 error
= got_error_from_errno("got_opentempfd");
4844 switch (type1
== GOT_OBJ_TYPE_ANY
? type2
: type1
) {
4845 case GOT_OBJ_TYPE_BLOB
:
4846 error
= got_diff_objects_as_blobs(NULL
, NULL
, f1
, f2
,
4847 fd1
, fd2
, ids
[0], ids
[1], NULL
, NULL
,
4848 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
4849 ignore_whitespace
, force_text_diff
,
4850 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
4852 case GOT_OBJ_TYPE_TREE
:
4853 error
= got_diff_objects_as_trees(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
4854 ids
[0], ids
[1], &paths
, "", "",
4855 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
4856 ignore_whitespace
, force_text_diff
,
4857 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
4859 case GOT_OBJ_TYPE_COMMIT
:
4860 fprintf(outfile
, "diff %s %s\n", labels
[0], labels
[1]);
4861 error
= got_diff_objects_as_commits(NULL
, NULL
, f1
, f2
,
4862 fd1
, fd2
, ids
[0], ids
[1], &paths
,
4863 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
4864 ignore_whitespace
, force_text_diff
,
4865 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
4868 error
= got_error(GOT_ERR_OBJ_TYPE
);
4873 if (show_diffstat
&& dsa
.nfiles
> 0) {
4874 char *header
= NULL
;
4876 if (asprintf(&header
, "diffstat %s %s",
4877 labels
[0], labels
[1]) == -1) {
4878 error
= got_error_from_errno("asprintf");
4882 error
= print_diffstat(&dsa
, header
);
4888 error
= printfile(outfile
);
4896 got_worktree_close(worktree
);
4898 const struct got_error
*close_err
= got_repo_close(repo
);
4903 const struct got_error
*pack_err
=
4904 got_repo_pack_fds_close(pack_fds
);
4908 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
4909 got_pathlist_free(&diffstat_paths
, GOT_PATHLIST_FREE_ALL
);
4910 got_ref_list_free(&refs
);
4911 if (outfile
&& fclose(outfile
) == EOF
&& error
== NULL
)
4912 error
= got_error_from_errno("fclose");
4913 if (f1
&& fclose(f1
) == EOF
&& error
== NULL
)
4914 error
= got_error_from_errno("fclose");
4915 if (f2
&& fclose(f2
) == EOF
&& error
== NULL
)
4916 error
= got_error_from_errno("fclose");
4917 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
4918 error
= got_error_from_errno("close");
4919 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
4920 error
= got_error_from_errno("close");
4928 "usage: %s blame [-c commit] [-r repository-path] path\n",
4937 char datebuf
[11]; /* YYYY-MM-DD + NUL */
4940 struct blame_cb_args
{
4941 struct blame_line
*lines
;
4945 off_t
*line_offsets
;
4947 struct got_repository
*repo
;
4950 static const struct got_error
*
4951 blame_cb(void *arg
, int nlines
, int lineno
,
4952 struct got_commit_object
*commit
, struct got_object_id
*id
)
4954 const struct got_error
*err
= NULL
;
4955 struct blame_cb_args
*a
= arg
;
4956 struct blame_line
*bline
;
4958 size_t linesize
= 0;
4961 time_t committer_time
;
4963 if (nlines
!= a
->nlines
||
4964 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
4965 return got_error(GOT_ERR_RANGE
);
4967 if (sigint_received
)
4968 return got_error(GOT_ERR_ITER_COMPLETED
);
4971 return NULL
; /* no change in this commit */
4973 /* Annotate this line. */
4974 bline
= &a
->lines
[lineno
- 1];
4975 if (bline
->annotated
)
4977 err
= got_object_id_str(&bline
->id_str
, id
);
4981 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
4982 if (bline
->committer
== NULL
) {
4983 err
= got_error_from_errno("strdup");
4987 committer_time
= got_object_commit_get_committer_time(commit
);
4988 if (gmtime_r(&committer_time
, &tm
) == NULL
)
4989 return got_error_from_errno("gmtime_r");
4990 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%F", &tm
) == 0) {
4991 err
= got_error(GOT_ERR_NO_SPACE
);
4994 bline
->annotated
= 1;
4996 /* Print lines annotated so far. */
4997 bline
= &a
->lines
[a
->lineno_cur
- 1];
4998 if (!bline
->annotated
)
5001 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
5002 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
5003 err
= got_error_from_errno("fseeko");
5007 while (a
->lineno_cur
<= a
->nlines
&& bline
->annotated
) {
5008 char *smallerthan
, *at
, *nl
, *committer
;
5011 if (getline(&line
, &linesize
, a
->f
) == -1) {
5013 err
= got_error_from_errno("getline");
5017 committer
= bline
->committer
;
5018 smallerthan
= strchr(committer
, '<');
5019 if (smallerthan
&& smallerthan
[1] != '\0')
5020 committer
= smallerthan
+ 1;
5021 at
= strchr(committer
, '@');
5024 len
= strlen(committer
);
5026 committer
[8] = '\0';
5028 nl
= strchr(line
, '\n');
5031 printf("%.*d) %.8s %s %-8s %s\n", a
->nlines_prec
, a
->lineno_cur
,
5032 bline
->id_str
, bline
->datebuf
, committer
, line
);
5035 bline
= &a
->lines
[a
->lineno_cur
- 1];
5042 static const struct got_error
*
5043 cmd_blame(int argc
, char *argv
[])
5045 const struct got_error
*error
;
5046 struct got_repository
*repo
= NULL
;
5047 struct got_worktree
*worktree
= NULL
;
5048 char *path
, *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
5049 char *link_target
= NULL
;
5050 struct got_object_id
*obj_id
= NULL
;
5051 struct got_object_id
*commit_id
= NULL
;
5052 struct got_commit_object
*commit
= NULL
;
5053 struct got_blob_object
*blob
= NULL
;
5054 char *commit_id_str
= NULL
;
5055 struct blame_cb_args bca
;
5056 int ch
, obj_type
, i
, fd1
= -1, fd2
= -1, fd3
= -1;
5058 int *pack_fds
= NULL
;
5059 FILE *f1
= NULL
, *f2
= NULL
;
5061 fd1
= got_opentempfd();
5063 return got_error_from_errno("got_opentempfd");
5065 memset(&bca
, 0, sizeof(bca
));
5068 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5073 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
5076 commit_id_str
= optarg
;
5079 repo_path
= realpath(optarg
, NULL
);
5080 if (repo_path
== NULL
)
5081 return got_error_from_errno2("realpath",
5083 got_path_strip_trailing_slashes(repo_path
);
5099 cwd
= getcwd(NULL
, 0);
5101 error
= got_error_from_errno("getcwd");
5105 error
= got_repo_pack_fds_open(&pack_fds
);
5109 if (repo_path
== NULL
) {
5110 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
5111 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5117 strdup(got_worktree_get_repo_path(worktree
));
5118 if (repo_path
== NULL
) {
5119 error
= got_error_from_errno("strdup");
5124 repo_path
= strdup(cwd
);
5125 if (repo_path
== NULL
) {
5126 error
= got_error_from_errno("strdup");
5132 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
5137 const char *prefix
= got_worktree_get_path_prefix(worktree
);
5140 error
= got_worktree_resolve_path(&p
, worktree
, path
);
5143 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5144 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
5146 error
= got_error_from_errno("asprintf");
5151 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5153 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5156 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
5161 if (commit_id_str
== NULL
) {
5162 struct got_reference
*head_ref
;
5163 error
= got_ref_open(&head_ref
, repo
, worktree
?
5164 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
, 0);
5167 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
5168 got_ref_close(head_ref
);
5172 struct got_reflist_head refs
;
5174 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5178 error
= got_repo_match_object_id(&commit_id
, NULL
,
5179 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
5180 got_ref_list_free(&refs
);
5186 /* Release work tree lock. */
5187 got_worktree_close(worktree
);
5191 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
5195 error
= got_object_resolve_symlinks(&link_target
, in_repo_path
,
5200 error
= got_object_id_by_path(&obj_id
, repo
, commit
,
5201 link_target
? link_target
: in_repo_path
);
5205 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
5209 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
5210 error
= got_error_path(link_target
? link_target
: in_repo_path
,
5215 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, 8192, fd1
);
5218 bca
.f
= got_opentemp();
5219 if (bca
.f
== NULL
) {
5220 error
= got_error_from_errno("got_opentemp");
5223 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
5224 &bca
.line_offsets
, bca
.f
, blob
);
5225 if (error
|| bca
.nlines
== 0)
5228 /* Don't include \n at EOF in the blame line count. */
5229 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
5232 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
5233 if (bca
.lines
== NULL
) {
5234 error
= got_error_from_errno("calloc");
5238 bca
.nlines_prec
= 0;
5246 fd2
= got_opentempfd();
5248 error
= got_error_from_errno("got_opentempfd");
5251 fd3
= got_opentempfd();
5253 error
= got_error_from_errno("got_opentempfd");
5256 f1
= got_opentemp();
5258 error
= got_error_from_errno("got_opentemp");
5261 f2
= got_opentemp();
5263 error
= got_error_from_errno("got_opentemp");
5266 error
= got_blame(link_target
? link_target
: in_repo_path
, commit_id
,
5267 repo
, GOT_DIFF_ALGORITHM_PATIENCE
, blame_cb
, &bca
,
5268 check_cancelled
, NULL
, fd2
, fd3
, f1
, f2
);
5277 got_object_commit_close(commit
);
5279 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
5280 error
= got_error_from_errno("close");
5281 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
5282 error
= got_error_from_errno("close");
5283 if (fd3
!= -1 && close(fd3
) == -1 && error
== NULL
)
5284 error
= got_error_from_errno("close");
5285 if (f1
&& fclose(f1
) == EOF
&& error
== NULL
)
5286 error
= got_error_from_errno("fclose");
5287 if (f2
&& fclose(f2
) == EOF
&& error
== NULL
)
5288 error
= got_error_from_errno("fclose");
5291 got_object_blob_close(blob
);
5293 got_worktree_close(worktree
);
5295 const struct got_error
*close_err
= got_repo_close(repo
);
5300 const struct got_error
*pack_err
=
5301 got_repo_pack_fds_close(pack_fds
);
5306 for (i
= 0; i
< bca
.nlines
; i
++) {
5307 struct blame_line
*bline
= &bca
.lines
[i
];
5308 free(bline
->id_str
);
5309 free(bline
->committer
);
5313 free(bca
.line_offsets
);
5314 if (bca
.f
&& fclose(bca
.f
) == EOF
&& error
== NULL
)
5315 error
= got_error_from_errno("fclose");
5322 fprintf(stderr
, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5323 "[path]\n", getprogname());
5327 static const struct got_error
*
5328 print_entry(struct got_tree_entry
*te
, const char *id
, const char *path
,
5329 const char *root_path
, struct got_repository
*repo
)
5331 const struct got_error
*err
= NULL
;
5332 int is_root_path
= (strcmp(path
, root_path
) == 0);
5333 const char *modestr
= "";
5334 mode_t mode
= got_tree_entry_get_mode(te
);
5335 char *link_target
= NULL
;
5337 path
+= strlen(root_path
);
5338 while (path
[0] == '/')
5341 if (got_object_tree_entry_is_submodule(te
))
5343 else if (S_ISLNK(mode
)) {
5346 err
= got_tree_entry_get_symlink_target(&link_target
, te
, repo
);
5349 for (i
= 0; link_target
[i
] != '\0'; i
++) {
5350 if (!isprint((unsigned char)link_target
[i
]))
5351 link_target
[i
] = '?';
5356 else if (S_ISDIR(mode
))
5358 else if (mode
& S_IXUSR
)
5361 printf("%s%s%s%s%s%s%s\n", id
? id
: "", path
,
5362 is_root_path
? "" : "/", got_tree_entry_get_name(te
), modestr
,
5363 link_target
? " -> ": "", link_target
? link_target
: "");
5369 static const struct got_error
*
5370 print_tree(const char *path
, struct got_commit_object
*commit
,
5371 int show_ids
, int recurse
, const char *root_path
,
5372 struct got_repository
*repo
)
5374 const struct got_error
*err
= NULL
;
5375 struct got_object_id
*tree_id
= NULL
;
5376 struct got_tree_object
*tree
= NULL
;
5379 err
= got_object_id_by_path(&tree_id
, repo
, commit
, path
);
5383 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
5386 nentries
= got_object_tree_get_nentries(tree
);
5387 for (i
= 0; i
< nentries
; i
++) {
5388 struct got_tree_entry
*te
;
5391 if (sigint_received
|| sigpipe_received
)
5394 te
= got_object_tree_get_entry(tree
, i
);
5397 err
= got_object_id_str(&id_str
,
5398 got_tree_entry_get_id(te
));
5401 if (asprintf(&id
, "%s ", id_str
) == -1) {
5402 err
= got_error_from_errno("asprintf");
5408 err
= print_entry(te
, id
, path
, root_path
, repo
);
5413 if (recurse
&& S_ISDIR(got_tree_entry_get_mode(te
))) {
5415 if (asprintf(&child_path
, "%s%s%s", path
,
5416 path
[0] == '/' && path
[1] == '\0' ? "" : "/",
5417 got_tree_entry_get_name(te
)) == -1) {
5418 err
= got_error_from_errno("asprintf");
5421 err
= print_tree(child_path
, commit
, show_ids
, 1,
5430 got_object_tree_close(tree
);
5435 static const struct got_error
*
5436 cmd_tree(int argc
, char *argv
[])
5438 const struct got_error
*error
;
5439 struct got_repository
*repo
= NULL
;
5440 struct got_worktree
*worktree
= NULL
;
5441 const char *path
, *refname
= NULL
;
5442 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
5443 struct got_object_id
*commit_id
= NULL
;
5444 struct got_commit_object
*commit
= NULL
;
5445 char *commit_id_str
= NULL
;
5446 int show_ids
= 0, recurse
= 0;
5448 int *pack_fds
= NULL
;
5451 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5456 while ((ch
= getopt(argc
, argv
, "c:iRr:")) != -1) {
5459 commit_id_str
= optarg
;
5468 repo_path
= realpath(optarg
, NULL
);
5469 if (repo_path
== NULL
)
5470 return got_error_from_errno2("realpath",
5472 got_path_strip_trailing_slashes(repo_path
);
5490 cwd
= getcwd(NULL
, 0);
5492 error
= got_error_from_errno("getcwd");
5496 error
= got_repo_pack_fds_open(&pack_fds
);
5500 if (repo_path
== NULL
) {
5501 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
5502 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5508 strdup(got_worktree_get_repo_path(worktree
));
5509 if (repo_path
== NULL
)
5510 error
= got_error_from_errno("strdup");
5514 repo_path
= strdup(cwd
);
5515 if (repo_path
== NULL
) {
5516 error
= got_error_from_errno("strdup");
5522 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
5527 const char *prefix
= got_worktree_get_path_prefix(worktree
);
5530 if (path
== NULL
|| got_path_is_root_dir(path
))
5532 error
= got_worktree_resolve_path(&p
, worktree
, path
);
5535 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5536 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
5538 error
= got_error_from_errno("asprintf");
5543 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5547 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5552 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
5557 if (commit_id_str
== NULL
) {
5558 struct got_reference
*head_ref
;
5560 refname
= got_worktree_get_head_ref_name(worktree
);
5562 refname
= GOT_REF_HEAD
;
5563 error
= got_ref_open(&head_ref
, repo
, refname
, 0);
5566 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
5567 got_ref_close(head_ref
);
5571 struct got_reflist_head refs
;
5573 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5577 error
= got_repo_match_object_id(&commit_id
, NULL
,
5578 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
5579 got_ref_list_free(&refs
);
5585 /* Release work tree lock. */
5586 got_worktree_close(worktree
);
5590 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
5594 error
= print_tree(in_repo_path
, commit
, show_ids
, recurse
,
5595 in_repo_path
, repo
);
5602 got_object_commit_close(commit
);
5604 got_worktree_close(worktree
);
5606 const struct got_error
*close_err
= got_repo_close(repo
);
5611 const struct got_error
*pack_err
=
5612 got_repo_pack_fds_close(pack_fds
);
5622 fprintf(stderr
, "usage: %s status [-I] [-S status-codes] "
5623 "[-s status-codes] [path ...]\n", getprogname());
5627 struct got_status_arg
{
5632 static const struct got_error
*
5633 print_status(void *arg
, unsigned char status
, unsigned char staged_status
,
5634 const char *path
, struct got_object_id
*blob_id
,
5635 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
5636 int dirfd
, const char *de_name
)
5638 struct got_status_arg
*st
= arg
;
5640 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
5641 status
= GOT_STATUS_NO_CHANGE
;
5642 if (st
!= NULL
&& st
->status_codes
) {
5643 size_t ncodes
= strlen(st
->status_codes
);
5646 for (i
= 0; i
< ncodes
; i
++) {
5648 if (status
== st
->status_codes
[i
] ||
5649 staged_status
== st
->status_codes
[i
]) {
5654 if (status
== st
->status_codes
[i
] ||
5655 staged_status
== st
->status_codes
[i
])
5660 if (st
->suppress
&& j
== 0)
5667 printf("%c%c %s\n", status
, staged_status
, path
);
5671 static const struct got_error
*
5672 cmd_status(int argc
, char *argv
[])
5674 const struct got_error
*error
= NULL
;
5675 struct got_repository
*repo
= NULL
;
5676 struct got_worktree
*worktree
= NULL
;
5677 struct got_status_arg st
;
5679 struct got_pathlist_head paths
;
5680 int ch
, i
, no_ignores
= 0;
5681 int *pack_fds
= NULL
;
5685 memset(&st
, 0, sizeof(st
));
5686 st
.status_codes
= NULL
;
5690 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5695 while ((ch
= getopt(argc
, argv
, "IS:s:")) != -1) {
5701 if (st
.status_codes
!= NULL
&& st
.suppress
== 0)
5702 option_conflict('S', 's');
5706 for (i
= 0; optarg
[i
] != '\0'; i
++) {
5707 switch (optarg
[i
]) {
5708 case GOT_STATUS_MODIFY
:
5709 case GOT_STATUS_ADD
:
5710 case GOT_STATUS_DELETE
:
5711 case GOT_STATUS_CONFLICT
:
5712 case GOT_STATUS_MISSING
:
5713 case GOT_STATUS_OBSTRUCTED
:
5714 case GOT_STATUS_UNVERSIONED
:
5715 case GOT_STATUS_MODE_CHANGE
:
5716 case GOT_STATUS_NONEXISTENT
:
5719 errx(1, "invalid status code '%c'",
5723 if (ch
== 's' && st
.suppress
)
5724 option_conflict('s', 'S');
5725 st
.status_codes
= optarg
;
5736 cwd
= getcwd(NULL
, 0);
5738 error
= got_error_from_errno("getcwd");
5742 error
= got_repo_pack_fds_open(&pack_fds
);
5746 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
5748 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
5749 error
= wrap_not_worktree_error(error
, "status", cwd
);
5753 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
5758 error
= apply_unveil(got_repo_get_path(repo
), 1,
5759 got_worktree_get_root_path(worktree
));
5763 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
5767 error
= got_worktree_status(worktree
, &paths
, repo
, no_ignores
,
5768 print_status
, &st
, check_cancelled
, NULL
);
5771 const struct got_error
*pack_err
=
5772 got_repo_pack_fds_close(pack_fds
);
5777 const struct got_error
*close_err
= got_repo_close(repo
);
5782 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
5790 fprintf(stderr
, "usage: %s tag [-lVv] [-c commit] [-m message] "
5791 "[-r repository-path] [-s signer-id] name\n", getprogname());
5796 static const struct got_error
*
5797 sort_tags(struct got_reflist_head
*sorted
, struct got_reflist_head
*tags
)
5799 const struct got_error
*err
= NULL
;
5800 struct got_reflist_entry
*re
, *se
, *new;
5801 struct got_object_id
*re_id
, *se_id
;
5802 struct got_tag_object
*re_tag
, *se_tag
;
5803 time_t re_time
, se_time
;
5805 STAILQ_FOREACH(re
, tags
, entry
) {
5806 se
= STAILQ_FIRST(sorted
);
5808 err
= got_reflist_entry_dup(&new, re
);
5811 STAILQ_INSERT_HEAD(sorted
, new, entry
);
5814 err
= got_ref_resolve(&re_id
, repo
, re
->ref
);
5817 err
= got_object_open_as_tag(&re_tag
, repo
, re_id
);
5821 re_time
= got_object_tag_get_tagger_time(re_tag
);
5822 got_object_tag_close(re_tag
);
5826 err
= got_ref_resolve(&se_id
, repo
, re
->ref
);
5829 err
= got_object_open_as_tag(&se_tag
, repo
, se_id
);
5833 se_time
= got_object_tag_get_tagger_time(se_tag
);
5834 got_object_tag_close(se_tag
);
5836 if (se_time
> re_time
) {
5837 err
= got_reflist_entry_dup(&new, re
);
5840 STAILQ_INSERT_AFTER(sorted
, se
, new, entry
);
5843 se
= STAILQ_NEXT(se
, entry
);
5852 static const struct got_error
*
5853 get_tag_refname(char **refname
, const char *tag_name
)
5855 const struct got_error
*err
;
5857 if (strncmp("refs/tags/", tag_name
, 10) == 0) {
5858 *refname
= strdup(tag_name
);
5859 if (*refname
== NULL
)
5860 return got_error_from_errno("strdup");
5861 } else if (asprintf(refname
, "refs/tags/%s", tag_name
) == -1) {
5862 err
= got_error_from_errno("asprintf");
5870 static const struct got_error
*
5871 list_tags(struct got_repository
*repo
, const char *tag_name
, int verify_tags
,
5872 const char *allowed_signers
, const char *revoked_signers
, int verbosity
)
5874 static const struct got_error
*err
= NULL
;
5875 struct got_reflist_head refs
;
5876 struct got_reflist_entry
*re
;
5877 char *wanted_refname
= NULL
;
5882 err
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
, repo
);
5887 struct got_reference
*ref
;
5888 err
= get_tag_refname(&wanted_refname
, tag_name
);
5891 /* Wanted tag reference should exist. */
5892 err
= got_ref_open(&ref
, repo
, wanted_refname
, 0);
5898 TAILQ_FOREACH(re
, &refs
, entry
) {
5899 const char *refname
;
5900 char *refstr
, *tagmsg0
, *tagmsg
, *line
, *id_str
, *datestr
;
5902 const char *tagger
, *ssh_sig
= NULL
;
5903 char *sig_msg
= NULL
;
5905 struct got_object_id
*id
;
5906 struct got_tag_object
*tag
;
5907 struct got_commit_object
*commit
= NULL
;
5909 refname
= got_ref_get_name(re
->ref
);
5910 if (strncmp(refname
, "refs/tags/", 10) != 0 ||
5911 (wanted_refname
&& strcmp(refname
, wanted_refname
) != 0))
5914 refstr
= got_ref_to_str(re
->ref
);
5915 if (refstr
== NULL
) {
5916 err
= got_error_from_errno("got_ref_to_str");
5920 err
= got_ref_resolve(&id
, repo
, re
->ref
);
5923 err
= got_object_open_as_tag(&tag
, repo
, id
);
5925 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
5929 /* "lightweight" tag */
5930 err
= got_object_open_as_commit(&commit
, repo
, id
);
5935 tagger
= got_object_commit_get_committer(commit
);
5937 got_object_commit_get_committer_time(commit
);
5938 err
= got_object_id_str(&id_str
, id
);
5944 tagger
= got_object_tag_get_tagger(tag
);
5945 tagger_time
= got_object_tag_get_tagger_time(tag
);
5946 err
= got_object_id_str(&id_str
,
5947 got_object_tag_get_object_id(tag
));
5952 if (tag
&& verify_tags
) {
5953 ssh_sig
= got_sigs_get_tagmsg_ssh_signature(
5954 got_object_tag_get_message(tag
));
5955 if (ssh_sig
&& allowed_signers
== NULL
) {
5956 err
= got_error_msg(
5957 GOT_ERR_VERIFY_TAG_SIGNATURE
,
5958 "SSH signature verification requires "
5959 "setting allowed_signers in "
5965 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR
, refname
, refstr
);
5967 printf("from: %s\n", tagger
);
5968 datestr
= get_datestr(&tagger_time
, datebuf
);
5970 printf("date: %s UTC\n", datestr
);
5972 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
, id_str
);
5974 switch (got_object_tag_get_object_type(tag
)) {
5975 case GOT_OBJ_TYPE_BLOB
:
5976 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB
,
5979 case GOT_OBJ_TYPE_TREE
:
5980 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE
,
5983 case GOT_OBJ_TYPE_COMMIT
:
5984 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
,
5987 case GOT_OBJ_TYPE_TAG
:
5988 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG
,
5998 err
= got_sigs_verify_tag_ssh(&sig_msg
, tag
, ssh_sig
,
5999 allowed_signers
, revoked_signers
, verbosity
);
6000 if (err
&& err
->code
== GOT_ERR_BAD_TAG_SIGNATURE
)
6004 printf("signature: %s", sig_msg
);
6010 err
= got_object_commit_get_logmsg(&tagmsg0
, commit
);
6013 got_object_commit_close(commit
);
6015 tagmsg0
= strdup(got_object_tag_get_message(tag
));
6016 got_object_tag_close(tag
);
6017 if (tagmsg0
== NULL
) {
6018 err
= got_error_from_errno("strdup");
6025 line
= strsep(&tagmsg
, "\n");
6027 printf(" %s\n", line
);
6032 got_ref_list_free(&refs
);
6033 free(wanted_refname
);
6035 if (err
== NULL
&& bad_sigs
)
6036 err
= got_error(GOT_ERR_BAD_TAG_SIGNATURE
);
6040 static const struct got_error
*
6041 get_tag_message(char **tagmsg
, char **tagmsg_path
, const char *commit_id_str
,
6042 const char *tag_name
, const char *repo_path
)
6044 const struct got_error
*err
= NULL
;
6045 char *template = NULL
, *initial_content
= NULL
;
6046 char *editor
= NULL
;
6047 int initial_content_len
;
6050 if (asprintf(&template, GOT_TMPDIR_STR
"/got-tagmsg") == -1) {
6051 err
= got_error_from_errno("asprintf");
6055 initial_content_len
= asprintf(&initial_content
,
6056 "\n# tagging commit %s as %s\n",
6057 commit_id_str
, tag_name
);
6058 if (initial_content_len
== -1) {
6059 err
= got_error_from_errno("asprintf");
6063 err
= got_opentemp_named_fd(tagmsg_path
, &fd
, template, "");
6067 if (write(fd
, initial_content
, initial_content_len
) == -1) {
6068 err
= got_error_from_errno2("write", *tagmsg_path
);
6071 if (close(fd
) == -1) {
6072 err
= got_error_from_errno2("close", *tagmsg_path
);
6077 err
= get_editor(&editor
);
6080 err
= edit_logmsg(tagmsg
, editor
, *tagmsg_path
, initial_content
,
6081 initial_content_len
, 1);
6083 free(initial_content
);
6087 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
6088 err
= got_error_from_errno2("close", *tagmsg_path
);
6097 static const struct got_error
*
6098 add_tag(struct got_repository
*repo
, const char *tagger
,
6099 const char *tag_name
, const char *commit_arg
, const char *tagmsg_arg
,
6100 const char *signer_id
, int verbosity
)
6102 const struct got_error
*err
= NULL
;
6103 struct got_object_id
*commit_id
= NULL
, *tag_id
= NULL
;
6104 char *label
= NULL
, *commit_id_str
= NULL
;
6105 struct got_reference
*ref
= NULL
;
6106 char *refname
= NULL
, *tagmsg
= NULL
;
6107 char *tagmsg_path
= NULL
, *tag_id_str
= NULL
;
6108 int preserve_tagmsg
= 0;
6109 struct got_reflist_head refs
;
6114 * Don't let the user create a tag name with a leading '-'.
6115 * While technically a valid reference name, this case is usually
6116 * an unintended typo.
6118 if (tag_name
[0] == '-')
6119 return got_error_path(tag_name
, GOT_ERR_REF_NAME_MINUS
);
6121 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
6125 err
= got_repo_match_object_id(&commit_id
, &label
, commit_arg
,
6126 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
6130 err
= got_object_id_str(&commit_id_str
, commit_id
);
6134 err
= get_tag_refname(&refname
, tag_name
);
6137 if (strncmp("refs/tags/", tag_name
, 10) == 0)
6140 err
= got_ref_open(&ref
, repo
, refname
, 0);
6142 err
= got_error(GOT_ERR_TAG_EXISTS
);
6144 } else if (err
->code
!= GOT_ERR_NOT_REF
)
6147 if (tagmsg_arg
== NULL
) {
6148 err
= get_tag_message(&tagmsg
, &tagmsg_path
, commit_id_str
,
6149 tag_name
, got_repo_get_path(repo
));
6151 if (err
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
6152 tagmsg_path
!= NULL
)
6153 preserve_tagmsg
= 1;
6156 /* Editor is done; we can now apply unveil(2) */
6157 err
= got_sigs_apply_unveil();
6160 err
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
6165 err
= got_object_tag_create(&tag_id
, tag_name
, commit_id
,
6166 tagger
, time(NULL
), tagmsg
? tagmsg
: tagmsg_arg
, signer_id
, repo
,
6170 preserve_tagmsg
= 1;
6174 err
= got_ref_alloc(&ref
, refname
, tag_id
);
6177 preserve_tagmsg
= 1;
6181 err
= got_ref_write(ref
, repo
);
6184 preserve_tagmsg
= 1;
6188 err
= got_object_id_str(&tag_id_str
, tag_id
);
6191 preserve_tagmsg
= 1;
6194 printf("Created tag %s\n", tag_id_str
);
6196 if (preserve_tagmsg
) {
6197 fprintf(stderr
, "%s: tag message preserved in %s\n",
6198 getprogname(), tagmsg_path
);
6199 } else if (tagmsg_path
&& unlink(tagmsg_path
) == -1 && err
== NULL
)
6200 err
= got_error_from_errno2("unlink", tagmsg_path
);
6205 free(commit_id_str
);
6209 got_ref_list_free(&refs
);
6213 static const struct got_error
*
6214 cmd_tag(int argc
, char *argv
[])
6216 const struct got_error
*error
= NULL
;
6217 struct got_repository
*repo
= NULL
;
6218 struct got_worktree
*worktree
= NULL
;
6219 char *cwd
= NULL
, *repo_path
= NULL
, *commit_id_str
= NULL
;
6220 char *gitconfig_path
= NULL
, *tagger
= NULL
;
6221 char *allowed_signers
= NULL
, *revoked_signers
= NULL
;
6222 const char *signer_id
= NULL
;
6223 const char *tag_name
= NULL
, *commit_id_arg
= NULL
, *tagmsg
= NULL
;
6224 int ch
, do_list
= 0, verify_tags
= 0, verbosity
= 0;
6225 int *pack_fds
= NULL
;
6228 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6229 "sendfd unveil", NULL
) == -1)
6233 while ((ch
= getopt(argc
, argv
, "c:lm:r:s:Vv")) != -1) {
6236 commit_id_arg
= optarg
;
6245 repo_path
= realpath(optarg
, NULL
);
6246 if (repo_path
== NULL
) {
6247 error
= got_error_from_errno2("realpath",
6251 got_path_strip_trailing_slashes(repo_path
);
6262 else if (verbosity
< 3)
6274 if (do_list
|| verify_tags
) {
6275 if (commit_id_arg
!= NULL
)
6277 "-c option can only be used when creating a tag");
6280 option_conflict('l', 'm');
6282 option_conflict('V', 'm');
6286 option_conflict('l', 's');
6288 option_conflict('V', 's');
6292 } else if (argc
!= 1)
6298 cwd
= getcwd(NULL
, 0);
6300 error
= got_error_from_errno("getcwd");
6304 error
= got_repo_pack_fds_open(&pack_fds
);
6308 if (repo_path
== NULL
) {
6309 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
6310 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6316 strdup(got_worktree_get_repo_path(worktree
));
6317 if (repo_path
== NULL
)
6318 error
= got_error_from_errno("strdup");
6322 repo_path
= strdup(cwd
);
6323 if (repo_path
== NULL
) {
6324 error
= got_error_from_errno("strdup");
6330 if (do_list
|| verify_tags
) {
6331 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
6334 error
= get_allowed_signers(&allowed_signers
, repo
, worktree
);
6337 error
= get_revoked_signers(&revoked_signers
, repo
, worktree
);
6341 /* Release work tree lock. */
6342 got_worktree_close(worktree
);
6347 * Remove "cpath" promise unless needed for signature tmpfile
6351 got_sigs_apply_unveil();
6354 if (pledge("stdio rpath wpath flock proc exec sendfd "
6355 "unveil", NULL
) == -1)
6359 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
6362 error
= list_tags(repo
, tag_name
, verify_tags
, allowed_signers
,
6363 revoked_signers
, verbosity
);
6365 error
= get_gitconfig_path(&gitconfig_path
);
6368 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
,
6373 error
= get_author(&tagger
, repo
, worktree
);
6376 if (signer_id
== NULL
)
6377 signer_id
= get_signer_id(repo
, worktree
);
6381 error
= got_sigs_apply_unveil();
6385 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
6390 if (commit_id_arg
== NULL
) {
6391 struct got_reference
*head_ref
;
6392 struct got_object_id
*commit_id
;
6393 error
= got_ref_open(&head_ref
, repo
,
6394 worktree
? got_worktree_get_head_ref_name(worktree
)
6398 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
6399 got_ref_close(head_ref
);
6402 error
= got_object_id_str(&commit_id_str
, commit_id
);
6409 /* Release work tree lock. */
6410 got_worktree_close(worktree
);
6414 error
= add_tag(repo
, tagger
, tag_name
,
6415 commit_id_str
? commit_id_str
: commit_id_arg
, tagmsg
,
6416 signer_id
, verbosity
);
6420 const struct got_error
*close_err
= got_repo_close(repo
);
6425 got_worktree_close(worktree
);
6427 const struct got_error
*pack_err
=
6428 got_repo_pack_fds_close(pack_fds
);
6434 free(gitconfig_path
);
6435 free(commit_id_str
);
6437 free(allowed_signers
);
6438 free(revoked_signers
);
6445 fprintf(stderr
, "usage: %s add [-IR] path ...\n", getprogname());
6449 static const struct got_error
*
6450 add_progress(void *arg
, unsigned char status
, const char *path
)
6452 while (path
[0] == '/')
6454 printf("%c %s\n", status
, path
);
6458 static const struct got_error
*
6459 cmd_add(int argc
, char *argv
[])
6461 const struct got_error
*error
= NULL
;
6462 struct got_repository
*repo
= NULL
;
6463 struct got_worktree
*worktree
= NULL
;
6465 struct got_pathlist_head paths
;
6466 struct got_pathlist_entry
*pe
;
6467 int ch
, can_recurse
= 0, no_ignores
= 0;
6468 int *pack_fds
= NULL
;
6473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6478 while ((ch
= getopt(argc
, argv
, "IR")) != -1) {
6498 cwd
= getcwd(NULL
, 0);
6500 error
= got_error_from_errno("getcwd");
6504 error
= got_repo_pack_fds_open(&pack_fds
);
6508 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
6510 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
6511 error
= wrap_not_worktree_error(error
, "add", cwd
);
6515 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
6520 error
= apply_unveil(got_repo_get_path(repo
), 1,
6521 got_worktree_get_root_path(worktree
));
6525 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
6532 TAILQ_FOREACH(pe
, &paths
, entry
) {
6533 if (asprintf(&ondisk_path
, "%s/%s",
6534 got_worktree_get_root_path(worktree
),
6536 error
= got_error_from_errno("asprintf");
6539 if (lstat(ondisk_path
, &sb
) == -1) {
6540 if (errno
== ENOENT
) {
6544 error
= got_error_from_errno2("lstat",
6550 if (S_ISDIR(sb
.st_mode
)) {
6551 error
= got_error_msg(GOT_ERR_BAD_PATH
,
6552 "adding directories requires -R option");
6558 error
= got_worktree_schedule_add(worktree
, &paths
, add_progress
,
6559 NULL
, repo
, no_ignores
);
6562 const struct got_error
*close_err
= got_repo_close(repo
);
6567 got_worktree_close(worktree
);
6569 const struct got_error
*pack_err
=
6570 got_repo_pack_fds_close(pack_fds
);
6574 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
6582 fprintf(stderr
, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
6587 static const struct got_error
*
6588 print_remove_status(void *arg
, unsigned char status
,
6589 unsigned char staged_status
, const char *path
)
6591 while (path
[0] == '/')
6593 if (status
== GOT_STATUS_NONEXISTENT
)
6595 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
6596 status
= GOT_STATUS_NO_CHANGE
;
6597 printf("%c%c %s\n", status
, staged_status
, path
);
6601 static const struct got_error
*
6602 cmd_remove(int argc
, char *argv
[])
6604 const struct got_error
*error
= NULL
;
6605 struct got_worktree
*worktree
= NULL
;
6606 struct got_repository
*repo
= NULL
;
6607 const char *status_codes
= NULL
;
6609 struct got_pathlist_head paths
;
6610 struct got_pathlist_entry
*pe
;
6611 int ch
, delete_local_mods
= 0, can_recurse
= 0, keep_on_disk
= 0, i
;
6612 int ignore_missing_paths
= 0;
6613 int *pack_fds
= NULL
;
6618 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6623 while ((ch
= getopt(argc
, argv
, "fkRs:")) != -1) {
6626 delete_local_mods
= 1;
6627 ignore_missing_paths
= 1;
6636 for (i
= 0; optarg
[i
] != '\0'; i
++) {
6637 switch (optarg
[i
]) {
6638 case GOT_STATUS_MODIFY
:
6639 delete_local_mods
= 1;
6641 case GOT_STATUS_MISSING
:
6642 ignore_missing_paths
= 1;
6645 errx(1, "invalid status code '%c'",
6649 status_codes
= optarg
;
6663 cwd
= getcwd(NULL
, 0);
6665 error
= got_error_from_errno("getcwd");
6669 error
= got_repo_pack_fds_open(&pack_fds
);
6673 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
6675 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
6676 error
= wrap_not_worktree_error(error
, "remove", cwd
);
6680 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
6685 error
= apply_unveil(got_repo_get_path(repo
), 1,
6686 got_worktree_get_root_path(worktree
));
6690 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
6697 TAILQ_FOREACH(pe
, &paths
, entry
) {
6698 if (asprintf(&ondisk_path
, "%s/%s",
6699 got_worktree_get_root_path(worktree
),
6701 error
= got_error_from_errno("asprintf");
6704 if (lstat(ondisk_path
, &sb
) == -1) {
6705 if (errno
== ENOENT
) {
6709 error
= got_error_from_errno2("lstat",
6715 if (S_ISDIR(sb
.st_mode
)) {
6716 error
= got_error_msg(GOT_ERR_BAD_PATH
,
6717 "removing directories requires -R option");
6723 error
= got_worktree_schedule_delete(worktree
, &paths
,
6724 delete_local_mods
, status_codes
, print_remove_status
, NULL
,
6725 repo
, keep_on_disk
, ignore_missing_paths
);
6728 const struct got_error
*close_err
= got_repo_close(repo
);
6733 got_worktree_close(worktree
);
6735 const struct got_error
*pack_err
=
6736 got_repo_pack_fds_close(pack_fds
);
6740 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
6748 fprintf(stderr
, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
6749 "[patchfile]\n", getprogname());
6753 static const struct got_error
*
6754 patch_from_stdin(int *patchfd
)
6756 const struct got_error
*err
= NULL
;
6759 sig_t sighup
, sigint
, sigquit
;
6761 *patchfd
= got_opentempfd();
6763 return got_error_from_errno("got_opentempfd");
6765 sighup
= signal(SIGHUP
, SIG_DFL
);
6766 sigint
= signal(SIGINT
, SIG_DFL
);
6767 sigquit
= signal(SIGQUIT
, SIG_DFL
);
6770 r
= read(0, buf
, sizeof(buf
));
6772 err
= got_error_from_errno("read");
6777 if (write(*patchfd
, buf
, r
) == -1) {
6778 err
= got_error_from_errno("write");
6783 signal(SIGHUP
, sighup
);
6784 signal(SIGINT
, sigint
);
6785 signal(SIGQUIT
, sigquit
);
6787 if (err
== NULL
&& lseek(*patchfd
, 0, SEEK_SET
) == -1)
6788 err
= got_error_from_errno("lseek");
6798 struct got_patch_progress_arg
{
6804 static const struct got_error
*
6805 patch_progress(void *arg
, const char *old
, const char *new,
6806 unsigned char status
, const struct got_error
*error
, int old_from
,
6807 int old_lines
, int new_from
, int new_lines
, int offset
,
6808 int ws_mangled
, const struct got_error
*hunk_err
)
6810 const char *path
= new == NULL
? old
: new;
6811 struct got_patch_progress_arg
*a
= arg
;
6813 while (*path
== '/')
6816 if (status
!= GOT_STATUS_NO_CHANGE
&&
6817 status
!= 0 /* per-hunk progress */) {
6818 printf("%c %s\n", status
, path
);
6819 a
->did_something
= 1;
6822 if (hunk_err
== NULL
) {
6823 if (status
== GOT_STATUS_CANNOT_UPDATE
)
6825 else if (status
== GOT_STATUS_CONFLICT
)
6830 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
6832 if (offset
!= 0 || hunk_err
!= NULL
|| ws_mangled
) {
6833 printf("@@ -%d,%d +%d,%d @@ ", old_from
,
6834 old_lines
, new_from
, new_lines
);
6835 if (hunk_err
!= NULL
)
6836 printf("%s\n", hunk_err
->msg
);
6837 else if (offset
!= 0)
6838 printf("applied with offset %d\n", offset
);
6840 printf("hunk contains mangled whitespace\n");
6847 print_patch_progress_stats(struct got_patch_progress_arg
*ppa
)
6849 if (!ppa
->did_something
)
6852 if (ppa
->conflicts
> 0)
6853 printf("Files with merge conflicts: %d\n", ppa
->conflicts
);
6855 if (ppa
->rejects
> 0) {
6856 printf("Files where patch failed to apply: %d\n",
6861 static const struct got_error
*
6862 cmd_patch(int argc
, char *argv
[])
6864 const struct got_error
*error
= NULL
, *close_error
= NULL
;
6865 struct got_worktree
*worktree
= NULL
;
6866 struct got_repository
*repo
= NULL
;
6867 struct got_reflist_head refs
;
6868 struct got_object_id
*commit_id
= NULL
;
6869 const char *commit_id_str
= NULL
;
6873 int ch
, nop
= 0, strip
= -1, reverse
= 0;
6875 int *pack_fds
= NULL
;
6876 struct got_patch_progress_arg ppa
;
6881 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
6882 "unveil", NULL
) == -1)
6886 while ((ch
= getopt(argc
, argv
, "c:np:R")) != -1) {
6889 commit_id_str
= optarg
;
6895 strip
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
6897 errx(1, "pathname strip count is %s: %s",
6913 error
= patch_from_stdin(&patchfd
);
6916 } else if (argc
== 1) {
6917 patchfd
= open(argv
[0], O_RDONLY
);
6919 return got_error_from_errno2("open", argv
[0]);
6920 if (fstat(patchfd
, &sb
) == -1) {
6921 error
= got_error_from_errno2("fstat", argv
[0]);
6924 if (!S_ISREG(sb
.st_mode
)) {
6925 error
= got_error_path(argv
[0], GOT_ERR_BAD_FILETYPE
);
6931 if ((cwd
= getcwd(NULL
, 0)) == NULL
) {
6932 error
= got_error_from_errno("getcwd");
6936 error
= got_repo_pack_fds_open(&pack_fds
);
6940 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
6944 const char *repo_path
= got_worktree_get_repo_path(worktree
);
6945 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
6949 error
= apply_unveil(got_repo_get_path(repo
), 0,
6950 got_worktree_get_root_path(worktree
));
6954 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
6958 if (commit_id_str
!= NULL
) {
6959 error
= got_repo_match_object_id(&commit_id
, NULL
,
6960 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
6965 memset(&ppa
, 0, sizeof(ppa
));
6966 error
= got_patch(patchfd
, worktree
, repo
, nop
, strip
, reverse
,
6967 commit_id
, patch_progress
, &ppa
, check_cancelled
, NULL
);
6968 print_patch_progress_stats(&ppa
);
6970 got_ref_list_free(&refs
);
6973 close_error
= got_repo_close(repo
);
6975 error
= close_error
;
6977 if (worktree
!= NULL
) {
6978 close_error
= got_worktree_close(worktree
);
6980 error
= close_error
;
6983 const struct got_error
*pack_err
=
6984 got_repo_pack_fds_close(pack_fds
);
6995 fprintf(stderr
, "usage: %s revert [-pR] [-F response-script] path ...\n",
7000 static const struct got_error
*
7001 revert_progress(void *arg
, unsigned char status
, const char *path
)
7003 if (status
== GOT_STATUS_UNVERSIONED
)
7006 while (path
[0] == '/')
7008 printf("%c %s\n", status
, path
);
7012 struct choose_patch_arg
{
7013 FILE *patch_script_file
;
7017 static const struct got_error
*
7018 show_change(unsigned char status
, const char *path
, FILE *patch_file
, int n
,
7019 int nchanges
, const char *action
)
7021 const struct got_error
*err
;
7023 size_t linesize
= 0;
7027 case GOT_STATUS_ADD
:
7028 printf("A %s\n%s this addition? [y/n] ", path
, action
);
7030 case GOT_STATUS_DELETE
:
7031 printf("D %s\n%s this deletion? [y/n] ", path
, action
);
7033 case GOT_STATUS_MODIFY
:
7034 if (fseek(patch_file
, 0L, SEEK_SET
) == -1)
7035 return got_error_from_errno("fseek");
7036 printf(GOT_COMMIT_SEP_STR
);
7037 while ((linelen
= getline(&line
, &linesize
, patch_file
)) != -1)
7039 if (linelen
== -1 && ferror(patch_file
)) {
7040 err
= got_error_from_errno("getline");
7045 printf(GOT_COMMIT_SEP_STR
);
7046 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7047 path
, n
, nchanges
, action
);
7050 return got_error_path(path
, GOT_ERR_FILE_STATUS
);
7057 static const struct got_error
*
7058 choose_patch(int *choice
, void *arg
, unsigned char status
, const char *path
,
7059 FILE *patch_file
, int n
, int nchanges
)
7061 const struct got_error
*err
= NULL
;
7063 size_t linesize
= 0;
7066 struct choose_patch_arg
*a
= arg
;
7068 *choice
= GOT_PATCH_CHOICE_NONE
;
7070 if (a
->patch_script_file
) {
7072 err
= show_change(status
, path
, patch_file
, n
, nchanges
,
7076 linelen
= getline(&line
, &linesize
, a
->patch_script_file
);
7077 if (linelen
== -1) {
7078 if (ferror(a
->patch_script_file
))
7079 return got_error_from_errno("getline");
7082 nl
= strchr(line
, '\n');
7085 if (strcmp(line
, "y") == 0) {
7086 *choice
= GOT_PATCH_CHOICE_YES
;
7088 } else if (strcmp(line
, "n") == 0) {
7089 *choice
= GOT_PATCH_CHOICE_NO
;
7091 } else if (strcmp(line
, "q") == 0 &&
7092 status
== GOT_STATUS_MODIFY
) {
7093 *choice
= GOT_PATCH_CHOICE_QUIT
;
7096 printf("invalid response '%s'\n", line
);
7101 while (resp
!= 'y' && resp
!= 'n' && resp
!= 'q') {
7102 err
= show_change(status
, path
, patch_file
, n
, nchanges
,
7109 if (status
== GOT_STATUS_MODIFY
) {
7110 if (resp
!= 'y' && resp
!= 'n' && resp
!= 'q') {
7111 printf("invalid response '%c'\n", resp
);
7114 } else if (resp
!= 'y' && resp
!= 'n') {
7115 printf("invalid response '%c'\n", resp
);
7121 *choice
= GOT_PATCH_CHOICE_YES
;
7122 else if (resp
== 'n')
7123 *choice
= GOT_PATCH_CHOICE_NO
;
7124 else if (resp
== 'q' && status
== GOT_STATUS_MODIFY
)
7125 *choice
= GOT_PATCH_CHOICE_QUIT
;
7130 struct wt_commitable_path_arg
{
7131 struct got_pathlist_head
*commit_paths
;
7136 * Shortcut work tree status callback to determine if the set of paths scanned
7137 * has at least one versioned path that is being modified and, if not NULL, is
7138 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
7139 * soon as a path is passed with a status that satisfies this criteria.
7141 static const struct got_error
*
7142 worktree_has_commitable_path(void *arg
, unsigned char status
,
7143 unsigned char staged_status
, const char *path
,
7144 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
7145 struct got_object_id
*commit_id
, int dirfd
, const char *de_name
)
7147 struct wt_commitable_path_arg
*a
= arg
;
7149 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
7150 status
= GOT_STATUS_NO_CHANGE
;
7152 if (!(status
== GOT_STATUS_NO_CHANGE
||
7153 status
== GOT_STATUS_UNVERSIONED
) ||
7154 staged_status
!= GOT_STATUS_NO_CHANGE
) {
7155 if (a
->commit_paths
!= NULL
) {
7156 struct got_pathlist_entry
*pe
;
7158 TAILQ_FOREACH(pe
, a
->commit_paths
, entry
) {
7159 if (strncmp(path
, pe
->path
,
7160 pe
->path_len
) == 0) {
7161 *a
->has_changes
= 1;
7166 *a
->has_changes
= 1;
7168 if (*a
->has_changes
)
7169 return got_error(GOT_ERR_FILE_MODIFIED
);
7176 * Check that the changeset of the commit identified by id is
7177 * comprised of at least one modified path that is being committed.
7179 static const struct got_error
*
7180 commit_path_changed_in_worktree(struct wt_commitable_path_arg
*wcpa
,
7181 struct got_object_id
*id
, struct got_worktree
*worktree
,
7182 struct got_repository
*repo
)
7184 const struct got_error
*err
;
7185 struct got_pathlist_head paths
;
7186 struct got_commit_object
*commit
= NULL
, *pcommit
= NULL
;
7187 struct got_tree_object
*tree
= NULL
, *ptree
= NULL
;
7188 struct got_object_qid
*pid
;
7192 err
= got_object_open_as_commit(&commit
, repo
, id
);
7196 err
= got_object_open_as_tree(&tree
, repo
,
7197 got_object_commit_get_tree_id(commit
));
7201 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
7203 err
= got_object_open_as_commit(&pcommit
, repo
, &pid
->id
);
7207 err
= got_object_open_as_tree(&ptree
, repo
,
7208 got_object_commit_get_tree_id(pcommit
));
7213 err
= got_diff_tree(ptree
, tree
, NULL
, NULL
, -1, -1, "", "", repo
,
7214 got_diff_tree_collect_changed_paths
, &paths
, 0);
7218 err
= got_worktree_status(worktree
, &paths
, repo
, 0,
7219 worktree_has_commitable_path
, wcpa
, check_cancelled
, NULL
);
7220 if (err
&& err
->code
== GOT_ERR_FILE_MODIFIED
) {
7222 * At least one changed path in the referenced commit is
7223 * modified in the work tree, that's all we need to know!
7229 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_ALL
);
7231 got_object_commit_close(commit
);
7233 got_object_commit_close(pcommit
);
7235 got_object_tree_close(tree
);
7237 got_object_tree_close(ptree
);
7242 * Remove any "logmsg" reference comprised entirely of paths that have
7243 * been reverted in this work tree. If any path in the logmsg ref changeset
7244 * remains in a changed state in the worktree, do not remove the reference.
7246 static const struct got_error
*
7247 rm_logmsg_ref(struct got_worktree
*worktree
, struct got_repository
*repo
)
7249 const struct got_error
*err
;
7250 struct got_reflist_head refs
;
7251 struct got_reflist_entry
*re
;
7252 struct got_commit_object
*commit
= NULL
;
7253 struct got_object_id
*commit_id
= NULL
;
7254 struct wt_commitable_path_arg wcpa
;
7255 char *uuidstr
= NULL
;
7259 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
7263 err
= got_ref_list(&refs
, repo
, "refs/got/worktree",
7264 got_ref_cmp_by_name
, repo
);
7268 TAILQ_FOREACH(re
, &refs
, entry
) {
7269 const char *refname
;
7270 int has_changes
= 0;
7272 refname
= got_ref_get_name(re
->ref
);
7274 if (!strncmp(refname
, GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
7275 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
))
7276 refname
+= GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
+ 1;
7277 else if (!strncmp(refname
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
7278 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
))
7279 refname
+= GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
+ 1;
7283 if (strncmp(refname
, uuidstr
, GOT_WORKTREE_UUID_STRLEN
) == 0)
7284 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
7288 err
= got_repo_match_object_id(&commit_id
, NULL
, refname
,
7289 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
7293 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
7297 wcpa
.commit_paths
= NULL
;
7298 wcpa
.has_changes
= &has_changes
;
7300 err
= commit_path_changed_in_worktree(&wcpa
, commit_id
,
7306 err
= got_ref_delete(re
->ref
, repo
);
7311 got_object_commit_close(commit
);
7320 got_ref_list_free(&refs
);
7322 got_object_commit_close(commit
);
7326 static const struct got_error
*
7327 cmd_revert(int argc
, char *argv
[])
7329 const struct got_error
*error
= NULL
;
7330 struct got_worktree
*worktree
= NULL
;
7331 struct got_repository
*repo
= NULL
;
7332 char *cwd
= NULL
, *path
= NULL
;
7333 struct got_pathlist_head paths
;
7334 struct got_pathlist_entry
*pe
;
7335 int ch
, can_recurse
= 0, pflag
= 0;
7336 FILE *patch_script_file
= NULL
;
7337 const char *patch_script_path
= NULL
;
7338 struct choose_patch_arg cpa
;
7339 int *pack_fds
= NULL
;
7344 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7345 "unveil", NULL
) == -1)
7349 while ((ch
= getopt(argc
, argv
, "F:pR")) != -1) {
7352 patch_script_path
= optarg
;
7371 if (patch_script_path
&& !pflag
)
7372 errx(1, "-F option can only be used together with -p option");
7374 cwd
= getcwd(NULL
, 0);
7376 error
= got_error_from_errno("getcwd");
7380 error
= got_repo_pack_fds_open(&pack_fds
);
7384 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
7386 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
7387 error
= wrap_not_worktree_error(error
, "revert", cwd
);
7391 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
7396 if (patch_script_path
) {
7397 patch_script_file
= fopen(patch_script_path
, "re");
7398 if (patch_script_file
== NULL
) {
7399 error
= got_error_from_errno2("fopen",
7406 * XXX "c" perm needed on repo dir to delete merge references.
7408 error
= apply_unveil(got_repo_get_path(repo
), 0,
7409 got_worktree_get_root_path(worktree
));
7413 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
7420 TAILQ_FOREACH(pe
, &paths
, entry
) {
7421 if (asprintf(&ondisk_path
, "%s/%s",
7422 got_worktree_get_root_path(worktree
),
7424 error
= got_error_from_errno("asprintf");
7427 if (lstat(ondisk_path
, &sb
) == -1) {
7428 if (errno
== ENOENT
) {
7432 error
= got_error_from_errno2("lstat",
7438 if (S_ISDIR(sb
.st_mode
)) {
7439 error
= got_error_msg(GOT_ERR_BAD_PATH
,
7440 "reverting directories requires -R option");
7446 cpa
.patch_script_file
= patch_script_file
;
7447 cpa
.action
= "revert";
7448 error
= got_worktree_revert(worktree
, &paths
, revert_progress
, NULL
,
7449 pflag
? choose_patch
: NULL
, &cpa
, repo
);
7451 error
= rm_logmsg_ref(worktree
, repo
);
7453 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
7455 error
= got_error_from_errno2("fclose", patch_script_path
);
7457 const struct got_error
*close_err
= got_repo_close(repo
);
7462 got_worktree_close(worktree
);
7464 const struct got_error
*pack_err
=
7465 got_repo_pack_fds_close(pack_fds
);
7469 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
7478 fprintf(stderr
, "usage: %s commit [-CNnS] [-A author] [-F path] "
7479 "[-m message] [path ...]\n", getprogname());
7483 struct collect_commit_logmsg_arg
{
7484 const char *cmdline_log
;
7485 const char *prepared_log
;
7486 const char *merged_log
;
7487 int non_interactive
;
7489 const char *worktree_path
;
7490 const char *repo_path
;
7491 const char *dial_proto
;
7495 static const struct got_error
*
7496 read_prepared_logmsg(char **logmsg
, const char *path
)
7498 const struct got_error
*err
= NULL
;
7504 memset(&sb
, 0, sizeof(sb
));
7506 f
= fopen(path
, "re");
7508 return got_error_from_errno2("fopen", path
);
7510 if (fstat(fileno(f
), &sb
) == -1) {
7511 err
= got_error_from_errno2("fstat", path
);
7514 if (sb
.st_size
== 0) {
7515 err
= got_error(GOT_ERR_COMMIT_MSG_EMPTY
);
7519 *logmsg
= malloc(sb
.st_size
+ 1);
7520 if (*logmsg
== NULL
) {
7521 err
= got_error_from_errno("malloc");
7525 r
= fread(*logmsg
, 1, sb
.st_size
, f
);
7526 if (r
!= sb
.st_size
) {
7528 err
= got_error_from_errno2("fread", path
);
7530 err
= got_error(GOT_ERR_IO
);
7533 (*logmsg
)[sb
.st_size
] = '\0';
7535 if (fclose(f
) == EOF
&& err
== NULL
)
7536 err
= got_error_from_errno2("fclose", path
);
7544 static const struct got_error
*
7545 collect_commit_logmsg(struct got_pathlist_head
*commitable_paths
,
7546 const char *diff_path
, char **logmsg
, void *arg
)
7548 char *initial_content
= NULL
;
7549 struct got_pathlist_entry
*pe
;
7550 const struct got_error
*err
= NULL
;
7551 char *template = NULL
;
7552 char *prepared_msg
= NULL
, *merged_msg
= NULL
;
7553 struct collect_commit_logmsg_arg
*a
= arg
;
7554 int initial_content_len
;
7558 /* if a message was specified on the command line, just use it */
7559 if (a
->cmdline_log
!= NULL
&& *a
->cmdline_log
!= '\0') {
7560 len
= strlen(a
->cmdline_log
) + 1;
7561 *logmsg
= malloc(len
+ 1);
7562 if (*logmsg
== NULL
)
7563 return got_error_from_errno("malloc");
7564 strlcpy(*logmsg
, a
->cmdline_log
, len
);
7566 } else if (a
->prepared_log
!= NULL
&& a
->non_interactive
)
7567 return read_prepared_logmsg(logmsg
, a
->prepared_log
);
7569 if (asprintf(&template, "%s/logmsg", a
->worktree_path
) == -1)
7570 return got_error_from_errno("asprintf");
7572 err
= got_opentemp_named_fd(&a
->logmsg_path
, &fd
, template, "");
7576 if (a
->prepared_log
) {
7577 err
= read_prepared_logmsg(&prepared_msg
, a
->prepared_log
);
7580 } else if (a
->merged_log
) {
7581 err
= read_prepared_logmsg(&merged_msg
, a
->merged_log
);
7586 initial_content_len
= asprintf(&initial_content
,
7587 "%s%s\n# changes to be committed:\n",
7588 prepared_msg
? prepared_msg
: "",
7589 merged_msg
? merged_msg
: "");
7590 if (initial_content_len
== -1) {
7591 err
= got_error_from_errno("asprintf");
7595 if (write(fd
, initial_content
, initial_content_len
) == -1) {
7596 err
= got_error_from_errno2("write", a
->logmsg_path
);
7600 TAILQ_FOREACH(pe
, commitable_paths
, entry
) {
7601 struct got_commitable
*ct
= pe
->data
;
7602 dprintf(fd
, "# %c %s\n",
7603 got_commitable_get_status(ct
),
7604 got_commitable_get_path(ct
));
7608 dprintf(fd
, "# detailed changes can be viewed in %s\n",
7612 if (close(fd
) == -1) {
7613 err
= got_error_from_errno2("close", a
->logmsg_path
);
7618 err
= edit_logmsg(logmsg
, a
->editor
, a
->logmsg_path
, initial_content
,
7619 initial_content_len
, a
->prepared_log
? 0 : 1);
7621 free(initial_content
);
7626 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
7627 err
= got_error_from_errno2("close", a
->logmsg_path
);
7629 /* Editor is done; we can now apply unveil(2) */
7631 err
= got_dial_apply_unveil(a
->dial_proto
);
7633 err
= apply_unveil(a
->repo_path
, 0, a
->worktree_path
);
7641 static const struct got_error
*
7642 cat_logmsg(FILE *f
, struct got_commit_object
*commit
, const char *idstr
,
7643 const char *type
, int has_content
)
7645 const struct got_error
*err
= NULL
;
7646 char *logmsg
= NULL
;
7648 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
7652 if (fprintf(f
, "%s# log message of %s commit %s:%s",
7653 has_content
? "\n" : "", type
, idstr
, logmsg
) < 0)
7654 err
= got_ferror(f
, GOT_ERR_IO
);
7661 * Lookup "logmsg" references of backed-out and cherrypicked commits
7662 * belonging to the current work tree. If found, and the worktree has
7663 * at least one modified file that was changed in the referenced commit,
7664 * add its log message to a new temporary file at *logmsg_path.
7665 * Add all refs found to matched_refs to be scheduled for removal on
7666 * successful commit.
7668 static const struct got_error
*
7669 lookup_logmsg_ref(char **logmsg_path
, struct got_pathlist_head
*paths
,
7670 struct got_reflist_head
*matched_refs
, struct got_worktree
*worktree
,
7671 struct got_repository
*repo
)
7673 const struct got_error
*err
;
7674 struct got_commit_object
*commit
= NULL
;
7675 struct got_object_id
*id
= NULL
;
7676 struct got_reflist_head refs
;
7677 struct got_reflist_entry
*re
, *re_match
;
7679 char *uuidstr
= NULL
;
7680 int added_logmsg
= 0;
7684 *logmsg_path
= NULL
;
7686 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
7690 err
= got_ref_list(&refs
, repo
, "refs/got/worktree",
7691 got_ref_cmp_by_name
, repo
);
7695 TAILQ_FOREACH(re
, &refs
, entry
) {
7696 const char *refname
, *type
;
7697 struct wt_commitable_path_arg wcpa
;
7700 refname
= got_ref_get_name(re
->ref
);
7702 if (strncmp(refname
, GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
7703 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
) == 0) {
7704 refname
+= GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
+ 1;
7705 type
= "cherrypicked";
7706 } else if (strncmp(refname
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
7707 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
) == 0) {
7708 refname
+= GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
+ 1;
7709 type
= "backed-out";
7713 if (strncmp(refname
, uuidstr
, GOT_WORKTREE_UUID_STRLEN
) == 0)
7714 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
7718 err
= got_repo_match_object_id(&id
, NULL
, refname
,
7719 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
7723 err
= got_object_open_as_commit(&commit
, repo
, id
);
7727 wcpa
.commit_paths
= paths
;
7728 wcpa
.has_changes
= &add_logmsg
;
7730 err
= commit_path_changed_in_worktree(&wcpa
, id
,
7737 err
= got_opentemp_named(logmsg_path
, &f
,
7738 "got-commit-logmsg", "");
7742 err
= cat_logmsg(f
, commit
, refname
, type
,
7749 err
= got_reflist_entry_dup(&re_match
, re
);
7752 TAILQ_INSERT_HEAD(matched_refs
, re_match
, entry
);
7755 got_object_commit_close(commit
);
7764 got_ref_list_free(&refs
);
7766 got_object_commit_close(commit
);
7767 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
7768 err
= got_error_from_errno("fclose");
7769 if (!added_logmsg
) {
7770 if (*logmsg_path
&& unlink(*logmsg_path
) != 0 && err
== NULL
)
7771 err
= got_error_from_errno2("unlink", *logmsg_path
);
7772 *logmsg_path
= NULL
;
7777 static const struct got_error
*
7778 cmd_commit(int argc
, char *argv
[])
7780 const struct got_error
*error
= NULL
;
7781 struct got_worktree
*worktree
= NULL
;
7782 struct got_repository
*repo
= NULL
;
7783 char *cwd
= NULL
, *id_str
= NULL
;
7784 struct got_object_id
*id
= NULL
;
7785 const char *logmsg
= NULL
;
7786 char *prepared_logmsg
= NULL
, *merged_logmsg
= NULL
;
7787 struct collect_commit_logmsg_arg cl_arg
;
7788 const char *author
= NULL
;
7789 char *gitconfig_path
= NULL
, *editor
= NULL
, *committer
= NULL
;
7790 int ch
, rebase_in_progress
, histedit_in_progress
, preserve_logmsg
= 0;
7791 int allow_bad_symlinks
= 0, non_interactive
= 0, merge_in_progress
= 0;
7792 int show_diff
= 1, commit_conflicts
= 0;
7793 struct got_pathlist_head paths
;
7794 struct got_reflist_head refs
;
7795 struct got_reflist_entry
*re
;
7796 int *pack_fds
= NULL
;
7797 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
7798 const struct got_remote_repo
*remotes
, *remote
= NULL
;
7800 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
7801 char *repo_name
= NULL
, *server_path
= NULL
;
7802 const char *remote_name
;
7808 cl_arg
.logmsg_path
= NULL
;
7811 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7812 "unveil", NULL
) == -1)
7816 while ((ch
= getopt(argc
, argv
, "A:CF:m:NnS")) != -1) {
7820 error
= valid_author(author
);
7825 commit_conflicts
= 1;
7829 option_conflict('F', 'm');
7830 prepared_logmsg
= realpath(optarg
, NULL
);
7831 if (prepared_logmsg
== NULL
)
7832 return got_error_from_errno2("realpath",
7836 if (prepared_logmsg
)
7837 option_conflict('m', 'F');
7841 non_interactive
= 1;
7847 allow_bad_symlinks
= 1;
7858 cwd
= getcwd(NULL
, 0);
7860 error
= got_error_from_errno("getcwd");
7864 error
= got_repo_pack_fds_open(&pack_fds
);
7868 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
7870 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
7871 error
= wrap_not_worktree_error(error
, "commit", cwd
);
7875 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
7878 if (rebase_in_progress
) {
7879 error
= got_error(GOT_ERR_REBASING
);
7883 error
= got_worktree_histedit_in_progress(&histedit_in_progress
,
7888 error
= get_gitconfig_path(&gitconfig_path
);
7891 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
7892 gitconfig_path
, pack_fds
);
7896 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
, repo
);
7899 if (merge_in_progress
) {
7900 error
= got_error(GOT_ERR_MERGE_BUSY
);
7904 error
= get_author(&committer
, repo
, worktree
);
7911 remote_name
= GOT_SEND_DEFAULT_REMOTE_NAME
;
7912 worktree_conf
= got_worktree_get_gotconfig(worktree
);
7913 if (worktree_conf
) {
7914 got_gotconfig_get_remotes(&nremotes
, &remotes
,
7916 for (i
= 0; i
< nremotes
; i
++) {
7917 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7918 remote
= &remotes
[i
];
7923 if (remote
== NULL
) {
7924 repo_conf
= got_repo_get_gotconfig(repo
);
7926 got_gotconfig_get_remotes(&nremotes
, &remotes
,
7928 for (i
= 0; i
< nremotes
; i
++) {
7929 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7930 remote
= &remotes
[i
];
7936 if (remote
== NULL
) {
7937 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
7938 for (i
= 0; i
< nremotes
; i
++) {
7939 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
7940 remote
= &remotes
[i
];
7945 if (remote
== NULL
) {
7946 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
7950 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
7951 &repo_name
, remote
->fetch_url
);
7955 if (strcmp(proto
, "git") == 0) {
7957 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7958 "sendfd dns inet unveil", NULL
) == -1)
7961 } else if (strcmp(proto
, "git+ssh") == 0 ||
7962 strcmp(proto
, "ssh") == 0) {
7964 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7965 "sendfd unveil", NULL
) == -1)
7968 } else if (strcmp(proto
, "http") == 0 ||
7969 strcmp(proto
, "git+http") == 0) {
7970 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
7973 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
7978 /*if (verbosity >= 0) {
7979 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
7980 remote->name, proto, host,
7981 port ? ":" : "", port ? port : "",
7982 *server_path == '/' ? "" : "/", server_path);
7986 * unveil(2) traverses exec(2); if an editor is used we have
7987 * to apply unveil after the log message has been written during
7990 if (logmsg
== NULL
|| strlen(logmsg
) == 0)
7991 error
= get_editor(&editor
);
7993 error
= got_dial_apply_unveil(proto
);
7996 error
= apply_unveil(got_repo_get_path(repo
), 0,
7997 got_worktree_get_root_path(worktree
));
8002 if (prepared_logmsg
== NULL
) {
8003 error
= lookup_logmsg_ref(&merged_logmsg
,
8004 argc
> 0 ? &paths
: NULL
, &refs
, worktree
, repo
);
8009 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
8013 cl_arg
.editor
= editor
;
8014 cl_arg
.cmdline_log
= logmsg
;
8015 cl_arg
.prepared_log
= prepared_logmsg
;
8016 cl_arg
.merged_log
= merged_logmsg
;
8017 cl_arg
.non_interactive
= non_interactive
;
8018 cl_arg
.worktree_path
= got_worktree_get_root_path(worktree
);
8019 cl_arg
.repo_path
= got_repo_get_path(repo
);
8020 cl_arg
.dial_proto
= proto
;
8021 error
= got_worktree_cvg_commit(&id
, worktree
, &paths
, author
,
8022 committer
, allow_bad_symlinks
, show_diff
, commit_conflicts
,
8023 collect_commit_logmsg
, &cl_arg
, print_status
, NULL
, proto
, host
,
8024 port
, server_path
, verbosity
, remote
, check_cancelled
, repo
);
8026 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
8027 cl_arg
.logmsg_path
!= NULL
)
8028 preserve_logmsg
= 1;
8032 error
= got_object_id_str(&id_str
, id
);
8035 printf("Created commit %s\n", id_str
);
8037 TAILQ_FOREACH(re
, &refs
, entry
) {
8038 error
= got_ref_delete(re
->ref
, repo
);
8044 if (preserve_logmsg
) {
8045 fprintf(stderr
, "%s: log message preserved in %s\n",
8046 getprogname(), cl_arg
.logmsg_path
);
8047 } else if (cl_arg
.logmsg_path
&& unlink(cl_arg
.logmsg_path
) == -1 &&
8049 error
= got_error_from_errno2("unlink", cl_arg
.logmsg_path
);
8050 free(cl_arg
.logmsg_path
);
8051 if (merged_logmsg
&& unlink(merged_logmsg
) == -1 && error
== NULL
)
8052 error
= got_error_from_errno2("unlink", merged_logmsg
);
8053 free(merged_logmsg
);
8055 const struct got_error
*close_err
= got_repo_close(repo
);
8060 got_worktree_close(worktree
);
8062 const struct got_error
*pack_err
=
8063 got_repo_pack_fds_close(pack_fds
);
8067 got_ref_list_free(&refs
);
8068 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
8071 free(gitconfig_path
);
8074 free(prepared_logmsg
);
8079 * Print and if delete is set delete all ref_prefix references.
8080 * If wanted_ref is not NULL, only print or delete this reference.
8082 static const struct got_error
*
8083 process_logmsg_refs(const char *ref_prefix
, size_t prefix_len
,
8084 const char *wanted_ref
, int delete, struct got_worktree
*worktree
,
8085 struct got_repository
*repo
)
8087 const struct got_error
*err
;
8088 struct got_pathlist_head paths
;
8089 struct got_reflist_head refs
;
8090 struct got_reflist_entry
*re
;
8091 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
8092 struct got_commit_object
*commit
= NULL
;
8093 struct got_object_id
*id
= NULL
;
8094 const char *header_prefix
;
8095 char *uuidstr
= NULL
;
8101 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, repo
);
8105 err
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
8109 if (worktree
!= NULL
) {
8110 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
8116 if (strncmp(wanted_ref
, "refs/heads/", 11) == 0)
8120 if (strcmp(ref_prefix
, GOT_WORKTREE_BACKOUT_REF_PREFIX
) == 0)
8121 header_prefix
= "backout";
8123 header_prefix
= "cherrypick";
8125 TAILQ_FOREACH(re
, &refs
, entry
) {
8126 const char *refname
, *wt
;
8128 refname
= got_ref_get_name(re
->ref
);
8130 err
= check_cancelled(NULL
);
8134 if (strncmp(refname
, ref_prefix
, prefix_len
) == 0)
8135 refname
+= prefix_len
+ 1; /* skip '-' delimiter */
8141 if (worktree
== NULL
|| strncmp(refname
, uuidstr
,
8142 GOT_WORKTREE_UUID_STRLEN
) == 0)
8143 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
8147 err
= got_repo_match_object_id(&id
, NULL
, refname
,
8148 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
8152 err
= got_object_open_as_commit(&commit
, repo
, id
);
8157 found
= strncmp(wanted_ref
, refname
,
8158 strlen(wanted_ref
)) == 0;
8159 if (wanted_ref
&& !found
) {
8160 struct got_reflist_head
*ci_refs
;
8162 ci_refs
= got_reflist_object_id_map_lookup(refs_idmap
,
8166 char *refs_str
= NULL
;
8167 char const *r
= NULL
;
8169 err
= build_refs_str(&refs_str
, ci_refs
, id
,
8176 if (strncmp(r
, wanted_ref
,
8177 strlen(wanted_ref
)) == 0) {
8189 if (wanted_ref
== NULL
|| found
) {
8191 err
= got_ref_delete(re
->ref
, repo
);
8194 printf("Deleted: ");
8195 err
= print_commit_oneline(commit
, id
, repo
,
8199 * Print paths modified by commit to help
8200 * associate commits with worktree changes.
8202 err
= get_changed_paths(&paths
, commit
,
8207 err
= print_commit(commit
, id
, repo
, NULL
,
8208 &paths
, NULL
, 0, 0, refs_idmap
, NULL
,
8210 got_pathlist_free(&paths
,
8211 GOT_PATHLIST_FREE_ALL
);
8213 if (worktree
== NULL
)
8214 printf("work tree: %.*s\n\n",
8215 GOT_WORKTREE_UUID_STRLEN
, wt
);
8221 got_object_commit_close(commit
);
8227 if (wanted_ref
!= NULL
&& !found
)
8228 err
= got_error_fmt(GOT_ERR_NOT_REF
, "%s", wanted_ref
);
8233 got_ref_list_free(&refs
);
8234 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_ALL
);
8236 got_reflist_object_id_map_free(refs_idmap
);
8238 got_object_commit_close(commit
);
8243 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
8244 * identified by id for log messages to prepopulate the editor on commit.
8246 static const struct got_error
*
8247 logmsg_ref(struct got_object_id
*id
, const char *prefix
,
8248 struct got_worktree
*worktree
, struct got_repository
*repo
)
8250 const struct got_error
*err
= NULL
;
8251 char *idstr
, *ref
= NULL
, *refname
= NULL
;
8252 int histedit_in_progress
;
8253 int rebase_in_progress
, merge_in_progress
;
8256 * Silenty refuse to create merge reference if any histedit, merge,
8257 * or rebase operation is in progress.
8259 err
= got_worktree_histedit_in_progress(&histedit_in_progress
,
8263 if (histedit_in_progress
)
8266 err
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
8269 if (rebase_in_progress
)
8272 err
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
8276 if (merge_in_progress
)
8279 err
= got_object_id_str(&idstr
, id
);
8283 err
= got_worktree_get_logmsg_ref_name(&refname
, worktree
, prefix
);
8287 if (asprintf(&ref
, "%s-%s", refname
, idstr
) == -1) {
8288 err
= got_error_from_errno("asprintf");
8292 err
= create_ref(ref
, got_worktree_get_base_commit_id(worktree
),
8302 usage_cherrypick(void)
8304 fprintf(stderr
, "usage: %s cherrypick [-lX] [commit-id]\n",
8309 static const struct got_error
*
8310 cmd_cherrypick(int argc
, char *argv
[])
8312 const struct got_error
*error
= NULL
;
8313 struct got_worktree
*worktree
= NULL
;
8314 struct got_repository
*repo
= NULL
;
8315 char *cwd
= NULL
, *commit_id_str
= NULL
;
8316 struct got_object_id
*commit_id
= NULL
;
8317 struct got_commit_object
*commit
= NULL
;
8318 struct got_object_qid
*pid
;
8319 int ch
, list_refs
= 0, remove_refs
= 0;
8320 struct got_update_progress_arg upa
;
8321 int *pack_fds
= NULL
;
8324 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8325 "unveil", NULL
) == -1)
8329 while ((ch
= getopt(argc
, argv
, "lX")) != -1) {
8346 if (list_refs
|| remove_refs
) {
8347 if (argc
!= 0 && argc
!= 1)
8349 } else if (argc
!= 1)
8351 if (list_refs
&& remove_refs
)
8352 option_conflict('l', 'X');
8354 cwd
= getcwd(NULL
, 0);
8356 error
= got_error_from_errno("getcwd");
8360 error
= got_repo_pack_fds_open(&pack_fds
);
8364 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
8366 if (list_refs
|| remove_refs
) {
8367 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
8370 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8371 error
= wrap_not_worktree_error(error
,
8377 error
= got_repo_open(&repo
,
8378 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
8383 error
= apply_unveil(got_repo_get_path(repo
), 0,
8384 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
8388 if (list_refs
|| remove_refs
) {
8389 error
= process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
8390 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
,
8391 argc
== 1 ? argv
[0] : NULL
, remove_refs
, worktree
, repo
);
8395 error
= got_repo_match_object_id(&commit_id
, NULL
, argv
[0],
8396 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
8399 error
= got_object_id_str(&commit_id_str
, commit_id
);
8403 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8406 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
8407 memset(&upa
, 0, sizeof(upa
));
8408 error
= got_worktree_merge_files(worktree
, pid
? &pid
->id
: NULL
,
8409 commit_id
, repo
, update_progress
, &upa
, check_cancelled
,
8414 if (upa
.did_something
) {
8415 error
= logmsg_ref(commit_id
,
8416 GOT_WORKTREE_CHERRYPICK_REF_PREFIX
, worktree
, repo
);
8419 printf("Merged commit %s\n", commit_id_str
);
8421 print_merge_progress_stats(&upa
);
8425 got_object_commit_close(commit
);
8426 free(commit_id_str
);
8428 got_worktree_close(worktree
);
8430 const struct got_error
*close_err
= got_repo_close(repo
);
8435 const struct got_error
*pack_err
=
8436 got_repo_pack_fds_close(pack_fds
);
8447 fprintf(stderr
, "usage: %s backout [-lX] [commit-id]\n", getprogname());
8451 static const struct got_error
*
8452 cmd_backout(int argc
, char *argv
[])
8454 const struct got_error
*error
= NULL
;
8455 struct got_worktree
*worktree
= NULL
;
8456 struct got_repository
*repo
= NULL
;
8457 char *cwd
= NULL
, *commit_id_str
= NULL
;
8458 struct got_object_id
*commit_id
= NULL
;
8459 struct got_commit_object
*commit
= NULL
;
8460 struct got_object_qid
*pid
;
8461 int ch
, list_refs
= 0, remove_refs
= 0;
8462 struct got_update_progress_arg upa
;
8463 int *pack_fds
= NULL
;
8466 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8467 "unveil", NULL
) == -1)
8471 while ((ch
= getopt(argc
, argv
, "lX")) != -1) {
8488 if (list_refs
|| remove_refs
) {
8489 if (argc
!= 0 && argc
!= 1)
8491 } else if (argc
!= 1)
8493 if (list_refs
&& remove_refs
)
8494 option_conflict('l', 'X');
8496 cwd
= getcwd(NULL
, 0);
8498 error
= got_error_from_errno("getcwd");
8502 error
= got_repo_pack_fds_open(&pack_fds
);
8506 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
8508 if (list_refs
|| remove_refs
) {
8509 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
8512 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8513 error
= wrap_not_worktree_error(error
,
8519 error
= got_repo_open(&repo
,
8520 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
8525 error
= apply_unveil(got_repo_get_path(repo
), 0,
8526 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
8530 if (list_refs
|| remove_refs
) {
8531 error
= process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX
,
8532 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
,
8533 argc
== 1 ? argv
[0] : NULL
, remove_refs
, worktree
, repo
);
8537 error
= got_repo_match_object_id(&commit_id
, NULL
, argv
[0],
8538 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
8541 error
= got_object_id_str(&commit_id_str
, commit_id
);
8545 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8548 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
8550 error
= got_error(GOT_ERR_ROOT_COMMIT
);
8554 memset(&upa
, 0, sizeof(upa
));
8555 error
= got_worktree_merge_files(worktree
, commit_id
, &pid
->id
,
8556 repo
, update_progress
, &upa
, check_cancelled
, NULL
);
8560 if (upa
.did_something
) {
8561 error
= logmsg_ref(commit_id
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
8565 printf("Backed out commit %s\n", commit_id_str
);
8567 print_merge_progress_stats(&upa
);
8571 got_object_commit_close(commit
);
8572 free(commit_id_str
);
8574 got_worktree_close(worktree
);
8576 const struct got_error
*close_err
= got_repo_close(repo
);
8581 const struct got_error
*pack_err
=
8582 got_repo_pack_fds_close(pack_fds
);
8592 fprintf(stderr
, "usage: %s cat [-P] [-c commit] [-r repository-path] "
8593 "arg ...\n", getprogname());
8597 static const struct got_error
*
8598 cat_blob(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
8600 const struct got_error
*err
;
8601 struct got_blob_object
*blob
;
8604 fd
= got_opentempfd();
8606 return got_error_from_errno("got_opentempfd");
8608 err
= got_object_open_as_blob(&blob
, repo
, id
, 8192, fd
);
8612 err
= got_object_blob_dump_to_file(NULL
, NULL
, NULL
, outfile
, blob
);
8614 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
8615 err
= got_error_from_errno("close");
8617 got_object_blob_close(blob
);
8621 static const struct got_error
*
8622 cat_tree(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
8624 const struct got_error
*err
;
8625 struct got_tree_object
*tree
;
8628 err
= got_object_open_as_tree(&tree
, repo
, id
);
8632 nentries
= got_object_tree_get_nentries(tree
);
8633 for (i
= 0; i
< nentries
; i
++) {
8634 struct got_tree_entry
*te
;
8636 if (sigint_received
|| sigpipe_received
)
8638 te
= got_object_tree_get_entry(tree
, i
);
8639 err
= got_object_id_str(&id_str
, got_tree_entry_get_id(te
));
8642 fprintf(outfile
, "%s %.7o %s\n", id_str
,
8643 got_tree_entry_get_mode(te
),
8644 got_tree_entry_get_name(te
));
8648 got_object_tree_close(tree
);
8652 static const struct got_error
*
8653 cat_commit(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
8655 const struct got_error
*err
;
8656 struct got_commit_object
*commit
;
8657 const struct got_object_id_queue
*parent_ids
;
8658 struct got_object_qid
*pid
;
8659 char *id_str
= NULL
;
8660 const char *logmsg
= NULL
;
8663 err
= got_object_open_as_commit(&commit
, repo
, id
);
8667 err
= got_object_id_str(&id_str
, got_object_commit_get_tree_id(commit
));
8671 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_TREE
, id_str
);
8672 parent_ids
= got_object_commit_get_parent_ids(commit
);
8673 fprintf(outfile
, "numparents %d\n",
8674 got_object_commit_get_nparents(commit
));
8675 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
8677 err
= got_object_id_str(&pid_str
, &pid
->id
);
8680 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_PARENT
, pid_str
);
8683 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
8684 got_object_commit_get_author_gmtoff(commit
));
8685 fprintf(outfile
, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR
,
8686 got_object_commit_get_author(commit
),
8687 (long long)got_object_commit_get_author_time(commit
),
8690 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
8691 got_object_commit_get_committer_gmtoff(commit
));
8692 fprintf(outfile
, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER
,
8693 got_object_commit_get_committer(commit
),
8694 (long long)got_object_commit_get_committer_time(commit
),
8697 logmsg
= got_object_commit_get_logmsg_raw(commit
);
8698 fprintf(outfile
, "messagelen %zd\n", strlen(logmsg
));
8699 fprintf(outfile
, "%s", logmsg
);
8702 got_object_commit_close(commit
);
8706 static const struct got_error
*
8707 cat_tag(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
8709 const struct got_error
*err
;
8710 struct got_tag_object
*tag
;
8711 char *id_str
= NULL
;
8712 const char *tagmsg
= NULL
;
8715 err
= got_object_open_as_tag(&tag
, repo
, id
);
8719 err
= got_object_id_str(&id_str
, got_object_tag_get_object_id(tag
));
8723 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_OBJECT
, id_str
);
8725 switch (got_object_tag_get_object_type(tag
)) {
8726 case GOT_OBJ_TYPE_BLOB
:
8727 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
8728 GOT_OBJ_LABEL_BLOB
);
8730 case GOT_OBJ_TYPE_TREE
:
8731 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
8732 GOT_OBJ_LABEL_TREE
);
8734 case GOT_OBJ_TYPE_COMMIT
:
8735 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
8736 GOT_OBJ_LABEL_COMMIT
);
8738 case GOT_OBJ_TYPE_TAG
:
8739 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
8746 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TAG
,
8747 got_object_tag_get_name(tag
));
8749 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
8750 got_object_tag_get_tagger_gmtoff(tag
));
8751 fprintf(outfile
, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER
,
8752 got_object_tag_get_tagger(tag
),
8753 (long long)got_object_tag_get_tagger_time(tag
),
8756 tagmsg
= got_object_tag_get_message(tag
);
8757 fprintf(outfile
, "messagelen %zd\n", strlen(tagmsg
));
8758 fprintf(outfile
, "%s", tagmsg
);
8761 got_object_tag_close(tag
);
8765 static const struct got_error
*
8766 cmd_cat(int argc
, char *argv
[])
8768 const struct got_error
*error
;
8769 struct got_repository
*repo
= NULL
;
8770 struct got_worktree
*worktree
= NULL
;
8771 char *cwd
= NULL
, *repo_path
= NULL
, *label
= NULL
;
8772 const char *commit_id_str
= NULL
;
8773 struct got_object_id
*id
= NULL
, *commit_id
= NULL
;
8774 struct got_commit_object
*commit
= NULL
;
8775 int ch
, obj_type
, i
, force_path
= 0;
8776 struct got_reflist_head refs
;
8777 int *pack_fds
= NULL
;
8782 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8787 while ((ch
= getopt(argc
, argv
, "c:Pr:")) != -1) {
8790 commit_id_str
= optarg
;
8796 repo_path
= realpath(optarg
, NULL
);
8797 if (repo_path
== NULL
)
8798 return got_error_from_errno2("realpath",
8800 got_path_strip_trailing_slashes(repo_path
);
8811 cwd
= getcwd(NULL
, 0);
8813 error
= got_error_from_errno("getcwd");
8817 error
= got_repo_pack_fds_open(&pack_fds
);
8821 if (repo_path
== NULL
) {
8822 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
8823 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
8827 got_worktree_get_repo_path(worktree
));
8828 if (repo_path
== NULL
) {
8829 error
= got_error_from_errno("strdup");
8833 /* Release work tree lock. */
8834 got_worktree_close(worktree
);
8839 if (repo_path
== NULL
) {
8840 repo_path
= strdup(cwd
);
8841 if (repo_path
== NULL
)
8842 return got_error_from_errno("strdup");
8845 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
8850 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
8854 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
8858 if (commit_id_str
== NULL
)
8859 commit_id_str
= GOT_REF_HEAD
;
8860 error
= got_repo_match_object_id(&commit_id
, NULL
,
8861 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
8865 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8869 for (i
= 0; i
< argc
; i
++) {
8871 error
= got_object_id_by_path(&id
, repo
, commit
,
8876 error
= got_repo_match_object_id(&id
, &label
, argv
[i
],
8877 GOT_OBJ_TYPE_ANY
, NULL
/* do not resolve tags */,
8880 if (error
->code
!= GOT_ERR_BAD_OBJ_ID_STR
&&
8881 error
->code
!= GOT_ERR_NOT_REF
)
8883 error
= got_object_id_by_path(&id
, repo
,
8890 error
= got_object_get_type(&obj_type
, repo
, id
);
8895 case GOT_OBJ_TYPE_BLOB
:
8896 error
= cat_blob(id
, repo
, stdout
);
8898 case GOT_OBJ_TYPE_TREE
:
8899 error
= cat_tree(id
, repo
, stdout
);
8901 case GOT_OBJ_TYPE_COMMIT
:
8902 error
= cat_commit(id
, repo
, stdout
);
8904 case GOT_OBJ_TYPE_TAG
:
8905 error
= cat_tag(id
, repo
, stdout
);
8908 error
= got_error(GOT_ERR_OBJ_TYPE
);
8923 got_object_commit_close(commit
);
8925 got_worktree_close(worktree
);
8927 const struct got_error
*close_err
= got_repo_close(repo
);
8932 const struct got_error
*pack_err
=
8933 got_repo_pack_fds_close(pack_fds
);
8938 got_ref_list_free(&refs
);
8945 fprintf(stderr
, "usage: %s info [path ...]\n",
8950 static const struct got_error
*
8951 print_path_info(void *arg
, const char *path
, mode_t mode
, time_t mtime
,
8952 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
8953 struct got_object_id
*commit_id
)
8955 const struct got_error
*err
= NULL
;
8956 char *id_str
= NULL
;
8958 struct tm mytm
, *tm
;
8959 struct got_pathlist_head
*paths
= arg
;
8960 struct got_pathlist_entry
*pe
;
8963 * Clear error indication from any of the path arguments which
8964 * would cause this file index entry to be displayed.
8966 TAILQ_FOREACH(pe
, paths
, entry
) {
8967 if (got_path_cmp(path
, pe
->path
, strlen(path
),
8968 pe
->path_len
) == 0 ||
8969 got_path_is_child(path
, pe
->path
, pe
->path_len
))
8970 pe
->data
= NULL
; /* no error */
8973 printf(GOT_COMMIT_SEP_STR
);
8975 printf("symlink: %s\n", path
);
8976 else if (S_ISREG(mode
)) {
8977 printf("file: %s\n", path
);
8978 printf("mode: %o\n", mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
));
8979 } else if (S_ISDIR(mode
))
8980 printf("directory: %s\n", path
);
8982 printf("something: %s\n", path
);
8984 tm
= localtime_r(&mtime
, &mytm
);
8987 if (strftime(datebuf
, sizeof(datebuf
), "%c %Z", tm
) == 0)
8988 return got_error(GOT_ERR_NO_SPACE
);
8989 printf("timestamp: %s\n", datebuf
);
8992 err
= got_object_id_str(&id_str
, blob_id
);
8995 printf("based on blob: %s\n", id_str
);
8999 if (staged_blob_id
) {
9000 err
= got_object_id_str(&id_str
, staged_blob_id
);
9003 printf("based on staged blob: %s\n", id_str
);
9008 err
= got_object_id_str(&id_str
, commit_id
);
9011 printf("based on commit: %s\n", id_str
);
9018 static const struct got_error
*
9019 cmd_info(int argc
, char *argv
[])
9021 const struct got_error
*error
= NULL
;
9022 struct got_repository
*repo
= NULL
;
9023 struct got_worktree
*worktree
= NULL
;
9024 struct got_fileindex
*fileindex
= NULL
;
9025 char *cwd
= NULL
, *id_str
= NULL
;
9026 struct got_pathlist_head paths
;
9027 char *uuidstr
= NULL
;
9028 int ch
, show_files
= 0;
9029 int *pack_fds
= NULL
;
9034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9039 while ((ch
= getopt(argc
, argv
, "")) != -1) {
9050 cwd
= getcwd(NULL
, 0);
9052 error
= got_error_from_errno("getcwd");
9056 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_CVG_DIR
);
9058 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
9059 error
= wrap_not_worktree_error(error
, "info", cwd
);
9063 error
= got_repo_pack_fds_open(&pack_fds
);
9067 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
), NULL
,
9073 /* Remove "wpath cpath proc exec sendfd" promises. */
9074 if (pledge("stdio rpath flock unveil", NULL
) == -1)
9077 error
= apply_unveil(NULL
, 0, got_worktree_get_root_path(worktree
));
9082 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
9089 error
= got_worktree_path_info_prepare(&fileindex
, worktree
, repo
);
9093 error
= got_object_id_str(&id_str
,
9094 got_worktree_get_base_commit_id(worktree
));
9098 error
= got_worktree_get_uuid(&uuidstr
, worktree
);
9102 printf("work tree: %s\n", got_worktree_get_root_path(worktree
));
9103 printf("work tree base commit: %s\n", id_str
);
9104 printf("work tree path prefix: %s\n",
9105 got_worktree_get_path_prefix(worktree
));
9106 printf("work tree branch reference: %s\n",
9107 got_worktree_get_head_ref_name(worktree
));
9108 printf("work tree UUID: %s\n", uuidstr
);
9109 printf("work tree format version: %d\n",
9110 got_worktree_get_format_version(worktree
));
9111 printf("file index version: %u\n",
9112 got_worktree_get_fileindex_version(fileindex
));
9113 printf("repository: %s\n", got_worktree_get_repo_path(worktree
));
9116 struct got_pathlist_entry
*pe
;
9117 TAILQ_FOREACH(pe
, &paths
, entry
) {
9118 if (pe
->path_len
== 0)
9121 * Assume this path will fail. This will be corrected
9122 * in print_path_info() in case the path does suceeed.
9124 pe
->data
= (void *)got_error(GOT_ERR_BAD_PATH
);
9126 error
= got_worktree_path_info(worktree
, fileindex
, &paths
,
9127 print_path_info
, &paths
, check_cancelled
, NULL
);
9130 TAILQ_FOREACH(pe
, &paths
, entry
) {
9131 if (pe
->data
!= NULL
) {
9132 const struct got_error
*perr
;
9135 error
= got_error_path(pe
->path
, perr
->code
);
9142 const struct got_error
*cerr
;
9144 cerr
= got_worktree_path_info_complete(fileindex
, worktree
);
9147 got_worktree_close(worktree
);
9150 const struct got_error
*close_err
= got_repo_close(repo
);
9155 const struct got_error
*pack_err
=
9156 got_repo_pack_fds_close(pack_fds
);
9160 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);