0d56b50a15888e3854843103b4e2d9b84d8b87b9
   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 strbuf *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 strbuf *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 strbuf *base,
  66                         const char *name,
  67                         void *cb_data)
  68{
  69        struct object *obj = &tree->object;
  70        struct tree_desc desc;
  71        struct name_entry entry;
  72        enum interesting match = revs->diffopt.pathspec.nr == 0 ?
  73                all_entries_interesting: entry_not_interesting;
  74        int baselen = base->len;
  75
  76        if (!revs->tree_objects)
  77                return;
  78        if (!obj)
  79                die("bad tree object");
  80        if (obj->flags & (UNINTERESTING | SEEN))
  81                return;
  82        if (parse_tree_gently(tree, revs->ignore_missing_links) < 0) {
  83                if (revs->ignore_missing_links)
  84                        return;
  85                die("bad tree object %s", sha1_to_hex(obj->sha1));
  86        }
  87
  88        obj->flags |= SEEN;
  89        show(obj, base, name, cb_data);
  90
  91        strbuf_addstr(base, name);
  92        if (base->len)
  93                strbuf_addch(base, '/');
  94
  95        init_tree_desc(&desc, tree->buffer, tree->size);
  96
  97        while (tree_entry(&desc, &entry)) {
  98                if (match != all_entries_interesting) {
  99                        match = tree_entry_interesting(&entry, base, 0,
 100                                                       &revs->diffopt.pathspec);
 101                        if (match == all_entries_not_interesting)
 102                                break;
 103                        if (match == entry_not_interesting)
 104                                continue;
 105                }
 106
 107                if (S_ISDIR(entry.mode))
 108                        process_tree(revs,
 109                                     lookup_tree(entry.sha1),
 110                                     show, base, entry.path,
 111                                     cb_data);
 112                else if (S_ISGITLINK(entry.mode))
 113                        process_gitlink(revs, entry.sha1,
 114                                        show, base, entry.path,
 115                                        cb_data);
 116                else
 117                        process_blob(revs,
 118                                     lookup_blob(entry.sha1),
 119                                     show, base, entry.path,
 120                                     cb_data);
 121        }
 122        strbuf_setlen(base, baselen);
 123        free_tree_buffer(tree);
 124}
 125
 126static void mark_edge_parents_uninteresting(struct commit *commit,
 127                                            struct rev_info *revs,
 128                                            show_edge_fn show_edge)
 129{
 130        struct commit_list *parents;
 131
 132        for (parents = commit->parents; parents; parents = parents->next) {
 133                struct commit *parent = parents->item;
 134                if (!(parent->object.flags & UNINTERESTING))
 135                        continue;
 136                mark_tree_uninteresting(parent->tree);
 137                if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
 138                        parent->object.flags |= SHOWN;
 139                        show_edge(parent);
 140                }
 141        }
 142}
 143
 144void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
 145{
 146        struct commit_list *list;
 147        int i;
 148
 149        for (list = revs->commits; list; list = list->next) {
 150                struct commit *commit = list->item;
 151
 152                if (commit->object.flags & UNINTERESTING) {
 153                        mark_tree_uninteresting(commit->tree);
 154                        if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
 155                                commit->object.flags |= SHOWN;
 156                                show_edge(commit);
 157                        }
 158                        continue;
 159                }
 160                mark_edge_parents_uninteresting(commit, revs, show_edge);
 161        }
 162        if (revs->edge_hint_aggressive) {
 163                for (i = 0; i < revs->cmdline.nr; i++) {
 164                        struct object *obj = revs->cmdline.rev[i].item;
 165                        struct commit *commit = (struct commit *)obj;
 166                        if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
 167                                continue;
 168                        mark_tree_uninteresting(commit->tree);
 169                        if (!(obj->flags & SHOWN)) {
 170                                obj->flags |= SHOWN;
 171                                show_edge(commit);
 172                        }
 173                }
 174        }
 175}
 176
 177static void add_pending_tree(struct rev_info *revs, struct tree *tree)
 178{
 179        add_pending_object(revs, &tree->object, "");
 180}
 181
 182void traverse_commit_list(struct rev_info *revs,
 183                          show_commit_fn show_commit,
 184                          show_object_fn show_object,
 185                          void *data)
 186{
 187        int i;
 188        struct commit *commit;
 189        struct strbuf base;
 190
 191        strbuf_init(&base, PATH_MAX);
 192        while ((commit = get_revision(revs)) != NULL) {
 193                /*
 194                 * an uninteresting boundary commit may not have its tree
 195                 * parsed yet, but we are not going to show them anyway
 196                 */
 197                if (commit->tree)
 198                        add_pending_tree(revs, commit->tree);
 199                show_commit(commit, data);
 200        }
 201        for (i = 0; i < revs->pending.nr; i++) {
 202                struct object_array_entry *pending = revs->pending.objects + i;
 203                struct object *obj = pending->item;
 204                const char *name = pending->name;
 205                const char *path = pending->path;
 206                if (obj->flags & (UNINTERESTING | SEEN))
 207                        continue;
 208                if (obj->type == OBJ_TAG) {
 209                        obj->flags |= SEEN;
 210                        show_object(obj, NULL, name, data);
 211                        continue;
 212                }
 213                if (!path)
 214                        path = "";
 215                if (obj->type == OBJ_TREE) {
 216                        process_tree(revs, (struct tree *)obj, show_object,
 217                                     &base, path, data);
 218                        continue;
 219                }
 220                if (obj->type == OBJ_BLOB) {
 221                        process_blob(revs, (struct blob *)obj, show_object,
 222                                     NULL, path, data);
 223                        continue;
 224                }
 225                die("unknown pending object %s (%s)",
 226                    sha1_to_hex(obj->sha1), name);
 227        }
 228        object_array_clear(&revs->pending);
 229        strbuf_release(&base);
 230}