Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe signature #445

Merged
merged 3 commits into from
Mar 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions ObjectiveGit/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -718,24 +718,33 @@ - (GTSubmodule *)submoduleWithName:(NSString *)name error:(NSError **)error {

#pragma mark User

+ (NSString *)defaultUserName {
NSString *name = NSFullUserName();
if (name.length == 0) name = NSUserName();
if (name.length == 0) name = @"nobody";
return name;
}

+ (NSString *)defaultEmail {
NSString *username = NSUserName();
if (username.length == 0) username = @"nobody";
NSString *domain = NSProcessInfo.processInfo.hostName ?: @"nowhere.local";
return [NSString stringWithFormat:@"%@@%@", username, domain];
}

- (GTSignature *)userSignatureForNow {
GTConfiguration *configuration = [self configurationWithError:NULL];
NSString *name = [configuration stringForKey:@"user.name"];
if (name.length == 0) {
name = NSFullUserName();
if (name.length == 0) name = NSUserName();
if (name.length == 0) name = @"nobody";
}
if (name.length == 0) name = self.class.defaultUserName;

NSString *email = [configuration stringForKey:@"user.email"];
if (email == nil) {
NSString *username = NSUserName();
if (username.length == 0) username = @"nobody";
NSString *domain = NSProcessInfo.processInfo.hostName ?: @"nowhere.local";
email = [NSString stringWithFormat:@"%@@%@", username, domain];
}
if (email.length == 0) email = self.class.defaultEmail;

NSDate *now = [NSDate date];
GTSignature *signature = [[GTSignature alloc] initWithName:name email:email time:now];
if (signature != nil) return signature;

return [[GTSignature alloc] initWithName:name email:email time:[NSDate date]];
return [[GTSignature alloc] initWithName:self.class.defaultUserName email:self.class.defaultEmail time:now];
}

#pragma mark Tagging
Expand Down
50 changes: 50 additions & 0 deletions ObjectiveGitTests/GTRepositorySpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,56 @@
});
});

describe(@"-userSignatureForNow", ^{
static NSString * const userName = @"johnsmith";
static NSString * const email = @"[email protected]";

__block GTConfiguration *configuration;

beforeEach(^{
configuration = [repository configurationWithError:NULL];
expect(configuration).notTo(beNil());
});

it(@"should use the values from the config", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:email forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(signature.name).to(equal(userName));
expect(signature.email).to(equal(email));
});

describe(@"invalid values", ^{
it(@"should use a default value if the name is empty", ^{
[configuration setString:@"" forKey:@"user.name"];
[configuration setString:email forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});

it(@"should use a default value if the email is empty", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:@"" forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});

it(@"should use a default value if the email contains angled brackets", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:@"<[email protected]>" forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});
});
});

afterEach(^{
[self tearDown];
});
Expand Down