Saturday, 30 July 2016

Version Control - 08.GIT-ADD

We now have some idea on GIT. One of the painful thing is when we or someone keep on modifying the code or files then we need to stage each and every change to have them under version control. If we have some numerous number of additions, deletions or modifications then it would be somehow become painful to manage those. Below script can be used to get everything which is ready to be staged. In this script i am checking the git status and getting the list of files which have been modified and adding the changes on those to stage.

 #!/bin/bash  
 # GIT Alias to status  
 git config --global alias.s "status -s"  
 A=`git s | grep '^A\|^AM\|^AR\|^AD^ A' | awk '{print $2}'`  
 U=`git s | grep ^? | awk '{print $2}'`  
 M=`git s | grep '^M\|^MA\|^MR\|^MM\|^MD\|^ M' | awk '{print $2}'`  
 R=`git s | grep '^R\|^RA\|^RM\|^RR\|^RD\|^ R' | awk '{print $4}'`  
 D=`git s | grep '^D\|^ D' | awk '{print $2}'`  
 function add {  
      for i in `echo $A`  
      do  
           echo "Adding $i to staging..."  
           git add $i  
      done  
 }  
 function stgnew {  
      for i in `echo $U`  
      do  
           echo "Adding $i to staging..."  
           git add $i  
      done  
 }  
 function modify {  
      for i in `echo $M`  
      do  
           echo "Modifying $i and staging..."  
           git add $i  
      done  
 }  
 function rename {  
      for i in `echo $R`  
      do  
           R1=`git s | grep $i | awk '{print $2}'`  
           R2=`git s | grep $i | awk '{print $4}'`  
           echo "Renaming/Moving $R1 to $R2 and staging..."  
           git add $R2  
      done  
 }  
 function delete {  
      for i in `echo $D`  
      do  
           echo "Removing $i ..."  
 #          git add $i  
      done  
 }  
 if [[ -z $(git s) ]]  
 then  
      echo "There are neither additions nor deletions to stage"  
 else  
      for i in A U M R D  
      do  
           case $i in  
           A)  
                add  
           ;;  
           U)  
                stgnew  
           ;;  
           M)  
                modify  
           ;;  
           R)  
                rename  
           ;;  
           D)  
                delete  
           ;;  
           esac  
      done  
 fi  
 # END 

No comments :

Post a Comment