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
|
2021-04-16 13:36:38 +00:00
|
|
|
attr_reader :rugged, :path
|
2019-07-16 19:47:44 +00:00
|
|
|
|
2021-04-16 13:36:18 +00:00
|
|
|
# @param [String] la ruta del repositorio
|
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)
|
|
|
|
end
|
|
|
|
|
2021-04-16 13:27:37 +00:00
|
|
|
# Obtiene la rama por defecto a partir de la referencia actual
|
|
|
|
#
|
|
|
|
# Por ejemplo "refs/heads/no-master" => "no-master"
|
|
|
|
#
|
|
|
|
# XXX: No memoizamos para obtener siempre el nombre de la rama
|
|
|
|
# actual, aunque por ahora asumimos que siempre estamos en la misma,
|
|
|
|
# internamente (ej. vía shell) podríamos cambiarla.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def default_branch
|
|
|
|
rugged.head.canonical_name.split('/', 3).last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Obtiene el origin
|
|
|
|
#
|
|
|
|
# @return [Rugged::Remote]
|
|
|
|
def origin
|
|
|
|
@origin ||= rugged.remotes.find do |remote|
|
|
|
|
remote.name == 'origin'
|
|
|
|
end
|
2019-07-16 19:47:44 +00:00
|
|
|
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.
|
2021-04-16 13:36:18 +00:00
|
|
|
#
|
|
|
|
# @return [Integer]
|
2019-07-16 19:47:44 +00:00
|
|
|
def fetch
|
2021-06-08 17:52:41 +00:00
|
|
|
if origin.check_connection(:fetch, credentials: credentials)
|
|
|
|
rugged.fetch(origin, credentials: credentials)[:received_objects]
|
2019-07-16 19:47:44 +00:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Incorpora los cambios en el repositorio actual
|
|
|
|
#
|
2021-04-16 13:36:18 +00:00
|
|
|
# @return [Rugged::Commit]
|
2019-08-16 23:12:22 +00:00
|
|
|
def merge(usuarie)
|
2021-04-16 13:27:37 +00:00
|
|
|
merge = rugged.merge_commits(head_commit, remote_head_commit)
|
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',
|
2021-04-16 13:27:37 +00:00
|
|
|
parents: [head_commit, remote_head_commit],
|
2019-08-16 23:23:04 +00:00
|
|
|
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
|
|
|
|
2021-04-16 13:27:37 +00:00
|
|
|
# El último commit
|
|
|
|
#
|
|
|
|
# @return [Rugged::Commit]
|
|
|
|
def head_commit
|
|
|
|
rugged.branches[default_branch].target
|
2019-08-16 23:12:22 +00:00
|
|
|
end
|
|
|
|
|
2021-04-16 13:27:37 +00:00
|
|
|
# El último commit del repositorio remoto
|
|
|
|
#
|
|
|
|
# XXX: Realmente no recuerdo por qué esto era necesario ~f
|
|
|
|
#
|
|
|
|
# @return [Rugged::Commit]
|
|
|
|
def remote_head_commit
|
|
|
|
rugged.branches["origin/#{default_branch}"].target
|
2019-08-16 23:12:22 +00:00
|
|
|
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
|
2021-04-16 13:27:37 +00:00
|
|
|
walker.push "refs/remotes/origin/#{default_branch}"
|
|
|
|
walker.hide "refs/heads/#{default_branch}"
|
2019-07-16 19:47:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-04-03 15:44:09 +00:00
|
|
|
# Cargar el árbol actual
|
|
|
|
rugged.index.read_tree rugged.head.target.tree
|
|
|
|
|
2019-10-18 20:35:09 +00:00
|
|
|
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
|
|
|
|
|
2022-04-15 13:40:18 +00:00
|
|
|
# Garbage collection
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def gc
|
|
|
|
env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path }
|
|
|
|
cmd = 'git gc'
|
|
|
|
|
|
|
|
r = nil
|
|
|
|
Dir.chdir(path) do
|
|
|
|
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, _, t|
|
|
|
|
r = t.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
r&.success?
|
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
private
|
|
|
|
|
2021-06-08 17:52:41 +00:00
|
|
|
# Si Sutty tiene una llave privada de tipo ED25519, devuelve las
|
|
|
|
# credenciales necesarias para trabajar con repositorios remotos.
|
|
|
|
#
|
|
|
|
# @return [Nil, Rugged::Credentials::SshKey]
|
|
|
|
def credentials
|
|
|
|
return unless File.exist? private_key
|
|
|
|
|
|
|
|
@credentials ||= Rugged::Credentials::SshKey.new username: 'git', publickey: public_key, privatekey: private_key
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def public_key
|
|
|
|
@public_key ||= Rails.root.join('.ssh', 'id_ed25519.pub').to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def private_key
|
|
|
|
@private_key ||= Rails.root.join('.ssh', 'id_ed25519').to_s
|
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
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
|