The 19th batch
[git.git] / compat / fsmonitor / fsm-ipc-darwin.c
blobfe149a1b37436db34665f5a436ea9961a34aac36
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "path.h"
8 #include "repository.h"
9 #include "strbuf.h"
10 #include "fsmonitor-ll.h"
11 #include "fsmonitor-ipc.h"
12 #include "fsmonitor-path-utils.h"
14 static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc")
16 const char *fsmonitor_ipc__get_path(struct repository *r)
18 static const char *ipc_path = NULL;
19 git_SHA_CTX sha1ctx;
20 char *sock_dir = NULL;
21 struct strbuf ipc_file = STRBUF_INIT;
22 unsigned char hash[GIT_SHA1_RAWSZ];
24 if (!r)
25 BUG("No repository passed into fsmonitor_ipc__get_path");
27 if (ipc_path)
28 return ipc_path;
31 /* By default the socket file is created in the .git directory */
32 if (fsmonitor__is_fs_remote(r->gitdir) < 1) {
33 ipc_path = fsmonitor_ipc__get_default_path();
34 return ipc_path;
37 git_SHA1_Init(&sha1ctx);
38 git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree));
39 git_SHA1_Final(hash, &sha1ctx);
41 repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir);
43 /* Create the socket file in either socketDir or $HOME */
44 if (sock_dir && *sock_dir) {
45 strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s",
46 sock_dir, hash_to_hex_algop(hash, &hash_algos[GIT_HASH_SHA1]));
47 } else {
48 strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s",
49 hash_to_hex_algop(hash, &hash_algos[GIT_HASH_SHA1]));
51 free(sock_dir);
53 ipc_path = interpolate_path(ipc_file.buf, 1);
54 if (!ipc_path)
55 die(_("Invalid path: %s"), ipc_file.buf);
57 strbuf_release(&ipc_file);
58 return ipc_path;