Skip to content

Commit

Permalink
Add menu commands to edit .git/info/exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jul 15, 2016
1 parent 2b2b72d commit 5532e44
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 21 deletions.
29 changes: 27 additions & 2 deletions GitUI/CommandsDialogs/FormAddToGitIgnore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,44 @@ public sealed partial class FormAddToGitIgnore : GitModuleForm
private readonly TranslationString _matchingFilesString = new TranslationString("{0} file(s) matched");
private readonly TranslationString _updateStatusString = new TranslationString("Updating ...");

private readonly bool _localExclude;
private readonly AsyncLoader _ignoredFilesLoader;

public FormAddToGitIgnore(GitUICommands aCommands, params string[] filePatterns)
public FormAddToGitIgnore(GitUICommands aCommands, bool localExclude, params string[] filePatterns)
: base(aCommands)
{
InitializeComponent();
_localExclude = localExclude;
_ignoredFilesLoader = new AsyncLoader();
Translate();

if (filePatterns != null)
FilePattern.Text = string.Join(Environment.NewLine, filePatterns);
}

private string ExcludeFileRelative
{
get
{
if (!_localExclude)
{
return ".gitignore";
}
else
{
return Path.Combine(".git", "info", "exclude");
}
}
}

private string ExcludeFile
{
get
{
return Module.WorkingDir + ExcludeFileRelative;
}
}

private void AddToIgnoreClick(object sender, EventArgs e)
{
var patterns = GetCurrentPatterns().ToArray();
Expand All @@ -39,7 +64,7 @@ private void AddToIgnoreClick(object sender, EventArgs e)

try
{
var fileName = Module.WorkingDir + ".gitignore";
var fileName = ExcludeFile;
FileInfoExtensions.MakeFileTemporaryWritable(fileName, x =>
{
var gitIgnoreFileAddition = new StringBuilder();
Expand Down
18 changes: 18 additions & 0 deletions GitUI/CommandsDialogs/FormBrowse.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,12 @@ private void SaveUserMenuPosition()

private void EditGitignoreToolStripMenuItem1Click(object sender, EventArgs e)
{
UICommands.StartEditGitIgnoreDialog(this);
UICommands.StartEditGitIgnoreDialog(this, false);
}

private void EditGitInfoExcludeToolStripMenuItemClick(object sender, EventArgs e)
{
UICommands.StartEditGitIgnoreDialog(this, true);
}

private void SettingsToolStripMenuItem2Click(object sender, EventArgs e)
Expand Down
11 changes: 11 additions & 0 deletions GitUI/CommandsDialogs/FormCommit.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,13 @@ private void resetUnstagedChangesToolStripMenuItem_Click(object sender, EventArg

private void EditGitIgnoreToolStripMenuItemClick(object sender, EventArgs e)
{
UICommands.StartEditGitIgnoreDialog(this);
UICommands.StartEditGitIgnoreDialog(this, false);
Initialize();
}

private void EditGitInfoExcludeToolStripMenuItemClick(object sender, EventArgs e)
{
UICommands.StartEditGitIgnoreDialog(this, false);
Initialize();
}

Expand Down Expand Up @@ -1718,7 +1724,7 @@ private void AddFileTogitignoreToolStripMenuItemClick(object sender, EventArgs e

SelectedDiff.Clear();
var fileNames = Unstaged.SelectedItems.Select(item => item.Name).ToArray();
if (UICommands.StartAddToGitIgnoreDialog(this, fileNames))
if (UICommands.StartAddToGitIgnoreDialog(this, false, fileNames))
Initialize();
}

Expand Down
37 changes: 31 additions & 6 deletions GitUI/CommandsDialogs/FormGitIgnore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed partial class FormGitIgnore : GitModuleForm
private readonly TranslationString _saveFileQuestionCaption =
new TranslationString("Save changes?");

private bool _localExclude;
private string _originalGitIgnoreFileContent = string.Empty;

#region default patterns
Expand Down Expand Up @@ -62,13 +63,37 @@ public sealed partial class FormGitIgnore : GitModuleForm
};
#endregion

public FormGitIgnore(GitUICommands aCommands)
public FormGitIgnore(GitUICommands aCommands, bool localExclude)
: base(aCommands)
{
_localExclude = localExclude;
InitializeComponent();
Translate();
}

private string ExcludeFileRelative
{
get
{
if (!_localExclude)
{
return ".gitignore";
}
else
{
return Path.Combine(".git", "info", "exclude");
}
}
}

private string ExcludeFile
{
get
{
return Module.WorkingDir + ExcludeFileRelative;
}
}

protected override void OnRuntimeLoad(EventArgs e)
{
base.OnRuntimeLoad(e);
Expand All @@ -80,8 +105,8 @@ private void LoadGitIgnore()
{
try
{
if (File.Exists(Module.WorkingDir + ".gitignore"))
_NO_TRANSLATE_GitIgnoreEdit.ViewFile(Module.WorkingDir + ".gitignore");
if (File.Exists(ExcludeFile))
_NO_TRANSLATE_GitIgnoreEdit.ViewFile(ExcludeFile);
}
catch (Exception ex)
{
Expand All @@ -103,7 +128,7 @@ private bool SaveGitIgnore()
{
FileInfoExtensions
.MakeFileTemporaryWritable(
Module.WorkingDir + ".gitignore",
ExcludeFile,
x =>
{
var fileContent = _NO_TRANSLATE_GitIgnoreEdit.GetText();
Expand Down Expand Up @@ -164,7 +189,7 @@ private void AddDefaultClick(object sender, EventArgs e)
// workaround to prevent GitIgnoreFileLoaded event handling (it causes wrong _originalGitIgnoreFileContent update)
// TODO: implement in FileViewer separate events for loading text from file and for setting text directly via ViewText
_NO_TRANSLATE_GitIgnoreEdit.TextLoaded -= GitIgnoreFileLoaded;
_NO_TRANSLATE_GitIgnoreEdit.ViewText(".gitignore",
_NO_TRANSLATE_GitIgnoreEdit.ViewText(ExcludeFileRelative,
currentFileContent + Environment.NewLine +
string.Join(Environment.NewLine, patternsToAdd) + Environment.NewLine + string.Empty);
_NO_TRANSLATE_GitIgnoreEdit.TextLoaded += GitIgnoreFileLoaded;
Expand All @@ -173,7 +198,7 @@ private void AddDefaultClick(object sender, EventArgs e)
private void AddPattern_Click(object sender, EventArgs e)
{
SaveGitIgnore();
UICommands.StartAddToGitIgnoreDialog(this, "*.dll");
UICommands.StartAddToGitIgnoreDialog(this, _localExclude, "*.dll");
LoadGitIgnore();
}

Expand Down
15 changes: 7 additions & 8 deletions GitUI/GitUICommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,11 +1210,11 @@ public bool StartDeleteTagDialog()
return StartDeleteTagDialog(null, "");
}

public bool StartEditGitIgnoreDialog(IWin32Window owner)
public bool StartEditGitIgnoreDialog(IWin32Window owner, bool localExcludes)
{
Func<bool> action = () =>
{
using (var form = new FormGitIgnore(this))
using (var form = new FormGitIgnore(this, localExcludes))
form.ShowDialog(owner);
return true;
Expand All @@ -1223,17 +1223,16 @@ public bool StartEditGitIgnoreDialog(IWin32Window owner)
return DoActionOnRepo(owner, true, false, PreEditGitIgnore, PostEditGitIgnore, action);
}

public bool StartEditGitIgnoreDialog()
public bool StartEditGitIgnoreDialog(bool localExcludes)
{
return StartEditGitIgnoreDialog(null);
return StartEditGitIgnoreDialog(null, localExcludes);
}

public bool StartAddToGitIgnoreDialog(IWin32Window owner, params string[] filePattern)
public bool StartAddToGitIgnoreDialog(IWin32Window owner, bool localExclude, params string[] filePattern)
{

Func<bool> action = () =>
{
using (var frm = new FormAddToGitIgnore(this, filePattern))
using (var frm = new FormAddToGitIgnore(this, localExclude, filePattern))
frm.ShowDialog(owner);
return true;
Expand Down Expand Up @@ -1905,7 +1904,7 @@ private void RunCommandBasedOnArgument(string[] args, Dictionary<string, string>
Module.RunBash();
return;
case "gitignore":
StartEditGitIgnoreDialog();
StartEditGitIgnoreDialog(false);
return;
case "init": // [path]
RunInitCommand(args);
Expand Down
2 changes: 1 addition & 1 deletion GitUI/UserControls/RevisionGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ private void CommitClick(object sender, EventArgs e)

private void GitIgnoreClick(object sender, EventArgs e)
{
UICommands.StartEditGitIgnoreDialog(this);
UICommands.StartEditGitIgnoreDialog(this, false);
}

internal void InvalidateRevisions()
Expand Down
2 changes: 1 addition & 1 deletion Plugins/GitUIPluginInterfaces/IGitUICommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public interface IGitUICommands
bool StartCreateTagDialog();
bool StartDeleteBranchDialog(string branch);
bool StartDeleteTagDialog();
bool StartEditGitIgnoreDialog();
bool StartEditGitIgnoreDialog(bool localExcludes);
void StartFileHistoryDialog(string fileName);
bool StartFormatPatchDialog();
bool StartGitCommandProcessDialog(string arguments);
Expand Down

0 comments on commit 5532e44

Please sign in to comment.