Skip to content

Commit 706fc59

Browse files
GiteaBotwxiaoguang
andauthored
Use correct hash for "git update-index" (#30626) (#30634)
Backport #30626 by @wxiaoguang Co-authored-by: wxiaoguang <[email protected]>
1 parent 4aba4f8 commit 706fc59

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

services/repository/files/temp_repo.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,18 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro
136136

137137
// RemoveFilesFromIndex removes the given files from the index
138138
func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) error {
139+
objFmt, err := t.gitRepo.GetObjectFormat()
140+
if err != nil {
141+
return fmt.Errorf("unable to get object format for temporary repo: %q, error: %w", t.repo.FullName(), err)
142+
}
139143
stdOut := new(bytes.Buffer)
140144
stdErr := new(bytes.Buffer)
141145
stdIn := new(bytes.Buffer)
142146
for _, file := range filenames {
143147
if file != "" {
144-
stdIn.WriteString("0 0000000000000000000000000000000000000000\t")
145-
stdIn.WriteString(file)
146-
stdIn.WriteByte('\000')
148+
// man git-update-index: input syntax (1): mode SP sha1 TAB path
149+
// mode=0 means "remove from index", then hash part "does not matter as long as it is well formatted."
150+
_, _ = fmt.Fprintf(stdIn, "0 %s\t%s\x00", objFmt.EmptyObjectID(), file)
147151
}
148152
}
149153

@@ -154,8 +158,7 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) er
154158
Stdout: stdOut,
155159
Stderr: stdErr,
156160
}); err != nil {
157-
log.Error("Unable to update-index for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String())
158-
return fmt.Errorf("Unable to update-index for temporary repo: %s Error: %w\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String())
161+
return fmt.Errorf("unable to update-index for temporary repo: %q, error: %w\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String())
159162
}
160163
return nil
161164
}

0 commit comments

Comments
 (0)