From b462c024022e3a41821f24bca9e43f16973693db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:51 +0700 Subject: completion: support git-worktree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds bare-bone completion support for git-worktree. More advanced completion (e.g. ref completion in git-worktree-add) can be added later. --force completion in "worktree add" is left out because that option should be handled with care. Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 34024754d..951a186df 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -2595,6 +2595,29 @@ _git_whatchanged () _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 () { local i c=1 command __git_dir -- cgit v1.2.1 From 360af2dadaae70f29de2f21d4eb8ac38aefcc263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:52 +0700 Subject: worktree.c: rewrite mark_current_worktree() to avoid strbuf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit strbuf is a bit overkill for this function. What we need is to call absolute_path() twice and make sure the second call does not destroy the result of the first. One buffer allocation is enough. Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- worktree.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/worktree.c b/worktree.c index 4817d6005..6a11611e6 100644 --- a/worktree.c +++ b/worktree.c @@ -153,21 +153,19 @@ done: static void mark_current_worktree(struct worktree **worktrees) { - struct strbuf git_dir = STRBUF_INIT; - struct strbuf path = STRBUF_INIT; + char *git_dir = xstrdup(absolute_path(get_git_dir())); int i; - strbuf_addstr(&git_dir, absolute_path(get_git_dir())); for (i = 0; worktrees[i]; i++) { struct worktree *wt = worktrees[i]; - strbuf_addstr(&path, absolute_path(get_worktree_git_dir(wt))); - wt->is_current = !fspathcmp(git_dir.buf, path.buf); - strbuf_reset(&path); - if (wt->is_current) + const char *wt_git_dir = get_worktree_git_dir(wt); + + if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { + wt->is_current = 1; break; + } } - strbuf_release(&git_dir); - strbuf_release(&path); + free(git_dir); } struct worktree **get_worktrees(void) -- cgit v1.2.1 From 7b722d906bd5b32f3c7a63fb77835b38c1e04e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:53 +0700 Subject: git-worktree.txt: keep subcommand listing in alphabetical order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is probably not the best order. But it makes it no-brainer to know where to insert new commands. At some point we might want to reorder at least the synopsis part again, grouping commonly use subcommands together. Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- Documentation/git-worktree.txt | 10 +++++----- builtin/worktree.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index c62234538..27feff6db 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -10,8 +10,8 @@ SYNOPSIS -------- [verse] 'git worktree add' [-f] [--detach] [--checkout] [-b ] [] -'git worktree prune' [-n] [-v] [--expire ] 'git worktree list' [--porcelain] +'git worktree prune' [-n] [-v] [--expire ] DESCRIPTION ----------- @@ -54,10 +54,6 @@ If `` is omitted and neither `-b` nor `-B` nor `--detached` used, then, as a convenience, a new branch based at HEAD is created automatically, as if `-b $(basename )` was specified. -prune:: - -Prune working tree information in $GIT_DIR/worktrees. - list:: List details of each worktree. The main worktree is listed first, followed by @@ -65,6 +61,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 (or 'detached HEAD' if none). +prune:: + +Prune working tree information in $GIT_DIR/worktrees. + OPTIONS ------- diff --git a/builtin/worktree.c b/builtin/worktree.c index 12c0af723..bf80111fd 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -13,8 +13,8 @@ static const char * const worktree_usage[] = { N_("git worktree add [] []"), - N_("git worktree prune []"), N_("git worktree list []"), + N_("git worktree prune []"), NULL }; -- cgit v1.2.1 From afb9e30b2cadfa010975b29e0ff7459d4b7ae953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:54 +0700 Subject: worktree.c: use is_dot_or_dotdot() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- builtin/worktree.c | 2 +- worktree.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index bf80111fd..aaee0e2fe 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -95,7 +95,7 @@ static void prune_worktrees(void) if (!dir) return; while ((d = readdir(dir)) != NULL) { - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + if (is_dot_or_dotdot(d->d_name)) continue; strbuf_reset(&reason); if (!prune_worktree(d->d_name, &reason)) diff --git a/worktree.c b/worktree.c index 6a11611e6..f4a4f3809 100644 --- a/worktree.c +++ b/worktree.c @@ -187,7 +187,7 @@ struct worktree **get_worktrees(void) if (dir) { while ((d = readdir(dir)) != NULL) { struct worktree *linked = NULL; - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + if (is_dot_or_dotdot(d->d_name)) continue; if ((linked = get_linked_worktree(d->d_name))) { -- cgit v1.2.1 From ef23c347cf363118e236ae72fe6372443fae1ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:55 +0700 Subject: worktree: avoid 0{40}, too many zeroes, hard to read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- builtin/worktree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index aaee0e2fe..b53f8024f 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -262,7 +262,7 @@ static int add_worktree(const char *path, const char *refname, */ strbuf_reset(&sb); 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_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); -- cgit v1.2.1 From 0409e0b6dc169b07c05d02e8b62389b1cc975a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 22 May 2016 16:33:56 +0700 Subject: worktree: simplify prefixing paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also makes slash conversion always happen on Windows (a side effect of prefix_filename). Which is a good thing. Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Eric Sunshine Signed-off-by: Junio C Hamano --- builtin/worktree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index b53f8024f..f9dac376f 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -337,7 +337,7 @@ static int add(int ac, const char **av, const char *prefix) if (ac < 1 || ac > 2) 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]; opts.force_new_branch = !!new_branch_force; @@ -467,6 +467,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix) if (ac < 2) usage_with_options(worktree_usage, options); + if (!prefix) + prefix = ""; if (!strcmp(av[1], "add")) return add(ac - 1, av + 1, prefix); if (!strcmp(av[1], "prune")) -- cgit v1.2.1