Log: Support arbitrary revisions
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
blobe54dffad3dc495dd8c00f928e20335c9ad5f0420
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
4 // Copyright (C) 2011-2013 - Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // RebaseDlg.cpp : implementation file
24 #include "stdafx.h"
25 #include "TortoiseProc.h"
26 #include "RebaseDlg.h"
27 #include "AppUtils.h"
28 #include "LoglistUtils.h"
29 #include "MessageBox.h"
30 #include "UnicodeUtils.h"
31 #include "BrowseRefsDlg.h"
32 #include "ProgressDlg.h"
33 #include "SmartHandle.h"
34 #include "../TGitCache/CacheInterface.h"
35 #include "Settings\Settings.h"
36 #include "MassiveGitTask.h"
38 // CRebaseDlg dialog
40 IMPLEMENT_DYNAMIC(CRebaseDlg, CResizableStandAloneDialog)
42 CRebaseDlg::CRebaseDlg(CWnd* pParent /*=NULL*/)
43 : CResizableStandAloneDialog(CRebaseDlg::IDD, pParent)
44 , m_bPickAll(FALSE)
45 , m_bSquashAll(FALSE)
46 , m_bEditAll(FALSE)
47 , m_bAddCherryPickedFrom(FALSE)
48 , m_bStatusWarning(false)
50 m_RebaseStage=CHOOSE_BRANCH;
51 m_CurrentRebaseIndex=-1;
52 m_bThreadRunning =FALSE;
53 this->m_IsCherryPick = FALSE;
54 m_bForce=FALSE;
55 m_IsFastForward=FALSE;
58 CRebaseDlg::~CRebaseDlg()
62 void CRebaseDlg::DoDataExchange(CDataExchange* pDX)
64 CDialog::DoDataExchange(pDX);
65 DDX_Control(pDX, IDC_REBASE_PROGRESS, m_ProgressBar);
66 DDX_Control(pDX, IDC_STATUS_STATIC, m_CtrlStatusText);
67 DDX_Check(pDX, IDC_PICK_ALL, m_bPickAll);
68 DDX_Check(pDX, IDC_SQUASH_ALL, m_bSquashAll);
69 DDX_Check(pDX, IDC_EDIT_ALL, m_bEditAll);
70 DDX_Control(pDX, IDC_REBASE_SPLIT, m_wndSplitter);
71 DDX_Control(pDX,IDC_COMMIT_LIST,m_CommitList);
72 DDX_Control(pDX,IDC_REBASE_COMBOXEX_BRANCH, this->m_BranchCtrl);
73 DDX_Control(pDX,IDC_REBASE_COMBOXEX_UPSTREAM, this->m_UpstreamCtrl);
74 DDX_Check(pDX, IDC_REBASE_CHECK_FORCE,m_bForce);
75 DDX_Check(pDX, IDC_CHECK_CHERRYPICKED_FROM, m_bAddCherryPickedFrom);
76 DDX_Control(pDX,IDC_REBASE_POST_BUTTON,m_PostButton);
80 BEGIN_MESSAGE_MAP(CRebaseDlg, CResizableStandAloneDialog)
81 ON_BN_CLICKED(IDC_PICK_ALL, &CRebaseDlg::OnBnClickedPickAll)
82 ON_BN_CLICKED(IDC_SQUASH_ALL, &CRebaseDlg::OnBnClickedSquashAll)
83 ON_BN_CLICKED(IDC_EDIT_ALL, &CRebaseDlg::OnBnClickedEditAll)
84 ON_BN_CLICKED(IDC_REBASE_SPLIT, &CRebaseDlg::OnBnClickedRebaseSplit)
85 ON_BN_CLICKED(IDC_REBASE_CONTINUE,OnBnClickedContinue)
86 ON_BN_CLICKED(IDC_REBASE_ABORT, OnBnClickedAbort)
87 ON_WM_SIZE()
88 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_BRANCH, &CRebaseDlg::OnCbnSelchangeBranch)
89 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_UPSTREAM, &CRebaseDlg::OnCbnSelchangeUpstream)
90 ON_MESSAGE(MSG_REBASE_UPDATE_UI, OnRebaseUpdateUI)
91 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnGitStatusListCtrlNeedsRefresh)
92 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CRebaseDlg::OnBnClickedButtonBrowse)
93 ON_BN_CLICKED(IDC_REBASE_CHECK_FORCE, &CRebaseDlg::OnBnClickedRebaseCheckForce)
94 ON_BN_CLICKED(IDC_CHECK_CHERRYPICKED_FROM, &CRebaseDlg::OnBnClickedCheckCherryPickedFrom)
95 ON_BN_CLICKED(IDC_REBASE_POST_BUTTON, &CRebaseDlg::OnBnClickedRebasePostButton)
96 ON_BN_CLICKED(IDC_BUTTON_UP2, &CRebaseDlg::OnBnClickedButtonUp2)
97 ON_BN_CLICKED(IDC_BUTTON_DOWN2, &CRebaseDlg::OnBnClickedButtonDown2)
98 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
99 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COMMIT_LIST, OnLvnItemchangedLoglist)
100 ON_WM_CTLCOLOR()
101 END_MESSAGE_MAP()
103 void CRebaseDlg::AddRebaseAnchor()
105 AddAnchor(IDC_REBASE_TAB,TOP_LEFT,BOTTOM_RIGHT);
106 AddAnchor(IDC_COMMIT_LIST,TOP_LEFT, TOP_RIGHT);
107 AddAnchor(IDC_REBASE_SPLIT,TOP_LEFT, TOP_RIGHT);
108 AddAnchor(IDC_STATUS_STATIC, BOTTOM_LEFT,BOTTOM_RIGHT);
109 AddAnchor(IDC_REBASE_CONTINUE,BOTTOM_RIGHT);
110 AddAnchor(IDC_REBASE_ABORT, BOTTOM_RIGHT);
111 AddAnchor(IDC_REBASE_PROGRESS,BOTTOM_LEFT, BOTTOM_RIGHT);
112 AddAnchor(IDC_PICK_ALL,TOP_LEFT);
113 AddAnchor(IDC_SQUASH_ALL,TOP_LEFT);
114 AddAnchor(IDC_EDIT_ALL,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_POST_BUTTON,BOTTOM_LEFT);
126 this->AddOthersToAnchor();
129 BOOL CRebaseDlg::OnInitDialog()
131 CResizableStandAloneDialog::OnInitDialog();
132 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
134 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
135 // do this, Explorer would be unable to send that message to our window if we
136 // were running elevated. It's OK to make the call all the time, since if we're
137 // not elevated, this is a no-op.
138 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
139 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
140 CAutoLibrary hUser = AtlLoadSystemLibraryUsingFullPath(_T("user32.dll"));
141 if (hUser)
143 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
144 if (pfnChangeWindowMessageFilterEx)
146 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
149 m_pTaskbarList.Release();
150 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
151 m_pTaskbarList = nullptr;
153 CRect rectDummy;
154 //IDC_REBASE_DUMY_TAB
156 GetClientRect(m_DlgOrigRect);
157 m_CommitList.GetClientRect(m_CommitListOrigRect);
159 CWnd *pwnd=this->GetDlgItem(IDC_REBASE_DUMY_TAB);
160 pwnd->GetWindowRect(&rectDummy);
161 this->ScreenToClient(rectDummy);
163 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_REBASE_TAB))
165 TRACE0("Failed to create output tab window\n");
166 return FALSE; // fail to create
168 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
169 // Create output panes:
170 //const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
171 DWORD dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
173 if (! this->m_FileListCtrl.Create(dwStyle,rectDummy,&this->m_ctrlTabCtrl,0) )
175 TRACE0("Failed to create output windows\n");
176 return FALSE; // fail to create
178 m_FileListCtrl.m_hwndLogicalParent = this;
180 if( ! this->m_LogMessageCtrl.Create(_T("Scintilla"),_T("source"),0,rectDummy,&m_ctrlTabCtrl,0,0) )
182 TRACE0("Failed to create log message control");
183 return FALSE;
185 m_LogMessageCtrl.Init(0);
186 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
188 dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
190 if (!m_wndOutputRebase.Create(_T("Scintilla"),_T("source"),0,rectDummy, &m_ctrlTabCtrl, 0,0) )
192 TRACE0("Failed to create output windows\n");
193 return -1; // fail to create
195 m_wndOutputRebase.Init(0);
196 m_wndOutputRebase.Call(SCI_SETREADONLY, TRUE);
198 m_tooltips.Create(this);
200 m_tooltips.AddTool(IDC_REBASE_CHECK_FORCE,IDS_REBASE_FORCE_TT);
201 m_tooltips.AddTool(IDC_REBASE_ABORT,IDS_REBASE_ABORT_TT);
205 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);
207 m_ctrlTabCtrl.AddTab(&m_FileListCtrl, CString(MAKEINTRESOURCE(IDS_PROC_REVISIONFILES)));
208 m_ctrlTabCtrl.AddTab(&m_LogMessageCtrl, CString(MAKEINTRESOURCE(IDS_PROC_COMMITMESSAGE)), 1);
209 AddRebaseAnchor();
211 AdjustControlSize(IDC_PICK_ALL);
212 AdjustControlSize(IDC_SQUASH_ALL);
213 AdjustControlSize(IDC_EDIT_ALL);
214 AdjustControlSize(IDC_CHECK_CHERRYPICKED_FROM);
216 CString sWindowTitle;
217 GetWindowText(sWindowTitle);
218 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
220 EnableSaveRestore(_T("RebaseDlg"));
222 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
223 RECT rcDlg, rcLogMsg, rcFileList;
224 GetClientRect(&rcDlg);
225 m_CommitList.GetWindowRect(&rcLogMsg);
226 ScreenToClient(&rcLogMsg);
227 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
228 ScreenToClient(&rcFileList);
229 if (yPos)
231 RECT rectSplitter;
232 m_wndSplitter.GetWindowRect(&rectSplitter);
233 ScreenToClient(&rectSplitter);
234 int delta = yPos - rectSplitter.top;
235 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
237 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
238 DoSize(delta);
242 if( this->m_RebaseStage == CHOOSE_BRANCH)
244 this->LoadBranchInfo();
247 else
249 this->m_BranchCtrl.EnableWindow(FALSE);
250 this->m_UpstreamCtrl.EnableWindow(FALSE);
253 m_CommitList.m_ColumnRegKey = _T("Rebase");
254 m_CommitList.m_IsIDReplaceAction = TRUE;
255 // m_CommitList.m_IsOldFirst = TRUE;
256 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
257 m_CommitList.m_bNoHightlightHead = TRUE;
259 m_CommitList.InsertGitColumn();
261 this->SetControlEnable();
263 if(!this->m_PreCmd.IsEmpty())
265 CProgressDlg progress;
266 progress.m_GitCmd=m_PreCmd;
267 progress.m_bAutoCloseOnSuccess=true;
268 progress.DoModal();
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 this->m_UpstreamCtrl.AddString(_T("HEAD"));
278 this->m_UpstreamCtrl.EnableWindow(FALSE);
279 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_CHERRYPICK)));
280 this->m_CommitList.StartFilter();
282 else
284 GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM)->ShowWindow(SW_HIDE);
285 SetContinueButtonText();
286 m_CommitList.DeleteAllItems();
287 FetchLogList();
290 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
291 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
292 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
293 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
294 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
295 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
296 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
298 if(m_CommitList.m_IsOldFirst)
299 this->m_CurrentRebaseIndex = -1;
300 else
301 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
304 if(this->CheckRebaseCondition())
306 /* Disable Start Rebase */
307 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
310 return TRUE;
312 // CRebaseDlg message handlers
314 HBRUSH CRebaseDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
316 if (pWnd->GetDlgCtrlID() == IDC_STATUS_STATIC && nCtlColor == CTLCOLOR_STATIC && m_bStatusWarning)
318 pDC->SetBkColor(RGB(255, 0, 0));
319 pDC->SetTextColor(RGB(255, 255, 255));
320 return CreateSolidBrush(RGB(255, 0, 0));
323 return CResizableStandAloneDialog::OnCtlColor(pDC, pWnd, nCtlColor);
326 void CRebaseDlg::OnBnClickedPickAll()
328 this->UpdateData();
329 if(this->m_bPickAll)
330 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_PICK);
332 this->m_bEditAll=FALSE;
333 this->m_bSquashAll=FALSE;
334 this->UpdateData(FALSE);
337 void CRebaseDlg::OnBnClickedSquashAll()
339 this->UpdateData();
340 if(this->m_bSquashAll)
341 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
343 this->m_bEditAll=FALSE;
344 this->m_bPickAll=FALSE;
345 this->UpdateData(FALSE);
348 void CRebaseDlg::OnBnClickedEditAll()
350 this->UpdateData();
351 if( this->m_bEditAll )
352 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
354 this->m_bPickAll=FALSE;
355 this->m_bSquashAll=FALSE;
356 this->UpdateData(FALSE);
359 void CRebaseDlg::SetAllRebaseAction(int action)
361 for (int i = 0; i < this->m_CommitList.m_logEntries.size(); ++i)
363 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList)=action;
365 m_CommitList.Invalidate();
368 void CRebaseDlg::OnBnClickedRebaseSplit()
370 this->UpdateData();
373 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
375 switch (message) {
376 case WM_NOTIFY:
377 if (wParam == IDC_REBASE_SPLIT)
379 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
380 DoSize(pHdr->delta);
382 break;
385 return __super::DefWindowProc(message, wParam, lParam);
388 void CRebaseDlg::DoSize(int delta)
390 this->RemoveAllAnchors();
392 CSplitterControl::ChangeHeight(GetDlgItem(IDC_COMMIT_LIST), delta, CW_TOPALIGN);
393 //CSplitterControl::ChangeHeight(GetDlgItem(), delta, CW_TOPALIGN);
394 CSplitterControl::ChangeHeight(GetDlgItem(IDC_REBASE_TAB), -delta, CW_BOTTOMALIGN);
395 //CSplitterControl::ChangeHeight(GetDlgItem(), -delta, CW_BOTTOMALIGN);
396 CSplitterControl::ChangePos(GetDlgItem(IDC_SQUASH_ALL),0,delta);
397 CSplitterControl::ChangePos(GetDlgItem(IDC_PICK_ALL),0,delta);
398 CSplitterControl::ChangePos(GetDlgItem(IDC_EDIT_ALL),0,delta);
399 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_UP2),0,delta);
400 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_DOWN2),0,delta);
401 CSplitterControl::ChangePos(GetDlgItem(IDC_REBASE_CHECK_FORCE),0,delta);
402 CSplitterControl::ChangePos(GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM), 0, delta);
404 this->AddRebaseAnchor();
405 // adjust the minimum size of the dialog to prevent the resizing from
406 // moving the list control too far down.
407 CRect rcLogMsg;
408 m_CommitList.GetClientRect(rcLogMsg);
409 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
411 SetSplitterRange();
412 // m_CommitList.Invalidate();
414 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
416 this->m_ctrlTabCtrl.Invalidate();
417 this->m_CommitList.Invalidate();
418 this->m_FileListCtrl.Invalidate();
419 this->m_LogMessageCtrl.Invalidate();
423 void CRebaseDlg::SetSplitterRange()
425 if ((m_CommitList)&&(m_ctrlTabCtrl))
427 CRect rcTop;
428 m_CommitList.GetWindowRect(rcTop);
429 ScreenToClient(rcTop);
430 CRect rcMiddle;
431 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
432 ScreenToClient(rcMiddle);
433 if (rcMiddle.Height() && rcMiddle.Width())
434 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
438 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
440 // first, let the resizing take place
441 __super::OnSize(nType, cx, cy);
443 //set range
444 SetSplitterRange();
447 void CRebaseDlg::SaveSplitterPos()
449 if (!IsIconic())
451 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
452 RECT rectSplitter;
453 m_wndSplitter.GetWindowRect(&rectSplitter);
454 ScreenToClient(&rectSplitter);
455 regPos = rectSplitter.top;
459 void CRebaseDlg::LoadBranchInfo()
461 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
462 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
464 STRING_VECTOR list;
465 list.clear();
466 int current;
467 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
468 m_BranchCtrl.AddString(list);
469 m_BranchCtrl.SetCurSel(current);
470 list.clear();
471 g_Git.GetBranchList(list, NULL, CGit::BRANCH_ALL_F);
472 m_UpstreamCtrl.AddString(list);
474 AddBranchToolTips(&m_BranchCtrl);
475 AddBranchToolTips(&m_UpstreamCtrl);
477 if(!m_Upstream.IsEmpty())
479 m_UpstreamCtrl.AddString(m_Upstream);
480 m_UpstreamCtrl.SetCurSel(m_UpstreamCtrl.GetCount()-1);
482 else
484 //Select pull-remote from current branch
485 CString currentBranch = g_Git.GetSymbolicRef();
486 CString configName;
487 configName.Format(L"branch.%s.remote", currentBranch);
488 CString pullRemote = g_Git.GetConfigValue(configName);
490 //Select pull-branch from current branch
491 configName.Format(L"branch.%s.merge", currentBranch);
492 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
494 CString defaultUpstream;
495 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
496 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
497 if(found >= 0)
498 m_UpstreamCtrl.SetCurSel(found);
499 else
500 m_UpstreamCtrl.SetCurSel(-1);
504 void CRebaseDlg::OnCbnSelchangeBranch()
506 FetchLogList();
509 void CRebaseDlg::OnCbnSelchangeUpstream()
511 FetchLogList();
514 void CRebaseDlg::FetchLogList()
516 CGitHash base,hash,upstream;
517 m_IsFastForward=FALSE;
519 if (g_Git.GetHash(hash, m_BranchCtrl.GetString()))
521 m_CommitList.ShowText(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")));
522 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
523 return;
525 if (g_Git.GetHash(upstream, m_UpstreamCtrl.GetString()))
527 m_CommitList.ShowText(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_UpstreamCtrl.GetString() + _T("\".")));
528 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
529 return;
532 if (hash == upstream)
534 m_CommitList.Clear();
535 CString text,fmt;
536 fmt.LoadString(IDS_REBASE_EQUAL_FMT);
537 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
539 m_CommitList.ShowText(text);
540 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
541 return;
544 if (g_Git.IsFastForward(m_BranchCtrl.GetString(), m_UpstreamCtrl.GetString(), &base))
546 //fast forword
547 this->m_IsFastForward=TRUE;
549 m_CommitList.Clear();
550 CString text,fmt;
551 fmt.LoadString(IDS_REBASE_FASTFORWARD_FMT);
552 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString(),
553 m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
555 m_CommitList.ShowText(text);
556 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
557 SetContinueButtonText();
559 return ;
562 if(!this->m_bForce)
564 if (base == upstream)
566 m_CommitList.Clear();
567 CString text,fmt;
568 fmt.LoadString(IDS_REBASE_UPTODATE_FMT);
569 text.Format(fmt,m_BranchCtrl.GetString());
570 m_CommitList.ShowText(text);
571 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
572 SetContinueButtonText();
573 return;
577 m_CommitList.Clear();
578 CString range;
579 range.Format(_T("%s..%s"), g_Git.FixBranchName(m_UpstreamCtrl.GetString()), g_Git.FixBranchName(m_BranchCtrl.GetString()));
580 this->m_CommitList.FillGitLog(nullptr, &range, 0);
582 if( m_CommitList.GetItemCount() == 0 )
583 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_NOTHINGTOREBASE)));
585 #if 0
586 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
588 if(upstream == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
590 m_CommitList.Clear();
591 m_CommitList.ShowText(_T("Nothing Rebase"));
594 #endif
596 m_tooltips.Pop();
597 AddBranchToolTips(&this->m_BranchCtrl);
598 AddBranchToolTips(&this->m_UpstreamCtrl);
600 for (int i = 0; i < m_CommitList.m_logEntries.size(); ++i)
602 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList) = CTGitPath::LOGACTIONS_REBASE_PICK;
605 m_CommitList.Invalidate();
607 if(m_CommitList.m_IsOldFirst)
608 this->m_CurrentRebaseIndex = -1;
609 else
610 this->m_CurrentRebaseIndex = (int)m_CommitList.m_logEntries.size();
612 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
613 SetContinueButtonText();
616 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
618 if(pBranch)
620 CString text=pBranch->GetString();
621 CString tooltip;
623 GitRev rev;
624 rev.GetCommit(text);
626 tooltip.Format(_T("%s: %s\n%s: %s <%s>\n%s: %s\n%s:\n%s\n%s"),
627 CString(MAKEINTRESOURCE(IDS_LOG_REVISION)),
628 rev.m_CommitHash.ToString(),
629 CString(MAKEINTRESOURCE(IDS_LOG_AUTHOR)),
630 rev.GetAuthorName(),
631 rev.GetAuthorEmail(),
632 CString(MAKEINTRESOURCE(IDS_LOG_DATE)),
633 CLoglistUtils::FormatDateAndTime(rev.GetAuthorDate(), DATE_LONGDATE),
634 CString(MAKEINTRESOURCE(IDS_LOG_MESSAGE)),
635 rev.GetSubject(),
636 rev.GetBody());
638 pBranch->DisableTooltip();
639 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
643 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
645 if (pMsg->message == WM_KEYDOWN)
647 switch (pMsg->wParam)
649 case ' ':
650 if(LogListHasFocus(pMsg->hwnd))
652 m_CommitList.ShiftSelectedAction();
653 return TRUE;
655 break;
656 case 'P':
657 if(LogListHasFocus(pMsg->hwnd))
659 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_PICK);
660 return TRUE;
662 break;
663 case 'S':
664 if(LogListHasFocus(pMsg->hwnd))
666 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SKIP);
667 return TRUE;
669 break;
670 case 'Q':
671 if(LogListHasFocus(pMsg->hwnd))
673 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
674 return TRUE;
676 break;
677 case 'E':
678 if(LogListHasFocus(pMsg->hwnd))
680 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
681 return TRUE;
683 break;
684 case 'A':
685 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
687 // select all entries
688 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
690 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
692 return TRUE;
694 break;
695 case VK_F5:
697 Refresh();
698 return TRUE;
700 break;
701 case VK_RETURN:
703 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
705 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
706 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
707 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
708 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
709 else
710 GetDlgItem(IDHELP)->SetFocus();
711 return TRUE;
714 break;
715 /* Avoid TAB control destroy but dialog exist*/
716 case VK_ESCAPE:
717 case VK_CANCEL:
719 TCHAR buff[128];
720 ::GetClassName(pMsg->hwnd,buff,128);
723 if(_tcsnicmp(buff,_T("RichEdit20W"),128)==0 ||
724 _tcsnicmp(buff,_T("Scintilla"),128)==0 ||
725 _tcsnicmp(buff,_T("SysListView32"),128)==0||
726 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
728 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
729 return TRUE;
734 else if (pMsg->message == WM_NEXTDLGCTL)
736 if (GetFocus()->GetSafeHwnd() == m_LogMessageCtrl.GetSafeHwnd())
738 if (GetDlgItem(IDC_REBASE_CONTINUE)->IsWindowEnabled())
739 GetDlgItem(IDC_REBASE_CONTINUE)->SetFocus();
740 else if (GetDlgItem(IDC_REBASE_ABORT)->IsWindowEnabled())
741 GetDlgItem(IDC_REBASE_ABORT)->SetFocus();
742 else
743 GetDlgItem(IDHELP)->SetFocus();
744 return TRUE;
747 m_tooltips.RelayEvent(pMsg);
748 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
751 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
753 TCHAR buff[128];
754 ::GetClassName(hwnd, buff, 128);
756 if(_tcsnicmp(buff, _T("SysListView32"), 128) == 0)
757 return true;
758 return false;
761 int CRebaseDlg::CheckRebaseCondition()
763 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
765 if( !g_Git.CheckCleanWorkTree() )
767 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
769 CString cmd,out;
770 cmd=_T("git.exe stash");
771 this->AddLogString(cmd);
772 if (g_Git.Run(cmd, &out, CP_UTF8))
774 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
775 return -1;
779 else
780 return -1;
782 //Todo Check $REBASE_ROOT
783 //Todo Check $DOTEST
785 if (!CAppUtils::CheckUserData())
786 return -1;
788 //Todo call pre_rebase_hook
789 return 0;
791 int CRebaseDlg::StartRebase()
793 CString cmd,out;
794 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
795 if(!this->m_IsCherryPick)
797 //Todo call comment_for_reflog
798 cmd.Format(_T("git.exe checkout %s"),this->m_BranchCtrl.GetString());
799 this->AddLogString(cmd);
800 while (true)
802 out.Empty();
803 if (g_Git.Run(cmd, &out, CP_UTF8))
805 this->AddLogString(out);
806 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
807 return -1;
809 else
810 break;
813 this->AddLogString(out);
816 cmd=_T("git.exe rev-parse --verify HEAD");
817 if(g_Git.Run(cmd,&out,CP_UTF8))
819 AddLogString(CString(MAKEINTRESOURCE(IDS_PROC_NOHEAD)));
820 return -1;
822 //Todo
823 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
824 // echo "detached HEAD" > "$DOTEST"/head-name
826 cmd.Format(_T("git.exe update-ref ORIG_HEAD HEAD"));
827 if(g_Git.Run(cmd,&out,CP_UTF8))
829 AddLogString(_T("update ORIG_HEAD Fail"));
830 return -1;
833 m_OrigUpstreamHash.Empty();
834 if (g_Git.GetHash(m_OrigUpstreamHash, m_UpstreamCtrl.GetString()))
836 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_UpstreamCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
837 return -1;
840 if( !this->m_IsCherryPick )
842 cmd.Format(_T("git.exe checkout -f %s"), m_OrigUpstreamHash.ToString());
843 this->AddLogString(cmd);
844 while (true)
846 out.Empty();
847 if (g_Git.Run(cmd, &out, CP_UTF8))
849 this->AddLogString(out);
850 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
851 return -1;
853 else
854 break;
858 CString log;
859 if( !this->m_IsCherryPick )
861 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
863 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
864 return -1;
866 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTREBASE)));
868 else
869 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTCHERRYPICK)));
871 this->AddLogString(log);
872 return 0;
874 int CRebaseDlg::VerifyNoConflict()
876 CTGitPathList list;
877 if(g_Git.ListConflictFile(list))
879 AddLogString(_T("Get conflict files fail"));
880 return -1;
882 if( list.GetCount() != 0 )
884 CMessageBox::Show(NULL, IDS_PROGRS_CONFLICTSOCCURED, IDS_APPNAME, MB_OK);
885 return -1;
887 return 0;
890 int CRebaseDlg::FinishRebase()
892 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
893 return 0;
895 CGitHash head;
896 if (g_Git.GetHash(head, _T("HEAD")))
898 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
899 return -1;
901 CString out,cmd;
903 cmd.Format(_T("git.exe checkout -f %s"),this->m_BranchCtrl.GetString());
904 AddLogString(cmd);
905 while (true)
907 out.Empty();
908 if (g_Git.Run(cmd, &out, CP_UTF8))
910 AddLogString(out);
911 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
912 return -1;
914 else
915 break;
917 AddLogString(out);
919 cmd.Format(_T("git.exe reset --hard %s"), head.ToString());
920 AddLogString(cmd);
921 while (true)
923 out.Empty();
924 if (g_Git.Run(cmd, &out, CP_UTF8))
926 AddLogString(out);
927 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
928 return -1;
930 else
931 break;
933 AddLogString(out);
935 while (m_ctrlTabCtrl.GetTabsNum() > 1)
936 m_ctrlTabCtrl.RemoveTab(0);
937 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED)));
938 m_sStatusText = CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED));
939 m_bStatusWarning = false;
940 m_CtrlStatusText.Invalidate();
942 return 0;
944 void CRebaseDlg::OnBnClickedContinue()
946 if( m_RebaseStage == REBASE_DONE)
948 OnOK();
949 return;
952 if( this->m_IsFastForward )
954 CString cmd,out;
955 CString oldbranch = g_Git.GetCurrentBranch();
956 if( oldbranch != m_BranchCtrl.GetString() )
958 cmd.Format(_T("git.exe checkout %s"),m_BranchCtrl.GetString());
959 AddLogString(cmd);
960 while (true)
962 out.Empty();
963 if (g_Git.Run(cmd, &out, CP_UTF8))
965 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
966 AddLogString(out);
967 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
968 return;
970 else
971 break;
974 AddLogString(out);
975 out.Empty();
976 if (g_Git.GetHash(m_OrigBranchHash, m_BranchCtrl.GetString()))
978 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_BranchCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
979 return;
981 if (g_Git.GetHash(m_OrigUpstreamHash, m_UpstreamCtrl.GetString()))
983 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_UpstreamCtrl.GetString() + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
984 return;
987 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
989 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
990 AddLogString(_T("No fast forward possible.\r\nMaybe repository changed"));
991 return;
994 cmd.Format(_T("git.exe reset --hard %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
995 CString log;
996 log.Format(IDS_PROC_REBASE_FFTO, m_UpstreamCtrl.GetString());
997 this->AddLogString(log);
999 AddLogString(cmd);
1000 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1001 while (true)
1003 out.Empty();
1004 if (g_Git.Run(cmd, &out, CP_UTF8))
1006 AddLogString(CString(MAKEINTRESOURCE(IDS_FAIL)));
1007 AddLogString(out);
1008 if (CMessageBox::Show(m_hWnd, out + _T("\nRetry?"), _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1009 return;
1011 else
1012 break;
1014 AddLogString(out);
1015 AddLogString(CString(MAKEINTRESOURCE(IDS_DONE)));
1016 m_RebaseStage = REBASE_DONE;
1017 UpdateCurrentStatus();
1018 return;
1021 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
1023 if(CheckRebaseCondition())
1024 return ;
1025 m_RebaseStage = REBASE_START;
1026 m_FileListCtrl.Clear();
1027 m_FileListCtrl.SetHasCheckboxes(false);
1028 m_FileListCtrl.m_CurrentVersion = L"";
1029 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, CString(MAKEINTRESOURCE(IDS_PROC_CONFLICTFILES)));
1030 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase, CString(MAKEINTRESOURCE(IDS_LOG)), 2);
1033 if( m_RebaseStage == REBASE_FINISH )
1035 if(FinishRebase())
1036 return ;
1038 OnOK();
1041 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
1043 if(VerifyNoConflict())
1044 return;
1045 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1046 if(this->CheckNextCommitIsSquash())
1047 {//next commit is not squash;
1048 m_RebaseStage = REBASE_SQUASH_EDIT;
1049 this->OnRebaseUpdateUI(0,0);
1050 this->UpdateCurrentStatus();
1051 return ;
1054 m_RebaseStage=REBASE_CONTINUE;
1055 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1056 this->UpdateCurrentStatus();
1060 if( m_RebaseStage == REBASE_CONFLICT )
1062 if(VerifyNoConflict())
1063 return;
1065 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1066 // ***************************************************
1067 // ATTENTION: Similar code in CommitDlg.cpp!!!
1068 // ***************************************************
1069 CMassiveGitTask mgtReAddAfterCommit(_T("add --ignore-errors -f"));
1070 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
1072 CString cmd, out;
1073 CTGitPath *entry = (CTGitPath *)m_FileListCtrl.GetItemData(i);
1074 if (entry->m_Checked)
1076 if (entry->m_Action & CTGitPath::LOGACTIONS_UNVER)
1077 cmd.Format(_T("git.exe add -f -- \"%s\""), entry->GetGitPathString());
1078 else if (entry->m_Action & CTGitPath::LOGACTIONS_DELETED)
1079 cmd.Format(_T("git.exe update-index --force-remove -- \"%s\""), entry->GetGitPathString());
1080 else
1081 cmd.Format(_T("git.exe update-index -- \"%s\""), entry->GetGitPathString());
1083 if (g_Git.Run(cmd, &out, CP_UTF8))
1085 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1086 return;
1089 if (entry->m_Action & CTGitPath::LOGACTIONS_REPLACED)
1090 cmd.Format(_T("git.exe rm -- \"%s\""), entry->GetGitOldPathString());
1092 g_Git.Run(cmd, &out, CP_UTF8);
1094 else
1096 if (entry->m_Action & CTGitPath::LOGACTIONS_ADDED || entry->m_Action & CTGitPath::LOGACTIONS_REPLACED)
1098 cmd.Format(_T("git.exe rm -f --cache -- \"%s\""), entry->GetGitPathString());
1099 if (g_Git.Run(cmd, &out, CP_UTF8))
1101 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK| MB_ICONERROR);
1102 return;
1104 mgtReAddAfterCommit.AddFile(*entry);
1106 if (entry->m_Action & CTGitPath::LOGACTIONS_REPLACED && !entry->GetGitOldPathString().IsEmpty())
1108 cmd.Format(_T("git.exe reset -- \"%s\""), entry->GetGitOldPathString());
1109 g_Git.Run(cmd, &out, CP_UTF8);
1112 else if(!(entry->m_Action & CTGitPath::LOGACTIONS_UNVER))
1114 cmd.Format(_T("git.exe reset -- \"%s\""), entry->GetGitPathString());
1115 g_Git.Run(cmd, &out, CP_UTF8);
1120 CString out =_T("");
1121 CString cmd;
1122 cmd.Format(_T("git.exe commit -C %s"), curRev->m_CommitHash.ToString());
1124 AddLogString(cmd);
1126 if(g_Git.Run(cmd,&out,CP_UTF8))
1128 AddLogString(out);
1129 if(!g_Git.CheckCleanWorkTree())
1131 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1132 return;
1136 AddLogString(out);
1138 if (((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\ReaddUnselectedAddedFilesAfterCommit"), TRUE)) == TRUE)
1140 BOOL cancel = FALSE;
1141 mgtReAddAfterCommit.Execute(cancel);
1144 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1145 if( curRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_EDIT)
1147 m_RebaseStage=REBASE_EDIT;
1148 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1149 this->UpdateCurrentStatus();
1150 return;
1152 else
1154 m_RebaseStage=REBASE_CONTINUE;
1155 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1156 this->UpdateCurrentStatus();
1160 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
1162 CString str;
1163 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1165 str=this->m_LogMessageCtrl.GetText();
1166 if(str.Trim().IsEmpty())
1168 CMessageBox::Show(NULL, IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1169 return;
1172 CString tempfile=::GetTempFile();
1173 CAppUtils::SaveCommitUnicodeFile(tempfile, str);
1175 CString out,cmd;
1177 if( m_RebaseStage == REBASE_SQUASH_EDIT )
1178 cmd.Format(_T("git.exe commit -F \"%s\""), tempfile);
1179 else
1180 cmd.Format(_T("git.exe commit --amend -F \"%s\""), tempfile);
1182 if(g_Git.Run(cmd,&out,CP_UTF8))
1184 if(!g_Git.CheckCleanWorkTree())
1186 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1187 return;
1191 CFile::Remove(tempfile);
1192 AddLogString(out);
1193 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1194 m_RebaseStage=REBASE_CONTINUE;
1195 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1196 this->UpdateCurrentStatus();
1200 InterlockedExchange(&m_bThreadRunning, TRUE);
1201 SetControlEnable();
1203 if (AfxBeginThread(RebaseThreadEntry, this)==NULL)
1205 InterlockedExchange(&m_bThreadRunning, FALSE);
1206 CMessageBox::Show(NULL, _T("Create Rebase Thread Fail"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1207 SetControlEnable();
1210 int CRebaseDlg::CheckNextCommitIsSquash()
1212 int index;
1213 if(m_CommitList.m_IsOldFirst)
1214 index=m_CurrentRebaseIndex+1;
1215 else
1216 index=m_CurrentRebaseIndex-1;
1218 GitRev *curRev;
1221 if(index<0)
1222 return -1;
1223 if(index>= m_CommitList.GetItemCount())
1224 return -1;
1226 curRev=(GitRev*)m_CommitList.m_arShownList[index];
1228 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SQUASH )
1229 return 0;
1230 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP)
1232 if(m_CommitList.m_IsOldFirst)
1233 ++index;
1234 else
1235 --index;
1237 else
1238 return -1;
1240 }while(curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP);
1242 return -1;
1245 int CRebaseDlg::GoNext()
1247 if(m_CommitList.m_IsOldFirst)
1248 ++m_CurrentRebaseIndex;
1249 else
1250 --m_CurrentRebaseIndex;
1251 return 0;
1254 int CRebaseDlg::StateAction()
1256 switch(this->m_RebaseStage)
1258 case CHOOSE_BRANCH:
1259 case CHOOSE_COMMIT_PICK_MODE:
1260 if(StartRebase())
1261 return -1;
1262 m_RebaseStage = REBASE_START;
1263 GoNext();
1264 break;
1267 return 0;
1269 void CRebaseDlg::SetContinueButtonText()
1271 CString Text;
1272 switch(this->m_RebaseStage)
1274 case CHOOSE_BRANCH:
1275 case CHOOSE_COMMIT_PICK_MODE:
1276 if(this->m_IsFastForward)
1277 Text.LoadString(IDS_PROC_STARTREBASEFFBUTTON);
1278 else
1279 Text.LoadString(IDS_PROC_STARTREBASEBUTTON);
1280 break;
1282 case REBASE_START:
1283 case REBASE_ERROR:
1284 case REBASE_CONTINUE:
1285 case REBASE_SQUASH_CONFLICT:
1286 Text.LoadString(IDS_CONTINUEBUTTON);
1287 break;
1289 case REBASE_CONFLICT:
1290 Text.LoadString(IDS_COMMITBUTTON);
1291 break;
1292 case REBASE_EDIT:
1293 Text.LoadString(IDS_AMENDBUTTON);
1294 break;
1296 case REBASE_SQUASH_EDIT:
1297 Text.LoadString(IDS_COMMITBUTTON);
1298 break;
1300 case REBASE_ABORT:
1301 case REBASE_FINISH:
1302 Text.LoadString(IDS_FINISHBUTTON);
1303 break;
1305 case REBASE_DONE:
1306 Text.LoadString(IDS_DONE);
1307 break;
1309 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1312 void CRebaseDlg::SetControlEnable()
1314 switch(this->m_RebaseStage)
1316 case CHOOSE_BRANCH:
1317 case CHOOSE_COMMIT_PICK_MODE:
1319 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(TRUE);
1320 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(TRUE);
1321 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(TRUE);
1322 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(TRUE);
1323 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(TRUE);
1325 if(!m_IsCherryPick)
1327 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1328 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1329 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1331 //this->m_CommitList.m_IsEnableRebaseMenu=TRUE;
1332 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1333 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1334 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1335 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP);
1336 break;
1338 case REBASE_START:
1339 case REBASE_CONTINUE:
1340 case REBASE_ABORT:
1341 case REBASE_ERROR:
1342 case REBASE_FINISH:
1343 case REBASE_CONFLICT:
1344 case REBASE_EDIT:
1345 case REBASE_SQUASH_CONFLICT:
1346 case REBASE_DONE:
1347 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(FALSE);
1348 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(FALSE);
1349 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(FALSE);
1350 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1351 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1352 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1353 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(FALSE);
1354 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(FALSE);
1355 //this->m_CommitList.m_IsEnableRebaseMenu=FALSE;
1356 this->m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1357 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1358 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1359 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP));
1361 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1363 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1364 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1365 this->m_PostButton.RemoveAll();
1366 this->m_PostButton.AddEntries(m_PostButtonTexts);
1367 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1369 break;
1372 if(m_bThreadRunning)
1374 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1377 else if (m_RebaseStage != REBASE_ERROR)
1379 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1383 void CRebaseDlg::UpdateProgress()
1385 int index;
1386 CRect rect;
1388 if(m_CommitList.m_IsOldFirst)
1389 index = m_CurrentRebaseIndex+1;
1390 else
1391 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1393 m_ProgressBar.SetRange32(1, m_CommitList.GetItemCount());
1394 m_ProgressBar.SetPos(index);
1395 if (m_pTaskbarList)
1397 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1398 m_pTaskbarList->SetProgressValue(m_hWnd, index, m_CommitList.GetItemCount());
1401 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1403 CString text;
1404 text.Format(IDS_PROC_REBASING_PROGRESS, index, m_CommitList.GetItemCount());
1405 m_sStatusText = text;
1406 m_CtrlStatusText.SetWindowText(text);
1407 m_bStatusWarning = false;
1408 m_CtrlStatusText.Invalidate();
1411 GitRev *prevRev=NULL, *curRev=NULL;
1413 if( m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex< m_CommitList.m_arShownList.GetSize())
1415 curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1418 for (int i = 0; i < m_CommitList.m_arShownList.GetSize(); ++i)
1420 prevRev=(GitRev*)m_CommitList.m_arShownList[i];
1421 if(prevRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_CURRENT)
1423 prevRev->GetAction(&m_CommitList) &= ~ CTGitPath::LOGACTIONS_REBASE_CURRENT;
1424 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1425 m_CommitList.InvalidateRect(rect);
1429 if(curRev)
1431 curRev->GetAction(&m_CommitList) |= CTGitPath::LOGACTIONS_REBASE_CURRENT;
1432 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1433 m_CommitList.InvalidateRect(rect);
1435 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1438 void CRebaseDlg::UpdateCurrentStatus()
1440 SetContinueButtonText();
1441 SetControlEnable();
1442 UpdateProgress();
1445 void CRebaseDlg::AddLogString(CString str)
1447 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1448 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1449 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1450 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1451 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1452 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1455 int CRebaseDlg::GetCurrentCommitID()
1457 if(m_CommitList.m_IsOldFirst)
1459 return this->m_CurrentRebaseIndex+1;
1462 else
1464 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1468 int CRebaseDlg::DoRebase()
1470 CString cmd,out;
1471 if(m_CurrentRebaseIndex <0)
1472 return 0;
1473 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1474 return 0;
1476 GitRev *pRev = (GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1477 int mode=pRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_MODE_MASK;
1478 CString nocommit;
1480 if( mode== CTGitPath::LOGACTIONS_REBASE_SKIP)
1482 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1483 return 0;
1486 if (!CheckNextCommitIsSquash() || mode != CTGitPath::LOGACTIONS_REBASE_PICK)
1487 { // next commit is squash or not pick
1488 if (!this->m_SquashMessage.IsEmpty())
1489 this->m_SquashMessage += _T("\n\n");
1490 this->m_SquashMessage += pRev->GetSubject();
1491 this->m_SquashMessage += _T("\n");
1492 this->m_SquashMessage += pRev->GetBody().TrimRight();
1494 else
1495 this->m_SquashMessage.Empty();
1497 if (!CheckNextCommitIsSquash() || mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1498 { // next or this commit is squash
1499 nocommit=_T(" --no-commit ");
1502 CString log;
1503 log.Format(_T("%s %d: %s"),CTGitPath::GetActionName(mode),this->GetCurrentCommitID(),pRev->m_CommitHash.ToString());
1504 AddLogString(log);
1505 AddLogString(pRev->GetSubject());
1506 if (pRev->GetSubject().IsEmpty())
1508 CMessageBox::Show(m_hWnd, IDS_PROC_REBASE_EMPTYCOMMITMSG, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
1509 mode = CTGitPath::LOGACTIONS_REBASE_EDIT;
1512 CString cherryPickedFrom;
1513 if (m_bAddCherryPickedFrom)
1514 cherryPickedFrom = _T("-x ");
1516 cmd.Format(_T("git.exe cherry-pick %s%s %s"), cherryPickedFrom, nocommit, pRev->m_CommitHash.ToString());
1518 if(g_Git.Run(cmd,&out,CP_UTF8))
1520 AddLogString(out);
1521 CTGitPathList list;
1522 if(g_Git.ListConflictFile(list))
1524 AddLogString(_T("Get conflict files fail"));
1525 return -1;
1527 if(list.GetCount() == 0 )
1529 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1531 m_RebaseStage = REBASE_ERROR;
1532 AddLogString(_T("An unrecoverable error occoured."));
1533 return -1;
1535 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1537 this->m_RebaseStage = REBASE_EDIT ;
1538 return -1; // Edit return -1 to stop rebase.
1540 // Squash Case
1541 if(CheckNextCommitIsSquash())
1542 { // no squash
1543 // let user edit last commmit message
1544 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1545 return -1;
1548 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1549 m_RebaseStage = REBASE_SQUASH_CONFLICT;
1550 else
1551 m_RebaseStage = REBASE_CONFLICT;
1552 return -1;
1555 else
1557 AddLogString(out);
1558 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1560 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1561 return 0;
1563 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1565 this->m_RebaseStage = REBASE_EDIT ;
1566 return -1; // Edit return -1 to stop rebase.
1569 // Squash Case
1570 if(CheckNextCommitIsSquash())
1571 { // no squash
1572 // let user edit last commmit message
1573 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1574 return -1;
1576 else if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1577 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1580 return 0;
1583 BOOL CRebaseDlg::IsEnd()
1585 if(m_CommitList.m_IsOldFirst)
1586 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
1587 else
1588 return m_CurrentRebaseIndex<0;
1591 int CRebaseDlg::RebaseThread()
1593 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
1595 int ret=0;
1596 while(1)
1598 if( m_RebaseStage == REBASE_START )
1600 if( this->StartRebase() )
1602 ret = -1;
1603 break;
1605 m_RebaseStage = REBASE_CONTINUE;
1608 else if( m_RebaseStage == REBASE_CONTINUE )
1610 this->GoNext();
1611 SendMessage(MSG_REBASE_UPDATE_UI);
1612 if(IsEnd())
1614 ret = 0;
1615 m_RebaseStage = REBASE_FINISH;
1618 else
1620 ret = DoRebase();
1622 if( ret )
1624 break;
1629 else if( m_RebaseStage == REBASE_FINISH )
1631 SendMessage(MSG_REBASE_UPDATE_UI);
1632 m_RebaseStage = REBASE_DONE;
1633 break;
1636 else
1638 break;
1640 this->PostMessage(MSG_REBASE_UPDATE_UI);
1643 InterlockedExchange(&m_bThreadRunning, FALSE);
1644 this->PostMessage(MSG_REBASE_UPDATE_UI);
1645 return ret;
1648 void CRebaseDlg::ListConflictFile()
1650 this->m_FileListCtrl.Clear();
1651 m_FileListCtrl.SetHasCheckboxes(true);
1652 CTGitPathList list;
1653 CTGitPath path;
1654 list.AddPath(path);
1656 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
1658 this->m_FileListCtrl.GetStatus(&list,true);
1659 this->m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED|CTGitPath::LOGACTIONS_MODIFIED|CTGitPath::LOGACTIONS_ADDED|CTGitPath::LOGACTIONS_DELETED,
1660 CTGitPath::LOGACTIONS_UNMERGED);
1662 m_FileListCtrl.Check(GITSLC_SHOWFILES);
1663 bool hasSubmoduleChange = false;
1664 for (int i = 0; i < m_FileListCtrl.GetItemCount(); i++)
1666 CTGitPath *entry = (CTGitPath *)m_FileListCtrl.GetItemData(i);
1667 if (entry->IsDirectory())
1669 hasSubmoduleChange = true;
1670 break;
1674 if (hasSubmoduleChange)
1676 m_CtrlStatusText.SetWindowText(m_sStatusText + _T(", ") + CString(MAKEINTRESOURCE(IDS_CARE_SUBMODULE_CHANGES)));
1677 m_bStatusWarning = true;
1678 m_CtrlStatusText.Invalidate();
1680 else
1682 m_CtrlStatusText.SetWindowText(m_sStatusText);
1683 m_bStatusWarning = false;
1684 m_CtrlStatusText.Invalidate();
1688 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
1690 if (m_RebaseStage == REBASE_FINISH)
1692 FinishRebase();
1693 if (m_pTaskbarList)
1694 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1695 return 0;
1697 UpdateCurrentStatus();
1698 if(m_CurrentRebaseIndex <0)
1699 return 0;
1700 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1701 return 0;
1702 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1704 switch(m_RebaseStage)
1706 case REBASE_CONFLICT:
1707 case REBASE_SQUASH_CONFLICT:
1708 ListConflictFile();
1709 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
1710 if (m_pTaskbarList)
1711 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1712 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1713 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1714 break;
1715 case REBASE_EDIT:
1716 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1717 if (m_pTaskbarList)
1718 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1719 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1720 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1721 break;
1722 case REBASE_SQUASH_EDIT:
1723 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1724 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1725 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
1726 if (m_pTaskbarList)
1727 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1728 break;
1729 default:
1730 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1732 return 0;
1734 void CRebaseDlg::OnCancel()
1736 OnBnClickedAbort();
1738 void CRebaseDlg::OnBnClickedAbort()
1740 if (m_pTaskbarList)
1741 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1743 CString cmd,out;
1744 CString pron = m_OrigUpstreamHash.ToString();
1745 if(m_OrigUpstreamHash.IsEmpty())
1747 __super::OnCancel();
1750 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
1752 return;
1755 if(CMessageBox::Show(NULL, IDS_PROC_REBASE_ABORT, IDS_APPNAME, MB_YESNO) != IDYES)
1756 return;
1758 if(this->m_IsFastForward)
1760 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1761 while (true)
1763 out.Empty();
1764 if (g_Git.Run(cmd, &out, CP_UTF8))
1766 AddLogString(out);
1767 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1768 break;
1770 else
1771 break;
1773 __super::OnCancel();
1774 return;
1776 cmd.Format(_T("git.exe checkout -f %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1777 while (true)
1779 out.Empty();
1780 if (g_Git.Run(cmd, &out, CP_UTF8))
1782 AddLogString(out);
1783 if (CMessageBox::Show(this->m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1785 __super::OnCancel();
1786 return;
1789 else
1790 break;
1793 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigUpstreamHash.ToString());
1794 while (true)
1796 out.Empty();
1797 if (g_Git.Run(cmd, &out, CP_UTF8))
1799 AddLogString(out);
1800 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1802 __super::OnCancel();
1803 return;
1806 else
1807 break;
1810 if(this->m_IsCherryPick) //there are not "branch" at cherry pick mode
1812 __super::OnCancel();
1813 return;
1816 cmd.Format(_T("git checkout -f %s"),this->m_BranchCtrl.GetString());
1817 if(g_Git.Run(cmd,&out,CP_UTF8))
1819 AddLogString(out);
1820 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1821 __super::OnCancel();
1822 return;
1825 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1826 while (true)
1828 out.Empty();
1829 if (g_Git.Run(cmd, &out, CP_UTF8))
1831 AddLogString(out);
1832 if (CMessageBox::Show(m_hWnd, _T("Retry?\nUnrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_YESNO | MB_ICONERROR) != IDYES)
1833 break;
1835 else
1836 break;
1838 __super::OnCancel();
1841 void CRebaseDlg::OnBnClickedButtonBrowse()
1843 if(CBrowseRefsDlg::PickRefForCombo(&m_UpstreamCtrl, gPickRef_NoTag))
1844 OnCbnSelchangeUpstream();
1847 void CRebaseDlg::OnBnClickedRebaseCheckForce()
1849 this->UpdateData();
1850 this->FetchLogList();
1851 if(this->CheckRebaseCondition())
1853 /* Disable Start Rebase */
1854 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1858 void CRebaseDlg::OnBnClickedRebasePostButton()
1860 this->m_Upstream=this->m_UpstreamCtrl.GetString();
1861 this->m_Branch=this->m_BranchCtrl.GetString();
1863 this->EndDialog((int)(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry()));
1866 LRESULT CRebaseDlg::OnGitStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
1868 Refresh();
1869 return 0;
1872 void CRebaseDlg::Refresh()
1874 if (m_RebaseStage == REBASE_CONFLICT || m_RebaseStage == REBASE_SQUASH_CONFLICT)
1876 ListConflictFile();
1877 return;
1880 if(this->m_IsCherryPick)
1881 return ;
1883 if(this->m_RebaseStage == CHOOSE_BRANCH )
1885 this->UpdateData();
1886 this->FetchLogList();
1887 if(this->CheckRebaseCondition())
1889 /* Disable Start Rebase */
1890 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1895 void CRebaseDlg::OnBnClickedButtonUp2()
1897 POSITION pos;
1898 pos = m_CommitList.GetFirstSelectedItemPosition();
1900 // do nothing if the first selected item is the first item in the list
1901 if (m_CommitList.GetNextSelectedItem(pos) == 0)
1902 return;
1904 pos = m_CommitList.GetFirstSelectedItemPosition();
1906 bool changed = false;
1907 while(pos)
1909 int index=m_CommitList.GetNextSelectedItem(pos);
1910 if(index>=1)
1912 CGitHash old = m_CommitList.m_logEntries[index-1];
1913 m_CommitList.m_logEntries[index-1] = m_CommitList.m_logEntries[index];
1914 m_CommitList.m_logEntries[index] = old;
1915 m_CommitList.SetItemState(index-1, LVIS_SELECTED, LVIS_SELECTED);
1916 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1917 changed = true;
1920 if (changed)
1922 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1923 m_CommitList.Invalidate();
1924 m_CommitList.SetFocus();
1928 void CRebaseDlg::OnBnClickedButtonDown2()
1930 if (m_CommitList.GetSelectedCount() == 0)
1931 return;
1933 POSITION pos;
1934 pos = m_CommitList.GetFirstSelectedItemPosition();
1935 bool changed = false;
1936 // use an array to store all selected item indexes; the user won't select too much items
1937 int* indexes = NULL;
1938 indexes = new int[m_CommitList.GetSelectedCount()];
1939 int i = 0;
1940 while(pos)
1942 indexes[i++] = m_CommitList.GetNextSelectedItem(pos);
1944 // don't move any item if the last selected item is the last item in the m_CommitList
1945 // (that would change the order of the selected items)
1946 if(indexes[m_CommitList.GetSelectedCount() - 1] < m_CommitList.GetItemCount() - 1)
1948 // iterate over the indexes backwards in order to correctly move multiselected items
1949 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
1951 int index = indexes[i];
1952 CGitHash old = m_CommitList.m_logEntries[index+1];
1953 m_CommitList.m_logEntries[index+1] = m_CommitList.m_logEntries[index];
1954 m_CommitList.m_logEntries[index] = old;
1955 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1956 m_CommitList.SetItemState(index+1, LVIS_SELECTED, LVIS_SELECTED);
1957 changed = true;
1960 delete [] indexes;
1961 indexes = NULL;
1962 if (changed)
1964 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1965 m_CommitList.Invalidate();
1966 m_CommitList.SetFocus();
1970 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1972 m_pTaskbarList.Release();
1973 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1974 return 0;
1977 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
1979 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
1980 *pResult = 0;
1981 if(m_CommitList.m_bNoDispUpdates)
1982 return;
1983 if (pNMLV->iItem >= 0)
1985 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
1986 if (pNMLV->iSubItem != 0)
1987 return;
1988 if ((pNMLV->iItem == m_CommitList.m_arShownList.GetCount()))
1990 // remove the selected state
1991 if (pNMLV->uChanged & LVIF_STATE)
1993 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
1994 FillLogMessageCtrl();
1996 return;
1998 if (pNMLV->uChanged & LVIF_STATE)
2000 FillLogMessageCtrl();
2003 else
2005 FillLogMessageCtrl();
2009 void CRebaseDlg::FillLogMessageCtrl()
2011 int selCount = m_CommitList.GetSelectedCount();
2012 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
2014 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
2015 int selIndex = m_CommitList.GetNextSelectedItem(pos);
2016 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_CommitList.m_arShownList.SafeGetAt(selIndex));
2017 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
2018 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
2019 m_FileListCtrl.Show(GITSLC_SHOWVERSIONED);
2020 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
2021 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + _T("\n") + pLogEntry->GetBody());
2022 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
2025 void CRebaseDlg::OnBnClickedCheckCherryPickedFrom()
2027 UpdateData();