Class: Amp::Core::Repositories::Git::LocalRepository

Inherits:
AbstractLocalRepository
  • Object
show all
Defined in:
lib/amp-git/repositories/local_repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "", create = false, config = nil) ⇒ LocalRepository

Returns a new instance of LocalRepository.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amp-git/repositories/local_repository.rb', line 28

def initialize(path="", create=false, config=nil)
  super(path, create, config)
  
  @config = config
  
  @file_opener = Support::RootedOpener.new @root # This will open relative to the repo root
  @file_opener.default = :open_file    # these two are the same, pretty much
  @git_opener  = Support::RootedOpener.new @root # this will open relative to root/.git
  @git_opener.default  = :open_git     # just with different defaults
  
  @staging_area = Amp::Core::Repositories::Git::StagingArea.new self
  
  if create
    init
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



23
24
25
# File 'lib/amp-git/repositories/local_repository.rb', line 23

def config
  @config
end

#file_openerObject

Returns the value of attribute file_opener.



24
25
26
# File 'lib/amp-git/repositories/local_repository.rb', line 24

def file_opener
  @file_opener
end

#git_openerObject

Returns the value of attribute git_opener.



25
26
27
# File 'lib/amp-git/repositories/local_repository.rb', line 25

def git_opener
  @git_opener
end

#rootObject

Returns the value of attribute root.



22
23
24
# File 'lib/amp-git/repositories/local_repository.rb', line 22

def root
  @root
end

#staging_areaObject

Returns the value of attribute staging_area.



26
27
28
# File 'lib/amp-git/repositories/local_repository.rb', line 26

def staging_area
  @staging_area
end

Instance Method Details

#[](rev) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/amp-git/repositories/local_repository.rb', line 82

def [](rev)
  case rev
  when String
    Amp::Git::Changeset.new self, rev
  when nil
    Amp::Git::WorkingDirectoryChangeset.new self
  when 'tip', :tip
    Amp::Git::Changeset.new self, parents[0]
  when Integer
    revs = `git log --pretty=oneline 2> /dev/null`.split("\n")
    short_name = revs[revs.size - 1 - rev].split(' ').first
    Amp::Git::Changeset.new self, short_name
  end
end

#add_all_filesObject



74
75
76
# File 'lib/amp-git/repositories/local_repository.rb', line 74

def add_all_files
  staging_area.add status[:modified]
end

#commit(opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/amp-git/repositories/local_repository.rb', line 64

def commit(opts={})
  add_all_files
  string = "git commit #{opts[:user] ? "--author #{opts[:user].inspect}" : "" }" +
           " #{opts[:empty_ok] ? "--allow-empty" : "" }" +
           " #{opts[:message] ? "-m #{opts[:message].inspect}" : "" } 2> /dev/null"
  string.strip!
  
  system string
end

#default_branch_nameString

Regarding branch support.

For each repository format, you begin in a default branch. Each repo format, of course, starts with a different default branch. Git’s is “master”.

Returns:

  • (String)

    the default branch name



60
61
62
# File 'lib/amp-git/repositories/local_repository.rb', line 60

def default_branch_name
  "master"
end

#file_modified?(file, opts = {}) ⇒ Boolean

Determines if a file has been modified from :node1 to :node2.

Returns:

  • (Boolean)

    has it been modified



117
118
119
# File 'lib/amp-git/repositories/local_repository.rb', line 117

def file_modified?(file, opts={})
  file_status(file, opts) == :included
end

#file_status(filename, opts = {}) ⇒ Object

Returns a Symbol. Possible results: :added (subset of :included) :removed :untracked :included (aka :modified) :normal

If you call localrepo#status from this method… well… I DARE YOU!



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/amp-git/repositories/local_repository.rb', line 132

def file_status(filename, opts={})
  parse_status! opts
  inverted = @status.inject({}) do |h, (k, v)|
    v.each {|v_| h[v_] = k }
    h
  end
  
  # Now convert it so it uses the same jargon
  # we REALLY need to get the dirstate and localrepo on
  # the same page here.
  case inverted[filename]
  when :modified
    :included
  when :added
    :added
  when :removed
    :removed
  when :unknown
    :untracked
  else
    :normal
  end
    
end

#forget(*files) ⇒ Object



78
79
80
# File 'lib/amp-git/repositories/local_repository.rb', line 78

def forget(*files)
  staging_area.forget *files
end

#init(config = @config) ⇒ Object



45
46
47
48
49
50
# File 'lib/amp-git/repositories/local_repository.rb', line 45

def init(config=@config)
  super(config)
  
  `cd #{@root} && git init 2> /dev/null`
  true
end

#parentsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/amp-git/repositories/local_repository.rb', line 174

def parents
  first = `git log -1 HEAD 2> /dev/null`
  dad   = first[/^commit (.+)$/, 1]
  dad   = dad ? dad[0..6] : nil
  mom   = nil
  
  if first =~ /Merge: (.+)\.\.\. (.+)\.\.\.$/ # Merge: 1c002dd... 35cfb2b...
    dad = $1 # just have them both use the short name, nbd
    mom = $2
  end
  
  [dad, mom]
end

#parse_status!(opts = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/amp-git/repositories/local_repository.rb', line 157

def parse_status!(opts={})
  return if @parsed
  
  data    = `git status #{opts[:node1]}..#{opts[:node2]}  2> /dev/null`.split("\n")
  @status = data.inject({}) do |h, line| # yeah i know stfu
    case line
    when /^#\s+(\w+):\s(.+)$/
      h[$1.to_sym] = $2; h
    when /^#\s+([^ ]+)$/
      h[:unknown] = $1; h
    else
      h
    end
  end
  @parsed = true
end

#sizeObject



97
98
99
# File 'lib/amp-git/repositories/local_repository.rb', line 97

def size
  `git log --pretty=oneline 2> /dev/null`.split("\n").size
end

#working_write(filename, text) ⇒ Object

Write text to filename, where filename is local to the root.

Parameters:

  • filename (String)

    The file as relative to the root

  • text (String)

    The text to write to said file



107
108
109
110
111
# File 'lib/amp-git/repositories/local_repository.rb', line 107

def working_write(filename, text)
  file_opener.open filename, 'w' do |f|
    f.write text
  end
end