Skip to content

Commit de3c509

Browse files
committed
Differ between active branches and available branches
Just because a branch is not currently being changed it does not mean its not there. Lets reflect this correctly.
1 parent 86e2e1e commit de3c509

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

src/gitsync/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def get_url_from_project(self) -> str:
2929
def get_active_branches(self, active_time=timedelta(days=40)) -> List[str]:
3030
pass
3131

32+
def get_all_branches(self) -> List[str]:
33+
pass
34+
3235
def get_tags(self) -> Set[str]:
3336
pass
3437

src/gitsync/github.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, project: Repository, provider):
1111
super().__init__()
1212
self.__project__ = project
1313
self.__provider__ = provider
14+
self.__branches__ = []
1415

1516
def get_url_from_project(self) -> str:
1617
url = self.__project__.clone_url
@@ -25,18 +26,30 @@ def get_url_from_project(self) -> str:
2526
+ url.path
2627
return url
2728

29+
def get_all_branches(self) -> List[str]:
30+
if self.__branches__:
31+
return [x.name for x in self.__branches__]
32+
33+
for branch in self.__project__.get_branches():
34+
self.__branch_commits__[branch.name] = branch.commit.sha
35+
self.__branches__.append(branch)
36+
37+
return [x.name for x in self.__branches__]
38+
2839
def get_active_branches(self, active_time=timedelta(days=40)) -> List[str]:
40+
if not self.__branches__:
41+
self.get_all_branches()
42+
2943
branches = set()
3044

31-
for branch in self.__project__.get_branches():
45+
for branch in self.__branches__:
3246
last_commit = branch.commit.commit.committer.date
3347
if datetime.now() - last_commit <= active_time:
3448
branches.add(branch.name)
3549
elif branch.protected:
3650
branches.add(branch.name)
3751
elif branch.name == self.__project__.default_branch:
3852
branches.add(branch.name)
39-
self.__branch_commits__[branch.name] = branch.commit.sha
4053

4154
return list(branches)
4255

src/gitsync/gitlab.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, project: Project, provider):
1111
super().__init__()
1212
self.__project__ = project
1313
self.__provider__ = provider
14+
self.__branches__ = []
1415

1516
def get_url_from_project(self) -> str:
1617
url = self.__project__.http_url_to_repo
@@ -25,7 +26,20 @@ def get_url_from_project(self) -> str:
2526
+ url.path
2627
return url
2728

29+
def get_all_branches(self) -> List[str]:
30+
if self.__branches__:
31+
return [x.name for x in self.__branches__]
32+
33+
for branch in self.__project__.branches.list(iterator=True):
34+
self.__branch_commits__[branch.name] = branch.commit['id']
35+
self.__branches__.append(branch)
36+
37+
return [x.name for x in self.__branches__]
38+
2839
def get_active_branches(self, active_time=timedelta(days=40)) -> List[str]:
40+
if not self.__branches__:
41+
self.get_all_branches()
42+
2943
branches = set([x.name for x in self.__project__.protectedbranches.list()])
3044
new_branches = set()
3145

@@ -34,7 +48,7 @@ def get_active_branches(self, active_time=timedelta(days=40)) -> List[str]:
3448
new_branches.add(branch)
3549
branches = new_branches
3650

37-
for branch in self.__project__.branches.list(iterator=True):
51+
for branch in self.__branches__:
3852
last_commit = datetime.fromisoformat(branch.commit['committed_date'])
3953
if datetime.now(timezone.utc) - last_commit <= active_time:
4054
branches.add(branch.name)

src/gitsynccmd/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def do_sync():
4040

4141
origin = providers[repo['origin']['provider']].get_project(repo['origin']['name'])
4242
branches = origin.get_active_branches()
43-
branch_commits = {x: origin.get_last_commit_on_branch(x) for x in branches}
43+
all_branches = origin.get_all_branches()
44+
branch_commits = {x: origin.get_last_commit_on_branch(x) for x in origin.get_all_branches()}
4445
tags = origin.get_tags()
4546

4647
mirror_refs = {}
@@ -54,7 +55,7 @@ def do_sync():
5455
mirror_tags[name] = mirror_ref.get_tags()
5556

5657
for x in mirror_ref.get_active_branches():
57-
if x not in branches:
58+
if x not in all_branches:
5859
log.info("Branch {} available on mirror but not on origin".format(x))
5960
branches.append(x)
6061
need_sync = True

0 commit comments

Comments
 (0)