<commit_before>package cache

import (
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/vinaymayar/sgit/utils"
)

var targets = map[string]string{
	"sbt": "target",
	"":    "bin",
}

func SaveCache() {
	settings, err := getCacheSettings()
	if err != nil && os.IsNotExist(err) {
		log.Print(err)
		return
	}

	targetDirs, err := getTargetDirs(settings)
	if err != nil {
		log.Print(err)
		return
	}

	for _, dir := range targetDirs {
		if 0 == len(dir) {
			continue
		}

		relDir, err := filepath.Rel(settings.rootDir, dir)
		if err != nil {
			log.Print(err)
			continue
		}

		log.Printf("Saving %v.\n", relDir)

		_, err = utils.Execute("rsync", "-Rr", relDir, settings.cachePrefix)
		if err != nil {
			log.Print(err)
		}
	}
}

func RestoreCache() {
	settings, err := getCacheSettings()
	if err != nil {
		log.Print(err)
		return
	}

	err = os.Chdir(settings.cachePrefix)
	if err != nil {
		log.Print(noCacheError(settings.branch))
	}
	cacheDirs, err := getCacheDirs(settings)
	if err != nil {
		log.Print(err)
		return
	}

	for _, dir := range cacheDirs {
		if 0 == len(dir) {
			continue
		}

		relDir, err := filepath.Rel(settings.cachePrefix, dir)
		if err != nil {
			log.Print(err)
			continue
		}

		log.Printf("Restoring %v.\n", relDir)

		_, err = utils.Execute("rsync", "-Rr", relDir, settings.rootDir)
		if err != nil {
			log.Print(err)
		}
	}
}

func ClearCache(args []string) {
	sgitDir, err := utils.GetSgitRootDir()
	if err != nil {
		log.Fatal(err)
	}

	if len(args) == 1 {

		utils.Execute(
			"find",
			sgitDir,
			"-d",
			"-mindepth", "1",
			"-type", "d",
			"-exec", "rm", "-r", "{}", ";")

	} else {

		for _, branch := range args[1:] {
			cachePrefix := filepath.Join(sgitDir, branch)
			err := utils.Execute("rm", "-r", cachePrefix)

			if err != nil && os.IsNotExist(err) {
				log.Print(noCacheError(branch))
			}
		}

	}
}

func getCacheDirs(settings CacheSettings) ([]string, error) {
	cacheDirs, err := utils.Execute(
		"find",
		settings.cachePrefix,
		"-name",
		targets[settings.config.ProjectType])
	return strings.Split(strings.TrimSpace(cacheDirs), "\n"), err
}<commit_msg>Added command clear-cache to clear the cache.<commit_after>package cache

import (
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/vinaymayar/sgit/utils"
)

var targets = map[string]string{
	"sbt": "target",
	"":    "bin",
}

func SaveCache() {
	settings, err := getCacheSettings()
	if err != nil && os.IsNotExist(err) {
		log.Print(err)
		return
	}

	targetDirs, err := getTargetDirs(settings)
	if err != nil {
		log.Print(err)
		return
	}

	for _, dir := range targetDirs {
		if 0 == len(dir) {
			continue
		}

		relDir, err := filepath.Rel(settings.rootDir, dir)
		if err != nil {
			log.Print(err)
			continue
		}

		log.Printf("Saving %v.\n", relDir)

		_, err = utils.Execute("rsync", "-Rr", relDir, settings.cachePrefix)
		if err != nil {
			log.Print(err)
		}
	}
}

func RestoreCache() {
	settings, err := getCacheSettings()
	if err != nil {
		log.Print(err)
		return
	}

	err = os.Chdir(settings.cachePrefix)
	if err != nil {
		log.Print(noCacheError(settings.branch))
	}
	cacheDirs, err := getCacheDirs(settings)
	if err != nil {
		log.Print(err)
		return
	}

	for _, dir := range cacheDirs {
		if 0 == len(dir) {
			continue
		}

		relDir, err := filepath.Rel(settings.cachePrefix, dir)
		if err != nil {
			log.Print(err)
			continue
		}

		log.Printf("Restoring %v.\n", relDir)

		_, err = utils.Execute("rsync", "-Rr", relDir, settings.rootDir)
		if err != nil {
			log.Print(err)
		}
	}
}

func ClearCache(args []string) {
	sgitDir, err := utils.GetSgitRootDir()
	if err != nil {
		log.Fatal(err)
	}

	if len(args) == 1 {

		utils.Execute(
			"find",
			sgitDir,
			"-d",
			"-mindepth", "1",
			"-type", "d",
			"-exec", "rm", "-r", "{}", ";")

	} else {

		for _, branch := range args[1:] {
			cachePrefix := filepath.Join(sgitDir, branch)
			_, err = utils.Execute("rm", "-r", cachePrefix)

			if err != nil && os.IsNotExist(err) {
				log.Print(noCacheError(branch))
			}
		}

	}
}

func getCacheDirs(settings CacheSettings) ([]string, error) {
	cacheDirs, err := utils.Execute(
		"find",
		settings.cachePrefix,
		"-name",
		targets[settings.config.ProjectType])
	return strings.Split(strings.TrimSpace(cacheDirs), "\n"), err
}
{'fraction_non_alphanumeric': 0.17586529466791395, 'fraction_numerical': 0.0023386342376052385, 'mean_word_length': 3.217948717948718, 'pattern_counts': {'":': 4, '<': 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': '7340383', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1670, 'n_words': 350}