firecow / gitlab-ci-local

Tired of pushing to test your .gitlab-ci.yml?
MIT License
2.03k stars 115 forks source link

isInGitRepository() is fragile and throws #1250

Closed Flowdalic closed 3 weeks ago

Flowdalic commented 4 weeks ago

On my system

$ ./gitlab-ci-local-bin-4.50.1
Error: Command failed with exit code 128: git rev-parse --is-inside-work-tree
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
    at makeError (/snapshot/firecow-gitlab-ci-local/node_modules/execa/lib/error.js:60:11)
    at handlePromise (/snapshot/firecow-gitlab-ci-local/node_modules/execa/index.js:118:26)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at isInGitRepository (/snapshot/firecow-gitlab-ci-local/src/argv.ts:12:9)
    at Argv.fallbackCwd (/snapshot/firecow-gitlab-ci-local/src/argv.ts:35:15)
    at Function.build (/snapshot/firecow-gitlab-ci-local/src/argv.ts:43:9)
    at handler (/snapshot/firecow-gitlab-ci-local/src/handler.ts:25:18)
    at Object.handler (/snapshot/firecow-gitlab-ci-local/src/index.ts:37:21)

Looking at the implementation of isInGitRepository() we find

    } catch (err: any) {
        if (err.stderr === "fatal: not a git repository (or any of the parent directories): .git") {
            return false;
        }
        throw err;
    }

which compares the stderr output with a fixed String. This is pretty fragile, as this bug shows, because as soon as git returns a (slightly) different output, it becomes impossible to use gitlab-ci-local.

Please make sure that your code is robust and use APIs that are meant for machine/machine interaction.