Skip to content

Instantly share code, notes, and snippets.

@justinTM
Last active June 2, 2022 20:58
Show Gist options
  • Save justinTM/b856c285f04fa5bdb5d0fec739c60d9e to your computer and use it in GitHub Desktop.
Save justinTM/b856c285f04fa5bdb5d0fec739c60d9e to your computer and use it in GitHub Desktop.

Revisions

  1. justinTM revised this gist Jul 11, 2021. 1 changed file with 23 additions and 20 deletions.
    43 changes: 23 additions & 20 deletions format-diff-file-as-markdown.sh
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,20 @@
    # 3. write out file argument 2, $DIFF_FILE_OUT

    # https://gitlab.com/gitlab-org/gitlab/-/issues/15582
    set -e
    set -e

    DIFF_START='```diff\n'
    DIFF_HEADER="The following rule changes will be applied upon successful rules sync,\
    based on rules files changes in this commit:\n"
    DIFF_END='\n```'
    DIFF_WRAPPER="${DIFF_HEADER}${DIFF_START}%s${DIFF_END}"

    # check input file variable, use $DIFF_FILE_IN from ./gitlab-ci.yml by default
    DIFF_FILE_IN="${1:-$DIFF_FILE_IN}"
    if [ -z "$DIFF_FILE_IN" ]; then
    echo "Error: input variable 1 'DIFF_FILE_IN' is not defined."
    exit 1
    elif test -f "$DIFF_FILE_IN"; then
    elif test -f "${DIFF_FILE_IN}"; then
    # print diff to pipeline
    echo
    echo
    @@ -34,36 +40,33 @@ if [ -z "$DIFF_FILE_OUT" ]; then
    fi

    # remove all occurences of unwanted characters from diff text
    diff_old=$(cat -v ${DIFF_FILE_IN}) # read in file with "show-all" characters flag
    diff_old=`echo -e "${diff_old//'[0m'/}"` # remove all SGR ASCII reset codes '[0m'
    diff_old=`echo -e "${diff_old//'$'/}"` # remove all newline characters '$'
    diff_old=`echo -e "${diff_old//'^['/}"` # remove all escape characters '^['
    diff_old=`echo -e "${diff_old//'+'/}"` # remove all '+'
    diff_old=`echo -e "${diff_old//'-'/}"` # remove all '-'
    DIFF_OLD=$(cat -v ${DIFF_FILE_IN}) # read in file with "show-all" characters flag
    DIFF_OLD=`echo -e "${DIFF_OLD//'[0m'/}"` # remove all SGR ASCII reset codes '[0m'
    DIFF_OLD=`echo -e "${DIFF_OLD//'$'/}"` # remove all newline characters '$'
    DIFF_OLD=`echo -e "${DIFF_OLD//'^['/}"` # remove all escape characters '^['
    DIFF_OLD=`echo -e "${DIFF_OLD//'+'/}"` # remove all '+'
    DIFF_OLD=`echo -e "${DIFF_OLD//'-'/}"` # remove all '-'

    # replace sgr color codes with characters, eg. '[32m' with '+'
    declare -A map_sgr_to_char=( ['[32m']='+' ['[31m']='-' ['[33m']='~' )
    declare -A MAP_SGR_TO_CHAR=( ['[32m']='+' ['[31m']='-' ['[33m']='~' )
    while read -r line; do # loop each line in $diff variable
    # check line for each color code
    for sgr_code in "${!map_sgr_to_char[@]}"; do
    for sgr_code in "${!MAP_SGR_TO_CHAR[@]}"; do
    # if found, replace with new character from associative array
    if [[ $line == *"$sgr_code"* ]]; then
    new_char="${map_sgr_to_char[$sgr_code]}"
    printf -v line "${new_char}%s" "${line/sgr_code/}"
    new_char="${MAP_SGR_TO_CHAR[$sgr_code]}"
    printf -v line '%s' "${new_char}${line/sgr_code/}"
    fi
    done
    diff+=`echo -e "$line\n"` # append markdown newline ' '
    done <<< "$diff_old"
    DIFF_NEW+=`echo -e "$line\n"` # append markdown newline ' '
    done <<< "${DIFF_OLD}"

    # wrap with markdown diff code section formatting ticks
    diff_start='```diff\n' && diff_end='\n```'
    printf -v diff "${diff_start}%s${diff_end}" "$diff"
    echo -e "$diff" > "${DIFF_FILE_OUT}" # write new diff file
    printf -v DIFF_NEW "${DIFF_WRAPPER}" "${DIFF_NEW}"
    echo -e "${DIFF_NEW}" > "${DIFF_FILE_OUT}" # write new diff file

    # print diff to pipeline
    echo
    echo
    echo "Markdown-formatted diff below"
    cat ${DIFF_FILE_OUT}
    echo
    echo
    cat "${DIFF_FILE_OUT}"
  2. justinTM revised this gist Jul 11, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions format-diff-file-as-markdown.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/bash

    # this script will:
    # 1. read in argument 1, $DIFF_FILE_IN
    # 1. read in file argument 1, $DIFF_FILE_IN
    # 2. format text to Markdown diff code section
    # 3. write out file argument 2, $DIFF_FILE_OUT

    @@ -41,7 +41,7 @@ diff_old=`echo -e "${diff_old//'^['/}"` # remove all escape characters '^['
    diff_old=`echo -e "${diff_old//'+'/}"` # remove all '+'
    diff_old=`echo -e "${diff_old//'-'/}"` # remove all '-'

    # replace sgr color codes with characters, eg. '[31m' with '+'
    # replace sgr color codes with characters, eg. '[32m' with '+'
    declare -A map_sgr_to_char=( ['[32m']='+' ['[31m']='-' ['[33m']='~' )
    while read -r line; do # loop each line in $diff variable
    # check line for each color code
  3. justinTM created this gist Jul 11, 2021.
    69 changes: 69 additions & 0 deletions format-diff-file-as-markdown.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/bin/bash

    # this script will:
    # 1. read in argument 1, $DIFF_FILE_IN
    # 2. format text to Markdown diff code section
    # 3. write out file argument 2, $DIFF_FILE_OUT

    # https://gitlab.com/gitlab-org/gitlab/-/issues/15582
    set -e

    # check input file variable, use $DIFF_FILE_IN from ./gitlab-ci.yml by default
    DIFF_FILE_IN="${1:-$DIFF_FILE_IN}"
    if [ -z "$DIFF_FILE_IN" ]; then
    echo "Error: input variable 1 'DIFF_FILE_IN' is not defined."
    exit 1
    elif test -f "$DIFF_FILE_IN"; then
    # print diff to pipeline
    echo
    echo
    echo "rules diff below"
    cat "${DIFF_FILE_IN}"
    echo
    echo
    else
    echo "Error: Input file \"${DIFF_FILE_IN}\" does not exist."
    exit 1
    fi

    # check input variable 2, use $DIFF_FILE_OUT from ./gitlab-ci.yml by default
    DIFF_FILE_OUT="${2:-$DIFF_FILE_OUT}"
    if [ -z "$DIFF_FILE_OUT" ]; then
    echo "Error: input variable 2 'DIFF_FILE_OUT' is not defined."
    exit 1
    fi

    # remove all occurences of unwanted characters from diff text
    diff_old=$(cat -v ${DIFF_FILE_IN}) # read in file with "show-all" characters flag
    diff_old=`echo -e "${diff_old//'[0m'/}"` # remove all SGR ASCII reset codes '[0m'
    diff_old=`echo -e "${diff_old//'$'/}"` # remove all newline characters '$'
    diff_old=`echo -e "${diff_old//'^['/}"` # remove all escape characters '^['
    diff_old=`echo -e "${diff_old//'+'/}"` # remove all '+'
    diff_old=`echo -e "${diff_old//'-'/}"` # remove all '-'

    # replace sgr color codes with characters, eg. '[31m' with '+'
    declare -A map_sgr_to_char=( ['[32m']='+' ['[31m']='-' ['[33m']='~' )
    while read -r line; do # loop each line in $diff variable
    # check line for each color code
    for sgr_code in "${!map_sgr_to_char[@]}"; do
    # if found, replace with new character from associative array
    if [[ $line == *"$sgr_code"* ]]; then
    new_char="${map_sgr_to_char[$sgr_code]}"
    printf -v line "${new_char}%s" "${line/sgr_code/}"
    fi
    done
    diff+=`echo -e "$line\n"` # append markdown newline ' '
    done <<< "$diff_old"

    # wrap with markdown diff code section formatting ticks
    diff_start='```diff\n' && diff_end='\n```'
    printf -v diff "${diff_start}%s${diff_end}" "$diff"
    echo -e "$diff" > "${DIFF_FILE_OUT}" # write new diff file

    # print diff to pipeline
    echo
    echo
    echo "Markdown-formatted diff below"
    cat ${DIFF_FILE_OUT}
    echo
    echo