|
| 1 | +""" |
| 2 | +Copyright (c) 2015 Functional Software, Inc and individual contributors. |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without modification, |
| 6 | +are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | + 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + this list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 12 | + following disclaimer in the documentation and/or other materials provided with the distribution. |
| 13 | +
|
| 14 | + 3. Neither the name of the Raven nor the names of its contributors may be used to endorse or promote products |
| 15 | + derived from this software without specific prior written permission. |
| 16 | +
|
| 17 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 18 | +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 | +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +""" |
| 25 | + |
| 26 | +import os.path |
| 27 | + |
| 28 | + |
| 29 | +def fetch_git_sha(path, head=None): |
| 30 | + if not head: |
| 31 | + head_path = os.path.join(path, '.git', 'HEAD') |
| 32 | + if not os.path.exists(head_path): |
| 33 | + raise Exception( |
| 34 | + 'Cannot identify HEAD for git repository at %s' % (path,)) |
| 35 | + |
| 36 | + with open(head_path, 'r') as fp: |
| 37 | + head = str(fp.read()).strip() |
| 38 | + |
| 39 | + if head.startswith('ref: '): |
| 40 | + head = head[5:] |
| 41 | + revision_file = os.path.join( |
| 42 | + path, '.git', *head.split('/') |
| 43 | + ) |
| 44 | + else: |
| 45 | + return head |
| 46 | + else: |
| 47 | + revision_file = os.path.join(path, '.git', 'refs', 'heads', head) |
| 48 | + |
| 49 | + if not os.path.exists(revision_file): |
| 50 | + if not os.path.exists(os.path.join(path, '.git')): |
| 51 | + raise Exception( |
| 52 | + '%s does not seem to be the root of a git repository' % (path,)) |
| 53 | + |
| 54 | + # Check for our .git/packed-refs' file since a `git gc` may have run |
| 55 | + # https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery |
| 56 | + packed_file = os.path.join(path, '.git', 'packed-refs') |
| 57 | + if os.path.exists(packed_file): |
| 58 | + with open(packed_file) as fh: |
| 59 | + for line in fh: |
| 60 | + line = line.rstrip() |
| 61 | + if line and line[:1] not in ('#', '^'): |
| 62 | + try: |
| 63 | + revision, ref = line.split(' ', 1) |
| 64 | + except ValueError: |
| 65 | + continue |
| 66 | + if ref == head: |
| 67 | + return str(revision) |
| 68 | + |
| 69 | + raise Exception( |
| 70 | + 'Unable to find ref to head "%s" in repository' % (head,)) |
| 71 | + |
| 72 | + with open(revision_file) as fh: |
| 73 | + return str(fh.read()).strip() |
0 commit comments