golang实现Git无缝轻量级时间追踪插件git-time-metric的使用
Golang实现Git无缝轻量级时间追踪插件git-time-metric的使用
Git Time Metric
为所有Git项目提供无缝时间追踪
GTM是自动、无缝且轻量级的。您不需要记住启动和停止计时器。它会在您的编辑器触发活动时运行以捕获时间数据。时间指标作为Git notes与git仓库本地存储,并可推送到远程仓库。
插件
只需为您喜欢的编辑器安装插件和GTM命令行工具,即可开始追踪您的时间。
快速开始
安装最新GTM版本
Mac OS X
最简单的方法是使用Homebrew
brew tap git-time-metric/gtm
brew install gtm
Windows
下载并运行Windows安装程序
Linux
最简单的方法是使用Linuxbrew
brew tap git-time-metric/gtm
brew install gtm
手动安装Linux、OSX或Windows
下载并安装可执行文件
为您的编辑器安装插件
- Sublime 3
- Atom
- Vim
- IntelliJ IDEA, PyCharm, WebStorm, AppCode, RubyMine, PhpStorm, AndroidStudio
- VSCode
- Visual Studio
- Terminal
初始化项目进行时间追踪
$ cd /my/project/dir
$ gtm init
Git Time Metric initialized for /my/project/dir
post-commit: gtm commit --yes
alias.fetchgtm: fetch origin refs/notes/gtm-data:refs/notes/gtm-data
alias.pushgtm: push origin refs/notes/gtm-data
notes.rewriteref: refs/notes/gtm-data
terminal: true
.gitignore: /.gtm/
tags: tag1, tag2
在项目中编辑一些文件
使用gtm status
检查您的进度。
$ gtm status
20m 40s 53% [m] plugin/gtm.vim
18m 5s 46% [r] Terminal
15s 1% [m] .gitignore
39m 0s gtm-vim-plugin
提交您的工作
当您准备好时,像往常一样提交您的工作。GTM会自动保存与您的提交关联的时间。要检查最后一次提交的时间,请输入gtm report
。
$ gtm report
7129f00 Remove post processing of status
Fri Sep 09 20:45:03 2016 -0500 gtm-vim-plugin Michael Schenk
20m 40s 53% [m] plugin/gtm.vim
18m 5s 46% [r] Terminal
15s 1% [m] .gitignore
39m 0s gtm-vim-plugin
可选地将时间保存在远程Git仓库中
GTM提供了git别名使这变得容易。默认情况下,它使用origin作为远程仓库。
可以通过推送将时间数据保存到远程仓库。
$ git pushgtm
可以通过获取从远程仓库检索时间数据。
$ git fetchgtm
获取帮助
在命令行中输入gtm --help
和gtm <subcommand> --help
获取帮助。
Golang实现示例
以下是一个简单的Golang示例,展示如何使用GTM API进行时间追踪:
package main
import (
"fmt"
"os/exec"
)
func main() {
// 初始化GTM
initGTM()
// 模拟编辑文件
editFile("main.go")
// 获取状态
getStatus()
// 提交更改
commitChanges("Update main.go")
// 生成报告
generateReport()
}
func initGTM() {
cmd := exec.Command("gtm", "init")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("初始化GTM失败: %v\n", err)
return
}
fmt.Println(string(output))
}
func editFile(filename string) {
// 在实际应用中,这里会是您编辑文件的代码
fmt.Printf("正在编辑文件: %s\n", filename)
}
func getStatus() {
cmd := exec.Command("gtm", "status")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("获取状态失败: %v\n", err)
return
}
fmt.Println(string(output))
}
func commitChanges(message string) {
// 先执行git add
addCmd := exec.Command("git", "add", ".")
if err := addCmd.Run(); err != nil {
fmt.Printf("git add失败: %v\n", err)
return
}
// 执行git commit
commitCmd := exec.Command("git", "commit", "-m", message)
output, err := commitCmd.CombinedOutput()
if err != nil {
fmt.Printf("提交失败: %v\n", err)
return
}
fmt.Println(string(output))
}
func generateReport() {
cmd := exec.Command("gtm", "report")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("生成报告失败: %v\n", err)
return
}
fmt.Println(string(output))
}
贡献
如果您发现错误或有新功能的想法,请随时提交新问题和PR。特别是如果还没有您喜欢的编辑器的插件,请创建一个!
支持
要报告错误,请在GitHub页面上提交问题。
更多关于golang实现Git无缝轻量级时间追踪插件git-time-metric的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang实现Git无缝轻量级时间追踪插件git-time-metric的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang实现Git无缝轻量级时间追踪插件git-time-metric
Git-time-metric (GTM)是一个轻量级的时间追踪工具,可以无缝集成到Git工作流中,帮助开发者记录在代码上花费的时间。下面我将介绍如何使用Golang实现类似功能。
核心功能设计
- 自动记录文件编辑时间
- 与Git提交关联
- 生成时间报告
- 轻量级无侵入集成
Golang实现方案
1. 基本数据结构
package gtm
import (
"time"
)
// TimeRecord 表示单个文件的时间记录
type TimeRecord struct {
FilePath string `json:"file_path"`
ActiveTime time.Duration `json:"active_time"`
LastUpdated time.Time `json:"last_updated"`
}
// CommitRecord 表示提交关联的时间记录
type CommitRecord struct {
CommitHash string `json:"commit_hash"`
Records []TimeRecord `json:"records"`
Timestamp time.Time `json:"timestamp"`
}
2. 文件监视器实现
package gtm
import (
"os"
"path/filepath"
"time"
)
type FileWatcher struct {
activeFiles map[string]*TimeRecord
dataDir string
}
func NewFileWatcher(dataDir string) *FileWatcher {
return &FileWatcher{
activeFiles: make(map[string]*TimeRecord),
dataDir: dataDir,
}
}
func (fw *FileWatcher) StartWatching() {
// 实现文件系统监视逻辑
// 可以使用fsnotify库来监视文件变化
}
func (fw *FileWatcher) recordActivity(filePath string) {
now := time.Now()
if record, exists := fw.activeFiles[filePath]; exists {
// 更新现有记录
record.ActiveTime += now.Sub(record.LastUpdated)
record.LastUpdated = now
} else {
// 创建新记录
fw.activeFiles[filePath] = &TimeRecord{
FilePath: filePath,
ActiveTime: 0,
LastUpdated: now,
}
}
}
3. Git钩子集成
package gtm
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func InstallGitHooks(repoPath string) error {
hookContent := `#!/bin/sh
gtm pre-commit
`
hookPath := filepath.Join(repoPath, ".git", "hooks", "pre-commit")
if err := os.WriteFile(hookPath, []byte(hookContent), 0755); err != nil {
return fmt.Errorf("failed to install pre-commit hook: %v", err)
}
return nil
}
func PreCommitHook() error {
// 在提交前记录时间数据
// 获取暂存区文件列表
cmd := exec.Command("git", "diff", "--name-only", "--cached")
output, err := cmd.Output()
if err != nil {
return err
}
// 处理文件列表并记录时间
// ...
return nil
}
4. 命令行接口
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "gtm",
Usage: "Git Time Metric - lightweight time tracking for Git",
Commands: []*cli.Command{
{
Name: "init",
Usage: "Initialize GTM in the current repository",
Action: func(c *cli.Context) error {
// 初始化逻辑
return nil
},
},
{
Name: "status",
Usage: "Show current time tracking status",
Action: func(c *cli.Context) error {
// 显示状态逻辑
return nil
},
},
{
Name: "report",
Usage: "Generate time tracking report",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "text",
Usage: "Report format (text, json, csv)",
},
},
Action: func(c *cli.Context) error {
// 生成报告逻辑
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
使用示例
- 初始化GTM
$ gtm init
GTM initialized in current repository.
Git hooks installed successfully.
-
正常工作(自动记录时间)
-
查看状态
$ gtm status
File Active Time
src/main.go 1h 23m
src/utils.go 45m
- 生成报告
$ gtm report --format=csv
file,time
src/main.go,1.38
src/utils.go,0.75
实现注意事项
- 性能考虑:文件监视要轻量级,避免影响开发体验
- 数据存储:使用简单的JSON文件存储时间数据
- Git集成:通过Git钩子自动触发时间记录
- 跨平台:确保在Windows、macOS和Linux上都能工作
- 配置选项:允许用户排除某些文件或目录
扩展功能
- IDE插件集成
- 团队时间统计
- 与项目管理工具(JIRA等)集成
- 可视化报告
这个实现提供了GTM的核心功能框架,你可以根据需要进一步扩展和完善。关键是要保持轻量级和无缝集成的特点,避免给开发者带来额外负担。