From 01b7f1cd782e14ca56f8579593e7d507ba87fef2 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 24 May 2025 00:25:35 +1000 Subject: [PATCH] untested git stuff --- src/ai_generators/ollama_md_generator.py | 7 ++++ src/main.py | 12 +++++-- src/repo_management/repo_manager.py | 41 +++++++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/ai_generators/ollama_md_generator.py b/src/ai_generators/ollama_md_generator.py index 4b60653..7fe5948 100644 --- a/src/ai_generators/ollama_md_generator.py +++ b/src/ai_generators/ollama_md_generator.py @@ -150,3 +150,10 @@ class OllamaGenerator: def save_to_file(self, filename: str) -> None: with open(filename, "w") as f: f.write(self.generate_markdown()) + + def generate_commit_message(self): + prompt_system = "You are a blog creator commiting a piece of content to a central git repo" + prompt_human = f"Generate a 10 word git commit message describing {self.response}" + messages = [("system", prompt_system), ("human", prompt_human),] + commit_message = self.llm.invoke(messages).text() + return commit_message \ No newline at end of file diff --git a/src/main.py b/src/main.py index b3ea601..494fe54 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,7 @@ import ai_generators.ollama_md_generator as omg import trilium.notes as tn -import string +import repo_management.repo_manager as git_repo +import string,os tril = tn.TrilumNotes() @@ -23,4 +24,11 @@ for note in tril_notes: ai_gen = omg.OllamaGenerator(os_friendly_title, tril_notes[note]['content'], tril_notes[note]['title']) - ai_gen.save_to_file(f"/blog_creator/generated_files/{os_friendly_title}.md") + blog_path = f"/blog_creator/generated_files/{os_friendly_title}.md" + ai_gen.save_to_file(blog_path) + # Generate commit messages and push to repo + commit_message = ai_gen.generate_commit_message() + git_user = os.environp["GIT_USER"] + git_pass = os.environ["GIT_PASS"] + repo_manager = git_repo("blog/", git_user, git_pass) + repo_manager.create_copy_commit_push(blog_path, os_friendly_title, commit_message) diff --git a/src/repo_management/repo_manager.py b/src/repo_management/repo_manager.py index ebf6e5d..2ce8585 100644 --- a/src/repo_management/repo_manager.py +++ b/src/repo_management/repo_manager.py @@ -14,7 +14,7 @@ class GitRepository: if os.path.exists(repo_path): shutil.rmtree(repo_path) - + self.repo_path = repo_path Repo.clone_from(remote, repo_path) self.repo = Repo(repo_path) self.username = username @@ -50,3 +50,42 @@ class GitRepository: def get_branches(self): """List all branches in the repository""" return [branch.name for branch in self.repo.branches] + + + def create_branch(self, branch_name, remote_name='origin', ref_name='main'): + """Create a new branch in the repository with authentication.""" + try: + # Use the same remote and ref as before + self.repo.git.branch(branch_name, commit=True) + return True + except GitCommandError as e: + print(f"Failed to create branch: {e}") + return False + + def add_and_commit(self, message=None): + """Add and commit changes to the repository.""" + try: + # Add all changes + self.repo.git.add(all=True) + # Commit with the provided message or a default + if message is None: + commit_message = "Added and committed new content" + else: + commit_message = message + self.repo.git.commit(commit_message=commit_message) + return True + except GitCommandError as e: + print(f"Commit failed: {e}") + return False + + def create_copy_commit_push(self, file_path, title, commit_messge): + self.create_branch(title) + + shutil.copy(f"{file_path}", f"{self.repo_path}src/content/") + + self.add_and_commit(commit_messge) + + self.repo.git.push(remote_name='origin', ref_name=title, force=True) + + def remove_repo(self): + shutil.rmtree(self.repo_path) \ No newline at end of file