Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(platform): Make git to be class-less #6635

Merged
merged 10 commits into from Jul 1, 2020
46 changes: 22 additions & 24 deletions lib/platform/git/index.ts
Expand Up @@ -81,6 +81,24 @@ async function isDirectory(dir: string): Promise<boolean> {
}
}

async function getDefaultBranch(git: Git.SimpleGit): Promise<string> {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
// see https://stackoverflow.com/a/44750379/1438522
try {
const res = await git.raw(['symbolic-ref', 'refs/remotes/origin/HEAD']);
return res.replace('refs/remotes/origin/', '').trim();
} catch (err) /* istanbul ignore next */ {
checkForPlatformFailure(err);
if (
err.message.startsWith(
'fatal: ref refs/remotes/origin/HEAD is not a symbolic ref'
)
) {
throw new Error(REPOSITORY_EMPTY);
}
throw err;
}
}

export class Storage {
private _config: LocalConfig = {} as any;

Expand Down Expand Up @@ -121,35 +139,14 @@ export class Storage {
const gitHead = join(cwd, '.git/HEAD');
let clone = true;

// TODO: move to private class scope
async function setBaseBranchToDefault(git: Git.SimpleGit): Promise<void> {
// see https://stackoverflow.com/a/44750379/1438522
try {
config.baseBranch =
config.baseBranch ||
(await git.raw(['symbolic-ref', 'refs/remotes/origin/HEAD']))
.replace('refs/remotes/origin/', '')
.trim();
} catch (err) /* istanbul ignore next */ {
checkForPlatformFailure(err);
if (
err.message.startsWith(
'fatal: ref refs/remotes/origin/HEAD is not a symbolic ref'
)
) {
throw new Error(REPOSITORY_EMPTY);
}
throw err;
}
}

if (await fs.exists(gitHead)) {
try {
this._git = Git(cwd).silent(true);
await this._git.raw(['remote', 'set-url', 'origin', config.url]);
const fetchStart = Date.now();
await this._git.fetch(['--depth=10']);
await setBaseBranchToDefault(this._git);
config.baseBranch =
config.baseBranch || (await getDefaultBranch(this._git));
await this._resetToBranch(config.baseBranch);
await this._cleanLocalBranches();
await this._git.raw(['remote', 'prune', 'origin']);
Expand Down Expand Up @@ -218,7 +215,8 @@ export class Storage {
throw new Error(REPOSITORY_TEMPORARY_ERROR);
}

await setBaseBranchToDefault(this._git);
config.baseBranch =
config.baseBranch || (await getDefaultBranch(this._git));
}

// istanbul ignore next
Expand Down