def init(branch=dolthub_branch): # check if repo already exists in our cwd cwd = os.getcwd() path = os.path.join(cwd, dolthub_repo) if os.path.isdir(path): print(' [*] DoltHub Repo found in ./{}, re-initializing'.format(dolthub_repo)) dolt = Dolt(path) # make sure the data isn't stale and pull new data print(' [*] Performing `dolt pull` to ensure repo is up to date') # check what branch we have b = Dolt.branch(dolt)[0].name print(' [*] Current Branch: {}'.format(b)) # if we are not on the branch passed, then switch if b != branch: try: print(' [*] Checking out branch: {}'.format(branch)) Dolt.checkout(dolt, branch=branch) # recheck the branch b = Dolt.branch(dolt)[0].name print(' [*] Current Branch: {}'.format(b)) except: pass p = Dolt.pull(dolt) s = Dolt.status(dolt) print(' [*] Current Status: {}'.format(s)) else: # clone the database from DoltHub, save it into a var to be referenced for read/write purposes print(' [*] Cloning DoltHub Repo: {} into ./{}'.format(dolthub_fullrepo, dolthub_repo)) dolt = Dolt.clone(dolthub_fullrepo, branch=branch) b = Dolt.branch(dolt)[0].name print(' [*] Current Branch: {}'.format(b)) return dolt
def new_branch(dolt, intake): # generates a modifier at the end # city-protect-import-py-5d365f9b new_branch = 'city-protect-import-py-{}'.format( str(uuid.uuid4()).split('-')[0]) try: print(' [*] Creating new Branch: {}'.format(new_branch)) b = Dolt.branch(dolt, branch_name=dolthub_branch, new_branch=new_branch, copy=True) b2 = Dolt.branch(intake, branch_name=intake_branch, new_branch=new_branch, copy=True) #print(b) Dolt.checkout(dolt, branch=new_branch) Dolt.checkout(intake, branch=new_branch) print(' [*] Checking out new branch...') # recheck the branch b = Dolt.branch(dolt)[0].name b2 = Dolt.branch(intake)[0].name print(' [*] Current Branch: {} / {}'.format(b, b2)) except: print(' [!] FATAL ERROR: CANNOT CHECKOUT NEW BRANCH. ABORTING') sys.exit()
def commit(dolt): branch = Dolt.branch(dolt)[0].name print(' [*] Commiting changes to {}'.format(branch)) # make sure we are in our custom branch and not a main one if 'city-protect' in branch: dolt.remote(name='origin', url=dolthub_fullrepo) dolt.add(['data_incident_reports', 'datasets']) dolt.commit('Data Added from cityprotect_load.py ETL Script') dolt.push('origin', branch, set_upstream='origin') print(' [*] Done!') else: print( '[!] ERROR: Cannot push to main branch. Aborting. Use dolt cli to migrate and finalize commit')