Ticket #18228: get_git_version.diff

File get_git_version.diff, 2.0 KB (added by Daniel Swarbrick, 13 years ago)
  • django/__init__.py

    diff --git a/django/__init__.py b/django/__init__.py
    index 20ca234..21469e4 100644
    a b def get_version(version=None):  
    1818    sub = ''
    1919    if version[3] == 'alpha' and version[4] == 0:
    2020        # At the toplevel, this would cause an import loop.
    21         from django.utils.version import get_svn_revision
    22         svn_revision = get_svn_revision()[4:]
    23         if svn_revision != 'unknown':
    24             sub = '.dev%s' % svn_revision
     21        from django.utils.version import get_git_revision
     22        git_revision = get_git_revision()[:10]
     23        if git_revision != 'unknown':
     24            sub = '.dev%s' % git_revision
    2525
    2626    elif version[3] != 'final':
    2727        mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
  • django/utils/version.py

    diff --git a/django/utils/version.py b/django/utils/version.py
    index cb8623b..ad6fbcb 100644
    a b  
    11import django
     2import os.path
    23import re
    34
    45def get_svn_revision(path=None):
    def get_svn_revision(path=None):  
    3940    if rev:
    4041        return u'SVN-%s' % rev
    4142    return u'SVN-unknown'
     43
     44
     45def get_git_revision(path=None):
     46    """
     47    Returns the full 40-char hexadecimal Git revision, mimicing the behaviour
     48    of 'git rev-parse HEAD'. Returns 'unknown' if anything goes wrong.
     49
     50    If path is provided, it should be a top-level directory of a Git clone. If
     51    it's not provided, this will use the parent directory of the django package
     52    directory.
     53    """
     54    rev = 'unknown'
     55    if path is None:
     56        path = os.path.dirname(os.path.realpath(django.__path__[0]))
     57    git_path = os.path.join(path, '.git')
     58
     59    try:
     60        head_ref = open(os.path.join(git_path, 'HEAD'), 'r').readline().strip()
     61        ref_path = os.path.join(git_path, head_ref.split(' ')[1])
     62        head = open(ref_path, 'r').readline()
     63    except IOError:
     64        pass
     65    else:
     66        rev_match = re.match(r'([0-9a-f]{40})', head)
     67        if rev_match:
     68            rev = rev_match.group(1)
     69
     70    return rev
Back to Top