Class: Releasinator::GitUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/git_util.rb

Class Method Summary collapse

Class Method Details

.all_filesObject



23
24
25
26
# File 'lib/git_util.rb', line 23

def self.all_files()
  current_branch = get_current_branch()
  CommandProcessor.command("git ls-tree --name-only -r #{current_branch}")
end

.cachedObject



66
67
68
# File 'lib/git_util.rb', line 66

def self.cached
  CommandProcessor.command("git diff --cached")
end

.checkout(branch_name) ⇒ Object



88
89
90
91
92
# File 'lib/git_util.rb', line 88

def self.checkout(branch_name)
  if get_current_branch != branch_name
    CommandProcessor.command("git checkout #{branch_name}")
  end
end

.confirm_tag_overwrite(new_tag) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/git_util.rb', line 94

def self.confirm_tag_overwrite(new_tag)
  tag_results = CommandProcessor.command('git tag -l')
  tag_results.split.each do |existing_tag|
    if existing_tag == new_tag
      Printer.check_proceed("Tag #{existing_tag} already present. Overwrite tag #{existing_tag}?", "Tag #{existing_tag} not overwritten.")
    end
  end
end

.delete_branch(branch_name) ⇒ Object



74
75
76
77
78
# File 'lib/git_util.rb', line 74

def self.delete_branch(branch_name)
  if has_branch? branch_name
    CommandProcessor.command("git branch -D #{branch_name}")
  end
end

.detached?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/git_util.rb', line 54

def self.detached?
  "" == CommandProcessor.command("git symbolic-ref --short -q HEAD | cat").strip
end

.diffObject



62
63
64
# File 'lib/git_util.rb', line 62

def self.diff
  CommandProcessor.command("git diff")
end

.exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/git_util.rb', line 17

def self.exist?(path)
  current_branch = get_current_branch()
  # grep is case sensitive, which is what we want.  Piped to cat so the grep error code is ignored.
  "" != CommandProcessor.command("git ls-tree --name-only -r #{current_branch} | grep ^#{path}$ | cat")
end

.fetchObject



13
14
15
# File 'lib/git_util.rb', line 13

def self.fetch()
  CommandProcessor.command("git fetch origin --prune --recurse-submodules -j9")
end

.get_current_branchObject



50
51
52
# File 'lib/git_util.rb', line 50

def self.get_current_branch
  CommandProcessor.command("git symbolic-ref --short HEAD").strip
end

.get_local_branch_sha1(branch_name) ⇒ Object



107
108
109
# File 'lib/git_util.rb', line 107

def self.get_local_branch_sha1(branch_name)
  CommandProcessor.command("git rev-parse --verify #{branch_name}").strip
end

.get_local_head_sha1Object



103
104
105
# File 'lib/git_util.rb', line 103

def self.get_local_head_sha1
  CommandProcessor.command("git rev-parse --verify head").strip
end

.get_remote_branch_sha1(branch_name) ⇒ Object



111
112
113
# File 'lib/git_util.rb', line 111

def self.get_remote_branch_sha1(branch_name)
  CommandProcessor.command("git rev-parse --verify origin/#{branch_name}").strip
end

.has_branch?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/git_util.rb', line 80

def self.has_branch?(branch_name)
  "" != CommandProcessor.command("git branch --list #{branch_name}").strip
end

.has_remote_branch?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/git_util.rb', line 84

def self.has_remote_branch?(branch_name)
  "" != CommandProcessor.command("git branch --list -r #{branch_name}").strip
end

.init_gh_pagesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/git_util.rb', line 126

def self.init_gh_pages
  if !has_branch? "gh-pages"
    if has_remote_branch? "origin/gh-pages"
      checkout("gh-pages")
    else
      CommandProcessor.command("git checkout --orphan gh-pages")
      CommandProcessor.command("GLOBIGNORE='.git' git rm -rf *")
      #http://stackoverflow.com/questions/19363795/git-rm-doesnt-remove-all-files-in-one-go
      CommandProcessor.command("GLOBIGNORE='.git' git rm -rf *")
      CommandProcessor.command("touch README.md")
      CommandProcessor.command("git add .")
      CommandProcessor.command("git commit -am \"Initial gh-pages commit\"")
      CommandProcessor.command("git push -u origin gh-pages")
    end
  end
end

.is_clean_git?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/git_util.rb', line 45

def self.is_clean_git?
  any_changes = CommandProcessor.command("git status --porcelain")
  '' == any_changes
end

.move(old_path, new_path) ⇒ Object



28
29
30
31
# File 'lib/git_util.rb', line 28

def self.move(old_path, new_path)
  puts "Renaming #{old_path} to #{new_path}".yellow
  CommandProcessor.command("git mv -f #{old_path} #{new_path}")
end

.push_branch(branch_name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/git_util.rb', line 33

def self.push_branch(branch_name)
  checkout(branch_name)
  fetch()
  # always merge to include any extra commits added during release process
  CommandProcessor.command("git merge origin/#{branch_name} --no-edit")
  CommandProcessor.command("git push origin #{branch_name}")
end

.push_tag(tag_name) ⇒ Object



41
42
43
# File 'lib/git_util.rb', line 41

def self.push_tag(tag_name)
  CommandProcessor.command("git push origin #{tag_name}")
end

.repo_urlObject



70
71
72
# File 'lib/git_util.rb', line 70

def self.repo_url
  CommandProcessor.command("git config --get remote.origin.url").strip
end

.reset_repo(branch_name) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/git_util.rb', line 5

def self.reset_repo(branch_name)
  # resets the repo to a clean state
  checkout(branch_name)
  fetch()
  CommandProcessor.command("git reset --hard origin/#{branch_name}")
  CommandProcessor.command("git clean -x -d -f")
end

.tag(new_tag, changelog) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/git_util.rb', line 115

def self.tag(new_tag, changelog)
  confirm_tag_overwrite(new_tag)
  puts "tagging with changelog: \n\n#{changelog}\n".yellow
  changelog_tempfile = Tempfile.new("#{new_tag}.changelog")
  changelog_tempfile.write(changelog)
  changelog_tempfile.close
  # include changelog in annotated tag
  CommandProcessor.command("git tag -a -f #{new_tag} -F #{changelog_tempfile.path}")
  changelog_tempfile.unlink
end

.untracked_filesObject



58
59
60
# File 'lib/git_util.rb', line 58

def self.untracked_files
  CommandProcessor.command("git ls-files --others --exclude-standard")
end