contrib/git-credential-gnome-keyring.c: small stylistic cleanups
Signed-off-by: John Szakmeister <john@szakmeister.net> Signed-off-by: Junio C Hamano <gitster@pobox.com> Reviewed-by: Felipe Contreras <felipe.contreras@gmail.com>
This commit is contained in:
parent
a155a5f075
commit
0162b3c430
@ -60,7 +60,7 @@
|
|||||||
#define gnome_keyring_memory_free gnome_keyring_free_password
|
#define gnome_keyring_memory_free gnome_keyring_free_password
|
||||||
#define gnome_keyring_memory_strdup g_strdup
|
#define gnome_keyring_memory_strdup g_strdup
|
||||||
|
|
||||||
static const char* gnome_keyring_result_to_message(GnomeKeyringResult result)
|
static const char *gnome_keyring_result_to_message(GnomeKeyringResult result)
|
||||||
{
|
{
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case GNOME_KEYRING_RESULT_OK:
|
case GNOME_KEYRING_RESULT_OK:
|
||||||
@ -95,9 +95,9 @@ static const char* gnome_keyring_result_to_message(GnomeKeyringResult result)
|
|||||||
|
|
||||||
static void gnome_keyring_done_cb(GnomeKeyringResult result, gpointer user_data)
|
static void gnome_keyring_done_cb(GnomeKeyringResult result, gpointer user_data)
|
||||||
{
|
{
|
||||||
gpointer *data = (gpointer*) user_data;
|
gpointer *data = (gpointer *)user_data;
|
||||||
int *done = (int*) data[0];
|
int *done = (int *)data[0];
|
||||||
GnomeKeyringResult *r = (GnomeKeyringResult*) data[1];
|
GnomeKeyringResult *r = (GnomeKeyringResult *)data[1];
|
||||||
|
|
||||||
*r = result;
|
*r = result;
|
||||||
*done = 1;
|
*done = 1;
|
||||||
@ -130,34 +130,30 @@ static GnomeKeyringResult gnome_keyring_item_delete_sync(const char *keyring, gu
|
|||||||
/*
|
/*
|
||||||
* This credential struct and API is simplified from git's credential.{h,c}
|
* This credential struct and API is simplified from git's credential.{h,c}
|
||||||
*/
|
*/
|
||||||
struct credential
|
struct credential {
|
||||||
{
|
char *protocol;
|
||||||
char *protocol;
|
char *host;
|
||||||
char *host;
|
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
char *path;
|
char *path;
|
||||||
char *username;
|
char *username;
|
||||||
char *password;
|
char *password;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CREDENTIAL_INIT \
|
#define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
|
||||||
{ NULL,NULL,0,NULL,NULL,NULL }
|
|
||||||
|
|
||||||
typedef int (*credential_op_cb)(struct credential*);
|
typedef int (*credential_op_cb)(struct credential *);
|
||||||
|
|
||||||
struct credential_operation
|
struct credential_operation {
|
||||||
{
|
char *name;
|
||||||
char *name;
|
|
||||||
credential_op_cb op;
|
credential_op_cb op;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CREDENTIAL_OP_END \
|
#define CREDENTIAL_OP_END { NULL, NULL }
|
||||||
{ NULL,NULL }
|
|
||||||
|
|
||||||
/* ----------------- GNOME Keyring functions ----------------- */
|
/* ----------------- GNOME Keyring functions ----------------- */
|
||||||
|
|
||||||
/* create a special keyring option string, if path is given */
|
/* create a special keyring option string, if path is given */
|
||||||
static char* keyring_object(struct credential *c)
|
static char *keyring_object(struct credential *c)
|
||||||
{
|
{
|
||||||
if (!c->path)
|
if (!c->path)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -170,7 +166,7 @@ static char* keyring_object(struct credential *c)
|
|||||||
|
|
||||||
static int keyring_get(struct credential *c)
|
static int keyring_get(struct credential *c)
|
||||||
{
|
{
|
||||||
char* object = NULL;
|
char *object = NULL;
|
||||||
GList *entries;
|
GList *entries;
|
||||||
GnomeKeyringNetworkPasswordData *password_data;
|
GnomeKeyringNetworkPasswordData *password_data;
|
||||||
GnomeKeyringResult result;
|
GnomeKeyringResult result;
|
||||||
@ -204,7 +200,7 @@ static int keyring_get(struct credential *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* pick the first one from the list */
|
/* pick the first one from the list */
|
||||||
password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
|
password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
|
||||||
|
|
||||||
gnome_keyring_memory_free(c->password);
|
gnome_keyring_memory_free(c->password);
|
||||||
c->password = gnome_keyring_memory_strdup(password_data->password);
|
c->password = gnome_keyring_memory_strdup(password_data->password);
|
||||||
@ -221,7 +217,7 @@ static int keyring_get(struct credential *c)
|
|||||||
static int keyring_store(struct credential *c)
|
static int keyring_store(struct credential *c)
|
||||||
{
|
{
|
||||||
guint32 item_id;
|
guint32 item_id;
|
||||||
char *object = NULL;
|
char *object = NULL;
|
||||||
GnomeKeyringResult result;
|
GnomeKeyringResult result;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -262,7 +258,7 @@ static int keyring_store(struct credential *c)
|
|||||||
|
|
||||||
static int keyring_erase(struct credential *c)
|
static int keyring_erase(struct credential *c)
|
||||||
{
|
{
|
||||||
char *object = NULL;
|
char *object = NULL;
|
||||||
GList *entries;
|
GList *entries;
|
||||||
GnomeKeyringNetworkPasswordData *password_data;
|
GnomeKeyringNetworkPasswordData *password_data;
|
||||||
GnomeKeyringResult result;
|
GnomeKeyringResult result;
|
||||||
@ -298,22 +294,20 @@ static int keyring_erase(struct credential *c)
|
|||||||
if (result == GNOME_KEYRING_RESULT_CANCELLED)
|
if (result == GNOME_KEYRING_RESULT_CANCELLED)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
if (result != GNOME_KEYRING_RESULT_OK)
|
if (result != GNOME_KEYRING_RESULT_OK) {
|
||||||
{
|
|
||||||
g_critical("%s", gnome_keyring_result_to_message(result));
|
g_critical("%s", gnome_keyring_result_to_message(result));
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pick the first one from the list (delete all matches?) */
|
/* pick the first one from the list (delete all matches?) */
|
||||||
password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
|
password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
|
||||||
|
|
||||||
result = gnome_keyring_item_delete_sync(
|
result = gnome_keyring_item_delete_sync(
|
||||||
password_data->keyring, password_data->item_id);
|
password_data->keyring, password_data->item_id);
|
||||||
|
|
||||||
gnome_keyring_network_password_list_free(entries);
|
gnome_keyring_network_password_list_free(entries);
|
||||||
|
|
||||||
if (result != GNOME_KEYRING_RESULT_OK)
|
if (result != GNOME_KEYRING_RESULT_OK) {
|
||||||
{
|
|
||||||
g_critical("%s", gnome_keyring_result_to_message(result));
|
g_critical("%s", gnome_keyring_result_to_message(result));
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -325,9 +319,8 @@ static int keyring_erase(struct credential *c)
|
|||||||
* Table with helper operation callbacks, used by generic
|
* Table with helper operation callbacks, used by generic
|
||||||
* credential helper main function.
|
* credential helper main function.
|
||||||
*/
|
*/
|
||||||
static struct credential_operation const credential_helper_ops[] =
|
static struct credential_operation const credential_helper_ops[] = {
|
||||||
{
|
{ "get", keyring_get },
|
||||||
{ "get", keyring_get },
|
|
||||||
{ "store", keyring_store },
|
{ "store", keyring_store },
|
||||||
{ "erase", keyring_erase },
|
{ "erase", keyring_erase },
|
||||||
CREDENTIAL_OP_END
|
CREDENTIAL_OP_END
|
||||||
@ -353,24 +346,23 @@ static void credential_clear(struct credential *c)
|
|||||||
|
|
||||||
static int credential_read(struct credential *c)
|
static int credential_read(struct credential *c)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t line_len;
|
size_t line_len;
|
||||||
char *key;
|
char *key;
|
||||||
char *value;
|
char *value;
|
||||||
|
|
||||||
key = buf = gnome_keyring_memory_alloc(1024);
|
key = buf = gnome_keyring_memory_alloc(1024);
|
||||||
|
|
||||||
while (fgets(buf, 1024, stdin))
|
while (fgets(buf, 1024, stdin)) {
|
||||||
{
|
|
||||||
line_len = strlen(buf);
|
line_len = strlen(buf);
|
||||||
|
|
||||||
if (line_len && buf[line_len-1] == '\n')
|
if (line_len && buf[line_len-1] == '\n')
|
||||||
buf[--line_len]='\0';
|
buf[--line_len] = '\0';
|
||||||
|
|
||||||
if (!line_len)
|
if (!line_len)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
value = strchr(buf,'=');
|
value = strchr(buf, '=');
|
||||||
if (!value) {
|
if (!value) {
|
||||||
g_warning("invalid credential line: %s", key);
|
g_warning("invalid credential line: %s", key);
|
||||||
gnome_keyring_memory_free(buf);
|
gnome_keyring_memory_free(buf);
|
||||||
@ -384,7 +376,7 @@ static int credential_read(struct credential *c)
|
|||||||
} else if (!strcmp(key, "host")) {
|
} else if (!strcmp(key, "host")) {
|
||||||
g_free(c->host);
|
g_free(c->host);
|
||||||
c->host = g_strdup(value);
|
c->host = g_strdup(value);
|
||||||
value = strrchr(c->host,':');
|
value = strrchr(c->host, ':');
|
||||||
if (value) {
|
if (value) {
|
||||||
*value++ = '\0';
|
*value++ = '\0';
|
||||||
c->port = atoi(value);
|
c->port = atoi(value);
|
||||||
@ -398,7 +390,8 @@ static int credential_read(struct credential *c)
|
|||||||
} else if (!strcmp(key, "password")) {
|
} else if (!strcmp(key, "password")) {
|
||||||
gnome_keyring_memory_free(c->password);
|
gnome_keyring_memory_free(c->password);
|
||||||
c->password = gnome_keyring_memory_strdup(value);
|
c->password = gnome_keyring_memory_strdup(value);
|
||||||
while (*value) *value++ = '\0';
|
while (*value)
|
||||||
|
*value++ = '\0';
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Ignore other lines; we don't know what they mean, but
|
* Ignore other lines; we don't know what they mean, but
|
||||||
@ -429,16 +422,16 @@ static void credential_write(const struct credential *c)
|
|||||||
static void usage(const char *name)
|
static void usage(const char *name)
|
||||||
{
|
{
|
||||||
struct credential_operation const *try_op = credential_helper_ops;
|
struct credential_operation const *try_op = credential_helper_ops;
|
||||||
const char *basename = strrchr(name,'/');
|
const char *basename = strrchr(name, '/');
|
||||||
|
|
||||||
basename = (basename) ? basename + 1 : name;
|
basename = (basename) ? basename + 1 : name;
|
||||||
fprintf(stderr, "usage: %s <", basename);
|
fprintf(stderr, "usage: %s <", basename);
|
||||||
while (try_op->name) {
|
while (try_op->name) {
|
||||||
fprintf(stderr,"%s",(try_op++)->name);
|
fprintf(stderr, "%s", (try_op++)->name);
|
||||||
if (try_op->name)
|
if (try_op->name)
|
||||||
fprintf(stderr,"%s","|");
|
fprintf(stderr, "%s", "|");
|
||||||
}
|
}
|
||||||
fprintf(stderr,"%s",">\n");
|
fprintf(stderr, "%s", ">\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@ -446,7 +439,7 @@ int main(int argc, char *argv[])
|
|||||||
int ret = EXIT_SUCCESS;
|
int ret = EXIT_SUCCESS;
|
||||||
|
|
||||||
struct credential_operation const *try_op = credential_helper_ops;
|
struct credential_operation const *try_op = credential_helper_ops;
|
||||||
struct credential cred = CREDENTIAL_INIT;
|
struct credential cred = CREDENTIAL_INIT;
|
||||||
|
|
||||||
if (!argv[1]) {
|
if (!argv[1]) {
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
|
Loading…
Reference in New Issue
Block a user