@@ -82,11 +82,14 @@ func getLocalRepoHeadRef(config *GitXargsConfig, localRepository *git.Repository
82
82
return ref , nil
83
83
}
84
84
85
- // executeCommand runs the user-supplied command and runs it against the given repository, adding any git changes that may occur
86
- func executeCommand (config * GitXargsConfig , repositoryDir string , repo * github.Repository , worktree * git. Worktree ) error {
87
-
88
- logger := logging . GetLogger ( "git-xargs" )
85
+ // executeCommand runs the user-supplied command against the given repository
86
+ func executeCommand (config * GitXargsConfig , repositoryDir string , repo * github.Repository ) error {
87
+ return executeCommandWithLogger ( config , repositoryDir , repo , logging . GetLogger ( "git-xargs" ))
88
+ }
89
89
90
+ // executeCommandWithLogger runs the user-supplied command against the given repository, and sends the log output
91
+ // to the given logger
92
+ func executeCommandWithLogger (config * GitXargsConfig , repositoryDir string , repo * github.Repository , logger * logrus.Logger ) error {
90
93
if len (config .Args ) < 1 {
91
94
return errors .WithStackTrace (NoCommandSuppliedErr {})
92
95
}
@@ -104,6 +107,10 @@ func executeCommand(config *GitXargsConfig, repositoryDir string, repo *github.R
104
107
105
108
stdoutStdErr , err := cmd .CombinedOutput ()
106
109
110
+ logger .WithFields (logrus.Fields {
111
+ "CombinedOutput" : string (stdoutStdErr ),
112
+ }).Debug ("Received output of command run" )
113
+
107
114
if err != nil {
108
115
logger .WithFields (logrus.Fields {
109
116
"Error" : err ,
@@ -113,58 +120,6 @@ func executeCommand(config *GitXargsConfig, repositoryDir string, repo *github.R
113
120
return errors .WithStackTrace (err )
114
121
}
115
122
116
- logger .WithFields (logrus.Fields {
117
- "CombinedOutput" : string (stdoutStdErr ),
118
- }).Debug ("Received output of command run" )
119
-
120
- status , statusErr := worktree .Status ()
121
-
122
- if statusErr != nil {
123
- logger .WithFields (logrus.Fields {
124
- "Error" : statusErr ,
125
- "Repo" : repo .GetName (),
126
- "Dir" : repositoryDir ,
127
- }).Debug ("Error looking up worktree status" )
128
-
129
- // Track the status check failure
130
- config .Stats .TrackSingle (WorktreeStatusCheckFailedCommand , repo )
131
- return errors .WithStackTrace (statusErr )
132
- }
133
-
134
- // If the supplied command resulted in any changes, we need to stage, add and commit them
135
- if ! status .IsClean () {
136
- logger .WithFields (logrus.Fields {
137
- "Repo" : repo .GetName (),
138
- }).Debug ("Local repository worktree no longer clean, will stage and add new files and commit changes" )
139
-
140
- // Track the fact that worktree changes were made following execution
141
- config .Stats .TrackSingle (WorktreeStatusDirty , repo )
142
-
143
- for filepath := range status {
144
- if status .IsUntracked (filepath ) {
145
- fmt .Printf ("Found untracked file %s. Adding to stage" , filepath )
146
- _ , addErr := worktree .Add (filepath )
147
- if addErr != nil {
148
- logger .WithFields (logrus.Fields {
149
- "Error" : addErr ,
150
- "Filepath" : filepath ,
151
- }).Debug ("Error adding file to git stage" )
152
- // Track the file staging failure
153
- config .Stats .TrackSingle (WorktreeAddFileFailed , repo )
154
- return errors .WithStackTrace (addErr )
155
- }
156
- }
157
- }
158
-
159
- } else {
160
- logger .WithFields (logrus.Fields {
161
- "Repo" : repo .GetName (),
162
- }).Debug ("Local repository status is clean - nothing to stage or commit" )
163
-
164
- // Track the fact that repo had no file changes post command execution
165
- config .Stats .TrackSingle (WorktreeStatusClean , repo )
166
- }
167
-
168
123
return nil
169
124
}
170
125
@@ -251,12 +206,61 @@ func checkoutLocalBranch(config *GitXargsConfig, ref *plumbing.Reference, worktr
251
206
return branchName , nil
252
207
}
253
208
254
- // commitLocalChanges will create a commit using the supplied or default commit message and will add any untracked, deleted
255
- // or modified files that resulted from script execution
256
- func commitLocalChanges (config * GitXargsConfig , worktree * git.Worktree , remoteRepository * github.Repository , localRepository * git.Repository ) error {
257
-
209
+ // commitLocalChanges will check for any changes in worktree as a result of script execution, and if any are present,
210
+ // add any untracked, deleted or modified files and create a commit using the supplied or default commit message.
211
+ func commitLocalChanges (config * GitXargsConfig , repositoryDir string , worktree * git.Worktree , remoteRepository * github.Repository , localRepository * git.Repository ) error {
258
212
logger := logging .GetLogger ("git-xargs" )
259
213
214
+ status , statusErr := worktree .Status ()
215
+
216
+ if statusErr != nil {
217
+ logger .WithFields (logrus.Fields {
218
+ "Error" : statusErr ,
219
+ "Repo" : remoteRepository .GetName (),
220
+ "Dir" : repositoryDir ,
221
+ }).Debug ("Error looking up worktree status" )
222
+
223
+ // Track the status check failure
224
+ config .Stats .TrackSingle (WorktreeStatusCheckFailedCommand , remoteRepository )
225
+ return errors .WithStackTrace (statusErr )
226
+ }
227
+
228
+ // If there are no changes, we log it, track it, and return
229
+ if status .IsClean () {
230
+ logger .WithFields (logrus.Fields {
231
+ "Repo" : remoteRepository .GetName (),
232
+ }).Debug ("Local repository status is clean - nothing to stage or commit" )
233
+
234
+ // Track the fact that repo had no file changes post command execution
235
+ config .Stats .TrackSingle (WorktreeStatusClean , remoteRepository )
236
+
237
+ return nil
238
+ }
239
+
240
+ // If there are changes, we need to stage, add and commit them
241
+ logger .WithFields (logrus.Fields {
242
+ "Repo" : remoteRepository .GetName (),
243
+ }).Debug ("Local repository worktree no longer clean, will stage and add new files and commit changes" )
244
+
245
+ // Track the fact that worktree changes were made following execution
246
+ config .Stats .TrackSingle (WorktreeStatusDirty , remoteRepository )
247
+
248
+ for filepath := range status {
249
+ if status .IsUntracked (filepath ) {
250
+ fmt .Printf ("Found untracked file %s. Adding to stage" , filepath )
251
+ _ , addErr := worktree .Add (filepath )
252
+ if addErr != nil {
253
+ logger .WithFields (logrus.Fields {
254
+ "Error" : addErr ,
255
+ "Filepath" : filepath ,
256
+ }).Debug ("Error adding file to git stage" )
257
+ // Track the file staging failure
258
+ config .Stats .TrackSingle (WorktreeAddFileFailed , remoteRepository )
259
+ return errors .WithStackTrace (addErr )
260
+ }
261
+ }
262
+ }
263
+
260
264
// With all our untracked files staged, we can now create a commit, passing the All
261
265
// option when configuring our commit option so that all modified and deleted files
262
266
// will have their changes committed
0 commit comments