Batch Script to Connect and Checkout the SVN branch

Batch Script to Connect and Checkout the SVN branch



To connect to an SVN repository and checkout a specific branch using batch script, you can use the svn command-line client along with batch scripts. Make sure you have the SVN command-line client installed on your Windows system.


Here the batch script that reads the SVN credentials and URL from a configuration file named co_config.txt.

Please follow the below steps:


1. Configuration File

Create a co_config.txt file in the same folder as your script with the following content:


  
    SVN_URL=http://your_svn_url/

    USERNAME=your_username

    PASSWORD=Your_password

    CHECKOUT_DIR=C:\path\to\checkout\directory
  


Update the values in co_config.txt with your SVN repository URL, username, password, and the local directory where you want to check out the repository.




2. Batch Script (checkout_svn.bat)

Create a batch script named checkout_svn.bat with the following content:

  
    @echo off

setlocal enabledelayedexpansion



rem Path to the config file

set CONFIG_FILE=co_config.txt



rem Check if config file exists

if not exist "%CONFIG_FILE%" (

    echo Config file %CONFIG_FILE% not found!

    exit /b 1

)



rem Read the config file and set variables

for /f "delims== tokens=1,2" %%A in (%CONFIG_FILE%) do (

    set %%A=%%B

)



rem Verify all required variables are set

if "%SVN_URL%"=="" (

    echo SVN_URL is not set in the config file!

    exit /b 1

)

if "%USERNAME%"=="" (

    echo USERNAME is not set in the config file!

    exit /b 1

)

if "%PASSWORD%"=="" (

    echo PASSWORD is not set in the config file!

    exit /b 1

)

if "%CHECKOUT_DIR%"=="" (

    echo CHECKOUT_DIR is not set in the config file!

    exit /b 1

)



rem Perform SVN checkout

svn checkout "%SVN_URL%" "%CHECKOUT_DIR%" --username "%USERNAME%" --password "%PASSWORD%" --non-interactive --trust-server-cert



rem Check the result of the SVN command

if errorlevel 1 (

    echo SVN checkout failed!

    exit /b 1

)



echo SVN checkout completed successfully!



endlocal

exit /b 0

  


Explanation

Config File Reading: The batch script reads the co_config.txt file line by line. For each line, it splits the line at the = character and sets the corresponding environment variable.


Variable Validation: The script checks if all required variables (SVN_URL, USERNAME, PASSWORD, CHECKOUT_DIR) are set. If any of these are missing, it will display an error message and exit.


SVN Checkout: The svn checkout command is executed using the parameters read from the config file. The --non-interactive and --trust-server-cert options are used to suppress prompts and automatically trust the server certificate.


Error Handling: The script checks if the SVN command was successful by examining the errorlevel. If the checkout fails, an error message is displayed.


How to Use

Place both co_config.txt and checkout_svn.bat in the same directory.

Update the values in co_config.txt with your SVN repository URL, username, password, and the local directory where you want to check out the repository.

Run the checkout_svn.bat script.

This setup ensures that your SVN checkout process is configurable and secure by keeping sensitive information in a separate, easily editable configuration file.





Post a Comment (0)
Previous Post Next Post

About