summaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
authorVicent Martí <tanoku@gmail.com>2011-07-28 05:32:47 -0700
committerVicent Martí <tanoku@gmail.com>2011-07-28 05:32:47 -0700
commitcb1c75635e9e6c30e147be80b71e72382a1097f7 (patch)
treec1f8928291552b432a41947a0fbbe5693f68248a /src/index.c
parent3737537b0ce9788226cf3a12ef96244d3ff349f6 (diff)
parent7db40d450de401100922b023ec5e687bc1a6d4d5 (diff)
downloadlibgit2-cb1c75635e9e6c30e147be80b71e72382a1097f7.tar.gz
Merge pull request #335 from carlosmn/read-updated
Don't stat so much when reading references
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/index.c b/src/index.c
index af71ebd29..f20c2a7ab 100644
--- a/src/index.c
+++ b/src/index.c
@@ -248,8 +248,9 @@ void git_index_clear(git_index *index)
int git_index_read(git_index *index)
{
- struct stat indexst;
- int error = GIT_SUCCESS;
+ int error = GIT_SUCCESS, updated;
+ git_fbuffer buffer = GIT_FBUFFER_INIT;
+ time_t mtime;
assert(index->index_file_path);
@@ -259,30 +260,24 @@ int git_index_read(git_index *index)
return GIT_SUCCESS;
}
- if (p_stat(index->index_file_path, &indexst) < 0)
- return git__throw(GIT_EOSERR, "Failed to read index. %s does not exist or is corrupted", index->index_file_path);
-
- if (!S_ISREG(indexst.st_mode))
- return git__throw(GIT_ENOTFOUND, "Failed to read index. %s is not an index file", index->index_file_path);
-
- if (indexst.st_mtime != index->last_modified) {
-
- git_fbuffer buffer;
-
- if ((error = git_futils_readbuffer(&buffer, index->index_file_path)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to read index");
+ /* We don't want to update the mtime if we fail to parse the index */
+ mtime = index->last_modified;
+ error = git_futils_readbuffer_updated(&buffer, index->index_file_path, &mtime, &updated);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to read index");
+ if (updated) {
git_index_clear(index);
error = parse_index(index, buffer.data, buffer.len);
if (error == GIT_SUCCESS)
- index->last_modified = indexst.st_mtime;
+ index->last_modified = mtime;
git_futils_freebuffer(&buffer);
}
if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to read index");
+ return git__rethrow(error, "Failed to parse index");
return error;
}