From 75e051c682255a3c9428200c20c733ec06137b99 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 27 Jan 2011 14:20:23 -0500 Subject: [PATCH 1/5] Added git_tree_add_entry_unsorted and git_tree_sort_entries --- src/git2/tree.h | 30 ++++++++++++++++++++++++++++++ src/tree.c | 23 ++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/git2/tree.h b/src/git2/tree.h index 9ebb26562..fedf2b7c3 100644 --- a/src/git2/tree.h +++ b/src/git2/tree.h @@ -148,6 +148,36 @@ GIT_EXTERN(int) git_tree_entry_2object(git_object **object, git_tree_entry *entr */ GIT_EXTERN(int) git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes); +/** + * Add a new entry to a tree, returning that new entry. + * The only difference with this call is that it does not sort + * tree afterwards, this requirement is left to the caller. + * + * This will mark the tree as modified; the new entry will + * be written back to disk on the next git_object_write() + * + * @param entry Entry object which will be created + * @param tree Tree object to store the entry + * @iparam id OID for the tree entry + * @param filename Filename for the tree entry + * @param attributes UNIX file attributes for the entry + * @return 0 on success; otherwise error code + */ +GIT_EXTERN(int) git_tree_add_entry_unsorted(git_tree_entry **entry, git_tree *tree, const git_oid *id, const char *filename, int attributes); + +/** + * Sort the entries in a tree created using git_tree_add_entry2. + * + * This does not mark the tree as modified. It is intended to be used + * after several invocations of git_tree_add_entry2. + * git_tree_add_entry, on the other hand, sorts after each entry is + * added. + * + * @param tree Tree object whose entries are to be sorted + * @return 0 on success; otherwise error code + */ +GIT_EXTERN(int) git_tree_sort_entries(git_tree *tree); + /** * Remove an entry by its index. * diff --git a/src/tree.c b/src/tree.c index dffe872e0..c4def2a97 100644 --- a/src/tree.c +++ b/src/tree.c @@ -174,12 +174,11 @@ size_t git_tree_entrycount(git_tree *tree) return tree->entries.length; } -int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes) +int git_tree_add_entry_unsorted(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes) { - git_tree_entry *entry; - assert(tree && id && filename); + git_tree_entry *entry; if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL) return GIT_ENOMEM; @@ -193,8 +192,6 @@ int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid if (git_vector_insert(&tree->entries, entry) < 0) return GIT_ENOMEM; - git_vector_sort(&tree->entries); - if (entry_out != NULL) *entry_out = entry; @@ -202,6 +199,22 @@ int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid return GIT_SUCCESS; } +int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes) +{ + int result = git_tree_add_entry_unsorted(entry_out, tree, id, filename, attributes); + if (result == GIT_SUCCESS) { + git_vector_sort(&tree->entries); + return GIT_SUCCESS; + } + return result; +} + +int git_tree_sort_entries(git_tree *tree) +{ + git_vector_sort(&tree->entries); + return GIT_SUCCESS; +} + int git_tree_remove_entry_byindex(git_tree *tree, int idx) { git_tree_entry *remove_ptr; From 89f9fc6f4b1a17b8318a0f11a51c967f1504ed34 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 28 Jan 2011 02:41:59 -0500 Subject: [PATCH 2/5] Make git_tree_clear_entries visible to the user --- src/git2/tree.h | 10 ++++++++++ src/tree.c | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/git2/tree.h b/src/git2/tree.h index fedf2b7c3..6f79ac455 100644 --- a/src/git2/tree.h +++ b/src/git2/tree.h @@ -204,6 +204,16 @@ GIT_EXTERN(int) git_tree_remove_entry_byindex(git_tree *tree, int idx); */ GIT_EXTERN(int) git_tree_remove_entry_byname(git_tree *tree, const char *filename); +/** + * Clear all the entries in a tree. + * + * This will mark the tree as modified; the modified entry will + * be written back to disk on the next git_object_write(). + * + * @param tree Tree object whose entries are to be sorted + */ +GIT_EXTERN(void) git_tree_clear_entries(git_tree *tree); + /** * Change the SHA1 id of a tree entry. * diff --git a/src/tree.c b/src/tree.c index c4def2a97..605af8b39 100644 --- a/src/tree.c +++ b/src/tree.c @@ -48,7 +48,7 @@ int entry_sort_cmp(const void *a, const void *b) return strcmp(entry_a->filename, entry_b->filename); } -static void clear_entries(git_tree *tree) +void git_tree_clear_entries(git_tree *tree) { unsigned int i; @@ -64,6 +64,8 @@ static void clear_entries(git_tree *tree) } git_vector_clear(&tree->entries); + + tree->object.modified = 1; } @@ -90,7 +92,7 @@ git_tree *git_tree__new(void) void git_tree__free(git_tree *tree) { - clear_entries(tree); + git_tree_clear_entries(tree); git_vector_free(&tree->entries); free(tree); } @@ -282,7 +284,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end) expected_size = (tree->object.source.raw.len / avg_entry_size) + 1; - clear_entries(tree); + git_tree_clear_entries(tree); while (buffer < buffer_end) { git_tree_entry *entry; From 9217bbda49264e016e4c24950292cd2572ffed51 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 1 Feb 2011 05:00:52 -0500 Subject: [PATCH 3/5] Fixed a bug with the way commits are written --- src/commit.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/commit.c b/src/commit.c index c3fa3823f..49e23545e 100644 --- a/src/commit.c +++ b/src/commit.c @@ -91,8 +91,10 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src) git_signature__write(src, "committer", commit->committer); - if (commit->message != NULL) - git__source_printf(src, "\n%s", commit->message); + if (commit->message != NULL) { + git__source_write(src, "\n", 1); + git__source_write(src, commit->message, strlen(commit->message)); + } /* Mark the commit as having all attributes */ commit->full_parse = 1; From bf3389b930b8f040cd6a929bddd6fcd0c390f8f8 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 1 Feb 2011 05:57:45 -0500 Subject: [PATCH 4/5] Fixed bug where git__source_printf needs multiple attempts --- src/object.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/object.c b/src/object.c index 87e1b6eac..43698d276 100644 --- a/src/object.c +++ b/src/object.c @@ -109,12 +109,9 @@ int git__source_printf(git_odb_source *source, const char *format, ...) if (source_resize(source) < GIT_SUCCESS) return GIT_ENOMEM; - did_resize = 1; + len = vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist); } - if (did_resize) - vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist); - source->write_ptr = (char *)source->write_ptr + len; source->written_bytes += len; From 35786cb7e8d8b06aa211acbc741207d08973920b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 2 Feb 2011 19:00:26 -0500 Subject: [PATCH 5/5] Use Git's own tree entry sorting algorithm If plain strcmp is used, as this code did before, the final sorting may end up different from what git-add would do (for example, 'boost' appearing before 'boost-build.jam', because Git sorts as if it were spelled 'boost/'). If the sorting is incorrect like this, Git 1.7.4 insists that unmodified files have been modified. For example, my test repository has these four entries: drwxr-xr-x 199 johnw wheel 6766 Feb 2 17:21 boost -rw-r--r-- 1 johnw wheel 849 Feb 2 17:22 boost-build.jam -rw-r--r-- 1 johnw wheel 989 Feb 2 17:21 boost.css -rw-r--r-- 1 johnw wheel 6308 Feb 2 17:21 boost.png Here is the output from git-ls-tree for these files, in a commit tree created using git-add and git-commit: 100644 blob 8b8775433aef73e9e12609610ae2e35cf1e7ec2c boost-build.jam 100644 blob 986c4050fa96d825a1311c8e871cdcc9a3e0d2c3 boost.css 100644 blob b4d51fcd5c9149fd77f5ca6ed2b6b1b70e8fe24f boost.png 040000 tree 46537eeaa4d577010f19b1c9e940cae9a670ff5c boost Here is the output for the same commit produced using libgit2: 040000 tree c27c0fd1436f28a6ba99acd0a6c17d178ed58288 boost 100644 blob 8b8775433aef73e9e12609610ae2e35cf1e7ec2c boost-build.jam 100644 blob 986c4050fa96d825a1311c8e871cdcc9a3e0d2c3 boost.css 100644 blob b4d51fcd5c9149fd77f5ca6ed2b6b1b70e8fe24f boost.png Due to this reordering, git-status claims the three blobs are always modified, no matter what I do using git-read-tree or git-reset or git-checkout to update the index. --- src/tree.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/tree.c b/src/tree.c index 605af8b39..ab3bec8cd 100644 --- a/src/tree.c +++ b/src/tree.c @@ -40,12 +40,33 @@ int entry_search_cmp(const void *key, const void *array_member) return strcmp(filename, entry->filename); } +static int cache_name_compare(const char *name1, int len1, int isdir1, + const char *name2, int len2, int isdir2) +{ + int len = len1 < len2 ? len1 : len2; + int cmp; + + cmp = memcmp(name1, name2, len); + if (cmp) + return cmp; + if (len1 < len2) + return ((isdir1 == isdir2) ? -1 : + (isdir1 ? '/' - name2[len1] : name2[len1] - '/')); + if (len1 > len2) + return ((isdir1 == isdir2) ? 1 : + (isdir2 ? name1[len2] - '/' : '/' - name1[len2])); + return 0; +} + int entry_sort_cmp(const void *a, const void *b) { const git_tree_entry *entry_a = *(const git_tree_entry **)(a); const git_tree_entry *entry_b = *(const git_tree_entry **)(b); - return strcmp(entry_a->filename, entry_b->filename); + return cache_name_compare(entry_a->filename, strlen(entry_a->filename), + entry_a->attr & 040000, + entry_b->filename, strlen(entry_b->filename), + entry_b->attr & 040000); } void git_tree_clear_entries(git_tree *tree)