index.c: Correctly check whether index contains extended entries Although write_index() supports writing extended header versions for index, this was never done as there was no check for extended index entries. Introduce function is_index_extended() that checks whether an index contains extended entries and check whether an index is extended before writing it to disk, adjusting its version number if necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/index.c b/src/index.c
index 6a31dd5..6a67e9c 100644
--- a/src/index.c
+++ b/src/index.c
@@ -101,6 +101,7 @@ static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
static git_index_tree *read_tree_internal(const char **, const char *, git_index_tree *);
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
+static int is_index_extended(git_index *index);
static void sort_index(git_index *index);
static int write_index(git_index *index, git_filebuf *file);
@@ -674,6 +675,24 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
return GIT_SUCCESS;
}
+static int is_index_extended(git_index *index)
+{
+ unsigned int i, extended;
+
+ extended = 0;
+
+ for (i = 0; i < index->entries.length; ++i) {
+ git_index_entry *entry;
+ entry = git_vector_get(&index->entries, i);
+ entry->flags &= ~GIT_IDXENTRY_EXTENDED;
+ if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
+ extended++;
+ entry->flags |= GIT_IDXENTRY_EXTENDED;
+ }
+ }
+ return extended;
+}
+
static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
{
struct entry_short *ondisk;
@@ -742,12 +761,14 @@ static int write_index(git_index *index, git_filebuf *file)
struct index_header header;
- int is_extended = 1;
+ int is_extended;
assert(index && file);
+ is_extended = is_index_extended(index);
+
header.signature = htonl(INDEX_HEADER_SIG);
- header.version = htonl(is_extended ? INDEX_VERSION_NUMBER : INDEX_VERSION_NUMBER_EXT);
+ header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER);
header.entry_count = htonl(index->entries.length);
git_filebuf_write(file, &header, sizeof(struct index_header));