|
| 1 | +/* Copyright 2012 the original author or authors. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.ajoberstar.gradle.git.tasks; |
| 16 | + |
| 17 | +import org.eclipse.jgit.api.CreateBranchCommand; |
| 18 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 19 | +import org.eclipse.jgit.api.errors.InvalidRefNameException; |
| 20 | +import org.eclipse.jgit.api.errors.RefAlreadyExistsException; |
| 21 | +import org.eclipse.jgit.api.errors.RefNotFoundException; |
| 22 | +import org.gradle.api.GradleException; |
| 23 | +import org.gradle.api.tasks.TaskAction; |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates a new branch in a Git repository. |
| 27 | + * @since 0.3.0 |
| 28 | + */ |
| 29 | +public class GitBranch extends GitBase { |
| 30 | + /** |
| 31 | + * Tracking mode for branches. |
| 32 | + */ |
| 33 | + public static enum Mode { |
| 34 | + NO_TRACK, |
| 35 | + TRACK, |
| 36 | + SET_UPSTREAM; |
| 37 | + } |
| 38 | + |
| 39 | + private String branchName; |
| 40 | + private String startPoint = "master"; |
| 41 | + private Mode mode; |
| 42 | + private boolean force = false; |
| 43 | + |
| 44 | + /** |
| 45 | + * Execute the creation or update of the branch. |
| 46 | + */ |
| 47 | + @TaskAction |
| 48 | + void branchCreate() { |
| 49 | + CreateBranchCommand cmd = getGit().branchCreate(); |
| 50 | + cmd.setName(getBranchName()); |
| 51 | + cmd.setStartPoint(getStartPoint()); |
| 52 | + cmd.setForce(getForce()); |
| 53 | + |
| 54 | + if (getMode() != null) { |
| 55 | + switch(getMode()) { |
| 56 | + case NO_TRACK: |
| 57 | + cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK); |
| 58 | + break; |
| 59 | + case TRACK: |
| 60 | + cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); |
| 61 | + break; |
| 62 | + case SET_UPSTREAM: |
| 63 | + cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM); |
| 64 | + break; |
| 65 | + default: |
| 66 | + throw new AssertionError("Illegal mode: " + getMode()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + cmd.call(); |
| 72 | + } catch (InvalidRefNameException e) { |
| 73 | + throw new GradleException("Invalid branch name: " + getName(), e); |
| 74 | + } catch (RefNotFoundException e) { |
| 75 | + throw new GradleException("Can't find start point: " + getStartPoint(), e); |
| 76 | + } catch (RefAlreadyExistsException e) { |
| 77 | + throw new GradleException("Branch " + getName() + " already exists. Use force=true to modify.", e); |
| 78 | + } catch (GitAPIException e) { |
| 79 | + throw new GradleException("Problem creating or updating branch " + getName(), e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets the name of the branch to create or update. |
| 85 | + * @return branchName of the branch |
| 86 | + */ |
| 87 | + public String getBranchName() { |
| 88 | + return branchName; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the name of the branch to create or update. |
| 93 | + * @param branchName the name of the branch |
| 94 | + */ |
| 95 | + public void setBranchName(String branchName) { |
| 96 | + this.branchName = branchName; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets the starting point of the branch. |
| 101 | + * @return the starting point of the branch |
| 102 | + */ |
| 103 | + public String getStartPoint() { |
| 104 | + return startPoint; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the starting point of the branch. |
| 109 | + * @param startPoint the start point of the branch |
| 110 | + */ |
| 111 | + public void setStartPoint(String startPoint) { |
| 112 | + this.startPoint = startPoint; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets the tracking mode of the branch. |
| 117 | + * @return the tracking mode |
| 118 | + */ |
| 119 | + public Mode getMode() { |
| 120 | + return mode; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the tracking mode of the branch. |
| 125 | + * @param mode the tracking mode |
| 126 | + */ |
| 127 | + public void setMode(Mode mode) { |
| 128 | + this.mode = mode; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets whether an existing branch will be modified |
| 133 | + * by this task. |
| 134 | + * @return whether to force changes to existing |
| 135 | + * branches |
| 136 | + */ |
| 137 | + public boolean getForce() { |
| 138 | + return force; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets whether an existing branch will be modified |
| 143 | + * by this task. |
| 144 | + * @param force {@code true} if existing branches |
| 145 | + * will be updated, {@code false} if the task should |
| 146 | + * fail if the branch exists |
| 147 | + */ |
| 148 | + public void setForce(boolean force) { |
| 149 | + this.force = force; |
| 150 | + } |
| 151 | +} |
0 commit comments