Default behavious changed: Filenames that look like hashes are skipped. Pass --always to override this.

Fortune for lazy-rebuild's current commit: Future small blessing − 末小吉
master
Avril 3 years ago
parent 11fa5b6724
commit 7d040f70eb
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,5 +1,7 @@
all: clean lazy-rebuild test
.PHONY: all
all: | clean
$(MAKE) lazy-rebuild test
clean:
rm -rf test

@ -0,0 +1,3 @@
module lazy-rebuild
go 1.17

@ -1,4 +1,3 @@
//go:binary-only-package
package main
import (
@ -12,6 +11,7 @@ import (
"os"
"sync"
"strconv"
"regexp"
)
var keepLongExt bool = false
@ -42,6 +42,12 @@ func hash(file string) []byte {
return h.Sum(nil)
}
var isHashRE *regexp.Regexp = regexp.MustCompile("^[0-9a-f]{64}$")
func is_hash(name string) bool {
name = strings.Split(name, ".")[0]
return isHashRE.MatchString(name)
}
func extractExt(x string) string {
if (keepLongExt) {
return last(strings.SplitN(x, ".", 2)[1:])
@ -50,8 +56,13 @@ func extractExt(x string) string {
}
}
var alwaysCheck bool = false
func dofile(i int, y string) {
loc, x := filepath.Split(y)
if !alwaysCheck && is_hash(x) {
fmt.Printf("E: %s already a hash, skipping.\n", x)
return
}
sha := hash(y)
str := fmt.Sprintf("%x", sha)
newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x))
@ -72,7 +83,7 @@ func main() {
var wg sync.WaitGroup
if (len(ar) < 1) {
fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0])
fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] [--always] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n\t--always\tDo not skip filenames which are already hashes.\n", os.Args[0])
return;
}
var ignore = false
@ -91,6 +102,8 @@ func main() {
recurse = !recurse
case "quiet":
noErr = !noErr
case "always":
alwaysCheck = true
case "threads":
if i<len(ar)-1 {
if th, err := strconv.Atoi(ar[i+1]); err ==nil {

Loading…
Cancel
Save