<commit_before>package project

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"sort"
	"time"

	"github.com/git-time-metric/gtm/scm"
)

type Index struct {
	Projects map[string]time.Time
}

func NewIndex() (Index, error) {
	i := Index{Projects: map[string]time.Time{}}

	err := i.load()
	if err != nil {
		err := i.save()
		if err != nil {
			return i, err
		}
	}

	return i, nil
}

func (i *Index) Get(tags []string, all bool) ([]string, error) {
	switch {
	case all:
		err := i.clean()
		return i.projects(), err
	case len(tags) > 0:
		if err := i.clean(); err != nil {
			return []string{}, err
		}
		projectsWithTags := []string{}
		for _, p := range i.projects() {
			found, err := i.hasTags(p, tags)
			if err != nil {
				return []string{}, nil
			}
			if found {
				projectsWithTags = append(projectsWithTags, p)
			}
		}
		sort.Strings(projectsWithTags)
		return projectsWithTags, nil
	default:
		curProjPath, err := scm.RootPath()
		if err != nil {
			return []string{}, err
		}
		if _, ok := i.Projects[curProjPath]; !ok {
			i.add(curProjPath)
			if err := i.save(); err != nil {
				return []string{}, err
			}
		}
		return []string{curProjPath}, nil
	}
}

func (i *Index) add(p string) {
	i.Projects[p] = time.Now()
}

func (i *Index) remove(p string) {
	// fmt.Println("Removing", p)
	delete(i.Projects, p)
}

func (i *Index) projects() []string {
	var keys []string
	for k := range i.Projects {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	return keys
}

func (i *Index) path() (string, error) {
	u, err := user.Current()
	if err != nil {
		return "", err
	}
	return filepath.Join(u.HomeDir, ".git-time-metric", "project.idx"), nil
}

func (i *Index) load() error {
	p, err := i.path()
	if err != nil {
		return err
	}

	raw, err := ioutil.ReadFile(p)
	if err != nil {<commit_msg>Change project index file extension to json<commit_after>package project

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"sort"
	"time"

	"github.com/git-time-metric/gtm/scm"
)

type Index struct {
	Projects map[string]time.Time
}

func NewIndex() (Index, error) {
	i := Index{Projects: map[string]time.Time{}}

	err := i.load()
	if err != nil {
		err := i.save()
		if err != nil {
			return i, err
		}
	}

	return i, nil
}

func (i *Index) Get(tags []string, all bool) ([]string, error) {
	switch {
	case all:
		err := i.clean()
		return i.projects(), err
	case len(tags) > 0:
		if err := i.clean(); err != nil {
			return []string{}, err
		}
		projectsWithTags := []string{}
		for _, p := range i.projects() {
			found, err := i.hasTags(p, tags)
			if err != nil {
				return []string{}, nil
			}
			if found {
				projectsWithTags = append(projectsWithTags, p)
			}
		}
		sort.Strings(projectsWithTags)
		return projectsWithTags, nil
	default:
		curProjPath, err := scm.RootPath()
		if err != nil {
			return []string{}, err
		}
		if _, ok := i.Projects[curProjPath]; !ok {
			i.add(curProjPath)
			if err := i.save(); err != nil {
				return []string{}, err
			}
		}
		return []string{curProjPath}, nil
	}
}

func (i *Index) add(p string) {
	i.Projects[p] = time.Now()
}

func (i *Index) remove(p string) {
	// fmt.Println("Removing", p)
	delete(i.Projects, p)
}

func (i *Index) projects() []string {
	var keys []string
	for k := range i.Projects {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	return keys
}

func (i *Index) path() (string, error) {
	u, err := user.Current()
	if err != nil {
		return "", err
	}
	return filepath.Join(u.HomeDir, ".git-time-metric", "project.json"), nil
}

func (i *Index) load() error {
	p, err := i.path()
	if err != nil {
		return err
	}

	raw, err := ioutil.ReadFile(p)
	if err != nil {
{'fraction_non_alphanumeric': 0.177379983726607, 'fraction_numerical': 0.0005424464334147003, 'mean_word_length': 3.1391694725028056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6296016', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1448, 'n_words': 386}