Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Change the behaviour of git_repository_is_empty to better align with expectations in a world where master isn't always the "initial branch" #6508

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

timrogers
Copy link
Contributor

@timrogers timrogers commented Feb 26, 2023

This pull request proposes a change to the definition of an "empty" repository used in git_repository_is_empty.

At present, an empty repository is one which:

  1. has just been initialized
  2. has no references apart from HEAD
  3. has a HEAD pointing to the unborn master branch or, if specified, the unborn branch specified in the init.defaultBranch configuration value

This pull request updates the definition of "empty", removing requirement 3.

Why? Since git_repository_is_empty was first implemented, there has been significant cultural change in the Git community towards using an "initial branch" other than master.

Because of this change, the current implementation of git_repository_is_empty considers repos to be not empty which an ordinary observer would consider to be empty.

Specifically, this happens where a repo is initialized with git init --initial-branch <initial_branch_which_is_not_master>.

An ordinary observer would consider such a repo to be empty, but currently, libgit2 considers it to be non-empty if the initial-branch is not master.

This is because, whilst git_repository_is_empty checks the init.defaultBranch to try to figure out the initial branch, git init --initial-branch doesn't set this value, as you can prove like this:

git config init.defaultBranch
# nothing is printed to STDOUT
mkdir test-repo
cd test-repo
git init --initial-branch main
git config init.defaultBranch
# nothing is printed to STDOUT, despite the `initial-branch` option

I'm not necessarily expecting this PR to be merged as is or ever - but I want to start a conversation about this, given that the current implementation is surprising and likely confusing.

See #6049.

… better align with expectations in a world where

`master` isn't always the "initial branch"

This pull request proposes a change to the definition of
an "empty" repository used in `git_repository_is_empty`.

At present, an empty is repository is one which:

1. has just been initialised
2. has no references apart from `HEAD`
3. has a `HEAD` pointing to the unborn `master` branch or,
  if specified, the unborn branch specified in the
  `init.defaultBranch` configuration value

This changes the definition of empty, removing requirement 3.

Why? Since `git_repository_is_empty` was first implemented,
there has been significant cultural change in the Git community
towards using an "initial branch" other than `master`.

Because of this change, the current implementation of
`git_repository_is_empty` considers repos to be *not empty* which
an ordinary observer would consider to be empty.

Specifically, this happens where a repo is initialized with
`git init --initial-branch`. An ordinary observer would consider
such a repo to be empty, but currently, libgit2 considers it to be
non-empty if the `initial-branch` is not `master`. This is because,
whilst `git_repository_is_empty` checks the `init.defaultBranch` to
try to figure out the initial branch, `git init --initial-branch`
doesn't set this value, as you can prove like this:

```bash
git config init.defaultBranch
mkdir test-repo
git init --initial-branch main
git config init.defaultBranch
```

I'm not necessarily expecting this PR to be merged as is or ever -
but I want to start a conversation about this, given that the
current implementation is surprising and likely confusing.

See libgit2#6049.
@timrogers timrogers marked this pull request as ready for review February 26, 2023 16:31
@timrogers
Copy link
Contributor Author

The tests aren't working yet, but happy to get them working if this is a proposal worth moving forward with.

@ethomson
Copy link
Member

Thanks @timrogers. I haven't read the code yet but agree that is_empty... doesn't make much sense as-is.

@ethomson
Copy link
Member

I'd be curious for @carlosmn's take since I suspect he's using it production (and I am not).

@timrogers
Copy link
Contributor Author

Thanks @timrogers. I haven't read the code yet but agree that is_empty... doesn't make much sense as-is.

Thanks! I'm conscious that breaking changes are a big deal, but this behaviour has caused some confusion for our team, and I'm sure we're not alone. I think we should do something to make this better - but I'd love the maintainers' thoughts on the right path.

@timrogers
Copy link
Contributor Author

@carlosmn 👋🏻 Just a reminder that it would be great if you could take a look at this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants