Bash Script to use for git commits

im not sure if my logic is correct but im working on converting powershell scripts over to bash. Does this look ok?
6 Replies
ZomaTheMasterOfDisaster
here is the code
!/bin/bash

echo "Please enter the path for a directory you would like to update to github"

# Check if the directory exists

if[[ -d "$1" ]]
then
echo "$1 directory does not exist"
else
# The directory exists now check for a .git folder
gitDir = find ./ -type d -name ".git"

if[[ -d "$gitDir" = ""]]
then
echo "The .git folder is not in the current directory\n"
echo "The program will now end"
exit
else

git add --all

# Ask for a commit message
read -p 'Commit Message: ' gitCommit

# Check for blank
if[[ -z "$gitCommit" ]]
then
echo "You didn't write a commit message"
else
git commit -m "$gitCommit"
git push --all
fi
fi
fi


# Program ends normally
exit
!/bin/bash

echo "Please enter the path for a directory you would like to update to github"

# Check if the directory exists

if[[ -d "$1" ]]
then
echo "$1 directory does not exist"
else
# The directory exists now check for a .git folder
gitDir = find ./ -type d -name ".git"

if[[ -d "$gitDir" = ""]]
then
echo "The .git folder is not in the current directory\n"
echo "The program will now end"
exit
else

git add --all

# Ask for a commit message
read -p 'Commit Message: ' gitCommit

# Check for blank
if[[ -z "$gitCommit" ]]
then
echo "You didn't write a commit message"
else
git commit -m "$gitCommit"
git push --all
fi
fi
fi


# Program ends normally
exit
b1mind
b1mind2y ago
it looks like you need to set some lower tab/space indents is what it looks like >.>;; pew pew *walks out
ZomaTheMasterOfDisaster
here's the original powershell
$gitFolderToUpdate = Read-Host -Prompt 'Please enter the folder path name to update to github'

if ($gitFolderToUpdate -eq "") {
Write-Host('The folder name is empty. Please provide a path to your github project and the folder must include a .git in it')
}
else {

<#
change the to the directory that
#>
$gitFolderToUpdate = Set-Location -Path "$($gitFolderToUpdate)"


<#
Check for the existence of the hidden .git folder to see if the project can be updated to github.
#>
#$checkForGitFolder = Get-ChildItem -Path $gitFolderToUpdate -Name '.git' -Recurse -Directory -Force -Hidden

$checkForGitFolder = Get-ChildItem -Recurse -Filter ".git" -Directory -ErrorAction SilentlyContinue -Path $gitFolderToUpdate

if ($checkForGitFolder -eq '') {
Write-Error('No .git folder exists. Cannot update to github without .git in the directory')
}

<#
We found the git directory so now we can run the git commands to update
#>

git add --all
$commitMessage = Read-Host -Prompt 'Please enter your commit message'

if ($commitMessage -eq '') {
Write-Error('Missing the commit message. Cannot be blank')
}

git commit -m "$($commitMessage)"
git push --all
}
$gitFolderToUpdate = Read-Host -Prompt 'Please enter the folder path name to update to github'

if ($gitFolderToUpdate -eq "") {
Write-Host('The folder name is empty. Please provide a path to your github project and the folder must include a .git in it')
}
else {

<#
change the to the directory that
#>
$gitFolderToUpdate = Set-Location -Path "$($gitFolderToUpdate)"


<#
Check for the existence of the hidden .git folder to see if the project can be updated to github.
#>
#$checkForGitFolder = Get-ChildItem -Path $gitFolderToUpdate -Name '.git' -Recurse -Directory -Force -Hidden

$checkForGitFolder = Get-ChildItem -Recurse -Filter ".git" -Directory -ErrorAction SilentlyContinue -Path $gitFolderToUpdate

if ($checkForGitFolder -eq '') {
Write-Error('No .git folder exists. Cannot update to github without .git in the directory')
}

<#
We found the git directory so now we can run the git commands to update
#>

git add --all
$commitMessage = Read-Host -Prompt 'Please enter your commit message'

if ($commitMessage -eq '') {
Write-Error('Missing the commit message. Cannot be blank')
}

git commit -m "$($commitMessage)"
git push --all
}
I was using nano :3
WillsterJohnson
so it looks to me like all the stuff with $1 is doing nothing, as it simply throws an error if $1 is not a directory and then does nothing else that would require $1 to exist. the gitDir section is a little verbose, you can simply if [[ -d "./.git" ]]. Notice that if[[ -d "$gitDir" = ""]] is invalid, you can either do a -d check, or an equality check, but not both at once. Also, you need to ensure there's a space between the if and the [[ else bash will try to do a property index. you don't need that exit at the end, bash will infer it automatically from EoF. be sure when you are using exit that you provide a code, exit 0 means "end successfully", while any other code is "exit with error", same as JS's process.exit() I've made some changes and this is what I got;
#!/bin/bash


# git will check the .git file for us automatically

# files to add from argv, `--all` if none specified
adders=("$@")
if [[ -z $adders ]]; then
adders="--all"
fi

git add $adders

# Ask for a commit message
read -p "Commit Message: " gitCommit

# Check for blank
if [[ -z "$gitCommit" ]]
then
echo "You didn't write a commit message"
else
git commit -m "$gitCommit"
git push --all
fi

# exit 0 automatically inferred
#!/bin/bash


# git will check the .git file for us automatically

# files to add from argv, `--all` if none specified
adders=("$@")
if [[ -z $adders ]]; then
adders="--all"
fi

git add $adders

# Ask for a commit message
read -p "Commit Message: " gitCommit

# Check for blank
if [[ -z "$gitCommit" ]]
then
echo "You didn't write a commit message"
else
git commit -m "$gitCommit"
git push --all
fi

# exit 0 automatically inferred
ZomaTheMasterOfDisaster
The $1 should be a check if it's actually a directory Being the only argument It should be if $1 is not a directory
dysbulic 🐙
dysbulic 🐙2y ago
I just posted https://discord.com/channels/436251713830125568/1074411022514921543/1076004353485307924
function t() {
if [ -z "$*" ]; then
git commit -S -a
else
echo "Commit: $*"
git commit -S -am "$*"
fi
}
function t() {
if [ -z "$*" ]; then
git commit -S -a
else
echo "Commit: $*"
git commit -S -am "$*"
fi
}
Which lets me do: t this is my commit message. In general, why do you want to commit a directory from outside it. You ought to be in the directory making sure you're including anything new and making sure you don't put anything in by accident. Commits require at least a little involvement lest you accidentally reveal keys or leave something out. Also, you'll frequently be committing from a subdirectory of the root, so your check for the .git directory's gonna fail.