Cannot create file in empty repo via API

Summary

It doesn't seem to be possible to commit a file to an empty repository. I attempted to do this via two different ways:

  1. Create Repository Branch
  2. Commit File

Steps to reproduce

  1. Create a new project.
  2. If needed, create a private token to interact with the API
  3. Try to create the master branch: curl --request POST --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects/5/repository/branches?branch_name=master&ref=master"
  4. Try to Commit a file: curl --request POST --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' 'https://gitlab.example.com/api/v3/projects/13083/repository/files?file_path=app/project.rb&branch_name=master&author_email=author%40example.com&author_name=Firstname%20Lastname&content=some%20content&commit_message=create%20a%20new%20file'

Expected behavior

I didn't expect 3 to actual work since there is no references to create the branch from. This potentially could've been alleviated if there was a way to create an orphan branch.

I would've expected 4 to create a master branch for me just like any git client would.

Actual behavior

Neither command worked, and I couldn't find a way to make a repo's first commit through the API. There's a handful of files that would be nice to automatically commit to every repo.

Relevant logs and/or screenshots

3: Response: { message: 'Invalid reference name' } 4: { message: 'You are not allowed to push into this branch' }

Output of checks

Results of GitLab application Check

$ sudo gitlab-rake gitlab:check SANITIZE=true
Checking GitLab Shell ...

GitLab Shell version >= 3.6.6 ? ... OK (3.6.6)
Repo base directory exists?
default... yes
Repo storage directories are symlinks?
default... no
Repo paths owned by git:git?
default... no
  User id for git: 998. Groupd id for git: 998
  Try fixing it:
  sudo chown -R git:git /var/opt/gitlab/git-data/repositories
  For more information see:
  doc/install/installation.md in section "GitLab Shell"
  Please fix the error above and rerun the checks.
Repo paths access is drwxrws---?
default... yes
hooks directories in repos are links: ...
6/1 ... ok
6/2 ... repository is empty
6/3 ... ok
6/5 ... ok
6/6 ... ok
6/9 ... repository is empty
6/11 ... repository is empty
6/12 ... ok
6/13 ... repository is empty
6/14 ... ok
6/15 ... ok
6/16 ... ok
Running /opt/gitlab/embedded/service/gitlab-shell/bin/check
Check GitLab API access: OK
Access to /var/opt/gitlab/.ssh/authorized_keys: OK
Send ping to redis server: OK
gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes
Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Checking Reply by email ...

Reply by email is disabled in config/gitlab.yml

Checking Reply by email ... Finished

Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Git configured with autocrlf=input? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory setup correctly? ... no
  Try fixing it:
  sudo chown -R git /var/opt/gitlab/gitlab-rails/uploads
  sudo find /var/opt/gitlab/gitlab-rails/uploads -type f -exec chmod 0644 {} \;
  sudo find /var/opt/gitlab/gitlab-rails/uploads -type d -not -path /var/opt/gitlab/gitlab-rails/uploads -exec chmod 0700 {} \;
  For more information see:
  doc/install/installation.md in section "GitLab"
  Please fix the error above and rerun the checks.
Init script exists? ... skipped (omnibus-gitlab has no init script)
Init script up-to-date? ... skipped (omnibus-gitlab has no init script)
projects have namespace: ...
6/1 ... yes
6/2 ... yes
6/3 ... yes
6/5 ... yes
6/6 ... yes
6/9 ... yes
6/11 ... yes
6/12 ... yes
6/13 ... yes
6/14 ... yes
6/15 ... yes
6/16 ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.1.0 ? ... yes (2.3.1)
Your git bin path is "/opt/gitlab/embedded/bin/git"
Git version >= 2.7.3 ? ... yes (2.7.4)
Active users: 8

Checking GitLab ... Finished

Results of GitLab environment info

$ sudo gitlab-rake gitlab:env:info

System information
System:		Ubuntu 14.04
Current User:	git
Using RVM:	no
Ruby Version:	2.3.1p112
Gem Version:	2.6.6
Bundler Version:1.13.5
Rake Version:	10.5.0
Sidekiq Version:4.2.1

GitLab information
Version:	8.13.3
Revision:	8d79ab3
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	postgresql
URL:		https://repo.reusser.io
HTTP Clone URL:	https://repo.reusser.io/some-group/some-project.git
SSH Clone URL:	git@repo.reusser.io:some-group/some-project.git
Using LDAP:	no
Using Omniauth:	no

GitLab Shell
Version:	3.6.6
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
Hooks:		/opt/gitlab/embedded/service/gitlab-shell/hooks/
Git:		/opt/gitlab/embedded/bin/git

Possible fixes

Require branch_name to equal 'master' in step 4 and commit the file like you normally would on an empty repo.

I'm not entirely sure if this would fix it, but perhaps moving changing the following

    def validate
      allowed = ::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(@target_branch)
      unless allowed
        raise_error("You are not allowed to push into this branch")
      end
      unless project.empty_repo?
        unless @source_project.repository.branch_names.include?(@source_branch)
          raise_error('You can only create or edit files when you are on a branch')
        end
        if different_branch?
          if repository.branch_names.include?(@target_branch)
            raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes')
          end
        end
      end
    end

to something like:

    def validate
      allowed = ::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(@target_branch)
      unless allowed || project.empty_repo?
        raise_error("You are not allowed to push into this branch")
      end
      unless project.empty_repo?
        unless @source_project.repository.branch_names.include?(@source_branch)
          raise_error('You can only create or edit files when you are on a branch')
        end
        if different_branch?
          if repository.branch_names.include?(@target_branch)
            raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes')
          end
        end
      end
    end