Skip to content

Commit

Permalink
Squashed 'git-duplicate-remotes/' content from commit 2a318c0
Browse files Browse the repository at this point in the history
git-subtree-dir: git-duplicate-remotes
git-subtree-split: 2a318c0bf9e7c0927934003450c0ded95c15c766
  • Loading branch information
neverkas committed Mar 23, 2023
0 parents commit f0045a9
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 0 deletions.
656 changes: 656 additions & 0 deletions DOCUMENTATION.org

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-include remotes.cfg
-include config.cfg
-include unix-utils.mk

GIT_INITIALIZED=git rev-parse --is-inside-work-tree

GIT_HOOKS = $(wildcard git-hooks/*)
CURRENT_DIRECTORY_NAME = $(notdir $(PWD))
PARENT_DIRECTORY_FILES = $(notdir $(wildcard ../*) $(wildcard ../.*))
EXCLUDED_FILES=$(CURRENT_DIRECTORY_NAME) . ..
PARENT_DIRECTORY_FILES_FILTERED = $(filter-out $(EXCLUDED_FILES), $(PARENT_DIRECTORY_FILES))

.DEFAULT_GOAL=help

ifeq (false,$(GNU_MAKE_PRINT_RECIPE))
AT:=@
endif

# TODO: si ya tiene creado el repositorio remoto dónde se subirán los cambios, trae problemas de merge porque no hacemos pull..
# (suponemos que es su primer push dónde creamos la branch upstream)

##@ Comandos
i init: git-init git-remotes-update git-pull git-push-upstream ## Agrega los repositorios remotos, descarga/sube de los remotos configurados

git-init:
ifneq ($(shell cd .. && $(GIT_INITIALIZED) 2>/dev/null),true)
$(info Inicializando repositorio de git..)
$(AT)cd .. \
&& git init
endif

p git-pull: ## descargar cambios del repositorio only-fetch
$(info Descargando cambios del repositorio only-fetch..)
$(AT)cd .. \
&& git pull only-fetch master

git-remotes-update: .targets/git-remotes

# se va a ejecutar sólo cuando el target y su dependencia (remote.cfg) difieran en el timestamp
.targets/git-remotes: remotes.cfg
$(info Agregando repositorio remotos a git..)
# 1. creamos el directorio que contiene a éste target (archivo)
$(AT)$(MKDIR) $(dir $@) && touch $@
# 2. agregamos los repositorios remotos en el directorio padre
$(AT)cd .. \
&& git remote add only-fetch $(URL_REMOTO_ONLY_FETCH) \
&& git remote add origin $(URL_REMOTO_PRIVADO)

# Notas:
# 1. luego de creada una branch (local), ésta requiere asociarla con una branch upstream (remota)
# para subir/descargar cambios al repositorio remoto (operaciones push y fetch)
#
# 2. se crea una única vez la branch upstream (remota) "cuando queremos hacer push ó fetch al repositorio remoto"
# y sólo se asocia una por branch (local)
git-push-upstream: .targets/git-hooks-update
$(info Configurando repositorio upstream y subiendo cambios al repositorio origin..)
$(AT)cd .. \
&& git push --set-upstream origin master

# se va a ejecutar sólo cuando el target y su dependencias difieran en el timestamp
# (una Regla Explícita no me parece viable y una Regla Implícita sería sobrediseño)
.targets/git-hooks-update: $(GIT_HOOKS)
# 1. creamos el directorio que contiene a éste target (archivo)
$(AT)$(MKDIR) $(dir $@) && touch $@
# 2. copiamos los archivos al directorio padre
$(AT)$(foreach hook-file, $^,\
chmod u+x $(hook-file) && \
$(COPY) $(hook-file) ../.git/hooks; \
)

##@ Utilidades
h help: ## Mostrar menú de ayuda
@awk 'BEGIN {FS = ":.*##"; printf "\nOpciones para usar:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

# Notas:
# 1. el comando `find` de bash sería la alternativa para obtener los archivos del directorio padre e ignorar éste directorio
# 2. Utilizamos el comando TEST (de linux) con la opción -eq porque éste se utiliza para comparar valores numéricos
clean: ## Eliminar archivos del directorio padre
$(AT)$(BOX_CONFIRM_CLEAN) \
&& test $(EXIT_STATUS) -eq $(EXIT_STATUS_SUCCESS) \
&& (echo "Eliminando archivos y directorios del directorio padre.." \
&& $(RM) .targets/* \
&& cd .. && $(RM) $(PARENT_DIRECTORY_FILES_FILTERED)) \
|| true

.PHONY: i init clean h help git-init git-pull git-remotes-update git-push-upstream
15 changes: 15 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#+TITLE: Git Duplicate Remotes
* ¿Qué es?
- Duplica un *repositorio remoto público* en uno *repositorio remoto privado*
* ¿Por qué lo utilizo?
- Reutilizar un proyecto de github y utilizarlo de forma privada
* ¿Qué ventajas tiene?
- Inicializa y configura los repositorios de forma automática
- Configura el repositorio privado como ~origin~ para subir los cambios
- Configura el repositorio público como ~only-fetch~ sólo para traer actualizaciones
- Bloquea las operaciones ~push~ en el repositorio ~only-fetch~ (el que queriamos duplicar)
* ¿Cómo utilizar?
1) Creamos un repositorio privado en un servidor github, gitlab ó bitbucket (/no agregar README ni subir cambios/)
2) Creamos una carpeta en nuestra maquina local con el nombre del proyecto que queremos duplicar y dentro clonar éste repositorio
3) Modificar el archivo ~remotes.cfg~ con las url de los repositorios (sólo hay dos variables, no requiere más configuración)
4) Ejecutar en la terminal de comandos de linux ~make init~ y listo :)
1 change: 1 addition & 0 deletions config.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GNU_MAKE_PRINT_RECIPE=false
25 changes: 25 additions & 0 deletions git-hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#

remote="$1"
url="$2"

# notas
# 1. uso el operador = en vez de == porque el primero es posix sh, el segundo es específico para bash
# (y git promueve el sh al parece, por la primera linea)
if [ "$remote" = "only-fetch" ]
then
echo "No podés pushear en un remoto de only-fetch"
exit 1
fi

exit 0
14 changes: 14 additions & 0 deletions remotes.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#####################################################################
# USAR SOLO PARA MODIFICAR LAS URL DE LOS REPOSITORIOS REMOTOS
# ESTE ARCHIVO ES UNA DEPENDENCIA DEL MAKEFILE
#####################################################################
#
# repositorio remoto (origen)
# - el que queremos duplicar
# - suponemos que tiene una rama remota master con commits
URL_REMOTO_ONLY_FETCH[email protected]:manu-projects/pdf-book-manager.git

# repositorio remoto privado (destino)
# - el que queremos que tenga el contenido de duplicado
# - NO debe tener ningún commit para evitar problemas de merge
URL_REMOTO_PRIVADO[email protected]:manu-learning/manu-books.git
16 changes: 16 additions & 0 deletions unix-utils.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PWD=$(shell pwd)
COPY=rsync -avz
RM=rm -rf
MKDIR=mkdir -p

BOX_CONFIRM_CLEAN=whiptail --title "Eliminar archivos directorio padre" \
--yesno "Está seguro de confirmar la acción?" 0 0 \
--no-button "Cancelar" --yes-button "Confirmar"

# - el $? NO es una macro de GNU Make, es propio de linux y guarda el Estado de Salida luego de ejecutar un comando de linux (programas)
EXIT_STATUS=$(shell echo $$?)

# - el valor 0 indíca que el comando de linux se ejecutó con éxito
# (se le pasaron opciones que posee, parámetros válidos como rutas, ...)
EXIT_STATUS_SUCCESS=0

0 comments on commit f0045a9

Please sign in to comment.