MCPcopy
hub / github.com/github/github-mcp-server / initializeRepository

Function initializeRepository

pkg/github/repositories_helper.go:18–67  ·  view source on GitHub ↗

initializeRepository creates an initial commit in an empty repository and returns the default branch ref and base commit

(ctx context.Context, client *github.Client, owner, repo string)

Source from the content-addressed store, hash-verified

16
17// initializeRepository creates an initial commit in an empty repository and returns the default branch ref and base commit
18func initializeRepository(ctx context.Context, client *github.Client, owner, repo string) (ref *github.Reference, baseCommit *github.Commit, err error) {
19 // First, we need to check what the default branch in this empty repo should be:
20 repository, resp, err := client.Repositories.Get(ctx, owner, repo)
21 if err != nil {
22 _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get repository", resp, err)
23 return nil, nil, fmt.Errorf("failed to get repository: %w", err)
24 }
25 if resp != nil && resp.Body != nil {
26 defer func() { _ = resp.Body.Close() }()
27 }
28
29 defaultBranch := repository.GetDefaultBranch()
30
31 fileOpts := &github.RepositoryContentFileOptions{
32 Message: github.Ptr("Initial commit"),
33 Content: []byte(""),
34 Branch: github.Ptr(defaultBranch),
35 }
36
37 // Create an initial empty commit to create the default branch
38 createResp, resp, err := client.Repositories.CreateFile(ctx, owner, repo, "README.md", fileOpts)
39 if err != nil {
40 _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to create initial file", resp, err)
41 return nil, nil, fmt.Errorf("failed to create initial file: %w", err)
42 }
43 if resp != nil && resp.Body != nil {
44 defer func() { _ = resp.Body.Close() }()
45 }
46
47 // Get the commit that was just created to use as base for remaining files
48 baseCommit, resp, err = client.Git.GetCommit(ctx, owner, repo, *createResp.Commit.SHA)
49 if err != nil {
50 _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get initial commit", resp, err)
51 return nil, nil, fmt.Errorf("failed to get initial commit: %w", err)
52 }
53 if resp != nil && resp.Body != nil {
54 defer func() { _ = resp.Body.Close() }()
55 }
56
57 ref, resp, err = client.Git.GetRef(ctx, owner, repo, "refs/heads/"+defaultBranch)
58 if err != nil {
59 _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
60 return nil, nil, fmt.Errorf("failed to get branch reference after initial commit: %w", err)
61 }
62 if resp != nil && resp.Body != nil {
63 defer func() { _ = resp.Body.Close() }()
64 }
65
66 return ref, baseCommit, nil
67}
68
69// createReferenceFromDefaultBranch creates a new branch reference from the repository's default branch
70func createReferenceFromDefaultBranch(ctx context.Context, client *github.Client, owner, repo, branch string) (*github.Reference, error) {

Callers 1

PushFilesFunction · 0.85

Calls 1

CloseMethod · 0.80

Tested by

no test coverage detected