Skip to content
Permalink
Browse files

fix: use git to check that you're in a Git repository (#689)

* test(core): added init-git tests to cover known cases

* test(core): add failing test for init-git

catches case where init-git is run within a subdirectory of a known
repository and forge should not create a nested .git directory

* fix(core): do not initialize when in an existing git repository
  • Loading branch information
shiftkey authored and MarshallOfSound committed Feb 8, 2019
1 parent d15a8cc commit 72b8eea4309fb93a5ee2e41d66899f3f31282f9a
Showing with 83 additions and 11 deletions.
  1. +15 −11 packages/api/core/src/api/init-scripts/init-git.ts
  2. +68 −0 packages/api/core/test/slow/init_git_spec_slow.ts
@@ -1,24 +1,28 @@
import { asyncOra } from '@electron-forge/async-ora';
import { exec } from 'child_process';
import debug from 'debug';
import fs from 'fs-extra';
import path from 'path';

const d = debug('electron-forge:init:git');

export default async (dir: string) => {
await asyncOra('Initializing Git Repository', async () => {
await new Promise(async (resolve, reject) => {
if (await fs.pathExists(path.resolve(dir, '.git'))) {
d('.git directory already exists, skipping git initialization');
return resolve();
}
d('executing "git init" in directory:', dir);
exec('git init', {
await new Promise((resolve, reject) => {
exec('git rev-parse --show-toplevel', {
cwd: dir,
}, (err) => {
if (err) return reject(err);
resolve();
if (err) {
// not run within a Git repository
d('executing "git init" in directory:', dir);
exec('git init', {
cwd: dir,
}, (err) => {
if (err) return reject(err);
resolve();
});
} else {
d('.git directory already exists, skipping git initialization');
resolve();
}
});
});
});
@@ -0,0 +1,68 @@
import { execSync } from 'child_process';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';

import { expect } from 'chai';

import initGit from '../../src/api/init-scripts/init-git';

let dir: string;
let dirID = Date.now();

const ensureTestDirIsNonexistent = async () => {
dir = path.resolve(os.tmpdir(), `electron-forge-git-test-${dirID}`);
dirID += 1;
await fs.remove(dir);
};

describe('init-git', () => {
beforeEach(async () => {
await ensureTestDirIsNonexistent();
await fs.mkdir(dir);
});

it('creates Git repository when run inside non-Git directory', async () => {
await initGit(dir);
const gitDir = path.join(dir, '.git');
expect(await fs.pathExists(gitDir), 'the .git directory inside the folder').to.equal(true);
});

it('skips when run at root of Git repository', async () => {
await execSync('git init', { cwd: dir });

const gitDir = path.join(dir, '.git');
const config = path.join(gitDir, 'config');
const statBefore = await fs.lstat(config);
const before = statBefore.mtimeMs;

await initGit(dir);

const statAfter = await fs.lstat(config);
const after = statAfter.mtimeMs;

expect(after, 'the config file in the repository').to.equal(before);
});

it('skips when run in subdirectory of Git repository', async () => {
await execSync('git init', { cwd: dir });

const gitDir = path.join(dir, '.git');
const config = path.join(gitDir, 'config');
const statBefore = await fs.lstat(config);
const before = statBefore.mtimeMs;

const subdir = path.join(dir, 'some', 'other', 'folder');
const innerGitDir = path.join(subdir, '.git');

await fs.mkdirp(subdir);

await initGit(subdir);

const statAfter = await fs.lstat(config);
const after = statAfter.mtimeMs;

expect(after, 'the config file in the repository').to.equal(before);
expect(await fs.pathExists(innerGitDir), 'a nested .git directory inside the repository').to.equal(false);
});
});

0 comments on commit 72b8eea

Please sign in to comment.
You can’t perform that action at this time.