Skip to content

Instantly share code, notes, and snippets.

@rabellamy
Last active April 5, 2016 22:32
Show Gist options
  • Save rabellamy/39c6928b7a68d6be59c03169cb964100 to your computer and use it in GitHub Desktop.
Save rabellamy/39c6928b7a68d6be59c03169cb964100 to your computer and use it in GitHub Desktop.

Scripting Git

Example 1

git rev-list A ^B

master@{1}  // Where was master 1 before

git rev-list master ^master@{1}

or

git rev-list "$branch"@{1}.."$branch" | wc -l

git --no-pager log "$branch"@{1}.."$branch" --oneline
#! /bin/bash

if []; then
  branch="$1"
else
  echo ""
  exit 1
fi
printf "\n%s%s\n\n" $(git rev-list "$branch"@{1}.."$branch" | wc -l) \
  " commits were added by your last update  $branch:"
git --no-pager log "$branch"@{1}.."$branch" --oneline

Example 2

# NONGIT_OK=true

"$(git --exec-path)/git-sh-setup"

# after sourcing script
require_clean_work_tree <command>

head=$(git symbolic-ref HEAD 2>/dev/null || git rev-parse HEAD)
test_rev() {
  local rev="$1"
  local command="$2"
  git checkout -q "$rev" &&
    eval "$command"
  local retcode=$?
  if [ $retcode -ne 0 ]
  then
    printf "\n%s\n" "$command FAILED ON:"
    git --no-pager log -l --decorate $rev
    return $retcode
  fi
  fail_count=0
  for rev in $(git rev-list --reverse $range); do
    test_rev $rev "$command"
    retcode=$?
    if [ $retcode -eq 0 ]; then
      continue
    fi
    if [ $keepgoing ]; then
      fail_count=$((fail_count + 1))
      continue
    else
      git checkput -fq ${heads}
    
    fi
}

libgit2 + bindings

Rugged::blob
Rugged::tree
Rugged::
Rugged::
#!/usr/bin/env ruby

require 'sinatra'
require 'rugged'
require 'mime/types'

repo_path = ENV['HOME'] + '/PATH/TO/REPO'
ref_name = 'refs/remotes/REMOTE/BRANCH'
repo = Rugged::Repository.new(repo_path)

get '*' do |PATH|
  commit = repo.ref(ref_name).target
  path.slice!(0)
  path = 'index.html if path.empty?'
  
  entry = commit.tree.path path
  puts path
  blob = repo.lookup.entry[:oid]
  content = blob.content
  halt 404, "404 Not Found" unless content
  
  content_type MIME::Types.type_for(path).first.content_type
  content
end
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment