eficode-academy / git-katas

A set of exercises for deliberate Git Practice
MIT License
1.32k stars 860 forks source link

Make exercises compatible with defaultBranch != master #297

Closed JKrag closed 3 years ago

JKrag commented 3 years ago

Some users have set a global config to default to e.g. main instead of master, and this makes a lot of the setup scripts fail when they do a checkout master after creating a branch.

We need to come up with a clean solution for this.

Expanding:

Recent versions of Git (mine is 2.30.1 right now) have started posting the following warning when you run git init:

$ git init foo
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /Users/jankrag/......./foo/.git/

This means that a lot of users might now be setting their init.defaultBranch to something other than master and thus he current scripts will fail.

JKrag commented 3 years ago

Maybe we can work around this by actually setting init.defaultBranch to master locally in the shared make-setup.sh script. But I haven't tested yet, and I am a bit uncertain as to whether this works as we are talking about a init setting that obviously has to be set somewhere before we actually run git init, so we can't actually set in inside the individual exercise repos.

JKrag commented 3 years ago

I looked a step further, and it turns out that since git 2.28 (where the init.defaultBranch showed up), git init has also been expanded to support:

git init --initial-branch=[BRANCHNAME]

I guess this should make it rather easy to always create the repos with (currently) master and later probably main.

The decision to make that move is not in scope of this issue. It will probably depend on when Git decides to change the default. It will also require a thorough search/replace of al the exercise instructions.

JKrag commented 3 years ago

I realised that we can not currently use the --initial-branch feature as that would fail with older versions of Git, but I found another solution, by setting the init.defaultBranch config inline when calling git init.

I have now created a branch where this is fully implemented. https://github.com/eficode-academy/git-katas/tree/default-branch-handling

For all Shell scripts, it refers to a global DEFAULT_BRANCH variable in utils.sh- For the PS scripts, I didn't have an easy way to do that (and I am not experienced with PS), so on those it is currently hardcoded to master.

The only reason I have not yet opened a PR for this branch, is that it is building on top of the improve-testing branch that isn't merged yet.

JKrag commented 3 years ago

For technical reasons, I had to include this change in #299 to make it build. The actual commit regarding the defaultBranch changes can be found here: https://github.com/eficode-academy/git-katas/pull/299/commits/9f5ca7470c46d1f52eecf5e573dbab2f1fa3a502

So there will not be a separate PR for this change. Please go to #299 to comment if relevant.

JKrag commented 3 years ago

The change has been merged as part of #299 so I am now closing this issue.