summaryrefslogtreecommitdiff
path: root/libbuild/GitRevTask/GitRevTask.java
blob: 5df9021e999f9d96129f07fefc9b5316c01a9517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.Project;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;


public class GitRevTask extends org.apache.tools.ant.Task {
	
	private String repoPath;
	private String branchprop;
	private String revprop;
	private String dateprop;
	
	public void setRepoPath(final String repoPath) {
		this.repoPath = repoPath;
	}
	
	public void setBranchprop(final String branchprop) {
		this.branchprop = branchprop;
	}

    public void setRevprop(final String revprop) {
        this.revprop = revprop;
    }

    public void setDateprop(final String dateprop) {
        this.dateprop = dateprop;
    }
	
	public void execute() {
		if (this.revprop==null || this.revprop.isEmpty()) {
            log("git entries file name revprop was not set properly",Project.MSG_ERR);
            return;
        }
		if (this.dateprop==null || this.dateprop.isEmpty()) {
            log("git entries file name dateprop was not set properly",Project.MSG_ERR);
            return;
        }

		String branch = null;
        String revision = null;
		String lastTag = null;
		String commitDate = null;
		
        Repository repo = null;
        Git git = null;
        RevWalk walk = null;
		try {
			final File src = new File(repoPath);
			repo = new FileRepositoryBuilder().readEnvironment()
					.findGitDir(src).build();
			branch = repo.getBranch();
			branch = "master".equals(branch)? "" : "_" + branch;
			final ObjectId head = repo.resolve("HEAD");
			
			git = new Git(repo);
			final List<Ref> tags = git.tagList().call();
			
			walk = new RevWalk(repo);
			final RevCommit headCommit = walk.parseCommit(head);
			final SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			commitDate = df.format(headCommit.getAuthorIdent().getWhen());
			walk.markStart(headCommit);
			int distance = 0;
			
			/* Peel known tags */
			final List<Ref> peeledTags = new ArrayList<>();
			for(final Ref tag : tags) {
				peeledTags.add(repo.peel(tag));
			}
			
			/* Look for the last tag commit and calculate distance with the HEAD commit */
			for (final RevCommit commit : walk) {
				for (final Ref tag : peeledTags) {
					if (commit.equals(tag.getPeeledObjectId()) || commit.equals(tag.getObjectId())) {
						lastTag = commit.getShortMessage();
						break;
					}
				}
                if (lastTag != null || distance++ > 90999) {
                    break;
                }
			}
			walk.dispose();
			if (lastTag == null) {
				revision = "0000";
			} else {
				revision = Integer.toString(distance + 9000);
			}
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GitAPIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			/* In all cases, properly release resources */
			if(walk != null) {
				walk.close();
			}
			if(git != null) {
				git.close();
			}
			if(repo != null) {
				repo.close();
			}
		}
        
        Project theProject = getProject();
        if (theProject != null) {
        	theProject.setProperty(this.branchprop, branch);
            log("Property '" + this.branchprop + "' set to '" + branch + "'", Project.MSG_VERBOSE);
        	theProject.setProperty(this.revprop, revision);
            log("Property '" + this.revprop + "' set to '" + revision + "'", Project.MSG_VERBOSE);
            theProject.setProperty(this.dateprop, commitDate);
            log("Property '" + this.dateprop + "' set to '" + commitDate + "'", Project.MSG_VERBOSE);
        }
	}

        /** use: GitRevTask.jar pathtoGitRepro  outputfile
         * optional parameter
         * 1st parameter = path to Git repository (default ..)
         * 2nd parameter = ouputfile (default gitbuild.properties)
         * */
    public static void main(String[] args) {
        GitRevTask gitRevTask = new GitRevTask();
        if (args.length == 0) {
            gitRevTask.setRepoPath(".."); // path to root of git repository
        } else {
            gitRevTask.setRepoPath(args[0]);
        }
        gitRevTask.setBranchprop("branch");
        gitRevTask.setRevprop("baseRevisionNr");
        gitRevTask.setDateprop("DSTAMP");

        Project p = new Project();
        gitRevTask.setProject(p);
        gitRevTask.execute();
        String branch = gitRevTask.getProject().getProperty("branch");
        String version = gitRevTask.getProject().getProperty("baseRevisionNr");
        String commitDate = gitRevTask.getProject().getProperty("DSTAMP");

        File f;
        if (args.length > 1) {
            f = new File (args[1]);
        } else {
            f = new File("gitbuildnumber.properties");
        }
        try {
            f.createNewFile();
            FileWriter w = new FileWriter(f);
            w.append("branch=" + branch + "\n");
            w.append("releaseNr=" + version + "\n");
            w.append("DSTAMP=" + commitDate + "\n");
            w.close();

        } catch (final IOException ex) {}
    }
}