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