~abentley/bzrtools/bzrtools.dev

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/sh
# Copyright (C) 2004 Aaron Bentley
#
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.

# Runs a standard aba command
aba_run()
{
if [ -z "$abaname" ]; then
  aba "$(basename $0)" "$@"
  exit
fi

case "$1" in
  "exec" )
  shift
  if [ "$1" = "-h" ] ; then
    cmd_help
  elif [ "$1" = "--help" ] ; then
    cmd_help
  elif [ "$1" = "-H" ] ; then
    cmd_help
    cmd_ext_help
  else
    if [ -n "$ABAFILTER" ]; then
      cmd_exec "$@" | $ABAFILTER
    else 
      cmd_exec "$@"
    fi
  fi
  ;;
  "desc" )
  cmd_desc
  ;;
esac
}

# Runs a command that functions as a tla wrapper
# The difference from aba_run is the handling of extended help
aba_run_tlawrap()
{
if [ -z "$abaname" ]; then
  aba "$(basename $0)" "$@"
  exit
fi

case "$1" in
  "exec" )
  shift
  if [ "$1" = "-h" ] ; then
    cmd_help
  elif [ "$1" = "--help" ] ; then
    cmd_help
  elif [ "$1" = "-H" ] ; then
    cmd_ext_help
  else
    if [ -n "$ABAFILTER" ]; then
      cmd_exec "$@" | $ABAFILTER
    else 
      cmd_exec "$@"
    fi
  fi
  ;;
  "desc" )
  cmd_desc
  ;;
esac
}

# Outputs the tag source of the current tree, according to patch-logs
# May be wrong under certain contrived circumstances
aba_tree_source()
{
  logs=$(tla logs 2>/dev/null)
  if [ $? -ne 0 ]; then
    return 2
  fi

  tagsource=$(for r in $logs; do tla cat-log $r;done|grep "Continuation-of: "|head -n 1|sed s/"Continuation-of: "//)
  if [ -z "$tagsource" ]; then
    return 1
  fi
  echo $tagsource
}

#Converts a fully-qualified revision to a fully-qualified version
aba_fqversion()
{
  echo $(tla parse-package-name -a $1)/$(tla parse-package-name --package-version $1)
}

# Outputs a full revision name; input may be either
# a full revision name
# a patchlevel refering to the current tree version
aba_revision_or_version()
{
  name=$(aba_full_revision $@)
  if [ -z "$name" ]; then
    if tla valid-package-name -p -v $1; then
      name=$1
    fi
  fi
  echo $name
}

# Outputs a full revision name; input may be either
# a full revision name
# a patchlevel refering to the current tree version
aba_full_revision()
{
  if [ -z "$1" ]; then
    aba_tree_revision
    return $?
  fi
  if tla valid-package-name -p -v --lvl $1; then
    echo $1
  else
    newname=`tla tree-version 2> /dev/null`--$1
    if tla valid-package-name -p -v --lvl $newname; then
      echo $newname
    fi
  fi
}

# Executes the given command, omitting the last $1 lines
# $1 is the number of lines
#
aba_omit_last()
{
  n=$1;
  shift;
  my_local_lines="$(expr $("$@" | wc -l) - $n)"
  "$@" | head  -n $my_local_lines
  unset my_local_lines
}

# Outputs a tla-style command description 
aba_desc()
{
  cmd=$1;
  shift;
  printf "%28s : %s\n" $cmd "$*"
}

# Creates a temporary directory and outputs its filename
aba_mktempdir()
{
  if [ -z "$TMPDIR" ]; then
    TMPDIR="/tmp"
  fi
  mktemp -d "$TMPDIR/aba.XXXXXX"
  if [ $? -ne 0 ] ; then
    echo "panic: Could not create temporary directory!"
    exit 2
  fi
}

# Outputs a temporary filename
aba_mktemp()
{
  if [ -z "$TMPDIR" ]; then
    TMPDIR="/tmp"
  fi
  mktemp "$TMPDIR/aba.XXXXXX"
  if [ $? -ne 0 ] ; then
    echo "panic: Could not create temporary file!"
    exit 2
  fi
}

# Attempts to execute a command, panics if returns non-zero
aba_try()
{
  "$@"
  if [ $? -ne 0 ] ; then
    echo PANIC: failed to execute $@ >&2
    exit 2
  fi
}

# Undoes a star-merge
# Takes the same parameters as star-merge (except --changes)
aba_star_merge_undo()
{
  tmp=$(aba_mktempdir)
  treeroot=$(tla tree-root)
  echo "* undoing current changes"
  aba_try tla undo > /dev/null
  echo "* generating star-merge changeset"
  aba_try tla star-merge --changes $tmp/merge-undo "$@" > /dev/null
  echo "* redoing changes"
  aba_try tla redo > /dev/null
  echo "* reverse-applying star-merge changeset"
  tla dopatch --reverse $tmp/merge-undo $treeroot
  status=$?
  if [ $status -gt 1 ]; then 
    echo Failed to execute tla dopatch --reverse $tmp/merge-undo . >&2
    return $status
  fi
  aba_try rm -Rf $tmp
  return $status
}

# Outputs the project tree revision
aba_tree_revision()
{
  tla logs -d "${1:-.}" -r -f | head -n 1
}

# Break any links that the given file may have
aba_break_link()
{
   cp -p $1 '++brokelink' && mv '++brokelink' $1
   if [ $? -ne 0 ]; then
     echo "Can't break link for $1"
     exit 2
   fi
}

# Ensure that the project tree has no uncommitted changes
aba_no_changes ()
{
  tla changes > /dev/null
  status=$?
  if [ $status -eq 1 ]; then 
    echo Project tree has uncommitted changes.  Aborting. 2>&2
    exit 1;
  fi
  if [ $status -gt 1 ]; then
    exit $status;
  fi
}

# Determine whether a command supports a given option (long form required)
aba_supports_option()
{
  tla $1 -h | grep -- "--$2" >/dev/null
  return $?
}

# Echoes message to stderr, exits with status 2
aba_panic()
{
  echo $@ >&2
  exit 2
}

aba_merge_source()
{
  tree_root=`tla tree-root 2> /dev/null`
  if [ $? -ne 0 ]; then
      echo "not in project tree ($(pwd))" >&2
      exit 2;
    exit 1
  fi
  if [ -f $tree_root/{arch}/+partner-versions ]; then
    partnerfile=$tree_root/{arch}/+partner-versions 
  else 
    partnerfile=$tree_root/{arch}/=partner-versions
  fi
  mergefile=$tree_root/++merge-source 
  if [ -n "$1" ]; then
    sourcever=$1;
    sourcetype="[supplied]"
  elif [ -f $partnerfile ] ; then
    sourcever=$(cat $partnerfile)
    sourcetype="[from partner-versions]"
  elif [ -f $mergefile ] ; then
    sourcever=$(cat $mergefile)
    sourcetype="[from ++merge-source]"
  else
    treesource=$(aba_tree_source)
    status=$?;
    # always false
    if [ $status -eq 2 ]; then
      echo "merge: not in project tree ($(pwd))" >&2
      exit 2;
    fi
    if [ $status -ne 0 ]; then
      echo "aba merge: Unable to find $sourcefile" or tag source >&2
      exit 1;
    fi
    sourcever="$(tla parse-package-name -a $treesource)/$(tla parse-package-name --package-version $treesource)"
    sourcetype="[from tag-source]"
  fi
}
aba_tag_this()
{
  tla tag -S `aba_tree_revision` $1 
}
aba_change_version()
{
  root=$(tla tree-root)
  if [ $? -ne 0 ]; then
    aba_panic "Unable to determine tree root"
  fi
  from=$(aba_tree_revision)
  if [ $? -ne 0 ]; then
    aba_panic "Unable to determine tree revision"
  fi
  to=$(tla revisions -f $1|tail -n 1)
  if [ $? -ne 0 ]; then
    aba_panic "Unable to determine target revision"
  fi
  if [ -z "$to" ]; then
    aba_panic "Unable to determine target revision"
  fi
  aba_try tla apply-delta --dir $root $from $to
  aba_try tla set-tree-version $1
}
# arch-tag: runner by Aaron Bentley (07:50 Jan 19, 2004)