<commit_before>package vcs

import (
	"os/exec"

	. "gist.github.com/5892738.git"
)

type gitVcs struct {
	commonVcs
}

func (this *gitVcs) Type() Type { return Git }

func (this *gitVcs) GetStatus() string {
	_, status := IsFolderGitRepo(this.rootPath)
	return status
}

func (this *gitVcs) GetRemote() string {
	cmd := exec.Command("git", "ls-remote", "--get-url", "origin")
	cmd.Dir = this.rootPath

	if out, err := cmd.Output(); err == nil {
		return TrimLastNewline(string(out))
	} else {
		return ""
	}
}

func (this *gitVcs) GetDefaultBranch() string {
	return "master"
}

func (this *gitVcs) GetLocalBranch() string {
	return CheckGitRepoLocalBranch(this.rootPath)
}

func (this *gitVcs) GetLocalRev() string {
	return CheckGitRepoLocal(this.rootPath, this.GetDefaultBranch())
}

func (this *gitVcs) GetRemoteRev() string {
	return CheckGitRepoRemote(this.rootPath, this.GetDefaultBranch())
}

// ---

func GetGitRepoRoot(path string) (isGitRepo bool, rootPath string) {
	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return true, TrimLastNewline(string(out)) // Since rev-parse is considered porcelain and may change, need to error-check its output
	} else {
		return false, ""
	}
}

func IsFolderGitRepo(path string) (isGitRepo bool, status string) {
	// Alternative: git rev-parse
	// For individual files: git ls-files --error-unmatch -- 'Filename', return code == 0
	cmd := exec.Command("git", "status", "--porcelain")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return true, string(out)
	} else {
		return false, ""
	}
}

func CheckGitRepoLocalBranch(path string) string {
	cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return TrimLastNewline(string(out)) // Since rev-parse is considered porcelain and may change, need to error-check its output
	} else {
		return ""
	}
}

// Length of a git revision hash.
const gitRevisionLength = 40

func CheckGitRepoLocal(path, branch string) string {
	cmd := exec.Command("git", "rev-parse", branch)
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil && len(out) >= gitRevisionLength {
		return string(out[:gitRevisionLength])
	} else {
		return ""
	}
}

func CheckGitRepoRemote(path, branch string) string {
	cmd := exec.Command("git", "-c", "core.askpass=true", "ls-remote", "--heads", "origin", branch)<commit_msg>vcs: Add a comment explaining "-c core.askpass=true".<commit_after>package vcs

import (
	"os/exec"

	. "gist.github.com/5892738.git"
)

type gitVcs struct {
	commonVcs
}

func (this *gitVcs) Type() Type { return Git }

func (this *gitVcs) GetStatus() string {
	_, status := IsFolderGitRepo(this.rootPath)
	return status
}

func (this *gitVcs) GetRemote() string {
	cmd := exec.Command("git", "ls-remote", "--get-url", "origin")
	cmd.Dir = this.rootPath

	if out, err := cmd.Output(); err == nil {
		return TrimLastNewline(string(out))
	} else {
		return ""
	}
}

func (this *gitVcs) GetDefaultBranch() string {
	return "master"
}

func (this *gitVcs) GetLocalBranch() string {
	return CheckGitRepoLocalBranch(this.rootPath)
}

func (this *gitVcs) GetLocalRev() string {
	return CheckGitRepoLocal(this.rootPath, this.GetDefaultBranch())
}

func (this *gitVcs) GetRemoteRev() string {
	return CheckGitRepoRemote(this.rootPath, this.GetDefaultBranch())
}

// ---

func GetGitRepoRoot(path string) (isGitRepo bool, rootPath string) {
	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return true, TrimLastNewline(string(out)) // Since rev-parse is considered porcelain and may change, need to error-check its output
	} else {
		return false, ""
	}
}

func IsFolderGitRepo(path string) (isGitRepo bool, status string) {
	// Alternative: git rev-parse
	// For individual files: git ls-files --error-unmatch -- 'Filename', return code == 0
	cmd := exec.Command("git", "status", "--porcelain")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return true, string(out)
	} else {
		return false, ""
	}
}

func CheckGitRepoLocalBranch(path string) string {
	cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil {
		return TrimLastNewline(string(out)) // Since rev-parse is considered porcelain and may change, need to error-check its output
	} else {
		return ""
	}
}

// Length of a git revision hash.
const gitRevisionLength = 40

func CheckGitRepoLocal(path, branch string) string {
	cmd := exec.Command("git", "rev-parse", branch)
	cmd.Dir = path

	if out, err := cmd.Output(); err == nil && len(out) >= gitRevisionLength {
		return string(out[:gitRevisionLength])
	} else {
		return ""
	}
}

func CheckGitRepoRemote(path, branch string) string {
	// true here is not a boolean value, but a command /bin/true that will make git think it asked for a password,
	// and prevent potential interactive password prompts (opting to return failure exit code instead).
	cmd := exec.Command("git", "-c", "core.askpass=true", "ls-remote", "--heads", "origin", branch)
{'fraction_non_alphanumeric': 0.15208535402521822, 'fraction_numerical': 0.0038797284190106693, 'mean_word_length': 4.140578265204387, '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': '1713808', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1760, 'n_words': 503}