fetch.con commit [PATCH] Avoid building object ref lists when not needed (8805cca)
   1#include "fetch.h"
   2
   3#include "cache.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "tag.h"
   7#include "blob.h"
   8#include "refs.h"
   9
  10const char *write_ref = NULL;
  11
  12const unsigned char *current_ref = NULL;
  13
  14int get_tree = 0;
  15int get_history = 0;
  16int get_all = 0;
  17int get_verbosely = 0;
  18static unsigned char current_commit_sha1[20];
  19
  20void pull_say(const char *fmt, const char *hex) 
  21{
  22        if (get_verbosely)
  23                fprintf(stderr, fmt, hex);
  24}
  25
  26static void report_missing(const char *what, const unsigned char *missing)
  27{
  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
  36static int make_sure_we_have_it(const char *what, unsigned char *sha1)
  37{
  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        }
  45        return status;
  46}
  47
  48static int process(unsigned char *sha1, const char *type);
  49
  50static int process_tree(struct tree *tree)
  51{
  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) {
  58                if (process(entries->item.any->sha1,
  59                            entries->directory ? tree_type : blob_type))
  60                        return -1;
  61        }
  62        return 0;
  63}
  64
  65#define COMPLETE        1U
  66static struct commit_list *complete = NULL;
  67
  68static int process_commit(struct commit *commit)
  69{
  70        if (parse_commit(commit))
  71                return -1;
  72
  73        while (complete && complete->item->date >= commit->date) {
  74                pop_most_recent_commit(&complete, COMPLETE);
  75        }
  76                
  77
  78        if (commit->object.flags & COMPLETE)
  79                return 0;
  80
  81        memcpy(current_commit_sha1, commit->object.sha1, 20);
  82
  83        if (get_tree) {
  84                if (process(commit->tree->object.sha1, tree_type))
  85                        return -1;
  86                if (!get_all)
  87                        get_tree = 0;
  88        }
  89        if (get_history) {
  90                struct commit_list *parents = commit->parents;
  91                for (; parents; parents = parents->next) {
  92                        if (process(parents->item->object.sha1,
  93                                    commit_type))
  94                                return -1;
  95                }
  96        }
  97        return 0;
  98}
  99
 100static int process_tag(struct tag *tag)
 101{
 102        if (parse_tag(tag))
 103                return -1;
 104        return process(tag->tagged->sha1, NULL);
 105}
 106
 107static struct object_list *process_queue = NULL;
 108static struct object_list **process_queue_end = &process_queue;
 109
 110static int process_object(struct object *obj)
 111{
 112        if (obj->type == commit_type) {
 113                if (process_commit((struct commit *)obj))
 114                        return -1;
 115                return 0;
 116        }
 117        if (obj->type == tree_type) {
 118                if (process_tree((struct tree *)obj))
 119                        return -1;
 120                return 0;
 121        }
 122        if (obj->type == blob_type) {
 123                return 0;
 124        }
 125        if (obj->type == tag_type) {
 126                if (process_tag((struct tag *)obj))
 127                        return -1;
 128                return 0;
 129        }
 130        return error("Unable to determine requirements "
 131                     "of type %s for %s",
 132                     obj->type, sha1_to_hex(obj->sha1));
 133}
 134
 135static int process(unsigned char *sha1, const char *type)
 136{
 137        struct object *obj = lookup_object_type(sha1, type);
 138
 139        if (has_sha1_file(sha1)) {
 140                parse_object(sha1);
 141                /* We already have it, so we should scan it now. */
 142                return process_object(obj);
 143        }
 144        if (object_list_contains(process_queue, obj))
 145                return 0;
 146        object_list_insert(obj, process_queue_end);
 147        process_queue_end = &(*process_queue_end)->next;
 148
 149        //fprintf(stderr, "prefetch %s\n", sha1_to_hex(sha1));
 150        prefetch(sha1);
 151                
 152        return 0;
 153}
 154
 155static int loop(void)
 156{
 157        while (process_queue) {
 158                struct object *obj = process_queue->item;
 159                /*
 160                fprintf(stderr, "%d objects to pull\n", 
 161                        object_list_length(process_queue));
 162                */
 163                process_queue = process_queue->next;
 164                if (!process_queue)
 165                        process_queue_end = &process_queue;
 166
 167                //fprintf(stderr, "fetch %s\n", sha1_to_hex(obj->sha1));
 168                
 169                if (make_sure_we_have_it(obj->type ? obj->type : "object", 
 170                                         obj->sha1))
 171                        return -1;
 172                if (!obj->type)
 173                        parse_object(obj->sha1);
 174                if (process_object(obj))
 175                        return -1;
 176        }
 177        return 0;
 178}
 179
 180static int interpret_target(char *target, unsigned char *sha1)
 181{
 182        if (!get_sha1_hex(target, sha1))
 183                return 0;
 184        if (!check_ref_format(target)) {
 185                if (!fetch_ref(target, sha1)) {
 186                        return 0;
 187                }
 188        }
 189        return -1;
 190}
 191
 192static int mark_complete(const char *path, const unsigned char *sha1)
 193{
 194        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
 195        if (commit) {
 196                commit->object.flags |= COMPLETE;
 197                insert_by_date(commit, &complete);
 198        }
 199        return 0;
 200}
 201
 202int pull(char *target)
 203{
 204        unsigned char sha1[20];
 205        int fd = -1;
 206
 207        save_commit_buffer = 0;
 208        if (write_ref && current_ref) {
 209                fd = lock_ref_sha1(write_ref, current_ref);
 210                if (fd < 0)
 211                        return -1;
 212        }
 213
 214        for_each_ref(mark_complete);
 215
 216        if (interpret_target(target, sha1))
 217                return error("Could not interpret %s as something to pull",
 218                             target);
 219        if (process(sha1, NULL))
 220                return -1;
 221        if (loop())
 222                return -1;
 223        
 224        if (write_ref) {
 225                if (current_ref) {
 226                        write_ref_sha1(write_ref, fd, sha1);
 227                } else {
 228                        write_ref_sha1_unlocked(write_ref, sha1);
 229                }
 230        }
 231        return 0;
 232}