Given a filename of a .git/HEAD file return ref path. In particular, if git is in detached head state, this will return None. If git is in attached head, it will return the branch reference. E.g. if on 'master', the HEAD will contain 'ref: refs/heads/master' so 'refs/heads/master' will be
(filename)
| 35 | |
| 36 | |
| 37 | def parse_branch_ref(filename): |
| 38 | """Given a filename of a .git/HEAD file return ref path. |
| 39 | |
| 40 | In particular, if git is in detached head state, this will |
| 41 | return None. If git is in attached head, it will return |
| 42 | the branch reference. E.g. if on 'master', the HEAD will |
| 43 | contain 'ref: refs/heads/master' so 'refs/heads/master' |
| 44 | will be returned. |
| 45 | |
| 46 | Example: parse_branch_ref(".git/HEAD") |
| 47 | Args: |
| 48 | filename: file to treat as a git HEAD file |
| 49 | Returns: |
| 50 | None if detached head, otherwise ref subpath |
| 51 | Raises: |
| 52 | RuntimeError: if the HEAD file is unparseable. |
| 53 | """ |
| 54 | |
| 55 | data = open(filename).read().strip() |
| 56 | items = data.split(" ") |
| 57 | if len(items) == 1: |
| 58 | return None |
| 59 | elif len(items) == 2 and items[0] == "ref:": |
| 60 | return items[1].strip() |
| 61 | else: |
| 62 | raise RuntimeError("Git directory has unparseable HEAD") |
| 63 | |
| 64 | |
| 65 | def configure(src_base_path, gen_path, debug=False): |