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