|
import random |
|
import subprocess |
|
|
|
import datasets |
|
|
|
""" |
|
git init |
|
git remote add origin https://github.com/huggingface/evaluate.git |
|
git fetch --depth 2 origin 9b056cdd5eb95459ae80142014865263e7dd75b8 |
|
# Get file after change |
|
git checkout FETCH_HEAD -- README.md |
|
# Get file before change |
|
git checkout FETCH_HEAD^ -- README.md |
|
""" |
|
|
|
|
|
def run_in_shell(cmd: str, cwd=None, timeout=120): |
|
|
|
completed = subprocess.run([cmd], capture_output=True, shell=True, cwd=cwd, timeout=timeout) |
|
return completed |
|
|
|
def get_file_contents(commit, old_file, new_file, repo, cwd=None): |
|
|
|
completed = run_in_shell("git init", cwd=cwd) |
|
completed = run_in_shell("git remote add origin " + repo, cwd=cwd) |
|
completed = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd) |
|
completed = run_in_shell("git checkout FETCH_HEAD -- " + new_file, cwd=cwd) |
|
new_contents = run_in_shell("cat " + new_file, cwd=cwd).stdout.decode() |
|
completed = run_in_shell("git checkout FETCH_HEAD^ -- " + old_file, cwd=cwd) |
|
|
|
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"] |
|
repos = list(set(ex["repos"].split(","))) |
|
old_file = ex["old_file"] |
|
new_file = ex["new_file"] |
|
for repo in repos: |
|
repo = "https://www.github.com/" + repo + ".git" |
|
|
|
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)) |
|
continue |
|
ex["new_contents"] = new_contents |
|
ex["old_contents"] = old_contents |
|
run_in_shell("rm -rf " + str(random_dir)) |
|
return ex |
|
|
|
ex["new_contents"] = "" |
|
ex["old_contents"] = "" |
|
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=128) |
|
|
|
diff_ds.to_json("diffs.jsonl") |
|
|