-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd39c8d
commit 1ce7968
Showing
3 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
#!/bin/bash | ||
|
||
# Check if the --help flag is used | ||
if [[ "$*" == *"--help"* ]]; then | ||
echo "Usage: commit [options] [files]" | ||
echo | ||
echo "Options:" | ||
echo " --help Display this help message" | ||
echo " --verbose Display verbose output" | ||
echo | ||
echo "Description:" | ||
echo " This script helps you stage, commit, and push changes to a Git repository." | ||
echo " It prompts for a commit message and performs the necessary Git commands." | ||
echo | ||
echo "Examples:" | ||
echo " commit => Commit all changes (assumes git add .)" | ||
echo " commit file1.txt file2.txt => Stage and commit specific files" | ||
echo " commit --verbose file.txt => Commit changes with verbose output" | ||
echo | ||
exit 0 | ||
fi | ||
|
||
# Check if the current directory is a Git repository | ||
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then | ||
read -p "The current directory is not a Git repository. Would you like to initialize it? (y/n): " initialize | ||
if [ "$initialize" == "y" ]; then | ||
git init | ||
else | ||
echo "Script terminated. Please initialize a Git repository to continue." | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Check if there are any changes in the remote repository | ||
remote_changes=$(git fetch && git rev-list HEAD..origin/master --count) | ||
if [ $remote_changes -gt 0 ]; then | ||
read -p "There are $remote_changes remote change(s). Do you want to pull the changes? (y/n): " pull_changes | ||
if [ "$pull_changes" == "y" ]; then | ||
git pull origin master | ||
fi | ||
fi | ||
|
||
# Prompt for user input | ||
read -p "Enter commit message: " message | ||
echo | ||
|
||
# Validate that the commit message is not empty | ||
if [[ -z "$message" ]]; then | ||
echo "Error: commit message cannot be empty" | ||
exit 1 | ||
fi | ||
|
||
# Validate the length of the commit message | ||
if [[ ${#message} -gt 50 ]]; then | ||
echo "Error: commit message exceeds the maximum length of 50 characters" | ||
exit 1 | ||
fi | ||
|
||
# Set git config for the current user | ||
if ! git config --global user.name "<user.name>"; then | ||
echo "Error: unable to set git user name" | ||
exit 1 | ||
fi | ||
|
||
if ! git config --global user.email "<user.email>"; then | ||
echo "Error: unable to set git user email" | ||
exit 1 | ||
fi | ||
|
||
# Check if the --verbose flag is used | ||
verbose_flag="" | ||
for arg in "$@"; do | ||
if [ "$arg" == "--verbose" ]; then | ||
verbose_flag="--verbose" | ||
break | ||
fi | ||
done | ||
|
||
# Remove the --verbose flag from the arguments | ||
new_args=() | ||
for arg in "$@"; do | ||
if [ "$arg" != "--verbose" ]; then | ||
new_args+=("$arg") | ||
fi | ||
done | ||
|
||
set -- "${new_args[@]}" | ||
|
||
# Check if any files are specified | ||
if [ $# -eq 0 ]; then | ||
# No files specified, assume all changes should be staged and committed | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display staging output when verbose flag is used | ||
echo "--- Staging Output ---" | ||
if ! git add .; then | ||
echo "--- Changes staged unsuccessfully ---" | ||
exit 1 | ||
fi | ||
echo "--- Changes staged successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git add . > /dev/null 2>&1; then | ||
echo "Error: unable to add changes to the staging area" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Check if any changes were added to the staging area | ||
staged_files=$(git diff --cached --name-only) | ||
if [[ -z "$staged_files" ]]; then | ||
echo "No recent changes have been made to any files" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
else | ||
echo "Changes staged successfully:" | ||
echo "$staged_files" | ||
fi | ||
else | ||
|
||
# Stage specified files | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display staging output when verbose flag is used | ||
echo "--- Staging Output ---" | ||
if ! git add "$@"; then | ||
echo "--- Changes staged unsuccessfully ---" | ||
exit 1 | ||
fi | ||
echo "--- Changes staged successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git add "$@" > /dev/null 2>&1; then | ||
echo "Error: unable to add changes to the staging area" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Check if any changes were added to the staging area | ||
staged_files=$(git diff --cached --name-only -- "$@") | ||
if [[ -z "$staged_files" ]]; then | ||
echo "No recent changes have been made to the specified file(s)" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
else | ||
echo "Changes staged successfully:" | ||
echo "$staged_files" | ||
fi | ||
fi | ||
echo | ||
|
||
# Commit the changes with the commit message | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display commit output when verbose flag is used | ||
echo "--- Commit Output ---" | ||
git commit "$verbose_flag" -m "$message" | ||
echo "--- Changes committed successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git commit -m "$message" > /dev/null 2>&1; then | ||
echo "Error: unable to commit changes" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
echo "Changes committed successfully" | ||
fi | ||
echo | ||
|
||
# Push the changes to the remote repository | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display push output when verbose flag is used | ||
echo "--- Push Output ---" | ||
git push | ||
echo "--- Changes pushed successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git push > /dev/null 2>&1; then | ||
echo "Error: unable to push changes to the remote repository" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
echo "Changes pushed successfully" | ||
fi | ||
echo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
#!/bin/bash | ||
|
||
# Check if the --help flag is used | ||
if [[ "$*" == *"--help"* ]]; then | ||
echo "Usage: commit [options] [files]" | ||
echo | ||
echo "Options:" | ||
echo " --help Display this help message" | ||
echo " --verbose Display verbose output" | ||
echo | ||
echo "Description:" | ||
echo " This script helps you stage, commit, and push changes to a Git repository." | ||
echo " It prompts for a commit message and performs the necessary Git commands." | ||
echo | ||
echo "Examples:" | ||
echo " commit => Commit all changes (assumes git add .)" | ||
echo " commit file1.txt file2.txt => Stage and commit specific files" | ||
echo " commit --verbose file.txt => Commit changes with verbose output" | ||
echo | ||
exit 0 | ||
fi | ||
|
||
# Check if the current directory is a Git repository | ||
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then | ||
read -p "The current directory is not a Git repository. Would you like to initialize it? (y/n): " initialize | ||
if [ "$initialize" == "y" ]; then | ||
git init | ||
else | ||
echo "Script terminated. Please initialize a Git repository to continue." | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Prompt for user input | ||
read -p "Enter commit message: " message | ||
echo | ||
|
||
# Validate that the commit message is not empty | ||
if [[ -z "$message" ]]; then | ||
echo "Error: commit message cannot be empty" | ||
exit 1 | ||
fi | ||
|
||
# Validate the length of the commit message | ||
if [[ ${#message} -gt 50 ]]; then | ||
echo "Error: commit message exceeds the maximum length of 50 characters" | ||
exit 1 | ||
fi | ||
|
||
# Set git config for the current user | ||
if ! git config --global user.name "<user.name>"; then | ||
echo "Error: unable to set git user name" | ||
exit 1 | ||
fi | ||
|
||
if ! git config --global user.email "<user.email>"; then | ||
echo "Error: unable to set git user email" | ||
exit 1 | ||
fi | ||
|
||
# Check if the --verbose flag is used | ||
verbose_flag="" | ||
for arg in "$@"; do | ||
if [ "$arg" == "--verbose" ]; then | ||
verbose_flag="--verbose" | ||
break | ||
fi | ||
done | ||
|
||
# Remove the --verbose flag from the arguments | ||
new_args=() | ||
for arg in "$@"; do | ||
if [ "$arg" != "--verbose" ]; then | ||
new_args+=("$arg") | ||
fi | ||
done | ||
|
||
set -- "${new_args[@]}" | ||
|
||
# Check if any files are specified | ||
if [ $# -eq 0 ]; then | ||
# No files specified, assume all changes should be staged and committed | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display staging output when verbose flag is used | ||
echo "--- Staging Output ---" | ||
if ! git add .; then | ||
echo "--- Changes staged unsuccessfully ---" | ||
exit 1 | ||
fi | ||
echo "--- Changes staged successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git add . > /dev/null 2>&1; then | ||
echo "Error: unable to add changes to the staging area" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Check if any changes were added to the staging area | ||
staged_files=$(git diff --cached --name-only) | ||
if [[ -z "$staged_files" ]]; then | ||
echo "No recent changes have been made to any files" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
else | ||
echo "Changes staged successfully:" | ||
echo "$staged_files" | ||
fi | ||
else | ||
|
||
# Stage specified files | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display staging output when verbose flag is used | ||
echo "--- Staging Output ---" | ||
if ! git add "$@"; then | ||
echo "--- Changes staged unsuccessfully ---" | ||
exit 1 | ||
fi | ||
echo "--- Changes staged successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git add "$@" > /dev/null 2>&1; then | ||
echo "Error: unable to add changes to the staging area" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Check if any changes were added to the staging area | ||
staged_files=$(git diff --cached --name-only -- "$@") | ||
if [[ -z "$staged_files" ]]; then | ||
echo "No recent changes have been made to the specified file(s)" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
else | ||
echo "Changes staged successfully:" | ||
echo "$staged_files" | ||
fi | ||
fi | ||
echo | ||
|
||
# Commit the changes with the commit message | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display commit output when verbose flag is used | ||
echo "--- Commit Output ---" | ||
git commit "$verbose_flag" -m "$message" | ||
echo "--- Changes committed successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git commit -m "$message" > /dev/null 2>&1; then | ||
echo "Error: unable to commit changes" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
echo "Changes committed successfully" | ||
fi | ||
echo | ||
|
||
# Push the changes to the remote repository | ||
if [[ -n "$verbose_flag" ]]; then | ||
# Display push output when verbose flag is used | ||
echo "--- Push Output ---" | ||
git push | ||
echo "--- Changes pushed successfully ---" | ||
else | ||
# Suppress output when verbose flag is not used | ||
if ! git push > /dev/null 2>&1; then | ||
echo "Error: unable to push changes to the remote repository" | ||
echo "(Consider using the --verbose flag for more information)" | ||
exit 1 | ||
fi | ||
echo "Changes pushed successfully" | ||
fi | ||
echo |
Oops, something went wrong.