Skip to content

Commit

Permalink
Add prompt to push unpushed changes while creating a new PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Jul 18, 2023
1 parent e67350f commit b24a614
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
40 changes: 34 additions & 6 deletions src/FFI/Git.idr
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ pushNewBranch : Git => (remoteName : String) -> (branch : String) -> Promise ()
pushNewBranch @{G ptr} remoteName branch =
ignore . promiseIO $ prim__pushNewBranch ptr remoteName branch

%foreign git_ffi "push"
prim__push : Ptr GitRef
-> (onSuccess : String -> PrimIO ())
-> (onFailure : String -> PrimIO ())
-> PrimIO ()

export
push : Git => Promise ()
push @{G ptr} = ignore . promiseIO $ prim__push ptr

%foreign git_ffi "list_remotes"
prim__listRemotes : Ptr GitRef
-> (onSuccess : String -> PrimIO ())
Expand Down Expand Up @@ -89,12 +99,30 @@ prim__remoteTrackingBranch : Ptr GitRef

export
remoteTrackingBranch : Git => Promise (Maybe String)
remoteTrackingBranch @{G ptr} =
do str <- promiseIO $ prim__remoteTrackingBranch ptr
pure $
case strM str of
StrNil => Nothing
(StrCons x xs) => Just str
remoteTrackingBranch @{G ptr} = do
str <- promiseIO $ prim__remoteTrackingBranch ptr
pure $
case strM str of
StrNil => Nothing
(StrCons x xs) => Just str

%foreign git_ffi "unpushed_commits"
prim__unpushedCommits : Ptr GitRef
-> (onSuccess : String -> PrimIO ())
-> (onFailure : String -> PrimIO ())
-> PrimIO ()

||| Get the Git output for unpushed commits (multiple lines per
||| commit including the ref, author, and commit message). If there
||| are no unpushed commits, returns @Nothing@.
export
unpushedCommits : Git => Promise (Maybe String)
unpushedCommits @{G ptr} = do
str <- promiseIO $ prim__unpushedCommits ptr
pure $
case strM str of
StrNil => Nothing
(StrCons x xs) => Just str

%foreign git_ffi "user_email"
prim__userEmail : Ptr GitRef
Expand Down
18 changes: 15 additions & 3 deletions src/PullRequest.idr
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,21 @@ identifyOrCreatePR @{config} {isDraft} branch = do

createPR : Promise PullRequest
createPR = do
when (!remoteTrackingBranch == Nothing) $
do putStrLn "Creating a new remote branch..."
pushNewBranch (fromMaybe "origin" config.defaultRemote) branch
-- create a remote tracking branch if needed
whenNothing !remoteTrackingBranch $ do
putStrLn "Creating a new remote branch..."
pushNewBranch (fromMaybe "origin" config.defaultRemote) branch

-- ask if unpushed commits should be pushed
whenJust !unpushedCommits $ \unpushedString => do
putStrLn "The following commits have not been pushed:\n"
putStrLn unpushedString
putStrLn "\n"
pushUnpushedChanges <-
yesNoPrompt "Would you like to push these changes before creating a PR?"
when pushUnpushedChanges push

-- proceed to creating a PR
putStrLn "Creating a new PR for the current branch (\{branch})."
putStrLn "What branch are you merging into (ENTER for default: \{config.mainBranch})?"
baseBranchInput <- trim <$> getLine
Expand Down
7 changes: 7 additions & 0 deletions src/Util.idr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import Text.PrettyPrint.Prettyprinter.Render.Terminal

%default total

||| Run the given applicative when the input is @Nothing@.
||| The dual of @whenJust@.
export
whenNothing : Applicative f => Maybe a -> f () -> f ()
whenNothing Nothing x = x
whenNothing (Just _) _ = pure ()

||| Render with or without color based on configuration
export
renderString : Config => Doc AnsiStyle -> String
Expand Down
29 changes: 23 additions & 6 deletions support/js/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ const git_git = () =>
const idris__git_unpromisify = (promise, onSuccess, onFailure) =>
promise.then(r => onSuccess(r)(), e => onFailure(e)())

// trim a result (second argument) and pass it to the given callback (first argument).
const idris__git_trim = callback => value => callback(value.trim())

// current branch
// @Returns String
const git_current_branch = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('branch', '--show-current'),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

Expand All @@ -32,18 +35,25 @@ const git_push_new_branch = (git, remoteName, branch, onSuccess, onFailure) =>
onFailure
)

const git_push = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('push'),
r => onSuccess(''),
onFailure
)

// remote URI
const git_remote_uri = (git, remoteName, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('remote', 'get-url', remoteName),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

const git_list_remotes = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('remote'),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

Expand All @@ -56,21 +66,28 @@ const git_remote_tracking_branch = (git, onSuccess, onFailure) =>
.then(headRef =>
git.raw('for-each-ref', '--format', '%(upstream:short)', `${headRef.trim()}`),
onFailure),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

const git_unpushed_commits = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('log', '@{push}..'),
idris__git_trim(onSuccess),
onFailure
)

const git_user_email = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('config', '--get', 'user.email'),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

const git_root_dir = (git, onSuccess, onFailure) =>
idris__git_unpromisify(
git.raw('rev-parse', '--show-toplevel'),
r => onSuccess(r.trim()),
idris__git_trim(onSuccess),
onFailure
)

0 comments on commit b24a614

Please sign in to comment.