Merge branch 'nd/worktree-cleanup-post-head-protection'
Further preparatory clean-up for "worktree" feature continues. * nd/worktree-cleanup-post-head-protection: worktree: simplify prefixing paths worktree: avoid 0{40}, too many zeroes, hard to read worktree.c: use is_dot_or_dotdot() git-worktree.txt: keep subcommand listing in alphabetical order worktree.c: rewrite mark_current_worktree() to avoid strbuf completion: support git-worktree
This commit is contained in:
commit
7a738b40f6
@ -10,8 +10,8 @@ SYNOPSIS
|
|||||||
--------
|
--------
|
||||||
[verse]
|
[verse]
|
||||||
'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
|
'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
|
||||||
'git worktree prune' [-n] [-v] [--expire <expire>]
|
|
||||||
'git worktree list' [--porcelain]
|
'git worktree list' [--porcelain]
|
||||||
|
'git worktree prune' [-n] [-v] [--expire <expire>]
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
@ -55,10 +55,6 @@ If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,
|
|||||||
then, as a convenience, a new branch based at HEAD is created automatically,
|
then, as a convenience, a new branch based at HEAD is created automatically,
|
||||||
as if `-b $(basename <path>)` was specified.
|
as if `-b $(basename <path>)` was specified.
|
||||||
|
|
||||||
prune::
|
|
||||||
|
|
||||||
Prune working tree information in $GIT_DIR/worktrees.
|
|
||||||
|
|
||||||
list::
|
list::
|
||||||
|
|
||||||
List details of each worktree. The main worktree is listed first, followed by
|
List details of each worktree. The main worktree is listed first, followed by
|
||||||
@ -66,6 +62,10 @@ each of the linked worktrees. The output details include if the worktree is
|
|||||||
bare, the revision currently checked out, and the branch currently checked out
|
bare, the revision currently checked out, and the branch currently checked out
|
||||||
(or 'detached HEAD' if none).
|
(or 'detached HEAD' if none).
|
||||||
|
|
||||||
|
prune::
|
||||||
|
|
||||||
|
Prune working tree information in $GIT_DIR/worktrees.
|
||||||
|
|
||||||
OPTIONS
|
OPTIONS
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
static const char * const worktree_usage[] = {
|
static const char * const worktree_usage[] = {
|
||||||
N_("git worktree add [<options>] <path> [<branch>]"),
|
N_("git worktree add [<options>] <path> [<branch>]"),
|
||||||
N_("git worktree prune [<options>]"),
|
|
||||||
N_("git worktree list [<options>]"),
|
N_("git worktree list [<options>]"),
|
||||||
|
N_("git worktree prune [<options>]"),
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ static void prune_worktrees(void)
|
|||||||
if (!dir)
|
if (!dir)
|
||||||
return;
|
return;
|
||||||
while ((d = readdir(dir)) != NULL) {
|
while ((d = readdir(dir)) != NULL) {
|
||||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
if (is_dot_or_dotdot(d->d_name))
|
||||||
continue;
|
continue;
|
||||||
strbuf_reset(&reason);
|
strbuf_reset(&reason);
|
||||||
if (!prune_worktree(d->d_name, &reason))
|
if (!prune_worktree(d->d_name, &reason))
|
||||||
@ -262,7 +262,7 @@ static int add_worktree(const char *path, const char *refname,
|
|||||||
*/
|
*/
|
||||||
strbuf_reset(&sb);
|
strbuf_reset(&sb);
|
||||||
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
|
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
|
||||||
write_file(sb.buf, "0000000000000000000000000000000000000000");
|
write_file(sb.buf, sha1_to_hex(null_sha1));
|
||||||
strbuf_reset(&sb);
|
strbuf_reset(&sb);
|
||||||
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
|
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
|
||||||
write_file(sb.buf, "../..");
|
write_file(sb.buf, "../..");
|
||||||
@ -337,7 +337,7 @@ static int add(int ac, const char **av, const char *prefix)
|
|||||||
if (ac < 1 || ac > 2)
|
if (ac < 1 || ac > 2)
|
||||||
usage_with_options(worktree_usage, options);
|
usage_with_options(worktree_usage, options);
|
||||||
|
|
||||||
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
|
path = prefix_filename(prefix, strlen(prefix), av[0]);
|
||||||
branch = ac < 2 ? "HEAD" : av[1];
|
branch = ac < 2 ? "HEAD" : av[1];
|
||||||
|
|
||||||
if (!strcmp(branch, "-"))
|
if (!strcmp(branch, "-"))
|
||||||
@ -470,6 +470,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
|
|||||||
|
|
||||||
if (ac < 2)
|
if (ac < 2)
|
||||||
usage_with_options(worktree_usage, options);
|
usage_with_options(worktree_usage, options);
|
||||||
|
if (!prefix)
|
||||||
|
prefix = "";
|
||||||
if (!strcmp(av[1], "add"))
|
if (!strcmp(av[1], "add"))
|
||||||
return add(ac - 1, av + 1, prefix);
|
return add(ac - 1, av + 1, prefix);
|
||||||
if (!strcmp(av[1], "prune"))
|
if (!strcmp(av[1], "prune"))
|
||||||
|
@ -2691,6 +2691,29 @@ _git_whatchanged ()
|
|||||||
_git_log
|
_git_log
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_git_worktree ()
|
||||||
|
{
|
||||||
|
local subcommands="add list prune"
|
||||||
|
local subcommand="$(__git_find_on_cmdline "$subcommands")"
|
||||||
|
if [ -z "$subcommand" ]; then
|
||||||
|
__gitcomp "$subcommands"
|
||||||
|
else
|
||||||
|
case "$subcommand,$cur" in
|
||||||
|
add,--*)
|
||||||
|
__gitcomp "--detach"
|
||||||
|
;;
|
||||||
|
list,--*)
|
||||||
|
__gitcomp "--porcelain"
|
||||||
|
;;
|
||||||
|
prune,--*)
|
||||||
|
__gitcomp "--dry-run --expire --verbose"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
__git_main ()
|
__git_main ()
|
||||||
{
|
{
|
||||||
local i c=1 command __git_dir
|
local i c=1 command __git_dir
|
||||||
|
18
worktree.c
18
worktree.c
@ -153,21 +153,19 @@ done:
|
|||||||
|
|
||||||
static void mark_current_worktree(struct worktree **worktrees)
|
static void mark_current_worktree(struct worktree **worktrees)
|
||||||
{
|
{
|
||||||
struct strbuf git_dir = STRBUF_INIT;
|
char *git_dir = xstrdup(absolute_path(get_git_dir()));
|
||||||
struct strbuf path = STRBUF_INIT;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
strbuf_addstr(&git_dir, absolute_path(get_git_dir()));
|
|
||||||
for (i = 0; worktrees[i]; i++) {
|
for (i = 0; worktrees[i]; i++) {
|
||||||
struct worktree *wt = worktrees[i];
|
struct worktree *wt = worktrees[i];
|
||||||
strbuf_addstr(&path, absolute_path(get_worktree_git_dir(wt)));
|
const char *wt_git_dir = get_worktree_git_dir(wt);
|
||||||
wt->is_current = !fspathcmp(git_dir.buf, path.buf);
|
|
||||||
strbuf_reset(&path);
|
if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
|
||||||
if (wt->is_current)
|
wt->is_current = 1;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
strbuf_release(&git_dir);
|
free(git_dir);
|
||||||
strbuf_release(&path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct worktree **get_worktrees(void)
|
struct worktree **get_worktrees(void)
|
||||||
@ -189,7 +187,7 @@ struct worktree **get_worktrees(void)
|
|||||||
if (dir) {
|
if (dir) {
|
||||||
while ((d = readdir(dir)) != NULL) {
|
while ((d = readdir(dir)) != NULL) {
|
||||||
struct worktree *linked = NULL;
|
struct worktree *linked = NULL;
|
||||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
if (is_dot_or_dotdot(d->d_name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ((linked = get_linked_worktree(d->d_name))) {
|
if ((linked = get_linked_worktree(d->d_name))) {
|
||||||
|
Loading…
Reference in New Issue
Block a user