Skip to content

Commit

Permalink
Add obtain-work command
Browse files Browse the repository at this point in the history
The command replaces the existing `pull` command. In addition to existing
logic, it allows
- checkouting of remote branches
- using pattern matching for branches allocation

`43` error code is introduced to handle logical constraints.

#10
  • Loading branch information
extsoft committed Aug 12, 2019
1 parent f2338a4 commit 8138793
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 43 deletions.
19 changes: 17 additions & 2 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,23 @@ git branch --delete --force __eg
git push origin --delete <remote-branch-name>
```

# `pull`
Downloads new updates for a local branch.
# `obtain-work`
Checkouts a local or remote branch matching against a pattern. It looks for local branches first, then for remotes ones.
If there are more then 1 matching branch, execution stops with a corresponding error message.

```bash
usage: git elegant obtain-work <pattern>
```

A sequence of original `git` commands:
```bash
# if a local branch matches a pattern
git checkout <branch name>
git pull
# if no local matches, seek through remotes
git fetch --all
git checkout <branch name>
```

# `clear-local`
Removes all local branches which don't have remote tracking branches.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ it may face its own errors and raises them as

- `0` - a successful execution
- `42` - a philosophical constraint
- `43` - a logical constraint
- `45` - a required parameter is empty
- `46` - an unknown command
2 changes: 1 addition & 1 deletion libexec/git-elegant-clear-local
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

default() {
git elegant pull $MASTER
git elegant obtain-work ${MASTER}
local cmd="git branch -lvv | grep gone | awk {'print \$1'}"
__loop "git branch -d" $(eval "$cmd") || \
(
Expand Down
2 changes: 1 addition & 1 deletion libexec/git-elegant-commands
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default() {
echo "save-work"
echo "deliver-work"
echo "accept-work"
echo "pull"
echo "obtain-work"
echo "clear-local"
echo "commands"
}
35 changes: 35 additions & 0 deletions libexec/git-elegant-obtain-work
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e

--up-to-one-branch(){
local BRANCHES=(${@})
if [[ ${#BRANCHES[@]} > 1 ]]; then
echo "The following branches are found:"
for branch in ${BRANCHES[@]}; do
echo " - ${branch}"
done
box "Please re-run the command with concrete branch name from the list above!"
exit 43
fi
}

default() {
local PATTERN=${1}
_error-if-empty "${PATTERN}" "Please provide a branch name or its part."
local LOCAL_BRANCHES=($(git for-each-ref --format='%(refname:strip=2)' refs/heads/* | grep "${PATTERN}"))
--up-to-one-branch ${LOCAL_BRANCHES[@]}
if [[ ${#LOCAL_BRANCHES[@]} = 1 ]]; then
boxtee git checkout ${LOCAL_BRANCHES[0]}
boxtee git pull
else
boxtee git fetch --all
local REMOTE_BRANCHES=($(git for-each-ref --format='%(refname:strip=3)' refs/remotes/** | grep "${PATTERN}"))
--up-to-one-branch ${REMOTE_BRANCHES[@]}
if [[ ${#REMOTE_BRANCHES[@]} = 1 ]]; then
boxtee git checkout ${REMOTE_BRANCHES[0]}
else
box "There are no either remove or local branches which match '${PATTERN}' pattern."
exit 43
fi
fi
}
10 changes: 0 additions & 10 deletions libexec/git-elegant-pull

This file was deleted.

2 changes: 1 addition & 1 deletion libexec/git-elegant-start-work
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
default() {
_error-if-empty "$1" "Please give a name for the new branch."
status=$(boxtee git stash save elegant-git)
git elegant pull $MASTER
git elegant obtain-work ${MASTER}
boxtee git checkout -b "$1"

if [[ "$status" =~ "Saved working directory" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/git-elegant-clear-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load addons-fake

setup() {
fake-pass git "branch -lvv" "first [gone]"
fake-pass git "elegant pull master"
fake-pass git "elegant obtain-work master"
fake-pass git "branch -d first"
}

Expand Down
2 changes: 1 addition & 1 deletion tests/git-elegant-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ teardown() {
"save-work"
"deliver-work"
"accept-work"
"pull"
"obtain-work"
"clear-local"
"commands"
)
Expand Down
61 changes: 61 additions & 0 deletions tests/git-elegant-obtain-work.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bats

load addons-common
load addons-fake

teardown() {
clean-fake
}

@test "'obtain-work': raise 45 error if branch name pattern in not set" {
check git-elegant obtain-work
[[ "$status" -eq 45 ]]
[[ "${lines[@]}" =~ "Please provide a branch name or its part." ]]
}

@test "'obtain-work': use found local branch when given pattern matches only one local branch" {
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/heads/*" "master\nfoo"
fake-pass git "checkout foo"
fake-pass git pull

check git-elegant obtain-work fo
[[ "$status" -eq 0 ]]
}

@test "'obtain-work': raise 43 error when given pattern matches several local branches" {
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/heads/*" "master\nfoo\nfo2\nfo3"

check git-elegant obtain-work fo
[[ "$status" -eq 43 ]]
[[ "${lines[@]}" =~ "Please re-run the command with concrete branch name from the list above!" ]]
}

@test "'obtain-work': use found remote branch when given pattern matches only one remote branch" {
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/heads/*"
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=3)' refs/remotes/**" "rremote\nmaster"
fake-pass git "checkout rremote"

check git-elegant obtain-work rr
[[ "$status" -eq 0 ]]
}

@test "'obtain-work': raise 43 error when given pattern matches several remote branches" {
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/heads/*"
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=3)' refs/remotes/**" "rremote\nmaster\nbarr"

check git-elegant obtain-work rr
[[ "$status" -eq 43 ]]
[[ "${lines[@]}" =~ "Please re-run the command with concrete branch name from the list above!" ]]
}

@test "'obtain-work': raise 43 error when given pattern matches zero remote branches" {
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/heads/*"
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=3)' refs/remotes/**" "rremote\nmaster\nbarr"

check git-elegant obtain-work aa
[[ "$status" -eq 43 ]]
[[ "${lines[@]}" =~ "There are no either remove or local branches which match 'aa' pattern." ]]
}
25 changes: 0 additions & 25 deletions tests/git-elegant-pull.bats

This file was deleted.

2 changes: 1 addition & 1 deletion tests/git-elegant-start-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load addons-common
load addons-fake

setup() {
fake-pass git "elegant pull master"
fake-pass git "elegant obtain-work master"
fake-pass git "checkout -b test-feature"
fake-pass git "stash save elegant-git"
}
Expand Down

0 comments on commit 8138793

Please sign in to comment.