From b6e6b62b689d8da0d3488c0039307308c3e9ae21 Mon Sep 17 00:00:00 2001
From: Christian Breunig <christian@breunig.cc>
Date: Sun, 25 Feb 2024 08:41:46 +0100
Subject: T6064: add build error if branch information from Git repository is
 missing

This was discussed in slack, where a user was missing the Git commit ID in his
custom build

Reason is/was:
  git clone --single-branch -b 1.4.0-epa1 https://github.com/vyos/vyos-build

Checks out the 1.4.0-epa1 tag as HEAD and does not clone any branch information.
This results in:

>>> import git
>>> repo = git.Repo('.')
>>> repo.head.object.hexsha[:14]
'bcac2eb1f9b49c'
>>> git_branch = repo.active_branch.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/git/repo/base.py", line 881, in active_branch
    return self.head.reference
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/git/refs/symbolic.py", line 311, in _get_reference
    raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
TypeError: HEAD is a detached symbolic reference as it points to 'bcac2eb1f9b49cc15ebda65838e5465543dbb9c6'
during the build. The exception handler resets the branch and commit name to an empty string: https://github.com/vyos/vyos-build/blob/a3e60a00b400a1bad8609d5ce1abb0bb7abed7bc/scripts/build-vyos-image#L281-L296

This now adds a proper error message during build so it fails early.

(07:46) vyos_bld 08278c5a1172:/vyos/vyos-build # isobuild -test
Building custom VyOS version: 1.5-test-202402250746
I: Checking if packages required for VyOS image build are installed
build/config
Could not retrieve information from git: HEAD is a detached symbolic reference as it points to '39612f541e55bea19868f50f16d7a6c6e0034ed2'

(cherry picked from commit 7c27657c79046dac8ae509a4eacb1a3a152e2d23)
---
 scripts/build-vyos-image | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

(limited to 'scripts')

diff --git a/scripts/build-vyos-image b/scripts/build-vyos-image
index 0f96cd68..8e299180 100755
--- a/scripts/build-vyos-image
+++ b/scripts/build-vyos-image
@@ -277,10 +277,9 @@ if __name__ == "__main__":
         # Retrieve git branch name
         git_branch = repo.active_branch.name
     except Exception as e:
-      print("Could not retrieve information from git: {0}".format(str(e)))
+      exit(f'Could not retrieve information from git: {e}')
       build_git = ""
       git_branch = ""
-      git_commit = ""
 
     # Create the build version string
     if build_config['build_type'] == 'development':
-- 
cgit v1.2.3