Andrey Bushman
Andrey Bushman

Reputation: 12516

Can I use a set of commands (external and internal) for a Git alias?

Git for Windows has a problem with writing to a config file which is located on the network.

The following can be used as a workaround of the bug:

mv "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "./gitconfig.txt"
git config -f "./gitconfig.txt" http.proxy http://@proxy2:8080
mv "./gitconfig.txt" "//hyprostr/dfs/groups/developers/settings/gitconfig.txt"

It is not a nice solution, but it works for me.

But I want to call this set of commands through a Git alias... Pay attention - the first and last commands are external, while the second command is internal.

I read the git help config section about alias.*. After reading I am thinking that it is not possible to write these three commands through an alias. Am I right? I hope I am mistaken, still and it is possible. If I am mistaken, then how can I do it?

UPD (decission)

Thanks to @VonC for his answer. I wrote such script:

# edit_config.sh
# © Andrey Bushman, 2015
# This is a workaround of the problem described here:
# https://github.com/git-for-windows/git/issues/241
# Arguments:
# $1 - target config file
# $2 - parameter full name
# $3 - parameter value
random_file_name=$RANDOM
mv $1 $random_file_name
git config -f $random_file_name $2 $3
mv $random_file_name $1

The script file I located in the network also. Now it can be launched either through Git Bash directly:

sh "//hyprostr/dfs/groups/developers/settings/edit_config.sh" "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "http.proxy" "http://@proxy2:8080"

or through the alias

git config --global alias.editconfig '!sh "//hyprostr/dfs/groups/developers/settings/edit_config.sh"'

I can launch this:

git editconfig "//hyprostr/dfs/groups/developers/settings/gitconfig.txt" "http.proxy" "http://@proxy2:8080"

It works fine for me.

Upvotes: 2

Views: 96

Answers (1)

VonC
VonC

Reputation: 1327394

If it isn't possible directly, you still can:

  • write a shell script (sh: even on Windows) which execute those commands
  • add that script as a git alias.

See "How to embed bash script directly inside a git alias"

 git config --global alias.diffall '!sh myscript.sh'

Actually, if your script is called git-xxx, you won't even need an alias: git xxx will work (and call the git-xxx script file).

Upvotes: 2

Related Questions