Skip to content

Commit 40e188f

Browse files
committed
Switch to rev-parse
This handles non-annotated tags, which were not handled well before. It does mean that we use the hash of the (annotated) tag object instead of the commit, but that seems OK. Added a test case.
1 parent cb1ef89 commit 40e188f

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

cmd/git-sync/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,16 @@ func cloneRepo(repo, branch, rev string, depth int, gitRoot string) error {
357357
}
358358

359359
func hashForRev(rev, gitRoot string) (string, error) {
360-
output, err := runCommand(gitRoot, "git", "rev-list", "-n1", rev)
360+
output, err := runCommand(gitRoot, "git", "rev-parse", rev)
361361
if err != nil {
362362
return "", err
363363
}
364364
return strings.Trim(string(output), "\n"), nil
365365
}
366366

367367
func revIsHash(rev, gitRoot string) (bool, error) {
368-
// If a rev is a tag name or HEAD, rev-list will produce the git hash. If
369-
// it is already a git hash, the output will be the same hash. Of course, a
368+
// If rev is a tag name or HEAD, rev-parse will produce the git hash. If
369+
// rev is already a git hash, the output will be the same hash. Of course, a
370370
// user could specify "abc" and match "abcdef12345678", so we just do a
371371
// prefix match.
372372
output, err := hashForRev(rev, gitRoot)
@@ -426,7 +426,7 @@ func getRevs(localDir, branch, rev string) (string, string, error) {
426426
if rev == "HEAD" {
427427
ref = "refs/heads/" + branch
428428
} else {
429-
ref = "refs/tags/" + rev + "^{}"
429+
ref = "refs/tags/" + rev
430430
}
431431

432432
// Figure out what hash the remote resolves ref to.

test_e2e.sh

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ function finish() {
5656

5757
trap finish INT EXIT
5858

59-
#########################
59+
# #####################
6060
# main
61-
#########################
61+
# #####################
6262

6363
# Build it
6464
make container REGISTRY=e2e TAG=$(make -s version)
@@ -248,6 +248,52 @@ TAG="$TESTCASE"--TAG
248248
# First sync
249249
echo "$TESTCASE 1" > "$REPO"/file
250250
git -C "$REPO" commit -qam "$TESTCASE 1"
251+
git -C "$REPO" tag -f "$TAG" >/dev/null
252+
GIT_SYNC \
253+
--logtostderr \
254+
--v=5 \
255+
--wait=0.1 \
256+
--repo="$REPO" \
257+
--rev="$TAG" \
258+
--root="$ROOT" \
259+
--dest="link" > "$DIR"/log."$TESTCASE" 2>&1 &
260+
sleep 3
261+
assert_link_exists "$ROOT"/link
262+
assert_file_exists "$ROOT"/link/file
263+
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
264+
# Add something and move the tag forward
265+
echo "$TESTCASE 2" > "$REPO"/file
266+
git -C "$REPO" commit -qam "$TESTCASE 2"
267+
git -C "$REPO" tag -f "$TAG" >/dev/null
268+
sleep 3
269+
assert_link_exists "$ROOT"/link
270+
assert_file_exists "$ROOT"/link/file
271+
assert_file_eq "$ROOT"/link/file "$TESTCASE 2"
272+
# Move the tag backward
273+
git -C "$REPO" reset -q --hard HEAD^
274+
git -C "$REPO" tag -f "$TAG" >/dev/null
275+
sleep 3
276+
assert_link_exists "$ROOT"/link
277+
assert_file_exists "$ROOT"/link/file
278+
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
279+
# Add something after the tag
280+
echo "$TESTCASE 3" > "$REPO"/file
281+
git -C "$REPO" commit -qam "$TESTCASE 3"
282+
sleep 3
283+
assert_link_exists "$ROOT"/link
284+
assert_file_exists "$ROOT"/link/file
285+
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
286+
# Wrap up
287+
remove_sync_container
288+
wait
289+
pass
290+
291+
# Test tag syncing with annotated tags
292+
testcase "tag-sync-annotated"
293+
TAG="$TESTCASE"--TAG
294+
# First sync
295+
echo "$TESTCASE 1" > "$REPO"/file
296+
git -C "$REPO" commit -qam "$TESTCASE 1"
251297
git -C "$REPO" tag -af "$TAG" -m "$TESTCASE 1" >/dev/null
252298
GIT_SYNC \
253299
--logtostderr \

0 commit comments

Comments
 (0)