| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 1 | #include "pull.h" |
| 2 | |
| 3 | #include "cache.h" |
| 4 | #include "commit.h" |
| 5 | #include "tree.h" |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 6 | #include "tag.h" |
| 7 | #include "blob.h" |
| Daniel Barkalow | cd541a6 | 2005-06-06 20:38:26 | [diff] [blame] | 8 | #include "refs.h" |
| 9 | |
| 10 | const char *write_ref = NULL; |
| 11 | |
| 12 | const unsigned char *current_ref = NULL; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 13 | |
| 14 | int get_tree = 0; |
| 15 | int get_history = 0; |
| 16 | int get_all = 0; |
| Junio C Hamano | e78d977 | 2005-05-06 08:37:21 | [diff] [blame] | 17 | int get_verbosely = 0; |
| Junio C Hamano | b2d62f1 | 2005-05-04 08:26:24 | [diff] [blame] | 18 | static unsigned char current_commit_sha1[20]; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 19 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 20 | void pull_say(const char *fmt, const char *hex) |
| 21 | { |
| Junio C Hamano | e78d977 | 2005-05-06 08:37:21 | [diff] [blame] | 22 | if (get_verbosely) |
| 23 | fprintf(stderr, fmt, hex); |
| 24 | } |
| 25 | |
| Junio C Hamano | b2d62f1 | 2005-05-04 08:26:24 | [diff] [blame] | 26 | static void report_missing(const char *what, const unsigned char *missing) |
| Junio C Hamano | ee4f439 | 2005-05-02 04:07:40 | [diff] [blame] | 27 | { |
| Junio C Hamano | b2d62f1 | 2005-05-04 08:26:24 | [diff] [blame] | 28 | char missing_hex[41]; |
| 29 | |
| 30 | strcpy(missing_hex, sha1_to_hex(missing));; |
| 31 | fprintf(stderr, |
| 32 | "Cannot obtain needed %s %s\nwhile processing commit %s.\n", |
| 33 | what, missing_hex, sha1_to_hex(current_commit_sha1)); |
| 34 | } |
| 35 | |
| 36 | static int make_sure_we_have_it(const char *what, unsigned char *sha1) |
| 37 | { |
| Junio C Hamano | a48e1d6 | 2005-06-05 06:11:38 | [diff] [blame] | 38 | int status = 0; |
| 39 | |
| 40 | if (!has_sha1_file(sha1)) { |
| 41 | status = fetch(sha1); |
| 42 | if (status && what) |
| 43 | report_missing(what, sha1); |
| 44 | } |
| Junio C Hamano | b2d62f1 | 2005-05-04 08:26:24 | [diff] [blame] | 45 | return status; |
| Junio C Hamano | ee4f439 | 2005-05-02 04:07:40 | [diff] [blame] | 46 | } |
| 47 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 48 | static int process(unsigned char *sha1, const char *type); |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 49 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 50 | static int process_tree(struct tree *tree) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 51 | { |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 52 | struct tree_entry_list *entries; |
| 53 | |
| 54 | if (parse_tree(tree)) |
| 55 | return -1; |
| 56 | |
| 57 | for (entries = tree->entries; entries; entries = entries->next) { |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 58 | if (process(entries->item.any->sha1, |
| 59 | entries->directory ? tree_type : blob_type)) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 60 | return -1; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 65 | static int process_commit(struct commit *commit) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 66 | { |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 67 | if (parse_commit(commit)) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 68 | return -1; |
| 69 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 70 | memcpy(current_commit_sha1, commit->object.sha1, 20); |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 71 | |
| 72 | if (get_tree) { |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 73 | if (process(commit->tree->object.sha1, tree_type)) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 74 | return -1; |
| 75 | if (!get_all) |
| 76 | get_tree = 0; |
| 77 | } |
| 78 | if (get_history) { |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 79 | struct commit_list *parents = commit->parents; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 80 | for (; parents; parents = parents->next) { |
| 81 | if (has_sha1_file(parents->item->object.sha1)) |
| 82 | continue; |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 83 | if (process(parents->item->object.sha1, |
| 84 | commit_type)) |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 85 | return -1; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 91 | static int process_tag(struct tag *tag) |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 92 | { |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 93 | if (parse_tag(tag)) |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 94 | return -1; |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 95 | return process(tag->tagged->sha1, NULL); |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 96 | } |
| 97 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 98 | static struct object_list *process_queue = NULL; |
| 99 | static struct object_list **process_queue_end = &process_queue; |
| 100 | |
| Daniel Barkalow | f88fcf8 | 2005-08-11 23:38:09 | [diff] [blame] | 101 | static int process_object(struct object *obj) |
| 102 | { |
| 103 | if (obj->type == commit_type) { |
| 104 | if (process_commit((struct commit *)obj)) |
| 105 | return -1; |
| 106 | return 0; |
| 107 | } |
| 108 | if (obj->type == tree_type) { |
| 109 | if (process_tree((struct tree *)obj)) |
| 110 | return -1; |
| 111 | return 0; |
| 112 | } |
| 113 | if (obj->type == blob_type) { |
| 114 | return 0; |
| 115 | } |
| 116 | if (obj->type == tag_type) { |
| 117 | if (process_tag((struct tag *)obj)) |
| 118 | return -1; |
| 119 | return 0; |
| 120 | } |
| 121 | return error("Unable to determine requirements " |
| 122 | "of type %s for %s", |
| 123 | obj->type, sha1_to_hex(obj->sha1)); |
| 124 | } |
| 125 | |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 126 | static int process(unsigned char *sha1, const char *type) |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 127 | { |
| Daniel Barkalow | f88fcf8 | 2005-08-11 23:38:09 | [diff] [blame] | 128 | struct object *obj = lookup_object_type(sha1, type); |
| 129 | if (has_sha1_file(sha1)) { |
| Daniel Barkalow | 0d62fb5 | 2005-08-12 03:17:55 | [diff] [blame] | 130 | parse_object(sha1); |
| Daniel Barkalow | f88fcf8 | 2005-08-11 23:38:09 | [diff] [blame] | 131 | /* We already have it, so we should scan it now. */ |
| 132 | return process_object(obj); |
| 133 | } |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 134 | if (object_list_contains(process_queue, obj)) |
| 135 | return 0; |
| 136 | object_list_insert(obj, process_queue_end); |
| 137 | process_queue_end = &(*process_queue_end)->next; |
| 138 | |
| 139 | //fprintf(stderr, "prefetch %s\n", sha1_to_hex(sha1)); |
| 140 | prefetch(sha1); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int loop(void) |
| 146 | { |
| 147 | while (process_queue) { |
| 148 | struct object *obj = process_queue->item; |
| 149 | /* |
| 150 | fprintf(stderr, "%d objects to pull\n", |
| 151 | object_list_length(process_queue)); |
| 152 | */ |
| 153 | process_queue = process_queue->next; |
| 154 | if (!process_queue) |
| 155 | process_queue_end = &process_queue; |
| 156 | |
| 157 | //fprintf(stderr, "fetch %s\n", sha1_to_hex(obj->sha1)); |
| 158 | |
| Jason Riedy | c7c81b3 | 2005-08-23 20:34:07 | [diff] [blame] | 159 | if (make_sure_we_have_it(obj->type ? obj->type : "object", |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 160 | obj->sha1)) |
| 161 | return -1; |
| 162 | if (!obj->type) |
| 163 | parse_object(obj->sha1); |
| Daniel Barkalow | f88fcf8 | 2005-08-11 23:38:09 | [diff] [blame] | 164 | if (process_object(obj)) |
| 165 | return -1; |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 166 | } |
| 167 | return 0; |
| Daniel Barkalow | 3173bd4 | 2005-06-22 00:35:53 | [diff] [blame] | 168 | } |
| 169 | |
| Daniel Barkalow | cd541a6 | 2005-06-06 20:38:26 | [diff] [blame] | 170 | static int interpret_target(char *target, unsigned char *sha1) |
| 171 | { |
| 172 | if (!get_sha1_hex(target, sha1)) |
| 173 | return 0; |
| 174 | if (!check_ref_format(target)) { |
| 175 | if (!fetch_ref(target, sha1)) { |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 183 | int pull(char *target) |
| 184 | { |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 185 | unsigned char sha1[20]; |
| Daniel Barkalow | cd541a6 | 2005-06-06 20:38:26 | [diff] [blame] | 186 | int fd = -1; |
| 187 | |
| 188 | if (write_ref && current_ref) { |
| 189 | fd = lock_ref_sha1(write_ref, current_ref); |
| 190 | if (fd < 0) |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | if (interpret_target(target, sha1)) |
| 195 | return error("Could not interpret %s as something to pull", |
| 196 | target); |
| barkalow@iabervon.org | 1e8be59 | 2005-08-02 23:46:10 | [diff] [blame] | 197 | if (process(sha1, NULL)) |
| 198 | return -1; |
| 199 | if (loop()) |
| Daniel Barkalow | cd541a6 | 2005-06-06 20:38:26 | [diff] [blame] | 200 | return -1; |
| 201 | |
| 202 | if (write_ref) { |
| 203 | if (current_ref) { |
| 204 | write_ref_sha1(write_ref, fd, sha1); |
| 205 | } else { |
| 206 | write_ref_sha1_unlocked(write_ref, sha1); |
| 207 | } |
| 208 | } |
| 209 | return 0; |
| Daniel Barkalow | 4250a5e | 2005-04-30 23:53:56 | [diff] [blame] | 210 | } |