6 helpful Bash scripts for Git

I wrote a bunch of Bash scripts that make my life simpler after I’m working with Git repositories. A lot of my colleagues say there is not any want; that all the things I must do may be completed with Git instructions. Whereas which may be true, I discover the scripts infinitely extra handy than attempting to determine the suitable Git command to do what I would like.

1. gitlog

gitlog prints an abbreviated listing of present patches in opposition to the grasp model. It prints them from oldest to latest and exhibits the writer and outline, with H for HEAD, ^ for HEAD^, 2 for HEAD~2, and so forth. For instance:

$ gitlog
———————–[ recovery25 ]———————–
(snip)
11 340d27a33895 Bob Peterson gfs2: drain the ail2 listing after io errors
10 9b3c4e6efb10 Bob Peterson gfs2: clear up iopen glock mess in gfs2_create_inode
9 d2e8c22be39b Bob Peterson gfs2: Do correct error checking for go_sync household of glops
8 9563e31f8bfd Christoph Hellwig gfs2: use page_offset in gfs2_page_mkwrite
7 ebac7a38036c Christoph Hellwig gfs2: do not use buffer_heads in gfs2_allocate_page_backing
6 f703a3c27874 Andreas Gruenbacher gfs2: Enhance mmap write vs. punch_hole consistency
5 a3e86d2ef30e Andreas Gruenbacher gfs2: Multi-block allocations in gfs2_page_mkwrite
Four da3c604755b0 Andreas Gruenbacher gfs2: Repair end-of-file dealing with in gfs2_page_mkwrite
3 4525c2f5b46f Bob Peterson Rafael Aquini’s slab instrumentation
2 a06a5b7dea02 Bob Peterson GFS2: Add go_get_holdtime to gl_ops
^ 8ba93c796d5c Bob Peterson gfs2: introduce new perform remaining_hold_time and use it in dq
H e8b5ff851bb9 Bob Peterson gfs2: Permit rgrps to have a minimal maintain time

If I need to see what patches are on a unique department, I can specify an alternate department:

$ gitlog restoration24

2. gitlog.id

gitlog.id simply prints the patch SHA1 IDs:

$ gitlog.id
———————–[ recovery25 ]———————–
56908eeb6940 2ca4a6b628a1 fc64ad5d99fe 02031a00a251 f6f38da7dd18 d8546e8f0023 fc3cc1f98f6b 12c3e0cb3523 76cce178b134 6fc1dce3ab9c 1b681ab074ca 26fed8de719b 802ff51a5670 49f67a512d8c f04f20193bbb 5f6afe809d23 2030521dc70e dada79b3be94 9b19a1e08161 78a035041d3e f03da011cae2 0d2b2e068fcd 2449976aa133 57dfb5e12ccd 53abedfdcf72 6fbdda3474b3 49544a547188 187032f7a63c 6f75dae23d93 95fc2a261b00 ebfb14ded191 f653ee9e414a 0e2911cb8111 73968b76e2e3 8a3e4cb5e92c a5f2da803b5b 7c9ef68388ed 71ca19d0cba8 340d27a33895 9b3c4e6efb10 d2e8c22be39b 9563e31f8bfd ebac7a38036c f703a3c27874 a3e86d2ef30e da3c604755b0 4525c2f5b46f a06a5b7dea02 8ba93c796d5c e8b5ff851bb9

Once more, it assumes the present department, however I can specify a unique department if I would like.

3. gitlog.id2

gitlog.id2 is similar as gitlog.id however with out the department line on the prime. That is helpful for cherry-picking all patches from one department to the present department:

$ # create a brand new department
$ git department –track origin/grasp
$ # take a look at the brand new department I simply created
$ git checkout restoration26
$ # cherry-pick all patches from the previous department to the brand new one
$ for i in `gitlog.id2 restoration25` ; do git cherry-pick $i ;completed

4. gitlog.grep

gitlog.grep greps for a string inside that assortment of patches. For instance, if I discover a bug and need to repair the patch that has a reference to perform inode_go_sync, I merely do:

$ gitlog.grep inode_go_sync
———————–[ recovery25 – 50 patches ]———————–
(snip)
11 340d27a33895 Bob Peterson gfs2: drain the ail2 listing after io errors
10 9b3c4e6efb10 Bob Peterson gfs2: clear up iopen glock mess in gfs2_create_inode
9 d2e8c22be39b Bob Peterson gfs2: Do correct error checking for go_sync household of glops
152:-static void inode_go_sync(struct gfs2_glock *gl)
153:+static int inode_go_sync(struct gfs2_glock *gl)
163:@@ -296,6 +302,7 @@ static void inode_go_sync(struct gfs2_glock *gl)
8 9563e31f8bfd Christoph Hellwig gfs2: use page_offset in gfs2_page_mkwrite
7 ebac7a38036c Christoph Hellwig gfs2: do not use buffer_heads in gfs2_allocate_page_backing
6 f703a3c27874 Andreas Gruenbacher gfs2: Enhance mmap write vs. punch_hole consistency
5 a3e86d2ef30e Andreas Gruenbacher gfs2: Multi-block allocations in gfs2_page_mkwrite
Four da3c604755b0 Andreas Gruenbacher gfs2: Repair end-of-file dealing with in gfs2_page_mkwrite
3 4525c2f5b46f Bob Peterson Rafael Aquini’s slab instrumentation
2 a06a5b7dea02 Bob Peterson GFS2: Add go_get_holdtime to gl_ops
^ 8ba93c796d5c Bob Peterson gfs2: introduce new perform remaining_hold_time and use it in dq
H e8b5ff851bb9 Bob Peterson gfs2: Permit rgrps to have a minimal maintain time

So, now I do know that patch HEAD~9 is the one which wants fixing. I exploit git rebase -i HEAD~10 to edit patch 9, git commit -a –amend, then git rebase –continue to make the required changes.

5. gitbranchcmp3

gitbranchcmp3 lets me examine my present department to a different department, so I can examine older variations of patches to my newer variations and shortly see what’s modified and what hasn’t. It generates a examine script (that makes use of the KDE instrument Kompare, which works on GNOME3, as nicely) to check the patches that are not fairly the identical. If there aren’t any variations aside from line numbers, it prints [SAME]. If there are solely remark variations, it prints [same] (in decrease case). For instance:

$ gitbranchcmp3 restoration24
Department restoration24 has 47 patches
Department restoration25 has 50 patches

(snip)
38 87eb6901607a 340d27a33895 [same] gfs2: drain the ail2 listing after io errors
39 90fefb577a26 9b3c4e6efb10 [same] gfs2: clear up iopen glock mess in gfs2_create_inode
40 ba3ae06b8b0e d2e8c22be39b [same] gfs2: Do correct error checking for go_sync household of glops
41 2ab662294329 9563e31f8bfd [SAME] gfs2: use page_offset in gfs2_page_mkwrite
42 0adc6d817b7a ebac7a38036c [SAME] gfs2: do not use buffer_heads in gfs2_allocate_page_backing
43 55ef1f8d0be8 f703a3c27874 [SAME] gfs2: Enhance mmap write vs. punch_hole consistency
44 de57c2f72570 a3e86d2ef30e [SAME] gfs2: Multi-block allocations in gfs2_page_mkwrite
45 7c5305fbd68a da3c604755b0 [SAME] gfs2: Repair end-of-file dealing with in gfs2_page_mkwrite
46 162524005151 4525c2f5b46f [SAME] Rafael Aquini’s slab instrumentation
47 a06a5b7dea02 [ ] GFS2: Add go_get_holdtime to gl_ops
48 8ba93c796d5c [ ] gfs2: introduce new perform remaining_hold_time and use it in dq
49 e8b5ff851bb9 [ ] gfs2: Permit rgrps to have a minimal maintain time

Lacking from restoration25:
The lacking:
Examine script generated at: /tmp/compare_mismatches.sh

6. gitlog.discover

Lastly, I’ve gitlog.discover, a script to assist me determine the place the upstream variations of my patches are and every patch’s present standing. It does this by matching the patch description. It additionally generates a examine script (once more, utilizing Kompare) to check the present patch to the upstream counterpart:

$ gitlog.discover
———————–[ recovery25 – 50 patches ]———————–
(snip)
11 340d27a33895 Bob Peterson gfs2: drain the ail2 listing after io errors
lo 5bcb9be74b2a Bob Peterson gfs2: drain the ail2 listing after io errors
10 9b3c4e6efb10 Bob Peterson gfs2: clear up iopen glock mess in gfs2_create_inode
fn 2c47c1be51fb Bob Peterson gfs2: clear up iopen glock mess in gfs2_create_inode
9 d2e8c22be39b Bob Peterson gfs2: Do correct error checking for go_sync household of glops
lo feb7ea639472 Bob Peterson gfs2: Do correct error checking for go_sync household of glops
8 9563e31f8bfd Christoph Hellwig gfs2: use page_offset in gfs2_page_mkwrite
ms f3915f83e84c Christoph Hellwig gfs2: use page_offset in gfs2_page_mkwrite
7 ebac7a38036c Christoph Hellwig gfs2: do not use buffer_heads in gfs2_allocate_page_backing
ms 35af80aef99b Christoph Hellwig gfs2: do not use buffer_heads in gfs2_allocate_page_backing
6 f703a3c27874 Andreas Gruenbacher gfs2: Enhance mmap write vs. punch_hole consistency
fn 39c3a948ecf6 Andreas Gruenbacher gfs2: Enhance mmap write vs. punch_hole consistency
5 a3e86d2ef30e Andreas Gruenbacher gfs2: Multi-block allocations in gfs2_page_mkwrite
fn f53056c43063 Andreas Gruenbacher gfs2: Multi-block allocations in gfs2_page_mkwrite
Four da3c604755b0 Andreas Gruenbacher gfs2: Repair end-of-file dealing with in gfs2_page_mkwrite
fn 184b4e60853d Andreas Gruenbacher gfs2: Repair end-of-file dealing with in gfs2_page_mkwrite
3 4525c2f5b46f Bob Peterson Rafael Aquini’s slab instrumentation
Not discovered upstream
2 a06a5b7dea02 Bob Peterson GFS2: Add go_get_holdtime to gl_ops
Not discovered upstream
^ 8ba93c796d5c Bob Peterson gfs2: introduce new perform remaining_hold_time and use it in dq
Not discovered upstream
H e8b5ff851bb9 Bob Peterson gfs2: Permit rgrps to have a minimal maintain time
Not discovered upstream
Examine script generated: /tmp/compare_upstream.sh

The patches are proven on two traces, the primary of which is your present patch, adopted by the corresponding upstream patch, and a 2-character abbreviation to point its upstream standing:

  • lo means the patch is within the native upstream Git repo solely (i.e., not pushed upstream but).
  • ms means the patch is in Linus Torvald’s grasp department.
  • fn means the patch is pushed to my “for-next” improvement department, supposed for the subsequent upstream merge window.

A few of my scripts make assumptions based mostly on how I usually work with Git. For instance, when trying to find upstream patches, it makes use of my well-known Git tree’s location. So, you have to to regulate or enhance them to fit your circumstances. The gitlog.discover script is designed to find GFS2 and DLM patches solely, so until you are a GFS2 developer, it would be best to customise it to the parts that curiosity you.

Supply code

Right here is the supply for these scripts.

1. gitlog

#!/bin/bash
department=$1

if check “x$department” = x; then
department=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
fi

patches=0
monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

LIST=`git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘`
for i in $LIST; do patches=$(echo $patches + 1 | bc);completed

if [[ $branch =~ .*for-next.* ]]
then
begin=HEAD
# begin=origin/for-next
else
begin=origin/grasp
fi

monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

/usr/bin/echo “———————–[” $branch “]———————–“
patches=$(echo $patches – 1 | bc);
for i in $LIST; do
if [ $patches -eq 1 ]; then
cnt=” ^”
elif [ $patches -eq 0 ]; then
cnt=” H”
else
if [ $patches -lt 10 ]; then
cnt=” $patches”
else
cnt=”$patches”
fi
fi
/usr/bin/git present –abbrev-commit -s –pretty=format:”$cnt %h %<|(32)%an %s %n” $i
patches=$(echo $patches – 1 | bc)
completed
#git log –reverse –abbrev-commit –pretty=format:”%h %<|(32)%an %s” $monitoring..$department
#git log –reverse –abbrev-commit –pretty=format:”%h %<|(32)%an %s” ^origin/grasp ^linux-gfs2/for-next $department

2. gitlog.id

#!/bin/bash
department=$1

if check “x$department” = x; then
department=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
fi

monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

/usr/bin/echo “———————–[” $branch “]———————–“
git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘

3. gitlog.id2

#!/bin/bash
department=$1

if check “x$department” = x; then
department=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
fi

monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`
git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘

4. gitlog.grep

#!/bin/bash
param1=$1
param2=$2

if check “x$param2” = x; then
department=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
string=$param1
else
department=$param1
string=$param2
fi

patches=0
monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

LIST=`git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘`
for i in $LIST; do patches=$(echo $patches + 1 | bc);completed
/usr/bin/echo “———————–[” $branch “-” $patches “patches ]———————–“
patches=$(echo $patches – 1 | bc);
for i in $LIST; do
if [ $patches -eq 1 ]; then
cnt=” ^”
elif [ $patches -eq 0 ]; then
cnt=” H”
else
if [ $patches -lt 10 ]; then
cnt=” $patches”
else
cnt=”$patches”
fi
fi
/usr/bin/git present –abbrev-commit -s –pretty=format:”$cnt %h %<|(32)%an %s” $i
/usr/bin/git present –pretty=electronic mail –patch-with-stat $i | grep -n “$string”
patches=$(echo $patches – 1 | bc)
completed

5. gitbranchcmp3

#!/bin/bash
#
# gitbranchcmp3 <previous department> [<new_branch>]
#
oldbranch=$1
newbranch=$2
script=/tmp/compare_mismatches.sh

/usr/bin/rm -f $script
echo “#!/bin/bash” > $script
/usr/bin/chmod 755 $script
echo “# Generated by gitbranchcmp3.sh” >> $script
echo “# Run this script to check the mismatched patches” >> $script
echo ” ” >> $script
echo “perform compare_them()” >> $script
echo “{” >> $script
echo ” git present –pretty=electronic mail –patch-with-stat $1 > /tmp/gronk1″ >> $script
echo ” git present –pretty=electronic mail –patch-with-stat $2 > /tmp/gronk2″ >> $script
echo ” kompare /tmp/gronk1 /tmp/gronk2″ >> $script
echo “}” >> $script
echo ” ” >> $script

if check “x$newbranch” = x; then
newbranch=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
fi

monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

declare -a oldsha1s=(`git log –reverse –abbrev-commit –pretty=oneline $monitoring..$oldbranch | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘`)
declare -a newsha1s=(`git log –reverse –abbrev-commit –pretty=oneline $monitoring..$newbranch | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘`)

#echo “previous: ” $oldsha1s
oldcount=${#oldsha1s[@]}
echo “Department $oldbranch has $oldcount patches”
oldcount=$(echo $oldcount – 1 | bc)
#for o in `seq 0 ${#oldsha1s[@]}`; do
# echo -n $ ” “
# desc=`git present $i | head -5 | tail -1|reduce -b5-`
#completed

#echo “new: ” $newsha1s
newcount=${#newsha1s[@]}
echo “Department $newbranch has $newcount patches”
newcount=$(echo $newcount – 1 | bc)
#for o in `seq 0 ${#newsha1s[@]}`; do
# echo -n $ ” “
# desc=`git present $i | head -5 | tail -1|reduce -b5-`
#completed
echo

for brand new in `seq 0 $newcount`; do
newsha=$
newdesc=`git present $newsha | head -5 | tail -1|reduce -b5-`
oldsha=” “
similar=”[ ]”
for previous in `seq 0 $oldcount`; do
if check “$” = “match”; then
proceed;
fi
olddesc=`git present $ | head -5 | tail -1|reduce -b5-`
if check “$olddesc” = “$newdesc” ; then
oldsha=$
#echo $oldsha
git present $oldsha |tail -n +2 |grep -v “index.*..” |grep -v “@@” > /tmp/gronk1
git present $newsha |tail -n +2 |grep -v “index.*..” |grep -v “@@” > /tmp/gronk2
diff /tmp/gronk1 /tmp/gronk2 &> /dev/null
if [ $? -eq 0 ] ;then
# No variations
similar=”[SAME]”
oldsha1s[$old]=”match”
break
fi
git present $oldsha |sed -n ‘/diff/,$p’ |grep -v “index.*..” |grep -v “@@” > /tmp/gronk1
git present $newsha |sed -n ‘/diff/,$p’ |grep -v “index.*..” |grep -v “@@” > /tmp/gronk2
diff /tmp/gronk1 /tmp/gronk2 &> /dev/null
if [ $? -eq 0 ] ;then
# Variations in feedback solely
similar=”[same]”
oldsha1s[$old]=”match”
break
fi
oldsha1s[$old]=”match”
echo “compare_them $oldsha $newsha” >> $script
fi
completed
echo “$new $oldsha $newsha $similar $newdesc”
completed

echo
echo “Lacking from $newbranch:”
the_missing=””
# Now run by way of the olds we have not matched up
for previous in `seq 0 $oldcount`; do
if check $ != “match”; then
olddesc=`git present $ | head -5 | tail -1|reduce -b5-`
echo “$ $olddesc”
the_missing=`echo “$the_missing $”`
fi
completed

echo “The lacking: ” $the_missing
echo “Examine script generated at: $script”
#git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘

6. gitlog.discover

#!/bin/bash
#
# Discover the upstream equal patch
#
# gitlog.discover
#
cwd=$PWD
param1=$1
ubranch=$2
patches=0
script=/tmp/compare_upstream.sh
echo “#!/bin/bash” > $script
/usr/bin/chmod 755 $script
echo “# Generated by gitbranchcmp3.sh” >> $script
echo “# Run this script to check the mismatched patches” >> $script
echo ” ” >> $script
echo “perform compare_them()” >> $script
echo “{” >> $script
echo ” cwd=$PWD” >> $script
echo ” git present –pretty=electronic mail –patch-with-stat $2 > /tmp/gronk2″ >> $script
echo ” cd ~/linux.git/fs/gfs2″ >> $script
echo ” git present –pretty=electronic mail –patch-with-stat $1 > /tmp/gronk1″ >> $script
echo ” cd $cwd” >> $script
echo ” kompare /tmp/gronk1 /tmp/gronk2″ >> $script
echo “}” >> $script
echo ” ” >> $script

#echo “Gathering upstream patch data. Please wait.”
department=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
monitoring=`git rev-parse –abbrev-ref –symbolic-full-name @`

cd ~/linux.git
if check “X$” = “X”; then
ubranch=`git department -a | grep “*” | reduce -d ‘ ‘ -f2`
fi
utracking=`git rev-parse –abbrev-ref –symbolic-full-name @`
#
# collect an inventory of gfs2 patches from grasp simply in case we won’t discover it
#
#git log –abbrev-commit –pretty=format:” %h %<|(32)%an %s” grasp |grep -i -e “gfs2” -e “dlm” > /tmp/gronk
git log –reverse –abbrev-commit –pretty=format:”ms %h %<|(32)%an %s” grasp fs/gfs2/ > /tmp/gronk.gfs2
# ms = in Linus’s grasp
git log –reverse –abbrev-commit –pretty=format:”ms %h %<|(32)%an %s” grasp fs/dlm/ > /tmp/gronk.dlm

cd $cwd
LIST=`git log –reverse –abbrev-commit –pretty=oneline $monitoring..$department | reduce -d ‘ ‘ -f1 |paste -s -d ‘ ‘`
for i in $LIST; do patches=$(echo $patches + 1 | bc);completed
/usr/bin/echo “———————–[” $branch “-” $patches “patches ]———————–“
patches=$(echo $patches – 1 | bc);
for i in $LIST; do
if [ $patches -eq 1 ]; then
cnt=” ^”
elif [ $patches -eq 0 ]; then
cnt=” H”
else
if [ $patches -lt 10 ]; then
cnt=” $patches”
else
cnt=”$patches”
fi
fi
/usr/bin/git present –abbrev-commit -s –pretty=format:”$cnt %h %<|(32)%an %s” $i
desc=`/usr/bin/git present –abbrev-commit -s –pretty=format:”%s” $i`
cd ~/linux.git
cmp=1
up_eq=`git log –reverse –abbrev-commit –pretty=format:”lo %h %<|(32)%an %s” $utracking..$ubranch | grep “$desc”`
# lo = in native for-next
if check “X$up_eq” = “X”; then
up_eq=`git log –reverse –abbrev-commit –pretty=format:”fn %h %<|(32)%an %s” grasp..$utracking | grep “$desc”`
# fn = in for-next for subsequent merge window
if check “X$up_eq” = “X”; then
up_eq=`grep “$desc” /tmp/gronk.gfs2`
if check “X$up_eq” = “X”; then
up_eq=`grep “$desc” /tmp/gronk.dlm`
if check “X$up_eq” = “X”; then
up_eq=” Not discovered upstream”
cmp=0
fi
fi
fi
fi
echo “$up_eq”
if [ $cmp -eq 1 ] ; then
UP_SHA1=`echo $up_eq|reduce -d’ ‘ -f2`
echo “compare_them $UP_SHA1 $i” >> $script
fi
cd $cwd
patches=$(echo $patches – 1 | bc)
completed
echo “Examine script generated: $script”

Supply

Germany Devoted Server

Leave a Reply