Skip to content
Browse files

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.
  • Loading branch information...
1 parent 593606f commit 19f4d94ec6a12d57f0aa1e1730f3a050d3943b17 @pieter committed Oct 27, 2008
Showing with 130 additions and 0 deletions.
  1. +19 −0 ApplicationController.m
  2. +6 −0 GitX.xcodeproj/project.pbxproj
  3. +27 −0 Info.plist
  4. +19 −0 PBServicesController.h
  5. +59 −0 PBServicesController.m
View
19 ApplicationController.m
@@ -11,6 +11,7 @@
#import "PBGitWindowController.h"
#import "PBRepositoryDocumentController.h"
#import "PBCLIProxy.h"
+#import "PBServicesController.h"
@implementation ApplicationController
@synthesize cliProxy;
@@ -31,8 +32,26 @@ - (ApplicationController*)init
return self;
}
+- (void)registerServices
+{
+ // Register the service class
+ PBServicesController *services = [[PBServicesController alloc] init];
+ [NSApp setServicesProvider:services];
+
+ // Force update the services menu if we have a new services version
+ int serviceVersion = [[NSUserDefaults standardUserDefaults] integerForKey:@"Services Version"];
+ if (serviceVersion < 2)
+ {
+ NSLog(@"Updating services menu…");
+ NSUpdateDynamicServices();
+ [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"Services Version"];
+ }
+}
+
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
+ [self registerServices];
+
// Only try to open a default document if there are no documents open already.
// For example, the application might have been launched by double-clicking a .git repository,
// or by dragging a folder to the app icon
View
6 GitX.xcodeproj/project.pbxproj
@@ -65,6 +65,7 @@
F5E92A1B0E88550E00056E75 /* empty_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F5E92A1A0E88550E00056E75 /* empty_file.png */; };
F5E92A230E88569500056E75 /* new_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F5E92A220E88569500056E75 /* new_file.png */; };
F5EF8C8E0E9D4A5D0050906B /* PBWebController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */; };
+ F5FE6C030EB13BC900F30D12 /* PBServicesController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FE6C020EB13BC900F30D12 /* PBServicesController.m */; };
F5FF4E180E0829C20006317A /* PBGitRevList.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E170E0829C20006317A /* PBGitRevList.m */; };
F5FF4E7A0E082E440006317A /* PBGitGrapher.m in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E790E082E440006317A /* PBGitGrapher.m */; };
/* End PBXBuildFile section */
@@ -191,6 +192,8 @@
F5E92A220E88569500056E75 /* new_file.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = new_file.png; path = Images/new_file.png; sourceTree = "<group>"; };
F5EF8C8C0E9D4A5D0050906B /* PBWebController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBWebController.h; sourceTree = "<group>"; };
F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBWebController.m; sourceTree = "<group>"; };
+ F5FE6C010EB13BC900F30D12 /* PBServicesController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBServicesController.h; sourceTree = "<group>"; };
+ F5FE6C020EB13BC900F30D12 /* PBServicesController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBServicesController.m; sourceTree = "<group>"; };
F5FF4E160E0829C20006317A /* PBGitRevList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRevList.h; sourceTree = "<group>"; };
F5FF4E170E0829C20006317A /* PBGitRevList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRevList.m; sourceTree = "<group>"; };
F5FF4E780E082E440006317A /* PBGitGrapher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitGrapher.h; sourceTree = "<group>"; };
@@ -365,6 +368,8 @@
F5EF8C8C0E9D4A5D0050906B /* PBWebController.h */,
F5E926050E8827D300056E75 /* PBViewController.m */,
F5EF8C8D0E9D4A5D0050906B /* PBWebController.m */,
+ F5FE6C010EB13BC900F30D12 /* PBServicesController.h */,
+ F5FE6C020EB13BC900F30D12 /* PBServicesController.m */,
);
name = Controllers;
sourceTree = "<group>";
@@ -623,6 +628,7 @@
93CB42C20EAB7B2200530609 /* PBGitDefaults.m in Sources */,
F5E424150EA3E4E10046E362 /* PBDiffWindowController.m in Sources */,
F5E424180EA3E4EB0046E362 /* PBWebDiffController.m in Sources */,
+ F5FE6C030EB13BC900F30D12 /* PBServicesController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
27 Info.plist
@@ -48,5 +48,32 @@
<string>NSApplication</string>
<key>SUFeedURL</key>
<string>http://github.com/pieter/gitx/tree/master/feed.xml?raw=true</string>
+ <key>NSServices</key>
+ <array>
+ <dict>
+ <key>NSKeyEquivalent</key>
+ <dict>
+ <key>default</key>
+ <string>&amp;</string>
+ </dict>
+ <key>NSPortName</key>
+ <string>GitX</string>
+ <key>NSReturnTypes</key>
+ <array>
+ <string>NSStringPboardType</string>
+ </array>
+ <key>NSSendTypes</key>
+ <array>
+ <string>NSStringPboardType</string>
+ </array>
+ <key>NSMessage</key>
+ <string>completeSha</string>
+ <key>NSMenuItem</key>
+ <dict>
+ <key>default</key>
+ <string>Complete SHA1</string>
+ </dict>
+ </dict>
+ </array>
</dict>
</plist>
View
19 PBServicesController.h
@@ -0,0 +1,19 @@
+//
+// PBServicesController.h
+// GitX
+//
+// Created by Pieter de Bie on 10/24/08.
+// Copyright 2008 __MyCompanyName__. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface PBServicesController : NSObject {
+
+}
+
+- (NSString *)completeSHA1For:(NSString *)sha;
+
+- (void)completeSha:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error;
+@end
View
59 PBServicesController.m
@@ -0,0 +1,59 @@
+//
+// PBServicesController.m
+// GitX
+//
+// Created by Pieter de Bie on 10/24/08.
+// Copyright 2008 __MyCompanyName__. All rights reserved.
+//
+
+#import "PBServicesController.h"
+#import "PBRepositoryDocumentController.h"
+#import "PBGitRepository.h"
+
+@implementation PBServicesController
+
+- (NSString *)completeSHA1For:(NSString *)sha
+{
+ NSArray *documents = [[NSApplication sharedApplication] orderedDocuments];
+ for (PBGitRepository *repo in documents)
+ {
+ int ret = 1;
+ NSString *s = [repo outputForArguments:[NSArray arrayWithObjects:@"log", @"-1", @"--pretty=format:%h (%s)", sha, nil] retValue:&ret];
+ if (!ret)
+ return s;
+ }
+ return @"Could not find SHA";
+}
+
+-(NSString *)runNameRevFor:(NSString *)s
+{
+ NSArray *repositories = [[NSApplication sharedApplication] orderedDocuments];
+ if ([repositories count] == 0)
+ return s;
+ PBGitRepository *repo = [repositories objectAtIndex:0];
+ int ret = 1;
+ NSString *returnString = [repo outputForArguments:[NSArray arrayWithObjects:@"name-rev", @"--stdin", nil] inputString:s retValue:&ret];
+ if (ret)
+ return s;
+ return returnString;
+}
+
+-(void)completeSha:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error
+{
+ NSArray *types = [pboard types];
+ if (![types containsObject:NSStringPboardType])
+ {
+ *error = @"Could not get data";
+ return;
+ }
+
+ NSString *s = [pboard stringForType:NSStringPboardType];
+ if ([s rangeOfString:@" "].location == NSNotFound)
+ s = [self completeSHA1For:s];
+ else
+ s = [self runNameRevFor:s];
+
+ [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
+ [pboard setString:s forType:NSStringPboardType];
+}
+@end

0 comments on commit 19f4d94

Please sign in to comment.
Something went wrong with that request. Please try again.