Subversion

Subversion (SVN) is a system for tracking changes to source code that enables concurrent work on a shared project. With each commit to the SVN server, only the changes to files placed under version control are stored. By keeping all versions of the source code, it is easier to control the development of a project, track changes and assess their impact on the operation of the whole project. Since all versions of a given file are kept, we can work on just one file without having to save stable or otherwise important versions of it. The work can thus be distributed in time and space. With Subversion, work on a project is easily divided into smaller tasks, which on each commit to the server we briefly describe as the work done by the submitted code changes. The server stores the differences in the source code, which enables tracking of and access to previous versions. Once the SVN repository is set up, we can store files in it and SVN automatically versions them.
After setting up SVN, we need to start with a new directory by checking out an empty SVN with the command:

 svn checkout --username=ime svn://forge.hpc.fs.uni-lj.si/projekt/trunk mojprojekt

which creates a new directory mojprojekt. We can add files to it. Alternatively, files can be imported from an existing directory with the command svn import. It is also possible to check out SVN into an already existing directory and manually add files under version control. In every directory under version control, a “hidden” directory is created that keeps records and stores a local copy of the files.

Files already under SVN control can be freely edited with an editor. The status of files in an SVN directory is printed with the command

svn status
    leon@duo:~/p/ppgplasma/doc$ svn st
    ?      phd.ps
    ?      auto
    M      phd.toc
    M      phd.tex
    

A question mark before a name means that the file or directory is not under SVN control, and the following actions are possible for such files:

  1. Ignore the warning. Since with a larger number of warnings we might overlook an important file, this is not a recommended way of working.

  2. If the file is important and cannot easily be derived (compiled) from other files, put the file under SVN control with the command svn add file

  3. Set which files in the directory are ignored. This is recommended for all files that are the result of a conversion or are of an auxiliary type. In the directory, we set files to be ignored with the command svn pe svn:ignore .

In the example above, we add ignoring of all files with the .toc extension and also of the auto directory. For the file phd.ps we have not yet decided whether it should be under version control or not, since such files are usually large and are the result of a conversion.
Files that have been changed (Modified) have the letter M before their name, meaning that the local copy differs from the one we last updated with the command

svn update  

If we want a listing of our changes, we do this with the command

 svn diff datoteka.tex

or just

svn diff

for all changes in the modified files. We send our changes to the SVN server with the command

svn ci -m "Spremenjen format strani in velikost črk"
Sending doc Sending doc/phd.tex
Sending doc/phd.toc
Transmitting file data ..
Committed revision 260. 

This sends all changes of the modified files to the server. The file phd.toc was under version control and was still sent as a change, even though *.toc had been added to svn:ignore, which means that svn:ignore only helps with the status listing and does not exclude files that we manually put under SVN control. For the file phd.toc this was done by mistake, so with the command

svn del *.toc 

we can simultaneously delete it locally and mark the file phd.toc for deletion on the server, which we see in the listing as the letter D before the file:

leon@duo:~/p/ppgplasma/doc$ svn st 
? phd.ps 
D phd.toc 

A new file is put under version control with the command

 svn add datoteka 

The same applies to directories and to renaming/copying/moving files, where we use the command

 svn mv prvotno.ime novo.ime
svn copy prvotno.ime kopija.ime  

which is better to use than the ADD/DEL commands, since it preserves version traceability from the original to the latest version of an individual file that has been moved between directories, renamed or copied.

Concurrent work on files

Simultaneous editing of files is possible and usually does not cause conflicts. Conflicts arise only when two people edit the same line at the same time and SVN does not know which change is correct. SVN even prevents you from sending changes to the server and thereby deleting the changes of other collaborators. It is therefore recommended to keep up to date with changes using the command

svn update 

When we have finished our part of the changes, we send them with the command

 svn ci -m "V eni vrstici opisane spremembe"  

Before sending, of course, we add new files and generally check the status of the local copy with the command

  svn status  

For more help, the following command is available

 svn help

or the freely available book at http://svnbook.red-bean.com/

Example of local work with an SVN directory

An SVN directory can be set up on a server that we access via the svn:// or http:// methods. Alternatively, we can create an SVN repository right in our home directory with the command

[leon@prelog ~]$ mkdir repos
[leon@prelog ~]$ svnadmin create repos/projekt

and access it via file:// by checking it out into the home directory with the command

[leon@prelog ~]$ svn checkout file://$HOME/repos/projekt projekt
Checked out revision 0

We cd into this directory and look at its empty contents:

[leon@prelog ~]$ cd projekt
[leon@prelog projekt]$ ls -la
skupno 12
drwxrwxr-x  3 leon leon 4096 okt 26 12:17 .
drwx--x--x 44 leon leon 4096 okt 26 12:17 ..
drwxrwxr-x  6 leon leon 4096 okt 26 12:17 .svn

For larger projects we usually create three conventional directories and store them in SVN with

[leon@prelog projekt]$ svn mkdir trunk branches tags
A         trunk
A         branches
A         tags
[leon@prelog projekt]$ svn ci -m "Začetna vzpostavitev strukture"
Adding         branches
Adding         tags
Adding         trunk

Committed revision 1

We create a small project with a Makedile file and the source file hello.c with the following commands:

[leon@prelog projekt]$ cd trunk
[leon@prelog trunk]$ kate Makefile hello.c &
[1] 1474
[leon@prelog trunk]$ ls
hello.c  Makefile
[leon@prelog trunk]$ make
cc     hello.c   -o hello
[leon@prelog trunk]$ ./hello
Test SVN

and the following “minimal” contents of Makefile and hello.c

[leon@prelog trunk]$ cat hello.c
#include <stdio.h>
int main()
{
  printf("Test SVN\n");
}
[leon@prelog trunk]$ cat Makefile
all: hello

which we store in SVN by adding the source code and excluding the executable from versioning with an explicit directory property entry.

[leon@prelog trunk]$ svn st
?      hello
?      hello.c
?      Makefile
[leon@prelog trunk]$ svn add hello.c Makefile
A         hello.c
A         Makefile
[leon@prelog trunk]$ export EDITOR=kate
[leon@prelog trunk]$ svn propedit svn:ignore .
QObject::disconnect: Unexpected null parameter
Set new value for property 'svn:ignore' on '.'
[leon@prelog trunk]$ svn st
 M     .
A      hello.c
A      Makefile
[leon@prelog trunk]$ svn ci -m "Osnutek projekta"
Sending        trunk
Adding         trunk/Makefile
Adding         trunk/hello.c
Transmitting file data ..
Committed revision 2.

We modify the program so that it prints the classic greeting Hello, world and look at the code difference with

[leon@prelog trunk]$ svn diff
Index: hello.c
===================================================================
--- hello.c     (revision 2)
+++ hello.c     (working copy)
@@ -1,5 +1,5 @@
 #include <stdio.h>
 int main()
 {
-  printf("Test SVN\n");
+  printf("Hello, World\n");
 }

We do not like the change, so we simply discard the code and revert to the latest version with

[leon@prelog trunk]$ rm *
[leon@prelog trunk]$ ls
[leon@prelog trunk]$ svn up
Restored 'hello.c'
Restored 'Makefile'
At revision 2.

Subversion

Subversion (SVN) je sistem za sledenje spremembam izvorne kode, ki omogoča sočasno delo na skupnem projektu. Pri vsaki shranitvi na SVN strežnik, se shranijo le spremembe datotek, ki so bile dane pod nadzor. S hranjenjem vseh verzij izvorne kode je tako lažje nadzorovati razvoj projekta, slediti spremembam in oceniti njihov vpliv na delovanje celotnega projekta. Ker se hranijo vse verzije določene datoteke, lahko tako delamo le na eni datoteki, ne da bi bilo potrebno shranjevanje stabilnih ali kako drugae pomembnih verzij datoteke. Način dela je tako lahko časovno in prostorsko razporejen. Delo na projektu je s Subversion enostavno razdeliti na manjše naloge, ki jih ob vsaki shranitvi na strežnik na kratko opišemo kot delo, ki ga poslane spremembe kode opravijo. Strežnik hrani razlike v izvorni kodi, kar omogoča sledenje in dostop do prejšnjih verzij. Po vzpostavitvi hranilnika SVN lahko vanj shranjujemo datoteke in SVN za nje avtomatsko vodi verzioniranje.
Po vzpostavitvi SVN je potrebno nažeti z novim imenikom s tem, da uvozimo prazen SVN z ukazom:

 svn checkout --username=ime svn://forge.hpc.fs.uni-lj.si/projekt/trunk mojprojekt

kar naredi nov imenik mojprojekt. Vanj lahko dodajamo datoteke. Lahko pa le te uvozimo že iz obstoječega imenika z ukazom svn import. Možno pa je uvoziti SVN v že obstoječi imenik in ročno dodajati datoteke pod nadzor. V vsakem imeniku, ki je pod nadzorom se ustvari “skriti” imenik, ki vodi evidenco in hrani lokalno kopijo datotek.

Datoteke ki so že pod kontrolo SVN lahko prosto urejamo z urejevalnikom. Stanje datotek v imeniku SVN izpišemo z ukazom

svn status
    leon@duo:~/p/ppgplasma/doc$ svn st
    ?      phd.ps
    ?      auto
    M      phd.toc
    M      phd.tex
    

Vprašaj pred imenom pomeni, da datoteka ali imenik ni pod SVN nadzorom in za take datoteke so možni naslednji ukrepi:

  1. Ne zmenimo se za opozorilo. Ker pri večjem številu opozoril lahko kakšno pomembno datoteko spregledamo to ti priporočljiv način dela.

  2. Če je datoteka pomembna in se je ne da enostavno izpeljati (prevesti) iz drugih datotek, se datoteko da pod SVN nadzor z ukazom svn add datoteka

  3. Nastavimo, katere datoteke se v imeniku ignorirajo. To je priporo?eno za vse datoteke, ki so rezultat pretvorbe ali so pomo?nega tipa. V imeniku nastavimo spregledovanje datotek z ukazom svn pe svn:ignore .

V zgornjem primeru dodamo ignoriranje za vse datoteke s končnico .toc in še za imenik auto. Za datoteko phd.ps se zaenkrat še nismo odločili, ali naj bo pod kontrolo ali ne saj so te datoteke obićajno velike in so rezultat pretvorbe.
Datoteke, ki so spremenjene (Modified) imajo pred imenom ?rko M, kar pomeni, da je lokalna kopija različna od tiste, ki smo jo nazadnje osvežili z ukazom

svn update  

Če želimo izpis naših popravkov to naredimo z ukazom

 svn diff datoteka.tex

ali samo

svn diff

za vse spremembe v spremenjenih datotekah. Svoje spremembe pošljemo na SVN strežnik z ukazom

svn ci -m "Spremenjen format strani in velikost črk"
Sending doc Sending doc/phd.tex
Sending doc/phd.toc
Transmitting file data ..
Committed revision 260. 

Pri tem se pošljejo na strežnik spremembe vse spremembe spremenjenih datotek. Datoteka phd.toc je bila pod nadzorom in se je vseeno poslala, kot sprememba, čeprav je bilo dodano *.toc v svn:ignore, kar pomeni, da je svn:ignore le pomoč pri izpisu stanja in ne izključuje datotek, ki jih ročno damo pod SVN kontrolo. Za datoteko phd.toc je bilo to narejeno pomotoma, zato lahko z ukazom

svn del *.toc 

hkrati lokalno pobrišemo in označimo datoteko phd.toc za brisanje na strežniku, kar vidimo pri izpisu kot črko D pred datoteko:

leon@duo:~/p/ppgplasma/doc$ svn st 
? phd.ps 
D phd.toc 

Novo datoteko damo pod kontrolo z ukazom

 svn add datoteka 

Podobno velja za imenike in preimenovanja/kopiranja/premikanja datotek kjer uporabimo ukaz

 svn mv prvotno.ime novo.ime
svn copy prvotno.ime kopija.ime  

kar je bolje uporabiti kot pa ukaza ADD/DEL saj je pri tem možna sledljivost verzij od prvotne pa vse do zadnje verzije posamezne datoteke, ki se je premikala po imenikih, preimenovala ali kopirala.

Sočasno delo na datotekah

Hkratno urejanje datotek je možno in običajno ne povzroča konfliktov. Konflikti nastanejo le takrat, ko dva hkrati popravita isto vrstico in SVN ne ve kateri popravek je pravilen. SVN celo onemogoča, da bi poslal spremembe na strežnik in s tem pobrisal spremembe drugih sodelujočih. Zato se priporoča, da se spremembe sproti dopolnjujejo z ukazom

svn update 

Ko pa s svojim delom sprememb zaključimo pa le te pošljemo z ukazom

 svn ci -m "V eni vrstici opisane spremembe"  

Pred pošiljanjem seveda dodamo nove datoteke in na sploh preverimo stanje lokalne kopije z ukazom

  svn status  

Za več pomoči je na voljo ukaz

 svn help

ali prosto dostopna knjiga na spletni strani http://svnbook.red-bean.com/

Primer lokalnega dela s SVN imenikom

SVN imenik lahko dobimo vzpostavljen na strežniku do katerega lahko dostopamo z metodama svn:// ali z http://  Lahko pa naredimo SVN skladišče kar v domačem imeniku z ukazom

[leon@prelog ~]$ mkdir repos
[leon@prelog ~]$ svnadmin create repos/projekt

in do njega dostopamo z načinom file:// tako da ga izvozimo v domači imenik z ukazom

[leon@prelog ~]$ svn checkout file://$HOME/repos/projekt projekt
Checked out revision 0

V ta imenik gremo s cd in pogledamo prazno vsebino:

[leon@prelog ~]$ cd projekt
[leon@prelog projekt]$ ls -la
skupno 12
drwxrwxr-x  3 leon leon 4096 okt 26 12:17 .
drwx--x--x 44 leon leon 4096 okt 26 12:17 ..
drwxrwxr-x  6 leon leon 4096 okt 26 12:17 .svn

Za večje projekte običajno naredimo tri dogovorjene imenike in jih shranimo v SVN z

[leon@prelog projekt]$ svn mkdir trunk branches tags
A         trunk
A         branches
A         tags
[leon@prelog projekt]$ svn ci -m "Začetna vzpostavitev strukture"
Adding         branches
Adding         tags
Adding         trunk

Committed revision 1

Naredimo manjši projekt z datoteko Makedile in izvorno datoteko hello.c z naslednjimi ukazi:

[leon@prelog projekt]$ cd trunk
[leon@prelog trunk]$ kate Makefile hello.c &
[1] 1474
[leon@prelog trunk]$ ls
hello.c  Makefile
[leon@prelog trunk]$ make
cc     hello.c   -o hello
[leon@prelog trunk]$ ./hello
Test SVN

in naslednjo “minimalno” vsebino Makefile in hello.c

[leon@prelog trunk]$ cat hello.c
#include <stdio.h>
int main()
{
  printf("Test SVN\n");
}
[leon@prelog trunk]$ cat Makefile
all: hello

ki jo shranimo v SVN tako, da dodamo izvorno kodo in izločimo verzioniranje izvršne kode z eksplicitnim vnosom lastnosti imenika.

[leon@prelog trunk]$ svn st
?      hello
?      hello.c
?      Makefile
[leon@prelog trunk]$ svn add hello.c Makefile
A         hello.c
A         Makefile
[leon@prelog trunk]$ export EDITOR=kate
[leon@prelog trunk]$ svn propedit svn:ignore .
QObject::disconnect: Unexpected null parameter
Set new value for property 'svn:ignore' on '.'
[leon@prelog trunk]$ svn st
 M     .
A      hello.c
A      Makefile
[leon@prelog trunk]$ svn ci -m "Osnutek projekta"
Sending        trunk
Adding         trunk/Makefile
Adding         trunk/hello.c
Transmitting file data ..
Committed revision 2.

Program popravimo tako, da izpiše klasični pozdrav Hello, world in pogledamo razliko kode z

[leon@prelog trunk]$ svn diff
Index: hello.c
===================================================================
--- hello.c     (revision 2)
+++ hello.c     (working copy)
@@ -1,5 +1,5 @@
 #include <stdio.h>
 int main()
 {
-  printf("Test SVN\n");
+  printf("Hello, World\n");
 }

Popravek nam ni všeč, zato kodo kar pobrišemo in spravimo na zadnjo verzijo z

[leon@prelog trunk]$ rm *
[leon@prelog trunk]$ ls
[leon@prelog trunk]$ svn up
Restored 'hello.c'
Restored 'Makefile'
At revision 2.