submodule.con commit git-svn: allow subset of branches/tags to be specified in glob spec (0757620)
   1#include "cache.h"
   2#include "submodule.h"
   3#include "dir.h"
   4#include "diff.h"
   5#include "commit.h"
   6#include "revision.h"
   7#include "run-command.h"
   8
   9static int add_submodule_odb(const char *path)
  10{
  11        struct strbuf objects_directory = STRBUF_INIT;
  12        struct alternate_object_database *alt_odb;
  13
  14        strbuf_addf(&objects_directory, "%s/.git/objects/", path);
  15        if (!is_directory(objects_directory.buf))
  16                return -1;
  17
  18        /* avoid adding it twice */
  19        for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
  20                if (alt_odb->name - alt_odb->base == objects_directory.len &&
  21                                !strncmp(alt_odb->base, objects_directory.buf,
  22                                        objects_directory.len))
  23                        return 0;
  24
  25        alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
  26        alt_odb->next = alt_odb_list;
  27        strcpy(alt_odb->base, objects_directory.buf);
  28        alt_odb->name = alt_odb->base + objects_directory.len;
  29        alt_odb->name[2] = '/';
  30        alt_odb->name[40] = '\0';
  31        alt_odb->name[41] = '\0';
  32        alt_odb_list = alt_odb;
  33        prepare_alt_odb();
  34        return 0;
  35}
  36
  37void show_submodule_summary(FILE *f, const char *path,
  38                unsigned char one[20], unsigned char two[20],
  39                const char *del, const char *add, const char *reset)
  40{
  41        struct rev_info rev;
  42        struct commit *commit, *left = left, *right = right;
  43        struct commit_list *merge_bases, *list;
  44        const char *message = NULL;
  45        struct strbuf sb = STRBUF_INIT;
  46        static const char *format = "  %m %s";
  47        int fast_forward = 0, fast_backward = 0;
  48
  49        if (is_null_sha1(two))
  50                message = "(submodule deleted)";
  51        else if (add_submodule_odb(path))
  52                message = "(not checked out)";
  53        else if (is_null_sha1(one))
  54                message = "(new submodule)";
  55        else if (!(left = lookup_commit_reference(one)) ||
  56                 !(right = lookup_commit_reference(two)))
  57                message = "(commits not present)";
  58
  59        if (!message) {
  60                init_revisions(&rev, NULL);
  61                setup_revisions(0, NULL, &rev, NULL);
  62                rev.left_right = 1;
  63                rev.first_parent_only = 1;
  64                left->object.flags |= SYMMETRIC_LEFT;
  65                add_pending_object(&rev, &left->object, path);
  66                add_pending_object(&rev, &right->object, path);
  67                merge_bases = get_merge_bases(left, right, 1);
  68                if (merge_bases) {
  69                        if (merge_bases->item == left)
  70                                fast_forward = 1;
  71                        else if (merge_bases->item == right)
  72                                fast_backward = 1;
  73                }
  74                for (list = merge_bases; list; list = list->next) {
  75                        list->item->object.flags |= UNINTERESTING;
  76                        add_pending_object(&rev, &list->item->object,
  77                                sha1_to_hex(list->item->object.sha1));
  78                }
  79                if (prepare_revision_walk(&rev))
  80                        message = "(revision walker failed)";
  81        }
  82
  83        strbuf_addf(&sb, "Submodule %s %s..", path,
  84                        find_unique_abbrev(one, DEFAULT_ABBREV));
  85        if (!fast_backward && !fast_forward)
  86                strbuf_addch(&sb, '.');
  87        strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
  88        if (message)
  89                strbuf_addf(&sb, " %s\n", message);
  90        else
  91                strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
  92        fwrite(sb.buf, sb.len, 1, f);
  93
  94        if (!message) {
  95                while ((commit = get_revision(&rev))) {
  96                        struct pretty_print_context ctx = {0};
  97                        ctx.date_mode = rev.date_mode;
  98                        strbuf_setlen(&sb, 0);
  99                        if (commit->object.flags & SYMMETRIC_LEFT) {
 100                                if (del)
 101                                        strbuf_addstr(&sb, del);
 102                        }
 103                        else if (add)
 104                                strbuf_addstr(&sb, add);
 105                        format_commit_message(commit, format, &sb, &ctx);
 106                        if (reset)
 107                                strbuf_addstr(&sb, reset);
 108                        strbuf_addch(&sb, '\n');
 109                        fprintf(f, "%s", sb.buf);
 110                }
 111                clear_commit_marks(left, ~0);
 112                clear_commit_marks(right, ~0);
 113        }
 114        strbuf_release(&sb);
 115}
 116
 117int is_submodule_modified(const char *path)
 118{
 119        int len;
 120        struct child_process cp;
 121        const char *argv[] = {
 122                "status",
 123                "--porcelain",
 124                NULL,
 125        };
 126        char *env[3];
 127        struct strbuf buf = STRBUF_INIT;
 128
 129        strbuf_addf(&buf, "%s/.git/", path);
 130        if (!is_directory(buf.buf)) {
 131                strbuf_release(&buf);
 132                /* The submodule is not checked out, so it is not modified */
 133                return 0;
 134
 135        }
 136        strbuf_reset(&buf);
 137
 138        strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
 139        env[0] = strbuf_detach(&buf, NULL);
 140        strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
 141        env[1] = strbuf_detach(&buf, NULL);
 142        env[2] = NULL;
 143
 144        memset(&cp, 0, sizeof(cp));
 145        cp.argv = argv;
 146        cp.env = (const char *const *)env;
 147        cp.git_cmd = 1;
 148        cp.no_stdin = 1;
 149        cp.out = -1;
 150        if (start_command(&cp))
 151                die("Could not run git status --porcelain");
 152
 153        len = strbuf_read(&buf, cp.out, 1024);
 154        close(cp.out);
 155
 156        if (finish_command(&cp))
 157                die("git status --porcelain failed");
 158
 159        free(env[0]);
 160        free(env[1]);
 161        strbuf_release(&buf);
 162        return len != 0;
 163}