gitlab create commits api return error "You can only create or edit files when you are on a branch" when startBrach is just created

Summary

create commit api return error "You can only create or edit files when you are on a branch" when the startBrach is just created

Steps to reproduce

  • Create a commit with branch-1 and create another commit start with branch branch-1
  • Repeat the above steps several times

this is a bash script to reproduce

#!/bin/bash

export GITLAB_TOKEN="private token"
export GITLAB_HOST=http://192.168.1.11
export GITLAB_PROJECT="115" # it should have a file named `demo.txt`


postCommit() {
    branch=branch-$RANDOM
    echo ">>>>>"
    curl --show-error --request POST -H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{\"branch\": \"${branch}\", \"start_branch\": \"main\", \"author_email\": \"author@example.com\", \"author_name\": \"Firstname Lastname\", \"actions\":[ {  \"action\": \"update\", \"file_path\": \"./demo.txt\", \"encoding\": \"base64\", \"content\": \"bWVvdyBtZW93IG1lb3c=\"} ],\"commit_message\": \"update a new file\"}" \
        --url "${GITLAB_HOST}/api/v4/projects/${GITLAB_PROJECT}/repository/commits"

    echo ""
    echo "<<<<<"
    branch2=branch-$RANDOM
    curl --show-error --request POST -H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{\"branch\": \"${branch2}\", \"start_branch\": \"${branch}\", \"author_email\": \"author@example.com\", \"author_name\": \"Firstname Lastname\", \"actions\":[ {  \"action\": \"update\", \"file_path\": \"./demo.txt\", \"encoding\": \"base64\", \"content\": \"bWVvdyBtZW93IG1lb3c=\"} ],\"commit_message\": \"update a new file\"}" \
        --url "${GITLAB_HOST}/api/v4/projects/${GITLAB_PROJECT}/repository/commits"
    echo ""
}

for i in {1..100};
do
    echo "-------------------"
    postCommit
done

output

-------------------
>>>>>
{"id":"09598500a0f69a55d1928bc2172cfff7b85b2359","short_id":"09598500","created_at":"2023-12-08T08:30:48.000+00:00","parent_ids":["d033be4619f48c687e058ad26c11826bda238f0f"],"title":"create a new file","message":"create a new file","author_name":"Firstname Lastname","author_email":"author@example.com","authored_date":"2023-12-08T08:30:48.000+00:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2023-12-08T08:30:48.000+00:00","web_url":"http://192.168.139.140:30323/root/demo/-/commit/09598500a0f69a55d1928bc2172cfff7b85b2359","stats":{"additions":1,"deletions":1,"total":2},"status":null,"project_id":115,"last_pipeline":null}
<<<<<
{"message":"You can only create or edit files when you are on a branch"}
-------------------
>>>>>
{"id":"35e9861646bce6dac61757b4112f66c8ad7a2073","short_id":"35e98616","created_at":"2023-12-08T08:30:49.000+00:00","parent_ids":["d033be4619f48c687e058ad26c11826bda238f0f"],"title":"create a new file","message":"create a new file","author_name":"Firstname Lastname","author_email":"author@example.com","authored_date":"2023-12-08T08:30:49.000+00:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2023-12-08T08:30:49.000+00:00","web_url":"http://192.168.139.140:30323/root/demo/-/commit/35e9861646bce6dac61757b4112f66c8ad7a2073","stats":{"additions":1,"deletions":1,"total":2},"status":null,"project_id":115,"last_pipeline":null}
<<<<<
{"message":"You can only create or edit files when you are on a branch"}
-------------------
>>>>>
{"id":"45ecd257c720d88b45eda51724fe940c213101bc","short_id":"45ecd257","created_at":"2023-12-08T08:30:50.000+00:00","parent_ids":["d033be4619f48c687e058ad26c11826bda238f0f"],"title":"create a new file","message":"create a new file","author_name":"Firstname Lastname","author_email":"author@example.com","authored_date":"2023-12-08T08:30:50.000+00:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2023-12-08T08:30:50.000+00:00","web_url":"http://192.168.139.140:30323/root/demo/-/commit/45ecd257c720d88b45eda51724fe940c213101bc","stats":{"additions":1,"deletions":1,"total":2},"status":null,"project_id":115,"last_pipeline":null}
<<<<<
{"id":"750d27ecbbe9a66b6e73598bcd2414bbf8716f8d","short_id":"750d27ec","created_at":"2023-12-08T08:30:50.000+00:00","parent_ids":["45ecd257c720d88b45eda51724fe940c213101bc"],"title":"create a new file","message":"create a new file","author_name":"Firstname Lastname","author_email":"author@example.com","authored_date":"2023-12-08T08:30:50.000+00:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2023-12-08T08:30:50.000+00:00","web_url":"http://192.168.139.140:30323/root/demo/-/commit/750d27ecbbe9a66b6e73598bcd2414bbf8716f8d","stats":{"additions":0,"deletions":0,"total":0},"status":null,"project_id":115,"last_pipeline":null}
-------------------
>>>>>
{"id":"ac6a415027d94f110d0fc02748a66f558b7542b8","short_id":"ac6a4150","created_at":"2023-12-08T08:30:51.000+00:00","parent_ids":["d033be4619f48c687e058ad26c11826bda238f0f"],"title":"create a new file","message":"create a new file","author_name":"Firstname Lastname","author_email":"author@example.com","authored_date":"2023-12-08T08:30:51.000+00:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2023-12-08T08:30:51.000+00:00","web_url":"http://192.168.139.140:30323/root/demo/-/commit/ac6a415027d94f110d0fc02748a66f558b7542b8","stats":{"additions":1,"deletions":1,"total":2},"status":null,"project_id":115,"last_pipeline":null}
<<<<<
{"message":"You can only create or edit files when you are on a branch"}
-------------------
>>>>>

What is the current bug behavior?

create commit api return error "You can only create or edit files when you are on a branch" occasionally when the startBrach is just created

What is the expected correct behavior?

api should return 201

Results of GitLab environment info

gitlab: 13.12.15

Additional information

In code implementation, we have many such usage scenarios. Is there any way to workaround ?

Edited by jtcheng