Use wcsncmp instead of Find for StartsWith checks
[TortoiseGit.git] / src / TortoiseProc / Commands / CloneCommand.cpp
blob443720618920c05416daec53de29164758961b50
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - TortoiseGit
4 // Copyright (C) 2012 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "CloneCommand.h"
23 #include "GitProgressDlg.h"
25 #include "CloneDlg.h"
26 #include "ProgressDlg.h"
27 #include "AppUtils.h"
28 #include "UnicodeUtils.h"
29 #include "SysProgressDlg.h"
30 #include "ProgressCommands/CloneProgressCommand.h"
32 static CString GetExistingDirectoryForClone(CString path)
34 if (PathFileExists(path))
35 return path;
36 int index = path.ReverseFind('\\');
37 while (index >= 0 && path.GetLength() >= 3)
39 if (PathFileExists(path.Left(index)))
41 if (index == 2 && path[1] == _T(':'))
42 return path.Left(index + 1);
43 return path.Left(index);
45 path = path.Left(index);
46 index = path.ReverseFind('\\');
48 GetTempPath(path);
49 return path;
52 static void StorePuttyKey(const CString& repoRoot, const CString& remote, const CString& keyFile)
54 CAutoRepository repo(repoRoot);
55 CAutoConfig config;
56 CString configName;
57 if (!repo)
58 goto error;
60 if (git_repository_config(config.GetPointer(), repo))
61 goto error;
63 configName.Format(_T("remote.%s.puttykeyfile"), (LPCTSTR)remote);
65 if (git_config_set_string(config, CUnicodeUtils::GetUTF8(configName), CUnicodeUtils::GetUTF8(keyFile)))
66 goto error;
68 return;
70 error:
71 MessageBox(hWndExplorer, CGit::GetLibGit2LastErr(L"Could not open repository"), _T("TortoiseGit"), MB_ICONERROR);
74 bool CloneCommand::Execute()
76 CTGitPath cloneDirectory;
77 if (!parser.HasKey(_T("hasurlhandler")))
79 if (orgCmdLinePath.IsEmpty())
81 cloneDirectory.SetFromWin(sOrigCWD, true);
82 DWORD len = ::GetTempPath(0, nullptr);
83 auto tszPath = std::make_unique<TCHAR[]>(len);
84 ::GetTempPath(len, tszPath.get());
85 if (_tcsncicmp(cloneDirectory.GetWinPath(), tszPath.get(), len-2 /* \\ and \0 */) == 0)
87 // if the current directory is set to a temp directory,
88 // we don't use that but leave it empty instead.
89 cloneDirectory.Reset();
92 else
93 cloneDirectory = orgCmdLinePath;
96 CCloneDlg dlg;
97 dlg.m_Directory = cloneDirectory.GetWinPathString();
99 if (parser.HasKey(_T("url")))
100 dlg.m_URL = parser.GetVal(_T("url"));
101 if (parser.HasKey(_T("exactpath")))
102 dlg.m_bExactPath = TRUE;
104 if(dlg.DoModal()==IDOK)
106 CString recursiveStr;
107 if(dlg.m_bRecursive)
108 recursiveStr = _T(" --recursive");
110 CString bareStr;
111 if(dlg.m_bBare)
112 bareStr = _T(" --bare");
114 CString nocheckoutStr;
115 if (dlg.m_bNoCheckout)
116 nocheckoutStr = _T(" --no-checkout");
118 CString branchStr;
119 if (dlg.m_bBranch)
120 branchStr = _T(" --branch ") + dlg.m_strBranch;
122 CString originStr;
123 if (dlg.m_bOrigin && !dlg.m_bSVN)
124 originStr = _T(" --origin ") + dlg.m_strOrigin;
126 if(dlg.m_bAutoloadPuttyKeyFile)
127 CAppUtils::LaunchPAgent(&dlg.m_strPuttyKeyFile);
129 CAppUtils::RemoveTrailSlash(dlg.m_Directory);
130 if (!dlg.m_bSVN)
131 CAppUtils::RemoveTrailSlash(dlg.m_URL);
133 CString dir=dlg.m_Directory;
134 CString url=dlg.m_URL;
136 // is this a windows format UNC path, ie starts with \\?
137 if (wcsncmp(url, L"\\\\", 2) == 0)
139 // yes, change all \ to /
140 // this should not be necessary but msysgit does not support the use \ here yet
141 int atSign = url.Find(_T('@'));
142 if (atSign > 0)
144 CString path = url.Mid(atSign);
145 path.Replace(_T('\\'), _T('/'));
146 url = url.Mid(0, atSign) + path;
148 else
149 url.Replace( _T('\\'), _T('/'));
152 CString depth;
153 if (dlg.m_bDepth)
154 depth.Format(_T(" --depth %d"),dlg.m_nDepth);
156 CString cmd;
157 cmd.Format(_T("git.exe clone --progress%s%s%s%s%s -v%s \"%s\" \"%s\""),
158 (LPCTSTR)nocheckoutStr,
159 (LPCTSTR)recursiveStr,
160 (LPCTSTR)bareStr,
161 (LPCTSTR)branchStr,
162 (LPCTSTR)originStr,
163 (LPCTSTR)depth,
164 (LPCTSTR)url,
165 (LPCTSTR)dir);
167 bool retry = false;
168 auto postCmdCallback = [&](DWORD status, PostCmdList& postCmdList)
170 if (status)
172 postCmdList.emplace_back(IDI_REFRESH, IDS_MSGBOX_RETRY, [&]{ retry = true; });
173 return;
176 // After cloning, change current directory to the cloned directory
177 g_Git.m_CurrentDir = dlg.m_Directory;
178 if (dlg.m_bAutoloadPuttyKeyFile) // do this here, since it might be needed for actions performed in Log
179 StorePuttyKey(dlg.m_Directory, dlg.m_bOrigin && !dlg.m_strOrigin.IsEmpty() ? dlg.m_strOrigin : _T("origin"), dlg.m_strPuttyKeyFile);
181 postCmdList.emplace_back(IDI_LOG, IDS_MENULOG, [&]
183 CString cmd = _T("/command:log");
184 cmd += _T(" /path:\"") + dlg.m_Directory + _T("\"");
185 CAppUtils::RunTortoiseGitProc(cmd);
188 postCmdList.emplace_back(IDI_EXPLORER, IDS_STATUSLIST_CONTEXT_EXPLORE, [&]{ CAppUtils::ExploreTo(hWndExplorer, dlg.m_Directory); });
191 // Handle Git SVN-clone
192 if(dlg.m_bSVN)
194 //g_Git.m_CurrentDir=dlg.m_Directory;
195 cmd.Format(_T("git.exe svn clone \"%s\" \"%s\""),
196 (LPCTSTR)url, (LPCTSTR)dlg.m_Directory);
198 if (dlg.m_bOrigin)
200 CString str;
201 if (dlg.m_strOrigin.IsEmpty())
202 str = _T(" --prefix \"\"");
203 else
204 str.Format(_T(" --prefix \"%s/\""), (LPCTSTR)dlg.m_strOrigin);
205 cmd += str;
208 if(dlg.m_bSVNTrunk)
209 cmd+=_T(" -T ")+dlg.m_strSVNTrunk;
211 if(dlg.m_bSVNBranch)
212 cmd+=_T(" -b ")+dlg.m_strSVNBranchs;
214 if(dlg.m_bSVNTags)
215 cmd+=_T(" -t ")+dlg.m_strSVNTags;
217 if(dlg.m_bSVNFrom)
219 CString str;
220 str.Format(_T("%d:HEAD"),dlg.m_nSVNFrom);
221 cmd+=_T(" -r ")+str;
224 if(dlg.m_bSVNUserName)
226 cmd+= _T(" --username ");
227 cmd+=dlg.m_strUserName;
230 else
232 if (g_Git.UsingLibGit2(CGit::GIT_CMD_CLONE))
234 while (true)
236 retry = false;
237 CGitProgressDlg GitDlg;
238 CTGitPathList list;
239 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
240 list.AddPath(CTGitPath(dir));
241 CloneProgressCommand cloneProgressCommand;
242 GitDlg.SetCommand(&cloneProgressCommand);
243 cloneProgressCommand.m_PostCmdCallback = postCmdCallback;
244 cloneProgressCommand.SetUrl(url);
245 cloneProgressCommand.SetPathList(list);
246 cloneProgressCommand.SetIsBare(dlg.m_bBare == TRUE);
247 if (dlg.m_bBranch)
248 cloneProgressCommand.SetRefSpec(dlg.m_strBranch);
249 if (dlg.m_bOrigin)
250 cloneProgressCommand.SetRemote(dlg.m_strOrigin);
251 cloneProgressCommand.SetNoCheckout(dlg.m_bNoCheckout == TRUE);
252 GitDlg.DoModal();
253 if (!retry)
254 return !GitDlg.DidErrorsOccur();
259 while (true)
261 retry = false;
262 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
263 CProgressDlg progress;
264 progress.m_GitCmd=cmd;
265 progress.m_PostCmdCallback = postCmdCallback;
266 INT_PTR ret = progress.DoModal();
268 if (!retry)
269 return ret == IDOK;
272 return FALSE;