Skip to content
Browse files

Accept a source root

  • Loading branch information...
1 parent 283f237 commit 6f839ece853e7db8c3a3e78c080ecf6d2610d014 @jferris jferris committed Sep 28, 2010
Showing with 71 additions and 17 deletions.
  1. +43 −0 features/sync_nested_file.feature
  2. +11 −6 lib/trout/cli.rb
  3. +17 −11 lib/trout/managed_file.rb
View
43 features/sync_nested_file.feature
@@ -0,0 +1,43 @@
+Feature: sync a file nested within a directory the source repo
+
+ Scenario: sync a nested file
+ Given a directory named "upstream_repo"
+ And a directory named "child_repo"
+ And a file named "upstream_repo/sub/dir/file" with:
+ """
+ contents
+ """
+ When I cd to "upstream_repo"
+ And I run "git init"
+ And I run "git add sub"
+ And I run "git commit -m 'Added file'"
+ And I cd to "../child_repo"
+ And I run "trout checkout --source-root sub/dir file ../upstream_repo"
+ Then the output should contain:
+ """
+ Checked out file from ../upstream_repo.
+ """
+ And I run "cat file"
+ Then the output should contain:
+ """
+ contents
+ """
+ When I cd to "../upstream_repo"
+ And I write to "sub/dir/file" with:
+ """
+ new contents
+ """
+ When I run "git add sub"
+ And I run "git commit -m 'updated file'"
+ And I cd to "../child_repo"
+ When I run "trout update file"
+ Then the output should contain:
+ """
+ Merged changes to file.
+ """
+ When I run "cat file"
+ Then the output should contain:
+ """
+ new contents
+ """
+
View
17 lib/trout/cli.rb
@@ -8,19 +8,21 @@ def self.run(arguments)
new(arguments).run
end
- attr_accessor :command, :option_parser, :arguments, :managed_files
+ attr_accessor :command, :option_parser, :arguments, :managed_files, :file_attributes
def initialize(arguments)
- self.arguments = arguments
- self.option_parser = parse_options
- self.managed_files = VersionList.new('.trout')
+ self.arguments = arguments
+ self.file_attributes = {}
+ self.option_parser = parse_options
+ self.managed_files = VersionList.new('.trout')
end
def run
case command
when 'checkout'
- file = ManagedFile.new(:filename => next_argument,
- :git_url => next_argument)
+ file_attributes[:filename] = next_argument
+ file_attributes[:git_url] = next_argument
+ file = ManagedFile.new(file_attributes)
end_arguments
file.checkout
managed_files << file
@@ -52,6 +54,9 @@ def parse_options
parser.separator "Options:"
parser.separator ""
+ parser.on("-s", "--source-root PATH", "Path to the file in the source repository") do |path|
+ file_attributes[:source_root] = path
+ end
parser.on("-h", "--help", "View this help document")
parser.separator ""
View
28 lib/trout/managed_file.rb
@@ -2,12 +2,13 @@
module Trout
class ManagedFile
- attr_accessor :filename, :git_url, :version, :latest_version
+ attr_accessor :filename, :git_url, :version, :latest_version, :source_root
def initialize(attributes)
- self.filename = attributes[:filename]
- self.git_url = attributes[:git_url]
- self.version = attributes[:version]
+ self.filename = attributes[:filename]
+ self.git_url = attributes[:git_url]
+ self.version = attributes[:version]
+ self.source_root = attributes[:source_root] || '/'
end
def checkout
@@ -31,9 +32,10 @@ def update
end
def to_hash
- { :filename => filename,
- :git_url => git_url,
- :version => version }
+ { :filename => filename,
+ :git_url => git_url,
+ :version => version,
+ :source_root => source_root }
end
private
@@ -44,7 +46,7 @@ def clone_repository
end
def copy_to_destination
- FileUtils.cp(working('git', filename), filename)
+ FileUtils.cp(working('git', source_path), filename)
self.version = checked_out_version
end
@@ -53,16 +55,16 @@ def merge_to_destination
at_last_update = working('at_last_update')
merge = working('merge')
- FileUtils.cp(working('git', filename), upstream)
+ FileUtils.cp(working('git', source_path), upstream)
checkout_last_version
- FileUtils.cp(working('git', filename), at_last_update)
+ FileUtils.cp(working('git', source_path), at_last_update)
enforce_newline(upstream)
enforce_newline(at_last_update)
enforce_newline(filename)
- run("diff3 -mX #{filename} #{at_last_update} #{upstream} > #{merge}")
+ run("diff3 -mE #{filename} #{at_last_update} #{upstream} > #{merge}")
FileUtils.mv(merge, filename)
self.version = latest_version
@@ -116,5 +118,9 @@ def run_or_fail(command)
end
output
end
+
+ def source_path
+ File.join(source_root, filename)
+ end
end
end

0 comments on commit 6f839ec

Please sign in to comment.
Something went wrong with that request. Please try again.