setup_git_directory: make sure GIT_DIR is a valid repository.
setup_git_directory() always trusted what the user told where GIT_DIR was, and assumed that is a valid .git/ directory. This commit changes it to at least do the same level validation as is_toplevel_directory() does -- has refs/, has objects/ unless GIT_OBJECT_DIRECTORY is set, and has valid HEAD symlink or symref. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 25, 2005 at 15:43 UTC
5e7bfe256755ef2862e960864db1edb829c82b77
1 file changed
+37
-5
setup.c
+37
-5
@@ -73,8 +73,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
73
}
74
75
/*
76
- * Test it it looks like we're at the top
77
- * level git directory. We want to see a
76
+ * Test if it looks like we're at the top level git directory.
77
+ * We want to see:
78
*
79
* - either a .git/objects/ directory _or_ the proper
80
* GIT_OBJECT_DIRECTORY environment variable
@@ -92,17 +92,43 @@ static int is_toplevel_directory(void)
92
return 1;
93
}
94
95
-const char *setup_git_directory(void)
95
+static const char *setup_git_directory_1(void)
96
{
97
static char cwd[PATH_MAX+1];
98
int len, offset;
99
100
/*
101
* If GIT_DIR is set explicitly, we're not going
102
- * to do any discovery
102
+ * to do any discovery, but we still do repository
103
+ * validation.
104
*/
104
- if (getenv(GIT_DIR_ENVIRONMENT))
105
+ if (getenv(GIT_DIR_ENVIRONMENT)) {
106
+ char path[PATH_MAX];
107
+ int len = strlen(getenv(GIT_DIR_ENVIRONMENT));
108
+ if (sizeof(path) - 40 < len)
109
+ die("'$%s' too big", GIT_DIR_ENVIRONMENT);
110
+ memcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);
111
+
112
+ strcpy(path + len, "/refs");
113
+ if (access(path, X_OK))
114
+ goto bad_dir_environ;
115
+ strcpy(path + len, "/HEAD");
116
+ if (validate_symref(path))
117
+ goto bad_dir_environ;
118
+ if (getenv(DB_ENVIRONMENT)) {
119
+ if (access(DB_ENVIRONMENT, X_OK))
120
+ goto bad_dir_environ;
121
+ }
122
+ else {
123
+ strcpy(path + len, "/objects");
124
+ if (access(path, X_OK))
125
+ goto bad_dir_environ;
126
+ }
127
return NULL;
128
+ bad_dir_environ:
129
+ path[len] = 0;
130
+ die("Not a git repository: '%s'", path);
131
+ }
132
133
if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
134
die("Unable to read current working directory");
@@ -127,3 +153,9 @@ const char *setup_git_directory(void)
153
cwd[len] = 0;
154
return cwd + offset;
155
}
156
+
157
+const char *setup_git_directory(void)
158
+{
159
+ const char *retval = setup_git_directory_1();
160
+ return retval;
161
+}