Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active December 8, 2019 22:29
Show Gist options
  • Save sanusart/2f226ad21742d79e5e07930c02aabd14 to your computer and use it in GitHub Desktop.
Save sanusart/2f226ad21742d79e5e07930c02aabd14 to your computer and use it in GitHub Desktop.
Check if file with certain extension exists in a commit #nodejs #git
#!/bin/bash
# Usage:
# ./scripts/git-files d304b82
#
# Or in script:
#
# if [ `sh ./scripts/git-files d304b82` -gt 0 ];
# then
# echo "matches";
# else
# echo "no match";
# fi
COMMIT_HASH=$1;
# MATCHES=(
# "components\/*"
# "README"
# );
# PATTERN=`echo "${MATCHES[@]}" | tr ' ' '|'`;
git diff-tree --no-commit-id --name-only -r ${COMMIT_HASH} | awk -F ': ' '/components\/*|README/' | wc -l | xargs;
#!/usr/bin/env node -e
/**
* Check if file with certain extension exists in a commit
*
* Usage in CLI:
* `node isFileTypeInCommit.js ext COMMIT_HASH`
*
* node >= v8.0.0
*
* Useage in script:
* `if [ "$(node isFileTypeInCommit.js json 37c5139ea)" == "true" ]; then echo "yes"; else echo "no"; fi`
*/
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const [ , , extension, commitHash ] = process.argv;
const listFiles = `git diff-tree --no-commit-id --name-only -r ${commitHash}`;
if (process.argv.length !== 4) {
throw new Error(`Usage: "node isFileTypeInCommit.js ext COMMIT_HASH"`);
}
(isFileTypeInCommit = async () => {
const { stdout, stderr } = await exec(listFiles);
if (stderr) throw new Error(`Error: ${stderr}`);
const filesList = stdout.split('\n');
const matchFiles = filesList.filter(el => el.endsWith(`.${extension}`));
console.log(matchFiles.length > 0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment