File: cleanup-commit

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (130 lines) | stat: -rwxr-xr-x 2,836 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash

##
## The cleanup-commit script
## -------------------------
## This little script will cleanup your commit before you send it. You need to add the files to the staged area and
## run this script. It will automatically cleanup tabs and trailing spaces for the files you added and then add the
## clean versions to the staging area.
##
## Use at your own risk, I spent some time making the script error-proof so it will abort if sees any inconsistency,
## but of course playing around with your commit might break things. Btw, it saves the original file to file.bak.
##
## Happy hacking!
##

ABORT="Ab0rT0p3r4+|0n"

#
# Cleanup function
#
function cleanup {
  echo Cleaning up $1...
  # prepare the ground
  rm -rf $1.bak
  # compress <TAB> into double <BLANK> and eliminate trailing <BLANC>s
  sed -i.bak -e 's/\t/  /g' -e 's/ *$//' $1
}


#
# Get the git status for the current staged commit
#
FULLSTATUS=`git status --porcelain`

if [ $? -ne 0 ]
then
  echo "Unable to run git. Check if:"
  echo " -- git is installed (you can run git in the command line)"
  echo " -- the current directory is a valid git repository"
  exit 1
fi

echo

#
# Based on the status decide what files will get cleaned up
#
CLEANUP_FILES=`echo "$FULLSTATUS" | while read LINE
do

  STATUS=$(echo $LINE | sed 's/^\(..\).*$/\1/')
  if [ $? -ne 0 ]
  then
    echo "Could not get the status for line: $LINE"
    echo " -- you have the basic unix tools installed (grep, cut, sed)"
    echo $ABORT # This goes to CLEANUP_FILES
    exit 1
  fi

  FILES=$(echo $LINE | sed 's/^..//')
  FILE1=$(echo $FILES | cut -d ' ' -f 1)
  FILE2=$(echo $FILES | cut -d ' ' -f 3)

  case "$STATUS" in
  [AMRDC]" ")
    case "$STATUS" in
      "A "|"M ")
         echo $FILE1
         ;;
      "R ")
         echo $FILE2
         ;;
      "D ")
         #nothing to do
         ;;
      "C ")
         echo $FILE1
         echo $FILE2
         ;;
    esac
    ;;
  "??")
    # File is not tracked, no need to do anything about it
    # echo Untracked: $FILE1
    ;;
  *)
    echo "Unstable status of file $FILE1 (\"$STATUS\")" >&2
    echo "Aborting cleanup!" >&2
    echo $ABORT # This goes to CLEANUP_FILES
    exit 1
  esac
done; echo $CLEANUP_FILES`


#
# Perform actual cleanup
#
case $CLEANUP_FILES in
*"$ABORT")
  echo
  exit 1
  ;;
"")
  echo Nothing to do!
  ;;
*)
  cd $(git rev-parse --show-toplevel)

  if [ $? -ne 0 ]
  then
    echo Unexpected error: cannot cd to the repository root
    echo Aborting cleanup!
    exit 1
  fi

  echo "$CLEANUP_FILES" | while read FILE
  do
    cleanup $FILE
  done

  cd - &>/dev/null

  echo
  echo "Cleanup done: "
  echo " - original files saved as .bak"
  echo " - you can do \"git diff\" to see the changes the script did"
  echo " - you can do \"git commit -a\" to commit the cleaned up files"
  echo
  ;;
esac