Skip to content

Commit a193d7d

Browse files
committed
🚧 Refactors get_project_default_branch interface
1 parent 7724ae7 commit a193d7d

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

git_repo/services/ext/bitbucket.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ def gist_delete(self, gist_id):
240240
raise ResourceNotFoundError("Could not find snippet {}.".format(gist_id)) from err
241241
raise ResourceError("Couldn't delete snippet: {}".format(err)) from err
242242

243+
@staticmethod
244+
def get_project_default_branch(project):
245+
return project.mainbranch.get('name', 'master')
246+
243247
def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=None, description=None, auto_slug=False, edit=None):
244248
try:
245249
onto_project = self.get_repository(onto_user, onto_repo)
@@ -270,10 +274,7 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
270274
# if no from branch has been defined, chances are we want to push
271275
# the branch we're currently working on
272276
if not onto_branch:
273-
try:
274-
onto_branch = next(onto_project.branches()).name
275-
except StopIteration:
276-
onto_branch = 'master'
277+
onto_branch = self.get_project_default_branch(onto_project)
277278

278279
from_target = '{}:{}'.format(from_user, from_branch)
279280
onto_target = '{}/{}:{}'.format(onto_user, onto_project, onto_branch)

git_repo/services/ext/github.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ def gist_delete(self, gist_id):
231231
raise ResourceNotFoundError('Could not find gist')
232232
gist.delete()
233233

234+
@staticmethod
235+
def get_project_default_branch(project):
236+
return project.default_branch or 'master'
237+
234238
def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=None, description=None, auto_slug=False, edit=None):
235239
onto_project = self.gh.repository(onto_user, onto_repo)
236240

@@ -266,7 +270,7 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
266270
# if no from branch has been defined, chances are we want to push
267271
# the branch we're currently working on
268272
if not onto_branch:
269-
onto_branch = onto_project.default_branch or 'master'
273+
onto_branch = self.get_project_default_branch(onto_project)
270274

271275
from_target = '{}:{}'.format(from_user, from_branch)
272276
onto_target = '{}/{}:{}'.format(onto_user, onto_project, onto_branch)

git_repo/services/ext/gitlab.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ def gist_delete(self, snippet):
257257

258258
return snippet.delete()
259259

260+
@staticmethod
261+
def get_project_default_branch(project):
262+
return project.default_branch or 'master'
263+
260264
def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=None, description=None, auto_slug=False, edit=None):
261265
try:
262266
onto_project = self.gl.projects.get('/'.join([onto_user, onto_repo]))
@@ -292,7 +296,7 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
292296
# if no from branch has been defined, chances are we want to push
293297
# the branch we're currently working on
294298
if not onto_branch:
295-
onto_branch = onto_project.default_branch or 'master'
299+
onto_branch = self.get_project_default_branch(onto_project)
296300

297301
onto_target = '{}/{}:{}'.format(onto_user, onto_project.name, onto_branch)
298302

git_repo/services/ext/gogs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def get_auth_token(cls, login, password, prompt=None):
116116
token = gg.create_token(auth, name, login)
117117
return token.token
118118

119+
@staticmethod
120+
def get_project_default_branch(project):
121+
# TODO get default branch
122+
return 'master'
123+
119124
@property
120125
def user(self):
121126
return self.gg.username

git_repo/services/service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ def clone(self, user, repo, branch='master', rw=True):
329329
'''
330330
log.info('Cloning {}…'.format(repo))
331331

332-
remote = self.add(user=user, repo=repo, tracking=True, rw=rw)
332+
if not branch:
333+
project = self.get_repository(user, repo)
334+
branch = self.get_project_default_branch(project)
335+
333336
self.pull(remote, branch)
334337

335338
def add(self, repo, user=None, name=None, tracking=False, alone=False, rw=True, auto_slug=False):

0 commit comments

Comments
 (0)