@@ -150,13 +150,14 @@ def _call_git(*args, directory=None):
150
150
cmd ,
151
151
cwd = directory ,
152
152
check = True ,
153
+ universal_newlines = True ,
153
154
stdout = subprocess .PIPE ,
154
155
stderr = subprocess .PIPE ,
155
156
)
156
157
except subprocess .CalledProcessError as e :
157
- err = e .stderr . decode ( "utf-8" )
158
+ err = e .stderr
158
159
if not err :
159
- err = e .stdout . decode ( "utf-8" )
160
+ err = e .stdout
160
161
raise RuntimeError (err ) from e
161
162
162
163
return ret
@@ -175,31 +176,6 @@ def remote_repo_exists(org, repository, token=None):
175
176
return True
176
177
177
178
178
- def check_student_repo_exists (org , course , student , token = None ):
179
- """Check if the student has a repository for the course.
180
-
181
- It happens that students delete their repository or do not accept the
182
- invitation to the course. In either case they will not have a repository
183
- yet.
184
- """
185
- # temporarily change log level of github3.py as it prints weird messages
186
- # XXX could be done more nicely with a context manager maybe
187
- gh3_log = logging .getLogger ("github3" )
188
- old_level = gh3_log .level
189
- gh3_log .setLevel ("ERROR" )
190
-
191
- try :
192
- g = gh3 .login (token = token )
193
- repository = "{}-{}" .format (course , student )
194
- g .repository (org , repository )
195
-
196
- except Exception as e :
197
- raise e
198
-
199
- finally :
200
- gh3_log .setLevel (old_level )
201
-
202
-
203
179
def clone_repo (organization , repo , dest_dir ):
204
180
"""Clone `repository` from `org` into a sub-directory in `directory`.
205
181
Assumes you have ssh keys setup for github (rather than using GitHub API
@@ -307,25 +283,43 @@ def init_and_commit(directory, custom_message=False):
307
283
print ("No changes to local repository." )
308
284
309
285
310
- def _master_branch_to_main (directory ):
286
+ def _master_branch_to_main (dir ):
311
287
"""Change the name of the master branch to main
312
288
313
289
Changes the name of the master branch to main for the repo in the
314
290
given directory. Since we create the repo on github first, which now sets
315
291
the default branch to 'main', we need the local repo to match
316
- in order to be able to push with error later.
292
+ in order to be able to push without errors later.
317
293
"""
318
- print (
319
- """Changing name of 'master' branch to 'main'
320
- in repo {}""" .format (
321
- directory
322
- )
323
- )
294
+
324
295
try :
325
- _call_git ("branch" , "-m" , "master" , "main" , directory = directory )
296
+ # first, verify if the master branch exists (this is only true
297
+ # if there are commits on the master branch)
298
+ _call_git (
299
+ "show-ref" ,
300
+ "--quiet" ,
301
+ "--verify" ,
302
+ "refs/heads/master" ,
303
+ directory = dir ,
304
+ )
326
305
except RuntimeError :
327
- # we get here if the master branch has already been renamed
328
- pass
306
+ # master branch verification fails, so we check for main and create
307
+ # it if it does not exist
308
+ try :
309
+ _call_git (
310
+ "show-ref" ,
311
+ "--quiet" ,
312
+ "--verify" ,
313
+ "refs/heads/main" ,
314
+ directory = dir ,
315
+ )
316
+ except RuntimeError :
317
+ # no main branch, create one
318
+ _call_git ("checkout" , "-b" , "main" , directory = dir )
319
+ else :
320
+ # rename branch
321
+ print ("master exists, renaming" )
322
+ _call_git ("branch" , "-m" , "master" , "main" , directory = dir )
329
323
330
324
331
325
def push_to_github (directory , branch = "main" ):
@@ -346,7 +340,7 @@ def pull_from_github(directory, branch="master"):
346
340
raise e
347
341
348
342
349
- def git_init (directory ):
343
+ def git_init (directory , defaultbranch = "main" ):
350
344
"""Initialize git repository"""
351
345
_call_git ("init" , directory = directory )
352
346
@@ -357,6 +351,31 @@ def git_init(directory):
357
351
# about correct function.
358
352
359
353
354
+ def check_student_repo_exists (org , course , student , token = None ):
355
+ """Check if the student has a repository for the course.
356
+
357
+ It happens that students delete their repository or do not accept the
358
+ invitation to the course. In either case they will not have a repository
359
+ yet.
360
+ """
361
+ # temporarily change log level of github3.py as it prints weird messages
362
+ # XXX could be done more nicely with a context manager maybe
363
+ gh3_log = logging .getLogger ("github3" )
364
+ old_level = gh3_log .level
365
+ gh3_log .setLevel ("ERROR" )
366
+
367
+ try :
368
+ g = gh3 .login (token = token )
369
+ repository = "{}-{}" .format (course , student )
370
+ g .repository (org , repository )
371
+
372
+ except Exception as e :
373
+ raise e
374
+
375
+ finally :
376
+ gh3_log .setLevel (old_level )
377
+
378
+
360
379
def close_existing_pullrequests (
361
380
org , repository , branch_base = "new-material-" , token = None
362
381
):
0 commit comments