File size: 2,795 Bytes
a1e1142 c4d6e89 a1e1142 fae37a0 a1e1142 12cb6ed a1e1142 d584ff7 a1e1142 d584ff7 a1e1142 d584ff7 a1e1142 e2e4945 a1e1142 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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
"""
#Shell utils
def run_in_shell(cmd: str, cwd=None, timeout=120):
# Default 2min timeout (mostly for git fetch --depth 2 origin when the repo is private & GH asks for a username)
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)
# If it times out as repo requires a username
if completed.returncode != 0:
return (new_contents, "")
old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode()
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 there's only a new file, but no old file
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"
# 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))
continue
ex["new_contents"] = new_contents
ex["old_contents"] = old_contents
run_in_shell("rm -rf " + str(random_dir))
return ex
# If no repo worked
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.push_to_hub("bigcode/commits", use_auth_token=True) # Not working
diff_ds.to_json("diffs.jsonl")
|