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.
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
& keyFile
)
54 CAutoRepository
repo(repoRoot
);
59 if (git_repository_config(config
.GetPointer(), repo
))
62 if (git_config_set_string(config
, "remote.origin.puttykeyfile", CUnicodeUtils::GetUTF8(keyFile
)))
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();
90 cloneDirectory
= orgCmdLinePath
;
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
;
105 recursiveStr
= _T(" --recursive");
109 bareStr
= _T(" --bare");
111 CString nocheckoutStr
;
112 if (dlg
.m_bNoCheckout
)
113 nocheckoutStr
= _T(" --no-checkout");
117 branchStr
= _T(" --branch ") + dlg
.m_strBranch
;
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
);
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('@'));
143 CString path
= url
.Mid(atSign
);
144 path
.Replace(_T('\\'), _T('/'));
145 url
= url
.Mid(0, atSign
) + path
;
148 url
.Replace( _T('\\'), _T('/'));
154 depth
.Format(_T(" --depth %d"),dlg
.m_nDepth
);
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
,
171 (LPCTSTR
)progressarg
,
177 auto postCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
181 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&] { retry
= true; }));
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
);
197 postCmdList
.push_back(PostCmd(IDI_EXPLORER
, IDS_STATUSLIST_CONTEXT_EXPLORE
, [&]{ CAppUtils::ExploreTo(hWndExplorer
, dlg
.m_Directory
); }));
200 // Handle Git SVN-clone
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
);
210 if (dlg
.m_strOrigin
.IsEmpty())
211 str
= _T(" --prefix \"\"");
213 str
.Format(_T(" --prefix \"%s/\""), (LPCTSTR
)dlg
.m_strOrigin
);
218 cmd
+=_T(" -T ")+dlg
.m_strSVNTrunk
;
221 cmd
+=_T(" -b ")+dlg
.m_strSVNBranchs
;
224 cmd
+=_T(" -t ")+dlg
.m_strSVNTags
;
229 str
.Format(_T("%d:HEAD"),dlg
.m_nSVNFrom
);
233 if(dlg
.m_bSVNUserName
)
235 cmd
+= _T(" --username ");
236 cmd
+=dlg
.m_strUserName
;
241 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_CLONE
))
246 CGitProgressDlg GitDlg
;
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
);
257 cloneProgressCommand
.SetRefSpec(dlg
.m_strBranch
);
259 cloneProgressCommand
.SetRemote(dlg
.m_strOrigin
);
260 cloneProgressCommand
.SetNoCheckout(dlg
.m_bNoCheckout
== TRUE
);
263 return !GitDlg
.DidErrorsOccur();
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();
278 ::DeleteFile(g_Git
.m_CurrentDir
+ _T("\\sys$command"));