Skip to content

Commit 70c0f9d

Browse files
pks-tgitster
authored andcommitted
object-file: split up concerns of HASH_* flags
The functions `hash_object_file()`, `write_object_file()` and `index_fd()` reuse the same set of flags to alter their behaviour. This not only adds confusion, but given that every function only supports a subset of the flags it becomes very hard to see which flags can be passed to what function. Last but not least, this entangles the implementation of all three function families. Split up concerns by creating separate flags for each of the function families. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9f517d commit 70c0f9d

File tree

9 files changed

+56
-28
lines changed

9 files changed

+56
-28
lines changed

builtin/hash-object.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "strbuf.h"
2020
#include "write-or-die.h"
2121

22+
enum {
23+
HASH_OBJECT_CHECK = (1 << 0),
24+
HASH_OBJECT_WRITE = (1 << 1),
25+
};
26+
2227
/*
2328
* This is to create corrupt objects for debugging and as such it
2429
* needs to bypass the data conversion performed by, and the type
@@ -33,7 +38,7 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig
3338
ret = -1;
3439
else
3540
ret = write_object_file_literally(buf.buf, buf.len, type, oid,
36-
flags);
41+
(flags & HASH_OBJECT_WRITE) ? WRITE_OBJECT_FILE_PERSIST : 0);
3742
close(fd);
3843
strbuf_release(&buf);
3944
return ret;
@@ -42,15 +47,21 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig
4247
static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
4348
int literally)
4449
{
50+
unsigned int index_flags = 0;
4551
struct stat st;
4652
struct object_id oid;
4753

54+
if (flags & HASH_OBJECT_WRITE)
55+
index_flags |= INDEX_WRITE_OBJECT;
56+
if (flags & HASH_OBJECT_CHECK)
57+
index_flags |= INDEX_FORMAT_CHECK;
58+
4859
if (fstat(fd, &st) < 0 ||
4960
(literally
5061
? hash_literally(&oid, fd, type, flags)
5162
: index_fd(the_repository->index, &oid, fd, &st,
52-
type_from_string(type), path, flags)))
53-
die((flags & HASH_WRITE_OBJECT)
63+
type_from_string(type), path, index_flags)))
64+
die((flags & HASH_OBJECT_WRITE)
5465
? "Unable to add %s to database"
5566
: "Unable to hash %s", path);
5667
printf("%s\n", oid_to_hex(&oid));
@@ -102,13 +113,13 @@ int cmd_hash_object(int argc,
102113
int no_filters = 0;
103114
int literally = 0;
104115
int nongit = 0;
105-
unsigned flags = HASH_FORMAT_CHECK;
116+
unsigned flags = HASH_OBJECT_CHECK;
106117
const char *vpath = NULL;
107118
char *vpath_free = NULL;
108119
const struct option hash_object_options[] = {
109120
OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
110121
OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
111-
HASH_WRITE_OBJECT),
122+
HASH_OBJECT_WRITE),
112123
OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
113124
OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
114125
OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
@@ -122,7 +133,7 @@ int cmd_hash_object(int argc,
122133
argc = parse_options(argc, argv, prefix, hash_object_options,
123134
hash_object_usage, 0);
124135

125-
if (flags & HASH_WRITE_OBJECT)
136+
if (flags & HASH_OBJECT_WRITE)
126137
prefix = setup_git_directory();
127138
else
128139
prefix = setup_git_directory_gently(&nongit);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static int import_object(struct object_id *oid, enum object_type type,
305305
strbuf_release(&result);
306306
} else {
307307
struct stat st;
308-
int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
308+
int flags = INDEX_FORMAT_CHECK | INDEX_WRITE_OBJECT;
309309

310310
if (fstat(fd, &st) < 0) {
311311
error_errno(_("unable to fstat %s"), filename);

builtin/update-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
304304
ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
305305

306306
if (index_path(the_repository->index, &ce->oid, path, st,
307-
info_only ? 0 : HASH_WRITE_OBJECT)) {
307+
info_only ? 0 : INDEX_WRITE_OBJECT)) {
308308
discard_cache_entry(ce);
309309
return -1;
310310
}

bulk-checkin.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
171171
unsigned char obuf[16384];
172172
unsigned hdrlen;
173173
int status = Z_OK;
174-
int write_object = (flags & HASH_WRITE_OBJECT);
174+
int write_object = (flags & INDEX_WRITE_OBJECT);
175175
off_t offset = 0;
176176

177177
git_deflate_init(&s, pack_compression_level);
@@ -241,7 +241,7 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
241241
static void prepare_to_stream(struct bulk_checkin_packfile *state,
242242
unsigned flags)
243243
{
244-
if (!(flags & HASH_WRITE_OBJECT) || state->f)
244+
if (!(flags & INDEX_WRITE_OBJECT) || state->f)
245245
return;
246246

247247
state->f = create_tmp_packfile(the_repository, &state->pack_tmp_name);
@@ -275,7 +275,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
275275
git_hash_update(&ctx, obuf, header_len);
276276

277277
/* Note: idx is non-NULL when we are writing */
278-
if ((flags & HASH_WRITE_OBJECT) != 0) {
278+
if ((flags & INDEX_WRITE_OBJECT) != 0) {
279279
CALLOC_ARRAY(idx, 1);
280280

281281
prepare_to_stream(state, flags);

cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ static int update_one(struct cache_tree *it,
452452
OBJ_TREE, &it->oid);
453453
} else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
454454
&it->oid, NULL, flags & WRITE_TREE_SILENT
455-
? HASH_SILENT : 0)) {
455+
? WRITE_OBJECT_FILE_SILENT : 0)) {
456456
strbuf_release(&buffer);
457457
return -1;
458458
}

notes-merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ int notes_merge_commit(struct notes_merge_options *o,
729729
/* write file as blob, and add to partial_tree */
730730
if (stat(path.buf, &st))
731731
die_errno("Failed to stat '%s'", path.buf);
732-
if (index_path(o->repo->index, &blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
732+
if (index_path(o->repo->index, &blob_oid, path.buf, &st, INDEX_WRITE_OBJECT))
733733
die("Failed to write blob object from '%s'", path.buf);
734734
if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
735735
die("Failed to add resolved note '%s' to notes tree",

object-file.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434
static int get_conv_flags(unsigned flags)
3535
{
36-
if (flags & HASH_RENORMALIZE)
36+
if (flags & INDEX_RENORMALIZE)
3737
return CONV_EOL_RENORMALIZE;
38-
else if (flags & HASH_WRITE_OBJECT)
38+
else if (flags & INDEX_WRITE_OBJECT)
3939
return global_conv_flags_eol | CONV_WRITE_OBJECT;
4040
else
4141
return 0;
@@ -835,7 +835,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
835835

836836
fd = create_tmpfile(tmp_file, filename);
837837
if (fd < 0) {
838-
if (flags & HASH_SILENT)
838+
if (flags & WRITE_OBJECT_FILE_SILENT)
839839
return -1;
840840
else if (errno == EACCES)
841841
return error(_("insufficient permission for adding "
@@ -967,7 +967,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
967967
utb.actime = mtime;
968968
utb.modtime = mtime;
969969
if (utime(tmp_file.buf, &utb) < 0 &&
970-
!(flags & HASH_SILENT))
970+
!(flags & WRITE_OBJECT_FILE_SILENT))
971971
warning_errno(_("failed utime() on %s"), tmp_file.buf);
972972
}
973973

@@ -1179,7 +1179,7 @@ int write_object_file_literally(const void *buf, unsigned long len,
11791179
write_object_file_prepare_literally(the_hash_algo, buf, len, type,
11801180
oid, header, &hdrlen);
11811181

1182-
if (!(flags & HASH_WRITE_OBJECT))
1182+
if (!(flags & WRITE_OBJECT_FILE_PERSIST))
11831183
goto cleanup;
11841184
if (freshen_packed_object(oid) || freshen_loose_object(oid))
11851185
goto cleanup;
@@ -1250,7 +1250,7 @@ static int index_mem(struct index_state *istate,
12501250
{
12511251
struct strbuf nbuf = STRBUF_INIT;
12521252
int ret = 0;
1253-
int write_object = flags & HASH_WRITE_OBJECT;
1253+
int write_object = flags & INDEX_WRITE_OBJECT;
12541254

12551255
if (!type)
12561256
type = OBJ_BLOB;
@@ -1265,7 +1265,7 @@ static int index_mem(struct index_state *istate,
12651265
size = nbuf.len;
12661266
}
12671267
}
1268-
if (flags & HASH_FORMAT_CHECK) {
1268+
if (flags & INDEX_FORMAT_CHECK) {
12691269
struct fsck_options opts = FSCK_OPTIONS_DEFAULT;
12701270

12711271
opts.strict = 1;
@@ -1291,7 +1291,7 @@ static int index_stream_convert_blob(struct index_state *istate,
12911291
unsigned flags)
12921292
{
12931293
int ret = 0;
1294-
const int write_object = flags & HASH_WRITE_OBJECT;
1294+
const int write_object = flags & INDEX_WRITE_OBJECT;
12951295
struct strbuf sbuf = STRBUF_INIT;
12961296

12971297
assert(path);
@@ -1423,7 +1423,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
14231423
case S_IFLNK:
14241424
if (strbuf_readlink(&sb, path, st->st_size))
14251425
return error_errno("readlink(\"%s\")", path);
1426-
if (!(flags & HASH_WRITE_OBJECT))
1426+
if (!(flags & INDEX_WRITE_OBJECT))
14271427
hash_object_file(the_hash_algo, sb.buf, sb.len,
14281428
OBJ_BLOB, oid);
14291429
else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))

object-file.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ struct index_state;
1414
*/
1515
extern int fetch_if_missing;
1616

17-
#define HASH_WRITE_OBJECT 1
18-
#define HASH_FORMAT_CHECK 2
19-
#define HASH_RENORMALIZE 4
20-
#define HASH_SILENT 8
17+
enum {
18+
INDEX_WRITE_OBJECT = (1 << 0),
19+
INDEX_FORMAT_CHECK = (1 << 1),
20+
INDEX_RENORMALIZE = (1 << 2),
21+
};
22+
2123
int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
2224
int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
2325

@@ -84,6 +86,21 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
8486
struct object_info;
8587
int parse_loose_header(const char *hdr, struct object_info *oi);
8688

89+
enum {
90+
/*
91+
* By default, `write_object_file_literally()` does not actually write
92+
* anything into the object store, but only computes the object ID.
93+
* This flag changes that so that the object will be written as a loose
94+
* object and persisted.
95+
*/
96+
WRITE_OBJECT_FILE_PERSIST = (1 << 0),
97+
98+
/*
99+
* Do not print an error in case something gose wrong.
100+
*/
101+
WRITE_OBJECT_FILE_SILENT = (1 << 1),
102+
};
103+
87104
int write_object_file_flags(const void *buf, unsigned long len,
88105
enum object_type type, struct object_id *oid,
89106
struct object_id *comapt_oid_in, unsigned flags);

read-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,11 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
706706
int intent_only = flags & ADD_CACHE_INTENT;
707707
int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
708708
(intent_only ? ADD_CACHE_NEW_ONLY : 0));
709-
unsigned hash_flags = pretend ? 0 : HASH_WRITE_OBJECT;
709+
unsigned hash_flags = pretend ? 0 : INDEX_WRITE_OBJECT;
710710
struct object_id oid;
711711

712712
if (flags & ADD_CACHE_RENORMALIZE)
713-
hash_flags |= HASH_RENORMALIZE;
713+
hash_flags |= INDEX_RENORMALIZE;
714714

715715
if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
716716
return error(_("%s: can only add regular files, symbolic links or git-directories"), path);

0 commit comments

Comments
 (0)