Skip to content

Commit

Permalink
Read multi slash branch names when deliver-work
Browse files Browse the repository at this point in the history
`git elegant deliver-work` incorrectly reads the name of the upstream
branch if it contains at least one slash like `feature/123`. This is
because the used pattern matching logic doesn't expect remote branch
names like `origin/feature/123`.

In order to solve it, the new `transformation` plugin is introduced. It
provides functions that parse remote branch names correctly. Now, the
plugin handles branch translation (mapping) for `deliver-work` command
as well as for `accept-work` and `obtain-work` in order to prevent
possible errors in the future.

#255
  • Loading branch information
extsoft committed Apr 15, 2020
1 parent 97cc560 commit d758b41
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
5 changes: 0 additions & 5 deletions libexec/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ _error-if-empty() {
fi
}

branch-from-remote-reference() {
# usage: branch-from-remote-reference <full reference name>
echo ${1} | sed "s|^[a-zA-Z0-9_-]*/||g"
}

remove-file() {
[[ -f ${1} ]] && rm ${1}
}
Expand Down
3 changes: 2 additions & 1 deletion libexec/git-elegant-accept-work
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ MESSAGE
if are-there-remotes; then
git-verbose push ${REMOTE_NAME} ${MASTER}:${MASTER}
if [[ ${actual_remote} =~ ^origin/ ]]; then
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${actual_remote})
source ${BINS}/plugins/transformation
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-branch ${actual_remote})
fi
fi
}
Expand Down
13 changes: 7 additions & 6 deletions libexec/git-elegant-deliver-work
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,26 @@ MESSAGE

--deliver-work-logic() {
source ${BINS}/plugins/state
source ${BINS}/plugins/transformation
if is-there-active-rebase; then
git-verbose rebase --continue
else
git-verbose fetch
git-verbose rebase ${RMASTER}
fi
local upstream=$(git branch --list --format "%(upstream:short)" ${1})
local remote_branch=${upstream/*\//}
local remote=${upstream/\/*/}
local branch=$(branch-from-remote-branch ${upstream})
local remote=$(remote-from-remote-branch ${upstream})
if [[ -n "${2}" ]]; then
remote_branch=${2}
branch=${2}
fi
if [[ -z "${remote_branch}" ]]; then
remote_branch=${1}
if [[ -z "${branch}" ]]; then
branch=${1}
fi
if [[ -z "${remote}" ]]; then
remote=${REMOTE_NAME}
fi
git-verbose-op --open-urls-if-possible push --set-upstream --force ${remote} ${1}:${remote_branch}
git-verbose-op --open-urls-if-possible push --set-upstream --force ${remote} ${1}:${branch}
}

default() {
Expand Down
3 changes: 2 additions & 1 deletion libexec/git-elegant-obtain-work
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ default() {
local REMOTE=${REMOTE_BRANCHES[0]}
local LOCAL=${2}
if [[ -z ${LOCAL} ]]; then
LOCAL=$(branch-from-remote-reference ${REMOTE})
source ${BINS}/plugins/transformation
LOCAL=$(branch-from-remote-branch ${REMOTE})
fi
git-verbose checkout -B ${LOCAL} ${REMOTE}
else
Expand Down
15 changes: 15 additions & 0 deletions libexec/plugins/transformation
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# The plugin holds functions that convert one object into another.
# The most part of functions manipulates with strings.

branch-from-remote-branch() {
# usage : echo $(branch-from-remote-branch origin/feature/a)
# result: feature/a
echo ${1#*/}
}

remote-from-remote-branch() {
# usage : echo $(remote-from-remote-branch origin/feature/a)
# result: origin
echo ${1%%/*}
}
10 changes: 5 additions & 5 deletions tests/git-elegant-deliver-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ teardown() {
}

@test "'deliver-work': use existing upstream branch if it is available" {
repo "git checkout -b some/remote"
repo "git checkout -b feature1"
repo "git branch --set-upstream-to some/remote"
fake-pass "git push --set-upstream --force some feature1:remote"
repo "git checkout -b some-remote/feature/123"
repo "git checkout -b 123"
repo "git branch --set-upstream-to some-remote/feature/123"
fake-pass "git push --set-upstream --force some-remote 123:feature/123"
check git-elegant deliver-work
[[ ${status} -eq 0 ]]
[[ ${lines[@]} =~ "git push --set-upstream --force some feature1:remote" ]]
[[ ${lines[@]} =~ "git push --set-upstream --force some-remote 123:feature/123" ]]
}

@test "'deliver-work': open URls if they are present in the push output" {
Expand Down

0 comments on commit d758b41

Please sign in to comment.