| |   |
| 1 | CW = ENV['TM_SUPPORT_PATH'] + '/bin/CommitWindow.app/Contents/MacOS/CommitWindow' |
| 2 | |
| 1 | 3 | module PartialCommitWorker |
| 2 | 4 | class NotOnBranchException < Exception; end |
| 3 | 5 | class NothingToCommitException < Exception; end |
| 4 | 6 | class CommitCanceledException < Exception; end |
| 5 | 7 | |
| 6 | 8 | def self.factory(_type, *args) |
| 7 | | klass = case _type |
| 8 | | when "amend" then |
| 9 | | PartialCommitWorker::Amend |
| 10 | | else |
| 11 | | PartialCommitWorker::Normal |
| 12 | | end |
| 13 | | |
| 9 | klass = (_type == "amend" ? PartialCommitWorker::Amend : PartialCommitWorker::Normal) |
| 14 | 10 | klass.new(*args) |
| 15 | 11 | end |
| 16 | 12 | |
| … | … | |
| 26 | 26 | @target_file_or_dir ||= git.paths.first |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | | def show_commit_dialog(files, statuses) |
| 30 | | status_helper_tool = ENV['TM_BUNDLE_SUPPORT'] + '/gateway/commit_dialog_helper.rb' |
| 29 | def split_file_statuses |
| 30 | [file_candidates.map{ |fc| fc[0] }, file_candidates.map{ |fc| fc[1] }] |
| 31 | end |
| 32 | |
| 33 | def status_helper_tool |
| 34 | ENV['TM_BUNDLE_SUPPORT'] + '/gateway/commit_dialog_helper.rb' |
| 35 | end |
| 36 | |
| 37 | def exec_commit_dialog |
| 38 | files, statuses = split_file_statuses |
| 31 | 39 | |
| 32 | 40 | res = %x{#{e_sh CW} \ |
| 33 | 41 | --diff-cmd '#{git.git},diff' \ |
| 34 | 42 | --action-cmd "M,D:Revert,#{status_helper_tool},revert" \ |
| 35 | | --status #{statuses.join ':'} \ |
| 36 | | #{files.join ' '} 2>/dev/console |
| 43 | --status #{statuses.join(':')} \ |
| 44 | #{files.map{ |f| e_sh(f) }.join(' ')} 2>/dev/console |
| 37 | 45 | } |
| 38 | | |
| 39 | | raise CommitCanceledException if $? != 0 |
| 40 | | |
| 46 | canceled = ($? != 0) |
| 41 | 47 | res = Shellwords.shellwords(res) |
| 42 | 48 | msg = res[1] |
| 43 | 49 | files = res[2..-1] |
| 44 | | return msg, files |
| 50 | return canceled, msg, files |
| 51 | end |
| 52 | |
| 53 | def show_commit_dialog |
| 54 | canceled, msg, files = exec_commit_dialog |
| 55 | raise CommitCanceledException if canceled |
| 56 | [msg, files] |
| 57 | end |
| 58 | |
| 59 | def file_candidates |
| 60 | @file_candidates ||= |
| 61 | git.status(target_file_or_dir).map do |e| |
| 62 | [shorten(e[:path], @base), e[:status][:short]] |
| 63 | end |
| 45 | 64 | end |
| 46 | 65 | |
| 47 | 66 | def run |
| 48 | 67 | raise NotOnBranchException unless ok_to_proceed_with_partial_commit? |
| 49 | | |
| 50 | | files, statuses = [], [] |
| 51 | | git.status(target_file_or_dir).each do |e| |
| 52 | | files << e_sh(shorten(e[:path], @base)) |
| 53 | | statuses << e_sh(e[:status][:short]) |
| 54 | | end |
| 55 | | |
| 56 | | raise NothingToCommitException if files.empty? |
| 68 | raise NothingToCommitException if nothing_to_commit? |
| 57 | 69 | |
| 58 | | msg, files = show_commit_dialog(files, statuses) |
| 70 | msg, files = show_commit_dialog |
| 59 | 71 | |
| 60 | 72 | git.auto_add_rm(files) |
| 61 | 73 | res = git.commit(msg, files, :amend => amend) |
| … | … | |
| 77 | 77 | def title |
| 78 | 78 | "#{title_prefix} in ‘#{htmlize(shorten(target_file_or_dir, ENV['TM_PROJECT_DIRECTORY'] || @base))}’ on branch ‘#{htmlize(git.branch.current_name)}’" |
| 79 | 79 | end |
| 80 | |
| 81 | def nothing_to_commit? |
| 82 | file_candidates.empty? |
| 83 | end |
| 80 | 84 | end |
| 81 | 85 | |
| 82 | 86 | class Normal < Base |
| … | … | |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | 96 | class Amend < Base |
| 97 | COMMIT_MESSAGE_FILENAME = "-- update commit message --" |
| 98 | |
| 97 | 99 | def title_prefix |
| 98 | 100 | "Amending the commit" |
| 99 | 101 | end |
| … | … | |
| 103 | 103 | def amend |
| 104 | 104 | true |
| 105 | 105 | end |
| 106 | |
| 107 | def show_commit_dialog(*args) |
| 108 | msg, files = super(*args) |
| 109 | if files.first==COMMIT_MESSAGE_FILENAME |
| 110 | files.shift |
| 111 | else |
| 112 | msg = "" |
| 113 | end |
| 114 | msg = git.log(:limit => 1).first[:msg] if msg.strip.empty? |
| 115 | |
| 116 | [msg, files] |
| 117 | end |
| 118 | |
| 119 | def file_candidates |
| 120 | return @file_candidates if @file_candidates |
| 121 | super |
| 122 | @file_candidates.unshift([COMMIT_MESSAGE_FILENAME, "M"]) |
| 123 | @file_candidates |
| 124 | end |
| 106 | 125 | end |
| 107 | 126 | end |
| 127 | |
| toggle raw diff |
--- a/Support/lib/partial_commit_worker.rb
+++ b/Support/lib/partial_commit_worker.rb
@@ -1,16 +1,12 @@
+CW = ENV['TM_SUPPORT_PATH'] + '/bin/CommitWindow.app/Contents/MacOS/CommitWindow'
+
module PartialCommitWorker
class NotOnBranchException < Exception; end
class NothingToCommitException < Exception; end
class CommitCanceledException < Exception; end
def self.factory(_type, *args)
- klass = case _type
- when "amend" then
- PartialCommitWorker::Amend
- else
- PartialCommitWorker::Normal
- end
-
+ klass = (_type == "amend" ? PartialCommitWorker::Amend : PartialCommitWorker::Normal)
klass.new(*args)
end
@@ -30,36 +26,48 @@ module PartialCommitWorker
@target_file_or_dir ||= git.paths.first
end
- def show_commit_dialog(files, statuses)
- status_helper_tool = ENV['TM_BUNDLE_SUPPORT'] + '/gateway/commit_dialog_helper.rb'
+ def split_file_statuses
+ [file_candidates.map{ |fc| fc[0] }, file_candidates.map{ |fc| fc[1] }]
+ end
+
+ def status_helper_tool
+ ENV['TM_BUNDLE_SUPPORT'] + '/gateway/commit_dialog_helper.rb'
+ end
+
+ def exec_commit_dialog
+ files, statuses = split_file_statuses
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
+ --status #{statuses.join(':')} \
+ #{files.map{ |f| e_sh(f) }.join(' ')} 2>/dev/console
}
-
- raise CommitCanceledException if $? != 0
-
+ canceled = ($? != 0)
res = Shellwords.shellwords(res)
msg = res[1]
files = res[2..-1]
- return msg, files
+ return canceled, msg, files
+ end
+
+ def show_commit_dialog
+ canceled, msg, files = exec_commit_dialog
+ raise CommitCanceledException if canceled
+ [msg, files]
+ end
+
+ def file_candidates
+ @file_candidates ||=
+ git.status(target_file_or_dir).map do |e|
+ [shorten(e[:path], @base), e[:status][:short]]
+ end
end
def run
raise NotOnBranchException unless ok_to_proceed_with_partial_commit?
-
- 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
-
- raise NothingToCommitException if files.empty?
+ raise NothingToCommitException if nothing_to_commit?
- msg, files = show_commit_dialog(files, statuses)
+ msg, files = show_commit_dialog
git.auto_add_rm(files)
res = git.commit(msg, files, :amend => amend)
@@ -69,6 +77,10 @@ module PartialCommitWorker
def title
"#{title_prefix} in ‘#{htmlize(shorten(target_file_or_dir, ENV['TM_PROJECT_DIRECTORY'] || @base))}’ on branch ‘#{htmlize(git.branch.current_name)}’"
end
+
+ def nothing_to_commit?
+ file_candidates.empty?
+ end
end
class Normal < Base
@@ -82,6 +94,8 @@ module PartialCommitWorker
end
class Amend < Base
+ COMMIT_MESSAGE_FILENAME = "-- update commit message --"
+
def title_prefix
"Amending the commit"
end
@@ -89,5 +103,25 @@ module PartialCommitWorker
def amend
true
end
+
+ def show_commit_dialog(*args)
+ msg, files = super(*args)
+ if files.first==COMMIT_MESSAGE_FILENAME
+ files.shift
+ else
+ msg = ""
+ end
+ msg = git.log(:limit => 1).first[:msg] if msg.strip.empty?
+
+ [msg, files]
+ end
+
+ def file_candidates
+ return @file_candidates if @file_candidates
+ super
+ @file_candidates.unshift([COMMIT_MESSAGE_FILENAME, "M"])
+ @file_candidates
+ end
end
end
+
\ No newline at end of file |
| |   |
| 2 | 2 | |
| 3 | 3 | describe Git do |
| 4 | 4 | before(:each) do |
| 5 | | @log = Git.new |
| 5 | @git = Git.new |
| 6 | 6 | end |
| 7 | 7 | include SpecHelpers |
| 8 | 8 | |
| 9 | 9 | describe "when parsing a plain log" do |
| 10 | 10 | before(:each) do |
| 11 | | @entries = @log.parse_log( fixture_file('log.txt')) |
| 11 | @entries = @git.parse_log( fixture_file('log.txt')) |
| 12 | 12 | end |
| 13 | 13 | |
| 14 | 14 | it "should parse out all items" do |
| … | … | |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | 28 | it "should stringify results" do |
| 29 | | @log.stringify(@entries) |
| 29 | @git.stringify(@entries) |
| 30 | 30 | @entries.first.keys.sort.should == ["author", "date", "msg", "rev"] |
| 31 | 31 | end |
| 32 | 32 | end |
| 33 | 33 | |
| 34 | 34 | describe "when parsing a log with diffs" do |
| 35 | 35 | before(:each) do |
| 36 | | @entries = @log.parse_log( fixture_file("log_with_diffs.txt")) |
| 36 | @entries = @git.parse_log( fixture_file("log_with_diffs.txt")) |
| 37 | 37 | @entry = @entries.first |
| 38 | | |
| 39 | 38 | end |
| 40 | 39 | |
| 41 | 40 | it "should extract and parse diffs" do |
| toggle raw diff |
--- a/Support/spec/lib/commands/log_spec.rb
+++ b/Support/spec/lib/commands/log_spec.rb
@@ -2,13 +2,13 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe Git do
before(:each) do
- @log = Git.new
+ @git = Git.new
end
include SpecHelpers
describe "when parsing a plain log" do
before(:each) do
- @entries = @log.parse_log( fixture_file('log.txt'))
+ @entries = @git.parse_log( fixture_file('log.txt'))
end
it "should parse out all items" do
@@ -26,16 +26,15 @@ made more failproof}
end
it "should stringify results" do
- @log.stringify(@entries)
+ @git.stringify(@entries)
@entries.first.keys.sort.should == ["author", "date", "msg", "rev"]
end
end
describe "when parsing a log with diffs" do
before(:each) do
- @entries = @log.parse_log( fixture_file("log_with_diffs.txt"))
+ @entries = @git.parse_log( fixture_file("log_with_diffs.txt"))
@entry = @entries.first
-
end
it "should extract and parse diffs" do |