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

Inherits:
AbstractLocalRepository show all
Defined in:
lib/amp/repository/git/repositories/local_repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractLocalRepository

#get_file, #mark_conflicted, #mark_resolved, #pull, #push, #try_resolve_conflict, #uncommitted_merge_files

Methods included from CommonLocalRepoMethods

#add, #each, #fix_files, #relative_join, #remove, #run_hook, #status, #walk, #working_join

Constructor Details

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

Returns a new instance of LocalRepository.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 13

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 8

def config
  @config
end

#file_openerObject

Returns the value of attribute file_opener.



9
10
11
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 9

def file_opener
  @file_opener
end

#git_openerObject

Returns the value of attribute git_opener.



10
11
12
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 10

def git_opener
  @git_opener
end

#rootObject

Returns the value of attribute root.



7
8
9
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 7

def root
  @root
end

#staging_areaObject

Returns the value of attribute staging_area.



11
12
13
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 11

def staging_area
  @staging_area
end

Instance Method Details

#[](rev) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 55

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



47
48
49
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 47

def add_all_files
  staging_area.add status[:modified]
end

#commit(opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 37

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

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

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

Returns:

  • (Boolean)

    has it been modified



90
91
92
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 90

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!



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 105

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



51
52
53
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 51

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

#init(config = @config) ⇒ Object



30
31
32
33
34
35
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 30

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

#parentsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 147

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 130

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



70
71
72
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 70

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



80
81
82
83
84
# File 'lib/amp/repository/git/repositories/local_repository.rb', line 80

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