submodules: introduce check to see whether to touch a submodule

In later patches we introduce the --recurse-submodule flag for commands that modify the working directory, e.g. git-checkout. It is potentially expensive to check if a submodule needs an update, because a common theme to interact with submodules is to spawn a child process for each interaction. So let's introduce a function that checks if a submodule needs to be checked for an update before attempting the update. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Mar 14, 2017 at 14:46 UTC 84f8925eeb97827e9d334b8b9ff7282177ab9105
2 files changed +23
submodule.c
+16
@@ -548,6 +548,22 @@ void set_config_update_recurse_submodules(int value)
548 config_update_recurse_submodules = value;
549 }
550
551 +int should_update_submodules(void)
552 +{
553 + return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
554 +}
555 +
556 +const struct submodule *submodule_from_ce(const struct cache_entry *ce)
557 +{
558 + if (!S_ISGITLINK(ce->ce_mode))
559 + return NULL;
560 +
561 + if (!should_update_submodules())
562 + return NULL;
563 +
564 + return submodule_from_path(null_sha1, ce->name);
565 +}
566 +
567 static int has_remote(const char *refname, const struct object_id *oid,
568 int flags, void *cb_data)
569 {
submodule.h
+7
@@ -65,6 +65,13 @@ extern void show_submodule_inline_diff(FILE *f, const char *path,
65 const struct diff_options *opt);
66 extern void set_config_fetch_recurse_submodules(int value);
67 extern void set_config_update_recurse_submodules(int value);
68 +/* Check if we want to update any submodule.*/
69 +extern int should_update_submodules(void);
70 +/*
71 + * Returns the submodule struct if the given ce entry is a submodule
72 + * and it should be updated. Returns NULL otherwise.
73 + */
74 +extern const struct submodule *submodule_from_ce(const struct cache_entry *ce);
75 extern void check_for_new_submodule_commits(unsigned char new_sha1[20]);
76 extern int fetch_populated_submodules(const struct argv_array *options,
77 const char *prefix, int command_line_option,