some spaces-tabs code cleanup
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
blob7d586db8c9783e9a9c9743cb7da89d6ed2690cf4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
4 // Copyright (C) 2011 - 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 "MessageBox.h"
29 #include "UnicodeUtils.h"
30 #include "BrowseRefsDlg.h"
31 #include "ProgressDlg.h"
32 // CRebaseDlg dialog
34 IMPLEMENT_DYNAMIC(CRebaseDlg, CResizableStandAloneDialog)
36 CRebaseDlg::CRebaseDlg(CWnd* pParent /*=NULL*/)
37 : CResizableStandAloneDialog(CRebaseDlg::IDD, pParent)
38 , m_bPickAll(FALSE)
39 , m_bSquashAll(FALSE)
40 , m_bEditAll(FALSE)
42 m_RebaseStage=CHOOSE_BRANCH;
43 m_CurrentRebaseIndex=-1;
44 m_bThreadRunning =FALSE;
45 this->m_IsCherryPick = FALSE;
46 m_bForce=FALSE;
47 m_IsFastForward=FALSE;
50 CRebaseDlg::~CRebaseDlg()
54 void CRebaseDlg::DoDataExchange(CDataExchange* pDX)
56 CDialog::DoDataExchange(pDX);
57 DDX_Control(pDX, IDC_REBASE_PROGRESS, m_ProgressBar);
58 DDX_Control(pDX, IDC_STATUS_STATIC, m_CtrlStatusText);
59 DDX_Check(pDX, IDC_PICK_ALL, m_bPickAll);
60 DDX_Check(pDX, IDC_SQUASH_ALL, m_bSquashAll);
61 DDX_Check(pDX, IDC_EDIT_ALL, m_bEditAll);
62 DDX_Control(pDX, IDC_REBASE_SPLIT, m_wndSplitter);
63 DDX_Control(pDX,IDC_COMMIT_LIST,m_CommitList);
64 DDX_Control(pDX,IDC_REBASE_COMBOXEX_BRANCH, this->m_BranchCtrl);
65 DDX_Control(pDX,IDC_REBASE_COMBOXEX_UPSTREAM, this->m_UpstreamCtrl);
66 DDX_Check(pDX, IDC_REBASE_CHECK_FORCE,m_bForce);
67 DDX_Control(pDX,IDC_REBASE_POST_BUTTON,m_PostButton);
71 BEGIN_MESSAGE_MAP(CRebaseDlg, CResizableStandAloneDialog)
72 ON_BN_CLICKED(IDC_PICK_ALL, &CRebaseDlg::OnBnClickedPickAll)
73 ON_BN_CLICKED(IDC_SQUASH_ALL, &CRebaseDlg::OnBnClickedSquashAll)
74 ON_BN_CLICKED(IDC_EDIT_ALL, &CRebaseDlg::OnBnClickedEditAll)
75 ON_BN_CLICKED(IDC_REBASE_SPLIT, &CRebaseDlg::OnBnClickedRebaseSplit)
76 ON_BN_CLICKED(IDC_REBASE_CONTINUE,OnBnClickedContinue)
77 ON_BN_CLICKED(IDC_REBASE_ABORT, OnBnClickedAbort)
78 ON_WM_SIZE()
79 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_BRANCH, &CRebaseDlg::OnCbnSelchangeBranch)
80 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_UPSTREAM, &CRebaseDlg::OnCbnSelchangeUpstream)
81 ON_MESSAGE(MSG_REBASE_UPDATE_UI, OnRebaseUpdateUI)
82 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CRebaseDlg::OnBnClickedButtonBrowse)
83 ON_BN_CLICKED(IDC_REBASE_CHECK_FORCE, &CRebaseDlg::OnBnClickedRebaseCheckForce)
84 ON_BN_CLICKED(IDC_REBASE_POST_BUTTON, &CRebaseDlg::OnBnClickedRebasePostButton)
85 ON_BN_CLICKED(IDC_BUTTON_UP2, &CRebaseDlg::OnBnClickedButtonUp2)
86 ON_BN_CLICKED(IDC_BUTTON_DOWN2, &CRebaseDlg::OnBnClickedButtonDown2)
87 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
88 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COMMIT_LIST, OnLvnItemchangedLoglist)
89 END_MESSAGE_MAP()
91 void CRebaseDlg::AddRebaseAnchor()
93 AddAnchor(IDC_REBASE_TAB,TOP_LEFT,BOTTOM_RIGHT);
94 AddAnchor(IDC_COMMIT_LIST,TOP_LEFT, TOP_RIGHT);
95 AddAnchor(IDC_REBASE_SPLIT,TOP_LEFT, TOP_RIGHT);
96 AddAnchor(IDC_STATUS_STATIC, BOTTOM_LEFT,BOTTOM_RIGHT);
97 AddAnchor(IDC_REBASE_CONTINUE,BOTTOM_RIGHT);
98 AddAnchor(IDC_REBASE_ABORT, BOTTOM_RIGHT);
99 AddAnchor(IDC_REBASE_PROGRESS,BOTTOM_LEFT, BOTTOM_RIGHT);
100 AddAnchor(IDC_PICK_ALL,TOP_LEFT);
101 AddAnchor(IDC_SQUASH_ALL,TOP_LEFT);
102 AddAnchor(IDC_EDIT_ALL,TOP_LEFT);
103 AddAnchor(IDC_BUTTON_UP2,TOP_LEFT);
104 AddAnchor(IDC_BUTTON_DOWN2,TOP_LEFT);
105 AddAnchor(IDC_REBASE_COMBOXEX_UPSTREAM,TOP_LEFT);
106 AddAnchor(IDC_REBASE_COMBOXEX_BRANCH,TOP_LEFT);
107 AddAnchor(IDC_REBASE_STATIC_UPSTREAM,TOP_LEFT);
108 AddAnchor(IDC_REBASE_STATIC_BRANCH,TOP_LEFT);
109 AddAnchor(IDHELP, BOTTOM_RIGHT);
110 AddAnchor(IDC_REBASE_CHECK_FORCE,TOP_RIGHT);
111 AddAnchor(IDC_REBASE_POST_BUTTON,BOTTOM_LEFT);
113 this->AddOthersToAnchor();
116 BOOL CRebaseDlg::OnInitDialog()
118 CResizableStandAloneDialog::OnInitDialog();
119 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
121 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
122 // do this, Explorer would be unable to send that message to our window if we
123 // were running elevated. It's OK to make the call all the time, since if we're
124 // not elevated, this is a no-op.
125 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
126 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
127 HMODULE hUser = ::LoadLibrary(_T("user32.dll"));
128 if (hUser)
130 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
131 if (pfnChangeWindowMessageFilterEx)
133 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
135 FreeLibrary(hUser);
137 m_pTaskbarList.Release();
138 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
140 CRect rectDummy;
141 //IDC_REBASE_DUMY_TAB
143 GetClientRect(m_DlgOrigRect);
144 m_CommitList.GetClientRect(m_CommitListOrigRect);
146 CWnd *pwnd=this->GetDlgItem(IDC_REBASE_DUMY_TAB);
147 pwnd->GetWindowRect(&rectDummy);
148 this->ScreenToClient(rectDummy);
150 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_REBASE_TAB))
152 TRACE0("Failed to create output tab window\n");
153 return FALSE; // fail to create
155 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
156 // Create output panes:
157 //const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
158 DWORD dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
160 if (! this->m_FileListCtrl.Create(dwStyle,rectDummy,&this->m_ctrlTabCtrl,0) )
162 TRACE0("Failed to create output windows\n");
163 return FALSE; // fail to create
166 if( ! this->m_LogMessageCtrl.Create(_T("Scintilla"),_T("source"),0,rectDummy,&m_ctrlTabCtrl,0,0) )
168 TRACE0("Failed to create log message control");
169 return FALSE;
171 m_LogMessageCtrl.Init(0);
172 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
174 dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
176 if (!m_wndOutputRebase.Create(_T("Scintilla"),_T("source"),0,rectDummy, &m_ctrlTabCtrl, 0,0) )
178 TRACE0("Failed to create output windows\n");
179 return -1; // fail to create
181 m_wndOutputRebase.Init(0);
182 m_wndOutputRebase.Call(SCI_SETREADONLY, TRUE);
184 m_tooltips.Create(this);
186 m_tooltips.AddTool(IDC_REBASE_CHECK_FORCE,IDS_REBASE_FORCE_TT);
187 m_tooltips.AddTool(IDC_REBASE_ABORT,IDS_REBASE_ABORT_TT);
191 m_FileListCtrl.Init(SVNSLC_COLEXT | SVNSLC_COLSTATUS |SVNSLC_COLADD|SVNSLC_COLDEL , _T("RebaseDlg"),(SVNSLC_POPALL ^ SVNSLC_POPCOMMIT),false);
193 m_ctrlTabCtrl.AddTab(&m_FileListCtrl,_T("Revision Files"));
194 m_ctrlTabCtrl.AddTab(&m_LogMessageCtrl,_T("Commit Message"),1);
195 AddRebaseAnchor();
197 CString sWindowTitle;
198 GetWindowText(sWindowTitle);
199 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
201 EnableSaveRestore(_T("RebaseDlg"));
203 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
204 RECT rcDlg, rcLogMsg, rcFileList;
205 GetClientRect(&rcDlg);
206 m_CommitList.GetWindowRect(&rcLogMsg);
207 ScreenToClient(&rcLogMsg);
208 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
209 ScreenToClient(&rcFileList);
210 if (yPos)
212 RECT rectSplitter;
213 m_wndSplitter.GetWindowRect(&rectSplitter);
214 ScreenToClient(&rectSplitter);
215 int delta = yPos - rectSplitter.top;
216 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
218 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
219 DoSize(delta);
223 if( this->m_RebaseStage == CHOOSE_BRANCH)
225 this->LoadBranchInfo();
227 }else
229 this->m_BranchCtrl.EnableWindow(FALSE);
230 this->m_UpstreamCtrl.EnableWindow(FALSE);
233 m_CommitList.m_ColumnRegKey = _T("Rebase");
234 m_CommitList.m_IsIDReplaceAction = TRUE;
235 // m_CommitList.m_IsOldFirst = TRUE;
236 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
238 m_CommitList.InsertGitColumn();
240 this->SetControlEnable();
242 if(!this->m_PreCmd.IsEmpty())
244 CProgressDlg progress;
245 progress.m_GitCmd=m_PreCmd;
246 progress.m_bAutoCloseOnSuccess=true;
247 progress.DoModal();
250 if(m_IsCherryPick)
252 this->m_BranchCtrl.SetCurSel(-1);
253 this->m_BranchCtrl.EnableWindow(FALSE);
254 this->m_UpstreamCtrl.EnableWindow(FALSE);
255 this->SetWindowText(_T("Cherry Pick"));
256 this->m_CommitList.StartFilter();
258 }else
260 SetContinueButtonText();
261 m_CommitList.DeleteAllItems();
262 FetchLogList();
265 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
266 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
267 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
268 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
269 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
270 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
271 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
273 if(m_CommitList.m_IsOldFirst)
274 this->m_CurrentRebaseIndex = -1;
275 else
276 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
279 if(this->CheckRebaseCondition())
281 /* Disable Start Rebase */
282 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
285 return TRUE;
287 // CRebaseDlg message handlers
289 void CRebaseDlg::OnBnClickedPickAll()
291 this->UpdateData();
292 if(this->m_bPickAll)
293 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_PICK);
295 this->m_bEditAll=FALSE;
296 this->m_bSquashAll=FALSE;
297 this->UpdateData(FALSE);
300 void CRebaseDlg::OnBnClickedSquashAll()
302 this->UpdateData();
303 if(this->m_bSquashAll)
304 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
306 this->m_bEditAll=FALSE;
307 this->m_bPickAll=FALSE;
308 this->UpdateData(FALSE);
311 void CRebaseDlg::OnBnClickedEditAll()
313 this->UpdateData();
314 if( this->m_bEditAll )
315 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
317 this->m_bPickAll=FALSE;
318 this->m_bSquashAll=FALSE;
319 this->UpdateData(FALSE);
322 void CRebaseDlg::SetAllRebaseAction(int action)
324 for(int i=0;i<this->m_CommitList.m_logEntries.size();i++)
326 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList)=action;
328 m_CommitList.Invalidate();
331 void CRebaseDlg::OnBnClickedRebaseSplit()
333 this->UpdateData();
336 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
338 switch (message) {
339 case WM_NOTIFY:
340 if (wParam == IDC_REBASE_SPLIT)
342 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
343 DoSize(pHdr->delta);
345 break;
348 return __super::DefWindowProc(message, wParam, lParam);
351 void CRebaseDlg::DoSize(int delta)
353 this->RemoveAllAnchors();
355 CSplitterControl::ChangeHeight(GetDlgItem(IDC_COMMIT_LIST), delta, CW_TOPALIGN);
356 //CSplitterControl::ChangeHeight(GetDlgItem(), delta, CW_TOPALIGN);
357 CSplitterControl::ChangeHeight(GetDlgItem(IDC_REBASE_TAB), -delta, CW_BOTTOMALIGN);
358 //CSplitterControl::ChangeHeight(GetDlgItem(), -delta, CW_BOTTOMALIGN);
359 CSplitterControl::ChangePos(GetDlgItem(IDC_SQUASH_ALL),0,delta);
360 CSplitterControl::ChangePos(GetDlgItem(IDC_PICK_ALL),0,delta);
361 CSplitterControl::ChangePos(GetDlgItem(IDC_EDIT_ALL),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);
366 this->AddRebaseAnchor();
367 // adjust the minimum size of the dialog to prevent the resizing from
368 // moving the list control too far down.
369 CRect rcLogMsg;
370 m_CommitList.GetClientRect(rcLogMsg);
371 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
373 SetSplitterRange();
374 // m_CommitList.Invalidate();
376 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
378 this->m_ctrlTabCtrl.Invalidate();
379 this->m_CommitList.Invalidate();
380 this->m_FileListCtrl.Invalidate();
381 this->m_LogMessageCtrl.Invalidate();
385 void CRebaseDlg::SetSplitterRange()
387 if ((m_CommitList)&&(m_ctrlTabCtrl))
389 CRect rcTop;
390 m_CommitList.GetWindowRect(rcTop);
391 ScreenToClient(rcTop);
392 CRect rcMiddle;
393 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
394 ScreenToClient(rcMiddle);
395 if (rcMiddle.Height() && rcMiddle.Width())
396 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
400 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
402 // first, let the resizing take place
403 __super::OnSize(nType, cx, cy);
405 //set range
406 SetSplitterRange();
409 void CRebaseDlg::SaveSplitterPos()
411 if (!IsIconic())
413 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
414 RECT rectSplitter;
415 m_wndSplitter.GetWindowRect(&rectSplitter);
416 ScreenToClient(&rectSplitter);
417 regPos = rectSplitter.top;
421 void CRebaseDlg::LoadBranchInfo()
423 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
424 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
426 STRING_VECTOR list;
427 list.clear();
428 int current;
429 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
430 m_BranchCtrl.AddString(list);
431 list.clear();
432 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL_F);
433 m_UpstreamCtrl.AddString(list);
435 m_BranchCtrl.SetCurSel(current);
437 AddBranchToolTips(&m_BranchCtrl);
438 AddBranchToolTips(&m_UpstreamCtrl);
440 if(!m_Upstream.IsEmpty())
442 m_UpstreamCtrl.AddString(m_Upstream);
443 m_UpstreamCtrl.SetCurSel(m_UpstreamCtrl.GetCount()-1);
445 else
447 //Select pull-remote from current branch
448 CString currentBranch = g_Git.GetSymbolicRef();
449 CString configName;
450 configName.Format(L"branch.%s.remote", currentBranch);
451 CString pullRemote = g_Git.GetConfigValue(configName);
453 //Select pull-branch from current branch
454 configName.Format(L"branch.%s.merge", currentBranch);
455 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
457 CString defaultUpstream;
458 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
459 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
460 if(found >= 0)
461 m_UpstreamCtrl.SetCurSel(found);
465 void CRebaseDlg::OnCbnSelchangeBranch()
467 FetchLogList();
470 void CRebaseDlg::OnCbnSelchangeUpstream()
472 FetchLogList();
475 void CRebaseDlg::FetchLogList()
477 CGitHash base,hash;
478 CString basestr;
479 CString cmd;
480 m_IsFastForward=FALSE;
481 cmd.Format(_T("git.exe merge-base %s %s"), g_Git.FixBranchName(m_UpstreamCtrl.GetString()),
482 g_Git.FixBranchName(m_BranchCtrl.GetString()));
483 if(g_Git.Run(cmd,&basestr,CP_ACP))
485 CMessageBox::Show(NULL,basestr,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
486 return;
488 base=basestr;
490 hash=g_Git.GetHash(m_BranchCtrl.GetString());
492 if(hash == g_Git.GetHash(this->m_UpstreamCtrl.GetString()))
494 m_CommitList.Clear();
495 CString text,fmt;
496 fmt.LoadString(IDS_REBASE_EQUAL_FMT);
497 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
499 m_CommitList.ShowText(text);
500 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
501 return;
504 if(hash == base )
506 //fast forword
507 this->m_IsFastForward=TRUE;
509 m_CommitList.Clear();
510 CString text,fmt;
511 fmt.LoadString(IDS_REBASE_FASTFORWARD_FMT);
512 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString(),
513 m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
515 m_CommitList.ShowText(text);
516 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
517 SetContinueButtonText();
519 return ;
522 hash.Empty();
524 if(!this->m_bForce)
526 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
528 if( base == hash )
530 m_CommitList.Clear();
531 CString text,fmt;
532 fmt.LoadString(IDS_REBASE_UPTODATE_FMT);
533 text.Format(fmt,m_BranchCtrl.GetString());
534 m_CommitList.ShowText(text);
535 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
536 SetContinueButtonText();
537 return;
541 m_CommitList.Clear();
542 CString from,to;
543 from = m_UpstreamCtrl.GetString();
544 to = m_BranchCtrl.GetString();
545 this->m_CommitList.FillGitLog(NULL,0,&from,&to);
547 if( m_CommitList.GetItemCount() == 0 )
548 m_CommitList.ShowText(_T("Nothing to Rebase"));
550 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
552 #if 0
553 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
555 if(hash == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
557 m_CommitList.Clear();
558 m_CommitList.ShowText(_T("Nothing Rebase"));
561 #endif
563 m_tooltips.Pop();
564 AddBranchToolTips(&this->m_BranchCtrl);
565 AddBranchToolTips(&this->m_UpstreamCtrl);
567 for(int i=0;i<m_CommitList.m_logEntries.size();i++)
569 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList) = CTGitPath::LOGACTIONS_REBASE_PICK;
572 m_CommitList.Invalidate();
574 if(m_CommitList.m_IsOldFirst)
575 this->m_CurrentRebaseIndex = -1;
576 else
577 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
579 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
580 SetContinueButtonText();
583 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
585 if(pBranch)
587 CString text=pBranch->GetString();
588 CString tooltip;
590 GitRev rev;
591 rev.GetCommit(text);
593 tooltip.Format(_T("CommitHash:%s\nCommit by: %s %s\n <b>%s</b> \n %s"),
594 rev.m_CommitHash.ToString(),
595 rev.GetAuthorName(),
596 CAppUtils::FormatDateAndTime(rev.GetAuthorDate(),DATE_LONGDATE),
597 rev.GetSubject(),
598 rev.GetBody());
600 pBranch->DisableTooltip();
601 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
605 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
607 if (pMsg->message == WM_KEYDOWN)
609 switch (pMsg->wParam)
611 case ' ':
612 if(LogListHasFocus(pMsg->hwnd))
614 m_CommitList.ShiftSelectedAction();
615 return TRUE;
617 break;
618 case 'P':
619 if(LogListHasFocus(pMsg->hwnd))
621 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_PICK);
622 return TRUE;
624 break;
625 case 'S':
626 if(LogListHasFocus(pMsg->hwnd))
628 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SKIP);
629 return TRUE;
631 break;
632 case 'Q':
633 if(LogListHasFocus(pMsg->hwnd))
635 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
636 return TRUE;
638 break;
639 case 'E':
640 if(LogListHasFocus(pMsg->hwnd))
642 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
643 return TRUE;
645 break;
646 case 'A':
647 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
649 // select all entries
650 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
652 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
654 return TRUE;
656 break;
657 case VK_F5:
659 Refresh();
660 return TRUE;
662 break;
663 /* Avoid TAB control destroy but dialog exist*/
664 case VK_ESCAPE:
665 case VK_CANCEL:
667 TCHAR buff[128];
668 ::GetClassName(pMsg->hwnd,buff,128);
671 if(_tcsnicmp(buff,_T("RichEdit20W"),128)==0 ||
672 _tcsnicmp(buff,_T("Scintilla"),128)==0 ||
673 _tcsnicmp(buff,_T("SysListView32"),128)==0||
674 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
676 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
677 return TRUE;
682 m_tooltips.RelayEvent(pMsg);
683 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
686 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
688 TCHAR buff[128];
689 ::GetClassName(hwnd, buff, 128);
691 if(_tcsnicmp(buff, _T("SysListView32"), 128) == 0)
692 return true;
693 return false;
696 int CRebaseDlg::CheckRebaseCondition()
698 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
700 if( !g_Git.CheckCleanWorkTree() )
702 if(CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
704 CString cmd,out;
705 cmd=_T("git.exe stash");
706 this->AddLogString(cmd);
707 if(g_Git.Run(cmd,&out,CP_ACP))
709 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
710 return -1;
713 }else
714 return -1;
716 //Todo Check $REBASE_ROOT
717 //Todo Check $DOTEST
719 CString cmd;
720 cmd=_T("git.exe var GIT_COMMITTER_IDENT");
721 if(g_Git.Run(cmd,NULL,CP_UTF8))
722 return -1;
724 //Todo call pre_rebase_hook
725 return 0;
727 int CRebaseDlg::StartRebase()
729 CString cmd,out;
730 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
731 if(!this->m_IsCherryPick)
733 //Todo call comment_for_reflog
734 cmd.Format(_T("git.exe checkout %s"),this->m_BranchCtrl.GetString());
735 this->AddLogString(cmd);
737 if(g_Git.Run(cmd,&out,CP_UTF8))
738 return -1;
740 this->AddLogString(out);
743 cmd=_T("git.exe rev-parse --verify HEAD");
744 if(g_Git.Run(cmd,&out,CP_UTF8))
746 AddLogString(_T("No Head"));
747 return -1;
749 //Todo
750 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
751 // echo "detached HEAD" > "$DOTEST"/head-name
753 cmd.Format(_T("git.exe update-ref ORIG_HEAD HEAD"));
754 if(g_Git.Run(cmd,&out,CP_UTF8))
756 AddLogString(_T("update ORIG_HEAD Fail"));
757 return -1;
760 m_OrigUpstreamHash.Empty();
761 m_OrigUpstreamHash= g_Git.GetHash(this->m_UpstreamCtrl.GetString());
762 if(m_OrigUpstreamHash.IsEmpty())
764 this->AddLogString(m_OrigUpstreamHash);
765 return -1;
768 if( !this->m_IsCherryPick )
770 cmd.Format(_T("git.exe checkout -f %s"), m_OrigUpstreamHash.ToString());
771 this->AddLogString(cmd);
773 out.Empty();
774 if(g_Git.Run(cmd,&out,CP_UTF8))
776 this->AddLogString(out);
777 return -1;
781 if( !this->m_IsCherryPick )
783 m_OrigBranchHash = g_Git.GetHash(this->m_BranchCtrl.GetString());
784 if(m_OrigBranchHash.IsEmpty())
786 this->AddLogString(m_OrigBranchHash.ToString());
787 return -1;
789 this->AddLogString(_T("Start Rebase\r\n"));
791 }else
792 this->AddLogString(_T("Start Cherry-pick\r\n"));
794 return 0;
796 int CRebaseDlg::VerifyNoConflict()
798 CTGitPathList list;
799 if(g_Git.ListConflictFile(list))
801 AddLogString(_T("Get conflict files fail"));
802 return -1;
804 if( list.GetCount() != 0 )
806 CMessageBox::Show(NULL,_T("There are conflict file, you should mark it resolve"),_T("TortoiseGit"),MB_OK);
807 return -1;
809 return 0;
812 int CRebaseDlg::FinishRebase()
814 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
815 return 0;
817 git_revnum_t head = g_Git.GetHash(_T("HEAD"));
818 CString out,cmd;
820 out.Empty();
821 cmd.Format(_T("git.exe checkout -f %s"),this->m_BranchCtrl.GetString());
822 AddLogString(cmd);
823 if(g_Git.Run(cmd,&out,CP_UTF8))
825 AddLogString(out);
826 return -1;
828 AddLogString(out);
830 out.Empty();
831 cmd.Format(_T("git.exe reset --hard %s"),head);
832 AddLogString(cmd);
833 if(g_Git.Run(cmd,&out,CP_UTF8))
835 AddLogString(out);
836 return -1;
838 AddLogString(out);
840 m_ctrlTabCtrl.RemoveTab(0);
841 m_ctrlTabCtrl.RemoveTab(0);
842 m_CtrlStatusText.SetWindowText(_T("Finished rebasing."));
844 return 0;
846 void CRebaseDlg::OnBnClickedContinue()
848 if( m_RebaseStage == REBASE_DONE)
850 OnOK();
851 return;
854 if( this->m_IsFastForward )
856 CString cmd,out;
857 CString oldbranch = g_Git.GetCurrentBranch();
858 if( oldbranch != m_BranchCtrl.GetString() )
860 cmd.Format(_T("git.exe checkout %s"),m_BranchCtrl.GetString());
861 AddLogString(cmd);
862 if( g_Git.Run(cmd,&out,CP_ACP) )
864 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
865 AddLogString(out);
866 return;
869 AddLogString(out);
870 out.Empty();
871 m_OrigBranchHash = g_Git.GetHash(m_BranchCtrl.GetString());
872 m_OrigUpstreamHash = g_Git.GetHash(this->m_UpstreamCtrl.GetString());
874 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
876 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
877 AddLogString(_T("No fast forward\r\nMaybe repository changed"));
878 return;
881 cmd.Format(_T("git.exe reset --hard %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
882 this->AddLogString(CString(_T("Fast forward to "))+m_UpstreamCtrl.GetString());
884 AddLogString(cmd);
885 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
886 if(g_Git.Run(cmd,&out,CP_ACP))
888 AddLogString(_T("Fail"));
889 AddLogString(out);
890 return;
892 AddLogString(out);
893 AddLogString(_T("Done"));
894 m_RebaseStage = REBASE_DONE;
895 UpdateCurrentStatus();
896 return;
899 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
901 if(CheckRebaseCondition())
902 return ;
903 m_RebaseStage = REBASE_START;
904 m_FileListCtrl.Clear();
905 m_FileListCtrl.m_CurrentVersion = L"";
906 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, _T("Conflict Files"));
907 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase,_T("Log"),2);
910 if( m_RebaseStage == REBASE_FINISH )
912 if(FinishRebase())
913 return ;
915 OnOK();
918 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
920 if(VerifyNoConflict())
921 return;
922 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
923 if(this->CheckNextCommitIsSquash())
924 {//next commit is not squash;
925 m_RebaseStage = REBASE_SQUASH_EDIT;
926 this->OnRebaseUpdateUI(0,0);
927 this->UpdateCurrentStatus();
928 return ;
931 m_RebaseStage=REBASE_CONTINUE;
932 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
933 this->UpdateCurrentStatus();
937 if( m_RebaseStage == REBASE_CONFLICT )
939 if(VerifyNoConflict())
940 return;
942 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
944 CString out =_T("");
945 CString cmd;
946 cmd.Format(_T("git.exe commit -a -C %s"), curRev->m_CommitHash.ToString());
948 AddLogString(cmd);
950 if(g_Git.Run(cmd,&out,CP_UTF8))
952 AddLogString(out);
953 if(!g_Git.CheckCleanWorkTree())
955 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
956 return;
960 AddLogString(out);
961 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
962 if( curRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_EDIT)
964 m_RebaseStage=REBASE_EDIT;
965 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
966 this->UpdateCurrentStatus();
967 return;
969 else
971 m_RebaseStage=REBASE_CONTINUE;
972 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
973 this->UpdateCurrentStatus();
977 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
979 CString str;
980 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
982 str=this->m_LogMessageCtrl.GetText();
983 if(str.Trim().IsEmpty())
985 CMessageBox::Show(NULL,_T("Commit Message Is Empty"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
986 return;
989 CString tempfile=::GetTempFile();
990 CFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
991 CStringA log=CUnicodeUtils::GetUTF8( str);
992 file.Write(log,log.GetLength());
993 //file.WriteString(m_sLogMessage);
994 file.Close();
996 CString out,cmd;
998 if( m_RebaseStage == REBASE_SQUASH_EDIT )
999 cmd.Format(_T("git.exe commit -F \"%s\""), tempfile);
1000 else
1001 cmd.Format(_T("git.exe commit --amend -F \"%s\""), tempfile);
1003 if(g_Git.Run(cmd,&out,CP_UTF8))
1005 if(!g_Git.CheckCleanWorkTree())
1007 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1008 return;
1012 CFile::Remove(tempfile);
1013 AddLogString(out);
1014 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1015 m_RebaseStage=REBASE_CONTINUE;
1016 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1017 this->UpdateCurrentStatus();
1021 InterlockedExchange(&m_bThreadRunning, TRUE);
1022 SetControlEnable();
1024 if (AfxBeginThread(RebaseThreadEntry, this)==NULL)
1026 InterlockedExchange(&m_bThreadRunning, FALSE);
1027 CMessageBox::Show(NULL, _T("Create Rebase Thread Fail"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1028 SetControlEnable();
1031 int CRebaseDlg::CheckNextCommitIsSquash()
1033 int index;
1034 if(m_CommitList.m_IsOldFirst)
1035 index=m_CurrentRebaseIndex+1;
1036 else
1037 index=m_CurrentRebaseIndex-1;
1039 GitRev *curRev;
1042 if(index<0)
1043 return -1;
1044 if(index>= m_CommitList.GetItemCount())
1045 return -1;
1047 curRev=(GitRev*)m_CommitList.m_arShownList[index];
1049 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SQUASH )
1050 return 0;
1051 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP)
1053 if(m_CommitList.m_IsOldFirst)
1054 index++;
1055 else
1056 index--;
1057 }else
1058 return -1;
1060 }while(curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP);
1062 return -1;
1065 int CRebaseDlg::GoNext()
1067 if(m_CommitList.m_IsOldFirst)
1068 m_CurrentRebaseIndex++;
1069 else
1070 m_CurrentRebaseIndex--;
1071 return 0;
1074 int CRebaseDlg::StateAction()
1076 switch(this->m_RebaseStage)
1078 case CHOOSE_BRANCH:
1079 case CHOOSE_COMMIT_PICK_MODE:
1080 if(StartRebase())
1081 return -1;
1082 m_RebaseStage = REBASE_START;
1083 GoNext();
1084 break;
1087 return 0;
1089 void CRebaseDlg::SetContinueButtonText()
1091 CString Text;
1092 switch(this->m_RebaseStage)
1094 case CHOOSE_BRANCH:
1095 case CHOOSE_COMMIT_PICK_MODE:
1096 if(this->m_IsFastForward)
1097 Text = _T("Start(FastFwd)");
1098 else
1099 Text = _T("Start");
1100 break;
1102 case REBASE_START:
1103 case REBASE_CONTINUE:
1104 case REBASE_SQUASH_CONFLICT:
1105 Text = _T("Continue");
1106 break;
1108 case REBASE_CONFLICT:
1109 Text = _T("Commit");
1110 break;
1111 case REBASE_EDIT:
1112 Text = _T("Amend");
1113 break;
1115 case REBASE_SQUASH_EDIT:
1116 Text = _T("Commit");
1117 break;
1119 case REBASE_ABORT:
1120 case REBASE_FINISH:
1121 Text = _T("Finish");
1122 break;
1124 case REBASE_DONE:
1125 Text = _T("Done");
1126 break;
1128 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1131 void CRebaseDlg::SetControlEnable()
1133 switch(this->m_RebaseStage)
1135 case CHOOSE_BRANCH:
1136 case CHOOSE_COMMIT_PICK_MODE:
1138 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(TRUE);
1139 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(TRUE);
1140 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(TRUE);
1141 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(TRUE);
1142 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(TRUE);
1144 if(!m_IsCherryPick)
1146 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1147 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1148 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1150 //this->m_CommitList.m_IsEnableRebaseMenu=TRUE;
1151 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1152 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1153 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1154 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP);
1155 break;
1157 case REBASE_START:
1158 case REBASE_CONTINUE:
1159 case REBASE_ABORT:
1160 case REBASE_FINISH:
1161 case REBASE_CONFLICT:
1162 case REBASE_EDIT:
1163 case REBASE_SQUASH_CONFLICT:
1164 case REBASE_DONE:
1165 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(FALSE);
1166 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(FALSE);
1167 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(FALSE);
1168 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1169 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1170 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1171 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(FALSE);
1172 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(FALSE);
1173 //this->m_CommitList.m_IsEnableRebaseMenu=FALSE;
1174 this->m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1175 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1176 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1177 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP));
1179 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1181 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1182 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1183 this->m_PostButton.RemoveAll();
1184 this->m_PostButton.AddEntries(m_PostButtonTexts);
1185 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1187 break;
1190 if(m_bThreadRunning)
1192 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1194 }else
1196 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1200 void CRebaseDlg::UpdateProgress()
1202 int index;
1203 CRect rect;
1205 if(m_CommitList.m_IsOldFirst)
1206 index = m_CurrentRebaseIndex+1;
1207 else
1208 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1210 m_ProgressBar.SetRange(1,m_CommitList.GetItemCount());
1211 m_ProgressBar.SetPos(index);
1212 if (m_pTaskbarList)
1214 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1215 m_pTaskbarList->SetProgressValue(m_hWnd, index, m_CommitList.GetItemCount());
1218 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1220 CString text;
1221 text.Format(_T("Rebasing... (%d/%d)"),index,m_CommitList.GetItemCount());
1222 m_CtrlStatusText.SetWindowText(text);
1226 GitRev *prevRev=NULL, *curRev=NULL;
1228 if( m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex< m_CommitList.m_arShownList.GetSize())
1230 curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1233 for(int i=0;i<m_CommitList.m_arShownList.GetSize();i++)
1235 prevRev=(GitRev*)m_CommitList.m_arShownList[i];
1236 if(prevRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_CURRENT)
1238 prevRev->GetAction(&m_CommitList) &= ~ CTGitPath::LOGACTIONS_REBASE_CURRENT;
1239 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1240 m_CommitList.InvalidateRect(rect);
1244 if(curRev)
1246 curRev->GetAction(&m_CommitList) |= CTGitPath::LOGACTIONS_REBASE_CURRENT;
1247 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1248 m_CommitList.InvalidateRect(rect);
1250 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1253 void CRebaseDlg::UpdateCurrentStatus()
1255 SetContinueButtonText();
1256 SetControlEnable();
1257 UpdateProgress();
1260 void CRebaseDlg::AddLogString(CString str)
1262 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1263 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1264 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1265 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1266 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1267 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1270 int CRebaseDlg::GetCurrentCommitID()
1272 if(m_CommitList.m_IsOldFirst)
1274 return this->m_CurrentRebaseIndex+1;
1276 }else
1278 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1282 int CRebaseDlg::DoRebase()
1284 CString cmd,out;
1285 if(m_CurrentRebaseIndex <0)
1286 return 0;
1287 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1288 return 0;
1290 GitRev *pRev = (GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1291 int mode=pRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_MODE_MASK;
1292 CString nocommit;
1294 if( mode== CTGitPath::LOGACTIONS_REBASE_SKIP)
1296 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1297 return 0;
1300 if( mode != CTGitPath::LOGACTIONS_REBASE_PICK )
1302 this->m_SquashMessage+= pRev->GetSubject();
1303 this->m_SquashMessage+= _T("\n");
1304 this->m_SquashMessage+= pRev->GetBody();
1306 else
1307 this->m_SquashMessage.Empty();
1309 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1310 nocommit=_T(" --no-commit ");
1312 CString log;
1313 log.Format(_T("%s %d: %s"),CTGitPath::GetActionName(mode),this->GetCurrentCommitID(),pRev->m_CommitHash.ToString());
1314 AddLogString(log);
1315 AddLogString(pRev->GetSubject());
1316 if (pRev->GetSubject().IsEmpty())
1318 CMessageBox::Show(m_hWnd, _T("Found an empty commit message. You have to enter one or rebase cannot proceed."), _T("TortoiseGit"), MB_OK | MB_ICONEXCLAMATION);
1319 mode = CTGitPath::LOGACTIONS_REBASE_EDIT;
1322 cmd.Format(_T("git.exe cherry-pick %s %s"),nocommit,pRev->m_CommitHash.ToString());
1324 if(g_Git.Run(cmd,&out,CP_UTF8))
1326 AddLogString(out);
1327 CTGitPathList list;
1328 if(g_Git.ListConflictFile(list))
1330 AddLogString(_T("Get conflict files fail"));
1331 return -1;
1333 if(list.GetCount() == 0 )
1335 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1337 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1338 return 0;
1340 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1342 this->m_RebaseStage = REBASE_EDIT ;
1343 return -1; // Edit return -1 to stop rebase.
1345 // Squash Case
1346 if(CheckNextCommitIsSquash())
1347 { // no squash
1348 // let user edit last commmit message
1349 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1350 return -1;
1353 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1354 m_RebaseStage = REBASE_SQUASH_CONFLICT;
1355 else
1356 m_RebaseStage = REBASE_CONFLICT;
1357 return -1;
1359 }else
1361 AddLogString(out);
1362 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1364 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1365 return 0;
1367 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1369 this->m_RebaseStage = REBASE_EDIT ;
1370 return -1; // Edit return -1 to stop rebase.
1373 // Squash Case
1374 if(CheckNextCommitIsSquash())
1375 { // no squash
1376 // let user edit last commmit message
1377 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1378 return -1;
1380 else if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1381 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1384 return 0;
1387 BOOL CRebaseDlg::IsEnd()
1389 if(m_CommitList.m_IsOldFirst)
1390 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
1391 else
1392 return m_CurrentRebaseIndex<0;
1395 int CRebaseDlg::RebaseThread()
1397 int ret=0;
1398 while(1)
1400 if( m_RebaseStage == REBASE_START )
1402 if( this->StartRebase() )
1404 InterlockedExchange(&m_bThreadRunning, FALSE);
1405 ret = -1;
1406 break;
1408 m_RebaseStage = REBASE_CONTINUE;
1410 }else if( m_RebaseStage == REBASE_CONTINUE )
1412 this->GoNext();
1413 UpdateCurrentStatus();
1414 if(IsEnd())
1416 ret = 0;
1417 m_RebaseStage = REBASE_FINISH;
1419 }else
1421 ret = DoRebase();
1423 if( ret )
1425 break;
1429 }else if( m_RebaseStage == REBASE_FINISH )
1431 FinishRebase();
1432 m_RebaseStage = REBASE_DONE;
1433 if (m_pTaskbarList)
1434 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1435 break;
1437 }else
1439 break;
1441 this->PostMessage(MSG_REBASE_UPDATE_UI);
1442 //this->UpdateCurrentStatus();
1445 InterlockedExchange(&m_bThreadRunning, FALSE);
1446 this->PostMessage(MSG_REBASE_UPDATE_UI);
1447 return ret;
1450 void CRebaseDlg::ListConflictFile()
1452 this->m_FileListCtrl.Clear();
1453 CTGitPathList list;
1454 CTGitPath path;
1455 list.AddPath(path);
1457 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
1459 this->m_FileListCtrl.GetStatus(&list,true);
1460 this->m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED|CTGitPath::LOGACTIONS_MODIFIED|CTGitPath::LOGACTIONS_ADDED|CTGitPath::LOGACTIONS_DELETED,
1461 CTGitPath::LOGACTIONS_UNMERGED);
1464 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
1466 UpdateCurrentStatus();
1467 if(m_CurrentRebaseIndex <0)
1468 return 0;
1469 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1470 return 0;
1471 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1473 switch(m_RebaseStage)
1475 case REBASE_CONFLICT:
1476 case REBASE_SQUASH_CONFLICT:
1477 ListConflictFile();
1478 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
1479 if (m_pTaskbarList)
1480 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1481 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1482 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1483 break;
1484 case REBASE_EDIT:
1485 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1486 if (m_pTaskbarList)
1487 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1488 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1489 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1490 break;
1491 case REBASE_SQUASH_EDIT:
1492 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1493 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
1494 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1495 if (m_pTaskbarList)
1496 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1497 break;
1498 default:
1499 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1501 return 0;
1503 void CRebaseDlg::OnCancel()
1505 OnBnClickedAbort();
1507 void CRebaseDlg::OnBnClickedAbort()
1509 if (m_pTaskbarList)
1510 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1512 CString cmd,out;
1513 if(m_OrigUpstreamHash.IsEmpty())
1515 __super::OnCancel();
1518 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
1520 return;
1523 if(CMessageBox::Show(NULL,_T("Are you sure you want to abort the rebase process?"),_T("TortoiseGit"),MB_YESNO) != IDYES)
1524 return;
1526 if(this->m_IsFastForward)
1528 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1529 if(g_Git.Run(cmd,&out,CP_UTF8))
1531 AddLogString(out);
1532 return ;
1534 __super::OnCancel();
1535 return;
1537 cmd.Format(_T("git.exe checkout -f %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1538 if(g_Git.Run(cmd,&out,CP_UTF8))
1540 AddLogString(out);
1541 return ;
1544 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigUpstreamHash.ToString());
1545 if(g_Git.Run(cmd,&out,CP_UTF8))
1547 AddLogString(out);
1548 return ;
1551 if(this->m_IsCherryPick) //there are not "branch" at cherry pick mode
1553 __super::OnCancel();
1554 return;
1557 cmd.Format(_T("git checkout -f %s"),this->m_BranchCtrl.GetString());
1558 if(g_Git.Run(cmd,&out,CP_UTF8))
1560 AddLogString(out);
1561 return ;
1564 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1565 if(g_Git.Run(cmd,&out,CP_UTF8))
1567 AddLogString(out);
1568 return ;
1570 __super::OnCancel();
1573 void CRebaseDlg::OnBnClickedButtonBrowse()
1575 if(CBrowseRefsDlg::PickRefForCombo(&m_UpstreamCtrl, gPickRef_NoTag))
1576 OnCbnSelchangeUpstream();
1579 void CRebaseDlg::OnBnClickedRebaseCheckForce()
1581 this->UpdateData();
1582 this->FetchLogList();
1583 if(this->CheckRebaseCondition())
1585 /* Disable Start Rebase */
1586 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1590 void CRebaseDlg::OnBnClickedRebasePostButton()
1592 this->m_Upstream=this->m_UpstreamCtrl.GetString();
1593 this->m_Branch=this->m_BranchCtrl.GetString();
1595 this->EndDialog(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry());
1598 void CRebaseDlg::Refresh()
1600 if(this->m_IsCherryPick)
1601 return ;
1603 if(this->m_RebaseStage == CHOOSE_BRANCH )
1605 this->UpdateData();
1606 this->FetchLogList();
1607 if(this->CheckRebaseCondition())
1609 /* Disable Start Rebase */
1610 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1615 void CRebaseDlg::OnBnClickedButtonUp2()
1617 POSITION pos;
1618 pos = m_CommitList.GetFirstSelectedItemPosition();
1620 // do nothing if the first selected item is the first item in the list
1621 if (m_CommitList.GetNextSelectedItem(pos) == 0)
1622 return;
1624 pos = m_CommitList.GetFirstSelectedItemPosition();
1626 bool changed = false;
1627 while(pos)
1629 int index=m_CommitList.GetNextSelectedItem(pos);
1630 if(index>=1)
1632 CGitHash old = m_CommitList.m_logEntries[index-1];
1633 m_CommitList.m_logEntries[index-1] = m_CommitList.m_logEntries[index];
1634 m_CommitList.m_logEntries[index] = old;
1635 m_CommitList.SetItemState(index-1, LVIS_SELECTED, LVIS_SELECTED);
1636 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1637 changed = true;
1640 if (changed)
1642 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1643 m_CommitList.Invalidate();
1644 m_CommitList.SetFocus();
1648 void CRebaseDlg::OnBnClickedButtonDown2()
1650 if (m_CommitList.GetSelectedCount() == 0)
1651 return;
1653 POSITION pos;
1654 pos = m_CommitList.GetFirstSelectedItemPosition();
1655 bool changed = false;
1656 // use an array to store all selected item indexes; the user won't select too much items
1657 int* indexes = NULL;
1658 indexes = new int[m_CommitList.GetSelectedCount()];
1659 int i = 0;
1660 while(pos)
1662 indexes[i++] = m_CommitList.GetNextSelectedItem(pos);
1664 // don't move any item if the last selected item is the last item in the m_CommitList
1665 // (that would change the order of the selected items)
1666 if(indexes[m_CommitList.GetSelectedCount() - 1] < m_CommitList.GetItemCount() - 1)
1668 // iterate over the indexes backwards in order to correctly move multiselected items
1669 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
1671 int index = indexes[i];
1672 CGitHash old = m_CommitList.m_logEntries[index+1];
1673 m_CommitList.m_logEntries[index+1] = m_CommitList.m_logEntries[index];
1674 m_CommitList.m_logEntries[index] = old;
1675 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1676 m_CommitList.SetItemState(index+1, LVIS_SELECTED, LVIS_SELECTED);
1677 changed = true;
1680 delete [] indexes;
1681 indexes = NULL;
1682 if (changed)
1684 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1685 m_CommitList.Invalidate();
1686 m_CommitList.SetFocus();
1690 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1692 m_pTaskbarList.Release();
1693 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1694 return 0;
1697 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
1699 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
1700 *pResult = 0;
1701 if(m_CommitList.m_bNoDispUpdates)
1702 return;
1703 if (pNMLV->iItem >= 0)
1705 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
1706 if (pNMLV->iSubItem != 0)
1707 return;
1708 if ((pNMLV->iItem == m_CommitList.m_arShownList.GetCount()))
1710 // remove the selected state
1711 if (pNMLV->uChanged & LVIF_STATE)
1713 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
1714 FillLogMessageCtrl();
1716 return;
1718 if (pNMLV->uChanged & LVIF_STATE)
1720 FillLogMessageCtrl();
1723 else
1725 FillLogMessageCtrl();
1729 void CRebaseDlg::FillLogMessageCtrl()
1731 int selCount = m_CommitList.GetSelectedCount();
1732 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
1734 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
1735 int selIndex = m_CommitList.GetNextSelectedItem(pos);
1736 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_CommitList.m_arShownList.SafeGetAt(selIndex));
1737 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
1738 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
1739 m_FileListCtrl.Show(SVNSLC_SHOWVERSIONED);
1740 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1741 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + _T("\n") + pLogEntry->GetBody());
1742 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);