Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pre commit #449

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

majiayu000
Copy link

Hi!

I've added a commit message caching feature.

I implemented this because my pre-commit checks frequently failed, and yesterday I consumed 40k tokens for a single commit attempt.

For the caching system, I followed the XDG specification and stored the cache in the ~/.cache directory.

…and performance

The commit introduces new configuration options for logging and caching in OpenCommit. Users can now enable/disable logging and caching, and customize the directories for storing logs and cache files. This improves debugging capabilities, performance optimization, and pre-commit hook integration. The changes include updates to the README.md documentation, configuration handling in config.ts, and enhancements to the commit cache and logger utilities.
@di-sukharev
Copy link
Owner

@majiayu000 hi, thank you for the contribution. could you describe in few words how it works pls? there is also a broken test

Comment on lines +16 to +24
execSync(`
mkdir -p "${TEST_REPO}"
cd "${TEST_REPO}"
git init
git config user.name "test"
git config user.email "test@test.com"
echo "test" > test.txt
git add test.txt
`);

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium test

This shell command depends on an uncontrolled
absolute path
.
Comment on lines +82 to +90
execSync(`
mkdir -p "${TEST_REPO2}"
cd "${TEST_REPO2}"
git init
git config user.name "test"
git config user.email "test@test.com"
echo "test2" > test2.txt
git add test2.txt
`);

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium test

This shell command depends on an uncontrolled
absolute path
.
@majiayu000
Copy link
Author

Hi, sorry for my recklessness.

The caching system is designed to store and reuse commit messages, improving commit efficiency and reducing AI API calls. The system preserves recent commit messages and offers reuse options when committing the same files again.

Cache functionality can be customized through the following configuration options:

# Enable/disable caching (default: true)
OCO_ENABLE_CACHE=true

# Custom cache directory path (default: ~/.cache/opencommit/)
OCO_CACHE_DIR=/path/to/custom/cache

Core Features

1. Cache Storage

  • Each Git repository uses an independent cache file
  • Cache filenames are generated based on the MD5 hash of the repository path
  • Cache data is stored in JSON format

2. Cache Lookup

Before generating a new commit message, the system will:

  1. Check for existing cache file
  2. Verify repository path match
  3. Verify staged files list exact match
  4. If matched, ask user whether to use the cached commit message

3. Cache Cleanup

The system implements two cache cleanup mechanisms:

Immediate Cleanup

  • Clears corresponding repository cache immediately after successful commit
  • Ensures commit messages are not reused after being used

Periodic Cleanup

  • Automatically cleans cache files older than 7 days
  • Prevents cache from consuming excessive storage space
  • Cleanup check is triggered with each commit operation

Workflow

Unable to render rich display

Could not find a suitable point for the given distance

For more information, see https://docs.github.com/get-started/writing-on-github/working-with-advanced-formatting/creating-diagrams#creating-mermaid-diagrams

graph TD
    A[Start Commit] --> B{Check Cache}
    B -->|Cache Match Found| C{Ask User}
    B -->|No Match| D[Generate New Message]
    C -->|Accept| E[Use Cached Message]
    C -->|Decline| D
    D --> F[Save to Cache]
    E --> G[Execute Commit]
    F --> G
    G --> H[Clear Current Cache]
    H --> I[End]

Implementation Details

Cache Directory Structure

~/.cache/opencommit/
├── {repo_hash_1}_commit_cache.json
├── {repo_hash_2}_commit_cache.json
└── ...

Cache File Example

{
  "message": "feat: implement user authentication",
  "timestamp": 1709123456789,
  "files": [
    "src/auth/login.ts",
    "src/auth/middleware.ts"
  ],
  "repoPath": "/path/to/repository"
}

- Simplify commit message and output display using a consistent separator
- Remove duplicate logging and reduce verbosity in commit and cache processes
- Enhance test setup for commit cache by explicitly enabling cache
@majiayu000
Copy link
Author

About the failed test, it was caused by environment variables.

When OCO_ENABLE_CACHE is false, the test fails.

Now I have fixed the bug in test. It use OCO_ENABLE_CACHE=trueas env.

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