Skip to content

Commit

Permalink
Add accept-work command
Browse files Browse the repository at this point in the history
The command aims to merge a work branch in `master` one. It's similar to
what happen if you accept and merge a PR.

#137
  • Loading branch information
extsoft committed Jul 21, 2019
1 parent c26e051 commit aa2fc0b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 13 deletions.
5 changes: 5 additions & 0 deletions completions/git-elegant.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ _git_elegant() {
check|echeck)
COMPREPLY=($(compgen -W '"--all" "--unstaged" "--staged"' -- ${cur}) )
return 0 ;;
accept-work)
COMPREPLY=(
$(compgen -W "$(git branch --remotes --list)" -- ${cur})
)
return 0 ;;
*)
return 0 ;;
esac
Expand Down
21 changes: 21 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
`git elegant <command>` where `<command>` is one of

- `start-work`
- `accept-work`
- `pull`
- `push`
- `push-after-rebase`
Expand Down Expand Up @@ -29,6 +30,26 @@ git pull
git checkout -b <branch-name>
```

# `accept-work`
Accepts proposed work (remote work branch) on top of the fresh history of remote `master` (using
`rebase`) and publishes work to `origin/master`. Also, it removes the remote work branch in case of
successful work acceptance.

```bash
usage: git elegant accept-work <remote-branch>
```
A sequence of original `git` commands:
```bash
git fetch --all --tags --prune
git checkout -b eg origin/master
git rebase --merge --strategy ff-only <remote-branch-name>
git checkout master
git merge --ff-only eg
git push origin master:master
git branch -d eg
git push origin --delete <remote-branch-name>
```

# `pull`
Downloads new updates for a local branch.

Expand Down
3 changes: 2 additions & 1 deletion libexec/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ __mmn() {
}

MASTER="master"
RMASTER="origin/master"
REMOTE_NAME="origin"
RMASTER="${REMOTE_NAME}/master"

_error-if-empty() {
# _error-if-empty <a value to check> <error message>
Expand Down
22 changes: 22 additions & 0 deletions libexec/git-elegant-accept-work
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e


default() {
_error-if-empty "${1}" "Please give a name of remote branch (like 'origin/123')."
local WORK_BRANCH="eg"
boxtee git fetch --all --tags --prune
boxtee git checkout -b ${WORK_BRANCH} ${RMASTER}
boxtee git rebase --merge --strategy ff-only ${1}
# @todo #137 Alternative flows of `accept-work`
# If this command is executed with `${WORK_BRANCH}` and
# 1. `git rebase` is in progress, it has to run `git rebase --continue` prior working with
# `${MASTER}` branch.
# 2. `git rebase` is completed (history of `${WORK_BRANCH}` and `${RMASTER}` are different),
# just work with `${MASTER}` branch.
boxtee git checkout ${MASTER}
boxtee git merge --ff-only eg
boxtee git push ${REMOTE_NAME} ${MASTER}:${MASTER}
boxtee git branch --delete ${WORK_BRANCH}
boxtee git push ${REMOTE_NAME} --delete $(echo "${1}" | sed -e "s|${REMOTE_NAME}/||")
}
1 change: 1 addition & 0 deletions libexec/git-elegant-commands
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -e

default() {
echo "start-work"
echo "accept-work"
echo "pull"
echo "push"
echo "push-after-rebase"
Expand Down
31 changes: 31 additions & 0 deletions tests/git-elegant-accept-work.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats -ex

load addons-common
load addons-fake

teardown() {
clean-fake
}

@test "'accept-work': a work is accepted successfully for given remote branch" {
fake-pass git "fetch --all --tags --prune"
fake-pass git "checkout -b eg origin/master"
fake-pass git "rebase --merge --strategy ff-only origin/work"
fake-pass git "checkout master"
fake-pass git "merge --ff-only eg"
fake-pass git "push origin master:master"
fake-pass git "branch --delete eg"
fake-pass git "push origin --delete work"
check git-elegant accept-work origin/work
[ "$status" -eq 0 ]
}

@test "'accept-work': exit code is 255 when remote branch name isn't set" {
check git-elegant accept-work
[ "$status" -eq 255 ]
}

@test "'accept-work': print error message when remote branch name isn't set" {
check git-elegant accept-work
[[ "${lines[0]}" =~ "Please give a name of remote branch (like 'origin/123')." ]]
}
25 changes: 13 additions & 12 deletions tests/git-elegant-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ teardown() {
@test "'commands': print all available commands" {
check git-elegant commands
[ "${lines[0]}" = "start-work" ]
[ "${lines[1]}" = "pull" ]
[ "${lines[2]}" = "push" ]
[ "${lines[3]}" = "push-after-rebase" ]
[ "${lines[4]}" = "rebase" ]
[ "${lines[5]}" = "init" ]
[ "${lines[6]}" = "acquire-repository" ]
[ "${lines[7]}" = "add" ]
[ "${lines[8]}" = "clear-local" ]
[ "${lines[9]}" = "configure-repository" ]
[ "${lines[10]}" = "check" ]
[ "${lines[11]}" = "save" ]
[ ${#lines[@]} -eq 12 ]
[ "${lines[1]}" = "accept-work" ]
[ "${lines[2]}" = "pull" ]
[ "${lines[3]}" = "push" ]
[ "${lines[4]}" = "push-after-rebase" ]
[ "${lines[5]}" = "rebase" ]
[ "${lines[6]}" = "init" ]
[ "${lines[7]}" = "acquire-repository" ]
[ "${lines[8]}" = "add" ]
[ "${lines[9]}" = "clear-local" ]
[ "${lines[10]}" = "configure-repository" ]
[ "${lines[11]}" = "check" ]
[ "${lines[12]}" = "save" ]
[ ${#lines[@]} -eq 13 ]
}

@test "'commands': default exit code is 0" {
Expand Down

1 comment on commit aa2fc0b

@0pdd
Copy link

@0pdd 0pdd commented on aa2fc0b Jul 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 137-b582b6a4 discovered in libexec/git-elegant-accept-work and submitted as #146. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.