Class: Amp::Core::Repositories::Git::LocalRepository
- Inherits:
-
AbstractLocalRepository
- Object
- AbstractLocalRepository
- Amp::Core::Repositories::Git::LocalRepository
- Defined in:
- lib/amp-git/repositories/local_repository.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#file_opener ⇒ Object
Returns the value of attribute file_opener.
-
#git_opener ⇒ Object
Returns the value of attribute git_opener.
-
#root ⇒ Object
Returns the value of attribute root.
-
#staging_area ⇒ Object
Returns the value of attribute staging_area.
Instance Method Summary collapse
- #[](rev) ⇒ Object
- #add_all_files ⇒ Object
- #commit(opts = {}) ⇒ Object
-
#default_branch_name ⇒ String
Regarding branch support.
-
#file_modified?(file, opts = {}) ⇒ Boolean
Determines if a file has been modified from :node1 to :node2.
-
#file_status(filename, opts = {}) ⇒ Object
Returns a Symbol.
- #forget(*files) ⇒ Object
- #init(config = @config) ⇒ Object
-
#initialize(path = "", create = false, config = nil) ⇒ LocalRepository
constructor
A new instance of LocalRepository.
- #parents ⇒ Object
- #parse_status!(opts = {}) ⇒ Object
- #size ⇒ Object
-
#working_write(filename, text) ⇒ Object
Write
text
tofilename
, wherefilename
is local to the root.
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
#config ⇒ Object
Returns the value of attribute config.
23 24 25 |
# File 'lib/amp-git/repositories/local_repository.rb', line 23 def config @config end |
#file_opener ⇒ Object
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_opener ⇒ Object
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 |
#root ⇒ Object
Returns the value of attribute root.
22 23 24 |
# File 'lib/amp-git/repositories/local_repository.rb', line 22 def root @root end |
#staging_area ⇒ Object
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_files ⇒ Object
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_name ⇒ String
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”.
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.
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 |
#parents ⇒ Object
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 |
#size ⇒ Object
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.
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 |