Total Pageviews

Wednesday, 11 January 2017

SVN Commands and Administration !!!

In my previous post we discussed the concept of SVN and its installation. In this post I am going to show the working with commands of SVN from Client and administration part at the server end.

SVN is a source code management tool which is used by a developer to push the code to the actual SVN server. Sometimes different users may work on the same directory and files, so to avoid conflicts related to data, svn uses "commit" which allows whether a file can be committed or any other user is still working on it.

If other users working on it, svn throws us a message asking whether to continue with the changes by A developer or to commit another developer B's work.

Let us get into the action by practicing few basic commands:

We have seen already, how to add a file and commit. Now lets see how to add a directory which contains different files.

F:\Workspace_local>svn st
svn: warning: W155007: 'F:\Workspace_local' is not a working copy

F:\Workspace_local>

Important rule never forget to perform work/ execute svn cmds in your Workspace.

F:\Workspace_local>cd subversion
F:\Workspace_local\subversion>
F:\Workspace_local\subversion>svn st  --- just to know workspace is in sync with repo or not....
F:\Workspace_local\subversion>

Before adding a directory to my repo,

F:\Workspace_local\subversion>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion

01/09/2017  05:51 PM    <DIR>          .
01/09/2017  05:51 PM    <DIR>          ..
01/09/2017  05:50 PM             1,914 README.md
01/09/2017  05:53 PM                17 Welcome.txt
               2 File(s)          1,931 bytes
               2 Dir(s)  230,929,895,424 bytes free
F:\Workspace_local\subversion>
Adding a folder named "New folder", it contains some text files and a image file.

Now add it to our server repo, 

F:\Workspace_local\subversion>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion

01/11/2017  07:17 PM    <DIR>          .
01/11/2017  07:17 PM    <DIR>          ..
01/11/2017  07:13 PM    <DIR>          New folder
01/09/2017  05:50 PM             1,914 README.md
01/09/2017  05:53 PM                17 Welcome.txt
               2 File(s)          1,931 bytes
               3 Dir(s)  230,929,895,424 bytes free
F:\Workspace_local\subversion>        Above is, current status before commit and after copying..

F:\Workspace_local\subversion>svn st
?       New folder
F:\Workspace_local\subversion>        Status shows recent changes which are yet to sync local with repo..

It is time to add our directory...

F:\Workspace_local\subversion>svn add "New folder"
A         New folder
A  (bin)  New folder\Capture1.PNG
A         New folder\hp_sr.txt
A         New folder\antivir.txt
A         New folder\icici amts.txt
F:\Workspace_local\subversion>

Once added, we need to commit this to reflect in repo....


F:\Workspace_local\subversion>svn commit -m "adding newfolder to repo"
Adding         New folder
Adding  (bin)  New folder\Capture1.PNG
Adding         New folder\antivir.txt
Adding         New folder\hp_sr.txt
Adding         New folder\icici amts.txt
Transmitting file data ....
Committed revision 3.
F:\Workspace_local\subversion>



To delete a file from local and repo,

F:\Workspace_local\subversion>svn delete "New folder"\hp_sr.txt
D         New folder\hp_sr.txt
F:\Workspace_local\subversion>cd "New folder"
F:\Workspace_local\subversion\New folder>
F:\Workspace_local\subversion\New folder>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion\New folder

01/11/2017  07:22 PM    <DIR>          .
01/11/2017  07:22 PM    <DIR>          ..
02/15/2016  01:13 PM               157 antivir.txt
01/11/2017  07:12 PM           170,057 Capture1.PNG
09/08/2016  10:37 AM                50 icici amts.txt
               3 File(s)        170,264 bytes
               2 Dir(s)  230,929,723,392 bytes free
F:\Workspace_local\subversion\New folder>

So far this is temporary. If we check in browser, this won't be reflected at server end.

Before committing and after deleting at local, still we can view the file and its contents...

F:\Workspace_local\subversion\New folder>svn commit -m "deleted hp_sr"
Deleting       hp_sr.txt
Committed revision 4.
F:\Workspace_local\subversion\New folder>


After commit, it will be permanently deleted....

To check the logs, we have "svn log"command.

F:\Workspace_local\subversion>svn log -r3
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
F:\Workspace_local\subversion>

Similarly to view log for a range of revisions,

F:\Workspace_local\subversion>svn log -r3:4
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
r4 | (no author) | 2017-01-11 19:23:55 -0500 (Wed, 11 Jan 2017) | 1 line

deleted hp_sr
------------------------------------------------------------------------
F:\Workspace_local\subversion>

Whenever we feel like updating repo with older we have "update" command,
If we consider that our previous deletion is not a proper step and want changes to go back.

F:\Workspace_local\subversion\New folder>svn update -r3
Updating '.':
A    hp_sr.txt
Updated to revision 3.
F:\Workspace_local\subversion\New folder>

F:\Workspace_local\subversion\New folder>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion\New folder

01/11/2017  07:32 PM    <DIR>          .
01/11/2017  07:32 PM    <DIR>          ..
02/15/2016  01:13 PM               157 antivir.txt
01/11/2017  07:12 PM           170,057 Capture1.PNG
01/11/2017  07:32 PM                11 hp_sr.txt
09/08/2016  10:37 AM                50 icici amts.txt
               4 File(s)        170,275 bytes
               2 Dir(s)  230,929,723,392 bytes free
F:\Workspace_local\subversion\New folder>

There are two other main commands with special usage. When it comes to a developer, he has his workspace checked out with the repo. But what if a one time user want to push some data from his PC. Similarly what if someone wants to pull a single file.

Usually we can use checkout command to push entire directory, so to push a single file we have "svn import" command.

Note: To make use of "import" command, no need to run it from Workspace. I copied a file outside of my workspace "subversion".

F:\Workspace_local>svn st
svn: warning: W155007: 'F:\Workspace_local' is not a working copy
F:\Workspace_local>

F:\Workspace_local>svn import hp_sr.txt http://10.0.0.218:81/subversion/New\folder/hp_sr.txt -m "adding hp_sr thru import"
Adding         hp_sr.txt
Committed revision 5.
F:\Workspace_local>

One import to New\folder... and below import to "subversion" (Workspace)...

F:\Workspace_local>svn import hp_sr.txt http://10.0.0.218:81/subversion/hp_sr.txt -m "adding hp_sr thru import"
Adding         hp_sr.txt
Committed revision 6.
F:\Workspace_local>
F:\Workspace_local>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local

01/11/2017  07:39 PM    <DIR>          .
01/11/2017  07:39 PM    <DIR>          ..
01/11/2017  07:32 PM                11 hp_sr.txt
01/11/2017  07:17 PM    <DIR>          subversion
               1 File(s)             11 bytes
               3 Dir(s)  230,929,723,392 bytes free

F:\Workspace_local>
To fetch/pull a single file, we make use of "export" command... We can run it from outside of workspace. 

F:\Workspace_local>svn export http://10.0.0.218:81/subversion/New%20folder/antivir.txt
A    antivir.txt
Export complete.

F:\Workspace_local>
F:\Workspace_local>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local

01/11/2017  07:48 PM    <DIR>          .
01/11/2017  07:48 PM    <DIR>          ..
01/11/2017  07:18 PM               157 antivir.txt
01/11/2017  07:32 PM                11 hp_sr.txt
01/11/2017  07:17 PM    <DIR>          subversion
               2 File(s)            168 bytes
               3 Dir(s)  230,929,723,392 bytes free

F:\Workspace_local>




Other than these, there are few other commands like "svn revert" to revert any operations before committing, and "svn diff" used to view difference between two files.

So far we completed svn commands practical from client side. Now let us see some SVN admin tasks. 

Admin should perform tasks related to Authorization, backups of repo and restricting few tasks or applying conditions to perform a task.

Observe the output:

F:\Workspace_local\subversion>svn log -r2:6
------------------------------------------------------------------------
r2 | (no author) | 2017-01-09 18:01:13 -0500 (Mon, 09 Jan 2017) | 1 line

1st Commit
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
r4 | (no author) | 2017-01-11 19:23:55 -0500 (Wed, 11 Jan 2017) | 1 line

deleted hp_sr
------------------------------------------------------------------------
r5 | (no author) | 2017-01-11 19:44:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding hp_sr thru import
------------------------------------------------------------------------
r6 | (no author) | 2017-01-11 19:46:34 -0500 (Wed, 11 Jan 2017) | 1 line

adding hp_sr thru import
------------------------------------------------------------------------


F:\Workspace_local\subversion>

We are working with anonymous user, we can restrict this by creating separate users and also by restricting their access to certain level (directories).

To create users,

root@ubuntu-new:/opt/subversion-1.9.5-0# cd apache2/
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -c /opt/subversion-1.9.5-0/svn_users dev1
New password:
Re-type new password:
Adding password for user dev1
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

For second developer,  used -m to modify the users file. If we use -c it will create the file again...

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -m /opt/subversion-1.9.5-0/svn_users dev2
New password:
Re-type new password:
Adding password for user dev2
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# cat /opt/subversion-1.9.5-0/svn_users
dev1:$apr1$hb7S2CtJ$GwVa5dEBDzY3EFS/K6yj71
dev2:$apr1$vhccSww7$y.W95.QX5ofsBosFPcxZH0
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

Let me show, if I use -c option:

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -c /opt/subversion-1.9.5-0/svn_users dev3
New password:
Re-type new password:
Adding password for user dev3
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# cat /opt/subversion-1.9.5-0/svn_users
dev3:$apr1$Fa3y4u5Q$INzi63RXzYiThQZ9hm8S.1
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

Create a new file with name "svn_access" (can give according to admin wish)

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# vi /opt/subversion-1.9.5-0/svn_access
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0# cat svn_access
[subversion:/]                        ------ can mention desired directory
dev3=rw
[groups]                        ------ can assign a group to a user

root@ubuntu-new:/opt/subversion-1.9.5-0#

To enable this authentication in our apache server,

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf# vi httpd.conf
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf# cat httpd.conf | tail -12
Include "/opt/subversion-1.9.5-0/apache2/conf/ssi.conf"
Include "/opt/subversion-1.9.5-0/apache2/conf/bitnami/bitnami.conf"
<Location /subversion>
DAV svn
SVNPath "/opt/subversion-1.9.5-0/repository"

AuthType Basic
AuthName "Subversion Project1"
AuthUserFile /opt/subversion-1.9.5-0/svn_users
Require valid-user
AuthzSVNAccessFile /opt/subversion-1.9.5-0/svn_access
</Location>

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf#

Then restart the apache server,

root@ubuntu-new:/opt/subversion-1.9.5-0# ./ctlscript.sh restart
/opt/subversion-1.9.5-0/subversion/scripts/ctl.sh : subversion stopped
Syntax OK
/opt/subversion-1.9.5-0/apache2/scripts/ctl.sh : httpd stopped
Syntax OK
/opt/subversion-1.9.5-0/apache2/scripts/ctl.sh : httpd started at port 81
/opt/subversion-1.9.5-0/subversion/scripts/ctl.sh : subversion started at port 3690

root@ubuntu-new:/opt/subversion-1.9.5-0#

Now try to open in browser, this time server will pop-up the Authentication window....
Use of svn_access file, is to restrict the access permissions of a user...

root@ubuntu-new:/opt/subversion-1.9.5-0# vi svn_access
root@ubuntu-new:/opt/subversion-1.9.5-0#
root@ubuntu-new:/opt/subversion-1.9.5-0#
root@ubuntu-new:/opt/subversion-1.9.5-0# cat svn_access
[subversion:/New folder]
dev3=rw
[groups]
root@ubuntu-new:/opt/subversion-1.9.5-0#

Provided access only to "New folder" for dev3 user...

Before changing access,
After providing new access, he can only view and edit "New folder"
Another admin task is to handle backup and restore of repo.

To take dump,

svnadmin dump /opt/subversion-1.9.5-0/repository/ > /file_loc

To load it,

svnadmin load /opt/subversion-1.9.5-0/repository/ < /file_loc

Similar to these, an admin can restrict commits with some conditions. Admin can restrict few types of files to get added to server.

root@ubuntu-new:/opt/subversion-1.9.5-0/repository# cd hooks
root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks#

In this hooks directory, we have different templates where we can write our scripts to restrict any kind of task...

root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks# ls
post-commit.tmpl  post-revprop-change.tmpl  pre-commit.tmpl  pre-revprop-change.tmpl  start-commit.tmpl
post-lock.tmpl    post-unlock.tmpl          pre-lock.tmpl    pre-unlock.tmpl

root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks#

For example, we can write a script inside "pre-commit.tmpl" which needs to be executed before commiting any type of file/dir...

An admin can perform/restrict different tasks, in this post I covered few of them....

################################################################################

No comments:

Post a Comment