Skip to content

Commit fe05033

Browse files
committed
Merge branch 'jk/quote-env-path-list-component'
A recent update to receive-pack to make it easier to drop garbage objects made it clear that GIT_ALTERNATE_OBJECT_DIRECTORIES cannot have a pathname with a colon in it (no surprise!), and this in turn made it impossible to push into a repository at such a path. This has been fixed by introducing a quoting mechanism used when appending such a path to the colon-separated list. * jk/quote-env-path-list-component: t5615-alternate-env: double-quotes in file names do not work on Windows t5547-push-quarantine: run the path separator test on Windows, too tmp-objdir: quote paths we add to alternates alternates: accept double-quoted paths
2 parents 1123026 + 5e74824 commit fe05033

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

Documentation/git.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,12 @@ Git so take care if using a foreign front-end.
871871
specifies a ":" separated (on Windows ";" separated) list
872872
of Git object directories which can be used to search for Git
873873
objects. New objects will not be written to these directories.
874+
+
875+
Entries that begin with `"` (double-quote) will be interpreted
876+
as C-style quoted paths, removing leading and trailing
877+
double-quotes and respecting backslash escapes. E.g., the value
878+
`"path-with-\"-and-:-in-it":vanilla-path` has two paths:
879+
`path-with-"-and-:-in-it` and `vanilla-path`.
874880

875881
`GIT_DIR`::
876882
If the `GIT_DIR` environment variable is set then it

sha1_file.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "mru.h"
2727
#include "list.h"
2828
#include "mergesort.h"
29+
#include "quote.h"
2930

3031
#ifndef O_NOATIME
3132
#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -329,13 +330,40 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
329330
return 0;
330331
}
331332

333+
static const char *parse_alt_odb_entry(const char *string,
334+
int sep,
335+
struct strbuf *out)
336+
{
337+
const char *end;
338+
339+
strbuf_reset(out);
340+
341+
if (*string == '#') {
342+
/* comment; consume up to next separator */
343+
end = strchrnul(string, sep);
344+
} else if (*string == '"' && !unquote_c_style(out, string, &end)) {
345+
/*
346+
* quoted path; unquote_c_style has copied the
347+
* data for us and set "end". Broken quoting (e.g.,
348+
* an entry that doesn't end with a quote) falls
349+
* back to the unquoted case below.
350+
*/
351+
} else {
352+
/* normal, unquoted path */
353+
end = strchrnul(string, sep);
354+
strbuf_add(out, string, end - string);
355+
}
356+
357+
if (*end)
358+
end++;
359+
return end;
360+
}
361+
332362
static void link_alt_odb_entries(const char *alt, int len, int sep,
333363
const char *relative_base, int depth)
334364
{
335-
struct string_list entries = STRING_LIST_INIT_NODUP;
336-
char *alt_copy;
337-
int i;
338365
struct strbuf objdirbuf = STRBUF_INIT;
366+
struct strbuf entry = STRBUF_INIT;
339367

340368
if (depth > 5) {
341369
error("%s: ignoring alternate object stores, nesting too deep.",
@@ -348,16 +376,13 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
348376
die("unable to normalize object directory: %s",
349377
objdirbuf.buf);
350378

351-
alt_copy = xmemdupz(alt, len);
352-
string_list_split_in_place(&entries, alt_copy, sep, -1);
353-
for (i = 0; i < entries.nr; i++) {
354-
const char *entry = entries.items[i].string;
355-
if (entry[0] == '\0' || entry[0] == '#')
379+
while (*alt) {
380+
alt = parse_alt_odb_entry(alt, sep, &entry);
381+
if (!entry.len)
356382
continue;
357-
link_alt_odb_entry(entry, relative_base, depth, objdirbuf.buf);
383+
link_alt_odb_entry(entry.buf, relative_base, depth, objdirbuf.buf);
358384
}
359-
string_list_clear(&entries, 0);
360-
free(alt_copy);
385+
strbuf_release(&entry);
361386
strbuf_release(&objdirbuf);
362387
}
363388

t/t5547-push-quarantine.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,29 @@ test_expect_success 'rejected objects are removed' '
3333
test_cmp expect actual
3434
'
3535

36+
test_expect_success 'push to repo path with path separator (colon)' '
37+
# The interesting failure case here is when the
38+
# receiving end cannot access its original object directory,
39+
# so make it likely for us to generate a delta by having
40+
# a non-trivial file with multiple versions.
41+
42+
test-genrandom foo 4096 >file.bin &&
43+
git add file.bin &&
44+
git commit -m bin &&
45+
46+
if test_have_prereq MINGW
47+
then
48+
pathsep=";"
49+
else
50+
pathsep=":"
51+
fi &&
52+
git clone --bare . "xxx${pathsep}yyy.git" &&
53+
54+
echo change >>file.bin &&
55+
git commit -am change &&
56+
# Note that we have to use the full path here, or it gets confused
57+
# with the ssh host:path syntax.
58+
git push "$(pwd)/xxx${pathsep}yyy.git" HEAD
59+
'
60+
3661
test_done

t/t5615-alternate-env.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ test_expect_success 'access alternate via relative path (subdir)' '
6868
EOF
6969
'
7070

71+
# set variables outside test to avoid quote insanity; the \057 is '/',
72+
# which doesn't need quoting, but just confirms that de-quoting
73+
# is working.
74+
quoted='"one.git\057objects"'
75+
unquoted='two.git/objects'
76+
test_expect_success 'mix of quoted and unquoted alternates' '
77+
check_obj "$quoted:$unquoted" <<-EOF
78+
$one blob
79+
$two blob
80+
'
81+
82+
test_expect_success !MINGW 'broken quoting falls back to interpreting raw' '
83+
mv one.git \"one.git &&
84+
check_obj \"one.git/objects <<-EOF
85+
$one blob
86+
EOF
87+
'
88+
7189
test_done

tmp-objdir.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "string-list.h"
66
#include "strbuf.h"
77
#include "argv-array.h"
8+
#include "quote.h"
89

910
struct tmp_objdir {
1011
struct strbuf path;
@@ -79,12 +80,27 @@ static void remove_tmp_objdir_on_signal(int signo)
7980
*/
8081
static void env_append(struct argv_array *env, const char *key, const char *val)
8182
{
82-
const char *old = getenv(key);
83+
struct strbuf quoted = STRBUF_INIT;
84+
const char *old;
8385

86+
/*
87+
* Avoid quoting if it's not necessary, for maximum compatibility
88+
* with older parsers which don't understand the quoting.
89+
*/
90+
if (*val == '"' || strchr(val, PATH_SEP)) {
91+
strbuf_addch(&quoted, '"');
92+
quote_c_style(val, &quoted, NULL, 1);
93+
strbuf_addch(&quoted, '"');
94+
val = quoted.buf;
95+
}
96+
97+
old = getenv(key);
8498
if (!old)
8599
argv_array_pushf(env, "%s=%s", key, val);
86100
else
87101
argv_array_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
102+
103+
strbuf_release(&quoted);
88104
}
89105

90106
static void env_replace(struct argv_array *env, const char *key, const char *val)

0 commit comments

Comments
 (0)