Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: bernardpaulus/mvedit
base: b965ddbe09
...
head fork: bernardpaulus/mvedit
compare: 50863f4dfe
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 56 additions and 20 deletions.
  1. +3 −6 README.md
  2. +53 −14 mvedit
View
9 README.md
@@ -10,15 +10,12 @@ This opens an editor with one line per file of the current directory.
Happy directory editing!
-Contribute
-==========
-Any help is welcomed, especially with the bugs :)
-See Bugs below, or TODO 's in the file.
+When you're done, any help/idea/contribution is welcome!
+Cookies also are, but their rate of survival is quite low :D
Bugs
======
-Beware, special things like exchanging the names of two files or moving two
-files to the same filename is still not handled.
+No known bug! Yaay :D
View
67 mvedit
@@ -2,9 +2,8 @@
# TODO:
# - git/svn support
# - whitespace lines as delete?
-# - two files exchange names
-# - two target files have the same name
-# - editor switch in options (use nano if no $EDITOR value
+# - editor switch in options (use nano if no $EDITOR value is not set)
+# - improve performance: printlineat is quite the ugly thing
if [ "$1" == "-h" -o "$1" == "-help" -o "$1" == "--help" ]
then
echo "moves multiple files at once by letting you edit the directory listing"
@@ -17,14 +16,6 @@ then
exit 0
fi
-# easy directory support
-function rmdir_or_rm() {
- if [ -d "$1" ]
- then rmdir "$1"
- else rm "$1"
- fi
-}
-
quiet="false"
if [ "$1" == "-q" -o "$1" == "--quiet" ]
then
@@ -39,6 +30,30 @@ then
exit -1
fi
+# rm file or empty directories
+function rmdir_or_rm() {
+ if [ -d "$1" ]
+ then rmdir "$1"
+ else rm "$1"
+ fi
+}
+
+# create a unique directory and returns it's name in $dirname
+function unique_dir() {
+ if [ -w . ]
+ then if [ ! -e "$1" ]
+ then
+ mkdir --mode=700 "$1" && dirname="$1"
+ else
+ unique_dir "$1$RANDOM"
+ fi
+ else
+ mkdir "$1" # fail and print error
+ fi
+}
+dirname=""
+
+
# prints line of file $1 at line $2
function printlineat {
cat -n "$1" | awk "\$1 == $2 {print;}" | cut --complement --fields=1
@@ -48,11 +63,18 @@ tmpfile="/tmp/mveditsource$RANDOM.tmp"
editfilename="mvedit_edit$RANDOM.tmp"
editfile="/tmp/$editfilename"
backup_on_error="$editfilename.bak"
-MV_COMMAND="mv"
+MV_COMMAND="mv -i"
RM_COMMAND="rmdir_or_rm"
-ls -1a | grep -v '^\.$' | grep -v '^\.\.$' > "$tmpfile"
+
+ls -1A > "$tmpfile"
cp "$tmpfile" "$editfile"
$EDITOR "$editfile"
+
+unique_dir "mvedit_clean_dir$RANDOM"
+if [ -n "$dirname" ]
+then clean_dir="$dirname"
+else exit -1
+fi
nlinestmp=`wc -l "$tmpfile" | awk '{print $1}'`
nlinesedit=`wc -l "$editfile" | awk '{print $1}'`
if [ $nlinestmp -eq $nlinesedit ]
@@ -69,7 +91,9 @@ then
$RM_COMMAND "$orig"
else
[ "$quiet" == "false" ] && echo "$i/$nlinesedit: MV $orig $target"
- $MV_COMMAND "$orig" "$target"
+ # move to clean dir to avoid conflicts with filename exchanges
+ # but stay on the current filesystem
+ $MV_COMMAND "$orig" "$clean_dir/$target"
fi
fi
done
@@ -79,5 +103,20 @@ else
echo "> Saved your version in $backup_on_error"
fi
+if [ -n "$(ls -A $clean_dir)" ]
+then
+ for f in "$clean_dir/."* "$clean_dir/"*
+ do
+ if [ "$f" != "$clean_dir/." -a "$f" != "$clean_dir/.." ]
+ then $MV_COMMAND "$f" .
+ fi
+ done
+fi
+
+if [ -z "$(ls -A $clean_dir)" ]
+then rmdir "$clean_dir"
+else echo "files that should have overwritten files are in $clean_dir/"
+fi
+
rm $tmpfile $editfile

No commit comments for this range

Something went wrong with that request. Please try again.