Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2016-05-21 01:14:22 +0300
committerDouwe Maan <douwe@selenight.nl>2016-05-21 01:14:22 +0300
commitec86644545c1c2567dfaacb6d53d150a5dfa28d6 (patch)
tree1446dee382b4bb3fc80e2021041268c19ce751d8 /lib
parentbaccd1838c92a0d38d3f38b93e9911c97adfef21 (diff)
parentab96ca2bf1ae72817ff5cedf1792c8f7563ebdef (diff)
Merge branch 'zj-gitignore-dropdown'
Diffstat (limited to 'lib')
-rw-r--r--lib/api/api.rb1
-rw-r--r--lib/api/entities.rb8
-rw-r--r--lib/api/gitignores.rb29
-rw-r--r--lib/gitlab/gitignore.rb56
-rw-r--r--lib/tasks/gitlab/update_gitignore.rake46
5 files changed, 140 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb
index 360fb41a721..6cd909f6115 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -58,5 +58,6 @@ module API
mount ::API::Runners
mount ::API::Licenses
mount ::API::Subscriptions
+ mount ::API::Gitignores
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 8298e3ad34c..31491cf31dd 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -457,5 +457,13 @@ module API
expose(:limitations) { |license| license.meta['limitations'] }
expose :content
end
+
+ class GitignoresList < Grape::Entity
+ expose :name
+ end
+
+ class Gitignore < Grape::Entity
+ expose :name, :content
+ end
end
end
diff --git a/lib/api/gitignores.rb b/lib/api/gitignores.rb
new file mode 100644
index 00000000000..270c9501dd2
--- /dev/null
+++ b/lib/api/gitignores.rb
@@ -0,0 +1,29 @@
+module API
+ class Gitignores < Grape::API
+
+ # Get the list of the available gitignore templates
+ #
+ # Example Request:
+ # GET /gitignores
+ get 'gitignores' do
+ present Gitlab::Gitignore.all, with: Entities::GitignoresList
+ end
+
+ # Get the text for a specific gitignore
+ #
+ # Parameters:
+ # name (required) - The name of a license
+ #
+ # Example Request:
+ # GET /gitignores/Elixir
+ #
+ get 'gitignores/:name' do
+ required_attributes! [:name]
+
+ gitignore = Gitlab::Gitignore.find(params[:name])
+ not_found!('.gitignore') unless gitignore
+
+ present gitignore, with: Entities::Gitignore
+ end
+ end
+end
diff --git a/lib/gitlab/gitignore.rb b/lib/gitlab/gitignore.rb
new file mode 100644
index 00000000000..f46b43b61a4
--- /dev/null
+++ b/lib/gitlab/gitignore.rb
@@ -0,0 +1,56 @@
+module Gitlab
+ class Gitignore
+ FILTER_REGEX = /\.gitignore\z/.freeze
+
+ def initialize(path)
+ @path = path
+ end
+
+ def name
+ File.basename(@path, '.gitignore')
+ end
+
+ def content
+ File.read(@path)
+ end
+
+ class << self
+ def all
+ languages_frameworks + global
+ end
+
+ def find(key)
+ file_name = "#{key}.gitignore"
+
+ directory = select_directory(file_name)
+ directory ? new(File.join(directory, file_name)) : nil
+ end
+
+ def global
+ files_for_folder(global_dir).map { |file| new(File.join(global_dir, file)) }
+ end
+
+ def languages_frameworks
+ files_for_folder(gitignore_dir).map { |file| new(File.join(gitignore_dir, file)) }
+ end
+
+ private
+
+ def select_directory(file_name)
+ [gitignore_dir, global_dir].find { |dir| File.exist?(File.join(dir, file_name)) }
+ end
+
+ def global_dir
+ File.join(gitignore_dir, 'Global')
+ end
+
+ def gitignore_dir
+ Rails.root.join('vendor/gitignore')
+ end
+
+ def files_for_folder(dir)
+ Dir.glob("#{dir.to_s}/*.gitignore").map { |file| file.gsub(FILTER_REGEX, '') }
+ end
+ end
+ end
+end
diff --git a/lib/tasks/gitlab/update_gitignore.rake b/lib/tasks/gitlab/update_gitignore.rake
new file mode 100644
index 00000000000..84aa312002b
--- /dev/null
+++ b/lib/tasks/gitlab/update_gitignore.rake
@@ -0,0 +1,46 @@
+namespace :gitlab do
+ desc "GitLab | Update gitignore"
+ task :update_gitignore do
+ unless clone_gitignores
+ puts "Cloning the gitignores failed".red
+ return
+ end
+
+ remove_unneeded_files(gitignore_directory)
+ remove_unneeded_files(global_directory)
+
+ puts "Done".green
+ end
+
+ def clone_gitignores
+ FileUtils.rm_rf(gitignore_directory) if Dir.exist?(gitignore_directory)
+ FileUtils.cd vendor_directory
+
+ system('git clone --depth=1 --branch=master https://github.com/github/gitignore.git')
+ end
+
+ # Retain only certain files:
+ # - The LICENSE, because we have to
+ # - The sub dir global
+ # - The gitignores themself
+ # - Dir.entires returns also the entries '.' and '..'
+ def remove_unneeded_files(path)
+ Dir.foreach(path) do |file|
+ FileUtils.rm_rf(File.join(path, file)) unless file =~ /(\.{1,2}|LICENSE|Global|\.gitignore)\z/
+ end
+ end
+
+ private
+
+ def vendor_directory
+ Rails.root.join('vendor')
+ end
+
+ def gitignore_directory
+ File.join(vendor_directory, 'gitignore')
+ end
+
+ def global_directory
+ File.join(gitignore_directory, 'Global')
+ end
+end