Force checkout of branch when loading repo

This commit is contained in:
mike cullerton 2022-02-02 14:48:47 -05:00
parent 21b5d034e2
commit e9c7f3d9b6

View File

@ -11,7 +11,7 @@ from datetime import datetime
class GitService(object):
@staticmethod
def setup_repo(remote_path, directory, branch):
def setup_repo(remote_path, directory):
# we use github
# Note that the 'password' is a token generated by github, not the site password
username = app.config["GIT_USER_NAME"]
@ -19,7 +19,6 @@ class GitService(object):
remote_url = f"https://{username}:{password}@github.com/{remote_path}.git"
repo = Repo.clone_from(remote_url, directory)
repo.git.checkout(branch)
return repo
def __get_repo(self):
@ -49,6 +48,7 @@ class GitService(object):
app.logger.error(e)
raise ApiError(code='unknown_exception',
message=f'There was an unknown exception. Original message is: {e}')
repo.git.checkout(git_branch)
remote_ref = repo.refs[f'origin/{git_branch}']
repo.active_branch.set_tracking_branch(remote_ref)
return repo
@ -81,17 +81,6 @@ class GitService(object):
print(repo)
return repo
def merge_with_branch(self, branch):
# https://stackoverflow.com/questions/36799362/how-do-you-merge-the-master-branch-into-a-feature-branch-with-gitpython#36802900
repo = self._get_repo()
# current = repo.active_branch
merge_branch = repo.branches[branch]
base = repo.merge_base(repo.active_branch, merge_branch)
repo.index.merge_tree(merge_branch, base=base)
repo.index.commit(f'Merge {branch} into working branch', parent_commits=(repo.active_branch.commit, merge_branch.commit))
repo.active_branch.checkout(force=True)
print('merge_with_branch')
def pull_from_remote(self):
repo = self._get_repo()
if not repo.is_dirty():
@ -105,13 +94,24 @@ class GitService(object):
print(repo)
return repo
def get_local_status(self):
def merge_with_branch(self, branch):
# https://stackoverflow.com/questions/36799362/how-do-you-merge-the-master-branch-into-a-feature-branch-with-gitpython#36802900
repo = self._get_repo()
# get list of changed files
changes = [item.a_path for item in repo.index.diff(None)]
# get list of untracked files
untracked_files = repo.untracked_files
return [changes, untracked_files]
# current = repo.active_branch
merge_branch = repo.branches[branch]
base = repo.merge_base(repo.active_branch, merge_branch)
repo.index.merge_tree(merge_branch, base=base)
repo.index.commit(f'Merge {branch} into working branch', parent_commits=(repo.active_branch.commit, merge_branch.commit))
repo.active_branch.checkout(force=True)
print('merge_with_branch')
# def get_local_status(self):
# repo = self._get_repo()
# # get list of changed files
# changes = [item.a_path for item in repo.index.diff(None)]
# # get list of untracked files
# untracked_files = repo.untracked_files
# return [changes, untracked_files]
# @staticmethod
# def init_repo(directory):