For git > 2.1, pass --date=now when amending commit with author date reset
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
blobd3645c430f28bbb5f8ddc7e26fcfaeb4359c81b1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // 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"
38 // CRebaseDlg dialog
40 IMPLEMENT_DYNAMIC(CRebaseDlg, CResizableStandAloneDialog)
42 CRebaseDlg::CRebaseDlg(CWnd* pParent /*=NULL*/)
43 : CResizableStandAloneDialog(CRebaseDlg::IDD, pParent)
44 , m_bAddCherryPickedFrom(FALSE)
45 , m_bStatusWarning(false)
46 , m_bAutoSkipFailedCommit(FALSE)
47 , m_bFinishedRebase(false)
48 , m_bStashed(false)
49 , m_bSplitCommit(FALSE)
51 m_RebaseStage=CHOOSE_BRANCH;
52 m_CurrentRebaseIndex=-1;
53 m_bThreadRunning =FALSE;
54 this->m_IsCherryPick = FALSE;
55 m_bForce=FALSE;
56 m_IsFastForward=FALSE;
59 CRebaseDlg::~CRebaseDlg()
63 void CRebaseDlg::DoDataExchange(CDataExchange* pDX)
65 CDialog::DoDataExchange(pDX);
66 DDX_Control(pDX, IDC_REBASE_PROGRESS, m_ProgressBar);
67 DDX_Control(pDX, IDC_STATUS_STATIC, m_CtrlStatusText);
68 DDX_Control(pDX, IDC_REBASE_SPLIT, m_wndSplitter);
69 DDX_Control(pDX,IDC_COMMIT_LIST,m_CommitList);
70 DDX_Control(pDX,IDC_REBASE_COMBOXEX_BRANCH, this->m_BranchCtrl);
71 DDX_Control(pDX,IDC_REBASE_COMBOXEX_UPSTREAM, this->m_UpstreamCtrl);
72 DDX_Check(pDX, IDC_REBASE_CHECK_FORCE,m_bForce);
73 DDX_Check(pDX, IDC_CHECK_CHERRYPICKED_FROM, m_bAddCherryPickedFrom);
74 DDX_Control(pDX,IDC_REBASE_POST_BUTTON,m_PostButton);
75 DDX_Control(pDX, IDC_SPLITALLOPTIONS, m_SplitAllOptions);
76 DDX_Check(pDX, IDC_REBASE_SPLIT_COMMIT, m_bSplitCommit);
80 BEGIN_MESSAGE_MAP(CRebaseDlg, CResizableStandAloneDialog)
81 ON_BN_CLICKED(IDC_REBASE_SPLIT, &CRebaseDlg::OnBnClickedRebaseSplit)
82 ON_BN_CLICKED(IDC_REBASE_CONTINUE,OnBnClickedContinue)
83 ON_BN_CLICKED(IDC_REBASE_ABORT, OnBnClickedAbort)
84 ON_WM_SIZE()
85 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_BRANCH, &CRebaseDlg::OnCbnSelchangeBranch)
86 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_UPSTREAM, &CRebaseDlg::OnCbnSelchangeUpstream)
87 ON_MESSAGE(MSG_REBASE_UPDATE_UI, OnRebaseUpdateUI)
88 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnGitStatusListCtrlNeedsRefresh)
89 ON_BN_CLICKED(IDC_BUTTON_REVERSE, OnBnClickedButtonReverse)
90 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CRebaseDlg::OnBnClickedButtonBrowse)
91 ON_BN_CLICKED(IDC_REBASE_CHECK_FORCE, &CRebaseDlg::OnBnClickedRebaseCheckForce)
92 ON_BN_CLICKED(IDC_CHECK_CHERRYPICKED_FROM, &CRebaseDlg::OnBnClickedCheckCherryPickedFrom)
93 ON_BN_CLICKED(IDC_REBASE_POST_BUTTON, &CRebaseDlg::OnBnClickedRebasePostButton)
94 ON_BN_CLICKED(IDC_BUTTON_UP2, &CRebaseDlg::OnBnClickedButtonUp2)
95 ON_BN_CLICKED(IDC_BUTTON_DOWN2, &CRebaseDlg::OnBnClickedButtonDown2)
96 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
97 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COMMIT_LIST, OnLvnItemchangedLoglist)
98 ON_REGISTERED_MESSAGE(CGitLogListBase::m_RebaseActionMessage, OnRebaseActionMessage)
99 ON_WM_CTLCOLOR()
100 ON_BN_CLICKED(IDC_SPLITALLOPTIONS, &CRebaseDlg::OnBnClickedSplitAllOptions)
101 ON_BN_CLICKED(IDC_REBASE_SPLIT_COMMIT, &CRebaseDlg::OnBnClickedRebaseSplitCommit)
102 ON_BN_CLICKED(IDC_BUTTON_ONTO, &CRebaseDlg::OnBnClickedButtonOnto)
103 END_MESSAGE_MAP()
105 void CRebaseDlg::AddRebaseAnchor()
107 AddAnchor(IDC_REBASE_TAB,TOP_LEFT,BOTTOM_RIGHT);
108 AddAnchor(IDC_COMMIT_LIST,TOP_LEFT, TOP_RIGHT);
109 AddAnchor(IDC_REBASE_SPLIT,TOP_LEFT, TOP_RIGHT);
110 AddAnchor(IDC_STATUS_STATIC, BOTTOM_LEFT,BOTTOM_RIGHT);
111 AddAnchor(IDC_REBASE_CONTINUE,BOTTOM_RIGHT);
112 AddAnchor(IDC_REBASE_ABORT, BOTTOM_RIGHT);
113 AddAnchor(IDC_REBASE_PROGRESS,BOTTOM_LEFT, BOTTOM_RIGHT);
114 AddAnchor(IDC_SPLITALLOPTIONS, TOP_LEFT);
115 AddAnchor(IDC_BUTTON_UP2,TOP_LEFT);
116 AddAnchor(IDC_BUTTON_DOWN2,TOP_LEFT);
117 AddAnchor(IDC_REBASE_COMBOXEX_UPSTREAM,TOP_LEFT);
118 AddAnchor(IDC_REBASE_COMBOXEX_BRANCH,TOP_LEFT);
119 AddAnchor(IDC_REBASE_STATIC_UPSTREAM,TOP_LEFT);
120 AddAnchor(IDC_REBASE_STATIC_BRANCH,TOP_LEFT);
121 AddAnchor(IDHELP, BOTTOM_RIGHT);
122 AddAnchor(IDC_REBASE_CHECK_FORCE,TOP_RIGHT);
123 AddAnchor(IDC_CHECK_CHERRYPICKED_FROM, TOP_RIGHT);
124 AddAnchor(IDC_REBASE_SPLIT_COMMIT, BOTTOM_RIGHT);
125 AddAnchor(IDC_REBASE_POST_BUTTON,BOTTOM_LEFT);
127 this->AddOthersToAnchor();
130 BOOL CRebaseDlg::OnInitDialog()
132 CResizableStandAloneDialog::OnInitDialog();
133 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
135 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
136 // do this, Explorer would be unable to send that message to our window if we
137 // were running elevated. It's OK to make the call all the time, since if we're
138 // not elevated, this is a no-op.
139 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
140 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
141 CAutoLibrary hUser = AtlLoadSystemLibraryUsingFullPath(_T("user32.dll"));
142 if (hUser)
144 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
145 if (pfnChangeWindowMessageFilterEx)
147 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
150 m_pTaskbarList.Release();
151 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
152 m_pTaskbarList = nullptr;
154 CRect rectDummy;
155 //IDC_REBASE_DUMY_TAB
157 GetClientRect(m_DlgOrigRect);
158 m_CommitList.GetClientRect(m_CommitListOrigRect);
160 CWnd *pwnd=this->GetDlgItem(IDC_REBASE_DUMY_TAB);
161 pwnd->GetWindowRect(&rectDummy);
162 this->ScreenToClient(rectDummy);
164 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_REBASE_TAB))
166 TRACE0("Failed to create output tab window\n");
167 return FALSE; // fail to create
169 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
170 // Create output panes:
171 //const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
172 DWORD dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
174 if (! this->m_FileListCtrl.Create(dwStyle,rectDummy,&this->m_ctrlTabCtrl,0) )
176 TRACE0("Failed to create output windows\n");
177 return FALSE; // fail to create
179 m_FileListCtrl.m_hwndLogicalParent = this;
181 if( ! this->m_LogMessageCtrl.Create(_T("Scintilla"),_T("source"),0,rectDummy,&m_ctrlTabCtrl,0,0) )
183 TRACE0("Failed to create log message control");
184 return FALSE;
186 m_ProjectProperties.ReadProps();
187 m_LogMessageCtrl.Init(m_ProjectProperties);
188 m_LogMessageCtrl.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
189 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
191 dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
193 if (!m_wndOutputRebase.Create(_T("Scintilla"),_T("source"),0,rectDummy, &m_ctrlTabCtrl, 0,0) )
195 TRACE0("Failed to create output windows\n");
196 return -1; // fail to create
198 m_wndOutputRebase.Init(0, FALSE);
199 m_wndOutputRebase.Call(SCI_SETREADONLY, TRUE);
201 m_tooltips.AddTool(IDC_REBASE_CHECK_FORCE,IDS_REBASE_FORCE_TT);
202 m_tooltips.AddTool(IDC_REBASE_ABORT, IDS_REBASE_ABORT_TT);
205 CString temp;
206 temp.LoadString(IDS_PROC_REBASE_SELECTALL_PICK);
207 m_SplitAllOptions.AddEntry(temp);
208 temp.LoadString(IDS_PROC_REBASE_SELECTALL_SQUASH);
209 m_SplitAllOptions.AddEntry(temp);
210 temp.LoadString(IDS_PROC_REBASE_SELECTALL_EDIT);
211 m_SplitAllOptions.AddEntry(temp);
214 m_FileListCtrl.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("RebaseDlg"),(GITSLC_POPALL ^ (GITSLC_POPCOMMIT|GITSLC_POPRESTORE)), false, true, GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL);
216 m_ctrlTabCtrl.AddTab(&m_FileListCtrl, CString(MAKEINTRESOURCE(IDS_PROC_REVISIONFILES)));
217 m_ctrlTabCtrl.AddTab(&m_LogMessageCtrl, CString(MAKEINTRESOURCE(IDS_PROC_COMMITMESSAGE)), 1);
218 AddRebaseAnchor();
220 AdjustControlSize(IDC_CHECK_CHERRYPICKED_FROM);
221 AdjustControlSize(IDC_REBASE_SPLIT_COMMIT);
223 CString sWindowTitle;
224 GetWindowText(sWindowTitle);
225 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
227 EnableSaveRestore(_T("RebaseDlg"));
229 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
230 RECT rcDlg, rcLogMsg, rcFileList;
231 GetClientRect(&rcDlg);
232 m_CommitList.GetWindowRect(&rcLogMsg);
233 ScreenToClient(&rcLogMsg);
234 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
235 ScreenToClient(&rcFileList);
236 if (yPos)
238 RECT rectSplitter;
239 m_wndSplitter.GetWindowRect(&rectSplitter);
240 ScreenToClient(&rectSplitter);
241 int delta = yPos - rectSplitter.top;
242 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
244 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
245 DoSize(delta);
249 if( this->m_RebaseStage == CHOOSE_BRANCH)
251 this->LoadBranchInfo();
254 else
256 this->m_BranchCtrl.EnableWindow(FALSE);
257 this->m_UpstreamCtrl.EnableWindow(FALSE);
258 GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
261 m_CommitList.m_ColumnRegKey = _T("Rebase");
262 m_CommitList.m_IsIDReplaceAction = TRUE;
263 // m_CommitList.m_IsOldFirst = TRUE;
264 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
265 m_CommitList.m_bNoHightlightHead = TRUE;
267 m_CommitList.InsertGitColumn();
269 this->SetControlEnable();
271 if(m_IsCherryPick)
273 this->m_BranchCtrl.SetCurSel(-1);
274 this->m_BranchCtrl.EnableWindow(FALSE);
275 GetDlgItem(IDC_REBASE_CHECK_FORCE)->ShowWindow(SW_HIDE);
276 GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(FALSE);
277 GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
278 GetDlgItem(IDC_BUTTON_ONTO)->EnableWindow(FALSE);
279 this->m_UpstreamCtrl.AddString(_T("HEAD"));
280 this->m_UpstreamCtrl.EnableWindow(FALSE);
281 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_CHERRYPICK)));
282 this->m_CommitList.StartFilter();
284 else
286 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
287 GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM)->ShowWindow(SW_HIDE);
288 ((CButton *)GetDlgItem(IDC_BUTTON_REVERSE))->SetIcon((HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SWITCHLEFTRIGHT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
289 SetContinueButtonText();
290 m_CommitList.DeleteAllItems();
291 FetchLogList();
294 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
295 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
296 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
297 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
298 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_MERGEREV) |
299 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
300 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
301 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
303 if(m_CommitList.m_IsOldFirst)
304 this->m_CurrentRebaseIndex = -1;
305 else
306 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
308 return TRUE;
310 // CRebaseDlg message handlers
312 HBRUSH CRebaseDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
314 if (pWnd->GetDlgCtrlID() == IDC_STATUS_STATIC && nCtlColor == CTLCOLOR_STATIC && m_bStatusWarning)
316 pDC->SetBkColor(RGB(255, 0, 0));
317 pDC->SetTextColor(RGB(255, 255, 255));
318 return CreateSolidBrush(RGB(255, 0, 0));
321 return CResizableStandAloneDialog::OnCtlColor(pDC, pWnd, nCtlColor);
324 void CRebaseDlg::SetAllRebaseAction(int action)
326 for (size_t i = 0; i < this->m_CommitList.m_logEntries.size(); ++i)
328 m_CommitList.m_logEntries.GetGitRevAt(i).GetRebaseAction() = action;
330 m_CommitList.Invalidate();
333 void CRebaseDlg::OnBnClickedRebaseSplit()
335 this->UpdateData();
338 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
340 switch (message) {
341 case WM_NOTIFY:
342 if (wParam == IDC_REBASE_SPLIT)
344 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
345 DoSize(pHdr->delta);
347 break;
350 return __super::DefWindowProc(message, wParam, lParam);
353 void CRebaseDlg::DoSize(int delta)
355 this->RemoveAllAnchors();
357 CSplitterControl::ChangeHeight(GetDlgItem(IDC_COMMIT_LIST), delta, CW_TOPALIGN);
358 //CSplitterControl::ChangeHeight(GetDlgItem(), delta, CW_TOPALIGN);
359 CSplitterControl::ChangeHeight(GetDlgItem(IDC_REBASE_TAB), -delta, CW_BOTTOMALIGN);
360 //CSplitterControl::ChangeHeight(GetDlgItem(), -delta, CW_BOTTOMALIGN);
361 CSplitterControl::ChangePos(GetDlgItem(IDC_SPLITALLOPTIONS), 0, delta);
362 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_UP2),0,delta);
363 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_DOWN2),0,delta);
364 CSplitterControl::ChangePos(GetDlgItem(IDC_REBASE_CHECK_FORCE),0,delta);
365 CSplitterControl::ChangePos(GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM), 0, delta);
367 this->AddRebaseAnchor();
368 // adjust the minimum size of the dialog to prevent the resizing from
369 // moving the list control too far down.
370 CRect rcLogMsg;
371 m_CommitList.GetClientRect(rcLogMsg);
372 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
374 SetSplitterRange();
375 // m_CommitList.Invalidate();
377 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
379 this->m_ctrlTabCtrl.Invalidate();
380 this->m_CommitList.Invalidate();
381 this->m_FileListCtrl.Invalidate();
382 this->m_LogMessageCtrl.Invalidate();
386 void CRebaseDlg::SetSplitterRange()
388 if ((m_CommitList)&&(m_ctrlTabCtrl))
390 CRect rcTop;
391 m_CommitList.GetWindowRect(rcTop);
392 ScreenToClient(rcTop);
393 CRect rcMiddle;
394 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
395 ScreenToClient(rcMiddle);
396 if (rcMiddle.Height() && rcMiddle.Width())
397 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
401 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
403 // first, let the resizing take place
404 __super::OnSize(nType, cx, cy);
406 //set range
407 SetSplitterRange();
410 void CRebaseDlg::SaveSplitterPos()
412 if (!IsIconic())
414 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
415 RECT rectSplitter;
416 m_wndSplitter.GetWindowRect(&rectSplitter);
417 ScreenToClient(&rectSplitter);
418 regPos = rectSplitter.top;
422 void CRebaseDlg::LoadBranchInfo()
424 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
425 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
427 STRING_VECTOR list;
428 list.clear();
429 int current = -1;
430 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
431 m_BranchCtrl.SetList(list);
432 if (current >= 0)
433 m_BranchCtrl.SetCurSel(current);
434 else
435 m_BranchCtrl.AddString(g_Git.GetCurrentBranch(true));
436 list.clear();
437 g_Git.GetBranchList(list, NULL, CGit::BRANCH_ALL_F);
438 g_Git.GetTagList(list);
439 m_UpstreamCtrl.SetList(list);
441 AddBranchToolTips(&m_BranchCtrl);
443 if(!m_Upstream.IsEmpty())
445 m_UpstreamCtrl.AddString(m_Upstream);
447 else
449 //Select pull-remote from current branch
450 CString pullRemote, pullBranch;
451 g_Git.GetRemoteTrackedBranchForHEAD(pullRemote, pullBranch);
453 CString defaultUpstream;
454 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
455 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
456 if(found >= 0)
457 m_UpstreamCtrl.SetCurSel(found);
458 else
459 m_UpstreamCtrl.SetCurSel(-1);
461 AddBranchToolTips(&m_UpstreamCtrl);
464 void CRebaseDlg::OnCbnSelchangeBranch()
466 FetchLogList();
469 void CRebaseDlg::OnCbnSelchangeUpstream()
471 FetchLogList();
474 void CRebaseDlg::FetchLogList()
476 CGitHash base,hash,upstream;
477 m_IsFastForward=FALSE;
479 if (m_BranchCtrl.GetString().IsEmpty())
481 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_SELECTBRANCH)));
482 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
483 return;
486 if (g_Git.GetHash(hash, m_BranchCtrl.GetString()))
488 m_CommitList.ShowText(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")));
489 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
490 return;
493 if (m_UpstreamCtrl.GetString().IsEmpty())
495 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_SELECTUPSTREAM)));
496 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
497 return;
500 if (g_Git.GetHash(upstream, m_UpstreamCtrl.GetString()))
502 m_CommitList.ShowText(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_UpstreamCtrl.GetString() + _T("\".")));
503 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
504 return;
507 if (hash == upstream)
509 m_CommitList.Clear();
510 CString text,fmt;
511 fmt.LoadString(IDS_REBASE_EQUAL_FMT);
512 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
514 m_CommitList.ShowText(text);
515 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
516 return;
519 if (g_Git.IsFastForward(m_BranchCtrl.GetString(), m_UpstreamCtrl.GetString(), &base) && m_Onto.IsEmpty())
521 //fast forword
522 this->m_IsFastForward=TRUE;
524 m_CommitList.Clear();
525 CString text,fmt;
526 fmt.LoadString(IDS_REBASE_FASTFORWARD_FMT);
527 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString(),
528 m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
530 m_CommitList.ShowText(text);
531 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
532 SetContinueButtonText();
534 return ;
537 if (!m_bForce && m_Onto.IsEmpty())
539 if (base == upstream)
541 m_CommitList.Clear();
542 CString text,fmt;
543 fmt.LoadString(IDS_REBASE_UPTODATE_FMT);
544 text.Format(fmt,m_BranchCtrl.GetString());
545 m_CommitList.ShowText(text);
546 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
547 SetContinueButtonText();
548 return;
552 m_CommitList.Clear();
553 CString refFrom = g_Git.FixBranchName(m_UpstreamCtrl.GetString());
554 CString refTo = g_Git.FixBranchName(m_BranchCtrl.GetString());
555 CString range;
556 range.Format(_T("%s..%s"), refFrom, refTo);
557 this->m_CommitList.FillGitLog(nullptr, &range, 0);
559 if( m_CommitList.GetItemCount() == 0 )
560 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_NOTHINGTOREBASE)));
562 #if 0
563 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
565 if(upstream == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
567 m_CommitList.Clear();
568 m_CommitList.ShowText(_T("Nothing Rebase"));
571 #endif
573 m_tooltips.Pop();
574 AddBranchToolTips(&this->m_BranchCtrl);
575 AddBranchToolTips(&this->m_UpstreamCtrl);
577 // Default all actions to 'pick'
578 std::map<CGitHash, size_t> revIxMap;
579 for (size_t i = 0; i < m_CommitList.m_logEntries.size(); ++i)
581 GitRevLoglist& rev = m_CommitList.m_logEntries.GetGitRevAt(i);
582 rev.GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_PICK;
583 revIxMap[rev.m_CommitHash] = i;
586 // Default to skip when already in upstream
587 if (!m_Onto.IsEmpty())
588 refFrom = g_Git.FixBranchName(m_Onto);
589 CString cherryCmd;
590 cherryCmd.Format(L"git.exe cherry \"%s\" \"%s\"", refFrom, refTo);
591 bool bHasSKip = false;
592 g_Git.Run(cherryCmd, [&](const CStringA& line)
594 if (line.GetLength() < 2)
595 return;
596 if (line[0] != '-')
597 return; // Don't skip (only skip commits starting with a '-')
598 CString hash = CUnicodeUtils::GetUnicode(line.Mid(1));
599 hash.Trim();
600 auto itIx = revIxMap.find(CGitHash(hash));
601 if (itIx == revIxMap.end())
602 return; // Not found?? Should not occur...
604 // Found. Skip it.
605 m_CommitList.m_logEntries.GetGitRevAt(itIx->second).GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_SKIP;
606 bHasSKip = true;
609 m_CommitList.Invalidate();
610 if (bHasSKip)
612 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_REBASE_AUTOSKIPPED)));
613 m_bStatusWarning = true;
615 else
617 m_CtrlStatusText.SetWindowText(m_sStatusText);
618 m_bStatusWarning = false;
620 m_CtrlStatusText.Invalidate();
622 if(m_CommitList.m_IsOldFirst)
623 this->m_CurrentRebaseIndex = -1;
624 else
625 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
627 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
628 SetContinueButtonText();
631 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
633 if(pBranch)
635 CString text=pBranch->GetString();
636 CString tooltip;
638 if (text.IsEmpty())
640 pBranch->DisableTooltip();
641 return;
644 GitRev rev;
645 if (rev.GetCommit(text))
647 MessageBox(rev.GetLastErr(), _T("TortoiseGit"), MB_ICONERROR);
648 pBranch->DisableTooltip();
649 return;
652 tooltip.Format(_T("%s: %s\n%s: %s <%s>\n%s: %s\n%s:\n%s\n%s"),
653 CString(MAKEINTRESOURCE(IDS_LOG_REVISION)),
654 rev.m_CommitHash.ToString(),
655 CString(MAKEINTRESOURCE(IDS_LOG_AUTHOR)),
656 rev.GetAuthorName(),
657 rev.GetAuthorEmail(),
658 CString(MAKEINTRESOURCE(IDS_LOG_DATE)),
659 CLoglistUtils::FormatDateAndTime(rev.GetAuthorDate(), DATE_LONGDATE),
660 CString(MAKEINTRESOURCE(IDS_LOG_MESSAGE)),
661 rev.GetSubject(),
662 rev.GetBody());
664 pBranch->DisableTooltip();
665 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
669 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
671 if (pMsg->message == WM_KEYDOWN)
673 switch (pMsg->wParam)
675 case ' ':
676 if (LogListHasFocus(pMsg->hwnd)
677 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_PICK)
678 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SQUASH)
679 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_EDIT)
680 && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SKIP))
682 m_CommitList.ShiftSelectedRebaseAction();
683 return TRUE;
685 break;
686 case 'P':
687 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_PICK))
689 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_PICK);
690 return TRUE;
692 break;
693 case 'S':
694 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SKIP))
696 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SKIP);
697 return TRUE;
699 break;
700 case 'Q':
701 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_SQUASH))
703 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SQUASH);
704 return TRUE;
706 break;
707 case 'E':
708 if (LogListHasFocus(pMsg->hwnd) && LogListHasMenuItem(CGitLogListBase::ID_REBASE_EDIT))
710 m_CommitList.SetSelectedRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_EDIT);
711 return TRUE;
713 break;
714 case 'A':
715 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
717 // select all entries
718 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
720 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
722 return TRUE;
724 break;
725 case VK_F5:
727 Refresh();
728 return TRUE;
730 break;
731 case VK_RETURN:
733 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
735 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
736 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
737 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
738 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
739 else
740 GetDlgItem(IDHELP)->SetFocus();
741 return TRUE;
744 break;
745 /* Avoid TAB control destroy but dialog exist*/
746 case VK_ESCAPE:
747 case VK_CANCEL:
749 TCHAR buff[128] = { 0 };
750 ::GetClassName(pMsg->hwnd,buff,128);
753 /* Use MSFTEDIT_CLASS http://msdn.microsoft.com/en-us/library/bb531344.aspx */
754 if (_tcsnicmp(buff, MSFTEDIT_CLASS, 128) == 0 || //Unicode and MFC 2012 and later
755 _tcsnicmp(buff, RICHEDIT_CLASS, 128) == 0 || //ANSI or MFC 2010
756 _tcsnicmp(buff,_T("Scintilla"),128)==0 ||
757 _tcsnicmp(buff,_T("SysListView32"),128)==0||
758 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
760 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
761 return TRUE;
766 else if (pMsg->message == WM_NEXTDLGCTL)
768 HWND hwnd = GetFocus()->GetSafeHwnd();
769 if (hwnd == m_LogMessageCtrl.GetSafeHwnd() || hwnd == m_wndOutputRebase.GetSafeHwnd())
771 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
772 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
773 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
774 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
775 else
776 GetDlgItem(IDHELP)->SetFocus();
777 return TRUE;
780 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
783 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
785 TCHAR buff[128] = { 0 };
786 ::GetClassName(hwnd, buff, 128);
788 if(_tcsnicmp(buff, _T("SysListView32"), 128) == 0)
789 return true;
790 return false;
793 bool CRebaseDlg::LogListHasMenuItem(int i)
795 return (m_CommitList.m_ContextMenuMask & m_CommitList.GetContextMenuBit(i)) != 0;
798 int CRebaseDlg::CheckRebaseCondition()
800 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
802 if( !g_Git.CheckCleanWorkTree() )
804 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
806 CString cmd,out;
807 cmd=_T("git.exe stash");
808 this->AddLogString(cmd);
809 if (g_Git.Run(cmd, &out, CP_UTF8))
811 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
812 return -1;
814 m_bStashed = true;
816 else
817 return -1;
819 //Todo Check $REBASE_ROOT
820 //Todo Check $DOTEST
822 if (!CAppUtils::CheckUserData())
823 return -1;
825 //Todo call pre_rebase_hook
826 return 0;
829 void CRebaseDlg::CheckRestoreStash()
831 if (m_bStashed && CMessageBox::Show(nullptr, IDS_DCOMMIT_STASH_POP, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) == IDYES)
832 CAppUtils::StashPop();
833 m_bStashed = false;
836 int CRebaseDlg::StartRebase()
838 CString cmd,out;
839 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
841 m_OrigHEADBranch = g_Git.GetCurrentBranch(true);
843 m_OrigHEADHash.Empty();
844 if (g_Git.GetHash(m_OrigHEADHash, _T("HEAD")))
846 AddLogString(CString(MAKEINTRESOURCE(IDS_PROC_NOHEAD)));
847 return -1;
849 //Todo
850 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
851 // echo "detached HEAD" > "$DOTEST"/head-name
853 cmd.Format(_T("git.exe update-ref ORIG_HEAD ") + m_OrigHEADHash.ToString());
854 if(g_Git.Run(cmd,&out,CP_UTF8))
856 AddLogString(_T("update ORIG_HEAD Fail"));
857 return -1;
860 m_OrigUpstreamHash.Empty();
861 if (g_Git.GetHash(m_OrigUpstreamHash, (m_IsCherryPick || m_Onto.IsEmpty()) ? m_UpstreamCtrl.GetString() : m_Onto))
863 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + (m_IsCherryPick || m_Onto.IsEmpty()) ? m_UpstreamCtrl.GetString() : m_Onto + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
864 return -1;
867 if( !this->m_IsCherryPick )
869 cmd.Format(_T("git.exe checkout -f %s --"), m_OrigUpstreamHash.ToString());
870 this->AddLogString(cmd);
871 while (true)
873 out.Empty();
874 if (g_Git.Run(cmd, &out, CP_UTF8))
876 this->AddLogString(out);
877 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
878 return -1;
880 else
881 break;
885 CString log;
886 if( !this->m_IsCherryPick )
888 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
890 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
891 return -1;
893 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTREBASE)));
895 else
896 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTCHERRYPICK)));
898 this->AddLogString(log);
899 return 0;
901 int CRebaseDlg::VerifyNoConflict()
903 int hasConflicts = g_Git.HasWorkingTreeConflicts();
904 if (hasConflicts < 0)
906 AddLogString(g_Git.GetGitLastErr(L"Checking for conflicts failed.", CGit::GIT_CMD_CHECKCONFLICTS));
907 return -1;
909 if (hasConflicts)
911 CMessageBox::Show(NULL, IDS_PROGRS_CONFLICTSOCCURED, IDS_APPNAME, MB_OK);
912 return -1;
914 return 0;
918 static bool IsLocalBranch(CString ref)
920 STRING_VECTOR list;
921 g_Git.GetBranchList(list, nullptr, CGit::BRANCH_LOCAL);
922 return std::find(list.begin(), list.end(), ref) != list.end();
925 int CRebaseDlg::FinishRebase()
927 if (m_bFinishedRebase)
928 return 0;
930 m_bFinishedRebase = true;
931 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
933 m_sStatusText.LoadString(IDS_DONE);
934 m_CtrlStatusText.SetWindowText(m_sStatusText);
935 m_bStatusWarning = false;
936 m_CtrlStatusText.Invalidate();
937 return 0;
940 CGitHash head;
941 if (g_Git.GetHash(head, _T("HEAD")))
943 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
944 return -1;
946 CString out,cmd;
948 if (IsLocalBranch(m_BranchCtrl.GetString()))
950 cmd.Format(_T("git.exe checkout -f -B %s %s --"), m_BranchCtrl.GetString(), head.ToString());
951 AddLogString(cmd);
952 while (true)
954 out.Empty();
955 if (g_Git.Run(cmd, &out, CP_UTF8))
957 AddLogString(out);
958 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
959 return -1;
961 else
962 break;
964 AddLogString(out);
967 cmd.Format(_T("git.exe reset --hard %s --"), head.ToString());
968 AddLogString(cmd);
969 while (true)
971 out.Empty();
972 if (g_Git.Run(cmd, &out, CP_UTF8))
974 AddLogString(out);
975 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
976 return -1;
978 else
979 break;
981 AddLogString(out);
983 while (m_ctrlTabCtrl.GetTabsNum() > 1)
984 m_ctrlTabCtrl.RemoveTab(0);
985 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED)));
986 m_sStatusText = CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED));
987 m_bStatusWarning = false;
988 m_CtrlStatusText.Invalidate();
990 return 0;
992 void CRebaseDlg::OnBnClickedContinue()
994 if( m_RebaseStage == REBASE_DONE)
996 OnOK();
997 CheckRestoreStash();
998 return;
1001 if (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE)
1003 if (CheckRebaseCondition())
1004 return;
1007 if( this->m_IsFastForward )
1009 CString cmd,out;
1010 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
1012 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
1013 return;
1015 if (g_Git.GetHash(m_OrigUpstreamHash, m_UpstreamCtrl.GetString()))
1017 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_UpstreamCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
1018 return;
1021 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
1023 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1024 AddLogString(_T("No fast forward possible.\r\nMaybe repository changed"));
1025 return;
1028 if (IsLocalBranch(m_BranchCtrl.GetString()))
1030 cmd.Format(_T("git.exe checkout --no-track -f -B %s %s --"), m_BranchCtrl.GetString(), m_UpstreamCtrl.GetString());
1031 AddLogString(cmd);
1032 while (true)
1034 out.Empty();
1035 if (g_Git.Run(cmd, &out, CP_UTF8))
1037 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1038 AddLogString(out);
1039 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1040 return;
1042 else
1043 break;
1045 AddLogString(out);
1046 out.Empty();
1048 cmd.Format(_T("git.exe reset --hard %s --"), g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1049 CString log;
1050 log.Format(IDS_PROC_REBASE_FFTO, m_UpstreamCtrl.GetString());
1051 this->AddLogString(log);
1053 AddLogString(cmd);
1054 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1055 while (true)
1057 out.Empty();
1058 if (g_Git.Run(cmd, &out, CP_UTF8))
1060 AddLogString(CString(MAKEINTRESOURCE(IDS_FAIL)));
1061 AddLogString(out);
1062 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1063 return;
1065 else
1066 break;
1068 AddLogString(out);
1069 AddLogString(CString(MAKEINTRESOURCE(IDS_DONE)));
1070 m_RebaseStage = REBASE_DONE;
1071 UpdateCurrentStatus();
1072 return;
1075 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
1077 if(CheckRebaseCondition())
1078 return ;
1079 m_RebaseStage = REBASE_START;
1080 m_FileListCtrl.Clear();
1081 m_FileListCtrl.SetHasCheckboxes(false);
1082 m_FileListCtrl.m_CurrentVersion = L"";
1083 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, CString(MAKEINTRESOURCE(IDS_PROC_CONFLICTFILES)));
1084 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase, CString(MAKEINTRESOURCE(IDS_LOG)), 2);
1087 if( m_RebaseStage == REBASE_FINISH )
1089 if(FinishRebase())
1090 return ;
1092 OnOK();
1095 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
1097 if(VerifyNoConflict())
1098 return;
1099 GitRevLoglist* curRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1100 if(this->CheckNextCommitIsSquash())
1101 {//next commit is not squash;
1102 m_RebaseStage = REBASE_SQUASH_EDIT;
1103 this->OnRebaseUpdateUI(0,0);
1104 this->UpdateCurrentStatus();
1105 return ;
1108 m_RebaseStage=REBASE_CONTINUE;
1109 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1110 this->UpdateCurrentStatus();
1114 if( m_RebaseStage == REBASE_CONFLICT )
1116 if(VerifyNoConflict())
1117 return;
1119 GitRevLoglist* curRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1120 // ***************************************************
1121 // ATTENTION: Similar code in CommitDlg.cpp!!!
1122 // ***************************************************
1123 CMassiveGitTask mgtReAddAfterCommit(_T("add --ignore-errors -f"));
1124 CMassiveGitTask mgtAdd(_T("add -f"));
1125 CMassiveGitTask mgtUpdateIndexForceRemove(_T("update-index --force-remove"));
1126 CMassiveGitTask mgtUpdateIndex(_T("update-index"));
1127 CMassiveGitTask mgtRm(_T("rm --ignore-unmatch"));
1128 CMassiveGitTask mgtRmFCache(_T("rm -f --cache"));
1129 CMassiveGitTask mgtReset(_T("reset"), TRUE, true);
1130 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
1132 CTGitPath *entry = (CTGitPath *)m_FileListCtrl.GetItemData(i);
1133 if (entry->m_Checked)
1135 if (entry->m_Action & CTGitPath::LOGACTIONS_UNVER)
1136 mgtAdd.AddFile(entry->GetGitPathString());
1137 else if (entry->m_Action & CTGitPath::LOGACTIONS_DELETED)
1138 mgtUpdateIndexForceRemove.AddFile(entry->GetGitPathString());
1139 else
1140 mgtUpdateIndex.AddFile(entry->GetGitPathString());
1142 if (entry->m_Action & CTGitPath::LOGACTIONS_REPLACED)
1143 mgtRm.AddFile(entry->GetGitOldPathString());
1145 else
1147 if (entry->m_Action & CTGitPath::LOGACTIONS_ADDED || entry->m_Action & CTGitPath::LOGACTIONS_REPLACED)
1149 mgtRmFCache.AddFile(entry->GetGitPathString());
1150 mgtReAddAfterCommit.AddFile(*entry);
1152 if (entry->m_Action & CTGitPath::LOGACTIONS_REPLACED && !entry->GetGitOldPathString().IsEmpty())
1153 mgtReset.AddFile(entry->GetGitOldPathString());
1155 else if(!(entry->m_Action & CTGitPath::LOGACTIONS_UNVER))
1156 mgtReset.AddFile(entry->GetGitPathString());
1160 BOOL cancel = FALSE;
1161 bool successful = true;
1162 successful = successful && mgtAdd.Execute(cancel);
1163 successful = successful && mgtUpdateIndexForceRemove.Execute(cancel);
1164 successful = successful && mgtUpdateIndex.Execute(cancel);
1165 successful = successful && mgtRm.Execute(cancel);
1166 successful = successful && mgtRmFCache.Execute(cancel);
1167 successful = successful && mgtReset.Execute(cancel);
1169 if (!successful)
1171 AddLogString(_T("An error occurred while updating the index."));
1172 return;
1175 CString out =_T("");
1176 CString cmd;
1177 cmd.Format(_T("git.exe commit -C %s"), curRev->m_CommitHash.ToString());
1179 AddLogString(cmd);
1181 if(g_Git.Run(cmd,&out,CP_UTF8))
1183 AddLogString(out);
1184 if(!g_Git.CheckCleanWorkTree())
1186 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1187 return;
1191 AddLogString(out);
1193 // update commit message if needed
1194 CString str = m_LogMessageCtrl.GetText().Trim();
1195 if (str != (curRev->GetSubject() + _T("\n") + curRev->GetBody()).Trim())
1197 if (str.Trim().IsEmpty())
1199 CMessageBox::Show(NULL, IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1200 return;
1202 CString tempfile = ::GetTempFile();
1203 if (CAppUtils::SaveCommitUnicodeFile(tempfile, str))
1205 CMessageBox::Show(nullptr, _T("Could not save commit message"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1206 return;
1209 out.Empty();
1210 cmd.Format(_T("git.exe commit --amend -F \"%s\""), tempfile);
1211 AddLogString(cmd);
1213 if (g_Git.Run(cmd, &out, CP_UTF8))
1215 AddLogString(out);
1216 if (!g_Git.CheckCleanWorkTree())
1218 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1219 return;
1223 AddLogString(out);
1226 if (((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\ReaddUnselectedAddedFilesAfterCommit"), TRUE)) == TRUE)
1228 BOOL cancel = FALSE;
1229 mgtReAddAfterCommit.Execute(cancel);
1232 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1233 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_EDIT)
1235 m_RebaseStage=REBASE_EDIT;
1236 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1237 this->UpdateCurrentStatus();
1238 return;
1240 else
1242 m_RebaseStage=REBASE_CONTINUE;
1243 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1244 this->UpdateCurrentStatus();
1246 if (CheckNextCommitIsSquash() == 0) // remember commit msg after edit if next commit if squash
1247 ResetParentForSquash(str);
1248 else
1249 m_SquashMessage.Empty();
1253 if ((m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_CONTINUE || m_bSplitCommit) && CheckNextCommitIsSquash() && (m_bSplitCommit || !g_Git.CheckCleanWorkTree(true)))
1255 if (!m_bSplitCommit && CMessageBox::Show(nullptr, IDS_PROC_REBASE_CONTINUE_NOTCLEAN, IDS_APPNAME, 1, IDI_ERROR, IDS_MSGBOX_OK, IDS_ABORTBUTTON) == 2)
1256 return;
1257 BOOL isFirst = TRUE;
1260 CCommitDlg dlg;
1261 if (isFirst)
1262 dlg.m_sLogMessage = m_LogMessageCtrl.GetText();
1263 dlg.m_bWholeProject = true;
1264 dlg.m_bSelectFilesForCommit = true;
1265 dlg.m_bCommitAmend = isFirst && (m_RebaseStage != REBASE_SQUASH_EDIT); // do not amend on squash_edit stage, we need a normal commit there
1266 CTGitPathList gpl;
1267 gpl.AddPath(CTGitPath(g_Git.m_CurrentDir));
1268 dlg.m_pathList = gpl;
1269 dlg.m_bAmendDiffToLastCommit = !m_bSplitCommit;
1270 dlg.m_bNoPostActions = true;
1271 if (dlg.m_bCommitAmend)
1272 dlg.m_AmendStr = dlg.m_sLogMessage;
1273 dlg.m_bWarnDetachedHead = false;
1275 if (dlg.DoModal() != IDOK)
1276 return;
1278 isFirst = !m_bSplitCommit; // only select amend on second+ runs if not in split commit mode
1280 m_SquashMessage.Empty();
1281 } while (!g_Git.CheckCleanWorkTree() || (m_bSplitCommit && CMessageBox::Show(GetSafeHwnd(), IDS_REBASE_ADDANOTHERCOMMIT, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) == IDYES));
1283 m_bSplitCommit = FALSE;
1284 UpdateData(FALSE);
1286 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1287 m_RebaseStage = REBASE_CONTINUE;
1288 GitRevLoglist* curRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1289 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1290 this->UpdateCurrentStatus();
1293 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
1295 CString str;
1296 GitRevLoglist* curRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1298 str=this->m_LogMessageCtrl.GetText();
1299 if(str.Trim().IsEmpty())
1301 CMessageBox::Show(NULL, IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1302 return;
1305 CString tempfile=::GetTempFile();
1306 if (CAppUtils::SaveCommitUnicodeFile(tempfile, str))
1308 CMessageBox::Show(nullptr, _T("Could not save commit message"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1309 return;
1312 CString out,cmd;
1314 if( m_RebaseStage == REBASE_SQUASH_EDIT )
1315 cmd.Format(_T("git.exe commit %s-F \"%s\""), m_SquashFirstMetaData, tempfile);
1316 else
1318 CString options;
1319 int isEmpty = IsCommitEmpty(curRev->m_CommitHash);
1320 if (isEmpty == 1)
1321 options = _T("--allow-empty ");
1322 else if (isEmpty < 0)
1323 return;
1324 cmd.Format(_T("git.exe commit --amend %s-F \"%s\""), options, tempfile);
1327 if(g_Git.Run(cmd,&out,CP_UTF8))
1329 if(!g_Git.CheckCleanWorkTree())
1331 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1332 return;
1336 ::DeleteFile(tempfile);
1337 AddLogString(out);
1338 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
1340 ResetParentForSquash(str);
1342 else
1343 m_SquashMessage.Empty();
1344 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1345 m_RebaseStage=REBASE_CONTINUE;
1346 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1347 this->UpdateCurrentStatus();
1351 InterlockedExchange(&m_bThreadRunning, TRUE);
1352 SetControlEnable();
1354 if (AfxBeginThread(RebaseThreadEntry, this)==NULL)
1356 InterlockedExchange(&m_bThreadRunning, FALSE);
1357 CMessageBox::Show(NULL, _T("Create Rebase Thread Fail"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1358 SetControlEnable();
1361 void CRebaseDlg::ResetParentForSquash(const CString& commitMessage)
1363 m_SquashMessage = commitMessage;
1364 // reset parent so that we can do "git cherry-pick --no-commit" w/o introducing an unwanted commit
1365 CString cmd = _T("git.exe reset --soft HEAD~1");
1366 m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1367 while (true)
1369 CString out;
1370 if (g_Git.Run(cmd, &out, CP_UTF8))
1372 AddLogString(cmd);
1373 AddLogString(CString(MAKEINTRESOURCE(IDS_FAIL)));
1374 AddLogString(out);
1375 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1376 return;
1378 else
1379 break;
1382 int CRebaseDlg::CheckNextCommitIsSquash()
1384 int index;
1385 if(m_CommitList.m_IsOldFirst)
1386 index=m_CurrentRebaseIndex+1;
1387 else
1388 index=m_CurrentRebaseIndex-1;
1390 GitRevLoglist* curRev;
1393 if(index<0)
1394 return -1;
1395 if(index>= m_CommitList.GetItemCount())
1396 return -1;
1398 curRev = (GitRevLoglist*)m_CommitList.m_arShownList[index];
1400 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1401 return 0;
1402 if (curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SKIP)
1404 if(m_CommitList.m_IsOldFirst)
1405 ++index;
1406 else
1407 --index;
1409 else
1410 return -1;
1412 } while(curRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_SKIP);
1414 return -1;
1417 int CRebaseDlg::GoNext()
1419 if(m_CommitList.m_IsOldFirst)
1420 ++m_CurrentRebaseIndex;
1421 else
1422 --m_CurrentRebaseIndex;
1423 return 0;
1426 int CRebaseDlg::StateAction()
1428 switch(this->m_RebaseStage)
1430 case CHOOSE_BRANCH:
1431 case CHOOSE_COMMIT_PICK_MODE:
1432 if(StartRebase())
1433 return -1;
1434 m_RebaseStage = REBASE_START;
1435 GoNext();
1436 break;
1439 return 0;
1441 void CRebaseDlg::SetContinueButtonText()
1443 CString Text;
1444 switch(this->m_RebaseStage)
1446 case CHOOSE_BRANCH:
1447 case CHOOSE_COMMIT_PICK_MODE:
1448 if(this->m_IsFastForward)
1449 Text.LoadString(IDS_PROC_STARTREBASEFFBUTTON);
1450 else
1451 Text.LoadString(IDS_PROC_STARTREBASEBUTTON);
1452 break;
1454 case REBASE_START:
1455 case REBASE_ERROR:
1456 case REBASE_CONTINUE:
1457 case REBASE_SQUASH_CONFLICT:
1458 Text.LoadString(IDS_CONTINUEBUTTON);
1459 break;
1461 case REBASE_CONFLICT:
1462 Text.LoadString(IDS_COMMITBUTTON);
1463 break;
1464 case REBASE_EDIT:
1465 Text.LoadString(IDS_AMENDBUTTON);
1466 break;
1468 case REBASE_SQUASH_EDIT:
1469 Text.LoadString(IDS_COMMITBUTTON);
1470 break;
1472 case REBASE_ABORT:
1473 case REBASE_FINISH:
1474 Text.LoadString(IDS_FINISHBUTTON);
1475 break;
1477 case REBASE_DONE:
1478 Text.LoadString(IDS_DONE);
1479 break;
1481 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1484 void CRebaseDlg::SetControlEnable()
1486 switch(this->m_RebaseStage)
1488 case CHOOSE_BRANCH:
1489 case CHOOSE_COMMIT_PICK_MODE:
1491 this->GetDlgItem(IDC_SPLITALLOPTIONS)->EnableWindow(TRUE);
1492 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(TRUE);
1493 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(TRUE);
1495 if(!m_IsCherryPick)
1497 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1498 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1499 this->GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(TRUE);
1500 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1502 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1503 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1504 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1505 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP);
1506 break;
1508 case REBASE_START:
1509 case REBASE_CONTINUE:
1510 case REBASE_ABORT:
1511 case REBASE_ERROR:
1512 case REBASE_FINISH:
1513 case REBASE_CONFLICT:
1514 case REBASE_EDIT:
1515 case REBASE_SQUASH_CONFLICT:
1516 case REBASE_DONE:
1517 this->GetDlgItem(IDC_SPLITALLOPTIONS)->EnableWindow(FALSE);
1518 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1519 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1520 this->GetDlgItem(IDC_BUTTON_REVERSE)->EnableWindow(FALSE);
1521 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1522 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(FALSE);
1523 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(FALSE);
1525 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1527 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1528 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1529 this->m_PostButton.RemoveAll();
1530 this->m_PostButton.AddEntries(m_PostButtonTexts);
1531 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1533 break;
1536 GetDlgItem(IDC_REBASE_SPLIT_COMMIT)->ShowWindow((m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT) ? SW_SHOW : SW_HIDE);
1538 if(m_bThreadRunning)
1540 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1543 else if (m_RebaseStage != REBASE_ERROR)
1545 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1549 void CRebaseDlg::UpdateProgress()
1551 int index;
1552 CRect rect;
1554 if(m_CommitList.m_IsOldFirst)
1555 index = m_CurrentRebaseIndex+1;
1556 else
1557 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1559 int finishedCommits = index - 1; // introduced an variable which shows the number handled revisions for the progress bars
1560 if (m_RebaseStage == REBASE_FINISH || finishedCommits == -1)
1561 finishedCommits = index;
1563 m_ProgressBar.SetRange32(0, m_CommitList.GetItemCount());
1564 m_ProgressBar.SetPos(finishedCommits);
1565 if (m_pTaskbarList)
1567 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1568 m_pTaskbarList->SetProgressValue(m_hWnd, finishedCommits, m_CommitList.GetItemCount());
1571 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1573 CString text;
1574 text.Format(IDS_PROC_REBASING_PROGRESS, index, m_CommitList.GetItemCount());
1575 m_sStatusText = text;
1576 m_CtrlStatusText.SetWindowText(text);
1577 m_bStatusWarning = false;
1578 m_CtrlStatusText.Invalidate();
1581 GitRevLoglist* prevRev = nullptr, *curRev = nullptr;
1583 if( m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex< m_CommitList.m_arShownList.GetSize())
1585 curRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1588 for (int i = 0; i < m_CommitList.m_arShownList.GetSize(); ++i)
1590 prevRev = (GitRevLoglist*)m_CommitList.m_arShownList[i];
1591 if (prevRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_CURRENT)
1593 prevRev->GetRebaseAction() &= ~CGitLogListBase::LOGACTIONS_REBASE_CURRENT;
1594 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1595 m_CommitList.InvalidateRect(rect);
1599 if(curRev)
1601 curRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_CURRENT;
1602 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1603 m_CommitList.InvalidateRect(rect);
1605 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1608 void CRebaseDlg::UpdateCurrentStatus()
1610 SetContinueButtonText();
1611 SetControlEnable();
1612 UpdateProgress();
1613 if (m_RebaseStage == REBASE_DONE)
1614 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
1617 void CRebaseDlg::AddLogString(CString str)
1619 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1620 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1621 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1622 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1623 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1624 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1627 int CRebaseDlg::GetCurrentCommitID()
1629 if(m_CommitList.m_IsOldFirst)
1631 return this->m_CurrentRebaseIndex+1;
1634 else
1636 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1640 int CRebaseDlg::IsCommitEmpty(const CGitHash& hash)
1642 CString cmd, tree, ptree;
1643 cmd.Format(_T("git.exe rev-parse -q --verify %s^{tree}"), hash.ToString());
1644 if (g_Git.Run(cmd, &tree, CP_UTF8))
1646 AddLogString(cmd);
1647 AddLogString(tree);
1648 return -1;
1650 cmd.Format(_T("git.exe rev-parse -q --verify %s^^{tree}"), hash.ToString());
1651 if (g_Git.Run(cmd, &ptree, CP_UTF8))
1652 ptree = _T("4b825dc642cb6eb9a060e54bf8d69288fbee4904"); // empty tree
1653 return tree == ptree;
1656 int CRebaseDlg::DoRebase()
1658 CString cmd,out;
1659 if(m_CurrentRebaseIndex <0)
1660 return 0;
1661 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1662 return 0;
1664 GitRevLoglist* pRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1665 int mode = pRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_MODE_MASK;
1666 CString nocommit;
1668 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SKIP)
1670 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1671 return 0;
1674 bool nextCommitIsSquash = (CheckNextCommitIsSquash() == 0);
1675 if (nextCommitIsSquash || mode != CGitLogListBase::LOGACTIONS_REBASE_PICK)
1676 { // next commit is squash or not pick
1677 if (!this->m_SquashMessage.IsEmpty())
1678 this->m_SquashMessage += _T("\n\n");
1679 this->m_SquashMessage += pRev->GetSubject();
1680 this->m_SquashMessage += _T("\n");
1681 this->m_SquashMessage += pRev->GetBody().TrimRight();
1682 if (m_bAddCherryPickedFrom)
1684 if (!pRev->GetBody().IsEmpty())
1685 m_SquashMessage += _T("\n");
1686 m_SquashMessage += _T("(cherry picked from commit ");
1687 m_SquashMessage += pRev->m_CommitHash.ToString();
1688 m_SquashMessage += _T(")");
1691 else
1693 this->m_SquashMessage.Empty();
1694 m_SquashFirstMetaData.Empty();
1697 if ((nextCommitIsSquash && mode != CGitLogListBase::LOGACTIONS_REBASE_EDIT) || mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1698 { // next or this commit is squash (don't do this on edit->squash sequence)
1699 nocommit=_T(" --no-commit ");
1702 if (nextCommitIsSquash && mode != CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1703 m_SquashFirstMetaData.Format(_T("--date=%s --author=\"%s <%s>\" "), pRev->GetAuthorDate().Format(_T("%Y-%m-%dT%H:%M:%S")), pRev->GetAuthorName(), pRev->GetAuthorEmail());
1705 CString log;
1706 log.Format(_T("%s %d: %s"), CGitLogListBase::GetRebaseActionName(mode), GetCurrentCommitID(), pRev->m_CommitHash.ToString());
1707 AddLogString(log);
1708 AddLogString(pRev->GetSubject());
1709 if (pRev->GetSubject().IsEmpty())
1711 CMessageBox::Show(m_hWnd, IDS_PROC_REBASE_EMPTYCOMMITMSG, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
1712 mode = CGitLogListBase::LOGACTIONS_REBASE_EDIT;
1715 CString cherryPickedFrom;
1716 if (m_bAddCherryPickedFrom)
1717 cherryPickedFrom = _T("-x ");
1718 else if (!m_IsCherryPick && nocommit.IsEmpty())
1719 cherryPickedFrom = _T("--ff "); // for issue #1833: "If the current HEAD is the same as the parent of the cherry-pick’ed commit, then a fast forward to this commit will be performed."
1721 int isEmpty = IsCommitEmpty(pRev->m_CommitHash);
1722 if (isEmpty == 1)
1723 cherryPickedFrom += _T("--allow-empty ");
1724 else if (isEmpty < 0)
1725 return -1;
1727 while (true)
1729 cmd.Format(_T("git.exe cherry-pick %s%s %s"), cherryPickedFrom, nocommit, pRev->m_CommitHash.ToString());
1731 if(g_Git.Run(cmd,&out,CP_UTF8))
1733 AddLogString(out);
1734 int hasConflicts = g_Git.HasWorkingTreeConflicts();
1735 if (hasConflicts < 0)
1737 AddLogString(g_Git.GetGitLastErr(L"Checking for conflicts failed.", CGit::GIT_CMD_CHECKCONFLICTS));
1738 return -1;
1740 if (!hasConflicts)
1742 if (mode == CGitLogListBase::LOGACTIONS_REBASE_PICK)
1744 if (m_pTaskbarList)
1745 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1746 int choose = -1;
1747 if (!m_bAutoSkipFailedCommit)
1749 choose = CMessageBox::ShowCheck(m_hWnd, IDS_CHERRYPICKFAILEDSKIP, IDS_APPNAME, 1, IDI_QUESTION, IDS_SKIPBUTTON, IDS_MSGBOX_RETRY, IDS_MSGBOX_CANCEL, NULL, IDS_DO_SAME_FOR_REST, &m_bAutoSkipFailedCommit);
1750 if (choose == 2)
1752 m_bAutoSkipFailedCommit = FALSE;
1753 continue; // retry cherry pick
1756 if (m_bAutoSkipFailedCommit || choose == 1)
1758 bool resetOK = false;
1759 while (!resetOK)
1761 out.Empty();
1762 if (g_Git.Run(_T("git.exe reset --hard"), &out, CP_UTF8))
1764 AddLogString(out);
1765 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1766 break;
1768 else
1769 resetOK = true;
1772 if (resetOK)
1774 pRev->GetRebaseAction() = CGitLogListBase::LOGACTIONS_REBASE_SKIP;
1775 m_CommitList.Invalidate();
1776 return 0;
1780 m_RebaseStage = REBASE_ERROR;
1781 AddLogString(_T("An unrecoverable error occurred."));
1782 return -1;
1784 if (mode == CGitLogListBase::LOGACTIONS_REBASE_EDIT)
1786 this->m_RebaseStage = REBASE_EDIT ;
1787 return -1; // Edit return -1 to stop rebase.
1789 // Squash Case
1790 if(CheckNextCommitIsSquash())
1791 { // no squash
1792 // let user edit last commmit message
1793 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1794 return -1;
1798 if (m_pTaskbarList)
1799 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1800 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1801 m_RebaseStage = REBASE_SQUASH_CONFLICT;
1802 else
1803 m_RebaseStage = REBASE_CONFLICT;
1804 return -1;
1807 else
1809 AddLogString(out);
1810 if (mode == CGitLogListBase::LOGACTIONS_REBASE_PICK)
1812 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1813 return 0;
1815 if (mode == CGitLogListBase::LOGACTIONS_REBASE_EDIT)
1817 this->m_RebaseStage = REBASE_EDIT ;
1818 return -1; // Edit return -1 to stop rebase.
1821 // Squash Case
1822 if(CheckNextCommitIsSquash())
1823 { // no squash
1824 // let user edit last commmit message
1825 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1826 return -1;
1828 else if (mode == CGitLogListBase::LOGACTIONS_REBASE_SQUASH)
1829 pRev->GetRebaseAction() |= CGitLogListBase::LOGACTIONS_REBASE_DONE;
1832 return 0;
1836 BOOL CRebaseDlg::IsEnd()
1838 if(m_CommitList.m_IsOldFirst)
1839 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
1840 else
1841 return m_CurrentRebaseIndex<0;
1844 int CRebaseDlg::RebaseThread()
1846 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
1848 int ret=0;
1849 while(1)
1851 if( m_RebaseStage == REBASE_START )
1853 if( this->StartRebase() )
1855 ret = -1;
1856 break;
1858 m_RebaseStage = REBASE_CONTINUE;
1861 else if( m_RebaseStage == REBASE_CONTINUE )
1863 this->GoNext();
1864 SendMessage(MSG_REBASE_UPDATE_UI);
1865 if(IsEnd())
1867 ret = 0;
1868 m_RebaseStage = REBASE_FINISH;
1871 else
1873 ret = DoRebase();
1875 if( ret )
1877 break;
1882 else if( m_RebaseStage == REBASE_FINISH )
1884 SendMessage(MSG_REBASE_UPDATE_UI);
1885 m_RebaseStage = REBASE_DONE;
1886 break;
1889 else
1891 break;
1893 this->PostMessage(MSG_REBASE_UPDATE_UI);
1896 InterlockedExchange(&m_bThreadRunning, FALSE);
1897 this->PostMessage(MSG_REBASE_UPDATE_UI);
1898 return ret;
1901 void CRebaseDlg::ListConflictFile()
1903 this->m_FileListCtrl.Clear();
1904 m_FileListCtrl.SetHasCheckboxes(true);
1905 CTGitPathList list;
1906 CTGitPath path;
1907 list.AddPath(path);
1909 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
1911 this->m_FileListCtrl.GetStatus(&list,true);
1912 this->m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED|CTGitPath::LOGACTIONS_MODIFIED|CTGitPath::LOGACTIONS_ADDED|CTGitPath::LOGACTIONS_DELETED,
1913 CTGitPath::LOGACTIONS_UNMERGED);
1915 m_FileListCtrl.Check(GITSLC_SHOWFILES);
1916 bool hasSubmoduleChange = false;
1917 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
1919 CTGitPath *entry = (CTGitPath *)m_FileListCtrl.GetItemData(i);
1920 if (entry->IsDirectory())
1922 hasSubmoduleChange = true;
1923 break;
1927 if (hasSubmoduleChange)
1929 m_CtrlStatusText.SetWindowText(m_sStatusText + _T(", ") + CString(MAKEINTRESOURCE(IDS_CARE_SUBMODULE_CHANGES)));
1930 m_bStatusWarning = true;
1931 m_CtrlStatusText.Invalidate();
1933 else
1935 m_CtrlStatusText.SetWindowText(m_sStatusText);
1936 m_bStatusWarning = false;
1937 m_CtrlStatusText.Invalidate();
1941 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
1943 if (m_RebaseStage == REBASE_FINISH)
1945 FinishRebase();
1946 return 0;
1948 UpdateCurrentStatus();
1949 if (m_RebaseStage == REBASE_DONE && m_pTaskbarList)
1950 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS); // do not show progress on taskbar any more to show we finished
1951 if(m_CurrentRebaseIndex <0)
1952 return 0;
1953 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1954 return 0;
1955 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1957 switch(m_RebaseStage)
1959 case REBASE_CONFLICT:
1960 case REBASE_SQUASH_CONFLICT:
1962 ListConflictFile();
1963 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
1964 if (m_pTaskbarList)
1965 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1966 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1967 CString logMessage;
1968 if (m_IsCherryPick)
1970 CString dotGitPath;
1971 GitAdminDir::GetAdminDirPath(g_Git.m_CurrentDir, dotGitPath);
1972 // vanilla git also re-uses MERGE_MSG on conflict (listing all conflicted files)
1973 // and it's also needed for cherry-pick in order to get cherry-picked-from included on conflicts
1974 CGit::LoadTextFile(dotGitPath + _T("MERGE_MSG"), logMessage);
1976 if (logMessage.IsEmpty())
1977 logMessage = curRev->GetSubject() + _T("\n") + curRev->GetBody();
1978 this->m_LogMessageCtrl.SetText(logMessage);
1979 break;
1981 case REBASE_EDIT:
1982 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1983 if (m_pTaskbarList)
1984 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1985 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1986 if (m_bAddCherryPickedFrom)
1988 // Since the new commit is done and the HEAD points to it,
1989 // just using the new body modified by git self.
1990 GitRev headRevision;
1991 if (headRevision.GetCommit(_T("HEAD")))
1992 MessageBox(headRevision.GetLastErr(), _T("TortoiseGit"), MB_ICONERROR);
1994 m_LogMessageCtrl.SetText(headRevision.GetSubject() + _T("\n") + headRevision.GetBody());
1996 else
1997 m_LogMessageCtrl.SetText(curRev->GetSubject() + _T("\n") + curRev->GetBody());
1998 break;
1999 case REBASE_SQUASH_EDIT:
2000 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
2001 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2002 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
2003 if (m_pTaskbarList)
2004 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
2005 break;
2006 default:
2007 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
2009 return 0;
2011 void CRebaseDlg::OnCancel()
2013 OnBnClickedAbort();
2015 void CRebaseDlg::OnBnClickedAbort()
2017 if (m_pTaskbarList)
2018 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
2020 m_tooltips.Pop();
2022 CString cmd,out;
2023 if(m_OrigUpstreamHash.IsEmpty())
2025 __super::OnCancel();
2028 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
2030 goto end;
2033 if(CMessageBox::Show(NULL, IDS_PROC_REBASE_ABORT, IDS_APPNAME, MB_YESNO) != IDYES)
2034 goto end;
2036 if(this->m_IsFastForward)
2038 cmd.Format(_T("git.exe reset --hard %s --"),this->m_OrigBranchHash.ToString());
2039 while (true)
2041 out.Empty();
2042 if (g_Git.Run(cmd, &out, CP_UTF8))
2044 AddLogString(out);
2045 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
2046 break;
2048 else
2049 break;
2051 __super::OnCancel();
2052 goto end;
2055 if (m_IsCherryPick) // there are not "branch" at cherry pick mode
2057 cmd.Format(_T("git.exe reset --hard %s --"), m_OrigUpstreamHash.ToString());
2058 while (true)
2060 out.Empty();
2061 if (g_Git.Run(cmd, &out, CP_UTF8))
2063 AddLogString(out);
2064 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
2065 break;
2067 else
2068 break;
2071 __super::OnCancel();
2072 goto end;
2075 if (m_OrigHEADBranch == m_BranchCtrl.GetString())
2077 if (IsLocalBranch(m_OrigHEADBranch))
2078 cmd.Format(_T("git.exe checkout -f -B %s %s --"), m_BranchCtrl.GetString(), m_OrigBranchHash.ToString());
2079 else
2080 cmd.Format(_T("git.exe checkout -f %s --"), m_OrigBranchHash.ToString());
2081 if (g_Git.Run(cmd, &out, CP_UTF8))
2083 AddLogString(out);
2084 ::MessageBox(m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
2085 __super::OnCancel();
2086 goto end;
2089 cmd.Format(_T("git.exe reset --hard %s --"), m_OrigBranchHash.ToString());
2090 while (true)
2092 out.Empty();
2093 if (g_Git.Run(cmd, &out, CP_UTF8))
2095 AddLogString(out);
2096 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
2097 break;
2099 else
2100 break;
2103 else
2105 if (m_OrigHEADBranch != g_Git.GetCurrentBranch(true))
2107 if (IsLocalBranch(m_OrigHEADBranch))
2108 cmd.Format(_T("git.exe checkout -f -B %s %s --"), m_OrigHEADBranch, m_OrigHEADHash.ToString());
2109 else
2110 cmd.Format(_T("git.exe checkout -f %s --"), m_OrigHEADHash.ToString());
2111 if (g_Git.Run(cmd, &out, CP_UTF8))
2113 AddLogString(out);
2114 ::MessageBox(m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
2115 // continue to restore moved branch
2119 cmd.Format(_T("git.exe reset --hard %s --"), m_OrigHEADHash.ToString());
2120 while (true)
2122 out.Empty();
2123 if (g_Git.Run(cmd, &out, CP_UTF8))
2125 AddLogString(out);
2126 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
2127 break;
2129 else
2130 break;
2133 // restore moved branch
2134 if (IsLocalBranch(m_BranchCtrl.GetString()))
2136 cmd.Format(_T("git.exe branch -f %s %s --"), m_BranchCtrl.GetString(), m_OrigBranchHash.ToString());
2137 if (g_Git.Run(cmd, &out, CP_UTF8))
2139 AddLogString(out);
2140 ::MessageBox(m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
2141 __super::OnCancel();
2142 goto end;
2146 __super::OnCancel();
2147 end:
2148 CheckRestoreStash();
2151 void CRebaseDlg::OnBnClickedButtonReverse()
2153 CString temp = m_BranchCtrl.GetString();
2154 m_BranchCtrl.AddString(m_UpstreamCtrl.GetString());
2155 m_UpstreamCtrl.AddString(temp);
2156 OnCbnSelchangeUpstream();
2159 void CRebaseDlg::OnBnClickedButtonBrowse()
2161 if(CBrowseRefsDlg::PickRefForCombo(&m_UpstreamCtrl))
2162 OnCbnSelchangeUpstream();
2165 void CRebaseDlg::OnBnClickedRebaseCheckForce()
2167 this->UpdateData();
2168 this->FetchLogList();
2171 void CRebaseDlg::OnBnClickedRebasePostButton()
2173 this->m_Upstream=this->m_UpstreamCtrl.GetString();
2174 this->m_Branch=this->m_BranchCtrl.GetString();
2176 this->EndDialog((int)(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry()));
2179 LRESULT CRebaseDlg::OnGitStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
2181 Refresh();
2182 return 0;
2185 void CRebaseDlg::Refresh()
2187 if (m_RebaseStage == REBASE_CONFLICT || m_RebaseStage == REBASE_SQUASH_CONFLICT)
2189 ListConflictFile();
2190 return;
2193 if(this->m_IsCherryPick)
2194 return ;
2196 if(this->m_RebaseStage == CHOOSE_BRANCH )
2198 this->UpdateData();
2199 this->FetchLogList();
2203 void CRebaseDlg::OnBnClickedButtonUp2()
2205 POSITION pos;
2206 pos = m_CommitList.GetFirstSelectedItemPosition();
2208 // do nothing if the first selected item is the first item in the list
2209 int idx = m_CommitList.GetNextSelectedItem(pos);
2210 if (idx == 0)
2211 return;
2213 bool moveToTop = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
2214 int move = moveToTop ? idx : 1;
2215 pos = m_CommitList.GetFirstSelectedItemPosition();
2217 bool changed = false;
2218 while(pos)
2220 int index=m_CommitList.GetNextSelectedItem(pos);
2221 if(index>=1)
2223 CGitHash old = m_CommitList.m_logEntries[index - move];
2224 m_CommitList.m_logEntries[index - move] = m_CommitList.m_logEntries[index];
2225 m_CommitList.m_logEntries[index] = old;
2226 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2227 m_CommitList.SetItemState(index - move, LVIS_SELECTED, LVIS_SELECTED);
2228 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
2229 changed = true;
2232 if (changed)
2234 pos = m_CommitList.GetFirstSelectedItemPosition();
2235 m_CommitList.EnsureVisible(m_CommitList.GetNextSelectedItem(pos), false);
2236 m_CommitList.Invalidate();
2237 m_CommitList.SetFocus();
2241 void CRebaseDlg::OnBnClickedButtonDown2()
2243 if (m_CommitList.GetSelectedCount() == 0)
2244 return;
2246 bool moveToBottom = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
2247 POSITION pos;
2248 pos = m_CommitList.GetFirstSelectedItemPosition();
2249 bool changed = false;
2250 // use an array to store all selected item indexes; the user won't select too much items
2251 int* indexes = NULL;
2252 indexes = new int[m_CommitList.GetSelectedCount()];
2253 int i = 0;
2254 while(pos)
2256 indexes[i++] = m_CommitList.GetNextSelectedItem(pos);
2258 int distanceToBottom = m_CommitList.GetItemCount() - 1 - indexes[m_CommitList.GetSelectedCount() - 1];
2259 int move = moveToBottom ? distanceToBottom : 1;
2260 // don't move any item if the last selected item is the last item in the m_CommitList
2261 // (that would change the order of the selected items)
2262 if (distanceToBottom > 0)
2264 // iterate over the indexes backwards in order to correctly move multiselected items
2265 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
2267 int index = indexes[i];
2268 CGitHash old = m_CommitList.m_logEntries[index + move];
2269 m_CommitList.m_logEntries[index + move] = m_CommitList.m_logEntries[index];
2270 m_CommitList.m_logEntries[index] = old;
2271 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
2272 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
2273 m_CommitList.SetItemState(index + move, LVIS_SELECTED, LVIS_SELECTED);
2274 changed = true;
2277 m_CommitList.EnsureVisible(indexes[m_CommitList.GetSelectedCount() - 1] + move, false);
2278 delete [] indexes;
2279 indexes = NULL;
2280 if (changed)
2282 m_CommitList.Invalidate();
2283 m_CommitList.SetFocus();
2287 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
2289 m_pTaskbarList.Release();
2290 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
2291 SetUUIDOverlayIcon(m_hWnd);
2292 return 0;
2295 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
2297 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
2298 *pResult = 0;
2299 if(m_CommitList.m_bNoDispUpdates)
2300 return;
2301 if (pNMLV->iItem >= 0)
2303 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
2304 if (pNMLV->iSubItem != 0)
2305 return;
2306 if ((pNMLV->iItem == m_CommitList.m_arShownList.GetCount()))
2308 // remove the selected state
2309 if (pNMLV->uChanged & LVIF_STATE)
2311 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
2312 FillLogMessageCtrl();
2314 return;
2316 if (pNMLV->uChanged & LVIF_STATE)
2318 FillLogMessageCtrl();
2321 else
2323 FillLogMessageCtrl();
2327 void CRebaseDlg::FillLogMessageCtrl()
2329 int selCount = m_CommitList.GetSelectedCount();
2330 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
2332 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
2333 int selIndex = m_CommitList.GetNextSelectedItem(pos);
2334 GitRevLoglist* pLogEntry = reinterpret_cast<GitRevLoglist*>(m_CommitList.m_arShownList.SafeGetAt(selIndex));
2335 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
2336 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
2337 m_FileListCtrl.Show(GITSLC_SHOWVERSIONED);
2338 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2339 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + _T("\n") + pLogEntry->GetBody());
2340 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
2343 void CRebaseDlg::OnBnClickedCheckCherryPickedFrom()
2345 UpdateData();
2348 LRESULT CRebaseDlg::OnRebaseActionMessage(WPARAM, LPARAM)
2350 if (m_RebaseStage == REBASE_ERROR || m_RebaseStage == REBASE_CONFLICT)
2352 GitRevLoglist* pRev = (GitRevLoglist*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
2353 int mode = pRev->GetRebaseAction() & CGitLogListBase::LOGACTIONS_REBASE_MODE_MASK;
2354 if (mode == CGitLogListBase::LOGACTIONS_REBASE_SKIP)
2356 CString out;
2357 bool resetOK = false;
2358 while (!resetOK)
2360 out.Empty();
2361 if (g_Git.Run(_T("git.exe reset --hard"), &out, CP_UTF8))
2363 AddLogString(out);
2364 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
2365 break;
2367 else
2368 resetOK = true;
2371 if (resetOK)
2373 m_FileListCtrl.Clear();
2374 m_RebaseStage = REBASE_CONTINUE;
2375 UpdateCurrentStatus();
2379 return 0;
2383 void CRebaseDlg::OnBnClickedSplitAllOptions()
2385 switch (m_SplitAllOptions.GetCurrentEntry())
2387 case 0:
2388 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_PICK);
2389 break;
2390 case 1:
2391 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_SQUASH);
2392 break;
2393 case 2:
2394 SetAllRebaseAction(CGitLogListBase::LOGACTIONS_REBASE_EDIT);
2395 break;
2396 default:
2397 ATLASSERT(false);
2401 void CRebaseDlg::OnBnClickedRebaseSplitCommit()
2403 UpdateData();
2406 static bool GetCompareHash(const CString& ref, const CGitHash& hash)
2408 CGitHash refHash;
2409 if (g_Git.GetHash(refHash, ref))
2410 MessageBox(nullptr, g_Git.GetGitLastErr(_T("Could not get hash of \"") + ref + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
2411 return refHash.IsEmpty() || (hash == refHash);
2414 void CRebaseDlg::OnBnClickedButtonOnto()
2416 m_Onto = CBrowseRefsDlg::PickRef(false, m_Onto);
2417 if (!m_Onto.IsEmpty())
2419 // make sure that the user did not select upstream, selected branch or HEAD
2420 CGitHash hash;
2421 if (g_Git.GetHash(hash, m_Onto))
2423 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
2424 m_Onto.Empty();
2425 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
2426 return;
2428 if (GetCompareHash(_T("HEAD"), hash) || GetCompareHash(m_UpstreamCtrl.GetString(), hash) || GetCompareHash(m_BranchCtrl.GetString(), hash))
2429 m_Onto.Empty();
2431 if (m_Onto.IsEmpty())
2432 m_tooltips.DelTool(IDC_BUTTON_ONTO);
2433 else
2434 m_tooltips.AddTool(IDC_BUTTON_ONTO, m_Onto);
2435 ((CButton*)GetDlgItem(IDC_BUTTON_ONTO))->SetCheck(m_Onto.IsEmpty() ? BST_UNCHECKED : BST_CHECKED);
2436 FetchLogList();