move git commit to a module

parent 76b1053f
import { execSync } from "child_process"
export function run(repositoryDir: string, args: string): string {
return execSync(`git ${args}`, { cwd: repositoryDir })
.toString()
.trim()
}
export function test(repositoryDir: string, args: string): boolean {
try {
execSync(`git ${args}`, {
cwd: repositoryDir,
stdio: ["ignore", "pipe", "pipe"],
})
return true
} catch (childProcess) {
if (childProcess.status != 0) return false
throw childProcess
}
}
export function commit(repositoryDir: string, message: string): boolean {
execSync("git add .", {
cwd: repositoryDir,
env: process.env,
encoding: "utf-8",
stdio: ["ignore", "ignore", "pipe"],
})
try {
execSync(`git commit -m "${message}"`, {
cwd: repositoryDir,
env: process.env,
encoding: "utf-8",
stdio: ["ignore", "pipe", "pipe"],
})
return true
} catch (childProcess) {
if (
childProcess.stdout === null ||
!/nothing to commit/.test(childProcess.stdout)
) {
console.error(childProcess.output)
throw childProcess
}
return false
}
}
......@@ -4,6 +4,8 @@ import commandLineArgs from "command-line-args"
import fs from "fs-extra"
import path from "path"
import * as git from "../git"
import { cleanActeur } from "../cleaners/acteurs"
import { cleanAmendement } from "../cleaners/amendements"
import { cleanDocumentOrDivision } from "../cleaners/documents"
......@@ -581,27 +583,7 @@ function commitAndPush(
): boolean {
let error = false
if (commit) {
execSync("git add .", {
cwd: repositoryDir,
env: process.env,
encoding: "utf-8",
stdio: ["ignore", "ignore", "pipe"],
})
try {
execSync('git commit -m "Nouvelle moisson"', {
cwd: repositoryDir,
env: process.env,
encoding: "utf-8",
})
} catch (childProcess) {
if (
childProcess.stderr === null ||
!/nothing to commit/.test(childProcess.stdout)
) {
console.error(childProcess.output)
throw childProcess
}
}
git.commit(repositoryDir, "Nouvelle moisson")
for (const remote of remotes || []) {
try {
execSync(`git push ${remote} master`, {
......
let sinon = require("sinon")
import fs from "fs-extra"
import { assert } from "chai"
import * as git from "../src/git"
import temp from "temp"
suite("git")
test("#commit", async function() {
const log = sinon.stub(console, "log")
const error = sinon.stub(console, "error")
const tmpDir = temp.mkdirSync("git")
fs.writeFileSync(`${tmpDir}/A`, "AA")
git.run(tmpDir, "init")
git.run(tmpDir, "config commit.gpgsign false")
git.run(tmpDir, "config user.email 'tricoteuses@tricoteuses.fr'")
git.run(tmpDir, "config user.name 'Tricoteuses données Assemblée'")
const message = "MESSAGE"
assert.equal(git.commit(tmpDir, message), true)
const gitLog = git.run(tmpDir, "log --oneline -1")
assert.include(gitLog, message)
assert.equal(git.commit(tmpDir, message), false)
assert.throws(
() => git.commit(tmpDir, 'A" --invalid-option "A'),
"unknown option `invalid-option",
)
log.restore()
error.restore()
})
test("#test", async function() {
const tmpDir = temp.mkdirSync("git")
git.run(tmpDir, "init")
git.run(tmpDir, "config commit.gpgsign false")
git.run(tmpDir, "config user.email 'tricoteuses@tricoteuses.fr'")
git.run(tmpDir, "config user.name 'Tricoteuses données Assemblée'")
assert(git.test(tmpDir, "rev-parse"))
assert.equal(git.test(tmpDir, "rev-parse unknown"), false)
})
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment