Skip to content

Instantly share code, notes, and snippets.

@EvanDotPro
Created May 2, 2010 18:52
Show Gist options
  • Save EvanDotPro/387349 to your computer and use it in GitHub Desktop.
Save EvanDotPro/387349 to your computer and use it in GitHub Desktop.
find . -name '*.c' -type f -exec sed -i 's/\.git\([\"\/]\)/\.get\1/g' {} \;
find . -name '*.h' -type f -exec sed -i 's/\.git\([\"\/]\)/\.get\1/g' {} \;
find . -name '*.c' -type f -exec sed -i 's/\.gitignore/\.getignore/g' {} \;
find . -name '*.c' -type f -exec sed -i "s/!= 'i')/!= 'e')/g" {} \;
#!/usr/bin/env python
import os, sys, subprocess, pyinotify
from threading import Timer
class HandleEvents(pyinotify.ProcessEvent):
timers = {}
def process_IN_CREATE(self, event):
self.startTimer(event.pathname)
def process_IN_DELETE(self, event):
self.startTimer(event.pathname)
def process_IN_CLOSE_WRITE(self, event):
self.startTimer(event.pathname)
def process_IN_MODIFY(self, event):
self.startTimer(event.pathname)
def process_IN_MOVED_FROM(self, event):
self.startTimer(event.pathname)
def process_IN_MOVED_TO(self, event):
self.startTimer(event.pathname)
def startTimer(self, path):
print "Change detected, not pulling"
if path.find('gitsyncemptydir') >= 0:
print "Change was just an empty folder, resuming"
return 0;
if 'pull' in self.timers:
self.timers['pull'].cancel()
if 'sync' in self.timers:
self.timers['sync'].cancel()
self.timers['sync'] = Timer(10, self.doSync, ())
self.timers['sync'].start()
def doSync(self):
print "DO SYNC NOW!"
os.popen("find . -type d -regex ``./[^.].*'' -empty -print0 | xargs -0 -I xxx touch xxx/gitsyncemptydir").read()
os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. add '*gitsyncemptydir' -f").read()
os.popen("find . -name '.git' -print0 | xargs -0 -I xxx /opt/git/bin/git --git-dir=._git --work-tree=. add xxx").read()
os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. add -A").read()
output = os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. status --short").read().strip()
os.popen("find . -name 'gitsyncemptydir' -print0 | xargs -0 rm").read()
if len(output) > 0:
os.system("/opt/git/bin/git --git-dir=._git --work-tree=. commit -m 'dropbox sync'")
# TODO: It would probably be better to just try to push, then check for a non-fast-forward, and pull then
#self.getChanges()
os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. push origin master").read()
self.getChanges()
def getChanges(self):
output = os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. pull origin master").read().strip()
if((output.find('Already up-to-date.') == -1) and (output.find('gitsyncemptydir') >= 0)):
os.popen("/opt/git/bin/git --git-dir=._git --work-tree=. status --short | grep gitsyncemptydir | sed -s 's/^\(.\{3\}\)//' | xargs -I xxx /opt/git/bin/git --git-dir=._git --work-tree=. checkout -- 'xxx'").read()
# This can cause an issue where you create a new dir at _just_ the right moment and it gets deleted.
os.popen("find . -depth -type d -empty -print0 | xargs -0 rmdir").read()
os.popen("find . -name 'gitsyncemptydir' -print0 | xargs -0 rm").read()
if 'pull' in self.timers:
self.timers['pull'].cancel()
self.timers['pull'] = Timer(10, self.getChanges, ())
self.timers['pull'].start()
pyinotify.log.setLevel(50)
path = sys.argv[1]
os.chdir(path)
p = HandleEvents()
p.getChanges()
#p.doPull()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, p)
excl_list = [path + '/._git*']
excl = pyinotify.ExcludeFilter(excl_list)
wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True, exclude_filter=excl)
notifier.loop()
#!/usr/bin/env python
import os, sys, subprocess, pyinotify
from threading import Timer
class HandleEvents(pyinotify.ProcessEvent):
timers = {}
processing = 0
sync_again = 0
def process_IN_CREATE(self, event):
self.changeDetected(event.pathname)
def process_IN_DELETE(self, event):
self.changeDetected(event.pathname)
def process_IN_CLOSE_WRITE(self, event):
self.changeDetected(event.pathname)
def process_IN_MODIFY(self, event):
self.changeDetected(event.pathname)
def process_IN_MOVED_FROM(self, event):
self.changeDetected(event.pathname)
def process_IN_MOVED_TO(self, event):
self.changeDetected(event.pathname)
def changeDetected(self, path):
if path.find('.getempty') >= 0:
#print "Empty dir, ignore: "+path
return 0
if self.processing == 1:
#print "Inoring change, sync in progress"
self.sync_again = 1
return 0
if 'sync' in self.timers:
self.timers['sync'].cancel()
#print "Change detected, not pulling: "+path
self.timers['sync'] = Timer(10, self.doSync, ())
self.timers['sync'].start()
def doSync(self):
print "DOING SYNC NOW!"
self.processing = 1
self.touchEmpty()
output = os.popen("/opt/get/bin/git status --short").read().strip()
if len(output) > 0:
os.system("/opt/get/bin/git add .")
os.popen("/opt/get/bin/git commit -a -m 'dropbox sync'").read()
self.rmGetEmpty()
os.system("/opt/get/bin/git push origin master")
print "CHANGES COMMITTED:"
print output
else:
self.rmGetEmpty()
print "CLEAN DROPBOX -- NOTHING TO COMMIT"
self.syncComplete()
def syncComplete(self):
if self.sync_again == 1:
self.sync_again = 0
self.doSync()
else:
self.sync_again = 0
self.processing = 0
def touchEmpty(self):
os.system("find . -type d -empty -not -wholename ./.get\* -print0 | xargs -0 -I xxx touch xxx/.getempty")
def rmGetEmpty(self):
os.system("find . -name '.getempty' -print0 | xargs -0 rm 2>/dev/null")
def getChanges(self):
if self.processing == 0:
self.processing = 1
output = os.popen("/opt/get/bin/git pull --log origin master").read().strip()
print output
mylist = output.split("\n")
for x in mylist:
rmdir = ''
x = x.replace('{','')
x = x.replace('}','')
if((x.find('delete mode') >= 0) and (x.find('.getempty') >= 0)):
x = x.split(' ')
x.pop(0)
x.pop(0)
x.pop(0)
x.pop(0)
rmdir = ' '.join(x)
elif((x.find(' rename ') >= 0) and (x.find('.getempty') >= 0)):
x = x.split(' => ')
y = x.pop(1)
z = x.pop(0)
if(z.find('.getempty') >= 0 or y.find('.getempty')):
x = z.split(' ')
x.pop(0)
x.pop(0)
rmdir = ' '.join(x)
if((y.find('.getempty') >=0) and (z.find('.getempty') == -1)):
y = ''.join(y)
y = y.split(' (100%)')
y = y.pop(0)
rmdir = rmdir + y
if rmdir.find('.getempty') >= 0:
rmdir = rmdir.replace('.getempty','')
rmdir = rmdir.replace('//','/')
os.system("rmdir '" + rmdir + "'")
print "**** rmdir: " + rmdir
self.syncComplete()
if 'pull' in self.timers:
self.timers['pull'].cancel()
self.timers['pull'] = Timer(10, self.getChanges, ())
self.timers['pull'].start()
pyinotify.log.setLevel(50)
path = sys.argv[1]
os.chdir(path)
p = HandleEvents()
p.getChanges()
p.doSync()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, p)
excl_list = [path + '/.get*']
excl = pyinotify.ExcludeFilter(excl_list)
wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True, exclude_filter=excl)
notifier.loop()
@EvanDotPro
Copy link
Author

Here's how I compile git on my Fedora box, as not to interfere with my binary version managed by yum:
sudo yum install python-devel curl-devel expat-devel perl-ExtUtils-MakeMaker zlib-devel openssl-devel gcc make
git clone git://git.kernel.org/pub/scm/git/git.git
cd git/
{download patch}
git apply {path/to/patch}
make prefix=/opt/git
sudo make install prefix=/opt/git

@EvanDotPro
Copy link
Author

New idea: replace all .git stuff with .get :)

find . -name '*.c' | xargs grep -s \\.git[\"\/]
find . -name '*.h' | xargs grep -s \\.git[\"\/]
find . -name '*.c' | xargs grep -s \\.gitignore
find . -name '*.c' | xargs grep -s \!\=\ \'i\'\)

find . -name '*.c' -type f -exec sed -i 's/\.git\([\"\/]\)/\.get\1/g' {} \;
find . -name '*.h' -type f -exec sed -i 's/\.git\([\"\/]\)/\.get\1/g' {} \;
find . -name '*.c' -type f -exec sed -i 's/\.gitignore/\.getignore/g' {} \;
find . -name '*.c' -type f -exec sed -i "s/!= 'i')/!= 'e')/g" {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment