git pre-commit 钩子格式代码 - Intellij/Android Studio [英] git pre-commit hook format code - Intellij/Android Studio

查看:47
本文介绍了git pre-commit 钩子格式代码 - Intellij/Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个要点展示了如何在预提交时使用 Eclipse 格式化程序自动格式化 Java 代码.

来源:https://gist.github.com/ktoso/708972

代码:

#!/bin/sh## 这个钩子将在任何提交之前运行 eclipse 代码格式化程序# 使源看起来像它在回购中的样子.ECLIPSE_HOME=$HOME/eclipseSTYLE_FILE=$HOME/org.eclipse.jdt.core.prefsecho "运行预提交钩子:run-eclipse-formatter------"echo "将运行 Eclipse 格式化程序,使用:$STYLE_FILE"回声列出要在其上运行格式化程序的文件夹..."code_dirs=`查找.-maxdepth 3 |grep 'src/'`对于 $code_dirs 中的目录;做回声 $dir;完毕;echo "正在启动 eclipse 代码格式化程序... "执行 $ECLIPSE_HOME/eclipse \-nosplash \-application org.eclipse.jdt.core.JavaCodeFormatter \-详细\-config $STYLE_FILE \$code_dirs回声完成-----------------------------------------------────」

我想通过 IntelliJ 和 Android Studio 实现这一目标.脚本会是什么样子?

另外我想最好只在更改的文件上运行格式化程序.也许这很有用:

changedJavaFiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')

ACM 代表添加、复制、修改.来源:http://git-scm.com/docs/git-diff>

如果有什么不清楚的,请评论.

更新

我的设置是 Windows 10,我想使用命令行工具 MINGW32(Git Bash).Git 版本是 1.9.5 msysgit.1

解决方案

您可以使用 IntelliJ 命令行源代码格式化程序 自 2016.3 版起在 IntelliJ 中可用.例如(对于 Git Bash):

CHANGED_JAVA_SRC_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')$INTELLIJ_DIR/bin/格式 $CHANGED_JAVA_SRC_FILES

虽然Android Studio是基于IntelliJ的,但是IDE打开的时候好像不支持这个,看下面的评论.

GitHub 中的 git-hooks-code-autoformat 项目.

请注意 snap 包 可用于 Ubuntu,因此也很容易将其添加到 Ubuntu Git 或 CI 服务器.

This gist shows how to auto-format Java code using the Eclipse formatter at pre-commit.

Source: https://gist.github.com/ktoso/708972

Code:

#!/bin/sh
#
# This hook will run the eclipse code formatter before any commit
# to make the source look as it's supposed to look like in the repo.

ECLIPSE_HOME=$HOME/eclipse
STYLE_FILE=$HOME/org.eclipse.jdt.core.prefs
echo "Running pre-commit hook: run-eclipse-formatter---------------------"
echo "Will run eclipse formatter, using: $STYLE_FILE"
echo "Listing folders to run formatter on… "
code_dirs=`find . -maxdepth 3 | grep 'src/'`
for dir in $code_dirs; do
   echo $dir;
done;

echo "Launching eclipse code formatter…    "
exec $ECLIPSE_HOME/eclipse \
                    -nosplash \
                    -application org.eclipse.jdt.core.JavaCodeFormatter \
                    -verbose \
                    -config $STYLE_FILE \
                    $code_dirs

echo "done---------------------------------------------------------------"

I'd like to achieve this with IntelliJ and Android Studio. How would the script look like then?

Also I guess it would be best to only run the formatter on changed files. Maybe this is useful:

changedJavaFiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')

ACM stands for Added, Copied, Modified. Source: http://git-scm.com/docs/git-diff

Please comment if anything is unclear.

Update

My setup is Windows 10 and I'd like to use the command line tool MINGW32(Git Bash). Git version is 1.9.5 msysgit.1

解决方案

You can use the IntelliJ command-line source code formatter available in IntelliJ since version 2016.3. For example (for Git Bash):

CHANGED_JAVA_SRC_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')
$INTELLIJ_DIR/bin/format $CHANGED_JAVA_SRC_FILES

Although Android Studio is based on IntelliJ, it seems not to support this when the IDE is open, see comment below.

A more advanced pre-commit hook example is available in the git-hooks-code-autoformat project in GitHub.

Note that a snap package is available for Ubuntu, so it is easy to add this to a Ubuntu Git or CI server as well.

这篇关于git pre-commit 钩子格式代码 - Intellij/Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆