From b389247c029b21f5e85abb5896d2cf22230c9cb1 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 10:06:56 -0400 Subject: Use Commit#show instead of Commits#show to view a single commit Commits#show (plural) is going to be for showing commit history on a specific path. --- lib/gitlab/markdown.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 9201003e2e1..9eb35b84d42 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -174,7 +174,7 @@ module Gitlab def reference_commit(identifier) if commit = @project.commit(identifier) - link_to(identifier, project_commit_path(@project, id: commit.id), html_options.merge(title: CommitDecorator.new(commit).link_title, class: "gfm gfm-commit #{html_options[:class]}")) + link_to(identifier, project_commit_path(@project, commit), html_options.merge(title: CommitDecorator.new(commit).link_title, class: "gfm gfm-commit #{html_options[:class]}")) end end end -- cgit v1.2.1 From 567767bcf2af2c330c46fde820101fcf847e852f Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 10:44:36 -0400 Subject: Add ref_extractor helper module for upcoming controllers --- lib/ref_extractor.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/ref_extractor.rb (limited to 'lib') diff --git a/lib/ref_extractor.rb b/lib/ref_extractor.rb new file mode 100644 index 00000000000..d3f02d6ec3e --- /dev/null +++ b/lib/ref_extractor.rb @@ -0,0 +1,64 @@ +# Module providing an extract_ref method for controllers working with Git +# tree-ish + path params +# +# Given a string containing both a Git ref - such as a branch or tag - and a +# filesystem path joined by forward slashes, attempts to separate the two. +# +# Expects a @project instance variable to contain the active project. Used to +# check the input against a list of valid repository refs. +# +# Examples +# +# # No @project available +# extract_ref('master') +# # => ['', ''] +# +# extract_ref('master') +# # => ['master', '/'] +# +# extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") +# # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', '/CHANGELOG'] +# +# extract_ref("v2.0.0/README.md") +# # => ['v2.0.0', '/README.md'] +# +# extract_ref('issues/1234/app/models/project.rb') +# # => ['issues/1234', '/app/models/project.rb'] +# +# # Given an invalid branch, we fall back to just splitting on the first slash +# extract_ref('non/existent/branch/README.md') +# # => ['non', '/existent/branch/README.md'] +# +# Returns an Array where the first value is the tree-ish and the second is the +# path +module RefExtractor + def extract_ref(input) + pair = ['', ''] + + return pair unless @project + + if input.match(/^([[:alnum:]]{40})(.+)/) + # If the ref appears to be a SHA, we're done, just split the string + pair = $~.captures + else + # Append a trailing slash if we only get a ref and no file path + id = input + id += '/' unless id.include?('/') + + # Otherwise, attempt to detect the ref using a list of the project's + # branches and tags + valid_refs = @project.branches + @project.tags + valid_refs.select! { |v| id.start_with?("#{v}/") } + + if valid_refs.length != 1 + # No exact ref match, so just try our best + pair = id.match(/([^\/]+)(.+)/).captures + else + # Partition the string into the ref and the path, ignoring the empty first value + pair = id.partition(valid_refs.first)[1..-1] + end + end + + pair + end +end -- cgit v1.2.1 From 37f0b600bc9a994136791e6838b3228c56f909b2 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 13:33:04 -0400 Subject: Another RefExtractor refactor --- lib/ref_extractor.rb | 74 ++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/ref_extractor.rb b/lib/ref_extractor.rb index d3f02d6ec3e..d7d446b1e62 100644 --- a/lib/ref_extractor.rb +++ b/lib/ref_extractor.rb @@ -1,37 +1,39 @@ # Module providing an extract_ref method for controllers working with Git # tree-ish + path params -# -# Given a string containing both a Git ref - such as a branch or tag - and a -# filesystem path joined by forward slashes, attempts to separate the two. -# -# Expects a @project instance variable to contain the active project. Used to -# check the input against a list of valid repository refs. -# -# Examples -# -# # No @project available -# extract_ref('master') -# # => ['', ''] -# -# extract_ref('master') -# # => ['master', '/'] -# -# extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") -# # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', '/CHANGELOG'] -# -# extract_ref("v2.0.0/README.md") -# # => ['v2.0.0', '/README.md'] -# -# extract_ref('issues/1234/app/models/project.rb') -# # => ['issues/1234', '/app/models/project.rb'] -# -# # Given an invalid branch, we fall back to just splitting on the first slash -# extract_ref('non/existent/branch/README.md') -# # => ['non', '/existent/branch/README.md'] -# -# Returns an Array where the first value is the tree-ish and the second is the -# path module RefExtractor + # Thrown when given an invalid path + class InvalidPathError < StandardError; end + + # Given a string containing both a Git ref - such as a branch or tag - and a + # filesystem path joined by forward slashes, attempts to separate the two. + # + # Expects a @project instance variable to contain the active project. Used to + # check the input against a list of valid repository refs. + # + # Examples + # + # # No @project available + # extract_ref('master') + # # => ['', ''] + # + # extract_ref('master') + # # => ['master', ''] + # + # extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") + # # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG'] + # + # extract_ref("v2.0.0/README.md") + # # => ['v2.0.0', 'README.md'] + # + # extract_ref('issues/1234/app/models/project.rb') + # # => ['issues/1234', 'app/models/project.rb'] + # + # # Given an invalid branch, we fall back to just splitting on the first slash + # extract_ref('non/existent/branch/README.md') + # # => ['non', 'existent/branch/README.md'] + # + # Returns an Array where the first value is the tree-ish and the second is the + # path def extract_ref(input) pair = ['', ''] @@ -41,24 +43,28 @@ module RefExtractor # If the ref appears to be a SHA, we're done, just split the string pair = $~.captures else + # Otherwise, attempt to detect the ref using a list of the project's + # branches and tags + # Append a trailing slash if we only get a ref and no file path id = input id += '/' unless id.include?('/') - # Otherwise, attempt to detect the ref using a list of the project's - # branches and tags valid_refs = @project.branches + @project.tags valid_refs.select! { |v| id.start_with?("#{v}/") } if valid_refs.length != 1 # No exact ref match, so just try our best - pair = id.match(/([^\/]+)(.+)/).captures + pair = id.match(/([^\/]+)(.*)/).captures else # Partition the string into the ref and the path, ignoring the empty first value pair = id.partition(valid_refs.first)[1..-1] end end + # Remove leading slash from path + pair[1].gsub!(/^\//, '') + pair end end -- cgit v1.2.1 From 398ba6f1bb60f176444dedc7b26188e08b920f54 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 14:24:31 -0400 Subject: DRY up Blame, Blob and Tree controllers --- lib/ref_extractor.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib') diff --git a/lib/ref_extractor.rb b/lib/ref_extractor.rb index d7d446b1e62..b9c0291761d 100644 --- a/lib/ref_extractor.rb +++ b/lib/ref_extractor.rb @@ -67,4 +67,31 @@ module RefExtractor pair end + + # Assigns common instance variables for views working with Git tree-ish objects + # + # Assignments are: + # + # - @id - A string representing the joined ref and path + # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) + # - @path - A string representing the filesystem path + # - @commit - A CommitDecorator representing the commit from the given ref + # - @tree - A TreeDecorator representing the tree at the given ref/path + # + # Automatically renders `not_found!` if a valid tree could not be resolved + # (e.g., when a user inserts an invalid path or ref). + def assign_ref_vars + @ref, @path = extract_ref(params[:id]) + + @id = File.join(@ref, @path) + + @commit = CommitDecorator.decorate(@project.commit(@ref)) + + @tree = Tree.new(@commit.tree, @project, @ref, @path) + @tree = TreeDecorator.new(@tree) + + raise InvalidPathError if @tree.invalid? + rescue NoMethodError, InvalidPathError + not_found! + end end -- cgit v1.2.1 From a8ea8d98a4f88a292289ddfedef4358033b68ec0 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 20 Sep 2012 13:25:28 -0400 Subject: Update RefExtractor to handle atom feeds --- lib/ref_extractor.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ref_extractor.rb b/lib/ref_extractor.rb index b9c0291761d..1db74b9513e 100644 --- a/lib/ref_extractor.rb +++ b/lib/ref_extractor.rb @@ -1,7 +1,7 @@ # Module providing an extract_ref method for controllers working with Git # tree-ish + path params module RefExtractor - # Thrown when given an invalid path + # Raised when given an invalid path class InvalidPathError < StandardError; end # Given a string containing both a Git ref - such as a branch or tag - and a @@ -81,6 +81,12 @@ module RefExtractor # Automatically renders `not_found!` if a valid tree could not be resolved # (e.g., when a user inserts an invalid path or ref). def assign_ref_vars + # Handle formats embedded in the id + if params[:id].ends_with?('.atom') + params[:id].gsub!(/\.atom$/, '') + request.format = :atom + end + @ref, @path = extract_ref(params[:id]) @id = File.join(@ref, @path) -- cgit v1.2.1 From a1e68a91205186287f21fb5fd1669acebcd7e79e Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 20 Sep 2012 13:55:14 -0400 Subject: Rename RefExtractor to ExtractsPath Update docs a bit --- lib/extracts_path.rb | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ref_extractor.rb | 103 ---------------------------------------------- 2 files changed, 114 insertions(+), 103 deletions(-) create mode 100644 lib/extracts_path.rb delete mode 100644 lib/ref_extractor.rb (limited to 'lib') diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb new file mode 100644 index 00000000000..6648ffdc9a3 --- /dev/null +++ b/lib/extracts_path.rb @@ -0,0 +1,114 @@ +# Module providing methods for dealing with separating a tree-ish string and a +# file path string when combined in a request parameter +module ExtractsPath + extend ActiveSupport::Concern + + # Raised when given an invalid file path + class InvalidPathError < StandardError; end + + included do + if respond_to?(:before_filter) + before_filter :assign_ref_vars + end + end + + # Given a string containing both a Git tree-ish, such as a branch or tag, and + # a filesystem path joined by forward slashes, attempts to separate the two. + # + # Expects a @project instance variable to contain the active project. This is + # used to check the input against a list of valid repository refs. + # + # Examples + # + # # No @project available + # extract_ref('master') + # # => ['', ''] + # + # extract_ref('master') + # # => ['master', ''] + # + # extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") + # # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG'] + # + # extract_ref("v2.0.0/README.md") + # # => ['v2.0.0', 'README.md'] + # + # extract_ref('issues/1234/app/models/project.rb') + # # => ['issues/1234', 'app/models/project.rb'] + # + # # Given an invalid branch, we fall back to just splitting on the first slash + # extract_ref('non/existent/branch/README.md') + # # => ['non', 'existent/branch/README.md'] + # + # Returns an Array where the first value is the tree-ish and the second is the + # path + def extract_ref(input) + pair = ['', ''] + + return pair unless @project + + if input.match(/^([[:alnum:]]{40})(.+)/) + # If the ref appears to be a SHA, we're done, just split the string + pair = $~.captures + else + # Otherwise, attempt to detect the ref using a list of the project's + # branches and tags + + # Append a trailing slash if we only get a ref and no file path + id = input + id += '/' unless id.include?('/') + + valid_refs = @project.branches + @project.tags + valid_refs.select! { |v| id.start_with?("#{v}/") } + + if valid_refs.length != 1 + # No exact ref match, so just try our best + pair = id.match(/([^\/]+)(.*)/).captures + else + # Partition the string into the ref and the path, ignoring the empty first value + pair = id.partition(valid_refs.first)[1..-1] + end + end + + # Remove leading slash from path + pair[1].gsub!(/^\//, '') + + pair + end + + # Assigns common instance variables for views working with Git tree-ish objects + # + # Assignments are: + # + # - @id - A string representing the joined ref and path + # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) + # - @path - A string representing the filesystem path + # - @commit - A CommitDecorator representing the commit from the given ref + # - @tree - A TreeDecorator representing the tree at the given ref/path + # + # If the :id parameter appears to be requesting a specific response format, + # that will be handled as well. + # + # Automatically renders `not_found!` if a valid tree path could not be + # resolved (e.g., when a user inserts an invalid path or ref). + def assign_ref_vars + # Handle formats embedded in the id + if params[:id].ends_with?('.atom') + params[:id].gsub!(/\.atom$/, '') + request.format = :atom + end + + @ref, @path = extract_ref(params[:id]) + + @id = File.join(@ref, @path) + + @commit = CommitDecorator.decorate(@project.commit(@ref)) + + @tree = Tree.new(@commit.tree, @project, @ref, @path) + @tree = TreeDecorator.new(@tree) + + raise InvalidPathError if @tree.invalid? + rescue NoMethodError, InvalidPathError + not_found! + end +end diff --git a/lib/ref_extractor.rb b/lib/ref_extractor.rb deleted file mode 100644 index 1db74b9513e..00000000000 --- a/lib/ref_extractor.rb +++ /dev/null @@ -1,103 +0,0 @@ -# Module providing an extract_ref method for controllers working with Git -# tree-ish + path params -module RefExtractor - # Raised when given an invalid path - class InvalidPathError < StandardError; end - - # Given a string containing both a Git ref - such as a branch or tag - and a - # filesystem path joined by forward slashes, attempts to separate the two. - # - # Expects a @project instance variable to contain the active project. Used to - # check the input against a list of valid repository refs. - # - # Examples - # - # # No @project available - # extract_ref('master') - # # => ['', ''] - # - # extract_ref('master') - # # => ['master', ''] - # - # extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") - # # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG'] - # - # extract_ref("v2.0.0/README.md") - # # => ['v2.0.0', 'README.md'] - # - # extract_ref('issues/1234/app/models/project.rb') - # # => ['issues/1234', 'app/models/project.rb'] - # - # # Given an invalid branch, we fall back to just splitting on the first slash - # extract_ref('non/existent/branch/README.md') - # # => ['non', 'existent/branch/README.md'] - # - # Returns an Array where the first value is the tree-ish and the second is the - # path - def extract_ref(input) - pair = ['', ''] - - return pair unless @project - - if input.match(/^([[:alnum:]]{40})(.+)/) - # If the ref appears to be a SHA, we're done, just split the string - pair = $~.captures - else - # Otherwise, attempt to detect the ref using a list of the project's - # branches and tags - - # Append a trailing slash if we only get a ref and no file path - id = input - id += '/' unless id.include?('/') - - valid_refs = @project.branches + @project.tags - valid_refs.select! { |v| id.start_with?("#{v}/") } - - if valid_refs.length != 1 - # No exact ref match, so just try our best - pair = id.match(/([^\/]+)(.*)/).captures - else - # Partition the string into the ref and the path, ignoring the empty first value - pair = id.partition(valid_refs.first)[1..-1] - end - end - - # Remove leading slash from path - pair[1].gsub!(/^\//, '') - - pair - end - - # Assigns common instance variables for views working with Git tree-ish objects - # - # Assignments are: - # - # - @id - A string representing the joined ref and path - # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) - # - @path - A string representing the filesystem path - # - @commit - A CommitDecorator representing the commit from the given ref - # - @tree - A TreeDecorator representing the tree at the given ref/path - # - # Automatically renders `not_found!` if a valid tree could not be resolved - # (e.g., when a user inserts an invalid path or ref). - def assign_ref_vars - # Handle formats embedded in the id - if params[:id].ends_with?('.atom') - params[:id].gsub!(/\.atom$/, '') - request.format = :atom - end - - @ref, @path = extract_ref(params[:id]) - - @id = File.join(@ref, @path) - - @commit = CommitDecorator.decorate(@project.commit(@ref)) - - @tree = Tree.new(@commit.tree, @project, @ref, @path) - @tree = TreeDecorator.new(@tree) - - raise InvalidPathError if @tree.invalid? - rescue NoMethodError, InvalidPathError - not_found! - end -end -- cgit v1.2.1 From 835be5b1bf25fc0509ef1b1f7f7f2e3a344ff231 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 25 Sep 2012 18:20:52 -0400 Subject: ExtractPaths - Only call assign_ref_vars on show action --- lib/extracts_path.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb index 6648ffdc9a3..f36dae5289b 100644 --- a/lib/extracts_path.rb +++ b/lib/extracts_path.rb @@ -8,7 +8,7 @@ module ExtractsPath included do if respond_to?(:before_filter) - before_filter :assign_ref_vars + before_filter :assign_ref_vars, only: [:show] end end -- cgit v1.2.1 From afc4a75499b6678a643e6b62f703f8e7e1eb0f0a Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 26 Sep 2012 14:52:01 -0400 Subject: Use Rails.root.join where appropriate --- lib/gitlab/backend/gitolite_config.rb | 4 ++-- lib/gitlab/logger.rb | 2 +- lib/gitlab/merge.rb | 4 ++-- lib/gitlab/satellite.rb | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/gitolite_config.rb b/lib/gitlab/backend/gitolite_config.rb index f51e8efc370..ffe15fb1123 100644 --- a/lib/gitlab/backend/gitolite_config.rb +++ b/lib/gitlab/backend/gitolite_config.rb @@ -10,7 +10,7 @@ module Gitlab attr_reader :config_tmp_dir, :ga_repo, :conf def config_tmp_dir - @config_tmp_dir ||= File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") + @config_tmp_dir ||= Rails.root.join('tmp',"gitlabhq-gitolite-#{Time.now.to_i}") end def ga_repo @@ -19,7 +19,7 @@ module Gitlab def apply Timeout::timeout(30) do - File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| + File.open(Rails.root.join('tmp', "gitlabhq-gitolite.lock"), "w+") do |f| begin # Set exclusive lock # to prevent race condition diff --git a/lib/gitlab/logger.rb b/lib/gitlab/logger.rb index 9405163dced..cf9a4c4afa2 100644 --- a/lib/gitlab/logger.rb +++ b/lib/gitlab/logger.rb @@ -15,7 +15,7 @@ module Gitlab end def self.build - new(File.join(Rails.root, "log", file_name)) + new(Rails.root.join("log", file_name)) end end end diff --git a/lib/gitlab/merge.rb b/lib/gitlab/merge.rb index 180135745f8..de8e737a8f3 100644 --- a/lib/gitlab/merge.rb +++ b/lib/gitlab/merge.rb @@ -28,7 +28,7 @@ module Gitlab def process Grit::Git.with_timeout(30.seconds) do - lock_file = File.join(Rails.root, "tmp", "merge_repo_#{project.path}.lock") + lock_file = Rails.root.join("tmp", "merge_repo_#{project.path}.lock") File.open(lock_file, "w+") do |f| f.flock(File::LOCK_EX) @@ -36,7 +36,7 @@ module Gitlab unless project.satellite.exists? raise "You should run: rake gitlab:app:enable_automerge" end - + project.satellite.clear Dir.chdir(project.satellite.path) do diff --git a/lib/gitlab/satellite.rb b/lib/gitlab/satellite.rb index 4bcbfe8db88..9d8dfb8e0a4 100644 --- a/lib/gitlab/satellite.rb +++ b/lib/gitlab/satellite.rb @@ -1,6 +1,6 @@ module Gitlab class Satellite - + PARKING_BRANCH = "__parking_branch" attr_accessor :project @@ -14,7 +14,7 @@ module Gitlab end def path - File.join(Rails.root, "tmp", "repo_satellites", project.path) + Rails.root.join("tmp", "repo_satellites", project.path) end def exists? @@ -36,6 +36,6 @@ module Gitlab end end end - + end end -- cgit v1.2.1