Permalink
Browse files

Respect core.ignorecase in BuildRelativePathFrom()

Fixes #214
  • Loading branch information...
1 parent a187d2e commit 020e14a7e031539f85e9ab3a9ce7ebf8bfdeb187 @dahlbyk dahlbyk committed with nulltoken Feb 19, 2013
@@ -121,10 +121,14 @@ public void CanStageANewFileInAPersistentManner()
}
}
- [Fact]
- public void CanStageANewFileWithAFullPath()
+ [SkippableTheory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void CanStageANewFileWithAFullPath(bool ignorecase)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
+ SetIgnoreCaseOrSkip(path.RepositoryPath, ignorecase);
+
using (var repo = new Repository(path.RepositoryPath))
{
int count = repo.Index.Count;
@@ -133,10 +137,24 @@ public void CanStageANewFileWithAFullPath()
string fullPath = Path.Combine(repo.Info.WorkingDirectory, filename);
Assert.True(File.Exists(fullPath));
- repo.Index.Stage(fullPath);
+ AssertStage(null, repo, fullPath);
+ AssertStage(ignorecase, repo, fullPath.ToUpperInvariant());
+ AssertStage(ignorecase, repo, fullPath.ToLowerInvariant());
+ }
+ }
- Assert.Equal(count + 1, repo.Index.Count);
- Assert.NotNull(repo.Index[filename]);
+ private static void AssertStage(bool? ignorecase, IRepository repo, string path)
+ {
+ try
+ {
+ repo.Index.Stage(path);
+ Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus(path));
+ repo.Reset();
+ Assert.Equal(FileStatus.Untracked, repo.Index.RetrieveStatus(path));
+ }
+ catch (ArgumentException)
+ {
+ Assert.False(ignorecase ?? true);
}
}
@@ -293,6 +293,21 @@ FileStatus expectedCamelCasedFileStatus
Assert.Equal(expectedlowerCasedFileStatus, repo.Index.RetrieveStatus("plop"));
Assert.Equal(expectedCamelCasedFileStatus, repo.Index.RetrieveStatus("Plop"));
+
+ AssertStatus(shouldIgnoreCase, expectedlowerCasedFileStatus, repo, camelCasedPath.ToLowerInvariant());
+ AssertStatus(shouldIgnoreCase, expectedCamelCasedFileStatus, repo, camelCasedPath.ToUpperInvariant());
+ }
+ }
+
+ private static void AssertStatus(bool shouldIgnoreCase, FileStatus expectedFileStatus, IRepository repo, string path)
+ {
+ try
+ {
+ Assert.Equal(expectedFileStatus, repo.Index.RetrieveStatus(path));
+ }
+ catch (ArgumentException)
+ {
+ Assert.False(shouldIgnoreCase);
}
}
}
@@ -120,6 +120,18 @@ protected static void AssertValueInConfigFile(string configFilePath, string rege
Assert.True(r.Success, text);
}
+ protected static void SetIgnoreCaseOrSkip(string path, bool ignorecase)
+ {
+ var canIgnoreCase = Directory.Exists(path.ToUpperInvariant()) && Directory.Exists(path.ToLowerInvariant());
+ if (!canIgnoreCase && ignorecase)
+ throw new SkipException("Skipping 'ignorecase = true' test on case-sensitive file system.");
+
+ using (var repo = new Repository(path))
+ {
+ repo.Config.Set("core.ignorecase", ignorecase);
+ }
+ }
+
public RepositoryOptions BuildFakeConfigs(SelfCleaningDirectory scd)
{
var options = BuildFakeRepositoryOptions(scd);
View
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Core.Handles;
@@ -52,7 +50,7 @@ private static FilePath[] ToFilePaths(Repository repo, IEnumerable<string> paths
throw new ArgumentException("At least one provided path is either null or empty.", "paths");
}
- filePaths.Add(BuildRelativePathFrom(repo, path));
+ filePaths.Add(repo.BuildRelativePathFrom(path));
}
if (filePaths.Count == 0)
@@ -63,26 +61,6 @@ private static FilePath[] ToFilePaths(Repository repo, IEnumerable<string> paths
return filePaths.ToArray();
}
- private static string BuildRelativePathFrom(Repository repo, string path)
- {
- //TODO: To be removed when libgit2 natively implements this
- if (!Path.IsPathRooted(path))
- {
- return path;
- }
-
- string normalizedPath = Path.GetFullPath(path);
-
- if (!normalizedPath.StartsWith(repo.Info.WorkingDirectory, StringComparison.Ordinal))
- {
- throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
- "Unable to process file '{0}'. This absolute filepath escapes out of the working directory of the repository ('{1}').",
- normalizedPath, repo.Info.WorkingDirectory));
- }
-
- return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
- }
-
/// <summary>
/// Needed for mocking purposes.
/// </summary>
View
@@ -355,7 +355,7 @@ public virtual void Remove(IEnumerable<string> paths)
throw new ArgumentException("At least one provided path is either null or empty.", "paths");
}
- string relativePath = BuildRelativePathFrom(repo, path);
+ string relativePath = repo.BuildRelativePathFrom(path);
FileStatus fileStatus = RetrieveStatus(relativePath);
dic.Add(relativePath, fileStatus);
@@ -388,7 +388,7 @@ public virtual void Remove(IEnumerable<string> paths)
private Tuple<string, FileStatus> BuildFrom(string path)
{
- string relativePath = BuildRelativePathFrom(repo, path);
+ string relativePath = repo.BuildRelativePathFrom(path);
return new Tuple<string, FileStatus>(relativePath, RetrieveStatus(relativePath));
}
@@ -420,26 +420,6 @@ private void UpdatePhysicalIndex()
Proxy.git_index_write(handle);
}
- private static string BuildRelativePathFrom(Repository repo, string path)
- {
- //TODO: To be removed when libgit2 natively implements this
- if (!Path.IsPathRooted(path))
- {
- return path;
- }
-
- string normalizedPath = Path.GetFullPath(path);
-
- if (!normalizedPath.StartsWith(repo.Info.WorkingDirectory, StringComparison.Ordinal))
- {
- throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
- "Unable to process file '{0}'. This file is not located under the working directory of the repository ('{1}').",
- normalizedPath, repo.Info.WorkingDirectory));
- }
-
- return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
- }
-
/// <summary>
/// Retrieves the state of a file in the working directory, comparing it against the staging area and the latest commmit.
/// </summary>
@@ -449,7 +429,7 @@ public virtual FileStatus RetrieveStatus(string filePath)
{
Ensure.ArgumentNotNullOrEmptyString(filePath, "filePath");
- string relativePath = BuildRelativePathFrom(repo, filePath);
+ string relativePath = repo.BuildRelativePathFrom(filePath);
return Proxy.git_status_file(repo.Handle, relativePath);
}
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
@@ -253,5 +255,26 @@ public static Branch Checkout(this IRepository repository, Branch branch)
{
return repository.Checkout(branch, CheckoutOptions.None, null);
}
+
+ internal static string BuildRelativePathFrom(this Repository repo, string path)
+ {
+ //TODO: To be removed when libgit2 natively implements this
+ if (!Path.IsPathRooted(path))
+ {
+ return path;
+ }
+
+ string normalizedPath = Path.GetFullPath(path);
+
+ if (!repo.PathStartsWith(normalizedPath, repo.Info.WorkingDirectory))
+ {
+ throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
+ "Unable to process file '{0}'. This file is not located under the working directory of the repository ('{1}').",
+ normalizedPath, repo.Info.WorkingDirectory));
+ }
+
+ return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
+ }
+
}
}

0 comments on commit 020e14a

Please sign in to comment.