Repository: ericsson/codechecker
Issue: Build from tarball fails in extend_version_file

**Describe the bug**
Trying to build from a tarball of a tagged release, rather than from a git clone, fails when it tries to use git commands inside `scripts/build/extend_version_file.py` to populate `build/CodeChecker/config/version.json`.

**CodeChecker version**
v6.9.1

**To Reproduce**
Steps to reproduce the behavior
1. Download `https://github.com/Ericsson/codechecker/tarball/$(VERSION_TAG)`
2. Untar the tarball
3. Follow the instructions in the "boostrapping" section of README *AFTER* `git clone`

Observe error similar to the following:
```
[INFO] - Extending version file '/rpmbuilddir/BUILDROOT/codechecker-6.9.1-201917252134.x86_64/config/analyzer_version.json'.
fatal: Not a git repository (or any of the parent directories): .git
[ERROR] - Failed to get last commit hash.
Traceback (most recent call last):
  File "./scripts/build/extend_version_file.py", line 41, in extend_version_file
    git_hash = run_cmd(git_hash_cmd, repository_root)
  File "./scripts/build/extend_version_file.py", line 28, in run_cmd
    cwd=cwd)
  File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['git', 'rev-parse', 'HEAD']' returned non-zero exit status 128
fatal: Not a git repository (or any of the parent directories): .git
[ERROR] - Failed to get last commit describe.
Traceback (most recent call last):
  File "./scripts/build/extend_version_file.py", line 61, in extend_version_file
    git_describe = run_cmd(git_describe_cmd, repository_root)
  File "./scripts/build/extend_version_file.py", line 28, in run_cmd
    cwd=cwd)
  File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['git', 'describe', '--always', '--tags', '--abbrev=0']' returned non-zero exit status 128
Traceback (most recent call last):
  File "./scripts/build/extend_version_file.py", line 139, in <module>
    extend_version_file(args['repository'], version_file)
  File "./scripts/build/extend_version_file.py", line 86, in extend_version_file
    version_json_data['git_describe'] = {'tag': git_tag,
UnboundLocalError: local variable 'git_tag' referenced before assignment
make[1]: *** [package] Error 1
```

**Expected behavior**
At minimum substitute a bogus made-up value for version.  Ideally I would be able to hard-code this version as part of the build.

**Desktop (please complete the following information)**
 - OS: Centos 7

**Additional context**
This is part of an effort to package codechecker as an installable RPM.

Changed files:
scripts/build/extend_version_file.py

diff --git a/scripts/build/extend_version_file.py b/scripts/build/extend_version_file.py
index e95cc2c5..be411a91 100755
--- a/scripts/build/extend_version_file.py
+++ b/scripts/build/extend_version_file.py
@@ -28,12 +28,21 @@ def run_cmd(cmd, cwd=None):
                                    cwd=cwd)
 
 
-def extend_version_file(repository_root, version_file):
+def extend_with_git_information(repository_root, version_json_data):
     """
     Extend CodeChecker version file with git information.
     """
-    with open(version_file) as v_file:
-        version_json_data = json.load(v_file)
+    version = version_json_data['version']
+    version_string = str(version['major'])
+    if int(version['minor']) != 0 or int(version['revision']) != 0:
+        version_string += ".{0}".format(version['minor'])
+    if int(version['revision']) != 0:
+        version_string += ".{0}".format(version['revision'])
+
+    LOG.info("This is CodeChecker v%s", version_string)
+
+    if not os.path.exists(os.path.join(repository_root, '.git')):
+        return
 
     git_hash = ''
     try:
@@ -46,12 +55,7 @@ def extend_version_file(repository_root, version_file):
         LOG.exception('Failed to run command: %s', ' '.join(git_hash_cmd))
         sys.exit(1)
 
-    version = version_json_data['version']
-    version_string = str(version['major'])
-    if int(version['minor']) != 0 or int(version['revision']) != 0:
-        version_string += ".{0}".format(version['minor'])
-    if int(version['revision']) != 0:
-        version_string += ".{0}".format(version['revision'])
+    LOG.info("Built from Git commit hash: %s", git_hash)
 
     git_describe = git_describe_dirty = version_string
     try:
@@ -79,13 +83,33 @@ def extend_version_file(repository_root, version_file):
     except subprocess.CalledProcessError:
         LOG.exception('Failed to get last commit describe.')
     except OSError:
-        LOG.exception('Failed to run command: %s', ' '.join(git_describe_cmd))
+        LOG.exception('Failed to run command: %s',
+                      ' '.join(git_describe_cmd))
         sys.exit(1)
 
     version_json_data['git_hash'] = git_hash
     version_json_data['git_describe'] = {'tag': git_tag,
                                          'dirty': git_tag_dirty}
 
+    # Show the original, Git-given describe information.
+    LOG.info("Built from Git tags: %s (%s)", git_describe,
+             git_describe_dirty)
+
+    # Show the touched-up version information that uses the configuration
+    # file.
+    LOG.info("Baked with Git tag information merged with configured "
+             "version: %s (%s)", git_tag, git_tag_dirty)
+
+
+def extend_version_file(repository_root, version_file):
+    """
+    Extend CodeChecker version file with build date and git information.
+    """
+    with open(version_file) as v_file:
+        version_json_data = json.load(v_file)
+
+    extend_with_git_information(repository_root, version_json_data)
+
     time_now = time.strftime("%Y-%m-%dT%H:%M")
     version_json_data['package_build_date'] = time_now
 
@@ -96,14 +120,6 @@ def extend_version_file(repository_root, version_file):
 
     # Show version information on the command-line.
     LOG.debug(json.dumps(version_json_data, sort_keys=True, indent=2))
-    LOG.info("This is CodeChecker v%s (%s)", version_string, git_hash)
-
-    # Show the original, Git-given describe information.
-    LOG.info("Built from Git tags: %s (%s)", git_describe, git_describe_dirty)
-
-    # Show the touched-up version information that uses the configuration file.
-    LOG.info("Baked with Git tag information merged with configured version: "
-             "%s (%s)", git_tag, git_tag_dirty)
 
 
 if __name__ == "__main__":