patman: Improve Series support for patchwork links
Update Series with a way to better manage the Series-links lines in
patches. Use this in the 'status' subcommand instead of the existing
primitive method of expecting a link without a version prefix.
Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/patman/series.py b/tools/patman/series.py
index 3d48836..ad61bbf 100644
--- a/tools/patman/series.py
+++ b/tools/patman/series.py
@@ -430,3 +430,58 @@
if self.get('postfix'):
postfix = ' %s' % self['postfix']
return '%s%sPATCH%s%s' % (git_prefix, prefix, postfix, version)
+
+ def get_links(self, links_str=None, cur_version=None):
+ """Look up the patchwork links for each version
+
+ Args:
+ links_str (str): Links string to parse, or None to use self.links
+ cur_version (int): Default version to assume for un-versioned links,
+ or None to use self.version
+
+ Return:
+ dict:
+ key (int): Version number
+ value (str): Link string
+ """
+ if links_str is None:
+ links_str = self.links if 'links' in self else ''
+ if cur_version is None:
+ cur_version = int(self.version) if 'version' in self else 1
+ assert isinstance(cur_version, int)
+ links = {}
+ for item in links_str.split():
+ if ':' in item:
+ version, link = item.split(':')
+ links[int(version)] = link
+ else:
+ links[cur_version] = item
+ return links
+
+ def build_links(self, links):
+ """Build a string containing the links
+
+ Args:
+ links (dict):
+ key (int): Version number
+ value (str): Link string
+
+ Return:
+ str: Link string, e.g. '2:4433 1:2872'
+ """
+ out = ''
+ for vers in sorted(links.keys(), reverse=True):
+ out += f' {vers}:{links[vers]}'
+ return out[1:]
+
+ def get_link_for_version(self, find_vers, links_str=None):
+ """Look up the patchwork link for a particular version
+
+ Args:
+ find_vers (int): Version to find
+ links_str (str): Links string to parse, or None to use self.links
+
+ Return:
+ str: Series-links entry for that version, or None if not found
+ """
+ return self.get_links(links_str).get(find_vers)