initial commit
[mi.git] / mi
blob658b38f978ba5b57a0592138c9c0cbf5a8c748a5
1 #!/bin/sh
3 # mi
5 # 28-Jan-2009 Clemens Buchacher <clemens.buchacher@infineon.com>
7 # BUGS
9 # o name conflicts
11 # If two files have the same target, or if files 'a', 'b', are renamed to
12 # 'b', 'c', respectively, in the wrong order. The file may be overwritten.
14 # o subentry conflicts
16 # If both a directory and its contained files are moved, this can have
17 # unexpected results.
20 prog=$0
22 error()
24 echo -n "$prog: error: "
25 echo $@ >&2
28 OPT=`getopt -o f -- "$@"` || exit 1
29 eval set -- "$OPT"
31 force=
32 done=
33 while test "$1" != "--"
35 case "$1" in
36 -f) force=YesPlease;;
37 *) echo "unkown option: $1"; exit 1;;
38 esac
39 shift
40 done
41 shift
43 if test -z "$1"
44 then
45 set -- `ls`
47 orignames=`mktemp`
48 while test -n "$1"
50 echo `echo $1 | git hash-object --stdin` $1
51 shift
52 done > "$orignames"
54 newnames=`mktemp`
56 echo "# This program is under development. Use at your own risk!";
57 echo "# Remove all content to abort";
58 cat "$orignames"
59 ) > $newnames
60 if ! $EDITOR "$newnames"
61 then
62 error "aborted"
63 exit 1
66 oldlines=`mktemp`
67 while read sha1 newname
69 if test -z "$sha1" || expr "$sha1" : "\s*#.*" >/dev/null
70 then
71 continue
73 if ! expr "$sha1" : '[0-9a-f]\{40\}$' >/dev/null
74 then
75 error "invalid hash: $sha1"
77 oldname=
78 sed -n "s/^$sha1 //p" "$orignames" > $oldlines
79 while read name
81 if test -n "$oldname"
82 then
83 error "duplicate hash: $sha1"
85 oldname="$name"
86 done < $oldlines
87 if test -z "$oldname"
88 then
89 error "invalid hash: $sha1"
90 else
91 if test "$oldname" != "$newname"
92 then
93 echo "$oldname -> $newname"
94 if test -z "$force" && test -e "$newname"
95 then
96 error "rename would overwrite existing file: $newname"
97 else
98 mv -f "$oldname" "$newname"
102 done < "$newnames"
104 rm -f "$newnames"
105 rm -f "$orignames"