Refactor rebase commit list: Don't fill list again and again when reordering commits
[TortoiseGit.git] / src / TortoiseProc / PullFetchDlg.cpp
blobbda7e3d1e6012b73f5c833f91541ddd7741330a0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2018 - 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 #include "MessageBox.h"
30 // CPullFetchDlg dialog
32 IMPLEMENT_DYNAMIC(CPullFetchDlg, CHorizontalResizableStandAloneDialog)
34 CPullFetchDlg::CPullFetchDlg(CWnd* pParent /*=nullptr*/)
35 : CHorizontalResizableStandAloneDialog(CPullFetchDlg::IDD, pParent)
36 , m_IsPull(TRUE)
37 , m_bRebaseActivatedInConfigForPull(false)
38 , m_bNoFF(BST_UNCHECKED)
39 , m_bRebase(BST_UNCHECKED)
40 , m_bRebasePreserveMerges(false)
41 , m_bSquash(BST_UNCHECKED)
42 , m_bNoCommit(BST_UNCHECKED)
43 , m_nDepth(1)
44 , m_bDepth(BST_UNCHECKED)
45 , m_bFFonly(BST_UNCHECKED)
46 , m_bFetchTags(BST_INDETERMINATE)
47 , m_bAllRemotes(BST_UNCHECKED)
48 , m_bPrune(BST_INDETERMINATE)
49 , m_bAutoLoad(CAppUtils::IsSSHPutty())
50 , m_bAutoLoadEnable(CAppUtils::IsSSHPutty())
51 , m_bNamedRemoteFetchAll(!!CRegDWORD(L"Software\\TortoiseGit\\NamedRemoteFetchAll", TRUE))
55 CPullFetchDlg::~CPullFetchDlg()
59 void CPullFetchDlg::DoDataExchange(CDataExchange* pDX)
61 CDialog::DoDataExchange(pDX);
62 DDX_Control(pDX, IDC_REMOTE_COMBO, this->m_Remote);
63 DDX_Control(pDX, IDC_OTHER, this->m_Other);
64 DDX_Control(pDX, IDC_REMOTE_BRANCH, this->m_RemoteBranch);
65 DDX_Control(pDX,IDC_REMOTE_MANAGE, this->m_RemoteManage);
66 DDX_Check(pDX,IDC_CHECK_NOFF, this->m_bNoFF);
67 DDX_Check(pDX,IDC_CHECK_SQUASH, this->m_bSquash);
68 DDX_Check(pDX,IDC_CHECK_NOCOMMIT, this->m_bNoCommit);
69 DDX_Check(pDX,IDC_PUTTYKEY_AUTOLOAD,m_bAutoLoad);
70 DDX_Check(pDX,IDC_CHECK_REBASE,m_bRebase);
71 DDX_Check(pDX,IDC_CHECK_PRUNE,m_bPrune);
72 DDX_Check(pDX, IDC_CHECK_DEPTH, m_bDepth);
73 DDX_Text(pDX, IDC_EDIT_DEPTH, m_nDepth);
74 DDX_Check(pDX, IDC_CHECK_FFONLY, m_bFFonly);
75 DDX_Check(pDX, IDC_CHECK_FETCHTAGS, m_bFetchTags);
79 BEGIN_MESSAGE_MAP(CPullFetchDlg, CHorizontalResizableStandAloneDialog)
80 ON_CBN_SELCHANGE(IDC_REMOTE_COMBO, &CPullFetchDlg::OnCbnSelchangeRemote)
81 ON_BN_CLICKED(IDC_REMOTE_RD, &CPullFetchDlg::OnBnClickedRd)
82 ON_BN_CLICKED(IDC_OTHER_RD, &CPullFetchDlg::OnBnClickedRd)
83 ON_BN_CLICKED(IDOK, &CPullFetchDlg::OnBnClickedOk)
84 ON_STN_CLICKED(IDC_REMOTE_MANAGE, &CPullFetchDlg::OnStnClickedRemoteManage)
85 ON_BN_CLICKED(IDC_BUTTON_BROWSE_REF, &CPullFetchDlg::OnBnClickedButtonBrowseRef)
86 ON_BN_CLICKED(IDC_CHECK_DEPTH, OnBnClickedCheckDepth)
87 ON_BN_CLICKED(IDC_CHECK_FFONLY, OnBnClickedCheckFfonly)
88 ON_BN_CLICKED(IDC_CHECK_NOFF, OnBnClickedCheckFfonly)
89 END_MESSAGE_MAP()
91 BOOL CPullFetchDlg::OnInitDialog()
93 CHorizontalResizableStandAloneDialog::OnInitDialog();
94 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
96 AdjustControlSize(IDC_REMOTE_RD);
97 AdjustControlSize(IDC_OTHER_RD);
98 AdjustControlSize(IDC_CHECK_SQUASH);
99 AdjustControlSize(IDC_CHECK_NOCOMMIT);
100 AdjustControlSize(IDC_CHECK_DEPTH);
101 AdjustControlSize(IDC_CHECK_NOFF);
102 AdjustControlSize(IDC_CHECK_FFONLY);
103 AdjustControlSize(IDC_CHECK_FETCHTAGS);
104 AdjustControlSize(IDC_PUTTYKEY_AUTOLOAD);
105 AdjustControlSize(IDC_CHECK_REBASE);
106 AdjustControlSize(IDC_CHECK_PRUNE);
108 AddAnchor(IDC_REMOTE_COMBO, TOP_LEFT, TOP_RIGHT);
109 AddAnchor(IDC_OTHER, TOP_LEFT,TOP_RIGHT);
111 AddAnchor(IDC_REMOTE_BRANCH, TOP_LEFT,TOP_RIGHT);
112 AddAnchor(IDC_BUTTON_BROWSE_REF,TOP_RIGHT);
114 AddAnchor(IDOK,BOTTOM_RIGHT);
115 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
116 AddAnchor(IDC_GROUPT_REMOTE,TOP_LEFT,TOP_RIGHT);
117 AddAnchor(IDC_GROUP_OPTION,TOP_LEFT,TOP_RIGHT);
118 AddAnchor(IDC_PUTTYKEY_AUTOLOAD,BOTTOM_LEFT);
119 AddAnchor(IDC_CHECK_PRUNE,BOTTOM_LEFT);
120 AddAnchor(IDC_CHECK_REBASE,BOTTOM_LEFT);
121 AddAnchor(IDC_REMOTE_MANAGE,BOTTOM_LEFT);
122 AddAnchor(IDHELP, BOTTOM_RIGHT);
124 CString WorkingDir=g_Git.m_CurrentDir;
125 WorkingDir.Replace(L':', L'_');
127 m_RemoteReg = CRegString(L"Software\\TortoiseGit\\History\\PullRemote\\" + WorkingDir);
128 CString regkey;
129 regkey.Format(L"Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\rebase", (LPCTSTR)WorkingDir, m_IsPull);
130 m_regRebase=CRegDWORD(regkey,false);
131 regkey.Format(L"Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\ffonly", (LPCTSTR)WorkingDir, m_IsPull);
132 m_regFFonly = CRegDWORD(regkey, false);
133 regkey.Format(L"Software\\TortoiseGit\\TortoiseProc\\PullFetch\\%s_%d\\autoload", (LPCTSTR)WorkingDir, m_IsPull);
135 m_regAutoLoadPutty = CRegDWORD(regkey,this->m_bAutoLoad);
136 m_bAutoLoad = m_regAutoLoadPutty;
138 if(!CAppUtils::IsSSHPutty())
139 m_bAutoLoad = false;
141 m_bRebase = m_regRebase;
143 CAutoRepository repo(g_Git.GetGitRepository());
144 if (!repo)
145 MessageBox(CGit::GetLibGit2LastErr(L"Could not open repository."), L"TortoiseGit", MB_OK | MB_ICONERROR);
147 // Check config branch.<name>.rebase and pull.reabse
150 if (!m_IsPull)
151 break;
152 if (!repo)
153 break;
155 if (git_repository_head_detached(repo) == 1)
156 break;
158 CAutoConfig config(true);
159 if (git_repository_config(config.GetPointer(), repo))
160 break;
162 BOOL rebase = FALSE;
163 // branch.<name>.rebase overrides pull.rebase
164 if (config.GetBOOL(L"branch." + g_Git.GetCurrentBranch() + L".rebase", rebase) == GIT_ENOTFOUND)
166 if (config.GetBOOL(L"pull.rebase", rebase) == GIT_ENOTFOUND)
167 break;
168 else
170 CString value;
171 config.GetString(L"pull.rebase", value);
172 if (value == L"preserve")
174 rebase = TRUE;
175 m_bRebasePreserveMerges = true;
179 else
181 CString value;
182 config.GetString(L"branch." + g_Git.GetCurrentBranch() + L".rebase", value);
183 if (value == L"preserve")
185 rebase = TRUE;
186 m_bRebasePreserveMerges = true;
189 if (!rebase)
190 break;
192 // Since rebase = true in config, means that "git.exe pull" will ALWAYS rebase without "--rebase".
193 // So, lock it, then let Fetch Rebase do the rest things.
194 m_bRebase = TRUE;
195 m_bRebaseActivatedInConfigForPull = true;
196 } while (0);
198 this->UpdateData(FALSE);
200 this->AddOthersToAnchor();
202 this->GetDlgItem(IDC_PUTTYKEY_AUTOLOAD)->EnableWindow(m_bAutoLoadEnable);
204 CheckRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD,IDC_REMOTE_RD);
205 m_Remote.EnableWindow(TRUE);
206 m_Remote.SetMaxHistoryItems(0x7FFFFFFF);
207 m_Other.EnableWindow(FALSE);
208 m_RemoteBranch.SetCaseSensitive(TRUE);
209 if (!m_IsPull && m_bNamedRemoteFetchAll)
211 m_RemoteBranch.EnableWindow(FALSE);
212 GetDlgItem(IDC_BUTTON_BROWSE_REF)->EnableWindow(FALSE);
215 if(m_IsPull)
217 m_bFFonly = m_regFFonly;
218 UpdateData(FALSE);
219 OnBnClickedCheckFfonly();
221 else
223 this->GetDlgItem(IDC_GROUP_OPTION)->EnableWindow(FALSE);
224 this->GetDlgItem(IDC_CHECK_SQUASH)->EnableWindow(FALSE);
225 this->GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(FALSE);
226 this->GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(FALSE);
227 this->GetDlgItem(IDC_CHECK_NOCOMMIT)->EnableWindow(FALSE);
230 if (GitAdminDir::IsBareRepo(g_Git.m_CurrentDir))
231 this->GetDlgItem(IDC_CHECK_REBASE)->EnableWindow(FALSE);
233 if (repo && git_repository_is_shallow(repo))
235 m_bDepth = TRUE;
236 UpdateData(FALSE);
238 else
240 GetDlgItem(IDC_CHECK_DEPTH)->ShowWindow(SW_HIDE);
241 GetDlgItem(IDC_EDIT_DEPTH)->ShowWindow(SW_HIDE);
243 OnBnClickedCheckDepth();
245 m_Other.SetCaseSensitive(TRUE);
246 m_Other.SetURLHistory(TRUE);
247 m_Other.LoadHistory(L"Software\\TortoiseGit\\History\\PullURLS", L"url");
249 m_RemoteBranch.LoadHistory(L"Software\\TortoiseGit\\History\\PullRemoteBranch", L"br");
250 m_RemoteBranch.SetCurSel(0);
252 CString sWindowTitle;
253 if(m_IsPull)
254 sWindowTitle.LoadString(IDS_PROGRS_TITLE_PULL);
255 else
256 sWindowTitle.LoadString(IDS_PROGRS_TITLE_FETCH);
258 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
260 Refresh();
262 EnableSaveRestore(L"PullFetchDlg");
263 this->m_RemoteManage.SetURL(CString());
264 return TRUE;
267 void CPullFetchDlg::Refresh()
269 //Select pull-remote from current branch
270 CString currentBranch;
271 if (g_Git.GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch))
272 currentBranch.Empty();
274 CString pullRemote, pullBranch;
275 g_Git.GetRemoteTrackedBranch(currentBranch, pullRemote, pullBranch);
276 m_configPullRemote = pullRemote;
277 m_configPullBranch = pullBranch;
279 if (pullBranch.IsEmpty())
280 m_RemoteBranch.AddString(currentBranch);
281 else
282 m_RemoteBranch.AddString(pullBranch);
284 if(pullRemote.IsEmpty())
285 pullRemote = m_RemoteReg;
287 if (!m_PreSelectRemote.IsEmpty())
288 pullRemote = m_PreSelectRemote;
290 STRING_VECTOR list;
291 m_Remote.Reset();
292 int sel=0;
293 if(!g_Git.GetRemoteList(list))
295 if (!m_IsPull && list.size() > 1)
296 m_Remote.AddString(CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)));
298 for (unsigned int i = 0; i < list.size(); ++i)
300 m_Remote.AddString(list[i]);
301 if (!m_bAllRemotes && list[i] == pullRemote)
302 sel = i + (!m_IsPull && list.size() > 1 ? 1 : 0);
305 m_Remote.SetCurSel(sel);
306 OnCbnSelchangeRemote();
308 // CPullFetchDlg message handlers
310 void CPullFetchDlg::OnCbnSelchangeRemote()
312 CString remote = m_Remote.GetString();
313 if (remote.IsEmpty() || remote == CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)))
315 GetDlgItem(IDC_STATIC_TAGOPT)->SetWindowText(L"");
316 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(L"");
317 return;
320 CString key;
321 key.Format(L"remote.%s.tagopt", (LPCTSTR)remote);
322 CString tagopt = g_Git.GetConfigValue(key);
323 if (tagopt == "--no-tags")
324 tagopt.LoadString(IDS_NONE);
325 else if (tagopt == "--tags")
326 tagopt.LoadString(IDS_ALL);
327 else
328 tagopt.LoadString(IDS_FETCH_REACHABLE);
329 CString value;
330 value.Format(L"%s: %s", (LPCTSTR)CString(MAKEINTRESOURCE(IDS_DEFAULT)), (LPCTSTR)tagopt);
331 GetDlgItem(IDC_STATIC_TAGOPT)->SetWindowText(value);
333 key.Format(L"remote.%s.prune", (LPCTSTR)remote);
334 CString prune = g_Git.GetConfigValue(key);
335 if (prune.IsEmpty())
336 prune = g_Git.GetConfigValue(L"fetch.prune");
337 if (!prune.IsEmpty())
339 value.Format(L"%s: %s", (LPCTSTR)CString(MAKEINTRESOURCE(IDS_DEFAULT)), (LPCTSTR)prune);
340 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(value);
342 else
343 GetDlgItem(IDC_STATIC_PRUNE)->SetWindowText(L"");
346 void CPullFetchDlg::OnBnClickedRd()
348 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_REMOTE_RD)
350 m_Remote.EnableWindow(TRUE);
351 m_Other.EnableWindow(FALSE);
352 DialogEnableWindow(IDC_CHECK_REBASE, TRUE);
353 m_bRebase = m_bRebaseActivatedInConfigForPull;
354 UpdateData(FALSE);
355 if (!m_IsPull && m_bNamedRemoteFetchAll)
356 m_RemoteBranch.EnableWindow(FALSE);
358 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_OTHER_RD)
360 CString clippath = CAppUtils::GetClipboardLink(m_IsPull ? L"git pull" : L"git fetch", 1);
361 if (clippath.IsEmpty())
362 clippath = CAppUtils::GetClipboardLink(!m_IsPull ? L"git pull" : L"git fetch", 1);
363 if (clippath.IsEmpty())
364 m_Other.SetCurSel(0);
365 else
367 int argSeparator = clippath.Find(' ');
368 if (argSeparator > 1 && clippath.GetLength() > argSeparator + 2)
370 m_Other.SetWindowText(clippath.Left(argSeparator));
371 m_RemoteBranch.SetWindowText(clippath.Mid(argSeparator + 1));
373 else
374 m_Other.SetWindowText(clippath);
376 m_Remote.EnableWindow(FALSE);
377 m_Other.EnableWindow(TRUE);;
378 DialogEnableWindow(IDC_CHECK_REBASE, FALSE);
379 m_bRebase = FALSE;
380 UpdateData(FALSE);
381 if(!m_IsPull)
382 m_RemoteBranch.EnableWindow(TRUE);
386 void CPullFetchDlg::OnBnClickedOk()
388 this->UpdateData();
389 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_REMOTE_RD)
391 m_RemoteURL=m_Remote.GetString();
392 m_bAllRemotes = (m_Remote.GetCurSel() == 0 && m_Remote.GetCount() > 1 && !m_IsPull);
393 if (m_bNamedRemoteFetchAll && (!m_IsPull ||
394 (m_configPullRemote == m_RemoteURL && m_configPullBranch == m_RemoteBranch.GetString())))
395 //When fetching or when pulling from the configured tracking branch, dont explicitly set the remote branch name,
396 //because otherwise git will not update the remote tracking branches.
397 m_RemoteBranchName.Empty();
398 else
399 m_RemoteBranchName=m_RemoteBranch.GetString();
401 if( GetCheckedRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD) == IDC_OTHER_RD)
403 m_RemoteURL = m_Other.GetString();
404 m_RemoteBranchName=m_RemoteBranch.GetString();
406 // only store URL in history if it's value was used
407 m_Other.SaveHistory();
410 if (m_bRebase && m_RemoteBranch.GetString().IsEmpty() && m_IsPull)
412 CMessageBox::Show(GetSafeHwnd(), IDS_PROC_PULL_EMPTYBRANCH, IDS_APPNAME, MB_ICONEXCLAMATION);
413 return;
416 m_RemoteReg = m_Remote.GetString();
418 m_RemoteBranch.SaveHistory();
419 this->m_regRebase=this->m_bRebase;
420 m_regFFonly = m_bFFonly;
422 m_regAutoLoadPutty = m_bAutoLoad;
424 this->OnOK();
427 void CPullFetchDlg::OnStnClickedRemoteManage()
429 CAppUtils::LaunchRemoteSetting();
430 Refresh();
433 void CPullFetchDlg::OnBnClickedButtonBrowseRef()
435 CString initialRef;
436 initialRef.Format(L"refs/remotes/%s/%s", (LPCTSTR)m_Remote.GetString(), (LPCTSTR)m_RemoteBranch.GetString());
437 CString selectedRef = CBrowseRefsDlg::PickRef(false, initialRef, gPickRef_Remote);
438 if (!CStringUtils::StartsWith(selectedRef, L"refs/remotes/"))
439 return;
441 selectedRef = selectedRef.Mid(13);
442 int ixSlash = selectedRef.Find('/');
444 CString remoteName = selectedRef.Left(ixSlash);
445 CString remoteBranch = selectedRef.Mid(ixSlash + 1);
447 int ixFound = m_Remote.FindStringExact(0, remoteName);
448 if(ixFound >= 0)
449 m_Remote.SetCurSel(ixFound);
450 m_RemoteBranch.AddString(remoteBranch, 0);
452 CheckRadioButton(IDC_REMOTE_RD,IDC_OTHER_RD,IDC_REMOTE_RD);
455 void CPullFetchDlg::OnBnClickedCheckDepth()
457 UpdateData(TRUE);
458 GetDlgItem(IDC_EDIT_DEPTH)->EnableWindow(m_bDepth);
461 void CPullFetchDlg::OnBnClickedCheckFfonly()
463 UpdateData();
464 if (m_bNoFF)
466 m_bFFonly = FALSE;
467 GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(FALSE);
469 else
470 GetDlgItem(IDC_CHECK_FFONLY)->EnableWindow(TRUE);
471 if (m_bFFonly)
473 m_bNoFF = FALSE;
474 GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(FALSE);
476 else
477 GetDlgItem(IDC_CHECK_NOFF)->EnableWindow(TRUE);
478 UpdateData(FALSE);