From: Pete Wyckoff <pw@padd.com>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>,
git@vger.kernel.org, Jeff King <peff@peff.net>,
Johannes Sixt <j.sixt@viscovery.net>
Subject: [PATCH v4] convert filter: supply path to external driver
Date: Tue, 21 Dec 2010 12:33:22 -0800 [thread overview]
Message-ID: <20101221203322.GA13868@honk.padd.com> (raw)
In-Reply-To: <20101221181924.GB25812@burratino>
Filtering to support keyword expansion may need the name of
the file being filtered. In particular, to support p4 keywords
like
$File: //depot/product/dir/script.sh $
the smudge filter needs to know the name of the file it is
smudging.
Add a "%f" conversion specifier to the gitattribute for filter.
It will be expanded with the path name to the file when invoking
the external filter command. The path name is quoted and
special characters are escaped to prevent the shell from splitting
incorrectly.
Signed-off-by: Pete Wyckoff <pw@padd.com>
---
jrnieder@gmail.com wrote on Tue, 21 Dec 2010 12:19 -0600:
> [detailed review]
Thanks for the nitpicks. I put the argc.sh and chmod +x into a
setup test. Tried to put some more in there, and to break up the
test in two, but did not want to duplicate the complex
calculation of "norm" and "spec" variables. So ended up with the
small setup, and just one big test still.
I couldn't quite bring myself to delete the nice spaces in
redirections like "> test". Rest of the usage in t/ seems
to be about 1/3 for space, 2/3 against.
Got the test_cmp, tabs and missing &&, too, thanks for finding
those.
-- Pete
Documentation/gitattributes.txt | 12 +++++++++
convert.c | 22 +++++++++++++++++-
t/t0021-conversion.sh | 48 +++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+), 1 deletions(-)
diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index 564586b..1afcf01 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -317,6 +317,18 @@ command is "cat").
smudge = cat
------------------------
+If your filter needs the path of the file it is working on,
+you can use the "%f" conversion specification. It will be
+replaced with the relative path to the file. This is important
+for keyword substitution that depends on the name of the
+file. Like this:
+
+------------------------
+[filter "p4"]
+ clean = git-p4-filter --clean %f
+ smudge = git-p4-filter --smudge %f
+------------------------
+
Interaction between checkin/checkout attributes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/convert.c b/convert.c
index e41a31e..8f020bc 100644
--- a/convert.c
+++ b/convert.c
@@ -317,6 +317,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 +330,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 +366,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 +395,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))
diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh
index 828e35b..69c22a6 100755
--- a/t/t0021-conversion.sh
+++ b/t/t0021-conversion.sh
@@ -93,4 +93,52 @@ test_expect_success expanded_in_repo '
cmp expanded-keywords expected-output
'
+test_expect_success 'filter shell-escaped filenames setup' '
+ cat >argc.sh <<-EOF &&
+ #!$SHELL_PATH
+ echo argc: \$# "\$@"
+ EOF
+ chmod +x argc.sh
+'
+
+# The use of %f in a filter definition is expanded to the path to
+# the filename being smudged or cleaned. It must be shell escaped.
+# First, set up some interesting file names and pet them in
+# .gitattributes.
+test_expect_success 'filter shell-escaped filenames test' '
+ norm=name-no-magic &&
+ spec=$(echo name:sgl\"dbl\ spc!bang | tr : \\047) &&
+ echo some test text > test &&
+ cat test > $norm &&
+ cat test > "$spec" &&
+ git add $norm &&
+ git add "$spec" &&
+ git commit -q -m "add files" &&
+ echo "name* filter=argc" > .gitattributes &&
+
+ # delete the files and check them out again, using a smudge filter
+ # that will count the args and echo the command-line back to us
+ git config filter.argc.smudge "./argc.sh %f" &&
+ rm $norm "$spec" &&
+ git checkout -- $norm "$spec" &&
+
+ # make sure argc.sh counted the right number of args
+ echo "argc: 1 $norm" > res &&
+ test_cmp res $norm &&
+ echo "argc: 1 $spec" > res &&
+ test_cmp res "$spec" &&
+
+ # do the same thing, but with more args in the filter expression
+ git config filter.argc.smudge "./argc.sh %f --myword" &&
+ rm $norm "$spec" &&
+ git checkout -- $norm "$spec" &&
+
+ # make sure argc.sh counted the right number of args
+ echo "argc: 2 $norm --myword" > res &&
+ test_cmp res $norm &&
+ echo "argc: 2 $spec --myword" > res &&
+ test_cmp res "$spec" &&
+ :
+'
+
test_done
--
1.7.2.3
next prev parent reply other threads:[~2010-12-21 20:33 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-18 22:38 smudge/clean filter needs filename Pete Wyckoff
2010-12-19 21:29 ` [PATCH] convert filter: supply path to external driver Pete Wyckoff
2010-12-19 21:59 ` Junio C Hamano
2010-12-20 2:24 ` Jeff King
2010-12-20 5:52 ` david
2010-12-20 16:09 ` [PATCH v2] " Pete Wyckoff
2010-12-20 17:59 ` Junio C Hamano
2010-12-21 13:44 ` [PATCH v3] " Pete Wyckoff
2010-12-21 18:19 ` Jonathan Nieder
2010-12-21 20:33 ` Pete Wyckoff [this message]
2010-12-21 21:24 ` [PATCH v4] " Junio C Hamano
2010-12-22 14:40 ` [PATCH v5] " Pete Wyckoff
2010-12-22 18:10 ` Junio C Hamano
2010-12-22 23:22 ` Junio C Hamano
2010-12-20 8:04 ` [PATCH] " Johannes Sixt
2010-12-20 8:52 ` Junio C Hamano
2010-12-20 14:41 ` Pete Wyckoff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20101221203322.GA13868@honk.padd.com \
--to=pw@padd.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=j.sixt@viscovery.net \
--cc=jrnieder@gmail.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).