diff options
author | Teddy Wing | 2019-06-09 21:31:38 +0200 |
---|---|---|
committer | Teddy Wing | 2019-06-09 21:36:26 +0200 |
commit | 3ff388797bc3abe7b9996877db4f2fcd860f01f0 (patch) | |
tree | 481af6959e3e84f616728771fd9cf6f3290e72be /code-review-start | |
parent | 022fa289cce900a4f178e0fcbdef8e609780a764 (diff) | |
download | code-review-3ff388797bc3abe7b9996877db4f2fcd860f01f0.tar.bz2 |
code-review-start: Add `get_merge_base` function
Function to get the merge base for the current branch. If none exists in
the database, default to `origin/master` if there's an `origin` remote,
or `master` if not.
Thanks to Serpiton
(https://codereview.stackexchange.com/users/61525/serpiton) on Stack
Overflow for describing how to get a default value using the `MAX()`
function:
https://codereview.stackexchange.com/questions/74192/select-first-value-if-exists-otherwise-select-default-value/74252#74252
Thanks to 'tig' (https://stackoverflow.com/users/96823/tig) on Stack
Overflow for the command to check if a given Git remote exists:
https://stackoverflow.com/questions/12170459/check-if-git-remote-exists-before-first-push/26843249#26843249
Diffstat (limited to 'code-review-start')
-rwxr-xr-x | code-review-start | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/code-review-start b/code-review-start index f4f8b37..0dc2265 100755 --- a/code-review-start +++ b/code-review-start @@ -52,9 +52,32 @@ function create_merge_base () { function create_merge_base_from_current () { local base="$1" - local current_branch=$(git rev-parse --abbrev-ref HEAD) - create_merge_base "$current_branch" "$base" + create_merge_base "$(current_branch)" "$base" +} + +function get_merge_base () { + local head="$(current_branch)" + local default_base='master' + + if git config remote.origin.url > /dev/null; then + default_base='origin/master' + fi + + sqlite3 "$DATABASE" <<-SQL + BEGIN TRANSACTION; + + SELECT ifnull(max(base), '$default_base') + FROM merge_bases + WHERE head = '$head' + LIMIT 1; + + COMMIT; + SQL +} + +function current_branch () { + git rev-parse --abbrev-ref HEAD } |