Skip to content

Commit 3407bb4

Browse files
author
Linus Torvalds
committed
Add "unpack-file" helper that unpacks a sha1 blob into a tmpfile.
1 parent e590d69 commit 3407bb4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ AR=ar
1515

1616
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
1717
cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
18-
check-files ls-tree merge-base merge-cache
18+
check-files ls-tree merge-base merge-cache unpack-file
1919

2020
all: $(PROG)
2121

@@ -78,6 +78,9 @@ merge-base: merge-base.o $(LIB_FILE) object.o commit.o tree.o blob.o
7878
merge-cache: merge-cache.o $(LIB_FILE)
7979
$(CC) $(CFLAGS) -o merge-cache merge-cache.o $(LIBS)
8080

81+
unpack-file: unpack-file.o $(LIB_FILE)
82+
$(CC) $(CFLAGS) -o unpack-file unpack-file.o $(LIBS)
83+
8184
blob.o: $(LIB_H)
8285
cat-file.o: $(LIB_H)
8386
check-files.o: $(LIB_H)
@@ -100,6 +103,7 @@ show-files.o: $(LIB_H)
100103
tree.o: $(LIB_H)
101104
update-cache.o: $(LIB_H)
102105
usage.o: $(LIB_H)
106+
unpack-file.o: $(LIB_H)
103107
write-tree.o: $(LIB_H)
104108

105109
clean:

unpack-file.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "cache.h"
2+
3+
static char *create_temp_file(unsigned char *sha1)
4+
{
5+
static char path[50];
6+
void *buf;
7+
char type[100];
8+
unsigned long size;
9+
int fd;
10+
11+
buf = read_sha1_file(sha1, type, &size);
12+
if (!buf || strcmp(type, "blob"))
13+
die("unable to read blob object %s", sha1_to_hex(sha1));
14+
15+
strcpy(path, ".merge_file_XXXXXX");
16+
fd = mkstemp(path);
17+
if (fd < 0)
18+
die("unable to create temp-file");
19+
if (write(fd, buf, size) != size)
20+
die("unable to write temp-file");
21+
close(fd);
22+
return path;
23+
}
24+
25+
int main(int argc, char **argv)
26+
{
27+
unsigned char sha1[20];
28+
29+
if (argc != 2 || get_sha1_hex(argv[1], sha1))
30+
usage("unpack-file.c <sha1>");
31+
32+
puts(create_temp_file(sha1));
33+
return 0;
34+
}

0 commit comments

Comments
 (0)