Compare commits

..

No commits in common. "main" and "v0.1.0" have entirely different histories.
main ... v0.1.0

12 changed files with 187 additions and 462 deletions

View file

@ -1,13 +1,6 @@
name: CI name: CI
on: on: [push, pull_request]
push:
branches:
- main
pull_request:
permissions:
contents: read
jobs: jobs:
test: test:
@ -17,13 +10,15 @@ jobs:
matrix: matrix:
os: [ubuntu-latest, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
steps: steps:
- name: Checkout code - uses: actions/checkout@master
uses: actions/checkout@v3
- name: Setup Go - name: Setup Go
uses: actions/setup-go@v4 uses: actions/setup-go@v1
with: with:
go-version: 1.x go-version: 1.x
- name: Test - name: Test
run: make test run: make test
- name: Lint - name: Lint
run: make lint run: |
export PATH=$PATH:$(go env GOPATH)/bin # will be removed
make lint
if: matrix.os != 'windows-latest'

View file

@ -5,24 +5,32 @@ on:
tags: tags:
- 'v*' - 'v*'
permissions:
contents: write
jobs: jobs:
release: release:
name: Release name: Release
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 uses: actions/checkout@master
- name: Setup Go - name: Setup Go
uses: actions/setup-go@v4 uses: actions/setup-go@v1
with: with:
go-version: 1.x go-version: 1.x
- name: Cross build - name: Cross build
run: make cross run: |
export PATH=$PATH:$(go env GOPATH)/bin # remove when actions/setup-go does this
make cross
- name: Create Release - name: Create Release
uses: ncipollo/release-action@v1 id: create_release
uses: actions/create-release@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
name: Release ${{ github.ref_name }} tag_name: ${{ github.ref }}
artifacts: 'goxz/*' release_name: Release ${{ github.ref }}
- name: Upload
run: |
export PATH=$PATH:$(go env GOPATH)/bin # remove when actions/setup-go does this
make upload
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

6
.gitignore vendored
View file

@ -1,6 +1,2 @@
/mmv
/goxz /goxz
/CREDITS /mmv
*.exe
*.test
*.out

View file

@ -1,22 +0,0 @@
# Changelog
## [v0.1.6](https://github.com/itchyny/mmv/compare/v0.1.5..v0.1.6) (2023-04-26)
* Remove dependency on shell for splitting EDITOR with spaces.
## [v0.1.5](https://github.com/itchyny/mmv/compare/v0.1.4..v0.1.5) (2023-04-09)
* Support EDITOR with spaces in the editor path.
## [v0.1.4](https://github.com/itchyny/mmv/compare/v0.1.3..v0.1.4) (2021-09-18)
* Release `arm64` artifacts.
## [v0.1.3](https://github.com/itchyny/mmv/compare/v0.1.2..v0.1.3) (2021-01-10)
* Support renaming when one of the paths is a parent directory of another.
## [v0.1.2](https://github.com/itchyny/mmv/compare/v0.1.1..v0.1.2) (2020-09-19)
* Fix for EDITOR configured with arguments.
## [v0.1.1](https://github.com/itchyny/mmv/compare/v0.1.0..v0.1.1) (2020-01-09)
* Undo the processed renames on error not to leave the temporary file.
* Return the stat source error on failure.
## [v0.1.0](https://github.com/itchyny/mmv/compare/ac88fa9...v0.1.0) (2020-01-08)
* Initial implementation.

View file

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2020-2023 itchyny Copyright (c) 2020 itchyny
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View file

@ -1,12 +1,13 @@
BIN := mmv BIN := mmv
VERSION := $$(make -s show-version) VERSION := $$(make -s show-version)
VERSION_PATH := cmd/$(BIN) VERSION_PATH := cmd/$(BIN)
CURRENT_REVISION = $(shell git rev-parse --short HEAD) CURRENT_REVISION := $(shell git rev-parse --short HEAD)
BUILD_LDFLAGS = "-s -w -X main.revision=$(CURRENT_REVISION)" BUILD_LDFLAGS := "-s -w -X main.revision=$(CURRENT_REVISION)"
GOBIN ?= $(shell go env GOPATH)/bin GOBIN ?= $(shell go env GOPATH)/bin
export GO111MODULE=on
.PHONY: all .PHONY: all
all: build all: clean build
.PHONY: build .PHONY: build
build: build:
@ -14,51 +15,56 @@ build:
.PHONY: install .PHONY: install
install: install:
go install -ldflags=$(BUILD_LDFLAGS) ./cmd/$(BIN) go install -ldflags=$(BUILD_LDFLAGS) ./...
.PHONY: show-version .PHONY: show-version
show-version: $(GOBIN)/gobump show-version: $(GOBIN)/gobump
@gobump show -r "$(VERSION_PATH)" @gobump show -r $(VERSION_PATH)
$(GOBIN)/gobump: $(GOBIN)/gobump:
@go install github.com/x-motemen/gobump/cmd/gobump@latest @cd && go get github.com/motemen/gobump/cmd/gobump
.PHONY: cross .PHONY: cross
cross: $(GOBIN)/goxz CREDITS cross: $(GOBIN)/goxz
goxz -n $(BIN) -pv=v$(VERSION) -build-ldflags=$(BUILD_LDFLAGS) ./cmd/$(BIN) goxz -n $(BIN) -pv=v$(VERSION) -build-ldflags=$(BUILD_LDFLAGS) ./cmd/$(BIN)
$(GOBIN)/goxz: $(GOBIN)/goxz:
go install github.com/Songmu/goxz/cmd/goxz@latest cd && go get github.com/Songmu/goxz/cmd/goxz
CREDITS: $(GOBIN)/gocredits go.sum
go mod tidy
gocredits -w .
$(GOBIN)/gocredits:
go install github.com/Songmu/gocredits/cmd/gocredits@latest
.PHONY: test .PHONY: test
test: build test: build
go test -v -race ./... go test -v ./...
.PHONY: lint .PHONY: lint
lint: $(GOBIN)/staticcheck lint: $(GOBIN)/golint
go vet ./... go vet ./...
staticcheck -checks all ./... golint -set_exit_status ./...
$(GOBIN)/staticcheck: $(GOBIN)/golint:
go install honnef.co/go/tools/cmd/staticcheck@latest cd && go get golang.org/x/lint/golint
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf $(BIN) goxz CREDITS rm -rf $(BIN) goxz
go clean go clean
.PHONY: bump .PHONY: bump
bump: $(GOBIN)/gobump bump: $(GOBIN)/gobump
test -z "$$(git status --porcelain || echo .)" ifneq ($(shell git status --porcelain),)
test "$$(git branch --show-current)" = "main" $(error git workspace is dirty)
endif
ifneq ($(shell git rev-parse --abbrev-ref HEAD),master)
$(error current branch is not master)
endif
@gobump up -w "$(VERSION_PATH)" @gobump up -w "$(VERSION_PATH)"
git commit -am "bump up version to $(VERSION)" git commit -am "bump up version to $(VERSION)"
git tag "v$(VERSION)" git tag "v$(VERSION)"
git push --atomic origin main tag "v$(VERSION)" git push origin master
git push origin "refs/tags/v$(VERSION)"
.PHONY: upload
upload: $(GOBIN)/ghr
ghr "v$(VERSION)" goxz
$(GOBIN)/ghr:
cd && go get github.com/tcnksm/ghr

View file

@ -1,14 +1,5 @@
# mmv # mmv [![CI Status](https://github.com/itchyny/mmv/workflows/CI/badge.svg)](https://github.com/itchyny/mmv/actions)
[![CI Status](https://github.com/itchyny/mmv/workflows/CI/badge.svg)](https://github.com/itchyny/mmv/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/itchyny/mmv)](https://goreportcard.com/report/github.com/itchyny/mmv)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/itchyny/mmv/blob/main/LICENSE)
[![release](https://img.shields.io/github/release/itchyny/mmv/all.svg)](https://github.com/itchyny/mmv/releases)
[![pkg.go.dev](https://pkg.go.dev/badge/github.com/itchyny/mmv)](https://pkg.go.dev/github.com/itchyny/mmv)
Rename multiple files using your `$EDITOR`. The command name is named after _multi-mv_. Rename multiple files using your `$EDITOR`. The command name is named after _multi-mv_.
![](https://user-images.githubusercontent.com/375258/72040421-d4f8cd00-32eb-11ea-828f-d9f14f3261ac.gif)
## Usage ## Usage
```bash ```bash
mmv file ... mmv file ...
@ -24,14 +15,13 @@ brew install itchyny/tap/mmv
### Build from source ### Build from source
```bash ```bash
go install github.com/itchyny/mmv/cmd/mmv@latest go get github.com/itchyny/mmv/cmd/mmv
``` ```
## Features ## Features
- `mmv` is implemented in Go language and completely portable. - `mmv` is implemented in Go language and completely portable.
- `mmv` is designed to be simple as `mv`. It requires no configuration file.
- `mmv` supports renaming in cycle (`mv a b`, `mv b c` and `mv c a` at the same time). - `mmv` supports renaming in cycle (`mv a b`, `mv b c` and `mv c a` at the same time).
- `mmv` creates destination directories automatically. You can arrange pictures like `yyyy-mm-dd xxxx.jpg` to `yyyy/mm/dd/xxxx.jpg`. - `mmv` is designed to be simple as `mv`. It requires no configuration file.
- `mmv` is capable to use as a library (just call `mmv.Rename`). - `mmv` is capable to use as a library (just call `mmv.Rename`).
- `mmv` is easy to remember (I believe), **m**ulti-**mv**. - `mmv` is easy to remember (I believe), **m**ulti-**mv**.

View file

@ -4,12 +4,12 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"github.com/kballard/go-shellquote"
_ "github.com/mattn/getwild" _ "github.com/mattn/getwild"
"github.com/mattn/go-tty" "github.com/mattn/go-tty"
@ -18,7 +18,7 @@ import (
const name = "mmv" const name = "mmv"
const version = "0.1.6" const version = "0.1.0"
var revision = "HEAD" var revision = "HEAD"
@ -79,45 +79,38 @@ func rename(args []string) error {
} }
xs[src] = true xs[src] = true
} }
f, err := ioutil.TempFile("", name+"-")
f, err := os.CreateTemp("", name+"-")
if err != nil { if err != nil {
return err return err
} }
defer os.Remove(f.Name()) defer func() {
f.Close()
os.Remove(f.Name())
}()
for _, arg := range args { for _, arg := range args {
f.WriteString(arg) f.WriteString(arg)
f.WriteString("\n") f.WriteString("\n")
} }
if err = f.Close(); err != nil { editor := os.Getenv("EDITOR")
return err if editor == "" {
editor = "vi"
} }
tty, err := tty.Open() tty, err := tty.Open()
if err != nil { if err != nil {
return err return err
} }
defer tty.Close() defer tty.Close()
cmd := exec.Command(editor, f.Name())
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
editorWithArgs, err := shellquote.Split(editor)
if err != nil {
return fmt.Errorf("%s: %s", err, editor)
}
editorWithArgs = append(editorWithArgs, f.Name())
cmd := exec.Command(editorWithArgs[0], editorWithArgs[1:]...)
cmd.Stdin = tty.Input() cmd.Stdin = tty.Input()
cmd.Stdout = tty.Output() cmd.Stdout = tty.Output()
cmd.Stderr = tty.Output() cmd.Stderr = tty.Output()
if err = cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return fmt.Errorf("abort renames: %s", err) return fmt.Errorf("abort renames: %s", err)
} }
if err := f.Close(); err != nil {
cnt, err := os.ReadFile(f.Name()) return err
}
cnt, err := ioutil.ReadFile(f.Name())
if err != nil { if err != nil {
return err return err
} }
@ -129,6 +122,5 @@ func rename(args []string) error {
for i, src := range args { for i, src := range args {
files[src] = got[i] files[src] = got[i]
} }
return mmv.Rename(files) return mmv.Rename(files)
} }

13
go.mod
View file

@ -1,14 +1,9 @@
module github.com/itchyny/mmv module github.com/itchyny/mmv
go 1.20 go 1.13
require ( require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mattn/getwild v0.0.1
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6 github.com/mattn/go-tty v0.0.3
github.com/mattn/go-tty v0.0.4 github.com/stretchr/testify v1.4.0
)
require (
github.com/mattn/go-isatty v0.0.18 // indirect
golang.org/x/sys v0.7.0 // indirect
) )

31
go.sum
View file

@ -1,18 +1,25 @@
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6 h1:uWR+2CTTaHQzDS/DApbJ2H8UEPQl90atrKtczXj2xcs= github.com/mattn/getwild v0.0.1 h1:+Nlzxt7fonj2MtO9y/rg5hxOnM3H6tuTqeD38W25jfo=
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6/go.mod h1:AG+GKQydHp7iLJn+VV+D7y8LeYs5bQ0Xz4fmKd5o1Sg= github.com/mattn/getwild v0.0.1/go.mod h1:AG+GKQydHp7iLJn+VV+D7y8LeYs5bQ0Xz4fmKd5o1Sg=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
github.com/mattn/go-tty v0.0.4 h1:NVikla9X8MN0SQAqCYzpGyXv0jY7MNl3HOWD2dkle7E= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/mattn/go-tty v0.0.4/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3pxse28= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

137
mmv.go
View file

@ -1,12 +1,10 @@
// Package mmv provides a method to rename multiple files.
package mmv package mmv
import ( import (
"crypto/rand" "fmt"
"encoding/base64" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// Rename multiple files. // Rename multiple files.
@ -15,16 +13,8 @@ func Rename(files map[string]string) error {
if err != nil { if err != nil {
return err return err
} }
for i, r := range rs { for _, r := range rs {
if err := doRename(r.src, r.dst); err != nil { if err := doRename(r.src, r.dst); err != nil {
// undo on error not to leave the temporary files
// this does not undo directory creation
for i--; i >= 0; i-- {
if r = rs[i]; os.Rename(r.dst, r.src) != nil {
// something wrong happens so give up not to overwrite files
break
}
}
return err return err
} }
} }
@ -37,16 +27,15 @@ func doRename(src, dst string) (err error) {
if err = os.Rename(src, dst); err != nil && os.IsNotExist(err) { if err = os.Rename(src, dst); err != nil && os.IsNotExist(err) {
// check the source file existence to exit without creating the destination // check the source file existence to exit without creating the destination
// directory when the both source file and destination directory do not exist // directory when the both source file and destination directory do not exist
if _, err := os.Stat(src); err != nil { if _, err := os.Stat(src); err == nil {
return err
}
// create the destination directory // create the destination directory
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return err return err
} }
// try renaming again // try renaming again
return os.Rename(src, dst) return os.Rename(src, dst)
} }
}
return return
} }
@ -65,7 +54,7 @@ type sameSourceError struct {
} }
func (err *sameSourceError) Error() string { func (err *sameSourceError) Error() string {
return "duplicate source: " + err.path return fmt.Sprintf("duplicate source: %s", err.path)
} }
type sameDestinationError struct { type sameDestinationError struct {
@ -73,23 +62,7 @@ type sameDestinationError struct {
} }
func (err *sameDestinationError) Error() string { func (err *sameDestinationError) Error() string {
return "duplicate destination: " + err.path return fmt.Sprintf("duplicate destination: %s", err.path)
}
type invalidRenameError struct {
src, dst string
}
func (err *invalidRenameError) Error() string {
return "invalid rename: " + err.src + ", " + err.dst
}
type temporaryPathError struct {
dir string
}
func (err *temporaryPathError) Error() string {
return "failed to create a temporary path: " + err.dir
} }
func buildRenames(files map[string]string) ([]rename, error) { func buildRenames(files map[string]string) ([]rename, error) {
@ -122,79 +95,22 @@ func buildRenames(files map[string]string) ([]rename, error) {
if _, ok := revs[dst]; ok { if _, ok := revs[dst]; ok {
return nil, &sameDestinationError{dst} return nil, &sameDestinationError{dst}
} }
if k, l := len(src), len(dst); k > l && src[l] == filepath.Separator && src[:l] == dst ||
k < l && dst[k] == filepath.Separator && dst[:k] == src {
return nil, &invalidRenameError{src, dst}
}
revs[dst] = src revs[dst] = src
} }
// group paths by directory depth // remove source == destination
srcdepths := make([][]string, 1)
dstdepths := make([][]string, 1)
for src, dst := range files { for src, dst := range files {
// group source paths by directory depth if src == dst {
i := strings.Count(src, string(filepath.Separator))
if len(srcdepths) <= i {
xs := make([][]string, i*2)
copy(xs, srcdepths)
srcdepths = xs
}
srcdepths[i] = append(srcdepths[i], src)
// group destination paths by directory depth
i = strings.Count(dst, string(filepath.Separator))
if len(dstdepths) <= i {
xs := make([][]string, i*2)
copy(xs, dstdepths)
dstdepths = xs
}
dstdepths[i] = append(dstdepths[i], dst)
}
// result renames
count := len(files)
rs := make([]rename, 0, 2*count)
// check if any parent directory will be moved
for i := len(srcdepths) - 1; i >= 0; i-- {
L:
for _, src := range srcdepths[i] {
for j := 0; j < i; j++ {
for _, s := range srcdepths[j] {
if k := len(s); len(src) > k && src[k] == filepath.Separator && src[:k] == s {
if d := files[s]; s != d {
if dst, l := files[src], len(d); i == j+1 && len(dst) > l && dst[:l] == d && dst[l:] == src[k:] {
// skip moving a file when it moves along with the closest parent directory
delete(files, src)
delete(revs, dst)
} else {
// move to a temporary path before any parent directory is moved
tmp, err := temporaryPath(filepath.Dir(s))
if err != nil {
return nil, err
}
rs = append(rs, rename{src, tmp})
files[tmp] = files[dst]
delete(files, src)
revs[dst] = tmp
}
continue L
}
}
}
}
// remove if source path is equal to destination path
if dst := files[src]; src == dst {
delete(files, src) delete(files, src)
delete(revs, dst) delete(revs, dst)
} }
} }
}
// list renames in increasing destination directory depth order // list the renames
i, vs := 0, make(map[string]int, count) var i int
for _, dsts := range dstdepths { rs := make([]rename, 0, 2*len(files))
for _, dst := range dsts { vs := make(map[string]int, len(files))
for _, dst := range files {
if vs[dst] > 0 { if vs[dst] > 0 {
continue continue
} }
@ -218,11 +134,7 @@ func buildRenames(files map[string]string) ([]rename, error) {
// if there is a cycle, rename to a temporary file // if there is a cycle, rename to a temporary file
var tmp string var tmp string
if cycle { if cycle {
var err error tmp = randomPath(filepath.Dir(dst))
tmp, err = temporaryPath(filepath.Dir(dst))
if err != nil {
return nil, err
}
rs = append(rs, rename{dst, tmp}) rs = append(rs, rename{dst, tmp})
vs[dst]-- vs[dst]--
} }
@ -240,26 +152,19 @@ func buildRenames(files map[string]string) ([]rename, error) {
} }
} }
// if there is a cycle, rename the temporary file // if there is a cycle, rename the remporary file
if cycle { if cycle {
rs = append(rs, rename{tmp, dst}) rs = append(rs, rename{tmp, dst})
} }
} }
}
return rs, nil return rs, nil
} }
// create a temporary path where there is no file currently func randomPath(dir string) string {
func temporaryPath(dir string) (string, error) { for {
bs := make([]byte, 16) path := filepath.Join(dir, fmt.Sprint(rand.Uint64()))
for i := 0; i < 256; i++ {
if _, err := rand.Read(bs); err != nil {
return "", err
}
path := filepath.Join(dir, base64.RawURLEncoding.EncodeToString(bs))
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return path, nil return path
} }
} }
return "", &temporaryPathError{dir}
} }

View file

@ -1,11 +1,13 @@
package mmv package mmv
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestRename(t *testing.T) { func TestRename(t *testing.T) {
@ -15,7 +17,7 @@ func TestRename(t *testing.T) {
contents map[string]string contents map[string]string
expected map[string]string expected map[string]string
count int count int
err string err error
}{ }{
{ {
name: "nothing", name: "nothing",
@ -38,7 +40,7 @@ func TestRename(t *testing.T) {
name: "two files", name: "two files",
files: map[string]string{ files: map[string]string{
"foo": "qux", "foo": "qux",
"bar": "quux", "bar": "quxx",
}, },
count: 2, count: 2,
contents: map[string]string{ contents: map[string]string{
@ -48,7 +50,7 @@ func TestRename(t *testing.T) {
}, },
expected: map[string]string{ expected: map[string]string{
"qux": "0", "qux": "0",
"quux": "1", "quxx": "1",
"baz": "2", "baz": "2",
}, },
}, },
@ -140,11 +142,7 @@ func TestRename(t *testing.T) {
"foo": "0", "foo": "0",
"bar": "1", "bar": "1",
}, },
expected: map[string]string{ err: &emptyPathError{},
"foo": "0",
"bar": "1",
},
err: "empty path error",
}, },
{ {
name: "empty destination path error", name: "empty destination path error",
@ -156,11 +154,7 @@ func TestRename(t *testing.T) {
"foo": "0", "foo": "0",
"bar": "1", "bar": "1",
}, },
expected: map[string]string{ err: &emptyPathError{},
"foo": "0",
"bar": "1",
},
err: "empty path error",
}, },
{ {
name: "same destination error", name: "same destination error",
@ -174,12 +168,7 @@ func TestRename(t *testing.T) {
"bar": "1", "bar": "1",
"baz": "2", "baz": "2",
}, },
expected: map[string]string{ err: &sameDestinationError{"baz"},
"foo": "0",
"bar": "1",
"baz": "2",
},
err: "duplicate destination: baz",
}, },
{ {
name: "clean source path", name: "clean source path",
@ -207,11 +196,7 @@ func TestRename(t *testing.T) {
"foo": "0", "foo": "0",
"bar": "1", "bar": "1",
}, },
expected: map[string]string{ err: &sameSourceError{"foo"},
"foo": "0",
"bar": "1",
},
err: "duplicate source: foo",
}, },
{ {
name: "cleaned path same destination error", name: "cleaned path same destination error",
@ -223,11 +208,7 @@ func TestRename(t *testing.T) {
"foo": "0", "foo": "0",
"bar": "1", "bar": "1",
}, },
expected: map[string]string{ err: &sameDestinationError{"baz"},
"foo": "0",
"bar": "1",
},
err: "duplicate destination: baz",
}, },
{ {
name: "same source and destination", name: "same source and destination",
@ -250,31 +231,7 @@ func TestRename(t *testing.T) {
"foo/": "foo/", "foo/": "foo/",
"bar/": "foo", "bar/": "foo",
}, },
err: "duplicate destination: foo", err: &sameDestinationError{"foo"},
},
{
name: "undo on error",
files: map[string]string{
"foo": "bar",
"bar": "foo",
"baz": "qux",
"qux": "quux",
"quux": "baz",
},
count: 7,
contents: map[string]string{
"foo": "0",
"bar": "1",
"baz": "2",
"qux": "3",
},
expected: map[string]string{
"foo": "0",
"bar": "1",
"baz": "2",
"qux": "3",
},
err: "quux: ", // no such file or directory
}, },
{ {
name: "create destination directory", name: "create destination directory",
@ -295,120 +252,22 @@ func TestRename(t *testing.T) {
"a/b/c/baz": "2", "a/b/c/baz": "2",
}, },
}, },
{
name: "invalid rename error",
files: map[string]string{
"x/y": "x",
},
contents: map[string]string{
"x/y": "0",
},
expected: map[string]string{
"x/y": "0",
},
err: "invalid rename: x",
},
{
name: "invalid rename error",
files: map[string]string{
"x/y": "x/y/z",
},
contents: map[string]string{
"x/y": "0",
},
expected: map[string]string{
"x/y": "0",
},
err: "invalid rename: x",
},
{
name: "directory renames",
files: map[string]string{
"x/foo": "y/bar",
"x/bar": "z/baz",
"x/qux": "z/qux",
"x/quy": "z/baz/qux",
"x/": "z/",
"y/bar": "x/foo",
"y/qux": "x/qux",
"y/": "w/",
"w/": "y/",
"w/x/": "y/y/",
"w/x/x/": "x/z/",
"w/x/y": "y/x/y",
"w/x/z": "w/x/z",
"w/x/w": "x/x/w",
"v/": "v/",
"v/x": "v/y",
"v/x/x": "v/y/x",
"xxxxx": "yyyyy",
},
count: 26,
contents: map[string]string{
"x/foo": "0",
"x/bar/a": "1",
"x/qux": "2",
"x/quy": "3",
"x/quz": "4",
"y/bar": "5",
"y/baz": "6",
"y/qux": "7",
"w/a": "8",
"w/x/a": "9",
"w/x/x/a": "10",
"w/x/y": "11",
"w/x/z": "12",
"w/x/w": "13",
"v/x/x": "14",
"xxxxx": "15",
},
expected: map[string]string{
"y/bar": "0",
"z/baz/a": "1",
"z/qux": "2",
"z/baz/qux": "3",
"z/quz": "4",
"x/foo": "5",
"w/baz": "6",
"x/qux": "7",
"y/a": "8",
"y/y/a": "9",
"x/z/a": "10",
"y/x/y": "11",
"w/x/z": "12",
"x/x/w": "13",
"v/y/x": "14",
"yyyyy": "15",
},
},
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
dir, err := os.MkdirTemp("", "mmv-") dir, err := ioutil.TempDir("", "mmv-")
if err != nil { defer os.RemoveAll(dir)
t.Fatalf("os.MkdirTemp returned an error: %s", err) require.NoError(t, os.Chdir(dir))
} require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dir) }) require.NoError(t, setupFiles(tc.contents))
if err := os.Chdir(dir); err != nil {
t.Fatalf("os.Chdir returned an error: %s", err)
}
if err := setupFiles(tc.contents); err != nil {
t.Fatalf("setupFiles returned an error: %s", err)
}
rs, _ := buildRenames(clone(tc.files)) rs, _ := buildRenames(clone(tc.files))
if got := len(rs); got != tc.count { assert.Equal(t, tc.count, len(rs))
t.Errorf("expected: %d, got: %d", tc.count, got) got := Rename(tc.files)
} if tc.err == nil {
err = Rename(tc.files) require.NoError(t, got)
if tc.err == "" { assert.Equal(t, tc.expected, fileContents("."))
if err != nil { } else {
t.Errorf("Rename returned an error: %s", err) assert.Equal(t, tc.err, got)
}
} else if !strings.Contains(err.Error(), tc.err) {
t.Errorf("error should contain: %s, got: %s", tc.err, err)
}
if got := fileContents("."); !reflect.DeepEqual(got, tc.expected) {
t.Errorf("expected: %v, got: %v", tc.expected, got)
} }
}) })
} }
@ -416,13 +275,7 @@ func TestRename(t *testing.T) {
func setupFiles(contents map[string]string) error { func setupFiles(contents map[string]string) error {
for f, cnt := range contents { for f, cnt := range contents {
dir := filepath.Dir(f) if err := ioutil.WriteFile(f, []byte(cnt), 0600); err != nil {
if dir != "." {
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
}
if err := os.WriteFile(f, []byte(cnt), 0o600); err != nil {
return err return err
} }
} }
@ -431,7 +284,7 @@ func setupFiles(contents map[string]string) error {
func fileContents(dir string) map[string]string { func fileContents(dir string) map[string]string {
m := make(map[string]string) m := make(map[string]string)
fis, _ := os.ReadDir(dir) fis, _ := ioutil.ReadDir(dir)
for _, fi := range fis { for _, fi := range fis {
if fi.IsDir() { if fi.IsDir() {
for k, v := range fileContents(filepath.Join(dir, fi.Name())) { for k, v := range fileContents(filepath.Join(dir, fi.Name())) {
@ -439,7 +292,7 @@ func fileContents(dir string) map[string]string {
} }
} else { } else {
path := filepath.Join(dir, fi.Name()) path := filepath.Join(dir, fi.Name())
cnt, _ := os.ReadFile(path) cnt, _ := ioutil.ReadFile(path)
m[filepath.ToSlash(path)] = string(cnt) m[filepath.ToSlash(path)] = string(cnt)
} }
} }