Skip to content

Commit d758b41

Browse files
committed
Read multi slash branch names when deliver-work
`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
1 parent 97cc560 commit d758b41

6 files changed

+31
-18
lines changed

libexec/git-elegant

-5
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ _error-if-empty() {
4040
fi
4141
}
4242

43-
branch-from-remote-reference() {
44-
# usage: branch-from-remote-reference <full reference name>
45-
echo ${1} | sed "s|^[a-zA-Z0-9_-]*/||g"
46-
}
47-
4843
remove-file() {
4944
[[ -f ${1} ]] && rm ${1}
5045
}

libexec/git-elegant-accept-work

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ MESSAGE
7676
if are-there-remotes; then
7777
git-verbose push ${REMOTE_NAME} ${MASTER}:${MASTER}
7878
if [[ ${actual_remote} =~ ^origin/ ]]; then
79-
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${actual_remote})
79+
source ${BINS}/plugins/transformation
80+
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-branch ${actual_remote})
8081
fi
8182
fi
8283
}

libexec/git-elegant-deliver-work

+7-6
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,26 @@ MESSAGE
5252

5353
--deliver-work-logic() {
5454
source ${BINS}/plugins/state
55+
source ${BINS}/plugins/transformation
5556
if is-there-active-rebase; then
5657
git-verbose rebase --continue
5758
else
5859
git-verbose fetch
5960
git-verbose rebase ${RMASTER}
6061
fi
6162
local upstream=$(git branch --list --format "%(upstream:short)" ${1})
62-
local remote_branch=${upstream/*\//}
63-
local remote=${upstream/\/*/}
63+
local branch=$(branch-from-remote-branch ${upstream})
64+
local remote=$(remote-from-remote-branch ${upstream})
6465
if [[ -n "${2}" ]]; then
65-
remote_branch=${2}
66+
branch=${2}
6667
fi
67-
if [[ -z "${remote_branch}" ]]; then
68-
remote_branch=${1}
68+
if [[ -z "${branch}" ]]; then
69+
branch=${1}
6970
fi
7071
if [[ -z "${remote}" ]]; then
7172
remote=${REMOTE_NAME}
7273
fi
73-
git-verbose-op --open-urls-if-possible push --set-upstream --force ${remote} ${1}:${remote_branch}
74+
git-verbose-op --open-urls-if-possible push --set-upstream --force ${remote} ${1}:${branch}
7475
}
7576

7677
default() {

libexec/git-elegant-obtain-work

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ default() {
4848
local REMOTE=${REMOTE_BRANCHES[0]}
4949
local LOCAL=${2}
5050
if [[ -z ${LOCAL} ]]; then
51-
LOCAL=$(branch-from-remote-reference ${REMOTE})
51+
source ${BINS}/plugins/transformation
52+
LOCAL=$(branch-from-remote-branch ${REMOTE})
5253
fi
5354
git-verbose checkout -B ${LOCAL} ${REMOTE}
5455
else

libexec/plugins/transformation

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
# The plugin holds functions that convert one object into another.
3+
# The most part of functions manipulates with strings.
4+
5+
branch-from-remote-branch() {
6+
# usage : echo $(branch-from-remote-branch origin/feature/a)
7+
# result: feature/a
8+
echo ${1#*/}
9+
}
10+
11+
remote-from-remote-branch() {
12+
# usage : echo $(remote-from-remote-branch origin/feature/a)
13+
# result: origin
14+
echo ${1%%/*}
15+
}

tests/git-elegant-deliver-work.bats

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ teardown() {
4949
}
5050

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

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

0 commit comments

Comments
 (0)