Muennighoff commited on
Commit
af3d2c7
·
1 Parent(s): fae37a0

Create get_single_diffs.py

Browse files
Files changed (1) hide show
  1. get_single_diffs.py +61 -0
get_single_diffs.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, timeout=120):
18
+ # Default 2min timeout (mostly for git fetch --depth 2 origin when the repo is private & GH asks for a username)
19
+ completed = subprocess.run([cmd], capture_output=True, shell=True, cwd=cwd, timeout=timeout)
20
+ return completed
21
+
22
+ def get_file_contents(commit, old_file, new_file, repo, cwd=None):
23
+
24
+ completed = run_in_shell("git init", cwd=cwd)
25
+ completed = run_in_shell("git remote add origin " + repo, cwd=cwd)
26
+ completed = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd)
27
+ # If it times out as repo requires a username
28
+ if completed.returncode != 0:
29
+ return (new_contents, "")
30
+ git_diff = run_in_shell(f"git diff {commit}^ {commit}", cwd=cwd).stdout.decode()
31
+ return git_diff
32
+
33
+ def get_diff(ex):
34
+ commit_id = ex["commit"]
35
+ repos = list(set(ex["repos"].split(",")))
36
+ old_file = ex["old_file"]
37
+ new_file = ex["new_file"]
38
+ for repo in repos:
39
+ repo = "https://www.github.com/" + repo + ".git"
40
+ # create a random directory to store the repo
41
+ random_dir = random.randint(0, 1000000)
42
+ run_in_shell("mkdir " + str(random_dir))
43
+ try:
44
+ git_diff = get_file_contents(commit_id, old_file, new_file, repo, cwd=str(random_dir))
45
+ except Exception as e:
46
+ print("ERROR", commit_id, old_file, new_file, repo, str(random_dir), e)
47
+ run_in_shell("rm -rf " + str(random_dir))
48
+ continue
49
+ ex["diff"] = git_diff
50
+ run_in_shell("rm -rf " + str(random_dir))
51
+ return ex
52
+ # If no repo worked
53
+ ex["diff"] = ""
54
+ return ex
55
+
56
+ if __name__ == "__main__":
57
+ ds = datasets.load_dataset("bigcode/github-commits", use_auth_token=True).shuffle()
58
+
59
+ diff_ds = ds["train"].select(range(128)).map(get_diff, num_proc=128)
60
+ # diff_ds.push_to_hub("bigcode/commits", use_auth_token=True) # Not working
61
+ diff_ds.to_json("gitdiffs.jsonl")