Month: June 2022

  • Migrating commits to open branch

    I recently had the problem that I was accidentally working on a local branch that was tracking a protected remote branch. In simple words: I was working on the main branch but was not allowed to push to origin/main. This is a good thing because I see the main branch as stable and I would not want anyone to push directly to main for obvious reasons. So when I started working, not realizing, that I am on the main branch, it looked like this:

    So now I have added some code (on my local main branch) and created a new commit:

    Until this point everything was fine but when I tried

    git push origin main

    I got the message

    PS C:\my.repo.name> git add --all
    PS C:\my.repo.name> git commit -m "Doing stuff."
    PS C:\my.repo.name> git push
    ...
     ! [remote rejected] main -> main (TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.)
    error: failed to push some refs to 'ssh://my.devops.server:22/MyCollection/MyProject/_git/my.repo.name'

    Well shit. Now I have my local branch one commit further than my remote and I can’t push them to the remote repository directly. One possibility is to ask the project leader if he would allow me to push to main. Probably he will not. But I found another solution, that worked for me that consists of two steps:

    1. “Export” the unpushable commit to a new branch and push this branch to integrate afterwards via pull|merge request.
    2. Reset the main branch to the same level as the remote branch.

    The first one is quite easy. Find the commit id with “git log” and create a new branch:

    PS C:\my.repo.name> git log
    commit E (HEAD -> main)
    Author: AndreasGottardi <andreas@goa.systems>
    Date:   Fri Jun 17 10:39:16 2022 +0200
    
        Adding comment.

    I have shortened the output. The commit id I used in the example is simply “E” and a real id would look something like “997f11ca98a823aed198df5410f97b769b0b8338”.

    With the found id you can create a new branch with

    git checkout -b "my/new/branch" E

    So basically it looks like this now:

    So now you have exported your commit into a pushable branch and you can push it to remote with

    git push origin "my/new/branch"

    From there on it is the regular procedure of creating a pull request and going through the defined review process to integrate it into “main”.

    The last step is to checkout main and reset it to the previous commit. Find the ID with “git log” and then reset it with

    git reset --hard D

    Now your local branch “main” is back in sync with the remote branch and as soon as the pull|merge request is through you can pull your changes from the remote branch “main” via

    git pull origin main
  • Fix ssh Git connection against Azure DevOps

    I recently had the issue against my on premise Azure DevOps server, that the clone via ssh would fail with the message:

    PS C:\Users\goa> git clone ssh://devops.example.com:22/GoaSystems/Java/_git/my.java.project %SERPROFILE%\workspaces\java\my.java.project
    Unable to negotiate with 1.2.3.4 port 22: no matching host key type found. Their offer: ssh-rsa
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.

    The error can look different depending on if you already have a configuration file for SSH or not. But in any case, if a error is thrown, it is likely that the keys are the culprit. Fortunately this is easy to solve. Create the following file

    notepad %USERPROFILE%\.ssh\config

    and add the following content.

    Host devops.example.com
        User git
        PubkeyAcceptedAlgorithms +ssh-rsa
        HostkeyAlgorithms +ssh-rsa
        AddressFamily inet

    Modify it of course with your specifications (i.e. hostname).

    One could ask why “AddressFamily inet”. Well, shame on me, I don’t have IPv6 not properly set up due to a lack of understanding. So now I rely on IPv4 (what this line configures) in order to not have to wait for a timeout every time I’d like to clone something.

  • Easy PDF form parsing and data handling

    Recently I got a employee registration form in “docx” format and I can only assume that after I wrote all my information into the form HR copied it from there into the corresponding system. I thought to myself: That could be done easier. So I wrote a SpringBoot application, that offers a downloadable PDF form where the data can be inserted and this form then can be uploaded to the application and is processed. And by processed I mean the data is taken an converted into JSON, XML and SQL. That’s enough for the scope of the application and shows how easy it is to do something like this.

    The project can be found here. It is a regular Gradle project that can be build without any editor support.

    gradlew.bat build
    java -jar build/libs/goa.systems.empman-0.0.1.jar

    The page then can be accessed on “http://localhost:8080” and shows this site:

    Download the PDF file via “Download registration form”, fill data and save it:

    Now choose “Browse…” and select the saved form

    Select “Upload”

    The system will provide a additional validation step to make corrections if something is wrong:

    Select “Yes” and the system will now generate a JSON and a XML data structure. Additionally a SQL INSERT command is generated.

    The PDF form was generated with “Libreoffice Writer” following this tutorial. The only convention is, that the text fields must have the prefix “field_”. They look like “field_prename”, “field_surname” and “field_telephone”. The fields are parsed dynamically from the PDF form. So if new fields need to be added, the application does not have to be changed.