module Hoe::MercurialHelpers

def delete_extra_files( filelist )

## Delete the files in the given +filelist+ after confirming with the user.
def delete_extra_files( filelist )
	description = humanize_file_list( filelist, '	 ' )
	log "Files to delete:\n ", description
	ask_for_confirmation( "Really delete them?", false ) do
		filelist.each do |f|
			rm_rf( f, :verbose => true )
		end
	end
end

def edit_commit_log( logfile )

## Generate a commit log and invoke the user's editor on it.
def edit_commit_log( logfile )
	diff = make_commit_log()
	File.open( logfile, 'w' ) do |fh|
		fh.print( diff )
	end
	edit( logfile )
end

def get_current_rev

## Return the ID for the current rev
def get_current_rev
	id = read_command_output( 'hg', '-q', 'identify' )
	return id.chomp
end

def get_manifest

def get_manifest
	raw = read_command_output( 'hg', 'manifest' )
	return raw.split( $/ )
end

def get_numeric_rev

## Return the current numeric (local) rev number
def get_numeric_rev
	id = read_command_output( 'hg', '-q', 'identify', '-n' )
	return id.chomp[ /^(\d+)/, 1 ] || '0'
end

def get_repo_paths

## Read any remote repo paths known by the current repo and return them as a hash.
def get_repo_paths
	paths = {}
	pathspec = read_command_output( 'hg', 'paths' )
	pathspec.split.each_slice( 3 ) do |name, _, url|
		paths[ name ] = url
	end
	return paths
end

def get_tags

## Read the list of existing tags and return them as an Array
def get_tags
	taglist = read_command_output( 'hg', 'tags' )
	return taglist.split( /\n/ ).collect {|tag| tag[/^\S+/] }
end

def get_tip_info

## Get the 'tip' info and return it as a Hash
def get_tip_info
	data = read_command_output( 'hg', 'tip' )
	return YAML.load( data )
end

def get_uncommitted_files

## Return the list of files which are not of status 'clean'
def get_uncommitted_files
	list = read_command_output( 'hg', 'status', '-n', '--color', 'never' )
	list = list.split( /\n/ )
	trace "Changed files: %p" % [ list ]
	return list
end

def get_unknown_files

## Return the list of files which are of status 'unknown'
def get_unknown_files
	list = read_command_output( 'hg', 'status', '-un', '--color', 'never' )
	list = list.split( /\n/ )
	trace "New files: %p" % [ list ]
	return list
end

def hg_ignore_files( *pathnames )

## Add the list of +pathnames+ to the .hgignore list.
def hg_ignore_files( *pathnames )
	patterns = pathnames.flatten.collect do |path|
		'^' + Regexp.escape(path) + '$'
	end
	trace "Ignoring %d files." % [ pathnames.length ]
	IGNORE_FILE.open( File::CREAT|File::WRONLY|File::APPEND, 0644 ) do |fh|
		fh.puts( patterns )
	end
end

def make_changelog

## Generate a changelog.
def make_changelog
	log = read_command_output( 'hg', 'log', '--style', 'changelog' )
	return log
end

def make_commit_log

## returns the diff as-is, but will (someday) do something better.
## Generate a commit log from a diff and return it as a String. At the moment it just
def make_commit_log
	diff = read_command_output( 'hg', 'diff' )
	fail "No differences." if diff.empty?
	return diff
end