DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Viktor is an experienced Technical Project Leader, Certified Scrum Master and SUN Certified Business Component (EJB) Developer with Master Degree in Computer Science. He is also founder of JV-ration, which believes Continuous Integration and Continuous Delivery environments should become an easy to get and to use commodity Viktor has posted 2 posts at DZone. You can read more from them at their website. View Full User Profile

SVN. Update a single file without checking out the entire source tree

12.02.2013
| 3161 views |
  • submit to reddit
#!/bin/sh
# Empties current directory and extracts single file/folder from svn
# see complete description on http://jv-ration.com/2013/11/modify-single-file-in-svn-tree

die () {
    echo >&2 "$@"
    exit 1
}

usage=$"This scripts extract single file/folder from SVN repository into the current directory,\n\
allowing to modify it locally without extracting all other sibling files and directories\n\
\n\
$0 svn_uri\n\
"

[ "$#" -ge 1 ] || die "SVN URI is not provided. $usage"
fullUri=$1

# Testing is svn CLI is present
svnRun=`which svn 2> /dev/null`
if [ ! $svnRun ]
then
  die "svn CLI is not found on PATH"
fi  

# determine parent folder and file from the give URI
svnFile=${fullUri##*/}
svnParent=${fullUri%%/${svnFile}}

# remove .svn and the file if they exist
rm -rf .svn
rm -f $svnFile

# extract the file
svn checkout -q --depth=empty $svnParent .
svn update $svnFile

echo "\n\
After $svnFile is changed locally commit it using\n\
  svn commit -m \"your update description\""

Sometimes, especially when you work cross various projects, you need to modify a single file in a project. A good  example for this updating pom.xml by adding description, name, SCM or CI sections.

However majority of UI tools do not provide a possibility to extract a single file, avoiding checking out all its siblings recursively. Commands of CLI enable this, but they are not well known.

The idea is to checkout the containing folder with ‘empty’ depth and then update the file you are interested in. For simplicity of use, I wrote a shell script, which take the URI to the file is SVN, and extracts it to the current directory. The URI of the file can be copied from a browser. The script is also available in Bitbucket