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
|
|
|
|
#
|
2023-07-18 18:47:26 +00:00
|
|
|
# @return [Rugged::Remote, nil]
|
2021-04-16 13:27:37 +00:00
|
|
|
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
|
2023-07-03 16:23:54 +00:00
|
|
|
#
|
|
|
|
# @param :add [Array] Archivos a agregar
|
|
|
|
# @param :rm [Array] Archivos a eliminar
|
|
|
|
# @param :usuarie [Usuarie] Quién hace el commit
|
|
|
|
# @param :message [String] Mensaje
|
|
|
|
def commit(add: [], rm: [], usuarie:, message:)
|
2023-04-03 15:44:09 +00:00
|
|
|
# Cargar el árbol actual
|
|
|
|
rugged.index.read_tree rugged.head.target.tree
|
|
|
|
|
2023-07-03 16:23:54 +00:00
|
|
|
add.each do |file|
|
|
|
|
rugged.index.add(relativize(file))
|
|
|
|
end
|
|
|
|
|
|
|
|
rm.each do |file|
|
|
|
|
rugged.index.remove(relativize(file))
|
2019-10-18 20:35:09 +00:00
|
|
|
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
|
|
|
|
2022-04-15 13:40:18 +00:00
|
|
|
# Garbage collection
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def gc
|
2023-07-20 18:31:38 +00:00
|
|
|
git_sh("git", "gc")
|
2023-07-18 18:47:26 +00:00
|
|
|
end
|
2022-04-15 13:40:18 +00:00
|
|
|
|
2023-07-18 18:47:26 +00:00
|
|
|
# Pushea cambios al repositorio remoto
|
|
|
|
#
|
2023-09-22 16:20:20 +00:00
|
|
|
# @param :remote [Rugged::Remote]
|
2023-07-18 18:47:26 +00:00
|
|
|
# @return [Boolean, nil]
|
2023-09-22 16:20:20 +00:00
|
|
|
def push(remote = origin)
|
|
|
|
remote.push(rugged.head.canonical_name, credentials: credentials_for(remote))
|
|
|
|
git_sh('git', 'lfs', 'push', remote.name, default_branch)
|
2022-04-15 13:40:18 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
private
|
|
|
|
|
2023-09-22 16:20:20 +00:00
|
|
|
# @deprecated
|
|
|
|
def credentials
|
|
|
|
@credentials ||= credentials_for(origin)
|
|
|
|
end
|
|
|
|
|
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.
|
|
|
|
#
|
2023-09-22 16:20:20 +00:00
|
|
|
# @param :remote [Rugged::Remote]
|
2021-06-08 17:52:41 +00:00
|
|
|
# @return [Nil, Rugged::Credentials::SshKey]
|
2023-09-22 16:20:20 +00:00
|
|
|
def credentials_for(remote)
|
2021-06-08 17:52:41 +00:00
|
|
|
return unless File.exist? private_key
|
|
|
|
|
2023-09-22 16:20:20 +00:00
|
|
|
Rugged::Credentials::SshKey.new username: username_for(remote), publickey: public_key, privatekey: private_key
|
2023-09-22 16:19:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Obtiene el nombre de usuario para el repositorio remoto, por
|
|
|
|
# defecto git
|
|
|
|
#
|
|
|
|
# @param :remote [Rugged::Remote]
|
|
|
|
# @return [String]
|
|
|
|
def username_for(remote)
|
|
|
|
username = parse_url(remote.url)&.user if remote.respond_to? :url
|
2023-09-22 15:21:55 +00:00
|
|
|
|
2023-09-22 16:19:21 +00:00
|
|
|
username || 'git'
|
2023-09-22 15:21:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @param :url [String]
|
|
|
|
# @return [URI, nil]
|
|
|
|
def parse_url(url)
|
|
|
|
GitCloneUrl.parse(url)
|
|
|
|
rescue URI::Error => e
|
2023-09-22 15:47:18 +00:00
|
|
|
ExceptionNotifier.notify_exception(e, data: { path: path, url: url })
|
2023-09-22 15:21:55 +00:00
|
|
|
nil
|
2021-06-08 17:52:41 +00:00
|
|
|
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
|
2023-07-18 18:47:26 +00:00
|
|
|
|
2023-07-20 18:31:38 +00:00
|
|
|
# Ejecuta un comando de git
|
|
|
|
#
|
|
|
|
# @param :args [Array]
|
|
|
|
# @return [Boolean]
|
|
|
|
def git_sh(*args)
|
2023-07-18 18:47:26 +00:00
|
|
|
env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path }
|
|
|
|
|
|
|
|
r = nil
|
2023-07-20 19:26:43 +00:00
|
|
|
Open3.popen2e(env, *args, unsetenv_others: true, chdir: path) do |_, _, t|
|
2023-07-18 18:47:26 +00:00
|
|
|
r = t.value
|
|
|
|
end
|
|
|
|
|
|
|
|
r&.success?
|
|
|
|
end
|
2019-07-16 19:47:44 +00:00
|
|
|
end
|
|
|
|
end
|