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