No need to explicitly initialize CString and use Empty() for clearing CString
[TortoiseGit.git] / src / TortoiseProc / Commands / CloneCommand.cpp
blobafc0bf8e2c92728cb93cf1953588cb3279e825d6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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& keyFile)
54 CAutoRepository repo(repoRoot);
55 CAutoConfig config;
56 if (!repo)
57 goto error;
59 if (git_repository_config(config.GetPointer(), repo))
60 goto error;
62 if (git_config_set_string(config, "remote.origin.puttykeyfile", CUnicodeUtils::GetUTF8(keyFile)))
63 goto error;
65 return;
67 error:
68 MessageBox(hWndExplorer, CGit::GetLibGit2LastErr(L"Could not open repository"), _T("TortoiseGit"), MB_ICONERROR);
71 bool CloneCommand::Execute()
73 CTGitPath cloneDirectory;
74 if (!parser.HasKey(_T("hasurlhandler")))
76 if (orgCmdLinePath.IsEmpty())
78 cloneDirectory.SetFromWin(sOrigCWD, true);
79 DWORD len = ::GetTempPath(0, NULL);
80 std::unique_ptr<TCHAR[]> tszPath(new TCHAR[len]);
81 ::GetTempPath(len, tszPath.get());
82 if (_tcsncicmp(cloneDirectory.GetWinPath(), tszPath.get(), len-2 /* \\ and \0 */) == 0)
84 // if the current directory is set to a temp directory,
85 // we don't use that but leave it empty instead.
86 cloneDirectory.Reset();
89 else
90 cloneDirectory = orgCmdLinePath;
93 CCloneDlg dlg;
94 dlg.m_Directory = cloneDirectory.GetWinPathString();
96 if (parser.HasKey(_T("url")))
97 dlg.m_URL = parser.GetVal(_T("url"));
98 if (parser.HasKey(_T("exactpath")))
99 dlg.m_bExactPath = TRUE;
101 if(dlg.DoModal()==IDOK)
103 CString recursiveStr;
104 if(dlg.m_bRecursive)
105 recursiveStr = _T(" --recursive");
107 CString bareStr;
108 if(dlg.m_bBare)
109 bareStr = _T(" --bare");
111 CString nocheckoutStr;
112 if (dlg.m_bNoCheckout)
113 nocheckoutStr = _T(" --no-checkout");
115 CString branchStr;
116 if (dlg.m_bBranch)
117 branchStr = _T(" --branch ") + dlg.m_strBranch;
119 CString originStr;
120 if (dlg.m_bOrigin && !dlg.m_bSVN)
121 originStr = _T(" --origin ") + dlg.m_strOrigin;
123 if(dlg.m_bAutoloadPuttyKeyFile)
125 CAppUtils::LaunchPAgent(&dlg.m_strPuttyKeyFile);
128 CAppUtils::RemoveTrailSlash(dlg.m_Directory);
129 if (!dlg.m_bSVN)
130 CAppUtils::RemoveTrailSlash(dlg.m_URL);
132 CString dir=dlg.m_Directory;
133 CString url=dlg.m_URL;
135 // is this a windows format UNC path, ie starts with \\?
136 if (url.Find(_T("\\\\")) == 0)
138 // yes, change all \ to /
139 // this should not be necessary but msysgit does not support the use \ here yet
140 int atSign = url.Find(_T('@'));
141 if (atSign > 0)
143 CString path = url.Mid(atSign);
144 path.Replace(_T('\\'), _T('/'));
145 url = url.Mid(0, atSign) + path;
147 else
148 url.Replace( _T('\\'), _T('/'));
151 CString depth;
152 if (dlg.m_bDepth)
154 depth.Format(_T(" --depth %d"),dlg.m_nDepth);
157 CString cmd;
158 CString progressarg;
160 int ver = CAppUtils::GetMsysgitVersion();
162 if(ver >= 0x01070002) //above 1.7.0.2
163 progressarg = _T(" --progress");
165 cmd.Format(_T("git.exe clone%s%s%s%s%s%s -v%s \"%s\" \"%s\""),
166 (LPCTSTR)nocheckoutStr,
167 (LPCTSTR)recursiveStr,
168 (LPCTSTR)bareStr,
169 (LPCTSTR)branchStr,
170 (LPCTSTR)originStr,
171 (LPCTSTR)progressarg,
172 (LPCTSTR)depth,
173 (LPCTSTR)url,
174 (LPCTSTR)dir);
176 bool retry = false;
177 auto postCmdCallback = [&](DWORD status, PostCmdList& postCmdList)
179 if (status)
181 postCmdList.push_back(PostCmd(IDI_REFRESH, IDS_MSGBOX_RETRY, [&] { retry = true; }));
182 return;
185 // After cloning, change current directory to the cloned directory
186 g_Git.m_CurrentDir = dlg.m_Directory;
187 if (dlg.m_bAutoloadPuttyKeyFile) // do this here, since it might be needed for actions performed in Log
188 StorePuttyKey(dlg.m_Directory, dlg.m_strPuttyKeyFile);
190 postCmdList.push_back(PostCmd(IDI_LOG, IDS_MENULOG, [&]
192 CString cmd = _T("/command:log");
193 cmd += _T(" /path:\"") + dlg.m_Directory + _T("\"");
194 CAppUtils::RunTortoiseGitProc(cmd);
195 }));
197 postCmdList.push_back(PostCmd(IDI_EXPLORER, IDS_STATUSLIST_CONTEXT_EXPLORE, [&]{ CAppUtils::ExploreTo(hWndExplorer, dlg.m_Directory); }));
200 // Handle Git SVN-clone
201 if(dlg.m_bSVN)
203 //g_Git.m_CurrentDir=dlg.m_Directory;
204 cmd.Format(_T("git.exe svn clone \"%s\" \"%s\""),
205 (LPCTSTR)url, (LPCTSTR)dlg.m_Directory);
207 if (dlg.m_bOrigin)
209 CString str;
210 if (dlg.m_strOrigin.IsEmpty())
211 str = _T(" --prefix \"\"");
212 else
213 str.Format(_T(" --prefix \"%s/\""), (LPCTSTR)dlg.m_strOrigin);
214 cmd += str;
217 if(dlg.m_bSVNTrunk)
218 cmd+=_T(" -T ")+dlg.m_strSVNTrunk;
220 if(dlg.m_bSVNBranch)
221 cmd+=_T(" -b ")+dlg.m_strSVNBranchs;
223 if(dlg.m_bSVNTags)
224 cmd+=_T(" -t ")+dlg.m_strSVNTags;
226 if(dlg.m_bSVNFrom)
228 CString str;
229 str.Format(_T("%d:HEAD"),dlg.m_nSVNFrom);
230 cmd+=_T(" -r ")+str;
233 if(dlg.m_bSVNUserName)
235 cmd+= _T(" --username ");
236 cmd+=dlg.m_strUserName;
239 else
241 if (g_Git.UsingLibGit2(CGit::GIT_CMD_CLONE))
243 while (true)
245 retry = false;
246 CGitProgressDlg GitDlg;
247 CTGitPathList list;
248 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
249 list.AddPath(CTGitPath(dir));
250 CloneProgressCommand cloneProgressCommand;
251 GitDlg.SetCommand(&cloneProgressCommand);
252 cloneProgressCommand.m_PostCmdCallback = postCmdCallback;
253 cloneProgressCommand.SetUrl(url);
254 cloneProgressCommand.SetPathList(list);
255 cloneProgressCommand.SetIsBare(dlg.m_bBare == TRUE);
256 if (dlg.m_bBranch)
257 cloneProgressCommand.SetRefSpec(dlg.m_strBranch);
258 if (dlg.m_bOrigin)
259 cloneProgressCommand.SetRemote(dlg.m_strOrigin);
260 cloneProgressCommand.SetNoCheckout(dlg.m_bNoCheckout == TRUE);
261 GitDlg.DoModal();
262 if (!retry)
263 return !GitDlg.DidErrorsOccur();
268 while (true)
270 retry = false;
271 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
272 CProgressDlg progress;
273 progress.m_GitCmd=cmd;
274 progress.m_PostCmdCallback = postCmdCallback;
275 INT_PTR ret = progress.DoModal();
277 if (dlg.m_bSVN)
278 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
280 if (!retry)
281 return ret == IDOK;
284 return FALSE;