github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/reflog.go (about) 1 package git 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 ) 8 9 type ReflogDeleteOptions struct{} 10 11 type ReflogExpireOptions struct { 12 ReflogDeleteOptions 13 14 Expire string 15 All bool 16 } 17 18 // Returns true if a reflog exists for refname r under client. 19 func ReflogExists(c *Client, r Refname) bool { 20 path := filepath.Join(c.GitDir.String(), "logs", string(r)) 21 return c.GitDir.File(File(path)).Exists() 22 } 23 24 func ReflogExpire(c *Client, opts ReflogExpireOptions, refpatterns []string) error { 25 if opts.Expire != "now" || len(refpatterns) != 0 { 26 return fmt.Errorf("Only reflog --expire=now --all currently supported") 27 } 28 if opts.All && len(refpatterns) != 0 { 29 return fmt.Errorf("Can not combine --all with explicit refs") 30 } 31 32 // If expire is now, we just truncate applicable reflogs 33 if opts.Expire == "now" && opts.All { 34 // This is a hack to get fsck's test setup working. Since 35 // we're expiring everything, we just truncate everything 36 // in the .git/logs directory. (We can't delete them, because 37 // the reflogs need to still exist.) 38 // 39 // (There's a catch-22 where the fsck tests depend on reflog, 40 // and the reflog tests depend on fsck.) 41 filepath.Walk(filepath.Join(c.GitDir.String(), "logs"), 42 func(path string, info os.FileInfo, err error) error { 43 if info.IsDir() { 44 return nil 45 } 46 47 // Use Create to truncate the file. We know it exists 48 // because we got here from a Walk to it, so we don't 49 // worry about creating new files. 50 fi, err := os.Create(path) 51 if err != nil { 52 return err 53 } 54 if err := fi.Close(); err != nil { 55 return err 56 } 57 return nil 58 }) 59 } 60 return nil 61 }