Code

Show submodules as modified when they contain a dirty work tree
[git.git] / submodule.c
index 461faf0e000ee14cdd0bb3d40121e1d21833e30d..3f851deb6afad564a786912a7bb4f9069d697ec7 100644 (file)
@@ -4,6 +4,7 @@
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
+#include "run-command.h"
 
 int add_submodule_odb(const char *path)
 {
@@ -38,7 +39,7 @@ void show_submodule_summary(FILE *f, const char *path,
                const char *del, const char *add, const char *reset)
 {
        struct rev_info rev;
-       struct commit *commit, *left = left, *right;
+       struct commit *commit, *left = left, *right = right;
        struct commit_list *merge_bases, *list;
        const char *message = NULL;
        struct strbuf sb = STRBUF_INIT;
@@ -112,3 +113,51 @@ void show_submodule_summary(FILE *f, const char *path,
        }
        strbuf_release(&sb);
 }
+
+int is_submodule_modified(const char *path)
+{
+       int len;
+       struct child_process cp;
+       const char *argv[] = {
+               "status",
+               "--porcelain",
+               NULL,
+       };
+       char *env[3];
+       struct strbuf buf = STRBUF_INIT;
+
+       strbuf_addf(&buf, "%s/.git/", path);
+       if (!is_directory(buf.buf)) {
+               strbuf_release(&buf);
+               /* The submodule is not checked out, so it is not modified */
+               return 0;
+
+       }
+       strbuf_reset(&buf);
+
+       strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
+       env[0] = strbuf_detach(&buf, NULL);
+       strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
+       env[1] = strbuf_detach(&buf, NULL);
+       env[2] = NULL;
+
+       memset(&cp, 0, sizeof(cp));
+       cp.argv = argv;
+       cp.env = (const char *const *)env;
+       cp.git_cmd = 1;
+       cp.no_stdin = 1;
+       cp.out = -1;
+       if (start_command(&cp))
+               die("Could not run git status --porcelain");
+
+       len = strbuf_read(&buf, cp.out, 1024);
+       close(cp.out);
+
+       if (finish_command(&cp))
+               die("git status --porcelain failed");
+
+       free(env[0]);
+       free(env[1]);
+       strbuf_release(&buf);
+       return len != 0;
+}