all repos

mugit @ 4af560ca6ad26b59b39cd04800777c7b2895372f

馃惍 git server that your cow will love
3 files changed, 67 insertions(+), 2 deletions(-)
fix: now /compare can compare a branch with a tag
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-05-26 15:17:59 +0300
Authored at: 2026-05-20 14:11:21 +0300
Change ID: yxpkrsswyxutrownwzlxqwrtnpvkulml
Parent: be1cdfd
M internal/git/compare.go
路路路
        82
        82
         	if err != nil {

      
        83
        83
         		return plumbing.ZeroHash, err

      
        84
        84
         	}

      
        85
        
        -	return *hash, nil

      
        
        85
        +	return g.peelToCommit(*hash), nil

      
        86
        86
         }

      
        87
        87
         

      
        88
        88
         func parseAheadBehind(counts []byte) (behind, ahead int, err error) {

      
M internal/git/compare_test.go
路路路
        2
        2
         

      
        3
        3
         import (

      
        4
        4
         	"testing"

      
        
        5
        +	"time"

      
        5
        6
         

      
        6
        7
         	"olexsmir.xyz/x/is"

      
        7
        8
         )

      路路路
        47
        48
         

      
        48
        49
         		_, err := r.open().Compare("master", "does-not-exist")

      
        49
        50
         		is.Err(t, err, "resolving head ref")

      
        
        51
        +	})

      
        
        52
        +

      
        
        53
        +	t.Run("compares branch with lightweight tag", func(t *testing.T) {

      
        
        54
        +		r := newTestRepo(t)

      
        
        55
        +		base := r.commitFile("README.md", "base\n", "base commit")

      
        
        56
        +		r.createTag("v1.0", base)

      
        
        57
        +		r.createBranch("develop", base)

      
        
        58
        +		r.checkoutBranch("develop", false)

      
        
        59
        +		r.commitFile("develop.txt", "develop only\n", "develop change")

      
        
        60
        +

      
        
        61
        +		cmp, err := r.open().Compare("v1.0", "develop")

      
        
        62
        +		is.Err(t, err, nil)

      
        
        63
        +		is.Equal(t, cmp.BaseRef, "v1.0")

      
        
        64
        +		is.Equal(t, cmp.HeadRef, "develop")

      
        
        65
        +		is.Equal(t, cmp.Behind, 0)

      
        
        66
        +		is.Equal(t, cmp.Ahead, 1)

      
        
        67
        +		is.Equal(t, cmp.Diff.Stat.FilesChanged, 1)

      
        
        68
        +	})

      
        
        69
        +

      
        
        70
        +	t.Run("compares branch with annotated tag", func(t *testing.T) {

      
        
        71
        +		r := newTestRepo(t)

      
        
        72
        +		base := r.commitFile("README.md", "base\n", "base commit")

      
        
        73
        +		r.createAnnotatedTag("v1.0", "v1 release", base, time.Now())

      
        
        74
        +		r.createBranch("develop", base)

      
        
        75
        +		r.checkoutBranch("develop", false)

      
        
        76
        +		r.commitFile("develop.txt", "develop only\n", "develop change")

      
        
        77
        +

      
        
        78
        +		cmp, err := r.open().Compare("v1.0", "develop")

      
        
        79
        +		is.Err(t, err, nil)

      
        
        80
        +		is.Equal(t, cmp.BaseRef, "v1.0")

      
        
        81
        +		is.Equal(t, cmp.HeadRef, "develop")

      
        
        82
        +		is.Equal(t, cmp.Behind, 0)

      
        
        83
        +		is.Equal(t, cmp.Ahead, 1)

      
        
        84
        +	})

      
        
        85
        +

      
        
        86
        +	t.Run("compares two annotated tags", func(t *testing.T) {

      
        
        87
        +		r := newTestRepo(t)

      
        
        88
        +		base := r.commitFile("README.md", "base\n", "base commit")

      
        
        89
        +		head := r.commitFile("develop.txt", "develop only\n", "develop change")

      
        
        90
        +		r.createAnnotatedTag("v1.0", "v1.0", base, time.Now())

      
        
        91
        +		r.createAnnotatedTag("v2.0", "v2.0", head, time.Now())

      
        
        92
        +

      
        
        93
        +		cmp, err := r.open().Compare("v1.0", "v2.0")

      
        
        94
        +		is.Err(t, err, nil)

      
        
        95
        +		is.Equal(t, cmp.Ahead, 1)

      
        50
        96
         	})

      
        51
        97
         }

      
M internal/git/repo.go
路路路
        59
        59
         		if err != nil {

      
        60
        60
         			return nil, fmt.Errorf("resolving rev %s for %s: %w", ref, path, err)

      
        61
        61
         		}

      
        62
        
        -		g.h = *hash

      
        
        62
        +		g.h = g.peelToCommit(*hash)

      
        63
        63
         	}

      
        64
        64
         	return &g, nil

      
        65
        65
         }

      路路路
        313
        313
         

      
        314
        314
         	return isUpdated, nil

      
        315
        315
         }

      
        
        316
        +

      
        
        317
        +func (g *Repo) peelToCommit(h plumbing.Hash) plumbing.Hash {

      
        
        318
        +	obj, err := g.r.Object(plumbing.AnyObject, h)

      
        
        319
        +	if err != nil {

      
        
        320
        +		return h

      
        
        321
        +	}

      
        
        322
        +	if obj.Type() == plumbing.TagObject {

      
        
        323
        +		tag, ok := obj.(*object.Tag)

      
        
        324
        +		if !ok {

      
        
        325
        +			return h

      
        
        326
        +		}

      
        
        327
        +		commit, err := tag.Commit()

      
        
        328
        +		if err != nil {

      
        
        329
        +			return h

      
        
        330
        +		}

      
        
        331
        +		return commit.Hash

      
        
        332
        +	}

      
        
        333
        +	return h

      
        
        334
        +}