@@ -9,12 +9,12 @@ class NotFound(Exception):
9
9
class UnknownError (Exception ):
10
10
pass
11
11
12
- def __init__ (self , owner , repo , token , branch = None ):
12
+ def __init__ (self , owner , repo , token , branch = "main" ):
13
13
self .owner = owner
14
14
self .repo = repo
15
15
self .token = token
16
- self .branch = branch
17
16
self .session = Session ()
17
+ self .branch = branch
18
18
19
19
def base_url (self ):
20
20
return "https://api.github.com/repos/{}/{}" .format (self .owner , self .repo )
@@ -42,11 +42,13 @@ def read(self, filepath):
42
42
raise self .UnknownError (response .content )
43
43
44
44
def read_large (self , filepath ):
45
- master = self .session .get (
46
- self .base_url () + "/git/trees/master?recursive=1" , headers = self .headers ()
45
+ "Returns (file_contents_in_bytes, sha1)"
46
+ default_tree = self .session .get (
47
+ self .base_url () + "/git/trees/{}?recursive=1" .format (self .branch ),
48
+ headers = self .headers (),
47
49
).json ()
48
50
try :
49
- tree_entry = [t for t in master ["tree" ] if t ["path" ] == filepath ][0 ]
51
+ tree_entry = [t for t in default_tree ["tree" ] if t ["path" ] == filepath ][0 ]
50
52
except IndexError :
51
53
raise self .NotFound (filepath )
52
54
data = self .session .get (tree_entry ["url" ], headers = self .headers ()).json ()
@@ -108,15 +110,16 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
108
110
},
109
111
headers = self .headers (),
110
112
).json ()
111
- # Retrieve master tree sha
112
- master_sha = self .session .get (
113
- self .base_url () + "/git/trees/master?recursive=1" , headers = self .headers ()
113
+ # Retrieve default tree sha
114
+ default_branch_sha = self .session .get (
115
+ self .base_url () + "/git/trees/{}?recursive=1" .format (self .branch ),
116
+ headers = self .headers (),
114
117
).json ()["sha" ]
115
118
# Construct a new tree
116
119
created_tree = self .session .post (
117
120
self .base_url () + "/git/trees" ,
118
121
json = {
119
- "base_tree" : master_sha ,
122
+ "base_tree" : default_branch_sha ,
120
123
"tree" : [
121
124
{
122
125
"mode" : "100644" , # file (blob),
@@ -131,7 +134,7 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
131
134
# Create a commit which references the new tree
132
135
payload = {
133
136
"message" : commit_message ,
134
- "parents" : [master_sha ],
137
+ "parents" : [default_branch_sha ],
135
138
"tree" : created_tree ["sha" ],
136
139
}
137
140
if committer :
@@ -141,7 +144,7 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
141
144
).json ()
142
145
# Move HEAD reference on master to the new commit
143
146
self .session .patch (
144
- self .base_url () + "/git/refs/heads/master" ,
147
+ self .base_url () + "/git/refs/heads/{}" . format ( self . branch ) ,
145
148
json = {"sha" : created_commit ["sha" ]},
146
149
headers = self .headers (),
147
150
).json ()
0 commit comments