OSDN Git Service

Add Patch Import Command (git am)
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / Commands / RenameCommand.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2007-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "RenameCommand.h"\r
21 \r
22 #include "MessageBox.h"\r
23 //#include "SVNProgressDlg.h"\r
24 //#include "ProgressDlg.h"\r
25 #include "RenameDlg.h"\r
26 #include "InputLogDlg.h"\r
27 #include "Git.h"\r
28 #include "DirFileEnum.h"\r
29 #include "ShellUpdater.h"\r
30 \r
31 bool RenameCommand::Execute()\r
32 {\r
33         bool bRet = false;\r
34         CString filename = cmdLinePath.GetFileOrDirectoryName();\r
35         CString basePath = cmdLinePath.GetContainingDirectory().GetGitPathString();\r
36         //::SetCurrentDirectory(basePath);\r
37 \r
38         // show the rename dialog until the user either cancels or enters a new\r
39         // name (one that's different to the original name\r
40         CString sNewName;\r
41         do \r
42         {\r
43                 CRenameDlg dlg;\r
44                 dlg.m_name = filename;\r
45                 if (dlg.DoModal() != IDOK)\r
46                         return FALSE;\r
47                 sNewName = dlg.m_name;\r
48         } while(PathIsRelative(sNewName) && !PathIsURL(sNewName) && (sNewName.IsEmpty() || (sNewName.Compare(filename)==0)));\r
49 \r
50         if(!basePath.IsEmpty())\r
51                 sNewName=basePath+"/"+sNewName;\r
52 \r
53         CString cmd;\r
54         CString output;\r
55         cmd.Format(_T("git.cmd mv \"%s\" \"%s\""),\r
56                                         cmdLinePath.GetGitPathString(),\r
57                                         sNewName);\r
58                                                                         \r
59         if(g_Git.Run(cmd,&output))\r
60         {\r
61                 CMessageBox::Show(hwndExplorer, output, _T("TortoiseGit"), MB_OK);\r
62         }\r
63 \r
64 #if 0\r
65         TRACE(_T("rename file %s to %s\n"), (LPCTSTR)cmdLinePath.GetWinPathString(), (LPCTSTR)sNewName);\r
66         CTSVNPath destinationPath(basePath);\r
67         if (PathIsRelative(sNewName) && !PathIsURL(sNewName))\r
68                 destinationPath.AppendPathString(sNewName);\r
69         else\r
70                 destinationPath.SetFromWin(sNewName);\r
71         // check if a rename just with case is requested: that's not possible on windows file systems\r
72         // and we have to show an error.\r
73         if (cmdLinePath.GetWinPathString().CompareNoCase(destinationPath.GetWinPathString())==0)\r
74         {\r
75                 //rename to the same file!\r
76                 CString sHelpPath = theApp.m_pszHelpFilePath;\r
77                 sHelpPath += _T("::/tsvn-dug-rename.html#tsvn-dug-renameincase");\r
78                 CMessageBox::Show(hwndExplorer, IDS_PROC_CASERENAME, IDS_APPNAME, MB_OK|MB_HELP, sHelpPath);\r
79         }\r
80         else\r
81         {\r
82                 CString sMsg;\r
83                 if (SVN::PathIsURL(cmdLinePath))\r
84                 {\r
85                         // rename an URL.\r
86                         // Ask for a commit message, then rename directly in\r
87                         // the repository\r
88                         CInputLogDlg input;\r
89                         CString sUUID;\r
90                         SVN svn;\r
91                         svn.GetRepositoryRootAndUUID(cmdLinePath, sUUID);\r
92                         input.SetUUID(sUUID);\r
93                         CString sHint;\r
94                         sHint.Format(IDS_INPUT_MOVE, (LPCTSTR)cmdLinePath.GetSVNPathString(), (LPCTSTR)destinationPath.GetSVNPathString());\r
95                         input.SetActionText(sHint);\r
96                         if (input.DoModal() == IDOK)\r
97                         {\r
98                                 sMsg = input.GetLogMessage();\r
99                         }\r
100                         else\r
101                         {\r
102                                 return FALSE;\r
103                         }\r
104                 }\r
105                 if ((cmdLinePath.IsDirectory())||(pathList.GetCount() > 1))\r
106                 {\r
107                         // renaming a directory can take a while: use the\r
108                         // progress dialog to show the progress of the renaming\r
109                         // operation.\r
110                         CSVNProgressDlg progDlg;\r
111                         progDlg.SetCommand(CSVNProgressDlg::SVNProgress_Rename);\r
112                         if (parser.HasVal(_T("closeonend")))\r
113                                 progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));\r
114                         progDlg.SetPathList(pathList);\r
115                         progDlg.SetUrl(destinationPath.GetWinPathString());\r
116                         progDlg.SetCommitMessage(sMsg);\r
117                         progDlg.SetRevision(SVNRev::REV_WC);\r
118                         progDlg.DoModal();\r
119                         bRet = !progDlg.DidErrorsOccur();\r
120                 }\r
121                 else\r
122                 {\r
123                         SVN svn;\r
124                         CString sFilemask = cmdLinePath.GetFilename();\r
125                         if (sFilemask.ReverseFind('.')>=0)\r
126                         {\r
127                                 sFilemask = sFilemask.Left(sFilemask.ReverseFind('.'));\r
128                         }\r
129                         else\r
130                                 sFilemask.Empty();\r
131                         CString sNewMask = sNewName;\r
132                         if (sNewMask.ReverseFind('.'>=0))\r
133                         {\r
134                                 sNewMask = sNewMask.Left(sNewMask.ReverseFind('.'));\r
135                         }\r
136                         else\r
137                                 sNewMask.Empty();\r
138 \r
139                         if (((!sFilemask.IsEmpty()) && (parser.HasKey(_T("noquestion")))) ||\r
140                                 (cmdLinePath.GetFileExtension().Compare(destinationPath.GetFileExtension())!=0))\r
141                         {\r
142                                 if (!svn.Move(CTSVNPathList(cmdLinePath), destinationPath, TRUE, sMsg))\r
143                                 {\r
144                                         TRACE(_T("%s\n"), (LPCTSTR)svn.GetLastErrorMessage());\r
145                                         CMessageBox::Show(hwndExplorer, svn.GetLastErrorMessage(), _T("TortoiseSVN"), MB_ICONERROR);\r
146                                 }\r
147                                 else\r
148                                         bRet = true;\r
149                         }\r
150                         else\r
151                         {\r
152                                 // when refactoring, multiple files have to be renamed\r
153                                 // at once because those files belong together.\r
154                                 // e.g. file.aspx, file.aspx.cs, file.aspx.resx\r
155                                 CTSVNPathList renlist;\r
156                                 CSimpleFileFind filefind(cmdLinePath.GetDirectory().GetWinPathString(), sFilemask+_T(".*"));\r
157                                 while (filefind.FindNextFileNoDots())\r
158                                 {\r
159                                         if (!filefind.IsDirectory())\r
160                                                 renlist.AddPath(CTSVNPath(filefind.GetFilePath()));\r
161                                 }\r
162                                 if (renlist.GetCount()<=1)\r
163                                 {\r
164                                         // we couldn't find any other matching files\r
165                                         // just do the default...\r
166                                         if (!svn.Move(CTSVNPathList(cmdLinePath), destinationPath, TRUE, sMsg))\r
167                                         {\r
168                                                 TRACE(_T("%s\n"), (LPCTSTR)svn.GetLastErrorMessage());\r
169                                                 CMessageBox::Show(hwndExplorer, svn.GetLastErrorMessage(), _T("TortoiseSVN"), MB_ICONERROR);\r
170                                         }\r
171                                         else\r
172                                         {\r
173                                                 bRet = true;\r
174                                                 CShellUpdater::Instance().AddPathForUpdate(destinationPath);\r
175                                         }\r
176                                 }\r
177                                 else\r
178                                 {\r
179                                         std::map<CString, CString> renmap;\r
180                                         CString sTemp;\r
181                                         CString sRenList;\r
182                                         for (int i=0; i<renlist.GetCount(); ++i)\r
183                                         {\r
184                                                 CString sFilename = renlist[i].GetFilename();\r
185                                                 CString sNewFilename = sNewMask + sFilename.Mid(sFilemask.GetLength());\r
186                                                 sTemp.Format(_T("\n%s -> %s"), (LPCTSTR)sFilename, (LPCTSTR)sNewFilename);\r
187                                                 if (!renlist[i].IsEquivalentTo(cmdLinePath))\r
188                                                         sRenList += sTemp;\r
189                                                 renmap[renlist[i].GetWinPathString()] = renlist[i].GetContainingDirectory().GetWinPathString()+_T("\\")+sNewFilename;\r
190                                         }\r
191                                         CString sRenameMultipleQuestion;\r
192                                         sRenameMultipleQuestion.Format(IDS_PROC_MULTIRENAME, (LPCTSTR)sRenList);\r
193                                         UINT idret = CMessageBox::Show(hwndExplorer, sRenameMultipleQuestion, _T("TortoiseSVN"), MB_ICONQUESTION|MB_YESNOCANCEL);\r
194                                         if (idret == IDYES)\r
195                                         {\r
196                                                 CProgressDlg progress;\r
197                                                 progress.SetTitle(IDS_PROC_MOVING);\r
198                                                 progress.SetAnimation(IDR_MOVEANI);\r
199                                                 progress.SetTime(true);\r
200                                                 progress.ShowModeless(CWnd::FromHandle(hwndExplorer));\r
201                                                 DWORD count = 1;\r
202                                                 for (std::map<CString, CString>::iterator it=renmap.begin(); it != renmap.end(); ++it)\r
203                                                 {\r
204                                                         progress.FormatPathLine(1, IDS_PROC_MOVINGPROG, (LPCTSTR)it->first);\r
205                                                         progress.FormatPathLine(2, IDS_PROC_CPYMVPROG2, (LPCTSTR)it->second);\r
206                                                         progress.SetProgress(count, renmap.size());\r
207                                                         if (!svn.Move(CTSVNPathList(CTSVNPath(it->first)), CTSVNPath(it->second), TRUE, sMsg))\r
208                                                         {\r
209                                                                 if (svn.Err->apr_err == SVN_ERR_ENTRY_NOT_FOUND)\r
210                                                                 {\r
211                                                                         bRet = !!MoveFile(it->first, it->second);\r
212                                                                 }\r
213                                                                 else\r
214                                                                 {\r
215                                                                         TRACE(_T("%s\n"), (LPCTSTR)svn.GetLastErrorMessage());\r
216                                                                         CMessageBox::Show(hwndExplorer, svn.GetLastErrorMessage(), _T("TortoiseSVN"), MB_ICONERROR);\r
217                                                                         bRet = false;\r
218                                                                 }\r
219                                                         }\r
220                                                         else\r
221                                                         {\r
222                                                                 bRet = true;\r
223                                                                 CShellUpdater::Instance().AddPathForUpdate(CTSVNPath(it->second));\r
224                                                         }\r
225                                                 }\r
226                                                 progress.Stop();\r
227                                         }\r
228                                         else if (idret == IDNO)\r
229                                         {\r
230                                                 // no, user wants to just rename the file he selected\r
231                                                 if (!svn.Move(CTSVNPathList(cmdLinePath), destinationPath, TRUE, sMsg))\r
232                                                 {\r
233                                                         TRACE(_T("%s\n"), (LPCTSTR)svn.GetLastErrorMessage());\r
234                                                         CMessageBox::Show(hwndExplorer, svn.GetLastErrorMessage(), _T("TortoiseSVN"), MB_ICONERROR);\r
235                                                 }\r
236                                                 else\r
237                                                 {\r
238                                                         bRet = true;\r
239                                                         CShellUpdater::Instance().AddPathForUpdate(destinationPath);\r
240                                                 }\r
241                                         }\r
242                                         else if (idret == IDCANCEL)\r
243                                         {\r
244                                                 // nothing\r
245                                         }\r
246                                 }\r
247                         }\r
248                 }\r
249         }\r
250 #endif\r
251         return bRet;\r
252 }\r