开发者

Add specific source file types to svn recursively

开发者 https://www.devze.com 2023-04-13 02:17 出处:网络
I know this is an ugly script but it does the job. What I am facing now is adding a few more extensions what would clutter the scrip even more.

I know this is an ugly script but it does the job. What I am facing now is adding a few more extensions what would clutter the scrip even more. How can I make it more modular? Specifically, how can I write this long regular expression (source file extensions) on multiple lines? say one extension on each line. I guess I am doing something wrong with string concatenation but not quite sure what exactly. Here's the original file:

#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVN='svn add'
$COMMAND | grep -E '(\.m|\.mat|\.java|\.js|\.php|\.cpp|\.h|\.c|\.py|\.hs|\.pl|\.xml|\.html|\.sh|.\asm|\.s|\.tex|\.bib|.\开发者_StackOverflowMakefile|.\jpg|.\gif|.\png|.\css)'$ | awk ' { print$2 } ' | $XARGS $SVN

and here's roughly what I am aiming at

...code code
'(.\m|
\.mat|
\.js|
.
.
.
.\css)'
..more code here

Anybody?


I know this doesn't answer the question directly, but from a readability perspective, I think most developers would agree that a single-line regex is the most common way to do things and therefore the most maintainable approach. Further, I'm not sure why you're including a period in each of your extensions, this should only need to be used once.

I wrote this little script to automatically add all images to svn. You should be able to simply add extensions between the pipes in the regex to add or remove different file types. Note that it makes sure to only add files that are unrecognized by making sure each line starts with a "?" (^\?) and ends with a period (\.(extensions)$). Hope it's helpful!

#!/bin/bash

svn st | grep -E "^\?.*\.(png|jpg|jpeg|tiff|bmp|gif)$" > /tmp/svn-auto-add-img

while read output; do
    FILE=$(echo $output | awk '{ print $2 }')
    svn add $FILE
done < /tmp/svn-auto-add-img

exit 0


How about this:

PATTERNS="
\.foo
\.bar
\.baz"

# Put them into one list separated by or ("|").
PATTERNS=`echo $PATTERNS |sed 's/\s\+/|/g'`

$COMMAND | grep -E "($PATTERNS)"

(Note that this would not work if you put quotes around $PATTERNS in the call to echo -- echo is taking care of stripping whitespace and converting newlines to spaces for us.)


#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVNADD='svn add'
pats=
pats+=' \.m'
pats+=' \.mat'
pats+=' \.java'
pats+=' \.js'

# add your 'or-able' sub patterns here

# build the full pattern 
pattern='(';for pat in $pats;do pattern+="$pat|";done;pattern=${pattern%\|}')$'

# run grep with the generated pattern
files=$($COMMAND | grep -E ${pattern} | awk ' { print $NF } ')
if [ " $files" != " " ]
then
   $COMMAND | grep -E ${pattern} | awk ' { print $NF } ' | $XARGS $SVNADD
fi
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号