threads / patch / 40697

patch, 4 partsupload-pack: strip refs before calling ref_is_hidden()

Subject: [PATCH 2/4] upload-pack: strip refs before calling ref_is_hidden()

## tl;dr

10 messages between Nov 1, 2015 and Nov 2, 2015. Diffs are folded; open one to read it.

replies: 9people: 2as markdown or json

Lukas Fleischer· Nov 1, 2015, 19:34 UTC · lore

[PATCH 0/4] Improve hideRefs when used with namespaces

This is a first set of patches improving documentation and behavior of the transfer.hideRefs feature as discussed in [1]. In particular, hideRefs is changed to generally match stripped refs by default and match full refs when prefixed with "^". The documentation is updated accordingly. Basic tests are added.

[1] http://marc.info/?l=git&m=144604694223920
Lukas Fleischer (4):
  Document the semantics of hideRefs with namespaces
  upload-pack: strip refs before calling ref_is_hidden()
  Add support for matching full refs in hideRefs
  t5509: add basic tests for hideRefs
 Documentation/config.txt         |  8 ++++++++
 builtin/receive-pack.c           | 22 ++++++++++++++++------
 refs.c                           | 14 +++++++++++---
 refs.h                           |  2 +-
 t/t5509-fetch-push-namespaces.sh | 29 +++++++++++++++++++++++++++++
 upload-pack.c                    | 13 ++++++++-----
 6 files changed, 73 insertions(+), 15 deletions(-)
-- 
2.6.2
Lukas Fleischer· Nov 1, 2015, 19:34 UTC · re: Lukas Fleischer · lore

[PATCH 1/4] Document the semantics of hideRefs with namespaces

Right now, there is no clear definition of how transfer.hideRefs should behave when a namespace is set. Explain that hideRefs prefixes match stripped names in that case. This is how hideRefs patterns are currently handled in receive-pack.

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
---
 Documentation/config.txt | 7 +++++++
 1 file changed, 7 insertions(+)
Show changes to Documentation/config.txt +7 −0
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 1204072..3da97a1 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2684,6 +2684,13 @@ You may also include a `!` in front of the ref name to negate the entry,
 explicitly exposing it, even if an earlier entry marked it as hidden.
 If you have multiple hideRefs values, later entries override earlier ones
 (and entries in more-specific config files override less-specific ones).
++
+If a namespace is set, references are stripped before matching. For example, if
+the prefix `refs/heads/master` is specified in `transfer.hideRefs` and the
+current namespace is `foo`, then `refs/namespaces/foo/refs/heads/master` is
+omitted from the advertisements but `refs/heads/master` and
+`refs/namespaces/bar/refs/heads/master` are still advertised as so-called
+"have" lines.
 
 transfer.unpackLimit::
 	When `fetch.unpackLimit` or `receive.unpackLimit` are
-- 
2.6.2
Lukas Fleischer· Nov 1, 2015, 19:34 UTC · re: Lukas Fleischer · lore

Make hideRefs handling in upload-pack consistent with the behavior described in the documentation by stripping refs before comparing them with prefixes in hideRefs.

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
---
 upload-pack.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Show changes to upload-pack.c +3 −3
diff --git a/upload-pack.c b/upload-pack.c
index d0bc3ca..4ca960e 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -692,7 +692,7 @@ static int mark_our_ref(const char *refname, const struct object_id *oid)
 {
 	struct object *o = lookup_unknown_object(oid->hash);
 
-	if (ref_is_hidden(refname)) {
+	if (refname && ref_is_hidden(refname)) {
 		o->flags |= HIDDEN_REF;
 		return 1;
 	}
@@ -703,7 +703,7 @@ static int mark_our_ref(const char *refname, const struct object_id *oid)
 static int check_ref(const char *refname, const struct object_id *oid,
 		     int flag, void *cb_data)
 {
-	mark_our_ref(refname, oid);
+	mark_our_ref(strip_namespace(refname), oid);
 	return 0;
 }
 
@@ -726,7 +726,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
 	const char *refname_nons = strip_namespace(refname);
 	struct object_id peeled;
 
-	if (mark_our_ref(refname, oid))
+	if (mark_our_ref(refname_nons, oid))
 		return 0;
 
 	if (capabilities) {
-- 
2.6.2
Lukas Fleischer· Nov 1, 2015, 19:34 UTC · re: Lukas Fleischer · lore

[PATCH 3/4] Add support for matching full refs in hideRefs

In addition to matching stripped refs, one can now add hideRefs patterns that the full (unstripped) ref is matched against. To distinguish between stripped and full matches, those new patterns must be prefixed with a circumflex (^).

This commit also removes support for the undocumented and unintended hideRefs settings "have" (suppressing all "have" lines) and "capabilities^{}" (suppressing the capabilities line).

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
---
 Documentation/config.txt |  3 ++-
 builtin/receive-pack.c   | 22 ++++++++++++++++------
 refs.c                   | 14 +++++++++++---
 refs.h                   |  2 +-
 upload-pack.c            | 13 ++++++++-----
 5 files changed, 38 insertions(+), 16 deletions(-)
Show changes to 5 files +38 −16

Documentation/config.txt, builtin/receive-pack.c, refs.c, refs.h, upload-pack.c

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 3da97a1..91ed6a5 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2690,7 +2690,8 @@ the prefix `refs/heads/master` is specified in `transfer.hideRefs` and the
 current namespace is `foo`, then `refs/namespaces/foo/refs/heads/master` is
 omitted from the advertisements but `refs/heads/master` and
 `refs/namespaces/bar/refs/heads/master` are still advertised as so-called
-"have" lines.
+"have" lines. In order to match refs before stripping, add a `^` in front of
+the ref name. If you combine `!` and `^`, `!` must be specified first.
 
 transfer.unpackLimit::
 	When `fetch.unpackLimit` or `receive.unpackLimit` are
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index bcb624b..d5e58e0 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -195,9 +195,6 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 
 static void show_ref(const char *path, const unsigned char *sha1)
 {
-	if (ref_is_hidden(path))
-		return;
-
 	if (sent_capabilities) {
 		packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
 	} else {
@@ -219,9 +216,14 @@ static void show_ref(const char *path, const unsigned char *sha1)
 	}
 }
 
-static int show_ref_cb(const char *path, const struct object_id *oid, int flag, void *unused)
+static int show_ref_cb(const char *path_full, const struct object_id *oid,
+		       int flag, void *unused)
 {
-	path = strip_namespace(path);
+	const char *path = strip_namespace(path_full);
+
+	if (ref_is_hidden(path, path_full))
+		return 1;
+
 	/*
 	 * Advertise refs outside our current namespace as ".have"
 	 * refs, so that the client can use them to minimize data
@@ -1198,7 +1200,15 @@ static void reject_updates_to_hidden(struct command *commands)
 	struct command *cmd;
 
 	for (cmd = commands; cmd; cmd = cmd->next) {
-		if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
+		const char *refname = cmd->ref_name;
+		struct strbuf refname_full_buf = STRBUF_INIT;
+		const char *refname_full;
+
+		strbuf_addf(&refname_full_buf, "%s%s", get_git_namespace(),
+				refname);
+		refname_full = strbuf_detach(&refname_full_buf, NULL);
+
+		if (cmd->error_string || !ref_is_hidden(refname, refname_full))
 			continue;
 		if (is_null_sha1(cmd->new_sha1))
 			cmd->error_string = "deny deleting a hidden ref";
diff --git a/refs.c b/refs.c
index 72d96ed..555c9d5 100644
--- a/refs.c
+++ b/refs.c
@@ -321,7 +321,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti
 	return 0;
 }
 
-int ref_is_hidden(const char *refname)
+int ref_is_hidden(const char *refname, const char *refname_full)
 {
 	int i;
 
@@ -329,6 +329,7 @@ int ref_is_hidden(const char *refname)
 		return 0;
 	for (i = hide_refs->nr - 1; i >= 0; i--) {
 		const char *match = hide_refs->items[i].string;
+		const char *subject;
 		int neg = 0;
 		int len;
 
@@ -337,10 +338,17 @@ int ref_is_hidden(const char *refname)
 			match++;
 		}
 
-		if (!starts_with(refname, match))
+		if (*match == '^') {
+			subject = refname_full;
+			match++;
+		} else {
+			subject = refname;
+		}
+
+		if (!subject || !starts_with(subject, match))
 			continue;
 		len = strlen(match);
-		if (!refname[len] || refname[len] == '/')
+		if (!subject[len] || subject[len] == '/')
 			return !neg;
 	}
 	return 0;
diff --git a/refs.h b/refs.h
index 69fa4df..fbf8e59 100644
--- a/refs.h
+++ b/refs.h
@@ -604,7 +604,7 @@ int update_ref(const char *msg, const char *refname,
 
 extern int parse_hide_refs_config(const char *var, const char *value, const char *);
 
-extern int ref_is_hidden(const char *);
+extern int ref_is_hidden(const char *, const char *);
 
 enum ref_type {
 	REF_TYPE_PER_WORKTREE,
diff --git a/upload-pack.c b/upload-pack.c
index 4ca960e..08efb1d 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -688,11 +688,12 @@ static void receive_needs(void)
 }
 
 /* return non-zero if the ref is hidden, otherwise 0 */
-static int mark_our_ref(const char *refname, const struct object_id *oid)
+static int mark_our_ref(const char *refname, const char *refname_full,
+			const struct object_id *oid)
 {
 	struct object *o = lookup_unknown_object(oid->hash);
 
-	if (refname && ref_is_hidden(refname)) {
+	if (ref_is_hidden(refname, refname_full)) {
 		o->flags |= HIDDEN_REF;
 		return 1;
 	}
@@ -700,10 +701,12 @@ static int mark_our_ref(const char *refname, const struct object_id *oid)
 	return 0;
 }
 
-static int check_ref(const char *refname, const struct object_id *oid,
+static int check_ref(const char *refname_full, const struct object_id *oid,
 		     int flag, void *cb_data)
 {
-	mark_our_ref(strip_namespace(refname), oid);
+	const char *refname = strip_namespace(refname_full);
+
+	mark_our_ref(refname, refname_full, oid);
 	return 0;
 }
 
@@ -726,7 +729,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
 	const char *refname_nons = strip_namespace(refname);
 	struct object_id peeled;
 
-	if (mark_our_ref(refname_nons, oid))
+	if (mark_our_ref(refname_nons, refname, oid))
 		return 0;
 
 	if (capabilities) {
-- 
2.6.2
Eric Sunshine· Nov 1, 2015, 20:42 UTC · re: Lukas Fleischer · lore

Re: [PATCH 3/4] Add support for matching full refs in hideRefs

On Sun, Nov 1, 2015 at 2:34 PM, Lukas Fleischer <lfleischer@lfos.de> wrote:
Show 17 quoted lines
> In addition to matching stripped refs, one can now add hideRefs patterns
> that the full (unstripped) ref is matched against. To distinguish
> between stripped and full matches, those new patterns must be prefixed
> with a circumflex (^).
>
> This commit also removes support for the undocumented and unintended
> hideRefs settings "have" (suppressing all "have" lines) and
> "capabilities^{}" (suppressing the capabilities line).
>
> Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
> ---
> diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> @@ -1198,7 +1200,15 @@ static void reject_updates_to_hidden(struct command *commands)
>         struct command *cmd;
>
>         for (cmd = commands; cmd; cmd = cmd->next) {
> -               if (cmd->error_string || !ref_is_hidden(cmd->ref_name))

Would it make sense to retain the cmd->error_string check here in order to avoid doing the extra refname_full construction work below?

    if (cmd->error_string)
        continue;
Show 10 quoted lines
> +               const char *refname = cmd->ref_name;
> +               struct strbuf refname_full_buf = STRBUF_INIT;
> +               const char *refname_full;
> +
> +               strbuf_addf(&refname_full_buf, "%s%s", get_git_namespace(),
> +                               refname);
> +               refname_full = strbuf_detach(&refname_full_buf, NULL);
> +
> +               if (cmd->error_string || !ref_is_hidden(refname, refname_full))
>                         continue;

This is leaking refname_full each time through the loop since it never gets free()d. If you restructure the code like this, it might be easier to avoid leaks:

    for (cmd = ...; ... ; ...) {
        if (cmd->error_string)
            continue;
        strbuf_addf(&refname_full, "%s%s", ...);
        if (ref_is_hidden(...)) {
            if (is_null_sha1(...))
                cmd->error_string = "...";
            else
                cmd->error_string = "...";
        }
        strbuf_release(&refname_full);
    }

As a micro-optimization, you could also pre-populate the strbuf with get_git_namespace() outside the loop and remember the length. Then, each time through the loop, just append cmd->ref_name, do your processing, and, at the bottom of the loop, set the strbuf back to the remembered length. (And, you still need to free the strbuf after the loop.)

>                 if (is_null_sha1(cmd->new_sha1))
>                         cmd->error_string = "deny deleting a hidden ref";
Lukas Fleischer· Nov 2, 2015, 05:47 UTC · re: Eric Sunshine · lore

Re: [PATCH 3/4] Add support for matching full refs in hideRefs

On Sun, 01 Nov 2015 at 21:42:37, Eric Sunshine wrote:
Show 50 quoted lines
> On Sun, Nov 1, 2015 at 2:34 PM, Lukas Fleischer <lfleischer@lfos.de> wrote:
> > In addition to matching stripped refs, one can now add hideRefs patterns
> > that the full (unstripped) ref is matched against. To distinguish
> > between stripped and full matches, those new patterns must be prefixed
> > with a circumflex (^).
> >
> > This commit also removes support for the undocumented and unintended
> > hideRefs settings "have" (suppressing all "have" lines) and
> > "capabilities^{}" (suppressing the capabilities line).
> >
> > Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
> > ---
> > diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> > @@ -1198,7 +1200,15 @@ static void reject_updates_to_hidden(struct command *commands)
> >         struct command *cmd;
> >
> >         for (cmd = commands; cmd; cmd = cmd->next) {
> > -               if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
> 
> Would it make sense to retain the cmd->error_string check here in
> order to avoid doing the extra refname_full construction work below?
> 
>     if (cmd->error_string)
>         continue;
> 
> [...]
> This is leaking refname_full each time through the loop since it never
> gets free()d. If you restructure the code like this, it might be
> easier to avoid leaks:
> 
>     for (cmd = ...; ... ; ...) {
>         if (cmd->error_string)
>             continue;
>         strbuf_addf(&refname_full, "%s%s", ...);
>         if (ref_is_hidden(...)) {
>             if (is_null_sha1(...))
>                 cmd->error_string = "...";
>             else
>                 cmd->error_string = "...";
>         }
>         strbuf_release(&refname_full);
>     }
> 
> As a micro-optimization, you could also pre-populate the strbuf with
> get_git_namespace() outside the loop and remember the length. Then,
> each time through the loop, just append cmd->ref_name, do your
> processing, and, at the bottom of the loop, set the strbuf back to the
> remembered length. (And, you still need to free the strbuf after the
> loop.)
> [...]

Sounds good to me. I changed the code to initialize the strbuf only once, save the prefix length and reset using strbuf_setlen() in every loop iteration. Thanks!

Lukas Fleischer· Nov 1, 2015, 19:34 UTC · re: Lukas Fleischer · lore

[PATCH 4/4] t5509: add basic tests for hideRefs

Test whether regular and full hideRefs patterns work as expected when namespaces are used.

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
---
 t/t5509-fetch-push-namespaces.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
Show changes to t/t5509-fetch-push-namespaces.sh +29 −0
diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh
index cc0b31f..a3f1060 100755
--- a/t/t5509-fetch-push-namespaces.sh
+++ b/t/t5509-fetch-push-namespaces.sh
@@ -82,4 +82,33 @@ test_expect_success 'mirroring a repository using a ref namespace' '
 	)
 '
 
+test_expect_success "Hide namespaced refs with transfer.hideRefs" '
+	cd pushee &&
+	test_config transfer.hideRefs refs/tags &&
+	GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
+	printf "$commit1\trefs/heads/master\n" >expected &&
+	test_cmp expected actual &&
+	cd ..
+'
+
+test_expect_success "Check that transfer.hideRefs does not match unstripped refs" '
+	cd pushee &&
+	test_config transfer.hideRefs "refs/namespaces/namespace/refs/tags" &&
+	GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
+	printf "$commit1\trefs/heads/master\n" >expected &&
+	printf "$commit0\trefs/tags/0\n" >>expected &&
+	printf "$commit1\trefs/tags/1\n" >>expected &&
+	test_cmp expected actual &&
+	cd ..
+'
+
+test_expect_success "Hide full refs with transfer.hideRefs" '
+	cd pushee &&
+	test_config transfer.hideRefs "^refs/namespaces/namespace/refs/tags" &&
+	GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
+	printf "$commit1\trefs/heads/master\n" >expected &&
+	test_cmp expected actual &&
+	cd ..
+'
+
 test_done
-- 
2.6.2
Eric Sunshine· Nov 1, 2015, 21:13 UTC · re: Lukas Fleischer · lore

Re: [PATCH 4/4] t5509: add basic tests for hideRefs

On Sun, Nov 1, 2015 at 2:34 PM, Lukas Fleischer <lfleischer@lfos.de> wrote:
Show 11 quoted lines
> Test whether regular and full hideRefs patterns work as expected when
> namespaces are used.
>
> Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
> ---
> diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh
> @@ -82,4 +82,33 @@ test_expect_success 'mirroring a repository using a ref namespace' '
>         )
>  '
>
> +test_expect_success "Hide namespaced refs with transfer.hideRefs" '

None of the other tests in this file capitalize the test description. These new test descriptions should probably follow suit by beginning with lowercase. It is also typical to use single quotes for the description rather than double.

Show 6 quoted lines
> +       cd pushee &&
> +       test_config transfer.hideRefs refs/tags &&
> +       GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
> +       printf "$commit1\trefs/heads/master\n" >expected &&
> +       test_cmp expected actual &&
> +       cd ..

If any of the commands above "cd .." fail, then "cd .." will never be invoked, thus subsequent tests will fail since they won't be executed in the expected directory. The typical way to handle this is to place the "cd foo" and remaining test body in a subshell, and drop "cd .." altogether. When the subshell exits (via success or failure), the working directory will be restored automatically.

    test_expect_success '...' '
        (
            cd pushee &&
            test_config ... &&
            ...
        )
    '
Show 25 quoted lines
> +'
> +
> +test_expect_success "Check that transfer.hideRefs does not match unstripped refs" '
> +       cd pushee &&
> +       test_config transfer.hideRefs "refs/namespaces/namespace/refs/tags" &&
> +       GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
> +       printf "$commit1\trefs/heads/master\n" >expected &&
> +       printf "$commit0\trefs/tags/0\n" >>expected &&
> +       printf "$commit1\trefs/tags/1\n" >>expected &&
> +       test_cmp expected actual &&
> +       cd ..
> +'
> +
> +test_expect_success "Hide full refs with transfer.hideRefs" '
> +       cd pushee &&
> +       test_config transfer.hideRefs "^refs/namespaces/namespace/refs/tags" &&
> +       GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
> +       printf "$commit1\trefs/heads/master\n" >expected &&
> +       test_cmp expected actual &&
> +       cd ..
> +'
> +
>  test_done
> --
> 2.6.2
Lukas Fleischer· Nov 2, 2015, 06:25 UTC · re: Eric Sunshine · lore

Re: [PATCH 4/4] t5509: add basic tests for hideRefs

On Sun, 01 Nov 2015 at 22:13:51, Eric Sunshine wrote:
Show 18 quoted lines
> On Sun, Nov 1, 2015 at 2:34 PM, Lukas Fleischer <lfleischer@lfos.de> wrote:
> > Test whether regular and full hideRefs patterns work as expected when
> > namespaces are used.
> >
> > Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
> > ---
> > diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh
> > @@ -82,4 +82,33 @@ test_expect_success 'mirroring a repository using a ref namespace' '
> >         )
> >  '
> >
> > +test_expect_success "Hide namespaced refs with transfer.hideRefs" '
> 
> None of the other tests in this file capitalize the test description.
> These new test descriptions should probably follow suit by beginning
> with lowercase. It is also typical to use single quotes for the
> description rather than double.
> 

Good catch. I originally put these tests somewhere else and noticed that adding them to t5509 is much better since we already set up the whole namespace infrastructure there. Seems like I forgot to adjust the style. Fixed that locally.

Show 22 quoted lines
> > +       cd pushee &&
> > +       test_config transfer.hideRefs refs/tags &&
> > +       GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
> > +       printf "$commit1    refs/heads/master\n" >expected &&
> > +       test_cmp expected actual &&
> > +       cd ..
> 
> If any of the commands above "cd .." fail, then "cd .." will never be
> invoked, thus subsequent tests will fail since they won't be executed
> in the expected directory. The typical way to handle this is to place
> the "cd foo" and remaining test body in a subshell, and drop "cd .."
> altogether. When the subshell exits (via success or failure), the
> working directory will be restored automatically.
> 
>     test_expect_success '...' '
>         (
>             cd pushee &&
>             test_config ... &&
>             ...
>         )
>     '
> [...]

I chose the `cd ..` approach because test_config does not work from a subshell. However, searching the Git log for "test_config", I found 1a9a23e (t7610: don't use test_config in a subshell, 2015-09-05) and da568b6 (t7800: don't use test_config in a subshell, 2015-09-05) which suggest to use the -C switch. The test cases now look like this:

    test_expect_success '[...]' '
        test_config -C pushee transfer.hideRefs [...] &&
        (
            cd pushee &&
            [...]
        )
    '
Thanks!
Eric Sunshine· Nov 2, 2015, 07:30 UTC · re: Lukas Fleischer · lore

Re: [PATCH 4/4] t5509: add basic tests for hideRefs

On Mon, Nov 2, 2015 at 1:25 AM, Lukas Fleischer <lfleischer@lfos.de> wrote:
Show 38 quoted lines
> On Sun, 01 Nov 2015 at 22:13:51, Eric Sunshine wrote:
>> On Sun, Nov 1, 2015 at 2:34 PM, Lukas Fleischer <lfleischer@lfos.de> wrote:
>> > +       cd pushee &&
>> > +       test_config transfer.hideRefs refs/tags &&
>> > +       GIT_NAMESPACE=namespace git ls-remote "ext::git %s ." >actual &&
>> > +       printf "$commit1    refs/heads/master\n" >expected &&
>> > +       test_cmp expected actual &&
>> > +       cd ..
>>
>> If any of the commands above "cd .." fail, then "cd .." will never be
>> invoked, thus subsequent tests will fail since they won't be executed
>> in the expected directory. The typical way to handle this is to place
>> the "cd foo" and remaining test body in a subshell, and drop "cd .."
>> altogether. When the subshell exits (via success or failure), the
>> working directory will be restored automatically.
>>
>>     test_expect_success '...' '
>>         (
>>             cd pushee &&
>>             test_config ... &&
>>             ...
>>         )
>>     '
>> [...]
>
> I chose the `cd ..` approach because test_config does not work from a
> subshell. However, searching the Git log for "test_config", I found
> 1a9a23e (t7610: don't use test_config in a subshell, 2015-09-05) and
> da568b6 (t7800: don't use test_config in a subshell, 2015-09-05) which
> suggest to use the -C switch. The test cases now look like this:
>
>     test_expect_success '[...]' '
>         test_config -C pushee transfer.hideRefs [...] &&
>         (
>             cd pushee &&
>             [...]
>         )
>     '

Yes, that can work, although for these simple cases, it might be more straightforward to use the git -c option to set the config variable just for the duration of the one git command. For instance:

test_expect_success 'hide namespaced refs with transfer.hideRefs' '
    (
        cd pushee &&
        GIT_NAMESPACE=namespace \
            git -c transfer.hideRefs=refs/tags \
            ls-remote "ext::git %s ." >actual &&
        printf "$commit1\trefs/heads/master\n" >expected &&
        test_cmp expected actual &&
    )
'

In fact, these test are so simple, that you don't really need the 'cd' at all. You could just use -C (along with -c):

test_expect_success 'hide namespaced refs with transfer.hideRefs' '
    GIT_NAMESPACE=namespace \
        git -C pushee -c transfer.hideRefs=refs/tags \
        ls-remote "ext::git %s ." >actual &&
    printf "$commit1\trefs/heads/master\n" >expected &&
    test_cmp expected actual &&
'

← back to recent threads