1

I'm using git filter-repo to rewrite the emails of the commit authors on a git repository.

The commit.author_email field is in this format:

AA01\myuserid

Yes, I know that this does not resemble an email address but it is what it is. What I need to achieve is setting the commit author's email to the second part of the commit.author_email field (e.g. myuserid)

This is the command I'm executing to split

git filter-repo --force --refs master --commit-callback '
>>   commit.author_email_split = commit.author_email.split("\\")
>> '

For now, I just want to let the split work but it fails with the error below:

Traceback (most recent call last):
  File "C:\git-filter-repo\git-filter-repo", line 4005, in <module>
    main()
  File "C:\git-filter-repo\git-filter-repo", line 4001, in main
    filter = RepoFilter(args)
  File "C:\git-filter-repo\git-filter-repo", line 2760, in __init__
    self._handle_arg_callbacks()
  File "C:\git-filter-repo\git-filter-repo", line 2865, in _handle_arg_callbacks
    handle('commit')
  File "C:\git-filter-repo\git-filter-repo", line 2858, in handle
    setattr(self, callback_field, make_callback(type, code_string))
  File "C:\git-filter-repo\git-filter-repo", line 2841, in make_callback
    '  '+'\n  '.join(str.splitlines()), globals())
  File "<string>", line 3
    commit.author_email_split = commit.author_email.split(\)
                                                           ^
SyntaxError: unexpected character after line continuation character

I tried checking an re-checking the command syntax, the presence of hidden chars and so on but nothing works. What can be the problem here?

gvdm
  • 3,006
  • 5
  • 35
  • 73
  • 2
    What shell are you entering that command in? It looks to me like your shell uses different escaping/quoting rules than the common bourne shell/bash (which most "shell" usage examples assume you use). Are you perchance typing this in PowerShell? – Joachim Sauer Mar 29 '23 at 16:10
  • Yes, I'm using Powershell (good guess!). Is it a problem? I will try with git bash – gvdm Mar 30 '23 at 07:04
  • I see that you already figured out a solution. Basically, PowerShell should be just as capable as other shells at executing this command, but it's rules about how to quote command argument (so that the shell doesn't try to interpret them) are different from a plain old bash/bourne shell, so you need to adopt the samples you find. And I personally don't know the precise rules of PowerShell, so I can't offer guidance here either. Switching to git bash is definitely a simple workaround, because most of the samples out there are written for bash. – Joachim Sauer Mar 30 '23 at 08:03

2 Answers2

1

My problem was that I was using Powershell and this was causing problems with the line continuation char (thanks Joachim Sauer for the tip, very useful!).

After fixing that problem, Python was complaining with the error %b requires a bytes-like object, or an object that implements __bytes__, not 'str', so I also added encode() and decode(). You can find the final solution that works in git bash (and I think in any bash-like prompt).

git filter-repo --force --refs master --commit-callback '
  commit.committer_email = commit.committer_email.decode().split("\\")[1].encode()
  commit.author_email = commit.author_email.decode().split("\\")[1].encode()
  if commit.author_name:
    commit.author_name = commit.author_email
  if commit.committer_name:
    commit.committer_name = commit.committer_email
'
gvdm
  • 3,006
  • 5
  • 35
  • 73
0

Here's a workaround for your problem, which should suffice if your goal is:

to rewrite the emails of the commit authors on a git repository.

  1. First get a list of all emails in your repo.

  2. Create a .mailmap file, and list all of the new emails followed by their old emails (which should be easy to do quickly in most editors, even if you have many emails to update). You can test your .mailmap file with git log.

  3. Now you can use the --mailmap option to git filter-repo to make the changes permanent.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • Hello. Thanks, but my approach is more easy and fast because you don't have to manually create the email mappings and all – gvdm Mar 30 '23 at 07:05
  • @gvdm I agree. I just mentioned this as a workaround because both are one time setup work, so in case you spend more than half an hour doing it the "easy" way, you have a fallback of spending 5-10 minutes doing it the this way. – TTT Mar 30 '23 at 13:36