Coder Social home page Coder Social logo

gitlab_git's Introduction

GitLab Git

GitLab wrapper around git objects.


build status Build Status Gem Version Code Climate Coverage Status

Move from Grit to Rugged

GitLab Git used grit as main library in past. Right now GitLab Git uses 2 libraries: rugged and grit. We want to increase usage of rugged and reduce usage of grit in this library. If you can help us with that - please send Merge Request.

How to use:

Repository

# Init repo with full 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', '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

# Blob object with sha 8a3f8ddcf3536628c9670d41e67a785383eded1d
raw_blob = Gitlab::Git::Blob.raw(repo, '8a3f8ddcf3536628c9670d41e67a785383eded1d')

# Attributes for raw blobs are more limited
raw_blob.id
raw_blob.size
raw_blob.data

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
 # [email protected]

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

Compare

Allows you to get difference(commits, diffs) between two sha/branch/tag

 compare = Gitlab::Git::Compare.new(repo, 'v4.3.2', 'master')
 
 compare.commits 
 # [ <Gitlab::Git::Commit:0x000..>, <Gitlab::Git::Commit:0x000..>]

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

gitlab_git's People

Contributors

dzaporozhets avatar jacobvosmaer avatar layzerar avatar sugarmo avatar edvakf avatar hiroponz avatar dosire avatar axilleas avatar zzet avatar 1ed avatar jacargentina avatar jvanbaarsen avatar terran42 avatar catatsuy avatar skv-headless avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.