public
Description: The Git TextMate Bundle
Homepage: http://tim.theenchanter.com/
Clone URL: git://github.com/timcharper/git-tmbundle.git
git-tmbundle / Support / app / controllers / commit_controller.rb
100644 81 lines (66 sloc) 2.605 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
CW = ENV['TM_SUPPORT_PATH'] + '/bin/CommitWindow.app/Contents/MacOS/CommitWindow'
 
class CommitController < ApplicationController
  def index
    if git.merge_message
      @status = git.status
      @message = git.merge_message
      render "merge_commit"
    else
      run_partial_commit
    end
  end
  
  def merge_commit
    message = params[:message]
    statuses = git.status(git.git_base)
    files = statuses.map { |status_options| (status_options[:status][:short] == "G") ? git.make_local_path(status_options[:path]) : nil }.compact
 
    auto_add_rm(files)
    res = git.commit(message, [])
    
    render "_commit_result", :locals => { :result => res, :files => files, :message => message }
  end
  
  protected
      
    def run_partial_commit
      @base = git.git_base
      target_file_or_dir = git.paths.first
      puts "<h1>Committing Files in ‘#{htmlize(shorten(target_file_or_dir, ENV['TM_PROJECT_DIRECTORY'] || @base))}’ on branch ‘#{htmlize(git.branch.current_name)}’</h1>"
      flush
 
      files, statuses = [], []
      git.status(target_file_or_dir).each do |e|
        files << e_sh(shorten(e[:path], @base))
        statuses << e_sh(e[:status][:short])
      end
      
      if files.empty?
        puts (git.clean_directory? ? "Working directory is clean (nothing to commit)" : "No changes to commit within the current scope. (Try selecting the root folder in the project drawer?)")
        return
      end
      
      msg, files = show_commit_dialog(files, statuses)
 
      unless files.empty?
        auto_add_rm(files)
        res = git.commit(msg, files)
        render "_commit_result", :locals => { :files => files, :message => msg, :result => res}
      end
    end
    
    def show_commit_dialog(files, statuses)
      status_helper_tool = ENV['TM_BUNDLE_SUPPORT'] + '/gateway/commit_dialog_helper.rb'
      
      res = %x{#{e_sh CW} \
--diff-cmd '#{git.git},diff' \
--action-cmd "M,D:Revert,#{status_helper_tool},revert" \
--status #{statuses.join ':'} \
#{files.join ' '} 2>/dev/console
}
 
      if $? != 0
        puts "<strong>Cancel</strong>"
        abort
      end
 
      res = Shellwords.shellwords(res)
      msg = res[1]
      files = res[2..-1]
      return msg, files
    end
    
    def auto_add_rm(files)
      git.chdir_base
      add_files = files.select{ |f| File.exists?(f) }
      remove_files = files.reject{ |f| File.exists?(f) }
      res = git.add(add_files) unless add_files.empty?
      res = git.rm(remove_files) unless remove_files.empty?
    end
end