Filter-Repo
In other answer it was suggested to save code to file .py and run it with
git filter-repo --commit-callback "$(cat ../<your file name>.py)"
but then it ignored in my case all whitespaces which in python breaks syntax. For me worked pasting it directly into command. I am using Windows, tested on PowerShell, beginner in these things.
More advanced example
Set new date for commits based on commits containing given string. But BEWARE, you shouldn't change date unless you have really good reason!
if(b'[structural-programming]' in commit.message):
base_date = datetime(2021,1,1,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
elif(b'[intro-into-programming]' in commit.message):
base_date = datetime(2021,1,2,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
else:
base_date = datetime(2021,1,3,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
and how it is used in console:
git filter-repo --commit-callback "
[seconds, timezone] = commit.committer_date.decode('utf-8').split()
if(b'[structural-programming]' in commit.message):
base_date = datetime(2021,1,1,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
elif(b'[intro-into-programming]' in commit.message):
base_date = datetime(2021,1,2,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
else:
base_date = datetime(2021,1,3,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
"