Skip to content

Commit

Permalink
Enable GetCommit to work when branch name, returns the last commit on
Browse files Browse the repository at this point in the history
branch
  • Loading branch information
nopcoder committed Nov 3, 2020
1 parent a49707e commit 9ebd4d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions catalog/cataloger_get_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"context"
"fmt"

"github.com/treeverse/lakefs/db"
)
Expand All @@ -22,6 +23,17 @@ func (c *cataloger) GetCommit(ctx context.Context, repository, reference string)
if err != nil {
return nil, err
}

// for branch (committed or uncommitted) we reference last commit
if ref.CommitID == UncommittedID || ref.CommitID == CommittedID {
lastCommitID, err := getLastCommitIDByBranchID(tx, branchID)
if err != nil {
return nil, fmt.Errorf("get last commit id: %w", err)
}
ref.CommitID = lastCommitID
}

// read commit log information
query := `SELECT b.name as branch_name,c.commit_id,c.previous_commit_id,c.committer,c.message,c.creation_date,c.metadata,
COALESCE(bb.name,'') as merge_source_branch_name,COALESCE(c.merge_source_commit,0) as merge_source_commit
FROM catalog_commits c JOIN catalog_branches b ON b.id = c.branch_id
Expand Down
26 changes: 23 additions & 3 deletions catalog/cataloger_get_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,30 @@ func TestCataloger_GetCommit(t *testing.T) {
wantErr: true,
},
{
name: "branch",
name: "branch uncommitted",
reference: "master",
want: nil,
wantErr: true,
want: &CommitLog{
Reference: "~KJ8Wd1Rs96b",
Committer: "tester1",
Message: "Commit1",
CreationDate: time.Now(),
Metadata: Metadata{"k1": "v1"},
Parents: []string{"~KJ8Wd1Rs96a"},
},
wantErr: false,
},
{
name: "branch committed",
reference: "master:HEAD",
want: &CommitLog{
Reference: "~KJ8Wd1Rs96b",
Committer: "tester1",
Message: "Commit1",
CreationDate: time.Now(),
Metadata: Metadata{"k1": "v1"},
Parents: []string{"~KJ8Wd1Rs96a"},
},
wantErr: false,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 9ebd4d7

Please sign in to comment.