@@ -46,30 +46,29 @@ Task("Push-Doc")
46
46
}
47
47
48
48
// TODO: [#6] Implement multi-version documentation
49
- string pushWorktreeName = "push-gh-pages" ;
49
+ string worktreeName = "push-gh-pages" ;
50
50
string committerName = "PleOps.Cake Bot" ;
51
51
string committerEmail = "[email protected] " ;
52
52
53
53
using ( Repository repo = new Repository ( GitFindRootFromPath ( "." ) . FullPath ) ) {
54
54
bool ownTree = false ;
55
55
Worktree tree = null ;
56
- if ( repo . Worktrees . Any ( w => w . Name == pushWorktreeName ) ) {
56
+ if ( repo . Worktrees . Any ( w => w . Name == worktreeName ) ) {
57
57
Information ( "Re-use worktree" ) ;
58
- tree = repo . Worktrees [ pushWorktreeName ] ;
58
+ tree = repo . Worktrees [ worktreeName ] ;
59
59
} else {
60
60
Information ( "Create worktree" ) ;
61
61
62
62
// libgit2sharp does not clean the refs and it complains if you run it again later
63
- string refPath = $ "{ repo . Info . Path } /refs/heads/{ pushWorktreeName } ";
63
+ string refPath = $ "{ repo . Info . Path } /refs/heads/{ worktreeName } ";
64
64
if ( FileExists ( refPath ) ) {
65
65
Information ( "Deleting old ref" ) ;
66
66
DeleteFile ( refPath ) ;
67
67
}
68
68
69
69
CreateDirectory ( $ "{ info . ArtifactsDirectory } /tmp") ;
70
70
tree = repo . Worktrees . Add (
71
- "origin/gh-pages" ,
72
- pushWorktreeName ,
71
+ worktreeName ,
73
72
$ "{ info . ArtifactsDirectory } /tmp/gh-pages",
74
73
false ) ; // no lock since it's not a portable media
75
74
ownTree = true ;
@@ -79,6 +78,29 @@ Task("Push-Doc")
79
78
string treePath = treeRepo . Info . WorkingDirectory ;
80
79
Information ( $ "Worktree at: { treePath } ") ;
81
80
81
+ // LibGit2Sharp seems to have a bug where it creates the worktree branch
82
+ // at the current HEAD (main branch), instead at the given reference.
83
+ // So we do the checkout with a pull ourselves.
84
+ var checkoutSettings = new ProcessSettings {
85
+ Arguments = "checkout gh-pages" ,
86
+ WorkingDirectory = treePath ,
87
+ } ;
88
+ int checkoutResult = StartProcess ( "git" , checkoutSettings ) ;
89
+ if ( checkoutResult != 0 ) {
90
+ Error ( "Error creating gh-pages branch" ) ;
91
+ }
92
+
93
+ var pullSettings = new ProcessSettings {
94
+ Arguments = "pull --ff-only origin gh-pages" ,
95
+ WorkingDirectory = treePath ,
96
+ } ;
97
+ int pullResult = StartProcess ( "git" , pullSettings ) ;
98
+ if ( pullResult != 0 ) {
99
+ Error ( "Error pulling" ) ;
100
+ }
101
+
102
+ Information ( $ "Current branch: { treeRepo . Head . FriendlyName } ") ;
103
+
82
104
// Clean directory so we don't keep old files
83
105
// Just move temporary the .git file so it's not deleted.
84
106
CopyFile ( $ "{ treePath } /.git", $ "{ info . ArtifactsDirectory } /tmp/.git") ;
@@ -111,10 +133,10 @@ Task("Push-Doc")
111
133
Information ( "No changes detected, no new commits done" ) ;
112
134
}
113
135
114
-
115
136
if ( ownTree ) {
116
137
Information ( "Prune worktree" ) ;
117
138
repo . Worktrees . Prune ( tree ) ;
139
+ repo . Branches . Remove ( worktreeName ) ;
118
140
}
119
141
}
120
142
} ) ;
0 commit comments