Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Eric_余浩
1024程序员开源挑战赛
提交
b803cec0
1
1024程序员开源挑战赛
项目概览
Eric_余浩
/
1024程序员开源挑战赛
与 Fork 源项目一致
Fork自
GitCode / 1024程序员开源挑战赛(10.23-11.14)
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
1
1024程序员开源挑战赛
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b803cec0
编写于
11月 14, 2022
作者:
Y
yuhaowin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改项目使得可以 preview
上级
6a1383ef
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
48 addition
and
218 deletion
+48
-218
pom.xml
pom.xml
+23
-44
preview.yml
preview.yml
+5
-7
src/main/java/com/yuhaowin/tools/GitHookInstallMojo.java
src/main/java/com/yuhaowin/tools/GitHookInstallMojo.java
+6
-167
src/main/java/com/yuhaowin/tools/controller/IndexController.java
...n/java/com/yuhaowin/tools/controller/IndexController.java
+14
-0
未找到文件。
pom.xml
浏览文件 @
b803cec0
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
2.7.5
</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>
com.yuhaowin.tools
</groupId>
<artifactId>
githook-maven-plugin
</artifactId>
<version>
1.0.0
</version>
<packaging>
maven-plugin
</packaging>
<name>
Git Hook Plugin
</name>
<description>
Maven plugin to configure and install local git hooks
</description>
<artifactId>
git-tools
</artifactId>
<version>
1.0
</version>
<name>
git-tools
</name>
<description>
git-tools
</description>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<java.version>
1.8
</java.version>
<maven.compiler.source>
${java.version}
</maven.compiler.source>
<maven.compiler.target>
${java.version}
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
org.apache.maven
</groupId>
<artifactId>
maven-plugin-api
</artifactId>
<version>
3.3.9
</version>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.maven.plugin-tools
</groupId>
<artifactId>
maven-plugin-annotations
</artifactId>
<version>
3.4
</version>
<scope>
provided
</scope>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.eclipse.jgit
</groupId>
<artifactId>
org.eclipse.jgit
</artifactId>
<version>
5.2.1.201812262042-r
</version>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-amqp
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-plugin-plugin
</artifactId>
<version>
3.4
</version>
<executions>
<execution>
<id>
default-descriptor
</id>
<goals>
<goal>
descriptor
</goal>
</goals>
<phase>
process-classes
</phase>
</execution>
<execution>
<id>
help-descriptor
</id>
<goals>
<goal>
helpmojo
</goal>
</goals>
<phase>
process-classes
</phase>
</execution>
</executions>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
preview.yml
浏览文件 @
b803cec0
autoOpen
:
tru
e
autoOpen
:
fals
e
apps
:
-
port
:
8501
command
:
mvn package -Dmaven.test.skip=true
root
:
./
name
:
git-hook-tools
description
:
git-hook-tools
autoOpen
:
true
\ No newline at end of file
-
root
:
.
port
:
8080
run
:
mvn package && java -jar target/git-tools-1.0.jar
autoOpen
:
true
src/main/java/com/yuhaowin/tools/GitHookInstallMojo.java
浏览文件 @
b803cec0
package
com.yuhaowin.tools
;
import
org.apache.maven.plugin.AbstractMojo
;
import
org.apache.maven.plugin.MojoExecutionException
;
import
org.apache.maven.plugin.MojoFailureException
;
import
org.apache.maven.plugins.annotations.LifecyclePhase
;
import
org.apache.maven.plugins.annotations.Mojo
;
import
org.apache.maven.plugins.annotations.Parameter
;
import
org.eclipse.jgit.storage.file.FileRepositoryBuilder
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
static
java
.
nio
.
file
.
StandardOpenOption
.
CREATE
;
import
static
java
.
nio
.
file
.
StandardOpenOption
.
TRUNCATE_EXISTING
;
@SpringBootApplication
public
class
GitHookInstallMojo
{
@Mojo
(
name
=
"install"
,
defaultPhase
=
LifecyclePhase
.
INITIALIZE
,
threadSafe
=
true
)
public
final
class
GitHookInstallMojo
extends
AbstractMojo
{
private
static
final
String
NEW_LINE
=
System
.
lineSeparator
();
private
static
final
String
SHEBANG
=
"#!/bin/sh"
+
NEW_LINE
;
private
static
final
List
<
String
>
validHooks
=
Arrays
.
asList
(
"applypatch-msg"
,
"pre-applypatch"
,
"post-applypatch"
,
"pre-commit"
,
"prepare-commit-msg"
,
"commit-msg"
,
"post-commit"
,
"pre-rebase"
,
"post-checkout"
,
"post-merge"
,
"pre-receive"
,
"update"
,
"post-receive"
,
"post-update"
,
"pre-auto-gc"
,
"post-rewrite"
,
"pre-push"
);
/**
* The hooks that should be installed. For each map entry, the key must be a
* valid Git hook name (see https://git-scm.com/docs/githooks#_hooks) and the
* value is the script to install
*/
@Parameter
(
name
=
"hooks"
)
private
Map
<
String
,
String
>
hooks
;
/**
* external git hooks shell script
*/
@Parameter
(
name
=
"resource-hooks"
)
private
Map
<
String
,
String
>
resourceHooks
;
/**
* Used to validate the .git directory,should appear in the hierarchy of where
* the project is being built
*/
@Parameter
(
defaultValue
=
"${project.build.directory}"
,
required
=
true
,
readonly
=
true
)
private
String
buildDirectory
;
/**
* Whether the plugin should be skipped
*/
@Parameter
(
property
=
"githook.plugin.skip"
)
private
boolean
skip
=
false
;
@Override
public
void
execute
()
throws
MojoExecutionException
,
MojoFailureException
{
if
(
skip
)
{
getLog
().
info
(
"Skipping GitHook plugin execution"
);
return
;
}
Path
hooksDir
=
getOrCreateHooksDirectory
(
buildDirectory
);
if
(
hooksDir
==
null
)
{
getLog
().
info
(
"No .git directory found, skipping plugin execution"
);
throw
new
MojoExecutionException
(
String
.
format
(
"Not a git repository, could not find a .git/hooks directory anywhere in the hierarchy of %s."
,
buildDirectory
));
}
buildHooks
(
hooksDir
);
buildResourceHooks
(
hooksDir
);
}
private
void
buildHooks
(
Path
hooksDir
)
throws
MojoExecutionException
{
if
(
hooks
==
null
||
hooks
.
isEmpty
())
{
getLog
().
info
(
"hooks is empty,skip..."
);
return
;
}
for
(
Map
.
Entry
<
String
,
String
>
hook
:
hooks
.
entrySet
())
{
String
hookName
=
hook
.
getKey
();
if
(!
validHooks
.
contains
(
hookName
))
{
getLog
().
error
(
String
.
format
(
"`%s` hook is not a valid git-hook name"
,
hookName
));
continue
;
}
String
hookScript
=
hook
.
getValue
();
String
finalScript
=
(
hookScript
.
startsWith
(
"#!"
)
?
""
:
SHEBANG
)
+
hookScript
+
NEW_LINE
;
try
{
getLog
().
info
(
String
.
format
(
"Installing %s hook into %s"
,
hookName
,
hooksDir
.
toAbsolutePath
()));
writeFile
(
hooksDir
.
resolve
(
hookName
),
finalScript
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
catch
(
IOException
e
)
{
throw
new
MojoExecutionException
(
"Could not write hook with name: "
+
hookName
,
e
);
}
}
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
GitHookInstallMojo
.
class
,
args
);
}
private
void
buildResourceHooks
(
Path
hooksDir
)
throws
MojoExecutionException
{
if
(
resourceHooks
==
null
||
resourceHooks
.
isEmpty
())
{
getLog
().
info
(
"resource-hooks is empty,skip..."
);
return
;
}
for
(
Map
.
Entry
<
String
,
String
>
hook
:
resourceHooks
.
entrySet
())
{
String
hookName
=
hook
.
getKey
();
if
(!
validHooks
.
contains
(
hookName
))
{
getLog
().
error
(
String
.
format
(
"`%s` hook is not a valid git-hook name"
,
hookName
));
continue
;
}
Path
local
=
Paths
.
get
(
""
);
Path
hookFilePath
=
Paths
.
get
(
hook
.
getValue
());
if
(!
hookFilePath
.
toAbsolutePath
().
startsWith
(
local
.
toAbsolutePath
()))
{
throw
new
MojoExecutionException
(
"only file inside the project can be used to generate git hooks"
);
}
try
{
getLog
().
info
(
"Installing "
+
hookName
+
" from "
+
hookFilePath
);
String
finalScript
=
Files
.
lines
(
hookFilePath
).
collect
(
Collectors
.
joining
(
"\n"
));
writeFile
(
hooksDir
.
resolve
(
hookName
),
finalScript
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
catch
(
IOException
e
)
{
throw
new
MojoExecutionException
(
"could not access hook resource : "
+
hookFilePath
,
e
);
}
}
}
private
synchronized
void
writeFile
(
Path
path
,
byte
[]
bytes
)
throws
IOException
{
File
created
=
Files
.
write
(
path
,
bytes
,
CREATE
,
TRUNCATE_EXISTING
).
toFile
();
boolean
success
=
created
.
setExecutable
(
true
,
true
)
&&
created
.
setReadable
(
true
,
true
)
&&
created
.
setWritable
(
true
,
true
);
if
(!
success
)
{
throw
new
IllegalStateException
(
String
.
format
(
"Could not set permissions on created file %s"
,
created
.
getAbsolutePath
()));
}
}
private
Path
getOrCreateHooksDirectory
(
String
base
)
{
getLog
().
debug
(
String
.
format
(
"Searching for .git directory starting at %s"
,
base
));
File
gitMetadataDir
=
new
FileRepositoryBuilder
().
findGitDir
(
new
File
(
base
)).
getGitDir
();
if
(
gitMetadataDir
==
null
)
{
return
null
;
}
Path
hooksDir
=
gitMetadataDir
.
toPath
().
resolve
(
"hooks"
);
if
(!
hooksDir
.
toFile
().
exists
())
{
getLog
().
info
(
String
.
format
(
"Creating missing hooks directory at %s"
,
hooksDir
.
toAbsolutePath
()));
try
{
Files
.
createDirectories
(
hooksDir
);
}
catch
(
IOException
e
)
{
getLog
().
error
(
e
);
return
null
;
}
}
return
hooksDir
;
}
}
src/main/java/com/yuhaowin/tools/controller/IndexController.java
0 → 100644
浏览文件 @
b803cec0
package
com.yuhaowin.tools.controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
public
class
IndexController
{
@RequestMapping
(
"/"
)
public
String
index
()
{
return
"git tools success"
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录