import * as simplegit from 'simple-git/promise' ; import { tempRepoFolder , noop } from '../common' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; /**
 * Returns the name of a repo from a repo path.
 * E.g. return `test_gitHistory` from `https://github.com/DonJayamanne/test_gitHistory.git`.
 */ export function getRepoName ( repoPath ) { if ( ! repoPath . endsWith ( '.git' ) ) { throw new Error ( 'Repo path must be of the form "https://github.com/<org>/<name>.git"' ) ; } return path . basename ( repoPath , '.git' ) ; } export function getLocalPath ( repoPath ) { return path . join ( tempRepoFolder , getRepoName ( repoPath ) ) ; } /**
 * Downloads/clones a git repo.
 */ export async function cloneRepo ( repoPath ) { const localPath = getLocalPath ( repoPath ) ; if ( await fs . existsSync ( path . join ( localPath , 'package.json' ) ) ) { return ; } await simplegit ( ) . clone ( repoPath , localPath ) ; } async function doesBranchExist ( repoPath , branchName ) { const localPath = getLocalPath ( repoPath ) ; const branches = await simplegit ( localPath ) . branchLocal ( ) ; return branches . all . includes ( branchName ) ; } /**
 * Remote branches to checkout locally.
 */ export async function checkoutRemote ( repoPath , remote , branchName ) { await cloneRepo ( repoPath ) ; if ( await doesBranchExist ( repoPath , branchName ) ) { return ; } const localPath = getLocalPath ( repoPath ) ; await simplegit ( localPath ) . checkout ( [ '--track' , `${ remote }/${ branchName }` ] ) ; } export async function createBranch ( repoPath , branchName ) { await cloneRepo ( repoPath ) ; if ( await doesBranchExist ( repoPath , branchName ) ) { return ; } const localPath = getLocalPath ( repoPath ) ; await simplegit ( localPath ) . checkoutLocalBranch ( branchName ) ; } export async function resetRepo ( repoPath ) { await cloneRepo ( repoPath ) ; const localPath = getLocalPath ( repoPath ) ; await simplegit ( localPath ) . reset ( 'hard' ) ; } export async function createTag ( repoPath , tagName ) { await cloneRepo ( repoPath ) ; const localPath = getLocalPath ( repoPath ) ; const tagNames = await simplegit ( localPath ) . tags ( ) ; if ( tagNames . all . includes ( tagName ) ) { return ; } await simplegit ( localPath ) . addTag ( tagName ) ; } export async function changeBranch ( repoPath , branchName ) { await cloneRepo ( repoPath ) ; const localPath = getLocalPath ( repoPath ) ; await simplegit ( localPath ) . checkout ( branchName ) ; } export async function setupDefaultRepo ( ) { const repoPath = 'https://github.com/DonJayamanne/test_gitHistory.git' ; await cloneRepo ( repoPath ) ; await checkoutRemote ( repoPath , 'origin' , 'WIP' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'addBranchTests' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'addTests' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'curvyGraphs' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'jest' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'part1FixStartup' ) . catch ( noop ) ; await checkoutRemote ( repoPath , 'origin' , 'replace-webserver-with-postmessage' ) . catch ( noop ) ; await createBranch ( repoPath , 'localBranch1' ) . catch ( noop ) ; await createBranch ( repoPath , 'localBranch2' ) . catch ( noop ) ; await createBranch ( repoPath , 'localBranch3' ) . catch ( noop ) ; await changeBranch ( repoPath , 'master' ) . catch ( noop ) ; }