Skip to content

Commit

Permalink
Further use of ObjectId
Browse files Browse the repository at this point in the history
Make most GitSubmoduleStatus properties readonly.
Add IGitRef.ObjectId.
  • Loading branch information
drewnoakes committed Jul 22, 2018
1 parent 9db5c48 commit a94a30a
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 95 deletions.
43 changes: 25 additions & 18 deletions GitCommands/Git/GitCommandHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public static GitSubmoduleStatus GetCurrentSubmoduleChanges(GitModule module, st
{
Patch patch = module.GetCurrentChanges(fileName, oldFileName, staged, "", module.FilesEncoding);
string text = patch != null ? patch.Text : "";
return GetSubmoduleStatus(text, module, fileName);
return ParseSubmoduleStatus(text, module, fileName);
}

[CanBeNull]
Expand All @@ -717,14 +717,21 @@ public static GitSubmoduleStatus GetCurrentSubmoduleChanges(GitModule module, st
}

[CanBeNull]
public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule module, string fileName)
public static GitSubmoduleStatus ParseSubmoduleStatus(string text, GitModule module, string fileName)
{
if (string.IsNullOrEmpty(text))
{
return null;
}

var status = new GitSubmoduleStatus();
string name = null;
string oldName = null;
bool isDirty = false;
ObjectId commitId = null;
ObjectId oldCommitId = null;
int? addedCommits = null;
int? removedCommits = null;

using (var reader = new StringReader(text))
{
string line = reader.ReadLine();
Expand All @@ -734,16 +741,16 @@ public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule modul
var match = Regex.Match(line, @"diff --git [abic]/(.+)\s[abwi]/(.+)");
if (match.Groups.Count > 1)
{
status.Name = match.Groups[1].Value;
status.OldName = match.Groups[2].Value;
name = match.Groups[1].Value;
oldName = match.Groups[2].Value;
}
else
{
match = Regex.Match(line, @"diff --cc (.+)");
if (match.Groups.Count > 1)
{
status.Name = match.Groups[1].Value;
status.OldName = match.Groups[1].Value;
name = match.Groups[1].Value;
oldName = match.Groups[1].Value;
}
}
}
Expand All @@ -761,38 +768,38 @@ public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule modul
}

char c = line[0];
const string commit = "commit ";
const string commitStr = "commit ";
string hash = "";
int pos = line.IndexOf(commit);
int pos = line.IndexOf(commitStr);
if (pos >= 0)
{
hash = line.Substring(pos + commit.Length);
hash = line.Substring(pos + commitStr.Length);
}

bool isDirty = hash.EndsWith("-dirty");
bool endsWithDirty = hash.EndsWith("-dirty");
hash = hash.Replace("-dirty", "");
if (c == '-')
{
status.OldCommit = hash;
oldCommitId = ObjectId.Parse(hash);
}
else if (c == '+')
{
status.Commit = hash;
status.IsDirty = isDirty;
commitId = ObjectId.Parse(hash);
isDirty = endsWithDirty;
}

// TODO: Support combined merge
}
}

if (status.OldCommit != null && status.Commit != null)
if (oldCommitId != null && commitId != null)
{
var submodule = module.GetSubmodule(fileName);
status.AddedCommits = submodule.GetCommitCount(status.Commit, status.OldCommit);
status.RemovedCommits = submodule.GetCommitCount(status.OldCommit, status.Commit);
addedCommits = submodule.GetCommitCount(commitId.ToString(), oldCommitId.ToString());
removedCommits = submodule.GetCommitCount(oldCommitId.ToString(), commitId.ToString());
}

return status;
return new GitSubmoduleStatus(name, oldName, isDirty, commitId, oldCommitId, addedCommits, removedCommits);
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion GitCommands/Git/GitItemStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using GitUIPluginInterfaces;
using JetBrains.Annotations;
using Microsoft.VisualStudio.Threading;

Expand Down Expand Up @@ -39,7 +40,8 @@ private enum Flags

public string Name { get; set; }
public string OldName { get; set; }
public string TreeGuid { get; set; }
[CanBeNull]
public ObjectId TreeGuid { get; set; }
public string RenameCopyPercentage { get; set; }

// Staged is three state and has no default status
Expand Down
15 changes: 8 additions & 7 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ public IReadOnlyList<GitItemStatus> GetTreeFiles(ObjectId treeGuid, bool full)
IsChanged = false,
IsDeleted = false,
Name = file.Name,
TreeGuid = file.Guid,
TreeGuid = file.ObjectId,
Staged = StagedStatus.None
}).ToList();

Expand Down Expand Up @@ -2550,7 +2550,7 @@ private void GetSubmoduleStatus(IReadOnlyList<GitItemStatus> status, string firs
Patch patch = GetSingleDiff(firstRevision, secondRevision, item.Name, item.OldName, "", SystemEncoding, true);
string text = patch != null ? patch.Text : "";
var submoduleStatus = GitCommandHelpers.GetSubmoduleStatus(text, this, item.Name);
var submoduleStatus = GitCommandHelpers.ParseSubmoduleStatus(text, this, item.Name);
if (submoduleStatus.Commit != submoduleStatus.OldCommit)
{
var submodule = submoduleStatus.GetSubmodule(this);
Expand Down Expand Up @@ -2941,7 +2941,7 @@ public IReadOnlyList<IGitRef> ParseRefs([NotNull] string refList)
foreach (Match match in matches)
{
var refName = match.Groups["refname"].Value;
var objectId = match.Groups["objectid"].Value;
var objectId = ObjectId.Parse(match.Groups["objectid"].Value);
var remoteName = GitRefName.GetRemoteName(refName);
var head = new GitRef(this, objectId, refName, remoteName);

Expand Down Expand Up @@ -3362,7 +3362,8 @@ public string GetFileText(string id, Encoding encoding)
return RunCacheableGitCmd($"cat-file blob \"{id}\"", encoding);
}

public string GetFileBlobHash(string fileName, ObjectId objectId)
[CanBeNull]
public ObjectId GetFileBlobHash(string fileName, ObjectId objectId)
{
if (objectId == ObjectId.UnstagedId)
{
Expand All @@ -3377,19 +3378,19 @@ public string GetFileBlobHash(string fileName, ObjectId objectId)
var lines = RunGitCmd($"ls-files -s \"{fileName}\"").Split(' ', '\t');
if (lines.Length >= 2)
{
return lines[1];
return ObjectId.Parse(lines[1]);
}
}
else
{
var lines = RunGitCmd($"ls-tree -r {objectId} \"{fileName}\"").Split(' ', '\t');
if (lines.Length >= 3)
{
return lines[2];
return ObjectId.Parse(lines[2]);
}
}

return "";
return null;
}

[CanBeNull]
Expand Down
22 changes: 11 additions & 11 deletions GitCommands/Git/GitRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

namespace GitCommands
{
public class GitRef : IGitRef
public sealed class GitRef : IGitRef
{
private readonly string _mergeSettingName;
private readonly string _remoteSettingName;

public IGitModule Module { get; }

public GitRef(IGitModule module, string guid, string completeName)
: this(module, guid, completeName, string.Empty)
public GitRef(IGitModule module, [CanBeNull] ObjectId objectId, string completeName)
: this(module, objectId, completeName, string.Empty)
{
}

public GitRef(IGitModule module, string guid, string completeName, string remote)
public GitRef(IGitModule module, [CanBeNull] ObjectId objectId, string completeName, string remote)
{
Module = module;
Guid = guid;
IsSelected = false;
ObjectId = objectId;
Guid = objectId?.ToString();
CompleteName = completeName;
Remote = remote;

Expand All @@ -41,7 +41,7 @@ public GitRef(IGitModule module, string guid, string completeName, string remote
}

public string CompleteName { get; }
public bool IsSelected { get; set; }
public bool IsSelected { get; set; } = false;
public bool IsSelectedHeadMergeSource { get; set; }
public bool IsTag { get; }
public bool IsHead { get; }
Expand Down Expand Up @@ -125,15 +125,15 @@ public static GitRef NoHead(GitModule module)

#region IGitItem Members

[CanBeNull]
public ObjectId ObjectId { get; }
[CanBeNull]
public string Guid { get; }
public string Name { get; }

#endregion

public override string ToString()
{
return CompleteName;
}
public override string ToString() => CompleteName;

[CanBeNull]
private string ParseName()
Expand Down
32 changes: 23 additions & 9 deletions GitCommands/Git/GitSubmoduleStatus.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
namespace GitCommands
using GitUIPluginInterfaces;

namespace GitCommands
{
public sealed class GitSubmoduleStatus
{
public string Name { get; set; }
public string OldName { get; set; }
public bool IsDirty { get; set; }
public string Commit { get; set; }
public string OldCommit { get; set; }
public string Name { get; }
public string OldName { get; }
public bool IsDirty { get; }
public ObjectId Commit { get; }
public ObjectId OldCommit { get; }
public int? AddedCommits { get; }
public int? RemovedCommits { get; }

public SubmoduleStatus Status { get; set; } = SubmoduleStatus.Unknown;
public int? AddedCommits { get; set; }
public int? RemovedCommits { get; set; }

public GitSubmoduleStatus(string name, string oldName, bool isDirty, ObjectId commit, ObjectId oldCommit, int? addedCommits, int? removedCommits)
{
Name = name;
OldName = oldName;
IsDirty = isDirty;
Commit = commit;
OldCommit = oldCommit;
AddedCommits = addedCommits;
RemovedCommits = removedCommits;
}

public GitModule GetSubmodule(GitModule module)
{
Expand All @@ -24,7 +38,7 @@ public void CheckSubmoduleStatus(GitModule submodule)
return;
}

Status = submodule.CheckSubmoduleStatus(Commit, OldCommit);
Status = submodule.CheckSubmoduleStatus(Commit.ToString(), OldCommit.ToString());
}

public string AddedAndRemovedString()
Expand Down
4 changes: 2 additions & 2 deletions GitCommands/Remote/GitRemoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public string GetDefaultPushRemote(GitRemote remote, string branch)
var module = GetModule();
bool IsSettingForBranch(string setting, string branchName)
{
var head = new GitRef(module, string.Empty, setting);
var head = new GitRef(module, null, setting);
return head.IsHead && head.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase);
}

var remoteHead = remote.Push
.Select(s => s.Split(':'))
.Where(t => t.Length == 2)
.Where(t => IsSettingForBranch(t[0], branch))
.Select(t => new GitRef(module, string.Empty, t[1]))
.Select(t => new GitRef(module, null, t[1]))
.FirstOrDefault(h => h.IsHead);

return remoteHead?.Name;
Expand Down
6 changes: 3 additions & 3 deletions GitCommands/RevisionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public sealed class RevisionReader : IDisposable

LatestRefs = module.GetRefs();
UpdateSelectedRef(module, LatestRefs, branchName);
var refsByObjectId = LatestRefs.ToLookup(head => head.Guid);
var refsByObjectId = LatestRefs.ToLookup(head => head.ObjectId);

token.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -144,8 +144,8 @@ public sealed class RevisionReader : IDisposable
revision.Body = null;
}

// Look up any refs associate with this revision
revision.Refs = refsByObjectId[revision.Guid].AsReadOnlyList();
// Look up any refs associated with this revision
revision.Refs = refsByObjectId[revision.ObjectId].AsReadOnlyList();

RevisionCount++;

Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/FormStash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void StashedSelectedIndexChanged(object sender, EventArgs e)
{
View.ViewTextAsync(
stashedItem.Name,
LocalizationHelpers.GetSubmoduleText(Module, stashedItem.Name, stashedItem.TreeGuid));
LocalizationHelpers.GetSubmoduleText(Module, stashedItem.Name, stashedItem.TreeGuid?.ToString()));
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/RevisionFileTreeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void tvGitTree_AfterSelect(object sender, TreeViewEventArgs e)
{
case GitObjectType.Blob:
{
FileText.ViewGitItemAsync(gitItem.FileName, gitItem.Guid);
FileText.ViewGitItemAsync(gitItem.FileName, gitItem.ObjectId);
break;
}

Expand Down
17 changes: 10 additions & 7 deletions GitUI/Editor/FileViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,27 +512,29 @@ public Task ViewGitItemRevisionAsync(string fileName, ObjectId objectId)
else
{
// Retrieve blob, same as GitItemStatus.TreeGuid
string blob = Module.GetFileBlobHash(fileName, objectId);
var blob = Module.GetFileBlobHash(fileName, objectId);
return ViewGitItemAsync(fileName, blob);
}
}

public Task ViewGitItemAsync(string fileName, string guid)
public Task ViewGitItemAsync(string fileName, [CanBeNull] ObjectId objectId)
{
var sha = objectId?.ToString();

return ViewItemAsync(
fileName,
getImage: GetImage,
getFileText: GetFileTextIfBlobExists,
getSubmoduleText: () => LocalizationHelpers.GetSubmoduleText(Module, fileName.TrimEnd('/'), guid),
openWithDifftool: () => Module.OpenWithDifftool(fileName, firstRevision: guid));
getSubmoduleText: () => LocalizationHelpers.GetSubmoduleText(Module, fileName.TrimEnd('/'), sha),
openWithDifftool: () => Module.OpenWithDifftool(fileName, firstRevision: sha));

string GetFileTextIfBlobExists() => guid != "" ? Module.GetFileText(guid, Encoding) : "";
string GetFileTextIfBlobExists() => sha != null ? Module.GetFileText(sha, Encoding) : "";

Image GetImage()
{
try
{
using (var stream = Module.GetFileStream(guid))
using (var stream = Module.GetFileStream(sha))
{
return CreateImage(fileName, stream);
}
Expand Down Expand Up @@ -596,7 +598,8 @@ private Task ViewItemAsync(string fileName, Func<Image> getImage, Func<string> g
}
}

private static Image CreateImage(string fileName, Stream stream)
[NotNull]
private static Image CreateImage([NotNull] string fileName, [NotNull] Stream stream)
{
if (IsIcon())
{
Expand Down

0 comments on commit a94a30a

Please sign in to comment.