Search code examples
gittextmate

Is it possible to hook a git commit to the save on Textmate?


The one feature that has kept me an Eclipse user is that each time you hit save, a copy of it goes into the Eclipse local history. Then you can do a diff on local history.

Still, I'd like to use Textmate since I heard such great things about it. I know there's a feature request for a future version...but I'm wondering if someone has found a plugin, or some other way to get this functionality shoehorned into Textmate now? I know it's a longshot but never hurts to ask.

Thank you.

UPDATE! (Edited the title of the question since the previous title got no interest) I just realized that perhaps this can be a solution. Is it possible to hook the Textmate save into a git commit?


Solution

  • 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!"
    end