1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.ant.tasks;
11
12 import java.io.File;
13 import java.io.IOException;
14
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Task;
17 import org.eclipse.jgit.api.CheckoutCommand;
18 import org.eclipse.jgit.api.Git;
19 import org.eclipse.jgit.lib.Repository;
20 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
21
22
23
24
25
26
27
28
29 public class GitCheckoutTask extends Task {
30
31 private File src;
32 private String branch;
33 private boolean createBranch;
34 private boolean force;
35
36
37
38
39
40
41
42 public void setSrc(File src) {
43 this.src = src;
44 }
45
46
47
48
49
50
51
52 public void setBranch(String branch) {
53 this.branch = branch;
54 }
55
56
57
58
59
60
61
62
63 public void setCreateBranch(boolean createBranch) {
64 this.createBranch = createBranch;
65 }
66
67
68
69
70
71
72
73
74
75
76 public void setForce(boolean force) {
77 this.force = force;
78 }
79
80
81 @Override
82 public void execute() throws BuildException {
83 CheckoutCommand checkout;
84 try (Repository repo = new FileRepositoryBuilder().readEnvironment()
85 .findGitDir(src).build();
86 Git git = new Git(repo)) {
87 checkout = git.checkout();
88 } catch (IOException e) {
89 throw new BuildException("Could not access repository " + src, e);
90 }
91
92 try {
93 checkout.setCreateBranch(createBranch).setForceRefUpdate(force)
94 .setName(branch);
95 checkout.call();
96 } catch (Exception e) {
97 throw new BuildException("Could not checkout repository " + src, e);
98 }
99 }
100
101 }