ChatGPT解决这个技术问题 Extra ChatGPT

SVN command to delete all locally missing files

In SVN is there a command I can use to delete all locally missing files in a directory?

Or failing that, some way of listing only those files that are missing (or, in the more general case, have status A, D, ?, etc.)

I maintain 1 working folder, and 1 checkout folder. And move only files from working folder to the check out folder. If a folder must be added/removed, I do it manually and then move files into that folder (for example new packages, old packages). Most svn issues happen when you move folders on your working copy. Even when coping files from your working copy to checkout copy, you should only move files, not folders.

P
Peter Mortensen

If you're using Mac (Darwin) or Linux you can pipe the outputs of the following commands to svn rm for all missing files. You can set the current working directory to the appropriate directory or subdirectory before running these - dependent on whether you want to run this your entire working copy, or only a subset.

Run an svn status Search for lines that begin with "!" (missing) Print the "--force" (svn argument) and the second column (the file name) of the output from #2 Run svn rm using the output of #3 as arguments

So the full command is:

svn st | grep ^! | awk '{print " --force "$2}' | xargs svn rm

References:

Examining fields (columns) with awk

Using xargs to run shell commands with arguments


This doesn't work if there are spaces in the file name. I don't know enough awk to fix it, though.
Use awk '{print " --force "$2"@"}' if your filename contains a @ (if you're an iOS dev for example)
And handle a filename with whitespace(s), awk '{$1=""; print " --force \""substr($0,2)"@\"" }' should do the trick (and begins to look ugly)
The full command taking care of the space is "svn st | grep ^! | awk '{$1=""; print " --force \""substr($0,2)"@\"" }' | xargs svn rm"
Why don't you put the --force into the xargs part?
P
Peter Mortensen

If you are using TortoiseSVN, just do a Check for Modifications, sort by the Status column, select all the entries marked missing, right-click to open the context menu, and select Delete. Finally, commit to publish the changes to the repository.

If you are on Windows, but prefer the command-line and enjoy dabbling in PowerShell, this one-liner will do the trick:

svn status | ? { $_ -match '^!\s+(.*)' } | % { svn rm $Matches[1] }

That is, filter the output to only those lines showing missing files (denoted by an exclamation at the start of the line), capture the associated file name, and perform an svn rm on that file name.

(Blog post Remove all “missing” files from a SVN working copy does something similar for Unix/Linux.)


is there a way in eclipse using subclipse plugin? it is so bad to update a then find them in project and delete them again.
FYI, if a subversion password is needed here is the syntax: svn status --username user_name_here --password password_here | ? { $_ -match '^!\s+(.*)' } | % { svn rm $Matches[1] --username user_name_here--password password_here }
should you first commit after this line before executing svn up ? because the files are restored from the svn server
Will this action delete the files from the repository too?
P
Peter Mortensen
svn st | grep ! | cut -d! -f2| sed 's/^ *//' | sed 's/^/"/g' | sed 's/$/"/g' | xargs svn rm

svn status Filter only on missing files Cut out exclamation point Filter out trailing whitespaces Add leading quote Add trailing quote svn remove each file


This solution works if there are spaces in the files/directories.
How does one add verbosity to this? xargs -verbose svn rm ?
Just a hint for users of scripts etc., this command fails if no files to delete are found: svn: E205001: Try 'svn help delete' for more information svn: E205001: Not enough arguments provided
@ThorstenSchöning's problem can be fixed by adding -r to the xargs options (do not run, if nor arguments are given). Additionally, the numer of arguments should be limited and removals should be batched because the invocation fails with very large number of files to remove, e.g. -n 500. And last but not least, the quoting performed is suboptimal for files containing shell specials like $ - better use \n as delimiter and leave quoting to xargs: svn status | grep "!" | cut -d! -f2 | sed 's/^ *//' | xargs -n 500 -d "\n" -r svn rm
b
brasofilo

I just found this, which does the trick, Remove all “missing” files from a SVN working copy:

svn rm $( svn status | sed -e '/^!/!d' -e 's/^!//' )

Worked for me on OS X. Thanks :)
P
Peter Mortensen

Thanks to Paul Martin for the Windows version.

Here is a slight modification to the script so Windows files with spaces are taken into account as well. Also the missing.list file will be removed at the end.

I saved the following in svndel.bat in my SVN bin directory (set in my %%PATH environment) so it can be called from any folder at the command prompt.

### svndel.bat
svn status | findstr /R "^!" > missing.list
for /F "tokens=* delims=! " %%A in (missing.list) do (svn delete "%%A")
del missing.list 2>NUL

nice to see some innovative batch code still appearing :)
Wow, great answer. I was about to go with powershell, but this was so much easier.
I added another line after del missing.list 2>NUL, because I added all new files directly after first removing the deleted files so it added missing.list to svn: svn delete "missing.list" worked for me
I had modified this script to attempt to run from an arbitrary location then encountered this issue stackoverflow.com/questions/1137219/… so it appears you MUST use the Current Working Directory to perform this action.
P
Peter Mortensen

I like the PowerShell option... But here's another option if you're using Windows batch scripts:

svn status | findstr /R "^!" > missing.list
for /F "tokens=2 delims= " %%A in (missing.list) do (svn delete %%A)

A
Amfasis

It is actually possible to completely remove the missing.list from user3689460 and Paul Martin

for /F "tokens=* delims=! " %%A in ('svn status ^| findstr /R "^!"') do (svn delete "%%A")

for reference - this is DOS / cmd.exe in windows land
g
gilyen

An alternative that works on Linux (bash) for to-be-removed files not containg spaces in path:

svn delete `svn status | grep ! | awk '{print $2}'`

c
cibercitizen1

This shell script, recursively examines (svn status) directories in your project, removing missing files (as the question demands) and adding new files to the repository. It is some sort of "store into the repository the current snapshot of the project".

if [ $# != 1 ]
then
    echo  "usage: doSVNsnapshot.sh DIR"
    exit 0
fi

ROOT=$1

for i in `find ${ROOT} -type d \! -path "*.svn*" `
do

    echo
    echo "--------------------------"
    ( cd $i ; 
    echo $i
    echo "--------------------------"


    svn status | awk '  
            /^[!]/ { system("svn rm " $2) }
            /^[?]/ { system("svn add " $2) }
        '
    )
    echo

done

P
Peter Mortensen

A slight modification of the command line, which works on Mac OS (hopefully even on Linux) and copes with the files the command "svm sr" reports, like "!M" (missing and modified).

It copes with spaces in the files.

It is based on a modification of a previous answer:

svn st | grep ! | sed 's/!M/!/' | cut -d! -f2| sed 's/^ *//' | sed 's/^/"/g' | sed 's/$/"/g' | xargs svn --force rm

J
Jonas Eberle

When dealing with a lot of files, it can happen that the argument input to xargs is getting too long. I went for a more naive implementation which works in that case, too.

This is for Linux:

#! /bin/bash
# 1. get all statii in the working copy
# 2. filter out only missing files
# 3. cut off the status indicator (!) and only return filepaths
MISSING_PATHS=$(svn status $1 | grep -E '^!' | awk '{print $2}')
# iterate over filepaths
for MISSING_PATH in $MISSING_PATHS; do
    echo $MISSING_PATH
    svn rm --force "$MISSING_PATH"
done

Z
Zeta

Improved Version

So the full command is:

svn st | grep ^! | sed 's/![[:space:]]*//' |tr '\n' '\0' | xargs -0 svn --force rm

The part "sed 's/![[:space:]]*//'" should read "sed 's/^![[:space:]]*//'" (notice the extra caret "^") so that it only matches "!" + spaces at the beginning of each line and not in the middle.