Can run cleanup without using recycle bin and add dry run option
[TortoiseGit.git] / src / TortoiseProc / Commands / CleanupCommand.cpp
blobe01654338bbc7fc39f0497026f9effbd19df5fd4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009,2011-2012 - TortoiseGit
4 // Copyright (C) 2007-2008 - TortoiseSVN
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.
20 #include "StdAfx.h"
21 #include "CleanupCommand.h"
23 #include "MessageBox.h"
24 #include "ProgressDlg.h"
25 #include "ShellUpdater.h"
26 #include "CleanTypeDlg.h"
27 #include "..\Utils\UnicodeUtils.h"
28 #include "ProjectProperties.h"
29 #include "SysProgressDlg.h"
31 static CString UnescapeQuotePath(CString s)
33 CStringA t;
34 for (int i = 0; i < s.GetLength(); i++)
36 if (s[i] == '\\' && i + 3 < s.GetLength())
38 char c = (char)((s[i + 1] - '0') * 64 + (s[i + 2] - '0') * 8 + (s[i + 3] - '0'));
39 t += c;
40 i += 3;
42 else
44 t += s[i];
48 return CUnicodeUtils::GetUnicode(t);
51 bool CleanupCommand::Execute()
53 bool bRet = false;
55 CCleanTypeDlg dlg;
56 if( dlg.DoModal() == IDOK)
58 ProjectProperties pp;
59 BOOL quotepath = TRUE;
60 pp.GetBOOLProps(quotepath, _T("core.quotepath"));
62 CString cmd;
63 cmd.Format(_T("git clean"));
64 if (dlg.m_bDryRun || !dlg.m_bNoRecycleBin)
65 cmd += _T(" -n ");
66 if(dlg.m_bDir)
67 cmd += _T(" -d ");
68 switch(dlg.m_CleanType)
70 case 0:
71 cmd += _T(" -fx");
72 break;
73 case 1:
74 cmd += _T(" -f");
75 break;
76 case 2:
77 cmd += _T(" -fX");
78 break;
81 if (dlg.m_bDryRun || dlg.m_bNoRecycleBin)
83 CProgressDlg progress;
84 for (int i = 0; i < this->pathList.GetCount(); i++)
86 CString path;
87 if (this->pathList[i].IsDirectory())
88 path = pathList[i].GetGitPathString();
89 else
90 path = pathList[i].GetContainingDirectory().GetGitPathString();
92 progress.m_GitCmdList.push_back(cmd + _T(" \"") + path + _T("\""));
94 if (progress.DoModal()==IDOK)
95 return TRUE;
97 else
99 CSysProgressDlg sysProgressDlg;
100 sysProgressDlg.SetAnimation(IDR_CLEANUPANI);
101 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
102 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CLEANUP_INFO1)));
103 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
104 sysProgressDlg.SetShowProgressBar(false);
105 sysProgressDlg.ShowModeless((HWND)NULL, true);
107 CString cmdout, cmdouterr;
108 if (g_Git.Run(cmd, &cmdout, &cmdouterr, CP_UTF8)) {
109 MessageBox(NULL, cmdouterr, _T("TortoiseGit"), MB_ICONERROR);
110 return FALSE;
113 if (sysProgressDlg.HasUserCancelled())
115 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
116 return FALSE;
119 int pos = 0;
120 CString token = cmdout.Tokenize(_T("\n"), pos);
121 CTGitPathList delList;
122 while (!token.IsEmpty())
124 if (token.Mid(0, 13) == _T("Would remove "))
126 CString tempPath = token.Mid(13).TrimRight();
127 if (quotepath)
129 tempPath = UnescapeQuotePath(tempPath.Trim(_T('"')));
131 delList.AddPath(CTGitPath(tempPath));
134 token = cmdout.Tokenize(_T("\n"), pos);
137 if (sysProgressDlg.HasUserCancelled())
139 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
140 return FALSE;
143 delList.DeleteAllFiles(true, false);
145 sysProgressDlg.Stop();
148 #if 0
149 CProgressDlg progress;
150 progress.SetTitle(IDS_PROC_CLEANUP);
151 progress.SetAnimation(IDR_CLEANUPANI);
152 progress.SetShowProgressBar(false);
153 progress.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CLEANUP_INFO1)));
154 progress.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROC_CLEANUP_INFO2)));
155 progress.ShowModeless(hwndExplorer);
157 CString strSuccessfullPaths, strFailedPaths;
158 for (int i=0; i<pathList.GetCount(); ++i)
160 SVN svn;
161 if (!svn.CleanUp(pathList[i]))
163 strFailedPaths += _T("- ") + pathList[i].GetWinPathString() + _T("\n");
164 strFailedPaths += svn.GetLastErrorMessage() + _T("\n\n");
166 else
168 strSuccessfullPaths += _T("- ") + pathList[i].GetWinPathString() + _T("\n");
170 // after the cleanup has finished, crawl the path downwards and send a change
171 // notification for every directory to the shell. This will update the
172 // overlays in the left tree view of the explorer.
173 CDirFileEnum crawler(pathList[i].GetWinPathString());
174 CString sPath;
175 bool bDir = false;
176 CTSVNPathList updateList;
177 while (crawler.NextFile(sPath, &bDir))
179 if ((bDir) && (!g_SVNAdminDir.IsAdminDirPath(sPath)))
181 updateList.AddPath(CTSVNPath(sPath));
184 updateList.AddPath(pathList[i]);
185 CShellUpdater::Instance().AddPathsForUpdate(updateList);
186 CShellUpdater::Instance().Flush();
187 updateList.SortByPathname(true);
188 for (INT_PTR i=0; i<updateList.GetCount(); ++i)
190 SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, updateList[i].GetWinPath(), NULL);
191 ATLTRACE(_T("notify change for path %s\n"), updateList[i].GetWinPath());
195 progress.Stop();
197 CString strMessage;
198 if ( !strSuccessfullPaths.IsEmpty() )
200 CString tmp;
201 tmp.Format(IDS_PROC_CLEANUPFINISHED, (LPCTSTR)strSuccessfullPaths);
202 strMessage += tmp;
203 bRet = true;
205 if ( !strFailedPaths.IsEmpty() )
207 if (!strMessage.IsEmpty())
208 strMessage += _T("\n");
209 CString tmp;
210 tmp.Format(IDS_PROC_CLEANUPFINISHED_FAILED, (LPCTSTR)strFailedPaths);
211 strMessage += tmp;
212 bRet = false;
214 CMessageBox::Show(hwndExplorer, strMessage, _T("TortoiseGit"), MB_OK | (strFailedPaths.IsEmpty()?MB_ICONINFORMATION:MB_ICONERROR));
215 #endif
216 CShellUpdater::Instance().Flush();
217 return bRet;