Skip to content

Commit

Permalink
Force git adds (#820)
Browse files Browse the repository at this point in the history
* add force

* remove force

* extract to method

* fix again

* check exist
  • Loading branch information
CTY-git authored Sep 13, 2024
1 parent e0a3787 commit 84069c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion patchwork/steps/CommitChanges/CommitChanges.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import contextlib
import fnmatch
from pathlib import Path

import git
Expand Down Expand Up @@ -123,11 +124,37 @@ def __init__(self, inputs: dict):
if self.enabled and self.branch_prefix == "" and self.branch_suffix == "":
raise ValueError("Both branch_prefix and branch_suffix cannot be empty")

def __get_ignored_groks(self, repo: Repo) -> set[str]:
ignored_groks = set()
gitignore_file = Path(repo.working_tree_dir) / ".gitignore"
if not gitignore_file.is_file():
return ignored_groks
lines = gitignore_file.read_text().splitlines()
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith("#") or stripped_line == "":
continue
ignored_groks.add(stripped_line)
return ignored_groks

def __get_repo_tracked_modified_files(self, repo: Repo) -> set[Path]:
repo_dir_path = Path(repo.working_tree_dir)
ignored_groks = self.__get_ignored_groks(repo)

repo_changed_files = set()
for item in repo.index.diff(None):
repo_changed_file = item.a_path
if any(fnmatch.fnmatch(repo_changed_file, ignored_grok) for ignored_grok in ignored_groks):
continue
repo_changed_files.add(repo_dir_path / repo_changed_file)

return repo_changed_files

def run(self) -> dict:
cwd = Path.cwd()
repo = git.Repo(cwd, search_parent_directories=True)
repo_dir_path = Path(repo.working_tree_dir)
repo_changed_files = {repo_dir_path / item.a_path for item in repo.index.diff(None)}
repo_changed_files = self.__get_repo_tracked_modified_files(repo)
repo_untracked_files = {repo_dir_path / item for item in repo.untracked_files}
modified_files = {Path(modified_code_file["path"]).resolve() for modified_code_file in self.modified_code_files}
true_modified_files = modified_files.intersection(repo_changed_files.union(repo_untracked_files))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "patchwork-cli"
version = "0.0.57"
version = "0.0.58"
description = ""
authors = ["patched.codes"]
license = "AGPL"
Expand Down

0 comments on commit 84069c4

Please sign in to comment.