]> git.ipfire.org Git - thirdparty/git.git/blame - local-fetch.c
GIT-VERSION-FILE: check ./version first.
[thirdparty/git.git] / local-fetch.c
CommitLineData
b02a26cb
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
dfcb4057
JH
4#include "cache.h"
5#include "commit.h"
215a7ad1 6#include "fetch.h"
dfcb4057 7
96f1e58f
DR
8static int use_link;
9static int use_symlink;
b02a26cb 10static int use_filecopy = 1;
96f1e58f 11static int commits_on_stdin;
dfcb4057 12
8e87ca66 13static const char *path; /* "Remote" git repository */
dfcb4057 14
1e8be59d
DB
15void prefetch(unsigned char *sha1)
16{
17}
18
96f1e58f 19static struct packed_git *packs;
08b11616 20
2ab141a2 21static void setup_index(unsigned char *sha1)
dfcb4057 22{
08b11616
DB
23 struct packed_git *new_pack;
24 char filename[PATH_MAX];
25 strcpy(filename, path);
26 strcat(filename, "/objects/pack/pack-");
27 strcat(filename, sha1_to_hex(sha1));
28 strcat(filename, ".idx");
29 new_pack = parse_pack_index_file(sha1, filename);
30 new_pack->next = packs;
31 packs = new_pack;
32}
dfcb4057 33
2ab141a2 34static int setup_indices(void)
08b11616
DB
35{
36 DIR *dir;
37 struct dirent *de;
38 char filename[PATH_MAX];
39 unsigned char sha1[20];
40 sprintf(filename, "%s/objects/pack/", path);
41 dir = opendir(filename);
8be707de
SV
42 if (!dir)
43 return -1;
08b11616
DB
44 while ((de = readdir(dir)) != NULL) {
45 int namelen = strlen(de->d_name);
83a2b841 46 if (namelen != 50 ||
5bb1cda5 47 !has_extension(de->d_name, ".pack"))
08b11616 48 continue;
d9200320 49 get_sha1_hex(de->d_name + 5, sha1);
08b11616 50 setup_index(sha1);
dfcb4057 51 }
8be707de 52 closedir(dir);
08b11616
DB
53 return 0;
54}
55
0ffdbbfe 56static int copy_file(const char *source, char *dest, const char *hex,
628cd543 57 int warn_if_not_exists)
08b11616 58{
0ffdbbfe 59 safe_create_leading_directories(dest);
fd0ffd3a 60 if (use_link) {
08b11616 61 if (!link(source, dest)) {
e78d9772 62 pull_say("link %s\n", hex);
fd0ffd3a
JH
63 return 0;
64 }
65 /* If we got ENOENT there is no point continuing. */
66 if (errno == ENOENT) {
628cd543
SV
67 if (warn_if_not_exists)
68 fprintf(stderr, "does not exist %s\n", source);
fd0ffd3a
JH
69 return -1;
70 }
dfcb4057 71 }
e2b77f02
SV
72 if (use_symlink) {
73 struct stat st;
74 if (stat(source, &st)) {
628cd543
SV
75 if (!warn_if_not_exists && errno == ENOENT)
76 return -1;
e2b77f02
SV
77 fprintf(stderr, "cannot stat %s: %s\n", source,
78 strerror(errno));
79 return -1;
80 }
81 if (!symlink(source, dest)) {
82 pull_say("symlink %s\n", hex);
83 return 0;
84 }
dfcb4057 85 }
b02a26cb 86 if (use_filecopy) {
52e4478d
JH
87 int ifd, ofd, status = 0;
88
08b11616 89 ifd = open(source, O_RDONLY);
52e4478d
JH
90 if (ifd < 0) {
91 if (!warn_if_not_exists && errno == ENOENT)
628cd543 92 return -1;
08b11616 93 fprintf(stderr, "cannot open %s\n", source);
b02a26cb
JH
94 return -1;
95 }
52e4478d
JH
96 ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666);
97 if (ofd < 0) {
98 fprintf(stderr, "cannot open %s\n", dest);
99 close(ifd);
b02a26cb
JH
100 return -1;
101 }
52e4478d
JH
102 status = copy_fd(ifd, ofd);
103 close(ofd);
b02a26cb 104 if (status)
08b11616 105 fprintf(stderr, "cannot write %s\n", dest);
b02a26cb 106 else
e78d9772 107 pull_say("copy %s\n", hex);
b02a26cb 108 return status;
dfcb4057 109 }
fd0ffd3a 110 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
b02a26cb 111 return -1;
dfcb4057
JH
112}
113
2ab141a2 114static int fetch_pack(const unsigned char *sha1)
08b11616
DB
115{
116 struct packed_git *target;
117 char filename[PATH_MAX];
118 if (setup_indices())
119 return -1;
120 target = find_sha1_pack(sha1, packs);
121 if (!target)
122 return error("Couldn't find %s: not separate or in any pack",
123 sha1_to_hex(sha1));
124 if (get_verbosely) {
125 fprintf(stderr, "Getting pack %s\n",
126 sha1_to_hex(target->sha1));
127 fprintf(stderr, " which contains %s\n",
128 sha1_to_hex(sha1));
129 }
130 sprintf(filename, "%s/objects/pack/pack-%s.pack",
d9200320
JH
131 path, sha1_to_hex(target->sha1));
132 copy_file(filename, sha1_pack_name(target->sha1),
628cd543 133 sha1_to_hex(target->sha1), 1);
08b11616 134 sprintf(filename, "%s/objects/pack/pack-%s.idx",
d9200320
JH
135 path, sha1_to_hex(target->sha1));
136 copy_file(filename, sha1_pack_index_name(target->sha1),
628cd543 137 sha1_to_hex(target->sha1), 1);
08b11616
DB
138 install_packed_git(target);
139 return 0;
140}
141
2ab141a2 142static int fetch_file(const unsigned char *sha1)
08b11616
DB
143{
144 static int object_name_start = -1;
145 static char filename[PATH_MAX];
146 char *hex = sha1_to_hex(sha1);
0ffdbbfe 147 char *dest_filename = sha1_file_name(sha1);
08b11616
DB
148
149 if (object_name_start < 0) {
150 strcpy(filename, path); /* e.g. git.git */
151 strcat(filename, "/objects/");
152 object_name_start = strlen(filename);
153 }
154 filename[object_name_start+0] = hex[0];
155 filename[object_name_start+1] = hex[1];
156 filename[object_name_start+2] = '/';
157 strcpy(filename + object_name_start + 3, hex + 2);
628cd543 158 return copy_file(filename, dest_filename, hex, 0);
08b11616
DB
159}
160
161int fetch(unsigned char *sha1)
162{
11f0dafe
NH
163 if (has_sha1_file(sha1))
164 return 0;
165 else
166 return fetch_file(sha1) && fetch_pack(sha1);
08b11616
DB
167}
168
cd541a68
DB
169int fetch_ref(char *ref, unsigned char *sha1)
170{
f5ab6cca
JH
171 static int ref_name_start = -1;
172 static char filename[PATH_MAX];
173 static char hex[41];
174 int ifd;
175
176 if (ref_name_start < 0) {
177 sprintf(filename, "%s/refs/", path);
178 ref_name_start = strlen(filename);
179 }
180 strcpy(filename + ref_name_start, ref);
181 ifd = open(filename, O_RDONLY);
182 if (ifd < 0) {
183 close(ifd);
184 fprintf(stderr, "cannot open %s\n", filename);
185 return -1;
186 }
93d26e4c 187 if (read_in_full(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
f5ab6cca
JH
188 close(ifd);
189 fprintf(stderr, "cannot read from %s\n", filename);
190 return -1;
191 }
192 close(ifd);
193 pull_say("ref %s\n", sha1_to_hex(sha1));
194 return 0;
cd541a68
DB
195}
196
4d1f1190 197static const char local_pull_usage[] =
8e87ca66 198"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] [--stdin] commit-id path";
dfcb4057 199
cc41cd2e 200/*
b02a26cb
JH
201 * By default we only use file copy.
202 * If -l is specified, a hard link is attempted.
203 * If -s is specified, then a symlink is attempted.
204 * If -n is _not_ specified, then a regular file-to-file copy is done.
205 */
8e87ca66 206int main(int argc, const char **argv)
dfcb4057 207{
8e87ca66
PB
208 int commits;
209 const char **write_ref = NULL;
210 char **commit_id;
dfcb4057
JH
211 int arg = 1;
212
5a327713 213 setup_git_directory();
d0740d92 214 git_config(git_default_config);
5a327713 215
dfcb4057
JH
216 while (arg < argc && argv[arg][0] == '-') {
217 if (argv[arg][1] == 't')
218 get_tree = 1;
219 else if (argv[arg][1] == 'c')
220 get_history = 1;
221 else if (argv[arg][1] == 'a') {
222 get_all = 1;
223 get_tree = 1;
224 get_history = 1;
225 }
226 else if (argv[arg][1] == 'l')
227 use_link = 1;
228 else if (argv[arg][1] == 's')
229 use_symlink = 1;
b02a26cb
JH
230 else if (argv[arg][1] == 'n')
231 use_filecopy = 0;
dfcb4057 232 else if (argv[arg][1] == 'v')
e78d9772 233 get_verbosely = 1;
f5ab6cca 234 else if (argv[arg][1] == 'w')
8e87ca66 235 write_ref = &argv[++arg];
820eca68
DB
236 else if (!strcmp(argv[arg], "--recover"))
237 get_recover = 1;
8e87ca66
PB
238 else if (!strcmp(argv[arg], "--stdin"))
239 commits_on_stdin = 1;
dfcb4057
JH
240 else
241 usage(local_pull_usage);
242 arg++;
243 }
8e87ca66 244 if (argc < arg + 2 - commits_on_stdin)
dfcb4057 245 usage(local_pull_usage);
8e87ca66
PB
246 if (commits_on_stdin) {
247 commits = pull_targets_stdin(&commit_id, &write_ref);
248 } else {
249 commit_id = (char **) &argv[arg++];
250 commits = 1;
251 }
252 path = argv[arg];
dfcb4057 253
8e87ca66 254 if (pull(commits, commit_id, write_ref, path))
dfcb4057
JH
255 return 1;
256
8e87ca66
PB
257 if (commits_on_stdin)
258 pull_targets_free(commits, commit_id, write_ref);
259
dfcb4057
JH
260 return 0;
261}