Skip to content

Commit bc41bcf

Browse files
Git add files that are changed
1 parent 9b70e40 commit bc41bcf

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Creates PRs for dependency updates in python projects.
77
- Currently only supports a single specifier.
88
- File structure is fixed
99
- GH actions must only use major version
10+
- Recommend to have a clean git before running (or at least no changes to pyproject etc.)
1011

1112
## TODO
1213

src/upgrade_dependencies/main.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from upgrade_dependencies.dependency import GitHubDependency, PyPIDependency
1515
from upgrade_dependencies.project import Project
16-
from upgrade_dependencies.utils import run_shell_command
16+
from upgrade_dependencies.utils import get_git_status, run_shell_command
1717

1818
if TYPE_CHECKING:
1919
from upgrade_dependencies.dependency import Dependency
@@ -330,22 +330,24 @@ def update(
330330
version: _description_
331331
project_path: _description_
332332
"""
333-
# TODO: print status
333+
# TODO: print status of update
334334
project = Project(
335335
project_path=project_path,
336336
gh_pat=GH_PAT,
337337
)
338338

339+
# search for dependency and save old version
339340
try:
340341
dep = project.get_dependency(name=dependency)
341342
old_ver = str(sorted(dep.specifier, key=str)[0].version)
342343
except RuntimeError as e:
343344
rprint(f"Cannot find {dependency} in {project.name}.")
344345
raise typer.Exit(code=1) from e
345346

347+
# fetch data from pypi/github
346348
asyncio.run(dep.save_data())
347349

348-
# get version
350+
# get latest/desired version
349351
if version is None:
350352
version = str(dep.get_latest_version())
351353

@@ -358,11 +360,19 @@ def update(
358360

359361
run_shell_command(["git", "checkout", "-b", branch_name])
360362

363+
# get status of files before changes
364+
files_before = get_git_status()
365+
361366
# update dependency
362367
project.update_dependency(dependency=dep, version=version)
363368

364-
# stage changes - TODO: get files that are changed
365-
run_shell_command(["git", "add", ".github/"])
369+
# get status of files after changes
370+
files_after = get_git_status()
371+
372+
# get only the files that were changed
373+
changed_files = [f for f in files_after if f not in files_before]
374+
375+
run_shell_command(["git", "add", *changed_files])
366376

367377
# commit the changes
368378
if isinstance(dep, GitHubDependency) and dep.action:

src/upgrade_dependencies/utils.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,40 @@ def update_pre_commit(
250250
yaml.dump(data, temp_f) # pyright: ignore
251251

252252

253-
def run_shell_command(shell_args: list[str]) -> None:
253+
def run_shell_command(shell_args: list[str]) -> Any:
254254
"""_summary_.
255255
256256
Args:
257257
shell_args: _description_
258+
259+
Returns:
260+
_description_
258261
"""
259262
try:
260-
subprocess.run(shell_args, check=True, capture_output=True, text=True) # noqa: S603
263+
res = subprocess.run(shell_args, check=True, capture_output=True, text=True) # noqa: S603
261264
except subprocess.CalledProcessError as e:
262265
msg = f"Command failed with return code {e.returncode}.\n"
263266
msg += f"Error output: {e.stderr}"
264267
raise RuntimeError(msg) from e
268+
269+
return res
270+
271+
272+
def get_git_status() -> list[str]:
273+
"""Get the list of modified or untracked files from git status.
274+
275+
Returns:
276+
_description_
277+
"""
278+
result = run_shell_command(["git", "status", "-s"])
279+
280+
# parse the result to get the list of files
281+
changed_files: list[str] = []
282+
283+
for line in result.stdout.strip().split("\n"):
284+
status, file_path = line.split(maxsplit=1) # status and file name
285+
286+
if status in ["M", "A", "D"]: # Modified, Added, or Deleted
287+
changed_files.append(file_path)
288+
289+
return changed_files

0 commit comments

Comments
 (0)