2019-05-01_Linux_Format

(singke) #1

9990May 21s120yhowu2hwtrieB May 2019 LXF249 91


Keep track of Git coding academy


transport directory need to be committed. command-
history and findit also need to have multiple files
committed. Using the shell script saves time: after
running the script, you can simply go to the directories
that need attention. The top half of the script
establishes what you’re going to look for in all of the git
status. outputs, and what you are going to display to
the user as a result. Running git status. in the base
directory will give you the same information, but it can
be a bit confusing to look at. Also, you may have a lot of
small projects, where the Git repository is the same as
the project directory.
In this scenario, you’d have to go to each of your Git
repository directories to issue the git status.
command. The git-status, command-history and
findit projects fall into this category.
Let’s look at the shell script in detail. Line 1 tells
Linux what kind of file this is (a Bash shell script) and
how it should be executed (in Bash); it’s standard
Linux/UNIX boilerplate.
#!/bin/bash
Then there are some escape sequence definitions so
that you will be able to output text in colour. Colour
makes things easier to comprehend.
escRed=’\e[1;31m’ # define a few colour escape
sequences
escYellow=’\e[0;33m’
escGreen=’\e[0;32m’
escReset=’\e[0m’
Following the colour escape sequence definitions are
the two commands that you will execute in each of your
source repository directories.


define cmd1 and cmd2


cmd1=”git status. | grep -c \”modified:\””
cmd2=”git status. | grep -c \”Untracked files:\””
You can execute each of the these commands
from the command line to see what the git status.
command is telling you before and after you pipe it
through grep. Notice that we’re using grep’s -c (count)
option. This will suppress the output of the text we’re
searching for and simply tell us how many times the
string appears in the output of the git status.
command. You’re looking for the strings modified: and
Untracked files:. The first string occurs once for each
file that is different from the version in the Git
repository. The second string occurs one time if git
status. finds any untracked files; it will not appear at all
if it finds no untracked files.
The next few lines indicate what you’re going to
output to the user depending on the values of count1
and count2.


define the output strings


strNoCommit=”has no files that need to be
committed.”
strCommit=”file(s) that need to be committed.”
strNoUntracked=”has no untracked files.”
strUntracked=”has one or more untracked files.”
The next lines contain a simple array of project
names and repository directories. Notice the vertical bar
character ‘|’ used to separate the project names from
their associated directories. Obviously, until you
populate the proj array with your own project names
and repository directories, you’ll get nothing but errors.
git status. will scream if you issue it in a directory that
is not associated with a Git repository.


# declare an array of the project names and their...
associated directories
declare -a proj
proj[0]=”utility|$HOME/Development/UDS/utility/src”
proj[1]=”transport|$HOME/Development/UDS/
transport/src”
proj[2]=”serverTest|$HOME/Development/UDS/
serverTest/src”
proj[3]=”clientTest|$HOME/Development/UDS/
clientTest/src”
proj[4]=”simpleClientTest|$HOME/Development/UDS/
simpleClientTest/src”
proj[5]=”throttledServerTest|$HOME/Development/
UDS/throttledServerTest/src”
proj[6]=”utilityTest|$HOME/Development/UDS/
utilityTest/src”
proj[7]=”command-history|$HOME/Development/
command-history”
proj[8]=”git-status|$HOME/Development/git-status”
proj[9]=”findit|$HOME/Development/findit”
numProjects=${#proj[@]} # keep track of the
number of projects in the array
The next lines timestamp the output and print a
blank line.
echo $(date) # timestamp the output
echo
In the bottom half of the script is a for loop. First,
inside the for loop, you recover your project and
source directory from the current element of the array
using Bash pattern matching.
project=”${proj[$i]%%|*}” # derive the project name
from proj[i] (return everything before ‘|’)
dir=”${proj[$i]#*|}” # derive the directory name from
proj[i] (return everything after ‘|’)
You iterate through the array to recover each of your
project names and cd to its associated source
directory. In each source directory you execute cmd1
and cmd2 and save the results in count1 and count2.
Based on the values of count1 and count2 you echo
two status statements to the user. The first statement
indicates the number of modified files that need to be
committed and the second statement indicates whether
or not gitStatus found any untracked files.
Once you’ve created your shell script in a text editor
and saved it, you must tell Linux that it is an executable
file. This is done so frequently that many developers
and system administrators keep an alias for the
purpose: alias mx=’chmod +x’. Add the alias to the file
$HOME/.alias or $HOME/.bash_aliases, depending
on your distro. You then copy the executable into your
/bin directory with a short name that you can
remember. Issue the following commands in your
working directory.
mx gitStatus.sh
cp gitStatus.sh $HOME/bin/gitStatus
Any time you want to know the status of your
projects, you type gitStatus on the command line. If
you like, you can create a gitStatus shell script for
every Git repository on your system, or you can include
multiple repositories and their associated projects in
the same gitStatus command.
Look at how much we can accomplish in a one-page
shell script. Hopefully this proves to be useful and that it
will give you some ideas for your own shell scripts. Have
fun and keep coding!
Free download pdf