```python import os import sys from git import Repo, GitCommandError def clone_repo(repo_url, branch="main"): try: Repo.clone_from(repo_url, ".", branch=branch) except GitCommandError as e: print(f"Error cloning repository: {e}") sys.exit(1) def create_markdown_file(file_name, content): try: with open(f"{file_name}.md", "w") as f: f.write(content) except IOError as e: print(f"Error creating Markdown file: {e}") sys.exit(1) def commit_and_push(file_name, message): repo = Repo(".") try: repo.index.add([f"{file_name}.md"]) repo.index.commit(message) repo.remote().push() except GitCommandError as e: print(f"Error committing and pushing changes: {e}") sys.exit(1) def create_new_branch(branch_name): repo = Repo(".") try: repo.create_head(branch_name).checkout() repo.head.reference.set_tracking_url( f"https://your_git_server/{REPO_OWNER}/{REPO_NAME}.git/{branch_name}" ) repo.remote().push() except GitCommandError as e: print(f"Error creating new branch: {e}") sys.exit(1) if __name__ == "__main__": if len(sys.argv) < 3: print("Usage: python push_markdown.py <repo_url> <markdown_file_name>") sys.exit(1) repo_url = sys.argv[1] file_name = sys.argv[2] # Clone the repository clone_repo(repo_url) # Create a new Markdown file with content create_markdown_file(file_name, "Hello, World!\n") print(f"Created '{file_name}.md' successfully.") # Commit and push changes to the main branch commit_and_push(file_name, f"Add {file_name}.md") print("Changes committed and pushed to main branch.") # Create a new branch named after the Markdown file create_new_branch(file_name) print(f"Successfully created '{file_name}' branch with '{file_name}.md'.") ``` Now, the script includes error handling for cloning the repository, creating the Markdown file, committing and pushing changes, and creating a new branch. It also provides better output formatting by printing success messages after each step. To use this updated script, run it from the command line like this: ```bash python push_markdown.py https://your_git_server/username/repo.git my_new_file ``` This will clone the repository, create a new Markdown file named `my_new_file.md`, commit and push changes to the main branch, create a new branch named `my_new_file`, and print success messages for each step.
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
import os
|
|
import sys
|
|
from git import Repo
|
|
|
|
def clone_repo(repo_url, branch="main"):
|
|
Repo.clone_from(repo_url, ".", branch=branch)
|
|
|
|
def create_markdown_file(file_name, content):
|
|
with open(f"{file_name}.md", "w") as f:
|
|
f.write(content)
|
|
|
|
def commit_and_push(file_name, message):
|
|
repo = Repo(".")
|
|
repo.index.add([f"{file_name}.md"])
|
|
repo.index.commit(message)
|
|
repo.remote().push()
|
|
|
|
def create_new_branch(branch_name):
|
|
repo = Repo(".")
|
|
repo.create_head(branch_name).checkout()
|
|
repo.head.reference.set_tracking_url(f"https://your_git_server/{REPO_OWNER}/{REPO_NAME}.git/{branch_name}")
|
|
repo.remote().push()
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python push_markdown.py <repo_url> <markdown_file_name>")
|
|
sys.exit(1)
|
|
|
|
repo_url = sys.argv[1]
|
|
file_name = sys.argv[2]
|
|
|
|
# Clone the repository
|
|
clone_repo(repo_url)
|
|
|
|
# Create a new Markdown file with content
|
|
create_markdown_file(file_name, "Hello, World!\n")
|
|
|
|
# Commit and push changes to the main branch
|
|
commit_and_push(file_name, f"Add {file_name}.md")
|
|
|
|
# Create a new branch named after the Markdown file
|
|
create_new_branch(file_name)
|
|
|
|
print(f"Successfully created '{file_name}' branch with '{file_name}.md'.")
|