Skip to content

Commit 5a26192

Browse files
Fix commit search in all branches (#11849)
* Fix commit search in all branches * comments Co-authored-by: techknowlogick <[email protected]>
1 parent ca8ecf7 commit 5a26192

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

modules/git/repo_commit.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,32 +220,49 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) (*list.List,
220220
}
221221

222222
func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list.List, error) {
223+
// create new git log command with limit of 100 commis
223224
cmd := NewCommand("log", id.String(), "-100", prettyLogFormat)
225+
// ignore case
224226
args := []string{"-i"}
227+
228+
// add authors if present in search query
225229
if len(opts.Authors) > 0 {
226230
for _, v := range opts.Authors {
227231
args = append(args, "--author="+v)
228232
}
229233
}
234+
235+
// add commiters if present in search query
230236
if len(opts.Committers) > 0 {
231237
for _, v := range opts.Committers {
232238
args = append(args, "--committer="+v)
233239
}
234240
}
241+
242+
// add time constraints if present in search query
235243
if len(opts.After) > 0 {
236244
args = append(args, "--after="+opts.After)
237245
}
238246
if len(opts.Before) > 0 {
239247
args = append(args, "--before="+opts.Before)
240248
}
249+
250+
// pretend that all refs along with HEAD were listed on command line as <commis>
251+
// https://git-scm.com/docs/git-log#Documentation/git-log.txt---all
252+
// note this is done only for command created above
241253
if opts.All {
242-
args = append(args, "--all")
254+
cmd.AddArguments("--all")
243255
}
256+
257+
// add remaining keywords from search string
258+
// note this is done only for command created above
244259
if len(opts.Keywords) > 0 {
245260
for _, v := range opts.Keywords {
246261
cmd.AddArguments("--grep=" + v)
247262
}
248263
}
264+
265+
// search for commits matching given constraints and keywords in commit msg
249266
cmd.AddArguments(args...)
250267
stdout, err := cmd.RunInDirBytes(repo.Path)
251268
if err != nil {
@@ -254,12 +271,21 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list
254271
if len(stdout) != 0 {
255272
stdout = append(stdout, '\n')
256273
}
274+
275+
// if there are any keywords (ie not commiter:, author:, time:)
276+
// then let's iterate over them
257277
if len(opts.Keywords) > 0 {
258278
for _, v := range opts.Keywords {
279+
// ignore anything below 4 characters as too unspecific
259280
if len(v) >= 4 {
281+
// create new git log command with 1 commit limit
260282
hashCmd := NewCommand("log", "-1", prettyLogFormat)
283+
// add previous arguments except for --grep and --all
261284
hashCmd.AddArguments(args...)
285+
// add keyword as <commit>
262286
hashCmd.AddArguments(v)
287+
288+
// search with given constraints for commit matching sha hash of v
263289
hashMatching, err := hashCmd.RunInDirBytes(repo.Path)
264290
if err != nil || bytes.Contains(stdout, hashMatching) {
265291
continue

0 commit comments

Comments
 (0)