Do not use GitAdminDir objects
[TortoiseGit.git] / src / TortoiseProc / Commands / SubmoduleCommand.cpp
blobd1e7f8bff913ea0371f3d1d8ededb7e5f4dacf2d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2009,2012-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "SubmoduleCommand.h"
22 #include "MessageBox.h"
23 #include "RenameDlg.h"
24 #include "InputLogDlg.h"
25 #include "Git.h"
26 #include "DirFileEnum.h"
27 #include "ShellUpdater.h"
28 #include "SubmoduleAddDlg.h"
29 #include "SubmoduleUpdateDlg.h"
30 #include "ProgressDlg.h"
31 #include "AppUtils.h"
33 bool SubmoduleAddCommand::Execute()
35 bool bRet = false;
36 CSubmoduleAddDlg dlg;
37 dlg.m_strPath = cmdLinePath.GetDirectory().GetWinPathString();
38 dlg.m_strProject = g_Git.m_CurrentDir;
39 if( dlg.DoModal() == IDOK )
41 if (dlg.m_bAutoloadPuttyKeyFile)
42 CAppUtils::LaunchPAgent(&dlg.m_strPuttyKeyFile);
44 CString cmd;
45 if(dlg.m_strPath.Left(g_Git.m_CurrentDir.GetLength()) == g_Git.m_CurrentDir)
46 dlg.m_strPath = dlg.m_strPath.Right(dlg.m_strPath.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
48 CString branch;
49 if(dlg.m_bBranch)
50 branch.Format(_T(" -b %s "), dlg.m_strBranch);
52 CString force;
53 if (dlg.m_bForce)
54 force = _T("--force");
56 dlg.m_strPath.Replace(_T('\\'),_T('/'));
57 dlg.m_strRepos.Replace(_T('\\'),_T('/'));
59 cmd.Format(_T("git.exe submodule add %s %s -- \"%s\" \"%s\""),
60 branch, force,
61 dlg.m_strRepos, dlg.m_strPath);
63 CProgressDlg progress;
64 progress.m_GitCmd=cmd;
65 progress.DoModal();
67 if (progress.m_GitStatus == 0)
69 if (dlg.m_bAutoloadPuttyKeyFile)
71 SetCurrentDirectory(g_Git.m_CurrentDir);
72 CGit subgit;
73 dlg.m_strPath.Replace(_T('/'), _T('\\'));
74 subgit.m_CurrentDir = PathIsRelative(dlg.m_strPath) ? g_Git.CombinePath(dlg.m_strPath) : dlg.m_strPath;
76 if (subgit.SetConfigValue(_T("remote.origin.puttykeyfile"), dlg.m_strPuttyKeyFile, CONFIG_LOCAL))
78 CMessageBox::Show(NULL, _T("Fail set config remote.origin.puttykeyfile"), _T("TortoiseGit"), MB_OK| MB_ICONERROR);
79 return FALSE;
84 bRet = TRUE;
86 return bRet;
89 bool SubmoduleUpdateCommand::Execute()
91 CString bkpath;
92 if (parser.HasKey(_T("bkpath")))
93 bkpath = parser.GetVal(_T("bkpath"));
94 else
96 bkpath = this->orgPathList[0].GetWinPathString();
97 int start = bkpath.ReverseFind(_T('\\'));
98 if (start >= 0)
99 bkpath = bkpath.Left(start);
102 CString super = GitAdminDir::GetSuperProjectRoot(bkpath);
103 if (super.IsEmpty())
105 CMessageBox::Show(NULL,IDS_ERR_NOTFOUND_SUPER_PRJECT,IDS_APPNAME,MB_OK|MB_ICONERROR);
106 //change current project root to super project
107 return false;
110 STRING_VECTOR pathFilterList;
111 for (int i = 0; i < orgPathList.GetCount(); i++)
113 if (orgPathList[i].IsDirectory())
115 CString path = ((CTGitPath &)orgPathList[i]).GetSubPath(CTGitPath(super)).GetGitPathString();
116 if (!path.IsEmpty())
117 pathFilterList.push_back(path);
121 CSubmoduleUpdateDlg submoduleUpdateDlg;
122 submoduleUpdateDlg.m_PathFilterList = pathFilterList;
123 if (parser.HasKey(_T("selectedpath")))
125 CString selectedPath = parser.GetVal(_T("selectedpath"));
126 selectedPath.Replace(_T('\\'), _T('/'));
127 submoduleUpdateDlg.m_PathList.push_back(selectedPath);
129 if (submoduleUpdateDlg.DoModal() != IDOK)
130 return false;
132 CProgressDlg progress;
134 g_Git.m_CurrentDir = super;
136 CString params;
137 if (submoduleUpdateDlg.m_bInit)
138 params = _T(" --init");
139 if (submoduleUpdateDlg.m_bRecursive)
140 params += _T(" --recursive");
141 if (submoduleUpdateDlg.m_bForce)
142 params += _T(" --force");
143 if (submoduleUpdateDlg.m_bNoFetch)
144 params += _T(" --no-fetch");
145 if (submoduleUpdateDlg.m_bMerge)
146 params += _T(" --merge");
147 if (submoduleUpdateDlg.m_bRebase)
148 params += _T(" --rebase");
149 if (submoduleUpdateDlg.m_bRemote)
150 params += _T(" --remote");
152 for (size_t i = 0; i < submoduleUpdateDlg.m_PathList.size(); ++i)
154 CString str;
155 str.Format(_T("git.exe submodule update%s -- \"%s\""), params, submoduleUpdateDlg.m_PathList[i]);
156 progress.m_GitCmdList.push_back(str);
159 progress.DoModal();
161 return !progress.m_GitStatus;
164 bool SubmoduleCommand::Execute(CString cmd, CString arg)
166 CProgressDlg progress;
167 CString bkpath;
169 if(parser.HasKey(_T("bkpath")))
171 bkpath=parser.GetVal(_T("bkpath"));
173 else
175 bkpath=this->orgPathList[0].GetWinPathString();
176 int start = bkpath.ReverseFind(_T('\\'));
177 if( start >= 0 )
178 bkpath=bkpath.Left(start);
181 CString super = GitAdminDir::GetSuperProjectRoot(bkpath);
182 if(super.IsEmpty())
184 CMessageBox::Show(NULL,IDS_ERR_NOTFOUND_SUPER_PRJECT,IDS_APPNAME,MB_OK|MB_ICONERROR);
185 //change current project root to super project
186 return false;
189 g_Git.m_CurrentDir=super;
191 //progress.m_GitCmd.Format(_T("git.exe submodule update --init "));
193 CString str;
194 for (int i = 0; i < this->orgPathList.GetCount(); ++i)
196 if(orgPathList[i].IsDirectory())
198 str.Format(_T("git.exe submodule %s %s -- \"%s\""),cmd,arg, ((CTGitPath &)orgPathList[i]).GetSubPath(CTGitPath(super)).GetGitPathString());
199 progress.m_GitCmdList.push_back(str);
203 progress.DoModal();
205 return !progress.m_GitStatus;