forked from go/golangs_learn
张献维
1 year ago
1 changed files with 121 additions and 0 deletions
@ -0,0 +1,121 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
"os/exec" |
|||
"path/filepath" |
|||
"strings" |
|||
) |
|||
|
|||
func main() { |
|||
projectDir := "E:\\Code-Java\\全国公务员\\" // 替换为指定目录 p 的路径
|
|||
author := "张献维" // 替换为指定的用户名称
|
|||
since := "2023-12-12" // 替换为指定的开始日期
|
|||
|
|||
// 更改工作目录到指定目录 p
|
|||
if err := os.Chdir(projectDir); err != nil { |
|||
fmt.Println("Error changing directory:", err) |
|||
return |
|||
} |
|||
|
|||
// 获取指定用户和时间后的 Java 和 XML 提交文件
|
|||
files, err := getGitFiles(author, since, "*.java", "*.xml") |
|||
if err != nil { |
|||
fmt.Println("Error getting Git files:", err) |
|||
return |
|||
} |
|||
|
|||
fmt.Println("Java and XML files in commit:", files) |
|||
|
|||
// 在 projectDir 下创建目录 b
|
|||
destinationDirB := filepath.Join(projectDir, "b-java") |
|||
if err := os.MkdirAll(destinationDirB, 0755); err != nil { |
|||
fmt.Println("Error creating directory b-java:", err) |
|||
return |
|||
} |
|||
// 在 projectDir 下创建目录 c
|
|||
destinationDirC := filepath.Join(projectDir, "c-class") |
|||
if err := os.MkdirAll(destinationDirC, 0755); err != nil { |
|||
fmt.Println("Error creating directory c-class:", err) |
|||
return |
|||
} |
|||
|
|||
// 复制找到的 Java 和 XML 文件到目录 b
|
|||
for _, file := range files { |
|||
srcPath := filepath.Join(projectDir, file) |
|||
dstPath := filepath.Join(destinationDirB, filepath.Base(file)) |
|||
if err := copyFile(srcPath, dstPath); err != nil { |
|||
fmt.Println("Error copying file:", err) |
|||
} |
|||
srcPath = strings.Replace(file, "src/main/java", "target/classes", 1) |
|||
srcPath = strings.Replace(srcPath, "src-check/main/java", "target/classes", 1) |
|||
srcPath = strings.Replace(srcPath, "src-3member/main/java", "target/classes", 1) |
|||
if strings.HasSuffix(file, ".java") { |
|||
srcPath = filepath.Join(projectDir, strings.Replace(srcPath, ".java", ".class", 1)) |
|||
} |
|||
dstPath = filepath.Join(destinationDirC, filepath.Base(srcPath)) |
|||
if err := copyFile(srcPath, dstPath); err != nil { |
|||
fmt.Println("Error copying file:", err) |
|||
} |
|||
} |
|||
} |
|||
|
|||
func getGitFiles(author, since string, patterns ...string) ([]string, error) { |
|||
cmdArgs := []string{"log", "--since=" + since, "--author=" + author, "--name-only", "--pretty=format:"} |
|||
cmdArgs = append(cmdArgs, "--") |
|||
cmdArgs = append(cmdArgs, patterns...) |
|||
cmd := exec.Command("git", cmdArgs...) |
|||
output, err := cmd.Output() |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
files := strings.Split(strings.TrimSpace(string(output)), "\n") |
|||
uniqueFiles := uniqueNonEmptyElementsOf(files) |
|||
return uniqueFiles, nil |
|||
} |
|||
|
|||
func copyClassFiles(sourceDir, destinationDir string) error { |
|||
return filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if strings.HasSuffix(path, ".class") { |
|||
destinationPath := filepath.Join(destinationDir, filepath.Base(path)) |
|||
return copyFile(path, destinationPath) |
|||
} |
|||
return nil |
|||
}) |
|||
} |
|||
|
|||
func copyFile(src, dst string) error { |
|||
sourceFile, err := os.Open(src) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
defer sourceFile.Close() |
|||
|
|||
destFile, err := os.Create(dst) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
defer destFile.Close() |
|||
|
|||
_, err = io.Copy(destFile, sourceFile) |
|||
return err |
|||
} |
|||
|
|||
func uniqueNonEmptyElementsOf(elements []string) []string { |
|||
encountered := map[string]bool{} |
|||
result := []string{} |
|||
|
|||
for _, element := range elements { |
|||
if element != "" && !encountered[element] { |
|||
encountered[element] = true |
|||
result = append(result, element) |
|||
} |
|||
} |
|||
return result |
|||
} |
Loading…
Reference in new issue