2 #include "credential.h"
3 #include "string-list.h"
4 #include "parse-options.h"
6 static int lookup_credential(const char *fn
, struct credential
*c
)
8 config_exclusive_filename
= fn
;
9 credential_from_config(c
);
10 return c
->username
&& c
->password
;
13 static void store_item(const char *fn
, const char *unique
,
14 const char *item
, const char *value
)
16 struct strbuf key
= STRBUF_INIT
;
21 config_exclusive_filename
= fn
;
24 strbuf_addf(&key
, "credential.%s.%s", unique
, item
);
25 git_config_set(key
.buf
, value
);
29 static void store_credential(const char *fn
, struct credential
*c
)
31 store_item(fn
, c
->unique
, "username", c
->username
);
32 store_item(fn
, c
->unique
, "password", c
->password
);
35 static void remove_credential(const char *fn
, struct credential
*c
)
37 store_item(fn
, c
->unique
, "username", NULL
);
38 store_item(fn
, c
->unique
, "password", NULL
);
41 int main(int argc
, const char **argv
)
43 const char * const usage
[] = {
44 "git credential-store [options]",
47 struct credential c
= { NULL
};
48 struct string_list chain
= STRING_LIST_INIT_NODUP
;
51 struct option options
[] = {
52 OPT_STRING_LIST(0, "store", &store
, "file",
53 "fetch and store credentials in <file>"),
54 OPT_STRING_LIST(0, "chain", &chain
, "helper",
55 "use <helper> to get non-cached credentials"),
56 OPT_BOOLEAN(0, "reject", &reject
,
57 "reject a stored credential"),
58 OPT_STRING(0, "username", &c
.username
, "name",
59 "an existing username"),
60 OPT_STRING(0, "description", &c
.description
, "desc",
61 "human-readable description of the credential"),
62 OPT_STRING(0, "unique", &c
.unique
, "token",
63 "a unique context for the credential"),
67 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
69 usage_with_options(usage
, options
);
72 store
= expand_user_path("~/.git-credentials");
74 die("unable to set up default store; use --store");
77 remove_credential(store
, &c
);
79 if (!lookup_credential(store
, &c
)) {
80 credential_fill(&c
, &chain
);
81 store_credential(store
, &c
);
83 printf("username=%s\n", c
.username
);
84 printf("password=%s\n", c
.password
);