Skip to content

Commit

Permalink
Merge pull request #16 from Techsola/unblock_inner_renames
Browse files Browse the repository at this point in the history
Finish implementing branch rename support
  • Loading branch information
jnm2 committed May 8, 2023
2 parents b3d389e + eab163e commit 3edec49
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
13 changes: 12 additions & 1 deletion src/TfvcMigrator/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public static string GetLeaf(string path)
return index == -1 ? path : path[(index + 1)..];
}

/// <summary>
/// If <paramref name="path"/> is or starts with <paramref name="containingPath"/>, <paramref
/// name="containingPath"/> is replaced with <paramref name="newContainingPath"/> in the returned path. Otherwise,
/// the unmodified <paramref name="path"/> is returned.
/// </summary>
/// <exception cref="ArgumentException">Thrown if any of the paths ends with a trailing slash.</exception>
public static string ReplaceContainingPath(string path, string containingPath, string newContainingPath)
{
if (path.EndsWith('/'))
Expand All @@ -65,11 +71,16 @@ public static string ReplaceContainingPath(string path, string containingPath, s
throw new ArgumentException("Path should not end with a trailing slash.", nameof(newContainingPath));

if (!IsOrContains(containingPath, path))
throw new ArgumentException("The specified containing path does not contain the specified path.");
return path;

return newContainingPath + path[containingPath.Length..];
}

/// <summary>
/// If <paramref name="path"/> is or starts with <paramref name="containingPath"/>, the relative part of the path
/// (if any) is returned. Otherwise, <see cref="ArgumentException"/> is thrown.
/// </summary>
/// <exception cref="ArgumentException">Thrown if any of the paths ends with a trailing slash.</exception>
public static string RemoveContainingPath(string path, string containingPath)
{
if (path.EndsWith('/'))
Expand Down
10 changes: 8 additions & 2 deletions src/TfvcMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private static async IAsyncEnumerable<MappingState> EnumerateMappingStatesAsync(
var mapping = branchMappings[branch.SourceBranch];

mapping = PathUtils.IsOrContains(branch.SourceBranchPath, mapping.RootDirectory)
? mapping.RenameRootDirectory(branch.SourceBranchPath, branch.NewBranch.Path)
? mapping.ApplyRename(branch.SourceBranchPath, branch.NewBranch.Path)
: mapping.WithSubdirectoryMapping(branch.NewBranch.Path, branch.SourceBranchPath);

branchMappings.Add(branch.NewBranch, mapping);
Expand All @@ -505,7 +505,13 @@ private static async IAsyncEnumerable<MappingState> EnumerateMappingStatesAsync(
case RenameOperation rename:
{
if (!branchMappings.Remove(rename.OldIdentity, out var mapping)) throw new NotImplementedException();
branchMappings.Add(rename.NewIdentity, mapping.RenameRootDirectory(rename.OldIdentity.Path, rename.NewIdentity.Path));

foreach (var (otherBranch, otherMapping) in branchMappings.ToArray())
{
branchMappings[otherBranch] = otherMapping.ApplyRename(rename.OldIdentity.Path, rename.NewIdentity.Path);
}

branchMappings.Add(rename.NewIdentity, mapping.ApplyRename(rename.OldIdentity.Path, rename.NewIdentity.Path));

if (trunk == rename.OldIdentity) trunk = rename.NewIdentity;
break;
Expand Down
15 changes: 5 additions & 10 deletions src/TfvcMigrator/RepositoryBranchMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,19 @@ public RepositoryBranchMapping(string rootDirectory, (string BranchDirectory, st
/// </summary>
public (string BranchDirectory, string TargetDirectory)? SubdirectoryMapping { get; }

public RepositoryBranchMapping RenameRootDirectory(string oldPath, string newPath)
public RepositoryBranchMapping ApplyRename(string oldPath, string newPath)
{
if (!PathUtils.IsAbsolute(oldPath))
throw new ArgumentException("Old path must be absolute.", nameof(oldPath));

if (!PathUtils.IsAbsolute(newPath))
throw new ArgumentException("New path must be absolute.", nameof(newPath));

if (!PathUtils.IsOrContains(RootDirectory, oldPath))
throw new InvalidOperationException("The rename does not apply to this mapping.");

if (SubdirectoryMapping is not null)
throw new NotImplementedException("Research: Renaming and branching might behave differently when subdirectory mapping is involved.");

return new RepositoryBranchMapping(
PathUtils.ReplaceContainingPath(RootDirectory, oldPath, newPath),
subdirectoryMapping: null);
SubdirectoryMapping is null ? null : (
PathUtils.ReplaceContainingPath(SubdirectoryMapping.Value.BranchDirectory, oldPath, newPath),
PathUtils.ReplaceContainingPath(SubdirectoryMapping.Value.TargetDirectory, oldPath, newPath)));
}

public RepositoryBranchMapping WithSubdirectoryMapping(string branchDirectory, string targetDirectory)
Expand All @@ -76,8 +72,7 @@ public RepositoryBranchMapping WithSubdirectoryMapping(string branchDirectory, s
if (PathUtils.IsOrContains(target, itemPath))
return null;

if (PathUtils.IsOrContains(branch, itemPath))
itemPath = PathUtils.ReplaceContainingPath(itemPath, branch, target);
itemPath = PathUtils.ReplaceContainingPath(itemPath, branch, target);
}

return PathUtils.IsOrContains(RootDirectory, itemPath)
Expand Down

0 comments on commit 3edec49

Please sign in to comment.