Honor renames in patch views
[TortoiseGit.git] / src / TortoiseProc / Commands / CreateRepositoryCommand.cpp
blob5279b0bea53b8a5c0aea6ddf3e29130945420ef2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2019, 2021, 2023 - 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"
24 #include "IconExtractor.h"
25 #include "CreateRepoDlg.h"
26 #include "SmartHandle.h"
27 #include "AppUtils.h"
29 static bool CheckSpecialFolder(const CString& folder)
31 // Drive or UNC root
32 if (PathIsRoot(folder) || PathIsUNCServer(folder))
33 return true;
35 static const GUID code[] = { FOLDERID_Desktop, FOLDERID_Profile, FOLDERID_Documents, FOLDERID_Windows, FOLDERID_System, FOLDERID_ProgramFiles, FOLDERID_SystemX86, FOLDERID_ProgramFilesX86 };
36 for (int i = 0; i < _countof(code); i++)
38 CComHeapPtr<WCHAR> pszPath;
39 if (SUCCEEDED(SHGetKnownFolderPath(code[i], 0, nullptr, &pszPath)) && CPathUtils::IsSamePath(folder, CString(pszPath)))
40 return true;
43 return false;
46 bool CreateRepositoryCommand::Execute()
48 CString folder = this->orgCmdLinePath.GetWinPath();
49 if (folder.IsEmpty())
50 folder = g_Git.m_CurrentDir;
51 if (folder.IsEmpty())
52 GetCurrentDirectory(MAX_PATH, CStrBuf(folder, MAX_PATH));
53 if (CheckSpecialFolder(folder))
55 CString message;
56 message.Format(IDS_WARN_GITINIT_SPECIALFOLDER, static_cast<LPCWSTR>(folder));
57 if (CMessageBox::Show(GetExplorerHWND(), message, L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)), CString(MAKEINTRESOURCE(IDS_PROCEEDBUTTON))) == 1)
58 return false;
61 CCreateRepoDlg dlg;
62 dlg.m_folder = folder;
63 if(dlg.DoModal() == IDOK)
65 CString message;
66 message.Format(IDS_WARN_GITINIT_FOLDERNOTEMPTY, static_cast<LPCWSTR>(folder));
67 if (dlg.m_bBare && PathIsDirectory(folder) && !PathIsDirectoryEmpty(folder) && CMessageBox::Show(GetExplorerHWND(), message, L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)), CString(MAKEINTRESOURCE(IDS_PROCEEDBUTTON))) == 1)
68 return false;
70 git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
71 options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
72 options.flags |= dlg.m_bBare ? GIT_REPOSITORY_INIT_BARE : 0;
73 CStringA envTemplateDir = CUnicodeUtils::GetUTF8(g_Git.m_Environment.GetEnv(L"GIT_TEMPLATE_DIR"));
74 if (!envTemplateDir.IsEmpty())
75 options.template_path = envTemplateDir;
76 CAutoRepository repo;
77 if (git_repository_init_ext(repo.GetPointer(), CUnicodeUtils::GetUTF8(folder), &options))
79 CMessageBox::Show(GetExplorerHWND(), CGit::GetLibGit2LastErr(L"Could not initialize a new repository."), L"TortoiseGit", MB_OK | MB_ICONERROR);
80 return false;
83 if (!dlg.m_bBare)
84 CShellUpdater::Instance().AddPathForUpdate(orgCmdLinePath);
85 else
86 CAppUtils::SetupBareRepoIcon(folder);
88 CString str;
89 str.Format(IDS_PROC_REPOCREATED, static_cast<LPCWSTR>(folder));
90 CMessageBox::Show(GetExplorerHWND(), str, L"TortoiseGit", MB_OK | MB_ICONINFORMATION);
91 return true;
93 return false;