blob: 08267581a43e9a0866bcadcdf2df6d8835794fb2 [file] [log] [blame]
Vincent Driessen61ade552010-01-21 01:28:08 +01001#!/bin/sh
Vincent Driessen093a1472010-01-21 01:36:44 +01002#
3# gitflow -- A collection of Git wrapper scripts to provide high-level
4# repository operations for Vincent Driessen's branching model:
5#
6# Original blog post presenting this model is found at:
7# http://nvie.com/archives/323
8#
9# Feel free to contribute to this project at:
10# http://github.com/nvie/gitflow
11#
12# Copyright (c) 2010 by Vincent Driessen
13#
Vincent Driessen61ade552010-01-21 01:28:08 +010014
Vincent Driessen4f1cc332010-01-25 16:44:57 +010015# Get the git dir
16GIT_DIR=$(git rev-parse --git-dir)
17
18# Get all available branches
Vincent Driessen7238e292010-01-25 23:52:26 +010019LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
20REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
Vincent Driessen4f1cc332010-01-25 16:44:57 +010021ALL_BRANCHES="$LOCAL_BRANCHES\n$REMOTE_BRANCHES"
22
Vincent Driessena0434ca2010-01-25 17:28:22 +010023warn() { echo "$@" >&2; }
24die() { warn "$@"; exit 1; }
Vincent Driessen4f1cc332010-01-25 16:44:57 +010025
Vincent Driessen61ade552010-01-21 01:28:08 +010026gitflow_check_clean_working_tree() {
Vincent Driessen144bb502010-01-25 23:55:18 +010027 if [ "$(git status 2>/dev/null | tail -n1)" != "nothing to commit (working directory clean)" ]; then
Vincent Driessena1bc8712010-01-25 21:50:08 +010028 die "Working directory is dirty. Only use gitflow in clean working directories for your own safety."
29 fi
Vincent Driessen4f1cc332010-01-25 16:44:57 +010030}
31
32gitflow_require_local_branch() {
Vincent Driessen144bb502010-01-25 23:55:18 +010033 echo "$LOCAL_BRANCHES" | grep "^$1\$" 2>&1 >/dev/null
Vincent Driessen4f1cc332010-01-25 16:44:57 +010034 if [ $? -ne 0 ]; then
35 die "Local branch '$1' does not exist and is required."
36 fi
37}
38
39gitflow_require_remote_branch() {
Vincent Driessen144bb502010-01-25 23:55:18 +010040 echo "$REMOTE_BRANCHES" | grep "^$1\$" 2>&1 >/dev/null
Vincent Driessen4f1cc332010-01-25 16:44:57 +010041 if [ $? -ne 0 ]; then
42 die "Remote branch '$1' does not exist and is required."
43 fi
44}
45
46gitflow_require_branch() {
Vincent Driessen144bb502010-01-25 23:55:18 +010047 echo "$ALL_BRANCHES" | grep "^$1\$" 2>&1 >/dev/null
Vincent Driessen4f1cc332010-01-25 16:44:57 +010048 if [ $? -ne 0 ]; then
49 die "Branch '$1' does not exist and is required."
50 fi
Vincent Driessen61ade552010-01-21 01:28:08 +010051}
52
Vincent Driessen6c9e8042010-01-25 21:59:10 +010053gitflow_require_branch_absent() {
Vincent Driessen144bb502010-01-25 23:55:18 +010054 echo "$ALL_BRANCHES" | grep "^$1\$" 2>&1 >/dev/null
Vincent Driessen6c9e8042010-01-25 21:59:10 +010055 if [ $? -eq 0 ]; then
Vincent Driessenec2c8952010-01-25 22:40:13 +010056 die "Branch '$1' already exists. Pick another name."
Vincent Driessen6c9e8042010-01-25 21:59:10 +010057 fi
58}
59
Vincent Driessenafee9fd2010-01-25 17:29:29 +010060#
61# gitflow_test_branches_equal()
62#
63# Tests whether branches and their "origin" counterparts have diverged and need
64# merging first. It returns error codes to provide more detail, like so:
65#
66# 0 Branch heads point to the same commit
67# 1 First given branch needs fast-forwarding
68# 2 Second given branch needs fast-forwarding
69# 3 Branch needs a real merge
70#
71gitflow_test_branches_equal() {
72 commit1=$(git rev-parse "$1")
73 commit2=$(git rev-parse "$2")
74 if [ "$commit1" != "$commit2" ]; then
75 base=$(git merge-base "$commit1" "$commit2")
76 short_base=$(git rev-parse --short "$base")
77
78 if [ "$commit1" = "$base" ]; then
79 return 1
80 elif [ "$commit2" = "$base" ]; then
81 return 2
82 else
83 return 3
84 fi
85 else
86 return 0
87 fi
88}
89
90gitflow_require_branches_equal() {
91 gitflow_require_local_branch "$1"
92 gitflow_require_remote_branch "$2"
93 gitflow_test_branches_equal "$1" "$2"
94 status=$?
95 if [ $status -gt 0 ]; then
96 warn "Branches '$1' and '$2' have diverged."
97
98 if [ $status -eq 1 ]; then
99 die "And branch '$1' may be fast-forwarded."
100 elif [ $status -eq 2 ]; then
Vincent Driessen51fa95d2010-01-26 00:23:15 +0100101 # Warn here, since there is no harm in being ahead
102 warn "And local branch '$1' is ahead of '$2'."
Vincent Driessenafee9fd2010-01-25 17:29:29 +0100103 else
104 die "Branches need merging first."
105 fi
106 fi
107}
108