35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import git
|
|
import os
|
|
from git import Repo
|
|
|
|
|
|
class GitHandler:
|
|
def __init__(self, repo_url, local_path, branch="main"):
|
|
self.repo_url = repo_url
|
|
self.local_path = local_path
|
|
self.branch = branch
|
|
self.repo = None
|
|
|
|
def clone_or_open_repo(self):
|
|
if not os.path.exists(self.local_path):
|
|
print(f"Cloning repository from {self.repo_url} to {self.local_path}")
|
|
self.repo = Repo.clone_from(self.repo_url, self.local_path)
|
|
else:
|
|
print(f"Opening existing repository at {self.local_path}")
|
|
self.repo = Repo(self.local_path)
|
|
|
|
def pull_updates(self):
|
|
try:
|
|
self.clone_or_open_repo()
|
|
print(f"Checking out and pulling branch: {self.branch}")
|
|
self.repo.git.checkout(self.branch)
|
|
self.repo.remotes.origin.pull()
|
|
print("Repository updated successfully.")
|
|
return True
|
|
except git.GitCommandError as e:
|
|
print(f"Error pulling updates: {e}")
|
|
return False
|
|
|
|
def get_current_commit(self):
|
|
return self.repo.head.commit.hexsha if self.repo else None
|