2014-11-24 09:58:57 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-11-24 09:58:57 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Inserts ~tag just before the extension of the filename.
|
|
|
|
func taggedFilename(name, tag string) string {
|
|
|
|
dir, file := filepath.Dir(name), filepath.Base(name)
|
|
|
|
ext := filepath.Ext(file)
|
|
|
|
withoutExt := file[:len(file)-len(ext)]
|
|
|
|
return filepath.Join(dir, withoutExt+"~"+tag+ext)
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:32:18 +00:00
|
|
|
var tagExp = regexp.MustCompile(`.*~([^~.]+)(?:\.[^.]+)?$`)
|
2014-11-24 09:58:57 +00:00
|
|
|
|
|
|
|
// Returns the tag from a filename, whether at the end or middle.
|
|
|
|
func filenameTag(path string) string {
|
|
|
|
match := tagExp.FindStringSubmatch(path)
|
2015-04-28 15:34:55 +00:00
|
|
|
// match is []string{"whole match", "submatch"} when successful
|
2014-11-24 09:58:57 +00:00
|
|
|
|
|
|
|
if len(match) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return match[1]
|
|
|
|
}
|