Compare commits
1 Commits
master
...
fix-git-pu
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b160be3b7 |
@ -1,8 +1,11 @@
|
|||||||
import os, shutil
|
import os
|
||||||
|
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!
|
||||||
@ -39,7 +42,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)
|
||||||
@ -48,7 +51,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:
|
||||||
@ -62,18 +65,6 @@ 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:
|
||||||
@ -91,12 +82,27 @@ 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_messge):
|
def create_copy_commit_push(self, file_path, title, commit_message):
|
||||||
self.create_and_switch_branch(title)
|
# Check if branch exists remotely
|
||||||
|
remote_branches = [
|
||||||
|
ref.name.split("/")[-1] for ref in self.repo.remotes.origin.refs
|
||||||
|
]
|
||||||
|
|
||||||
|
if title in remote_branches:
|
||||||
|
# Branch exists remotely, checkout and pull
|
||||||
|
self.repo.git.checkout(title)
|
||||||
self.pull(ref_name=title)
|
self.pull(ref_name=title)
|
||||||
shutil.copy(f"{file_path}", f"{self.repo_path}src/content/")
|
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user