Skip to content

Commit 7672db2

Browse files
bryanlarsenLinus Torvalds
authored andcommitted
[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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7558ef8 commit 7672db2

File tree

11 files changed

+113
-75
lines changed

11 files changed

+113
-75
lines changed

Documentation/git-hash-object.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
git-hash-object(1)
2+
==================
3+
v0.1, May 2005
4+
5+
NAME
6+
----
7+
git-hash-object - Computes object ID and optionally creates a blob from a file.
8+
9+
10+
SYNOPSIS
11+
--------
12+
'git-hash-object' [-t <type>] [-w] <any-file-on-the-filesystem>
13+
14+
DESCRIPTION
15+
-----------
16+
Computes the object ID value for an object with specified type
17+
with the contents of the named file (which can be outside of the
18+
work tree), and optionally writes the resulting object into the
19+
object database. Reports its object ID to its standard output.
20+
This is used by "git-cvsimport-script" to update the cache
21+
without modifying files in the work tree. When <type> is not
22+
specified, it defaults to "blob".
23+
24+
25+
Author
26+
------
27+
Written by Junio C Hamano <[email protected]>
28+
29+
Documentation
30+
--------------
31+
Documentation by David Greaves, Junio C Hamano and the git-list <[email protected]>.
32+
33+
GIT
34+
---
35+
Part of the link:git.html[git] suite
36+

Documentation/git-write-blob.txt

Lines changed: 0 additions & 33 deletions
This file was deleted.

Documentation/git.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ link:git-read-tree.html[git-read-tree]::
6060
link:git-update-cache.html[git-update-cache]::
6161
Modifies the index or directory cache
6262

63-
link:git-write-blob.html[git-write-blob]::
64-
Creates a blob from a file
63+
link:git-hash-object.html[git-hash-object]::
64+
Computes the object ID from a file.
6565

6666
link:git-write-tree.html[git-write-tree]::
6767
Creates a tree from the current cache

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PROG= git-update-cache git-diff-files git-init-db git-write-tree \
4242
git-check-files git-ls-tree git-merge-base git-merge-cache \
4343
git-unpack-file git-export git-diff-cache git-convert-cache \
4444
git-http-pull git-ssh-push git-ssh-pull git-rev-list git-mktag \
45-
git-diff-helper git-tar-tree git-local-pull git-write-blob \
45+
git-diff-helper git-tar-tree git-local-pull git-hash-object \
4646
git-get-tar-commit-id git-apply git-stripspace \
4747
git-diff-stages git-rev-parse git-patch-id git-pack-objects \
4848
git-unpack-objects git-verify-pack git-receive-pack git-send-pack \
@@ -135,7 +135,7 @@ git-rev-list: rev-list.c
135135
git-mktag: mktag.c
136136
git-diff-helper: diff-helper.c
137137
git-tar-tree: tar-tree.c
138-
git-write-blob: write-blob.c
138+
git-hash-object: hash-object.c
139139
git-stripspace: stripspace.c
140140
git-diff-stages: diff-stages.c
141141
git-rev-parse: rev-parse.c

README

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ object. The object is totally independent of it's location in the
102102
directory tree, and renaming a file does not change the object that
103103
file is associated with in any way.
104104

105-
A blob is created with link:git-write-blob.html[git-write-blob] and
106-
it's data can be accessed by link:git-cat-file.html[git-cat-file]
105+
A blob is typically created when link:git-update-cache.html[git-update-cache]
106+
is run, and it's data can be accessed by link:git-cat-file.html[git-cat-file].
107107

108108
Tree Object
109109
~~~~~~~~~~~

cache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extern int remove_cache_entry_at(int pos);
138138
extern int remove_file_from_cache(char *path);
139139
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
140140
extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
141-
extern int index_fd(unsigned char *sha1, int fd, struct stat *st);
141+
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
142142
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
143143

144144
struct cache_file {
@@ -172,6 +172,12 @@ extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
172172
extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
173173
extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
174174
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
175+
extern char *write_sha1_file_prepare(void *buf,
176+
unsigned long len,
177+
const char *type,
178+
unsigned char *sha1,
179+
unsigned char *hdr,
180+
int *hdrlen);
175181

176182
extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
177183

git-cvsimport-script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ while(<CVS>) {
683683
$fn =~ s#^/+##;
684684
my ($tmpname, $size) = $cvs->file($fn,$rev);
685685
print "".($init ? "New" : "Update")." $fn: $size bytes.\n" if $opt_v;
686-
open my $F, '-|', "git-write-blob $tmpname"
686+
open my $F, '-|', "git-hash-object -w $tmpname"
687687
or die "Cannot create object: $!\n";
688688
my $sha = <$F>;
689689
chomp $sha;

hash-object.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* GIT - The information manager from hell
3+
*
4+
* Copyright (C) Linus Torvalds, 2005
5+
* Copyright (C) Junio C Hamano, 2005
6+
*/
7+
#include "cache.h"
8+
9+
static void hash_object(const char *path, const char *type, int write_object)
10+
{
11+
int fd;
12+
struct stat st;
13+
unsigned char sha1[20];
14+
fd = open(path, O_RDONLY);
15+
if (fd < 0 ||
16+
fstat(fd, &st) < 0 ||
17+
index_fd(sha1, fd, &st, write_object, type))
18+
die(write_object
19+
? "Unable to add %s to database"
20+
: "Unable to hash %s", path);
21+
printf("%s\n", sha1_to_hex(sha1));
22+
}
23+
24+
static const char *hash_object_usage =
25+
"git-hash-object [-t <type>] [-w] <file>...";
26+
27+
int main(int argc, char **argv)
28+
{
29+
int i;
30+
const char *type = "blob";
31+
int write_object = 0;
32+
33+
for (i = 1 ; i < argc; i++) {
34+
if (!strcmp(argv[i], "-t")) {
35+
if (argc <= ++i)
36+
die(hash_object_usage);
37+
type = argv[i];
38+
}
39+
else if (!strcmp(argv[i], "-w"))
40+
write_object = 1;
41+
else
42+
hash_object(argv[i], type, write_object);
43+
}
44+
return 0;
45+
}

sha1_file.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,12 +1100,12 @@ void *read_object_with_reference(const unsigned char *sha1,
11001100
}
11011101
}
11021102

1103-
static char *write_sha1_file_prepare(void *buf,
1104-
unsigned long len,
1105-
const char *type,
1106-
unsigned char *sha1,
1107-
unsigned char *hdr,
1108-
int *hdrlen)
1103+
char *write_sha1_file_prepare(void *buf,
1104+
unsigned long len,
1105+
const char *type,
1106+
unsigned char *sha1,
1107+
unsigned char *hdr,
1108+
int *hdrlen)
11091109
{
11101110
SHA_CTX c;
11111111

@@ -1299,11 +1299,13 @@ int has_sha1_file(const unsigned char *sha1)
12991299
return find_pack_entry(sha1, &e);
13001300
}
13011301

1302-
int index_fd(unsigned char *sha1, int fd, struct stat *st)
1302+
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
13031303
{
13041304
unsigned long size = st->st_size;
13051305
void *buf;
13061306
int ret;
1307+
unsigned char hdr[50];
1308+
int hdrlen;
13071309

13081310
buf = "";
13091311
if (size)
@@ -1312,7 +1314,14 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st)
13121314
if ((int)(long)buf == -1)
13131315
return -1;
13141316

1315-
ret = write_sha1_file(buf, size, "blob", sha1);
1317+
if (!type)
1318+
type = "blob";
1319+
if (write_object)
1320+
ret = write_sha1_file(buf, size, type, sha1);
1321+
else {
1322+
write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1323+
ret = 0;
1324+
}
13161325
if (size)
13171326
munmap(buf, size);
13181327
return ret;

update-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static int add_file_to_cache(char *path)
6868
fd = open(path, O_RDONLY);
6969
if (fd < 0)
7070
return -1;
71-
if (index_fd(ce->sha1, fd, &st) < 0)
71+
if (index_fd(ce->sha1, fd, &st, 1, NULL) < 0)
7272
return -1;
7373
break;
7474
case S_IFLNK:

write-blob.c

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)