commit 567001ae976c0f92835613778c51db71afc405020301113a2d80e1f7fb1dbd99 from: Stefan Sperling date: Mon May 18 15:02:25 2026 UTC fix serving a branch of unrelated history with gotd Fix a case where gotd started sending pack progress output in response to the client sending 'done', without any ACK or NAK in-between. I saw this happen when trying to fetch an unrelated main branch from gotd, with completely different history and initial root commit, into an existing repository with a different main branch history. Add test cases covering 'got fetch' against a git-upload-pack server (is always passing), and 'got fetch' and 'git fetch' against gotd (was not passing, i.e. both got and git fail to fetch unless this fix is applied). ok op@ commit - dada3c2badb26038d48e8de5f367ce086e5afaef6c7d694220e69b44e82a1761 commit + 567001ae976c0f92835613778c51db71afc405020301113a2d80e1f7fb1dbd99 blob - e02160cc83e98aa06cccaeaee15398a0682a9add58fbee01537726c02425eb9c blob + 21b96ad8ac52530cdcbc62b83f142ecfd36df7b3c562183638f16acc1df8147f --- lib/serve.c +++ lib/serve.c @@ -803,7 +803,7 @@ serve_read(int infd, int outfd, int gotd_sock, const c STATE_DONE, }; enum protostate curstate = STATE_EXPECT_WANT; - int have_ack = 0, use_sidebands = 0, seen_have = 0; + int have_ack = 0, use_sidebands = 0, seen_have = 0, sent_nak = 0; int packfd = -1; size_t pack_chunksize; @@ -864,6 +864,7 @@ serve_read(int infd, int outfd, int gotd_sock, const c err = send_nak(outfd, chattygot); if (err) goto done; + sent_nak = 1; } if (curstate == STATE_EXPECT_MORE_WANT) curstate = STATE_EXPECT_HAVE_OR_DONE; @@ -908,7 +909,7 @@ serve_read(int infd, int outfd, int gotd_sock, const c } } - if (!seen_have) { + if (!seen_have || (!have_ack && !sent_nak)) { err = send_nak(outfd, chattygot); if (err) goto done; blob - d78b90d69e94a6e24d03531abf2f64d646bd412c939a53baddd7ae076c5fe274 blob + 12bc63f9a34a1b0e3c80f8065b5358982bf4b5691c6196a530318b3bfec2547e --- regress/cmdline/fetch.sh +++ regress/cmdline/fetch.sh @@ -2153,6 +2153,107 @@ test_fetch_basic_http() { test_done "$testroot" "$ret" } +test_fetch_unrelated_history() { + local testroot=`test_init fetch_unrelated_history` + local testurl=ssh://127.0.0.1/$testroot + local commit_id=`git_show_head $testroot/repo` + + got clone -q $testurl/repo $testroot/repo-clone + ret=$? + if [ $ret -ne 0 ]; then + echo "got clone command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + got ref -l -r $testroot/repo-clone > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + echo "got ref command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo "HEAD: refs/heads/master" > $testroot/stdout.expected + echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected + echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \ + >> $testroot/stdout.expected + echo "refs/remotes/origin/master: $commit_id" \ + >> $testroot/stdout.expected + + rm -rf $testroot/repo + mkdir $testroot/repo + git_init $testroot/repo + echo 'new history with just one file' > $testroot/repo/alpha + git -C $testroot/repo add alpha + git_commit $testroot/repo -m "adding the test tree" + touch $testroot/repo/.git/git-daemon-export-ok + local commit_id2=`git_show_head $testroot/repo` + + got fetch -q -r $testroot/repo-clone > $testroot/stdout \ + 2> $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + echo "got fetch command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo -n > $testroot/stdout.expected + + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + got ref -l -r $testroot/repo > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + echo "got ref command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo "HEAD: refs/heads/master" > $testroot/stdout.expected + echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected + + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + got ref -l -r $testroot/repo-clone > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + echo "got ref command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo "HEAD: refs/heads/master" > $testroot/stdout.expected + echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected + echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \ + >> $testroot/stdout.expected + echo "refs/remotes/origin/master: $commit_id2" \ + >> $testroot/stdout.expected + + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + test_done "$testroot" "$ret" +} + + test_parseargs "$@" run_test test_fetch_basic no-sha256 run_test test_fetch_list no-sha256 @@ -2172,3 +2273,4 @@ run_test test_fetch_delete_remote_refs no-sha256 run_test test_fetch_honor_wt_conf_bflag no-sha256 run_test test_fetch_from_out_of_date_remote no-sha256 run_test test_fetch_basic_http no-sha256 +run_test test_fetch_unrelated_history no-sha256 blob - 35641361678d59e9cdf95f660adfe311203584c39ec46538cdae77d3dda7627a blob + a759625757238ee3e7284db3b4874839820d352987ff2b9d7d192092791618ef --- regress/gotd/repo_write.sh +++ regress/gotd/repo_write.sh @@ -554,9 +554,192 @@ EOF test_done "$testroot" 0 } +test_fetch_unrelated_history() { + local testroot=`test_init fetch_unrelated_history 1` + + got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone + ret=$? + if [ $ret -ne 0 ]; then + echo "got clone failed unexpectedly" >&2 + test_done "$testroot" "1" + return 1 + fi + local commit_id0=`git_show_head $testroot/repo-clone` + + mkdir $testroot/new-repo + git_init $testroot/new-repo + git -C $testroot/new-repo symbolic-ref HEAD refs/heads/main + + echo 'new history with just one file' > $testroot/new-repo/alpha + git -C $testroot/new-repo add alpha + git_commit $testroot/new-repo -m "adding the test tree" + for i in 1 2 3; do + echo 'line $i' >> $testroot/new-repo/alpha + git -C $testroot/new-repo add alpha + git_commit $testroot/new-repo -m "more changes" + done + got tag -r $testroot/new-repo -m "tagging 1.0" 1.0 >/dev/null + local commit_id2=`git_show_head $testroot/new-repo` + local tag_id=`got ref -r "$testroot/new-repo" -l refs/tags/1.0 | \ + awk '{print $2}'` + + cp $testroot/repo-clone/got.conf $testroot/new-repo/.git/got.conf + + got send -q -b main -t 1.0 -r $testroot/new-repo -f + ret=$? + if [ $ret -ne 0 ]; then + echo "got send failed unexpectedly" >&2 + test_done "$testroot" "1" + return 1 + fi + + # Verify that the send operation worked fine. + got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone2 + ret=$? + if [ $ret -ne 0 ]; then + echo "got fetch failed unexpectedly" >&2 + test_done "$testroot" "1" + return 1 + fi + + got tree -R -r $testroot/repo-clone2 > $testroot/stdout + cat > $testroot/stdout.expected < /dev/null + got fetch -X -r $testroot/repo-clone origin > /dev/null + + # Rewrite main branch hisotry to avoid sending a HAVE with a + # commit ID which the server can find locally. + got ref -r $testroot/repo-clone -d refs/heads/main > /dev/null + mkdir $testroot/import-tree + echo 'parallel universe alpha' > $testroot/import-tree/alpha + + got import -r $testroot/repo-clone -b main -m init \ + $testroot/import-tree > /dev/null + ret=$? + if [ $ret -ne 0 ]; then + echo "got import failed unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + local commit_id=`git_show_head $testroot/repo-clone` + + cp -r $testroot/repo-clone $testroot/repo-clone3 + cp $testroot/repo-clone/config $testroot/repo-clone3/config + + # verify that unrelated history can be fetched into the initial clone. + # This used to fail with: + # got-fetch-pack: readpkt: 48: [0x02]1 commits colored, \ + # 0 objects found, deltify 0%[0x0d] + # got-fetch-pack: unexpected message from server + got fetch -q -r $testroot/repo-clone > $testroot/stdout \ + 2> $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + echo "got fetch command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo -n > $testroot/stdout.expected + + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + got ref -l -r $testroot/repo-clone > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + echo "got ref command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + cat > $testroot/stdout.expected < $testroot/stdout \ + 2> $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + echo "git fetch command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo -n > $testroot/stdout.expected + + cmp -s $testroot/stdout $testroot/stdout.expected + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + got ref -l -r $testroot/repo-clone3 > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + echo "got ref command failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + cat > $testroot/stdout.expected <