fix git pull technique

This commit is contained in:
Andrew Ridgway 2025-12-24 12:12:12 +10:00
parent 733241554f
commit 7b160be3b7
Signed by: armistace
GPG Key ID: C8D9EAC514B47EF1

View File

@ -1,8 +1,11 @@
import os, shutil
import os
import shutil
from urllib.parse import quote
from git import Repo
from git.exc import GitCommandError
class GitRepository:
# This is designed to be transitory it will desctruvtively create the repo at repo_path
# if you have uncommited changes you can kiss them goodbye!
@ -11,8 +14,8 @@ class GitRepository:
def __init__(self, repo_path, username=None, password=None):
git_protocol = os.environ["GIT_PROTOCOL"]
git_remote = os.environ["GIT_REMOTE"]
#if username is not set we don't need parse to the url
if username==None or password == None:
# if username is not set we don't need parse to the url
if username == None or password == None:
remote = f"{git_protocol}://{git_remote}"
else:
# of course if it is we need to parse and escape it so that it
@ -39,7 +42,7 @@ class GitRepository:
print(f"Cloning failed: {e}")
return False
def fetch(self, remote_name='origin', ref_name='main'):
def fetch(self, remote_name="origin", ref_name="main"):
"""Fetch updates from a remote repository with authentication"""
try:
self.repo.remotes[remote_name].fetch(ref_name=ref_name)
@ -48,7 +51,7 @@ class GitRepository:
print(f"Fetching failed: {e}")
return False
def pull(self, remote_name='origin', ref_name='main'):
def pull(self, remote_name="origin", ref_name="main"):
"""Pull updates from a remote repository with authentication"""
print("Pulling Latest Updates (if any)")
try:
@ -62,18 +65,6 @@ class GitRepository:
"""List all branches in the repository"""
return [branch.name for branch in self.repo.branches]
def create_and_switch_branch(self, branch_name, remote_name='origin', ref_name='main'):
"""Create a new branch in the repository with authentication."""
try:
print(f"Creating Branch {branch_name}")
# Use the same remote and ref as before
self.repo.git.branch(branch_name)
except GitCommandError:
print("Branch already exists switching")
# ensure remote commits are pulled into local
self.repo.git.checkout(branch_name)
def add_and_commit(self, message=None):
"""Add and commit changes to the repository."""
try:
@ -91,12 +82,27 @@ class GitRepository:
print(f"Commit failed: {e}")
return False
def create_copy_commit_push(self, file_path, title, commit_messge):
self.create_and_switch_branch(title)
def create_copy_commit_push(self, file_path, title, commit_message):
# Check if branch exists remotely
remote_branches = [
ref.name.split("/")[-1] for ref in self.repo.remotes.origin.refs
]
self.pull(ref_name=title)
shutil.copy(f"{file_path}", f"{self.repo_path}src/content/")
if title in remote_branches:
# Branch exists remotely, checkout and pull
self.repo.git.checkout(title)
self.pull(ref_name=title)
else:
# New branch, create from main
self.repo.git.checkout("-b", title, "origin/main")
self.add_and_commit(f"'{commit_messge}'")
# Ensure destination directory exists
dest_dir = f"{self.repo_path}src/content/"
os.makedirs(dest_dir, exist_ok=True)
self.repo.git.push()
# Copy file
shutil.copy(f"{file_path}", dest_dir)
# Commit and push
self.add_and_commit(commit_message)
self.repo.git.push("--set-upstream", "origin", title)