File size: 2,250 Bytes
af3d2c7 e3107d8 af3d2c7 ff09ecc af3d2c7 |
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 |
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=10):
# Default 10sec 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 ""
git_diff = run_in_shell(f"git diff {commit}^ {commit}", cwd=cwd).stdout.decode()
return git_diff
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:
git_diff = 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["diff"] = git_diff
run_in_shell("rm -rf " + str(random_dir))
return ex
# If no repo worked
ex["diff"] = ""
return ex
if __name__ == "__main__":
ds = datasets.load_dataset("bigcode/github-commits", use_auth_token=True).shuffle()
diff_ds = ds["train"].select(range(128)).map(get_diff, num_proc=128)
# diff_ds.push_to_hub("bigcode/commits", use_auth_token=True) # Not working
diff_ds.to_json("gitdiffs.jsonl") |