Simple Git & GitHub CLI Shell Scripts
1#!/bin/bash
2
3function glc {
4 if ! is_a_git_repo; then
5 echo "${BOLD} This won't work, you are not in a git repo !";
6 return 0
7 fi
8
9 has_commits=$(git log > /dev/null 2>&1 && echo "true" || echo "false")
10
11 if [ "$has_commits" = "false" ]; then
12 echo "${BOLD} Sorry, no commits yet inside this repo !";
13 return 0
14 fi
15
16 repo_name=$(basename "$(git rev-parse --show-toplevel)")
17 current_branch=$(git branch | awk '/\*/ {print $2}');
18 commits_num=$(git log --oneline | wc -l);
19 last_commit=$(git log --format="%H" -n 1);
20 last_commit_message=$(git show --format=%B -s "$last_commit" | head -n 1);
21 last_commit_author=$(git log --format='%an' -n 1)
22 current_user=$(git config user.name)
23 commits_done_today=$(git log --oneline --since="$(date +"%Y-%m-%d 00:00:00")" --author="$current_user" | wc -l)
24 commits_contrib_today=$(git log --oneline --since="$(date +"%Y-%m-%d 00:00:00")" --author="$last_commit_author" | wc -l)
25
26 [ $commits_num -le 1 ] && commit_text="commit" || commit_text="commits";
27 [ $commits_done_today -le 1 ] && commit_done_text="commit" || commit_done_text="commits";
28 [ $commits_contrib_today -le 1 ] && commit_contrib_text="commit" || commit_contrib_text="commits";
29 [ $commits_done_today -gt 0 ] &&
30 commit_done="${RESET_COLOR}Including ${LIGHT_BLUE}$commits_done_today $commit_done_text ${RESET_COLOR}by ${GREEN}$current_user ${RESET_COLOR}today" ||
31 commit_done="${RESET_COLOR}Including ${LIGHT_BLUE}$commits_contrib_today $commit_contrib_text ${RESET_COLOR}by ${GREEN}$last_commit_author ${RESET_COLOR}today"
32
33 if [ "$1" = "show" ]; then
34 git log --oneline --no-decorate;
35 return 0
36 fi
37
38 echo "${BOLD}${LIGHT_BLUE} $repo_name ${RESET_COLOR}has ${LIGHT_BLUE}$commits_num $commit_text ";
39 echo " $commit_done";
40 echo "${BOLD}${RESET_COLOR} Last Commit on ${GREEN}$current_branch ${RESET_COLOR}: $last_commit_message";
41 echo
42}
43
44# Resolve the full path to the script's directory
45REAL_PATH="$(dirname "$(readlink -f "$0")")"
46PARENT_DIR="$(dirname "$REAL_PATH")"
47CATEGORY="git.scripts"
48
49HELPS_DIR="$PARENT_DIR/helps/$CATEGORY"
50HELP_FILE="$(basename "$0" .sh)_help.sh"
51
52UTILS_DIR="$PARENT_DIR/utils"
53
54# Import necessary variables and functions
55source "$UTILS_DIR/check_git.sh"
56source "$UTILS_DIR/setup_git.sh"
57source "$UTILS_DIR/check_sudo.sh"
58source "$UTILS_DIR/colors.sh"
59source "$UTILS_DIR/usage.sh"
60
61# Import help file
62source "$HELPS_DIR/$HELP_FILE"
63
64# Usage function to display help
65function usage {
66 show_help "Usage" "${glc_arguments[@]}"
67 show_help "Description" "${glc_descriptions[@]}"
68 show_help "Options" "${glc_options[@]}"
69 show_extra "${glc_extras[@]}"
70 exit 0
71}
72
73# Check if --help is the first argument
74[ "$1" = "--help" ] && usage
75
76# prompt for sudo
77# password if required
78allow_sudo
79
80# Setting up git
81setup_git
82
83# Call glc function
84glc "$@"