<commit_before>"""Commands to be used as console scripts for git_origin."""

from sys import argv
from os import makedirs, environ
from os.path import join, isabs
from errno import EEXIST
from subprocess import call
from git.repo import Repo
from git.cmd import Git


notes_ref = "refs/notes/origins"


class Messenger:
    def __init__(self, **kwargs):
        self.__dict__ = kwargs

def _commit(repo, ref="HEAD"):
    id = repo.git.rev_parse(ref, verify=True).strip()
    return repo.commit(id)

def _origins(commit):
    repo = commit.repo
    notes = _commit(repo, notes_ref)
    for fn in notes.tree:
        if fn == commit.id:
            blob = notes.tree[fn]
            return [repo.commit(id.strip()) for id in blob.data.splitlines()]

def _add_origin(origin, commit):
    origin_wd = join(commit.repo.path, "origins-wd")
    try:
        makedirs(origin_wd)
    except OSError, e:
        if e.errno != EEXIST:
            raise

    originenv = {
        "GIT_INDEX_FILE": join(commit.repo.path, "origins-index"),
        "GIT_WORK_TREE": origin_wd
    }

    ret = call(["git", "read-tree", notes_ref], env=originenv)
    if ret != 0:
        raise Exception("Unable to check out origins.")

    ret = call(["git", "checkout-index", "-af"], env=originenv)
    if ret != 0:
        raise Exception("Unable to check out origins.")

def new_blob(repo, data, path=None):
    args = dict(stdin=True, t="blob", w=True, input=data)
    if path:
        args["path"] = path
    hash = repo.git.hash_object(**args)
    return repo.blob(hash)

class Index(object):
    def __init__(self, repo, path=None):
        if path is None:
            path = join(repo.path, "index")
        self.git = repo.git
        #else:
        #    self.git = Git(repo.wd, index)
        self.path = path
        self.repo = repo

    # FIXME: Make update operate against the self.path index file.
    def update(self, path, cacheinfo=None, **kwargs):
        if isabs(path):
            path = path[1:]

        if cacheinfo:
            self.git.update_index("--cacheinfo", cacheinfo.mode,
                                       cacheinfo.blob.id, path, **kwargs)
        else:
            self.git.update_index(path, **kwargs)

# Commands
def origin():
    """Add the supplied commit-ish as an origin on HEAD (or the second supplied commit-ish)."""

    if "GIT_NOTES_REF" in environ:
        del environ["GIT_NOTES_REF"]

    repo = Repo()
    if len(argv) > 1:
        commitid = repo.git.rev_parse(argv[1].strip(), verify=True)
        commit = repo.commit(commitid)
        origins = _origins(commit)
        if origins:
            print("Origins for %s:" % commit)
            for origin in origins:
                print("    %s" % origin)
        #_add_origin(commit, commit)
        #blob = new_blob(repo, "Foo\nBar\nBaz", "/foo")
        #cacheinfo = Messenger(mode="0755", blob=blob)
        #index = Index(repo)
        #index.update("/foo", cacheinfo, add=True)
<commit_msg>Make our Index class call a git that obeys its path, and do a first pass functional git-origin implementation.<commit_after>"""Commands to be used as console scripts for git_origin."""

from sys import argv
from os import makedirs, environ
from os.path import join, isabs
from errno import EEXIST
from subprocess import call
from git.repo import Repo
from git.cmd import Git


notes_ref = "refs/notes/origins"


class Messenger:
    def __init__(self, **kwargs):
        self.__dict__ = kwargs

def _commit(repo, ref="HEAD"):
    id = repo.git.rev_parse(ref, verify=True).strip()
    return repo.commit(id)

def _origins(commit):
    repo = commit.repo
    notes = _commit(repo, notes_ref)
    for fn in notes.tree:
        if fn == commit.id:
            blob = notes.tree[fn]
            return [repo.commit(id.strip()) for id in blob.data.splitlines()]

def _add_origin(origin, commit):
    origin_wd = join(commit.repo.path, "origins-wd")
    try:
        makedirs(origin_wd)
    except OSError, e:
        if e.errno != EEXIST:
            raise

    originenv = {
        "GIT_INDEX_FILE": join(commit.repo.path, "origins-index"),
        "GIT_WORK_TREE": origin_wd
    }

    ret = call(["git", "read-tree", notes_ref], env=originenv)
    if ret != 0:
        raise Exception("Unable to check out origins.")

    ret = call(["git", "checkout-index", "-af"], env=originenv)
    if ret != 0:
        raise Exception("Unable to check out origins.")

def new_blob(repo, data, path=None):
    args = dict(stdin=True, t="blob", w=True, input=data)
    if path:
        args["path"] = path
    hash = repo.git.hash_object(**args)
    return repo.blob(hash)

class Index(object):
    def __init__(self, repo, path=None):
        if path is None:
            path = join(repo.path, "index")
            self.git = repo.git
        else:
            self.git = Git(repo.wd)
            env = {
                "GIT_INDEX_FILE": path,
            }
            if not repo.bare:
                env["GIT_WORK_TREE"] = repo.wd
            self.git.extra["env"] = env

        self.path = path
        self.repo = repo

    def update(self, path, cacheinfo=None, **kwargs):
        if isabs(path):
            path = path[1:]

        if cacheinfo:
            self.git.update_index("--cacheinfo", cacheinfo.mode,
                                       cacheinfo.blob.id, path, **kwargs)
        else:
            self.git.update_index(path, **kwargs)

# Commands
def origin():
    """Add the supplied commit-ish as an origin on HEAD (or the second supplied commit-ish)."""

    if "GIT_NOTES_REF" in environ:
        del environ["GIT_NOTES_REF"]

    repo = Repo()
    if len(argv) > 1:
        commitid = repo.git.rev_parse(argv[1].strip(), verify=True)
        commit = repo.commit(commitid)
        origins = _origins(commit) or []
        if origins:
            print("Origins for %s:" % commit)
            for origin in origins:
                print("    %s" % origin)

        try:
            neworigin = _commit(repo, argv[2])
        except IndexError:
            neworigin = _commit(repo)

        originids = [c.id for c in origins]
        if neworigin.id in originids:
            raise SystemExit(0)

        originids.append(neworigin.id)
        blob = new_blob(repo, "\n".join(originids), commitid)
        cacheinfo = Messenger(mode="0644", blob=blob)
        index = Index(repo, join(repo.path, "origins-index"))
        index.git.read_tree(notes_ref)
        index.update(commitid, cacheinfo, add=True)
        newtreeid = index.git.write_tree().strip()
        newcommitid = repo.git.commit_tree(newtreeid,
                                           "-p", repo.commit(notes_ref).id,
                                           input="Add origin %s to commit %s" % (neworigin.id, commitid))
        repo.git.update_ref(notes_ref, newcommitid.strip())

{'fraction_non_alphanumeric': 0.12416009348524686, 'fraction_numerical': 0.00292141396435875, 'mean_word_length': 2.2465623518255096, 'pattern_counts': {'":': 5, '<': 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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1033314', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2121, 'n_words': 571}