Fix some bugs in newest_study_id and add a test

Firstly, newest_study_id operated on the current branch instead of the master
branch, which is incorrect. Next, it did not take into account that there could
have been newly-created studies on local branches. Thus, it would checkout an
already existing branch and attempt to write a new study to it.