@@ -65,11 +65,28 @@ const (
6565 MergeStyleRebaseUpdate MergeStyle = "rebase-update-only"
6666)
6767
68+ // Add head repo remote
69+ func addCacheRepo (staging , cache string ) error {
70+ p := filepath .Join (staging , ".git" , "objects" , "info" , "alternates" )
71+ f , err := os .OpenFile (p , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
72+ if err != nil {
73+ log .Printf ("Could not create .git/objects/info/alternates file in %s: %v\n " , staging , err )
74+ return err
75+ }
76+ defer f .Close ()
77+ data := filepath .Join (cache , "objects" )
78+ if _ , err := fmt .Fprintln (f , data ); err != nil {
79+ log .Printf ("Could not write to .git/objects/info/alternates file in %s: %v\n " , staging , err )
80+ return err
81+ }
82+ return nil
83+ }
84+
6885func CreateQuarantineRepo (baseRepositoryID uint64 , headRepositoryID uint64 , baseBranch string , headBranch string ) (string , error ) {
6986 // Clone base repo
7087 tmpBasePath , err := ioutil .TempDir (os .TempDir (), "merge-" )
7188 if err != nil {
72- log .Fatal (err )
89+ log .Fatal (err ) // TODO: fix crash on request specific failure
7390 }
7491
7592 gitDir := viper .GetString ("GIT_DIR" )
@@ -87,70 +104,52 @@ func CreateQuarantineRepo(baseRepositoryID uint64, headRepositoryID uint64, base
87104 baseBranchName := "base"
88105
89106 // Add head repo remote.
90- addCacheRepo := func (staging , cache string ) error {
91- p := filepath .Join (staging , ".git" , "objects" , "info" , "alternates" )
92- f , err := os .OpenFile (p , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
93- if err != nil {
94- log .Printf ("Could not create .git/objects/info/alternates file in %s: %v\n " , staging , err )
95- os .RemoveAll (tmpBasePath )
96- return err
97- }
98- defer f .Close ()
99- data := filepath .Join (cache , "objects" )
100- if _ , err := fmt .Fprintln (f , data ); err != nil {
101- log .Printf ("Could not write to .git/objects/info/alternates file in %s: %v\n " , staging , err )
102- os .RemoveAll (tmpBasePath )
103- return err
104- }
105- return nil
106- }
107-
108107 if err := addCacheRepo (tmpBasePath , baseRepoPath ); err != nil {
109- log .Printf ("Unable to add base repository to temporary repo [%s -> %s]: %v\n " , baseRepoPath , tmpBasePath , err )
108+ log .Printf ("unable to add base repository to temporary repo [%s -> %s]: %v\n " , baseRepoPath , tmpBasePath , err )
110109 os .RemoveAll (tmpBasePath )
111- return "" , fmt .Errorf ("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v" , baseRepoPath , err )
110+ return "" , fmt .Errorf ("unable to add base repository to temporary repo [%s -> tmpBasePath]: %v" , baseRepoPath , err )
112111 }
113112
114113 cmd := exec .Command ("git" , "remote" , "add" , "-t" , baseBranch , "-m" , baseBranch , "origin" , baseRepoPath )
115114 cmd .Dir = tmpBasePath
116115 out , err := cmd .Output ()
117116 if err != nil {
118- log .Printf ("Unable to add base repository as origin [%s -> %s]: %v\n %s\n " , baseRepoPath , tmpBasePath , err , string (out ))
117+ log .Printf ("unable to add base repository as origin [%s -> %s]: %v\n %s\n " , baseRepoPath , tmpBasePath , err , string (out ))
119118 os .RemoveAll (tmpBasePath )
120- return "" , fmt .Errorf ("Unable to add base repository as origin [%s -> tmpBasePath]: %v\n %s" , baseRepoPath , err , out )
119+ return "" , fmt .Errorf ("unable to add base repository as origin [%s -> tmpBasePath]: %v\n %s" , baseRepoPath , err , out )
121120 }
122121
123122 cmd = exec .Command ("git" , "fetch" , "origin" , "--no-tags" , "--" , baseBranch + ":" + baseBranchName , baseBranch + ":original_" + baseBranchName )
124123 cmd .Dir = tmpBasePath
125124 out , err = cmd .Output ()
126125 if err != nil {
127- log .Printf ("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n %s\n " , baseRepoPath , baseBranch , tmpBasePath , err , string (out ))
126+ log .Printf ("unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n %s\n " , baseRepoPath , baseBranch , tmpBasePath , err , string (out ))
128127 os .RemoveAll (tmpBasePath )
129- return "" , fmt .Errorf ("Unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n %s" , baseRepoPath , baseBranch , err , string (out ))
128+ return "" , fmt .Errorf ("unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n %s" , baseRepoPath , baseBranch , err , string (out ))
130129 }
131130
132131 cmd = exec .Command ("git" , "symbolic-ref" , "HEAD" , BranchPrefix + baseBranchName )
133132 cmd .Dir = tmpBasePath
134133 out , err = cmd .Output ()
135134 if err != nil {
136- log .Printf ("Unable to set HEAD as base branch [%s]: %v\n %s\n " , tmpBasePath , err , string (out ))
135+ log .Printf ("unable to set HEAD as base branch [%s]: %v\n %s\n " , tmpBasePath , err , string (out ))
137136 os .RemoveAll (tmpBasePath )
138- return "" , fmt .Errorf ("Unable to set HEAD as base branch [tmpBasePath]: %v\n %s" , err , string (out ))
137+ return "" , fmt .Errorf ("unable to set HEAD as base branch [tmpBasePath]: %v\n %s" , err , string (out ))
139138 }
140139
141140 if err := addCacheRepo (tmpBasePath , headRepoPath ); err != nil {
142- log .Printf ("Unable to add head repository to temporary repo [%s -> %s]: %v\n " , headRepoPath , tmpBasePath , err )
141+ log .Printf ("unable to add head repository to temporary repo [%s -> %s]: %v\n " , headRepoPath , tmpBasePath , err )
143142 os .RemoveAll (tmpBasePath )
144- return "" , fmt .Errorf ("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v" , headRepoPath , err )
143+ return "" , fmt .Errorf ("unable to head base repository to temporary repo [%s -> tmpBasePath]: %v" , headRepoPath , err )
145144 }
146145
147146 cmd = exec .Command ("git" , "remote" , "add" , remoteRepoName , headRepoPath )
148147 cmd .Dir = tmpBasePath
149148 out , err = cmd .Output ()
150149 if err != nil {
151- log .Printf ("Unable to add head repository as head_repo [%s -> %s]: %v\n %s\n " , headRepoPath , tmpBasePath , err , string (out ))
150+ log .Printf ("unable to add head repository as head_repo [%s -> %s]: %v\n %s\n " , headRepoPath , tmpBasePath , err , string (out ))
152151 os .RemoveAll (tmpBasePath )
153- return "" , fmt .Errorf ("Unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n %s" , headRepoPath , err , string (out ))
152+ return "" , fmt .Errorf ("unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n %s" , headRepoPath , err , string (out ))
154153 }
155154
156155 trackingBranch := "tracking"
@@ -160,9 +159,42 @@ func CreateQuarantineRepo(baseRepositoryID uint64, headRepositoryID uint64, base
160159 cmd .Dir = tmpBasePath
161160 out , err = cmd .Output ()
162161 if err != nil {
162+ log .Printf ("unable to fetch head_repo head branch [%s:%s -> tracking in %s]: %v:\n %s\n " , headRepoPath , headBranch , tmpBasePath , err , string (out ))
163+ os .RemoveAll (tmpBasePath )
164+ return "" , fmt .Errorf ("unable to fetch head_repo head branch [%s:%s -> tracking in tmpBasePath]: %v\n %s" , headRepoPath , headBranch , err , string (out ))
165+ }
166+
167+ return tmpBasePath , nil
168+ }
169+
170+ func CreateReadOnlyQuarantineRepo (baseRepositoryID uint64 , headRepositoryID uint64 ) (string , error ) {
171+ // Clone base repo
172+ tmpBasePath , err := ioutil .TempDir (os .TempDir (), "merge-" )
173+ if err != nil {
174+ log .Fatal (err )
175+ }
176+
177+ gitDir := viper .GetString ("GIT_DIR" )
178+ baseRepoPath := path .Join (gitDir , fmt .Sprintf ("%v.git" , baseRepositoryID ))
179+ headRepoPath := path .Join (gitDir , fmt .Sprintf ("%v.git" , headRepositoryID ))
180+
181+ _ , err = git .InitRepository (tmpBasePath , false )
182+ if err != nil {
183+ log .Printf ("git init tmpBasePath: %v\n " , err )
184+ os .RemoveAll (tmpBasePath )
185+ return "" , err
186+ }
187+
188+ if err := addCacheRepo (tmpBasePath , baseRepoPath ); err != nil {
189+ log .Printf ("unable to add base repository to temporary repo [%s -> %s]: %v\n " , baseRepoPath , tmpBasePath , err )
190+ os .RemoveAll (tmpBasePath )
191+ return "" , fmt .Errorf ("unable to add base repository to temporary repo [%s -> tmpBasePath]: %v" , baseRepoPath , err )
192+ }
193+
194+ if err := addCacheRepo (tmpBasePath , headRepoPath ); err != nil {
195+ log .Printf ("unable to add head repository to temporary repo [%s -> %s]: %v\n " , headRepoPath , tmpBasePath , err )
163196 os .RemoveAll (tmpBasePath )
164- log .Printf ("Unable to fetch head_repo head branch [%s:%s -> tracking in %s]: %v:\n %s\n " , headRepoPath , headBranch , tmpBasePath , err , string (out ))
165- return "" , fmt .Errorf ("Unable to fetch head_repo head branch [%s:%s -> tracking in tmpBasePath]: %v\n %s" , headRepoPath , headBranch , err , string (out ))
197+ return "" , fmt .Errorf ("unable to head base repository to temporary repo [%s -> tmpBasePath]: %v" , headRepoPath , err )
166198 }
167199
168200 return tmpBasePath , nil
0 commit comments