Skip to content

Add new GitConfig resource#288

Open
Gijsreyn wants to merge 5 commits into
microsoft:mainfrom
Gijsreyn:gh-244/main/add-gitconfig-resource
Open

Add new GitConfig resource#288
Gijsreyn wants to merge 5 commits into
microsoft:mainfrom
Gijsreyn:gh-244/main/add-gitconfig-resource

Conversation

@Gijsreyn
Copy link
Copy Markdown
Contributor

@Gijsreyn Gijsreyn commented May 8, 2026

📖 Description

This pull request adds a new generic GitDsc/GitConfig DSC resource that can manage any git config key-value pair (global, system, local, worktree). It supersedes the other resources, but they are kept for backward compatibility.

Sample configuration: SetGitConfigUserEmail.v3.winget. This sets user.email to a GitHub noreply address globally.

🔗 References

Resolves #244

🔍 Validation

Need pipeline validation. Local tests passed.

✅ Checklist

📋 Issue Type

  • Bug fix
  • Feature
  • Task
Microsoft Reviewers: Open in CodeFlow

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new general-purpose GitDsc/GitConfig DSC resource that can manage any git config key/value pair at the global, system, local, worktree, or unscoped (none) level, alongside helper functions, a sample configuration, Pester tests, and spell-check entries. Existing GitConfigUserName/GitConfigUserEmail resources are preserved for backward compatibility.

Changes:

  • New GitConfig DSC class with Get/Test/Set/Export implementations plus new Invoke-GitWorkingDirectory and Get-GitConfigArguments helpers.
  • Module manifest registers GitConfig (replacing a stale GitConfigFile entry) and fixes a LicenseURI spacing issue.
  • Adds a SetGitConfigUserEmail.v3.winget sample, an extensive Pester suite for the new resource, and longpaths/filemode to the spell-check allowlist.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
resources/GitDsc/GitDsc.psm1 Implements the new GitConfig resource and shared git-config helper functions.
resources/GitDsc/GitDsc.psd1 Exports GitConfig instead of the (nonexistent) GitConfigFile and fixes manifest formatting.
samples/DscResources/GitDsc/SetGitConfigUserEmail.v3.winget New dscv3 sample demonstrating GitConfig setting user.email globally.
tests/GitDsc/GitDsc.tests.ps1 Adds Pester coverage for GitConfig across global/local scopes, export, and validation.
.github/actions/spelling/expect/generic_terms.txt Allows longpaths and filemode in spell check.
Comments suppressed due to low confidence (2)

resources/GitDsc/GitDsc.psm1:740

  • Invoke-GitWorkingDirectory permanently changes the caller's working directory via Set-Location without ever restoring it. Because Set() calls Test() (which calls Get()), and both Get() and Set() invoke Invoke-GitWorkingDirectory, a single Invoke-DscResource call for a local/worktree scope can leave the PowerShell session's current directory inside the user's project directory, affecting subsequent DSC resources and any caller code. Prefer using git -C $ProjectDirectory ... (i.e. pass -C in the argument list returned by Get-GitConfigArguments) so no global state is mutated, or wrap the directory change in Push-Location/Pop-Location with a try/finally.
function Invoke-GitWorkingDirectory {
    param (
        [Parameter(Mandatory)]
        [ConfigLocation] $ConfigLocation,

        [Parameter()]
        [string] $ProjectDirectory
    )

    if ($ConfigLocation -eq [ConfigLocation]::local -or $ConfigLocation -eq [ConfigLocation]::worktree) {
        if ([string]::IsNullOrEmpty($ProjectDirectory)) {
            throw 'ProjectDirectory must be specified for local and worktree configurations.'
        }
        if (-not (Test-Path -Path $ProjectDirectory)) {
            throw "ProjectDirectory '$ProjectDirectory' does not exist."
        }
        Set-Location -Path $ProjectDirectory
    }
}

resources/GitDsc/GitDsc.psm1:567

  • In Set(), the check that throws when Value is empty while Exist is $true runs after Invoke-GitWorkingDirectory has already changed the working directory. That means an invalid call still has the side effect of Set-Location-ing into ProjectDirectory. Move the Value validation above the Invoke-GitWorkingDirectory call so misconfigured input is rejected before any state is mutated.
        Invoke-GitWorkingDirectory -ConfigLocation $this.ConfigLocation -ProjectDirectory $this.ProjectDirectory

        if ($this.Exist) {
            if ([string]::IsNullOrEmpty($this.Value)) {
                throw 'Value must be specified when Exist is true.'
            }
            $gitArgs = Get-GitConfigArguments -ConfigLocation $this.ConfigLocation -Tail @($this.Name, $this.Value)
        } else {
            $gitArgs = Get-GitConfigArguments -ConfigLocation $this.ConfigLocation -Tail @('--unset', $this.Name)
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread samples/DscResources/GitDsc/SetGitConfigUserEmail.v3.winget
Comment thread samples/DscResources/GitDsc/SetGitConfigUserEmail.v3.winget
Comment thread resources/GitDsc/GitDsc.psm1
Comment thread resources/GitDsc/GitDsc.psm1
@github-actions

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requested resource: GitDsc/GitConfig

2 participants