git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [filter-repo PATCH] filter-repo: add new --replace-message option
@ 2021-08-18  4:37 Gwyneth Morgan
  2021-08-23 17:34 ` Elijah Newren
  2021-08-23 20:55 ` [filter-repo PATCH v2] " Gwyneth Morgan
  0 siblings, 2 replies; 5+ messages in thread
From: Gwyneth Morgan @ 2021-08-18  4:37 UTC (permalink / raw)
  To: git; +Cc: Gwyneth Morgan, Elijah Newren

Like --replace-text, add an option --replace-message which replaces text
in commit message bodies, so that users can easily replace text without
constructing a --message-callback.
---
 Documentation/git-filter-repo.txt | 19 +++++++-
 git-filter-repo                   | 12 ++++-
 t/t9390-filter-repo.sh            |  1 +
 t/t9390/basic-message             | 78 +++++++++++++++++++++++++++++++
 t/t9390/sample-message            |  2 +
 5 files changed, 110 insertions(+), 2 deletions(-)
 create mode 100644 t/t9390/basic-message
 create mode 100644 t/t9390/sample-message

diff --git a/Documentation/git-filter-repo.txt b/Documentation/git-filter-repo.txt
index 2798378..7a71375 100644
--- a/Documentation/git-filter-repo.txt
+++ b/Documentation/git-filter-repo.txt
@@ -181,6 +181,10 @@ Renaming of refs (see also --refname-callback)
 Filtering of commit messages (see also --message-callback)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+--replace-message <expressions_file>::
+	A file with expressions that, if found in commit messages, will
+	be replaced. This file uses the same syntax as --replace-text.
+
 --preserve-commit-hashes::
 	By default, since commits are rewritten and thus gain new
 	hashes, references to old commit hashes in commit messages are
@@ -894,7 +898,20 @@ YYYY-MM-DD.  In the expressions file, there are a few things to note:
     beginning and ends of lines rather than the beginning and end of file.
     See https://docs.python.org/3/library/re.html for details.
 
-See also the `--blob-callback` from <<CALLBACKS>>.
+See also the `--blob-callback` from <<CALLBACKS>>.  Similarly, if you
+want to modify commit messages, you can do so with the same syntax.  For
+example, with a file named expressions.txt containing
+
+--------------------------------------------------
+foo==>bar
+--------------------------------------------------
+
+then running
+--------------------------------------------------
+git filter-repo --replace-message expressions.txt
+--------------------------------------------------
+
+will replace `foo` in commit messages with `bar`.
 
 Refname based filtering
 ~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/git-filter-repo b/git-filter-repo
index b91bd96..5fe0f91 100755
--- a/git-filter-repo
+++ b/git-filter-repo
@@ -1843,6 +1843,10 @@ EXAMPLES
 
     messages = parser.add_argument_group(title=_("Filtering of commit messages "
                                                "(see also --message-callback)"))
+    messages.add_argument('--replace-message', metavar='EXPRESSIONS_FILE',
+        help=_("A file with expressions that, if found in commit messages, "
+               "will be replaced. This file uses the same syntax as "
+               "--replace-text."))
     messages.add_argument('--preserve-commit-hashes', action='store_true',
         help=_("By default, since commits are rewritten and thus gain new "
                "hashes, references to old commit hashes in commit messages "
@@ -2189,6 +2193,8 @@ EXAMPLES
       args.mailmap = MailmapInfo(args.mailmap)
     if args.replace_text:
       args.replace_text = FilteringOptions.get_replace_text(args.replace_text)
+    if args.replace_message:
+      args.replace_message = FilteringOptions.get_replace_text(args.replace_message)
     if args.strip_blobs_with_ids:
       with open(args.strip_blobs_with_ids, 'br') as f:
         args.strip_blobs_with_ids = set(f.read().split())
@@ -3374,9 +3380,13 @@ class RepoFilter(object):
     if not self._args.preserve_commit_hashes:
       commit.message = self._hash_re.sub(self._translate_commit_hash,
                                          commit.message)
+    if self._args.replace_message:
+      for literal, replacement in self._args.replace_message['literals']:
+        commit.message = commit.message.replace(literal, replacement)
+      for regex,   replacement in self._args.replace_message['regexes']:
+        commit.message = regex.sub(replacement, commit.message)
     if self._message_callback:
       commit.message = self._message_callback(commit.message)
-
     # Change the author & committer according to mailmap rules
     args = self._args
     if args.mailmap:
diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh
index 3f567e7..6d2d985 100755
--- a/t/t9390-filter-repo.sh
+++ b/t/t9390-filter-repo.sh
@@ -39,6 +39,7 @@ filter_testcase basic basic-filename --invert-paths --path-glob 't*en*'
 filter_testcase basic basic-numbers  --invert-paths --path-regex 'f.*e.*e'
 filter_testcase basic basic-mailmap  --mailmap ../t9390/sample-mailmap
 filter_testcase basic basic-replace  --replace-text ../t9390/sample-replace
+filter_testcase basic basic-message  --replace-message ../t9390/sample-message
 filter_testcase empty empty-keepme   --path keepme
 filter_testcase empty more-empty-keepme --path keepme --prune-empty=always \
 		                                   --prune-degenerate=always
diff --git a/t/t9390/basic-message b/t/t9390/basic-message
new file mode 100644
index 0000000..4ac1968
--- /dev/null
+++ b/t/t9390/basic-message
@@ -0,0 +1,78 @@
+feature done
+blob
+mark :1
+data 8
+initial
+
+reset refs/heads/B
+commit refs/heads/B
+mark :2
+author Little O. Me <me@little.net> 1535228562 -0700
+committer Little O. Me <me@little.net> 1535228562 -0700
+data 9
+Modified
+M 100644 :1 filename
+M 100644 :1 ten
+M 100644 :1 twenty
+
+blob
+mark :3
+data 11
+twenty-mod
+
+commit refs/heads/B
+mark :4
+author Little 'ol Me <me@laptop.(none)> 1535229544 -0700
+committer Little 'ol Me <me@laptop.(none)> 1535229544 -0700
+data 18
+add the number 20
+from :2
+M 100644 :3 twenty
+
+blob
+mark :5
+data 8
+ten-mod
+
+commit refs/heads/A
+mark :6
+author Little O. Me <me@machine52.little.net> 1535229523 -0700
+committer Little O. Me <me@machine52.little.net> 1535229523 -0700
+data 8
+add ten
+from :2
+M 100644 :5 ten
+
+commit refs/heads/master
+mark :7
+author Lit.e Me <me@fire.com> 1535229559 -0700
+committer Lit.e Me <me@fire.com> 1535229580 -0700
+data 24
+Merge branch 'A' into B
+from :4
+merge :6
+M 100644 :5 ten
+
+blob
+mark :8
+data 6
+final
+
+commit refs/heads/master
+mark :9
+author Little Me <me@bigcompany.com> 1535229601 -0700
+committer Little Me <me@bigcompany.com> 1535229601 -0700
+data 9
+whatever
+from :7
+M 100644 :8 filename
+M 100644 :8 ten
+M 100644 :8 twenty
+
+tag v1.0
+from :9
+tagger Little John <second@merry.men> 1535229618 -0700
+data 5
+v1.0
+
+done
diff --git a/t/t9390/sample-message b/t/t9390/sample-message
new file mode 100644
index 0000000..a374d61
--- /dev/null
+++ b/t/t9390/sample-message
@@ -0,0 +1,2 @@
+Initial==>Modified
+regex:tw.nty==>the number 20
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [filter-repo PATCH] filter-repo: add new --replace-message option
  2021-08-18  4:37 [filter-repo PATCH] filter-repo: add new --replace-message option Gwyneth Morgan
@ 2021-08-23 17:34 ` Elijah Newren
  2021-08-23 20:53   ` Gwyneth Morgan
  2021-08-23 20:55 ` [filter-repo PATCH v2] " Gwyneth Morgan
  1 sibling, 1 reply; 5+ messages in thread
From: Elijah Newren @ 2021-08-23 17:34 UTC (permalink / raw)
  To: Gwyneth Morgan; +Cc: Git Mailing List

Hi,

On Tue, Aug 17, 2021 at 9:38 PM Gwyneth Morgan <gwymor@tilde.club> wrote:
>
> Like --replace-text, add an option --replace-message which replaces text
> in commit message bodies, so that users can easily replace text without
> constructing a --message-callback.

Interesting idea.

Missing a Signed-off-by trailer.

> ---
>  Documentation/git-filter-repo.txt | 19 +++++++-
>  git-filter-repo                   | 12 ++++-
>  t/t9390-filter-repo.sh            |  1 +
>  t/t9390/basic-message             | 78 +++++++++++++++++++++++++++++++
>  t/t9390/sample-message            |  2 +
>  5 files changed, 110 insertions(+), 2 deletions(-)
>  create mode 100644 t/t9390/basic-message
>  create mode 100644 t/t9390/sample-message
>
> diff --git a/Documentation/git-filter-repo.txt b/Documentation/git-filter-repo.txt
> index 2798378..7a71375 100644
> --- a/Documentation/git-filter-repo.txt
> +++ b/Documentation/git-filter-repo.txt
> @@ -181,6 +181,10 @@ Renaming of refs (see also --refname-callback)
>  Filtering of commit messages (see also --message-callback)
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> +--replace-message <expressions_file>::
> +       A file with expressions that, if found in commit messages, will
> +       be replaced. This file uses the same syntax as --replace-text.
> +

Just commit messages?  What about tag messages?

>  --preserve-commit-hashes::
>         By default, since commits are rewritten and thus gain new
>         hashes, references to old commit hashes in commit messages are
> @@ -894,7 +898,20 @@ YYYY-MM-DD.  In the expressions file, there are a few things to note:
>      beginning and ends of lines rather than the beginning and end of file.
>      See https://docs.python.org/3/library/re.html for details.
>
> -See also the `--blob-callback` from <<CALLBACKS>>.
> +See also the `--blob-callback` from <<CALLBACKS>>.  Similarly, if you
> +want to modify commit messages, you can do so with the same syntax.  For
> +example, with a file named expressions.txt containing
> +
> +--------------------------------------------------
> +foo==>bar
> +--------------------------------------------------
> +
> +then running
> +--------------------------------------------------
> +git filter-repo --replace-message expressions.txt
> +--------------------------------------------------
> +
> +will replace `foo` in commit messages with `bar`.

You've added this text to the "Content based filtering" section of the
manual, which doesn't make sense.  It should go in a section about
updating commit/tag messages.

>  Refname based filtering
>  ~~~~~~~~~~~~~~~~~~~~~~~
> diff --git a/git-filter-repo b/git-filter-repo
> index b91bd96..5fe0f91 100755
> --- a/git-filter-repo
> +++ b/git-filter-repo
> @@ -1843,6 +1843,10 @@ EXAMPLES
>
>      messages = parser.add_argument_group(title=_("Filtering of commit messages "
>                                                 "(see also --message-callback)"))
> +    messages.add_argument('--replace-message', metavar='EXPRESSIONS_FILE',
> +        help=_("A file with expressions that, if found in commit messages, "
> +               "will be replaced. This file uses the same syntax as "
> +               "--replace-text."))
>      messages.add_argument('--preserve-commit-hashes', action='store_true',
>          help=_("By default, since commits are rewritten and thus gain new "
>                 "hashes, references to old commit hashes in commit messages "
> @@ -2189,6 +2193,8 @@ EXAMPLES
>        args.mailmap = MailmapInfo(args.mailmap)
>      if args.replace_text:
>        args.replace_text = FilteringOptions.get_replace_text(args.replace_text)
> +    if args.replace_message:
> +      args.replace_message = FilteringOptions.get_replace_text(args.replace_message)
>      if args.strip_blobs_with_ids:
>        with open(args.strip_blobs_with_ids, 'br') as f:
>          args.strip_blobs_with_ids = set(f.read().split())
> @@ -3374,9 +3380,13 @@ class RepoFilter(object):
>      if not self._args.preserve_commit_hashes:
>        commit.message = self._hash_re.sub(self._translate_commit_hash,
>                                           commit.message)
> +    if self._args.replace_message:
> +      for literal, replacement in self._args.replace_message['literals']:
> +        commit.message = commit.message.replace(literal, replacement)
> +      for regex,   replacement in self._args.replace_message['regexes']:
> +        commit.message = regex.sub(replacement, commit.message)

Makes sense.

>      if self._message_callback:
>        commit.message = self._message_callback(commit.message)
> -

Why this stray line removal?

>      # Change the author & committer according to mailmap rules
>      args = self._args
>      if args.mailmap:

As noted above, just as --message-callback affects both commit and tag
messages, shouldn't this option affect both (i.e. should there also be
a section in tweak_tag() similar to the one you added to
tweak_commit())?

> diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh
> index 3f567e7..6d2d985 100755
> --- a/t/t9390-filter-repo.sh
> +++ b/t/t9390-filter-repo.sh
> @@ -39,6 +39,7 @@ filter_testcase basic basic-filename --invert-paths --path-glob 't*en*'
>  filter_testcase basic basic-numbers  --invert-paths --path-regex 'f.*e.*e'
>  filter_testcase basic basic-mailmap  --mailmap ../t9390/sample-mailmap
>  filter_testcase basic basic-replace  --replace-text ../t9390/sample-replace
> +filter_testcase basic basic-message  --replace-message ../t9390/sample-message
>  filter_testcase empty empty-keepme   --path keepme
>  filter_testcase empty more-empty-keepme --path keepme --prune-empty=always \
>                                                    --prune-degenerate=always
> diff --git a/t/t9390/basic-message b/t/t9390/basic-message
> new file mode 100644
> index 0000000..4ac1968
> --- /dev/null
> +++ b/t/t9390/basic-message
> @@ -0,0 +1,78 @@
> +feature done
> +blob
> +mark :1
> +data 8
> +initial
> +
> +reset refs/heads/B
> +commit refs/heads/B
> +mark :2
> +author Little O. Me <me@little.net> 1535228562 -0700
> +committer Little O. Me <me@little.net> 1535228562 -0700
> +data 9
> +Modified
> +M 100644 :1 filename
> +M 100644 :1 ten
> +M 100644 :1 twenty
> +
> +blob
> +mark :3
> +data 11
> +twenty-mod
> +
> +commit refs/heads/B
> +mark :4
> +author Little 'ol Me <me@laptop.(none)> 1535229544 -0700
> +committer Little 'ol Me <me@laptop.(none)> 1535229544 -0700
> +data 18
> +add the number 20
> +from :2
> +M 100644 :3 twenty
> +
> +blob
> +mark :5
> +data 8
> +ten-mod
> +
> +commit refs/heads/A
> +mark :6
> +author Little O. Me <me@machine52.little.net> 1535229523 -0700
> +committer Little O. Me <me@machine52.little.net> 1535229523 -0700
> +data 8
> +add ten
> +from :2
> +M 100644 :5 ten
> +
> +commit refs/heads/master
> +mark :7
> +author Lit.e Me <me@fire.com> 1535229559 -0700
> +committer Lit.e Me <me@fire.com> 1535229580 -0700
> +data 24
> +Merge branch 'A' into B
> +from :4
> +merge :6
> +M 100644 :5 ten
> +
> +blob
> +mark :8
> +data 6
> +final
> +
> +commit refs/heads/master
> +mark :9
> +author Little Me <me@bigcompany.com> 1535229601 -0700
> +committer Little Me <me@bigcompany.com> 1535229601 -0700
> +data 9
> +whatever
> +from :7
> +M 100644 :8 filename
> +M 100644 :8 ten
> +M 100644 :8 twenty
> +
> +tag v1.0
> +from :9
> +tagger Little John <second@merry.men> 1535229618 -0700
> +data 5
> +v1.0
> +
> +done
> diff --git a/t/t9390/sample-message b/t/t9390/sample-message
> new file mode 100644
> index 0000000..a374d61
> --- /dev/null
> +++ b/t/t9390/sample-message
> @@ -0,0 +1,2 @@
> +Initial==>Modified
> +regex:tw.nty==>the number 20
> --
> 2.32.0

Testcase looks good.

Thanks for sending this along; if you fix up the issues I pointed out,
I'd be happy to apply this to git-filter-repo.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [filter-repo PATCH] filter-repo: add new --replace-message option
  2021-08-23 17:34 ` Elijah Newren
@ 2021-08-23 20:53   ` Gwyneth Morgan
  0 siblings, 0 replies; 5+ messages in thread
From: Gwyneth Morgan @ 2021-08-23 20:53 UTC (permalink / raw)
  To: Elijah Newren; +Cc: Git Mailing List

On 2021-08-23 10:34:07-0700, Elijah Newren wrote:
> Hi,
> 
> On Tue, Aug 17, 2021 at 9:38 PM Gwyneth Morgan <gwymor@tilde.club> wrote:
> >
> > Like --replace-text, add an option --replace-message which replaces text
> > in commit message bodies, so that users can easily replace text without
> > constructing a --message-callback.
> 
> Interesting idea.
> 
> Missing a Signed-off-by trailer.

Will fix.

> > @@ -894,7 +898,20 @@ YYYY-MM-DD.  In the expressions file, there are a few things to note:
> >      beginning and ends of lines rather than the beginning and end of file.
> >      See https://docs.python.org/3/library/re.html for details.
> >
> > -See also the `--blob-callback` from <<CALLBACKS>>.
> > +See also the `--blob-callback` from <<CALLBACKS>>.  Similarly, if you
> > +want to modify commit messages, you can do so with the same syntax.  For
> > +example, with a file named expressions.txt containing
> > +
> > +--------------------------------------------------
> > +foo==>bar
> > +--------------------------------------------------
> > +
> > +then running
> > +--------------------------------------------------
> > +git filter-repo --replace-message expressions.txt
> > +--------------------------------------------------
> > +
> > +will replace `foo` in commit messages with `bar`.
> 
> You've added this text to the "Content based filtering" section of the
> manual, which doesn't make sense.  It should go in a section about
> updating commit/tag messages.

Ah, got it. I'll move that into a new section.

> >      if self._message_callback:
> >        commit.message = self._message_callback(commit.message)
> > -
> 
> Why this stray line removal?

That was accidental. Will fix.

> >      # Change the author & committer according to mailmap rules
> >      args = self._args
> >      if args.mailmap:
> 
> As noted above, just as --message-callback affects both commit and tag
> messages, shouldn't this option affect both (i.e. should there also be
> a section in tweak_tag() similar to the one you added to
> tweak_commit())?

Yes, it should. I'll change that.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [filter-repo PATCH v2] filter-repo: add new --replace-message option
  2021-08-18  4:37 [filter-repo PATCH] filter-repo: add new --replace-message option Gwyneth Morgan
  2021-08-23 17:34 ` Elijah Newren
@ 2021-08-23 20:55 ` Gwyneth Morgan
  2021-08-25 15:08   ` Elijah Newren
  1 sibling, 1 reply; 5+ messages in thread
From: Gwyneth Morgan @ 2021-08-23 20:55 UTC (permalink / raw)
  To: git; +Cc: Gwyneth Morgan, Elijah Newren

Like --replace-text, add an option --replace-message which replaces text
in commit/tag message bodies, so that users can easily replace text
without constructing a --message-callback.

Signed-off-by: Gwyneth Morgan <gwymor@tilde.club>
---
 Documentation/git-filter-repo.txt | 25 ++++++++++
 git-filter-repo                   | 16 +++++++
 t/t9390-filter-repo.sh            |  1 +
 t/t9390/basic-message             | 78 +++++++++++++++++++++++++++++++
 t/t9390/sample-message            |  4 ++
 5 files changed, 124 insertions(+)
 create mode 100644 t/t9390/basic-message
 create mode 100644 t/t9390/sample-message

diff --git a/Documentation/git-filter-repo.txt b/Documentation/git-filter-repo.txt
index 2798378..1e590c7 100644
--- a/Documentation/git-filter-repo.txt
+++ b/Documentation/git-filter-repo.txt
@@ -181,6 +181,11 @@ Renaming of refs (see also --refname-callback)
 Filtering of commit messages (see also --message-callback)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+--replace-message <expressions_file>::
+	A file with expressions that, if found in commit or tag
+	messages, will be replaced. This file uses the same syntax as
+	--replace-text.
+
 --preserve-commit-hashes::
 	By default, since commits are rewritten and thus gain new
 	hashes, references to old commit hashes in commit messages are
@@ -896,6 +901,26 @@ YYYY-MM-DD.  In the expressions file, there are a few things to note:
 
 See also the `--blob-callback` from <<CALLBACKS>>.
 
+Updating commit/tag messages
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+If you want to modify commit or tag messages, you can do so with the
+same syntax as `--replace-text`, explained above.  For example, with a
+file named expressions.txt containing
+
+--------------------------------------------------
+foo==>bar
+--------------------------------------------------
+
+then running
+--------------------------------------------------
+git filter-repo --replace-message expressions.txt
+--------------------------------------------------
+
+will replace `foo` in commit or tag messages with `bar`.
+
+See also the `--message-callback` from <<CALLBACKS>>.
+
 Refname based filtering
 ~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/git-filter-repo b/git-filter-repo
index b91bd96..5e726c9 100755
--- a/git-filter-repo
+++ b/git-filter-repo
@@ -1843,6 +1843,10 @@ EXAMPLES
 
     messages = parser.add_argument_group(title=_("Filtering of commit messages "
                                                "(see also --message-callback)"))
+    messages.add_argument('--replace-message', metavar='EXPRESSIONS_FILE',
+        help=_("A file with expressions that, if found in commit messages, "
+               "will be replaced. This file uses the same syntax as "
+               "--replace-text."))
     messages.add_argument('--preserve-commit-hashes', action='store_true',
         help=_("By default, since commits are rewritten and thus gain new "
                "hashes, references to old commit hashes in commit messages "
@@ -2189,6 +2193,8 @@ EXAMPLES
       args.mailmap = MailmapInfo(args.mailmap)
     if args.replace_text:
       args.replace_text = FilteringOptions.get_replace_text(args.replace_text)
+    if args.replace_message:
+      args.replace_message = FilteringOptions.get_replace_text(args.replace_message)
     if args.strip_blobs_with_ids:
       with open(args.strip_blobs_with_ids, 'br') as f:
         args.strip_blobs_with_ids = set(f.read().split())
@@ -3374,6 +3380,11 @@ class RepoFilter(object):
     if not self._args.preserve_commit_hashes:
       commit.message = self._hash_re.sub(self._translate_commit_hash,
                                          commit.message)
+    if self._args.replace_message:
+      for literal, replacement in self._args.replace_message['literals']:
+        commit.message = commit.message.replace(literal, replacement)
+      for regex,   replacement in self._args.replace_message['regexes']:
+        commit.message = regex.sub(replacement, commit.message)
     if self._message_callback:
       commit.message = self._message_callback(commit.message)
 
@@ -3474,6 +3485,11 @@ class RepoFilter(object):
 
   def _tweak_tag(self, tag):
     # Tweak the tag message according to callbacks
+    if self._args.replace_message:
+      for literal, replacement in self._args.replace_message['literals']:
+        tag.message = tag.message.replace(literal, replacement)
+      for regex,   replacement in self._args.replace_message['regexes']:
+        tag.message = regex.sub(replacement, tag.message)
     if self._message_callback:
       tag.message = self._message_callback(tag.message)
 
diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh
index 3f567e7..6d2d985 100755
--- a/t/t9390-filter-repo.sh
+++ b/t/t9390-filter-repo.sh
@@ -39,6 +39,7 @@ filter_testcase basic basic-filename --invert-paths --path-glob 't*en*'
 filter_testcase basic basic-numbers  --invert-paths --path-regex 'f.*e.*e'
 filter_testcase basic basic-mailmap  --mailmap ../t9390/sample-mailmap
 filter_testcase basic basic-replace  --replace-text ../t9390/sample-replace
+filter_testcase basic basic-message  --replace-message ../t9390/sample-message
 filter_testcase empty empty-keepme   --path keepme
 filter_testcase empty more-empty-keepme --path keepme --prune-empty=always \
 		                                   --prune-degenerate=always
diff --git a/t/t9390/basic-message b/t/t9390/basic-message
new file mode 100644
index 0000000..5b6b41b
--- /dev/null
+++ b/t/t9390/basic-message
@@ -0,0 +1,78 @@
+feature done
+blob
+mark :1
+data 8
+initial
+
+reset refs/heads/B
+commit refs/heads/B
+mark :2
+author Little O. Me <me@little.net> 1535228562 -0700
+committer Little O. Me <me@little.net> 1535228562 -0700
+data 9
+Modified
+M 100644 :1 filename
+M 100644 :1 ten
+M 100644 :1 twenty
+
+blob
+mark :3
+data 11
+twenty-mod
+
+commit refs/heads/B
+mark :4
+author Little 'ol Me <me@laptop.(none)> 1535229544 -0700
+committer Little 'ol Me <me@laptop.(none)> 1535229544 -0700
+data 18
+add the number 20
+from :2
+M 100644 :3 twenty
+
+blob
+mark :5
+data 8
+ten-mod
+
+commit refs/heads/A
+mark :6
+author Little O. Me <me@machine52.little.net> 1535229523 -0700
+committer Little O. Me <me@machine52.little.net> 1535229523 -0700
+data 8
+add ten
+from :2
+M 100644 :5 ten
+
+commit refs/heads/master
+mark :7
+author Lit.e Me <me@fire.com> 1535229559 -0700
+committer Lit.e Me <me@fire.com> 1535229580 -0700
+data 24
+Merge branch 'A' into B
+from :4
+merge :6
+M 100644 :5 ten
+
+blob
+mark :8
+data 6
+final
+
+commit refs/heads/master
+mark :9
+author Little Me <me@bigcompany.com> 1535229601 -0700
+committer Little Me <me@bigcompany.com> 1535229601 -0700
+data 9
+whatever
+from :7
+M 100644 :8 filename
+M 100644 :8 ten
+M 100644 :8 twenty
+
+tag v1.0
+from :9
+tagger Little John <second@merry.men> 1535229618 -0700
+data 15
+version one :)
+
+done
diff --git a/t/t9390/sample-message b/t/t9390/sample-message
new file mode 100644
index 0000000..0412c3c
--- /dev/null
+++ b/t/t9390/sample-message
@@ -0,0 +1,4 @@
+Initial==>Modified
+regex:tw.nty==>the number 20
+v1.0==>version one!
+regex:!$==> :)
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [filter-repo PATCH v2] filter-repo: add new --replace-message option
  2021-08-23 20:55 ` [filter-repo PATCH v2] " Gwyneth Morgan
@ 2021-08-25 15:08   ` Elijah Newren
  0 siblings, 0 replies; 5+ messages in thread
From: Elijah Newren @ 2021-08-25 15:08 UTC (permalink / raw)
  To: Gwyneth Morgan; +Cc: Git Mailing List

On Mon, Aug 23, 2021 at 1:58 PM Gwyneth Morgan <gwymor@tilde.club> wrote:
>
> Like --replace-text, add an option --replace-message which replaces text
> in commit/tag message bodies, so that users can easily replace text
> without constructing a --message-callback.
>
> Signed-off-by: Gwyneth Morgan <gwymor@tilde.club>
> ---
>  Documentation/git-filter-repo.txt | 25 ++++++++++
>  git-filter-repo                   | 16 +++++++
>  t/t9390-filter-repo.sh            |  1 +
>  t/t9390/basic-message             | 78 +++++++++++++++++++++++++++++++
>  t/t9390/sample-message            |  4 ++
>  5 files changed, 124 insertions(+)
>  create mode 100644 t/t9390/basic-message
>  create mode 100644 t/t9390/sample-message
>
> diff --git a/Documentation/git-filter-repo.txt b/Documentation/git-filter-repo.txt
> index 2798378..1e590c7 100644
> --- a/Documentation/git-filter-repo.txt
> +++ b/Documentation/git-filter-repo.txt
> @@ -181,6 +181,11 @@ Renaming of refs (see also --refname-callback)
>  Filtering of commit messages (see also --message-callback)
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> +--replace-message <expressions_file>::
> +       A file with expressions that, if found in commit or tag
> +       messages, will be replaced. This file uses the same syntax as
> +       --replace-text.
> +
>  --preserve-commit-hashes::
>         By default, since commits are rewritten and thus gain new
>         hashes, references to old commit hashes in commit messages are
> @@ -896,6 +901,26 @@ YYYY-MM-DD.  In the expressions file, there are a few things to note:
>
>  See also the `--blob-callback` from <<CALLBACKS>>.
>
> +Updating commit/tag messages
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +If you want to modify commit or tag messages, you can do so with the
> +same syntax as `--replace-text`, explained above.  For example, with a
> +file named expressions.txt containing
> +
> +--------------------------------------------------
> +foo==>bar
> +--------------------------------------------------
> +
> +then running
> +--------------------------------------------------
> +git filter-repo --replace-message expressions.txt
> +--------------------------------------------------
> +
> +will replace `foo` in commit or tag messages with `bar`.
> +
> +See also the `--message-callback` from <<CALLBACKS>>.
> +
>  Refname based filtering
>  ~~~~~~~~~~~~~~~~~~~~~~~
>
> diff --git a/git-filter-repo b/git-filter-repo
> index b91bd96..5e726c9 100755
> --- a/git-filter-repo
> +++ b/git-filter-repo
> @@ -1843,6 +1843,10 @@ EXAMPLES
>
>      messages = parser.add_argument_group(title=_("Filtering of commit messages "
>                                                 "(see also --message-callback)"))
> +    messages.add_argument('--replace-message', metavar='EXPRESSIONS_FILE',
> +        help=_("A file with expressions that, if found in commit messages, "
> +               "will be replaced. This file uses the same syntax as "
> +               "--replace-text."))
>      messages.add_argument('--preserve-commit-hashes', action='store_true',
>          help=_("By default, since commits are rewritten and thus gain new "
>                 "hashes, references to old commit hashes in commit messages "
> @@ -2189,6 +2193,8 @@ EXAMPLES
>        args.mailmap = MailmapInfo(args.mailmap)
>      if args.replace_text:
>        args.replace_text = FilteringOptions.get_replace_text(args.replace_text)
> +    if args.replace_message:
> +      args.replace_message = FilteringOptions.get_replace_text(args.replace_message)
>      if args.strip_blobs_with_ids:
>        with open(args.strip_blobs_with_ids, 'br') as f:
>          args.strip_blobs_with_ids = set(f.read().split())
> @@ -3374,6 +3380,11 @@ class RepoFilter(object):
>      if not self._args.preserve_commit_hashes:
>        commit.message = self._hash_re.sub(self._translate_commit_hash,
>                                           commit.message)
> +    if self._args.replace_message:
> +      for literal, replacement in self._args.replace_message['literals']:
> +        commit.message = commit.message.replace(literal, replacement)
> +      for regex,   replacement in self._args.replace_message['regexes']:
> +        commit.message = regex.sub(replacement, commit.message)
>      if self._message_callback:
>        commit.message = self._message_callback(commit.message)
>
> @@ -3474,6 +3485,11 @@ class RepoFilter(object):
>
>    def _tweak_tag(self, tag):
>      # Tweak the tag message according to callbacks
> +    if self._args.replace_message:
> +      for literal, replacement in self._args.replace_message['literals']:
> +        tag.message = tag.message.replace(literal, replacement)
> +      for regex,   replacement in self._args.replace_message['regexes']:
> +        tag.message = regex.sub(replacement, tag.message)
>      if self._message_callback:
>        tag.message = self._message_callback(tag.message)
>
> diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh
> index 3f567e7..6d2d985 100755
> --- a/t/t9390-filter-repo.sh
> +++ b/t/t9390-filter-repo.sh
> @@ -39,6 +39,7 @@ filter_testcase basic basic-filename --invert-paths --path-glob 't*en*'
>  filter_testcase basic basic-numbers  --invert-paths --path-regex 'f.*e.*e'
>  filter_testcase basic basic-mailmap  --mailmap ../t9390/sample-mailmap
>  filter_testcase basic basic-replace  --replace-text ../t9390/sample-replace
> +filter_testcase basic basic-message  --replace-message ../t9390/sample-message
>  filter_testcase empty empty-keepme   --path keepme
>  filter_testcase empty more-empty-keepme --path keepme --prune-empty=always \
>                                                    --prune-degenerate=always
> diff --git a/t/t9390/basic-message b/t/t9390/basic-message
> new file mode 100644
> index 0000000..5b6b41b
> --- /dev/null
> +++ b/t/t9390/basic-message
> @@ -0,0 +1,78 @@
> +feature done
> +blob
> +mark :1
> +data 8
> +initial
> +
> +reset refs/heads/B
> +commit refs/heads/B
> +mark :2
> +author Little O. Me <me@little.net> 1535228562 -0700
> +committer Little O. Me <me@little.net> 1535228562 -0700
> +data 9
> +Modified
> +M 100644 :1 filename
> +M 100644 :1 ten
> +M 100644 :1 twenty
> +
> +blob
> +mark :3
> +data 11
> +twenty-mod
> +
> +commit refs/heads/B
> +mark :4
> +author Little 'ol Me <me@laptop.(none)> 1535229544 -0700
> +committer Little 'ol Me <me@laptop.(none)> 1535229544 -0700
> +data 18
> +add the number 20
> +from :2
> +M 100644 :3 twenty
> +
> +blob
> +mark :5
> +data 8
> +ten-mod
> +
> +commit refs/heads/A
> +mark :6
> +author Little O. Me <me@machine52.little.net> 1535229523 -0700
> +committer Little O. Me <me@machine52.little.net> 1535229523 -0700
> +data 8
> +add ten
> +from :2
> +M 100644 :5 ten
> +
> +commit refs/heads/master
> +mark :7
> +author Lit.e Me <me@fire.com> 1535229559 -0700
> +committer Lit.e Me <me@fire.com> 1535229580 -0700
> +data 24
> +Merge branch 'A' into B
> +from :4
> +merge :6
> +M 100644 :5 ten
> +
> +blob
> +mark :8
> +data 6
> +final
> +
> +commit refs/heads/master
> +mark :9
> +author Little Me <me@bigcompany.com> 1535229601 -0700
> +committer Little Me <me@bigcompany.com> 1535229601 -0700
> +data 9
> +whatever
> +from :7
> +M 100644 :8 filename
> +M 100644 :8 ten
> +M 100644 :8 twenty
> +
> +tag v1.0
> +from :9
> +tagger Little John <second@merry.men> 1535229618 -0700
> +data 15
> +version one :)
> +
> +done
> diff --git a/t/t9390/sample-message b/t/t9390/sample-message
> new file mode 100644
> index 0000000..0412c3c
> --- /dev/null
> +++ b/t/t9390/sample-message
> @@ -0,0 +1,4 @@
> +Initial==>Modified
> +regex:tw.nty==>the number 20
> +v1.0==>version one!
> +regex:!$==> :)
> --
> 2.33.0

Applied; thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-25 15:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-18  4:37 [filter-repo PATCH] filter-repo: add new --replace-message option Gwyneth Morgan
2021-08-23 17:34 ` Elijah Newren
2021-08-23 20:53   ` Gwyneth Morgan
2021-08-23 20:55 ` [filter-repo PATCH v2] " Gwyneth Morgan
2021-08-25 15:08   ` Elijah Newren

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).