Remove -prune flag.

-prune is unnecessary and misleading. Once a release is cut,
.pending/ contents must be recursively removed by git rm -r
and changes committed and pushed.

Ref #3892
This commit is contained in:
Alessio Treglia 2019-03-20 14:29:41 +01:00
parent 4b91909ae8
commit 2832941711
No known key found for this signature in database
GPG Key ID: E8A48AE5311D765A
1 changed files with 40 additions and 31 deletions

View File

@ -27,7 +27,6 @@ var (
verboseLog *log.Logger verboseLog *log.Logger
entriesDir string entriesDir string
pruneAfterGenerate bool
verboseLogging bool verboseLogging bool
// sections name-title map // sections name-title map
@ -55,7 +54,6 @@ func init() {
} }
flag.StringVar(&entriesDir, "d", filepath.Join(cwd, entriesDirName), "entry files directory") flag.StringVar(&entriesDir, "d", filepath.Join(cwd, entriesDirName), "entry files directory")
flag.BoolVar(&pruneAfterGenerate, "prune", false, "prune old entries after changelog generation")
flag.BoolVar(&verboseLogging, "v", false, "enable verbose logging") flag.BoolVar(&verboseLogging, "v", false, "enable verbose logging")
flag.Usage = printUsage flag.Usage = printUsage
@ -99,7 +97,10 @@ func main() {
if flag.NArg() > 1 { if flag.NArg() > 1 {
version = strings.Join(flag.Args()[1:], " ") version = strings.Join(flag.Args()[1:], " ")
} }
generateChangelog(version, pruneAfterGenerate) generateChangelog(version)
case "prune":
pruneEmptyDirectories()
default: default:
unknownCommand(cmd) unknownCommand(cmd)
@ -142,20 +143,20 @@ func generateFileName(line string) string {
return ret[:int(math.Min(float64(len(ret)), float64(maxEntryFilenameLength)))] return ret[:int(math.Min(float64(len(ret)), float64(maxEntryFilenameLength)))]
} }
func directoryContents(dirPath string) ([]os.FileInfo, error) { func directoryContents(dirPath string) []os.FileInfo {
contents, err := ioutil.ReadDir(dirPath) contents, err := ioutil.ReadDir(dirPath)
if err != nil { if err != nil && !os.IsNotExist(err) {
return nil, errors.New("couldn't read directory") log.Fatalf("couldn't read directory %s: %v", dirPath, err)
} }
if len(contents) == 0 { if len(contents) == 0 {
return nil, nil return nil
} }
// Filter out hidden files // Filter out hidden files
newContents := contents[:0] newContents := contents[:0]
for _, f := range contents { for _, f := range contents {
if f.Name()[0] != '.' { if f.Name()[0] != '.' { // skip hidden files
newContents = append(newContents, f) newContents = append(newContents, f)
} }
} }
@ -163,46 +164,32 @@ func directoryContents(dirPath string) ([]os.FileInfo, error) {
contents[i] = nil contents[i] = nil
} }
return newContents, nil return newContents
} }
func generateChangelog(version string, prune bool) { func generateChangelog(version string) {
fmt.Printf("# %s\n\n", version) fmt.Printf("# %s\n\n", version)
for sectionDir, sectionTitle := range sections { for sectionDir, sectionTitle := range sections {
sectionTitlePrinted := false sectionTitlePrinted := false
for stanzaDir, stanzaTitle := range stanzas { for stanzaDir, stanzaTitle := range stanzas {
path := filepath.Join(entriesDir, sectionDir, stanzaDir) path := filepath.Join(entriesDir, sectionDir, stanzaDir)
if contents, err := directoryContents(path); err != nil || len(contents) == 0 { files := directoryContents(path)
if err != nil { if len(files) == 0 {
verboseLog.Printf("skipping %s: %v", filepath.Join(filepath.Base(entriesDir), sectionDir, stanzaDir), err)
}
continue continue
} }
if !sectionTitlePrinted { if !sectionTitlePrinted {
fmt.Printf("## %s\n\n", sectionTitle) fmt.Printf("## %s\n\n", sectionTitle)
sectionTitlePrinted = true sectionTitlePrinted = true
} }
fmt.Printf("### %s\n\n", stanzaTitle) fmt.Printf("### %s\n\n", stanzaTitle)
files, err := ioutil.ReadDir(path)
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
for _, f := range files { for _, f := range files {
if f.Name()[0] == '.' { verboseLog.Println("processing", f.Name())
continue // skip hidden files
}
filename := filepath.Join(path, f.Name()) filename := filepath.Join(path, f.Name())
if err := indentAndPrintFile(filename); err != nil { if err := indentAndPrintFile(filename); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if prune {
if err := os.Remove(filename); err != nil {
verboseLog.Printf("couldn't delete %s: %v", filename, err)
}
}
} }
fmt.Println() fmt.Println()
@ -210,6 +197,15 @@ func generateChangelog(version string, prune bool) {
} }
} }
func pruneEmptyDirectories() {
for sectionDir, _ := range sections {
for stanzaDir, _ := range stanzas {
mustPruneDirIfEmpty(filepath.Join(entriesDir, sectionDir, stanzaDir))
}
mustPruneDirIfEmpty(filepath.Join(entriesDir, sectionDir))
}
}
// nolint: errcheck // nolint: errcheck
func indentAndPrintFile(filename string) error { func indentAndPrintFile(filename string) error {
file, err := os.Open(filename) file, err := os.Open(filename)
@ -327,8 +323,20 @@ func launchUserEditor() (string, error) {
return tempfilename, nil return tempfilename, nil
} }
func mustPruneDirIfEmpty(path string) {
if contents := directoryContents(path); len(contents) == 0 {
if err := os.Remove(path); err != nil {
if !os.IsNotExist(err) {
log.Fatal(err)
}
return
}
log.Println(path, "removed")
}
}
func printUsage() { func printUsage() {
usageText := fmt.Sprintf(`usage: %s [-d directory] [-prune] [-v] command usageText := fmt.Sprintf(`usage: %s [-d directory] [-v] command
Maintain unreleased changelog entries in a modular fashion. Maintain unreleased changelog entries in a modular fashion.
@ -337,6 +345,7 @@ Commands:
the editor to edit the message. the editor to edit the message.
generate [version] Generate a changelog in Markdown format and print generate [version] Generate a changelog in Markdown format and print
it to STDOUT. version defaults to UNRELEASED. it to STDOUT. version defaults to UNRELEASED.
prune Delete empty sub-directories recursively.
Sections Stanzas Sections Stanzas
--- --- --- ---