For now, only handle --prune/--no-prune.  But handle the option via a
callback so that in the future --prune=PATTERN can be implemented.

The new functions are not yet used.

Signed-off-by: Michael Haggerty <mhag...@alum.mit.edu>
---
 remote.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 remote.h | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)

diff --git a/remote.c b/remote.c
index 075ed71..297e52f 100644
--- a/remote.c
+++ b/remote.c
@@ -7,6 +7,7 @@
 #include "dir.h"
 #include "tag.h"
 #include "string-list.h"
+#include "argv-array.h"
 #include "mergesort.h"
 
 enum map_direction { FROM_SRC, FROM_DST };
@@ -58,6 +59,67 @@ static struct rewrites rewrites_push;
 #define BUF_SIZE (2048)
 static char buffer[BUF_SIZE];
 
+int prune_option_parse(const struct option *opt, const char *arg, int unset)
+{
+       struct prune_option *target = opt->value;
+
+       if (unset) {
+               target->prune = 0;
+       } else {
+               target->prune = 1;
+       }
+       return 0;
+}
+
+static int git_fetch_config(const char *k, const char *v, void *cb)
+{
+       int *fetch_prune_config = cb;
+
+       if (!strcmp(k, "fetch.prune")) {
+               *fetch_prune_config = git_config_bool(k, v);
+               return 0;
+       }
+       return 0;
+}
+
+void prune_option_fill(struct remote *remote,
+                      struct prune_option *prune_option, int default_prune)
+{
+       if (prune_option->prune < 0) {
+               /*
+                * The user specified neither --prune nor --no-prune;
+                * use the configured value of remote.<name>.prune or
+                * fetch.prune:
+                */
+               if (remote->prune >= 0) {
+                       prune_option->prune = remote->prune;
+               } else {
+                       int fetch_prune_config = -1;
+
+                       git_config(git_fetch_config, &fetch_prune_config);
+
+                       if (fetch_prune_config >= 0)
+                               prune_option->prune = fetch_prune_config;
+                       else
+                               prune_option->prune = default_prune;
+               }
+       }
+}
+
+void argv_push_prune_option(struct argv_array *argv,
+                           struct prune_option *prune_option)
+{
+       if (prune_option->prune != -1)
+               argv_array_pushf(argv,
+                                prune_option->prune
+                                ? "--prune"
+                                : "--no-prune");
+}
+
+void prune_option_clear(struct prune_option *prune_option)
+{
+}
+
 static int valid_remote(const struct remote *remote)
 {
        return (!!remote->url) || (!!remote->foreign_vcs);
diff --git a/remote.h b/remote.h
index afa3792..21ff4cb 100644
--- a/remote.h
+++ b/remote.h
@@ -53,6 +53,40 @@ struct remote {
        char *http_proxy;
 };
 
+/* Structure to hold parsed --prune/--no-prune options */
+struct prune_option {
+       /* Should we prune at all?  -1 is indeterminate. */
+       int prune;
+};
+
+#define PRUNE_OPTION_INIT { -1 }
+
+/* parse_opts() callback for --prune/--no-prune options */
+int prune_option_parse(const struct option *opt, const char *arg, int unset);
+
+/*
+ * Fill in prune_option for the specified remote, given the
+ * prune_option values parsed from the command-line.  default_prune
+ * specifies whether pruning should default to true or false if it has
+ * not been configured explicitly.
+ */
+void prune_option_fill(struct remote *remote,
+                      struct prune_option *prune_option, int default_prune);
+
+/*
+ * Add --prune/--prune=<pattern>/--no-prune options to the argv_array
+ * to represent the options in prune_options.
+ */
+struct argv_array;
+void argv_push_prune_option(struct argv_array *argv,
+                           struct prune_option *prune_option);
+
+/*
+ * Free any resources used by *prune_option (but not *prune_option
+ * itself).
+ */
+void prune_option_clear(struct prune_option *prune_option);
+
 struct remote *remote_get(const char *name);
 struct remote *pushremote_get(const char *name);
 int remote_is_configured(const char *name);
@@ -238,6 +272,7 @@ struct ref *guess_remote_head(const struct ref *head,
  * Return refs that no longer exist on remote and that match one of
  * the patterns.
  */
+struct string_list;
 struct ref *get_stale_heads(struct refspec *refs, int ref_count,
                            struct ref *fetch_map,
                            struct string_list *patterns);
-- 
1.8.4.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to