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

use git to check that you're in a Git repository #689

Merged
merged 3 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/api/core/src/api/init-scripts/init-git.ts
Original file line number Diff line number Diff line change
@@ -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();
}
});
});
});
Expand Down
68 changes: 68 additions & 0 deletions packages/api/core/test/slow/init_git_spec_slow.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});