Code

[PATCH] Expose object ID computation functions.
authorBryan Larsen <bryan.larsen@gmail.com>
Fri, 8 Jul 2005 23:51:55 +0000 (16:51 -0700)
committerLinus Torvalds <torvalds@g5.osdl.org>
Sat, 9 Jul 2005 00:07:37 +0000 (17:07 -0700)
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>
Documentation/git-hash-object.txt [new file with mode: 0644]
Documentation/git-write-blob.txt [deleted file]
Documentation/git.txt
Makefile
README
cache.h
git-cvsimport-script
hash-object.c [new file with mode: 0644]
sha1_file.c
update-cache.c
write-blob.c [deleted file]

diff --git a/Documentation/git-hash-object.txt b/Documentation/git-hash-object.txt
new file mode 100644 (file)
index 0000000..8790d19
--- /dev/null
@@ -0,0 +1,36 @@
+git-hash-object(1)
+==================
+v0.1, May 2005
+
+NAME
+----
+git-hash-object - Computes object ID and optionally creates a blob from a file.
+
+
+SYNOPSIS
+--------
+'git-hash-object' [-t <type>] [-w] <any-file-on-the-filesystem>
+
+DESCRIPTION
+-----------
+Computes the object ID value for an object with specified type
+with the contents of the named file (which can be outside of the
+work tree), and optionally writes the resulting object into the
+object database.  Reports its object ID to its standard output.
+This is used by "git-cvsimport-script" to update the cache
+without modifying files in the work tree.  When <type> is not
+specified, it defaults to "blob". 
+
+
+Author
+------
+Written by Junio C Hamano <junkio@cox.net>
+
+Documentation
+--------------
+Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
+
+GIT
+---
+Part of the link:git.html[git] suite
+
diff --git a/Documentation/git-write-blob.txt b/Documentation/git-write-blob.txt
deleted file mode 100644 (file)
index 22d7555..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-git-write-blob(1)
-=================
-v0.1, May 2005
-
-NAME
-----
-git-write-blob - Creates a blob from a file
-
-
-SYNOPSIS
---------
-'git-write-blob' <any-file-on-the-filesystem>
-
-DESCRIPTION
------------
-Writes the contents of the named file (which can be outside of the work
-tree) as a blob into the object database, and reports its object ID to its
-standard output.  This is used by "git-merge-one-file-script" to update the
-cache without modifying files in the work tree.
-
-
-Author
-------
-Written by Linus Torvalds <torvalds@osdl.org>
-
-Documentation
---------------
-Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
-
-GIT
----
-Part of the link:git.html[git] suite
-
index 797eb332785d3b84b792e4bc6368490d66a51e0d..06205024b2628d13cf9ba8297d15428b94ca4c1d 100644 (file)
@@ -60,8 +60,8 @@ link:git-read-tree.html[git-read-tree]::
 link:git-update-cache.html[git-update-cache]::
        Modifies the index or directory cache
 
-link:git-write-blob.html[git-write-blob]::
-       Creates a blob from a file
+link:git-hash-object.html[git-hash-object]::
+       Computes the object ID from a file.
 
 link:git-write-tree.html[git-write-tree]::
        Creates a tree from the current cache
index 1add2ae6f5a23111cb23f2ea9d0e0e865270240c..11f5bcd90f2f642c877cdcd6db7fa09df09a7d0d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,7 @@ PROG=   git-update-cache git-diff-files git-init-db git-write-tree \
        git-check-files git-ls-tree git-merge-base git-merge-cache \
        git-unpack-file git-export git-diff-cache git-convert-cache \
        git-http-pull git-ssh-push git-ssh-pull git-rev-list git-mktag \
-       git-diff-helper git-tar-tree git-local-pull git-write-blob \
+       git-diff-helper git-tar-tree git-local-pull git-hash-object \
        git-get-tar-commit-id git-apply git-stripspace \
        git-diff-stages git-rev-parse git-patch-id git-pack-objects \
        git-unpack-objects git-verify-pack git-receive-pack git-send-pack \
@@ -135,7 +135,7 @@ git-rev-list: rev-list.c
 git-mktag: mktag.c
 git-diff-helper: diff-helper.c
 git-tar-tree: tar-tree.c
-git-write-blob: write-blob.c
+git-hash-object: hash-object.c
 git-stripspace: stripspace.c
 git-diff-stages: diff-stages.c
 git-rev-parse: rev-parse.c
diff --git a/README b/README
index 1b8d9b8b9771ce941a3a6f8f98fb8c197f61f4e0..80cc27913e449fbc25f2df1c515abc294ba9afde 100644 (file)
--- a/README
+++ b/README
@@ -102,8 +102,8 @@ object. The object is totally independent of it's location in the
 directory tree, and renaming a file does not change the object that
 file is associated with in any way.
 
-A blob is created with link:git-write-blob.html[git-write-blob] and
-it's data can be accessed by link:git-cat-file.html[git-cat-file]
+A blob is typically created when link:git-update-cache.html[git-update-cache]
+is run, and it's data can be accessed by link:git-cat-file.html[git-cat-file].
 
 Tree Object
 ~~~~~~~~~~~
diff --git a/cache.h b/cache.h
index ff0321341d99c233e788f9d55fab88182fd981de..84d43d366c6145a30865aa65d92ada88ab95bb9f 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -138,7 +138,7 @@ extern int remove_cache_entry_at(int pos);
 extern int remove_file_from_cache(char *path);
 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
 extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
-extern int index_fd(unsigned char *sha1, int fd, struct stat *st);
+extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
 
 struct cache_file {
@@ -172,6 +172,12 @@ extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
 extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
+extern char *write_sha1_file_prepare(void *buf,
+                                    unsigned long len,
+                                    const char *type,
+                                    unsigned char *sha1,
+                                    unsigned char *hdr,
+                                    int *hdrlen);
 
 extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
 
index 6d5a7fad38b128876903c96ce3badcc01001b185..666de6f09a0c5bd526ec8c526cdfdec15432b580 100755 (executable)
@@ -683,7 +683,7 @@ while(<CVS>) {
                $fn =~ s#^/+##;
                my ($tmpname, $size) = $cvs->file($fn,$rev);
                print "".($init ? "New" : "Update")." $fn: $size bytes.\n" if $opt_v;
-               open my $F, '-|', "git-write-blob $tmpname"
+               open my $F, '-|', "git-hash-object -w $tmpname"
                        or die "Cannot create object: $!\n";
                my $sha = <$F>;
                chomp $sha;
diff --git a/hash-object.c b/hash-object.c
new file mode 100644 (file)
index 0000000..0821e54
--- /dev/null
@@ -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;
+}
index fc4e6bf91f1b402f2e69c1521498551d5e82acb4..b2914dd2ea629ae974fd4b4c272e77cb04e5c0e0 100644 (file)
@@ -1100,12 +1100,12 @@ void *read_object_with_reference(const unsigned char *sha1,
        }
 }
 
-static char *write_sha1_file_prepare(void *buf,
-                                    unsigned long len,
-                                    const char *type,
-                                    unsigned char *sha1,
-                                    unsigned char *hdr,
-                                    int *hdrlen)
+char *write_sha1_file_prepare(void *buf,
+                             unsigned long len,
+                             const char *type,
+                             unsigned char *sha1,
+                             unsigned char *hdr,
+                             int *hdrlen)
 {
        SHA_CTX c;
 
@@ -1299,11 +1299,13 @@ int has_sha1_file(const unsigned char *sha1)
        return find_pack_entry(sha1, &e);
 }
 
-int index_fd(unsigned char *sha1, int fd, struct stat *st)
+int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
 {
        unsigned long size = st->st_size;
        void *buf;
        int ret;
+       unsigned char hdr[50];
+       int hdrlen;
 
        buf = "";
        if (size)
@@ -1312,7 +1314,14 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st)
        if ((int)(long)buf == -1)
                return -1;
 
-       ret = write_sha1_file(buf, size, "blob", sha1);
+       if (!type)
+               type = "blob";
+       if (write_object)
+               ret = write_sha1_file(buf, size, type, sha1);
+       else {
+               write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
+               ret = 0;
+       }
        if (size)
                munmap(buf, size);
        return ret;
index 7c2698df6e7b08315e7d20bd40c1e7e313f52bf3..d5225c37644086f9b3c0b911c6e283fb5ec50c8e 100644 (file)
@@ -68,7 +68,7 @@ static int add_file_to_cache(char *path)
                fd = open(path, O_RDONLY);
                if (fd < 0)
                        return -1;
-               if (index_fd(ce->sha1, fd, &st) < 0)
+               if (index_fd(ce->sha1, fd, &st, 1, NULL) < 0)
                        return -1;
                break;
        case S_IFLNK:
diff --git a/write-blob.c b/write-blob.c
deleted file mode 100644 (file)
index 8bfd576..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-
-int main(int argc, char **argv)
-{
-       int i;
-
-       for (i = 1 ; i < argc; i++) {
-               char *path = argv[i];
-               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) < 0)
-                       die("Unable to add blob %s to database", path);
-               printf("%s\n", sha1_to_hex(sha1));
-       }
-       return 0;
-}