Modify single file in SVN tree

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

1#!/bin/sh
2# Empties current directory and extracts single file/folder from svn
4 
5die () {
6 echo >&2 "$@"
7 exit 1
8}
9 
10usage=$"This scripts extract single file/folder from SVN repository into the current directory,\n\
11allowing to modify it locally without extracting all other sibling files and directories\n\
12\n\
13$0 svn_uri\n\
14"
15 
16[ "$#" -ge 1 ] || die "SVN URI is not provided. $usage"
17fullUri=$1
18 
19# Testing is svn CLI is present
20svnRun=`which svn 2> /dev/null`
21if [ ! $svnRun ]
22then
23 die "svn CLI is not found on PATH"
24fi
25 
26# determine parent folder and file from the give URI
27svnFile=${fullUri##*/}
28svnParent=${fullUri%%/${svnFile}}
29 
30# remove .svn and the file if they exist
31rm -rf .svn
32rm -f $svnFile
33 
34# extract the file
35svn checkout -q --depth=empty $svnParent .
36svn update $svnFile
37 
38echo "\n\
39After $svnFile is changed locally commit it using\n\
40 svn commit -m \"your update description\""

Leave a Reply

Your email address will not be published.

Please, enter correct number below to prevent spam (required)