Skip to content

Instantly share code, notes, and snippets.

@jcarsique
Last active March 16, 2021 20:40
Show Gist options
  • Save jcarsique/359400aab7403428e2a6703276f8e544 to your computer and use it in GitHub Desktop.
Save jcarsique/359400aab7403428e2a6703276f8e544 to your computer and use it in GitHub Desktop.
Git format
#!/bin/bash -e
# Usage: gitformat
# Create a git commit with formatting changes
echo "Ref before changes: $(git id)"
#HEAD=${1:-HEAD}
pushd $(git rev-parse --show-toplevel)
FILES=$(git diff-index --name-only --diff-filter=ad HEAD|xargs)
if [ -z "$FILES" ]; then
echo "No files"
exit 0
fi
TMP=$(mktemp -d)
cp -a --parents $FILES $TMP/
nb=$(find $TMP/ -type f|wc -l)
echo Copied $nb files to $TMP/
git stash push -q -u -m"safety stash $TMP"
git stash list |head -1|grep '$TMP' && git stash apply -q
git co -f HEAD -- $FILES
cat >/tmp/format.properties <<EOF
idea.config.path=\${user.home}/.IntelliJIdea/format/config
idea.system.path=\${user.home}/.IntelliJIdea/format/system
EOF
format_command=$(grep idea.sh $(which idea)|sed "s,idea.sh,format.sh,"|cut -f 1 -d " ")
eval "env IDEA_PROPERTIES=/tmp/format.properties $format_command -s $HOME/workspace/idea-nuxeo_codestyle.xml $FILES"
git add -- $FILES
#git fix $HEAD
git ci -m"cleanup & format"
cp -a $TMP/* .
echo rm -rf $TMP/
git stash list |head -1|grep '$TMP' && echo You can now run 'git stash drop'
popd
#!/bin/bash -e
# Usage: gitformat1
# Reset files, format and stop for review and amend. When you're fine with the formatting changes, run gitformat2 to get back original files
pushd $(git rev-parse --show-toplevel)
FILES=$(git diff-index --name-only --diff-filter=ad HEAD|xargs)
TMP=$(mktemp -d)
echo $TMP > /tmp/gitformat1
cp -a --parents $FILES $TMP/
nb=$(find $TMP/ -type f|wc -l)
echo Copied $nb files to $TMP/
git stash save -q -u "safety stash $TMP"
git stash apply -q
git reset --hard HEAD
echo
echo eclipse $FILES
echo
echo idea format $FILES
echo
cat >/tmp/format.properties <<EOF
idea.config.path=\${user.home}/.IntelliJIdea/format/config
idea.system.path=\${user.home}/.IntelliJIdea/format/system
EOF
format_command=$(grep idea.sh $(which idea)|sed "s,idea.sh,format.sh,")
eval "env IDEA_PROPERTIES=/tmp/format.properties $format_command -s /home/julien/workspace/idea-nuxeo_codestyle.xml $FILES"
echo "When you're done, commit and run gitformat2"
popd
#!/bin/bash -e
# Usage: gitformat2
# Put back the original files after execution of 'gitformat1'
pushd $(git rev-parse --show-toplevel)
TMP=$(cat /tmp/gitformat1)
cp -a $TMP/* .
echo rm -rf $TMP/
git stash list |head -1|grep '$TMP' && echo You can now run 'git stash drop'
popd
cat >/tmp/format.properties <<EOF
idea.config.path=\${user.home}/.IntelliJIdea/format/config
idea.system.path=\${user.home}/.IntelliJIdea/format/system
EOF
format_command=$(grep idea.sh $(which idea)|sed "s,idea.sh,format.sh,")
eval "env IDEA_PROPERTIES=/tmp/format.properties $format_command -s $HOME/workspace/idea-nuxeo_codestyle.xml"
<code_scheme name="Nuxeo" version="173">
<option name="LINE_SEPARATOR" value="&#xA;" />
<option name="SOFT_MARGINS" value="120" />
<GroovyCodeStyleSettings>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="500" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="500" />
</GroovyCodeStyleSettings>
<JavaCodeStyleSettings>
<option name="JD_KEEP_EMPTY_LINES" value="false" />
</JavaCodeStyleSettings>
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<codeStyleSettings language="JAVA">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_CONTROL_STATEMENT_IN_ONE_LINE" value="false" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
<option name="BLANK_LINES_AROUND_FIELD" value="1" />
<option name="INDENT_CASE_FROM_SWITCH" value="false" />
<option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" />
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
<option name="ALIGN_MULTILINE_RESOURCES" value="false" />
<option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACES" value="true" />
<option name="SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE" value="true" />
<option name="CALL_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_WRAP" value="1" />
<option name="RESOURCE_LIST_WRAP" value="5" />
<option name="EXTENDS_LIST_WRAP" value="1" />
<option name="THROWS_LIST_WRAP" value="1" />
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
<option name="THROWS_KEYWORD_WRAP" value="1" />
<option name="METHOD_CALL_CHAIN_WRAP" value="5" />
<option name="BINARY_OPERATION_WRAP" value="1" />
<option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
<option name="TERNARY_OPERATION_WRAP" value="5" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="VARIABLE_ANNOTATION_WRAP" value="2" />
</codeStyleSettings>
<codeStyleSettings language="Markdown">
<option name="WRAP_ON_TYPING" value="0" />
</codeStyleSettings>
<codeStyleSettings language="Shell Script">
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="4" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="XML">
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="2" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment