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