Moved some more strings to resources
[TortoiseGit.git] / src / TortoiseProc / RebaseDlg.cpp
blobebcd8245cae76be8ca4049fbf7ba56f97f2d720e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2011-2012 - Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // RebaseDlg.cpp : implementation file
24 #include "stdafx.h"
25 #include "TortoiseProc.h"
26 #include "RebaseDlg.h"
27 #include "AppUtils.h"
28 #include "LoglistUtils.h"
29 #include "MessageBox.h"
30 #include "UnicodeUtils.h"
31 #include "BrowseRefsDlg.h"
32 #include "ProgressDlg.h"
33 #include "SmartHandle.h"
34 #include "../TGitCache/CacheInterface.h"
36 // CRebaseDlg dialog
38 IMPLEMENT_DYNAMIC(CRebaseDlg, CResizableStandAloneDialog)
40 CRebaseDlg::CRebaseDlg(CWnd* pParent /*=NULL*/)
41 : CResizableStandAloneDialog(CRebaseDlg::IDD, pParent)
42 , m_bPickAll(FALSE)
43 , m_bSquashAll(FALSE)
44 , m_bEditAll(FALSE)
45 , m_bAddCherryPickedFrom(FALSE)
47 m_RebaseStage=CHOOSE_BRANCH;
48 m_CurrentRebaseIndex=-1;
49 m_bThreadRunning =FALSE;
50 this->m_IsCherryPick = FALSE;
51 m_bForce=FALSE;
52 m_IsFastForward=FALSE;
55 CRebaseDlg::~CRebaseDlg()
59 void CRebaseDlg::DoDataExchange(CDataExchange* pDX)
61 CDialog::DoDataExchange(pDX);
62 DDX_Control(pDX, IDC_REBASE_PROGRESS, m_ProgressBar);
63 DDX_Control(pDX, IDC_STATUS_STATIC, m_CtrlStatusText);
64 DDX_Check(pDX, IDC_PICK_ALL, m_bPickAll);
65 DDX_Check(pDX, IDC_SQUASH_ALL, m_bSquashAll);
66 DDX_Check(pDX, IDC_EDIT_ALL, m_bEditAll);
67 DDX_Control(pDX, IDC_REBASE_SPLIT, m_wndSplitter);
68 DDX_Control(pDX,IDC_COMMIT_LIST,m_CommitList);
69 DDX_Control(pDX,IDC_REBASE_COMBOXEX_BRANCH, this->m_BranchCtrl);
70 DDX_Control(pDX,IDC_REBASE_COMBOXEX_UPSTREAM, this->m_UpstreamCtrl);
71 DDX_Check(pDX, IDC_REBASE_CHECK_FORCE,m_bForce);
72 DDX_Check(pDX, IDC_CHECK_CHERRYPICKED_FROM, m_bAddCherryPickedFrom);
73 DDX_Control(pDX,IDC_REBASE_POST_BUTTON,m_PostButton);
77 BEGIN_MESSAGE_MAP(CRebaseDlg, CResizableStandAloneDialog)
78 ON_BN_CLICKED(IDC_PICK_ALL, &CRebaseDlg::OnBnClickedPickAll)
79 ON_BN_CLICKED(IDC_SQUASH_ALL, &CRebaseDlg::OnBnClickedSquashAll)
80 ON_BN_CLICKED(IDC_EDIT_ALL, &CRebaseDlg::OnBnClickedEditAll)
81 ON_BN_CLICKED(IDC_REBASE_SPLIT, &CRebaseDlg::OnBnClickedRebaseSplit)
82 ON_BN_CLICKED(IDC_REBASE_CONTINUE,OnBnClickedContinue)
83 ON_BN_CLICKED(IDC_REBASE_ABORT, OnBnClickedAbort)
84 ON_WM_SIZE()
85 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_BRANCH, &CRebaseDlg::OnCbnSelchangeBranch)
86 ON_CBN_SELCHANGE(IDC_REBASE_COMBOXEX_UPSTREAM, &CRebaseDlg::OnCbnSelchangeUpstream)
87 ON_MESSAGE(MSG_REBASE_UPDATE_UI, OnRebaseUpdateUI)
88 ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CRebaseDlg::OnBnClickedButtonBrowse)
89 ON_BN_CLICKED(IDC_REBASE_CHECK_FORCE, &CRebaseDlg::OnBnClickedRebaseCheckForce)
90 ON_BN_CLICKED(IDC_CHECK_CHERRYPICKED_FROM, &CRebaseDlg::OnBnClickedCheckCherryPickedFrom)
91 ON_BN_CLICKED(IDC_REBASE_POST_BUTTON, &CRebaseDlg::OnBnClickedRebasePostButton)
92 ON_BN_CLICKED(IDC_BUTTON_UP2, &CRebaseDlg::OnBnClickedButtonUp2)
93 ON_BN_CLICKED(IDC_BUTTON_DOWN2, &CRebaseDlg::OnBnClickedButtonDown2)
94 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
95 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COMMIT_LIST, OnLvnItemchangedLoglist)
96 END_MESSAGE_MAP()
98 void CRebaseDlg::AddRebaseAnchor()
100 AddAnchor(IDC_REBASE_TAB,TOP_LEFT,BOTTOM_RIGHT);
101 AddAnchor(IDC_COMMIT_LIST,TOP_LEFT, TOP_RIGHT);
102 AddAnchor(IDC_REBASE_SPLIT,TOP_LEFT, TOP_RIGHT);
103 AddAnchor(IDC_STATUS_STATIC, BOTTOM_LEFT,BOTTOM_RIGHT);
104 AddAnchor(IDC_REBASE_CONTINUE,BOTTOM_RIGHT);
105 AddAnchor(IDC_REBASE_ABORT, BOTTOM_RIGHT);
106 AddAnchor(IDC_REBASE_PROGRESS,BOTTOM_LEFT, BOTTOM_RIGHT);
107 AddAnchor(IDC_PICK_ALL,TOP_LEFT);
108 AddAnchor(IDC_SQUASH_ALL,TOP_LEFT);
109 AddAnchor(IDC_EDIT_ALL,TOP_LEFT);
110 AddAnchor(IDC_BUTTON_UP2,TOP_LEFT);
111 AddAnchor(IDC_BUTTON_DOWN2,TOP_LEFT);
112 AddAnchor(IDC_REBASE_COMBOXEX_UPSTREAM,TOP_LEFT);
113 AddAnchor(IDC_REBASE_COMBOXEX_BRANCH,TOP_LEFT);
114 AddAnchor(IDC_REBASE_STATIC_UPSTREAM,TOP_LEFT);
115 AddAnchor(IDC_REBASE_STATIC_BRANCH,TOP_LEFT);
116 AddAnchor(IDHELP, BOTTOM_RIGHT);
117 AddAnchor(IDC_REBASE_CHECK_FORCE,TOP_RIGHT);
118 AddAnchor(IDC_CHECK_CHERRYPICKED_FROM, TOP_RIGHT);
119 AddAnchor(IDC_REBASE_POST_BUTTON,BOTTOM_LEFT);
121 this->AddOthersToAnchor();
124 BOOL CRebaseDlg::OnInitDialog()
126 CResizableStandAloneDialog::OnInitDialog();
127 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
129 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
130 // do this, Explorer would be unable to send that message to our window if we
131 // were running elevated. It's OK to make the call all the time, since if we're
132 // not elevated, this is a no-op.
133 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
134 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
135 CAutoLibrary hUser = ::LoadLibrary(_T("user32.dll"));
136 if (hUser)
138 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
139 if (pfnChangeWindowMessageFilterEx)
141 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
144 m_pTaskbarList.Release();
145 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
147 CRect rectDummy;
148 //IDC_REBASE_DUMY_TAB
150 GetClientRect(m_DlgOrigRect);
151 m_CommitList.GetClientRect(m_CommitListOrigRect);
153 CWnd *pwnd=this->GetDlgItem(IDC_REBASE_DUMY_TAB);
154 pwnd->GetWindowRect(&rectDummy);
155 this->ScreenToClient(rectDummy);
157 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_REBASE_TAB))
159 TRACE0("Failed to create output tab window\n");
160 return FALSE; // fail to create
162 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
163 // Create output panes:
164 //const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
165 DWORD dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
167 if (! this->m_FileListCtrl.Create(dwStyle,rectDummy,&this->m_ctrlTabCtrl,0) )
169 TRACE0("Failed to create output windows\n");
170 return FALSE; // fail to create
173 if( ! this->m_LogMessageCtrl.Create(_T("Scintilla"),_T("source"),0,rectDummy,&m_ctrlTabCtrl,0,0) )
175 TRACE0("Failed to create log message control");
176 return FALSE;
178 m_LogMessageCtrl.Init(0);
179 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
181 dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
183 if (!m_wndOutputRebase.Create(_T("Scintilla"),_T("source"),0,rectDummy, &m_ctrlTabCtrl, 0,0) )
185 TRACE0("Failed to create output windows\n");
186 return -1; // fail to create
188 m_wndOutputRebase.Init(0);
189 m_wndOutputRebase.Call(SCI_SETREADONLY, TRUE);
191 m_tooltips.Create(this);
193 m_tooltips.AddTool(IDC_REBASE_CHECK_FORCE,IDS_REBASE_FORCE_TT);
194 m_tooltips.AddTool(IDC_REBASE_ABORT,IDS_REBASE_ABORT_TT);
198 m_FileListCtrl.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("RebaseDlg"),(GITSLC_POPALL ^ (GITSLC_POPCOMMIT|GITSLC_POPRESTORE)),false);
200 m_ctrlTabCtrl.AddTab(&m_FileListCtrl, CString(MAKEINTRESOURCE(IDS_PROC_REVISIONFILES)));
201 m_ctrlTabCtrl.AddTab(&m_LogMessageCtrl, CString(MAKEINTRESOURCE(IDS_PROC_COMMITMESSAGE)), 1);
202 AddRebaseAnchor();
204 CString sWindowTitle;
205 GetWindowText(sWindowTitle);
206 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
208 EnableSaveRestore(_T("RebaseDlg"));
210 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
211 RECT rcDlg, rcLogMsg, rcFileList;
212 GetClientRect(&rcDlg);
213 m_CommitList.GetWindowRect(&rcLogMsg);
214 ScreenToClient(&rcLogMsg);
215 this->m_ctrlTabCtrl.GetWindowRect(&rcFileList);
216 ScreenToClient(&rcFileList);
217 if (yPos)
219 RECT rectSplitter;
220 m_wndSplitter.GetWindowRect(&rectSplitter);
221 ScreenToClient(&rectSplitter);
222 int delta = yPos - rectSplitter.top;
223 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
225 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
226 DoSize(delta);
230 if( this->m_RebaseStage == CHOOSE_BRANCH)
232 this->LoadBranchInfo();
235 else
237 this->m_BranchCtrl.EnableWindow(FALSE);
238 this->m_UpstreamCtrl.EnableWindow(FALSE);
241 m_CommitList.m_ColumnRegKey = _T("Rebase");
242 m_CommitList.m_IsIDReplaceAction = TRUE;
243 // m_CommitList.m_IsOldFirst = TRUE;
244 m_CommitList.m_IsRebaseReplaceGraph = TRUE;
246 m_CommitList.InsertGitColumn();
248 this->SetControlEnable();
250 if(!this->m_PreCmd.IsEmpty())
252 CProgressDlg progress;
253 progress.m_GitCmd=m_PreCmd;
254 progress.m_bAutoCloseOnSuccess=true;
255 progress.DoModal();
258 if(m_IsCherryPick)
260 this->m_BranchCtrl.SetCurSel(-1);
261 this->m_BranchCtrl.EnableWindow(FALSE);
262 GetDlgItem(IDC_REBASE_CHECK_FORCE)->ShowWindow(SW_HIDE);
263 GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(FALSE);
264 this->m_UpstreamCtrl.AddString(_T("HEAD"));
265 this->m_UpstreamCtrl.EnableWindow(FALSE);
266 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_CHERRYPICK)));
267 this->m_CommitList.StartFilter();
269 else
271 GetDlgItem(IDC_CHECK_CHERRYPICKED_FROM)->ShowWindow(SW_HIDE);
272 SetContinueButtonText();
273 m_CommitList.DeleteAllItems();
274 FetchLogList();
277 m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_CHERRY_PICK)|
278 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_SWITCHTOREV)|
279 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_RESET)|
280 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTREV)|
281 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_TO_VERSION)|
282 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REVERTTOREV)|
283 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_COMBINE_COMMIT));
285 if(m_CommitList.m_IsOldFirst)
286 this->m_CurrentRebaseIndex = -1;
287 else
288 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
291 if(this->CheckRebaseCondition())
293 /* Disable Start Rebase */
294 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
297 return TRUE;
299 // CRebaseDlg message handlers
301 void CRebaseDlg::OnBnClickedPickAll()
303 this->UpdateData();
304 if(this->m_bPickAll)
305 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_PICK);
307 this->m_bEditAll=FALSE;
308 this->m_bSquashAll=FALSE;
309 this->UpdateData(FALSE);
312 void CRebaseDlg::OnBnClickedSquashAll()
314 this->UpdateData();
315 if(this->m_bSquashAll)
316 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
318 this->m_bEditAll=FALSE;
319 this->m_bPickAll=FALSE;
320 this->UpdateData(FALSE);
323 void CRebaseDlg::OnBnClickedEditAll()
325 this->UpdateData();
326 if( this->m_bEditAll )
327 this->SetAllRebaseAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
329 this->m_bPickAll=FALSE;
330 this->m_bSquashAll=FALSE;
331 this->UpdateData(FALSE);
334 void CRebaseDlg::SetAllRebaseAction(int action)
336 for(int i=0;i<this->m_CommitList.m_logEntries.size();i++)
338 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList)=action;
340 m_CommitList.Invalidate();
343 void CRebaseDlg::OnBnClickedRebaseSplit()
345 this->UpdateData();
348 LRESULT CRebaseDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
350 switch (message) {
351 case WM_NOTIFY:
352 if (wParam == IDC_REBASE_SPLIT)
354 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
355 DoSize(pHdr->delta);
357 break;
360 return __super::DefWindowProc(message, wParam, lParam);
363 void CRebaseDlg::DoSize(int delta)
365 this->RemoveAllAnchors();
367 CSplitterControl::ChangeHeight(GetDlgItem(IDC_COMMIT_LIST), delta, CW_TOPALIGN);
368 //CSplitterControl::ChangeHeight(GetDlgItem(), delta, CW_TOPALIGN);
369 CSplitterControl::ChangeHeight(GetDlgItem(IDC_REBASE_TAB), -delta, CW_BOTTOMALIGN);
370 //CSplitterControl::ChangeHeight(GetDlgItem(), -delta, CW_BOTTOMALIGN);
371 CSplitterControl::ChangePos(GetDlgItem(IDC_SQUASH_ALL),0,delta);
372 CSplitterControl::ChangePos(GetDlgItem(IDC_PICK_ALL),0,delta);
373 CSplitterControl::ChangePos(GetDlgItem(IDC_EDIT_ALL),0,delta);
374 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_UP2),0,delta);
375 CSplitterControl::ChangePos(GetDlgItem(IDC_BUTTON_DOWN2),0,delta);
376 CSplitterControl::ChangePos(GetDlgItem(IDC_REBASE_CHECK_FORCE),0,delta);
378 this->AddRebaseAnchor();
379 // adjust the minimum size of the dialog to prevent the resizing from
380 // moving the list control too far down.
381 CRect rcLogMsg;
382 m_CommitList.GetClientRect(rcLogMsg);
383 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_CommitListOrigRect.Height()+rcLogMsg.Height()));
385 SetSplitterRange();
386 // m_CommitList.Invalidate();
388 // GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
390 this->m_ctrlTabCtrl.Invalidate();
391 this->m_CommitList.Invalidate();
392 this->m_FileListCtrl.Invalidate();
393 this->m_LogMessageCtrl.Invalidate();
397 void CRebaseDlg::SetSplitterRange()
399 if ((m_CommitList)&&(m_ctrlTabCtrl))
401 CRect rcTop;
402 m_CommitList.GetWindowRect(rcTop);
403 ScreenToClient(rcTop);
404 CRect rcMiddle;
405 m_ctrlTabCtrl.GetWindowRect(rcMiddle);
406 ScreenToClient(rcMiddle);
407 if (rcMiddle.Height() && rcMiddle.Width())
408 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
412 void CRebaseDlg::OnSize(UINT nType,int cx, int cy)
414 // first, let the resizing take place
415 __super::OnSize(nType, cx, cy);
417 //set range
418 SetSplitterRange();
421 void CRebaseDlg::SaveSplitterPos()
423 if (!IsIconic())
425 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RebaseDlgSizer"));
426 RECT rectSplitter;
427 m_wndSplitter.GetWindowRect(&rectSplitter);
428 ScreenToClient(&rectSplitter);
429 regPos = rectSplitter.top;
433 void CRebaseDlg::LoadBranchInfo()
435 m_BranchCtrl.SetMaxHistoryItems(0x7FFFFFFF);
436 m_UpstreamCtrl.SetMaxHistoryItems(0x7FFFFFFF);
438 STRING_VECTOR list;
439 list.clear();
440 int current;
441 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL);
442 m_BranchCtrl.AddString(list);
443 list.clear();
444 g_Git.GetBranchList(list,&current,CGit::BRANCH_ALL_F);
445 m_UpstreamCtrl.AddString(list);
447 m_BranchCtrl.SetCurSel(current);
449 AddBranchToolTips(&m_BranchCtrl);
450 AddBranchToolTips(&m_UpstreamCtrl);
452 if(!m_Upstream.IsEmpty())
454 m_UpstreamCtrl.AddString(m_Upstream);
455 m_UpstreamCtrl.SetCurSel(m_UpstreamCtrl.GetCount()-1);
457 else
459 //Select pull-remote from current branch
460 CString currentBranch = g_Git.GetSymbolicRef();
461 CString configName;
462 configName.Format(L"branch.%s.remote", currentBranch);
463 CString pullRemote = g_Git.GetConfigValue(configName);
465 //Select pull-branch from current branch
466 configName.Format(L"branch.%s.merge", currentBranch);
467 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
469 CString defaultUpstream;
470 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
471 int found = m_UpstreamCtrl.FindStringExact(0, defaultUpstream);
472 if(found >= 0)
473 m_UpstreamCtrl.SetCurSel(found);
477 void CRebaseDlg::OnCbnSelchangeBranch()
479 FetchLogList();
482 void CRebaseDlg::OnCbnSelchangeUpstream()
484 FetchLogList();
487 void CRebaseDlg::FetchLogList()
489 CGitHash base,hash;
490 CString basestr, err;
491 CString cmd;
492 m_IsFastForward=FALSE;
493 cmd.Format(_T("git.exe merge-base %s %s"), g_Git.FixBranchName(m_UpstreamCtrl.GetString()),
494 g_Git.FixBranchName(m_BranchCtrl.GetString()));
495 g_Git.Run(cmd, &basestr, &err, CP_UTF8);
497 base=basestr;
499 hash=g_Git.GetHash(m_BranchCtrl.GetString());
501 if(hash == g_Git.GetHash(this->m_UpstreamCtrl.GetString()))
503 m_CommitList.Clear();
504 CString text,fmt;
505 fmt.LoadString(IDS_REBASE_EQUAL_FMT);
506 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
508 m_CommitList.ShowText(text);
509 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(false);
510 return;
513 if(hash == base )
515 //fast forword
516 this->m_IsFastForward=TRUE;
518 m_CommitList.Clear();
519 CString text,fmt;
520 fmt.LoadString(IDS_REBASE_FASTFORWARD_FMT);
521 text.Format(fmt,m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString(),
522 m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString());
524 m_CommitList.ShowText(text);
525 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(true);
526 SetContinueButtonText();
528 return ;
531 hash.Empty();
533 if(!this->m_bForce)
535 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
537 if( base == hash )
539 m_CommitList.Clear();
540 CString text,fmt;
541 fmt.LoadString(IDS_REBASE_UPTODATE_FMT);
542 text.Format(fmt,m_BranchCtrl.GetString());
543 m_CommitList.ShowText(text);
544 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
545 SetContinueButtonText();
546 return;
550 m_CommitList.Clear();
551 CString from,to;
552 from = m_UpstreamCtrl.GetString();
553 to = m_BranchCtrl.GetString();
554 this->m_CommitList.FillGitLog(NULL,0,&from,&to);
556 if( m_CommitList.GetItemCount() == 0 )
557 m_CommitList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_NOTHINGTOREBASE)));
559 hash=g_Git.GetHash(m_UpstreamCtrl.GetString());
561 #if 0
562 if(m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash.size() >=0 )
564 if(hash == m_CommitList.m_logEntries[m_CommitList.m_logEntries.size()-1].m_ParentHash[0])
566 m_CommitList.Clear();
567 m_CommitList.ShowText(_T("Nothing Rebase"));
570 #endif
572 m_tooltips.Pop();
573 AddBranchToolTips(&this->m_BranchCtrl);
574 AddBranchToolTips(&this->m_UpstreamCtrl);
576 for(int i=0;i<m_CommitList.m_logEntries.size();i++)
578 m_CommitList.m_logEntries.GetGitRevAt(i).GetAction(&m_CommitList) = CTGitPath::LOGACTIONS_REBASE_PICK;
581 m_CommitList.Invalidate();
583 if(m_CommitList.m_IsOldFirst)
584 this->m_CurrentRebaseIndex = -1;
585 else
586 this->m_CurrentRebaseIndex = m_CommitList.m_logEntries.size();
588 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(m_CommitList.GetItemCount());
589 SetContinueButtonText();
592 void CRebaseDlg::AddBranchToolTips(CHistoryCombo *pBranch)
594 if(pBranch)
596 CString text=pBranch->GetString();
597 CString tooltip;
599 GitRev rev;
600 rev.GetCommit(text);
602 tooltip.Format(_T("%s: %s\n%s: %s\n%s: %s\n%s:\n<b>%s</b>\n%s"),
603 CString(MAKEINTRESOURCE(IDS_LOG_REVISION)),
604 rev.m_CommitHash.ToString(),
605 CString(MAKEINTRESOURCE(IDS_LOG_AUTHOR)),
606 rev.GetAuthorName(),
607 CString(MAKEINTRESOURCE(IDS_LOG_DATE)),
608 CLoglistUtils::FormatDateAndTime(rev.GetAuthorDate(), DATE_LONGDATE),
609 CString(MAKEINTRESOURCE(IDS_LOG_MESSAGE)),
610 rev.GetSubject(),
611 rev.GetBody());
613 pBranch->DisableTooltip();
614 this->m_tooltips.AddTool(pBranch->GetComboBoxCtrl(),tooltip);
618 BOOL CRebaseDlg::PreTranslateMessage(MSG*pMsg)
620 if (pMsg->message == WM_KEYDOWN)
622 switch (pMsg->wParam)
624 case ' ':
625 if(LogListHasFocus(pMsg->hwnd))
627 m_CommitList.ShiftSelectedAction();
628 return TRUE;
630 break;
631 case 'P':
632 if(LogListHasFocus(pMsg->hwnd))
634 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_PICK);
635 return TRUE;
637 break;
638 case 'S':
639 if(LogListHasFocus(pMsg->hwnd))
641 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SKIP);
642 return TRUE;
644 break;
645 case 'Q':
646 if(LogListHasFocus(pMsg->hwnd))
648 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
649 return TRUE;
651 break;
652 case 'E':
653 if(LogListHasFocus(pMsg->hwnd))
655 m_CommitList.SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
656 return TRUE;
658 break;
659 case 'A':
660 if(LogListHasFocus(pMsg->hwnd) && GetAsyncKeyState(VK_CONTROL) & 0x8000)
662 // select all entries
663 for (int i = 0; i < m_CommitList.GetItemCount(); ++i)
665 m_CommitList.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
667 return TRUE;
669 break;
670 case VK_F5:
672 Refresh();
673 return TRUE;
675 break;
676 /* Avoid TAB control destroy but dialog exist*/
677 case VK_ESCAPE:
678 case VK_CANCEL:
680 TCHAR buff[128];
681 ::GetClassName(pMsg->hwnd,buff,128);
684 if(_tcsnicmp(buff,_T("RichEdit20W"),128)==0 ||
685 _tcsnicmp(buff,_T("Scintilla"),128)==0 ||
686 _tcsnicmp(buff,_T("SysListView32"),128)==0||
687 ::GetParent(pMsg->hwnd) == this->m_ctrlTabCtrl.m_hWnd)
689 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
690 return TRUE;
695 m_tooltips.RelayEvent(pMsg);
696 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
699 bool CRebaseDlg::LogListHasFocus(HWND hwnd)
701 TCHAR buff[128];
702 ::GetClassName(hwnd, buff, 128);
704 if(_tcsnicmp(buff, _T("SysListView32"), 128) == 0)
705 return true;
706 return false;
709 int CRebaseDlg::CheckRebaseCondition()
711 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
713 if( !g_Git.CheckCleanWorkTree() )
715 if(CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
717 CString cmd,out;
718 cmd=_T("git.exe stash");
719 this->AddLogString(cmd);
720 if (g_Git.Run(cmd, &out, CP_UTF8))
722 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
723 return -1;
727 else
728 return -1;
730 //Todo Check $REBASE_ROOT
731 //Todo Check $DOTEST
733 CString cmd;
734 cmd=_T("git.exe var GIT_COMMITTER_IDENT");
735 if(g_Git.Run(cmd,NULL,CP_UTF8))
736 return -1;
738 //Todo call pre_rebase_hook
739 return 0;
741 int CRebaseDlg::StartRebase()
743 CString cmd,out;
744 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
745 if(!this->m_IsCherryPick)
747 //Todo call comment_for_reflog
748 cmd.Format(_T("git.exe checkout %s"),this->m_BranchCtrl.GetString());
749 this->AddLogString(cmd);
751 if(g_Git.Run(cmd,&out,CP_UTF8))
753 this->AddLogString(out);
754 return -1;
757 this->AddLogString(out);
760 cmd=_T("git.exe rev-parse --verify HEAD");
761 if(g_Git.Run(cmd,&out,CP_UTF8))
763 AddLogString(CString(MAKEINTRESOURCE(IDS_PROC_NOHEAD)));
764 return -1;
766 //Todo
767 //git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
768 // echo "detached HEAD" > "$DOTEST"/head-name
770 cmd.Format(_T("git.exe update-ref ORIG_HEAD HEAD"));
771 if(g_Git.Run(cmd,&out,CP_UTF8))
773 AddLogString(_T("update ORIG_HEAD Fail"));
774 return -1;
777 m_OrigUpstreamHash.Empty();
778 m_OrigUpstreamHash= g_Git.GetHash(this->m_UpstreamCtrl.GetString());
779 if(m_OrigUpstreamHash.IsEmpty())
781 this->AddLogString(m_OrigUpstreamHash);
782 return -1;
785 if( !this->m_IsCherryPick )
787 cmd.Format(_T("git.exe checkout -f %s"), m_OrigUpstreamHash.ToString());
788 this->AddLogString(cmd);
790 out.Empty();
791 if(g_Git.Run(cmd,&out,CP_UTF8))
793 this->AddLogString(out);
794 return -1;
798 CString log;
799 if( !this->m_IsCherryPick )
801 m_OrigBranchHash = g_Git.GetHash(this->m_BranchCtrl.GetString());
802 if(m_OrigBranchHash.IsEmpty())
804 this->AddLogString(m_OrigBranchHash.ToString());
805 return -1;
807 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTREBASE)));
809 else
810 log.Format(_T("%s\r\n"), CString(MAKEINTRESOURCE(IDS_PROC_REBASE_STARTCHERRYPICK)));
812 this->AddLogString(log);
813 return 0;
815 int CRebaseDlg::VerifyNoConflict()
817 CTGitPathList list;
818 if(g_Git.ListConflictFile(list))
820 AddLogString(_T("Get conflict files fail"));
821 return -1;
823 if( list.GetCount() != 0 )
825 CMessageBox::Show(NULL, IDS_PROGRS_CONFLICTSOCCURED, IDS_APPNAME, MB_OK);
826 return -1;
828 return 0;
831 int CRebaseDlg::FinishRebase()
833 if(this->m_IsCherryPick) //cherry pick mode no "branch", working at upstream branch
834 return 0;
836 git_revnum_t head = g_Git.GetHash(_T("HEAD"));
837 CString out,cmd;
839 out.Empty();
840 cmd.Format(_T("git.exe checkout -f %s"),this->m_BranchCtrl.GetString());
841 AddLogString(cmd);
842 if(g_Git.Run(cmd,&out,CP_UTF8))
844 AddLogString(out);
845 return -1;
847 AddLogString(out);
849 out.Empty();
850 cmd.Format(_T("git.exe reset --hard %s"),head);
851 AddLogString(cmd);
852 if(g_Git.Run(cmd,&out,CP_UTF8))
854 AddLogString(out);
855 return -1;
857 AddLogString(out);
859 m_ctrlTabCtrl.RemoveTab(0);
860 m_ctrlTabCtrl.RemoveTab(0);
861 m_CtrlStatusText.SetWindowText(CString(MAKEINTRESOURCE(IDS_PROC_REBASEFINISHED)));
863 return 0;
865 void CRebaseDlg::OnBnClickedContinue()
867 if( m_RebaseStage == REBASE_DONE)
869 OnOK();
870 return;
873 if( this->m_IsFastForward )
875 CString cmd,out;
876 CString oldbranch = g_Git.GetCurrentBranch();
877 if( oldbranch != m_BranchCtrl.GetString() )
879 cmd.Format(_T("git.exe checkout %s"),m_BranchCtrl.GetString());
880 AddLogString(cmd);
881 if (g_Git.Run(cmd, &out, CP_UTF8))
883 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
884 AddLogString(out);
885 return;
888 AddLogString(out);
889 out.Empty();
890 m_OrigBranchHash = g_Git.GetHash(m_BranchCtrl.GetString());
891 m_OrigUpstreamHash = g_Git.GetHash(this->m_UpstreamCtrl.GetString());
893 if(!g_Git.IsFastForward(this->m_BranchCtrl.GetString(),this->m_UpstreamCtrl.GetString()))
895 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
896 AddLogString(_T("No fast forward possible.\r\nMaybe repository changed"));
897 return;
900 cmd.Format(_T("git.exe reset --hard %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
901 CString log;
902 log.Format(IDS_PROC_REBASE_FFTO, m_UpstreamCtrl.GetString());
903 this->AddLogString(log);
905 AddLogString(cmd);
906 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
907 if (g_Git.Run(cmd, &out, CP_UTF8))
909 AddLogString(CString(MAKEINTRESOURCE(IDS_FAIL)));
910 AddLogString(out);
911 return;
913 AddLogString(out);
914 AddLogString(CString(MAKEINTRESOURCE(IDS_DONE)));
915 m_RebaseStage = REBASE_DONE;
916 UpdateCurrentStatus();
917 return;
920 if( m_RebaseStage == CHOOSE_BRANCH|| m_RebaseStage == CHOOSE_COMMIT_PICK_MODE )
922 if(CheckRebaseCondition())
923 return ;
924 m_RebaseStage = REBASE_START;
925 m_FileListCtrl.Clear();
926 m_FileListCtrl.m_CurrentVersion = L"";
927 m_ctrlTabCtrl.SetTabLabel(REBASE_TAB_CONFLICT, CString(MAKEINTRESOURCE(IDS_PROC_CONFLICTFILES)));
928 m_ctrlTabCtrl.AddTab(&m_wndOutputRebase, CString(MAKEINTRESOURCE(IDS_LOG)), 2);
931 if( m_RebaseStage == REBASE_FINISH )
933 if(FinishRebase())
934 return ;
936 OnOK();
939 if( m_RebaseStage == REBASE_SQUASH_CONFLICT)
941 if(VerifyNoConflict())
942 return;
943 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
944 if(this->CheckNextCommitIsSquash())
945 {//next commit is not squash;
946 m_RebaseStage = REBASE_SQUASH_EDIT;
947 this->OnRebaseUpdateUI(0,0);
948 this->UpdateCurrentStatus();
949 return ;
952 m_RebaseStage=REBASE_CONTINUE;
953 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
954 this->UpdateCurrentStatus();
958 if( m_RebaseStage == REBASE_CONFLICT )
960 if(VerifyNoConflict())
961 return;
963 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
965 CString out =_T("");
966 CString cmd;
967 cmd.Format(_T("git.exe commit -a -C %s"), curRev->m_CommitHash.ToString());
969 AddLogString(cmd);
971 if(g_Git.Run(cmd,&out,CP_UTF8))
973 AddLogString(out);
974 if(!g_Git.CheckCleanWorkTree())
976 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
977 return;
981 AddLogString(out);
982 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
983 if( curRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_EDIT)
985 m_RebaseStage=REBASE_EDIT;
986 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
987 this->UpdateCurrentStatus();
988 return;
990 else
992 m_RebaseStage=REBASE_CONTINUE;
993 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
994 this->UpdateCurrentStatus();
998 if( m_RebaseStage == REBASE_EDIT || m_RebaseStage == REBASE_SQUASH_EDIT )
1000 CString str;
1001 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1003 str=this->m_LogMessageCtrl.GetText();
1004 if(str.Trim().IsEmpty())
1006 CMessageBox::Show(NULL, IDS_PROC_COMMITMESSAGE_EMPTY,IDS_APPNAME, MB_OK | MB_ICONERROR);
1007 return;
1010 CString tempfile=::GetTempFile();
1011 CAppUtils::SaveCommitUnicodeFile(tempfile, str);
1013 CString out,cmd;
1015 if( m_RebaseStage == REBASE_SQUASH_EDIT )
1016 cmd.Format(_T("git.exe commit -F \"%s\""), tempfile);
1017 else
1018 cmd.Format(_T("git.exe commit --amend -F \"%s\""), tempfile);
1020 if(g_Git.Run(cmd,&out,CP_UTF8))
1022 if(!g_Git.CheckCleanWorkTree())
1024 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1025 return;
1029 CFile::Remove(tempfile);
1030 AddLogString(out);
1031 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1032 m_RebaseStage=REBASE_CONTINUE;
1033 curRev->GetAction(&m_CommitList)|=CTGitPath::LOGACTIONS_REBASE_DONE;
1034 this->UpdateCurrentStatus();
1038 InterlockedExchange(&m_bThreadRunning, TRUE);
1039 SetControlEnable();
1041 if (AfxBeginThread(RebaseThreadEntry, this)==NULL)
1043 InterlockedExchange(&m_bThreadRunning, FALSE);
1044 CMessageBox::Show(NULL, _T("Create Rebase Thread Fail"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1045 SetControlEnable();
1048 int CRebaseDlg::CheckNextCommitIsSquash()
1050 int index;
1051 if(m_CommitList.m_IsOldFirst)
1052 index=m_CurrentRebaseIndex+1;
1053 else
1054 index=m_CurrentRebaseIndex-1;
1056 GitRev *curRev;
1059 if(index<0)
1060 return -1;
1061 if(index>= m_CommitList.GetItemCount())
1062 return -1;
1064 curRev=(GitRev*)m_CommitList.m_arShownList[index];
1066 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SQUASH )
1067 return 0;
1068 if( curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP)
1070 if(m_CommitList.m_IsOldFirst)
1071 index++;
1072 else
1073 index--;
1075 else
1076 return -1;
1078 }while(curRev->GetAction(&m_CommitList)&CTGitPath::LOGACTIONS_REBASE_SKIP);
1080 return -1;
1083 int CRebaseDlg::GoNext()
1085 if(m_CommitList.m_IsOldFirst)
1086 m_CurrentRebaseIndex++;
1087 else
1088 m_CurrentRebaseIndex--;
1089 return 0;
1092 int CRebaseDlg::StateAction()
1094 switch(this->m_RebaseStage)
1096 case CHOOSE_BRANCH:
1097 case CHOOSE_COMMIT_PICK_MODE:
1098 if(StartRebase())
1099 return -1;
1100 m_RebaseStage = REBASE_START;
1101 GoNext();
1102 break;
1105 return 0;
1107 void CRebaseDlg::SetContinueButtonText()
1109 CString Text;
1110 switch(this->m_RebaseStage)
1112 case CHOOSE_BRANCH:
1113 case CHOOSE_COMMIT_PICK_MODE:
1114 if(this->m_IsFastForward)
1115 Text.LoadString(IDS_PROC_STARTREBASEFFBUTTON);
1116 else
1117 Text.LoadString(IDS_PROC_STARTREBASEBUTTON);
1118 break;
1120 case REBASE_START:
1121 case REBASE_CONTINUE:
1122 case REBASE_SQUASH_CONFLICT:
1123 Text.LoadString(IDS_CONTINUEBUTTON);
1124 break;
1126 case REBASE_CONFLICT:
1127 Text.LoadString(IDS_COMMITBUTTON);
1128 break;
1129 case REBASE_EDIT:
1130 Text.LoadString(IDS_AMENDBUTTON);
1131 break;
1133 case REBASE_SQUASH_EDIT:
1134 Text.LoadString(IDS_COMMITBUTTON);
1135 break;
1137 case REBASE_ABORT:
1138 case REBASE_FINISH:
1139 Text.LoadString(IDS_FINISHBUTTON);
1140 break;
1142 case REBASE_DONE:
1143 Text.LoadString(IDS_DONE);
1144 break;
1146 this->GetDlgItem(IDC_REBASE_CONTINUE)->SetWindowText(Text);
1149 void CRebaseDlg::SetControlEnable()
1151 switch(this->m_RebaseStage)
1153 case CHOOSE_BRANCH:
1154 case CHOOSE_COMMIT_PICK_MODE:
1156 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(TRUE);
1157 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(TRUE);
1158 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(TRUE);
1159 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(TRUE);
1160 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(TRUE);
1162 if(!m_IsCherryPick)
1164 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(TRUE);
1165 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(TRUE);
1166 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(TRUE);
1168 //this->m_CommitList.m_IsEnableRebaseMenu=TRUE;
1169 this->m_CommitList.m_ContextMenuMask |= m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1170 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1171 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1172 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP);
1173 break;
1175 case REBASE_START:
1176 case REBASE_CONTINUE:
1177 case REBASE_ABORT:
1178 case REBASE_FINISH:
1179 case REBASE_CONFLICT:
1180 case REBASE_EDIT:
1181 case REBASE_SQUASH_CONFLICT:
1182 case REBASE_DONE:
1183 this->GetDlgItem(IDC_PICK_ALL)->EnableWindow(FALSE);
1184 this->GetDlgItem(IDC_EDIT_ALL)->EnableWindow(FALSE);
1185 this->GetDlgItem(IDC_SQUASH_ALL)->EnableWindow(FALSE);
1186 this->GetDlgItem(IDC_REBASE_COMBOXEX_BRANCH)->EnableWindow(FALSE);
1187 this->GetDlgItem(IDC_REBASE_COMBOXEX_UPSTREAM)->EnableWindow(FALSE);
1188 this->GetDlgItem(IDC_REBASE_CHECK_FORCE)->EnableWindow(FALSE);
1189 this->GetDlgItem(IDC_BUTTON_UP2)->EnableWindow(FALSE);
1190 this->GetDlgItem(IDC_BUTTON_DOWN2)->EnableWindow(FALSE);
1191 //this->m_CommitList.m_IsEnableRebaseMenu=FALSE;
1192 this->m_CommitList.m_ContextMenuMask &= ~(m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_PICK)|
1193 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SQUASH)|
1194 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_EDIT)|
1195 m_CommitList.GetContextMenuBit(CGitLogListBase::ID_REBASE_SKIP));
1197 if( m_RebaseStage == REBASE_DONE && (this->m_PostButtonTexts.GetCount() != 0) )
1199 this->GetDlgItem(IDC_STATUS_STATIC)->ShowWindow(SW_HIDE);
1200 this->GetDlgItem(IDC_REBASE_POST_BUTTON)->ShowWindow(SW_SHOWNORMAL);
1201 this->m_PostButton.RemoveAll();
1202 this->m_PostButton.AddEntries(m_PostButtonTexts);
1203 //this->GetDlgItem(IDC_REBASE_POST_BUTTON)->SetWindowText(this->m_PostButtonText);
1205 break;
1208 if(m_bThreadRunning)
1210 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1213 else
1215 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(TRUE);
1219 void CRebaseDlg::UpdateProgress()
1221 int index;
1222 CRect rect;
1224 if(m_CommitList.m_IsOldFirst)
1225 index = m_CurrentRebaseIndex+1;
1226 else
1227 index = m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1229 m_ProgressBar.SetRange(1,m_CommitList.GetItemCount());
1230 m_ProgressBar.SetPos(index);
1231 if (m_pTaskbarList)
1233 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1234 m_pTaskbarList->SetProgressValue(m_hWnd, index, m_CommitList.GetItemCount());
1237 if(m_CurrentRebaseIndex>=0 && m_CurrentRebaseIndex< m_CommitList.GetItemCount())
1239 CString text;
1240 text.Format(IDS_PROC_REBASING_PROGRESS, index, m_CommitList.GetItemCount());
1241 m_CtrlStatusText.SetWindowText(text);
1245 GitRev *prevRev=NULL, *curRev=NULL;
1247 if( m_CurrentRebaseIndex >= 0 && m_CurrentRebaseIndex< m_CommitList.m_arShownList.GetSize())
1249 curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1252 for(int i=0;i<m_CommitList.m_arShownList.GetSize();i++)
1254 prevRev=(GitRev*)m_CommitList.m_arShownList[i];
1255 if(prevRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_CURRENT)
1257 prevRev->GetAction(&m_CommitList) &= ~ CTGitPath::LOGACTIONS_REBASE_CURRENT;
1258 m_CommitList.GetItemRect(i,&rect,LVIR_BOUNDS);
1259 m_CommitList.InvalidateRect(rect);
1263 if(curRev)
1265 curRev->GetAction(&m_CommitList) |= CTGitPath::LOGACTIONS_REBASE_CURRENT;
1266 m_CommitList.GetItemRect(m_CurrentRebaseIndex,&rect,LVIR_BOUNDS);
1267 m_CommitList.InvalidateRect(rect);
1269 m_CommitList.EnsureVisible(m_CurrentRebaseIndex,FALSE);
1272 void CRebaseDlg::UpdateCurrentStatus()
1274 SetContinueButtonText();
1275 SetControlEnable();
1276 UpdateProgress();
1279 void CRebaseDlg::AddLogString(CString str)
1281 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, FALSE);
1282 CStringA sTextA = m_wndOutputRebase.StringForControl(str);//CUnicodeUtils::GetUTF8(str);
1283 this->m_wndOutputRebase.SendMessage(SCI_DOCUMENTEND);
1284 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)sTextA);
1285 this->m_wndOutputRebase.SendMessage(SCI_REPLACESEL, 0, (LPARAM)(LPCSTR)"\n");
1286 this->m_wndOutputRebase.SendMessage(SCI_SETREADONLY, TRUE);
1289 int CRebaseDlg::GetCurrentCommitID()
1291 if(m_CommitList.m_IsOldFirst)
1293 return this->m_CurrentRebaseIndex+1;
1296 else
1298 return m_CommitList.GetItemCount()-m_CurrentRebaseIndex;
1302 int CRebaseDlg::DoRebase()
1304 CString cmd,out;
1305 if(m_CurrentRebaseIndex <0)
1306 return 0;
1307 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1308 return 0;
1310 GitRev *pRev = (GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1311 int mode=pRev->GetAction(&m_CommitList) & CTGitPath::LOGACTIONS_REBASE_MODE_MASK;
1312 CString nocommit;
1314 if( mode== CTGitPath::LOGACTIONS_REBASE_SKIP)
1316 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1317 return 0;
1320 if( mode != CTGitPath::LOGACTIONS_REBASE_PICK )
1322 this->m_SquashMessage+= pRev->GetSubject();
1323 this->m_SquashMessage+= _T("\n");
1324 this->m_SquashMessage+= pRev->GetBody();
1326 else
1327 this->m_SquashMessage.Empty();
1329 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1330 nocommit=_T(" --no-commit ");
1332 CString log;
1333 log.Format(_T("%s %d: %s"),CTGitPath::GetActionName(mode),this->GetCurrentCommitID(),pRev->m_CommitHash.ToString());
1334 AddLogString(log);
1335 AddLogString(pRev->GetSubject());
1336 if (pRev->GetSubject().IsEmpty())
1338 CMessageBox::Show(m_hWnd, IDS_PROC_REBASE_EMPTYCOMMITMSG, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
1339 mode = CTGitPath::LOGACTIONS_REBASE_EDIT;
1342 CString cherryPickedFrom;
1343 if (m_bAddCherryPickedFrom)
1344 cherryPickedFrom = _T("-x ");
1346 cmd.Format(_T("git.exe cherry-pick %s%s %s"), cherryPickedFrom, nocommit, pRev->m_CommitHash.ToString());
1348 if(g_Git.Run(cmd,&out,CP_UTF8))
1350 AddLogString(out);
1351 CTGitPathList list;
1352 if(g_Git.ListConflictFile(list))
1354 AddLogString(_T("Get conflict files fail"));
1355 return -1;
1357 if(list.GetCount() == 0 )
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.
1369 // Squash Case
1370 if(CheckNextCommitIsSquash())
1371 { // no squash
1372 // let user edit last commmit message
1373 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1374 return -1;
1377 if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1378 m_RebaseStage = REBASE_SQUASH_CONFLICT;
1379 else
1380 m_RebaseStage = REBASE_CONFLICT;
1381 return -1;
1384 else
1386 AddLogString(out);
1387 if(mode == CTGitPath::LOGACTIONS_REBASE_PICK)
1389 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1390 return 0;
1392 if(mode == CTGitPath::LOGACTIONS_REBASE_EDIT)
1394 this->m_RebaseStage = REBASE_EDIT ;
1395 return -1; // Edit return -1 to stop rebase.
1398 // Squash Case
1399 if(CheckNextCommitIsSquash())
1400 { // no squash
1401 // let user edit last commmit message
1402 this->m_RebaseStage = REBASE_SQUASH_EDIT;
1403 return -1;
1405 else if(mode == CTGitPath::LOGACTIONS_REBASE_SQUASH)
1406 pRev->GetAction(&m_CommitList)|= CTGitPath::LOGACTIONS_REBASE_DONE;
1409 return 0;
1412 BOOL CRebaseDlg::IsEnd()
1414 if(m_CommitList.m_IsOldFirst)
1415 return m_CurrentRebaseIndex>= this->m_CommitList.GetItemCount();
1416 else
1417 return m_CurrentRebaseIndex<0;
1420 int CRebaseDlg::RebaseThread()
1422 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
1424 int ret=0;
1425 while(1)
1427 if( m_RebaseStage == REBASE_START )
1429 if( this->StartRebase() )
1431 InterlockedExchange(&m_bThreadRunning, FALSE);
1432 ret = -1;
1433 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1434 break;
1436 m_RebaseStage = REBASE_CONTINUE;
1439 else if( m_RebaseStage == REBASE_CONTINUE )
1441 this->GoNext();
1442 UpdateCurrentStatus();
1443 if(IsEnd())
1445 ret = 0;
1446 m_RebaseStage = REBASE_FINISH;
1449 else
1451 ret = DoRebase();
1453 if( ret )
1455 break;
1460 else if( m_RebaseStage == REBASE_FINISH )
1462 FinishRebase();
1463 m_RebaseStage = REBASE_DONE;
1464 if (m_pTaskbarList)
1465 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1466 break;
1469 else
1471 break;
1473 this->PostMessage(MSG_REBASE_UPDATE_UI);
1474 //this->UpdateCurrentStatus();
1477 InterlockedExchange(&m_bThreadRunning, FALSE);
1478 this->PostMessage(MSG_REBASE_UPDATE_UI);
1479 return ret;
1482 void CRebaseDlg::ListConflictFile()
1484 this->m_FileListCtrl.Clear();
1485 CTGitPathList list;
1486 CTGitPath path;
1487 list.AddPath(path);
1489 m_FileListCtrl.m_bIsRevertTheirMy = !m_IsCherryPick;
1491 this->m_FileListCtrl.GetStatus(&list,true);
1492 this->m_FileListCtrl.Show(CTGitPath::LOGACTIONS_UNMERGED|CTGitPath::LOGACTIONS_MODIFIED|CTGitPath::LOGACTIONS_ADDED|CTGitPath::LOGACTIONS_DELETED,
1493 CTGitPath::LOGACTIONS_UNMERGED);
1496 LRESULT CRebaseDlg::OnRebaseUpdateUI(WPARAM,LPARAM)
1498 UpdateCurrentStatus();
1499 if(m_CurrentRebaseIndex <0)
1500 return 0;
1501 if(m_CurrentRebaseIndex >= m_CommitList.GetItemCount() )
1502 return 0;
1503 GitRev *curRev=(GitRev*)m_CommitList.m_arShownList[m_CurrentRebaseIndex];
1505 switch(m_RebaseStage)
1507 case REBASE_CONFLICT:
1508 case REBASE_SQUASH_CONFLICT:
1509 ListConflictFile();
1510 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_CONFLICT);
1511 if (m_pTaskbarList)
1512 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1513 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1514 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1515 break;
1516 case REBASE_EDIT:
1517 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1518 if (m_pTaskbarList)
1519 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1520 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1521 this->m_LogMessageCtrl.SetText(curRev->GetSubject()+_T("\n")+curRev->GetBody());
1522 break;
1523 case REBASE_SQUASH_EDIT:
1524 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_MESSAGE);
1525 this->m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1526 this->m_LogMessageCtrl.SetText(this->m_SquashMessage);
1527 if (m_pTaskbarList)
1528 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_PAUSED);
1529 break;
1530 default:
1531 this->m_ctrlTabCtrl.SetActiveTab(REBASE_TAB_LOG);
1533 return 0;
1535 void CRebaseDlg::OnCancel()
1537 OnBnClickedAbort();
1539 void CRebaseDlg::OnBnClickedAbort()
1541 if (m_pTaskbarList)
1542 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1544 CString cmd,out;
1545 CString pron = m_OrigUpstreamHash.ToString();
1546 if(m_OrigUpstreamHash.IsEmpty())
1548 __super::OnCancel();
1551 if(m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage== CHOOSE_COMMIT_PICK_MODE)
1553 return;
1556 if(CMessageBox::Show(NULL, IDS_PROC_REBASE_ABORT, IDS_APPNAME, MB_YESNO) != IDYES)
1557 return;
1559 if(this->m_IsFastForward)
1561 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1562 if(g_Git.Run(cmd,&out,CP_UTF8))
1564 AddLogString(out);
1565 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1567 __super::OnCancel();
1568 return;
1570 cmd.Format(_T("git.exe checkout -f %s"),g_Git.FixBranchName(this->m_UpstreamCtrl.GetString()));
1571 if(g_Git.Run(cmd,&out,CP_UTF8))
1573 AddLogString(out);
1574 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1575 __super::OnCancel();
1576 return;
1579 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigUpstreamHash.ToString());
1580 if(g_Git.Run(cmd,&out,CP_UTF8))
1582 AddLogString(out);
1583 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1584 __super::OnCancel();
1585 return;
1588 if(this->m_IsCherryPick) //there are not "branch" at cherry pick mode
1590 __super::OnCancel();
1591 return;
1594 cmd.Format(_T("git checkout -f %s"),this->m_BranchCtrl.GetString());
1595 if(g_Git.Run(cmd,&out,CP_UTF8))
1597 AddLogString(out);
1598 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1599 __super::OnCancel();
1600 return;
1603 cmd.Format(_T("git.exe reset --hard %s"),this->m_OrigBranchHash.ToString());
1604 if(g_Git.Run(cmd,&out,CP_UTF8))
1606 AddLogString(out);
1607 ::MessageBox(this->m_hWnd, _T("Unrecoverable error on cleanup:\n") + out, _T("TortoiseGit"), MB_ICONERROR);
1609 __super::OnCancel();
1612 void CRebaseDlg::OnBnClickedButtonBrowse()
1614 if(CBrowseRefsDlg::PickRefForCombo(&m_UpstreamCtrl, gPickRef_NoTag))
1615 OnCbnSelchangeUpstream();
1618 void CRebaseDlg::OnBnClickedRebaseCheckForce()
1620 this->UpdateData();
1621 this->FetchLogList();
1622 if(this->CheckRebaseCondition())
1624 /* Disable Start Rebase */
1625 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1629 void CRebaseDlg::OnBnClickedRebasePostButton()
1631 this->m_Upstream=this->m_UpstreamCtrl.GetString();
1632 this->m_Branch=this->m_BranchCtrl.GetString();
1634 this->EndDialog(IDC_REBASE_POST_BUTTON+this->m_PostButton.GetCurrentEntry());
1637 void CRebaseDlg::Refresh()
1639 if(this->m_IsCherryPick)
1640 return ;
1642 if(this->m_RebaseStage == CHOOSE_BRANCH )
1644 this->UpdateData();
1645 this->FetchLogList();
1646 if(this->CheckRebaseCondition())
1648 /* Disable Start Rebase */
1649 this->GetDlgItem(IDC_REBASE_CONTINUE)->EnableWindow(FALSE);
1654 void CRebaseDlg::OnBnClickedButtonUp2()
1656 POSITION pos;
1657 pos = m_CommitList.GetFirstSelectedItemPosition();
1659 // do nothing if the first selected item is the first item in the list
1660 if (m_CommitList.GetNextSelectedItem(pos) == 0)
1661 return;
1663 pos = m_CommitList.GetFirstSelectedItemPosition();
1665 bool changed = false;
1666 while(pos)
1668 int index=m_CommitList.GetNextSelectedItem(pos);
1669 if(index>=1)
1671 CGitHash old = m_CommitList.m_logEntries[index-1];
1672 m_CommitList.m_logEntries[index-1] = m_CommitList.m_logEntries[index];
1673 m_CommitList.m_logEntries[index] = old;
1674 m_CommitList.SetItemState(index-1, LVIS_SELECTED, LVIS_SELECTED);
1675 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1676 changed = true;
1679 if (changed)
1681 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1682 m_CommitList.Invalidate();
1683 m_CommitList.SetFocus();
1687 void CRebaseDlg::OnBnClickedButtonDown2()
1689 if (m_CommitList.GetSelectedCount() == 0)
1690 return;
1692 POSITION pos;
1693 pos = m_CommitList.GetFirstSelectedItemPosition();
1694 bool changed = false;
1695 // use an array to store all selected item indexes; the user won't select too much items
1696 int* indexes = NULL;
1697 indexes = new int[m_CommitList.GetSelectedCount()];
1698 int i = 0;
1699 while(pos)
1701 indexes[i++] = m_CommitList.GetNextSelectedItem(pos);
1703 // don't move any item if the last selected item is the last item in the m_CommitList
1704 // (that would change the order of the selected items)
1705 if(indexes[m_CommitList.GetSelectedCount() - 1] < m_CommitList.GetItemCount() - 1)
1707 // iterate over the indexes backwards in order to correctly move multiselected items
1708 for (i = m_CommitList.GetSelectedCount() - 1; i >= 0; i--)
1710 int index = indexes[i];
1711 CGitHash old = m_CommitList.m_logEntries[index+1];
1712 m_CommitList.m_logEntries[index+1] = m_CommitList.m_logEntries[index];
1713 m_CommitList.m_logEntries[index] = old;
1714 m_CommitList.SetItemState(index, 0, LVIS_SELECTED);
1715 m_CommitList.SetItemState(index+1, LVIS_SELECTED, LVIS_SELECTED);
1716 changed = true;
1719 delete [] indexes;
1720 indexes = NULL;
1721 if (changed)
1723 m_CommitList.RecalculateShownList(&m_CommitList.m_arShownList);
1724 m_CommitList.Invalidate();
1725 m_CommitList.SetFocus();
1729 LRESULT CRebaseDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1731 m_pTaskbarList.Release();
1732 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1733 return 0;
1736 void CRebaseDlg::OnLvnItemchangedLoglist(NMHDR *pNMHDR, LRESULT *pResult)
1738 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
1739 *pResult = 0;
1740 if(m_CommitList.m_bNoDispUpdates)
1741 return;
1742 if (pNMLV->iItem >= 0)
1744 this->m_CommitList.m_nSearchIndex = pNMLV->iItem;
1745 if (pNMLV->iSubItem != 0)
1746 return;
1747 if ((pNMLV->iItem == m_CommitList.m_arShownList.GetCount()))
1749 // remove the selected state
1750 if (pNMLV->uChanged & LVIF_STATE)
1752 m_CommitList.SetItemState(pNMLV->iItem, 0, LVIS_SELECTED);
1753 FillLogMessageCtrl();
1755 return;
1757 if (pNMLV->uChanged & LVIF_STATE)
1759 FillLogMessageCtrl();
1762 else
1764 FillLogMessageCtrl();
1768 void CRebaseDlg::FillLogMessageCtrl()
1770 int selCount = m_CommitList.GetSelectedCount();
1771 if (selCount == 1 && (m_RebaseStage == CHOOSE_BRANCH || m_RebaseStage == CHOOSE_COMMIT_PICK_MODE))
1773 POSITION pos = m_CommitList.GetFirstSelectedItemPosition();
1774 int selIndex = m_CommitList.GetNextSelectedItem(pos);
1775 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_CommitList.m_arShownList.SafeGetAt(selIndex));
1776 m_FileListCtrl.UpdateWithGitPathList(pLogEntry->GetFiles(&m_CommitList));
1777 m_FileListCtrl.m_CurrentVersion = pLogEntry->m_CommitHash;
1778 m_FileListCtrl.Show(GITSLC_SHOWVERSIONED);
1779 m_LogMessageCtrl.Call(SCI_SETREADONLY, FALSE);
1780 m_LogMessageCtrl.SetText(pLogEntry->GetSubject() + _T("\n") + pLogEntry->GetBody());
1781 m_LogMessageCtrl.Call(SCI_SETREADONLY, TRUE);
1784 void CRebaseDlg::OnBnClickedCheckCherryPickedFrom()
1786 UpdateData();