"is it possible to hook a git commit to the save on textmate?" Code Answer


what you want is this: create a new command, set "save" to "current file" (this option is above the text area), "input" to "entire document" and "output" to "show as tool tip". now copy the code below into the text area and assign command-s as the commands key-binding.

#!/usr/bin/env ruby
filename = env["tm_filepath"].split("/").last
`git add #{env["tm_filepath"]}`
`git commit -m "#{filename}"`

every time you type in command-s the file will be saved and committed to an (already existing) git repository. if the file wasn't changed, no commit will be done, because git will block the commit.

below i have extended the command to pop up a dialog for the commit message and to give a nice warning, if no git repository was found. you should be able to figure out the rest yourself. :)

#!/usr/bin/env ruby
require env['tm_support_path'] + '/lib/ui'

filename = env["tm_filepath"].split("/").last
message = textmate::ui.request_string(
    :title => "commiting changes of #{filename}",
    :prompt => "please enter the commit message for your changes."

add = `git add #{env["tm_filepath"]} 2>&1`
commit = `git commit -m "#{message}" 2>&1`

git_answer = add + commit
unless git_answer.grep(/fatal/).empty?
  puts "please initialize git repository first!"
By ueouoeuoeu on June 11 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.