metadata_scm: rewrite git hash logic
PATCH 1/1 metadata_scm: rewrite git hash logic
The code to get the git and branch hash for the current commit in a specific repository was quite complex and prone to breakage, replace it with subprocess and git rev-parse.
Signed-off-by Ross Burton <[email protected]>
--- meta/classes/metadata_scm.bbclass | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
meta/classes/metadata_scm.bbclass +12 -11
@@ -65,18 +65,19 @@ def base_get_metadata_svn_revision(path, d): return revision def base_get_metadata_git_branch(path, d): - branch = os.popen('cd %s; git branch 2>&1 | grep "^* " | tr -d "* "' % path).read() + import subprocess - if len(branch) != 0: - return branch - return "<unknown>" + try: + return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], + cwd=path).strip() + except: + return "<unknown>" def base_get_metadata_git_revision(path, d): - f = os.popen("cd %s; git log -n 1 --pretty=oneline -- 2>&1" % path) - data = f.read() - if f.close() is None: - rev = data.split(" ")[0] - if len(rev) != 0: - return rev - return "<unknown>" + import subprocess + try: + return subprocess.check_output(["git", "rev-parse", "HEAD"], + cwd=path).strip() + except: + return "<unknown>"
Signature · 1 line
2.1.4
Quoted 8 lines · Ross Burton
The code to get the git and branch hash for the current commit in a specific
repository was quite complex and prone to breakage, replace it with subprocess
and git rev-parse.Signed-off-by Ross Burton <[email protected]>
Nicely done, parsing output of porcelains = bad. :)
Signature · 5 lines
Christopher Larson clarson at kergoth dot com Founder - BitBake, OpenEmbedded, OpenZaurus Maintainer - Tslib Senior Software Engineer, Mentor Graphics