* Be more strict with git arguments * fix-up commit test * use bindings for branch nametags/v1.10.0-rc1
if all { | if all { | ||||
cmd.AddArguments("--all") | cmd.AddArguments("--all") | ||||
} | } | ||||
cmd.AddArguments("--") | |||||
_, err := cmd.AddArguments(files...).RunInDir(repoPath) | _, err := cmd.AddArguments(files...).RunInDir(repoPath) | ||||
return err | return err | ||||
} | } | ||||
} | } | ||||
// FileChangedSinceCommit Returns true if the file given has changed since the the past commit | // FileChangedSinceCommit Returns true if the file given has changed since the the past commit | ||||
// YOU MUST ENSURE THAT pastCommit is a valid commit ID. | |||||
func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) { | func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) { | ||||
return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String()) | return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String()) | ||||
} | } |
if opts.All { | if opts.All { | ||||
cmd.AddArguments("--all") | cmd.AddArguments("--all") | ||||
} else { | } else { | ||||
cmd.AddArguments(opts.Remote) | |||||
cmd.AddArguments(opts.Branch) | |||||
cmd.AddArguments("--", opts.Remote, opts.Branch) | |||||
} | } | ||||
if opts.Timeout <= 0 { | if opts.Timeout <= 0 { | ||||
if opts.Force { | if opts.Force { | ||||
cmd.AddArguments("-f") | cmd.AddArguments("-f") | ||||
} | } | ||||
cmd.AddArguments(opts.Remote, opts.Branch) | |||||
cmd.AddArguments("--", opts.Remote, opts.Branch) | |||||
_, err := cmd.RunInDirWithEnv(repoPath, opts.Env) | _, err := cmd.RunInDirWithEnv(repoPath, opts.Env) | ||||
return err | return err | ||||
} | } |
cmd.AddArguments("-d") | cmd.AddArguments("-d") | ||||
} | } | ||||
cmd.AddArguments(name) | |||||
cmd.AddArguments("--", name) | |||||
_, err := cmd.RunInDir(repo.Path) | _, err := cmd.RunInDir(repo.Path) | ||||
return err | return err |
return commit, nil | return commit, nil | ||||
} | } | ||||
// GetCommit returns commit object of by ID string. | |||||
func (repo *Repository) GetCommit(commitID string) (*Commit, error) { | |||||
// ConvertToSHA1 returns a Hash object from a potential ID string | |||||
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) { | |||||
if len(commitID) != 40 { | if len(commitID) != 40 { | ||||
var err error | var err error | ||||
actualCommitID, err := NewCommand("rev-parse", commitID).RunInDir(repo.Path) | |||||
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path) | |||||
if err != nil { | if err != nil { | ||||
if strings.Contains(err.Error(), "unknown revision or path") { | |||||
return nil, ErrNotExist{commitID, ""} | |||||
if strings.Contains(err.Error(), "unknown revision or path") || | |||||
strings.Contains(err.Error(), "fatal: Needed a single revision") { | |||||
return SHA1{}, ErrNotExist{commitID, ""} | |||||
} | } | ||||
return nil, err | |||||
return SHA1{}, err | |||||
} | } | ||||
commitID = actualCommitID | commitID = actualCommitID | ||||
} | } | ||||
id, err := NewIDFromString(commitID) | |||||
return NewIDFromString(commitID) | |||||
} | |||||
// GetCommit returns commit object of by ID string. | |||||
func (repo *Repository) GetCommit(commitID string) (*Commit, error) { | |||||
id, err := repo.ConvertToSHA1(commitID) | |||||
if err != nil { | if err != nil { | ||||
return nil, err | return nil, err | ||||
} | } | ||||
} | } | ||||
// FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2 | // FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2 | ||||
// You must ensure that id1 and id2 are valid commit ids. | |||||
func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) { | func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) { | ||||
stdout, err := NewCommand("diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path) | stdout, err := NewCommand("diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path) | ||||
if err != nil { | if err != nil { |
commit, err := bareRepo1.GetCommit("bad_branch") | commit, err := bareRepo1.GetCommit("bad_branch") | ||||
assert.Nil(t, commit) | assert.Nil(t, commit) | ||||
assert.Error(t, err) | assert.Error(t, err) | ||||
assert.EqualError(t, err, "object does not exist [id: bad_branch, rel_path: ]") | |||||
assert.True(t, IsErrNotExist(err)) | |||||
} | } |
} | } | ||||
} | } | ||||
stdout, err := NewCommand("merge-base", base, head).RunInDir(repo.Path) | |||||
stdout, err := NewCommand("merge-base", "--", base, head).RunInDir(repo.Path) | |||||
return strings.TrimSpace(stdout), base, err | return strings.TrimSpace(stdout), base, err | ||||
} | } | ||||
// ReadTreeToIndex reads a treeish to the index | // ReadTreeToIndex reads a treeish to the index | ||||
func (repo *Repository) ReadTreeToIndex(treeish string) error { | func (repo *Repository) ReadTreeToIndex(treeish string) error { | ||||
if len(treeish) != 40 { | if len(treeish) != 40 { | ||||
res, err := NewCommand("rev-parse", treeish).RunInDir(repo.Path) | |||||
res, err := NewCommand("rev-parse", "--verify", treeish).RunInDir(repo.Path) | |||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
} | } |
// CreateTag create one tag in the repository | // CreateTag create one tag in the repository | ||||
func (repo *Repository) CreateTag(name, revision string) error { | func (repo *Repository) CreateTag(name, revision string) error { | ||||
_, err := NewCommand("tag", name, revision).RunInDir(repo.Path) | |||||
_, err := NewCommand("tag", "--", name, revision).RunInDir(repo.Path) | |||||
return err | return err | ||||
} | } | ||||
// CreateAnnotatedTag create one annotated tag in the repository | // CreateAnnotatedTag create one annotated tag in the repository | ||||
func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error { | func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error { | ||||
_, err := NewCommand("tag", "-a", "-m", message, name, revision).RunInDir(repo.Path) | |||||
_, err := NewCommand("tag", "-a", "-m", message, "--", name, revision).RunInDir(repo.Path) | |||||
return err | return err | ||||
} | } | ||||
// GetTagID returns the object ID for a tag (annotated tags have both an object SHA AND a commit SHA) | // GetTagID returns the object ID for a tag (annotated tags have both an object SHA AND a commit SHA) | ||||
func (repo *Repository) GetTagID(name string) (string, error) { | func (repo *Repository) GetTagID(name string) (string, error) { | ||||
stdout, err := NewCommand("show-ref", name).RunInDir(repo.Path) | |||||
stdout, err := NewCommand("show-ref", "--", name).RunInDir(repo.Path) | |||||
if err != nil { | if err != nil { | ||||
return "", err | return "", err | ||||
} | } |
// GetTree find the tree object in the repository. | // GetTree find the tree object in the repository. | ||||
func (repo *Repository) GetTree(idStr string) (*Tree, error) { | func (repo *Repository) GetTree(idStr string) (*Tree, error) { | ||||
if len(idStr) != 40 { | if len(idStr) != 40 { | ||||
res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path) | |||||
res, err := NewCommand("rev-parse", "--verify", idStr).RunInDir(repo.Path) | |||||
if err != nil { | if err != nil { | ||||
return nil, err | return nil, err | ||||
} | } |
// Assigned LastCommitID in opts if it hasn't been set | // Assigned LastCommitID in opts if it hasn't been set | ||||
if opts.LastCommitID == "" { | if opts.LastCommitID == "" { | ||||
opts.LastCommitID = commit.ID.String() | opts.LastCommitID = commit.ID.String() | ||||
} else { | |||||
lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID) | |||||
if err != nil { | |||||
return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err) | |||||
} | |||||
opts.LastCommitID = lastCommitID.String() | |||||
} | } | ||||
// Get the files in the index | // Get the files in the index |
// Assigned LastCommitID in opts if it hasn't been set | // Assigned LastCommitID in opts if it hasn't been set | ||||
if opts.LastCommitID == "" { | if opts.LastCommitID == "" { | ||||
opts.LastCommitID = commit.ID.String() | opts.LastCommitID = commit.ID.String() | ||||
} else { | |||||
lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID) | |||||
if err != nil { | |||||
return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err) | |||||
} | |||||
opts.LastCommitID = lastCommitID.String() | |||||
} | } | ||||
encoding := "UTF-8" | encoding := "UTF-8" |
// message (optional) for the commit of this file. if not supplied, a default message will be used | // message (optional) for the commit of this file. if not supplied, a default message will be used | ||||
Message string `json:"message"` | Message string `json:"message"` | ||||
// branch (optional) to base this file from. if not given, the default branch is used | // branch (optional) to base this file from. if not given, the default branch is used | ||||
BranchName string `json:"branch"` | |||||
BranchName string `json:"branch" binding:"GitRefName;MaxSize(100)"` | |||||
// new_branch (optional) will make a new branch from `branch` before creating the file | // new_branch (optional) will make a new branch from `branch` before creating the file | ||||
NewBranchName string `json:"new_branch"` | |||||
NewBranchName string `json:"new_branch" binding:"GitRefName;MaxSize(100)"` | |||||
// `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used) | // `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used) | ||||
Author Identity `json:"author"` | Author Identity `json:"author"` | ||||
Committer Identity `json:"committer"` | Committer Identity `json:"committer"` |