Skip to content

Commit

Permalink
add Pending Changes Tool Window
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Aug 7, 2011
1 parent 35475ba commit 80f3e87
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 205 deletions.
141 changes: 63 additions & 78 deletions BasicSccProvider.Tests/GitFileStatusTrackerTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.IO;
using GitScc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using System.Diagnostics;
using GitSharp.Core;
using GitScc;

namespace BasicSccProvider.Tests
{
Expand Down Expand Up @@ -81,82 +82,6 @@ public void HasGitRepositoryTest()

}

//[TestMethod]
//public void GetFileStatusTest()
//{
// var tempFolder = Environment.CurrentDirectory + "\\_gitscc_test_2";
// var tempFile = Path.Combine(tempFolder, "test");

// GitFileStatusTracker.Init(tempFolder);
// GitFileStatusTracker tracker = new GitFileStatusTracker(tempFolder);

// string[] lines = { "First line", "Second line", "Third line" };

// File.WriteAllLines(tempFile, lines);
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.New, tracker.GetFileStatus(tempFile));

// using (var repo = new Repository(tempFolder))
// {
// repo.Index.Add(tempFile);
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.Added, tracker.GetFileStatus(tempFile));

// repo.Index.CommitChanges("test", new Author("test", "[email protected]"));
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.Trackered, tracker.GetFileStatus(tempFile));

// File.WriteAllText(tempFile, "changed text");
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile));

// File.Delete(tempFile);
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.Missing, tracker.GetFileStatus(tempFile));

// repo.Index.Remove(tempFile);
// tracker.Refresh();
// Assert.AreEqual(GitFileStatus.Removed, tracker.GetFileStatus(tempFile));

// repo.Close();
// }
//}

//[TestMethod]
//public void GetFileContentTest()
//{
// var tempFolder = Environment.CurrentDirectory + "\\_gitscc_test_3";
// var tempFile = Path.Combine(tempFolder, "test");

// GitFileStatusTracker.Init(tempFolder);

// string[] lines = { "First line", "Second line", "Third line" };
// File.WriteAllLines(tempFile, lines);


// using (var repo = new Repository(tempFolder))
// {
// repo.Index.Add(tempFile);
// repo.Index.CommitChanges("test", new Author("test", "[email protected]"));
// repo.Close();
// }

// GitFileStatusTracker tracker = new GitFileStatusTracker(tempFolder);

// var fileContent = tracker.GetFileContent(tempFile);

// using (var binWriter = new BinaryWriter(File.Open(tempFile + ".bk", FileMode.Create)))
// {
// binWriter.Write(fileContent);
// }

// 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 GetFileStatusTest()
{
Expand Down Expand Up @@ -210,7 +135,7 @@ public void GetFileContentTest()

var fileContent = tracker.GetFileContent(tempFile);

using (var binWriter = new BinaryWriter(File.Open(tempFile + ".bk", FileMode.Create)))
using (var binWriter = new BinaryWriter(File.Open(tempFile + ".bk", System.IO.FileMode.Create)))
{
binWriter.Write(fileContent);
}
Expand All @@ -220,5 +145,65 @@ public void GetFileContentTest()
Assert.AreEqual(lines[1], newlines[1]);
Assert.AreEqual(lines[2], newlines[2]);
}

[TestMethod]
public void GetChangedFilesTest()
{
var initFolder = @"E:\Users\Public\My Projects\GitScc\Publish\TestProjects\Folder Test";
Repository repository = Repository.Open(initFolder);

var commit = repository.MapCommit("HEAD");
var tree = (commit != null ? commit.TreeEntry : new Tree(repository));
var index = repository.Index;
index.RereadIfNecessary();

DirectoryInfo root = new DirectoryInfo(initFolder);
var visitor = new AbstractIndexTreeVisitor { VisitEntryAux = OnVisitEntry };

IgnoreHandler = new IgnoreHandler(repository);
var mainTree = new Tree(repository);

// Create the subdir branch
foreach (string sdir in initFolder.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
mainTree = mainTree.AddTree(sdir);

if (root.Exists)
FillTree(root, mainTree);

//new IndexTreeWalker(index, tree, mainTree, root, visitor).Walk();

}

IgnoreHandler IgnoreHandler;

private void FillTree(DirectoryInfo dir, Tree tree)
{
foreach (var subdir in dir.GetDirectories())
{
if (subdir.Name == Constants.DOT_GIT || IgnoreHandler.IsIgnored(tree.FullName + "/" + subdir.Name))
continue;
var t = tree.AddTree(subdir.Name);
FillTree(subdir, t);
}
foreach (var file in dir.GetFiles())
{
if (IgnoreHandler.IsIgnored(tree.FullName + "/" + file.Name))
continue;
tree.AddFile(file.Name);

Console.WriteLine("{0}", file.Name);
}
}

private void OnVisitEntry(TreeEntry treeEntry, TreeEntry wdirEntry, GitIndex.Entry indexEntry, FileInfo file)
{
Console.WriteLine("{0} - {1} - {2} - {3}", file.Name, treeEntry, wdirEntry, indexEntry);
}

[TestMethod]
public void GetChangedFilesTest_Core()
{

}
}
}
56 changes: 53 additions & 3 deletions BasicSccProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace GitScc
[MsVsShell.ProvideOptionPageAttribute(typeof(SccProviderOptions), "Source Control", "Git Source Control Provider Options", 106, 107, false)]
[ProvideToolsOptionsPageVisibility("Source Control", "Git Source Control Provider Options", "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
// Register a sample tool window visible only when the provider is active
//[MsVsShell.ProvideToolWindow(typeof(PendingChangesToolWindow), Style = VsDockStyle.Tabbed, Orientation = ToolWindowOrientation.Bottom)]
//[MsVsShell.ProvideToolWindowVisibility(typeof(PendingChangesToolWindow), "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
[MsVsShell.ProvideToolWindow(typeof(PendingChangesToolWindow), Style = VsDockStyle.Tabbed, Orientation = ToolWindowOrientation.Bottom)]
[MsVsShell.ProvideToolWindowVisibility(typeof(PendingChangesToolWindow), "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
//[MsVsShell.ProvideToolWindow(typeof(HistoryToolWindow), Style = VsDockStyle.Tabbed, Orientation = ToolWindowOrientation.Bottom)]
//[MsVsShell.ProvideToolWindowVisibility(typeof(HistoryToolWindow), "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
//Register the source control provider's service (implementing IVsScciProvider interface)
Expand Down Expand Up @@ -119,6 +119,10 @@ protected override void Initialize()
var mc = new MenuCommand(new EventHandler(OnGitTorCommandExec), cmd);
mcs.AddCommand(mc);
}

cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandPendingChanges);
menu = new MenuCommand(new EventHandler(ShowPendingChangesWindow), cmd);
mcs.AddCommand(menu);
}


Expand Down Expand Up @@ -220,11 +224,15 @@ int IOleCommandTarget.QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] pr
if (sccService.CanCompareSelectedFile) cmdf |= OLECMDF.OLECMDF_ENABLED;
break;

case CommandId.icmdSccCommandHistory:
cmdf |= OLECMDF.OLECMDF_INVISIBLE;
break;

case CommandId.icmdSccCommandRefresh:
//if (sccService.IsSolutionGitControlled)
cmdf |= OLECMDF.OLECMDF_ENABLED;
break;

case CommandId.icmdSccCommandInit:
if (!sccService.IsSolutionGitControlled)
cmdf |= OLECMDF.OLECMDF_ENABLED;
Expand Down Expand Up @@ -364,6 +372,35 @@ private void OnGitExtCommandExec(object sender, EventArgs e)
}
}

private void ShowPendingChangesWindow(object sender, EventArgs e)
{
ShowToolWindow(typeof(PendingChangesToolWindow));
}

private void ShowHistoryWindow(object sender, EventArgs e)
{
ShowToolWindow(typeof(HistoryToolWindow));
}

private void ShowToolWindow(Type type)
{
ToolWindowPane window = this.FindToolWindow(type, 0, true);
IVsWindowFrame windowFrame = null;
if (window != null && window.Frame != null)
{
windowFrame = (IVsWindowFrame)window.Frame;
}
if (windowFrame != null)
{
ErrorHandler.ThrowOnFailure(windowFrame.Show());

//if (window is PendingChangesToolWindow)
//{
// ((PendingChangesToolWindow)window).Refresh();
//}
}
}

#endregion

// This function is called by the IVsSccProvider service implementation when the active state of the provider changes
Expand Down Expand Up @@ -428,5 +465,18 @@ internal void RunDetatched(string cmd, string arguments)
}
#endregion

//internal void OnSccStatusChanged()
//{
// var pendingChangesToolWindow = GetToolWindowPane<PendingChangesToolWindow>();
// if (pendingChangesToolWindow != null)
// {
// pendingChangesToolWindow.Refresh(sccService.CurrentTracker);
// }
//}

//private T GetToolWindowPane<T>() where T : ToolWindowPane
//{
// return (T)this.FindToolWindow(typeof(T), 0, true);
//}
}
}
70 changes: 60 additions & 10 deletions GitFileStatusTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GitFileStatusTracker : IDisposable
private Repository repository;
private Tree commitTree;
private GitIndex index;
//private IgnoreHandler ignoreHandler;
private IgnoreHandler ignoreHandler;

private Dictionary<string, GitFileStatus> cache;

Expand All @@ -44,7 +44,7 @@ public void Refresh()
this.commitTree = (commit != null ? commit.TreeEntry : new Tree(repository));
this.index = repository.Index;
this.index.RereadIfNecessary();
//this.ignoreHandler = new IgnoreHandler(repository);
this.ignoreHandler = new IgnoreHandler(repository);
//this.watcher = new FileSystemWatcher(this.repository.WorkingDirectory.FullName);
}
}
Expand Down Expand Up @@ -150,10 +150,12 @@ private GitFileStatus GetFileStatusNoCache(string fileName)

private string GetRelativeFileName(string fileName)
{
Uri workingFolderUri = new Uri(repository.WorkingDirectory.FullName + "\\");
fileName = workingFolderUri.MakeRelativeUri(new Uri(fileName)).ToString();
fileName = fileName.Replace("%20", " ");
return fileName;
//Uri workingFolderUri = new Uri(repository.WorkingDirectory.FullName + "\\");
//fileName = workingFolderUri.MakeRelativeUri(new Uri(fileName)).ToString();
//fileName = fileName.Replace("%20", " ");
//return fileName;

return PathUtil.RelativePath(repository.WorkingDirectory.FullName, fileName);
}

public byte[] GetFileContent(string fileName)
Expand Down Expand Up @@ -194,23 +196,69 @@ public override string ToString()
return repository == null ? "[no repo]" : repository.WorkingDirectory.FullName;
}

public System.Collections.IEnumerable ChangedFiles { get; set; }
public IEnumerable<GitFile> ChangedFiles
{
get
{
DirectoryInfo root = new DirectoryInfo(initFolder);
var mainTree = new Tree(repository);
FillTree(root, mainTree);

return from f in cache
where f.Value != GitFileStatus.Tracked &&
f.Value != GitFileStatus.NotControlled
select new GitFile
{
FileName = GetRelativeFileName(f.Key),
Status = f.Value,
IsStaged = f.Value == GitFileStatus.Added ||
f.Value == GitFileStatus.Staged ||
f.Value == GitFileStatus.Removed
};
}
}

private void FillTree(DirectoryInfo dir, Tree tree)
{
foreach (var subdir in dir.GetDirectories())
{
if (subdir.Name == Constants.DOT_GIT || this.ignoreHandler.IsIgnored(tree.FullName + "/" + subdir.Name))
continue;
var t = tree.AddTree(subdir.Name);
FillTree(subdir, t);
}

foreach (var file in dir.GetFiles())
{
if (this.ignoreHandler.IsIgnored(tree.FullName + "/" + file.Name))
continue;
tree.AddFile(file.Name);

var fileName = string.IsNullOrEmpty(tree.FullName) ?
Path.Combine(initFolder, file.Name)
:
Path.Combine(initFolder, tree.FullName + "\\" + file.Name);

var status = GetFileStatus(fileName.Replace("/", "\\"));
}
}

public void UnStageFile(string fileName)
{
fileName = Path.Combine(initFolder, fileName);

if (!this.HasGitRepository) return;
this.index.RereadIfNecessary();

var content = GetFileContent(fileName);
fileName = GetRelativeFileName(fileName);
if (content == null)
{
this.index.remove(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName));
this.index.Remove(fileName);
}
else
{

this.index.add(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName),
Expand All @@ -222,6 +270,8 @@ public void UnStageFile(string fileName)

public void StageFile(string fileName)
{
fileName = Path.Combine(initFolder, fileName);

if (!this.HasGitRepository) return;
this.index.RereadIfNecessary();

Expand Down
Loading

0 comments on commit 80f3e87

Please sign in to comment.