add: include magic part of pathspec on --refresh error
[git/debian.git] / builtin / credential-cache.c
blob9b3f709905979ef213e76069b573390f4eda7333
1 #include "builtin.h"
2 #include "parse-options.h"
4 #ifndef NO_UNIX_SOCKETS
6 #include "credential.h"
7 #include "string-list.h"
8 #include "unix-socket.h"
9 #include "run-command.h"
11 #define FLAG_SPAWN 0x1
12 #define FLAG_RELAY 0x2
14 static int send_request(const char *socket, const struct strbuf *out)
16 int got_data = 0;
17 int fd = unix_stream_connect(socket);
19 if (fd < 0)
20 return -1;
22 if (write_in_full(fd, out->buf, out->len) < 0)
23 die_errno("unable to write to cache daemon");
24 shutdown(fd, SHUT_WR);
26 while (1) {
27 char in[1024];
28 int r;
30 r = read_in_full(fd, in, sizeof(in));
31 if (r == 0 || (r < 0 && errno == ECONNRESET))
32 break;
33 if (r < 0)
34 die_errno("read error from cache daemon");
35 write_or_die(1, in, r);
36 got_data = 1;
38 close(fd);
39 return got_data;
42 static void spawn_daemon(const char *socket)
44 struct child_process daemon = CHILD_PROCESS_INIT;
45 char buf[128];
46 int r;
48 strvec_pushl(&daemon.args,
49 "credential-cache--daemon", socket,
50 NULL);
51 daemon.git_cmd = 1;
52 daemon.no_stdin = 1;
53 daemon.out = -1;
55 if (start_command(&daemon))
56 die_errno("unable to start cache daemon");
57 r = read_in_full(daemon.out, buf, sizeof(buf));
58 if (r < 0)
59 die_errno("unable to read result code from cache daemon");
60 if (r != 3 || memcmp(buf, "ok\n", 3))
61 die("cache daemon did not start: %.*s", r, buf);
62 close(daemon.out);
65 static void do_cache(const char *socket, const char *action, int timeout,
66 int flags)
68 struct strbuf buf = STRBUF_INIT;
70 strbuf_addf(&buf, "action=%s\n", action);
71 strbuf_addf(&buf, "timeout=%d\n", timeout);
72 if (flags & FLAG_RELAY) {
73 if (strbuf_read(&buf, 0, 0) < 0)
74 die_errno("unable to relay credential");
77 if (send_request(socket, &buf) < 0) {
78 if (errno != ENOENT && errno != ECONNREFUSED)
79 die_errno("unable to connect to cache daemon");
80 if (flags & FLAG_SPAWN) {
81 spawn_daemon(socket);
82 if (send_request(socket, &buf) < 0)
83 die_errno("unable to connect to cache daemon");
86 strbuf_release(&buf);
89 static char *get_socket_path(void)
91 struct stat sb;
92 char *old_dir, *socket;
93 old_dir = expand_user_path("~/.git-credential-cache", 0);
94 if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
95 socket = xstrfmt("%s/socket", old_dir);
96 else
97 socket = xdg_cache_home("credential/socket");
98 free(old_dir);
99 return socket;
102 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
104 char *socket_path = NULL;
105 int timeout = 900;
106 const char *op;
107 const char * const usage[] = {
108 "git credential-cache [<options>] <action>",
109 NULL
111 struct option options[] = {
112 OPT_INTEGER(0, "timeout", &timeout,
113 "number of seconds to cache credentials"),
114 OPT_STRING(0, "socket", &socket_path, "path",
115 "path of cache-daemon socket"),
116 OPT_END()
119 argc = parse_options(argc, argv, prefix, options, usage, 0);
120 if (!argc)
121 usage_with_options(usage, options);
122 op = argv[0];
124 if (!socket_path)
125 socket_path = get_socket_path();
126 if (!socket_path)
127 die("unable to find a suitable socket path; use --socket");
129 if (!strcmp(op, "exit"))
130 do_cache(socket_path, op, timeout, 0);
131 else if (!strcmp(op, "get") || !strcmp(op, "erase"))
132 do_cache(socket_path, op, timeout, FLAG_RELAY);
133 else if (!strcmp(op, "store"))
134 do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN);
135 else
136 ; /* ignore unknown operation */
138 return 0;
141 #else
143 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
145 const char * const usage[] = {
146 "git credential-cache [options] <action>",
148 "credential-cache is disabled in this build of Git",
149 NULL
151 struct option options[] = { OPT_END() };
153 argc = parse_options(argc, argv, prefix, options, usage, 0);
154 die(_("credential-cache unavailable; no unix socket support"));
157 #endif /* NO_UNIX_SOCKETS */