Skip to content
Newer
Older
100755 102 lines (91 sloc) 2.78 KB
f93be01 initial commit
kollerma authored Mar 18, 2011
1 #!/bin/bash -e
2 ## Attaches HEAD if commit and top of branch are identical anyway
4976386 Did a lot of work, added a README and most of the functionality work …
kollerma authored Mar 22, 2011
3 ## Fast forwards local branch if HEAD matches top of remote branch.
4 ## If argument -w is given: warn if HEAD cannot be attached
959c247 Testversion of attach-head that does not depend on what-branch.
kollerma authored Mar 28, 2011
5 ## prefer master over other branches
f93be01 initial commit
kollerma authored Mar 18, 2011
6
46f53ef Added --help arguments to all scripts.
kollerma authored May 20, 2011
7 ## read input, display help if necessary
8 if [[ "$@" == *--help* ]]; then
9 cat<<EOF
49975a3 Added a short description in the NAME field of the man pages.
kollerma authored Jul 8, 2011
10 Attach HEAD to a branch if possible
11
46f53ef Added --help arguments to all scripts.
kollerma authored May 20, 2011
12 This command tries to reattach the head of a git repository to some
13 branch.
14 If the HEAD and the top of a branch are identical, then this branch
15 is checked out. If this is not the case, but the local branch can be
16 fast forwarded, then the script does so. Prefers "master" over other
17 branches.
18
19 Usage:
20 git attach-head [-w]
21
22 -w: warn if HEAD cannot be attached
23 EOF
24 exit 0;
25 fi
26
f93be01 initial commit
kollerma authored Mar 18, 2011
27 ## TODO: do some error checking?
28
29 ## stop if HEAD is a ref already
3c89365 Cleaning up code and output.
Manuel Koller authored Dec 1, 2011
30 if git symbolic-ref -q HEAD > /dev/null ; then
f93be01 initial commit
kollerma authored Mar 18, 2011
31 exit 0
32 fi
33
4976386 Did a lot of work, added a README and most of the functionality work …
kollerma authored Mar 22, 2011
34 ## get arguments
35 while getopts ":w" opt; do
36 case $opt in
37 w)
38 w=1
39 #echo "-f was triggered!" >&2
40 ;;
41 \?)
42 # ignore this
43 #echo "Invalid option: -$OPTARG" >&2
44 ;;
45 esac
46 done
47
f93be01 initial commit
kollerma authored Mar 18, 2011
48 HEAD=`git rev-parse HEAD`
959c247 Testversion of attach-head that does not depend on what-branch.
kollerma authored Mar 28, 2011
49 ## get first refs for master (prefer master)
50 branch=`git show-ref master | grep $HEAD | sed 's/.*refs\///' | sed -e '2,$ d'`
51 if [[ ! "$branch" ]]; then
f96fd12 git-attach-head: do not try to attach to tags, prints debug informati…
kollerma authored Mar 29, 2011
52 ## ok, allow all refs but HEAD and tags/*, get first match
53 branch=`git show-ref | grep $HEAD | sed -e 's/.*refs\///' -e '/HEAD/ d' -e '/tags\// d' | sed -e '2,$ d'`
4976386 Did a lot of work, added a README and most of the functionality work …
kollerma authored Mar 22, 2011
54 fi
959c247 Testversion of attach-head that does not depend on what-branch.
kollerma authored Mar 28, 2011
55 if [[ ! "$branch" ]]; then
4976386 Did a lot of work, added a README and most of the functionality work …
kollerma authored Mar 22, 2011
56 if [[ "$w" ]]; then
67f9fbc Routing all errors and warnings to stderr instead of stdout.
Manuel Koller authored Mar 1, 2012
57 cat >&2 <<EOF
4ba252b Modified output, replaced any multiple "echo"s by "cat"
Manuel Koller authored Dec 9, 2011
58 Warning in $PWD:
59 Cannot attach HEAD. There are probably unmerged updates available.
60 Go ("cd") there and use "git branch -v" list available branches.
61 Switch branches with "git checkout <branchname>".
62 EOF
4976386 Did a lot of work, added a README and most of the functionality work …
kollerma authored Mar 22, 2011
63 fi
f96fd12 git-attach-head: do not try to attach to tags, prints debug informati…
kollerma authored Mar 29, 2011
64 else
65 ## try to attach head now
66 branch=${branch#heads/}
1579fa9 git-attach-head: dependence on git-what-branch removed.
kollerma authored Mar 29, 2011
67 ##echo "Attaching $PWD to $branch..."
f96fd12 git-attach-head: do not try to attach to tags, prints debug informati…
kollerma authored Mar 29, 2011
68 lbranch=${branch#remotes/*/}
1579fa9 git-attach-head: dependence on git-what-branch removed.
kollerma authored Mar 29, 2011
69 if [ "$branch" != "$lbranch" ]; then
70 ## we matched a remote/../ branch
71 rbranch=${branch#remotes/}
72 ## check if a local branch exists that tracks this branch
73 tbranch=`git branch -vv | grep "\[$rbranch" | sed -e 's/ \([^ ]*\) .*/\1/' -e '2,$ d'`
74 if [[ "$tbranch" ]]; then
75 ## check if we can fast forward the corresponding local branch
76 if git branch -vv | grep " $tbranch" | grep " ahead " -q; then
77 ## cannot, since local branch is ahead -> exit
78 if [[ "$w" ]]; then
67f9fbc Routing all errors and warnings to stderr instead of stdout.
Manuel Koller authored Mar 1, 2012
79 cat >&2 <<EOF
4ba252b Modified output, replaced any multiple "echo"s by "cat"
Manuel Koller authored Dec 9, 2011
80 Warning in $PWD:
81 Cannot attach HEAD.
82 The local branch $tbranch is ahead of $rbranch.
83 EOF
1579fa9 git-attach-head: dependence on git-what-branch removed.
kollerma authored Mar 29, 2011
84 fi
85 exit 0
86 fi
87 lbranch="$tbranch"
88 else
89 ## there is no local tracking branch for $remote: create one
0b2a2a1 Cleaning up output of git-attach-head.
Manuel Koller authored Dec 8, 2011
90 git branch "$lbranch" "$rbranch" > /dev/null
1579fa9 git-attach-head: dependence on git-what-branch removed.
kollerma authored Mar 29, 2011
91 branch="$lbranch"
92 fi
93 fi
f96fd12 git-attach-head: do not try to attach to tags, prints debug informati…
kollerma authored Mar 29, 2011
94 git checkout --quiet "$lbranch" > /dev/null
95 if [ "$branch" != "$lbranch" ]; then
0b2a2a1 Cleaning up output of git-attach-head.
Manuel Koller authored Dec 8, 2011
96 #echo "Fast forwarding local branch"
f96fd12 git-attach-head: do not try to attach to tags, prints debug informati…
kollerma authored Mar 29, 2011
97 git merge --quiet --ff-only $HEAD
98 fi
f93be01 initial commit
kollerma authored Mar 18, 2011
99 fi
100
101
Something went wrong with that request. Please try again.