2 * Copyright (C) 2005 Junio C Hamano
14 static int use_link
= 0;
15 static int use_symlink
= 0;
16 static int use_filecopy
= 1;
17 static int verbose
= 0;
21 static void say(const char *fmt
, const char *hex
) {
23 fprintf(stderr
, fmt
, hex
);
26 int fetch(unsigned char *sha1
)
28 static int object_name_start
= -1;
29 static char filename
[PATH_MAX
];
30 char *hex
= sha1_to_hex(sha1
);
31 const char *dest_filename
= sha1_file_name(sha1
);
33 if (object_name_start
< 0) {
34 strcpy(filename
, path
); /* e.g. git.git */
35 strcat(filename
, "/objects/");
36 object_name_start
= strlen(filename
);
38 filename
[object_name_start
+0] = hex
[0];
39 filename
[object_name_start
+1] = hex
[1];
40 filename
[object_name_start
+2] = '/';
41 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
42 if (use_link
&& !link(filename
, dest_filename
)) {
43 say("Hardlinked %s.\n", hex
);
46 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
47 say("Symlinked %s.\n", hex
);
54 ifd
= open(filename
, O_RDONLY
);
55 if (ifd
< 0 || fstat(ifd
, &st
) < 0) {
57 fprintf(stderr
, "Cannot open %s\n", filename
);
60 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
62 if (-1 == (int)(long)map
) {
63 fprintf(stderr
, "Cannot mmap %s\n", filename
);
66 ofd
= open(dest_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
67 status
= ((ofd
< 0) ||
68 (write(ofd
, map
, st
.st_size
) != st
.st_size
));
69 munmap(map
, st
.st_size
);
72 fprintf(stderr
, "Cannot write %s (%ld bytes)\n",
73 dest_filename
, st
.st_size
);
75 say("Copied %s.\n", hex
);
78 fprintf(stderr
, "No copy method was provided to copy %s.\n", hex
);
82 static const char *local_pull_usage
=
83 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
86 * By default we only use file copy.
87 * If -l is specified, a hard link is attempted.
88 * If -s is specified, then a symlink is attempted.
89 * If -n is _not_ specified, then a regular file-to-file copy is done.
91 int main(int argc
, char **argv
)
96 while (arg
< argc
&& argv
[arg
][0] == '-') {
97 if (argv
[arg
][1] == 't')
99 else if (argv
[arg
][1] == 'c')
101 else if (argv
[arg
][1] == 'a') {
106 else if (argv
[arg
][1] == 'l')
108 else if (argv
[arg
][1] == 's')
110 else if (argv
[arg
][1] == 'n')
112 else if (argv
[arg
][1] == 'v')
115 usage(local_pull_usage
);
119 usage(local_pull_usage
);
120 commit_id
= argv
[arg
];
121 path
= argv
[arg
+ 1];