Skip to content

Commit

Permalink
Highlighting of authored commits (PR #2672)
Browse files Browse the repository at this point in the history
Adds highlighting of commits by author. Commits are matched with current user's email address (if no commit is selected) or with email of author of currently selected commit. The feature can be configured in the 'Settings' dialog on the 'Colors' page, commits are highlighted with LighYellow color by default.
  • Loading branch information
tomasdeml committed Mar 11, 2015
1 parent 22cbadc commit 3ed5480
Show file tree
Hide file tree
Showing 18 changed files with 527 additions and 258 deletions.
10 changes: 10 additions & 0 deletions GitCommands/Config/SettingKeyString.cs
Expand Up @@ -16,5 +16,15 @@ public class SettingKeyString
/// "remote.{0}.url"
/// </summary>
public const string RemoteUrl = "remote.{0}.url";

/// <summary>
/// user.name
/// </summary>
public const string UserName = "user.name";

/// <summary>
/// user.email
/// </summary>
public const string UserEmail = "user.email";
}
}
37 changes: 37 additions & 0 deletions GitCommands/Git/AuthorEmailEqualityComparer.cs
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;

namespace GitCommands.Git
{
public sealed class AuthorEmailEqualityComparer : IEqualityComparer<GitRevision>, IEqualityComparer<string>
{
private static readonly AuthorEmailEqualityComparer CachedInstance = new AuthorEmailEqualityComparer();
public static AuthorEmailEqualityComparer Instance
{
get { return CachedInstance; }
}

public bool Equals(GitRevision x, GitRevision y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
return Equals(x.AuthorEmail, y.AuthorEmail);
}

public int GetHashCode(GitRevision revision)
{
return GetHashCode(revision.AuthorEmail);
}

public bool Equals(string firstAuthorEmail, string secondAuthorEmail)
{
return String.Equals(firstAuthorEmail, secondAuthorEmail, StringComparison.OrdinalIgnoreCase);
}

public int GetHashCode(string authorEmail)
{
return authorEmail != null ? authorEmail.GetHashCode() : 0;
}
}
}
1 change: 1 addition & 0 deletions GitCommands/GitCommands.csproj
Expand Up @@ -80,6 +80,7 @@
<Compile Include="DateTimeUtils.cs" />
<Compile Include="ExceptionUtils.cs" />
<Compile Include="FileHelper.cs" />
<Compile Include="Git\AuthorEmailEqualityComparer.cs" />
<Compile Include="Logging\CommandLogEntry.cs" />
<Compile Include="RemoteActionResult.cs" />
<Compile Include="Settings\BasicSettingTypes.cs" />
Expand Down
12 changes: 12 additions & 0 deletions GitCommands/Settings/AppSettings.cs
Expand Up @@ -873,6 +873,12 @@ public static Color DiffAddedExtraColor
set { SetColor("diffaddedextracolor", value); }
}

public static Color AuthoredRevisionsColor
{
get { return GetColor("authoredrevisionscolor", Color.LightYellow); }
set { SetColor("authoredrevisionscolor", value); }
}

public static Font DiffFont
{
get { return GetFont("difffont", new Font("Courier New", 10)); }
Expand Down Expand Up @@ -911,6 +917,12 @@ public static bool BranchBorders
set { SetBool("branchborders", value); }
}

public static bool HighlightAuthoredRevisions
{
get { return GetBool("highlightauthoredrevisions", true); }
set { SetBool("highlightauthoredrevisions", value); }
}

public static string LastFormatPatchDir
{
get { return GetString("lastformatpatchdir", ""); }
Expand Down
1 change: 1 addition & 0 deletions GitExtensionsTest/GitExtensionsTest.csproj
Expand Up @@ -64,6 +64,7 @@
<Compile Include="GitCommands\Helpers\PathUtilTest.cs" />
<Compile Include="GitCommands\Patch\PatchManagerTest.cs" />
<Compile Include="GitCommands\Patch\PatchTest.cs" />
<Compile Include="GitUI\AuthorEmailBasedRevisionHighlightingFixture.cs" />
<Compile Include="GitUI\FollowParentRewriterFixture.cs" />
<Compile Include="GitUI\FormUpdateFixture.cs" />
<Compile Include="GitUI\HotkeySettingsManagerFixture.cs" />
Expand Down
168 changes: 168 additions & 0 deletions GitExtensionsTest/GitUI/AuthorEmailBasedRevisionHighlightingFixture.cs
@@ -0,0 +1,168 @@
using System;
using System.IO;
using FluentAssertions;
using GitCommands;
using GitCommands.Config;
using GitUI.UserControls;
using NUnit.Framework;

namespace GitExtensionsTest.GitUI
{
[TestFixture]
class AuthorEmailBasedRevisionHighlightingFixture
{
private const string ExpectedAuthorEmail1 = "doe1@example.org";
private const string ExpectedAuthorEmail2 = "doe2@example.org";

[Test]
public void AuthorEmailToHighlight_should_be_null_when_no_revision_change_processed_yet()
{
var sut = new AuthorEmailBasedRevisionHighlighting();

sut.AuthorEmailToHighlight.Should().BeNull();
}

[Test]
public void When_multiple_revisions_selected_Then_ProcessSelectionChange_should_return_NoAction()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();

var action = sut.ProcessRevisionSelectionChange(currentModule,
new[]
{
NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1),
NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1)
});

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.NoAction);
}

[Test]
public void Given_previously_selected_revision_When_multiple_revisions_selected_Then_AuthorEmailToHighlight_should_not_change()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
sut.ProcessRevisionSelectionChange(currentModule,
new[] {NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1)});

sut.ProcessRevisionSelectionChange(currentModule,
new[]
{
NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail2),
NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1)
});

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail1);
}

[Test]
public void Given_no_previously_selected_revision_When_single_revision_selected_Then_ProcessSelectionChange_should_return_RefreshUserInterface()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();

var action = sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.RefreshUserInterface);
}

[Test]
public void Given_no_previously_selected_revision_When_single_revision_selected_Then_AuthorEmailToHighlight_should_change()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();

sut.ProcessRevisionSelectionChange(currentModule, new[] {NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1)});

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail1);
}

[Test]
public void Given_previously_selected_revision_When_single_revision_with_same_author_email_selected_Then_ProcessSelectionChange_should_return_NoAction()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

var action = sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.NoAction);
}

[Test]
public void Given_previously_selected_revision_When_single_revision_with_same_author_email_selected_Then_AuthorEmailToHighlight_should_not_change()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail1);
}

[Test]
public void Given_previously_selected_revision_When_single_revision_with_different_author_email_selected_Then_ProcessSelectionChange_should_return_RefreshUserInterface()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

var action = sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail2) });

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.RefreshUserInterface);
}

[Test]
public void Given_previously_selected_revision_When_single_revision_with_different_author_email_selected_Then_AuthorEmailToHighlight_should_change()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail2) });

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail2);
}

[Test]
public void Given_previously_selected_revision_When_no_revision_selected_Then_ProcessSelectionChange_should_return_RefreshUserInterface()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
currentModule.SetSetting(SettingKeyString.UserEmail, ExpectedAuthorEmail2);
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

var action = sut.ProcessRevisionSelectionChange(currentModule, new GitRevision[0]);

action.Should().Be(AuthorEmailBasedRevisionHighlighting.SelectionChangeAction.RefreshUserInterface);
}

[Test]
public void Given_previously_selected_revision_When_no_revision_selected_Then_AuthorEmailToHighlight_should_be_value_of_current_user_email_setting()
{
var sut = new AuthorEmailBasedRevisionHighlighting();
var currentModule = NewModule();
currentModule.SetSetting(SettingKeyString.UserEmail, ExpectedAuthorEmail2);
sut.ProcessRevisionSelectionChange(currentModule, new[] { NewRevisionWithAuthorEmail(currentModule, ExpectedAuthorEmail1) });

sut.ProcessRevisionSelectionChange(currentModule, new GitRevision[0]);

sut.AuthorEmailToHighlight.Should().Be(ExpectedAuthorEmail2);
}

private static GitModule NewModule()
{
return new GitModule(Path.GetTempPath());
}

private static GitRevision NewRevisionWithAuthorEmail(GitModule currentModule, string authorEmail)
{
return new GitRevision(currentModule, Guid.NewGuid().ToString())
{
AuthorEmail = authorEmail
};
}
}
}
8 changes: 3 additions & 5 deletions GitUI/CommandsDialogs/FormCommit.cs
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using GitCommands;
using GitCommands.Config;
using GitCommands.Utils;
using GitUI.AutoCompletion;
using GitUI.CommandsDialogs.CommitDialog;
Expand All @@ -25,9 +26,6 @@ namespace GitUI.CommandsDialogs
{
public sealed partial class FormCommit : GitModuleForm //, IHotkeyable
{
const string UserNameKey = "user.name";
const string UserEmailKey = "user.email";

#region Translation
private readonly TranslationString _amendCommit =
new TranslationString("You are about to rewrite history." + Environment.NewLine +
Expand Down Expand Up @@ -1852,8 +1850,8 @@ private void updateAuthorInfo()

private void GetUserSettings()
{
_userName = Module.GetEffectiveSetting(UserNameKey);
_userEmail = Module.GetEffectiveSetting(UserEmailKey);
_userName = Module.GetEffectiveSetting(SettingKeyString.UserName);
_userEmail = Module.GetEffectiveSetting(SettingKeyString.UserEmail);



Expand Down
3 changes: 2 additions & 1 deletion GitUI/CommandsDialogs/FormFormatPatch.cs
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mail;
using System.Windows.Forms;
using GitCommands;
using GitCommands.Config;
using GitUI.CommandsDialogs.FormatPatchDialog;
using ResourceManager;

Expand Down Expand Up @@ -44,7 +45,7 @@ public FormFormatPatch(GitUICommands aCommands)
InitializeComponent();
Translate();
if (aCommands != null)
MailFrom.Text = Module.GetEffectiveSetting("user.email");
MailFrom.Text = Module.GetEffectiveSetting(SettingKeyString.UserEmail);
}

private void Browse_Click(object sender, EventArgs e)
Expand Down
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Windows.Forms;
using GitCommands;
using GitCommands.Config;
using GitCommands.Utils;
using Microsoft.Win32;
using ResourceManager;
Expand Down Expand Up @@ -633,8 +634,8 @@ private bool CheckMergeTool()
private bool CheckGlobalUserSettingsValid()
{
UserNameSet.Visible = true;
if (string.IsNullOrEmpty(GetGlobalSetting("user.name")) ||
string.IsNullOrEmpty(GetGlobalSetting("user.email")))
if (string.IsNullOrEmpty(GetGlobalSetting(SettingKeyString.UserName)) ||
string.IsNullOrEmpty(GetGlobalSetting(SettingKeyString.UserEmail)))
{
UserNameSet.BackColor = Color.LightSalmon;
UserNameSet.Text = _noEmailSet.Text;
Expand Down

0 comments on commit 3ed5480

Please sign in to comment.