link.go 2.24 KB
Newer Older
1
2
3
4
package objectpool

import (
	"context"
5
	"fmt"
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
6
7
8
	"io/ioutil"
	"os"
	"path/filepath"
9

10
11
	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/git"
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
12
	"gitlab.com/gitlab-org/gitaly/internal/helper"
13
14
)

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
15
16
17
// Link will write the relative path to the object pool from the repository that
// is to join the pool. This does not trigger deduplication, which is the
// responsibility of the caller.
18
func (o *ObjectPool) Link(ctx context.Context, repo *gitalypb.Repository) error {
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	altPath, err := alternatesPath(repo)
	if err != nil {
		return err
	}

	repoPath, err := helper.GetRepoPath(repo)
	if err != nil {
		return err
	}

	relPath, err := filepath.Rel(filepath.Join(repoPath, "objects"), o.FullPath())
	if err != nil {
		return err
	}

34
35
36
37
38
39
40
41
42
43
44
	remoteName := repo.GetGlRepository()
	for k, v := range map[string]string{
		fmt.Sprintf("remote.%s.url", remoteName):    relPath,
		fmt.Sprintf("remote.%s.fetch", remoteName):  fmt.Sprintf("+refs/*:refs/remotes/%s/*", remoteName),
		fmt.Sprintf("remote.%s.tagOpt", remoteName): "--no-tags",
	} {
		if err := o.setConfig(ctx, k, v); err != nil {
			return err
		}
	}

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
45
	return ioutil.WriteFile(altPath, []byte(filepath.Join(relPath, "objects")), 0644)
46
47
}

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
48
// Unlink removes the alternates file, so Git won't look there anymore
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// It removes the remote from the object pool too,
func (o *ObjectPool) Unlink(ctx context.Context, repo *gitalypb.Repository) error {
	if !o.Exists() {
		return nil
	}

	// We need to use removeRemote, and can't leverage `git config --remove-section`
	// as the latter doesn't clean up refs
	remoteName := repo.GetGlRepository()
	if err := o.removeRemote(ctx, remoteName); err != nil {
		if present, err2 := o.hasRemote(ctx, remoteName); err2 != nil || present {
			return err
		}
	}

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
64
65
66
67
68
69
	altPath, err := alternatesPath(repo)
	if err != nil {
		return err
	}

	return os.RemoveAll(altPath)
70
}
71
72
73
74
75
76
77
78
79
80
81
82

// Config options setting will leak the key value pairs in the logs. This makes
// this function not suitable for general usage, and scoped to this package.
// To be corrected in: https://gitlab.com/gitlab-org/gitaly/issues/1430
func (o *ObjectPool) setConfig(ctx context.Context, key, value string) error {
	cmd, err := git.Command(ctx, o, "config", key, value)
	if err != nil {
		return err
	}

	return cmd.Wait()
}