aboutsummaryrefslogtreecommitdiffstats
path: root/tools/kgit
blob: 195addba0f919753afb9d8edf3de988c89062a2d (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only

#  (kgit), (mux and demux for kgit* tools)

#  Copyright (c) 2008-2016 Wind River Systems, Inc.

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#  See the GNU General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

mydir=`dirname $0`
PATH=$mydir:$PATH

# For consistent behaviour with "grep -w"
LC_ALL=C
export LC_ALL

usage()
{
cat <<EOF

 kgit -h --version <command>

   --meta  :  print the meta directory name
   -h      :  this message
   -v      :  version

EOF
}

meta_dir()
{
    # if there's already a located and logged meta directory, just use that and
    # return. Otherwise, we'll look around to see what we can find.
    if [ -f ".metadir" ]; then
        md=`cat .metadir`
        echo $md
        return
    fi

    # determine the meta directory name
    meta_dir_options=`git ls-files -o --directory`
    for m in $meta_dir_options; do
        if [ -d "$m/cfg" ]; then
            md=`echo $m | sed 's%/%%'`
        fi
    done

    # if nothing was found, we create the minimal structure
    if [ -z "$md" ]; then
        md=".kernel-meta"
    fi

    mkdir -p "${md}/cfg/scratch"

    # store our results where other scripts can quickly find the answer
    echo "${md}" > .metadir
    # return the directory to he caller
    echo $md
}

_commands()
{
    find "`dirname $0`" -maxdepth 1 -name "kgit-*" -type f \
                  -perm -111 | sed -e "s/.*\\/`basename $0`-//"
}

# take the entire command into an array
raw_command=($@)

# are we being sourced ?
script_name=$( basename $0 )
this_script=$( basename ${BASH_SOURCE} )
if [[ ${script_name} = ${this_script} ]] ; then
    sourced=
else
    sourced=t
fi

if [ -z "${sourced}" ]; then
    non_dashed=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -v) verbose=t
                ;;
            --meta)
                if [ -z "${non_dashed}" ]; then
                    meta=t
                    meta_dir
                    exit 0
                fi
                ;;
            -h|--help)
                if [ -z "${non_dashed}" ]; then
                    usage
                    exit
                fi
                ;;
            --*)
                # if the next argument is dashed, we just add $1 to our collection
                # of dashed arguments. If $2 is NOT dashed, we assume it is a parameter
                # to the --dashed option, and we grab it as well. Don't try and mix
                # dashed and non-dashed, since we'll grab them!
                case $2 in
                    --*)
                        dashed="${dashed} $1"
                        ;;
                    *)
                        # sneak a : in between, so we can split it later
                        dashed="${dashed} $1:$2"
                        shift
                        ;;
                esac
                ;;
            *) non_dashed="${non_dashed} $1"
               ;;
        esac
        shift
    done

    # based off guilt's demux and other references
    if [ "`basename $0`" = "kgit" ]; then
        # make an array from whatever was non-dashed
        cmd_options_non_dashed=(${non_dashed})

        cmd=
        if [ ${#cmd_options_non_dashed[@]} -ne 0 ]; then
            # take first arg, and try to execute it
            arg="${cmd_options_non_dashed[0]}"
            dir=`dirname $0`

            if [ -x "$dir/kgit-$arg" ]; then
                cmd=$arg
            else
                # might be a short handed
                for command in $(_commands); do
                    case $command in
                        $arg*)
                            if [ -x "$dir/kgit-$command" ]; then
                                cmd=$command
                            fi
                            ;;
                    esac
                done
            fi
            if [ -n "$cmd" ]; then
                # remove the first element, since that is our command. The rest are what
                # we pass to the sub command.
                sub_cmd_options=("${raw_command[@]:1}")
                exec "$dir/kgit-$cmd" "${sub_cmd_options[@]}"

                # this is not reached because of the exec
                die "Exec failed! Something is terribly wrong!"
            fi
        fi

        # no args passed or invalid command entered, just output help summary
        usage
        echo " Available commands: "
        echo ""
        echo -n "    "
        count=0
        for c in $(_commands); do
            if [ $count -lt 7 ]; then
                echo -n "$c "
            else
                echo "$c"
                count=0
                echo -n "    "
            fi
            let count=$count+1
        done
        echo ""; echo ""

        # now, let's exit
        exit 1
    fi
fi


get_current_git_branch()
{
    git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}   

strlen ()		# echo ${#string} ...
{
    for i in "$@"; do
        echo ${#i}
    done
}

# arg1: length limit
# arg2: string
length_limited_string()
{
    limit=$1
    input_string=$2

    mid_point=`expr length $input_string / 2`
    mid_point=`expr $mid_point - 5`

    # limit_point=`expr length $limit / 2`
    # len=`expr length $input_string`
    # if [ `expr $limit_point+$limit_point < $len` ]; then
    #    echo "why bother ?"
    # fi

    x=${input_string:0:$mid_point}
    y=${input_string: -$mid_point}

    echo "$x..$y"
}

find_dir()
{
    start_dir=`pwd`
    done=0
    count=0
    tgt_dir="$1"
    max_depth=$2

    if [ -z "$max_depth" ]; then
	max_depth=4
    fi

    cdir=".";
    while [ $done -eq 0 ]; do
        # echo "testing: $start_dir/$cdir/tgt_dir"
        if [ -d "$start_dir/$cdir/$tgt_dir" ]; then
            done=1;
        else
            # echo "not found, heading back one ...";
            cdir="../$cdir";
            let count=$count+1;
            # echo "count: $count";
        fi;

        if [ $count -gt $max_depth ]; then
            cdir="";
            done=1;
        fi;
    done

    if [ "$cdir" != "" ]; then\
        cdir=`echo $cdir | sed "s/\.\///"`;
        echo "$cdir/$tgt_dir";
    fi
}

read_answer()
{
    prompt=$1
    default_answer=$2

    answer="$default_answer"
    echo -n "    $prompt [$default_answer]: "
    read answer

    echo $answer
}

clean_path()
{
    _p=$1

    _p=`echo $_p | sed 's%//%/%g' | sed 's%\\.\/%%g'`

    # double check our efforts
    while :
    do
	case $_p in
	    # disable the check for trailing slash and removal. We want this
	    # to be present, and we really only care about double //
	    #*/) _p=${_p%/} 
	    #	;;

	    *//*) _p=`echo $_p | sed s%//%/%g` 
                ;;

	    *) break
		;;
	esac
    done

    echo $_p
}

clean_repo()
{
    if [ -n "$verbose" ]; then
	echo "[INFO] cleaning base clone"
    fi

    if [ -d .git ]; then
	# Keep tags that came from kernel.org; delete the rest, i.e.
	# v3.nn, v2.6.nn and v2.6.nn-rcM and v2.6.nn.M (nn=10,11, ..99, M=0,1, ...99)
	for tg in `git tag| grep -v 'v2.6.[0-9]\{2\}\(\(-rc[0-9]\{1,2\}\)\{0,1\}$\|\.[0-9]\{1,2\}$\)' |
                            grep -v 'v3.[0-9]\{2\}\(\(-rc[0-9]\{1,2\}\)\{0,1\}$\|\.[0-9]\{1,2\}$\)' |
                            grep -v 'v4.[0-9]\{2\}\(\(-rc[0-9]\{1,2\}\)\{0,1\}$\|\.[0-9]\{1,2\}$\)'`
	do
	    git tag -d $tg > /dev/null
	done

	# We've come here without an active checkout, on what was a bare
	# clone, so manually make sure we are on master. We should be on
	# master anyway, but make sure, otherwise we won't be able to
	# delete whatever branch we are on.
	echo 'ref: refs/heads/master' > .git/HEAD
	for br in `git branch | grep -v '\* master$'`
	do
	    git branch -D $br > /dev/null
	    if [ $? != 0 ]; then
		echo "[ERROR]: delete of branch $br failed"
		exit -1
	    fi
	done
    fi
}