Skip to content

Commit 19f4d94

Browse files
committed
Add a Complete SHA service
This adds a service bound to ⌘⇧7 that expands any ref to its abbreviated SHA and its subject line, much like ⌘C in the commit list does.
1 parent 593606f commit 19f4d94

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

ApplicationController.m

+19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "PBGitWindowController.h"
1212
#import "PBRepositoryDocumentController.h"
1313
#import "PBCLIProxy.h"
14+
#import "PBServicesController.h"
1415

1516
@implementation ApplicationController
1617
@synthesize cliProxy;
@@ -31,8 +32,26 @@ - (ApplicationController*)init
3132
return self;
3233
}
3334

35+
- (void)registerServices
36+
{
37+
// Register the service class
38+
PBServicesController *services = [[PBServicesController alloc] init];
39+
[NSApp setServicesProvider:services];
40+
41+
// Force update the services menu if we have a new services version
42+
int serviceVersion = [[NSUserDefaults standardUserDefaults] integerForKey:@"Services Version"];
43+
if (serviceVersion < 2)
44+
{
45+
NSLog(@"Updating services menu…");
46+
NSUpdateDynamicServices();
47+
[[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"Services Version"];
48+
}
49+
}
50+
3451
- (void)applicationDidFinishLaunching:(NSNotification*)notification
3552
{
53+
[self registerServices];
54+
3655
// Only try to open a default document if there are no documents open already.
3756
// For example, the application might have been launched by double-clicking a .git repository,
3857
// or by dragging a folder to the app icon

GitX.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
F5E92A1B0E88550E00056E75 /* empty_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F5E92A1A0E88550E00056E75 /* empty_file.png */; };
6666
F5E92A230E88569500056E75 /* new_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F5E92A220E88569500056E75 /* new_file.png */; };
6767
F5EF8C8E0E9D4A5D0050906B /* PBWebController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */; };
68+
F5FE6C030EB13BC900F30D12 /* PBServicesController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FE6C020EB13BC900F30D12 /* PBServicesController.m */; };
6869
F5FF4E180E0829C20006317A /* PBGitRevList.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E170E0829C20006317A /* PBGitRevList.m */; };
6970
F5FF4E7A0E082E440006317A /* PBGitGrapher.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E790E082E440006317A /* PBGitGrapher.m */; };
7071
/* End PBXBuildFile section */
@@ -191,6 +192,8 @@
191192
F5E92A220E88569500056E75 /* new_file.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = new_file.png; path = Images/new_file.png; sourceTree = "<group>"; };
192193
F5EF8C8C0E9D4A5D0050906B /* PBWebController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBWebController.h; sourceTree = "<group>"; };
193194
F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBWebController.m; sourceTree = "<group>"; };
195+
F5FE6C010EB13BC900F30D12 /* PBServicesController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBServicesController.h; sourceTree = "<group>"; };
196+
F5FE6C020EB13BC900F30D12 /* PBServicesController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBServicesController.m; sourceTree = "<group>"; };
194197
F5FF4E160E0829C20006317A /* PBGitRevList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRevList.h; sourceTree = "<group>"; };
195198
F5FF4E170E0829C20006317A /* PBGitRevList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRevList.m; sourceTree = "<group>"; };
196199
F5FF4E780E082E440006317A /* PBGitGrapher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitGrapher.h; sourceTree = "<group>"; };
@@ -365,6 +368,8 @@
365368
F5EF8C8C0E9D4A5D0050906B /* PBWebController.h */,
366369
F5E926050E8827D300056E75 /* PBViewController.m */,
367370
F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */,
371+
F5FE6C010EB13BC900F30D12 /* PBServicesController.h */,
372+
F5FE6C020EB13BC900F30D12 /* PBServicesController.m */,
368373
);
369374
name = Controllers;
370375
sourceTree = "<group>";
@@ -623,6 +628,7 @@
623628
93CB42C20EAB7B2200530609 /* PBGitDefaults.m in Sources */,
624629
F5E424150EA3E4E10046E362 /* PBDiffWindowController.m in Sources */,
625630
F5E424180EA3E4EB0046E362 /* PBWebDiffController.m in Sources */,
631+
F5FE6C030EB13BC900F30D12 /* PBServicesController.m in Sources */,
626632
);
627633
runOnlyForDeploymentPostprocessing = 0;
628634
};

Info.plist

+27
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,32 @@
4848
<string>NSApplication</string>
4949
<key>SUFeedURL</key>
5050
<string>http://github.com/pieter/gitx/tree/master/feed.xml?raw=true</string>
51+
<key>NSServices</key>
52+
<array>
53+
<dict>
54+
<key>NSKeyEquivalent</key>
55+
<dict>
56+
<key>default</key>
57+
<string>&amp;</string>
58+
</dict>
59+
<key>NSPortName</key>
60+
<string>GitX</string>
61+
<key>NSReturnTypes</key>
62+
<array>
63+
<string>NSStringPboardType</string>
64+
</array>
65+
<key>NSSendTypes</key>
66+
<array>
67+
<string>NSStringPboardType</string>
68+
</array>
69+
<key>NSMessage</key>
70+
<string>completeSha</string>
71+
<key>NSMenuItem</key>
72+
<dict>
73+
<key>default</key>
74+
<string>Complete SHA1</string>
75+
</dict>
76+
</dict>
77+
</array>
5178
</dict>
5279
</plist>

PBServicesController.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// PBServicesController.h
3+
// GitX
4+
//
5+
// Created by Pieter de Bie on 10/24/08.
6+
// Copyright 2008 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
12+
@interface PBServicesController : NSObject {
13+
14+
}
15+
16+
- (NSString *)completeSHA1For:(NSString *)sha;
17+
18+
- (void)completeSha:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error;
19+
@end

PBServicesController.m

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// PBServicesController.m
3+
// GitX
4+
//
5+
// Created by Pieter de Bie on 10/24/08.
6+
// Copyright 2008 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "PBServicesController.h"
10+
#import "PBRepositoryDocumentController.h"
11+
#import "PBGitRepository.h"
12+
13+
@implementation PBServicesController
14+
15+
- (NSString *)completeSHA1For:(NSString *)sha
16+
{
17+
NSArray *documents = [[NSApplication sharedApplication] orderedDocuments];
18+
for (PBGitRepository *repo in documents)
19+
{
20+
int ret = 1;
21+
NSString *s = [repo outputForArguments:[NSArray arrayWithObjects:@"log", @"-1", @"--pretty=format:%h (%s)", sha, nil] retValue:&ret];
22+
if (!ret)
23+
return s;
24+
}
25+
return @"Could not find SHA";
26+
}
27+
28+
-(NSString *)runNameRevFor:(NSString *)s
29+
{
30+
NSArray *repositories = [[NSApplication sharedApplication] orderedDocuments];
31+
if ([repositories count] == 0)
32+
return s;
33+
PBGitRepository *repo = [repositories objectAtIndex:0];
34+
int ret = 1;
35+
NSString *returnString = [repo outputForArguments:[NSArray arrayWithObjects:@"name-rev", @"--stdin", nil] inputString:s retValue:&ret];
36+
if (ret)
37+
return s;
38+
return returnString;
39+
}
40+
41+
-(void)completeSha:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error
42+
{
43+
NSArray *types = [pboard types];
44+
if (![types containsObject:NSStringPboardType])
45+
{
46+
*error = @"Could not get data";
47+
return;
48+
}
49+
50+
NSString *s = [pboard stringForType:NSStringPboardType];
51+
if ([s rangeOfString:@" "].location == NSNotFound)
52+
s = [self completeSHA1For:s];
53+
else
54+
s = [self runNameRevFor:s];
55+
56+
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
57+
[pboard setString:s forType:NSStringPboardType];
58+
}
59+
@end

0 commit comments

Comments
 (0)