From 7c19bc3b02efc051a21fbdb447cd7f55bd8272f9 Mon Sep 17 00:00:00 2001 From: jaisejose1123 Date: Wed, 31 Jul 2024 15:25:14 +0530 Subject: [PATCH] Added option to prune the brance from client --- lib/git_worktree.rb | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/git_worktree.rb b/lib/git_worktree.rb index b9dbe3cf130..2128bbb119e 100644 --- a/lib/git_worktree.rb +++ b/lib/git_worktree.rb @@ -3,9 +3,9 @@ class GitWorktree attr_accessor :name, :email, :base_name - ENTRY_KEYS = [:path, :dev, :ino, :mode, :gid, :uid, :ctime, :mtime].freeze + ENTRY_KEYS = [:path, :dev, :ino, :mode, :gid, :uid, :ctime, :mtime] DEFAULT_FILE_MODE = 0o100644 - LOCK_REFERENCE = 'refs/locks'.freeze + LOCK_REFERENCE = 'refs/locks' def self.checkout_at(url, directory, options = {}) worktree_opts = options.merge( @@ -45,7 +45,7 @@ def initialize(options = {}) # $ bundle config set --local build.rugged --with-ssh # $ bin/bundle # - if @ssh_private_key && Rugged.features.exclude?(:ssh) + if @ssh_private_key && !Rugged.features.include?(:ssh) raise GitWorktreeException::InvalidCredentialType, "ssh credentials are not enabled for use. Recompile the rugged/libgit2 gem with ssh support to enable it." end @@ -139,13 +139,17 @@ def add(path, data, default_entry_keys = {}) ENTRY_KEYS.each { |key| entry[key] = default_entry_keys[key] if default_entry_keys.key?(key) } entry[:oid] = @repo.write(data, :blob) entry[:mode] ||= DEFAULT_FILE_MODE - entry[:mtime] ||= Time.zone.now + entry[:mtime] ||= Time.now current_index.add(entry) end - delegate :remove, :to => :current_index + def remove(path) + current_index.remove(path) + end - delegate :remove_dir, :to => :current_index + def remove_dir(path) + current_index.remove_dir(path) + end def file_exists?(path) !!find_entry(path) @@ -166,7 +170,7 @@ def read_entry(entry) def entries(path) tree = get_tree(path) - tree.find_all.pluck(:name) + tree.find_all.collect { |e| e[:name] } end def nodes(path) @@ -192,7 +196,7 @@ def file_attributes(fname) walker = Rugged::Walker.new(@repo) walker.sorting(Rugged::SORT_DATE) walker.push(@repo.ref(local_ref).target.oid) - commit = walker.find { |c| !c.diff(:paths => [fname]).empty? } + commit = walker.find { |c| c.diff(:paths => [fname]).size > 0 } return {} unless commit {:updated_on => commit.time.gmtime, :updated_by => commit.author[:name]} @@ -356,7 +360,7 @@ def merge_and_push(commit) def merge(commit, rebase = false) current_branch = @repo.ref(local_ref) merge_index = @repo.merge_commits(current_branch.target, commit) if current_branch - if merge_index&.conflicts? + if merge_index && merge_index.conflicts? result = differences_with_current(commit) raise GitWorktreeException::GitConflicts, result end @@ -392,7 +396,7 @@ def process_repo(options) def create_repo @repo = @bare ? Rugged::Repository.init_at(@path, :bare) : Rugged::Repository.init_at(@path) - @repo.config['user.name'] = @username if @username + @repo.config['user.name'] = @username if @username @repo.config['user.email'] = @email if @email @repo.config['merge.ff'] = 'only' if @fast_forward_merge end @@ -415,8 +419,8 @@ def fetch_entry(path) end def fix_path_mv(dir_name) - dir_name = dir_name[1..] if dir_name[0] == '/' - dir_name += '/' if dir_name[-1] != '/' + dir_name = dir_name[1..-1] if dir_name[0] == '/' + dir_name += '/' if dir_name[-1] != '/' dir_name end @@ -434,11 +438,11 @@ def lookup_commit_tree return nil if !@commit_sha && !@repo.branches['master'] ct = @commit_sha ? @repo.lookup(@commit_sha) : @repo.branches['master'].target - ct&.tree + ct.tree if ct end def get_tree_entry(path) - path = path[1..] if path[0] == '/' + path = path[1..-1] if path[0] == '/' tree = lookup_commit_tree begin entry = tree.path(path) @@ -463,7 +467,7 @@ def current_index end def create_commit(message, tree, parents) - author = {:email => @email, :name => @username || @email, :time => Time.zone.now} + author = {:email => @email, :name => @username || @email, :time => Time.now} # Create the actual commit but do not update the reference Rugged::Commit.create(@repo, :author => author, :committer => author, :message => message, :parents => parents, @@ -512,4 +516,4 @@ def differences_with_current(commit) end differences end -end +end \ No newline at end of file