# Git Pivotal Tracker Integration # Copyright (c) 2013 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'git-pivotal-tracker-integration/util/shell' require 'git-pivotal-tracker-integration/util/util' # Utilities for dealing with Git class GitPivotalTrackerIntegration::Util::Git # Adds a Git hook to the current repository # # @param [String] name the name of the hook to add # @param [String] source the file to use as the source for the created hook # @param [Boolean] overwrite whether to overwrite the hook if it already exists # @return [void] def self.add_hook(name, source, overwrite = false) hooks_directory = File.join repository_root, '.git', 'hooks' hook = File.join hooks_directory, name if overwrite || !File.exist?(hook) print "Creating Git hook #{name}... " FileUtils.mkdir_p hooks_directory File.open(source, 'r') do |input| File.open(hook, 'w') do |output| output.write(input.read) output.chmod(0755) end end puts 'OK' end end # Returns the name of the currently checked out branch # # @return [String] the name of the currently checked out branch def self.branch_name GitPivotalTrackerIntegration::Util::Shell.exec('git branch').scan(/\* (.*)/)[0][0] end # Creates a branch with a given +name+. First pulls the current branch to # ensure that it is up to date and then creates and checks out the new # branch. If specified, sets branch-specific properties that are passed in. # # @param [String] name the name of the branch to create # @param [Boolean] print_messages whether to print messages # @return [void] def self.create_branch(name, print_messages = true) root_branch = branch_name root_remote = get_config KEY_REMOTE, :branch if print_messages; print "Pulling #{root_branch}... " end GitPivotalTrackerIntegration::Util::Shell.exec 'git pull --quiet --ff-only' if print_messages; puts 'OK' end if print_messages; print "Creating and checking out #{name}... " end GitPivotalTrackerIntegration::Util::Shell.exec "git checkout --quiet -b #{name}" set_config KEY_ROOT_BRANCH, root_branch, :branch set_config KEY_ROOT_REMOTE, root_remote, :branch if print_messages; puts 'OK' end end # Creates a commit with a given message. The commit includes all change # files. # # @param [String] message The commit message, which will be appended with # +[#