Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ migration-lint --loader=local_git --extractor=<your extractor>
It will examine files in current repository that are added or modified
and not yet commited.

If you need to check all committed changes in the current branch compared to
master/main branch (configurable with `CI_DEFAULT_BRANCH`):

```shell linenums="0"
migration-lint --loader=local_git_branch --extractor=<your extractor>
```

### GitLab

If you need to run it on the GitLab pipeline:
Expand Down
43 changes: 42 additions & 1 deletion migration_lint/source_loader/local.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Sequence
from typing import Any, Sequence

from migration_lint import logger
from migration_lint.source_loader.base import BaseSourceLoader
Expand Down Expand Up @@ -34,3 +34,44 @@ def get_changed_files(self) -> Sequence[SourceDiff]:
SourceDiff(old_path=diff.a_path, path=diff.b_path)
for diff in filtered_diffs
]


class LocalGitBranchLoader(BaseSourceLoader):
"""A loader to obtain files changed for local committed files since default branch."""

NAME = "local_git_branch"

def __init__(
self,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
self.default_branch = os.environ.get("CI_DEFAULT_BRANCH", "master")

def get_changed_files(self) -> Sequence[SourceDiff]:
"""Return a list of changed files."""

from git import Repo

logger.info(
f"### Getting changed files for local commited files since {self.default_branch}"
)

repo = Repo(os.getcwd(), search_parent_directories=True)
current_branch = repo.active_branch.name
diffs = repo.commit(self.default_branch).diff(repo.commit(current_branch))

filtered_diffs = [
d
for d in diffs
if not d.deleted_file
and (not self.only_new_files or self.only_new_files and d.new_file)
]

logger.info("Files changed: ")
logger.info("\n".join([f"- {d.a_path}" for d in filtered_diffs]))

return [
SourceDiff(old_path=diff.a_path, path=diff.b_path)
for diff in filtered_diffs
]