aboutsummaryrefslogtreecommitdiffstats
path: root/builtin/hash-object.c
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/hash-object.c
parentobject-file: split out functions relating to object store subsystem (diff)
downloadgit-70c0f9db4e00586e4df5cca24fe7ce05848ee59c.tar.gz
git-70c0f9db4e00586e4df5cca24fe7ce05848ee59c.zip
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/hash-object.c')
-rw-r--r--builtin/hash-object.c23
1 files changed, 17 insertions, 6 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);