summaryrefslogtreecommitdiffstats
path: root/lib/git/status.rb
diff options
context:
space:
mode:
authorscott Chacon <schacon@agadorsparticus.(none)>2007-11-11 10:04:26 -0800
committerscott Chacon <schacon@agadorsparticus.(none)>2007-11-11 10:04:26 -0800
commita1237671ba3ec38f5b123ee6600e4352dc03196b (patch)
tree71bc13cb36799e778237097b343a4d4fa9189b3f /lib/git/status.rb
parentefcce7fc123b9e64fb9d93224e4e78d09144af3d (diff)
downloadthird_party-ruby-git-a1237671ba3ec38f5b123ee6600e4352dc03196b.zip
third_party-ruby-git-a1237671ba3ec38f5b123ee6600e4352dc03196b.tar.gz
third_party-ruby-git-a1237671ba3ec38f5b123ee6600e4352dc03196b.tar.xz
git add working, git status object working
Diffstat (limited to 'lib/git/status.rb')
-rw-r--r--lib/git/status.rb103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/git/status.rb b/lib/git/status.rb
new file mode 100644
index 0000000..24fd98d
--- /dev/null
+++ b/lib/git/status.rb
@@ -0,0 +1,103 @@
+module Git
+
+ class Status
+ include Enumerable
+
+ @base = nil
+ @files = nil
+
+ def initialize(base)
+ @base = base
+ construct_status
+ end
+
+ def changed
+ @files.select { |k, f| f.type == 'M' }
+ end
+
+ def added
+ @files.select { |k, f| f.type == 'A' }
+ end
+
+ def untracked
+ @files.select { |k, f| f.untracked }
+ end
+
+ def pretty
+ 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\ttype " + file.type.to_s
+ out << "\n\tstage " + file.stage.to_s
+ out << "\n\tuntrac " + file.untracked.to_s
+ out << "\n"
+ end
+ out << "\n"
+ out
+ end
+
+ # enumerable method
+
+ def [](file)
+ @files[file]
+ end
+
+ def each
+ @files.each do |k, file|
+ yield file
+ end
+ end
+
+ class StatusFile
+ attr_accessor :path, :type, :stage, :untracked
+ attr_accessor :mode_index, :mode_repo
+ attr_accessor :sha_index, :sha_repo
+
+ def initialize(hash)
+ @path = hash[:path]
+ @type = hash[:type]
+ @stage = hash[:stage]
+ @mode_index = hash[:mode_index]
+ @mode_repo = hash[:mode_repo]
+ @sha_index = hash[:sha_index]
+ @sha_repo = hash[:sha_repo]
+ @untracked = hash[:untracked]
+ end
+
+
+ end
+
+ private
+
+ def construct_status
+ @files = @base.lib.ls_files
+
+ # find untracked in working dir
+ Dir.chdir(@base.dir.path) do
+ Dir.glob('**/*') do |file|
+ if !@files[file]
+ @files[file] = {:path => file, :untracked => true} if !File.directory?(file)
+ end
+ end
+ end
+
+ # find modified in tree
+ @base.lib.diff_files.each do |path, data|
+ @files[path].merge!(data)
+ end
+
+ # find added but not committed - new files
+ @base.lib.diff_index('HEAD').each do |path, data|
+ @files[path].merge!(data)
+ end
+
+ @files.each do |k, file_hash|
+ @files[k] = StatusFile.new(file_hash)
+ end
+ end
+
+ end
+
+end \ No newline at end of file