File: githead.md

package info (click to toggle)
bnd 5.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,128 kB
  • sloc: java: 249,039; xml: 90,728; sh: 655; perl: 153; makefile: 96; python: 47; javascript: 9
file content (57 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (2)
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
---
layout: default
class: Builder
title: githead
summary: Get the head commit number. Look for a .git/HEAD file, going up in the file hierarchy. Then get this file, and resolve any symbolic reference.
---


	/**
	 * #388 Manifest header to get GIT head Get the head commit number. Look
	 * for a .git/HEAD file, going up in the file hierarchy. Then get this file,
	 * and resolve any symbolic reference.
	 *
	 * @throws IOException
	 */
	static Pattern	GITREF	= Pattern.compile("ref:\\s*(refs/(heads|tags|remotes)/([^\\s]+))\\s*");

	static String	_githeadHelp	= "${githead}, provide the SHA for the current git head";

	public String _githead(String[] args) throws IOException {
		Macro.verifyCommand(args, _githeadHelp, null, 1, 1);

		//
		// Locate the .git directory
		//

		File rover = getBase();
		while (rover !=null && rover.isDirectory()) {
			File headFile = IO.getFile(rover, ".git/HEAD");
			if (headFile.isFile()) {
				//
				// The head is either a symref (ref: refs/(heads|tags|remotes)/<name>)
				//
				String head = IO.collect(headFile).trim();
				if (!Hex.isHex(head)) {
					//
					// Should be a symref
					//
					Matcher m = GITREF.matcher(head);
					if (m.matches()) {

						// so the commit is in the following path

						head = IO.collect(IO.getFile(rover, ".git/" + m.group(1)));
					}
					else {
						error("Git repo seems corrupt. It exists, find the HEAD but the content is neither hex nor a sym-ref: %s",
								head);
					}
				}
				return head.trim().toUpperCase();
			}
			rover = rover.getParentFile();
		}
		// Cannot find git directory
		return "";
	}