Skip to content

Commit

Permalink
fix relative path for saving file content and checkout file
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Dec 7, 2011
1 parent e07ffcc commit b363750
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
1 change: 0 additions & 1 deletion BasicSccProvider.Tests/BasicSccProvider.Tests.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
</CodeAnalysisDependentAssemblyPaths> </CodeAnalysisDependentAssemblyPaths>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="GetFileStatusTest.cs" />
<Compile Include="GitFileStatusTrackerTest.cs" /> <Compile Include="GitFileStatusTrackerTest.cs" />
<Compile Include="PackageTest.cs" /> <Compile Include="PackageTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
50 changes: 45 additions & 5 deletions BasicSccProvider.Tests/GitFileStatusTrackerTest.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class GitFileStatusTrackerTest
public GitFileStatusTrackerTest() public GitFileStatusTrackerTest()
{ {
tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString(); tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString();
tempFile = Path.Combine(tempFolder, "test"); tempFile = Path.Combine(tempFolder, "t e s t");
lines = new string[] { "First line", "Second line", "Third line" }; lines = new string[] { "First line", "中文 2", "čtestč" };
} }


private TestContext testContextInstance; private TestContext testContextInstance;
Expand Down Expand Up @@ -276,7 +276,11 @@ public void FileNameCaseTest()
tempFile = tempFile.Replace("test", "TEST"); tempFile = tempFile.Replace("test", "TEST");
File.WriteAllText(tempFile, "changed text"); File.WriteAllText(tempFile, "changed text");
tracker.Refresh(); tracker.Refresh();
Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile)); //This test fails all cases because status check uses ngit, never git.exe
//Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile));

var file = tracker.ChangedFiles.First();
Assert.AreEqual(GitFileStatus.Modified, file.Status);
} }


[TestMethod] [TestMethod]
Expand All @@ -298,6 +302,42 @@ public void GetBranchTest()
tracker.CheckOutBranch("dev", true); tracker.CheckOutBranch("dev", true);
Assert.AreEqual("dev", tracker.CurrentBranch); Assert.AreEqual("dev", tracker.CurrentBranch);
} }

[TestMethod]
public void SaveFileFromRepositoryTest()
{
GitFileStatusTracker.Init(tempFolder);
File.WriteAllLines(tempFile, lines);

GitFileStatusTracker tracker = new GitFileStatusTracker(tempFolder);
tracker.StageFile(tempFile);
tracker.Commit("test");

tracker.SaveFileFromRepository(tempFile, tempFile + ".bk");
var newlines = File.ReadAllLines(tempFile + ".bk");
Assert.AreEqual(lines[0], newlines[0]);
Assert.AreEqual(lines[1], newlines[1]);
Assert.AreEqual(lines[2], newlines[2]);
}

[TestMethod]
public void CheckOutFileTest()
{
GitFileStatusTracker.Init(tempFolder);
File.WriteAllLines(tempFile, lines);

GitFileStatusTracker tracker = new GitFileStatusTracker(tempFolder);
tracker.StageFile(tempFile);
tracker.Commit("test");

File.WriteAllText(tempFile, "changed text");
tracker.CheckOutFile(tempFile);
var newlines = File.ReadAllLines(tempFile);
Assert.AreEqual(lines[0], newlines[0]);
Assert.AreEqual(lines[1], newlines[1]);
Assert.AreEqual(lines[2], newlines[2]);
}

} }


[TestClass()] [TestClass()]
Expand All @@ -319,7 +359,7 @@ public GitFileStatusTrackerTest_WithSubFolder()
GitBash.GitExePath = null; GitBash.GitExePath = null;
tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString(); tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString();
Directory.CreateDirectory(Path.Combine(tempFolder, "folder 1")); Directory.CreateDirectory(Path.Combine(tempFolder, "folder 1"));
tempFile = Path.Combine(tempFolder, "folder 1\\test"); tempFile = Path.Combine(tempFolder, "folder 1\\t e s t");
} }
} }


Expand All @@ -342,7 +382,7 @@ public GitFileStatusTrackerTest_WithSubFolder_UsingGitBash()
GitBash.GitExePath = @"C:\Program Files (x86)\Git\bin\sh.exe"; GitBash.GitExePath = @"C:\Program Files (x86)\Git\bin\sh.exe";
tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString(); tempFolder = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString();
Directory.CreateDirectory(Path.Combine(tempFolder, "folder 2")); Directory.CreateDirectory(Path.Combine(tempFolder, "folder 2"));
tempFile = Path.Combine(tempFolder, "folder 2\\test"); tempFile = Path.Combine(tempFolder, "folder 2\\t e s t");
} }
} }
} }
13 changes: 6 additions & 7 deletions GitFileStatusTracker.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public IEnumerable<GitFile> ChangedFiles
private const int INDEX = 1; private const int INDEX = 1;
private const int WORKDIR = 2; private const int WORKDIR = 2;


public IList<GitFile> GetChangedFiles() internal IList<GitFile> GetChangedFiles()
{ {
if (GitBash.Exists) if (GitBash.Exists)
{ {
Expand Down Expand Up @@ -1046,15 +1046,14 @@ public IList<GitFile> ParseGitStatus(string statusString)


#endregion #endregion


internal void SaveFileFromRepository(string fileName, string tempFile) public void SaveFileFromRepository(string fileName, string tempFile)
{ {
if (!this.HasGitRepository || this.head == null) return; if (!this.HasGitRepository || this.head == null) return;


string fileNameRel = GetRelativeFileName(fileName);

if (GitBash.Exists) if (GitBash.Exists)
{ {
GitBash.RunCmd(string.Format("show HEAD:{0} > \"{1}\"", fileNameRel, tempFile), this.GitWorkingDirectory); string fileNameRel = GetRelativeFileNameForGit(fileName);
GitBash.RunCmd(string.Format("show \"HEAD:{0}\" > \"{1}\"", fileNameRel, tempFile), this.GitWorkingDirectory);
} }
else else
{ {
Expand All @@ -1066,15 +1065,15 @@ internal void SaveFileFromRepository(string fileName, string tempFile)
} }
} }


internal void CheckOutFile(string fileName) public void CheckOutFile(string fileName)
{ {
if (!this.HasGitRepository || this.head == null) return; if (!this.HasGitRepository || this.head == null) return;


string fileNameRel = GetRelativeFileName(fileName); string fileNameRel = GetRelativeFileName(fileName);


if (GitBash.Exists) if (GitBash.Exists)
{ {
GitBash.Run("checkout -- " + fileNameRel, this.GitWorkingDirectory); GitBash.Run(string.Format("checkout -- \"{0}\"", fileNameRel), this.GitWorkingDirectory);
} }
else else
{ {
Expand Down

0 comments on commit b363750

Please sign in to comment.