Skip to content

Instantly share code, notes, and snippets.

@nmarley
Created April 25, 2013 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmarley/5461529 to your computer and use it in GitHub Desktop.
Save nmarley/5461529 to your computer and use it in GitHub Desktop.
Quick-and-dirty utility to: 1) initialize a new git repo in the current directory (git init) 2) add all existing files, and: (git add .) 3) make an initial commit with message 'Initial commit' (git commit -m 'Initial commit') It won't run the commands if a git repo already exists. It checks the current directory only (does not accept a dir as an…
#! /usr/bin/env ruby
#
# git-start.rb
#
# Quick-and-dirty utility to initialize a new git repo in the current
# directory, add all existing files and make an initial commit with message
# 'Initial commit'.
#
# Saves a few seconds here and there.
#
# Does not check for nested git repos/submodules.
require 'open3'
def git_init
cmd = "git init"
stdin, stdout, stderr = Open3.popen3(cmd)
this_dir = Dir.pwd
git_dir = this_dir + '/.git/'
git_init_msg = 'Initialized empty Git repository in ' + git_dir +
"\n"
out = stdout.readlines
err = stderr.readlines
unless out == [git_init_msg] &&
err.size == 0
raise "Error initializing git dir."
end
end
def git_check_clean
cmd = "git status"
stdin, stdout, stderr = Open3.popen3(cmd)
no_git_repo_msg =
%{fatal: Not a git repository (or any of the parent directories): .git\n}
unless stdout.readlines.size == 0 &&
stderr.readlines == [no_git_repo_msg]
raise "A git repo already exists."
end
end
def git_add_all
cmd = "git add ."
stdin, stdout, stderr = Open3.popen3(cmd)
out = stdout.readlines
err = stderr.readlines
unless out.size == 0 && err.size == 0
raise "Error adding files to git index."
end
end
def git_initial_commit
cmd = "git commit -m 'Initial commit'"
stdin, stdout, stderr = Open3.popen3(cmd)
out = stdout.readlines
err = stderr.readlines
unless out.size != 0 && err.size == 0
raise "Error with git commit."
end
end
begin
git_check_clean()
git_init()
git_add_all()
git_initial_commit()
rescue Exception => ex
$stderr.puts ex.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment