Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

[Version Control] Avoid creating .git if it already exists #7354

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@
using MonoDevelop.Ide.Projects;
using MonoDevelop.Ide.Templates;
using MonoDevelop.Core;

using System;

namespace MonoDevelop.VersionControl.Git
{
public class ProjectTemplateHandler : IVersionControlProjectTemplateHandler
{
{
public bool CanCreateRepository (FilePath filePath)
{
try {
return String.IsNullOrEmpty (LibGit2Sharp.Repository.Discover (filePath.ResolveLinks ()));
} catch (Exception ex) {
LoggingService.LogInternalError ("Discovering Git repository failed", ex);
return false;
}
}

public void Run (NewProjectConfiguration config)
{
if (config.UseGit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.IO;
using MonoDevelop.Core;
using MonoDevelop.Core.StringParsing;
using MonoDevelop.Ide.Templates;
using ProjectCreateParameters = MonoDevelop.Projects.ProjectCreateParameters;
Expand Down Expand Up @@ -87,6 +90,7 @@ public string Location {
set {
config.Location = value;
CheckIsValid ();
OnParentFolderChanged ();
}
}

Expand Down Expand Up @@ -178,12 +182,16 @@ public bool IsCreateProjectDirectoryInsideSolutionDirectoryEnabled {
get { return HasProjects && IsNewSolution && createProjectDirectoryInsideSolutionDirectoryEnabled; }
}

public bool IsGitIgnoreEnabled {
get { return config.UseGit && IsUseGitEnabled && gitIgnoreEnabled; }
public bool IsGitIgnoreEnabled {
get { return !GitIgnoreFileExistsInSolutionFolder (); }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests for this new logic?

There are some tests for the IsGitIgnoreEnabled flag in the NewProjectDialogTests.

Also should the IsUseGitEnabled check be removed here? That is used

Assert.IsFalse (controller.FinalConfiguration.IsGitIgnoreEnabled);


public bool IsUseGitEnabled { get; set; }

public bool GitIgnoreExists {
get { return GitIgnoreFileExistsInSolutionFolder () && GitIgnoreFileExistsInParentFolder (); }
}

public bool IsNewSolution {
get { return config.CreateSolution; }
}
Expand Down Expand Up @@ -231,15 +239,19 @@ public bool IsValid {
}
}

public event EventHandler IsValidChanged;
public event EventHandler IsValidChanged;
public event EventHandler ParentFolderChanged;

void OnIsValidChanged ()
{
if (IsValidChanged != null) {
IsValidChanged (this, new EventArgs ());
}
}

{
IsValidChanged?.Invoke (this, EventArgs.Empty);
}

void OnParentFolderChanged ()
{
ParentFolderChanged?.Invoke (this, EventArgs.Empty);
}

public void UpdateFromParameters ()
{
ProjectName = Parameters ["ProjectName"];
Expand All @@ -259,6 +271,36 @@ public void UpdateFromParameters ()
return Parameters.GetBoolValue (name);
}
return null;
}

public bool GitIgnoreFileExistsInSolutionFolder ()
{
FilePath solutionPath = config.SolutionLocation;
if (Location == solutionPath) {
FilePath gitIgnoreFilePath = solutionPath.Combine (".gitignore");
return File.Exists (gitIgnoreFilePath);
}
return false;
}

public bool GitIgnoreFileExistsInParentFolder ()
{
FilePath path = new FilePath (config.SolutionLocation);
while (!path.IsNullOrEmpty) {
if (File.Exists (path.Combine (".gitignore")))
return true;
path = path.ParentDirectory;
}
return false;
}

void GetAllParentDirectories (DirectoryInfo directoryToScan, ref Stack<DirectoryInfo> directories)
{
if (directoryToScan == null || directoryToScan.Name == directoryToScan.Root.Name)
return;

directories.Push (directoryToScan);
GetAllParentDirectories (directoryToScan.Parent, ref directories);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,17 @@ void OnLocationTextBoxChanged ()
{
projectConfiguration.Location = locationTextBox.Text;
projectFolderPreviewWidget.UpdateLocation ();
UpdateGitStatus ();
}

void UpdateGitStatus ()
{
useGitCheckBox.Sensitive = projectConfiguration.IsUseGitEnabled;
useGitCheckBox.Active = projectConfiguration.UseGit;
createGitIgnoreFileCheckBox.Sensitive = projectConfiguration.IsGitIgnoreEnabled;
createGitIgnoreFileCheckBox.Active = projectConfiguration.CreateGitIgnoreFile;
}

void ProjectNameTextInserted (object o, TextInsertedArgs args)
{
if (args.Text.IndexOf ('\r') >= 0) {
Expand Down Expand Up @@ -185,6 +194,7 @@ void OnCreateGitIgnoreFileCheckBoxClicked ()
void OnUseGitCheckBoxClicked ()
{
projectConfiguration.UseGit = useGitCheckBox.Active;
createGitIgnoreFileCheckBox.Active = projectConfiguration.CreateGitIgnoreFile;
createGitIgnoreFileCheckBox.Sensitive = projectConfiguration.IsGitIgnoreEnabled;
projectFolderPreviewWidget.ShowGitFolder ();
projectFolderPreviewWidget.ShowGitIgnoreFile ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,15 @@ void CreateFinalConfigurationPage ()
{
finalConfigurationPage = new FinalProjectConfigurationPage (projectConfiguration);
finalConfigurationPage.ParentFolder = ParentFolder;
finalConfigurationPage.IsUseGitEnabled = IsNewSolution && (versionControlHandler != null);
finalConfigurationPage.IsUseGitEnabled = IsNewSolution && (versionControlHandler != null);
finalConfigurationPage.ParentFolderChanged += (sender, e) => {
finalConfigurationPage.UseGit = versionControlHandler != null;
finalConfigurationPage.IsUseGitEnabled = IsNewSolution && versionControlHandler != null &&
versionControlHandler.CanCreateRepository (projectConfiguration.Location);
finalConfigurationPage.CreateGitIgnoreFile = versionControlHandler != null &&
!finalConfigurationPage.GitIgnoreFileExistsInSolutionFolder () &&
!finalConfigurationPage.GitIgnoreFileExistsInParentFolder ();
};
finalConfigurationPage.IsValidChanged += (sender, e) => {
dialog.CanMoveToNextPage = finalConfigurationPage.IsValid;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
//
// IVersionControlTemplateHandler.cs
//
// Author:
// Matt Ward <matt.ward@xamarin.com>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//
// IVersionControlTemplateHandler.cs
//
// Author:
// Matt Ward <matt.ward@xamarin.com>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using MonoDevelop.Core;
using MonoDevelop.Ide.Projects;

namespace MonoDevelop.Ide.Templates
{
public interface IVersionControlProjectTemplateHandler
{
{
bool CanCreateRepository (FilePath filePath);
void Run (NewProjectConfiguration config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ public void Git_NewSolution_FinalConfigurationPage (bool useGit, bool createGitI

controller.Show ();

bool gitIgnoreExists = controller.FinalConfiguration.GitIgnoreExists;

Assert.AreEqual (useGit, controller.FinalConfiguration.UseGit);
Assert.AreEqual (createGitIgnore, controller.FinalConfiguration.CreateGitIgnoreFile);
Assert.AreEqual (useGit, controller.FinalConfiguration.IsGitIgnoreEnabled);
Assert.AreEqual (!gitIgnoreExists, controller.FinalConfiguration.IsGitIgnoreEnabled);
Assert.IsTrue (controller.FinalConfiguration.IsUseGitEnabled);
}

Expand Down