Commit
·
a1e1142
1
Parent(s):
a393e0d
Create get_commits.py
Browse files- get_commits.py +69 -0
get_commits.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
"""
|
7 |
+
git init
|
8 |
+
git remote add origin https://github.com/huggingface/evaluate.git
|
9 |
+
git fetch --depth 2 origin 9b056cdd5eb95459ae80142014865263e7dd75b8
|
10 |
+
# Get file after change
|
11 |
+
git checkout FETCH_HEAD -- README.md
|
12 |
+
# Get file before change
|
13 |
+
git checkout FETCH_HEAD^ -- README.md
|
14 |
+
"""
|
15 |
+
|
16 |
+
#Shell utils
|
17 |
+
def run_in_shell(cmd: str, cwd=None):
|
18 |
+
completed = subprocess.run([cmd], capture_output=True, shell=True, cwd=cwd)
|
19 |
+
return completed
|
20 |
+
|
21 |
+
def get_file_contents(commit, old_file, new_file, repo, cwd=None):
|
22 |
+
|
23 |
+
completed = run_in_shell("git init", cwd=cwd)
|
24 |
+
print(completed)
|
25 |
+
completed = run_in_shell("git remote add origin " + repo, cwd=cwd)
|
26 |
+
print(completed)
|
27 |
+
completed = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd)
|
28 |
+
print(completed)
|
29 |
+
completed = run_in_shell("git checkout FETCH_HEAD -- " + new_file, cwd=cwd)
|
30 |
+
print(completed)
|
31 |
+
|
32 |
+
new_contents = run_in_shell("cat " + new_file, cwd=cwd).stdout.decode()
|
33 |
+
|
34 |
+
completed = run_in_shell("git checkout FETCH_HEAD^ -- " + old_file, cwd=cwd)
|
35 |
+
print(completed)
|
36 |
+
if completed.returncode != 0:
|
37 |
+
return (new_contents, "")
|
38 |
+
|
39 |
+
old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode()
|
40 |
+
return (new_contents, old_contents)
|
41 |
+
|
42 |
+
def get_diff(ex):
|
43 |
+
commit_id = ex["commit"]
|
44 |
+
repo = ex["unnested_repo_name"]
|
45 |
+
repo = "https://www.github.com/" + repo + ".git"
|
46 |
+
old_file = ex["old_file"]
|
47 |
+
new_file = ex["new_file"]
|
48 |
+
|
49 |
+
# create a random directory to store the repo
|
50 |
+
random_dir = random.randint(0, 1000000)
|
51 |
+
run_in_shell("mkdir " + str(random_dir))
|
52 |
+
try:
|
53 |
+
new_contents, old_contents = get_file_contents(commit_id, old_file, new_file, repo, cwd=str(random_dir))
|
54 |
+
except Exception as e:
|
55 |
+
print("ERROR", commit_id, old_file, new_file, repo, str(random_dir), e)
|
56 |
+
run_in_shell("rm -rf " + str(random_dir))
|
57 |
+
ex["new_contents"] = ""
|
58 |
+
ex["old_contents"] = ""
|
59 |
+
return ex
|
60 |
+
ex["new_contents"] = new_contents
|
61 |
+
ex["old_contents"] = old_contents
|
62 |
+
run_in_shell("rm -rf " + str(random_dir))
|
63 |
+
return ex
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
ds = datasets.load_dataset("bigcode/github-commits", use_auth_token=True).shuffle()
|
67 |
+
|
68 |
+
diff_ds = ds["train"].map(get_diff, num_proc=50)
|
69 |
+
diff_ds.to_json("diffs.jsonl")
|