# [PATCH] Disallow '\' in ref names

6 messages from 2009-05-08 to 2009-05-08. Participants: Robin Rosenberg, Andreas Ericsson, Johannes Sixt, Michael J Gruber.
Thread: https://gitlist.dev/t/19244

## Robin Rosenberg, 2009-05-08 05:32

Subject: [PATCH] Disallow '\' in ref names
Message-ID: <1241760757-26068-1-git-send-email-robin.rosenberg@dewire.com>
URL: https://gitlist.dev/e/1241760757-26068-1-git-send-email-robin.rosenberg%40dewire.com

```
This is asking for trouble since '\' is a directory separator in
Windows and thus may produce unpredictable results.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 Documentation/git-check-ref-format.txt |    2 ++
 refs.c                                 |    3 ++-
 2 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/Documentation/git-check-ref-format.txt b/Documentation/git-check-ref-format.txt
index c1ce268..4884520 100644
--- a/Documentation/git-check-ref-format.txt
+++ b/Documentation/git-check-ref-format.txt
@@ -38,6 +38,8 @@ imposes the following rules on how references are named:
 
 . They cannot contain a sequence `@{`.
 
+- They cannot contain a `\\`
+
 These rules make it easy for shell script based tools to parse
 reference names, pathname expansion by the shell when a reference name is used
 unquoted (by mistake), and also avoids ambiguities in certain
diff --git a/refs.c b/refs.c
index e65a3b4..fc33bc6 100644
--- a/refs.c
+++ b/refs.c
@@ -682,12 +682,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
  * - it ends with a "/".
  * - it ends with ".lock"
+ * - it contains a "\" (backslash)
  */
 
 static inline int bad_ref_char(int ch)
 {
 	if (((unsigned) ch) <= ' ' ||
-	    ch == '~' || ch == '^' || ch == ':')
+	    ch == '~' || ch == '^' || ch == ':' || ch == '\\')
 		return 1;
 	/* 2.13 Pattern Matching Notation */
 	if (ch == '?' || ch == '[') /* Unsupported */
-- 
1.6.3.dirty

```

## Andreas Ericsson, 2009-05-08 06:55

Subject: Re: [PATCH] Disallow '\' in ref names
Message-ID: <4A03D776.6070309@op5.se>
URL: https://gitlist.dev/e/4A03D776.6070309%40op5.se
In-Reply-To: <1241760757-26068-1-git-send-email-robin.rosenberg@dewire.com>

```
Robin Rosenberg wrote:
> This is asking for trouble since '\' is a directory separator in
> Windows and thus may produce unpredictable results.
> 

NAK. We allow / on unixy systems, and that's the path separator there.
Junio even makes extensive use of it to create per-contributor
namespaces for topic-branches.

Are you guessing this might be a problem in the future, or have you
actually run into it?

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

Register now for Nordic Meet on Nagios, June 3-4 in Stockholm
 http://nordicmeetonnagios.op5.org/

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.

```

## Johannes Sixt, 2009-05-08 07:17

Subject: Re: [PATCH] Disallow '\' in ref names
Message-ID: <4A03DC9C.2050204@viscovery.net>
URL: https://gitlist.dev/e/4A03DC9C.2050204%40viscovery.net
In-Reply-To: <4A03D776.6070309@op5.se>

```
Andreas Ericsson schrieb:
> Robin Rosenberg wrote:
>> This is asking for trouble since '\' is a directory separator in
>> Windows and thus may produce unpredictable results.
>>
> 
> NAK. We allow / on unixy systems, and that's the path separator there.
> Junio even makes extensive use of it to create per-contributor
> namespaces for topic-branches.
> 
> Are you guessing this might be a problem in the future, or have you
> actually run into it?

This is not possible on Windows:

$ git update-ref refs/heads/foo\bar HEAD
fatal: Unable to create '.git/refs/heads/foo\bar.lock': No such file or
directory

The problem is that git doesn't create the directory .git/refs/heads/foo
because the ref handling only mkdir()s directories that were split off
from the ref at forward-slashes.

The decision not to mangle command line arguments that are refs (on
Windows) was intentional. (We do mangle pathspec, i.e. we convert '\' to
'/'.) But back then I did not think about what should happen if a ref
contains a backslash.

BTW, it's the same problem with backslash in path components: Just don't
do that if you want to be cross-platfrom.

-- Hannes

```

## Andreas Ericsson, 2009-05-08 07:46

Subject: Re: [PATCH] Disallow '\' in ref names
Message-ID: <4A03E343.4020000@op5.se>
URL: https://gitlist.dev/e/4A03E343.4020000%40op5.se
In-Reply-To: <4A03DC9C.2050204@viscovery.net>

```
Johannes Sixt wrote:
> Andreas Ericsson schrieb:
>> Robin Rosenberg wrote:
>>> This is asking for trouble since '\' is a directory separator in
>>> Windows and thus may produce unpredictable results.
>>>
>> NAK. We allow / on unixy systems, and that's the path separator there.
>> Junio even makes extensive use of it to create per-contributor
>> namespaces for topic-branches.
>>
>> Are you guessing this might be a problem in the future, or have you
>> actually run into it?
> 
> This is not possible on Windows:
> 
> $ git update-ref refs/heads/foo\bar HEAD
> fatal: Unable to create '.git/refs/heads/foo\bar.lock': No such file or
> directory
> 
> The problem is that git doesn't create the directory .git/refs/heads/foo
> because the ref handling only mkdir()s directories that were split off
> from the ref at forward-slashes.
> 

Ok. Isn't the slash -> backslash conversion thing supposed to be done by
a macro? I seem to remember something about PATH_DELIM.

> The decision not to mangle command line arguments that are refs (on
> Windows) was intentional. (We do mangle pathspec, i.e. we convert '\' to
> '/'.) But back then I did not think about what should happen if a ref
> contains a backslash.
> 

But what about when locating directory cutoffs for mkdir() stuff?

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

Register now for Nordic Meet on Nagios, June 3-4 in Stockholm
 http://nordicmeetonnagios.op5.org/

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.

```

## Michael J Gruber, 2009-05-08 07:54

Subject: Re: [PATCH] Disallow '\' in ref names
Message-ID: <4A03E551.8080904@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4A03E551.8080904%40drmicha.warpmail.net
In-Reply-To: <1241760757-26068-1-git-send-email-robin.rosenberg@dewire.com>

```
Robin Rosenberg venit, vidit, dixit 08.05.2009 07:32:
> This is asking for trouble since '\' is a directory separator in
> Windows and thus may produce unpredictable results.
> 
> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
> ---
>  Documentation/git-check-ref-format.txt |    2 ++
>  refs.c                                 |    3 ++-
>  2 files changed, 4 insertions(+), 1 deletions(-)
> 
> diff --git a/Documentation/git-check-ref-format.txt b/Documentation/git-check-ref-format.txt
> index c1ce268..4884520 100644
> --- a/Documentation/git-check-ref-format.txt
> +++ b/Documentation/git-check-ref-format.txt
> @@ -38,6 +38,8 @@ imposes the following rules on how references are named:
>  
>  . They cannot contain a sequence `@{`.
>  
> +- They cannot contain a `\\`

Minor nit from the doc watch dog:

+. They cannot contain a `\\`.

> +
>  These rules make it easy for shell script based tools to parse
>  reference names, pathname expansion by the shell when a reference name is used
>  unquoted (by mistake), and also avoids ambiguities in certain
> diff --git a/refs.c b/refs.c
> index e65a3b4..fc33bc6 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -682,12 +682,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
>   * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
>   * - it ends with a "/".
>   * - it ends with ".lock"
> + * - it contains a "\" (backslash)
>   */
>  
>  static inline int bad_ref_char(int ch)
>  {
>  	if (((unsigned) ch) <= ' ' ||
> -	    ch == '~' || ch == '^' || ch == ':')
> +	    ch == '~' || ch == '^' || ch == ':' || ch == '\\')
>  		return 1;
>  	/* 2.13 Pattern Matching Notation */
>  	if (ch == '?' || ch == '[') /* Unsupported */

```

## Johannes Sixt, 2009-05-08 08:04

Subject: Re: [PATCH] Disallow '\' in ref names
Message-ID: <4A03E78C.8000801@viscovery.net>
URL: https://gitlist.dev/e/4A03E78C.8000801%40viscovery.net
In-Reply-To: <4A03E343.4020000@op5.se>

```
Andreas Ericsson schrieb:
> Johannes Sixt wrote:
>> Andreas Ericsson schrieb:
>>> Robin Rosenberg wrote:
>>>> This is asking for trouble since '\' is a directory separator in
>>>> Windows and thus may produce unpredictable results.
>>>>
>>> NAK. We allow / on unixy systems, and that's the path separator there.
>>> Junio even makes extensive use of it to create per-contributor
>>> namespaces for topic-branches.
>>>
>>> Are you guessing this might be a problem in the future, or have you
>>> actually run into it?
>>
>> This is not possible on Windows:
>>
>> $ git update-ref refs/heads/foo\bar HEAD
>> fatal: Unable to create '.git/refs/heads/foo\bar.lock': No such file or
>> directory
>>
>> The problem is that git doesn't create the directory .git/refs/heads/foo
>> because the ref handling only mkdir()s directories that were split off
>> from the ref at forward-slashes.
>>
> 
> Ok. Isn't the slash -> backslash conversion thing supposed to be done by
> a macro? I seem to remember something about PATH_DELIM.

It is is_dir_sep(). This macro is only used with pathspec, not with refs.
Therefore, backslashes in refs enter git unaltered and cause havoc on
Windows because git internally only knows how to treat forward-slashes in
refs in a special way.

Note that using is_dir_sep() in refs is not a solution because then refs
that were read from the file system would be reported with a forward-slash.

>> The decision not to mangle command line arguments that are refs (on
>> Windows) was intentional. (We do mangle pathspec, i.e. we convert '\' to
>> '/'.) But back then I did not think about what should happen if a ref
>> contains a backslash.
> 
> But what about when locating directory cutoffs for mkdir() stuff?

I can't parse your statement. ;)

-- Hannes

```
