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.
21 #include "CloneCommand.h"
23 #include "GitProgressDlg.h"
24 #include "StringUtils.h"
26 #include "ProgressDlg.h"
28 #include "UnicodeUtils.h"
29 #include "SysProgressDlg.h"
30 #include "ProgressCommands/CloneProgressCommand.h"
32 static CString
GetExistingDirectoryForClone(CString path
)
34 if (PathFileExists(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('\\');
52 static void StorePuttyKey(const CString
& repoRoot
, const CString
& remote
, const CString
& keyFile
)
54 CAutoRepository
repo(repoRoot
);
60 if (git_repository_config(config
.GetPointer(), repo
))
63 configName
.Format(L
"remote.%s.puttykeyfile", static_cast<LPCWSTR
>(remote
));
65 if (git_config_set_string(config
, CUnicodeUtils::GetUTF8(configName
), CUnicodeUtils::GetUTF8(keyFile
)))
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();
93 cloneDirectory
= orgCmdLinePath
;
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
;
108 recursiveStr
= L
" --recursive";
112 bareStr
= L
" --bare";
114 CString nocheckoutStr
;
115 if (dlg
.m_bNoCheckout
)
116 nocheckoutStr
= L
" --no-checkout";
120 branchStr
= L
" --branch " + dlg
.m_strBranch
;
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
);
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
'@');
144 CString path
= url
.Mid(atSign
);
145 path
.Replace(L
'\\', L
'/');
146 url
= url
.Left(atSign
) + path
;
149 url
.Replace( L
'\\', L
'/');
154 depth
.Format(L
" --depth %d", dlg
.m_nDepth
);
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
));
168 auto postCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
172 postCmdList
.emplace_back(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ retry
= true; });
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
197 // git-svn requires some mangling: \ -> /
200 url
.Replace(L
'\\', L
'/');
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
':')
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
));
219 if (dlg
.m_strOrigin
.IsEmpty())
220 str
= L
" --prefix \"\"";
222 str
.Format(L
" --prefix \"%s/\"", static_cast<LPCWSTR
>(dlg
.m_strOrigin
));
227 cmd
+= L
" -T " + dlg
.m_strSVNTrunk
;
230 cmd
+= L
" -b " + dlg
.m_strSVNBranchs
;
233 cmd
+= L
" -t " + dlg
.m_strSVNTags
;
236 cmd
.AppendFormat(L
" -r %d:HEAD", dlg
.m_nSVNFrom
);
238 if(dlg
.m_bSVNUserName
)
240 cmd
+= L
" --username ";
241 cmd
+=dlg
.m_strUserName
;
246 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_CLONE
))
251 CGitProgressDlg GitDlg
;
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
);
262 cloneProgressCommand
.SetRefSpec(dlg
.m_strBranch
);
264 cloneProgressCommand
.SetRemote(dlg
.m_strOrigin
);
265 cloneProgressCommand
.SetNoCheckout(dlg
.m_bNoCheckout
== TRUE
);
268 return !GitDlg
.DidErrorsOccur();
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();