-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubrepo
executable file
·203 lines (170 loc) · 4.63 KB
/
subrepo
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
#!/bin/bash
#
# git-subrepo common functions
#
# Copyright (C) Brecht Machiels <[email protected]>
#
require_work_tree
GIT_TOP_LEVEL=$(git rev-parse --show-toplevel)
debug()
{
if [ -n "$debug" ]; then
echo "$@" >&2
fi
}
say()
{
if [ -z "$quiet" ]; then
echo "$@" >&2
fi
}
assert()
{
if "$@"; then
:
else
die "assertion failed: " "$@"
fi
}
length()
{
echo $#;
}
short()
# Return abbreviated (but still unique) version for each of the SHA-1 values
# in $sha1s.
{
sha1s=$@
result=
for sha1 in $sha1s; do
result="$result $(git rev-parse --short $sha1)"
done
echo $result
}
read_subrepo_dirs()
# Reads the subrepo directories from .gitsubrepos (one per line) and returns
# them as a list. If no .gitsubrepos file is found, returns and empty list.
{
dotfile="$GIT_TOP_LEVEL/.gitsubrepos"
if [ -f $dotfile ]; then
cat $dotfile |
while read line; do
echo "$line"
done
fi
}
find_last_subrepo_commit()
# Return the commit-ish of the last $subdir subrepo commit.
{
commit="$1"
subdir="$2"
debug "[D] CALL $FUNCNAME(commit=$(short $commit), subdir='$subdir')"
git rev-list --merges --parents --topo-order $commit |
while read rev main_parent parents; do
subrepo_parent=$(get_subrepo_parent "$rev" "$parents" "$subdir")
if [[ -n $subrepo_parent ]]; then
debug "[D] RTRN $FUNCNAME -> $(short $subrepo_parent)"
echo $subrepo_parent
break
fi
done || exit $?
}
get_subrepo_parent()
# Assuming $commit is a subtree merge commit, returns the commit-ish of the
# parent of $commit that represents the subtree $subdir.
{
commit="$1"
parents="$2"
subdir="$3"
debug "[D] CALL $FUNCNAME(commit=$(short $commit), subdir='$subdir')"
subrepo_tree=$(subtree_for_commit $commit $subdir)
for parent in $parents; do
parent_tree=$(toptree_for_commit $parent)
if [[ "$parent_tree" = "$subrepo_tree" ]]; then
echo $parent
fi
done || exit $?
}
toptree_for_commit()
# Return the tree-ish for the root directory in $commit.
#
# (borrowed from git-subtree)
{
commit="$1"
debug "[D] CALL $FUNCNAME(commit=$(short $commit)"
# TODO: replace with plumbing
git show -s --format=format:%T "$commit" -- || exit $?
}
subtree_for_commit()
# Return the tree-ish for $subdir in $commit.
#
# (borrowed from git-subtree)
{
commit="$1"
subdir="$2"
debug "[D] CALL $FUNCNAME(commit=$(short $commit), subdir='$subdir')"
git ls-tree "$commit" -- "$subdir" |
while read mode type tree name; do
assert [ "$name" = "$subdir" ]
assert [ "$type" = "tree" -o "$type" = "commit" ]
[ "$type" = "commit" ] && continue # ignore submodules
echo $tree
break
done
}
has_subrepo_changes()
# Check whether $commit contains changes to files in $subdir.
{
commit="$1"
subdir="$2"
debug "[D] CALL $FUNCNAME(commit=$(short $commit), subdir='$subdir')"
! git diff-tree --exit-code --quiet "$commit" -- "$subdir" >/dev/null
}
make_parent_args()
# Takes a list of parent commit-ishes and returns the argument list that can
# be passed to git-commit-tree.
#
# $ make_parent_args 0e4fb0 4e988 c01cbf
# > -p 0e4fb0 -p 4e988 -p c01cbf
{
parents=$@
result=
debug "[D] CALL $FUNCNAME(parents='$(short $parents)')"
for parent in $parents; do
result="$result -p $parent"
done
echo $result
}
clone_commit()
# Create a new commit consisting of:
# - the metadata of $source_commit
# - the directory tree described by the tree-ish $tree
# - the parents listed in $parents
#
# (borrowed from git-subtree)
{
source_commit="$1"
tree="$2"
parents="$3"
debug "[D] CALL $FUNCNAME(source_commit=$(short $source_commit), tree=$(short $tree), parents='$(short $parents)')"
parent_args=$(make_parent_args $parents)
# We're going to set some environment vars here, so
# do it in a subshell to get rid of them safely later
git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%B' "$source_commit" |
(
read GIT_AUTHOR_NAME
read GIT_AUTHOR_EMAIL
read GIT_AUTHOR_DATE
read GIT_COMMITTER_NAME
read GIT_COMMITTER_EMAIL
read GIT_COMMITTER_DATE
export GIT_AUTHOR_NAME \
GIT_AUTHOR_EMAIL \
GIT_AUTHOR_DATE \
GIT_COMMITTER_NAME \
GIT_COMMITTER_EMAIL \
GIT_COMMITTER_DATE
(echo -n "$annotate"; cat ) |
git commit-tree $parent_args $tree # reads the rest of stdin
) || die "Can't clone commit $source_commit"
}