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 void prefetch(unsigned char *sha1
)
18 static struct packed_git
*packs
= NULL
;
20 static void setup_index(unsigned char *sha1
)
22 struct packed_git
*new_pack
;
23 char filename
[PATH_MAX
];
24 strcpy(filename
, path
);
25 strcat(filename
, "/objects/pack/pack-");
26 strcat(filename
, sha1_to_hex(sha1
));
27 strcat(filename
, ".idx");
28 new_pack
= parse_pack_index_file(sha1
, filename
);
29 new_pack
->next
= packs
;
33 static int setup_indices(void)
37 char filename
[PATH_MAX
];
38 unsigned char sha1
[20];
39 sprintf(filename
, "%s/objects/pack/", path
);
40 dir
= opendir(filename
);
43 while ((de
= readdir(dir
)) != NULL
) {
44 int namelen
= strlen(de
->d_name
);
46 strcmp(de
->d_name
+ namelen
- 5, ".pack"))
48 get_sha1_hex(de
->d_name
+ 5, sha1
);
55 static int copy_file(const char *source
, char *dest
, const char *hex
,
56 int warn_if_not_exists
)
58 safe_create_leading_directories(dest
);
60 if (!link(source
, dest
)) {
61 pull_say("link %s\n", hex
);
64 /* If we got ENOENT there is no point continuing. */
65 if (errno
== ENOENT
) {
66 if (warn_if_not_exists
)
67 fprintf(stderr
, "does not exist %s\n", source
);
73 if (stat(source
, &st
)) {
74 if (!warn_if_not_exists
&& errno
== ENOENT
)
76 fprintf(stderr
, "cannot stat %s: %s\n", source
,
80 if (!symlink(source
, dest
)) {
81 pull_say("symlink %s\n", hex
);
86 int ifd
, ofd
, status
= 0;
88 ifd
= open(source
, O_RDONLY
);
90 if (!warn_if_not_exists
&& errno
== ENOENT
)
92 fprintf(stderr
, "cannot open %s\n", source
);
95 ofd
= open(dest
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
97 fprintf(stderr
, "cannot open %s\n", dest
);
101 status
= copy_fd(ifd
, ofd
);
104 fprintf(stderr
, "cannot write %s\n", dest
);
106 pull_say("copy %s\n", hex
);
109 fprintf(stderr
, "failed to copy %s with given copy methods.\n", hex
);
113 static int fetch_pack(const unsigned char *sha1
)
115 struct packed_git
*target
;
116 char filename
[PATH_MAX
];
119 target
= find_sha1_pack(sha1
, packs
);
121 return error("Couldn't find %s: not separate or in any pack",
124 fprintf(stderr
, "Getting pack %s\n",
125 sha1_to_hex(target
->sha1
));
126 fprintf(stderr
, " which contains %s\n",
129 sprintf(filename
, "%s/objects/pack/pack-%s.pack",
130 path
, sha1_to_hex(target
->sha1
));
131 copy_file(filename
, sha1_pack_name(target
->sha1
),
132 sha1_to_hex(target
->sha1
), 1);
133 sprintf(filename
, "%s/objects/pack/pack-%s.idx",
134 path
, sha1_to_hex(target
->sha1
));
135 copy_file(filename
, sha1_pack_index_name(target
->sha1
),
136 sha1_to_hex(target
->sha1
), 1);
137 install_packed_git(target
);
141 static int fetch_file(const unsigned char *sha1
)
143 static int object_name_start
= -1;
144 static char filename
[PATH_MAX
];
145 char *hex
= sha1_to_hex(sha1
);
146 char *dest_filename
= sha1_file_name(sha1
);
148 if (object_name_start
< 0) {
149 strcpy(filename
, path
); /* e.g. git.git */
150 strcat(filename
, "/objects/");
151 object_name_start
= strlen(filename
);
153 filename
[object_name_start
+0] = hex
[0];
154 filename
[object_name_start
+1] = hex
[1];
155 filename
[object_name_start
+2] = '/';
156 strcpy(filename
+ object_name_start
+ 3, hex
+ 2);
157 return copy_file(filename
, dest_filename
, hex
, 0);
160 int fetch(unsigned char *sha1
)
162 if (has_sha1_file(sha1
))
165 return fetch_file(sha1
) && fetch_pack(sha1
);
168 int fetch_ref(char *ref
, unsigned char *sha1
)
170 static int ref_name_start
= -1;
171 static char filename
[PATH_MAX
];
175 if (ref_name_start
< 0) {
176 sprintf(filename
, "%s/refs/", path
);
177 ref_name_start
= strlen(filename
);
179 strcpy(filename
+ ref_name_start
, ref
);
180 ifd
= open(filename
, O_RDONLY
);
183 fprintf(stderr
, "cannot open %s\n", filename
);
186 if (read(ifd
, hex
, 40) != 40 || get_sha1_hex(hex
, sha1
)) {
188 fprintf(stderr
, "cannot read from %s\n", filename
);
192 pull_say("ref %s\n", sha1_to_hex(sha1
));
196 static const char local_pull_usage
[] =
197 "git-local-fetch [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
200 * By default we only use file copy.
201 * If -l is specified, a hard link is attempted.
202 * If -s is specified, then a symlink is attempted.
203 * If -n is _not_ specified, then a regular file-to-file copy is done.
205 int main(int argc
, char **argv
)
210 setup_git_directory();
212 while (arg
< argc
&& argv
[arg
][0] == '-') {
213 if (argv
[arg
][1] == 't')
215 else if (argv
[arg
][1] == 'c')
217 else if (argv
[arg
][1] == 'a') {
222 else if (argv
[arg
][1] == 'l')
224 else if (argv
[arg
][1] == 's')
226 else if (argv
[arg
][1] == 'n')
228 else if (argv
[arg
][1] == 'v')
230 else if (argv
[arg
][1] == 'w')
231 write_ref
= argv
[++arg
];
232 else if (!strcmp(argv
[arg
], "--recover"))
235 usage(local_pull_usage
);
239 usage(local_pull_usage
);
240 commit_id
= argv
[arg
];
241 path
= argv
[arg
+ 1];