From 7caafe3cadf0cebb8c7690554af2041b8a9f37b7 Mon Sep 17 00:00:00 2001 From: nabeix Date: Tue, 4 Jun 2019 02:02:17 +0900 Subject: [PATCH 1/3] feat: assing special environment variables --- README.md | 7 +++++++ src/Config.ts | 2 +- src/Runner.ts | 14 ++++++++++---- src/git.ts | 16 ++++++++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3908e90..59019c4 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,13 @@ module.exports = { } ``` +## Environment variables in command + +git-trigger assign the following environtment variables to a command in `run` parameter. + +- GIT_TRIGGER_COMMIT_HASH +- GIT_TRIGGER_PREV_COMMIT_HASH + ## Live config update `git-trigger` supports live config update by `SIGUSR1` signal. diff --git a/src/Config.ts b/src/Config.ts index 5e523a3..d731ef7 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -1,6 +1,6 @@ export interface ActionConfig { - gitCmd: string; when: "changed"|"always"; + gitCmd?: string; run?: string; } diff --git a/src/Runner.ts b/src/Runner.ts index 41b8085..3666c72 100644 --- a/src/Runner.ts +++ b/src/Runner.ts @@ -1,5 +1,5 @@ import Timer from "./Timer"; -import { Config, RepositoryConfig } from "./Config"; +import { Config } from "./Config"; import * as fs from "fs-extra"; import * as git from "./git"; import * as execa from "execa"; @@ -48,14 +48,20 @@ export default class Runner { console.error("error: " + repo.dir + " is not " + repo.branch + " branch"); continue; } - const changed = await git.remoteChanged(repo.branch, repo.dir); + const [changed, prevHash] = await git.remoteChanged(repo.branch, repo.dir); for (const act of repo.actions) { if (act.when === "changed" && !changed) { continue; } - await git.exec(act.gitCmd, repo.dir); + if (act.gitCmd) { + await git.exec(act.gitCmd, repo.dir); + } if (act.run) { - await execa.shell(act.run, {cwd: repo.dir}); + const commitHash = await git.commitHash(repo.dir); + await execa.shell(act.run, {cwd: repo.dir, env: { + GIT_TRIGGER_COMMIT_HASH: commitHash, + GIT_TRIGGER_PREV_COMMIT_HASH: prevHash + }}); } } console.log("success: " + JSON.stringify({url: repo.url, branch: repo.branch, dir: repo.dir})); diff --git a/src/git.ts b/src/git.ts index ea9028c..1140825 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,12 +14,20 @@ export async function remoteUrl(cwd: string): Promise { return url; } -export async function remoteChanged(branch: string, cwd: string): Promise { - const {stdout: remote} = await execa("git", ["ls-remote", "origin", "refs/heads/" + branch], {cwd}); - const {stdout: local} = await execa("git", ["rev-parse", "HEAD"], {cwd}); - return remote.indexOf(local) !== 0; +export async function remoteChanged(branch: string, cwd: string): Promise<[boolean, string, string]> { + const remote = await (async () => { + const {stdout} = await execa("git", ["ls-remote", "origin", "refs/heads/" + branch], {cwd}); + return stdout.split("\t")[0].split(" ")[0]; + })(); + const local = await commitHash(cwd); + return [remote === local, local, remote]; } export async function exec(cmd: string, cwd: string): Promise { await execa("git", cmd.split(" "), {cwd}); } + +export async function commitHash(cwd: string): Promise { + const { stdout } = await execa("git", ["rev-parse", "HEAD"], {cwd}); + return stdout; +} From a84a6d74d459b6f012eba8180494f8a02a4b48ac Mon Sep 17 00:00:00 2001 From: nabeix Date: Tue, 4 Jun 2019 02:11:18 +0900 Subject: [PATCH 2/3] update doc --- README.md | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 59019c4..b228941 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # git-trigger -git-trigger monitors a git branch and trigger a command defined in config file when changes are detected. +git-trigger monitors a git branch and trigger a shell command defined in config file when changes are detected. ## Usage @@ -53,12 +53,30 @@ module.exports = { } ``` +## Configuration parameters + +- interval: number + - intervals milliseconds for check git branches +- repositories: Array + - url: string + - target git repository + - branch: string + - target branch name + - dir: string + - clone directory + - actions: Array + - when: "changed"|"always" + - gitCmd: string(optional) + - git sub command executed before `run` command + - run: string(optional) + - shell command + ## Environment variables in command -git-trigger assign the following environtment variables to a command in `run` parameter. +git-trigger assigns the following environtment variables to a command in `run` parameter. -- GIT_TRIGGER_COMMIT_HASH -- GIT_TRIGGER_PREV_COMMIT_HASH +- `GIT_TRIGGER_COMMIT_HASH` +- `GIT_TRIGGER_PREV_COMMIT_HASH` ## Live config update From 85825edcd4a99cdce2397b829711d77e215f4a60 Mon Sep 17 00:00:00 2001 From: nabeix Date: Tue, 4 Jun 2019 02:12:33 +0900 Subject: [PATCH 3/3] fix --- src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 1140825..336d990 100644 --- a/src/git.ts +++ b/src/git.ts @@ -20,7 +20,7 @@ export async function remoteChanged(branch: string, cwd: string): Promise<[boole return stdout.split("\t")[0].split(" ")[0]; })(); const local = await commitHash(cwd); - return [remote === local, local, remote]; + return [remote !== local, local, remote]; } export async function exec(cmd: string, cwd: string): Promise {