Previously, if we did e.g.
rename '' _ ./foo.txt
it would try to rename to ._/foo.txt, because `string_replace` was
passed `s="/foo.txt"` and `from` would match at the front. Now we pass
`s="foo.txt"`.
This doesn't affect the case when `to` contains a slash. In that
situation we'll still place `to` at the very beginning of the path, e.g.
rename '' _/ ./foo.txt # rename to _/./foo.txt
rename '' /_ ./foo.txt # rename to /_./foo.txt
warn(_("stat of %s failed"), s);
return 2;
}
- if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
+ if (strchr(from, '/') == NULL && strchr(to, '/') == NULL) {
file = strrchr(s, '/');
+ /* We're going to search for `from` in `file`. If `from` is
+ empty, we don't want it to match before the '/'. */
+ if (file != NULL)
+ file++;
+ }
if (file == NULL)
file = s;
if (string_replace(from, to, file, s, &newname) != 0)
rename_path_a
rename_path_b
rename_path_b/test2
+== empty 'from' ==
+`rename_test' -> `_rename_test'
+`./rename_test' -> `./_rename_test'
+`rename_test' -> `rename_subdir/rename_test'
+`./rename_test' -> `rename_subdir/./rename_test'
rmdir rename_path_a rename_path_b
+echo "== empty 'from' ==" >> $TS_OUTPUT
+
+touch rename_test
+$TS_CMD_RENAME -v '' _ rename_test >> $TS_OUTPUT 2>> $TS_ERRLOG
+rm -f *rename_test
+
+touch rename_test
+$TS_CMD_RENAME -v '' _ ./rename_test >> $TS_OUTPUT 2>> $TS_ERRLOG
+rm -f *rename_test
+
+touch rename_test
+mkdir rename_subdir
+$TS_CMD_RENAME -v '' rename_subdir/ rename_test >> $TS_OUTPUT 2>> $TS_ERRLOG
+rm -rf rename_subdir
+
+touch rename_test
+mkdir rename_subdir
+$TS_CMD_RENAME -v '' rename_subdir/ ./rename_test >> $TS_OUTPUT 2>> $TS_ERRLOG
+rm -rf rename_subdir
+
ts_finalize