Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

namespace GitRewrite.CleanupTask
{
class RewriteContributerTask : CleanupTaskBase<Commit>
class RewriteContributorTask : CleanupTaskBase<Commit>
{
private readonly Dictionary<string, string> _contributerMappings = new Dictionary<string, string>();
private readonly Dictionary<string, string> _contributorMappings = new Dictionary<string, string>();

public RewriteContributerTask(string repositoryPath, string contributerMappingFile) : base(repositoryPath)
public RewriteContributorTask(string repositoryPath, string contributorMappingFile) : base(repositoryPath)
{
var contributerMappingLines = File.ReadAllLines(contributerMappingFile);
var contributorMappingLines = File.ReadAllLines(contributorMappingFile);

foreach (var line in contributerMappingLines)
foreach (var line in contributorMappingLines)
{
var contributerMapping = line.Split('=').Select(x => x.Trim()).ToList();
if (contributerMapping.Count != 2)
var contributorMapping = line.Split('=').Select(x => x.Trim()).ToList();
if (contributorMapping.Count != 2)
throw new ArgumentException("Mapping is not formatted properly.");

_contributerMappings.Add(contributerMapping[0], contributerMapping[1]);
_contributorMappings.Add(contributorMapping[0], contributorMapping[1]);
}
}

Expand All @@ -29,7 +29,7 @@ public RewriteContributerTask(string repositoryPath, string contributerMappingFi
protected override void SynchronousStep(Commit commit)
{
var rewrittenParentHashes = GetRewrittenCommitHashes(commit.Parents);
var changedCommit = commit.WithChangedContributer(_contributerMappings, rewrittenParentHashes);
var changedCommit = commit.WithChangedContributor(_contributorMappings, rewrittenParentHashes);

var resultBytes = GitObjectFactory.GetBytesWithHeader(GitObjectType.Commit, changedCommit);
var newCommitHash = new ObjectHash(Hash.Create(resultBytes));
Expand Down
33 changes: 16 additions & 17 deletions GitRewrite/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private CommandLineOptions()
{
}

public bool ListContributerNames { get; private set; }
public bool ListContributorNames { get; private set; }

public bool ShowHelp { get; private set; }

Expand All @@ -24,7 +24,7 @@ private CommandLineOptions()

public bool RemoveEmptyCommits { get; private set; }

public string ContributerMappingFile { get; private set; }
public string ContributorMappingFile { get; private set; }

public bool ProtectRefs { get; private set; }

Expand All @@ -33,7 +33,7 @@ internal static bool TryParse(string[] args, out CommandLineOptions options)
options = new CommandLineOptions();
var deleteFilesStarted = false;
var deleteFoldersStarted = false;
var rewriteContributersFileExpected = false;
var rewriteContributorsFileExpected = false;

foreach (var arg in args)
if (deleteFilesStarted)
Expand All @@ -46,10 +46,10 @@ internal static bool TryParse(string[] args, out CommandLineOptions options)
options.FoldersToDelete.AddRange(GetFiles(arg));
deleteFoldersStarted = false;
}
else if (rewriteContributersFileExpected)
else if (rewriteContributorsFileExpected)
{
options.ContributerMappingFile = arg;
rewriteContributersFileExpected = false;
options.ContributorMappingFile = arg;
rewriteContributorsFileExpected = false;
}
else
{
Expand All @@ -73,11 +73,11 @@ internal static bool TryParse(string[] args, out CommandLineOptions options)
case "--help":
options.ShowHelp = true;
break;
case "--contributer-names":
options.ListContributerNames = true;
case "--contributor-names":
options.ListContributorNames = true;
break;
case "--rewrite-contributers":
rewriteContributersFileExpected = true;
case "--rewrite-contributors":
rewriteContributorsFileExpected = true;
break;
case "--protect-refs":
options.ProtectRefs = true;
Expand All @@ -99,8 +99,8 @@ internal static bool TryParse(string[] args, out CommandLineOptions options)
optionsSet += options.FixTrees ? 1 : 0;
optionsSet += options.FilesToDelete.Any() || options.FoldersToDelete.Any() ? 1 : 0;
optionsSet += options.RemoveEmptyCommits ? 1 : 0;
optionsSet += options.ListContributerNames ? 1 : 0;
optionsSet += !string.IsNullOrWhiteSpace(options.ContributerMappingFile) ? 1 : 0;
optionsSet += options.ListContributorNames ? 1 : 0;
optionsSet += !string.IsNullOrWhiteSpace(options.ContributorMappingFile) ? 1 : 0;

if (optionsSet > 1)
{
Expand Down Expand Up @@ -150,15 +150,14 @@ public static void PrintHelp()
Console.WriteLine(" Can be combined with deleting files.");
Console.WriteLine();


Console.WriteLine("--rewrite-contributers [contributers.txt]");
Console.WriteLine("--rewrite-contributors [contributors.txt]");
Console.WriteLine(" Rewrite author and committer information.");
Console.WriteLine(" contributers.txt is the mapping file for the names that should be replaced. Each line represents one contributer to replace.");
Console.WriteLine(" contributors.txt is the mapping file for the names that should be replaced. Each line represents one contributor to replace.");
Console.WriteLine(" Format is ");
Console.WriteLine(" Test User <test@user.com> = New Test User <newtest@user.comm>");
Console.WriteLine();

Console.WriteLine("--contributer-names");
Console.WriteLine("--contributor-names");
Console.WriteLine(" Writes all authors and committers to stdout");
Console.WriteLine();

Expand Down Expand Up @@ -194,4 +193,4 @@ private static List<string> GetFiles(string fileString)
return result;
}
}
}
}
18 changes: 9 additions & 9 deletions GitRewrite/GitObjects/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public Commit(ObjectHash hash, byte[] bytes) : base(hash, GitObjectType.Commit)

private static readonly byte[] PgpSignatureEnd = "-----END PGP SIGNATURE-----".Select(c => (byte) c).ToArray();

public ReadOnlySpan<byte> GetCommitterBytes() => GetContributerName(_committerLine.Slice(10));
public ReadOnlySpan<byte> GetCommitterBytes() => GetContributorName(_committerLine.Slice(10));

public string GetCommitterName() => Encoding.UTF8.GetString(GetCommitterBytes());

public ReadOnlySpan<byte> GetAuthorBytes() => GetContributerName(_authorLine.Slice(7));
public ReadOnlySpan<byte> GetAuthorBytes() => GetContributorName(_authorLine.Slice(7));

public string GetAuthorName() => Encoding.UTF8.GetString(GetAuthorBytes());

private ReadOnlySpan<byte> GetContributerName(in ReadOnlyMemory<byte> contributerWithTime)
private ReadOnlySpan<byte> GetContributorName(in ReadOnlyMemory<byte> contributorWithTime)
{
var span = contributerWithTime.Span;
var span = contributorWithTime.Span;
int spaces = 0;
int index = 0;
for (int i = contributerWithTime.Length - 1; i >= 0; i--)
for (int i = contributorWithTime.Length - 1; i >= 0; i--)
{
if (span[i] == ' ' && ++spaces == 2)
{
Expand All @@ -83,7 +83,7 @@ private ReadOnlySpan<byte> GetContributerName(in ReadOnlyMemory<byte> contribute
}
}

return contributerWithTime.Span.Slice(0, index);
return contributorWithTime.Span.Slice(0, index);
}

public ObjectHash TreeHash => new ObjectHash(_treeHash.Span.Slice(5));
Expand Down Expand Up @@ -145,17 +145,17 @@ public static byte[] GetSerializedCommitWithChangedTreeAndParents(Commit commit,
return resultBuffer;
}

public byte[] WithChangedContributer(Dictionary<string, string> contributerMapping, IEnumerable<ObjectHash> parents)
public byte[] WithChangedContributor(Dictionary<string, string> contributorMapping, IEnumerable<ObjectHash> parents)
{
const int firstLineLength = 46;
const int parentLineLength = 7 + 40 + 1;

var author = GetAuthorName();
var committer = GetCommitterName();
if (!contributerMapping.TryGetValue(author, out var newAuthor))
if (!contributorMapping.TryGetValue(author, out var newAuthor))
newAuthor = author;

if (!contributerMapping.TryGetValue(this.GetCommitterName(), out var newCommitter))
if (!contributorMapping.TryGetValue(this.GetCommitterName(), out var newCommitter))
newCommitter = committer;

var authorLine = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(this._authorLine.Span).Replace(author, newAuthor));
Expand Down
12 changes: 6 additions & 6 deletions GitRewrite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ static void Main(string[] args)
using (var removeEmptyCommitsTask = new RemoveEmptyCommitsTask(options.RepositoryPath))
removeEmptyCommitsTask.Run();
}
else if (!string.IsNullOrWhiteSpace(options.ContributerMappingFile))
else if (!string.IsNullOrWhiteSpace(options.ContributorMappingFile))
{
using (var rewriteContributerTask = new RewriteContributerTask(options.RepositoryPath, options.ContributerMappingFile))
rewriteContributerTask.Run();
using (var rewriteContributorTask = new RewriteContributorTask(options.RepositoryPath, options.ContributorMappingFile))
rewriteContributorTask.Run();
}
else if (options.ListContributerNames)
else if (options.ListContributorNames)
{
foreach (var contributer in CommitWalker.CommitsRandomOrder(options.RepositoryPath)
foreach (var contributor in CommitWalker.CommitsRandomOrder(options.RepositoryPath)
.SelectMany(commit => new[] {commit.GetAuthorName(), commit.GetCommitterName()})
.Distinct()
.AsParallel()
.OrderBy(x => x))
Console.WriteLine(contributer);
Console.WriteLine(contributor);
}
}

Expand Down