2 * Copyright (C) 2005 Junio C Hamano
15 static int use_link
= 0;
16 static int use_symlink
= 0;
17 static int use_filecopy
= 1;
21 int fetch(unsigned char *sha1
)
23 static int object_name_start
= -1;
24 static char filename
[PATH_MAX
];
25 char *hex
= sha1_to_hex(sha1
);
26 const char *dest_filename
= sha1_file_name(sha1
);
28 if (object_name_start
< 0) {
29 strcpy(filename
, path
); /* e.g. git.git */
30 strcat(filename
, "/objects/");
31 object_name_start
= strlen(filename
);
33 filename
[object_name_start
+0] = hex
[0];
34 filename
[object_name_start
+1] = hex
[1];
35 filename
[object_name_start
+2] = '/';
36 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
38 if (!link(filename
, dest_filename
)) {
39 pull_say("link %s\n", hex
);
42 /* If we got ENOENT there is no point continuing. */
43 if (errno
== ENOENT
) {
44 fprintf(stderr
, "does not exist %s\n", filename
);
48 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
49 pull_say("symlink %s\n", hex
);
56 ifd
= open(filename
, O_RDONLY
);
57 if (ifd
< 0 || fstat(ifd
, &st
) < 0) {
59 fprintf(stderr
, "cannot open %s\n", filename
);
62 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
64 if (-1 == (int)(long)map
) {
65 fprintf(stderr
, "cannot mmap %s\n", filename
);
68 ofd
= open(dest_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
69 status
= ((ofd
< 0) ||
70 (write(ofd
, map
, st
.st_size
) != st
.st_size
));
71 munmap(map
, st
.st_size
);
74 fprintf(stderr
, "cannot write %s\n", dest_filename
);
76 pull_say("copy %s\n", hex
);
79 fprintf(stderr
, "failed to copy %s with given copy methods.\n", hex
);
83 static const char *local_pull_usage
=
84 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
87 * By default we only use file copy.
88 * If -l is specified, a hard link is attempted.
89 * If -s is specified, then a symlink is attempted.
90 * If -n is _not_ specified, then a regular file-to-file copy is done.
92 int main(int argc
, char **argv
)
97 while (arg
< argc
&& argv
[arg
][0] == '-') {
98 if (argv
[arg
][1] == 't')
100 else if (argv
[arg
][1] == 'c')
102 else if (argv
[arg
][1] == 'a') {
107 else if (argv
[arg
][1] == 'l')
109 else if (argv
[arg
][1] == 's')
111 else if (argv
[arg
][1] == 'n')
113 else if (argv
[arg
][1] == 'v')
116 usage(local_pull_usage
);
120 usage(local_pull_usage
);
121 commit_id
= argv
[arg
];
122 path
= argv
[arg
+ 1];