Skip to content

Fix bug: tests overwrites git local config #52

@josecelano

Description

@josecelano

We have to change the createGitInstance function. I think I had a problem with SimpleGit because it did not get the git global configuration. I decided to copy the global configuration to the local one because I thought it was an in-memory git config used by SimpleGit (probably because I was working with another Git wrapper for Python that does so), and it actually overwrites the local Git config. If you run the tests in your local machine it will change your local git config for the project (in PROJCT_ROOT/.git/config).

We have to double-check that SImpleGit is not getting the right global git config. If that's right we can use the env variables GIT_*

export async function createGitInstance(
  gitRepoDir: GitRepoDir
): Promise<SimpleGit> {
  const git: SimpleGit = simpleGit(gitRepoDir.getDirPath())

  /*
   * We need to pass the env vars to the child git process
   * because the user might want to use some env vars like:
   *
   * For GPG:
   * GNUPGHOME
   *
   * For git commit:
   * GIT_AUTHOR_NAME
   * GIT_AUTHOR_EMAIL
   * GIT_AUTHOR_DATE
   * GIT_COMMITTER_NAME
   * GIT_COMMITTER_EMAIL
   * GIT_COMMITTER_DATE
   *
   * TODO: Code review. Should we pass only the env vars used by git commit?
   */
  git.env(process.env)

  /*
   * It seems the `git` child process does not apply the global git config,
   * at least for the `git commit` command. You have to overwrite local config with the global.
   */

  const userName = await getGitConfig('user.name', git)
  if (userName) {
    git.addConfig('user.name', userName)
  }

  const userEmail = await getGitConfig('user.email', git)
  if (userEmail) {
    git.addConfig('user.email', userEmail)
  }

  const userSigningkey = await getGitConfig('user.signingkey', git)
  if (userSigningkey) {
    git.addConfig('user.signingkey', userSigningkey)
  }

  const commitGpgsign = await getGitConfig('commit.gpgsign', git)
  if (commitGpgsign) {
    git.addConfig('commit.gpgsign', commitGpgsign)
  }

  return git
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions