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.
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
)
30 if (folder
== "\\" || folder
.GetLength() == 2 && folder
[1] == ':' || folder
.GetLength() == 3 && folder
[1] == ':' && folder
[2] == '\\')
34 if (folder
.GetLength() > 2 && CStringUtils::StartsWith(folder
, L
"\\\\"))
36 int index
= folder
.Find('\\', 2);
39 else if (folder
.GetLength() == index
- 1)
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
++)
48 if (SUCCEEDED(SHGetFolderPath(nullptr, code
[i
], nullptr, 0, path
)))
56 bool CreateRepositoryCommand::Execute()
58 CString folder
= this->orgCmdLinePath
.GetWinPath();
60 folder
= g_Git
.m_CurrentDir
;
62 GetCurrentDirectory(MAX_PATH
, CStrBuf(folder
, MAX_PATH
));
63 if (CheckSpecialFolder(folder
))
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)
72 dlg
.m_folder
= folder
;
73 if(dlg
.DoModal() == IDOK
)
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)
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;
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
);
91 CShellUpdater::Instance().AddPathForUpdate(orgCmdLinePath
);
93 str
.Format(IDS_PROC_REPOCREATED
, (LPCTSTR
)folder
);
94 CMessageBox::Show(hwndExplorer
, str
, L
"TortoiseGit", MB_OK
| MB_ICONINFORMATION
);