blob: 3181c3f5e50014853ab492c0ec720298a321f774 [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
19LOCAL_BRANCHES=$(cd "$GIT_DIR/refs/heads"; find * -type f)
20REMOTE_BRANCHES=$(cd "$GIT_DIR/refs/remotes"; find * -type f)
21ALL_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 Driessena1bc8712010-01-25 21:50:08 +010027 if [ "$(git status 2> /dev/null | tail -n1)" != "nothing to commit (working directory clean)" ]; then
28 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() {
33 echo "$LOCAL_BRANCHES" | grep "^$1\$" 2>/dev/null >/dev/null
34 if [ $? -ne 0 ]; then
35 die "Local branch '$1' does not exist and is required."
36 fi
37}
38
39gitflow_require_remote_branch() {
40 echo "$REMOTE_BRANCHES" | grep "^$1\$" 2>/dev/null >/dev/null
41 if [ $? -ne 0 ]; then
42 die "Remote branch '$1' does not exist and is required."
43 fi
44}
45
46gitflow_require_branch() {
47 echo "$ALL_BRANCHES" | grep "^$1\$" 2>/dev/null >/dev/null
48 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 Driessenafee9fd2010-01-25 17:29:29 +010053#
54# gitflow_test_branches_equal()
55#
56# Tests whether branches and their "origin" counterparts have diverged and need
57# merging first. It returns error codes to provide more detail, like so:
58#
59# 0 Branch heads point to the same commit
60# 1 First given branch needs fast-forwarding
61# 2 Second given branch needs fast-forwarding
62# 3 Branch needs a real merge
63#
64gitflow_test_branches_equal() {
65 commit1=$(git rev-parse "$1")
66 commit2=$(git rev-parse "$2")
67 if [ "$commit1" != "$commit2" ]; then
68 base=$(git merge-base "$commit1" "$commit2")
69 short_base=$(git rev-parse --short "$base")
70
71 if [ "$commit1" = "$base" ]; then
72 return 1
73 elif [ "$commit2" = "$base" ]; then
74 return 2
75 else
76 return 3
77 fi
78 else
79 return 0
80 fi
81}
82
83gitflow_require_branches_equal() {
84 gitflow_require_local_branch "$1"
85 gitflow_require_remote_branch "$2"
86 gitflow_test_branches_equal "$1" "$2"
87 status=$?
88 if [ $status -gt 0 ]; then
89 warn "Branches '$1' and '$2' have diverged."
90
91 if [ $status -eq 1 ]; then
92 die "And branch '$1' may be fast-forwarded."
93 elif [ $status -eq 2 ]; then
94 die "And local branch '$1' is ahead of '$2'."
95 else
96 die "Branches need merging first."
97 fi
98 fi
99}
100