Do not use GitAdminDir objects
[TortoiseGit.git] / src / TortoiseProc / PullFetchDlg.cpp
blob450bbc035e6daaa4991dba89907c4facaa0094fe
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // PullFetchDlg.cpp : implementation file
23 #include "stdafx.h"
24 #include "TortoiseProc.h"
25 #include "PullFetchDlg.h"
26 #include "Git.h"
27 #include "AppUtils.h"
28 #include "BrowseRefsDlg.h"
29 // CPullFetchDlg dialog
31 IMPLEMENT_DYNAMIC(CPullFetchDlg, CHorizontalResizableStandAloneDialog)
33 CPullFetchDlg::CPullFetchDlg(CWnd* pParent /*=NULL*/)
34 : CHorizontalResizableStandAloneDialog(CPullFetchDlg::IDD, pParent)
36 m_IsPull=TRUE;
37 m_bRebaseActivatedInConfigForPull = false;
38 m_bAutoLoad = CAppUtils::IsSSHPutty();
39 m_bAutoLoadEnable=CAppUtils::IsSSHPutty();;
40 m_regRebase = false;
41 m_bNoFF = false;
42 m_bRebase = FALSE;
43 m_bSquash = false;
44 m_bNoCommit = false;
45 m_nDepth = 1;
46 m_bDepth = FALSE;
47 m_bFFonly = false;
48 m_bFetchTags = 2;
49 m_bAllRemotes = FALSE;
50 m_bPrune = CAppUtils::GetMsysgitVersion() >= 0x01080500 ? 2 : FALSE;
53 CPullFetchDlg::~CPullFetchDlg()
57 void CPullFetchDlg::DoDataExchange(CDataExchange* pDX)
59 CDialog::DoDataExchange(pDX);
60 DDX_Control(pDX, IDC_REMOTE_COMBO, this->m_Remote);
61 DDX_Control(pDX, IDC_OTHER, this->m_Other);
62 DDX_Control(pDX, IDC_REMOTE_BRANCH, this->m_RemoteBranch);
63 DDX_Control(pDX,IDC_REMOTE_MANAGE, this->m_RemoteManage);
64 DDX_Check(pDX,IDC_CHECK_NOFF, this->m_bNoFF);
65 DDX_Check(pDX,IDC_CHECK_SQUASH, this->m_bSquash);
66 DDX_Check(pDX,IDC_CHECK_NOCOMMIT, this->m_bNoCommit);
67 DDX_Check(pDX,IDC_PUTTYKEY_AUTOLOAD,m_bAutoLoad);
68 DDX_Check(pDX,IDC_CHECK_REBASE,m_bRebase);
69 DDX_Check(pDX,IDC_CHECK_PRUNE,m_bPrune);
70 DDX_Check(pDX, IDC_CHECK_DEPTH, m_bDepth);
71 DDX_Text(pDX, IDC_EDIT_DEPTH, m_nDepth);
72 DDX_Check(pDX, IDC_CHECK_FFONLY, m_bFFonly);
73 DDX_Check(pDX, IDC_CHECK_FETCHTAGS, m_bFetchTags);
77 BEGIN_MESSAGE_MAP(CPullFetchDlg, CHorizontalResizableStandAloneDialog)
78 ON_CBN_SELCHANGE(IDC_REMOTE_COMBO, &CPullFetchDlg::OnCbnSelchangeRemote)
79 ON_BN_CLICKED(IDC_REMOTE_RD, &CPullFetchDlg::OnBnClickedRd)
80 ON_BN_CLICKED(IDC_OTHER_RD, &CPullFetchDlg::OnBnClickedRd)
81 ON_BN_CLICKED(IDOK, &CPullFetchDlg::OnBnClickedOk)
82 ON_STN_CLICKED(IDC_REMOTE_MANAGE, &CPullFetchDlg::OnStnClickedRemoteManage)
83 ON_BN_CLICKED(IDC_BUTTON_BROWSE_REF, &CPullFetchDlg::OnBnClickedButtonBrowseRef)
84 ON_BN_CLICKED(IDC_CHECK_DEPTH, OnBnClickedCheckDepth)
85 ON_BN_CLICKED(IDC_CHECK_FETCHTAGS, OnBnClickedCheckFetchtags)
86 ON_BN_CLICKED(IDC_CHECK_FFONLY, OnBnClickedCheckFfonly)
87 ON_BN_CLICKED(IDC_CHECK_NOFF, OnBnClickedCheckFfonly)
88 END_MESSAGE_MAP()
90 BOOL CPullFetchDlg::OnInitDialog()
92 CHorizontalResizableStandAloneDialog::OnInitDialog();
93 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
95 AddAnchor(IDC_REMOTE_COMBO, TOP_LEFT, TOP_RIGHT);
96 AddAnchor(IDC_OTHER, TOP_LEFT,TOP_RIGHT);
98 AddAnchor(IDC_REMOTE_BRANCH, TOP_LEFT,TOP_RIGHT);
99 AddAnchor(IDC_BUTTON_BROWSE_REF,TOP_RIGHT);
101 AddAnchor(IDOK,BOTTOM_RIGHT);
102 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
103 AddAnchor(IDC_GROUPT_REMOTE,TOP_LEFT,TOP_RIGHT);
104 AddAnchor(IDC_GROUP_OPTION,TOP_LEFT,TOP_RIGHT);
105 AddAnchor(IDC_PUTTYKEY_AUTOLOAD,BOTTOM_LEFT);
106 AddAnchor(IDC_CHECK_PRUNE,BOTTOM_LEFT);
107 AddAnchor(IDC_CHECK_REBASE,BOTTOM_LEFT);
108 AddAnchor(IDC_REMOTE_MANAGE,BOTTOM_LEFT);
109 AddAnchor(IDHELP, BOTTOM_RIGHT);
111 AdjustControlSize(IDC_REMOTE_RD);
112 AdjustControlSize(IDC_OTHER_RD);
113 AdjustControlSize(IDC_CHECK_SQUASH);
114 AdjustControlSize(IDC_CHECK_NOCOMMIT);
115 AdjustControlSize(IDC_CHECK_DEPTH);
116 AdjustControlSize(IDC_CHECK_NOFF);
117 AdjustControlSize(IDC_CHECK_FFONLY);
118 AdjustControlSize(IDC_CHECK_FETCHTAGS);
119 AdjustControlSize(IDC_PUTTYKEY_AUTOLOAD);
120 AdjustControlSize(IDC_CHECK_REBASE);
121 AdjustControlSize(IDC_CHECK_PRUNE);
123 CString WorkingDir=g_Git.m_CurrentDir;
124 WorkingDir.Replace(_T(':'),_T('_'));
126 m_RemoteReg = CRegString(CString(_T("Software\\TortoiseGit\\History\\PullRemote\\")+WorkingDir));
127 CString regkey;
128 regkey.Format(_T("Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\rebase"),WorkingDir,this->m_IsPull);
129 m_regRebase=CRegDWORD(regkey,false);
130 regkey.Format(_T("Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\ffonly"), WorkingDir, m_IsPull);
131 m_regFFonly = CRegDWORD(regkey, false);
132 regkey.Format(_T("Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\autoload"),WorkingDir,this->m_IsPull);
134 m_regAutoLoadPutty = CRegDWORD(regkey,this->m_bAutoLoad);
135 m_bAutoLoad = m_regAutoLoadPutty;
137 if(!CAppUtils::IsSSHPutty())
138 m_bAutoLoad = false;
140 m_bRebase = m_regRebase;
142 CAutoRepository repo(g_Git.GetGitRepository());
143 if (!repo)
144 MessageBox(CGit::GetLibGit2LastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
146 // Check config branch.<name>.rebase and pull.reabse
149 if (!m_IsPull)
150 break;
151 if (!repo)
152 break;
154 if (git_repository_head_detached(repo) == 1)
155 break;
157 CAutoConfig config(true);
158 if (git_repository_config(config.GetPointer(), repo))
159 break;
161 BOOL rebase = FALSE;
162 // branch.<name>.rebase overrides pull.rebase
163 if (config.GetBOOL(_T("branch.") + g_Git.GetCurrentBranch() + _T(".rebase"), rebase) == GIT_ENOTFOUND)
165 if (config.GetBOOL(_T("pull.rebase"), rebase) == GIT_ENOTFOUND)
166 break;
168 if (!rebase)
169 break;
171 // Since rebase = true in config, means that "git.exe pull" will ALWAYS rebase without "--rebase".
172 // So, lock it, then let Fetch Rebase do the rest things.
173 m_bRebase = TRUE;
174 m_bRebaseActivatedInConfigForPull = true;
175 } while (0);
177 this->UpdateData(FALSE);
179 this->AddOthersToAnchor();
181 this->GetDlgItem(IDC_PUTTYKEY_AUTOLOAD)->EnableWindow(m_bAutoLoadEnable);
183 CheckRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD,IDC_REMOTE_RD);
184 m_Remote.EnableWindow(TRUE);
185 m_Other.EnableWindow(FALSE);
186 if(!m_IsPull)
188 m_RemoteBranch.EnableWindow(FALSE);
189 GetDlgItem(IDC_BUTTON_BROWSE_REF)->EnableWindow(FALSE);
192 if(m_IsPull)
194 m_bFFonly = m_regFFonly;
195 UpdateData(FALSE);
196 OnBnClickedCheckFfonly();
198 else
200 this->GetDlgItem(IDC_GROUP_OPTION)->EnableWindow(FALSE);
201 this->GetDlgItem(IDC_CHECK_SQUASH)->EnableWindow(FALSE);
202 this->GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(FALSE);
203 this->GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(FALSE);
204 this->GetDlgItem(IDC_CHECK_NOCOMMIT)->EnableWindow(FALSE);
207 if (GitAdminDir::IsBareRepo(g_Git.m_CurrentDir))
208 this->GetDlgItem(IDC_CHECK_REBASE)->EnableWindow(FALSE);
210 if (repo && git_repository_is_shallow(repo))
212 m_bDepth = TRUE;
213 UpdateData(FALSE);
215 else
217 GetDlgItem(IDC_CHECK_DEPTH)->ShowWindow(SW_HIDE);
218 GetDlgItem(IDC_EDIT_DEPTH)->ShowWindow(SW_HIDE);
220 OnBnClickedCheckDepth();
222 m_Other.SetCaseSensitive(TRUE);
223 m_Other.SetURLHistory(TRUE);
224 m_Other.LoadHistory(_T("Software\\TortoiseGit\\History\\PullURLS"), _T("url"));
226 m_RemoteBranch.LoadHistory(_T("Software\\TortoiseGit\\History\\PullRemoteBranch"), _T("br"));
227 m_RemoteBranch.SetCurSel(0);
229 CString sWindowTitle;
230 if(m_IsPull)
231 sWindowTitle = CString(MAKEINTRESOURCE(IDS_PROGRS_TITLE_PULL));
232 else
233 sWindowTitle = CString(MAKEINTRESOURCE(IDS_PROGRS_TITLE_FETCH));
235 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
237 Refresh();
239 EnableSaveRestore(_T("PullFetchDlg"));
240 this->m_RemoteManage.SetURL(CString());
241 return TRUE;
244 void CPullFetchDlg::Refresh()
246 //Select pull-remote from current branch
247 CString currentBranch;
248 if (g_Git.GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch))
249 currentBranch.Empty();
251 CString pullRemote, pullBranch;
252 g_Git.GetRemoteTrackedBranch(currentBranch, pullRemote, pullBranch);
253 m_configPullRemote = pullRemote;
254 m_configPullBranch = pullBranch;
256 if (pullBranch.IsEmpty())
257 m_RemoteBranch.AddString(currentBranch);
258 else
259 m_RemoteBranch.AddString(pullBranch);
261 if(pullRemote.IsEmpty())
262 pullRemote = m_RemoteReg;
264 if (!m_PreSelectRemote.IsEmpty())
265 pullRemote = m_PreSelectRemote;
267 STRING_VECTOR list;
268 m_Remote.Reset();
269 int sel=0;
270 if(!g_Git.GetRemoteList(list))
272 if (!m_IsPull && list.size() > 1)
273 m_Remote.AddString(CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)));
275 for (unsigned int i = 0; i < list.size(); ++i)
277 m_Remote.AddString(list[i]);
278 if (!m_bAllRemotes && list[i] == pullRemote)
279 sel = i + (!m_IsPull && list.size() > 1 ? 1 : 0);
282 m_Remote.SetCurSel(sel);
283 OnCbnSelchangeRemote();
285 // CPullFetchDlg message handlers
287 void CPullFetchDlg::OnCbnSelchangeRemote()
289 CString remote = m_Remote.GetString();
290 if (remote.IsEmpty() || remote == CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)))
292 GetDlgItem(IDC_STATIC_TAGOPT)->SetWindowText(_T(""));
293 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(_T(""));
294 return;
297 CString key;
298 key.Format(_T("remote.%s.tagopt"), remote);
299 CString tagopt = g_Git.GetConfigValue(key);
300 if (tagopt == "--no-tags")
301 tagopt = CString(MAKEINTRESOURCE(IDS_NONE));
302 else if (tagopt == "--tags")
303 tagopt = CString(MAKEINTRESOURCE(CAppUtils::GetMsysgitVersion() < 0x01090000 ? IDS_FETCH_TAGS_ONLY : IDS_ALL));
304 else
305 tagopt = CString(MAKEINTRESOURCE(IDS_FETCH_REACHABLE));
306 CString value;
307 value.Format(_T("%s: %s"), CString(MAKEINTRESOURCE(IDS_DEFAULT)), tagopt);
308 GetDlgItem(IDC_STATIC_TAGOPT)->SetWindowText(value);
310 CString prune;
311 if (CAppUtils::GetMsysgitVersion() >= 0x01080500)
313 key.Format(_T("remote.%s.prune"), remote);
314 prune = g_Git.GetConfigValue(key);
315 if (prune.IsEmpty())
316 prune = g_Git.GetConfigValue(_T("fetch.prune"));
318 if (!prune.IsEmpty())
320 CString value;
321 value.Format(_T("%s: %s"), CString(MAKEINTRESOURCE(IDS_DEFAULT)), prune);
322 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(value);
324 else
325 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(_T(""));
328 void CPullFetchDlg::OnBnClickedRd()
330 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_REMOTE_RD)
332 m_Remote.EnableWindow(TRUE);
333 m_Other.EnableWindow(FALSE);
334 DialogEnableWindow(IDC_CHECK_REBASE, TRUE);
335 m_bRebase = m_bRebaseActivatedInConfigForPull;
336 UpdateData(FALSE);
337 if(!m_IsPull)
338 m_RemoteBranch.EnableWindow(FALSE);
340 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_OTHER_RD)
342 CString clippath = CAppUtils::GetClipboardLink(m_IsPull ? _T("git pull") : _T("git fetch"), 1);
343 if (clippath.IsEmpty())
344 m_Other.SetCurSel(0);
345 else
347 int argSeparator = clippath.Find(' ');
348 if (argSeparator > 1 && clippath.GetLength() > argSeparator + 2)
350 m_Other.SetWindowText(clippath.Left(argSeparator));
351 m_RemoteBranch.SetWindowText(clippath.Mid(argSeparator + 1));
353 else
354 m_Other.SetWindowText(clippath);
356 m_Remote.EnableWindow(FALSE);
357 m_Other.EnableWindow(TRUE);;
358 DialogEnableWindow(IDC_CHECK_REBASE, FALSE);
359 m_bRebase = FALSE;
360 UpdateData(FALSE);
361 if(!m_IsPull)
362 m_RemoteBranch.EnableWindow(TRUE);
366 void CPullFetchDlg::OnBnClickedOk()
368 this->UpdateData();
369 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_REMOTE_RD)
371 m_RemoteURL=m_Remote.GetString();
372 m_bAllRemotes = (m_Remote.GetCurSel() == 0 && m_Remote.GetCount() > 1 && !m_IsPull);
373 if( !m_IsPull ||
374 (m_configPullRemote == m_RemoteURL && m_configPullBranch == m_RemoteBranch.GetString() ))
375 //When fetching or when pulling from the configured tracking branch, dont explicitly set the remote branch name,
376 //because otherwise git will not update the remote tracking branches.
377 m_RemoteBranchName.Empty();
378 else
379 m_RemoteBranchName=m_RemoteBranch.GetString();
381 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_OTHER_RD)
383 m_Other.GetWindowTextW(m_RemoteURL);
384 m_RemoteBranchName=m_RemoteBranch.GetString();
386 // only store URL in history if it's value was used
387 m_Other.SaveHistory();
390 m_RemoteReg = m_Remote.GetString();
392 m_RemoteBranch.SaveHistory();
393 this->m_regRebase=this->m_bRebase;
394 m_regFFonly = m_bFFonly;
396 m_regAutoLoadPutty = m_bAutoLoad;
398 this->OnOK();
401 void CPullFetchDlg::OnStnClickedRemoteManage()
403 CAppUtils::LaunchRemoteSetting();
404 Refresh();
407 void CPullFetchDlg::OnBnClickedButtonBrowseRef()
409 CString initialRef;
410 initialRef.Format(L"refs/remotes/%s/%s", m_Remote.GetString(), m_RemoteBranch.GetString());
411 CString selectedRef = CBrowseRefsDlg::PickRef(false, initialRef, gPickRef_Remote);
412 if(selectedRef.Left(13) != "refs/remotes/")
413 return;
415 selectedRef = selectedRef.Mid(13);
416 int ixSlash = selectedRef.Find('/');
418 CString remoteName = selectedRef.Left(ixSlash);
419 CString remoteBranch = selectedRef.Mid(ixSlash + 1);
421 int ixFound = m_Remote.FindStringExact(0, remoteName);
422 if(ixFound >= 0)
423 m_Remote.SetCurSel(ixFound);
424 m_RemoteBranch.AddString(remoteBranch, 0);
426 CheckRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD,IDC_REMOTE_RD);
429 void CPullFetchDlg::OnBnClickedCheckDepth()
431 UpdateData(TRUE);
432 GetDlgItem(IDC_EDIT_DEPTH)->EnableWindow(m_bDepth);
435 void CPullFetchDlg::OnBnClickedCheckFetchtags()
437 if (CAppUtils::GetMsysgitVersion() < 0x01090000)
439 UpdateData();
440 if (m_bFetchTags == TRUE)
441 m_bFetchTags = 2;
442 UpdateData(FALSE);
446 void CPullFetchDlg::OnBnClickedCheckFfonly()
448 UpdateData();
449 if (m_bNoFF)
451 m_bFFonly = FALSE;
452 GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(FALSE);
454 else
456 GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(TRUE);
458 if (m_bFFonly)
460 m_bNoFF = FALSE;
461 GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(FALSE);
463 else
465 GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(TRUE);
467 UpdateData(FALSE);