Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #3

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.vscode
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "test/bats"]
path = test/bats
url = https://github.com/bats-core/bats-core.git
[submodule "test/test_helper/bats-support"]
path = test/test_helper/bats-support
url = https://github.com/bats-core/bats-support.git
[submodule "test/test_helper/bats-assert"]
path = test/test_helper/bats-assert
url = https://github.com/bats-core/bats-assert.git
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- New setup script to handle linking the script into user's local bin directory.
- Tests to ensure functionality of setup script.

### Changed

- Updated LICENSE copyright year to 2024.
- Improved the handling of ANSI colors for output text.
- Updated and removed unnecessary comments.
- Updated and improved bash script formatting and style.
- Reduced if nesting where possible.

### Fixed

- Fixed slight problem with `readpath`.

## [v1.2.0] - 2022-08-05

### Added
Expand All @@ -32,8 +49,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Initial release.

[unreleased]: https://github.com/StrangeRanger/mass-git/compare/v1.2.0...HEAD

[unreleased]: https://github.com/StrangeRanger/mass-git/compare/v1.2.2...HEAD
[v1.2.2]: https://github.com/StrangeRanger/mass-git/releases/tag/v1.2.2
[v1.2.0]: https://github.com/StrangeRanger/mass-git/releases/tag/v1.2.0
[v1.1.0]: https://github.com/StrangeRanger/mass-git/releases/tag/v1.1.0
[v1.0.0]: https://github.com/StrangeRanger/mass-git/releases/tag/v1.0.0
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021-2022 Hunter T. (StrangeRanger)
Copyright (c) 2021-2024 Hunter T. (StrangeRanger)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
111 changes: 52 additions & 59 deletions mass-git
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,53 @@
# Fetch or pull one or more git repositories at a specified location on your system.
#
# License: MIT License
# Copyright (c) 2021-2022 Hunter T. (StrangeRanger)
# Copyright (c) 2021-2024 Hunter T. (StrangeRanger)
#
########################################################################################
#### [ Variables ]
####[ Variables ]#######################################################################


green="$(printf '\033[0;32m')"
blue="$(printf '\033[0;34m')"
cyan="$(printf '\033[0;36m')"
red="$(printf '\033[1;31m')"
nc="$(printf '\033[0m')"
## ANSI color codes.
C_GREEN="$(printf '\033[0;32m')"
C_BLUE="$(printf '\033[0;34m')"
C_CYAN="$(printf '\033[0;36m')"
C_RED="$(printf '\033[1;31m')"
C_NC="$(printf '\033[0m')"

version="v1.2.0" # Program version.
## Shorthanded variables for colorized output.
C_INVALID_INPUT="${C_RED}Invalid input:${C_NC} "
C_SUCCESS="${C_GREEN}==>${C_NC} "
C_ERROR="${C_RED}ERROR:${C_NC} "
C_INFO="${C_BLUE}==>${C_NC} "
C_NOTE="${C_CYAN}==>${C_NC} "

## Program information.
C_VERSION="v1.2.2"

## Set constants as readonly.
readonly C_GREEN C_BLUE C_CYAN C_RED C_NC
readonly C_INVALID_INPUT C_SUCCESS C_ERROR C_INFO C_NOTE
readonly C_VERSION

## Other variables.
maxdepth="-maxdepth 2" # Peform recursive searches with a maximum depth of 2.
provided_path=false # Validates that a path was provided.
git_repos=() # List of paths to existing repositories on the system.
provided_path=false # Validates that a path was provided.
git_repos=() # List of paths to existing repositories on the system.
git_action="pull"
git_action_prog="Pulling"


#### End of [ Variables ]
########################################################################################
#### [ Functions ]
####[ Functions ]#######################################################################


####
# Display the usage message for the program.
usage() {
echo "Fetch or pull one or more git repositories at a specified location on your system."
echo ""
echo "Usage: ./${0##*/} [-r] [-f] [-d] -p <path>"
echo " ./${0##*/} -h"
echo " ./${0##*/} -v"
echo "Usage: ${0##*/} [-r] [-f] [-d] -p <path>"
echo " ${0##*/} -h"
echo " ${0##*/} -v"
echo ""
echo "Options:"
echo " -h, --help : Displays this help message."
Expand All @@ -45,9 +61,7 @@ usage() {
}


#### End of [ Functions ]
########################################################################################
#### [ Options ]
####[ Options ]#########################################################################


# If no arguments are provided...
Expand All @@ -67,22 +81,10 @@ while [[ -n $1 ]]; do
shift

if hash realpath; then
path="$(realpath "$1")"
path="$(realpath "$1" 2>/dev/null || echo "BAD MAGIC")"
else
####
# EXPLANATION:
# 1. Gets the relative path as argument `"$1"`
# 2. Gets the dirname part of that path: `dirname "$1"`
# 3. `cd "$(dirname "$1")` into the relative dir and get the absolute
# path for it by running `pwd`
# 4. Append the basename to the absolute path: `$(basename "$1")`
#
# This solution was found at the following link:
# https://stackoverflow.com/questions/4175264/how-to-retrieve-absolute-path-given-relative/31605674#31605674
#
# '|| exit' makes sure that if the script is unable to change
# directories, the script is exitted.
####
path="$(cd "${1%/*}" || exit; pwd)/${1##*/}"
fi
provided_path=true
Expand All @@ -98,11 +100,11 @@ while [[ -n $1 ]]; do
dry_run="--dry-run"
;;
"-v"|"--version")
echo "Mass Git $version"
echo "Mass Git $C_VERSION"
exit 0
;;
*)
echo "${red}Invalid option:${nc} $1" >&2
echo "${C_RED}Invalid option:${C_NC} $1" >&2
exit 1
;;
esac
Expand All @@ -111,62 +113,53 @@ done

## Check if the provided path is a valid directory.
if "$provided_path"; then
if [[ ! -d $path ]]; then
if [[ -f $path ]]; then
echo "${red}Invalid input:${nc} File was provided when a directory was" \
"expected" >&2
else
echo "${red}Invalid input:${nc} Directory does not exist" >&2
fi
if [[ -f $path ]]; then
echo "${C_INVALID_INPUT}File was provided when a directory was expected" >&2
exit 1
elif [[ ! -d $path ]]; then
echo "${C_INVALID_INPUT}Directory does not exist" >&2
exit 1
fi
else
echo "${red}Invalid input:${nc} Missing required argument '-p <path>'"
echo "${C_INVALID_INPUT}Missing required argument '-p <path>'"
exit 1
fi


#### End of [ Options ]
########################################################################################
#### [ Main ]
####[ Main ]############################################################################


## Store the location of all local repositories found.
## NOTE: $maxdepth is purposefully unquoted. Do not quote it!
# shellcheck disable=SC2086
while read -r -d $'\0'; do
git_repos+=("${REPLY/%.git/}")
done < <(find "$path" $maxdepth -type d -name ".git" -prune -print0)

# If no local repositories were found...
# NOTE: Leave $git_repos as is. It's fine that only the first index of the array is
# provided, as all we are trying to do is confirm whether or not a git initialized
# directory was located.
# provided, as all I'm are trying to do is confirm whether or not a git
# initialized directory was located.
# shellcheck disable=SC2128
if [[ -z $git_repos ]]; then
echo "${red}ERROR:${nc} No git initialized directory could be found"
echo "${C_ERROR}No git initialized directory could be found"
exit 1
fi

[[ $dry_run = "--dry-run" ]] && echo "${cyan}==>${nc} Performing a dry-run..."
[[ $dry_run = "--dry-run" ]] && echo "${C_NOTE}Performing a dry-run..."

for repo_path in "${git_repos[@]}"; do
echo "${blue}==>${nc} Changing directories to '$repo_path'..."
echo "${C_INFO}Changing directories to '$repo_path'..."
cd "$repo_path" || {
echo "${red}ERROR:${nc} Failed to change directories"
echo "${C_ERROR}Failed to change directories"
exit 1
}
repo_name="$(git config --get remote.origin.url)"

echo "${blue}==>${nc} $git_action_prog changes from '$repo_name'..."
## NOTE: $dry_run is purposefully unquoted. Do not quote it!
echo "${C_INFO}$git_action_prog changes from '$repo_name'..."
# shellcheck disable=SC2086
git "$git_action" $dry_run \
|| echo "${red}ERROR:${nc} Failed to $git_action changes from '$repo_name'"
|| echo "${C_ERROR}Failed to $git_action changes from '$repo_name'"
done

echo "${green}==>${nc} Done"
echo "${C_SUCCESS}Done"


#### End of [ Main ]
########################################################################################
21 changes: 21 additions & 0 deletions setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
#
#
########################################################################################

use_hard_links=false
pattern="^export PATH=.*(/.local/bin).*"

if [[ ! -d "$HOME/.local/bin/" ]]; then
echo "Creating '~/.local/bin/' directory..."
mkdir -p "$HOME/.local/bin/"
fi

if [[ $use_hard_links == false ]]; then
echo "Creating symlink to 'mass-git' in '~/.local/bin/'..."
ln -s "${0%/*}/mass-git" "$HOME/.local/bin/mass-git"
else
echo "Creating hard link to 'mass-git' in '~/.local/bin/'..."
ln "${0%/*}/mass-git" "$HOME/.local/bin/mass-git"
fi
1 change: 1 addition & 0 deletions test/bats
Submodule bats added at 4417a9
36 changes: 36 additions & 0 deletions test/setup-test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bats

setup() {
test_dir=$(mktemp -d)
pattern="^export PATH=.*(/.local/bin).*"
original_home="$HOME"
original_path="$PATH"

### Determine the shell rc file to be used.
case "$SHELL" in
*/zsh) shell_rc=".zshrc" ;;
*/bash) shell_rc=".bashrc" ;;
esac

cd "$test_dir"
mkdir mass-git
cp "$BATS_TEST_DIRNAME/../setup.bash" "$test_dir"
cp "$BATS_TEST_DIRNAME/../mass-git" "$test_dir/mass-git"
echo 'export PATH="/usr/local/opt/curl/bin:/usr/local/sbin"' >> "$test_dir/$shell_rc"

export HOME="$test_dir"
./setup.bash
}

teardown() {
export HOME="$original_home"

# For debugging purposes.
#ls -la "$test_dir"

rm -r "$test_dir"
}

@test "mass-git symlinked in ~/.local/bin/" {
[ -L "$HOME/.local/bin/mass-git" ]
}
1 change: 1 addition & 0 deletions test/test_helper/bats-assert
Submodule bats-assert added at 44913f
1 change: 1 addition & 0 deletions test/test_helper/bats-support
Submodule bats-support added at 3c8fad