Browse Source

Refs #52 Create new orphan branch
Refs #52 Push orphan branch to remote

isundil 7 years ago
parent
commit
a944c62979

+ 30 - 3
app/src/main/java/info/knacki/pass/git/BaseGitProtocol.java

@@ -1,5 +1,7 @@
 package info.knacki.pass.git;
 
+import java.util.logging.Logger;
+
 import info.knacki.pass.git.entities.GitCommit;
 import info.knacki.pass.git.entities.GitRef;
 import info.knacki.pass.io.CharsetHelper;
@@ -9,12 +11,13 @@ import info.knacki.pass.settings.SettingsManager;
 
 public abstract class BaseGitProtocol implements GitInterface {
     protected final SettingsManager.Git fConfig;
+    private final static Logger log = Logger.getLogger(BaseGitProtocol.class.getName());
 
     BaseGitProtocol(SettingsManager.Git config) {
         fConfig = config;
     }
 
-    abstract protected void PushPack(GitCommit commit, Pacman pack, OnStreamResponseListener<Void> resp);
+    abstract protected void PushPack(GitCommit commit, Pacman pack, String branch, OnStreamResponseListener<Void> resp);
     abstract protected void FetchCommit(GitRef ref, OnStreamResponseListener<GitCommit> response);
 
     void GetHeadCommitFromRef(GitRef myRef, final OnStreamResponseListener<GitCommit> response) {
@@ -41,7 +44,7 @@ public abstract class BaseGitProtocol implements GitInterface {
         }
     }
 
-    private GitRef GetRefFromBranchName(GitRef[] refs, String branchName) {
+    protected GitRef GetRefFromBranchName(GitRef[] refs, String branchName) {
         if (refs != null)
             for (GitRef ref: refs)
                 if (ref.GetBranch().equals(branchName))
@@ -80,6 +83,30 @@ public abstract class BaseGitProtocol implements GitInterface {
 
     @Override
     public void PushCommitBuilder(GitCommit.Builder commit, OnStreamResponseListener<Void> resp) {
-        PushPack(commit.Build(), new Pacman(commit.PreparePack()), resp);
+        PushPack(commit.Build(), new Pacman(commit.PreparePack()), fConfig.GetBranch(), resp);
+    }
+
+    protected abstract void OnBranchCreated();
+
+    @Override
+    public void CreateBranch(String branchName, OnResponseListener<GitRef[]> callback) {
+        final GitCommit.Builder commit = new GitCommit.Builder(fConfig.GetUsername(), fConfig.GetUserEmail(), "init");
+        PushPack(commit.Build(), new Pacman(commit.PreparePack()), "refs/heads/" +branchName, new OnStreamResponseListener<Void>() {
+            @Override
+            public void OnMsg(String message) {
+                log.info(message);
+            }
+
+            @Override
+            public void OnResponse(Void result) {
+                OnBranchCreated();
+                GetRefs(callback);
+            }
+
+            @Override
+            public void OnError(String msg, Throwable e) {
+                callback.OnError(msg, e);
+            }
+        });
     }
 }

+ 1 - 0
app/src/main/java/info/knacki/pass/git/GitInterface.java

@@ -11,4 +11,5 @@ public interface GitInterface {
     void FetchHead(final OnStreamResponseListener<GitCommit> response);
     void FetchBlob(GitObject.GitBlob blob, OnStreamResponseListener<byte[]> response);
     void PushCommitBuilder(GitCommit.Builder commit, OnStreamResponseListener<Void> resp);
+    void CreateBranch(String branchName, OnResponseListener<GitRef[]> callback);
 }

+ 30 - 21
app/src/main/java/info/knacki/pass/git/HttpGitProtocol.java

@@ -38,20 +38,24 @@ class HttpGitProtocol extends BaseGitProtocol {
             protoGet(url, fConfig, new OnResponseListener<String>() {
                 @Override
                 public void OnResponse(String result) {
-                    String [] refStrings = result.split("\n");
-                    fRefsCache = new GitRef[refStrings.length];
-                    for (int i =0; i < refStrings.length; ++i) {
-                        String refLine = refStrings[i];
-                        for (int j =0; j < refLine.length(); ++j) {
-                            if (refLine.charAt(j) == '\t') {
-                                fRefsCache[i] = new GitRef(refLine.substring(0, j).trim(), refLine.substring(j +1).trim());
-                                break;
+                    if (result.length() == 0) {
+                        fRefsCache = new GitRef[0];
+                    } else {
+                        String[] refStrings = result.split("\n");
+                        fRefsCache = new GitRef[refStrings.length];
+                        for (int i = 0; i < refStrings.length; ++i) {
+                            String refLine = refStrings[i];
+                            for (int j = 0; j < refLine.length(); ++j) {
+                                if (refLine.charAt(j) == '\t') {
+                                    fRefsCache[i] = new GitRef(refLine.substring(0, j).trim(), refLine.substring(j + 1).trim());
+                                    break;
+                                }
+                            }
+                            if (fRefsCache[i] == null) {
+                                fRefsCache = null;
+                                callback.OnError("Cannot read references from repository", null);
+                                return;
                             }
-                        }
-                        if (fRefsCache[i] == null) {
-                            fRefsCache = null;
-                            callback.OnError("Cannot read references from repository", null);
-                            return;
                         }
                     }
                     callback.OnResponse(fRefsCache);
@@ -171,16 +175,15 @@ class HttpGitProtocol extends BaseGitProtocol {
     }
 
     @Override
-    public void PushPack(final GitCommit commit, Pacman pack, final OnStreamResponseListener<Void> response) {
+    public void PushPack(final GitCommit commit, Pacman pack, String branch, final OnStreamResponseListener<Void> response) {
         GetRefs(new OnResponseListener<GitRef[]>() {
             @Override
             public void OnResponse(final GitRef[] result) {
-                final GitRef myRef = GetHeadRef(result);
-                if (myRef == null) {
-                    response.OnError("Branch " +fConfig.GetBranch() + " not found on remote for pushing", null);
-                    return;
-                }
-                response.OnMsg("Pushing over " +myRef.GetBranch() +" revision " +myRef.GetHash());
+                final GitRef myRef = GetRefFromBranchName(result, branch);
+                if (myRef == null)
+                    response.OnMsg("Creating branch " +branch);
+                else
+                    response.OnMsg("Pushing over " +myRef.GetBranch() +" revision " +myRef.GetHash());
                 try {
                     HashMap<String, String> headers = new HashMap<>();
                     headers.put("Content-Type", "application/x-git-receive-pack-request");
@@ -199,7 +202,8 @@ class HttpGitProtocol extends BaseGitProtocol {
                         @Override
                         public void OnResponse(OutputStream result) {
                             try {
-                                result.write(GitLine(myRef.GetHash() + " " + GitSha1.BytesToString(GitSha1.getRawSha1OfPackable(commit)) +" " +myRef.GetBranch()));
+                                String prev = myRef == null ? "0000000000000000000000000000000000000000 " : (myRef.GetHash() +" ");
+                                result.write(GitLine(prev +GitSha1.BytesToString(GitSha1.getRawSha1OfPackable(commit)) +" " +branch));
                                 result.write(GitLine(null));
                                 if (!pack.Write(result)) {
                                     OnError("Pack error", null);
@@ -230,4 +234,9 @@ class HttpGitProtocol extends BaseGitProtocol {
             }
         });
     }
+
+    @Override
+    protected void OnBranchCreated() {
+        fRefsCache = null;
+    }
 }

+ 54 - 57
app/src/main/java/info/knacki/pass/git/SSHGitProtocol.java

@@ -27,7 +27,7 @@ import info.knacki.pass.settings.SettingsManager;
 public class SSHGitProtocol extends BaseGitProtocol {
     private final static Logger log = Logger.getLogger(SSHGitProtocol.class.getName());
     private final SSHUrl fRepoUrl;
-
+    private GitRef[] fRefsCache = null;
     private final class ActiveSSHWrapper {
         final SSHConnection fConnection;
         final OutputStream fStdin;
@@ -42,8 +42,6 @@ public class SSHGitProtocol extends BaseGitProtocol {
         }
     }
 
-    private ActiveSSHWrapper fActiveConnection = null;
-
     private final class SSHUrl implements SSHConnection.ConnectionParams {
         private final String repoHost;
         final String repoName;
@@ -148,8 +146,12 @@ public class SSHGitProtocol extends BaseGitProtocol {
         return references.toArray(refs);
     }
 
-    private void ListenForErrors(InputStream stderr, OnErrorListener OnError) {
-        (new Thread() {
+    private void ListenForErrors(InputStream stderr, OnErrorListener onError) {
+        ListenForErrors(stderr, onError, true);
+    }
+
+    private void ListenForErrors(InputStream stderr, OnErrorListener onError, boolean separateThread) {
+        Thread thread = (new Thread() {
             @Override
             public void run() {
                 BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
@@ -157,36 +159,39 @@ public class SSHGitProtocol extends BaseGitProtocol {
 
                 try {
                     while ((line = reader.readLine()) != null) {
-                        OnError.OnError(line, null);
+                        onError.OnError(line, null);
                     }
                 }
                 catch (IOException e) {
                     log.log(Level.SEVERE, "Cannot read from stderr", e);
                 }
             }
-        }).start();
+        });
+
+        if (separateThread)
+            thread.start();
+        else
+            thread.run();
     }
 
     @Override
     public void GetRefs(OnResponseListener<GitRef[]> callback) {
-        if (fActiveConnection != null) {
-            GitRef[] refs = GetRefs(fActiveConnection, callback);
-            if (refs != null)
-                callback.OnResponse(refs);
-        } else {
-            AppendableInputStream stdin = new AppendableInputStream();
-
-            SSHFactory
-                    .createInstance(fRepoUrl, "git-upload-pack " + fRepoUrl.repoName)
-                    .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
-                        ListenForErrors(stderr, (msg, exc) -> log.severe(msg));
-                        GitRef[] refs = GetRefs(new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr), callback);
-                        connection.disconnect();
-                        if (refs != null)
-                            callback.OnResponse(refs);
-                    })
-                    .connect();
+        if (fRefsCache != null) {
+            callback.OnResponse(fRefsCache);
+            return;
         }
+        AppendableInputStream stdin = new AppendableInputStream();
+
+        SSHFactory
+                .createInstance(fRepoUrl, "git-upload-pack " + fRepoUrl.repoName)
+                .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
+                    ListenForErrors(stderr, (msg, exc) -> log.severe(msg));
+                    fRefsCache = GetRefs(new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr), callback);
+                    connection.disconnect();
+                    if (fRefsCache != null)
+                        callback.OnResponse(fRefsCache);
+                })
+                .connect();
     }
 
     private byte[] PullPackData(ActiveSSHWrapper sshWrapper, String hash, OnStreamResponseListener<byte[]> listener) {
@@ -212,22 +217,17 @@ public class SSHGitProtocol extends BaseGitProtocol {
     }
 
     private void PullPackData(String hash, OnStreamResponseListener<byte[]> response) {
-        if (fActiveConnection != null) {
-            byte[] result = PullPackData(fActiveConnection, hash, response);
-            response.OnResponse(result);
-        } else {
-            AppendableInputStream stdin = new AppendableInputStream();
-
-            SSHFactory
-                    .createInstance(fRepoUrl, "git-upload-pack " + fRepoUrl.repoName)
-                    .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
-                        ListenForErrors(stderr, (msg, exc) -> log.severe(msg));
-                        byte[] data = PullPackData(new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr), hash, response);
-                        connection.disconnect();
-                        response.OnResponse(data);
-                    })
-                    .connect();
-        }
+        AppendableInputStream stdin = new AppendableInputStream();
+
+        SSHFactory
+                .createInstance(fRepoUrl, "git-upload-pack " + fRepoUrl.repoName)
+                .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
+                    ListenForErrors(stderr, (msg, exc) -> log.severe(msg));
+                    byte[] data = PullPackData(new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr), hash, response);
+                    connection.disconnect();
+                    response.OnResponse(data);
+                })
+                .connect();
     }
 
     @Override
@@ -258,24 +258,19 @@ public class SSHGitProtocol extends BaseGitProtocol {
     }
 
     @Override
-    protected void PushPack(GitCommit commit, Pacman pack, OnStreamResponseListener<Void> resp) {
-        if (fActiveConnection != null)
-            fActiveConnection.fConnection.disconnect();
+    protected void PushPack(GitCommit commit, Pacman pack, String branch, OnStreamResponseListener<Void> resp) {
         AppendableInputStream in = new AppendableInputStream();
         SSHFactory
             .createInstance(fRepoUrl, "git-receive-pack " +fRepoUrl.repoName)
             .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
-                fActiveConnection = new ActiveSSHWrapper(connection, in.GetWriter(), stdout, stderr);
-                GitRef ref = GetHeadRef(GetRefs(fActiveConnection, resp));
-                if (ref == null) {
-                    resp.OnError("Cannot push to " +fRepoUrl.repoName +": branch ref not found", null);
-                    return;
-                }
-                ListenForErrors(stdout, (msg, e) -> resp.OnMsg(msg));
+                ActiveSSHWrapper activeSSHWrapper = new ActiveSSHWrapper(connection, in.GetWriter(), stdout, stderr);
+                fRefsCache = null;
+                GitRef ref = GetRefFromBranchName(GetRefs(activeSSHWrapper, resp), branch);
                 ListenForErrors(stderr, (msg, e) -> resp.OnMsg(msg));
                 try {
                     OutputStream w = in.GetWriter();
-                    w.write(GitLine(ref.GetHash() + " " + GitSha1.BytesToString(GitSha1.getRawSha1OfPackable(commit)) + " " + ref.GetBranch()));
+                    String prev = ref == null ? "0000000000000000000000000000000000000000" : ref.GetHash();
+                    w.write(GitLine(prev + " " + GitSha1.BytesToString(GitSha1.getRawSha1OfPackable(commit)) + " " + branch));
                     w.write(GitLine(null));
                     if (!pack.Write(w)) {
                         resp.OnError("Pack error", null);
@@ -285,6 +280,7 @@ public class SSHGitProtocol extends BaseGitProtocol {
                 catch (IOException e) {
                     resp.OnError("Cannot write pack: " +e.getMessage(), e);
                 }
+                ListenForErrors(stdout, (msg, e) -> resp.OnMsg(msg), false);
                 resp.OnResponse(null);
             })
             .connect(in);
@@ -305,8 +301,8 @@ public class SSHGitProtocol extends BaseGitProtocol {
         SSHFactory
             .createInstance(fRepoUrl, "git-upload-pack " +fRepoUrl.repoName)
             .SetOnConnectionReadyListener((connection, stdout, stderr) -> {
-                fActiveConnection = new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr);
-                GitRef ref = GetHeadRef(GetRefs(fActiveConnection, response));
+                ActiveSSHWrapper SSHWrapper = new ActiveSSHWrapper(connection, stdin.GetWriter(), stdout, stderr);
+                GitRef ref = GetHeadRef(GetRefs(SSHWrapper, response));
                 if (ref == null)
                     return;
                 GetHeadCommitFromRef(ref, new OnStreamResponseListener<GitCommit>() {
@@ -317,19 +313,20 @@ public class SSHGitProtocol extends BaseGitProtocol {
 
                     @Override
                     public void OnResponse(GitCommit result) {
-                        fActiveConnection.fConnection.disconnect();
-                        fActiveConnection = null;
                         response.OnResponse(result);
                     }
 
                     @Override
                     public void OnError(String msg, Throwable e) {
-                        fActiveConnection.fConnection.disconnect();
-                        fActiveConnection = null;
                         response.OnError(msg, e);
                     }
                 });
             })
             .connect(stdin);
     }
+
+    @Override
+    protected void OnBranchCreated() {
+        fRefsCache = null;
+    }
 }

+ 8 - 2
app/src/main/java/info/knacki/pass/git/entities/GitCommit.java

@@ -50,7 +50,7 @@ public class GitCommit implements GitPackable {
     protected GitCommit(GitCommit parent, String author, String committer, String msg) {
         fAuthor = author;
         fCommitter = committer;
-        fParent = parent.fHash;
+        fParent = parent != null ? parent.fHash : null;
         fMessage = msg;
         fHash = null;
         fTime = new Date();
@@ -97,7 +97,7 @@ public class GitCommit implements GitPackable {
         if (fTime != null)
             dateStr = " " +(fTime.getTime() / 1000) +" " +(new SimpleDateFormat("Z", Locale.US).format(fTime));
         String data = "tree " +(fTree == null ? GitSha1.BytesToString(fTreeHash) : GitSha1.BytesToString(fTree.GetHash())) +"\n" +
-                "parent " +GetParent() +"\n" +
+                (GetParent() != null ? ("parent " +GetParent() +"\n") : "")+
                 "author " +GetAuthor() +dateStr +"\n" +
                 "committer " +GetCommitter() +dateStr +"\n" +
                 "\n" +
@@ -124,6 +124,12 @@ public class GitCommit implements GitPackable {
             fTree = new GitObject.GitTree(null, parent.GetTree());
         }
 
+        public Builder(String author, String authorEmail, String message) {
+            String aut = author +" <" +authorEmail +">";
+            co = new GitCommit(null, aut, aut, message);
+            fTree = GitObject.GitTree.CreateEmpty();
+        }
+
         public GitCommit Build() {
             co.fTree = fTree;
             for (GitPackable go: PreparePack()) {

+ 5 - 0
app/src/main/java/info/knacki/pass/git/entities/GitObject.java

@@ -139,6 +139,11 @@ public abstract class GitObject implements Comparable<GitObject>, GitPackable {
             }
         }
 
+        public static GitTree CreateEmpty() {
+            GitTree tree = new GitTree(null);
+            return tree.Initialize();
+        }
+
         public GitTree Initialize() {
             fItems = new HashMap<>();
             fItemOrder = new ArrayDeque<>();

+ 75 - 25
app/src/main/java/info/knacki/pass/settings/ui/SettingsActivity.java

@@ -52,6 +52,7 @@ import info.knacki.pass.io.pgp.GPGUtil;
 import info.knacki.pass.settings.SettingsManager;
 import info.knacki.pass.ui.GitPullActivity;
 import info.knacki.pass.ui.alertPrompt.AlertPromptGenerator;
+import info.knacki.pass.ui.alertPrompt.views.SimpleTextEdit;
 import info.knacki.pass.ui.alertPrompt.views.TextView;
 import info.knacki.pass.ui.alertPrompt.views.UsernameAndEmail;
 import info.knacki.pass.ui.passwordPicker.PasswordPickerFactory;
@@ -331,9 +332,20 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
             findPreference(getResources().getString(R.string.id_vcs_git_branches)).setOnPreferenceChangeListener(new PrefListener() {
                 @Override
                 void SavePref(Object o) {
-                    SettingsManager.Git git = (SettingsManager.Git) SettingsManager.GetVCS(getActivity());
-                    git.SetBranch(((String) o).trim());
-                    SettingsManager.SetVCS(getActivity(), git);
+                    String branch = (String) o;
+                    if (branch.equals("0000")) {
+                        AlertPromptGenerator.StaticMake(getActivity())
+                                .setView(new SimpleTextEdit(getActivity())
+                                    .setText("master"))
+                                .setTitle(R.string.new_branch)
+                                .setPositiveButton(R.string.ok, (d, v) -> CreateBranch(((SimpleTextEdit)v).getStr().trim()))
+                                .setNegativeButton(R.string.cancel, null)
+                                .show();
+                    } else {
+                        SettingsManager.Git git = (SettingsManager.Git) SettingsManager.GetVCS(getActivity());
+                        git.SetBranch(branch.trim());
+                        SettingsManager.SetVCS(getActivity(), git);
+                    }
                 }
             });
 
@@ -344,6 +356,35 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
             reload();
         }
 
+        private final void CreateBranch(String branchName) {
+            if (branchName.equals(""))
+                return;
+            final SettingsManager.VCS versioning = SettingsManager.GetVCS(getActivity());
+            if (versioning instanceof SettingsManager.Git) {
+                GitInterface gitInterface;
+                try {
+                    gitInterface = GitInterfaceFactory.factory((SettingsManager.Git) versioning);
+                }
+                catch (GitInterfaceFactory.GitInterfaceException e) {
+                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
+                    return;
+                }
+
+                gitInterface.CreateBranch(branchName, new OnResponseListener<GitRef[]>() {
+                    @Override
+                    public void OnResponse(GitRef[] result) {
+                        // TODO select new branch by default
+                        getActivity().runOnUiThread(() -> PopulateBranches((ListPreference) FindPreference(R.string.id_vcs_git_branches), (SettingsManager.Git) versioning, result));
+                    }
+
+                    @Override
+                    public void OnError(String msg, Throwable e) {
+                        getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show());
+                    }
+                });
+            }
+        }
+
         final SparseArray<Preference> hiddenPreferences = new SparseArray<>();
 
         private void RemovePrefs(int... prefIds) {
@@ -395,7 +436,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
 
                     FindPreference(R.string.id_vcs_git_commit_info_category);
                     ListPreference gitBranches = (ListPreference) FindPreference(R.string.id_vcs_git_branches);
-                    populateBranches(gitBranches, (SettingsManager.Git) versioning);
+                    PopulateBranches(gitBranches, (SettingsManager.Git) versioning);
 
                     FindPreference(R.string.id_vcs_git_pull);
 
@@ -437,13 +478,13 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
             }
         }
 
-        protected void clearBranches(final ListPreference gitBranches) {
+        protected void ClearBranches(final ListPreference gitBranches) {
             gitBranches.setEntryValues(new CharSequence[]{});
             gitBranches.setEntries(new CharSequence[]{});
             gitBranches.setEnabled(false);
         }
 
-        protected void populateBranches(final ListPreference gitBranches, final SettingsManager.Git versioning) {
+        protected void PopulateBranches(final ListPreference gitBranches, final SettingsManager.Git versioning) {
             gitBranches.setEnabled(false);
             if (versioning != null) {
                 GitInterface gitInterface;
@@ -452,30 +493,17 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                 }
                 catch (GitInterfaceFactory.GitInterfaceException e) {
                     Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
-                    clearBranches(gitBranches);
+                    ClearBranches(gitBranches);
                     return;
                 }
                 gitInterface.GetRefs(new OnResponseListener<GitRef[]>() {
                     @Override
                     public void OnResponse(final GitRef[] result) {
                         getActivity().runOnUiThread(() -> {
-                            CharSequence[] refNames = new CharSequence[result.length];
-                            CharSequence[] refs = new CharSequence[result.length];
-                            int selected = -1;
-
-                            for (int i = 0; i < result.length; ++i) {
-                                refNames[i] = result[i].GetBranchName();
-                                refs[i] = result[i].GetBranch();
-                                if (result[i].GetBranch().equals(versioning.GetBranch()))
-                                    selected = i;
-                            }
-                            gitBranches.setEntries(refNames);
-                            gitBranches.setEntryValues(refs);
-                            gitBranches.setEnabled(true);
-                            if (selected >= 0) {
-                                gitBranches.setSummary(refNames[selected]);
-                                gitBranches.setValueIndex(selected);
-                            }
+                            if (result == null)
+                                ClearBranches(gitBranches);
+                            else
+                                PopulateBranches(gitBranches, versioning, result);
                         });
                     }
 
@@ -485,7 +513,29 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                     }
                 });
             } else {
-                clearBranches(gitBranches);
+                ClearBranches(gitBranches);
+            }
+        }
+
+        private void PopulateBranches(ListPreference gitBranches, SettingsManager.Git versioning, GitRef[] result) {
+            CharSequence[] refNames = new CharSequence[result.length +1];
+            CharSequence[] refs = new CharSequence[result.length +1];
+            int selected = -1;
+
+            for (int i = 0; i < result.length; ++i) {
+                refNames[i] = result[i].GetBranchName();
+                refs[i] = result[i].GetBranch();
+                if (result[i].GetBranch().equals(versioning.GetBranch()))
+                    selected = i;
+            }
+            refNames[result.length] = getResources().getString(R.string.new_branch);
+            refs[result.length] = "0000";
+            gitBranches.setEntries(refNames);
+            gitBranches.setEntryValues(refs);
+            gitBranches.setEnabled(true);
+            if (selected >= 0) {
+                gitBranches.setSummary(refNames[selected]);
+                gitBranches.setValueIndex(selected);
             }
         }
 

+ 1 - 0
app/src/main/res/values-fr/lang.xml

@@ -55,6 +55,7 @@
         <item>Fort</item>
         <item>Personalisé</item>
     </array>
+    <string name="new_branch">Nouvelle branche</string>
     <string name="pref_summary_keyboard_enabled"> </string>
     <string name="pref_summary_enable_keyboard">Autoriser le clavier pour la saisie des mots de passes</string>
     <string name="pref_gpg_title_username_mail">Username</string>

+ 1 - 0
app/src/main/res/values/lang.xml

@@ -55,6 +55,7 @@
         <item>Strong</item>
         <item>Custom</item>
     </array>
+    <string name="new_branch">New branch</string>
     <string name="pref_summary_keyboard_enabled">Keyboard is enabled</string>
     <string name="pref_summary_enable_keyboard">Allow pass keyboard</string>
     <string name="pref_gpg_title_username_mail">Username</string>