list-objects.con commit Allow checkout -B <current-branch> to update the current branch (39bd6f7)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "tree.h"
   5#include "blob.h"
   6#include "diff.h"
   7#include "tree-walk.h"
   8#include "revision.h"
   9#include "list-objects.h"
  10
  11static void process_blob(struct rev_info *revs,
  12                         struct blob *blob,
  13                         show_object_fn show,
  14                         struct name_path *path,
  15                         const char *name,
  16                         void *cb_data)
  17{
  18        struct object *obj = &blob->object;
  19
  20        if (!revs->blob_objects)
  21                return;
  22        if (!obj)
  23                die("bad blob object");
  24        if (obj->flags & (UNINTERESTING | SEEN))
  25                return;
  26        obj->flags |= SEEN;
  27        show(obj, path, name, cb_data);
  28}
  29
  30/*
  31 * Processing a gitlink entry currently does nothing, since
  32 * we do not recurse into the subproject.
  33 *
  34 * We *could* eventually add a flag that actually does that,
  35 * which would involve:
  36 *  - is the subproject actually checked out?
  37 *  - if so, see if the subproject has already been added
  38 *    to the alternates list, and add it if not.
  39 *  - process the commit (or tag) the gitlink points to
  40 *    recursively.
  41 *
  42 * However, it's unclear whether there is really ever any
  43 * reason to see superprojects and subprojects as such a
  44 * "unified" object pool (potentially resulting in a totally
  45 * humongous pack - avoiding which was the whole point of
  46 * having gitlinks in the first place!).
  47 *
  48 * So for now, there is just a note that we *could* follow
  49 * the link, and how to do it. Whether it necessarily makes
  50 * any sense what-so-ever to ever do that is another issue.
  51 */
  52static void process_gitlink(struct rev_info *revs,
  53                            const unsigned char *sha1,
  54                            show_object_fn show,
  55                            struct name_path *path,
  56                            const char *name,
  57                            void *cb_data)
  58{
  59        /* Nothing to do */
  60}
  61
  62static void process_tree(struct rev_info *revs,
  63                         struct tree *tree,
  64                         show_object_fn show,
  65                         struct name_path *path,
  66                         struct strbuf *base,
  67                         const char *name,
  68                         void *cb_data)
  69{
  70        struct object *obj = &tree->object;
  71        struct tree_desc desc;
  72        struct name_entry entry;
  73        struct name_path me;
  74        int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
  75        int baselen = base->len;
  76
  77        if (!revs->tree_objects)
  78                return;
  79        if (!obj)
  80                die("bad tree object");
  81        if (obj->flags & (UNINTERESTING | SEEN))
  82                return;
  83        if (parse_tree(tree) < 0)
  84                die("bad tree object %s", sha1_to_hex(obj->sha1));
  85        obj->flags |= SEEN;
  86        show(obj, path, name, cb_data);
  87        me.up = path;
  88        me.elem = name;
  89        me.elem_len = strlen(name);
  90
  91        if (!match) {
  92                strbuf_addstr(base, name);
  93                if (base->len)
  94                        strbuf_addch(base, '/');
  95        }
  96
  97        init_tree_desc(&desc, tree->buffer, tree->size);
  98
  99        while (tree_entry(&desc, &entry)) {
 100                if (match != 2) {
 101                        match = tree_entry_interesting(&entry, base, 0,
 102                                                       &revs->diffopt.pathspec);
 103                        if (match < 0)
 104                                break;
 105                        if (match == 0)
 106                                continue;
 107                }
 108
 109                if (S_ISDIR(entry.mode))
 110                        process_tree(revs,
 111                                     lookup_tree(entry.sha1),
 112                                     show, &me, base, entry.path,
 113                                     cb_data);
 114                else if (S_ISGITLINK(entry.mode))
 115                        process_gitlink(revs, entry.sha1,
 116                                        show, &me, entry.path,
 117                                        cb_data);
 118                else
 119                        process_blob(revs,
 120                                     lookup_blob(entry.sha1),
 121                                     show, &me, entry.path,
 122                                     cb_data);
 123        }
 124        strbuf_setlen(base, baselen);
 125        free(tree->buffer);
 126        tree->buffer = NULL;
 127}
 128
 129static void mark_edge_parents_uninteresting(struct commit *commit,
 130                                            struct rev_info *revs,
 131                                            show_edge_fn show_edge)
 132{
 133        struct commit_list *parents;
 134
 135        for (parents = commit->parents; parents; parents = parents->next) {
 136                struct commit *parent = parents->item;
 137                if (!(parent->object.flags & UNINTERESTING))
 138                        continue;
 139                mark_tree_uninteresting(parent->tree);
 140                if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
 141                        parent->object.flags |= SHOWN;
 142                        show_edge(parent);
 143                }
 144        }
 145}
 146
 147void mark_edges_uninteresting(struct commit_list *list,
 148                              struct rev_info *revs,
 149                              show_edge_fn show_edge)
 150{
 151        for ( ; list; list = list->next) {
 152                struct commit *commit = list->item;
 153
 154                if (commit->object.flags & UNINTERESTING) {
 155                        mark_tree_uninteresting(commit->tree);
 156                        continue;
 157                }
 158                mark_edge_parents_uninteresting(commit, revs, show_edge);
 159        }
 160}
 161
 162static void add_pending_tree(struct rev_info *revs, struct tree *tree)
 163{
 164        add_pending_object(revs, &tree->object, "");
 165}
 166
 167void traverse_commit_list(struct rev_info *revs,
 168                          show_commit_fn show_commit,
 169                          show_object_fn show_object,
 170                          void *data)
 171{
 172        int i;
 173        struct commit *commit;
 174        struct strbuf base;
 175
 176        strbuf_init(&base, PATH_MAX);
 177        while ((commit = get_revision(revs)) != NULL) {
 178                /*
 179                 * an uninteresting boundary commit may not have its tree
 180                 * parsed yet, but we are not going to show them anyway
 181                 */
 182                if (commit->tree)
 183                        add_pending_tree(revs, commit->tree);
 184                show_commit(commit, data);
 185        }
 186        for (i = 0; i < revs->pending.nr; i++) {
 187                struct object_array_entry *pending = revs->pending.objects + i;
 188                struct object *obj = pending->item;
 189                const char *name = pending->name;
 190                if (obj->flags & (UNINTERESTING | SEEN))
 191                        continue;
 192                if (obj->type == OBJ_TAG) {
 193                        obj->flags |= SEEN;
 194                        show_object(obj, NULL, name, data);
 195                        continue;
 196                }
 197                if (obj->type == OBJ_TREE) {
 198                        process_tree(revs, (struct tree *)obj, show_object,
 199                                     NULL, &base, name, data);
 200                        continue;
 201                }
 202                if (obj->type == OBJ_BLOB) {
 203                        process_blob(revs, (struct blob *)obj, show_object,
 204                                     NULL, name, data);
 205                        continue;
 206                }
 207                die("unknown pending object %s (%s)",
 208                    sha1_to_hex(obj->sha1), name);
 209        }
 210        if (revs->pending.nr) {
 211                free(revs->pending.objects);
 212                revs->pending.nr = 0;
 213                revs->pending.alloc = 0;
 214                revs->pending.objects = NULL;
 215        }
 216        strbuf_release(&base);
 217}