From a4fa110279ea28873fe1e4df10c89ddc591046b4 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sun, 11 Nov 2007 10:43:27 -0800 Subject: added the commit(), changed base.commit, base.tree, base.blob to gcommit, gtree, gblob --- lib/git/base.rb | 15 ++++++++++++--- lib/git/lib.rb | 21 ++++++++++++++++----- lib/git/status.rb | 23 +++++++++++++++++------ 3 files changed, 45 insertions(+), 14 deletions(-) (limited to 'lib/git') diff --git a/lib/git/base.rb b/lib/git/base.rb index eded12d..9be9fec 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -107,9 +107,9 @@ module Git def object(objectish) Git::Object.new(self, objectish) end - alias_method :tree, :object - alias_method :commit, :object - alias_method :blob, :object + alias_method :gtree, :object + alias_method :gcommit, :object + alias_method :gblob, :object def log(count = 30) @@ -140,6 +140,15 @@ module Git def add(path = '.') self.lib.add(path) end + + def commit(message, opts = {}) + self.lib.commit(message, opts) + end + + def commit_all(message, opts = {}) + opts = {:add_all => true}.merge(opts) + self.lib.commit(message, opts) + end # convenience methods diff --git a/lib/git/lib.rb b/lib/git/lib.rb index ec65e55..d0dda8b 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -150,7 +150,7 @@ module Git command_lines('diff-files').each do |line| (info, file) = line.split("\t") (mode_src, mode_dest, sha_src, sha_dest, type) = info.split - hsh[file] = {:path => file, :mode_file => mode_src, :mode_index => mode_dest, + hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest, :sha_file => sha_src, :sha_index => sha_dest, :type => type} end hsh @@ -162,7 +162,7 @@ module Git command_lines('diff-index', treeish).each do |line| (info, file) = line.split("\t") (mode_src, mode_dest, sha_src, sha_dest, type) = info.split - hsh[file] = {:path => file, :mode_repo => mode_src, :mode_index => mode_dest, + hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest, :sha_repo => sha_src, :sha_index => sha_dest, :type => type} end hsh @@ -208,10 +208,16 @@ module Git end def add(path = '.') + path = path.join(' ') if path.is_a?(Array) command('add', path) end - + def commit(message, opts = {}) + arr_opts = ["-m '#{message}'"] + arr_opts << '-a' if opts[:add_all] + command('commit', arr_opts) + end + private def command_lines(cmd, opts = {}) @@ -222,10 +228,15 @@ module Git ENV['GIT_DIR'] = @git_dir if @git_dir ENV['GIT_INDEX_FILE'] = @git_index_file if @git_index_file ENV['GIT_WORK_DIR'] = @git_work_dir if @git_work_dir - Dir.chdir(@git_work_dir || @git_dir || @path) do + path = @git_work_dir || @git_dir || @path + Dir.chdir(path) do opts = opts.to_a.join(' ') - #puts "git #{cmd} #{opts}" out = `git #{cmd} #{opts} 2>&1`.chomp + #puts path + #puts "gd: #{@git_work_dir}" + #puts "gi: #{@git_index_file}" + #puts "pp: #{@path}" + #puts "git #{cmd} #{opts}" #puts out #puts if $?.exitstatus > 1 diff --git a/lib/git/status.rb b/lib/git/status.rb index 24fd98d..f738644 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -27,8 +27,8 @@ module Git out = '' self.each do |file| out << file.path - out << "\n\tsha(r) " + file.sha_repo.to_s - out << "\n\tsha(i) " + file.sha_index.to_s + out << "\n\tsha(r) " + file.sha_repo.to_s + ' ' + file.mode_repo.to_s + out << "\n\tsha(i) " + file.sha_index.to_s + ' ' + file.mode_index.to_s out << "\n\ttype " + file.type.to_s out << "\n\tstage " + file.stage.to_s out << "\n\tuntrac " + file.untracked.to_s @@ -55,7 +55,10 @@ module Git attr_accessor :mode_index, :mode_repo attr_accessor :sha_index, :sha_repo - def initialize(hash) + @base = nil + + def initialize(base, hash) + @base = base @path = hash[:path] @type = hash[:type] @stage = hash[:stage] @@ -66,6 +69,14 @@ module Git @untracked = hash[:untracked] end + def blob(type = :index) + if type == :repo + @base.object(@sha_repo) + else + @base.object(@sha_index) rescue @base.object(@sha_repo) + end + end + end @@ -85,16 +96,16 @@ module Git # find modified in tree @base.lib.diff_files.each do |path, data| - @files[path].merge!(data) + @files[path] ? @files[path].merge!(data) : @files[path] = data end # find added but not committed - new files @base.lib.diff_index('HEAD').each do |path, data| - @files[path].merge!(data) + @files[path] ? @files[path].merge!(data) : @files[path] = data end @files.each do |k, file_hash| - @files[k] = StatusFile.new(file_hash) + @files[k] = StatusFile.new(@base, file_hash) end end -- cgit