1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009,2011-2013 - 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.
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
)
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'));
48 return CUnicodeUtils::GetUnicode(t
);
51 bool CleanupCommand::Execute()
56 if( dlg
.DoModal() == IDOK
)
59 BOOL quotepath
= TRUE
;
60 pp
.GetBOOLProps(quotepath
, _T("core.quotepath"));
63 cmd
.Format(_T("git clean"));
64 if (dlg
.m_bDryRun
|| !dlg
.m_bNoRecycleBin
)
68 switch(dlg
.m_CleanType
)
81 if (dlg
.m_bDryRun
|| dlg
.m_bNoRecycleBin
)
83 CProgressDlg progress
;
84 for (int i
= 0; i
< this->pathList
.GetCount(); ++i
)
87 if (this->pathList
[i
].IsDirectory())
88 path
= pathList
[i
].GetGitPathString();
90 path
= pathList
[i
].GetContainingDirectory().GetGitPathString();
92 progress
.m_GitCmdList
.push_back(cmd
+ _T(" \"") + path
+ _T("\""));
94 if (progress
.DoModal()==IDOK
)
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
);
113 if (sysProgressDlg
.HasUserCancelled())
115 CMessageBox::Show(NULL
, IDS_SVN_USERCANCELLED
, IDS_APPNAME
, MB_OK
);
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();
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
);
143 delList
.DeleteAllFiles(true, false);
145 sysProgressDlg
.Stop();
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
)
161 if (!svn
.CleanUp(pathList
[i
]))
163 strFailedPaths
+= _T("- ") + pathList
[i
].GetWinPathString() + _T("\n");
164 strFailedPaths
+= svn
.GetLastErrorMessage() + _T("\n\n");
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());
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());
198 if ( !strSuccessfullPaths
.IsEmpty() )
201 tmp
.Format(IDS_PROC_CLEANUPFINISHED
, (LPCTSTR
)strSuccessfullPaths
);
205 if ( !strFailedPaths
.IsEmpty() )
207 if (!strMessage
.IsEmpty())
208 strMessage
+= _T("\n");
210 tmp
.Format(IDS_PROC_CLEANUPFINISHED_FAILED
, (LPCTSTR
)strFailedPaths
);
214 CMessageBox::Show(hwndExplorer
, strMessage
, _T("TortoiseGit"), MB_OK
| (strFailedPaths
.IsEmpty()?MB_ICONINFORMATION
:MB_ICONERROR
));
216 CShellUpdater::Instance().Flush();