Created
March 3, 2016 22:22
-
-
Save iansltx/4e6ae73f34df21ea8108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($argc < 3) { | |
error_log("Usage: php gitlab_archive.php <private_token> <namespaced path> <optional-branch-or-commit-sha> > archive.tar.gz"); die(); | |
} | |
$token = $argv[1]; | |
$path = $argv[2]; | |
$ref = isset($argv[3]) ? $argv[3] : null; | |
$get = function($path, $decode = true) use ($token) { | |
$ch = curl_init("https://gitlab.com/api/v3/" . $path); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ["PRIVATE-TOKEN: " . $token]); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
return $decode ? json_decode(curl_exec($ch)) : curl_exec($ch); | |
}; | |
if (is_numeric($path)) { | |
$projectId = $path; | |
} else { | |
error_log("Finding project ID..."); | |
$project = $get("projects/" . urlencode($path)); | |
if (!isset($project->id)) { | |
error_log("Could not find project"); die(); | |
} | |
$projectId = $project->id; | |
} | |
if (!$ref) { | |
error_log("Getting archive of tip of default branch..."); | |
echo $get('projects/' . $projectId . '/repository/archive', false); | |
return; | |
} | |
if (ctype_xdigit($ref) && strlen($ref) >= 8) { | |
error_log("Getting archive of commit " . $ref); | |
echo $get('projects/' . $projectId . '/repository/archive?sha=' . $ref, false); | |
return; | |
} | |
error_log("Getting commit ID of tip of branch " . $ref); | |
$branch = $get('projects/' . $projectId . '/repository/branches/' . $ref); | |
if (!isset($branch->name)) { | |
error_log("Could not find branch"); die(); | |
} | |
$ref = $branch->commit->id; | |
error_log("Getting archive of commit " . $ref); | |
echo $get('projects/' . $projectId . '/repository/archive?sha=' . $ref, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment