Fix typos
[TortoiseGit.git] / src / TortoiseProc / Commands / CloneCommand.cpp
blob99b4e2d52433f83df7a7c7d56a7e4299f80e38bd
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2019, 2021-2023 - 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"
24 #include "StringUtils.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] == L':')
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(L"remote.%s.puttykeyfile", static_cast<LPCWSTR>(remote));
65 if (git_config_set_string(config, CUnicodeUtils::GetUTF8(configName), CUnicodeUtils::GetUTF8(keyFile)))
66 goto error;
68 return;
70 error:
71 MessageBox(GetExplorerHWND(), CGit::GetLibGit2LastErr(L"Could not open repository"), L"TortoiseGit", MB_ICONERROR);
74 bool CloneCommand::Execute()
76 CTGitPath cloneDirectory;
77 if (!parser.HasKey(L"hasurlhandler"))
79 if (orgCmdLinePath.IsEmpty())
81 cloneDirectory.SetFromWin(sOrigCWD, true);
82 DWORD len = ::GetTempPath(0, nullptr);
83 auto tszPath = std::make_unique<wchar_t[]>(len);
84 ::GetTempPath(len, tszPath.get());
85 if (_wcsnicmp(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(L"url"))
100 dlg.m_URL = parser.GetVal(L"url");
101 if (parser.HasKey(L"exactpath"))
102 dlg.m_bExactPath = TRUE;
104 if(dlg.DoModal()==IDOK)
106 CString recursiveStr;
107 if(dlg.m_bRecursive)
108 recursiveStr = L" --recursive";
110 CString bareStr;
111 if(dlg.m_bBare)
112 bareStr = L" --bare";
114 CString nocheckoutStr;
115 if (dlg.m_bNoCheckout)
116 nocheckoutStr = L" --no-checkout";
118 CString branchStr;
119 if (dlg.m_bBranch)
120 branchStr = L" --branch " + dlg.m_strBranch;
122 CString originStr;
123 if (dlg.m_bOrigin && !dlg.m_bSVN)
124 originStr = L" --origin " + dlg.m_strOrigin;
126 if(dlg.m_bAutoloadPuttyKeyFile)
127 CAppUtils::LaunchPAgent(GetExplorerHWND(), &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 (CStringUtils::StartsWith(url, L"\\\\"))
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(L'@');
142 if (atSign > 0)
144 CString path = url.Mid(atSign);
145 path.Replace(L'\\', L'/');
146 url = url.Left(atSign) + path;
148 else
149 url.Replace( L'\\', L'/');
152 CString depth;
153 if (dlg.m_bDepth)
154 depth.Format(L" --depth %d", dlg.m_nDepth);
156 CString cmd;
157 cmd.Format(L"git.exe clone --progress%s%s%s%s%s -v%s -- \"%s\" \"%s\"",
158 static_cast<LPCWSTR>(nocheckoutStr),
159 static_cast<LPCWSTR>(recursiveStr),
160 static_cast<LPCWSTR>(bareStr),
161 static_cast<LPCWSTR>(branchStr),
162 static_cast<LPCWSTR>(originStr),
163 static_cast<LPCWSTR>(depth),
164 static_cast<LPCWSTR>(url),
165 static_cast<LPCWSTR>(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 if (dlg.m_bBare)
177 CAppUtils::SetupBareRepoIcon(dir);
179 // After cloning, change current directory to the cloned directory
180 g_Git.m_CurrentDir = dlg.m_Directory;
181 if (dlg.m_bAutoloadPuttyKeyFile) // do this here, since it might be needed for actions performed in Log
182 StorePuttyKey(dlg.m_Directory, dlg.m_bOrigin && !dlg.m_strOrigin.IsEmpty() ? dlg.m_strOrigin : CString("origin"), dlg.m_strPuttyKeyFile);
184 postCmdList.emplace_back(IDI_LOG, IDS_MENULOG, [&]
186 CString cmd = L"/command:log";
187 cmd += L" /path:\"" + dlg.m_Directory + L'"';
188 CAppUtils::RunTortoiseGitProc(cmd);
191 postCmdList.emplace_back(IDI_EXPLORER, IDS_STATUSLIST_CONTEXT_EXPLORE, [&]{ CAppUtils::ExploreTo(GetExplorerHWND(), dlg.m_Directory); });
194 // Handle Git SVN-clone
195 if(dlg.m_bSVN)
197 // git-svn requires some mangling: \ -> /
198 if (!PathIsURL(url))
200 url.Replace(L'\\', L'/');
201 if (PathIsUNC(url))
202 url = L"file:" + url;
203 else if (PathIsDirectory(url))
205 // prefix: file:///, and no colon after drive letter for normal paths
206 if (url.GetLength() > 2 && url.GetAt(1) == L':')
207 url.Delete(1, 1);
208 url = L"file:///" + url;
212 //g_Git.m_CurrentDir=dlg.m_Directory;
213 cmd.Format(L"git.exe svn clone -- \"%s\" \"%s\"",
214 static_cast<LPCWSTR>(url), static_cast<LPCWSTR>(dlg.m_Directory));
216 if (dlg.m_bOrigin)
218 CString str;
219 if (dlg.m_strOrigin.IsEmpty())
220 str = L" --prefix \"\"";
221 else
222 str.Format(L" --prefix \"%s/\"", static_cast<LPCWSTR>(dlg.m_strOrigin));
223 cmd += str;
226 if(dlg.m_bSVNTrunk)
227 cmd += L" -T " + dlg.m_strSVNTrunk;
229 if(dlg.m_bSVNBranch)
230 cmd += L" -b " + dlg.m_strSVNBranchs;
232 if(dlg.m_bSVNTags)
233 cmd += L" -t " + dlg.m_strSVNTags;
235 if(dlg.m_bSVNFrom)
236 cmd.AppendFormat(L" -r %d:HEAD", dlg.m_nSVNFrom);
238 if(dlg.m_bSVNUserName)
240 cmd += L" --username ";
241 cmd+=dlg.m_strUserName;
244 else
246 if (g_Git.UsingLibGit2(CGit::GIT_CMD_CLONE))
248 while (true)
250 retry = false;
251 CGitProgressDlg GitDlg;
252 CTGitPathList list;
253 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
254 list.AddPath(CTGitPath(dir));
255 CloneProgressCommand cloneProgressCommand;
256 GitDlg.SetCommand(&cloneProgressCommand);
257 cloneProgressCommand.m_PostCmdCallback = postCmdCallback;
258 cloneProgressCommand.SetUrl(url);
259 cloneProgressCommand.SetPathList(list);
260 cloneProgressCommand.SetIsBare(dlg.m_bBare == TRUE);
261 if (dlg.m_bBranch)
262 cloneProgressCommand.SetRefSpec(dlg.m_strBranch);
263 if (dlg.m_bOrigin)
264 cloneProgressCommand.SetRemote(dlg.m_strOrigin);
265 cloneProgressCommand.SetNoCheckout(dlg.m_bNoCheckout == TRUE);
266 GitDlg.DoModal();
267 if (!retry)
268 return !GitDlg.DidErrorsOccur();
273 while (true)
275 retry = false;
276 g_Git.m_CurrentDir = GetExistingDirectoryForClone(dlg.m_Directory);
277 CProgressDlg progress;
278 progress.m_GitCmd=cmd;
279 progress.m_PostCmdCallback = postCmdCallback;
280 INT_PTR ret = progress.DoModal();
282 if (!retry)
283 return ret == IDOK;
286 return FALSE;