Reduce the number of unnecessary objects in pushed packs
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/src/push.c b/src/push.c
index 37f6418..ee21bd9 100644
--- a/src/push.c
+++ b/src/push.c
@@ -13,6 +13,7 @@
#include "remote.h"
#include "vector.h"
#include "push.h"
+#include "tree.h"
static int push_spec_rref_cmp(const void *a, const void *b)
{
@@ -346,60 +347,166 @@ on_error:
return error == GIT_ITEROVER ? 0 : error;
}
-static int queue_objects(git_push *push)
+static int queue_differences(
+ git_tree *base,
+ git_tree *delta,
+ git_packbuilder *pb)
{
- git_vector commits;
- git_oid *o;
- unsigned int i;
+ git_tree *b_child = NULL, *d_child = NULL;
+ size_t b_length = git_tree_entrycount(base);
+ size_t d_length = git_tree_entrycount(delta);
+ size_t i = 0, j = 0;
int error;
- if (git_vector_init(&commits, 0, NULL) < 0)
- return -1;
+#define _enqueue_object(ENTRY) do { \
+ switch (git_tree_entry_type((ENTRY))) { \
+ case GIT_OBJ_COMMIT: \
+ break; \
+ case GIT_OBJ_TREE: \
+ if ((error = git_packbuilder_insert_tree(pb, &(ENTRY)->oid)) < 0) \
+ goto on_error; \
+ break; \
+ default: \
+ if ((error = git_packbuilder_insert(pb, &(ENTRY)->oid, \
+ (ENTRY)->filename)) < 0) \
+ goto on_error; \
+ break; \
+ } \
+} while (0)
+
+ while (i < b_length && j < d_length) {
+ const git_tree_entry *b_entry = git_tree_entry_byindex(base, i);
+ const git_tree_entry *d_entry = git_tree_entry_byindex(delta, j);
+ int cmp = 0;
+
+ if (!git_oid_cmp(&b_entry->oid, &d_entry->oid))
+ goto loop;
+
+ cmp = memcmp(b_entry->filename,
+ d_entry->filename,
+ b_entry->filename_len);
+
+ /* If the entries are both trees and they have the same name but are
+ * different, then we'll recurse after adding the right-hand entry */
+ if (!cmp &&
+ git_tree_entry__is_tree(b_entry) &&
+ git_tree_entry__is_tree(d_entry)) {
+ /* Add the right-hand entry */
+ if ((error = git_packbuilder_insert(pb, &d_entry->oid,
+ d_entry->filename)) < 0)
+ goto on_error;
+
+ /* Acquire the subtrees and recurse */
+ if ((error = git_tree_lookup(&b_child,
+ git_tree_owner(base), &b_entry->oid)) < 0 ||
+ (error = git_tree_lookup(&d_child,
+ git_tree_owner(delta), &d_entry->oid)) < 0 ||
+ (error = queue_differences(b_child, d_child, pb)) < 0)
+ goto on_error;
+
+ git_tree_free(b_child); b_child = NULL;
+ git_tree_free(d_child); d_child = NULL;
+ }
+ /* If the object is new or different in the right-hand tree,
+ * then enumerate it */
+ else if (cmp >= 0)
+ _enqueue_object(d_entry);
+
+ loop:
+ if (cmp <= 0) i++;
+ if (cmp >= 0) j++;
+ }
+
+ /* Drain the right-hand tree of entries */
+ for (; j < d_length; j++)
+ _enqueue_object(git_tree_entry_byindex(delta, j));
+
+#undef _enqueue_object
+
+ error = 0;
+
+on_error:
+ if (b_child)
+ git_tree_free(b_child);
+
+ if (d_child)
+ git_tree_free(d_child);
+
+ return error;
+}
+
+static int queue_objects(git_push *push)
+{
+ git_vector commits = GIT_VECTOR_INIT;
+ git_oid *oid;
+ size_t i;
+ unsigned j;
+ int error;
if ((error = revwalk(&commits, push)) < 0)
goto on_error;
- if (!commits.length) {
- git_vector_free(&commits);
- return 0; /* nothing to do */
- }
+ git_vector_foreach(&commits, i, oid) {
+ git_commit *parent = NULL, *commit;
+ git_tree *tree = NULL, *ptree = NULL;
+ size_t parentcount;
- git_vector_foreach(&commits, i, o) {
- if ((error = git_packbuilder_insert(push->pb, o, NULL)) < 0)
+ if ((error = git_commit_lookup(&commit, push->repo, oid)) < 0)
goto on_error;
- }
- git_vector_foreach(&commits, i, o) {
- git_object *obj;
+ /* Insert the commit */
+ if ((error = git_packbuilder_insert(push->pb, oid, NULL)) < 0)
+ goto loop_error;
- if ((error = git_object_lookup(&obj, push->repo, o, GIT_OBJ_ANY)) < 0)
- goto on_error;
+ parentcount = git_commit_parentcount(commit);
- switch (git_object_type(obj)) {
- case GIT_OBJ_TAG: /* TODO: expect tags */
- case GIT_OBJ_COMMIT:
+ if (!parentcount) {
if ((error = git_packbuilder_insert_tree(push->pb,
- git_commit_tree_id((git_commit *)obj))) < 0) {
- git_object_free(obj);
- goto on_error;
+ git_commit_tree_id(commit))) < 0)
+ goto loop_error;
+ } else {
+ if ((error = git_tree_lookup(&tree, push->repo,
+ git_commit_tree_id(commit))) < 0 ||
+ (error = git_packbuilder_insert(push->pb,
+ git_commit_tree_id(commit), NULL)) < 0)
+ goto loop_error;
+
+ /* For each parent, add the items which are different */
+ for (j = 0; j < parentcount; j++) {
+ if ((error = git_commit_parent(&parent, commit, j)) < 0 ||
+ (error = git_commit_tree(&ptree, parent)) < 0 ||
+ (error = queue_differences(ptree, tree, push->pb)) < 0)
+ goto loop_error;
+
+ git_tree_free(ptree); ptree = NULL;
+ git_commit_free(parent); parent = NULL;
}
- break;
- case GIT_OBJ_TREE:
- case GIT_OBJ_BLOB:
- default:
- git_object_free(obj);
- giterr_set(GITERR_INVALID, "Given object type invalid");
- error = -1;
- goto on_error;
}
- git_object_free(obj);
+
+ error = 0;
+
+ loop_error:
+ if (tree)
+ git_tree_free(tree);
+
+ if (ptree)
+ git_tree_free(ptree);
+
+ if (parent)
+ git_commit_free(parent);
+
+ git_commit_free(commit);
+
+ if (error < 0)
+ goto on_error;
}
+
error = 0;
on_error:
- git_vector_foreach(&commits, i, o) {
- git__free(o);
- }
+ git_vector_foreach(&commits, i, oid)
+ git__free(oid);
+
git_vector_free(&commits);
return error;
}