@@ -41,7 +41,7 @@ def set_git_credentials(username: str, password: str):
4141
4242
4343def git_clone (url : str , mirror : bool = False , disable_ssl_verify : bool = False , proxy : str = None ) -> Repo :
44- if os .path .exists (TMP_REPO_GIT_DIRECTORY ):
44+ if os .path .exists (TMP_REPO_GIT_DIRECTORY ) and len ( os . listdir ( TMP_REPO_GIT_DIRECTORY )) :
4545 repo_cloned = Repo (TMP_REPO_GIT_DIRECTORY )
4646 origin_url = repo_cloned .remote ('origin' ).url
4747 if repo_cloned .bare == mirror and origin_url == url :
@@ -74,7 +74,7 @@ def configure_remote_to(repo: Repo, clone_url_to: str, proxy: str = '', ssl_veri
7474def repo_mirror (create_repo : bool , dry_run : bool , clone_url_from : str , login_from : str , password_from : str , proxy_from : str , disable_ssl_verify_from : bool ,
7575 git_to : GitClient , proxy_to : str , disable_ssl_verify_to : bool , org_to : str , repo : str , description : str = '' ):
7676 if dry_run :
77- logger .info (' Dry-run mode, skipping repository creation and mirroring.' )
77+ logger .info (' Dry-run mode, skipping repository creation and mirroring.' )
7878 return
7979 if create_repo :
8080 git_to .create_repo (org_to , repo , description )
@@ -88,15 +88,15 @@ def repo_mirror(create_repo: bool, dry_run: bool, clone_url_from: str, login_fro
8888
8989def repo_tags_sync (args , clone_url_from : str , git_from : GitClient , git_to : GitClient , repo : str , branches_updated : int ) -> bool :
9090 if args .dry_run :
91- logger .info (' Dry-run mode, skipping tags synchronization.' )
91+ logger .info (' Dry-run mode, skipping tags synchronization.' )
9292 return False
9393
9494 tags_commits_from = git_from .get_tags (args .from_org , repo )
9595 tags_commits_to = git_to .get_tags (args .to_org , repo )
9696 if branches_updated > 0 or set (tags_commits_from .keys ()).issubset (set (tags_commits_to .keys ())):
9797 return False
9898
99- logger .info (' All branches already synchronized, do tags only...' )
99+ logger .info (' All branches already synchronized, do tags only...' )
100100 set_git_credentials (git_from .get_login_or_token (), git_from .get_password ())
101101 repo_from_cloned = git_clone (url = clone_url_from , disable_ssl_verify = args .from_disable_ssl_verify , proxy = args .from_proxy )
102102 clone_url_to = git_to .get_repo_clone_url (args .to_org , repo )
@@ -107,17 +107,17 @@ def repo_tags_sync(args, clone_url_from: str, git_from: GitClient, git_to: GitCl
107107
108108
109109def repo_branch_sync (dry_run : bool , clone_url_from : str , login_from : str , password_from : str , proxy_from : str , disable_ssl_verify_from : bool ,
110- git_to : GitClient , proxy_to : str , disable_ssl_verify_to : bool , org_to : str , repo : str , branch : str ):
110+ git_to : GitClient , proxy_to : str , disable_ssl_verify_to : bool , org_to : str , repo : str , branch : str , with_tags : bool ):
111111 if dry_run :
112- logger .info (' Dry-run mode, skipping branch synchronization.' )
112+ logger .info (' Dry-run mode, skipping branch synchronization.' )
113113 return
114114 set_git_credentials (login_from , password_from )
115115 repo_from_cloned = git_clone (url = clone_url_from , disable_ssl_verify = disable_ssl_verify_from , proxy = proxy_from )
116116 repo_from_cloned .git .checkout (branch )
117117 clone_url_to = git_to .get_repo_clone_url (org_to , repo )
118118 configure_remote_to (repo_from_cloned , clone_url_to , proxy_to , not disable_ssl_verify_to )
119119 set_git_credentials (git_to .get_login_or_token (), git_to .get_password ())
120- repo_from_cloned .remote (GIT_REMOTE_TO ).push ().raise_if_error ()
120+ repo_from_cloned .remote (GIT_REMOTE_TO ).push (refspec = branch + ':' + branch , tags = with_tags ).raise_if_error ()
121121
122122
123123def repo_branches_sync (args , branches_commits_from : dict , branches_commits_to : dict ,
@@ -132,18 +132,23 @@ def repo_branches_sync(args, branches_commits_from: dict, branches_commits_to: d
132132 branches_scanned = branches_updated = 0
133133 for branch in input_parser .reduce (branches_commits_from .keys (), args .branches_include , args .branches_exclude ):
134134 branches_scanned += 1
135- logger .info (' Branch: %s' , branch )
135+ logger .info (' Branch: %s' , branch )
136136 commit_from = branches_commits_from .get (branch , None )
137- logger .info (' Commit From: %s' , commit_from )
137+ logger .info (' Commit From: %s' , commit_from )
138138 commit_to = branches_commits_to .get (branch , None )
139- logger .info (' Commit To : %s' , commit_to )
139+ logger .info (' Commit To : %s' , commit_to )
140140 if commit_from == commit_to :
141- logger .info (' Already synchronized.' )
141+ logger .info (' Already synchronized.' )
142142 continue
143- logger .info (' Synchronize branch...' )
143+ with_tags = False
144+ with_tags_str = ''
145+ if branches_updated == 0 :
146+ with_tags = True
147+ with_tags_str = ' (with tags)'
148+ logger .info (' Synchronize branch' + with_tags_str + '...' )
144149 branches_updated += 1
145150 repo_branch_sync (args .dry_run , clone_url_from , args .from_login , args .from_password , args .from_proxy , args .from_disable_ssl_verify ,
146- git_to , args .to_proxy , args .to_disable_ssl_verify , args .to_org , repo , branch )
151+ git_to , args .to_proxy , args .to_disable_ssl_verify , args .to_org , repo , branch , with_tags )
147152 return branches_scanned , branches_updated
148153
149154
@@ -175,7 +180,7 @@ def main() -> int:
175180
176181 # New repo to create and mirror
177182 if not git_to .has_repo (args .to_org , repo ):
178- logger .info (' Repository does not exist on "to" plaform, create as mirror...' )
183+ logger .info (' Repository does not exist on "to" plaform, create as mirror...' )
179184 total_repos_updated += 1
180185 description = git_from .get_repo_description (args .from_org , repo )
181186 repo_mirror (True , args .dry_run , clone_url_from , args .from_login , args .from_password , args .from_proxy , args .from_disable_ssl_verify ,
@@ -185,13 +190,13 @@ def main() -> int:
185190 # Branches on "from", skip if no commits
186191 branches_commits_from = git_from .get_branches (args .from_org , repo )
187192 if len (branches_commits_from ) == 0 :
188- logger .info (' Repository has no branches on "from" platform, skipping.' )
193+ logger .info (' Repository has no branches on "from" platform, skipping.' )
189194 continue
190195
191196 # Branches on "to", mirror repo if empty
192197 branches_commits_to = git_to .get_branches (args .to_org , repo )
193198 if len (branches_commits_to ) == 0 :
194- logger .info (' Repository has no branches on "to" platform, synchronize as mirror...' )
199+ logger .info (' Repository has no branches on "to" platform, synchronize as mirror...' )
195200 total_repos_updated += 1
196201 repo_mirror (
197202 False ,
0 commit comments