repository: free fields before overwriting them
[gitweb.git] / convert.c
index 972bf8aec27594a94ad756431e551c2f30796c4d..c5f0b210370c0ee83ef59298fa2260d627332399 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -513,78 +513,17 @@ static struct hashmap subprocess_map;
 
 static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
 {
-       int err, i;
-       struct cmd2process *entry = (struct cmd2process *)subprocess;
-       struct string_list cap_list = STRING_LIST_INIT_NODUP;
-       char *cap_buf;
-       const char *cap_name;
-       struct child_process *process = &subprocess->process;
-       const char *cmd = subprocess->cmd;
-
-       static const struct {
-               const char *name;
-               unsigned int cap;
-       } known_caps[] = {
+       static int versions[] = {2, 0};
+       static struct subprocess_capability capabilities[] = {
                { "clean",  CAP_CLEAN  },
                { "smudge", CAP_SMUDGE },
                { "delay",  CAP_DELAY  },
+               { NULL, 0 }
        };
-
-       sigchain_push(SIGPIPE, SIG_IGN);
-
-       err = packet_writel(process->in, "git-filter-client", "version=2", NULL);
-       if (err)
-               goto done;
-
-       err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
-       if (err) {
-               error("external filter '%s' does not support filter protocol version 2", cmd);
-               goto done;
-       }
-       err = strcmp(packet_read_line(process->out, NULL), "version=2");
-       if (err)
-               goto done;
-       err = packet_read_line(process->out, NULL) != NULL;
-       if (err)
-               goto done;
-
-       for (i = 0; i < ARRAY_SIZE(known_caps); ++i) {
-               err = packet_write_fmt_gently(
-                       process->in, "capability=%s\n", known_caps[i].name);
-               if (err)
-                       goto done;
-       }
-       err = packet_flush_gently(process->in);
-       if (err)
-               goto done;
-
-       for (;;) {
-               cap_buf = packet_read_line(process->out, NULL);
-               if (!cap_buf)
-                       break;
-               string_list_split_in_place(&cap_list, cap_buf, '=', 1);
-
-               if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
-                       continue;
-
-               cap_name = cap_list.items[1].string;
-               i = ARRAY_SIZE(known_caps) - 1;
-               while (i >= 0 && strcmp(cap_name, known_caps[i].name))
-                       i--;
-
-               if (i >= 0)
-                       entry->supported_capabilities |= known_caps[i].cap;
-               else
-                       warning("external filter '%s' requested unsupported filter capability '%s'",
-                       cmd, cap_name);
-
-               string_list_clear(&cap_list, 0);
-       }
-
-done:
-       sigchain_pop(SIGPIPE);
-
-       return err;
+       struct cmd2process *entry = (struct cmd2process *)subprocess;
+       return subprocess_handshake(subprocess, "git-filter", versions, NULL,
+                                   capabilities,
+                                   &entry->supported_capabilities);
 }
 
 static void handle_filter_error(const struct strbuf *filter_status,
@@ -625,8 +564,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
 
        if (!subprocess_map_initialized) {
                subprocess_map_initialized = 1;
-               hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
-                            NULL, 0);
+               hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
                entry = NULL;
        } else {
                entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
@@ -1194,10 +1132,12 @@ int convert_to_git(const struct index_state *istate,
                src = dst->buf;
                len = dst->len;
        }
-       ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
-       if (ret && dst) {
-               src = dst->buf;
-               len = dst->len;
+       if (checksafe != SAFE_CRLF_KEEP_CRLF) {
+               ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
+               if (ret && dst) {
+                       src = dst->buf;
+                       len = dst->len;
+               }
        }
        return ret | ident_to_git(path, src, len, dst, ca.ident);
 }