Improve line margin icon for moved lines
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
bloba313c0917698fbcc95d316c7e4cfbbc0472c2d48
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2017 - 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 // RebaseDlg.cpp : implementation file
23 #include "stdafx.h"
24 #include "TortoiseProc.h"
25 #include "RebaseDlg.h"
26 #include "AppUtils.h"
27 #include "LoglistUtils.h"
28 #include "MessageBox.h"
29 #include "UnicodeUtils.h"
30 #include "BrowseRefsDlg.h"
31 #include "ProgressDlg.h"
32 #include "SmartHandle.h"
33 #include "../TGitCache/CacheInterface.h"
34 #include "Settings\Settings.h"
35 #include "MassiveGitTask.h"
36 #include "CommitDlg.h"
37 #include "StringUtils.h"
38 #include "Hooks.h"
39 #include "LogDlg.h"
41 // CRebaseDlg dialog
43 IMPLEMENT_DYNAMIC(CRebaseDlg, CResizableStandAloneDialog)
45 CRebaseDlg::CRebaseDlg(CWnd* pParent /*=nullptr*/)
46 : CResizableStandAloneDialog(CRebaseDlg::IDD, pParent)
47 , m_bAddCherryPickedFrom(FALSE)
48 , m_bStatusWarning(false)
49 , m_bAutoSkipFailedCommit(FALSE)
50 , m_bFinishedRebase(false)
51 , m_bStashed(false)
52 , m_bSplitCommit(FALSE)
53 , m_bPreserveMerges(FALSE)
54 , m_bRebaseAutoStart(false)
55 , m_bRebaseAutoEnd(false)
56 , m_RebaseStage(CHOOSE_BRANCH)
57 , m_CurrentRebaseIndex(-1)
58 , m_bThreadRunning(FALSE)
59 , m_IsCherryPick(FALSE)
60 , m_bForce(BST_UNCHECKED)
61 , m_IsFastForward(FALSE)
62 , m_iSquashdate((int)CRegDWORD(L"Software\\TortoiseGit\\SquashDate", 0))
66 CRebaseDlg::~CRebaseDlg()
70 void CRebaseDlg::DoDataExchange(CDataExchange* pDX)
72 CDialog::DoDataExchange(pDX);
73 DDX_Control(pDX, IDC_REBASE_PROGRESS, m_ProgressBar);
74 DDX_Control(pDX, IDC_STATUS_STATIC, m_CtrlStatusText);
75 DDX_Control(pDX, IDC_REBASE_SPLIT, m_wndSplitter);
76 DDX_Control(pDX,IDC_COMMIT_LIST,m_CommitList);
77 DDX_Control(pDX,IDC_REBASE_COMBOXEX_BRANCH, this->m_BranchCtrl);
78 DDX_Control(pDX,IDC_REBASE_COMBOXEX_UPSTREAM, this->m_UpstreamCtrl);
79 DDX_Check(pDX, IDC_REBASE_CHECK_FORCE,m_bForce);
80 DDX_Check(pDX, IDC_REBASE_CHECK_PRESERVEMERGES, m_bPreserveMerges);
81 DDX_Check(pDX, IDC_CHECK_CHERRYPICKED_FROM, m_bAddCherryPickedFrom);
82 DDX_Control(pDX,IDC_REBASE_POST_BUTTON,m_PostButton);
83 DDX_Control(pDX, IDC_SPLITALLOPTIONS, m_SplitAllOptions);
84 DDX_Check(pDX, IDC_REBASE_SPLIT_COMMIT, m_bSplitCommit);
88 BEGIN_MESSAGE_MAP(CRebaseDlg, CResizableStandAloneDialog)
89 ON_BN_CLICKED(IDC_REBASE_SPLIT, &CRebaseDlg::OnBnClickedRebaseSplit)
90 ON_BN_CLICKED(IDC_REBASE_CONTINUE,OnBnClickedContinue)
91 ON_BN_CLICKED(IDC_REBASE_ABORT, OnBnClickedAbort)
92 ON_WM_SIZE()
93 ON_WM_THEMECHANGED()
94 ON_WM_SYSCOLORCHANGE()
95 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_BRANCH, &CRebaseDlg::OnCbnSelchangeBranch)
96 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_UPSTREAM, &CRebaseDlg::OnCbnSelchangeUpstream)
97 ON_MESSAGE(MSG_REBASE_UPDATE_UI, OnRebaseUpdateUI)
98 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnGitStatusListCtrlNeedsRefresh)
99 ON_BN_CLICKED(IDC_BUTTON_REVERSE, OnBnClickedButtonReverse)
100 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CRebaseDlg::OnBnClickedButtonBrowse)
101 ON_BN_CLICKED(IDC_REBASE_CHECK_FORCE, &CRebaseDlg::OnBnClickedRebaseCheckForce)
102 ON_BN_CLICKED(IDC_REBASE_CHECK_PRESERVEMERGES, &CRebaseDlg::OnBnClickedRebaseCheckForce)
103 ON_BN_CLICKED(IDC_CHECK_CHERRYPICKED_FROM, &CRebaseDlg::OnBnClickedCheckCherryPickedFrom)
104 ON_BN_CLICKED(IDC_REBASE_POST_BUTTON, &CRebaseDlg::OnBnClickedRebasePostButton)
105 ON_BN_CLICKED(IDC_BUTTON_UP, &CRebaseDlg::OnBnClickedButtonUp)
106 ON_BN_CLICKED(IDC_BUTTON_DOWN, &CRebaseDlg::OnBnClickedButtonDown)
107 ON_REGISTERED_MESSAGE(TaskBarButtonCreated, OnTaskbarBtnCreated)
108 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COMMIT_LIST, OnLvnItemchangedLoglist)
109 ON_REGISTERED_MESSAGE(CGitLogListBase::m_RebaseActionMessage, OnRebaseActionMessage)
110 ON_WM_CTLCOLOR()
111 ON_BN_CLICKED(IDC_SPLITALLOPTIONS, &CRebaseDlg::OnBnClickedSplitAllOptions)
112 ON_BN_CLICKED(IDC_REBASE_SPLIT_COMMIT, &CRebaseDlg::OnBnClickedRebaseSplitCommit)
113 ON_BN_CLICKED(IDC_BUTTON_ONTO, &CRebaseDlg::OnBnClickedButtonOnto)
114 ON_BN_CLICKED(IDHELP, OnHelp)
115 ON_BN_CLICKED(IDC_BUTTON_ADD, &CRebaseDlg::OnBnClickedButtonAdd)
116 ON_MESSAGE(MSG_COMMITS_REORDERED, OnCommitsReordered)
117 END_MESSAGE_MAP()
119 void CRebaseDlg::CleanUpRebaseActiveFolder()
121 if (m_IsCherryPick)
122 return;
123 CString adminDir;
124 if (GitAdminDir::GetAdminDirPath(g_Git.m_CurrentDir, adminDir))
126 CString dir(adminDir + L"tgitrebase.active");
127 ::DeleteFile(dir + L"\\head-name");
128 ::DeleteFile(dir + L"\\onto");
129 ::RemoveDirectory(dir);
133 void CRebaseDlg::AddRebaseAnchor()
135 AdjustControlSize(IDC_CHECK_CHERRYPICKED_FROM);
136 AdjustControlSize(IDC_REBASE_SPLIT_COMMIT);
137 AdjustControlSize(IDC_REBASE_CHECK_FORCE);
138 AdjustControlSize(IDC_REBASE_CHECK_PRESERVEMERGES);
140 AddAnchor(IDC_REBASE_TAB,TOP_LEFT,BOTTOM_RIGHT);
141 AddAnchor(IDC_COMMIT_LIST,TOP_LEFT, TOP_RIGHT);
142 AddAnchor(IDC_REBASE_SPLIT,TOP_LEFT, TOP_RIGHT);
143 AddAnchor(IDC_STATUS_STATIC, BOTTOM_LEFT,BOTTOM_RIGHT);
144 AddAnchor(IDC_REBASE_CONTINUE,BOTTOM_RIGHT);
145 AddAnchor(IDC_REBASE_ABORT, BOTTOM_RIGHT);
146 AddAnchor(IDC_REBASE_PROGRESS,BOTTOM_LEFT, BOTTOM_RIGHT);
147 AddAnchor(IDC_SPLITALLOPTIONS, TOP_LEFT);
148 AddAnchor(IDC_BUTTON_UP, TOP_LEFT);
149 AddAnchor(IDC_BUTTON_DOWN, TOP_LEFT);
150 AddAnchor(IDC_BUTTON_ADD, TOP_LEFT);
151 AddAnchor(IDC_REBASE_COMBOXEX_UPSTREAM, TOP_CENTER, TOP_RIGHT);
152 AddAnchor(IDC_REBASE_COMBOXEX_BRANCH, TOP_LEFT, TOP_CENTER);
153 AddAnchor(IDC_BUTTON_REVERSE, TOP_CENTER);
154 AddAnchor(IDC_BUTTON_BROWSE, TOP_RIGHT);
155 AddAnchor(IDC_BUTTON_ONTO, TOP_RIGHT);
156 AddAnchor(IDC_REBASE_STATIC_UPSTREAM, TOP_CENTER);
157 AddAnchor(IDC_REBASE_STATIC_BRANCH,TOP_LEFT);
158 AddAnchor(IDHELP, BOTTOM_RIGHT);
159 AddAnchor(IDC_REBASE_CHECK_FORCE, TOP_CENTER, TOP_RIGHT);
160 AddAnchor(IDC_REBASE_CHECK_PRESERVEMERGES, TOP_LEFT, TOP_CENTER);
161 AddAnchor(IDC_CHECK_CHERRYPICKED_FROM, TOP_RIGHT);
162 AddAnchor(IDC_REBASE_SPLIT_COMMIT, BOTTOM_RIGHT);
163 AddAnchor(IDC_REBASE_POST_BUTTON,BOTTOM_LEFT);
165 this->AddOthersToAnchor();
168 BOOL CRebaseDlg::OnInitDialog()
170 CResizableStandAloneDialog::OnInitDialog();
171 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
173 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
174 // do this, Explorer would be unable to send that message to our window if we
175 // were running elevated. It's OK to make the call all the time, since if we're
176 // not elevated, this is a no-op.
177 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
178 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
179 CAutoLibrary hUser = AtlLoadSystemLibraryUsingFullPath(L"user32.dll");
180 if (hUser)
182 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
183 if (pfnChangeWindowMessageFilterEx)
185 pfnChangeWindowMessageFilterEx(m_hWnd, TaskBarButtonCreated, MSGFLT_ALLOW, &cfs);
188 m_pTaskbarList.Release();
189 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
190 m_pTaskbarList = nullptr;
192 CRect rectDummy;
193 //IDC_REBASE_DUMY_TAB
195 GetClientRect(m_DlgOrigRect);
196 m_CommitList.GetClientRect(m_CommitListOrigRect);
198 CWnd *pwnd=this->GetDlgItem(IDC_REBASE_DUMY_TAB);
199 pwnd->GetWindowRect(&rectDummy);
200 this->ScreenToClient(rectDummy);
202 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_REBASE_TAB))
204 TRACE0("Failed to create output tab window\n");
205 return FALSE; // fail to create
207 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
208 // Create output panes:
209 //const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
210 DWORD dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
212 if (! this->m_FileListCtrl.Create(dwStyle,rectDummy,&this->m_ctrlTabCtrl,0) )
214 TRACE0("Failed to create output windows\n");
215 return FALSE; // fail to create
217 m_FileListCtrl.m_hwndLogicalParent = this;
219 if (!m_LogMessageCtrl.Create(L"Scintilla", L"source", 0, rectDummy, &m_ctrlTabCtrl, 0, 0))
221 TRACE0("Failed to create log message control");
222 return FALSE;
224 m_ProjectProperties.ReadProps();
225 m_LogMessageCtrl.Init(m_ProjectProperties);
226 m_LogMessageCtrl.SetFont(CAppUtils::GetLogFontName(), CAppUtils::GetLogFontSize());
227 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
229 dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
231 if (!m_wndOutputRebase.Create(L"Scintilla", L"source", 0, rectDummy, &m_ctrlTabCtrl, 0, 0))
233 TRACE0("Failed to create output windows\n");
234 return -1; // fail to create
236 m_wndOutputRebase.Init(-1);
237 m_wndOutputRebase.SetFont(CAppUtils::GetLogFontName(), CAppUtils::GetLogFontSize());
238 m_wndOutputRebase.Call(SCI_SETREADONLY, TRUE);
240 m_tooltips.AddTool(IDC_REBASE_CHECK_FORCE,IDS_REBASE_FORCE_TT);
241 m_tooltips.AddTool(IDC_REBASE_ABORT, IDS_REBASE_ABORT_TT);
242 m_tooltips.AddTool(IDC_REBASE_CHECK_PRESERVEMERGES, IDS_REBASE_PRESERVEMERGES_TT);
245 CString temp;
246 temp.LoadString(IDS_PROC_REBASE_SELECTALL_PICK);
247 m_SplitAllOptions.AddEntry(temp);
248 temp.LoadString(IDS_PROC_REBASE_SELECTALL_SQUASH);
249 m_SplitAllOptions.AddEntry(temp);
250 temp.LoadString(IDS_PROC_REBASE_SELECTALL_EDIT);
251 m_SplitAllOptions.AddEntry(temp);
252 temp.LoadString(IDS_PROC_REBASE_UNSELECTED_SKIP);
253 m_SplitAllOptions.AddEntry(temp);
254 temp.LoadString(IDS_PROC_REBASE_UNSELECTED_SQUASH);
255 m_SplitAllOptions.AddEntry(temp);
256 temp.LoadString(IDS_PROC_REBASE_UNSELECTED_EDIT);
257 m_SplitAllOptions.AddEntry(temp);
260 m_FileListCtrl.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD | GITSLC_COLDEL, L"RebaseDlg", (GITSLC_POPALL ^ (GITSLC_POPCOMMIT | GITSLC_POPRESTORE)), false, true, GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL);
262 m_ctrlTabCtrl.AddTab(&m_FileListCtrl, CString(MAKEINTRESOURCE(IDS_PROC_REVISIONFILES)));
263 m_ctrlTabCtrl.AddTab(&m_LogMessageCtrl, CString(MAKEINTRESOURCE(IDS_PROC_COMMITMESSAGE)), 1);
264 AddRebaseAnchor();
266 CString sWindowTitle;
267 GetWindowText(sWindowTitle);
268 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
270 EnableSaveRestore(L"RebaseDlg");
272 DWORD yPos = CRegDWORD(L"Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer");
273 RECT rcDlg, rcLogMsg, rcFileList;
274 GetClientRect(&rcDlg);
275 m_CommitList.GetWindowRect(&rcLogMsg);
276 ScreenToClient(&rcLogMsg);
277 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
278 ScreenToClient(&rcFileList);
279 if (yPos)
281 RECT rectSplitter;
282 m_wndSplitter.GetWindowRect(&rectSplitter);
283 ScreenToClient(&rectSplitter);
284 int delta = yPos - rectSplitter.top;
285 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
287 m_wndSplitter.SetWindowPos(nullptr, 0, yPos, 0, 0, SWP_NOSIZE);
288 DoSize(delta);
292 if (this->m_RebaseStage == CHOOSE_BRANCH && !m_IsCherryPick)
293 this->LoadBranchInfo();
294 else
296 this->m_BranchCtrl.EnableWindow(FALSE);
297 this->m_UpstreamCtrl.EnableWindow(FALSE);
298 GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
301 m_CommitList.m_ColumnRegKey = L"Rebase";
302 m_CommitList.m_IsIDReplaceAction = TRUE;
303 // m_CommitList.m_IsOldFirst = TRUE;
304 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
305 m_CommitList.m_bNoHightlightHead = TRUE;
306 m_CommitList.m_bIsCherryPick = !!m_IsCherryPick;
308 m_CommitList.InsertGitColumn();
310 this->SetControlEnable();
312 if(m_IsCherryPick)
314 this->m_BranchCtrl.SetCurSel(-1);
315 this->m_BranchCtrl.EnableWindow(FALSE);
316 GetDlgItem(IDC_REBASE_CHECK_FORCE)->ShowWindow(SW_HIDE);
317 GetDlgItem(IDC_REBASE_CHECK_PRESERVEMERGES)->ShowWindow(SW_HIDE);
318 GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(FALSE);
319 GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
320 GetDlgItem(IDC_BUTTON_ONTO)->EnableWindow(FALSE);
321 this->m_UpstreamCtrl.AddString(L"HEAD");
322 this->m_UpstreamCtrl.EnableWindow(FALSE);
323 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_CHERRYPICK)));
324 this->m_CommitList.StartFilter();
326 else
328 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
329 GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM)->ShowWindow(SW_HIDE);
330 int iconWidth = GetSystemMetrics(SM_CXSMICON);
331 int iconHeight = GetSystemMetrics(SM_CYSMICON);
332 ((CButton*)GetDlgItem(IDC_BUTTON_REVERSE))->SetIcon((HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SWITCHLEFTRIGHT), IMAGE_ICON, iconWidth, iconHeight, LR_DEFAULTCOLOR));
333 SetContinueButtonText();
334 m_CommitList.DeleteAllItems();
335 FetchLogList();
338 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
339 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
340 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
341 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
342 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_MERGEREV) |
343 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
344 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
345 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
347 if(m_CommitList.m_IsOldFirst)
348 this->m_CurrentRebaseIndex = -1;
349 else
350 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
352 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled() && m_bRebaseAutoStart)
353 this->PostMessage(WM_COMMAND, MAKELONG(IDC_REBASE_CONTINUE, BN_CLICKED), (LPARAM)GetDlgItem(IDC_REBASE_CONTINUE)->GetSafeHwnd());
355 return TRUE;
357 // CRebaseDlg message handlers
359 HBRUSH CRebaseDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
361 if (pWnd->GetDlgCtrlID() == IDC_STATUS_STATIC && nCtlColor == CTLCOLOR_STATIC && m_bStatusWarning)
363 pDC->SetBkColor(RGB(255, 0, 0));
364 pDC->SetTextColor(RGB(255, 255, 255));
365 return CreateSolidBrush(RGB(255, 0, 0));
368 return CResizableStandAloneDialog::OnCtlColor(pDC, pWnd, nCtlColor);
371 void CRebaseDlg::SetAllRebaseAction(int action)
373 for (size_t i = 0; i < this->m_CommitList.m_logEntries.size(); ++i)
375 if (action == CGitLogListBase::LOGACTIONS_REBASE_SQUASH && (i == this->m_CommitList.m_logEntries.size() - 1 || (!m_IsCherryPick && m_CommitList.m_logEntries.GetGitRevAt(i).ParentsCount() != 1)))
376 continue;
377 m_CommitList.m_logEntries.GetGitRevAt(i).GetRebaseAction() = action;
379 m_CommitList.Invalidate();
382 void CRebaseDlg::OnBnClickedRebaseSplit()
384 this->UpdateData();
387 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
389 switch (message) {
390 case WM_NOTIFY:
391 if (wParam == IDC_REBASE_SPLIT)
393 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
394 DoSize(pHdr->delta);
396 break;
399 return __super::DefWindowProc(message, wParam, lParam);
402 void CRebaseDlg::DoSize(int delta)
404 this->RemoveAllAnchors();
406 auto hdwp = BeginDeferWindowPos(9);
407 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_COMMIT_LIST), 0, 0, 0, delta);
408 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_REBASE_TAB), 0, delta, 0, 0);
409 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_SPLITALLOPTIONS), 0, delta, 0, delta);
410 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_BUTTON_UP), 0, delta, 0, delta);
411 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_BUTTON_DOWN), 0, delta, 0, delta);
412 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_BUTTON_ADD), 0, delta, 0, delta);
413 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_REBASE_CHECK_FORCE), 0, delta, 0, delta);
414 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_REBASE_CHECK_PRESERVEMERGES), 0, delta, 0, delta);
415 hdwp = CSplitterControl::ChangeRect(hdwp, GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM), 0, delta, 0, delta);
416 EndDeferWindowPos(hdwp);
418 this->AddRebaseAnchor();
419 // adjust the minimum size of the dialog to prevent the resizing from
420 // moving the list control too far down.
421 CRect rcLogMsg;
422 m_CommitList.GetClientRect(rcLogMsg);
423 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
425 SetSplitterRange();
426 // m_CommitList.Invalidate();
428 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
430 this->m_ctrlTabCtrl.Invalidate();
431 this->m_CommitList.Invalidate();
432 this->m_FileListCtrl.Invalidate();
433 this->m_LogMessageCtrl.Invalidate();
434 m_SplitAllOptions.Invalidate();
435 GetDlgItem(IDC_REBASE_CHECK_FORCE)->Invalidate();
436 GetDlgItem(IDC_REBASE_CHECK_PRESERVEMERGES)->Invalidate();
437 GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM)->Invalidate();
438 GetDlgItem(IDC_BUTTON_UP)->Invalidate();
439 GetDlgItem(IDC_BUTTON_DOWN)->Invalidate();
440 GetDlgItem(IDC_BUTTON_ADD)->Invalidate();
443 void CRebaseDlg::SetSplitterRange()
445 if ((m_CommitList)&&(m_ctrlTabCtrl))
447 CRect rcTop;
448 m_CommitList.GetWindowRect(rcTop);
449 ScreenToClient(rcTop);
450 CRect rcMiddle;
451 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
452 ScreenToClient(rcMiddle);
453 if (rcMiddle.Height() && rcMiddle.Width())
454 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
458 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
460 // first, let the resizing take place
461 __super::OnSize(nType, cx, cy);
463 //set range
464 SetSplitterRange();
467 void CRebaseDlg::SaveSplitterPos()
469 if (!IsIconic())
471 CRegDWORD regPos = CRegDWORD(L"Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer");
472 RECT rectSplitter;
473 m_wndSplitter.GetWindowRect(&rectSplitter);
474 ScreenToClient(&rectSplitter);
475 regPos = rectSplitter.top;
479 void CRebaseDlg::LoadBranchInfo()
481 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
482 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
484 STRING_VECTOR list;
485 list.clear();
486 int current = -1;
487 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
488 m_BranchCtrl.SetList(list);
489 if (current >= 0)
490 m_BranchCtrl.SetCurSel(current);
491 else
492 m_BranchCtrl.AddString(g_Git.GetCurrentBranch(true));
493 list.clear();
494 g_Git.GetBranchList(list, nullptr, CGit::BRANCH_ALL_F);
495 g_Git.GetTagList(list);
496 m_UpstreamCtrl.SetList(list);
498 AddBranchToolTips(&m_BranchCtrl);
500 if(!m_Upstream.IsEmpty())
501 m_UpstreamCtrl.AddString(m_Upstream);
502 else
504 //Select pull-remote from current branch
505 CString pullRemote, pullBranch;
506 g_Git.GetRemoteTrackedBranchForHEAD(pullRemote, pullBranch);
508 CString defaultUpstream;
509 defaultUpstream.Format(L"remotes/%s/%s", (LPCTSTR)pullRemote, (LPCTSTR)pullBranch);
510 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
511 if(found >= 0)
512 m_UpstreamCtrl.SetCurSel(found);
513 else
514 m_UpstreamCtrl.SetCurSel(-1);
516 AddBranchToolTips(&m_UpstreamCtrl);
519 void CRebaseDlg::OnCbnSelchangeBranch()
521 FetchLogList();
524 void CRebaseDlg::OnCbnSelchangeUpstream()
526 FetchLogList();
529 void CRebaseDlg::FetchLogList()
531 CGitHash base,hash,upstream;
532 m_IsFastForward=FALSE;
534 if (m_BranchCtrl.GetString().IsEmpty())
536 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_SELECTBRANCH)));
537 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
538 return;
541 if (g_Git.GetHash(hash, m_BranchCtrl.GetString()))
543 m_CommitList.ShowText(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_BranchCtrl.GetString() + L"\"."));
544 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
545 return;
548 if (m_UpstreamCtrl.GetString().IsEmpty())
550 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_SELECTUPSTREAM)));
551 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
552 return;
555 if (g_Git.GetHash(upstream, m_UpstreamCtrl.GetString()))
557 m_CommitList.ShowText(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_UpstreamCtrl.GetString() + L"\"."));
558 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
559 return;
562 if (hash == upstream)
564 m_CommitList.Clear();
565 CString text;
566 text.Format(IDS_REBASE_EQUAL_FMT, (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)this->m_UpstreamCtrl.GetString());
568 m_CommitList.ShowText(text);
569 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
570 if (m_bRebaseAutoStart)
571 PostMessage(WM_COMMAND, MAKELONG(IDC_REBASE_ABORT, BN_CLICKED), (LPARAM)GetDlgItem(IDC_REBASE_ABORT)->GetSafeHwnd());
572 return;
575 if (g_Git.IsFastForward(m_BranchCtrl.GetString(), m_UpstreamCtrl.GetString(), &base) && m_Onto.IsEmpty())
577 this->m_IsFastForward=TRUE;
579 m_CommitList.Clear();
580 CString text;
581 text.Format(IDS_REBASE_FASTFORWARD_FMT, (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)this->m_UpstreamCtrl.GetString(),
582 (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)this->m_UpstreamCtrl.GetString());
584 m_CommitList.ShowText(text);
585 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
586 SetContinueButtonText();
588 return ;
591 if (!m_bForce && m_Onto.IsEmpty())
593 if (base == upstream)
595 m_CommitList.Clear();
596 CString text;
597 text.Format(IDS_REBASE_UPTODATE_FMT, (LPCTSTR)m_BranchCtrl.GetString());
598 m_CommitList.ShowText(text);
599 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
600 SetContinueButtonText();
601 if (m_bRebaseAutoStart)
602 PostMessage(WM_COMMAND, MAKELONG(IDC_REBASE_ABORT, BN_CLICKED), (LPARAM)GetDlgItem(IDC_REBASE_ABORT)->GetSafeHwnd());
603 return;
607 m_CommitList.Clear();
608 CString refFrom = g_Git.FixBranchName(m_UpstreamCtrl.GetString());
609 CString refTo = g_Git.FixBranchName(m_BranchCtrl.GetString());
610 CString range;
611 range.Format(L"%s..%s", (LPCTSTR)refFrom, (LPCTSTR)refTo);
612 this->m_CommitList.FillGitLog(nullptr, &range, (m_bPreserveMerges ? 0 : CGit::LOG_INFO_NO_MERGE) | CGit::LOG_ORDER_TOPOORDER);
614 if( m_CommitList.GetItemCount() == 0 )
615 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_NOTHINGTOREBASE)));
617 m_rewrittenCommitsMap.clear();
618 if (m_bPreserveMerges)
620 CGitHash head;
621 if (g_Git.GetHash(head, L"HEAD"))
623 AddLogString(CString(MAKEINTRESOURCE(IDS_PROC_NOHEAD)));
624 return;
626 CGitHash upstreamHash;
627 if (g_Git.GetHash(upstreamHash, m_Onto.IsEmpty() ? m_UpstreamCtrl.GetString() : m_Onto))
629 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + (m_Onto.IsEmpty() ? m_UpstreamCtrl.GetString() : m_Onto) + L"\"."), L"TortoiseGit", MB_ICONERROR);
630 return;
632 CString mergecmd;
633 mergecmd.Format(L"git merge-base --all %s %s", (LPCTSTR)head.ToString(), (LPCTSTR)upstreamHash.ToString());
634 g_Git.Run(mergecmd, [&](const CStringA& line)
636 CGitHash hash;
637 hash.ConvertFromStrA(line);
638 if (hash.IsEmpty())
639 return;
640 m_rewrittenCommitsMap[hash] = upstreamHash;
643 std::vector<size_t> toDrop;
644 for (size_t i = m_CommitList.m_arShownList.size(); i-- > 0;)
646 bool preserve = false;
647 GitRevLoglist* pRev = m_CommitList.m_arShownList.SafeGetAt(i);
648 for (const auto& parent : pRev->m_ParentHash)
650 const auto rewrittenParent = m_rewrittenCommitsMap.find(parent);
651 if (rewrittenParent != m_rewrittenCommitsMap.cend())
653 preserve = true;
654 break;
657 if (preserve)
658 m_rewrittenCommitsMap[pRev->m_CommitHash] = CGitHash();
659 else
660 toDrop.push_back(i);
663 // Drop already included commits
664 std::vector<CGitHash> nonCherryPicked;
665 CString cherryCmd;
666 cherryCmd.Format(L"git rev-list \"%s...%s\" --left-right --cherry-pick", (LPCTSTR)refFrom, (LPCTSTR)refTo);
667 g_Git.Run(cherryCmd, [&](const CStringA& line)
669 if (line.GetLength() < 2)
670 return;
671 if (line[0] != '>')
672 return;
673 CString hash = CUnicodeUtils::GetUnicode(line.Mid(1));
674 hash.Trim();
675 nonCherryPicked.emplace_back(hash);
677 for (size_t i = m_CommitList.m_arShownList.size(); i-- > 0;)
679 GitRevLoglist* pRev = m_CommitList.m_arShownList.SafeGetAt(i);
680 pRev->GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_PICK;
681 if (m_rewrittenCommitsMap.find(pRev->m_CommitHash) != m_rewrittenCommitsMap.cend() && std::find(nonCherryPicked.cbegin(), nonCherryPicked.cend(), pRev->m_CommitHash) == nonCherryPicked.cend())
683 m_droppedCommitsMap[pRev->m_CommitHash].clear();
684 m_droppedCommitsMap[pRev->m_CommitHash].push_back(pRev->m_ParentHash[0]);
685 toDrop.push_back(i);
686 m_rewrittenCommitsMap.erase(pRev->m_CommitHash);
689 std::sort(toDrop.begin(), toDrop.end());
690 toDrop.erase(unique(toDrop.begin(), toDrop.end()), toDrop.end());
691 for (auto it = toDrop.crbegin(); it != toDrop.crend(); ++it)
693 m_CommitList.m_arShownList.SafeRemoveAt(*it);
694 m_CommitList.m_logEntries.erase(m_CommitList.m_logEntries.begin() + *it);
696 m_CommitList.SetItemCountEx((int)m_CommitList.m_logEntries.size());
699 #if 0
700 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
702 if(upstream == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
704 m_CommitList.Clear();
705 m_CommitList.ShowText(L"Nothing Rebase");
708 #endif
710 m_tooltips.Pop();
711 AddBranchToolTips(&this->m_BranchCtrl);
712 AddBranchToolTips(&this->m_UpstreamCtrl);
714 bool bHasSKip = false;
715 if (!m_bPreserveMerges)
717 // Default all actions to 'pick'
718 std::unordered_map<CGitHash, size_t> revIxMap;
719 for (size_t i = 0; i < m_CommitList.m_logEntries.size(); ++i)
721 GitRevLoglist& rev = m_CommitList.m_logEntries.GetGitRevAt(i);
722 rev.GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_PICK;
723 revIxMap[rev.m_CommitHash] = i;
726 // Default to skip when already in upstream
727 if (!m_Onto.IsEmpty())
728 refFrom = g_Git.FixBranchName(m_Onto);
729 CString cherryCmd;
730 cherryCmd.Format(L"git.exe cherry \"%s\" \"%s\"", (LPCTSTR)refFrom, (LPCTSTR)refTo);
731 g_Git.Run(cherryCmd, [&](const CStringA& line)
733 if (line.GetLength() < 2)
734 return;
735 if (line[0] != '-')
736 return; // Don't skip (only skip commits starting with a '-')
737 CString hash = CUnicodeUtils::GetUnicode(line.Mid(1));
738 hash.Trim();
739 auto itIx = revIxMap.find(CGitHash(hash));
740 if (itIx == revIxMap.end())
741 return; // Not found?? Should not occur...
743 // Found. Skip it.
744 m_CommitList.m_logEntries.GetGitRevAt(itIx->second).GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_SKIP;
745 bHasSKip = true;
748 m_CommitList.Invalidate();
749 if (bHasSKip)
751 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_REBASE_AUTOSKIPPED)));
752 m_bStatusWarning = true;
754 else
756 m_CtrlStatusText.SetWindowText(m_sStatusText);
757 m_bStatusWarning = false;
759 m_CtrlStatusText.Invalidate();
761 if(m_CommitList.m_IsOldFirst)
762 this->m_CurrentRebaseIndex = -1;
763 else
764 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
766 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_bPreserveMerges || m_CommitList.GetItemCount());
767 SetContinueButtonText();
770 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
772 if(pBranch)
774 CString text=pBranch->GetString();
775 CString tooltip;
777 if (text.IsEmpty())
779 pBranch->DisableTooltip();
780 return;
783 GitRev rev;
784 if (rev.GetCommit(text + L"^{}"))
786 MessageBox(L"Failed to get commit.\n" + rev.GetLastErr(), L"TortoiseGit", MB_ICONERROR);
787 pBranch->DisableTooltip();
788 return;
791 tooltip.Format(L"%s: %s\n%s: %s <%s>\n%s: %s\n%s:\n%s\n%s",
792 (LPCTSTR)CString(MAKEINTRESOURCE(IDS_LOG_REVISION)),
793 (LPCTSTR)rev.m_CommitHash.ToString(),
794 (LPCTSTR)CString(MAKEINTRESOURCE(IDS_LOG_AUTHOR)),
795 (LPCTSTR)rev.GetAuthorName(),
796 (LPCTSTR)rev.GetAuthorEmail(),
797 (LPCTSTR)CString(MAKEINTRESOURCE(IDS_LOG_DATE)),
798 (LPCTSTR)CLoglistUtils::FormatDateAndTime(rev.GetAuthorDate(), DATE_LONGDATE),
799 (LPCTSTR)CString(MAKEINTRESOURCE(IDS_LOG_MESSAGE)),
800 (LPCTSTR)rev.GetSubject(),
801 (LPCTSTR)rev.GetBody());
803 pBranch->DisableTooltip();
804 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
808 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
810 if (pMsg->message == WM_KEYDOWN)
812 switch (pMsg->wParam)
814 case ' ':
815 if (LogListHasFocus(pMsg->hwnd)
816 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_PICK)
817 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SQUASH)
818 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_EDIT)
819 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SKIP))
821 m_CommitList.ShiftSelectedRebaseAction();
822 return TRUE;
824 break;
825 case 'P':
826 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_PICK))
828 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_PICK);
829 return TRUE;
831 break;
832 case 'S':
833 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SKIP))
835 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SKIP);
836 return TRUE;
838 break;
839 case 'Q':
840 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SQUASH))
842 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SQUASH);
843 return TRUE;
845 break;
846 case 'E':
847 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_EDIT))
849 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_EDIT);
850 return TRUE;
852 break;
853 case 'U':
854 if (LogListHasFocus(pMsg->hwnd))
856 OnBnClickedButtonDown();
857 return TRUE;
859 break;
860 case 'D':
861 if (LogListHasFocus(pMsg->hwnd))
863 OnBnClickedButtonUp();
864 return TRUE;
866 break;
867 case 'A':
868 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
870 // select all entries
871 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
872 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
873 return TRUE;
875 break;
876 case VK_F5:
878 Refresh();
879 return TRUE;
881 break;
882 case VK_RETURN:
884 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
886 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
887 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
888 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
889 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
890 else
891 GetDlgItem(IDHELP)->SetFocus();
892 return TRUE;
895 break;
896 /* Avoid TAB control destroy but dialog exist*/
897 case VK_ESCAPE:
898 case VK_CANCEL:
900 TCHAR buff[128] = { 0 };
901 ::GetClassName(pMsg->hwnd,buff,128);
904 /* Use MSFTEDIT_CLASS http://msdn.microsoft.com/en-us/library/bb531344.aspx */
905 if (_wcsnicmp(buff, MSFTEDIT_CLASS, 128) == 0 || //Unicode and MFC 2012 and later
906 _wcsnicmp(buff, RICHEDIT_CLASS, 128) == 0 || //ANSI or MFC 2010
907 _wcsnicmp(buff, L"Scintilla", 128) == 0 ||
908 _wcsnicmp(buff, L"SysListView32", 128) == 0 ||
909 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
911 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
912 return TRUE;
917 else if (pMsg->message == WM_NEXTDLGCTL)
919 HWND hwnd = GetFocus()->GetSafeHwnd();
920 if (hwnd == m_LogMessageCtrl.GetSafeHwnd() || hwnd == m_wndOutputRebase.GetSafeHwnd())
922 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
923 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
924 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
925 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
926 else
927 GetDlgItem(IDHELP)->SetFocus();
928 return TRUE;
931 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
934 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
936 TCHAR buff[128] = { 0 };
937 ::GetClassName(hwnd, buff, 128);
939 if (_wcsnicmp(buff, L"SysListView32", 128) == 0)
940 return true;
941 return false;
944 bool CRebaseDlg::LogListHasMenuItem(int i)
946 return (m_CommitList.m_ContextMenuMask & m_CommitList.GetContextMenuBit(i)) != 0;
949 int CRebaseDlg::CheckRebaseCondition()
951 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
953 if( !g_Git.CheckCleanWorkTree() )
955 if ((!m_IsCherryPick && g_Git.GetConfigValueBool(L"rebase.autostash")) || CMessageBox::Show(GetSafeHwnd(), IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
957 CString out;
958 CString cmd = L"git.exe stash";
959 this->AddLogString(cmd);
960 if (g_Git.Run(cmd, &out, CP_UTF8))
962 MessageBox(out, L"TortoiseGit", MB_OK | MB_ICONERROR);
963 return -1;
965 m_bStashed = true;
967 else
968 return -1;
970 //Todo Check $REBASE_ROOT
971 //Todo Check $DOTEST
973 if (!CAppUtils::CheckUserData())
974 return -1;
976 if (!m_IsCherryPick)
978 CString error;
979 DWORD exitcode = 0xFFFFFFFF;
980 if (CHooks::Instance().PreRebase(g_Git.m_CurrentDir, m_UpstreamCtrl.GetString(), m_BranchCtrl.GetString(), exitcode, error))
982 if (exitcode)
984 CString temp;
985 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
986 MessageBox(temp, L"TortoiseGit", MB_OK | MB_ICONERROR);
987 return -1;
992 return 0;
995 void CRebaseDlg::CheckRestoreStash()
997 bool autoStash = !m_IsCherryPick && g_Git.GetConfigValueBool(L"rebase.autostash");
998 if (m_bStashed && (autoStash || CMessageBox::Show(GetSafeHwnd(), IDS_DCOMMIT_STASH_POP, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) == IDYES))
999 CAppUtils::StashPop(autoStash ? 0 : 1);
1000 m_bStashed = false;
1003 int CRebaseDlg::StartRebase()
1005 CString cmd,out;
1006 m_OrigHEADBranch = g_Git.GetCurrentBranch(true);
1008 m_OrigHEADHash.Empty();
1009 if (g_Git.GetHash(m_OrigHEADHash, L"HEAD"))
1011 AddLogString(CString(MAKEINTRESOURCE(IDS_PROC_NOHEAD)));
1012 return -1;
1014 //Todo
1015 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1016 // echo "detached HEAD" > "$DOTEST"/head-name
1018 cmd.Format(L"git.exe update-ref ORIG_HEAD %s", (LPCTSTR)m_OrigHEADHash.ToString());
1019 if(g_Git.Run(cmd,&out,CP_UTF8))
1021 AddLogString(L"update ORIG_HEAD Fail");
1022 return -1;
1025 m_OrigUpstreamHash.Empty();
1026 if (g_Git.GetHash(m_OrigUpstreamHash, (m_IsCherryPick || m_Onto.IsEmpty()) ? m_UpstreamCtrl.GetString() : m_Onto))
1028 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + ((m_IsCherryPick || m_Onto.IsEmpty()) ? m_UpstreamCtrl.GetString() : m_Onto) + L"\"."), L"TortoiseGit", MB_ICONERROR);
1029 return -1;
1032 if( !this->m_IsCherryPick )
1034 cmd.Format(L"git.exe checkout -f %s --", (LPCTSTR)m_OrigUpstreamHash.ToString());
1035 this->AddLogString(cmd);
1036 if (RunGitCmdRetryOrAbort(cmd))
1037 return -1;
1040 CString log;
1041 if( !this->m_IsCherryPick )
1043 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
1045 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_BranchCtrl.GetString() + L"\"."), L"TortoiseGit", MB_ICONERROR);
1046 return -1;
1048 log.Format(L"%s\r\n", (LPCTSTR)CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTREBASE)));
1050 else
1051 log.Format(L"%s\r\n", (LPCTSTR)CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTCHERRYPICK)));
1053 this->AddLogString(log);
1054 return 0;
1056 int CRebaseDlg::VerifyNoConflict()
1058 int hasConflicts = g_Git.HasWorkingTreeConflicts();
1059 if (hasConflicts < 0)
1061 AddLogString(g_Git.GetGitLastErr(L"Checking for conflicts failed.", CGit::GIT_CMD_CHECKCONFLICTS));
1062 return -1;
1064 if (hasConflicts)
1066 CMessageBox::Show(GetSafeHwnd(), IDS_PROGRS_CONFLICTSOCCURED, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
1067 auto locker(m_FileListCtrl.AcquireReadLock());
1068 auto pos = m_FileListCtrl.GetFirstSelectedItemPosition();
1069 while (pos)
1070 m_FileListCtrl.SetItemState(m_FileListCtrl.GetNextSelectedItem(pos), 0, LVIS_SELECTED);
1071 int nListItems = m_FileListCtrl.GetItemCount();
1072 for (int i = 0; i < nListItems; ++i)
1074 auto entry = m_FileListCtrl.GetListEntry(i);
1075 if (entry->m_Action & CTGitPath::LOGACTIONS_UNMERGED)
1077 m_FileListCtrl.EnsureVisible(i, FALSE);
1078 m_FileListCtrl.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
1079 m_FileListCtrl.SetFocus();
1080 return -1;
1083 return -1;
1085 CleanUpRebaseActiveFolder();
1086 return 0;
1089 int CRebaseDlg::FinishRebase()
1091 if (m_bFinishedRebase)
1092 return 0;
1094 m_bFinishedRebase = true;
1095 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
1097 m_sStatusText.LoadString(IDS_DONE);
1098 m_CtrlStatusText.SetWindowText(m_sStatusText);
1099 m_bStatusWarning = false;
1100 m_CtrlStatusText.Invalidate();
1101 return 0;
1104 RewriteNotes();
1106 CGitHash head;
1107 if (g_Git.GetHash(head, L"HEAD"))
1109 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
1110 return -1;
1112 CString out,cmd;
1114 if (g_Git.IsLocalBranch(m_BranchCtrl.GetString()))
1116 cmd.Format(L"git.exe checkout -f -B %s %s --", (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)head.ToString());
1117 AddLogString(cmd);
1118 if (RunGitCmdRetryOrAbort(cmd))
1119 return -1;
1120 AddLogString(out);
1123 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)head.ToString());
1124 AddLogString(cmd);
1125 if (RunGitCmdRetryOrAbort(cmd))
1126 return -1;
1127 AddLogString(out);
1129 while (m_ctrlTabCtrl.GetTabsNum() > 1)
1130 m_ctrlTabCtrl.RemoveTab(0);
1131 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED)));
1132 m_sStatusText.LoadString(IDS_PROC_REBASEFINISHED);
1133 m_bStatusWarning = false;
1134 m_CtrlStatusText.Invalidate();
1136 m_bRebaseAutoEnd = m_bRebaseAutoStart;
1138 return 0;
1141 void CRebaseDlg::RewriteNotes()
1143 CString rewrites;
1144 for (const auto& entry : m_rewrittenCommitsMap)
1146 if (entry.second.IsEmpty())
1147 continue;
1148 rewrites += entry.first.ToString();
1149 rewrites += L' ';
1150 rewrites += entry.second.ToString();
1151 rewrites += L'\n';
1153 if (rewrites.IsEmpty())
1154 return;
1155 CString tmpfile = GetTempFile();
1156 tmpfile.Replace(L'\\', L'/');
1157 if (!CStringUtils::WriteStringToTextFile(tmpfile, rewrites))
1158 return;
1159 SCOPE_EXIT{ ::DeleteFile(tmpfile); };
1160 CString pipefile = GetTempFile();
1161 pipefile.Replace(L'\\', L'/');
1162 CString pipecmd;
1163 pipecmd.Format(L"git notes copy --for-rewrite=rebase < %s", (LPCTSTR)tmpfile);
1164 if (!CStringUtils::WriteStringToTextFile(pipefile, pipecmd))
1165 return;
1166 SCOPE_EXIT{ ::DeleteFile(pipefile); };
1167 CString out;
1168 g_Git.Run(L"bash.exe " + pipefile, &out, CP_UTF8);
1171 void CRebaseDlg::OnBnClickedContinue()
1173 if( m_RebaseStage == REBASE_DONE)
1175 OnOK();
1176 CleanUpRebaseActiveFolder();
1177 CheckRestoreStash();
1178 SaveSplitterPos();
1179 return;
1182 if (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE)
1184 if (CAppUtils::IsTGitRebaseActive())
1185 return;
1186 if (CheckRebaseCondition())
1187 return;
1190 if( this->m_IsFastForward )
1192 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1193 CString cmd,out;
1194 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
1196 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_BranchCtrl.GetString() + L"\"."), L"TortoiseGit", MB_ICONERROR);
1197 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1198 return;
1200 if (g_Git.GetHash(m_OrigUpstreamHash, m_UpstreamCtrl.GetString()))
1202 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_UpstreamCtrl.GetString() + L"\"."), L"TortoiseGit", MB_ICONERROR);
1203 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1204 return;
1207 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
1209 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1210 AddLogString(L"No fast forward possible.\r\nMaybe repository changed");
1211 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1212 return;
1215 if (g_Git.IsLocalBranch(m_BranchCtrl.GetString()))
1217 cmd.Format(L"git.exe checkout --no-track -f -B %s %s --", (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)m_UpstreamCtrl.GetString());
1218 AddLogString(cmd);
1219 if (RunGitCmdRetryOrAbort(cmd))
1221 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1222 return;
1224 AddLogString(out);
1225 out.Empty();
1227 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1228 CString log;
1229 log.Format(IDS_PROC_REBASE_FFTO, (LPCTSTR)m_UpstreamCtrl.GetString());
1230 this->AddLogString(log);
1232 AddLogString(cmd);
1233 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1234 if (RunGitCmdRetryOrAbort(cmd))
1236 GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1237 return;
1239 AddLogString(out);
1240 AddLogString(CString(MAKEINTRESOURCE(IDS_DONE)));
1241 m_RebaseStage = REBASE_DONE;
1242 UpdateCurrentStatus();
1244 if (m_bRebaseAutoStart)
1245 this->PostMessage(WM_COMMAND, MAKELONG(IDC_REBASE_CONTINUE, BN_CLICKED), (LPARAM)GetDlgItem(IDC_REBASE_CONTINUE)->GetSafeHwnd());
1247 return;
1250 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
1252 m_RebaseStage = REBASE_START;
1253 m_FileListCtrl.Clear();
1254 m_FileListCtrl.SetHasCheckboxes(false);
1255 m_FileListCtrl.m_CurrentVersion.Empty();
1256 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, CString(MAKEINTRESOURCE(IDS_PROC_CONFLICTFILES)));
1257 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase, CString(MAKEINTRESOURCE(IDS_LOG)), 2);
1260 if( m_RebaseStage == REBASE_FINISH )
1262 if(FinishRebase())
1263 return ;
1265 SaveSplitterPos();
1266 OnOK();
1269 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
1271 if(VerifyNoConflict())
1272 return;
1273 GitRevLoglist* curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1274 if(this->CheckNextCommitIsSquash())
1275 {//next commit is not squash;
1276 m_RebaseStage = REBASE_SQUASH_EDIT;
1277 this->OnRebaseUpdateUI(0,0);
1278 this->UpdateCurrentStatus();
1279 return ;
1281 m_RebaseStage=REBASE_CONTINUE;
1282 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1283 m_forRewrite.push_back(curRev->m_CommitHash);
1284 this->UpdateCurrentStatus();
1287 if( m_RebaseStage == REBASE_CONFLICT )
1289 if(VerifyNoConflict())
1290 return;
1292 GitRevLoglist* curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1293 // ***************************************************
1294 // ATTENTION: Similar code in CommitDlg.cpp!!!
1295 // ***************************************************
1296 CMassiveGitTask mgtReAddAfterCommit(L"add --ignore-errors -f");
1297 CMassiveGitTask mgtReDelAfterCommit(L"rm --cached --ignore-unmatch");
1298 CMassiveGitTask mgtAdd(L"add -f");
1299 CMassiveGitTask mgtUpdateIndexForceRemove(L"update-index --force-remove");
1300 CMassiveGitTask mgtUpdateIndex(L"update-index");
1301 CMassiveGitTask mgtRm(L"rm --ignore-unmatch");
1302 CMassiveGitTask mgtRmFCache(L"rm -f --cache");
1303 CMassiveGitTask mgtReset(L"reset", TRUE, true);
1304 auto locker(m_FileListCtrl.AcquireReadLock());
1305 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
1307 auto entry = m_FileListCtrl.GetListEntry(i);
1308 if (entry->m_Checked)
1310 if ((entry->m_Action & CTGitPath::LOGACTIONS_UNVER) || entry->IsDirectory())
1311 mgtAdd.AddFile(entry->GetGitPathString());
1312 else if (entry->m_Action & CTGitPath::LOGACTIONS_DELETED)
1313 mgtUpdateIndexForceRemove.AddFile(entry->GetGitPathString());
1314 else
1315 mgtUpdateIndex.AddFile(entry->GetGitPathString());
1317 if ((entry->m_Action & CTGitPath::LOGACTIONS_REPLACED) && !entry->GetGitOldPathString().IsEmpty())
1318 mgtRm.AddFile(entry->GetGitOldPathString());
1320 else
1322 if (entry->m_Action & CTGitPath::LOGACTIONS_ADDED || entry->m_Action & CTGitPath::LOGACTIONS_REPLACED)
1324 mgtRmFCache.AddFile(entry->GetGitPathString());
1325 mgtReAddAfterCommit.AddFile(*entry);
1327 if (entry->m_Action & CTGitPath::LOGACTIONS_REPLACED && !entry->GetGitOldPathString().IsEmpty())
1329 mgtReset.AddFile(entry->GetGitOldPathString());
1330 mgtReDelAfterCommit.AddFile(entry->GetGitOldPathString());
1333 else if(!(entry->m_Action & CTGitPath::LOGACTIONS_UNVER))
1335 mgtReset.AddFile(entry->GetGitPathString());
1336 if (entry->m_Action & CTGitPath::LOGACTIONS_DELETED && !(entry->m_Action & CTGitPath::LOGACTIONS_MISSING))
1337 mgtReDelAfterCommit.AddFile(entry->GetGitPathString());
1342 BOOL cancel = FALSE;
1343 bool successful = true;
1344 successful = successful && mgtAdd.Execute(cancel);
1345 successful = successful && mgtUpdateIndexForceRemove.Execute(cancel);
1346 successful = successful && mgtUpdateIndex.Execute(cancel);
1347 successful = successful && mgtRm.Execute(cancel);
1348 successful = successful && mgtRmFCache.Execute(cancel);
1349 successful = successful && mgtReset.Execute(cancel);
1351 if (!successful)
1353 AddLogString(L"An error occurred while updating the index.");
1354 return;
1357 CString out;
1358 CString cmd;
1359 cmd.Format(L"git.exe commit --allow-empty-message -C %s", (LPCTSTR)curRev->m_CommitHash.ToString());
1361 AddLogString(cmd);
1363 if(g_Git.Run(cmd,&out,CP_UTF8))
1365 AddLogString(out);
1366 if(!g_Git.CheckCleanWorkTree())
1368 CMessageBox::Show(GetSafeHwnd(), out, L"TortoiseGit", MB_OK | MB_ICONERROR);
1369 return;
1373 AddLogString(out);
1375 // update commit message if needed
1376 CString str = m_LogMessageCtrl.GetText().Trim();
1377 if (str != (curRev->GetSubject() + L'\n' + curRev->GetBody()).Trim())
1379 if (str.IsEmpty())
1381 CMessageBox::Show(GetSafeHwnd(), IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1382 return;
1384 CString tempfile = ::GetTempFile();
1385 SCOPE_EXIT{ ::DeleteFile(tempfile); };
1386 if (CAppUtils::SaveCommitUnicodeFile(tempfile, str))
1388 CMessageBox::Show(GetSafeHwnd(), L"Could not save commit message", L"TortoiseGit", MB_OK | MB_ICONERROR);
1389 return;
1392 out.Empty();
1393 cmd.Format(L"git.exe commit --amend -F \"%s\"", (LPCTSTR)tempfile);
1394 AddLogString(cmd);
1396 if (g_Git.Run(cmd, &out, CP_UTF8))
1398 AddLogString(out);
1399 if (!g_Git.CheckCleanWorkTree())
1401 CMessageBox::Show(GetSafeHwnd(), out, L"TortoiseGit", MB_OK | MB_ICONERROR);
1402 return;
1406 AddLogString(out);
1409 if (((DWORD)CRegStdDWORD(L"Software\\TortoiseGit\\ReaddUnselectedAddedFilesAfterCommit", TRUE)) == TRUE)
1411 BOOL cancel2 = FALSE;
1412 mgtReAddAfterCommit.Execute(cancel2);
1413 mgtReDelAfterCommit.Execute(cancel2);
1416 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1417 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_EDIT)
1419 m_RebaseStage=REBASE_EDIT;
1420 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1421 this->UpdateCurrentStatus();
1422 return;
1424 else
1426 m_RebaseStage=REBASE_CONTINUE;
1427 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1428 this->UpdateCurrentStatus();
1430 if (CheckNextCommitIsSquash() == 0) // remember commit msg after edit if next commit if squash
1431 ResetParentForSquash(str);
1432 else
1434 m_SquashMessage.Empty();
1435 CGitHash head;
1436 if (g_Git.GetHash(head, L"HEAD"))
1438 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
1439 return;
1441 m_rewrittenCommitsMap[curRev->m_CommitHash] = head;
1446 if ((m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_CONTINUE || m_bSplitCommit || m_RebaseStage == REBASE_SQUASH_EDIT) && CheckNextCommitIsSquash() && (m_bSplitCommit || !g_Git.CheckCleanWorkTree(true)))
1448 if (!m_bSplitCommit && CMessageBox::Show(GetSafeHwnd(), IDS_PROC_REBASE_CONTINUE_NOTCLEAN, IDS_APPNAME, 1, IDI_ERROR, IDS_MSGBOX_OK, IDS_ABORTBUTTON) == 2)
1449 return;
1450 BOOL isFirst = TRUE;
1453 CCommitDlg dlg;
1454 if (isFirst)
1455 dlg.m_sLogMessage = m_LogMessageCtrl.GetText();
1456 dlg.m_bWholeProject = true;
1457 dlg.m_bSelectFilesForCommit = true;
1458 dlg.m_bCommitAmend = isFirst && (m_RebaseStage != REBASE_SQUASH_EDIT); // do not amend on squash_edit stage, we need a normal commit there
1459 if (isFirst && m_RebaseStage == REBASE_SQUASH_EDIT)
1461 if (m_iSquashdate != 2)
1462 dlg.SetTime(m_SquashFirstMetaData.time);
1463 dlg.SetAuthor(m_SquashFirstMetaData.GetAuthor());
1465 CTGitPathList gpl;
1466 gpl.AddPath(CTGitPath());
1467 dlg.m_pathList = gpl;
1468 dlg.m_bAmendDiffToLastCommit = !m_bSplitCommit;
1469 dlg.m_bNoPostActions = true;
1470 if (dlg.m_bCommitAmend)
1471 dlg.m_AmendStr = dlg.m_sLogMessage;
1472 dlg.m_bWarnDetachedHead = false;
1474 if (dlg.DoModal() != IDOK)
1475 return;
1477 isFirst = !m_bSplitCommit; // only select amend on second+ runs if not in split commit mode
1479 m_SquashMessage.Empty();
1480 } while (!g_Git.CheckCleanWorkTree() || (m_bSplitCommit && CMessageBox::Show(GetSafeHwnd(), IDS_REBASE_ADDANOTHERCOMMIT, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) == IDYES));
1482 m_bSplitCommit = FALSE;
1483 UpdateData(FALSE);
1485 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1486 m_RebaseStage = REBASE_CONTINUE;
1487 GitRevLoglist* curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1488 CGitHash head;
1489 if (g_Git.GetHash(head, L"HEAD"))
1491 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
1492 return;
1494 m_rewrittenCommitsMap[curRev->m_CommitHash] = head;
1495 for (const auto& hash : m_forRewrite)
1496 m_rewrittenCommitsMap[hash] = head;
1497 m_forRewrite.clear();
1498 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1499 this->UpdateCurrentStatus();
1502 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
1504 CString str;
1505 GitRevLoglist* curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1507 str=this->m_LogMessageCtrl.GetText();
1508 if(str.Trim().IsEmpty())
1510 CMessageBox::Show(GetSafeHwnd(), IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1511 return;
1514 CString tempfile=::GetTempFile();
1515 SCOPE_EXIT{ ::DeleteFile(tempfile); };
1516 if (CAppUtils::SaveCommitUnicodeFile(tempfile, str))
1518 CMessageBox::Show(GetSafeHwnd(), L"Could not save commit message", L"TortoiseGit", MB_OK | MB_ICONERROR);
1519 return;
1522 CString out,cmd;
1524 if (m_RebaseStage == REBASE_SQUASH_EDIT)
1525 cmd.Format(L"git.exe commit %s-F \"%s\"", (LPCTSTR)m_SquashFirstMetaData.GetAsParam(m_iSquashdate == 2), (LPCTSTR)tempfile);
1526 else
1528 CString options;
1529 int isEmpty = IsCommitEmpty(curRev->m_CommitHash);
1530 if (isEmpty == 1)
1531 options = L"--allow-empty ";
1532 else if (isEmpty < 0)
1533 return;
1534 cmd.Format(L"git.exe commit --amend %s-F \"%s\"", (LPCTSTR)options, (LPCTSTR)tempfile);
1537 if(g_Git.Run(cmd,&out,CP_UTF8))
1539 if(!g_Git.CheckCleanWorkTree())
1541 CMessageBox::Show(GetSafeHwnd(), out, L"TortoiseGit", MB_OK | MB_ICONERROR);
1542 return;
1546 AddLogString(out);
1547 if (CheckNextCommitIsSquash() == 0 && m_RebaseStage != REBASE_SQUASH_EDIT) // remember commit msg after edit if next commit if squash; but don't do this if ...->squash(reset here)->pick->squash
1549 ResetParentForSquash(str);
1551 else
1552 m_SquashMessage.Empty();
1553 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1554 m_RebaseStage=REBASE_CONTINUE;
1555 CGitHash head;
1556 if (g_Git.GetHash(head, L"HEAD"))
1558 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
1559 return;
1561 m_rewrittenCommitsMap[curRev->m_CommitHash] = head; // we had a reset to parent, so this is not the correct hash
1562 for (const auto& hash : m_forRewrite)
1563 m_rewrittenCommitsMap[hash] = head;
1564 m_forRewrite.clear();
1565 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1566 this->UpdateCurrentStatus();
1570 InterlockedExchange(&m_bThreadRunning, TRUE);
1571 SetControlEnable();
1573 if (!AfxBeginThread(RebaseThreadEntry, this))
1575 InterlockedExchange(&m_bThreadRunning, FALSE);
1576 CMessageBox::Show(GetSafeHwnd(), L"Create Rebase Thread Fail", L"TortoiseGit", MB_OK | MB_ICONERROR);
1577 SetControlEnable();
1581 void CRebaseDlg::ResetParentForSquash(const CString& commitMessage)
1583 m_SquashMessage = commitMessage;
1584 // reset parent so that we can do "git cherry-pick --no-commit" w/o introducing an unwanted commit
1585 CString cmd = L"git.exe reset --soft HEAD~1";
1586 m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1587 if (RunGitCmdRetryOrAbort(cmd))
1588 return;
1591 int CRebaseDlg::CheckNextCommitIsSquash()
1593 int index;
1594 if(m_CommitList.m_IsOldFirst)
1595 index=m_CurrentRebaseIndex+1;
1596 else
1597 index=m_CurrentRebaseIndex-1;
1599 GitRevLoglist* curRev;
1602 if(index<0)
1603 return -1;
1604 if(index>= m_CommitList.GetItemCount())
1605 return -1;
1607 curRev = m_CommitList.m_arShownList.SafeGetAt(index);
1609 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1610 return 0;
1611 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SKIP)
1613 if(m_CommitList.m_IsOldFirst)
1614 ++index;
1615 else
1616 --index;
1618 else
1619 return -1;
1621 } while(curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SKIP);
1623 return -1;
1626 int CRebaseDlg::GoNext()
1628 if(m_CommitList.m_IsOldFirst)
1629 ++m_CurrentRebaseIndex;
1630 else
1631 --m_CurrentRebaseIndex;
1632 return 0;
1635 void CRebaseDlg::SetContinueButtonText()
1637 CString Text;
1638 switch(this->m_RebaseStage)
1640 case CHOOSE_BRANCH:
1641 case CHOOSE_COMMIT_PICK_MODE:
1642 if(this->m_IsFastForward)
1643 Text.LoadString(IDS_PROC_STARTREBASEFFBUTTON);
1644 else
1645 Text.LoadString(IDS_PROC_STARTREBASEBUTTON);
1646 break;
1648 case REBASE_START:
1649 case REBASE_ERROR:
1650 case REBASE_CONTINUE:
1651 case REBASE_SQUASH_CONFLICT:
1652 Text.LoadString(IDS_CONTINUEBUTTON);
1653 break;
1655 case REBASE_CONFLICT:
1656 Text.LoadString(IDS_COMMITBUTTON);
1657 break;
1658 case REBASE_EDIT:
1659 Text.LoadString(IDS_AMENDBUTTON);
1660 break;
1662 case REBASE_SQUASH_EDIT:
1663 Text.LoadString(IDS_COMMITBUTTON);
1664 break;
1666 case REBASE_ABORT:
1667 case REBASE_FINISH:
1668 Text.LoadString(IDS_FINISHBUTTON);
1669 break;
1671 case REBASE_DONE:
1672 Text.LoadString(IDS_DONE);
1673 break;
1675 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1678 void CRebaseDlg::SetControlEnable()
1680 switch(this->m_RebaseStage)
1682 case CHOOSE_BRANCH:
1683 case CHOOSE_COMMIT_PICK_MODE:
1685 this->GetDlgItem(IDC_SPLITALLOPTIONS)->EnableWindow(TRUE);
1686 this->GetDlgItem(IDC_BUTTON_UP)->EnableWindow(TRUE);
1687 this->GetDlgItem(IDC_BUTTON_DOWN)->EnableWindow(TRUE);
1688 this->GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(!m_bPreserveMerges);
1689 m_CommitList.EnableDragnDrop(true);
1691 if(!m_IsCherryPick)
1693 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1694 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1695 this->GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(TRUE);
1696 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1697 this->GetDlgItem(IDC_REBASE_CHECK_PRESERVEMERGES)->EnableWindow(TRUE);
1699 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1700 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1701 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1702 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP)|
1703 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_LOG);
1704 break;
1706 case REBASE_START:
1707 case REBASE_CONTINUE:
1708 case REBASE_ABORT:
1709 case REBASE_ERROR:
1710 case REBASE_FINISH:
1711 case REBASE_CONFLICT:
1712 case REBASE_EDIT:
1713 case REBASE_SQUASH_CONFLICT:
1714 case REBASE_DONE:
1715 this->GetDlgItem(IDC_SPLITALLOPTIONS)->EnableWindow(FALSE);
1716 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1717 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1718 this->GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
1719 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1720 this->GetDlgItem(IDC_REBASE_CHECK_PRESERVEMERGES)->EnableWindow(FALSE);
1721 this->GetDlgItem(IDC_BUTTON_UP)->EnableWindow(FALSE);
1722 this->GetDlgItem(IDC_BUTTON_DOWN)->EnableWindow(FALSE);
1723 m_CommitList.EnableDragnDrop(false);
1724 this->GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(FALSE);
1726 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1728 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1729 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1730 this->m_PostButton.RemoveAll();
1731 this->m_PostButton.AddEntries(m_PostButtonTexts);
1732 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1734 break;
1737 GetDlgItem(IDC_REBASE_SPLIT_COMMIT)->ShowWindow((m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT) ? SW_SHOW : SW_HIDE);
1739 if(m_bThreadRunning)
1741 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1744 else if (m_RebaseStage != REBASE_ERROR)
1746 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1750 void CRebaseDlg::UpdateProgress()
1752 int index;
1753 CRect rect;
1755 if(m_CommitList.m_IsOldFirst)
1756 index = m_CurrentRebaseIndex+1;
1757 else
1758 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1760 int finishedCommits = index - 1; // introduced an variable which shows the number handled revisions for the progress bars
1761 if (m_RebaseStage == REBASE_FINISH || finishedCommits == -1)
1762 finishedCommits = index;
1764 m_ProgressBar.SetRange32(0, m_CommitList.GetItemCount());
1765 m_ProgressBar.SetPos(finishedCommits);
1766 if (m_pTaskbarList)
1768 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1769 m_pTaskbarList->SetProgressValue(m_hWnd, finishedCommits, m_CommitList.GetItemCount());
1772 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1774 CString text;
1775 text.Format(IDS_PROC_REBASING_PROGRESS, index, m_CommitList.GetItemCount());
1776 m_sStatusText = text;
1777 m_CtrlStatusText.SetWindowText(text);
1778 m_bStatusWarning = false;
1779 m_CtrlStatusText.Invalidate();
1782 GitRevLoglist* prevRev = nullptr, *curRev = nullptr;
1784 if (m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex < (int)m_CommitList.m_arShownList.size())
1785 curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1787 for (int i = 0; i < (int)m_CommitList.m_arShownList.size(); ++i)
1789 prevRev = m_CommitList.m_arShownList.SafeGetAt(i);
1790 if (prevRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_CURRENT)
1792 prevRev->GetRebaseAction() &= ~CGitLogListBase::LOGACTIONS_REBASE_CURRENT;
1793 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1794 m_CommitList.InvalidateRect(rect);
1798 if(curRev)
1800 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_CURRENT;
1801 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1802 m_CommitList.InvalidateRect(rect);
1804 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1807 void CRebaseDlg::UpdateCurrentStatus()
1809 SetContinueButtonText();
1810 SetControlEnable();
1811 UpdateProgress();
1812 if (m_RebaseStage == REBASE_DONE)
1813 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
1816 void CRebaseDlg::AddLogString(CString str)
1818 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1819 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1820 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1821 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1822 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1823 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1826 int CRebaseDlg::GetCurrentCommitID()
1828 if(m_CommitList.m_IsOldFirst)
1829 return this->m_CurrentRebaseIndex+1;
1830 else
1831 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1834 int CRebaseDlg::IsCommitEmpty(const CGitHash& hash)
1836 CString cmd, tree, ptree;
1837 cmd.Format(L"git.exe rev-parse -q --verify %s^{tree}", (LPCTSTR)hash.ToString());
1838 if (g_Git.Run(cmd, &tree, CP_UTF8))
1840 AddLogString(cmd);
1841 AddLogString(tree);
1842 return -1;
1844 cmd.Format(L"git.exe rev-parse -q --verify %s^^{tree}", (LPCTSTR)hash.ToString());
1845 if (g_Git.Run(cmd, &ptree, CP_UTF8))
1846 ptree = L"4b825dc642cb6eb9a060e54bf8d69288fbee4904"; // empty tree
1847 return tree == ptree;
1850 int CRebaseDlg::DoRebase()
1852 CString cmd,out;
1853 if(m_CurrentRebaseIndex <0)
1854 return 0;
1855 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1856 return 0;
1858 GitRevLoglist* pRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
1859 int mode = pRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_MODE_MASK;
1860 CString nocommit;
1862 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SKIP)
1864 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1865 return 0;
1868 bool nextCommitIsSquash = (CheckNextCommitIsSquash() == 0);
1869 if (nextCommitIsSquash || mode != CGitLogListBase::LOGACTIONS_REBASE_PICK)
1870 { // next commit is squash or not pick
1871 if (!this->m_SquashMessage.IsEmpty())
1872 this->m_SquashMessage += L"\n\n";
1873 this->m_SquashMessage += pRev->GetSubject();
1874 this->m_SquashMessage += L'\n';
1875 this->m_SquashMessage += pRev->GetBody().TrimRight();
1876 if (m_bAddCherryPickedFrom)
1878 if (!pRev->GetBody().IsEmpty())
1879 m_SquashMessage += L'\n';
1880 m_SquashMessage += L"(cherry picked from commit ";
1881 m_SquashMessage += pRev->m_CommitHash.ToString();
1882 m_SquashMessage += L')';
1885 else
1887 this->m_SquashMessage.Empty();
1888 m_SquashFirstMetaData.Empty();
1891 if (nextCommitIsSquash && mode != CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1892 m_SquashFirstMetaData = SquashFirstMetaData(pRev);
1894 if ((nextCommitIsSquash && mode != CGitLogListBase::LOGACTIONS_REBASE_EDIT) || mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1895 { // next or this commit is squash (don't do this on edit->squash sequence)
1896 nocommit = L" --no-commit ";
1897 if (m_iSquashdate == 1)
1898 m_SquashFirstMetaData.UpdateDate(pRev);
1901 CString log;
1902 log.Format(L"%s %d: %s", (LPCTSTR)CGitLogListBase::GetRebaseActionName(mode), GetCurrentCommitID(), (LPCTSTR)pRev->m_CommitHash.ToString());
1903 AddLogString(log);
1904 AddLogString(pRev->GetSubject());
1905 if (pRev->GetSubject().IsEmpty())
1907 CMessageBox::Show(m_hWnd, IDS_PROC_REBASE_EMPTYCOMMITMSG, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
1908 mode = CGitLogListBase::LOGACTIONS_REBASE_EDIT;
1911 CString cherryPickedFrom;
1912 if (m_bAddCherryPickedFrom)
1913 cherryPickedFrom = L"-x ";
1914 else if (!m_IsCherryPick && nocommit.IsEmpty())
1915 cherryPickedFrom = L"--ff "; // for issue #1833: "If the current HEAD is the same as the parent of the cherry-picked commit, then a fast forward to this commit will be performed."
1917 int isEmpty = IsCommitEmpty(pRev->m_CommitHash);
1918 if (isEmpty == 1)
1919 cherryPickedFrom += L"--allow-empty ";
1920 else if (isEmpty < 0)
1921 return -1;
1923 if (m_IsCherryPick && pRev->m_ParentHash.size() > 1)
1925 CString msg;
1926 msg.Format(IDS_CHERRYPICK_MERGECOMMIT, (LPCTSTR)pRev->m_CommitHash.ToString(), (LPCTSTR)pRev->GetSubject());
1927 CString parent1;
1928 parent1.Format(IDS_PARENT, 1);
1929 CString parent2;
1930 parent2.Format(IDS_PARENT, 2);
1931 CString cancel;
1932 cancel.LoadString(IDS_MSGBOX_CANCEL);
1933 auto ret = CMessageBox::Show(m_hWnd, msg, L"TortoiseGit", 3, IDI_QUESTION, parent1, parent2, cancel);
1934 if (ret == 3)
1935 return - 1;
1937 cherryPickedFrom.AppendFormat(L"-m %d ", ret);
1940 while (true)
1942 cmd.Format(L"git.exe cherry-pick %s%s %s", (LPCTSTR)cherryPickedFrom, (LPCTSTR)nocommit, (LPCTSTR)pRev->m_CommitHash.ToString());
1943 if (m_bPreserveMerges)
1945 bool parentRewritten = false;
1946 CGitHash currentHeadHash;
1947 if (g_Git.GetHash(currentHeadHash, L"HEAD"))
1949 m_RebaseStage = REBASE_ERROR;
1950 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
1951 return -1;
1953 if (!m_currentCommits.empty())
1955 for (const auto& commit : m_currentCommits)
1956 m_rewrittenCommitsMap[commit] = currentHeadHash;
1957 m_currentCommits.clear();
1959 m_currentCommits.push_back(pRev->m_CommitHash);
1960 GIT_REV_LIST possibleParents = pRev->m_ParentHash;
1961 GIT_REV_LIST newParents;
1962 for (auto it = possibleParents.cbegin(); it != possibleParents.cend(); it = possibleParents.begin())
1964 CGitHash parent = *it;
1965 possibleParents.erase(it);
1967 const auto rewrittenParent = m_rewrittenCommitsMap.find(parent);
1968 if (rewrittenParent == m_rewrittenCommitsMap.cend())
1970 auto droppedCommitParents = m_droppedCommitsMap.find(parent);
1971 if (droppedCommitParents != m_droppedCommitsMap.cend())
1973 parentRewritten = true;
1974 for (auto droppedIt = droppedCommitParents->second.crbegin(); droppedIt != droppedCommitParents->second.crend(); ++droppedIt)
1975 possibleParents.insert(possibleParents.begin(), *droppedIt);
1976 continue;
1979 newParents.push_back(parent);
1980 continue;
1983 if (rewrittenParent->second.IsEmpty() && parent == pRev->m_ParentHash[0] && pRev->ParentsCount() > 1)
1985 m_RebaseStage = REBASE_ERROR;
1986 AddLogString(L"");
1987 AddLogString(L"Unrecoverable error: Merge commit parent missing.");
1988 return -1;
1991 CGitHash newParent = rewrittenParent->second;
1992 if (newParent.IsEmpty()) // use current HEAD as fallback
1993 newParent = currentHeadHash;
1995 if (newParent != parent)
1996 parentRewritten = true;
1998 if (std::find(newParents.begin(), newParents.end(), newParent) == newParents.end())
1999 newParents.push_back(newParent);
2001 if (pRev->ParentsCount() > 1)
2003 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
2005 m_RebaseStage = REBASE_ERROR;
2006 AddLogString(L"Cannot squash merge commit on rebase.");
2007 return -1;
2009 if (!parentRewritten && nocommit.IsEmpty())
2010 cmd.Format(L"git.exe reset --hard %s", (LPCTSTR)pRev->m_CommitHash.ToString());
2011 else
2013 CString parentString;
2014 for (const auto& parent : newParents)
2015 parentString += L' ' + parent.ToString();
2016 cmd.Format(L"git.exe checkout %s", (LPCTSTR)newParents[0].ToString());
2017 if (RunGitCmdRetryOrAbort(cmd))
2019 m_RebaseStage = REBASE_ERROR;
2020 return -1;
2022 cmd.Format(L"git.exe merge --no-ff%s %s", (LPCTSTR)nocommit, (LPCTSTR)parentString);
2023 if (nocommit.IsEmpty())
2025 if (g_Git.Run(cmd, &out, CP_UTF8))
2027 AddLogString(cmd);
2028 AddLogString(out);
2029 int hasConflicts = g_Git.HasWorkingTreeConflicts();
2030 if (hasConflicts > 0)
2032 m_RebaseStage = REBASE_CONFLICT;
2033 return -1;
2035 else if (hasConflicts < 0)
2036 AddLogString(g_Git.GetGitLastErr(L"Checking for conflicts failed.", CGit::GIT_CMD_CHECKCONFLICTS));
2037 AddLogString(L"An unrecoverable error occurred.");
2038 m_RebaseStage = REBASE_ERROR;
2039 return -1;
2041 CGitHash newHeadHash;
2042 if (g_Git.GetHash(newHeadHash, L"HEAD"))
2044 m_RebaseStage = REBASE_ERROR;
2045 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
2046 return -1;
2048 // do nothing if already up2date
2049 if (currentHeadHash != newHeadHash)
2050 cmd.Format(L"git.exe commit --amend -C %s", (LPCTSTR)pRev->m_CommitHash.ToString());
2054 else
2056 if (mode != CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
2058 cmd.Format(L"git.exe checkout %s", (LPCTSTR)newParents[0].ToString());
2059 if (RunGitCmdRetryOrAbort(cmd))
2061 m_RebaseStage = REBASE_ERROR;
2062 return -1;
2065 cmd.Format(L"git.exe cherry-pick %s%s %s", (LPCTSTR)cherryPickedFrom, (LPCTSTR)nocommit, (LPCTSTR)pRev->m_CommitHash.ToString());
2069 if(g_Git.Run(cmd,&out,CP_UTF8))
2071 AddLogString(out);
2072 int hasConflicts = g_Git.HasWorkingTreeConflicts();
2073 if (hasConflicts < 0)
2075 AddLogString(g_Git.GetGitLastErr(L"Checking for conflicts failed.", CGit::GIT_CMD_CHECKCONFLICTS));
2076 return -1;
2078 if (!hasConflicts)
2080 if (mode == CGitLogListBase::LOGACTIONS_REBASE_PICK)
2082 if (m_pTaskbarList)
2083 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
2084 int choose = -1;
2085 if (!m_bAutoSkipFailedCommit)
2087 choose = CMessageBox::ShowCheck(GetSafeHwnd(), IDS_CHERRYPICKFAILEDSKIP, IDS_APPNAME, 1, IDI_QUESTION, IDS_SKIPBUTTON, IDS_MSGBOX_RETRY, IDS_MSGBOX_CANCEL, nullptr, IDS_DO_SAME_FOR_REST, &m_bAutoSkipFailedCommit);
2088 if (choose == 2)
2090 m_bAutoSkipFailedCommit = FALSE;
2091 continue; // retry cherry pick
2094 if (m_bAutoSkipFailedCommit || choose == 1)
2096 if (!RunGitCmdRetryOrAbort(L"git.exe reset --hard"))
2098 pRev->GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_SKIP;
2099 m_CommitList.Invalidate();
2100 return 0;
2104 m_RebaseStage = REBASE_ERROR;
2105 AddLogString(L"An unrecoverable error occurred.");
2106 return -1;
2108 if (mode == CGitLogListBase::LOGACTIONS_REBASE_EDIT)
2110 this->m_RebaseStage = REBASE_EDIT ;
2111 return -1; // Edit return -1 to stop rebase.
2113 // Squash Case
2114 if(CheckNextCommitIsSquash())
2115 { // no squash
2116 // let user edit last commmit message
2117 this->m_RebaseStage = REBASE_SQUASH_EDIT;
2118 return -1;
2122 if (m_pTaskbarList)
2123 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
2124 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
2125 m_RebaseStage = REBASE_SQUASH_CONFLICT;
2126 else
2127 m_RebaseStage = REBASE_CONFLICT;
2128 return -1;
2131 else
2133 AddLogString(out);
2134 if (mode == CGitLogListBase::LOGACTIONS_REBASE_PICK)
2136 if (nocommit.IsEmpty())
2138 CGitHash head;
2139 if (g_Git.GetHash(head, L"HEAD"))
2141 MessageBox(g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
2142 m_RebaseStage = REBASE_ERROR;
2143 return -1;
2145 m_rewrittenCommitsMap[pRev->m_CommitHash] = head;
2147 else
2148 m_forRewrite.push_back(pRev->m_CommitHash);
2149 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
2150 return 0;
2152 if (mode == CGitLogListBase::LOGACTIONS_REBASE_EDIT)
2154 this->m_RebaseStage = REBASE_EDIT ;
2155 return -1; // Edit return -1 to stop rebase.
2158 // Squash Case
2159 if(CheckNextCommitIsSquash())
2160 { // no squash
2161 // let user edit last commmit message
2162 this->m_RebaseStage = REBASE_SQUASH_EDIT;
2163 return -1;
2165 else if (mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
2167 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
2168 m_forRewrite.push_back(pRev->m_CommitHash);
2172 return 0;
2176 BOOL CRebaseDlg::IsEnd()
2178 if(m_CommitList.m_IsOldFirst)
2179 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
2180 else
2181 return m_CurrentRebaseIndex<0;
2184 int CRebaseDlg::RebaseThread()
2186 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
2188 int ret=0;
2189 while(1)
2191 if( m_RebaseStage == REBASE_START )
2193 if( this->StartRebase() )
2195 ret = -1;
2196 break;
2198 m_RebaseStage = REBASE_CONTINUE;
2200 else if( m_RebaseStage == REBASE_CONTINUE )
2202 this->GoNext();
2203 SendMessage(MSG_REBASE_UPDATE_UI);
2204 if(IsEnd())
2206 ret = 0;
2207 m_RebaseStage = REBASE_FINISH;
2209 else
2211 ret = DoRebase();
2212 if( ret )
2213 break;
2216 else if( m_RebaseStage == REBASE_FINISH )
2218 SendMessage(MSG_REBASE_UPDATE_UI);
2219 m_RebaseStage = REBASE_DONE;
2220 break;
2222 else
2223 break;
2224 this->PostMessage(MSG_REBASE_UPDATE_UI);
2227 InterlockedExchange(&m_bThreadRunning, FALSE);
2228 this->PostMessage(MSG_REBASE_UPDATE_UI);
2229 return ret;
2232 void CRebaseDlg::ListConflictFile(bool noStoreScrollPosition)
2234 if (!noStoreScrollPosition)
2235 m_FileListCtrl.StoreScrollPos();
2236 this->m_FileListCtrl.Clear();
2237 m_FileListCtrl.SetHasCheckboxes(true);
2238 CTGitPathList list;
2239 CTGitPath path;
2240 list.AddPath(path);
2242 if (!m_IsCherryPick)
2244 CString adminDir;
2245 if (GitAdminDir::GetAdminDirPath(g_Git.m_CurrentDir, adminDir))
2247 CString dir(adminDir + L"tgitrebase.active");
2248 ::CreateDirectory(dir, nullptr);
2249 CStringUtils::WriteStringToTextFile(dir + L"\\head-name", m_BranchCtrl.GetString());
2250 CStringUtils::WriteStringToTextFile(dir + L"\\onto", m_Onto.IsEmpty() ? m_UpstreamCtrl.GetString() : m_Onto);
2254 this->m_FileListCtrl.GetStatus(&list,true);
2255 m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED | CTGitPath::LOGACTIONS_MODIFIED | CTGitPath::LOGACTIONS_ADDED | CTGitPath::LOGACTIONS_DELETED | CTGitPath::LOGACTIONS_REPLACED, CTGitPath::LOGACTIONS_UNMERGED);
2257 m_FileListCtrl.Check(GITSLC_SHOWFILES);
2258 bool hasSubmoduleChange = false;
2259 auto locker(m_FileListCtrl.AcquireReadLock());
2260 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
2262 auto entry = m_FileListCtrl.GetListEntry(i);
2263 if (entry->IsDirectory())
2265 hasSubmoduleChange = true;
2266 break;
2270 if (hasSubmoduleChange)
2272 m_CtrlStatusText.SetWindowText(m_sStatusText + L", " + CString(MAKEINTRESOURCE(IDS_CARE_SUBMODULE_CHANGES)));
2273 m_bStatusWarning = true;
2274 m_CtrlStatusText.Invalidate();
2276 else
2278 m_CtrlStatusText.SetWindowText(m_sStatusText);
2279 m_bStatusWarning = false;
2280 m_CtrlStatusText.Invalidate();
2284 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
2286 if (m_RebaseStage == REBASE_FINISH)
2288 FinishRebase();
2289 return 0;
2291 UpdateCurrentStatus();
2293 if (m_RebaseStage == REBASE_DONE && m_bRebaseAutoEnd)
2295 m_bRebaseAutoEnd = false;
2296 this->PostMessage(WM_COMMAND, MAKELONG(IDC_REBASE_CONTINUE, BN_CLICKED), (LPARAM)GetDlgItem(IDC_REBASE_CONTINUE)->GetSafeHwnd());
2299 if (m_RebaseStage == REBASE_DONE && m_pTaskbarList)
2300 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS); // do not show progress on taskbar any more to show we finished
2301 if(m_CurrentRebaseIndex <0)
2302 return 0;
2303 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
2304 return 0;
2305 GitRev* curRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
2307 switch(m_RebaseStage)
2309 case REBASE_CONFLICT:
2310 case REBASE_SQUASH_CONFLICT:
2312 ListConflictFile(true);
2313 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
2314 if (m_pTaskbarList)
2315 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
2316 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2317 CString logMessage;
2318 if (m_IsCherryPick)
2320 CString dotGitPath;
2321 GitAdminDir::GetWorktreeAdminDirPath(g_Git.m_CurrentDir, dotGitPath);
2322 // vanilla git also re-uses MERGE_MSG on conflict (listing all conflicted files)
2323 // and it's also needed for cherry-pick in order to get cherry-picked-from included on conflicts
2324 CGit::LoadTextFile(dotGitPath + L"MERGE_MSG", logMessage);
2326 if (logMessage.IsEmpty())
2327 logMessage = curRev->GetSubject() + L'\n' + curRev->GetBody();
2328 this->m_LogMessageCtrl.SetText(logMessage);
2329 break;
2331 case REBASE_EDIT:
2332 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
2333 if (m_pTaskbarList)
2334 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
2335 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2336 if (m_bAddCherryPickedFrom)
2338 // Since the new commit is done and the HEAD points to it,
2339 // just using the new body modified by git self.
2340 GitRev headRevision;
2341 if (headRevision.GetCommit(L"HEAD"))
2342 MessageBox(headRevision.GetLastErr(), L"TortoiseGit", MB_ICONERROR);
2344 m_LogMessageCtrl.SetText(headRevision.GetSubject() + L'\n' + headRevision.GetBody());
2346 else
2347 m_LogMessageCtrl.SetText(curRev->GetSubject() + L'\n' + curRev->GetBody());
2348 break;
2349 case REBASE_SQUASH_EDIT:
2350 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
2351 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2352 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
2353 if (m_pTaskbarList)
2354 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
2355 break;
2356 default:
2357 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
2359 return 0;
2362 void CRebaseDlg::OnCancel()
2364 OnBnClickedAbort();
2367 void CRebaseDlg::OnBnClickedAbort()
2369 if (m_pTaskbarList)
2370 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
2372 m_tooltips.Pop();
2374 SaveSplitterPos();
2376 if(m_OrigUpstreamHash.IsEmpty())
2378 __super::OnCancel();
2381 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
2383 goto end;
2386 if (CMessageBox::Show(GetSafeHwnd(), IDS_PROC_REBASE_ABORT, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) != IDYES)
2387 goto end;
2389 if(this->m_IsFastForward)
2391 CString cmd;
2392 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)this->m_OrigBranchHash.ToString());
2393 RunGitCmdRetryOrAbort(cmd);
2394 __super::OnCancel();
2395 goto end;
2398 if (m_IsCherryPick) // there are not "branch" at cherry pick mode
2400 CString cmd;
2401 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)m_OrigUpstreamHash.ToString());
2402 RunGitCmdRetryOrAbort(cmd);
2403 __super::OnCancel();
2404 goto end;
2407 if (m_OrigHEADBranch == m_BranchCtrl.GetString())
2409 CString cmd, out;
2410 if (g_Git.IsLocalBranch(m_OrigHEADBranch))
2411 cmd.Format(L"git.exe checkout -f -B %s %s --", (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)m_OrigBranchHash.ToString());
2412 else
2413 cmd.Format(L"git.exe checkout -f %s --", (LPCTSTR)m_OrigBranchHash.ToString());
2414 if (g_Git.Run(cmd, &out, CP_UTF8))
2416 AddLogString(out);
2417 ::MessageBox(m_hWnd, L"Unrecoverable error on cleanup:\n" + out, L"TortoiseGit", MB_ICONERROR);
2418 __super::OnCancel();
2419 goto end;
2422 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)m_OrigBranchHash.ToString());
2423 RunGitCmdRetryOrAbort(cmd);
2425 else
2427 CString cmd, out;
2428 if (m_OrigHEADBranch != g_Git.GetCurrentBranch(true))
2430 if (g_Git.IsLocalBranch(m_OrigHEADBranch))
2431 cmd.Format(L"git.exe checkout -f -B %s %s --", (LPCTSTR)m_OrigHEADBranch, (LPCTSTR)m_OrigHEADHash.ToString());
2432 else
2433 cmd.Format(L"git.exe checkout -f %s --", (LPCTSTR)m_OrigHEADHash.ToString());
2434 if (g_Git.Run(cmd, &out, CP_UTF8))
2436 AddLogString(out);
2437 ::MessageBox(m_hWnd, L"Unrecoverable error on cleanup:\n" + out, L"TortoiseGit", MB_ICONERROR);
2438 // continue to restore moved branch
2442 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)m_OrigHEADHash.ToString());
2443 RunGitCmdRetryOrAbort(cmd);
2445 // restore moved branch
2446 if (g_Git.IsLocalBranch(m_BranchCtrl.GetString()))
2448 cmd.Format(L"git.exe branch -f %s %s --", (LPCTSTR)m_BranchCtrl.GetString(), (LPCTSTR)m_OrigBranchHash.ToString());
2449 if (g_Git.Run(cmd, &out, CP_UTF8))
2451 AddLogString(out);
2452 ::MessageBox(m_hWnd, L"Unrecoverable error on cleanup:\n" + out, L"TortoiseGit", MB_ICONERROR);
2453 __super::OnCancel();
2454 goto end;
2458 __super::OnCancel();
2459 end:
2460 CleanUpRebaseActiveFolder();
2461 CheckRestoreStash();
2464 void CRebaseDlg::OnBnClickedButtonReverse()
2466 CString temp = m_BranchCtrl.GetString();
2467 m_BranchCtrl.AddString(m_UpstreamCtrl.GetString());
2468 m_UpstreamCtrl.AddString(temp);
2469 OnCbnSelchangeUpstream();
2472 void CRebaseDlg::OnBnClickedButtonBrowse()
2474 if (CBrowseRefsDlg::PickRefForCombo(m_UpstreamCtrl))
2475 OnCbnSelchangeUpstream();
2478 void CRebaseDlg::OnBnClickedRebaseCheckForce()
2480 this->UpdateData();
2481 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(!m_bPreserveMerges);
2482 this->FetchLogList();
2485 void CRebaseDlg::OnBnClickedRebasePostButton()
2487 this->m_Upstream=this->m_UpstreamCtrl.GetString();
2488 this->m_Branch=this->m_BranchCtrl.GetString();
2490 this->EndDialog((int)(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry()));
2493 LRESULT CRebaseDlg::OnGitStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
2495 Refresh();
2496 return 0;
2499 void CRebaseDlg::Refresh()
2501 if (m_RebaseStage == REBASE_CONFLICT || m_RebaseStage == REBASE_SQUASH_CONFLICT)
2503 ListConflictFile(false);
2504 return;
2507 if(this->m_IsCherryPick)
2508 return ;
2510 if(this->m_RebaseStage == CHOOSE_BRANCH )
2512 this->UpdateData();
2513 this->FetchLogList();
2517 void CRebaseDlg::OnBnClickedButtonUp()
2519 POSITION pos;
2520 pos = m_CommitList.GetFirstSelectedItemPosition();
2522 bool moveToTop = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
2523 // do nothing if the first selected item is the first item in the list
2524 if (!moveToTop && m_CommitList.GetNextSelectedItem(pos) == 0)
2525 return;
2527 pos = m_CommitList.GetFirstSelectedItemPosition();
2529 int count = 0;
2530 bool changed = false;
2531 while(pos)
2533 int index=m_CommitList.GetNextSelectedItem(pos);
2534 count = moveToTop ? count : (index - 1);
2535 while (index > count)
2537 CGitHash old = m_CommitList.m_logEntries[index - 1];
2538 m_CommitList.m_logEntries[index - 1] = m_CommitList.m_logEntries[index];
2539 m_CommitList.m_logEntries[index] = old;
2540 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2541 m_CommitList.SetItemState(index - 1, LVIS_SELECTED, LVIS_SELECTED);
2542 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
2543 changed = true;
2544 index--;
2546 count++;
2548 if (changed)
2550 pos = m_CommitList.GetFirstSelectedItemPosition();
2551 m_CommitList.EnsureVisible(m_CommitList.GetNextSelectedItem(pos), false);
2552 m_CommitList.Invalidate();
2553 m_CommitList.SetFocus();
2557 void CRebaseDlg::OnBnClickedButtonDown()
2559 if (m_CommitList.GetSelectedCount() == 0)
2560 return;
2562 bool moveToBottom = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
2563 POSITION pos;
2564 pos = m_CommitList.GetFirstSelectedItemPosition();
2565 bool changed = false;
2566 // use an array to store all selected item indexes; the user won't select too much items
2567 auto indexes = std::make_unique<int[]>(m_CommitList.GetSelectedCount());
2568 int i = 0;
2569 while(pos)
2570 indexes.get()[i++] = m_CommitList.GetNextSelectedItem(pos);
2571 // don't move any item if the last selected item is the last item in the m_CommitList
2572 // (that would change the order of the selected items)
2573 if (!moveToBottom && indexes.get()[m_CommitList.GetSelectedCount() - 1] >= m_CommitList.GetItemCount() - 1)
2574 return;
2575 int count = m_CommitList.GetItemCount() - 1;
2576 // iterate over the indexes backwards in order to correctly move multiselected items
2577 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
2579 int index = indexes.get()[i];
2580 count = moveToBottom ? count : (index + 1);
2581 while (index < count)
2583 CGitHash old = m_CommitList.m_logEntries[index + 1];
2584 m_CommitList.m_logEntries[index + 1] = m_CommitList.m_logEntries[index];
2585 m_CommitList.m_logEntries[index] = old;
2586 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2587 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
2588 m_CommitList.SetItemState(index + 1, LVIS_SELECTED, LVIS_SELECTED);
2589 changed = true;
2590 index++;
2592 count--;
2594 m_CommitList.EnsureVisible(indexes.get()[m_CommitList.GetSelectedCount() - 1] + 1, false);
2595 if (changed)
2597 m_CommitList.Invalidate();
2598 m_CommitList.SetFocus();
2602 LRESULT CRebaseDlg::OnCommitsReordered(WPARAM wParam, LPARAM /*lParam*/)
2604 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
2605 int first = m_CommitList.GetNextSelectedItem(pos);
2606 int last = first;
2607 while (pos)
2608 last = m_CommitList.GetNextSelectedItem(pos);
2609 ++last;
2611 for (int i = first; i < last; ++i)
2612 m_CommitList.SetItemState(i, 0, LVIS_SELECTED);
2614 int dest = (int)wParam;
2615 if (dest > first)
2617 std::rotate(m_CommitList.m_logEntries.begin() + first, m_CommitList.m_logEntries.begin() + last, m_CommitList.m_logEntries.begin() + dest);
2618 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2619 for (int i = first + dest - last; i < dest; ++i)
2620 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
2622 else
2624 std::rotate(m_CommitList.m_logEntries.begin() + dest, m_CommitList.m_logEntries.begin() + first, m_CommitList.m_logEntries.begin() + last);
2625 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2626 for (int i = dest; i < dest + (last - first); ++i)
2627 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
2630 m_CommitList.Invalidate();
2632 return 0;
2635 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM wParam, LPARAM lParam)
2637 m_pTaskbarList.Release();
2638 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
2639 return __super::OnTaskbarButtonCreated(wParam, lParam);
2642 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
2644 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
2645 *pResult = 0;
2646 if(m_CommitList.m_bNoDispUpdates)
2647 return;
2648 if (pNMLV->iItem >= 0)
2650 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
2651 if (pNMLV->iSubItem != 0)
2652 return;
2653 if (pNMLV->iItem == (int)m_CommitList.m_arShownList.size())
2655 // remove the selected state
2656 if (pNMLV->uChanged & LVIF_STATE)
2658 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
2659 FillLogMessageCtrl();
2661 return;
2663 if (pNMLV->uChanged & LVIF_STATE)
2664 FillLogMessageCtrl();
2666 else
2667 FillLogMessageCtrl();
2670 void CRebaseDlg::FillLogMessageCtrl()
2672 int selCount = m_CommitList.GetSelectedCount();
2673 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
2675 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
2676 int selIndex = m_CommitList.GetNextSelectedItem(pos);
2677 GitRevLoglist* pLogEntry = m_CommitList.m_arShownList.SafeGetAt(selIndex);
2678 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
2679 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
2680 m_FileListCtrl.Show(GITSLC_SHOWVERSIONED);
2681 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2682 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + L'\n' + pLogEntry->GetBody());
2683 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
2686 void CRebaseDlg::OnBnClickedCheckCherryPickedFrom()
2688 UpdateData();
2691 LRESULT CRebaseDlg::OnRebaseActionMessage(WPARAM, LPARAM)
2693 if (m_RebaseStage == REBASE_ERROR || m_RebaseStage == REBASE_CONFLICT)
2695 GitRevLoglist* pRev = m_CommitList.m_arShownList.SafeGetAt(m_CurrentRebaseIndex);
2696 int mode = pRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_MODE_MASK;
2697 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SKIP)
2699 if (!RunGitCmdRetryOrAbort(L"git.exe reset --hard"))
2701 m_FileListCtrl.Clear();
2702 m_RebaseStage = REBASE_CONTINUE;
2703 UpdateCurrentStatus();
2707 return 0;
2711 void CRebaseDlg::OnBnClickedSplitAllOptions()
2713 switch (m_SplitAllOptions.GetCurrentEntry())
2715 case 0:
2716 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_PICK);
2717 break;
2718 case 1:
2719 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SQUASH);
2720 break;
2721 case 2:
2722 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_EDIT);
2723 break;
2724 case 3:
2725 m_CommitList.SetUnselectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SKIP);
2726 break;
2727 case 4:
2728 m_CommitList.SetUnselectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SQUASH);
2729 break;
2730 case 5:
2731 m_CommitList.SetUnselectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_EDIT);
2732 break;
2733 default:
2734 ATLASSERT(false);
2738 void CRebaseDlg::OnBnClickedRebaseSplitCommit()
2740 UpdateData();
2743 static bool GetCompareHash(const CString& ref, const CGitHash& hash)
2745 CGitHash refHash;
2746 if (g_Git.GetHash(refHash, ref))
2747 MessageBox(nullptr, g_Git.GetGitLastErr(L"Could not get hash of \"" + ref + L"\"."), L"TortoiseGit", MB_ICONERROR);
2748 return refHash.IsEmpty() || (hash == refHash);
2751 void CRebaseDlg::OnBnClickedButtonOnto()
2753 m_Onto = CBrowseRefsDlg::PickRef(false, m_Onto);
2754 if (!m_Onto.IsEmpty())
2756 // make sure that the user did not select upstream, selected branch or HEAD
2757 CGitHash hash;
2758 if (g_Git.GetHash(hash, m_Onto))
2760 MessageBox(g_Git.GetGitLastErr(L"Could not get hash of \"" + m_BranchCtrl.GetString() + L"\"."), L"TortoiseGit", MB_ICONERROR);
2761 m_Onto.Empty();
2762 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
2763 return;
2765 if (GetCompareHash(L"HEAD", hash) || GetCompareHash(m_UpstreamCtrl.GetString(), hash) || GetCompareHash(m_BranchCtrl.GetString(), hash))
2766 m_Onto.Empty();
2768 if (m_Onto.IsEmpty())
2769 m_tooltips.DelTool(IDC_BUTTON_ONTO);
2770 else
2771 m_tooltips.AddTool(IDC_BUTTON_ONTO, m_Onto);
2772 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
2773 FetchLogList();
2776 void CRebaseDlg::OnHelp()
2778 HtmlHelp(0x20000 + (m_IsCherryPick ? IDD_REBASECHERRYPICK : IDD_REBASE));
2781 int CRebaseDlg::RunGitCmdRetryOrAbort(const CString& cmd)
2783 while (true)
2785 CString out;
2786 if (g_Git.Run(cmd, &out, CP_UTF8))
2788 AddLogString(cmd);
2789 AddLogString(CString(MAKEINTRESOURCE(IDS_FAIL)));
2790 AddLogString(out);
2791 CString msg;
2792 msg.Format(L"\"%s\" failed.\n%s", (LPCTSTR)cmd, (LPCTSTR)out);
2793 if (CMessageBox::Show(GetSafeHwnd(), msg, L"TortoiseGit", 1, IDI_ERROR, CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)), CString(MAKEINTRESOURCE(IDS_MSGBOX_ABORT))) != 1)
2794 return -1;
2796 else
2797 return 0;
2801 LRESULT CRebaseDlg::OnThemeChanged()
2803 CMFCVisualManager::GetInstance()->DestroyInstance();
2804 return 0;
2807 void CRebaseDlg::OnSysColorChange()
2809 __super::OnSysColorChange();
2810 m_LogMessageCtrl.SetColors(true);
2811 m_LogMessageCtrl.SetFont(CAppUtils::GetLogFontName(), CAppUtils::GetLogFontSize());
2812 m_wndOutputRebase.SetColors(true);
2813 m_wndOutputRebase.SetFont(CAppUtils::GetLogFontName(), CAppUtils::GetLogFontSize());
2816 void CRebaseDlg::OnBnClickedButtonAdd()
2818 CLogDlg dlg;
2819 // tell the dialog to use mode for selecting revisions
2820 dlg.SetSelect(true);
2821 // allow multi-select
2822 dlg.SingleSelection(false);
2823 if (dlg.DoModal() != IDOK || dlg.GetSelectedHash().empty())
2824 return;
2826 auto selectedHashes = dlg.GetSelectedHash();
2827 for (auto it = selectedHashes.crbegin(); it != selectedHashes.crend(); ++it)
2829 GitRevLoglist* pRev = m_CommitList.m_logEntries.m_pLogCache->GetCacheData(*it);
2830 if (pRev->GetCommit(*it))
2831 return;
2832 if (pRev->GetParentFromHash(pRev->m_CommitHash))
2833 return;
2834 pRev->GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_PICK;
2835 if (m_CommitList.m_IsOldFirst)
2837 m_CommitList.m_logEntries.push_back(pRev->m_CommitHash);
2838 m_CommitList.m_arShownList.SafeAdd(pRev);
2840 else
2842 m_CommitList.m_logEntries.insert(m_CommitList.m_logEntries.cbegin(), pRev->m_CommitHash);
2843 m_CommitList.m_arShownList.SafeAddFront(pRev);
2846 m_CommitList.SetItemCountEx((int)m_CommitList.m_logEntries.size());
2847 m_CommitList.Invalidate();
2849 if (m_CommitList.m_IsOldFirst)
2850 m_CurrentRebaseIndex = -1;
2851 else
2852 m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();