In later patches we introduce the options and flag for commands
that modify the working directory, e.g. git-checkout.

This piece of code will answer the question:
"Is it safe to change the submodule to this new state?"
e.g. is it overwriting untracked files or are there local
changes that would be overwritten?

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 submodule.c | 22 ++++++++++++++++++++++
 submodule.h |  2 ++
 2 files changed, 24 insertions(+)

diff --git a/submodule.c b/submodule.c
index 2773151..2149ef7 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1201,6 +1201,28 @@ int ok_to_remove_submodule(const char *path)
        return ok_to_remove;
 }
 
+int is_submodule_checkout_safe(const char *path, const struct object_id *oid)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+
+       if (!is_submodule_populated(path))
+               /* The submodule is not populated, it's safe to check it out */
+               /*
+                * TODO: When git learns to re-populate submodules, a check 
must be
+                * added here to assert that no local files will be overwritten.
+                */
+               return 1;
+
+       argv_array_pushl(&cp.args, "read-tree", "-n", "-m", "HEAD",
+                        sha1_to_hex(oid->hash), NULL);
+
+       prepare_submodule_repo_env(&cp.env_array);
+       cp.git_cmd = 1;
+       cp.no_stdin = 1;
+       cp.dir = path;
+       return run_command(&cp) == 0;
+}
+
 static int find_first_merges(struct object_array *result, const char *path,
                struct commit *a, struct commit *b)
 {
diff --git a/submodule.h b/submodule.h
index f01f87c..a7fa634 100644
--- a/submodule.h
+++ b/submodule.h
@@ -74,6 +74,8 @@ extern unsigned is_submodule_modified(const char *path, int 
ignore_untracked);
 extern int is_submodule_populated(const char *path);
 extern int submodule_uses_gitfile(const char *path);
 extern int ok_to_remove_submodule(const char *path);
+extern int is_submodule_checkout_safe(const char *path,
+                                     const struct object_id *oid);
 extern int merge_submodule(unsigned char result[20], const char *path,
                           const unsigned char base[20],
                           const unsigned char a[20],
-- 
2.10.1.469.g00a8914

Reply via email to