Giter VIP home page Giter VIP logo

gitlab_git's Introduction

GitLab Git

GitLab wrapper around git objects.

build status Build Status Gem Version Code Climate Coverage Status

Moved from Grit to Rugged

GitLab Git used grit as main library in past. Now it uses rugged

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 rugged repo object
repo.rugged

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..>]

# Diff object
diff = commit.diffs.first

diff.diff #  "--- a/Gemfile.lock\....."
diff.new_path # => "Gemfile.lock",
diff.old_path # => "Gemfile.lock",
diff.a_mode # => nil,
diff.b_mode # => "100644",
diff.new_file # => false,
diff.renamed_file # => false,
diff.deleted_file # => false

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

1ed avatar asedge avatar axilleas avatar catatsuy avatar cirosantilli avatar dosire avatar dzaporozhets avatar edvakf avatar hiroponz avatar jacargentina avatar jacobvosmaer avatar jvanbaarsen avatar layzerar avatar molizz avatar mr-vinn avatar skv-headless avatar sugarmo avatar terran42 avatar zzet avatar

Watchers

 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.