@@ -7,12 +7,12 @@ package models
77import (
88 "fmt"
99 "io/ioutil"
10+ "net/url"
1011 "os"
1112 "path"
1213 "path/filepath"
1314 "strings"
1415 "sync"
15- "net/url"
1616
1717 "github.com/Unknwon/com"
1818
@@ -116,6 +116,23 @@ func (repo *Repository) UpdateLocalWiki() error {
116116 return updateLocalCopy (repo .WikiPath (), repo .LocalWikiPath ())
117117}
118118
119+ // discardLocalWikiChanges discards local commits make sure
120+ // it is even to remote branch when local copy exists.
121+ func discardLocalWikiChanges (localPath string ) error {
122+ if ! com .IsExist (localPath ) {
123+ return nil
124+ }
125+ // No need to check if nothing in the repository.
126+ if ! git .IsBranchExist (localPath , "master" ) {
127+ return nil
128+ }
129+
130+ if err := git .ResetHEAD (localPath , true , "origin/master" ); err != nil {
131+ return fmt .Errorf ("ResetHEAD: %v" , err )
132+ }
133+ return nil
134+ }
135+
119136// updateWikiPage adds new page to repository wiki.
120137func (repo * Repository ) updateWikiPage (doer * User , oldTitle , title , content , message string , isNew bool ) (err error ) {
121138 wikiWorkingPool .CheckIn (com .ToStr (repo .ID ))
@@ -126,18 +143,9 @@ func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, mes
126143 }
127144
128145 localPath := repo .LocalWikiPath ()
129-
130- // Discard local commits make sure even to remote when local copy exists.
131- if com .IsExist (localPath ) {
132- // No need to check if nothing in the repository.
133- if git .IsBranchExist (localPath , "master" ) {
134- if err = git .ResetHEAD (localPath , true , "origin/master" ); err != nil {
135- return fmt .Errorf ("Reset: %v" , err )
136- }
137- }
138- }
139-
140- if err = repo .UpdateLocalWiki (); err != nil {
146+ if err = discardLocalWikiChanges (localPath ); err != nil {
147+ return fmt .Errorf ("discardLocalWikiChanges: %v" , err )
148+ } else if err = repo .UpdateLocalWiki (); err != nil {
141149 return fmt .Errorf ("UpdateLocalWiki: %v" , err )
142150 }
143151
@@ -184,23 +192,14 @@ func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
184192 defer wikiWorkingPool .CheckOut (com .ToStr (repo .ID ))
185193
186194 localPath := repo .LocalWikiPath ()
187-
188- // Discard local commits make sure even to remote when local copy exists.
189- if com .IsExist (localPath ) {
190- // No need to check if nothing in the repository.
191- if git .IsBranchExist (localPath , "master" ) {
192- if err = git .ResetHEAD (localPath , true , "origin/master" ); err != nil {
193- return fmt .Errorf ("Reset: %v" , err )
194- }
195- }
196- }
197-
198- if err = repo .UpdateLocalWiki (); err != nil {
195+ if err = discardLocalWikiChanges (localPath ); err != nil {
196+ return fmt .Errorf ("discardLocalWikiChanges: %v" , err )
197+ } else if err = repo .UpdateLocalWiki (); err != nil {
199198 return fmt .Errorf ("UpdateLocalWiki: %v" , err )
200199 }
201200
201+ title = ToWikiPageName (strings .Replace (title , "/" , " " , - 1 ))
202202 filename := path .Join (localPath , title + ".md" )
203-
204203 os .Remove (filename )
205204
206205 message := "Delete page '" + title + "'"
0 commit comments