Skip to content

Commit

Permalink
RangeDiff: Commit count in name
Browse files Browse the repository at this point in the history
Commit hash and branch names are too long to read
  • Loading branch information
gerhardol committed Nov 28, 2020
1 parent e0d47d8 commit 9bdef2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
23 changes: 21 additions & 2 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,11 @@ private IGitItem GetSubmoduleCommitHash(string filename, string refName)
return null;
}

public int? GetCommitDiffCount(string parentHash, string childHash)
public int? GetCommitDiffCount(ObjectId baseId, ObjectId parentId)
{
var args = new GitArgumentBuilder("rev-list")
{
$"{parentHash}...{childHash}",
$"{baseId} {parentId}",
"--count"
};
var output = _gitExecutable.GetOutput(args);
Expand All @@ -835,6 +835,25 @@ private IGitItem GetSubmoduleCommitHash(string filename, string refName)
return null;
}

public (int? first, int? second) GetCommitRangeDiffCount(ObjectId firstId, ObjectId secondId)
{
var args = new GitArgumentBuilder("rev-list")
{
$"{firstId}...{secondId}",
"--count",
"--left-right"
};
var output = _gitExecutable.GetOutput(args);

var counts = output.Split('\t');
if (counts.Length == 2 && int.TryParse(counts[0], out var first) && int.TryParse(counts[1], out var second))
{
return (first, second);
}

return (null, null);
}

public string GetCommitCountString(string from, string to)
{
int? removed = GetCommitCount(from, to);
Expand Down
31 changes: 16 additions & 15 deletions GitUI/UserControls/FileStatusDiffCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ public FileStatusDiffCalculator(Func<GitModule> getModule)
statuses: commonBaseToAandB));

// Add rangeDiff as a separate group (range is not the same as diff with artificial commits)
List<GitItemStatus> statuses = new List<GitItemStatus> { new GitItemStatus { Name = Strings.DiffRange, IsRangeDiff = true } };

var desc = Strings.DiffRange + ": " + GetDescriptionForRevision(describeRevision, firstRevHead) + " -> " +
GetDescriptionForRevision(describeRevision, selectedRevHead);
var statuses = new List<GitItemStatus> { new GitItemStatus { Name = Strings.DiffRange, IsRangeDiff = true } };
var first = firstRev.ObjectId == firstRevHead ? firstRev : new GitRevision(firstRevHead);
var selected = selectedRev.ObjectId == selectedRevHead ? selectedRev : new GitRevision(selectedRevHead);
var (baseToFirstCount, baseToSecondCount) = module.GetCommitRangeDiffCount(first.ObjectId, selected.ObjectId);
const int rangeDiffCommitLimit = 100;
var desc = $"{Strings.DiffRange} {baseToFirstCount ?? rangeDiffCommitLimit}{baseToSecondCount ?? rangeDiffCommitLimit}";

var rangeDiff = new FileStatusWithDescription(
firstRev: first,
secondRev: selected,
Expand All @@ -185,25 +186,25 @@ public FileStatusDiffCalculator(Func<GitModule> getModule)

// Git range-diff has cubic runtime complexity and can be slow and memory consuming, so just skip if diff is large
// to avoid that GE seem to hang when selecting the range diff
const int maxRangeDiffCommits = 100;
int count = (baseA == null || baseB == null
? module.GetCommitDiffCount(firstRevHead.ToString(), selectedRevHead.ToString())
: module.GetCommitDiffCount(baseA.ToString(), firstRevHead.ToString())
+ module.GetCommitDiffCount(baseB.ToString(), selectedRevHead.ToString()))
?? maxRangeDiffCommits + 1;
if (!GitVersion.Current.SupportRangeDiffTool || count > maxRangeDiffCommits)
? baseToFirstCount + baseToSecondCount
: module.GetCommitDiffCount(baseA, firstRevHead)
+ module.GetCommitDiffCount(baseB, selectedRevHead))
?? rangeDiffCommitLimit;
if (!GitVersion.Current.SupportRangeDiffTool || count >= rangeDiffCommitLimit)
{
var range = baseA is null || baseB is null
? $"{first.ObjectId}...{selected.ObjectId}"
: $"{baseA}..{first.ObjectId} {baseB}..{selected.ObjectId}";
statuses[0].IsStatusOnly = true;

// Message is not translated, considered as an error message
statuses[0].ErrorMessage = $"# The symmetric difference from {first.ObjectId.ToShortString()} to {selected.ObjectId.ToShortString()} is {count} which is higher than the limit {maxRangeDiffCommits}\n" +
"# Git range-diff may take a long time and Git Extensions seem to hang during execution, why the command is not executed\n" +
"# You can still run the command in a Git terminal\n" +
"# Remove '--no-patch' to see changes to files too\n" +
$"git range-diff {range} --no-patch";
statuses[0].ErrorMessage =
$"# The symmetric difference from {first.ObjectId.ToShortString()} to {selected.ObjectId.ToShortString()} is {count} >= {rangeDiffCommitLimit}\n"
+ "# Git range-diff may take a long time and Git Extensions seem to hang during execution, why the command is not executed.\n"
+ "# You can still run the command in a Git terminal.\n"
+ "# Remove '--no-patch' to see changes to files too.\n"
+ $"git range-diff {range} --no-patch";
}

return fileStatusDescs;
Expand Down

0 comments on commit 9bdef2b

Please sign in to comment.