Libgit2sharp: Getting a compatible git branch name from a given string

Created on 11 Nov 2012  路  8Comments  路  Source: libgit2/libgit2sharp

For a development in the project git-tfs, I need to create automaticaly branches for each Tfs branches.

I want to create a git branch from the name of the tfs one.

That's why I need from a string (the tfs branch name) to determin the compatible git branch the name the nearer possible like what's done with the command :
git check-ref-format--branch myExpectedStringName

Is it possible to do it?

Most helpful comment

Long dead thread, but I came across this when searching on Google. I have created an npm module to support converting a string to a valid git ref name. For those that might come across this post in the future, perhaps this is what you are looking for: https://github.com/TheSavior/clean-git-ref

All 8 comments

That's why I need from a string (the tfs branch name) to determine the compatible git branch the name the nearer possible like what's done with the command:

I'm not completely sure to understand what you're after with "the nearer possible"? Unless I'm wrong git check-ref-format doesn't clean a badly formatted reference name. It only tells if it is valid or not. There's a --normalize option, but it only copes with forward slashes.

For instance considering the rules defined in the doc, pmiossec@{0} is an invalid branch name, and there's no way I know of to make git check-ref-format _not_ cringe about it. If there's an option to "transform" the name into a valid one, I do not know it either.

$ git check-ref-format --branch pmiossec@{0}
fatal: 'pmiossec@{0}' is not a valid branch name

_Note:_ I may have completely misunderstood your request. If this is the case, please do not hesitate and correct me!


In libgit2, a function git_reference_is_valid_name() exposes the same functionality as

$ git check-ref-format --normalize --allow-onelevel refs/heads/pmiossec-branch

This could be easily wrapped in the Refs namespace, with something similar to:

using (var repo = new Repository("path/to/repo")
{
   bool isValid = repo.Refs.IsNameValid("refs/heads/pmiossec-branch");
}

What's your opinion about that?

_There's a --normalize option, but it only copes with forward slashes._
I didn't know that was the only thing it is doing. I thought it was removing other characters...

I agree with your proposition. It's what I wanted.

But do you know all the characters forbidden?

relevant GitTfs issue: git-tfs/git-tfs#232

But do you know all the characters forbidden?

@pmiossec As stated in the the doc

git imposes the following rules on how references are named:

 - They can include slash / for hierarchical (directory) grouping,
but no slash-separated component can begin with a dot . or end with the sequence .lock

 - They must contain at least one /. This enforces the presence of a category like heads/,
tags/ etc. but the actual names are not restricted. If the --allow-onelevel option is used,
this rule is waived

 - They cannot have two consecutive dots .. anywhere

 - They cannot have ASCII control characters (i.e. bytes whose values are lower
than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere

 - They cannot have question-mark ?, asterisk *, or open bracket [ anywhere. See
the --refspec-pattern option below for an exception to this rule

 - They cannot begin or end with a slash / or contain multiple consecutive slashes
(see the --normalize option below for an exception to this rule)

 - They cannot end with a dot .

 - They cannot contain a sequence @{

 - They cannot contain a \

@pmiossec FIxed in vNext

/cc @sc68cal

You are awesome.

Yep awesome. Very prompt to give a feedback! :+1:

Cheers!

Long dead thread, but I came across this when searching on Google. I have created an npm module to support converting a string to a valid git ref name. For those that might come across this post in the future, perhaps this is what you are looking for: https://github.com/TheSavior/clean-git-ref

Was this page helpful?
0 / 5 - 0 ratings