aboutsummaryrefslogtreecommitdiffstats
path: root/tools/kgit-init
blob: f71ae0084244a62a736549de3587adae9a512e9d (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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only

#  (kgit-init), (initialize a meta-based kernel git tree)

#  Copyright (c) 2008-2011 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

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

path=`dirname $0`
. $path/kgit

usage()
{
cat <<EOF

 kgit-init [-v] [-c] <src> [<base_branch>] <tgt_repo>
 
  Initialize an existing or new repository

   -c: clean the tgt_repo after the clone completes. This will
       perform garbage collection and remove all existing 
       branches (under the assumption they'll be recreated)

   <src>        : The source repository.
   <base branch>: optional. indicates a base branch point in the
                  src repository to use as the base for the
                  dest repository
   <tgt_rep>    : repository to initialize. Can exist or not
                  exist before the command is run.

   -h: help
   -v: verbose

EOF
}

# command line processing
while [ $# -gt 0 ]; do
    case "$1" in
	-o|--o)
		dest="$2"
		shift
		;;
	-v|--v)
		verbose=t
		;;
	-c|--c)
		clean=t
		;;
	-h|--h) 
	        usage
                exit;
                ;;
	*)
	        break
		;;
    esac
    shift
done

if [ ! $# -gt 0 ]; then
    usage
    exit
fi

source kgit

# assign the default set of arguments
#   source tree, branch point and destination
src=$1
initial_branch_point=$2
dest=$3

content_branch=master

# now fixup based on zero'd values
if [ -z "$initial_branch_point" ]; then
    # no source, no branch, just a dest. We'll be
    # reanimating this repo
    dest=$src
    src=
else
    if [ -z "$dest" ]; then
	# no branch was passed, this is just <src> <dest>
	dest=$initial_branch_point
	initial_branch_point=
    fi
fi

if [ -n "$dest" ] && [ -z "$src" ]; then
    if [ ! -d "$dest" ]; then
	echo "ERROR. If no src is passed, $dest must exist"
	exit 1
    fi
fi  

if [ -z "$src" ] && [ ! -d "$src" ]; then
    echo "ERROR. $src is not a valid src repository"
    exit 1
fi

if [ -z "$message" ]; then
    message="base: add .gitignore and exclusions"
fi

# figure out what type of type destination repository we are creating
new_repo=
existing_repo=
if [ -d "$dest/.git" ]; then
    existing_repo=t
else
    # this means we are starting a repository from scratch
    new_repo=t
fi

if [ -n "$new_repo" ]; then
    if [ -n "$verbose" ]; then
	echo "[INFO] git clone --shared $src $dest"
    else
	clone_opt=-q
    fi

    mkdir -p $dest

    clone_opt="$clone_opt --shared"

    # this means that we don't have to worry about converting
    # remote branches into local tracking ones
    git clone --bare $clone_opt $src $dest/.git
    r=$?
    if [ $r -ne 0 ]; then
	echo [ERROR] git clone of \"$src\" failed
	exit $r
    fi

    # check to see if the src is a bare repository and if
    # it is, do an optimized startup
    if [ -d $src/.git ]; then
	cp -a $src/.git/config $dest/.git
	cd $dest
    else
	cd $dest
	git config core.bare false
	# layering on 1k+ of patches makes auto gc inevitable/annoying
	git config gc.auto 0
	git checkout -q -f $checkout_opts master
	if [ $? != 0 ]; then
	    echo "[ERROR] git checkout -q -f master failed"
	    exit -1
	fi
    fi

    if [ -n "$clean" ]; then
	clean_tgt_repo
    fi

    git checkout -q -f $checkout_opts master
    if [ $? != 0 ]; then
	echo "[ERROR] git checkout -qf master failed"
	exit -1
    fi
    git reset --hard $initial_branch_point &> /dev/null
    if [ $? -ne 0 ]; then
	echo "[ERROR] branch point $initial_branch_point is not valid"
	exit 1
    fi

    meta_dir=$(kgit --meta)

    if [ ! -d "$meta_dir/cfg/scratch/obj" ]; then
	mkdir -p $meta_dir/cfg/scratch/obj
    fi
    if [ ! -d "$meta_dir/patches" ]; then
	mkdir -p $meta_dir/patches
    fi

    git checkout -q $content_branch
    if [ $? != 0 ]; then
	echo "[ERROR] git checkout -q content_branch failed"
	exit -1
    fi
fi