Skip to content

Instantly share code, notes, and snippets.

@iansltx
Created March 3, 2016 22:22
Show Gist options
  • Save iansltx/4e6ae73f34df21ea8108 to your computer and use it in GitHub Desktop.
Save iansltx/4e6ae73f34df21ea8108 to your computer and use it in GitHub Desktop.
<?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