From 40e35d82df50cdb2aade25b449bd9cab7d87171f Mon Sep 17 00:00:00 2001 From: Sup Yut Sum Date: Fri, 24 Jan 2014 22:51:18 +0800 Subject: [PATCH] Warn when creating repository in special folder, such as Desktop Create Repository menu item is probably clicked by mistake, which is not supposed to do so in such folder Signed-off-by: Sup Yut Sum --- Languages/Tortoise.pot | 5 +++ src/Resources/TortoiseProcENG.rc | 2 ++ .../Commands/CreateRepositoryCommand.cpp | 37 ++++++++++++++++++++++ src/TortoiseProc/resource.h | 1 + 4 files changed, 45 insertions(+) diff --git a/Languages/Tortoise.pot b/Languages/Tortoise.pot index df748df3c..1ec4b92d9 100644 --- a/Languages/Tortoise.pot +++ b/Languages/Tortoise.pot @@ -8836,6 +8836,11 @@ msgstr "" msgid "The submodule \"%s\" is dirty.\nMerely committing the superproject cannot track or save such changes to the submodule.\nCommit the submodule now or ignore dirty changes?" msgstr "" +#. Resource IDs: (93) +#, c-format +msgid "The target folder \n%s\nis a special folder and is not supposed to be a repository root!\nAre you sure you want to initialize a git repository inside that folder?" +msgstr "" + #. Resource IDs: (88) #, c-format msgid "The target folder \n%s\nis not empty!\nAre you sure you want to initialize a git repository inside that folder?" diff --git a/src/Resources/TortoiseProcENG.rc b/src/Resources/TortoiseProcENG.rc index b48a773ad..b195ff364 100644 --- a/src/Resources/TortoiseProcENG.rc +++ b/src/Resources/TortoiseProcENG.rc @@ -4335,6 +4335,8 @@ BEGIN IDS_STATUSLIST_UNSETIGNORELOCALCHANGES "Unflag as skip-worktree and assume-unchanged" IDS_LOG_FILTER_TOGGLE "Toggle checked filters" + IDS_WARN_GITINIT_SPECIALFOLDER + "The target folder \n%s\nis a special folder and is not supposed to be a repository root!\nAre you sure you want to initialize a git repository inside that folder?" END STRINGTABLE diff --git a/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp b/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp index ad00316f9..b1003f6d3 100644 --- a/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp +++ b/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp @@ -25,9 +25,46 @@ #include "CreateRepoDlg.h" +bool CheckSpecialFolder(CString &folder) +{ + // Drive root + if (folder == "\\" || folder.GetLength() == 2 && folder[1] == ':' || folder.GetLength() == 3 && folder[1] == ':' && folder[2] == '\\') + return true; + + // UNC root + if (folder.GetLength() > 2 && folder.Left(2) == "\\\\") + { + int index = folder.Find('\\', 2); + if (index < 0) + return true; + else if (folder.GetLength() == index - 1) + return true; + } + + TCHAR path[MAX_PATH + 1]; + int code[] = { CSIDL_DESKTOPDIRECTORY, CSIDL_PROFILE, CSIDL_PERSONAL, CSIDL_WINDOWS, CSIDL_SYSTEM, CSIDL_PROGRAM_FILES, CSIDL_SYSTEMX86, CSIDL_PROGRAM_FILESX86 }; + for (int i = 0; i < _countof(code); i++) + { + path[0] = '\0'; + if (SUCCEEDED(SHGetFolderPath(nullptr, code[i], nullptr, 0, path))) + if (folder == path) + return true; + } + + return false; +} + bool CreateRepositoryCommand::Execute() { CString folder = this->orgCmdLinePath.GetWinPath(); + if (CheckSpecialFolder(folder)) + { + CString message; + message.Format(IDS_WARN_GITINIT_SPECIALFOLDER, folder); + if (CMessageBox::Show(hwndExplorer, message, _T("TortoiseGit"), 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)), CString(MAKEINTRESOURCE(IDS_PROCEEDBUTTON))) == 1) + return false; + } + CCreateRepoDlg dlg; dlg.m_folder = folder; if(dlg.DoModal() == IDOK) diff --git a/src/TortoiseProc/resource.h b/src/TortoiseProc/resource.h index 066d1fe76..7c12349f6 100644 --- a/src/TortoiseProc/resource.h +++ b/src/TortoiseProc/resource.h @@ -1018,6 +1018,7 @@ #define IDC_BUTTON_BROWSE_REF 1483 #define IDC_BUTTON_MANAGE 1483 #define IDC_KEEP_CR 1483 +#define IDS_WARN_GITINIT_SPECIALFOLDER 1483 #define IDC_CLONE_URL 1484 #define IDC_BUTTON_BROWSE_SOURCE_BRANCH2 1484 #define IDC_BUTTON_BROWSE_DEST_BRANCH 1484 -- 2.11.4.GIT