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