Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow moving of records with the ability to keep all records properly named #131

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/_adr_move
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash
set -e
eval "$($(dirname $0)/adr-config)"

echo -n | sed -E
whichsed=$?

adr_dir=$("$adr_bin_dir/_adr_dir")

function move {
source_num=$(printf "%04d" $1)
source=$("$adr_bin_dir/_adr_file" "${adr_dir}/${source_num}")
if [ ! -f "$source" ];
then
return
fi
source_dir=${source%/*}
source_filename=${source##*/}
target=$2
target_num=$(printf "%04d" ${target})
target_filename=${source_filename/[0-9][0-9][0-9][0-9]/${target_num}}

source_title=$("$adr_bin_dir/_adr_title" "$source")
if [ $whichsed -eq 0 ];
then
source_short_title=$(echo "${source_title}" | sed -E 's/^[0-9]+\.[[:space:]]+//g')
else
source_short_title=$(echo "${source_title}" | sed 's/^[0-9]\+\.\s\+//g')
fi
target_title="${target}. ${source_short_title}"

pattern_escape_cmd='sed -e s/[]\/$*.^[]/\\&/g'
sub_escape_cmd='sed -e s/[\/&]/\\&/g'
source_filename_escaped=$(echo "${source_filename}" | ${pattern_escape_cmd})
target_filename_escaped=$(echo "${target_filename}" | ${sub_escape_cmd})

source_title_escaped=$(echo "${source_title}" | ${pattern_escape_cmd})
target_title_escaped=$(echo "${target_title}" | ${sub_escape_cmd})

echo -n "."
# update file
mv ${source} ${source_dir}/${target_filename}

# update references (it will update $target_file as well)
for adr_filename in $("$adr_bin_dir/adr-list")
do
if [ $whichsed -eq 0 ];
then
sed -i '' "s/${source_filename_escaped}/${target_filename_escaped}/g ; s/${source_title_escaped}/${target_title_escaped}/g ;" ${adr_filename}
else
sed -i "s/${source_filename_escaped}/${target_filename_escaped}/g ; s/${source_title_escaped}/${target_title_escaped}/g ;" ${adr_filename}
fi
done
}

if [ -d $adr_dir ]
then
count=`find $adr_dir | grep -E "^$adr_dir/[0-9]+-[^/]*\\.md" | wc -l | xargs`
else
echo "The $adr_dir directory does not exist"
exit 1
fi

end=`expr $count + 1`

move $1 $end

#Let's also rename all subsequence files so that they're still numbered in the correct order
if test $1 -gt $2
then
before=$(expr $1 - 1)
for i in $(seq $before $2);
do
plusone=$(expr $i + 1)
move $i $plusone
done
move $end $2
else
after=$(expr $1 + 1)
for i in $(seq $after $2);
do
minusone=$(expr $i - 1)
move $i $minusone
done
move $end $2
fi

20 changes: 20 additions & 0 deletions src/adr-move
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -e
eval "$($(dirname $0)/adr-config)"

## usage: adr link SOURCE LINK TARGET REVERSE-LINK
##
## Creates a link between two ADRs, from SOURCE to TARGET new.
## SOURCE and TARGET are both a reference (number or partial filename) to an ADR
## LINK is the description of the link created in the SOURCE.
## REVERSE-LINK is the description of the link created in the TARGET
##
## E.g. to create link ADR 12 to ADR 10
##
## adr link 12 Amends 10 "Amended by"
##

source="${1:?SOURCE}"
target="${2:?TARGET}"

"$adr_bin_dir/_adr_move" "$source" "$target"