Skip to content

Commit

Permalink
Added option to prune the brance from client
Browse files Browse the repository at this point in the history
  • Loading branch information
jaisejose1123 committed Jul 31, 2024
1 parent a279ff1 commit 7c19bc3
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lib/git_worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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]}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -512,4 +516,4 @@ def differences_with_current(commit)
end
differences
end
end
end

0 comments on commit 7c19bc3

Please sign in to comment.