131
return -1;
132
}
133
134
-static char *current_dir(void)
134
+static char *user_path(char *buf, char *path, int sz)
135
{
136
- return getcwd(pathname, sizeof(pathname));
137
-}
138
-
139
-static int user_chdir(char *path)
140
-{
141
- char *dir = path;
136
+ struct passwd *pw;
137
+ char *slash;
138
+ int len, baselen;
139
143
- if(*dir == '~') { /* user-relative path */
144
- struct passwd *pw;
145
- char *slash = strchr(dir, '/');
146
-
147
- dir++;
148
- /* '~/' and '~' (no slash) means users own home-dir */
149
- if(!*dir || *dir == '/')
150
- pw = getpwuid(getuid());
151
- else {
152
- if (slash) {
153
- *slash = '\0';
154
- pw = getpwnam(dir);
155
- *slash = '/';
156
- }
157
- else
158
- pw = getpwnam(dir);
140
+ if (!path || path[0] != '~')
141
+ return NULL;
142
+ path++;
143
+ slash = strchr(path, '/');
144
+ if (path[0] == '/' || !path[0]) {
145
+ pw = getpwuid(getuid());
146
+ }
147
+ else {
148
+ if (slash) {
149
+ *slash = 0;
150
+ pw = getpwnam(path);
151
+ *slash = '/';
152
}
160
-
161
- /* make sure we got something back that we can chdir() to */
162
- if(!pw || chdir(pw->pw_dir) < 0)
163
- return -1;
164
-
165
- if(!slash || !slash[1]) /* no path following username */
166
- return 0;
167
-
168
- dir = slash + 1;
153
+ else
154
+ pw = getpwnam(path);
155
}
170
-
171
- /* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
172
- if(chdir(dir) < 0)
173
- return -1;
174
-
175
- return 0;
156
+ if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
157
+ return NULL;
158
+ baselen = strlen(pw->pw_dir);
159
+ memcpy(buf, pw->pw_dir, baselen);
160
+ while ((1 < baselen) && (buf[baselen-1] == '/')) {
161
+ buf[baselen-1] = 0;
162
+ baselen--;
163
+ }
164
+ if (slash && slash[1]) {
165
+ len = strlen(slash);
166
+ if (sz <= baselen + len)
167
+ return NULL;
168
+ memcpy(buf + baselen, slash, len + 1);
169
+ }
170
+ return buf;
171
}
172
173
+/*
174
+ * First, one directory to try is determined by the following algorithm.
175
+ *
176
+ * (0) If "strict" is given, the path is used as given and no DWIM is
177
+ * done. Otherwise:
178
+ * (1) "~/path" to mean path under the running user's home directory;
179
+ * (2) "~user/path" to mean path under named user's home directory;
180
+ * (3) "relative/path" to mean cwd relative directory; or
181
+ * (4) "/absolute/path" to mean absolute directory.
182
+ *
183
+ * Unless "strict" is given, we try access() for existence of "%s.git/.git",
184
+ * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
185
+ * what we try.
186
+ *
187
+ * Second, we try chdir() to that. Upon failure, we return NULL.
188
+ *
189
+ * Then, we try if the current directory is a valid git repository.
190
+ * Upon failure, we return NULL.
191
+ *
192
+ * If all goes well, we return the directory we used to chdir() (but
193
+ * before ~user is expanded), avoiding getcwd() resolving symbolic
194
+ * links. User relative paths are also returned as they are given,
195
+ * except DWIM suffixing.
196
+ */
197
char *enter_repo(char *path, int strict)
198
{
180
- if(!path)
199
+ static char used_path[PATH_MAX];
200
+ static char validated_path[PATH_MAX];
201
+
202
+ if (!path)
203
return NULL;
204
183
- if (strict) {
184
- if (chdir(path) < 0)
205
+ if (!strict) {
206
+ static const char *suffix[] = {
207
+ ".git/.git", "/.git", ".git", "", NULL,
208
+ };
209
+ int len = strlen(path);
210
+ int i;
211
+ while ((1 < len) && (path[len-1] == '/')) {
212
+ path[len-1] = 0;
213
+ len--;
214
+ }
215
+ if (PATH_MAX <= len)
216
return NULL;
186
- }
187
- else {
188
- if (!*path)
189
- ; /* happy -- no chdir */
190
- else if (!user_chdir(path))
191
- ; /* happy -- as given */
192
- else if (!user_chdir(mkpath("%s.git", path)))
193
- ; /* happy -- uemacs --> uemacs.git */
194
- else
217
+ if (path[0] == '~') {
218
+ if (!user_path(used_path, path, PATH_MAX))
219
+ return NULL;
220
+ strcpy(validated_path, path);
221
+ path = used_path;
222
+ }
223
+ else if (PATH_MAX - 10 < len)
224
+ return NULL;
225
+ else {
226
+ path = strcpy(used_path, path);
227
+ strcpy(validated_path, path);
228
+ }
229
+ len = strlen(path);
230
+ for (i = 0; suffix[i]; i++) {
231
+ strcpy(path + len, suffix[i]);
232
+ if (!access(path, F_OK)) {
233
+ strcat(validated_path, suffix[i]);
234
+ break;
235
+ }
236
+ }
237
+ if (!suffix[i] || chdir(path))
238
return NULL;
196
- (void)chdir(".git");
239
+ path = validated_path;
240
}
241
+ else if (chdir(path))
242
+ return NULL;
243
199
- if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
200
- validate_symref("HEAD") == 0) {
244
+ if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
245
+ validate_symref("HEAD") == 0) {
246
putenv("GIT_DIR=.");
247
check_repository_format();
203
- return current_dir();
248
+ return path;
249
}
250
251
return NULL;