Skip to content

Commit ce2bc42

Browse files
peffgitster
authored andcommitted
pack-objects: split add_object_entry
This function actually does three things: 1. Check whether we've already added the object to our packing list. 2. Check whether the object meets our criteria for adding. 3. Actually add the object to our packing list. It's a little hard to see these three phases, because they happen linearly in the rather long function. Instead, this patch breaks them up into three separate helper functions. The result is a little easier to follow, though it unfortunately suffers from some optimization interdependencies between the stages (e.g., during step 3 we use the packing list index from step 1 and the packfile information from step 2). More importantly, though, the various parts can be composed differently, as they will be in the next patch. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fff4275 commit ce2bc42

File tree

1 file changed

+78
-20
lines changed

1 file changed

+78
-20
lines changed

builtin/pack-objects.c

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -800,41 +800,69 @@ static int no_try_delta(const char *path)
800800
return 0;
801801
}
802802

803-
static int add_object_entry(const unsigned char *sha1, enum object_type type,
804-
const char *name, int exclude)
803+
/*
804+
* When adding an object, check whether we have already added it
805+
* to our packing list. If so, we can skip. However, if we are
806+
* being asked to excludei t, but the previous mention was to include
807+
* it, make sure to adjust its flags and tweak our numbers accordingly.
808+
*
809+
* As an optimization, we pass out the index position where we would have
810+
* found the item, since that saves us from having to look it up again a
811+
* few lines later when we want to add the new entry.
812+
*/
813+
static int have_duplicate_entry(const unsigned char *sha1,
814+
int exclude,
815+
uint32_t *index_pos)
805816
{
806817
struct object_entry *entry;
807-
struct packed_git *p, *found_pack = NULL;
808-
off_t found_offset = 0;
809-
uint32_t hash = pack_name_hash(name);
810-
uint32_t index_pos;
811818

812-
entry = packlist_find(&to_pack, sha1, &index_pos);
813-
if (entry) {
814-
if (exclude) {
815-
if (!entry->preferred_base)
816-
nr_result--;
817-
entry->preferred_base = 1;
818-
}
819+
entry = packlist_find(&to_pack, sha1, index_pos);
820+
if (!entry)
819821
return 0;
822+
823+
if (exclude) {
824+
if (!entry->preferred_base)
825+
nr_result--;
826+
entry->preferred_base = 1;
820827
}
821828

829+
return 1;
830+
}
831+
832+
/*
833+
* Check whether we want the object in the pack (e.g., we do not want
834+
* objects found in non-local stores if the "--local" option was used).
835+
*
836+
* As a side effect of this check, we will find the packed version of this
837+
* object, if any. We therefore pass out the pack information to avoid having
838+
* to look it up again later.
839+
*/
840+
static int want_object_in_pack(const unsigned char *sha1,
841+
int exclude,
842+
struct packed_git **found_pack,
843+
off_t *found_offset)
844+
{
845+
struct packed_git *p;
846+
822847
if (!exclude && local && has_loose_object_nonlocal(sha1))
823848
return 0;
824849

850+
*found_pack = NULL;
851+
*found_offset = 0;
852+
825853
for (p = packed_git; p; p = p->next) {
826854
off_t offset = find_pack_entry_one(sha1, p);
827855
if (offset) {
828-
if (!found_pack) {
856+
if (!*found_pack) {
829857
if (!is_pack_valid(p)) {
830858
warning("packfile %s cannot be accessed", p->pack_name);
831859
continue;
832860
}
833-
found_offset = offset;
834-
found_pack = p;
861+
*found_offset = offset;
862+
*found_pack = p;
835863
}
836864
if (exclude)
837-
break;
865+
return 1;
838866
if (incremental)
839867
return 0;
840868
if (local && !p->pack_local)
@@ -844,6 +872,20 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
844872
}
845873
}
846874

875+
return 1;
876+
}
877+
878+
static void create_object_entry(const unsigned char *sha1,
879+
enum object_type type,
880+
uint32_t hash,
881+
int exclude,
882+
int no_try_delta,
883+
uint32_t index_pos,
884+
struct packed_git *found_pack,
885+
off_t found_offset)
886+
{
887+
struct object_entry *entry;
888+
847889
entry = packlist_alloc(&to_pack, sha1, index_pos);
848890
entry->hash = hash;
849891
if (type)
@@ -857,11 +899,27 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
857899
entry->in_pack_offset = found_offset;
858900
}
859901

860-
display_progress(progress_state, to_pack.nr_objects);
902+
entry->no_try_delta = no_try_delta;
903+
}
904+
905+
static int add_object_entry(const unsigned char *sha1, enum object_type type,
906+
const char *name, int exclude)
907+
{
908+
struct packed_git *found_pack;
909+
off_t found_offset;
910+
uint32_t index_pos;
861911

862-
if (name && no_try_delta(name))
863-
entry->no_try_delta = 1;
912+
if (have_duplicate_entry(sha1, exclude, &index_pos))
913+
return 0;
864914

915+
if (!want_object_in_pack(sha1, exclude, &found_pack, &found_offset))
916+
return 0;
917+
918+
create_object_entry(sha1, type, pack_name_hash(name),
919+
exclude, name && no_try_delta(name),
920+
index_pos, found_pack, found_offset);
921+
922+
display_progress(progress_state, to_pack.nr_objects);
865923
return 1;
866924
}
867925

0 commit comments

Comments
 (0)