builtin/ls-files: convert overlay_tree_on_cache to object_id
[gitweb.git] / submodule.c
index 6368d5fa6afe001d38a63908a5a62cfe498ceaea..ba5429be15c9f21369366585ce067e8e1263552e 100644 (file)
@@ -14,6 +14,7 @@
 #include "blob.h"
 #include "thread-utils.h"
 #include "quote.h"
+#include "remote.h"
 #include "worktree.h"
 
 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
@@ -446,8 +447,8 @@ static void show_submodule_header(FILE *f, const char *path,
         * Attempt to lookup the commit references, and determine if this is
         * a fast forward or fast backwards update.
         */
-       *left = lookup_commit_reference(one->hash);
-       *right = lookup_commit_reference(two->hash);
+       *left = lookup_commit_reference(one);
+       *right = lookup_commit_reference(two);
 
        /*
         * Warn about missing commits in the submodule project, but only if
@@ -633,7 +634,7 @@ static int check_has_commit(const struct object_id *oid, void *data)
 {
        int *has_commit = data;
 
-       if (!lookup_commit_reference(oid->hash))
+       if (!lookup_commit_reference(oid))
                *has_commit = 0;
 
        return 0;
@@ -783,7 +784,11 @@ int find_unpushed_submodules(struct oid_array *commits,
        return needs_pushing->nr;
 }
 
-static int push_submodule(const char *path, int dry_run)
+static int push_submodule(const char *path,
+                         const struct remote *remote,
+                         const char **refspec, int refspec_nr,
+                         const struct string_list *push_options,
+                         int dry_run)
 {
        if (add_submodule_odb(path))
                return 1;
@@ -794,6 +799,20 @@ static int push_submodule(const char *path, int dry_run)
                if (dry_run)
                        argv_array_push(&cp.args, "--dry-run");
 
+               if (push_options && push_options->nr) {
+                       const struct string_list_item *item;
+                       for_each_string_list_item(item, push_options)
+                               argv_array_pushf(&cp.args, "--push-option=%s",
+                                                item->string);
+               }
+
+               if (remote->origin != REMOTE_UNCONFIGURED) {
+                       int i;
+                       argv_array_push(&cp.args, remote->name);
+                       for (i = 0; i < refspec_nr; i++)
+                               argv_array_push(&cp.args, refspec[i]);
+               }
+
                prepare_submodule_repo_env(&cp.env_array);
                cp.git_cmd = 1;
                cp.no_stdin = 1;
@@ -806,20 +825,67 @@ static int push_submodule(const char *path, int dry_run)
        return 1;
 }
 
+/*
+ * Perform a check in the submodule to see if the remote and refspec work.
+ * Die if the submodule can't be pushed.
+ */
+static void submodule_push_check(const char *path, const struct remote *remote,
+                                const char **refspec, int refspec_nr)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+       int i;
+
+       argv_array_push(&cp.args, "submodule--helper");
+       argv_array_push(&cp.args, "push-check");
+       argv_array_push(&cp.args, remote->name);
+
+       for (i = 0; i < refspec_nr; i++)
+               argv_array_push(&cp.args, refspec[i]);
+
+       prepare_submodule_repo_env(&cp.env_array);
+       cp.git_cmd = 1;
+       cp.no_stdin = 1;
+       cp.no_stdout = 1;
+       cp.dir = path;
+
+       /*
+        * Simply indicate if 'submodule--helper push-check' failed.
+        * More detailed error information will be provided by the
+        * child process.
+        */
+       if (run_command(&cp))
+               die("process for submodule '%s' failed", path);
+}
+
 int push_unpushed_submodules(struct oid_array *commits,
-                            const char *remotes_name,
+                            const struct remote *remote,
+                            const char **refspec, int refspec_nr,
+                            const struct string_list *push_options,
                             int dry_run)
 {
        int i, ret = 1;
        struct string_list needs_pushing = STRING_LIST_INIT_DUP;
 
-       if (!find_unpushed_submodules(commits, remotes_name, &needs_pushing))
+       if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
                return 1;
 
+       /*
+        * Verify that the remote and refspec can be propagated to all
+        * submodules.  This check can be skipped if the remote and refspec
+        * won't be propagated due to the remote being unconfigured (e.g. a URL
+        * instead of a remote name).
+        */
+       if (remote->origin != REMOTE_UNCONFIGURED)
+               for (i = 0; i < needs_pushing.nr; i++)
+                       submodule_push_check(needs_pushing.items[i].string,
+                                            remote, refspec, refspec_nr);
+
+       /* Actually push the submodules */
        for (i = 0; i < needs_pushing.nr; i++) {
                const char *path = needs_pushing.items[i].string;
                fprintf(stderr, "Pushing submodule '%s'\n", path);
-               if (!push_submodule(path, dry_run)) {
+               if (!push_submodule(path, remote, refspec, refspec_nr,
+                                   push_options, dry_run)) {
                        fprintf(stderr, "Unable to push submodule '%s'\n", path);
                        ret = 0;
                }
@@ -830,17 +896,17 @@ int push_unpushed_submodules(struct oid_array *commits,
        return ret;
 }
 
-static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
+static int is_submodule_commit_present(const char *path, struct object_id *oid)
 {
        int is_present = 0;
-       if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
+       if (!add_submodule_odb(path) && lookup_commit_reference(oid)) {
                /* Even if the submodule is checked out and the commit is
                 * present, make sure it is reachable from a ref. */
                struct child_process cp = CHILD_PROCESS_INIT;
                const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
                struct strbuf buf = STRBUF_INIT;
 
-               argv[3] = sha1_to_hex(sha1);
+               argv[3] = oid_to_hex(oid);
                cp.argv = argv;
                prepare_submodule_repo_env(&cp.env_array);
                cp.git_cmd = 1;
@@ -871,7 +937,7 @@ static void submodule_collect_changed_cb(struct diff_queue_struct *q,
                         * being moved around. */
                        struct string_list_item *path;
                        path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
-                       if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
+                       if (!path && !is_submodule_commit_present(p->two->path, &p->two->oid))
                                string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
                } else {
                        /* Submodule is new or was moved here */
@@ -1263,7 +1329,7 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
        cp.dir = path;
        if (start_command(&cp)) {
                if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
-                       die(_("could not start 'git status in submodule '%s'"),
+                       die(_("could not start 'git status' in submodule '%s'"),
                                path);
                ret = -1;
                goto out;
@@ -1276,7 +1342,7 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
 
        if (finish_command(&cp)) {
                if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
-                       die(_("could not run 'git status in submodule '%s'"),
+                       die(_("could not run 'git status' in submodule '%s'"),
                                path);
                ret = -1;
        }
@@ -1408,8 +1474,7 @@ int submodule_move_head(const char *path,
                        cp1.no_stdin = 1;
                        cp1.dir = path;
 
-                       argv_array_pushl(&cp1.args, "update-ref", "HEAD",
-                                        new ? new : EMPTY_TREE_SHA1_HEX, NULL);
+                       argv_array_pushl(&cp1.args, "update-ref", "HEAD", new, NULL);
 
                        if (run_command(&cp1)) {
                                ret = -1;
@@ -1501,9 +1566,9 @@ static void print_commit(struct commit *commit)
 #define MERGE_WARNING(path, msg) \
        warning("Failed to merge submodule %s (%s)", path, msg);
 
-int merge_submodule(unsigned char result[20], const char *path,
-                   const unsigned char base[20], const unsigned char a[20],
-                   const unsigned char b[20], int search)
+int merge_submodule(struct object_id *result, const char *path,
+                   const struct object_id *base, const struct object_id *a,
+                   const struct object_id *b, int search)
 {
        struct commit *commit_base, *commit_a, *commit_b;
        int parent_count;
@@ -1512,14 +1577,14 @@ int merge_submodule(unsigned char result[20], const char *path,
        int i;
 
        /* store a in result in case we fail */
-       hashcpy(result, a);
+       oidcpy(result, a);
 
        /* we can not handle deletion conflicts */
-       if (is_null_sha1(base))
+       if (is_null_oid(base))
                return 0;
-       if (is_null_sha1(a))
+       if (is_null_oid(a))
                return 0;
-       if (is_null_sha1(b))
+       if (is_null_oid(b))
                return 0;
 
        if (add_submodule_odb(path)) {
@@ -1543,11 +1608,11 @@ int merge_submodule(unsigned char result[20], const char *path,
 
        /* Case #1: a is contained in b or vice versa */
        if (in_merge_bases(commit_a, commit_b)) {
-               hashcpy(result, b);
+               oidcpy(result, b);
                return 1;
        }
        if (in_merge_bases(commit_b, commit_a)) {
-               hashcpy(result, a);
+               oidcpy(result, a);
                return 1;
        }
 
@@ -1800,3 +1865,34 @@ const char *get_superproject_working_tree(void)
 
        return ret;
 }
+
+int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
+{
+       const struct submodule *sub;
+       const char *git_dir;
+       int ret = 0;
+
+       strbuf_reset(buf);
+       strbuf_addstr(buf, submodule);
+       strbuf_complete(buf, '/');
+       strbuf_addstr(buf, ".git");
+
+       git_dir = read_gitfile(buf->buf);
+       if (git_dir) {
+               strbuf_reset(buf);
+               strbuf_addstr(buf, git_dir);
+       }
+       if (!is_git_directory(buf->buf)) {
+               gitmodules_config();
+               sub = submodule_from_path(null_sha1, submodule);
+               if (!sub) {
+                       ret = -1;
+                       goto cleanup;
+               }
+               strbuf_reset(buf);
+               strbuf_git_path(buf, "%s/%s", "modules", sub->name);
+       }
+
+cleanup:
+       return ret;
+}