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