chiark / gitweb /
Revert "Rewrite fd-commit in POSIX Shell"
[fdroidserver.git] / fd-commit
1 #!/bin/bash
2 #
3 # fd-commit - part of the FDroid server tools
4 # Commits updates to apps, allowing you to edit the commit messages
5 #
6 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 commands=()
22
23 if [ ! -d metadata ]; then
24         if [ -d ../metadata ]; then
25                 cd ..
26         else
27                 echo "No metadata files found!"
28                 exit 2
29         fi
30 fi
31
32 while read line; do
33         if [[ "$line" == *\?\?*metadata/*.txt ]]; then
34                 new=true
35         elif [[ "$line" == *M*metadata/*.txt ]]; then
36                 new=false
37         fi
38         file=${line##* }
39
40         id=${file##*/}
41         id=${id%.txt*}
42         if [ $# -gt 0 ]; then
43                 case "$@" in
44                         *" $id "*) ;;  # Middle
45                         "$id "*) ;;    # Start
46                         *" $id") ;;    # End
47                         "$id") ;;      # Alone
48                         *) continue ;; # Missing
49                 esac
50         fi
51
52         [ -d metadata/$id ] && extra=metadata/$id || extra=
53
54         name= autoname=
55         while read l; do
56                 if [[ "$l" == "Auto Name:"* ]]; then
57                         autoname=${l#*:}
58                 elif [[ "$l" == "Name:"* ]]; then
59                         name=${l#*:}
60                 fi
61         done < "$file"
62
63         if [ -n "$name" ]; then
64                 fullname="$name"
65         elif [ -n "$autoname" ]; then
66                 fullname="$autoname"
67         else
68                 fullname="$id"
69         fi
70
71         if $new; then
72                 message="New app: $fullname"
73         else
74                 onlybuild=true
75                 newbuild=false
76                 disable=false
77                 while read l; do
78                         if [[ "$l" == *"Maintainer Notes:"* ]]; then
79                                 break
80                         fi
81                         if [[ "$l" == "-Build:"* ]]; then
82                                 onlybuild=false
83                         elif [[ "$l" == "+Build:"* ]]; then
84                                 if $newbuild; then
85                                         onlybuild=false
86                                 fi
87                                 newbuild=true
88                                 build=${l#*:}
89                                 version=${build%%,*}
90                                 build=${build#*,}
91                                 vercode=${build%%,*}
92                         elif $newbuild && $onlybuild && [[ "$l" == "+"*"disable="* ]]; then
93                                 disable=true
94                         fi
95                 done < <(git diff HEAD -- "$file")
96
97                 if $newbuild && $onlybuild; then
98                         if $disable; then
99                                 message="Don't update $fullname to $version ($vercode)"
100                         else
101                                 message="Update $fullname to $version ($vercode)"
102                         fi
103                 else
104                         message="$fullname:"
105                 fi
106         fi
107
108         message=${message//\"/\\\"}
109         commands+=("git add -- $file $extra && git commit -m \"$message\" -e -v")
110 done < <(git status --porcelain metadata)
111
112 [[ -z $commands ]] && exit 0
113
114 git reset >/dev/null
115 for cmd in "${commands[@]}"; do
116         eval "$cmd"
117         git reset >/dev/null
118 done