walker.con commit Merge branch 'jc/shortlog-ref-exclude' into maint (8675779)
   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(tree);
  60        return 0;
  61}
  62
  63/* Remember to update object flag allocation in object.h */
  64#define COMPLETE        (1U << 0)
  65#define SEEN            (1U << 1)
  66#define TO_SCAN         (1U << 2)
  67
  68static struct commit_list *complete = NULL;
  69
  70static int process_commit(struct walker *walker, struct commit *commit)
  71{
  72        if (parse_commit(commit))
  73                return -1;
  74
  75        while (complete && complete->item->date >= commit->date) {
  76                pop_most_recent_commit(&complete, COMPLETE);
  77        }
  78
  79        if (commit->object.flags & COMPLETE)
  80                return 0;
  81
  82        hashcpy(current_commit_sha1, commit->object.sha1);
  83
  84        walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
  85
  86        if (walker->get_tree) {
  87                if (process(walker, &commit->tree->object))
  88                        return -1;
  89                if (!walker->get_all)
  90                        walker->get_tree = 0;
  91        }
  92        if (walker->get_history) {
  93                struct commit_list *parents = commit->parents;
  94                for (; 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                     typename(obj->type), sha1_to_hex(obj->sha1));
 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_sha1_file(obj->sha1)) {
 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->sha1);
 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->sha1)) {
 175                                report_missing(obj);
 176                                return -1;
 177                        }
 178                }
 179                if (!obj->type)
 180                        parse_object(obj->sha1);
 181                if (process_object(walker, obj))
 182                        return -1;
 183        }
 184        return 0;
 185}
 186
 187static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
 188{
 189        if (!get_sha1_hex(target, sha1))
 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                        hashcpy(sha1, ref->old_sha1);
 195                        free(ref);
 196                        return 0;
 197                }
 198                free(ref);
 199        }
 200        return -1;
 201}
 202
 203static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 204{
 205        struct commit *commit = lookup_commit_reference_gently(sha1, 1);
 206        if (commit) {
 207                commit->object.flags |= COMPLETE;
 208                commit_list_insert_by_date(commit, &complete);
 209        }
 210        return 0;
 211}
 212
 213int walker_targets_stdin(char ***target, const char ***write_ref)
 214{
 215        int targets = 0, targets_alloc = 0;
 216        struct strbuf buf = STRBUF_INIT;
 217        *target = NULL; *write_ref = NULL;
 218        while (1) {
 219                char *rf_one = NULL;
 220                char *tg_one;
 221
 222                if (strbuf_getline(&buf, stdin, '\n') == EOF)
 223                        break;
 224                tg_one = buf.buf;
 225                rf_one = strchr(tg_one, '\t');
 226                if (rf_one)
 227                        *rf_one++ = 0;
 228
 229                if (targets >= targets_alloc) {
 230                        targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
 231                        *target = xrealloc(*target, targets_alloc * sizeof(**target));
 232                        *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
 233                }
 234                (*target)[targets] = xstrdup(tg_one);
 235                (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
 236                targets++;
 237        }
 238        strbuf_release(&buf);
 239        return targets;
 240}
 241
 242void walker_targets_free(int targets, char **target, const char **write_ref)
 243{
 244        while (targets--) {
 245                free(target[targets]);
 246                if (write_ref)
 247                        free((char *) write_ref[targets]);
 248        }
 249}
 250
 251int walker_fetch(struct walker *walker, int targets, char **target,
 252                 const char **write_ref, const char *write_ref_log_details)
 253{
 254        struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
 255        unsigned char *sha1 = xmalloc(targets * 20);
 256        char *msg;
 257        int ret;
 258        int i;
 259
 260        save_commit_buffer = 0;
 261
 262        for (i = 0; i < targets; i++) {
 263                if (!write_ref || !write_ref[i])
 264                        continue;
 265
 266                lock[i] = lock_ref_sha1(write_ref[i], NULL);
 267                if (!lock[i]) {
 268                        error("Can't lock ref %s", write_ref[i]);
 269                        goto unlock_and_fail;
 270                }
 271        }
 272
 273        if (!walker->get_recover)
 274                for_each_ref(mark_complete, NULL);
 275
 276        for (i = 0; i < targets; i++) {
 277                if (interpret_target(walker, target[i], &sha1[20 * i])) {
 278                        error("Could not interpret response from server '%s' as something to pull", target[i]);
 279                        goto unlock_and_fail;
 280                }
 281                if (process(walker, lookup_unknown_object(&sha1[20 * i])))
 282                        goto unlock_and_fail;
 283        }
 284
 285        if (loop(walker))
 286                goto unlock_and_fail;
 287
 288        if (write_ref_log_details) {
 289                msg = xmalloc(strlen(write_ref_log_details) + 12);
 290                sprintf(msg, "fetch from %s", write_ref_log_details);
 291        } else {
 292                msg = NULL;
 293        }
 294        for (i = 0; i < targets; i++) {
 295                if (!write_ref || !write_ref[i])
 296                        continue;
 297                ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
 298                lock[i] = NULL;
 299                if (ret)
 300                        goto unlock_and_fail;
 301        }
 302        free(msg);
 303
 304        return 0;
 305
 306unlock_and_fail:
 307        for (i = 0; i < targets; i++)
 308                if (lock[i])
 309                        unlock_ref(lock[i]);
 310
 311        return -1;
 312}
 313
 314void walker_free(struct walker *walker)
 315{
 316        walker->cleanup(walker);
 317        free(walker);
 318}