Skip to content

Commit

Permalink
Use string_ncopy when copying ref names
Browse files Browse the repository at this point in the history
Change code that updates the internal env/state variables to use
string_ncopy instead of string_copy_rev, since the latter copies atmost
40 characters. Test that both branches and tags with long names can be
succesfully checked out.

Fixes #290
  • Loading branch information
jonas committed May 30, 2014
1 parent e4cff4f commit b4760a9
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/refdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,18 @@ ref_update_env(struct argv_env *env, const struct ref *ref, bool clear)
string_copy_rev(env->commit, ref->id);

if (ref_is_tag(ref)) {
string_copy_rev(env->tag, ref->name);
string_ncopy(env->tag, ref->name, strlen(ref->name));

} else if (ref_is_remote(ref)) {
const char *sep = strchr(ref->name, '/');

if (!sep)
return;
string_ncopy(env->remote, ref->name, sep - ref->name);
string_copy_rev(env->branch, sep + 1);
string_ncopy(env->branch, sep + 1, strlen(sep + 1));

} else if (ref->type == REFERENCE_BRANCH) {
string_copy_rev(env->branch, ref->name);
string_ncopy(env->branch, ref->name, strlen(ref->name));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ refs_select(struct view *view, struct line *line)
}
string_copy_rev(view->ref, reference->ref->id);
string_copy_rev(view->env->head, reference->ref->id);
string_copy_rev(view->env->ref, reference->ref->name);
string_ncopy(view->env->ref, reference->ref->name, strlen(reference->ref->name));
ref_update_env(view->env, reference->ref, TRUE);
}

Expand Down
114 changes: 114 additions & 0 deletions test/refs/branch-checkout-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/sh
#
# Test branch checkout (GitHub issue #290)

. libtest.sh
. libgit.sh

export LINES=9

tigrc <<EOF
# Dump information about the current reference
bind generic <F1> \
@sh -c 'printf "refs=%s\nbranch=%s\ntag=%s\n" \
"%(ref)" "%(branch)" "%(tag)" > $HOME/%(commit).refinfo'
bind generic <F2> @git checkout -q %(branch)
bind generic <F3> @git checkout -q %(tag)
bind generic <F4> @sh -c 'git rev-parse HEAD > $HOME/%(ref).head'
EOF

steps '
:view-refs
:wait
:save-display refs-init.screen
:3
<F1>
<F2>
:wait
:save-display refs-checked-out-branch.screen
<F4>
:6
<F1>
<F3>
:wait
:save-display refs-checked-out-tag.screen
<F4>
'

LONG_BRANCH_NAME_ID=19455fa3642af6a6a7d527dd043caf5a70eaad2d
LONG_BRANCH_NAME=this_is_really_a_long_long_long_long_long_long_long_branch

LONG_TAG_NAME_ID=a3f25ca556c1cef1ef3a0704f5deaec8687c84b9
LONG_TAG_NAME=this_is_really_a_long_long_long_long_long_long_long_tag

create_branches()
{
git branch "$LONG_BRANCH_NAME" HEAD~4
git tag "$LONG_TAG_NAME" HEAD~9
}

git_clone 'repo-one'
in_work_dir create_branches

test_tig

assert_equals "$LONG_BRANCH_NAME_ID.refinfo" <<EOF
refs=$LONG_BRANCH_NAME
branch=$LONG_BRANCH_NAME
tag=
EOF

assert_equals "$LONG_BRANCH_NAME_ID.refinfo" <<EOF
refs=$LONG_BRANCH_NAME
branch=$LONG_BRANCH_NAME
tag=
EOF

assert_equals "$LONG_BRANCH_NAME.head" <<EOF
$LONG_BRANCH_NAME_ID
EOF

assert_equals "$LONG_TAG_NAME_ID.refinfo" <<EOF
refs=$LONG_TAG_NAME
branch=
tag=$LONG_TAG_NAME
EOF

assert_equals "$LONG_TAG_NAME.head" <<EOF
$LONG_TAG_NAME_ID
EOF

assert_equals 'refs-init.screen' <<EOF
All references
2010-04-07 05:37 Max Power master
2010-03-04 04:09 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2010-04-07 05:37 Max Power origin/master
2010-04-07 05:37 Max Power origin/HEAD
2010-01-20 14:18 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2009-12-17 12:49 René Lévesque v1.0
[refs] All references 100%
EOF

assert_equals 'refs-checked-out-branch.screen' <<EOF
All references
2010-04-07 05:37 Max Power master
2010-03-04 04:09 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2010-04-07 05:37 Max Power origin/master
2010-04-07 05:37 Max Power origin/HEAD
2010-01-20 14:18 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2009-12-17 12:49 René Lévesque v1.0
[refs] 19455fa3642af6a6a7d527dd043caf5a70eaad2d - reference 2 of 6 100%
EOF

assert_equals 'refs-checked-out-tag.screen' <<EOF
All references
2010-04-07 05:37 Max Power master
2010-03-04 04:09 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2010-04-07 05:37 Max Power origin/master
2010-04-07 05:37 Max Power origin/HEAD
2010-01-20 14:18 A. U. Thor this_is_really_a_long_long_long_long_long_long_l
2009-12-17 12:49 René Lévesque v1.0
[refs] a3f25ca556c1cef1ef3a0704f5deaec8687c84b9 - reference 5 of 6 100%
EOF

0 comments on commit b4760a9

Please sign in to comment.