From 1b6dfe76702165ad98d5369ce706bc28bd701714 Mon Sep 17 00:00:00 2001
From: Kay0u <pierre@kayou.io>
Date: Fri, 21 Aug 2020 13:33:08 +0200
Subject: [PATCH] add branch when the needed one is not tracked

---
 list_builder.py | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/list_builder.py b/list_builder.py
index 12a2651..7da1fde 100755
--- a/list_builder.py
+++ b/list_builder.py
@@ -95,10 +95,19 @@ def refresh_cache(app, infos):
     if os.path.exists(fetch_head) and now - os.path.getmtime(fetch_head) < 3600:
         return
 
-    branch=infos.get("branch", "master")
+    branch = infos.get("branch", "master")
 
     try:
         git("remote set-url origin " + infos["url"], in_folder=app_cache_folder(app))
+        current_branch = git("branch --show-current", in_folder=app_cache_folder(app))
+        if current_branch != branch:
+            all_branches = git("branch --format=%(refname:short)", in_folder=app_cache_folder(app)).split()
+            if branch not in all_branches:
+                git("remote set-branches --add origin %s" % branch, in_folder=app_cache_folder(app))
+                git("fetch origin %s:%s" % (branch, branch), in_folder=app_cache_folder(app))
+            else:
+                git("checkout --force %s" % branch,  in_folder=app_cache_folder(app))
+
         git("fetch --quiet origin %s --force" % branch, in_folder=app_cache_folder(app))
         git("reset origin/%s --hard" % branch, in_folder=app_cache_folder(app))
     except: