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