简体   繁体   中英

Powershell: SVN revert for all files matching a given pattern

I accidentally added dozens of backup files to SVN and would like to recursively revert all files matching a given pattern (*.~sql)

On UNIX, I'd simply use a

svn revert `svn status .|grep "~sql$"|awk '{print $2}'`

which is a variation of one of the answers to How do I 'svn add' all unversioned files to SVN?

Using Powershell, I came up with

svn status|findstr "~sql" | %{ $_.Split('`t')[1]; }

but this seems to cut of the first characters of the filenames (eg I get ch_sys.~sql instead of scratch_sys.~sql).

What am I missing here? Or is there a more Powershellish way of doing this?

You would need to use

$_.Split("`t")

as a quick fix for your code because currently you have a string containing `t instead of a string containing a tab because single-quoted strings don't allow escape sequences (except "" ). string.Split gets a char[] as argument and splits at any of the characters you passed, so in this case it splits on a grave accent and t .

But you are right, that's not really a PowerShell-y way of doing this. First of all, you're using findstr which has plenty of PowerShell equivalents, eg

svn status | where { $_ -match '~sql' }
(svn status) -match '~sql'

When working with SVN from PowerShell you have another option, though. Most SVN commands can output XML which is a lot more robust to handle than text output, usually.

([xml](svn status --xml)).status.target.entry |
  select -exp path |
  where {$_ -match '~sql'} |
  foreach { svn revert $_ }

Whether that's prettier is probably debatable, but I prefer working with structured data when possible.

However,

svn revert -R *~sql*

should work, too, I guess.

If you use Select-String, alias sls you can also use regex for matching.

([xml](svn status --xml)).status.target.entry.path | sls ".+sql" | %{svn revert $_}

(nb $_ is a matchinfo that gets converted into a string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM