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.
21 #include "CloneCommand.h"
23 #include "GitProgressDlg.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] == _T(':'))
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(_T("remote.%s.puttykeyfile"), (LPCTSTR
)remote
);
65 if (git_config_set_string(config
, CUnicodeUtils::GetUTF8(configName
), CUnicodeUtils::GetUTF8(keyFile
)))
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();
93 cloneDirectory
= orgCmdLinePath
;
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
;
108 recursiveStr
= _T(" --recursive");
112 bareStr
= _T(" --bare");
114 CString nocheckoutStr
;
115 if (dlg
.m_bNoCheckout
)
116 nocheckoutStr
= _T(" --no-checkout");
120 branchStr
= _T(" --branch ") + dlg
.m_strBranch
;
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
);
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('@'));
144 CString path
= url
.Mid(atSign
);
145 path
.Replace(_T('\\'), _T('/'));
146 url
= url
.Mid(0, atSign
) + path
;
149 url
.Replace( _T('\\'), _T('/'));
154 depth
.Format(_T(" --depth %d"),dlg
.m_nDepth
);
157 cmd
.Format(_T("git.exe clone --progress%s%s%s%s%s -v%s \"%s\" \"%s\""),
158 (LPCTSTR
)nocheckoutStr
,
159 (LPCTSTR
)recursiveStr
,
168 auto postCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
172 postCmdList
.emplace_back(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ retry
= true; });
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
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
);
201 if (dlg
.m_strOrigin
.IsEmpty())
202 str
= _T(" --prefix \"\"");
204 str
.Format(_T(" --prefix \"%s/\""), (LPCTSTR
)dlg
.m_strOrigin
);
209 cmd
+=_T(" -T ")+dlg
.m_strSVNTrunk
;
212 cmd
+=_T(" -b ")+dlg
.m_strSVNBranchs
;
215 cmd
+=_T(" -t ")+dlg
.m_strSVNTags
;
220 str
.Format(_T("%d:HEAD"),dlg
.m_nSVNFrom
);
224 if(dlg
.m_bSVNUserName
)
226 cmd
+= _T(" --username ");
227 cmd
+=dlg
.m_strUserName
;
232 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_CLONE
))
237 CGitProgressDlg GitDlg
;
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
);
248 cloneProgressCommand
.SetRefSpec(dlg
.m_strBranch
);
250 cloneProgressCommand
.SetRemote(dlg
.m_strOrigin
);
251 cloneProgressCommand
.SetNoCheckout(dlg
.m_bNoCheckout
== TRUE
);
254 return !GitDlg
.DidErrorsOccur();
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();