~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Api/RenameBranchCommand.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using System.IO;
 
45
using NGit;
 
46
using NGit.Api;
 
47
using NGit.Api.Errors;
 
48
using Sharpen;
 
49
 
 
50
namespace NGit.Api
 
51
{
 
52
        /// <summary>Used to rename branches.</summary>
 
53
        /// <remarks>Used to rename branches.</remarks>
 
54
        /// <seealso><a
 
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>
 
58
        {
 
59
                private string oldName;
 
60
 
 
61
                private string newName;
 
62
 
 
63
                /// <param name="repo"></param>
 
64
                protected internal RenameBranchCommand(Repository repo) : base(repo)
 
65
                {
 
66
                }
 
67
 
 
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)
 
71
                /// </exception>
 
72
                /// <exception cref="NGit.Api.Errors.InvalidRefNameException">
 
73
                /// if the provided new name is <code>null</code> or otherwise
 
74
                /// invalid
 
75
                /// </exception>
 
76
                /// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">if a branch with the new name already exists
 
77
                ///     </exception>
 
78
                /// <exception cref="NGit.Api.Errors.DetachedHeadException">
 
79
                /// if rename is tried without specifying the old name and HEAD
 
80
                /// is detached
 
81
                /// </exception>
 
82
                public override Ref Call()
 
83
                {
 
84
                        CheckCallable();
 
85
                        if (newName == null)
 
86
                        {
 
87
                                throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
 
88
                                        , "<null>"));
 
89
                        }
 
90
                        try
 
91
                        {
 
92
                                string fullOldName;
 
93
                                string fullNewName;
 
94
                                if (repo.GetRef(newName) != null)
 
95
                                {
 
96
                                        throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadExists
 
97
                                                , newName));
 
98
                                }
 
99
                                if (oldName != null)
 
100
                                {
 
101
                                        Ref @ref = repo.GetRef(oldName);
 
102
                                        if (@ref == null)
 
103
                                        {
 
104
                                                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
 
105
                                                        , oldName));
 
106
                                        }
 
107
                                        if (@ref.GetName().StartsWith(Constants.R_TAGS))
 
108
                                        {
 
109
                                                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().renameBranchFailedBecauseTag
 
110
                                                        , oldName));
 
111
                                        }
 
112
                                        fullOldName = @ref.GetName();
 
113
                                }
 
114
                                else
 
115
                                {
 
116
                                        fullOldName = repo.GetFullBranch();
 
117
                                        if (ObjectId.IsId(fullOldName))
 
118
                                        {
 
119
                                                throw new DetachedHeadException();
 
120
                                        }
 
121
                                }
 
122
                                if (fullOldName.StartsWith(Constants.R_REMOTES))
 
123
                                {
 
124
                                        fullNewName = Constants.R_REMOTES + newName;
 
125
                                }
 
126
                                else
 
127
                                {
 
128
                                        fullNewName = Constants.R_HEADS + newName;
 
129
                                }
 
130
                                if (!Repository.IsValidRefName(fullNewName))
 
131
                                {
 
132
                                        throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
 
133
                                                , fullNewName));
 
134
                                }
 
135
                                RefRename rename = repo.RenameRef(fullOldName, fullNewName);
 
136
                                RefUpdate.Result renameResult = rename.Rename();
 
137
                                SetCallable(false);
 
138
                                bool ok = RefUpdate.Result.RENAMED == renameResult;
 
139
                                if (ok)
 
140
                                {
 
141
                                        if (fullNewName.StartsWith(Constants.R_HEADS))
 
142
                                        {
 
143
                                                // move the upstream configuration over to the new branch
 
144
                                                string shortOldName = Sharpen.Runtime.Substring(fullOldName, Constants.R_HEADS.Length
 
145
                                                        );
 
146
                                                StoredConfig repoConfig = repo.GetConfig();
 
147
                                                string oldRemote = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
 
148
                                                        , ConfigConstants.CONFIG_KEY_REMOTE);
 
149
                                                if (oldRemote != null)
 
150
                                                {
 
151
                                                        repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
 
152
                                                                .CONFIG_KEY_REMOTE, oldRemote);
 
153
                                                }
 
154
                                                string oldMerge = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
 
155
                                                        , ConfigConstants.CONFIG_KEY_MERGE);
 
156
                                                if (oldMerge != null)
 
157
                                                {
 
158
                                                        repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
 
159
                                                                .CONFIG_KEY_MERGE, oldMerge);
 
160
                                                }
 
161
                                                repoConfig.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName);
 
162
                                                repoConfig.Save();
 
163
                                        }
 
164
                                }
 
165
                                else
 
166
                                {
 
167
                                        throw new JGitInternalException(MessageFormat.Format(JGitText.Get().renameBranchUnexpectedResult
 
168
                                                , renameResult.ToString()));
 
169
                                }
 
170
                                Ref resultRef = repo.GetRef(newName);
 
171
                                if (resultRef == null)
 
172
                                {
 
173
                                        throw new JGitInternalException(JGitText.Get().renameBranchFailedUnknownReason);
 
174
                                }
 
175
                                return resultRef;
 
176
                        }
 
177
                        catch (IOException ioe)
 
178
                        {
 
179
                                throw new JGitInternalException(ioe.Message, ioe);
 
180
                        }
 
181
                }
 
182
 
 
183
                /// <param name="newName">the new name</param>
 
184
                /// <returns>this instance</returns>
 
185
                public virtual NGit.Api.RenameBranchCommand SetNewName(string newName)
 
186
                {
 
187
                        CheckCallable();
 
188
                        this.newName = newName;
 
189
                        return this;
 
190
                }
 
191
 
 
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
 
195
                /// </param>
 
196
                /// <returns>this instance</returns>
 
197
                public virtual NGit.Api.RenameBranchCommand SetOldName(string oldName)
 
198
                {
 
199
                        CheckCallable();
 
200
                        this.oldName = oldName;
 
201
                        return this;
 
202
                }
 
203
        }
 
204
}