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

Snippets has posted 5883 posts at DZone. View Full User Profile

Svn Move Afterwards

08.27.2009
| 2356 views |
  • submit to reddit
        
#!/usr/bin/python

# Author: Raphael Jolivet
# Date  : 25-Aug-2009
# 
# Purpose:
#   This script enables to make svn aware of change of file locations, after having used "mv".
#   Normally, one should use 'svn move' to move a file that is under version control.
#   But sometimes, you just forget to do so.
#   This script detect that files have moved afterwards, and uses svn to move them properly.
#
# Usage:
#   svn-move-afterwards.py
#  
#   This script takes no option.
#   It works in the current directory.


import commands
from os.path import *
import os
import sys

# Get the list given by 'svn status'
list=commands.getoutput('svn status')

removed={}
added={}

# Parse the list : Gather removed and added files
for line in list.splitlines():

    (flag, path)=line.split()
    file=basename(path)
    dir=dirname(path)

    # Added file ?
    if flag=='?' : 
        added[file]=dir

    # Removed file
    if flag=='!':
        removed[file]=dir

# Loop on removed files
for file in removed:
   
    # File found in "added" files ? It has moved 
    if added.has_key(file):
        
        # Compute full paths
        srcDir=removed[file]
        destDir=added[file]
        src=join(srcDir, file)
        dest=join(destDir, file)

        # Commands
        mv_cmd='mv "%s" "%s"' % (dest, src) # Move back to original location
        svn_cmd='svn move "%s" "%s"' % (src, dest) # Use SVN move 

        # Execute commands
        res=os.system(mv_cmd)
        if (res!=0) :
            print "Error while executing : %s.\nAborting." % mv_cmd
            sys.exit(-1)
            
        res=os.system(svn_cmd)
        if (res!=0) :
            print "Error while executing : %s.\nAborting." % svn_cmd
            sys.exit(-1)