Skip to content

Commit afc2791

Browse files
author
Andrew J. Oberstar
committed
Converted GitBranch to Java.
1 parent dae7da2 commit afc2791

File tree

5 files changed

+169
-64
lines changed

5 files changed

+169
-64
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ bin
1010
*.ipr
1111
.idea
1212
out
13-
13+
*.sublime-*

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ buildscript {
33
mavenCentral()
44
maven { url 'https://oss.sonatype.org/content/groups/public/' }
55
}
6-
dependencies { classpath 'org.ajoberstar:gradle-git:0.2.2' }
6+
dependencies { classpath 'org.ajoberstar:gradle-git:0.2.3' }
77
}
88

99
apply plugin: 'groovy'
@@ -15,7 +15,7 @@ apply plugin: 'github-pages'
1515

1616
group = 'org.ajoberstar'
1717
description = 'Git plugins for Gradle'
18-
version = '0.2.3'
18+
version = '0.3.0-SNAPSHOT'
1919

2020
repositories {
2121
mavenCentral()
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}

src/main/groovy/org/ajoberstar/gradle/git/tasks/GitBranchCreate.groovy

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/test/groovy/org/ajoberstar/gradle/git/tasks/GitBranchCreateTest.groovy renamed to src/test/groovy/org/ajoberstar/gradle/git/tasks/GitBranchTest.groovy

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import org.gradle.api.Project
88
import org.gradle.testfixtures.ProjectBuilder
99
import spock.lang.Specification
1010

11-
import static org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode.NOTRACK
12-
13-
public class GitBranchCreateTest extends Specification {
11+
public class GitBranchTest extends Specification {
1412
def testDir = new File("build/tmp/test/gradle-git")
1513
Project project
1614
Git git
@@ -33,7 +31,7 @@ public class GitBranchCreateTest extends Specification {
3331

3432
def 'should wrap git errors with GradleException'() {
3533
given:
36-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate)
34+
project.tasks.add(name: 'branchCreate', type: GitBranch)
3735
when:
3836
project.branchCreate.execute()
3937
then:
@@ -42,19 +40,22 @@ public class GitBranchCreateTest extends Specification {
4240

4341
def 'should create branch with passed name'() {
4442
given:
45-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
43+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
4644
branchName = 'branch1'
4745
}
4846
when:
4947
project.branchCreate.execute()
50-
then:
48+
then: 'name is correct'
5149
git.branchList().call().find { it.name =~ 'branch1' }
50+
and: 'branch is not a tracking branch'
51+
fileRepository.config.getNames("branch", "branch1").size() == 0
5252
}
5353

5454
def 'when create tracking branch `master` should be used by default'() {
5555
given:
56-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
56+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
5757
branchName = 'branch1'
58+
mode = GitBranch.Mode.TRACK
5859
}
5960
when:
6061
project.branchCreate.execute()
@@ -67,9 +68,10 @@ public class GitBranchCreateTest extends Specification {
6768
def 'when start point given then create tracking branch from it'() {
6869
given:
6970
git.branchCreate().setName("startBranch").call()
70-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
71+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
7172
branchName = 'branch1'
7273
startPoint = 'startBranch'
74+
mode = GitBranch.Mode.TRACK
7375
}
7476
when:
7577
project.branchCreate.execute()
@@ -80,9 +82,9 @@ public class GitBranchCreateTest extends Specification {
8082

8183
def 'when configured then create non tracking branch'() {
8284
given:
83-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
85+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
8486
branchName = 'branch1'
85-
upstreamMode = NOTRACK
87+
mode = GitBranch.Mode.NO_TRACK
8688
}
8789
when:
8890
project.branchCreate.execute()
@@ -93,7 +95,7 @@ public class GitBranchCreateTest extends Specification {
9395
def 'when create branch tha already exist w/o `force` then exception'() {
9496
given:
9597
git.branchCreate().setName("branch1").call()
96-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
98+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
9799
branchName = 'branch1'
98100
}
99101
when:
@@ -105,7 +107,7 @@ public class GitBranchCreateTest extends Specification {
105107
def 'when create branch wish existing name w/o `force` then exception'() {
106108
given:
107109
git.branchCreate().setName("branch1").call()
108-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
110+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
109111
branchName = 'branch1'
110112
}
111113
when:
@@ -117,7 +119,7 @@ public class GitBranchCreateTest extends Specification {
117119
def 'when create branch wish existing name w/ `force` then OK'() {
118120
given:
119121
git.branchCreate().setName("branch1").call()
120-
project.tasks.add(name: 'branchCreate', type: GitBranchCreate) {
122+
project.tasks.add(name: 'branchCreate', type: GitBranch) {
121123
branchName = 'branch1'
122124
force = true
123125
}

0 commit comments

Comments
 (0)