Class: Piston::Git::WorkingCopy

Inherits:
WorkingCopy show all
Defined in:
lib/piston/git/working_copy.rb

Instance Attribute Summary

Attributes inherited from WorkingCopy

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WorkingCopy

add_handler, #copy_from, #copy_to, #diff, guess, handlers, #import, #info, #initialize, #logger, logger, #pistonized?, #recall, #remember, #remotely_modified, #repository, #temp_dir_name, #to_s, #update, #validate!

Constructor Details

This class inherits a constructor from Piston::WorkingCopy

Class Method Details

.clientObject



36
37
38
# File 'lib/piston/git/working_copy.rb', line 36

def client
  @@client ||= Piston::Git::Client.instance
end

.git(*args) ⇒ Object



40
41
42
# File 'lib/piston/git/working_copy.rb', line 40

def git(*args)
  client.git(*args)
end

.understands_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/piston/git/working_copy.rb', line 11

def understands_dir?(dir)
  path = dir
  begin
    begin
      logger.debug {"git status on #{path}"}
      Dir.chdir(path) do
        response = git(:status)
        return true if response =~ /# On branch /
      end
    rescue Errno::ENOENT
      # NOP, we assume this is simply because the folder hasn't been created yet
      path = path.parent
      retry unless path.to_s == "/"
      return false
    end
  rescue Piston::Git::Client::BadCommand
    # NOP, as we return false below
  rescue Piston::Git::Client::CommandError
    # This is certainly not a Git repository
    false
  end

  false
end

Instance Method Details

#add(added) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/piston/git/working_copy.rb', line 65

def add(added)
  Dir.chdir(path) do
    added.each do |item|
      target = path + item
      target.mkdir unless target.exist?
      git(:add, "-f", item)
    end
  end
end

#after_remember(path) ⇒ Object



57
58
59
# File 'lib/piston/git/working_copy.rb', line 57

def after_remember(path)
  Dir.chdir(self.path) { git(:add, "--force", path.relative_path_from(self.path)) }
end

#createObject



49
50
51
# File 'lib/piston/git/working_copy.rb', line 49

def create
  path.mkpath rescue nil
end

#delete(deleted) ⇒ Object



75
76
77
78
79
# File 'lib/piston/git/working_copy.rb', line 75

def delete(deleted)
  Dir.chdir(path) do
    deleted.each { |item| git(:rm, "-r", item) }
  end
end

#downgrade_to(revision) ⇒ Object



91
92
93
94
# File 'lib/piston/git/working_copy.rb', line 91

def downgrade_to(revision)
  logger.debug {"Creating a branch to copy changes from remote repository"}
  Dir.chdir(path) { git(:checkout, '-b', "my-#{revision}", revision) }
end

#exclude_for_diffObject



124
125
126
# File 'lib/piston/git/working_copy.rb', line 124

def exclude_for_diff
  Piston::Git::EXCLUDE
end

#exist?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/piston/git/working_copy.rb', line 53

def exist?
  path.directory?
end

#finalizeObject



61
62
63
# File 'lib/piston/git/working_copy.rb', line 61

def finalize
  Dir.chdir(path) { git(:add, "--force", ".") }
end

#git(*args) ⇒ Object



45
46
47
# File 'lib/piston/git/working_copy.rb', line 45

def git(*args)
  self.class.git(*args)
end

#locally_modifiedObject



115
116
117
118
119
120
121
122
# File 'lib/piston/git/working_copy.rb', line 115

def locally_modified
  logger.debug {"Get last changed revision for #{yaml_path}"}
  # get latest commit for .piston.yml
  initial_revision = last_changed_revision(yaml_path)
  logger.debug {"Get last log line for #{path} after #{initial_revision}"}
  # get latest revisions for this working copy since last update
  Dir.chdir(path) { not git(:log, '-n', '1', "#{initial_revision}..", '.').empty? }
end

#merge_local_changes(revision) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/piston/git/working_copy.rb', line 96

def merge_local_changes(revision)
  from_revision = current_revision
  Dir.chdir(path) do
    begin
      logger.debug {"Saving changes in temporary branch"}
      git(:commit, '-a', '-m', 'merging')
      logger.debug {"Return to previous branch"}
      git(:checkout, revision)
      logger.debug {"Merge changes from temporary branch"}
      git(:merge, '--squash', from_revision)
    rescue Piston::Git::Client::CommandError
      git(:checkout, revision)
    ensure
      logger.debug {"Deleting temporary branch"}
      git(:branch, '-D', from_revision)
    end
  end
end

#rename(renamed) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/piston/git/working_copy.rb', line 81

def rename(renamed)
  Dir.chdir(path) do
    renamed.each do |from, to|
      target = path + File.dirname(to)
      target.mkpath unless target.exist?
      git(:mv, from, to)
    end
  end
end

#status(subpath = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/piston/git/working_copy.rb', line 128

def status(subpath=nil)
  Dir.chdir(path) do
    git(:status).split("\n").inject([]) do |memo, line|
      next memo unless line =~ /\s(\w+):\s+(.*)$/
      memo << [$1, $2]
    end
  end
end