summaryrefslogtreecommitdiffstats
path: root/modules/repofiles/temp_repo.go
blob: 46e03f5650232b65a506e0d2be9465fa4e6faaeb (plain)
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
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repofiles

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path"
	"regexp"
	"strings"
	"time"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/process"
	"code.gitea.io/gitea/modules/setting"

	"github.com/Unknwon/com"
)

// TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
type TemporaryUploadRepository struct {
	repo     *models.Repository
	gitRepo  *git.Repository
	basePath string
}

// NewTemporaryUploadRepository creates a new temporary upload repository
func NewTemporaryUploadRepository(repo *models.Repository) (*TemporaryUploadRepository, error) {
	timeStr := com.ToStr(time.Now().Nanosecond()) // SHOULD USE SOMETHING UNIQUE
	basePath := path.Join(models.LocalCopyPath(), "upload-"+timeStr+".git")
	if err := os.MkdirAll(path.Dir(basePath), os.ModePerm); err != nil {
		return nil, fmt.Errorf("failed to create dir %s: %v", basePath, err)
	}
	if repo.RepoPath() == "" {
		return nil, fmt.Errorf("no path to repository on system")
	}
	t := &TemporaryUploadRepository{repo: repo, basePath: basePath}
	return t, nil
}

// Close the repository cleaning up all files
func (t *TemporaryUploadRepository) Close() {
	if _, err := os.Stat(t.basePath); !os.IsNotExist(err) {
		os.RemoveAll(t.basePath)
	}
}

// Clone the base repository to our path and set branch as the HEAD
func (t *TemporaryUploadRepository) Clone(branch string) error {
	if _, stderr, err := process.GetManager().ExecTimeout(5*time.Minute,
		fmt.Sprintf("Clone (git clone -s --bare): %s", t.basePath),
		"git", "clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath); err != nil {
		if matched, _ := regexp.MatchString(".*Remote branch .* not found in upstream origin.*", stderr); matched {
			return git.ErrBranchNotExist{
				Name: branch,
			}
		} else if matched, _ := regexp.MatchString(".* repository .* does not exist.*", stderr); matched {
			return models.ErrRepoNotExist{
				ID:        t.repo.ID,
				UID:       t.repo.OwnerID,
				OwnerName: t.repo.OwnerName,
				Name:      t.repo.Name,
			}
		} else {
			return fmt.Errorf("Clone: %v %s", err, stderr)
		}
	}
	gitRepo, err := git.OpenRepository(t.basePath)
	if err != nil {
		return err
	}
	t.gitRepo = gitRepo
	return nil
}

// SetDefaultIndex sets the git index to our HEAD
func (t *TemporaryUploadRepository) SetDefaultIndex() error {
	if _, stderr, err := process.GetManager().ExecDir(5*time.Minute,
		t.basePath,
		fmt.Sprintf("SetDefaultIndex (git read-tree HEAD): %s", t.basePath),
		"git", "read-tree", "HEAD"); err != nil {
		return fmt.Errorf("SetDefaultIndex: %v %s", err, stderr)
	}
	return nil
}

// LsFiles checks if the given filename arguments are in the index
func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, error) {
	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)

	timeout := 5 * time.Minute
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	cmdArgs := []string{"ls-files", "-z", "--"}
	for _, arg := range filenames {
		if arg != "" {
			cmdArgs = append(cmdArgs, arg)
		}
	}

	cmd := exec.CommandContext(ctx, "git", cmdArgs...)
	desc := fmt.Sprintf("lsFiles: (git ls-files) %v", cmdArgs)
	cmd.Dir = t.basePath
	cmd.Stdout = stdOut
	cmd.Stderr = stdErr

	if err := cmd.Start(); err != nil {
		return nil, fmt.Errorf("exec(%s) failed: %v(%v)", desc, err, ctx.Err())
	}

	pid := process.GetManager().Add(desc, cmd)
	err := cmd.Wait()
	process.GetManager().Remove(pid)

	if err != nil {
		err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOut, stdErr)
		return nil, err
	}

	filelist := make([]string, len(filenames))
	for _, line := range bytes.Split(stdOut.Bytes(), []byte{'\000'}) {
		filelist = append(filelist, string(line))
	}

	return filelist, err
}

// RemoveFilesFromIndex removes the given files from the index
func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) error {
	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)
	stdIn := new(bytes.Buffer)
	for _, file := range filenames {
		if file != "" {
			stdIn.WriteString("0 0000000000000000000000000000000000000000\t")
			stdIn.WriteString(file)
			stdIn.WriteByte('\000')
		}
	}

	timeout := 5 * time.Minute
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	cmdArgs := []string{"update-index", "--remove", "-z", "--index-info"}
	cmd := exec.CommandContext(ctx, "git", cmdArgs...)
	desc := fmt.Sprintf("removeFilesFromIndex: (git update-index) %v", filenames)
	cmd.Dir = t.basePath
	cmd.Stdout = stdOut
	cmd.Stderr = stdErr
	cmd.Stdin = bytes.NewReader(stdIn.Bytes())

	if err := cmd.Start(); err != nil {
		return fmt.Errorf("exec(%s) failed: %v(%v)", desc, err, ctx.Err())
	}

	pid := process.GetManager().Add(desc, cmd)
	err := cmd.Wait()
	process.GetManager().Remove(pid)

	if err != nil {
		err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOut, stdErr)
	}

	return err
}

// HashObject writes the provided content to the object db and returns its hash
func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error) {
	timeout := 5 * time.Minute
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	hashCmd := exec.CommandContext(ctx, "git", "hash-object", "-w", "--stdin")
	hashCmd.Dir = t.basePath
	hashCmd.Stdin = content
	stdOutBuffer := new(bytes.Buffer)
	stdErrBuffer := new(bytes.Buffer)
	hashCmd.Stdout = stdOutBuffer
	hashCmd.Stderr = stdErrBuffer
	desc := fmt.Sprintf("hashObject: (git hash-object)")
	if err := hashCmd.Start(); err != nil {
		return "", fmt.Errorf("git hash-object: %s", err)
	}

	pid := process.GetManager().Add(desc, hashCmd)
	err := hashCmd.Wait()
	process.GetManager().Remove(pid)

	if err != nil {
		err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOutBuffer, stdErrBuffer)
		return "", err
	}

	return strings.TrimSpace(stdOutBuffer.String()), nil
}

// AddObjectToIndex adds the provided object hash to the index with the provided mode and path
func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPath string) error {
	if _, stderr, err := process.GetManager().ExecDir(5*time.Minute,
		t.basePath,
		fmt.Sprintf("addObjectToIndex (git update-index): %s", t.basePath),
		"git", "update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath); err != nil {
		if matched, _ := regexp.MatchString(".*Invalid path '.*", stderr); matched {
			return models.ErrFilePathInvalid{
				Message: objectPath,
				Path:    objectPath,
			}
		}
		return fmt.Errorf("git update-index: %s", stderr)
	}
	return nil
}

// WriteTree writes the current index as a tree to the object db and returns its hash
func (t *TemporaryUploadRepository) WriteTree() (string, error) {
	treeHash, stderr, err := process.GetManager().ExecDir(5*time.Minute,
		t.basePath,
		fmt.Sprintf("WriteTree (git write-tree): %s", t.basePath),
		"git", "write-tree")
	if err != nil {
		return "", fmt.Errorf("git write-tree: %s", stderr)
	}
	return strings.TrimSpace(treeHash), nil
}

// GetLastCommit gets the last commit ID SHA of the repo
func (t *TemporaryUploadRepository) GetLastCommit() (string, error) {
	return t.GetLastCommitByRef("HEAD")
}

// GetLastCommitByRef gets the last commit ID SHA of the repo by ref
func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, error) {
	if ref == "" {
		ref = "HEAD"
	}
	treeHash, stderr, err := process.GetManager().ExecDir(5*time.Minute,
		t.basePath,
		fmt.Sprintf("GetLastCommit (git rev-parse %s): %s", ref, t.basePath),
		"git", "rev-parse", ref)
	if err != nil {
		return "", fmt.Errorf("git rev-parse %s: %s", ref, stderr)
	}
	return strings.TrimSpace(treeHash), nil
}

// CommitTree creates a commit from a given tree for the user with provided message
func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, treeHash string, message string) (string, error) {
	commitTimeStr := time.Now().Format(time.UnixDate)
	authorSig := author.NewGitSig()
	committerSig := committer.NewGitSig()

	// FIXME: Should we add SSH_ORIGINAL_COMMAND to this
	// Because this may call hooks we should pass in the environment
	env := append(os.Environ(),
		"GIT_AUTHOR_NAME="+authorSig.Name,
		"GIT_AUTHOR_EMAIL="+authorSig.Email,
		"GIT_AUTHOR_DATE="+commitTimeStr,
		"GIT_COMMITTER_NAME="+committerSig.Name,
		"GIT_COMMITTER_EMAIL="+committerSig.Email,
		"GIT_COMMITTER_DATE="+commitTimeStr,
	)
	commitHash, stderr, err := process.GetManager().ExecDirEnv(5*time.Minute,
		t.basePath,
		fmt.Sprintf("commitTree (git commit-tree): %s", t.basePath),
		env,
		"git", "commit-tree", treeHash, "-p", "HEAD", "-m", message)
	if err != nil {
		return "", fmt.Errorf("git commit-tree: %s", stderr)
	}
	return strings.TrimSpace(commitHash), nil
}

// Push the provided commitHash to the repository branch by the provided user
func (t *TemporaryUploadRepository) Push(doer *models.User, commitHash string, branch string) error {
	isWiki := "false"
	if strings.HasSuffix(t.repo.Name, ".wiki") {
		isWiki = "true"
	}

	sig := doer.NewGitSig()

	// FIXME: Should we add SSH_ORIGINAL_COMMAND to this
	// Because calls hooks we need to pass in the environment
	env := append(os.Environ(),
		"GIT_AUTHOR_NAME="+sig.Name,
		"GIT_AUTHOR_EMAIL="+sig.Email,
		"GIT_COMMITTER_NAME="+sig.Name,
		"GIT_COMMITTER_EMAIL="+sig.Email,
		models.EnvRepoName+"="+t.repo.Name,
		models.EnvRepoUsername+"="+t.repo.OwnerName,
		models.EnvRepoIsWiki+"="+isWiki,
		models.EnvPusherName+"="+doer.Name,
		models.EnvPusherID+"="+fmt.Sprintf("%d", doer.ID),
		models.ProtectedBranchRepoID+"="+fmt.Sprintf("%d", t.repo.ID),
	)

	if _, stderr, err := process.GetManager().ExecDirEnv(5*time.Minute,
		t.basePath,
		fmt.Sprintf("actuallyPush (git push): %s", t.basePath),
		env,
		"git", "push", t.repo.RepoPath(), strings.TrimSpace(commitHash)+":refs/heads/"+strings.TrimSpace(branch)); err != nil {
		return fmt.Errorf("git push: %s", stderr)
	}
	return nil
}

// DiffIndex returns a Diff of the current index to the head
func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
	timeout := 5 * time.Minute
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	stdErr := new(bytes.Buffer)

	cmd := exec.CommandContext(ctx, "git", "diff-index", "--cached", "-p", "HEAD")
	cmd.Dir = t.basePath
	cmd.Stderr = stdErr

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, fmt.Errorf("StdoutPipe: %v stderr %s", err, stdErr.String())
	}

	if err = cmd.Start(); err != nil {
		return nil, fmt.Errorf("Start: %v stderr %s", err, stdErr.String())
	}

	pid := process.GetManager().Add(fmt.Sprintf("diffIndex [repo_path: %s]", t.repo.RepoPath()), cmd)
	defer process.GetManager().Remove(pid)

	diff, err = models.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
	if err != nil {
		return nil, fmt.Errorf("ParsePatch: %v", err)
	}

	if err = cmd.Wait(); err != nil {
		return nil, fmt.Errorf("Wait: %v", err)
	}

	return diff, nil
}

// CheckAttribute checks the given attribute of the provided files
func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...string) (map[string]map[string]string, error) {
	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)

	timeout := 5 * time.Minute
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	cmdArgs := []string{"check-attr", "-z", attribute, "--cached", "--"}
	for _, arg := range args {
		if arg != "" {
			cmdArgs = append(cmdArgs, arg)
		}
	}

	cmd := exec.CommandContext(ctx, "git", cmdArgs...)
	desc := fmt.Sprintf("checkAttr: (git check-attr) %s %v", attribute, cmdArgs)
	cmd.Dir = t.basePath
	cmd.Stdout = stdOut
	cmd.Stderr = stdErr

	if err := cmd.Start(); err != nil {
		return nil, fmt.Errorf("exec(%s) failed: %v(%v)", desc, err, ctx.Err())
	}

	pid := process.GetManager().Add(desc, cmd)
	err := cmd.Wait()
	process.GetManager().Remove(pid)

	if err != nil {
		err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOut, stdErr)
		return nil, err
	}

	fields := bytes.Split(stdOut.Bytes(), []byte{'\000'})

	if len(fields)%3 != 1 {
		return nil, fmt.Errorf("Wrong number of fields in return from check-attr")
	}

	var name2attribute2info = make(map[string]map[string]string)

	for i := 0; i < (len(fields) / 3); i++ {
		filename := string(fields[3*i])
		attribute := string(fields[3*i+1])
		info := string(fields[3*i+2])
		attribute2info := name2attribute2info[filename]
		if attribute2info == nil {
			attribute2info = make(map[string]string)
		}
		attribute2info[attribute] = info
		name2attribute2info[filename] = attribute2info
	}

	return name2attribute2info, err
}

// GetBranchCommit Gets the commit object of the given branch
func (t *TemporaryUploadRepository) GetBranchCommit(branch string) (*git.Commit, error) {
	if t.gitRepo == nil {
		return nil, fmt.Errorf("repository has not been cloned")
	}
	return t.gitRepo.GetBranchCommit(branch)
}

// GetCommit Gets the commit object of the given commit ID
func (t *TemporaryUploadRepository) GetCommit(commitID string) (*git.Commit, error) {
	if t.gitRepo == nil {
		return nil, fmt.Errorf("repository has not been cloned")
	}
	return t.gitRepo.GetCommit(commitID)
}