From 7a3dcb3dccf4e5a62de0f761e381d67ef183de3f Mon Sep 17 00:00:00 2001 From: jgart Date: Sat, 6 Nov 2021 23:46:45 -0400 Subject: [PATCH] Sets branch for /blob URLs and removes from path. Co-authored-by: Ryan Prior --- giturlparse/platforms/github.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/giturlparse/platforms/github.py b/giturlparse/platforms/github.py index 8eb44ef..786b98d 100644 --- a/giturlparse/platforms/github.py +++ b/giturlparse/platforms/github.py @@ -1,3 +1,5 @@ +import re + from .base import BasePlatform @@ -33,7 +35,14 @@ class GitHubPlatform(BasePlatform): def clean_data(data): data = BasePlatform.clean_data(data) if data["path_raw"].startswith("/blob/"): - data["path"] = data["path_raw"].replace("/blob/", "") + path = data["path_raw"].replace("/blob/", "") + branch_regex = re.compile(r"[^/]+") + match = branch_regex.match(path) + branch = match and match[0] + data["branch"] = branch + if branch: + path = path.replace(branch + "/", "") + data["path"] = path if data["path_raw"].startswith("/tree/"): data["branch"] = data["path_raw"].replace("/tree/", "") return data