summaryrefslogtreecommitdiff
path: root/hash-object.c
diff options
context:
space:
mode:
authorBryan Larsen <bryan.larsen@gmail.com>2005-07-08 23:51:55 (GMT)
committerLinus Torvalds <torvalds@g5.osdl.org>2005-07-09 00:07:37 (GMT)
commit7672db20c2060f20b01788e4a4289ebc5f818605 (patch)
tree0b5c5ed90d81628aa03df60ee5116d707f4a0803 /hash-object.c
parent7558ef89edce07555c6436cfcb98c31388dd99b0 (diff)
downloadgit-7672db20c2060f20b01788e4a4289ebc5f818605.zip
git-7672db20c2060f20b01788e4a4289ebc5f818605.tar.gz
git-7672db20c2060f20b01788e4a4289ebc5f818605.tar.bz2
[PATCH] Expose object ID computation functions.
This patch makes the first half of write_sha1_file() and index_fd() externally visible, to allow callers to compute the object ID without actually storing it in the object database. [JC demangled the whitespaces himself because he liked the patch so much, and reworked the interface to index_fd() slightly, taking suggestion from Linus and of his own.] Signed-off-by: Bryan Larsen <bryan.larsen@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'hash-object.c')
-rw-r--r--hash-object.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/hash-object.c b/hash-object.c
new file mode 100644
index 0000000..0821e54
--- /dev/null
+++ b/hash-object.c
@@ -0,0 +1,45 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ * Copyright (C) Junio C Hamano, 2005
+ */
+#include "cache.h"
+
+static void hash_object(const char *path, const char *type, int write_object)
+{
+ int fd;
+ struct stat st;
+ unsigned char sha1[20];
+ fd = open(path, O_RDONLY);
+ if (fd < 0 ||
+ fstat(fd, &st) < 0 ||
+ index_fd(sha1, fd, &st, write_object, type))
+ die(write_object
+ ? "Unable to add %s to database"
+ : "Unable to hash %s", path);
+ printf("%s\n", sha1_to_hex(sha1));
+}
+
+static const char *hash_object_usage =
+"git-hash-object [-t <type>] [-w] <file>...";
+
+int main(int argc, char **argv)
+{
+ int i;
+ const char *type = "blob";
+ int write_object = 0;
+
+ for (i = 1 ; i < argc; i++) {
+ if (!strcmp(argv[i], "-t")) {
+ if (argc <= ++i)
+ die(hash_object_usage);
+ type = argv[i];
+ }
+ else if (!strcmp(argv[i], "-w"))
+ write_object = 1;
+ else
+ hash_object(argv[i], type, write_object);
+ }
+ return 0;
+}