Fix typos
[TortoiseGit.git] / src / TortoiseProc / Commands / RenameCommand.cpp
blob903589e2319508bad8f47b143044393bcbbe92b6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012, 2015-2019, 2021 - 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 "RenameCommand.h"
23 #include "MessageBox.h"
24 #include "RenameDlg.h"
25 #include "Git.h"
26 #include "ShellUpdater.h"
28 bool RenameCommand::Execute()
30 if (!GitAdminDir::HasAdminDir(g_Git.m_CurrentDir))
32 CMessageBox::Show(GetExplorerHWND(), IDS_NOWORKINGCOPY, IDS_APPNAME, MB_ICONERROR);
33 return false;
36 bool bRet = true;
37 CString filename = cmdLinePath.GetFileOrDirectoryName();
38 CString basePath = cmdLinePath.GetContainingDirectory().GetGitPathString();
40 // show the rename dialog until the user either cancels or enters a new
41 // name (one that's different to the original name
42 CString sNewName;
43 CRenameDlg dlg;
44 dlg.SetInputValidator([&](const int /*nID*/, const CString& input) -> CString
46 CString newName;
47 if (!basePath.IsEmpty())
48 newName = basePath + "/" + input;
49 else
50 newName = input;
52 if (newName.CompareNoCase(cmdLinePath.GetGitPathString()) != 0 && PathFileExists(g_Git.CombinePath(newName)))
53 return CString(static_cast<LPCWSTR>(CFormatMessageWrapper(ERROR_FILE_EXISTS)));
55 return{};
56 });
57 dlg.m_sBaseDir = g_Git.CombinePath(basePath);
58 dlg.m_name = filename;
59 if (dlg.DoModal() != IDOK)
60 return FALSE;
61 if (!basePath.IsEmpty())
62 sNewName = basePath + "/" + dlg.m_name;
63 else
64 sNewName = dlg.m_name;
66 CString force;
67 // if the filenames only differ in case, we have to pass "-f"
68 if (sNewName.CompareNoCase(cmdLinePath.GetGitPathString()) == 0)
69 force = L"-f ";
71 CString cmd;
72 CString output;
73 cmd.Format(L"git.exe mv %s-- \"%s\" \"%s\"",
74 static_cast<LPCWSTR>(force),
75 static_cast<LPCWSTR>(cmdLinePath.GetGitPathString()),
76 static_cast<LPCWSTR>(sNewName));
78 if (g_Git.Run(cmd, &output, CP_UTF8))
80 CMessageBox::Show(GetExplorerHWND(), output, L"TortoiseGit", MB_OK);
81 bRet = false;
84 CTGitPath newpath;
85 newpath.SetFromGit(sNewName);
87 CShellUpdater::Instance().AddPathForUpdate(newpath);
88 CShellUpdater::Instance().Flush();
89 return bRet;