sequencer: allow disabling conflict advice
[alt-git.git] / builtin / credential-cache.c
blobbba96d4ffd6f198adb186aaba0c853e34a93dd11
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "parse-options.h"
4 #include "path.h"
5 #include "strbuf.h"
6 #include "write-or-die.h"
8 #ifndef NO_UNIX_SOCKETS
10 #include "unix-socket.h"
11 #include "run-command.h"
13 #define FLAG_SPAWN 0x1
14 #define FLAG_RELAY 0x2
16 #ifdef GIT_WINDOWS_NATIVE
18 static int connection_closed(int error)
20 return (error == EINVAL);
23 static int connection_fatally_broken(int error)
25 return (error != ENOENT) && (error != ENETDOWN);
28 #else
30 static int connection_closed(int error)
32 return (error == ECONNRESET);
35 static int connection_fatally_broken(int error)
37 return (error != ENOENT) && (error != ECONNREFUSED);
40 #endif
42 static int send_request(const char *socket, const struct strbuf *out)
44 int got_data = 0;
45 int fd = unix_stream_connect(socket, 0);
47 if (fd < 0)
48 return -1;
50 if (write_in_full(fd, out->buf, out->len) < 0)
51 die_errno("unable to write to cache daemon");
52 shutdown(fd, SHUT_WR);
54 while (1) {
55 char in[1024];
56 int r;
58 r = read_in_full(fd, in, sizeof(in));
59 if (r == 0 || (r < 0 && connection_closed(errno)))
60 break;
61 if (r < 0)
62 die_errno("read error from cache daemon");
63 write_or_die(1, in, r);
64 got_data = 1;
66 close(fd);
67 return got_data;
70 static void spawn_daemon(const char *socket)
72 struct child_process daemon = CHILD_PROCESS_INIT;
73 char buf[128];
74 int r;
76 strvec_pushl(&daemon.args,
77 "credential-cache--daemon", socket,
78 NULL);
79 daemon.git_cmd = 1;
80 daemon.no_stdin = 1;
81 daemon.out = -1;
83 if (start_command(&daemon))
84 die_errno("unable to start cache daemon");
85 r = read_in_full(daemon.out, buf, sizeof(buf));
86 if (r < 0)
87 die_errno("unable to read result code from cache daemon");
88 if (r != 3 || memcmp(buf, "ok\n", 3))
89 die("cache daemon did not start: %.*s", r, buf);
90 close(daemon.out);
93 static void do_cache(const char *socket, const char *action, int timeout,
94 int flags)
96 struct strbuf buf = STRBUF_INIT;
98 strbuf_addf(&buf, "action=%s\n", action);
99 strbuf_addf(&buf, "timeout=%d\n", timeout);
100 if (flags & FLAG_RELAY) {
101 if (strbuf_read(&buf, 0, 0) < 0)
102 die_errno("unable to relay credential");
105 if (send_request(socket, &buf) < 0) {
106 if (connection_fatally_broken(errno))
107 die_errno("unable to connect to cache daemon");
108 if (flags & FLAG_SPAWN) {
109 spawn_daemon(socket);
110 if (send_request(socket, &buf) < 0)
111 die_errno("unable to connect to cache daemon");
114 strbuf_release(&buf);
117 static char *get_socket_path(void)
119 struct stat sb;
120 char *old_dir, *socket;
121 old_dir = interpolate_path("~/.git-credential-cache", 0);
122 if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
123 socket = xstrfmt("%s/socket", old_dir);
124 else
125 socket = xdg_cache_home("credential/socket");
126 free(old_dir);
127 return socket;
130 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
132 char *socket_path = NULL;
133 int timeout = 900;
134 const char *op;
135 const char * const usage[] = {
136 "git credential-cache [<options>] <action>",
137 NULL
139 struct option options[] = {
140 OPT_INTEGER(0, "timeout", &timeout,
141 "number of seconds to cache credentials"),
142 OPT_STRING(0, "socket", &socket_path, "path",
143 "path of cache-daemon socket"),
144 OPT_END()
147 argc = parse_options(argc, argv, prefix, options, usage, 0);
148 if (!argc)
149 usage_with_options(usage, options);
150 op = argv[0];
152 if (!socket_path)
153 socket_path = get_socket_path();
154 if (!socket_path)
155 die("unable to find a suitable socket path; use --socket");
157 if (!strcmp(op, "exit"))
158 do_cache(socket_path, op, timeout, 0);
159 else if (!strcmp(op, "get") || !strcmp(op, "erase"))
160 do_cache(socket_path, op, timeout, FLAG_RELAY);
161 else if (!strcmp(op, "store"))
162 do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN);
163 else
164 ; /* ignore unknown operation */
166 return 0;
169 #else
171 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
173 const char * const usage[] = {
174 "git credential-cache [options] <action>",
176 "credential-cache is disabled in this build of Git",
177 NULL
179 struct option options[] = { OPT_END() };
181 argc = parse_options(argc, argv, prefix, options, usage, 0);
182 die(_("credential-cache unavailable; no unix socket support"));
185 #endif /* NO_UNIX_SOCKETS */