Skip to content

Commit db735f4

Browse files
committed
Remove remote branches only for origin after accepting work
A user may accept work from forked remotes. In this case, it's possible that a remote fork provides only pull permissions. And, in order to prevent errors, the removal is enabled only for `origin` - is a user accepts work, it has a write permissions to `origin`. #171
1 parent 47ea024 commit db735f4

3 files changed

Lines changed: 35 additions & 21 deletions

File tree

docs/commands.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ There are commands used in various situations such as
2929

3030
operate a flow of work management
3131
obtain-work Checkouts a remote branch matching by a name.
32-
accept-work Applies a branch on top of upstream branch.
32+
accept-work Applies a branch on top of `master` branch.
3333

3434
release new versions
3535
show-release-notes Prints a release log between two references.
@@ -51,10 +51,12 @@ usage: git elegant accept-work <remote branch>
5151
```
5252

5353
Checkouts given branch using `git elegant obtain-work` into a temporary one.
54-
Then, it makes a rebase of the latest version of default upstream branch with
55-
current changes. The final index merges using fast-forward strategy into the
56-
default local branch and pushes into the default upstream branch. After a
57-
successful push, the given and temporary branches are removed.
54+
Then, it makes a rebase of the latest version of default upstream branch
55+
(`master`) with current changes. The final prepared index merges using
56+
fast-forward strategy into the default local branch and pushes into the
57+
default upstream branch (`origin/master`). After a successful push, the
58+
temporary branch is removed as well as given branch if it locates in `origin`
59+
remote.
5860

5961
Prior to the execution, a current state is saved (a branch with modifications).
6062
After the successful accepting a work, the state will be restored. In the case

libexec/git-elegant-accept-work

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -e
33

44
command-purpose() {
55
cat <<MESSAGE
6-
Applies a branch on top of upstream branch.
6+
Applies a branch on top of \`master\` branch.
77
MESSAGE
88
}
99

@@ -16,10 +16,12 @@ MESSAGE
1616
command-description() {
1717
cat<<MESSAGE
1818
Checkouts given branch using \`git elegant obtain-work\` into a temporary one.
19-
Then, it makes a rebase of the latest version of default upstream branch with
20-
current changes. The final index merges using fast-forward strategy into the
21-
default local branch and pushes into the default upstream branch. After a
22-
successful push, the given and temporary branches are removed.
19+
Then, it makes a rebase of the latest version of default upstream branch
20+
(\`master\`) with current changes. The final prepared index merges using
21+
fast-forward strategy into the default local branch and pushes into the
22+
default upstream branch (\`origin/master\`). After a successful push, the
23+
temporary branch is removed as well as given branch if it locates in \`origin\`
24+
remote.
2325
2426
Prior to the execution, a current state is saved (a branch with modifications).
2527
After the successful accepting a work, the state will be restored. In the case
@@ -57,7 +59,9 @@ MESSAGE
5759
git-verbose merge --ff-only ${WORK_BRANCH}
5860
git-verbose push ${REMOTE_NAME} ${MASTER}:${MASTER}
5961
git-verbose branch --delete --force ${WORK_BRANCH}
60-
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
62+
if [[ ${ACTUAL_REMOTE} =~ ^origin/ ]]; then
63+
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
64+
fi
6165
}
6266

6367
default() {

tests/git-elegant-accept-work.bats

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ load addons-repo
66

77
setup() {
88
repo-new
9+
repo "git checkout -b __eg; git checkout -"
910
fake-pass "git elegant obtain-work test-feature __eg"
1011
fake-pass "git rebase origin/master"
1112
fake-pass "git fetch --all"
12-
fake-pass "git merge --ff-only __eg"
1313
fake-pass "git push origin master:master"
14-
fake-pass "git branch --delete --force __eg"
15-
fake-pass "git for-each-ref --format='%(upstream:short)' refs/heads/__eg}" "origin/test-feature"
16-
fake-pass "git push origin --delete test-feature"
1714
}
1815

1916
teardown() {
@@ -22,21 +19,32 @@ teardown() {
2219
}
2320

2421
@test "'accept-work': a work is accepted successfully for given remote branch" {
22+
fake-pass "git for-each-ref --format='%(upstream:short)' refs/heads/__eg}" "origin/test-feature"
23+
fake-fail "git push origin --delete test-feature"
2524
check git-elegant accept-work test-feature
26-
[[ "${status}" -eq 0 ]]
25+
[[ ${status} -eq 100 ]]
2726
}
2827

2928
@test "'accept-work': exit code is 45 when remote branch name isn't set" {
3029
check git-elegant accept-work
31-
[[ "${status}" -eq 45 ]]
30+
[[ ${status} -eq 45 ]]
3231
}
3332

3433
@test "'accept-work': save WIP prior accepting and restore after it" {
3534
repo "git checkout -b other"
3635
repo-non-staged-change "A new line..."
36+
fake-pass "git for-each-ref --format='%(upstream:short)' refs/heads/__eg}" "origin/test-feature"
37+
fake-pass "git push origin --delete test-feature"
38+
check git-elegant accept-work test-feature
39+
[[ ${status} -eq 0 ]]
40+
[[ ${lines[@]} =~ "git stash push" ]]
41+
[[ ${lines[@]} =~ "git stash pop" ]]
42+
[[ ${lines[@]} =~ "git checkout other" ]]
43+
}
44+
45+
@test "'accept-work': a remote branch is not removed when it is pulled from fork" {
46+
fake-pass "git for-each-ref --format='%(upstream:short)' refs/heads/__eg}" "origin-a/test-feature"
47+
fake-fail "git push origin --delete test-feature"
3748
check git-elegant accept-work test-feature
38-
[[ "${status}" -eq 0 ]]
39-
[[ "${lines[@]}" =~ "git stash push" ]]
40-
[[ "${lines[@]}" =~ "git stash pop" ]]
41-
[[ "${lines[@]}" =~ "git checkout other" ]]
49+
[[ ${status} -eq 0 ]]
4250
}

0 commit comments

Comments
 (0)