aboutsummaryrefslogtreecommitdiffstats
path: root/builtin
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-04-15 11:38:19 +0200
committerJunio C Hamano <gitster@pobox.com>2025-04-15 08:24:36 -0700
commit70c0f9db4e00586e4df5cca24fe7ce05848ee59c (patch)
tree54480f65c224dbe45dd0d35bbecbbfd07eac6044 /builtin
parentd9f517d051d1008178cb6c809b5f906d0905508f (diff)
downloadgit-70c0f9db4e00586e4df5cca24fe7ce05848ee59c.tar.gz
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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/hash-object.c23
-rw-r--r--builtin/replace.c2
-rw-r--r--builtin/update-index.c2
3 files changed, 19 insertions, 8 deletions
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index a25f0403f4..e7c0d6afde 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -19,6 +19,11 @@
#include "strbuf.h"
#include "write-or-die.h"
+enum {
+ HASH_OBJECT_CHECK = (1 << 0),
+ HASH_OBJECT_WRITE = (1 << 1),
+};
+
/*
* This is to create corrupt objects for debugging and as such it
* 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
ret = -1;
else
ret = write_object_file_literally(buf.buf, buf.len, type, oid,
- flags);
+ (flags & HASH_OBJECT_WRITE) ? WRITE_OBJECT_FILE_PERSIST : 0);
close(fd);
strbuf_release(&buf);
return ret;
@@ -42,15 +47,21 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig
static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
int literally)
{
+ unsigned int index_flags = 0;
struct stat st;
struct object_id oid;
+ if (flags & HASH_OBJECT_WRITE)
+ index_flags |= INDEX_WRITE_OBJECT;
+ if (flags & HASH_OBJECT_CHECK)
+ index_flags |= INDEX_FORMAT_CHECK;
+
if (fstat(fd, &st) < 0 ||
(literally
? hash_literally(&oid, fd, type, flags)
: index_fd(the_repository->index, &oid, fd, &st,
- type_from_string(type), path, flags)))
- die((flags & HASH_WRITE_OBJECT)
+ type_from_string(type), path, index_flags)))
+ die((flags & HASH_OBJECT_WRITE)
? "Unable to add %s to database"
: "Unable to hash %s", path);
printf("%s\n", oid_to_hex(&oid));
@@ -102,13 +113,13 @@ int cmd_hash_object(int argc,
int no_filters = 0;
int literally = 0;
int nongit = 0;
- unsigned flags = HASH_FORMAT_CHECK;
+ unsigned flags = HASH_OBJECT_CHECK;
const char *vpath = NULL;
char *vpath_free = NULL;
const struct option hash_object_options[] = {
OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
- HASH_WRITE_OBJECT),
+ HASH_OBJECT_WRITE),
OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
@@ -122,7 +133,7 @@ int cmd_hash_object(int argc,
argc = parse_options(argc, argv, prefix, hash_object_options,
hash_object_usage, 0);
- if (flags & HASH_WRITE_OBJECT)
+ if (flags & HASH_OBJECT_WRITE)
prefix = setup_git_directory();
else
prefix = setup_git_directory_gently(&nongit);
diff --git a/builtin/replace.c b/builtin/replace.c
index 15ec0922ce..2b4fc9a68b 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -305,7 +305,7 @@ static int import_object(struct object_id *oid, enum object_type type,
strbuf_release(&result);
} else {
struct stat st;
- int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
+ int flags = INDEX_FORMAT_CHECK | INDEX_WRITE_OBJECT;
if (fstat(fd, &st) < 0) {
error_errno(_("unable to fstat %s"), filename);
diff --git a/builtin/update-index.c b/builtin/update-index.c
index b2f6b1a3fb..f0cf964294 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -304,7 +304,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
if (index_path(the_repository->index, &ce->oid, path, st,
- info_only ? 0 : HASH_WRITE_OBJECT)) {
+ info_only ? 0 : INDEX_WRITE_OBJECT)) {
discard_cache_entry(ce);
return -1;
}