11 static int use_link
= 0;
12 static int use_symlink
= 0;
13 static int verbose
= 0;
17 static void say(const char *fmt
, const char *hex
) {
19 fprintf(stderr
, fmt
, hex
);
22 int fetch(unsigned char *sha1
)
24 static int object_name_start
= -1;
25 static char filename
[PATH_MAX
];
26 char *hex
= sha1_to_hex(sha1
);
27 const char *dest_filename
= sha1_file_name(sha1
);
32 if (object_name_start
< 0) {
33 strcpy(filename
, path
); /* e.g. git.git */
34 strcat(filename
, "/objects/");
35 object_name_start
= strlen(filename
);
37 filename
[object_name_start
+0] = hex
[0];
38 filename
[object_name_start
+1] = hex
[1];
39 filename
[object_name_start
+2] = '/';
40 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
41 if (use_link
&& !link(filename
, dest_filename
)) {
42 say("Hardlinked %s.\n", hex
);
45 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
46 say("Symlinked %s.\n", hex
);
49 ifd
= open(filename
, O_RDONLY
);
50 if (ifd
< 0 || fstat(ifd
, &st
) < 0) {
52 fprintf(stderr
, "Cannot open %s\n", filename
);
55 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
57 if (-1 == (int)(long)map
) {
58 fprintf(stderr
, "Cannot mmap %s\n", filename
);
61 ofd
= open(dest_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
62 status
= ((ofd
< 0) || (write(ofd
, map
, st
.st_size
) != st
.st_size
));
63 munmap(map
, st
.st_size
);
66 fprintf(stderr
, "Cannot write %s (%ld bytes)\n",
67 dest_filename
, st
.st_size
);
69 say("Copied %s.\n", hex
);
73 static const char *local_pull_usage
=
74 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-v] commit-id path";
76 int main(int argc
, char **argv
)
81 while (arg
< argc
&& argv
[arg
][0] == '-') {
82 if (argv
[arg
][1] == 't')
84 else if (argv
[arg
][1] == 'c')
86 else if (argv
[arg
][1] == 'a') {
91 else if (argv
[arg
][1] == 'l')
93 else if (argv
[arg
][1] == 's')
95 else if (argv
[arg
][1] == 'v')
98 usage(local_pull_usage
);
102 usage(local_pull_usage
);
103 commit_id
= argv
[arg
];
104 path
= argv
[arg
+ 1];