2019-07-16 19:47:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Site
|
|
|
|
# Acciones para el repositorio Git de un sitio. Por ahora hacemos un
|
|
|
|
# uso muy básico de Git, con lo que asumimos varias cosas, por ejemplo
|
|
|
|
# que un sitio tiene un solo origen, que siempre se trabaja con la
|
|
|
|
# rama master, etc.
|
|
|
|
class Repository
|
2019-08-09 19:49:25 +00:00
|
|
|
attr_reader :rugged, :changes, :path
|
2019-07-16 19:47:44 +00:00
|
|
|
|
|
|
|
def initialize(path)
|
2019-08-09 19:49:25 +00:00
|
|
|
@path = path
|
2019-07-16 19:47:44 +00:00
|
|
|
@rugged = Rugged::Repository.new(path)
|
|
|
|
@changes = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def remote
|
|
|
|
@remote ||= rugged.remotes.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Trae los cambios del repositorio de origen sin aplicarlos y
|
2019-07-30 20:05:46 +00:00
|
|
|
# devuelve la cantidad de commits pendientes.
|
|
|
|
#
|
|
|
|
# XXX: Prestar atención a la velocidad de respuesta cuando tengamos
|
|
|
|
# repositorios remotos.
|
2019-07-16 19:47:44 +00:00
|
|
|
def fetch
|
|
|
|
if remote.check_connection :fetch
|
|
|
|
@changes = rugged.fetch(remote)[:received_objects]
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Incorpora los cambios en el repositorio actual
|
|
|
|
#
|
2019-08-16 23:12:22 +00:00
|
|
|
def merge(usuarie)
|
|
|
|
merge = rugged.merge_commits(master, origin_master)
|
2019-07-16 19:47:44 +00:00
|
|
|
|
2020-07-18 13:37:46 +00:00
|
|
|
# No hacemos nada si hay conflictos, pero notificarnos
|
|
|
|
begin
|
|
|
|
raise MergeConflictsException if merge.conflicts?
|
|
|
|
rescue MergeConflictsException => e
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { path: path, merge: merge })
|
|
|
|
return # No hacer nada
|
|
|
|
end
|
2019-07-16 19:47:44 +00:00
|
|
|
|
|
|
|
commit = Rugged::Commit
|
2019-08-16 23:23:04 +00:00
|
|
|
.create(rugged, update_ref: 'HEAD',
|
|
|
|
parents: [master, origin_master],
|
|
|
|
tree: merge.write_tree(rugged),
|
|
|
|
message: I18n.t('sites.fetch.merge.message'),
|
|
|
|
author: author(usuarie), committer: committer)
|
2019-07-16 19:47:44 +00:00
|
|
|
|
|
|
|
# Forzamos el checkout para mover el HEAD al último commit y
|
|
|
|
# escribir los cambios
|
|
|
|
rugged.checkout 'HEAD', strategy: :force
|
|
|
|
commit
|
|
|
|
end
|
2019-08-16 23:12:22 +00:00
|
|
|
|
|
|
|
def master
|
|
|
|
rugged.branches['master'].target
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_master
|
|
|
|
rugged.branches['origin/master'].target
|
|
|
|
end
|
2019-07-16 19:47:44 +00:00
|
|
|
|
|
|
|
# Compara los commits entre el repositorio remoto y el actual para
|
|
|
|
# que luego los podamos mostrar.
|
|
|
|
def commits
|
|
|
|
walker = Rugged::Walker.new rugged
|
|
|
|
|
|
|
|
# Obtenemos todos los commits que existen en origin/master que no
|
|
|
|
# están en la rama master local
|
|
|
|
#
|
|
|
|
# XXX: monitorear esto por performance
|
|
|
|
walker.push 'refs/remotes/origin/master'
|
|
|
|
walker.hide 'refs/heads/master'
|
|
|
|
|
|
|
|
walker.each.to_a
|
|
|
|
end
|
2019-07-30 21:07:08 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Hay commits sin aplicar?
|
|
|
|
def needs_pull?
|
2019-09-18 20:15:46 +00:00
|
|
|
fetch
|
2019-09-12 18:10:37 +00:00
|
|
|
!commits.empty?
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2019-10-18 20:35:09 +00:00
|
|
|
# Guarda los cambios en git
|
2019-08-16 23:12:22 +00:00
|
|
|
def commit(file:, usuarie:, message:, remove: false)
|
2019-10-18 20:35:09 +00:00
|
|
|
file = [file] unless file.respond_to? :each
|
|
|
|
|
|
|
|
file.each do |f|
|
|
|
|
remove ? rm(f) : add(f)
|
|
|
|
end
|
2019-08-16 23:12:22 +00:00
|
|
|
|
|
|
|
# Escribir los cambios para que el repositorio se vea tal cual
|
2019-07-30 21:07:08 +00:00
|
|
|
rugged.index.write
|
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
Rugged::Commit.create(rugged, message: message, update_ref: 'HEAD',
|
|
|
|
parents: [rugged.head.target],
|
|
|
|
tree: rugged.index.write_tree,
|
|
|
|
author: author(usuarie),
|
|
|
|
committer: committer)
|
2019-07-30 21:07:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def author(author)
|
|
|
|
{ name: author.name, email: author.email, time: Time.now }
|
|
|
|
end
|
|
|
|
|
|
|
|
def committer
|
|
|
|
{ name: 'Sutty', email: "sutty@#{Site.domain}", time: Time.now }
|
|
|
|
end
|
2019-08-09 19:49:25 +00:00
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
def add(file)
|
|
|
|
rugged.index.add(relativize(file))
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm(file)
|
|
|
|
rugged.index.remove(relativize(file))
|
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def relativize(file)
|
|
|
|
Pathname.new(file).relative_path_from(Pathname.new(path)).to_s
|
|
|
|
end
|
2019-07-16 19:47:44 +00:00
|
|
|
end
|
|
|
|
end
|