2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
using NGit.Api.Errors;
52
/// <summary>Used to rename branches.</summary>
53
/// <remarks>Used to rename branches.</remarks>
55
/// * href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
56
/// * >Git documentation about Branch</a></seealso>
57
public class RenameBranchCommand : GitCommand<Ref>
59
private string oldName;
61
private string newName;
63
/// <param name="repo"></param>
64
protected internal RenameBranchCommand(Repository repo) : base(repo)
68
/// <exception cref="NGit.Api.Errors.RefNotFoundException">
69
/// if the old branch can not be found (branch with provided old
70
/// name does not exist or old name resolves to a tag)
72
/// <exception cref="NGit.Api.Errors.InvalidRefNameException">
73
/// if the provided new name is <code>null</code> or otherwise
76
/// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">if a branch with the new name already exists
78
/// <exception cref="NGit.Api.Errors.DetachedHeadException">
79
/// if rename is tried without specifying the old name and HEAD
82
public override Ref Call()
87
throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
94
if (repo.GetRef(newName) != null)
96
throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadExists
101
Ref @ref = repo.GetRef(oldName);
104
throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
107
if (@ref.GetName().StartsWith(Constants.R_TAGS))
109
throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().renameBranchFailedBecauseTag
112
fullOldName = @ref.GetName();
116
fullOldName = repo.GetFullBranch();
117
if (ObjectId.IsId(fullOldName))
119
throw new DetachedHeadException();
122
if (fullOldName.StartsWith(Constants.R_REMOTES))
124
fullNewName = Constants.R_REMOTES + newName;
128
fullNewName = Constants.R_HEADS + newName;
130
if (!Repository.IsValidRefName(fullNewName))
132
throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
135
RefRename rename = repo.RenameRef(fullOldName, fullNewName);
136
RefUpdate.Result renameResult = rename.Rename();
138
bool ok = RefUpdate.Result.RENAMED == renameResult;
141
if (fullNewName.StartsWith(Constants.R_HEADS))
143
// move the upstream configuration over to the new branch
144
string shortOldName = Sharpen.Runtime.Substring(fullOldName, Constants.R_HEADS.Length
146
StoredConfig repoConfig = repo.GetConfig();
147
string oldRemote = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
148
, ConfigConstants.CONFIG_KEY_REMOTE);
149
if (oldRemote != null)
151
repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
152
.CONFIG_KEY_REMOTE, oldRemote);
154
string oldMerge = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
155
, ConfigConstants.CONFIG_KEY_MERGE);
156
if (oldMerge != null)
158
repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
159
.CONFIG_KEY_MERGE, oldMerge);
161
repoConfig.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName);
167
throw new JGitInternalException(MessageFormat.Format(JGitText.Get().renameBranchUnexpectedResult
168
, renameResult.ToString()));
170
Ref resultRef = repo.GetRef(newName);
171
if (resultRef == null)
173
throw new JGitInternalException(JGitText.Get().renameBranchFailedUnknownReason);
177
catch (IOException ioe)
179
throw new JGitInternalException(ioe.Message, ioe);
183
/// <param name="newName">the new name</param>
184
/// <returns>this instance</returns>
185
public virtual NGit.Api.RenameBranchCommand SetNewName(string newName)
188
this.newName = newName;
192
/// <param name="oldName">
193
/// the name of the branch to rename; if not set, the currently
194
/// checked out branch (if any) will be renamed
196
/// <returns>this instance</returns>
197
public virtual NGit.Api.RenameBranchCommand SetOldName(string oldName)
200
this.oldName = oldName;