50

I am writing a script which will add a new project in the repository, based on the name supplied by the user. Part of this involves checking that an url with the same name does not already exist on the repository.

In the repository, all the projects of our team are stored in

https://developernetwork.repo.net/svn/Projects/

Let's say that the user wants to call the project "Calculator" and runs the script. In this case, we need to ensure that the following does not already exist in the repository

https://developernetwork.repo.net/svn/Projects/Calculator/

Is there an svn command which I can use to accomplish that? Unfortunately I cannot see an appropriate command I can use in the svn documentation (http://svnbook.red-bean.com/en/1.0/svn-book.html) at all.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Andy
  • 2,770
  • 9
  • 35
  • 42
  • 3
    The documentation you link to is way out of date, for svn 1.0. Read http://svnbook.red-bean.com/nightly/en/index.html for the latest docs. – Wim Coenen May 15 '09 at 18:44

6 Answers6

52

Instead of checking for return strings I would just check for the return code:

both

svn ls REPOSITORY/PATH

and

svn info REPOSITORY/PATH

return 0 if all went fine, and 1 if things went wrong; you can quickly check it out:

echo $?

so in a bash script just do something like:

error=$?
if [ $error -ne 0 ]; then
      YOUR ERROR HANDLING
fi

works like a treat!

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Stefano
  • 18,083
  • 13
  • 64
  • 79
  • Thank you for your comment. Is it even possible to do that in the middle of a Perl script. In any case, this has become an academic question as the project I was working on has been scrapped! – Andy Apr 01 '11 at 09:36
48

You can just use

svn ls https://developernetwork.repo.net/svn/Projects/Calculator/

It will tell you if the repository(directory) exist or not.

Update

for directories with many files use:

svn ls http://server/svn/foo --depth empty
vinnyjames
  • 2,040
  • 18
  • 26
Liwen
  • 937
  • 10
  • 12
  • This works. If the directory does not exist I get the following error: svn: URL 'https://developernetwork.repo.net/svn/Projects/Calculator/ ' non-existent in that revision. That should be a good enough search string. Just out of interest, is there a better way for the script to analyse the results than searching the text output for a "non-existent in that revision" match? – Andy May 15 '09 at 13:11
  • As suggested in the other comment, searching for 'non-existent' might not be ideal as the error msg changes from version to version, e.g. svn 1.6.1 has changed the error msg to 'svn: No repository found in 'svn+ssh://'. However, parsing xml is not reliable as of svn 1.6.1, if the repository doesn't exsit, it WILL return an incomplete xml which is not parsable. That would make it worse. The best way I can think probably is just to check if the original repository url you sent in is in the return msg. It seems to be guranteed that it will get returned if something went wrong. – Liwen May 16 '09 at 12:38
  • Thank you. Having to analyse the text output of commands is one of the least enjoyable aspects of scripting, but it has to be done. I will check for the repository url in the return message then. – Andy May 19 '09 at 14:35
  • 2 Andy: use --xml option and some XML-parsing library. – Denis Barmenkov Jun 22 '14 at 15:38
  • 1
    @Andy If you are using shell script then just look at `$?` It contains return code for previous command svn ls return "1" if it fails. – Xk0nSid Apr 21 '16 at 09:30
  • 1
    For future reference, return code appears to be 0 if the dir exists, nonzero if not, at least in v1.10.0-dev. – Tydaeus Apr 03 '19 at 14:59
9

To receive information about any existing repository (e.g. for possibly enriching an error message) you could also use

svn info https://developernetwork.repo.net/svn/Projects/Calculator/

For a non-existing project it will just return

svn: Could not open the requested SVN filesystem
amain
  • 1,668
  • 13
  • 19
  • Actually, my version of Slik SVN returned: https://developernetwork.repo.net/svn/Projects/Calculator/ : (Not a valid URL) svn: A problem occurred; see other errors for details However, "Not a valid URL" should be good enough search string. Just out of interest, is there a better way for the script to analyse the results than searching the text output for a "Not a valid URL" string? – Andy May 15 '09 at 13:09
  • Given the different possible error messages different build of svn might give, you should consider to parse the output string for a positive pattern rather than a negative. That is, test for the occurrence of something you won't get when the repo does NOT exist. Here, CollabNet SVN 1.6.1 (Windows) provides the ability to turn on xml output via the `--xml` parameter. Personally I'd just check for the existence of the UUID element, that should be included in any `info` data (correct me if not). – amain May 15 '09 at 14:27
  • Thank you. We do not even have server 1.5.0 running yet, so I will note down your comment and put this in the to-do list. – Andy May 15 '09 at 14:56
2

I face the above problem and i tried all the way. not work for me.. if we access svn with a path not in svn it's failed with error and the shell script will break.

Instead of accessing unknown path, what i did is get the directory list of parent folder and check whether matching one contain or not.

Get all the projects form your Parent directory (if you want to find a specific tags then your parent directory path should be https://developernetwork.repo.net/svn/Projects/Calculator/tags)

projects=$(svn ls https://developernetwork.repo.net/svn/Projects/)

then check your project folder exists. Remember to add / after your project name.

projectName="Calculator/"
for i in $projects; do 
    if [ "$i" == "$projectName/" ];
        then
        echo Project Exist
        else
        echo Project does not exist
    fi
done 

simple.

damithH
  • 5,148
  • 2
  • 27
  • 31
1

I'd have commented on an existing answer but I don't have sufficient reputation.

I wanted to find if a file had been imported successfully or not so I tried "svn info" and "svn ls" with "echo $?". I could only get a zero/non-zero return code when I used "svn ls": "svn info" always returned zero.

This was with subversion 1.5.5 on Redhat RHEL6.

A Scott
  • 11
  • 2
0

Like @A Scott says, the echo $? can't be used to know wether the URL is correct (I'm on Mac OS 10.11 and SVN client 1.9.5).

For potential future readers, i use this workaround :

content=$(svn info $SVN_TAG_URL)

if [[ -z $content ]]; then
    echo "The SVN URL doesn't exist"; exit
fi
pom421
  • 1,731
  • 19
  • 38