Skip to content

fix(github-provider): preserve old_filename and content on renames - #2776

Merged
IsmaelMartinez merged 2 commits into
The-PR-Agent:mainfrom
AmirF194:fix/2775-github-rename-old-filename
Aug 22, 2026
Merged

IsmaelMartinez merged 2 commits into
The-PR-Agent:mainfrom
AmirF194:fix/2775-github-rename-old-filename

Conversation

@AmirF194

Copy link
Copy Markdown
Contributor

PR Type

Bug fix

Description

For a pure GitHub rename (status renamed, identical content, so GitHub omits .patch
and reports 0 additions/0 deletions), GithubProvider._get_diff_files does two things
wrong:

  • It never sets FilePatchInfo.old_filename, even though PyGithub's File object
    exposes previous_filename for a rename. The GitLab, local-git and generic
    diff-parsing providers all populate old_filename from the equivalent field; the
    GitHub provider was the one that dropped it.
  • It looks up the base-commit content for the original file at the file's NEW path
    (_get_pr_file_content(file, merge_base_commit.sha)), which does not exist at that
    commit, since the file lived under the OLD path there. The 404 is swallowed by
    get_pr_file_content's blanket except: return "", so original_file_content_str
    ends up empty and load_large_diff("", <full new content>) renders every line of the
    file as a + addition instead of a 0-line diff.

The fix computes old_filename from file.previous_filename when file.status == 'renamed', passes it through to FilePatchInfo, and uses it as the path for the
base-commit content lookup (mirroring gitlab_provider.py's old_path/new_path
split). _get_pr_file_content gained an optional path override for that purpose.

This is the same mechanism #1452 fixed for the Azure DevOps provider and
#2580/#2581 fixed for the local-git provider; the GitHub provider was never covered.

Verification

  • New regression tests in tests/unittest/test_github_provider_urls_and_diffs.py
    (TestGetDiffFilesRename) drive the real _get_diff_files/FilePatchInfo
    construction with only the PyGithub network boundary faked. Two of the three fail on
    unmodified main and pass on this branch; the third is a negative-case check
    (non-renamed files keep old_filename is None) that passes on both.
  • Full tests/unittest suite: 1977 passed, 1 skipped, 1 xfailed, 0 failed (Docker,
    docker/Dockerfile --target test, same image and command as the build-and-test
    CI job).
  • A standalone end-to-end script exercising _get_diff_files for a 40-line same-content
    rename, confirming old_filename is preserved and the patch is a 0/0 diff instead of a
    40-line addition.
  • Not checked: the incremental-review content lookup (self.incremental.last_seen_commit_sha),
    which has the same shape but a rename mid-review is a different, unverified scenario;
    left untouched.

verify_cmd: docker build -f docker/Dockerfile --target test -t pr-agent-test . && docker run --rm pr-agent-test python -m pytest tests/unittest -q

Fixes #2775

GithubProvider._get_diff_files never set FilePatchInfo.old_filename for
a renamed file, and looked up the base-commit content at the file's new
path instead of its previous_filename. For a same-content rename that
lookup 404s, the resulting empty original content makes load_large_diff
render the whole file as an addition instead of a 0-line diff.

Mirrors the old_filename handling already used by the GitLab, local-git
and generic diff-parsing providers.

Fixes The-PR-Agent#2775
@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Fix GitHub rename diffs by preserving old_filename and fetching base content by old path

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Populate FilePatchInfo.old_filename for GitHub renamed files using previous_filename.
• Fetch base-commit content using the pre-rename path to avoid false full-file additions.
• Add regression tests covering pure rename behavior and a non-rename negative case.
Diagram

graph TD
  A{{"GitHub API"}} --> B["PyGithub File"] --> C["_get_diff_files"] --> D{"status==renamed?"}
  D -->|"yes"| E["old path (previous_filename)"] --> G["get_pr_file_content(base_sha, path)"] --> H["FilePatchInfo(old_filename)"]
  D -->|"no"| F["new path (filename)"] --> G --> H
  subgraph Legend
    direction LR
    _ext{{"External"}} ~~~ _proc["Process"] ~~~ _dec{"Decision"}
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Fallback path probing (try new path, then old path on 404)
  • ➕ Avoids changing internal helper signatures by keeping call sites simpler
  • ➕ More resilient if upstream providers change rename metadata fields
  • ➖ Requires distinguishing 404 vs other errors (currently swallowed)
  • ➖ Adds extra API calls on renames or failures, increasing rate-limit pressure
2. Stop swallowing exceptions in get_pr_file_content (surface 404/diagnostics)
  • ➕ Prevents silent failures that can corrupt diff rendering
  • ➕ Improves debuggability across all content fetch scenarios
  • ➖ Behavior change may be breaking if callers rely on empty-string fallback
  • ➖ Would require broader auditing of error-handling and tests

Recommendation: Current approach is the best fit: it mirrors other providers’ old/new path split, preserves old_filename explicitly, and fixes the pure-rename false-additions issue without adding extra API calls or changing broader error-handling behavior.

Files changed (2) +54 / -8

Bug fix (1) +5 / -3
github_provider.pyFix pure rename handling by preserving old_filename and using old path for base content +5/-3

Fix pure rename handling by preserving old_filename and using old path for base content

• Derives 'old_filename' from PyGithub’s 'previous_filename' when 'file.status == 'renamed'' and stores it in 'FilePatchInfo'. Uses that old path when fetching base-commit content so same-content renames don’t render as full-file additions. Extends '_get_pr_file_content' with an optional path override to support the old/new split cleanly.

pr_agent/git_providers/github_provider.py

Tests (1) +49 / -5
test_github_provider_urls_and_diffs.pyAdd regression coverage for GitHub pure rename diffs +49/-5

Add regression coverage for GitHub pure rename diffs

• Extends file fakes to include 'previous_filename' and updates existing tests for the new '_get_pr_file_content' signature. Adds a dedicated test class verifying 'old_filename' propagation, base-content lookup uses the old path, and non-renamed files keep 'old_filename' unset.

tests/unittest/test_github_provider_urls_and_diffs.py

@qodo-code-review

qodo-code-review Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Single quotes in new code ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
The new old_filename assignment introduces single-quoted string literals (e.g., 'renamed',
'previous_filename'). This violates the project's requirement to use double quotes for Python
string literals.
Code

pr_agent/git_providers/github_provider.py[305]

+                old_filename = file.previous_filename if file.status == 'renamed' and getattr(file, 'previous_filename', None) else None
Relevance

●●● Strong

Recent accepted precedents enforce double quotes in newly added Python code, including provider
code.

PR-#2406
PR-#2677

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
PR Compliance ID 2694657 mandates double quotes for Python string literals. The newly added line
uses single quotes for string literals.

Rule 2694657: Use double quotes for all Python string literals
pr_agent/git_providers/github_provider.py[304-306]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
New Python code uses single-quoted string literals where the project requires double quotes.

## Issue Context
The added conditional assignment uses `'renamed'` and `'previous_filename'`; these should be switched to double-quoted equivalents unless single quotes avoid excessive escaping (not the case here).

## Fix Focus Areas
- pr_agent/git_providers/github_provider.py[305-305]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. old_filename line too long ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
The newly added old_filename assignment line exceeds the 120-character Ruff line-length limit.
This can cause CI lint failures and reduces readability.
Code

pr_agent/git_providers/github_provider.py[305]

+                old_filename = file.previous_filename if file.status == 'renamed' and getattr(file, 'previous_filename', None) else None
Relevance

●●● Strong

Recent GitHub precedent accepted wrapping newly added lines to satisfy Ruff’s 120-character limit.

PR-#2424

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
PR Compliance ID 2694655 requires Python lines to be <= 120 characters. The newly added
old_filename = ... line is a single long statement (including indentation) that exceeds this
limit.

Rule 2694655: Enforce 120-character maximum line length in Python source per Ruff config
pr_agent/git_providers/github_provider.py[304-306]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A new line in `github_provider.py` exceeds the 120-character maximum line length required by Ruff.

## Issue Context
This is a single long conditional assignment that should be split across multiple lines (or rewritten as a small `if` block) to stay within the configured lint limit.

## Fix Focus Areas
- pr_agent/git_providers/github_provider.py[304-306]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Incremental rename baseline wrong ✓ Resolved 🐞 Bug ≡ Correctness
Description
In incremental mode, _get_diff_files still fetches the baseline content using file.filename (the
new path) instead of the pre-rename path, so a rename between last_seen_commit_sha and head can
yield an empty baseline and a full-file + diff. Because get_pr_file_content swallows all
exceptions and returns "", this failure becomes indistinguishable from a real empty file and
silently produces incorrect patches.
Code

pr_agent/git_providers/github_provider.py[331]

+                            original_file_content_str = self._get_pr_file_content(file, merge_base_commit.sha, path=old_filename)
Relevance

●● Moderate

Rename baseline correctness is compelling, but incremental-mode behavior was explicitly unverified
and lacks a close GitHub precedent.

PR-#2387
PR-#2389

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The PR introduces old_filename and uses it only for the merge-base baseline fetch; the
incremental-mode baseline fetch still uses the new filename. Since get_pr_file_content returns an
empty string on exceptions, a missing path at the baseline commit will silently become an empty
baseline and load_large_diff will generate an additions-only patch.

pr_agent/git_providers/github_provider.py[304-335]
pr_agent/git_providers/github_provider.py[1134-1143]
pr_agent/algo/utils.py[705-721]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`GithubProvider._get_diff_files()` now correctly uses `old_filename` for the merge-base baseline fetch, but the incremental-mode baseline fetch (`last_seen_commit_sha`) still uses `file.filename` unconditionally. If a file was renamed after `last_seen_commit_sha`, the content at the new path does not exist at that commit, and `get_pr_file_content()` returns `""` due to its blanket exception handler. This can cause `load_large_diff()` to render the entire file as added.

### Issue Context
- Non-incremental path now passes `path=old_filename`.
- Incremental path still calls `_get_pr_file_content(file, self.incremental.last_seen_commit_sha)` without a `path` override.
- `get_pr_file_content()` returns `""` on any exception, masking missing-path failures.

### Fix Focus Areas
- pr_agent/git_providers/github_provider.py[304-335]
- pr_agent/git_providers/github_provider.py[1134-1143]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Context sources
✅ Compliance rules (platform): 34 rules
Review mode: 🚀 Fast: This is a localized rename-handling fix in one provider path, with focused regression coverage and no security, API, migration, or broad cross-cutting risk.

Grey Divider

Tip of the day
💡 Did you know, you can commit Qodo's fix in one click with committable suggestions (GitHub & GitLab)

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

…iew path too

The non-incremental base-commit lookup already used old_filename after
b40c237; the incremental-review lookup (last_seen_commit_sha) still used
the file's new path, so a rename inside an incremental review window hits
the same 404-swallowed-to-empty-string bug this PR exists to fix.

Also splits the previous_filename guard into a named boolean and switches
to double-quoted string literals to match the file's Ruff config.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit f90cca9

@IsmaelMartinez
IsmaelMartinez merged commit c577fdf into The-PR-Agent:main Aug 22, 2026
5 checks passed
@IsmaelMartinez

Copy link
Copy Markdown
Collaborator

Merged, thanks! The rename path had been quietly showing whole files as new, so this is a good one to have caught.

@AmirF194

Copy link
Copy Markdown
Contributor Author

Thanks for the quick merge. Glad it was worth catching, that whole-file-as-new rendering would have been an easy one to miss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GithubProvider renders a same-content file rename as a full-file addition and drops old_filename

2 participants