Refactoring.
[git-central.git] / server / functions
1 #!/bin/sh
2
3 # Sets: new_commits
4 # Assumes: $oldrev $newrev $refname
5 #
6 # This is for use in post receive hooks, as it assumes the refname has moved and
7 # is now newrev, we need to discard it. This is down with bash string replace,
8 # as it will replace only the first match, compared to the canonical "grep -v"
9 # approach which will throw out multiple matches if the same commit is referred
10 # to by multiple branches.
11 function set_new_commits() {
12         nl=$'\n'
13
14         # Get all the current branches, not'd as we want only new ones
15         new_commits=$(git rev-parse --not --branches)
16
17         # Strip off the not current branch
18         new_hash=$(git rev-parse $refname)
19         new_commits=${new_commits/^$new_hash/}
20
21         # Put back newrev without the not
22         new_commits=${new_commits}${nl}${newrev}
23
24         # Put in ^oldrev if it's not a new branch
25         if [ "$oldrev" != "0000000000000000000000000000000000000000" ] ; then
26                 new_commits=${new_commits}${nl}^${oldrev}
27         fi
28
29         new_commits=${new_commits/$nl$nl/$nl}
30         new_commits=${new_commits/#$nl/}
31 }
32
33 # Sets: $change_type
34 # Assumes: $oldrev $newrev
35 #
36 # --- Interpret
37 # 0000->1234 (create)
38 # 1234->2345 (update)
39 # 2345->0000 (delete)
40 function set_change_type() {
41         if [ "$oldrev" == "0000000000000000000000000000000000000000" ] ; then
42                 change_type="create"
43         else
44                 if [ "$newrev" == "0000000000000000000000000000000000000000" ] ; then
45                         change_type="delete"
46                 else
47                         change_type="update"
48                 fi
49         fi
50 }
51
52 # Sets: $newrev_type $oldrev_type $rev $rev_type
53 # Assumes: $newrev $oldrev
54 # --- Get the revision types
55 function set_rev_types() {
56         newrev_type=$(git cat-file -t "$newrev" 2> /dev/null)
57         oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
58         if [ "$newrev" == "0000000000000000000000000000000000000000" ] ; then
59                 rev_type="$oldrev_type"
60                 rev="$oldrev"
61         else
62                 rev_type="$newrev_type"
63                 rev="$newrev"
64         fi
65 }
66
67 # Sets: $describe
68 # Assumes: $rev
69 #
70 # The email subject will contain the best description of the ref that we can build from the parameters
71 function set_describe() {
72         describe=$(git describe $rev 2>/dev/null)
73         if [ -z "$describe" ]; then
74                 describe=$rev
75         fi
76 }
77