<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

// Version ...
type Version struct {
	Major uint64
	Minor uint64
	Patch uint64
}

func (v Version) String() string {
	s := make([]byte, 0, 5)
	s = strconv.AppendUint(s, v.Major, 10)
	s = append(s, '.')
	s = strconv.AppendUint(s, v.Minor, 10)
	s = append(s, '.')
	s = strconv.AppendUint(s, v.Patch, 10)

	return string(s)
}

func bumpVersion(v Version, bumpLevel string) (Version, error) {

	if bumpLevel == "patch" {
		v.Patch++
	} else if bumpLevel == "minor" {
		v.Minor++
		v.Patch = 0
	} else if bumpLevel == "major" {
		v.Major++
		v.Minor = 0
		v.Patch = 0
	} else {
		return Version{}, fmt.Errorf("Bump level '%s' unknown", bumpLevel)
	}

	return v, nil
}

func gitCommit(path, bumpLevel, newVersion, gitRemotes string, gitPush bool) {

	// git add
	fmt.Println("Adding changes to Git index")
	os.Chdir(path)
	_, err := exec.Command("git", "add", "metadata.rb").Output()
	if err != nil {
		log.Fatal(err)
	}

	if gitPush {

		// git commit
		fmt.Println("Committing changes")
		commitMessage := fmt.Sprintf("%s bump to version %s", bumpLevel, newVersion)
		_, err := exec.Command("git", "commit", "-m", commitMessage, "-n").Output()
		if err != nil {
			log.Fatal(err)
		}

		// git tag
		fmt.Printf("Adding tag '%s'\n", newVersion)
		tagMessage := fmt.Sprintf("%s", newVersion)
		_, err = exec.Command("git", "tag", "-a", newVersion, "-m", tagMessage).Output()
		if err != nil {
			log.Fatal(err)
		}

		// git push to gitRemotes
		remotes := strings.Split(gitRemotes, ",")
		for i := range remotes {
			remote := strings.TrimSpace(remotes[i])
			fmt.Printf("Pushing changes to '%s'\n", remote)
			_, err = exec.Command("git", "push", remote, "master", "--follow-tags").Output()
			if err != nil {
				log.Fatal(err)
			}
		}

	}
}

func parseVersion(version string) (Version, error) {
	versionArray := strings.SplitN(version, ".", 3)
	if len(versionArray) != 3 {
		return Version{}, fmt.Errorf("No major/minor/patch elements found")
	}

	var err error
	v := Version{}

	// major
	v.Major, err = strconv.ParseUint(versionArray[0], 10, 64)
	if err != nil {
		return Version{}, err
	}

	// minor
	v.Minor, err = strconv.ParseUint(versionArray[1], 10, 64)
	if err != nil {
		return Version{}, err
	}

	// patch
	v.Patch, err = strconv.ParseUint(versionArray[2], 10, 64)
	if err != nil {
		return Version{}, err
	}

	return v, nil
}

func metadata(path, bumpLevel string) string {
	path += "/metadata.rb"
	input, err := ioutil.ReadFile(path)
	if err != nil {
		log.Fatalln(err)
	}

	lines := strings.Split(string(input), "\n")
	newVersion := Version{}

	for i, line := range lines {
		if strings.Contains(line, "version") {
			lineArray := strings.Split(line, "'")
			fmt.Printf("Current version: %s\n", lineArray[1])<commit_msg>Support double quotes for the version value on metadata.rb file<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

// Version ...
type Version struct {
	Major uint64
	Minor uint64
	Patch uint64
}

func (v Version) String() string {
	s := make([]byte, 0, 5)
	s = strconv.AppendUint(s, v.Major, 10)
	s = append(s, '.')
	s = strconv.AppendUint(s, v.Minor, 10)
	s = append(s, '.')
	s = strconv.AppendUint(s, v.Patch, 10)

	return string(s)
}

func bumpVersion(v Version, bumpLevel string) (Version, error) {

	if bumpLevel == "patch" {
		v.Patch++
	} else if bumpLevel == "minor" {
		v.Minor++
		v.Patch = 0
	} else if bumpLevel == "major" {
		v.Major++
		v.Minor = 0
		v.Patch = 0
	} else {
		return Version{}, fmt.Errorf("Bump level '%s' unknown", bumpLevel)
	}

	return v, nil
}

func gitCommit(path, bumpLevel, newVersion, gitRemotes string, gitPush bool) {

	// git add
	fmt.Println("Adding changes to Git index")
	os.Chdir(path)
	_, err := exec.Command("git", "add", "metadata.rb").Output()
	if err != nil {
		log.Fatal(err)
	}

	if gitPush {

		// git commit
		fmt.Println("Committing changes")
		commitMessage := fmt.Sprintf("%s bump to version %s", bumpLevel, newVersion)
		_, err := exec.Command("git", "commit", "-m", commitMessage, "-n").Output()
		if err != nil {
			log.Fatal(err)
		}

		// git tag
		fmt.Printf("Adding tag '%s'\n", newVersion)
		tagMessage := fmt.Sprintf("%s", newVersion)
		_, err = exec.Command("git", "tag", "-a", newVersion, "-m", tagMessage).Output()
		if err != nil {
			log.Fatal(err)
		}

		// git push to gitRemotes
		remotes := strings.Split(gitRemotes, ",")
		for i := range remotes {
			remote := strings.TrimSpace(remotes[i])
			fmt.Printf("Pushing changes to '%s'\n", remote)
			_, err = exec.Command("git", "push", remote, "master", "--follow-tags").Output()
			if err != nil {
				log.Fatal(err)
			}
		}

	}
}

func parseVersion(version string) (Version, error) {
	versionArray := strings.SplitN(version, ".", 3)
	if len(versionArray) != 3 {
		return Version{}, fmt.Errorf("No major/minor/patch elements found")
	}

	var err error
	v := Version{}

	// major
	v.Major, err = strconv.ParseUint(versionArray[0], 10, 64)
	if err != nil {
		return Version{}, err
	}

	// minor
	v.Minor, err = strconv.ParseUint(versionArray[1], 10, 64)
	if err != nil {
		return Version{}, err
	}

	// patch
	v.Patch, err = strconv.ParseUint(versionArray[2], 10, 64)
	if err != nil {
		return Version{}, err
	}

	return v, nil
}

func metadata(path, bumpLevel string) string {
	path += "/metadata.rb"
	input, err := ioutil.ReadFile(path)
	if err != nil {
		log.Fatalln(err)
	}

	lines := strings.Split(string(input), "\n")
	newVersion := Version{}

	for i, line := range lines {
		if strings.Contains(line, "version") {
			line = strings.Replace(line, "\"", "'", 2)
			lineArray := strings.Split(line, "'")
			fmt.Printf("Current version: %s\n", lineArray[1])
{'fraction_non_alphanumeric': 0.1776588397790055, 'fraction_numerical': 0.01225828729281768, 'mean_word_length': 3.4699074074074074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4563786', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2136, 'n_words': 561}