-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-ls.sh
executable file
·183 lines (163 loc) · 5.78 KB
/
git-ls.sh
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
################################################################################
# This Bash script is used to print the files changed by commit with absolute #
# path. The commit is specified by input parameter $1. Use HEAD as the commit #
# if no input parameter is specified. #
# #
# This script will be called by alias "git sls", see conf.git-config. #
################################################################################
#-------------------------------------------------------------------------------
# 1) Global variables
#-------------------------------------------------------------------------------
# Script name and location of this script
scriptName="git-ls.sh"
# Flag for options
opt_p=
opt_c=
# Show all commit files in one line by default if option '-n' is not specified
newLine="No"
#-------------------------------------------------------------------------------
# 2) Usage
#-------------------------------------------------------------------------------
function usage(){
echo
echo "Usage:"
echo "${scriptName} [-h]"
echo "${scriptName} [-p <previous-commit>] [-c <current-commit>] [-e | -n]"
echo "${scriptName} [-c <current-commit>] [-s | -l]"
echo
echo " -p <previous-commit>"
echo " Specify the previous commit that you want to check commit files of."
echo " If this option is not specified, use <current-commit>~ as commit ID"
echo " by default."
echo
echo " -c <current-commit>"
echo " Specify the current commit that you want to check commit files of."
echo " If this option is not specified, use HEAD as commit ID by default."
echo
echo " -e"
echo " Open the commit files with type text/plain by gedit. Exit if gedit"
echo " is not found."
echo
echo " NOTE: The file content is the one in working directory, not the one"
echo " in specified commit. Use option -s to open the files with content"
echo " of the specified commit."
echo
echo " -s"
echo " Show the commit files by less. Exit if less is not found. Note"
echo " that the file content is the one in specified commit, not the one"
echo " in working directory. Use option -e to open the files with content"
echo " of the working directory."
echo
echo " -l"
echo " Unlike -s, it just list commands to show file content in specified"
echo " commit."
echo
echo " -n"
echo " By default, all commit files are shown in one line. One commit file"
echo " per line if this option is specified."
echo
echo " -h"
echo " Show the usage of this script."
echo
}
#-------------------------------------------------------------------------------
# 3) Check script options
#-------------------------------------------------------------------------------
while getopts "p:c:eslnh" arg
do
case ${arg} in
p) # -p <previous-commit>
opt_p=Yes
prevCommit=${OPTARG}
;;
c) # -c <current-commit>
opt_c=Yes
currCommit=${OPTARG}
;;
e) # -e
openByGedit=Yes
;;
s) # -s
openByLessOfCommit=Yes
;;
l) # -l
listCmdOfSpecifiedCommit=Yes
;;
n) # -n
newLine=Yes
;;
h) # -h
usage
exit 1
;;
?) # unkonw argument
echo "ERROR: Unknown argument"
exit 1
;;
esac
done
#--------------------------------------
# Set default commit ID
#--------------------------------------
# If no option "-c <current-commit>", use HEAD by default
if [ x${opt_c} == x ]; then
currCommit="HEAD"
fi
# If no option "-p <previous-commit>", the parent commit of ${currCommit} by default
if [ x${opt_p} == x ]; then
prevCommit="${currCommit}~"
fi
#-------------------------------------------------------------------------------
# 4) Get all changed files between <prevCommit> and <currCommit>
#-------------------------------------------------------------------------------
commitFiles=`git diff-tree --no-commit-id --name-only -r ${prevCommit} ${currCommit}`
#-------------------------------------------------------------------------------
# 5) Print absolute path of each commit file
#-------------------------------------------------------------------------------
topPath=`git rev-parse --show-toplevel`
unset absoluteFiles
# option -l
if [ x${listCmdOfSpecifiedCommit} == xYes ]; then
for file in ${commitFiles}; do
fileType=`file -b --mime-type ${file}`
if [[ ${fileType} =~ 'text/'.* ]]; then
echo "git cat-file -p ${currCommit}:${file} | less -N -M"
fi
done
# option -s
elif [ x${openByLessOfCommit} == xYes ]; then
lessBin=`which less`
if [ x${lessBin} == x ]; then
echo "ERROR: less is not found"
else
for file in ${commitFiles}; do
git cat-file -p ${currCommit}:${file} | less -N -M
done
fi
# option -e
elif [ x${openByGedit} == xYes ]; then
geditBin=`which gedit`
if [ x${geditBin} == x ]; then
echo "ERROR: gedit is not found"
else
for file in ${commitFiles}; do
fileType=`file -b --mime-type ${file}`
if [[ ${fileType} =~ 'text/'.* ]]; then
absoluteFiles+="${topPath}/${file} "
fi
done
${geditBin} ${absoluteFiles} &
fi
# option -n
elif [ x${newLine} == xYes ]; then
for file in ${commitFiles}; do
echo ${topPath}/${file}
done
# default
else
for file in ${commitFiles}; do
absoluteFiles+="${topPath}/${file} "
done
echo ${absoluteFiles}
fi