[#201] Switch the default branch to 'main' (#202)

* [#201] Switch the default branch to 'main'

Resolves #201

* Update src/Hit/Git/Fresh.hs

Co-authored-by: hint-man[bot] <44720633+hint-man[bot]@users.noreply.github.com>

* Update src/Hit/Git/Branch.hs

Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>

* Use main at CI

Co-authored-by: hint-man[bot] <44720633+hint-man[bot]@users.noreply.github.com>
Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>
This commit is contained in:
Veronika Romashkina 2020-11-06 13:57:38 +00:00 committed by GitHub
parent d67b274109
commit 880efa2e11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 34 deletions

View File

@ -2,10 +2,10 @@ name: CI
on:
# Trigger the workflow on push or pull request,
# but only for the master branch
# but only for the main branch
push:
branches:
- master
- main
pull_request:
jobs:
@ -20,7 +20,7 @@ jobs:
steps:
- uses: actions/checkout@v2
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/main'
- name: Setup Haskell
uses: actions/setup-haskell@v1.1.3
@ -57,7 +57,7 @@ jobs:
steps:
- uses: actions/checkout@v2
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/main'
- name: Run HLint
run: |

View File

@ -69,6 +69,9 @@ The changelog is available [on GitHub][2].
* [#178](https://github.com/kowainik/hit-on/issues/178):
Add ability to add newly created issues to the specified milestone with the
`-m|--current-milestone` or `--milestone ID` during the `hit new` command.
* [#201](https://github.com/kowainik/hit-on/issues/201):
Do not assume the main branch of the repo. Take in from the git command
instead.
### 0.1.0.0 — Aug 3, 2019

View File

@ -142,7 +142,7 @@ git config --global user.login <your_login>
| issue | Show the information about the issue |
| push | Push the current branch |
| sync | Sync local branch with its remote |
| resolve | Switch to master, sync and delete the branch |
| resolve | Switch to the main branch, sync and delete the branch |
| clear | Remove all local changes permanently |
| current | Show info about current branch and issue (if applicable) |
| status | Show current branch and beautiful stats with COMMIT_HASH (by default HEAD) |
@ -154,7 +154,7 @@ git config --global user.login <your_login>
The best way to demonstrate the power of the `hit` tool on a day-to-day basis with our workflow is to go through the entire workflow step by step, solving an ordinary problem of the typical [`git`][git] user.
> Here we assume that you work with `origin` remote with the main branch set to `master`.
> Here we assume that you work with `origin` remote.
### hit clone
@ -185,7 +185,7 @@ hit clone owner-name/project-name
When you want to start working on a new issue, you usually want to make sure you're using the latest version of your project. As a `git` user you may use the following commands:
```shell
git checkout master
git checkout main
git pull --rebase --prune
```
@ -311,13 +311,13 @@ hit sync
### hit fresh
While you were waiting for the second round of reviews, another pull request was merged to the `master` branch. Now you need to apply the new `master` changes to your local branch.
While you were waiting for the second round of reviews, another pull request was merged to the `main` branch. Now you need to apply the new `main` changes to your local branch.
With `git` you can do the following:
```shell
git fetch origin master
git rebase origin/master
git fetch origin main
git rebase origin/main
```
Again, with `hit` you can do better:
@ -368,7 +368,7 @@ Hooray, your PR just got merged! It's time to clean your local repository and st
With `git` you would do the following:
```shell
git checkout master
git checkout main
git pull --rebase --prune
git branch -D my-login/42-short-desc
```

View File

@ -143,7 +143,7 @@ hitP = subparser
<> com "issue" issueP "Show the information about the issue"
<> com "push" pushP "Push the current branch"
<> com "sync" syncP "Sync local branch with its remote"
<> com "resolve" resolveP "Switch to master, sync and delete the branch"
<> com "resolve" resolveP "Switch to the main branch, sync and delete the branch"
<> com "clear" clearP "Remove all local changes permanently"
<> com "current" currentP "Show info about current branch and issue (if applicable)"
<> com "status" statusP "Show current branch and beautiful stats with COMMIT_HASH (by default HEAD)"

View File

@ -30,7 +30,7 @@ import Shellmet (($?))
import Hit.Core (Milestone, NewOptions (..), newOptionsWithName)
import Hit.Formatting (stripRfc)
import Hit.Git.Common (getCurrentBranch, getUsername)
import Hit.Git.Common (getCurrentBranch, getMainBranch, getUsername)
import Hit.Issue (assignIssue, createIssue, fetchIssue, getAllIssues, getMilestoneId, meToUsername,
mkIssueId, printIssues)
@ -60,15 +60,16 @@ runNew NewOptions{..} = do
{- | @hit rename@ command.
Renames the current (non-master) branch to the given name or issue with the
Renames the current (non-main) branch to the given name or issue with the
username prefix.
If the current branch is master, it just creates a new branch as in 'runNew'.
If the current branch is the main one, it just creates a new branch as in 'runNew'.
-}
runRename :: Text -> IO ()
runRename issueOrName = do
curBranch <- getCurrentBranch
if curBranch == "master"
mainBranch <- getMainBranch
if curBranch == mainBranch
then runNew $ newOptionsWithName issueOrName
else do
newBranch <- mkBranchName False Nothing issueOrName

View File

@ -10,9 +10,11 @@ Helper functions used by different Hit Commands.
-}
module Hit.Git.Common
( nameOrMaster
( branchOrMain
, getUsername
, getMainBranch
, getCurrentBranch
, whenOnMainBranch
, issueFromBranch
, withDeletedFiles
, withUntrackedFiles
@ -26,8 +28,11 @@ import Shellmet (($|))
import qualified Data.Text as T
nameOrMaster :: Maybe Text -> Text
nameOrMaster = fromMaybe "master"
-- | If no branch provided returns the main branch of the repo.
branchOrMain :: Maybe Text -> IO Text
branchOrMain = \case
Just branch -> pure branch
Nothing -> getMainBranch
-- | Get current user name from the local global git config.
getUsername :: IO Text
@ -41,6 +46,20 @@ getUsername = do
getCurrentBranch :: IO Text
getCurrentBranch = "git" $| ["rev-parse", "--abbrev-ref", "HEAD"]
-- | Get the name of the default main branch.
getMainBranch :: IO Text
getMainBranch = do
br <- "git" $| ["rev-parse", "--abbrev-ref", "origin/HEAD"]
"basename" $| [br]
-- | Run the action if the current branch is the main branch of the repo.
whenOnMainBranch :: IO () -> IO ()
whenOnMainBranch act = do
mainBranch <- getMainBranch
curBranch <- getCurrentBranch
when (mainBranch == curBranch) act
{- | Extracts issue number from the branch in form like:
@

View File

@ -17,13 +17,14 @@ import Colourista (errorMessage, infoMessage, warningMessage)
import Shellmet (($?))
import System.IO (hFlush)
import Hit.Git.Common (nameOrMaster)
import Hit.Git.Common (branchOrMain)
import Hit.Prompt (Answer (..), arrow, prompt, yesOrNoText)
-- | @hit fresh@ command.
runFresh :: Maybe Text -> IO ()
runFresh (nameOrMaster -> branch) = do
runFresh mBranch = do
branch <- branchOrMain mBranch
"git" ["fetch", "origin", branch]
isRebase <- (True <$ "git" ["rebase", "origin/" <> branch]) $? pure False
unless isRebase $ do

View File

@ -15,11 +15,12 @@ module Hit.Git.Hop
import Shellmet ()
import Hit.Git.Common (nameOrMaster)
import Hit.Git.Common (branchOrMain)
-- | @hit hop@ command.
runHop :: Maybe Text -> IO ()
runHop (nameOrMaster -> branch) = do
runHop mBranch = do
branch <- branchOrMain mBranch
"git" ["checkout", branch]
"git" ["pull", "--rebase", "--prune"]

View File

@ -23,7 +23,7 @@ import GitHub.Request (executeRequest)
import Hit.Core (CommitOptions (..), ForceFlag (..), newOptionsWithName)
import Hit.Git.Branch (runNew)
import Hit.Git.Commit (runCommit)
import Hit.Git.Common (getCurrentBranch, getUsername, issueFromBranch)
import Hit.Git.Common (getCurrentBranch, getUsername, issueFromBranch, whenOnMainBranch)
import Hit.GitHub (withAuthOwnerRepo)
import Hit.Hub (withHub)
import Hit.Issue (fetchIssue, mkIssueId)
@ -39,7 +39,7 @@ It requires @hub@ tool to be installed.
-}
runPr :: Bool -> IO ()
runPr isDraft = do
whenM ((== "master") <$> getCurrentBranch) $ runNew $ newOptionsWithName "patch"
whenOnMainBranch $ runNew $ newOptionsWithName "patch"
curBranch <- getCurrentBranch
-- check if the open PR with head @owner:branch_name@ already exist
res <- withAuthOwnerRepo $ \auth owner repo -> do

View File

@ -15,13 +15,14 @@ module Hit.Git.Resolve
import Shellmet ()
import Hit.Git.Common (getCurrentBranch, nameOrMaster)
import Hit.Git.Common (branchOrMain, getCurrentBranch)
import Hit.Git.Hop (runHop)
-- | @hit resolve@ command.
runResolve :: Maybe Text -> IO ()
runResolve (nameOrMaster -> master)= do
runResolve mBranch = do
branchToHop <- branchOrMain mBranch
curBranch <- getCurrentBranch
runHop $ Just master
when (curBranch /= master) $ "git" ["branch", "-D", curBranch]
runHop $ Just branchToHop
when (curBranch /= branchToHop) $ "git" ["branch", "-D", curBranch]

View File

@ -9,7 +9,8 @@ Portability : Portable
Everything related to the @hit wip@ command. The command behaves
in the following way:
* If the current branch is @master@, switch to @username/wip@ branch
* If the current branch is the default one (e.g. `main`), switch to
@username/wip@ branch
* Create a commit with the @WIP@ message
* Push it
-}
@ -21,14 +22,13 @@ module Hit.Git.Wip
import Hit.Core (CommitOptions (..), ForceFlag (..), newOptionsWithName)
import Hit.Git.Branch (runNew)
import Hit.Git.Commit (runCommit)
import Hit.Git.Common (getCurrentBranch)
import Hit.Git.Common (whenOnMainBranch)
-- | @hit wip@ command: create and push @WIP@ commit.
runWip :: IO ()
runWip = do
curBranch <- getCurrentBranch
when (curBranch == "master") $ runNew $ newOptionsWithName "wip"
whenOnMainBranch $ runNew $ newOptionsWithName "wip"
runCommit CommitOptions
{ coName = Just "WIP"
, coNoIssueNumber = True

View File

@ -3,16 +3,18 @@ module Test.Hit.Names
) where
import GitHub (IssueNumber (..))
import Test.Hspec (Expectation, Spec, describe, it, shouldBe)
import Test.Hspec (Expectation, Spec, describe, it, runIO, shouldBe)
import Hit.Git.Branch (assignAndDisplayBranchDescription, mkBranchDescription)
import Hit.Git.Commit (toCommitMessage)
import Hit.Git.Common (getMainBranch)
namesSpec :: Spec
namesSpec = describe "Names for branches and commit messages" $ do
branchNamesSpec
commitMessagesSpec
mainBranchSpec
branchNamesSpec :: Spec
branchNamesSpec = describe "Branch naming" $ do
@ -52,3 +54,9 @@ commitMessagesSpec = describe "Commit messages naming" $ do
toCommitMessage False "Commit message" (Just 42) `shouldBe` "Commit message"
it "without issue" $
toCommitMessage True "Commit message" Nothing `shouldBe` "Commit message"
mainBranchSpec :: Spec
mainBranchSpec = describe "Main branch of the repo" $ do
branch <- runIO getMainBranch
it "should be named 'main'" $
branch `shouldBe` "main"