Home Account

git auto commit script

2013-03-25 12:53 dennis iversen

Tags: php scripts-and-functions git

Problem: I almost always use public repos (which are cloned from HTTPS). I even do this if it is my own repos. Sometimes I would like to commit to the repo, but when you have cloned a HTTPS url, you will have to enter your credentials.

This small script does a git add . && git commit -m "my message" && git push

And in the process it rewrites the push URL from https:// to the git@ (ssh) format. Therefor you don't need to think about if you have checked out a HTTPS or a SSH repo version.

#!/usr/bin/php
<?php

/**
 * About: Small script to use if you don't want to do: 
 * git add . && git commit && git push
 * If you use a https URL, then the https URL will 
 * be rewritten to a SSH private URL
 *  
 * install: save script as e.g. git_all.sh in a bin/ folder
 * usage: git_all.sh "my commit message"
 */

$git_add = "git add . ";
system($git_add);

if (isset($argv[1])) {
    $message = $argv[1];
} else {
    $message = "auto commit";
}

$git_commit = "git commit -m \"$message\" ";
system($git_commit);

$origin = git_get_remote_origin ();
$push_url = getSshFromHttps ($origin);

$git_push = "git push $push_url";
system($git_push);

function getSshFromHttps($url) {

    $ary = parse_url(trim($url));
    $num = count($ary);

    // Is in the format git@ ...
    if ($num == 1) {
        return $ary['path'];
    }

    // E.g. https://github.com/diversen/vote.git
    if (isset($ary['scheme']) && $ary['scheme'] == 'git') {
        return "git@$ary[host]:" . ltrim($ary['path'], '/');
    }

    return "git@$ary[host]:" . ltrim($ary['path'], '/');
}

function git_get_remote_origin () {
    return shell_exec("git config --get remote.origin.url");
}

This page has been requested 5871 times