Commit ·
a1e1142
1
Parent(s): a393e0d
Create get_commits.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import randomimport subprocess
import datasets
"""git initgit remote add origin https://github.com/huggingface/evaluate.gitgit fetch --depth 2 origin 9b056cdd5eb95459ae80142014865263e7dd75b8# Get file after changegit checkout FETCH_HEAD -- README.md# Get file before changegit checkout FETCH_HEAD^ -- README.md"""
#Shell utilsdef run_in_shell(cmd: str, cwd=None): completed = subprocess.run([cmd], capture_output=True, shell=True, cwd=cwd) return completed
def get_file_contents(commit, old_file, new_file, repo, cwd=None):
completed = run_in_shell("git init", cwd=cwd) print(completed) completed = run_in_shell("git remote add origin " + repo, cwd=cwd) print(completed) completed = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd) print(completed) completed = run_in_shell("git checkout FETCH_HEAD -- " + new_file, cwd=cwd) print(completed)
new_contents = run_in_shell("cat " + new_file, cwd=cwd).stdout.decode()
completed = run_in_shell("git checkout FETCH_HEAD^ -- " + old_file, cwd=cwd) print(completed) if completed.returncode != 0: return (new_contents, "")
old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode() return (new_contents, old_contents)
def get_diff(ex): commit_id = ex["commit"] repo = ex["unnested_repo_name"] repo = "https://www.github.com/" + repo + ".git" old_file = ex["old_file"] new_file = ex["new_file"]
# create a random directory to store the repo random_dir = random.randint(0, 1000000) run_in_shell("mkdir " + str(random_dir)) try: new_contents, old_contents = get_file_contents(commit_id, old_file, new_file, repo, cwd=str(random_dir)) except Exception as e: print("ERROR", commit_id, old_file, new_file, repo, str(random_dir), e) run_in_shell("rm -rf " + str(random_dir)) ex["new_contents"] = "" ex["old_contents"] = "" return ex ex["new_contents"] = new_contents ex["old_contents"] = old_contents run_in_shell("rm -rf " + str(random_dir)) return ex
if __name__ == "__main__": ds = datasets.load_dataset("bigcode/github-commits", use_auth_token=True).shuffle()
diff_ds = ds["train"].map(get_diff, num_proc=50) diff_ds.to_json("diffs.jsonl")get_commits.pyAdded+69