git-whence 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ff4eae5cbbb56bd17fdd8baa9a653bb45b68f70b1214358d6b595d1c5b8b829
4
- data.tar.gz: de12ddbebc457c3e72c324b17242787c96c5c4243868042d3ba199706d4f19e1
3
+ metadata.gz: d905ac8083db64392216de76354c9f4fdf082ec37654e6d17a5355c2d616091e
4
+ data.tar.gz: 80bcec14ef080aecc1b8ad0db28ba44d9eb273ce890cbd75e7bc76d21fac17f9
5
5
  SHA512:
6
- metadata.gz: 5a3193767c1a1cb822946ae91b9e4956294349669f32664364f62ea99806ad0af882e3313998bb2f8c3c5505c4a2daced0087af63858e0162fdca48a2a88902f
7
- data.tar.gz: 7c6c048d81234fdeae88b330d768d6485bb5a5dd18a3c3f923d49aa2ba99a5196a22a268203d76be7ffe4882fb08d1f14b54f43b579e9a301868bcab55ce9680
6
+ metadata.gz: 0d3626ddbb2c37530b32f152809ac02675f816df2457e1c339af15da06a0c87a75a1d67b7f2ca6820769eb55c286cee7385881ae92182b06772fe1f0aba8325e
7
+ data.tar.gz: a35ab59affb9244aed513371ad3ebdc2b0c9b29c060dd945b1d801a7578b18d19f8d4d0c89957e670587f60a6118d2074ad62d374097fd5c87cc5818226d03ee
@@ -1,5 +1,5 @@
1
1
  module Git
2
2
  module Whence
3
- VERSION = "0.2.1"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
data/lib/git/whence.rb CHANGED
@@ -3,6 +3,9 @@ require "optparse"
3
3
 
4
4
  module Git::Whence
5
5
  module CLI
6
+ SQUASH_REGEX = /\(#(\d+)\)$/
7
+ DEFAULT_BRANCHES = ["main", "master"]
8
+
6
9
  class << self
7
10
  def run(argv)
8
11
  options = parse_options(argv)
@@ -19,8 +22,7 @@ module Git::Whence
19
22
  show_commit(commit, options)
20
23
  1
21
24
  else
22
- merge = find_merge(commit)
23
- if merge
25
+ if merge = find_merge(commit)
24
26
  show_commit(merge, options)
25
27
  0
26
28
  else
@@ -44,7 +46,7 @@ module Git::Whence
44
46
  def show_commit(merge, options)
45
47
  info = sh("git show -s --oneline #{merge}").strip
46
48
  if options[:open]
47
- if pr = info[/Merge pull request #(\d+) from /, 1]
49
+ if pr = info[/Merge pull request #(\d+) from /, 1] || info[SQUASH_REGEX, 1]
48
50
  exec "open", "https://github.com/#{origin}/pull/#{pr}"
49
51
  else
50
52
  warn "Unable to find PR number in #{info}"
@@ -57,19 +59,23 @@ module Git::Whence
57
59
 
58
60
  # https://github.com/foo/bar or git@github.com:foo/bar.git -> foo/bar
59
61
  def origin
60
- repo = sh("git remote get-url origin").strip
62
+ repo = sh("git remote get-url origin").strip # TODO: read file instead
61
63
  repo.sub!(/\.git$/, "")
62
64
  repo.split(/[:\/]/).last(2).join("/")
63
65
  end
64
66
 
65
67
  def find_merge(commit)
66
- commit, merge = (
68
+ merge_commit, merge = (
67
69
  find_merge_simple(commit, "HEAD") ||
68
- find_merge_simple(commit, "master") ||
69
- find_merge_fuzzy(commit, "master")
70
+ find_merge_simple(commit, default_branch) ||
71
+ find_merge_fuzzy(commit, default_branch)
70
72
  )
71
73
 
72
- merge if merge && merge_include_commit?(merge, commit)
74
+ if merge && merge_include_commit?(merge, merge_commit)
75
+ merge
76
+ else
77
+ find_squash_merge(commit) # not very exact, so do this last ... ideally ask github api
78
+ end
73
79
  end
74
80
 
75
81
  def merge_include_commit?(merge, commit)
@@ -78,23 +84,45 @@ module Git::Whence
78
84
  end
79
85
 
80
86
  def find_merge_fuzzy(commit, branch)
81
- if similar = find_similar(commit, branch)
87
+ if (similar = find_similar(commit, branch))
82
88
  find_merge_simple(similar, branch)
83
89
  end
84
90
  end
85
91
 
92
+ def default_branch
93
+ @default_branch ||= remote_default_branch || local_default_branch
94
+ end
95
+
96
+ def remote_default_branch
97
+ remotes = sh("git remote").split("\n")
98
+ return nil if remotes.empty?
99
+ preferred = (remotes.include?("origin") ? "origin" : remotes.first)
100
+ folder = ".git/refs/remotes/#{preferred}"
101
+ (Dir["#{folder}/*"].map { |f| f.sub("#{folder}/", "") } & DEFAULT_BRANCHES).sort.first
102
+ end
103
+
104
+ # guess default branch by last changed commonly used default branch or current branch
105
+ def local_default_branch
106
+ branches = sh("git branch --sort=-committerdate").split("\n").map { |br| br.split(" ").last }
107
+ (branches & DEFAULT_BRANCHES).first || sh("git symbolic-ref HEAD").strip.sub("refs/heads/", "")
108
+ end
109
+
110
+ def find_squash_merge(commit)
111
+ commit if sh("git show -s --format='%s' #{commit}") =~ SQUASH_REGEX
112
+ end
113
+
86
114
  def find_similar(commit, branch)
87
115
  month = 30 * 24 * 60 * 60
88
116
  time, search = sh("git show -s --format='%ct %an %s' #{commit}").strip.split(" ", 2)
89
117
  time = time.to_i
90
118
  same = sh("git log #{branch} --pretty=format:'%H %an %s' --before #{time + month} --after #{time - month}")
91
119
  found = same.split("\n").map { |x| x.split(" ", 2) }.detect { |_, message| message == search }
92
- found && found.first
120
+ found&.first
93
121
  end
94
122
 
95
123
  def find_merge_simple(commit, branch)
96
- result = sh "git log #{commit}..#{branch} --ancestry-path --merges --pretty='%H' 2>/dev/null | tail -n 1"
97
- [commit, result.strip] unless result.strip.empty?
124
+ result = sh("git log #{commit}..#{branch} --ancestry-path --merges --pretty='%H' 2>/dev/null | tail -n 1").chomp
125
+ [commit, result] unless result.empty?
98
126
  end
99
127
 
100
128
  def sh(command)
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-whence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-22 00:00:00.000000000 Z
11
+ date: 2024-07-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email: michael@grosser.it
15
15
  executables:
16
16
  - git-whence
@@ -25,7 +25,7 @@ homepage: https://github.com/grosser/git-whence
25
25
  licenses:
26
26
  - MIT
27
27
  metadata: {}
28
- post_install_message:
28
+ post_install_message:
29
29
  rdoc_options: []
30
30
  require_paths:
31
31
  - lib
@@ -33,16 +33,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: '0'
36
+ version: '2.3'
37
37
  required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubyforge_project:
44
- rubygems_version: 2.7.6
45
- signing_key:
43
+ rubygems_version: 3.4.10
44
+ signing_key:
46
45
  specification_version: 4
47
46
  summary: Find the merge and pull request a commit came from + find cherry-picks
48
47
  test_files: []