]> git.ipfire.org Git - thirdparty/git.git/blobdiff - convert.c
Merge git://git.bogomips.org/git-svn
[thirdparty/git.git] / convert.c
index e41a31e4807e92e210854214d46767b9752c5181..7eb51b16ed03e650f3af11383c5c43c00d7d9812 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "attr.h"
 #include "run-command.h"
+#include "quote.h"
 
 /*
  * convert.c - convert a file when checking it out and checking it in.
@@ -17,7 +18,7 @@ enum action {
        CRLF_TEXT,
        CRLF_INPUT,
        CRLF_CRLF,
-       CRLF_AUTO,
+       CRLF_AUTO
 };
 
 struct text_stat {
@@ -93,7 +94,8 @@ static int is_binary(unsigned long size, struct text_stat *stats)
        return 0;
 }
 
-static enum eol determine_output_conversion(enum action action) {
+static enum eol determine_output_conversion(enum action action)
+{
        switch (action) {
        case CRLF_BINARY:
                return EOL_UNSET;
@@ -317,6 +319,7 @@ struct filter_params {
        const char *src;
        unsigned long size;
        const char *cmd;
+       const char *path;
 };
 
 static int filter_buffer(int in, int out, void *data)
@@ -329,7 +332,23 @@ static int filter_buffer(int in, int out, void *data)
        int write_err, status;
        const char *argv[] = { NULL, NULL };
 
-       argv[0] = params->cmd;
+       /* apply % substitution to cmd */
+       struct strbuf cmd = STRBUF_INIT;
+       struct strbuf path = STRBUF_INIT;
+       struct strbuf_expand_dict_entry dict[] = {
+               { "f", NULL, },
+               { NULL, NULL, },
+       };
+
+       /* quote the path to preserve spaces, etc. */
+       sq_quote_buf(&path, params->path);
+       dict[0].value = path.buf;
+
+       /* expand all %f with the quoted path */
+       strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
+       strbuf_release(&path);
+
+       argv[0] = cmd.buf;
 
        memset(&child_process, 0, sizeof(child_process));
        child_process.argv = argv;
@@ -349,6 +368,8 @@ static int filter_buffer(int in, int out, void *data)
        status = finish_command(&child_process);
        if (status)
                error("external filter %s failed %d", params->cmd, status);
+
+       strbuf_release(&cmd);
        return (write_err || status);
 }
 
@@ -376,6 +397,7 @@ static int apply_filter(const char *path, const char *src, size_t len,
        params.src = src;
        params.size = len;
        params.cmd = cmd;
+       params.path = path;
 
        fflush(NULL);
        if (start_async(&async))
@@ -693,7 +715,8 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check)
        return !!ATTR_TRUE(value);
 }
 
-enum action determine_action(enum action text_attr, enum eol eol_attr) {
+static enum action determine_action(enum action text_attr, enum eol eol_attr)
+{
        if (text_attr == CRLF_BINARY)
                return CRLF_BINARY;
        if (eol_attr == EOL_LF)
@@ -739,7 +762,9 @@ int convert_to_git(const char *path, const char *src, size_t len,
        return ret | ident_to_git(path, src, len, dst, ident);
 }
 
-int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
+static int convert_to_working_tree_internal(const char *path, const char *src,
+                                           size_t len, struct strbuf *dst,
+                                           int normalizing)
 {
        struct git_attr_check check[5];
        enum action action = CRLF_GUESS;
@@ -765,11 +790,32 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc
                src = dst->buf;
                len = dst->len;
        }
-       action = determine_action(action, eol_attr);
-       ret |= crlf_to_worktree(path, src, len, dst, action);
+       /*
+        * CRLF conversion can be skipped if normalizing, unless there
+        * is a smudge filter.  The filter might expect CRLFs.
+        */
+       if (filter || !normalizing) {
+               action = determine_action(action, eol_attr);
+               ret |= crlf_to_worktree(path, src, len, dst, action);
+               if (ret) {
+                       src = dst->buf;
+                       len = dst->len;
+               }
+       }
+       return ret | apply_filter(path, src, len, dst, filter);
+}
+
+int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
+{
+       return convert_to_working_tree_internal(path, src, len, dst, 0);
+}
+
+int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
+{
+       int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
        if (ret) {
                src = dst->buf;
                len = dst->len;
        }
-       return ret | apply_filter(path, src, len, dst, filter);
+       return ret | convert_to_git(path, src, len, dst, 0);
 }