-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitconfig_test.go
More file actions
614 lines (497 loc) · 15.4 KB
/
gitconfig_test.go
File metadata and controls
614 lines (497 loc) · 15.4 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package gitconfig
import (
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html#EXAMPLES
var configSampleDocs = `#
# This is the config file, and
# a '#' or ';' character indicates
# a comment
#
; core variables
[core]
; Don't trust file modes
filemode = false
; Our diff algorithm
[diff]
external = /usr/local/bin/diff-wrapper
renames = true
; Proxy settings
[core]
gitproxy = default-proxy ; default proxy
; HTTP
[http]
sslVerify
[http "https://weak.example.com"]
sslVerify = false
cookieFile = /tmp/cookie.txt
`
var configSampleComplex = `
[alias]
# add
a = add # add
aa = add --all
all = add -A
chunkyadd = add --patch # stage commits chunk by chunk
# branch
b = branch -v # branch (verbose)
branches = branch -a
recent = branch --sort=-committerdate
# commit
c = commit -m
ca = commit -am
ci = commit
credit = "!f() { git commit --amend --author \"$1 <$2>\" -C HEAD; }; f" # Credit an author on the latest commit
credit = commit --amend --author "$1 <$2>" -C HEAD
amend = commit --amend
commend = commit --amend --no-edit
# checkout
co = checkout # checkout
nb = checkout -b # create and switch to a new branch (mnemonic: "git new branch branchname...")
go = checkout -B # Switch to a branch, creating it if necessary
# clone
cr = clone --recursive # Clone a repository including all submodules
# cherry-pick
cp = cherry-pick -x # grab a change from a branch
# diff
d = !"git diff-index --quiet HEAD -- || clear; git diff --patch-with-stat" # Show the diff between the latest commit and the current state
di = diff
dc = diff --cached
div = divergence # Divergence (commits we added and commits remote added)
ds = diff --stat=160,120
gn = goodness # Goodness (summary of diff lines added/removed/total)
gnc = goodness --cached
last = diff HEAD^
# log
l = log --pretty=oneline -n 20 --graph # View the SHA, description, and history graph of the latest 20 commits
changes = log --pretty=format:\"%h %cr %cn %Cgreen%s%Creset\" --name-status
short = log --pretty=format:\"%h %cr %cn %Cgreen%s%Creset\"
changelog = log --pretty=format:\" * %s\"
shortnocolor = log --pretty=format:\"%h %cr %cn %s\"
show-graph = log --graph --abbrev-commit --pretty=oneline
# pull
please = push --force-with-lease
pl = pull
fa = fetch --all
ff = merge --ff-only
noff = merge --no-ff
pullff = pull --ff-only
p = !"git pull; git submodule foreach git pull origin master" # Pull in remote changes for the current repository and all its submodules
pp = !"git pull ; git push origin master"
ppd = !"git pull origin develop ; git push origin develop"
mdm = !"git checkout master ; git pull origin master ; git push origin master ; git merge develop ; git push origin master ; git checkout develop"
# push
ps = push
pom = pull origin master
pum = push origin master
# rebase
rc = rebase --continue # continue rebase
rs = rebase --skip # skip rebase
reb = "!r() { git rebase -i HEAD~$1; }; r" # Interactive rebase with the given number of latest commits
# remote
r = remote -v
remotes = remote -v
# reset
unstage = reset HEAD # remove files from index (tracking)
uncommit = reset --soft HEAD^ # go back before last commit, with files in uncommitted state
undo = reset --soft HEAD^
filelog = log -u # show changes to a file
mt = mergetool # fire up the merge tool
# stash
ss = stash # stash changes
sl = stash list # list stashes
sa = stash apply # apply stash (restore changes)
sd = stash drop # drop stashes (destroy changes)
stsh = stash --keep-index
staash = stash --include-untracked
staaash = stahs --all
# status
s = status -s # View the current working tree status using the short format
st = status # status
stat = status # status
shorty = status --short --branch
# tag
t = tag -n # show tags with <n> lines of each tag message
tags = tag -l # Show verbose output about tags, branches or remotes
# init
it = !"git init && git commit -m "root" --allow-empty"
# merge
merc = merge --no-ff
grog = log --graph --abbrev-commit --decorate --all --format=format:\"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)\"
[apply]
# Detect whitespace errors when applying a patch
whitespace = fix
[branch]
autosetupmerge = true
[core]
editor = vim
excludesfile = ~/.gitignore
attributesfile = ~/.gitattributes
# Treat spaces before tabs, lines that are indented with 8 or more spaces, and all kinds of trailing whitespace as an error
whitespace = space-before-tab,indent-with-non-tab,trailing-space
autocrlf = input
protectHFS = true
protectNTFS = true
sshCommand = ssh -oControlMaster=auto -oControlPersist=600 -oControlPath=/tmp/.ssh-%C
[receive]
fsckObjects = true
quotepath = false
[color]
# Use colors in Git commands that are capable of colored output when outputting to the terminal
ui = auto
[diff]
[format]
pretty = format:%C(blue)%ad%Creset %C(yellow)%h%C(green)%d%Creset %C(blue)%s %C(magenta) [%an]%Creset
[mergetool]
prompt = false
#[mergetool "mvimdiff"]
# cmd="mvim -c 'Gdiff' $MERGED" # use fugitive.vim for 3-way merge
# keepbackup=false
[merge]
# Include summaries of merged commits in newly created merge commit messages
log = true
summary = true
verbosity = 1
# tool = mvimdiff
# Use origin as the default remote on the master branch in all cases
[branch "master \""]
remote = origin
merge = refs/heads/master
[github]
user = johndoe
# URL shorthands
[url "git@github.com:"]
insteadOf = "gh:"
pushInsteadOf = "github:"
pushInsteadOf = "git://github.com/"
insteadOf = https://github.com
[url "git://github.com/"]
insteadOf = "github:"
[url "git@gist.github.com:"]
insteadOf = "gst:"
pushInsteadOf = "gist:"
pushInsteadOf = "git://gist.github.com/"
[url "git://gist.github.com/"]
insteadOf = "gist:"
[url "git@gitlab.com:"]
insteadOf = https://gitlab.com/
[url "git@gitlab.com:"]
insteadOf = http://gitlab.com/
[user]
email = john.doe@gmail.com
name = John Doe
signingkey = DEADBEEF
[push]
default = simple
[gc]
auto = 64
autopacklimit = 64
[pull]
rebase = false
[init]
defaultBranch = master
[fetch]
prune = true
[credential]
helper = osxkeychain
`
var configSampleGopass = `
# This is a gopass config file
[core]
autoimport = true
cliptimeout = 45
editor = vim
exportkeys = true
pager = false
notifications = true
[generate]
autoclip = true
[show]
safecontent = false
[mounts]
path = /home/johndoe/.password-store
[mounts "foo/sub"]
path = /home/johndoe/.password-store-foo-sub
[mounts "work"]
path = /home/johndoe/.password-store-work
[domain-alias "foo.com"]
insteadOf = foo.de
[domain-alias "foo.com"]
insteadOf = foo.it
`
func TestGopassSingleRead(t *testing.T) {
t.Parallel()
c := &Configs{
global: ParseConfig(strings.NewReader(configSampleGopass)),
}
c.global.noWrites = true
assert.Equal(t, "true", c.Get("generate.autoclip"))
assert.Equal(t, "true", c.Get("core.autoimport"))
assert.Equal(t, "45", c.Get("core.cliptimeout"))
assert.Equal(t, "vim", c.Get("core.editor"))
assert.Equal(t, "true", c.Get("core.exportkeys"))
assert.Equal(t, "false", c.Get("core.pager"))
assert.Equal(t, "true", c.Get("core.notifications"))
assert.Equal(t, "false", c.Get("show.safecontent"))
assert.Equal(t, []string{"foo.de", "foo.it"}, c.GetAll("domain-alias.foo.com.insteadOf"))
assert.Equal(t, "/home/johndoe/.password-store", c.Get("mounts.path"))
assert.Equal(t, "/home/johndoe/.password-store-foo-sub", c.Get("mounts.foo/sub.path"))
assert.Equal(t, "/home/johndoe/.password-store-work", c.Get("mounts.work.path"))
t.Logf("Raw:\n%s\n", c.global.raw.String())
t.Logf("Vars:\n%+v\n", c.global.vars)
}
func TestGopassReadWrite(t *testing.T) {
td := t.TempDir()
sysCfg := filepath.Join(td, "system", "config")
require.NoError(t, os.MkdirAll(filepath.Dir(sysCfg), 0o700))
require.NoError(t, os.WriteFile(sysCfg, []byte(`[core]
autoimport = true
editor = vim
cliptimeout = 45
exportkeys = true
pager = false
notifications = true
`), 0o600))
globalCfg := filepath.Join(td, "global", "config")
require.NoError(t, os.MkdirAll(filepath.Dir(globalCfg), 0o700))
require.NoError(t, os.WriteFile(globalCfg, []byte(`[generate]
autoclip = true
pager = false
[show]
safecontent = false
[mounts]
path = /home/johndoe/.password-store
[mounts "foo/sub"]
path = /home/johndoe/.password-store-foo-sub
[mounts "work"]
path = /home/johndoe/.password-store-work
[domain-alias "foo.com"]
insteadOf = foo.de
[domain-alias "foo.com"]
insteadOf = foo.it
`), 0o600))
t.Setenv("GOPASS_HOMEDIR", td)
c := New()
c.SystemConfig = sysCfg
c.GlobalConfig = filepath.Join("global", "config")
c.global.path = globalCfg
c.LoadAll(td)
assert.Equal(t, "true", c.Get("generate.autoclip"))
assert.Equal(t, "true", c.Get("core.autoimport"))
assert.Equal(t, "45", c.Get("core.cliptimeout"))
assert.Equal(t, "vim", c.Get("core.editor"))
assert.Equal(t, "true", c.Get("core.exportkeys"))
assert.Equal(t, "false", c.Get("core.pager"))
assert.Equal(t, "true", c.Get("core.notifications"))
assert.Equal(t, "false", c.Get("show.safecontent"))
assert.Equal(t, []string{"foo.de", "foo.it"}, c.GetAll("domain-alias.foo.com.insteadOf"))
assert.Equal(t, "/home/johndoe/.password-store", c.Get("mounts.path"))
assert.Equal(t, "/home/johndoe/.password-store-foo-sub", c.Get("mounts.foo/sub.path"))
assert.Equal(t, "/home/johndoe/.password-store-work", c.Get("mounts.work.path"))
t.Logf("Raw:\n%s\n", c.global.raw.String())
t.Logf("Vars:\n%+v\n", c.global.vars)
}
func TestParseSimple(t *testing.T) {
t.Parallel()
c := ParseConfig(strings.NewReader(configSampleDocs))
for k, v := range c.vars {
t.Logf("%s => %s\n", k, v)
}
want := map[string]string{
"core.filemode": "false",
"diff.external": "/usr/local/bin/diff-wrapper",
"diff.renames": "true",
"core.gitproxy": "default-proxy",
// TODO(gitconfig): "http.sslVerify": "", // not supported, yet
"http.https://weak.example.com.sslVerify": "false",
"http.https://weak.example.com.cookieFile": "/tmp/cookie.txt",
}
for k, w := range want {
v, ok := c.Get(k)
assert.True(t, ok)
assert.Equal(t, w, v)
}
}
func TestParseComplex(t *testing.T) {
t.Parallel()
c := ParseConfig(strings.NewReader(configSampleComplex))
// variable names are case-insensitive
_, ok := c.vars["core.sshcommand"]
assert.True(t, ok)
v, ok := c.Get("core.sshCommand")
assert.True(t, ok)
assert.Equal(t, "ssh -oControlMaster=auto -oControlPersist=600 -oControlPath=/tmp/.ssh-%C", v)
}
func TestParseDocs(t *testing.T) {
t.Parallel()
c := ParseConfig(strings.NewReader(configSampleComplex)) //nolint:staticcheck
v, ok := c.Get("core.sshCommand")
assert.True(t, ok)
assert.Equal(t, "ssh -oControlMaster=auto -oControlPersist=600 -oControlPath=/tmp/.ssh-%C", v)
}
func TestSet(t *testing.T) {
t.Parallel()
c := ParseConfig(strings.NewReader(configSampleDocs))
c.noWrites = true
require.NoError(t, c.Set("core.gitproxy", "foobar"))
want := strings.ReplaceAll(configSampleDocs, "default-proxy", "foobar")
assert.Equal(t, want, c.raw.String())
}
func TestUnset(t *testing.T) {
t.Parallel()
c := ParseConfig(strings.NewReader(configSampleDocs))
c.noWrites = true
require.NoError(t, c.Unset("core.filemode"))
want := `#
# This is the config file, and
# a '#' or ';' character indicates
# a comment
#
; core variables
[core]
; Don't trust file modes
; Our diff algorithm
[diff]
external = /usr/local/bin/diff-wrapper
renames = true
; Proxy settings
[core]
gitproxy = default-proxy ; default proxy
; HTTP
[http]
sslVerify
[http "https://weak.example.com"]
sslVerify = false
cookieFile = /tmp/cookie.txt
`
assert.Equal(t, want, c.raw.String())
}
func TestSetEmptyConfig(t *testing.T) {
t.Parallel()
td := t.TempDir()
c := &Config{
path: filepath.Join(td, "config"),
noWrites: false,
}
require.Error(t, c.Set("foobar", "baz"))
require.NoError(t, c.Set("foo.bar", "baz"))
v, ok := c.Get("foo.bar")
assert.True(t, ok)
assert.Equal(t, "baz", v)
buf, err := os.ReadFile(c.path)
require.NoError(t, err)
assert.Equal(t, "[foo]\n\tbar = baz\n", string(buf))
}
func TestList(t *testing.T) {
t.Parallel()
c := &Configs{
global: ParseConfig(strings.NewReader(configSampleGopass)),
}
c.global.noWrites = true
assert.Equal(t, []string{
"mounts.foo/sub.path",
"mounts.path",
"mounts.work.path",
}, c.List("mounts."))
}
func TestListSections(t *testing.T) {
t.Parallel()
c := &Configs{global: ParseConfig(strings.NewReader(configSampleGopass))}
c.global.noWrites = true
assert.Equal(t, []string{"core", "domain-alias", "generate", "mounts", "show"}, c.ListSections())
}
func TestListSubsections(t *testing.T) {
t.Parallel()
c := &Configs{global: ParseConfig(strings.NewReader(configSampleGopass))}
c.global.noWrites = true
assert.Equal(t, []string{"foo/sub", "work"}, c.ListSubsections("mounts"))
}
func TestGitBinary(t *testing.T) {
t.Skip("not ready, yet") // TODO(gitconfig) make tests pass
cfgs := New()
cfgs.LoadAll(".")
cmd := exec.Command("git", "config", "--list")
buf, err := cmd.Output()
require.NoError(t, err)
lines := strings.Split(string(buf), "\n")
for _, line := range lines {
p := strings.SplitN(line, "=", 2)
if len(p) < 2 {
continue
}
key := p[0]
want := p[1]
assert.Equal(t, want, cfgs.Get(key), key)
}
}
func TestGitCliList(t *testing.T) {
t.Parallel()
t.Skip("not ready, yet") // TODO(gitconfig) make tests pass
c := New()
c.NoWrites = true
c.LoadAll("./.git")
cmd := exec.CommandContext(t.Context(), "git", "config", "--list")
buf, err := cmd.Output()
require.NoError(t, err)
cliOut := strings.Split(string(buf), "\n")
sort.Strings(cliOut)
// filter invalid lines and duplicates
cliOut = func(s []string) []string {
prev := ""
out := make([]string, 0, len(s))
for _, l := range s {
if !strings.Contains(l, "=") {
continue
}
if l == prev {
continue
}
out = append(out, l)
prev = l
}
return out
}(cliOut)
libOut := c.KVList("", "=")
// filter duplicates
libOut = func(s []string) []string {
prev := ""
out := make([]string, 0, len(s))
for _, l := range s {
if l == prev {
continue
}
out = append(out, l)
prev = l
}
return out
}(libOut)
// TODO: Enable this asserting when we have burned down all diffs.
// assert.Equal(t, cliOut, libOut)
diffs := 0
t.Logf("% 38s | % 38s | % 5s", "CLI", "LIB", "DIFF")
for i := 0; i < len(cliOut) || i < len(libOut); i++ {
left := ""
right := ""
if i < len(cliOut) {
left = cliOut[i]
}
if i < len(libOut) {
right = libOut[i]
}
left = strings.TrimSpace(left)
right = strings.TrimSpace(right)
// t.Logf("% 38s | % 38s | %t", left, right, left != right)
if left != right {
t.Logf("% 38s | % 38s | %t", left, right, left != right)
diffs++
}
}
assert.Zero(t, diffs, "There are %d differences between the CLI and the library output", diffs)
}