]>
Commit | Line | Data |
---|---|---|
b02a26cb JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
dfcb4057 JH |
4 | #include "cache.h" |
5 | #include "commit.h" | |
dfcb4057 JH |
6 | #include "pull.h" |
7 | ||
8 | static int use_link = 0; | |
9 | static int use_symlink = 0; | |
b02a26cb | 10 | static int use_filecopy = 1; |
dfcb4057 | 11 | |
f5ab6cca | 12 | static char *path; /* "Remote" git repository */ |
dfcb4057 | 13 | |
1e8be59d DB |
14 | void prefetch(unsigned char *sha1) |
15 | { | |
16 | } | |
17 | ||
08b11616 DB |
18 | static struct packed_git *packs = NULL; |
19 | ||
20 | void setup_index(unsigned char *sha1) | |
dfcb4057 | 21 | { |
08b11616 DB |
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; | |
30 | packs = new_pack; | |
31 | } | |
dfcb4057 | 32 | |
08b11616 DB |
33 | int setup_indices() |
34 | { | |
35 | DIR *dir; | |
36 | struct dirent *de; | |
37 | char filename[PATH_MAX]; | |
38 | unsigned char sha1[20]; | |
39 | sprintf(filename, "%s/objects/pack/", path); | |
40 | dir = opendir(filename); | |
41 | while ((de = readdir(dir)) != NULL) { | |
42 | int namelen = strlen(de->d_name); | |
43 | if (namelen != 50 || | |
44 | strcmp(de->d_name + namelen - 5, ".pack")) | |
45 | continue; | |
46 | get_sha1_hex(sha1, de->d_name + 5); | |
47 | setup_index(sha1); | |
dfcb4057 | 48 | } |
08b11616 DB |
49 | return 0; |
50 | } | |
51 | ||
52 | int copy_file(const char *source, const char *dest, const char *hex) | |
53 | { | |
fd0ffd3a | 54 | if (use_link) { |
08b11616 | 55 | if (!link(source, dest)) { |
e78d9772 | 56 | pull_say("link %s\n", hex); |
fd0ffd3a JH |
57 | return 0; |
58 | } | |
59 | /* If we got ENOENT there is no point continuing. */ | |
60 | if (errno == ENOENT) { | |
08b11616 | 61 | fprintf(stderr, "does not exist %s\n", source); |
fd0ffd3a JH |
62 | return -1; |
63 | } | |
dfcb4057 | 64 | } |
08b11616 | 65 | if (use_symlink && !symlink(source, dest)) { |
e78d9772 | 66 | pull_say("symlink %s\n", hex); |
dfcb4057 JH |
67 | return 0; |
68 | } | |
b02a26cb JH |
69 | if (use_filecopy) { |
70 | int ifd, ofd, status; | |
71 | struct stat st; | |
72 | void *map; | |
08b11616 | 73 | ifd = open(source, O_RDONLY); |
b02a26cb JH |
74 | if (ifd < 0 || fstat(ifd, &st) < 0) { |
75 | close(ifd); | |
08b11616 | 76 | fprintf(stderr, "cannot open %s\n", source); |
b02a26cb JH |
77 | return -1; |
78 | } | |
79 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0); | |
dfcb4057 | 80 | close(ifd); |
e35f9824 | 81 | if (map == MAP_FAILED) { |
08b11616 | 82 | fprintf(stderr, "cannot mmap %s\n", source); |
b02a26cb JH |
83 | return -1; |
84 | } | |
08b11616 | 85 | ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666); |
b02a26cb JH |
86 | status = ((ofd < 0) || |
87 | (write(ofd, map, st.st_size) != st.st_size)); | |
88 | munmap(map, st.st_size); | |
89 | close(ofd); | |
90 | if (status) | |
08b11616 | 91 | fprintf(stderr, "cannot write %s\n", dest); |
b02a26cb | 92 | else |
e78d9772 | 93 | pull_say("copy %s\n", hex); |
b02a26cb | 94 | return status; |
dfcb4057 | 95 | } |
fd0ffd3a | 96 | fprintf(stderr, "failed to copy %s with given copy methods.\n", hex); |
b02a26cb | 97 | return -1; |
dfcb4057 JH |
98 | } |
99 | ||
08b11616 DB |
100 | int fetch_pack(unsigned char *sha1) |
101 | { | |
102 | struct packed_git *target; | |
103 | char filename[PATH_MAX]; | |
104 | if (setup_indices()) | |
105 | return -1; | |
106 | target = find_sha1_pack(sha1, packs); | |
107 | if (!target) | |
108 | return error("Couldn't find %s: not separate or in any pack", | |
109 | sha1_to_hex(sha1)); | |
110 | if (get_verbosely) { | |
111 | fprintf(stderr, "Getting pack %s\n", | |
112 | sha1_to_hex(target->sha1)); | |
113 | fprintf(stderr, " which contains %s\n", | |
114 | sha1_to_hex(sha1)); | |
115 | } | |
116 | sprintf(filename, "%s/objects/pack/pack-%s.pack", | |
117 | path, sha1_to_hex(sha1)); | |
118 | copy_file(filename, sha1_pack_name(sha1), sha1_to_hex(sha1)); | |
119 | sprintf(filename, "%s/objects/pack/pack-%s.idx", | |
120 | path, sha1_to_hex(sha1)); | |
121 | copy_file(filename, sha1_pack_index_name(sha1), sha1_to_hex(sha1)); | |
122 | install_packed_git(target); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | int fetch_file(unsigned char *sha1) | |
127 | { | |
128 | static int object_name_start = -1; | |
129 | static char filename[PATH_MAX]; | |
130 | char *hex = sha1_to_hex(sha1); | |
131 | const char *dest_filename = sha1_file_name(sha1); | |
132 | ||
133 | if (object_name_start < 0) { | |
134 | strcpy(filename, path); /* e.g. git.git */ | |
135 | strcat(filename, "/objects/"); | |
136 | object_name_start = strlen(filename); | |
137 | } | |
138 | filename[object_name_start+0] = hex[0]; | |
139 | filename[object_name_start+1] = hex[1]; | |
140 | filename[object_name_start+2] = '/'; | |
141 | strcpy(filename + object_name_start + 3, hex + 2); | |
142 | return copy_file(filename, dest_filename, hex); | |
143 | } | |
144 | ||
145 | int fetch(unsigned char *sha1) | |
146 | { | |
147 | return fetch_file(sha1) && fetch_pack(sha1); | |
148 | } | |
149 | ||
cd541a68 DB |
150 | int fetch_ref(char *ref, unsigned char *sha1) |
151 | { | |
f5ab6cca JH |
152 | static int ref_name_start = -1; |
153 | static char filename[PATH_MAX]; | |
154 | static char hex[41]; | |
155 | int ifd; | |
156 | ||
157 | if (ref_name_start < 0) { | |
158 | sprintf(filename, "%s/refs/", path); | |
159 | ref_name_start = strlen(filename); | |
160 | } | |
161 | strcpy(filename + ref_name_start, ref); | |
162 | ifd = open(filename, O_RDONLY); | |
163 | if (ifd < 0) { | |
164 | close(ifd); | |
165 | fprintf(stderr, "cannot open %s\n", filename); | |
166 | return -1; | |
167 | } | |
168 | if (read(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) { | |
169 | close(ifd); | |
170 | fprintf(stderr, "cannot read from %s\n", filename); | |
171 | return -1; | |
172 | } | |
173 | close(ifd); | |
174 | pull_say("ref %s\n", sha1_to_hex(sha1)); | |
175 | return 0; | |
cd541a68 DB |
176 | } |
177 | ||
4d1f1190 | 178 | static const char local_pull_usage[] = |
f5ab6cca | 179 | "git-local-pull [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path"; |
dfcb4057 | 180 | |
b02a26cb JH |
181 | /* |
182 | * By default we only use file copy. | |
183 | * If -l is specified, a hard link is attempted. | |
184 | * If -s is specified, then a symlink is attempted. | |
185 | * If -n is _not_ specified, then a regular file-to-file copy is done. | |
186 | */ | |
dfcb4057 JH |
187 | int main(int argc, char **argv) |
188 | { | |
189 | char *commit_id; | |
190 | int arg = 1; | |
191 | ||
192 | while (arg < argc && argv[arg][0] == '-') { | |
193 | if (argv[arg][1] == 't') | |
194 | get_tree = 1; | |
195 | else if (argv[arg][1] == 'c') | |
196 | get_history = 1; | |
197 | else if (argv[arg][1] == 'a') { | |
198 | get_all = 1; | |
199 | get_tree = 1; | |
200 | get_history = 1; | |
201 | } | |
202 | else if (argv[arg][1] == 'l') | |
203 | use_link = 1; | |
204 | else if (argv[arg][1] == 's') | |
205 | use_symlink = 1; | |
b02a26cb JH |
206 | else if (argv[arg][1] == 'n') |
207 | use_filecopy = 0; | |
dfcb4057 | 208 | else if (argv[arg][1] == 'v') |
e78d9772 | 209 | get_verbosely = 1; |
f5ab6cca JH |
210 | else if (argv[arg][1] == 'w') |
211 | write_ref = argv[++arg]; | |
dfcb4057 JH |
212 | else |
213 | usage(local_pull_usage); | |
214 | arg++; | |
215 | } | |
216 | if (argc < arg + 2) | |
217 | usage(local_pull_usage); | |
218 | commit_id = argv[arg]; | |
219 | path = argv[arg + 1]; | |
220 | ||
221 | if (pull(commit_id)) | |
222 | return 1; | |
223 | ||
224 | return 0; | |
225 | } |