]> asedeno.scripts.mit.edu Git - git.git/blobdiff - path.c
add_file_to_index: skip rehashing if the cached stat already matches
[git.git] / path.c
diff --git a/path.c b/path.c
index 066f62195508033a5f72504e4805ea436424296e..dfff41f62668709e80cc54f816394303e8f6cefc 100644 (file)
--- a/path.c
+++ b/path.c
@@ -71,29 +71,26 @@ char *git_path(const char *fmt, ...)
 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
 int git_mkstemp(char *path, size_t len, const char *template)
 {
-       char *env, *pch = path;
+       const char *tmp;
+       size_t n;
 
-       if ((env = getenv("TMPDIR")) == NULL) {
-               strcpy(pch, "/tmp/");
-               len -= 5;
-               pch += 5;
-       } else {
-               size_t n = snprintf(pch, len, "%s/", env);
-
-               len -= n;
-               pch += n;
+       tmp = getenv("TMPDIR");
+       if (!tmp)
+               tmp = "/tmp";
+       n = snprintf(path, len, "%s/%s", tmp, template);
+       if (len <= n) {
+               errno = ENAMETOOLONG;
+               return -1;
        }
-
-       strlcpy(pch, template, len);
-
        return mkstemp(path);
 }
 
 
-int validate_symref(const char *path)
+int validate_headref(const char *path)
 {
        struct stat st;
        char *buf, buffer[256];
+       unsigned char sha1[20];
        int len, fd;
 
        if (lstat(path, &st) < 0)
@@ -113,20 +110,29 @@ int validate_symref(const char *path)
        fd = open(path, O_RDONLY);
        if (fd < 0)
                return -1;
-       len = read(fd, buffer, sizeof(buffer)-1);
+       len = read_in_full(fd, buffer, sizeof(buffer)-1);
        close(fd);
 
        /*
         * Is it a symbolic ref?
         */
-       if (len < 4 || memcmp("ref:", buffer, 4))
+       if (len < 4)
                return -1;
-       buf = buffer + 4;
-       len -= 4;
-       while (len && isspace(*buf))
-               buf++, len--;
-       if (len >= 5 && !memcmp("refs/", buf, 5))
+       if (!memcmp("ref:", buffer, 4)) {
+               buf = buffer + 4;
+               len -= 4;
+               while (len && isspace(*buf))
+                       buf++, len--;
+               if (len >= 5 && !memcmp("refs/", buf, 5))
+                       return 0;
+       }
+
+       /*
+        * Is this a detached HEAD?
+        */
+       if (!get_sha1_hex(buffer, sha1))
                return 0;
+
        return -1;
 }
 
@@ -241,8 +247,8 @@ char *enter_repo(char *path, int strict)
                return NULL;
 
        if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
-           validate_symref("HEAD") == 0) {
-               putenv("GIT_DIR=.");
+           validate_headref("HEAD") == 0) {
+               setenv(GIT_DIR_ENVIRONMENT, ".", 1);
                check_repository_format();
                return path;
        }