Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / TortoiseProc / Commands / SVNRebaseCommand.cpp
blobf9eddb8e82338cc678e69c743081b5c791dfcadd
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2018 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "SVNRebaseCommand.h"
22 #include "SysProgressDlg.h"
23 #include "ProgressDlg.h"
24 #include "MessageBox.h"
25 #include "Git.h"
26 #include "RebaseDlg.h"
27 #include "AppUtils.h"
29 bool SVNRebaseCommand::Execute()
31 bool isStash = false;
33 if(!g_Git.CheckCleanWorkTree())
35 if (CMessageBox::Show(hwndExplorer, g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_ERROR_NOCLEAN_STASH)), L"TortoiseGit", 1, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_STASHBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
37 CSysProgressDlg sysProgressDlg;
38 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
39 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
40 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
41 sysProgressDlg.SetShowProgressBar(false);
42 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
43 sysProgressDlg.ShowModeless((HWND)nullptr, true);
45 CString out;
46 if (g_Git.Run(L"git.exe stash", &out, CP_UTF8))
48 sysProgressDlg.Stop();
49 MessageBox(hwndExplorer, out, L"TortoiseGit", MB_OK | MB_ICONERROR);
50 return false;
52 sysProgressDlg.Stop();
53 isStash = true;
55 else
57 return false;
61 CRebaseDlg dlg;
63 // dlg.m_PreCmd=L"git.exe svn fetch";
65 CString out, err;
66 if (!g_Git.Run(L"git.exe config svn-remote.svn.fetch", &out, &err, CP_UTF8))
68 int start = out.Find(L':');
69 if( start >=0 )
70 out=out.Mid(start);
72 if (CStringUtils::StartsWith(out, L":refs"))
73 out=out.Mid(6);
75 start = 0;
76 out = out.Tokenize(L"\n", start);
78 else
80 MessageBox(hwndExplorer, L"Could not get \"svn-remote.svn.fetch\" config value.\n" + out + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
81 return false;
84 dlg.m_Upstream=out;
86 CGitHash UpStreamOldHash,HeadHash,UpStreamNewHash;
87 if (g_Git.GetHash(UpStreamOldHash, out))
89 MessageBox(hwndExplorer, g_Git.GetGitLastErr(L"Could not get hash of SVN branch."), L"TortoiseGit", MB_ICONERROR);
90 return false;
92 if (g_Git.GetHash(HeadHash, L"HEAD"))
94 MessageBox(hwndExplorer, g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
95 return false;
97 CProgressDlg progress;
98 progress.m_GitCmd = L"git.exe svn fetch";
99 progress.m_AutoClose = AUTOCLOSE_IF_NO_ERRORS;
101 if(progress.DoModal()!=IDOK)
102 return false;
104 if(progress.m_GitStatus)
105 return false;
107 if (g_Git.GetHash(UpStreamNewHash, out))
109 MessageBox(hwndExplorer, g_Git.GetGitLastErr(L"Could not get upstream hash after fetching."), L"TortoiseGit", MB_ICONERROR);
110 return false;
113 //everything updated
114 if(UpStreamNewHash==HeadHash)
116 MessageBox(hwndExplorer, g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_PROC_EVERYTHINGUPDATED)), L"TortoiseGit", MB_OK | MB_ICONQUESTION);
117 if(isStash)
118 askIfUserWantsToStashPop();
120 return true;
123 //fast forward;
124 if (g_Git.IsFastForward(L"HEAD", out))
126 CProgressDlg progressReset;
127 CString cmd;
128 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)out);
129 progressReset.m_GitCmd = cmd;
130 progressReset.m_AutoClose = AUTOCLOSE_IF_NO_ERRORS;
132 if (progressReset.DoModal() != IDOK)
133 return false;
134 else
136 MessageBox(hwndExplorer, g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_PROC_FASTFORWARD)) + L":\n" + progressReset.m_LogText, L"TortoiseGit", MB_OK | MB_ICONQUESTION);
137 if(isStash)
138 askIfUserWantsToStashPop();
140 return true;
144 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
145 //need rebase
146 INT_PTR response = dlg.DoModal();
147 if (response == IDOK || response == IDC_REBASE_POST_BUTTON)
149 if(isStash)
150 askIfUserWantsToStashPop();
151 if (response == IDC_REBASE_POST_BUTTON)
153 CString cmd = L"/command:log";
154 cmd += L" /path:\"" + g_Git.m_CurrentDir + L'"';
155 CAppUtils::RunTortoiseGitProc(cmd);
157 return true;
159 return false;
162 void SVNRebaseCommand::askIfUserWantsToStashPop()
164 if (MessageBox(hwndExplorer, g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_DCOMMIT_STASH_POP)), L"TortoiseGit", MB_YESNO | MB_ICONINFORMATION) == IDYES)
165 CAppUtils::StashPop(hwndExplorer);