kk-git 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d9014b93ea9dd165e25f518850042be4d5fc9db34ae9dfef0c58ad6048f7854
4
- data.tar.gz: 74c6a6322b0750892c0d3368822906449c484423a053b1eb20735e8081fc79f5
3
+ metadata.gz: 12e086855631f99aae0e8a8aafcf859ffeae6a95713fbd20ef581bfd0c51f476
4
+ data.tar.gz: 7f9d679f4f1c02d6aac412ea5f444bbd4175feb382e2ece08a5fa99e20f190b6
5
5
  SHA512:
6
- metadata.gz: c722139e54af3754afa25b7301ab7b5dcf2a9a0e864d8e97345e18ab632e300b7dc74e3814b7e8a80f21935cf296925d5cbac0fd51393af95662dca6c71c7fa1
7
- data.tar.gz: ac29d630884974c8b857c858fd04cfe62570eabaf09bdd02b237cdcf78c46e7d076d7a34fdc02397dce099006759457f2b314e2fe07f7dc28cbbf49833638c85
6
+ metadata.gz: 8ca699fd9ac5d5632ccbfd50e5b49d65de882cc9abb0cdd59af5941fcaa46433c6db5703ac8a86ced40a3f44704775e3e14f1e78ab37406afce0bedff12a287a
7
+ data.tar.gz: a42a83b7ff556d866d26c630ecf8b11331d3b1ffb7621ecdff43b52eba25f951c0012c7cc918096707eefa6df3f24749ef9c1d2dfb22122b97c2e51ca53623aa
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'kk/git'
5
+ require 'open3'
6
+ require 'tempfile'
7
+
8
+ module KKGit
9
+ # Rake 集成:在任意项目的 Rakefile 中 `require 'kk/git/rake_tasks'` 即可注册任务。
10
+ #
11
+ # 说明:
12
+ # - 这些任务**不会 exit/abort**,以便在其它 task 中被 invoke。
13
+ # - 会把生成结果写入 `ENV['KK_GIT_COMMIT_MESSAGE']`,方便上层 task 复用。
14
+ module RakeTasks
15
+ def self.run_cmd(*cmd)
16
+ stdout, stderr, status = Open3.capture3(*cmd)
17
+ [stdout.to_s, stderr.to_s, status.success?]
18
+ end
19
+
20
+ def self.ensure_ok!(ok, title, stdout: nil, stderr: nil)
21
+ return if ok
22
+
23
+ msg = +"#{title} 失败"
24
+ msg << "\n#{stderr}" unless stderr.to_s.strip.empty?
25
+ msg << "\n#{stdout}" unless stdout.to_s.strip.empty?
26
+ raise msg
27
+ end
28
+
29
+ def self.current_branch
30
+ out, err, ok = run_cmd('git', 'rev-parse', '--abbrev-ref', 'HEAD')
31
+ ensure_ok!(ok, '获取当前分支', stdout: out, stderr: err)
32
+ out.strip
33
+ end
34
+
35
+ def self.working_tree_clean?
36
+ out, err, ok = run_cmd('git', 'status', '--porcelain')
37
+ ensure_ok!(ok, '检查 git 状态', stdout: out, stderr: err)
38
+ out.strip.empty?
39
+ end
40
+
41
+ def self.install!
42
+ extend Rake::DSL
43
+
44
+ namespace :git do
45
+ desc '根据暂存区变更生成 commit message(Conventional Commits)'
46
+ task :commit_message do
47
+ msg = KKGit::CommitMessage.generate(mode: :staged)
48
+ ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
49
+ puts msg if msg
50
+ end
51
+
52
+ desc '根据工作区变更生成 commit message(含 untracked)'
53
+ task :commit_message_worktree do
54
+ msg = KKGit::CommitMessage.generate(mode: :worktree)
55
+ ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
56
+ puts msg if msg
57
+ end
58
+
59
+ desc '合并暂存区+工作区变更生成 commit message'
60
+ task :commit_message_all do
61
+ msg = KKGit::CommitMessage.generate(mode: :all)
62
+ ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
63
+ puts msg if msg
64
+ end
65
+
66
+ desc '自动 add/commit/pull/push(基于 git:commit_message_all)'
67
+ task :auto_commit_push do
68
+ if working_tree_clean?
69
+ puts '没有变更需要提交'
70
+ next
71
+ end
72
+
73
+ remote = ENV.fetch('KK_GIT_REMOTE', 'origin')
74
+ branch = ENV.fetch('KK_GIT_BRANCH', current_branch)
75
+
76
+ # 1) add
77
+ out, err, ok = run_cmd('git', 'add', '.')
78
+ ensure_ok!(ok, 'git add', stdout: out, stderr: err)
79
+
80
+ # 2) 生成 commit message(允许重复 invoke)
81
+ Rake::Task['git:commit_message_all'].reenable
82
+ Rake::Task['git:commit_message_all'].invoke
83
+ commit_message = ENV['KK_GIT_COMMIT_MESSAGE'].to_s.strip
84
+ commit_message = "chore(repo): 更新项目文件\n\n#{Time.now}" if commit_message.empty?
85
+
86
+ # 3) commit(用临时文件避免转义问题)
87
+ Tempfile.create('commit_message') do |f|
88
+ f.write(commit_message)
89
+ f.flush
90
+ out, err, ok = run_cmd('git', 'commit', '-F', f.path)
91
+ # 没有 staged 变更时 git commit 会失败;这里给出更友好的提示
92
+ unless ok
93
+ if err.include?('nothing to commit') || out.include?('nothing to commit')
94
+ puts '没有暂存变更需要提交'
95
+ next
96
+ end
97
+ end
98
+ ensure_ok!(ok, 'git commit', stdout: out, stderr: err)
99
+ end
100
+
101
+ # 4) pull(默认使用 --ff-only,避免非交互环境进入合并流程)
102
+ pull_args = ENV.fetch('KK_GIT_PULL_ARGS', '--ff-only').split
103
+ out, err, ok = run_cmd('git', 'pull', *pull_args)
104
+ ensure_ok!(ok, 'git pull', stdout: out, stderr: err)
105
+
106
+ # 5) push
107
+ out, err, ok = run_cmd('git', 'push', remote, branch)
108
+ ensure_ok!(ok, 'git push', stdout: out, stderr: err)
109
+
110
+ puts "✅ 已推送: #{remote} #{branch}"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ KKGit::RakeTasks.install!
118
+
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KKGit
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.5'
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kk-git
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kk
@@ -22,6 +22,7 @@ files:
22
22
  - exe/kk-git
23
23
  - lib/kk/git.rb
24
24
  - lib/kk/git/commit_message.rb
25
+ - lib/kk/git/rake_tasks.rb
25
26
  - lib/kk/git/version.rb
26
27
  homepage: ''
27
28
  licenses: