Skip to content

Instantly share code, notes, and snippets.

@icholy
Created March 1, 2024 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/afaf59a68848aca1bb08f6c46d45bbb6 to your computer and use it in GitHub Desktop.
Save icholy/afaf59a68848aca1bb08f6c46d45bbb6 to your computer and use it in GitHub Desktop.
#!/usr/bin/expect -f
# Set timeout to prevent the script from hanging
set timeout -1
# Get the search pattern as a command line argument
if {[llength $argv] != 0} {
set pattern [lindex $argv 0]
} else {
puts "Error: search pattern not provided"
exit 1
}
# Open the interaction with git add -p
spawn git add -p
# This is the main loop that handles the user interaction
expect {
# This expect block is for the hunk that contains the provided pattern
"*$pattern*Stage this hunk*" {
send "y\r"
exp_continue
}
# This expect block is for continuing to the next hunk
"*Stage this hunk*" {
send "n\r"
exp_continue
}
eof
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment