Skip to content

Commit

Permalink
move GitUtils back to helix-cli #523
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg committed Feb 11, 2019
1 parent 83bac66 commit a50614f
Show file tree
Hide file tree
Showing 9 changed files with 2,169 additions and 1,638 deletions.
3,559 changes: 1,927 additions & 1,632 deletions package-lock.json

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions src/GitUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint no-console: off */

const $ = require('shelljs');
const path = require('path');
const { GitUrl } = require('@adobe/helix-shared');

function runIn(dir, fn) {
const pwd = $.pwd();
try {
if (dir) {
$.cd(dir);
}
return fn();
} finally {
if (dir) {
$.cd(pwd);
}
}
}

class GitUtils {
static isDirty(dir) {
return runIn(dir, () => $
.exec('git status --porcelain', {
silent: true,
})
.stdout.replace(/\n/g, '')
.replace(/[\W]/g, '-').length > 0);
}

static getBranch(dir) {
return runIn(dir, () => {
const rev = $
.exec('git rev-parse HEAD', {
silent: true,
})
.stdout.replace(/\n/g, '');

const tag = $
.exec(`git name-rev --tags --name-only ${rev}`, {
silent: true,
})
.stdout.replace(/\n/g, '');

const branchname = $
.exec('git rev-parse --abbrev-ref HEAD', {
silent: true,
})
.stdout.replace(/\n/g, '');

return tag !== 'undefined' ? tag : branchname;
});
}

static getBranchFlag(dir) {
return GitUtils.isDirty(dir) ? 'dirty' : GitUtils.getBranch(dir).replace(/[\W]/g, '-');
}

static getRepository(dir) {
return runIn(dir, () => {
const repo = GitUtils.getOrigin()
.replace(/[\W]/g, '-');
if (repo !== '') {
return repo;
}
return `local--${path.basename(process.cwd())}`;
});
}

static getOrigin(dir) {
return runIn(dir, () => {
try {
const origin = $.exec('git config --get remote.origin.url', {
silent: true,
}).stdout.replace(/\n/g, '');
return origin;
} catch (e) {
return '';
}
});
}

static getOriginURL(dir) {
return new GitUrl(GitUtils.getOrigin(dir));
}

static getCurrentRevision(dir) {
return runIn(dir, () => {
const rev = $
.exec('git rev-parse HEAD', {
silent: true,
})
.stdout.replace(/\n/g, '')
.replace(/[\W]/g, '-');
return rev;
});
}
}

module.exports = GitUtils;
3 changes: 2 additions & 1 deletion src/config/config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/
const fs = require('fs-extra');
const path = require('path');
const { GitUrl, GitUtils } = require('@adobe/helix-shared');
const { GitUrl } = require('@adobe/helix-shared');
const GitUtils = require('../GitUtils');

const DEFAULT_CONFIG = path.resolve(__dirname, 'default-config.yaml');

Expand Down
3 changes: 2 additions & 1 deletion src/deploy.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const path = require('path');
const fs = require('fs-extra');
const uuidv4 = require('uuid/v4');
const ProgressBar = require('progress');
const { HelixConfig, GitUrl, GitUtils } = require('@adobe/helix-shared');
const { HelixConfig, GitUrl } = require('@adobe/helix-shared');
const GitUtils = require('./GitUtils');
const useragent = require('./user-agent-util');
const AbstractCommand = require('./abstract.cmd.js');
const PackageCommand = require('./package.cmd.js');
Expand Down
2 changes: 1 addition & 1 deletion src/publish.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const path = require('path');
const glob = require('glob-to-regexp');
const { toBase64 } = require('request/lib/helpers');
const ProgressBar = require('progress');
const { GitUtils } = require('@adobe/helix-shared');
const GitUtils = require('./GitUtils');
const strainconfig = require('./strain-config-utils');
const include = require('./include-util');
const useragent = require('./user-agent-util');
Expand Down
2 changes: 1 addition & 1 deletion test/testBlankRemoteDeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const assert = require('assert');
const path = require('path');
const fse = require('fs-extra');
const Replay = require('replay');
const { GitUtils } = require('@adobe/helix-shared');
const GitUtils = require('../src/GitUtils');
const DemoCommand = require('../src/demo.cmd');

const TEST_DIR = path.resolve(__dirname, 'tmp');
Expand Down
2 changes: 1 addition & 1 deletion test/testDeployCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

const assert = require('assert');
const sinon = require('sinon');
const { GitUtils } = require('@adobe/helix-shared');
const GitUtils = require('../src/GitUtils');
const CLI = require('../src/cli.js');
const DeployCommand = require('../src/deploy.cmd');

Expand Down
3 changes: 2 additions & 1 deletion test/testDeployCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const fs = require('fs-extra');
const assert = require('assert');
const path = require('path');
const $ = require('shelljs');
const { GitUtils, Logger } = require('@adobe/helix-shared');
const { Logger } = require('@adobe/helix-shared');
const { initGit, createTestRoot } = require('./utils.js');
const GitUtils = require('../src/GitUtils');
const BuildCommand = require('../src/build.cmd.js');
const DeployCommand = require('../src/deploy.cmd.js');

Expand Down
121 changes: 121 additions & 0 deletions test/testGitUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

const assert = require('assert');
const path = require('path');
const fse = require('fs-extra');
const crypto = require('crypto');
const shell = require('shelljs');
const GitUtils = require('../src/GitUtils');

if (!shell.which('git')) {
shell.echo('Sorry, this tests requires git');
shell.exit(1);
}

async function createTestRoot() {
const dir = path.resolve(__dirname, 'tmp', crypto.randomBytes(16).toString('hex'));
await fse.ensureDir(dir);
return dir;
}

describe('Testing GitUtils', () => {
let testRoot;
let pwd;
beforeEach(async () => {
testRoot = await createTestRoot();
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello\n', 'utf-8');

// throw a Javascript error when any shell.js command encounters an error
shell.config.fatal = true;

// init git repo
pwd = shell.pwd();
shell.cd(testRoot);
shell.exec('git init');
shell.exec('git add -A');
shell.exec('git commit -m"initial commit."');
});

afterEach(async () => {
shell.cd(pwd);
await fse.remove(testRoot);
});

it('getOrigin #unit', () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.ok(GitUtils.getOrigin());
assert.ok(/dummy/.test(GitUtils.getOrigin()));

shell.cd(pwd);
assert.ok(/dummy/.test(GitUtils.getOrigin(testRoot)));
});

it('getOriginURL #unit', () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.ok(GitUtils.getOriginURL());
assert.equal(GitUtils.getOriginURL().toString(), 'http://github.com/adobe/dummy.git');

shell.cd(pwd);
assert.equal(GitUtils.getOriginURL(testRoot).toString(), 'http://github.com/adobe/dummy.git');
});

it('getBranch #unit', () => {
shell.exec('git checkout -b newbranch');
assert.equal(GitUtils.getBranch(), 'newbranch');

shell.cd(pwd);
assert.equal(GitUtils.getBranch(testRoot), 'newbranch');
});

it('isDirty #unit', async () => {
assert.equal(GitUtils.isDirty(), false);
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello, world.\n', 'utf-8');
assert.equal(GitUtils.isDirty(), true);

shell.cd(pwd);
assert.equal(GitUtils.isDirty(testRoot), true);
});

it('getBranchFlag #unit', async () => {
assert.equal(GitUtils.getBranchFlag(), 'master');
await fse.writeFile(path.resolve(testRoot, 'README.md'), 'Hello, world.\n', 'utf-8');
assert.equal(GitUtils.getBranchFlag(), 'dirty');

shell.cd(pwd);
assert.equal(GitUtils.getBranchFlag(testRoot), 'dirty');
});

it('getRepository #unit', async () => {
shell.exec('git remote add origin http://github.com/adobe/dummy.git');
assert.equal(GitUtils.getRepository(), 'http---github.aaakk.us.kg-adobe-dummy-git');

shell.cd(pwd);
assert.equal(GitUtils.getRepository(testRoot), 'http---github.aaakk.us.kg-adobe-dummy-git');
});

it('getRepository (local) #unit', async () => {
assert.equal(GitUtils.getRepository(), `local--${path.basename(testRoot)}`);

shell.cd(pwd);
assert.equal(GitUtils.getRepository(testRoot), `local--${path.basename(testRoot)}`);
});

it('getCurrentRevision #unit', async () => {
assert.ok(/[0-9a-fA-F]+/.test(GitUtils.getCurrentRevision()));

shell.cd(pwd);
assert.ok(/[0-9a-fA-F]+/.test(GitUtils.getCurrentRevision(testRoot)));
});
});

0 comments on commit a50614f

Please sign in to comment.