walker.con commit rev-list --children (72276a3)
   1#include "cache.h"
   2#include "walker.h"
   3#include "commit.h"
   4#include "tree.h"
   5#include "tree-walk.h"
   6#include "tag.h"
   7#include "blob.h"
   8#include "refs.h"
   9
  10static unsigned char current_commit_sha1[20];
  11
  12void walker_say(struct walker *walker, const char *fmt, const char *hex)
  13{
  14        if (walker->get_verbosely)
  15                fprintf(stderr, fmt, hex);
  16}
  17
  18static void report_missing(const struct object *obj)
  19{
  20        char missing_hex[41];
  21        strcpy(missing_hex, sha1_to_hex(obj->sha1));;
  22        fprintf(stderr, "Cannot obtain needed %s %s\n",
  23                obj->type ? typename(obj->type): "object", missing_hex);
  24        if (!is_null_sha1(current_commit_sha1))
  25                fprintf(stderr, "while processing commit %s.\n",
  26                        sha1_to_hex(current_commit_sha1));
  27}
  28
  29static int process(struct walker *walker, struct object *obj);
  30
  31static int process_tree(struct walker *walker, struct tree *tree)
  32{
  33        struct tree_desc desc;
  34        struct name_entry entry;
  35
  36        if (parse_tree(tree))
  37                return -1;
  38
  39        init_tree_desc(&desc, tree->buffer, tree->size);
  40        while (tree_entry(&desc, &entry)) {
  41                struct object *obj = NULL;
  42
  43                /* submodule commits are not stored in the superproject */
  44                if (S_ISGITLINK(entry.mode))
  45                        continue;
  46                if (S_ISDIR(entry.mode)) {
  47                        struct tree *tree = lookup_tree(entry.sha1);
  48                        if (tree)
  49                                obj = &tree->object;
  50                }
  51                else {
  52                        struct blob *blob = lookup_blob(entry.sha1);
  53                        if (blob)
  54                                obj = &blob->object;
  55                }
  56                if (!obj || process(walker, obj))
  57                        return -1;
  58        }
  59        free(tree->buffer);
  60        tree->buffer = NULL;
  61        tree->size = 0;
  62        return 0;
  63}
  64
  65#define COMPLETE        (1U << 0)
  66#define SEEN            (1U << 1)
  67#define TO_SCAN         (1U << 2)
  68
  69static struct commit_list *complete = NULL;
  70
  71static int process_commit(struct walker *walker, struct commit *commit)
  72{
  73        if (parse_commit(commit))
  74                return -1;
  75
  76        while (complete && complete->item->date >= commit->date) {
  77                pop_most_recent_commit(&complete, COMPLETE);
  78        }
  79
  80        if (commit->object.flags & COMPLETE)
  81                return 0;
  82
  83        hashcpy(current_commit_sha1, commit->object.sha1);
  84
  85        walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
  86
  87        if (walker->get_tree) {
  88                if (process(walker, &commit->tree->object))
  89                        return -1;
  90                if (!walker->get_all)
  91                        walker->get_tree = 0;
  92        }
  93        if (walker->get_history) {
  94                struct commit_list *parents = commit->parents;
  95                for (; parents; parents = parents->next) {
  96                        if (process(walker, &parents->item->object))
  97                                return -1;
  98                }
  99        }
 100        return 0;
 101}
 102
 103static int process_tag(struct walker *walker, struct tag *tag)
 104{
 105        if (parse_tag(tag))
 106                return -1;
 107        return process(walker, tag->tagged);
 108}
 109
 110static struct object_list *process_queue = NULL;
 111static struct object_list **process_queue_end = &process_queue;
 112
 113static int process_object(struct walker *walker, struct object *obj)
 114{
 115        if (obj->type == OBJ_COMMIT) {
 116                if (process_commit(walker, (struct commit *)obj))
 117                        return -1;
 118                return 0;
 119        }
 120        if (obj->type == OBJ_TREE) {
 121                if (process_tree(walker, (struct tree *)obj))
 122                        return -1;
 123                return 0;
 124        }
 125        if (obj->type == OBJ_BLOB) {
 126                return 0;
 127        }
 128        if (obj->type == OBJ_TAG) {
 129                if (process_tag(walker, (struct tag *)obj))
 130                        return -1;
 131                return 0;
 132        }
 133        return error("Unable to determine requirements "
 134                     "of type %s for %s",
 135                     typename(obj->type), sha1_to_hex(obj->sha1));
 136}
 137
 138static int process(struct walker *walker, struct object *obj)
 139{
 140        if (obj->flags & SEEN)
 141                return 0;
 142        obj->flags |= SEEN;
 143
 144        if (has_sha1_file(obj->sha1)) {
 145                /* We already have it, so we should scan it now. */
 146                obj->flags |= TO_SCAN;
 147        }
 148        else {
 149                if (obj->flags & COMPLETE)
 150                        return 0;
 151                walker->prefetch(walker, obj->sha1);
 152        }
 153
 154        object_list_insert(obj, process_queue_end);
 155        process_queue_end = &(*process_queue_end)->next;
 156        return 0;
 157}
 158
 159static int loop(struct walker *walker)
 160{
 161        struct object_list *elem;
 162
 163        while (process_queue) {
 164                struct object *obj = process_queue->item;
 165                elem = process_queue;
 166                process_queue = elem->next;
 167                free(elem);
 168                if (!process_queue)
 169                        process_queue_end = &process_queue;
 170
 171                /* If we are not scanning this object, we placed it in
 172                 * the queue because we needed to fetch it first.
 173                 */
 174                if (! (obj->flags & TO_SCAN)) {
 175                        if (walker->fetch(walker, obj->sha1)) {
 176                                report_missing(obj);
 177                                return -1;
 178                        }
 179                }
 180                if (!obj->type)
 181                        parse_object(obj->sha1);
 182                if (process_object(walker, obj))
 183                        return -1;
 184        }
 185        return 0;
 186}
 187
 188static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
 189{
 190        if (!get_sha1_hex(target, sha1))
 191                return 0;
 192        if (!check_ref_format(target)) {
 193                if (!walker->fetch_ref(walker, target, sha1)) {
 194                        return 0;
 195                }
 196        }
 197        return -1;
 198}
 199
 200static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 201{
 202        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
 203        if (commit) {
 204                commit->object.flags |= COMPLETE;
 205                insert_by_date(commit, &complete);
 206        }
 207        return 0;
 208}
 209
 210int walker_targets_stdin(char ***target, const char ***write_ref)
 211{
 212        int targets = 0, targets_alloc = 0;
 213        struct strbuf buf;
 214        *target = NULL; *write_ref = NULL;
 215        strbuf_init(&buf, 0);
 216        while (1) {
 217                char *rf_one = NULL;
 218                char *tg_one;
 219
 220                if (strbuf_getline(&buf, stdin, '\n') == EOF)
 221                        break;
 222                tg_one = buf.buf;
 223                rf_one = strchr(tg_one, '\t');
 224                if (rf_one)
 225                        *rf_one++ = 0;
 226
 227                if (targets >= targets_alloc) {
 228                        targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
 229                        *target = xrealloc(*target, targets_alloc * sizeof(**target));
 230                        *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
 231                }
 232                (*target)[targets] = xstrdup(tg_one);
 233                (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
 234                targets++;
 235        }
 236        strbuf_release(&buf);
 237        return targets;
 238}
 239
 240void walker_targets_free(int targets, char **target, const char **write_ref)
 241{
 242        while (targets--) {
 243                free(target[targets]);
 244                if (write_ref && write_ref[targets])
 245                        free((char *) write_ref[targets]);
 246        }
 247}
 248
 249int walker_fetch(struct walker *walker, int targets, char **target,
 250                 const char **write_ref, const char *write_ref_log_details)
 251{
 252        struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
 253        unsigned char *sha1 = xmalloc(targets * 20);
 254        char *msg;
 255        int ret;
 256        int i;
 257
 258        save_commit_buffer = 0;
 259
 260        for (i = 0; i < targets; i++) {
 261                if (!write_ref || !write_ref[i])
 262                        continue;
 263
 264                lock[i] = lock_ref_sha1(write_ref[i], NULL);
 265                if (!lock[i]) {
 266                        error("Can't lock ref %s", write_ref[i]);
 267                        goto unlock_and_fail;
 268                }
 269        }
 270
 271        if (!walker->get_recover)
 272                for_each_ref(mark_complete, NULL);
 273
 274        for (i = 0; i < targets; i++) {
 275                if (interpret_target(walker, target[i], &sha1[20 * i])) {
 276                        error("Could not interpret response from server '%s' as something to pull", target[i]);
 277                        goto unlock_and_fail;
 278                }
 279                if (process(walker, lookup_unknown_object(&sha1[20 * i])))
 280                        goto unlock_and_fail;
 281        }
 282
 283        if (loop(walker))
 284                goto unlock_and_fail;
 285
 286        if (write_ref_log_details) {
 287                msg = xmalloc(strlen(write_ref_log_details) + 12);
 288                sprintf(msg, "fetch from %s", write_ref_log_details);
 289        } else {
 290                msg = NULL;
 291        }
 292        for (i = 0; i < targets; i++) {
 293                if (!write_ref || !write_ref[i])
 294                        continue;
 295                ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
 296                lock[i] = NULL;
 297                if (ret)
 298                        goto unlock_and_fail;
 299        }
 300        free(msg);
 301
 302        return 0;
 303
 304unlock_and_fail:
 305        for (i = 0; i < targets; i++)
 306                if (lock[i])
 307                        unlock_ref(lock[i]);
 308
 309        return -1;
 310}
 311
 312void walker_free(struct walker *walker)
 313{
 314        walker->cleanup(walker);
 315        free(walker);
 316}