Search code examples
gitgit-alias

Trouble with Custom Git Aliases on Windows


I have these 2 aliases:

ctt = "!f() { $(git rev-parse --abbrev-ref HEAD) -ne 'develop' && git tag -a testing-$(git rev-parse --abbrev-ref HEAD)-$(date +'%Y%m%d')-$1 -m '$2' && git push --tags; }; f"
cpt = "!f() { [ "$(git rev-parse --abbrev-ref HEAD)" = "develop" ] && git tag -a production-$1-$(date +'%Y%m%d')-$2 -m '$3' && git push --tags; }; f"

So that I can do:

git ctt BranchName rc1 "Trying to add XYZ feature" to create tag testing-BranchName-20240229-rc1

git cpt FeatureName r1 "XYZ feature release" to create tag production-FeatureName-20240229-r1

I wanted ctt to only work on branches that are not develop and cpt to only work on develop so I added the condition but I get the error:

cpt_test = "!f() { [ "$(git rev-parse --abbrev-ref HEAD)" = "develop" ] && echo production-$1-$(date +'%Y%m%d')-$2 }; f"
fatal: bad config line 21 in file C:/Users/david/.gitconfig

I've tried escaping quotes, adding \ to make it multiline and see if I had any error but nothing I've tried works.

I'm on Windows 11, git version 2.39.2.windows.1

Whole .gitconfig

[user]
    email = [email protected]
    name = David 
[credential "http://192.168.1.89"]
    provider = generic
[pager]
    log = false
    show = false
[alias]
    lg1 = "log --reverse --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)(%ar)%C(reset) %C(blue)- %an%C(reset) - %C(white)%s%C(reset) %C(auto)%d%C(reset)' --all"
    lg2 = "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold cyan)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"
    lg = !"git lg1"
    lg3 = log -10 --reverse --abbrev-commit
    files = !"git show --pretty --name-only $1 #"
    pop = !"git stash pop"
    pa = !"git push --all"
    paf = !"git push --all --force"
    dbe = !"git branch -d $1 && git push origin --delete $1 && :"
    # I've tried:
    ctt_test = "!f() { ctt 'tag message' } f"
    #ctt_test = !"f() { ctt 'tag message' ;}"
    #ctt_test = !"ctt 'tag message'"
[diff]
    tool = icdiff
[difftool]
    prompt = false
[difftool "icdiff"]
    cmd = C:/Users/david/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0/LocalCache/local-packages/python310/site-packages/icdiff.py --line-numbers $LOCAL $REMOTE
[rerere]
    enabled = true
[push]
    autoSetupRemote = true
[credential "http://git.local"]
    provider = generic

profile:

function Create-TestingTag {
    $currentBranch = git rev-parse --abbrev-ref HEAD

    if ($currentBranch -ne 'develop') {
        $releaseCandidateNumbers = git tag -l "testing-$currentBranch-*" | ForEach-Object { [regex]::Match($_, '(?<=testing-$currentBranch-\d{8}-)rc\d+').Value } | Sort-Object -Descending

        if ($releaseCandidateNumbers.Count -gt 0) {
            $latestReleaseCandidate = $releaseCandidateNumbers[0]
            $releaseNumber = [regex]::Match($latestReleaseCandidate, '\d+').Value
            $newReleaseCandidateNumber = [int]$releaseNumber + 1
        } else {
            $newReleaseCandidateNumber = 1
        }


        $tagName = "testing-$currentBranch-{0:yyyyMMdd}-rc$newReleaseCandidateNumber" -f (Get-Date)
        $tagMessage = $args[0]
        git tag -a $tagName -m '$tagMessage'
        git push --tags
    } else {
        Write-Output "You're not on 'develop' branch, exiting."
    }
}

function Create-ProductionTag {
    $currentBranch = git rev-parse --abbrev-ref HEAD

    if ($currentBranch == 'develop') {
        $releaseNumbers = git tag -l 'production-*' | ForEach-Object { [regex]::Match($_, '(?<=production-\w+-\d{8}-)r\d+').Value } | Sort-Object -Descending
        
        if ($releaseNumbers.Count -gt 0) {
            $latestRelease = $releaseNumbers[0]
            $releaseNumber = [regex]::Match($latestRelease, '\d+').Value
            $newReleaseNumber = [int]$releaseNumber + 1
        } else {
            $newReleaseNumber = 1
        }

        $tagName = "production-$($args[0])-{0:yyyyMMdd}-r$newReleaseNumber" -f (Get-Date)
        $tagMessage = $args[1]
        git tag -a $tagName -m '$tagMessage'
        git push --tags
    } else {
        Write-Output "You're not on 'develop' branch, exiting."
    }
}

New-Alias -Name 'ctt' -Value 'Create-TestingTag' -Scope Global
New-Alias -Name 'cpt' -Value 'Create-ProductionTag' -Scope Global


Solution

  • Try

    cpt = "!f() { if [[ \"$(git rev-parse --abbrev-ref HEAD)\" = develop ]];then echo production-$1-$(date +'%Y%m%d')-$2;fi }; f"
    

    I'm not quite sure what the cause is in your version. This modified version can work in git-bash.

    Note that it's not a valid alias name if it contains _. So, rename it. The shell function part needs modification too.

    It would be easier to write the function in ~/.bashrc or other similar files and call it in the alias.

    In the configuration file,

    [alias]
        cpt = "!f() { if [[ \"$(git rev-parse --abbrev-ref HEAD)\" = develop ]];then echo production-$1-$(date +'%Y%m%d')-$2;fi }; f"