Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / TortoiseProc / Commands / CreateRepositoryCommand.cpp
blob86f514f554f8e759ac55d9618349ee6937877f55
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2017 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "CreateRepositoryCommand.h"
21 #include "ShellUpdater.h"
22 #include "MessageBox.h"
23 #include "UnicodeUtils.h"
25 #include "CreateRepoDlg.h"
27 static bool CheckSpecialFolder(CString &folder)
29 // Drive root
30 if (folder == "\\" || folder.GetLength() == 2 && folder[1] == ':' || folder.GetLength() == 3 && folder[1] == ':' && folder[2] == '\\')
31 return true;
33 // UNC root
34 if (folder.GetLength() > 2 && CStringUtils::StartsWith(folder, L"\\\\"))
36 int index = folder.Find('\\', 2);
37 if (index < 0)
38 return true;
39 else if (folder.GetLength() == index - 1)
40 return true;
43 TCHAR path[MAX_PATH + 1];
44 int code[] = { CSIDL_DESKTOPDIRECTORY, CSIDL_PROFILE, CSIDL_PERSONAL, CSIDL_WINDOWS, CSIDL_SYSTEM, CSIDL_PROGRAM_FILES, CSIDL_SYSTEMX86, CSIDL_PROGRAM_FILESX86 };
45 for (int i = 0; i < _countof(code); i++)
47 path[0] = L'\0';
48 if (SUCCEEDED(SHGetFolderPath(nullptr, code[i], nullptr, 0, path)))
49 if (folder == path)
50 return true;
53 return false;
56 bool CreateRepositoryCommand::Execute()
58 CString folder = this->orgCmdLinePath.GetWinPath();
59 if (folder.IsEmpty())
60 folder = g_Git.m_CurrentDir;
61 if (folder.IsEmpty())
62 GetCurrentDirectory(MAX_PATH, CStrBuf(folder, MAX_PATH));
63 if (CheckSpecialFolder(folder))
65 CString message;
66 message.Format(IDS_WARN_GITINIT_SPECIALFOLDER, (LPCTSTR)folder);
67 if (CMessageBox::Show(hwndExplorer, message, L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)), CString(MAKEINTRESOURCE(IDS_PROCEEDBUTTON))) == 1)
68 return false;
71 CCreateRepoDlg dlg;
72 dlg.m_folder = folder;
73 if(dlg.DoModal() == IDOK)
75 CString message;
76 message.Format(IDS_WARN_GITINIT_FOLDERNOTEMPTY, (LPCTSTR)folder);
77 if (dlg.m_bBare && PathIsDirectory(folder) && !PathIsDirectoryEmpty(folder) && CMessageBox::Show(hwndExplorer, message, L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)), CString(MAKEINTRESOURCE(IDS_PROCEEDBUTTON))) == 1)
78 return false;
80 git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
81 options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
82 options.flags |= dlg.m_bBare ? GIT_REPOSITORY_INIT_BARE : 0;
83 CAutoRepository repo;
84 if (git_repository_init_ext(repo.GetPointer(), CUnicodeUtils::GetUTF8(folder), &options))
86 CMessageBox::Show(hwndExplorer, CGit::GetLibGit2LastErr(L"Could not initialize a new repository."), L"TortoiseGit", MB_OK | MB_ICONERROR);
87 return false;
90 if (!dlg.m_bBare)
91 CShellUpdater::Instance().AddPathForUpdate(orgCmdLinePath);
92 CString str;
93 str.Format(IDS_PROC_REPOCREATED, (LPCTSTR)folder);
94 CMessageBox::Show(hwndExplorer, str, L"TortoiseGit", MB_OK | MB_ICONINFORMATION);
95 return true;
97 return false;