<commit_before>package fix

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"github.com/ghthor/journal/idea"
	"io"
	"io/ioutil"
	"strings"
	"time"
)

var entryFixes []EntryFix

func init() {
	entryFixes = []EntryFix{
		FixAddClosedAtTimestamp{},
		FixSplitCommitMessage{},
		FixCommitMessagePrefixWithTilde{},
		FixIdeasInBody{},
	}
}

type EntryFix interface {
	// Parses io.Reader for the error that can be fixed
	CanFix(io.Reader) (bool, error)

	// Returns byte slice that has been fixed
	Execute(io.Reader) ([]byte, error)
}

// Parse the opened at timestamp and add 2 mins
// then append it to the end
type FixAddClosedAtTimestamp struct{}

func (FixAddClosedAtTimestamp) CanFix(r io.Reader) (bool, error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return false, err
	}

	return !lastLineIsTimestamp(data), nil
}

func lastLineIsTimestamp(data []byte) bool {
	scanner := bufio.NewScanner(bytes.NewReader(data))
	// Scan to the last line
	for scanner.Scan() {
	}
	_, err := time.Parse(time.UnixDate, scanner.Text())

	return err == nil
}

func (FixAddClosedAtTimestamp) Execute(r io.Reader) ([]byte, error) {
	// For adding 2 mins and making the closed at timestamp
	var openedAt time.Time

	// For storing the fixed output
	b := bytes.NewBuffer(make([]byte, 0, 1024))

	scanner := bufio.NewScanner(r)

	// Scan in the opened at timestamp
	if scanner.Scan() {
		if time, err := time.Parse(time.UnixDate, scanner.Text()); err != nil {
			// First line wasn't a timestamp
			return nil, err
		} else {
			openedAt = time
			// Print the timestamp to the fixed buffer
			if _, err := fmt.Fprintln(b, scanner.Text()); err != nil {
				return nil, err
			}
		}
	} else {
		return nil, errors.New("error parsing opened at timestamp")<commit_msg>Fix for scanner behavior changed in go 1.4<commit_after>package fix

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"github.com/ghthor/journal/idea"
	"io"
	"io/ioutil"
	"strings"
	"time"
)

var entryFixes []EntryFix

func init() {
	entryFixes = []EntryFix{
		FixAddClosedAtTimestamp{},
		FixSplitCommitMessage{},
		FixCommitMessagePrefixWithTilde{},
		FixIdeasInBody{},
	}
}

type EntryFix interface {
	// Parses io.Reader for the error that can be fixed
	CanFix(io.Reader) (bool, error)

	// Returns byte slice that has been fixed
	Execute(io.Reader) ([]byte, error)
}

// Parse the opened at timestamp and add 2 mins
// then append it to the end
type FixAddClosedAtTimestamp struct{}

func (FixAddClosedAtTimestamp) CanFix(r io.Reader) (bool, error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return false, err
	}

	return !lastLineIsTimestamp(data), nil
}

func lastLineIsTimestamp(data []byte) bool {
	scanner := bufio.NewScanner(bytes.NewReader(data))

	// Scan to the last line
	var prevLine string
	for scanner.Scan() {
		prevLine = scanner.Text()
	}
	_, err := time.Parse(time.UnixDate, prevLine)

	return err == nil
}

func (FixAddClosedAtTimestamp) Execute(r io.Reader) ([]byte, error) {
	// For adding 2 mins and making the closed at timestamp
	var openedAt time.Time

	// For storing the fixed output
	b := bytes.NewBuffer(make([]byte, 0, 1024))

	scanner := bufio.NewScanner(r)

	// Scan in the opened at timestamp
	if scanner.Scan() {
		if time, err := time.Parse(time.UnixDate, scanner.Text()); err != nil {
			// First line wasn't a timestamp
			return nil, err
		} else {
			openedAt = time
			// Print the timestamp to the fixed buffer
			if _, err := fmt.Fprintln(b, scanner.Text()); err != nil {
				return nil, err
			}
		}
	} else {
		return nil, errors.New("error parsing opened at timestamp")
{'fraction_non_alphanumeric': 0.1258741258741259, 'fraction_numerical': 0.0044755244755244755, 'mean_word_length': 3.7616511318242343, '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': '6463198', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1215, 'n_words': 377}