From 1ce79689c1dab77a3fa20fa8d857e8d1017e97ec Mon Sep 17 00:00:00 2001 From: DestinedCodes Date: Sat, 20 May 2023 12:29:52 +0100 Subject: [PATCH] Upload Scripts --- commit | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++ commit~ | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 44 +++++++++++++ 3 files changed, 403 insertions(+) create mode 100755 commit create mode 100755 commit~ create mode 100755 install.sh diff --git a/commit b/commit new file mode 100755 index 0000000..701b110 --- /dev/null +++ b/commit @@ -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 ""; then + echo "Error: unable to set git user name" + exit 1 +fi + +if ! git config --global 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 diff --git a/commit~ b/commit~ new file mode 100755 index 0000000..d547e7a --- /dev/null +++ b/commit~ @@ -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 ""; then + echo "Error: unable to set git user name" + exit 1 +fi + +if ! git config --global 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 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..73d75c8 --- /dev/null +++ b/install.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +set -e + +# Copy commit.sh to the bin directory +if ! sudo cp commit /usr/local/bin/; then + echo "Error: Failed to copy commit script to /usr/local/bin/" + exit 1 +fi + +# Prompt for Git username +read -p "Enter Git username: " username + +# Validate that the username is not empty +if [[ -z "$username" ]]; then + echo "Error: Git username cannot be empty" + exit 1 +fi + +# Prompt for Git email +read -p "Enter Git email: " email + +# Validate that the email is not empty +if [[ -z "$email" ]]; then + echo "Error: Git email cannot be empty" + exit 1 +fi + +echo + +# Replace and in commit.sh with the provided values +if ! sudo sed -i "s//$username/g" /usr/local/bin/commit; then + echo "Error: Failed to replace username in commit script" + exit 1 +fi + +if ! sudo sed -i "s//$email/g" /usr/local/bin/commit; then + echo "Error: Failed to replace email in commit script" + exit 1 +fi + +sudo chmod 755 /usr/local/bin/commit +echo "Installation completed successfully" +