Example #1
0
func tmpRepo(t *testing.T) *git.Repository {
	tmp := tmpdir(t)
	repo, err := git.InitRepository(tmp, true)
	if err != nil {
		t.Fatal(err)
	}
	return repo
}
Example #2
0
// Init initializes a new git-backed database from the following
// elements:
// * A bare git repository at `repo`
// * A git reference name `ref` (for example "refs/heads/foo")
// * An optional scope to expose only a subset of the git tree (for example "/myapp/v1")
func Init(repo, ref string) (*DB, error) {
	r, err := git.InitRepository(repo, true)
	if err != nil {
		return nil, err
	}
	db, err := newRepo(r, ref)
	if err != nil {
		return nil, err
	}
	return db, nil
}
Example #3
0
func CreateVcsRepository(dirName string) (Repository, error) {
	// the directory must already exist!
	repoStat, err := os.Stat(dirName)

	if err != nil {
		return Repository{nil}, err
	}

	if !repoStat.IsDir() {
		return Repository{nil}, errors.New(dirName + " must be a directory!")
	}

	gitRepo, err := git.InitRepository(dirName, false)

	if err != nil {
		return Repository{nil}, err
	}

	return Repository{gitRepo}, nil
}
Example #4
0
func CreateCodebase(localPath string, originURL string) (model.Codebase, error) {
	repo, err := git2go.InitRepository(localPath, true)
	if err != nil {
		return nil, err
	}

	if originURL != "" {
		config, err := repo.Config()
		if err != nil {
			return nil, err
		}

		err = config.SetString("remote.origin.url", originURL)
		if err != nil {
			return nil, err
		}
	}

	return &Codebase{
		repo:      repo,
		originURL: originURL,
	}, nil
}
Example #5
0
func newGraderContext() (*Context, error) {
	dirname, err := ioutil.TempDir("/tmp", "gradertest")
	if err != nil {
		return nil, err
	}
	ctx, err := NewContext(bytes.NewBufferString(
		fmt.Sprintf(
			"{"+
				"\"Logging\": {\"File\": \"stderr\"}, "+
				"\"Tracing\": {\"Enabled\": false}, "+
				"\"InputManager\": {\"CacheSize\": 1024}, "+
				"\"Grader\": {\"RuntimePath\": %q} "+
				"}",
			dirname,
		),
	))
	if err != nil {
		return nil, err
	}
	ctx.Config.Runner.PreserveFiles = os.Getenv("PRESERVE") != ""

	// Git repository for problem.
	repo, err := git.InitRepository(path.Join(dirname, "problems.git/test"), true)
	if err != nil {
		return nil, err
	}
	odb, err := repo.Odb()
	if err != nil {
		return nil, err
	}
	loc, err := time.LoadLocation("Etc/UTC")
	if err != nil {
		return nil, err
	}
	sig := &git.Signature{
		Name:  "Rand Om Hacker",
		Email: "[email protected]",
		When:  time.Date(2015, 11, 30, 03, 01, 0, 0, loc),
	}
	idx, err := repo.Index()
	if err != nil {
		return nil, err
	}
	treeId, err := idx.WriteTree()
	if err != nil {
		return nil, err
	}

	message := "Initial commit\n"
	tree, err := repo.LookupTree(treeId)
	if err != nil {
		return nil, err
	}
	if _, err := repo.CreateCommit("HEAD", sig, sig, message, tree); err != nil {
		return nil, err
	}
	type filecontents struct {
		path, contents string
	}
	gitcommits := []struct {
		message      string
		expectedhash string
		files        []filecontents
	}{
		{
			"first commit\n",
			"6b70f47f4f4d57fcac0ac91ed0c6320218764236",
			[]filecontents{
				{"cases/in/0.in", "1 2"},
				{"cases/out/0.out", "3"},
				{"cases/settings.json", `{
  "Cases": [
		{"Cases": [{"Name": "0", "Weight": 1.0}], "Name": "0", "Weight": 1.0}
  ], 
  "Limits": {
    "ExtraWallTime": 0, 
    "MemoryLimit": 67108864, 
    "OutputLimit": 16384, 
    "OverallWallTimeLimit": 60000, 
    "TimeLimit": 3000, 
    "ValidatorTimeLimit": 3000
  }, 
  "Slow": false, 
	"Validator": {"Name": "token-numeric", "Tolerance": 0.001}
}`},
			},
		},
		{
			"second commit\n",
			"2af3227d22470f4d9730937b6b47fd79622fdb32",
			[]filecontents{
				{"cases/in/0.in", "1 2"},
				{"cases/out/0.out", "3"},
				{"cases/in/1.in", "2 3"},
				{"cases/out/1.out", "5"},
				{"cases/settings.json", `{
  "Cases": [
		{"Cases": [{"Name": "0", "Weight": 0.5}], "Name": "0", "Weight": 0.5}, 
		{"Cases": [{"Name": "1", "Weight": 0.5}], "Name": "1", "Weight": 0.5}
  ], 
  "Limits": {
    "ExtraWallTime": 0, 
    "MemoryLimit": 67108864, 
    "OutputLimit": 16384, 
    "OverallWallTimeLimit": 60000, 
    "TimeLimit": 3000, 
    "ValidatorTimeLimit": 3000
  }, 
  "Slow": false, 
	"Validator": {"Name": "token-numeric", "Tolerance": 0.001}
}`},
			},
		},
	}
	for _, gct := range gitcommits {
		for _, gft := range gct.files {
			oid, err := odb.Write([]byte(gft.contents), git.ObjectBlob)
			if err != nil {
				return nil, err
			}
			if err := idx.Add(&git.IndexEntry{
				Mode: git.FilemodeBlob,
				Size: uint32(len(gft.contents)),
				Id:   oid,
				Path: gft.path,
			}); err != nil {
				return nil, err
			}
		}
		treeId, err := idx.WriteTree()
		if err != nil {
			return nil, err
		}

		currentBranch, err := repo.Head()
		if err != nil {
			return nil, err
		}
		currentTip, err := repo.LookupCommit(currentBranch.Target())
		if err != nil {
			return nil, err
		}

		message := gct.message
		tree, err := repo.LookupTree(treeId)
		if err != nil {
			return nil, err
		}
		casesTree, err := tree.EntryByPath("cases")
		if err != nil {
			return nil, err
		}
		if casesTree.Id.String() != gct.expectedhash {
			return nil, errors.New(fmt.Sprintf(
				"expected %q, got %q",
				gct.expectedhash,
				casesTree.Id.String(),
			))
		}
		if _, err := repo.CreateCommit(
			"HEAD", sig, sig, message, tree, currentTip,
		); err != nil {
			return nil, err
		}
	}

	return ctx, nil
}
Example #6
0
func setupRepo(dir string) (*git.Repository, error) {
	repo, err := git.InitRepository(dir, false)
	if err != nil {
		return nil, err
	}
	odb, err := repo.Odb()
	if err != nil {
		return nil, err
	}

	blobId, err := odb.Write([]byte("hello"), git.ObjectBlob)
	if err != nil {
		return nil, err
	}

	subTree, err := repo.TreeBuilder()
	if err != nil {
		return nil, err
	}
	defer subTree.Free()

	if err = subTree.Insert("subfile", blobId, git.FilemodeBlobExecutable); err != nil {
		return nil, err
	}
	treeId, err := subTree.Write()
	if err != nil {
		return nil, err
	}

	rootTree, err := repo.TreeBuilder()
	if err != nil {
		return nil, err
	}
	defer rootTree.Free()

	if err := rootTree.Insert("dir", treeId, git.FilemodeTree); err != nil {
		return nil, err
	}
	if err := rootTree.Insert("file", blobId, git.FilemodeBlob); err != nil {
		return nil, err
	}
	if err = rootTree.Insert("link", blobId, git.FilemodeLink); err != nil {
		return nil, err
	}

	rootId, err := rootTree.Write()
	if err != nil {
		return nil, err
	}

	root, err := repo.LookupTree(rootId)
	if err != nil {
		return nil, err
	}

	sig := &git.Signature{"user", "[email protected]", time.Now()}
	if _, err := repo.CreateCommit("refs/heads/master", sig, sig,
		"message", root); err != nil {
		return nil, err
	}

	return repo, nil
}
Example #7
0
// Checkout creates a new repository in the generated `public` folder and
// creates a new commit with the generated stuff
func Checkout(pathToPublic, branch string) (*git.Repository, error) {
	repo, err := git.InitRepository(pathToPublic, false)
	if err != nil {
		return nil, err
	}

	idx, err := repo.Index()
	if err != nil {
		return nil, err
	}

	err = filepath.Walk(pathToPublic, func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			log.Println(err)
			return err
		}

		if fi.IsDir() {
			if path[len(pathToPublic):] == ".git" {
				return filepath.SkipDir // Skip entire .git directory
			}
			return nil
		}

		err = idx.AddByPath(path[len(pathToPublic):])
		if err != nil {
			panic(err)
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	treeID, err := idx.WriteTree()
	if err != nil {
		return nil, err
	}

	err = idx.Write()
	if err != nil {
		return nil, err
	}

	tree, err := repo.LookupTree(treeID)
	if err != nil {
		return nil, err
	}

	signature := &git.Signature{
		Name:  "Hugopages",
		Email: "[email protected]",
		When:  time.Now(),
	}

	message := "Publish to gh-pages"
	_, err = repo.CreateCommit("refs/heads/"+branch, signature, signature, message, tree)
	if err != nil {
		return nil, err
	}

	return repo, nil
}