0
@@ -3,13 +3,14 @@ module GitRuby
0
class GitRubyIndexError < StandardError
0
+ # this file implements my imagination of what the index file does.
0
+ # it accomplishes the same basic tasks, but uses it's own index.gitr file
0
+ # rather than the actual git index file because I don't have the strength to
0
+ # reverse engineer that particular format quite yet. Perhaps one day.
0
attr_accessor :base, :path, :ref, :last_commit, :files
0
- # for now, this is done entirely in memory - not writing out a file
0
- # so until I can duplicate the git index format, path will be ignored
0
- # for the simple stuff i need to do, pretending we have an index file should be fine
0
def initialize(base, path, check_path = false)
0
# read in the current HEAD
0
if File.extname(path) != '.gitr'
0
@@ -27,6 +28,24 @@ module GitRuby
0
+ scan_working_directory
0
+ outs << ['mode', 'repo ', 'index ', 'working ', 'path'].join("\t")
0
+ @files.sort.each do |f, hsh|
0
+ outs << [hsh[:mode_index], short_sha(hsh[:sha_repo]), short_sha(hsh[:sha_index]), short_sha(hsh[:sha_working]), hsh[:path]].join("\t")
0
FileUtils.cd(@base.repo.path) do
0
@@ -45,6 +64,117 @@ module GitRuby
0
+ # scans the current working directory to calculate the shas of files that
0
+ # have been changed since the last scan
0
+ # !! TODO : find removed files !!
0
+ def scan_working_directory
0
+ Dir.chdir(@base.dir.path) do
0
+ Dir.glob('**/*') do |file|
0
+ file = File.join('.', file) if file[0, 1] != '.'
0
+ mode = sprintf("%o", s.mode)
0
+ mtimesize = s.mtime.to_i.to_s + s.size.to_s
0
+ if(@files[file][:working_mtime_size] != mtimesize)
0
+ sha = Raw::Internal::LooseStorage.calculate_sha(File.read(file), 'blob')
0
+ @files[file][:sha_working] = sha
0
+ @files[file][:mode_working] = mode
0
+ @files[file][:working_mtime_size] = mtimesize
0
+ sha = Raw::Internal::LooseStorage.calculate_sha(File.read(file), 'blob')
0
+ @files[file] = {:path => file, :file => File.basename(file), :type => 'blob',
0
+ :sha_working => sha, :mode_working => mode,
0
+ :working_mtime_size => mtimesize}
0
+ # are there any files that are in the index not in the repo
0
+ # or modified in the working directory that are not in the index
0
+ scan_working_directory
0
+ @files.each do |f, hsh|
0
+ if hsh[:type] == 'blob' && hsh[:sha_index]
0
+ clean &&= ((hsh[:sha_working] == hsh[:sha_index]) && (hsh[:sha_index] == hsh[:sha_repo]))
0
+ # make working directory match the index
0
+ # (takes a ref : ref/heads/master, ref/remote/origin/master)
0
+ # - make sure the current index is clean
0
+ # - resolve the new sha and read it into a parallel struc
0
+ # -- remove wd files in old not in new
0
+ # -- add index files in new not in old
0
+ # -- overwrite wd files different in indexes
0
+ # - update the HEAD to the new branch ref if 'ref/heads', else set to new sha
0
+ @old_files = @files.dup
0
+ @old_last_commit = @last_commit.dup
0
+ Dir.chdir(@base.repo.path) do
0
+ @last_commit = File.read(@ref).chomp
0
+ read_commit(@last_commit)
0
+ Dir.chdir(@base.dir.path) do
0
+ # -- remove wd files in old not in new
0
+ @old_files.each do |f, hsh|
0
+ if(!@files[f] || !@files[f][:sha_index])
0
+ File.unlink(f) if File.file?(f)
0
+ Dir.rmdir(File.dirname(f)) rescue nil
0
+ # -- add index files in new not in old
0
+ # -- overwrite wd files different in indexes
0
+ @files.each do |f, hsh|
0
+ if(!@old_files[f] || (@old_files[f][:sha_index] != hsh[:sha_index]))
0
+ FileUtils.mkdir_p(File.dirname(f))
0
+ @base.lib.write_file(f, @base.gblob(hsh[:sha_index]).contents)
0
+ end # end work in the working dir
0
+ ref = @base.lib.write_file('HEAD', "ref: #{ref}")
0
+ puts 'branch does not exist'
0
+ @last_commit = @old_last_commit
0
+ puts 'not clean index'
0
# read in the commit so we know what in the working directory differs
0
def read_commit(commit_sha)
0
# find the tree sha and read the tree
0
@@ -90,21 +220,25 @@ module GitRuby
0
# update an existing file
0
- @files[file][:sha_working] = sha
0
- @files[file][:mode_working] = mode
0
@files[file][:sha_index] = sha
0
@files[file][:mode_index] = mode
0
- @files[file] = {:path => file, :file => File.basename(file), :type => 'blob',
0
- :sha_working => sha, :sha_index => sha,
0
- :mode_working => mode, :mode_index => mode}
0
+ @files[file] = {:path => file,
0
+ :file => File.basename(file),
0
+ puts 'no staged files'
0
# find all the modified files
0
@@ -159,6 +293,7 @@ module GitRuby
0
+ scan_working_directory
Comments
No one has commented yet.