Skip to content

Commit 3e30cb0

Browse files
committed
Merge branch 'mh/replace-refs-variable-rename'
* mh/replace-refs-variable-rename: Document some functions defined in object.c Add docstrings for lookup_replace_object() and do_lookup_replace_object() rename read_replace_refs to check_replace_refs
2 parents d552f8d + 33bef7e commit 3e30cb0

14 files changed

+79
-15
lines changed

builtin/fsck.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
630630
struct alternate_object_database *alt;
631631

632632
errors_found = 0;
633-
read_replace_refs = 0;
633+
check_replace_refs = 0;
634634

635635
argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
636636

builtin/index-pack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
15021502
if (argc == 2 && !strcmp(argv[1], "-h"))
15031503
usage(index_pack_usage);
15041504

1505-
read_replace_refs = 0;
1505+
check_replace_refs = 0;
15061506

15071507
reset_pack_idx_option(&opts);
15081508
git_config(git_index_pack_config, &opts);

builtin/pack-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
26122612
OPT_END(),
26132613
};
26142614

2615-
read_replace_refs = 0;
2615+
check_replace_refs = 0;
26162616

26172617
reset_pack_idx_option(&pack_idx_opts);
26182618
git_config(git_pack_config, NULL);

builtin/prune.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
150150

151151
expire = ULONG_MAX;
152152
save_commit_buffer = 0;
153-
read_replace_refs = 0;
153+
check_replace_refs = 0;
154154
init_revisions(&revs, prefix);
155155

156156
argc = parse_options(argc, argv, prefix, options, prune_usage, 0);

builtin/replace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
178178
OPT_END()
179179
};
180180

181-
read_replace_refs = 0;
181+
check_replace_refs = 0;
182182

183183
argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
184184

builtin/unpack-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
497497
int i;
498498
unsigned char sha1[20];
499499

500-
read_replace_refs = 0;
500+
check_replace_refs = 0;
501501

502502
git_config(git_default_config, NULL);
503503

cache.h

+25-2
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,17 @@ extern size_t packed_git_limit;
581581
extern size_t delta_base_cache_limit;
582582
extern unsigned long big_file_threshold;
583583
extern unsigned long pack_size_limit_cfg;
584-
extern int read_replace_refs;
584+
585+
/*
586+
* Do replace refs need to be checked this run? This variable is
587+
* initialized to true unless --no-replace-object is used or
588+
* $GIT_NO_REPLACE_OBJECTS is set, but is set to false by some
589+
* commands that do not want replace references to be active. As an
590+
* optimization it is also set to false if replace references have
591+
* been sought but there were none.
592+
*/
593+
extern int check_replace_refs;
594+
585595
extern int fsync_object_files;
586596
extern int core_preload_index;
587597
extern int core_apply_sparse_checkout;
@@ -808,13 +818,26 @@ static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *
808818
{
809819
return read_sha1_file_extended(sha1, type, size, LOOKUP_REPLACE_OBJECT);
810820
}
821+
822+
/*
823+
* This internal function is only declared here for the benefit of
824+
* lookup_replace_object(). Please do not call it directly.
825+
*/
811826
extern const unsigned char *do_lookup_replace_object(const unsigned char *sha1);
827+
828+
/*
829+
* If object sha1 should be replaced, return the replacement object's
830+
* name (replaced recursively, if necessary). The return value is
831+
* either sha1 or a pointer to a permanently-allocated value. When
832+
* object replacement is suppressed, always return sha1.
833+
*/
812834
static inline const unsigned char *lookup_replace_object(const unsigned char *sha1)
813835
{
814-
if (!read_replace_refs)
836+
if (!check_replace_refs)
815837
return sha1;
816838
return do_lookup_replace_object(sha1);
817839
}
840+
818841
static inline const unsigned char *lookup_replace_object_extended(const unsigned char *sha1, unsigned flag)
819842
{
820843
if (!(flag & LOOKUP_REPLACE_OBJECT))

environment.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const char *editor_program;
4545
const char *askpass_program;
4646
const char *excludes_file;
4747
enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
48-
int read_replace_refs = 1; /* NEEDSWORK: rename to use_replace_refs */
48+
int check_replace_refs = 1;
4949
enum eol core_eol = EOL_UNSET;
5050
enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
5151
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
@@ -147,7 +147,7 @@ static void setup_git_env(void)
147147
if (!git_graft_file)
148148
git_graft_file = git_pathdup("info/grafts");
149149
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
150-
read_replace_refs = 0;
150+
check_replace_refs = 0;
151151
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
152152
namespace_len = strlen(namespace);
153153
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);

git.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
7878
if (envchanged)
7979
*envchanged = 1;
8080
} else if (!strcmp(cmd, "--no-replace-objects")) {
81-
read_replace_refs = 0;
81+
check_replace_refs = 0;
8282
setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
8383
if (envchanged)
8484
*envchanged = 1;

log-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
100100

101101
if (starts_with(refname, "refs/replace/")) {
102102
unsigned char original_sha1[20];
103-
if (!read_replace_refs)
103+
if (!check_replace_refs)
104104
return 0;
105105
if (get_sha1_hex(refname + 13, original_sha1)) {
106106
warning("invalid replace ref %s", refname);

object.c

+28-1
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ int type_from_string(const char *str)
4343
die("invalid object type \"%s\"", str);
4444
}
4545

46+
/*
47+
* Return a numerical hash value between 0 and n-1 for the object with
48+
* the specified sha1. n must be a power of 2. Please note that the
49+
* return value is *not* consistent across computer architectures.
50+
*/
4651
static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
4752
{
4853
unsigned int hash;
54+
55+
/*
56+
* Since the sha1 is essentially random, we just take the
57+
* required number of bits directly from the first
58+
* sizeof(unsigned int) bytes of sha1. First we have to copy
59+
* the bytes into a properly aligned integer. If we cared
60+
* about getting consistent results across architectures, we
61+
* would have to call ntohl() here, too.
62+
*/
4963
memcpy(&hash, sha1, sizeof(unsigned int));
50-
/* Assumes power-of-2 hash sizes in grow_object_hash */
5164
return hash & (n - 1);
5265
}
5366

67+
/*
68+
* Insert obj into the hash table hash, which has length size (which
69+
* must be a power of 2). On collisions, simply overflow to the next
70+
* empty bucket.
71+
*/
5472
static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
5573
{
5674
unsigned int j = hash_obj(obj->sha1, size);
@@ -63,6 +81,10 @@ static void insert_obj_hash(struct object *obj, struct object **hash, unsigned i
6381
hash[j] = obj;
6482
}
6583

84+
/*
85+
* Look up the record for the given sha1 in the hash map stored in
86+
* obj_hash. Return NULL if it was not found.
87+
*/
6688
struct object *lookup_object(const unsigned char *sha1)
6789
{
6890
unsigned int i, first;
@@ -92,6 +114,11 @@ struct object *lookup_object(const unsigned char *sha1)
92114
return obj;
93115
}
94116

117+
/*
118+
* Increase the size of the hash map stored in obj_hash to the next
119+
* power of 2 (but at least 32). Copy the existing values to the new
120+
* hash map.
121+
*/
95122
static void grow_object_hash(void)
96123
{
97124
int i;

object.h

+7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ struct object {
4242
extern const char *typename(unsigned int type);
4343
extern int type_from_string(const char *str);
4444

45+
/*
46+
* Return the current number of buckets in the object hashmap.
47+
*/
4548
extern unsigned int get_max_object_index(void);
49+
50+
/*
51+
* Return the object from the specified bucket in the object hashmap.
52+
*/
4653
extern struct object *get_indexed_object(unsigned int);
4754

4855
/*

replace_object.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,19 @@ static void prepare_replace_object(void)
9191
for_each_replace_ref(register_replace_ref, NULL);
9292
replace_object_prepared = 1;
9393
if (!replace_object_nr)
94-
read_replace_refs = 0;
94+
check_replace_refs = 0;
9595
}
9696

9797
/* We allow "recursive" replacement. Only within reason, though */
9898
#define MAXREPLACEDEPTH 5
9999

100+
/*
101+
* If a replacement for object sha1 has been set up, return the
102+
* replacement object's name (replaced recursively, if necessary).
103+
* The return value is either sha1 or a pointer to a
104+
* permanently-allocated value. This function always respects replace
105+
* references, regardless of the value of check_replace_refs.
106+
*/
100107
const unsigned char *do_lookup_replace_object(const unsigned char *sha1)
101108
{
102109
int pos, depth = MAXREPLACEDEPTH;

upload-pack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ int main(int argc, char **argv)
796796

797797
packet_trace_identity("upload-pack");
798798
git_extract_argv0_path(argv[0]);
799-
read_replace_refs = 0;
799+
check_replace_refs = 0;
800800

801801
for (i = 1; i < argc; i++) {
802802
char *arg = argv[i];

0 commit comments

Comments
 (0)