0

For my django project, I need to read the current git revision number of my project that powers the site, assuming the project is the root dir of the git repository.

This way I will be keeping my css and js pages always up to date as such:

<link rel="stylesheet" type="text/css" href="/media/css/app.css?v46"> 

Where 46 is the revision number.

Hellnar
  • 62,315
  • 79
  • 204
  • 279

4 Answers4

2

Okay, sorry for not answering your question but there are a couple of points you might want to note:

  1. Git doesn't have revision numbers (okay, maybe it does. Read comments below)
  2. If you do this, your site will refresh it's cache even if you make a minor change in a single unrelated file (for example some database logic, totally irrelevant to the actual .css file served out to clients)
  3. Which makes it a bad idea.
  4. You could however use the last-modified filesystem property for the same (os.path.getmtime("path.to.file"))
aviraldg
  • 9,531
  • 6
  • 41
  • 56
  • +1, although I do need to point out that git *does* have revision numbers, however, they are not sequential. They are also inconvenient for humans, because they are large hexadecimal strings. – Arafangion Oct 09 '11 at 12:32
  • @Arafangion They aren't quite revision *numbers* are they? ;) – aviraldg Oct 09 '11 at 12:34
  • 3
    I'mma let you finish, but hexadecimal strings are numbers, they just have a different base (16) – Issac Kelly Oct 09 '11 at 15:26
  • thanks guys for backing me up, we should respect all numbers, regardless of their base! – Hellnar Oct 09 '11 at 15:44
  • Oh, the technicalities. What's the correct name, though? AFAIK it's called a revision hash. – aviraldg Oct 09 '11 at 16:24
  • 4
    I think it's reasonable to distinguish revision numbers from commit ID's; The difference is very obvious when you use a SCM that uses both, like mercurial; the revision number increases monotinically, is convenient for humans but is local to the individual repo; where the sha1 is independent of commit history and cryptographically strong; they are both useful, but almost never for the same things. I don't think either is that good for what the OP is asking. – SingleNegationElimination Oct 09 '11 at 16:40
2

You might want to look into projects which take care of this problem for you, like django-mediagenerator: http://www.allbuttonspressed.com/projects/django-mediagenerator

No need to reinvent the wheel ;)

markijbema
  • 3,985
  • 20
  • 32
1

I solved this by adding a templatetag to my django project:

in proj/templatetags, added version.py:

from django import template
import time
import os

register = template.Library()

@register.simple_tag
def version_date():
    return time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime('../.git')))

Then, in my base.html, adding:

{% load version %}
<span class='version'>Last Updated: {% version_date %}</span>

You could tweak it to use a version num if you choose based on date/time, then append to your stylesheets/js files.

JayCrossler
  • 2,079
  • 2
  • 22
  • 22
0
  1. I think, you don't want to use tip changeset ID in parameter, see note 2 of @aviral-dasgupta answer, but id of last-change-this-file changeset in order to resresh cache only when file changed
  2. Changeset ID in Git is long 40-digits hex-hash, hardly readed by human
  3. Git haven't natively support for RCS-style keyword expansion.
  4. If you still want it, read this full answer on similar question from @jakub-narebski. Pro Git reading can help understand also (with nice example of using filters for $Date$ keyword constructing)
Community
  • 1
  • 1
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110