Class: UpstreamWatchr::GitRemotes
- Inherits:
-
Object
- Object
- UpstreamWatchr::GitRemotes
- Defined in:
- lib/UpstreamWatchr/gitremotes.rb
Instance Method Summary collapse
- #ahead_behind(branch = nil) ⇒ Object
- #clone_path ⇒ Object
- #diff(branch = nil) ⇒ Object
- #has_changes? ⇒ Boolean
-
#initialize(origin, upstream, main_branch = 'master') ⇒ GitRemotes
constructor
A new instance of GitRemotes.
- #origin_tree_size(branch = nil) ⇒ Object
- #push_to_my_fork(my_fork_url, branch = nil) ⇒ Object
- #ssh_agent ⇒ Object
- #to_s(branch = nil) ⇒ Object
- #tree_size_diff(branch = nil) ⇒ Object
- #trees(branch = nil) ⇒ Object
- #upstream_ahead ⇒ Object
- #upstream_ahead? ⇒ Boolean
- #upstream_behind ⇒ Object
- #upstream_behind? ⇒ Boolean
- #upstream_tree_size(branch = nil) ⇒ Object
Constructor Details
#initialize(origin, upstream, main_branch = 'master') ⇒ GitRemotes
Returns a new instance of GitRemotes.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 7 def initialize(origin, upstream, main_branch = 'master') @origin = origin @upstream = upstream @main_branch = main_branch begin @repo = Rugged::Repository.clone_at(@origin, clone_path, {:bare => true, :credentials => ssh_agent}) puts "Debug: cloned #{@origin} to #{clone_path}" if ENV['DEBUG'] rescue Rugged::InvalidError puts "Debug: didn't clone (#{@origin} to #{clone_path}), local repo already there." if ENV['DEBUG'] @repo = Rugged::Repository.new(clone_path) @repo.remotes['origin'].fetch({:credentials => ssh_agent}) @repo.remotes['origin'].save puts "Debug: fetched 'origin' from #{origin}" if ENV['DEBUG'] end @repo.remotes.create('upstream', @upstream) unless @repo.remotes['upstream'] fail "Can not read from upstream. Something seems off with remote #{@upstream}" unless @repo.remotes['upstream'].check_connection(:fetch, {:credentials => ssh_agent}) @repo.remotes['upstream'].fetch({:credentials => ssh_agent}) @repo.remotes['upstream'].save puts "Debug: fetched 'upstream' from #{@upstream}" if ENV['DEBUG'] end |
Instance Method Details
#ahead_behind(branch = nil) ⇒ Object
55 56 57 58 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 55 def ahead_behind(branch = nil) branch ||= @main_branch @repo.ahead_behind(@repo.references["refs/remotes/origin/#{branch}"].target, @repo.references["refs/remotes/upstream/#{branch}"].target) end |
#clone_path ⇒ Object
34 35 36 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 34 def clone_path File.join(Dir.tmpdir, @origin.split('/').last) end |
#diff(branch = nil) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 46 def diff(branch = nil) branch ||= @main_branch origin_target = @repo.references["refs/remotes/origin/#{branch}"].target puts "Debug: origin has its #{branch} at #{origin_target}" if ENV['DEBUG'] upstream_target = @repo.references["refs/remotes/upstream/#{branch}"].target puts "Debug: upstream has its #{branch} at #{upstream_target}" if ENV['DEBUG'] @repo.diff(origin_target, upstream_target) end |
#has_changes? ⇒ Boolean
96 97 98 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 96 def has_changes? ahead_behind != [0,0] end |
#origin_tree_size(branch = nil) ⇒ Object
81 82 83 84 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 81 def origin_tree_size(branch = nil) branch ||= @main_branch trees(branch)[0].count end |
#push_to_my_fork(my_fork_url, branch = nil) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 38 def push_to_my_fork(my_fork_url, branch = nil) branch ||= @main_branch @repo.remotes.create('my_fork', my_fork_url) unless @repo.remotes['my_fork'] @repo.reset("upstream/#{branch}", :soft) @repo.remotes["my_fork"].push("refs/heads/#{branch}", {:credentials => ssh_agent}) end |
#ssh_agent ⇒ Object
30 31 32 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 30 def ssh_agent lambda { |_, username, _| Rugged::Credentials::SshKeyFromAgent.new(username: username)} end |
#to_s(branch = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 100 def to_s(branch = nil) branch ||= @main_branch a_b = ahead_behind(branch) if a_b == [0,0] "origin/#{branch} is up-to-date with 'upstream/#{branch}'" else "upstream/#{branch} is #{upstream_ahead} ahead and #{upstream_behind} behind 'origin/#{branch}'" end end |
#tree_size_diff(branch = nil) ⇒ Object
91 92 93 94 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 91 def tree_size_diff(branch = nil) branch ||= @main_branch upstream_tree_size(branch) - origin_tree_size(branch) end |
#trees(branch = nil) ⇒ Object
76 77 78 79 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 76 def trees(branch = nil) branch ||= @main_branch [@repo.references["refs/remotes/origin/#{branch}"].target.tree, @repo.references["refs/remotes/upstream/#{branch}"].target.tree] end |
#upstream_ahead ⇒ Object
60 61 62 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 60 def upstream_ahead ahead_behind[1] end |
#upstream_ahead? ⇒ Boolean
64 65 66 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 64 def upstream_ahead? ahead_behind[1] > 0 end |
#upstream_behind ⇒ Object
68 69 70 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 68 def upstream_behind ahead_behind[0] end |
#upstream_behind? ⇒ Boolean
72 73 74 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 72 def upstream_behind? ahead_behind[0] > 0 end |
#upstream_tree_size(branch = nil) ⇒ Object
86 87 88 89 |
# File 'lib/UpstreamWatchr/gitremotes.rb', line 86 def upstream_tree_size(branch = nil) branch ||= @main_branch trees(branch)[1].count end |