Module: ChangeAgent::Sync
- Included in:
- Client
- Defined in:
- lib/change_agent/sync.rb
Defined Under Namespace
Classes: MergeConflict, MissingRemote
Constant Summary collapse
- DEFAULT_REMOTE =
'origin'- DEFAULT_REMOTE_BRANCH =
'origin/master'- DEFAULT_LOCAL_REF =
'refs/heads/master'
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Default to token-based credentials passed as GITHUB_TOKEN Can be over ridden by overwritting @credentials with a different Rugged Credentialing method.
Instance Method Summary collapse
-
#add_remote(name, url) ⇒ Object
Helper method to simplify adding a remote.
-
#fetch(remote = nil) ⇒ Object
Fetch a remote.
-
#has_remotes? ⇒ Boolean
Does the current repo have at least a single remote?.
-
#merge(options = {}) ⇒ Object
Merge two refs.
-
#pull(options = {}) ⇒ Object
Fetch a remote and merge.
-
#push(options = {}) ⇒ Object
Push to a remote.
-
#remotes ⇒ Object
Helper method to return all remots.
-
#sync ⇒ Object
Perform both a pull and a push.
Instance Attribute Details
#credentials ⇒ Object
Default to token-based credentials passed as GITHUB_TOKEN Can be over ridden by overwritting @credentials with a different Rugged Credentialing method
17 18 19 20 21 22 |
# File 'lib/change_agent/sync.rb', line 17 def credentials @credentials ||= Rugged::Credentials::UserPassword.new({ username: 'x-oauth-basic', password: ENV.fetch('GITHUB_TOKEN', nil) }) end |
Instance Method Details
#add_remote(name, url) ⇒ Object
Helper method to simplify adding a remote
30 31 32 |
# File 'lib/change_agent/sync.rb', line 30 def add_remote(name, url) remotes.create name, url end |
#fetch(remote = nil) ⇒ Object
Fetch a remote
Options:
remote - the name of the remote (default: origin)
56 57 58 59 60 |
# File 'lib/change_agent/sync.rb', line 56 def fetch(remote = nil) raise MissingRemote unless has_remotes? repo.fetch(remote || DEFAULT_REMOTE, credentials: credentials) end |
#has_remotes? ⇒ Boolean
Does the current repo have at least a single remote?
35 36 37 |
# File 'lib/change_agent/sync.rb', line 35 def has_remotes? remotes.count.positive? end |
#merge(options = {}) ⇒ Object
Merge two refs
Options:
:from - the remote ref (default: "origin/master")
:to - the local ref (default: "refs/heads/master")
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/change_agent/sync.rb', line 67 def merge( = {}) [:from] = DEFAULT_REMOTE_BRANCH [:to] = DEFAULT_LOCAL_REF theirs = repo.rev_parse [:from] ours = repo.rev_parse [:to] analysis = repo.merge_analysis(theirs) return analysis if analysis.include? :up_to_date base = repo.rev_parse(repo.merge_base(ours, theirs)) index = ours.tree.merge(theirs.tree, base.tree) raise MergeConflict if index.conflicts? Rugged::Commit.create(repo, { parents: [ours, theirs], tree: index.write_tree(repo), message: "Merged `#{[:from]}` into `#{[:to].sub('refs/heads/', '')}`", update_ref: [:to] }) end |
#pull(options = {}) ⇒ Object
Fetch a remote and merge
Options:
:remote - the name of the remote (default: origin)
:from - the remote ref (default: "origin/master")
:to - the local ref (default: "refs/heads/master")
95 96 97 98 |
# File 'lib/change_agent/sync.rb', line 95 def pull( = {}) fetch([:remote]) merge() end |
#push(options = {}) ⇒ Object
Push to a remote
Options:
:remote - the name of the remote (default: origin)
:ref - the ref to push (default: "refs/heads/master")
44 45 46 47 48 49 50 |
# File 'lib/change_agent/sync.rb', line 44 def push( = {}) raise MissingRemote unless has_remotes? [:remote] = DEFAULT_REMOTE [:ref] = DEFAULT_LOCAL_REF remotes[[:remote]].push([[:ref]], { credentials: credentials }) end |
#remotes ⇒ Object
Helper method to return all remots
25 26 27 |
# File 'lib/change_agent/sync.rb', line 25 def remotes repo.remotes end |
#sync ⇒ Object
Perform both a pull and a push
Will fail if any conflicts occur
103 104 105 |
# File 'lib/change_agent/sync.rb', line 103 def sync pull && push end |