summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorAbdo Roig-Maranges <abdo.roig@gmail.com>2013-02-10 12:35:56 +0100
committerAbdo Roig-Maranges <abdo.roig@gmail.com>2013-02-12 12:08:44 +0100
commitbd87e709f27eec2eee27433c9769c8122770b3c4 (patch)
treeca7a219e36c35166f18f673548630b97d6ae2784 /ranger
parent470cf6795559fb8525e6d4e7cbe6c693e0004639 (diff)
downloadranger-bd87e709f27eec2eee27433c9769c8122770b3c4.tar.gz
Added version control system related commands
Adds commands to stage, unstage changes, and to display diffs and logs using the
embedded pager.
Diffstat (limited to 'ranger')
-rw-r--r--ranger/config/commands.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 14e3f918..02ef0351 100644
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1081,3 +1081,102 @@ class grep(Command):
             action.extend(['-e', self.rest(1), '-r'])
             action.extend(f.path for f in self.fm.thistab.get_selection())
             self.fm.execute_command(action, flags='p')
+
+
+# Version control commands
+# --------------------------------
+class stage(Command):
+    """
+    :stage
+
+    Stage selected files for the corresponding version control system
+    """
+    def execute(self):
+        from ranger.vcs import VcsError
+
+        filelist = [f.path for f in self.fm.thistab.get_selection()]
+        self.fm.thisdir.vcs_outdated = True
+#        for f in self.fm.thistab.get_selection():
+#            f.vcs_outdated = True
+
+        try:
+            self.fm.thisdir.vcs.add(filelist)
+        except VcsError:
+            self.fm.notify("Could not stage files.")
+
+        self.fm.reload_cwd()
+
+
+class unstage(Command):
+    """
+    :unstage
+
+    Unstage selected files for the corresponding version control system
+    """
+    def execute(self):
+        from ranger.vcs import VcsError
+
+        filelist = [f.path for f in self.fm.thistab.get_selection()]
+        self.fm.thisdir.vcs_outdated = True
+#        for f in self.fm.thistab.get_selection():
+#            f.vcs_outdated = True
+
+        try:
+            self.fm.thisdir.vcs.reset(filelist)
+        except VcsError:
+            self.fm.notify("Could not unstage files.")
+
+        self.fm.reload_cwd()
+
+
+class diff(Command):
+    """
+    :diff
+
+    Displays a diff of selected files against last last commited version
+    """
+    def execute(self):
+        from ranger.vcs import VcsError
+        import tempfile
+
+        L = self.fm.thistab.get_selection()
+        if len(L) == 0: return
+
+        filelist = [f.path for f in L]
+        vcs = L[0].vcs
+
+        diff = vcs.get_raw_diff(filelist=filelist)
+        if len(diff.strip()) > 0:
+            tmp = tempfile.NamedTemporaryFile()
+            tmp.write(diff.encode('utf-8'))
+            tmp.flush()
+
+            pager = os.environ.get('PAGER', ranger.DEFAULT_PAGER)
+            self.fm.run([pager, tmp.name])
+        else:
+            raise Exception("diff is empty")
+
+
+class log(Command):
+    """
+    :log
+
+    Displays the log of the current repo or files
+    """
+    def execute(self):
+        from ranger.vcs import VcsError
+        import tempfile
+
+        L = self.fm.thistab.get_selection()
+        if len(L) == 0: return
+
+        filelist = [f.path for f in L]
+        vcs = L[0].vcs
+
+        log = vcs.get_raw_log(filelist=filelist)
+        tmp = tempfile.NamedTemporaryFile()
+        tmp.write(log.encode('utf-8'))
+        tmp.flush()
+
+        pager = os.environ.get('PAGER', ranger.DEFAULT_PAGER)
+        self.fm.run([pager, tmp.name])