2 * Copyright (C) 2005 Junio C Hamano
8 static int use_link
= 0;
9 static int use_symlink
= 0;
10 static int use_filecopy
= 1;
12 static char *path
; /* "Remote" git repository */
14 int fetch(unsigned char *sha1
)
16 static int object_name_start
= -1;
17 static char filename
[PATH_MAX
];
18 char *hex
= sha1_to_hex(sha1
);
19 const char *dest_filename
= sha1_file_name(sha1
);
21 if (object_name_start
< 0) {
22 strcpy(filename
, path
); /* e.g. git.git */
23 strcat(filename
, "/objects/");
24 object_name_start
= strlen(filename
);
26 filename
[object_name_start
+0] = hex
[0];
27 filename
[object_name_start
+1] = hex
[1];
28 filename
[object_name_start
+2] = '/';
29 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
31 if (!link(filename
, dest_filename
)) {
32 pull_say("link %s\n", hex
);
35 /* If we got ENOENT there is no point continuing. */
36 if (errno
== ENOENT
) {
37 fprintf(stderr
, "does not exist %s\n", filename
);
41 if (use_symlink
&& !symlink(filename
, dest_filename
)) {
42 pull_say("symlink %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) ||
63 (write(ofd
, map
, st
.st_size
) != st
.st_size
));
64 munmap(map
, st
.st_size
);
67 fprintf(stderr
, "cannot write %s\n", dest_filename
);
69 pull_say("copy %s\n", hex
);
72 fprintf(stderr
, "failed to copy %s with given copy methods.\n", hex
);
76 int fetch_ref(char *ref
, unsigned char *sha1
)
78 static int ref_name_start
= -1;
79 static char filename
[PATH_MAX
];
83 if (ref_name_start
< 0) {
84 sprintf(filename
, "%s/refs/", path
);
85 ref_name_start
= strlen(filename
);
87 strcpy(filename
+ ref_name_start
, ref
);
88 ifd
= open(filename
, O_RDONLY
);
91 fprintf(stderr
, "cannot open %s\n", filename
);
94 if (read(ifd
, hex
, 40) != 40 || get_sha1_hex(hex
, sha1
)) {
96 fprintf(stderr
, "cannot read from %s\n", filename
);
100 pull_say("ref %s\n", sha1_to_hex(sha1
));
104 static const char *local_pull_usage
=
105 "git-local-pull [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
108 * By default we only use file copy.
109 * If -l is specified, a hard link is attempted.
110 * If -s is specified, then a symlink is attempted.
111 * If -n is _not_ specified, then a regular file-to-file copy is done.
113 int main(int argc
, char **argv
)
118 while (arg
< argc
&& argv
[arg
][0] == '-') {
119 if (argv
[arg
][1] == 't')
121 else if (argv
[arg
][1] == 'c')
123 else if (argv
[arg
][1] == 'a') {
128 else if (argv
[arg
][1] == 'l')
130 else if (argv
[arg
][1] == 's')
132 else if (argv
[arg
][1] == 'n')
134 else if (argv
[arg
][1] == 'v')
136 else if (argv
[arg
][1] == 'w')
137 write_ref
= argv
[++arg
];
139 usage(local_pull_usage
);
143 usage(local_pull_usage
);
144 commit_id
= argv
[arg
];
145 path
= argv
[arg
+ 1];