question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add flag to force branch creation

See original GitHub issue

The use case I’m trying to address is to either create a branch and switch to it or just switch to it if it already exists. It’s the functional equivalent of:

await git.branch({ ...repo, ref: 'branchname', checkout: true })
  .catch((e) => {
    if (e.code === git.E.RefExistsError && e.data.noun === 'branch') {
      return git.checkout({ ...repo, ref: 'branchname' })
    }
    throw e
  })

However, that’s not idiomatic. It also requires some work to avoid calling checkout with side effects (the creation of a remote tracking branch).

What I’d like to be able to do is force create the branch:

await git.branch({ ...repo, ref: 'branchname', force: true })

which is the same as git branch -f branchname.

which I can then combine with the checkout option to checkout the branch at the same time:

await git.branch({ ...repo, ref: 'branchname', checkout: true, force: true })

which is the same as git checkout -B branchname.

The force flag would thus allow isomorphic-git to cover both use cases.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
wmhiltoncommented, Jan 1, 2019

Hmm. I don’t like that solution, because I’ve had bad experiences creating optional arguments the default to true (like this reset argument). Edit: although that’s easy to fix, by naming the argument noReset *shrug*

It also seems complicated. Every time you add a boolean option, you double the complexity of a function. If you think about it, a single function with 3 boolean options is equivalent to 8 functions. (Or one switch statement with 2^3 cases.) Out of all 8 combinations of checkout, force, and reset - how many behaviors do we actually care about?

Side note: I made this explicit one time in calculateBasicAuthUsernamePasswordPair and somebody tweeted it as exceptionally clever. Edit: to this day, I’m not sure whether that function is code golf or extremely readable.

Let’s sit on this for a bit and see if we can come up with a simpler solution.

1reaction
mojavelinuxcommented, Dec 11, 2018

Wouldn’t force forcefully overwrite the branch to point at HEAD?

Oops, you’re absolutely right!

Read more comments on GitHub >

github_iconTop Results From Across the Web

github - Git branch editing flag
Is there an oportunity to put some sort of flag like "USERNAME is editing this branch" to git's branch? Such feature can help...
Read more >
Git - git-branch Documentation
Shortcut for --delete --force . --create-reflog. Create the branch's reflog. This activates recording of all changes made to the branch ref, enabling use...
Read more >
Force Push in Git - Everything You Need to Know
You can use the --force flag (or -f for short). This can look like an easy workaround when the git push command does...
Read more >
Git Branch | Atlassian Git Tutorial
The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked...
Read more >
Introduction to Git rebase and force-push - GitLab Docs
If you added changes to my-feature-branch after creating the backup branch, ... To force an update, pass the flag --force or -f to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found