git-story 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/git-story +62 -37
  2. metadata +2 -2
data/lib/git-story CHANGED
@@ -4,67 +4,92 @@ require 'set'
4
4
  require 'grit'
5
5
  require 'trollop'
6
6
 
7
- def print(commit, suffix)
8
- puts "#{commit.committed_date} #{commit.sha} #{commit.message.strip.split("\n").first} #{commit.committer} #{suffix}"
7
+ def describe(commit, suffix)
8
+ "#{commit.committed_date} #{commit.sha} #{commit.message.strip.split("\n").first} #{commit.committer} #{suffix}\n"
9
9
  end
10
10
 
11
- def print_story(current_commit, head, merge_commits, merge_commits_directions)
12
- puts "\n"
13
- puts "Possible path"
14
- puts "-------------"
11
+ def get_travel_path(current_commit, head, merge_commits, merge_commits_directions)
12
+ travel_path = ''
13
+ travel_path << "\n"
14
+ travel_path << "Possible path\n"
15
+ travel_path << "-------------\n"
15
16
 
16
- print(current_commit, 'START')
17
+ travel_path << describe(current_commit, 'START')
17
18
  i = (merge_commits.length - 1)
18
19
  while(i >= 0)
19
- #direction 0 indicates there was no branch change
20
- print(merge_commits[i], 'MERGE') if(merge_commits_directions[i] != 0)
20
+ # direction 0 indicates there was no branch change
21
+ travel_path << describe(merge_commits[i], 'MERGE') if(merge_commits_directions[i] != 0)
21
22
  i -= 1
22
23
  end
23
- print(head, 'HEAD')
24
+ travel_path << describe(head, 'HEAD')
25
+ travel_path
24
26
  end
25
27
 
26
28
  def story(searched_sha, branch, repo)
29
+ travel_paths = Set.new
30
+ encountered_shas = Set.new
31
+ nb_commits = (`git rev-list #{branch} | wc -l`).chomp!.to_i
32
+
27
33
  merge_commits = []
28
34
  merge_commits_directions = []
29
- processed_shas = Set.new
30
35
 
31
36
  head = repo.commits(branch).first
32
37
  next_commit = head
33
- finished = false
34
38
 
35
- while(!finished)
39
+ while(!next_commit.nil?)
40
+ # move to next commit
36
41
  current_commit = next_commit
37
42
 
38
- #print travel path if the current commit is the one we're looking for
39
- print_story(current_commit, head, merge_commits, merge_commits_directions) if(current_commit.sha == searched_sha)
43
+ # current commit is the one we're looking for
44
+ # collect travel paths
45
+ if(current_commit.sha == searched_sha)
46
+ travel_paths << get_travel_path(current_commit, head, merge_commits, merge_commits_directions)
47
+ end
40
48
 
41
- #go back to previous merge commit and go to its next parent
42
- if(processed_shas.include?(current_commit.sha) or current_commit.parents.empty? or current_commit.sha == searched_sha)
43
- done = false
44
- while(!done and merge_commits.any?)
45
- merge_commits_directions[merge_commits_directions.length - 1] += 1
46
- if(merge_commits_directions.last == merge_commits.last.parents.length)
47
- processed_shas << merge_commits.pop.sha
48
- merge_commits_directions.pop
49
- else
50
- done = true
51
- end
52
- end
53
- finished = merge_commits.empty?
54
- next_commit = merge_commits.last.parents[merge_commits_directions.last] if !finished
49
+ # current commit has no parents
50
+ # add sha to set of encountered shas and jump back to last merge commit
51
+ if(current_commit.parents.length == 0)
52
+ encountered_shas << current_commit.sha
53
+ next_commit = merge_commits.last
55
54
 
56
- #merge commit - update merge commits info and go to first parent of this merge commit
57
- elsif(current_commit.parents.length > 1)
58
- merge_commits << current_commit
59
- merge_commits_directions << 0
55
+ # current commit only has 1 parent
56
+ # add sha to set of encountered shas and go to parent
57
+ elsif(current_commit.parents.length == 1)
58
+ encountered_shas << current_commit.sha
60
59
  next_commit = current_commit.parents[0]
61
60
 
62
- #standard commit - add current commit to set of processed commits and go to first parent
63
- else
64
- processed_shas << current_commit.sha
65
- next_commit = current_commit.parents[0]
61
+ # current commit is merge commit
62
+ else(current_commit.parents.length > 1)
63
+
64
+ # newly encountered merge commit
65
+ # add sha to set of encountered shas, update merge commits and go to first parent
66
+ if(!encountered_shas.include?(current_commit.sha))
67
+ encountered_shas << current_commit.sha
68
+ merge_commits << current_commit
69
+ merge_commits_directions << 0
70
+ next_commit = merge_commits.last.parents[merge_commits_directions.last]
71
+
72
+ # previously encountered merge commit
73
+ # go to next parent if possible, otherwise jump back to last merge commit
74
+ else
75
+ if(merge_commits_directions.last < (merge_commits.last.parents.length - 1))
76
+ merge_commits_directions[merge_commits_directions.length - 1] += 1
77
+ next_commit = merge_commits.last.parents[merge_commits_directions.last]
78
+ else
79
+ merge_commits.pop
80
+ merge_commits_directions.pop
81
+ next_commit = merge_commits.last
82
+ end
83
+ end
66
84
  end
85
+
86
+ # display progress
87
+ puts "progress: #{encountered_shas.length} / #{nb_commits}"
67
88
  end
89
+
90
+ # display travel paths
91
+ travel_paths.each { |travel_path| puts travel_path }
92
+ puts "\n"
68
93
  end
69
94
 
70
95
  opts = Trollop::options do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-story
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-27 00:00:00.000000000 Z
12
+ date: 2012-06-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grit