miércoles, 13 de febrero de 2013

Utilizando GIT contra un servidor SVN

Fai uns días xa faláramos dos sistemas de control de versións, de GIT e das ventaxas que tiñan os que coma el eran distribuídos, hoxe ímos ver como usar GIT coma interfaz sobre un sistema SVN (non distribuído), para obter certas ventaxas que teñen estes sistemas.

Para as probas imaxinade un servidor SVN por HTTP `svn-test-server` cun repositorio na ruta `/svn/hl/`, neste caso con SVN para descargar o código faríase:
[~/git-svn]$ svn checkout http://svn-test-server/svn/hl/
Checked out revision 0.
[~/git-svn]$ ls -a hl
.  ..  .svn
[~/git-svn]$ 

Ben, agora coma escenario para as probas crearémo-la estructura de arquivos típica de SVN (aforramos pásos adicionais se facemos o primeiro commit dende o SVN e xa aproveitamos :P)
[~/git-svn]$ cd hl
[~/git-svn/hl]$ mkdir branch tags trunk
[~/git-svn/hl]$ svn add branch/ tags/ trunk/
A         branch
A         tags
A         trunk
[~/git-svn/hl]$ svn commit -m 'Estrutura de directorios típica de SVN'
Adding         branch
Adding         tags
Adding         trunk

Committed revision 1.
[~/git-svn/hl]$ 

Ben, iso xa está funcionando, deixemos ese directorio a man e ímos con GIT, o comando para tomar o repositorio é `git svn clone <URL>`
[~/git-svn]$ mv hl/ hl-svn
[~/git-svn]$ git svn clone http://svn-test-server/svn/hl/
Initialized empty Git repository in /home/kenkeiras/git-svn/hl/.git/
W: +empty_dir: branch
W: +empty_dir: tags
W: +empty_dir: trunk
r1 = e86e31e156196ef4928851c88b4dd15dfec8ef32 (refs/remotes/git-svn)
Checked out HEAD:
  http://svn-test-server/svn/hl r1
creating empty directory: branch
creating empty directory: tags
creating empty directory: trunk
# Podemos botarlle unha ollada
[~/git-svn]$ cd hl
[~/git-svn/hl]$ git log
commit e86e31e156196ef4928851c88b4dd15dfec8ef32
Author: kenkeiras <kenkeiras b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:21:46 2013 +0000

    Estrutura de directorios típica de SVN
    
    git-svn-id: http://svn-test-server/svn/hl@1 b3b2f9e3-104c-4a07-99f8-c5963656e42c
[~/git-svn/hl]$ 

E xa o temos, podémos xestionalo (case) como se fora un GIT calqueira
[~/git-svn/hl]$ ls
branch  tags  trunk
[~/git-svn/hl]$ cd trunk/
[~/git-svn/hl/trunk]$ echo 'Texto de exemplo' > README
[~/git-svn/hl/trunk]$ git add README
[~/git-svn/hl/trunk]$ git commit -m 'Todo bo repo ten un readme'
[master fcc10c3] Todo bo repo ten un readme
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 trunk/README
# E o commit está feito, sen necesidade de mandalo o servidor
[~/git-svn/hl/trunk]$ git log
commit fcc10c3a44e297e0d72abf5b45c4e6f0d36b84b4
Author: kenkeiras <kenkeiras@gmail.com>
Date:   Wed Feb 13 22:27:16 2013 +0100

    Todo bo repo ten un readme

commit e86e31e156196ef4928851c88b4dd15dfec8ef32
Author: kenkeiras <kenkeiras b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:21:46 2013 +0000

    Estrutura de directorios típica de SVN
    
    git-svn-id: http://svn-test-server/svn/hl@1 b3b2f9e3-104c-4a07-99f8-c5963656e42c
[~/git-svn/hl/trunk]$ 

Iso sí, cando vaiamos mandálos cambios temos que facer `git svn dcommit`
[~/git-svn/hl/trunk]$ git svn dcommit
Committing to http://svn-test-server/svn/hl ...
 A trunk/README
Committed r2
 A trunk/README
r2 = 013fd1684e38ec994ac13af914767e1565bed844 (refs/remotes/git-svn)
No changes between current HEAD and refs/remotes/git-svn
Resetting to the latest refs/remotes/git-svn
# Iso sí, vai ter que “reescribir” levemente a historia (autor, data de envío e engadir un ID)
[~/git-svn/hl/trunk]$ git log| head -n 8
commit 013fd1684e38ec994ac13af914767e1565bed844
Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:30:51 2013 +0000

    Todo bo repo ten un readme
    
    git-svn-id: http://svn-test-server/svn/hl@2 b3b2f9e3-104c-4a07-99f8-c5963656e42c


Agora ímos ver como receber cambios do repositorio. Primeiro haberá que producilos
[~/git-svn/hl/trunk]$ cd ../../hl-svn/
[~/git-svn/hl-svn]$ svn update
A    trunk/README
Updated to revision 2.
# Notade que actualizou !
# Un cambio no trunk
[~/git-svn/hl-svn]$ echo 'Estaría ben estender un pouco o texto' >> trunk/README 
# E a facer commit
[~/git-svn/hl-svn]$ svn commit -m 'Actualización do README'
Sending        trunk/README
Transmitting file data .
Committed revision 3.
# E despois facemos un tag, logo veremos para que ;)
[~/git-svn/hl-svn]$ svn copy trunk/ tags/v1
A         tags/v1
[~/git-svn/hl-svn]$ ls tags/v1/
README
# E a facer commit
[~/git-svn/hl-svn]$ svn commit -m 'Tagging'
Adding         tags/v1
Transmitting file data .
Committed revision 4.

De volta no GIT, podemos tomar os cambios con `git svn rebase`
[~/git-svn/hl-svn]$ cd ../hl
[~/git-svn/hl]$ git svn rebase
 M trunk/README
...
Fast-forwarded master to refs/remotes/git-svn.
# E aquí están os cambios
[~/git-svn/hl]$ git log
commit 57d2ed246838c32e78f85981bae691d5ee9eeb4f
Author: kenkeiras <kenkeiras b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 22:06:13 2013 +0000

    Tagging
    
    git-svn-id: http://svn-test-server/svn/hl@4 b3b2f9e3-104c-4a07-99f8-c5963656e42c

commit 4a62851e5e06b8e2dc2063b6113296f07bf3b188
Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:39:07 2013 +0000

    Actualización do README
    
    git-svn-id: http://svn-test-server/svn/hl@3 b3b2f9e3-104c-4a07-99f8-c5963656e42c

commit 013fd1684e38ec994ac13af914767e1565bed844
Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:30:51 2013 +0000

    Todo bo repo ten un readme
    
    git-svn-id: http://svn-test-server/svn/hl@2 b3b2f9e3-104c-4a07-99f8-c5963656e42c

commit e86e31e156196ef4928851c88b4dd15dfec8ef32
Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
Date:   Wed Feb 13 21:21:46 2013 +0000

    Estrutura de directorios típica de SVN
    
    git-svn-id: http://svn-test-server/svn/hl@1 b3b2f9e3-104c-4a07-99f8-c5963656e42c
[~/git-svn/hl]$ 

E para o truco final ímos voltar a tomar o repositorio con GIT, pero desta vez ímos facer que absorba a estructura:
[~/git-svn]$ git svn clone http://svn-test-server/svn/hl/ --stdlayout 
Initialized empty Git repository in /home/kenkeiras/git-svn/hl/.git/
r1 = 4b78a8c0dad91a5b66e9e529cb3b79149fb334cb (refs/remotes/trunk)
 A README
r2 = 83c362be434048afb88f26bd8f83e1c65c5cdaec (refs/remotes/trunk)
 M README
r3 = eccf0deaa90f85a518a66c5caaf1c3bd361aacc5 (refs/remotes/trunk)
branch_from: /tag => /tag/v1
Found possible branch point: http://svn-test-server/svn/hl/tag/v1 => http://svn-test-server/svn/hl/tags/v1, 3
Initializing parent: refs/remotes/tags/v1@3
Following parent with do_switch
Successfully followed parent
r3 = 8fb7e22d73e717c1f2fb3b1aa0c1b7b358df51a6 (refs/remotes/tags/v1@3)
Found branch parent: (refs/remotes/tags/v1) 8fb7e22d73e717c1f2fb3b1aa0c1b7b358df51a6
Following parent with do_switch
Successfully followed parent
r4 = 7959f1379053b2279f25572ba57ea0b47a9f63c1 (refs/remotes/tags/v1)
Checked out HEAD:
  http://svn-test-server/svn/hl/trunk r3
[~/git-svn]$ cd hl
[~/git-svn/hl]$ ls
README
[~/git-svn/hl]$ 

E so con engadir o parámetro `--stdlayout` xa temos o directorio `trunk` en primeiro plano, os demais están a man en forma de ramas do repositorio:
[~/git-svn/hl]$ git graph
* commit 7959f13 (tags/v1)
| Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
| Date:   Wed Feb 13 22:06:13 2013 +0000
| 
|     Tagging
|     
|     git-svn-id: http://svn-test-server/svn/hl/tags/v1@4 b3b2f9e3-104c-4a07-99f8-c5963656e42c
|  
| * commit eccf0de (HEAD, trunk, master)
|/  Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
|   Date:   Wed Feb 13 21:39:07 2013 +0000
|   
|       Actualización do README
|       
|       git-svn-id: http://svn-test-server/svn/hl/trunk@3 b3b2f9e3-104c-4a07-99f8-c5963656e42c
|  
* commit 83c362b
| Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
| Date:   Wed Feb 13 21:30:51 2013 +0000
| 
|     Todo bo repo ten un readme
|     
|     git-svn-id: http://svn-test-server/svn/hl/trunk@2 b3b2f9e3-104c-4a07-99f8-c5963656e42c
|  
* commit 4b78a8c
  Author: kenkeiras <kenkeiras@b3b2f9e3-104c-4a07-99f8-c5963656e42c>
  Date:   Wed Feb 13 21:21:46 2013 +0000
  
      Estrutura de directorios típica de SVN
      
      git-svn-id: http://svn-test-server/svn/hl/trunk@1 b3b2f9e3-104c-4a07-99f8-c5963656e42c
# (O git graph é un alias para amosar as ramas)
[~/git-svn/hl]$ git config alias.graph
log --graph --decorate --abbrev-commit --all
[~/git-svn/hl]$ 

Ou podemos podemos usar `--branches`, `--tags` e `--trunk` se no son os estándar.

Happy coding!

[Referencia]
Git and Other Systems (Pro GIT)

No hay comentarios:

Publicar un comentario