2009-11-28 19:39:06 +08:00
|
|
|
/*
|
2013-07-23 18:20:06 +08:00
|
|
|
* Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
|
2009-11-28 19:39:06 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
2009-10-17 19:18:40 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE (256 * 1024) /* 256 KB */
|
|
|
|
|
2012-04-19 02:55:24 +08:00
|
|
|
#define URL_FORMAT "https://api.github.com/repos/%s/%s/commits"
|
2009-10-17 19:18:40 +08:00
|
|
|
#define URL_SIZE 256
|
|
|
|
|
|
|
|
/* Return the offset of the first newline in text or the length of
|
|
|
|
text if there's no newline */
|
|
|
|
static int newline_offset(const char *text)
|
|
|
|
{
|
|
|
|
const char *newline = strchr(text, '\n');
|
|
|
|
if(!newline)
|
|
|
|
return strlen(text);
|
|
|
|
else
|
|
|
|
return (int)(newline - text);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct write_result
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
int pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
|
|
|
struct write_result *result = (struct write_result *)stream;
|
|
|
|
|
|
|
|
if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: too small buffer\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(result->data + result->pos, ptr, size * nmemb);
|
|
|
|
result->pos += size * nmemb;
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *request(const char *url)
|
|
|
|
{
|
2013-07-28 02:23:34 +08:00
|
|
|
CURL *curl = NULL;
|
2009-10-17 19:18:40 +08:00
|
|
|
CURLcode status;
|
2013-12-27 14:55:05 +08:00
|
|
|
struct curl_slist *headers = NULL;
|
2013-07-28 02:23:34 +08:00
|
|
|
char *data = NULL;
|
2009-10-17 19:18:40 +08:00
|
|
|
long code;
|
|
|
|
|
2013-07-28 02:23:34 +08:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
2009-10-17 19:18:40 +08:00
|
|
|
curl = curl_easy_init();
|
2013-07-28 02:23:34 +08:00
|
|
|
if(!curl)
|
|
|
|
goto error;
|
|
|
|
|
2009-10-17 19:18:40 +08:00
|
|
|
data = malloc(BUFFER_SIZE);
|
2013-07-28 02:23:34 +08:00
|
|
|
if(!data)
|
|
|
|
goto error;
|
2009-10-17 19:18:40 +08:00
|
|
|
|
|
|
|
struct write_result write_result = {
|
|
|
|
.data = data,
|
|
|
|
.pos = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2013-12-27 14:55:05 +08:00
|
|
|
|
|
|
|
/* GitHub commits API v3 requires a User-Agent header */
|
|
|
|
headers = curl_slist_append(headers, "User-Agent: Jansson-Tutorial");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
2009-10-17 19:18:40 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
|
|
|
|
|
|
|
|
status = curl_easy_perform(curl);
|
|
|
|
if(status != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: unable to request data from %s:\n", url);
|
|
|
|
fprintf(stderr, "%s\n", curl_easy_strerror(status));
|
2013-07-28 02:23:34 +08:00
|
|
|
goto error;
|
2009-10-17 19:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
if(code != 200)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: server responded with code %ld\n", code);
|
2013-07-28 02:23:34 +08:00
|
|
|
goto error;
|
2009-10-17 19:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2013-12-27 14:55:05 +08:00
|
|
|
curl_slist_free_all(headers);
|
2009-10-17 19:18:40 +08:00
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
|
|
/* zero-terminate the result */
|
|
|
|
data[write_result.pos] = '\0';
|
|
|
|
|
|
|
|
return data;
|
2013-07-28 02:23:34 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
if(data)
|
|
|
|
free(data);
|
|
|
|
if(curl)
|
|
|
|
curl_easy_cleanup(curl);
|
2013-12-27 14:55:05 +08:00
|
|
|
if(headers)
|
|
|
|
curl_slist_free_all(headers);
|
2013-07-28 02:23:34 +08:00
|
|
|
curl_global_cleanup();
|
|
|
|
return NULL;
|
2009-10-17 19:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i;
|
2009-10-17 19:18:40 +08:00
|
|
|
char *text;
|
|
|
|
char url[URL_SIZE];
|
|
|
|
|
|
|
|
json_t *root;
|
|
|
|
json_error_t error;
|
|
|
|
|
|
|
|
if(argc != 3)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
|
|
|
|
fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
|
|
|
|
|
|
|
|
text = request(url);
|
|
|
|
if(!text)
|
|
|
|
return 1;
|
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
root = json_loads(text, 0, &error);
|
2009-10-17 19:18:40 +08:00
|
|
|
free(text);
|
|
|
|
|
|
|
|
if(!root)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:55:24 +08:00
|
|
|
if(!json_is_array(root))
|
2009-10-17 19:18:40 +08:00
|
|
|
{
|
2012-04-19 02:55:24 +08:00
|
|
|
fprintf(stderr, "error: root is not an array\n");
|
2013-03-10 03:15:09 +08:00
|
|
|
json_decref(root);
|
2009-10-17 19:18:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:55:24 +08:00
|
|
|
for(i = 0; i < json_array_size(root); i++)
|
2009-10-17 19:18:40 +08:00
|
|
|
{
|
2012-04-19 02:55:24 +08:00
|
|
|
json_t *data, *sha, *commit, *message;
|
2009-10-17 19:18:40 +08:00
|
|
|
const char *message_text;
|
|
|
|
|
2012-04-19 02:55:24 +08:00
|
|
|
data = json_array_get(root, i);
|
|
|
|
if(!json_is_object(data))
|
|
|
|
{
|
2013-07-28 02:23:34 +08:00
|
|
|
fprintf(stderr, "error: commit data %d is not an object\n", (int)(i + 1));
|
2013-03-10 03:15:09 +08:00
|
|
|
json_decref(root);
|
2012-04-19 02:55:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sha = json_object_get(data, "sha");
|
|
|
|
if(!json_is_string(sha))
|
2009-10-17 19:18:40 +08:00
|
|
|
{
|
2013-07-28 02:23:34 +08:00
|
|
|
fprintf(stderr, "error: commit %d: sha is not a string\n", (int)(i + 1));
|
2009-10-17 19:18:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:55:24 +08:00
|
|
|
commit = json_object_get(data, "commit");
|
|
|
|
if(!json_is_object(commit))
|
2009-10-17 19:18:40 +08:00
|
|
|
{
|
2013-07-28 02:23:34 +08:00
|
|
|
fprintf(stderr, "error: commit %d: commit is not an object\n", (int)(i + 1));
|
2013-03-10 03:15:09 +08:00
|
|
|
json_decref(root);
|
2009-10-17 19:18:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = json_object_get(commit, "message");
|
2009-10-20 02:55:21 +08:00
|
|
|
if(!json_is_string(message))
|
2009-10-17 19:18:40 +08:00
|
|
|
{
|
2013-07-28 02:23:34 +08:00
|
|
|
fprintf(stderr, "error: commit %d: message is not a string\n", (int)(i + 1));
|
2013-03-10 03:15:09 +08:00
|
|
|
json_decref(root);
|
2009-10-17 19:18:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
message_text = json_string_value(message);
|
|
|
|
printf("%.8s %.*s\n",
|
2012-04-19 02:55:24 +08:00
|
|
|
json_string_value(sha),
|
2009-10-17 19:18:40 +08:00
|
|
|
newline_offset(message_text),
|
|
|
|
message_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(root);
|
|
|
|
return 0;
|
|
|
|
}
|