Do not verify filenames in a bare repository
For example, it makes no sense to check the presence of a file named "HEAD" when calling "git log HEAD" in a bare repository. Noticed by Han-Wen Nienhuys. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Johannes Schindelin committed
Jan 20, 2007 at 03:09 UTC
68025633e352264898de44ec4856552d9a3abece
3 files changed
+56
cache.h
+1
@@ -129,6 +129,7 @@ extern int cache_errno;
129
130
extern int is_bare_repository_cfg;
131
extern int is_bare_repository(void);
132
+extern int is_inside_git_dir(void);
133
extern const char *get_git_dir(void);
134
extern char *get_object_directory(void);
135
extern char *get_refs_directory(void);
setup.c
+26
@@ -95,6 +95,8 @@ void verify_non_filename(const char *prefix, const char *arg)
95
const char *name;
96
struct stat st;
97
98
+ if (is_inside_git_dir())
99
+ return;
100
if (*arg == '-')
101
return; /* flag */
102
name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
@@ -168,6 +170,28 @@ static int is_git_directory(const char *suspect)
170
return 1;
171
}
172
173
+static int inside_git_dir = -1;
174
+
175
+int is_inside_git_dir(void)
176
+{
177
+ if (inside_git_dir < 0) {
178
+ char buffer[1024];
179
+
180
+ if (is_bare_repository())
181
+ return (inside_git_dir = 1);
182
+ if (getcwd(buffer, sizeof(buffer))) {
183
+ const char *git_dir = get_git_dir(), *cwd = buffer;
184
+ while (*git_dir && *git_dir == *cwd) {
185
+ git_dir++;
186
+ cwd++;
187
+ }
188
+ inside_git_dir = !*git_dir;
189
+ } else
190
+ inside_git_dir = 0;
191
+ }
192
+ return inside_git_dir;
193
+}
194
+
195
const char *setup_git_directory_gently(int *nongit_ok)
196
{
197
static char cwd[PATH_MAX+1];
@@ -206,6 +230,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
230
if (chdir(cwd))
231
die("Cannot come back to cwd");
232
setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
233
+ inside_git_dir = 1;
234
return NULL;
235
}
236
if (nongit_ok) {
@@ -226,6 +251,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
251
offset++;
252
cwd[len++] = '/';
253
cwd[len] = 0;
254
+ inside_git_dir = !strncmp(cwd + offset, ".git/", 5);
255
return cwd + offset;
256
}
257
t/t1020-subdirectory.sh
+29
@@ -106,4 +106,33 @@ test_expect_success 'read-tree' '
106
cmp ../one ../original.one
107
'
108
109
+test_expect_success 'no file/rev ambuguity check inside .git' '
110
+ cd $HERE &&
111
+ git commit -a -m 1 &&
112
+ cd $HERE/.git &&
113
+ git show -s HEAD
114
+'
115
+
116
+test_expect_success 'no file/rev ambuguity check inside a bare repo' '
117
+ cd $HERE &&
118
+ git clone -s --bare .git foo.git &&
119
+ cd foo.git && GIT_DIR=. git show -s HEAD
120
+'
121
+
122
+# This still does not work as it should...
123
+: test_expect_success 'no file/rev ambuguity check inside a bare repo' '
124
+ cd $HERE &&
125
+ git clone -s --bare .git foo.git &&
126
+ cd foo.git && git show -s HEAD
127
+'
128
+
129
+test_expect_success 'detection should not be fooled by a symlink' '
130
+ cd $HERE &&
131
+ rm -fr foo.git &&
132
+ git clone -s .git another &&
133
+ ln -s another yetanother &&
134
+ cd yetanother/.git &&
135
+ git show -s HEAD
136
+'
137
+
138
test_done