How can I dynamically add git reviewers using powershell script?

Viewed 109

I am working on a project where I want to add different reviewers for different branch of my repository.

I generally add reviewers with this command:

git push --receive-pack='git receive-pack --reviewer=abc.def --reviewer=john.doe' origin HEAD:refs/for/<branch_name>

Since I use powershell terminal I wrote few scripts to ease my work. How can I add different reviewers group dynamically based on my current repository branch?

So far my powershell function look like this...

$BranchReviewerMap = @{ 
    'devO' = "'git receive-pack --reviewer=abc.def --reviewer=efg.hij'"
    'devI' = "'git receive-pack --reviewer=m.ks --reviewer=t.ch --reviewer=imt.h'"
    'devT' = "'git receive-pack --reviewer=m.ks --reviewer=t.ch --reviewer=ay.an'"
    'test' = "'git receive-pack --reviewer=m.ks'"
}

function mkb_git_push {
    [CmdletBinding()]
    param (
            [ValidateSet("merge", "commit", "test")]
            [String] $commit_type = "commit"
    )
    $default_commit = "commit"
    $test_commit = "test"
    Clear-Host

    # ===========>>> Please ignore below parts. These are working just fine <<<=========== #
    # Get Current branch
    $cur_branch = git branch --show-current
    # Some other calculation and logic, which works.
    # ===========>>> Please ignore above parts. These are working just fine <<<=========== #

    #Not able to make it work. Tried both from below two lines.
    $reviewer = [String] $BranchReviewerMap[$cur_branch]
    #$reviewer = $BranchReviewerMap[$cur_branch]
    git push --receive-pack=$reviewer origin HEAD:refs/for/$cur_branch
}

The error I get is..

fatal: Gerrit Code Review: $reviewer: not found fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

I am not looking for any if()-else if()-else type solution.

FYI: This is my first post on. Kindly ignore my mistakes on the post.

1 Answers

At least one problem is in how you construct your hashtable:

Do not include embedded '...'-quoting in the entry values; syntactic quoting, during entry definition, is sufficient (you could use single-quoting here, given that the values don't reference PowerShell variables):

$BranchReviewerMap = @{ 
    'devO' = "git receive-pack --reviewer=abc.def --reviewer=efg.hij"
    'devI' = "git receive-pack --reviewer=m.ks --reviewer=t.ch --reviewer=imt.h"
    'devT' = "git receive-pack --reviewer=m.ks --reviewer=t.ch --reviewer=ay.an"
    'test' = "git receive-pack --reviewer=m.ks"
}

PowerShell will automatically ensure that --receive-pack=$reviewer is passed as a single argument to git.

Caveat: On Windows, if $reviewer contains spaces (as is the case here), PowerShell will wrap the entire argument in "..." behind the scenes rather than just the part after =.

E.g., the actual Windows command line used behind the scenes may look something like this:

# Command line actually used, as rebuilt by PowerShell behind the scenes. 
git push "--receive-pack=git receive-pack --reviewer=m.ks" origin HEAD:refs/for/test

However, it appears that git on Windows accepts this quoting style; by contrast, other CLIs with similar options may not, such as msiexec.

Related