-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.nix
More file actions
165 lines (144 loc) · 5.2 KB
/
git.nix
File metadata and controls
165 lines (144 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{ config, pkgs, lib, ... }:
with lib;
let
conf = config.git;
esc = escapeShellArg;
mapAttrsToLines = f: attrs: concatStringsSep "\n" (mapAttrsToList f attrs);
get_remote = role: remote:
let r = getAttr remote conf.remotes;
in if isAttrs r then getAttr role r else r;
# Generate the [remote "name"] section of a remote.
mk_remote_conf = name: url: {
"remote \"${name}\"" = {
url = if isAttrs url then url.fetch else url;
pushurl = mkIf (isAttrs url) url.push;
fetch = "+refs/heads/*:refs/remotes/${name}/*";
};
};
# Write a git config into a file. Argument is a
# attributes of attributes of strings
gen_git_config = filename: conf:
builtins.toFile filename (mapAttrsToLines (section: opts: ''
[${section}]
${mapAttrsToLines (key: val: " ${key} = ${val}") opts}
'') conf);
in {
options.git = with types; {
remotes = mkOption {
type = attrsOf (either str (submodule [{
options = {
fetch = mkOption { type = str; };
push = mkOption { type = str; };
};
}]));
default = { };
description = ''
Git remotes. When the workspace is activated, new remotes defined here
are added to the Git repository automatically and changed URL are
updated.
Adding a remote is enough to activate this module. The repository is
cloned on the first time the workspace is opened.
'';
};
main_branch = mkOption {
type = nullOr str;
default = null;
description = ''
Defines the 'MAIN' symbolic ref. Also used for the initial checkout.
By default, the default branch is taken from the remote repository
during the initial checkout. This can only work if the 'main_remote'
option is set to a configured remote.
If it is not set at the time of activation, it is guessed.
'';
};
main_remote = mkOption {
type = str;
default = "origin";
description = "See the 'main_branch' option.";
};
gitignore = mkOption {
type = lines;
default = "";
description = "Local gitignore rules.";
};
config = mkOption {
type = attrsOf (attrsOf str);
description = ''
Local git config options. The toplevel attributes represents the
sections and the nested attributes are the config options.
'"remotes.*".url' and similar are set by the 'remotes' option.
'';
};
};
config = mkIf (conf.remotes != { }) {
buildInputs = with pkgs; [ git ];
git.config = {
core.excludesFile = mkIf (conf.gitignore != "")
(builtins.toFile "gitignore" conf.gitignore);
} // concatMapAttrs mk_remote_conf conf.remotes;
# If the 'main_remote' option correspond to a configured remote,
# Use 'git clone' if possible, fallback to 'git init' if the
# 'main_remote' option doesn't correspond to a configured remote.
# If the main branch is set to be guessed, it might only be set in the
# 'git clone' branch and won't be set in the fallback branch.
init_script = ''
${if hasAttr conf.main_remote conf.remotes then ''
git clone --origin=${esc conf.main_remote} ${
esc (get_remote "fetch" conf.main_remote)
} .
${if conf.main_branch == null then ''
# Set the 'MAIN' symbolic ref to the HEAD advertised by the remote.
MAIN=$(git symbolic-ref --short HEAD)
git symbolic-ref MAIN "refs/heads/$MAIN"
'' else
""}
'' else ''
git init ${
if conf.main_branch != null then
"--initial-branch=${esc conf.main_branch}"
else
""
}
''}
git fetch --all --tags --update-head-ok --no-show-forced-updates --force
'';
activation_script = ''
${
# Remove remotes and ignore rules previously set using 'git remote' and
# '.git/info/exclude' as they take precedence over the new method using an
# included config file.
""}
# Migrate workspaces using an old format
if ! git config get --local --value="^/nix/store/.*-workspace.git$" include.path &>/dev/null; then
# Remove all remotes
while read name; do
git remote remove "$name"
done < <(git remote)
rm -f .git/info/exclude # Now set through config
fi
git config set --local --all --value="^/nix/store/.*-workspace.git$" "include.path" ${
gen_git_config "workspace.git" conf.config
}
${
# If the 'main_branch' option is set, the 'MAIN' symbolic ref is updated to
# point to the specified branch.
# If it is not set, the branch to use is guessed from a list of probable main
# branches.
if conf.main_branch == null then ''
guess_default_branch ()
{
local default=$(git config init.defaultBranch)
for guess in "$default" main master trunk; do
if [[ -e .git/refs/heads/$guess ]]; then
git symbolic-ref MAIN "refs/heads/$guess"
return
fi
done
}
if ! [[ -e .git/MAIN ]]; then guess_default_branch; fi
'' else ''
echo "ref: "${esc "refs/heads/${conf.main_branch}"} > .git/MAIN
''}
'';
};
}