Fix typos
[TortoiseGit.git] / src / TortoiseProc / Commands / DropMoveCommand.cpp
blobdee8d855ed987f33801d30dc4bd923b853f22573
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009, 2011-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 "DropMoveCommand.h"
23 #include "SysProgressDlg.h"
24 #include "MessageBox.h"
25 #include "Git.h"
26 #include "RenameDlg.h"
27 #include "ShellUpdater.h"
29 bool DropMoveCommand::Execute()
31 CString droppath = parser.GetVal(L"droptarget");
32 CString ProjectTop;
33 if (!CTGitPath(droppath).HasAdminDir(&ProjectTop))
34 return FALSE;
36 if (g_Git.m_CurrentDir.CompareNoCase(ProjectTop) != 0)
38 CMessageBox::Show(GetExplorerHWND(), IDS_ERR_MUSTBESAMEWT, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
39 return FALSE;
42 if (ProjectTop.GetLength() == 3 && ProjectTop.Mid(1, 2) == L":\\") // working tree root is directly on a drive
43 droppath = droppath.Right(droppath.GetLength() - ProjectTop.GetLength());
44 else
45 droppath = droppath.Right(droppath.GetLength() - ProjectTop.GetLength() - 1);
46 if (!droppath.IsEmpty())
47 droppath += L'\\';
49 unsigned long count = 0;
50 pathList.RemoveAdminPaths();
51 CString sNewName;
53 if (parser.HasKey(L"rename") && pathList.GetCount() == 1)
55 // ask for a new name of the source item
56 CRenameDlg renDlg;
57 renDlg.SetRenameRequired(pathList[0].GetContainingDirectory().IsEquivalentToWithoutCase(droppath));
58 renDlg.SetInputValidator([&](const int /*nID*/, const CString& input) -> CString
60 if (PathFileExists(g_Git.CombinePath(droppath + L'\\' + input)))
61 return CString(static_cast<LPCWSTR>(CFormatMessageWrapper(ERROR_FILE_EXISTS)));
63 return{};
64 });
65 renDlg.m_sBaseDir = g_Git.CombinePath(droppath);
66 renDlg.m_windowtitle.LoadString(IDS_PROC_MOVERENAME);
67 renDlg.m_name = pathList[0].GetFileOrDirectoryName();
68 if (renDlg.DoModal() != IDOK)
69 return FALSE;
70 sNewName = renDlg.m_name;
72 CSysProgressDlg progress;
73 if (progress.IsValid())
75 progress.SetTitle(IDS_PROC_MOVING);
76 progress.SetTime(true);
77 progress.ShowModeless(CWnd::FromHandle(GetExplorerHWND()));
79 for (int nPath = 0; nPath < pathList.GetCount(); ++nPath)
81 CTGitPath destPath;
82 if (sNewName.IsEmpty())
83 destPath = CTGitPath(droppath + pathList[nPath].GetFileOrDirectoryName());
84 else
85 destPath = CTGitPath(droppath + sNewName);
86 if (destPath.Exists())
88 progress.Stop();
90 CString name = destPath.GetFileOrDirectoryName();
91 progress.Stop();
92 CRenameDlg dlg;
93 dlg.SetInputValidator([&](const int /*nID*/, const CString& input) -> CString
95 if (PathFileExists(g_Git.CombinePath(droppath + L'\\' + input)))
96 return CString(static_cast<LPCWSTR>(CFormatMessageWrapper(ERROR_FILE_EXISTS)));
98 return{};
99 });
100 dlg.m_sBaseDir = g_Git.CombinePath(destPath);
101 dlg.m_name = name;
102 dlg.m_windowtitle.Format(IDS_PROC_NEWNAMEMOVE, static_cast<LPCWSTR>(name));
103 if (dlg.DoModal() != IDOK)
104 return FALSE;
106 // rebuild the progress dialog
107 progress.EnsureValid();
108 progress.SetTitle(IDS_PROC_MOVING);
109 progress.SetTime(true);
110 progress.SetProgress(count, pathList.GetCount());
111 progress.ShowModeless(CWnd::FromHandle(GetExplorerHWND()));
113 // Rebuild the destination path, with the new name
114 destPath.SetFromUnknown(droppath);
115 destPath.AppendPathString(dlg.m_name);
117 CString cmd,out;
119 cmd.Format(L"git.exe mv -- \"%s\" \"%s\"", static_cast<LPCWSTR>(pathList[nPath].GetGitPathString()), static_cast<LPCWSTR>(destPath.GetGitPathString()));
120 if (g_Git.Run(cmd, &out, CP_UTF8))
122 if (CMessageBox::Show(GetExplorerHWND(), out, L"TortoiseGit", 2, IDI_EXCLAMATION, CString(MAKEINTRESOURCE(IDS_IGNOREBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
124 #if 0
125 if (!svn.Move(CTSVNPathList(pathList[nPath]), destPath, TRUE))
127 CMessageBox::Show(GetExplorerHWND(), svn.GetLastErrorMessage(), L"TortoiseGit", MB_ICONERROR);
128 return FALSE; //get out of here
130 CShellUpdater::Instance().AddPathForUpdate(destPath);
131 #endif
133 else
135 CMessageBox::Show(GetExplorerHWND(), IDS_USERCANCELLED, IDS_APPNAME, MB_ICONERROR);
136 return FALSE; //get out of here
139 else
140 CShellUpdater::Instance().AddPathForUpdate(destPath);
141 ++count;
142 if (progress.IsValid())
144 progress.FormatPathLine(1, IDS_PROC_MOVINGPROG, pathList[nPath].GetWinPath());
145 progress.FormatPathLine(2, IDS_PROC_CPYMVPROG2, destPath.GetWinPath());
146 progress.SetProgress(count, pathList.GetCount());
148 if ((progress.IsValid())&&(progress.HasUserCancelled()))
150 CMessageBox::Show(GetExplorerHWND(), IDS_USERCANCELLED, IDS_APPNAME, MB_ICONINFORMATION);
151 return FALSE;
154 return true;