Skip to contentSkip to conversation

Make SRCREV_FORMAT name substitution safer

PATCH Superseded Ulf Magnusson 1 participant 2 messages Sep 12, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Make SRCREV_FORMAT name substitution safer

Changes since v2:

Using re.compile() is a bit silly for a one-off regex. Use re.sub() directly instead.

Changes since v1:

Fix the original patch to make it actually work, and broaden it to make the substitution more robust in general by substituting all names at once.

Ulf Magnusson (1):
bitbake: fetch2: Make SRCREV_FORMAT name substitution safer

 bitbake/lib/bb/fetch2/__init__.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH v3 1/1 bitbake: fetch2: Make SRCREV_FORMAT name substitution safer

The implementation of SRCREV_FORMAT has at least two issues:

1. Given two names "foo" and "foobar" and SRCREV_FORMAT = "foo_foobar", "foo" might currently get substituted twice, and "foobar" not at all.

2. If the revision substitued for some name happens to contain another name as a substring, then that substring might incorrectly get replaced.

Fix both issues by sorting the names with the longest ones first and replacing all names at once with a regular expression. This was inspired by
http://stackoverflow.com/questions/6116978/python-replace-multiple-strings.

---
 bitbake/lib/bb/fetch2/__init__.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
bitbake/lib/bb/fetch2/__init__.py +11 -1
index 11c75cc..5568fc3 100644
@@ -761,6 +761,7 @@ def get_srcrev(d, method_name='sortable_revision'):
     if not format:
         raise FetchError("The SRCREV_FORMAT variable must be set when multiple SCMs are used.")
 
+    name_to_rev = {}
     seenautoinc = False
     for scm in scms:
         ud = urldata[scm]
@@ -769,7 +770,16 @@ def get_srcrev(d, method_name='sortable_revision'):
             seenautoinc = seenautoinc or autoinc
             if len(rev) > 10:
                 rev = rev[:10]
-            format = format.replace(name, rev)
+            name_to_rev[name] = rev
+    # Replace names by revisions in the SRCREV_FORMAT string. The approach used
+    # here can handle names being prefixes of other names and names appearing
+    # as substrings in revisions (in which case the name should not be
+    # expanded). The '|' regular expression operator tries matches from left to
+    # right, so we need to sort the names with the longest ones first.
+    names_ascending_len = sorted(name_to_rev, key=len, reverse=True)
+    name_to_rev_re = "|".join(re.escape(name) for name in names_ascending_len)
+    format = re.sub(name_to_rev_re, lambda match: name_to_rev[match.group(0)], format)
+
     if seenautoinc:
        format = "AUTOINC+" + format
 
Signature · 1 line
2.5.0