Class: Fixi::Command::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/fixi/command/commit.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arghelpObject



10
11
12
# File 'lib/fixi/command/commit.rb', line 10

def self.arghelp
  "[<dir>|<file>]"
end

.detailsObject



14
15
16
# File 'lib/fixi/command/commit.rb', line 14

def self.details
  "If no argument is given, the current directory ('.') is assumed."
end

.synopsisObject



6
7
8
# File 'lib/fixi/command/commit.rb', line 6

def self.synopsis
  "Commit modified files to the index"
end

Instance Method Details

#execute(args) ⇒ Object



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
# File 'lib/fixi/command/commit.rb', line 18

def execute args
  opts = Trollop::options args do
    banner Fixi::Command.banner "commit"
    opt :absolute, "Show absolute paths. By default, paths are reported
      relative to the index root.".pack
    opt :dry_run, "Don't do anything; just report what would be done"
    opt :shallow, "Do shallow comparisons when determining which files have
      changed. If specified, only file sizes and mtimes will be used. By 
      default, checksums will also be computed and compared if necessary.".pack
    opt :verbose, "For modified files, show which attribute changed.
      By default, only the path is shown.".pack
  end
  path = File.expand_path(args[0] || ".")
  index = Fixi::Index.new(path)

  index.each(args[0]) do |hash|
    relpath = hash['relpath']
    abspath = index.rootpath + '/' + relpath
    if index.file_in_scope(relpath) && File.exists?(abspath)
      size = File.size(abspath)
      mtime = File.mtime(abspath).to_i
      if size != hash['size']
        detail = opts[:verbose] ? "size=#{size} " : ""
        puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
        index.update(relpath) unless opts[:dry_run]
      elsif mtime != hash['mtime']
        detail = opts[:verbose] ? "mtime=#{Time.at(mtime).utc.iso8601} " : ""
        puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
        index.update(relpath) unless opts[:dry_run]
      elsif not opts[:shallow]
        hexdigests = Fixi::hexdigests(Fixi::digests(index.algorithms), abspath)
        i = 0
        need_update = false
        index.algorithms.split(',').each do |algorithm|
          if not(need_update) && (hexdigests[i] != hash[algorithm])
            need_update = true
            detail = opts[:verbose] ? "#{algorithm}=#{hexdigests[i]} " : ""
            puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
          end
          hash[algorithm] = hexdigests[i]
          i += 1
        end
        index.update(relpath, hash) if need_update && not(opts[:dry_run])
      end
    end
  end
end