teach index_fd to work with pipes
index_fd can now work with file descriptors that are not normal files but any readable file. If the given file descriptor is a regular file then mmap() is used; for other files, strbuf_read is used. The path parameter, which has been used as hint for filters, can be NULL now to indicate that the file should be hashed literally without any filter. The index_pipe function is removed as redundant. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Dmitry Potapov committed
Aug 3, 2008 at 08:39 UTC
43df4f86e035056605ceb757029181d7ddee1e7e
3 files changed
+41
-52
cache.h
-1
@@ -391,7 +391,6 @@ extern int ie_modified(const struct index_state *, struct cache_entry *, struct
391
392
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
393
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
394
-extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
394
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
395
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
396
hash-object.c
+13
-15
@@ -8,15 +8,12 @@
8
#include "blob.h"
9
#include "quote.h"
10
11
-static void hash_object(const char *path, enum object_type type, int write_object)
11
+static void hash_fd(int fd, const char *type, int write_object, const char *path)
12
{
13
- int fd;
13
struct stat st;
14
unsigned char sha1[20];
16
- fd = open(path, O_RDONLY);
17
- if (fd < 0 ||
18
- fstat(fd, &st) < 0 ||
19
- index_fd(sha1, fd, &st, write_object, type, path))
15
+ if (fstat(fd, &st) < 0 ||
16
+ index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
17
die(write_object
18
? "Unable to add %s to database"
19
: "Unable to hash %s", path);
@@ -24,12 +21,13 @@ static void hash_object(const char *path, enum object_type type, int write_objec
21
maybe_flush_or_die(stdout, "hash to stdout");
22
}
23
27
-static void hash_stdin(const char *type, int write_object)
24
+static void hash_object(const char *path, const char *type, int write_object)
25
{
29
- unsigned char sha1[20];
30
- if (index_pipe(sha1, 0, type, write_object))
31
- die("Unable to add stdin to database");
32
- printf("%s\n", sha1_to_hex(sha1));
26
+ int fd;
27
+ fd = open(path, O_RDONLY);
28
+ if (fd < 0)
29
+ die("Cannot open %s", path);
30
+ hash_fd(fd, type, write_object, path);
31
}
32
33
static void hash_stdin_paths(const char *type, int write_objects)
@@ -45,7 +43,7 @@ static void hash_stdin_paths(const char *type, int write_objects)
43
die("line is badly quoted");
44
strbuf_swap(&buf, &nbuf);
45
}
48
- hash_object(buf.buf, type_from_string(type), write_objects);
46
+ hash_object(buf.buf, type, write_objects);
47
}
48
strbuf_release(&buf);
49
strbuf_release(&nbuf);
@@ -116,13 +114,13 @@ int main(int argc, char **argv)
114
}
115
116
if (hashstdin) {
119
- hash_stdin(type, write_object);
117
+ hash_fd(0, type, write_object, NULL);
118
hashstdin = 0;
119
}
120
if (0 <= prefix_length)
121
arg = prefix_filename(prefix, prefix_length,
122
arg);
125
- hash_object(arg, type_from_string(type), write_object);
123
+ hash_object(arg, type, write_object);
124
no_more_flags = 1;
125
}
126
}
@@ -131,6 +129,6 @@ int main(int argc, char **argv)
129
hash_stdin_paths(type, write_object);
130
131
if (hashstdin)
134
- hash_stdin(type, write_object);
132
+ hash_fd(0, type, write_object, NULL);
133
return 0;
134
}
sha1_file.c
+28
-36
@@ -2353,51 +2353,22 @@ int has_sha1_file(const unsigned char *sha1)
2353
return has_loose_object(sha1);
2354
}
2355
2356
-int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
2356
+static int index_mem(unsigned char *sha1, void *buf, size_t size,
2357
+ int write_object, enum object_type type, const char *path)
2358
{
2358
- struct strbuf buf;
2359
- int ret;
2360
-
2361
- strbuf_init(&buf, 0);
2362
- if (strbuf_read(&buf, fd, 4096) < 0) {
2363
- strbuf_release(&buf);
2364
- return -1;
2365
- }
2366
-
2367
- if (!type)
2368
- type = blob_type;
2369
- if (write_object)
2370
- ret = write_sha1_file(buf.buf, buf.len, type, sha1);
2371
- else
2372
- ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
2373
- strbuf_release(&buf);
2374
-
2375
- return ret;
2376
-}
2377
-
2378
-int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2379
- enum object_type type, const char *path)
2380
-{
2381
- size_t size = xsize_t(st->st_size);
2382
- void *buf = NULL;
2359
int ret, re_allocated = 0;
2360
2385
- if (size)
2386
- buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2387
- close(fd);
2388
-
2361
if (!type)
2362
type = OBJ_BLOB;
2363
2364
/*
2365
* Convert blobs to git internal format
2366
*/
2395
- if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
2367
+ if ((type == OBJ_BLOB) && path) {
2368
struct strbuf nbuf;
2369
strbuf_init(&nbuf, 0);
2370
if (convert_to_git(path, buf, size, &nbuf,
2371
write_object ? safe_crlf : 0)) {
2400
- munmap(buf, size);
2372
buf = strbuf_detach(&nbuf, &size);
2373
re_allocated = 1;
2374
}
@@ -2407,12 +2378,33 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2378
ret = write_sha1_file(buf, size, typename(type), sha1);
2379
else
2380
ret = hash_sha1_file(buf, size, typename(type), sha1);
2410
- if (re_allocated) {
2381
+ if (re_allocated)
2382
free(buf);
2412
- return ret;
2413
- }
2414
- if (size)
2383
+ return ret;
2384
+}
2385
+
2386
+int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2387
+ enum object_type type, const char *path)
2388
+{
2389
+ int ret;
2390
+ size_t size = xsize_t(st->st_size);
2391
+
2392
+ if (!S_ISREG(st->st_mode)) {
2393
+ struct strbuf sbuf;
2394
+ strbuf_init(&sbuf, 0);
2395
+ if (strbuf_read(&sbuf, fd, 4096) >= 0)
2396
+ ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object,
2397
+ type, path);
2398
+ else
2399
+ ret = -1;
2400
+ strbuf_release(&sbuf);
2401
+ } else if (size) {
2402
+ void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2403
+ ret = index_mem(sha1, buf, size, write_object, type, path);
2404
munmap(buf, size);
2405
+ } else
2406
+ ret = index_mem(sha1, NULL, size, write_object, type, path);
2407
+ close(fd);
2408
return ret;
2409
}
2410