TortoiseSVN -> TortoiseGit
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
bloba1e362d93109f7562a9d8614519880b2e1be90dc
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();
198 EnableSaveRestore(_T("RebaseDlg"));
200 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
201 RECT rcDlg, rcLogMsg, rcFileList;
202 GetClientRect(&rcDlg);
203 m_CommitList.GetWindowRect(&rcLogMsg);
204 ScreenToClient(&rcLogMsg);
205 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
206 ScreenToClient(&rcFileList);
207 if (yPos)
209 RECT rectSplitter;
210 m_wndSplitter.GetWindowRect(&rectSplitter);
211 ScreenToClient(&rectSplitter);
212 int delta = yPos - rectSplitter.top;
213 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
215 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
216 DoSize(delta);
220 if( this->m_RebaseStage == CHOOSE_BRANCH)
222 this->LoadBranchInfo();
224 }else
226 this->m_BranchCtrl.EnableWindow(FALSE);
227 this->m_UpstreamCtrl.EnableWindow(FALSE);
230 m_CommitList.m_ColumnRegKey = _T("Rebase");
231 m_CommitList.m_IsIDReplaceAction = TRUE;
232 // m_CommitList.m_IsOldFirst = TRUE;
233 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
235 m_CommitList.InsertGitColumn();
237 this->SetControlEnable();
239 if(!this->m_PreCmd.IsEmpty())
241 CProgressDlg progress;
242 progress.m_GitCmd=m_PreCmd;
243 progress.m_bAutoCloseOnSuccess=true;
244 progress.DoModal();
247 if(m_IsCherryPick)
249 this->m_BranchCtrl.SetCurSel(-1);
250 this->m_BranchCtrl.EnableWindow(FALSE);
251 this->m_UpstreamCtrl.EnableWindow(FALSE);
252 this->SetWindowText(_T("Cherry Pick"));
253 this->m_CommitList.StartFilter();
255 }else
257 SetContinueButtonText();
258 m_CommitList.DeleteAllItems();
259 FetchLogList();
262 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
263 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
264 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
265 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
266 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
267 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
268 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
270 if(m_CommitList.m_IsOldFirst)
271 this->m_CurrentRebaseIndex = -1;
272 else
273 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
276 if(this->CheckRebaseCondition())
278 /* Disable Start Rebase */
279 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
282 return TRUE;
284 // CRebaseDlg message handlers
286 void CRebaseDlg::OnBnClickedPickAll()
288 this->UpdateData();
289 if(this->m_bPickAll)
290 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_PICK);
292 this->m_bEditAll=FALSE;
293 this->m_bSquashAll=FALSE;
294 this->UpdateData(FALSE);
297 void CRebaseDlg::OnBnClickedSquashAll()
299 this->UpdateData();
300 if(this->m_bSquashAll)
301 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
303 this->m_bEditAll=FALSE;
304 this->m_bPickAll=FALSE;
305 this->UpdateData(FALSE);
308 void CRebaseDlg::OnBnClickedEditAll()
310 this->UpdateData();
311 if( this->m_bEditAll )
312 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
314 this->m_bPickAll=FALSE;
315 this->m_bSquashAll=FALSE;
316 this->UpdateData(FALSE);
319 void CRebaseDlg::SetAllRebaseAction(int action)
321 for(int i=0;i<this->m_CommitList.m_logEntries.size();i++)
323 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList)=action;
325 m_CommitList.Invalidate();
328 void CRebaseDlg::OnBnClickedRebaseSplit()
330 this->UpdateData();
333 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
335 switch (message) {
336 case WM_NOTIFY:
337 if (wParam == IDC_REBASE_SPLIT)
339 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
340 DoSize(pHdr->delta);
342 break;
345 return __super::DefWindowProc(message, wParam, lParam);
348 void CRebaseDlg::DoSize(int delta)
350 this->RemoveAllAnchors();
352 CSplitterControl::ChangeHeight(GetDlgItem(IDC_COMMIT_LIST), delta, CW_TOPALIGN);
353 //CSplitterControl::ChangeHeight(GetDlgItem(), delta, CW_TOPALIGN);
354 CSplitterControl::ChangeHeight(GetDlgItem(IDC_REBASE_TAB), -delta, CW_BOTTOMALIGN);
355 //CSplitterControl::ChangeHeight(GetDlgItem(), -delta, CW_BOTTOMALIGN);
356 CSplitterControl::ChangePos(GetDlgItem(IDC_SQUASH_ALL),0,delta);
357 CSplitterControl::ChangePos(GetDlgItem(IDC_PICK_ALL),0,delta);
358 CSplitterControl::ChangePos(GetDlgItem(IDC_EDIT_ALL),0,delta);
359 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_UP2),0,delta);
360 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_DOWN2),0,delta);
361 CSplitterControl::ChangePos(GetDlgItem(IDC_REBASE_CHECK_FORCE),0,delta);
363 this->AddRebaseAnchor();
364 // adjust the minimum size of the dialog to prevent the resizing from
365 // moving the list control too far down.
366 CRect rcLogMsg;
367 m_CommitList.GetClientRect(rcLogMsg);
368 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
370 SetSplitterRange();
371 // m_CommitList.Invalidate();
373 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
375 this->m_ctrlTabCtrl.Invalidate();
376 this->m_CommitList.Invalidate();
377 this->m_FileListCtrl.Invalidate();
378 this->m_LogMessageCtrl.Invalidate();
382 void CRebaseDlg::SetSplitterRange()
384 if ((m_CommitList)&&(m_ctrlTabCtrl))
386 CRect rcTop;
387 m_CommitList.GetWindowRect(rcTop);
388 ScreenToClient(rcTop);
389 CRect rcMiddle;
390 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
391 ScreenToClient(rcMiddle);
392 if (rcMiddle.Height() && rcMiddle.Width())
393 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
397 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
399 // first, let the resizing take place
400 __super::OnSize(nType, cx, cy);
402 //set range
403 SetSplitterRange();
406 void CRebaseDlg::SaveSplitterPos()
408 if (!IsIconic())
410 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
411 RECT rectSplitter;
412 m_wndSplitter.GetWindowRect(&rectSplitter);
413 ScreenToClient(&rectSplitter);
414 regPos = rectSplitter.top;
418 void CRebaseDlg::LoadBranchInfo()
420 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
421 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
423 STRING_VECTOR list;
424 list.clear();
425 int current;
426 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
427 m_BranchCtrl.AddString(list);
428 list.clear();
429 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL_F);
430 m_UpstreamCtrl.AddString(list);
432 m_BranchCtrl.SetCurSel(current);
434 AddBranchToolTips(&m_BranchCtrl);
435 AddBranchToolTips(&m_UpstreamCtrl);
437 if(!m_Upstream.IsEmpty())
439 m_UpstreamCtrl.AddString(m_Upstream);
440 m_UpstreamCtrl.SetCurSel(m_UpstreamCtrl.GetCount()-1);
442 else
444 //Select pull-remote from current branch
445 CString currentBranch = g_Git.GetSymbolicRef();
446 CString configName;
447 configName.Format(L"branch.%s.remote", currentBranch);
448 CString pullRemote = g_Git.GetConfigValue(configName);
450 //Select pull-branch from current branch
451 configName.Format(L"branch.%s.merge", currentBranch);
452 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
454 CString defaultUpstream;
455 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
456 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
457 if(found >= 0)
458 m_UpstreamCtrl.SetCurSel(found);
462 void CRebaseDlg::OnCbnSelchangeBranch()
464 FetchLogList();
467 void CRebaseDlg::OnCbnSelchangeUpstream()
469 FetchLogList();
472 void CRebaseDlg::FetchLogList()
474 CGitHash base,hash;
475 CString basestr;
476 CString cmd;
477 m_IsFastForward=FALSE;
478 cmd.Format(_T("git.exe merge-base %s %s"), g_Git.FixBranchName(m_UpstreamCtrl.GetString()),
479 g_Git.FixBranchName(m_BranchCtrl.GetString()));
480 if(g_Git.Run(cmd,&basestr,CP_ACP))
482 CMessageBox::Show(NULL,basestr,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
483 return;
485 base=basestr;
487 hash=g_Git.GetHash(m_BranchCtrl.GetString());
489 if(hash == g_Git.GetHash(this->m_UpstreamCtrl.GetString()))
491 m_CommitList.Clear();
492 CString text,fmt;
493 fmt.LoadString(IDS_REBASE_EQUAL_FMT);
494 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
496 m_CommitList.ShowText(text);
497 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
498 return;
501 if(hash == base )
503 //fast forword
504 this->m_IsFastForward=TRUE;
506 m_CommitList.Clear();
507 CString text,fmt;
508 fmt.LoadString(IDS_REBASE_FASTFORWARD_FMT);
509 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString(),
510 m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
512 m_CommitList.ShowText(text);
513 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
514 SetContinueButtonText();
516 return ;
519 hash.Empty();
521 if(!this->m_bForce)
523 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
525 if( base == hash )
527 m_CommitList.Clear();
528 CString text,fmt;
529 fmt.LoadString(IDS_REBASE_UPTODATE_FMT);
530 text.Format(fmt,m_BranchCtrl.GetString());
531 m_CommitList.ShowText(text);
532 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
533 SetContinueButtonText();
534 return;
538 m_CommitList.Clear();
539 CString from,to;
540 from = m_UpstreamCtrl.GetString();
541 to = m_BranchCtrl.GetString();
542 this->m_CommitList.FillGitLog(NULL,0,&from,&to);
544 if( m_CommitList.GetItemCount() == 0 )
545 m_CommitList.ShowText(_T("Nothing to Rebase"));
547 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
549 #if 0
550 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
552 if(hash == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
554 m_CommitList.Clear();
555 m_CommitList.ShowText(_T("Nothing Rebase"));
558 #endif
560 m_tooltips.Pop();
561 AddBranchToolTips(&this->m_BranchCtrl);
562 AddBranchToolTips(&this->m_UpstreamCtrl);
564 for(int i=0;i<m_CommitList.m_logEntries.size();i++)
566 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList) = CTGitPath::LOGACTIONS_REBASE_PICK;
569 m_CommitList.Invalidate();
571 if(m_CommitList.m_IsOldFirst)
572 this->m_CurrentRebaseIndex = -1;
573 else
574 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
576 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
577 SetContinueButtonText();
580 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
582 if(pBranch)
584 CString text=pBranch->GetString();
585 CString tooltip;
587 GitRev rev;
588 rev.GetCommit(text);
590 tooltip.Format(_T("CommitHash:%s\nCommit by: %s %s\n <b>%s</b> \n %s"),
591 rev.m_CommitHash.ToString(),
592 rev.GetAuthorName(),
593 CAppUtils::FormatDateAndTime(rev.GetAuthorDate(),DATE_LONGDATE),
594 rev.GetSubject(),
595 rev.GetBody());
597 pBranch->DisableTooltip();
598 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
602 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
604 if (pMsg->message == WM_KEYDOWN)
606 switch (pMsg->wParam)
608 case ' ':
609 if(LogListHasFocus(pMsg->hwnd))
611 m_CommitList.ShiftSelectedAction();
612 return TRUE;
614 break;
615 case 'P':
616 if(LogListHasFocus(pMsg->hwnd))
618 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_PICK);
619 return TRUE;
621 break;
622 case 'S':
623 if(LogListHasFocus(pMsg->hwnd))
625 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SKIP);
626 return TRUE;
628 break;
629 case 'Q':
630 if(LogListHasFocus(pMsg->hwnd))
632 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
633 return TRUE;
635 break;
636 case 'E':
637 if(LogListHasFocus(pMsg->hwnd))
639 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
640 return TRUE;
642 break;
643 case 'A':
644 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
646 // select all entries
647 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
649 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
651 return TRUE;
653 break;
654 case VK_F5:
656 Refresh();
657 return TRUE;
659 break;
660 /* Avoid TAB control destroy but dialog exist*/
661 case VK_ESCAPE:
662 case VK_CANCEL:
664 TCHAR buff[128];
665 ::GetClassName(pMsg->hwnd,buff,128);
668 if(_tcsnicmp(buff,_T("RichEdit20W"),128)==0 ||
669 _tcsnicmp(buff,_T("Scintilla"),128)==0 ||
670 _tcsnicmp(buff,_T("SysListView32"),128)==0||
671 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
673 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
674 return TRUE;
679 m_tooltips.RelayEvent(pMsg);
680 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
683 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
685 TCHAR buff[128];
686 ::GetClassName(hwnd, buff, 128);
688 if(_tcsnicmp(buff, _T("SysListView32"), 128) == 0)
689 return true;
690 return false;
693 int CRebaseDlg::CheckRebaseCondition()
695 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
697 if( !g_Git.CheckCleanWorkTree() )
699 if(CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
701 CString cmd,out;
702 cmd=_T("git.exe stash");
703 this->AddLogString(cmd);
704 if(g_Git.Run(cmd,&out,CP_ACP))
706 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
707 return -1;
710 }else
711 return -1;
713 //Todo Check $REBASE_ROOT
714 //Todo Check $DOTEST
716 CString cmd;
717 cmd=_T("git.exe var GIT_COMMITTER_IDENT");
718 if(g_Git.Run(cmd,NULL,CP_UTF8))
719 return -1;
721 //Todo call pre_rebase_hook
722 return 0;
724 int CRebaseDlg::StartRebase()
726 CString cmd,out;
727 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
728 if(!this->m_IsCherryPick)
730 //Todo call comment_for_reflog
731 cmd.Format(_T("git.exe checkout %s"),this->m_BranchCtrl.GetString());
732 this->AddLogString(cmd);
734 if(g_Git.Run(cmd,&out,CP_UTF8))
735 return -1;
737 this->AddLogString(out);
740 cmd=_T("git.exe rev-parse --verify HEAD");
741 if(g_Git.Run(cmd,&out,CP_UTF8))
743 AddLogString(_T("No Head"));
744 return -1;
746 //Todo
747 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
748 // echo "detached HEAD" > "$DOTEST"/head-name
750 cmd.Format(_T("git.exe update-ref ORIG_HEAD HEAD"));
751 if(g_Git.Run(cmd,&out,CP_UTF8))
753 AddLogString(_T("update ORIG_HEAD Fail"));
754 return -1;
757 m_OrigUpstreamHash.Empty();
758 m_OrigUpstreamHash= g_Git.GetHash(this->m_UpstreamCtrl.GetString());
759 if(m_OrigUpstreamHash.IsEmpty())
761 this->AddLogString(m_OrigUpstreamHash);
762 return -1;
765 if( !this->m_IsCherryPick )
767 cmd.Format(_T("git.exe checkout -f %s"), m_OrigUpstreamHash.ToString());
768 this->AddLogString(cmd);
770 out.Empty();
771 if(g_Git.Run(cmd,&out,CP_UTF8))
773 this->AddLogString(out);
774 return -1;
778 if( !this->m_IsCherryPick )
780 m_OrigBranchHash = g_Git.GetHash(this->m_BranchCtrl.GetString());
781 if(m_OrigBranchHash.IsEmpty())
783 this->AddLogString(m_OrigBranchHash.ToString());
784 return -1;
786 this->AddLogString(_T("Start Rebase\r\n"));
788 }else
789 this->AddLogString(_T("Start Cherry-pick\r\n"));
791 return 0;
793 int CRebaseDlg::VerifyNoConflict()
795 CTGitPathList list;
796 if(g_Git.ListConflictFile(list))
798 AddLogString(_T("Get conflict files fail"));
799 return -1;
801 if( list.GetCount() != 0 )
803 CMessageBox::Show(NULL,_T("There are conflict file, you should mark it resolve"),_T("TortoiseGit"),MB_OK);
804 return -1;
806 return 0;
809 int CRebaseDlg::FinishRebase()
811 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
812 return 0;
814 git_revnum_t head = g_Git.GetHash(_T("HEAD"));
815 CString out,cmd;
817 out.Empty();
818 cmd.Format(_T("git.exe checkout -f %s"),this->m_BranchCtrl.GetString());
819 AddLogString(cmd);
820 if(g_Git.Run(cmd,&out,CP_UTF8))
822 AddLogString(out);
823 return -1;
825 AddLogString(out);
827 out.Empty();
828 cmd.Format(_T("git.exe reset --hard %s"),head);
829 AddLogString(cmd);
830 if(g_Git.Run(cmd,&out,CP_UTF8))
832 AddLogString(out);
833 return -1;
835 AddLogString(out);
837 m_ctrlTabCtrl.RemoveTab(0);
838 m_ctrlTabCtrl.RemoveTab(0);
839 m_CtrlStatusText.SetWindowText(_T("Finished rebasing."));
841 return 0;
843 void CRebaseDlg::OnBnClickedContinue()
845 if( m_RebaseStage == REBASE_DONE)
847 OnOK();
848 return;
851 if( this->m_IsFastForward )
853 CString cmd,out;
854 CString oldbranch = g_Git.GetCurrentBranch();
855 if( oldbranch != m_BranchCtrl.GetString() )
857 cmd.Format(_T("git.exe checkout %s"),m_BranchCtrl.GetString());
858 AddLogString(cmd);
859 if( g_Git.Run(cmd,&out,CP_ACP) )
861 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
862 AddLogString(out);
863 return;
866 AddLogString(out);
867 out.Empty();
868 m_OrigBranchHash = g_Git.GetHash(m_BranchCtrl.GetString());
869 m_OrigUpstreamHash = g_Git.GetHash(this->m_UpstreamCtrl.GetString());
871 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
873 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
874 AddLogString(_T("No fast forward\r\nMaybe repository changed"));
875 return;
878 cmd.Format(_T("git.exe reset --hard %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
879 this->AddLogString(CString(_T("Fast forward to "))+m_UpstreamCtrl.GetString());
881 AddLogString(cmd);
882 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
883 if(g_Git.Run(cmd,&out,CP_ACP))
885 AddLogString(_T("Fail"));
886 AddLogString(out);
887 return;
889 AddLogString(out);
890 AddLogString(_T("Done"));
891 m_RebaseStage = REBASE_DONE;
892 UpdateCurrentStatus();
893 return;
896 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
898 if(CheckRebaseCondition())
899 return ;
900 m_RebaseStage = REBASE_START;
901 m_FileListCtrl.Clear();
902 m_FileListCtrl.m_CurrentVersion = L"";
903 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, _T("Conflict Files"));
904 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase,_T("Log"),2);
907 if( m_RebaseStage == REBASE_FINISH )
909 if(FinishRebase())
910 return ;
912 OnOK();
915 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
917 if(VerifyNoConflict())
918 return;
919 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
920 if(this->CheckNextCommitIsSquash())
921 {//next commit is not squash;
922 m_RebaseStage = REBASE_SQUASH_EDIT;
923 this->OnRebaseUpdateUI(0,0);
924 this->UpdateCurrentStatus();
925 return ;
928 m_RebaseStage=REBASE_CONTINUE;
929 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
930 this->UpdateCurrentStatus();
934 if( m_RebaseStage == REBASE_CONFLICT )
936 if(VerifyNoConflict())
937 return;
939 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
941 CString out =_T("");
942 CString cmd;
943 cmd.Format(_T("git.exe commit -a -C %s"), curRev->m_CommitHash.ToString());
945 AddLogString(cmd);
947 if(g_Git.Run(cmd,&out,CP_UTF8))
949 AddLogString(out);
950 if(!g_Git.CheckCleanWorkTree())
952 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
953 return;
957 AddLogString(out);
958 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
959 if( curRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_EDIT)
961 m_RebaseStage=REBASE_EDIT;
962 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
963 this->UpdateCurrentStatus();
964 return;
966 else
968 m_RebaseStage=REBASE_CONTINUE;
969 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
970 this->UpdateCurrentStatus();
974 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
976 CString str;
977 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
979 str=this->m_LogMessageCtrl.GetText();
980 if(str.Trim().IsEmpty())
982 CMessageBox::Show(NULL,_T("Commit Message Is Empty"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
983 return;
986 CString tempfile=::GetTempFile();
987 CFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
988 CStringA log=CUnicodeUtils::GetUTF8( str);
989 file.Write(log,log.GetLength());
990 //file.WriteString(m_sLogMessage);
991 file.Close();
993 CString out,cmd;
995 if( m_RebaseStage == REBASE_SQUASH_EDIT )
996 cmd.Format(_T("git.exe commit -F \"%s\""), tempfile);
997 else
998 cmd.Format(_T("git.exe commit --amend -F \"%s\""), tempfile);
1000 if(g_Git.Run(cmd,&out,CP_UTF8))
1002 if(!g_Git.CheckCleanWorkTree())
1004 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1005 return;
1009 CFile::Remove(tempfile);
1010 AddLogString(out);
1011 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1012 m_RebaseStage=REBASE_CONTINUE;
1013 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1014 this->UpdateCurrentStatus();
1018 InterlockedExchange(&m_bThreadRunning, TRUE);
1019 SetControlEnable();
1021 if (AfxBeginThread(RebaseThreadEntry, this)==NULL)
1023 InterlockedExchange(&m_bThreadRunning, FALSE);
1024 CMessageBox::Show(NULL, _T("Create Rebase Thread Fail"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1025 SetControlEnable();
1028 int CRebaseDlg::CheckNextCommitIsSquash()
1030 int index;
1031 if(m_CommitList.m_IsOldFirst)
1032 index=m_CurrentRebaseIndex+1;
1033 else
1034 index=m_CurrentRebaseIndex-1;
1036 GitRev *curRev;
1039 if(index<0)
1040 return -1;
1041 if(index>= m_CommitList.GetItemCount())
1042 return -1;
1044 curRev=(GitRev*)m_CommitList.m_arShownList[index];
1046 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SQUASH )
1047 return 0;
1048 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP)
1050 if(m_CommitList.m_IsOldFirst)
1051 index++;
1052 else
1053 index--;
1054 }else
1055 return -1;
1057 }while(curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP);
1059 return -1;
1062 int CRebaseDlg::GoNext()
1064 if(m_CommitList.m_IsOldFirst)
1065 m_CurrentRebaseIndex++;
1066 else
1067 m_CurrentRebaseIndex--;
1068 return 0;
1071 int CRebaseDlg::StateAction()
1073 switch(this->m_RebaseStage)
1075 case CHOOSE_BRANCH:
1076 case CHOOSE_COMMIT_PICK_MODE:
1077 if(StartRebase())
1078 return -1;
1079 m_RebaseStage = REBASE_START;
1080 GoNext();
1081 break;
1084 return 0;
1086 void CRebaseDlg::SetContinueButtonText()
1088 CString Text;
1089 switch(this->m_RebaseStage)
1091 case CHOOSE_BRANCH:
1092 case CHOOSE_COMMIT_PICK_MODE:
1093 if(this->m_IsFastForward)
1094 Text = _T("Start(FastFwd)");
1095 else
1096 Text = _T("Start");
1097 break;
1099 case REBASE_START:
1100 case REBASE_CONTINUE:
1101 case REBASE_SQUASH_CONFLICT:
1102 Text = _T("Continue");
1103 break;
1105 case REBASE_CONFLICT:
1106 Text = _T("Commit");
1107 break;
1108 case REBASE_EDIT:
1109 Text = _T("Amend");
1110 break;
1112 case REBASE_SQUASH_EDIT:
1113 Text = _T("Commit");
1114 break;
1116 case REBASE_ABORT:
1117 case REBASE_FINISH:
1118 Text = _T("Finish");
1119 break;
1121 case REBASE_DONE:
1122 Text = _T("Done");
1123 break;
1125 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1128 void CRebaseDlg::SetControlEnable()
1130 switch(this->m_RebaseStage)
1132 case CHOOSE_BRANCH:
1133 case CHOOSE_COMMIT_PICK_MODE:
1135 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(TRUE);
1136 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(TRUE);
1137 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(TRUE);
1138 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(TRUE);
1139 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(TRUE);
1141 if(!m_IsCherryPick)
1143 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1144 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1145 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1147 //this->m_CommitList.m_IsEnableRebaseMenu=TRUE;
1148 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1149 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1150 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1151 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP);
1152 break;
1154 case REBASE_START:
1155 case REBASE_CONTINUE:
1156 case REBASE_ABORT:
1157 case REBASE_FINISH:
1158 case REBASE_CONFLICT:
1159 case REBASE_EDIT:
1160 case REBASE_SQUASH_CONFLICT:
1161 case REBASE_DONE:
1162 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(FALSE);
1163 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(FALSE);
1164 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(FALSE);
1165 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1166 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1167 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1168 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(FALSE);
1169 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(FALSE);
1170 //this->m_CommitList.m_IsEnableRebaseMenu=FALSE;
1171 this->m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1172 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1173 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1174 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP));
1176 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1178 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1179 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1180 this->m_PostButton.RemoveAll();
1181 this->m_PostButton.AddEntries(m_PostButtonTexts);
1182 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1184 break;
1187 if(m_bThreadRunning)
1189 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1191 }else
1193 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1197 void CRebaseDlg::UpdateProgress()
1199 int index;
1200 CRect rect;
1202 if(m_CommitList.m_IsOldFirst)
1203 index = m_CurrentRebaseIndex+1;
1204 else
1205 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1207 m_ProgressBar.SetRange(1,m_CommitList.GetItemCount());
1208 m_ProgressBar.SetPos(index);
1209 if (m_pTaskbarList)
1211 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1212 m_pTaskbarList->SetProgressValue(m_hWnd, index, m_CommitList.GetItemCount());
1215 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1217 CString text;
1218 text.Format(_T("Rebasing... (%d/%d)"),index,m_CommitList.GetItemCount());
1219 m_CtrlStatusText.SetWindowText(text);
1223 GitRev *prevRev=NULL, *curRev=NULL;
1225 if( m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex< m_CommitList.m_arShownList.GetSize())
1227 curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1230 for(int i=0;i<m_CommitList.m_arShownList.GetSize();i++)
1232 prevRev=(GitRev*)m_CommitList.m_arShownList[i];
1233 if(prevRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_CURRENT)
1235 prevRev->GetAction(&m_CommitList) &= ~ CTGitPath::LOGACTIONS_REBASE_CURRENT;
1236 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1237 m_CommitList.InvalidateRect(rect);
1241 if(curRev)
1243 curRev->GetAction(&m_CommitList) |= CTGitPath::LOGACTIONS_REBASE_CURRENT;
1244 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1245 m_CommitList.InvalidateRect(rect);
1247 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1250 void CRebaseDlg::UpdateCurrentStatus()
1252 SetContinueButtonText();
1253 SetControlEnable();
1254 UpdateProgress();
1257 void CRebaseDlg::AddLogString(CString str)
1259 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1260 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1261 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1262 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1263 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1264 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1267 int CRebaseDlg::GetCurrentCommitID()
1269 if(m_CommitList.m_IsOldFirst)
1271 return this->m_CurrentRebaseIndex+1;
1273 }else
1275 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1279 int CRebaseDlg::DoRebase()
1281 CString cmd,out;
1282 if(m_CurrentRebaseIndex <0)
1283 return 0;
1284 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1285 return 0;
1287 GitRev *pRev = (GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1288 int mode=pRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_MODE_MASK;
1289 CString nocommit;
1291 if( mode== CTGitPath::LOGACTIONS_REBASE_SKIP)
1293 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1294 return 0;
1297 if( mode != CTGitPath::LOGACTIONS_REBASE_PICK )
1299 this->m_SquashMessage+= pRev->GetSubject();
1300 this->m_SquashMessage+= _T("\n");
1301 this->m_SquashMessage+= pRev->GetBody();
1303 else
1304 this->m_SquashMessage.Empty();
1306 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1307 nocommit=_T(" --no-commit ");
1309 CString log;
1310 log.Format(_T("%s %d: %s"),CTGitPath::GetActionName(mode),this->GetCurrentCommitID(),pRev->m_CommitHash.ToString());
1311 AddLogString(log);
1312 AddLogString(pRev->GetSubject());
1313 if (pRev->GetSubject().IsEmpty())
1315 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);
1316 mode = CTGitPath::LOGACTIONS_REBASE_EDIT;
1319 cmd.Format(_T("git.exe cherry-pick %s %s"),nocommit,pRev->m_CommitHash.ToString());
1321 if(g_Git.Run(cmd,&out,CP_UTF8))
1323 AddLogString(out);
1324 CTGitPathList list;
1325 if(g_Git.ListConflictFile(list))
1327 AddLogString(_T("Get conflict files fail"));
1328 return -1;
1330 if(list.GetCount() == 0 )
1332 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1334 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1335 return 0;
1337 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1339 this->m_RebaseStage = REBASE_EDIT ;
1340 return -1; // Edit return -1 to stop rebase.
1342 // Squash Case
1343 if(CheckNextCommitIsSquash())
1344 { // no squash
1345 // let user edit last commmit message
1346 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1347 return -1;
1350 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1351 m_RebaseStage = REBASE_SQUASH_CONFLICT;
1352 else
1353 m_RebaseStage = REBASE_CONFLICT;
1354 return -1;
1356 }else
1358 AddLogString(out);
1359 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1361 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1362 return 0;
1364 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1366 this->m_RebaseStage = REBASE_EDIT ;
1367 return -1; // Edit return -1 to stop rebase.
1370 // Squash Case
1371 if(CheckNextCommitIsSquash())
1372 { // no squash
1373 // let user edit last commmit message
1374 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1375 return -1;
1376 } else if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1377 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1380 return 0;
1383 BOOL CRebaseDlg::IsEnd()
1385 if(m_CommitList.m_IsOldFirst)
1386 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
1387 else
1388 return m_CurrentRebaseIndex<0;
1391 int CRebaseDlg::RebaseThread()
1393 int ret=0;
1394 while(1)
1396 if( m_RebaseStage == REBASE_START )
1398 if( this->StartRebase() )
1400 InterlockedExchange(&m_bThreadRunning, FALSE);
1401 ret = -1;
1402 break;
1404 m_RebaseStage = REBASE_CONTINUE;
1406 }else if( m_RebaseStage == REBASE_CONTINUE )
1408 this->GoNext();
1409 UpdateCurrentStatus();
1410 if(IsEnd())
1412 ret = 0;
1413 m_RebaseStage = REBASE_FINISH;
1415 }else
1417 ret = DoRebase();
1419 if( ret )
1421 break;
1425 }else if( m_RebaseStage == REBASE_FINISH )
1427 FinishRebase();
1428 m_RebaseStage = REBASE_DONE;
1429 if (m_pTaskbarList)
1430 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1431 break;
1433 }else
1435 break;
1437 this->PostMessage(MSG_REBASE_UPDATE_UI);
1438 //this->UpdateCurrentStatus();
1441 InterlockedExchange(&m_bThreadRunning, FALSE);
1442 this->PostMessage(MSG_REBASE_UPDATE_UI);
1443 return ret;
1446 void CRebaseDlg::ListConflictFile()
1448 this->m_FileListCtrl.Clear();
1449 CTGitPathList list;
1450 CTGitPath path;
1451 list.AddPath(path);
1453 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
1455 this->m_FileListCtrl.GetStatus(&list,true);
1456 this->m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED|CTGitPath::LOGACTIONS_MODIFIED|CTGitPath::LOGACTIONS_ADDED|CTGitPath::LOGACTIONS_DELETED,
1457 CTGitPath::LOGACTIONS_UNMERGED);
1460 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
1462 UpdateCurrentStatus();
1463 if(m_CurrentRebaseIndex <0)
1464 return 0;
1465 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1466 return 0;
1467 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1469 switch(m_RebaseStage)
1471 case REBASE_CONFLICT:
1472 case REBASE_SQUASH_CONFLICT:
1473 ListConflictFile();
1474 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
1475 if (m_pTaskbarList)
1476 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1477 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1478 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1479 break;
1480 case REBASE_EDIT:
1481 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1482 if (m_pTaskbarList)
1483 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1484 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1485 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1486 break;
1487 case REBASE_SQUASH_EDIT:
1488 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1489 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
1490 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1491 if (m_pTaskbarList)
1492 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1493 break;
1494 default:
1495 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1497 return 0;
1499 void CRebaseDlg::OnCancel()
1501 OnBnClickedAbort();
1503 void CRebaseDlg::OnBnClickedAbort()
1505 if (m_pTaskbarList)
1506 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1508 CString cmd,out;
1509 if(m_OrigUpstreamHash.IsEmpty())
1511 __super::OnCancel();
1514 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
1516 return;
1519 if(CMessageBox::Show(NULL,_T("Are you sure you want to abort the rebase process?"),_T("TortoiseGit"),MB_YESNO) != IDYES)
1520 return;
1522 if(this->m_IsFastForward)
1524 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1525 if(g_Git.Run(cmd,&out,CP_UTF8))
1527 AddLogString(out);
1528 return ;
1530 __super::OnCancel();
1531 return;
1533 cmd.Format(_T("git.exe checkout -f %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1534 if(g_Git.Run(cmd,&out,CP_UTF8))
1536 AddLogString(out);
1537 return ;
1540 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigUpstreamHash.ToString());
1541 if(g_Git.Run(cmd,&out,CP_UTF8))
1543 AddLogString(out);
1544 return ;
1547 if(this->m_IsCherryPick) //there are not "branch" at cherry pick mode
1549 __super::OnCancel();
1550 return;
1553 cmd.Format(_T("git checkout -f %s"),this->m_BranchCtrl.GetString());
1554 if(g_Git.Run(cmd,&out,CP_UTF8))
1556 AddLogString(out);
1557 return ;
1560 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1561 if(g_Git.Run(cmd,&out,CP_UTF8))
1563 AddLogString(out);
1564 return ;
1566 __super::OnCancel();
1569 void CRebaseDlg::OnBnClickedButtonBrowse()
1571 if(CBrowseRefsDlg::PickRefForCombo(&m_UpstreamCtrl, gPickRef_NoTag))
1572 OnCbnSelchangeUpstream();
1575 void CRebaseDlg::OnBnClickedRebaseCheckForce()
1577 this->UpdateData();
1578 this->FetchLogList();
1579 if(this->CheckRebaseCondition())
1581 /* Disable Start Rebase */
1582 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1586 void CRebaseDlg::OnBnClickedRebasePostButton()
1588 this->m_Upstream=this->m_UpstreamCtrl.GetString();
1589 this->m_Branch=this->m_BranchCtrl.GetString();
1591 this->EndDialog(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry());
1594 void CRebaseDlg::Refresh()
1596 if(this->m_IsCherryPick)
1597 return ;
1599 if(this->m_RebaseStage == CHOOSE_BRANCH )
1601 this->UpdateData();
1602 this->FetchLogList();
1603 if(this->CheckRebaseCondition())
1605 /* Disable Start Rebase */
1606 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1611 void CRebaseDlg::OnBnClickedButtonUp2()
1613 POSITION pos;
1614 pos = m_CommitList.GetFirstSelectedItemPosition();
1616 // do nothing if the first selected item is the first item in the list
1617 if (m_CommitList.GetNextSelectedItem(pos) == 0)
1618 return;
1620 pos = m_CommitList.GetFirstSelectedItemPosition();
1622 bool changed = false;
1623 while(pos)
1625 int index=m_CommitList.GetNextSelectedItem(pos);
1626 if(index>=1)
1628 CGitHash old = m_CommitList.m_logEntries[index-1];
1629 m_CommitList.m_logEntries[index-1] = m_CommitList.m_logEntries[index];
1630 m_CommitList.m_logEntries[index] = old;
1631 m_CommitList.SetItemState(index-1, LVIS_SELECTED, LVIS_SELECTED);
1632 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1633 changed = true;
1636 if (changed)
1638 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1639 m_CommitList.Invalidate();
1640 m_CommitList.SetFocus();
1644 void CRebaseDlg::OnBnClickedButtonDown2()
1646 if (m_CommitList.GetSelectedCount() == 0)
1647 return;
1649 POSITION pos;
1650 pos = m_CommitList.GetFirstSelectedItemPosition();
1651 bool changed = false;
1652 // use an array to store all selected item indexes; the user won't select too much items
1653 int* indexes = NULL;
1654 indexes = new int[m_CommitList.GetSelectedCount()];
1655 int i = 0;
1656 while(pos)
1658 indexes[i++] = m_CommitList.GetNextSelectedItem(pos);
1660 // don't move any item if the last selected item is the last item in the m_CommitList
1661 // (that would change the order of the selected items)
1662 if(indexes[m_CommitList.GetSelectedCount() - 1] < m_CommitList.GetItemCount() - 1)
1664 // iterate over the indexes backwards in order to correctly move multiselected items
1665 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
1667 int index = indexes[i];
1668 CGitHash old = m_CommitList.m_logEntries[index+1];
1669 m_CommitList.m_logEntries[index+1] = m_CommitList.m_logEntries[index];
1670 m_CommitList.m_logEntries[index] = old;
1671 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1672 m_CommitList.SetItemState(index+1, LVIS_SELECTED, LVIS_SELECTED);
1673 changed = true;
1676 delete [] indexes;
1677 indexes = NULL;
1678 if (changed)
1680 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1681 m_CommitList.Invalidate();
1682 m_CommitList.SetFocus();
1686 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1688 m_pTaskbarList.Release();
1689 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1690 return 0;
1693 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
1695 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
1696 *pResult = 0;
1697 if(m_CommitList.m_bNoDispUpdates)
1698 return;
1699 if (pNMLV->iItem >= 0)
1701 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
1702 if (pNMLV->iSubItem != 0)
1703 return;
1704 if ((pNMLV->iItem == m_CommitList.m_arShownList.GetCount()))
1706 // remove the selected state
1707 if (pNMLV->uChanged & LVIF_STATE)
1709 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
1710 FillLogMessageCtrl();
1712 return;
1714 if (pNMLV->uChanged & LVIF_STATE)
1716 FillLogMessageCtrl();
1719 else
1721 FillLogMessageCtrl();
1725 void CRebaseDlg::FillLogMessageCtrl()
1727 int selCount = m_CommitList.GetSelectedCount();
1728 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
1730 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
1731 int selIndex = m_CommitList.GetNextSelectedItem(pos);
1732 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_CommitList.m_arShownList.SafeGetAt(selIndex));
1733 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
1734 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
1735 m_FileListCtrl.Show(SVNSLC_SHOWVERSIONED);
1736 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1737 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + _T("\n") + pLogEntry->GetBody());
1738 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);