Skip to content

Commit

Permalink
FIXME: The test that comes with this fails, I can't get the created t…
Browse files Browse the repository at this point in the history
…ags to have the specified author/email

Support reading GIT_AUTHOR_{NAME,EMAIL} env variables

Change the discovery of the user.name and user.email variables to
first look up the user/name in the GIT_AUTHOR_NAME and
GIT_AUTHOR_EMAIL environment variables. This mirrors the behavior of
Git itself, see git-commit-tree(1) for details.

The reason to do this is that managing your Git identity via these
variables can be more conveniente than setting them in a config file,
it makes it easier to set them differently on different hosts.

Also change the error message we display if these aren't set, we'll
now error out with something like this when we can't get an author
name or an E-Mail;

    $ perl -Ilib bin/git-deploy start
    # FATAL: Please make sure you set your Git user name & E-Mail
    # FATAL: before using this tool, i.e. either run:
    # FATAL:
    # FATAL:     git config --global user.name 'Your Name'
    # FATAL:     git config --global user.email 'email@host.com'
    # FATAL:
    # FATAL: Or make sure the GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL
    # FATAL: environment variables are appropriately set.

This patch is inspired by Rob's
#20, I thought it made
more sense to introduce a function for looking up the user name &
E-Mail rather than hooking into the low-level config mechanism. This
also adjusts our documentation to indicate to users that they can use
this instead of setting user.{name,email}.
  • Loading branch information
avar committed Sep 1, 2012
1 parent ac97025 commit 61e0054
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
18 changes: 13 additions & 5 deletions bin/git-deploy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ sub send_email_with_sendmail {


my $handle; my $handle;
my $sendmail= get_config("mail-tool","/usr/sbin/sendmail -f"); my $sendmail= get_config("mail-tool","/usr/sbin/sendmail -f");
my $from_name= get_config("user.name"); my $from_name= get_user_name();
my $from_email= get_config("user.email"); my $from_email= get_user_email();


my $cmd= "$sendmail '$from_email' '$to_mail'"; my $cmd= "$sendmail '$from_email' '$to_mail'";
if ( $to_mail eq 'debug' ) { if ( $to_mail eq 'debug' ) {
Expand Down Expand Up @@ -641,12 +641,14 @@ if ( $list || $show_tag ) {
exit(0); exit(0);
} }


if (!$force and (!get_config("user.name","") or !get_config("user.email",""))) { if (!$force and (!get_user_name() or !get_user_email())) {
_die "Please make sure you execute:\n\n" _die "Please make sure you set your Git user name & E-Mail\n"
. "before using this tool, i.e. either run:\n\n"
. " git config --global user.name 'Your Name'\n" . " git config --global user.name 'Your Name'\n"
. " git config --global user.email 'email\@host.com'\n" . " git config --global user.email 'email\@host.com'\n"
. "\n" . "\n"
. "before you use this tool"; . "Or make sure the GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL\n"
. "environment variables are appropriately set.";
} }


if ($check_clean) { if ($check_clean) {
Expand Down Expand Up @@ -1637,8 +1639,14 @@ L<git-config(1)> for details:
=item * user.name =item * user.name
You can also make sure C<GIT_AUTHOR_NAME> is set. See
L<git-commit-tree(1)> for details.
=item * user.email =item * user.email
You can also make sure C<GIT_AUTHOR_EMAIL> is set. See
L<git-commit-tree(1)> for details.
=back =back
And these are L<git-deploy(1)>-specific options: And these are L<git-deploy(1)>-specific options:
Expand Down
5 changes: 5 additions & 0 deletions lib/Git/Deploy.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ our @EXPORT= qw(
get_config_int get_config_int
get_config_path get_config_path
get_config_bool get_config_bool
get_user_name
get_user_email
get_current_branch get_current_branch
get_deploy_file_name get_deploy_file_name
get_ref_info get_ref_info
Expand Down Expand Up @@ -251,6 +253,9 @@ sub get_config_path { return _get_config("--path",@_) }
sub get_config_int { return _get_config("--int",@_) } sub get_config_int { return _get_config("--int",@_) }
sub get_config_bool { return 'true' eq _get_config("--bool",@_) } sub get_config_bool { return 'true' eq _get_config("--bool",@_) }


sub get_user_name { return $ENV{GIT_AUTHOR_NAME} || get_config("user.name", "") }
sub get_user_email { return $ENV{GIT_AUTHOR_EMAIL} || get_config("user.email", "") }

} }




Expand Down
35 changes: 31 additions & 4 deletions t/run.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,10 +9,37 @@ git_deploy_test(
"A rollout etc.", "A rollout etc.",
sub { sub {
my $ctx = shift; my $ctx = shift;
_run_git_deploy(
$ctx, # First test that the user.{email,name} warnings work properly
args => "start", {
); local $ENV{GIT_AUTHOR_NAME} = '';
local $ENV{GIT_AUTHOR_EMAIL} = '';
_system qq[git config user.$_ ""] for qw(name email);
_run_git_deploy(
$ctx,
args => "start",
wanted_exit_code => -1, # -1 = any non-zero OK
);
like(
`cat $ctx->{last_stderr}`,
qr/Please make sure you set your Git user name & E-Mail/,
"We should whine about an unset user name & E-Mail"
);

# We should support
local $ENV{GIT_AUTHOR_NAME} = 'Git Deploy AUTHOR_NAME Test';
local $ENV{GIT_AUTHOR_EMAIL} = 'git-deploy_AUTHOR_EMAIL-test@example.com';
_run_git_deploy(
$ctx,
args => "start",
);
chomp(my $tag_created_on_start_due_to_no_tag_existing = qx[git tag -l]);
chomp(my $tag_info = qx[git show $tag_created_on_start_due_to_no_tag_existing]);
like($tag_info, qr/\Q$ENV{GIT_AUTHOR_NAME}\E/, "We should have created a tag with $ENV{GIT_AUTHOR_NAME} as the author");
like($tag_info, qr/\Q$ENV{GIT_AUTHOR_EMAIL}\E/, "We should have created a tag with $ENV{GIT_AUTHOR_EMAIL} as the author");
_system "git config --remove-section user";
}

like(`git tag -l`, qr/debug/, "We created a tag because there wasn't one already"); like(`git tag -l`, qr/debug/, "We created a tag because there wasn't one already");
like(`cat $ctx->{last_stderr}`, qr/Deploy procedure has started/, "We print a notice about the deploy being started"); like(`cat $ctx->{last_stderr}`, qr/Deploy procedure has started/, "We print a notice about the deploy being started");
like(`cat $ctx->{last_stderr}`, qr/Not sending mail on action/, "By default we don't send mail on 'start'"); like(`cat $ctx->{last_stderr}`, qr/Not sending mail on action/, "By default we don't send mail on 'start'");
Expand Down

0 comments on commit 61e0054

Please sign in to comment.