Skip to content

Commit 32d1f7f

Browse files
authored
Merge pull request #3 from SOSETH/bugfixes
Bugfixes
2 parents ed2c236 + de3c509 commit 32d1f7f

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
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: 17 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,16 +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)
37-
self.__branch_commits__[branch.name] = branch.commit.sha
51+
elif branch.name == self.__project__.default_branch:
52+
branches.add(branch.name)
3853

3954
return list(branches)
4055

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/gitsync/repo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def is_ancestor(self, commit_a: Commit, commit_b: Commit) -> bool:
1919
return self.__repo__.is_ancestor(commit_a, commit_b)
2020

2121
def push(self, commit: str, name: str, remote: str) -> bool:
22-
self.__repo__.create_head(name, commit)
22+
if name not in self.__repo__.heads:
23+
self.__repo__.create_head(name, commit)
24+
else:
25+
self.__repo__.heads[name].commit = commit
2326
remote = self.__repo__.remote(remote)
2427
result = remote.push(name + ':' + name)
2528
if len(result) < 1:

src/gitsync/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def filter_url(url: str) -> str:
99
return url
1010
else:
1111
groups = matches.groups()
12-
return groups[0] + '*****:*****' + groups[2]
12+
return groups[0] + '*****:*****' + groups[3]

src/gitsynccmd/__init__.py

Lines changed: 9 additions & 6 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
@@ -79,19 +80,21 @@ def do_sync():
7980
git_repo.add_remote(downstream.get_url_from_project(), name)
8081

8182
for x in branches:
82-
upstream_commit = branch_commits[x]
83-
upstream_commit_ref = git_repo.get_commit(upstream_commit)
83+
upstream_commit = branch_commits[x] if x in branch_commits else ''
84+
upstream_commit_ref = git_repo.get_commit(upstream_commit) if x in branch_commits else ''
8485
downstream_commit = downstream.get_last_commit_on_branch(x)
8586
downstream_commit_ref = git_repo.get_commit(downstream_commit)
8687

8788
if upstream_commit == downstream_commit:
8889
continue
8990

90-
if git_repo.is_ancestor(downstream_commit_ref, upstream_commit_ref):
91+
if upstream_commit_ref != '' and git_repo.is_ancestor(downstream_commit_ref,
92+
upstream_commit_ref):
9193
# Upstream is newer
9294
log.info("Pushing origin commits to mirror")
9395
git_repo.push(upstream_commit, x, name)
94-
elif git_repo.is_ancestor(upstream_commit_ref, downstream_commit_ref):
96+
elif upstream_commit_ref == '' or git_repo.is_ancestor(upstream_commit_ref,
97+
downstream_commit_ref):
9598
# Downstream is newer
9699
log.info("Pushing mirror commits to origin")
97100
git_repo.push(downstream_commit, x, 'origin')

0 commit comments

Comments
 (0)