Skip to content

Commit 90fca14

Browse files
committed
Add git-add-match
Add `git-add-match` from [Git: programmatic staging](https://choly.ca/post/git-programmatic-staging/) blog post Signed-off-by: Joe Block <[email protected]>
1 parent d2f9f7c commit 90fca14

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ If you wrote one of these scripts and want it removed from this collection, plea
4545

4646
| Script | Original Source | Description |
4747
| ------ | --------------- | ----------- |
48+
| `git-add-match` | [Git: programmatic staging](https://choly.ca/post/git-programmatic-staging/) | Allows you to use `expect` to automatically stage hunks matching a search term |
4849
| `git-add-username-remote` | Ryan Tomayko's dotfiles | Adds a remote for the current repository for the given GitHub username. |
4950
| `git-age` | Kristoffer Gronlund's [wiki](https://github.com/krig/git-age/wiki) | A git-blame viewer, written using PyGTK. |
5051
| `git-amend-all` | John Wiegley's [git scripts](https://github.com/jwiegley/git-scripts) | Adds all modified and deleted files, except the new files and adds them to the recent commit by amending it. |

bin/git-add-match

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env expect -f
2+
#
3+
# Original source: https://choly.ca/post/git-programmatic-staging
4+
5+
# Set timeout to prevent the script from hanging
6+
set timeout -1
7+
8+
# Get the search pattern as a command line argument
9+
if {[llength $argv] != 0} {
10+
set pattern [lindex $argv 0]
11+
} else {
12+
puts "Error: search pattern not provided"
13+
exit 1
14+
}
15+
16+
# Open the interaction with git add -p
17+
spawn git add -p
18+
19+
# This is the main loop that handles the user interaction
20+
expect {
21+
# This expect block is for the hunk that contains the provided pattern
22+
"*$pattern*Stage this hunk*" {
23+
send "y\r"
24+
exp_continue
25+
}
26+
# This expect block is for continuing to the next hunk
27+
"*Stage this hunk*" {
28+
send "n\r"
29+
exp_continue
30+
}
31+
eof
32+
}

0 commit comments

Comments
 (0)