@@ -210,6 +210,13 @@ def current_commit(self):
210
210
"""
211
211
return self .repo .head .commit .hexsha
212
212
213
+ def get_hash (self , ** kwargs ):
214
+ """
215
+ Returns the current commit hash
216
+ pass short=True for short
217
+ """
218
+ return self .repo .git .rev_parse ("HEAD" , ** kwargs )
219
+
213
220
def init_repository (self ):
214
221
"""
215
222
Clones the repository if it does not exist
@@ -219,6 +226,9 @@ def init_repository(self):
219
226
if not os .path .exists (self .directory ):
220
227
os .makedirs (self .directory )
221
228
229
+ # init services first to setup auth
230
+ self .init_services (self .repository_config )
231
+
222
232
try :
223
233
self .repo = git .Repo (self .directory )
224
234
@@ -241,8 +251,14 @@ def init_repository(self):
241
251
"No url specified and specified directory is not a git repository"
242
252
)
243
253
254
+ env = os .environ .copy ()
255
+ self .log .debug (f"Cloning repository from { self .url } : { self .directory } " )
244
256
self .repo = git .Repo .clone_from (
245
- self .url , self .directory , branch = self .default_branch , progress = None
257
+ self .url ,
258
+ self .directory ,
259
+ branch = self .default_branch ,
260
+ progress = None ,
261
+ env = env ,
246
262
)
247
263
self .init_submodules ()
248
264
@@ -256,8 +272,6 @@ def init_repository(self):
256
272
257
273
self .load_repository_config (self .repository_config_filename )
258
274
259
- self .init_services (self .repository_config )
260
-
261
275
def init_submodules (self ):
262
276
"""
263
277
Initializes and updates existing submodules
@@ -322,13 +336,29 @@ def init_services(self, config: RepositoryConfig):
322
336
"""
323
337
Initializes the services for the repository
324
338
"""
325
- if config .gitlab_url and not self .services .gitlab :
339
+ # why do we have 2 configs?
340
+ if config .gitlab_url != self .repository_config .gitlab_url :
341
+ raise ValueError ("config passed is not repo config" )
342
+
343
+ # argparse seems to be interfering with the GITLAB_URL var
344
+ gitlab_url = os .getenv ("GITLAB_URL" ) or config .gitlab_url
345
+ gitlab_token = os .getenv ("GITLAB_TOKEN" ) or config .gitlab_token
346
+ # update repo config
347
+ self .repository_config .gitlab_token = gitlab_token
348
+ self .repository_config .gitlab_url = gitlab_url
349
+
350
+ if gitlab_url and not self .services .gitlab :
326
351
# instance_url wants only the scheme and host
327
352
# so we need to parse it out of the full url
353
+ instance_url = (
354
+ urllib .parse .urlparse (gitlab_url ).scheme
355
+ + "://"
356
+ + urllib .parse .urlparse (gitlab_url ).netloc
357
+ )
328
358
329
359
self .services .gitlab = GitlabService (
330
360
token = config .gitlab_token ,
331
- instance_url = self . repository_config . gitlab_url ,
361
+ instance_url = instance_url ,
332
362
)
333
363
if config .github_token and not self .services .github :
334
364
self .services .github = GithubService (token = config .github_token )
@@ -367,14 +397,18 @@ def fetch(self, prune: bool = True):
367
397
fetch_args .append ("--prune" )
368
398
369
399
self .log .info (f"Fetching from { self .origin .name } " )
370
- self .repo .git .fetch (* fetch_args )
400
+ fetch_info = self .repo .git .fetch (* fetch_args )
401
+ self .log .debug (f"Fetch info: { fetch_info } " )
402
+ return fetch_info
371
403
372
404
def pull (self ):
373
405
"""
374
406
Pulls the origin repository
375
407
"""
376
408
self .log .info (f"Pulling from { self .origin .name } " )
377
- self .repo .git .pull (self .origin .name , self .branch )
409
+ fetch_info = self .repo .git .pull (self .origin .name , self .branch )
410
+ self .log .debug (f"Fetch info: { fetch_info } " )
411
+ return fetch_info
378
412
379
413
def push (self , force : bool = False ):
380
414
"""
@@ -486,6 +520,8 @@ def switch_branch(self, branch_name: str, create: bool = True):
486
520
"""
487
521
488
522
self .log .info (f"Switching to branch { branch_name } " )
523
+ # fetch to make sure we have the latest refs
524
+ self .fetch ()
489
525
490
526
try :
491
527
branch_exists_locally = self .repo .heads [branch_name ]
@@ -619,6 +655,47 @@ def remote_branch_reference(self, branch_name: str):
619
655
return ref
620
656
return None
621
657
658
+ def archive_branch (self , new_name : str , branch : str = None ):
659
+ """
660
+ Rename the remote branch and delete the local
661
+
662
+ This renames remote and doesn't check out to local
663
+
664
+ **Arguments**
665
+
666
+ - branch_name: The new name of the branch
667
+ """
668
+
669
+ if not branch :
670
+ branch = self .branch
671
+
672
+ if branch == self .default_branch :
673
+ raise ValueError (f"Cannot rename default branch { self .default_branch } " )
674
+
675
+ if branch == self .branch :
676
+ # cannot rename current branch
677
+ self .switch_branch (self .default_branch )
678
+
679
+ self .log .info (f"Renaming branch { self .branch } to { new_name } " )
680
+
681
+ # this doesn't rename remote
682
+ # self.repo.heads[self.branch].rename(new_name)
683
+
684
+ # Push the archive branch and delete the merge branch both locally and remotely
685
+ repo = self .repo
686
+ remote_name = self .origin .name
687
+
688
+ # not sure if pushing the remote ref is actually working
689
+ repo .git .push (remote_name , f"{ remote_name } /{ branch } :refs/heads/{ new_name } " )
690
+
691
+ # if old remote branch is still there, delete it
692
+ # this can depend on if the merge option to delete branch was checked
693
+ if branch in repo .git .branch ("-r" ).split ():
694
+ repo .git .push (remote_name , "--delete" , branch )
695
+
696
+ # delete local branch if it exists
697
+ repo .delete_head (branch , force = True )
698
+
622
699
def create_change_request (
623
700
self ,
624
701
title : str ,
@@ -744,7 +821,7 @@ def create_pull_request(self, title: str):
744
821
return self .create_change_request (title )
745
822
746
823
def merge_change_request (
747
- self , target_branch : str , source_branch : str , squash : bool = False
824
+ self , target_branch : str , source_branch : str , squash : bool = True
748
825
):
749
826
"""
750
827
Merge the change request
@@ -770,6 +847,7 @@ def merge_change_request(
770
847
- Pull requests: read and write
771
848
- Metadata: read
772
849
"""
850
+ self .log .info (f"Merging change request for branch { source_branch } { squash } " )
773
851
774
852
if not self .service :
775
853
raise ValueError ("No service configured" )
0 commit comments