blob: f24025d43da05e7efab41e8ae56ea6834b21dcde [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 Driessen6c9e8042010-01-25 21:59:10 +010053gitflow_require_branch_absent() {
54 echo "$ALL_BRANCHES" | grep "^$1\$" 2>/dev/null >/dev/null
55 if [ $? -eq 0 ]; then
56 die "Branch '$1' already exists."
57 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
101 die "And local branch '$1' is ahead of '$2'."
102 else
103 die "Branches need merging first."
104 fi
105 fi
106}
107