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;
20 int fetch(unsigned char *sha1
)
22 static int object_name_start
= -1;
23 static char filename
[PATH_MAX
];
24 char *hex
= sha1_to_hex(sha1
);
25 const char *dest_filename
= sha1_file_name(sha1
);
27 if (object_name_start
< 0) {
28 strcpy(filename
, path
); /* e.g. git.git */
29 strcat(filename
, "/objects/");
30 object_name_start
= strlen(filename
);
32 filename
[object_name_start
+0] = hex
[0];
33 filename
[object_name_start
+1] = hex
[1];
34 filename
[object_name_start
+2] = '/';
35 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
37 if (!link(filename
, dest_filename
)) {
38 pull_say("link %s\n", hex
);
41 /* If we got ENOENT there is no point continuing. */
42 if (errno
== ENOENT
) {
43 fprintf(stderr
, "does not exist %s\n", filename
);
47 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
48 pull_say("symlink %s\n", hex
);
55 ifd
= open(filename
, O_RDONLY
);
56 if (ifd
< 0 || fstat(ifd
, &st
) < 0) {
58 fprintf(stderr
, "cannot open %s\n", filename
);
61 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, ifd
, 0);
63 if (-1 == (int)(long)map
) {
64 fprintf(stderr
, "cannot mmap %s\n", filename
);
67 ofd
= open(dest_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
68 status
= ((ofd
< 0) ||
69 (write(ofd
, map
, st
.st_size
) != st
.st_size
));
70 munmap(map
, st
.st_size
);
73 fprintf(stderr
, "cannot write %s (%ld bytes)\n",
74 dest_filename
, st
.st_size
);
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];