import subprocess
from django.template import Library

register = Library()

try:
    head = subprocess.Popen("git rev-parse --short HEAD",
        shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    VERSION = head.stdout.readline().strip()
except:
    VERSION = u'unknown'


@register.simple_tag()
def git_version():
    return VERSION


import subprocess
from django.template import Library

register = Library()

GIT_VERSION = None


@register.simple_tag()
def git_version():
    global GIT_VERSION

    if GIT_VERSION:
        return GIT_VERSION

    try:
        head = subprocess.Popen("git rev-parse --short HEAD",
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)

        GIT_VERSION = head.stdout.readline().strip()
    except:
        GIT_VERSION = 'unknown'

    return GIT_VERSION

Make GIT_VERSION on footer more robust
[REFACTOR] Make GIT_VERSION on footer more robust

  import subprocess
  from django.template import Library
  
  register = Library()
  
+ GIT_VERSION = None
- try:
-     head = subprocess.Popen("git rev-parse --short HEAD",
-         shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-     VERSION = head.stdout.readline().strip()
- except:
-     VERSION = u'unknown'
  
  
  @register.simple_tag()
  def git_version():
-     return VERSION
+     global GIT_VERSION
  
+     if GIT_VERSION:
+         return GIT_VERSION
  
+     try:
+         head = subprocess.Popen("git rev-parse --short HEAD",
+                                 shell=True,
+                                 stdout=subprocess.PIPE,
+                                 stderr=subprocess.STDOUT)
+ 
+         GIT_VERSION = head.stdout.readline().strip()
+     except:
+         GIT_VERSION = 'unknown'
+ 
+     return GIT_VERSION
+ 
Make GIT_VERSION on footer more robust
## Code Before:
import subprocess
from django.template import Library

register = Library()

try:
    head = subprocess.Popen("git rev-parse --short HEAD",
        shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    VERSION = head.stdout.readline().strip()
except:
    VERSION = u'unknown'


@register.simple_tag()
def git_version():
    return VERSION


## Instruction:
Make GIT_VERSION on footer more robust
## Code After:
import subprocess
from django.template import Library

register = Library()

GIT_VERSION = None


@register.simple_tag()
def git_version():
    global GIT_VERSION

    if GIT_VERSION:
        return GIT_VERSION

    try:
        head = subprocess.Popen("git rev-parse --short HEAD",
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)

        GIT_VERSION = head.stdout.readline().strip()
    except:
        GIT_VERSION = 'unknown'

    return GIT_VERSION
