Skip to content

Add directory cleanup after ref deletion#160

Open
tomchon wants to merge 3 commits into
mainfrom
tomchon-patch-1
Open

Add directory cleanup after ref deletion#160
tomchon wants to merge 3 commits into
mainfrom
tomchon-patch-1

Conversation

@tomchon

@tomchon tomchon commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

This pull request refactors and enhances the .github/scripts/git_ref_lock_cleaner.py script to improve error handling, robustness, and cleanup of empty directories after reference deletions. The changes ensure that the script can better identify and resolve git ref lock errors, clean up any resulting empty directories, and retry operations when appropriate.

Error handling and recovery improvements:

  • Refactored the handler logic so that handle, delete_ref, and related methods return a boolean indicating success, allowing for better control flow and retry logic. [1] [2] [3]
  • Enhanced the main execution flow to support retrying git fetch up to two times if an error is detected and fixed, improving robustness against transient issues.

Directory cleanup enhancements:

  • Added the remove_empty_parents utility and integrated it into the ref deletion process to automatically remove empty parent directories under .git/refs and .git/logs/refs after a ref or reflog is deleted. [1] [2]

Error pattern matching and handling:

  • Improved regex patterns in the handlers to be more flexible and to support both English and Chinese error messages, and added logic to handle specific conflict scenarios where a blocking ref prevents the creation of a target ref. [1] [2]

These changes collectively make the script more reliable and easier to maintain in the face of various git ref lock error patterns.

Copilot AI review requested due to automatic review settings April 27, 2026 08:22

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the git ref lock cleaner script by introducing a retry mechanism for git fetch operations and implementing automatic cleanup of empty parent directories within the .git structure after a reference is deleted. It also refines the error handling flow to return success statuses and updates regex patterns for better compatibility. A review comment identifies that the handle override in Type2Handler is redundant and potentially problematic due to a fragile regex and incorrect directory cleanup logic, suggesting that the base class implementation is sufficient.

Comment thread .github/scripts/git_ref_lock_cleaner.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the .github/scripts/git_ref_lock_cleaner.py helper used in CI/local automation to recover from git fetch failures caused by ref lock/conflict issues, adding retry logic and cleanup of now-empty ref/reflog directories under .git.

Changes:

  • Refactors handler flow so handle() / delete_ref() return a boolean success value, enabling controlled retries.
  • Adds cleanup of empty parent directories after ref/reflog deletions.
  • Expands/refines error matching (including a more specific conflict case) and retries git fetch up to 2 times when a fix was applied.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread .github/scripts/git_ref_lock_cleaner.py
Comment thread .github/scripts/git_ref_lock_cleaner.py Outdated
Comment thread .github/scripts/git_ref_lock_cleaner.py Outdated
Comment thread .github/scripts/git_ref_lock_cleaner.py Outdated
- git_ref_lock_cleaner.py: add clean_gc_log() and run_gc() before fetch,
  add timeout to git fetch (600s) and git prune (300s)
- prepare_test_env.py: add timeout (600s) and progress logging to
  _execute_remote_command for SSH remote operations
Copilot AI review requested due to automatic review settings May 14, 2026 10:11
- Type2Handler regex: use \s* after colon to handle multi-line git error output
- remove_empty_parents: use startswith(stop_dir + os.sep) to prevent matching
  sibling paths like refs_backup when stop_dir is refs
- cleanup_ref_dirs: use isfile() instead of exists(), handle directory-form ref
  paths that conflict with ref creation
@tomchon tomchon force-pushed the tomchon-patch-1 branch from 757642b to 32db562 Compare May 14, 2026 10:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread .github/scripts/git_ref_lock_cleaner.py
Comment on lines 93 to 96
# error: cannot lock ref 'refs/remotes/origin/fix/3.0/TD-32817': is at 7af5 but expected eaba
# match the branch name before is at with a regular expression
# match the branch name before 'is at' with a regular expression
def match(self, error_output: str) -> bool:
return "is at" in error_output and "but expected" in error_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants