aboutsummaryrefslogtreecommitdiff
path: root/update
blob: fc2ba5414b297d69da4afd34d89f378ed1d5487e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env bash

# Commit and push an update to an existing note.
#
# -c
#   Commit only, don't push.
#
usage="usage: $0 [-c]"

owd=`pwd`
trap "{ cd $owd; exit 1; }" ERR
set -o errtrace # Trap in functions.

function info () { echo "$*" 1>&2; }
function error () { info "$*"; exit 1; }

push="y"

while [ $# -gt 0 ]; do
  case $1 in
    -c)
      push="n"
      shift
      ;;
    *)
      error "$usage"
      ;;
  esac
done

if git status --porcelain | grep -q '^M '; then
  error "error: repository already has staged changes"
fi

# @@ We could probably handle the case where extra files are added to the
# note. We just need to make sure they are not changes to other notes.
#
if git status --porcelain | grep -q '^?? '; then
  error "error: repository has untracked files"
fi

modified="$(git status --porcelain  | sed -n -e 's/^ M \(.*\)/\1/p')"

mc="$(echo "$modified" | wc -w)"

if [ "$mc" -eq 0 ]; then
  error "error: nothing modified"
fi

if [ "$mc" -gt 1 ]; then
  error "error: multiple modified files"
fi

# Extract the start line and count from the changeset header.
#
# The format is:
#
# @@ [+-]FROM-START,FROM-COUNT [+-]TO-START,TO-COUNT @@
#
# Note that if the changeset has only one line then the count is omitted.
#
function changeset_start ()
{
  echo "$1" | sed -e 's/^@@ [^ ]* [+-]\([^, ]*\).*$/\1/'
}

function changeset_count ()
{
  local r
  r="$(echo "$1" | sed -n -e 's/^@@ [^ ]* [+-][^,]*,\([^ ]*\) @@$/\1/p')"
  if [ -z "$r" ]; then
    r="1"
  fi
  echo "$r"
}

#changeset_start "@@ -12,3 +13,11 @@"
#changeset_count "@@ -12,3 +13,11 @@"
#changeset_start "@@ -12 +13 @@"
#changeset_count "@@ -12 +13 @@"

h=
if [ "$(basename "$modified")" = "list" ]; then
  #
  # This is a list of notes. The plan is to get the start line numbers for the
  # first and last change set. We then scan the list line by line looking for
  # headers until we hit the start of the first change set: the last header
  # that we see should be our header. We also continue scanning until we hit
  # the end of the last changeset: if we see any headers then we are updating
  # multiple notes.
  #

  # Get the diff output as a list of change set headers.
  #
  diff="$(git diff -U0 | sed -n -e '/^@@ /p')"

  fc="$(echo "$diff" | head -n 1)"
  lc="$(echo "$diff" | tail -n 1)"

  sl="$(changeset_start "$fc")"
  el="$(($(changeset_start "$lc") + $(changeset_count "$lc") - 1))"

  # Scan the list looking for headers.
  #
  l=
  c="0"
  while read l || [ -n "$l" ]; do

    # Stop if we are past the end line.
    #
    c="$(($c + 1))"
    if [ "$c" -gt "$el" ]; then
      break
    fi

    # See if this is a header.
    #
    if echo "$l" | grep -q '^[-?!] '; then

      # If we are past the start line then we are modifying multiple notes.
      #
      if [ "$c" -gt "$sl" ]; then
	error "error: multiple modified notes in $modified:${sl}-${el}"
      fi

      # This is the last header.
      #
      h="$l"
    fi
  done < <(cat "$modified")

  if [ -z "$h" ]; then
    error "error: unable to find note header for $modified:${sl}-${el}"
  fi
else
  # This is a file note. All we need to do is get its header (the first line).
  #
  h="$(head -n 1 "$modified")"
fi

# Header should start with one of [-?!].
#
sum="$(echo "$h" | sed -n -re 's/^[-?!] ([^[]*)( \[.*\])?$/\1/p')"

if [ -z "$sum" ]; then
  error "error: unable to parse note header '$h'"
fi

# By convention the first label is a note type (bug, feature, etc).
#
type="$(echo "$h" | sed -n -re 's/^[^[]*\[([^ ]*).*\]$/\1/p')"

if [ -z "$type" ]; then
  error "error: note header does not include note type as first label"
fi

# Ok, all looks good so add, commit and push the change.
#
git add "$modified"
git ci -m "Update $type: $sum"

if [ "$push" = "y" ]; then
  git push
fi