|
5 | 5 |
|
6 | 6 | package models |
7 | 7 |
|
8 | | -import ( |
9 | | - "fmt" |
10 | | - |
11 | | - "code.gitea.io/gitea/modules/git" |
12 | | - "code.gitea.io/gitea/modules/log" |
13 | | -) |
14 | | - |
15 | 8 | // CanCreateBranch returns true if repository meets the requirements for creating new branches. |
16 | 9 | func (repo *Repository) CanCreateBranch() bool { |
17 | 10 | return !repo.IsMirror |
18 | 11 | } |
19 | | - |
20 | | -// GetBranch returns a branch by its name |
21 | | -func (repo *Repository) GetBranch(branch string) (*git.Branch, error) { |
22 | | - gitRepo, err := git.OpenRepository(repo.RepoPath()) |
23 | | - if err != nil { |
24 | | - return nil, err |
25 | | - } |
26 | | - defer gitRepo.Close() |
27 | | - |
28 | | - return gitRepo.GetBranch(branch) |
29 | | -} |
30 | | - |
31 | | -// GetBranches returns all the branches of a repository |
32 | | -func (repo *Repository) GetBranches() ([]*git.Branch, error) { |
33 | | - return git.GetBranchesByPath(repo.RepoPath()) |
34 | | -} |
35 | | - |
36 | | -// CheckBranchName validates branch name with existing repository branches |
37 | | -func (repo *Repository) CheckBranchName(name string) error { |
38 | | - gitRepo, err := git.OpenRepository(repo.RepoPath()) |
39 | | - if err != nil { |
40 | | - return err |
41 | | - } |
42 | | - defer gitRepo.Close() |
43 | | - |
44 | | - branches, err := repo.GetBranches() |
45 | | - if err != nil { |
46 | | - return err |
47 | | - } |
48 | | - |
49 | | - for _, branch := range branches { |
50 | | - if branch.Name == name { |
51 | | - return ErrBranchAlreadyExists{branch.Name} |
52 | | - } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) || |
53 | | - (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) { |
54 | | - return ErrBranchNameConflict{branch.Name} |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - if _, err := gitRepo.GetTag(name); err == nil { |
59 | | - return ErrTagAlreadyExists{name} |
60 | | - } |
61 | | - |
62 | | - return nil |
63 | | -} |
64 | | - |
65 | | -// CreateNewBranch creates a new repository branch |
66 | | -func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) { |
67 | | - // Check if branch name can be used |
68 | | - if err := repo.CheckBranchName(branchName); err != nil { |
69 | | - return err |
70 | | - } |
71 | | - |
72 | | - if !git.IsBranchExist(repo.RepoPath(), oldBranchName) { |
73 | | - return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName) |
74 | | - } |
75 | | - |
76 | | - basePath, err := CreateTemporaryPath("branch-maker") |
77 | | - if err != nil { |
78 | | - return err |
79 | | - } |
80 | | - defer func() { |
81 | | - if err := RemoveTemporaryPath(basePath); err != nil { |
82 | | - log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err) |
83 | | - } |
84 | | - }() |
85 | | - |
86 | | - if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{ |
87 | | - Bare: true, |
88 | | - Shared: true, |
89 | | - }); err != nil { |
90 | | - log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) |
91 | | - return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) |
92 | | - } |
93 | | - |
94 | | - gitRepo, err := git.OpenRepository(basePath) |
95 | | - if err != nil { |
96 | | - log.Error("Unable to open temporary repository: %s (%v)", basePath, err) |
97 | | - return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) |
98 | | - } |
99 | | - defer gitRepo.Close() |
100 | | - |
101 | | - if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil { |
102 | | - log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err) |
103 | | - return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err) |
104 | | - } |
105 | | - |
106 | | - if err = git.Push(basePath, git.PushOptions{ |
107 | | - Remote: "origin", |
108 | | - Branch: branchName, |
109 | | - Env: PushingEnvironment(doer, repo), |
110 | | - }); err != nil { |
111 | | - return fmt.Errorf("Push: %v", err) |
112 | | - } |
113 | | - |
114 | | - return nil |
115 | | -} |
116 | | - |
117 | | -// CreateNewBranchFromCommit creates a new repository branch |
118 | | -func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) { |
119 | | - // Check if branch name can be used |
120 | | - if err := repo.CheckBranchName(branchName); err != nil { |
121 | | - return err |
122 | | - } |
123 | | - basePath, err := CreateTemporaryPath("branch-maker") |
124 | | - if err != nil { |
125 | | - return err |
126 | | - } |
127 | | - defer func() { |
128 | | - if err := RemoveTemporaryPath(basePath); err != nil { |
129 | | - log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err) |
130 | | - } |
131 | | - }() |
132 | | - |
133 | | - if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{ |
134 | | - Bare: true, |
135 | | - Shared: true, |
136 | | - }); err != nil { |
137 | | - log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) |
138 | | - return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) |
139 | | - } |
140 | | - |
141 | | - gitRepo, err := git.OpenRepository(basePath) |
142 | | - if err != nil { |
143 | | - log.Error("Unable to open temporary repository: %s (%v)", basePath, err) |
144 | | - return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) |
145 | | - } |
146 | | - defer gitRepo.Close() |
147 | | - |
148 | | - if err = gitRepo.CreateBranch(branchName, commit); err != nil { |
149 | | - log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err) |
150 | | - return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err) |
151 | | - } |
152 | | - |
153 | | - if err = git.Push(basePath, git.PushOptions{ |
154 | | - Remote: "origin", |
155 | | - Branch: branchName, |
156 | | - Env: PushingEnvironment(doer, repo), |
157 | | - }); err != nil { |
158 | | - return fmt.Errorf("Push: %v", err) |
159 | | - } |
160 | | - |
161 | | - return nil |
162 | | -} |
0 commit comments