Skip to content

Commit 699d07a

Browse files
authored
Merge pull request #16 from butschster/feature/git-history-diff
Add Git commit diff source for tracking code changes
2 parents a520bce + b070d95 commit 699d07a

27 files changed

+1995
-21
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ vendor
1010
node_modules
1111
.php-cs-fixer.cache
1212
.context
13-
runtime
13+
runtime/*

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ It automates the process of building context files from various sources:
1717

1818
- code files,
1919
- GitHub repositories,
20+
- Git commit changes and diffs
2021
- Web pages (URLs) with CSS selectors,
2122
- and plain text.
2223

@@ -186,6 +187,25 @@ Create a `context.json` file in your project root:
186187
]
187188
}
188189
]
190+
},
191+
{
192+
"description": "Recent Git Changes",
193+
"outputPath": "docs/recent-changes.md",
194+
"sources": [
195+
{
196+
"type": "text",
197+
"description": "Documentation Header",
198+
"content": "# Recent Git Changes\nThis document contains recent changes from the git repository."
199+
},
200+
{
201+
"type": "git_diff",
202+
"description": "Recent Commits (Last 1)",
203+
"repository": ".",
204+
"commit": "last",
205+
"filePattern": "*.php",
206+
"showStats": true
207+
}
208+
]
189209
}
190210
]
191211
}
@@ -494,6 +514,113 @@ Pull files directly from a GitHub repository:
494514
}
495515
```
496516

517+
### Git Diff Source
518+
519+
The source allows you to include changes from Git commits, providing a streamlined way to show recent code changes:
520+
521+
```json
522+
{
523+
"type": "git_diff",
524+
"description": "Recent Git Changes",
525+
"commit": "last",
526+
"filePattern": "*.php",
527+
"notPath": [
528+
"tests",
529+
"vendor"
530+
],
531+
"path": "src",
532+
"contains": "class",
533+
"notContains": "@deprecated",
534+
"showStats": true
535+
}
536+
```
537+
538+
#### Commit Range Presets
539+
540+
The Git diff source supports many convenient presets for `commit` parameter:
541+
542+
543+
```json
544+
{
545+
"type": "git_diff",
546+
"repository": ".",
547+
"commit": "last-week",
548+
"filePattern": "*.php"
549+
}
550+
```
551+
552+
Available presets include:
553+
554+
- **Basic ranges**:
555+
- `last`: Last commit (HEAD~1..HEAD)
556+
- `last-2`, `last-3`, `last-5`, `last-10`: Last N commits
557+
- `staged`: Changes staged but not committed
558+
- `unstaged`: Changes not yet staged
559+
560+
- **Time-based ranges**:
561+
- `today`: Changes from today
562+
- `last-24h`: Changes in the last 24 hours
563+
- `yesterday`: Changes from yesterday
564+
- `last-week`: Changes in the last week
565+
- `last-2weeks`: Changes in the last two weeks
566+
- `last-month`: Changes in the last month
567+
- `last-quarter`: Changes in the last three months
568+
- `last-year`: Changes in the last year
569+
570+
- **Branch comparisons**:
571+
- `main-diff`: Differences between main and current branch
572+
- `master-diff`: Differences between master and current branch
573+
- `develop-diff`: Differences between develop and current branch
574+
575+
#### Advanced Commit Specifications
576+
577+
You can use more specific Git expressions:
578+
579+
```json
580+
{
581+
"type": "git_diff",
582+
"repository": ".",
583+
"commit": "abc1234",
584+
"filePattern": "*.php"
585+
}
586+
```
587+
588+
```json
589+
{
590+
"type": "git_diff",
591+
"repository": ".",
592+
"commit": "abc1234:path/to/file.php",
593+
"filePattern": "*.php"
594+
}
595+
```
596+
597+
```json
598+
{
599+
"type": "git_diff",
600+
"repository": ".",
601+
"commit": "v1.0.0..v2.0.0",
602+
"filePattern": "*.php"
603+
}
604+
```
605+
606+
```json
607+
{
608+
"type": "git_diff",
609+
"repository": ".",
610+
"commit": "since:2023-01-15",
611+
"filePattern": "*.php"
612+
}
613+
```
614+
615+
```json
616+
{
617+
"type": "git_diff",
618+
"repository": ".",
619+
"commit": "date:2023-01-15",
620+
"filePattern": "*.php"
621+
}
622+
```
623+
497624
### URL Source
498625

499626
Fetch content from websites:
@@ -640,6 +767,26 @@ new GithubSource(
640767
);
641768
```
642769

770+
### CommitDiffSource
771+
772+
The `Butschster\ContextGenerator\Source\CommitDiffSource` allows you to include git diff content from commits or staged changes:
773+
774+
```php
775+
use Butschster\ContextGenerator\Source\CommitDiffSource;
776+
777+
new CommitDiffSource(
778+
repository: __DIR__, // Path to git repository (use current directory with __DIR__)
779+
description: 'Recent changes', // Optional description
780+
commit: 'last', // Commit range (preset, hash, or expression)
781+
filePattern: '*.php', // Pattern to match files (default: *.php)
782+
notPath: ['tests', 'vendor'], // Patterns to exclude
783+
path: 'src/Controller', // Path filter to include
784+
contains: 'namespace App', // Content pattern to include
785+
notContains: '@deprecated', // Content pattern to exclude
786+
showStats: true, // Whether to show git stats (default: true)
787+
);
788+
```
789+
643790
### UrlSource
644791

645792
The `Butschster\ContextGenerator\Source\UrlSource` allows you to fetch content from websites and extract specific

context.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
{
22
"documents": [
3+
{
4+
"description": "Recent Git Changes",
5+
"outputPath": "docs/recent-changes.md",
6+
"sources": [
7+
{
8+
"type": "text",
9+
"description": "Documentation Header",
10+
"content": "# Recent Git Changes\n\nThis document contains recent changes from the git repository.\n"
11+
},
12+
{
13+
"type": "git_diff",
14+
"description": "Recent Commits (Last 1)",
15+
"commit": "staged",
16+
"notPath": [
17+
"vendor",
18+
"tests"
19+
],
20+
"showStats": true
21+
}
22+
]
23+
},
324
{
425
"description": "Context generator code.",
526
"outputPath": "code.md",
@@ -13,6 +34,20 @@
1334
}
1435
]
1536
},
37+
{
38+
"description": "Git finder.",
39+
"outputPath": "git_diff.md",
40+
"sources": [
41+
{
42+
"type": "file",
43+
"sourcePaths": [
44+
"src/Fetcher/Git",
45+
"src/Fetcher/FinderResult"
46+
],
47+
"filePattern": "*.php"
48+
}
49+
]
50+
},
1651
{
1752
"description": "Context generator tests.",
1853
"outputPath": "tests.md",

json-schema.json

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"php_class",
6262
"url",
6363
"text",
64-
"github"
64+
"github",
65+
"git_diff"
6566
],
6667
"description": "Type of content source"
6768
},
@@ -158,6 +159,18 @@
158159
"then": {
159160
"$ref": "#/definitions/githubSource"
160161
}
162+
},
163+
{
164+
"if": {
165+
"properties": {
166+
"type": {
167+
"const": "git_diff"
168+
}
169+
}
170+
},
171+
"then": {
172+
"$ref": "#/definitions/gitCommitDiffSource"
173+
}
161174
}
162175
]
163176
},
@@ -444,6 +457,136 @@
444457
"description": "GitHub API token for private repositories (can use env var pattern ${TOKEN_NAME})"
445458
}
446459
}
460+
},
461+
"gitCommitDiffSource": {
462+
"properties": {
463+
"repository": {
464+
"type": "string",
465+
"description": "Path to the git repository"
466+
},
467+
"commit": {
468+
"type": "string",
469+
"enum": [
470+
"last",
471+
"last-5",
472+
"last-10",
473+
"last-week",
474+
"last-month",
475+
"unstaged",
476+
"staged",
477+
"wip",
478+
"main-diff",
479+
"master-diff",
480+
"develop-diff",
481+
"today",
482+
"last-24h",
483+
"yesterday",
484+
"last-2weeks",
485+
"last-quarter",
486+
"last-year",
487+
"stash",
488+
"stash-last",
489+
"stash-1",
490+
"stash-2",
491+
"stash-3",
492+
"stash-all",
493+
"stash-latest-2",
494+
"stash-latest-3",
495+
"stash-latest-5",
496+
"stash-before-pull",
497+
"stash-wip",
498+
"stash-untracked",
499+
"stash-index"
500+
],
501+
"description": "Git commit range (e.g., 'HEAD~5..HEAD')",
502+
"default": "staged"
503+
},
504+
"filePattern": {
505+
"oneOf": [
506+
{
507+
"type": "string",
508+
"description": "Pattern to match files (e.g., *.php)"
509+
},
510+
{
511+
"type": "array",
512+
"description": "List of patterns to match files (e.g., ['*.php', '*.md'])",
513+
"items": {
514+
"type": "string"
515+
}
516+
}
517+
],
518+
"default": "*.*"
519+
},
520+
"path": {
521+
"oneOf": [
522+
{
523+
"type": "string",
524+
"description": "Pattern to include only files in specific paths"
525+
},
526+
{
527+
"type": "array",
528+
"description": "List of patterns to include only files in specific paths",
529+
"items": {
530+
"type": "string"
531+
}
532+
}
533+
],
534+
"default": []
535+
},
536+
"notPath": {
537+
"type": "array",
538+
"description": "Patterns to exclude files by path",
539+
"items": {
540+
"type": "string"
541+
},
542+
"default": []
543+
},
544+
"contains": {
545+
"oneOf": [
546+
{
547+
"type": "string",
548+
"description": "Pattern to include only diffs containing specific content"
549+
},
550+
{
551+
"type": "array",
552+
"description": "List of patterns to include only diffs containing specific content",
553+
"items": {
554+
"type": "string"
555+
}
556+
}
557+
],
558+
"default": []
559+
},
560+
"notContains": {
561+
"oneOf": [
562+
{
563+
"type": "string",
564+
"description": "Pattern to exclude diffs containing specific content"
565+
},
566+
{
567+
"type": "array",
568+
"description": "List of patterns to exclude diffs containing specific content",
569+
"items": {
570+
"type": "string"
571+
}
572+
}
573+
],
574+
"default": []
575+
},
576+
"showStats": {
577+
"type": "boolean",
578+
"description": "Whether to show commit stats in output",
579+
"default": true
580+
},
581+
"modifiers": {
582+
"type": "array",
583+
"description": "List of content modifiers to apply",
584+
"items": {
585+
"type": "string"
586+
},
587+
"default": []
588+
}
589+
}
447590
}
448591
}
449592
}

runtime/php-cs-fixer.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)