Add more checks for git repository before executing commands
[TortoiseGit.git] / src / TortoiseProc / Commands / SVNRebaseCommand.cpp
blob483afd2111fb54843c4b62699815c28a41c66f37
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2019 - 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 if (!GitAdminDir::HasAdminDir(g_Git.m_CurrentDir))
33 CMessageBox::Show(GetExplorerHWND(), IDS_NOWORKINGCOPY, IDS_APPNAME, MB_ICONERROR);
34 return false;
37 bool isStash = false;
39 if(!g_Git.CheckCleanWorkTree())
41 if (CMessageBox::Show(GetExplorerHWND(), 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)
43 CSysProgressDlg sysProgressDlg;
44 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
45 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
46 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
47 sysProgressDlg.SetShowProgressBar(false);
48 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
49 sysProgressDlg.ShowModeless((HWND)nullptr, true);
51 CString out;
52 if (g_Git.Run(L"git.exe stash", &out, CP_UTF8))
54 sysProgressDlg.Stop();
55 MessageBox(GetExplorerHWND(), out, L"TortoiseGit", MB_OK | MB_ICONERROR);
56 return false;
58 sysProgressDlg.Stop();
59 isStash = true;
61 else
63 return false;
67 CRebaseDlg dlg;
69 // dlg.m_PreCmd=L"git.exe svn fetch";
71 CString out, err;
72 if (!g_Git.Run(L"git.exe config svn-remote.svn.fetch", &out, &err, CP_UTF8))
74 int start = out.Find(L':');
75 if( start >=0 )
76 out=out.Mid(start);
78 if (CStringUtils::StartsWith(out, L":refs"))
79 out = out.Mid((int)wcslen(L":refs") + 1);
81 start = 0;
82 out = out.Tokenize(L"\n", start);
84 else
86 MessageBox(GetExplorerHWND(), L"Could not get \"svn-remote.svn.fetch\" config value.\n" + out + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
87 return false;
90 dlg.m_Upstream=out;
92 CGitHash UpStreamOldHash,HeadHash,UpStreamNewHash;
93 if (g_Git.GetHash(UpStreamOldHash, out))
95 MessageBox(GetExplorerHWND(), g_Git.GetGitLastErr(L"Could not get hash of SVN branch."), L"TortoiseGit", MB_ICONERROR);
96 return false;
98 if (g_Git.GetHash(HeadHash, L"HEAD"))
100 MessageBox(GetExplorerHWND(), g_Git.GetGitLastErr(L"Could not get HEAD hash."), L"TortoiseGit", MB_ICONERROR);
101 return false;
103 CProgressDlg progress;
104 progress.m_GitCmd = L"git.exe svn fetch";
105 progress.m_AutoClose = AUTOCLOSE_IF_NO_ERRORS;
107 if(progress.DoModal()!=IDOK)
108 return false;
110 if(progress.m_GitStatus)
111 return false;
113 if (g_Git.GetHash(UpStreamNewHash, out))
115 MessageBox(GetExplorerHWND(), g_Git.GetGitLastErr(L"Could not get upstream hash after fetching."), L"TortoiseGit", MB_ICONERROR);
116 return false;
119 //everything updated
120 if(UpStreamNewHash==HeadHash)
122 MessageBox(GetExplorerHWND(), g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_PROC_EVERYTHINGUPDATED)), L"TortoiseGit", MB_OK | MB_ICONQUESTION);
123 if(isStash)
124 askIfUserWantsToStashPop();
126 return true;
129 //fast forward;
130 if (g_Git.IsFastForward(L"HEAD", out))
132 CProgressDlg progressReset;
133 CString cmd;
134 cmd.Format(L"git.exe reset --hard %s --", (LPCTSTR)out);
135 progressReset.m_GitCmd = cmd;
136 progressReset.m_AutoClose = AUTOCLOSE_IF_NO_ERRORS;
138 if (progressReset.DoModal() != IDOK)
139 return false;
140 else
142 MessageBox(GetExplorerHWND(), g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_PROC_FASTFORWARD)) + L":\n" + progressReset.m_LogText, L"TortoiseGit", MB_OK | MB_ICONQUESTION);
143 if(isStash)
144 askIfUserWantsToStashPop();
146 return true;
150 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
151 //need rebase
152 INT_PTR response = dlg.DoModal();
153 if (response == IDOK || response == IDC_REBASE_POST_BUTTON)
155 if(isStash)
156 askIfUserWantsToStashPop();
157 if (response == IDC_REBASE_POST_BUTTON)
159 CString cmd = L"/command:log";
160 cmd += L" /path:\"" + g_Git.m_CurrentDir + L'"';
161 CAppUtils::RunTortoiseGitProc(cmd);
163 return true;
165 return false;
168 void SVNRebaseCommand::askIfUserWantsToStashPop()
170 if (MessageBox(GetExplorerHWND(), g_Git.m_CurrentDir + L"\r\n" + CString(MAKEINTRESOURCE(IDS_DCOMMIT_STASH_POP)), L"TortoiseGit", MB_YESNO | MB_ICONINFORMATION) == IDYES)
171 CAppUtils::StashPop(GetExplorerHWND());