-
Notifications
You must be signed in to change notification settings - Fork 0
/
mulle-sourcetree
executable file
·627 lines (503 loc) · 16.8 KB
/
mulle-sourcetree
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#! /usr/bin/env mulle-bash
# shellcheck shell=bash
#
# Copyright (c) 2015-2017 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Mulle kybernetiK nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
[ "${TRACE:-}" = 'YES' -o "${MULLE_SOURCETREE_TRACE:-}" = 'YES' ] && set -x && : "$0" "$@"
MULLE_EXECUTABLE_VERSION="1.3.4"
#
# Determine location of own libexec directory. Which is `src` in development
# and ../libexec when deployed. MULLE_EXECUTABLE needs to be defined for this.
#
if ! [ ${MULLE_SOURCETREE_LIBEXEC_DIR+x} ]
then
r_get_libexec_dir "${MULLE_EXECUTABLE}" "mulle-sourcetree" "mulle-sourcetree-action.sh"
MULLE_SOURCETREE_LIBEXEC_DIR="${RVAL}"
fi
# not -u clean yet
set +u
#
# This is the sourcetree::main user interface to mulle-sourcetree
# sorta like git
#
sourcetree::print_commands()
{
local show_all="${1:-NO}"
SHOWN_COMMANDS="\
add : add a node to the sourcetree
clean : remove files added by a sync
json : show current sourcetree as JSON
list : list nodes in the current sourcetree
mark : add or remove a mark from a node in the sourcetree
move : move a node relative to other nodes in the sourcetree
set : set a node's properties in the sourcetree
status : query state of the tree
sync : synchronize the project tree with the sourcetree [default]"
HIDDEN_COMMANDS="\
config : manipulate sourcetree config files
craftorder : emit nodes that are marked \"build\", in dependency order
dbstatus : query state of the database
desecrate : remove all graveyards
dotdump : create a picture of the sourcetree or the database
duplicate : duplicate a sourcetree node
filter : test a filter with user supplied marks
fix : support to track user modifications in the filesystem
eval-add : batch process mulle-sourcetree add commands
get : get a node's properties from the sourcetree
info : print current sourcetree information
libexec-dir : print path to ${MULLE_USAGE_NAME} libexec
knownmarks : list known marks
plugin : list available plugins
rcopy : copy a node from another sourcetree
rename : rename a node in the sourcetree
rename-marks : rename marks in the sourcetree
remove : remove a node from the sourcetree
reset : clear the database, to force a sync
reuuid : create new uuids for the sourcetree
rewrite : clean up marks of a sourcetree
sourcetree-dir : print path to project (if a sourcetree exists)
star-search : search for duplicates by name listed as '*' in list
supermark : inspect supermarks
wrap : wrap nodetypes,branches,URLs in environment variables
test : test if a mark matches against a list of marks
touch : mark sourcetree as dirty
uname : ${MULLE_USAGE_NAME}'s simplified uname(1)
unmark : remove marks from a node in the sourcetree
version : print ${MULLE_USAGE_NAME} version
walk : visit all the sourcetree nodes with a callback"
printf "%s\n" "${SHOWN_COMMANDS}"
if [ "${MULLE_UNAME}" = 'darwin' ]
then
printf "%s\n" "${DARWIN_COMMANDS}"
fi
if [ "${show_all}" != 'NO' ]
then
printf "%s\n" "${HIDDEN_COMMANDS}"
fi
}
sourcetree::usage()
{
local verbose="${1:-NO}"
cat <<EOF
Usage:
${MULLE_USAGE_NAME} [flags] [command] [options]
Maintain a project sourcetree composed of multiple repositories
and archives and other things. The sourcetree is backed by a database,
which is created during \`sync\`.
A sourcetree is created with the "add" of the first node. The actual
contents will be fetched during "sync".
Flags:
EOF
(
cat <<EOF
-N : bypass search for enclosing source tree
--flat : flat operation
--recurse : recursive operation
--share : share operation (default)
EOF
options_technical_flags_usage " : "
) | LC_ALL=C sort
cat <<EOF
Commands:
EOF
sourcetree::print_commands "${verbose}" | sort
if [ "${verbose}" = 'NO' ]
then
cat <<EOF
(use mulle-sourcetree -v help to show more commands)
EOF
fi
cat <<EOF
Options are command specific. Use mulle-sourcetree <command> -h for help.
EOF
cat <<EOF
Environment:
MULLE_SOURCETREE_CONFIG_NAME : config name to search (config)
MULLE_SOURCETREE_CONFIG_NAME_<name> : config to use for dependency (config)
MULLE_SOURCETREE_STASH_DIR : place to store dependencies (stash)
MULLE_SOURCETREE_SYMLINK : allow symlinks to be used
MULLE_SOURCETREE_FETCH_<name> : if NO, inhibits dependency fetch
EOF
exit 1
}
sourcetree::main()
{
local MULLE_FLAG_DEFER="" # test also uses this ?
local FLAG_USE_FALLBACK='NO' # should probably remove
local FLAG_SOURCETREE_MODE=""
local FLAG_CONFIG_DIR=""
local FLAG_SHARE_DIR=""
local FLAG_CONFIG_NAME=""
#
# simple option handling
#
while [ $# -ne 0 ]
do
case "$1" in
-lxx|--trace-database)
MULLE_FLAG_DB_LOG_EXEKUTOR='YES'
;;
-lxw|--trace-walk)
MULLE_FLAG_WALK_LOG_EXEKUTOR='YES'
;;
# intentionaly fall through to options
esac
if options_technical_flags "$1"
then
shift
continue
fi
case "$1" in
-f|--force)
MULLE_FLAG_MAGNUM_FORCE='YES'
;;
-h*|--help|help)
sourcetree::usage "${MULLE_TRACE}"
;;
-lxx|--trace-database)
MULLE_FLAG_DB_LOG_EXEKUTOR='YES'
;;
-lxw|--trace-walk)
MULLE_FLAG_WALK_LOG_EXEKUTOR='YES'
;;
--echo-args)
shift
printf "%s\n" "$@"
exit 0
;;
--config-dir)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
FLAG_CONFIG_DIR="$1"
;;
--config-name|--config-names)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
FLAG_CONFIG_NAME="$1"
;;
-d|--directory)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
cd "$1" || exit
;;
--git-terminal-prompt)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
GIT_TERMINAL_PROMPT="$1"
export GIT_TERMINAL_PROMPT
;;
#
# DEFER MODES
#
-R|--defer-root)
MULLE_FLAG_DEFER="ROOT" # up to virtual root
;;
-T|--defer-this)
MULLE_FLAG_DEFER="NEAREST" # this or next best
;;
-P|--defer-parent) # one above us
MULLE_FLAG_DEFER="PARENT"
;;
-N|--no-defer) # stay in PWD
MULLE_FLAG_DEFER="NONE"
;;
--virtual-root) # stay in PWD
MULLE_FLAG_DEFER="VIRTUAL"
;;
--mode)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
FLAG_SOURCETREE_MODE="$1"
;;
-r|--recurse|--recursive)
FLAG_SOURCETREE_MODE="recurse"
;;
--flat)
FLAG_SOURCETREE_MODE="flat"
;;
--share)
FLAG_SOURCETREE_MODE="share"
;;
--share-dir|--stash-dir)
[ $# -eq 1 ] && fail "Missing argument to \"$1\""
shift
FLAG_SHARE_DIR="$1"
;;
--use-fallback)
FLAG_USE_FALLBACK='YES'
;;
--version)
printf "%s\n" "${MULLE_EXECUTABLE_VERSION}"
exit 0
;;
--zprof)
RUN_ZPROF='YES'
zmodload "zsh/zprof" || exit 1
;;
-*)
log_error "${MULLE_EXECUTABLE_FAIL_PREFIX}: Unknown flag \"$1\""
sourcetree::usage "${MULLE_TRACE}"
;;
*)
break
;;
esac
shift
done
options_setup_trace "${MULLE_TRACE}" && set -x
cmd="${1}"
[ $# -eq 0 ] || shift
case "${cmd}" in
libexec-dir|library-path)
printf "%s\n" "${MULLE_SOURCETREE_LIBEXEC_DIR}"
exit 0
;;
esac
# shellcheck source=mulle-sourcetree-environment.sh
include "sourcetree::environment"
local SOURCETREE_START
local SOURCETREE_CONFIG_NAME
FLAG_CONFIG_NAME="${FLAG_CONFIG_NAME:-${MULLE_SOURCETREE_CONFIG_NAME}}"
sourcetree::environment::default "${FLAG_SHARE_DIR}" \
"${FLAG_CONFIG_DIR}" \
"${FLAG_CONFIG_NAME}" \
"${FLAG_USE_FALLBACK}" \
"${MULLE_FLAG_DEFER}"
#
# dbstatus should be fast and doesn't need share
#
if [ "${cmd}" = "dbstatus" ]
then
# shellcheck source=src/mulle-sourcetree-clean.sh
include "sourcetree::dbstatus"
sourcetree::dbstatus::main "$@"
return $?
fi
#
# dont' check if resetting
#
# for status it's debatable, but I don't like it
# for craftorder, we want to be fast so we don't check and dont get
# default mode
#
case "${cmd}" in
reset|craftorder|dbstatus)
;;
*)
sourcetree::environment::check_sane_stash_dir
;;
esac
local SOURCETREE_MODE
sourcetree::environment::set_default_db_mode "${SOURCETREE_START}" "${FLAG_SOURCETREE_MODE}" \
|| fail "Could not set database to \"${FLAG_SOURCETREE_MODE}\""
log_setting "MULLE_SOURCETREE_PROJECT_DIR : ${MULLE_SOURCETREE_PROJECT_DIR}"
log_setting "MULLE_SOURCETREE_ETC_DIR : ${MULLE_SOURCETREE_ETC_DIR}"
log_setting "MULLE_SOURCETREE_STASH_DIRNAME : ${MULLE_SOURCETREE_STASH_DIRNAME}"
log_setting "MULLE_SOURCETREE_STASH_DIR : ${MULLE_SOURCETREE_STASH_DIR}"
log_setting "MULLE_SOURCETREE_VAR_DIR : ${MULLE_SOURCETREE_VAR_DIR}"
log_setting "MULLE_VIRTUAL_ROOT : ${MULLE_VIRTUAL_ROOT}"
log_setting "PWD : ${PWD}"
log_setting "SOURCETREE_CONFIG_NAME : ${SOURCETREE_CONFIG_NAME}"
log_setting "SOURCETREE_CONFIG_DIR : ${SOURCETREE_CONFIG_DIR}"
log_setting "SOURCETREE_DB_FILENAME : ${SOURCETREE_DB_FILENAME}"
log_setting "SOURCETREE_FIX_FILENAME : ${SOURCETREE_FIX_FILENAME}"
log_setting "SOURCETREE_MODE : ${SOURCETREE_MODE}"
log_setting "SOURCETREE_START : ${SOURCETREE_START}"
log_setting "MULLE_FLAG_DB_LOG_EXEKUTOR : ${MULLE_FLAG_DB_LOG_EXEKUTOR}"
log_setting "MULLE_FLAG_WALK_LOG_EXEKUTOR : ${MULLE_FLAG_WALK_LOG_EXEKUTOR}"
case "${cmd}" in
add|copy|get|duplicate|info|knownmarks|mark|move|rcopy|rename|rename-marks|remove|set|unmark)
# shellcheck source=src/mulle-sourcetree-commands.sh
include "sourcetree::commands"
sourcetree::commands::${cmd//-/_}_main "$@"
;;
craftorder|clean|fix|json|list|reset|reuuid|rewrite|wrap|status|walk)
include "sourcetree::${cmd}"
sourcetree::${cmd}::main "$@"
;;
supermark)
include "sourcetree::${cmd}s"
sourcetree::${cmd}s::main "$@"
;;
commands)
echo "\
add
clean
config
craftorder
dbstatus
desecrate
dotdump
duplicate
etc-dir
eval-add
filter
fix
get
info
json
knownmarks
libexec-dir
list
mark
move
plugin
project-dir
pwd
rcopy
remove
rename
rename-marks
reset
reuuid
rewrite
set
sourcetree-dir
star-search
status
supermark
sync
test
touch
uname
unmark
var-dir
version
wrap"
;;
config)
# shellcheck source=src/mulle-sourcetree-config.sh
include "sourcetree::config"
sourcetree::config::main "$@"
;;
desecrate)
# shellcheck source=src/mulle-sourcetree-clean.sh
include "sourcetree::clean"
sourcetree::clean::main --all-graveyards "$@"
;;
dotdump)
# shellcheck source=src/mulle-sourcetree-dotdump.sh
include "sourcetree::dotdump" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::dotdump::main "$@"
;;
eval-add)
# shellcheck source=src/mulle-sourcetree-eval-add.sh
include "sourcetree::eval-add" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::eval_add::main "$@"
;;
filter)
# shellcheck source=src/mulle-sourcetree-filter.sh
include "sourcetree::filter" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::filter::main "$@"
;;
mode)
printf "%s\n" "${SOURCETREE_MODE}"
;;
plugin|plugins)
# shellcheck source=src/mulle-sourcetree-plugin.sh
include "sourcetree::plugin" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::plugin::main "$@"
;;
pwd|path)
# shellcheck source=src/mulle-sourcetree-walk.sh
pwd -P # prefer physical path for our needs
;;
etc-dir)
printf "%s\n" "${MULLE_SOURCETREE_ETC_DIR}"
;;
project-dir)
printf "%s\n" "${MULLE_SOURCETREE_PROJECT_DIR}"
;;
var-dir)
printf "%s\n" "${MULLE_SOURCETREE_VAR_DIR}"
;;
share-dir|stash-dir)
printf "%s\n" "${MULLE_SOURCETREE_STASH_DIR}"
;;
star-search)
# shellcheck source=src/mulle-sourcetree-walk.sh
include "sourcetree::walk" # shellcheck source=src/mulle-sourcetree-dotdump.sh
if [ -z "$1" -o "$#" -ne 1 -o "$1" = "-h" -o "$1" = "--help" -o "$1" = "help" ]
then
fail "star-search needs one node address to search for"
fi
sourcetree::walk::main \
--dedupe-mode nodeline-no-uuid \
--lenient \
"[ \"\${NODE_ADDRESS}\" = \"$1\" ] && \
echo \"\${NODE_MARKS} \${NODE_TAG} \${NODE_BRANCH} \${NODE_URL} (\${WALK_DATASOURCE#\${PWD}/})\"" | sort -u
;;
tool-env)
printf "MULLE_SOURCETREE_PROJECT_DIR='%s'\n" "${MULLE_SOURCETREE_PROJECT_DIR}"
printf "MULLE_SOURCETREE_STASH_DIR='%s'\n" "${MULLE_SOURCETREE_STASH_DIR}"
;;
touch)
if [ -f "${MULLE_SOURCETREE_ETC_DIR}/config}" ]
then
exekutor touch "${MULLE_SOURCETREE_ETC_DIR}/config"
fi
if [ -f "${MULLE_SOURCETREE_ETC_DIR}/config.${MULLE_UNAME}" ]
then
exekutor touch "${MULLE_SOURCETREE_ETC_DIR}/config.${MULLE_UNAME}"
fi
;;
uname)
printf "%s\n" "${MULLE_UNAME}"
;;
sync|update)
# shellcheck source=src/mulle-sourcetree-sync.sh
include "sourcetree::sync" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::sync::main "$@"
;;
test)
# shellcheck source=src/mulle-sourcetree-test.sh
include "sourcetree::test" # shellcheck source=src/mulle-sourcetree-dotdump.sh
sourcetree::test::main "$@"
;;
version)
printf "%s\n" "${MULLE_EXECUTABLE_VERSION}"
return 0
;;
"")
sourcetree::usage "${MULLE_TRACE}"
;;
*)
log_error "${MULLE_EXECUTABLE_FAIL_PREFIX}: Unknown command \"${cmd}\""
sourcetree::usage "${MULLE_TRACE}"
;;
esac
}
call_with_flags "sourcetree::main" "${MULLE_SOURCETREE_FLAGS:-}" "$@"
rval=$?
if [ "${RUN_ZPROF}" = 'YES' ]
then
zprof
fi
exit $rval