setup.c: convert is_git_directory() to use strbuf
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Nov 30, 2014 at 15:24 UTC
1d186b6f3517b88325fec893f53cf59c3098f409
1 file changed
+21
-16
setup.c
+21
-16
@@ -238,31 +238,36 @@ void verify_non_filename(const char *prefix, const char *arg)
238
*/
239
int is_git_directory(const char *suspect)
240
{
241
- char path[PATH_MAX];
242
- size_t len = strlen(suspect);
241
+ struct strbuf path = STRBUF_INIT;
242
+ int ret = 0;
243
+ size_t len;
244
244
- if (PATH_MAX <= len + strlen("/objects"))
245
- die("Too long path: %.*s", 60, suspect);
246
- strcpy(path, suspect);
245
+ strbuf_addstr(&path, suspect);
246
+ len = path.len;
247
if (getenv(DB_ENVIRONMENT)) {
248
if (access(getenv(DB_ENVIRONMENT), X_OK))
249
- return 0;
249
+ goto done;
250
}
251
else {
252
- strcpy(path + len, "/objects");
253
- if (access(path, X_OK))
254
- return 0;
252
+ strbuf_addstr(&path, "/objects");
253
+ if (access(path.buf, X_OK))
254
+ goto done;
255
}
256
257
- strcpy(path + len, "/refs");
258
- if (access(path, X_OK))
259
- return 0;
257
+ strbuf_setlen(&path, len);
258
+ strbuf_addstr(&path, "/refs");
259
+ if (access(path.buf, X_OK))
260
+ goto done;
261
261
- strcpy(path + len, "/HEAD");
262
- if (validate_headref(path))
263
- return 0;
262
+ strbuf_setlen(&path, len);
263
+ strbuf_addstr(&path, "/HEAD");
264
+ if (validate_headref(path.buf))
265
+ goto done;
266
265
- return 1;
267
+ ret = 1;
268
+done:
269
+ strbuf_release(&path);
270
+ return ret;
271
}
272
273
int is_inside_git_dir(void)