Skip to content

xdite/gitlab_git

 
 

Repository files navigation

GitLab Git

GitLab wrapper around git objects. Use patched Grit as main library for parsing git objects

Code status

  • Build Status
  • Code Climate
  • Coverage Status

How to use:

Repository

# Init repo with relative path according to repos_path. 
# 
repo = Gitlab::Git::Repository.new('/home/git/repositories/gitlab/gitlab-ci.git')

repo.path
# "/home/git/repositories/gitlab/gitlab-ci.git"

repo.name
# "gitlab-ci.git"

# Get branches and tags
repo.branches
repo.tags

# Get branch or tag names
repo.branch_names
repo.tag_names

# Archive repo to `/tmp` dir
repo.archive_repo('master', '/tmp')

# Bare repo size in MB.
repo.size
# 10.43

# Search for code
repo.search_files('rspec', 'master')
# [ <Gitlab::Git::BlobSnippet:0x000..>, <Gitlab::Git::BlobSnippet:0x000..>]

# Access to grit repo object 
repo.grit

Tree

# Tree objects for root dir
tree = Gitlab::Git::Tree.where(repo, '893ade32')

# Tree objects for sub dir
tree = Gitlab::Git::Tree.where(repo, '893ade32', 'master', 'app/models/')

# [
#   #<Gitlab::Git::Tree:0x00000002b2ed80 @id="38f45392ae61f0effa84048f208a81019cc306bb", @name="lib", @path="projects/lib", @type=:tree, @mode="040000", @commit_id="8470d70da67355c9c009e4401746b1d5410af2e3">
#   #<Gitlab::Git::Tree:0x00000002b2ed80 @id="32a45392ae61f0effa84048f208a81019cc306bb", @name="sample.rb", @path="projects/sample.rb", @type=:blob, @mode="040000", @commit_id="8470d70da67355c9c009e4401746b1d5410af2e3">
# ]

dir = tree.first
dir.name # lib
dir.type # :tree
dir.dir? # true
dir.file? # false

file = tree.last
file.name # sample.rb
file.type # :blob
file.dir? # false
file.file? # true

# Select only files for tree
tree.select(&:file?)

# Find readme
tree.find(&:readme?)

Blob

# Blob object for Commit sha 893ade32
blob = Gitlab::Git::Blob.find(repo, '893ade32', 'Gemfile')

# Attributes 
blob.id
blob.name
blob.size
blob.data
blob.mode
blob.path
blob.commit_id

Commit

Picking
 # Get commits collection with pagination
 Gitlab::Git::Commit.where(
   repo: repo,
   ref: 'master',
   path: 'app/models',
   limit: 10,
   offset: 5,
 )

 # Find single commit
 Gitlab::Git::Commit.find(repo, '29eda46b')
 Gitlab::Git::Commit.find(repo, 'v2.4.6')

 # Get last commit for HEAD
 commit = Gitlab::Git::Commit.last(repo)
 
 # Get last commit for specified file/directory
 Gitlab::Git::Commit.find_for_path(repo, '29eda46b', 'app/models')

 # Commits between branches
 Gitlab::Git::Commit.between(repo, 'dev', 'master')
 # [ <Gitlab::Git::Commit:0x000..>, <Gitlab::Git::Commit:0x000..>]
Commit object
 # Commit id
 commit.id
 commit.sha
 # ba8812a2de5e5ea191da6930a8ee1965873286e3

 commit.short_id
 # ba8812a2de

 commit.message
 commit.safe_message
 # Fix bug 891

 commit.parent_id
 # ba8812a2de5e5ea191da6930a8ee1965873286e3

 commit.diffs
 # [ <Gitlab::Git::Diff:0x000..>, <Gitlab::Git::Diff:0x000..>]
 
 commit.created_at 
 commit.authored_date
 commit.committed_date
 # 2013-07-03 22:11:26 +0300

 commit.committer_name
 commit.author_name
 # John Smith
 
 commit.committer_email
 commit.author_email
 # jsmith@sample.com

Diff object

 # From commit
 commit.diffs
 # [ <Gitlab::Git::Diff:0x000..>, <Gitlab::Git::Diff:0x000..>]

 # Diff between several commits
 Gitlab::Git::Diff.between(repo, 'dev', 'master')
 # [ <Gitlab::Git::Diff:0x000..>, <Gitlab::Git::Diff:0x000..>]

Git blame

 # Git blame for file
 blame = Gitlab::Git::Blame.new(repo, 'master, 'app/models/project.rb')
 blame.each do |commit, lines|
   commit # <Gitlab::Git::Commit:0x000..>
   lines # ['class Project', 'def initialize']
 end

About

Access git objects via GitLab Git. Basically its wrapper around grit

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%